From f9acc9350769d57e60f1a5028e0c1f935e409c35 Mon Sep 17 00:00:00 2001 From: acanas Date: Mon, 20 Sep 2021 13:40:17 +0200 Subject: [PATCH] Version 21.6.3: Sep 20, 2021 Queries moved to module swad_game_database. --- swad_changelog.h | 3 +- swad_game.c | 61 ++++++----------------------------- swad_game_database.c | 75 ++++++++++++++++++++++++++++++++++++++++++++ swad_game_database.h | 5 +++ 4 files changed, 92 insertions(+), 52 deletions(-) diff --git a/swad_changelog.h b/swad_changelog.h index 11cffcf8..a6cd3098 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.6.2 (2021-09-18)" +#define Log_PLATFORM_VERSION "SWAD 21.6.3 (2021-09-20)" #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.6.3: Sep 20, 2021 Queries moved to module swad_game_database. (315708 lines) Version 21.6.2: Sep 18, 2021 Queries moved to module swad_game_database. (315679 lines) Version 21.6.1: Sep 18, 2021 Queries moved to module swad_game_database. (315571 lines) Version 21.6: Sep 18, 2021 New module swad_game_database for database queries related to games. (315565 lines) diff --git a/swad_game.c b/swad_game.c index ed3eb80a..886e24d1 100644 --- a/swad_game.c +++ b/swad_game.c @@ -2175,9 +2175,7 @@ static void Gam_ExchangeQuestions (long GamCod, long QstCodBottom; /***** Lock table to make the move atomic *****/ - DB_Query ("can not lock tables to move game question", - "LOCK TABLES gam_questions WRITE"); - Gbl.DB.LockedTables = true; + Gam_DB_LockTable (); /***** Get question code of the questions to be moved *****/ QstCodTop = Gam_DB_GetQstCodFromQstInd (GamCod,QstIndTop); @@ -2199,40 +2197,16 @@ static void Gam_ExchangeQuestions (long GamCod, */ /* Step 1: change temporarily top index to minus bottom index in order to not repeat unique index (GamCod,QstInd) */ - DB_QueryUPDATE ("can not exchange indexes of questions", - "UPDATE gam_questions" - " SET QstInd=-%u" - " WHERE GamCod=%ld" - " AND QstCod=%ld", - QstIndBottom, - GamCod, - QstCodTop); + Gam_DB_UpdateQstIndex (-((long) QstIndBottom),GamCod,QstCodTop ); /* Step 2: change bottom index to old top index */ - DB_QueryUPDATE ("can not exchange indexes of questions", - "UPDATE gam_questions" - " SET QstInd=%u" - " WHERE GamCod=%ld" - " AND QstCod=%ld", - QstIndTop, - GamCod, - QstCodBottom); + Gam_DB_UpdateQstIndex ( (long) QstIndTop ,GamCod,QstCodBottom); /* Step 3: change top index to old bottom index */ - DB_QueryUPDATE ("can not exchange indexes of questions", - "UPDATE gam_questions" - " SET QstInd=%u" - " WHERE GamCod=%ld" - " AND QstCod=%ld", - QstIndBottom, - GamCod, - QstCodTop); + Gam_DB_UpdateQstIndex ( (long) QstIndBottom ,GamCod,QstCodTop ); /***** Unlock table *****/ - Gbl.DB.LockedTables = false; // Set to false before the following unlock... - // ...to not retry the unlock if error in unlocking - DB_Query ("can not unlock tables after moving game questions", - "UNLOCK TABLES"); + Gam_DB_UnlockTable (); } /*****************************************************************************/ @@ -2302,17 +2276,7 @@ void Gam_ShowTstTagsPresentInAGame (long GamCod) unsigned long NumTags; /***** Get all tags of questions in this game *****/ - NumTags = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get tags present in a match result", - "SELECT tst_tags.TagTxt" // row[0] - " FROM (SELECT DISTINCT(tst_question_tags.TagCod)" - " FROM tst_question_tags,gam_questions" - " WHERE gam_questions.GamCod=%ld" - " AND gam_questions.QstCod=tst_question_tags.QstCod) AS TagsCods," - "tst_tags" - " WHERE TagsCods.TagCod=tst_tags.TagCod" - " ORDER BY tst_tags.TagTxt", - GamCod); + NumTags = Gam_DB_GetTstTagsPresentInAGame (&mysql_res,GamCod); Tst_ShowTagList (NumTags,mysql_res); /***** Free structure that stores the query result *****/ @@ -2332,14 +2296,7 @@ void Gam_GetScoreRange (long GamCod,double *MinScore,double *MaxScore) unsigned NumAnswers; /***** Get maximum score of a game from database *****/ - NumQsts = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get data of a question", - "SELECT COUNT(tst_answers.AnsInd) AS N" // row[0] - " FROM tst_answers,gam_questions" - " WHERE gam_questions.GamCod=%ld" - " AND gam_questions.QstCod=tst_answers.QstCod" - " GROUP BY tst_answers.QstCod", - GamCod); + NumQsts = Gam_DB_GetNumAnswersOfQstsInGame (&mysql_res,GamCod); for (NumQst = 0, *MinScore = *MaxScore = 0.0; NumQst < NumQsts; NumQst++) @@ -2350,9 +2307,11 @@ void Gam_GetScoreRange (long GamCod,double *MinScore,double *MaxScore) if (sscanf (row[0],"%u",&NumAnswers) != 1) NumAnswers = 0; - /* Accumulate minimum and maximum score */ + /* Check number of answers */ if (NumAnswers < 2) Err_ShowErrorAndExit ("Wrong number of answers."); + + /* Accumulate minimum and maximum score */ *MinScore += -1.0 / (double) (NumAnswers - 1); *MaxScore += 1.0; } diff --git a/swad_game_database.c b/swad_game_database.c index f71c9526..5f621e6b 100644 --- a/swad_game_database.c +++ b/swad_game_database.c @@ -472,6 +472,45 @@ void Gam_DB_UpdateIndexesOfQstsGreaterThan (long GamCod,unsigned QstInd) QstInd); } +/*****************************************************************************/ +/********************* Change index of a set in an exam **********************/ +/*****************************************************************************/ + +void Gam_DB_UpdateQstIndex (long QstInd,long GamCod,long QstCod) + { + DB_QueryUPDATE ("can not exchange indexes of questions", + "UPDATE gam_questions" + " SET QstInd=%ld" + " WHERE GamCod=%ld" + " AND QstCod=%ld", + QstInd, + GamCod, + QstCod); + } + +/*****************************************************************************/ +/************ Lock table to make the exchange of questions atomic ************/ +/*****************************************************************************/ + +void Gam_DB_LockTable (void) + { + DB_Query ("can not lock tables to move game question", + "LOCK TABLES gam_questions WRITE"); + Gbl.DB.LockedTables = true; + } + +/*****************************************************************************/ +/********** Unlock table to make the exchange of questions atomic ************/ +/*****************************************************************************/ + +void Gam_DB_UnlockTable (void) + { + Gbl.DB.LockedTables = false; // Set to false before the following unlock... + // ...to not retry the unlock if error in unlocking + DB_Query ("can not unlock tables after moving game questions", + "UNLOCK TABLES"); + } + /*****************************************************************************/ /******************* Get number of questions of a game *********************/ /*****************************************************************************/ @@ -697,6 +736,42 @@ double Gam_DB_GetNumQstsPerGame (HieLvl_Level_t Scope) } } +/*****************************************************************************/ +/********************* Get all tags of questions in a game *******************/ +/*****************************************************************************/ + +unsigned Gam_DB_GetTstTagsPresentInAGame (MYSQL_RES **mysql_res,long GamCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get tags present in a match result", + "SELECT tst_tags.TagTxt" // row[0] + " FROM (SELECT DISTINCT(tst_question_tags.TagCod)" + " FROM tst_question_tags,gam_questions" + " WHERE gam_questions.GamCod=%ld" + " AND gam_questions.QstCod=tst_question_tags.QstCod) AS TagsCods," + "tst_tags" + " WHERE TagsCods.TagCod=tst_tags.TagCod" + " ORDER BY tst_tags.TagTxt", + GamCod); + } + +/*****************************************************************************/ +/************* Get number of answers of each question in a game **************/ +/*****************************************************************************/ + +unsigned Gam_DB_GetNumAnswersOfQstsInGame (MYSQL_RES **mysql_res,long GamCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get data of a question", + "SELECT COUNT(tst_answers.AnsInd) AS N" // row[0] + " FROM tst_answers," + "gam_questions" + " WHERE gam_questions.GamCod=%ld" + " AND gam_questions.QstCod=tst_answers.QstCod" + " GROUP BY tst_answers.QstCod", + GamCod); + } + /*****************************************************************************/ /************************* Remove question from game *************************/ /*****************************************************************************/ diff --git a/swad_game_database.h b/swad_game_database.h index d44cb133..f5bfe703 100644 --- a/swad_game_database.h +++ b/swad_game_database.h @@ -58,6 +58,9 @@ void Gam_DB_RemoveCrsGames (long CrsCod); //---------------------------- Game questions --------------------------------- void Gam_DB_InsertQstInGame (long GamCod,unsigned QstInd,long QstCod); void Gam_DB_UpdateIndexesOfQstsGreaterThan (long GamCod,unsigned QstInd); +void Gam_DB_UpdateQstIndex (long QstInd,long GamCod,long QstCod); +void Gam_DB_LockTable (void); +void Gam_DB_UnlockTable (void); unsigned Gam_DB_GetNumQstsGame (long GamCod); unsigned Gam_DB_GetGameQuestions (MYSQL_RES **mysql_res,long GamCod); @@ -67,6 +70,8 @@ unsigned Gam_DB_GetMaxQuestionIndexInGame (long GamCod); unsigned Gam_DB_GetPrevQuestionIndexInGame (long GamCod,unsigned QstInd); unsigned Gam_DB_GetNextQuestionIndexInGame (long GamCod,unsigned QstInd); double Gam_DB_GetNumQstsPerGame (HieLvl_Level_t Scope); +unsigned Gam_DB_GetTstTagsPresentInAGame (MYSQL_RES **mysql_res,long GamCod); +unsigned Gam_DB_GetNumAnswersOfQstsInGame (MYSQL_RES **mysql_res,long GamCod); void Gam_DB_RemoveQstFromGame (long GamCod,unsigned QstInd); void Gam_DB_RemoveGameQsts (long GamCod);