diff --git a/swad_changelog.h b/swad_changelog.h index 24cb50fca..f51045ac3 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -355,10 +355,11 @@ En OpenSWAD: ps2pdf source.ps destination.pdf */ -#define Log_PLATFORM_VERSION "SWAD 18.11.11 (2018-11-01)" +#define Log_PLATFORM_VERSION "SWAD 18.11.12 (2018-11-01)" #define CSS_FILE "swad18.4.css" #define JS_FILE "swad17.17.1.js" /* + Version 18.11.12: Nov 01, 2018 Joining building and performing query into one function. (235965 lines) Version 18.11.11: Nov 01, 2018 Joining building and performing query into one function. (235896 lines) Version 18.11.10: Nov 01, 2018 Joining building and performing query into one function. (235861 lines) Version 18.11.9: Oct 31, 2018 Joining building and performing query into one function. (235847 lines) diff --git a/swad_nickname.c b/swad_nickname.c index 8f78d0956..85854180b 100644 --- a/swad_nickname.c +++ b/swad_nickname.c @@ -120,10 +120,10 @@ bool Nck_GetNicknameFromUsrCod (long UsrCod, bool Found; /***** Get current (last updated) user's nickname from database *****/ - DB_BuildQuery ("SELECT Nickname FROM usr_nicknames" - " WHERE UsrCod=%ld ORDER BY CreatTime DESC LIMIT 1", - UsrCod); - if (DB_QuerySELECT_new (&mysql_res,"can not get nickname")) + if (DB_QuerySELECT (&mysql_res,"can not get nickname", + "SELECT Nickname FROM usr_nicknames" + " WHERE UsrCod=%ld ORDER BY CreatTime DESC LIMIT 1", + UsrCod)) { /* Get nickname */ row = mysql_fetch_row (mysql_res); @@ -166,12 +166,12 @@ long Nck_GetUsrCodFromNickname (const char *Nickname) /***** Get user's code from database *****/ /* Check if user code from table usr_nicknames is also in table usr_data */ - DB_BuildQuery ("SELECT usr_nicknames.UsrCod" - " FROM usr_nicknames,usr_data" - " WHERE usr_nicknames.Nickname='%s'" - " AND usr_nicknames.UsrCod=usr_data.UsrCod", - NicknameWithoutArroba); - if (DB_QuerySELECT_new (&mysql_res,"can not get user's code")) + if (DB_QuerySELECT (&mysql_res,"can not get user's code", + "SELECT usr_nicknames.UsrCod" + " FROM usr_nicknames,usr_data" + " WHERE usr_nicknames.Nickname='%s'" + " AND usr_nicknames.UsrCod=usr_data.UsrCod", + NicknameWithoutArroba)) { /* Get row */ row = mysql_fetch_row (mysql_res); @@ -237,11 +237,12 @@ static void Nck_ShowFormChangeUsrNickname (const struct UsrData *UsrDat,bool Its Lay_StartSection (Nck_NICKNAME_SECTION_ID); /***** Get my nicknames *****/ - DB_BuildQuery ("SELECT Nickname FROM usr_nicknames" - " WHERE UsrCod=%ld" - " ORDER BY CreatTime DESC", - UsrDat->UsrCod); - NumNicks = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get nicknames of a user"); + NumNicks = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get nicknames of a user", + "SELECT Nickname FROM usr_nicknames" + " WHERE UsrCod=%ld" + " ORDER BY CreatTime DESC", + UsrDat->UsrCod); /***** Start box *****/ snprintf (StrRecordWidth,sizeof (StrRecordWidth), diff --git a/swad_notice.c b/swad_notice.c index 843d6aa5b..003211b09 100644 --- a/swad_notice.c +++ b/swad_notice.c @@ -349,7 +349,7 @@ void Not_ShowNotices (Not_Listing_t TypeNoticesListing) char PathRelRSSFile[PATH_MAX + 1]; long NotCod; unsigned long NumNot; - unsigned long NumNotices; + unsigned long NumNotices = 0; // Initialized to avoid warning char Content[Cns_MAX_BYTES_TEXT + 1]; time_t TimeUTC; long UsrCod; @@ -363,22 +363,31 @@ void Not_ShowNotices (Not_Listing_t TypeNoticesListing) switch (TypeNoticesListing) { case Not_LIST_BRIEF_NOTICES: - DB_BuildQuery ("SELECT NotCod,UNIX_TIMESTAMP(CreatTime) AS F,UsrCod,Content,Status" - " FROM notices" - " WHERE CrsCod=%ld AND Status=%u" - " ORDER BY CreatTime DESC", - Gbl.CurrentCrs.Crs.CrsCod, - (unsigned) Not_ACTIVE_NOTICE); + NumNotices = DB_QuerySELECT (&mysql_res,"can not get notices from database", + "SELECT NotCod," + "UNIX_TIMESTAMP(CreatTime) AS F," + "UsrCod," + "Content," + "Status" + " FROM notices" + " WHERE CrsCod=%ld AND Status=%u" + " ORDER BY CreatTime DESC", + Gbl.CurrentCrs.Crs.CrsCod, + (unsigned) Not_ACTIVE_NOTICE); break; case Not_LIST_FULL_NOTICES: - DB_BuildQuery ("SELECT NotCod,UNIX_TIMESTAMP(CreatTime) AS F,UsrCod,Content,Status" - " FROM notices" - " WHERE CrsCod=%ld" - " ORDER BY CreatTime DESC", - Gbl.CurrentCrs.Crs.CrsCod); + NumNotices = DB_QuerySELECT (&mysql_res,"can not get notices from database", + "SELECT NotCod," + "UNIX_TIMESTAMP(CreatTime) AS F," + "UsrCod," + "Content," + "Status" + " FROM notices" + " WHERE CrsCod=%ld" + " ORDER BY CreatTime DESC", + Gbl.CurrentCrs.Crs.CrsCod); break; } - NumNotices = DB_QuerySELECT_new (&mysql_res,"can not get notices from database"); if (TypeNoticesListing == Not_LIST_FULL_NOTICES) { @@ -543,12 +552,14 @@ static void Not_GetDataAndShowNotice (long NotCod) Not_Status_t Status; /***** Get notice data from database *****/ - DB_BuildQuery ("SELECT UNIX_TIMESTAMP(CreatTime) AS F,UsrCod,Content,Status" - " FROM notices" - " WHERE NotCod=%ld AND CrsCod=%ld", - NotCod, - Gbl.CurrentCrs.Crs.CrsCod); - if (DB_QuerySELECT_new (&mysql_res,"can not get notice from database")) + if (DB_QuerySELECT (&mysql_res,"can not get notice from database", + "SELECT UNIX_TIMESTAMP(CreatTime) AS F," + "UsrCod," + "Content," + "Status" + " FROM notices" + " WHERE NotCod=%ld AND CrsCod=%ld", + NotCod,Gbl.CurrentCrs.Crs.CrsCod)) { row = mysql_fetch_row (mysql_res); @@ -756,8 +767,9 @@ void Not_GetSummaryAndContentNotice (char SummaryStr[Ntf_MAX_BYTES_SUMMARY + 1], // This function may be called inside a web service, so don't report error /***** Get subject of message from database *****/ - DB_BuildQuery ("SELECT Content FROM notices WHERE NotCod=%ld",NotCod); - if (DB_QuerySELECT_new (&mysql_res,"can not get content of notice") == 1) // Result should have a unique row + if (DB_QuerySELECT (&mysql_res,"can not get content of notice", + "SELECT Content FROM notices WHERE NotCod=%ld", + NotCod) == 1) // Result should have a unique row { /***** Get sumary / content *****/ row = mysql_fetch_row (mysql_res); @@ -805,66 +817,71 @@ unsigned Not_GetNumNotices (Sco_Scope_t Scope,Not_Status_t Status,unsigned *NumN switch (Scope) { case Sco_SCOPE_SYS: - DB_BuildQuery ("SELECT COUNT(*),SUM(NumNotif)" - " FROM notices" - " WHERE Status=%u", - Status); + DB_QuerySELECT (&mysql_res,"can not get number of notices", + "SELECT COUNT(*),SUM(NumNotif)" + " FROM notices" + " WHERE Status=%u", + Status); break; case Sco_SCOPE_CTY: - DB_BuildQuery ("SELECT COUNT(*),SUM(notices.NumNotif)" - " FROM institutions,centres,degrees,courses,notices" - " WHERE institutions.CtyCod=%ld" - " AND institutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=notices.CrsCod" - " AND notices.Status=%u", - Gbl.CurrentCty.Cty.CtyCod, - Status); + DB_QuerySELECT (&mysql_res,"can not get number of notices", + "SELECT COUNT(*),SUM(notices.NumNotif)" + " FROM institutions,centres,degrees,courses,notices" + " WHERE institutions.CtyCod=%ld" + " AND institutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=notices.CrsCod" + " AND notices.Status=%u", + Gbl.CurrentCty.Cty.CtyCod, + Status); break; case Sco_SCOPE_INS: - DB_BuildQuery ("SELECT COUNT(*),SUM(notices.NumNotif)" - " FROM centres,degrees,courses,notices" - " WHERE centres.InsCod=%ld" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=notices.CrsCod" - " AND notices.Status=%u", - Gbl.CurrentIns.Ins.InsCod, - Status); + DB_QuerySELECT (&mysql_res,"can not get number of notices", + "SELECT COUNT(*),SUM(notices.NumNotif)" + " FROM centres,degrees,courses,notices" + " WHERE centres.InsCod=%ld" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=notices.CrsCod" + " AND notices.Status=%u", + Gbl.CurrentIns.Ins.InsCod, + Status); break; case Sco_SCOPE_CTR: - DB_BuildQuery ("SELECT COUNT(*),SUM(notices.NumNotif)" - " FROM degrees,courses,notices" - " WHERE degrees.CtrCod=%ld" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=notices.CrsCod" - " AND notices.Status=%u", - Gbl.CurrentCtr.Ctr.CtrCod, - Status); + DB_QuerySELECT (&mysql_res,"can not get number of notices", + "SELECT COUNT(*),SUM(notices.NumNotif)" + " FROM degrees,courses,notices" + " WHERE degrees.CtrCod=%ld" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=notices.CrsCod" + " AND notices.Status=%u", + Gbl.CurrentCtr.Ctr.CtrCod, + Status); break; case Sco_SCOPE_DEG: - DB_BuildQuery ("SELECT COUNT(*),SUM(notices.NumNotif)" - " FROM courses,notices" - " WHERE courses.DegCod=%ld" - " AND courses.CrsCod=notices.CrsCod" - " AND notices.Status=%u", - Gbl.CurrentDeg.Deg.DegCod, - Status); + DB_QuerySELECT (&mysql_res,"can not get number of notices", + "SELECT COUNT(*),SUM(notices.NumNotif)" + " FROM courses,notices" + " WHERE courses.DegCod=%ld" + " AND courses.CrsCod=notices.CrsCod" + " AND notices.Status=%u", + Gbl.CurrentDeg.Deg.DegCod, + Status); break; case Sco_SCOPE_CRS: - DB_BuildQuery ("SELECT COUNT(*),SUM(NumNotif)" - " FROM notices" - " WHERE CrsCod=%ld" - " AND Status=%u", - Gbl.CurrentCrs.Crs.CrsCod, - Status); + DB_QuerySELECT (&mysql_res,"can not get number of notices", + "SELECT COUNT(*),SUM(NumNotif)" + " FROM notices" + " WHERE CrsCod=%ld" + " AND Status=%u", + Gbl.CurrentCrs.Crs.CrsCod, + Status); break; default: Lay_WrongScopeExit (); break; } - DB_QuerySELECT_new (&mysql_res,"can not get number of notices"); /***** Get number of notices *****/ row = mysql_fetch_row (mysql_res); @@ -902,54 +919,59 @@ unsigned Not_GetNumNoticesDeleted (Sco_Scope_t Scope,unsigned *NumNotif) switch (Scope) { case Sco_SCOPE_SYS: - DB_BuildQuery ("SELECT COUNT(*),SUM(NumNotif)" - " FROM notices_deleted"); + DB_QuerySELECT (&mysql_res,"can not get number of deleted notices", + "SELECT COUNT(*),SUM(NumNotif)" + " FROM notices_deleted"); break; case Sco_SCOPE_CTY: - DB_BuildQuery ("SELECT COUNT(*),SUM(notices_deleted.NumNotif)" - " FROM institutions,centres,degrees,courses,notices_deleted" - " WHERE institutions.CtyCod=%ld" - " AND institutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=notices_deleted.CrsCod", - Gbl.CurrentCty.Cty.CtyCod); + DB_QuerySELECT (&mysql_res,"can not get number of deleted notices", + "SELECT COUNT(*),SUM(notices_deleted.NumNotif)" + " FROM institutions,centres,degrees,courses,notices_deleted" + " WHERE institutions.CtyCod=%ld" + " AND institutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=notices_deleted.CrsCod", + Gbl.CurrentCty.Cty.CtyCod); break; case Sco_SCOPE_INS: - DB_BuildQuery ("SELECT COUNT(*),SUM(notices_deleted.NumNotif)" - " FROM centres,degrees,courses,notices_deleted" - " WHERE centres.InsCod=%ld" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=notices_deleted.CrsCod", - Gbl.CurrentIns.Ins.InsCod); + DB_QuerySELECT (&mysql_res,"can not get number of deleted notices", + "SELECT COUNT(*),SUM(notices_deleted.NumNotif)" + " FROM centres,degrees,courses,notices_deleted" + " WHERE centres.InsCod=%ld" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=notices_deleted.CrsCod", + Gbl.CurrentIns.Ins.InsCod); break; case Sco_SCOPE_CTR: - DB_BuildQuery ("SELECT COUNT(*),SUM(notices_deleted.NumNotif)" - " FROM degrees,courses,notices_deleted" - " WHERE degrees.CtrCod=%ld" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=notices_deleted.CrsCod", - Gbl.CurrentCtr.Ctr.CtrCod); + DB_QuerySELECT (&mysql_res,"can not get number of deleted notices", + "SELECT COUNT(*),SUM(notices_deleted.NumNotif)" + " FROM degrees,courses,notices_deleted" + " WHERE degrees.CtrCod=%ld" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=notices_deleted.CrsCod", + Gbl.CurrentCtr.Ctr.CtrCod); break; case Sco_SCOPE_DEG: - DB_BuildQuery ("SELECT COUNT(*),SUM(notices_deleted.NumNotif)" - " FROM courses,notices_deleted" - " WHERE courses.DegCod=%ld" - " AND courses.CrsCod=notices_deleted.CrsCod", - Gbl.CurrentDeg.Deg.DegCod); + DB_QuerySELECT (&mysql_res,"can not get number of deleted notices", + "SELECT COUNT(*),SUM(notices_deleted.NumNotif)" + " FROM courses,notices_deleted" + " WHERE courses.DegCod=%ld" + " AND courses.CrsCod=notices_deleted.CrsCod", + Gbl.CurrentDeg.Deg.DegCod); break; case Sco_SCOPE_CRS: - DB_BuildQuery ("SELECT COUNT(*),SUM(NumNotif)" - " FROM notices_deleted" - " WHERE CrsCod=%ld", - Gbl.CurrentCrs.Crs.CrsCod); + DB_QuerySELECT (&mysql_res,"can not get number of deleted notices", + "SELECT COUNT(*),SUM(NumNotif)" + " FROM notices_deleted" + " WHERE CrsCod=%ld", + Gbl.CurrentCrs.Crs.CrsCod); break; default: Lay_WrongScopeExit (); break; } - DB_QuerySELECT_new (&mysql_res,"can not get number of deleted notices"); /***** Get number of notices *****/ row = mysql_fetch_row (mysql_res); diff --git a/swad_notification.c b/swad_notification.c index f8c760d6e..85b2935dd 100644 --- a/swad_notification.c +++ b/swad_notification.c @@ -354,22 +354,13 @@ void Ntf_ShowMyNotifications (void) sprintf (SubQuery," AND (Status&%u)=0", Ntf_STATUS_BIT_READ | Ntf_STATUS_BIT_REMOVED); - /* - if (asprintf (&Query,"SELECT NotifyEvent,FromUsrCod,InsCod,CtrCod,DegCod,CrsCod," - "Cod,UNIX_TIMESTAMP(TimeNotif),Status" - " FROM notif" - " WHERE ToUsrCod=%ld%s" - " ORDER BY TimeNotif DESC", - Gbl.Usrs.Me.UsrDat.UsrCod,SubQuery) < 0) - Lay_NotEnoughMemoryExit (); - */ - DB_BuildQuery ("SELECT NotifyEvent,FromUsrCod,InsCod,CtrCod,DegCod,CrsCod," - "Cod,UNIX_TIMESTAMP(TimeNotif),Status" - " FROM notif" - " WHERE ToUsrCod=%ld%s" - " ORDER BY TimeNotif DESC", - Gbl.Usrs.Me.UsrDat.UsrCod,SubQuery); - NumNotifications = DB_QuerySELECT_new (&mysql_res,"can not get your notifications"); + NumNotifications = DB_QuerySELECT (&mysql_res,"can not get your notifications", + "SELECT NotifyEvent,FromUsrCod,InsCod,CtrCod,DegCod,CrsCod," + "Cod,UNIX_TIMESTAMP(TimeNotif),Status" + " FROM notif" + " WHERE ToUsrCod=%ld%s" + " ORDER BY TimeNotif DESC", + Gbl.Usrs.Me.UsrDat.UsrCod,SubQuery); /***** Contextual links *****/ fprintf (Gbl.F.Out,"
"); @@ -1188,7 +1179,7 @@ unsigned Ntf_StoreNotifyEventsToAllUsrs (Ntf_NotifyEvent_t NotifyEvent,long Cod) MYSQL_RES *mysql_res; MYSQL_ROW row; unsigned long NumRow; - unsigned long NumRows; + unsigned long NumRows = 0; // Initialized to avoid warning struct UsrData UsrDat; struct Forum ForumSelected; unsigned NumUsrsToBeNotifiedByEMail = 0; @@ -1208,42 +1199,50 @@ unsigned Ntf_StoreNotifyEventsToAllUsrs (Ntf_NotifyEvent_t NotifyEvent,long Cod) case Brw_ADMI_DOC_CRS: case Brw_ADMI_SHR_CRS: case Brw_ADMI_MRK_CRS: // Notify all users in course except me - DB_BuildQuery ("SELECT UsrCod FROM crs_usr" - " WHERE CrsCod=%ld" - " AND UsrCod<>%ld", - Gbl.CurrentCrs.Crs.CrsCod, - Gbl.Usrs.Me.UsrDat.UsrCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " to be notified", + "SELECT UsrCod FROM crs_usr" + " WHERE CrsCod=%ld" + " AND UsrCod<>%ld", + Gbl.CurrentCrs.Crs.CrsCod, + Gbl.Usrs.Me.UsrDat.UsrCod); break; case Brw_ADMI_TCH_CRS: // Notify all teachers in course except me - DB_BuildQuery ("SELECT UsrCod FROM crs_usr" - " WHERE CrsCod=%ld" - " AND UsrCod<>%ld" - " AND Role=%u", // Notify teachers only - Gbl.CurrentCrs.Crs.CrsCod, - Gbl.Usrs.Me.UsrDat.UsrCod, - (unsigned) Rol_TCH); + NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " to be notified", + "SELECT UsrCod FROM crs_usr" + " WHERE CrsCod=%ld" + " AND UsrCod<>%ld" + " AND Role=%u", // Notify teachers only + Gbl.CurrentCrs.Crs.CrsCod, + Gbl.Usrs.Me.UsrDat.UsrCod, + (unsigned) Rol_TCH); break; case Brw_ADMI_DOC_GRP: case Brw_ADMI_SHR_GRP: case Brw_ADMI_MRK_GRP: // Notify all users in group except me - DB_BuildQuery ("SELECT UsrCod FROM crs_grp_usr" - " WHERE crs_grp_usr.GrpCod=%ld" - " AND crs_grp_usr.UsrCod<>%ld", - Gbl.CurrentCrs.Grps.GrpCod, - Gbl.Usrs.Me.UsrDat.UsrCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " to be notified", + "SELECT UsrCod FROM crs_grp_usr" + " WHERE crs_grp_usr.GrpCod=%ld" + " AND crs_grp_usr.UsrCod<>%ld", + Gbl.CurrentCrs.Grps.GrpCod, + Gbl.Usrs.Me.UsrDat.UsrCod); break; case Brw_ADMI_TCH_GRP: // Notify all teachers in group except me - DB_BuildQuery ("SELECT crs_grp_usr.UsrCod" - " FROM crs_grp_usr,crs_grp,crs_grp_types,crs_usr" - " WHERE crs_grp_usr.GrpCod=%ld" - " AND crs_grp_usr.UsrCod<>%ld" - " AND crs_grp_usr.GrpCod=crs_grp.GrpCod" - " AND crs_grp.GrpTypCod=crs_grp_types.GrpTypCod" - " AND crs_grp_types.CrsCod=crs_usr.CrsCod" - " AND crs_usr.Role=%u", // Notify teachers only - Gbl.CurrentCrs.Grps.GrpCod, - Gbl.Usrs.Me.UsrDat.UsrCod, - (unsigned) Rol_TCH); + NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " to be notified", + "SELECT crs_grp_usr.UsrCod" + " FROM crs_grp_usr,crs_grp,crs_grp_types,crs_usr" + " WHERE crs_grp_usr.GrpCod=%ld" + " AND crs_grp_usr.UsrCod<>%ld" + " AND crs_grp_usr.GrpCod=crs_grp.GrpCod" + " AND crs_grp.GrpTypCod=crs_grp_types.GrpTypCod" + " AND crs_grp_types.CrsCod=crs_usr.CrsCod" + " AND crs_usr.Role=%u", // Notify teachers only + Gbl.CurrentCrs.Grps.GrpCod, + Gbl.Usrs.Me.UsrDat.UsrCod, + (unsigned) Rol_TCH); break; default: // This function should not be called in other cases return 0; @@ -1253,28 +1252,32 @@ unsigned Ntf_StoreNotifyEventsToAllUsrs (Ntf_NotifyEvent_t NotifyEvent,long Cod) // 1. If the assignment is available for the whole course ==> get all users enroled in the course except me // 2. If the assignment is available only for some groups ==> get all users who belong to any of the groups except me // Cases 1 and 2 are mutually exclusive, so the union returns the case 1 or 2 - DB_BuildQuery ("(SELECT crs_usr.UsrCod" - " FROM assignments,crs_usr" - " WHERE assignments.AsgCod=%ld" - " AND assignments.AsgCod NOT IN" - " (SELECT AsgCod FROM asg_grp WHERE AsgCod=%ld)" - " AND assignments.CrsCod=crs_usr.CrsCod" - " AND crs_usr.UsrCod<>%ld)" - " UNION " - "(SELECT DISTINCT crs_grp_usr.UsrCod" - " FROM asg_grp,crs_grp_usr" - " WHERE asg_grp.AsgCod=%ld" - " AND asg_grp.GrpCod=crs_grp_usr.GrpCod" - " AND crs_grp_usr.UsrCod<>%ld)", - Cod,Cod,Gbl.Usrs.Me.UsrDat.UsrCod, - Cod,Gbl.Usrs.Me.UsrDat.UsrCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " to be notified", + "(SELECT crs_usr.UsrCod" + " FROM assignments,crs_usr" + " WHERE assignments.AsgCod=%ld" + " AND assignments.AsgCod NOT IN" + " (SELECT AsgCod FROM asg_grp WHERE AsgCod=%ld)" + " AND assignments.CrsCod=crs_usr.CrsCod" + " AND crs_usr.UsrCod<>%ld)" + " UNION " + "(SELECT DISTINCT crs_grp_usr.UsrCod" + " FROM asg_grp,crs_grp_usr" + " WHERE asg_grp.AsgCod=%ld" + " AND asg_grp.GrpCod=crs_grp_usr.GrpCod" + " AND crs_grp_usr.UsrCod<>%ld)", + Cod,Cod,Gbl.Usrs.Me.UsrDat.UsrCod, + Cod,Gbl.Usrs.Me.UsrDat.UsrCod); break; case Ntf_EVENT_EXAM_ANNOUNCEMENT: case Ntf_EVENT_NOTICE: - DB_BuildQuery ("SELECT UsrCod FROM crs_usr" - " WHERE CrsCod=%ld AND UsrCod<>%ld", - Gbl.CurrentCrs.Crs.CrsCod, - Gbl.Usrs.Me.UsrDat.UsrCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " to be notified", + "SELECT UsrCod FROM crs_usr" + " WHERE CrsCod=%ld AND UsrCod<>%ld", + Gbl.CurrentCrs.Crs.CrsCod, + Gbl.Usrs.Me.UsrDat.UsrCod); break; case Ntf_EVENT_ENROLMENT_STD: // This function should not be called in this case case Ntf_EVENT_ENROLMENT_NET: // This function should not be called in this case @@ -1284,13 +1287,15 @@ unsigned Ntf_StoreNotifyEventsToAllUsrs (Ntf_NotifyEvent_t NotifyEvent,long Cod) if (Gbl.CurrentCrs.Crs.NumUsrs[Rol_TCH]) { // If this course has teachers ==> send notification to teachers - DB_BuildQuery ("SELECT UsrCod FROM crs_usr" - " WHERE CrsCod=%ld" - " AND UsrCod<>%ld" - " AND Role=%u", // Notify teachers only - Gbl.CurrentCrs.Crs.CrsCod, - Gbl.Usrs.Me.UsrDat.UsrCod, - (unsigned) Rol_TCH); + NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " to be notified", + "SELECT UsrCod FROM crs_usr" + " WHERE CrsCod=%ld" + " AND UsrCod<>%ld" + " AND Role=%u", // Notify teachers only + Gbl.CurrentCrs.Crs.CrsCod, + Gbl.Usrs.Me.UsrDat.UsrCod, + (unsigned) Rol_TCH); } else // Course without teachers { @@ -1298,26 +1303,31 @@ unsigned Ntf_StoreNotifyEventsToAllUsrs (Ntf_NotifyEvent_t NotifyEvent,long Cod) // and I want to be a teacher (checked before calling this function // to not send requests to be a student to admins) // ==> send notification to administrators or superusers - DB_BuildQuery ("SELECT UsrCod FROM admin" - " WHERE (Scope='%s'" - " OR (Scope='%s' AND Cod=%ld)" - " OR (Scope='%s' AND Cod=%ld)" - " OR (Scope='%s' AND Cod=%ld))" - " AND UsrCod<>%ld", - Sco_ScopeDB[Sco_SCOPE_SYS], - Sco_ScopeDB[Sco_SCOPE_INS],Gbl.CurrentIns.Ins.InsCod, - Sco_ScopeDB[Sco_SCOPE_CTR],Gbl.CurrentCtr.Ctr.CtrCod, - Sco_ScopeDB[Sco_SCOPE_DEG],Gbl.CurrentDeg.Deg.DegCod, - Gbl.Usrs.Me.UsrDat.UsrCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " to be notified", + "SELECT UsrCod FROM admin" + " WHERE (Scope='%s'" + " OR (Scope='%s' AND Cod=%ld)" + " OR (Scope='%s' AND Cod=%ld)" + " OR (Scope='%s' AND Cod=%ld))" + " AND UsrCod<>%ld", + Sco_ScopeDB[Sco_SCOPE_SYS], + Sco_ScopeDB[Sco_SCOPE_INS],Gbl.CurrentIns.Ins.InsCod, + Sco_ScopeDB[Sco_SCOPE_CTR],Gbl.CurrentCtr.Ctr.CtrCod, + Sco_ScopeDB[Sco_SCOPE_DEG],Gbl.CurrentDeg.Deg.DegCod, + Gbl.Usrs.Me.UsrDat.UsrCod); } break; case Ntf_EVENT_TIMELINE_COMMENT: // New comment to one of my social notes or comments // Cod is the code of the social publishing - DB_BuildQuery ("SELECT DISTINCT(PublisherCod) FROM social_pubs" - " WHERE NotCod = (SELECT NotCod FROM social_pubs" - " WHERE PubCod=%ld)" - " AND PublisherCod<>%ld", - Cod,Gbl.Usrs.Me.UsrDat.UsrCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " to be notified", + "SELECT DISTINCT(PublisherCod) FROM social_pubs" + " WHERE NotCod =" + " (SELECT NotCod FROM social_pubs" + " WHERE PubCod=%ld)" + " AND PublisherCod<>%ld", + Cod,Gbl.Usrs.Me.UsrDat.UsrCod); break; case Ntf_EVENT_TIMELINE_FAV: // New favourite to one of my social notes or comments case Ntf_EVENT_TIMELINE_SHARE: // New sharing of one of my social notes @@ -1331,28 +1341,34 @@ unsigned Ntf_StoreNotifyEventsToAllUsrs (Ntf_NotifyEvent_t NotifyEvent,long Cod) switch (ForumSelected.Type) { case For_FORUM_COURSE_USRS: - DB_BuildQuery ("SELECT UsrCod FROM crs_usr" - " WHERE CrsCod=%ld AND UsrCod<>%ld", - Gbl.CurrentCrs.Crs.CrsCod, - Gbl.Usrs.Me.UsrDat.UsrCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " to be notified", + "SELECT UsrCod FROM crs_usr" + " WHERE CrsCod=%ld AND UsrCod<>%ld", + Gbl.CurrentCrs.Crs.CrsCod, + Gbl.Usrs.Me.UsrDat.UsrCod); break; case For_FORUM_COURSE_TCHS: - DB_BuildQuery ("SELECT UsrCod FROM crs_usr" - " WHERE CrsCod=%ld AND Role=%u AND UsrCod<>%ld", - Gbl.CurrentCrs.Crs.CrsCod, - (unsigned) Rol_TCH, - Gbl.Usrs.Me.UsrDat.UsrCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " to be notified", + "SELECT UsrCod FROM crs_usr" + " WHERE CrsCod=%ld AND Role=%u AND UsrCod<>%ld", + Gbl.CurrentCrs.Crs.CrsCod, + (unsigned) Rol_TCH, + Gbl.Usrs.Me.UsrDat.UsrCod); break; default: return 0; } break; case Ntf_EVENT_FORUM_REPLY: - DB_BuildQuery ("SELECT DISTINCT(UsrCod) FROM forum_post" - " WHERE ThrCod = (SELECT ThrCod FROM forum_post" - " WHERE PstCod=%ld)" - " AND UsrCod<>%ld", - Cod,Gbl.Usrs.Me.UsrDat.UsrCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " to be notified", + "SELECT DISTINCT(UsrCod) FROM forum_post" + " WHERE ThrCod = (SELECT ThrCod FROM forum_post" + " WHERE PstCod=%ld)" + " AND UsrCod<>%ld", + Cod,Gbl.Usrs.Me.UsrDat.UsrCod); break; case Ntf_EVENT_MESSAGE: // This function should not be called in this case return 0; @@ -1360,35 +1376,37 @@ unsigned Ntf_StoreNotifyEventsToAllUsrs (Ntf_NotifyEvent_t NotifyEvent,long Cod) // 1. If the survey is available for the whole course ==> get users enroled in the course whose role is available in survey, except me // 2. If the survey is available only for some groups ==> get users who belong to any of the groups and whose role is available in survey, except me // Cases 1 and 2 are mutually exclusive, so the union returns the case 1 or 2 - DB_BuildQuery ("(SELECT crs_usr.UsrCod" - " FROM surveys,crs_usr" - " WHERE surveys.SvyCod=%ld" - " AND surveys.SvyCod NOT IN" - " (SELECT SvyCod FROM svy_grp WHERE SvyCod=%ld)" - " AND surveys.Scope='%s' AND surveys.Cod=crs_usr.CrsCod" - " AND crs_usr.UsrCod<>%ld" - " AND (surveys.Roles&(1<0)" - " UNION " - "(SELECT DISTINCT crs_grp_usr.UsrCod" - " FROM svy_grp,crs_grp_usr,surveys,crs_usr" - " WHERE svy_grp.SvyCod=%ld" - " AND svy_grp.GrpCod=crs_grp_usr.GrpCod" - " AND crs_grp_usr.UsrCod=crs_usr.UsrCod" - " AND crs_grp_usr.UsrCod<>%ld" - " AND svy_grp.SvyCod=surveys.SvyCod" - " AND surveys.Scope='%s' AND surveys.Cod=crs_usr.CrsCod" - " AND (surveys.Roles&(1<0)", - Cod, - Cod, - Sco_ScopeDB[Sco_SCOPE_CRS], - Gbl.Usrs.Me.UsrDat.UsrCod, - Cod, - Gbl.Usrs.Me.UsrDat.UsrCod, - Sco_ScopeDB[Sco_SCOPE_CRS]); + NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " to be notified", + "(SELECT crs_usr.UsrCod" + " FROM surveys,crs_usr" + " WHERE surveys.SvyCod=%ld" + " AND surveys.SvyCod NOT IN" + " (SELECT SvyCod FROM svy_grp WHERE SvyCod=%ld)" + " AND surveys.Scope='%s' AND surveys.Cod=crs_usr.CrsCod" + " AND crs_usr.UsrCod<>%ld" + " AND (surveys.Roles&(1<0)" + " UNION " + "(SELECT DISTINCT crs_grp_usr.UsrCod" + " FROM svy_grp,crs_grp_usr,surveys,crs_usr" + " WHERE svy_grp.SvyCod=%ld" + " AND svy_grp.GrpCod=crs_grp_usr.GrpCod" + " AND crs_grp_usr.UsrCod=crs_usr.UsrCod" + " AND crs_grp_usr.UsrCod<>%ld" + " AND svy_grp.SvyCod=surveys.SvyCod" + " AND surveys.Scope='%s' AND surveys.Cod=crs_usr.CrsCod" + " AND (surveys.Roles&(1<0)", + Cod, + Cod, + Sco_ScopeDB[Sco_SCOPE_CRS], + Gbl.Usrs.Me.UsrDat.UsrCod, + Cod, + Gbl.Usrs.Me.UsrDat.UsrCod, + Sco_ScopeDB[Sco_SCOPE_CRS]); break; } - if ((NumRows = DB_QuerySELECT_new (&mysql_res,"can not get users to be notified"))) // Users found + if (NumRows) // Users found { /***** Initialize structure with user's data *****/ Usr_UsrDataConstructor (&UsrDat); @@ -1516,15 +1534,20 @@ void Ntf_SendPendingNotifByEMailToAllUsrs (void) unsigned NumTotalMails = 0; /***** Get users who must be notified from database ******/ - // (Status & Ntf_STATUS_BIT_EMAIL) && !(Status & Ntf_STATUS_BIT_SENT) && !(Status & (Ntf_STATUS_BIT_READ | Ntf_STATUS_BIT_REMOVED)) - DB_BuildQuery ("SELECT DISTINCT ToUsrCod FROM notif" - " WHERE TimeNotif0 AND (Status & %u)=0 AND (Status & %u)=0", - Cfg_TIME_TO_SEND_PENDING_NOTIF, - (unsigned) Ntf_STATUS_BIT_EMAIL, - (unsigned) Ntf_STATUS_BIT_SENT, - (unsigned) (Ntf_STATUS_BIT_READ | Ntf_STATUS_BIT_REMOVED)); - if ((NumRows = DB_QuerySELECT_new (&mysql_res,"can not get users who must be notified"))) // Events found + // (Status & Ntf_STATUS_BIT_EMAIL) && + // !(Status & Ntf_STATUS_BIT_SENT) && + // !(Status & (Ntf_STATUS_BIT_READ | Ntf_STATUS_BIT_REMOVED)) + if ((NumRows = DB_QuerySELECT (&mysql_res,"can not get users" + " who must be notified", + "SELECT DISTINCT ToUsrCod FROM notif" + " WHERE TimeNotif0" + " AND (Status & %u)=0" + " AND (Status & %u)=0", + Cfg_TIME_TO_SEND_PENDING_NOTIF, + (unsigned) Ntf_STATUS_BIT_EMAIL, + (unsigned) Ntf_STATUS_BIT_SENT, + (unsigned) (Ntf_STATUS_BIT_READ | Ntf_STATUS_BIT_REMOVED)))) // Events found { /***** Initialize structure with user's data *****/ Usr_UsrDataConstructor (&UsrDat); @@ -1606,15 +1629,24 @@ static void Ntf_SendPendingNotifByEMailToOneUsr (struct UsrData *ToUsrDat,unsign if (Mai_CheckIfUsrCanReceiveEmailNotif (ToUsrDat)) { /***** Get pending notifications of this user from database ******/ - DB_BuildQuery ("SELECT NotifyEvent,FromUsrCod,InsCod,CtrCod,DegCod,CrsCod,Cod" - " FROM notif WHERE ToUsrCod=%ld" - " AND (Status & %u)<>0 AND (Status & %u)=0 AND (Status & %u)=0" - " ORDER BY TimeNotif,NotifyEvent", - ToUsrDat->UsrCod, - (unsigned) Ntf_STATUS_BIT_EMAIL, - (unsigned) Ntf_STATUS_BIT_SENT, - (unsigned) (Ntf_STATUS_BIT_READ | Ntf_STATUS_BIT_REMOVED)); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get pending notifications of a user"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get pending notifications" + " of a user", + "SELECT NotifyEvent," + "FromUsrCod," + "InsCod," + "CtrCod," + "DegCod," + "CrsCod," + "Cod" + " FROM notif WHERE ToUsrCod=%ld" + " AND (Status & %u)<>0" + " AND (Status & %u)=0" + " AND (Status & %u)=0" + " ORDER BY TimeNotif,NotifyEvent", + ToUsrDat->UsrCod, + (unsigned) Ntf_STATUS_BIT_EMAIL, + (unsigned) Ntf_STATUS_BIT_SENT, + (unsigned) (Ntf_STATUS_BIT_READ | Ntf_STATUS_BIT_REMOVED)); if (NumRows) // Events found { @@ -1819,10 +1851,12 @@ static void Ntf_GetNumNotifSent (long DegCod,long CrsCod, unsigned long NumRows; /***** Get number of notifications sent by email from database *****/ - DB_BuildQuery ("SELECT NumEvents,NumMails FROM sta_notif" - " WHERE DegCod=%ld AND CrsCod=%ld AND NotifyEvent=%u", - DegCod,CrsCod,(unsigned) NotifyEvent); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get number of notifications sent by email"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get number of notifications" + " sent by email", + "SELECT NumEvents,NumMails FROM sta_notif" + " WHERE DegCod=%ld AND CrsCod=%ld" + " AND NotifyEvent=%u", + DegCod,CrsCod,(unsigned) NotifyEvent); /***** Get number of rows *****/ if (NumRows) diff --git a/swad_pagination.c b/swad_pagination.c index 13c5e12ef..376748cf0 100644 --- a/swad_pagination.c +++ b/swad_pagination.c @@ -820,11 +820,12 @@ unsigned Pag_GetLastPageMsgFromSession (Pag_WhatPaginate_t WhatPaginate) unsigned NumPage; /***** Get last page of received/sent messages from database *****/ - DB_BuildQuery ("SELECT %s FROM sessions WHERE SessionId='%s'", - WhatPaginate == Pag_MESSAGES_RECEIVED ? "LastPageMsgRcv" : - "LastPageMsgSnt", - Gbl.Session.Id); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get last page of messages"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get last page of messages", + "SELECT %s FROM sessions" + " WHERE SessionId='%s'", + WhatPaginate == Pag_MESSAGES_RECEIVED ? "LastPageMsgRcv" : + "LastPageMsgSnt", + Gbl.Session.Id); /***** Check number of rows of the result ****/ if (NumRows != 1) diff --git a/swad_password.c b/swad_password.c index 3b28cc650..6911b78e8 100644 --- a/swad_password.c +++ b/swad_password.c @@ -118,10 +118,10 @@ bool Pwd_CheckPendingPassword (void) MYSQL_ROW row; /***** Get pending password from database *****/ - DB_BuildQuery ("SELECT PendingPassword FROM pending_passwd" - " WHERE UsrCod=%ld", - Gbl.Usrs.Me.UsrDat.UsrCod); - if (DB_QuerySELECT_new (&mysql_res,"can not get pending password")) + if (DB_QuerySELECT (&mysql_res,"can not get pending password", + "SELECT PendingPassword FROM pending_passwd" + " WHERE UsrCod=%ld", + Gbl.Usrs.Me.UsrDat.UsrCod)) { /* Get encrypted pending password */ row = mysql_fetch_row (mysql_res); diff --git a/swad_photo.c b/swad_photo.c index 0b7e621d9..d38b17298 100644 --- a/swad_photo.c +++ b/swad_photo.c @@ -121,7 +121,7 @@ static void Pho_PutLinkToCalculateDegreeStats (void); static void Pho_GetMaxStdsPerDegree (void); static void Pho_ShowOrPrintClassPhotoDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint); static void Pho_ShowOrPrintListDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint); -static void Pho_BuildQueryOfDegrees (void); +static unsigned long Pho_BuildQueryOfDegrees (MYSQL_RES **mysql_res); 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_ShowDegreeStat (int NumStds,int NumStdsWithPhoto); @@ -983,10 +983,11 @@ unsigned Pho_UpdateMyClicksWithoutPhoto (void) unsigned NumClicks; /***** Get number of clicks without photo from database *****/ - DB_BuildQuery ("SELECT NumClicks FROM clicks_without_photo" - " WHERE UsrCod=%ld", - Gbl.Usrs.Me.UsrDat.UsrCod); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get number of clicks without photo"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get number of clicks" + " without photo", + "SELECT NumClicks FROM clicks_without_photo" + " WHERE UsrCod=%ld", + Gbl.Usrs.Me.UsrDat.UsrCod); /***** Update the list of clicks without photo *****/ if (NumRows) // The user exists ==> update number of clicks without photo @@ -1435,16 +1436,16 @@ static long Pho_GetDegWithAvgPhotoLeastRecentlyUpdated (void) /***** 1. If a degree is not in table of computed degrees, choose it as least recently updated *****/ /* Get one degree with students not yet computed */ - DB_BuildQuery ("SELECT DISTINCT degrees.DegCod" - " FROM degrees,courses,crs_usr" - " WHERE degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=crs_usr.CrsCod" - " AND crs_usr.Role=%u" - " AND degrees.DegCod NOT IN" - " (SELECT DISTINCT DegCod FROM sta_degrees)" - " LIMIT 1", - (unsigned) Rol_STD); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get degrees"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get degrees", + "SELECT DISTINCT degrees.DegCod" + " FROM degrees,courses,crs_usr" + " WHERE degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=crs_usr.CrsCod" + " AND crs_usr.Role=%u" + " AND degrees.DegCod NOT IN" + " (SELECT DISTINCT DegCod FROM sta_degrees)" + " LIMIT 1", + (unsigned) Rol_STD); /* If number of rows is 1, then get the degree code */ if (NumRows == 1) @@ -1464,16 +1465,17 @@ static long Pho_GetDegWithAvgPhotoLeastRecentlyUpdated (void) /***** 2. If all the degrees are in table, choose the least recently updated that has students *****/ /* Get degrees from database */ - DB_BuildQuery ("SELECT sta_degrees.DegCod" - " FROM sta_degrees,courses,crs_usr" - " WHERE sta_degrees.TimeAvgPhoto0"); - NumRows = DB_QuerySELECT_new (&mysql_res, - "can not get maximum number of students in a degree"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get maximum" + " number of students in a degree", + "SELECT MAX(NumStds),MAX(NumStdsWithPhoto)," + "MAX(NumStdsWithPhoto/NumStds)" + " FROM sta_degrees" + " WHERE Sex='all' AND NumStds>0"); /***** Count number of rows in result *****/ if (NumRows == 1) @@ -2115,8 +2118,8 @@ static void Pho_ShowOrPrintClassPhotoDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrin { MYSQL_RES *mysql_res; MYSQL_ROW row; - unsigned long NumRow; - unsigned long NumRows; + unsigned long NumDeg; + unsigned long NumDegs; struct Degree Deg; unsigned NumDegsNotEmpty; int NumStds; @@ -2124,10 +2127,9 @@ static void Pho_ShowOrPrintClassPhotoDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrin bool TRIsOpen = false; /***** Get degrees from database *****/ - Pho_BuildQueryOfDegrees (); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get degrees"); + NumDegs = Pho_BuildQueryOfDegrees (&mysql_res); - if (NumRows) // Degrees with students found + if (NumDegs) // Degrees with students found { /***** Form to select type of list used to display degree photos *****/ if (SeeOrPrint == Pho_DEGREES_SEE) @@ -2135,9 +2137,9 @@ static void Pho_ShowOrPrintClassPhotoDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrin Tbl_StartTableCenter (0); /***** Get and print degrees *****/ - for (NumRow = 0, NumDegsNotEmpty = 0; - NumRow < NumRows; - NumRow++) + for (NumDeg = 0, NumDegsNotEmpty = 0; + NumDeg < NumDegs; + NumDeg++) { /***** Get next degree *****/ row = mysql_fetch_row (mysql_res); @@ -2196,8 +2198,8 @@ static void Pho_ShowOrPrintListDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint) extern const char *Txt_SEX_PLURAL_Abc[Usr_NUM_SEXS]; MYSQL_RES *mysql_res; MYSQL_ROW row; - unsigned long NumRow; - unsigned long NumRows; + unsigned long NumDeg; + unsigned long NumDegs; unsigned NumDegsNotEmpty; int NumStds; int NumStdsWithPhoto; @@ -2205,10 +2207,9 @@ static void Pho_ShowOrPrintListDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint) Usr_Sex_t Sex; /***** Get degrees from database *****/ - Pho_BuildQueryOfDegrees (); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get degrees"); + NumDegs = Pho_BuildQueryOfDegrees (&mysql_res); - if (NumRows) // Degrees with students found + if (NumDegs) // Degrees with students found { /***** Class photo start *****/ if (SeeOrPrint == Pho_DEGREES_SEE) @@ -2236,9 +2237,9 @@ static void Pho_ShowOrPrintListDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint) fprintf (Gbl.F.Out,""); /***** Get degrees *****/ - for (NumRow = 0, Gbl.RowEvenOdd = 0, NumDegsNotEmpty = 0; - NumRow < NumRows; - NumRow++, Gbl.RowEvenOdd = 1 - Gbl.RowEvenOdd) + for (NumDeg = 0, Gbl.RowEvenOdd = 0, NumDegsNotEmpty = 0; + NumDeg < NumDegs; + NumDeg++, Gbl.RowEvenOdd = 1 - Gbl.RowEvenOdd) { /***** Get next degree *****/ row = mysql_fetch_row (mysql_res); @@ -2302,48 +2303,57 @@ static void Pho_ShowOrPrintListDegrees (Pho_AvgPhotoSeeOrPrint_t SeeOrPrint) /****** Build a query to get the degrees ordered by different criteria *******/ /*****************************************************************************/ -static void Pho_BuildQueryOfDegrees (void) +static unsigned long Pho_BuildQueryOfDegrees (MYSQL_RES **mysql_res) { + unsigned long NumDegs = 0; // Initialized to avoid warning + switch (Gbl.Stat.DegPhotos.HowOrderDegrees) { case Pho_NUMBER_OF_STUDENTS: - DB_BuildQuery ("SELECT degrees.DegCod" - " FROM degrees,sta_degrees" - " WHERE sta_degrees.Sex='all'" - " AND sta_degrees.NumStds>0" - " AND degrees.DegCod=sta_degrees.DegCod" - " ORDER BY sta_degrees.NumStds DESC," - "sta_degrees.NumStdsWithPhoto DESC," - "degrees.ShortName"); + NumDegs = DB_QuerySELECT (mysql_res,"can not get degrees", + "SELECT degrees.DegCod" + " FROM degrees,sta_degrees" + " WHERE sta_degrees.Sex='all'" + " AND sta_degrees.NumStds>0" + " AND degrees.DegCod=sta_degrees.DegCod" + " ORDER BY sta_degrees.NumStds DESC," + "sta_degrees.NumStdsWithPhoto DESC," + "degrees.ShortName"); break; case Pho_NUMBER_OF_PHOTOS: - DB_BuildQuery ("SELECT degrees.DegCod" - " FROM degrees,sta_degrees" - " WHERE sta_degrees.Sex='all'" - " AND sta_degrees.NumStds>0" - " AND degrees.DegCod=sta_degrees.DegCod" - " ORDER BY sta_degrees.NumStdsWithPhoto DESC," - "sta_degrees.NumStds DESC," - "degrees.ShortName"); + NumDegs = DB_QuerySELECT (mysql_res,"can not get degrees", + "SELECT degrees.DegCod" + " FROM degrees,sta_degrees" + " WHERE sta_degrees.Sex='all'" + " AND sta_degrees.NumStds>0" + " AND degrees.DegCod=sta_degrees.DegCod" + " ORDER BY sta_degrees.NumStdsWithPhoto DESC," + "sta_degrees.NumStds DESC," + "degrees.ShortName"); break; case Pho_PERCENT: - DB_BuildQuery ("SELECT degrees.DegCod" - " FROM degrees,sta_degrees" - " WHERE sta_degrees.Sex='all'" - " AND sta_degrees.NumStds>0" - " AND degrees.DegCod=sta_degrees.DegCod" - " ORDER BY sta_degrees.NumStdsWithPhoto/sta_degrees.NumStds DESC," - "degrees.ShortName"); + NumDegs = DB_QuerySELECT (mysql_res,"can not get degrees", + "SELECT degrees.DegCod" + " FROM degrees,sta_degrees" + " WHERE sta_degrees.Sex='all'" + " AND sta_degrees.NumStds>0" + " AND degrees.DegCod=sta_degrees.DegCod" + " ORDER BY sta_degrees.NumStdsWithPhoto/" + "sta_degrees.NumStds DESC," + "degrees.ShortName"); break; case Pho_DEGREE_NAME: - DB_BuildQuery ("SELECT degrees.DegCod" - " FROM degrees,sta_degrees" - " WHERE sta_degrees.Sex='all'" - " AND sta_degrees.NumStds>0" - " AND degrees.DegCod=sta_degrees.DegCod" - " ORDER BY degrees.ShortName"); + NumDegs = DB_QuerySELECT (mysql_res,"can not get degrees", + "SELECT degrees.DegCod" + " FROM degrees,sta_degrees" + " WHERE sta_degrees.Sex='all'" + " AND sta_degrees.NumStds>0" + " AND degrees.DegCod=sta_degrees.DegCod" + " ORDER BY degrees.ShortName"); break; } + + return NumDegs; } /*****************************************************************************/ @@ -2358,10 +2368,12 @@ static void Pho_GetNumStdsInDegree (long DegCod,Usr_Sex_t Sex,int *NumStds,int * unsigned long NumRows; /***** Get the number of students in a degree from database *****/ - DB_BuildQuery ("SELECT NumStds,NumStdsWithPhoto FROM sta_degrees" - " WHERE DegCod=%ld AND Sex='%s'", - DegCod,Usr_StringsSexDB[Sex]); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get the number of students in a degree"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get the number of students" + " in a degree", + "SELECT NumStds,NumStdsWithPhoto" + " FROM sta_degrees" + " WHERE DegCod=%ld AND Sex='%s'", + DegCod,Usr_StringsSexDB[Sex]); if (NumRows == 0) *NumStds = *NumStdsWithPhoto = -1;