Version 21.47.1: Oct 28, 2021 Queries moved to module swad_questions_database.

This commit is contained in:
acanas 2021-10-28 18:48:21 +02:00
parent 72eb33e914
commit b5ff64cb8d
9 changed files with 88 additions and 88 deletions

View File

@ -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"

View File

@ -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)

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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);

View File

@ -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;
}

View File

@ -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

View File

@ -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