diff --git a/swad_changelog.h b/swad_changelog.h index c8e87c63..883061a6 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -602,13 +602,14 @@ TODO: FIX BUG, URGENT! En las fechas como par TODO: En las encuestas, que los estudiantes no puedan ver los resultados hasta que no finalice el plazo. */ -#define Log_PLATFORM_VERSION "SWAD 21.24 (2021-10-05)" +#define Log_PLATFORM_VERSION "SWAD 21.24.1 (2021-10-05)" #define CSS_FILE "swad20.45.css" #define JS_FILE "swad20.69.1.js" /* TODO: Rename CENTRE to CENTER in help wiki. TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams + Version 21.24.1: Oct 05, 2021 Queries moved to module swad_photo_database. (318406 lines) Version 21.24: Oct 05, 2021 New module swad_photo_database for database queries related to users' photos. (318352 lines) Version 21.23: Oct 04, 2021 New module swad_password_database for database queries related to passwords. (318197 lines) Version 21.22: Oct 04, 2021 Queries related to notifications moved to other modules. (318066 lines) diff --git a/swad_photo.c b/swad_photo.c index 07822fd3..015ce5bf 100644 --- a/swad_photo.c +++ b/swad_photo.c @@ -129,7 +129,8 @@ static void Pho_PutLinkToCalculateDegreeStats (const struct Pho_DegPhotos *DegPh static void Pho_GetMaxStdsPerDegree (struct Pho_DegPhotos *DegPhotos); static void Pho_ShowOrPrintClassPhotoDegrees (struct Pho_DegPhotos *DegPhotos, Pho_AvgPhotoSeeOrPrint_t SeeOrPrint); -static void Pho_GetNumStdsInDegree (long DegCod,Usr_Sex_t Sex,int *NumStds,int *NumStdsWithPhoto); +static void Pho_GetNumStdsInDegree (long DegCod,Usr_Sex_t Sex, + int *NumStds,int *NumStdsWithPhoto); static void Pho_ShowOrPrintListDegrees (struct Pho_DegPhotos *DegPhotos, Pho_AvgPhotoSeeOrPrint_t SeeOrPrint); @@ -1387,38 +1388,12 @@ static long Pho_GetDegWithAvgPhotoLeastRecentlyUpdated (void) /***** 1. If a degree is not in table of computed degrees, choose it as least recently updated *****/ - DegCod = DB_QuerySELECTCode ("can not get degrees", - "SELECT DISTINCT deg_degrees.DegCod" - " FROM deg_degrees," - "crs_courses," - "crs_users" - " WHERE deg_degrees.DegCod=crs_courses.DegCod" - " AND crs_courses.CrsCod=crs_users.CrsCod" - " AND crs_users.Role=%u" - " AND deg_degrees.DegCod NOT IN" - " (SELECT DISTINCT DegCod" - " FROM sta_degrees)" - " LIMIT 1", - (unsigned) Rol_STD); + if ((DegCod = Pho_DB_GetADegWithStdsNotInTableOfComputedDegs ()) > 0) + return DegCod; - if (DegCod <= 0) - /***** 2. If all the degrees are in table, - choose the least recently updated that has students *****/ - DegCod = DB_QuerySELECTCode ("can not get degrees", - "SELECT sta_degrees.DegCod" - " FROM sta_degrees," - "crs_courses," - "crs_users" - " WHERE sta_degrees.TimeAvgPhoto0") == 1) + if (Pho_DB_GetMaxStdsPerDegree (&mysql_res) == 1) { row = mysql_fetch_row (mysql_res); @@ -2230,26 +2189,19 @@ static void Pho_ShowOrPrintListDegrees (struct Pho_DegPhotos *DegPhotos, /*** Get number of students and number of students with photo in a degree ****/ /*****************************************************************************/ -static void Pho_GetNumStdsInDegree (long DegCod,Usr_Sex_t Sex,int *NumStds,int *NumStdsWithPhoto) +static void Pho_GetNumStdsInDegree (long DegCod,Usr_Sex_t Sex, + int *NumStds,int *NumStdsWithPhoto) { - extern const char *Usr_StringsSexDB[Usr_NUM_SEXS]; MYSQL_RES *mysql_res; MYSQL_ROW row; /***** Get the number of students in a degree from database *****/ - if (DB_QuerySELECT (&mysql_res,"can not get the number of students" - " in a degree", - "SELECT NumStds," // row[0] - "NumStdsWithPhoto" // row[1] - " FROM sta_degrees" - " WHERE DegCod=%ld" - " AND Sex='%s'", - DegCod, - Usr_StringsSexDB[Sex]) == 0) - *NumStds = *NumStdsWithPhoto = -1; - else + if (Pho_DB_GetNumStdsInDegree (&mysql_res,DegCod,Sex)) { row = mysql_fetch_row (mysql_res); + + /* Get number of students (row[0]) + and number of students with photo (row[1]) */ if (sscanf (row[0],"%d",NumStds) != 1) *NumStds = -1; if (sscanf (row[1],"%d",NumStdsWithPhoto) != 1) @@ -2258,6 +2210,8 @@ static void Pho_GetNumStdsInDegree (long DegCod,Usr_Sex_t Sex,int *NumStds,int * /***** Free structure that stores the query result *****/ DB_FreeMySQLResult (&mysql_res); } + else + *NumStds = *NumStdsWithPhoto = -1; } /*****************************************************************************/ diff --git a/swad_photo_database.c b/swad_photo_database.c index ebd2e532..302c04d6 100644 --- a/swad_photo_database.c +++ b/swad_photo_database.c @@ -249,6 +249,111 @@ unsigned Pho_DB_QueryDegrees (MYSQL_RES **mysql_res, return 0; } +/*****************************************************************************/ +/******** Get degree with students not in table of computed degrees **********/ +/*****************************************************************************/ + +long Pho_DB_GetADegWithStdsNotInTableOfComputedDegs (void) + { + return DB_QuerySELECTCode ("can not get degree", + "SELECT DISTINCT deg_degrees.DegCod" + " FROM deg_degrees," + "crs_courses," + "crs_users" + " WHERE deg_degrees.DegCod=crs_courses.DegCod" + " AND crs_courses.CrsCod=crs_users.CrsCod" + " AND crs_users.Role=%u" + " AND deg_degrees.DegCod NOT IN" + " (SELECT DISTINCT DegCod" + " FROM sta_degrees)" + " LIMIT 1", + (unsigned) Rol_STD); + } +/*****************************************************************************/ +/********* Get the least recently updated degree that has students ***********/ +/*****************************************************************************/ + +long Pho_DB_GetDegWithAvgPhotoLeastRecentlyUpdated (void) + { + return DB_QuerySELECTCode ("can not get degree", + "SELECT sta_degrees.DegCod" + " FROM sta_degrees," + "crs_courses," + "crs_users" + " WHERE sta_degrees.TimeAvgPhoto0"); + } + +/*****************************************************************************/ +/*** Get number of students and number of students with photo in a degree ****/ +/*****************************************************************************/ + +unsigned Pho_DB_GetNumStdsInDegree (MYSQL_RES **mysql_res,long DegCod,Usr_Sex_t Sex) + { + extern const char *Usr_StringsSexDB[Usr_NUM_SEXS]; + + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get the number of students in a degree", + "SELECT NumStds," // row[0] + "NumStdsWithPhoto" // row[1] + " FROM sta_degrees" + " WHERE DegCod=%ld" + " AND Sex='%s'", + DegCod, + Usr_StringsSexDB[Sex]); + } + +/*****************************************************************************/ +/********* Get last time an average photo was computed from database *********/ +/*****************************************************************************/ + +unsigned Pho_DB_GetTimeAvgPhotoWasComputed (MYSQL_RES **mysql_res,long DegCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get last time" + " an average photo was computed", + "SELECT COALESCE(MIN(UNIX_TIMESTAMP(TimeAvgPhoto)),0)" // row[0] + " FROM sta_degrees" + " WHERE DegCod=%ld", + DegCod); + } + +/*****************************************************************************/ +/********************* Get time to compute average photo *********************/ +/*****************************************************************************/ + +unsigned Pho_DB_GetTimeToComputeAvgPhoto (MYSQL_RES **mysql_res,long DegCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get time to compute average photo", + "SELECT TimeToComputeAvgPhoto" // row[0] + " FROM sta_degrees" + " WHERE DegCod=%ld", + DegCod); + } + /*****************************************************************************/ /* Delete all the degrees in sta_degrees table not present in degrees table **/ /*****************************************************************************/ diff --git a/swad_photo_database.h b/swad_photo_database.h index e843b47d..57e87954 100644 --- a/swad_photo_database.h +++ b/swad_photo_database.h @@ -57,8 +57,16 @@ void Pho_DB_RemoveUsrFromTableClicksWithoutPhoto (long UsrCod); void Pho_DB_UpdateDegStats (long DegCod,Usr_Sex_t Sex, unsigned NumStds,unsigned NumStdsWithPhoto, long TimeToComputeAvgPhotoInMicroseconds); + unsigned Pho_DB_QueryDegrees (MYSQL_RES **mysql_res, Pho_HowOrderDegrees_t HowOrderDegrees); +long Pho_DB_GetADegWithStdsNotInTableOfComputedDegs (void); +long Pho_DB_GetDegWithAvgPhotoLeastRecentlyUpdated (void); +unsigned Pho_DB_GetMaxStdsPerDegree (MYSQL_RES **mysql_res); +unsigned Pho_DB_GetNumStdsInDegree (MYSQL_RES **mysql_res,long DegCod,Usr_Sex_t Sex); +unsigned Pho_DB_GetTimeAvgPhotoWasComputed (MYSQL_RES **mysql_res,long DegCod); +unsigned Pho_DB_GetTimeToComputeAvgPhoto (MYSQL_RES **mysql_res,long DegCod); + void Pho_DB_RemoveObsoleteStatDegrees (void); #endif