swad-core/swad_game.c

4240 lines
145 KiB
C
Raw Normal View History

2017-09-07 18:38:18 +02:00
// swad_game.c: games using remote control
2017-07-09 20:31:40 +02:00
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
2019-01-07 21:52:19 +01:00
Copyright (C) 1999-2019 Antonio Ca<EFBFBD>as Vargas
2017-07-09 20:31:40 +02:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/********************************* Headers ***********************************/
/*****************************************************************************/
2018-10-22 09:04:08 +02:00
#define _GNU_SOURCE // For asprintf
2017-07-09 20:31:40 +02:00
#include <linux/limits.h> // For PATH_MAX
#include <linux/stddef.h> // For NULL
2018-10-22 09:04:08 +02:00
#include <stdio.h> // For asprintf
2017-07-09 20:31:40 +02:00
#include <stdlib.h> // For calloc
#include <string.h> // For string functions
#include "swad_alert.h"
#include "swad_box.h"
#include "swad_database.h"
2018-11-09 20:47:39 +01:00
#include "swad_form.h"
2017-09-07 18:38:18 +02:00
#include "swad_game.h"
2017-07-09 20:31:40 +02:00
#include "swad_global.h"
#include "swad_group.h"
#include "swad_pagination.h"
#include "swad_parameter.h"
#include "swad_role.h"
2019-03-26 11:53:21 +01:00
#include "swad_setting.h"
2017-07-09 20:31:40 +02:00
#include "swad_table.h"
2017-07-16 13:54:11 +02:00
#include "swad_test.h"
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
#define Gam_MAX_CHARS_ANSWER (1024 - 1) // 1023
#define Gam_MAX_BYTES_ANSWER ((Gam_MAX_CHARS_ANSWER + 1) * Str_MAX_BYTES_PER_CHAR - 1) // 16383
2017-07-09 20:31:40 +02:00
2017-09-07 18:38:18 +02:00
#define Gam_MAX_BYTES_LIST_ANSWER_TYPES (10 + (Gam_NUM_ANS_TYPES - 1) * (1 + 10))
2017-07-09 20:31:40 +02:00
2017-09-07 18:38:18 +02:00
const char *Gam_StrAnswerTypesDB[Gam_NUM_ANS_TYPES] =
2017-07-09 20:31:40 +02:00
{
"unique_choice",
"multiple_choice",
};
2017-09-07 18:38:18 +02:00
#define Gam_MAX_ANSWERS_PER_QUESTION 10
2017-07-09 20:31:40 +02:00
2017-09-07 18:38:18 +02:00
#define Gam_MAX_SELECTED_QUESTIONS 1000
#define Gam_MAX_BYTES_LIST_SELECTED_QUESTIONS (Gam_MAX_SELECTED_QUESTIONS * (1 + 10 + 1))
2017-07-18 20:34:32 +02:00
2019-05-28 15:06:53 +02:00
#define Gam_NEW_MATCH_SECTION_ID "new_match"
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
/******************************* Private types *******************************/
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
struct Match
{
long MchCod;
long GamCod;
long UsrCod;
time_t TimeUTC[2];
char Title[Gam_MAX_BYTES_TITLE + 1];
2019-05-29 01:14:56 +02:00
struct
{
2019-05-30 12:57:31 +02:00
unsigned QstInd; // 0 means that the game has not started. First question has index 0.
2019-05-29 01:14:56 +02:00
long QstCod;
time_t QstStartTimeUTC;
2019-05-30 20:52:23 +02:00
bool ShowingAnswers;
2019-05-29 01:14:56 +02:00
bool Finished;
} Status;
2019-05-28 15:06:53 +02:00
};
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
/***************************** Private variables *****************************/
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
long Gam_CurrentGamCod = -1L; // Used as parameter in contextual links
long Gam_CurrentMchCod = -1L; // Used as parameter in contextual links
2019-05-30 12:57:31 +02:00
unsigned Gam_CurrentQstInd = 0; // Used as parameter in contextual links
2019-05-20 08:52:07 +02:00
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_ListAllGames (void);
static bool Gam_CheckIfICanCreateGame (void);
static void Gam_PutIconsListGames (void);
static void Gam_PutIconToCreateNewGame (void);
static void Gam_PutButtonToCreateNewGame (void);
static void Gam_PutParamsToCreateNewGame (void);
static void Gam_ParamsWhichGroupsToShow (void);
2017-09-13 16:24:29 +02:00
static void Gam_ShowOneGame (long GamCod,
bool ShowOnlyThisGame,
bool ListGameQuestions,
2019-05-28 15:06:53 +02:00
bool PutFormNewMatch,
2019-05-20 10:35:57 +02:00
bool PutButtonToPlay);
2017-09-07 18:38:18 +02:00
static void Gam_WriteAuthor (struct Game *Game);
static void Gam_GetParamGameOrder (void);
2017-07-09 20:31:40 +02:00
2019-04-20 22:40:57 +02:00
static void Gam_PutFormsToRemEditOneGame (const struct Game *Game,
2019-05-20 08:52:07 +02:00
const char *Anchor);
2017-09-07 18:38:18 +02:00
static void Gam_PutParams (void);
2019-05-28 15:06:53 +02:00
static void Gam_PutParamCurrentMchCod (void);
2017-07-09 20:31:40 +02:00
2017-09-07 18:38:18 +02:00
static void Gam_GetGameTxtFromDB (long GamCod,char Txt[Cns_MAX_BYTES_TEXT + 1]);
2017-07-09 20:31:40 +02:00
2019-05-31 10:25:20 +02:00
static void Gam_PutParamMatchCod (long MchCod);
static long Gam_GetParamMatchCod (void);
2017-09-07 18:38:18 +02:00
static bool Gam_CheckIfSimilarGameExists (struct Game *Game);
2019-05-28 15:06:53 +02:00
static void Gam_ShowLstGrpsToEditMatch (void);
2017-09-13 16:24:29 +02:00
2017-09-07 18:38:18 +02:00
static void Gam_CreateGame (struct Game *Game,const char *Txt);
static void Gam_UpdateGame (struct Game *Game,const char *Txt);
2019-05-28 15:06:53 +02:00
static void Gam_CreateGrps (long MchCod);
static void Gam_GetAndWriteNamesOfGrpsAssociatedToMatch (struct Match *Match);
static bool Gam_CheckIfIPlayThisMatchBasedOnGrps (long GamCod);
2017-09-07 18:38:18 +02:00
static unsigned Gam_GetNumQstsGame (long GamCod);
2017-09-13 21:22:52 +02:00
static void Gam_PutParamQstInd (unsigned QstInd);
static unsigned Gam_GetParamQstInd (void);
2019-05-30 12:57:31 +02:00
static unsigned Gam_GetQstIndFromStr (const char *UnsignedStr);
static void Gam_RemAnswersOfAQuestion (long GamCod,unsigned QstInd);
2017-09-07 18:38:18 +02:00
2017-09-11 19:06:46 +02:00
static long Gam_GetQstCodFromQstInd (long GamCod,unsigned QstInd);
2019-05-30 12:57:31 +02:00
static unsigned Gam_GetMaxQuestionIndexInGame (long GamCod);
2019-05-30 13:46:57 +02:00
static unsigned Gam_GetFirstQuestionIndexInGame (long GamCod);
2019-05-30 12:57:31 +02:00
static unsigned Gam_GetPrevQuestionIndexInGame (long GamCod,unsigned QstInd);
static unsigned Gam_GetNextQuestionIndexInGame (long GamCod,unsigned QstInd);
2017-09-07 18:38:18 +02:00
static void Gam_ListGameQuestions (struct Game *Game);
2019-05-28 15:06:53 +02:00
static void Gam_ListOneOrMoreQuestionsForEdition (long GamCod,unsigned NumQsts,
2017-09-06 15:03:43 +02:00
MYSQL_RES *mysql_res);
2017-09-07 18:38:18 +02:00
static void Gam_PutIconToAddNewQuestions (void);
static void Gam_PutButtonToAddNewQuestions (void);
2017-07-18 20:34:32 +02:00
2017-09-07 18:38:18 +02:00
static void Gam_AllocateListSelectedQuestions (void);
static void Gam_FreeListsSelectedQuestions (void);
static unsigned Gam_CountNumQuestionsInList (void);
2017-07-18 20:34:32 +02:00
2019-05-30 12:57:31 +02:00
static unsigned Gam_GetNumUsrsWhoAnswered (long GamCod,unsigned QstInd,unsigned AnsInd);
2017-09-07 18:38:18 +02:00
static void Gam_DrawBarNumUsrs (unsigned NumUsrs,unsigned MaxUsrs);
2017-07-09 20:31:40 +02:00
2017-09-08 01:18:20 +02:00
static void Gam_PutParamsOneQst (void);
2017-07-09 20:31:40 +02:00
2017-09-11 19:06:46 +02:00
static void Gam_ExchangeQuestions (long GamCod,
unsigned QstIndTop,unsigned QstIndBottom);
2019-05-28 15:06:53 +02:00
static void Gam_ListPlayedMatches (struct Game *Game,bool PutFormNewMatch);
static void Gam_PutIconToPlayNewMatch (void);
2019-05-31 10:04:13 +02:00
static void Gam_ListOneOrMoreMatchesForEdition (struct Game *Game,
unsigned NumMatches,
2019-05-28 15:06:53 +02:00
MYSQL_RES *mysql_res);
static void Gam_GetMatchDataFromRow (MYSQL_RES *mysql_res,
struct Match *Match);
static void Gam_PutButtonNewMatch (long GamCod);
static void Gam_PutBigButtonToPlayMatchTch (struct Game *Game);
static void Gam_PutBigButtonToPlayMatchStd (long MchCod);
2017-09-14 02:32:36 +02:00
2019-05-30 20:52:23 +02:00
static void Gam_CreateMatch (struct Match *Match);
static void Gam_UpdateMatchBeingPlayed (struct Match *Match);
2019-05-20 19:47:17 +02:00
2019-05-31 10:25:20 +02:00
static void Gam_ShowAlertFinishedMatch (void);
2019-05-30 20:52:23 +02:00
static void Gam_PlayGameShowQuestionAndAnswers (struct Match *Match);
static void Gam_PutBigButtonToContinue (long MchCod);
2019-05-30 12:57:31 +02:00
static void Gam_PutBigButtonToFinishMatch (long GamCod);
2017-09-13 16:24:29 +02:00
2019-05-29 01:14:56 +02:00
static void Gam_ShowQuestionBeingPlayed (struct Match *Match);
2019-05-22 09:36:18 +02:00
2017-09-07 18:38:18 +02:00
static void Gam_ReceiveAndStoreUserAnswersToAGame (long GamCod);
static void Gam_IncreaseAnswerInDB (long QstCod,unsigned AnsInd);
static void Gam_RegisterIHaveAnsweredGame (long GamCod);
static bool Gam_CheckIfIHaveAnsweredGame (long GamCod);
static unsigned Gam_GetNumUsrsWhoHaveAnsweredGame (long GamCod);
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
/*************************** List all the games ******************************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_SeeAllGames (void)
2017-07-09 20:31:40 +02:00
{
/***** Get parameters *****/
2017-09-07 18:38:18 +02:00
Gam_GetParamGameOrder ();
2017-07-09 20:31:40 +02:00
Grp_GetParamWhichGrps ();
2017-09-13 16:24:29 +02:00
Gbl.Games.CurrentPage = Pag_GetParamPagNum (Pag_GAMES);
2017-07-09 20:31:40 +02:00
/***** Show all the games *****/
2017-09-07 18:38:18 +02:00
Gam_ListAllGames ();
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
/**************************** Show all the games *****************************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_ListAllGames (void)
2017-07-09 20:31:40 +02:00
{
extern const char *Hlp_ASSESSMENT_Games;
extern const char *Txt_Games;
extern const char *Txt_START_END_TIME_HELP[Dat_NUM_START_END_TIME];
extern const char *Txt_START_END_TIME[Dat_NUM_START_END_TIME];
extern const char *Txt_Game;
extern const char *Txt_No_games;
2017-09-07 18:38:18 +02:00
Gam_Order_t Order;
2017-07-09 20:31:40 +02:00
struct Pagination Pagination;
unsigned NumGame;
/***** Get number of groups in current course *****/
2019-04-04 10:45:15 +02:00
if (!Gbl.Crs.Grps.NumGrps)
Gbl.Crs.Grps.WhichGrps = Grp_ALL_GROUPS;
2017-07-09 20:31:40 +02:00
/***** Get list of games *****/
2017-09-07 18:38:18 +02:00
Gam_GetListGames ();
2017-07-09 20:31:40 +02:00
/***** Compute variables related to pagination *****/
Pagination.NumItems = Gbl.Games.Num;
Pagination.CurrentPage = (int) Gbl.Games.CurrentPage;
Pag_CalculatePagination (&Pagination);
Gbl.Games.CurrentPage = (unsigned) Pagination.CurrentPage;
/***** Write links to pages *****/
if (Pagination.MoreThanOnePage)
2017-09-13 16:24:29 +02:00
Pag_WriteLinksToPagesCentered (Pag_GAMES,
2017-07-09 20:31:40 +02:00
0,
&Pagination);
/***** Start box *****/
2017-09-07 18:38:18 +02:00
Box_StartBox ("100%",Txt_Games,Gam_PutIconsListGames,
2017-07-09 20:31:40 +02:00
Hlp_ASSESSMENT_Games,Box_NOT_CLOSABLE);
/***** Select whether show only my groups or all groups *****/
2019-04-04 10:45:15 +02:00
if (Gbl.Crs.Grps.NumGrps)
2019-02-25 15:14:28 +01:00
{
2019-03-26 11:53:21 +01:00
Set_StartSettingsHead ();
2019-02-25 15:14:28 +01:00
Grp_ShowFormToSelWhichGrps (ActSeeAllGam,Gam_ParamsWhichGroupsToShow);
2019-03-26 11:53:21 +01:00
Set_EndSettingsHead ();
2019-02-25 15:14:28 +01:00
}
2017-07-09 20:31:40 +02:00
if (Gbl.Games.Num)
{
/***** Table head *****/
Tbl_StartTableWideMargin (2);
fprintf (Gbl.F.Out,"<tr>"
"<th class=\"CONTEXT_COL\"></th>"); // Column for contextual icons
2017-09-07 18:38:18 +02:00
for (Order = Gam_ORDER_BY_START_DATE;
Order <= Gam_ORDER_BY_END_DATE;
2017-07-09 20:31:40 +02:00
Order++)
{
fprintf (Gbl.F.Out,"<th class=\"LEFT_MIDDLE\">");
/* Form to change order */
2018-11-09 20:47:39 +01:00
Frm_StartForm (ActSeeAllGam);
2017-07-09 20:31:40 +02:00
Grp_PutParamWhichGrps ();
2017-09-13 16:24:29 +02:00
Pag_PutHiddenParamPagNum (Pag_GAMES,Gbl.Games.CurrentPage);
2017-07-09 20:31:40 +02:00
Par_PutHiddenParamUnsigned ("Order",(unsigned) Order);
2018-11-09 20:47:39 +01:00
Frm_LinkFormSubmit (Txt_START_END_TIME_HELP[Order],"TIT_TBL",NULL);
2017-07-09 20:31:40 +02:00
if (Order == Gbl.Games.SelectedOrder)
fprintf (Gbl.F.Out,"<u>");
fprintf (Gbl.F.Out,"%s",Txt_START_END_TIME[Order]);
if (Order == Gbl.Games.SelectedOrder)
fprintf (Gbl.F.Out,"</u>");
fprintf (Gbl.F.Out,"</a>");
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out,"</th>");
}
fprintf (Gbl.F.Out,"<th class=\"LEFT_MIDDLE\">"
"%s"
"</th>"
"</tr>",
2019-05-28 15:06:53 +02:00
Txt_Game);
2017-07-09 20:31:40 +02:00
/***** Write all the games *****/
for (NumGame = Pagination.FirstItemVisible;
NumGame <= Pagination.LastItemVisible;
NumGame++)
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Gbl.Games.LstGamCods[NumGame - 1],
false,
false,
2019-05-28 15:06:53 +02:00
false, // Do not put form to start new match
2019-05-20 10:35:57 +02:00
false); // Do not put button to play
2017-07-09 20:31:40 +02:00
/***** End table *****/
Tbl_EndTable ();
}
else // No games created
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_INFO,Txt_No_games);
2017-07-09 20:31:40 +02:00
/***** Button to create a new game *****/
2017-09-07 18:38:18 +02:00
if (Gam_CheckIfICanCreateGame ())
Gam_PutButtonToCreateNewGame ();
2017-07-09 20:31:40 +02:00
/***** End box *****/
Box_EndBox ();
/***** Write again links to pages *****/
if (Pagination.MoreThanOnePage)
2017-09-13 16:24:29 +02:00
Pag_WriteLinksToPagesCentered (Pag_GAMES,
2017-07-09 20:31:40 +02:00
0,
&Pagination);
/***** Free list of games *****/
2017-09-07 18:38:18 +02:00
Gam_FreeListGames ();
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/******************* Check if I can create a new game **********************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static bool Gam_CheckIfICanCreateGame (void)
2017-07-09 20:31:40 +02:00
{
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_TCH:
case Rol_DEG_ADM:
case Rol_CTR_ADM:
case Rol_INS_ADM:
case Rol_SYS_ADM:
return true;
default:
return false;
}
return false;
}
/*****************************************************************************/
/***************** Put contextual icons in list of games *******************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_PutIconsListGames (void)
2017-07-09 20:31:40 +02:00
{
/***** Put icon to create a new game *****/
2017-09-07 18:38:18 +02:00
if (Gam_CheckIfICanCreateGame ())
Gam_PutIconToCreateNewGame ();
2017-07-09 20:31:40 +02:00
/***** Put icon to show a figure *****/
2019-02-12 14:46:14 +01:00
Gbl.Figures.FigureType = Fig_GAMES;
Fig_PutIconToShowFigure ();
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/********************** Put icon to create a new game **********************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_PutIconToCreateNewGame (void)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_New_game;
2019-01-10 15:26:33 +01:00
Ico_PutContextualIconToAdd (ActFrmNewGam,NULL,Gam_PutParamsToCreateNewGame,
Txt_New_game);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/********************* Put button to create a new game *********************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_PutButtonToCreateNewGame (void)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_New_game;
2018-11-09 20:47:39 +01:00
Frm_StartForm (ActFrmNewGam);
2017-09-07 18:38:18 +02:00
Gam_PutParamsToCreateNewGame ();
2017-07-09 20:31:40 +02:00
Btn_PutConfirmButton (Txt_New_game);
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/******************* Put parameters to create a new game *******************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_PutParamsToCreateNewGame (void)
2017-07-09 20:31:40 +02:00
{
2017-09-07 18:38:18 +02:00
Gam_PutHiddenParamGameOrder ();
2017-07-09 20:31:40 +02:00
Grp_PutParamWhichGrps ();
2017-09-13 16:24:29 +02:00
Pag_PutHiddenParamPagNum (Pag_GAMES,Gbl.Games.CurrentPage);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2019-02-25 15:14:28 +01:00
/**************** Put params to select which groups to show ******************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_ParamsWhichGroupsToShow (void)
2017-07-09 20:31:40 +02:00
{
2017-09-07 18:38:18 +02:00
Gam_PutHiddenParamGameOrder ();
2017-09-13 16:24:29 +02:00
Pag_PutHiddenParamPagNum (Pag_GAMES,Gbl.Games.CurrentPage);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/****************************** Show one game ******************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_SeeOneGame (void)
2017-07-09 20:31:40 +02:00
{
struct Game Game;
/***** Get parameters *****/
2017-09-07 18:38:18 +02:00
Gam_GetParamGameOrder ();
2017-07-09 20:31:40 +02:00
Grp_GetParamWhichGrps ();
2017-09-13 16:24:29 +02:00
Gbl.Games.CurrentPage = Pag_GetParamPagNum (Pag_GAMES);
2017-07-09 20:31:40 +02:00
/***** Get game code *****/
2017-09-07 18:38:18 +02:00
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
/***** Show game *****/
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Game.GamCod,
true, // Show only this game
true, // List game questions
2019-05-28 15:06:53 +02:00
false, // Do not put form to start new match
2019-05-20 10:35:57 +02:00
false); // Do not put button to play
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2017-09-13 16:24:29 +02:00
/******************************* Show one game *******************************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-13 16:24:29 +02:00
static void Gam_ShowOneGame (long GamCod,
bool ShowOnlyThisGame,
bool ListGameQuestions,
2019-05-28 15:06:53 +02:00
bool PutFormNewMatch,
2019-05-20 10:35:57 +02:00
bool PutButtonToPlay)
2017-07-09 20:31:40 +02:00
{
extern const char *Hlp_ASSESSMENT_Games;
extern const char *Txt_Game;
extern const char *Txt_Today;
extern const char *Txt_View_game;
extern const char *Txt_No_of_questions;
extern const char *Txt_No_of_users;
extern const char *Txt_Play;
2019-05-28 15:06:53 +02:00
extern const char *Txt_New_match;
2019-04-20 22:40:57 +02:00
char *Anchor = NULL;
2017-07-09 20:31:40 +02:00
static unsigned UniqueId = 0;
struct Game Game;
char Txt[Cns_MAX_BYTES_TEXT + 1];
/***** Start box *****/
2017-09-13 16:24:29 +02:00
if (ShowOnlyThisGame)
2017-07-09 20:31:40 +02:00
Box_StartBox (NULL,Txt_Game,NULL,
Hlp_ASSESSMENT_Games,Box_NOT_CLOSABLE);
/***** Get data of this game *****/
Game.GamCod = GamCod;
2017-09-07 18:38:18 +02:00
Gam_GetDataOfGameByCod (&Game);
2019-05-20 08:52:07 +02:00
Gam_CurrentGamCod = Game.GamCod; // Used as parameter in contextual links
2017-07-09 20:31:40 +02:00
2019-04-20 22:40:57 +02:00
/***** Set anchor string *****/
Frm_SetAnchorStr (Game.GamCod,&Anchor);
2017-07-09 20:31:40 +02:00
/***** Start table *****/
2017-09-13 16:24:29 +02:00
if (ShowOnlyThisGame)
2017-07-09 20:31:40 +02:00
Tbl_StartTableWide (2);
2019-05-20 08:52:07 +02:00
/***** Start first row of this game *****/
fprintf (Gbl.F.Out,"<tr>");
/***** Icons related to this game *****/
fprintf (Gbl.F.Out,"<td rowspan=\"2\" class=\"CONTEXT_COL");
2017-09-13 16:24:29 +02:00
if (!ShowOnlyThisGame)
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out," COLOR%u",Gbl.RowEvenOdd);
fprintf (Gbl.F.Out,"\">");
2019-05-20 08:52:07 +02:00
2017-07-09 20:31:40 +02:00
if (Game.Status.ICanEdit)
2019-05-20 08:52:07 +02:00
/* Icons to remove/edit this game */
Gam_PutFormsToRemEditOneGame (&Game,Anchor);
2019-05-20 10:35:57 +02:00
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_STD:
2019-05-28 15:06:53 +02:00
/* Icon to play match */
Lay_PutContextualLinkOnlyIcon (ActPlyMchStd,NULL,
Gam_PutParams,
"play.svg",
Txt_Play);
break;
2019-05-20 10:35:57 +02:00
case Rol_NET:
case Rol_TCH:
2019-05-28 15:06:53 +02:00
if (ShowOnlyThisGame)
/* Icon to start a new match */
Lay_PutContextualLinkOnlyIcon (ActFrmNewMch,Gam_NEW_MATCH_SECTION_ID,
Gam_PutParams,
"play.svg",
Txt_New_match);
2019-05-20 10:35:57 +02:00
break;
default:
break;
}
2019-05-20 08:52:07 +02:00
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out,"</td>");
2019-05-20 08:52:07 +02:00
/***** Start date/time *****/
2017-07-09 20:31:40 +02:00
UniqueId++;
fprintf (Gbl.F.Out,"<td id=\"gam_date_start_%u\" class=\"%s LEFT_TOP",
UniqueId,
Game.Status.Visible ? (Game.Status.Open ? "DATE_GREEN" :
2017-09-13 16:24:29 +02:00
"DATE_RED") :
(Game.Status.Open ? "DATE_GREEN_LIGHT" :
"DATE_RED_LIGHT"));
if (!ShowOnlyThisGame)
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out," COLOR%u",Gbl.RowEvenOdd);
2019-07-04 16:06:31 +02:00
fprintf (Gbl.F.Out,"\">");
if (Game.TimeUTC[Gam_START_TIME])
fprintf (Gbl.F.Out,"<script type=\"text/javascript\">"
"writeLocalDateHMSFromUTC('gam_date_start_%u',%ld,"
"%u,'<br />','%s',true,true,0x7);"
"</script>",
UniqueId,Game.TimeUTC[Gam_START_TIME],
(unsigned) Gbl.Prefs.DateFormat,Txt_Today);
fprintf (Gbl.F.Out,"</td>");
2017-07-09 20:31:40 +02:00
2019-05-20 08:52:07 +02:00
/***** End date/time *****/
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out,"<td id=\"gam_date_end_%u\" class=\"%s LEFT_TOP",
UniqueId,
Game.Status.Visible ? (Game.Status.Open ? "DATE_GREEN" :
"DATE_RED") :
(Game.Status.Open ? "DATE_GREEN_LIGHT" :
"DATE_RED_LIGHT"));
2017-09-13 16:24:29 +02:00
if (!ShowOnlyThisGame)
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out," COLOR%u",Gbl.RowEvenOdd);
2019-07-04 16:06:31 +02:00
fprintf (Gbl.F.Out,"\">");
if (Game.TimeUTC[Gam_END_TIME])
fprintf (Gbl.F.Out,"<script type=\"text/javascript\">"
"writeLocalDateHMSFromUTC('gam_date_end_%u',%ld,"
"%u,'<br />','%s',false,true,0x7);"
"</script>",
UniqueId,Game.TimeUTC[Gam_END_TIME],
(unsigned) Gbl.Prefs.DateFormat,Txt_Today);
fprintf (Gbl.F.Out,"</td>");
2017-07-09 20:31:40 +02:00
2019-05-20 08:52:07 +02:00
/***** Game title and main data *****/
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out,"<td class=\"LEFT_TOP");
2017-09-13 16:24:29 +02:00
if (!ShowOnlyThisGame)
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out," COLOR%u",Gbl.RowEvenOdd);
fprintf (Gbl.F.Out,"\">");
2019-05-20 08:52:07 +02:00
/* Game title */
2019-04-20 22:40:57 +02:00
Lay_StartArticle (Anchor);
2019-05-20 08:52:07 +02:00
Frm_StartForm (ActSeeGam);
2017-09-07 18:38:18 +02:00
Gam_PutParamGameCod (GamCod);
2018-11-09 20:47:39 +01:00
Frm_LinkFormSubmit (Txt_View_game,
2017-07-09 20:31:40 +02:00
Game.Status.Visible ? "ASG_TITLE" :
2017-09-07 18:38:18 +02:00
"ASG_TITLE_LIGHT",NULL);
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out,"%s</a>",
Game.Title);
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2019-04-20 22:40:57 +02:00
Lay_EndArticle ();
2017-07-09 20:31:40 +02:00
2019-05-20 08:52:07 +02:00
/* Number of questions and number of distinct students who have already answered */
fprintf (Gbl.F.Out,"<div class=\"%s\">%s: %u; %s: %u</div>",
2017-07-09 20:31:40 +02:00
Game.Status.Visible ? "ASG_GRP" :
2017-09-13 16:24:29 +02:00
"ASG_GRP_LIGHT",
2017-07-09 20:31:40 +02:00
Txt_No_of_questions,
Game.NumQsts,
Txt_No_of_users,
Game.NumUsrs);
2019-05-20 08:52:07 +02:00
fprintf (Gbl.F.Out,"</td>");
/***** End 1st row of this game *****/
fprintf (Gbl.F.Out,"</tr>");
2017-07-09 20:31:40 +02:00
2019-05-20 08:52:07 +02:00
/***** Start 2nd row of this game *****/
2019-05-20 10:35:57 +02:00
fprintf (Gbl.F.Out,"<tr>");
/***** Author of the game *****/
fprintf (Gbl.F.Out,"<td colspan=\"2\" class=\"LEFT_TOP");
2017-09-13 16:24:29 +02:00
if (!ShowOnlyThisGame)
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out," COLOR%u",Gbl.RowEvenOdd);
fprintf (Gbl.F.Out,"\">");
2017-09-07 18:38:18 +02:00
Gam_WriteAuthor (&Game);
2019-05-20 10:35:57 +02:00
fprintf (Gbl.F.Out,"</td>");
2017-07-09 20:31:40 +02:00
2019-05-20 10:35:57 +02:00
/***** Text of the game *****/
fprintf (Gbl.F.Out,"<td class=\"LEFT_TOP");
2017-09-13 16:24:29 +02:00
if (!ShowOnlyThisGame)
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out," COLOR%u",Gbl.RowEvenOdd);
fprintf (Gbl.F.Out,"\">");
2017-09-07 18:38:18 +02:00
Gam_GetGameTxtFromDB (Game.GamCod,Txt);
2017-07-09 20:31:40 +02:00
Str_ChangeFormat (Str_FROM_HTML,Str_TO_RIGOROUS_HTML,
Txt,Cns_MAX_BYTES_TEXT,false); // Convert from HTML to rigorous HTML
Str_InsertLinks (Txt,Cns_MAX_BYTES_TEXT,60); // Insert links
2017-10-24 11:26:01 +02:00
fprintf (Gbl.F.Out,"<div class=\"PAR %s\">%s</div>"
2019-05-20 10:35:57 +02:00
"</td>",
2017-07-09 20:31:40 +02:00
Game.Status.Visible ? "DAT" :
2017-09-13 16:24:29 +02:00
"DAT_LIGHT",
2017-07-09 20:31:40 +02:00
Txt);
2019-05-20 10:35:57 +02:00
/***** End 2nd row of this game *****/
fprintf (Gbl.F.Out,"</tr>");
2017-09-13 16:24:29 +02:00
/***** End table *****/
if (ShowOnlyThisGame)
2017-07-09 20:31:40 +02:00
Tbl_EndTable ();
2017-09-13 16:24:29 +02:00
else
Gbl.RowEvenOdd = 1 - Gbl.RowEvenOdd;
2019-04-20 22:40:57 +02:00
/***** Free anchor string *****/
Frm_FreeAnchorStr (Anchor);
2019-05-28 15:06:53 +02:00
if (ShowOnlyThisGame)
{
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_STD:
/* Put big button to play match */
if (PutButtonToPlay)
Gam_PutBigButtonToPlayMatchStd (Game.GamCod); // TODO: Change to match !!!!!!!!!!!!!!!!!!
break;
case Rol_NET:
case Rol_TCH:
case Rol_SYS_ADM:
/* List played matches */
Gam_ListPlayedMatches (&Game,PutFormNewMatch);
break;
default:
break;
}
2019-05-20 10:35:57 +02:00
2019-05-28 15:06:53 +02:00
/***** Write questions of this game *****/
if (ListGameQuestions)
Gam_ListGameQuestions (&Game);
2017-07-09 20:31:40 +02:00
2019-05-28 15:06:53 +02:00
/***** End box *****/
2017-07-09 20:31:40 +02:00
Box_EndBox ();
2019-05-28 15:06:53 +02:00
}
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/*********************** Write the author of a game ************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_WriteAuthor (struct Game *Game)
2017-07-09 20:31:40 +02:00
{
Usr_WriteAuthor1Line (Game->UsrCod,!Game->Status.Visible);
}
/*****************************************************************************/
2017-09-13 21:22:52 +02:00
/********** Get parameter with the type or order in list of games ************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_GetParamGameOrder (void)
2017-07-09 20:31:40 +02:00
{
2017-09-07 18:38:18 +02:00
Gbl.Games.SelectedOrder = (Gam_Order_t)
2017-09-13 21:22:52 +02:00
Par_GetParToUnsignedLong ("Order",
0,
Gam_NUM_ORDERS - 1,
(unsigned long) Gam_ORDER_DEFAULT);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2017-09-13 21:22:52 +02:00
/****** Put a hidden parameter with the type of order in list of games *******/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_PutHiddenParamGameOrder (void)
2017-07-09 20:31:40 +02:00
{
Par_PutHiddenParamUnsigned ("Order",(unsigned) Gbl.Games.SelectedOrder);
}
/*****************************************************************************/
2017-09-01 14:36:25 +02:00
/******************** Put a link (form) to edit one game *********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-04-20 22:40:57 +02:00
static void Gam_PutFormsToRemEditOneGame (const struct Game *Game,
2019-05-20 08:52:07 +02:00
const char *Anchor)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_Reset;
2017-09-13 12:25:45 +02:00
extern const char *Txt_Play;
2017-07-09 20:31:40 +02:00
2017-09-13 12:25:45 +02:00
/***** Put icon to remove game *****/
2017-09-07 18:38:18 +02:00
Ico_PutContextualIconToRemove (ActReqRemGam,Gam_PutParams);
2017-07-09 20:31:40 +02:00
2017-09-13 12:25:45 +02:00
/***** Put icon to reset game *****/
2019-01-12 03:00:59 +01:00
Lay_PutContextualLinkOnlyIcon (ActReqRstGam,NULL,Gam_PutParams,
"recycle.svg",
Txt_Reset);
2017-07-09 20:31:40 +02:00
2017-09-13 12:25:45 +02:00
/***** Put icon to hide/show game *****/
2019-04-20 22:40:57 +02:00
if (Game->Status.Visible)
Ico_PutContextualIconToHide (ActHidGam,Anchor,Gam_PutParams);
2017-07-09 20:31:40 +02:00
else
2019-04-20 22:40:57 +02:00
Ico_PutContextualIconToUnhide (ActShoGam,Anchor,Gam_PutParams);
2017-07-09 20:31:40 +02:00
2017-09-13 12:25:45 +02:00
/***** Put icon to edit game *****/
2017-09-07 18:38:18 +02:00
Ico_PutContextualIconToEdit (ActEdiOneGam,Gam_PutParams);
2017-07-09 20:31:40 +02:00
}
2017-12-20 19:01:49 +01:00
/*****************************************************************************/
/******************** Params used to edit/play a game ************************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_PutParams (void)
2017-07-09 20:31:40 +02:00
{
2019-05-20 08:52:07 +02:00
if (Gam_CurrentGamCod > 0)
Gam_PutParamGameCod (Gam_CurrentGamCod);
2017-12-20 19:01:49 +01:00
2017-07-09 20:31:40 +02:00
Att_PutHiddenParamAttOrder ();
Grp_PutParamWhichGrps ();
2017-09-13 16:24:29 +02:00
Pag_PutHiddenParamPagNum (Pag_GAMES,Gbl.Games.CurrentPage);
2017-07-09 20:31:40 +02:00
}
2019-05-28 15:06:53 +02:00
static void Gam_PutParamCurrentMchCod (void)
{
if (Gam_CurrentMchCod > 0)
Gam_PutParamMatchCod (Gam_CurrentMchCod);
}
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
/*********************** Get list of all the games *************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_GetListGames (void)
2017-07-09 20:31:40 +02:00
{
2019-01-03 15:25:18 +01:00
static const char *OrderBySubQuery[Gam_NUM_ORDERS] =
{
2019-07-04 16:06:31 +02:00
"StartTime DESC,EndTime DESC,games.Title DESC", // Gam_ORDER_BY_START_DATE
"EndTime DESC,StartTime DESC,games.Title DESC", // Gam_ORDER_BY_END_DATE
2019-01-03 15:25:18 +01:00
};
2017-07-09 20:31:40 +02:00
MYSQL_RES *mysql_res;
MYSQL_ROW row;
2018-10-31 10:19:01 +01:00
unsigned long NumRows = 0; // Initialized to avoid warning
2017-07-09 20:31:40 +02:00
unsigned NumGame;
/***** Free list of games *****/
if (Gbl.Games.LstIsRead)
2017-09-07 18:38:18 +02:00
Gam_FreeListGames ();
2017-07-09 20:31:40 +02:00
/***** Get list of games from database *****/
2019-05-20 08:52:07 +02:00
NumRows = DB_QuerySELECT (&mysql_res,"can not get games",
2019-07-04 16:06:31 +02:00
"SELECT games.GamCod,"
"MIN(gam_matches.StartTime) AS StartTime,"
"MAX(gam_matches.EndTime) AS EndTime"
" FROM games"
" LEFT JOIN gam_matches"
" ON games.GamCod=gam_matches.GamCod"
" WHERE games.CrsCod=%ld"
" GROUP BY games.GamCod"
2019-05-20 08:52:07 +02:00
" ORDER BY %s",
2019-05-28 15:06:53 +02:00
Gbl.Hierarchy.Crs.CrsCod,
2019-05-20 08:52:07 +02:00
OrderBySubQuery[Gbl.Games.SelectedOrder]);
2017-07-09 20:31:40 +02:00
if (NumRows) // Games found...
{
Gbl.Games.Num = (unsigned) NumRows;
/***** Create list of games *****/
if ((Gbl.Games.LstGamCods = (long *) calloc (NumRows,sizeof (long))) == NULL)
2018-10-18 20:06:54 +02:00
Lay_NotEnoughMemoryExit ();
2017-07-09 20:31:40 +02:00
/***** Get the games codes *****/
for (NumGame = 0;
NumGame < Gbl.Games.Num;
NumGame++)
{
/* Get next game code */
row = mysql_fetch_row (mysql_res);
2017-09-11 19:06:46 +02:00
if ((Gbl.Games.LstGamCods[NumGame] = Str_ConvertStrCodToLongCod (row[0])) <= 0)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Error: wrong game code.");
}
}
else
Gbl.Games.Num = 0;
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
Gbl.Games.LstIsRead = true;
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/********************** Get game data using its code *************************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_GetDataOfGameByCod (struct Game *Game)
2017-07-09 20:31:40 +02:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRows;
/***** Get data of game from database *****/
2018-10-31 10:19:01 +01:00
NumRows = DB_QuerySELECT (&mysql_res,"can not get game data",
2019-07-04 16:06:31 +02:00
"SELECT games.GamCod," // row[0]
"games.Hidden," // row[1]
"games.UsrCod," // row[2]
"games.Title" // row[3]
2018-10-31 10:19:01 +01:00
" FROM games"
2019-07-04 16:06:31 +02:00
" LEFT JOIN gam_matches"
" ON games.GamCod=gam_matches.GamCod"
" WHERE games.GamCod=%ld",
2018-10-31 10:19:01 +01:00
Game->GamCod);
2017-07-09 20:31:40 +02:00
if (NumRows) // Game found...
{
/* Get row */
row = mysql_fetch_row (mysql_res);
/* Get code of the game (row[0]) */
Game->GamCod = Str_ConvertStrCodToLongCod (row[0]);
2019-05-20 08:52:07 +02:00
/* Get whether the game is hidden (row[1]) */
Game->Status.Visible = (row[1][0] == 'N');
2017-07-09 20:31:40 +02:00
2019-05-20 08:52:07 +02:00
/* Get author of the game (row[2]) */
Game->UsrCod = Str_ConvertStrCodToLongCod (row[2]);
2017-07-09 20:31:40 +02:00
2019-07-04 16:06:31 +02:00
/* Get the title of the game (row[3]) */
Str_Copy (Game->Title,row[3],
2019-05-28 15:06:53 +02:00
Gam_MAX_BYTES_TITLE);
2017-07-09 20:31:40 +02:00
/* Get number of questions and number of users who have already answer this game */
2017-09-07 18:38:18 +02:00
Game->NumQsts = Gam_GetNumQstsGame (Game->GamCod);
Game->NumUsrs = Gam_GetNumUsrsWhoHaveAnsweredGame (Game->GamCod);
2017-07-09 20:31:40 +02:00
/* Have I answered this game? */
2017-09-07 18:38:18 +02:00
Game->Status.IHaveAnswered = Gam_CheckIfIHaveAnsweredGame (Game->GamCod);
2017-07-09 20:31:40 +02:00
/* Can I view results of the game?
Can I edit game? */
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_STD:
2019-05-20 08:52:07 +02:00
Game->Status.ICanViewResults = Game->NumQsts != 0 &&
Game->Status.Visible &&
Game->Status.Open &&
Game->Status.IHaveAnswered;
2017-07-09 20:31:40 +02:00
Game->Status.ICanEdit = false;
break;
case Rol_NET:
2019-07-04 16:06:31 +02:00
Game->Status.ICanViewResults = Game->NumQsts != 0;
2017-07-09 20:31:40 +02:00
Game->Status.ICanEdit = false;
break;
case Rol_TCH:
case Rol_DEG_ADM:
case Rol_CTR_ADM:
case Rol_INS_ADM:
2019-07-04 16:06:31 +02:00
Game->Status.ICanViewResults = Game->NumQsts != 0;
2019-05-28 15:06:53 +02:00
Game->Status.ICanEdit = true;
2017-07-09 20:31:40 +02:00
break;
case Rol_SYS_ADM:
2019-05-20 08:52:07 +02:00
Game->Status.ICanViewResults = Game->NumQsts != 0;
2017-07-09 20:31:40 +02:00
Game->Status.ICanEdit = true;
break;
default:
Game->Status.ICanViewResults = false;
Game->Status.ICanEdit = false;
break;
}
}
else
{
/* Initialize to empty game */
Game->GamCod = -1L;
Game->UsrCod = -1L;
Game->Title[0] = '\0';
Game->NumQsts = 0;
Game->NumUsrs = 0;
2019-05-20 08:52:07 +02:00
Game->Status.Visible = true;
Game->Status.IHaveAnswered = false;
Game->Status.ICanViewResults = false;
Game->Status.ICanEdit = false;
2017-07-09 20:31:40 +02:00
}
2019-07-04 16:06:31 +02:00
/* Free structure that stores the query result */
2017-07-09 20:31:40 +02:00
DB_FreeMySQLResult (&mysql_res);
2019-07-04 16:06:31 +02:00
if (Game->GamCod > 0)
{
/***** Get start and end times from database *****/
NumRows = DB_QuerySELECT (&mysql_res,"can not get game data",
"SELECT UNIX_TIMESTAMP(MIN(StartTime))," // row[0]
"UNIX_TIMESTAMP(MAX(EndTime))" // row[1]
" FROM gam_matches"
" WHERE GamCod=%ld",
Game->GamCod);
if (NumRows)
{
/* Get row */
row = mysql_fetch_row (mysql_res);
/* Get start date (row[0] holds the start UTC time) */
Game->TimeUTC[Gam_START_TIME] = Dat_GetUNIXTimeFromStr (row[0]);
/* Get end date (row[1] holds the end UTC time) */
Game->TimeUTC[Gam_END_TIME ] = Dat_GetUNIXTimeFromStr (row[1]);
}
/* Free structure that stores the query result */
DB_FreeMySQLResult (&mysql_res);
/***** Get whether the game is open or closed *****/
Game->Status.Open = DB_QueryCOUNT ("can not get game data",
"SELECT COUNT(*) FROM gam_matches"
" WHERE GamCod=%ld"
" AND Finished='N'", // Matches not finished
Game->GamCod);
}
else
{
Game->TimeUTC[Gam_START_TIME] =
Game->TimeUTC[Gam_END_TIME ] = (time_t) 0;
Game->Status.Open = false;
}
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/***************************** Free list of games ****************************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_FreeListGames (void)
2017-07-09 20:31:40 +02:00
{
if (Gbl.Games.LstIsRead && Gbl.Games.LstGamCods)
{
/***** Free memory used by the list of games *****/
free ((void *) Gbl.Games.LstGamCods);
Gbl.Games.LstGamCods = NULL;
Gbl.Games.Num = 0;
Gbl.Games.LstIsRead = false;
}
}
/*****************************************************************************/
/********************** Get game text from database ************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_GetGameTxtFromDB (long GamCod,char Txt[Cns_MAX_BYTES_TEXT + 1])
2017-07-09 20:31:40 +02:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRows;
/***** Get text of game from database *****/
2018-10-31 10:19:01 +01:00
NumRows = DB_QuerySELECT (&mysql_res,"can not get game text",
"SELECT Txt FROM games WHERE GamCod=%ld",
GamCod);
2017-07-09 20:31:40 +02:00
/***** The result of the query must have one row or none *****/
if (NumRows == 1)
{
/* Get info text */
row = mysql_fetch_row (mysql_res);
Str_Copy (Txt,row[0],
Cns_MAX_BYTES_TEXT);
}
else
Txt[0] = '\0';
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
if (NumRows > 1)
Lay_ShowErrorAndExit ("Error when getting game text.");
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/******************** Write parameter with code of game **********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_PutParamGameCod (long GamCod)
2017-07-09 20:31:40 +02:00
{
Par_PutHiddenParamLong ("GamCod",GamCod);
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/********************* Get parameter with code of game ***********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
long Gam_GetParamGameCod (void)
2017-07-09 20:31:40 +02:00
{
/***** Get code of game *****/
return Par_GetParToLong ("GamCod");
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/******************** Write parameter with code of match **********************/
/*****************************************************************************/
2019-05-31 10:25:20 +02:00
static void Gam_PutParamMatchCod (long MchCod)
2019-05-28 15:06:53 +02:00
{
Par_PutHiddenParamLong ("MchCod",MchCod);
}
/*****************************************************************************/
/********************* Get parameter with code of match **********************/
/*****************************************************************************/
2019-05-31 10:25:20 +02:00
static long Gam_GetParamMatchCod (void)
2019-05-28 15:06:53 +02:00
{
/***** Get code of match *****/
return Par_GetParToLong ("MchCod");
}
/*****************************************************************************/
/*************** Ask for confirmation of removing of a game ******************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_AskRemGame (void)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_Do_you_really_want_to_remove_the_game_X;
extern const char *Txt_Remove_game;
struct Game Game;
/***** Get parameters *****/
2017-09-07 18:38:18 +02:00
Gam_GetParamGameOrder ();
2017-07-09 20:31:40 +02:00
Grp_GetParamWhichGrps ();
2017-09-13 16:24:29 +02:00
Gbl.Games.CurrentPage = Pag_GetParamPagNum (Pag_GAMES);
2017-07-09 20:31:40 +02:00
/***** Get game code *****/
2017-09-07 18:38:18 +02:00
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
/***** Get data of the game from database *****/
2017-09-07 18:38:18 +02:00
Gam_GetDataOfGameByCod (&Game);
2017-07-09 20:31:40 +02:00
if (!Game.Status.ICanEdit)
Lay_ShowErrorAndExit ("You can not remove this game.");
/***** Show question and button to remove game *****/
2019-05-20 08:52:07 +02:00
Gam_CurrentGamCod = Game.GamCod;
2019-02-17 01:14:55 +01:00
Ale_ShowAlertAndButton (ActRemGam,NULL,NULL,Gam_PutParams,
Btn_REMOVE_BUTTON,Txt_Remove_game,
Ale_QUESTION,Txt_Do_you_really_want_to_remove_the_game_X,
Game.Title);
2017-07-09 20:31:40 +02:00
/***** Show games again *****/
2017-09-07 18:38:18 +02:00
Gam_ListAllGames ();
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/******************************* Remove a game *******************************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_RemoveGame (void)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_Game_X_removed;
struct Game Game;
/***** Get game code *****/
2017-09-07 18:38:18 +02:00
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
/***** Get data of the game from database *****/
2017-09-07 18:38:18 +02:00
Gam_GetDataOfGameByCod (&Game);
2017-07-09 20:31:40 +02:00
if (!Game.Status.ICanEdit)
Lay_ShowErrorAndExit ("You can not remove this game.");
/***** Remove all the users in this game *****/
2018-11-02 22:00:31 +01:00
DB_QueryDELETE ("can not remove users who are answered a game",
"DELETE FROM gam_users WHERE GamCod=%ld",
Game.GamCod);
2017-07-09 20:31:40 +02:00
/***** Remove all the questions in this game *****/
2018-11-02 22:00:31 +01:00
DB_QueryDELETE ("can not remove questions of a game",
"DELETE FROM gam_questions WHERE GamCod=%ld",
Game.GamCod);
2017-07-09 20:31:40 +02:00
2019-05-28 15:06:53 +02:00
/***** 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 gam_grp USING gam_grp,gam_matches"
" WHERE gam_matches.GrpCod=%ld"
" AND gam_matches.MchCod=gam_grp.MchCod",
Game.GamCod);
/* Remove matches of the game */
DB_QueryDELETE ("can not remove matches of a game",
"DELETE FROM gam_matches WHERE GamCod=%ld",
Game.GamCod);
2017-07-09 20:31:40 +02:00
/***** Remove game *****/
2018-11-02 22:00:31 +01:00
DB_QueryDELETE ("can not remove game",
"DELETE FROM games WHERE GamCod=%ld",
Game.GamCod);
2017-07-09 20:31:40 +02:00
/***** Write message to show the change made *****/
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_SUCCESS,Txt_Game_X_removed,
Game.Title);
2017-07-09 20:31:40 +02:00
/***** Show games again *****/
2017-09-07 18:38:18 +02:00
Gam_ListAllGames ();
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/***************** Ask for confirmation of reset of a game *****************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_AskResetGame (void)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_Do_you_really_want_to_reset_the_game_X;
2019-05-20 08:52:07 +02:00
extern const char *Txt_Reset_game;
2017-07-09 20:31:40 +02:00
struct Game Game;
/***** Get parameters *****/
2017-09-07 18:38:18 +02:00
Gam_GetParamGameOrder ();
2017-07-09 20:31:40 +02:00
Grp_GetParamWhichGrps ();
2017-09-13 16:24:29 +02:00
Gbl.Games.CurrentPage = Pag_GetParamPagNum (Pag_GAMES);
2017-07-09 20:31:40 +02:00
/***** Get game code *****/
2017-09-07 18:38:18 +02:00
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
/***** Get data of the game from database *****/
2017-09-07 18:38:18 +02:00
Gam_GetDataOfGameByCod (&Game);
2017-07-09 20:31:40 +02:00
if (!Game.Status.ICanEdit)
Lay_ShowErrorAndExit ("You can not reset this game.");
/***** Ask for confirmation of reset *****/
2019-05-20 08:52:07 +02:00
Gam_CurrentGamCod = Game.GamCod;
Ale_ShowAlertAndButton (ActRstGam,NULL,NULL,
Gam_PutParams,
Btn_REMOVE_BUTTON,
Txt_Reset_game,
Ale_QUESTION,Txt_Do_you_really_want_to_reset_the_game_X,
Game.Title);
2017-07-09 20:31:40 +02:00
/***** Show games again *****/
2017-09-07 18:38:18 +02:00
Gam_ListAllGames ();
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/******************************* Reset a game ******************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_ResetGame (void)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_Game_X_reset;
struct Game Game;
/***** Get game code *****/
2017-09-07 18:38:18 +02:00
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
/***** Get data of the game from database *****/
2017-09-07 18:38:18 +02:00
Gam_GetDataOfGameByCod (&Game);
2017-07-09 20:31:40 +02:00
if (!Game.Status.ICanEdit)
Lay_ShowErrorAndExit ("You can not reset this game.");
/***** Remove all the users in this game *****/
2018-11-02 22:00:31 +01:00
DB_QueryDELETE ("can not remove users who are answered a game",
"DELETE FROM gam_users WHERE GamCod=%ld",
Game.GamCod);
2017-07-09 20:31:40 +02:00
/***** Reset all the answers in this game *****/
2018-11-03 12:16:40 +01:00
DB_QueryUPDATE ("can not reset answers of a game",
"UPDATE gam_answers,gam_questions"
" SET gam_answers.NumUsrs=0"
" WHERE gam_questions.GamCod=%ld"
" AND gam_questions.QstCod=gam_answers.QstCod",
Game.GamCod);
2017-07-09 20:31:40 +02:00
/***** Write message to show the change made *****/
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_SUCCESS,Txt_Game_X_reset,
Game.Title);
2017-07-09 20:31:40 +02:00
/***** Show games again *****/
2017-09-07 18:38:18 +02:00
Gam_ListAllGames ();
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/******************************** Hide a game ******************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_HideGame (void)
2017-07-09 20:31:40 +02:00
{
struct Game Game;
/***** Get game code *****/
2017-09-07 18:38:18 +02:00
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
/***** Get data of the game from database *****/
2017-09-07 18:38:18 +02:00
Gam_GetDataOfGameByCod (&Game);
2017-07-09 20:31:40 +02:00
if (!Game.Status.ICanEdit)
Lay_ShowErrorAndExit ("You can not hide this game.");
/***** Hide game *****/
2018-11-03 12:16:40 +01:00
DB_QueryUPDATE ("can not hide game",
"UPDATE games SET Hidden='Y' WHERE GamCod=%ld",
Game.GamCod);
2017-07-09 20:31:40 +02:00
/***** Show games again *****/
2017-09-07 18:38:18 +02:00
Gam_ListAllGames ();
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/******************************** Show a game ******************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_UnhideGame (void)
2017-07-09 20:31:40 +02:00
{
struct Game Game;
/***** Get game code *****/
2017-09-07 18:38:18 +02:00
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
/***** Get data of the game from database *****/
2017-09-07 18:38:18 +02:00
Gam_GetDataOfGameByCod (&Game);
2017-07-09 20:31:40 +02:00
if (!Game.Status.ICanEdit)
Lay_ShowErrorAndExit ("You can not unhide this game.");
/***** Show game *****/
2018-11-03 12:16:40 +01:00
DB_QueryUPDATE ("can not show game",
"UPDATE games SET Hidden='N' WHERE GamCod=%ld",
Game.GamCod);
2017-07-09 20:31:40 +02:00
/***** Show games again *****/
2017-09-07 18:38:18 +02:00
Gam_ListAllGames ();
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/******************* Check if the title of a game exists *******************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static bool Gam_CheckIfSimilarGameExists (struct Game *Game)
2017-07-09 20:31:40 +02:00
{
/***** Get number of games with a field value from database *****/
2018-11-03 20:52:00 +01:00
return (DB_QueryCOUNT ("can not get similar games",
"SELECT COUNT(*) FROM games"
2019-05-20 08:52:07 +02:00
" WHERE CrsCod=%ld AND Title='%s'"
" AND GamCod<>%ld",
Gbl.Hierarchy.Crs.CrsCod,Game->Title,
Game->GamCod) != 0);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/********************* Put a form to create a new game *********************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_RequestCreatOrEditGame (void)
2017-07-09 20:31:40 +02:00
{
extern const char *Hlp_ASSESSMENT_Games_new_game;
extern const char *Hlp_ASSESSMENT_Games_edit_game;
2019-02-22 21:47:50 +01:00
extern const char *The_ClassFormInBox[The_NUM_THEMES];
2017-07-09 20:31:40 +02:00
extern const char *Txt_New_game;
extern const char *Txt_Edit_game;
extern const char *Txt_Title;
extern const char *Txt_Description;
extern const char *Txt_Create_game;
2019-02-18 18:27:45 +01:00
extern const char *Txt_Save_changes;
2017-07-09 20:31:40 +02:00
struct Game Game;
bool ItsANewGame;
char Txt[Cns_MAX_BYTES_TEXT + 1];
/***** Get parameters *****/
2017-09-07 18:38:18 +02:00
Gam_GetParamGameOrder ();
2017-07-09 20:31:40 +02:00
Grp_GetParamWhichGrps ();
2017-09-13 16:24:29 +02:00
Gbl.Games.CurrentPage = Pag_GetParamPagNum (Pag_GAMES);
2017-07-09 20:31:40 +02:00
/***** Get the code of the game *****/
2017-09-07 18:38:18 +02:00
ItsANewGame = ((Game.GamCod = Gam_GetParamGameCod ()) == -1L);
2017-07-09 20:31:40 +02:00
/***** Get from the database the data of the game *****/
if (ItsANewGame)
{
/***** Put link (form) to create new game *****/
2017-09-07 18:38:18 +02:00
if (!Gam_CheckIfICanCreateGame ())
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("You can not create a new game here.");
/* Initialize to empty game */
Game.GamCod = -1L;
Game.UsrCod = Gbl.Usrs.Me.UsrDat.UsrCod;
2017-09-07 18:38:18 +02:00
Game.TimeUTC[Gam_START_TIME] = Gbl.StartExecutionTimeUTC;
Game.TimeUTC[Gam_END_TIME ] = Gbl.StartExecutionTimeUTC + (24 * 60 * 60); // +24 hours
2019-05-20 08:52:07 +02:00
Game.Title[0] = '\0';
Game.NumQsts = 0;
Game.NumUsrs = 0;
Game.Status.Visible = true;
Game.Status.Open = true;
Game.Status.IHaveAnswered = false;
Game.Status.ICanViewResults = false;
2017-07-09 20:31:40 +02:00
}
else
{
/* Get data of the game from database */
2017-09-07 18:38:18 +02:00
Gam_GetDataOfGameByCod (&Game);
2017-07-09 20:31:40 +02:00
if (!Game.Status.ICanEdit)
Lay_ShowErrorAndExit ("You can not update this game.");
/* Get text of the game from database */
2017-09-07 18:38:18 +02:00
Gam_GetGameTxtFromDB (Game.GamCod,Txt);
2017-07-09 20:31:40 +02:00
}
/***** Start form *****/
2019-05-20 08:52:07 +02:00
Gam_CurrentGamCod = Game.GamCod;
2018-11-09 20:47:39 +01:00
Frm_StartForm (ItsANewGame ? ActNewGam :
2017-07-09 20:31:40 +02:00
ActChgGam);
2017-09-07 18:38:18 +02:00
Gam_PutParams ();
2017-07-09 20:31:40 +02:00
/***** Start box and table *****/
if (ItsANewGame)
Box_StartBoxTable (NULL,Txt_New_game,NULL,
Hlp_ASSESSMENT_Games_new_game,Box_NOT_CLOSABLE,2);
else
2017-10-01 11:57:25 +02:00
Box_StartBoxTable (NULL,
Game.Title[0] ? Game.Title :
Txt_Edit_game,
NULL,
2017-07-09 20:31:40 +02:00
Hlp_ASSESSMENT_Games_edit_game,Box_NOT_CLOSABLE,2);
/***** Game title *****/
fprintf (Gbl.F.Out,"<tr>"
"<td class=\"RIGHT_MIDDLE\">"
"<label for=\"Title\" class=\"%s\">%s:</label>"
"</td>"
"<td class=\"LEFT_MIDDLE\">"
"<input type=\"text\" id=\"Title\" name=\"Title\""
" size=\"45\" maxlength=\"%u\" value=\"%s\""
" required=\"required\" />"
"</td>"
"</tr>",
2019-02-22 21:47:50 +01:00
The_ClassFormInBox[Gbl.Prefs.Theme],
2017-07-09 20:31:40 +02:00
Txt_Title,
2019-05-28 15:06:53 +02:00
Gam_MAX_CHARS_TITLE,Game.Title);
2017-07-09 20:31:40 +02:00
/***** Game start and end dates *****/
2019-07-04 16:06:31 +02:00
// Dat_PutFormStartEndClientLocalDateTimes (Game.TimeUTC,Dat_FORM_SECONDS_ON);
2017-07-09 20:31:40 +02:00
/***** Game text *****/
fprintf (Gbl.F.Out,"<tr>"
"<td class=\"RIGHT_TOP\">"
"<label for=\"Txt\" class=\"%s\">%s:</label>"
"</td>"
"<td class=\"LEFT_TOP\">"
"<textarea id=\"Txt\" name=\"Txt\""
" cols=\"60\" rows=\"10\">",
2019-02-22 21:47:50 +01:00
The_ClassFormInBox[Gbl.Prefs.Theme],
2017-07-09 20:31:40 +02:00
Txt_Description);
if (!ItsANewGame)
fprintf (Gbl.F.Out,"%s",Txt);
fprintf (Gbl.F.Out,"</textarea>"
"</td>"
"</tr>");
/***** End table, send button and end box *****/
if (ItsANewGame)
Box_EndBoxTableWithButton (Btn_CREATE_BUTTON,Txt_Create_game);
else
2019-02-18 18:27:45 +01:00
Box_EndBoxTableWithButton (Btn_CONFIRM_BUTTON,Txt_Save_changes);
2017-07-09 20:31:40 +02:00
/***** End form *****/
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2017-07-09 20:31:40 +02:00
/***** Show questions of the game ready to be edited *****/
if (!ItsANewGame)
2017-09-07 18:38:18 +02:00
Gam_ListGameQuestions (&Game);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/******************** Show list of groups to edit a game *******************/
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
static void Gam_ShowLstGrpsToEditMatch (void)
2017-07-09 20:31:40 +02:00
{
2019-02-22 21:47:50 +01:00
extern const char *The_ClassFormInBox[The_NUM_THEMES];
2017-07-09 20:31:40 +02:00
extern const char *Txt_Groups;
extern const char *Txt_The_whole_course;
unsigned NumGrpTyp;
/***** Get list of groups types and groups in this course *****/
Grp_GetListGrpTypesAndGrpsInThisCrs (Grp_ONLY_GROUP_TYPES_WITH_GROUPS);
2019-04-04 10:45:15 +02:00
if (Gbl.Crs.Grps.GrpTypes.Num)
2017-07-09 20:31:40 +02:00
{
/***** Start box and table *****/
fprintf (Gbl.F.Out,"<tr>"
"<td class=\"%s RIGHT_TOP\">"
"%s:"
"</td>"
"<td class=\"LEFT_TOP\">",
2019-02-22 21:47:50 +01:00
The_ClassFormInBox[Gbl.Prefs.Theme],
2017-07-09 20:31:40 +02:00
Txt_Groups);
Box_StartBoxTable ("95%",NULL,NULL,
NULL,Box_NOT_CLOSABLE,0);
/***** First row: checkbox to select the whole course *****/
fprintf (Gbl.F.Out,"<tr>"
"<td colspan=\"7\" class=\"DAT LEFT_MIDDLE\">"
"<label>"
"<input type=\"checkbox\""
2019-05-31 10:25:20 +02:00
" id=\"WholeCrs\" name=\"WholeCrs\" value=\"Y\""
" checked=\"checked\""
" onclick=\"uncheckChildren(this,'GrpCods')\" />"
2017-07-09 20:31:40 +02:00
"%s %s"
"</label>"
"</td>"
"</tr>",
2019-04-04 10:45:15 +02:00
Txt_The_whole_course,Gbl.Hierarchy.Crs.ShrtName);
2017-07-09 20:31:40 +02:00
/***** List the groups for each group type *****/
for (NumGrpTyp = 0;
2019-04-04 10:45:15 +02:00
NumGrpTyp < Gbl.Crs.Grps.GrpTypes.Num;
2017-07-09 20:31:40 +02:00
NumGrpTyp++)
2019-04-04 10:45:15 +02:00
if (Gbl.Crs.Grps.GrpTypes.LstGrpTypes[NumGrpTyp].NumGrps)
2019-05-28 15:06:53 +02:00
Grp_ListGrpsToEditAsgAttSvyMch (&Gbl.Crs.Grps.GrpTypes.LstGrpTypes[NumGrpTyp],
-1L, // -1 means "New match"
Grp_MATCH);
2017-07-09 20:31:40 +02:00
/***** End table and box *****/
Box_EndBoxTable ();
fprintf (Gbl.F.Out,"</td>"
"</tr>");
}
/***** Free list of groups types and groups in this course *****/
Grp_FreeListGrpTypesAndGrps ();
}
/*****************************************************************************/
/********************* Receive form to create a new game *******************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_RecFormGame (void)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_Already_existed_a_game_with_the_title_X;
extern const char *Txt_You_must_specify_the_title_of_the_game;
struct Game OldGame;
struct Game NewGame;
bool ItsANewGame;
bool NewGameIsCorrect = true;
char Txt[Cns_MAX_BYTES_TEXT + 1];
/***** Get the code of the game *****/
2017-09-07 18:38:18 +02:00
ItsANewGame = ((NewGame.GamCod = Gam_GetParamGameCod ()) == -1L);
2017-07-09 20:31:40 +02:00
2019-05-20 08:52:07 +02:00
if (!ItsANewGame)
2017-07-09 20:31:40 +02:00
{
/* Get data of the old (current) game from database */
OldGame.GamCod = NewGame.GamCod;
2017-09-07 18:38:18 +02:00
Gam_GetDataOfGameByCod (&OldGame);
2017-07-09 20:31:40 +02:00
if (!OldGame.Status.ICanEdit)
Lay_ShowErrorAndExit ("You can not update this game.");
}
/***** Get game title *****/
2019-05-28 15:06:53 +02:00
Par_GetParToText ("Title",NewGame.Title,Gam_MAX_BYTES_TITLE);
2017-07-09 20:31:40 +02:00
/***** Get game text and insert links *****/
Par_GetParToHTML ("Txt",Txt,Cns_MAX_BYTES_TEXT); // Store in HTML format (not rigorous)
/***** Check if title is correct *****/
if (NewGame.Title[0]) // If there's a game title
{
/* If title of game was in database... */
2017-09-07 18:38:18 +02:00
if (Gam_CheckIfSimilarGameExists (&NewGame))
2017-07-09 20:31:40 +02:00
{
NewGameIsCorrect = false;
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_WARNING,Txt_Already_existed_a_game_with_the_title_X,
NewGame.Title);
2017-07-09 20:31:40 +02:00
}
}
else // If there is not a game title
{
NewGameIsCorrect = false;
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_WARNING,Txt_You_must_specify_the_title_of_the_game);
2017-07-09 20:31:40 +02:00
}
/***** Create a new game or update an existing one *****/
if (NewGameIsCorrect)
{
/* Get groups for this games */
Grp_GetParCodsSeveralGrps ();
if (ItsANewGame)
2017-09-07 18:38:18 +02:00
Gam_CreateGame (&NewGame,Txt); // Add new game to database
2017-07-09 20:31:40 +02:00
else
2017-09-07 18:38:18 +02:00
Gam_UpdateGame (&NewGame,Txt);
2017-07-09 20:31:40 +02:00
/* Free memory for list of selected groups */
Grp_FreeListCodSelectedGrps ();
}
else
2017-09-07 18:38:18 +02:00
Gam_RequestCreatOrEditGame ();
2017-07-09 20:31:40 +02:00
/***** Show games again *****/
2017-09-07 18:38:18 +02:00
Gam_ListAllGames ();
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2017-09-13 09:47:45 +02:00
/**************************** Create a new game ******************************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_CreateGame (struct Game *Game,const char *Txt)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_Created_new_game_X;
/***** Create a new game *****/
2018-11-03 01:45:36 +01:00
Game->GamCod =
DB_QueryINSERTandReturnCode ("can not create new game",
"INSERT INTO games"
2019-07-04 16:06:31 +02:00
" (CrsCod,Hidden,UsrCod,Title,Txt)"
2018-11-03 01:45:36 +01:00
" VALUES"
2019-07-04 16:06:31 +02:00
" (%ld,'N',%ld,'%s','%s')",
2019-05-20 08:52:07 +02:00
Gbl.Hierarchy.Crs.CrsCod,
2018-11-03 01:45:36 +01:00
Gbl.Usrs.Me.UsrDat.UsrCod,
Game->Title,
Txt);
2017-07-09 20:31:40 +02:00
/***** Write success message *****/
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_SUCCESS,Txt_Created_new_game_X,
Game->Title);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/************************* Update an existing game *************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_UpdateGame (struct Game *Game,const char *Txt)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_The_game_has_been_modified;
/***** Update the data of the game *****/
2018-11-03 12:16:40 +01:00
DB_QueryUPDATE ("can not update game",
"UPDATE games"
2019-05-20 08:52:07 +02:00
" SET CrsCod=%ld,"
"Title='%s',"
"Txt='%s'"
2018-11-03 12:16:40 +01:00
" WHERE GamCod=%ld",
2019-05-20 08:52:07 +02:00
Gbl.Hierarchy.Crs.CrsCod,
2018-11-03 12:16:40 +01:00
Game->Title,
Txt,
Game->GamCod);
2017-07-09 20:31:40 +02:00
/***** Write success message *****/
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_SUCCESS,Txt_The_game_has_been_modified);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/************* Check if a match is associated to a given group ***************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
bool Gam_CheckIfMatchIsAssociatedToGrp (long MchCod,long GrpCod)
2017-07-09 20:31:40 +02:00
{
2019-05-28 15:06:53 +02:00
/***** Get if a match is associated to a group from database *****/
return (DB_QueryCOUNT ("can not check if a match is associated to a group",
2018-11-03 20:52:00 +01:00
"SELECT COUNT(*) FROM gam_grp"
2019-05-28 15:06:53 +02:00
" WHERE MchCod=%ld AND GrpCod=%ld",
MchCod,GrpCod) != 0);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/******************** Remove one group from all the games ********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-05-29 01:14:56 +02:00
// TODO: Check if this function should be called when removing group
2017-07-09 20:31:40 +02:00
2017-09-07 18:38:18 +02:00
void Gam_RemoveGroup (long GrpCod)
2017-07-09 20:31:40 +02:00
{
/***** Remove group from all the games *****/
2018-11-02 22:00:31 +01:00
DB_QueryDELETE ("can not remove group"
" from the associations between games and groups",
"DELETE FROM gam_grp WHERE GrpCod=%ld",
GrpCod);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/**************** Remove groups of one type from all the games ***************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-05-29 01:14:56 +02:00
// TODO: Check if this function should be called when removing group type
2017-07-09 20:31:40 +02:00
2017-09-07 18:38:18 +02:00
void Gam_RemoveGroupsOfType (long GrpTypCod)
2017-07-09 20:31:40 +02:00
{
/***** Remove group from all the games *****/
2018-11-02 22:00:31 +01:00
DB_QueryDELETE ("can not remove groups of a type"
" from the associations between games and groups",
"DELETE FROM gam_grp USING crs_grp,gam_grp"
" WHERE crs_grp.GrpTypCod=%ld"
" AND crs_grp.GrpCod=gam_grp.GrpCod",
GrpTypCod);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/******************* Create groups associated to a match *********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
static void Gam_CreateGrps (long MchCod)
2017-07-09 20:31:40 +02:00
{
unsigned NumGrpSel;
2019-05-28 15:06:53 +02:00
/***** Create groups associated to the match *****/
2017-07-09 20:31:40 +02:00
for (NumGrpSel = 0;
2019-04-04 10:45:15 +02:00
NumGrpSel < Gbl.Crs.Grps.LstGrpsSel.NumGrps;
2017-07-09 20:31:40 +02:00
NumGrpSel++)
/* Create group */
2019-05-28 15:06:53 +02:00
DB_QueryINSERT ("can not associate a group to a match",
2018-11-02 19:37:11 +01:00
"INSERT INTO gam_grp"
2019-05-28 15:06:53 +02:00
" (MchCod,GrpCod)"
2018-11-02 19:37:11 +01:00
" VALUES"
" (%ld,%ld)",
2019-05-28 15:06:53 +02:00
MchCod,Gbl.Crs.Grps.LstGrpsSel.GrpCods[NumGrpSel]);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/************* Get and write the names of the groups of a match **************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
static void Gam_GetAndWriteNamesOfGrpsAssociatedToMatch (struct Match *Match)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_Group;
extern const char *Txt_Groups;
extern const char *Txt_and;
extern const char *Txt_The_whole_course;
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRow;
unsigned long NumRows;
2019-05-28 15:06:53 +02:00
/***** Get groups associated to a match from database *****/
NumRows = DB_QuerySELECT (&mysql_res,"can not get groups of a match",
2018-10-31 10:19:01 +01:00
"SELECT crs_grp_types.GrpTypName,crs_grp.GrpName"
" FROM gam_grp,crs_grp,crs_grp_types"
2019-05-28 15:06:53 +02:00
" WHERE gam_grp.MchCod=%ld"
2018-10-31 10:19:01 +01:00
" AND gam_grp.GrpCod=crs_grp.GrpCod"
" AND crs_grp.GrpTypCod=crs_grp_types.GrpTypCod"
" ORDER BY crs_grp_types.GrpTypName,crs_grp.GrpName",
2019-05-28 15:06:53 +02:00
Match->MchCod);
2017-07-09 20:31:40 +02:00
/***** Write heading *****/
2019-05-28 15:06:53 +02:00
fprintf (Gbl.F.Out,"<div class=\"ASG_GRP\">%s: ",
2017-07-09 20:31:40 +02:00
NumRows == 1 ? Txt_Group :
Txt_Groups);
/***** Write groups *****/
if (NumRows) // Groups found...
{
/* Get and write the group types and names */
for (NumRow = 0;
NumRow < NumRows;
NumRow++)
{
/* Get next group */
row = mysql_fetch_row (mysql_res);
/* Write group type name and group name */
fprintf (Gbl.F.Out,"%s %s",row[0],row[1]);
if (NumRows >= 2)
{
if (NumRow == NumRows-2)
fprintf (Gbl.F.Out," %s ",Txt_and);
if (NumRows >= 3)
if (NumRow < NumRows-2)
fprintf (Gbl.F.Out,", ");
}
}
}
else
fprintf (Gbl.F.Out,"%s %s",
2019-04-04 10:45:15 +02:00
Txt_The_whole_course,Gbl.Hierarchy.Crs.ShrtName);
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out,"</div>");
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/************* Remove all the games of a place on the hierarchy **************/
/************* (country, institution, centre, degree or course) **************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-04-03 20:57:04 +02:00
void Gam_RemoveGames (Hie_Level_t Scope,long Cod)
2017-07-09 20:31:40 +02:00
{
/***** Remove all the users in course games *****/
2018-11-02 22:00:31 +01:00
DB_QueryDELETE ("can not remove users who had answered games"
" in a place on the hierarchy",
"DELETE FROM gam_users"
" USING games,gam_users"
" WHERE games.Scope='%s' AND games.Cod=%ld"
" AND games.GamCod=gam_users.GamCod",
2019-04-01 23:15:17 +02:00
Sco_GetDBStrFromScope (Scope),Cod);
2017-07-09 20:31:40 +02:00
/***** Remove all the answers in course games *****/
2018-11-02 22:00:31 +01:00
DB_QueryDELETE ("can not remove answers of games"
" in a place on the hierarchy"
"DELETE FROM gam_answers"
" USING games,gam_questions,gam_answers"
" WHERE games.Scope='%s' AND games.Cod=%ld"
" AND games.GamCod=gam_questions.GamCod"
" AND gam_questions.QstCod=gam_answers.QstCod",
2019-04-01 23:15:17 +02:00
Sco_GetDBStrFromScope (Scope),Cod);
2017-07-09 20:31:40 +02:00
/***** Remove all the questions in course games *****/
2018-11-02 22:00:31 +01:00
DB_QueryDELETE ("can not remove questions of games"
" in a place on the hierarchy",
"DELETE FROM gam_questions"
" USING games,gam_questions"
" WHERE games.Scope='%s' AND games.Cod=%ld"
" AND games.GamCod=gam_questions.GamCod",
2019-04-01 23:15:17 +02:00
Sco_GetDBStrFromScope (Scope),Cod);
2017-07-09 20:31:40 +02:00
/***** Remove groups *****/
2018-11-02 22:00:31 +01:00
DB_QueryDELETE ("can not remove all the groups"
" associated to games of a course",
"DELETE FROM gam_grp"
" USING games,gam_grp"
" WHERE games.Scope='%s' AND games.Cod=%ld"
" AND games.GamCod=gam_grp.GamCod",
2019-04-01 23:15:17 +02:00
Sco_GetDBStrFromScope (Scope),Cod);
2017-07-09 20:31:40 +02:00
/***** Remove course games *****/
2018-11-02 22:00:31 +01:00
DB_QueryDELETE ("can not remove all the games in a place on the hierarchy",
"DELETE FROM games WHERE Scope='%s' AND Cod=%ld",
2019-04-01 23:15:17 +02:00
Sco_GetDBStrFromScope (Scope),Cod);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/************ Check if I belong to any of the groups of a match **************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
static bool Gam_CheckIfIPlayThisMatchBasedOnGrps (long MchCod)
2017-07-09 20:31:40 +02:00
{
2019-05-28 15:06:53 +02:00
/***** Get if I can play a match from database *****/
return (DB_QueryCOUNT ("can not check if I can play a match",
"SELECT COUNT(*) FROM gam_matches"
" WHERE MchCod=%ld"
" AND (MchCod NOT IN (SELECT MchCod FROM gam_grp) OR"
" MchCod IN (SELECT gam_grp.MchCod FROM gam_grp,crs_grp_usr"
2018-11-03 20:52:00 +01:00
" WHERE crs_grp_usr.UsrCod=%ld"
" AND gam_grp.GrpCod=crs_grp_usr.GrpCod))",
2019-05-28 15:06:53 +02:00
MchCod,Gbl.Usrs.Me.UsrDat.UsrCod) != 0);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/******************* Get number of questions of a game *********************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static unsigned Gam_GetNumQstsGame (long GamCod)
2017-07-09 20:31:40 +02:00
{
/***** Get data of questions from database *****/
2018-11-03 20:52:00 +01:00
return
(unsigned) DB_QueryCOUNT ("can not get number of questions of a game",
"SELECT COUNT(*) FROM gam_questions"
" WHERE GamCod=%ld",
GamCod);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/*********** Put a form to edit/create a question in game *****************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_RequestNewQuestion (void)
2017-07-09 20:31:40 +02:00
{
2017-09-01 00:52:19 +02:00
struct Game Game;
2017-07-09 20:31:40 +02:00
/***** Get game code *****/
2017-09-07 18:38:18 +02:00
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
/***** Get other parameters *****/
2017-09-07 18:38:18 +02:00
Gam_GetParamGameOrder ();
2017-07-09 20:31:40 +02:00
Grp_GetParamWhichGrps ();
2017-09-13 16:24:29 +02:00
Gbl.Games.CurrentPage = Pag_GetParamPagNum (Pag_GAMES);
2017-07-09 20:31:40 +02:00
/***** Show form to create a new question in this game *****/
2017-09-01 00:52:19 +02:00
Tst_ShowFormAskSelectTstsForGame (Game.GamCod);
2017-07-09 20:31:40 +02:00
/***** Show current game *****/
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Game.GamCod,
true, // Show only this game
true, // List game questions
2019-05-28 15:06:53 +02:00
false, // Do not put form to start new match
2019-05-20 10:35:57 +02:00
false); // Do not put button to play
2017-07-09 20:31:40 +02:00
}
2017-09-13 21:22:52 +02:00
/*****************************************************************************/
/****************** Write parameter with index of question *******************/
/*****************************************************************************/
static void Gam_PutParamQstInd (unsigned QstInd)
{
Par_PutHiddenParamUnsigned ("QstInd",QstInd);
}
/*****************************************************************************/
/******************* Get parameter with index of question ********************/
/*****************************************************************************/
static unsigned Gam_GetParamQstInd (void)
{
2019-05-30 12:57:31 +02:00
long LongNum;
2017-09-13 21:22:52 +02:00
2019-05-30 12:57:31 +02:00
LongNum = Par_GetParToLong ("QstInd");
if (LongNum < 0)
2017-09-13 21:22:52 +02:00
Lay_ShowErrorAndExit ("Wrong question index.");
2019-05-30 12:57:31 +02:00
return (unsigned) LongNum;
2017-09-13 21:22:52 +02:00
}
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-05-30 12:57:31 +02:00
/******************* Get parameter with index of question ********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-05-30 12:57:31 +02:00
static unsigned Gam_GetQstIndFromStr (const char *UnsignedStr)
2017-07-09 20:31:40 +02:00
{
2019-05-30 12:57:31 +02:00
long LongNum;
LongNum = Str_ConvertStrCodToLongCod (UnsignedStr);
return (LongNum > 0) ? (unsigned) LongNum :
0;
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2019-05-30 12:57:31 +02:00
/********************** Remove answers of a game question ********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-05-30 12:57:31 +02:00
static void Gam_RemAnswersOfAQuestion (long GamCod,unsigned QstInd)
2017-07-09 20:31:40 +02:00
{
2019-05-30 12:57:31 +02:00
/***** Remove answers *****/
DB_QueryDELETE ("can not remove the answers of a question",
"DELETE FROM gam_answers"
" WHERE GamCod=%ld AND QstInd=%u",
GamCod,QstInd);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2017-09-11 19:06:46 +02:00
/************ Get question code given game and index of question *************/
/*****************************************************************************/
static long Gam_GetQstCodFromQstInd (long GamCod,unsigned QstInd)
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
long QstCod;
/***** Get question code of thw question to be moved up *****/
2018-10-31 10:19:01 +01:00
if (!DB_QuerySELECT (&mysql_res,"can not get question code",
"SELECT QstCod FROM gam_questions"
" WHERE GamCod=%ld AND QstInd=%u",
GamCod,QstInd))
2017-09-11 19:06:46 +02:00
Lay_ShowErrorAndExit ("Error: wrong question code.");
/***** Get question code (row[0]) *****/
row = mysql_fetch_row (mysql_res);
if ((QstCod = Str_ConvertStrCodToLongCod (row[0])) <= 0)
Lay_ShowErrorAndExit ("Error: wrong question code.");
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
return QstCod;
}
/*****************************************************************************/
/****************** Get maximum question index in a game *********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-05-30 12:57:31 +02:00
// Question index can be 1, 2, 3...
// Return 0 if no questions
2017-07-09 20:31:40 +02:00
2019-05-30 12:57:31 +02:00
static unsigned Gam_GetMaxQuestionIndexInGame (long GamCod)
2017-07-09 20:31:40 +02:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
2019-05-30 12:57:31 +02:00
unsigned QstInd = 0;
2017-07-09 20:31:40 +02:00
2017-09-01 14:36:25 +02:00
/***** Get maximum question index in a game from database *****/
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get last question index",
"SELECT MAX(QstInd) FROM gam_questions WHERE GamCod=%ld",
GamCod);
2017-07-09 20:31:40 +02:00
row = mysql_fetch_row (mysql_res);
if (row[0]) // There are questions
2019-05-30 12:57:31 +02:00
if (sscanf (row[0],"%u",&QstInd) != 1)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Error when getting last question index.");
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
return QstInd;
}
2019-05-30 13:46:57 +02:00
/*****************************************************************************/
/******************** Get first question index in a game *********************/
/*****************************************************************************/
static unsigned Gam_GetFirstQuestionIndexInGame (long GamCod)
{
return Gam_GetNextQuestionIndexInGame (GamCod,
0); // First index > 0
}
2017-09-11 19:06:46 +02:00
/*****************************************************************************/
/*********** Get previous question index to a given index in a game **********/
/*****************************************************************************/
2019-05-30 13:46:57 +02:00
// Input question index can be 1, 2, 3... n-1
// Return question index will be 1, 2, 3... n if previous question exists, or 0 if no previous question
2017-09-11 19:06:46 +02:00
2019-05-30 12:57:31 +02:00
static unsigned Gam_GetPrevQuestionIndexInGame (long GamCod,unsigned QstInd)
2017-09-11 19:06:46 +02:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
2019-05-30 12:57:31 +02:00
unsigned PrevQstInd = 0;
2017-09-11 19:06:46 +02:00
/***** Get previous question index in a game from database *****/
// Although indexes are always continuous...
// ...this implementation works even with non continuous indexes
2018-10-31 10:19:01 +01:00
if (!DB_QuerySELECT (&mysql_res,"can not get previous question index",
"SELECT MAX(QstInd) FROM gam_questions"
2019-05-30 12:57:31 +02:00
" WHERE GamCod=%ld AND QstInd<%u",
2018-10-31 10:19:01 +01:00
GamCod,QstInd))
2017-09-11 19:06:46 +02:00
Lay_ShowErrorAndExit ("Error: previous question index not found.");
/***** Get previous question index (row[0]) *****/
row = mysql_fetch_row (mysql_res);
2017-09-14 02:32:36 +02:00
if (row[0])
2019-05-30 12:57:31 +02:00
if (sscanf (row[0],"%u",&PrevQstInd) != 1)
2017-09-14 02:32:36 +02:00
Lay_ShowErrorAndExit ("Error when getting previous question index.");
2017-09-11 19:06:46 +02:00
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
return PrevQstInd;
}
/*****************************************************************************/
/************* Get next question index to a given index in a game ************/
/*****************************************************************************/
2019-05-30 13:46:57 +02:00
// Input question index can be 0, 1, 2, 3... n-1
// Return question index will be 1, 2, 3... n if next question exists, or 0 if no next question
2017-09-11 19:06:46 +02:00
2019-05-30 12:57:31 +02:00
static unsigned Gam_GetNextQuestionIndexInGame (long GamCod,unsigned QstInd)
2017-09-11 19:06:46 +02:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
2019-05-30 12:57:31 +02:00
unsigned NextQstInd = 0;
2017-09-11 19:06:46 +02:00
/***** Get next question index in a game from database *****/
// Although indexes are always continuous...
// ...this implementation works even with non continuous indexes
2018-10-31 10:19:01 +01:00
if (!DB_QuerySELECT (&mysql_res,"can not get next question index",
"SELECT MIN(QstInd) FROM gam_questions"
2019-05-30 12:57:31 +02:00
" WHERE GamCod=%ld AND QstInd>%u",
2018-10-31 10:19:01 +01:00
GamCod,QstInd))
2017-09-11 19:06:46 +02:00
Lay_ShowErrorAndExit ("Error: next question index not found.");
/***** Get next question index (row[0]) *****/
row = mysql_fetch_row (mysql_res);
2017-09-14 02:32:36 +02:00
if (row[0])
2019-05-30 12:57:31 +02:00
if (sscanf (row[0],"%u",&NextQstInd) != 1)
2017-09-14 02:32:36 +02:00
Lay_ShowErrorAndExit ("Error when getting next question index.");
2017-09-11 19:06:46 +02:00
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
return NextQstInd;
}
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-06 15:03:43 +02:00
/************************ List the questions of a game ***********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_ListGameQuestions (struct Game *Game)
2017-07-09 20:31:40 +02:00
{
extern const char *Hlp_ASSESSMENT_Games_questions;
extern const char *Txt_Questions;
extern const char *Txt_This_game_has_no_questions;
extern const char *Txt_Done;
MYSQL_RES *mysql_res;
unsigned NumQsts;
bool Editing = (Gbl.Action.Act == ActEdiOneGam ||
2017-09-07 12:00:01 +02:00
Gbl.Action.Act == ActAddOneGamQst);
2017-09-01 14:36:25 +02:00
Tst_ActionToDoWithQuestions_t ActionToDoWithQuestions;
/***** How to show the questions ******/
2019-07-04 16:06:31 +02:00
if (Editing)
2017-09-01 14:36:25 +02:00
ActionToDoWithQuestions = Tst_SHOW_GAME_RESULT;
2019-07-04 16:06:31 +02:00
else
ActionToDoWithQuestions = Tst_SHOW_GAME_TO_ANSWER;
2017-07-09 20:31:40 +02:00
/***** Get data of questions from database *****/
2018-10-31 10:19:01 +01:00
NumQsts = (unsigned) DB_QuerySELECT (&mysql_res,"can not get data of a question",
2019-05-30 12:57:31 +02:00
"SELECT gam_questions.QstInd," // row[0]
"gam_questions.QstCod," // row[1]
"tst_questions.AnsType," // row[2]
"tst_questions.Stem," // row[3]
"tst_questions.Feedback," // row[4]
"tst_questions.MedCod" // row[5]
2018-10-31 10:19:01 +01:00
" FROM gam_questions,tst_questions"
" WHERE gam_questions.GamCod=%ld"
" AND gam_questions.QstCod=tst_questions.QstCod"
" ORDER BY gam_questions.QstInd",
Game->GamCod);
2017-07-09 20:31:40 +02:00
/***** Start box *****/
2019-05-20 08:52:07 +02:00
Gam_CurrentGamCod = Game->GamCod;
2017-09-07 18:38:18 +02:00
Box_StartBox (NULL,Txt_Questions,Game->Status.ICanEdit ? Gam_PutIconToAddNewQuestions :
2017-07-09 20:31:40 +02:00
NULL,
Hlp_ASSESSMENT_Games_questions,Box_NOT_CLOSABLE);
if (NumQsts)
{
2017-09-06 15:03:43 +02:00
/***** Show the table with the questions *****/
2019-05-28 15:06:53 +02:00
Gam_ListOneOrMoreQuestionsForEdition (Game->GamCod,NumQsts,mysql_res);
2017-07-09 20:31:40 +02:00
2017-09-01 14:36:25 +02:00
if (ActionToDoWithQuestions == Tst_SHOW_GAME_TO_ANSWER)
2017-07-09 20:31:40 +02:00
{
/***** Button to create/modify game *****/
Btn_PutConfirmButton (Txt_Done);
/***** End form *****/
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2017-07-09 20:31:40 +02:00
}
}
else // This game has no questions
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_INFO,Txt_This_game_has_no_questions);
2017-07-09 20:31:40 +02:00
if (Game->Status.ICanEdit && // I can edit
(!NumQsts || // This game has no questions
Editing)) // I am editing
/***** Put button to add a new question in this game *****/
2017-09-07 18:38:18 +02:00
Gam_PutButtonToAddNewQuestions ();
2017-07-09 20:31:40 +02:00
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
/***** End box *****/
Box_EndBox ();
}
2017-09-06 15:03:43 +02:00
/*****************************************************************************/
/********************* List game questions for edition ***********************/
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
static void Gam_ListOneOrMoreQuestionsForEdition (long GamCod,unsigned NumQsts,
2017-09-06 15:03:43 +02:00
MYSQL_RES *mysql_res)
{
extern const char *Txt_Questions;
extern const char *Txt_No_INDEX;
extern const char *Txt_Code;
extern const char *Txt_Tags;
extern const char *Txt_Question;
2017-09-08 01:18:20 +02:00
extern const char *Txt_Move_up_X;
extern const char *Txt_Move_down_X;
2017-09-10 18:58:26 +02:00
extern const char *Txt_Movement_not_allowed;
2017-09-06 15:03:43 +02:00
extern const char *Txt_TST_STR_ANSWER_TYPES[Tst_NUM_ANS_TYPES];
unsigned NumQst;
MYSQL_ROW row;
2019-05-30 12:57:31 +02:00
unsigned QstInd;
unsigned MaxQstInd;
2017-09-07 12:00:01 +02:00
long QstCod;
2019-05-30 12:57:31 +02:00
char StrQstInd[10 + 1];
/***** Get maximum question index *****/
MaxQstInd = Gam_GetMaxQuestionIndexInGame (GamCod);
2017-09-06 15:03:43 +02:00
/***** Write the heading *****/
Tbl_StartTableWideMargin (2);
fprintf (Gbl.F.Out,"<tr>"
"<th></th>"
"<th class=\"CENTER_TOP\">"
"%s"
"</th>"
"<th class=\"CENTER_TOP\">"
"%s"
"</th>"
"<th class=\"CENTER_TOP\">"
"%s"
"</th>"
"<th class=\"CENTER_TOP\">"
"%s"
"</th>"
"</tr>",
Txt_No_INDEX,
Txt_Code,
Txt_Tags,
Txt_Question);
/***** Write rows *****/
2019-05-30 12:57:31 +02:00
for (NumQst = 0;
2017-09-06 15:03:43 +02:00
NumQst < NumQsts;
2019-05-30 12:57:31 +02:00
NumQst++)
2017-09-06 15:03:43 +02:00
{
Gbl.RowEvenOdd = NumQst % 2;
row = mysql_fetch_row (mysql_res);
/*
2019-05-30 12:57:31 +02:00
row[0] QstInd
row[1] QstCod
row[2] AnsType
row[3] Stem
row[4] Feedback
row[5] MedCod
2017-09-06 15:03:43 +02:00
*/
/***** Create test question *****/
Tst_QstConstructor ();
2019-05-30 12:57:31 +02:00
/* Get question index (row[0]) */
QstInd = Gam_GetQstIndFromStr (row[0]);
snprintf (StrQstInd,sizeof (StrQstInd),
"%u",
QstInd);
/* Get question code (row[1]) */
QstCod = Str_ConvertStrCodToLongCod (row[1]);
2017-09-06 15:03:43 +02:00
/***** Icons *****/
2019-05-28 15:06:53 +02:00
Gam_CurrentGamCod = GamCod;
2019-05-30 12:57:31 +02:00
Gam_CurrentQstInd = QstInd;
2017-09-06 15:03:43 +02:00
fprintf (Gbl.F.Out,"<tr>"
"<td class=\"BT%u\">",Gbl.RowEvenOdd);
2017-09-13 12:25:45 +02:00
/* Put icon to remove the question */
2018-11-09 20:47:39 +01:00
Frm_StartForm (ActReqRemGamQst);
2019-05-28 15:06:53 +02:00
Gam_PutParamGameCod (GamCod);
2019-05-30 12:57:31 +02:00
Gam_PutParamQstInd (QstInd);
2017-09-06 15:03:43 +02:00
Ico_PutIconRemove ();
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2017-09-06 15:03:43 +02:00
2017-09-11 19:06:46 +02:00
/* Put icon to move up the question */
2019-05-30 12:57:31 +02:00
if (QstInd > 1)
2017-09-10 18:58:26 +02:00
{
2018-10-18 02:02:32 +02:00
snprintf (Gbl.Title,sizeof (Gbl.Title),
Txt_Move_up_X,
2019-05-30 12:57:31 +02:00
StrQstInd);
2019-01-12 03:00:59 +01:00
Lay_PutContextualLinkOnlyIcon (ActUp_GamQst,NULL,Gam_PutParamsOneQst,
"arrow-up.svg",
Gbl.Title);
2017-09-10 18:58:26 +02:00
}
else
2019-01-11 02:55:01 +01:00
Ico_PutIconOff ("arrow-up.svg",Txt_Movement_not_allowed);
2017-09-08 01:18:20 +02:00
2017-09-11 19:06:46 +02:00
/* Put icon to move down the question */
2019-05-30 12:57:31 +02:00
if (QstInd < MaxQstInd)
2017-09-10 18:58:26 +02:00
{
2018-10-18 02:02:32 +02:00
snprintf (Gbl.Title,sizeof (Gbl.Title),
Txt_Move_down_X,
2019-05-30 12:57:31 +02:00
StrQstInd);
2019-01-12 03:00:59 +01:00
Lay_PutContextualLinkOnlyIcon (ActDwnGamQst,NULL,Gam_PutParamsOneQst,
"arrow-down.svg",
Gbl.Title);
2017-09-10 18:58:26 +02:00
}
else
2019-01-11 02:55:01 +01:00
Ico_PutIconOff ("arrow-down.svg",Txt_Movement_not_allowed);
2017-09-08 01:18:20 +02:00
2017-09-11 19:06:46 +02:00
/* Put icon to edit the question */
2017-09-08 01:18:20 +02:00
Gbl.Test.QstCod = QstCod;
Ico_PutContextualIconToEdit (ActEdiOneTstQst,Tst_PutParamQstCod);
2017-09-06 15:03:43 +02:00
fprintf (Gbl.F.Out,"</td>");
/* Write number of question */
fprintf (Gbl.F.Out,"<td class=\"RIGHT_TOP COLOR%u\">"
2017-10-10 18:25:59 +02:00
"<div class=\"BIG_INDEX\">%s</div>",
2017-09-06 15:03:43 +02:00
Gbl.RowEvenOdd,
2019-05-30 12:57:31 +02:00
StrQstInd);
2017-09-06 15:03:43 +02:00
2019-05-30 12:57:31 +02:00
/* Write answer type (row[2]) */
Gbl.Test.AnswerType = Tst_ConvertFromStrAnsTypDBToAnsTyp (row[2]);
2017-09-06 15:03:43 +02:00
fprintf (Gbl.F.Out,"<div class=\"DAT_SMALL\">%s</div>"
"</td>",
Txt_TST_STR_ANSWER_TYPES[Gbl.Test.AnswerType]);
/* Write question code */
fprintf (Gbl.F.Out,"<td class=\"DAT_SMALL CENTER_TOP COLOR%u\">"
"%ld&nbsp;"
"</td>",
Gbl.RowEvenOdd,Gbl.Test.QstCod);
/* Write the question tags */
fprintf (Gbl.F.Out,"<td class=\"LEFT_TOP COLOR%u\">",
Gbl.RowEvenOdd);
Tst_GetAndWriteTagsQst (Gbl.Test.QstCod);
fprintf (Gbl.F.Out,"</td>");
2019-05-30 12:57:31 +02:00
/* Write stem (row[3]) */
2017-09-06 15:03:43 +02:00
fprintf (Gbl.F.Out,"<td class=\"LEFT_TOP COLOR%u\">",
Gbl.RowEvenOdd);
2019-05-30 12:57:31 +02:00
Tst_WriteQstStem (row[3],"TEST_EDI");
2019-03-18 15:42:22 +01:00
2019-05-30 12:57:31 +02:00
/* Get media (row[5]) */
Gbl.Test.Media.MedCod = Str_ConvertStrCodToLongCod (row[5]);
2019-03-18 15:42:22 +01:00
Med_GetMediaDataByCod (&Gbl.Test.Media);
/* Show media */
2019-03-02 21:49:11 +01:00
Med_ShowMedia (&Gbl.Test.Media,
2019-03-27 14:36:57 +01:00
"TEST_MED_EDIT_LIST_STEM_CONTAINER",
"TEST_MED_EDIT_LIST_STEM");
2019-03-18 15:42:22 +01:00
2019-05-30 12:57:31 +02:00
/* Show feedback (row[4]) */
Tst_WriteQstFeedback (row[4],"TEST_EDI_LIGHT");
2019-03-18 15:42:22 +01:00
/* Show answers */
2019-05-30 12:57:31 +02:00
Tst_WriteAnswersGameResult (GamCod,QstInd,QstCod,
2017-09-15 11:39:02 +02:00
"TEST_EDI",true); // Show result
2017-09-06 23:17:52 +02:00
2017-09-06 15:03:43 +02:00
fprintf (Gbl.F.Out,"</td>"
"</tr>");
/***** Destroy test question *****/
Tst_QstDestructor ();
}
/***** End table *****/
Tbl_EndTable ();
/***** Button to add a new question *****/
2017-09-07 18:38:18 +02:00
Gam_PutButtonToAddNewQuestions ();
2017-09-06 15:03:43 +02:00
/***** End box *****/
Box_EndBox ();
}
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-07-16 19:39:20 +02:00
/***************** Put icon to add a new questions to game *******************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_PutIconToAddNewQuestions (void)
2017-07-09 20:31:40 +02:00
{
2017-07-16 19:39:20 +02:00
extern const char *Txt_Add_questions;
2017-07-09 20:31:40 +02:00
/***** Put form to create a new question *****/
2019-01-10 15:26:33 +01:00
Ico_PutContextualIconToAdd (ActAddOneGamQst,NULL,Gam_PutParams,
Txt_Add_questions);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2017-07-16 19:39:20 +02:00
/***************** Put button to add new questions to game *******************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_PutButtonToAddNewQuestions (void)
2017-07-09 20:31:40 +02:00
{
2017-07-16 19:39:20 +02:00
extern const char *Txt_Add_questions;
2017-07-09 20:31:40 +02:00
2018-11-09 20:47:39 +01:00
Frm_StartForm (ActAddOneGamQst);
2017-09-07 18:38:18 +02:00
Gam_PutParams ();
2017-07-16 19:39:20 +02:00
Btn_PutConfirmButton (Txt_Add_questions);
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2017-07-09 20:31:40 +02:00
}
2017-07-16 20:50:01 +02:00
/*****************************************************************************/
/******************** Add selected test questions to game ********************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_AddTstQuestionsToGame (void)
2017-07-16 20:50:01 +02:00
{
2017-07-18 20:34:32 +02:00
extern const char *Txt_You_must_select_one_ore_more_questions;
2017-09-01 00:52:19 +02:00
struct Game Game;
const char *Ptr;
char LongStr[1 + 10 + 1];
2017-09-07 12:00:01 +02:00
long QstCod;
2019-05-30 12:57:31 +02:00
unsigned MaxQstInd;
2017-09-01 00:52:19 +02:00
/***** Get game code *****/
2017-09-07 18:38:18 +02:00
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
2017-09-01 00:52:19 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
2017-07-18 20:34:32 +02:00
/***** Get selected questions *****/
/* Allocate space for selected question codes */
2017-09-07 18:38:18 +02:00
Gam_AllocateListSelectedQuestions ();
2017-07-18 20:34:32 +02:00
/* Get question codes */
Par_GetParMultiToText ("QstCods",Gbl.Games.ListQuestions,
2017-09-07 18:38:18 +02:00
Gam_MAX_BYTES_LIST_SELECTED_QUESTIONS);
2017-07-18 20:34:32 +02:00
/* Check number of questions */
2017-09-07 18:38:18 +02:00
if (Gam_CountNumQuestionsInList () == 0) // If no questions selected...
2017-07-18 20:34:32 +02:00
{ // ...write warning alert
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_WARNING,Txt_You_must_select_one_ore_more_questions);
2017-07-18 20:34:32 +02:00
// TODO: Show form again!!!
}
2017-09-01 00:52:19 +02:00
/***** Insert questions in database *****/
Ptr = Gbl.Games.ListQuestions;
while (*Ptr)
{
/* Get next code */
Par_GetNextStrUntilSeparParamMult (&Ptr,LongStr,1 + 10);
2017-09-07 12:00:01 +02:00
if (sscanf (LongStr,"%ld",&QstCod) != 1)
2017-09-01 00:52:19 +02:00
Lay_ShowErrorAndExit ("Wrong question code.");
2017-09-11 19:06:46 +02:00
/* Get current maximum index */
MaxQstInd = Gam_GetMaxQuestionIndexInGame (Game.GamCod); // -1 if no questions
2017-09-01 00:52:19 +02:00
/* Insert question in the table of questions */
2018-11-02 19:37:11 +01:00
DB_QueryINSERT ("can not create question",
"INSERT INTO gam_questions"
" (GamCod,QstCod,QstInd)"
" VALUES"
" (%ld,%ld,%u)",
2019-05-30 12:57:31 +02:00
Game.GamCod,QstCod,MaxQstInd + 1);
2017-09-01 00:52:19 +02:00
}
/***** Free space for selected question codes *****/
2017-09-07 18:38:18 +02:00
Gam_FreeListsSelectedQuestions ();
2017-09-07 12:00:01 +02:00
2017-09-07 18:38:18 +02:00
/***** Show current game *****/
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Game.GamCod,
true, // Show only this game
true, // List game questions
2019-05-28 15:06:53 +02:00
false, // Do not put form to start new match
2019-05-20 10:35:57 +02:00
false); // Do not put button to play
2017-07-18 20:34:32 +02:00
}
/*****************************************************************************/
/****************** Allocate memory for list of questions ********************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_AllocateListSelectedQuestions (void)
2017-07-18 20:34:32 +02:00
{
if (!Gbl.Games.ListQuestions)
{
2017-09-07 18:38:18 +02:00
if ((Gbl.Games.ListQuestions = (char *) malloc (Gam_MAX_BYTES_LIST_SELECTED_QUESTIONS + 1)) == NULL)
2018-10-18 20:06:54 +02:00
Lay_NotEnoughMemoryExit ();;
2017-07-18 20:34:32 +02:00
Gbl.Games.ListQuestions[0] = '\0';
}
}
/*****************************************************************************/
/*********** Free memory used by list of selected question codes *************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_FreeListsSelectedQuestions (void)
2017-07-18 20:34:32 +02:00
{
if (Gbl.Games.ListQuestions)
{
free ((void *) Gbl.Games.ListQuestions);
Gbl.Games.ListQuestions = NULL;
}
}
/*****************************************************************************/
/**** Count the number of questions in the list of selected question codes ***/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static unsigned Gam_CountNumQuestionsInList (void)
2017-07-18 20:34:32 +02:00
{
const char *Ptr;
unsigned NumQuestions = 0;
2017-09-01 00:52:19 +02:00
char LongStr[1 + 10 + 1];
2017-07-18 20:34:32 +02:00
long QstCod;
/***** Go over the list Gbl.Test.ListAnsTypes counting the number of types of answer *****/
Ptr = Gbl.Games.ListQuestions;
while (*Ptr)
{
Par_GetNextStrUntilSeparParamMult (&Ptr,LongStr,1 + 10);
if (sscanf (LongStr,"%ld",&QstCod) != 1)
Lay_ShowErrorAndExit ("Wrong question code.");
NumQuestions++;
}
return NumQuestions;
2017-07-16 20:50:01 +02:00
}
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-06 23:17:52 +02:00
/*** Get number of users who selected this answer and draw proportional bar **/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-05-30 12:57:31 +02:00
void Gam_GetAndDrawBarNumUsrsWhoAnswered (long GamCod,unsigned QstInd,unsigned AnsInd,unsigned NumUsrs)
2017-09-06 23:17:52 +02:00
{
unsigned NumUsrsThisAnswer;
2017-07-09 20:31:40 +02:00
2017-09-06 23:17:52 +02:00
/***** Get number of users who selected this answer *****/
2019-05-30 12:57:31 +02:00
NumUsrsThisAnswer = Gam_GetNumUsrsWhoAnswered (GamCod,QstInd,AnsInd);
2017-07-09 20:31:40 +02:00
2017-09-06 23:17:52 +02:00
/***** Show stats of this answer *****/
2019-05-28 15:06:53 +02:00
Gam_DrawBarNumUsrs (NumUsrsThisAnswer,NumUsrs);
2017-07-09 20:31:40 +02:00
}
2017-09-06 23:17:52 +02:00
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-06 23:17:52 +02:00
/**** Get number of users who selected a given answer of a game question *****/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-05-30 12:57:31 +02:00
static unsigned Gam_GetNumUsrsWhoAnswered (long GamCod,unsigned QstInd,unsigned AnsInd)
2017-07-09 20:31:40 +02:00
{
2017-09-01 14:36:25 +02:00
MYSQL_RES *mysql_res;
MYSQL_ROW row;
2017-09-06 23:17:52 +02:00
unsigned NumUsrs = 0; // Default returned value
2017-09-01 14:36:25 +02:00
2017-09-06 23:17:52 +02:00
/***** Get answers of a question from database *****/
2018-10-31 10:19:01 +01:00
if (DB_QuerySELECT (&mysql_res,"can not get number of users who answered",
"SELECT NumUsrs FROM gam_answers"
2019-05-30 12:57:31 +02:00
" WHERE GamCod=%ld AND QstInd=%u AND AnsInd=%u",
GamCod,QstInd,AnsInd))
2017-09-01 14:36:25 +02:00
{
row = mysql_fetch_row (mysql_res);
2017-09-06 23:17:52 +02:00
if (row[0]) // There are users who selected this answer
if (sscanf (row[0],"%u",&NumUsrs) != 1)
Lay_ShowErrorAndExit ("Error when getting number of users who answered.");
2017-07-09 20:31:40 +02:00
}
2017-09-06 23:17:52 +02:00
return NumUsrs;
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/***************** Draw a bar with the percentage of answers *****************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
#define Gam_MAX_BAR_WIDTH 125
2017-07-09 20:31:40 +02:00
2017-09-07 18:38:18 +02:00
static void Gam_DrawBarNumUsrs (unsigned NumUsrs,unsigned MaxUsrs)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_of_PART_OF_A_TOTAL;
unsigned BarWidth = 0;
/***** String with the number of users *****/
if (MaxUsrs)
2018-10-18 02:02:32 +02:00
snprintf (Gbl.Title,sizeof (Gbl.Title),
"%u&nbsp;(%u%%&nbsp;%s&nbsp;%u)",
NumUsrs,
(unsigned) ((((float) NumUsrs * 100.0) / (float) MaxUsrs) + 0.5),
Txt_of_PART_OF_A_TOTAL,MaxUsrs);
2017-07-09 20:31:40 +02:00
else
2018-10-18 02:02:32 +02:00
snprintf (Gbl.Title,sizeof (Gbl.Title),
"0&nbsp;(0%%&nbsp;%s&nbsp;%u)",
Txt_of_PART_OF_A_TOTAL,MaxUsrs);
2017-07-09 20:31:40 +02:00
/***** Draw bar with a with proportional to the number of clicks *****/
if (NumUsrs && MaxUsrs)
2017-09-07 18:38:18 +02:00
BarWidth = (unsigned) ((((float) NumUsrs * (float) Gam_MAX_BAR_WIDTH) /
2017-07-09 20:31:40 +02:00
(float) MaxUsrs) + 0.5);
if (BarWidth < 2)
BarWidth = 2;
fprintf (Gbl.F.Out,"<img src=\"%s/c1x16.gif\""
" alt=\"%s\" title=\"%s\""
" class=\"LEFT_TOP\""
" style=\"width:%upx; height:20px;\" />"
"&nbsp;",
2019-03-20 01:36:36 +01:00
Cfg_URL_ICON_PUBLIC,
2017-07-09 20:31:40 +02:00
Gbl.Title,
Gbl.Title,
BarWidth);
/***** Write the number of users *****/
2019-05-20 19:47:17 +02:00
fprintf (Gbl.F.Out,"%s",Gbl.Title);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2017-09-08 01:18:20 +02:00
/**************** Put parameter to move/remove one question ******************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-08 01:18:20 +02:00
static void Gam_PutParamsOneQst (void)
2017-07-09 20:31:40 +02:00
{
2019-05-20 08:52:07 +02:00
Gam_PutParamGameCod (Gam_CurrentGamCod);
2019-05-30 12:57:31 +02:00
Gam_PutParamQstInd (Gam_CurrentQstInd);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/********************** Request the removal of a question ********************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_RequestRemoveQst (void)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_Do_you_really_want_to_remove_the_question_X;
extern const char *Txt_Remove_question;
2017-09-01 00:52:19 +02:00
struct Game Game;
2017-09-07 12:00:01 +02:00
unsigned QstInd;
2017-07-09 20:31:40 +02:00
2019-05-28 15:06:53 +02:00
/***** Get parameters *****/
2017-07-09 20:31:40 +02:00
/* Get game code */
2017-09-07 18:38:18 +02:00
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
/* Get question index */
2019-05-30 12:57:31 +02:00
QstInd = Gam_GetParamQstInd ();
2017-07-09 20:31:40 +02:00
/***** Show question and button to remove question *****/
2019-05-20 08:52:07 +02:00
Gam_CurrentGamCod = Game.GamCod;
2019-05-30 12:57:31 +02:00
Gam_CurrentQstInd = QstInd;
2019-02-17 01:14:55 +01:00
Ale_ShowAlertAndButton (ActRemGamQst,NULL,NULL,Gam_PutParamsOneQst,
Btn_REMOVE_BUTTON,Txt_Remove_question,
Ale_QUESTION,Txt_Do_you_really_want_to_remove_the_question_X,
2019-05-30 12:57:31 +02:00
QstInd);
2017-07-09 20:31:40 +02:00
/***** Show current game *****/
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Game.GamCod,
true, // Show only this game
true, // List game questions
2019-05-28 15:06:53 +02:00
false, // Do not put form to start new match
2019-05-20 10:35:57 +02:00
false); // Do not put button to play
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/****************************** Remove a question ****************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_RemoveQst (void)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_Question_removed;
2017-09-01 00:52:19 +02:00
struct Game Game;
2017-09-07 12:00:01 +02:00
unsigned QstInd;
2017-07-09 20:31:40 +02:00
2019-05-28 15:06:53 +02:00
/***** Get parameters *****/
2017-07-09 20:31:40 +02:00
/* Get game code */
2017-09-07 18:38:18 +02:00
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
/* Get question index */
2019-05-30 12:57:31 +02:00
QstInd = Gam_GetParamQstInd ();
2017-07-09 20:31:40 +02:00
/***** Remove the question from all the tables *****/
/* Remove answers from this test question */
2019-05-30 12:57:31 +02:00
Gam_RemAnswersOfAQuestion (Game.GamCod,QstInd);
2017-07-09 20:31:40 +02:00
/* Remove the question itself */
2018-11-02 22:00:31 +01:00
DB_QueryDELETE ("can not remove a question",
2019-05-30 12:57:31 +02:00
"DELETE FROM gam_questions"
" WHERE GamCod=%ld AND QstInd=%u",
Game.GamCod,QstInd);
2017-07-09 20:31:40 +02:00
if (!mysql_affected_rows (&Gbl.mysql))
Lay_ShowErrorAndExit ("The question to be removed does not exist.");
/* Change index of questions greater than this */
2019-05-30 12:57:31 +02:00
DB_QueryUPDATE ("can not update indexes of questions in table of answers",
"UPDATE gam_answers SET QstInd=QstInd-1"
" WHERE GamCod=%ld AND QstInd>%u",
Game.GamCod,QstInd);
2018-11-03 12:16:40 +01:00
DB_QueryUPDATE ("can not update indexes of questions",
"UPDATE gam_questions SET QstInd=QstInd-1"
" WHERE GamCod=%ld AND QstInd>%u",
Game.GamCod,QstInd);
2017-07-09 20:31:40 +02:00
/***** Write message *****/
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_SUCCESS,Txt_Question_removed);
2017-07-09 20:31:40 +02:00
/***** Show current game *****/
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Game.GamCod,
true, // Show only this game
true, // List game questions
2019-05-28 15:06:53 +02:00
false, // Do not put form to start new match
2019-05-20 10:35:57 +02:00
false); // Do not put button to play
2017-07-09 20:31:40 +02:00
}
2017-09-11 19:06:46 +02:00
/*****************************************************************************/
/***************** Move up position of a question in a game ******************/
/*****************************************************************************/
void Gam_MoveUpQst (void)
{
extern const char *Txt_The_question_has_been_moved_up;
2019-05-30 12:57:31 +02:00
extern const char *Txt_Movement_not_allowed;
2017-09-11 19:06:46 +02:00
struct Game Game;
2019-05-30 12:57:31 +02:00
unsigned QstIndTop;
unsigned QstIndBottom;
2017-09-11 19:06:46 +02:00
2019-05-28 15:06:53 +02:00
/***** Get parameters *****/
2017-09-11 19:06:46 +02:00
/* Get game code */
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
Lay_ShowErrorAndExit ("Code of game is missing.");
/* Get question index */
2019-05-30 12:57:31 +02:00
QstIndBottom = Gam_GetParamQstInd ();
2017-09-11 19:06:46 +02:00
/***** Move up question *****/
2019-05-30 12:57:31 +02:00
if (QstIndBottom > 1)
2017-09-11 19:06:46 +02:00
{
/* Indexes of questions to be exchanged */
QstIndTop = Gam_GetPrevQuestionIndexInGame (Game.GamCod,QstIndBottom);
2019-05-30 12:57:31 +02:00
if (!QstIndTop)
2017-09-14 02:32:36 +02:00
Lay_ShowErrorAndExit ("Wrong index of question.");
2017-09-11 19:06:46 +02:00
/* Exchange questions */
2019-05-30 12:57:31 +02:00
Gam_ExchangeQuestions (Game.GamCod,QstIndTop,QstIndBottom);
2017-09-11 19:06:46 +02:00
/* Success alert */
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_SUCCESS,Txt_The_question_has_been_moved_up);
2017-09-11 19:06:46 +02:00
}
2019-05-30 12:57:31 +02:00
else
Ale_ShowAlert (Ale_WARNING,Txt_Movement_not_allowed);
2017-09-11 19:06:46 +02:00
/***** Show current game *****/
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Game.GamCod,
true, // Show only this game
true, // List game questions
2019-05-28 15:06:53 +02:00
false, // Do not put form to start new match
2019-05-20 10:35:57 +02:00
false); // Do not put button to play
2017-09-11 19:06:46 +02:00
}
/*****************************************************************************/
/**************** Move down position of a question in a game *****************/
/*****************************************************************************/
void Gam_MoveDownQst (void)
{
extern const char *Txt_The_question_has_been_moved_down;
2019-05-30 12:57:31 +02:00
extern const char *Txt_Movement_not_allowed;
extern const char *Txt_This_game_has_no_questions;
2017-09-11 19:06:46 +02:00
struct Game Game;
2019-05-30 12:57:31 +02:00
unsigned QstIndTop;
unsigned QstIndBottom;
unsigned MaxQstInd; // 0 if no questions
2017-09-11 19:06:46 +02:00
2019-05-28 15:06:53 +02:00
/***** Get parameters *****/
2017-09-11 19:06:46 +02:00
/* Get game code */
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
Lay_ShowErrorAndExit ("Code of game is missing.");
/* Get question index */
2019-05-30 12:57:31 +02:00
QstIndTop = Gam_GetParamQstInd ();
2017-09-11 19:06:46 +02:00
/* Get maximum question index */
MaxQstInd = Gam_GetMaxQuestionIndexInGame (Game.GamCod);
/***** Move down question *****/
2019-05-30 12:57:31 +02:00
if (MaxQstInd)
{
2017-09-14 02:32:36 +02:00
if (QstIndTop < MaxQstInd)
2017-09-11 19:06:46 +02:00
{
/* Indexes of questions to be exchanged */
QstIndBottom = Gam_GetNextQuestionIndexInGame (Game.GamCod,QstIndTop);
2019-05-30 12:57:31 +02:00
if (!QstIndBottom)
2017-09-14 02:32:36 +02:00
Lay_ShowErrorAndExit ("Wrong index of question.");
2017-09-11 19:06:46 +02:00
/* Exchange questions */
2019-05-30 12:57:31 +02:00
Gam_ExchangeQuestions (Game.GamCod,QstIndTop,QstIndBottom);
2017-09-11 19:06:46 +02:00
/* Success alert */
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_SUCCESS,Txt_The_question_has_been_moved_down);
2017-09-11 19:06:46 +02:00
}
2019-05-30 12:57:31 +02:00
else
Ale_ShowAlert (Ale_WARNING,Txt_Movement_not_allowed);
}
else
Ale_ShowAlert (Ale_WARNING,Txt_This_game_has_no_questions);
2017-09-11 19:06:46 +02:00
/***** Show current game *****/
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Game.GamCod,
true, // Show only this game
true, // List game questions
2019-05-28 15:06:53 +02:00
false, // Do not put form to start new match
2019-05-20 10:35:57 +02:00
false); // Do not put button to play
2017-09-11 19:06:46 +02:00
}
/*****************************************************************************/
/********* Exchange the order of two consecutive questions in a game *********/
/*****************************************************************************/
static void Gam_ExchangeQuestions (long GamCod,
unsigned QstIndTop,unsigned QstIndBottom)
{
long QstCodTop;
long QstCodBottom;
/***** Lock table to make the inscription atomic *****/
2018-11-02 22:41:02 +01:00
DB_Query ("can not lock tables to move game question",
"LOCK TABLES gam_questions WRITE");
2017-09-11 19:06:46 +02:00
Gbl.DB.LockedTables = true;
/***** Get question code of the questions to be moved *****/
QstCodTop = Gam_GetQstCodFromQstInd (GamCod,QstIndTop);
QstCodBottom = Gam_GetQstCodFromQstInd (GamCod,QstIndBottom);
/***** Exchange indexes of questions *****/
/*
Example:
2019-05-30 12:57:31 +02:00
QstIndTop = 1; QstCodTop = 218
QstIndBottom = 2; QstCodBottom = 220
2017-09-11 19:06:46 +02:00
+--------+--------+ +--------+--------+ +--------+--------+
| QstInd | QstCod | | QstInd | QstCod | | QstInd | QstCod |
+--------+--------+ +--------+--------+ +--------+--------+
2019-05-30 12:57:31 +02:00
| 1 | 218 | -----> | 2 | 218 | = | 1 | 220 |
| 2 | 220 | | 1 | 220 | | 2 | 218 |
| 3 | 232 | | 3 | 232 | | 3 | 232 |
2017-09-11 19:06:46 +02:00
+--------+--------+ +--------+--------+ +--------+--------+
*/
2018-11-03 12:16:40 +01:00
DB_QueryUPDATE ("can not exchange indexes of questions",
"UPDATE gam_questions SET QstInd=%u"
" WHERE GamCod=%ld AND QstCod=%ld",
QstIndBottom,
GamCod,QstCodTop);
DB_QueryUPDATE ("can not exchange indexes of questions",
"UPDATE gam_questions SET QstInd=%u"
" WHERE GamCod=%ld AND QstCod=%ld",
QstIndTop,
GamCod,QstCodBottom);
2017-09-11 19:06:46 +02:00
/***** Unlock table *****/
Gbl.DB.LockedTables = false; // Set to false before the following unlock...
// ...to not retry the unlock if error in unlocking
2018-11-02 22:41:02 +01:00
DB_Query ("can not unlock tables after moving game questions",
"UNLOCK TABLES");
2017-09-11 19:06:46 +02:00
}
2019-05-28 15:06:53 +02:00
/*****************************************************************************/
/************************* List the matches of a game ************************/
/*****************************************************************************/
static void Gam_ListPlayedMatches (struct Game *Game,bool PutFormNewMatch)
{
extern const char *Hlp_ASSESSMENT_Games_matches;
extern const char *Txt_Matches;
char *SubQuery;
MYSQL_RES *mysql_res;
unsigned NumMatches;
/***** Get data of matches from database *****/
/* Fill subquery for game */
if (Gbl.Crs.Grps.WhichGrps == Grp_ONLY_MY_GROUPS)
{
if (asprintf (&SubQuery,"GamCod=%ld"
" AND"
"(MchCod NOT IN"
" (SELECT MchCod FROM gam_grp)"
" OR"
" MchCod IN"
" (SELECT gam_grp.MchCod"
" FROM gam_grp,crs_grp_usr"
" WHERE crs_grp_usr.UsrCod=%ld"
" AND gam_grp.GrpCod=crs_grp_usr.GrpCod))",
Game->GamCod,
Gbl.Usrs.Me.UsrDat.UsrCod) < 0)
Lay_NotEnoughMemoryExit ();
}
else // Gbl.Crs.Grps.WhichGrps == Grp_ALL_GROUPS
{
if (asprintf (&SubQuery,"GamCod=%ld",
Game->GamCod) < 0)
Lay_NotEnoughMemoryExit ();
}
/* Make query */
NumMatches = (unsigned) DB_QuerySELECT (&mysql_res,"can not get matches",
2019-05-29 01:14:56 +02:00
"SELECT MchCod," // row[ 0]
"GamCod," // row[ 1]
"UsrCod," // row[ 2]
"UNIX_TIMESTAMP(StartTime)," // row[ 3]
"UNIX_TIMESTAMP(EndTime)," // row[ 4]
"Title," // row[ 5]
"QstInd," // row[ 6]
"QstCod," // row[ 7]
"UNIX_TIMESTAMP(QstStartTime)," // row[ 8]
"ShowingAnswers," // row[ 9]
"Finished" // row[10]
2019-05-28 15:06:53 +02:00
" FROM gam_matches"
" WHERE %s"
" ORDER BY MchCod",
SubQuery);
/* Free allocated memory for subquery */
free ((void *) SubQuery);
/***** Start box *****/
Gam_CurrentGamCod = Game->GamCod;
Box_StartBox (NULL,Txt_Matches,Gam_PutIconToPlayNewMatch,
Hlp_ASSESSMENT_Games_matches,Box_NOT_CLOSABLE);
if (NumMatches)
/***** Show the table with the matches *****/
2019-05-31 10:04:13 +02:00
Gam_ListOneOrMoreMatchesForEdition (Game,NumMatches,mysql_res);
2019-05-28 15:06:53 +02:00
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
/***** Put button to play a new match in this game *****/
if (PutFormNewMatch)
Gam_PutBigButtonToPlayMatchTch (Game);
else
Gam_PutButtonNewMatch (Game->GamCod);
/***** End box *****/
Box_EndBox ();
}
/*****************************************************************************/
/********************** Get match data using its code ************************/
/*****************************************************************************/
void Gam_GetDataOfMatchByCod (struct Match *Match)
{
MYSQL_RES *mysql_res;
unsigned long NumRows;
/***** Get data of match from database *****/
NumRows = (unsigned) DB_QuerySELECT (&mysql_res,"can not get matches",
2019-05-29 01:14:56 +02:00
"SELECT MchCod," // row[ 0]
"GamCod," // row[ 1]
"UsrCod," // row[ 2]
"UNIX_TIMESTAMP(StartTime)," // row[ 3]
"UNIX_TIMESTAMP(EndTime)," // row[ 4]
"Title," // row[ 5]
"QstInd," // row[ 6]
"QstCod," // row[ 7]
"UNIX_TIMESTAMP(QstStartTime)," // row[ 8]
"ShowingAnswers," // row[ 9]
"Finished" // row[10]
2019-05-28 15:06:53 +02:00
" FROM gam_matches"
" WHERE MchCod=%ld"
" AND GamCod IN" // Extra check
" (SELECT GamCod FROM games"
" WHERE CrsCod='%ld')",
Match->MchCod,
Gbl.Hierarchy.Crs.CrsCod);
if (NumRows) // Match found...
/***** Get match data from row *****/
Gam_GetMatchDataFromRow (mysql_res,Match);
else
{
/* Initialize to empty match */
Match->MchCod = -1L;
Match->GamCod = -1L;
Match->UsrCod = -1L;
Match->TimeUTC[Gam_START_TIME] =
Match->TimeUTC[Gam_END_TIME ] = (time_t) 0;
Match->Title[0] = '\0';
2019-05-30 12:57:31 +02:00
Match->Status.QstInd = 0;
2019-05-29 01:14:56 +02:00
Match->Status.QstCod = -1L;
Match->Status.QstStartTimeUTC = (time_t) 0;
2019-05-30 20:52:23 +02:00
Match->Status.ShowingAnswers = false;
2019-05-29 01:14:56 +02:00
Match->Status.Finished = false;
2019-05-28 15:06:53 +02:00
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
/*****************************************************************************/
/***************** Put icon to add a new questions to game *******************/
/*****************************************************************************/
static void Gam_PutIconToPlayNewMatch (void)
{
extern const char *Txt_New_match;
/***** Put form to create a new question *****/
Ico_PutContextualIconToAdd (ActFrmNewMch,Gam_NEW_MATCH_SECTION_ID,Gam_PutParams,
Txt_New_match);
}
/*****************************************************************************/
/*********************** List game matches for edition ***********************/
/*****************************************************************************/
2019-05-31 10:04:13 +02:00
static void Gam_ListOneOrMoreMatchesForEdition (struct Game *Game,
unsigned NumMatches,
2019-05-28 15:06:53 +02:00
MYSQL_RES *mysql_res)
{
extern const char *Txt_No_INDEX;
extern const char *Txt_ROLES_SINGUL_Abc[Rol_NUM_ROLES][Usr_NUM_SEXS];
extern const char *Txt_START_END_TIME[Dat_NUM_START_END_TIME];
extern const char *Txt_Match;
2019-05-30 13:46:57 +02:00
extern const char *Txt_Status;
2019-05-30 12:57:31 +02:00
extern const char *Txt_Resume;
2019-05-28 15:06:53 +02:00
extern const char *Txt_Today;
2019-05-31 10:25:20 +02:00
extern const char *Txt_View_game_results;
2019-05-28 15:06:53 +02:00
unsigned NumMatch;
unsigned UniqueId;
struct Match Match;
/***** Write the heading *****/
Tbl_StartTableWideMargin (2);
fprintf (Gbl.F.Out,"<tr>"
"<th></th>"
2019-05-29 01:14:56 +02:00
"<th class=\"RIGHT_TOP\">"
2019-05-28 15:06:53 +02:00
"%s"
"</th>"
2019-05-29 01:14:56 +02:00
"<th class=\"LEFT_TOP\">"
2019-05-28 15:06:53 +02:00
"%s"
"</th>"
2019-05-29 01:14:56 +02:00
"<th class=\"LEFT_TOP\">"
2019-05-28 15:06:53 +02:00
"%s"
"</th>"
2019-05-29 01:14:56 +02:00
"<th class=\"LEFT_TOP\">"
2019-05-28 15:06:53 +02:00
"%s"
"</th>"
2019-05-29 01:14:56 +02:00
"<th class=\"LEFT_TOP\">"
2019-05-28 15:06:53 +02:00
"%s"
"</th>"
2019-05-31 10:04:13 +02:00
"<th class=\"RIGHT_TOP\">"
2019-05-30 13:46:57 +02:00
"%s"
"</th>"
2019-05-28 15:06:53 +02:00
"</tr>",
Txt_No_INDEX,
Txt_ROLES_SINGUL_Abc[Rol_TCH][Usr_SEX_UNKNOWN],
Txt_START_END_TIME[Gam_ORDER_BY_START_DATE],
Txt_START_END_TIME[Gam_ORDER_BY_END_DATE],
2019-05-30 13:46:57 +02:00
Txt_Match,
Txt_Status);
2019-05-28 15:06:53 +02:00
/***** Write rows *****/
for (NumMatch = 0, UniqueId = 1;
NumMatch < NumMatches;
NumMatch++, UniqueId++)
{
Gbl.RowEvenOdd = NumMatch % 2;
/***** Get match data from row *****/
Gam_GetMatchDataFromRow (mysql_res,&Match);
/***** Icons *****/
fprintf (Gbl.F.Out,"<tr>"
"<td class=\"BT%u\">",Gbl.RowEvenOdd);
/* Put icon to remove the match */
Frm_StartForm (ActReqRemMch);
Gam_PutParamMatchCod (Match.MchCod);
Ico_PutIconRemove ();
Frm_EndForm ();
2019-05-30 12:57:31 +02:00
/* Put icon to continue playing an unfinished match */
2019-05-31 10:04:13 +02:00
/*
2019-05-29 01:14:56 +02:00
if (!Match.Status.Finished)
{
Gam_CurrentMchCod = Match.MchCod;
2019-05-30 12:57:31 +02:00
Lay_PutContextualLinkOnlyIcon (ActResMch,NULL,
2019-05-29 01:14:56 +02:00
Gam_PutParamCurrentMchCod,
"play.svg",
2019-05-30 12:57:31 +02:00
Txt_Resume);
2019-05-29 01:14:56 +02:00
}
2019-05-31 10:04:13 +02:00
*/
2019-05-29 01:14:56 +02:00
2019-05-28 15:06:53 +02:00
fprintf (Gbl.F.Out,"</td>");
/***** Number of match ******/
2019-05-30 13:46:57 +02:00
fprintf (Gbl.F.Out,"<td class=\"BIG_INDEX RIGHT_TOP COLOR%u\">%u</td>",
Gbl.RowEvenOdd,NumMatch + 1);
2019-05-28 15:06:53 +02:00
/***** Match player *****/
fprintf (Gbl.F.Out,"<td class=\"LEFT_TOP COLOR%u\">",
Gbl.RowEvenOdd);
Usr_WriteAuthor1Line (Match.UsrCod,false);
fprintf (Gbl.F.Out,"</td>");
/***** Start date/time *****/
fprintf (Gbl.F.Out,"<td id=\"mch_date_start_%u\""
" class=\"%s LEFT_TOP COLOR%u\">",
UniqueId,
2019-05-29 01:14:56 +02:00
Match.Status.Finished ? "DATE_RED" :
"DATE_GREEN",
2019-05-28 15:06:53 +02:00
Gbl.RowEvenOdd);
fprintf (Gbl.F.Out,"<script type=\"text/javascript\">"
"writeLocalDateHMSFromUTC('mch_date_start_%u',%ld,"
"%u,'<br />','%s',true,true,0x7);"
"</script>"
"</td>",
UniqueId,Match.TimeUTC[Gam_START_TIME],
(unsigned) Gbl.Prefs.DateFormat,Txt_Today);
/***** End date/time *****/
fprintf (Gbl.F.Out,"<td id=\"mch_date_end_%u\""
" class=\"%s LEFT_TOP COLOR%u\">",
UniqueId,
2019-05-29 01:14:56 +02:00
Match.Status.Finished ? "DATE_RED" :
"DATE_GREEN",
2019-05-28 15:06:53 +02:00
Gbl.RowEvenOdd);
fprintf (Gbl.F.Out,"\">"
"<script type=\"text/javascript\">"
"writeLocalDateHMSFromUTC('mch_date_end_%u',%ld,"
"%u,'<br />','%s',false,true,0x7);"
"</script>"
"</td>",
UniqueId,Match.TimeUTC[Gam_END_TIME],
(unsigned) Gbl.Prefs.DateFormat,Txt_Today);
/***** Title and groups *****/
2019-05-29 01:14:56 +02:00
fprintf (Gbl.F.Out,"<td class=\"LEFT_TOP COLOR%u\">",Gbl.RowEvenOdd);
2019-05-28 15:06:53 +02:00
/* Title */
2019-05-29 01:14:56 +02:00
fprintf (Gbl.F.Out,"<span class=\"ASG_TITLE\">%s</span>",Match.Title);
2019-05-28 15:06:53 +02:00
/* Groups whose students can answer this match */
if (Gbl.Crs.Grps.NumGrps)
Gam_GetAndWriteNamesOfGrpsAssociatedToMatch (&Match);
fprintf (Gbl.F.Out,"</td>");
2019-05-30 13:46:57 +02:00
/***** Match status ******/
2019-05-31 10:04:13 +02:00
fprintf (Gbl.F.Out,"<td class=\"DAT RIGHT_TOP COLOR%u\">",Gbl.RowEvenOdd);
2019-05-30 13:46:57 +02:00
if (Match.Status.Finished)
2019-05-31 10:04:13 +02:00
/* Icon to inform about finished match */
// Ico_PutIconOff ("flag-checkered.svg",Txt_Finished_match);
Lay_PutContextualLinkOnlyIcon (ActShoMch,NULL,
Gam_PutParamCurrentMchCod,
"flag-checkered.svg",
2019-05-31 10:25:20 +02:00
Txt_View_game_results);
2019-05-30 13:46:57 +02:00
else // Unfinished match
{
2019-05-31 10:04:13 +02:00
/* Current question index / total of questions */
fprintf (Gbl.F.Out,"<div class=\"DAT\">%u/%u</div>",
Match.Status.QstInd,Game->NumQsts);
/* Icon to resume */
2019-05-30 13:46:57 +02:00
Gam_CurrentMchCod = Match.MchCod;
Lay_PutContextualLinkOnlyIcon (ActResMch,NULL,
Gam_PutParamCurrentMchCod,
"play.svg",
Txt_Resume);
}
fprintf (Gbl.F.Out,"</td>");
2019-05-28 15:06:53 +02:00
fprintf (Gbl.F.Out,"</tr>");
}
/***** End table *****/
Tbl_EndTable ();
}
/*****************************************************************************/
/******************** Get game data from a database row **********************/
/*****************************************************************************/
static void Gam_GetMatchDataFromRow (MYSQL_RES *mysql_res,
struct Match *Match)
{
MYSQL_ROW row;
/***** Get match data *****/
row = mysql_fetch_row (mysql_res);
/*
2019-05-29 01:14:56 +02:00
row[ 0] MchCod
row[ 1] GamCod
row[ 2] UsrCod
row[ 3] UNIX_TIMESTAMP(StartTime)
row[ 4] UNIX_TIMESTAMP(EndTime)
row[ 5] Title
2019-05-28 15:06:53 +02:00
*/
2019-05-29 01:14:56 +02:00
/***** Get match data *****/
/* Code of the match (row[0]) */
2019-05-28 15:06:53 +02:00
if ((Match->MchCod = Str_ConvertStrCodToLongCod (row[0])) <= 0)
Lay_ShowErrorAndExit ("Wrong code of match.");
2019-05-29 01:14:56 +02:00
/* Code of the game (row[1]) */
2019-05-28 15:06:53 +02:00
if ((Match->GamCod = Str_ConvertStrCodToLongCod (row[1])) <= 0)
Lay_ShowErrorAndExit ("Wrong code of game.");
2019-05-29 01:14:56 +02:00
/* Get match teacher (row[2]) */
Match->UsrCod = Str_ConvertStrCodToLongCod (row[2]);
2019-05-28 15:06:53 +02:00
2019-05-29 01:14:56 +02:00
/* Get start date (row[3] holds the start UTC time) */
Match->TimeUTC[Gam_START_TIME] = Dat_GetUNIXTimeFromStr (row[3]);
2019-05-28 15:06:53 +02:00
2019-05-29 01:14:56 +02:00
/* Get end date (row[4] holds the end UTC time) */
Match->TimeUTC[Gam_END_TIME ] = Dat_GetUNIXTimeFromStr (row[4]);
2019-05-28 15:06:53 +02:00
2019-05-29 01:14:56 +02:00
/* Get the title of the game (row[5]) */
if (row[5])
Str_Copy (Match->Title,row[5],
2019-05-28 15:06:53 +02:00
Gam_MAX_BYTES_TITLE);
else
Match->Title[0] = '\0';
2019-05-29 01:14:56 +02:00
/***** Get current match status *****/
/*
row[ 6] QstInd
row[ 7] QstCod
row[ 8] UNIX_TIMESTAMP(QstStartTime)
row[ 9] ShowingAnswers
row[10] Finished
*/
/* Current question index (row[6]) */
2019-05-30 12:57:31 +02:00
Match->Status.QstInd = Gam_GetQstIndFromStr (row[6]);
2019-05-29 01:14:56 +02:00
/* Current question code (row[7]) */
Match->Status.QstCod = Str_ConvertStrCodToLongCod (row[7]);
/* Get question start date (row[8] holds the start UTC time) */
Match->Status.QstStartTimeUTC = Dat_GetUNIXTimeFromStr (row[8]);
/* Get whether to show question answers or not (row(9)) */
2019-05-30 20:52:23 +02:00
Match->Status.ShowingAnswers = (row[9][0] == 'Y');
2019-05-29 01:14:56 +02:00
/* Get whether the match is finished or not (row(10)) */
Match->Status.Finished = (row[10][0] == 'Y');
2019-05-31 00:40:23 +02:00
/* If question index is 0 ==> the game is finished */
if (Match->Status.QstInd == 0)
{
Match->Status.QstCod = -1L;
Match->Status.Finished = true;
}
2019-05-28 15:06:53 +02:00
}
/*****************************************************************************/
/************** Request the removal of a match (game instance) ***************/
/*****************************************************************************/
void Gam_RequestRemoveMatch (void)
{
extern const char *Txt_Do_you_really_want_to_remove_the_match_X;
extern const char *Txt_Remove_match;
struct Match Match;
/***** Get parameters *****/
/* Get match code */
if ((Match.MchCod = Gam_GetParamMatchCod ()) == -1L)
Lay_ShowErrorAndExit ("Code of match is missing.");
/***** Get data of the match from database *****/
Gam_GetDataOfMatchByCod (&Match);
/***** Show question and button to remove question *****/
Gam_CurrentMchCod = Match.MchCod;
Ale_ShowAlertAndButton (ActRemMch,NULL,NULL,Gam_PutParamCurrentMchCod,
Btn_REMOVE_BUTTON,Txt_Remove_match,
Ale_QUESTION,Txt_Do_you_really_want_to_remove_the_match_X,
Match.Title);
/***** Show current game *****/
Gam_ShowOneGame (Match.GamCod,
true, // Show only this game
true, // List game questions
false, // Do not put form to start new match
false); // Do not put button to play
}
/*****************************************************************************/
/********************** Remove a match (game instance) ***********************/
/*****************************************************************************/
void Gam_RemoveMatch (void)
{
extern const char *Txt_Match_X_removed;
struct Match Match;
/***** Get parameters *****/
/* Get match code */
if ((Match.MchCod = Gam_GetParamMatchCod ()) == -1L)
Lay_ShowErrorAndExit ("Code of match is missing.");
/***** Get data of the match from database *****/
Gam_GetDataOfMatchByCod (&Match);
/***** Remove the match from all the tables *****/
/* Remove groups associated to the match */
DB_QueryDELETE ("can not remove the groups associated to matches of a game",
"DELETE FROM gam_grp USING gam_grp,gam_matches,games"
" WHERE gam_grp.MchCod=%ld"
" AND gam_grp.MchCod=gam_matches.MchCod"
" AND gam_matches.GamCod=games.GamCod"
" AND games.CrsCod=%ld", // Extra check
Match.MchCod);
/* Remove the match itself */
DB_QueryDELETE ("can not remove a match",
"DELETE FROM gam_matches USING gam_matches,games"
" WHERE gam_matches.MchCod=%ld"
" AND gam_matches.GamCod=games.GamCod"
" AND games.CrsCod=%ld", // Extra check
Match.MchCod,Gbl.Hierarchy.Crs.CrsCod);
if (!mysql_affected_rows (&Gbl.mysql))
Lay_ShowErrorAndExit ("The match to be removed does not exist.");
/***** Write message *****/
Ale_ShowAlert (Ale_SUCCESS,Txt_Match_X_removed,
Match.Title);
/***** Show current game *****/
Gam_ShowOneGame (Match.GamCod,
true, // Show only this game
true, // List game questions
false, // Do not put form to start new match
false); // Do not put button to play
}
/*****************************************************************************/
/********************* Put button to create a new match **********************/
/*****************************************************************************/
static void Gam_PutButtonNewMatch (long GamCod)
{
extern const char *Txt_New_match;
Frm_StartFormAnchor (ActFrmNewMch,Gam_NEW_MATCH_SECTION_ID);
Gam_PutParamGameCod (GamCod);
Btn_PutConfirmButton (Txt_New_match);
Frm_EndForm ();
}
2017-09-13 09:47:45 +02:00
/*****************************************************************************/
2019-05-20 10:35:57 +02:00
/******************* Start playing a game as a teacher ***********************/
2017-09-13 09:47:45 +02:00
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
void Gam_RequestNewMatch (void)
2017-09-13 09:47:45 +02:00
{
2019-05-20 10:35:57 +02:00
long GamCod;
2017-09-13 09:47:45 +02:00
/***** Get parameters *****/
Gam_GetParamGameOrder ();
Grp_GetParamWhichGrps ();
2017-09-13 16:24:29 +02:00
Gbl.Games.CurrentPage = Pag_GetParamPagNum (Pag_GAMES);
2017-09-13 09:47:45 +02:00
/***** Get game code *****/
2019-05-20 10:35:57 +02:00
if ((GamCod = Gam_GetParamGameCod ()) == -1L)
2017-09-13 09:47:45 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
/***** Show game *****/
2019-05-20 10:35:57 +02:00
Gam_ShowOneGame (GamCod,
true, // Show only this game
false, // Do not list questions
2019-05-28 15:06:53 +02:00
true, // Put form to start new match
2019-05-20 10:35:57 +02:00
false); // Do not put button to play
}
/*****************************************************************************/
/******************* Start playing a game as a student ***********************/
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
void Gam_PlayMatchStd (void)
2019-05-20 10:35:57 +02:00
{
2019-05-28 15:06:53 +02:00
struct Match Match;
bool IBelongToGroups;
2019-05-20 10:35:57 +02:00
/***** Get parameters *****/
Gam_GetParamGameOrder ();
Grp_GetParamWhichGrps ();
Gbl.Games.CurrentPage = Pag_GetParamPagNum (Pag_GAMES);
2019-05-28 15:06:53 +02:00
/***** Get match code *****/
if ((Match.MchCod = Gam_GetParamMatchCod ()) == -1L)
Lay_ShowErrorAndExit ("Code of match is missing.");
/***** Get data of the match from database *****/
Gam_GetDataOfMatchByCod (&Match);
/***** Do I belong to valid groups to play this match? *****/
IBelongToGroups = Gbl.Usrs.Me.IBelongToCurrentCrs &&
Gam_CheckIfIPlayThisMatchBasedOnGrps (Match.MchCod);
2019-05-20 10:35:57 +02:00
/***** Show game *****/
2019-05-28 15:06:53 +02:00
Gam_ShowOneGame (Match.GamCod,
true, // Show only this game
false, // Do not list questions
false, // Do not put form to start new match
IBelongToGroups); // Put button to play
2019-05-20 10:35:57 +02:00
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/****** Put a big button to play match (start a new match) as a teacher ******/
2019-05-20 10:35:57 +02:00
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
static void Gam_PutBigButtonToPlayMatchTch (struct Game *Game)
2019-05-20 10:35:57 +02:00
{
2019-05-28 15:06:53 +02:00
extern const char *Hlp_ASSESSMENT_Games_new_match;
extern const char *The_ClassFormInBox[The_NUM_THEMES];
extern const char *Txt_New_match;
extern const char *Txt_Title;
extern const char *Txt_Play;
/***** Start section for a new match *****/
Lay_StartSection (Gam_NEW_MATCH_SECTION_ID);
2019-05-20 10:35:57 +02:00
/***** Start form *****/
2019-05-30 12:57:31 +02:00
Frm_StartForm (ActNewMch);
2019-05-28 15:06:53 +02:00
Gam_PutParamGameCod (Game->GamCod);
2019-05-20 10:35:57 +02:00
Gam_PutParamQstInd (0); // Start by first question in game
2019-05-28 15:06:53 +02:00
/***** Start box and table *****/
Box_StartBoxTable (NULL,Txt_New_match,NULL,
Hlp_ASSESSMENT_Games_new_match,Box_NOT_CLOSABLE,2);
/***** Match title *****/
fprintf (Gbl.F.Out,"<tr>"
"<td class=\"RIGHT_MIDDLE\">"
"<label for=\"Title\" class=\"%s\">%s:</label>"
"</td>"
"<td class=\"LEFT_MIDDLE\">"
"<input type=\"text\" id=\"Title\" name=\"Title\""
" size=\"45\" maxlength=\"%u\" value=\"%s\""
" required=\"required\" />"
"</td>"
"</tr>",
The_ClassFormInBox[Gbl.Prefs.Theme],
Txt_Title,
Gam_MAX_CHARS_TITLE,Game->Title);
/***** Groups *****/
Gam_ShowLstGrpsToEditMatch ();
/***** End table *****/
Tbl_EndTable ();
2019-05-20 10:35:57 +02:00
/***** Put icon with link *****/
2019-05-28 15:06:53 +02:00
Frm_LinkFormSubmit (Txt_Play,NULL,NULL);
2019-05-20 10:35:57 +02:00
fprintf (Gbl.F.Out,"<img src=\"%s/play.svg\""
" alt=\"%s\" title=\"%s\""
" class=\"CONTEXT_OPT ICO_HIGHLIGHT ICO64x64\" />",
2019-05-28 15:06:53 +02:00
Cfg_URL_ICON_PUBLIC,Txt_Play,Txt_Play);
2019-05-20 10:35:57 +02:00
fprintf (Gbl.F.Out,"</a>");
2019-05-28 15:06:53 +02:00
/***** End box *****/
Box_EndBox ();
2019-05-20 10:35:57 +02:00
/***** End form *****/
Frm_EndForm ();
2019-05-28 15:06:53 +02:00
/***** End section for a new match *****/
Lay_EndSection ();
2017-09-13 16:24:29 +02:00
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/******** Put a big button to play match (join a match) as a student *********/
2017-09-13 16:24:29 +02:00
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
static void Gam_PutBigButtonToPlayMatchStd (long MchCod)
2017-09-13 16:24:29 +02:00
{
extern const char *Txt_Play;
/***** Start form *****/
2019-05-20 12:26:15 +02:00
Frm_StartForm (ActGamStdCurQst);
2019-05-28 15:06:53 +02:00
Gam_PutParamMatchCod (MchCod);
2017-09-13 21:22:52 +02:00
Gam_PutParamQstInd (0); // Start by first question in game
2017-09-13 16:24:29 +02:00
/***** Put icon with link *****/
2018-11-09 20:47:39 +01:00
Frm_LinkFormSubmit (Txt_Play,NULL,NULL);
2019-01-11 02:55:01 +01:00
fprintf (Gbl.F.Out,"<img src=\"%s/play.svg\""
" alt=\"%s\" title=\"%s\""
2017-09-13 16:24:29 +02:00
" class=\"CONTEXT_OPT ICO_HIGHLIGHT ICO64x64\" />",
2019-03-20 01:36:36 +01:00
Cfg_URL_ICON_PUBLIC,Txt_Play,Txt_Play);
2017-09-13 16:24:29 +02:00
fprintf (Gbl.F.Out,"</a>");
/***** End form *****/
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2017-09-13 09:47:45 +02:00
}
2017-09-13 21:22:52 +02:00
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/********* Create a new match and show first question (by a teacher) *********/
2017-09-13 21:22:52 +02:00
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
void Gam_CreateAndStartNewMatch (void)
2017-09-14 02:32:36 +02:00
{
2019-05-28 15:06:53 +02:00
struct Match Match;
2019-05-20 19:47:17 +02:00
2019-05-28 15:06:53 +02:00
/***** Get form parameters *****/
2019-05-20 19:47:17 +02:00
/* Get game code */
2019-05-28 15:06:53 +02:00
if ((Match.GamCod = Gam_GetParamGameCod ()) == -1L)
2019-05-20 19:47:17 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
2019-05-28 15:06:53 +02:00
/* Get match title */
Par_GetParToText ("Title",Match.Title,Gam_MAX_BYTES_TITLE);
/* Get groups for this games */
Grp_GetParCodsSeveralGrps ();
/***** Create a new match *****/
2019-05-30 20:52:23 +02:00
Gam_CreateMatch (&Match);
2019-05-28 15:06:53 +02:00
/***** Free memory for list of selected groups *****/
Grp_FreeListCodSelectedGrps ();
/***** Show questions and possible answers *****/
2019-05-30 20:52:23 +02:00
Gam_PlayGameShowQuestionAndAnswers (&Match);
2017-09-14 02:32:36 +02:00
}
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/********************** Create a new match in a game *************************/
2017-09-14 02:32:36 +02:00
/*****************************************************************************/
2019-05-30 20:52:23 +02:00
static void Gam_CreateMatch (struct Match *Match)
2019-05-20 19:47:17 +02:00
{
2019-05-30 20:52:23 +02:00
/***** Initialize new match *****/
Match->UsrCod = Gbl.Usrs.Me.UsrDat.UsrCod; // Player (me)
Match->Status.QstInd = Gam_GetFirstQuestionIndexInGame (Match->GamCod);
if (Match->Status.QstInd > 0)
{
2019-05-31 00:40:23 +02:00
Match->Status.QstCod = Gam_GetQstCodFromQstInd (Match->GamCod,Match->Status.QstInd);
2019-05-30 20:52:23 +02:00
Match->Status.ShowingAnswers = false; // Don't show answers initially
2019-05-31 00:40:23 +02:00
Match->Status.Finished = false; // Game not finished
2019-05-30 20:52:23 +02:00
}
else // The game has no questions!
{
2019-05-31 00:40:23 +02:00
Match->Status.QstCod = -1L; // Non-existent question
2019-05-30 20:52:23 +02:00
Match->Status.ShowingAnswers = false; // Don't show answers initially
2019-05-31 00:40:23 +02:00
Match->Status.Finished = true; // Game not finished
2019-05-30 20:52:23 +02:00
}
2019-05-28 15:06:53 +02:00
2019-05-30 20:52:23 +02:00
/***** Insert this new match into database *****/
Match->MchCod = DB_QueryINSERTandReturnCode ("can not create match",
"INSERT gam_matches"
" (GamCod,UsrCod,StartTime,EndTime,Title,"
"QstInd,QstCod,QstStartTime,ShowingAnswers,Finished)"
" VALUES"
" (%ld,%ld,NOW(),NOW(),'%s',"
"%u,%ld,NOW(),'%c','%c')",
Match->GamCod,Match->UsrCod,Match->Title,
Match->Status.QstInd,Match->Status.QstCod,
Match->Status.ShowingAnswers ? 'Y' :
2019-05-31 00:40:23 +02:00
'N',
Match->Status.Finished ? 'Y' :
'N');
2019-05-28 15:06:53 +02:00
/***** Create groups associated to the match *****/
if (Gbl.Crs.Grps.LstGrpsSel.NumGrps)
2019-05-30 20:52:23 +02:00
Gam_CreateGrps (Match->MchCod);
2019-05-28 15:06:53 +02:00
}
2019-05-30 12:57:31 +02:00
/*****************************************************************************/
/**** Resume an unfinished match and show current question (by a teacher) ****/
/*****************************************************************************/
void Gam_ResumeUnfinishedMatch (void)
{
struct Match Match;
/***** Get parameters *****/
/* Get match code */
if ((Match.MchCod = Gam_GetParamMatchCod ()) == -1L)
Lay_ShowErrorAndExit ("Code of match is missing.");
/***** Get data of the match from database *****/
Gam_GetDataOfMatchByCod (&Match);
if (Match.Status.Finished)
2019-05-31 10:25:20 +02:00
Gam_ShowAlertFinishedMatch ();
2019-05-30 12:57:31 +02:00
else // Unfinished match
2019-05-30 13:46:57 +02:00
{
/***** In what question do we resume the match? *****/
if (Match.Status.QstInd == 0)
/* If current question index is 0 ==>
start playing the first question */
2019-05-30 20:52:23 +02:00
Match.Status.QstInd = Gam_GetFirstQuestionIndexInGame (Match.GamCod);
2019-05-30 13:46:57 +02:00
2019-05-30 12:57:31 +02:00
/***** Show questions and possible answers *****/
2019-05-30 20:52:23 +02:00
Gam_PlayGameShowQuestionAndAnswers (&Match);
2019-05-30 13:46:57 +02:00
}
2019-05-30 12:57:31 +02:00
}
2019-05-20 19:47:17 +02:00
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/***************** Insert/update a game match being played *******************/
2019-05-20 19:47:17 +02:00
/*****************************************************************************/
2019-05-30 20:52:23 +02:00
static void Gam_UpdateMatchBeingPlayed (struct Match *Match)
2019-05-20 19:47:17 +02:00
{
2019-05-30 20:52:23 +02:00
/***** Update match status in database *****/
DB_QueryUPDATE ("can not update match being played",
"UPDATE gam_matches,games"
" SET gam_matches.EndTime=NOW(),"
"gam_matches.QstInd=%u,"
"gam_matches.QstCod=%ld,"
2019-05-31 00:40:23 +02:00
"gam_matches.QstStartTime=NOW(),"
2019-05-30 20:52:23 +02:00
"gam_matches.ShowingAnswers='%c',"
2019-05-31 00:40:23 +02:00
"gam_matches.Finished='%c'"
2019-05-30 20:52:23 +02:00
" WHERE gam_matches.MchCod=%ld"
" AND gam_matches.GamCod=games.GamCod"
" AND games.CrsCod=%ld", // Extra check
Match->Status.QstInd,Match->Status.QstCod,
Match->Status.ShowingAnswers ? 'Y' :
'N',
2019-05-31 00:40:23 +02:00
Match->Status.Finished ? 'Y' :
'N',
2019-05-30 20:52:23 +02:00
Match->MchCod,Gbl.Hierarchy.Crs.CrsCod);
2019-05-20 19:47:17 +02:00
}
/*****************************************************************************/
2019-05-30 20:52:23 +02:00
/*** Show next match status (show next question, answers...) (by a teacher) **/
2019-05-20 19:47:17 +02:00
/*****************************************************************************/
2019-05-30 20:52:23 +02:00
void Gam_NextStatusMatch (void)
2017-09-14 02:32:36 +02:00
{
2019-05-30 20:52:23 +02:00
struct Match Match;
long NxtQstInd;
2019-05-20 19:47:17 +02:00
/***** Get parameters *****/
2019-05-28 15:06:53 +02:00
/* Get match code */
2019-05-30 20:52:23 +02:00
if ((Match.MchCod = Gam_GetParamMatchCod ()) == -1L)
2019-05-28 15:06:53 +02:00
Lay_ShowErrorAndExit ("Code of match is missing.");
2019-05-20 19:47:17 +02:00
2019-05-30 20:52:23 +02:00
/***** Get data of the match from database *****/
Gam_GetDataOfMatchByCod (&Match);
2017-09-14 02:32:36 +02:00
2019-05-30 20:52:23 +02:00
/***** If not yet finished, update status *****/
if (!Match.Status.Finished)
{
if (Match.Status.ShowingAnswers) // Showing answers currently
{
/* Get index of the next question */
NxtQstInd = Gam_GetNextQuestionIndexInGame (Match.GamCod,
Match.Status.QstInd);
if (NxtQstInd) // Not last question
{
Match.Status.QstInd = NxtQstInd; // Go to the next question
Match.Status.QstCod = Gam_GetQstCodFromQstInd (Match.GamCod,
Match.Status.QstInd);
Match.Status.ShowingAnswers = false; // Don't show answers
Match.Status.Finished = false; // Game is not finished
}
else // No more questions
{
Match.Status.QstInd = 0; // No more questions
Match.Status.QstCod = -1L; // No more questions
Match.Status.ShowingAnswers = false; // Don't show answers
2019-05-31 00:40:23 +02:00
Match.Status.Finished = true; // Game is finished
2019-05-30 20:52:23 +02:00
}
}
else
{
Match.Status.ShowingAnswers = true; // Show answers
Match.Status.Finished = false; // Game is not finished
}
2017-09-13 21:22:52 +02:00
2019-05-30 20:52:23 +02:00
/* Update match status in database */
Gam_UpdateMatchBeingPlayed (&Match);
}
2017-09-13 21:22:52 +02:00
2019-05-30 20:52:23 +02:00
/***** Show status and questions *****/
if (Match.Status.Finished)
2019-05-31 10:25:20 +02:00
Gam_ShowAlertFinishedMatch ();
2019-05-30 20:52:23 +02:00
else
/* Show questions and possible answers */
Gam_PlayGameShowQuestionAndAnswers (&Match);
2019-05-20 19:47:17 +02:00
}
2019-05-31 10:25:20 +02:00
/*****************************************************************************/
/********************* Show alert about finished match ***********************/
/*****************************************************************************/
static void Gam_ShowAlertFinishedMatch (void)
{
extern const char *Txt_Finished_match;
/***** Show alert *****/
Ale_ShowAlert (Ale_WARNING,Txt_Finished_match);
/***** Button to close browser tab *****/
2019-07-04 10:13:51 +02:00
Btn_PutCloseTabButton ("Cerrar"); // TODO: Need translation!!!!!
2019-05-31 10:25:20 +02:00
}
2019-05-20 19:47:17 +02:00
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
/*********** Show question and its answers when playing a match **************/
2019-05-20 19:47:17 +02:00
/*****************************************************************************/
2019-05-30 20:52:23 +02:00
static void Gam_PlayGameShowQuestionAndAnswers (struct Match *Match)
2019-05-20 19:47:17 +02:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
2019-05-30 12:57:31 +02:00
unsigned NxtQstInd;
2019-05-20 19:47:17 +02:00
2017-09-13 21:22:52 +02:00
/***** Get data of question from database *****/
2018-10-31 10:19:01 +01:00
if (!DB_QuerySELECT (&mysql_res,"can not get data of a question",
2019-05-30 20:52:23 +02:00
"SELECT AnsType," // row[0]
"Stem," // row[1]
"MedCod" // row[2]
" FROM tst_questions"
" WHERE QstCod=%ld",
Match->Status.QstCod))
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_ERROR,"Question doesn't exist.");
2017-09-13 21:22:52 +02:00
row = mysql_fetch_row (mysql_res);
/***** Show question *****/
2017-09-14 02:32:36 +02:00
/* Start container for number and question */
2019-05-22 09:36:18 +02:00
fprintf (Gbl.F.Out,"<div class=\"GAM_PLAY_TCH_CONTAINER\">");
2017-09-13 21:22:52 +02:00
/* Write number of question */
2019-05-30 20:52:23 +02:00
fprintf (Gbl.F.Out,"<div class=\"GAM_PLAY_TCH_NUM_QST\">%u</div>",
Match->Status.QstInd);
2017-09-13 21:22:52 +02:00
2019-05-22 09:36:18 +02:00
fprintf (Gbl.F.Out,"<div class=\"GAM_PLAY_TCH_QST_CONTAINER\">");
2018-04-24 13:21:53 +02:00
2019-05-30 20:52:23 +02:00
/* Get answer type (row[0]) */
Gbl.Test.AnswerType = Tst_ConvertFromStrAnsTypDBToAnsTyp (row[0]);
// TODO: Check that answer type is correct (unique choice)
2019-05-20 19:47:17 +02:00
2019-05-30 20:52:23 +02:00
/* Write stem (row[1]) */
Tst_WriteQstStem (row[1],"GAM_PLAY_TCH_QST");
2019-03-18 15:42:22 +01:00
2019-05-30 20:52:23 +02:00
/* Get media (row[2]) */
Gbl.Test.Media.MedCod = Str_ConvertStrCodToLongCod (row[2]);
2019-03-18 15:42:22 +01:00
Med_GetMediaDataByCod (&Gbl.Test.Media);
/* Show media */
2019-03-02 21:49:11 +01:00
Med_ShowMedia (&Gbl.Test.Media,
2019-03-27 14:36:57 +01:00
"TEST_MED_EDIT_LIST_STEM_CONTAINER",
"TEST_MED_EDIT_LIST_STEM");
2017-09-15 11:39:02 +02:00
/* Write answers? */
2019-05-30 20:52:23 +02:00
if (Match->Status.ShowingAnswers)
2017-09-15 11:39:02 +02:00
/* Write answers */
2019-05-30 20:52:23 +02:00
Tst_WriteAnswersGameResult (Match->GamCod,
Match->Status.QstInd,
Match->Status.QstCod,
2019-05-22 09:36:18 +02:00
"GAM_PLAY_TCH_QST",false); // Don't show result
2017-09-13 21:22:52 +02:00
2017-09-13 22:17:27 +02:00
fprintf (Gbl.F.Out,"</div>");
2017-09-14 02:32:36 +02:00
/***** Put button to continue *****/
2019-05-22 09:36:18 +02:00
fprintf (Gbl.F.Out,"<div class=\"GAM_PLAY_TCH_NXT_CONTAINER\">");
2019-05-30 20:52:23 +02:00
if (Match->Status.ShowingAnswers)
2017-09-14 02:32:36 +02:00
{
/* Get index of the next question */
2019-05-30 20:52:23 +02:00
NxtQstInd = Gam_GetNextQuestionIndexInGame (Match->GamCod,
Match->Status.QstInd);
2019-05-30 12:57:31 +02:00
if (NxtQstInd) // Not last question
2017-09-14 02:32:36 +02:00
/* Put button to show next question */
2019-05-30 20:52:23 +02:00
Gam_PutBigButtonToContinue (Match->MchCod);
2019-05-30 12:57:31 +02:00
else // Last question
2019-05-20 19:47:17 +02:00
/* Put button to end */
2019-05-30 20:52:23 +02:00
Gam_PutBigButtonToFinishMatch (Match->MchCod);
2017-09-14 02:32:36 +02:00
}
else
/* Put button to show answers */
2019-05-30 20:52:23 +02:00
Gam_PutBigButtonToContinue (Match->MchCod);
2018-04-24 13:21:53 +02:00
fprintf (Gbl.F.Out,"</div>");
/***** End container for question *****/
fprintf (Gbl.F.Out,"</div>");
2017-09-13 22:17:27 +02:00
}
/*****************************************************************************/
/*********************** Put a big button to continue ************************/
/*****************************************************************************/
2019-05-30 20:52:23 +02:00
static void Gam_PutBigButtonToContinue (long MchCod)
2017-09-13 22:17:27 +02:00
{
extern const char *Txt_Continue;
/***** Start container *****/
2019-05-22 09:36:18 +02:00
fprintf (Gbl.F.Out,"<div class=\"GAM_PLAY_TCH_CONTINUE_CONTAINER\">");
2017-09-13 22:17:27 +02:00
/***** Start form *****/
2019-05-30 20:52:23 +02:00
Frm_StartForm (ActNxtMch);
2019-05-28 15:06:53 +02:00
Gam_PutParamMatchCod (MchCod);
2017-09-13 22:17:27 +02:00
/***** Put icon with link *****/
2019-05-22 09:36:18 +02:00
Frm_LinkFormSubmit (Txt_Continue,"GAM_PLAY_TCH_CONTINUE ICO_HIGHLIGHT",NULL);
2019-01-11 02:55:01 +01:00
fprintf (Gbl.F.Out,"<img src=\"%s/step-forward.svg\""
2017-09-14 02:32:36 +02:00
" alt=\"%s\" title=\"%s\" class=\"ICO64x64\" />"
2017-09-13 22:17:27 +02:00
"<br />"
"%s",
2019-05-20 19:47:17 +02:00
Cfg_URL_ICON_PUBLIC,
Txt_Continue,Txt_Continue,
Txt_Continue);
fprintf (Gbl.F.Out,"</a>");
/***** End form *****/
Frm_EndForm ();
/***** End container *****/
fprintf (Gbl.F.Out,"</div>");
}
/*****************************************************************************/
2019-05-30 12:57:31 +02:00
/******************** Put a big button to finish a match *********************/
2019-05-20 19:47:17 +02:00
/*****************************************************************************/
2019-05-30 12:57:31 +02:00
static void Gam_PutBigButtonToFinishMatch (long MchCod)
2019-05-20 19:47:17 +02:00
{
extern const char *Txt_Finish;
/***** Start container *****/
fprintf (Gbl.F.Out,"<div class=\"GAM_PLAY_CONTINUE_CONTAINER\">");
/***** Start form *****/
2019-05-31 00:40:23 +02:00
Frm_StartForm (ActNxtMch);
2019-05-28 15:06:53 +02:00
Gam_PutParamMatchCod (MchCod);
2019-05-20 19:47:17 +02:00
/***** Put icon with link *****/
Frm_LinkFormSubmit (Txt_Finish,"GAM_PLAY_CONTINUE ICO_HIGHLIGHT",NULL);
fprintf (Gbl.F.Out,"<img src=\"%s/flag-checkered.svg\""
" alt=\"%s\" title=\"%s\" class=\"ICO64x64\" />"
"<br />"
"%s",
Cfg_URL_ICON_PUBLIC,
Txt_Finish,Txt_Finish,
Txt_Finish);
2017-09-13 22:17:27 +02:00
fprintf (Gbl.F.Out,"</a>");
/***** End form *****/
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2017-09-13 22:17:27 +02:00
/***** End container *****/
2017-09-13 21:22:52 +02:00
fprintf (Gbl.F.Out,"</div>");
}
2019-05-20 12:26:15 +02:00
/*****************************************************************************/
2019-05-31 10:04:13 +02:00
/******************* Show the results of a finished match ********************/
/*****************************************************************************/
void Gam_ShowFinishedMatchResults (void)
{
Ale_ShowAlert (Ale_INFO,"To be implemented...");
}
/*****************************************************************************/
/********************** Get code of match being played ***********************/
2019-05-20 12:26:15 +02:00
/*****************************************************************************/
2019-05-28 15:06:53 +02:00
void Gam_GetMatchBeingPlayed (void)
2019-05-20 12:26:15 +02:00
{
2019-05-28 15:06:53 +02:00
/***** Get match code ****/
if ((Gbl.Games.MchCodBeingPlayed = Gam_GetParamMatchCod ()) == -1L)
Lay_ShowErrorAndExit ("Code of match is missing.");
2019-05-22 09:36:18 +02:00
}
2019-05-20 12:26:15 +02:00
2019-05-22 09:36:18 +02:00
/*****************************************************************************/
/********* Show game being played to me as student in a new window ***********/
/*****************************************************************************/
2019-05-20 12:26:15 +02:00
2019-05-28 15:06:53 +02:00
void Gam_ShowNewMatchToMeAsStd (void)
2019-05-22 09:36:18 +02:00
{
2019-05-29 01:14:56 +02:00
struct Match Match;
/***** Get data of the match from database *****/
Match.MchCod = Gbl.Games.MchCodBeingPlayed;
Gam_GetDataOfMatchByCod (&Match);
/***** Show current question *****/
2019-05-22 09:36:18 +02:00
fprintf (Gbl.F.Out,"<div id=\"game\" class=\"GAM_PLAY_STD_CONTAINER\">");
2019-05-29 01:14:56 +02:00
Gam_ShowQuestionBeingPlayed (&Match);
2019-05-22 09:36:18 +02:00
fprintf (Gbl.F.Out,"</div>");
}
2019-05-20 12:26:15 +02:00
2019-05-22 09:36:18 +02:00
/*****************************************************************************/
/*************** Refresh current game for a student via AJAX *****************/
/*****************************************************************************/
2019-05-20 12:26:15 +02:00
2019-05-28 15:06:53 +02:00
void Gam_RefreshCurrentMatchStd (void)
2019-05-22 09:36:18 +02:00
{
2019-05-29 01:14:56 +02:00
struct Match Match;
if (!Gbl.Session.IsOpen) // If session has been closed, do not write anything
return;
/***** Get data of the match from database *****/
Match.MchCod = Gbl.Games.MchCodBeingPlayed;
Gam_GetDataOfMatchByCod (&Match);
/***** Show current question *****/
Gam_ShowQuestionBeingPlayed (&Match);
2019-05-22 09:36:18 +02:00
}
2019-05-20 12:26:15 +02:00
2019-05-22 09:36:18 +02:00
/*****************************************************************************/
/************ Show current question being played for a student ***************/
/*****************************************************************************/
2019-05-20 12:26:15 +02:00
2019-05-29 01:14:56 +02:00
static void Gam_ShowQuestionBeingPlayed (struct Match *Match)
2019-05-22 09:36:18 +02:00
{
2019-05-28 15:06:53 +02:00
bool IBelongToGroups;
2019-05-22 09:36:18 +02:00
unsigned NumOptions;
unsigned NumOpt;
2019-05-28 15:06:53 +02:00
/***** Do I belong to valid groups to play this match? *****/
IBelongToGroups = Gbl.Usrs.Me.IBelongToCurrentCrs &&
2019-05-29 01:14:56 +02:00
Gam_CheckIfIPlayThisMatchBasedOnGrps (Match->MchCod);
2019-05-28 15:06:53 +02:00
if (!IBelongToGroups)
Lay_ShowErrorAndExit ("You can not play this match!");
2019-05-22 09:36:18 +02:00
/***** Show question *****/
2019-05-29 01:14:56 +02:00
if (!Match->Status.Finished)
2019-05-22 09:36:18 +02:00
{
/***** Show question *****/
/* Write number of question */
fprintf (Gbl.F.Out,"<div class=\"GAM_PLAY_STD_NUM_QST\">%u</div>",
2019-05-30 12:57:31 +02:00
Match->Status.QstInd);
2019-05-22 09:36:18 +02:00
fprintf (Gbl.F.Out,"<div class=\"GAM_PLAY_STD_QST_CONTAINER\">");
/* Write answers? */
2019-05-30 20:52:23 +02:00
if (Match->Status.ShowingAnswers)
2019-05-22 09:36:18 +02:00
{
2019-05-29 01:14:56 +02:00
if (Tst_CheckIfQuestionIsValidForGame (Match->Status.QstCod))
2019-05-22 09:36:18 +02:00
{
/***** Start table *****/
Tbl_StartTableWide (8);
/***** Write answers *****/
2019-05-29 01:14:56 +02:00
NumOptions = Tst_GetNumAnswersQst (Match->Status.QstCod);
2019-05-22 09:36:18 +02:00
for (NumOpt = 0;
NumOpt < NumOptions;
NumOpt++)
{
// if (NumOpt % 2 == 0)
fprintf (Gbl.F.Out,"<tr>");
/***** Write letter for this option *****/
fprintf (Gbl.F.Out,"<td class=\"GAM_PLAY_STD_CELL\">"
"<div class=\"GAM_PLAY_STD_BUTTON BT_%c\">"
"%c"
"</div>"
"</td>",
'A' + (char) NumOpt,
'a' + (char) NumOpt);
// if (NumOpt % 2 == 1)
fprintf (Gbl.F.Out,"</tr>");
}
/***** End table *****/
Tbl_EndTable ();
}
else
Ale_ShowAlert (Ale_ERROR,"Type of answer not valid in a game.");
}
fprintf (Gbl.F.Out,"</div>");
}
2019-05-22 10:48:28 +02:00
else
{
fprintf (Gbl.F.Out,"<img src=\"%s/wait.gif\""
" alt=\"Please wait\" title=\"%s\""
" class=\"GAM_PLAY_STD_WAIT\" />",
Cfg_URL_ICON_PUBLIC,
"Por favor, espere a que el juego comience..."); // TODO: Need translation!!!!!
}
2019-05-20 12:26:15 +02:00
}
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
/************************ Receive answers of a game ************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_ReceiveGameAnswers (void)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_You_already_played_this_game_before;
extern const char *Txt_Thanks_for_playing_the_game;
struct Game Game;
/***** Get game code *****/
2017-09-07 18:38:18 +02:00
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Code of game is missing.");
/***** Get data of the game from database *****/
2017-09-07 18:38:18 +02:00
Gam_GetDataOfGameByCod (&Game);
2017-07-09 20:31:40 +02:00
/***** Check if I have no answered this game formerly *****/
if (Game.Status.IHaveAnswered)
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_WARNING,Txt_You_already_played_this_game_before);
2017-07-09 20:31:40 +02:00
else
{
/***** Receive and store user's answers *****/
2017-09-07 18:38:18 +02:00
Gam_ReceiveAndStoreUserAnswersToAGame (Game.GamCod);
2019-02-16 16:18:54 +01:00
Ale_ShowAlert (Ale_INFO,Txt_Thanks_for_playing_the_game);
2017-07-09 20:31:40 +02:00
}
/***** Show current game *****/
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Game.GamCod,
true, // Show only this game
true, // List game questions
2019-05-28 15:06:53 +02:00
false, // Do not put form to start new match
2019-05-20 10:35:57 +02:00
false); // Do not put button to play
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/**************** Get and store user's answers to a game *******************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_ReceiveAndStoreUserAnswersToAGame (long GamCod)
2017-07-09 20:31:40 +02:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumQst;
unsigned NumQsts;
long QstCod;
char ParamName[3 + 10 + 6 + 1];
2017-09-07 12:00:01 +02:00
char StrAnswersIndexes[Tst_MAX_OPTIONS_PER_QUESTION * (10 + 1)];
2017-07-09 20:31:40 +02:00
const char *Ptr;
char UnsignedStr[10 + 1];
unsigned AnsInd;
/***** Get questions of this game from database *****/
2018-10-31 10:19:01 +01:00
NumQsts = (unsigned) DB_QuerySELECT (&mysql_res,"can not get questions of a game",
"SELECT QstCod FROM gam_questions"
" WHERE GamCod=%ld ORDER BY QstCod",
GamCod);
if (NumQsts) // The game has questions
2017-07-09 20:31:40 +02:00
{
/***** Get questions *****/
for (NumQst = 0;
NumQst < NumQsts;
NumQst++)
{
/* Get next answer */
row = mysql_fetch_row (mysql_res);
/* Get question code (row[0]) */
if ((QstCod = Str_ConvertStrCodToLongCod (row[0])) <= 0)
Lay_ShowErrorAndExit ("Error: wrong question code.");
/* Get possible parameter with the user's answer */
2018-10-18 02:02:32 +02:00
snprintf (ParamName,sizeof (ParamName),
"Ans%010u",
(unsigned) QstCod);
2017-07-09 20:31:40 +02:00
// Lay_ShowAlert (Lay_INFO,ParamName);
Par_GetParMultiToText (ParamName,StrAnswersIndexes,
2017-09-07 18:38:18 +02:00
Gam_MAX_ANSWERS_PER_QUESTION * (10 + 1));
2017-07-09 20:31:40 +02:00
Ptr = StrAnswersIndexes;
while (*Ptr)
{
Par_GetNextStrUntilSeparParamMult (&Ptr,UnsignedStr,10);
if (sscanf (UnsignedStr,"%u",&AnsInd) == 1)
// Parameter exists, so user has marked this answer, so store it in database
2017-09-07 18:38:18 +02:00
Gam_IncreaseAnswerInDB (QstCod,AnsInd);
2017-07-09 20:31:40 +02:00
}
}
}
else // The game has no questions and answers
Lay_ShowErrorAndExit ("Error: this game has no questions.");
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
/***** Register that you have answered this game *****/
2017-09-07 18:38:18 +02:00
Gam_RegisterIHaveAnsweredGame (GamCod);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/************ Increase number of users who have marked one answer ************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_IncreaseAnswerInDB (long QstCod,unsigned AnsInd)
2017-07-09 20:31:40 +02:00
{
2017-09-01 10:37:00 +02:00
/***** Increase number of users who have selected
the answer AnsInd in the question QstCod *****/
2019-03-04 14:36:29 +01:00
DB_QueryUPDATE ("can not register your answer to the game",
2018-11-02 19:37:11 +01:00
"UPDATE gam_answers SET NumUsrs=NumUsrs+1"
" WHERE QstCod=%ld AND AnsInd=%u",
QstCod,AnsInd);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2017-09-01 10:37:00 +02:00
/******************* Register that I have answered a game ********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_RegisterIHaveAnsweredGame (long GamCod)
2017-07-09 20:31:40 +02:00
{
2018-11-02 19:37:11 +01:00
DB_QueryINSERT ("can not register that you have answered the game",
"INSERT INTO gam_users"
" (GamCod,UsrCod)"
" VALUES"
" (%ld,%ld)",
GamCod,Gbl.Usrs.Me.UsrDat.UsrCod);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2017-09-01 10:37:00 +02:00
/******************** Check if I have answered a game ************************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static bool Gam_CheckIfIHaveAnsweredGame (long GamCod)
2017-07-09 20:31:40 +02:00
{
/***** Get number of games with a field value from database *****/
2018-11-03 20:52:00 +01:00
return (DB_QueryCOUNT ("can not check if you have answered a game",
"SELECT COUNT(*) FROM gam_users"
" WHERE GamCod=%ld AND UsrCod=%ld",
GamCod,Gbl.Usrs.Me.UsrDat.UsrCod) != 0);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2017-09-01 10:37:00 +02:00
/************** Get number of users who have answered a game *****************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static unsigned Gam_GetNumUsrsWhoHaveAnsweredGame (long GamCod)
2017-07-09 20:31:40 +02:00
{
/***** Get number of games with a field value from database *****/
2018-11-03 20:52:00 +01:00
return
(unsigned) DB_QueryCOUNT ("can not get number of users"
" who have answered a game",
"SELECT COUNT(*) FROM gam_users"
" WHERE GamCod=%ld",
GamCod);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
2017-09-01 10:37:00 +02:00
/********************* Get number of courses with games **********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-17 18:22:36 +02:00
// Returns the number of courses with games in this location
2017-07-09 20:31:40 +02:00
2019-04-03 20:57:04 +02:00
unsigned Gam_GetNumCoursesWithGames (Hie_Level_t Scope)
2017-07-09 20:31:40 +02:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumCourses;
/***** Get number of courses with games from database *****/
switch (Scope)
{
2019-04-03 20:57:04 +02:00
case Hie_SYS:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of courses with games",
"SELECT COUNT(DISTINCT Cod)"
" FROM games"
" WHERE Scope='%s'",
2019-04-03 20:57:04 +02:00
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CTY:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of courses with games",
"SELECT COUNT(DISTINCT games.Cod)"
" FROM institutions,centres,degrees,courses,games"
" WHERE institutions.CtyCod=%ld"
" AND institutions.InsCod=centres.InsCod"
" AND centres.CtrCod=degrees.CtrCod"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ins.InsCod,
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_INS:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of courses with games",
"SELECT COUNT(DISTINCT games.Cod)"
" FROM centres,degrees,courses,games"
" WHERE centres.InsCod=%ld"
" AND centres.CtrCod=degrees.CtrCod"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ins.InsCod,
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CTR:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of courses with games",
" FROM degrees,courses,games"
" WHERE degrees.CtrCod=%ld"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ctr.CtrCod,
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_DEG:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of courses with games",
"SELECT COUNT(DISTINCT games.Cod)"
" FROM courses,games"
" WHERE courses.DegCod=%ld"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Deg.DegCod,
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CRS:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of courses with games",
"SELECT COUNT(DISTINCT Cod)"
" FROM games"
" WHERE Scope='%s' AND Cod=%ld",
2019-04-03 20:57:04 +02:00
Sco_GetDBStrFromScope (Hie_CRS),
2019-04-04 10:45:15 +02:00
Gbl.Hierarchy.Crs.CrsCod);
2017-07-09 20:31:40 +02:00
break;
default:
2018-10-24 23:03:11 +02:00
Lay_WrongScopeExit ();
2017-07-09 20:31:40 +02:00
break;
}
/***** Get number of games *****/
row = mysql_fetch_row (mysql_res);
if (sscanf (row[0],"%u",&NumCourses) != 1)
Lay_ShowErrorAndExit ("Error when getting number of courses with games.");
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
return NumCourses;
}
/*****************************************************************************/
2017-09-17 18:22:36 +02:00
/**************************** Get number of games ****************************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-17 18:22:36 +02:00
// Returns the number of games in this location
2017-07-09 20:31:40 +02:00
2019-04-03 20:57:04 +02:00
unsigned Gam_GetNumGames (Hie_Level_t Scope)
2017-07-09 20:31:40 +02:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumGames;
/***** Get number of games from database *****/
switch (Scope)
{
2019-04-03 20:57:04 +02:00
case Hie_SYS:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of games",
"SELECT COUNT(*)"
" FROM games"
" WHERE Scope='%s'",
2019-04-03 20:57:04 +02:00
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CTY:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of games",
"SELECT COUNT(*)"
" FROM institutions,centres,degrees,courses,games"
" WHERE institutions.CtyCod=%ld"
" AND institutions.InsCod=centres.InsCod"
" AND centres.CtrCod=degrees.CtrCod"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Cty.CtyCod,
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_INS:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of games",
"SELECT COUNT(*)"
" FROM centres,degrees,courses,games"
" WHERE centres.InsCod=%ld"
" AND centres.CtrCod=degrees.CtrCod"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ins.InsCod,
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CTR:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of games",
"SELECT COUNT(*)"
" FROM degrees,courses,games"
" WHERE degrees.CtrCod=%ld"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ctr.CtrCod,
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_DEG:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of games",
"SELECT COUNT(*)"
" FROM courses,games"
" WHERE courses.DegCod=%ld"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Deg.DegCod,
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CRS:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of games",
"SELECT COUNT(*)"
" FROM games"
" WHERE games.Scope='%s'"
" AND CrsCod=%ld",
2019-04-03 20:57:04 +02:00
Sco_GetDBStrFromScope (Hie_CRS),
2019-04-04 10:45:15 +02:00
Gbl.Hierarchy.Crs.CrsCod);
2017-07-09 20:31:40 +02:00
break;
default:
2018-10-24 23:03:11 +02:00
Lay_WrongScopeExit ();
2017-07-09 20:31:40 +02:00
break;
}
/***** Get number of games *****/
row = mysql_fetch_row (mysql_res);
if (sscanf (row[0],"%u",&NumGames) != 1)
Lay_ShowErrorAndExit ("Error when getting number of games.");
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
return NumGames;
}
/*****************************************************************************/
2017-09-01 10:37:00 +02:00
/************* Get average number of questions per course game ***************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2019-04-03 20:57:04 +02:00
float Gam_GetNumQstsPerCrsGame (Hie_Level_t Scope)
2017-07-09 20:31:40 +02:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
float NumQstsPerGame;
/***** Get number of questions per game from database *****/
switch (Scope)
{
2019-04-03 20:57:04 +02:00
case Hie_SYS:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of questions per game",
"SELECT AVG(NumQsts) FROM"
" (SELECT COUNT(gam_questions.QstCod) AS NumQsts"
" FROM games,gam_questions"
" WHERE games.Scope='%s'"
" AND games.GamCod=gam_questions.GamCod"
" GROUP BY gam_questions.GamCod) AS NumQstsTable",
2019-04-03 20:57:04 +02:00
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CTY:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of questions per game",
"SELECT AVG(NumQsts) FROM"
" (SELECT COUNT(gam_questions.QstCod) AS NumQsts"
" FROM institutions,centres,degrees,courses,games,gam_questions"
" WHERE institutions.CtyCod=%ld"
" AND institutions.InsCod=centres.InsCod"
" AND centres.CtrCod=degrees.CtrCod"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'"
" AND games.GamCod=gam_questions.GamCod"
" GROUP BY gam_questions.GamCod) AS NumQstsTable",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Cty.CtyCod,
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_INS:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of questions per game",
"SELECT AVG(NumQsts) FROM"
" (SELECT COUNT(gam_questions.QstCod) AS NumQsts"
" FROM centres,degrees,courses,games,gam_questions"
" WHERE centres.InsCod=%ld"
" AND centres.CtrCod=degrees.CtrCod"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'"
" AND games.GamCod=gam_questions.GamCod"
" GROUP BY gam_questions.GamCod) AS NumQstsTable",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ins.InsCod,
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CTR:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of questions per game",
"SELECT AVG(NumQsts) FROM"
" (SELECT COUNT(gam_questions.QstCod) AS NumQsts"
" FROM degrees,courses,games,gam_questions"
" WHERE degrees.CtrCod=%ld"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'"
" AND games.GamCod=gam_questions.GamCod"
" GROUP BY gam_questions.GamCod) AS NumQstsTable",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ctr.CtrCod,
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_DEG:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of questions per game",
"SELECT AVG(NumQsts) FROM"
" (SELECT COUNT(gam_questions.QstCod) AS NumQsts"
" FROM courses,games,gam_questions"
" WHERE courses.DegCod=%ld"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'"
" AND games.GamCod=gam_questions.GamCod"
" GROUP BY gam_questions.GamCod) AS NumQstsTable",
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Deg.DegCod,
Sco_GetDBStrFromScope (Hie_CRS));
2017-07-09 20:31:40 +02:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CRS:
2018-10-31 10:19:01 +01:00
DB_QuerySELECT (&mysql_res,"can not get number of questions per game",
"SELECT AVG(NumQsts) FROM"
" (SELECT COUNT(gam_questions.QstCod) AS NumQsts"
" FROM games,gam_questions"
" WHERE games.Scope='%s' AND games.Cod=%ld"
" AND games.GamCod=gam_questions.GamCod"
" GROUP BY gam_questions.GamCod) AS NumQstsTable",
2019-04-04 10:45:15 +02:00
Sco_GetDBStrFromScope (Hie_CRS),Gbl.Hierarchy.Crs.CrsCod);
2017-07-09 20:31:40 +02:00
break;
default:
2018-10-24 23:03:11 +02:00
Lay_WrongScopeExit ();
2017-07-09 20:31:40 +02:00
break;
}
/***** Get number of courses *****/
row = mysql_fetch_row (mysql_res);
NumQstsPerGame = Str_GetFloatNumFromStr (row[0]);
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
return NumQstsPerGame;
}