diff --git a/swad_changelog.h b/swad_changelog.h index 91864f757..6b4068643 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -492,7 +492,7 @@ enscript -2 --landscape --color --file-align=2 --highlight --line-numbers -o - * En OpenSWAD: ps2pdf source.ps destination.pdf */ -#define Log_PLATFORM_VERSION "SWAD 19.112.11 (2020-01-06)" +#define Log_PLATFORM_VERSION "SWAD 19.112.12 (2020-01-06)" #define CSS_FILE "swad19.112.css" #define JS_FILE "swad19.91.1.js" /* @@ -502,6 +502,7 @@ ps2pdf source.ps destination.pdf // TODO: Mapas más estrechos en móvil // TODO: Quitar todos los EXTRA_DATA. + Version 19.112.12:Jan 06, 2020 Optimization in number of users who claim to belong to an institution. (278519 lines) Version 19.112.11:Jan 06, 2020 Optimization in number of users in courses of an institution. (278486 lines) Version 19.112.10:Jan 06, 2020 Optimization in number of users in courses of a degree. (278468 lines) Version 19.112.9: Jan 05, 2020 Optimization in number of users in a course. (278419 lines) diff --git a/swad_global.c b/swad_global.c index 07aac42f2..2fa3df8fe 100644 --- a/swad_global.c +++ b/swad_global.c @@ -414,6 +414,7 @@ void Gbl_InitializeGlobals (void) Deg_FlushCacheNumDegsInCtr (); Crs_FlushCacheNumCrssInCtr (); Crs_FlushCacheNumCrssInDeg (); + Usr_FlushCacheNumUsrsWhoClaimToBelongToIns (); Usr_FlushCacheNumUsrsWhoClaimToBelongToCtr (); Usr_FlushCacheNumUsrsInCrssOfIns (); Usr_FlushCacheNumUsrsInCrssOfCtr (); diff --git a/swad_global.h b/swad_global.h index 7d0854e4f..0620dd3de 100644 --- a/swad_global.h +++ b/swad_global.h @@ -787,6 +787,11 @@ struct Globals long DegCod; unsigned NumCrss; } NumCrssInDeg; + struct + { + long InsCod; + unsigned NumUsrs; + } NumUsrsWhoClaimToBelongToIns; struct { long CtrCod; diff --git a/swad_institution.c b/swad_institution.c index 571462a91..ca2d5de9c 100644 --- a/swad_institution.c +++ b/swad_institution.c @@ -402,7 +402,7 @@ static void Ins_ListOneInstitutionForSeeing (struct Instit *Ins,unsigned NumIns) /***** Stats *****/ /* Number of users who claim to belong to this institution */ HTM_TD_Begin ("class=\"%s RM %s\"",TxtClassNormal,BgColor); - HTM_Unsigned (Ins->NumUsrsWhoClaimToBelongToIns); + HTM_Unsigned (Usr_GetNumUsrsWhoClaimToBelongToIns (Ins->InsCod)); HTM_TD_End (); /* Number of centres in this institution */ @@ -593,11 +593,6 @@ static void Ins_PutIconToViewInstitutions (void) void Ins_GetListInstitutions (long CtyCod,Ins_GetExtraData_t GetExtraData) { - static const char *OrderBySubQuery[Ins_NUM_ORDERS] = - { - [Ins_ORDER_BY_INSTITUTION] = "FullName", - [Ins_ORDER_BY_NUM_USRS ] = "NumUsrs DESC,FullName", - }; MYSQL_RES *mysql_res; MYSQL_ROW row; unsigned long NumRows = 0; // Initialized to avoid warning @@ -605,35 +600,51 @@ void Ins_GetListInstitutions (long CtyCod,Ins_GetExtraData_t GetExtraData) struct Instit *Ins; /***** Get institutions from database *****/ - switch (GetExtraData) + switch (Gbl.Hierarchy.Cty.Inss.SelectedOrder) { - case Ins_GET_BASIC_DATA: + case Ins_ORDER_BY_INSTITUTION: NumRows = DB_QuerySELECT (&mysql_res,"can not get institutions", - "SELECT InsCod,CtyCod,Status,RequesterUsrCod,ShortName,FullName,WWW" + "SELECT InsCod," // row[0] + "CtyCod," // row[1] + "Status," // row[2] + "RequesterUsrCod," // row[3] + "ShortName," // row[4] + "FullName," // row[5] + "WWW" // row[6] " FROM institutions" " WHERE CtyCod=%ld" " ORDER BY FullName", CtyCod); break; - case Ins_GET_EXTRA_DATA: + case Ins_ORDER_BY_NUM_USRS: NumRows = DB_QuerySELECT (&mysql_res,"can not get institutions", - "(SELECT institutions.InsCod,institutions.CtyCod," - "institutions.Status,institutions.RequesterUsrCod," - "institutions.ShortName,institutions.FullName," - "institutions.WWW,COUNT(*) AS NumUsrs" + "(SELECT institutions.InsCod," // row[0] + "institutions.CtyCod," // row[1] + "institutions.Status," // row[2] + "institutions.RequesterUsrCod," // row[3] + "institutions.ShortName," // row[4] + "institutions.FullName," // row[5] + "institutions.WWW," // row[6] + "COUNT(*) AS NumUsrs" // row[7] " FROM institutions,usr_data" " WHERE institutions.CtyCod=%ld" " AND institutions.InsCod=usr_data.InsCod" " GROUP BY institutions.InsCod)" " UNION " - "(SELECT InsCod,CtyCod,Status,RequesterUsrCod,ShortName,FullName,WWW,0 AS NumUsrs" + "(SELECT InsCod," // row[0] + "CtyCod," // row[1] + "Status," // row[2] + "RequesterUsrCod," // row[3] + "ShortName," // row[4] + "FullName," // row[5] + "WWW," // row[6] + "0 AS NumUsrs" // row[7] " FROM institutions" " WHERE CtyCod=%ld" " AND InsCod NOT IN" " (SELECT DISTINCT InsCod FROM usr_data))" - " ORDER BY %s", - CtyCod,CtyCod, - OrderBySubQuery[Gbl.Hierarchy.Cty.Inss.SelectedOrder]); + " ORDER BY NumUsrs DESC,FullName", + CtyCod,CtyCod); break; } @@ -643,7 +654,8 @@ void Ins_GetListInstitutions (long CtyCod,Ins_GetExtraData_t GetExtraData) Gbl.Hierarchy.Cty.Inss.Num = (unsigned) NumRows; /***** Create list with institutions *****/ - if ((Gbl.Hierarchy.Cty.Inss.Lst = (struct Instit *) calloc (NumRows,sizeof (struct Instit))) == NULL) + if ((Gbl.Hierarchy.Cty.Inss.Lst = (struct Instit *) + calloc (NumRows,sizeof (struct Instit))) == NULL) Lay_NotEnoughMemoryExit (); /***** Get the institutions *****/ @@ -686,14 +698,9 @@ void Ins_GetListInstitutions (long CtyCod,Ins_GetExtraData_t GetExtraData) switch (GetExtraData) { case Ins_GET_BASIC_DATA: - Ins->NumUsrsWhoClaimToBelongToIns = 0; Ins->Ctrs.Num = Ins->NumDegs = Ins->NumCrss = Ins->NumDpts = 0; break; case Ins_GET_EXTRA_DATA: - /* Get number of users who claim to belong to this institution (row[7]) */ - if (sscanf (row[7],"%u",&Ins->NumUsrsWhoClaimToBelongToIns) != 1) - Ins->NumUsrsWhoClaimToBelongToIns = 0; - /* Get number of centres in this institution */ Ins->Ctrs.Num = Ctr_GetNumCtrsInIns (Ins->InsCod); @@ -752,7 +759,6 @@ bool Ins_GetDataOfInstitutionByCod (struct Instit *Ins, Ins->ShrtName[0] = Ins->FullName[0] = Ins->WWW[0] = '\0'; - Ins->NumUsrsWhoClaimToBelongToIns = 0; Ins->Ctrs.Num = Ins->NumDegs = Ins->NumCrss = Ins->NumDpts = 0; /***** Check if institution code is correct *****/ @@ -1034,6 +1040,7 @@ static void Ins_ListInstitutionsForEdition (void) struct UsrData UsrDat; bool ICanEdit; unsigned NumUsrsInCrssOfIns; + unsigned NumUsrsWhoClaimToBelongToIns; Ins_StatusTxt_t StatusTxt; unsigned StatusUnsigned; @@ -1053,6 +1060,7 @@ static void Ins_ListInstitutionsForEdition (void) ICanEdit = Ins_CheckIfICanEdit (Ins); NumUsrsInCrssOfIns = Usr_GetNumUsrsInCrssOfIns (Rol_UNK,Ins->InsCod); // Here Rol_UNK means "all users" + NumUsrsWhoClaimToBelongToIns = Usr_GetNumUsrsWhoClaimToBelongToIns (Ins->InsCod); HTM_TR_Begin (NULL); @@ -1061,7 +1069,7 @@ static void Ins_ListInstitutionsForEdition (void) if (!ICanEdit || Ins->Ctrs.Num || // Institution has centres NumUsrsInCrssOfIns || // Institution has users - Ins->NumUsrsWhoClaimToBelongToIns) + NumUsrsWhoClaimToBelongToIns) // Institution has users // Institution has centres or users ==> deletion forbidden Ico_PutIconRemovalNotAllowed (); else @@ -1137,7 +1145,7 @@ static void Ins_ListInstitutionsForEdition (void) /* Number of users who claim to belong to this institution */ HTM_TD_Begin ("class=\"DAT RM\""); - HTM_Unsigned (Ins->NumUsrsWhoClaimToBelongToIns); + HTM_Unsigned (NumUsrsWhoClaimToBelongToIns); HTM_TD_End (); /* Number of centres */ @@ -1312,15 +1320,18 @@ void Ins_RemoveInstitution (void) /***** Check if this institution has users *****/ if (!Ins_CheckIfICanEdit (Ins_EditingIns)) Lay_NoPermissionExit (); - else if (Ins_EditingIns->Ctrs.Num || - Ins_EditingIns->NumUsrsWhoClaimToBelongToIns) - // Institution has centres or users ==> don't remove + else if (Ins_EditingIns->Ctrs.Num) + // Institution has centres ==> don't remove Ale_CreateAlert (Ale_WARNING,NULL, Txt_To_remove_an_institution_you_must_first_remove_all_centres_and_users_in_the_institution); else if (Usr_GetNumUsrsInCrssOfIns (Rol_UNK,Ins_EditingIns->InsCod)) // Here Rol_UNK means "all users" // Institution has users ==> don't remove Ale_CreateAlert (Ale_WARNING,NULL, Txt_To_remove_an_institution_you_must_first_remove_all_centres_and_users_in_the_institution); + else if (Usr_GetNumUsrsWhoClaimToBelongToIns (Ins_EditingIns->InsCod)) + // Institution has users ==> don't remove + Ale_CreateAlert (Ale_WARNING,NULL, + Txt_To_remove_an_institution_you_must_first_remove_all_centres_and_users_in_the_institution); else // Institution has no users ==> remove it { /***** Remove all the threads and posts in forums of the institution *****/ @@ -1348,6 +1359,7 @@ void Ins_RemoveInstitution (void) /***** Flush caches *****/ Ins_FlushCacheShortNameOfInstitution (); Ins_FlushCacheFullNameAndCtyOfInstitution (); + Usr_FlushCacheNumUsrsWhoClaimToBelongToIns (); Usr_FlushCacheNumUsrsInCrssOfIns (); /***** Write message to show the change made *****/ @@ -2029,7 +2041,6 @@ static void Ins_EditingInstitutionConstructor (void) Ins_EditingIns->Ctrs.SelectedOrder = Ctr_ORDER_DEFAULT; Ins_EditingIns->NumDpts = 0; Ins_EditingIns->NumDegs = 0; - Ins_EditingIns->NumUsrsWhoClaimToBelongToIns = 0; } static void Ins_EditingInstitutionDestructor (void) diff --git a/swad_institution.h b/swad_institution.h index 0dbedb2fe..7eaccc157 100644 --- a/swad_institution.h +++ b/swad_institution.h @@ -74,7 +74,6 @@ struct Instit unsigned NumDegs; unsigned NumCrss; unsigned NumDpts; - unsigned NumUsrsWhoClaimToBelongToIns; }; #define Ins_NUM_ORDERS 2 diff --git a/swad_user.c b/swad_user.c index 388aba97c..1b49d5c74 100644 --- a/swad_user.c +++ b/swad_user.c @@ -4562,15 +4562,32 @@ unsigned Usr_GetNumUsrsWhoClaimToBelongToCty (long CtyCod) /******** Get number of users who claim to belong to an institution **********/ /*****************************************************************************/ +void Usr_FlushCacheNumUsrsWhoClaimToBelongToIns (void) + { + Gbl.Cache.NumUsrsWhoClaimToBelongToIns.InsCod = -1L; + Gbl.Cache.NumUsrsWhoClaimToBelongToIns.NumUsrs = 0; + } + unsigned Usr_GetNumUsrsWhoClaimToBelongToIns (long InsCod) { - /***** Get the number of users in an institution from database *****/ - return + /***** 1. Fast check: Trivial case *****/ + if (InsCod <= 0) + return 0; + + /***** 2. Fast check: If cached... *****/ + if (InsCod == Gbl.Cache.NumUsrsWhoClaimToBelongToCtr.CtrCod) + return Gbl.Cache.NumUsrsWhoClaimToBelongToCtr.NumUsrs; + + /***** 3. Slow: number of users who claim to belong to an institution + from database *****/ + Gbl.Cache.NumUsrsWhoClaimToBelongToIns.InsCod = InsCod; + Gbl.Cache.NumUsrsWhoClaimToBelongToIns.NumUsrs = (unsigned) DB_QueryCOUNT ("can not get the number of users" " in an institution", "SELECT COUNT(UsrCod) FROM usr_data" " WHERE InsCod=%ld", InsCod); + return Gbl.Cache.NumUsrsWhoClaimToBelongToIns.NumUsrs; } /*****************************************************************************/ diff --git a/swad_user.h b/swad_user.h index c40780aea..fc9b163a2 100644 --- a/swad_user.h +++ b/swad_user.h @@ -422,6 +422,7 @@ long Usr_GetRamdomStdFromGrp (long GrpCod); unsigned Usr_GetNumTchsCurrentInsInDepartment (long DptCod); unsigned Usr_GetNumUsrsWhoClaimToBelongToCty (long CtyCod); +void Usr_FlushCacheNumUsrsWhoClaimToBelongToIns (void); unsigned Usr_GetNumUsrsWhoClaimToBelongToIns (long InsCod); void Usr_FlushCacheNumUsrsWhoClaimToBelongToCtr (void); unsigned Usr_GetNumUsrsWhoClaimToBelongToCtr (long CtrCod);