From c1559c1d52f6e840989779de4f30a73ccee2ff35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ca=C3=B1as=20Vargas?= Date: Tue, 30 Oct 2018 13:59:37 +0100 Subject: [PATCH] Version 18.11.1 --- swad_attendance.c | 244 ++++++++++++++++++++++++---------------------- swad_banner.c | 132 +++++++++++++------------ swad_centre.c | 166 +++++++++++++++---------------- swad_centre.h | 4 +- swad_changelog.h | 3 +- swad_chat.c | 16 +-- swad_search.c | 22 +++-- 7 files changed, 307 insertions(+), 280 deletions(-) diff --git a/swad_attendance.c b/swad_attendance.c index f0140f3b..170c29be 100644 --- a/swad_attendance.c +++ b/swad_attendance.c @@ -643,23 +643,24 @@ static void Att_GetListAttEvents (Att_OrderTime_t Order) break; } if (Gbl.CurrentCrs.Grps.WhichGrps == Grp_ONLY_MY_GROUPS) - DB_BuildQuery ("SELECT AttCod" - " FROM att_events" - " WHERE CrsCod=%ld%s" - " AND (AttCod NOT IN (SELECT AttCod FROM att_grp) OR" - " AttCod IN (SELECT att_grp.AttCod FROM att_grp,crs_grp_usr" - " WHERE crs_grp_usr.UsrCod=%ld" - " AND att_grp.GrpCod=crs_grp_usr.GrpCod))" - " ORDER BY %s", - Gbl.CurrentCrs.Crs.CrsCod,HiddenSubQuery, - Gbl.Usrs.Me.UsrDat.UsrCod,OrderBySubQuery); + NumRows = DB_QuerySELECT (&mysql_res,"can not get attendance events", + "SELECT AttCod" + " FROM att_events" + " WHERE CrsCod=%ld%s" + " AND (AttCod NOT IN (SELECT AttCod FROM att_grp) OR" + " AttCod IN (SELECT att_grp.AttCod FROM att_grp,crs_grp_usr" + " WHERE crs_grp_usr.UsrCod=%ld" + " AND att_grp.GrpCod=crs_grp_usr.GrpCod))" + " ORDER BY %s", + Gbl.CurrentCrs.Crs.CrsCod,HiddenSubQuery, + Gbl.Usrs.Me.UsrDat.UsrCod,OrderBySubQuery); else // Gbl.CurrentCrs.Grps.WhichGrps == Grp_ALL_GROUPS - DB_BuildQuery ("SELECT AttCod" - " FROM att_events" - " WHERE CrsCod=%ld%s" - " ORDER BY %s", - Gbl.CurrentCrs.Crs.CrsCod,HiddenSubQuery,OrderBySubQuery); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get attendance events"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get attendance events", + "SELECT AttCod" + " FROM att_events" + " WHERE CrsCod=%ld%s" + " ORDER BY %s", + Gbl.CurrentCrs.Crs.CrsCod,HiddenSubQuery,OrderBySubQuery); /* Free allocated memory for subqueries */ free ((void *) OrderBySubQuery); @@ -718,6 +719,7 @@ bool Att_GetDataOfAttEventByCod (struct AttendanceEvent *Att) { MYSQL_RES *mysql_res; MYSQL_ROW row; + unsigned long NumRows; bool Found = false; /***** Reset attendance event data *****/ @@ -726,18 +728,19 @@ bool Att_GetDataOfAttEventByCod (struct AttendanceEvent *Att) if (Att->AttCod > 0) { /***** Build query *****/ - DB_BuildQuery ("SELECT AttCod,CrsCod,Hidden,UsrCod," - "UNIX_TIMESTAMP(StartTime)," - "UNIX_TIMESTAMP(EndTime)," - "NOW() BETWEEN StartTime AND EndTime," - "CommentTchVisible," - "Title" - " FROM att_events" - " WHERE AttCod=%ld", - Att->AttCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get attendance event data", + "SELECT AttCod,CrsCod,Hidden,UsrCod," + "UNIX_TIMESTAMP(StartTime)," + "UNIX_TIMESTAMP(EndTime)," + "NOW() BETWEEN StartTime AND EndTime," + "CommentTchVisible," + "Title" + " FROM att_events" + " WHERE AttCod=%ld", + Att->AttCod); /***** Get data of attendance event from database *****/ - if ((Found = (DB_QuerySELECT_new (&mysql_res,"can not get attendance event data") != 0))) // Attendance event found... + if ((Found = (NumRows != 0))) // Attendance event found... { /* Get row */ row = mysql_fetch_row (mysql_res); @@ -828,10 +831,10 @@ static void Att_GetAttEventTxtFromDB (long AttCod,char Txt[Cns_MAX_BYTES_TEXT + unsigned long NumRows; /***** Get text of attendance event from database *****/ - DB_BuildQuery ("SELECT Txt FROM att_events" - " WHERE AttCod=%ld AND CrsCod=%ld", - AttCod,Gbl.CurrentCrs.Crs.CrsCod); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get attendance event text"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get attendance event text", + "SELECT Txt FROM att_events" + " WHERE AttCod=%ld AND CrsCod=%ld", + AttCod,Gbl.CurrentCrs.Crs.CrsCod); /***** The result of the query must have one row or none *****/ if (NumRows == 1) @@ -1507,14 +1510,14 @@ static void Att_GetAndWriteNamesOfGrpsAssociatedToAttEvent (struct AttendanceEve unsigned NumGrps; /***** Get groups associated to an attendance event from database *****/ - DB_BuildQuery ("SELECT crs_grp_types.GrpTypName,crs_grp.GrpName" - " FROM att_grp,crs_grp,crs_grp_types" - " WHERE att_grp.AttCod=%ld" - " AND att_grp.GrpCod=crs_grp.GrpCod" - " AND crs_grp.GrpTypCod=crs_grp_types.GrpTypCod" - " ORDER BY crs_grp_types.GrpTypName,crs_grp.GrpName", - Att->AttCod); - NumGrps = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get groups of an attendance event"); + NumGrps = (unsigned) DB_QuerySELECT (&mysql_res,"can not get groups of an attendance event", + "SELECT crs_grp_types.GrpTypName,crs_grp.GrpName" + " FROM att_grp,crs_grp,crs_grp_types" + " WHERE att_grp.AttCod=%ld" + " AND att_grp.GrpCod=crs_grp.GrpCod" + " AND crs_grp.GrpTypCod=crs_grp_types.GrpTypCod" + " ORDER BY crs_grp_types.GrpTypName,crs_grp.GrpName", + Att->AttCod); /***** Write heading *****/ fprintf (Gbl.F.Out,"
%s: ", @@ -1657,48 +1660,52 @@ unsigned Att_GetNumCoursesWithAttEvents (Sco_Scope_t Scope) switch (Scope) { case Sco_SCOPE_SYS: - DB_BuildQuery ("SELECT COUNT(DISTINCT CrsCod)" - " FROM att_events" - " WHERE CrsCod>0"); + DB_QuerySELECT (&mysql_res,"can not get number of courses with attendance events", + "SELECT COUNT(DISTINCT CrsCod)" + " FROM att_events" + " WHERE CrsCod>0"); break; case Sco_SCOPE_INS: - DB_BuildQuery ("SELECT COUNT(DISTINCT att_events.CrsCod)" - " FROM centres,degrees,courses,att_events" - " WHERE centres.InsCod=%ld" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.Status=0" - " AND courses.CrsCod=att_events.CrsCod", - Gbl.CurrentIns.Ins.InsCod); + DB_QuerySELECT (&mysql_res,"can not get number of courses with attendance events", + "SELECT COUNT(DISTINCT att_events.CrsCod)" + " FROM centres,degrees,courses,att_events" + " WHERE centres.InsCod=%ld" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.Status=0" + " AND courses.CrsCod=att_events.CrsCod", + Gbl.CurrentIns.Ins.InsCod); break; case Sco_SCOPE_CTR: - DB_BuildQuery ("SELECT COUNT(DISTINCT att_events.CrsCod)" - " FROM degrees,courses,att_events" - " WHERE degrees.CtrCod=%ld" - " AND degrees.DegCod=courses.DegCod" - " AND courses.Status=0" - " AND courses.CrsCod=att_events.CrsCod", - Gbl.CurrentCtr.Ctr.CtrCod); + DB_QuerySELECT (&mysql_res,"can not get number of courses with attendance events", + "SELECT COUNT(DISTINCT att_events.CrsCod)" + " FROM degrees,courses,att_events" + " WHERE degrees.CtrCod=%ld" + " AND degrees.DegCod=courses.DegCod" + " AND courses.Status=0" + " AND courses.CrsCod=att_events.CrsCod", + Gbl.CurrentCtr.Ctr.CtrCod); break; case Sco_SCOPE_DEG: - DB_BuildQuery ("SELECT COUNT(DISTINCT att_events.CrsCod)" - " FROM courses,att_events" - " WHERE courses.DegCod=%ld" - " AND courses.Status=0" - " AND courses.CrsCod=att_events.CrsCod", - Gbl.CurrentDeg.Deg.DegCod); + DB_QuerySELECT (&mysql_res,"can not get number of courses with attendance events", + "SELECT COUNT(DISTINCT att_events.CrsCod)" + " FROM courses,att_events" + " WHERE courses.DegCod=%ld" + " AND courses.Status=0" + " AND courses.CrsCod=att_events.CrsCod", + Gbl.CurrentDeg.Deg.DegCod); break; case Sco_SCOPE_CRS: - DB_BuildQuery ("SELECT COUNT(DISTINCT CrsCod)" - " FROM att_events" - " WHERE CrsCod=%ld", - Gbl.CurrentCrs.Crs.CrsCod); + DB_QuerySELECT (&mysql_res,"can not get number of courses with attendance events", + "SELECT COUNT(DISTINCT CrsCod)" + " FROM att_events" + " WHERE CrsCod=%ld", + Gbl.CurrentCrs.Crs.CrsCod); break; default: Lay_WrongScopeExit (); break; } - DB_QuerySELECT_new (&mysql_res,"can not get number of courses with attendance events"); /***** Get number of courses *****/ row = mysql_fetch_row (mysql_res); @@ -1727,45 +1734,49 @@ unsigned Att_GetNumAttEvents (Sco_Scope_t Scope,unsigned *NumNotif) switch (Scope) { case Sco_SCOPE_SYS: - DB_BuildQuery ("SELECT COUNT(*),SUM(NumNotif)" - " FROM att_events" - " WHERE CrsCod>0"); + DB_QuerySELECT (&mysql_res,"can not get number of attendance events", + "SELECT COUNT(*),SUM(NumNotif)" + " FROM att_events" + " WHERE CrsCod>0"); break; case Sco_SCOPE_INS: - DB_BuildQuery ("SELECT COUNT(*),SUM(att_events.NumNotif)" - " FROM centres,degrees,courses,att_events" - " WHERE centres.InsCod=%ld" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=att_events.CrsCod", - Gbl.CurrentIns.Ins.InsCod); + DB_QuerySELECT (&mysql_res,"can not get number of attendance events", + "SELECT COUNT(*),SUM(att_events.NumNotif)" + " FROM centres,degrees,courses,att_events" + " WHERE centres.InsCod=%ld" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=att_events.CrsCod", + Gbl.CurrentIns.Ins.InsCod); break; case Sco_SCOPE_CTR: - DB_BuildQuery ("SELECT COUNT(*),SUM(att_events.NumNotif)" - " FROM degrees,courses,att_events" - " WHERE degrees.CtrCod=%ld" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=att_events.CrsCod", - Gbl.CurrentCtr.Ctr.CtrCod); + DB_QuerySELECT (&mysql_res,"can not get number of attendance events", + "SELECT COUNT(*),SUM(att_events.NumNotif)" + " FROM degrees,courses,att_events" + " WHERE degrees.CtrCod=%ld" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=att_events.CrsCod", + Gbl.CurrentCtr.Ctr.CtrCod); break; case Sco_SCOPE_DEG: - DB_BuildQuery ("SELECT COUNT(*),SUM(att_events.NumNotif)" - " FROM courses,att_events" - " WHERE courses.DegCod=%ld" - " AND courses.CrsCod=att_events.CrsCod", - Gbl.CurrentDeg.Deg.DegCod); + DB_QuerySELECT (&mysql_res,"can not get number of attendance events", + "SELECT COUNT(*),SUM(att_events.NumNotif)" + " FROM courses,att_events" + " WHERE courses.DegCod=%ld" + " AND courses.CrsCod=att_events.CrsCod", + Gbl.CurrentDeg.Deg.DegCod); break; case Sco_SCOPE_CRS: - DB_BuildQuery ("SELECT COUNT(*),SUM(NumNotif)" - " FROM att_events" - " WHERE CrsCod=%ld", - Gbl.CurrentCrs.Crs.CrsCod); + DB_QuerySELECT (&mysql_res,"can not get number of attendance events", + "SELECT COUNT(*),SUM(NumNotif)" + " FROM att_events" + " WHERE CrsCod=%ld", + Gbl.CurrentCrs.Crs.CrsCod); break; default: Lay_WrongScopeExit (); break; } - DB_QuerySELECT_new (&mysql_res,"can not get number of attendance events"); /***** Get number of attendance events *****/ row = mysql_fetch_row (mysql_res); @@ -2177,12 +2188,10 @@ static void Att_PutParamsCodGrps (long AttCod) /***** Get groups associated to an attendance event from database *****/ if (Gbl.CurrentCrs.Grps.NumGrps) - { - DB_BuildQuery ("SELECT GrpCod FROM att_grp" - " WHERE att_grp.AttCod=%ld", - AttCod); - NumGrps = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get groups of an attendance event"); - } + NumGrps = (unsigned) DB_QuerySELECT (&mysql_res,"can not get groups of an attendance event", + "SELECT GrpCod FROM att_grp" + " WHERE att_grp.AttCod=%ld", + AttCod); else NumGrps = 0; @@ -2465,14 +2474,17 @@ static bool Att_CheckIfUsrIsInTableAttUsr (long AttCod,long UsrCod,bool *Present { MYSQL_RES *mysql_res; MYSQL_ROW row; - unsigned NumRows; + unsigned long NumRows; bool InDBTable; /***** Check if a student is registered in an event in database *****/ - DB_BuildQuery ("SELECT Present FROM att_usr" - " WHERE AttCod=%ld AND UsrCod=%ld", - AttCod,UsrCod); - if ((NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get if a student is already registered in an event"))) + NumRows = DB_QuerySELECT (&mysql_res,"can not get if a student" + " is already registered" + " in an event", + "SELECT Present FROM att_usr" + " WHERE AttCod=%ld AND UsrCod=%ld", + AttCod,UsrCod); + if (NumRows) { InDBTable = true; @@ -2517,14 +2529,18 @@ static bool Att_CheckIfUsrIsPresentInAttEventAndGetComments (long AttCod,long Us { MYSQL_RES *mysql_res; MYSQL_ROW row; - unsigned NumRows; + unsigned long NumRows; bool Present; /***** Check if a students is registered in an event in database *****/ - DB_BuildQuery ("SELECT Present,CommentStd,CommentTch FROM att_usr" - " WHERE AttCod=%ld AND UsrCod=%ld", - AttCod,UsrCod); - if ((NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get if a student is already registered in an event"))) + NumRows = DB_QuerySELECT (&mysql_res,"can not get if a student" + " is already registered" + " in an event", + "SELECT Present,CommentStd,CommentTch" + " FROM att_usr" + " WHERE AttCod=%ld AND UsrCod=%ld", + AttCod,UsrCod); + if (NumRows) { /* Get row */ row = mysql_fetch_row (mysql_res); @@ -2998,11 +3014,11 @@ static void Att_GetListSelectedAttCods (char **StrAttCodsSelected) else // No students attended to this event { /***** Get groups associated to an attendance event from database *****/ - DB_BuildQuery ("SELECT GrpCod FROM att_grp" - " WHERE att_grp.AttCod=%ld", - Gbl.AttEvents.Lst[NumAttEvent].AttCod); - NumGrpsInThisEvent = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get groups of an attendance event"); - + NumGrpsInThisEvent = (unsigned) DB_QuerySELECT (&mysql_res,"can not get groups" + " of an attendance event", + "SELECT GrpCod FROM att_grp" + " WHERE att_grp.AttCod=%ld", + Gbl.AttEvents.Lst[NumAttEvent].AttCod); if (NumGrpsInThisEvent) // This event is associated to groups /* Get groups associated to this event */ for (NumGrpInThisEvent = 0; diff --git a/swad_banner.c b/swad_banner.c index 320db16d..4f84df57 100644 --- a/swad_banner.c +++ b/swad_banner.c @@ -65,7 +65,7 @@ extern struct Globals Gbl; static void Ban_WriteListOfBanners (void); static void Ban_PutFormToEditBanners (void); -static void Ban_GetListBanners (void); +static void Ban_GetListBanners (MYSQL_RES **mysql_res,unsigned long NumRows); static void Ban_PutIconToViewBanners (void); static void Ban_ListBannersForEdition (void); static void Ban_PutParamBanCod (long BanCod); @@ -104,6 +104,8 @@ void Ban_SeeBanners (void) extern const char *Txt_Banners; extern const char *Txt_No_banners; extern const char *Txt_New_banner; + MYSQL_RES *mysql_res; + unsigned long NumRows; /***** Put contextual links *****/ fprintf (Gbl.F.Out,"
"); @@ -114,11 +116,12 @@ void Ban_SeeBanners (void) fprintf (Gbl.F.Out,"
"); /***** Get list of banners *****/ - DB_BuildQuery ("SELECT BanCod,Hidden,ShortName,FullName,Img,WWW" - " FROM banners" - " WHERE Hidden='N'" - " ORDER BY ShortName"); - Ban_GetListBanners (); + NumRows = DB_QuerySELECT (&mysql_res,"can not get banners", + "SELECT BanCod,Hidden,ShortName,FullName,Img,WWW" + " FROM banners" + " WHERE Hidden='N'" + " ORDER BY ShortName"); + Ban_GetListBanners (&mysql_res,NumRows); /***** Start box *****/ Box_StartBox (NULL,Txt_Banners,Ban_PutFormToEditBanners, @@ -197,6 +200,8 @@ void Ban_EditBanners (void) { extern const char *Hlp_SYSTEM_Banners_edit; extern const char *Txt_Banners; + MYSQL_RES *mysql_res; + unsigned long NumRows; /***** Put contextual links *****/ fprintf (Gbl.F.Out,"
"); @@ -207,9 +212,10 @@ void Ban_EditBanners (void) fprintf (Gbl.F.Out,"
"); /***** Get list of banners *****/ - DB_BuildQuery ("SELECT BanCod,Hidden,ShortName,FullName,Img,WWW" - " FROM banners ORDER BY ShortName"); - Ban_GetListBanners (); + NumRows = DB_QuerySELECT (&mysql_res,"can not get banners", + "SELECT BanCod,Hidden,ShortName,FullName,Img,WWW" + " FROM banners ORDER BY ShortName"); + Ban_GetListBanners (&mysql_res,NumRows); /***** Start box *****/ Box_StartBox (NULL,Txt_Banners,Ban_PutIconToViewBanners, @@ -233,67 +239,61 @@ void Ban_EditBanners (void) /**************************** List all the banners ***************************/ /*****************************************************************************/ -static void Ban_GetListBanners (void) +static void Ban_GetListBanners (MYSQL_RES **mysql_res,unsigned long NumRows) { - MYSQL_RES *mysql_res; MYSQL_ROW row; - unsigned long NumRows; unsigned NumBan; struct Banner *Ban; - if (Gbl.DB.DatabaseIsOpen) + /***** Get banners from database *****/ + if (NumRows) // Banners found... { - /***** Get banners from database *****/ - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get banners"); + Gbl.Banners.Num = (unsigned) NumRows; - if (NumRows) // Banners found... + /***** Create list with banners *****/ + if ((Gbl.Banners.Lst = (struct Banner *) + calloc (NumRows,sizeof (struct Banner))) == NULL) + Lay_NotEnoughMemoryExit (); + + /***** Get the banners *****/ + for (NumBan = 0; + NumBan < Gbl.Banners.Num; + NumBan++) { - Gbl.Banners.Num = (unsigned) NumRows; + Ban = &(Gbl.Banners.Lst[NumBan]); - /***** Create list with banners *****/ - if ((Gbl.Banners.Lst = (struct Banner *) calloc (NumRows,sizeof (struct Banner))) == NULL) - Lay_NotEnoughMemoryExit (); + /* Get next banner */ + row = mysql_fetch_row (*mysql_res); - /***** Get the banners *****/ - for (NumBan = 0; - NumBan < Gbl.Banners.Num; - NumBan++) - { - Ban = &(Gbl.Banners.Lst[NumBan]); + /* Get banner code (row[0]) */ + if ((Ban->BanCod = Str_ConvertStrCodToLongCod (row[0])) < 0) + Lay_ShowErrorAndExit ("Wrong code of banner."); - /* Get next banner */ - row = mysql_fetch_row (mysql_res); + /* Get if banner is hidden (row[1]) */ + Ban->Hidden = (row[1][0] == 'Y'); - /* Get banner code (row[0]) */ - if ((Ban->BanCod = Str_ConvertStrCodToLongCod (row[0])) < 0) - Lay_ShowErrorAndExit ("Wrong code of banner."); + /* Get the short name of the banner (row[2]) */ + Str_Copy (Ban->ShrtName,row[2], + Ban_MAX_BYTES_SHRT_NAME); - /* Get if banner is hidden (row[1]) */ - Ban->Hidden = (row[1][0] == 'Y'); + /* Get the full name of the banner (row[3]) */ + Str_Copy (Ban->FullName,row[3], + Ban_MAX_BYTES_FULL_NAME); - /* Get the short name of the banner (row[2]) */ - Str_Copy (Ban->ShrtName,row[2], - Ban_MAX_BYTES_SHRT_NAME); + /* Get the image of the banner (row[4]) */ + Str_Copy (Ban->Img,row[4], + Ban_MAX_BYTES_IMAGE); - /* Get the full name of the banner (row[3]) */ - Str_Copy (Ban->FullName,row[3], - Ban_MAX_BYTES_FULL_NAME); - - /* Get the image of the banner (row[4]) */ - Str_Copy (Ban->Img,row[4], - Ban_MAX_BYTES_IMAGE); - - /* Get the URL of the banner (row[5]) */ - Str_Copy (Ban->WWW,row[5], - Cns_MAX_BYTES_WWW); - } + /* Get the URL of the banner (row[5]) */ + Str_Copy (Ban->WWW,row[5], + Cns_MAX_BYTES_WWW); } - else - Gbl.Banners.Num = 0; - - /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); } + else + Gbl.Banners.Num = 0; + + /***** Free structure that stores the query result *****/ + DB_FreeMySQLResult (mysql_res); } /*****************************************************************************/ @@ -314,11 +314,10 @@ void Ban_GetDataOfBannerByCod (struct Banner *Ban) if (Ban->BanCod > 0) { /***** Get data of a banner from database *****/ - DB_BuildQuery ("SELECT Hidden,ShortName,FullName,Img,WWW" - " FROM banners WHERE BanCod=%ld", - Ban->BanCod); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get data of a banner"); - + NumRows = DB_QuerySELECT (&mysql_res,"can not get data of a banner", + "SELECT Hidden,ShortName,FullName,Img,WWW" + " FROM banners WHERE BanCod=%ld", + Ban->BanCod); if (NumRows) // Banner found... { /* Get row */ @@ -1007,17 +1006,20 @@ static void Ban_CreateBanner (struct Banner *Ban) void Ban_WriteMenuWithBanners (void) { + MYSQL_RES *mysql_res; + unsigned long NumRows; unsigned NumBan; /***** Get random banner *****/ // The banner(s) will change once in a while - DB_BuildQuery ("SELECT BanCod,Hidden,ShortName,FullName,Img,WWW" - " FROM banners" - " WHERE Hidden='N'" - " ORDER BY RAND(%lu) LIMIT %u", - (unsigned long) (Gbl.StartExecutionTimeUTC / Cfg_TIME_TO_CHANGE_BANNER), - Cfg_NUMBER_OF_BANNERS); - Ban_GetListBanners (); + NumRows = DB_QuerySELECT (&mysql_res,"can not get banners", + "SELECT BanCod,Hidden,ShortName,FullName,Img,WWW" + " FROM banners" + " WHERE Hidden='N'" + " ORDER BY RAND(%lu) LIMIT %u", + (unsigned long) (Gbl.StartExecutionTimeUTC / Cfg_TIME_TO_CHANGE_BANNER), + Cfg_NUMBER_OF_BANNERS); + Ban_GetListBanners (&mysql_res,NumRows); /***** Write all the banners *****/ for (NumBan = 0; diff --git a/swad_centre.c b/swad_centre.c index 584b94df..6d32429f 100644 --- a/swad_centre.c +++ b/swad_centre.c @@ -144,29 +144,32 @@ void Ctr_SeeCtrWithPendingDegs (void) switch (Gbl.Usrs.Me.Role.Logged) { case Rol_CTR_ADM: - DB_BuildQuery ("SELECT degrees.CtrCod,COUNT(*)" - " FROM degrees,ctr_admin,centres" - " WHERE (degrees.Status & %u)<>0" - " AND degrees.CtrCod=ctr_admin.CtrCod" - " AND ctr_admin.UsrCod=%ld" - " AND degrees.CtrCod=centres.CtrCod" - " GROUP BY degrees.CtrCod ORDER BY centres.ShortName", - (unsigned) Deg_STATUS_BIT_PENDING,Gbl.Usrs.Me.UsrDat.UsrCod); + NumCtrs = (unsigned) DB_QuerySELECT (&mysql_res,"can not get centres" + " with pending degrees", + "SELECT degrees.CtrCod,COUNT(*)" + " FROM degrees,ctr_admin,centres" + " WHERE (degrees.Status & %u)<>0" + " AND degrees.CtrCod=ctr_admin.CtrCod" + " AND ctr_admin.UsrCod=%ld" + " AND degrees.CtrCod=centres.CtrCod" + " GROUP BY degrees.CtrCod ORDER BY centres.ShortName", + (unsigned) Deg_STATUS_BIT_PENDING, + Gbl.Usrs.Me.UsrDat.UsrCod); break; case Rol_SYS_ADM: - DB_BuildQuery ("SELECT degrees.CtrCod,COUNT(*)" - " FROM degrees,centres" - " WHERE (degrees.Status & %u)<>0" - " AND degrees.CtrCod=centres.CtrCod" - " GROUP BY degrees.CtrCod ORDER BY centres.ShortName", - (unsigned) Deg_STATUS_BIT_PENDING); + NumCtrs = (unsigned) DB_QuerySELECT (&mysql_res,"can not get centres" + " with pending degrees", + "SELECT degrees.CtrCod,COUNT(*)" + " FROM degrees,centres" + " WHERE (degrees.Status & %u)<>0" + " AND degrees.CtrCod=centres.CtrCod" + " GROUP BY degrees.CtrCod ORDER BY centres.ShortName", + (unsigned) Deg_STATUS_BIT_PENDING); break; default: // Forbidden for other users return; } - - /***** Get centres *****/ - if ((NumCtrs = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get centres with pending degrees"))) + if (NumCtrs) { /***** Start box and table *****/ Box_StartBoxTable (NULL,Txt_Centres_with_pending_degrees,NULL, @@ -1052,26 +1055,26 @@ void Ctr_GetListCentres (long InsCod) Lay_NotEnoughMemoryExit (); break; } - DB_BuildQuery ("(SELECT centres.CtrCod,centres.InsCod,centres.PlcCod," - "centres.Status,centres.RequesterUsrCod," - "centres.ShortName,centres.FullName,centres.WWW," - "COUNT(DISTINCT usr_data.UsrCod) AS NumUsrs" - " FROM centres,usr_data" - " WHERE centres.InsCod=%ld" - " AND centres.CtrCod=usr_data.CtrCod" - " GROUP BY centres.CtrCod)" - " UNION " - "(SELECT CtrCod,InsCod,PlcCod,Status,RequesterUsrCod," - "ShortName,FullName,WWW,0 AS NumUsrs" - " FROM centres" - " WHERE centres.InsCod=%ld" - " AND CtrCod NOT IN" - " (SELECT DISTINCT CtrCod FROM usr_data))" - " ORDER BY %s", - InsCod, - InsCod, - OrderBySubQuery); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get centres"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get centres", + "(SELECT centres.CtrCod,centres.InsCod,centres.PlcCod," + "centres.Status,centres.RequesterUsrCod," + "centres.ShortName,centres.FullName,centres.WWW," + "COUNT(DISTINCT usr_data.UsrCod) AS NumUsrs" + " FROM centres,usr_data" + " WHERE centres.InsCod=%ld" + " AND centres.CtrCod=usr_data.CtrCod" + " GROUP BY centres.CtrCod)" + " UNION " + "(SELECT CtrCod,InsCod,PlcCod,Status,RequesterUsrCod," + "ShortName,FullName,WWW,0 AS NumUsrs" + " FROM centres" + " WHERE centres.InsCod=%ld" + " AND CtrCod NOT IN" + " (SELECT DISTINCT CtrCod FROM usr_data))" + " ORDER BY %s", + InsCod, + InsCod, + OrderBySubQuery); free ((void *) OrderBySubQuery); if (NumRows) // Centres found... @@ -1151,6 +1154,7 @@ bool Ctr_GetDataOfCentreByCod (struct Centre *Ctr) { MYSQL_RES *mysql_res; MYSQL_ROW row; + unsigned long NumRows; bool CtrFound = false; /***** Clear data *****/ @@ -1170,26 +1174,27 @@ bool Ctr_GetDataOfCentreByCod (struct Centre *Ctr) if (Ctr->CtrCod > 0) { /***** Get data of a centre from database *****/ - DB_BuildQuery ("(SELECT centres.InsCod,centres.PlcCod," - "centres.Status,centres.RequesterUsrCod," - "centres.ShortName,centres.FullName,centres.WWW," - "COUNT(DISTINCT usr_data.UsrCod) AS NumUsrs" - " FROM centres,usr_data" - " WHERE centres.CtrCod=%ld" - " AND centres.CtrCod=usr_data.CtrCod" - " GROUP BY centres.CtrCod)" - " UNION " - "(SELECT InsCod,PlcCod," - "Status,RequesterUsrCod," - "ShortName,FullName,WWW," - "0 AS NumUsrs" - " FROM centres" - " WHERE CtrCod=%ld" - " AND CtrCod NOT IN" - " (SELECT DISTINCT CtrCod FROM usr_data))", - Ctr->CtrCod, - Ctr->CtrCod); - if (DB_QuerySELECT_new (&mysql_res,"can not get data of a centre")) // Centre found... + NumRows = DB_QuerySELECT (&mysql_res,"can not get data of a centre", + "(SELECT centres.InsCod,centres.PlcCod," + "centres.Status,centres.RequesterUsrCod," + "centres.ShortName,centres.FullName,centres.WWW," + "COUNT(DISTINCT usr_data.UsrCod) AS NumUsrs" + " FROM centres,usr_data" + " WHERE centres.CtrCod=%ld" + " AND centres.CtrCod=usr_data.CtrCod" + " GROUP BY centres.CtrCod)" + " UNION " + "(SELECT InsCod,PlcCod," + "Status,RequesterUsrCod," + "ShortName,FullName,WWW," + "0 AS NumUsrs" + " FROM centres" + " WHERE CtrCod=%ld" + " AND CtrCod NOT IN" + " (SELECT DISTINCT CtrCod FROM usr_data))", + Ctr->CtrCod, + Ctr->CtrCod); + if (NumRows) // Centre found... { /* Get row */ row = mysql_fetch_row (mysql_res); @@ -1256,9 +1261,9 @@ long Ctr_GetInsCodOfCentreByCod (long CtrCod) if (CtrCod > 0) { /***** Get the institution code of a centre from database *****/ - DB_BuildQuery ("SELECT InsCod FROM centres WHERE CtrCod=%ld", - CtrCod); - if (DB_QuerySELECT_new (&mysql_res,"can not get the institution of a centre") == 1) + if (DB_QuerySELECT (&mysql_res,"can not get the institution of a centre", + "SELECT InsCod FROM centres WHERE CtrCod=%ld", + CtrCod) == 1) { /***** Get the institution code of this centre *****/ row = mysql_fetch_row (mysql_res); @@ -1285,10 +1290,10 @@ void Ctr_GetShortNameOfCentreByCod (struct Centre *Ctr) if (Ctr->CtrCod > 0) { /***** Get the short name of a centre from database *****/ - DB_BuildQuery ("SELECT ShortName FROM centres" - " WHERE CtrCod=%ld", - Ctr->CtrCod); - if (DB_QuerySELECT_new (&mysql_res,"can not get the short name of a centre") == 1) + if (DB_QuerySELECT (&mysql_res,"can not get the short name of a centre", + "SELECT ShortName FROM centres" + " WHERE CtrCod=%ld", + Ctr->CtrCod) == 1) { /***** Get the short name of this centre *****/ row = mysql_fetch_row (mysql_res); @@ -1316,10 +1321,10 @@ static void Ctr_GetPhotoAttribution (long CtrCod,char **PhotoAttribution) Ctr_FreePhotoAttribution (PhotoAttribution); /***** Get photo attribution from database *****/ - DB_BuildQuery ("SELECT PhotoAttribution" - " FROM centres WHERE CtrCod=%ld", - CtrCod); - if (DB_QuerySELECT_new (&mysql_res,"can not get photo attribution")) + if (DB_QuerySELECT (&mysql_res,"can not get photo attribution", + "SELECT PhotoAttribution" + " FROM centres WHERE CtrCod=%ld", + CtrCod)) { /* Get row */ row = mysql_fetch_row (mysql_res); @@ -1398,12 +1403,12 @@ void Ctr_WriteSelectorOfCentre (void) if (Gbl.CurrentIns.Ins.InsCod > 0) { /***** Get centres from database *****/ - DB_BuildQuery ("SELECT DISTINCT CtrCod,ShortName" - " FROM centres" - " WHERE InsCod=%ld" - " ORDER BY ShortName", - Gbl.CurrentIns.Ins.InsCod); - NumCtrs = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get centres"); + NumCtrs = (unsigned) DB_QuerySELECT (&mysql_res,"can not get centres", + "SELECT DISTINCT CtrCod,ShortName" + " FROM centres" + " WHERE InsCod=%ld" + " ORDER BY ShortName", + Gbl.CurrentIns.Ins.InsCod); /***** Get centres *****/ for (NumCtr = 0; @@ -2935,20 +2940,17 @@ unsigned Ctr_GetNumCtrsWithUsrs (Rol_Role_t Role,const char *SubQuery) /*****************************************************************************/ /****************************** List centres found ***************************/ /*****************************************************************************/ -// Returns number of centres found -unsigned Ctr_ListCtrsFound (void) +void Ctr_ListCtrsFound (MYSQL_RES **mysql_res,unsigned NumCtrs) { extern const char *Txt_centre; extern const char *Txt_centres; - MYSQL_RES *mysql_res; MYSQL_ROW row; - unsigned NumCtrs; unsigned NumCtr; struct Centre Ctr; /***** Query database *****/ - if ((NumCtrs = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get centres"))) + if (NumCtrs) { /***** Start box and table *****/ /* Number of centres found */ @@ -2968,7 +2970,7 @@ unsigned Ctr_ListCtrsFound (void) NumCtr++) { /* Get next centre */ - row = mysql_fetch_row (mysql_res); + row = mysql_fetch_row (*mysql_res); /* Get centre code (row[0]) */ Ctr.CtrCod = Str_ConvertStrCodToLongCod (row[0]); @@ -2985,7 +2987,5 @@ unsigned Ctr_ListCtrsFound (void) } /***** Free structure that stores the query result *****/ - DB_FreeMySQLResult (&mysql_res); - - return NumCtrs; + DB_FreeMySQLResult (mysql_res); } diff --git a/swad_centre.h b/swad_centre.h index e7a6a624..0a50046c 100644 --- a/swad_centre.h +++ b/swad_centre.h @@ -27,6 +27,8 @@ /********************************* Headers ***********************************/ /*****************************************************************************/ +#include // To access MySQL databases + #include "swad_action.h" #include "swad_constant.h" #include "swad_degree.h" @@ -134,6 +136,6 @@ unsigned Ctr_GetNumCtrsWithDegs (const char *SubQuery); unsigned Ctr_GetNumCtrsWithCrss (const char *SubQuery); unsigned Ctr_GetNumCtrsWithUsrs (Rol_Role_t Role,const char *SubQuery); -unsigned Ctr_ListCtrsFound (void); +void Ctr_ListCtrsFound (MYSQL_RES **mysql_res,unsigned NumCtrs); #endif diff --git a/swad_changelog.h b/swad_changelog.h index cef31472..e5d71f0b 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 (2018-10-30)" +#define Log_PLATFORM_VERSION "SWAD 18.11.1 (2018-10-30)" #define CSS_FILE "swad18.4.css" #define JS_FILE "swad17.17.1.js" /* + Version 18.11.1: Oct 30, 2018 Joining building and performing query into one function. (235497 lines) Version 18.11: Oct 30, 2018 Joining building and performing query into one function. (235465 lines) Version 18.10.2: Oct 30, 2018 Fixed bugs in access to database. (235414 lines) Version 18.10.1: Oct 30, 2018 Fixed bugs in access to database. (235399 lines) diff --git a/swad_chat.c b/swad_chat.c index 3532ab5b..c0d6936c 100644 --- a/swad_chat.c +++ b/swad_chat.c @@ -273,10 +273,11 @@ void Cht_ShowListOfChatRoomsWithUsrs (void) unsigned long NumRow,NumRows; /***** Get chat rooms with connected users from database *****/ - DB_BuildQuery ("SELECT RoomCode,NumUsrs FROM chat" - " WHERE NumUsrs>0 ORDER BY NumUsrs DESC,RoomCode"); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get chat rooms with connected users"); - + NumRows = DB_QuerySELECT (&mysql_res,"can not get chat rooms" + " with connected users", + "SELECT RoomCode,NumUsrs FROM chat" + " WHERE NumUsrs>0" + " ORDER BY NumUsrs DESC,RoomCode"); if (NumRows > 0) // If not empty chat rooms found { /***** Start box and table *****/ @@ -381,9 +382,10 @@ static unsigned Cht_GetNumUsrsInChatRoom (const char *RoomCode) unsigned NumUsrs = 0; /***** Get number of users connected to chat rooms from database *****/ - DB_BuildQuery ("SELECT NumUsrs FROM chat WHERE RoomCode='%s'", - RoomCode); - if (DB_QuerySELECT_new (&mysql_res,"can not get number of users connected to a chat room")) + if (DB_QuerySELECT (&mysql_res,"can not get number of users" + " connected to a chat room", + "SELECT NumUsrs FROM chat WHERE RoomCode='%s'", + RoomCode)) { /* Get number of users connected to the chat room */ row = mysql_fetch_row (mysql_res); diff --git a/swad_search.c b/swad_search.c index d0f6382e..6cfa8ab4 100644 --- a/swad_search.c +++ b/swad_search.c @@ -688,6 +688,8 @@ static unsigned Sch_SearchInstitutionsInDB (const char *RangeQuery) static unsigned Sch_SearchCentresInDB (const char *RangeQuery) { char SearchQuery[Sch_MAX_BYTES_SEARCH_QUERY + 1]; + MYSQL_RES *mysql_res; + unsigned NumCtrs; /***** Check scope *****/ if (Gbl.Scope.Current != Sco_SCOPE_DEG && @@ -698,15 +700,17 @@ static unsigned Sch_SearchCentresInDB (const char *RangeQuery) if (Sch_BuildSearchQuery (SearchQuery,"centres.FullName",NULL,NULL)) { /***** Query database and list centres found *****/ - DB_BuildQuery ("SELECT centres.CtrCod" - " FROM centres,institutions,countries" - " WHERE %s" - " AND centres.InsCod=institutions.InsCod" - " AND institutions.CtyCod=countries.CtyCod" - "%s" - " ORDER BY centres.FullName,institutions.FullName", - SearchQuery,RangeQuery); - return Ctr_ListCtrsFound (); + NumCtrs = (unsigned) DB_QuerySELECT (&mysql_res,"can not get centres", + "SELECT centres.CtrCod" + " FROM centres,institutions,countries" + " WHERE %s" + " AND centres.InsCod=institutions.InsCod" + " AND institutions.CtyCod=countries.CtyCod" + "%s" + " ORDER BY centres.FullName,institutions.FullName", + SearchQuery,RangeQuery); + Ctr_ListCtrsFound (&mysql_res,NumCtrs); + return NumCtrs; } return 0;