From e1db62661b9ac35652c94f957bea06af15c1815b Mon Sep 17 00:00:00 2001 From: acanas Date: Fri, 18 Jun 2021 18:39:21 +0200 Subject: [PATCH] Version 20.94.2: Jun 18, 2021 Queries moved to module swad_group_database. --- swad_changelog.h | 3 +- swad_group.c | 176 +++++++++++++----------------------------- swad_group.h | 1 + swad_group_database.c | 120 ++++++++++++++++++++++++++++ swad_group_database.h | 9 +++ 5 files changed, 185 insertions(+), 124 deletions(-) diff --git a/swad_changelog.h b/swad_changelog.h index 2f159f33..c17b25a4 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 20.94.1 (2021-06-17)" +#define Log_PLATFORM_VERSION "SWAD 20.94.2 (2021-06-18)" #define CSS_FILE "swad20.45.css" #define JS_FILE "swad20.69.1.js" /* TODO: Rename CENTRE to CENTER in help wiki. TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams + Version 20.94.2: Jun 18, 2021 Queries moved to module swad_group_database. (313557 lines) Version 20.94.1: Jun 17, 2021 Queries moved to module swad_group_database. (313511 lines) Version 20.94: Jun 17, 2021 New module swad_group_database for database queries related to groups. (313489 lines) Version 20.93: Jun 17, 2021 Code refactoring. (313382 lines) diff --git a/swad_group.c b/swad_group.c index 9498e65d..952adcfa 100644 --- a/swad_group.c +++ b/swad_group.c @@ -1218,12 +1218,7 @@ static void Grp_RemoveUsrFromGroup (long UsrCod,long GrpCod) bool ItsMe = Usr_ItsMe (UsrCod); /***** Remove user from group *****/ - DB_QueryDELETE ("can not remove a user from a group", - "DELETE FROM grp_users" - " WHERE GrpCod=%ld" - " AND UsrCod=%ld", - GrpCod, - UsrCod); + Grp_DB_RemoveUsrFromGrp (UsrCod,GrpCod); /***** Flush caches *****/ Grp_FlushCacheUsrSharesAnyOfMyGrpsInCurrentCrs (); @@ -1967,49 +1962,49 @@ static bool Grp_ListGrpsForChangeMySelection (struct GroupType *GrpTyp, /* Put radio item or checkbox to select the group */ HTM_TR_Begin (NULL); - if (IBelongToThisGroup) - HTM_TD_Begin ("class=\"LM LIGHT_BLUE\""); - else - HTM_TD_Begin ("class=\"LM\""); + if (IBelongToThisGroup) + HTM_TD_Begin ("class=\"LM LIGHT_BLUE\""); + else + HTM_TD_Begin ("class=\"LM\""); - snprintf (StrGrpCod,sizeof (StrGrpCod),"GrpCod%ld",GrpTyp->GrpTypCod); - if (Gbl.Usrs.Me.Role.Logged == Rol_STD && // If I am a student - !GrpTyp->MultipleEnrolment && // ...and the enrolment is single - GrpTyp->NumGrps > 1) // ...and there are more than one group - { - /* Put a radio item */ - if (GrpTyp->MandatoryEnrolment) - HTM_INPUT_RADIO (StrGrpCod,false, - "id=\"Grp%ld\" value=\"%ld\"%s%s", - Grp->GrpCod,Grp->GrpCod, - IBelongToThisGroup ? " checked=\"checked\"" : "", // Group selected? - ICanChangeMySelectionForThisGrp ? "" : - IBelongToThisGroup ? " readonly" : // I can not unregister (disabled does not work because the value is not submitted) - " disabled=\"disabled\""); // I can not register - else // If the enrolment is not mandatory, I can select no groups - HTM_INPUT_RADIO (StrGrpCod,false, - "id=\"Grp%ld\" value=\"%ld\"%s%s" - " onclick=\"selectUnselectRadio(this,this.form.GrpCod%ld,%u)\"", - Grp->GrpCod,Grp->GrpCod, - IBelongToThisGroup ? " checked=\"checked\"" : "", // Group selected? - ICanChangeMySelectionForThisGrp ? "" : - IBelongToThisGroup ? " readonly" : // I can not unregister (disabled does not work because the value is not submitted) - " disabled=\"disabled\"", // I can not register - GrpTyp->GrpTypCod,GrpTyp->NumGrps); - } - else - /* Put a checkbox item */ - HTM_INPUT_CHECKBOX (StrGrpCod,HTM_DONT_SUBMIT_ON_CHANGE, - "id=\"Grp%ld\" value=\"%ld\"%s%s", - Grp->GrpCod,Grp->GrpCod, - IBelongToThisGroup ? " checked=\"checked\"" : "", - ICanChangeMySelectionForThisGrp ? "" : - IBelongToThisGroup ? " readonly" : // I can not unregister (disabled does not work because the value is not submitted) - " disabled=\"disabled\""); // I can not register + snprintf (StrGrpCod,sizeof (StrGrpCod),"GrpCod%ld",GrpTyp->GrpTypCod); + if (Gbl.Usrs.Me.Role.Logged == Rol_STD && // If I am a student + !GrpTyp->MultipleEnrolment && // ...and the enrolment is single + GrpTyp->NumGrps > 1) // ...and there are more than one group + { + /* Put a radio item */ + if (GrpTyp->MandatoryEnrolment) + HTM_INPUT_RADIO (StrGrpCod,false, + "id=\"Grp%ld\" value=\"%ld\"%s%s", + Grp->GrpCod,Grp->GrpCod, + IBelongToThisGroup ? " checked=\"checked\"" : "", // Group selected? + ICanChangeMySelectionForThisGrp ? "" : + IBelongToThisGroup ? " readonly" : // I can not unregister (disabled does not work because the value is not submitted) + " disabled=\"disabled\""); // I can not register + else // If the enrolment is not mandatory, I can select no groups + HTM_INPUT_RADIO (StrGrpCod,false, + "id=\"Grp%ld\" value=\"%ld\"%s%s" + " onclick=\"selectUnselectRadio(this,this.form.GrpCod%ld,%u)\"", + Grp->GrpCod,Grp->GrpCod, + IBelongToThisGroup ? " checked=\"checked\"" : "", // Group selected? + ICanChangeMySelectionForThisGrp ? "" : + IBelongToThisGroup ? " readonly" : // I can not unregister (disabled does not work because the value is not submitted) + " disabled=\"disabled\"", // I can not register + GrpTyp->GrpTypCod,GrpTyp->NumGrps); + } + else + /* Put a checkbox item */ + HTM_INPUT_CHECKBOX (StrGrpCod,HTM_DONT_SUBMIT_ON_CHANGE, + "id=\"Grp%ld\" value=\"%ld\"%s%s", + Grp->GrpCod,Grp->GrpCod, + IBelongToThisGroup ? " checked=\"checked\"" : "", + ICanChangeMySelectionForThisGrp ? "" : + IBelongToThisGroup ? " readonly" : // I can not unregister (disabled does not work because the value is not submitted) + " disabled=\"disabled\""); // I can not register - HTM_TD_End (); + HTM_TD_End (); - Grp_WriteRowGrp (Grp,IBelongToThisGroup); + Grp_WriteRowGrp (Grp,IBelongToThisGroup); HTM_TR_End (); } @@ -2649,6 +2644,11 @@ void Grp_GetListGrpTypesInThisCrs (Grp_WhichGroupTypes_t WhichGroupTypes) MYSQL_RES *mysql_res; MYSQL_ROW row; unsigned long NumGrpTyp; + static unsigned (*Grp_DB_GetGrpTypesInCurrentCrs[Grp_NUM_WHICH_GROUP_TYPES]) (MYSQL_RES **mysql_res) = + { + [Grp_ONLY_GROUP_TYPES_WITH_GROUPS] = Grp_DB_GetGrpTypesWithGrpsInCurrentCrs, + [Grp_ALL_GROUP_TYPES ] = Grp_DB_GetAllGrpTypesInCurrentCrs, + }; if (++Gbl.Crs.Grps.GrpTypes.NestedCalls > 1) // If list is created yet, there's nothing to do return; @@ -2657,61 +2657,8 @@ void Grp_GetListGrpTypesInThisCrs (Grp_WhichGroupTypes_t WhichGroupTypes) if open time is in the past *****/ Grp_OpenGroupsAutomatically (); - /***** Get group types with groups + groups types without groups from database *****/ - // The tables in the second part of the UNION requires ALIAS in order to LOCK TABLES when registering in groups - switch (WhichGroupTypes) - { - case Grp_ONLY_GROUP_TYPES_WITH_GROUPS: - Gbl.Crs.Grps.GrpTypes.Num = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get types of group of a course", - "SELECT grp_types.GrpTypCod," // row[0] - "grp_types.GrpTypName," // row[1] - "grp_types.Mandatory," // row[2] - "grp_types.Multiple," // row[3] - "grp_types.MustBeOpened," // row[4] - "UNIX_TIMESTAMP(grp_types.OpenTime)," // row[5] - "COUNT(grp_groups.GrpCod)" // row[6] - " FROM grp_types," - "grp_groups" - " WHERE grp_types.CrsCod=%ld" - " AND grp_types.GrpTypCod=grp_groups.GrpTypCod" - " GROUP BY grp_types.GrpTypCod" - " ORDER BY grp_types.GrpTypName", - Gbl.Hierarchy.Crs.CrsCod); - break; - case Grp_ALL_GROUP_TYPES: - Gbl.Crs.Grps.GrpTypes.Num = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get types of group of a course", - "(SELECT grp_types.GrpTypCod," // row[0] - "grp_types.GrpTypName AS GrpTypName," // row[1] - "grp_types.Mandatory," // row[2] - "grp_types.Multiple," // row[3] - "grp_types.MustBeOpened," // row[4] - "UNIX_TIMESTAMP(grp_types.OpenTime)," // row[5] - "COUNT(grp_groups.GrpCod)" // row[6] - " FROM grp_types," - "grp_groups" - " WHERE grp_types.CrsCod=%ld" - " AND grp_types.GrpTypCod=grp_groups.GrpTypCod" - " GROUP BY grp_types.GrpTypCod)" - " UNION " - "(SELECT GrpTypCod," // row[0] - "GrpTypName," // row[1] - "Mandatory," // row[2] - "Multiple," // row[3] - "MustBeOpened," // row[4] - "UNIX_TIMESTAMP(OpenTime)," // row[5] - "0" - " FROM grp_types" - " WHERE CrsCod=%ld" - " AND GrpTypCod NOT IN" - " (SELECT GrpTypCod" - " FROM grp_groups))" - " ORDER BY GrpTypName", - Gbl.Hierarchy.Crs.CrsCod, - Gbl.Hierarchy.Crs.CrsCod); - break; - } + /***** Get group types from database *****/ + Gbl.Crs.Grps.GrpTypes.Num = Grp_DB_GetGrpTypesInCurrentCrs[WhichGroupTypes] (&mysql_res); /***** Get group types *****/ Gbl.Crs.Grps.GrpTypes.NumGrpsTotal = 0; @@ -2780,37 +2727,20 @@ void Grp_OpenGroupsAutomatically (void) long GrpTypCod; /***** Find group types to be opened *****/ - NumGrpTypes = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get the types of group to be opened", - "SELECT GrpTypCod" - " FROM grp_types" - " WHERE CrsCod=%ld" - " AND MustBeOpened='Y'" - " AND OpenTime<=NOW()", - Gbl.Hierarchy.Crs.CrsCod); + NumGrpTypes = Grp_DB_GetGrpTypesInCurrentCrsToBeOpened (&mysql_res); for (NumGrpTyp = 0; NumGrpTyp < NumGrpTypes; NumGrpTyp++) { - /* Get next group TYPE */ + /* Get next group type */ if ((GrpTypCod = DB_GetNextCode (mysql_res)) > 0) { - /***** Open all the closed groups in this course the must be opened - and with open time in the past ****/ - DB_QueryUPDATE ("can not open groups", - "UPDATE grp_groups" - " SET Open='Y'" - " WHERE GrpTypCod=%ld" - " AND Open='N'", - GrpTypCod); + /***** Open all the closed groups of this type that are closed ****/ + Grp_DB_OpenGrpsOfType (GrpTypCod); /***** To not try to open groups again, set MustBeOpened to false *****/ - DB_QueryUPDATE ("can not update the opening of a type of group", - "UPDATE grp_types" - " SET MustBeOpened='N'" - " WHERE GrpTypCod=%ld", - GrpTypCod); + Grp_DB_ClearMustBeOpened (GrpTypCod); } } diff --git a/swad_group.h b/swad_group.h index 0ec91de5..59218df5 100644 --- a/swad_group.h +++ b/swad_group.h @@ -49,6 +49,7 @@ /******************************* Public types ********************************/ /*****************************************************************************/ +#define Grp_NUM_WHICH_GROUP_TYPES 2 typedef enum { Grp_ONLY_GROUP_TYPES_WITH_GROUPS, diff --git a/swad_group_database.c b/swad_group_database.c index 199abe4d..fce4bfa6 100644 --- a/swad_group_database.c +++ b/swad_group_database.c @@ -280,6 +280,85 @@ bool Grp_DB_CheckIfIBelongToGrpsOfType (long GrpTypCod) Gbl.Usrs.Me.UsrDat.UsrCod) != 0); } +/*****************************************************************************/ +/************** Get group types with groups in current course ****************/ +/*****************************************************************************/ + +unsigned Grp_DB_GetGrpTypesWithGrpsInCurrentCrs (MYSQL_RES **mysql_res) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get types of group of a course", + "SELECT grp_types.GrpTypCod," // row[0] + "grp_types.GrpTypName," // row[1] + "grp_types.Mandatory," // row[2] + "grp_types.Multiple," // row[3] + "grp_types.MustBeOpened," // row[4] + "UNIX_TIMESTAMP(grp_types.OpenTime)," // row[5] + "COUNT(grp_groups.GrpCod)" // row[6] + " FROM grp_types," + "grp_groups" + " WHERE grp_types.CrsCod=%ld" + " AND grp_types.GrpTypCod=grp_groups.GrpTypCod" + " GROUP BY grp_types.GrpTypCod" + " ORDER BY grp_types.GrpTypName", + Gbl.Hierarchy.Crs.CrsCod); + } + +/*****************************************************************************/ +/********** Get group types with or without groups in current course *********/ +/*****************************************************************************/ + +unsigned Grp_DB_GetAllGrpTypesInCurrentCrs (MYSQL_RES **mysql_res) + { + // The tables in the second part of the UNION requires ALIAS in order to LOCK TABLES when registering in groups + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get types of group of a course", + "(SELECT grp_types.GrpTypCod," // row[0] + "grp_types.GrpTypName AS GrpTypName," // row[1] + "grp_types.Mandatory," // row[2] + "grp_types.Multiple," // row[3] + "grp_types.MustBeOpened," // row[4] + "UNIX_TIMESTAMP(grp_types.OpenTime)," // row[5] + "COUNT(grp_groups.GrpCod)" // row[6] + " FROM grp_types," + "grp_groups" + " WHERE grp_types.CrsCod=%ld" + " AND grp_types.GrpTypCod=grp_groups.GrpTypCod" + " GROUP BY grp_types.GrpTypCod)" + " UNION " + "(SELECT GrpTypCod," // row[0] + "GrpTypName," // row[1] + "Mandatory," // row[2] + "Multiple," // row[3] + "MustBeOpened," // row[4] + "UNIX_TIMESTAMP(OpenTime)," // row[5] + "0" + " FROM grp_types" + " WHERE CrsCod=%ld" + " AND GrpTypCod NOT IN" + " (SELECT GrpTypCod" + " FROM grp_groups))" + " ORDER BY GrpTypName", + Gbl.Hierarchy.Crs.CrsCod, + Gbl.Hierarchy.Crs.CrsCod); + } + +/*****************************************************************************/ +/*********** Get group types in current course that must be opened ***********/ +/*****************************************************************************/ + +unsigned Grp_DB_GetGrpTypesInCurrentCrsToBeOpened (MYSQL_RES **mysql_res) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get the types of group to be opened", + "SELECT GrpTypCod" + " FROM grp_types" + " WHERE CrsCod=%ld" + " AND MustBeOpened='Y'" + " AND OpenTime<=NOW()", + Gbl.Hierarchy.Crs.CrsCod); + } + /*****************************************************************************/ /*********** Query the number of groups that hay in this course **************/ /*****************************************************************************/ @@ -396,6 +475,33 @@ bool Grp_DB_CheckIfAssociatedToGrps (const char *Table,const char *Field,long Co Field,Cod) != 0); } +/*****************************************************************************/ +/******************** Open all the closed groups of a tyoe *******************/ +/*****************************************************************************/ + +void Grp_DB_OpenGrpsOfType (long GrpTypCod) + { + DB_QueryUPDATE ("can not open groups", + "UPDATE grp_groups" + " SET Open='Y'" + " WHERE GrpTypCod=%ld" + " AND Open='N'", + GrpTypCod); + } + +/*****************************************************************************/ +/******************** Set type of group to not be opened *********************/ +/*****************************************************************************/ + +void Grp_DB_ClearMustBeOpened (long GrpTypCod) + { + DB_QueryUPDATE ("can not update the opening of a type of group", + "UPDATE grp_types" + " SET MustBeOpened='N'" + " WHERE GrpTypCod=%ld", + GrpTypCod); + } + /*****************************************************************************/ /*********************** Register a user in a group **************************/ /*****************************************************************************/ @@ -411,6 +517,20 @@ void Grp_DB_AddUsrToGrp (long UsrCod,long GrpCod) UsrCod); } +/*****************************************************************************/ +/************************* Remove a user from a group ************************/ +/*****************************************************************************/ + +void Grp_DB_RemoveUsrFromGrp (long UsrCod,long GrpCod) + { + DB_QueryDELETE ("can not remove a user from a group", + "DELETE FROM grp_users" + " WHERE GrpCod=%ld" + " AND UsrCod=%ld", + GrpCod, + UsrCod); + } + /*****************************************************************************/ /*************** Remove a user of all the groups of a course *****************/ /*****************************************************************************/ diff --git a/swad_group_database.h b/swad_group_database.h index 82759800..7eb43b6e 100644 --- a/swad_group_database.h +++ b/swad_group_database.h @@ -61,6 +61,11 @@ unsigned Grp_DB_CountNumUsrsInNoGrpsOfType (Rol_Role_t Role,long GrpTypCod); bool Grp_DB_CheckIfIBelongToGrpsOfType (long GrpTypCod); +unsigned Grp_DB_GetGrpTypesWithGrpsInCurrentCrs (MYSQL_RES **mysql_res); +unsigned Grp_DB_GetAllGrpTypesInCurrentCrs (MYSQL_RES **mysql_res); + +unsigned Grp_DB_GetGrpTypesInCurrentCrsToBeOpened (MYSQL_RES **mysql_res); + unsigned Grp_DB_CountNumGrpsInCurrentCrs (void); unsigned Grp_DB_CountNumGrpsInThisCrsOfType (long GrpTypCod); unsigned Grp_DB_GetGrpsOfType (MYSQL_RES **mysql_res,long GrpTypCod); @@ -71,8 +76,12 @@ bool Grp_DB_CheckIfAssociatedToGrp (const char *Table,const char *Field, long Cod,long GrpCod); bool Grp_DB_CheckIfAssociatedToGrps (const char *Table,const char *Field,long Cod); +void Grp_DB_OpenGrpsOfType (long GrpTypCod); +void Grp_DB_ClearMustBeOpened (long GrpTypCod); + void Grp_DB_AddUsrToGrp (long UsrCod,long GrpCod); +void Grp_DB_RemoveUsrFromGrp (long UsrCod,long GrpCod); void Grp_DB_RemUsrFromAllGrpsInCrs (long UsrCod,long CrsCod); void Grp_DB_RemUsrFromAllGrps (long UsrCod);