diff --git a/swad_changelog.h b/swad_changelog.h index 54fb7916..c76561f0 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.17 (2020-01-07)" +#define Log_PLATFORM_VERSION "SWAD 19.112.18 (2020-01-07)" #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.18:Jan 07, 2020 Optimization in number of institutions in a country. (278668 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) diff --git a/swad_country.c b/swad_country.c index 87367f97..01d732d7 100644 --- a/swad_country.c +++ b/swad_country.c @@ -69,7 +69,6 @@ static void Cty_ListOneCountryForSeeing (struct Country *Cty,unsigned NumCty); static void Cty_PutIconsListingCountries (void); static void Cty_PutIconToEditCountries (void); -static unsigned Cty_GetNumUsrsWhoClaimToBelongToCty (long CtyCod); static void Cty_GetParamCtyOrder (void); static void Cty_EditCountriesInternal (void); @@ -258,7 +257,7 @@ void Cty_ListCountries2 (void) HTM_TD_End (); HTM_TD_Begin ("class=\"DAT RM\""); - HTM_Unsigned (Cty_GetNumUsrsWhoClaimToBelongToCty (0)); + HTM_Unsigned (Usr_GetNumUsrsWhoClaimToBelongToAnotherCty ()); HTM_TD_End (); HTM_TD_Begin ("class=\"DAT RM\""); @@ -294,7 +293,7 @@ void Cty_ListCountries2 (void) HTM_TD_End (); HTM_TD_Begin ("class=\"DAT RM\""); - HTM_Unsigned (Cty_GetNumUsrsWhoClaimToBelongToCty (-1L)); + HTM_Unsigned (Usr_GetNumUsrsWhoDontClaimToBelongToAnyCty ()); HTM_TD_End (); HTM_TD_Begin ("class=\"DAT RM\""); @@ -417,7 +416,7 @@ static void Cty_ListOneCountryForSeeing (struct Country *Cty,unsigned NumCty) /* Write stats of this country */ HTM_TD_Begin ("class=\"DAT RM %s\"",BgColor); - HTM_Unsigned (Cty->NumUsrsWhoClaimToBelongToCty); + HTM_Unsigned (Usr_GetNumUsrsWhoClaimToBelongToCty (Cty)); HTM_TD_End (); HTM_TD_Begin ("class=\"DAT RM %s\"",BgColor); @@ -478,21 +477,6 @@ static void Cty_PutIconToEditCountries (void) Ico_PutContextualIconToEdit (ActEdiCty,NULL); } -/*****************************************************************************/ -/******** Get number of users who claim to belong to other countries *********/ -/*****************************************************************************/ - -static unsigned Cty_GetNumUsrsWhoClaimToBelongToCty (long CtyCod) - { - /***** Get number of users from database *****/ - return - (unsigned) DB_QueryCOUNT ("can not get number of users" - " who claim to belong to other countries", - "SELECT COUNT(*) FROM usr_data" - " WHERE CtyCod=%ld", - CtyCod); - } - /*****************************************************************************/ /********************* Draw country map and name with link *******************/ /*****************************************************************************/ @@ -582,6 +566,7 @@ void Cty_WriteScriptGoogleGeochart (void) extern const char *Txt_Users_NO_HTML; extern const char *Txt_Institutions_NO_HTML; unsigned NumCty; + unsigned NumUsrsWhoClaimToBelongToCty; unsigned MaxUsrsInCountry = 0; unsigned NumCtysWithUsrs = 0; @@ -606,17 +591,20 @@ void Cty_WriteScriptGoogleGeochart (void) for (NumCty = 0; NumCty < Gbl.Hierarchy.Sys.Ctys.Num; NumCty++) - if (Gbl.Hierarchy.Sys.Ctys.Lst[NumCty].NumUsrsWhoClaimToBelongToCty) + { + NumUsrsWhoClaimToBelongToCty = Usr_GetNumUsrsWhoClaimToBelongToCty (&Gbl.Hierarchy.Sys.Ctys.Lst[NumCty]); + if (NumUsrsWhoClaimToBelongToCty) { /* Write data of this country */ HTM_TxtF (" ['%s', %u, %u],\n", Gbl.Hierarchy.Sys.Ctys.Lst[NumCty].Alpha2, - Gbl.Hierarchy.Sys.Ctys.Lst[NumCty].NumUsrsWhoClaimToBelongToCty, + NumUsrsWhoClaimToBelongToCty, Gbl.Hierarchy.Sys.Ctys.Lst[NumCty].Inss.Num); - if (Gbl.Hierarchy.Sys.Ctys.Lst[NumCty].NumUsrsWhoClaimToBelongToCty > MaxUsrsInCountry) - MaxUsrsInCountry = Gbl.Hierarchy.Sys.Ctys.Lst[NumCty].NumUsrsWhoClaimToBelongToCty; + if (NumUsrsWhoClaimToBelongToCty > MaxUsrsInCountry) + MaxUsrsInCountry = NumUsrsWhoClaimToBelongToCty; NumCtysWithUsrs++; } + } /***** Write end of the script *****/ HTM_TxtF (" ]);\n" @@ -855,7 +843,7 @@ void Cty_GetListCountries (Cty_GetExtraData_t GetExtraData) Cty->Name[Lan][0] = '\0'; Cty->WWW[Lan][0] = '\0'; } - Cty->NumUsrsWhoClaimToBelongToCty = 0; + Cty->NumUsrsWhoClaimToBelongToCty.Valid = false; Cty->Inss.Num = Cty->NumCtrs = Cty->NumDegs = Cty->NumCrss = 0; Cty->NumUsrs = 0; @@ -877,8 +865,10 @@ void Cty_GetListCountries (Cty_GetExtraData_t GetExtraData) /* Get number of users who claim to belong to this country */ if (sscanf (row[1 + Lan_NUM_LANGUAGES * 2 + 1],"%u", - &Cty->NumUsrsWhoClaimToBelongToCty) != 1) - Cty->NumUsrsWhoClaimToBelongToCty = 0; + &(Cty->NumUsrsWhoClaimToBelongToCty.NumUsrs)) == 1) + Cty->NumUsrsWhoClaimToBelongToCty.Valid = true; + else + Cty->NumUsrsWhoClaimToBelongToCty.Valid = false; /* Get number of centres in this country */ Cty->NumCtrs = Ctr_GetNumCtrsInCty (Cty->CtyCod); @@ -1017,7 +1007,7 @@ bool Cty_GetDataOfCountryByCod (struct Country *Cty,Cty_GetExtraData_t GetExtraD Cty->Name[Lan][0] = '\0'; Cty->WWW[Lan][0] = '\0'; } - Cty->NumUsrsWhoClaimToBelongToCty = 0; + Cty->NumUsrsWhoClaimToBelongToCty.Valid = false; Cty->Inss.Num = Cty->NumCtrs = Cty->NumDegs = Cty->NumCrss = 0; Cty->NumUsrs = 0; @@ -1059,39 +1049,31 @@ bool Cty_GetDataOfCountryByCod (struct Country *Cty,Cty_GetExtraData_t GetExtraD Lan++) { snprintf (StrField,sizeof (StrField), - "countries.Name_%s,", + ",countries.Name_%s", Lan_STR_LANG_ID[Lan]); Str_Concat (SubQueryNam1,StrField, Cty_MAX_BYTES_SUBQUERY_CTYS); snprintf (StrField,sizeof (StrField), - "Name_%s,", + ",Name_%s", Lan_STR_LANG_ID[Lan]); Str_Concat (SubQueryNam2,StrField, Cty_MAX_BYTES_SUBQUERY_CTYS); snprintf (StrField,sizeof (StrField), - "countries.WWW_%s,", + ",countries.WWW_%s", Lan_STR_LANG_ID[Lan]); Str_Concat (SubQueryWWW1,StrField, Cty_MAX_BYTES_SUBQUERY_CTYS); snprintf (StrField,sizeof (StrField), - "WWW_%s,", + ",WWW_%s", Lan_STR_LANG_ID[Lan]); Str_Concat (SubQueryWWW2,StrField, Cty_MAX_BYTES_SUBQUERY_CTYS); } NumRows = DB_QuerySELECT (&mysql_res,"can not get data of a country", - "(SELECT countries.Alpha2,%s%sCOUNT(*) AS NumUsrs" - " FROM countries,usr_data" - " WHERE countries.CtyCod='%03ld'" - " AND countries.CtyCod=usr_data.CtyCod)" - " UNION " - "(SELECT Alpha2,%s%s0 AS NumUsrs" + "SELECT Alpha2,%s%s" " FROM countries" - " WHERE CtyCod='%03ld'" - " AND CtyCod NOT IN" - " (SELECT DISTINCT CtyCod FROM usr_data))", - SubQueryNam1,SubQueryWWW1,Cty->CtyCod, + " WHERE CtyCod='%03ld'", SubQueryNam2,SubQueryWWW2,Cty->CtyCod); break; } @@ -1129,11 +1111,6 @@ bool Cty_GetDataOfCountryByCod (struct Country *Cty,Cty_GetExtraData_t GetExtraD Cns_MAX_BYTES_WWW); } - /* Get number of users who claim to belong to this country */ - if (sscanf (row[Lan_NUM_LANGUAGES * 2 + 1],"%u", - &Cty->NumUsrsWhoClaimToBelongToCty) != 1) - Cty->NumUsrsWhoClaimToBelongToCty = 0; - /* Get number of user in courses of this institution */ Cty->NumUsrs = Usr_GetNumUsrsInCrssOfCty (Rol_UNK,Cty->CtyCod); // Here Rol_UNK means "all users" @@ -1235,7 +1212,7 @@ static void Cty_ListCountriesForEdition (void) HTM_TABLE_BeginWidePadding (2); Cty_PutHeadCountriesForEdition (); - /***** Write all the countries *****/ + /***** Write all countries *****/ for (NumCty = 0; NumCty < Gbl.Hierarchy.Sys.Ctys.Num; NumCty++) @@ -1247,9 +1224,11 @@ static void Cty_ListCountriesForEdition (void) /* Put icon to remove country */ HTM_TD_Begin ("rowspan=\"%u\" class=\"BT\"",1 + Lan_NUM_LANGUAGES); - if (NumInssInCty || // Country has institutions - Cty->NumUsrsWhoClaimToBelongToCty || - Cty->NumUsrs) // Country has users + if (NumInssInCty || // Country has institutions + Cty->NumUsrs) // Country has users + // Deletion forbidden + Ico_PutIconRemovalNotAllowed (); + else if (Usr_GetNumUsrsWhoClaimToBelongToCty (Cty)) // Country has users // Deletion forbidden Ico_PutIconRemovalNotAllowed (); else @@ -1275,7 +1254,7 @@ static void Cty_ListCountriesForEdition (void) /* Number of users */ HTM_TD_Begin ("rowspan=\"%u\" class=\"DAT RT\"",1 + Lan_NUM_LANGUAGES); - HTM_Unsigned (Cty->NumUsrsWhoClaimToBelongToCty); + HTM_Unsigned (Usr_GetNumUsrsWhoClaimToBelongToCty (Cty)); HTM_TD_End (); /* Number of institutions */ @@ -1383,11 +1362,13 @@ void Cty_RemoveCountry (void) Cty_GetDataOfCountryByCod (Cty_EditingCty,Cty_GET_EXTRA_DATA); /***** Check if this country has users *****/ - if (Ins_GetNumInssInCty (Cty_EditingCty->CtyCod)) // Country has institutions ==> 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 + else if (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 if (Usr_GetNumUsrsWhoClaimToBelongToCty (Cty_EditingCty)) // 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 @@ -2108,7 +2089,7 @@ static void Cty_EditingCountryConstructor (void) Cty_EditingCty->NumDegs = 0; Cty_EditingCty->NumCrss = 0; Cty_EditingCty->NumUsrs = 0; - Cty_EditingCty->NumUsrsWhoClaimToBelongToCty = 0; + Cty_EditingCty->NumUsrsWhoClaimToBelongToCty.Valid = false; } static void Cty_EditingCountryDestructor (void) diff --git a/swad_country.h b/swad_country.h index 01733ca4..61f46fbb 100644 --- a/swad_country.h +++ b/swad_country.h @@ -61,7 +61,11 @@ struct Country unsigned NumDegs; unsigned NumCrss; unsigned NumUsrs; // Number of users in courses of the institution - unsigned NumUsrsWhoClaimToBelongToCty; + struct + { + bool Valid; + unsigned NumUsrs; + } NumUsrsWhoClaimToBelongToCty; }; #define Cty_NUM_ORDERS 2 diff --git a/swad_country_config.c b/swad_country_config.c index 562f34b3..e1e6a8e5 100644 --- a/swad_country_config.c +++ b/swad_country_config.c @@ -459,7 +459,7 @@ static void CtyCfg_NumUsrs (void) /* Data */ HTM_TD_Begin ("class=\"DAT LB\""); - HTM_Unsigned (Usr_GetNumUsrsWhoClaimToBelongToCty (Gbl.Hierarchy.Cty.CtyCod)); + HTM_Unsigned (Usr_GetNumUsrsWhoClaimToBelongToCty (&Gbl.Hierarchy.Cty)); HTM_TD_End (); HTM_TR_End (); diff --git a/swad_global.c b/swad_global.c index 50ce0d0b..ab33354c 100644 --- a/swad_global.c +++ b/swad_global.c @@ -414,6 +414,8 @@ void Gbl_InitializeGlobals (void) Crs_FlushCacheNumCrssInIns (); Crs_FlushCacheNumCrssInCtr (); Crs_FlushCacheNumCrssInDeg (); + Usr_FlushCacheNumUsrsWhoDontClaimToBelongToAnyCty (); + Usr_FlushCacheNumUsrsWhoClaimToBelongToAnotherCty (); Usr_FlushCacheNumUsrsWhoClaimToBelongToIns (); Usr_FlushCacheNumUsrsWhoClaimToBelongToCtr (); Usr_FlushCacheNumUsrsInCrssOfIns (); diff --git a/swad_global.h b/swad_global.h index 4cf40fac..7ad16337 100644 --- a/swad_global.h +++ b/swad_global.h @@ -813,6 +813,21 @@ struct Globals long DegCod; unsigned NumCrss; } NumCrssInDeg; + struct + { + bool Valid; + unsigned NumUsrs; + } NumUsrsWhoDontClaimToBelongToAnyCty; + struct + { + bool Valid; + unsigned NumUsrs; + } NumUsrsWhoClaimToBelongToAnotherCty; + struct + { + long CtyCod; + unsigned NumUsrs; + } NumUsrsWhoClaimToBelongToCty; struct { long InsCod; diff --git a/swad_user.c b/swad_user.c index 3d4a67af..4cf708fd 100644 --- a/swad_user.c +++ b/swad_user.c @@ -1177,7 +1177,7 @@ unsigned Usr_GetNumUsrsInCrssOfAUsr (long UsrCod,Rol_Role_t UsrRole, Usr_MAX_BYTES_ROLES_STR); } NumUsrs = - (unsigned) DB_QueryCOUNT ("can not get the number of users", + (unsigned) DB_QueryCOUNT ("can not get number of users", "SELECT COUNT(DISTINCT crs_usr.UsrCod)" " FROM crs_usr,usr_courses_tmp" " WHERE crs_usr.CrsCod=usr_courses_tmp.CrsCod" @@ -4206,13 +4206,13 @@ unsigned Usr_GetNumUsrsInCrs (Rol_Role_t Role,long CrsCod) Gbl.Cache.NumUsrsInCrs[Role].CrsCod = CrsCod; if (Role == Rol_UNK) Gbl.Cache.NumUsrsInCrs[Rol_UNK].NumUsrs = - (unsigned) DB_QueryCOUNT ("can not get the number of users in a course", + (unsigned) DB_QueryCOUNT ("can not get number of users", "SELECT COUNT(*) FROM crs_usr" " WHERE CrsCod=%ld", CrsCod); else Gbl.Cache.NumUsrsInCrs[Role].NumUsrs = - (unsigned) DB_QueryCOUNT ("can not get the number of users in a course", + (unsigned) DB_QueryCOUNT ("can not get number of users", "SELECT COUNT(*) FROM crs_usr" " WHERE CrsCod=%ld AND Role=%u", CrsCod,(unsigned) Role); @@ -4263,7 +4263,7 @@ unsigned Usr_GetNumUsrsInCrssOfDeg (Rol_Role_t Role,long DegCod) Gbl.Cache.NumUsrsInCrssOfDeg[Role].DegCod = DegCod; if (Role == Rol_UNK) // Any user Gbl.Cache.NumUsrsInCrssOfDeg[Rol_UNK].NumUsrs = - (unsigned) DB_QueryCOUNT ("can not get the number of users" + (unsigned) DB_QueryCOUNT ("can not get number of users" " in courses of a degree", "SELECT COUNT(DISTINCT crs_usr.UsrCod)" " FROM courses,crs_usr" @@ -4272,7 +4272,7 @@ unsigned Usr_GetNumUsrsInCrssOfDeg (Rol_Role_t Role,long DegCod) DegCod); else Gbl.Cache.NumUsrsInCrssOfDeg[Role].NumUsrs = - (unsigned) DB_QueryCOUNT ("can not get the number of users" + (unsigned) DB_QueryCOUNT ("can not get number of users" " in courses of a degree", "SELECT COUNT(DISTINCT crs_usr.UsrCod)" " FROM courses,crs_usr" @@ -4328,7 +4328,7 @@ unsigned Usr_GetNumUsrsInCrssOfCtr (Rol_Role_t Role,long CtrCod) Gbl.Cache.NumUsrsInCrssOfCtr[Role].CtrCod = CtrCod; if (Role == Rol_UNK) // Any user Gbl.Cache.NumUsrsInCrssOfCtr[Rol_UNK].NumUsrs = - (unsigned) DB_QueryCOUNT ("can not get the number of users" + (unsigned) DB_QueryCOUNT ("can not get number of users" " in courses of a centre", "SELECT COUNT(DISTINCT crs_usr.UsrCod)" " FROM degrees,courses,crs_usr" @@ -4340,7 +4340,7 @@ unsigned Usr_GetNumUsrsInCrssOfCtr (Rol_Role_t Role,long CtrCod) // This query is very slow. // It's a bad idea to get number of teachers or students for a big list of centres Gbl.Cache.NumUsrsInCrssOfCtr[Role].NumUsrs = - (unsigned) DB_QueryCOUNT ("can not get the number of users" + (unsigned) DB_QueryCOUNT ("can not get number of users" " in courses of a centre", "SELECT COUNT(DISTINCT crs_usr.UsrCod)" " FROM degrees,courses,crs_usr" @@ -4397,8 +4397,7 @@ unsigned Usr_GetNumUsrsInCrssOfIns (Rol_Role_t Role,long InsCod) Gbl.Cache.NumUsrsInCrssOfIns[Role].InsCod = InsCod; if (Role == Rol_UNK) // Any user Gbl.Cache.NumUsrsInCrssOfIns[Rol_UNK].NumUsrs = - (unsigned) DB_QueryCOUNT ("can not get the number of users" - " in courses of an institution", + (unsigned) DB_QueryCOUNT ("can not get number of users", "SELECT COUNT(DISTINCT crs_usr.UsrCod)" " FROM centres,degrees,courses,crs_usr" " WHERE centres.InsCod=%ld" @@ -4410,8 +4409,7 @@ unsigned Usr_GetNumUsrsInCrssOfIns (Rol_Role_t Role,long InsCod) // This query is very slow. // It's a bad idea to get number of teachers or students for a big list of institutions Gbl.Cache.NumUsrsInCrssOfIns[Role].NumUsrs = - (unsigned) DB_QueryCOUNT ("can not get the number of users" - " in courses of an institution", + (unsigned) DB_QueryCOUNT ("can not get number of users", "SELECT COUNT(DISTINCT crs_usr.UsrCod)" " FROM centres,degrees,courses,crs_usr" " WHERE centres.InsCod=%ld" @@ -4435,8 +4433,7 @@ unsigned Usr_GetNumUsrsInCrssOfCty (Rol_Role_t Role,long CtyCod) /***** Get the number of users in courses of a country from database ******/ if (Role == Rol_UNK) // Any user NumUsrs = - (unsigned) DB_QueryCOUNT ("can not get the number of users" - " in courses of a country", + (unsigned) DB_QueryCOUNT ("can not get number of users", "SELECT COUNT(DISTINCT crs_usr.UsrCod)" " FROM institutions,centres,degrees,courses,crs_usr" " WHERE institutions.CtyCod=%ld" @@ -4449,8 +4446,7 @@ unsigned Usr_GetNumUsrsInCrssOfCty (Rol_Role_t Role,long CtyCod) // This query is very slow. // It's a bad idea to get number of teachers or students for a big list of countries NumUsrs = - (unsigned) DB_QueryCOUNT ("can not get the number of users" - " in courses of a country", + (unsigned) DB_QueryCOUNT ("can not get number of users", "SELECT COUNT(DISTINCT crs_usr.UsrCod)" " FROM institutions,centres,degrees,courses,crs_usr" " WHERE institutions.CtyCod=%ld" @@ -4544,18 +4540,91 @@ unsigned Usr_GetNumTchsCurrentInsInDepartment (long DptCod) (unsigned) Rol_NET,(unsigned) Rol_TCH); } +/*****************************************************************************/ +/******* Get number of users who don't claim to belong to any country ********/ +/*****************************************************************************/ + +void Usr_FlushCacheNumUsrsWhoDontClaimToBelongToAnyCty (void) + { + Gbl.Cache.NumUsrsWhoDontClaimToBelongToAnyCty.Valid = false; + } + +unsigned Usr_GetNumUsrsWhoDontClaimToBelongToAnyCty (void) + { + /***** 1. Fast check: If cached... *****/ + if (Gbl.Cache.NumUsrsWhoDontClaimToBelongToAnyCty.Valid) + return Gbl.Cache.NumUsrsWhoDontClaimToBelongToAnyCty.NumUsrs; + + /***** 2. Slow: number of users who don't claim to belong to any country + from database *****/ + Gbl.Cache.NumUsrsWhoDontClaimToBelongToAnyCty.NumUsrs = + (unsigned) DB_QueryCOUNT ("can not get number of users", + "SELECT COUNT(UsrCod) FROM usr_data" + " WHERE CtyCod<0"); + Gbl.Cache.NumUsrsWhoDontClaimToBelongToAnyCty.Valid = true; + return Gbl.Cache.NumUsrsWhoDontClaimToBelongToAnyCty.NumUsrs; + } + +/*****************************************************************************/ +/******** Get number of users who claim to belong to another country *********/ +/*****************************************************************************/ + +void Usr_FlushCacheNumUsrsWhoClaimToBelongToAnotherCty (void) + { + Gbl.Cache.NumUsrsWhoClaimToBelongToAnotherCty.Valid = false; + } + +unsigned Usr_GetNumUsrsWhoClaimToBelongToAnotherCty (void) + { + /***** 1. Fast check: If cached... *****/ + if (Gbl.Cache.NumUsrsWhoClaimToBelongToAnotherCty.Valid) + return Gbl.Cache.NumUsrsWhoClaimToBelongToAnotherCty.NumUsrs; + + /***** 2. Slow: number of users who claim to belong to another country + from database *****/ + Gbl.Cache.NumUsrsWhoClaimToBelongToAnotherCty.NumUsrs = + (unsigned) DB_QueryCOUNT ("can not get number of users", + "SELECT COUNT(UsrCod) FROM usr_data" + " WHERE CtyCod=0"); + Gbl.Cache.NumUsrsWhoClaimToBelongToAnotherCty.Valid = true; + return Gbl.Cache.NumUsrsWhoClaimToBelongToAnotherCty.NumUsrs; + } + /*****************************************************************************/ /*********** Get number of users who claim to belong to a country ************/ /*****************************************************************************/ -unsigned Usr_GetNumUsrsWhoClaimToBelongToCty (long CtyCod) +void Usr_FlushCacheNumUsrsWhoClaimToBelongToCty (void) { - /***** Get the number of users in a country from database *****/ - return - (unsigned) DB_QueryCOUNT ("can not get the number of users in a country", + Gbl.Cache.NumUsrsWhoClaimToBelongToCty.CtyCod = -1L; + Gbl.Cache.NumUsrsWhoClaimToBelongToCty.NumUsrs = 0; + } + +unsigned Usr_GetNumUsrsWhoClaimToBelongToCty (struct Country *Cty) + { + /***** 1. Fast check: Trivial case *****/ + if (Cty->CtyCod <= 0) + return 0; + + /***** 2. Fast check: If cached... *****/ + if (Cty->CtyCod == Gbl.Cache.NumUsrsWhoClaimToBelongToCty.CtyCod) + { + Cty->NumUsrsWhoClaimToBelongToCty.NumUsrs = Gbl.Cache.NumUsrsWhoClaimToBelongToCty.NumUsrs; + Cty->NumUsrsWhoClaimToBelongToCty.Valid = true; + return Cty->NumUsrsWhoClaimToBelongToCty.NumUsrs; + } + + /***** 3. Slow: number of users who claim to belong to an institution + from database *****/ + Gbl.Cache.NumUsrsWhoClaimToBelongToCty.CtyCod = Cty->CtyCod; + Gbl.Cache.NumUsrsWhoClaimToBelongToCty.NumUsrs = + Cty->NumUsrsWhoClaimToBelongToCty.NumUsrs = + (unsigned) DB_QueryCOUNT ("can not get number of users", "SELECT COUNT(UsrCod) FROM usr_data" " WHERE CtyCod=%ld", - CtyCod); + Cty->CtyCod); + Cty->NumUsrsWhoClaimToBelongToCty.Valid = true; + return Cty->NumUsrsWhoClaimToBelongToCty.NumUsrs; } /*****************************************************************************/ @@ -4582,8 +4651,7 @@ unsigned Usr_GetNumUsrsWhoClaimToBelongToIns (long InsCod) from database *****/ Gbl.Cache.NumUsrsWhoClaimToBelongToIns.InsCod = InsCod; Gbl.Cache.NumUsrsWhoClaimToBelongToIns.NumUsrs = - (unsigned) DB_QueryCOUNT ("can not get the number of users" - " in an institution", + (unsigned) DB_QueryCOUNT ("can not get number of users", "SELECT COUNT(UsrCod) FROM usr_data" " WHERE InsCod=%ld", InsCod); @@ -4614,7 +4682,7 @@ unsigned Usr_GetNumUsrsWhoClaimToBelongToCtr (long CtrCod) from database *****/ Gbl.Cache.NumUsrsWhoClaimToBelongToCtr.CtrCod = CtrCod; Gbl.Cache.NumUsrsWhoClaimToBelongToCtr.NumUsrs = - (unsigned) DB_QueryCOUNT ("can not get the number of users in a centre", + (unsigned) DB_QueryCOUNT ("can not get number of users", "SELECT COUNT(UsrCod) FROM usr_data" " WHERE CtrCod=%ld", CtrCod); diff --git a/swad_user.h b/swad_user.h index fc9b163a..fb845227 100644 --- a/swad_user.h +++ b/swad_user.h @@ -31,6 +31,7 @@ #include "swad_action.h" #include "swad_constant.h" +#include "swad_country.h" #include "swad_cryptography.h" #include "swad_date.h" #include "swad_degree.h" @@ -421,7 +422,11 @@ long Usr_GetRamdomStdFromCrs (long CrsCod); long Usr_GetRamdomStdFromGrp (long GrpCod); unsigned Usr_GetNumTchsCurrentInsInDepartment (long DptCod); -unsigned Usr_GetNumUsrsWhoClaimToBelongToCty (long CtyCod); +void Usr_FlushCacheNumUsrsWhoDontClaimToBelongToAnyCty (void); +unsigned Usr_GetNumUsrsWhoDontClaimToBelongToAnyCty (void); +void Usr_FlushCacheNumUsrsWhoClaimToBelongToAnotherCty (void); +unsigned Usr_GetNumUsrsWhoClaimToBelongToAnotherCty (void); +unsigned Usr_GetNumUsrsWhoClaimToBelongToCty (struct Country *Cty); void Usr_FlushCacheNumUsrsWhoClaimToBelongToIns (void); unsigned Usr_GetNumUsrsWhoClaimToBelongToIns (long InsCod); void Usr_FlushCacheNumUsrsWhoClaimToBelongToCtr (void);