From 5673416716a9ca28e8c13f442b21854a5cc86a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ca=C3=B1as=20Vargas?= Date: Fri, 2 Nov 2018 01:23:05 +0100 Subject: [PATCH] Version 18.11.15 --- swad_changelog.h | 3 +- swad_survey.c | 447 ++++++++++++++++++++----------------- swad_test_import.c | 24 +- swad_timetable.c | 236 ++++++++++---------- swad_web_service.c | 539 +++++++++++++++++++++++++-------------------- 5 files changed, 674 insertions(+), 575 deletions(-) diff --git a/swad_changelog.h b/swad_changelog.h index b32b6f4ce..ce852e568 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.14 (2018-11-01)" +#define Log_PLATFORM_VERSION "SWAD 18.11.15 (2018-11-02)" #define CSS_FILE "swad18.4.css" #define JS_FILE "swad17.17.1.js" /* + Version 18.11.15: Nov 02, 2018 Joining building and performing query into one function. (236312 lines) Version 18.11.14: Nov 01, 2018 Joining building and performing query into one function. (236211 lines) Version 18.11.13: Nov 01, 2018 Joining building and performing query into one function. (236011 lines) Version 18.11.12: Nov 01, 2018 Joining building and performing query into one function. (235965 lines) diff --git a/swad_survey.c b/swad_survey.c index 4af691442..e5992c150 100644 --- a/swad_survey.c +++ b/swad_survey.c @@ -923,7 +923,7 @@ void Svy_GetListSurveys (void) else SubQuery[Sco_SCOPE_CRS][0] = '\0'; - /* Build query */ + /* Make query */ if (SubQueryFilled) { switch (Gbl.Svys.SelectedOrder) @@ -936,22 +936,23 @@ void Svy_GetListSurveys (void) break; } - DB_BuildQuery ("SELECT SvyCod FROM surveys" - " WHERE %s%s%s%s%s%s" - " ORDER BY %s", - SubQuery[Sco_SCOPE_SYS], - SubQuery[Sco_SCOPE_CTY], - SubQuery[Sco_SCOPE_INS], - SubQuery[Sco_SCOPE_CTR], - SubQuery[Sco_SCOPE_DEG], - SubQuery[Sco_SCOPE_CRS], - OrderBySubQuery); + NumRows = DB_QuerySELECT (&mysql_res,"can not get surveys", + "SELECT SvyCod FROM surveys" + " WHERE %s%s%s%s%s%s" + " ORDER BY %s", + SubQuery[Sco_SCOPE_SYS], + SubQuery[Sco_SCOPE_CTY], + SubQuery[Sco_SCOPE_INS], + SubQuery[Sco_SCOPE_CTR], + SubQuery[Sco_SCOPE_DEG], + SubQuery[Sco_SCOPE_CRS], + OrderBySubQuery); } else + { Lay_ShowErrorAndExit ("Can not get list of surveys."); - - /* Make query */ - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get surveys"); + NumRows = 0; // Not reached. Initialized to avoid warning + } if (NumRows) // Surveys found... { @@ -1158,18 +1159,16 @@ void Svy_GetDataOfSurveyByCod (struct Survey *Svy) MYSQL_ROW row; unsigned long NumRows; - /***** Build query *****/ - DB_BuildQuery ("SELECT SvyCod,Scope,Cod,Hidden,Roles,UsrCod," - "UNIX_TIMESTAMP(StartTime)," - "UNIX_TIMESTAMP(EndTime)," - "NOW() BETWEEN StartTime AND EndTime," - "Title" - " FROM surveys" - " WHERE SvyCod=%ld", - Svy->SvyCod); - /***** Get data of survey from database *****/ - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get survey data"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get survey data", + "SELECT SvyCod,Scope,Cod,Hidden,Roles,UsrCod," + "UNIX_TIMESTAMP(StartTime)," + "UNIX_TIMESTAMP(EndTime)," + "NOW() BETWEEN StartTime AND EndTime," + "Title" + " FROM surveys" + " WHERE SvyCod=%ld", + Svy->SvyCod); if (NumRows) // Survey found... { @@ -1389,8 +1388,9 @@ static void Svy_GetSurveyTxtFromDB (long SvyCod,char Txt[Cns_MAX_BYTES_TEXT + 1] unsigned long NumRows; /***** Get text of survey from database *****/ - DB_BuildQuery ("SELECT Txt FROM surveys WHERE SvyCod=%ld",SvyCod); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get survey text"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get survey text", + "SELECT Txt FROM surveys WHERE SvyCod=%ld", + SvyCod); /***** The result of the query must have one row or none *****/ if (NumRows == 1) @@ -2417,14 +2417,15 @@ static void Svy_GetAndWriteNamesOfGrpsAssociatedToSvy (struct Survey *Svy) unsigned long NumRows; /***** Get groups associated to a survey from database *****/ - DB_BuildQuery ("SELECT crs_grp_types.GrpTypName,crs_grp.GrpName" - " FROM svy_grp,crs_grp,crs_grp_types" - " WHERE svy_grp.SvyCod=%ld" - " AND svy_grp.GrpCod=crs_grp.GrpCod" - " AND crs_grp.GrpTypCod=crs_grp_types.GrpTypCod" - " ORDER BY crs_grp_types.GrpTypName,crs_grp.GrpName", - Svy->SvyCod); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get groups of a survey"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get groups of a survey", + "SELECT crs_grp_types.GrpTypName,crs_grp.GrpName" + " FROM svy_grp,crs_grp,crs_grp_types" + " WHERE svy_grp.SvyCod=%ld" + " AND svy_grp.GrpCod=crs_grp.GrpCod" + " AND crs_grp.GrpTypCod=crs_grp_types.GrpTypCod" + " ORDER BY crs_grp_types.GrpTypName," + "crs_grp.GrpName", + Svy->SvyCod); /***** Write heading *****/ fprintf (Gbl.F.Out,"
%s: ", @@ -2609,10 +2610,10 @@ static void Svy_ShowFormEditOneQst (long SvyCod,struct SurveyQuestion *SvyQst, { /***** Get the type of answer and the stem from the database *****/ /* Get the question from database */ - DB_BuildQuery ("SELECT QstInd,AnsType,Stem FROM svy_questions" - " WHERE QstCod=%ld AND SvyCod=%ld", - SvyQst->QstCod,SvyCod); - DB_QuerySELECT_new (&mysql_res,"can not get a question"); + DB_QuerySELECT (&mysql_res,"can not get a question", + "SELECT QstInd,AnsType,Stem FROM svy_questions" + " WHERE QstCod=%ld AND SvyCod=%ld", + SvyQst->QstCod,SvyCod); row = mysql_fetch_row (mysql_res); @@ -2856,10 +2857,10 @@ static unsigned Svy_GetAnswersQst (long QstCod,MYSQL_RES **mysql_res) unsigned long NumRows; /***** Get answers of a question from database *****/ - DB_BuildQuery ("SELECT AnsInd,NumUsrs,Answer FROM svy_answers" - " WHERE QstCod=%ld ORDER BY AnsInd", - QstCod); - NumRows = DB_QuerySELECT_new (mysql_res,"can not get answers of a question"); + NumRows = DB_QuerySELECT (mysql_res,"can not get answers of a question", + "SELECT AnsInd,NumUsrs,Answer FROM svy_answers" + " WHERE QstCod=%ld ORDER BY AnsInd", + QstCod); /***** Count number of rows of result *****/ if (NumRows == 0) @@ -3095,8 +3096,10 @@ static unsigned Svy_GetQstIndFromQstCod (long QstCod) unsigned QstInd = 0; /***** Get number of surveys with a field value from database *****/ - DB_BuildQuery ("SELECT QstInd FROM svy_questions WHERE QstCod=%ld",QstCod); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get question index"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get question index", + "SELECT QstInd FROM svy_questions" + " WHERE QstCod=%ld", + QstCod); /***** Get number of users *****/ if (NumRows) @@ -3125,9 +3128,9 @@ static unsigned Svy_GetNextQuestionIndexInSvy (long SvyCod) unsigned QstInd = 0; /***** Get number of surveys with a field value from database *****/ - DB_BuildQuery ("SELECT MAX(QstInd) FROM svy_questions WHERE SvyCod=%ld", - SvyCod); - DB_QuerySELECT_new (&mysql_res,"can not get last question index"); + DB_QuerySELECT (&mysql_res,"can not get last question index", + "SELECT MAX(QstInd) FROM svy_questions WHERE SvyCod=%ld", + SvyCod); /***** Get number of users *****/ row = mysql_fetch_row (mysql_res); @@ -3169,11 +3172,11 @@ static void Svy_ListSvyQuestions (struct Survey *Svy, bool PutFormAnswerSurvey = Svy->Status.ICanAnswer && !Editing; /***** Get data of questions from database *****/ - DB_BuildQuery ("SELECT QstCod,QstInd,AnsType,Stem" - " FROM svy_questions" - " WHERE SvyCod=%ld ORDER BY QstInd", - Svy->SvyCod); - NumQsts = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get data of a question"); + NumQsts = (unsigned) DB_QuerySELECT (&mysql_res,"can not get data of a question", + "SELECT QstCod,QstInd,AnsType,Stem" + " FROM svy_questions" + " WHERE SvyCod=%ld ORDER BY QstInd", + Svy->SvyCod); /***** Start box *****/ Gbl.Svys.SvyCodToEdit = Svy->SvyCod; @@ -3663,11 +3666,12 @@ static void Svy_ReceiveAndStoreUserAnswersToASurvey (long SvyCod) unsigned AnsInd; /***** Get questions of this survey from database *****/ - DB_BuildQuery ("SELECT QstCod FROM svy_questions" - " WHERE SvyCod=%ld ORDER BY QstCod", - SvyCod); - if ((NumQsts = (unsigned) DB_QuerySELECT_new (&mysql_res, - "can not get questions of a survey"))) + NumQsts = (unsigned) DB_QuerySELECT (&mysql_res,"can not get questions" + " of a survey", + "SELECT QstCod FROM svy_questions" + " WHERE SvyCod=%ld ORDER BY QstCod", + SvyCod); + if (NumQsts) { // This survey has questions /***** Get questions *****/ @@ -3778,65 +3782,76 @@ unsigned Svy_GetNumCoursesWithCrsSurveys (Sco_Scope_t Scope) switch (Scope) { case Sco_SCOPE_SYS: - DB_BuildQuery ("SELECT COUNT(DISTINCT Cod)" - " FROM surveys" - " WHERE Scope='%s'", - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of courses" + " with surveys", + "SELECT COUNT(DISTINCT Cod)" + " FROM surveys" + " WHERE Scope='%s'", + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_CTY: - DB_BuildQuery ("SELECT COUNT(DISTINCT surveys.Cod)" - " FROM institutions,centres,degrees,courses,surveys" - " WHERE institutions.CtyCod=%ld" - " AND institutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=surveys.Cod" - " AND surveys.Scope='%s'", - Gbl.CurrentIns.Ins.InsCod, - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of courses" + " with surveys", + "SELECT COUNT(DISTINCT surveys.Cod)" + " FROM institutions,centres,degrees,courses,surveys" + " WHERE institutions.CtyCod=%ld" + " AND institutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=surveys.Cod" + " AND surveys.Scope='%s'", + Gbl.CurrentIns.Ins.InsCod, + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_INS: - DB_BuildQuery ("SELECT COUNT(DISTINCT surveys.Cod)" - " FROM centres,degrees,courses,surveys" - " WHERE centres.InsCod=%ld" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=surveys.Cod" - " AND surveys.Scope='%s'", - Gbl.CurrentIns.Ins.InsCod, - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of courses" + " with surveys", + "SELECT COUNT(DISTINCT surveys.Cod)" + " FROM centres,degrees,courses,surveys" + " WHERE centres.InsCod=%ld" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=surveys.Cod" + " AND surveys.Scope='%s'", + Gbl.CurrentIns.Ins.InsCod, + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_CTR: - DB_BuildQuery ("SELECT COUNT(DISTINCT surveys.Cod)" - " FROM degrees,courses,surveys" - " WHERE degrees.CtrCod=%ld" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=surveys.Cod" - " AND surveys.Scope='%s'", - Gbl.CurrentCtr.Ctr.CtrCod, - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of courses" + " with surveys", + "SELECT COUNT(DISTINCT surveys.Cod)" + " FROM degrees,courses,surveys" + " WHERE degrees.CtrCod=%ld" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=surveys.Cod" + " AND surveys.Scope='%s'", + Gbl.CurrentCtr.Ctr.CtrCod, + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_DEG: - DB_BuildQuery ("SELECT COUNT(DISTINCT surveys.Cod)" - " FROM courses,surveys" - " WHERE courses.DegCod=%ld" - " AND courses.CrsCod=surveys.Cod" - " AND surveys.Scope='%s'", - Gbl.CurrentDeg.Deg.DegCod, - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of courses" + " with surveys", + "SELECT COUNT(DISTINCT surveys.Cod)" + " FROM courses,surveys" + " WHERE courses.DegCod=%ld" + " AND courses.CrsCod=surveys.Cod" + " AND surveys.Scope='%s'", + Gbl.CurrentDeg.Deg.DegCod, + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_CRS: - DB_BuildQuery ("SELECT COUNT(DISTINCT Cod)" - " FROM surveys" - " WHERE Scope='%s' AND Cod=%ld", - Sco_ScopeDB[Sco_SCOPE_CRS], - Gbl.CurrentCrs.Crs.CrsCod); + DB_QuerySELECT (&mysql_res,"can not get number of courses" + " with surveys", + "SELECT COUNT(DISTINCT Cod)" + " FROM surveys" + " WHERE Scope='%s' AND Cod=%ld", + Sco_ScopeDB[Sco_SCOPE_CRS], + Gbl.CurrentCrs.Crs.CrsCod); break; default: Lay_WrongScopeExit (); break; } - DB_QuerySELECT_new (&mysql_res,"can not get number of courses with surveys"); /***** Get number of surveys *****/ row = mysql_fetch_row (mysql_res); @@ -3866,66 +3881,71 @@ unsigned Svy_GetNumCrsSurveys (Sco_Scope_t Scope,unsigned *NumNotif) switch (Scope) { case Sco_SCOPE_SYS: - DB_BuildQuery ("SELECT COUNT(*),SUM(NumNotif)" - " FROM surveys" - " WHERE Scope='%s'", - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of surveys", + "SELECT COUNT(*),SUM(NumNotif)" + " FROM surveys" + " WHERE Scope='%s'", + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_CTY: - DB_BuildQuery ("SELECT COUNT(*),SUM(surveys.NumNotif)" - " FROM institutions,centres,degrees,courses,surveys" - " WHERE institutions.CtyCod=%ld" - " AND institutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=surveys.Cod" - " AND surveys.Scope='%s'", - Gbl.CurrentCty.Cty.CtyCod, - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of surveys", + "SELECT COUNT(*),SUM(surveys.NumNotif)" + " FROM institutions,centres,degrees,courses,surveys" + " WHERE institutions.CtyCod=%ld" + " AND institutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=surveys.Cod" + " AND surveys.Scope='%s'", + Gbl.CurrentCty.Cty.CtyCod, + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_INS: - DB_BuildQuery ("SELECT COUNT(*),SUM(surveys.NumNotif)" - " FROM centres,degrees,courses,surveys" - " WHERE centres.InsCod=%ld" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=surveys.Cod" - " AND surveys.Scope='%s'", - Gbl.CurrentIns.Ins.InsCod, - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of surveys", + "SELECT COUNT(*),SUM(surveys.NumNotif)" + " FROM centres,degrees,courses,surveys" + " WHERE centres.InsCod=%ld" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=surveys.Cod" + " AND surveys.Scope='%s'", + Gbl.CurrentIns.Ins.InsCod, + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_CTR: - DB_BuildQuery ("SELECT COUNT(*),SUM(surveys.NumNotif)" - " FROM degrees,courses,surveys" - " WHERE degrees.CtrCod=%ld" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=surveys.Cod" - " AND surveys.Scope='%s'", - Gbl.CurrentCtr.Ctr.CtrCod, - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of surveys", + "SELECT COUNT(*),SUM(surveys.NumNotif)" + " FROM degrees,courses,surveys" + " WHERE degrees.CtrCod=%ld" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=surveys.Cod" + " AND surveys.Scope='%s'", + Gbl.CurrentCtr.Ctr.CtrCod, + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_DEG: - DB_BuildQuery ("SELECT COUNT(*),SUM(surveys.NumNotif)" - " FROM courses,surveys" - " WHERE courses.DegCod=%ld" - " AND courses.CrsCod=surveys.Cod" - " AND surveys.Scope='%s'", - Gbl.CurrentDeg.Deg.DegCod, - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of surveys", + "SELECT COUNT(*),SUM(surveys.NumNotif)" + " FROM courses,surveys" + " WHERE courses.DegCod=%ld" + " AND courses.CrsCod=surveys.Cod" + " AND surveys.Scope='%s'", + Gbl.CurrentDeg.Deg.DegCod, + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_CRS: - DB_BuildQuery ("SELECT COUNT(*),SUM(NumNotif)" - " FROM surveys" - " WHERE surveys.Scope='%s'" - " AND CrsCod=%ld", - Sco_ScopeDB[Sco_SCOPE_CRS], - Gbl.CurrentCrs.Crs.CrsCod); + DB_QuerySELECT (&mysql_res,"can not get number of surveys", + "SELECT COUNT(*),SUM(NumNotif)" + " FROM surveys" + " WHERE surveys.Scope='%s'" + " AND CrsCod=%ld", + Sco_ScopeDB[Sco_SCOPE_CRS], + Gbl.CurrentCrs.Crs.CrsCod); break; default: Lay_WrongScopeExit (); break; } - DB_QuerySELECT_new (&mysql_res,"can not get number of surveys"); /***** Get number of surveys *****/ row = mysql_fetch_row (mysql_res); @@ -3962,82 +3982,93 @@ float Svy_GetNumQstsPerCrsSurvey (Sco_Scope_t Scope) switch (Scope) { case Sco_SCOPE_SYS: - DB_BuildQuery ("SELECT AVG(NumQsts) FROM" - " (SELECT COUNT(svy_questions.QstCod) AS NumQsts" - " FROM surveys,svy_questions" - " WHERE surveys.Scope='%s'" - " AND surveys.SvyCod=svy_questions.SvyCod" - " GROUP BY svy_questions.SvyCod) AS NumQstsTable", - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of questions" + " per survey", + "SELECT AVG(NumQsts) FROM" + " (SELECT COUNT(svy_questions.QstCod) AS NumQsts" + " FROM surveys,svy_questions" + " WHERE surveys.Scope='%s'" + " AND surveys.SvyCod=svy_questions.SvyCod" + " GROUP BY svy_questions.SvyCod) AS NumQstsTable", + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_CTY: - DB_BuildQuery ("SELECT AVG(NumQsts) FROM" - " (SELECT COUNT(svy_questions.QstCod) AS NumQsts" - " FROM institutions,centres,degrees,courses,surveys,svy_questions" - " WHERE institutions.CtyCod=%ld" - " AND institutions.InsCod=centres.InsCod" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=surveys.Cod" - " AND surveys.Scope='%s'" - " AND surveys.SvyCod=svy_questions.SvyCod" - " GROUP BY svy_questions.SvyCod) AS NumQstsTable", - Gbl.CurrentCty.Cty.CtyCod, - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of questions" + " per survey", + "SELECT AVG(NumQsts) FROM" + " (SELECT COUNT(svy_questions.QstCod) AS NumQsts" + " FROM institutions,centres,degrees,courses,surveys,svy_questions" + " WHERE institutions.CtyCod=%ld" + " AND institutions.InsCod=centres.InsCod" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=surveys.Cod" + " AND surveys.Scope='%s'" + " AND surveys.SvyCod=svy_questions.SvyCod" + " GROUP BY svy_questions.SvyCod) AS NumQstsTable", + Gbl.CurrentCty.Cty.CtyCod, + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_INS: - DB_BuildQuery ("SELECT AVG(NumQsts) FROM" - " (SELECT COUNT(svy_questions.QstCod) AS NumQsts" - " FROM centres,degrees,courses,surveys,svy_questions" - " WHERE centres.InsCod=%ld" - " AND centres.CtrCod=degrees.CtrCod" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=surveys.Cod" - " AND surveys.Scope='%s'" - " AND surveys.SvyCod=svy_questions.SvyCod" - " GROUP BY svy_questions.SvyCod) AS NumQstsTable", - Gbl.CurrentIns.Ins.InsCod, - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of questions" + " per survey", + "SELECT AVG(NumQsts) FROM" + " (SELECT COUNT(svy_questions.QstCod) AS NumQsts" + " FROM centres,degrees,courses,surveys,svy_questions" + " WHERE centres.InsCod=%ld" + " AND centres.CtrCod=degrees.CtrCod" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=surveys.Cod" + " AND surveys.Scope='%s'" + " AND surveys.SvyCod=svy_questions.SvyCod" + " GROUP BY svy_questions.SvyCod) AS NumQstsTable", + Gbl.CurrentIns.Ins.InsCod, + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_CTR: - DB_BuildQuery ("SELECT AVG(NumQsts) FROM" - " (SELECT COUNT(svy_questions.QstCod) AS NumQsts" - " FROM degrees,courses,surveys,svy_questions" - " WHERE degrees.CtrCod=%ld" - " AND degrees.DegCod=courses.DegCod" - " AND courses.CrsCod=surveys.Cod" - " AND surveys.Scope='%s'" - " AND surveys.SvyCod=svy_questions.SvyCod" - " GROUP BY svy_questions.SvyCod) AS NumQstsTable", - Gbl.CurrentCtr.Ctr.CtrCod, - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of questions" + " per survey", + "SELECT AVG(NumQsts) FROM" + " (SELECT COUNT(svy_questions.QstCod) AS NumQsts" + " FROM degrees,courses,surveys,svy_questions" + " WHERE degrees.CtrCod=%ld" + " AND degrees.DegCod=courses.DegCod" + " AND courses.CrsCod=surveys.Cod" + " AND surveys.Scope='%s'" + " AND surveys.SvyCod=svy_questions.SvyCod" + " GROUP BY svy_questions.SvyCod) AS NumQstsTable", + Gbl.CurrentCtr.Ctr.CtrCod, + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_DEG: - DB_BuildQuery ("SELECT AVG(NumQsts) FROM" - " (SELECT COUNT(svy_questions.QstCod) AS NumQsts" - " FROM courses,surveys,svy_questions" - " WHERE courses.DegCod=%ld" - " AND courses.CrsCod=surveys.Cod" - " AND surveys.Scope='%s'" - " AND surveys.SvyCod=svy_questions.SvyCod" - " GROUP BY svy_questions.SvyCod) AS NumQstsTable", - Gbl.CurrentDeg.Deg.DegCod, - Sco_ScopeDB[Sco_SCOPE_CRS]); + DB_QuerySELECT (&mysql_res,"can not get number of questions" + " per survey", + "SELECT AVG(NumQsts) FROM" + " (SELECT COUNT(svy_questions.QstCod) AS NumQsts" + " FROM courses,surveys,svy_questions" + " WHERE courses.DegCod=%ld" + " AND courses.CrsCod=surveys.Cod" + " AND surveys.Scope='%s'" + " AND surveys.SvyCod=svy_questions.SvyCod" + " GROUP BY svy_questions.SvyCod) AS NumQstsTable", + Gbl.CurrentDeg.Deg.DegCod, + Sco_ScopeDB[Sco_SCOPE_CRS]); break; case Sco_SCOPE_CRS: - DB_BuildQuery ("SELECT AVG(NumQsts) FROM" - " (SELECT COUNT(svy_questions.QstCod) AS NumQsts" - " FROM surveys,svy_questions" - " WHERE surveys.Scope='%s' AND surveys.Cod=%ld" - " AND surveys.SvyCod=svy_questions.SvyCod" - " GROUP BY svy_questions.SvyCod) AS NumQstsTable", - Sco_ScopeDB[Sco_SCOPE_CRS],Gbl.CurrentCrs.Crs.CrsCod); + DB_QuerySELECT (&mysql_res,"can not get number of questions" + " per survey", + "SELECT AVG(NumQsts) FROM" + " (SELECT COUNT(svy_questions.QstCod) AS NumQsts" + " FROM surveys,svy_questions" + " WHERE surveys.Scope='%s' AND surveys.Cod=%ld" + " AND surveys.SvyCod=svy_questions.SvyCod" + " GROUP BY svy_questions.SvyCod) AS NumQstsTable", + Sco_ScopeDB[Sco_SCOPE_CRS],Gbl.CurrentCrs.Crs.CrsCod); break; default: Lay_WrongScopeExit (); break; } - DB_QuerySELECT_new (&mysql_res,"can not get number of questions per survey"); /***** Get number of courses *****/ row = mysql_fetch_row (mysql_res); diff --git a/swad_test_import.c b/swad_test_import.c index c06e025eb..48319c652 100644 --- a/swad_test_import.c +++ b/swad_test_import.c @@ -730,12 +730,14 @@ static bool TsI_CheckIfQuestionExistsInDB (void) unsigned i; /***** Check if stem exists *****/ - DB_BuildQuery ("SELECT QstCod FROM tst_questions" - " WHERE CrsCod=%ld AND AnsType='%s' AND Stem='%s'", - Gbl.CurrentCrs.Crs.CrsCod, - Tst_StrAnswerTypesDB[Gbl.Test.AnswerType], - Gbl.Test.Stem.Text); - NumQstsWithThisStem = (unsigned) DB_QuerySELECT_new (&mysql_res_qst,"can not check if a question exists"); + NumQstsWithThisStem = + (unsigned) DB_QuerySELECT (&mysql_res_qst,"can not check" + " if a question exists", + "SELECT QstCod FROM tst_questions" + " WHERE CrsCod=%ld AND AnsType='%s' AND Stem='%s'", + Gbl.CurrentCrs.Crs.CrsCod, + Tst_StrAnswerTypesDB[Gbl.Test.AnswerType], + Gbl.Test.Stem.Text); if (NumQstsWithThisStem) // There are questions in database with the same stem that the one of this question { @@ -750,10 +752,12 @@ static bool TsI_CheckIfQuestionExistsInDB (void) Lay_ShowErrorAndExit ("Wrong code of question."); /* Get answers from this question */ - DB_BuildQuery ("SELECT Answer FROM tst_answers" - " WHERE QstCod=%ld ORDER BY AnsInd", - QstCod); - NumOptsExistingQstInDB = (unsigned) DB_QuerySELECT_new (&mysql_res_ans,"can not get the answer of a question"); + NumOptsExistingQstInDB = + (unsigned) DB_QuerySELECT (&mysql_res_ans,"can not get the answer" + " of a question", + "SELECT Answer FROM tst_answers" + " WHERE QstCod=%ld ORDER BY AnsInd", + QstCod); switch (Gbl.Test.AnswerType) { diff --git a/swad_timetable.c b/swad_timetable.c index bdebb827e..b71fda1be 100644 --- a/swad_timetable.c +++ b/swad_timetable.c @@ -657,8 +657,8 @@ static void TT_FillTimeTableFromDB (long UsrCod) extern const char *Txt_Incomplete_timetable_for_lack_of_space; MYSQL_RES *mysql_res; MYSQL_ROW row; + unsigned long NumRows = 0; // Initialized to avoid warning unsigned long NumRow; - unsigned long NumRows; unsigned Weekday; unsigned Interval; unsigned i; // To iterate through intervals @@ -682,76 +682,73 @@ static void TT_FillTimeTableFromDB (long UsrCod) switch (Gbl.CurrentCrs.Grps.WhichGrps) { case Grp_ONLY_MY_GROUPS: - DB_BuildQuery ("SELECT " - "timetable_crs.Weekday," - "TIME_TO_SEC(timetable_crs.StartTime) AS S," - "TIME_TO_SEC(timetable_crs.Duration) AS D," - "timetable_crs.Place," - "timetable_crs.ClassType," - "timetable_crs.GroupName," - "timetable_crs.GrpCod," - "timetable_crs.CrsCod" - " FROM timetable_crs,crs_usr" - " WHERE crs_usr.UsrCod=%ld" - " AND timetable_crs.GrpCod=-1" - " AND timetable_crs.CrsCod=crs_usr.CrsCod" - " UNION DISTINCT " - "SELECT " - "timetable_crs.Weekday," - "TIME_TO_SEC(timetable_crs.StartTime) AS S," - "TIME_TO_SEC(timetable_crs.Duration) AS D," - "timetable_crs.Place," - "timetable_crs.ClassType," - "timetable_crs.GroupName," - "timetable_crs.GrpCod," - "timetable_crs.CrsCod" - " FROM timetable_crs,crs_grp_usr" - " WHERE crs_grp_usr.UsrCod=%ld" - " AND timetable_crs.GrpCod=crs_grp_usr.GrpCod" - " UNION " - "SELECT " - "Weekday," - "TIME_TO_SEC(StartTime) AS S," - "TIME_TO_SEC(Duration) AS D," - "Place," - "'tutoring' AS ClassType," - "'' AS GroupName," - "-1 AS GrpCod," - "-1 AS CrsCod" - " FROM timetable_tut" - " WHERE UsrCod=%ld" - " ORDER BY Weekday,S,ClassType," - "GroupName,GrpCod,Place,D DESC,CrsCod", - UsrCod,UsrCod,UsrCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get timetable", + "SELECT timetable_crs.Weekday," + "TIME_TO_SEC(timetable_crs.StartTime) AS S," + "TIME_TO_SEC(timetable_crs.Duration) AS D," + "timetable_crs.Place," + "timetable_crs.ClassType," + "timetable_crs.GroupName," + "timetable_crs.GrpCod," + "timetable_crs.CrsCod" + " FROM timetable_crs,crs_usr" + " WHERE crs_usr.UsrCod=%ld" + " AND timetable_crs.GrpCod=-1" + " AND timetable_crs.CrsCod=crs_usr.CrsCod" + " UNION DISTINCT " + "SELECT timetable_crs.Weekday," + "TIME_TO_SEC(timetable_crs.StartTime) AS S," + "TIME_TO_SEC(timetable_crs.Duration) AS D," + "timetable_crs.Place," + "timetable_crs.ClassType," + "timetable_crs.GroupName," + "timetable_crs.GrpCod," + "timetable_crs.CrsCod" + " FROM timetable_crs,crs_grp_usr" + " WHERE crs_grp_usr.UsrCod=%ld" + " AND timetable_crs.GrpCod=crs_grp_usr.GrpCod" + " UNION " + "SELECT Weekday," + "TIME_TO_SEC(StartTime) AS S," + "TIME_TO_SEC(Duration) AS D," + "Place," + "'tutoring' AS ClassType," + "'' AS GroupName," + "-1 AS GrpCod," + "-1 AS CrsCod" + " FROM timetable_tut" + " WHERE UsrCod=%ld" + " ORDER BY Weekday,S,ClassType," + "GroupName,GrpCod,Place,D DESC,CrsCod", + UsrCod,UsrCod,UsrCod); break; case Grp_ALL_GROUPS: - DB_BuildQuery ("SELECT " - "timetable_crs.Weekday," - "TIME_TO_SEC(timetable_crs.StartTime) AS S," - "TIME_TO_SEC(timetable_crs.Duration) AS D," - "timetable_crs.Place," - "timetable_crs.ClassType," - "timetable_crs.GroupName," - "timetable_crs.GrpCod," - "timetable_crs.CrsCod" - " FROM timetable_crs,crs_usr" - " WHERE crs_usr.UsrCod=%ld" - " AND timetable_crs.CrsCod=crs_usr.CrsCod" - " UNION " - "SELECT " - "Weekday," - "TIME_TO_SEC(StartTime) AS S," - "TIME_TO_SEC(Duration) AS D," - "Place," - "'tutoring' AS ClassType," - "'' AS GroupName," - "-1 AS GrpCod," - "-1 AS CrsCod" - " FROM timetable_tut" - " WHERE UsrCod=%ld" - " ORDER BY Weekday,S,ClassType," - "GroupName,GrpCod,Place,D DESC,CrsCod", - UsrCod,UsrCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get timetable", + "SELECT timetable_crs.Weekday," + "TIME_TO_SEC(timetable_crs.StartTime) AS S," + "TIME_TO_SEC(timetable_crs.Duration) AS D," + "timetable_crs.Place," + "timetable_crs.ClassType," + "timetable_crs.GroupName," + "timetable_crs.GrpCod," + "timetable_crs.CrsCod" + " FROM timetable_crs,crs_usr" + " WHERE crs_usr.UsrCod=%ld" + " AND timetable_crs.CrsCod=crs_usr.CrsCod" + " UNION " + "SELECT Weekday," + "TIME_TO_SEC(StartTime) AS S," + "TIME_TO_SEC(Duration) AS D," + "Place," + "'tutoring' AS ClassType," + "'' AS GroupName," + "-1 AS GrpCod," + "-1 AS CrsCod" + " FROM timetable_tut" + " WHERE UsrCod=%ld" + " ORDER BY Weekday,S,ClassType," + "GroupName,GrpCod,Place,D DESC,CrsCod", + UsrCod,UsrCod); break; } break; @@ -759,62 +756,61 @@ static void TT_FillTimeTableFromDB (long UsrCod) if (Gbl.CurrentCrs.Grps.WhichGrps == Grp_ALL_GROUPS || Gbl.Action.Act == ActEdiCrsTT || Gbl.Action.Act == ActChgCrsTT) // If we are editing, all groups are shown - DB_BuildQuery ("SELECT " - "Weekday," - "TIME_TO_SEC(StartTime) AS S," - "TIME_TO_SEC(Duration) AS D," - "Place," - "ClassType," - "GroupName," - "GrpCod" - " FROM timetable_crs" - " WHERE CrsCod=%ld" - " ORDER BY Weekday,S,ClassType," - "GroupName,GrpCod,Place,D DESC", - Gbl.CurrentCrs.Crs.CrsCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get timetable", + "SELECT Weekday," + "TIME_TO_SEC(StartTime) AS S," + "TIME_TO_SEC(Duration) AS D," + "Place," + "ClassType," + "GroupName," + "GrpCod" + " FROM timetable_crs" + " WHERE CrsCod=%ld" + " ORDER BY Weekday,S,ClassType," + "GroupName,GrpCod,Place,D DESC", + Gbl.CurrentCrs.Crs.CrsCod); else - DB_BuildQuery ("SELECT " - "timetable_crs.Weekday," - "TIME_TO_SEC(timetable_crs.StartTime) AS S," - "TIME_TO_SEC(timetable_crs.Duration) AS D," - "timetable_crs.Place," - "timetable_crs.ClassType," - "timetable_crs.GroupName," - "timetable_crs.GrpCod" - " FROM timetable_crs,crs_usr" - " WHERE timetable_crs.CrsCod=%ld" - " AND timetable_crs.GrpCod=-1 AND crs_usr.UsrCod=%ld" - " AND timetable_crs.CrsCod=crs_usr.CrsCod" - " UNION DISTINCT " - "SELECT timetable_crs.Weekday," - "TIME_TO_SEC(timetable_crs.StartTime) AS S," - "TIME_TO_SEC(timetable_crs.Duration) AS D," - "timetable_crs.Place," - "timetable_crs.ClassType," - "timetable_crs.GroupName," - "timetable_crs.GrpCod" - " FROM timetable_crs,crs_grp_usr" - " WHERE timetable_crs.CrsCod=%ld" - " AND crs_grp_usr.UsrCod=%ld" - " AND timetable_crs.GrpCod=crs_grp_usr.GrpCod" - " ORDER BY Weekday,S,ClassType," - "GroupName,GrpCod,Place,D DESC", - Gbl.CurrentCrs.Crs.CrsCod,UsrCod, - Gbl.CurrentCrs.Crs.CrsCod,UsrCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get timetable", + "SELECT timetable_crs.Weekday," + "TIME_TO_SEC(timetable_crs.StartTime) AS S," + "TIME_TO_SEC(timetable_crs.Duration) AS D," + "timetable_crs.Place," + "timetable_crs.ClassType," + "timetable_crs.GroupName," + "timetable_crs.GrpCod" + " FROM timetable_crs,crs_usr" + " WHERE timetable_crs.CrsCod=%ld" + " AND timetable_crs.GrpCod=-1 AND crs_usr.UsrCod=%ld" + " AND timetable_crs.CrsCod=crs_usr.CrsCod" + " UNION DISTINCT " + "SELECT timetable_crs.Weekday," + "TIME_TO_SEC(timetable_crs.StartTime) AS S," + "TIME_TO_SEC(timetable_crs.Duration) AS D," + "timetable_crs.Place," + "timetable_crs.ClassType," + "timetable_crs.GroupName," + "timetable_crs.GrpCod" + " FROM timetable_crs,crs_grp_usr" + " WHERE timetable_crs.CrsCod=%ld" + " AND crs_grp_usr.UsrCod=%ld" + " AND timetable_crs.GrpCod=crs_grp_usr.GrpCod" + " ORDER BY Weekday,S,ClassType," + "GroupName,GrpCod,Place,D DESC", + Gbl.CurrentCrs.Crs.CrsCod,UsrCod, + Gbl.CurrentCrs.Crs.CrsCod,UsrCod); break; case TT_TUTORING_TIMETABLE: - DB_BuildQuery ("SELECT " - "Weekday," - "TIME_TO_SEC(StartTime) AS S," - "TIME_TO_SEC(Duration) AS D," - "Place" - " FROM timetable_tut" - " WHERE UsrCod=%ld" - " ORDER BY Weekday,S,Place,D DESC", - UsrCod); + NumRows = DB_QuerySELECT (&mysql_res,"can not get timetable", + "SELECT Weekday," + "TIME_TO_SEC(StartTime) AS S," + "TIME_TO_SEC(Duration) AS D," + "Place" + " FROM timetable_tut" + " WHERE UsrCod=%ld" + " ORDER BY Weekday,S,Place,D DESC", + UsrCod); break; } - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get timetable"); /***** If viewing (not editing) ==> calculate range of hours and resolution *****/ diff --git a/swad_web_service.c b/swad_web_service.c index 2edb63034..ad02ff3a5 100644 --- a/swad_web_service.c +++ b/swad_web_service.c @@ -288,8 +288,9 @@ static int Svc_GetPlgCodFromAppKey (const char *appKey) Gbl.WebService.PlgCod = -1L; /***** Get number of plugins with a IP address *****/ - DB_BuildQuery ("SELECT PlgCod FROM plugins WHERE AppKey='%s'",appKey); - if (DB_QuerySELECT_new (&mysql_res,"can not check application key")) // Session found in table of sessions + if (DB_QuerySELECT (&mysql_res,"can not check application key", + "SELECT PlgCod FROM plugins WHERE AppKey='%s'", + appKey)) // Session found in table of sessions { row = mysql_fetch_row (mysql_res); @@ -381,8 +382,9 @@ static int Svc_CheckWSKey (char WSKey[Svc_BYTES_WS_KEY + 1]) Gbl.WebService.PlgCod = -1L; /***** Check that key does not exist in database *****/ - DB_BuildQuery ("SELECT UsrCod,PlgCod FROM ws_keys WHERE WSKey='%s'",WSKey); - if (DB_QuerySELECT_new (&mysql_res,"can not get existence of key")) // Session found in table of sessions + if (DB_QuerySELECT (&mysql_res,"can not get existence of key", + "SELECT UsrCod,PlgCod FROM ws_keys WHERE WSKey='%s'", + WSKey)) // Session found in table of sessions { row = mysql_fetch_row (mysql_res); @@ -504,9 +506,9 @@ static int Svc_GetCurrentDegCodFromCurrentCrsCod (void) Gbl.CurrentDeg.Deg.DegCod = -1L; /***** Check that key does not exist in database *****/ - DB_BuildQuery ("SELECT DegCod FROM courses WHERE CrsCod=%ld", - Gbl.CurrentCrs.Crs.CrsCod); - if (DB_QuerySELECT_new (&mysql_res,"can not get the degree of a course")) // Course found in table of courses + if (DB_QuerySELECT (&mysql_res,"can not get the degree of a course", + "SELECT DegCod FROM courses WHERE CrsCod=%ld", + Gbl.CurrentCrs.Crs.CrsCod)) // Course found in table of courses { row = mysql_fetch_row (mysql_res); @@ -534,13 +536,10 @@ static bool Svc_GetSomeUsrDataFromUsrCod (struct UsrData *UsrDat,long CrsCod) return false; /***** Get some user's data *****/ - /* Query database */ - DB_BuildQuery ("SELECT Surname1,Surname2,FirstName,Photo,DATE_FORMAT(Birthday,'%%Y%%m%%d')" - " FROM usr_data WHERE UsrCod=%ld", - UsrDat->UsrCod); - - /* Check number of rows in result */ - if (DB_QuerySELECT_new (&mysql_res,"can not get user's data") != 1) + if (DB_QuerySELECT (&mysql_res,"can not get user's data", + "SELECT Surname1,Surname2,FirstName,Photo,DATE_FORMAT(Birthday,'%%Y%%m%%d')" + " FROM usr_data WHERE UsrCod=%ld", + UsrDat->UsrCod) != 1) return false; /* Read some user's data */ @@ -575,10 +574,10 @@ static bool Svc_GetSomeUsrDataFromUsrCod (struct UsrData *UsrDat,long CrsCod) if (CrsCod > 0) { /* Get the role in the given course */ - DB_BuildQuery ("SELECT Role FROM crs_usr" - " WHERE CrsCod=%ld AND UsrCod=%ld", - CrsCod,UsrDat->UsrCod); - if (DB_QuerySELECT_new (&mysql_res,"can not get user's role")) // User belongs to course + if (DB_QuerySELECT (&mysql_res,"can not get user's role", + "SELECT Role FROM crs_usr" + " WHERE CrsCod=%ld AND UsrCod=%ld", + CrsCod,UsrDat->UsrCod)) // User belongs to course { row = mysql_fetch_row (mysql_res); if (row[0]) @@ -606,10 +605,10 @@ static bool Svc_GetSomeUsrDataFromUsrCod (struct UsrData *UsrDat,long CrsCod) else { /* Get the maximum role in any course */ - DB_BuildQuery ("SELECT MAX(Role)" - " FROM crs_usr WHERE UsrCod=%ld", - UsrDat->UsrCod); - if (DB_QuerySELECT_new (&mysql_res,"can not get user's role") == 1) + if (DB_QuerySELECT (&mysql_res,"can not get user's role", + "SELECT MAX(Role)" + " FROM crs_usr WHERE UsrCod=%ld", + UsrDat->UsrCod) == 1) { row = mysql_fetch_row (mysql_res); if (row[0]) @@ -827,23 +826,27 @@ int swad__loginByUserPasswordKey (struct soap *soap, Str_RemoveLeadingArrobas (UsrIDNickOrEmail); /* User has typed a nickname */ - DB_BuildQuery ("SELECT usr_nicknames.UsrCod" - " FROM usr_nicknames,usr_data" - " WHERE usr_nicknames.Nickname='%s'" - " AND usr_nicknames.UsrCod=usr_data.UsrCod" - " AND usr_data.Password='%s'", - UsrIDNickOrEmail,userPassword); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get user's data", + "SELECT usr_nicknames.UsrCod" + " FROM usr_nicknames,usr_data" + " WHERE usr_nicknames.Nickname='%s'" + " AND usr_nicknames.UsrCod=usr_data.UsrCod" + " AND usr_data.Password='%s'", + UsrIDNickOrEmail,userPassword); } else if (Mai_CheckIfEmailIsValid (Gbl.Usrs.Me.UsrIdLogin)) // 2: It's an email { /* User has typed an email */ // TODO: Get only if email confirmed? - DB_BuildQuery ("SELECT usr_emails.UsrCod" - " FROM usr_emails,usr_data" - " WHERE usr_emails.E_mail='%s'" - " AND usr_emails.UsrCod=usr_data.UsrCod" - " AND usr_data.Password='%s'", - UsrIDNickOrEmail,userPassword); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get user's data", + "SELECT usr_emails.UsrCod" + " FROM usr_emails,usr_data" + " WHERE usr_emails.E_mail='%s'" + " AND usr_emails.UsrCod=usr_data.UsrCod" + " AND usr_data.Password='%s'", + UsrIDNickOrEmail,userPassword); } else // 3: It's not a nickname nor email { @@ -854,11 +857,14 @@ int swad__loginByUserPasswordKey (struct soap *soap, { /* User has typed a valid user's ID (existing or not) */ // TODO: Get only if ID confirmed? - DB_BuildQuery ("SELECT usr_IDs.UsrCod FROM usr_IDs,usr_data" - " WHERE usr_IDs.UsrID='%s'" - " AND usr_IDs.UsrCod=usr_data.UsrCod" - " AND usr_data.Password='%s'", - UsrIDNickOrEmail,userPassword); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get user's data", + "SELECT usr_IDs.UsrCod" + " FROM usr_IDs,usr_data" + " WHERE usr_IDs.UsrID='%s'" + " AND usr_IDs.UsrCod=usr_data.UsrCod" + " AND usr_data.Password='%s'", + UsrIDNickOrEmail,userPassword); } else // String is not a valid user's nickname, email or ID return soap_receiver_fault (Gbl.soap, @@ -867,7 +873,7 @@ int swad__loginByUserPasswordKey (struct soap *soap, } /***** Get user's data from database *****/ - if ((NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get user's data")) == 1) // User found in table of users' data + if (NumRows == 1) // User found in table of users' data { row = mysql_fetch_row (mysql_res); @@ -1002,10 +1008,12 @@ int swad__loginBySessionKey (struct soap *soap, // Now, we know that sessionID is a valid session identifier /***** Query data of the session from database *****/ - DB_BuildQuery ("SELECT UsrCod,DegCod,CrsCod FROM sessions" - " WHERE SessionId='%s'", - sessionID); - if ((NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get session data")) == 1) // Session found in table of sessions + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get session data", + "SELECT UsrCod,DegCod,CrsCod FROM sessions" + " WHERE SessionId='%s'", + sessionID); + if (NumRows == 1) // Session found in table of sessions { row = mysql_fetch_row (mysql_res); @@ -1116,15 +1124,21 @@ int swad__getNewPassword (struct soap *soap, Str_RemoveLeadingArrobas (UsrIDNickOrEmail); /* User has typed a nickname */ - DB_BuildQuery ("SELECT UsrCod FROM usr_nicknames WHERE Nickname='%s'", - UsrIDNickOrEmail); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get user's data", + "SELECT UsrCod FROM usr_nicknames" + " WHERE Nickname='%s'", + UsrIDNickOrEmail); } else if (Mai_CheckIfEmailIsValid (Gbl.Usrs.Me.UsrIdLogin)) // 2: It's an email { /* User has typed an email */ // TODO: Get only if email confirmed? - DB_BuildQuery ("SELECT UsrCod FROM usr_emails WHERE E_mail='%s'", - UsrIDNickOrEmail); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get user's data", + "SELECT UsrCod FROM usr_emails" + " WHERE E_mail='%s'", + UsrIDNickOrEmail); } else // 3: It's not a nickname nor email { @@ -1135,8 +1149,11 @@ int swad__getNewPassword (struct soap *soap, { /* User has typed a valid user's ID (existing or not) */ // TODO: Get only if ID confirmed? - DB_BuildQuery ("SELECT UsrCod FROM usr_IDs WHERE UsrID='%s'", - UsrIDNickOrEmail); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get user's data", + "SELECT UsrCod FROM usr_IDs" + " WHERE UsrID='%s'", + UsrIDNickOrEmail); } else // String is not a valid user's nickname, email or ID return soap_receiver_fault (Gbl.soap, @@ -1145,7 +1162,7 @@ int swad__getNewPassword (struct soap *soap, } /***** Get user's data from database *****/ - if ((NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get user's data")) == 1) // One unique user found in table of users' data + if (NumRows == 1) // One unique user found in table of users' data { row = mysql_fetch_row (mysql_res); @@ -1204,11 +1221,17 @@ int swad__getCourses (struct soap *soap, Gbl.Usrs.Me.Role.Logged = Gbl.Usrs.Me.UsrDat.Roles.InCurrentCrs.Role; /***** Query my courses from database *****/ - DB_BuildQuery ("SELECT courses.CrsCod,courses.ShortName,courses.FullName,crs_usr.Role FROM crs_usr,courses" - " WHERE crs_usr.UsrCod=%ld AND crs_usr.CrsCod=courses.CrsCod" - " ORDER BY courses.FullName", - Gbl.Usrs.Me.UsrDat.UsrCod); - NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get user's courses"); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get user's courses", + "SELECT courses.CrsCod," + "courses.ShortName," + "courses.FullName," + "crs_usr.Role" + " FROM crs_usr,courses" + " WHERE crs_usr.UsrCod=%ld" + " AND crs_usr.CrsCod=courses.CrsCod" + " ORDER BY courses.FullName", + Gbl.Usrs.Me.UsrDat.UsrCod); getCoursesOut->numCourses = (int) NumRows; getCoursesOut->coursesArray.__size = (int) NumRows; @@ -1667,12 +1690,17 @@ int swad__getGroupTypes (struct soap *soap, "Requester must belong to course"); /***** Query group types in a course from database *****/ - DB_BuildQuery ("SELECT GrpTypCod,GrpTypName,Mandatory,Multiple,UNIX_TIMESTAMP(OpenTime)" - " FROM crs_grp_types" - " WHERE CrsCod=%d" - " ORDER BY GrpTypName", - courseCode); - NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get group types"); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get group types", + "SELECT GrpTypCod," + "GrpTypName," + "Mandatory," + "Multiple," + "UNIX_TIMESTAMP(OpenTime)" + " FROM crs_grp_types" + " WHERE CrsCod=%d" + " ORDER BY GrpTypName", + courseCode); getGroupTypesOut->numGroupTypes = (int) NumRows; getGroupTypesOut->groupTypesArray.__size = (int) NumRows; @@ -1775,15 +1803,21 @@ int swad__getGroups (struct soap *soap, "Requester must belong to course"); /***** Query groups in a course from database *****/ - DB_BuildQuery ("SELECT crs_grp_types.GrpTypCod,crs_grp_types.GrpTypName," - "crs_grp.GrpCod,crs_grp.GrpName," - "crs_grp.MaxStudents,crs_grp.Open,crs_grp.FileZones" - " FROM crs_grp_types,crs_grp" - " WHERE crs_grp_types.CrsCod=%d" - " AND crs_grp_types.GrpTypCod=crs_grp.GrpTypCod" - " ORDER BY crs_grp_types.GrpTypName,crs_grp.GrpName", - courseCode); - NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get user's groups"); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get user's groups", + "SELECT crs_grp_types.GrpTypCod," + "crs_grp_types.GrpTypName," + "crs_grp.GrpCod," + "crs_grp.GrpName," + "crs_grp.MaxStudents," + "crs_grp.Open," + "crs_grp.FileZones" + " FROM crs_grp_types,crs_grp" + " WHERE crs_grp_types.CrsCod=%d" + " AND crs_grp_types.GrpTypCod=crs_grp.GrpTypCod" + " ORDER BY crs_grp_types.GrpTypName," + "crs_grp.GrpName", + courseCode); getGroupsOut->numGroups = (int) NumRows; getGroupsOut->groupsArray.__size = (int) NumRows; @@ -1861,7 +1895,8 @@ int swad__sendMyGroups (struct soap *soap, unsigned NumGrp; MYSQL_RES *mysql_res; MYSQL_ROW row; - unsigned NumRow,NumRows; + unsigned NumRow; + unsigned NumRows; long GrpCod; unsigned MaxStudents; @@ -1936,15 +1971,20 @@ int swad__sendMyGroups (struct soap *soap, Grp_FreeListCodGrp (&LstGrpsIWant); /***** Query groups in a course from database *****/ - DB_BuildQuery ("SELECT crs_grp_types.GrpTypCod,crs_grp_types.GrpTypName," - "crs_grp.GrpCod,crs_grp.GrpName," - "crs_grp.MaxStudents,crs_grp.Open,crs_grp.FileZones" - " FROM crs_grp_types,crs_grp" - " WHERE crs_grp_types.CrsCod=%d" - " AND crs_grp_types.GrpTypCod=crs_grp.GrpTypCod" - " ORDER BY crs_grp_types.GrpTypName,crs_grp.GrpName", - courseCode); - NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get user's groups"); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get user's groups", + "SELECT crs_grp_types.GrpTypCod," + "crs_grp_types.GrpTypName," + "crs_grp.GrpCod," + "crs_grp.GrpName," + "crs_grp.MaxStudents," + "crs_grp.Open," + "crs_grp.FileZones" + " FROM crs_grp_types,crs_grp" + " WHERE crs_grp_types.CrsCod=%d" + " AND crs_grp_types.GrpTypCod=crs_grp.GrpTypCod" + " ORDER BY crs_grp_types.GrpTypName,crs_grp.GrpName", + courseCode); SendMyGroupsOut->numGroups = (int) NumRows; SendMyGroupsOut->groupsArray.__size = (int) NumRows; @@ -2073,6 +2113,7 @@ int swad__getAttendanceEvents (struct soap *soap, int ReturnCode; MYSQL_RES *mysql_res; MYSQL_ROW row; + unsigned NumRows; int NumAttEvent; long AttCod; char PhotoURL[Cns_MAX_BYTES_WWW + 1]; @@ -2114,16 +2155,19 @@ int swad__getAttendanceEvents (struct soap *soap, "Requester must be a teacher"); /***** Query list of attendance events *****/ - DB_BuildQuery ("SELECT AttCod,Hidden,UsrCod," - "UNIX_TIMESTAMP(StartTime) AS ST," - "UNIX_TIMESTAMP(EndTime) AS ET," - "CommentTchVisible,Title,Txt" - " FROM att_events" - " WHERE CrsCod=%d" - " ORDER BY ST DESC,ET DESC,Title DESC", - courseCode); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get attendance events", + "SELECT AttCod,Hidden,UsrCod," + "UNIX_TIMESTAMP(StartTime) AS ST," + "UNIX_TIMESTAMP(EndTime) AS ET," + "CommentTchVisible,Title,Txt" + " FROM att_events" + " WHERE CrsCod=%d" + " ORDER BY ST DESC,ET DESC,Title DESC", + courseCode); + getAttendanceEventsOut->eventsArray.__size = - getAttendanceEventsOut->numEvents = (int) DB_QuerySELECT_new (&mysql_res,"can not get attendance events"); + getAttendanceEventsOut->numEvents = (int) NumRows; if (getAttendanceEventsOut->numEvents == 0) getAttendanceEventsOut->eventsArray.__ptr = NULL; @@ -2237,9 +2281,12 @@ static void Svc_GetListGrpsInAttendanceEventFromDB (long AttCod,char **ListGroup size_t Length; /***** Get list of groups *****/ - DB_BuildQuery ("SELECT GrpCod FROM att_grp WHERE AttCod=%ld", - AttCod); - if ((NumGrps = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get groups of an attendance event")) == 0) + NumGrps = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get groups" + " of an attendance event", + "SELECT GrpCod FROM att_grp WHERE AttCod=%ld", + AttCod); + if (NumGrps == 0) *ListGroups = NULL; else // Events found { @@ -2566,14 +2613,18 @@ int swad__getAttendanceUsers (struct soap *soap, (unsigned) Rol_STD, Att.AttCod); // Query: list of users in attendance list + rest of users (subquery) - DB_BuildQuery ("SELECT u.UsrCod,u.Present FROM " - "(SELECT UsrCod,Present" - " FROM att_usr WHERE AttCod=%ld" - " UNION %s) AS u,usr_data" - " WHERE u.UsrCod=usr_data.UsrCod" - " ORDER BY usr_data.Surname1,usr_data.Surname2,usr_data.FirstName", - (long) attendanceEventCode,SubQuery); - NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get users in an attendance event"); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get users" + " in an attendance event", + "SELECT u.UsrCod,u.Present FROM " + "(SELECT UsrCod,Present" + " FROM att_usr WHERE AttCod=%ld" + " UNION %s) AS u,usr_data" + " WHERE u.UsrCod=usr_data.UsrCod" + " ORDER BY usr_data.Surname1," + "usr_data.Surname2," + "usr_data.FirstName", + (long) attendanceEventCode,SubQuery); getAttendanceUsersOut->numUsers = (int) NumRows; getAttendanceUsersOut->usersArray.__size = (int) NumRows; @@ -2853,13 +2904,23 @@ int swad__getNotifications (struct soap *soap, return ReturnCode; /***** Get my notifications from database *****/ - DB_BuildQuery ("SELECT NtfCod,NotifyEvent,UNIX_TIMESTAMP(TimeNotif)," - "FromUsrCod,InsCod,CtrCod,DegCod,CrsCod,Cod,Status" - " FROM notif" - " WHERE ToUsrCod=%ld AND TimeNotif>=FROM_UNIXTIME(%ld)" - " ORDER BY TimeNotif DESC", - Gbl.Usrs.Me.UsrDat.UsrCod,beginTime); - NumNotifications = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get user's notifications"); + NumNotifications = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get user's notifications", + "SELECT NtfCod," // row[0] + "NotifyEvent," // row[1] + "UNIX_TIMESTAMP(TimeNotif)," // row[2] + "FromUsrCod," // row[3] + "InsCod," // row[4] + "CtrCod," // row[5] + "DegCod," // row[6] + "CrsCod," // row[7] + "Cod," // row[8] + "Status" // row[9] + " FROM notif" + " WHERE ToUsrCod=%ld AND TimeNotif>=FROM_UNIXTIME(%ld)" + " ORDER BY TimeNotif DESC", + Gbl.Usrs.Me.UsrDat.UsrCod,beginTime); + if (NumNotifications) // Notifications found { getNotificationsOut->numNotifications = (int) NumNotifications; @@ -3039,10 +3100,10 @@ static int Svc_GetMyLanguage (void) Txt_Language_t Lan; /***** Get user's language *****/ - DB_BuildQuery ("SELECT Language FROM usr_data" - " WHERE UsrCod=%ld", - Gbl.Usrs.Me.UsrDat.UsrCod); - if (DB_QuerySELECT_new (&mysql_res,"can not get user's language") != 1) + if (DB_QuerySELECT (&mysql_res,"can not get user's language", + "SELECT Language FROM usr_data" + " WHERE UsrCod=%ld", + Gbl.Usrs.Me.UsrDat.UsrCod) != 1) return soap_receiver_fault (Gbl.soap, "Can not get user's language from database", "User doen't exist in database"); @@ -3181,15 +3242,15 @@ int swad__sendMessage (struct soap *soap, if (messageCode) { /***** Check if the original message was really received by me *****/ - DB_BuildQuery ("SELECT SUM(N) FROM" - " (SELECT COUNT(*) AS N FROM msg_rcv" - " WHERE UsrCod=%ld AND MsgCod=%ld" - " UNION" - " SELECT COUNT(*) AS N FROM msg_rcv_deleted" - " WHERE UsrCod=%ld AND MsgCod=%ld) AS T", - Gbl.Usrs.Me.UsrDat.UsrCod,(long) messageCode, - Gbl.Usrs.Me.UsrDat.UsrCod,(long) messageCode); - if (!DB_QuerySELECT_new (&mysql_res,"can not check original message")) + if (!DB_QuerySELECT (&mysql_res,"can not check original message", + "SELECT SUM(N) FROM" + " (SELECT COUNT(*) AS N FROM msg_rcv" + " WHERE UsrCod=%ld AND MsgCod=%ld" + " UNION" + " SELECT COUNT(*) AS N FROM msg_rcv_deleted" + " WHERE UsrCod=%ld AND MsgCod=%ld) AS T", + Gbl.Usrs.Me.UsrDat.UsrCod,(long) messageCode, + Gbl.Usrs.Me.UsrDat.UsrCod,(long) messageCode)) return soap_sender_fault (Gbl.soap, "Can not check original message", "Error reading from database"); @@ -3210,13 +3271,15 @@ int swad__sendMessage (struct soap *soap, "Original message does not exist"); /***** Get the recipient of the message *****/ - DB_BuildQuery ("SELECT UsrCod FROM msg_snt" - " WHERE MsgCod=%ld" - " UNION " - "SELECT UsrCod FROM msg_snt_deleted" - " WHERE MsgCod=%ld", - (long) messageCode,(long) messageCode); - if ((NumRows = DB_QuerySELECT_new (&mysql_res,"can not check original message"))) // Message found in any of the two tables of sent messages + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not check original message", + "SELECT UsrCod FROM msg_snt" + " WHERE MsgCod=%ld" + " UNION " + "SELECT UsrCod FROM msg_snt_deleted" + " WHERE MsgCod=%ld", + (long) messageCode,(long) messageCode); + if (NumRows) // Message found in any of the two tables of sent messages { row = mysql_fetch_row (mysql_res); ReplyUsrCod = Str_ConvertStrCodToLongCod (row[0]); @@ -3571,10 +3634,10 @@ static int Svc_GetTstConfig (long CrsCod) MYSQL_ROW row; /***** Query database *****/ - DB_BuildQuery ("SELECT Pluggable,Min,Def,Max,MinTimeNxtTstPerQst,Feedback" - " FROM tst_config WHERE CrsCod=%ld", - CrsCod); - if (DB_QuerySELECT_new (&mysql_res,"can not get test configuration")) + if (DB_QuerySELECT (&mysql_res,"can not get test configuration", + "SELECT Pluggable,Min,Def,Max,MinTimeNxtTstPerQst,Feedback" + " FROM tst_config WHERE CrsCod=%ld", + CrsCod)) { /***** Get minimun, default and maximum *****/ row = mysql_fetch_row (mysql_res); @@ -3714,12 +3777,12 @@ static int Svc_GetTstTags (long CrsCod,struct swad__getTestsOutput *getTestsOut) unsigned NumRows; /***** Get available tags from database *****/ - DB_BuildQuery ("SELECT TagCod,TagTxt" - " FROM tst_tags" - " WHERE CrsCod=%ld AND TagHidden='N'" - " ORDER BY TagTxt", - CrsCod); - NumRows = DB_QuerySELECT_new (&mysql_res,"can not get test tags"); + NumRows = DB_QuerySELECT (&mysql_res,"can not get test tags", + "SELECT TagCod,TagTxt" + " FROM tst_tags" + " WHERE CrsCod=%ld AND TagHidden='N'" + " ORDER BY TagTxt", + CrsCod); getTestsOut->tagsArray.__size = (int) NumRows; @@ -3767,29 +3830,30 @@ static int Svc_GetTstQuestions (long CrsCod,long BeginTime,struct swad__getTests /***** Get recent test questions from database *****/ // DISTINCTROW is necessary to not repeat questions - DB_BuildQuery ("SELECT DISTINCTROW tst_questions.QstCod," - "tst_questions.AnsType,tst_questions.Shuffle," - "tst_questions.Stem,tst_questions.Feedback" - " FROM tst_questions,tst_question_tags,tst_tags" - " WHERE tst_questions.CrsCod=%ld" - " AND tst_questions.QstCod NOT IN" - " (SELECT tst_question_tags.QstCod FROM tst_tags,tst_question_tags" - " WHERE tst_tags.CrsCod=%ld AND tst_tags.TagHidden='Y'" - " AND tst_tags.TagCod=tst_question_tags.TagCod)" - " AND tst_questions.QstCod=tst_question_tags.QstCod" - " AND tst_question_tags.TagCod=tst_tags.TagCod" - " AND tst_tags.CrsCod=%ld" - " AND " - "(" - "tst_questions.EditTime>=FROM_UNIXTIME(%ld)" - " OR " - "tst_tags.ChangeTime>=FROM_UNIXTIME(%ld)" - ")" - " ORDER BY QstCod", - CrsCod,CrsCod,CrsCod, - BeginTime, - BeginTime); - NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get test questions"); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get test questions", + "SELECT DISTINCTROW tst_questions.QstCod," + "tst_questions.AnsType,tst_questions.Shuffle," + "tst_questions.Stem,tst_questions.Feedback" + " FROM tst_questions,tst_question_tags,tst_tags" + " WHERE tst_questions.CrsCod=%ld" + " AND tst_questions.QstCod NOT IN" + " (SELECT tst_question_tags.QstCod FROM tst_tags,tst_question_tags" + " WHERE tst_tags.CrsCod=%ld AND tst_tags.TagHidden='Y'" + " AND tst_tags.TagCod=tst_question_tags.TagCod)" + " AND tst_questions.QstCod=tst_question_tags.QstCod" + " AND tst_question_tags.TagCod=tst_tags.TagCod" + " AND tst_tags.CrsCod=%ld" + " AND " + "(" + "tst_questions.EditTime>=FROM_UNIXTIME(%ld)" + " OR " + "tst_tags.ChangeTime>=FROM_UNIXTIME(%ld)" + ")" + " ORDER BY QstCod", + CrsCod,CrsCod,CrsCod, + BeginTime, + BeginTime); getTestsOut->questionsArray.__size = (int) NumRows; @@ -3851,30 +3915,31 @@ static int Svc_GetTstAnswers (long CrsCod,long BeginTime,struct swad__getTestsOu unsigned Index; /***** Get recent test questions from database *****/ - DB_BuildQuery ("SELECT QstCod,AnsInd,Correct,Answer,Feedback" - " FROM tst_answers WHERE QstCod IN " - "(SELECT tst_questions.QstCod" - " FROM tst_questions,tst_question_tags,tst_tags" - " WHERE tst_questions.CrsCod=%ld" - " AND tst_questions.QstCod NOT IN" - " (SELECT tst_question_tags.QstCod FROM tst_tags,tst_question_tags" - " WHERE tst_tags.CrsCod=%ld AND tst_tags.TagHidden='Y'" - " AND tst_tags.TagCod=tst_question_tags.TagCod)" - " AND tst_questions.QstCod=tst_question_tags.QstCod" - " AND tst_question_tags.TagCod=tst_tags.TagCod" - " AND tst_tags.CrsCod=%ld" - " AND " - "(" - "tst_questions.EditTime>=FROM_UNIXTIME(%ld)" - " OR " - "tst_tags.ChangeTime>=FROM_UNIXTIME(%ld)" - ")" - ")" - " ORDER BY QstCod,AnsInd", - CrsCod,CrsCod,CrsCod, - BeginTime, - BeginTime); - NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get test answers"); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get test answers", + "SELECT QstCod,AnsInd,Correct,Answer,Feedback" + " FROM tst_answers WHERE QstCod IN " + "(SELECT tst_questions.QstCod" + " FROM tst_questions,tst_question_tags,tst_tags" + " WHERE tst_questions.CrsCod=%ld" + " AND tst_questions.QstCod NOT IN" + " (SELECT tst_question_tags.QstCod FROM tst_tags,tst_question_tags" + " WHERE tst_tags.CrsCod=%ld AND tst_tags.TagHidden='Y'" + " AND tst_tags.TagCod=tst_question_tags.TagCod)" + " AND tst_questions.QstCod=tst_question_tags.QstCod" + " AND tst_question_tags.TagCod=tst_tags.TagCod" + " AND tst_tags.CrsCod=%ld" + " AND " + "(" + "tst_questions.EditTime>=FROM_UNIXTIME(%ld)" + " OR " + "tst_tags.ChangeTime>=FROM_UNIXTIME(%ld)" + ")" + ")" + " ORDER BY QstCod,AnsInd", + CrsCod,CrsCod,CrsCod, + BeginTime, + BeginTime); getTestsOut->answersArray.__size = (int) NumRows; @@ -3935,30 +4000,31 @@ static int Svc_GetTstQuestionTags (long CrsCod,long BeginTime,struct swad__getTe unsigned Index; /***** Get recent test questions from database *****/ - DB_BuildQuery ("SELECT QstCod,TagCod,TagInd" - " FROM tst_question_tags WHERE QstCod IN " - "(SELECT tst_questions.QstCod" - " FROM tst_questions,tst_question_tags,tst_tags" - " WHERE tst_questions.CrsCod=%ld" - " AND tst_questions.QstCod NOT IN" - " (SELECT tst_question_tags.QstCod FROM tst_tags,tst_question_tags" - " WHERE tst_tags.CrsCod=%ld AND tst_tags.TagHidden='Y'" - " AND tst_tags.TagCod=tst_question_tags.TagCod)" - " AND tst_questions.QstCod=tst_question_tags.QstCod" - " AND tst_question_tags.TagCod=tst_tags.TagCod" - " AND tst_tags.CrsCod=%ld" - " AND " - "(" - "tst_questions.EditTime>=FROM_UNIXTIME(%ld)" - " OR " - "tst_tags.ChangeTime>=FROM_UNIXTIME(%ld)" - ")" - ")" - " ORDER BY QstCod,TagInd", - CrsCod,CrsCod,CrsCod, - BeginTime, - BeginTime); - NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get test question tags"); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get test question tags", + "SELECT QstCod,TagCod,TagInd" + " FROM tst_question_tags WHERE QstCod IN " + "(SELECT tst_questions.QstCod" + " FROM tst_questions,tst_question_tags,tst_tags" + " WHERE tst_questions.CrsCod=%ld" + " AND tst_questions.QstCod NOT IN" + " (SELECT tst_question_tags.QstCod FROM tst_tags,tst_question_tags" + " WHERE tst_tags.CrsCod=%ld AND tst_tags.TagHidden='Y'" + " AND tst_tags.TagCod=tst_question_tags.TagCod)" + " AND tst_questions.QstCod=tst_question_tags.QstCod" + " AND tst_question_tags.TagCod=tst_tags.TagCod" + " AND tst_tags.CrsCod=%ld" + " AND " + "(" + "tst_questions.EditTime>=FROM_UNIXTIME(%ld)" + " OR " + "tst_tags.ChangeTime>=FROM_UNIXTIME(%ld)" + ")" + ")" + " ORDER BY QstCod,TagInd", + CrsCod,CrsCod,CrsCod, + BeginTime, + BeginTime); getTestsOut->questionTagsArray.__size = (int) NumRows; @@ -4095,30 +4161,30 @@ int swad__getTrivialQuestion (struct soap *soap, /***** Start query *****/ Str_SetDecimalPointToUS (); // To print the floating point as a dot - DB_BuildQuery ("SELECT DISTINCTROW tst_questions.QstCod," - "tst_questions.AnsType,tst_questions.Shuffle," - "tst_questions.Stem,tst_questions.Feedback," - "tst_questions.Score/tst_questions.NumHits AS S" - " FROM courses,tst_questions" - " WHERE courses.DegCod IN (%s)" - " AND courses.CrsCod=tst_questions.CrsCod" - " AND tst_questions.AnsType='unique_choice'" - " AND tst_questions.NumHits>0" - " AND tst_questions.QstCod NOT IN" - " (SELECT tst_question_tags.QstCod" - " FROM courses,tst_tags,tst_question_tags" - " WHERE courses.DegCod IN (%s)" - " AND courses.CrsCod=tst_tags.CrsCod" - " AND tst_tags.TagHidden='Y'" - " AND tst_tags.TagCod=tst_question_tags.TagCod)" - " HAVING S>='%f' AND S<='%f'" - " ORDER BY RAND(NOW()) LIMIT 1", - DegreesStr,DegreesStr, - lowerScore,upperScore); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get test questions", + "SELECT DISTINCTROW tst_questions.QstCod," + "tst_questions.AnsType,tst_questions.Shuffle," + "tst_questions.Stem,tst_questions.Feedback," + "tst_questions.Score/tst_questions.NumHits AS S" + " FROM courses,tst_questions" + " WHERE courses.DegCod IN (%s)" + " AND courses.CrsCod=tst_questions.CrsCod" + " AND tst_questions.AnsType='unique_choice'" + " AND tst_questions.NumHits>0" + " AND tst_questions.QstCod NOT IN" + " (SELECT tst_question_tags.QstCod" + " FROM courses,tst_tags,tst_question_tags" + " WHERE courses.DegCod IN (%s)" + " AND courses.CrsCod=tst_tags.CrsCod" + " AND tst_tags.TagHidden='Y'" + " AND tst_tags.TagCod=tst_question_tags.TagCod)" + " HAVING S>='%f' AND S<='%f'" + " ORDER BY RAND(NOW()) LIMIT 1", + DegreesStr,DegreesStr, + lowerScore,upperScore); Str_SetDecimalPointToLocal (); // Return to local system - NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get test questions"); - if (NumRows == 1) // Question found { /* @@ -4184,11 +4250,12 @@ int swad__getTrivialQuestion (struct soap *soap, if (QstCod > 0) { /***** Get answer from database *****/ - DB_BuildQuery ("SELECT QstCod,AnsInd,Correct,Answer,Feedback" - " FROM tst_answers WHERE QstCod=%ld" - " ORDER BY AnsInd", - QstCod); - NumRows = (unsigned) DB_QuerySELECT_new (&mysql_res,"can not get test answers"); + NumRows = + (unsigned) DB_QuerySELECT (&mysql_res,"can not get test answers", + "SELECT QstCod,AnsInd,Correct,Answer,Feedback" + " FROM tst_answers WHERE QstCod=%ld" + " ORDER BY AnsInd", + QstCod); getTrivialQuestionOut->answersArray.__size = (int) NumRows;