swad-core/swad_match.c

3180 lines
108 KiB
C
Raw Normal View History

2019-09-14 12:59:34 +02:00
// swad_match.c: matches in games using remote control
/*
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-2019 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 ***********************************/
/*****************************************************************************/
#define _GNU_SOURCE // For asprintf
#include <linux/limits.h> // For PATH_MAX
#include <linux/stddef.h> // For NULL
#include <stdio.h> // For asprintf
#include <stdlib.h> // For calloc
#include <string.h> // For string functions
#include "swad_database.h"
2019-09-30 01:23:24 +02:00
#include "swad_date.h"
2019-09-14 12:59:34 +02:00
#include "swad_form.h"
#include "swad_game.h"
#include "swad_global.h"
#include "swad_match.h"
2019-09-28 02:31:42 +02:00
#include "swad_match_result.h"
2019-09-14 12:59:34 +02:00
#include "swad_role.h"
#include "swad_setting.h"
#include "swad_table.h"
#include "swad_test.h"
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
#define Mch_ICON_CLOSE "fas fa-times"
#define Mch_ICON_PLAY "fas fa-play"
#define Mch_ICON_PAUSE "fas fa-pause"
#define Mch_ICON_PREVIOUS "fas fa-step-backward"
#define Mch_ICON_NEXT "fas fa-step-forward"
#define Mch_ICON_RESULTS "fas fa-chart-bar"
/*****************************************************************************/
/******************************* Private types *******************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
const char *Mch_ShowingStringsDB[Mch_NUM_SHOWING] =
{
2019-09-17 15:42:41 +02:00
"nothing",
2019-09-16 00:15:40 +02:00
"stem",
2019-09-14 12:59:34 +02:00
"answers",
"results",
};
/*****************************************************************************/
/***************************** Private variables *****************************/
/*****************************************************************************/
long Mch_CurrentMchCod = -1L; // Used as parameter in contextual links
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
2019-09-30 10:51:44 +02:00
static void Mch_PutIconToCreateNewMatch (void);
2019-09-25 00:42:13 +02:00
2019-09-14 12:59:34 +02:00
static void Mch_ListOneOrMoreMatches (struct Game *Game,
unsigned NumMatches,
MYSQL_RES *mysql_res);
2019-09-29 19:36:32 +02:00
static void Mch_ListOneOrMoreMatchesHeading (bool ICanEditMatches);
2019-09-29 18:44:40 +02:00
static bool Mch_CheckIfICanEditMatches (void);
2019-09-30 01:10:57 +02:00
static bool Mch_CheckIfICanEditThisMatch (const struct Match *Match);
2019-09-25 09:29:44 +02:00
static void Mch_ListOneOrMoreMatchesIcons (const struct Match *Match);
static void Mch_ListOneOrMoreMatchesAuthor (const struct Match *Match);
2019-09-25 00:42:13 +02:00
static void Mch_ListOneOrMoreMatchesTimes (const struct Match *Match,unsigned UniqueId);
static void Mch_ListOneOrMoreMatchesTitleGrps (const struct Match *Match);
static void Mch_GetAndWriteNamesOfGrpsAssociatedToMatch (const struct Match *Match);
2019-09-25 09:29:44 +02:00
static void Mch_ListOneOrMoreMatchesNumPlayers (const struct Match *Match);
2019-09-29 19:36:32 +02:00
static void Mch_ListOneOrMoreMatchesStatus (const struct Match *Match,unsigned NumQsts,
bool ICanPlayThisMatchBasedOnGrps);
static void Mch_ListOneOrMoreMatchesResult (const struct Match *Match,
bool ICanPlayThisMatchBasedOnGrps);
2019-09-25 00:42:13 +02:00
2019-09-14 12:59:34 +02:00
static void Mch_GetMatchDataFromRow (MYSQL_RES *mysql_res,
struct Match *Match);
static Mch_Showing_t Mch_GetShowingFromStr (const char *Str);
2019-09-27 20:00:47 +02:00
static void Mch_RemoveMatchFromAllTables (long MchCod);
static void Mch_RemoveMatchFromTable (long MchCod,const char *TableName);
2019-09-28 01:12:53 +02:00
static void Mch_RemoveMatchesInGameFromTable (long GamCod,const char *TableName);
static void Mch_RemoveMatchInCourseFromTable (long CrsCod,const char *TableName);
2019-09-29 23:13:20 +02:00
static void Mch_RemoveUsrMchResultsInCrs (long UsrCod,long CrsCod,const char *TableName);
2019-09-27 20:00:47 +02:00
2019-09-29 22:38:32 +02:00
static void Mch_PutParamsPlay (void);
2019-09-30 01:10:57 +02:00
static void Mch_PutParamMchCod (long MchCod);
2019-09-14 12:59:34 +02:00
static void Mch_PutFormNewMatch (struct Game *Game);
static void Mch_ShowLstGrpsToCreateMatch (void);
static long Mch_CreateMatch (long GamCod,char Title[Gam_MAX_BYTES_TITLE + 1]);
2019-09-23 21:39:54 +02:00
static void Mch_CreateIndexes (long GamCod,long MchCod);
static void Mch_ReorderAnswer (long MchCod,unsigned QstInd,
long QstCod,bool Shuffle);
2019-09-14 12:59:34 +02:00
static void Mch_CreateGrps (long MchCod);
static void Mch_UpdateMatchStatusInDB (struct Match *Match);
static void Mch_UpdateElapsedTimeInQuestion (struct Match *Match);
static void Mch_GetElapsedTimeInQuestion (struct Match *Match,
struct Time *Time);
static void Mch_GetElapsedTimeInMatch (struct Match *Match,
struct Time *Time);
static void Mch_GetElapsedTime (unsigned NumRows,MYSQL_RES *mysql_res,
struct Time *Time);
static void Mch_SetMatchStatusToPrev (struct Match *Match);
2019-09-17 15:42:41 +02:00
static void Mch_SetMatchStatusToPrevQst (struct Match *Match);
static void Mch_SetMatchStatusToStart (struct Match *Match);
2019-09-14 12:59:34 +02:00
static void Mch_SetMatchStatusToNext (struct Match *Match);
2019-09-17 15:42:41 +02:00
static void Mch_SetMatchStatusToNextQst (struct Match *Match);
static void Mch_SetMatchStatusToEnd (struct Match *Match);
2019-09-14 12:59:34 +02:00
static void Mch_ShowMatchStatusForTch (struct Match *Match);
static void Mch_ShowMatchStatusForStd (struct Match *Match);
2019-09-26 18:19:07 +02:00
2019-09-14 12:59:34 +02:00
static void Mch_ShowLeftColumnTch (struct Match *Match);
2019-09-30 16:25:44 +02:00
static void Mch_ShowRefreshablePartTch (struct Match *Match);
2019-09-26 18:19:07 +02:00
static void Mch_ShowRightColumnTch (struct Match *Match);
2019-09-14 12:59:34 +02:00
static void Mch_ShowLeftColumnStd (struct Match *Match);
2019-09-26 18:19:07 +02:00
static void Mch_ShowRightColumnStd (struct Match *Match);
2019-09-14 12:59:34 +02:00
static void Mch_ShowNumQstInMatch (struct Match *Match);
2019-09-14 19:46:26 +02:00
static void Mch_PutMatchControlButtons (struct Match *Match);
static void Mch_PutCheckboxResult (struct Match *Match);
2019-09-14 12:59:34 +02:00
static void Mch_ShowMatchTitle (struct Match *Match);
static void Mch_ShowQuestionAndAnswersTch (struct Match *Match);
static void Mch_ShowQuestionAndAnswersStd (struct Match *Match);
2019-10-01 01:01:35 +02:00
static void Mch_ShowMatchPodium (struct Match *Match);
2019-09-14 12:59:34 +02:00
2019-09-24 01:41:51 +02:00
static void Mch_PutParamNumOpt (unsigned NumOpt);
static unsigned Mch_GetParamNumOpt (void);
2019-09-14 12:59:34 +02:00
static void Mch_PutBigButton (Act_Action_t NextAction,long MchCod,
const char *Icon,const char *Txt);
static void Mch_PutBigButtonOff (const char *Icon);
static void Mch_PutBigButtonClose (void);
static void Mch_ShowWaitImage (const char *Txt);
static void Mch_RemoveOldPlayers (void);
static void Mch_UpdateMatchAsBeingPlayed (long MchCod);
static void Mch_SetMatchAsNotBeingPlayed (long MchCod);
static bool Mch_GetIfMatchIsBeingPlayed (long MchCod);
static void Mch_RegisterMeAsPlayerInMatch (long MchCod);
static void Mch_GetNumPlayers (struct Match *Match);
2019-09-24 21:29:01 +02:00
static double Mch_ComputeScore (unsigned NumQsts);
2019-09-14 12:59:34 +02:00
2019-09-16 00:15:40 +02:00
static unsigned Mch_GetNumUsrsWhoHaveChosenAns (long MchCod,unsigned QstInd,unsigned AnsInd);
2019-09-17 20:44:29 +02:00
static unsigned Mch_GetNumUsrsWhoHaveAnswerMch (long MchCod);
2019-09-16 00:15:40 +02:00
static void Mch_DrawBarNumUsrs (unsigned NumAnswerersAns,unsigned NumAnswerersQst,bool Correct);
2019-09-14 12:59:34 +02:00
2019-09-29 22:38:32 +02:00
static long Mch_GetParamCurrentMchCod (void);
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
/************************* List the matches of a game ************************/
/*****************************************************************************/
void Mch_ListMatches (struct Game *Game,bool PutFormNewMatch)
{
extern const char *Hlp_ASSESSMENT_Games_matches;
extern const char *Txt_Matches;
char *SubQuery;
MYSQL_RES *mysql_res;
unsigned NumMatches;
2019-09-30 10:51:44 +02:00
bool ICanEditMatches = Mch_CheckIfICanEditMatches ();
2019-09-14 12:59:34 +02:00
/***** Get data of matches from database *****/
/* Fill subquery for game */
2019-09-29 17:33:39 +02:00
if (Gbl.Crs.Grps.WhichGrps == Grp_MY_GROUPS)
2019-09-14 12:59:34 +02:00
{
if (asprintf (&SubQuery," AND"
"(MchCod NOT IN"
2019-09-17 01:37:07 +02:00
" (SELECT MchCod FROM mch_groups)"
2019-09-14 12:59:34 +02:00
" OR"
" MchCod IN"
2019-09-17 01:37:07 +02:00
" (SELECT mch_groups.MchCod"
" FROM mch_groups,crs_grp_usr"
2019-09-14 12:59:34 +02:00
" WHERE crs_grp_usr.UsrCod=%ld"
2019-09-17 01:37:07 +02:00
" AND mch_groups.GrpCod=crs_grp_usr.GrpCod))",
2019-09-14 12:59:34 +02:00
Gbl.Usrs.Me.UsrDat.UsrCod) < 0)
Lay_NotEnoughMemoryExit ();
}
else // Gbl.Crs.Grps.WhichGrps == Grp_ALL_GROUPS
if (asprintf (&SubQuery,"%s","") < 0)
Lay_NotEnoughMemoryExit ();
/* Make query */
NumMatches = (unsigned) DB_QuerySELECT (&mysql_res,"can not get matches",
2019-09-25 21:58:12 +02:00
"SELECT MchCod," // row[ 0]
"GamCod," // row[ 1]
"UsrCod," // row[ 2]
"UNIX_TIMESTAMP(StartTime)," // row[ 3]
"UNIX_TIMESTAMP(EndTime)," // row[ 4]
"Title," // row[ 5]
"QstInd," // row[ 6]
"QstCod," // row[ 7]
"Showing," // row[ 8]
"ShowQstResults," // row[ 9]
"ShowUsrResults" // row[10]
2019-09-17 01:37:07 +02:00
" FROM mch_matches"
2019-09-14 12:59:34 +02:00
" WHERE GamCod=%ld%s"
" ORDER BY MchCod",
Game->GamCod,
SubQuery);
/* Free allocated memory for subquery */
free ((void *) SubQuery);
/***** Start box *****/
2019-09-29 22:38:32 +02:00
Gam_SetParamCurrentGamCod (Game->GamCod); // Used to pass parameter
2019-09-30 10:51:44 +02:00
Box_StartBox (NULL,Txt_Matches,ICanEditMatches ? Mch_PutIconToCreateNewMatch :
NULL,
2019-09-14 12:59:34 +02:00
Hlp_ASSESSMENT_Games_matches,Box_NOT_CLOSABLE);
2019-09-29 17:33:39 +02:00
/***** Select whether show only my groups or all groups *****/
if (Gbl.Crs.Grps.NumGrps)
{
Set_StartSettingsHead ();
Grp_ShowFormToSelWhichGrps (ActSeeGam,Gam_PutParams);
Set_EndSettingsHead ();
}
2019-09-14 12:59:34 +02:00
if (NumMatches)
/***** Show the table with the matches *****/
Mch_ListOneOrMoreMatches (Game,NumMatches,mysql_res);
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
/***** Put button to play a new match in this game *****/
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_NET:
case Rol_TCH:
case Rol_SYS_ADM:
if (PutFormNewMatch)
Mch_PutFormNewMatch (Game); // Form to fill in data and start playing a new match
else
2019-09-29 17:33:39 +02:00
Gam_PutButtonNewMatch (Game->GamCod); // Button to create a new match
2019-09-14 12:59:34 +02:00
break;
default:
break;
}
/***** End box *****/
Box_EndBox ();
}
/*****************************************************************************/
/********************** Get match data using its code ************************/
/*****************************************************************************/
2019-09-28 02:31:42 +02:00
void Mch_GetDataOfMatchByCod (struct Match *Match)
2019-09-14 12:59:34 +02:00
{
MYSQL_RES *mysql_res;
unsigned long NumRows;
2019-09-22 19:50:24 +02:00
Dat_StartEndTime_t StartEndTime;
2019-09-14 12:59:34 +02:00
/***** Get data of match from database *****/
NumRows = (unsigned) DB_QuerySELECT (&mysql_res,"can not get matches",
2019-09-25 21:58:12 +02:00
"SELECT MchCod," // row[ 0]
"GamCod," // row[ 1]
"UsrCod," // row[ 2]
"UNIX_TIMESTAMP(StartTime)," // row[ 3]
"UNIX_TIMESTAMP(EndTime)," // row[ 4]
"Title," // row[ 5]
"QstInd," // row[ 6]
"QstCod," // row[ 7]
"Showing," // row[ 8]
"ShowQstResults," // row[ 9]
"ShowUsrResults" // row[10]
2019-09-17 01:37:07 +02:00
" FROM mch_matches"
2019-09-14 12:59:34 +02:00
" WHERE MchCod=%ld"
" AND GamCod IN" // Extra check
2019-09-17 01:37:07 +02:00
" (SELECT GamCod FROM gam_games"
2019-09-14 12:59:34 +02:00
" WHERE CrsCod='%ld')",
Match->MchCod,
Gbl.Hierarchy.Crs.CrsCod);
if (NumRows) // Match found...
/***** Get match data from row *****/
Mch_GetMatchDataFromRow (mysql_res,Match);
else
{
/* Initialize to empty match */
Match->MchCod = -1L;
Match->GamCod = -1L;
Match->UsrCod = -1L;
2019-09-22 19:50:24 +02:00
for (StartEndTime = (Dat_StartEndTime_t) 0;
StartEndTime <= (Dat_StartEndTime_t) (Dat_NUM_START_END_TIME - 1);
StartEndTime++)
Match->TimeUTC[StartEndTime] = (time_t) 0;
2019-09-14 12:59:34 +02:00
Match->Title[0] = '\0';
Match->Status.QstInd = 0;
Match->Status.QstCod = -1L;
Match->Status.QstStartTimeUTC = (time_t) 0;
2019-09-16 00:15:40 +02:00
Match->Status.Showing = Mch_STEM;
2019-09-17 15:42:41 +02:00
Match->Status.Playing = false;
2019-09-25 21:58:12 +02:00
Match->Status.NumPlayers = 0;
Match->Status.ShowQstResults = false;
Match->Status.ShowUsrResults = false;
2019-09-14 12:59:34 +02:00
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
/*****************************************************************************/
2019-09-29 17:33:39 +02:00
/********************* Put icon to create a new match ************************/
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
2019-09-30 10:51:44 +02:00
static void Mch_PutIconToCreateNewMatch (void)
2019-09-14 12:59:34 +02:00
{
extern const char *Txt_New_match;
2019-09-29 17:33:39 +02:00
/***** Put form to create a new match *****/
2019-09-29 21:33:27 +02:00
Ico_PutContextualIconToAdd (ActReqNewMch,Mch_NEW_MATCH_SECTION_ID,Gam_PutParams,
2019-09-14 12:59:34 +02:00
Txt_New_match);
}
/*****************************************************************************/
/*********************** List game matches for edition ***********************/
/*****************************************************************************/
static void Mch_ListOneOrMoreMatches (struct Game *Game,
unsigned NumMatches,
MYSQL_RES *mysql_res)
2019-09-25 00:42:13 +02:00
{
unsigned NumMatch;
unsigned UniqueId;
struct Match Match;
2019-09-29 19:36:32 +02:00
bool ICanPlayThisMatchBasedOnGrps;
bool ICanEditMatches = Mch_CheckIfICanEditMatches ();
2019-09-25 00:42:13 +02:00
/***** Write the heading *****/
Tbl_StartTableWideMargin (2);
2019-09-29 19:36:32 +02:00
Mch_ListOneOrMoreMatchesHeading (ICanEditMatches);
2019-09-25 00:42:13 +02:00
/***** Write rows *****/
for (NumMatch = 0, UniqueId = 1;
NumMatch < NumMatches;
NumMatch++, UniqueId++)
{
Gbl.RowEvenOdd = NumMatch % 2;
/***** Get match data from row *****/
Mch_GetMatchDataFromRow (mysql_res,&Match);
2019-09-29 19:36:32 +02:00
ICanPlayThisMatchBasedOnGrps = Mch_CheckIfICanPlayThisMatchBasedOnGrps (Match.MchCod);
2019-09-25 00:42:13 +02:00
2019-09-25 09:29:44 +02:00
/***** Write row for this match ****/
fprintf (Gbl.F.Out,"<tr>");
2019-09-25 00:42:13 +02:00
2019-09-25 09:29:44 +02:00
/* Icons */
2019-09-29 19:36:32 +02:00
if (ICanEditMatches)
2019-09-29 18:44:40 +02:00
Mch_ListOneOrMoreMatchesIcons (&Match);
2019-09-25 00:42:13 +02:00
2019-09-25 09:29:44 +02:00
/* Match player */
Mch_ListOneOrMoreMatchesAuthor (&Match);
2019-09-25 00:42:13 +02:00
2019-09-25 09:29:44 +02:00
/* Start/end date/time */
2019-09-25 00:42:13 +02:00
Mch_ListOneOrMoreMatchesTimes (&Match,UniqueId);
2019-09-25 09:29:44 +02:00
/* Title and groups */
2019-09-25 00:42:13 +02:00
Mch_ListOneOrMoreMatchesTitleGrps (&Match);
2019-09-25 09:29:44 +02:00
/* Number of players who have answered any question in the match */
Mch_ListOneOrMoreMatchesNumPlayers (&Match);
2019-09-25 00:42:13 +02:00
2019-09-25 09:29:44 +02:00
/* Match status */
2019-09-29 19:36:32 +02:00
Mch_ListOneOrMoreMatchesStatus (&Match,Game->NumQsts,
ICanPlayThisMatchBasedOnGrps);
2019-09-25 00:42:13 +02:00
2019-09-25 09:29:44 +02:00
/* Match result visible? */
2019-09-29 19:36:32 +02:00
Mch_ListOneOrMoreMatchesResult (&Match,
ICanPlayThisMatchBasedOnGrps);
2019-09-25 00:42:13 +02:00
fprintf (Gbl.F.Out,"</tr>");
}
/***** End table *****/
Tbl_EndTable ();
}
/*****************************************************************************/
/***************** Put a column for match start and end times ****************/
/*****************************************************************************/
2019-09-29 19:36:32 +02:00
static void Mch_ListOneOrMoreMatchesHeading (bool ICanEditMatches)
2019-09-14 12:59:34 +02:00
{
extern const char *Txt_ROLES_SINGUL_Abc[Rol_NUM_ROLES][Usr_NUM_SEXS];
extern const char *Txt_START_END_TIME[Dat_NUM_START_END_TIME];
extern const char *Txt_Match;
2019-09-17 20:44:29 +02:00
extern const char *Txt_Players;
2019-09-14 12:59:34 +02:00
extern const char *Txt_Status;
2019-09-25 00:18:23 +02:00
extern const char *Txt_Result;
2019-09-14 12:59:34 +02:00
2019-09-29 18:44:40 +02:00
/***** Start row *****/
fprintf (Gbl.F.Out,"<tr>");
/***** Column for icons *****/
2019-09-29 19:36:32 +02:00
if (ICanEditMatches)
2019-09-29 18:44:40 +02:00
fprintf (Gbl.F.Out,"<th></th>");
/***** The rest of columns *****/
fprintf (Gbl.F.Out,"<th class=\"LEFT_TOP\">"
2019-09-14 12:59:34 +02:00
"%s"
"</th>"
"<th class=\"LEFT_TOP\">"
"%s"
"</th>"
"<th class=\"LEFT_TOP\">"
"%s"
"</th>"
"<th class=\"LEFT_TOP\">"
"%s"
"</th>"
"<th class=\"RIGHT_TOP\">"
"%s"
"</th>"
2019-09-25 21:58:12 +02:00
"<th class=\"CENTER_TOP\">"
2019-09-17 20:44:29 +02:00
"%s"
"</th>"
2019-09-25 21:58:12 +02:00
"<th class=\"CENTER_TOP\">"
2019-09-25 00:18:23 +02:00
"%s"
2019-09-29 18:44:40 +02:00
"</th>",
2019-09-14 12:59:34 +02:00
Txt_ROLES_SINGUL_Abc[Rol_TCH][Usr_SEX_UNKNOWN],
Txt_START_END_TIME[Gam_ORDER_BY_START_DATE],
Txt_START_END_TIME[Gam_ORDER_BY_END_DATE],
Txt_Match,
2019-09-17 20:44:29 +02:00
Txt_Players,
2019-09-25 00:18:23 +02:00
Txt_Status,
Txt_Result);
2019-09-29 18:44:40 +02:00
/***** End row *****/
fprintf (Gbl.F.Out,"</tr>");
}
/*****************************************************************************/
2019-09-29 23:40:33 +02:00
/*********************** Check if I can edit matches *************************/
2019-09-29 18:44:40 +02:00
/*****************************************************************************/
static bool Mch_CheckIfICanEditMatches (void)
{
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_NET:
case Rol_TCH:
case Rol_SYS_ADM:
return true;
default:
return false;
}
2019-09-25 00:42:13 +02:00
}
2019-09-14 12:59:34 +02:00
2019-09-29 23:40:33 +02:00
/*****************************************************************************/
2019-09-30 01:10:57 +02:00
/***************** Check if I can edit (remove/resume) a match ***************/
2019-09-29 23:40:33 +02:00
/*****************************************************************************/
2019-09-30 01:10:57 +02:00
static bool Mch_CheckIfICanEditThisMatch (const struct Match *Match)
2019-09-29 23:40:33 +02:00
{
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_NET:
return (Match->UsrCod == Gbl.Usrs.Me.UsrDat.UsrCod); // Only if I am the creator
case Rol_TCH:
case Rol_SYS_ADM:
return true;
default:
return false;
}
}
2019-09-25 09:29:44 +02:00
/*****************************************************************************/
/************************* Put a column for icons ****************************/
/*****************************************************************************/
static void Mch_ListOneOrMoreMatchesIcons (const struct Match *Match)
{
fprintf (Gbl.F.Out,"<td class=\"BT%u\">",Gbl.RowEvenOdd);
2019-09-29 17:33:39 +02:00
/***** Put icon to remove the match *****/
2019-09-30 01:10:57 +02:00
if (Mch_CheckIfICanEditThisMatch (Match))
2019-09-29 23:40:33 +02:00
{
Gam_SetParamCurrentGamCod (Match->GamCod); // Used to pass parameter
Mch_SetParamCurrentMchCod (Match->MchCod); // Used to pass parameter
Frm_StartForm (ActReqRemMch);
Mch_PutParamsEdit ();
Ico_PutIconRemove ();
Frm_EndForm ();
}
else
Ico_PutIconRemovalNotAllowed ();
2019-09-25 09:29:44 +02:00
fprintf (Gbl.F.Out,"</td>");
}
/*****************************************************************************/
/************* Put a column for teacher who created the match ****************/
/*****************************************************************************/
static void Mch_ListOneOrMoreMatchesAuthor (const struct Match *Match)
{
/***** Match author (teacher) *****/
fprintf (Gbl.F.Out,"<td class=\"LEFT_TOP COLOR%u\">",Gbl.RowEvenOdd);
Usr_WriteAuthor1Line (Match->UsrCod,false);
fprintf (Gbl.F.Out,"</td>");
}
2019-09-25 00:42:13 +02:00
/*****************************************************************************/
/***************** Put a column for match start and end times ****************/
/*****************************************************************************/
2019-09-14 12:59:34 +02:00
2019-09-25 00:42:13 +02:00
static void Mch_ListOneOrMoreMatchesTimes (const struct Match *Match,unsigned UniqueId)
{
extern const char *Txt_Today;
Dat_StartEndTime_t StartEndTime;
2019-09-14 12:59:34 +02:00
2019-09-25 00:42:13 +02:00
for (StartEndTime = (Dat_StartEndTime_t) 0;
StartEndTime <= (Dat_StartEndTime_t) (Dat_NUM_START_END_TIME - 1);
StartEndTime++)
{
fprintf (Gbl.F.Out,"<td id=\"mch_time_%u_%u\""
" class=\"%s LEFT_TOP COLOR%u\">",
(unsigned) StartEndTime,UniqueId,
Match->Status.QstInd >= Mch_AFTER_LAST_QUESTION ? "DATE_RED" :
"DATE_GREEN",
Gbl.RowEvenOdd);
fprintf (Gbl.F.Out,"<script type=\"text/javascript\">"
"writeLocalDateHMSFromUTC('mch_time_%u_%u',"
"%ld,%u,'<br />','%s',true,true,0x7);"
"</script>"
"</td>",
(unsigned) StartEndTime,UniqueId,
Match->TimeUTC[StartEndTime],
(unsigned) Gbl.Prefs.DateFormat,Txt_Today);
}
}
2019-09-14 12:59:34 +02:00
2019-09-25 00:42:13 +02:00
/*****************************************************************************/
/***************** Put a column for match title and grous ********************/
/*****************************************************************************/
2019-09-14 12:59:34 +02:00
2019-09-25 00:42:13 +02:00
static void Mch_ListOneOrMoreMatchesTitleGrps (const struct Match *Match)
{
fprintf (Gbl.F.Out,"<td class=\"LEFT_TOP COLOR%u\">",Gbl.RowEvenOdd);
2019-09-14 12:59:34 +02:00
2019-09-25 00:42:13 +02:00
/***** Title *****/
fprintf (Gbl.F.Out,"<span class=\"ASG_TITLE\">%s</span>",Match->Title);
2019-09-14 12:59:34 +02:00
2019-09-25 00:42:13 +02:00
/***** Groups whose students can answer this match *****/
if (Gbl.Crs.Grps.NumGrps)
Mch_GetAndWriteNamesOfGrpsAssociatedToMatch (Match);
2019-09-14 12:59:34 +02:00
2019-09-25 00:42:13 +02:00
fprintf (Gbl.F.Out,"</td>");
}
2019-09-14 12:59:34 +02:00
2019-09-25 00:42:13 +02:00
/*****************************************************************************/
/************* Get and write the names of the groups of a match **************/
/*****************************************************************************/
2019-09-14 12:59:34 +02:00
2019-09-25 00:42:13 +02:00
static void Mch_GetAndWriteNamesOfGrpsAssociatedToMatch (const struct Match *Match)
{
extern const char *Txt_Group;
extern const char *Txt_Groups;
extern const char *Txt_and;
extern const char *Txt_The_whole_course;
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned long NumRow;
unsigned long NumRows;
2019-09-14 12:59:34 +02:00
2019-09-25 00:42:13 +02:00
/***** Get groups associated to a match from database *****/
NumRows = DB_QuerySELECT (&mysql_res,"can not get groups of a match",
"SELECT crs_grp_types.GrpTypName,crs_grp.GrpName"
" FROM mch_groups,crs_grp,crs_grp_types"
" WHERE mch_groups.MchCod=%ld"
" AND mch_groups.GrpCod=crs_grp.GrpCod"
" AND crs_grp.GrpTypCod=crs_grp_types.GrpTypCod"
" ORDER BY crs_grp_types.GrpTypName,crs_grp.GrpName",
Match->MchCod);
2019-09-14 12:59:34 +02:00
2019-09-25 00:42:13 +02:00
/***** Write heading *****/
fprintf (Gbl.F.Out,"<div class=\"ASG_GRP\">%s: ",
NumRows == 1 ? Txt_Group :
Txt_Groups);
2019-09-17 20:44:29 +02:00
2019-09-25 00:42:13 +02:00
/***** 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);
2019-09-14 12:59:34 +02:00
2019-09-25 00:42:13 +02:00
/* Write group type name and group name */
fprintf (Gbl.F.Out,"%s %s",row[0],row[1]);
2019-09-25 00:18:23 +02:00
2019-09-25 00:42:13 +02:00
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,", ");
}
}
2019-09-14 12:59:34 +02:00
}
2019-09-25 00:42:13 +02:00
else
fprintf (Gbl.F.Out,"%s %s",
Txt_The_whole_course,Gbl.Hierarchy.Crs.ShrtName);
2019-09-14 12:59:34 +02:00
2019-09-25 00:42:13 +02:00
fprintf (Gbl.F.Out,"</div>");
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
2019-09-14 12:59:34 +02:00
}
2019-09-25 09:29:44 +02:00
/*****************************************************************************/
/******************* Put a column for number of players **********************/
/*****************************************************************************/
static void Mch_ListOneOrMoreMatchesNumPlayers (const struct Match *Match)
{
/***** Number of players who have answered any question in the match ******/
fprintf (Gbl.F.Out,"<td class=\"DAT RIGHT_TOP COLOR%u\">"
"%u"
"</td>",
Gbl.RowEvenOdd,
Mch_GetNumUsrsWhoHaveAnswerMch (Match->MchCod));
}
2019-09-25 00:28:57 +02:00
/*****************************************************************************/
/********************** Put a column for match status ************************/
/*****************************************************************************/
2019-09-29 19:36:32 +02:00
static void Mch_ListOneOrMoreMatchesStatus (const struct Match *Match,unsigned NumQsts,
bool ICanPlayThisMatchBasedOnGrps)
2019-09-25 00:28:57 +02:00
{
extern const char *Txt_Play;
extern const char *Txt_Resume;
2019-09-25 21:58:12 +02:00
fprintf (Gbl.F.Out,"<td class=\"DAT CENTER_TOP COLOR%u\">",Gbl.RowEvenOdd);
2019-09-25 00:28:57 +02:00
if (Match->Status.QstInd < Mch_AFTER_LAST_QUESTION) // Unfinished match
/* Current question index / total of questions */
fprintf (Gbl.F.Out,"<div class=\"DAT\">%u/%u</div>",
Match->Status.QstInd,NumQsts);
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_STD:
2019-09-29 19:36:32 +02:00
if (ICanPlayThisMatchBasedOnGrps)
2019-09-29 18:24:08 +02:00
{
/* Icon to play as student */
2019-09-29 22:38:32 +02:00
Mch_SetParamCurrentMchCod (Match->MchCod); // Used to pass parameter
2019-09-29 21:33:27 +02:00
Lay_PutContextualLinkOnlyIcon (ActJoiMch,NULL,
2019-09-29 22:38:32 +02:00
Mch_PutParamsPlay,
2019-09-29 18:24:08 +02:00
Match->Status.QstInd < Mch_AFTER_LAST_QUESTION ? "play.svg" :
"flag-checkered.svg",
Txt_Play);
}
2019-09-25 00:28:57 +02:00
break;
case Rol_NET:
case Rol_TCH:
case Rol_SYS_ADM:
/* Icon to resume */
2019-09-30 01:10:57 +02:00
if (Mch_CheckIfICanEditThisMatch (Match))
{
Mch_SetParamCurrentMchCod (Match->MchCod); // Used to pass parameter
Lay_PutContextualLinkOnlyIcon (ActResMch,NULL,
Mch_PutParamsPlay,
Match->Status.QstInd < Mch_AFTER_LAST_QUESTION ? "play.svg" :
"flag-checkered.svg",
Txt_Resume);
}
2019-09-25 00:28:57 +02:00
break;
default:
break;
}
fprintf (Gbl.F.Out,"</td>");
}
/*****************************************************************************/
/**************** Put a column for visibility of match result ****************/
/*****************************************************************************/
2019-09-29 19:36:32 +02:00
static void Mch_ListOneOrMoreMatchesResult (const struct Match *Match,
bool ICanPlayThisMatchBasedOnGrps)
2019-09-25 00:28:57 +02:00
{
2019-09-27 00:58:14 +02:00
extern const char *Txt_Match_result;
2019-09-25 00:28:57 +02:00
extern const char *Txt_Hidden_result;
2019-09-27 00:58:14 +02:00
extern const char *Txt_Visible_result;
2019-09-25 00:28:57 +02:00
2019-09-25 21:58:12 +02:00
fprintf (Gbl.F.Out,"<td class=\"DAT CENTER_TOP COLOR%u\">",Gbl.RowEvenOdd);
2019-09-25 00:28:57 +02:00
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_STD:
2019-09-29 19:36:32 +02:00
if (ICanPlayThisMatchBasedOnGrps)
2019-09-27 00:58:14 +02:00
{
2019-09-29 19:36:32 +02:00
/* Match result visible or hidden? */
if (Match->Status.ShowUsrResults)
{
2019-09-30 01:10:57 +02:00
/* Result is visible by me */
2019-09-29 22:38:32 +02:00
Gam_SetParamCurrentGamCod (Match->GamCod); // Used to pass parameter
Mch_SetParamCurrentMchCod (Match->MchCod); // Used to pass parameter
2019-09-29 19:36:32 +02:00
Frm_StartForm (ActSeeOneMchResMe);
2019-09-29 22:38:32 +02:00
Mch_PutParamsEdit ();
2019-09-29 19:36:32 +02:00
Ico_PutIconLink ("tasks.svg",Txt_Match_result);
Frm_EndForm ();
}
else
2019-09-30 01:10:57 +02:00
/* Result is forbidden to me */
2019-09-29 19:36:32 +02:00
Ico_PutIconOff ("eye-slash.svg",Txt_Hidden_result);
2019-09-27 00:58:14 +02:00
}
2019-09-25 00:28:57 +02:00
break;
case Rol_NET:
case Rol_TCH:
case Rol_SYS_ADM:
/* Match result visible or hidden? */
2019-09-30 01:10:57 +02:00
if (Mch_CheckIfICanEditThisMatch (Match))
{
/* I can edit visibility */
Gam_SetParamCurrentGamCod (Match->GamCod); // Used to pass parameter
Mch_SetParamCurrentMchCod (Match->MchCod); // Used to pass parameter
Lay_PutContextualLinkOnlyIcon (ActChgVisResMchUsr,NULL,
Mch_PutParamsEdit,
Match->Status.ShowUsrResults ? "eye.svg" :
"eye-slash.svg",
Match->Status.ShowUsrResults ? Txt_Visible_result :
Txt_Hidden_result);
}
else
/* I can not edit visibility */
Ico_PutIconOff (Match->Status.ShowUsrResults ? "eye.svg" :
"eye-slash.svg",
Match->Status.ShowUsrResults ? Txt_Visible_result :
Txt_Hidden_result);
2019-09-25 00:28:57 +02:00
break;
default:
break;
}
fprintf (Gbl.F.Out,"</td>");
}
2019-09-25 21:58:12 +02:00
/*****************************************************************************/
/******************** Toggle visibility of match results *********************/
/*****************************************************************************/
void Mch_ToggleVisibilResultsMchUsr (void)
{
2019-09-29 22:38:32 +02:00
struct Game Game;
2019-09-25 21:58:12 +02:00
struct Match Match;
2019-09-29 22:38:32 +02:00
/***** Get and check parameters *****/
Mch_GetAndCheckParameters (&Game,&Match);
2019-09-25 21:58:12 +02:00
2019-09-30 01:10:57 +02:00
/***** Check if I have permission to change visibility *****/
if (!Mch_CheckIfICanEditThisMatch (&Match))
Act_NoPermissionExit ();
2019-09-25 21:58:12 +02:00
/***** Toggle visibility of match results *****/
Match.Status.ShowUsrResults = !Match.Status.ShowUsrResults;
DB_QueryUPDATE ("can not toggle visibility of match results",
"UPDATE mch_matches"
" SET ShowUsrResults='%c'"
" WHERE MchCod=%ld",
Match.Status.ShowUsrResults ? 'Y' :
'N',
Match.MchCod);
/***** Show current game *****/
Gam_ShowOneGame (Match.GamCod,
true, // Show only this game
false, // Do not list game questions
false); // Do not put form to start new match
}
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
/******************** Get game data from a database row **********************/
/*****************************************************************************/
static void Mch_GetMatchDataFromRow (MYSQL_RES *mysql_res,
struct Match *Match)
{
MYSQL_ROW row;
2019-09-22 19:50:24 +02:00
Dat_StartEndTime_t StartEndTime;
2019-09-14 12:59:34 +02:00
/***** Get match data *****/
row = mysql_fetch_row (mysql_res);
/*
2019-09-25 21:58:12 +02:00
row[ 0] MchCod
row[ 1] GamCod
row[ 2] UsrCod
row[ 3] UNIX_TIMESTAMP(StartTime)
row[ 4] UNIX_TIMESTAMP(EndTime)
row[ 5] Title
2019-09-14 12:59:34 +02:00
*/
/***** Get match data *****/
/* Code of the match (row[0]) */
if ((Match->MchCod = Str_ConvertStrCodToLongCod (row[0])) <= 0)
Lay_ShowErrorAndExit ("Wrong code of match.");
/* Code of the game (row[1]) */
if ((Match->GamCod = Str_ConvertStrCodToLongCod (row[1])) <= 0)
Lay_ShowErrorAndExit ("Wrong code of game.");
/* Get match teacher (row[2]) */
Match->UsrCod = Str_ConvertStrCodToLongCod (row[2]);
2019-09-22 19:50:24 +02:00
/* Get start/end times (row[3], row[4] hold start/end UTC times) */
for (StartEndTime = (Dat_StartEndTime_t) 0;
StartEndTime <= (Dat_StartEndTime_t) (Dat_NUM_START_END_TIME - 1);
StartEndTime++)
Match->TimeUTC[StartEndTime] = Dat_GetUNIXTimeFromStr (row[3 + StartEndTime]);
2019-09-14 12:59:34 +02:00
/* Get the title of the game (row[5]) */
if (row[5])
Str_Copy (Match->Title,row[5],
Gam_MAX_BYTES_TITLE);
else
Match->Title[0] = '\0';
/***** Get current match status *****/
/*
2019-09-25 21:58:12 +02:00
row[ 6] QstInd
row[ 7] QstCod
row[ 8] Showing
row[ 9] ShowQstResults
row[10] ShowUsrResults
2019-09-14 12:59:34 +02:00
*/
/* Current question index (row[6]) */
Match->Status.QstInd = Gam_GetQstIndFromStr (row[6]);
/* Current question code (row[7]) */
Match->Status.QstCod = Str_ConvertStrCodToLongCod (row[7]);
2019-09-25 21:58:12 +02:00
/* Get what to show (stem, answers, results) (row(8)) */
Match->Status.Showing = Mch_GetShowingFromStr (row[8]);
2019-09-14 12:59:34 +02:00
2019-09-25 21:58:12 +02:00
/* Get whether to show question results or not (row(9)) */
Match->Status.ShowQstResults = (row[9][0] == 'Y');
2019-09-25 23:44:30 +02:00
/* Get whether to show user results or not (row(10)) */
2019-09-25 21:58:12 +02:00
Match->Status.ShowUsrResults = (row[10][0] == 'Y');
2019-09-14 12:59:34 +02:00
/***** Get whether the match is being played or not *****/
if (Match->Status.QstInd >= Mch_AFTER_LAST_QUESTION) // Finished
2019-09-17 15:42:41 +02:00
Match->Status.Playing = false;
2019-09-14 12:59:34 +02:00
else // Unfinished
2019-09-17 15:42:41 +02:00
Match->Status.Playing = Mch_GetIfMatchIsBeingPlayed (Match->MchCod);
2019-09-14 12:59:34 +02:00
}
/*****************************************************************************/
/****************** Get parameter with what is being shown *******************/
/*****************************************************************************/
static Mch_Showing_t Mch_GetShowingFromStr (const char *Str)
{
Mch_Showing_t Showing;
for (Showing = (Mch_Showing_t) 0;
Showing <= (Mch_Showing_t) (Mch_NUM_SHOWING - 1);
Showing++)
if (!strcmp (Str,Mch_ShowingStringsDB[Showing]))
return Showing;
return (Mch_Showing_t) Mch_SHOWING_DEFAULT;
}
/*****************************************************************************/
/************** Request the removal of a match (game instance) ***************/
/*****************************************************************************/
2019-09-29 21:33:27 +02:00
void Mch_RequestRemoveMatch (void)
2019-09-14 12:59:34 +02:00
{
extern const char *Txt_Do_you_really_want_to_remove_the_match_X;
extern const char *Txt_Remove_match;
2019-09-29 22:38:32 +02:00
struct Game Game;
2019-09-14 12:59:34 +02:00
struct Match Match;
2019-09-29 22:38:32 +02:00
/***** Get and check parameters *****/
Mch_GetAndCheckParameters (&Game,&Match);
2019-09-14 12:59:34 +02:00
/***** Show question and button to remove question *****/
2019-09-29 22:38:32 +02:00
Gam_SetParamCurrentGamCod (Match.GamCod); // Used to pass parameter
Mch_SetParamCurrentMchCod (Match.MchCod); // Used to pass parameter
Ale_ShowAlertAndButton (ActRemMch,NULL,NULL,Mch_PutParamsEdit,
2019-09-14 12:59:34 +02:00
Btn_REMOVE_BUTTON,Txt_Remove_match,
Ale_QUESTION,Txt_Do_you_really_want_to_remove_the_match_X,
Match.Title);
/***** Show current game *****/
Gam_ShowOneGame (Match.GamCod,
true, // Show only this game
false, // Do not list game questions
false); // Do not put form to start new match
}
/*****************************************************************************/
/********************** Remove a match (game instance) ***********************/
/*****************************************************************************/
2019-09-29 21:33:27 +02:00
void Mch_RemoveMatch (void)
2019-09-14 12:59:34 +02:00
{
extern const char *Txt_Match_X_removed;
2019-09-27 20:00:47 +02:00
struct Game Game;
2019-09-29 22:38:32 +02:00
struct Match Match;
2019-09-14 12:59:34 +02:00
2019-09-29 22:38:32 +02:00
/***** Get and check parameters *****/
Mch_GetAndCheckParameters (&Game,&Match);
2019-09-27 20:00:47 +02:00
2019-09-29 23:40:33 +02:00
/***** Check if I can remove this match *****/
2019-09-30 01:10:57 +02:00
if (!Mch_CheckIfICanEditThisMatch (&Match))
Act_NoPermissionExit ();
2019-09-29 23:40:33 +02:00
2019-09-27 20:00:47 +02:00
/***** Remove the match from all database tables *****/
Mch_RemoveMatchFromAllTables (Match.MchCod);
2019-09-14 12:59:34 +02:00
/***** Write message *****/
Ale_ShowAlert (Ale_SUCCESS,Txt_Match_X_removed,
Match.Title);
/***** Show current game *****/
Gam_ShowOneGame (Match.GamCod,
true, // Show only this game
false, // Do not list game questions
false); // Do not put form to start new match
}
2019-09-27 20:00:47 +02:00
/*****************************************************************************/
/********************** Remove match from all tables *************************/
/*****************************************************************************/
2019-09-29 23:13:20 +02:00
/*
mysql> SELECT table_name FROM information_schema.tables WHERE table_name LIKE 'mch%';
*/
2019-09-27 20:00:47 +02:00
static void Mch_RemoveMatchFromAllTables (long MchCod)
{
2019-09-28 01:12:53 +02:00
/***** Remove match from secondary tables *****/
2019-09-29 23:13:20 +02:00
Mch_RemoveMatchFromTable (MchCod,"mch_players");
Mch_RemoveMatchFromTable (MchCod,"mch_playing");
Mch_RemoveMatchFromTable (MchCod,"mch_results");
Mch_RemoveMatchFromTable (MchCod,"mch_answers");
Mch_RemoveMatchFromTable (MchCod,"mch_times");
Mch_RemoveMatchFromTable (MchCod,"mch_groups");
Mch_RemoveMatchFromTable (MchCod,"mch_indexes");
2019-09-28 01:12:53 +02:00
/***** Remove match from main table *****/
DB_QueryDELETE ("can not remove match",
"DELETE FROM mch_matches WHERE MchCod=%ld",
MchCod);
2019-09-27 20:00:47 +02:00
}
2019-09-29 23:13:20 +02:00
static void Mch_RemoveMatchFromTable (long MchCod,const char *TableName)
{
/***** Remove match from secondary table *****/
DB_QueryDELETE ("can not remove match from table",
"DELETE FROM %s WHERE MchCod=%ld",
TableName,
MchCod);
}
2019-09-27 21:45:53 +02:00
/*****************************************************************************/
/******************** Remove match in game from all tables *******************/
/*****************************************************************************/
2019-09-28 01:12:53 +02:00
void Mch_RemoveMatchesInGameFromAllTables (long GamCod)
{
/***** Remove matches from secondary tables *****/
2019-09-29 23:13:20 +02:00
Mch_RemoveMatchesInGameFromTable (GamCod,"mch_players");
Mch_RemoveMatchesInGameFromTable (GamCod,"mch_playing");
Mch_RemoveMatchesInGameFromTable (GamCod,"mch_results");
Mch_RemoveMatchesInGameFromTable (GamCod,"mch_answers");
Mch_RemoveMatchesInGameFromTable (GamCod,"mch_times");
Mch_RemoveMatchesInGameFromTable (GamCod,"mch_groups");
Mch_RemoveMatchesInGameFromTable (GamCod,"mch_indexes");
2019-09-28 01:12:53 +02:00
/***** Remove matches from main table *****/
DB_QueryDELETE ("can not remove matches of a game",
"DELETE FROM mch_matches WHERE GamCod=%ld",
GamCod);
}
2019-09-29 23:13:20 +02:00
static void Mch_RemoveMatchesInGameFromTable (long GamCod,const char *TableName)
{
/***** Remove matches in game from secondary table *****/
DB_QueryDELETE ("can not remove matches of a game from table",
"DELETE FROM %s"
" USING mch_matches,%s"
" WHERE mch_matches.GamCod=%ld"
" AND mch_matches.MchCod=%s.MchCod",
TableName,
TableName,
GamCod,
TableName);
}
2019-09-28 01:12:53 +02:00
/*****************************************************************************/
/******************* Remove match in course from all tables ******************/
/*****************************************************************************/
void Mch_RemoveMatchInCourseFromAllTables (long CrsCod)
2019-09-27 21:45:53 +02:00
{
2019-09-28 01:12:53 +02:00
/***** Remove matches from secondary tables *****/
2019-09-29 23:13:20 +02:00
Mch_RemoveMatchInCourseFromTable (CrsCod,"mch_players");
Mch_RemoveMatchInCourseFromTable (CrsCod,"mch_playing");
Mch_RemoveMatchInCourseFromTable (CrsCod,"mch_results");
Mch_RemoveMatchInCourseFromTable (CrsCod,"mch_answers");
Mch_RemoveMatchInCourseFromTable (CrsCod,"mch_times");
Mch_RemoveMatchInCourseFromTable (CrsCod,"mch_groups");
Mch_RemoveMatchInCourseFromTable (CrsCod,"mch_indexes");
2019-09-28 01:12:53 +02:00
/***** Remove matches from main table *****/
DB_QueryDELETE ("can not remove matches of a course from table",
"DELETE FROM %s"
" USING gam_games,mch_matches"
" WHERE gam_games.CrsCod=%ld"
" AND gam_games.GamCod=mch_matches.GamCod",
CrsCod);
2019-09-27 21:45:53 +02:00
}
2019-09-29 23:13:20 +02:00
static void Mch_RemoveMatchInCourseFromTable (long CrsCod,const char *TableName)
2019-09-27 21:45:53 +02:00
{
2019-09-29 23:13:20 +02:00
/***** Remove matches in course from secondary table *****/
DB_QueryDELETE ("can not remove matches of a course from table",
2019-09-27 21:45:53 +02:00
"DELETE FROM %s"
2019-09-29 23:13:20 +02:00
" USING gam_games,mch_matches,%s"
" WHERE gam_games.CrsCod=%ld"
" AND gam_games.GamCod=mch_matches.GamCod"
2019-09-27 21:45:53 +02:00
" AND mch_matches.MchCod=%s.MchCod",
TableName,
TableName,
2019-09-29 23:13:20 +02:00
CrsCod,
2019-09-27 21:45:53 +02:00
TableName);
2019-09-27 20:00:47 +02:00
}
2019-09-28 01:12:53 +02:00
/*****************************************************************************/
2019-09-29 23:13:20 +02:00
/***************** Remove user from secondary match tables *******************/
2019-09-28 01:12:53 +02:00
/*****************************************************************************/
2019-09-29 23:13:20 +02:00
void Mch_RemoveUsrFromMatchTablesInCrs (long UsrCod,long CrsCod)
{
/***** Remove student from secondary tables *****/
Mch_RemoveUsrMchResultsInCrs (UsrCod,CrsCod,"mch_players");
Mch_RemoveUsrMchResultsInCrs (UsrCod,CrsCod,"mch_results");
Mch_RemoveUsrMchResultsInCrs (UsrCod,CrsCod,"mch_answers");
}
static void Mch_RemoveUsrMchResultsInCrs (long UsrCod,long CrsCod,const char *TableName)
2019-09-28 01:12:53 +02:00
{
/***** Remove matches in course from secondary table *****/
2019-09-29 23:13:20 +02:00
DB_QueryDELETE ("can not remove matches of a user from table",
2019-09-28 01:12:53 +02:00
"DELETE FROM %s"
" USING gam_games,mch_matches,%s"
" WHERE gam_games.CrsCod=%ld"
" AND gam_games.GamCod=mch_matches.GamCod"
2019-09-29 23:13:20 +02:00
" AND mch_matches.MchCod=%s.MchCod"
" AND %s.UsrCod=%ld",
2019-09-28 01:12:53 +02:00
TableName,
TableName,
CrsCod,
2019-09-29 23:13:20 +02:00
TableName,
TableName,
UsrCod);
2019-09-28 01:12:53 +02:00
}
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
2019-09-29 17:33:39 +02:00
/*********************** Params used to edit a match *************************/
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
2019-09-30 01:10:57 +02:00
void Mch_PutParamsEdit (void)
2019-09-14 12:59:34 +02:00
{
2019-09-29 22:38:32 +02:00
Gam_PutParams ();
Mch_PutParamsPlay ();
}
/*****************************************************************************/
/*********************** Params used to edit a match *************************/
/*****************************************************************************/
static void Mch_PutParamsPlay (void)
{
long CurrentMchCod = Mch_GetParamCurrentMchCod ();
if (CurrentMchCod > 0)
Mch_PutParamMchCod (CurrentMchCod);
2019-09-14 12:59:34 +02:00
}
/*****************************************************************************/
/******************** Write parameter with code of match **********************/
/*****************************************************************************/
2019-09-30 01:10:57 +02:00
static void Mch_PutParamMchCod (long MchCod)
2019-09-14 12:59:34 +02:00
{
Par_PutHiddenParamLong ("MchCod",MchCod);
}
2019-09-29 22:38:32 +02:00
/*****************************************************************************/
/************************** Get and check parameters *************************/
/*****************************************************************************/
void Mch_GetAndCheckParameters (struct Game *Game,struct Match *Match)
{
/***** Get parameters *****/
/* Get parameters of game */
if ((Game->GamCod = Gam_GetParams ()) == -1L)
Lay_ShowErrorAndExit ("Code of game is missing.");
Gam_GetDataOfGameByCod (Game);
/* Get match code */
if ((Match->MchCod = Mch_GetParamMchCod ()) == -1L)
Lay_ShowErrorAndExit ("Code of match is missing.");
Mch_GetDataOfMatchByCod (Match);
/***** Ensure parameters are correct *****/
if (Game->GamCod != Match->GamCod)
Lay_ShowErrorAndExit ("Wrong game code.");
if (Game->CrsCod != Gbl.Hierarchy.Crs.CrsCod)
Lay_ShowErrorAndExit ("Match does not belong to this course.");
}
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
/********************* Get parameter with code of match **********************/
/*****************************************************************************/
2019-09-28 02:31:42 +02:00
long Mch_GetParamMchCod (void)
2019-09-14 12:59:34 +02:00
{
/***** Get code of match *****/
return Par_GetParToLong ("MchCod");
}
/*****************************************************************************/
/****** Put a big button to play match (start a new match) as a teacher ******/
/*****************************************************************************/
static void Mch_PutFormNewMatch (struct Game *Game)
{
extern const char *Hlp_ASSESSMENT_Games_new_match;
extern const char *The_ClassFormInBox[The_NUM_THEMES];
extern const char *Txt_New_match;
extern const char *Txt_Title;
extern const char *Txt_Play;
/***** Start section for a new match *****/
Lay_StartSection (Mch_NEW_MATCH_SECTION_ID);
/***** Start form *****/
2019-09-29 21:33:27 +02:00
Frm_StartForm (ActNewMch);
2019-09-14 12:59:34 +02:00
Gam_PutParamGameCod (Game->GamCod);
Gam_PutParamQstInd (0); // Start by first question in game
/***** Start box and table *****/
Box_StartBoxTable (NULL,Txt_New_match,NULL,
Hlp_ASSESSMENT_Games_new_match,Box_NOT_CLOSABLE,2);
/***** Match title *****/
fprintf (Gbl.F.Out,"<tr>"
"<td class=\"RIGHT_MIDDLE\">"
"<label for=\"Title\" class=\"%s\">%s:</label>"
"</td>"
"<td class=\"LEFT_MIDDLE\">"
"<input type=\"text\" id=\"Title\" name=\"Title\""
" size=\"45\" maxlength=\"%u\" value=\"%s\""
" required=\"required\" />"
"</td>"
"</tr>",
The_ClassFormInBox[Gbl.Prefs.Theme],
Txt_Title,
Gam_MAX_CHARS_TITLE,Game->Title);
/***** Groups *****/
Mch_ShowLstGrpsToCreateMatch ();
/***** End table *****/
Tbl_EndTable ();
/***** Put icon with link *****/
Frm_LinkFormSubmit (Txt_Play,NULL,NULL);
fprintf (Gbl.F.Out,"<img src=\"%s/play.svg\""
" alt=\"%s\" title=\"%s\""
" class=\"CONTEXT_OPT ICO_HIGHLIGHT ICO64x64\" />",
Cfg_URL_ICON_PUBLIC,Txt_Play,Txt_Play);
fprintf (Gbl.F.Out,"</a>");
/***** End box *****/
Box_EndBox ();
/***** End form *****/
Frm_EndForm ();
/***** End section for a new match *****/
Lay_EndSection ();
}
/*****************************************************************************/
/***************** Show list of groups to create a new match *****************/
/*****************************************************************************/
static void Mch_ShowLstGrpsToCreateMatch (void)
{
extern const char *The_ClassFormInBox[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.Crs.Grps.GrpTypes.Num)
{
/***** Start box and table *****/
fprintf (Gbl.F.Out,"<tr>"
"<td class=\"%s RIGHT_TOP\">"
"%s:"
"</td>"
"<td class=\"LEFT_TOP\">",
The_ClassFormInBox[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\""
" checked=\"checked\""
" onclick=\"uncheckChildren(this,'GrpCods')\" />"
"%s %s"
"</label>"
"</td>"
"</tr>",
Txt_The_whole_course,Gbl.Hierarchy.Crs.ShrtName);
/***** List the groups for each group type *****/
for (NumGrpTyp = 0;
NumGrpTyp < Gbl.Crs.Grps.GrpTypes.Num;
NumGrpTyp++)
if (Gbl.Crs.Grps.GrpTypes.LstGrpTypes[NumGrpTyp].NumGrps)
Grp_ListGrpsToEditAsgAttSvyMch (&Gbl.Crs.Grps.GrpTypes.LstGrpTypes[NumGrpTyp],
-1L, // -1 means "New match"
Grp_MATCH);
/***** End table and box *****/
Box_EndBoxTable ();
fprintf (Gbl.F.Out,"</td>"
"</tr>");
}
/***** Free list of groups types and groups in this course *****/
Grp_FreeListGrpTypesAndGrps ();
}
/*****************************************************************************/
/********************* Create a new match (by a teacher) *********************/
/*****************************************************************************/
void Mch_CreateNewMatchTch (void)
{
long GamCod;
char Title[Gam_MAX_BYTES_TITLE + 1];
/***** Get form parameters *****/
/* Get match code */
if ((GamCod = Gam_GetParamGameCod ()) == -1L)
Lay_ShowErrorAndExit ("Code of game is missing.");
/* Get match title */
Par_GetParToText ("Title",Title,Gam_MAX_BYTES_TITLE);
/* Get groups for this games */
Grp_GetParCodsSeveralGrps ();
/***** Create a new match *****/
Gbl.Games.MchCodBeingPlayed = Mch_CreateMatch (GamCod,Title);
/***** Free memory for list of selected groups *****/
Grp_FreeListCodSelectedGrps ();
}
/*****************************************************************************/
/******* Show button to actually start / resume a match (by a teacher) *******/
/*****************************************************************************/
2019-09-29 21:33:27 +02:00
void Mch_ResumeMatch (void)
2019-09-14 12:59:34 +02:00
{
struct Match Match;
/***** Remove old players.
2019-10-01 21:18:53 +02:00
This function must be called by a teacher
before getting match status. *****/
2019-09-14 12:59:34 +02:00
Mch_RemoveOldPlayers ();
/***** Get data of the match from database *****/
Match.MchCod = Gbl.Games.MchCodBeingPlayed;
Mch_GetDataOfMatchByCod (&Match);
2019-09-30 01:10:57 +02:00
/***** Check if I have permission to resume match *****/
if (!Mch_CheckIfICanEditThisMatch (&Match))
Act_NoPermissionExit ();
2019-09-14 12:59:34 +02:00
/***** Update match status in database *****/
Mch_UpdateMatchStatusInDB (&Match);
/***** Show current match status *****/
2019-09-26 18:37:10 +02:00
fprintf (Gbl.F.Out,"<div id=\"match\" class=\"MATCH_CONT\">");
2019-09-14 12:59:34 +02:00
Mch_ShowMatchStatusForTch (&Match);
fprintf (Gbl.F.Out,"</div>");
}
/*****************************************************************************/
/********************** Create a new match in a game *************************/
/*****************************************************************************/
static long Mch_CreateMatch (long GamCod,char Title[Gam_MAX_BYTES_TITLE + 1])
{
long MchCod;
/***** Insert this new match into database *****/
MchCod = DB_QueryINSERTandReturnCode ("can not create match",
2019-09-17 23:35:30 +02:00
"INSERT mch_matches "
"(GamCod,UsrCod,StartTime,EndTime,Title,"
2019-09-25 21:58:12 +02:00
"QstInd,QstCod,Showing,"
"ShowQstResults,ShowUsrResults)"
2019-09-17 23:35:30 +02:00
" VALUES "
"(%ld," // GamCod
2019-09-14 12:59:34 +02:00
"%ld," // UsrCod
"NOW()," // StartTime
"NOW()," // EndTime
"'%s'," // Title
"0," // QstInd: Match has not started, so not the first question yet
"-1," // QstCod: Non-existent question
2019-09-25 21:58:12 +02:00
"'%s'," // Showing: What is being shown
2019-09-26 01:25:43 +02:00
"'N'," // ShowQstResults: Don't show question results initially
2019-09-25 21:58:12 +02:00
"'N')", // ShowUsrResults: Don't show user results initially
2019-09-14 12:59:34 +02:00
GamCod,
Gbl.Usrs.Me.UsrDat.UsrCod, // Game creator
Title,
Mch_ShowingStringsDB[Mch_SHOWING_DEFAULT]);
2019-09-23 21:39:54 +02:00
/***** Create indexes for answers *****/
Mch_CreateIndexes (GamCod,MchCod);
2019-09-14 12:59:34 +02:00
/***** Create groups associated to the match *****/
if (Gbl.Crs.Grps.LstGrpsSel.NumGrps)
Mch_CreateGrps (MchCod);
return MchCod;
}
2019-09-23 21:39:54 +02:00
/*****************************************************************************/
/*********************** Create indexes for a match **************************/
/*****************************************************************************/
/* Everytime a new match is created,
the answers of each shufflable question are shuffled.
The shuffling is stored in a table of indexes
that will be read when showing a match */
static void Mch_CreateIndexes (long GamCod,long MchCod)
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumQsts;
unsigned NumQst;
long QstCod;
long LongNum;
unsigned QstInd;
Tst_AnswerType_t AnswerType;
bool Shuffle;
/***** Get questions of the game *****/
NumQsts = (unsigned)
DB_QuerySELECT (&mysql_res,"can not get questions of a game",
"SELECT gam_questions.QstCod," // row[0]
"gam_questions.QstInd," // row[1]
2019-09-24 01:41:51 +02:00
"tst_questions.AnsType," // row[2]
2019-09-23 21:39:54 +02:00
"tst_questions.Shuffle" // row[3]
" FROM gam_questions,tst_questions"
" WHERE gam_questions.GamCod=%ld"
" AND gam_questions.QstCod=tst_questions.QstCod"
" ORDER BY gam_questions.QstInd",
GamCod);
/***** For each question in game... *****/
for (NumQst = 0;
NumQst < NumQsts;
NumQst++)
{
/***** Get question data *****/
row = mysql_fetch_row (mysql_res);
/* Get question code (row[0]) */
if ((QstCod = Str_ConvertStrCodToLongCod (row[0])) < 0)
Lay_ShowErrorAndExit ("Wrong code of question.");
/* Get question index (row[1]) */
if ((LongNum = Str_ConvertStrCodToLongCod (row[1])) < 0)
Lay_ShowErrorAndExit ("Wrong question index.");
QstInd = (unsigned) LongNum;
/* Get answer type (row[2]) */
AnswerType = Tst_ConvertFromStrAnsTypDBToAnsTyp (row[2]);
if (AnswerType != Tst_ANS_UNIQUE_CHOICE)
Lay_ShowErrorAndExit ("Wrong answer type.");
/* Get shuffle (row[3]) */
Shuffle = (row[3][0] == 'Y');
/***** Reorder answer *****/
Mch_ReorderAnswer (MchCod,QstInd,QstCod,Shuffle);
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
/*****************************************************************************/
/******************* Reorder answers of a match question *********************/
/*****************************************************************************/
static void Mch_ReorderAnswer (long MchCod,unsigned QstInd,
long QstCod,bool Shuffle)
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumAnss;
unsigned NumAns;
long LongNum;
unsigned AnsInd;
char StrOneAnswer[10 + 1];
char StrAnswersOneQst[Tst_MAX_BYTES_ANSWERS_ONE_QST + 1];
/***** Initialize list of answers to empty string *****/
StrAnswersOneQst[0] = '\0';
/***** Get questions of the game *****/
NumAnss = (unsigned)
DB_QuerySELECT (&mysql_res,"can not get questions of a game",
"SELECT AnsInd" // row[0]
" FROM tst_answers"
2019-09-24 01:41:51 +02:00
" WHERE QstCod=%ld"
2019-09-23 21:39:54 +02:00
" ORDER BY %s",
QstCod,
2019-09-24 01:41:51 +02:00
Shuffle ? "RAND()" : // Use RAND() because is really random; RAND(NOW()) repeats order
2019-09-23 21:39:54 +02:00
"AnsInd");
/***** For each answer in question... *****/
for (NumAns = 0;
NumAns < NumAnss;
NumAns++)
{
row = mysql_fetch_row (mysql_res);
/* Get answer index (row[0]) */
if ((LongNum = Str_ConvertStrCodToLongCod (row[0])) < 0)
Lay_ShowErrorAndExit ("Wrong answer index.");
AnsInd = (unsigned) LongNum;
2019-09-24 01:41:51 +02:00
snprintf (StrOneAnswer,sizeof (StrOneAnswer),
"%u",AnsInd);
2019-09-23 21:39:54 +02:00
/* Concatenate answer index to list of answers */
if (NumAns)
Str_Concat (StrAnswersOneQst,",",
Tst_MAX_BYTES_ANSWERS_ONE_QST);
Str_Concat (StrAnswersOneQst,StrOneAnswer,
Tst_MAX_BYTES_ANSWERS_ONE_QST);
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
/***** Create entry for this question in table of match indexes *****/
DB_QueryINSERT ("can not create match indexes",
"INSERT INTO mch_indexes"
" (MchCod,QstInd,Indexes)"
" VALUES"
" (%ld,%u,'%s')",
MchCod,QstInd,StrAnswersOneQst);
}
2019-09-24 01:41:51 +02:00
/*****************************************************************************/
/***************** Get indexes for a question from database ******************/
/*****************************************************************************/
void Mch_GetIndexes (long MchCod,unsigned QstInd,
unsigned Indexes[Tst_MAX_OPTIONS_PER_QUESTION])
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
char StrIndexesOneQst[Tst_MAX_BYTES_INDEXES_ONE_QST + 1];
/***** Get indexes for a question from database *****/
if (!DB_QuerySELECT (&mysql_res,"can not get data of a question",
"SELECT Indexes" // row[0]
" FROM mch_indexes"
" WHERE MchCod=%ld AND QstInd=%u",
MchCod,QstInd))
Lay_ShowErrorAndExit ("No indexes found for a question.");
row = mysql_fetch_row (mysql_res);
/* Get indexes (row[0]) */
Str_Copy (StrIndexesOneQst,row[0],
Tst_MAX_BYTES_INDEXES_ONE_QST);
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
/***** Get indexes from string *****/
Par_ReplaceCommaBySeparatorMultiple (StrIndexesOneQst);
Tst_GetIndexesFromStr (StrIndexesOneQst,Indexes);
}
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
/******************* Create groups associated to a match *********************/
/*****************************************************************************/
static void Mch_CreateGrps (long MchCod)
{
unsigned NumGrpSel;
/***** Create groups associated to the match *****/
for (NumGrpSel = 0;
NumGrpSel < Gbl.Crs.Grps.LstGrpsSel.NumGrps;
NumGrpSel++)
/* Create group */
DB_QueryINSERT ("can not associate a group to a match",
2019-09-17 01:37:07 +02:00
"INSERT INTO mch_groups"
2019-09-14 12:59:34 +02:00
" (MchCod,GrpCod)"
" VALUES"
" (%ld,%ld)",
MchCod,Gbl.Crs.Grps.LstGrpsSel.GrpCods[NumGrpSel]);
}
2019-09-27 20:22:32 +02:00
/*****************************************************************************/
/********************* Remove one group from all matches *********************/
/*****************************************************************************/
void Mch_RemoveGroup (long GrpCod)
{
/***** Remove group from all the matches *****/
DB_QueryDELETE ("can not remove group"
" from the associations between matches and groups",
"DELETE FROM mch_groups WHERE GrpCod=%ld",
GrpCod);
}
/*****************************************************************************/
/***************** Remove groups of one type from all matches ****************/
/*****************************************************************************/
void Mch_RemoveGroupsOfType (long GrpTypCod)
{
/***** Remove group from all the matches *****/
DB_QueryDELETE ("can not remove groups of a type"
" from the associations between matches and groups",
"DELETE FROM mch_groups"
" USING crs_grp,mch_groups"
" WHERE crs_grp.GrpTypCod=%ld"
" AND crs_grp.GrpCod=mch_groups.GrpCod",
GrpTypCod);
}
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
/***************** Insert/update a game match being played *******************/
/*****************************************************************************/
2019-09-17 21:01:57 +02:00
#define Mch_MAX_BYTES_SUBQUERY 128
2019-09-14 12:59:34 +02:00
static void Mch_UpdateMatchStatusInDB (struct Match *Match)
{
2019-09-17 21:01:57 +02:00
char MchSubQuery[Mch_MAX_BYTES_SUBQUERY];
/***** Update end time only if match is currently being played *****/
if (Match->Status.Playing)
Str_Copy (MchSubQuery,"mch_matches.EndTime=NOW(),",
Mch_MAX_BYTES_SUBQUERY);
else
MchSubQuery[0] = '\0';
2019-09-14 12:59:34 +02:00
/***** Update match status in database *****/
DB_QueryUPDATE ("can not update match being played",
2019-09-17 01:37:07 +02:00
"UPDATE mch_matches,gam_games"
2019-09-17 21:01:57 +02:00
" SET %s"
2019-09-17 01:37:07 +02:00
"mch_matches.QstInd=%u,"
"mch_matches.QstCod=%ld,"
2019-09-25 21:58:12 +02:00
"mch_matches.Showing='%s',"
"mch_matches.ShowQstResults='%c'"
2019-09-17 01:37:07 +02:00
" WHERE mch_matches.MchCod=%ld"
" AND mch_matches.GamCod=gam_games.GamCod"
" AND gam_games.CrsCod=%ld", // Extra check
2019-09-17 21:01:57 +02:00
MchSubQuery,
2019-09-14 12:59:34 +02:00
Match->Status.QstInd,Match->Status.QstCod,
Mch_ShowingStringsDB[Match->Status.Showing],
2019-09-25 21:58:12 +02:00
Match->Status.ShowQstResults ? 'Y' : 'N',
2019-09-14 12:59:34 +02:00
Match->MchCod,Gbl.Hierarchy.Crs.CrsCod);
2019-09-17 15:42:41 +02:00
if (Match->Status.Playing)
2019-09-14 12:59:34 +02:00
/* Update match as being played */
Mch_UpdateMatchAsBeingPlayed (Match->MchCod);
else
/* Update match as not being played */
Mch_SetMatchAsNotBeingPlayed (Match->MchCod);
}
/*****************************************************************************/
/********** Update elapsed time in current question (by a teacher) ***********/
/*****************************************************************************/
static void Mch_UpdateElapsedTimeInQuestion (struct Match *Match)
{
/***** Update elapsed time in current question in database *****/
2019-09-17 15:42:41 +02:00
if (Match->Status.Playing &&
2019-09-14 12:59:34 +02:00
Match->Status.QstInd > 0 &&
Match->Status.QstInd < Mch_AFTER_LAST_QUESTION)
DB_QueryINSERT ("can not update elapsed time in question",
2019-09-17 01:37:07 +02:00
"INSERT INTO mch_times (MchCod,QstInd,ElapsedTime)"
2019-09-14 12:59:34 +02:00
" VALUES (%ld,%u,SEC_TO_TIME(%u))"
" ON DUPLICATE KEY"
" UPDATE ElapsedTime=ADDTIME(ElapsedTime,SEC_TO_TIME(%u))",
Match->MchCod,Match->Status.QstInd,
2019-10-01 21:18:53 +02:00
Cfg_SECONDS_TO_REFRESH_MATCH_TCH,
Cfg_SECONDS_TO_REFRESH_MATCH_TCH);
2019-09-14 12:59:34 +02:00
}
/*****************************************************************************/
/******************* Get elapsed time in a match question ********************/
/*****************************************************************************/
static void Mch_GetElapsedTimeInQuestion (struct Match *Match,
struct Time *Time)
{
MYSQL_RES *mysql_res;
unsigned NumRows;
/***** Query database *****/
NumRows = (unsigned) DB_QuerySELECT (&mysql_res,"can not get elapsed time",
"SELECT ElapsedTime"
2019-09-17 01:37:07 +02:00
" FROM mch_times"
2019-09-14 12:59:34 +02:00
" WHERE MchCod=%ld AND QstInd=%u",
Match->MchCod,Match->Status.QstInd);
/***** Get elapsed time from query result *****/
Mch_GetElapsedTime (NumRows,mysql_res,Time);
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
/*****************************************************************************/
/*********************** Get elapsed time in a match *************************/
/*****************************************************************************/
static void Mch_GetElapsedTimeInMatch (struct Match *Match,
struct Time *Time)
{
MYSQL_RES *mysql_res;
unsigned NumRows;
/***** Query database *****/
NumRows = (unsigned) DB_QuerySELECT (&mysql_res,"can not get elapsed time",
"SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(ElapsedTime)))"
2019-09-17 01:37:07 +02:00
" FROM mch_times WHERE MchCod=%ld",
2019-09-14 12:59:34 +02:00
Match->MchCod);
/***** Get elapsed time from query result *****/
Mch_GetElapsedTime (NumRows,mysql_res,Time);
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
/*****************************************************************************/
/*********************** Get elapsed time in a match *************************/
/*****************************************************************************/
static void Mch_GetElapsedTime (unsigned NumRows,MYSQL_RES *mysql_res,
struct Time *Time)
{
MYSQL_ROW row;
bool ElapsedTimeGotFromDB = false;
/***** Get time from H...H:MM:SS string *****/
if (NumRows)
{
row = mysql_fetch_row (mysql_res);
if (row[0])
/* Get the elapsed time (row[0]) */
if (sscanf (row[0],"%u:%02u:%02u",&Time->Hour,&Time->Minute,&Time->Second) == 3)
ElapsedTimeGotFromDB = true;
}
/***** Initialize time to default value (0) *****/
if (!ElapsedTimeGotFromDB)
Time->Hour =
Time->Minute =
Time->Second = 0;
}
/*****************************************************************************/
/********************* Pause current match (by a teacher) ********************/
/*****************************************************************************/
2019-09-29 21:33:27 +02:00
void Mch_PauseMatch (void)
2019-09-14 12:59:34 +02:00
{
struct Match Match;
/***** Remove old players.
2019-10-01 21:18:53 +02:00
This function must be called by a teacher
before getting match status. *****/
2019-09-14 12:59:34 +02:00
Mch_RemoveOldPlayers ();
/***** Get data of the match from database *****/
Match.MchCod = Gbl.Games.MchCodBeingPlayed;
Mch_GetDataOfMatchByCod (&Match);
/***** Update status *****/
2019-09-17 15:42:41 +02:00
Match.Status.Playing = false; // Pause match
2019-09-14 12:59:34 +02:00
/***** Update match status in database *****/
Mch_UpdateMatchStatusInDB (&Match);
/***** Show current match status *****/
2019-09-26 18:37:10 +02:00
fprintf (Gbl.F.Out,"<div id=\"match\" class=\"MATCH_CONT\">");
2019-09-14 12:59:34 +02:00
Mch_ShowMatchStatusForTch (&Match);
fprintf (Gbl.F.Out,"</div>");
}
/*****************************************************************************/
/** Show current match status (current question, answers...) (by a teacher) **/
/*****************************************************************************/
2019-09-29 21:33:27 +02:00
void Mch_PlayMatch (void)
2019-09-14 12:59:34 +02:00
{
struct Match Match;
/***** Remove old players.
2019-10-01 21:18:53 +02:00
This function must be called by a teacher
before getting match status. *****/
2019-09-14 12:59:34 +02:00
Mch_RemoveOldPlayers ();
/***** Get data of the match from database *****/
Match.MchCod = Gbl.Games.MchCodBeingPlayed;
Mch_GetDataOfMatchByCod (&Match);
/***** If not yet finished, update status *****/
if (Match.Status.QstInd < Mch_AFTER_LAST_QUESTION) // Unfinished
{
if (Match.Status.QstInd == 0) // Match has been created, but it has not started
Mch_SetMatchStatusToNext (&Match);
2019-09-17 15:42:41 +02:00
Match.Status.Playing = true; // Start/resume match
2019-09-14 12:59:34 +02:00
}
/***** Update match status in database *****/
Mch_UpdateMatchStatusInDB (&Match);
/***** Show current match status *****/
2019-09-26 18:37:10 +02:00
fprintf (Gbl.F.Out,"<div id=\"match\" class=\"MATCH_CONT\">");
2019-09-14 12:59:34 +02:00
Mch_ShowMatchStatusForTch (&Match);
fprintf (Gbl.F.Out,"</div>");
}
/*****************************************************************************/
2019-09-14 19:46:26 +02:00
/********* Toggle the display of results in a match (by a teacher) ***********/
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
2019-09-25 21:58:12 +02:00
void Mch_ToggleVisibilResultsMchQst (void)
2019-09-14 12:59:34 +02:00
{
struct Match Match;
/***** Remove old players.
2019-10-01 21:18:53 +02:00
This function must be called by a teacher
before getting match status. *****/
2019-09-14 12:59:34 +02:00
Mch_RemoveOldPlayers ();
/***** Get data of the match from database *****/
Match.MchCod = Gbl.Games.MchCodBeingPlayed;
Mch_GetDataOfMatchByCod (&Match);
/***** Update status *****/
2019-09-25 21:58:12 +02:00
Match.Status.ShowQstResults = !Match.Status.ShowQstResults; // Toggle display
if (Match.Status.Showing == Mch_RESULTS &&
!Match.Status.ShowQstResults)
2019-09-14 19:46:26 +02:00
Match.Status.Showing = Mch_ANSWERS; // Hide results
2019-09-14 12:59:34 +02:00
/***** Update match status in database *****/
Mch_UpdateMatchStatusInDB (&Match);
/***** Show current match status *****/
2019-09-26 18:37:10 +02:00
fprintf (Gbl.F.Out,"<div id=\"match\" class=\"MATCH_CONT\">");
2019-09-14 12:59:34 +02:00
Mch_ShowMatchStatusForTch (&Match);
fprintf (Gbl.F.Out,"</div>");
}
/*****************************************************************************/
/************* Show previous question in a match (by a teacher) **************/
/*****************************************************************************/
2019-09-29 21:33:27 +02:00
void Mch_BackMatch (void)
2019-09-14 12:59:34 +02:00
{
struct Match Match;
/***** Remove old players.
2019-10-01 21:18:53 +02:00
This function must be called by a teacher
before getting match status. *****/
2019-09-14 12:59:34 +02:00
Mch_RemoveOldPlayers ();
/***** Get data of the match from database *****/
Match.MchCod = Gbl.Games.MchCodBeingPlayed;
Mch_GetDataOfMatchByCod (&Match);
/***** Update status *****/
Mch_SetMatchStatusToPrev (&Match);
/***** Update match status in database *****/
Mch_UpdateMatchStatusInDB (&Match);
/***** Show current match status *****/
2019-09-26 18:37:10 +02:00
fprintf (Gbl.F.Out,"<div id=\"match\" class=\"MATCH_CONT\">");
2019-09-14 12:59:34 +02:00
Mch_ShowMatchStatusForTch (&Match);
fprintf (Gbl.F.Out,"</div>");
}
/*****************************************************************************/
/*************** Show next question in a match (by a teacher) ****************/
/*****************************************************************************/
2019-09-29 21:33:27 +02:00
void Mch_ForwardMatch (void)
2019-09-14 12:59:34 +02:00
{
struct Match Match;
/***** Remove old players.
2019-10-01 21:18:53 +02:00
This function must be called by a teacher
before getting match status. *****/
2019-09-14 12:59:34 +02:00
Mch_RemoveOldPlayers ();
/***** Get data of the match from database *****/
Match.MchCod = Gbl.Games.MchCodBeingPlayed;
Mch_GetDataOfMatchByCod (&Match);
/***** Update status *****/
Mch_SetMatchStatusToNext (&Match);
/***** Update match status in database *****/
Mch_UpdateMatchStatusInDB (&Match);
/***** Show current match status *****/
2019-09-26 18:37:10 +02:00
fprintf (Gbl.F.Out,"<div id=\"match\" class=\"MATCH_CONT\">");
2019-09-14 12:59:34 +02:00
Mch_ShowMatchStatusForTch (&Match);
fprintf (Gbl.F.Out,"</div>");
}
/*****************************************************************************/
/************** Set match status to previous (backward) status ***************/
/*****************************************************************************/
static void Mch_SetMatchStatusToPrev (struct Match *Match)
{
/***** What to show *****/
2019-09-17 15:42:41 +02:00
if (Match->Status.QstInd == 0) // Start
Mch_SetMatchStatusToStart (Match);
else if (Match->Status.QstInd >= Mch_AFTER_LAST_QUESTION) // End
Mch_SetMatchStatusToPrevQst (Match);
else // Between start and end
switch (Match->Status.Showing)
{
case Mch_NOTHING:
case Mch_STEM:
Mch_SetMatchStatusToPrevQst (Match);
break;
case Mch_ANSWERS:
Match->Status.Showing = Mch_STEM;
break;
case Mch_RESULTS:
Match->Status.Showing = Mch_ANSWERS;
break;
}
}
static void Mch_SetMatchStatusToPrevQst (struct Match *Match)
{
/***** Get index of the previous question *****/
Match->Status.QstInd = Gam_GetPrevQuestionIndexInGame (Match->GamCod,
Match->Status.QstInd);
if (Match->Status.QstInd == 0) // Start of questions has been reached
Mch_SetMatchStatusToStart (Match);
else
2019-09-14 12:59:34 +02:00
{
2019-09-17 15:42:41 +02:00
Match->Status.QstCod = Gam_GetQstCodFromQstInd (Match->GamCod,
Match->Status.QstInd);
2019-09-25 21:58:12 +02:00
Match->Status.Showing = Match->Status.ShowQstResults ? Mch_RESULTS :
Mch_ANSWERS;
2019-09-14 12:59:34 +02:00
}
}
2019-09-17 15:42:41 +02:00
static void Mch_SetMatchStatusToStart (struct Match *Match)
{
Match->Status.QstInd = 0; // Before first question
Match->Status.QstCod = -1L;
Match->Status.Playing = false;
Match->Status.Showing = Mch_NOTHING;
}
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
/**************** Set match status to next (forward) status ******************/
/*****************************************************************************/
static void Mch_SetMatchStatusToNext (struct Match *Match)
{
/***** What to show *****/
2019-09-17 15:42:41 +02:00
if (Match->Status.QstInd == 0) // Start
Mch_SetMatchStatusToNextQst (Match);
else if (Match->Status.QstInd >= Mch_AFTER_LAST_QUESTION) // End
Mch_SetMatchStatusToEnd (Match);
else // Between start and end
switch (Match->Status.Showing)
2019-09-14 19:46:26 +02:00
{
2019-09-17 15:42:41 +02:00
case Mch_NOTHING:
Match->Status.Showing = Mch_STEM;
break;
case Mch_STEM:
Match->Status.Showing = Mch_ANSWERS;
break;
case Mch_ANSWERS:
2019-09-25 21:58:12 +02:00
if (Match->Status.ShowQstResults)
2019-09-17 15:42:41 +02:00
Match->Status.Showing = Mch_RESULTS;
else
Mch_SetMatchStatusToNextQst (Match);
break;
case Mch_RESULTS:
Mch_SetMatchStatusToNextQst (Match);
break;
2019-09-14 19:46:26 +02:00
}
2019-09-17 15:42:41 +02:00
}
static void Mch_SetMatchStatusToNextQst (struct Match *Match)
{
/***** Get index of the next question *****/
Match->Status.QstInd = Gam_GetNextQuestionIndexInGame (Match->GamCod,
Match->Status.QstInd);
/***** Get question code *****/
if (Match->Status.QstInd >= Mch_AFTER_LAST_QUESTION) // Finished
Mch_SetMatchStatusToEnd (Match);
else // Unfinished
{
Match->Status.QstCod = Gam_GetQstCodFromQstInd (Match->GamCod,
Match->Status.QstInd);
Match->Status.Showing = Mch_STEM;
2019-09-14 19:46:26 +02:00
}
2019-09-14 12:59:34 +02:00
}
2019-09-17 15:42:41 +02:00
static void Mch_SetMatchStatusToEnd (struct Match *Match)
{
Match->Status.QstInd = Mch_AFTER_LAST_QUESTION; // After last question
Match->Status.QstCod = -1L;
Match->Status.Playing = false;
Match->Status.Showing = Mch_NOTHING;
}
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
/******* Show current match status (number, question, answers, button) *******/
/*****************************************************************************/
static void Mch_ShowMatchStatusForTch (struct Match *Match)
{
/***** Left column *****/
Mch_ShowLeftColumnTch (Match);
/***** Right column *****/
2019-09-26 18:19:07 +02:00
Mch_ShowRightColumnTch (Match);
2019-09-14 12:59:34 +02:00
}
/*****************************************************************************/
/************ Show current question being played for a student ***************/
/*****************************************************************************/
static void Mch_ShowMatchStatusForStd (struct Match *Match)
{
2019-09-29 19:36:32 +02:00
bool ICanPlayThisMatchBasedOnGrps;
2019-09-14 12:59:34 +02:00
2019-09-29 19:36:32 +02:00
/***** Can I play this match? *****/
ICanPlayThisMatchBasedOnGrps = Mch_CheckIfICanPlayThisMatchBasedOnGrps (Match->MchCod);
if (!ICanPlayThisMatchBasedOnGrps)
2019-09-30 01:10:57 +02:00
Act_NoPermissionExit ();
2019-09-14 12:59:34 +02:00
/***** Left column *****/
Mch_ShowLeftColumnStd (Match);
/***** Right column *****/
2019-09-26 18:19:07 +02:00
Mch_ShowRightColumnStd (Match);
2019-09-14 12:59:34 +02:00
}
2019-09-26 01:15:21 +02:00
/*****************************************************************************/
/******************* Get number of questions of a game *********************/
/*****************************************************************************/
2019-09-28 02:31:42 +02:00
unsigned Mch_GetNumMchsInGame (long GamCod)
2019-09-26 01:15:21 +02:00
{
2019-09-26 21:27:36 +02:00
/***** Trivial check *****/
if (GamCod < 0) // A non-existing game...
return 0; // ...has no matches
2019-09-26 01:15:21 +02:00
/***** Get number of matches in a game from database *****/
return
(unsigned) DB_QueryCOUNT ("can not get number of matches of a game",
"SELECT COUNT(*) FROM mch_matches"
" WHERE GamCod=%ld",
GamCod);
}
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
/************ Check if I belong to any of the groups of a match **************/
/*****************************************************************************/
2019-09-29 19:36:32 +02:00
bool Mch_CheckIfICanPlayThisMatchBasedOnGrps (long MchCod)
2019-09-14 12:59:34 +02:00
{
2019-09-29 19:36:32 +02:00
switch (Gbl.Usrs.Me.Role.Logged)
{
case Rol_STD:
if (Gbl.Usrs.Me.IBelongToCurrentCrs)
/***** Get if I can play a match from database *****/
return (DB_QueryCOUNT ("can not check if I can play a match",
"SELECT COUNT(*) FROM mch_matches"
" WHERE MchCod=%ld"
" AND (MchCod NOT IN (SELECT MchCod FROM mch_groups) OR"
" MchCod IN (SELECT mch_groups.MchCod FROM mch_groups,crs_grp_usr"
" WHERE crs_grp_usr.UsrCod=%ld"
" AND mch_groups.GrpCod=crs_grp_usr.GrpCod))",
MchCod,Gbl.Usrs.Me.UsrDat.UsrCod) != 0);
else
return false;
break;
case Rol_NET:
case Rol_TCH:
return Gbl.Usrs.Me.IBelongToCurrentCrs;
case Rol_SYS_ADM:
return true;
default:
return false;
}
2019-09-14 12:59:34 +02:00
}
/*****************************************************************************/
2019-09-26 18:19:07 +02:00
/*********** Show left column when playing a match (as a teacher) ************/
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
static void Mch_ShowLeftColumnTch (struct Match *Match)
2019-09-26 19:54:09 +02:00
{
/***** Start left container *****/
2019-09-30 16:25:44 +02:00
fprintf (Gbl.F.Out,"<div class=\"MATCH_LEFT\">");
/***** Refreshable part *****/
fprintf (Gbl.F.Out,"<div id=\"match_left\""
" class=\"MATCH_REFRESHABLE_TEACHER\">");
Mch_ShowRefreshablePartTch (Match);
fprintf (Gbl.F.Out,"</div>");
2019-09-26 19:54:09 +02:00
2019-09-30 16:25:44 +02:00
/***** Buttons *****/
Mch_PutMatchControlButtons (Match);
/***** Write button to request viewing results *****/
Mch_PutCheckboxResult (Match);
2019-09-26 19:54:09 +02:00
/***** End left container *****/
fprintf (Gbl.F.Out,"</div>");
}
2019-09-30 16:25:44 +02:00
static void Mch_ShowRefreshablePartTch (struct Match *Match)
2019-09-14 12:59:34 +02:00
{
extern const char *Txt_MATCH_respond;
struct Time Time;
2019-09-16 00:15:40 +02:00
unsigned NumAnswerersQst;
2019-09-14 12:59:34 +02:00
/***** Top *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_TOP\">");
/* Write elapsed time in match */
Mch_GetElapsedTimeInMatch (Match,&Time);
Dat_WriteHoursMinutesSeconds (&Time);
fprintf (Gbl.F.Out,"</div>");
/***** Write number of question *****/
Mch_ShowNumQstInMatch (Match);
/***** Write elapsed time in question *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_TIME_QST\">");
if (Match->Status.QstInd > 0 &&
Match->Status.QstInd < Mch_AFTER_LAST_QUESTION)
{
Mch_GetElapsedTimeInQuestion (Match,&Time);
Dat_WriteHoursMinutesSeconds (&Time);
}
else
fprintf (Gbl.F.Out,"-");
fprintf (Gbl.F.Out,"</div>");
2019-09-30 14:55:55 +02:00
/***** Number of users who have answered this question *****/
NumAnswerersQst = Mch_GetNumUsrsWhoHaveAnswerQst (Match->MchCod,
Match->Status.QstInd);
fprintf (Gbl.F.Out,"<div class=\"MATCH_NUM_ANSWERERS\">"
"%s<br />"
2019-10-01 01:01:35 +02:00
"<strong>",
Txt_MATCH_respond);
if (Match->Status.QstInd > 0 &&
Match->Status.QstInd < Mch_AFTER_LAST_QUESTION)
2019-09-30 14:55:55 +02:00
{
2019-10-01 01:01:35 +02:00
fprintf (Gbl.F.Out,"%u",NumAnswerersQst);
if (Match->Status.Playing)
{
/* Get current number of players */
Mch_GetNumPlayers (Match);
2019-09-30 14:55:55 +02:00
2019-10-01 01:01:35 +02:00
/* Show current number of players */
fprintf (Gbl.F.Out,"/%u",
Match->Status.NumPlayers);
}
2019-09-30 14:55:55 +02:00
}
2019-10-01 01:01:35 +02:00
else
fprintf (Gbl.F.Out,"-");
2019-09-30 14:55:55 +02:00
fprintf (Gbl.F.Out,"</strong>"
"</div>");
2019-09-14 12:59:34 +02:00
}
/*****************************************************************************/
2019-09-26 18:19:07 +02:00
/********** Show right column when playing a match (as a teacher) ************/
/*****************************************************************************/
static void Mch_ShowRightColumnTch (struct Match *Match)
{
/***** Start right container *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_RIGHT\">");
/***** Top row: match title *****/
Mch_ShowMatchTitle (Match);
/***** Bottom row: current question and possible answers *****/
2019-10-01 01:01:35 +02:00
if (Match->Status.QstInd < Mch_AFTER_LAST_QUESTION) // Not finished
Mch_ShowQuestionAndAnswersTch (Match);
else // Finished
Mch_ShowMatchPodium (Match);
2019-09-26 18:19:07 +02:00
/***** End right container *****/
fprintf (Gbl.F.Out,"</div>");
}
/*****************************************************************************/
/*********** Show left column when playing a match (as a student) ************/
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
static void Mch_ShowLeftColumnStd (struct Match *Match)
{
/***** Start left container *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_LEFT\">");
/***** Top *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_TOP\"></div>");
/***** Write number of question *****/
Mch_ShowNumQstInMatch (Match);
/***** End left container *****/
fprintf (Gbl.F.Out,"</div>");
}
2019-09-26 18:19:07 +02:00
/*****************************************************************************/
/********** Show right column when playing a match (as a student) ************/
/*****************************************************************************/
static void Mch_ShowRightColumnStd (struct Match *Match)
{
extern const char *Txt_Please_wait_;
/***** Start right container *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_RIGHT\">");
/***** Top row *****/
Mch_ShowMatchTitle (Match);
/***** Bottom row *****/
2019-09-29 21:33:27 +02:00
if (Match->Status.Playing)
2019-09-26 18:19:07 +02:00
{
2019-09-29 21:33:27 +02:00
if (Match->Status.QstInd < Mch_AFTER_LAST_QUESTION) // Unfinished
{
fprintf (Gbl.F.Out,"<div class=\"MATCH_BOTTOM\">");
2019-09-26 18:19:07 +02:00
2019-09-29 21:33:27 +02:00
/***** Update players ******/
Mch_RegisterMeAsPlayerInMatch (Match->MchCod);
2019-09-26 18:19:07 +02:00
/* Show current question and possible answers */
Mch_ShowQuestionAndAnswersStd (Match);
2019-09-29 21:33:27 +02:00
fprintf (Gbl.F.Out,"</div>");
}
2019-09-26 18:19:07 +02:00
}
2019-09-29 21:33:27 +02:00
else // Not being played
Mch_ShowWaitImage (Txt_Please_wait_);
2019-09-26 18:19:07 +02:00
/***** End right container *****/
fprintf (Gbl.F.Out,"</div>");
}
2019-09-14 19:46:26 +02:00
/*****************************************************************************/
/********************* Show number of question in game ***********************/
/*****************************************************************************/
static void Mch_ShowNumQstInMatch (struct Match *Match)
{
extern const char *Txt_MATCH_Start;
extern const char *Txt_MATCH_End;
unsigned NumQsts = Gam_GetNumQstsGame (Match->GamCod);
fprintf (Gbl.F.Out,"<div class=\"MATCH_NUM_QST\">");
if (Match->Status.QstInd == 0) // Not started
fprintf (Gbl.F.Out,"%s",Txt_MATCH_Start);
else if (Match->Status.QstInd >= Mch_AFTER_LAST_QUESTION) // Finished
fprintf (Gbl.F.Out,"%s",Txt_MATCH_End);
else
fprintf (Gbl.F.Out,"%u/%u",Match->Status.QstInd,NumQsts);
fprintf (Gbl.F.Out,"</div>");
}
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
/********************** Put buttons to control a match ***********************/
/*****************************************************************************/
static void Mch_PutMatchControlButtons (struct Match *Match)
{
extern const char *Txt_Go_back;
extern const char *Txt_Go_forward;
extern const char *Txt_Pause;
extern const char *Txt_Start;
extern const char *Txt_Resume;
/***** Start buttons container *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_BUTTONS_CONTAINER\">");
/***** Left button *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_BUTTON_LEFT_CONTAINER\">");
if (Match->Status.QstInd == 0)
/* Put button to close browser tab */
Mch_PutBigButtonClose ();
else
/* Put button to go back */
2019-09-29 21:33:27 +02:00
Mch_PutBigButton (ActBckMch,Match->MchCod,
2019-09-14 12:59:34 +02:00
Mch_ICON_PREVIOUS,Txt_Go_back);
fprintf (Gbl.F.Out,"</div>");
/***** Center button *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_BUTTON_CENTER_CONTAINER\">");
2019-09-17 15:42:41 +02:00
if (Match->Status.Playing) // Being played
2019-09-14 12:59:34 +02:00
/* Put button to pause match */
2019-09-29 21:33:27 +02:00
Mch_PutBigButton (ActPauMch,
2019-09-14 12:59:34 +02:00
Match->MchCod,
Mch_ICON_PAUSE,Txt_Pause);
else // Paused
{
if (Match->Status.QstInd < Mch_AFTER_LAST_QUESTION) // Not finished
/* Put button to play match */
2019-09-29 21:33:27 +02:00
Mch_PutBigButton (ActPlyMch,
2019-09-14 12:59:34 +02:00
Match->MchCod,
Mch_ICON_PLAY,Match->Status.QstInd == 0 ? Txt_Start :
Txt_Resume);
else // Finished
/* Put disabled button to play match */
Mch_PutBigButtonOff (Mch_ICON_PLAY);
}
fprintf (Gbl.F.Out,"</div>");
/***** Right button *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_BUTTON_RIGHT_CONTAINER\">");
if (Match->Status.QstInd >= Mch_AFTER_LAST_QUESTION) // Finished
/* Put button to close browser tab */
Mch_PutBigButtonClose ();
else
/* Put button to show answers */
2019-09-29 21:33:27 +02:00
Mch_PutBigButton (ActFwdMch,Match->MchCod,
2019-09-14 12:59:34 +02:00
Mch_ICON_NEXT,Txt_Go_forward);
fprintf (Gbl.F.Out,"</div>");
/***** End buttons container *****/
fprintf (Gbl.F.Out,"</div>");
}
/*****************************************************************************/
2019-09-14 19:46:26 +02:00
/***************** Put checkbox to select if show results ********************/
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
2019-09-14 19:46:26 +02:00
static void Mch_PutCheckboxResult (struct Match *Match)
2019-09-14 12:59:34 +02:00
{
2019-09-14 19:46:26 +02:00
extern const char *Txt_View_results;
2019-09-14 12:59:34 +02:00
2019-09-14 19:46:26 +02:00
/***** Start container *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_SHOW_RESULTS\">");
/***** Start form *****/
2019-09-25 21:58:12 +02:00
Frm_StartForm (ActChgVisResMchQst);
2019-09-22 13:54:11 +02:00
Mch_PutParamMchCod (Match->MchCod); // Current match being played
2019-09-14 19:46:26 +02:00
/***** Put icon with link *****/
fprintf (Gbl.F.Out,"<div class=\"CONTEXT_OPT\">"
"<a href=\"\" class=\"ICO_HIGHLIGHT\""
" title=\"%s\" "
2019-09-30 16:25:44 +02:00
" onclick=\"document.getElementById('%s').submit();"
2019-09-14 19:46:26 +02:00
" return false;\">"
"<i class=\"%s\"></i>"
"&nbsp;%s"
"</a>"
"</div>",
Txt_View_results,
Gbl.Form.Id,
2019-09-25 21:58:12 +02:00
Match->Status.ShowQstResults ? "fas fa-toggle-on" :
"fas fa-toggle-off",
2019-09-14 19:46:26 +02:00
Txt_View_results);
/***** End form *****/
Frm_EndForm ();
/***** End container *****/
2019-09-14 12:59:34 +02:00
fprintf (Gbl.F.Out,"</div>");
}
/*****************************************************************************/
/***************************** Show match title ******************************/
/*****************************************************************************/
static void Mch_ShowMatchTitle (struct Match *Match)
{
/***** Match title *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_TOP\">%s</div>",Match->Title);
}
/*****************************************************************************/
/***** Show question and its answers when playing a match (as a teacher) *****/
/*****************************************************************************/
static void Mch_ShowQuestionAndAnswersTch (struct Match *Match)
{
extern const char *Txt_MATCH_Paused;
extern const char *Txt_View_results;
MYSQL_RES *mysql_res;
MYSQL_ROW row;
/***** Trivial check: question index should be correct *****/
if (Match->Status.QstInd == 0 ||
Match->Status.QstInd >= Mch_AFTER_LAST_QUESTION)
return;
/***** Get data of question from database *****/
if (!DB_QuerySELECT (&mysql_res,"can not get data of a question",
"SELECT AnsType," // row[0]
"Stem," // row[1]
"MedCod" // row[2]
" FROM tst_questions"
" WHERE QstCod=%ld",
Match->Status.QstCod))
Ale_ShowAlert (Ale_ERROR,"Question doesn't exist.");
row = mysql_fetch_row (mysql_res);
/***** Show question *****/
/* Get answer type (row[0]) */
Gbl.Test.AnswerType = Tst_ConvertFromStrAnsTypDBToAnsTyp (row[0]);
2019-09-23 21:39:54 +02:00
if (Gbl.Test.AnswerType != Tst_ANS_UNIQUE_CHOICE)
Lay_ShowErrorAndExit ("Wrong answer type.");
2019-09-14 12:59:34 +02:00
2019-09-16 00:15:40 +02:00
fprintf (Gbl.F.Out,"<div class=\"MATCH_BOTTOM\">"); // Bottom
2019-09-14 12:59:34 +02:00
/* Write stem (row[1]) */
2019-09-16 00:15:40 +02:00
Tst_WriteQstStem (row[1],"MATCH_TCH_STEM");
2019-09-14 12:59:34 +02:00
/* Get media (row[2]) */
Gbl.Test.Media.MedCod = Str_ConvertStrCodToLongCod (row[2]);
Med_GetMediaDataByCod (&Gbl.Test.Media);
/* Show media */
Med_ShowMedia (&Gbl.Test.Media,
"TEST_MED_EDIT_LIST_STEM_CONTAINER",
"TEST_MED_EDIT_LIST_STEM");
2019-09-24 01:41:51 +02:00
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
/***** Write answers? *****/
2019-09-14 12:59:34 +02:00
switch (Match->Status.Showing)
{
2019-09-17 15:42:41 +02:00
case Mch_NOTHING:
2019-09-16 00:15:40 +02:00
case Mch_STEM:
2019-09-14 12:59:34 +02:00
/* Don't write anything */
break;
case Mch_ANSWERS:
2019-09-17 15:42:41 +02:00
if (Match->Status.Playing) // Being played
2019-09-14 12:59:34 +02:00
/* Write answers */
Tst_WriteAnswersMatchResult (Match->MchCod,
Match->Status.QstInd,
Match->Status.QstCod,
2019-09-16 00:15:40 +02:00
"MATCH_TCH_ANS",
false); // Don't show result
else // Not being played
2019-09-14 12:59:34 +02:00
Mch_ShowWaitImage (Txt_MATCH_Paused);
break;
case Mch_RESULTS:
/* Write answers with results */
Tst_WriteAnswersMatchResult (Match->MchCod,
Match->Status.QstInd,
Match->Status.QstCod,
2019-09-16 00:15:40 +02:00
"MATCH_TCH_ANS",
true); // Show result
2019-09-14 12:59:34 +02:00
break;
}
2019-09-16 00:15:40 +02:00
fprintf (Gbl.F.Out,"</div>"); // Bottom
2019-09-14 12:59:34 +02:00
}
/*****************************************************************************/
/***** Show question and its answers when playing a match (as a student) *****/
/*****************************************************************************/
static void Mch_ShowQuestionAndAnswersStd (struct Match *Match)
{
2019-09-24 01:41:51 +02:00
struct Mch_UsrAnswer UsrAnswer;
2019-09-14 12:59:34 +02:00
unsigned NumOptions;
unsigned NumOpt;
/***** Show question *****/
/* Write buttons for answers? */
if (Match->Status.Showing == Mch_ANSWERS)
{
if (Tst_CheckIfQuestionIsValidForGame (Match->Status.QstCod))
{
/***** Get student's answer to this question
(<0 ==> no answer) *****/
2019-09-24 01:41:51 +02:00
Mch_GetQstAnsFromDB (Match->MchCod,
Gbl.Usrs.Me.UsrDat.UsrCod,
Match->Status.QstInd,
&UsrAnswer);
2019-09-14 12:59:34 +02:00
/***** Get number of options in this question *****/
NumOptions = Tst_GetNumAnswersQst (Match->Status.QstCod);
/***** Start table *****/
Tbl_StartTableWide (8);
for (NumOpt = 0;
NumOpt < NumOptions;
NumOpt++)
{
/***** Start row *****/
fprintf (Gbl.F.Out,"<tr>");
/***** Write letter for this option *****/
/* Start table cell */
fprintf (Gbl.F.Out,"<td class=\"MATCH_STD_CELL\">");
/* Form with button.
Sumitting onmousedown instead of default onclick
is necessary in order to be fast
and not lose clicks due to refresh */
Frm_StartForm (ActAnsMchQstStd);
2019-09-22 13:54:11 +02:00
Mch_PutParamMchCod (Match->MchCod); // Current match being played
2019-09-14 12:59:34 +02:00
Gam_PutParamQstInd (Match->Status.QstInd); // Current question index shown
2019-09-24 01:41:51 +02:00
Mch_PutParamNumOpt (NumOpt); // Number of button
2019-09-14 12:59:34 +02:00
fprintf (Gbl.F.Out,"<button type=\"submit\""
" onmousedown=\"document.getElementById('%s').submit();"
"return false;\" class=\"",
Gbl.Form.Id);
2019-09-24 01:41:51 +02:00
if (UsrAnswer.NumOpt == (int) NumOpt) // Student's answer
2019-09-14 12:59:34 +02:00
fprintf (Gbl.F.Out,"MATCH_STD_ANSWER_SELECTED ");
fprintf (Gbl.F.Out,"MATCH_STD_BUTTON BT_%c\">"
"%c"
"</button>",
'A' + (char) NumOpt,
'a' + (char) NumOpt);
Frm_EndForm ();
/* End table cell */
fprintf (Gbl.F.Out,"</td>");
/***** End row *****/
fprintf (Gbl.F.Out,"</tr>");
}
/***** End table *****/
Tbl_EndTable ();
}
else
Ale_ShowAlert (Ale_ERROR,"Type of answer not valid in a game.");
}
}
2019-10-01 01:01:35 +02:00
/*****************************************************************************/
/***************************** Show match podium *****************************/
/*****************************************************************************/
static void Mch_ShowMatchPodium (struct Match *Match)
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumRows;
unsigned NumRow;
struct
{
double Score;
unsigned NumUsrs;
} Podium[3];
unsigned i;
static const unsigned Position[3] =
{
1, // 2nd position
0, // 1st position
2 // 3rd position
};
/***** Get podium from database *****/
NumRows = (unsigned)
DB_QuerySELECT (&mysql_res,"can not get data of a question",
"SELECT Score," // row[0]
"COUNT(*) AS NumUsrs" // row[1]
" FROM mch_results"
" WHERE MchCod=%ld"
" GROUP BY Score"
" ORDER BY Score DESC LIMIT 3",
Match->MchCod);
/* Get podium */
for (NumRow = 0;
NumRow < NumRows;
NumRow++)
{
row = mysql_fetch_row (mysql_res);
/* Get score (row[0]) */
Str_SetDecimalPointToUS (); // To get the decimal point as a dot
if (sscanf (row[0],"%lf",&(Podium[NumRow].Score)) != 1)
Podium[NumRow].Score = 0.0;
Str_SetDecimalPointToLocal (); // Return to local system
/* Get number of users (row[1]) *****/
if (sscanf (row[1],"%u",&(Podium[NumRow].NumUsrs)) != 1)
Podium[NumRow].NumUsrs = 0;
}
/* Reset remaining positions */
for (;
NumRow < 3;
NumRow++)
{
/* Score */
Podium[NumRow].Score = 0.0;
/* Number of users */
Podium[NumRow].NumUsrs = 0;
}
/***** Show podium *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_BOTTOM\">"); // Bottom
fprintf (Gbl.F.Out,"<div class=\"MATCH_PODIUM\">"); // Podium
for (i = 0;
i < 3;
i++)
{
fprintf (Gbl.F.Out,"<div class=\"MATCH_PODIUM_%u\">",Position[i]);
if (Podium[Position[i]].NumUsrs)
fprintf (Gbl.F.Out,"<div class=\"MATCH_PODIUM_POSITION\">%u</div>"
"<div class=\"MATCH_PODIUM_SCORE\">%.2lf (%u)</div>",
Position[i] + 1,
Podium[Position[i]].Score,
Podium[Position[i]].NumUsrs);
fprintf (Gbl.F.Out,"</div>");
}
fprintf (Gbl.F.Out,"</div>"); // Podium
fprintf (Gbl.F.Out,"</div>"); // Bottom
}
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
2019-09-24 01:41:51 +02:00
/****** Write parameter with number of option (button) pressed by user *******/
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
2019-09-24 01:41:51 +02:00
static void Mch_PutParamNumOpt (unsigned NumOpt)
2019-09-14 12:59:34 +02:00
{
2019-09-24 01:41:51 +02:00
Par_PutHiddenParamUnsigned ("NumOpt",NumOpt);
2019-09-14 12:59:34 +02:00
}
/*****************************************************************************/
2019-09-24 01:41:51 +02:00
/******* Get parameter with number of option (button) pressed by user ********/
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
2019-09-24 01:41:51 +02:00
static unsigned Mch_GetParamNumOpt (void)
2019-09-14 12:59:34 +02:00
{
2019-09-24 01:41:51 +02:00
long NumOpt;
2019-09-14 12:59:34 +02:00
2019-09-24 01:41:51 +02:00
NumOpt = Par_GetParToLong ("NumOpt");
if (NumOpt < 0)
Lay_ShowErrorAndExit ("Wrong number of option.");
2019-09-14 12:59:34 +02:00
2019-09-24 01:41:51 +02:00
return (unsigned) NumOpt;
2019-09-14 12:59:34 +02:00
}
/*****************************************************************************/
/*********************** Put a big button to do action ***********************/
/*****************************************************************************/
static void Mch_PutBigButton (Act_Action_t NextAction,long MchCod,
const char *Icon,const char *Txt)
{
/***** Start form *****/
Frm_StartForm (NextAction);
2019-09-22 13:54:11 +02:00
Mch_PutParamMchCod (MchCod);
2019-09-14 12:59:34 +02:00
/***** Put icon with link *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_BUTTON_CONTAINER\">"
"<a href=\"\" class=\"MATCH_BUTTON_ON\" title=\"%s\" "
2019-09-30 16:25:44 +02:00
" onclick=\"document.getElementById('%s').submit();"
2019-09-14 12:59:34 +02:00
" return false;\">"
"<i class=\"%s\"></i>"
"</a>"
"</div>",
Txt,
Gbl.Form.Id,
Icon);
/***** End form *****/
Frm_EndForm ();
}
static void Mch_PutBigButtonOff (const char *Icon)
{
/***** Put inactive icon *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_BUTTON_CONTAINER\">"
"<div class=\"MATCH_BUTTON_OFF\">"
"<i class=\"%s\"></i>"
"</div>"
"</div>",
Icon);
}
static void Mch_PutBigButtonClose (void)
{
extern const char *Txt_Close;
/***** Put icon with link *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_BUTTON_CONTAINER\">"
"<a href=\"\" class=\"MATCH_BUTTON_ON\" title=\"%s\" "
2019-10-01 15:35:29 +02:00
" onclick=\"window.close();"
2019-09-14 12:59:34 +02:00
" return false;\"\">"
"<i class=\"%s\"></i>"
"</a>"
"</div>",
Txt_Close,Mch_ICON_CLOSE);
}
/*****************************************************************************/
/****************************** Show wait image ******************************/
/*****************************************************************************/
static void Mch_ShowWaitImage (const char *Txt)
{
fprintf (Gbl.F.Out,"<div class=\"MATCH_WAIT_CONTAINER\">"
"<img src=\"%s/wait.gif\""
" alt=\"%s\" title=\"%s\""
" class=\"MATCH_WAIT_IMAGE\" />"
"</div>",
Cfg_URL_ICON_PUBLIC,
Txt,
Txt);
}
/*****************************************************************************/
/**************************** Remove old players *****************************/
/*****************************************************************************/
static void Mch_RemoveOldPlayers (void)
{
2019-10-01 21:18:53 +02:00
/***** Delete matches not being played by teacher *****/
2019-09-14 12:59:34 +02:00
DB_QueryDELETE ("can not update matches as not being played",
2019-09-17 01:37:07 +02:00
"DELETE FROM mch_playing"
2019-09-14 12:59:34 +02:00
" WHERE TS<FROM_UNIXTIME(UNIX_TIMESTAMP()-%lu)",
2019-10-01 21:18:53 +02:00
Cfg_SECONDS_TO_REFRESH_MATCH_TCH*3);
2019-09-14 12:59:34 +02:00
2019-10-01 21:18:53 +02:00
/***** Delete players (students) who have left matches *****/
2019-09-14 12:59:34 +02:00
DB_QueryDELETE ("can not update match players",
2019-09-17 01:37:07 +02:00
"DELETE FROM mch_players"
2019-09-14 12:59:34 +02:00
" WHERE TS<FROM_UNIXTIME(UNIX_TIMESTAMP()-%lu)",
2019-10-01 21:18:53 +02:00
Cfg_SECONDS_TO_REFRESH_MATCH_STD*3);
2019-09-14 12:59:34 +02:00
}
static void Mch_UpdateMatchAsBeingPlayed (long MchCod)
{
/***** Insert match as being played *****/
DB_QueryREPLACE ("can not set match as being played",
2019-09-17 01:37:07 +02:00
"REPLACE mch_playing (MchCod) VALUE (%ld)",
2019-09-14 12:59:34 +02:00
MchCod);
}
static void Mch_SetMatchAsNotBeingPlayed (long MchCod)
{
/***** Delete all match players ******/
DB_QueryDELETE ("can not update match players",
2019-09-17 01:37:07 +02:00
"DELETE FROM mch_players"
2019-09-14 12:59:34 +02:00
" WHERE MchCod=%ld",
MchCod);
/***** Delete match as being played ******/
DB_QueryDELETE ("can not set match as not being played",
2019-09-17 01:37:07 +02:00
"DELETE FROM mch_playing"
2019-09-14 12:59:34 +02:00
" WHERE MchCod=%ld",
MchCod);
}
static bool Mch_GetIfMatchIsBeingPlayed (long MchCod)
{
/***** Get if a match is being played or not *****/
return
(bool) (DB_QueryCOUNT ("can not get if match is being played",
2019-09-17 01:37:07 +02:00
"SELECT COUNT(*) FROM mch_playing"
2019-09-14 12:59:34 +02:00
" WHERE MchCod=%ld",
MchCod) != 0);
}
static void Mch_RegisterMeAsPlayerInMatch (long MchCod)
{
/***** Insert me as match player *****/
DB_QueryREPLACE ("can not insert match player",
2019-09-17 01:37:07 +02:00
"REPLACE mch_players (MchCod,UsrCod) VALUES (%ld,%ld)",
2019-09-14 12:59:34 +02:00
MchCod,Gbl.Usrs.Me.UsrDat.UsrCod);
}
static void Mch_GetNumPlayers (struct Match *Match)
{
/***** Get number of players who are playing a match *****/
2019-09-17 01:37:07 +02:00
Match->Status.NumPlayers =
(unsigned) DB_QueryCOUNT ("can not get number of players",
"SELECT COUNT(*) FROM mch_players"
" WHERE MchCod=%ld",
Match->MchCod);
2019-09-14 12:59:34 +02:00
}
/*****************************************************************************/
/********************** Get code of match being played ***********************/
/*****************************************************************************/
void Mch_GetMatchBeingPlayed (void)
{
/***** Get match code ****/
2019-09-22 13:54:11 +02:00
if ((Gbl.Games.MchCodBeingPlayed = Mch_GetParamMchCod ()) == -1L)
2019-09-14 12:59:34 +02:00
Lay_ShowErrorAndExit ("Code of match is missing.");
}
/*****************************************************************************/
/********* Show game being played to me as student in a new window ***********/
/*****************************************************************************/
2019-09-29 21:33:27 +02:00
void Mch_JoinMatchAsStd (void)
2019-09-14 12:59:34 +02:00
{
struct Match Match;
/***** Get data of the match from database *****/
Match.MchCod = Gbl.Games.MchCodBeingPlayed;
Mch_GetDataOfMatchByCod (&Match);
/***** Show current match status *****/
2019-09-26 18:37:10 +02:00
fprintf (Gbl.F.Out,"<div id=\"match\" class=\"MATCH_CONT\">");
2019-09-14 12:59:34 +02:00
Mch_ShowMatchStatusForStd (&Match);
fprintf (Gbl.F.Out,"</div>");
}
/*****************************************************************************/
/****************** Refresh match for a teacher via AJAX *********************/
/*****************************************************************************/
void Mch_RefreshMatchTch (void)
{
struct Match Match;
if (!Gbl.Session.IsOpen) // If session has been closed, do not write anything
return;
/***** Remove old players.
2019-10-01 21:18:53 +02:00
This function must be called by a teacher
before getting match status. *****/
2019-09-14 12:59:34 +02:00
Mch_RemoveOldPlayers ();
/***** Get data of the match from database *****/
Match.MchCod = Gbl.Games.MchCodBeingPlayed;
Mch_GetDataOfMatchByCod (&Match);
/***** Update match status in database *****/
Mch_UpdateMatchStatusInDB (&Match);
/***** Update elapsed time in this question *****/
Mch_UpdateElapsedTimeInQuestion (&Match);
/***** Show current match status *****/
2019-09-30 16:25:44 +02:00
Mch_ShowRefreshablePartTch (&Match);
2019-09-14 12:59:34 +02:00
}
/*****************************************************************************/
/*************** Refresh current game for a student via AJAX *****************/
/*****************************************************************************/
void Mch_RefreshMatchStd (void)
{
struct Match Match;
if (!Gbl.Session.IsOpen) // If session has been closed, do not write anything
return;
/***** Get data of the match from database *****/
Match.MchCod = Gbl.Games.MchCodBeingPlayed;
Mch_GetDataOfMatchByCod (&Match);
/***** Show current match status *****/
Mch_ShowMatchStatusForStd (&Match);
}
/*****************************************************************************/
/**** Receive previous question answer in a match question from database *****/
/*****************************************************************************/
2019-09-28 02:31:42 +02:00
void Mch_GetQstAnsFromDB (long MchCod,long UsrCod,unsigned QstInd,
struct Mch_UsrAnswer *UsrAnswer)
2019-09-14 12:59:34 +02:00
{
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumRows;
2019-09-24 01:41:51 +02:00
/***** Set default values for number of option and answer index *****/
UsrAnswer->NumOpt = -1; // < 0 ==> no answer selected
UsrAnswer->AnsInd = -1; // < 0 ==> no answer selected
2019-09-14 12:59:34 +02:00
/***** Get student's answer *****/
2019-09-24 01:41:51 +02:00
NumRows = (unsigned) DB_QuerySELECT (&mysql_res,"can not get user's answer to a match question",
"SELECT NumOpt," // row[0]
"AnsInd" // row[1]
" FROM mch_answers"
2019-09-23 19:17:12 +02:00
" WHERE MchCod=%ld"
" AND UsrCod=%ld"
" AND QstInd=%u",
MchCod,UsrCod,QstInd);
2019-09-14 12:59:34 +02:00
if (NumRows) // Answer found...
{
row = mysql_fetch_row (mysql_res);
2019-09-24 01:41:51 +02:00
/***** Get number of option index (row[0]) *****/
if (sscanf (row[0],"%d",&(UsrAnswer->NumOpt)) != 1)
Lay_ShowErrorAndExit ("Error when getting student's answer to a match question.");
/***** Get answer index (row[1]) *****/
if (sscanf (row[1],"%d",&(UsrAnswer->AnsInd)) != 1)
2019-09-14 12:59:34 +02:00
Lay_ShowErrorAndExit ("Error when getting student's answer to a match question.");
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
/*****************************************************************************/
/********* Receive question answer from student when playing a match *********/
/*****************************************************************************/
2019-09-29 21:33:27 +02:00
void Mch_ReceiveQuestionAnswer (void)
2019-09-14 12:59:34 +02:00
{
struct Match Match;
unsigned QstInd;
2019-09-24 01:41:51 +02:00
unsigned Indexes[Tst_MAX_OPTIONS_PER_QUESTION];
struct Mch_UsrAnswer PreviousUsrAnswer;
struct Mch_UsrAnswer UsrAnswer;
2019-09-23 01:48:28 +02:00
unsigned NumQsts;
unsigned NumQstsNotBlank;
double TotalScore;
2019-09-14 12:59:34 +02:00
/***** Get data of the match from database *****/
Match.MchCod = Gbl.Games.MchCodBeingPlayed;
Mch_GetDataOfMatchByCod (&Match);
/***** Get question index from form *****/
QstInd = Gam_GetParamQstInd ();
2019-09-24 01:41:51 +02:00
/***** Get indexes for this question from database *****/
Mch_GetIndexes (Match.MchCod,QstInd,Indexes);
2019-09-14 12:59:34 +02:00
/***** Check that question index is the current one being played *****/
if (QstInd == Match.Status.QstInd) // Receiving an answer
// to the current question being played
{
/***** Get answer index *****/
2019-09-24 01:41:51 +02:00
/*
Indexes[4] = {0,3,1,2}
+-------+--------+----------+---------+
| Button | Option | Answer | Correct |
| letter | number | index | |
| screen | screen | database | |
+--------+--------+----------+---------+
| a | 0 | 0 | |
| b | 1 | 3 | |
| c | 2 | 1 | Y | <---- User press button #2 (index = 1, correct)
| d | 3 | 2 | |
+--------+--------+----------+---------+
UsrAnswer.NumOpt = 2
UsrAnswer.AnsInd = 1
*/
UsrAnswer.NumOpt = Mch_GetParamNumOpt ();
UsrAnswer.AnsInd = Indexes[UsrAnswer.NumOpt];
2019-09-14 12:59:34 +02:00
/***** Get previous student's answer to this question
(<0 ==> no answer) *****/
2019-09-24 01:41:51 +02:00
Mch_GetQstAnsFromDB (Match.MchCod,Gbl.Usrs.Me.UsrDat.UsrCod,QstInd,
&PreviousUsrAnswer);
2019-09-14 12:59:34 +02:00
/***** Store student's answer *****/
2019-09-24 01:41:51 +02:00
if (PreviousUsrAnswer.AnsInd == UsrAnswer.AnsInd)
DB_QueryDELETE ("can not remove your answer to the match question",
2019-09-17 01:37:07 +02:00
"DELETE FROM mch_answers"
2019-09-14 12:59:34 +02:00
" WHERE MchCod=%ld AND UsrCod=%ld AND QstInd=%u",
Match.MchCod,Gbl.Usrs.Me.UsrDat.UsrCod,QstInd);
2019-09-24 01:41:51 +02:00
else if (UsrAnswer.NumOpt >= 0 &&
UsrAnswer.AnsInd >= 0)
2019-09-14 12:59:34 +02:00
DB_QueryREPLACE ("can not register your answer to the match question",
2019-09-17 01:37:07 +02:00
"REPLACE mch_answers"
2019-09-24 01:41:51 +02:00
" (MchCod,UsrCod,QstInd,NumOpt,AnsInd)"
2019-09-14 12:59:34 +02:00
" VALUES"
2019-09-24 01:41:51 +02:00
" (%ld,%ld,%u,%d,%d)",
Match.MchCod,Gbl.Usrs.Me.UsrDat.UsrCod,QstInd,
UsrAnswer.NumOpt,
UsrAnswer.AnsInd);
2019-09-23 01:48:28 +02:00
/***** Update student's match result *****/
2019-09-30 01:26:19 +02:00
McR_GetMatchResultQuestionsFromDB (Match.MchCod,Gbl.Usrs.Me.UsrDat.UsrCod,
2019-09-24 21:29:01 +02:00
&NumQsts,&NumQstsNotBlank);
TotalScore = Mch_ComputeScore (NumQsts);
2019-09-24 09:24:36 +02:00
2019-09-23 01:48:28 +02:00
Str_SetDecimalPointToUS (); // To print the floating point as a dot
2019-09-23 09:03:55 +02:00
if (DB_QueryCOUNT ("can not get if match result exists",
"SELECT COUNT(*) FROM mch_results"
" WHERE MchCod=%ld AND UsrCod=%ld",
Match.MchCod,Gbl.Usrs.Me.UsrDat.UsrCod)) // Result exists
/* Update result */
DB_QueryUPDATE ("can not update match result",
"UPDATE mch_results"
" SET EndTime=NOW(),"
"NumQsts=%u,"
"NumQstsNotBlank=%u,"
"Score='%lf'"
2019-09-24 01:41:51 +02:00
" WHERE MchCod=%ld AND UsrCod=%ld",
2019-09-23 09:03:55 +02:00
NumQsts,NumQstsNotBlank,TotalScore,
Match.MchCod,Gbl.Usrs.Me.UsrDat.UsrCod);
else // Result doesn't exist
/* Create result */
DB_QueryINSERT ("can not create match result",
"INSERT mch_results "
"(MchCod,UsrCod,StartTime,EndTime,NumQsts,NumQstsNotBlank,Score)"
" VALUES "
"(%ld," // MchCod
"%ld," // UsrCod
"NOW()," // StartTime
"NOW()," // EndTime
"%u," // NumQsts
"%u," // NumQstsNotBlank
"'%lf')", // Score
Match.MchCod,Gbl.Usrs.Me.UsrDat.UsrCod,
NumQsts,NumQstsNotBlank,TotalScore);
2019-09-23 01:48:28 +02:00
Str_SetDecimalPointToLocal (); // Return to local system
2019-09-14 12:59:34 +02:00
}
/***** Show current match status *****/
2019-09-26 18:37:10 +02:00
fprintf (Gbl.F.Out,"<div id=\"match\" class=\"MATCH_CONT\">");
2019-09-14 12:59:34 +02:00
Mch_ShowMatchStatusForStd (&Match);
fprintf (Gbl.F.Out,"</div>");
}
2019-09-23 01:48:28 +02:00
/*****************************************************************************/
/******************** Compute match score for a student **********************/
/*****************************************************************************/
2019-09-24 21:29:01 +02:00
static double Mch_ComputeScore (unsigned NumQsts)
2019-09-23 01:48:28 +02:00
{
unsigned NumQst;
double ScoreThisQst;
bool AnswerIsNotBlank;
unsigned Indexes[Tst_MAX_OPTIONS_PER_QUESTION]; // Indexes of all answers of this question
bool AnswersUsr[Tst_MAX_OPTIONS_PER_QUESTION];
2019-09-24 21:29:01 +02:00
double TotalScore = 0.0;
2019-09-23 01:48:28 +02:00
2019-09-24 21:29:01 +02:00
for (NumQst = 0;
NumQst < NumQsts;
2019-09-23 19:17:12 +02:00
NumQst++)
2019-09-23 01:48:28 +02:00
{
2019-09-23 19:17:12 +02:00
/***** Get indexes for this question from string *****/
Tst_GetIndexesFromStr (Gbl.Test.StrIndexesOneQst[NumQst],Indexes);
/***** Get the user's answers for this question from string *****/
Tst_GetAnswersFromStr (Gbl.Test.StrAnswersOneQst[NumQst],AnswersUsr);
/***** Get correct answers of test question from database *****/
Tst_GetCorrectAnswersFromDB (Gbl.Test.QstCodes[NumQst]);
2019-09-23 01:48:28 +02:00
/***** Compute the total score of this question *****/
Tst_ComputeScoreQst (Indexes,AnswersUsr,&ScoreThisQst,&AnswerIsNotBlank);
/***** Compute total score *****/
2019-09-24 21:29:01 +02:00
TotalScore += ScoreThisQst;
2019-09-23 01:48:28 +02:00
}
2019-09-24 21:29:01 +02:00
return TotalScore;
2019-09-23 01:48:28 +02:00
}
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
2019-09-16 00:15:40 +02:00
/*** Get number of users who selected this answer and draw proportional bar **/
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
2019-09-16 00:15:40 +02:00
void Mch_GetAndDrawBarNumUsrsWhoHaveChosenAns (long MchCod,unsigned QstInd,unsigned AnsInd,
unsigned NumAnswerersQst,bool Correct)
2019-09-14 12:59:34 +02:00
{
2019-09-16 00:15:40 +02:00
unsigned NumAnswerersAns;
/***** Get number of users who selected this answer *****/
NumAnswerersAns = Mch_GetNumUsrsWhoHaveChosenAns (MchCod,QstInd,AnsInd);
/***** Show stats of this answer *****/
Mch_DrawBarNumUsrs (NumAnswerersAns,NumAnswerersQst,Correct);
2019-09-14 12:59:34 +02:00
}
/*****************************************************************************/
2019-09-16 00:15:40 +02:00
/******* Get number of users who have answered a question in a match *********/
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
2019-09-16 00:15:40 +02:00
unsigned Mch_GetNumUsrsWhoHaveAnswerQst (long MchCod,unsigned QstInd)
2019-09-14 12:59:34 +02:00
{
2019-09-16 00:15:40 +02:00
/***** Get number of users who have answered
2019-09-17 20:44:29 +02:00
a question in match from database *****/
2019-09-16 00:15:40 +02:00
return
2019-09-23 09:03:55 +02:00
(unsigned) DB_QueryCOUNT ("can not get number of users who have answered a question",
2019-09-17 01:37:07 +02:00
"SELECT COUNT(*) FROM mch_answers"
2019-09-16 00:15:40 +02:00
" WHERE MchCod=%ld AND QstInd=%u",
MchCod,QstInd);
2019-09-14 12:59:34 +02:00
}
/*****************************************************************************/
2019-09-16 00:15:40 +02:00
/*** Get number of users who have chosen a given answer of a game question ***/
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
2019-09-16 00:15:40 +02:00
static unsigned Mch_GetNumUsrsWhoHaveChosenAns (long MchCod,unsigned QstInd,unsigned AnsInd)
2019-09-14 12:59:34 +02:00
{
/***** Get number of users who have chosen
an answer of a question from database *****/
2019-09-16 00:15:40 +02:00
return
(unsigned) DB_QueryCOUNT ("can not get number of users who have chosen an answer",
2019-09-17 01:37:07 +02:00
"SELECT COUNT(*) FROM mch_answers"
2019-09-16 00:15:40 +02:00
" WHERE MchCod=%ld AND QstInd=%u AND AnsInd=%u",
MchCod,QstInd,AnsInd);
2019-09-14 12:59:34 +02:00
}
2019-09-17 20:44:29 +02:00
/*****************************************************************************/
/****** Get number of users who have answered any question in a match ********/
/*****************************************************************************/
static unsigned Mch_GetNumUsrsWhoHaveAnswerMch (long MchCod)
{
/***** Get number of users who have answered
any question in match from database *****/
return
2019-09-23 09:03:55 +02:00
(unsigned) DB_QueryCOUNT ("can not get number of users who have answered a match",
2019-09-17 20:44:29 +02:00
"SELECT COUNT(DISTINCT UsrCod) FROM mch_answers"
" WHERE MchCod=%ld",
MchCod);
}
2019-09-14 12:59:34 +02:00
/*****************************************************************************/
/***************** Draw a bar with the percentage of answers *****************/
/*****************************************************************************/
2019-09-16 00:15:40 +02:00
// #define Mch_MAX_BAR_WIDTH 400
#define Mch_MAX_BAR_WIDTH 100
2019-09-14 12:59:34 +02:00
2019-09-16 00:15:40 +02:00
static void Mch_DrawBarNumUsrs (unsigned NumAnswerersAns,unsigned NumAnswerersQst,bool Correct)
2019-09-14 12:59:34 +02:00
{
extern const char *Txt_of_PART_OF_A_TOTAL;
2019-09-16 00:15:40 +02:00
unsigned i;
2019-09-14 12:59:34 +02:00
unsigned BarWidth = 0;
/***** String with the number of users *****/
2019-09-16 00:15:40 +02:00
if (NumAnswerersQst)
2019-09-14 12:59:34 +02:00
snprintf (Gbl.Title,sizeof (Gbl.Title),
"%u&nbsp;(%u%%&nbsp;%s&nbsp;%u)",
2019-09-16 00:15:40 +02:00
NumAnswerersAns,
(unsigned) ((((float) NumAnswerersAns * 100.0) / (float) NumAnswerersQst) + 0.5),
Txt_of_PART_OF_A_TOTAL,NumAnswerersQst);
2019-09-14 12:59:34 +02:00
else
snprintf (Gbl.Title,sizeof (Gbl.Title),
"0&nbsp;(0%%&nbsp;%s&nbsp;%u)",
2019-09-16 00:15:40 +02:00
Txt_of_PART_OF_A_TOTAL,NumAnswerersQst);
/***** Start container *****/
fprintf (Gbl.F.Out,"<div class=\"MATCH_RESULT\">");
2019-09-14 12:59:34 +02:00
/***** Draw bar with a with proportional to the number of clicks *****/
2019-09-16 00:15:40 +02:00
if (NumAnswerersAns && NumAnswerersQst)
BarWidth = (unsigned) ((((float) NumAnswerersAns * (float) Mch_MAX_BAR_WIDTH) /
(float) NumAnswerersQst) + 0.5);
/***** Bar proportional to number of users *****/
Tbl_StartTableWide (0);
fprintf (Gbl.F.Out,"<tr class=\"MATCH_RES_TR\">");
for (i = 0;
i < 100;
i++)
fprintf (Gbl.F.Out,"<td class=\"%s\"></td>",
(i < BarWidth) ? (Correct ? "MATCH_RES_CORRECT" :
"MATCH_RES_WRONG") :
"MATCH_RES_VOID");
fprintf (Gbl.F.Out,"</tr>");
Tbl_EndTable ();
2019-09-14 12:59:34 +02:00
/***** Write the number of users *****/
fprintf (Gbl.F.Out,"%s",Gbl.Title);
2019-09-16 00:15:40 +02:00
/***** End container *****/
fprintf (Gbl.F.Out,"</div>");
2019-09-14 12:59:34 +02:00
}
2019-09-29 22:38:32 +02:00
/*****************************************************************************/
/**************** Access to variable used to pass parameter ******************/
/*****************************************************************************/
2019-09-30 01:10:57 +02:00
void Mch_SetParamCurrentMchCod (long MchCod)
2019-09-29 22:38:32 +02:00
{
Mch_CurrentMchCod = MchCod;
}
static long Mch_GetParamCurrentMchCod (void)
{
return Mch_CurrentMchCod;
}