diff --git a/swad_changelog.h b/swad_changelog.h index c505a6ff..13cb2aeb 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 20.96.3 (2021-09-08)" +#define Log_PLATFORM_VERSION "SWAD 20.96.4 (2021-09-08)" #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 20.96.4: Sep 08, 2021 Queries moved to module swad_exam_database. (314414 lines) Version 20.96.3: Sep 08, 2021 Queries moved to module swad_exam_database. (314384 lines) Version 20.96.2: Sep 08, 2021 Queries moved to module swad_exam_database. (314362 lines) Version 20.96.1: Jul 30, 2021 Fix bugs removing exams. (314373 lines) diff --git a/swad_exam_database.c b/swad_exam_database.c index 56af4bc3..51c9ddc0 100644 --- a/swad_exam_database.c +++ b/swad_exam_database.c @@ -470,6 +470,37 @@ void Exa_DB_RemoveSetsFromCrs (long CrsCod) CrsCod); } +/*****************************************************************************/ +/******************* Insert question in table of questions *******************/ +/*****************************************************************************/ + +long Exa_DB_AddQuestionToSet (long SetCod,const struct Tst_Question *Question,long MedCod) + { + extern const char *Tst_StrAnswerTypesDB[Tst_NUM_ANS_TYPES]; + static char CharInvalid[Tst_NUM_VALIDITIES] = + { + [Tst_INVALID_QUESTION] = 'Y', + [Tst_VALID_QUESTION ] = 'N' + }; + + return + DB_QueryINSERTandReturnCode ("can not add question to set", + "INSERT INTO exa_set_questions" + " (SetCod,Invalid,AnsType,Shuffle," + "Stem,Feedback,MedCod)" + " VALUES" + " (%ld,'%c','%s','%c'," + "'%s','%s',%ld)", + SetCod, + CharInvalid[Question->Validity], + Tst_StrAnswerTypesDB[Question->Answer.Type], + Question->Answer.Shuffle ? 'Y' : + 'N', + Question->Stem, + Question->Feedback, + MedCod); + } + /*****************************************************************************/ /********************* Get number of questions in a set **********************/ /*****************************************************************************/ @@ -600,6 +631,57 @@ void Exa_DB_RemoveSetQuestionsFromCrs (long CrsCod) CrsCod); } +/*****************************************************************************/ +/********************* Add one answer to question in set *********************/ +/*****************************************************************************/ + +void Exa_DB_AddAnsToQstInSet (long QstCod,unsigned AnsInd, + const char *Answer,const char *Feedback, + long MedCod,bool Correct) + { + DB_QueryINSERT ("can not add answer to set", + "INSERT INTO exa_set_answers" + " (QstCod,AnsInd,Answer,Feedback,MedCod,Correct)" + " VALUES" + " (%ld,%u,'%s','%s',%ld,'%c')", + QstCod, // Question code in set + AnsInd, // Answer index (number of option) + Answer, // Copy of text + Feedback, // Copy of feedback + MedCod, // Media code of the new cloned media + Correct ? 'Y' : + 'N'); // Copy of correct + } + +/*****************************************************************************/ +/*************** Get answers of a test question from database ****************/ +/*****************************************************************************/ + +unsigned Exa_DB_GetQstAnswersFromSet (MYSQL_RES **mysql_res,long QstCod,bool Shuffle) + { + unsigned NumOptions; + + /***** Get answers of a question from database *****/ + 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 exa_set_answers" + " WHERE QstCod=%ld" + " ORDER BY %s", + QstCod, + Shuffle ? "RAND()" : + "AnsInd"); + + if (!NumOptions) + Ale_ShowAlert (Ale_ERROR,"Error when getting answers of a question."); + + return NumOptions; + } + /*****************************************************************************/ /************** Get answers text for a question in an exam set ***************/ /*****************************************************************************/ diff --git a/swad_exam_database.h b/swad_exam_database.h index 8fdc1bc7..9dcf47af 100644 --- a/swad_exam_database.h +++ b/swad_exam_database.h @@ -68,6 +68,7 @@ unsigned Exa_DB_GetNextSetIndexInExam (long ExaCod,unsigned SetInd); void Exa_DB_RemoveSetsFromExam (long ExaCod); void Exa_DB_RemoveSetsFromCrs (long CrsCod); +long Exa_DB_AddQuestionToSet (long SetCod,const struct Tst_Question *Question,long MedCod); unsigned Exa_DB_GetNumQstsInSet (long SetCod); unsigned Exa_DB_GetQstsFromSet (MYSQL_RES **mysql_res,long SetCod); unsigned Exa_DB_GetSomeQstsFromSetToPrint (MYSQL_RES **mysql_res, @@ -78,6 +79,10 @@ unsigned Exa_DB_GetAnswerType (MYSQL_RES **mysql_res,long QstCod); void Exa_DB_RemoveSetQuestionsFromExam (long ExaCod); void Exa_DB_RemoveSetQuestionsFromCrs (long CrsCod); +void Exa_DB_AddAnsToQstInSet (long QstCod,unsigned AnsInd, + const char *Answer,const char *Feedback, + long MedCod,bool Correct); +unsigned Exa_DB_GetQstAnswersFromSet (MYSQL_RES **mysql_res,long QstCod,bool Shuffle); unsigned Exa_DB_GetQstAnswersTextFromSet (MYSQL_RES **mysql_res,long QstCod); unsigned Exa_DB_GetQstAnswersCorrFromSet (MYSQL_RES **mysql_res,long QstCod); void Exa_DB_RemoveSetAnswersFromExam (long ExaCod); diff --git a/swad_exam_print.c b/swad_exam_print.c index 96ed5fbc..5ca7da43 100644 --- a/swad_exam_print.c +++ b/swad_exam_print.c @@ -479,7 +479,9 @@ static void ExaPrn_GenerateChoiceIndexes (struct TstPrn_PrintedQuestion *Printed Question.QstCod = PrintedQuestion->QstCod; /***** Get answers of question from database *****/ - ExaSet_GetAnswersQst (&Question,&mysql_res,Shuffle); + Question.Answer.NumOptions = Exa_DB_GetQstAnswersFromSet (&mysql_res, + Question.QstCod, + Shuffle); /* row[0] AnsInd row[1] Answer @@ -487,7 +489,6 @@ static void ExaPrn_GenerateChoiceIndexes (struct TstPrn_PrintedQuestion *Printed row[3] MedCod row[4] Correct */ - for (NumOpt = 0; NumOpt < Question.Answer.NumOptions; NumOpt++) diff --git a/swad_exam_set.c b/swad_exam_set.c index 7e65973b..9b612fc2 100644 --- a/swad_exam_set.c +++ b/swad_exam_set.c @@ -114,7 +114,7 @@ static void ExaSet_ListQuestionForEdition (struct Tst_Question *Question, static void ExaSet_AllocateListSelectedQuestions (struct Exa_Exams *Exams); static void ExaSet_FreeListsSelectedQuestions (struct Exa_Exams *Exams); -static void ExaSet_CopyQstFromBankToExamSet (struct ExaSet_Set *Set,long QstCod); +static void ExaSet_CopyQstFromBankToExamSet (const struct ExaSet_Set *Set,long QstCod); static void ExaSet_RemoveMediaFromStemOfQst (long SetCod,long QstCod); static void ExaSet_RemoveMediaFromAllAnsOfQst (long SetCod,long QstCod); @@ -1104,8 +1104,9 @@ void ExaSet_GetQstDataFromDB (struct Tst_Question *Question) DB_FreeMySQLResult (&mysql_res); /***** Get the answers from the database *****/ - ExaSet_GetAnswersQst (Question,&mysql_res, - false); // Don't shuffle + Question->Answer.NumOptions = Exa_DB_GetQstAnswersFromSet (&mysql_res, + Question->QstCod, + false); // Don't shuffle /* row[0] AnsInd row[1] Answer @@ -1179,32 +1180,6 @@ void ExaSet_GetQstDataFromDB (struct Tst_Question *Question) Err_WrongQuestionExit (); } -/*****************************************************************************/ -/*************** Get answers of a test question from database ****************/ -/*****************************************************************************/ - -void ExaSet_GetAnswersQst (struct Tst_Question *Question,MYSQL_RES **mysql_res, - bool Shuffle) - { - /***** Get answers of a question from database *****/ - Question->Answer.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 exa_set_answers" - " WHERE QstCod=%ld" - " ORDER BY %s", - Question->QstCod, - Shuffle ? "RAND()" : - "AnsInd"); - if (!Question->Answer.NumOptions) - Ale_ShowAlert (Ale_ERROR,"Error when getting answers of a question."); - } - - /*****************************************************************************/ /********************* List question in set for edition **********************/ /*****************************************************************************/ @@ -1355,9 +1330,8 @@ static void ExaSet_FreeListsSelectedQuestions (struct Exa_Exams *Exams) /******* Copy question and answers from back of questions to exam set ********/ /*****************************************************************************/ -static void ExaSet_CopyQstFromBankToExamSet (struct ExaSet_Set *Set,long QstCod) +static void ExaSet_CopyQstFromBankToExamSet (const struct ExaSet_Set *Set,long QstCod) { - extern const char *Tst_StrAnswerTypesDB[Tst_NUM_ANS_TYPES]; extern const char *Txt_Question_removed; struct Tst_Question Question; long CloneMedCod; @@ -1365,11 +1339,6 @@ static void ExaSet_CopyQstFromBankToExamSet (struct ExaSet_Set *Set,long QstCod) unsigned NumOpt; MYSQL_RES *mysql_res; MYSQL_ROW row; - static char CharInvalid[Tst_NUM_VALIDITIES] = - { - [Tst_INVALID_QUESTION] = 'Y', - [Tst_VALID_QUESTION ] = 'N' - }; /***** Create test question *****/ Tst_QstConstructor (&Question); @@ -1381,23 +1350,8 @@ static void ExaSet_CopyQstFromBankToExamSet (struct ExaSet_Set *Set,long QstCod) /***** Clone media *****/ CloneMedCod = Med_CloneMedia (&Question.Media); - /***** Insert question in table of questions *****/ - QstCodInSet = - DB_QueryINSERTandReturnCode ("can not add question to set", - "INSERT INTO exa_set_questions" - " (SetCod,Invalid,AnsType,Shuffle," - "Stem,Feedback,MedCod)" - " VALUES" - " (%ld,'%c','%s','%c'," - "'%s','%s',%ld)", - Set->SetCod, - CharInvalid[Question.Validity], - Tst_StrAnswerTypesDB[Question.Answer.Type], - Question.Answer.Shuffle ? 'Y' : - 'N', - Question.Stem, - Question.Feedback, - CloneMedCod); + /***** Add question to set *****/ + QstCodInSet = Exa_DB_AddQuestionToSet (Set->SetCod,&Question,CloneMedCod); /***** Get the answers from the database *****/ Tst_GetAnswersQst (&Question,&mysql_res, @@ -1423,17 +1377,12 @@ static void ExaSet_CopyQstFromBankToExamSet (struct ExaSet_Set *Set,long QstCod) CloneMedCod = Med_CloneMedia (&Question.Answer.Options[NumOpt].Media); /* Copy answer option to exam set */ - DB_QueryINSERT ("can not add answer to set", - "INSERT INTO exa_set_answers" - " (QstCod,AnsInd,Answer,Feedback,MedCod,Correct)" - " VALUES" - " (%ld,%u,'%s','%s',%ld,'%s')", - QstCodInSet, // Question code in set - NumOpt, // Answer index (number of option) - row[1], // Copy of text - row[2], // Copy of feedback - CloneMedCod, // Media code of the new cloned media - row[4]); // Copy of correct + Exa_DB_AddAnsToQstInSet (QstCodInSet, // Question code in set + NumOpt, // Answer index (number of option) + row[1], // Copy of text + row[2], // Copy of feedback + CloneMedCod, // Media code of the new cloned media + row[4][0] == 'Y'); // Copy of correct } /* Free structure that stores the query result */ diff --git a/swad_exam_set.h b/swad_exam_set.h index e26b3b6a..ad4d2dfc 100644 --- a/swad_exam_set.h +++ b/swad_exam_set.h @@ -60,8 +60,6 @@ void ExaSet_ResetSet (struct ExaSet_Set *Set); Tst_AnswerType_t ExaSet_GetAnswerType (long QstCod); void ExaSet_GetQstDataFromDB (struct Tst_Question *Question); -void ExaSet_GetAnswersQst (struct Tst_Question *Question,MYSQL_RES **mysql_res, - bool Shuffle); void ExaSet_AddQstsToSet (void);