Version 21.49: Nov 03, 2021 New module swad_user_database for database queries related to users.

This commit is contained in:
acanas 2021-11-03 11:35:21 +01:00
parent 84aa601825
commit 4d0863b000
43 changed files with 1001 additions and 800 deletions

View File

@ -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

View File

@ -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"
/*****************************************************************************/

View File

@ -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 *****************************/

View File

@ -28,6 +28,7 @@
#include "swad_admin.h"
#include "swad_admin_database.h"
#include "swad_global.h"
#include "swad_user_database.h"
/*****************************************************************************/
/****************************** Public constants *****************************/

View File

@ -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;

View File

@ -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 ******************/
/*****************************************************************************/

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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 ******************/
/*****************************************************************************/

View File

@ -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);

View File

@ -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)

View File

@ -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 **************/
/*****************************************************************************/

View File

@ -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);

View File

@ -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 *********************/
/*****************************************************************************/

View File

@ -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);

View File

@ -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);

View File

@ -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 ***************/
/*****************************************************************************/

View File

@ -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);

View File

@ -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 =

View File

@ -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 **************/
/*****************************************************************************/

View File

@ -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);

View File

@ -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 ****************/

View File

@ -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);

View File

@ -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 ();

View File

@ -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 *******/
/*****************************************************************************/

View File

@ -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);

View File

@ -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 ****************************/
/*****************************************************************************/

View File

@ -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

View File

@ -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;

View File

@ -34,6 +34,7 @@
#include <unistd.h> // 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

View File

@ -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 ****************/

View File

@ -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\"");

View File

@ -64,6 +64,7 @@
#include "swad_timeline.h"
#include "swad_timeline_database.h"
#include "swad_user.h"
#include "swad_user_database.h"
/*****************************************************************************/
/****************************** Public constants *****************************/

View File

@ -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 ****************/

View File

@ -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;

View File

@ -43,6 +43,7 @@
#include "swad_parameter.h"
#include "swad_session_database.h"
#include "swad_user.h"
#include "swad_user_database.h"
/*****************************************************************************/
/****************************** Public constants *****************************/

View File

@ -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;
}

View File

@ -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<FROM_UNIXTIME(UNIX_TIMESTAMP()-%llu)"
" UNION "
"SELECT UsrCod"
" FROM usr_data"
" WHERE UsrCod NOT IN"
" (SELECT UsrCod"
" FROM usr_last)"
") AS candidate_usrs"
" WHERE UsrCod NOT IN"
" (SELECT DISTINCT UsrCod"
" FROM crs_users)",
(unsigned long long) SecondsWithoutAccess);
}
/*****************************************************************************/
/************** 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
}
return 0; // Not reached
}
/*****************************************************************************/
/****** 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);
}
/*****************************************************************************/
/******************************* 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);
}

View File

@ -336,44 +336,36 @@ void Usr_FlushCacheUsrSharesAnyOfMyCrs (void);
bool Usr_CheckIfUsrSharesAnyOfMyCrs (struct UsrData *UsrDat);
bool Usr_CheckIfUsrSharesAnyOfMyCrsWithDifferentRole (long UsrCod);
void Usr_GetMyCountrs (void);
void Usr_GetMyInstits (void);
void Usr_GetMyCenters (void);
void Usr_GetMyDegrees (void);
void Usr_GetMyCourses (void);
void Cty_GetMyCountrs (void);
void Ins_GetMyInstits (void);
void Ctr_GetMyCenters (void);
void Deg_GetMyDegrees (void);
void Deg_GetUsrMainDeg (long UsrCod,
char ShrtName[Cns_HIERARCHY_MAX_BYTES_SHRT_NAME + 1],
Rol_Role_t *MaxRole);
void Crs_GetMyCourses (void);
void Usr_FreeMyCountrs (void);
void Usr_FreeMyInstits (void);
void Usr_FreeMyCenters (void);
void Usr_FreeMyDegrees (void);
void Usr_FreeMyCourses (void);
void Cty_FreeMyCountrs (void);
void Ins_FreeMyInstits (void);
void Ctr_FreeMyCenters (void);
void Deg_FreeMyDegrees (void);
void Crs_FreeMyCourses (void);
void Usr_FlushCacheUsrBelongsToIns (void);
bool Usr_CheckIfUsrBelongsToIns (long UsrCod,long InsCod);
void Usr_FlushCacheUsrBelongsToCtr (void);
bool Usr_CheckIfUsrBelongsToCtr (long UsrCod,long CtrCod);
void Usr_FlushCacheUsrBelongsToDeg (void);
bool Usr_CheckIfUsrBelongsToDeg (long UsrCod,long DegCod);
void Usr_FlushCacheUsrBelongsToCrs (void);
bool Usr_CheckIfUsrBelongsToCrs (long UsrCod,long CrsCod,
void Ins_FlushCacheUsrBelongsToIns (void);
bool Ins_CheckIfUsrBelongsToIns (long UsrCod,long InsCod);
void Ctr_FlushCacheUsrBelongsToCtr (void);
bool Ctr_CheckIfUsrBelongsToCtr (long UsrCod,long CtrCod);
void Deg_FlushCacheUsrBelongsToDeg (void);
bool Deg_CheckIfUsrBelongsToDeg (long UsrCod,long DegCod);
void Crs_FlushCacheUsrBelongsToCrs (void);
bool Crs_CheckIfUsrBelongsToCrs (long UsrCod,long CrsCod,
bool CountOnlyAcceptedCourses);
bool Usr_CheckIfIBelongToCty (long CtyCod);
bool Usr_CheckIfIBelongToIns (long InsCod);
bool Usr_CheckIfIBelongToCtr (long CtrCod);
bool Usr_CheckIfIBelongToDeg (long DegCod);
bool Usr_CheckIfIBelongToCrs (long CrsCod);
unsigned Usr_DB_GetCtysFromUsr (long UsrCod,MYSQL_RES **mysql_res);
unsigned Usr_GetInssFromUsr (long UsrCod,long CtyCod,MYSQL_RES **mysql_res);
unsigned Usr_GetCtrsFromUsr (long UsrCod,long InsCod,MYSQL_RES **mysql_res);
unsigned Usr_GetDegsFromUsr (long UsrCod,long CtrCod,MYSQL_RES **mysql_res);
unsigned Usr_GetCrssFromUsr (long UsrCod,long DegCod,MYSQL_RES **mysql_res);
void Usr_GetMainDeg (long UsrCod,
char ShrtName[Cns_HIERARCHY_MAX_BYTES_SHRT_NAME + 1],
Rol_Role_t *MaxRole);
bool Usr_DB_ChkIfEncryptedUsrCodExists (const char EncryptedUsrCod[Cry_BYTES_ENCRYPTED_STR_SHA256_BASE64]);
bool Cty_CheckIfIBelongToCty (long CtyCod);
bool Ins_CheckIfIBelongToIns (long InsCod);
bool Ctr_CheckIfIBelongToCtr (long CtrCod);
bool Deg_CheckIfIBelongToDeg (long DegCod);
bool Crs_CheckIfIBelongToCrs (long CrsCod);
void Usr_WriteLandingPage (void);
void Usr_WriteFormLogout (void);
@ -407,21 +399,12 @@ bool Usr_ChkUsrCodAndGetAllUsrDataFromUsrCod (struct UsrData *UsrDat,
Usr_GetPrefs_t GetPrefs,
Usr_GetRoleInCurrentCrs_t GetRoleInCurrentCrs);
void Usr_UpdateMyLastData (void);
void Usr_DB_UpdateMyLastWhatToSearch (void);
void Usr_InsertMyLastCrsTabAndTime (void);
void Usr_DB_RemoveUsrLastData (long UsrCod);
void Usr_DB_RemoveUsrData (long UsrCod);
void Usr_WriteRowUsrMainData (unsigned NumUsr,struct UsrData *UsrDat,
bool PutCheckBoxToSelectUsr,Rol_Role_t Role,
struct SelectedUsrs *SelectedUsrs);
long Usr_DB_GetRamdomStdFromCrs (long CrsCod);
long Usr_DB_GetRamdomStdFromGrp (long GrpCod);
unsigned Usr_DB_GetNumTchsCurrentInsInDepartment (long DptCod);
void Usr_FlushCacheNumUsrsWhoDontClaimToBelongToAnyCty (void);
unsigned Usr_GetNumUsrsWhoDontClaimToBelongToAnyCty (void);
unsigned Usr_GetCachedNumUsrsWhoDontClaimToBelongToAnyCty (void);
@ -527,7 +510,6 @@ void Usr_SeeTchClassPhotoPrn (void);
void Usr_PutSelectorNumColsClassPhoto (void);
void Usr_ConstructPathUsr (long UsrCod,char PathUsr[PATH_MAX + 1 + Cns_MAX_DECIMAL_DIGITS_LONG + 1]);
bool Usr_DB_ChkIfUsrCodExists (long UsrCod);
void Usr_ShowWarningNoUsersFound (Rol_Role_t Role);
@ -553,13 +535,4 @@ void Usr_PutWhoIcon (Usr_Who_t Who);
void Usr_PutHiddenParamWho (Usr_Who_t Who);
Usr_Who_t Usr_GetHiddenParamWho (void);
unsigned Usr_DB_GetOldUsrs (MYSQL_RES **mysql_res,time_t SecondsWithoutAccess);
unsigned Usr_DB_GetNumUsrsWhoChoseAnOption (const char *SubQuery);
bool Usr_DB_FindStrInUsrsNames (const char *Str);
void Usr_DB_UpdateMyOffice (void);
void Usr_DB_UpdateMyOfficePhone (void);
#endif

344
swad_user_database.c Normal file
View File

@ -0,0 +1,344 @@
// swad_user_database.c: users, operations with database
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2021 Antonio Cañas Vargas
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General 3 License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/*********************************** Headers *********************************/
/*****************************************************************************/
// #define _GNU_SOURCE // For asprintf
// #include <ctype.h> // For isalnum, isdigit, etc.
// #include <limits.h> // For maximum values
// #include <linux/limits.h> // For PATH_MAX
// #include <stddef.h> // For NULL
// #include <stdio.h> // For asprintf
// #include <stdlib.h> // For exit, system, malloc, free, rand, etc.
// #include <string.h> // For string functions
// #include <sys/wait.h> // For the macro WEXITSTATUS
// #include <unistd.h> // 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<FROM_UNIXTIME(UNIX_TIMESTAMP()-%llu)"
" UNION "
"SELECT UsrCod"
" FROM usr_data"
" WHERE UsrCod NOT IN"
" (SELECT UsrCod"
" FROM usr_last)"
") AS candidate_usrs"
" WHERE UsrCod NOT IN"
" (SELECT DISTINCT UsrCod"
" FROM crs_users)",
(unsigned long long) SecondsWithoutAccess);
}
/*****************************************************************************/
/*************************** 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);
}

77
swad_user_database.h Normal file
View File

@ -0,0 +1,77 @@
// swad_user_database.h: users, operations with database
#ifndef _SWAD_USR_DB
#define _SWAD_USR_DB
/*
SWAD (Shared Workspace At a Distance in Spanish),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2021 Antonio Cañas Vargas
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/********************************** Headers **********************************/
/*****************************************************************************/
#include <mysql/mysql.h> // To access MySQL databases
#include <stdbool.h> // For boolean type
#include <sys/types.h> // 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

View File

@ -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 ("&nbsp;%s&nbsp;",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 ("&nbsp;%s&nbsp;",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 ();