From e18dd654705ce8ea53d1f12e7b3b594c892abc02 Mon Sep 17 00:00:00 2001 From: acanas Date: Thu, 17 Jun 2021 19:39:08 +0200 Subject: [PATCH] Version 20.94.1: Jun 17, 2021 Queries moved to module swad_group_database. --- swad_changelog.h | 3 +- swad_file_browser.c | 2 +- swad_group.c | 268 +++---------------------------------- swad_group.h | 3 - swad_group_database.c | 297 ++++++++++++++++++++++++++++++++++++++---- swad_group_database.h | 28 +++- swad_mark.c | 1 + 7 files changed, 317 insertions(+), 285 deletions(-) diff --git a/swad_changelog.h b/swad_changelog.h index c351d6dc..2f159f33 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 (2021-06-17)" +#define Log_PLATFORM_VERSION "SWAD 20.94.1 (2021-06-17)" #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.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) Version 20.92: Jun 11, 2021 New modules swad_admin and swad_admin_database for administrators. (313381 lines) diff --git a/swad_file_browser.c b/swad_file_browser.c index 84ef977a..e6a9c875 100644 --- a/swad_file_browser.c +++ b/swad_file_browser.c @@ -5289,7 +5289,7 @@ static long Brw_GetGrpLastAccZone (const char *FieldNameDB) /***** Check if group exists (it's possible that this group has been removed after my last access to it) *****/ if (GrpCod >= 0) - if (Grp_DB_CheckIfGroupExists (GrpCod)) + if (Grp_DB_CheckIfGrpExists (GrpCod)) /* Check if I belong to this group (it's possible that I have been removed from this group after my last access to it) */ if (Grp_GetIfIBelongToGrp (GrpCod)) return GrpCod; diff --git a/swad_group.c b/swad_group.c index 71d4e323..9498e65d 100644 --- a/swad_group.c +++ b/swad_group.c @@ -111,7 +111,6 @@ static void Grp_PutCheckboxAllGrps (Grp_WhichGroups_t GroupsSelectableByStdsOrNE static void Grp_ConstructorListGrpAlreadySelec (struct ListGrpsAlreadySelec **AlreadyExistsGroupOfType); static void Grp_DestructorListGrpAlreadySelec (struct ListGrpsAlreadySelec **AlreadyExistsGroupOfType); static void Grp_RemoveUsrFromGroup (long UsrCod,long GrpCod); -static void Grp_DB_AddUsrToGroup (struct UsrData *UsrDat,long GrpCod); static void Grp_ListGroupTypesForEdition (void); static void Grp_PutIconsEditingGroupTypes (__attribute__((unused)) void *Args); @@ -136,17 +135,10 @@ static void Grp_PutFormToCreateGroupType (void); static void Grp_PutFormToCreateGroup (const struct Roo_Rooms *Rooms); static void Grp_GetDataOfGroupTypeByCod (struct GroupType *GrpTyp); static bool Grp_GetMultipleEnrolmentOfAGroupType (long GrpTypCod); -static long Grp_DB_GetTypeOfGroupOfAGroup (long GrpCod); -static unsigned Grp_DB_CountNumUsrsInNoGrpsOfType (Rol_Role_t Role,long GrpTypCod); -static bool Grp_DB_CheckIfIBelongToGrpsOfType (long GrpTypCod); static void Grp_GetLstCodGrpsUsrBelongs (long CrsCod,long GrpTypCod,long UsrCod, struct ListCodGrps *LstGrps); static bool Grp_CheckIfGrpIsInList (long GrpCod,struct ListCodGrps *LstGrps); static bool Grp_CheckIfOpenTimeInTheFuture (time_t OpenTimeUTC); -static bool Grp_DB_CheckIfGroupTypeNameExists (const char *GrpTypName,long GrpTypCod); -static bool Grp_DB_CheckIfGroupNameExists (long GrpTypCod,const char *GrpName,long GrpCod); -static long Grp_DB_CreateGroupType (const struct GroupType *GrpTyp); -static void Grp_DB_CreateGroup (void); static void Grp_AskConfirmRemGrpTypWithGrps (unsigned NumGrps); static void Grp_AskConfirmRemGrp (void); @@ -888,7 +880,8 @@ bool Grp_ChangeMyGrpsAtomically (struct ListCodGrps *LstGrpsIWant) if (LstGrpsIWant->GrpCods[NumGrpIWant] == LstGrpsIBelong.GrpCods[NumGrpIBelong]) RegisterMeInThisGrp = false; if (RegisterMeInThisGrp) - Grp_DB_AddUsrToGroup (&Gbl.Usrs.Me.UsrDat,LstGrpsIWant->GrpCods[NumGrpIWant]); + Grp_DB_AddUsrToGrp (Gbl.Usrs.Me.UsrDat.UsrCod, + LstGrpsIWant->GrpCods[NumGrpIWant]); } ChangesMade = true; @@ -954,7 +947,8 @@ void Grp_ChangeGrpsOtherUsrAtomically (struct ListCodGrps *LstGrpsUsrWants) if (LstGrpsUsrWants->GrpCods[NumGrpUsrWants] == LstGrpsUsrBelongs.GrpCods[NumGrpUsrBelongs]) RegisterUsrInThisGrp = false; if (RegisterUsrInThisGrp) - Grp_DB_AddUsrToGroup (&Gbl.Usrs.Other.UsrDat,LstGrpsUsrWants->GrpCods[NumGrpUsrWants]); + Grp_DB_AddUsrToGrp (Gbl.Usrs.Other.UsrDat.UsrCod, + LstGrpsUsrWants->GrpCods[NumGrpUsrWants]); } /***** Free memory with the list of groups which user belonged to *****/ @@ -997,7 +991,7 @@ bool Grp_CheckIfSelectionGrpsSingleEnrolmentIsValid (Rol_Role_t Role,struct List SelectionValid && NumCodGrp < LstGrps->NumGrps; NumCodGrp++) { - GrpTypCod = Grp_DB_GetTypeOfGroupOfAGroup (LstGrps->GrpCods[NumCodGrp]); + GrpTypCod = Grp_DB_GetGrpTypeFromGrp (LstGrps->GrpCods[NumCodGrp]); MultipleEnrolment = Grp_GetMultipleEnrolmentOfAGroupType (GrpTypCod); if (!MultipleEnrolment) @@ -1118,7 +1112,8 @@ void Grp_RegisterUsrIntoGroups (struct UsrData *UsrDat,struct ListCodGrps *LstGr if (!AlreadyRegisteredInGrp) // If the user does not belong to the selected group { - Grp_DB_AddUsrToGroup (UsrDat,LstGrps->GrpCods[NumGrpSel]); + Grp_DB_AddUsrToGrp (UsrDat->UsrCod, + LstGrps->GrpCods[NumGrpSel]); Ale_ShowAlert (Ale_SUCCESS,Txt_THE_USER_X_has_been_enroled_in_the_group_of_type_Y_Z, UsrDat->FullName,Gbl.Crs.Grps.GrpTypes.LstGrpTypes[NumGrpTyp].GrpTypName, Gbl.Crs.Grps.GrpTypes.LstGrpTypes[NumGrpTyp].LstGrps[NumGrpThisType].GrpName); @@ -1190,23 +1185,12 @@ unsigned Grp_RemoveUsrFromGroups (struct UsrData *UsrDat,struct ListCodGrps *Lst void Grp_RemUsrFromAllGrpsInCrs (long UsrCod,long CrsCod) { - bool ItsMe = Usr_ItsMe (UsrCod); - - /***** Remove user from all the groups of the course *****/ - DB_QueryDELETE ("can not remove a user from all groups of a course", - "DELETE FROM grp_users" - " WHERE UsrCod=%ld" - " AND GrpCod IN" - " (SELECT grp_groups.GrpCod" - " FROM grp_types," - "grp_groups" - " WHERE grp_types.CrsCod=%ld" - " AND grp_types.GrpTypCod=grp_groups.GrpTypCod)", - UsrCod,CrsCod); + /***** Remove user from all groups in the given course *****/ + Grp_DB_RemUsrFromAllGrpsInCrs (UsrCod,CrsCod); /***** Flush caches *****/ Grp_FlushCacheUsrSharesAnyOfMyGrpsInCurrentCrs (); - if (ItsMe) + if (Usr_ItsMe (UsrCod)) Grp_FlushCacheIBelongToGrp (); } @@ -1216,17 +1200,12 @@ void Grp_RemUsrFromAllGrpsInCrs (long UsrCod,long CrsCod) void Grp_RemUsrFromAllGrps (long UsrCod) { - bool ItsMe = Usr_ItsMe (UsrCod); - /***** Remove user from all groups *****/ - DB_QueryDELETE ("can not remove a user from the groups he/she belongs to", - "DELETE FROM grp_users" - " WHERE UsrCod=%ld", - UsrCod); + Grp_DB_RemUsrFromAllGrps (UsrCod); /***** Flush caches *****/ Grp_FlushCacheUsrSharesAnyOfMyGrpsInCurrentCrs (); - if (ItsMe) + if (Usr_ItsMe (UsrCod)) Grp_FlushCacheIBelongToGrp (); } @@ -1252,22 +1231,6 @@ static void Grp_RemoveUsrFromGroup (long UsrCod,long GrpCod) Grp_FlushCacheIBelongToGrp (); } -/*****************************************************************************/ -/*********************** Register a user in a group **************************/ -/*****************************************************************************/ - -static void Grp_DB_AddUsrToGroup (struct UsrData *UsrDat,long GrpCod) - { - /***** Register in group *****/ - DB_QueryINSERT ("can not add a user to a group", - "INSERT INTO grp_users" - " (GrpCod,UsrCod)" - " VALUES" - " (%ld,%ld)", - GrpCod, - UsrDat->UsrCod); - } - /*****************************************************************************/ /******************** List current group types for edition *******************/ /*****************************************************************************/ @@ -3125,124 +3088,6 @@ void Grp_GetDataOfGroupByCod (struct GroupData *GrpDat) } } -/*****************************************************************************/ -/********************** Get the type of group of a group *********************/ -/*****************************************************************************/ - -static long Grp_DB_GetTypeOfGroupOfAGroup (long GrpCod) - { - long GrpTypCod; - - /***** Get group type of a group from database *****/ - GrpTypCod = DB_QuerySELECTCode ("can not get the type of a group", - "SELECT GrpTypCod" - " FROM grp_groups" - " WHERE GrpCod=%ld", - GrpCod); - if (GrpTypCod <= 0) - Err_WrongGrpTypExit (); - - return GrpTypCod; - } - -/*****************************************************************************/ -/******************** Check if a group exists in database ********************/ -/*****************************************************************************/ - -bool Grp_DB_CheckIfGroupExists (long GrpCod) - { - /***** Get if a group exists from database *****/ - return (DB_QueryCOUNT ("can not check if a group exists", - "SELECT COUNT(*)" - " FROM grp_groups" - " WHERE GrpCod=%ld", - GrpCod) != 0); - } - -/*****************************************************************************/ -/******************* Check if a group belongs to a course ********************/ -/*****************************************************************************/ - -bool Grp_DB_CheckIfGrpBelongsToCrs (long GrpCod,long CrsCod) - { - /***** Get if a group exists from database *****/ - return (DB_QueryCOUNT ("can not check if a group belongs to a course", - "SELECT COUNT(*)" - " FROM grp_groups," - "grp_types" - " WHERE grp_groups.GrpCod=%ld" - " AND grp_groups.GrpTypCod=grp_types.GrpTypCod" - " AND grp_types.CrsCod=%ld", - GrpCod,CrsCod) != 0); - } - -/*****************************************************************************/ -/********************* Count number of users in a group **********************/ -/*****************************************************************************/ - -unsigned Grp_DB_CountNumUsrsInGrp (Rol_Role_t Role,long GrpCod) - { - /***** Get number of students in a group from database *****/ - return (unsigned) - DB_QueryCOUNT ("can not get number of users in a group", - "SELECT COUNT(*)" - " FROM grp_users," - "grp_groups," - "grp_types," - "crs_users" - " WHERE grp_users.GrpCod=%ld" - " AND grp_users.GrpCod=grp_groups.GrpCod" - " AND grp_groups.GrpTypCod=grp_types.GrpTypCod" - " AND grp_types.CrsCod=crs_users.CrsCod" - " AND grp_users.UsrCod=crs_users.UsrCod" - " AND crs_users.Role=%u", - GrpCod, - (unsigned) Role); - } - -/*****************************************************************************/ -/*** Count # of users of current course not belonging to groups of a type ****/ -/*****************************************************************************/ - -static unsigned Grp_DB_CountNumUsrsInNoGrpsOfType (Rol_Role_t Role,long GrpTypCod) - { - /***** Get number of users not belonging to groups of a type ******/ - return (unsigned) - DB_QueryCOUNT ("can not get the number of users" - " not belonging to groups of a type", - "SELECT COUNT(UsrCod)" - " FROM crs_users" - " WHERE CrsCod=%ld" - " AND Role=%u" - " AND UsrCod NOT IN" - " (SELECT DISTINCT grp_users.UsrCod" - " FROM grp_groups," - "grp_users" - " WHERE grp_groups.GrpTypCod=%ld" - " AND grp_groups.GrpCod=grp_users.GrpCod)", - Gbl.Hierarchy.Crs.CrsCod, - (unsigned) Role, - GrpTypCod); - } - -/*****************************************************************************/ -/********* Check if I belong to any groups of a given type I belong **********/ -/*****************************************************************************/ - -static bool Grp_DB_CheckIfIBelongToGrpsOfType (long GrpTypCod) - { - /***** Get a group which I belong to from database *****/ - return (DB_QueryCOUNT ("can not check if you belong to a group type", - "SELECT COUNT(grp_groups.GrpCod)" - " FROM grp_groups," - "grp_users" - " WHERE grp_groups.GrpTypCod=%ld" - " AND grp_groups.GrpCod=grp_users.GrpCod" - " AND grp_users.UsrCod=%ld", // I belong - GrpTypCod, - Gbl.Usrs.Me.UsrDat.UsrCod) != 0); - } - /*****************************************************************************/ /************************ Check if I belong to a group ***********************/ /*****************************************************************************/ @@ -3663,7 +3508,7 @@ void Grp_ReceiveFormNewGrpTyp (void) if (Gbl.Crs.Grps.GrpTyp.GrpTypName[0]) // If there's a group type name { /***** If name of group type was in database... *****/ - if (Grp_DB_CheckIfGroupTypeNameExists (Gbl.Crs.Grps.GrpTyp.GrpTypName,-1L)) + if (Grp_DB_CheckIfGrpTypNameExistsInCurrentCrs (Gbl.Crs.Grps.GrpTyp.GrpTypName,-1L)) { AlertType = Ale_WARNING; snprintf (AlertTxt,sizeof (AlertTxt), @@ -3738,7 +3583,7 @@ void Grp_ReceiveFormNewGrp (void) if (Gbl.Crs.Grps.GrpName[0]) // If there's a group name { /***** If name of group was in database... *****/ - if (Grp_DB_CheckIfGroupNameExists (Gbl.Crs.Grps.GrpTyp.GrpTypCod,Gbl.Crs.Grps.GrpName,-1L)) + if (Grp_DB_CheckIfGrpNameExistsForGrpTyp (Gbl.Crs.Grps.GrpTyp.GrpTypCod,Gbl.Crs.Grps.GrpName,-1L)) { AlertType = Ale_WARNING; snprintf (AlertTxt,sizeof (AlertTxt), @@ -3747,7 +3592,7 @@ void Grp_ReceiveFormNewGrp (void) } else // Add new group to database { - Grp_DB_CreateGroup (); + Grp_DB_CreateGroup (&Gbl.Crs.Grps); /* Write success message */ AlertType = Ale_SUCCESS; @@ -3775,83 +3620,6 @@ void Grp_ReceiveFormNewGrp (void) AlertType,AlertTxt); } -/*****************************************************************************/ -/******************* Check if name of group type exists **********************/ -/*****************************************************************************/ - -static bool Grp_DB_CheckIfGroupTypeNameExists (const char *GrpTypName,long GrpTypCod) - { - /***** Get number of group types with a name from database *****/ - return (DB_QueryCOUNT ("can not check if the name of type of group" - " already existed", - "SELECT COUNT(*)" - " FROM grp_types" - " WHERE CrsCod=%ld" - " AND GrpTypName='%s'" - " AND GrpTypCod<>%ld", - Gbl.Hierarchy.Crs.CrsCod,GrpTypName,GrpTypCod) != 0); - } - -/*****************************************************************************/ -/************************ Check if name of group exists **********************/ -/*****************************************************************************/ - -static bool Grp_DB_CheckIfGroupNameExists (long GrpTypCod,const char *GrpName,long GrpCod) - { - /***** Get number of groups with a type and a name from database *****/ - return (DB_QueryCOUNT ("can not check if the name of group already existed", - "SELECT COUNT(*)" - " FROM grp_groups" - " WHERE GrpTypCod=%ld" - " AND GrpName='%s'" - " AND GrpCod<>%ld", - GrpTypCod,GrpName,GrpCod) != 0); - } - -/*****************************************************************************/ -/************************** Create a new group type **************************/ -/*****************************************************************************/ - -static long Grp_DB_CreateGroupType (const struct GroupType *GrpTyp) - { - /***** Create a new group type *****/ - return - DB_QueryINSERTandReturnCode ("can not create type of group", - "INSERT INTO grp_types" - " (CrsCod,GrpTypName," - "Mandatory,Multiple,MustBeOpened,OpenTime)" - " VALUES" - " (%ld,'%s'," - "'%c','%c','%c',FROM_UNIXTIME(%ld))", - Gbl.Hierarchy.Crs.CrsCod, - GrpTyp->GrpTypName, - GrpTyp->MandatoryEnrolment ? 'Y' : - 'N', - GrpTyp->MultipleEnrolment ? 'Y' : - 'N', - GrpTyp->MustBeOpened ? 'Y' : - 'N', - (long) GrpTyp->OpenTimeUTC); - } - -/*****************************************************************************/ -/***************************** Create a new group ****************************/ -/*****************************************************************************/ - -static void Grp_DB_CreateGroup (void) - { - /***** Create a new group *****/ - DB_QueryINSERT ("can not create group", - "INSERT INTO grp_groups" - " (GrpTypCod,GrpName,RooCod,MaxStudents,Open,FileZones)" - " VALUES" - " (%ld,'%s',%ld,%u,'N','N')", - Gbl.Crs.Grps.GrpTyp.GrpTypCod, - Gbl.Crs.Grps.GrpName, - Gbl.Crs.Grps.RooCod, - Gbl.Crs.Grps.MaxStudents); - } - /*****************************************************************************/ /********************* Request removing of a group type **********************/ /*****************************************************************************/ @@ -4288,7 +4056,7 @@ void Grp_ChangeGroupType (void) Grp_GetDataOfGroupByCod (&GrpDat); /***** If group was in database... *****/ - if (Grp_DB_CheckIfGroupNameExists (NewGrpTypCod,GrpDat.GrpName,-1L)) + if (Grp_DB_CheckIfGrpNameExistsForGrpTyp (NewGrpTypCod,GrpDat.GrpName,-1L)) { /* Create warning message */ AlertType = Ale_WARNING; @@ -4644,7 +4412,7 @@ void Grp_RenameGroupType (void) if (strcmp (Gbl.Crs.Grps.GrpTyp.GrpTypName,NewNameGrpTyp)) // Different names { /***** If group type was in database... *****/ - if (Grp_DB_CheckIfGroupTypeNameExists (NewNameGrpTyp,Gbl.Crs.Grps.GrpTyp.GrpTypCod)) + if (Grp_DB_CheckIfGrpTypNameExistsInCurrentCrs (NewNameGrpTyp,Gbl.Crs.Grps.GrpTyp.GrpTypCod)) { AlertType = Ale_WARNING; snprintf (AlertTxt,sizeof (AlertTxt), @@ -4730,7 +4498,7 @@ void Grp_RenameGroup (void) if (strcmp (GrpDat.GrpName,NewNameGrp)) // Different names { /***** If group was in database... *****/ - if (Grp_DB_CheckIfGroupNameExists (GrpDat.GrpTypCod,NewNameGrp,Gbl.Crs.Grps.GrpCod)) + if (Grp_DB_CheckIfGrpNameExistsForGrpTyp (GrpDat.GrpTypCod,NewNameGrp,Gbl.Crs.Grps.GrpCod)) { AlertType = Ale_WARNING; snprintf (AlertTxt,sizeof (AlertTxt), diff --git a/swad_group.h b/swad_group.h index f96d5a2f..0ec91de5 100644 --- a/swad_group.h +++ b/swad_group.h @@ -193,9 +193,6 @@ void Grp_FreeListGrpTypesAndGrps (void); void Grp_OpenGroupsAutomatically (void); void Grp_GetListGrpTypesAndGrpsInThisCrs (Grp_WhichGroupTypes_t WhichGroupTypes); void Grp_GetDataOfGroupByCod (struct GroupData *GrpDat); -bool Grp_DB_CheckIfGroupExists (long GrpCod); -bool Grp_DB_CheckIfGrpBelongsToCrs (long GrpCod,long CrsCod); -unsigned Grp_DB_CountNumUsrsInGrp (Rol_Role_t Role,long GrpCod); void Grp_FlushCacheIBelongToGrp (void); bool Grp_GetIfIBelongToGrp (long GrpCod); diff --git a/swad_group_database.c b/swad_group_database.c index ae377704..199abe4d 100644 --- a/swad_group_database.c +++ b/swad_group_database.c @@ -36,7 +36,7 @@ // #include "swad_attendance_database.h" // #include "swad_box.h" #include "swad_database.h" -// #include "swad_error.h" +#include "swad_error.h" // #include "swad_exam_session.h" // #include "swad_form.h" // #include "swad_game.h" @@ -103,43 +103,181 @@ void Grp_DB_UnlockTables (void) } /*****************************************************************************/ -/************ Check if an assignment is associated to a group ****************/ +/************************** Create a new group type **************************/ /*****************************************************************************/ -bool Grp_DB_CheckIfAssociatedToGrp (const char *Table,const char *Field, - long Cod,long GrpCod) +long Grp_DB_CreateGroupType (const struct GroupType *GrpTyp) { - /***** Get if an assignment, attendance event, survey, exam event or match - is associated to a given group from database *****/ - return (DB_QueryCOUNT ("can not check if associated to a group", - "SELECT COUNT(*)" - " FROM %s" - " WHERE %s=%ld" - " AND GrpCod=%ld", - Table, - Field,Cod, - GrpCod) != 0); + /***** Create a new group type *****/ + return + DB_QueryINSERTandReturnCode ("can not create type of group", + "INSERT INTO grp_types" + " (CrsCod,GrpTypName," + "Mandatory,Multiple,MustBeOpened,OpenTime)" + " VALUES" + " (%ld,'%s'," + "'%c','%c','%c',FROM_UNIXTIME(%ld))", + Gbl.Hierarchy.Crs.CrsCod, + GrpTyp->GrpTypName, + GrpTyp->MandatoryEnrolment ? 'Y' : + 'N', + GrpTyp->MultipleEnrolment ? 'Y' : + 'N', + GrpTyp->MustBeOpened ? 'Y' : + 'N', + (long) GrpTyp->OpenTimeUTC); } /*****************************************************************************/ -/*** Check if an assignment, attendance event, survey, exam event or match ***/ -/*** is associated to any group ***/ +/***************************** Create a new group ****************************/ /*****************************************************************************/ -bool Grp_DB_CheckIfAssociatedToGrps (const char *Table,const char *Field,long Cod) +void Grp_DB_CreateGroup (const struct Grp_Groups *Grps) { - /***** Trivial check *****/ - if (Cod <= 0) // Assignment, attendance event, survey, exam event or match code - return false; + /***** Create a new group *****/ + DB_QueryINSERT ("can not create group", + "INSERT INTO grp_groups" + " (GrpTypCod,GrpName,RooCod,MaxStudents,Open,FileZones)" + " VALUES" + " (%ld,'%s',%ld,%u,'N','N')", + Grps->GrpTyp.GrpTypCod, + Grps->GrpName, + Grps->RooCod, + Grps->MaxStudents); + } - /***** Get if an assignment, attendance event, survey, exam event or match - is associated to any group from database *****/ - return (DB_QueryCOUNT ("can not check if associated to groups", +/*****************************************************************************/ +/******************** Check if a group exists in database ********************/ +/*****************************************************************************/ + +bool Grp_DB_CheckIfGrpExists (long GrpCod) + { + return (DB_QueryCOUNT ("can not check if a group exists", "SELECT COUNT(*)" - " FROM %s" - " WHERE %s=%ld", - Table, - Field,Cod) != 0); + " FROM grp_groups" + " WHERE GrpCod=%ld", + GrpCod) != 0); + } + +/*****************************************************************************/ +/******************* Check if a group belongs to a course ********************/ +/*****************************************************************************/ + +bool Grp_DB_CheckIfGrpBelongsToCrs (long GrpCod,long CrsCod) + { + /***** Get if a group exists from database *****/ + return (DB_QueryCOUNT ("can not check if a group belongs to a course", + "SELECT COUNT(*)" + " FROM grp_groups," + "grp_types" + " WHERE grp_groups.GrpCod=%ld" + " AND grp_groups.GrpTypCod=grp_types.GrpTypCod" + " AND grp_types.CrsCod=%ld", + GrpCod,CrsCod) != 0); + } + +/*****************************************************************************/ +/******************* Check if name of group type exists **********************/ +/*****************************************************************************/ + +bool Grp_DB_CheckIfGrpTypNameExistsInCurrentCrs (const char *GrpTypName,long GrpTypCod) + { + /***** Get number of group types with a name from database *****/ + return (DB_QueryCOUNT ("can not check if the name of type of group" + " already existed", + "SELECT COUNT(*)" + " FROM grp_types" + " WHERE CrsCod=%ld" + " AND GrpTypName='%s'" + " AND GrpTypCod<>%ld", + Gbl.Hierarchy.Crs.CrsCod, + GrpTypName, + GrpTypCod) != 0); + } + +/*****************************************************************************/ +/************************ Check if name of group exists **********************/ +/*****************************************************************************/ + +bool Grp_DB_CheckIfGrpNameExistsForGrpTyp (long GrpTypCod,const char *GrpName,long GrpCod) + { + /***** Get number of groups with a type and a name from database *****/ + return (DB_QueryCOUNT ("can not check if the name of group already existed", + "SELECT COUNT(*)" + " FROM grp_groups" + " WHERE GrpTypCod=%ld" + " AND GrpName='%s'" + " AND GrpCod<>%ld", + GrpTypCod, + GrpName, + GrpCod) != 0); + } + +/*****************************************************************************/ +/********************* Count number of users in a group **********************/ +/*****************************************************************************/ + +unsigned Grp_DB_CountNumUsrsInGrp (Rol_Role_t Role,long GrpCod) + { + /***** Get number of students in a group from database *****/ + return (unsigned) + DB_QueryCOUNT ("can not get number of users in a group", + "SELECT COUNT(*)" + " FROM grp_users," + "grp_groups," + "grp_types," + "crs_users" + " WHERE grp_users.GrpCod=%ld" + " AND grp_users.GrpCod=grp_groups.GrpCod" + " AND grp_groups.GrpTypCod=grp_types.GrpTypCod" + " AND grp_types.CrsCod=crs_users.CrsCod" + " AND grp_users.UsrCod=crs_users.UsrCod" + " AND crs_users.Role=%u", + GrpCod, + (unsigned) Role); + } + +/*****************************************************************************/ +/*** Count # of users of current course not belonging to groups of a type ****/ +/*****************************************************************************/ + +unsigned Grp_DB_CountNumUsrsInNoGrpsOfType (Rol_Role_t Role,long GrpTypCod) + { + /***** Get number of users not belonging to groups of a type ******/ + return (unsigned) + DB_QueryCOUNT ("can not get the number of users" + " not belonging to groups of a type", + "SELECT COUNT(UsrCod)" + " FROM crs_users" + " WHERE CrsCod=%ld" + " AND Role=%u" + " AND UsrCod NOT IN" + " (SELECT DISTINCT grp_users.UsrCod" + " FROM grp_groups," + "grp_users" + " WHERE grp_groups.GrpTypCod=%ld" + " AND grp_groups.GrpCod=grp_users.GrpCod)", + Gbl.Hierarchy.Crs.CrsCod, + (unsigned) Role, + GrpTypCod); + } + +/*****************************************************************************/ +/********* Check if I belong to any groups of a given type I belong **********/ +/*****************************************************************************/ + +bool Grp_DB_CheckIfIBelongToGrpsOfType (long GrpTypCod) + { + /***** Get a group which I belong to from database *****/ + return (DB_QueryCOUNT ("can not check if you belong to a group type", + "SELECT COUNT(grp_groups.GrpCod)" + " FROM grp_groups," + "grp_users" + " WHERE grp_groups.GrpTypCod=%ld" + " AND grp_groups.GrpCod=grp_users.GrpCod" + " AND grp_users.UsrCod=%ld", // I belong + GrpTypCod, + Gbl.Usrs.Me.UsrDat.UsrCod) != 0); } /*****************************************************************************/ @@ -198,3 +336,108 @@ unsigned Grp_DB_GetGrpsOfType (MYSQL_RES **mysql_res,long GrpTypCod) " ORDER BY grp_groups.GrpName", GrpTypCod); } + +/*****************************************************************************/ +/********************** Get the type of group of a group *********************/ +/*****************************************************************************/ + +long Grp_DB_GetGrpTypeFromGrp (long GrpCod) + { + long GrpTypCod; + + /***** Get group type of a group from database *****/ + GrpTypCod = DB_QuerySELECTCode ("can not get the type of a group", + "SELECT GrpTypCod" + " FROM grp_groups" + " WHERE GrpCod=%ld", + GrpCod); + if (GrpTypCod <= 0) + Err_WrongGrpTypExit (); + + return GrpTypCod; + } + +/*****************************************************************************/ +/*********** Check if an assignment, attendance event, survey, ************/ +/*********** exam session or match is associated to a given group ************/ +/*****************************************************************************/ + +bool Grp_DB_CheckIfAssociatedToGrp (const char *Table,const char *Field, + long Cod,long GrpCod) + { + return (DB_QueryCOUNT ("can not check if associated to a group", + "SELECT COUNT(*)" + " FROM %s" + " WHERE %s=%ld" + " AND GrpCod=%ld", + Table, + Field,Cod, + GrpCod) != 0); + } + +/*****************************************************************************/ +/************ Check if an assignment, attendance event, survey, **************/ +/************ exam session or match is associated to any group **************/ +/*****************************************************************************/ + +bool Grp_DB_CheckIfAssociatedToGrps (const char *Table,const char *Field,long Cod) + { + /***** Trivial check *****/ + if (Cod <= 0) // Assignment, attendance event, survey, exam event or match code + return false; + + /***** Check if an assignment, attendance event, survey, + exam session or match is associated to any group *****/ + return (DB_QueryCOUNT ("can not check if associated to groups", + "SELECT COUNT(*)" + " FROM %s" + " WHERE %s=%ld", + Table, + Field,Cod) != 0); + } + +/*****************************************************************************/ +/*********************** Register a user in a group **************************/ +/*****************************************************************************/ + +void Grp_DB_AddUsrToGrp (long UsrCod,long GrpCod) + { + DB_QueryINSERT ("can not add a user to a group", + "INSERT INTO grp_users" + " (GrpCod,UsrCod)" + " VALUES" + " (%ld,%ld)", + GrpCod, + UsrCod); + } + +/*****************************************************************************/ +/*************** Remove a user of all the groups of a course *****************/ +/*****************************************************************************/ + +void Grp_DB_RemUsrFromAllGrpsInCrs (long UsrCod,long CrsCod) + { + DB_QueryDELETE ("can not remove a user from all groups of a course", + "DELETE FROM grp_users" + " WHERE UsrCod=%ld" + " AND GrpCod IN" + " (SELECT grp_groups.GrpCod" + " FROM grp_types," + "grp_groups" + " WHERE grp_types.CrsCod=%ld" + " AND grp_types.GrpTypCod=grp_groups.GrpTypCod)", + UsrCod, + CrsCod); + } + +/*****************************************************************************/ +/*********************** Remove a user from all groups ***********************/ +/*****************************************************************************/ + +void Grp_DB_RemUsrFromAllGrps (long UsrCod) + { + DB_QueryDELETE ("can not remove a user from the groups he/she belongs to", + "DELETE FROM grp_users" + " WHERE UsrCod=%ld", + UsrCod); + } diff --git a/swad_group_database.h b/swad_group_database.h index f1c9fc81..82759800 100644 --- a/swad_group_database.h +++ b/swad_group_database.h @@ -27,6 +27,7 @@ /********************************* Headers ***********************************/ /*****************************************************************************/ +#include "swad_group.h" // #include "swad_info.h" // #include "swad_room.h" // #include "swad_user.h" @@ -46,12 +47,33 @@ void Grp_DB_LockTables (void); void Grp_DB_UnlockTables (void); -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); +long Grp_DB_CreateGroupType (const struct GroupType *GrpTyp); +void Grp_DB_CreateGroup (const struct Grp_Groups *Grps); + +bool Grp_DB_CheckIfGrpExists (long GrpCod); +bool Grp_DB_CheckIfGrpBelongsToCrs (long GrpCod,long CrsCod); + +bool Grp_DB_CheckIfGrpTypNameExistsInCurrentCrs (const char *GrpTypName,long GrpTypCod); +bool Grp_DB_CheckIfGrpNameExistsForGrpTyp (long GrpTypCod,const char *GrpName,long GrpCod); + +unsigned Grp_DB_CountNumUsrsInGrp (Rol_Role_t Role,long GrpCod); +unsigned Grp_DB_CountNumUsrsInNoGrpsOfType (Rol_Role_t Role,long GrpTypCod); + +bool Grp_DB_CheckIfIBelongToGrpsOfType (long GrpTypCod); unsigned Grp_DB_CountNumGrpsInCurrentCrs (void); unsigned Grp_DB_CountNumGrpsInThisCrsOfType (long GrpTypCod); unsigned Grp_DB_GetGrpsOfType (MYSQL_RES **mysql_res,long GrpTypCod); +long Grp_DB_GetGrpTypeFromGrp (long GrpCod); + +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_AddUsrToGrp (long UsrCod,long GrpCod); + +void Grp_DB_RemUsrFromAllGrpsInCrs (long UsrCod,long CrsCod); +void Grp_DB_RemUsrFromAllGrps (long UsrCod); + #endif diff --git a/swad_mark.c b/swad_mark.c index c4e3cf7f..f96a6d9a 100644 --- a/swad_mark.c +++ b/swad_mark.c @@ -37,6 +37,7 @@ #include "swad_error.h" #include "swad_form.h" #include "swad_global.h" +#include "swad_group_database.h" #include "swad_hierarchy_level.h" #include "swad_HTML.h" #include "swad_ID.h"