From 19249f76897a36d4f05aecd210ce8f8756783036 Mon Sep 17 00:00:00 2001 From: acanas Date: Wed, 13 Oct 2021 19:02:22 +0200 Subject: [PATCH] Version 21.32: Oct 13, 2021 New module swad_role_database for database queries related to user roles. --- Makefile | 3 +- swad_changelog.h | 3 +- swad_enrolment.c | 45 +++++++---------- swad_record.c | 4 +- swad_role.c | 65 ++---------------------- swad_role.h | 5 -- swad_role_database.c | 115 +++++++++++++++++++++++++++++++++++++++++++ swad_role_database.h | 46 +++++++++++++++++ 8 files changed, 189 insertions(+), 97 deletions(-) create mode 100644 swad_role_database.c create mode 100644 swad_role_database.h diff --git a/Makefile b/Makefile index a36cf91c..64fd79c8 100644 --- a/Makefile +++ b/Makefile @@ -76,7 +76,8 @@ OBJS = swad_account.o swad_account_database.o swad_action.o swad_admin.o \ swad_program_database.o swad_project.o swad_project_database.o \ swad_QR.o \ swad_record.o swad_record_database.o swad_report.o \ - swad_report_database.o swad_role.o swad_room.o swad_RSS.o \ + swad_report_database.o swad_role.o swad_role_database.o swad_room.o \ + swad_RSS.o \ swad_scope.o swad_search.o swad_session.o swad_setting.o \ swad_statistic.o swad_string.o swad_survey.o swad_syllabus.o \ swad_system_config.o \ diff --git a/swad_changelog.h b/swad_changelog.h index 970825fe..29ca59ae 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -602,13 +602,14 @@ TODO: FIX BUG, URGENT! En las fechas como par TODO: En las encuestas, que los estudiantes no puedan ver los resultados hasta que no finalice el plazo. */ -#define Log_PLATFORM_VERSION "SWAD 21.31.2 (2021-10-13)" +#define Log_PLATFORM_VERSION "SWAD 21.32 (2021-10-13)" #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.32: Oct 13, 2021 New module swad_role_database for database queries related to user roles. (319611 lines) Version 21.31.2: Oct 13, 2021 Queries moved to module swad_log_database. (319542 lines) Version 21.31.1: Oct 13, 2021 Queries moved to module swad_log_database. (319520 lines) Version 21.31: Oct 13, 2021 New module swad_report_database for database queries related to user reports. (319475 lines) diff --git a/swad_enrolment.c b/swad_enrolment.c index 57dde544..f4f92f20 100644 --- a/swad_enrolment.c +++ b/swad_enrolment.c @@ -59,6 +59,7 @@ #include "swad_photo.h" #include "swad_record_database.h" #include "swad_role.h" +#include "swad_role_database.h" #include "swad_setting.h" #include "swad_test_print.h" #include "swad_user.h" @@ -225,7 +226,7 @@ void Enr_ModifyRoleInCurrentCrs (struct UsrData *UsrDat,Rol_Role_t NewRole) } /***** Update the role of a user in a course *****/ - Rol_DB_UpdateUsrRoleInCurrentCrs (UsrDat->UsrCod,NewRole); + Rol_DB_UpdateUsrRoleInCrs (Gbl.Hierarchy.Crs.CrsCod,UsrDat->UsrCod,NewRole); /***** Flush caches *****/ Usr_FlushCachesUsr (); @@ -460,39 +461,28 @@ void Enr_GetNotifEnrolment (char SummaryStr[Ntf_MAX_BYTES_SUMMARY + 1], long CrsCod,long UsrCod) { extern const char *Txt_ROLES_SINGUL_Abc[Rol_NUM_ROLES][Usr_NUM_SEXS]; - MYSQL_RES *mysql_res; - MYSQL_ROW row; struct UsrData UsrDat; Rol_Role_t Role; - SummaryStr[0] = '\0'; // Return nothing on error - /***** Get user's role in course from database *****/ - if (Rol_DB_GetUsrRoleInCrs (&mysql_res,CrsCod,UsrCod) == 1) // Result should have a unique row - { - /***** Get user's role in course *****/ - row = mysql_fetch_row (mysql_res); + Role = Rol_GetRoleUsrInCrs (UsrCod,CrsCod); - /* Initialize structure with user's data */ - Usr_UsrDataConstructor (&UsrDat); + /***** Set summary string *****/ + /* Initialize structure with user's data */ + Usr_UsrDataConstructor (&UsrDat); - /* Get user's data */ - UsrDat.UsrCod = UsrCod; - Usr_ChkUsrCodAndGetAllUsrDataFromUsrCod (&UsrDat, - Usr_DONT_GET_PREFS, - Usr_DONT_GET_ROLE_IN_CURRENT_CRS); + /* Get user's data */ + UsrDat.UsrCod = UsrCod; + Usr_ChkUsrCodAndGetAllUsrDataFromUsrCod (&UsrDat, + Usr_DONT_GET_PREFS, + Usr_DONT_GET_ROLE_IN_CURRENT_CRS); - /* Role (row[0]) */ - Role = Rol_ConvertUnsignedStrToRole (row[0]); - Str_Copy (SummaryStr,Txt_ROLES_SINGUL_Abc[Role][UsrDat.Sex], - Ntf_MAX_BYTES_SUMMARY); + /* Set summary string depending on role and sex */ + Str_Copy (SummaryStr,Txt_ROLES_SINGUL_Abc[Role][UsrDat.Sex], + Ntf_MAX_BYTES_SUMMARY); - /* Free memory used for user's data */ - Usr_UsrDataDestructor (&UsrDat); - } - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); + /* Free memory used for user's data */ + Usr_UsrDataDestructor (&UsrDat); } /*****************************************************************************/ @@ -2002,7 +1992,8 @@ void Enr_AskIfRejectSignUp (void) } else // User does not belong to this course { - Role = Rol_DB_GetRequestedRole (Gbl.Usrs.Other.UsrDat.UsrCod); + Role = Rol_DB_GetRequestedRole (Gbl.Hierarchy.Crs.CrsCod, + Gbl.Usrs.Other.UsrDat.UsrCod); if (Role == Rol_STD || Role == Rol_NET || Role == Rol_TCH) diff --git a/swad_record.c b/swad_record.c index 1be24570..27ecfcdf 100644 --- a/swad_record.c +++ b/swad_record.c @@ -58,6 +58,7 @@ #include "swad_record.h" #include "swad_record_database.h" #include "swad_role.h" +#include "swad_role_database.h" #include "swad_setting.h" #include "swad_timetable.h" #include "swad_user.h" @@ -2833,7 +2834,8 @@ static void Rec_ShowRole (struct UsrData *UsrDat, break; default: // User does not belong to current course /* If there is a request of this user, default role is the requested role */ - DefaultRoleInForm = Rol_DB_GetRequestedRole (UsrDat->UsrCod); + DefaultRoleInForm = Rol_DB_GetRequestedRole (Gbl.Hierarchy.Crs.CrsCod, + UsrDat->UsrCod); switch (DefaultRoleInForm) { diff --git a/swad_role.c b/swad_role.c index 042d12eb..c515279b 100644 --- a/swad_role.c +++ b/swad_role.c @@ -33,6 +33,7 @@ #include "swad_HTML.h" #include "swad_parameter.h" #include "swad_role.h" +#include "swad_role_database.h" #include "swad_role_type.h" /*****************************************************************************/ @@ -410,14 +411,7 @@ Rol_Role_t Rol_GetRoleUsrInCrs (long UsrCod,long CrsCod) Gbl.Cache.RoleUsrInCrs.Role = Rol_GetMyRoleInCrs (CrsCod); else /* Get role of the user in course from database */ - Gbl.Cache.RoleUsrInCrs.Role = - DB_QuerySELECTRole ("can not get the role of a user in a course", - "SELECT Role" - " FROM crs_users" - " WHERE CrsCod=%ld" - " AND UsrCod=%ld", - CrsCod, - UsrCod); + Gbl.Cache.RoleUsrInCrs.Role = Rol_DB_GetRoleUsrInCrs (UsrCod,CrsCod); return Gbl.Cache.RoleUsrInCrs.Role; } @@ -439,12 +433,7 @@ void Rol_GetRolesInAllCrss (struct UsrData *UsrDat) return; /***** Get distinct roles in all courses of the user from database *****/ - NumRoles = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get user's roles in all courses", - "SELECT DISTINCT(Role)" // row[0] - " FROM crs_users" - " WHERE UsrCod=%ld", - UsrDat->UsrCod); + NumRoles = Rol_DB_GetRolesInAllCrss (&mysql_res,UsrDat->UsrCod); for (NumRole = 0, UsrDat->Roles.InCrss = 0; NumRole < NumRoles; NumRole++) @@ -632,51 +621,3 @@ unsigned Rol_GetSelectedRoles (void) return Roles; } - -/*****************************************************************************/ -/************ Get requested role of a user in current course *****************/ -/*****************************************************************************/ - -Rol_Role_t Rol_DB_GetRequestedRole (long UsrCod) - { - /***** Get requested role from database *****/ - return DB_QuerySELECTRole ("can not get requested role", - "SELECT Role" - " FROM crs_requests" - " WHERE CrsCod=%ld" - " AND UsrCod=%ld", - Gbl.Hierarchy.Crs.CrsCod, - UsrCod); - } - -/*****************************************************************************/ -/******************* Get user's role in course from database *****************/ -/*****************************************************************************/ - -unsigned Rol_DB_GetUsrRoleInCrs (MYSQL_RES **mysql_res,long CrsCod,long UsrCod) - { - return (unsigned) - DB_QuerySELECT (mysql_res,"can not get user's role in course", - "SELECT Role" // row[0] - " FROM crs_users" - " WHERE CrsCod=%ld" - " AND UsrCod=%ld", - CrsCod, - UsrCod); - } - -/*****************************************************************************/ -/************* Update the role of a user in the current course ***************/ -/*****************************************************************************/ - -void Rol_DB_UpdateUsrRoleInCurrentCrs (long UsrCod,Rol_Role_t NewRole) - { - DB_QueryUPDATE ("can not modify user's role in course", - "UPDATE crs_users" - " SET Role=%u" - " WHERE CrsCod=%ld" - " AND UsrCod=%ld", - (unsigned) NewRole, - Gbl.Hierarchy.Crs.CrsCod, - UsrCod); - } diff --git a/swad_role.h b/swad_role.h index b911ea06..7a8ba84b 100644 --- a/swad_role.h +++ b/swad_role.h @@ -62,9 +62,4 @@ void Rol_WriteSelectorRoles (unsigned RolesAllowed,unsigned RolesSelected, void Rol_PutHiddenParamRoles (unsigned Roles); unsigned Rol_GetSelectedRoles (void); -Rol_Role_t Rol_DB_GetRequestedRole (long UsrCod); - -unsigned Rol_DB_GetUsrRoleInCrs (MYSQL_RES **mysql_res,long CrsCod,long UsrCod); -void Rol_DB_UpdateUsrRoleInCurrentCrs (long UsrCod,Rol_Role_t NewRole); - #endif diff --git a/swad_role_database.c b/swad_role_database.c new file mode 100644 index 00000000..702f1570 --- /dev/null +++ b/swad_role_database.c @@ -0,0 +1,115 @@ +// swad_role_database.c: user's roles, operations with database + +/* + SWAD (Shared Workspace At a Distance), + is a web platform developed at the University of Granada (Spain), + and used to support university teaching. + + This file is part of SWAD core. + Copyright (C) 1999-2021 Antonio Caņas Vargas + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General 3 License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ +/*****************************************************************************/ +/*********************************** Headers *********************************/ +/*****************************************************************************/ + +#include "swad_database.h" + +/*****************************************************************************/ +/****************************** Public constants *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private constants *****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/******************************* Private types *******************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/************** External global variables from others modules ****************/ +/*****************************************************************************/ + +// extern struct Globals Gbl; + +/*****************************************************************************/ +/************************* Private global variables **************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/***************************** Private prototypes ****************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/************* Update the role of a user in the current course ***************/ +/*****************************************************************************/ + +void Rol_DB_UpdateUsrRoleInCrs (long CrsCod,long UsrCod,Rol_Role_t NewRole) + { + DB_QueryUPDATE ("can not modify user's role in course", + "UPDATE crs_users" + " SET Role=%u" + " WHERE CrsCod=%ld" + " AND UsrCod=%ld", + (unsigned) NewRole, + CrsCod, + UsrCod); + } + +/*****************************************************************************/ +/********************** Get role of a user in a course ***********************/ +/*****************************************************************************/ + +Rol_Role_t Rol_DB_GetRoleUsrInCrs (long UsrCod,long CrsCod) + { + return + DB_QuerySELECTRole ("can not get the role of a user in a course", + "SELECT Role" + " FROM crs_users" + " WHERE CrsCod=%ld" + " AND UsrCod=%ld", + CrsCod, + UsrCod); + } + +/*****************************************************************************/ +/*************** Get distinct roles in all courses of a user *****************/ +/*****************************************************************************/ + +unsigned Rol_DB_GetRolesInAllCrss (MYSQL_RES **mysql_res,long UsrCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get user's roles in all courses", + "SELECT DISTINCT(Role)" // row[0] + " FROM crs_users" + " WHERE UsrCod=%ld", + UsrCod); + } + +/*****************************************************************************/ +/************ Get requested role of a user in current course *****************/ +/*****************************************************************************/ + +Rol_Role_t Rol_DB_GetRequestedRole (long CrsCod,long UsrCod) + { + return DB_QuerySELECTRole ("can not get requested role", + "SELECT Role" + " FROM crs_requests" + " WHERE CrsCod=%ld" + " AND UsrCod=%ld", + CrsCod, + UsrCod); + } diff --git a/swad_role_database.h b/swad_role_database.h new file mode 100644 index 00000000..f68e0c0b --- /dev/null +++ b/swad_role_database.h @@ -0,0 +1,46 @@ +// swad_role_database.h: user's roles, operations with database + +#ifndef _SWAD_ROL_DB +#define _SWAD_ROL_DB +/* + SWAD (Shared Workspace At a Distance in Spanish), + is a web platform developed at the University of Granada (Spain), + and used to support university teaching. + + This file is part of SWAD core. + Copyright (C) 1999-2021 Antonio Caņas Vargas + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ +/*****************************************************************************/ +/********************************** Headers **********************************/ +/*****************************************************************************/ + +#include "swad_role_type.h" + +/*****************************************************************************/ +/************************** Public constant and types ************************/ +/*****************************************************************************/ + +/*****************************************************************************/ +/****************************** Public prototypes ****************************/ +/*****************************************************************************/ + +void Rol_DB_UpdateUsrRoleInCrs (long CrsCod,long UsrCod,Rol_Role_t NewRole); + +Rol_Role_t Rol_DB_GetRoleUsrInCrs (long UsrCod,long CrsCod); +unsigned Rol_DB_GetRolesInAllCrss (MYSQL_RES **mysql_res,long UsrCod); +Rol_Role_t Rol_DB_GetRequestedRole (long CrsCod,long UsrCod); + +#endif