From b5ff64cb8d24acb53221a19a947a8310ea72efb0 Mon Sep 17 00:00:00 2001 From: acanas Date: Thu, 28 Oct 2021 18:48:21 +0200 Subject: [PATCH] Version 21.47.1: Oct 28, 2021 Queries moved to module swad_questions_database. --- swad_API.c | 1 + swad_changelog.h | 3 +- swad_exam_set.c | 5 ++- swad_match.c | 1 + swad_question.c | 95 +++++++--------------------------------- swad_question.h | 5 --- swad_question_database.c | 60 +++++++++++++++++++++++++ swad_question_database.h | 4 ++ swad_test.c | 2 +- 9 files changed, 88 insertions(+), 88 deletions(-) diff --git a/swad_API.c b/swad_API.c index 0d92f96f..58927218 100644 --- a/swad_API.c +++ b/swad_API.c @@ -119,6 +119,7 @@ cp -f /home/acanas/swad/swad/swad /var/www/cgi-bin/ #include "swad_notice.h" #include "swad_notification.h" #include "swad_password.h" +#include "swad_question_database.h" #include "swad_role.h" #include "swad_search.h" #include "swad_test_config.h" diff --git a/swad_changelog.h b/swad_changelog.h index 7ca46f67..0d3410b7 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -602,13 +602,14 @@ TODO: FIX BUG, URGENT! En las fechas como par TODO: En las encuestas, que los estudiantes no puedan ver los resultados hasta que no finalice el plazo. */ -#define Log_PLATFORM_VERSION "SWAD 21.47 (2021-10-27)" +#define Log_PLATFORM_VERSION "SWAD 21.47.1 (2021-10-28)" #define CSS_FILE "swad20.45.css" #define JS_FILE "swad20.69.1.js" /* TODO: Rename CENTRE to CENTER in help wiki. TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams + Version 21.47.1: Oct 28, 2021 Queries moved to module swad_questions_database. (321231 lines) Version 21.47: Oct 27, 2021 New module swad_questions_database for database queries related to test/exam/game questions. (321236 lines) Version 21.46.5: Oct 27, 2021 Queries moved to module swad_test_database. (321140 lines) Version 21.46.4: Oct 27, 2021 Code refactoring in test prints. (321059 lines) diff --git a/swad_exam_set.c b/swad_exam_set.c index 5df5e78e..46167cd3 100644 --- a/swad_exam_set.c +++ b/swad_exam_set.c @@ -49,6 +49,7 @@ #include "swad_match.h" #include "swad_match_result.h" #include "swad_pagination.h" +#include "swad_question_database.h" #include "swad_role.h" #include "swad_test.h" #include "swad_test_visibility.h" @@ -1354,8 +1355,8 @@ static void ExaSet_CopyQstFromBankToExamSet (const struct ExaSet_Set *Set,long Q QstCodInSet = Exa_DB_AddQuestionToSet (Set->SetCod,&Question,CloneMedCod); /***** Get the answers from the database *****/ - Question.Answer.NumOptions = Qst_DB_GetAnswersQst (&mysql_res,&Question, - false); // Don't shuffle + Question.Answer.NumOptions = Qst_DB_GetDataOfAnswers (&mysql_res,Question.QstCod, + false); // Don't shuffle /* row[0] AnsInd row[1] Answer diff --git a/swad_match.c b/swad_match.c index c52f6772..f599008c 100644 --- a/swad_match.c +++ b/swad_match.c @@ -44,6 +44,7 @@ #include "swad_match.h" #include "swad_match_database.h" #include "swad_match_result.h" +#include "swad_question_database.h" #include "swad_role.h" #include "swad_setting.h" #include "swad_test.h" diff --git a/swad_question.c b/swad_question.c index 33ca079a..b776c790 100644 --- a/swad_question.c +++ b/swad_question.c @@ -761,7 +761,7 @@ void Qst_ListQuestionsToEdit (void) { Mnu_ContextMenuBegin (); QstImp_CreateXML (Questions.NumQsts,mysql_res); // Create XML file with exported questions... - // ...and put a link to download it + // ...and put a link to download it Mnu_ContextMenuEnd (); } @@ -1375,49 +1375,6 @@ void Qst_PutParamsEditQst (void *Questions) } } -/*****************************************************************************/ -/************ Get number of answers of a question from database **************/ -/*****************************************************************************/ - -unsigned Qst_DB_GetNumAnswersQst (long QstCod) - { - return (unsigned) - DB_QueryCOUNT ("can not get number of answers of a question", - "SELECT COUNT(*)" - " FROM tst_answers" - " WHERE QstCod=%ld", - QstCod); - } - -/*****************************************************************************/ -/***************** Get answers of a question from database *******************/ -/*****************************************************************************/ - -unsigned Qst_DB_GetAnswersQst (MYSQL_RES **mysql_res, - const struct Qst_Question *Question, - bool Shuffle) - { - unsigned NumOptions; - - /***** Get answers of a question from database *****/ - if (!(NumOptions = (unsigned) - DB_QuerySELECT (mysql_res,"can not get answers of a question", - "SELECT AnsInd," // row[0] - "Answer," // row[1] - "Feedback," // row[2] - "MedCod," // row[3] - "Correct" // row[4] - " FROM tst_answers" - " WHERE QstCod=%ld" - " ORDER BY %s", - Question->QstCod, - Shuffle ? "RAND()" : - "AnsInd"))) - Err_WrongAnswerExit (); - - return NumOptions; - } - /*****************************************************************************/ /**************** Get and write the answers of a test question ***************/ /*****************************************************************************/ @@ -1591,12 +1548,7 @@ void Qst_GetCorrectIntAnswerFromDB (struct Qst_Question *Question) MYSQL_ROW row; /***** Query database *****/ - Question->Answer.NumOptions = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get answers of a question", - "SELECT Answer" // row[0] - " FROM tst_answers" - " WHERE QstCod=%ld", - Question->QstCod); + Question->Answer.NumOptions = Qst_DB_GetTextOfAnswers (&mysql_res,Question->QstCod); /***** Check if number of rows is correct *****/ Qst_CheckIfNumberOfAnswersIsOne (Question); @@ -1618,12 +1570,7 @@ void Qst_GetCorrectFltAnswerFromDB (struct Qst_Question *Question) double Tmp; /***** Query database *****/ - Question->Answer.NumOptions = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get answers of a question", - "SELECT Answer" // row[0] - " FROM tst_answers" - " WHERE QstCod=%ld", - Question->QstCod); + Question->Answer.NumOptions = Qst_DB_GetTextOfAnswers (&mysql_res,Question->QstCod); /***** Check if number of rows is correct *****/ if (Question->Answer.NumOptions != 2) @@ -1656,12 +1603,7 @@ void Qst_GetCorrectTF_AnswerFromDB (struct Qst_Question *Question) MYSQL_ROW row; /***** Query database *****/ - Question->Answer.NumOptions = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get answers of a question", - "SELECT Answer" // row[0] - " FROM tst_answers" - " WHERE QstCod=%ld", - Question->QstCod); + Question->Answer.NumOptions = Qst_DB_GetTextOfAnswers (&mysql_res,Question->QstCod); /***** Check if number of rows is correct *****/ Qst_CheckIfNumberOfAnswersIsOne (Question); @@ -1681,13 +1623,9 @@ void Qst_GetCorrectChoAnswerFromDB (struct Qst_Question *Question) unsigned NumOpt; /***** Query database *****/ - Question->Answer.NumOptions = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get answers of a question", - "SELECT Correct" // row[0] - " FROM tst_answers" - " WHERE QstCod=%ld" - " ORDER BY AnsInd", - Question->QstCod); + Question->Answer.NumOptions = Qst_DB_GetTextOfAnswers (&mysql_res,Question->QstCod); + + /***** Get options *****/ for (NumOpt = 0; NumOpt < Question->Answer.NumOptions; NumOpt++) @@ -1710,14 +1648,10 @@ void Qst_GetCorrectTxtAnswerFromDB (struct Qst_Question *Question) unsigned NumOpt; /***** Query database *****/ - Question->Answer.NumOptions = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get answers of a question", - "SELECT Answer" // row[0] - " FROM tst_answers" - " WHERE QstCod=%ld", - Question->QstCod); + Question->Answer.NumOptions = Qst_DB_GetTextOfAnswers (&mysql_res,Question->QstCod); - /***** Get text and correctness of answers for this question from database (one row per answer) *****/ + /***** Get text and correctness of answers for this question + from database (one row per answer) *****/ for (NumOpt = 0; NumOpt < Question->Answer.NumOptions; NumOpt++) @@ -1815,7 +1749,10 @@ void Qst_GetQuestions (struct Qst_Questions *Questions,MYSQL_RES **mysql_res) Str_Copy (Query,"SELECT tst_questions.QstCod" // row[0] " FROM tst_questions",Qst_MAX_BYTES_QUERY_QUESTIONS); if (!Questions->Tags.All) - Str_Concat (Query,",tst_question_tags,tst_tags",Qst_MAX_BYTES_QUERY_QUESTIONS); + Str_Concat (Query,"," + "tst_question_tags," + "tst_tags", + Qst_MAX_BYTES_QUERY_QUESTIONS); Str_Concat (Query," WHERE tst_questions.CrsCod='",Qst_MAX_BYTES_QUERY_QUESTIONS); snprintf (CrsCodStr,sizeof (CrsCodStr),"%ld",Gbl.Hierarchy.Crs.CrsCod); @@ -2764,8 +2701,8 @@ bool Qst_GetQstDataFromDB (struct Qst_Question *Question) DB_FreeMySQLResult (&mysql_res); /***** Get the answers from the database *****/ - Question->Answer.NumOptions = Qst_DB_GetAnswersQst (&mysql_res,Question, - false); // Don't shuffle + Question->Answer.NumOptions = Qst_DB_GetDataOfAnswers (&mysql_res,Question->QstCod, + false); // Don't shuffle /* row[0] AnsInd row[1] Answer diff --git a/swad_question.h b/swad_question.h index 9b0eb77c..f9f50ffc 100644 --- a/swad_question.h +++ b/swad_question.h @@ -177,11 +177,6 @@ void Qst_WriteQuestionRowForSelection (unsigned QstInd, void Qst_PutParamsEditQst (void *Questions); -unsigned Qst_DB_GetNumAnswersQst (long QstCod); -unsigned Qst_DB_GetAnswersQst (MYSQL_RES **mysql_res, - const struct Qst_Question *Question, - bool Shuffle); - void Qst_WriteAnswersBank (struct Qst_Question *Question, const char *ClassTxt, const char *ClassFeedback); diff --git a/swad_question_database.c b/swad_question_database.c index 912f1fb9..35029038 100644 --- a/swad_question_database.c +++ b/swad_question_database.c @@ -755,3 +755,63 @@ unsigned Qst_DB_GetNumCrssWithPluggableQsts (HieLvl_Level_t Scope, return 0; // Not reached } } + +/*****************************************************************************/ +/************ Get number of answers of a question from database **************/ +/*****************************************************************************/ + +unsigned Qst_DB_GetNumAnswersQst (long QstCod) + { + return (unsigned) + DB_QueryCOUNT ("can not get number of answers of a question", + "SELECT COUNT(*)" + " FROM tst_answers" + " WHERE QstCod=%ld", + QstCod); + } + +/*****************************************************************************/ +/***************** Get answers of a question from database *******************/ +/*****************************************************************************/ + +unsigned Qst_DB_GetDataOfAnswers (MYSQL_RES **mysql_res,long QstCod,bool Shuffle) + { + unsigned NumOptions; + + if (!(NumOptions = (unsigned) + DB_QuerySELECT (mysql_res,"can not get answers of a question", + "SELECT AnsInd," // row[0] + "Answer," // row[1] + "Feedback," // row[2] + "MedCod," // row[3] + "Correct" // row[4] + " FROM tst_answers" + " WHERE QstCod=%ld" + " ORDER BY %s", + QstCod, + Shuffle ? "RAND()" : + "AnsInd"))) + Err_WrongAnswerExit (); + + return NumOptions; + } + +/*****************************************************************************/ +/***************** Get answers of a question from database *******************/ +/*****************************************************************************/ + +unsigned Qst_DB_GetTextOfAnswers (MYSQL_RES **mysql_res,long QstCod) + { + unsigned NumOptions; + + if (!(NumOptions = (unsigned) + DB_QuerySELECT (mysql_res,"can not get answers of a question", + "SELECT Answer" // row[0] + " FROM tst_answers" + " WHERE QstCod=%ld" + " ORDER BY AnsInd", + QstCod))) + Err_WrongAnswerExit (); + + return NumOptions; + } diff --git a/swad_question_database.h b/swad_question_database.h index 3152a738..870018f4 100644 --- a/swad_question_database.h +++ b/swad_question_database.h @@ -60,4 +60,8 @@ unsigned Qst_DB_GetNumCrssWithQsts (HieLvl_Level_t Scope, unsigned Qst_DB_GetNumCrssWithPluggableQsts (HieLvl_Level_t Scope, Qst_AnswerType_t AnsType); +unsigned Qst_DB_GetNumAnswersQst (long QstCod); +unsigned Qst_DB_GetDataOfAnswers (MYSQL_RES **mysql_res,long QstCod,bool Shuffle); +unsigned Qst_DB_GetTextOfAnswers (MYSQL_RES **mysql_res,long QstCod); + #endif diff --git a/swad_test.c b/swad_test.c index b5641236..190c5737 100644 --- a/swad_test.c +++ b/swad_test.c @@ -583,7 +583,7 @@ static void Tst_GenerateChoiceIndexes (struct TstPrn_PrintedQuestion *PrintedQue Question.QstCod = PrintedQuestion->QstCod; /***** Get answers of question from database *****/ - Question.Answer.NumOptions = Qst_DB_GetAnswersQst (&mysql_res,&Question,Shuffle); + Question.Answer.NumOptions = Qst_DB_GetDataOfAnswers (&mysql_res,Question.QstCod,Shuffle); /* row[0] AnsInd row[1] Answer