diff --git a/Makefile b/Makefile index d88366a3..6b5a3bf5 100644 --- a/Makefile +++ b/Makefile @@ -91,7 +91,7 @@ OBJS = swad_account.o swad_account_database.o swad_action.o swad_admin.o \ swad_timeline_notification.o swad_timeline_post.o \ swad_timeline_publication.o swad_timeline_share.o swad_timeline_user.o \ swad_timeline_who.o swad_timetable.o swad_timetable_database.o \ - swad_user.o \ + swad_user.o swad_user_database.o \ swad_xml.o \ swad_zip.o SOAPOBJS = soap/soapC.o soap/soapServer.o diff --git a/swad_API.c b/swad_API.c index 789aa480..19550aaa 100644 --- a/swad_API.c +++ b/swad_API.c @@ -125,6 +125,7 @@ cp -f /home/acanas/swad/swad/swad /var/www/cgi-bin/ #include "swad_test_config.h" #include "swad_test_visibility.h" #include "swad_user.h" +#include "swad_user_database.h" #include "swad_xml.h" /*****************************************************************************/ diff --git a/swad_account.c b/swad_account.c index 9efbe7c5..545c6bec 100644 --- a/swad_account.c +++ b/swad_account.c @@ -71,6 +71,7 @@ #include "swad_setting_database.h" #include "swad_test_print.h" #include "swad_timeline.h" +#include "swad_user_database.h" /*****************************************************************************/ /****************************** Public constants *****************************/ diff --git a/swad_admin.c b/swad_admin.c index e921cbf3..b2260dd3 100644 --- a/swad_admin.c +++ b/swad_admin.c @@ -28,6 +28,7 @@ #include "swad_admin.h" #include "swad_admin_database.h" #include "swad_global.h" +#include "swad_user_database.h" /*****************************************************************************/ /****************************** Public constants *****************************/ diff --git a/swad_browser.c b/swad_browser.c index fd5985fc..f669a424 100644 --- a/swad_browser.c +++ b/swad_browser.c @@ -42,6 +42,7 @@ #include "swad_browser.h" #include "swad_browser_database.h" #include "swad_config.h" +#include "swad_course_database.h" #include "swad_database.h" #include "swad_enrolment_database.h" #include "swad_error.h" @@ -72,6 +73,7 @@ #include "swad_string.h" #include "swad_timeline.h" #include "swad_timeline_note.h" +#include "swad_user_database.h" #include "swad_zip.h" /*****************************************************************************/ @@ -9694,7 +9696,7 @@ void Brw_RemoveUsrWorksInAllCrss (struct UsrData *UsrDat) struct Crs_Course Crs; /***** Query database *****/ - NumCrss = Usr_GetCrssFromUsr (UsrDat->UsrCod,-1L,&mysql_res); + NumCrss = Crs_DB_GetCrssFromUsr (&mysql_res,UsrDat->UsrCod,-1L); /***** Remove the zone of works of the user in the courses he/she belongs to *****/ for (NumCrs = 0; diff --git a/swad_center_database.c b/swad_center_database.c index d30a7386..2276142b 100644 --- a/swad_center_database.c +++ b/swad_center_database.c @@ -514,6 +514,50 @@ unsigned Ctr_DB_GetNumCtrsWithUsrs (Rol_Role_t Role, SubQuery,(unsigned) Role); } +/*****************************************************************************/ +/***************** Get the centers of a user from database *******************/ +/*****************************************************************************/ +// Returns the number of rows of the result + +unsigned Ctr_DB_GetCtrsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long InsCod) + { + /***** Get from database the centers a user belongs to *****/ + if (InsCod > 0) + return (unsigned) + DB_QuerySELECT (mysql_res,"can not check the centers a user belongs to", + "SELECT ctr_centers.CtrCod," // row[0] + "MAX(crs_users.Role)" // row[1] + " FROM crs_users," + "crs_courses," + "deg_degrees," + "ctr_centers" + " WHERE crs_users.UsrCod=%ld" + " AND crs_users.CrsCod=crs_courses.CrsCod" + " AND crs_courses.DegCod=deg_degrees.DegCod" + " AND deg_degrees.CtrCod=ctr_centers.CtrCod" + " AND ctr_centers.InsCod=%ld" + " GROUP BY ctr_centers.CtrCod" + " ORDER BY ctr_centers.ShortName", + UsrCod, + InsCod); + else + return (unsigned) + DB_QuerySELECT (mysql_res,"can not check the centers a user belongs to", + "SELECT deg_degrees.CtrCod," // row[0] + "MAX(crs_users.Role)" // row[1] + " FROM crs_users," + "crs_courses," + "deg_degrees," + "ctr_centers" + " WHERE crs_users.UsrCod=%ld" + " AND crs_users.CrsCod=crs_courses.CrsCod" + " AND crs_courses.DegCod=deg_degrees.DegCod" + " AND deg_degrees.CtrCod=ctr_centers.CtrCod" + " GROUP BY ctr_centers.CtrCod" + " ORDER BY ctr_centers.ShortName", + UsrCod); + } + /*****************************************************************************/ /******************* Update institution in table of centers ******************/ /*****************************************************************************/ diff --git a/swad_center_database.h b/swad_center_database.h index 89933a6c..e4ea3bd7 100644 --- a/swad_center_database.h +++ b/swad_center_database.h @@ -71,6 +71,8 @@ unsigned Ctr_DB_GetNumCtrsWithCrss (HieLvl_Level_t Scope,long Cod); unsigned Ctr_DB_GetNumCtrsWithUsrs (Rol_Role_t Role, HieLvl_Level_t Scope,long Cod); +unsigned Ctr_DB_GetCtrsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long InsCod); + void Ctr_DB_UpdateCtrIns (long CtrCod,long NewInsCod); void Ctr_DB_UpdateCtrPlc (long CtrCod,long NewPlcCod); void Ctr_DB_UpdateCtrName (long CtrCod,const char *FieldName,const char *NewCtrName); diff --git a/swad_changelog.h b/swad_changelog.h index b98cb27b..ca3c3ffb 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -602,13 +602,14 @@ 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 21.48 (2021-11-03)" +#define Log_PLATFORM_VERSION "SWAD 21.49 (2021-11-03)" #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 21.49: Nov 03, 2021 New module swad_user_database for database queries related to users. (321615 lines) Version 21.48: Nov 03, 2021 New module swad_timetable_database for database queries related to timetables. (321438 lines) Version 21.47.5: Oct 30, 2021 Queries moved to module swad_questions_database. (321308 lines) Version 21.47.4: Oct 30, 2021 Review of comments. (321253 lines) diff --git a/swad_chat.c b/swad_chat.c index 6981edbb..061a1ce7 100644 --- a/swad_chat.c +++ b/swad_chat.c @@ -33,6 +33,7 @@ #include "swad_chat.h" #include "swad_chat_database.h" #include "swad_config.h" +#include "swad_course_database.h" #include "swad_database.h" #include "swad_error.h" #include "swad_form.h" @@ -123,7 +124,7 @@ void Cht_ShowListOfAvailableChatRooms (void) char ThisRoomFullName[Cht_MAX_BYTES_ROOM_FULL_NAME + 1]; /***** Fill the list with the degrees I belong to *****/ - Usr_GetMyDegrees (); + Deg_GetMyDegrees (); /***** Begin box *****/ Box_BoxBegin (NULL,Txt_Chat_rooms, @@ -194,7 +195,9 @@ void Cht_ShowListOfAvailableChatRooms (void) Cht_WriteLinkToChat2 (ThisRoomCode,ThisRoomFullName); /* Get my courses in this degree from database */ - NumCrss = Usr_GetCrssFromUsr (Gbl.Usrs.Me.UsrDat.UsrCod,Deg.DegCod,&mysql_res); + NumCrss = Crs_DB_GetCrssFromUsr (&mysql_res, + Gbl.Usrs.Me.UsrDat.UsrCod, + Deg.DegCod); for (NumCrs = 0; NumCrs < NumCrss; NumCrs++) @@ -402,8 +405,8 @@ void Cht_OpenChatWindow (void) Err_ShowErrorAndExit ("Wrong code of chat room."); /***** Fill the lists with the degrees and courses I belong to *****/ - Usr_GetMyDegrees (); - Usr_GetMyCourses (); + Deg_GetMyDegrees (); + Crs_GetMyCourses (); /***** Build my user's name *****/ Str_Copy (UsrName,Gbl.Usrs.Me.UsrDat.Surname1,sizeof (UsrName) - 1); diff --git a/swad_country_database.c b/swad_country_database.c index 78136c70..091bf734 100644 --- a/swad_country_database.c +++ b/swad_country_database.c @@ -368,6 +368,37 @@ unsigned Cty_DB_GetNumCtysWithUsrs (Rol_Role_t Role, SubQuery,(unsigned) Role); } +/*****************************************************************************/ +/**************** Get the countries of a user from database ******************/ +/*****************************************************************************/ +// Returns the number of rows of the result + +unsigned Cty_DB_GetCtysFromUsr (MYSQL_RES **mysql_res,long UsrCod) + { + extern const char *Lan_STR_LANG_ID[1 + Lan_NUM_LANGUAGES]; + + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get the countries a user belongs to", + "SELECT cty_countrs.CtyCod," // row[0] + "MAX(crs_users.Role)" // row[1] + " FROM crs_users," + "crs_courses," + "deg_degrees," + "ctr_centers," + "ins_instits," + "cty_countrs" + " WHERE crs_users.UsrCod=%ld" + " AND crs_users.CrsCod=crs_courses.CrsCod" + " AND crs_courses.DegCod=deg_degrees.DegCod" + " AND deg_degrees.CtrCod=ctr_centers.CtrCod" + " AND ctr_centers.InsCod=ins_instits.InsCod" + " AND ins_instits.CtyCod=cty_countrs.CtyCod" + " GROUP BY cty_countrs.CtyCod" + " ORDER BY cty_countrs.Name_%s", + UsrCod, + Lan_STR_LANG_ID[Gbl.Prefs.Language]); + } + /*****************************************************************************/ /******************* Check if a numeric country code exists ******************/ /*****************************************************************************/ diff --git a/swad_country_database.h b/swad_country_database.h index 269d63a0..9940bacb 100644 --- a/swad_country_database.h +++ b/swad_country_database.h @@ -58,6 +58,8 @@ unsigned Cty_DB_GetNumCtysWithCrss (void); unsigned Cty_DB_GetNumCtysWithUsrs (Rol_Role_t Role, HieLvl_Level_t Scope,long Cod); +unsigned Cty_DB_GetCtysFromUsr (MYSQL_RES **mysql_res,long UsrCod); + bool Cty_DB_CheckIfNumericCountryCodeExists (long CtyCod); bool Cty_DB_CheckIfAlpha2CountryCodeExists (const char Alpha2[2 + 1]); bool Cty_DB_CheckIfCountryNameExists (Lan_Language_t Language,const char *Name,long CtyCod); diff --git a/swad_course.c b/swad_course.c index fbe0be64..5f78bbfc 100644 --- a/swad_course.c +++ b/swad_course.c @@ -34,10 +34,13 @@ #include "swad_attendance.h" #include "swad_browser_database.h" #include "swad_call_for_exam_database.h" +#include "swad_center_database.h" +#include "swad_country_database.h" #include "swad_course.h" #include "swad_course_config.h" #include "swad_course_database.h" #include "swad_database.h" +#include "swad_degree_database.h" #include "swad_enrolment_database.h" #include "swad_error.h" #include "swad_figure.h" @@ -51,6 +54,7 @@ #include "swad_hierarchy_level.h" #include "swad_HTML.h" #include "swad_info.h" +#include "swad_institution_database.h" #include "swad_logo.h" #include "swad_message.h" #include "swad_notice.h" @@ -213,7 +217,7 @@ static void Crs_WriteListMyCoursesToSelectOne (void) HTM_LI_End (); /***** Get my countries *****/ - NumCtys = Usr_DB_GetCtysFromUsr (Gbl.Usrs.Me.UsrDat.UsrCod,&mysql_resCty); + NumCtys = Cty_DB_GetCtysFromUsr (&mysql_resCty,Gbl.Usrs.Me.UsrDat.UsrCod); for (NumCty = 0; NumCty < NumCtys; NumCty++) @@ -246,8 +250,9 @@ static void Crs_WriteListMyCoursesToSelectOne (void) HTM_LI_End (); /***** Get my institutions in this country *****/ - NumInss = Usr_GetInssFromUsr (Gbl.Usrs.Me.UsrDat.UsrCod, - Hie.Cty.CtyCod,&mysql_resIns); + NumInss = Ins_DB_GetInssFromUsr (&mysql_resIns, + Gbl.Usrs.Me.UsrDat.UsrCod, + Hie.Cty.CtyCod); for (NumIns = 0; NumIns < NumInss; NumIns++) @@ -280,8 +285,9 @@ static void Crs_WriteListMyCoursesToSelectOne (void) HTM_LI_End (); /***** Get my centers in this institution *****/ - NumCtrs = Usr_GetCtrsFromUsr (Gbl.Usrs.Me.UsrDat.UsrCod, - Hie.Ins.InsCod,&mysql_resCtr); + NumCtrs = Ctr_DB_GetCtrsFromUsr (&mysql_resCtr, + Gbl.Usrs.Me.UsrDat.UsrCod, + Hie.Ins.InsCod); for (NumCtr = 0; NumCtr < NumCtrs; NumCtr++) @@ -314,8 +320,9 @@ static void Crs_WriteListMyCoursesToSelectOne (void) HTM_LI_End (); /***** Get my degrees in this center *****/ - NumDegs = Usr_GetDegsFromUsr (Gbl.Usrs.Me.UsrDat.UsrCod, - Hie.Ctr.CtrCod,&mysql_resDeg); + NumDegs = Deg_DB_GetDegsFromUsr (&mysql_resDeg, + Gbl.Usrs.Me.UsrDat.UsrCod, + Hie.Ctr.CtrCod); for (NumDeg = 0; NumDeg < NumDegs; NumDeg++) @@ -348,8 +355,9 @@ static void Crs_WriteListMyCoursesToSelectOne (void) HTM_LI_End (); /***** Get my courses in this degree *****/ - NumCrss = Usr_GetCrssFromUsr (Gbl.Usrs.Me.UsrDat.UsrCod, - Hie.Deg.DegCod,&mysql_resCrs); + NumCrss = Crs_DB_GetCrssFromUsr (&mysql_resCrs, + Gbl.Usrs.Me.UsrDat.UsrCod, + Hie.Deg.DegCod); for (NumCrs = 0; NumCrs < NumCrss; NumCrs++) @@ -782,7 +790,7 @@ void Crs_WriteSelectorMyCoursesInBreadcrumb (void) /***** Fill the list with the courses I belong to, if not filled *****/ if (Gbl.Usrs.Me.Logged) - Usr_GetMyCourses (); + Crs_GetMyCourses (); /***** Begin form *****/ Frm_BeginFormGoTo (Gbl.Usrs.Me.MyCrss.Num ? ActSeeCrsInf : @@ -969,7 +977,7 @@ static bool Crs_ListCoursesOfAYearForSeeing (unsigned Year) } /* Check if this course is one of my courses */ - BgColor = (Usr_CheckIfIBelongToCrs (Crs->CrsCod)) ? "LIGHT_BLUE" : + BgColor = (Crs_CheckIfIBelongToCrs (Crs->CrsCod)) ? "LIGHT_BLUE" : Gbl.ColorRows[Gbl.RowEvenOdd]; HTM_TR_Begin (NULL); @@ -2182,7 +2190,7 @@ void Crs_ContEditAfterChgCrs (void) PutButtonToRequestRegistration = true; break; case Rol_USR: - PutButtonToRequestRegistration = !Usr_CheckIfUsrBelongsToCrs (Gbl.Usrs.Me.UsrDat.UsrCod, + PutButtonToRequestRegistration = !Crs_CheckIfUsrBelongsToCrs (Gbl.Usrs.Me.UsrDat.UsrCod, Crs_EditingCrs->CrsCod, false); break; @@ -2190,7 +2198,7 @@ void Crs_ContEditAfterChgCrs (void) case Rol_NET: case Rol_TCH: if (Crs_EditingCrs->CrsCod != Gbl.Hierarchy.Crs.CrsCod) - PutButtonToRequestRegistration = !Usr_CheckIfUsrBelongsToCrs (Gbl.Usrs.Me.UsrDat.UsrCod, + PutButtonToRequestRegistration = !Crs_CheckIfUsrBelongsToCrs (Gbl.Usrs.Me.UsrDat.UsrCod, Crs_EditingCrs->CrsCod, false); break; @@ -2261,7 +2269,7 @@ static void Crs_PutButtonToRegisterInCrs (void) void Crs_ReqSelectOneOfMyCourses (void) { /***** Fill the list with the courses I belong to, if not filled *****/ - Usr_GetMyCourses (); + Crs_GetMyCourses (); /***** Select one of my courses *****/ if (Gbl.Usrs.Me.MyCrss.Num) diff --git a/swad_course_database.c b/swad_course_database.c index 6fb1d2a3..66b7c35b 100644 --- a/swad_course_database.c +++ b/swad_course_database.c @@ -418,6 +418,44 @@ unsigned Crs_DB_GetNumCrssWithUsrs (Rol_Role_t Role, (unsigned) Role); } +/*****************************************************************************/ +/**************** Get all courses of a user from database ********************/ +/*****************************************************************************/ +// Returns the number of rows of the result + +unsigned Crs_DB_GetCrssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long DegCod) + { + if (DegCod > 0) // Courses in a degree + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get the courses a user belongs to", + "SELECT crs_users.CrsCod," // row[0] + "crs_users.Role," // row[1] + "crs_courses.DegCod" // row[2] + " FROM crs_users," + "crs_courses" + " WHERE crs_users.UsrCod=%ld" + " AND crs_users.CrsCod=crs_courses.CrsCod" + " AND crs_courses.DegCod=%ld" + " ORDER BY crs_courses.ShortName", + UsrCod, + DegCod); + else // All the courses + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get the courses a user belongs to", + "SELECT crs_users.CrsCod," // row[0] + "crs_users.Role," // row[1] + "crs_courses.DegCod" // row[2] + " FROM crs_users," + "crs_courses," + "deg_degrees" + " WHERE crs_users.UsrCod=%ld" + " AND crs_users.CrsCod=crs_courses.CrsCod" + " AND crs_courses.DegCod=deg_degrees.DegCod" + " ORDER BY deg_degrees.ShortName," + "crs_courses.ShortName", + UsrCod); + } + /*****************************************************************************/ /************* Change the institutional course code of a course **************/ /*****************************************************************************/ diff --git a/swad_course_database.h b/swad_course_database.h index 0c5704ae..f0ebb22a 100644 --- a/swad_course_database.h +++ b/swad_course_database.h @@ -68,6 +68,8 @@ unsigned Crs_DB_GetNumCrssInDeg (long DegCod); unsigned Crs_DB_GetNumCrssWithUsrs (Rol_Role_t Role, HieLvl_Level_t Scope,long Cod); +unsigned Crs_DB_GetCrssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long DegCod); + void Crs_DB_UpdateInstitutionalCrsCod (long CrsCod,const char *NewInstitutionalCrsCod); void Crs_DB_UpdateCrsStatus (long CrsCod,Crs_Status_t Status); void Crs_DB_UpdateCrsYear (long CrsCod,unsigned NewYear); diff --git a/swad_degree_database.c b/swad_degree_database.c index df6e56f7..c0428d70 100644 --- a/swad_degree_database.c +++ b/swad_degree_database.c @@ -552,6 +552,45 @@ unsigned Deg_DB_GetNumDegsWithUsrs (Rol_Role_t Role, SubQuery,(unsigned) Role); } +/*****************************************************************************/ +/***************** Get the degrees of a user from database *******************/ +/*****************************************************************************/ +// Returns the number of rows of the result + +unsigned Deg_DB_GetDegsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long CtrCod) + { + if (CtrCod > 0) + return (unsigned) + DB_QuerySELECT (mysql_res,"can not check the degrees a user belongs to", + "SELECT deg_degrees.DegCod," // row[0] + "MAX(crs_users.Role)" // row[1] + " FROM crs_users," + "crs_courses," + "deg_degrees" + " WHERE crs_users.UsrCod=%ld" + " AND crs_users.CrsCod=crs_courses.CrsCod" + " AND crs_courses.DegCod=deg_degrees.DegCod" + " AND deg_degrees.CtrCod=%ld" + " GROUP BY deg_degrees.DegCod" + " ORDER BY deg_degrees.ShortName", + UsrCod, + CtrCod); + else + return (unsigned) + DB_QuerySELECT (mysql_res,"can not check the degrees a user belongs to", + "SELECT deg_degrees.DegCod," // row[0] + "MAX(crs_users.Role)" // row[1] + " FROM crs_users," + "crs_courses," + "deg_degrees" + " WHERE crs_users.UsrCod=%ld" + " AND crs_users.CrsCod=crs_courses.CrsCod" + " AND crs_courses.DegCod=deg_degrees.DegCod" + " GROUP BY deg_degrees.DegCod" + " ORDER BY deg_degrees.ShortName", + UsrCod); + } + /*****************************************************************************/ /******************** Get number of degrees in a country *********************/ /*****************************************************************************/ diff --git a/swad_degree_database.h b/swad_degree_database.h index 3fff300c..ce31e4b8 100644 --- a/swad_degree_database.h +++ b/swad_degree_database.h @@ -72,6 +72,9 @@ unsigned Deg_DB_SearchDegs (MYSQL_RES **mysql_res, unsigned Deg_DB_GetNumDegsWithCrss (HieLvl_Level_t Scope,long Cod); unsigned Deg_DB_GetNumDegsWithUsrs (Rol_Role_t Role, HieLvl_Level_t Scope,long Cod); + +unsigned Deg_DB_GetDegsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long CtrCod); + unsigned Deg_DB_GetNumDegsInCty (long CtyCod); unsigned Deg_DB_GetNumDegsInIns (long InsCod); unsigned Deg_DB_GetNumDegsInCtr (long CtrCod); diff --git a/swad_department.c b/swad_department.c index 6cf393e4..7a3919ca 100644 --- a/swad_department.c +++ b/swad_department.c @@ -196,7 +196,7 @@ void Dpt_SeeDepts (void) HTM_TR_End (); /***** Write teachers of this institution with other department *****/ - NumTchsInsInOtherDpts = Usr_DB_GetNumTchsCurrentInsInDepartment (0); + NumTchsInsInOtherDpts = Dpt_DB_GetNumTchsCurrentInsInDepartment (0); HTM_TR_Begin (NULL); @@ -211,7 +211,7 @@ void Dpt_SeeDepts (void) HTM_TR_End (); /***** Write teachers with no department *****/ - NumTchsInsWithNoDpt = Usr_DB_GetNumTchsCurrentInsInDepartment (-1L); + NumTchsInsWithNoDpt = Dpt_DB_GetNumTchsCurrentInsInDepartment (-1L); HTM_TR_Begin (NULL); diff --git a/swad_department_database.c b/swad_department_database.c index e7de6b29..22d97cd2 100644 --- a/swad_department_database.c +++ b/swad_department_database.c @@ -189,6 +189,35 @@ unsigned Dpt_DB_GetNumDepartmentsInInstitution (long InsCod) InsCod); } +/*****************************************************************************/ +/* Get number of teachers in courses of the current instit. in a department **/ +/*****************************************************************************/ + +unsigned Dpt_DB_GetNumTchsCurrentInsInDepartment (long DptCod) + { + /***** Get the number of teachers + from the current institution in a department *****/ + return (unsigned) + DB_QueryCOUNT ("can not get the number of teachers in a department", + "SELECT COUNT(DISTINCT usr_data.UsrCod)" + " FROM usr_data," + "crs_users," + "crs_courses," + "deg_degrees," + "ctr_centers" + " WHERE usr_data.InsCod=%ld" // User in the current institution... + " AND usr_data.DptCod=%ld" // ...and the specified department... + " AND usr_data.UsrCod=crs_users.UsrCod" // ...who is... + " AND crs_users.Role IN (%u,%u)" // ...a teacher... + " AND crs_users.CrsCod=crs_courses.CrsCod" // ...in a course... + " AND crs_courses.DegCod=deg_degrees.DegCod" // ...of a degree... + " AND deg_degrees.CtrCod=ctr_centers.InsCod" // ...of a center... + " AND ctr_centers.InsCod=%ld", // ...of the current institution + Gbl.Hierarchy.Ins.InsCod,DptCod, + (unsigned) Rol_NET,(unsigned) Rol_TCH, + Gbl.Hierarchy.Ins.InsCod); + } + /*****************************************************************************/ /****************** Update institution in table of departments ***************/ /*****************************************************************************/ diff --git a/swad_department_database.h b/swad_department_database.h index 665237b0..6c8158e6 100644 --- a/swad_department_database.h +++ b/swad_department_database.h @@ -47,6 +47,7 @@ unsigned Dpt_DB_GetListDepartments (MYSQL_RES **mysql_res, unsigned Dpt_DB_GetDataOfDepartmentByCod (MYSQL_RES **mysql_res,long DptCod); bool Dpt_DB_CheckIfDepartmentNameExists (const char *FieldName,const char *Name,long DptCod); unsigned Dpt_DB_GetNumDepartmentsInInstitution (long InsCod); +unsigned Dpt_DB_GetNumTchsCurrentInsInDepartment (long DptCod); void Dpt_DB_UpdateDptIns (long DptCod,long NewInsCod); void Dpt_DB_UpdateDptName (long DptCod,const char *FieldName,const char *NewDptName); diff --git a/swad_enrolment.c b/swad_enrolment.c index 0aff75c0..ce077cef 100644 --- a/swad_enrolment.c +++ b/swad_enrolment.c @@ -64,6 +64,7 @@ #include "swad_setting_database.h" #include "swad_test_print.h" #include "swad_user.h" +#include "swad_user_database.h" /*****************************************************************************/ /****************************** Public constants *****************************/ @@ -2235,7 +2236,7 @@ static void Enr_ShowEnrolmentRequestsGivenRoles (unsigned RolesSelected) DesiredRole = Rol_ConvertUnsignedStrToRole (row[3]); if (UsrExists) - UsrBelongsToCrs = Usr_CheckIfUsrBelongsToCrs (UsrDat.UsrCod, + UsrBelongsToCrs = Crs_CheckIfUsrBelongsToCrs (UsrDat.UsrCod, Crs.CrsCod, false); else @@ -3242,7 +3243,7 @@ static void Enr_EffectivelyRemUsrFromCrs (struct UsrData *UsrDat, /* Fill the list with the courses I belong to */ Gbl.Usrs.Me.MyCrss.Filled = false; - Usr_GetMyCourses (); + Crs_GetMyCourses (); /* Set my roles */ Gbl.Usrs.Me.Role.FromSession = diff --git a/swad_enrolment_database.c b/swad_enrolment_database.c index 59b9d0db..1931af36 100644 --- a/swad_enrolment_database.c +++ b/swad_enrolment_database.c @@ -92,6 +92,24 @@ void Enr_DB_AcceptUsrInCrs (long UsrCod,long CrsCod) UsrCod); } +/*****************************************************************************/ +/******** Get the user's code of a random student from current course ********/ +/*****************************************************************************/ +// Returns user's code or -1 if no user found + +long Enr_DB_GetRamdomStdFromCrs (long CrsCod) + { + return DB_QuerySELECTCode ("can not get a random student from a course", + "SELECT UsrCod" + " FROM crs_users" + " WHERE CrsCod=%ld" + " AND Role=%u" + " ORDER BY RAND()" + " LIMIT 1", + CrsCod, + (unsigned) Rol_STD); + } + /*****************************************************************************/ /*********** Get all user codes belonging to the current course **************/ /*****************************************************************************/ diff --git a/swad_enrolment_database.h b/swad_enrolment_database.h index 850cbe4b..52cc6d3c 100644 --- a/swad_enrolment_database.h +++ b/swad_enrolment_database.h @@ -46,6 +46,7 @@ void Enr_DB_InsertUsrInCurrentCrs (long UsrCod,long CrsCod,Rol_Role_t NewRole, Enr_KeepOrSetAccepted_t KeepOrSetAccepted); void Enr_DB_AcceptUsrInCrs (long UsrCod,long CrsCod); +long Enr_DB_GetRamdomStdFromCrs (long CrsCod); unsigned Enr_DB_GetUsrsFromCurrentCrs (MYSQL_RES **mysql_res); unsigned Enr_DB_GetUsrsFromCurrentCrsExceptMe (MYSQL_RES **mysql_res); unsigned Enr_DB_GetTchsFromCurrentCrsExceptMe (MYSQL_RES **mysql_res); diff --git a/swad_figure.c b/swad_figure.c index 0fa29636..3926de61 100644 --- a/swad_figure.c +++ b/swad_figure.c @@ -71,6 +71,7 @@ #include "swad_timeline.h" #include "swad_timeline_database.h" #include "swad_timeline_note.h" +#include "swad_user_database.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ diff --git a/swad_forum.c b/swad_forum.c index d3bb328a..73e46afa 100644 --- a/swad_forum.c +++ b/swad_forum.c @@ -35,8 +35,11 @@ #include "swad_action.h" #include "swad_box.h" +#include "swad_center_database.h" #include "swad_config.h" +#include "swad_course_database.h" #include "swad_database.h" +#include "swad_degree_database.h" #include "swad_error.h" #include "swad_figure.h" #include "swad_form.h" @@ -1199,7 +1202,7 @@ static void For_ShowForumList (struct For_Forums *Forums) Forums->Thread.ToMove = For_DB_GetThrInMyClipboard (); /***** Fill the list with the institutions I belong to *****/ - Usr_GetMyInstits (); + Ins_GetMyInstits (); /***** Begin box *****/ Box_BoxBegin (NULL,Txt_Forums, @@ -1222,7 +1225,7 @@ static void For_ShowForumList (struct For_Forums *Forums) if (Gbl.Usrs.Me.Role.Logged >= Rol_DEG_ADM) ICanSeeInsForum = true; else - ICanSeeInsForum = Usr_CheckIfIBelongToIns (Gbl.Hierarchy.Ins.InsCod); + ICanSeeInsForum = Ins_CheckIfIBelongToIns (Gbl.Hierarchy.Ins.InsCod); } else ICanSeeInsForum = false; @@ -1235,7 +1238,7 @@ static void For_ShowForumList (struct For_Forums *Forums) if (Gbl.Usrs.Me.Role.Logged >= Rol_DEG_ADM) ICanSeeCtrForum = true; else - ICanSeeCtrForum = Usr_CheckIfIBelongToCtr (Gbl.Hierarchy.Ctr.CtrCod); + ICanSeeCtrForum = Ctr_CheckIfIBelongToCtr (Gbl.Hierarchy.Ctr.CtrCod); /***** Links to forums of current institution *****/ if (For_WriteLinksToInsForums (Forums,Gbl.Hierarchy.Ins.InsCod, @@ -1246,7 +1249,7 @@ static void For_ShowForumList (struct For_Forums *Forums) if (Gbl.Usrs.Me.Role.Logged >= Rol_DEG_ADM) ICanSeeDegForum = true; else - ICanSeeDegForum = Usr_CheckIfIBelongToDeg (Gbl.Hierarchy.Deg.DegCod); + ICanSeeDegForum = Deg_CheckIfIBelongToDeg (Gbl.Hierarchy.Deg.DegCod); /***** Links to forums of current center *****/ if (For_WriteLinksToCtrForums (Forums, @@ -1287,8 +1290,9 @@ static void For_ShowForumList (struct For_Forums *Forums) IsLastItemInLevel); /* Get my centers in this institution from database */ - NumCtrs = Usr_GetCtrsFromUsr (Gbl.Usrs.Me.UsrDat.UsrCod, - InsCod,&mysql_resCtr); + NumCtrs = Ctr_DB_GetCtrsFromUsr (&mysql_resCtr, + Gbl.Usrs.Me.UsrDat.UsrCod, + InsCod); for (NumCtr = 0; NumCtr < NumCtrs; NumCtr++) @@ -1304,46 +1308,46 @@ static void For_ShowForumList (struct For_Forums *Forums) IsLastItemInLevel) > 0) { /* Get my degrees in this institution from database */ - if ((NumDegs = Usr_GetDegsFromUsr (Gbl.Usrs.Me.UsrDat.UsrCod, - CtrCod, - &mysql_resDeg)) > 0) // Degrees found in this center - for (NumDeg = 0; - NumDeg < NumDegs; - NumDeg++) + NumDegs = Deg_DB_GetDegsFromUsr (&mysql_resDeg, + Gbl.Usrs.Me.UsrDat.UsrCod, + CtrCod); + for (NumDeg = 0; + NumDeg < NumDegs; + NumDeg++) + { + /* Get next degree */ + row = mysql_fetch_row (mysql_resDeg); + DegCod = Str_ConvertStrCodToLongCod (row[0]); + + /* Links to forums of this degree */ + if (For_WriteLinksToDegForums (Forums, + DegCod, + (NumDeg == NumDegs - 1), + IsLastItemInLevel) > 0) { - /* Get next degree */ - row = mysql_fetch_row (mysql_resDeg); - DegCod = Str_ConvertStrCodToLongCod (row[0]); - - /* Links to forums of this degree */ - if (For_WriteLinksToDegForums (Forums, - DegCod, - (NumDeg == NumDegs - 1), - IsLastItemInLevel) > 0) + /* Get my courses in this degree from database */ + NumCrss = Crs_DB_GetCrssFromUsr (&mysql_resCrs, + Gbl.Usrs.Me.UsrDat.UsrCod, + DegCod); + for (NumCrs = 0; + NumCrs < NumCrss; + NumCrs++) { - /* Get my courses in this degree from database */ - NumCrss = Usr_GetCrssFromUsr (Gbl.Usrs.Me.UsrDat.UsrCod, - DegCod, - &mysql_resCrs); - for (NumCrs = 0; - NumCrs < NumCrss; - NumCrs++) - { - /* Get next course */ - row = mysql_fetch_row (mysql_resCrs); - CrsCod = Str_ConvertStrCodToLongCod (row[0]); + /* Get next course */ + row = mysql_fetch_row (mysql_resCrs); + CrsCod = Str_ConvertStrCodToLongCod (row[0]); - /* Links to forums of this course */ - For_WriteLinksToCrsForums (Forums, - CrsCod, - (NumCrs == NumCrss - 1), - IsLastItemInLevel); - } - - /* Free structure that stores the query result */ - DB_FreeMySQLResult (&mysql_resCrs); + /* Links to forums of this course */ + For_WriteLinksToCrsForums (Forums, + CrsCod, + (NumCrs == NumCrss - 1), + IsLastItemInLevel); } + + /* Free structure that stores the query result */ + DB_FreeMySQLResult (&mysql_resCrs); } + } /* Free structure that stores the query result */ DB_FreeMySQLResult (&mysql_resDeg); diff --git a/swad_global.c b/swad_global.c index 568a6676..c070141f 100644 --- a/swad_global.c +++ b/swad_global.c @@ -332,10 +332,10 @@ void Gbl_InitializeGlobals (void) Usr_FlushCacheNumUsrsWhoClaimToBelongToIns (); Usr_FlushCacheNumUsrsWhoClaimToBelongToCtr (); Usr_FlushCacheUsrIsSuperuser (); - Usr_FlushCacheUsrBelongsToIns (); - Usr_FlushCacheUsrBelongsToCtr (); - Usr_FlushCacheUsrBelongsToDeg (); - Usr_FlushCacheUsrBelongsToCrs (); + Ins_FlushCacheUsrBelongsToIns (); + Ctr_FlushCacheUsrBelongsToCtr (); + Deg_FlushCacheUsrBelongsToDeg (); + Crs_FlushCacheUsrBelongsToCrs (); Usr_FlushCacheUsrBelongsToCurrentCrs (); Usr_FlushCacheUsrHasAcceptedInCurrentCrs (); Usr_FlushCacheUsrSharesAnyOfMyCrs (); @@ -359,11 +359,11 @@ void Gbl_Cleanup (void) !Gbl.WebService.IsWebService && Act_GetBrowserTab (Gbl.Action.Act) == Act_BRW_1ST_TAB) Ses_DB_RemoveParam (); - Usr_FreeMyCourses (); - Usr_FreeMyDegrees (); - Usr_FreeMyCenters (); - Usr_FreeMyInstits (); - Usr_FreeMyCountrs (); + Crs_FreeMyCourses (); + Deg_FreeMyDegrees (); + Ctr_FreeMyCenters (); + Ins_FreeMyInstits (); + Cty_FreeMyCountrs (); Usr_UsrDataDestructor (&Gbl.Usrs.Me.UsrDat); Usr_UsrDataDestructor (&Gbl.Usrs.Other.UsrDat); Rec_FreeListFields (); diff --git a/swad_group_database.c b/swad_group_database.c index d5604296..6b1c7747 100644 --- a/swad_group_database.c +++ b/swad_group_database.c @@ -306,6 +306,26 @@ unsigned Grp_DB_CountNumUsrsInNoGrpsOfType (Rol_Role_t Role,long GrpTypCod) GrpTypCod); } +/*****************************************************************************/ +/*********** Get the user's code of a random student from a group ************/ +/*****************************************************************************/ +// Returns user's code or -1 if no user found + +long Grp_DB_GetRamdomStdFromGrp (long GrpCod) + { + return DB_QuerySELECTCode ("can not get a random student from a group", + "SELECT grp_users.UsrCod" + " FROM grp_users," + "crs_users" + " WHERE grp_users.GrpCod=%ld" + " AND grp_users.UsrCod=crs_users.UsrCod" + " AND crs_users.Role=%u" + " ORDER BY RAND()" + " LIMIT 1", + GrpCod, + (unsigned) Rol_STD); + } + /*****************************************************************************/ /******** Get all user codes belonging to the current group, except me *******/ /*****************************************************************************/ diff --git a/swad_group_database.h b/swad_group_database.h index a7df8597..fe870dc1 100644 --- a/swad_group_database.h +++ b/swad_group_database.h @@ -59,6 +59,7 @@ bool Grp_DB_CheckIfGrpNameExistsForGrpTyp (long GrpTypCod,const char *GrpName,lo unsigned Grp_DB_CountNumUsrsInGrp (Rol_Role_t Role,long GrpCod); unsigned Grp_DB_CountNumUsrsInNoGrpsOfType (Rol_Role_t Role,long GrpTypCod); +long Grp_DB_GetRamdomStdFromGrp (long GrpCod); unsigned Grp_DB_GetUsrsFromCurrentGrpExceptMe (MYSQL_RES **mysql_res); unsigned Grp_DB_GetTchsFromCurrentGrpExceptMe (MYSQL_RES **mysql_res); diff --git a/swad_institution_database.c b/swad_institution_database.c index e54980dc..05cf2a33 100644 --- a/swad_institution_database.c +++ b/swad_institution_database.c @@ -753,6 +753,53 @@ unsigned Ins_DB_GetNumInnsWithUsrs (Rol_Role_t Role, SubQuery,(unsigned) Role); } +/*****************************************************************************/ +/************** Get the institutions of a user from database *****************/ +/*****************************************************************************/ +// Returns the number of rows of the result + +unsigned Ins_DB_GetInssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long CtyCod) + { + if (CtyCod > 0) + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get the institutions a user belongs to", + "SELECT ins_instits.InsCod," // row[0] + "MAX(crs_users.Role)" // row[1] + " FROM crs_users," + "crs_courses," + "deg_degrees," + "ctr_centers," + "ins_instits" + " WHERE crs_users.UsrCod=%ld" + " AND crs_users.CrsCod=crs_courses.CrsCod" + " AND crs_courses.DegCod=deg_degrees.DegCod" + " AND deg_degrees.CtrCod=ctr_centers.CtrCod" + " AND ctr_centers.InsCod=ins_instits.InsCod" + " AND ins_instits.CtyCod=%ld" + " GROUP BY ins_instits.InsCod" + " ORDER BY ins_instits.ShortName", + UsrCod, + CtyCod); + else + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get the institutions a user belongs to", + "SELECT ins_instits.InsCod," // row[0] + "MAX(crs_users.Role)" // row[1] + " FROM crs_users," + "crs_courses," + "deg_degrees," + "ctr_centers," + "ins_instits" + " WHERE crs_users.UsrCod=%ld" + " AND crs_users.CrsCod=crs_courses.CrsCod" + " AND crs_courses.DegCod=deg_degrees.DegCod" + " AND deg_degrees.CtrCod=ctr_centers.CtrCod" + " AND ctr_centers.InsCod=ins_instits.InsCod" + " GROUP BY ins_instits.InsCod" + " ORDER BY ins_instits.ShortName", + UsrCod); + } + /*****************************************************************************/ /***************************** Remove institution ****************************/ /*****************************************************************************/ diff --git a/swad_institution_database.h b/swad_institution_database.h index 4e3f5181..38a29848 100644 --- a/swad_institution_database.h +++ b/swad_institution_database.h @@ -80,6 +80,8 @@ unsigned Ins_DB_GetNumInssWithCrss (HieLvl_Level_t Scope,long Cod); unsigned Ins_DB_GetNumInnsWithUsrs (Rol_Role_t Role, HieLvl_Level_t Scope,long Cod); +unsigned Ins_DB_GetInssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long CtyCod); + void Ins_DB_RemoveInstitution (long InsCod); #endif diff --git a/swad_mail.c b/swad_mail.c index de954a5e..22849433 100644 --- a/swad_mail.c +++ b/swad_mail.c @@ -1681,17 +1681,17 @@ bool Mai_ICanSeeOtherUsrEmail (const struct UsrData *UsrDat) case Rol_DEG_ADM: /* If I am an administrator of current degree, I only can see the user's email of users from current degree */ - return Usr_CheckIfUsrBelongsToDeg (UsrDat->UsrCod, + return Deg_CheckIfUsrBelongsToDeg (UsrDat->UsrCod, Gbl.Hierarchy.Deg.DegCod); case Rol_CTR_ADM: /* If I am an administrator of current center, I only can see the user's email of users from current center */ - return Usr_CheckIfUsrBelongsToCtr (UsrDat->UsrCod, + return Ctr_CheckIfUsrBelongsToCtr (UsrDat->UsrCod, Gbl.Hierarchy.Ctr.CtrCod); case Rol_INS_ADM: /* If I am an administrator of current institution, I only can see the user's email of users from current institution */ - return Usr_CheckIfUsrBelongsToIns (UsrDat->UsrCod, + return Ins_CheckIfUsrBelongsToIns (UsrDat->UsrCod, Gbl.Hierarchy.Ins.InsCod); case Rol_SYS_ADM: return true; diff --git a/swad_mark.c b/swad_mark.c index 7ef60a10..a93ed6b5 100644 --- a/swad_mark.c +++ b/swad_mark.c @@ -34,6 +34,7 @@ #include // For unlink #include "swad_database.h" +#include "swad_enrolment_database.h" #include "swad_error.h" #include "swad_form.h" #include "swad_global.h" @@ -561,7 +562,7 @@ void Mrk_ShowMyMarks (void) { if (Grp_DB_CountNumUsrsInGrp (Rol_STD,Gbl.Crs.Grps.GrpCod)) // If there are students in this group { - Gbl.Usrs.Other.UsrDat.UsrCod = Usr_DB_GetRamdomStdFromGrp (Gbl.Crs.Grps.GrpCod); + Gbl.Usrs.Other.UsrDat.UsrCod = Grp_DB_GetRamdomStdFromGrp (Gbl.Crs.Grps.GrpCod); UsrDat = &Gbl.Usrs.Other.UsrDat; } else @@ -572,7 +573,7 @@ void Mrk_ShowMyMarks (void) if (Usr_GetNumUsrsInCrss (HieLvl_CRS,Gbl.Hierarchy.Crs.CrsCod, 1 << Rol_STD)) // If there are students in this course { - Gbl.Usrs.Other.UsrDat.UsrCod = Usr_DB_GetRamdomStdFromCrs (Gbl.Hierarchy.Crs.CrsCod); + Gbl.Usrs.Other.UsrDat.UsrCod = Enr_DB_GetRamdomStdFromCrs (Gbl.Hierarchy.Crs.CrsCod); UsrDat = &Gbl.Usrs.Other.UsrDat; } else diff --git a/swad_password.c b/swad_password.c index cde1ae91..7d28ec92 100644 --- a/swad_password.c +++ b/swad_password.c @@ -47,6 +47,7 @@ #include "swad_parameter.h" #include "swad_session_database.h" #include "swad_user.h" +#include "swad_user_database.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ diff --git a/swad_photo.c b/swad_photo.c index 6d180983..6a242d88 100644 --- a/swad_photo.c +++ b/swad_photo.c @@ -1129,7 +1129,7 @@ void Pho_ShowUsrPhoto (const struct UsrData *UsrDat,const char *PhotoURL, } /* Main degree (in which the user has more courses) short name */ - Usr_GetMainDeg (UsrDat->UsrCod,MainDegreeShrtName,&MaxRole); + Deg_GetUsrMainDeg (UsrDat->UsrCod,MainDegreeShrtName,&MaxRole); if (MainDegreeShrtName[0]) { HTM_DIV_Begin ("class=\"ZOOM_TXT_LINE DAT_SMALL\""); diff --git a/swad_profile.c b/swad_profile.c index 3182cefc..9f21b5be 100644 --- a/swad_profile.c +++ b/swad_profile.c @@ -64,6 +64,7 @@ #include "swad_timeline.h" #include "swad_timeline_database.h" #include "swad_user.h" +#include "swad_user_database.h" /*****************************************************************************/ /****************************** Public constants *****************************/ diff --git a/swad_record.c b/swad_record.c index 00ee82db..03e61c47 100644 --- a/swad_record.c +++ b/swad_record.c @@ -62,6 +62,7 @@ #include "swad_setting.h" #include "swad_timetable.h" #include "swad_user.h" +#include "swad_user_database.h" /*****************************************************************************/ /************** External global variables from others modules ****************/ diff --git a/swad_role.c b/swad_role.c index 5173ddbf..4ab65c19 100644 --- a/swad_role.c +++ b/swad_role.c @@ -157,7 +157,7 @@ void Rol_SetMyRoles (void) if (Gbl.Usrs.Me.IBelongToCurrentCrs) Gbl.Usrs.Me.IBelongToCurrentDeg = true; else - Gbl.Usrs.Me.IBelongToCurrentDeg = Usr_CheckIfIBelongToDeg (Gbl.Hierarchy.Deg.DegCod); + Gbl.Usrs.Me.IBelongToCurrentDeg = Deg_CheckIfIBelongToDeg (Gbl.Hierarchy.Deg.DegCod); } else Gbl.Usrs.Me.IBelongToCurrentDeg = false; @@ -168,7 +168,7 @@ void Rol_SetMyRoles (void) if (Gbl.Usrs.Me.IBelongToCurrentDeg) Gbl.Usrs.Me.IBelongToCurrentCtr = true; else - Gbl.Usrs.Me.IBelongToCurrentCtr = Usr_CheckIfIBelongToCtr (Gbl.Hierarchy.Ctr.CtrCod); + Gbl.Usrs.Me.IBelongToCurrentCtr = Ctr_CheckIfIBelongToCtr (Gbl.Hierarchy.Ctr.CtrCod); } else Gbl.Usrs.Me.IBelongToCurrentCtr = false; @@ -179,7 +179,7 @@ void Rol_SetMyRoles (void) if (Gbl.Usrs.Me.IBelongToCurrentCtr) Gbl.Usrs.Me.IBelongToCurrentIns = true; else - Gbl.Usrs.Me.IBelongToCurrentIns = Usr_CheckIfIBelongToIns (Gbl.Hierarchy.Ins.InsCod); + Gbl.Usrs.Me.IBelongToCurrentIns = Ins_CheckIfIBelongToIns (Gbl.Hierarchy.Ins.InsCod); } else Gbl.Usrs.Me.IBelongToCurrentIns = false; @@ -271,7 +271,7 @@ Rol_Role_t Rol_GetMyMaxRoleInIns (long InsCod) if (InsCod > 0) { /***** Fill the list with the institutions I belong to (if not already filled) *****/ - Usr_GetMyInstits (); + Ins_GetMyInstits (); /***** Check if the institution passed as parameter is any of my institutions *****/ for (NumMyIns = 0; @@ -295,7 +295,7 @@ Rol_Role_t Rol_GetMyMaxRoleInCtr (long CtrCod) if (CtrCod > 0) { /***** Fill the list with the centers I belong to (if not already filled) *****/ - Usr_GetMyCenters (); + Ctr_GetMyCenters (); /***** Check if the center passed as parameter is any of my centers *****/ for (NumMyCtr = 0; @@ -321,7 +321,7 @@ Rol_Role_t Rol_GetMyMaxRoleInDeg (long DegCod) return Rol_UNK; /***** Fill the list with the degrees I belong to (if not already filled) *****/ - Usr_GetMyDegrees (); + Deg_GetMyDegrees (); /***** Check if the degree passed as parameter is any of my degrees *****/ for (NumMyDeg = 0; @@ -358,7 +358,7 @@ Rol_Role_t Rol_GetMyRoleInCrs (long CrsCod) /***** 3. Slow check: get my role from list of my courses *****/ /* Fill the list with the courses I belong to (if not already filled) */ - Usr_GetMyCourses (); + Crs_GetMyCourses (); /* Check if the current course is any of my courses */ for (NumMyCrs = 0, Role = Rol_UNK; diff --git a/swad_search.c b/swad_search.c index 051cfc2e..382e56fa 100644 --- a/swad_search.c +++ b/swad_search.c @@ -43,6 +43,7 @@ #include "swad_parameter.h" #include "swad_session_database.h" #include "swad_user.h" +#include "swad_user_database.h" /*****************************************************************************/ /****************************** Public constants *****************************/ diff --git a/swad_survey.c b/swad_survey.c index 9e083dd8..e38f9843 100644 --- a/swad_survey.c +++ b/swad_survey.c @@ -974,16 +974,16 @@ static void Svy_SetAllowedAndHiddenScopes (unsigned *ScopesAllowed, // ...but not belonging to the current course ********* *ScopesAllowed = 1 << HieLvl_SYS; *HiddenAllowed = 0; - if (Usr_CheckIfIBelongToCty (Gbl.Hierarchy.Cty.CtyCod)) + if (Cty_CheckIfIBelongToCty (Gbl.Hierarchy.Cty.CtyCod)) { *ScopesAllowed |= 1 << HieLvl_CTY; - if (Usr_CheckIfIBelongToIns (Gbl.Hierarchy.Ins.InsCod)) + if (Ins_CheckIfIBelongToIns (Gbl.Hierarchy.Ins.InsCod)) { *ScopesAllowed |= 1 << HieLvl_INS; - if (Usr_CheckIfIBelongToCtr (Gbl.Hierarchy.Ctr.CtrCod)) + if (Ctr_CheckIfIBelongToCtr (Gbl.Hierarchy.Ctr.CtrCod)) { *ScopesAllowed |= 1 << HieLvl_CTR; - if (Usr_CheckIfIBelongToDeg (Gbl.Hierarchy.Deg.DegCod)) + if (Deg_CheckIfIBelongToDeg (Gbl.Hierarchy.Deg.DegCod)) *ScopesAllowed |= 1 << HieLvl_DEG; } } @@ -992,16 +992,16 @@ static void Svy_SetAllowedAndHiddenScopes (unsigned *ScopesAllowed, case Rol_STD: // Student in current course ************************** *ScopesAllowed = 1 << HieLvl_SYS; *HiddenAllowed = 0; - if (Usr_CheckIfIBelongToCty (Gbl.Hierarchy.Cty.CtyCod)) + if (Cty_CheckIfIBelongToCty (Gbl.Hierarchy.Cty.CtyCod)) { *ScopesAllowed |= 1 << HieLvl_CTY; - if (Usr_CheckIfIBelongToIns (Gbl.Hierarchy.Ins.InsCod)) + if (Ins_CheckIfIBelongToIns (Gbl.Hierarchy.Ins.InsCod)) { *ScopesAllowed |= 1 << HieLvl_INS; - if (Usr_CheckIfIBelongToCtr (Gbl.Hierarchy.Ctr.CtrCod)) + if (Ctr_CheckIfIBelongToCtr (Gbl.Hierarchy.Ctr.CtrCod)) { *ScopesAllowed |= 1 << HieLvl_CTR; - if (Usr_CheckIfIBelongToDeg (Gbl.Hierarchy.Deg.DegCod)) + if (Deg_CheckIfIBelongToDeg (Gbl.Hierarchy.Deg.DegCod)) { *ScopesAllowed |= 1 << HieLvl_DEG; if (Gbl.Usrs.Me.IBelongToCurrentCrs) @@ -1015,16 +1015,16 @@ static void Svy_SetAllowedAndHiddenScopes (unsigned *ScopesAllowed, case Rol_TCH: // Teacher in current course ************************** *ScopesAllowed = 1 << HieLvl_SYS; *HiddenAllowed = 0; - if (Usr_CheckIfIBelongToCty (Gbl.Hierarchy.Cty.CtyCod)) + if (Cty_CheckIfIBelongToCty (Gbl.Hierarchy.Cty.CtyCod)) { *ScopesAllowed |= 1 << HieLvl_CTY; - if (Usr_CheckIfIBelongToIns (Gbl.Hierarchy.Ins.InsCod)) + if (Ins_CheckIfIBelongToIns (Gbl.Hierarchy.Ins.InsCod)) { *ScopesAllowed |= 1 << HieLvl_INS; - if (Usr_CheckIfIBelongToCtr (Gbl.Hierarchy.Ctr.CtrCod)) + if (Ctr_CheckIfIBelongToCtr (Gbl.Hierarchy.Ctr.CtrCod)) { *ScopesAllowed |= 1 << HieLvl_CTR; - if (Usr_CheckIfIBelongToDeg (Gbl.Hierarchy.Deg.DegCod)) + if (Deg_CheckIfIBelongToDeg (Gbl.Hierarchy.Deg.DegCod)) { *ScopesAllowed |= 1 << HieLvl_DEG; if (Gbl.Usrs.Me.IBelongToCurrentCrs) @@ -1183,19 +1183,19 @@ void Svy_GetDataOfSurveyByCod (struct Svy_Survey *Svy) Svy->Status.IBelongToScope = Gbl.Usrs.Me.Logged; break; case HieLvl_CTY: // Country - Svy->Status.IBelongToScope = Usr_CheckIfIBelongToCty (Svy->Cod); + Svy->Status.IBelongToScope = Cty_CheckIfIBelongToCty (Svy->Cod); break; case HieLvl_INS: // Institution - Svy->Status.IBelongToScope = Usr_CheckIfIBelongToIns (Svy->Cod); + Svy->Status.IBelongToScope = Ins_CheckIfIBelongToIns (Svy->Cod); break; case HieLvl_CTR: // Center - Svy->Status.IBelongToScope = Usr_CheckIfIBelongToCtr (Svy->Cod); + Svy->Status.IBelongToScope = Ctr_CheckIfIBelongToCtr (Svy->Cod); break; case HieLvl_DEG: // Degree - Svy->Status.IBelongToScope = Usr_CheckIfIBelongToDeg (Svy->Cod); + Svy->Status.IBelongToScope = Deg_CheckIfIBelongToDeg (Svy->Cod); break; case HieLvl_CRS: // Course - Svy->Status.IBelongToScope = Usr_CheckIfIBelongToCrs (Svy->Cod) && + Svy->Status.IBelongToScope = Crs_CheckIfIBelongToCrs (Svy->Cod) && Svy_DB_CheckIfICanDoThisSurveyBasedOnGrps (Svy->SvyCod); break; } diff --git a/swad_user.c b/swad_user.c index d27a4e0a..c27291b4 100644 --- a/swad_user.c +++ b/swad_user.c @@ -41,10 +41,13 @@ #include "swad_announcement.h" #include "swad_box.h" #include "swad_calendar.h" +#include "swad_center_database.h" #include "swad_config.h" #include "swad_connected_database.h" +#include "swad_country_database.h" #include "swad_course.h" #include "swad_database.h" +#include "swad_degree_database.h" #include "swad_department.h" #include "swad_duplicate.h" #include "swad_enrolment.h" @@ -60,6 +63,7 @@ #include "swad_hierarchy_level.h" #include "swad_HTML.h" #include "swad_ID.h" +#include "swad_institution_database.h" #include "swad_language.h" #include "swad_mail_database.h" #include "swad_message.h" @@ -79,6 +83,7 @@ #include "swad_setting.h" #include "swad_tab.h" #include "swad_user.h" +#include "swad_user_database.h" /*****************************************************************************/ /****************************** Public constants *****************************/ @@ -904,10 +909,10 @@ void Usr_WriteFirstNameBRSurnames (const struct UsrData *UsrDat) void Usr_FlushCachesUsr (void) { - Usr_FlushCacheUsrBelongsToIns (); - Usr_FlushCacheUsrBelongsToCtr (); - Usr_FlushCacheUsrBelongsToDeg (); - Usr_FlushCacheUsrBelongsToCrs (); + Ins_FlushCacheUsrBelongsToIns (); + Ctr_FlushCacheUsrBelongsToCtr (); + Deg_FlushCacheUsrBelongsToDeg (); + Crs_FlushCacheUsrBelongsToCrs (); Usr_FlushCacheUsrBelongsToCurrentCrs (); Usr_FlushCacheUsrHasAcceptedInCurrentCrs (); Usr_FlushCacheUsrSharesAnyOfMyCrs (); @@ -1019,7 +1024,7 @@ bool Usr_ICanEditOtherUsr (const struct UsrData *UsrDat) case Rol_DEG_ADM: /* If I am an administrator of current degree, I only can edit users from current degree who have accepted */ - if (Usr_CheckIfUsrBelongsToDeg (UsrDat->UsrCod,Gbl.Hierarchy.Deg.DegCod)) + if (Deg_CheckIfUsrBelongsToDeg (UsrDat->UsrCod,Gbl.Hierarchy.Deg.DegCod)) // Degree admins can't edit superusers' data if (!Usr_CheckIfUsrIsSuperuser (UsrDat->UsrCod)) return true; @@ -1027,7 +1032,7 @@ bool Usr_ICanEditOtherUsr (const struct UsrData *UsrDat) case Rol_CTR_ADM: /* If I am an administrator of current center, I only can edit from current center who have accepted */ - if (Usr_CheckIfUsrBelongsToCtr (UsrDat->UsrCod,Gbl.Hierarchy.Ctr.CtrCod)) + if (Ctr_CheckIfUsrBelongsToCtr (UsrDat->UsrCod,Gbl.Hierarchy.Ctr.CtrCod)) // Center admins can't edit superusers' data if (!Usr_CheckIfUsrIsSuperuser (UsrDat->UsrCod)) return true; @@ -1035,7 +1040,7 @@ bool Usr_ICanEditOtherUsr (const struct UsrData *UsrDat) case Rol_INS_ADM: /* If I am an administrator of current institution, I only can edit from current institution who have accepted */ - if (Usr_CheckIfUsrBelongsToIns (UsrDat->UsrCod,Gbl.Hierarchy.Ins.InsCod)) + if (Ins_CheckIfUsrBelongsToIns (UsrDat->UsrCod,Gbl.Hierarchy.Ins.InsCod)) // Institution admins can't edit superusers' data if (!Usr_CheckIfUsrIsSuperuser (UsrDat->UsrCod)) return true; @@ -1486,7 +1491,7 @@ bool Usr_CheckIfUsrSharesAnyOfMyCrs (struct UsrData *UsrDat) /***** 7. Slow check: Get if user shares any course with me from database *****/ /* Fill the list with the courses I belong to (if not already filled) */ - Usr_GetMyCourses (); + Crs_GetMyCourses (); /* Check if user shares any course with me */ Gbl.Cache.UsrSharesAnyOfMyCrs.UsrCod = UsrDat->UsrCod; @@ -1517,7 +1522,7 @@ bool Usr_CheckIfUsrSharesAnyOfMyCrsWithDifferentRole (long UsrCod) /***** 2. Slow check: Get if user shares any course with me with a different role, from database *****/ /* Fill the list with the courses I belong to (if not already filled) */ - Usr_GetMyCourses (); + Crs_GetMyCourses (); /* Remove temporary table if exists */ DB_Query ("can not remove temporary tables", @@ -1554,7 +1559,7 @@ bool Usr_CheckIfUsrSharesAnyOfMyCrsWithDifferentRole (long UsrCod) /**** Get all my countries (those of my courses) and store them in a list ****/ /*****************************************************************************/ -void Usr_GetMyCountrs (void) +void Cty_GetMyCountrs (void) { MYSQL_RES *mysql_res; MYSQL_ROW row; @@ -1568,7 +1573,7 @@ void Usr_GetMyCountrs (void) Gbl.Usrs.Me.MyCtys.Num = 0; /***** Get my institutions from database *****/ - NumCtys = Usr_DB_GetCtysFromUsr (Gbl.Usrs.Me.UsrDat.UsrCod,&mysql_res); + NumCtys = Cty_DB_GetCtysFromUsr (&mysql_res,Gbl.Usrs.Me.UsrDat.UsrCod); for (NumCty = 0; NumCty < NumCtys; NumCty++) @@ -1601,7 +1606,7 @@ void Usr_GetMyCountrs (void) /** Get all my institutions (those of my courses) and store them in a list ***/ /*****************************************************************************/ -void Usr_GetMyInstits (void) +void Ins_GetMyInstits (void) { MYSQL_RES *mysql_res; MYSQL_ROW row; @@ -1615,7 +1620,8 @@ void Usr_GetMyInstits (void) Gbl.Usrs.Me.MyInss.Num = 0; /***** Get my institutions from database *****/ - NumInss = Usr_GetInssFromUsr (Gbl.Usrs.Me.UsrDat.UsrCod,-1L,&mysql_res); + NumInss = Ins_DB_GetInssFromUsr (&mysql_res, + Gbl.Usrs.Me.UsrDat.UsrCod,-1L); for (NumIns = 0; NumIns < NumInss; NumIns++) @@ -1648,7 +1654,7 @@ void Usr_GetMyInstits (void) /***** Get all my centers (those of my courses) and store them in a list *****/ /*****************************************************************************/ -void Usr_GetMyCenters (void) +void Ctr_GetMyCenters (void) { MYSQL_RES *mysql_res; MYSQL_ROW row; @@ -1662,7 +1668,8 @@ void Usr_GetMyCenters (void) Gbl.Usrs.Me.MyCtrs.Num = 0; /***** Get my centers from database *****/ - NumCtrs = Usr_GetCtrsFromUsr (Gbl.Usrs.Me.UsrDat.UsrCod,-1L,&mysql_res); + NumCtrs = Ctr_DB_GetCtrsFromUsr (&mysql_res, + Gbl.Usrs.Me.UsrDat.UsrCod,-1L); for (NumCtr = 0; NumCtr < NumCtrs; NumCtr++) @@ -1695,7 +1702,7 @@ void Usr_GetMyCenters (void) /***** Get all my degrees (those of my courses) and store them in a list *****/ /*****************************************************************************/ -void Usr_GetMyDegrees (void) +void Deg_GetMyDegrees (void) { MYSQL_RES *mysql_res; MYSQL_ROW row; @@ -1709,7 +1716,8 @@ void Usr_GetMyDegrees (void) Gbl.Usrs.Me.MyDegs.Num = 0; /***** Get my degrees from database *****/ - NumDegs = Usr_GetDegsFromUsr (Gbl.Usrs.Me.UsrDat.UsrCod,-1L,&mysql_res); + NumDegs = Deg_DB_GetDegsFromUsr (&mysql_res, + Gbl.Usrs.Me.UsrDat.UsrCod,-1L); for (NumDeg = 0; NumDeg < NumDegs; NumDeg++) @@ -1738,11 +1746,62 @@ void Usr_GetMyDegrees (void) } } +/*****************************************************************************/ +/********* Get the degree in which a user is enroled in more courses *********/ +/*****************************************************************************/ + +void Deg_GetUsrMainDeg (long UsrCod, + char ShrtName[Cns_HIERARCHY_MAX_BYTES_SHRT_NAME + 1], + Rol_Role_t *MaxRole) + { + MYSQL_RES *mysql_res; + MYSQL_ROW row; + + /***** Get a random student from current course from database *****/ + if (DB_QuerySELECT (&mysql_res,"can not get user's main degree", + "SELECT deg_degrees.ShortName," // row[0] + "main_degree.MaxRole" // row[1] + " FROM deg_degrees," + + // The second table contain only one row with the main degree + " (SELECT crs_courses.DegCod AS DegCod," + "MAX(crs_users.Role) AS MaxRole," + "COUNT(*) AS N" + " FROM crs_users," + "crs_courses" + " WHERE crs_users.UsrCod=%ld" + " AND crs_users.CrsCod=crs_courses.CrsCod" + " GROUP BY crs_courses.DegCod" + " ORDER BY N DESC" // Ordered by number of courses in which user is enroled + " LIMIT 1)" // We need only the main degree + " AS main_degree" + + " WHERE deg_degrees.DegCod=main_degree.DegCod", + UsrCod)) + { + row = mysql_fetch_row (mysql_res); + + /* Get degree name (row[0]) */ + Str_Copy (ShrtName,row[0],Cns_HIERARCHY_MAX_BYTES_SHRT_NAME); + + /* Get maximum role (row[1]) */ + *MaxRole = Rol_ConvertUnsignedStrToRole (row[1]); + } + else // User is not enroled in any course + { + ShrtName[0] = '\0'; + *MaxRole = Rol_UNK; + } + + /***** Free structure that stores the query result *****/ + DB_FreeMySQLResult (&mysql_res); + } + /*****************************************************************************/ /*************** Get all my courses and store them in a list *****************/ /*****************************************************************************/ -void Usr_GetMyCourses (void) +void Crs_GetMyCourses (void) { MYSQL_RES *mysql_res; MYSQL_ROW row; @@ -1819,7 +1878,7 @@ void Usr_GetMyCourses (void) /************************ Free the list of my countries ************************/ /*****************************************************************************/ -void Usr_FreeMyCountrs (void) +void Cty_FreeMyCountrs (void) { if (Gbl.Usrs.Me.MyCtys.Filled) { @@ -1833,7 +1892,7 @@ void Usr_FreeMyCountrs (void) /********************* Free the list of my institutions **********************/ /*****************************************************************************/ -void Usr_FreeMyInstits (void) +void Ins_FreeMyInstits (void) { if (Gbl.Usrs.Me.MyInss.Filled) { @@ -1847,7 +1906,7 @@ void Usr_FreeMyInstits (void) /************************ Free the list of my centers ************************/ /*****************************************************************************/ -void Usr_FreeMyCenters (void) +void Ctr_FreeMyCenters (void) { if (Gbl.Usrs.Me.MyCtrs.Filled) { @@ -1861,7 +1920,7 @@ void Usr_FreeMyCenters (void) /************************ Free the list of my degrees ************************/ /*****************************************************************************/ -void Usr_FreeMyDegrees (void) +void Deg_FreeMyDegrees (void) { if (Gbl.Usrs.Me.MyDegs.Filled) { @@ -1875,7 +1934,7 @@ void Usr_FreeMyDegrees (void) /************************ Free the list of my courses ************************/ /*****************************************************************************/ -void Usr_FreeMyCourses (void) +void Crs_FreeMyCourses (void) { if (Gbl.Usrs.Me.MyCrss.Filled) { @@ -1906,14 +1965,14 @@ static void Usr_RemoveTemporaryTableMyCourses (void) /**************** Check if a user belongs to an institution ******************/ /*****************************************************************************/ -void Usr_FlushCacheUsrBelongsToIns (void) +void Ins_FlushCacheUsrBelongsToIns (void) { Gbl.Cache.UsrBelongsToIns.UsrCod = -1L; Gbl.Cache.UsrBelongsToIns.InsCod = -1L; Gbl.Cache.UsrBelongsToIns.Belongs = false; } -bool Usr_CheckIfUsrBelongsToIns (long UsrCod,long InsCod) +bool Ins_CheckIfUsrBelongsToIns (long UsrCod,long InsCod) { /***** 1. Fast check: Trivial case *****/ if (UsrCod <= 0 || @@ -1949,14 +2008,14 @@ bool Usr_CheckIfUsrBelongsToIns (long UsrCod,long InsCod) /******************* Check if a user belongs to a center *********************/ /*****************************************************************************/ -void Usr_FlushCacheUsrBelongsToCtr (void) +void Ctr_FlushCacheUsrBelongsToCtr (void) { Gbl.Cache.UsrBelongsToCtr.UsrCod = -1L; Gbl.Cache.UsrBelongsToCtr.CtrCod = -1L; Gbl.Cache.UsrBelongsToCtr.Belongs = false; } -bool Usr_CheckIfUsrBelongsToCtr (long UsrCod,long CtrCod) +bool Ctr_CheckIfUsrBelongsToCtr (long UsrCod,long CtrCod) { /***** 1. Fast check: Trivial case *****/ if (UsrCod <= 0 || @@ -1991,14 +2050,14 @@ bool Usr_CheckIfUsrBelongsToCtr (long UsrCod,long CtrCod) /******************* Check if a user belongs to a degree *********************/ /*****************************************************************************/ -void Usr_FlushCacheUsrBelongsToDeg (void) +void Deg_FlushCacheUsrBelongsToDeg (void) { Gbl.Cache.UsrBelongsToDeg.UsrCod = -1L; Gbl.Cache.UsrBelongsToDeg.DegCod = -1L; Gbl.Cache.UsrBelongsToDeg.Belongs = false; } -bool Usr_CheckIfUsrBelongsToDeg (long UsrCod,long DegCod) +bool Deg_CheckIfUsrBelongsToDeg (long UsrCod,long DegCod) { /***** 1. Fast check: Trivial case *****/ if (UsrCod <= 0 || @@ -2030,7 +2089,7 @@ bool Usr_CheckIfUsrBelongsToDeg (long UsrCod,long DegCod) /******************** Check if a user belongs to a course ********************/ /*****************************************************************************/ -void Usr_FlushCacheUsrBelongsToCrs (void) +void Crs_FlushCacheUsrBelongsToCrs (void) { Gbl.Cache.UsrBelongsToCrs.UsrCod = -1L; Gbl.Cache.UsrBelongsToCrs.CrsCod = -1L; @@ -2038,7 +2097,7 @@ void Usr_FlushCacheUsrBelongsToCrs (void) Gbl.Cache.UsrBelongsToCrs.Belongs = false; } -bool Usr_CheckIfUsrBelongsToCrs (long UsrCod,long CrsCod, +bool Crs_CheckIfUsrBelongsToCrs (long UsrCod,long CrsCod, bool CountOnlyAcceptedCourses) { const char *SubQuery; @@ -2104,7 +2163,7 @@ bool Usr_CheckIfUsrBelongsToCurrentCrs (const struct UsrData *UsrDat) /***** 4. Fast / slow check: Get if user belongs to current course *****/ Gbl.Cache.UsrBelongsToCurrentCrs.UsrCod = UsrDat->UsrCod; - Gbl.Cache.UsrBelongsToCurrentCrs.Belongs = Usr_CheckIfUsrBelongsToCrs (UsrDat->UsrCod, + Gbl.Cache.UsrBelongsToCurrentCrs.Belongs = Crs_CheckIfUsrBelongsToCrs (UsrDat->UsrCod, Gbl.Hierarchy.Crs.CrsCod, false); return Gbl.Cache.UsrBelongsToCurrentCrs.Belongs; @@ -2135,7 +2194,7 @@ bool Usr_CheckIfUsrHasAcceptedInCurrentCrs (const struct UsrData *UsrDat) /***** 3. Fast / slow check: Get if user belongs to current course and has accepted *****/ Gbl.Cache.UsrHasAcceptedInCurrentCrs.UsrCod = UsrDat->UsrCod; - Gbl.Cache.UsrHasAcceptedInCurrentCrs.Accepted = Usr_CheckIfUsrBelongsToCrs (UsrDat->UsrCod, + Gbl.Cache.UsrHasAcceptedInCurrentCrs.Accepted = Crs_CheckIfUsrBelongsToCrs (UsrDat->UsrCod, Gbl.Hierarchy.Crs.CrsCod, true); return Gbl.Cache.UsrHasAcceptedInCurrentCrs.Accepted; @@ -2145,12 +2204,12 @@ bool Usr_CheckIfUsrHasAcceptedInCurrentCrs (const struct UsrData *UsrDat) /********************** Check if I belong to a country **********************/ /*****************************************************************************/ -bool Usr_CheckIfIBelongToCty (long CtyCod) +bool Cty_CheckIfIBelongToCty (long CtyCod) { unsigned NumMyCty; /***** Fill the list with the institutions I belong to *****/ - Usr_GetMyCountrs (); + Cty_GetMyCountrs (); /***** Check if the country passed as parameter is any of my countries *****/ for (NumMyCty = 0; @@ -2165,12 +2224,12 @@ bool Usr_CheckIfIBelongToCty (long CtyCod) /******************** Check if I belong to an institution ********************/ /*****************************************************************************/ -bool Usr_CheckIfIBelongToIns (long InsCod) +bool Ins_CheckIfIBelongToIns (long InsCod) { unsigned NumMyIns; /***** Fill the list with the institutions I belong to *****/ - Usr_GetMyInstits (); + Ins_GetMyInstits (); /***** Check if the institution passed as parameter is any of my institutions *****/ for (NumMyIns = 0; @@ -2185,12 +2244,12 @@ bool Usr_CheckIfIBelongToIns (long InsCod) /*********************** Check if I belong to a center ***********************/ /*****************************************************************************/ -bool Usr_CheckIfIBelongToCtr (long CtrCod) +bool Ctr_CheckIfIBelongToCtr (long CtrCod) { unsigned NumMyCtr; /***** Fill the list with the centers I belong to *****/ - Usr_GetMyCenters (); + Ctr_GetMyCenters (); /***** Check if the center passed as parameter is any of my centers *****/ for (NumMyCtr = 0; @@ -2205,12 +2264,12 @@ bool Usr_CheckIfIBelongToCtr (long CtrCod) /*********************** Check if I belong to a degree ***********************/ /*****************************************************************************/ -bool Usr_CheckIfIBelongToDeg (long DegCod) +bool Deg_CheckIfIBelongToDeg (long DegCod) { unsigned NumMyDeg; /***** Fill the list with the degrees I belong to *****/ - Usr_GetMyDegrees (); + Deg_GetMyDegrees (); /***** Check if the degree passed as parameter is any of my degrees *****/ for (NumMyDeg = 0; @@ -2225,12 +2284,12 @@ bool Usr_CheckIfIBelongToDeg (long DegCod) /*********************** Check if I belong to a course ***********************/ /*****************************************************************************/ -bool Usr_CheckIfIBelongToCrs (long CrsCod) +bool Crs_CheckIfIBelongToCrs (long CrsCod) { unsigned NumMyCrs; /***** Fill the list with the courses I belong to *****/ - Usr_GetMyCourses (); + Crs_GetMyCourses (); /***** Check if the course passed as parameter is any of my courses *****/ for (NumMyCrs = 0; @@ -2242,275 +2301,6 @@ bool Usr_CheckIfIBelongToCrs (long CrsCod) return false; } -/*****************************************************************************/ -/**************** Get the countries of a user from database ******************/ -/*****************************************************************************/ -// Returns the number of rows of the result - -unsigned Usr_DB_GetCtysFromUsr (long UsrCod,MYSQL_RES **mysql_res) - { - extern const char *Lan_STR_LANG_ID[1 + Lan_NUM_LANGUAGES]; - - /***** Get the institutions a user belongs to from database *****/ - return (unsigned) - DB_QuerySELECT (mysql_res,"can not get the countries a user belongs to", - "SELECT cty_countrs.CtyCod," // row[0] - "MAX(crs_users.Role)" // row[1] - " FROM crs_users," - "crs_courses," - "deg_degrees," - "ctr_centers," - "ins_instits," - "cty_countrs" - " WHERE crs_users.UsrCod=%ld" - " AND crs_users.CrsCod=crs_courses.CrsCod" - " AND crs_courses.DegCod=deg_degrees.DegCod" - " AND deg_degrees.CtrCod=ctr_centers.CtrCod" - " AND ctr_centers.InsCod=ins_instits.InsCod" - " AND ins_instits.CtyCod=cty_countrs.CtyCod" - " GROUP BY cty_countrs.CtyCod" - " ORDER BY cty_countrs.Name_%s", - UsrCod, - Lan_STR_LANG_ID[Gbl.Prefs.Language]); - } - -/*****************************************************************************/ -/************** Get the institutions of a user from database *****************/ -/*****************************************************************************/ -// Returns the number of rows of the result - -unsigned Usr_GetInssFromUsr (long UsrCod,long CtyCod,MYSQL_RES **mysql_res) - { - /***** Get the institutions a user belongs to from database *****/ - if (CtyCod > 0) - return (unsigned) - DB_QuerySELECT (mysql_res,"can not get the institutions a user belongs to", - "SELECT ins_instits.InsCod," // row[0] - "MAX(crs_users.Role)" // row[1] - " FROM crs_users," - "crs_courses," - "deg_degrees," - "ctr_centers," - "ins_instits" - " WHERE crs_users.UsrCod=%ld" - " AND crs_users.CrsCod=crs_courses.CrsCod" - " AND crs_courses.DegCod=deg_degrees.DegCod" - " AND deg_degrees.CtrCod=ctr_centers.CtrCod" - " AND ctr_centers.InsCod=ins_instits.InsCod" - " AND ins_instits.CtyCod=%ld" - " GROUP BY ins_instits.InsCod" - " ORDER BY ins_instits.ShortName", - UsrCod, - CtyCod); - else - return (unsigned) - DB_QuerySELECT (mysql_res,"can not get the institutions a user belongs to", - "SELECT ins_instits.InsCod," // row[0] - "MAX(crs_users.Role)" // row[1] - " FROM crs_users," - "crs_courses," - "deg_degrees," - "ctr_centers," - "ins_instits" - " WHERE crs_users.UsrCod=%ld" - " AND crs_users.CrsCod=crs_courses.CrsCod" - " AND crs_courses.DegCod=deg_degrees.DegCod" - " AND deg_degrees.CtrCod=ctr_centers.CtrCod" - " AND ctr_centers.InsCod=ins_instits.InsCod" - " GROUP BY ins_instits.InsCod" - " ORDER BY ins_instits.ShortName", - UsrCod); - } - -/*****************************************************************************/ -/***************** Get the centers of a user from database *******************/ -/*****************************************************************************/ -// Returns the number of rows of the result - -unsigned Usr_GetCtrsFromUsr (long UsrCod,long InsCod,MYSQL_RES **mysql_res) - { - /***** Get from database the centers a user belongs to *****/ - if (InsCod > 0) - return (unsigned) - DB_QuerySELECT (mysql_res,"can not check the centers a user belongs to", - "SELECT ctr_centers.CtrCod," // row[0] - "MAX(crs_users.Role)" // row[1] - " FROM crs_users," - "crs_courses," - "deg_degrees," - "ctr_centers" - " WHERE crs_users.UsrCod=%ld" - " AND crs_users.CrsCod=crs_courses.CrsCod" - " AND crs_courses.DegCod=deg_degrees.DegCod" - " AND deg_degrees.CtrCod=ctr_centers.CtrCod" - " AND ctr_centers.InsCod=%ld" - " GROUP BY ctr_centers.CtrCod" - " ORDER BY ctr_centers.ShortName", - UsrCod, - InsCod); - else - return (unsigned) - DB_QuerySELECT (mysql_res,"can not check the centers a user belongs to", - "SELECT deg_degrees.CtrCod," // row[0] - "MAX(crs_users.Role)" // row[1] - " FROM crs_users," - "crs_courses," - "deg_degrees," - "ctr_centers" - " WHERE crs_users.UsrCod=%ld" - " AND crs_users.CrsCod=crs_courses.CrsCod" - " AND crs_courses.DegCod=deg_degrees.DegCod" - " AND deg_degrees.CtrCod=ctr_centers.CtrCod" - " GROUP BY ctr_centers.CtrCod" - " ORDER BY ctr_centers.ShortName", - UsrCod); - } - -/*****************************************************************************/ -/***************** Get the degrees of a user from database *******************/ -/*****************************************************************************/ -// Returns the number of rows of the result - -unsigned Usr_GetDegsFromUsr (long UsrCod,long CtrCod,MYSQL_RES **mysql_res) - { - /***** Get from database the degrees a user belongs to *****/ - if (CtrCod > 0) - return (unsigned) - DB_QuerySELECT (mysql_res,"can not check the degrees a user belongs to", - "SELECT deg_degrees.DegCod," // row[0] - "MAX(crs_users.Role)" // row[1] - " FROM crs_users," - "crs_courses," - "deg_degrees" - " WHERE crs_users.UsrCod=%ld" - " AND crs_users.CrsCod=crs_courses.CrsCod" - " AND crs_courses.DegCod=deg_degrees.DegCod" - " AND deg_degrees.CtrCod=%ld" - " GROUP BY deg_degrees.DegCod" - " ORDER BY deg_degrees.ShortName", - UsrCod, - CtrCod); - else - return (unsigned) - DB_QuerySELECT (mysql_res,"can not check the degrees a user belongs to", - "SELECT deg_degrees.DegCod," // row[0] - "MAX(crs_users.Role)" // row[1] - " FROM crs_users," - "crs_courses," - "deg_degrees" - " WHERE crs_users.UsrCod=%ld" - " AND crs_users.CrsCod=crs_courses.CrsCod" - " AND crs_courses.DegCod=deg_degrees.DegCod" - " GROUP BY deg_degrees.DegCod" - " ORDER BY deg_degrees.ShortName", - UsrCod); - } - -/*****************************************************************************/ -/**************** Get all courses of a user from database ********************/ -/*****************************************************************************/ -// Returns the number of rows of the result - -unsigned Usr_GetCrssFromUsr (long UsrCod,long DegCod,MYSQL_RES **mysql_res) - { - /***** Get from database the courses a user belongs to *****/ - if (DegCod > 0) // Courses in a degree - return (unsigned) - DB_QuerySELECT (mysql_res,"can not get the courses a user belongs to", - "SELECT crs_users.CrsCod," // row[0] - "crs_users.Role," // row[1] - "crs_courses.DegCod" // row[2] - " FROM crs_users," - "crs_courses" - " WHERE crs_users.UsrCod=%ld" - " AND crs_users.CrsCod=crs_courses.CrsCod" - " AND crs_courses.DegCod=%ld" - " ORDER BY crs_courses.ShortName", - UsrCod, - DegCod); - else // All the courses - return (unsigned) - DB_QuerySELECT (mysql_res,"can not get the courses a user belongs to", - "SELECT crs_users.CrsCod," // row[0] - "crs_users.Role," // row[1] - "crs_courses.DegCod" // row[2] - " FROM crs_users," - "crs_courses," - "deg_degrees" - " WHERE crs_users.UsrCod=%ld" - " AND crs_users.CrsCod=crs_courses.CrsCod" - " AND crs_courses.DegCod=deg_degrees.DegCod" - " ORDER BY deg_degrees.ShortName," - "crs_courses.ShortName", - UsrCod); - } - -/*****************************************************************************/ -/********* Get the degree in which a user is enroled in more courses *********/ -/*****************************************************************************/ - -void Usr_GetMainDeg (long UsrCod, - char ShrtName[Cns_HIERARCHY_MAX_BYTES_SHRT_NAME + 1], - Rol_Role_t *MaxRole) - { - MYSQL_RES *mysql_res; - MYSQL_ROW row; - - /***** Get a random student from current course from database *****/ - if (DB_QuerySELECT (&mysql_res,"can not get user's main degree", - "SELECT deg_degrees.ShortName," // row[0] - "main_degree.MaxRole" // row[1] - " FROM deg_degrees," - - // The second table contain only one row with the main degree - " (SELECT crs_courses.DegCod AS DegCod," - "MAX(crs_users.Role) AS MaxRole," - "COUNT(*) AS N" - " FROM crs_users," - "crs_courses" - " WHERE crs_users.UsrCod=%ld" - " AND crs_users.CrsCod=crs_courses.CrsCod" - " GROUP BY crs_courses.DegCod" - " ORDER BY N DESC" // Ordered by number of courses in which user is enroled - " LIMIT 1)" // We need only the main degree - " AS main_degree" - - " WHERE deg_degrees.DegCod=main_degree.DegCod", - UsrCod)) - { - row = mysql_fetch_row (mysql_res); - - /* Get degree name (row[0]) */ - Str_Copy (ShrtName,row[0],Cns_HIERARCHY_MAX_BYTES_SHRT_NAME); - - /* Get maximum role (row[1]) */ - *MaxRole = Rol_ConvertUnsignedStrToRole (row[1]); - } - else // User is not enroled in any course - { - ShrtName[0] = '\0'; - *MaxRole = Rol_UNK; - } - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - } - -/*****************************************************************************/ -/******** Check if a user exists with a given encrypted user's code **********/ -/*****************************************************************************/ - -bool Usr_DB_ChkIfEncryptedUsrCodExists (const char EncryptedUsrCod[Cry_BYTES_ENCRYPTED_STR_SHA256_BASE64]) - { - /***** Get if an encrypted user's code already existed in database *****/ - return (DB_QueryCOUNT ("can not check if an encrypted user's code" - " already existed", - "SELECT COUNT(*)" - " FROM usr_data" - " WHERE EncryptedUsrCod='%s'", - EncryptedUsrCod) != 0); - } - /*****************************************************************************/ /***************************** Write landing page ****************************/ /*****************************************************************************/ @@ -3618,22 +3408,6 @@ void Usr_UpdateMyLastData (void) Usr_DB_InsertMyLastData (); } -/*****************************************************************************/ -/********************** Update my last type of search ************************/ -/*****************************************************************************/ - -void Usr_DB_UpdateMyLastWhatToSearch (void) - { - // WhatToSearch is stored in usr_last for next time I log in - // In other existing sessions distinct to this, WhatToSearch will remain unchanged - DB_QueryUPDATE ("can not update type of search in user's last data", - "UPDATE usr_last" - " SET WhatToSearch=%u" - " WHERE UsrCod=%ld", - (unsigned) Gbl.Search.WhatToSearch, - Gbl.Usrs.Me.UsrDat.UsrCod); - } - /*****************************************************************************/ /*************** Create new entry for my last data in database ***************/ /*****************************************************************************/ @@ -3657,31 +3431,6 @@ static void Usr_DB_InsertMyLastData (void) (long) (time_t) 0); // The user never accessed to notifications } -/*****************************************************************************/ -/*************************** Remove user's last data *************************/ -/*****************************************************************************/ - -void Usr_DB_RemoveUsrLastData (long UsrCod) - { - DB_QueryDELETE ("can not remove user's last data", - "DELETE FROM usr_last" - " WHERE UsrCod=%ld", - UsrCod); - } - -/*****************************************************************************/ -/****************************** Remove user's data ***************************/ -/*****************************************************************************/ - -void Usr_DB_RemoveUsrData (long UsrCod) - { - /***** Remove user's data *****/ - DB_QueryDELETE ("can not remove user's data", - "DELETE FROM usr_data" - " WHERE UsrCod=%ld", - UsrCod); - } - /*****************************************************************************/ /*********** Write a row of a table with the main data of a user *************/ /*****************************************************************************/ @@ -4165,75 +3914,6 @@ static void Usr_WriteUsrData (const char *BgColor, HTM_TD_End (); } -/*****************************************************************************/ -/******** Get the user's code of a random student from current course ********/ -/*****************************************************************************/ -// Returns user's code or -1 if no user found - -long Usr_DB_GetRamdomStdFromCrs (long CrsCod) - { - /***** Get a random student from current course from database *****/ - return DB_QuerySELECTCode ("can not get a random student from a course", - "SELECT UsrCod" - " FROM crs_users" - " WHERE CrsCod=%ld" - " AND Role=%u" - " ORDER BY RAND()" - " LIMIT 1", - CrsCod, - (unsigned) Rol_STD); - } - -/*****************************************************************************/ -/*********** Get the user's code of a random student from a group ************/ -/*****************************************************************************/ -// Returns user's code or -1 if no user found - -long Usr_DB_GetRamdomStdFromGrp (long GrpCod) - { - /***** Get a random student from a group from database *****/ - return DB_QuerySELECTCode ("can not get a random student from a group", - "SELECT grp_users.UsrCod" - " FROM grp_users," - "crs_users" - " WHERE grp_users.GrpCod=%ld" - " AND grp_users.UsrCod=crs_users.UsrCod" - " AND crs_users.Role=%u" - " ORDER BY RAND()" - " LIMIT 1", - GrpCod, - (unsigned) Rol_STD); - } - -/*****************************************************************************/ -/* Get number of teachers in courses of the current instit. in a department **/ -/*****************************************************************************/ - -unsigned Usr_DB_GetNumTchsCurrentInsInDepartment (long DptCod) - { - /***** Get the number of teachers - from the current institution in a department *****/ - return (unsigned) - DB_QueryCOUNT ("can not get the number of teachers in a department", - "SELECT COUNT(DISTINCT usr_data.UsrCod)" - " FROM usr_data," - "crs_users," - "crs_courses," - "deg_degrees," - "ctr_centers" - " WHERE usr_data.InsCod=%ld" // User in the current institution... - " AND usr_data.DptCod=%ld" // ...and the specified department... - " AND usr_data.UsrCod=crs_users.UsrCod" // ...who is... - " AND crs_users.Role IN (%u,%u)" // ...a teacher... - " AND crs_users.CrsCod=crs_courses.CrsCod" // ...in a course... - " AND crs_courses.DegCod=deg_degrees.DegCod" // ...of a degree... - " AND deg_degrees.CtrCod=ctr_centers.InsCod" // ...of a center... - " AND ctr_centers.InsCod=%ld", // ...of the current institution - Gbl.Hierarchy.Ins.InsCod,DptCod, - (unsigned) Rol_NET,(unsigned) Rol_TCH, - Gbl.Hierarchy.Ins.InsCod); - } - /*****************************************************************************/ /******* Get number of users who don't claim to belong to any country ********/ /*****************************************************************************/ @@ -9422,24 +9102,6 @@ void Usr_ConstructPathUsr (long UsrCod,char PathUsr[PATH_MAX + 1 + Cns_MAX_DECIM PathAboveUsr,UsrCod); } -/*****************************************************************************/ -/************** Check if a user exists with a given user's code **************/ -/*****************************************************************************/ - -bool Usr_DB_ChkIfUsrCodExists (long UsrCod) - { - /***** Trivial check: user's code should be > 0 *****/ - if (UsrCod <= 0) // Wrong user's code - return false; - - /***** Get if a user exists in database *****/ - return (DB_QueryCOUNT ("can not check if a user exists", - "SELECT COUNT(*)" - " FROM usr_data" - " WHERE UsrCod=%ld", - UsrCod) != 0); - } - /*****************************************************************************/ /********************** Show warning "no users found" ************************/ /*****************************************************************************/ @@ -10393,166 +10055,3 @@ Usr_Who_t Usr_GetHiddenParamWho (void) Usr_NUM_WHO - 1, Usr_WHO_UNKNOWN); } - -/*****************************************************************************/ -/************************* Get old users from database ***********************/ -/*****************************************************************************/ - -unsigned Usr_DB_GetOldUsrs (MYSQL_RES **mysql_res,time_t SecondsWithoutAccess) - { - return (unsigned) - DB_QuerySELECT (mysql_res,"can not get old users", - "SELECT UsrCod" - " FROM (SELECT UsrCod" - " FROM usr_last" - " WHERE LastTime. +*/ +/*****************************************************************************/ +/*********************************** Headers *********************************/ +/*****************************************************************************/ + +// #define _GNU_SOURCE // For asprintf +// #include // For isalnum, isdigit, etc. +// #include // For maximum values +// #include // For PATH_MAX +// #include // For NULL +// #include // For asprintf +// #include // For exit, system, malloc, free, rand, etc. +// #include // For string functions +// #include // For the macro WEXITSTATUS +// #include // For access, lstat, getpid, chdir, symlink, unlink + +// #include "swad_account.h" +// #include "swad_agenda.h" +// #include "swad_announcement.h" +// #include "swad_box.h" +// #include "swad_calendar.h" +// #include "swad_config.h" +// #include "swad_connected_database.h" +// #include "swad_course.h" +#include "swad_database.h" +// #include "swad_department.h" +// #include "swad_duplicate.h" +// #include "swad_enrolment.h" +#include "swad_error.h" +// #include "swad_figure.h" +// #include "swad_figure_cache.h" +// #include "swad_follow.h" +// #include "swad_form.h" +#include "swad_global.h" +// #include "swad_group.h" +// #include "swad_help.h" +// #include "swad_hierarchy.h" +// #include "swad_hierarchy_level.h" +// #include "swad_HTML.h" +// #include "swad_ID.h" +// #include "swad_language.h" +// #include "swad_mail_database.h" +// #include "swad_message.h" +// #include "swad_MFU.h" +// #include "swad_nickname.h" +// #include "swad_nickname_database.h" +// #include "swad_notification.h" +// #include "swad_parameter.h" +// #include "swad_password.h" +// #include "swad_photo.h" +// #include "swad_privacy.h" +// #include "swad_QR.h" +// #include "swad_record.h" +// #include "swad_record_database.h" +// #include "swad_role.h" +// #include "swad_session_database.h" +// #include "swad_setting.h" +// #include "swad_tab.h" +// #include "swad_user.h" +#include "swad_user_database.h" + +/*****************************************************************************/ +/****************************** Public constants *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private constants *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/****************************** Private types ********************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/************** External global variables from others modules ****************/ +/*****************************************************************************/ + +extern struct Globals Gbl; + +/*****************************************************************************/ +/****************************** Private variables ****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private prototypes ****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/******************************* Update my office ****************************/ +/*****************************************************************************/ + +void Usr_DB_UpdateMyOffice (void) + { + DB_QueryUPDATE ("can not update office", + "UPDATE usr_data" + " SET Office='%s'" + " WHERE UsrCod=%ld", + Gbl.Usrs.Me.UsrDat.Tch.Office, + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/***************************** Update my office phone ************************/ +/*****************************************************************************/ + +void Usr_DB_UpdateMyOfficePhone (void) + { + DB_QueryUPDATE ("can not update office phone", + "UPDATE usr_data" + " SET OfficePhone='%s'" + " WHERE UsrCod=%ld", + Gbl.Usrs.Me.UsrDat.Tch.OfficePhone, + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/********************** Update my last type of search ************************/ +/*****************************************************************************/ + +void Usr_DB_UpdateMyLastWhatToSearch (void) + { + // WhatToSearch is stored in usr_last for next time I log in + // In other existing sessions distinct to this, WhatToSearch will remain unchanged + DB_QueryUPDATE ("can not update type of search in user's last data", + "UPDATE usr_last" + " SET WhatToSearch=%u" + " WHERE UsrCod=%ld", + (unsigned) Gbl.Search.WhatToSearch, + Gbl.Usrs.Me.UsrDat.UsrCod); + } + +/*****************************************************************************/ +/************** Check if a user exists with a given user's code **************/ +/*****************************************************************************/ + +bool Usr_DB_ChkIfUsrCodExists (long UsrCod) + { + /***** Trivial check: user's code should be > 0 *****/ + if (UsrCod <= 0) // Wrong user's code + return false; + + /***** Get if a user exists in database *****/ + return (DB_QueryCOUNT ("can not check if a user exists", + "SELECT COUNT(*)" + " FROM usr_data" + " WHERE UsrCod=%ld", + UsrCod) != 0); + } + +/*****************************************************************************/ +/******** Check if a user exists with a given encrypted user's code **********/ +/*****************************************************************************/ + +bool Usr_DB_ChkIfEncryptedUsrCodExists (const char EncryptedUsrCod[Cry_BYTES_ENCRYPTED_STR_SHA256_BASE64]) + { + /***** Get if an encrypted user's code already existed in database *****/ + return (DB_QueryCOUNT ("can not check if an encrypted user's code" + " already existed", + "SELECT COUNT(*)" + " FROM usr_data" + " WHERE EncryptedUsrCod='%s'", + EncryptedUsrCod) != 0); + } + +/*****************************************************************************/ +/****** Check if a string is found in first name or surnames of anybody ******/ +/*****************************************************************************/ + +bool Usr_DB_FindStrInUsrsNames (const char *Str) + { + return (DB_QueryCOUNT ("can not check if a string matches" + " a first name or a surname", + "SELECT COUNT(*)" + " FROM usr_data" + " WHERE FirstName='%s'" + " OR Surname1='%s'" + " OR Surname2='%s'", + Str, + Str, + Str) != 0); + } + +/*****************************************************************************/ +/************** Get number of users who have chosen an option ****************/ +/*****************************************************************************/ + +unsigned Usr_DB_GetNumUsrsWhoChoseAnOption (const char *SubQuery) + { + switch (Gbl.Scope.Current) + { + case HieLvl_SYS: + return (unsigned) + DB_QueryCOUNT ("can not get the number of users who have chosen an option", + "SELECT COUNT(*)" + " FROM usr_data" + " WHERE %s", + SubQuery); + case HieLvl_CTY: + return (unsigned) + DB_QueryCOUNT ("can not get the number of users who have chosen an option", + "SELECT COUNT(DISTINCT usr_data.UsrCod)" + " FROM ins_instits," + "ctr_centers," + "deg_degrees," + "crs_courses," + "crs_users," + "usr_data" + " WHERE ins_instits.CtyCod=%ld" + " AND ins_instits.InsCod=ctr_centers.InsCod" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.UsrCod=usr_data.UsrCod" + " AND %s", + Gbl.Hierarchy.Cty.CtyCod,SubQuery); + case HieLvl_INS: + return (unsigned) + DB_QueryCOUNT ("can not get the number of users who have chosen an option", + "SELECT COUNT(DISTINCT usr_data.UsrCod)" + " FROM ctr_centers," + "deg_degrees," + "crs_courses," + "crs_users," + "usr_data" + " WHERE ctr_centers.InsCod=%ld" + " AND ctr_centers.CtrCod=deg_degrees.CtrCod" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.UsrCod=usr_data.UsrCod" + " AND %s", + Gbl.Hierarchy.Ins.InsCod,SubQuery); + case HieLvl_CTR: + return (unsigned) + DB_QueryCOUNT ("can not get the number of users who have chosen an option", + "SELECT COUNT(DISTINCT usr_data.UsrCod)" + " FROM deg_degrees," + "crs_courses," + "crs_users," + "usr_data" + " WHERE deg_degrees.CtrCod=%ld" + " AND deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.UsrCod=usr_data.UsrCod" + " AND %s", + Gbl.Hierarchy.Ctr.CtrCod,SubQuery); + case HieLvl_DEG: + return (unsigned) + DB_QueryCOUNT ("can not get the number of users who have chosen an option", + "SELECT COUNT(DISTINCT usr_data.UsrCod)" + " FROM crs_courses," + "crs_users," + "usr_data" + " WHERE crs_courses.DegCod=%ld" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.UsrCod=usr_data.UsrCod" + " AND %s", + Gbl.Hierarchy.Deg.DegCod,SubQuery); + case HieLvl_CRS: + return (unsigned) + DB_QueryCOUNT ("can not get the number of users who have chosen an option", + "SELECT COUNT(DISTINCT usr_data.UsrCod)" + " FROM crs_users," + "usr_data" + " WHERE crs_users.CrsCod=%ld" + " AND crs_users.UsrCod=usr_data.UsrCod" + " AND %s", + Gbl.Hierarchy.Crs.CrsCod,SubQuery); + default: + Err_WrongScopeExit (); + return 0; // Not reached + } + } + +/*****************************************************************************/ +/************************* Get old users from database ***********************/ +/*****************************************************************************/ + +unsigned Usr_DB_GetOldUsrs (MYSQL_RES **mysql_res,time_t SecondsWithoutAccess) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get old users", + "SELECT UsrCod" + " FROM (SELECT UsrCod" + " FROM usr_last" + " WHERE LastTime. +*/ +/*****************************************************************************/ +/********************************** Headers **********************************/ +/*****************************************************************************/ + +#include // To access MySQL databases +#include // For boolean type +#include // For time_t + +// #include "swad_action.h" +// #include "swad_constant.h" +// #include "swad_country.h" +#include "swad_cryptography.h" +// #include "swad_date.h" +// #include "swad_degree.h" +// #include "swad_icon.h" +// #include "swad_layout.h" +// #include "swad_menu.h" +// #include "swad_nickname.h" +// #include "swad_password.h" +// #include "swad_privacy_visibility_type.h" +// #include "swad_role_type.h" +// #include "swad_scope.h" +// #include "swad_search.h" +// #include "swad_string.h" +// #include "swad_theme.h" + +/*****************************************************************************/ +/****************************** Public constants *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/******************************** Public types *******************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/****************************** Public prototypes ****************************/ +/*****************************************************************************/ + +void Usr_DB_UpdateMyOffice (void); +void Usr_DB_UpdateMyOfficePhone (void); +void Usr_DB_UpdateMyLastWhatToSearch (void); + +bool Usr_DB_ChkIfUsrCodExists (long UsrCod); +bool Usr_DB_ChkIfEncryptedUsrCodExists (const char EncryptedUsrCod[Cry_BYTES_ENCRYPTED_STR_SHA256_BASE64]); +bool Usr_DB_FindStrInUsrsNames (const char *Str); +unsigned Usr_DB_GetNumUsrsWhoChoseAnOption (const char *SubQuery); +unsigned Usr_DB_GetOldUsrs (MYSQL_RES **mysql_res,time_t SecondsWithoutAccess); + +void Usr_DB_RemoveUsrLastData (long UsrCod); +void Usr_DB_RemoveUsrData (long UsrCod); + +#endif diff --git a/swad_zip.c b/swad_zip.c index 74cf1015..e4b85918 100644 --- a/swad_zip.c +++ b/swad_zip.c @@ -546,53 +546,53 @@ static void ZIP_ShowLinkToDownloadZIP (const char *FileName,const char *URL, NULL,NULL, NULL,2); - /***** Link to download the file *****/ - HTM_TR_Begin (NULL); - HTM_TD_Begin ("colspan=\"2\" class=\"FILENAME_TXT CM\""); - HTM_A_Begin ("href=\"%s\" class=\"FILENAME_TXT\" title=\"%s\" target=\"_blank\"", - URL,FileName); - HTM_IMG (CfG_URL_ICON_FILEXT_PUBLIC "32x32","zip32x32.gif",Txt_ZIP_file, - "class=\"ICO40x40\""); - HTM_TxtF (" %s ",FileName); - Ico_PutIcon ("download.svg",Txt_Download,"ICO40x40"); - HTM_A_End (); - HTM_TD_End (); - HTM_TR_End (); + /***** Link to download the file *****/ + HTM_TR_Begin (NULL); + HTM_TD_Begin ("colspan=\"2\" class=\"FILENAME_TXT CM\""); + HTM_A_Begin ("href=\"%s\" class=\"FILENAME_TXT\" title=\"%s\" target=\"_blank\"", + URL,FileName); + HTM_IMG (CfG_URL_ICON_FILEXT_PUBLIC "32x32","zip32x32.gif",Txt_ZIP_file, + "class=\"ICO40x40\""); + HTM_TxtF (" %s ",FileName); + Ico_PutIcon ("download.svg",Txt_Download,"ICO40x40"); + HTM_A_End (); + HTM_TD_End (); + HTM_TR_End (); - /***** Filename *****/ - HTM_TR_Begin (NULL); + /***** Filename *****/ + HTM_TR_Begin (NULL); - HTM_TD_Begin ("class=\"%s RM\"",The_ClassFormInBox[Gbl.Prefs.Theme]); - HTM_TxtColon (Txt_Filename); - HTM_TD_End (); + HTM_TD_Begin ("class=\"%s RM\"",The_ClassFormInBox[Gbl.Prefs.Theme]); + HTM_TxtColon (Txt_Filename); + HTM_TD_End (); - HTM_TD_Begin ("class=\"DAT LM\""); - HTM_A_Begin ("href=\"%s\" class=\"DAT\" title=\"%s\" target=\"_blank\"", - URL,FileName); - HTM_Txt (FileName); - HTM_A_End (); - HTM_TD_End (); + HTM_TD_Begin ("class=\"DAT LM\""); + HTM_A_Begin ("href=\"%s\" class=\"DAT\" title=\"%s\" target=\"_blank\"", + URL,FileName); + HTM_Txt (FileName); + HTM_A_End (); + HTM_TD_End (); - HTM_TR_End (); + HTM_TR_End (); - /***** Write the file size *****/ - Fil_WriteFileSizeFull ((double) FileSize,FileSizeStr); - HTM_TR_Begin (NULL); + /***** Write the file size *****/ + Fil_WriteFileSizeFull ((double) FileSize,FileSizeStr); + HTM_TR_Begin (NULL); - HTM_TD_Begin ("class=\"%s RM\"",The_ClassFormInBox[Gbl.Prefs.Theme]); - HTM_TxtColon (Txt_File_size); - HTM_TD_End (); + HTM_TD_Begin ("class=\"%s RM\"",The_ClassFormInBox[Gbl.Prefs.Theme]); + HTM_TxtColon (Txt_File_size); + HTM_TD_End (); - HTM_TD_Begin ("class=\"DAT LM\""); - HTM_Txt (FileSizeStr); - if (UncompressedSize) - { - Fil_WriteFileSizeFull ((double) UncompressedSize,FileSizeStr); - HTM_TxtF (" (%s %s)",FileSizeStr,Txt_FILE_uncompressed); - } - HTM_TD_End (); + HTM_TD_Begin ("class=\"DAT LM\""); + HTM_Txt (FileSizeStr); + if (UncompressedSize) + { + Fil_WriteFileSizeFull ((double) UncompressedSize,FileSizeStr); + HTM_TxtF (" (%s %s)",FileSizeStr,Txt_FILE_uncompressed); + } + HTM_TD_End (); - HTM_TR_End (); + HTM_TR_End (); /***** End table and box *****/ Box_BoxTableEnd ();