From 86884a2e7e7e6b82e187459a8f51ccbb0315196b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ca=C3=B1as=20Vargas?= Date: Fri, 27 Sep 2019 21:45:53 +0200 Subject: [PATCH] Version19.17.2 --- swad_changelog.h | 3 +- swad_game.c | 66 ++++++++++++++++++++++++++------------- swad_match.c | 81 +++++++++++++++++++++++++++++++----------------- swad_match.h | 2 ++ 4 files changed, 101 insertions(+), 51 deletions(-) diff --git a/swad_changelog.h b/swad_changelog.h index 7c476224b..d9f932de2 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -471,10 +471,11 @@ enscript -2 --landscape --color --file-align=2 --highlight --line-numbers -o - * En OpenSWAD: ps2pdf source.ps destination.pdf */ -#define Log_PLATFORM_VERSION "SWAD 19.17.1 (2019-09-27)" +#define Log_PLATFORM_VERSION "SWAD 19.17.2 (2019-09-27)" #define CSS_FILE "swad19.15.css" #define JS_FILE "swad19.15.js" /* + Version 19.17.2: Sep 27, 2019 Code refactoring removing matches and games. (246448 lines) Version 19.17.1: Sep 27, 2019 Remove associations between matches and groups when removing groups. (246412 lines) Version 19.17: Sep 27, 2019 Improvements in the code that deletes a match. (246410 lines) Version 19.16.8: Sep 27, 2019 Code refactoring. (246387 lines) diff --git a/swad_game.c b/swad_game.c index 72227c9fd..c6eed4c05 100644 --- a/swad_game.c +++ b/swad_game.c @@ -73,6 +73,16 @@ const char *Gam_StrAnswerTypesDB[Gam_NUM_ANS_TYPES] = #define Gam_MAX_SELECTED_QUESTIONS 1000 #define Gam_MAX_BYTES_LIST_SELECTED_QUESTIONS (Gam_MAX_SELECTED_QUESTIONS * (1 + 10 + 1)) +/* +mysql> SELECT table_name FROM information_schema.tables WHERE table_name LIKE 'gam%'; +*/ +const char *GameTables[] = + { + "gam_questions", // questions in games + "gam_games" // the games themselves + }; +#define Gam_NUM_TABLES (sizeof (GameTables) / sizeof (GameTables[0])) + /*****************************************************************************/ /******************************* Private types *******************************/ /*****************************************************************************/ @@ -105,6 +115,9 @@ static void Gam_ResetGame (struct Game *Game); static void Gam_GetGameTxtFromDB (long GamCod,char Txt[Cns_MAX_BYTES_TEXT + 1]); +static void Gam_RemoveGameFromAllTables (long GamCod); +static void Gam_RemoveGameFromTable (long GamCod,const char *TableName); + static bool Gam_CheckIfSimilarGameExists (struct Game *Game); static void Gam_CreateGame (struct Game *Game,const char *Txt); @@ -933,28 +946,11 @@ void Gam_RemoveGame (void) if (!Game.Status.ICanEdit) Lay_ShowErrorAndExit ("You can not remove this game."); - /***** Remove all the questions in this game *****/ - DB_QueryDELETE ("can not remove questions of a game", - "DELETE FROM gam_questions WHERE GamCod=%ld", - Game.GamCod); - /***** Remove all the matches in this game *****/ - /* Remove groups in matches of the game */ - DB_QueryDELETE ("can not remove the groups associated to matches of a game", - "DELETE FROM mch_groups" - " USING mch_matches,mch_groups" - " WHERE mch_matches.GamCod=%ld" - " AND mch_matches.MchCod=mch_groups.MchCod", - Game.GamCod); - /* Remove matches of the game */ - DB_QueryDELETE ("can not remove matches of a game", - "DELETE FROM mch_matches WHERE GamCod=%ld", - Game.GamCod); + Mch_RemoveMatchInGameFromAllTables (Game.GamCod); - /***** Remove game *****/ - DB_QueryDELETE ("can not remove game", - "DELETE FROM gam_games WHERE GamCod=%ld", - Game.GamCod); + /***** Remove game from all tables *****/ + Gam_RemoveGameFromAllTables (Game.GamCod); /***** Write message to show the change made *****/ Ale_ShowAlert (Ale_SUCCESS,Txt_Game_X_removed, @@ -964,6 +960,34 @@ void Gam_RemoveGame (void) Gam_ListAllGames (); } +/*****************************************************************************/ +/*********************** Remove game from all tables *************************/ +/*****************************************************************************/ + +static void Gam_RemoveGameFromAllTables (long GamCod) + { + unsigned NumTable; + + for (NumTable = 0; + NumTable < Gam_NUM_TABLES; + NumTable++) + /* Remove game from table */ + Gam_RemoveGameFromTable (GamCod,GameTables[NumTable]); + } + +/*****************************************************************************/ +/************************* Remove game from table ****************************/ +/*****************************************************************************/ + +static void Gam_RemoveGameFromTable (long GamCod,const char *TableName) + { + /***** Remove game from table *****/ + DB_QueryDELETE ("can not remove game from table", + "DELETE FROM %s WHERE GamCod=%ld", + TableName, + GamCod); + } + /*****************************************************************************/ /******************************** Hide a game ******************************/ /*****************************************************************************/ @@ -1867,7 +1891,7 @@ void Gam_AddTstQuestionsToGame (void) /* Check number of questions */ if (Gam_CountNumQuestionsInList () == 0) // If no questions selected... - { // ...write warning alert + { // ...write warning alert Ale_ShowAlert (Ale_WARNING,Txt_You_must_select_one_ore_more_questions); // TODO: Show form again!!! diff --git a/swad_match.c b/swad_match.c index 1112190f6..edae30f8d 100644 --- a/swad_match.c +++ b/swad_match.c @@ -118,6 +118,22 @@ const char *Mch_ShowingStringsDB[Mch_NUM_SHOWING] = "results", }; +/* +mysql> SELECT table_name FROM information_schema.tables WHERE table_name LIKE 'mch%'; +*/ +const char *MatchTables[] = + { + "mch_players", // match players + "mch_playing", // matches being played + "mch_results", // matches results + "mch_answers", // students' answers to matches + "mch_times", // times associated to matches + "mch_groups", // groups associated to matches + "mch_indexes", // indexes associated to matches + "mch_matches" // the matches themselves + }; +#define Mch_NUM_TABLES (sizeof (MatchTables) / sizeof (MatchTables[0])) + /*****************************************************************************/ /***************************** Private variables *****************************/ /*****************************************************************************/ @@ -155,6 +171,7 @@ static Mch_Showing_t Mch_GetShowingFromStr (const char *Str); static void Mch_RemoveMatchFromAllTables (long MchCod); static void Mch_RemoveMatchFromTable (long MchCod,const char *TableName); +static void Mch_RemoveMatchInGameFromTable (long GamCod,const char *TableName); static void Mch_PutParamCurrentMchCod (void); static void Mch_PutParamMchCod (long MchCod); @@ -1044,36 +1061,9 @@ void Mch_RemoveMatchTch (void) /*****************************************************************************/ /********************** Remove match from all tables *************************/ /*****************************************************************************/ -/* -mysql> SELECT table_name FROM information_schema.tables WHERE table_name LIKE 'mch%'; -+-------------+ -| table_name | -+-------------+ -| mch_answers | -| mch_groups | -| mch_indexes | -| mch_matches | -| mch_players | -| mch_playing | -| mch_results | -| mch_times | -+-------------+ -8 rows in set (0.00 sec) -*/ + static void Mch_RemoveMatchFromAllTables (long MchCod) { - static const char *MatchTables[] = - { - "mch_players", // match players - "mch_playing", // matches being played - "mch_results", // matches results - "mch_answers", // students' answers to matches - "mch_times", // times associated to matches - "mch_groups", // groups associated to matches - "mch_indexes", // indexes associated to matches - "mch_matches" // the matches themselves - }; -#define Mch_NUM_TABLES (sizeof (MatchTables) / sizeof (MatchTables[0])) unsigned NumTable; for (NumTable = 0; @@ -1083,6 +1073,21 @@ static void Mch_RemoveMatchFromAllTables (long MchCod) Mch_RemoveMatchFromTable (MchCod,MatchTables[NumTable]); } +/*****************************************************************************/ +/******************** Remove match in game from all tables *******************/ +/*****************************************************************************/ + +void Mch_RemoveMatchInGameFromAllTables (long GamCod) + { + unsigned NumTable; + + for (NumTable = 0; + NumTable < Mch_NUM_TABLES; + NumTable++) + /* Remove match from table */ + Mch_RemoveMatchInGameFromTable (GamCod,MatchTables[NumTable]); + } + /*****************************************************************************/ /************************ Remove match from table ****************************/ /*****************************************************************************/ @@ -1092,7 +1097,25 @@ static void Mch_RemoveMatchFromTable (long MchCod,const char *TableName) /***** Remove match from table *****/ DB_QueryDELETE ("can not remove match from table", "DELETE FROM %s WHERE MchCod=%ld", - TableName,MchCod); + TableName, + MchCod); + } + +/*****************************************************************************/ +/****************** Remove all matches in game from table ********************/ +/*****************************************************************************/ + +static void Mch_RemoveMatchInGameFromTable (long GamCod,const char *TableName) + { + DB_QueryDELETE ("can not remove matches of a game from table", + "DELETE FROM %s" + " USING mch_matches,%s" + " WHERE mch_matches.GamCod=%ld" + " AND mch_matches.MchCod=%s.MchCod", + TableName, + TableName, + GamCod, + TableName); } /*****************************************************************************/ diff --git a/swad_match.h b/swad_match.h index 8f1a2a713..ef3ee58ed 100644 --- a/swad_match.h +++ b/swad_match.h @@ -48,6 +48,8 @@ void Mch_ToggleVisibilResultsMchUsr (void); void Mch_RequestRemoveMatchTch (void); void Mch_RemoveMatchTch (void); +void Mch_RemoveMatchInGameFromAllTables (long GamCod); + void Mch_CreateNewMatchTch (void); void Mch_RequestStartResumeMatchTch (void); void Mch_GetIndexes (long MchCod,unsigned QstInd,