swad-core/swad_duplicate.c

467 lines
17 KiB
C
Raw Normal View History

2016-06-16 14:34:17 +02:00
// swad_duplicate.c: duplicate users
/*
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.
2019-01-07 21:52:19 +01:00
Copyright (C) 1999-2019 Antonio Ca<EFBFBD>as Vargas
2016-06-16 14:34:17 +02:00
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 <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/*********************************** Headers *********************************/
/*****************************************************************************/
2016-06-17 23:38:32 +02:00
#include "swad_account.h"
2017-06-10 21:38:10 +02:00
#include "swad_box.h"
2016-06-16 14:34:17 +02:00
#include "swad_database.h"
2016-06-16 19:30:29 +02:00
#include "swad_duplicate.h"
2017-03-30 11:20:06 +02:00
#include "swad_enrolment.h"
2018-11-09 20:47:39 +01:00
#include "swad_form.h"
2016-06-16 14:34:17 +02:00
#include "swad_global.h"
#include "swad_layout.h"
2016-06-17 11:13:11 +02:00
#include "swad_profile.h"
2016-12-13 13:32:19 +01:00
#include "swad_role_type.h"
2017-06-11 20:09:59 +02:00
#include "swad_table.h"
2016-06-16 14:34:17 +02:00
#include "swad_user.h"
/*****************************************************************************/
/****************************** Public constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/****************************** Internal types *******************************/
/*****************************************************************************/
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/************************* Internal global variables *************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
2016-06-18 00:28:59 +02:00
static void Dup_ListSimilarUsrs (void);
2016-06-17 20:59:48 +02:00
static bool Dup_CheckIfUsrIsDup (long UsrCod);
2016-06-17 20:37:42 +02:00
2016-06-18 00:28:59 +02:00
static void Dup_PutButtonToViewSimilarUsrs (const struct UsrData *UsrDat);
static void Dup_PutButtonToEliminateUsrAccount (const struct UsrData *UsrDat);
static void Dup_PutButtonToRemoveFromListOfDupUsrs (const struct UsrData *UsrDat);
2016-06-16 14:34:17 +02:00
/*****************************************************************************/
/******************** Report a user as possible duplicate ********************/
/*****************************************************************************/
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 */
2018-10-10 23:56:42 +02:00
ItsMe = Usr_ItsMe (Gbl.Usrs.Other.UsrDat.UsrCod);
2017-06-04 18:18:54 +02:00
if (!ItsMe && Gbl.Usrs.Me.Role.Logged >= Rol_TCH)
2016-06-16 14:34:17 +02:00
{
/***** Insert possible duplicate into database *****/
2018-11-02 19:37:11 +01:00
DB_QueryINSERT ("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);
2016-06-16 14:34:17 +02:00
/***** Show feedback message *****/
2019-02-16 14:37:34 +01:00
Ale_ShowAlert (Ale_SUCCESS,Txt_Thank_you_for_reporting_a_possible_duplicate_user);
2016-06-16 14:34:17 +02:00
}
else
2019-03-09 20:12:44 +01:00
Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission ();
2016-06-16 14:34:17 +02:00
}
else
2019-03-09 20:12:44 +01:00
Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission ();
2016-06-16 14:34:17 +02:00
}
/*****************************************************************************/
/************ Put a link (form) to list possible duplicate users *************/
/*****************************************************************************/
void Dup_PutLinkToListDupUsrs (void)
{
extern const char *Txt_Duplicate_USERS;
/***** Put form to remove old users *****/
2019-01-12 03:00:59 +01:00
Lay_PutContextualLinkIconText (ActLstDupUsr,NULL,NULL,
"user-friends.svg",
Txt_Duplicate_USERS);
2016-06-16 14:34:17 +02:00
}
/*****************************************************************************/
/*********************** List possible duplicate users ***********************/
/*****************************************************************************/
void Dup_ListDuplicateUsrs (void)
{
2016-11-20 21:15:31 +01:00
extern const char *Hlp_USERS_Duplicates_possibly_duplicate_users;
2016-06-16 14:34:17 +02:00
extern const char *Txt_Possibly_duplicate_users;
extern const char *Txt_Informants;
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumUsrs;
unsigned NumUsr;
struct UsrData UsrDat;
unsigned NumInformants;
2017-06-12 14:16:33 +02:00
/***** Start box with list of possible duplicate users *****/
2017-06-10 21:38:10 +02:00
Box_StartBox (NULL,Txt_Possibly_duplicate_users,NULL,
2017-06-12 15:03:29 +02:00
Hlp_USERS_Duplicates_possibly_duplicate_users,Box_NOT_CLOSABLE);
2016-06-16 14:34:17 +02:00
2018-10-30 17:47:57 +01:00
/***** Make query *****/
NumUsrs = (unsigned) DB_QuerySELECT (&mysql_res,"can not get possibly"
" duplicate users",
2018-11-03 20:52:00 +01:00
"SELECT UsrCod,COUNT(*) AS N,UNIX_TIMESTAMP(MIN(InformTime)) AS T"
2018-10-30 17:47:57 +01:00
" FROM usr_duplicated"
" GROUP BY UsrCod"
" ORDER BY N DESC,T DESC");
2016-06-16 14:34:17 +02:00
/***** List possible duplicated users *****/
if (NumUsrs)
{
/***** Initialize field names *****/
Usr_SetUsrDatMainFieldNames ();
/***** Initialize structure with user's data *****/
Usr_UsrDataConstructor (&UsrDat);
/***** Start table *****/
2017-06-11 20:09:59 +02:00
Tbl_StartTableCenter (2);
2016-06-16 14:34:17 +02:00
/***** Heading row with column names *****/
2016-06-16 18:32:12 +02:00
Gbl.Usrs.Listing.WithPhotos = true;
Usr_WriteHeaderFieldsUsrDat (false); // Columns for the data
2016-06-16 14:34:17 +02:00
/***** 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]);
2019-03-19 13:22:14 +01:00
if (Usr_ChkUsrCodAndGetAllUsrDataFromUsrCod (&UsrDat,Usr_DONT_GET_PREFS))
2016-06-16 14:34:17 +02:00
{
2016-06-17 23:53:14 +02:00
/* Get if user has accepted all his/her courses */
2016-06-16 18:05:23 +02:00
if (Usr_GetNumCrssOfUsr (UsrDat.UsrCod) != 0)
UsrDat.Accepted = (Usr_GetNumCrssOfUsrNotAccepted (UsrDat.UsrCod) == 0);
else
UsrDat.Accepted = false;
2016-06-16 14:34:17 +02:00
/* Write data of this user */
2017-05-30 17:34:49 +02:00
Usr_WriteRowUsrMainData (NumUsrs - NumUsr,&UsrDat,false,Rol_UNK);
2016-06-16 14:34:17 +02:00
2016-06-16 19:16:09 +02:00
fprintf (Gbl.F.Out,"<tr>"
"<td colspan=\"2\" class=\"COLOR%u\"></td>"
"<td colspan=\"%u\""
" class=\"DAT LEFT_MIDDLE COLOR%u\">",
Gbl.RowEvenOdd,
Usr_NUM_MAIN_FIELDS_DATA_USR-2,
Gbl.RowEvenOdd);
2016-06-18 00:28:59 +02:00
/* Write number of informants (row[1]) if greater than 1 */
if (sscanf (row[1],"%u",&NumInformants) != 1)
Lay_ShowErrorAndExit ("Wrong number of informants.");
if (NumInformants > 1)
fprintf (Gbl.F.Out,"%s: %u<br />",
Txt_Informants,
NumInformants);
/* Button to view users similar to this */
Dup_PutButtonToViewSimilarUsrs (&UsrDat);
/* Button to remove from list of possible duplicate users */
Dup_PutButtonToRemoveFromListOfDupUsrs (&UsrDat);
2016-06-16 19:30:29 +02:00
fprintf (Gbl.F.Out,"</td>"
"</tr>");
Gbl.RowEvenOdd = 1 - Gbl.RowEvenOdd;
}
else // User does not exists ==>
// remove user from table of possible duplicate users
Dup_RemoveUsrFromDuplicated (UsrDat.UsrCod);
}
/***** End table *****/
2017-06-11 20:09:59 +02:00
Tbl_EndTable ();
2016-06-16 19:30:29 +02:00
/***** Free memory used for user's data *****/
Usr_UsrDataDestructor (&UsrDat);
}
2017-05-29 21:34:43 +02:00
else // There are no duplicated users
2017-02-09 19:27:18 +01:00
/***** Show warning indicating no users found *****/
2017-05-18 19:13:41 +02:00
Usr_ShowWarningNoUsersFound (Rol_UNK);
2016-06-16 19:30:29 +02:00
2018-10-20 21:19:30 +02:00
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
2017-06-12 14:16:33 +02:00
/***** End box *****/
2017-06-10 21:38:10 +02:00
Box_EndBox ();
2016-06-16 19:30:29 +02:00
}
/*****************************************************************************/
/********************* List similar users to a given one *********************/
/*****************************************************************************/
2016-06-18 00:28:59 +02:00
void Dup_GetUsrCodAndListSimilarUsrs (void)
2016-06-17 20:59:48 +02:00
{
/***** Get user to be removed from list of possible duplicates *****/
if (Usr_GetParamOtherUsrCodEncryptedAndGetUsrData ())
2016-06-18 00:28:59 +02:00
Dup_ListSimilarUsrs ();
2016-06-17 20:59:48 +02:00
else
2019-03-09 20:12:44 +01:00
Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission ();
2016-06-17 20:59:48 +02:00
}
2016-06-18 00:28:59 +02:00
static void Dup_ListSimilarUsrs (void)
2016-06-16 19:30:29 +02:00
{
2017-03-23 11:55:27 +01:00
extern const char *Hlp_USERS_Duplicates_similar_users;
2016-11-20 21:15:31 +01:00
extern const char *Txt_Similar_users;
2016-06-17 01:05:50 +02:00
struct UsrData UsrDat;
2016-06-16 19:30:29 +02:00
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumUsrs;
unsigned NumUsr;
2016-06-17 01:05:50 +02:00
2017-06-12 14:16:33 +02:00
/***** Start box with list of possible duplicate users *****/
2017-06-10 21:38:10 +02:00
Box_StartBox (NULL,Txt_Similar_users,NULL,
2017-06-12 15:03:29 +02:00
Hlp_USERS_Duplicates_similar_users,Box_NOT_CLOSABLE);
2016-06-16 19:30:29 +02:00
2018-10-30 17:47:57 +01:00
/***** Make query *****/
2016-06-18 01:01:26 +02:00
if (Gbl.Usrs.Other.UsrDat.Surname1[0] &&
Gbl.Usrs.Other.UsrDat.FirstName[0]) // Name and surname 1 not empty
2018-10-30 17:47:57 +01:00
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.FirstName);
2016-06-18 01:01:26 +02:00
else
2018-10-30 17:47:57 +01:00
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);
2016-06-16 19:30:29 +02:00
2017-05-29 21:34:43 +02:00
/***** List possible similar users *****/
2016-06-16 19:30:29 +02:00
if (NumUsrs)
{
/***** Initialize field names *****/
Usr_SetUsrDatMainFieldNames ();
/***** Initialize structure with user's data *****/
Usr_UsrDataConstructor (&UsrDat);
/***** Start table *****/
2017-06-11 20:09:59 +02:00
Tbl_StartTableCenter (2);
2016-06-16 19:30:29 +02:00
/***** 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);
/* Get user code (row[0]) */
UsrDat.UsrCod = Str_ConvertStrCodToLongCod (row[0]);
2019-03-19 13:22:14 +01:00
if (Usr_ChkUsrCodAndGetAllUsrDataFromUsrCod (&UsrDat,Usr_DONT_GET_PREFS))
2016-06-16 19:30:29 +02:00
{
2016-06-17 23:53:14 +02:00
/* Get if user has accepted all his/her courses */
2016-06-16 19:30:29 +02:00
if (Usr_GetNumCrssOfUsr (UsrDat.UsrCod) != 0)
UsrDat.Accepted = (Usr_GetNumCrssOfUsrNotAccepted (UsrDat.UsrCod) == 0);
else
UsrDat.Accepted = false;
/* Write data of this user */
2017-05-30 17:34:49 +02:00
Usr_WriteRowUsrMainData (NumUsrs - NumUsr,&UsrDat,false,Rol_UNK);
2016-06-16 19:30:29 +02:00
2016-06-17 10:45:02 +02:00
fprintf (Gbl.F.Out,"<tr>"
"<td colspan=\"2\" class=\"COLOR%u\"></td>"
"<td colspan=\"%u\" class=\"COLOR%u\">",
Gbl.RowEvenOdd,
Usr_NUM_MAIN_FIELDS_DATA_USR-2,
Gbl.RowEvenOdd);
2016-06-17 11:13:11 +02:00
/* Show details of user's profile */
Prf_ShowDetailsUserProfile (&UsrDat);
/* Write all the courses this user belongs to */
2017-05-18 19:13:41 +02:00
Crs_GetAndWriteCrssOfAUsr (&UsrDat,Rol_TCH);
2017-05-21 21:23:13 +02:00
Crs_GetAndWriteCrssOfAUsr (&UsrDat,Rol_NET);
2017-05-18 19:13:41 +02:00
Crs_GetAndWriteCrssOfAUsr (&UsrDat,Rol_STD);
2016-06-17 11:13:11 +02:00
2016-06-17 10:45:02 +02:00
fprintf (Gbl.F.Out,"</td>"
"</tr>");
fprintf (Gbl.F.Out,"<tr>"
"<td colspan=\"2\" class=\"COLOR%u\"></td>"
"<td colspan=\"%u\" class=\"LEFT_TOP COLOR%u\""
" style=\"padding-bottom:20px;\">",
Gbl.RowEvenOdd,
Usr_NUM_MAIN_FIELDS_DATA_USR-2,
Gbl.RowEvenOdd);
2016-06-17 01:05:50 +02:00
/* Button to remove this user */
2016-06-17 23:38:32 +02:00
if (Acc_CheckIfICanEliminateAccount (UsrDat.UsrCod))
2016-06-18 00:28:59 +02:00
Dup_PutButtonToEliminateUsrAccount (&UsrDat);
2016-06-17 01:05:50 +02:00
2016-06-17 20:37:42 +02:00
/* Button to remove from list of possible duplicate users */
2016-06-17 20:59:48 +02:00
if (Dup_CheckIfUsrIsDup (UsrDat.UsrCod))
2016-06-18 00:28:59 +02:00
Dup_PutButtonToRemoveFromListOfDupUsrs (&UsrDat);
2016-06-17 20:37:42 +02:00
2016-06-16 19:16:09 +02:00
fprintf (Gbl.F.Out,"</td>"
"</tr>");
2016-06-16 14:34:17 +02:00
Gbl.RowEvenOdd = 1 - Gbl.RowEvenOdd;
}
}
/***** End table *****/
2017-06-11 20:09:59 +02:00
Tbl_EndTable ();
2016-06-16 14:34:17 +02:00
/***** Free memory used for user's data *****/
Usr_UsrDataDestructor (&UsrDat);
}
2017-05-29 21:34:43 +02:00
else // There are no similar users
2017-02-09 19:27:18 +01:00
/***** Show warning indicating no users found *****/
2017-05-18 19:13:41 +02:00
Usr_ShowWarningNoUsersFound (Rol_UNK);
2016-06-16 14:34:17 +02:00
2018-10-20 21:19:30 +02:00
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
2017-06-12 14:16:33 +02:00
/***** End box *****/
2017-06-10 21:38:10 +02:00
Box_EndBox ();
2016-06-16 14:34:17 +02:00
}
2016-06-17 20:37:42 +02:00
/*****************************************************************************/
/********** Check if a user is in list of possible duplicate users ***********/
/*****************************************************************************/
2016-06-17 20:59:48 +02:00
static bool Dup_CheckIfUsrIsDup (long UsrCod)
2016-06-17 20:37:42 +02:00
{
2018-11-03 15:33:20 +01:00
return (DB_QueryCOUNT ("can not if user is in list"
" of possible duplicate users",
"SELECT COUNT(*) FROM usr_duplicated"
" WHERE UsrCod=%ld",
UsrCod) != 0);
2016-06-17 20:37:42 +02:00
}
2016-06-18 00:28:59 +02:00
/*****************************************************************************/
/********************* Put button to view similar users **********************/
/*****************************************************************************/
static void Dup_PutButtonToViewSimilarUsrs (const struct UsrData *UsrDat)
{
extern const char *Txt_Similar_users;
2018-11-09 20:47:39 +01:00
Frm_StartForm (ActLstSimUsr);
2016-06-18 00:28:59 +02:00
Usr_PutParamUsrCodEncrypted (UsrDat->EncryptedUsrCod);
2017-06-11 19:02:40 +02:00
Btn_PutConfirmButtonInline (Txt_Similar_users);
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2016-06-18 00:28:59 +02:00
}
/*****************************************************************************/
/********* Put button to request the elimination of a user's account *********/
/*****************************************************************************/
static void Dup_PutButtonToEliminateUsrAccount (const struct UsrData *UsrDat)
{
extern const char *Txt_Eliminate_user_account;
2018-11-09 20:47:39 +01:00
Frm_StartForm (ActUpdOth);
2016-06-18 00:28:59 +02:00
Usr_PutParamUsrCodEncrypted (UsrDat->EncryptedUsrCod);
Par_PutHiddenParamUnsigned ("RegRemAction",(unsigned) Enr_ELIMINATE_ONE_USR_FROM_PLATFORM);
2017-06-11 19:02:40 +02:00
Btn_PutRemoveButtonInline (Txt_Eliminate_user_account);
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2016-06-18 00:28:59 +02:00
}
/*****************************************************************************/
/****** Put button to remove user from list of possible duplicate users ******/
/*****************************************************************************/
static void Dup_PutButtonToRemoveFromListOfDupUsrs (const struct UsrData *UsrDat)
{
extern const char *Txt_Not_duplicated;
2018-11-09 20:47:39 +01:00
Frm_StartForm (ActRemDupUsr);
2016-06-18 00:28:59 +02:00
Usr_PutParamUsrCodEncrypted (UsrDat->EncryptedUsrCod);
2017-06-11 19:02:40 +02:00
Btn_PutConfirmButtonInline (Txt_Not_duplicated);
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2016-06-18 00:28:59 +02:00
}
2016-06-16 14:34:17 +02:00
/*****************************************************************************/
2016-06-17 20:59:48 +02:00
/*********** Remove user from list of possible duplicate users ***************/
/*****************************************************************************/
void Dup_RemoveUsrFromListDupUsrs (void)
{
/***** Get user to be removed from list of possible duplicates *****/
if (Usr_GetParamOtherUsrCodEncryptedAndGetUsrData ())
{
/* Remove entry from database */
Dup_RemoveUsrFromDuplicated (Gbl.Usrs.Other.UsrDat.UsrCod);
/* Show list of similar users again */
2016-06-18 00:28:59 +02:00
// Dup_ListSimilarUsrsInternal ();
/* Show list of possible duplicated users again */
Dup_ListDuplicateUsrs ();
2016-06-17 20:59:48 +02:00
}
else
2019-03-09 20:12:44 +01:00
Ale_ShowAlertUserNotFoundOrYouDoNotHavePermission ();
2016-06-17 20:59:48 +02:00
}
/*****************************************************************************/
/******* Remove user from list of possible duplicate users in database *******/
2016-06-16 14:34:17 +02:00
/*****************************************************************************/
void Dup_RemoveUsrFromDuplicated (long UsrCod)
{
2017-03-30 11:20:06 +02:00
/***** Remove user from list of duplicated users *****/
2018-11-02 22:00:31 +01:00
DB_QueryDELETE ("can not remove a user from possible duplicates",
"DELETE FROM usr_duplicated WHERE UsrCod=%ld",UsrCod);
2016-06-16 14:34:17 +02:00
}