diff --git a/Makefile b/Makefile index fd240d9b1..fc3455cf1 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ OBJS = swad_account.o swad_account_database.o swad_action.o swad_agenda.o \ swad_cryptography.o \ swad_database.o swad_date.o swad_degree.o swad_degree_config.o \ swad_degree_database.o swad_degree_type.o swad_department.o \ - swad_department_database.o swad_duplicate.o \ + swad_department_database.o swad_duplicate.o swad_duplicate_database.o \ swad_enrolment.o swad_error.o swad_exam.o swad_exam_log.o \ swad_exam_print.o swad_exam_result.o swad_exam_session.o \ swad_exam_set.o \ diff --git a/swad_account.c b/swad_account.c index d7061f1c0..77c5920fa 100644 --- a/swad_account.c +++ b/swad_account.c @@ -36,7 +36,7 @@ #include "swad_calendar.h" #include "swad_connected_database.h" #include "swad_database.h" -#include "swad_duplicate.h" +#include "swad_duplicate_database.h" #include "swad_enrolment.h" #include "swad_error.h" #include "swad_exam_print.h" diff --git a/swad_changelog.h b/swad_changelog.h index be0bf90ac..aebbb981e 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -602,13 +602,15 @@ TODO: FIX BUG, URGENT! En las fechas como par TODO: En las encuestas, que los estudiantes no puedan ver los resultados hasta que no finalice el plazo. */ -#define Log_PLATFORM_VERSION "SWAD 20.89.1 (2021-06-05)" +#define Log_PLATFORM_VERSION "SWAD 20.90.1 (2021-06-09)" #define CSS_FILE "swad20.45.css" #define JS_FILE "swad20.69.1.js" /* TODO: Rename CENTRE to CENTER in help wiki. TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams + Version 20.90.1: Jun 09, 2021 Queries moved to module swad_duplicate_database. (312962 lines) + Version 20.90: Jun 05, 2021 New module swad_duplicate_database for database queries related to duplicate users. (? lines) Version 20.89.1: Jun 05, 2021 Queries moved to module swad_department_database. (312869 lines) Version 20.89: Jun 02, 2021 New module swad_department_database for database queries related to departments. (312824 lines) Version 20.88.3: Jun 02, 2021 Queries moved to module swad_center_database. (312755 lines) diff --git a/swad_department_database.c b/swad_department_database.c index c291a58a1..e7de6b291 100644 --- a/swad_department_database.c +++ b/swad_department_database.c @@ -25,24 +25,10 @@ /********************************* Headers ***********************************/ /*****************************************************************************/ -// #include // For boolean type -// #include // For NULL -// #include // For calloc -// #include // For string functions - -// #include "swad_box.h" -// #include "swad_constant.h" #include "swad_database.h" #include "swad_department.h" #include "swad_department_database.h" -// #include "swad_error.h" -// #include "swad_form.h" #include "swad_global.h" -// #include "swad_HTML.h" -// #include "swad_institution.h" -// #include "swad_language.h" -// #include "swad_parameter.h" -// #include "swad_string.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ diff --git a/swad_duplicate.c b/swad_duplicate.c index ec813ad3d..fdb71b815 100644 --- a/swad_duplicate.c +++ b/swad_duplicate.c @@ -29,6 +29,7 @@ #include "swad_box.h" #include "swad_database.h" #include "swad_duplicate.h" +#include "swad_duplicate_database.h" #include "swad_enrolment.h" #include "swad_error.h" #include "swad_form.h" @@ -67,8 +68,6 @@ extern struct Globals Gbl; static void Dup_ListSimilarUsrs (void); -static bool Dup_CheckIfUsrIsDup (long UsrCod); - static void Dup_PutButtonToViewSimilarUsrs (const struct UsrData *UsrDat); static void Dup_PutButtonToEliminateUsrAccount (const struct UsrData *UsrDat); static void Dup_PutButtonToRemoveFromListOfDupUsrs (const struct UsrData *UsrDat); @@ -80,23 +79,16 @@ static void Dup_PutButtonToRemoveFromListOfDupUsrs (const struct UsrData *UsrDat void Dup_ReportUsrAsPossibleDuplicate (void) { extern const char *Txt_Thank_you_for_reporting_a_possible_duplicate_user; - bool ItsMe; /***** Get user to be reported as possible duplicate *****/ if (Usr_GetParamOtherUsrCodEncryptedAndGetUsrData ()) { /* Check if it's allowed to me to report users as possible duplicatedr */ - ItsMe = Usr_ItsMe (Gbl.Usrs.Other.UsrDat.UsrCod); - if (!ItsMe && Gbl.Usrs.Me.Role.Logged >= Rol_TCH) + if (Gbl.Usrs.Me.Role.Logged >= Rol_TCH && + !Usr_ItsMe (Gbl.Usrs.Other.UsrDat.UsrCod)) { /***** Insert possible duplicate into database *****/ - DB_QueryREPLACE ("can not report duplicate", - "REPLACE INTO usr_duplicated" - " (UsrCod,InformerCod,InformTime)" - " VALUES" - " (%ld,%ld,NOW())", - Gbl.Usrs.Other.UsrDat.UsrCod, - Gbl.Usrs.Me.UsrDat.UsrCod); + Dup_DB_AddUsrToDuplicated (Gbl.Usrs.Other.UsrDat.UsrCod); /***** Show feedback message *****/ Ale_ShowAlert (Ale_SUCCESS,Txt_Thank_you_for_reporting_a_possible_duplicate_user); @@ -144,104 +136,93 @@ void Dup_ListDuplicateUsrs (void) NULL,NULL, Hlp_USERS_Duplicates_possibly_duplicate_users,Box_NOT_CLOSABLE); - /***** Make query *****/ - NumUsrs = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get possibly duplicate users", - "SELECT UsrCod," // row[0] - "COUNT(*) AS N," // row[1] - "UNIX_TIMESTAMP(MIN(InformTime)) AS T" // row[2] - " FROM usr_duplicated" - " GROUP BY UsrCod" - " ORDER BY N DESC," - "T DESC"); + /***** List possible duplicated users *****/ + if ((NumUsrs = Dup_DB_GetListDuplicateUsrs (&mysql_res))) + { + /***** Initialize field names *****/ + Usr_SetUsrDatMainFieldNames (); - /***** List possible duplicated users *****/ - if (NumUsrs) - { - /***** Initialize field names *****/ - Usr_SetUsrDatMainFieldNames (); + /***** Initialize structure with user's data *****/ + Usr_UsrDataConstructor (&UsrDat); - /***** Initialize structure with user's data *****/ - Usr_UsrDataConstructor (&UsrDat); + /***** Begin table *****/ + HTM_TABLE_BeginCenterPadding (2); - /***** Begin table *****/ - HTM_TABLE_BeginCenterPadding (2); + /***** Heading row with column names *****/ + Gbl.Usrs.Listing.WithPhotos = true; + Usr_WriteHeaderFieldsUsrDat (false); // Columns for the data - /***** Heading row with column names *****/ - Gbl.Usrs.Listing.WithPhotos = true; - Usr_WriteHeaderFieldsUsrDat (false); // Columns for the data + /***** List users *****/ + for (NumUsr = 0, Gbl.RowEvenOdd = 0; + NumUsr < NumUsrs; + NumUsr++) + { + row = mysql_fetch_row (mysql_res); - /***** List users *****/ - for (NumUsr = 0, Gbl.RowEvenOdd = 0; - NumUsr < NumUsrs; - NumUsr++) - { - row = mysql_fetch_row (mysql_res); + /* Get user code (row[0]) */ + UsrDat.UsrCod = Str_ConvertStrCodToLongCod (row[0]); + if (Usr_ChkUsrCodAndGetAllUsrDataFromUsrCod (&UsrDat, + Usr_DONT_GET_PREFS, + Usr_DONT_GET_ROLE_IN_CURRENT_CRS)) + { + /* Get if user has accepted all his/her courses */ + if (Usr_GetNumCrssOfUsr (UsrDat.UsrCod) != 0) + UsrDat.Accepted = (Usr_GetNumCrssOfUsrNotAccepted (UsrDat.UsrCod) == 0); + else + UsrDat.Accepted = false; - /* Get user code (row[0]) */ - UsrDat.UsrCod = Str_ConvertStrCodToLongCod (row[0]); - if (Usr_ChkUsrCodAndGetAllUsrDataFromUsrCod (&UsrDat, - Usr_DONT_GET_PREFS, - Usr_DONT_GET_ROLE_IN_CURRENT_CRS)) - { - /* Get if user has accepted all his/her courses */ - if (Usr_GetNumCrssOfUsr (UsrDat.UsrCod) != 0) - UsrDat.Accepted = (Usr_GetNumCrssOfUsrNotAccepted (UsrDat.UsrCod) == 0); - else - UsrDat.Accepted = false; + /* Write data of this user */ + Usr_WriteRowUsrMainData (NumUsrs - NumUsr,&UsrDat,false,Rol_UNK, + &Gbl.Usrs.Selected); - /* Write data of this user */ - Usr_WriteRowUsrMainData (NumUsrs - NumUsr,&UsrDat,false,Rol_UNK, - &Gbl.Usrs.Selected); + HTM_TR_Begin (NULL); - HTM_TR_Begin (NULL); + HTM_TD_Begin ("colspan=\"2\" class=\"COLOR%u\"",Gbl.RowEvenOdd); + HTM_TD_End (); - HTM_TD_Begin ("colspan=\"2\" class=\"COLOR%u\"",Gbl.RowEvenOdd); - HTM_TD_End (); + HTM_TD_Begin ("colspan=\"%u\" class=\"DAT LM COLOR%u\"", + Usr_NUM_MAIN_FIELDS_DATA_USR-2, + Gbl.RowEvenOdd); - HTM_TD_Begin ("colspan=\"%u\" class=\"DAT LM COLOR%u\"", - Usr_NUM_MAIN_FIELDS_DATA_USR-2, - Gbl.RowEvenOdd); + /* Write number of informants (row[1]) if greater than 1 */ + if (sscanf (row[1],"%u",&NumInformants) != 1) + Err_ShowErrorAndExit ("Wrong number of informants."); + if (NumInformants > 1) + { + HTM_TxtColonNBSP (Txt_Informants); + HTM_Unsigned (NumInformants); + HTM_BR (); + } - /* Write number of informants (row[1]) if greater than 1 */ - if (sscanf (row[1],"%u",&NumInformants) != 1) - Err_ShowErrorAndExit ("Wrong number of informants."); - if (NumInformants > 1) - { - HTM_TxtColonNBSP (Txt_Informants); - HTM_Unsigned (NumInformants); - HTM_BR (); - } + /* Button to view users similar to this */ + Dup_PutButtonToViewSimilarUsrs (&UsrDat); - /* Button to view users similar to this */ - Dup_PutButtonToViewSimilarUsrs (&UsrDat); + /* Button to remove from list of possible duplicate users */ + Dup_PutButtonToRemoveFromListOfDupUsrs (&UsrDat); - /* Button to remove from list of possible duplicate users */ - Dup_PutButtonToRemoveFromListOfDupUsrs (&UsrDat); + HTM_TD_End (); - HTM_TD_End (); + HTM_TR_End (); - HTM_TR_End (); + Gbl.RowEvenOdd = 1 - Gbl.RowEvenOdd; + } + else // User does not exists ==> + // remove user from table of possible duplicate users + Dup_DB_RemoveUsrFromDuplicated (UsrDat.UsrCod); + } - Gbl.RowEvenOdd = 1 - Gbl.RowEvenOdd; - } - else // User does not exists ==> - // remove user from table of possible duplicate users - Dup_DB_RemoveUsrFromDuplicated (UsrDat.UsrCod); - } + /***** End table *****/ + HTM_TABLE_End (); - /***** End table *****/ - HTM_TABLE_End (); + /***** Free memory used for user's data *****/ + Usr_UsrDataDestructor (&UsrDat); + } + else // There are no duplicated users + /***** Show warning indicating no users found *****/ + Usr_ShowWarningNoUsersFound (Rol_UNK); - /***** Free memory used for user's data *****/ - Usr_UsrDataDestructor (&UsrDat); - } - else // There are no duplicated users - /***** Show warning indicating no users found *****/ - Usr_ShowWarningNoUsersFound (Rol_UNK); - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); + /***** Free structure that stores the query result *****/ + DB_FreeMySQLResult (&mysql_res); /***** End box *****/ Box_BoxEnd (); @@ -274,42 +255,8 @@ static void Dup_ListSimilarUsrs (void) NULL,NULL, Hlp_USERS_Duplicates_similar_users,Box_NOT_CLOSABLE); - /***** Make query *****/ - if (Gbl.Usrs.Other.UsrDat.Surname1[0] && - Gbl.Usrs.Other.UsrDat.FrstName[0]) // Name and surname 1 not empty - NumUsrs = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get similar users", - "SELECT DISTINCT UsrCod" - " FROM (SELECT DISTINCT UsrCod" - " FROM usr_ids" - " WHERE UsrID IN" - " (SELECT UsrID" - " FROM usr_ids" - " WHERE UsrCod=%ld)" - " UNION" - " SELECT UsrCod" - " FROM usr_data" - " WHERE Surname1='%s'" - " AND Surname2='%s'" - " AND FirstName='%s')" - " AS U", - Gbl.Usrs.Other.UsrDat.UsrCod, - Gbl.Usrs.Other.UsrDat.Surname1, - Gbl.Usrs.Other.UsrDat.Surname2, - Gbl.Usrs.Other.UsrDat.FrstName); - else - NumUsrs = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get similar users", - "SELECT DISTINCT UsrCod" - " FROM usr_ids" - " WHERE UsrID IN" - " (SELECT UsrID" - " FROM usr_ids" - " WHERE UsrCod=%ld)", - Gbl.Usrs.Other.UsrDat.UsrCod); - /***** List possible similar users *****/ - if (NumUsrs) + if ((NumUsrs = Dup_DB_GetUsrsSimilarTo (&mysql_res,&Gbl.Usrs.Other.UsrDat))) { /***** Initialize field names *****/ Usr_SetUsrDatMainFieldNames (); @@ -320,72 +267,72 @@ static void Dup_ListSimilarUsrs (void) /***** Begin table *****/ HTM_TABLE_BeginCenterPadding (5); - /***** Heading row with column names *****/ - Gbl.Usrs.Listing.WithPhotos = true; - Usr_WriteHeaderFieldsUsrDat (false); // Columns for the data + /***** Heading row with column names *****/ + Gbl.Usrs.Listing.WithPhotos = true; + Usr_WriteHeaderFieldsUsrDat (false); // Columns for the data - /***** List users *****/ - for (NumUsr = 0, Gbl.RowEvenOdd = 0; - NumUsr < NumUsrs; - NumUsr++) - { - /* Get user code */ - UsrDat.UsrCod = DB_GetNextCode (mysql_res); - if (Usr_ChkUsrCodAndGetAllUsrDataFromUsrCod (&UsrDat, - Usr_DONT_GET_PREFS, - Usr_DONT_GET_ROLE_IN_CURRENT_CRS)) - { - /* Get if user has accepted all his/her courses */ - if (Usr_GetNumCrssOfUsr (UsrDat.UsrCod) != 0) - UsrDat.Accepted = (Usr_GetNumCrssOfUsrNotAccepted (UsrDat.UsrCod) == 0); - else - UsrDat.Accepted = false; + /***** List users *****/ + for (NumUsr = 0, Gbl.RowEvenOdd = 0; + NumUsr < NumUsrs; + NumUsr++) + { + /* Get user code */ + UsrDat.UsrCod = DB_GetNextCode (mysql_res); + if (Usr_ChkUsrCodAndGetAllUsrDataFromUsrCod (&UsrDat, + Usr_DONT_GET_PREFS, + Usr_DONT_GET_ROLE_IN_CURRENT_CRS)) + { + /* Get if user has accepted all his/her courses */ + if (Usr_GetNumCrssOfUsr (UsrDat.UsrCod) != 0) + UsrDat.Accepted = (Usr_GetNumCrssOfUsrNotAccepted (UsrDat.UsrCod) == 0); + else + UsrDat.Accepted = false; - /***** Write data of this user *****/ - Usr_WriteRowUsrMainData (NumUsrs - NumUsr,&UsrDat,false,Rol_UNK, - &Gbl.Usrs.Selected); + /***** Write data of this user *****/ + Usr_WriteRowUsrMainData (NumUsrs - NumUsr,&UsrDat,false,Rol_UNK, + &Gbl.Usrs.Selected); - /***** Write user's profile and user's courses *****/ - HTM_TR_Begin (NULL); + /***** Write user's profile and user's courses *****/ + HTM_TR_Begin (NULL); - HTM_TD_Begin ("colspan=\"2\" class=\"COLOR%u\"",Gbl.RowEvenOdd); - HTM_TD_End (); + HTM_TD_Begin ("colspan=\"2\" class=\"COLOR%u\"",Gbl.RowEvenOdd); + HTM_TD_End (); - HTM_TD_Begin ("colspan=\"%u\" class=\"COLOR%u\"", - Usr_NUM_MAIN_FIELDS_DATA_USR-2, - Gbl.RowEvenOdd); - /* Show details of user's profile */ - Prf_ShowDetailsUserProfile (&UsrDat); - /* Write all the courses this user belongs to */ - Crs_GetAndWriteCrssOfAUsr (&UsrDat,Rol_TCH); - Crs_GetAndWriteCrssOfAUsr (&UsrDat,Rol_NET); - Crs_GetAndWriteCrssOfAUsr (&UsrDat,Rol_STD); - HTM_TD_End (); + HTM_TD_Begin ("colspan=\"%u\" class=\"COLOR%u\"", + Usr_NUM_MAIN_FIELDS_DATA_USR-2, + Gbl.RowEvenOdd); + /* Show details of user's profile */ + Prf_ShowDetailsUserProfile (&UsrDat); + /* Write all the courses this user belongs to */ + Crs_GetAndWriteCrssOfAUsr (&UsrDat,Rol_TCH); + Crs_GetAndWriteCrssOfAUsr (&UsrDat,Rol_NET); + Crs_GetAndWriteCrssOfAUsr (&UsrDat,Rol_STD); + HTM_TD_End (); - HTM_TR_End (); + HTM_TR_End (); - /***** Buttons to remove user / mark as not dubplicated *****/ - HTM_TR_Begin (NULL); + /***** Buttons to remove user / mark as not dubplicated *****/ + HTM_TR_Begin (NULL); - HTM_TD_Begin ("colspan=\"2\" class=\"COLOR%u\"",Gbl.RowEvenOdd); - HTM_TD_End (); + HTM_TD_Begin ("colspan=\"2\" class=\"COLOR%u\"",Gbl.RowEvenOdd); + HTM_TD_End (); - HTM_TD_Begin ("colspan=\"%u\" class=\"LT COLOR%u\"", - Usr_NUM_MAIN_FIELDS_DATA_USR-2, - Gbl.RowEvenOdd); - /* Button to remove this user */ - if (Acc_CheckIfICanEliminateAccount (UsrDat.UsrCod)) - Dup_PutButtonToEliminateUsrAccount (&UsrDat); - /* Button to remove from list of possible duplicate users */ - if (Dup_CheckIfUsrIsDup (UsrDat.UsrCod)) - Dup_PutButtonToRemoveFromListOfDupUsrs (&UsrDat); - HTM_TD_End (); + HTM_TD_Begin ("colspan=\"%u\" class=\"LT COLOR%u\"", + Usr_NUM_MAIN_FIELDS_DATA_USR-2, + Gbl.RowEvenOdd); + /* Button to remove this user */ + if (Acc_CheckIfICanEliminateAccount (UsrDat.UsrCod)) + Dup_PutButtonToEliminateUsrAccount (&UsrDat); + /* Button to remove from list of possible duplicate users */ + if (Dup_DB_CheckIfUsrIsDup (UsrDat.UsrCod)) + Dup_PutButtonToRemoveFromListOfDupUsrs (&UsrDat); + HTM_TD_End (); - HTM_TR_End (); + HTM_TR_End (); - Gbl.RowEvenOdd = 1 - Gbl.RowEvenOdd; - } - } + Gbl.RowEvenOdd = 1 - Gbl.RowEvenOdd; + } + } /***** End table *****/ HTM_TABLE_End (); @@ -404,20 +351,6 @@ static void Dup_ListSimilarUsrs (void) Box_BoxEnd (); } -/*****************************************************************************/ -/********** Check if a user is in list of possible duplicate users ***********/ -/*****************************************************************************/ - -static bool Dup_CheckIfUsrIsDup (long UsrCod) - { - return (DB_QueryCOUNT ("can not if user is in list" - " of possible duplicate users", - "SELECT COUNT(*)" - " FROM usr_duplicated" - " WHERE UsrCod=%ld", - UsrCod) != 0); - } - /*****************************************************************************/ /********************* Put button to view similar users **********************/ /*****************************************************************************/ @@ -428,7 +361,7 @@ static void Dup_PutButtonToViewSimilarUsrs (const struct UsrData *UsrDat) Frm_BeginForm (ActLstSimUsr); Usr_PutParamUsrCodEncrypted (UsrDat->EnUsrCod); - Btn_PutConfirmButtonInline (Txt_Similar_users); + Btn_PutConfirmButtonInline (Txt_Similar_users); Frm_EndForm (); } @@ -443,7 +376,7 @@ static void Dup_PutButtonToEliminateUsrAccount (const struct UsrData *UsrDat) Frm_BeginForm (ActUpdOth); Usr_PutParamUsrCodEncrypted (UsrDat->EnUsrCod); Par_PutHiddenParamUnsigned (NULL,"RegRemAction",(unsigned) Enr_ELIMINATE_ONE_USR_FROM_PLATFORM); - Btn_PutRemoveButtonInline (Txt_Eliminate_user_account); + Btn_PutRemoveButtonInline (Txt_Eliminate_user_account); Frm_EndForm (); } @@ -457,7 +390,7 @@ static void Dup_PutButtonToRemoveFromListOfDupUsrs (const struct UsrData *UsrDat Frm_BeginForm (ActRemDupUsr); Usr_PutParamUsrCodEncrypted (UsrDat->EnUsrCod); - Btn_PutConfirmButtonInline (Txt_Not_duplicated); + Btn_PutConfirmButtonInline (Txt_Not_duplicated); Frm_EndForm (); } @@ -473,24 +406,9 @@ void Dup_RemoveUsrFromListDupUsrs (void) /* Remove entry from database */ Dup_DB_RemoveUsrFromDuplicated (Gbl.Usrs.Other.UsrDat.UsrCod); - /* Show list of similar users again */ - // Dup_ListSimilarUsrsInternal (); - /* Show list of possible duplicated users again */ Dup_ListDuplicateUsrs (); } else Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission (); } - -/*****************************************************************************/ -/******* Remove user from list of possible duplicate users in database *******/ -/*****************************************************************************/ - -void Dup_DB_RemoveUsrFromDuplicated (long UsrCod) - { - DB_QueryDELETE ("can not remove a user from possible duplicates", - "DELETE FROM usr_duplicated" - " WHERE UsrCod=%ld", - UsrCod); - } diff --git a/swad_duplicate.h b/swad_duplicate.h index e915a5e9b..cae353cef 100644 --- a/swad_duplicate.h +++ b/swad_duplicate.h @@ -47,6 +47,5 @@ void Dup_ListDuplicateUsrs (void); void Dup_GetUsrCodAndListSimilarUsrs (void); void Dup_RemoveUsrFromListDupUsrs (void); -void Dup_DB_RemoveUsrFromDuplicated (long UsrCod); #endif diff --git a/swad_duplicate_database.c b/swad_duplicate_database.c new file mode 100644 index 000000000..f6e0d512c --- /dev/null +++ b/swad_duplicate_database.c @@ -0,0 +1,164 @@ +// swad_duplicate_database.c: duplicate users operations with database + +/* + SWAD (Shared Workspace At a Distance), + is a web platform developed at the University of Granada (Spain), + and used to support university teaching. + + This file is part of SWAD core. + Copyright (C) 1999-2021 Antonio Caņas Vargas + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General 3 License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ +/*****************************************************************************/ +/*********************************** Headers *********************************/ +/*****************************************************************************/ + +// #include "swad_account.h" +// #include "swad_box.h" +#include "swad_database.h" +// #include "swad_duplicate.h" +// #include "swad_enrolment.h" +// #include "swad_error.h" +// #include "swad_form.h" +#include "swad_global.h" +// #include "swad_HTML.h" +// #include "swad_layout.h" +// #include "swad_profile.h" +// #include "swad_role_type.h" +// #include "swad_user.h" + +/*****************************************************************************/ +/****************************** Public constants *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private constants *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/******************************* Private types *******************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/************** External global variables from others modules ****************/ +/*****************************************************************************/ + +extern struct Globals Gbl; + +/*****************************************************************************/ +/************************** Private global variables *************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private prototypes ****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/******************* Get list of possible duplicate users ********************/ +/*****************************************************************************/ + +unsigned Dup_DB_GetListDuplicateUsrs (MYSQL_RES **mysql_res) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get possibly duplicate users", + "SELECT UsrCod," // row[0] + "COUNT(*) AS N," // row[1] + "UNIX_TIMESTAMP(MIN(InformTime)) AS T" // row[2] + " FROM usr_duplicated" + " GROUP BY UsrCod" + " ORDER BY N DESC," + "T DESC"); + } + +/*****************************************************************************/ +/***************** Get list of users similar to a given one ******************/ +/*****************************************************************************/ + +unsigned Dup_DB_GetUsrsSimilarTo (MYSQL_RES **mysql_res,const struct UsrData *UsrDat) + { + if (UsrDat->Surname1[0] && + UsrDat->FrstName[0]) // Name and surname 1 not empty + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get similar users", + "SELECT DISTINCT UsrCod" + " FROM (SELECT DISTINCT UsrCod" + " FROM usr_ids" + " WHERE UsrID IN" + " (SELECT UsrID" + " FROM usr_ids" + " WHERE UsrCod=%ld)" + " UNION" + " SELECT UsrCod" + " FROM usr_data" + " WHERE Surname1='%s'" + " AND Surname2='%s'" + " AND FirstName='%s')" + " AS U", + UsrDat->UsrCod, + UsrDat->Surname1, + UsrDat->Surname2, + UsrDat->FrstName); + + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get similar users", + "SELECT DISTINCT UsrCod" + " FROM usr_ids" + " WHERE UsrID IN" + " (SELECT UsrID" + " FROM usr_ids" + " WHERE UsrCod=%ld)", + UsrDat->UsrCod); + } + +/*****************************************************************************/ +/********** Check if a user is in list of possible duplicate users ***********/ +/*****************************************************************************/ + +bool Dup_DB_CheckIfUsrIsDup (long UsrCod) + { + return (DB_QueryCOUNT ("can not if user is in list" + " of possible duplicate users", + "SELECT COUNT(*)" + " FROM usr_duplicated" + " WHERE UsrCod=%ld", + UsrCod) != 0); + } + +/*****************************************************************************/ +/******************* Insert possible duplicate into database *****************/ +/*****************************************************************************/ + +void Dup_DB_AddUsrToDuplicated (long UsrCod) + { + DB_QueryREPLACE ("can not report duplicate", + "REPLACE INTO usr_duplicated" + " (UsrCod,InformerCod,InformTime)" + " VALUES" + " (%ld,%ld,NOW())", + UsrCod, + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/******* Remove user from list of possible duplicate users in database *******/ +/*****************************************************************************/ + +void Dup_DB_RemoveUsrFromDuplicated (long UsrCod) + { + DB_QueryDELETE ("can not remove a user from possible duplicates", + "DELETE FROM usr_duplicated" + " WHERE UsrCod=%ld", + UsrCod); + } diff --git a/swad_duplicate_database.h b/swad_duplicate_database.h new file mode 100644 index 000000000..e0c5140ee --- /dev/null +++ b/swad_duplicate_database.h @@ -0,0 +1,52 @@ +// swad_duplicate_database.h: duplicate users operations with database + +#ifndef _SWAD_DUP_DB +#define _SWAD_DUP_DB +/* + SWAD (Shared Workspace At a Distance in Spanish), + is a web platform developed at the University of Granada (Spain), + and used to support university teaching. + + This file is part of SWAD core. + Copyright (C) 1999-2021 Antonio Caņas Vargas + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ +/*****************************************************************************/ +/********************************** Headers **********************************/ +/*****************************************************************************/ + +#include "swad_user.h" + +/*****************************************************************************/ +/****************************** Public constants *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/******************************** Public types *******************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/****************************** Public prototypes ****************************/ +/*****************************************************************************/ + +unsigned Dup_DB_GetListDuplicateUsrs (MYSQL_RES **mysql_res); +unsigned Dup_DB_GetUsrsSimilarTo (MYSQL_RES **mysql_res,const struct UsrData *UsrDat); +bool Dup_DB_CheckIfUsrIsDup (long UsrCod); + +void Dup_DB_AddUsrToDuplicated (long UsrCod); + +void Dup_DB_RemoveUsrFromDuplicated (long UsrCod); + +#endif