diff --git a/swad_center.c b/swad_center.c index ced54aaf..7cc5c030 100644 --- a/swad_center.c +++ b/swad_center.c @@ -1763,7 +1763,7 @@ static void Ctr_FormToGoToMap (struct Hie_Node *Ctr) /*****************************************************************************/ /***** Get all my centers (those of my courses) and store them in a list *****/ /*****************************************************************************/ - +/* void Ctr_GetMyCenters (void) { MYSQL_RES *mysql_res; @@ -1772,64 +1772,46 @@ void Ctr_GetMyCenters (void) unsigned NumCtrs; long CtrCod; - /***** If my centers are yet filled, there's nothing to do *****/ - if (!Gbl.Usrs.Me.MyCtrs.Filled) + ***** If my centers are yet filled, there's nothing to do ***** + if (!Gbl.Usrs.Me.Hierarchy[Hie_CTR].Filled) { - Gbl.Usrs.Me.MyCtrs.Num = 0; - Gbl.Usrs.Me.MyCtrs.Ctrs = NULL; + Gbl.Usrs.Me.Hierarchy[Hie_CTR].Num = 0; + Gbl.Usrs.Me.Hierarchy[Hie_CTR].Nodes = NULL; - /***** Get my centers from database *****/ + ***** Get my centers from database ***** NumCtrs = Ctr_DB_GetCtrsFromUsr (&mysql_res, Gbl.Usrs.Me.UsrDat.UsrCod,-1L); if (NumCtrs) { - if ((Gbl.Usrs.Me.MyCtrs.Ctrs = malloc (NumCtrs * - sizeof (*Gbl.Usrs.Me.MyCtrs.Ctrs))) == NULL) + if ((Gbl.Usrs.Me.Hierarchy[Hie_CTR].Nodes = malloc (NumCtrs * + sizeof (*Gbl.Usrs.Me.Hierarchy[Hie_CTR].Nodes))) == NULL) Err_NotEnoughMemoryExit (); for (NumCtr = 0; NumCtr < NumCtrs; NumCtr++) { - /* Get next center */ + * Get next center * row = mysql_fetch_row (mysql_res); - /* Get center code */ + * Get center code * if ((CtrCod = Str_ConvertStrCodToLongCod (row[0])) > 0) { - Gbl.Usrs.Me.MyCtrs.Ctrs[Gbl.Usrs.Me.MyCtrs.Num].HieCod = CtrCod; - Gbl.Usrs.Me.MyCtrs.Ctrs[Gbl.Usrs.Me.MyCtrs.Num].MaxRole = Rol_ConvertUnsignedStrToRole (row[1]); + Gbl.Usrs.Me.Hierarchy[Hie_CTR].Nodes[Gbl.Usrs.Me.Hierarchy[Hie_CTR].Num].HieCod = CtrCod; + Gbl.Usrs.Me.Hierarchy[Hie_CTR].Nodes[Gbl.Usrs.Me.Hierarchy[Hie_CTR].Num].MaxRole = Rol_ConvertUnsignedStrToRole (row[1]); - Gbl.Usrs.Me.MyCtrs.Num++; + Gbl.Usrs.Me.Hierarchy[Hie_CTR].Num++; } } } - /***** Free structure that stores the query result *****/ + ***** Free structure that stores the query result ***** DB_FreeMySQLResult (&mysql_res); - /***** Set boolean that indicates that my centers are yet filled *****/ - Gbl.Usrs.Me.MyCtrs.Filled = true; + ***** Set boolean that indicates that my centers are yet filled ***** + Gbl.Usrs.Me.Hierarchy[Hie_CTR].Filled = true; } } - -/*****************************************************************************/ -/************************ Free the list of my centers ************************/ -/*****************************************************************************/ - -void Ctr_FreeMyCenters (void) - { - if (Gbl.Usrs.Me.MyCtrs.Filled) - { - /***** Reset list *****/ - Gbl.Usrs.Me.MyCtrs.Filled = false; - if (Gbl.Usrs.Me.MyCtrs.Num && - Gbl.Usrs.Me.MyCtrs.Ctrs) - free (Gbl.Usrs.Me.MyCtrs.Ctrs); - Gbl.Usrs.Me.MyCtrs.Ctrs = NULL; - Gbl.Usrs.Me.MyCtrs.Num = 0; - } - } - +*/ /*****************************************************************************/ /*********************** Check if I belong to a center ***********************/ /*****************************************************************************/ @@ -1839,13 +1821,14 @@ bool Ctr_CheckIfIBelongToCtr (long CtrCod) unsigned NumMyCtr; /***** Fill the list with the centers I belong to *****/ - Ctr_GetMyCenters (); + // Ctr_GetMyCenters (); + Hie_GetMyHierarchy (Hie_CTR); /***** Check if the center passed as parameter is any of my centers *****/ for (NumMyCtr = 0; - NumMyCtr < Gbl.Usrs.Me.MyCtrs.Num; + NumMyCtr < Gbl.Usrs.Me.Hierarchy[Hie_CTR].Num; NumMyCtr++) - if (Gbl.Usrs.Me.MyCtrs.Ctrs[NumMyCtr].HieCod == CtrCod) + if (Gbl.Usrs.Me.Hierarchy[Hie_CTR].Nodes[NumMyCtr].HieCod == CtrCod) return true; return false; } diff --git a/swad_center.h b/swad_center.h index eed27e2d..9bb9a137 100644 --- a/swad_center.h +++ b/swad_center.h @@ -73,8 +73,7 @@ unsigned Ctr_GetCachedNumCtrsWithUsrs (Rol_Role_t Role); void Ctr_ListCtrsFound (MYSQL_RES **mysql_res,unsigned NumCtrs); -void Ctr_GetMyCenters (void); -void Ctr_FreeMyCenters (void); +// void Ctr_GetMyCenters (void); bool Ctr_CheckIfIBelongToCtr (long CtrCod); #endif diff --git a/swad_center_database.c b/swad_center_database.c index 22b33b9a..32b83d5a 100644 --- a/swad_center_database.c +++ b/swad_center_database.c @@ -724,10 +724,10 @@ unsigned Ctr_DB_GetCtrsWithCoordsInCurrentIns (MYSQL_RES **mysql_res) /*****************************************************************************/ // Returns the number of rows of the result -unsigned Ctr_DB_GetCtrsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long InsCod) +unsigned Ctr_DB_GetCtrsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long HieCod) { /***** Get from database the centers a user belongs to *****/ - if (InsCod > 0) + if (HieCod > 0) return (unsigned) DB_QuerySELECT (mysql_res,"can not check the centers a user belongs to", "SELECT ctr_centers.CtrCod," // row[0] @@ -744,7 +744,7 @@ unsigned Ctr_DB_GetCtrsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long InsCod) " GROUP BY ctr_centers.CtrCod" " ORDER BY ctr_centers.ShortName", UsrCod, - InsCod); + HieCod); else return (unsigned) DB_QuerySELECT (mysql_res,"can not check the centers a user belongs to", diff --git a/swad_center_database.h b/swad_center_database.h index 38b08094..77bfd09b 100644 --- a/swad_center_database.h +++ b/swad_center_database.h @@ -85,7 +85,7 @@ void Ctr_DB_GetAvgCoordAndZoomInCurrentIns (struct Map_Coordinates *Coord,unsign unsigned Ctr_DB_GetCtrsWithCoords (MYSQL_RES **mysql_res); unsigned Ctr_DB_GetCtrsWithCoordsInCurrentIns (MYSQL_RES **mysql_res); -unsigned Ctr_DB_GetCtrsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long InsCod); +unsigned Ctr_DB_GetCtrsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long HieCod); bool Ctr_DB_CheckIfUsrBelongsToCtr (long UsrCod,long HieCod, bool CountOnlyAcceptedCourses); unsigned Ctr_DB_GetNumUsrsWhoClaimToBelongToCtr (long CtrCod); diff --git a/swad_changelog.h b/swad_changelog.h index c1548043..f07f899a 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -632,10 +632,12 @@ TODO: Francisco Javier Fern Me sale este error, no sé si por no recordar yo la sintaxis apropiada para mandar a varios destinatarios. ¿No era así? "can npt create received message (duplicated entry '243218-2160773' for key 'UsrCod_MsgCod') */ -#define Log_PLATFORM_VERSION "SWAD 23.21 (2023-10-03)" +#define Log_PLATFORM_VERSION "SWAD 23.23 (2023-10-03)" #define CSS_FILE "swad23.16.1.css" #define JS_FILE "swad22.49.js" /* + Version 23.23: Oct 03, 2023 Code refactoring un user's hierarchy. (336620 lines) + Version 23.22: Oct 03, 2023 Code refactoring un user's hierarchy. (336518 lines) Version 23.21: Oct 03, 2023 User's hierarchy is allocated dinamically. (336598 lines) Version 23.20.1: Oct 02, 2023 Fixed minor CSS issue in games and exams. (336576 lines) Version 23.20: Sep 29, 2023 Removed button to change between view and edit mode in in file browser. (336571 lines) diff --git a/swad_chat.c b/swad_chat.c index a19c25f3..409607db 100644 --- a/swad_chat.c +++ b/swad_chat.c @@ -126,7 +126,8 @@ void Cht_ShowListOfAvailableChatRooms (void) char ThisRoomFullName[Cht_MAX_BYTES_ROOM_FULL_NAME + 1]; /***** Fill the list with the degrees I belong to *****/ - Deg_GetMyDegrees (); + // Deg_GetMyDegrees (); + Hie_GetMyHierarchy (Hie_DEG); /***** Begin box *****/ Box_BoxBegin (NULL,Txt_Chat_rooms, @@ -138,14 +139,14 @@ void Cht_ShowListOfAvailableChatRooms (void) /***** Link to chat available for all users *****/ IsLastItemInLevel[1] = (!Gbl.Usrs.Me.IBelongToCurrentCrs && - !Gbl.Usrs.Me.MyDegs.Num); + !Gbl.Usrs.Me.Hierarchy[Hie_DEG].Num); snprintf (ThisRoomFullName,sizeof (ThisRoomFullName),"%s (%s)", Txt_General,Txt_SEX_PLURAL_abc[Usr_SEX_ALL]); Cht_WriteLinkToChat1 ("GBL_USR",Txt_SEX_PLURAL_Abc[Usr_SEX_ALL],ThisRoomFullName,1,IsLastItemInLevel); Ico_PutIcon ("comments.svg",Ico_BLACK,ThisRoomFullName,"ICO16x16"); Cht_WriteLinkToChat2 ("GBL_USR",ThisRoomFullName); - IsLastItemInLevel[1] = !Gbl.Usrs.Me.MyDegs.Num; + IsLastItemInLevel[1] = !Gbl.Usrs.Me.Hierarchy[Hie_DEG].Num; switch (Gbl.Usrs.Me.Role.Logged) { case Rol_STD: @@ -171,16 +172,16 @@ void Cht_ShowListOfAvailableChatRooms (void) /***** Link to chat of users from my degrees *****/ for (NumMyDeg = 0; - NumMyDeg < Gbl.Usrs.Me.MyDegs.Num; + NumMyDeg < Gbl.Usrs.Me.Hierarchy[Hie_DEG].Num; NumMyDeg++) { /* Get data of this degree */ - Deg.HieCod = Gbl.Usrs.Me.MyDegs.Degs[NumMyDeg].HieCod; + Deg.HieCod = Gbl.Usrs.Me.Hierarchy[Hie_DEG].Nodes[NumMyDeg].HieCod; if (!Deg_GetDegreeDataByCod (&Deg)) Err_WrongDegreeExit (); /* Link to the room of this degree */ - IsLastItemInLevel[1] = (NumMyDeg == Gbl.Usrs.Me.MyDegs.Num - 1); + IsLastItemInLevel[1] = (NumMyDeg == Gbl.Usrs.Me.Hierarchy[Hie_DEG].Num - 1); snprintf (ThisRoomCode,sizeof (ThisRoomCode),"DEG_%ld",Deg.HieCod); snprintf (ThisRoomShrtName,sizeof (ThisRoomShrtName),"%s", Deg.ShrtName); @@ -406,8 +407,10 @@ void Cht_OpenChatWindow (void) Err_ShowErrorAndExit ("Wrong code of chat room."); /***** Fill the lists with the degrees and courses I belong to *****/ - Deg_GetMyDegrees (); - Enr_GetMyCourses (); + // Deg_GetMyDegrees (); + Hie_GetMyHierarchy (Hie_DEG); + // Enr_GetMyCourses (); + Hie_GetMyHierarchy (Hie_CRS); /***** Build my user's name *****/ Str_Copy (UsrName,Gbl.Usrs.Me.UsrDat.Surname1,sizeof (UsrName) - 1); @@ -467,18 +470,18 @@ void Cht_OpenChatWindow (void) } for (NumMyDeg = 0; - NumMyDeg < Gbl.Usrs.Me.MyDegs.Num; + NumMyDeg < Gbl.Usrs.Me.Hierarchy[Hie_DEG].Num; NumMyDeg++) { snprintf (ThisRoomCode,sizeof (ThisRoomCode),"DEG_%ld", - Gbl.Usrs.Me.MyDegs.Degs[NumMyDeg].HieCod); + Gbl.Usrs.Me.Hierarchy[Hie_DEG].Nodes[NumMyDeg].HieCod); if (strcmp (RoomCode,ThisRoomCode)) { Str_Concat (ListRoomCodes,"|#",sizeof (ListRoomCodes) - 1); Str_Concat (ListRoomCodes,ThisRoomCode,sizeof (ListRoomCodes) - 1); /* Get data of this degree */ - Deg.HieCod = Gbl.Usrs.Me.MyDegs.Degs[NumMyDeg].HieCod; + Deg.HieCod = Gbl.Usrs.Me.Hierarchy[Hie_DEG].Nodes[NumMyDeg].HieCod; Deg_GetDegreeDataByCod (&Deg); snprintf (ThisRoomShortName,sizeof (ThisRoomShortName),"%s", @@ -494,18 +497,18 @@ void Cht_OpenChatWindow (void) } for (NumMyCrs = 0; - NumMyCrs < Gbl.Usrs.Me.MyCrss.Num; + NumMyCrs < Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num; NumMyCrs++) { snprintf (ThisRoomCode,sizeof (ThisRoomCode),"CRS_%ld", - Gbl.Usrs.Me.MyCrss.Crss[NumMyCrs].HieCod); + Gbl.Usrs.Me.Hierarchy[Hie_CRS].Nodes[NumMyCrs].HieCod); if (strcmp (RoomCode,ThisRoomCode)) { Str_Concat (ListRoomCodes,"|#",sizeof (ListRoomCodes) - 1); Str_Concat (ListRoomCodes,ThisRoomCode,sizeof (ListRoomCodes) - 1); /* Get data of this course */ - Crs.HieCod = Gbl.Usrs.Me.MyCrss.Crss[NumMyCrs].HieCod; + Crs.HieCod = Gbl.Usrs.Me.Hierarchy[Hie_CRS].Nodes[NumMyCrs].HieCod; Crs_GetCourseDataByCod (&Crs); snprintf (ThisRoomShortName,sizeof (ThisRoomShortName), diff --git a/swad_country.c b/swad_country.c index 8d134503..c31b3510 100644 --- a/swad_country.c +++ b/swad_country.c @@ -1777,7 +1777,7 @@ static void Cty_FormToGoToMap (struct Hie_Node *Cty) /*****************************************************************************/ /**** Get all my countries (those of my courses) and store them in a list ****/ /*****************************************************************************/ - +/* void Cty_GetMyCountrs (void) { MYSQL_RES *mysql_res; @@ -1786,63 +1786,45 @@ void Cty_GetMyCountrs (void) unsigned NumCtys; long CtyCod; - /***** If my countries are yet filled, there's nothing to do *****/ - if (!Gbl.Usrs.Me.MyCtys.Filled) + ***** If my countries are yet filled, there's nothing to do ***** + if (!Gbl.Usrs.Me.Hierarchy[Hie_CTY].Filled) { - Gbl.Usrs.Me.MyCtys.Num = 0; - Gbl.Usrs.Me.MyCtys.Ctys = NULL; + Gbl.Usrs.Me.Hierarchy[Hie_CTY].Num = 0; + Gbl.Usrs.Me.Hierarchy[Hie_CTY].Nodes = NULL; - /***** Get my institutions from database *****/ - NumCtys = Cty_DB_GetCtysFromUsr (&mysql_res,Gbl.Usrs.Me.UsrDat.UsrCod); + ***** Get my countries from database ***** + NumCtys = Cty_DB_GetCtysFromUsr (&mysql_res,Gbl.Usrs.Me.UsrDat.UsrCod,-1L); if (NumCtys) { - if ((Gbl.Usrs.Me.MyCtys.Ctys = malloc (NumCtys * - sizeof (*Gbl.Usrs.Me.MyCtys.Ctys))) == NULL) + if ((Gbl.Usrs.Me.Hierarchy[Hie_CTY].Nodes = malloc (NumCtys * + sizeof (*Gbl.Usrs.Me.Hierarchy[Hie_CTY].Nodes))) == NULL) Err_NotEnoughMemoryExit (); for (NumCty = 0; NumCty < NumCtys; NumCty++) { - /* Get next country */ + * Get next country * row = mysql_fetch_row (mysql_res); - /* Get country code */ + * Get country code * if ((CtyCod = Str_ConvertStrCodToLongCod (row[0])) > 0) { - Gbl.Usrs.Me.MyCtys.Ctys[Gbl.Usrs.Me.MyCtys.Num].HieCod = CtyCod; - Gbl.Usrs.Me.MyCtys.Ctys[Gbl.Usrs.Me.MyCtys.Num].MaxRole = Rol_ConvertUnsignedStrToRole (row[1]); + Gbl.Usrs.Me.Hierarchy[Hie_CTY].Nodes[Gbl.Usrs.Me.Hierarchy[Hie_CTY].Num].HieCod = CtyCod; + Gbl.Usrs.Me.Hierarchy[Hie_CTY].Nodes[Gbl.Usrs.Me.Hierarchy[Hie_CTY].Num].MaxRole = Rol_ConvertUnsignedStrToRole (row[1]); - Gbl.Usrs.Me.MyCtys.Num++; + Gbl.Usrs.Me.Hierarchy[Hie_CTY].Num++; } } } - /***** Free structure that stores the query result *****/ + ***** Free structure that stores the query result ***** DB_FreeMySQLResult (&mysql_res); - /***** Set boolean that indicates that my institutions are yet filled *****/ - Gbl.Usrs.Me.MyCtys.Filled = true; + ***** Set boolean that indicates that my institutions are yet filled ***** + Gbl.Usrs.Me.Hierarchy[Hie_CTY].Filled = true; } } - -/*****************************************************************************/ -/************************ Free the list of my countries ************************/ -/*****************************************************************************/ - -void Cty_FreeMyCountrs (void) - { - if (Gbl.Usrs.Me.MyCtys.Filled) - { - /***** Reset list *****/ - Gbl.Usrs.Me.MyCtys.Filled = false; - if (Gbl.Usrs.Me.MyCtys.Num && - Gbl.Usrs.Me.MyCtys.Ctys) - free (Gbl.Usrs.Me.MyCtys.Ctys); - Gbl.Usrs.Me.MyCtys.Ctys = NULL; - Gbl.Usrs.Me.MyCtys.Num = 0; - } - } - +*/ /*****************************************************************************/ /********************** Check if I belong to a country **********************/ /*****************************************************************************/ @@ -1852,13 +1834,14 @@ bool Cty_CheckIfIBelongToCty (long CtyCod) unsigned NumMyCty; /***** Fill the list with the institutions I belong to *****/ - Cty_GetMyCountrs (); + // Cty_GetMyCountrs (); + Hie_GetMyHierarchy (Hie_CTY); /***** Check if the country passed as parameter is any of my countries *****/ for (NumMyCty = 0; - NumMyCty < Gbl.Usrs.Me.MyCtys.Num; + NumMyCty < Gbl.Usrs.Me.Hierarchy[Hie_CTY].Num; NumMyCty++) - if (Gbl.Usrs.Me.MyCtys.Ctys[NumMyCty].HieCod == CtyCod) + if (Gbl.Usrs.Me.Hierarchy[Hie_CTY].Nodes[NumMyCty].HieCod == CtyCod) return true; return false; } diff --git a/swad_country.h b/swad_country.h index dbb2cb3d..75685c26 100644 --- a/swad_country.h +++ b/swad_country.h @@ -84,8 +84,7 @@ unsigned Cty_GetCachedNumCtysWithUsrs (Rol_Role_t Role); void Cty_ListCtysFound (MYSQL_RES **mysql_res,unsigned NumCtys); -void Cty_GetMyCountrs (void); -void Cty_FreeMyCountrs (void); +// void Cty_GetMyCountrs (void); bool Cty_CheckIfIBelongToCty (long CtyCod); void Cty_FlushCacheNumUsrsWhoDontClaimToBelongToAnyCty (void); diff --git a/swad_country_database.c b/swad_country_database.c index a8173835..b7311b0e 100644 --- a/swad_country_database.c +++ b/swad_country_database.c @@ -599,7 +599,8 @@ void Cty_DB_UpdateCtyMapAttr (const char NewMapAttribution[Med_MAX_BYTES_ATTRIBU /*****************************************************************************/ // Returns the number of rows of the result -unsigned Cty_DB_GetCtysFromUsr (MYSQL_RES **mysql_res,long UsrCod) +unsigned Cty_DB_GetCtysFromUsr (MYSQL_RES **mysql_res, + long UsrCod,__attribute__((unused)) long HieCod) { extern const char *Lan_STR_LANG_ID[1 + Lan_NUM_LANGUAGES]; diff --git a/swad_country_database.h b/swad_country_database.h index e19cdd65..d4fd6b91 100644 --- a/swad_country_database.h +++ b/swad_country_database.h @@ -75,7 +75,8 @@ bool Cty_DB_CheckIfMapIsAvailable (long CtyCod); void Cty_DB_UpdateCtyField (long CtyCod,const char *FldName,const char *FldValue); void Cty_DB_UpdateCtyMapAttr (const char NewMapAttribution[Med_MAX_BYTES_ATTRIBUTION + 1]); -unsigned Cty_DB_GetCtysFromUsr (MYSQL_RES **mysql_res,long UsrCod); +unsigned Cty_DB_GetCtysFromUsr (MYSQL_RES **mysql_res, + long UsrCod,__attribute__((unused)) long HieCod); unsigned Cty_DB_GetNumUsrsWhoDontClaimToBelongToAnyCty (void); unsigned Cty_DB_GetNumUsrsWhoClaimToBelongToAnotherCty (void); unsigned Cty_DB_GetNumUsrsWhoClaimToBelongToCty (long CtyCod); diff --git a/swad_course.c b/swad_course.c index fb5e5d96..a18f5fe1 100644 --- a/swad_course.c +++ b/swad_course.c @@ -199,15 +199,15 @@ static void Crs_WriteListMyCoursesToSelectOne (void) HTM_LI_End (); /***** Get my countries *****/ - NumCtys = Cty_DB_GetCtysFromUsr (&mysql_resCty,Gbl.Usrs.Me.UsrDat.UsrCod); + NumCtys = Cty_DB_GetCtysFromUsr (&mysql_resCty,Gbl.Usrs.Me.UsrDat.UsrCod,-1L); for (NumCty = 0; NumCty < NumCtys; NumCty++) { - /***** Get next institution *****/ + /***** Get next country *****/ row = mysql_fetch_row (mysql_resCty); - /***** Get data of this institution *****/ + /***** Get data of this country *****/ Hie[Hie_CTY].HieCod = Str_ConvertStrCodToLongCod (row[0]); if (!Cty_GetBasicCountryDataByCod (&Hie[Hie_CTY])) Err_WrongCountrExit (); @@ -575,10 +575,11 @@ void Crs_WriteSelectorMyCoursesInBreadcrumb (void) /***** Fill the list with the courses I belong to, if not filled *****/ if (Gbl.Usrs.Me.Logged) - Enr_GetMyCourses (); + // Enr_GetMyCourses (); + Hie_GetMyHierarchy (Hie_CRS); /***** Begin form *****/ - Frm_BeginFormGoTo (Gbl.Usrs.Me.MyCrss.Num ? ActSeePrg : + Frm_BeginFormGoTo (Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num ? ActSeePrg : ActReqSch); /***** Begin selector of courses *****/ @@ -593,15 +594,15 @@ void Crs_WriteSelectorMyCoursesInBreadcrumb (void) HTM_OPTION_DISABLED, "%s",Txt_Course); - if (Gbl.Usrs.Me.MyCrss.Num) + if (Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num) { /***** Write an option for each of my courses *****/ for (NumMyCrs = 0, LastDegCod = -1L; - NumMyCrs < Gbl.Usrs.Me.MyCrss.Num; + NumMyCrs < Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num; NumMyCrs++) { - CrsCod = Gbl.Usrs.Me.MyCrss.Crss[NumMyCrs].HieCod; - DegCod = Gbl.Usrs.Me.MyCrss.Crss[NumMyCrs].PrtCod; + CrsCod = Gbl.Usrs.Me.Hierarchy[Hie_CRS].Nodes[NumMyCrs].HieCod; + DegCod = Gbl.Usrs.Me.Hierarchy[Hie_CRS].Nodes[NumMyCrs].PrtCod; Crs_DB_GetShortNamesByCod (CrsCod,CrsShortName,DegShortName); @@ -1954,10 +1955,11 @@ static void Crs_PutButtonToRegisterInCrs (void) void Crs_ReqSelectOneOfMyCourses (void) { /***** Fill the list with the courses I belong to, if not filled *****/ - Enr_GetMyCourses (); + // Enr_GetMyCourses (); + Hie_GetMyHierarchy (Hie_CRS); /***** Select one of my courses *****/ - if (Gbl.Usrs.Me.MyCrss.Num) + if (Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num) /* Show my courses */ Crs_WriteListMyCoursesToSelectOne (); else // I am not enroled in any course diff --git a/swad_course_database.c b/swad_course_database.c index 503e6fa0..0fdfe368 100644 --- a/swad_course_database.c +++ b/swad_course_database.c @@ -446,9 +446,9 @@ unsigned Crs_DB_GetNumCrssWithUsrs (Rol_Role_t Role, /*****************************************************************************/ // Returns the number of rows of the result -unsigned Crs_DB_GetCrssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long DegCod) +unsigned Crs_DB_GetCrssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long PrtCod) { - if (DegCod > 0) // Courses in a degree + if (PrtCod > 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] @@ -461,7 +461,7 @@ unsigned Crs_DB_GetCrssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long DegCod) " AND crs_courses.DegCod=%ld" " ORDER BY crs_courses.ShortName", UsrCod, - DegCod); + PrtCod); else // All the courses return (unsigned) DB_QuerySELECT (mysql_res,"can not get the courses a user belongs to", diff --git a/swad_course_database.h b/swad_course_database.h index b86670c3..82261dd0 100644 --- a/swad_course_database.h +++ b/swad_course_database.h @@ -63,7 +63,7 @@ unsigned Crs_DB_GetNumCrssInDeg (long DegCod); unsigned Crs_DB_GetNumCrssWithUsrs (Rol_Role_t Role, Hie_Level_t Level,long HieCod); -unsigned Crs_DB_GetCrssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long DegCod); +unsigned Crs_DB_GetCrssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long PrtCod); void Crs_DB_UpdateInstitutionalCrsCod (long CrsCod,const char *NewInstitutionalCrsCod); void Crs_DB_UpdateCrsYear (long CrsCod,unsigned NewYear); diff --git a/swad_degree.c b/swad_degree.c index 1962bb48..d689b565 100644 --- a/swad_degree.c +++ b/swad_degree.c @@ -1645,7 +1645,7 @@ static void Deg_EditingDegreeDestructor (void) /*****************************************************************************/ /***** Get all my degrees (those of my courses) and store them in a list *****/ /*****************************************************************************/ - +/* void Deg_GetMyDegrees (void) { MYSQL_RES *mysql_res; @@ -1654,64 +1654,46 @@ void Deg_GetMyDegrees (void) unsigned NumDegs; long DegCod; - /***** If my degrees are yet filled, there's nothing to do *****/ - if (!Gbl.Usrs.Me.MyDegs.Filled) + ***** If my degrees are yet filled, there's nothing to do ***** + if (!Gbl.Usrs.Me.Hierarchy[Hie_DEG].Filled) { - Gbl.Usrs.Me.MyDegs.Num = 0; - Gbl.Usrs.Me.MyDegs.Degs = NULL; + Gbl.Usrs.Me.Hierarchy[Hie_DEG].Num = 0; + Gbl.Usrs.Me.Hierarchy[Hie_DEG].Nodes = NULL; - /***** Get my degrees from database *****/ + ***** Get my degrees from database ***** NumDegs = Deg_DB_GetDegsFromUsr (&mysql_res, Gbl.Usrs.Me.UsrDat.UsrCod,-1L); if (NumDegs) { - if ((Gbl.Usrs.Me.MyDegs.Degs = malloc (NumDegs * - sizeof (*Gbl.Usrs.Me.MyDegs.Degs))) == NULL) + if ((Gbl.Usrs.Me.Hierarchy[Hie_DEG].Nodes = malloc (NumDegs * + sizeof (*Gbl.Usrs.Me.Hierarchy[Hie_DEG].Nodes))) == NULL) Err_NotEnoughMemoryExit (); for (NumDeg = 0; NumDeg < NumDegs; NumDeg++) { - /* Get next degree */ + * Get next degree * row = mysql_fetch_row (mysql_res); - /* Get degree code */ + * Get degree code * if ((DegCod = Str_ConvertStrCodToLongCod (row[0])) > 0) { - Gbl.Usrs.Me.MyDegs.Degs[Gbl.Usrs.Me.MyDegs.Num].HieCod = DegCod; - Gbl.Usrs.Me.MyDegs.Degs[Gbl.Usrs.Me.MyDegs.Num].MaxRole = Rol_ConvertUnsignedStrToRole (row[1]); + Gbl.Usrs.Me.Hierarchy[Hie_DEG].Nodes[Gbl.Usrs.Me.Hierarchy[Hie_DEG].Num].HieCod = DegCod; + Gbl.Usrs.Me.Hierarchy[Hie_DEG].Nodes[Gbl.Usrs.Me.Hierarchy[Hie_DEG].Num].MaxRole = Rol_ConvertUnsignedStrToRole (row[1]); - Gbl.Usrs.Me.MyDegs.Num++; + Gbl.Usrs.Me.Hierarchy[Hie_DEG].Num++; } } } - /***** Free structure that stores the query result *****/ + ***** Free structure that stores the query result ***** DB_FreeMySQLResult (&mysql_res); - /***** Set boolean that indicates that my degrees are yet filled *****/ - Gbl.Usrs.Me.MyDegs.Filled = true; + ***** Set boolean that indicates that my degrees are yet filled ***** + Gbl.Usrs.Me.Hierarchy[Hie_DEG].Filled = true; } } - -/*****************************************************************************/ -/************************ Free the list of my degrees ************************/ -/*****************************************************************************/ - -void Deg_FreeMyDegrees (void) - { - if (Gbl.Usrs.Me.MyDegs.Filled) - { - /***** Reset list *****/ - Gbl.Usrs.Me.MyDegs.Filled = false; - if (Gbl.Usrs.Me.MyDegs.Num && - Gbl.Usrs.Me.MyDegs.Degs) - free (Gbl.Usrs.Me.MyDegs.Degs); - Gbl.Usrs.Me.MyDegs.Degs = NULL; - Gbl.Usrs.Me.MyDegs.Num = 0; - } - } - +*/ /*****************************************************************************/ /*********************** Check if I belong to a degree ***********************/ /*****************************************************************************/ @@ -1721,13 +1703,14 @@ bool Deg_CheckIfIBelongToDeg (long DegCod) unsigned NumMyDeg; /***** Fill the list with the degrees I belong to *****/ - Deg_GetMyDegrees (); + // Deg_GetMyDegrees (); + Hie_GetMyHierarchy (Hie_DEG); /***** Check if the degree passed as parameter is any of my degrees *****/ for (NumMyDeg = 0; - NumMyDeg < Gbl.Usrs.Me.MyDegs.Num; + NumMyDeg < Gbl.Usrs.Me.Hierarchy[Hie_DEG].Num; NumMyDeg++) - if (Gbl.Usrs.Me.MyDegs.Degs[NumMyDeg].HieCod == DegCod) + if (Gbl.Usrs.Me.Hierarchy[Hie_DEG].Nodes[NumMyDeg].HieCod == DegCod) return true; return false; } diff --git a/swad_degree.h b/swad_degree.h index f644271b..d5cda7de 100644 --- a/swad_degree.h +++ b/swad_degree.h @@ -83,8 +83,7 @@ unsigned Deg_GetCachedNumDegsWithUsrs (Rol_Role_t Role); void Deg_ListDegsFound (MYSQL_RES **mysql_res,unsigned NumCrss); -void Deg_GetMyDegrees (void); -void Deg_FreeMyDegrees (void); +// void Deg_GetMyDegrees (void); bool Deg_CheckIfIBelongToDeg (long DegCod); void Deg_GetUsrMainDeg (long UsrCod, char ShrtName[Hie_MAX_BYTES_SHRT_NAME + 1], diff --git a/swad_degree_database.c b/swad_degree_database.c index 496ed1a8..dfb376e4 100644 --- a/swad_degree_database.c +++ b/swad_degree_database.c @@ -761,9 +761,9 @@ void Deg_DB_UpdateDegStatus (long DegCod,Hie_Status_t NewStatus) /*****************************************************************************/ // Returns the number of rows of the result -unsigned Deg_DB_GetDegsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long CtrCod) +unsigned Deg_DB_GetDegsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long HieCod) { - if (CtrCod > 0) + if (HieCod > 0) return (unsigned) DB_QuerySELECT (mysql_res,"can not check the degrees a user belongs to", "SELECT deg_degrees.DegCod," // row[0] @@ -778,7 +778,7 @@ unsigned Deg_DB_GetDegsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long CtrCod) " GROUP BY deg_degrees.DegCod" " ORDER BY deg_degrees.ShortName", UsrCod, - CtrCod); + HieCod); else return (unsigned) DB_QuerySELECT (mysql_res,"can not check the degrees a user belongs to", diff --git a/swad_degree_database.h b/swad_degree_database.h index 1f9838e0..1b288764 100644 --- a/swad_degree_database.h +++ b/swad_degree_database.h @@ -80,7 +80,7 @@ void Deg_DB_UpdateDegTyp (long DegCod,long NewDegTypCod); void Deg_DB_UpdateDegWWW (long DegCod,const char NewWWW[Cns_MAX_BYTES_WWW + 1]); void Deg_DB_UpdateDegStatus (long DegCod,Hie_Status_t NewStatus); -unsigned Deg_DB_GetDegsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long CtrCod); +unsigned Deg_DB_GetDegsFromUsr (MYSQL_RES **mysql_res,long UsrCod,long HieCod); unsigned Deg_DB_GetUsrMainDeg (MYSQL_RES **mysql_res,long UsrCod); bool Deg_DB_CheckIfUsrBelongsToDeg (long UsrCod,long HieCod, bool CountOnlyAcceptedCourses); diff --git a/swad_enrolment.c b/swad_enrolment.c index 9f33e78d..9b12cb35 100644 --- a/swad_enrolment.c +++ b/swad_enrolment.c @@ -3188,8 +3188,9 @@ static void Enr_EffectivelyRemUsrFromCrs (struct Usr_Data *UsrDat, Gbl.Usrs.Me.UsrDat.Accepted = false; /* Fill the list with the courses I belong to */ - Gbl.Usrs.Me.MyCrss.Filled = false; - Enr_GetMyCourses (); + Gbl.Usrs.Me.Hierarchy[Hie_CRS].Filled = false; + // Enr_GetMyCourses (); + Hie_GetMyHierarchy (Hie_CRS); /* Set my roles */ Gbl.Usrs.Me.Role.FromSession = @@ -3224,7 +3225,7 @@ static void Enr_EffectivelyRemUsrFromCrs (struct Usr_Data *UsrDat, /*****************************************************************************/ /*************** Get all my courses and store them in a list *****************/ /*****************************************************************************/ - +/* void Enr_GetMyCourses (void) { MYSQL_RES *mysql_res; @@ -3233,78 +3234,58 @@ void Enr_GetMyCourses (void) unsigned NumCrs; long CrsCod; - /***** Trivial check 1: if my courses are already filled, there's nothing to do *****/ - if (Gbl.Usrs.Me.MyCrss.Filled) + ***** Trivial check 1: if my courses are already filled, there's nothing to do ***** + if (Gbl.Usrs.Me.Hierarchy[Hie_CRS].Filled) return; - /***** Trivial check 2: if user's code is not set, don't query database *****/ + ***** Trivial check 2: if user's code is not set, don't query database ***** if (Gbl.Usrs.Me.UsrDat.UsrCod <= 0) return; - /***** Remove temporary table with my courses *****/ + ***** Remove temporary table with my courses ***** Enr_DB_DropTmpTableMyCourses (); - /***** Create temporary table with my courses *****/ + ***** Create temporary table with my courses ***** Enr_DB_CreateTmpTableMyCourses (); - /***** Get my courses from database *****/ - Gbl.Usrs.Me.MyCrss.Num = 0; - Gbl.Usrs.Me.MyCrss.Crss = NULL; + ***** Get my courses from database ***** + Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num = 0; + Gbl.Usrs.Me.Hierarchy[Hie_CRS].Nodes = NULL; NumCrss = Enr_DB_GetMyCourses (&mysql_res); if (NumCrss) { - if ((Gbl.Usrs.Me.MyCrss.Crss = malloc (NumCrss * - sizeof (*Gbl.Usrs.Me.MyCrss.Crss))) == NULL) + if ((Gbl.Usrs.Me.Hierarchy[Hie_CRS].Nodes = malloc (NumCrss * + sizeof (*Gbl.Usrs.Me.Hierarchy[Hie_CRS].Nodes))) == NULL) Err_NotEnoughMemoryExit (); for (NumCrs = 0; NumCrs < NumCrss; NumCrs++) { - /* Get next course */ + * Get next course * row = mysql_fetch_row (mysql_res); - /* Get course code (row[0]) */ + * Get course code (row[0]) * if ((CrsCod = Str_ConvertStrCodToLongCod (row[0])) > 0) { - Gbl.Usrs.Me.MyCrss.Crss[Gbl.Usrs.Me.MyCrss.Num].HieCod = CrsCod; + Gbl.Usrs.Me.Hierarchy[Hie_CRS].Nodes[Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num].HieCod = CrsCod; - /* Get role (row[1]) and degree code (row[2]) */ - Gbl.Usrs.Me.MyCrss.Crss[Gbl.Usrs.Me.MyCrss.Num].MaxRole = Rol_ConvertUnsignedStrToRole (row[1]); - Gbl.Usrs.Me.MyCrss.Crss[Gbl.Usrs.Me.MyCrss.Num].PrtCod = Str_ConvertStrCodToLongCod (row[2]); + * Get role (row[1]) and degree code (row[2]) * + Gbl.Usrs.Me.Hierarchy[Hie_CRS].Nodes[Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num].MaxRole = Rol_ConvertUnsignedStrToRole (row[1]); + Gbl.Usrs.Me.Hierarchy[Hie_CRS].Nodes[Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num].PrtCod = Str_ConvertStrCodToLongCod (row[2]); - Gbl.Usrs.Me.MyCrss.Num++; + Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num++; } } } - /***** Free structure that stores the query result *****/ + ***** Free structure that stores the query result ***** DB_FreeMySQLResult (&mysql_res); - /***** Set boolean that indicates that my courses are already filled *****/ - Gbl.Usrs.Me.MyCrss.Filled = true; + ***** Set boolean that indicates that my courses are already filled ***** + Gbl.Usrs.Me.Hierarchy[Hie_CRS].Filled = true; } - -/*****************************************************************************/ -/************************ Free the list of my courses ************************/ -/*****************************************************************************/ - -void Enr_FreeMyCourses (void) - { - if (Gbl.Usrs.Me.MyCrss.Filled) - { - /***** Reset list *****/ - Gbl.Usrs.Me.MyCrss.Filled = false; - if (Gbl.Usrs.Me.MyCrss.Num && - Gbl.Usrs.Me.MyCrss.Crss) - free (Gbl.Usrs.Me.MyCrss.Crss); - Gbl.Usrs.Me.MyCrss.Num = 0; - - /***** Remove temporary table with my courses *****/ - Enr_DB_DropTmpTableMyCourses (); - } - } - +*/ /*****************************************************************************/ /*********************** Check if I belong to a course ***********************/ /*****************************************************************************/ @@ -3314,13 +3295,14 @@ bool Enr_CheckIfIBelongToCrs (long CrsCod) unsigned NumMyCrs; /***** Fill the list with the courses I belong to *****/ - Enr_GetMyCourses (); + // Enr_GetMyCourses (); + Hie_GetMyHierarchy (Hie_CRS); /***** Check if the course passed as parameter is any of my courses *****/ for (NumMyCrs = 0; - NumMyCrs < Gbl.Usrs.Me.MyCrss.Num; + NumMyCrs < Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num; NumMyCrs++) - if (Gbl.Usrs.Me.MyCrss.Crss[NumMyCrs].HieCod == CrsCod) + if (Gbl.Usrs.Me.Hierarchy[Hie_CRS].Nodes[NumMyCrs].HieCod == CrsCod) return true; return false; diff --git a/swad_enrolment.h b/swad_enrolment.h index 45b4d406..d1ad47a2 100644 --- a/swad_enrolment.h +++ b/swad_enrolment.h @@ -137,8 +137,7 @@ void Enr_CreateNewUsr2 (void); void Enr_ModifyUsr1 (void); void Enr_ModifyUsr2 (void); -void Enr_GetMyCourses (void); -void Enr_FreeMyCourses (void); +// void Enr_GetMyCourses (void); bool Enr_CheckIfIBelongToCrs (long CrsCod); void Enr_FlushCacheUsrBelongsToCurrentCrs (void); bool Enr_CheckIfUsrBelongsToCurrentCrs (const struct Usr_Data *UsrDat); diff --git a/swad_enrolment_database.c b/swad_enrolment_database.c index a99ec523..757270fa 100644 --- a/swad_enrolment_database.c +++ b/swad_enrolment_database.c @@ -101,7 +101,9 @@ void Enr_DB_CreateTmpTableMyCourses (void) /************************* Get my courses from database **********************/ /*****************************************************************************/ -unsigned Enr_DB_GetMyCourses (MYSQL_RES **mysql_res) +unsigned Enr_DB_GetMyCourses (MYSQL_RES **mysql_res, + __attribute__((unused)) long UsrCod, + __attribute__((unused)) long HieCod) { return (unsigned) DB_QuerySELECT (mysql_res,"can not get which courses you belong to", @@ -169,10 +171,11 @@ bool Enr_DB_CheckIfUsrBelongsToCrs (long UsrCod,long HieCod, bool Enr_DB_CheckIfUsrSharesAnyOfMyCrs (long UsrCod) { - /* Fill the list with the courses I belong to (if not already filled) */ - Enr_GetMyCourses (); + /***** Fill the list with the courses I belong to (if not already filled) *****/ + // Enr_GetMyCourses (); + Hie_GetMyHierarchy (Hie_CRS); - /* Check if user shares any course with me */ + /***** Check if user shares any course with me *****/ return DB_QueryEXISTS ("can not check if a user shares any course with you", "SELECT EXISTS" @@ -200,7 +203,8 @@ bool Enr_DB_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) */ - Enr_GetMyCourses (); + // Enr_GetMyCourses (); + Hie_GetMyHierarchy (Hie_CRS); /* Remove temporary table if exists */ DB_DropTmpTable ("usr_courses_tmp"); diff --git a/swad_enrolment_database.h b/swad_enrolment_database.h index 7feabd82..1b1cfab0 100644 --- a/swad_enrolment_database.h +++ b/swad_enrolment_database.h @@ -40,7 +40,9 @@ void Enr_DB_InsertUsrInCurrentCrs (long UsrCod,long CrsCod,Rol_Role_t NewRole, void Enr_DB_AcceptUsrInCrs (long UsrCod,long CrsCod); void Enr_DB_CreateTmpTableMyCourses (void); -unsigned Enr_DB_GetMyCourses (MYSQL_RES **mysql_res); +unsigned Enr_DB_GetMyCourses (MYSQL_RES **mysql_res, + __attribute__((unused)) long UsrCod, + __attribute__((unused)) long HieCod); void Enr_DB_DropTmpTableMyCourses (void); unsigned Enr_DB_GetMyCoursesNames (MYSQL_RES **mysql_res); diff --git a/swad_forum.c b/swad_forum.c index 98a15dc8..149adab0 100644 --- a/swad_forum.c +++ b/swad_forum.c @@ -1238,7 +1238,8 @@ void For_ShowForumList (struct For_Forums *Forums) Forums->Thread.ToMove = For_DB_GetThrInMyClipboard (); /***** Fill the list with the institutions I belong to *****/ - Ins_GetMyInstits (); + // Ins_GetMyInstits (); + Hie_GetMyHierarchy (Hie_INS); /***** Begin box *****/ Box_BoxBegin (NULL,Txt_Forums, @@ -1314,15 +1315,15 @@ void For_ShowForumList (struct For_Forums *Forums) /***** Links to forums of users from my institutions, the degrees in each institution and the courses in each degree *****/ for (NumMyIns = 0; - NumMyIns < Gbl.Usrs.Me.MyInss.Num; + NumMyIns < Gbl.Usrs.Me.Hierarchy[Hie_INS].Num; NumMyIns++) { - InsCod = Gbl.Usrs.Me.MyInss.Inss[NumMyIns].HieCod; + InsCod = Gbl.Usrs.Me.Hierarchy[Hie_INS].Nodes[NumMyIns].HieCod; /* Links to forums of this institution */ For_WriteLinksToInsForums (Forums, InsCod, - (NumMyIns == Gbl.Usrs.Me.MyInss.Num - 1), + (NumMyIns == Gbl.Usrs.Me.Hierarchy[Hie_INS].Num - 1), IsLastItemInLevel); /* Get my centers in this institution from database */ diff --git a/swad_global.c b/swad_global.c index 0d7f580c..60c24a0f 100644 --- a/swad_global.c +++ b/swad_global.c @@ -122,7 +122,7 @@ void Gbl_InitializeGlobals (void) Gbl.Usrs.Me.Role.FromSession = Gbl.Usrs.Me.Role.Logged = Gbl.Usrs.Me.Role.LoggedBeforeCloseSession = - Gbl.Usrs.Me.Role.Max = Rol_UNK; + Gbl.Usrs.Me.Role.Max = Rol_UNK; Gbl.Usrs.Me.Role.HasChanged = false; Gbl.Usrs.Me.IBelongToCurrentIns = false; Gbl.Usrs.Me.IBelongToCurrentCtr = false; @@ -131,11 +131,7 @@ void Gbl_InitializeGlobals (void) Gbl.Usrs.Me.MyPhotoExists = false; Gbl.Usrs.Me.NumAccWithoutPhoto = 0; Gbl.Usrs.Me.TimeLastAccToThisFileBrowser = LONG_MAX; // Initialized to a big value, so by default files are not shown as recent or new - Gbl.Usrs.Me.MyInss.Filled = false; - Gbl.Usrs.Me.MyCtrs.Filled = false; - Gbl.Usrs.Me.MyDegs.Filled = false; - Gbl.Usrs.Me.MyCrss.Filled = false; - Gbl.Usrs.Me.MyCrss.Num = 0; + Hie_ResetMyHierarchy (); Gbl.Usrs.Me.ConfirmEmailJustSent = false; // An email to confirm my email address has not just been sent Gbl.Usrs.Other.UsrDat.UsrCod = -1L; @@ -268,11 +264,9 @@ void Gbl_Cleanup (void) !Gbl.WebService.IsWebService && Act_GetBrowserTab (Gbl.Action.Act) == Act_BRW_1ST_TAB) Ses_DB_RemovePar (); - Enr_FreeMyCourses (); - Deg_FreeMyDegrees (); - Ctr_FreeMyCenters (); - Ins_FreeMyInstits (); - Cty_FreeMyCountrs (); + + Hie_FreeMyHierarchy (); + Usr_UsrDataDestructor (&Gbl.Usrs.Me.UsrDat); Usr_UsrDataDestructor (&Gbl.Usrs.Other.UsrDat); Rec_FreeListFields (); diff --git a/swad_global.h b/swad_global.h index 24c5a14e..4723f09f 100644 --- a/swad_global.h +++ b/swad_global.h @@ -135,34 +135,10 @@ struct Globals bool ConfirmEmailJustSent; // An email to confirm my email address has just been sent struct { - bool Filled; // My institutions are already filled? - unsigned Num; - struct Hie_MyNode *Ctys; - } MyCtys; - struct - { - bool Filled; // My institutions are already filled? - unsigned Num; - struct Hie_MyNode *Inss; - } MyInss; - struct - { - bool Filled; // My centers are already filled? - unsigned Num; - struct Hie_MyNode *Ctrs; - } MyCtrs; - struct - { - bool Filled; // My degrees are already filled? - unsigned Num; - struct Hie_MyNode *Degs; - } MyDegs; - struct - { - bool Filled; // My courses are already filled? - unsigned Num; - struct Hie_MyNode *Crss; - } MyCrss; + struct Hie_MyNode *Nodes; // List of courses/degrees/centers/institutions/countries + unsigned Num; // Number of courses/degrees/centers/institutions/countries + bool Filled; // List is already filled? + } Hierarchy[Hie_NUM_LEVELS]; // My hierarchy Set_ShowUsrsType_t ListType; // My preference about user's list type unsigned NumFollowers; // Number of users who follow me unsigned NumFollowing; // Number of users I follow diff --git a/swad_help.c b/swad_help.c index 237b3b31..416071a5 100644 --- a/swad_help.c +++ b/swad_help.c @@ -112,7 +112,7 @@ void Hlp_ShowHelpWhatWouldYouLikeToDo (void) /***** Alert message *****/ if (Gbl.Usrs.Me.Logged && - !Gbl.Usrs.Me.MyCrss.Num) + !Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num) Ale_ShowAlert (Ale_INFO,"%s
%s
%s", Txt_You_are_not_enroled_in_any_course[Gbl.Usrs.Me.UsrDat.Sex], Txt_You_can_search_for_courses_select_them_and_request_your_enrolment_in_them, @@ -157,7 +157,7 @@ void Hlp_ShowHelpWhatWouldYouLikeToDo (void) } } - if (Gbl.Usrs.Me.MyCrss.Num) // I am enroled in some courses + if (Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num) // I am enroled in some courses { if (Gbl.Hierarchy.Level == Hie_CRS && // Course selected Gbl.Usrs.Me.UsrDat.Roles.InCurrentCrs == Rol_TCH) // I am a teacher in current course diff --git a/swad_hierarchy.c b/swad_hierarchy.c index 84152646..82efbd82 100644 --- a/swad_hierarchy.c +++ b/swad_hierarchy.c @@ -905,7 +905,7 @@ Hie_Order_t Hie_GetParHieOrder (void) } /*****************************************************************************/ -/**************************** Free list of centers ***************************/ +/***************** Free list of courses/degrees/centers... *******************/ /*****************************************************************************/ void Hie_FreeList (Hie_Level_t Level) @@ -919,6 +919,128 @@ void Hie_FreeList (Hie_Level_t Level) } } +/*****************************************************************************/ +/***** Reset lists of my courses/degrees/centers/institutions/countries ******/ +/*****************************************************************************/ + +void Hie_ResetMyHierarchy (void) + { + Hie_Level_t Level; + + for (Level = Hie_CTY; + Level <= Hie_CRS; + Level++) + { + Gbl.Usrs.Me.Hierarchy[Level].Nodes = NULL; + Gbl.Usrs.Me.Hierarchy[Level].Num = 0; + Gbl.Usrs.Me.Hierarchy[Level].Filled = false; + } + } + +/*****************************************************************************/ +/****** Free lists of my courses/degrees/centers/institutions/countries ******/ +/*****************************************************************************/ + +void Hie_FreeMyHierarchy (void) + { + Hie_Level_t Level; + + /***** Remove temporary table with my courses *****/ + if (Gbl.Usrs.Me.Hierarchy[Hie_CRS].Filled) + Enr_DB_DropTmpTableMyCourses (); + + /***** Free allocated memory + for my courses/degrees/centers/institutions/countries *****/ + for (Level = Hie_CTY; + Level <= Hie_CRS; + Level++) + if (Gbl.Usrs.Me.Hierarchy[Level].Filled && + Gbl.Usrs.Me.Hierarchy[Level].Num && + Gbl.Usrs.Me.Hierarchy[Level].Nodes) + free (Gbl.Usrs.Me.Hierarchy[Level].Nodes); + + Hie_ResetMyHierarchy (); + } + +/*****************************************************************************/ +/********* Get all my courses/degrees/centers/institutions/countries *********/ +/********* and store them in a list *********/ +/*****************************************************************************/ + +void Hie_GetMyHierarchy (Hie_Level_t Level) + { + MYSQL_RES *mysql_res; + MYSQL_ROW row; + unsigned NumNode; + unsigned NumNodes; + long HieCod; + static unsigned (*GetNodesFromDB[Hie_NUM_LEVELS]) (MYSQL_RES **mysql_res, + long UsrCod,long HieCod) = + { + [Hie_CTY] = Cty_DB_GetCtysFromUsr, + [Hie_INS] = Ins_DB_GetInssFromUsr, + [Hie_CTR] = Ctr_DB_GetCtrsFromUsr, + [Hie_DEG] = Deg_DB_GetDegsFromUsr, + [Hie_CRS] = Enr_DB_GetMyCourses, + }; + + /***** Trivial check 1: if list of nodes is already filled, there's nothing to do *****/ + if (Gbl.Usrs.Me.Hierarchy[Level].Filled) + return; + + /***** Trivial check 2: if user's code is not set, don't query database *****/ + if (Gbl.Usrs.Me.UsrDat.UsrCod <= 0) + return; + + /***** If retrieving my list of courses, create a temporary table with them *****/ + if (Level == Hie_CRS) + { + Enr_DB_DropTmpTableMyCourses (); + Enr_DB_CreateTmpTableMyCourses (); + } + + /***** Set default values *****/ + Gbl.Usrs.Me.Hierarchy[Level].Num = 0; + Gbl.Usrs.Me.Hierarchy[Level].Nodes = NULL; + + /***** Get my courses/degrees/centers/institutions/countries from database *****/ + NumNodes = GetNodesFromDB[Level] (&mysql_res,Gbl.Usrs.Me.UsrDat.UsrCod,-1L); + if (NumNodes) + { + if ((Gbl.Usrs.Me.Hierarchy[Level].Nodes = malloc (NumNodes * + sizeof (*Gbl.Usrs.Me.Hierarchy[Level].Nodes))) == NULL) + Err_NotEnoughMemoryExit (); + for (NumNode = 0; + NumNode < NumNodes; + NumNode++) + { + /* Get next course/degree/center/institution/country */ + row = mysql_fetch_row (mysql_res); + + /* Get hierarchy code (row[0]) */ + if ((HieCod = Str_ConvertStrCodToLongCod (row[0])) > 0) + { + Gbl.Usrs.Me.Hierarchy[Level].Nodes[Gbl.Usrs.Me.Hierarchy[Level].Num].HieCod = HieCod; + + /* Get role or maximum role (row[1]) in this node */ + Gbl.Usrs.Me.Hierarchy[Level].Nodes[Gbl.Usrs.Me.Hierarchy[Level].Num].MaxRole = Rol_ConvertUnsignedStrToRole (row[1]); + + /* Get parent hierarchy code */ + if (Level == Hie_CRS) + Gbl.Usrs.Me.Hierarchy[Level].Nodes[Gbl.Usrs.Me.Hierarchy[Level].Num].PrtCod = Str_ConvertStrCodToLongCod (row[2]); + + Gbl.Usrs.Me.Hierarchy[Level].Num++; + } + } + } + + /***** Free structure that stores the query result *****/ + DB_FreeMySQLResult (&mysql_res); + + /***** Set boolean that indicates that my institutions are yet filled *****/ + Gbl.Usrs.Me.Hierarchy[Level].Filled = true; + } + /*****************************************************************************/ /*** Flush cache that stores if a user belongs to a node of the hierarchy ****/ /*****************************************************************************/ diff --git a/swad_hierarchy.h b/swad_hierarchy.h index 1bd656d9..9481f282 100644 --- a/swad_hierarchy.h +++ b/swad_hierarchy.h @@ -64,6 +64,10 @@ Hie_Order_t Hie_GetParHieOrder (void); void Hie_FreeList (Hie_Level_t Level); +void Hie_ResetMyHierarchy (void); +void Hie_FreeMyHierarchy (void); +void Hie_GetMyHierarchy (Hie_Level_t Level); + void Hie_FlushCacheUsrBelongsTo (Hie_Level_t Level); bool Hie_CheckIfUsrBelongsTo (Hie_Level_t Level,long UsrCod,long HieCod, bool CountOnlyAcceptedCourses); diff --git a/swad_institution.c b/swad_institution.c index db93c438..bf40c1bf 100644 --- a/swad_institution.c +++ b/swad_institution.c @@ -1686,7 +1686,7 @@ static void Ins_FormToGoToMap (struct Hie_Node *Ins) /*****************************************************************************/ /** Get all my institutions (those of my courses) and store them in a list ***/ /*****************************************************************************/ - +/* void Ins_GetMyInstits (void) { MYSQL_RES *mysql_res; @@ -1695,64 +1695,47 @@ void Ins_GetMyInstits (void) unsigned NumInss; long InsCod; - /***** If my institutions are yet filled, there's nothing to do *****/ - if (!Gbl.Usrs.Me.MyInss.Filled) + ***** If my institutions are yet filled, there's nothing to do ***** + if (!Gbl.Usrs.Me.Hierarchy[Hie_INS].Filled) { - Gbl.Usrs.Me.MyInss.Num = 0; - Gbl.Usrs.Me.MyInss.Inss = NULL; + Gbl.Usrs.Me.Hierarchy[Hie_INS].Num = 0; + Gbl.Usrs.Me.Hierarchy[Hie_INS].Nodes = NULL; - /***** Get my institutions from database *****/ + ***** Get my institutions from database ***** NumInss = Ins_DB_GetInssFromUsr (&mysql_res, Gbl.Usrs.Me.UsrDat.UsrCod,-1L); if (NumInss) { - if ((Gbl.Usrs.Me.MyInss.Inss = malloc (NumInss * - sizeof (*Gbl.Usrs.Me.MyInss.Inss))) == NULL) + if ((Gbl.Usrs.Me.Hierarchy[Hie_INS].Nodes = malloc (NumInss * + sizeof (*Gbl.Usrs.Me.Hierarchy[Hie_INS].Nodes))) == NULL) Err_NotEnoughMemoryExit (); for (NumIns = 0; NumIns < NumInss; NumIns++) { - /* Get next institution */ + * Get next institution * row = mysql_fetch_row (mysql_res); - /* Get institution code */ + * Get institution code * if ((InsCod = Str_ConvertStrCodToLongCod (row[0])) > 0) { - Gbl.Usrs.Me.MyInss.Inss[Gbl.Usrs.Me.MyInss.Num].HieCod = InsCod; - Gbl.Usrs.Me.MyInss.Inss[Gbl.Usrs.Me.MyInss.Num].MaxRole = Rol_ConvertUnsignedStrToRole (row[1]); + Gbl.Usrs.Me.Hierarchy[Hie_INS].Nodes[Gbl.Usrs.Me.Hierarchy[Hie_INS].Num].HieCod = InsCod; + Gbl.Usrs.Me.Hierarchy[Hie_INS].Nodes[Gbl.Usrs.Me.Hierarchy[Hie_INS].Num].MaxRole = Rol_ConvertUnsignedStrToRole (row[1]); - Gbl.Usrs.Me.MyInss.Num++; + Gbl.Usrs.Me.Hierarchy[Hie_INS].Num++; } } } - /***** Free structure that stores the query result *****/ + + ***** Free structure that stores the query result ***** DB_FreeMySQLResult (&mysql_res); - /***** Set boolean that indicates that my institutions are yet filled *****/ - Gbl.Usrs.Me.MyInss.Filled = true; + ***** Set boolean that indicates that my institutions are yet filled ***** + Gbl.Usrs.Me.Hierarchy[Hie_INS].Filled = true; } } - -/*****************************************************************************/ -/********************* Free the list of my institutions **********************/ -/*****************************************************************************/ - -void Ins_FreeMyInstits (void) - { - if (Gbl.Usrs.Me.MyInss.Filled) - { - /***** Reset list *****/ - Gbl.Usrs.Me.MyInss.Filled = false; - if (Gbl.Usrs.Me.MyInss.Num && - Gbl.Usrs.Me.MyInss.Inss) - free (Gbl.Usrs.Me.MyInss.Inss); - Gbl.Usrs.Me.MyInss.Inss = NULL; - Gbl.Usrs.Me.MyInss.Num = 0; - } - } - +*/ /*****************************************************************************/ /******************** Check if I belong to an institution ********************/ /*****************************************************************************/ @@ -1762,13 +1745,14 @@ bool Ins_CheckIfIBelongToIns (long InsCod) unsigned NumMyIns; /***** Fill the list with the institutions I belong to *****/ - Ins_GetMyInstits (); + // Ins_GetMyInstits (); + Hie_GetMyHierarchy (Hie_INS); /***** Check if the institution passed as parameter is any of my institutions *****/ for (NumMyIns = 0; - NumMyIns < Gbl.Usrs.Me.MyInss.Num; + NumMyIns < Gbl.Usrs.Me.Hierarchy[Hie_INS].Num; NumMyIns++) - if (Gbl.Usrs.Me.MyInss.Inss[NumMyIns].HieCod == InsCod) + if (Gbl.Usrs.Me.Hierarchy[Hie_INS].Nodes[NumMyIns].HieCod == InsCod) return true; return false; } diff --git a/swad_institution.h b/swad_institution.h index c330e627..c5f1e171 100644 --- a/swad_institution.h +++ b/swad_institution.h @@ -70,8 +70,7 @@ unsigned Ins_GetCachedNumInssWithUsrs (Rol_Role_t Role); void Ins_ListInssFound (MYSQL_RES **mysql_res,unsigned NumInss); -void Ins_GetMyInstits (void); -void Ins_FreeMyInstits (void); +// void Ins_GetMyInstits (void); bool Ins_CheckIfIBelongToIns (long InsCod); void Ins_PutParInsCod (void *InsCod); diff --git a/swad_institution_database.c b/swad_institution_database.c index 5c9641fa..ef2ec7fe 100644 --- a/swad_institution_database.c +++ b/swad_institution_database.c @@ -760,9 +760,9 @@ unsigned Ins_DB_GetNumInnsWithUsrs (Rol_Role_t Role, /*****************************************************************************/ // Returns the number of rows of the result -unsigned Ins_DB_GetInssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long CtyCod) +unsigned Ins_DB_GetInssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long HieCod) { - if (CtyCod > 0) + if (HieCod > 0) return (unsigned) DB_QuerySELECT (mysql_res,"can not get the institutions a user belongs to", "SELECT ins_instits.InsCod," // row[0] @@ -781,7 +781,7 @@ unsigned Ins_DB_GetInssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long CtyCod) " GROUP BY ins_instits.InsCod" " ORDER BY ins_instits.ShortName", UsrCod, - CtyCod); + HieCod); else return (unsigned) DB_QuerySELECT (mysql_res,"can not get the institutions a user belongs to", diff --git a/swad_institution_database.h b/swad_institution_database.h index b1a28a25..3692e6e5 100644 --- a/swad_institution_database.h +++ b/swad_institution_database.h @@ -77,7 +77,7 @@ unsigned Ins_DB_GetNumInssWithCrss (Hie_Level_t Level,long HieCod); unsigned Ins_DB_GetNumInnsWithUsrs (Rol_Role_t Role, Hie_Level_t Level,long HieCod); -unsigned Ins_DB_GetInssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long CtyCod); +unsigned Ins_DB_GetInssFromUsr (MYSQL_RES **mysql_res,long UsrCod,long HieCod); bool Ins_DB_CheckIfUsrBelongsToIns (long UsrCod,long HieCod, bool CountOnlyAcceptedCourses); unsigned Ins_DB_GetNumUsrsWhoClaimToBelongToIns (long InsCod); diff --git a/swad_role.c b/swad_role.c index 637a60d2..a235ea49 100644 --- a/swad_role.c +++ b/swad_role.c @@ -257,14 +257,15 @@ Rol_Role_t Rol_GetMyMaxRoleInIns (long InsCod) if (InsCod > 0) { /***** Fill the list with the institutions I belong to (if not already filled) *****/ - Ins_GetMyInstits (); + // Ins_GetMyInstits (); + Hie_GetMyHierarchy (Hie_INS); /***** Check if the institution passed as parameter is any of my institutions *****/ for (NumMyIns = 0; - NumMyIns < Gbl.Usrs.Me.MyInss.Num; + NumMyIns < Gbl.Usrs.Me.Hierarchy[Hie_INS].Num; NumMyIns++) - if (Gbl.Usrs.Me.MyInss.Inss[NumMyIns].HieCod == InsCod) - return Gbl.Usrs.Me.MyInss.Inss[NumMyIns].MaxRole; + if (Gbl.Usrs.Me.Hierarchy[Hie_INS].Nodes[NumMyIns].HieCod == InsCod) + return Gbl.Usrs.Me.Hierarchy[Hie_INS].Nodes[NumMyIns].MaxRole; return Rol_GST; } return Rol_UNK; // No degree @@ -281,14 +282,15 @@ Rol_Role_t Rol_GetMyMaxRoleInCtr (long CtrCod) if (CtrCod > 0) { /***** Fill the list with the centers I belong to (if not already filled) *****/ - Ctr_GetMyCenters (); + // Ctr_GetMyCenters (); + Hie_GetMyHierarchy (Hie_CTR); /***** Check if the center passed as parameter is any of my centers *****/ for (NumMyCtr = 0; - NumMyCtr < Gbl.Usrs.Me.MyCtrs.Num; + NumMyCtr < Gbl.Usrs.Me.Hierarchy[Hie_CTR].Num; NumMyCtr++) - if (Gbl.Usrs.Me.MyCtrs.Ctrs[NumMyCtr].HieCod == CtrCod) - return Gbl.Usrs.Me.MyCtrs.Ctrs[NumMyCtr].MaxRole; + if (Gbl.Usrs.Me.Hierarchy[Hie_CTR].Nodes[NumMyCtr].HieCod == CtrCod) + return Gbl.Usrs.Me.Hierarchy[Hie_CTR].Nodes[NumMyCtr].MaxRole; return Rol_GST; } return Rol_UNK; // No center @@ -307,14 +309,15 @@ Rol_Role_t Rol_GetMyMaxRoleInDeg (long DegCod) return Rol_UNK; /***** Fill the list with the degrees I belong to (if not already filled) *****/ - Deg_GetMyDegrees (); + // Deg_GetMyDegrees (); + Hie_GetMyHierarchy (Hie_DEG); /***** Check if the degree passed as parameter is any of my degrees *****/ for (NumMyDeg = 0; - NumMyDeg < Gbl.Usrs.Me.MyDegs.Num; + NumMyDeg < Gbl.Usrs.Me.Hierarchy[Hie_DEG].Num; NumMyDeg++) - if (Gbl.Usrs.Me.MyDegs.Degs[NumMyDeg].HieCod == DegCod) - return Gbl.Usrs.Me.MyDegs.Degs[NumMyDeg].MaxRole; + if (Gbl.Usrs.Me.Hierarchy[Hie_DEG].Nodes[NumMyDeg].HieCod == DegCod) + return Gbl.Usrs.Me.Hierarchy[Hie_DEG].Nodes[NumMyDeg].MaxRole; return Rol_GST; } @@ -343,15 +346,16 @@ 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) */ - Enr_GetMyCourses (); + // Enr_GetMyCourses (); + Hie_GetMyHierarchy (Hie_CRS); /* Check if the current course is any of my courses */ for (NumMyCrs = 0, Role = Rol_UNK; - NumMyCrs < Gbl.Usrs.Me.MyCrss.Num; + NumMyCrs < Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num; NumMyCrs++) - if (Gbl.Usrs.Me.MyCrss.Crss[NumMyCrs].HieCod == CrsCod) + if (Gbl.Usrs.Me.Hierarchy[Hie_CRS].Nodes[NumMyCrs].HieCod == CrsCod) { - Role = Gbl.Usrs.Me.MyCrss.Crss[NumMyCrs].MaxRole; + Role = Gbl.Usrs.Me.Hierarchy[Hie_CRS].Nodes[NumMyCrs].MaxRole; break; } diff --git a/swad_session.c b/swad_session.c index a0647eff..8dc08fe5 100644 --- a/swad_session.c +++ b/swad_session.c @@ -128,8 +128,8 @@ void Ses_CloseSession (void) Gbl.Usrs.Me.IBelongToCurrentDeg = false; Gbl.Usrs.Me.IBelongToCurrentCrs = false; Gbl.Usrs.Me.Role.Logged = Rol_UNK; // Don't uncomment this line. Don't change the role to unknown. Keep user's role in order to log the access - Gbl.Usrs.Me.MyCrss.Filled = false; - Gbl.Usrs.Me.MyCrss.Num = 0; + Gbl.Usrs.Me.Hierarchy[Hie_CRS].Filled = false; + Gbl.Usrs.Me.Hierarchy[Hie_CRS].Num = 0; /***** Update number of open sessions in order to show them properly *****/ Ses_GetNumSessions ();