swad-core/swad_game.c

3910 lines
141 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.
Copyright (C) 1999-2017 Antonio Ca<EFBFBD>as Vargas
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 ***********************************/
/*****************************************************************************/
#include <linux/limits.h> // For PATH_MAX
#include <linux/stddef.h> // For NULL
#include <stdlib.h> // For calloc
#include <string.h> // For string functions
#include "swad_alert.h"
#include "swad_box.h"
#include "swad_database.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"
#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
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
/******************************* Private types *******************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private variables *****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** 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_PutFormToSelectWhichGroupsToShow (void);
static void Gam_ParamsWhichGroupsToShow (void);
2017-09-13 16:24:29 +02:00
static void Gam_ShowOneGame (long GamCod,
bool ShowOnlyThisGame,
bool ListGameQuestions,
bool PutButtonToStart);
2017-09-07 18:38:18 +02:00
static void Gam_WriteAuthor (struct Game *Game);
static void Gam_WriteStatus (struct Game *Game);
static void Gam_GetParamGameOrder (void);
2017-07-09 20:31:40 +02:00
2017-09-07 18:38:18 +02:00
static void Gam_PutFormsToRemEditOneGame (long GamCod,bool Visible);
static void Gam_PutParams (void);
2017-07-09 20:31:40 +02:00
2017-09-07 18:38:18 +02:00
static void Gam_SetAllowedAndHiddenScopes (unsigned *ScopesAllowed,
2017-07-09 20:31:40 +02:00
unsigned *HiddenAllowed);
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
2017-09-07 18:38:18 +02:00
static void Gam_PutButtonToResetGame (void);
2017-07-09 20:31:40 +02:00
2017-09-07 18:38:18 +02:00
static bool Gam_CheckIfSimilarGameExists (struct Game *Game);
static void Gam_SetDefaultAndAllowedScope (struct Game *Game);
static void Gam_ShowLstGrpsToEditGame (long GamCod);
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);
static bool Gam_CheckIfGamIsAssociatedToGrps (long GamCod);
static void Gam_RemoveAllTheGrpsAssociatedToAndGame (long GamCod);
static void Gam_CreateGrps (long GamCod);
static void Gam_GetAndWriteNamesOfGrpsAssociatedToGame (struct Game *Game);
static bool Gam_CheckIfICanDoThisGameBasedOnGrps (long GamCod);
static unsigned Gam_GetNumQstsGame (long GamCod);
static void Gam_PutParamQstCod (long QstCod);
static long Gam_GetParamQstCod (void);
static void Gam_RemAnswersOfAQuestion (long QstCod);
2017-09-11 19:06:46 +02:00
static unsigned Gam_GetQstIndFromQstCod (long GamCod,long QstCod);
static long Gam_GetQstCodFromQstInd (long GamCod,unsigned QstInd);
static int Gam_GetMaxQuestionIndexInGame (long GamCod);
static unsigned Gam_GetNextQuestionIndexInGame (long GamCod,unsigned QstInd);
static unsigned Gam_GetPrevQuestionIndexInGame (long GamCod,unsigned QstInd);
2017-09-07 18:38:18 +02:00
static void Gam_ListGameQuestions (struct Game *Game);
static void Gam_ListOneOrMoreQuestionsForEdition (struct Game *Game,
2017-09-06 15:03:43 +02:00
unsigned NumQsts,
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
2017-09-07 18:38:18 +02:00
static unsigned Gam_GetNumUsrsWhoAnswered (long GamCod,long QstCod,unsigned AnsInd);
static void Gam_DrawBarNumUsrs (unsigned NumUsrs,unsigned MaxUsrs);
2017-07-09 20:31:40 +02:00
2017-09-07 18:38:18 +02:00
// static void Gam_PutIconToRemoveOneQst (void);
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);
2017-09-13 16:24:29 +02:00
static void Gam_PutBigButtonToStartGame (void);
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_Status;
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 *****/
if (!Gbl.CurrentCrs.Grps.NumGrps)
Gbl.CurrentCrs.Grps.WhichGrps = Grp_ALL_GROUPS;
/***** 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 *****/
if (Gbl.CurrentCrs.Grps.NumGrps)
2017-09-07 18:38:18 +02:00
Gam_PutFormToSelectWhichGroupsToShow ();
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 */
Act_FormStart (ActSeeAllGam);
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);
Act_LinkFormSubmit (Txt_START_END_TIME_HELP[Order],"TIT_TBL",NULL);
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>");
Act_FormEnd ();
fprintf (Gbl.F.Out,"</th>");
}
fprintf (Gbl.F.Out,"<th class=\"LEFT_MIDDLE\">"
"%s"
"</th>"
"<th class=\"CENTER_MIDDLE\">"
"%s"
"</th>"
"</tr>",
Txt_Game,
Txt_Status);
/***** 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,
false);
2017-07-09 20:31:40 +02:00
/***** End table *****/
Tbl_EndTable ();
}
else // No games created
Ale_ShowAlert (Ale_INFO,Txt_No_games);
/***** 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:
return (Gbl.CurrentCrs.Crs.CrsCod > 0);
case Rol_DEG_ADM:
return (Gbl.CurrentDeg.Deg.DegCod > 0); // Always true
case Rol_CTR_ADM:
return (Gbl.CurrentCtr.Ctr.CtrCod > 0); // Always true
case Rol_INS_ADM:
return (Gbl.CurrentIns.Ins.InsCod > 0); // Always true
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 *****/
Gbl.Stat.FigureType = Sta_SURVEYS;
Sta_PutIconToShowFigure ();
}
/*****************************************************************************/
/********************** 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;
2017-09-07 18:38:18 +02:00
Lay_PutContextualLink (ActFrmNewGam,NULL,Gam_PutParamsToCreateNewGame,
2017-07-09 20:31:40 +02:00
"plus64x64.png",
Txt_New_game,NULL,
NULL);
}
/*****************************************************************************/
/********************* 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;
Act_FormStart (ActFrmNewGam);
2017-09-07 18:38:18 +02:00
Gam_PutParamsToCreateNewGame ();
2017-07-09 20:31:40 +02:00
Btn_PutConfirmButton (Txt_New_game);
Act_FormEnd ();
}
/*****************************************************************************/
/******************* 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
}
/*****************************************************************************/
/***************** Put form to select which groups to show *******************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_PutFormToSelectWhichGroupsToShow (void)
2017-07-09 20:31:40 +02:00
{
fprintf (Gbl.F.Out,"<div style=\"display:table; margin:0 auto;\">");
2017-09-07 18:38:18 +02:00
Grp_ShowFormToSelWhichGrps (ActSeeAllGam,Gam_ParamsWhichGroupsToShow);
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out,"</div>");
}
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
false);
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,
bool PutButtonToStart)
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_Scope;
extern const char *Txt_Country;
extern const char *Txt_Institution;
extern const char *Txt_Centre;
extern const char *Txt_Degree;
extern const char *Txt_Course;
extern const char *Txt_Users;
extern const char *Txt_Play;
extern const char *Txt_View_game_results;
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);
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);
/***** Write first row of data of this assignment *****/
/* Forms to remove/edit this assignment */
fprintf (Gbl.F.Out,"<tr>"
"<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,"\">");
if (Game.Status.ICanEdit)
2017-09-07 18:38:18 +02:00
Gam_PutFormsToRemEditOneGame (Game.GamCod,Game.Status.Visible);
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out,"</td>");
/* Start date/time */
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);
fprintf (Gbl.F.Out,"\">"
"<script type=\"text/javascript\">"
"writeLocalDateHMSFromUTC('gam_date_start_%u',%ld,"
"%u,'<br />','%s',true,true,0x7);"
"</script>"
"</td>",
2017-09-07 18:38:18 +02:00
UniqueId,Game.TimeUTC[Gam_START_TIME],
2017-07-09 20:31:40 +02:00
(unsigned) Gbl.Prefs.DateFormat,Txt_Today);
/* End date/time */
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);
fprintf (Gbl.F.Out,"\">"
"<script type=\"text/javascript\">"
"writeLocalDateHMSFromUTC('gam_date_end_%u',%ld,"
"%u,'<br />','%s',false,true,0x7);"
"</script>"
"</td>",
2017-09-07 18:38:18 +02:00
UniqueId,Game.TimeUTC[Gam_END_TIME],
2017-07-09 20:31:40 +02:00
(unsigned) Gbl.Prefs.DateFormat,Txt_Today);
/* Game title */
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,"\">");
/* Put form to view game */
Act_FormStart (ActSeeOneGam);
2017-09-07 18:38:18 +02:00
Gam_PutParamGameCod (GamCod);
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
Act_LinkFormSubmit (Txt_View_game,
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);
Act_FormEnd ();
/* Number of questions and number of distinct users who have already answered this game */
fprintf (Gbl.F.Out,"<div class=\"%s\">%s: %u; %s: %u</div>"
"</td>",
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);
/* Status of the game */
fprintf (Gbl.F.Out,"<td rowspan=\"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_WriteStatus (&Game);
2017-07-09 20:31:40 +02:00
2017-09-13 16:24:29 +02:00
if (ShowOnlyThisGame)
2017-09-13 09:47:45 +02:00
{
fprintf (Gbl.F.Out,"<div class=\"BUTTONS_AFTER_ALERT\">");
Act_FormStart (ActPlyGam);
Gam_PutParamGameCod (Game.GamCod);
Gam_PutHiddenParamGameOrder ();
Grp_PutParamWhichGrps ();
2017-09-13 16:24:29 +02:00
Pag_PutHiddenParamPagNum (Pag_GAMES,Gbl.Games.CurrentPage);
2017-09-13 09:47:45 +02:00
Btn_PutCreateButtonInline (Txt_Play);
Act_FormEnd ();
fprintf (Gbl.F.Out,"</div>");
}
else // Show several games
2017-07-09 20:31:40 +02:00
{
/* Possible button to answer this game */
if (Game.Status.ICanAnswer)
{
fprintf (Gbl.F.Out,"<div class=\"BUTTONS_AFTER_ALERT\">");
Act_FormStart (ActSeeOneGam);
2017-09-07 18:38:18 +02:00
Gam_PutParamGameCod (Game.GamCod);
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
Btn_PutCreateButtonInline (Txt_Play);
Act_FormEnd ();
fprintf (Gbl.F.Out,"</div>");
}
/* Possible button to see the result of the game */
else if (Game.Status.ICanViewResults)
{
fprintf (Gbl.F.Out,"<div class=\"BUTTONS_AFTER_ALERT\">");
Act_FormStart (ActSeeOneGam);
2017-09-07 18:38:18 +02:00
Gam_PutParamGameCod (Game.GamCod);
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
Btn_PutConfirmButtonInline (Txt_View_game_results);
Act_FormEnd ();
fprintf (Gbl.F.Out,"</div>");
}
}
fprintf (Gbl.F.Out,"</td>"
"</tr>");
/***** Write second row of data of this game *****/
fprintf (Gbl.F.Out,"<tr>"
"<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,"\">");
/* Author of the game */
2017-09-07 18:38:18 +02:00
Gam_WriteAuthor (&Game);
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out,"</td>"
"<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,"\">");
/* Scope of the game */
fprintf (Gbl.F.Out,"<div class=\"%s\">%s: ",
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_Scope);
switch (Game.Scope)
{
case Sco_SCOPE_UNK: // Unknown
Lay_ShowErrorAndExit ("Wrong game scope.");
break;
case Sco_SCOPE_SYS: // System
fprintf (Gbl.F.Out,"%s",
Cfg_PLATFORM_SHORT_NAME);
break;
case Sco_SCOPE_CTY: // Country
fprintf (Gbl.F.Out,"%s %s",
Txt_Country,Gbl.CurrentCty.Cty.Name[Gbl.Prefs.Language]);
break;
case Sco_SCOPE_INS: // Institution
fprintf (Gbl.F.Out,"%s %s",
Txt_Institution,Gbl.CurrentIns.Ins.ShrtName);
break;
case Sco_SCOPE_CTR: // Centre
fprintf (Gbl.F.Out,"%s %s",
Txt_Centre,Gbl.CurrentCtr.Ctr.ShrtName);
break;
case Sco_SCOPE_DEG: // Degree
fprintf (Gbl.F.Out,"%s %s",
Txt_Degree,Gbl.CurrentDeg.Deg.ShrtName);
break;
case Sco_SCOPE_CRS: // Course
fprintf (Gbl.F.Out,"%s %s",
Txt_Course,Gbl.CurrentCrs.Crs.ShrtName);
break;
}
fprintf (Gbl.F.Out,"</div>");
/* Users' roles who can answer the game */
fprintf (Gbl.F.Out,"<div class=\"%s\">%s:<br />",
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_Users);
Rol_WriteSelectorRoles (1 << Rol_STD |
1 << Rol_NET |
1 << Rol_TCH,
Game.Roles,
true,false);
fprintf (Gbl.F.Out,"</div>");
/* Groups whose users can answer this game */
if (Game.Scope == Sco_SCOPE_CRS)
if (Gbl.CurrentCrs.Grps.NumGrps)
2017-09-07 18:38:18 +02:00
Gam_GetAndWriteNamesOfGrpsAssociatedToGame (&Game);
2017-07-09 20:31:40 +02:00
/* Text of the game */
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
fprintf (Gbl.F.Out,"<p class=\"%s\">"
"%s"
"</p>"
"</td>"
"</tr>",
Game.Status.Visible ? "DAT" :
2017-09-13 16:24:29 +02:00
"DAT_LIGHT",
2017-07-09 20:31:40 +02:00
Txt);
/***** Write questions of this game *****/
2017-09-13 16:24:29 +02:00
if (ListGameQuestions)
2017-07-09 20:31:40 +02:00
{
fprintf (Gbl.F.Out,"<tr>"
"<td colspan=\"5\">");
2017-09-07 18:38:18 +02:00
Gam_ListGameQuestions (&Game);
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out,"</td>"
"</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;
/***** Put big button to start playing *****/
if (PutButtonToStart)
Gam_PutBigButtonToStartGame ();
2017-07-09 20:31:40 +02:00
2017-09-13 16:24:29 +02:00
/***** End box *****/
if (ShowOnlyThisGame)
2017-07-09 20:31:40 +02:00
Box_EndBox ();
}
/*****************************************************************************/
/*********************** 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);
}
/*****************************************************************************/
/************************ Write status of a game ***************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_WriteStatus (struct Game *Game)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_Hidden_game;
extern const char *Txt_Visible_game;
extern const char *Txt_Closed_game;
extern const char *Txt_Open_game;
extern const char *Txt_SURVEY_Type_of_user_not_allowed;
extern const char *Txt_SURVEY_Type_of_user_allowed;
extern const char *Txt_GAME_You_belong_to_the_scope_of_the_game;
extern const char *Txt_GAME_You_dont_belong_to_the_scope_of_the_game;
extern const char *Txt_SURVEY_You_have_already_answered;
extern const char *Txt_SURVEY_You_have_not_answered;
/***** Start list with items of status *****/
fprintf (Gbl.F.Out,"<ul>");
/* Write whether game is visible or hidden */
if (Game->Status.Visible)
fprintf (Gbl.F.Out,"<li class=\"STATUS_GREEN\">%s</li>",
Txt_Visible_game);
else
fprintf (Gbl.F.Out,"<li class=\"STATUS_RED_LIGHT\">%s</li>",
Txt_Hidden_game);
/* Write whether game is open or closed */
if (Game->Status.Open)
fprintf (Gbl.F.Out,"<li class=\"%s\">%s</li>",
Game->Status.Visible ? "STATUS_GREEN" :
"STATUS_GREEN_LIGHT",
Txt_Open_game);
else
fprintf (Gbl.F.Out,"<li class=\"%s\">%s</li>",
Game->Status.Visible ? "STATUS_RED" :
"STATUS_RED_LIGHT",
Txt_Closed_game);
/* Write whether game can be answered by me or not depending on user type */
if (Game->Status.IAmLoggedWithAValidRoleToAnswer)
fprintf (Gbl.F.Out,"<li class=\"%s\">%s</li>",
Game->Status.Visible ? "STATUS_GREEN" :
"STATUS_GREEN_LIGHT",
Txt_SURVEY_Type_of_user_allowed);
else
fprintf (Gbl.F.Out,"<li class=\"%s\">%s</li>",
Game->Status.Visible ? "STATUS_RED" :
"STATUS_RED_LIGHT",
Txt_SURVEY_Type_of_user_not_allowed);
/* Write whether game can be answered by me or not depending on groups */
if (Game->Status.IBelongToScope)
fprintf (Gbl.F.Out,"<li class=\"%s\">%s</li>",
Game->Status.Visible ? "STATUS_GREEN" :
"STATUS_GREEN_LIGHT",
Txt_GAME_You_belong_to_the_scope_of_the_game);
else
fprintf (Gbl.F.Out,"<li class=\"%s\">%s</li>",
Game->Status.Visible ? "STATUS_RED" :
"STATUS_RED_LIGHT",
Txt_GAME_You_dont_belong_to_the_scope_of_the_game);
/* Write whether game has been already answered by me or not */
if (Game->Status.IHaveAnswered)
fprintf (Gbl.F.Out,"<li class=\"%s\">%s</li>",
Game->Status.Visible ? "STATUS_GREEN" :
"STATUS_GREEN_LIGHT",
Txt_SURVEY_You_have_already_answered);
else
fprintf (Gbl.F.Out,"<li class=\"%s\">%s</li>",
Game->Status.Visible ? "STATUS_RED" :
"STATUS_RED_LIGHT",
Txt_SURVEY_You_have_not_answered);
/***** End list with items of status *****/
fprintf (Gbl.F.Out,"</ul>");
}
/*****************************************************************************/
/********* Get parameter with the type or order in list of games ***********/
/*****************************************************************************/
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-07-09 20:31:40 +02:00
Par_GetParToUnsignedLong ("Order",
0,
2017-09-07 18:38:18 +02:00
Gam_NUM_ORDERS - 1,
(unsigned long) Gam_ORDER_DEFAULT);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/***** Put a hidden parameter with the type of order in list of games ******/
/*****************************************************************************/
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
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_PutFormsToRemEditOneGame (long GamCod,bool Visible)
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-01 14:36:25 +02:00
Gbl.Games.CurrentGamCod = GamCod; // Used as parameter in contextual links
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 *****/
2017-09-07 18:38:18 +02:00
Lay_PutContextualLink (ActReqRstGam,NULL,Gam_PutParams,
2017-07-09 20:31:40 +02:00
"recycle64x64.png",
Txt_Reset,NULL,
NULL);
2017-09-13 12:25:45 +02:00
/***** Put icon to hide/show game *****/
2017-07-09 20:31:40 +02:00
if (Visible)
2017-09-07 18:38:18 +02:00
Ico_PutContextualIconToHide (ActHidGam,Gam_PutParams);
2017-07-09 20:31:40 +02:00
else
2017-09-07 18:38:18 +02:00
Ico_PutContextualIconToUnhide (ActShoGam,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-09-13 12:25:45 +02:00
/***** Put icon to play game *****/
Lay_PutContextualLink (ActPlyGam,NULL,Gam_PutParams,
"play64x64.png",
Txt_Play,NULL,
NULL);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/********************** Params used to edit a game *************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_PutParams (void)
2017-07-09 20:31:40 +02:00
{
2017-09-01 14:36:25 +02:00
if (Gbl.Games.CurrentGamCod > 0)
2017-09-07 18:38:18 +02:00
Gam_PutParamGameCod (Gbl.Games.CurrentGamCod);
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
}
/*****************************************************************************/
/*********************** 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
{
extern const char *Sco_ScopeDB[Sco_NUM_SCOPES];
char SubQuery[Sco_NUM_SCOPES][256];
char OrderBySubQuery[256];
char Query[2048];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRows;
unsigned NumGame;
unsigned ScopesAllowed = 0;
unsigned HiddenAllowed = 0;
long Cods[Sco_NUM_SCOPES];
Sco_Scope_t Scope;
bool SubQueryFilled;
/***** 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
/***** Set allowed and hidden scopes to get list depending on my user's role *****/
2017-09-07 18:38:18 +02:00
Gam_SetAllowedAndHiddenScopes (&ScopesAllowed,&HiddenAllowed);
2017-07-09 20:31:40 +02:00
/***** Get list of games from database *****/
Cods[Sco_SCOPE_SYS] = -1L; // System
Cods[Sco_SCOPE_CTY] = Gbl.CurrentCty.Cty.CtyCod; // Country
Cods[Sco_SCOPE_INS] = Gbl.CurrentIns.Ins.InsCod; // Institution
Cods[Sco_SCOPE_CTR] = Gbl.CurrentCtr.Ctr.CtrCod; // Centre
Cods[Sco_SCOPE_DEG] = Gbl.CurrentDeg.Deg.DegCod; // Degree
Cods[Sco_SCOPE_CRS] = Gbl.CurrentCrs.Crs.CrsCod; // Course
/* Fill subqueries for system, country, institution, centre and degree */
for (Scope = Sco_SCOPE_SYS, SubQueryFilled = false;
Scope <= Sco_SCOPE_DEG;
Scope++)
if (ScopesAllowed & 1 << Scope)
{
sprintf (SubQuery[Scope],"%s(Scope='%s' AND Cod=%ld%s)",
SubQueryFilled ? " OR " :
"",
Sco_ScopeDB[Scope],Cods[Scope],
(HiddenAllowed & 1 << Scope) ? "" :
" AND Hidden='N'");
SubQueryFilled = true;
}
else
SubQuery[Scope][0] = '\0';
/* Fill subquery for course */
if (ScopesAllowed & 1 << Sco_SCOPE_CRS)
{
if (Gbl.CurrentCrs.Grps.WhichGrps == Grp_ONLY_MY_GROUPS)
sprintf (SubQuery[Sco_SCOPE_CRS],"%s("
"Scope='%s' AND Cod=%ld%s"
" AND "
"(GamCod NOT IN"
" (SELECT GamCod FROM gam_grp)"
" OR"
" GamCod IN"
" (SELECT gam_grp.GamCod"
" FROM gam_grp,crs_grp_usr"
" WHERE crs_grp_usr.UsrCod=%ld"
" AND gam_grp.GrpCod=crs_grp_usr.GrpCod))"
")",
SubQueryFilled ? " OR " :
"",
Sco_ScopeDB[Sco_SCOPE_CRS],Cods[Sco_SCOPE_CRS],
(HiddenAllowed & 1 << Sco_SCOPE_CRS) ? "" :
" AND Hidden='N'",
Gbl.Usrs.Me.UsrDat.UsrCod);
else // Gbl.CurrentCrs.Grps.WhichGrps == Grp_ALL_GROUPS
sprintf (SubQuery[Sco_SCOPE_CRS],"%s(Scope='%s' AND Cod=%ld%s)",
SubQueryFilled ? " OR " :
"",
Sco_ScopeDB[Sco_SCOPE_CRS],Cods[Sco_SCOPE_CRS],
(HiddenAllowed & 1 << Sco_SCOPE_CRS) ? "" :
" AND Hidden='N'");
SubQueryFilled = true;
}
else
SubQuery[Sco_SCOPE_CRS][0] = '\0';
/* Build query */
if (SubQueryFilled)
{
switch (Gbl.Games.SelectedOrder)
{
2017-09-07 18:38:18 +02:00
case Gam_ORDER_BY_START_DATE:
2017-07-09 20:31:40 +02:00
sprintf (OrderBySubQuery,"StartTime DESC,EndTime DESC,Title DESC");
break;
2017-09-07 18:38:18 +02:00
case Gam_ORDER_BY_END_DATE:
2017-07-09 20:31:40 +02:00
sprintf (OrderBySubQuery,"EndTime DESC,StartTime DESC,Title DESC");
break;
}
sprintf (Query,"SELECT GamCod FROM games"
" WHERE %s%s%s%s%s%s"
" ORDER BY %s",
SubQuery[Sco_SCOPE_SYS],
SubQuery[Sco_SCOPE_CTY],
SubQuery[Sco_SCOPE_INS],
SubQuery[Sco_SCOPE_CTR],
SubQuery[Sco_SCOPE_DEG],
SubQuery[Sco_SCOPE_CRS],
OrderBySubQuery);
}
else
Lay_ShowErrorAndExit ("Can not get list of games.");
/* Make query */
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get games");
if (NumRows) // Games found...
{
Gbl.Games.Num = (unsigned) NumRows;
/***** Create list of games *****/
if ((Gbl.Games.LstGamCods = (long *) calloc (NumRows,sizeof (long))) == NULL)
Lay_ShowErrorAndExit ("Not enough memory to store list of games.");
/***** 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;
}
/*****************************************************************************/
/*** Set allowed and hidden scopes to get list depending on my user's role ***/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_SetAllowedAndHiddenScopes (unsigned *ScopesAllowed,
2017-07-09 20:31:40 +02:00
unsigned *HiddenAllowed)
{
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_UNK: // User not logged in *********************************
*ScopesAllowed = 0;
*HiddenAllowed = 0;
break;
case Rol_GST: // User not belonging to any course *******************
*ScopesAllowed = 1 << Sco_SCOPE_SYS;
*HiddenAllowed = 0;
break;
case Rol_USR: // Student or teacher in other courses...
// ...but not belonging to the current course *********
*ScopesAllowed = 1 << Sco_SCOPE_SYS;
*HiddenAllowed = 0;
if (Usr_CheckIfIBelongToCty (Gbl.CurrentCty.Cty.CtyCod))
{
*ScopesAllowed |= 1 << Sco_SCOPE_CTY;
if (Usr_CheckIfIBelongToIns (Gbl.CurrentIns.Ins.InsCod))
{
*ScopesAllowed |= 1 << Sco_SCOPE_INS;
if (Usr_CheckIfIBelongToCtr (Gbl.CurrentCtr.Ctr.CtrCod))
{
*ScopesAllowed |= 1 << Sco_SCOPE_CTR;
if (Usr_CheckIfIBelongToDeg (Gbl.CurrentDeg.Deg.DegCod))
*ScopesAllowed |= 1 << Sco_SCOPE_DEG;
}
}
}
break;
case Rol_STD: // Student in current course **************************
*ScopesAllowed = 1 << Sco_SCOPE_SYS;
*HiddenAllowed = 0;
if (Usr_CheckIfIBelongToCty (Gbl.CurrentCty.Cty.CtyCod))
{
*ScopesAllowed |= 1 << Sco_SCOPE_CTY;
if (Usr_CheckIfIBelongToIns (Gbl.CurrentIns.Ins.InsCod))
{
*ScopesAllowed |= 1 << Sco_SCOPE_INS;
if (Usr_CheckIfIBelongToCtr (Gbl.CurrentCtr.Ctr.CtrCod))
{
*ScopesAllowed |= 1 << Sco_SCOPE_CTR;
if (Usr_CheckIfIBelongToDeg (Gbl.CurrentDeg.Deg.DegCod))
{
*ScopesAllowed |= 1 << Sco_SCOPE_DEG;
if (Gbl.Usrs.Me.IBelongToCurrentCrs)
*ScopesAllowed |= 1 << Sco_SCOPE_CRS;
}
}
}
}
break;
case Rol_NET: // Non-editing teacher in current course **************
case Rol_TCH: // Teacher in current course **************************
*ScopesAllowed = 1 << Sco_SCOPE_SYS;
*HiddenAllowed = 0;
if (Usr_CheckIfIBelongToCty (Gbl.CurrentCty.Cty.CtyCod))
{
*ScopesAllowed |= 1 << Sco_SCOPE_CTY;
if (Usr_CheckIfIBelongToIns (Gbl.CurrentIns.Ins.InsCod))
{
*ScopesAllowed |= 1 << Sco_SCOPE_INS;
if (Usr_CheckIfIBelongToCtr (Gbl.CurrentCtr.Ctr.CtrCod))
{
*ScopesAllowed |= 1 << Sco_SCOPE_CTR;
if (Usr_CheckIfIBelongToDeg (Gbl.CurrentDeg.Deg.DegCod))
{
*ScopesAllowed |= 1 << Sco_SCOPE_DEG;
if (Gbl.Usrs.Me.IBelongToCurrentCrs)
{
*ScopesAllowed |= 1 << Sco_SCOPE_CRS;
*HiddenAllowed |= 1 << Sco_SCOPE_CRS; // A non-editing teacher or teacher can view hidden course games
}
}
}
}
}
break;
case Rol_DEG_ADM: // Degree administrator *******************************
*ScopesAllowed = 1 << Sco_SCOPE_SYS;
*HiddenAllowed = 0;
if (Gbl.CurrentCty.Cty.CtyCod > 0) // Country selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_CTY;
if (Gbl.CurrentIns.Ins.InsCod > 0) // Institution selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_INS;
if (Gbl.CurrentCtr.Ctr.CtrCod > 0) // Centre selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_CTR;
if (Gbl.CurrentDeg.Deg.DegCod > 0) // Degree selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_DEG;
*HiddenAllowed |= 1 << Sco_SCOPE_DEG; // A degree admin can view hidden degree games
}
}
}
}
break;
case Rol_CTR_ADM: // Centre administrator *******************************
*ScopesAllowed = 1 << Sco_SCOPE_SYS;
*HiddenAllowed = 0;
if (Gbl.CurrentCty.Cty.CtyCod > 0) // Country selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_CTY;
if (Gbl.CurrentIns.Ins.InsCod > 0) // Institution selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_INS;
if (Gbl.CurrentCtr.Ctr.CtrCod > 0) // Centre selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_CTR;
*HiddenAllowed |= 1 << Sco_SCOPE_CTR; // A centre admin can view hidden centre games
}
}
}
break;
case Rol_INS_ADM: // Institution administrator **************************
*ScopesAllowed = 1 << Sco_SCOPE_SYS;
*HiddenAllowed = 0;
if (Gbl.CurrentCty.Cty.CtyCod > 0) // Country selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_CTY;
if (Gbl.CurrentIns.Ins.InsCod > 0) // Institution selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_INS;
*HiddenAllowed |= 1 << Sco_SCOPE_INS; // An institution admin can view hidden institution games
}
}
break;
case Rol_SYS_ADM: // System administrator (superuser) *******************
*ScopesAllowed = 1 << Sco_SCOPE_SYS;
*HiddenAllowed = 1 << Sco_SCOPE_SYS; // A system admin can view hidden system games
if (Gbl.CurrentCty.Cty.CtyCod > 0) // Country selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_CTY;
*HiddenAllowed |= 1 << Sco_SCOPE_CTY; // A system admin can view hidden country games
if (Gbl.CurrentIns.Ins.InsCod > 0) // Institution selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_INS;
*HiddenAllowed |= 1 << Sco_SCOPE_INS; // A system admin can view hidden institution games
if (Gbl.CurrentCtr.Ctr.CtrCod > 0) // Centre selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_CTR;
*HiddenAllowed |= 1 << Sco_SCOPE_CTR; // A system admin can view hidden centre games
if (Gbl.CurrentDeg.Deg.DegCod > 0) // Degree selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_DEG;
*HiddenAllowed |= 1 << Sco_SCOPE_DEG; // A system admin can view hidden degree games
if (Gbl.CurrentCrs.Crs.CrsCod > 0) // Course selected
{
*ScopesAllowed |= 1 << Sco_SCOPE_CRS;
*HiddenAllowed |= 1 << Sco_SCOPE_CRS; // A system admin can view hidden course games
}
}
}
}
}
break;
}
}
/*****************************************************************************/
/********************* Get game data using its code ************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_GetDataOfGameByCod (struct Game *Game)
2017-07-09 20:31:40 +02:00
{
char Query[1024];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRows;
/***** Build query *****/
sprintf (Query,"SELECT GamCod,Scope,Cod,Hidden,Roles,UsrCod,"
"UNIX_TIMESTAMP(StartTime),"
"UNIX_TIMESTAMP(EndTime),"
"NOW() BETWEEN StartTime AND EndTime,"
"Title"
" FROM games"
" WHERE GamCod=%ld",
Game->GamCod);
/***** Get data of game from database *****/
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get game data");
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]);
/* Get game scope (row[1]) */
if ((Game->Scope = Sco_GetScopeFromDBStr (row[1])) == Sco_SCOPE_UNK)
Lay_ShowErrorAndExit ("Wrong game scope.");
/* Get code of the country, institution, centre, degree or course (row[2]) */
Game->Cod = Str_ConvertStrCodToLongCod (row[2]);
/* Get whether the game is hidden (row[3]) */
Game->Status.Visible = (row[3][0] == 'N');
/* Get roles (row[4]) */
if (sscanf (row[4],"%u",&Game->Roles) != 1)
Lay_ShowErrorAndExit ("Error when reading roles of game.");
/* Get author of the game (row[5]) */
Game->UsrCod = Str_ConvertStrCodToLongCod (row[5]);
/* Get start date (row[6] holds the start UTC time) */
Game->TimeUTC[Att_START_TIME] = Dat_GetUNIXTimeFromStr (row[6]);
/* Get end date (row[7] holds the end UTC time) */
Game->TimeUTC[Att_END_TIME ] = Dat_GetUNIXTimeFromStr (row[7]);
/* Get whether the game is open or closed (row(8)) */
Game->Status.Open = (row[8][0] == '1');
/* Get the title of the game (row[9]) */
Str_Copy (Game->Title,row[9],
2017-09-07 18:38:18 +02:00
Gam_MAX_BYTES_SURVEY_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
/* Am I logged with a valid role to answer this game? */
Game->Status.IAmLoggedWithAValidRoleToAnswer = (Game->Roles & (1 << Gbl.Usrs.Me.Role.Logged));
/* Do I belong to valid groups to answer this game? */
switch (Game->Scope)
{
case Sco_SCOPE_UNK: // Unknown
Lay_ShowErrorAndExit ("Wrong game scope.");
break;
case Sco_SCOPE_SYS: // System
Game->Status.IBelongToScope = Gbl.Usrs.Me.Logged;
break;
case Sco_SCOPE_CTY: // Country
Game->Status.IBelongToScope = Usr_CheckIfIBelongToCty (Game->Cod);
break;
case Sco_SCOPE_INS: // Institution
Game->Status.IBelongToScope = Usr_CheckIfIBelongToIns (Game->Cod);
break;
case Sco_SCOPE_CTR: // Centre
Game->Status.IBelongToScope = Usr_CheckIfIBelongToCtr (Game->Cod);
break;
case Sco_SCOPE_DEG: // Degree
Game->Status.IBelongToScope = Usr_CheckIfIBelongToDeg (Game->Cod);
break;
case Sco_SCOPE_CRS: // Course
Game->Status.IBelongToScope = Usr_CheckIfIBelongToCrs (Game->Cod) &&
2017-09-07 18:38:18 +02:00
Gam_CheckIfICanDoThisGameBasedOnGrps (Game->GamCod);
2017-07-09 20:31:40 +02:00
break;
}
/* 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 answer game? */
Game->Status.ICanAnswer = (Game->NumQsts != 0) &&
Game->Status.Visible &&
Game->Status.Open &&
Game->Status.IAmLoggedWithAValidRoleToAnswer &&
Game->Status.IBelongToScope &&
!Game->Status.IHaveAnswered;
/* Can I view results of the game?
Can I edit game? */
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_STD:
Game->Status.ICanViewResults = (Game->Scope == Sco_SCOPE_CRS ||
Game->Scope == Sco_SCOPE_DEG ||
Game->Scope == Sco_SCOPE_CTR ||
Game->Scope == Sco_SCOPE_INS ||
Game->Scope == Sco_SCOPE_CTY ||
Game->Scope == Sco_SCOPE_SYS) &&
(Game->NumQsts != 0) &&
Game->Status.Visible &&
Game->Status.Open &&
Game->Status.IAmLoggedWithAValidRoleToAnswer &&
Game->Status.IBelongToScope &&
Game->Status.IHaveAnswered;
Game->Status.ICanEdit = false;
break;
case Rol_NET:
Game->Status.ICanViewResults = (Game->Scope == Sco_SCOPE_CRS ||
Game->Scope == Sco_SCOPE_DEG ||
Game->Scope == Sco_SCOPE_CTR ||
Game->Scope == Sco_SCOPE_INS ||
Game->Scope == Sco_SCOPE_CTY ||
Game->Scope == Sco_SCOPE_SYS) &&
Game->NumQsts != 0 &&
!Game->Status.ICanAnswer;
Game->Status.ICanEdit = false;
break;
case Rol_TCH:
Game->Status.ICanViewResults = (Game->Scope == Sco_SCOPE_CRS ||
Game->Scope == Sco_SCOPE_DEG ||
Game->Scope == Sco_SCOPE_CTR ||
Game->Scope == Sco_SCOPE_INS ||
Game->Scope == Sco_SCOPE_CTY ||
Game->Scope == Sco_SCOPE_SYS) &&
Game->NumQsts != 0 &&
!Game->Status.ICanAnswer;
Game->Status.ICanEdit = Game->Scope == Sco_SCOPE_CRS &&
Game->Status.IBelongToScope;
break;
case Rol_DEG_ADM:
Game->Status.ICanViewResults = (Game->Scope == Sco_SCOPE_DEG ||
Game->Scope == Sco_SCOPE_CTR ||
Game->Scope == Sco_SCOPE_INS ||
Game->Scope == Sco_SCOPE_CTY ||
Game->Scope == Sco_SCOPE_SYS) &&
(Game->NumQsts != 0) &&
!Game->Status.ICanAnswer;
Game->Status.ICanEdit = Game->Scope == Sco_SCOPE_DEG &&
Game->Status.IBelongToScope;
break;
case Rol_CTR_ADM:
Game->Status.ICanViewResults = (Game->Scope == Sco_SCOPE_CTR ||
Game->Scope == Sco_SCOPE_INS ||
Game->Scope == Sco_SCOPE_CTY ||
Game->Scope == Sco_SCOPE_SYS) &&
(Game->NumQsts != 0) &&
!Game->Status.ICanAnswer;
Game->Status.ICanEdit = Game->Scope == Sco_SCOPE_CTR &&
Game->Status.IBelongToScope;
break;
case Rol_INS_ADM:
Game->Status.ICanViewResults = (Game->Scope == Sco_SCOPE_INS ||
Game->Scope == Sco_SCOPE_CTY ||
Game->Scope == Sco_SCOPE_SYS) &&
(Game->NumQsts != 0) &&
!Game->Status.ICanAnswer;
Game->Status.ICanEdit = Game->Scope == Sco_SCOPE_INS &&
Game->Status.IBelongToScope;
break;
case Rol_SYS_ADM:
Game->Status.ICanViewResults = (Game->NumQsts != 0);
Game->Status.ICanEdit = true;
break;
default:
Game->Status.ICanViewResults = false;
Game->Status.ICanEdit = false;
break;
}
}
else
{
/* Initialize to empty game */
Game->GamCod = -1L;
Game->Scope = Sco_SCOPE_UNK;
Game->Roles = 0;
Game->UsrCod = -1L;
2017-09-07 18:38:18 +02:00
Game->TimeUTC[Gam_START_TIME] =
Game->TimeUTC[Gam_END_TIME ] = (time_t) 0;
2017-07-09 20:31:40 +02:00
Game->Title[0] = '\0';
Game->NumQsts = 0;
Game->NumUsrs = 0;
Game->Status.Visible = true;
Game->Status.Open = false;
Game->Status.IAmLoggedWithAValidRoleToAnswer = false;
Game->Status.IBelongToScope = false;
Game->Status.IHaveAnswered = false;
Game->Status.ICanAnswer = false;
Game->Status.ICanViewResults = false;
Game->Status.ICanEdit = false;
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
/*****************************************************************************/
/**************************** Free list of games ***************************/
/*****************************************************************************/
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
{
char Query[128];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRows;
/***** Get text of game from database *****/
sprintf (Query,"SELECT Txt FROM games WHERE GamCod=%ld",GamCod);
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get game text");
/***** 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.");
}
/*****************************************************************************/
/******************* Write parameter with code of game *********************/
/*****************************************************************************/
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);
}
/*****************************************************************************/
/******************** Get parameter with code of game **********************/
/*****************************************************************************/
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");
}
/*****************************************************************************/
/*************** Ask for confirmation of removing of a game ****************/
/*****************************************************************************/
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 *****/
2017-09-01 14:36:25 +02:00
Gbl.Games.CurrentGamCod = Game.GamCod;
2017-07-09 20:31:40 +02:00
sprintf (Gbl.Alert.Txt,Txt_Do_you_really_want_to_remove_the_game_X,
Game.Title);
Ale_ShowAlertAndButton (Ale_QUESTION,Gbl.Alert.Txt,
2017-09-07 18:38:18 +02:00
ActRemGam,NULL,NULL,Gam_PutParams,
2017-07-09 20:31:40 +02:00
Btn_REMOVE_BUTTON,Txt_Remove_game);
/***** Show games again *****/
2017-09-07 18:38:18 +02:00
Gam_ListAllGames ();
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/****************************** Remove a game ******************************/
/*****************************************************************************/
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;
char Query[512];
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 *****/
2017-07-16 13:54:11 +02:00
sprintf (Query,"DELETE FROM gam_users WHERE GamCod=%ld",
2017-07-09 20:31:40 +02:00
Game.GamCod);
DB_QueryDELETE (Query,"can not remove users who are answered a game");
/***** Remove all the questions in this game *****/
2017-07-16 13:54:11 +02:00
sprintf (Query,"DELETE FROM gam_questions"
2017-07-09 20:31:40 +02:00
" WHERE GamCod=%ld",
Game.GamCod);
DB_QueryDELETE (Query,"can not remove questions of a game");
/***** Remove all the groups of this game *****/
2017-09-07 18:38:18 +02:00
Gam_RemoveAllTheGrpsAssociatedToAndGame (Game.GamCod);
2017-07-09 20:31:40 +02:00
/***** Remove game *****/
sprintf (Query,"DELETE FROM games WHERE GamCod=%ld",
Game.GamCod);
DB_QueryDELETE (Query,"can not remove game");
/***** Write message to show the change made *****/
sprintf (Gbl.Alert.Txt,Txt_Game_X_removed,
Game.Title);
Ale_ShowAlert (Ale_SUCCESS,Gbl.Alert.Txt);
/***** 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;
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 *****/
sprintf (Gbl.Alert.Txt,Txt_Do_you_really_want_to_reset_the_game_X,
Game.Title);
Ale_ShowAlert (Ale_WARNING,Gbl.Alert.Txt);
/***** Button of confirmation of reset *****/
2017-09-01 14:36:25 +02:00
Gbl.Games.CurrentGamCod = Game.GamCod;
2017-09-07 18:38:18 +02:00
Gam_PutButtonToResetGame ();
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
}
/*****************************************************************************/
/************************* Put button to reset game ************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_PutButtonToResetGame (void)
2017-07-09 20:31:40 +02:00
{
extern const char *Txt_Reset_game;
Act_FormStart (ActRstGam);
2017-09-07 18:38:18 +02:00
Gam_PutParams ();
2017-07-09 20:31:40 +02:00
Btn_PutRemoveButton (Txt_Reset_game);
Act_FormEnd ();
}
/*****************************************************************************/
/******************************* 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;
char Query[512];
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 *****/
2017-07-16 13:54:11 +02:00
sprintf (Query,"DELETE FROM gam_users WHERE GamCod=%ld",
2017-07-09 20:31:40 +02:00
Game.GamCod);
DB_QueryDELETE (Query,"can not remove users who are answered a game");
/***** Reset all the answers in this game *****/
2017-07-16 13:54:11 +02:00
sprintf (Query,"UPDATE gam_answers,gam_questions SET gam_answers.NumUsrs=0"
" WHERE gam_questions.GamCod=%ld"
" AND gam_questions.QstCod=gam_answers.QstCod",
2017-07-09 20:31:40 +02:00
Game.GamCod);
DB_QueryUPDATE (Query,"can not reset answers of a game");
/***** Write message to show the change made *****/
sprintf (Gbl.Alert.Txt,Txt_Game_X_reset,
Game.Title);
Ale_ShowAlert (Ale_SUCCESS,Gbl.Alert.Txt);
/***** 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
{
extern const char *Txt_Game_X_is_now_hidden;
char Query[128];
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 *****/
sprintf (Query,"UPDATE games SET Hidden='Y' WHERE GamCod=%ld",
Game.GamCod);
DB_QueryUPDATE (Query,"can not hide game");
/***** Write message to show the change made *****/
sprintf (Gbl.Alert.Txt,Txt_Game_X_is_now_hidden,
Game.Title);
Ale_ShowAlert (Ale_SUCCESS,Gbl.Alert.Txt);
/***** 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
{
extern const char *Txt_Game_X_is_now_visible;
char Query[128];
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 *****/
sprintf (Query,"UPDATE games SET Hidden='N' WHERE GamCod=%ld",
Game.GamCod);
DB_QueryUPDATE (Query,"can not show game");
/***** Write message to show the change made *****/
sprintf (Gbl.Alert.Txt,Txt_Game_X_is_now_visible,
Game.Title);
Ale_ShowAlert (Ale_SUCCESS,Gbl.Alert.Txt);
/***** 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
{
extern const char *Sco_ScopeDB[Sco_NUM_SCOPES];
2017-09-07 18:38:18 +02:00
char Query[512 + Gam_MAX_BYTES_SURVEY_TITLE];
2017-07-09 20:31:40 +02:00
/***** Get number of games with a field value from database *****/
sprintf (Query,"SELECT COUNT(*) FROM games"
" WHERE Scope='%s' AND Cod=%ld"
" AND Title='%s' AND GamCod<>%ld",
Sco_ScopeDB[Game->Scope],Game->Cod,
Game->Title,Game->GamCod);
return (DB_QueryCOUNT (Query,"can not get similar games") != 0);
}
/*****************************************************************************/
/********************* 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;
extern const char *The_ClassForm[The_NUM_THEMES];
extern const char *Txt_New_game;
extern const char *Txt_Scope;
extern const char *Txt_Edit_game;
extern const char *Txt_Title;
extern const char *Txt_Description;
extern const char *Txt_Users;
extern const char *Txt_Create_game;
extern const char *Txt_Save;
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.Scope = Sco_SCOPE_UNK;
Game.Roles = (1 << Rol_STD);
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
2017-07-09 20:31:40 +02:00
Game.Title[0] = '\0';
Game.NumQsts = 0;
Game.NumUsrs = 0;
Game.Status.Visible = true;
Game.Status.Open = true;
Game.Status.IAmLoggedWithAValidRoleToAnswer = false;
Game.Status.IBelongToScope = false;
Game.Status.IHaveAnswered = false;
Game.Status.ICanAnswer = false;
Game.Status.ICanViewResults = false;
}
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 *****/
2017-09-01 14:36:25 +02:00
Gbl.Games.CurrentGamCod = Game.GamCod;
2017-07-09 20:31:40 +02:00
Act_FormStart (ItsANewGame ? ActNewGam :
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
Box_StartBoxTable (NULL,Txt_Edit_game,NULL,
Hlp_ASSESSMENT_Games_edit_game,Box_NOT_CLOSABLE,2);
/***** Scope of the game *****/
fprintf (Gbl.F.Out,"<tr>"
"<td class=\"RIGHT_MIDDLE\">"
"<label for=\"ScopeGame\" class=\"%s\">%s:</label>"
"</td>"
"<td class=\"LEFT_MIDDLE\">",
The_ClassForm[Gbl.Prefs.Theme],
Txt_Scope);
2017-09-07 18:38:18 +02:00
Gam_SetDefaultAndAllowedScope (&Game);
2017-07-09 20:31:40 +02:00
Sco_GetScope ("ScopeGame");
Sco_PutSelectorScope ("ScopeGame",false);
fprintf (Gbl.F.Out,"</td>"
"</tr>");
/***** 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>",
The_ClassForm[Gbl.Prefs.Theme],
Txt_Title,
2017-09-07 18:38:18 +02:00
Gam_MAX_CHARS_SURVEY_TITLE,Game.Title);
2017-07-09 20:31:40 +02:00
/***** Game start and end dates *****/
Dat_PutFormStartEndClientLocalDateTimes (Game.TimeUTC,Dat_FORM_SECONDS_ON);
/***** 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\">",
The_ClassForm[Gbl.Prefs.Theme],
Txt_Description);
if (!ItsANewGame)
fprintf (Gbl.F.Out,"%s",Txt);
fprintf (Gbl.F.Out,"</textarea>"
"</td>"
"</tr>");
/***** Users' roles who can answer the game *****/
fprintf (Gbl.F.Out,"<tr>"
"<td class=\"RIGHT_TOP %s\">%s:"
"</td>"
"<td class=\"DAT LEFT_MIDDLE\">",
The_ClassForm[Gbl.Prefs.Theme],
Txt_Users);
Rol_WriteSelectorRoles (1 << Rol_STD |
1 << Rol_NET |
1 << Rol_TCH,
Game.Roles,
false,false);
fprintf (Gbl.F.Out,"</td>"
"</tr>");
/***** Groups *****/
2017-09-07 18:38:18 +02:00
Gam_ShowLstGrpsToEditGame (Game.GamCod);
2017-07-09 20:31:40 +02:00
/***** End table, send button and end box *****/
if (ItsANewGame)
Box_EndBoxTableWithButton (Btn_CREATE_BUTTON,Txt_Create_game);
else
Box_EndBoxTableWithButton (Btn_CONFIRM_BUTTON,Txt_Save);
/***** End form *****/
Act_FormEnd ();
/***** 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
}
/*****************************************************************************/
/****** Set default and allowed scopes depending on logged user's role *******/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_SetDefaultAndAllowedScope (struct Game *Game)
2017-07-09 20:31:40 +02:00
{
bool ICanEdit = false;
/***** Set default scope *****/
Gbl.Scope.Default = Sco_SCOPE_UNK;
Gbl.Scope.Allowed = 0;
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_TCH: // Teachers only can edit course games
if (Gbl.CurrentCrs.Crs.CrsCod > 0)
{
if (Game->Scope == Sco_SCOPE_UNK) // Scope not defined
Game->Scope = Sco_SCOPE_CRS;
if (Game->Scope == Sco_SCOPE_CRS)
{
Gbl.Scope.Default = Game->Scope;
Gbl.Scope.Allowed = 1 << Sco_SCOPE_CRS;
ICanEdit = true;
}
}
break;
case Rol_DEG_ADM: // Degree admins only can edit degree games
if (Game->Scope == Sco_SCOPE_UNK) // Scope not defined
Game->Scope = Sco_SCOPE_DEG;
if (Game->Scope == Sco_SCOPE_DEG)
{
Gbl.Scope.Default = Game->Scope;
Gbl.Scope.Allowed = 1 << Sco_SCOPE_DEG;
ICanEdit = true;
}
break;
case Rol_CTR_ADM: // Centre admins only can edit centre games
if (Game->Scope == Sco_SCOPE_UNK) // Scope not defined
Game->Scope = Sco_SCOPE_CTR;
if (Game->Scope == Sco_SCOPE_CTR)
{
Gbl.Scope.Default = Game->Scope;
Gbl.Scope.Allowed = 1 << Sco_SCOPE_CTR;
ICanEdit = true;
}
break;
case Rol_INS_ADM: // Institution admins only can edit institution games
if (Game->Scope == Sco_SCOPE_UNK) // Scope not defined
Game->Scope = Sco_SCOPE_INS;
if (Game->Scope == Sco_SCOPE_INS)
{
Gbl.Scope.Default = Game->Scope;
Gbl.Scope.Allowed = 1 << Sco_SCOPE_INS;
ICanEdit = true;
}
break;
case Rol_SYS_ADM:// System admins can edit any game
if (Game->Scope == Sco_SCOPE_UNK) // Scope not defined
{
if (Gbl.CurrentCrs.Crs.CrsCod > 0)
Game->Scope = Sco_SCOPE_CRS;
else if (Gbl.CurrentDeg.Deg.DegCod > 0)
Game->Scope = Sco_SCOPE_DEG;
else if (Gbl.CurrentCtr.Ctr.CtrCod > 0)
Game->Scope = Sco_SCOPE_CTR;
else if (Gbl.CurrentIns.Ins.InsCod > 0)
Game->Scope = Sco_SCOPE_INS;
else if (Gbl.CurrentCty.Cty.CtyCod > 0)
Game->Scope = Sco_SCOPE_CTY;
else
Game->Scope = Sco_SCOPE_SYS;
}
Gbl.Scope.Default = Game->Scope;
Gbl.Scope.Allowed = 1 << Sco_SCOPE_SYS |
1 << Sco_SCOPE_CTY |
1 << Sco_SCOPE_INS |
1 << Sco_SCOPE_CTR |
1 << Sco_SCOPE_DEG |
1 << Sco_SCOPE_CRS;
ICanEdit = true;
break;
default:
break;
}
if (!ICanEdit)
Lay_ShowErrorAndExit ("You don't have permission to edit games here.");
}
/*****************************************************************************/
/******************** Show list of groups to edit a game *******************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_ShowLstGrpsToEditGame (long GamCod)
2017-07-09 20:31:40 +02:00
{
extern const char *The_ClassForm[The_NUM_THEMES];
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);
if (Gbl.CurrentCrs.Grps.GrpTypes.Num)
{
/***** Start box and table *****/
fprintf (Gbl.F.Out,"<tr>"
"<td class=\"%s RIGHT_TOP\">"
"%s:"
"</td>"
"<td class=\"LEFT_TOP\">",
The_ClassForm[Gbl.Prefs.Theme],
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\""
" id=\"WholeCrs\" name=\"WholeCrs\" value=\"Y\"");
2017-09-07 18:38:18 +02:00
if (!Gam_CheckIfGamIsAssociatedToGrps (GamCod))
2017-07-09 20:31:40 +02:00
fprintf (Gbl.F.Out," checked=\"checked\"");
fprintf (Gbl.F.Out," onclick=\"uncheckChildren(this,'GrpCods')\" />"
"%s %s"
"</label>"
"</td>"
"</tr>",
Txt_The_whole_course,Gbl.CurrentCrs.Crs.ShrtName);
/***** List the groups for each group type *****/
for (NumGrpTyp = 0;
NumGrpTyp < Gbl.CurrentCrs.Grps.GrpTypes.Num;
NumGrpTyp++)
if (Gbl.CurrentCrs.Grps.GrpTypes.LstGrpTypes[NumGrpTyp].NumGrps)
Grp_ListGrpsToEditAsgAttSvyGam (&Gbl.CurrentCrs.Grps.GrpTypes.LstGrpTypes[NumGrpTyp],
GamCod,Grp_SURVEY);
/***** 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
if (ItsANewGame)
NewGame.Scope = Sco_SCOPE_UNK;
else
{
/* 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.");
NewGame.Scope = OldGame.Scope;
}
/***** Get scope *****/
2017-09-07 18:38:18 +02:00
Gam_SetDefaultAndAllowedScope (&NewGame);
2017-07-09 20:31:40 +02:00
Sco_GetScope ("ScopeGame");
switch (Gbl.Scope.Current)
{
case Sco_SCOPE_SYS:
if (Gbl.Usrs.Me.Role.Logged != Rol_SYS_ADM)
Lay_ShowErrorAndExit ("Wrong game scope.");
NewGame.Scope = Sco_SCOPE_SYS;
NewGame.Cod = -1L;
break;
case Sco_SCOPE_CTY:
if (Gbl.Usrs.Me.Role.Logged != Rol_SYS_ADM)
Lay_ShowErrorAndExit ("Wrong game scope.");
NewGame.Scope = Sco_SCOPE_CTY;
NewGame.Cod = Gbl.CurrentCty.Cty.CtyCod;
break;
case Sco_SCOPE_INS:
if (Gbl.Usrs.Me.Role.Logged != Rol_SYS_ADM &&
Gbl.Usrs.Me.Role.Logged != Rol_INS_ADM)
Lay_ShowErrorAndExit ("Wrong game scope.");
NewGame.Scope = Sco_SCOPE_INS;
NewGame.Cod = Gbl.CurrentIns.Ins.InsCod;
break;
case Sco_SCOPE_CTR:
if (Gbl.Usrs.Me.Role.Logged != Rol_SYS_ADM &&
Gbl.Usrs.Me.Role.Logged != Rol_CTR_ADM)
Lay_ShowErrorAndExit ("Wrong game scope.");
NewGame.Scope = Sco_SCOPE_CTR;
NewGame.Cod = Gbl.CurrentCtr.Ctr.CtrCod;
break;
case Sco_SCOPE_DEG:
if (Gbl.Usrs.Me.Role.Logged != Rol_SYS_ADM &&
Gbl.Usrs.Me.Role.Logged != Rol_DEG_ADM)
Lay_ShowErrorAndExit ("Wrong game scope.");
NewGame.Scope = Sco_SCOPE_DEG;
NewGame.Cod = Gbl.CurrentDeg.Deg.DegCod;
break;
case Sco_SCOPE_CRS:
if (Gbl.Usrs.Me.Role.Logged != Rol_SYS_ADM &&
Gbl.Usrs.Me.Role.Logged != Rol_TCH)
Lay_ShowErrorAndExit ("Wrong game scope.");
NewGame.Scope = Sco_SCOPE_CRS;
NewGame.Cod = Gbl.CurrentCrs.Crs.CrsCod;
break;
default:
Lay_ShowErrorAndExit ("Wrong scope.");
break;
}
/***** Get start/end date-times *****/
NewGame.TimeUTC[Dat_START_TIME] = Dat_GetTimeUTCFromForm ("StartTimeUTC");
NewGame.TimeUTC[Dat_END_TIME ] = Dat_GetTimeUTCFromForm ("EndTimeUTC" );
/***** Get game title *****/
2017-09-07 18:38:18 +02:00
Par_GetParToText ("Title",NewGame.Title,Gam_MAX_BYTES_SURVEY_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)
/***** Adjust dates *****/
2017-09-07 18:38:18 +02:00
if (NewGame.TimeUTC[Gam_START_TIME] == 0)
NewGame.TimeUTC[Gam_START_TIME] = Gbl.StartExecutionTimeUTC;
if (NewGame.TimeUTC[Gam_END_TIME] == 0)
NewGame.TimeUTC[Gam_END_TIME] = NewGame.TimeUTC[Gam_START_TIME] + 24 * 60 * 60; // +24 hours
2017-07-09 20:31:40 +02:00
/***** Get users who can answer this game *****/
NewGame.Roles = Rol_GetSelectedRoles ();
/***** 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;
sprintf (Gbl.Alert.Txt,Txt_Already_existed_a_game_with_the_title_X,
NewGame.Title);
Ale_ShowAlert (Ale_WARNING,Gbl.Alert.Txt);
}
}
else // If there is not a game title
{
NewGameIsCorrect = false;
Ale_ShowAlert (Ale_WARNING,Txt_You_must_specify_the_title_of_the_game);
}
/***** 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 *Sco_ScopeDB[Sco_NUM_SCOPES];
extern const char *Txt_Created_new_game_X;
char Query[1024 +
2017-09-07 18:38:18 +02:00
Gam_MAX_BYTES_SURVEY_TITLE +
2017-07-09 20:31:40 +02:00
Cns_MAX_BYTES_TEXT];
/***** Create a new game *****/
sprintf (Query,"INSERT INTO games"
" (Scope,Cod,Hidden,Roles,UsrCod,StartTime,EndTime,Title,Txt)"
" VALUES"
" ('%s',%ld,'N',%u,%ld,"
"FROM_UNIXTIME(%ld),FROM_UNIXTIME(%ld),"
"'%s','%s')",
Sco_ScopeDB[Game->Scope],Game->Cod,
Game->Roles,
Gbl.Usrs.Me.UsrDat.UsrCod,
2017-09-07 18:38:18 +02:00
Game->TimeUTC[Gam_START_TIME],
Game->TimeUTC[Gam_END_TIME ],
2017-07-09 20:31:40 +02:00
Game->Title,
Txt);
Game->GamCod = DB_QueryINSERTandReturnCode (Query,"can not create new game");
/***** Create groups *****/
if (Gbl.CurrentCrs.Grps.LstGrpsSel.NumGrps)
2017-09-07 18:38:18 +02:00
Gam_CreateGrps (Game->GamCod);
2017-07-09 20:31:40 +02:00
/***** Write success message *****/
sprintf (Gbl.Alert.Txt,Txt_Created_new_game_X,
Game->Title);
Ale_ShowAlert (Ale_SUCCESS,Gbl.Alert.Txt);
}
/*****************************************************************************/
/************************* 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 *Sco_ScopeDB[Sco_NUM_SCOPES];
extern const char *Txt_The_game_has_been_modified;
char Query[1024 +
2017-09-07 18:38:18 +02:00
Gam_MAX_BYTES_SURVEY_TITLE +
2017-07-09 20:31:40 +02:00
Cns_MAX_BYTES_TEXT];
/***** Update the data of the game *****/
sprintf (Query,"UPDATE games"
" SET Scope='%s',Cod=%ld,Roles=%u,"
"StartTime=FROM_UNIXTIME(%ld),"
"EndTime=FROM_UNIXTIME(%ld),"
"Title='%s',Txt='%s'"
" WHERE GamCod=%ld",
Sco_ScopeDB[Game->Scope],Game->Cod,
Game->Roles,
2017-09-07 18:38:18 +02:00
Game->TimeUTC[Gam_START_TIME],
Game->TimeUTC[Gam_END_TIME ],
2017-07-09 20:31:40 +02:00
Game->Title,
Txt,
Game->GamCod);
DB_QueryUPDATE (Query,"can not update game");
/***** Update groups *****/
/* Remove old groups */
2017-09-07 18:38:18 +02:00
Gam_RemoveAllTheGrpsAssociatedToAndGame (Game->GamCod);
2017-07-09 20:31:40 +02:00
/* Create new groups */
if (Gbl.CurrentCrs.Grps.LstGrpsSel.NumGrps)
2017-09-07 18:38:18 +02:00
Gam_CreateGrps (Game->GamCod);
2017-07-09 20:31:40 +02:00
/***** Write success message *****/
Ale_ShowAlert (Ale_SUCCESS,Txt_The_game_has_been_modified);
}
/*****************************************************************************/
/*************** Check if a game is associated to any group ****************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static bool Gam_CheckIfGamIsAssociatedToGrps (long GamCod)
2017-07-09 20:31:40 +02:00
{
char Query[128];
/***** Get if a game is associated to a group from database *****/
sprintf (Query,"SELECT COUNT(*) FROM gam_grp WHERE GamCod=%ld",
GamCod);
return (DB_QueryCOUNT (Query,"can not check if a game is associated to groups") != 0);
}
/*****************************************************************************/
/**************** Check if a game is associated to a group *****************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
bool Gam_CheckIfGamIsAssociatedToGrp (long GamCod,long GrpCod)
2017-07-09 20:31:40 +02:00
{
char Query[256];
/***** Get if a game is associated to a group from database *****/
sprintf (Query,"SELECT COUNT(*) FROM gam_grp"
" WHERE GamCod=%ld AND GrpCod=%ld",
GamCod,GrpCod);
return (DB_QueryCOUNT (Query,"can not check if a game is associated to a group") != 0);
}
/*****************************************************************************/
/************************* Remove groups of a game *************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_RemoveAllTheGrpsAssociatedToAndGame (long GamCod)
2017-07-09 20:31:40 +02:00
{
char Query[128];
/***** Remove groups of the game *****/
sprintf (Query,"DELETE FROM gam_grp WHERE GamCod=%ld",
GamCod);
DB_QueryDELETE (Query,"can not remove the groups associated to a game");
}
/*****************************************************************************/
/******************* Remove one group from all the games *******************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_RemoveGroup (long GrpCod)
2017-07-09 20:31:40 +02:00
{
char Query[128];
/***** Remove group from all the games *****/
sprintf (Query,"DELETE FROM gam_grp WHERE GrpCod=%ld",
GrpCod);
DB_QueryDELETE (Query,"can not remove group"
" from the associations between games and groups");
}
/*****************************************************************************/
/*************** Remove groups of one type from all the games **************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_RemoveGroupsOfType (long GrpTypCod)
2017-07-09 20:31:40 +02:00
{
char Query[256];
/***** Remove group from all the games *****/
sprintf (Query,"DELETE FROM gam_grp USING crs_grp,gam_grp"
" WHERE crs_grp.GrpTypCod=%ld"
" AND crs_grp.GrpCod=gam_grp.GrpCod",
GrpTypCod);
DB_QueryDELETE (Query,"can not remove groups of a type"
" from the associations between games and groups");
}
/*****************************************************************************/
/************************ Create groups of a game **************************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_CreateGrps (long GamCod)
2017-07-09 20:31:40 +02:00
{
unsigned NumGrpSel;
char Query[256];
/***** Create groups of the game *****/
for (NumGrpSel = 0;
NumGrpSel < Gbl.CurrentCrs.Grps.LstGrpsSel.NumGrps;
NumGrpSel++)
{
/* Create group */
sprintf (Query,"INSERT INTO gam_grp"
" (GamCod,GrpCod)"
" VALUES"
" (%ld,%ld)",
GamCod,Gbl.CurrentCrs.Grps.LstGrpsSel.GrpCods[NumGrpSel]);
DB_QueryINSERT (Query,"can not associate a group to a game");
}
}
/*****************************************************************************/
/************ Get and write the names of the groups of a game **************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_GetAndWriteNamesOfGrpsAssociatedToGame (struct Game *Game)
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;
char Query[512];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRow;
unsigned long NumRows;
/***** Get groups associated to a game from database *****/
sprintf (Query,"SELECT crs_grp_types.GrpTypName,crs_grp.GrpName"
" FROM gam_grp,crs_grp,crs_grp_types"
" WHERE gam_grp.GamCod=%ld"
" AND gam_grp.GrpCod=crs_grp.GrpCod"
" AND crs_grp.GrpTypCod=crs_grp_types.GrpTypCod"
" ORDER BY crs_grp_types.GrpTypName,crs_grp.GrpName",
Game->GamCod);
NumRows = DB_QuerySELECT (Query,&mysql_res,"can not get groups of a game");
/***** Write heading *****/
fprintf (Gbl.F.Out,"<div class=\"%s\">%s: ",
Game->Status.Visible ? "ASG_GRP" :
"ASG_GRP_LIGHT",
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",
Txt_The_whole_course,Gbl.CurrentCrs.Crs.ShrtName);
fprintf (Gbl.F.Out,"</div>");
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
/*****************************************************************************/
/************ Remove all the games of a place on the hierarchy *************/
/************ (country, institution, centre, degree or course) *************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_RemoveGames (Sco_Scope_t Scope,long Cod)
2017-07-09 20:31:40 +02:00
{
extern const char *Sco_ScopeDB[Sco_NUM_SCOPES];
char Query[512];
/***** Remove all the users in course games *****/
2017-07-16 13:54:11 +02:00
sprintf (Query,"DELETE FROM gam_users"
" USING games,gam_users"
2017-07-09 20:31:40 +02:00
" WHERE games.Scope='%s' AND games.Cod=%ld"
2017-07-16 13:54:11 +02:00
" AND games.GamCod=gam_users.GamCod",
2017-07-09 20:31:40 +02:00
Sco_ScopeDB[Scope],Cod);
DB_QueryDELETE (Query,"can not remove users"
" who had answered games in a place on the hierarchy");
/***** Remove all the answers in course games *****/
sprintf (Query,"DELETE FROM gam_answers"
2017-07-16 13:54:11 +02:00
" USING games,gam_questions,gam_answers"
2017-07-09 20:31:40 +02:00
" WHERE games.Scope='%s' AND games.Cod=%ld"
2017-07-16 13:54:11 +02:00
" AND games.GamCod=gam_questions.GamCod"
" AND gam_questions.QstCod=gam_answers.QstCod",
2017-07-09 20:31:40 +02:00
Sco_ScopeDB[Scope],Cod);
DB_QueryDELETE (Query,"can not remove answers of games in a place on the hierarchy");
/***** Remove all the questions in course games *****/
2017-07-16 13:54:11 +02:00
sprintf (Query,"DELETE FROM gam_questions"
" USING games,gam_questions"
2017-07-09 20:31:40 +02:00
" WHERE games.Scope='%s' AND games.Cod=%ld"
2017-07-16 13:54:11 +02:00
" AND games.GamCod=gam_questions.GamCod",
2017-07-09 20:31:40 +02:00
Sco_ScopeDB[Scope],Cod);
DB_QueryDELETE (Query,"can not remove questions of games in a place on the hierarchy");
/***** Remove groups *****/
sprintf (Query,"DELETE FROM gam_grp"
" USING games,gam_grp"
" WHERE games.Scope='%s' AND games.Cod=%ld"
" AND games.GamCod=gam_grp.GamCod",
Sco_ScopeDB[Scope],Cod);
DB_QueryDELETE (Query,"can not remove all the groups"
" associated to games of a course");
/***** Remove course games *****/
sprintf (Query,"DELETE FROM games"
" WHERE Scope='%s' AND Cod=%ld",
Sco_ScopeDB[Scope],Cod);
DB_QueryDELETE (Query,"can not remove all the games in a place on the hierarchy");
}
/*****************************************************************************/
/************ Check if I belong to any of the groups of a game *************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static bool Gam_CheckIfICanDoThisGameBasedOnGrps (long GamCod)
2017-07-09 20:31:40 +02:00
{
char Query[512];
/***** Get if I can do a game from database *****/
sprintf (Query,"SELECT COUNT(*) FROM games"
" WHERE GamCod=%ld"
" AND (GamCod NOT IN (SELECT GamCod FROM gam_grp) OR"
" GamCod IN (SELECT gam_grp.GamCod FROM gam_grp,crs_grp_usr"
" WHERE crs_grp_usr.UsrCod=%ld"
" AND gam_grp.GrpCod=crs_grp_usr.GrpCod))",
GamCod,Gbl.Usrs.Me.UsrDat.UsrCod);
return (DB_QueryCOUNT (Query,"can not check if I can do a game") != 0);
}
/*****************************************************************************/
/******************* 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
{
char Query[128];
/***** Get data of questions from database *****/
2017-07-16 13:54:11 +02:00
sprintf (Query,"SELECT COUNT(*) FROM gam_questions WHERE GamCod=%ld",
2017-07-09 20:31:40 +02:00
GamCod);
return (unsigned) DB_QueryCOUNT (Query,"can not get number of questions of a game");
}
/*****************************************************************************/
/*********** 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
false);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/****************** Write parameter with code of question ********************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_PutParamQstCod (long QstCod)
2017-07-09 20:31:40 +02:00
{
Par_PutHiddenParamLong ("QstCod",QstCod);
}
/*****************************************************************************/
/******************* Get parameter with code of question *********************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static long Gam_GetParamQstCod (void)
2017-07-09 20:31:40 +02:00
{
/***** Get code of question *****/
return Par_GetParToLong ("QstCod");
}
/*****************************************************************************/
/********************* Remove answers of a game question *******************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_RemAnswersOfAQuestion (long QstCod)
2017-07-09 20:31:40 +02:00
{
char Query[128];
/***** Remove answers *****/
sprintf (Query,"DELETE FROM gam_answers WHERE QstCod=%ld",
QstCod);
DB_QueryDELETE (Query,"can not remove the answers of a question");
}
/*****************************************************************************/
2017-09-01 10:37:00 +02:00
/******************** Get next question index in a game **********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
2017-09-11 19:06:46 +02:00
static unsigned Gam_GetQstIndFromQstCod (long GamCod,long QstCod)
2017-07-09 20:31:40 +02:00
{
2017-09-11 19:06:46 +02:00
char Query[256];
2017-07-09 20:31:40 +02:00
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned QstInd = 0;
/***** Get number of games with a field value from database *****/
2017-09-11 19:06:46 +02:00
sprintf (Query,"SELECT QstInd FROM gam_questions"
" WHERE GamCod=%ld AND QstCod=%ld",
GamCod,QstCod);
if (!DB_QuerySELECT (Query,&mysql_res,"can not get question index"))
{
Ale_ShowAlert (Ale_INFO,Query);
Lay_ShowErrorAndExit ("Error when getting question index.");
}
2017-07-09 20:31:40 +02:00
2017-09-11 19:06:46 +02:00
/***** Get question index (row[0]) *****/
row = mysql_fetch_row (mysql_res);
if (sscanf (row[0],"%u",&QstInd) != 1)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Error when getting question index.");
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
return QstInd;
}
/*****************************************************************************/
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)
{
char Query[256];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
long QstCod;
/***** Get question code of thw question to be moved up *****/
sprintf (Query,"SELECT QstCod FROM gam_questions"
" WHERE GamCod=%ld AND QstInd=%u",
GamCod,QstInd);
if (!DB_QuerySELECT (Query,&mysql_res,"can not get question code"))
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
/*****************************************************************************/
2017-09-11 19:06:46 +02:00
// Question index can be 0, 1, 2,...
// Return -1 if no questions
2017-07-09 20:31:40 +02:00
2017-09-11 19:06:46 +02:00
static int Gam_GetMaxQuestionIndexInGame (long GamCod)
2017-07-09 20:31:40 +02:00
{
char Query[128];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
2017-09-11 19:06:46 +02:00
int QstInd = -1;
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 *****/
2017-07-16 13:54:11 +02:00
sprintf (Query,"SELECT MAX(QstInd) FROM gam_questions WHERE GamCod=%ld",
2017-07-09 20:31:40 +02:00
GamCod);
DB_QuerySELECT (Query,&mysql_res,"can not get last question index");
row = mysql_fetch_row (mysql_res);
if (row[0]) // There are questions
2017-09-11 19:06:46 +02:00
if (sscanf (row[0],"%d",&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;
}
2017-09-11 19:06:46 +02:00
/*****************************************************************************/
/*********** Get previous question index to a given index in a game **********/
/*****************************************************************************/
static unsigned Gam_GetPrevQuestionIndexInGame (long GamCod,unsigned QstInd)
{
char Query[256];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned PrevQstInd;
/***** Get previous question index in a game from database *****/
// Although indexes are always continuous...
// ...this implementation works even with non continuous indexes
sprintf (Query,"SELECT MAX(QstInd) FROM gam_questions"
" WHERE GamCod=%ld AND QstInd<%u",
GamCod,QstInd);
if (!DB_QuerySELECT (Query,&mysql_res,"can not get previous question index"))
Lay_ShowErrorAndExit ("Error: previous question index not found.");
/***** Get previous question index (row[0]) *****/
row = mysql_fetch_row (mysql_res);
if (sscanf (row[0],"%u",&PrevQstInd) != 1)
Lay_ShowErrorAndExit ("Error when getting previous question index.");
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
return PrevQstInd;
}
/*****************************************************************************/
/************* Get next question index to a given index in a game ************/
/*****************************************************************************/
static unsigned Gam_GetNextQuestionIndexInGame (long GamCod,unsigned QstInd)
{
char Query[256];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NextQstInd;
/***** Get next question index in a game from database *****/
// Although indexes are always continuous...
// ...this implementation works even with non continuous indexes
sprintf (Query,"SELECT MIN(QstInd) FROM gam_questions"
" WHERE GamCod=%ld AND QstInd>%u",
GamCod,QstInd);
if (!DB_QuerySELECT (Query,&mysql_res,"can not get next question index"))
Lay_ShowErrorAndExit ("Error: next question index not found.");
/***** Get next question index (row[0]) *****/
row = mysql_fetch_row (mysql_res);
if (sscanf (row[0],"%u",&NextQstInd) != 1)
Lay_ShowErrorAndExit ("Error when getting next question index.");
/***** 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;
2017-09-06 15:03:43 +02:00
char Query[1024];
2017-07-09 20:31:40 +02:00
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 ******/
if (Game->Status.ICanAnswer && !Editing)
ActionToDoWithQuestions = Tst_SHOW_GAME_TO_ANSWER;
else
ActionToDoWithQuestions = Tst_SHOW_GAME_RESULT;
2017-07-09 20:31:40 +02:00
/***** Get data of questions from database *****/
2017-09-06 15:03:43 +02:00
/*
row[0] QstCod
row[1] AnsType
row[2] Stem
row[3] Feedback
row[4] ImageName
row[5] ImageTitle
row[6] ImageURL
*/
sprintf (Query,"SELECT tst_questions.QstCod,"
"tst_questions.AnsType,"
"tst_questions.Stem,"
"tst_questions.Feedback,"
"tst_questions.ImageName,"
"tst_questions.ImageTitle,"
"tst_questions.ImageURL"
2017-09-01 00:52:19 +02:00
" FROM gam_questions,tst_questions"
" WHERE gam_questions.GamCod=%ld"
" AND gam_questions.QstCod=tst_questions.QstCod"
" ORDER BY gam_questions.QstInd",
2017-07-09 20:31:40 +02:00
Game->GamCod);
NumQsts = (unsigned) DB_QuerySELECT (Query,&mysql_res,"can not get data of a question");
/***** Start box *****/
2017-09-01 14:36:25 +02:00
Gbl.Games.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 *****/
2017-09-07 18:38:18 +02:00
Gam_ListOneOrMoreQuestionsForEdition (Game,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 *****/
Act_FormEnd ();
}
}
else // This game has no questions
Ale_ShowAlert (Ale_INFO,Txt_This_game_has_no_questions);
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 ***********************/
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static void Gam_ListOneOrMoreQuestionsForEdition (struct Game *Game,
2017-09-06 15:03:43 +02:00
unsigned NumQsts,
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;
unsigned UniqueId;
2017-09-07 12:00:01 +02:00
long QstCod;
2017-09-08 01:18:20 +02:00
char StrNumQst[10 + 1];
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 *****/
for (NumQst = 0, UniqueId = 1;
NumQst < NumQsts;
NumQst++, UniqueId++)
{
Gbl.RowEvenOdd = NumQst % 2;
2017-09-10 18:58:26 +02:00
sprintf (StrNumQst,"%u",NumQst + 1);
2017-09-06 15:03:43 +02:00
row = mysql_fetch_row (mysql_res);
/*
row[0] QstCod
row[1] AnsType
row[2] Stem
row[3] Feedback
row[4] ImageName
row[5] ImageTitle
row[6] ImageURL
*/
/***** Create test question *****/
Tst_QstConstructor ();
/* row[0] holds the code of the question */
2017-09-11 19:06:46 +02:00
if ((QstCod = Str_ConvertStrCodToLongCod (row[0])) <= 0)
2017-09-06 15:03:43 +02:00
Lay_ShowErrorAndExit ("Wrong code of question.");
/***** Icons *****/
2017-09-11 19:06:46 +02:00
Gbl.Games.CurrentGamCod = Game->GamCod;
Gbl.Games.CurrentQstCod = QstCod;
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 */
2017-09-06 15:03:43 +02:00
Act_FormStart (ActReqRemGamQst);
2017-09-07 18:38:18 +02:00
Gam_PutParamGameCod (Game->GamCod);
Gam_PutParamQstCod (QstCod);
2017-09-06 15:03:43 +02:00
Ico_PutIconRemove ();
Act_FormEnd ();
2017-09-11 19:06:46 +02:00
/* Put icon to move up the question */
2017-09-10 18:58:26 +02:00
if (NumQst)
{
sprintf (Gbl.Title,Txt_Move_up_X,StrNumQst);
2017-09-11 19:06:46 +02:00
Lay_PutContextualLink (ActUp_GamQst,NULL,Gam_PutParamsOneQst,
2017-09-10 18:58:26 +02:00
"up_on16x16.gif",
Gbl.Title,NULL,
NULL);
}
else
2017-09-11 09:05:26 +02:00
Ico_PutIcon ("up_off16x16.gif",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 */
2017-09-10 18:58:26 +02:00
if (NumQst + 1 < NumQsts)
{
sprintf (Gbl.Title,Txt_Move_down_X,StrNumQst);
2017-09-11 19:06:46 +02:00
Lay_PutContextualLink (ActDwnGamQst,NULL,Gam_PutParamsOneQst,
2017-09-10 18:58:26 +02:00
"down_on16x16.gif",
Gbl.Title,NULL,
NULL);
}
else
2017-09-11 09:05:26 +02:00
Ico_PutIcon ("down_off16x16.gif",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-09-10 18:58:26 +02:00
"<div class=\"TEST_NUM_QST\">%s</div>",
2017-09-06 15:03:43 +02:00
Gbl.RowEvenOdd,
2017-09-10 18:58:26 +02:00
StrNumQst);
2017-09-06 15:03:43 +02:00
/* Write answer type (row[1]) */
Gbl.Test.AnswerType = Tst_ConvertFromStrAnsTypDBToAnsTyp (row[1]);
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>");
/* Write the stem (row[2]), the image (row[4], row[5], row[6]),
the feedback (row[3]) and the answers */
fprintf (Gbl.F.Out,"<td class=\"LEFT_TOP COLOR%u\">",
Gbl.RowEvenOdd);
Tst_WriteQstStem (row[2],"TEST_EDI");
Img_GetImageNameTitleAndURLFromRow (row[4],row[5],row[6],&Gbl.Test.Image);
Img_ShowImage (&Gbl.Test.Image,
"TEST_IMG_EDIT_LIST_STEM_CONTAINER",
"TEST_IMG_EDIT_LIST_STEM");
Tst_WriteQstFeedback (row[3],"TEST_EDI_LIGHT");
2017-09-07 12:00:01 +02:00
Tst_WriteAnswersGameResult (Game,NumQst,QstCod);
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 *****/
2017-09-07 18:38:18 +02:00
Lay_PutContextualLink (ActAddOneGamQst,NULL,Gam_PutParams,
2017-07-09 20:31:40 +02:00
"plus64x64.png",
2017-07-16 19:39:20 +02:00
Txt_Add_questions,NULL,
2017-07-09 20:31:40 +02:00
NULL);
}
/*****************************************************************************/
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
2017-09-07 12:00:01 +02:00
Act_FormStart (ActAddOneGamQst);
2017-09-07 18:38:18 +02:00
Gam_PutParams ();
2017-07-16 19:39:20 +02:00
Btn_PutConfirmButton (Txt_Add_questions);
2017-07-09 20:31:40 +02:00
Act_FormEnd ();
}
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;
2017-09-11 19:06:46 +02:00
int MaxQstInd;
2017-09-01 00:52:19 +02:00
char Query[256];
/***** 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
Ale_ShowAlert (Ale_WARNING,Txt_You_must_select_one_ore_more_questions);
// 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 */
sprintf (Query,"INSERT INTO gam_questions"
" (GamCod,QstCod,QstInd)"
" VALUES"
" (%ld,%ld,%u)",
2017-09-11 19:06:46 +02:00
Game.GamCod,QstCod,(unsigned) (MaxQstInd + 1));
2017-09-01 00:52:19 +02:00
DB_QueryINSERT (Query,"can not create question");
}
/***** 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
false);
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)
2017-07-18 20:34:32 +02:00
Lay_ShowErrorAndExit ("Not enough memory to store list of questions.");
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
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
void Gam_GetAndDrawBarNumUsrsWhoAnswered (struct Game *Game,long QstCod,unsigned AnsInd)
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 *****/
2017-09-07 18:38:18 +02:00
NumUsrsThisAnswer = Gam_GetNumUsrsWhoAnswered (Game->GamCod,QstCod,AnsInd);
2017-07-09 20:31:40 +02:00
2017-09-06 23:17:52 +02:00
/***** Show stats of this answer *****/
if (Game->Status.ICanViewResults)
2017-09-07 18:38:18 +02:00
Gam_DrawBarNumUsrs (NumUsrsThisAnswer,Game->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
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
static unsigned Gam_GetNumUsrsWhoAnswered (long GamCod,long QstCod,unsigned AnsInd)
2017-07-09 20:31:40 +02:00
{
2017-09-06 23:17:52 +02:00
char Query[256];
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 *****/
sprintf (Query,"SELECT NumUsrs FROM gam_answers"
" WHERE GamCod=%ld AND QstCod=%ld AND AnsInd=%u",
GamCod,QstCod,AnsInd);
if (DB_QuerySELECT (Query,&mysql_res,"can not get number of users who answered"))
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)
sprintf (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);
else
sprintf (Gbl.Title,"0&nbsp;(0%%&nbsp;%s&nbsp;%u)",
Txt_of_PART_OF_A_TOTAL,MaxUsrs);
/***** Draw bar with a with proportional to the number of clicks *****/
fprintf (Gbl.F.Out,"<td class=\"DAT LEFT_TOP\" style=\"width:%upx;\">",
2017-09-07 18:38:18 +02:00
Gam_MAX_BAR_WIDTH + 125);
2017-07-09 20:31:40 +02:00
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;",
Gbl.Prefs.IconsURL,
Gbl.Title,
Gbl.Title,
BarWidth);
/***** Write the number of users *****/
2017-09-06 23:17:52 +02:00
fprintf (Gbl.F.Out,"%s</td>",Gbl.Title);
2017-07-09 20:31:40 +02:00
}
/*****************************************************************************/
/********************* Put icon to remove one question ***********************/
/*****************************************************************************/
2017-09-01 10:37:00 +02:00
/*
2017-09-07 18:38:18 +02:00
static void Gam_PutIconToRemoveOneQst (void)
2017-07-09 20:31:40 +02:00
{
2017-09-07 18:38:18 +02:00
Ico_PutContextualIconToRemove (ActReqRemGamQst,Gam_PutParamsRemoveOneQst);
2017-07-09 20:31:40 +02:00
}
2017-09-01 10:37:00 +02:00
*/
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
{
2017-09-07 18:38:18 +02:00
Gam_PutParamGameCod (Gbl.Games.CurrentGamCod);
Gam_PutParamQstCod (Gbl.Games.CurrentQstCod);
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
long QstCod;
unsigned QstInd;
2017-07-09 20:31:40 +02:00
/***** Get parameters from form *****/
/* 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 code */
2017-09-11 19:06:46 +02:00
if ((QstCod = Gam_GetParamQstCod ()) <= 0)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Wrong code of question.");
/* Get question index */
2017-09-11 19:06:46 +02:00
QstInd = Gam_GetQstIndFromQstCod (Game.GamCod,QstCod);
2017-07-09 20:31:40 +02:00
/***** Show question and button to remove question *****/
2017-09-01 14:36:25 +02:00
Gbl.Games.CurrentGamCod = Game.GamCod;
2017-09-07 12:00:01 +02:00
Gbl.Games.CurrentQstCod = QstCod;
2017-07-09 20:31:40 +02:00
sprintf (Gbl.Alert.Txt,Txt_Do_you_really_want_to_remove_the_question_X,
2017-09-07 12:00:01 +02:00
(unsigned long) (QstInd + 1));
2017-07-09 20:31:40 +02:00
Ale_ShowAlertAndButton (Ale_QUESTION,Gbl.Alert.Txt,
2017-09-08 01:18:20 +02:00
ActRemGamQst,NULL,NULL,Gam_PutParamsOneQst,
2017-07-09 20:31:40 +02:00
Btn_REMOVE_BUTTON,Txt_Remove_question);
/***** Show current game *****/
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Game.GamCod,
true, // Show only this game
true, // List game questions
false);
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;
char Query[512];
2017-09-01 00:52:19 +02:00
struct Game Game;
2017-09-07 12:00:01 +02:00
long QstCod;
unsigned QstInd;
2017-07-09 20:31:40 +02:00
/***** Get parameters from form *****/
/* 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 code */
2017-09-11 19:06:46 +02:00
if ((QstCod = Gam_GetParamQstCod ()) <= 0)
2017-07-09 20:31:40 +02:00
Lay_ShowErrorAndExit ("Wrong code of question.");
/* Get question index */
2017-09-11 19:06:46 +02:00
QstInd = Gam_GetQstIndFromQstCod (Game.GamCod,QstCod);
2017-07-09 20:31:40 +02:00
/***** Remove the question from all the tables *****/
/* Remove answers from this test question */
2017-09-07 18:38:18 +02:00
Gam_RemAnswersOfAQuestion (QstCod);
2017-07-09 20:31:40 +02:00
/* Remove the question itself */
2017-07-16 13:54:11 +02:00
sprintf (Query,"DELETE FROM gam_questions WHERE QstCod=%ld",
2017-09-07 12:00:01 +02:00
QstCod);
2017-07-09 20:31:40 +02:00
DB_QueryDELETE (Query,"can not remove a question");
if (!mysql_affected_rows (&Gbl.mysql))
Lay_ShowErrorAndExit ("The question to be removed does not exist.");
/* Change index of questions greater than this */
2017-07-16 13:54:11 +02:00
sprintf (Query,"UPDATE gam_questions SET QstInd=QstInd-1"
2017-07-09 20:31:40 +02:00
" WHERE GamCod=%ld AND QstInd>%u",
2017-09-07 12:00:01 +02:00
Game.GamCod,QstInd);
2017-07-09 20:31:40 +02:00
DB_QueryUPDATE (Query,"can not update indexes of questions");
/***** Write message *****/
sprintf (Gbl.Alert.Txt,"%s",Txt_Question_removed);
Ale_ShowAlert (Ale_SUCCESS,Gbl.Alert.Txt);
/***** Show current game *****/
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Game.GamCod,
true, // Show only this game
true, // List game questions
false);
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;
struct Game Game;
long QstCod;
unsigned QstIndTop;
unsigned QstIndBottom;
/***** Get parameters from form *****/
/* Get game code */
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
Lay_ShowErrorAndExit ("Code of game is missing.");
/* Get question code */
if ((QstCod = Gam_GetParamQstCod ()) <= 0)
Lay_ShowErrorAndExit ("Wrong code of question.");
/* Get question index */
QstIndBottom = Gam_GetQstIndFromQstCod (Game.GamCod,QstCod);
/***** Move up question *****/
if (QstIndBottom > 0)
{
/* Indexes of questions to be exchanged */
QstIndTop = Gam_GetPrevQuestionIndexInGame (Game.GamCod,QstIndBottom);
/* Exchange questions */
Gam_ExchangeQuestions (Game.GamCod,
QstIndTop,QstIndBottom);
/* Success alert */
Ale_ShowAlert (Ale_SUCCESS,Txt_The_question_has_been_moved_up);
}
/***** Show current game *****/
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Game.GamCod,
true, // Show only this game
true, // List game questions
false);
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;
struct Game Game;
long QstCod;
unsigned QstIndTop;
unsigned QstIndBottom;
int MaxQstInd; // -1 if no questions
/***** Get parameters from form *****/
/* Get game code */
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
Lay_ShowErrorAndExit ("Code of game is missing.");
/* Get question code */
if ((QstCod = Gam_GetParamQstCod ()) <= 0)
Lay_ShowErrorAndExit ("Wrong code of question.");
/* Get question index */
QstIndTop = Gam_GetQstIndFromQstCod (Game.GamCod,QstCod);
/* Get maximum question index */
MaxQstInd = Gam_GetMaxQuestionIndexInGame (Game.GamCod);
/***** Move down question *****/
if (MaxQstInd > 0)
if (QstIndTop < (unsigned) MaxQstInd)
{
/* Indexes of questions to be exchanged */
QstIndBottom = Gam_GetNextQuestionIndexInGame (Game.GamCod,QstIndTop);
/* Exchange questions */
Gam_ExchangeQuestions (Game.GamCod,QstIndTop,QstIndBottom);
/* Success alert */
Ale_ShowAlert (Ale_SUCCESS,Txt_The_question_has_been_moved_down);
}
/***** Show current game *****/
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Game.GamCod,
true, // Show only this game
true, // List game questions
false);
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)
{
char Query[256];
long QstCodTop;
long QstCodBottom;
/***** Lock table to make the inscription atomic *****/
DB_Query ("LOCK TABLES gam_questions WRITE",
"Can not lock tables to move game question");
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:
QstIndTop = 0; QstCodTop = 218
QstIndBottom = 1; QstCodBottom = 220
+--------+--------+ +--------+--------+ +--------+--------+
| QstInd | QstCod | | QstInd | QstCod | | QstInd | QstCod |
+--------+--------+ +--------+--------+ +--------+--------+
| 0 | 218 | -----> | 1 | 218 | = | 0 | 220 |
| 1 | 220 | | 0 | 220 | | 1 | 218 |
| 2 | 232 | | 2 | 232 | | 2 | 232 |
+--------+--------+ +--------+--------+ +--------+--------+
*/
sprintf (Query,"UPDATE gam_questions SET QstInd=%u"
" WHERE GamCod=%ld AND QstCod=%ld",
QstIndBottom,
GamCod,QstCodTop);
DB_QueryUPDATE (Query,"can not exchange indexes of questions");
sprintf (Query,"UPDATE gam_questions SET QstInd=%u"
" WHERE GamCod=%ld AND QstCod=%ld",
QstIndTop,
GamCod,QstCodBottom);
DB_QueryUPDATE (Query,"can not exchange indexes of questions");
/***** Unlock table *****/
Gbl.DB.LockedTables = false; // Set to false before the following unlock...
// ...to not retry the unlock if error in unlocking
DB_Query ("UNLOCK TABLES",
"Can not unlock tables after moving game questions");
}
2017-09-13 09:47:45 +02:00
/*****************************************************************************/
/************************* Start playing a game ******************************/
/*****************************************************************************/
void Gam_PlayGame (void)
{
struct Game Game;
/***** 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 *****/
if ((Game.GamCod = Gam_GetParamGameCod ()) == -1L)
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
false,
true); // Put button to start
}
/*****************************************************************************/
/********************* Put a big button to start game ************************/
/*****************************************************************************/
static void Gam_PutBigButtonToStartGame (void)
{
extern const char *Txt_Play;
/***** Start form *****/
Act_FormStart (ActPlyGam);
Gam_PutParams ();
/***** Put icon with link *****/
Act_LinkFormSubmit (Txt_Play,NULL,NULL);
fprintf (Gbl.F.Out,"<img src=\"%s/play64x64.png\" alt=\"%s\" title=\"%s\""
" class=\"CONTEXT_OPT ICO_HIGHLIGHT ICO64x64\" />",
Gbl.Prefs.IconsURL,Txt_Play,Txt_Play);
fprintf (Gbl.F.Out,"</a>");
/***** End form *****/
Act_FormEnd ();
2017-09-13 09:47:45 +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)
Ale_ShowAlert (Ale_WARNING,Txt_You_already_played_this_game_before);
else
{
/***** Receive and store user's answers *****/
2017-09-07 18:38:18 +02:00
Gam_ReceiveAndStoreUserAnswersToAGame (Game.GamCod);
2017-07-09 20:31:40 +02:00
Ale_ShowAlert (Ale_INFO,Txt_Thanks_for_playing_the_game);
}
/***** Show current game *****/
2017-09-13 16:24:29 +02:00
Gam_ShowOneGame (Game.GamCod,
true, // Show only this game
true, // List game questions
false);
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
{
char Query[256];
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 *****/
2017-07-16 13:54:11 +02:00
sprintf (Query,"SELECT QstCod FROM gam_questions"
2017-07-09 20:31:40 +02:00
" WHERE GamCod=%ld ORDER BY QstCod",
GamCod);
DB_QuerySELECT (Query,&mysql_res,"can not get questions of a game");
if ((NumQsts = (unsigned) DB_QuerySELECT (Query,&mysql_res,"can not get games"))) // The game has questions
{
/***** 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 */
sprintf (ParamName,"Ans%010u",(unsigned) QstCod);
// 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
{
char Query[256];
2017-09-01 10:37:00 +02:00
/***** Increase number of users who have selected
the answer AnsInd in the question QstCod *****/
2017-07-09 20:31:40 +02:00
sprintf (Query,"UPDATE gam_answers SET NumUsrs=NumUsrs+1"
" WHERE QstCod=%ld AND AnsInd=%u",
QstCod,AnsInd);
DB_QueryINSERT (Query,"can not register your answer to the game");
}
/*****************************************************************************/
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
{
char Query[256];
2017-07-16 13:54:11 +02:00
sprintf (Query,"INSERT INTO gam_users"
2017-07-09 20:31:40 +02:00
" (GamCod,UsrCod)"
" VALUES"
" (%ld,%ld)",
GamCod,Gbl.Usrs.Me.UsrDat.UsrCod);
DB_QueryINSERT (Query,"can not register that you have answered the game");
}
/*****************************************************************************/
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
{
char Query[256];
/***** Get number of games with a field value from database *****/
2017-07-16 13:54:11 +02:00
sprintf (Query,"SELECT COUNT(*) FROM gam_users"
2017-07-09 20:31:40 +02:00
" WHERE GamCod=%ld AND UsrCod=%ld",
GamCod,Gbl.Usrs.Me.UsrDat.UsrCod);
return (DB_QueryCOUNT (Query,"can not check if you have answered a game") != 0);
}
/*****************************************************************************/
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
{
char Query[128];
/***** Get number of games with a field value from database *****/
2017-07-16 13:54:11 +02:00
sprintf (Query,"SELECT COUNT(*) FROM gam_users WHERE GamCod=%ld",
2017-07-09 20:31:40 +02:00
GamCod);
return (unsigned) DB_QueryCOUNT (Query,"can not get number of users who have answered a game");
}
/*****************************************************************************/
2017-09-01 10:37:00 +02:00
/********************* Get number of courses with games **********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
// Returns the number of courses with games for courses
// in this location (all the platform, current degree or current course)
2017-09-07 18:38:18 +02:00
unsigned Gam_GetNumCoursesWithCrsGames (Sco_Scope_t Scope)
2017-07-09 20:31:40 +02:00
{
extern const char *Sco_ScopeDB[Sco_NUM_SCOPES];
char Query[1024];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumCourses;
/***** Get number of courses with games from database *****/
switch (Scope)
{
case Sco_SCOPE_SYS:
sprintf (Query,"SELECT COUNT(DISTINCT Cod)"
" FROM games"
" WHERE Scope='%s'",
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_CTY:
sprintf (Query,"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'",
Gbl.CurrentIns.Ins.InsCod,
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_INS:
sprintf (Query,"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'",
Gbl.CurrentIns.Ins.InsCod,
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_CTR:
sprintf (Query,"SELECT COUNT(DISTINCT games.Cod)"
" FROM degrees,courses,games"
" WHERE degrees.CtrCod=%ld"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'",
Gbl.CurrentCtr.Ctr.CtrCod,
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_DEG:
sprintf (Query,"SELECT COUNT(DISTINCT games.Cod)"
" FROM courses,games"
" WHERE courses.DegCod=%ld"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'",
Gbl.CurrentDeg.Deg.DegCod,
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_CRS:
sprintf (Query,"SELECT COUNT(DISTINCT Cod)"
" FROM games"
" WHERE Scope='%s' AND Cod=%ld",
Sco_ScopeDB[Sco_SCOPE_CRS],
Gbl.CurrentCrs.Crs.CrsCod);
break;
default:
Lay_ShowErrorAndExit ("Wrong scope.");
break;
}
DB_QuerySELECT (Query,&mysql_res,"can not get number of courses with games");
/***** 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-01 10:37:00 +02:00
/********************* Get number of games for courses ***********************/
2017-07-09 20:31:40 +02:00
/*****************************************************************************/
// Returns the number of games for courses
// in this location (all the platform, current degree or current course)
2017-09-13 16:24:29 +02:00
unsigned Gam_GetNumCrsGames (Sco_Scope_t Scope)
2017-07-09 20:31:40 +02:00
{
extern const char *Sco_ScopeDB[Sco_NUM_SCOPES];
char Query[1024];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumGames;
/***** Get number of games from database *****/
switch (Scope)
{
case Sco_SCOPE_SYS:
2017-09-13 16:24:29 +02:00
sprintf (Query,"SELECT COUNT(*)"
2017-07-09 20:31:40 +02:00
" FROM games"
" WHERE Scope='%s'",
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_CTY:
2017-09-13 16:24:29 +02:00
sprintf (Query,"SELECT COUNT(*)"
2017-07-09 20:31:40 +02:00
" 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'",
Gbl.CurrentCty.Cty.CtyCod,
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_INS:
2017-09-13 16:24:29 +02:00
sprintf (Query,"SELECT COUNT(*)"
2017-07-09 20:31:40 +02:00
" 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'",
Gbl.CurrentIns.Ins.InsCod,
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_CTR:
2017-09-13 16:24:29 +02:00
sprintf (Query,"SELECT COUNT(*)"
2017-07-09 20:31:40 +02:00
" FROM degrees,courses,games"
" WHERE degrees.CtrCod=%ld"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'",
Gbl.CurrentCtr.Ctr.CtrCod,
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_DEG:
2017-09-13 16:24:29 +02:00
sprintf (Query,"SELECT COUNT(*)"
2017-07-09 20:31:40 +02:00
" FROM courses,games"
" WHERE courses.DegCod=%ld"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'",
Gbl.CurrentDeg.Deg.DegCod,
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_CRS:
2017-09-13 16:24:29 +02:00
sprintf (Query,"SELECT COUNT(*)"
2017-07-09 20:31:40 +02:00
" FROM games"
" WHERE games.Scope='%s'"
" AND CrsCod=%ld",
Sco_ScopeDB[Sco_SCOPE_CRS],
Gbl.CurrentCrs.Crs.CrsCod);
break;
default:
Lay_ShowErrorAndExit ("Wrong scope.");
break;
}
DB_QuerySELECT (Query,&mysql_res,"can not get number of games");
/***** 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
/*****************************************************************************/
2017-09-07 18:38:18 +02:00
float Gam_GetNumQstsPerCrsGame (Sco_Scope_t Scope)
2017-07-09 20:31:40 +02:00
{
extern const char *Sco_ScopeDB[Sco_NUM_SCOPES];
char Query[1024];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
float NumQstsPerGame;
/***** Get number of questions per game from database *****/
switch (Scope)
{
case Sco_SCOPE_SYS:
sprintf (Query,"SELECT AVG(NumQsts) FROM"
2017-07-16 13:54:11 +02:00
" (SELECT COUNT(gam_questions.QstCod) AS NumQsts"
" FROM games,gam_questions"
2017-07-09 20:31:40 +02:00
" WHERE games.Scope='%s'"
2017-07-16 13:54:11 +02:00
" AND games.GamCod=gam_questions.GamCod"
" GROUP BY gam_questions.GamCod) AS NumQstsTable",
2017-07-09 20:31:40 +02:00
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_CTY:
sprintf (Query,"SELECT AVG(NumQsts) FROM"
2017-07-16 13:54:11 +02:00
" (SELECT COUNT(gam_questions.QstCod) AS NumQsts"
" FROM institutions,centres,degrees,courses,games,gam_questions"
2017-07-09 20:31:40 +02:00
" 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'"
2017-07-16 13:54:11 +02:00
" AND games.GamCod=gam_questions.GamCod"
" GROUP BY gam_questions.GamCod) AS NumQstsTable",
2017-07-09 20:31:40 +02:00
Gbl.CurrentCty.Cty.CtyCod,
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_INS:
sprintf (Query,"SELECT AVG(NumQsts) FROM"
2017-07-16 13:54:11 +02:00
" (SELECT COUNT(gam_questions.QstCod) AS NumQsts"
" FROM centres,degrees,courses,games,gam_questions"
2017-07-09 20:31:40 +02:00
" WHERE centres.InsCod=%ld"
" AND centres.CtrCod=degrees.CtrCod"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'"
2017-07-16 13:54:11 +02:00
" AND games.GamCod=gam_questions.GamCod"
" GROUP BY gam_questions.GamCod) AS NumQstsTable",
2017-07-09 20:31:40 +02:00
Gbl.CurrentIns.Ins.InsCod,
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_CTR:
sprintf (Query,"SELECT AVG(NumQsts) FROM"
2017-07-16 13:54:11 +02:00
" (SELECT COUNT(gam_questions.QstCod) AS NumQsts"
" FROM degrees,courses,games,gam_questions"
2017-07-09 20:31:40 +02:00
" WHERE degrees.CtrCod=%ld"
" AND degrees.DegCod=courses.DegCod"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'"
2017-07-16 13:54:11 +02:00
" AND games.GamCod=gam_questions.GamCod"
" GROUP BY gam_questions.GamCod) AS NumQstsTable",
2017-07-09 20:31:40 +02:00
Gbl.CurrentCtr.Ctr.CtrCod,
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_DEG:
sprintf (Query,"SELECT AVG(NumQsts) FROM"
2017-07-16 13:54:11 +02:00
" (SELECT COUNT(gam_questions.QstCod) AS NumQsts"
" FROM courses,games,gam_questions"
2017-07-09 20:31:40 +02:00
" WHERE courses.DegCod=%ld"
" AND courses.CrsCod=games.Cod"
" AND games.Scope='%s'"
2017-07-16 13:54:11 +02:00
" AND games.GamCod=gam_questions.GamCod"
" GROUP BY gam_questions.GamCod) AS NumQstsTable",
2017-07-09 20:31:40 +02:00
Gbl.CurrentDeg.Deg.DegCod,
Sco_ScopeDB[Sco_SCOPE_CRS]);
break;
case Sco_SCOPE_CRS:
sprintf (Query,"SELECT AVG(NumQsts) FROM"
2017-07-16 13:54:11 +02:00
" (SELECT COUNT(gam_questions.QstCod) AS NumQsts"
" FROM games,gam_questions"
2017-07-09 20:31:40 +02:00
" WHERE games.Scope='%s' AND games.Cod=%ld"
2017-07-16 13:54:11 +02:00
" AND games.GamCod=gam_questions.GamCod"
" GROUP BY gam_questions.GamCod) AS NumQstsTable",
2017-07-09 20:31:40 +02:00
Sco_ScopeDB[Sco_SCOPE_CRS],Gbl.CurrentCrs.Crs.CrsCod);
break;
default:
Lay_ShowErrorAndExit ("Wrong scope.");
break;
}
DB_QuerySELECT (Query,&mysql_res,"can not get number of questions per game");
/***** 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;
}