Version 18.7.39

This commit is contained in:
Antonio Cañas Vargas 2018-10-24 09:25:09 +02:00
parent 41a6f927ab
commit 0b43d04e52
4 changed files with 220 additions and 169 deletions

View File

@ -355,10 +355,11 @@ En OpenSWAD:
ps2pdf source.ps destination.pdf ps2pdf source.ps destination.pdf
*/ */
#define Log_PLATFORM_VERSION "SWAD 18.7.38 (2018-10-24)" #define Log_PLATFORM_VERSION "SWAD 18.7.39 (2018-10-24)"
#define CSS_FILE "swad18.4.css" #define CSS_FILE "swad18.4.css"
#define JS_FILE "swad17.17.1.js" #define JS_FILE "swad17.17.1.js"
/* /*
Version 18.7.39: Oct 24, 2018 Some sprintf for database queries changed by asprintf. (238037 lines)
Version 18.7.38: Oct 24, 2018 Some sprintf for database queries changed by asprintf. (237986 lines) Version 18.7.38: Oct 24, 2018 Some sprintf for database queries changed by asprintf. (237986 lines)
Version 18.7.37: Oct 24, 2018 Some sprintf for database queries changed by asprintf. (237926 lines) Version 18.7.37: Oct 24, 2018 Some sprintf for database queries changed by asprintf. (237926 lines)
Version 18.7.36: Oct 23, 2018 Some sprintf for database queries changed by asprintf. (237905 lines) Version 18.7.36: Oct 23, 2018 Some sprintf for database queries changed by asprintf. (237905 lines)

View File

@ -25,7 +25,9 @@
/********************************** Headers **********************************/ /********************************** Headers **********************************/
/*****************************************************************************/ /*****************************************************************************/
#define _GNU_SOURCE // For asprintf
#include <linux/stddef.h> // For NULL #include <linux/stddef.h> // For NULL
#include <stdio.h> // For asprintf
#include "swad_action.h" #include "swad_action.h"
#include "swad_database.h" #include "swad_database.h"
@ -798,14 +800,15 @@ unsigned Pag_GetParamPagNum (Pag_WhatPaginate_t WhatPaginate)
void Pag_SaveLastPageMsgIntoSession (Pag_WhatPaginate_t WhatPaginate,unsigned NumPage) void Pag_SaveLastPageMsgIntoSession (Pag_WhatPaginate_t WhatPaginate,unsigned NumPage)
{ {
char Query[128 + Cns_BYTES_SESSION_ID]; char *Query;
/***** Save last page of received/sent messages *****/ /***** Save last page of received/sent messages *****/
sprintf (Query,"UPDATE sessions SET %s=%u WHERE SessionId='%s'", if (asprintf (&Query,"UPDATE sessions SET %s=%u WHERE SessionId='%s'",
WhatPaginate == Pag_MESSAGES_RECEIVED ? "LastPageMsgRcv" : WhatPaginate == Pag_MESSAGES_RECEIVED ? "LastPageMsgRcv" :
"LastPageMsgSnt", "LastPageMsgSnt",
NumPage,Gbl.Session.Id); NumPage,Gbl.Session.Id) < 0)
DB_QueryUPDATE (Query,"can not update last page of messages"); Lay_NotEnoughMemoryExit ();
DB_QueryUPDATE_free (Query,"can not update last page of messages");
} }
/*****************************************************************************/ /*****************************************************************************/
@ -814,18 +817,19 @@ void Pag_SaveLastPageMsgIntoSession (Pag_WhatPaginate_t WhatPaginate,unsigned Nu
unsigned Pag_GetLastPageMsgFromSession (Pag_WhatPaginate_t WhatPaginate) unsigned Pag_GetLastPageMsgFromSession (Pag_WhatPaginate_t WhatPaginate)
{ {
char Query[128 + Cns_BYTES_SESSION_ID]; char *Query;
MYSQL_RES *mysql_res; MYSQL_RES *mysql_res;
MYSQL_ROW row; MYSQL_ROW row;
unsigned long NumRows; unsigned long NumRows;
unsigned NumPage; unsigned NumPage;
/***** Get last page of received/sent messages from database *****/ /***** Get last page of received/sent messages from database *****/
sprintf (Query,"SELECT %s FROM sessions WHERE SessionId='%s'", if (asprintf (&Query,"SELECT %s FROM sessions WHERE SessionId='%s'",
WhatPaginate == Pag_MESSAGES_RECEIVED ? "LastPageMsgRcv" : WhatPaginate == Pag_MESSAGES_RECEIVED ? "LastPageMsgRcv" :
"LastPageMsgSnt", "LastPageMsgSnt",
Gbl.Session.Id); Gbl.Session.Id) < 0)
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get last page of messages"); Lay_NotEnoughMemoryExit ();
NumRows = DB_QuerySELECT_free (Query,&mysql_res,"can not get last page of messages");
/***** Check number of rows of the result ****/ /***** Check number of rows of the result ****/
if (NumRows != 1) if (NumRows != 1)

View File

@ -25,6 +25,8 @@
/********************************* Headers ***********************************/ /********************************* Headers ***********************************/
/*****************************************************************************/ /*****************************************************************************/
#define _GNU_SOURCE // For asprintf
#include <stdio.h> // For asprintf
#include <stdlib.h> // For system, getenv, etc. #include <stdlib.h> // For system, getenv, etc.
#include <string.h> // For string functions #include <string.h> // For string functions
#include <sys/wait.h> // For the macro WEXITSTATUS #include <sys/wait.h> // For the macro WEXITSTATUS
@ -112,15 +114,16 @@ bool Pwd_CheckCurrentPassword (void)
bool Pwd_CheckPendingPassword (void) bool Pwd_CheckPendingPassword (void)
{ {
char Query[256]; char *Query;
MYSQL_RES *mysql_res; MYSQL_RES *mysql_res;
MYSQL_ROW row; MYSQL_ROW row;
/***** Get pending password from database *****/ /***** Get pending password from database *****/
sprintf (Query,"SELECT PendingPassword FROM pending_passwd" if (asprintf (&Query,"SELECT PendingPassword FROM pending_passwd"
" WHERE UsrCod=%ld", " WHERE UsrCod=%ld",
Gbl.Usrs.Me.UsrDat.UsrCod); Gbl.Usrs.Me.UsrDat.UsrCod) < 0)
if (DB_QuerySELECT (Query,&mysql_res,"can not get pending password")) Lay_NotEnoughMemoryExit ();
if (DB_QuerySELECT_free (Query,&mysql_res,"can not get pending password"))
{ {
/* Get encrypted pending password */ /* Get encrypted pending password */
row = mysql_fetch_row (mysql_res); row = mysql_fetch_row (mysql_res);
@ -144,14 +147,15 @@ bool Pwd_CheckPendingPassword (void)
void Pwd_AssignMyPendingPasswordToMyCurrentPassword (void) void Pwd_AssignMyPendingPasswordToMyCurrentPassword (void)
{ {
char Query[128 + Pwd_BYTES_ENCRYPTED_PASSWORD]; char *Query;
/***** Update my current password in database *****/ /***** Update my current password in database *****/
sprintf (Query,"UPDATE usr_data SET Password='%s'" if (asprintf (&Query,"UPDATE usr_data SET Password='%s'"
" WHERE UsrCod=%ld", " WHERE UsrCod=%ld",
Gbl.Usrs.Me.PendingPassword, Gbl.Usrs.Me.PendingPassword,
Gbl.Usrs.Me.UsrDat.UsrCod); Gbl.Usrs.Me.UsrDat.UsrCod) < 0)
DB_QueryUPDATE (Query,"can not update your password"); Lay_NotEnoughMemoryExit ();
DB_QueryUPDATE_free (Query,"can not update your password");
/***** Update my current password *****/ /***** Update my current password *****/
Str_Copy (Gbl.Usrs.Me.UsrDat.Password,Gbl.Usrs.Me.PendingPassword, Str_Copy (Gbl.Usrs.Me.UsrDat.Password,Gbl.Usrs.Me.PendingPassword,
@ -510,24 +514,26 @@ static void Pwd_CreateANewPassword (char PlainPassword[Pwd_MAX_BYTES_PLAIN_PASSW
void Pwd_SetMyPendingPassword (char PlainPassword[Pwd_MAX_BYTES_PLAIN_PASSWORD + 1]) void Pwd_SetMyPendingPassword (char PlainPassword[Pwd_MAX_BYTES_PLAIN_PASSWORD + 1])
{ {
char Query[256 + Pwd_BYTES_ENCRYPTED_PASSWORD]; char *Query;
/***** Encrypt my pending password *****/ /***** Encrypt my pending password *****/
Cry_EncryptSHA512Base64 (PlainPassword,Gbl.Usrs.Me.PendingPassword); Cry_EncryptSHA512Base64 (PlainPassword,Gbl.Usrs.Me.PendingPassword);
/***** Remove expired pending passwords from database *****/ /***** Remove expired pending passwords from database *****/
sprintf (Query,"DELETE FROM pending_passwd" if (asprintf (&Query,"DELETE FROM pending_passwd"
" WHERE DateAndTime<FROM_UNIXTIME(UNIX_TIMESTAMP()-'%lu')", " WHERE DateAndTime<FROM_UNIXTIME(UNIX_TIMESTAMP()-'%lu')",
Cfg_TIME_TO_DELETE_OLD_PENDING_PASSWORDS); Cfg_TIME_TO_DELETE_OLD_PENDING_PASSWORDS) < 0)
DB_QueryDELETE (Query,"can not remove expired pending passwords"); Lay_NotEnoughMemoryExit ();
DB_QueryDELETE_free (Query,"can not remove expired pending passwords");
/***** Update my current password in database *****/ /***** Update my current password in database *****/
sprintf (Query,"REPLACE INTO pending_passwd" if (asprintf (&Query,"REPLACE INTO pending_passwd"
" (UsrCod,PendingPassword,DateAndTime)" " (UsrCod,PendingPassword,DateAndTime)"
" VALUES" " VALUES"
" (%ld,'%s',NOW())", " (%ld,'%s',NOW())",
Gbl.Usrs.Me.UsrDat.UsrCod,Gbl.Usrs.Me.PendingPassword); Gbl.Usrs.Me.UsrDat.UsrCod,Gbl.Usrs.Me.PendingPassword) < 0)
DB_QueryREPLACE (Query,"can not create pending password"); Lay_NotEnoughMemoryExit ();
DB_QueryREPLACE_free (Query,"can not create pending password");
} }
/*****************************************************************************/ /*****************************************************************************/
@ -574,21 +580,23 @@ bool Pwd_SlowCheckIfPasswordIsGood (const char *PlainPassword,
static bool Pwd_CheckIfPasswdIsUsrIDorName (const char *PlainPassword) static bool Pwd_CheckIfPasswdIsUsrIDorName (const char *PlainPassword)
{ {
char Query[128 + 3 * Pwd_MAX_BYTES_PLAIN_PASSWORD]; char *Query;
bool Found; bool Found;
/***** Get if password is found in user's ID from database *****/ /***** Get if password is found in user's ID from database *****/
sprintf (Query,"SELECT COUNT(*) FROM usr_IDs WHERE UsrID='%s'", if (asprintf (&Query,"SELECT COUNT(*) FROM usr_IDs WHERE UsrID='%s'",
PlainPassword); PlainPassword) < 0)
Found = (DB_QueryCOUNT (Query,"can not check if a password matches a user's ID") != 0); Lay_NotEnoughMemoryExit ();
Found = (DB_QueryCOUNT_free (Query,"can not check if a password matches a user's ID") != 0);
/***** Get if password is found in first name or surnames of anybody, from database *****/ /***** Get if password is found in first name or surnames of anybody, from database *****/
if (!Found) if (!Found)
{ {
sprintf (Query,"SELECT COUNT(*) FROM usr_data" if (asprintf (&Query,"SELECT COUNT(*) FROM usr_data"
" WHERE FirstName='%s' OR Surname1='%s' OR Surname2='%s'", " WHERE FirstName='%s' OR Surname1='%s' OR Surname2='%s'",
PlainPassword,PlainPassword,PlainPassword); PlainPassword,PlainPassword,PlainPassword) < 0)
Found = (DB_QueryCOUNT (Query,"can not check if a password matches a first name or a surname") != 0); Lay_NotEnoughMemoryExit ();
Found = (DB_QueryCOUNT_free (Query,"can not check if a password matches a first name or a surname") != 0);
} }
return Found; return Found;
@ -600,19 +608,25 @@ static bool Pwd_CheckIfPasswdIsUsrIDorName (const char *PlainPassword)
static unsigned Pwd_GetNumOtherUsrsWhoUseThisPassword (const char *EncryptedPassword,long UsrCod) static unsigned Pwd_GetNumOtherUsrsWhoUseThisPassword (const char *EncryptedPassword,long UsrCod)
{ {
char Query[512]; char *Query;
/***** Get number of other users who use a password from database *****/ /***** Get number of other users who use a password from database *****/
/* Query database */ /* Query database */
if (UsrCod > 0) if (UsrCod > 0)
sprintf (Query,"SELECT COUNT(*) FROM usr_data" {
" WHERE Password='%s' AND UsrCod<>%ld", if (asprintf (&Query,"SELECT COUNT(*) FROM usr_data"
EncryptedPassword,UsrCod); " WHERE Password='%s' AND UsrCod<>%ld",
EncryptedPassword,UsrCod) < 0)
Lay_NotEnoughMemoryExit ();
}
else else
sprintf (Query,"SELECT COUNT(*) FROM usr_data" {
" WHERE Password='%s'", if (asprintf (&Query,"SELECT COUNT(*) FROM usr_data"
EncryptedPassword); " WHERE Password='%s'",
return (unsigned) DB_QueryCOUNT (Query,"can not check if a password is trivial"); EncryptedPassword) < 0)
Lay_NotEnoughMemoryExit ();
}
return (unsigned) DB_QueryCOUNT_free (Query,"can not check if a password is trivial");
} }
/*****************************************************************************/ /*****************************************************************************/

View File

@ -25,9 +25,11 @@
/********************************* Headers ***********************************/ /********************************* Headers ***********************************/
/*****************************************************************************/ /*****************************************************************************/
#define _GNU_SOURCE // For asprintf
#include <linux/limits.h> // For PATH_MAX #include <linux/limits.h> // For PATH_MAX
#include <linux/stddef.h> // For NULL #include <linux/stddef.h> // For NULL
#include <math.h> // For log10, floor, ceil, modf, sqrt... #include <math.h> // For log10, floor, ceil, modf, sqrt...
#include <stdio.h> // For asprintf
#include <stdlib.h> // For system, getenv, etc. #include <stdlib.h> // For system, getenv, etc.
#include <string.h> // For string functions #include <string.h> // For string functions
#include <sys/wait.h> // For the macro WEXITSTATUS #include <sys/wait.h> // For the macro WEXITSTATUS
@ -119,7 +121,7 @@ static void Pho_PutLinkToCalculateDegreeStats (void);
static void Pho_GetMaxStdsPerDegree (void); static void Pho_GetMaxStdsPerDegree (void);
static void Pho_ShowOrPrintClassPhotoDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint); static void Pho_ShowOrPrintClassPhotoDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint);
static void Pho_ShowOrPrintListDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint); static void Pho_ShowOrPrintListDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint);
static void Pho_BuildQueryOfDegrees (char *Query); static void Pho_BuildQueryOfDegrees (char **Query);
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_UpdateDegStats (long DegCod,Usr_Sex_t Sex,unsigned NumStds,unsigned NumStdsWithPhoto,long TimeToComputeAvgPhoto); static void Pho_UpdateDegStats (long DegCod,Usr_Sex_t Sex,unsigned NumStds,unsigned NumStdsWithPhoto,long TimeToComputeAvgPhoto);
static void Pho_ShowDegreeStat (int NumStds,int NumStdsWithPhoto); static void Pho_ShowDegreeStat (int NumStds,int NumStdsWithPhoto);
@ -975,17 +977,18 @@ static void Pho_UpdatePhoto2 (void)
unsigned Pho_UpdateMyClicksWithoutPhoto (void) unsigned Pho_UpdateMyClicksWithoutPhoto (void)
{ {
char Query[512]; char *Query;
MYSQL_RES *mysql_res; MYSQL_RES *mysql_res;
MYSQL_ROW row; MYSQL_ROW row;
unsigned long NumRows; unsigned long NumRows;
unsigned NumClicks; unsigned NumClicks;
/***** Get number of clicks without photo from database *****/ /***** Get number of clicks without photo from database *****/
sprintf (Query,"SELECT NumClicks FROM clicks_without_photo" if (asprintf (&Query,"SELECT NumClicks FROM clicks_without_photo"
" WHERE UsrCod=%ld", " WHERE UsrCod=%ld",
Gbl.Usrs.Me.UsrDat.UsrCod); Gbl.Usrs.Me.UsrDat.UsrCod) < 0)
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get number of clicks without photo"); Lay_NotEnoughMemoryExit ();
NumRows = DB_QuerySELECT_free (Query,&mysql_res,"can not get number of clicks without photo");
/***** Update the list of clicks without photo *****/ /***** Update the list of clicks without photo *****/
if (NumRows) // The user exists ==> update number of clicks without photo if (NumRows) // The user exists ==> update number of clicks without photo
@ -997,22 +1000,24 @@ unsigned Pho_UpdateMyClicksWithoutPhoto (void)
/* Update number of clicks */ /* Update number of clicks */
if (NumClicks <= Pho_MAX_CLICKS_WITHOUT_PHOTO) if (NumClicks <= Pho_MAX_CLICKS_WITHOUT_PHOTO)
{ {
sprintf (Query,"UPDATE clicks_without_photo" if (asprintf (&Query,"UPDATE clicks_without_photo"
" SET NumClicks=NumClicks+1 WHERE UsrCod=%ld", " SET NumClicks=NumClicks+1 WHERE UsrCod=%ld",
Gbl.Usrs.Me.UsrDat.UsrCod); Gbl.Usrs.Me.UsrDat.UsrCod) < 0)
DB_QueryUPDATE (Query,"can not update number of clicks without photo"); Lay_NotEnoughMemoryExit ();
DB_QueryUPDATE_free (Query,"can not update number of clicks without photo");
NumClicks++; NumClicks++;
} }
} }
else // The user does not exist ==> add him/her else // The user does not exist ==> add him/her
{ {
/* Add the user, with one access */ /* Add the user, with one access */
sprintf (Query,"INSERT INTO clicks_without_photo" if (asprintf (&Query,"INSERT INTO clicks_without_photo"
" (UsrCod,NumClicks)" " (UsrCod,NumClicks)"
" VALUES" " VALUES"
" (%ld,1)", " (%ld,1)",
Gbl.Usrs.Me.UsrDat.UsrCod); Gbl.Usrs.Me.UsrDat.UsrCod) < 0)
DB_QueryINSERT (Query,"can not create number of clicks without photo"); Lay_NotEnoughMemoryExit ();
DB_QueryINSERT_free (Query,"can not create number of clicks without photo");
NumClicks = 1; NumClicks = 1;
} }
@ -1029,10 +1034,12 @@ unsigned Pho_UpdateMyClicksWithoutPhoto (void)
void Pho_RemoveUsrFromTableClicksWithoutPhoto (long UsrCod) void Pho_RemoveUsrFromTableClicksWithoutPhoto (long UsrCod)
{ {
char Query[512]; char *Query;
sprintf (Query,"DELETE FROM clicks_without_photo WHERE UsrCod=%ld",UsrCod); if (asprintf (&Query,"DELETE FROM clicks_without_photo WHERE UsrCod=%ld",
DB_QueryDELETE (Query,"can not remove a user from the list of users without photo"); UsrCod) < 0)
Lay_NotEnoughMemoryExit ();
DB_QueryDELETE_free (Query,"can not remove a user from the list of users without photo");
} }
/*****************************************************************************/ /*****************************************************************************/
@ -1185,13 +1192,14 @@ bool Pho_RemovePhoto (struct UsrData *UsrDat)
static void Pho_ClearPhotoName (long UsrCod) static void Pho_ClearPhotoName (long UsrCod)
{ {
char Query[128]; char *Query;
/***** Clear photo name in user's data *****/ /***** Clear photo name in user's data *****/
sprintf (Query,"UPDATE usr_data SET Photo=''" if (asprintf (&Query,"UPDATE usr_data SET Photo=''"
" WHERE UsrCod=%ld", " WHERE UsrCod=%ld",
UsrCod); UsrCod) < 0)
DB_QueryUPDATE (Query,"can not clear the name of a user's photo"); Lay_NotEnoughMemoryExit ();
DB_QueryUPDATE_free (Query,"can not clear the name of a user's photo");
} }
/*****************************************************************************/ /*****************************************************************************/
@ -1200,14 +1208,15 @@ static void Pho_ClearPhotoName (long UsrCod)
void Pho_UpdatePhotoName (struct UsrData *UsrDat) void Pho_UpdatePhotoName (struct UsrData *UsrDat)
{ {
char Query[512]; char *Query;
char PathPublPhoto[PATH_MAX + 1]; char PathPublPhoto[PATH_MAX + 1];
/***** Update photo name in database *****/ /***** Update photo name in database *****/
sprintf (Query,"UPDATE usr_data SET Photo='%s'" if (asprintf (&Query,"UPDATE usr_data SET Photo='%s'"
" WHERE UsrCod=%ld", " WHERE UsrCod=%ld",
Gbl.UniqueNameEncrypted,UsrDat->UsrCod); Gbl.UniqueNameEncrypted,UsrDat->UsrCod) < 0)
DB_QueryUPDATE (Query,"can not update the name of a user's photo"); Lay_NotEnoughMemoryExit ();
DB_QueryUPDATE_free (Query,"can not update the name of a user's photo");
/***** Remove the old symbolic link to photo *****/ /***** Remove the old symbolic link to photo *****/
snprintf (PathPublPhoto,sizeof (PathPublPhoto), snprintf (PathPublPhoto,sizeof (PathPublPhoto),
@ -1329,17 +1338,18 @@ void Pho_ShowUsrPhoto (const struct UsrData *UsrDat,const char *PhotoURL,
void Pho_ChangePhotoVisibility (void) void Pho_ChangePhotoVisibility (void)
{ {
extern const char *Pri_VisibilityDB[Pri_NUM_OPTIONS_PRIVACY]; extern const char *Pri_VisibilityDB[Pri_NUM_OPTIONS_PRIVACY];
char Query[128]; char *Query;
/***** Get param with public/private photo *****/ /***** Get param with public/private photo *****/
Gbl.Usrs.Me.UsrDat.PhotoVisibility = Pri_GetParamVisibility ("VisPho"); Gbl.Usrs.Me.UsrDat.PhotoVisibility = Pri_GetParamVisibility ("VisPho");
/***** Store public/private photo in database *****/ /***** Store public/private photo in database *****/
sprintf (Query,"UPDATE usr_data SET PhotoVisibility='%s'" if (asprintf (&Query,"UPDATE usr_data SET PhotoVisibility='%s'"
" WHERE UsrCod=%ld", " WHERE UsrCod=%ld",
Pri_VisibilityDB[Gbl.Usrs.Me.UsrDat.PhotoVisibility], Pri_VisibilityDB[Gbl.Usrs.Me.UsrDat.PhotoVisibility],
Gbl.Usrs.Me.UsrDat.UsrCod); Gbl.Usrs.Me.UsrDat.UsrCod) < 0)
DB_QueryUPDATE (Query,"can not update your preference about photo visibility"); Lay_NotEnoughMemoryExit ();
DB_QueryUPDATE_free (Query,"can not update your preference about photo visibility");
/***** Show form again *****/ /***** Show form again *****/
Pre_EditPrefs (); Pre_EditPrefs ();
@ -1433,7 +1443,7 @@ void Pho_CalcPhotoDegree (void)
static long Pho_GetDegWithAvgPhotoLeastRecentlyUpdated (void) static long Pho_GetDegWithAvgPhotoLeastRecentlyUpdated (void)
{ {
char Query[1024]; char *Query;
MYSQL_RES *mysql_res; MYSQL_RES *mysql_res;
MYSQL_ROW row; MYSQL_ROW row;
unsigned long NumRows = 0; unsigned long NumRows = 0;
@ -1445,16 +1455,17 @@ static long Pho_GetDegWithAvgPhotoLeastRecentlyUpdated (void)
/***** 1. If a degree is not in table of computed degrees, /***** 1. If a degree is not in table of computed degrees,
choose it as least recently updated *****/ choose it as least recently updated *****/
/* Get one degree with students not yet computed */ /* Get one degree with students not yet computed */
sprintf (Query,"SELECT DISTINCT degrees.DegCod" if (asprintf (&Query,"SELECT DISTINCT degrees.DegCod"
" FROM degrees,courses,crs_usr" " FROM degrees,courses,crs_usr"
" WHERE degrees.DegCod=courses.DegCod" " WHERE degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=crs_usr.CrsCod" " AND courses.CrsCod=crs_usr.CrsCod"
" AND crs_usr.Role=%u" " AND crs_usr.Role=%u"
" AND degrees.DegCod NOT IN" " AND degrees.DegCod NOT IN"
" (SELECT DISTINCT DegCod FROM sta_degrees)" " (SELECT DISTINCT DegCod FROM sta_degrees)"
" LIMIT 1", " LIMIT 1",
(unsigned) Rol_STD); (unsigned) Rol_STD) < 0)
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get degrees"); Lay_NotEnoughMemoryExit ();
NumRows = DB_QuerySELECT_free (Query,&mysql_res,"can not get degrees");
/* If number of rows is 1, then get the degree code */ /* If number of rows is 1, then get the degree code */
if (NumRows == 1) if (NumRows == 1)
@ -1474,16 +1485,17 @@ static long Pho_GetDegWithAvgPhotoLeastRecentlyUpdated (void)
/***** 2. If all the degrees are in table, /***** 2. If all the degrees are in table,
choose the least recently updated that has students *****/ choose the least recently updated that has students *****/
/* Get degrees from database */ /* Get degrees from database */
sprintf (Query,"SELECT sta_degrees.DegCod" if (asprintf (&Query,"SELECT sta_degrees.DegCod"
" FROM sta_degrees,courses,crs_usr" " FROM sta_degrees,courses,crs_usr"
" WHERE sta_degrees.TimeAvgPhoto<FROM_UNIXTIME(UNIX_TIMESTAMP()-'%lu')" " WHERE sta_degrees.TimeAvgPhoto<FROM_UNIXTIME(UNIX_TIMESTAMP()-'%lu')"
" AND sta_degrees.DegCod=courses.DegCod" " AND sta_degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=crs_usr.CrsCod" " AND courses.CrsCod=crs_usr.CrsCod"
" AND crs_usr.Role=%u" " AND crs_usr.Role=%u"
" ORDER BY sta_degrees.TimeAvgPhoto LIMIT 1", " ORDER BY sta_degrees.TimeAvgPhoto LIMIT 1",
Cfg_MIN_TIME_TO_RECOMPUTE_AVG_PHOTO, Cfg_MIN_TIME_TO_RECOMPUTE_AVG_PHOTO,
(unsigned) Rol_STD); (unsigned) Rol_STD) < 0)
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get degrees"); Lay_NotEnoughMemoryExit ();
NumRows = DB_QuerySELECT_free (Query,&mysql_res,"can not get degrees");
/* If number of rows is 1, then get the degree code */ /* If number of rows is 1, then get the degree code */
if (NumRows == 1) if (NumRows == 1)
@ -1509,11 +1521,12 @@ static long Pho_GetDegWithAvgPhotoLeastRecentlyUpdated (void)
void Pho_RemoveObsoleteStatDegrees (void) void Pho_RemoveObsoleteStatDegrees (void)
{ {
char Query[512]; char *Query;
sprintf (Query,"DELETE FROM sta_degrees" if (asprintf (&Query,"DELETE FROM sta_degrees"
" WHERE DegCod NOT IN (SELECT DegCod FROM degrees)"); " WHERE DegCod NOT IN (SELECT DegCod FROM degrees)") < 0)
DB_QueryDELETE (Query,"can not remove old degrees from stats"); Lay_NotEnoughMemoryExit ();
DB_QueryDELETE_free (Query,"can not remove old degrees from stats");
} }
/*****************************************************************************/ /*****************************************************************************/
@ -1522,17 +1535,19 @@ void Pho_RemoveObsoleteStatDegrees (void)
static long Pho_GetTimeAvgPhotoWasComputed (long DegCod) static long Pho_GetTimeAvgPhotoWasComputed (long DegCod)
{ {
char Query[256]; char *Query;
MYSQL_RES *mysql_res; MYSQL_RES *mysql_res;
MYSQL_ROW row; MYSQL_ROW row;
unsigned long NumRows; unsigned long NumRows;
long TimeAvgPhotoWasComputed = 0L; long TimeAvgPhotoWasComputed = 0L;
/***** Get last time an average photo was computed from database *****/ /***** Get last time an average photo was computed from database *****/
sprintf (Query,"SELECT MIN(UNIX_TIMESTAMP(TimeAvgPhoto))" if (asprintf (&Query,"SELECT MIN(UNIX_TIMESTAMP(TimeAvgPhoto))"
" FROM sta_degrees WHERE DegCod=%ld", " FROM sta_degrees WHERE DegCod=%ld",
DegCod); DegCod) < 0)
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get last time an average photo was computed"); Lay_NotEnoughMemoryExit ();
NumRows = DB_QuerySELECT_free (Query,&mysql_res,
"can not get last time an average photo was computed");
if (NumRows == 1) if (NumRows == 1)
{ {
@ -1556,7 +1571,7 @@ static long Pho_GetTimeAvgPhotoWasComputed (long DegCod)
static long Pho_GetTimeToComputeAvgPhoto (long DegCod) static long Pho_GetTimeToComputeAvgPhoto (long DegCod)
{ {
char Query[256]; char *Query;
MYSQL_RES *mysql_res; MYSQL_RES *mysql_res;
MYSQL_ROW row; MYSQL_ROW row;
unsigned long NumRows; unsigned long NumRows;
@ -1565,9 +1580,11 @@ static long Pho_GetTimeToComputeAvgPhoto (long DegCod)
long TotalTimeToComputeAvgPhoto = -1L; long TotalTimeToComputeAvgPhoto = -1L;
/***** Get time to compute average photo from database *****/ /***** Get time to compute average photo from database *****/
sprintf (Query,"SELECT TimeToComputeAvgPhoto FROM sta_degrees" if (asprintf (&Query,"SELECT TimeToComputeAvgPhoto FROM sta_degrees"
" WHERE DegCod=%ld",DegCod); " WHERE DegCod=%ld",DegCod) < 0)
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get time to compute average photo"); Lay_NotEnoughMemoryExit ();
NumRows = DB_QuerySELECT_free (Query,&mysql_res,
"can not get time to compute average photo");
/***** Count number of rows in result *****/ /***** Count number of rows in result *****/
if (NumRows == Usr_NUM_SEXS) if (NumRows == Usr_NUM_SEXS)
@ -2077,17 +2094,19 @@ static void Pho_PutLinkToCalculateDegreeStats (void)
static void Pho_GetMaxStdsPerDegree (void) static void Pho_GetMaxStdsPerDegree (void)
{ {
char Query[512]; char *Query;
MYSQL_RES *mysql_res; MYSQL_RES *mysql_res;
MYSQL_ROW row; MYSQL_ROW row;
unsigned long NumRows; unsigned long NumRows;
/***** Get maximum number of students in a degree from database *****/ /***** Get maximum number of students in a degree from database *****/
sprintf (Query,"SELECT MAX(NumStds),MAX(NumStdsWithPhoto)," if (asprintf (&Query,"SELECT MAX(NumStds),MAX(NumStdsWithPhoto),"
"MAX(NumStdsWithPhoto/NumStds)" "MAX(NumStdsWithPhoto/NumStds)"
" FROM sta_degrees" " FROM sta_degrees"
" WHERE Sex='all' AND NumStds>0"); " WHERE Sex='all' AND NumStds>0") < 0)
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get maximum number of students in a degree"); Lay_NotEnoughMemoryExit ();
NumRows = DB_QuerySELECT_free (Query,&mysql_res,
"can not get maximum number of students in a degree");
/***** Count number of rows in result *****/ /***** Count number of rows in result *****/
if (NumRows == 1) if (NumRows == 1)
@ -2125,7 +2144,7 @@ static void Pho_GetMaxStdsPerDegree (void)
static void Pho_ShowOrPrintClassPhotoDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint) static void Pho_ShowOrPrintClassPhotoDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint)
{ {
char Query[512]; char *Query;
MYSQL_RES *mysql_res; MYSQL_RES *mysql_res;
MYSQL_ROW row; MYSQL_ROW row;
unsigned long NumRow; unsigned long NumRow;
@ -2137,8 +2156,8 @@ static void Pho_ShowOrPrintClassPhotoDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrin
bool TRIsOpen = false; bool TRIsOpen = false;
/***** Get degrees from database *****/ /***** Get degrees from database *****/
Pho_BuildQueryOfDegrees (Query); Pho_BuildQueryOfDegrees (&Query);
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get degrees"); NumRows = DB_QuerySELECT_free (Query,&mysql_res,"can not get degrees");
if (NumRows) // Degrees with students found if (NumRows) // Degrees with students found
{ {
@ -2207,7 +2226,7 @@ static void Pho_ShowOrPrintListDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint)
extern const char *Txt_No_INDEX; extern const char *Txt_No_INDEX;
extern const char *Txt_Degree; extern const char *Txt_Degree;
extern const char *Txt_SEX_PLURAL_Abc[Usr_NUM_SEXS]; extern const char *Txt_SEX_PLURAL_Abc[Usr_NUM_SEXS];
char Query[512]; char *Query;
MYSQL_RES *mysql_res; MYSQL_RES *mysql_res;
MYSQL_ROW row; MYSQL_ROW row;
unsigned long NumRow; unsigned long NumRow;
@ -2219,8 +2238,8 @@ static void Pho_ShowOrPrintListDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint)
Usr_Sex_t Sex; Usr_Sex_t Sex;
/***** Get degrees from database *****/ /***** Get degrees from database *****/
Pho_BuildQueryOfDegrees (Query); Pho_BuildQueryOfDegrees (&Query);
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get degrees"); NumRows = DB_QuerySELECT_free (Query,&mysql_res,"can not get degrees");
if (NumRows) // Degrees with students found if (NumRows) // Degrees with students found
{ {
@ -2316,41 +2335,50 @@ static void Pho_ShowOrPrintListDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint)
/****** Build a query to get the degrees ordered by different criteria *******/ /****** Build a query to get the degrees ordered by different criteria *******/
/*****************************************************************************/ /*****************************************************************************/
static void Pho_BuildQueryOfDegrees (char *Query) static void Pho_BuildQueryOfDegrees (char **Query)
{ {
switch (Gbl.Stat.DegPhotos.HowOrderDegrees) switch (Gbl.Stat.DegPhotos.HowOrderDegrees)
{ {
case Pho_NUMBER_OF_STUDENTS: case Pho_NUMBER_OF_STUDENTS:
sprintf (Query,"SELECT degrees.DegCod" if (asprintf (Query,"SELECT degrees.DegCod"
" FROM degrees,sta_degrees" " FROM degrees,sta_degrees"
" WHERE sta_degrees.Sex='all'" " WHERE sta_degrees.Sex='all'"
" AND sta_degrees.NumStds>0" " AND sta_degrees.NumStds>0"
" AND degrees.DegCod=sta_degrees.DegCod" " AND degrees.DegCod=sta_degrees.DegCod"
" ORDER BY sta_degrees.NumStds DESC,sta_degrees.NumStdsWithPhoto DESC,degrees.ShortName"); " ORDER BY sta_degrees.NumStds DESC,"
"sta_degrees.NumStdsWithPhoto DESC,"
"degrees.ShortName") < 0)
Lay_NotEnoughMemoryExit ();
break; break;
case Pho_NUMBER_OF_PHOTOS: case Pho_NUMBER_OF_PHOTOS:
sprintf (Query,"SELECT degrees.DegCod" if (asprintf (Query,"SELECT degrees.DegCod"
" FROM degrees,sta_degrees" " FROM degrees,sta_degrees"
" WHERE sta_degrees.Sex='all'" " WHERE sta_degrees.Sex='all'"
" AND sta_degrees.NumStds>0" " AND sta_degrees.NumStds>0"
" AND degrees.DegCod=sta_degrees.DegCod" " AND degrees.DegCod=sta_degrees.DegCod"
" ORDER BY sta_degrees.NumStdsWithPhoto DESC,sta_degrees.NumStds DESC,degrees.ShortName"); " ORDER BY sta_degrees.NumStdsWithPhoto DESC,"
"sta_degrees.NumStds DESC,"
"degrees.ShortName") < 0)
Lay_NotEnoughMemoryExit ();
break; break;
case Pho_PERCENT: case Pho_PERCENT:
sprintf (Query,"SELECT degrees.DegCod" if (asprintf (Query,"SELECT degrees.DegCod"
" FROM degrees,sta_degrees" " FROM degrees,sta_degrees"
" WHERE sta_degrees.Sex='all'" " WHERE sta_degrees.Sex='all'"
" AND sta_degrees.NumStds>0" " AND sta_degrees.NumStds>0"
" AND degrees.DegCod=sta_degrees.DegCod" " AND degrees.DegCod=sta_degrees.DegCod"
" ORDER BY sta_degrees.NumStdsWithPhoto/sta_degrees.NumStds DESC,degrees.ShortName"); " ORDER BY sta_degrees.NumStdsWithPhoto/sta_degrees.NumStds DESC,"
"degrees.ShortName") < 0)
Lay_NotEnoughMemoryExit ();
break; break;
case Pho_DEGREE_NAME: case Pho_DEGREE_NAME:
sprintf (Query,"SELECT degrees.DegCod" if (asprintf (Query,"SELECT degrees.DegCod"
" FROM degrees,sta_degrees" " FROM degrees,sta_degrees"
" WHERE sta_degrees.Sex='all'" " WHERE sta_degrees.Sex='all'"
" AND sta_degrees.NumStds>0" " AND sta_degrees.NumStds>0"
" AND degrees.DegCod=sta_degrees.DegCod" " AND degrees.DegCod=sta_degrees.DegCod"
" ORDER BY degrees.ShortName"); " ORDER BY degrees.ShortName") < 0)
Lay_NotEnoughMemoryExit ();
break; break;
} }
} }
@ -2362,16 +2390,17 @@ static void Pho_BuildQueryOfDegrees (char *Query)
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]; extern const char *Usr_StringsSexDB[Usr_NUM_SEXS];
char Query[512]; char *Query;
MYSQL_RES *mysql_res; MYSQL_RES *mysql_res;
MYSQL_ROW row; MYSQL_ROW row;
unsigned long NumRows; unsigned long NumRows;
/***** Get the number of students in a degree from database *****/ /***** Get the number of students in a degree from database *****/
sprintf (Query,"SELECT NumStds,NumStdsWithPhoto FROM sta_degrees" if (asprintf (&Query,"SELECT NumStds,NumStdsWithPhoto FROM sta_degrees"
" WHERE DegCod=%ld AND Sex='%s'", " WHERE DegCod=%ld AND Sex='%s'",
DegCod,Usr_StringsSexDB[Sex]); DegCod,Usr_StringsSexDB[Sex]) < 0)
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get the number of students in a degree"); Lay_NotEnoughMemoryExit ();
NumRows = DB_QuerySELECT_free (Query,&mysql_res,"can not get the number of students in a degree");
if (NumRows == 0) if (NumRows == 0)
*NumStds = *NumStdsWithPhoto = -1; *NumStds = *NumStdsWithPhoto = -1;
@ -2395,14 +2424,17 @@ static void Pho_GetNumStdsInDegree (long DegCod,Usr_Sex_t Sex,int *NumStds,int *
static void Pho_UpdateDegStats (long DegCod,Usr_Sex_t Sex,unsigned NumStds,unsigned NumStdsWithPhoto,long TimeToComputeAvgPhotoInMicroseconds) static void Pho_UpdateDegStats (long DegCod,Usr_Sex_t Sex,unsigned NumStds,unsigned NumStdsWithPhoto,long TimeToComputeAvgPhotoInMicroseconds)
{ {
extern const char *Usr_StringsSexDB[Usr_NUM_SEXS]; extern const char *Usr_StringsSexDB[Usr_NUM_SEXS];
char Query[1024]; char *Query;
sprintf (Query,"REPLACE INTO sta_degrees" if (asprintf (&Query,"REPLACE INTO sta_degrees"
" (DegCod,Sex,NumStds,NumStdsWithPhoto,TimeAvgPhoto,TimeToComputeAvgPhoto)" " (DegCod,Sex,NumStds,NumStdsWithPhoto,"
" VALUES" "TimeAvgPhoto,TimeToComputeAvgPhoto)"
" (%ld,'%s',%u,%u,NOW(),%ld)", " VALUES"
DegCod,Usr_StringsSexDB[Sex],NumStds,NumStdsWithPhoto,TimeToComputeAvgPhotoInMicroseconds); " (%ld,'%s',%u,%u,NOW(),%ld)",
DB_QueryREPLACE (Query,"can not save stats of a degree"); DegCod,Usr_StringsSexDB[Sex],NumStds,NumStdsWithPhoto,
TimeToComputeAvgPhotoInMicroseconds) < 0)
Lay_NotEnoughMemoryExit ();
DB_QueryREPLACE_free (Query,"can not save stats of a degree");
} }
/*****************************************************************************/ /*****************************************************************************/