diff --git a/swad_changelog.h b/swad_changelog.h index e68ced2e..54fb7916 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.16 (2020-01-06)" +#define Log_PLATFORM_VERSION "SWAD 19.112.17 (2020-01-07)" #define CSS_FILE "swad19.112.css" #define JS_FILE "swad19.91.1.js" /* @@ -501,9 +501,9 @@ ps2pdf source.ps destination.pdf // TODO: No se puede entrar con DNI '1' suponiendo que no tenga password ¿por qué? // TODO: Mapas más estrechos en móvil // TODO: Quitar todos los EXTRA_DATA. -Optimize Ins_GetNumInssInCty - Version 19.112.16:Jan 06, 2020 Optimization in number of centres in an institution. (? lines) + Version 19.112.17:Jan 07, 2020 Optimization in number of institutions in a country. (278600 lines) + Version 19.112.16:Jan 07, 2020 Optimization in number of centres in an institution. (278576 lines) Version 19.112.15:Jan 06, 2020 Optimization in number of degrees in an institution. (278567 lines) Version 19.112.14:Jan 06, 2020 Optimization in number of courses in an institution. (278555 lines) Version 19.112.13:Jan 06, 2020 Optimization in number of departments in an institution. (278535 lines) diff --git a/swad_country.c b/swad_country.c index 7762ff80..87367f97 100644 --- a/swad_country.c +++ b/swad_country.c @@ -421,7 +421,7 @@ static void Cty_ListOneCountryForSeeing (struct Country *Cty,unsigned NumCty) HTM_TD_End (); HTM_TD_Begin ("class=\"DAT RM %s\"",BgColor); - HTM_Unsigned (Cty->Inss.Num); + HTM_Unsigned (Ins_GetNumInssInCty (Cty->CtyCod)); HTM_TD_End (); HTM_TD_Begin ("class=\"DAT RM %s\"",BgColor); @@ -880,9 +880,6 @@ void Cty_GetListCountries (Cty_GetExtraData_t GetExtraData) &Cty->NumUsrsWhoClaimToBelongToCty) != 1) Cty->NumUsrsWhoClaimToBelongToCty = 0; - /* Get number of institutions in this country */ - Cty->Inss.Num = Ins_GetNumInssInCty (Cty->CtyCod); - /* Get number of centres in this country */ Cty->NumCtrs = Ctr_GetNumCtrsInCty (Cty->CtyCod); @@ -1140,9 +1137,6 @@ bool Cty_GetDataOfCountryByCod (struct Country *Cty,Cty_GetExtraData_t GetExtraD /* Get number of user in courses of this institution */ Cty->NumUsrs = Usr_GetNumUsrsInCrssOfCty (Rol_UNK,Cty->CtyCod); // Here Rol_UNK means "all users" - /* Get number of institutions in this country */ - Cty->Inss.Num = Ins_GetNumInssInCty (Cty->CtyCod); - break; } } @@ -1234,6 +1228,7 @@ static void Cty_ListCountriesForEdition (void) extern const char *Txt_STR_LANG_NAME[1 + Lan_NUM_LANGUAGES]; unsigned NumCty; struct Country *Cty; + unsigned NumInssInCty; Lan_Language_t Lan; /***** Write heading *****/ @@ -1246,14 +1241,16 @@ static void Cty_ListCountriesForEdition (void) NumCty++) { Cty = &Gbl.Hierarchy.Sys.Ctys.Lst[NumCty]; + NumInssInCty = Ins_GetNumInssInCty (Cty->CtyCod); HTM_TR_Begin (NULL); /* Put icon to remove country */ HTM_TD_Begin ("rowspan=\"%u\" class=\"BT\"",1 + Lan_NUM_LANGUAGES); - if (Cty->Inss.Num || + if (NumInssInCty || // Country has institutions Cty->NumUsrsWhoClaimToBelongToCty || - Cty->NumUsrs) // Country has institutions or users ==> deletion forbidden + Cty->NumUsrs) // Country has users + // Deletion forbidden Ico_PutIconRemovalNotAllowed (); else { @@ -1283,7 +1280,7 @@ static void Cty_ListCountriesForEdition (void) /* Number of institutions */ HTM_TD_Begin ("rowspan=\"%u\" class=\"DAT RT\"",1 + Lan_NUM_LANGUAGES); - HTM_Unsigned (Cty->Inss.Num); + HTM_Unsigned (NumInssInCty); HTM_TD_End (); HTM_TR_End (); @@ -1386,9 +1383,11 @@ void Cty_RemoveCountry (void) Cty_GetDataOfCountryByCod (Cty_EditingCty,Cty_GET_EXTRA_DATA); /***** Check if this country has users *****/ - if (Cty_EditingCty->Inss.Num || - Cty_EditingCty->NumUsrsWhoClaimToBelongToCty || - Cty_EditingCty->NumUsrs) // Country has institutions or users ==> don't remove + if (Ins_GetNumInssInCty (Cty_EditingCty->CtyCod)) // Country has institutions ==> don't remove + Ale_CreateAlert (Ale_WARNING,NULL, + Txt_You_can_not_remove_a_country_with_institutions_or_users); + else if (Cty_EditingCty->NumUsrsWhoClaimToBelongToCty || + Cty_EditingCty->NumUsrs) // Country has users ==> don't remove Ale_CreateAlert (Ale_WARNING,NULL, Txt_You_can_not_remove_a_country_with_institutions_or_users); else // Country has no users ==> remove it @@ -1403,6 +1402,7 @@ void Cty_RemoveCountry (void) /***** Flush cache *****/ Cty_FlushCacheCountryName (); + Ins_FlushCacheNumInssInCty (); /***** Write message to show the change made *****/ Ale_CreateAlert (Ale_SUCCESS,NULL, diff --git a/swad_global.c b/swad_global.c index 21ba4e56..50ce0d0b 100644 --- a/swad_global.c +++ b/swad_global.c @@ -406,6 +406,7 @@ void Gbl_InitializeGlobals (void) Cty_FlushCacheCountryName (); Ins_FlushCacheShortNameOfInstitution (); Ins_FlushCacheFullNameAndCtyOfInstitution (); + Ins_FlushCacheNumInssInCty (); Dpt_FlushCacheNumDptsInIns (); Ctr_FlushCacheNumCtrsInIns (); Deg_FlushCacheNumDegsInIns (); diff --git a/swad_global.h b/swad_global.h index 9edcac34..4cf40fac 100644 --- a/swad_global.h +++ b/swad_global.h @@ -777,6 +777,12 @@ struct Globals long InsCod; unsigned NumDpts; } NumDptsInIns; + struct + { + bool Valid; + long CtyCod; + unsigned NumInss; + } NumInssInCty; struct { long InsCod; diff --git a/swad_institution.c b/swad_institution.c index de087deb..8345db61 100644 --- a/swad_institution.c +++ b/swad_institution.c @@ -321,7 +321,7 @@ static void Ins_ListInstitutions (void) { Frm_StartForm (ActEdiIns); Btn_PutConfirmButton (Gbl.Hierarchy.Cty.Inss.Num ? Txt_Create_another_institution : - Txt_Create_institution); + Txt_Create_institution); Frm_EndForm (); } @@ -696,7 +696,10 @@ void Ins_GetListInstitutions (long CtyCod) } } else + { Gbl.Hierarchy.Cty.Inss.Num = 0; + Gbl.Hierarchy.Cty.Inss.Lst = NULL; + } /***** Free structure that stores the query result *****/ DB_FreeMySQLResult (&mysql_res); @@ -925,8 +928,8 @@ void Ins_FreeListInstitutions (void) { /***** Free memory used by the list of institutions *****/ free (Gbl.Hierarchy.Cty.Inss.Lst); - Gbl.Hierarchy.Cty.Inss.Lst = NULL; Gbl.Hierarchy.Cty.Inss.Num = 0; + Gbl.Hierarchy.Cty.Inss.Lst = NULL; } } @@ -1860,15 +1863,28 @@ unsigned Ins_GetNumInssTotal (void) /**************** Get number of institutions in a country ********************/ /*****************************************************************************/ +void Ins_FlushCacheNumInssInCty (void) + { + Gbl.Cache.NumInssInCty.Valid = false; + } + unsigned Ins_GetNumInssInCty (long CtyCod) { - /***** Get number of degrees of a place from database *****/ - return + /***** 1. Fast check: If cached... *****/ + if (Gbl.Cache.NumInssInCty.Valid && + CtyCod == Gbl.Cache.NumInssInCty.CtyCod) + return Gbl.Cache.NumInssInCty.NumInss; + + /***** 2. Slow: number of institutions in a country from database *****/ + Gbl.Cache.NumInssInCty.CtyCod = CtyCod; + Gbl.Cache.NumInssInCty.NumInss = (unsigned) DB_QueryCOUNT ("can not get the number of institutions" " in a country", "SELECT COUNT(*) FROM institutions" " WHERE CtyCod=%ld", CtyCod); + Gbl.Cache.NumInssInCty.Valid = true; + return Gbl.Cache.NumInssInCty.NumInss; } /*****************************************************************************/ diff --git a/swad_institution.h b/swad_institution.h index 11cb3b03..39ecee09 100644 --- a/swad_institution.h +++ b/swad_institution.h @@ -129,6 +129,7 @@ void Ins_RecFormReqIns (void); void Ins_RecFormNewIns (void); unsigned Ins_GetNumInssTotal (void); +void Ins_FlushCacheNumInssInCty (void); unsigned Ins_GetNumInssInCty (long CtyCod); unsigned Ins_GetNumInssWithCtrs (const char *SubQuery); unsigned Ins_GetNumInssWithDegs (const char *SubQuery);