Version 21.16.2: Sep 25, 2021 Code refactoring related to error alerts.

This commit is contained in:
acanas 2021-09-25 11:43:52 +02:00
parent fb7bc2b0c8
commit 1ed23c7c72
14 changed files with 131 additions and 85 deletions

View File

@ -1621,7 +1621,7 @@ static void Ctr_ReceiveFormRequestOrCreateCtr (Ctr_Status_t Status)
/* Get place */
if ((Ctr_EditingCtr->PlcCod = Plc_GetParamPlcCod ()) < 0) // 0 is reserved for "other place"
Ale_ShowAlert (Ale_ERROR,"Wrong place.");
Err_WrongPlaceExit ();
/* Get center short name and full name */
Par_GetParToText ("ShortName",Ctr_EditingCtr->ShrtName,Cns_HIERARCHY_MAX_BYTES_SHRT_NAME);

View File

@ -602,13 +602,14 @@ TODO: FIX BUG, URGENT! En las fechas como par
TODO: En las encuestas, que los estudiantes no puedan ver los resultados hasta que no finalice el plazo.
*/
#define Log_PLATFORM_VERSION "SWAD 21.16.1 (2021-09-24)"
#define Log_PLATFORM_VERSION "SWAD 21.16.2 (2021-09-25)"
#define CSS_FILE "swad20.45.css"
#define JS_FILE "swad20.69.1.js"
/*
TODO: Rename CENTRE to CENTER in help wiki.
TODO: Rename ASSESSMENT.Announcements to ASSESSMENT.Calls_for_exams
Version 21.16.2: Sep 25, 2021 Code refactoring related to error alerts. (317087 lines)
Version 21.16.1: Sep 24, 2021 Queries moved to module swad_match_database. (317054 lines)
Version 21.16: Sep 24, 2021 New module swad_match_database for database queries related to game matches. (316995 lines)
Version 21.15: Sep 24, 2021 New module swad_mark_database for database queries related to files of marks. (316792 lines)

View File

@ -391,6 +391,11 @@ void Err_WrongAnswerIndexExit (void)
Err_ShowErrorAndExit ("Wrong answer index.");
}
void Err_WrongAnswerTypeExit (void)
{
Err_ShowErrorAndExit ("Wrong answer type.");
}
/*****************************************************************************/
/*********** Write error message and exit when wrong exam session ************/
/*****************************************************************************/
@ -516,6 +521,17 @@ void Err_WrongUserExit (void)
Err_ShowErrorAndExit (Txt_The_user_does_not_exist);
}
/*****************************************************************************/
/******** Write error message when no permission to perform an action ********/
/*****************************************************************************/
void Err_NoPermission (void)
{
extern const char *Txt_You_dont_have_permission_to_perform_this_action;
Ale_ShowAlert (Ale_ERROR,Txt_You_dont_have_permission_to_perform_this_action);
}
/*****************************************************************************/
/*** Write error message and exit when no permission to perform an action ****/
/*****************************************************************************/

View File

@ -81,6 +81,7 @@ void Err_WrongQuestionExit (void);
void Err_WrongQuestionIndexExit (void);
void Err_WrongAnswerExit (void);
void Err_WrongAnswerIndexExit (void);
void Err_WrongAnswerTypeExit (void);
void Err_WrongExamSessionExit (void);
void Err_WrongGameExit (void);
void Err_WrongMatchExit (void);
@ -96,6 +97,7 @@ void Err_WrongMessageExit (void);
void Err_WrongMailDomainExit (void);
void Err_WrongRoleExit (void);
void Err_WrongUserExit (void);
void Err_NoPermission (void);
void Err_NoPermissionExit (void);
void Err_ShowErrorAndExit (const char *Txt);

View File

@ -1175,7 +1175,7 @@ unsigned Exa_DB_GetQstAnswersFromSet (MYSQL_RES **mysql_res,long QstCod,bool Shu
"AnsInd");
if (!NumOptions)
Ale_ShowAlert (Ale_ERROR,"Error when getting answers of a question.");
Err_WrongAnswerExit ();
return NumOptions;
}

View File

@ -198,7 +198,7 @@ static void Mch_WriteAnswersMatchResult (const struct Mch_Match *Match,
static void Mch_WriteChoiceAnsViewMatch (const struct Mch_Match *Match,
struct Tst_Question *Question,
const char *Class,bool ShowResult);
static bool Mch_ShowQuestionAndAnswersStd (const struct Mch_Match *Match,
static void Mch_ShowQuestionAndAnswersStd (const struct Mch_Match *Match,
const struct Mch_UsrAnswer *UsrAnswer,
Mch_Update_t Update);
@ -218,7 +218,6 @@ static void Mch_PutBigButtonClose (void);
static void Mch_ShowWaitImage (const char *Txt);
static void Mch_DB_RemoveOldPlayers (void);
static void Mch_DB_UpdateMatchAsBeingPlayed (long MchCod);
static void Mch_DB_SetMatchAsNotBeingPlayed (long MchCod);
static bool Mch_DB_GetIfMatchIsBeingPlayed (long MchCod);
@ -2528,8 +2527,7 @@ static void Mch_ShowRightColumnStd (struct Mch_Match *Match,
{
if (Match->Status.Showing == Mch_ANSWERS) // Teacher's screen is showing question answers
/* Show current question and possible answers */
if (!Mch_ShowQuestionAndAnswersStd (Match,UsrAnswer,Update))
Ale_ShowAlert (Ale_ERROR,"Wrong question.");
Mch_ShowQuestionAndAnswersStd (Match,UsrAnswer,Update);
}
else
Ale_ShowAlert (Ale_ERROR,"You can not join this match.");
@ -2871,7 +2869,7 @@ static void Mch_ShowQuestionAndAnswersTch (const struct Mch_Match *Match)
/* Write stem */
Tst_WriteQstStem (Question.Stem,"MCH_TCH_STEM",
true); // Visible
true); // Visible
/* Show media */
Med_ShowMedia (&Question.Media,
@ -2882,7 +2880,7 @@ static void Mch_ShowQuestionAndAnswersTch (const struct Mch_Match *Match)
switch (Match->Status.Showing)
{
case Mch_ANSWERS:
if (Match->Status.Playing) // Match is being played
if (Match->Status.Playing) // Match is being played
/* Write answers */
Mch_WriteAnswersMatchResult (Match,
&Question,
@ -2896,7 +2894,7 @@ static void Mch_ShowQuestionAndAnswersTch (const struct Mch_Match *Match)
Mch_WriteAnswersMatchResult (Match,
&Question,
"MCH_TCH_ANS",
true); // Show result
true); // Show result
break;
default:
/* Don't write anything */
@ -2927,7 +2925,7 @@ static void Mch_WriteAnswersMatchResult (const struct Mch_Match *Match,
Question,
Class,ShowResult);
else
Ale_ShowAlert (Ale_ERROR,"Type of answer not valid in a game.");
Err_WrongAnswerTypeExit ();
}
/*****************************************************************************/
@ -3021,9 +3019,8 @@ static void Mch_WriteChoiceAnsViewMatch (const struct Mch_Match *Match,
/*****************************************************************************/
/***** Show question and its answers when playing a match (as a student) *****/
/*****************************************************************************/
// Return true on valid question, false on invalid question
static bool Mch_ShowQuestionAndAnswersStd (const struct Mch_Match *Match,
static void Mch_ShowQuestionAndAnswersStd (const struct Mch_Match *Match,
const struct Mch_UsrAnswer *UsrAnswer,
Mch_Update_t Update)
{
@ -3079,8 +3076,6 @@ static bool Mch_ShowQuestionAndAnswersStd (const struct Mch_Match *Match,
/***** End table *****/
HTM_TABLE_End ();
return true;
}
/*****************************************************************************/
@ -3114,25 +3109,10 @@ static void Mch_ShowMatchScore (const struct Mch_Match *Match)
NumRowsPerScorePoint = (double) Mch_NUM_ROWS_SCORE / Range;
/***** Get maximum number of users *****/
MaxUsrs = DB_QuerySELECTUnsigned ("can not get max users",
"SELECT MAX(NumUsrs)"
" FROM (SELECT COUNT(*) AS NumUsrs"
" FROM mch_results"
" WHERE MchCod=%ld"
" GROUP BY Score"
" ORDER BY Score) AS Scores",
Match->MchCod);
MaxUsrs = Mch_DB_GetMaxUsrs (Match->MchCod);
/***** Get scores from database *****/
NumScores = (unsigned)
DB_QuerySELECT (&mysql_res,"can not get scores",
"SELECT Score," // row[0]
"COUNT(*) AS NumUsrs" // row[1]
" FROM mch_results"
" WHERE MchCod=%ld"
" GROUP BY Score"
" ORDER BY Score DESC",
Match->MchCod);
NumScores = Mch_DB_GetUsrsPerScore (&mysql_res,Match->MchCod);
/***** Begin table ****/
HTM_TABLE_BeginWide ();
@ -3389,25 +3369,6 @@ static void Mch_ShowWaitImage (const char *Txt)
HTM_DIV_End ();
}
/*****************************************************************************/
/**************************** Remove old players *****************************/
/*****************************************************************************/
static void Mch_DB_RemoveOldPlayers (void)
{
/***** Delete matches not being played by teacher *****/
DB_QueryDELETE ("can not update matches as not being played",
"DELETE FROM mch_playing"
" WHERE TS<FROM_UNIXTIME(UNIX_TIMESTAMP()-%lu)",
Cfg_SECONDS_TO_REFRESH_MATCH_TCH*3);
/***** Delete players (students) who have left matches *****/
DB_QueryDELETE ("can not update match players",
"DELETE FROM mch_players"
" WHERE TS<FROM_UNIXTIME(UNIX_TIMESTAMP()-%lu)",
Cfg_SECONDS_TO_REFRESH_MATCH_STD*3);
}
/*****************************************************************************/
/********************** Update match as being played *************************/
/*****************************************************************************/

View File

@ -639,3 +639,55 @@ unsigned Mch_DB_GetElapsedTimeInMatch (MYSQL_RES **mysql_res,long MchCod)
" WHERE MchCod=%ld",
MchCod);
}
/*****************************************************************************/
/**************************** Remove old players *****************************/
/*****************************************************************************/
void Mch_DB_RemoveOldPlayers (void)
{
/***** Delete matches not being played by teacher *****/
DB_QueryDELETE ("can not update matches as not being played",
"DELETE FROM mch_playing"
" WHERE TS<FROM_UNIXTIME(UNIX_TIMESTAMP()-%lu)",
Cfg_SECONDS_TO_REFRESH_MATCH_TCH*3);
/***** Delete players (students) who have left matches *****/
DB_QueryDELETE ("can not update match players",
"DELETE FROM mch_players"
" WHERE TS<FROM_UNIXTIME(UNIX_TIMESTAMP()-%lu)",
Cfg_SECONDS_TO_REFRESH_MATCH_STD*3);
}
/*****************************************************************************/
/********* Get maximum number of users per score in match results ************/
/*****************************************************************************/
unsigned Mch_DB_GetMaxUsrs (long MchCod)
{
return DB_QuerySELECTUnsigned ("can not get max users",
"SELECT MAX(NumUsrs)"
" FROM (SELECT COUNT(*) AS NumUsrs"
" FROM mch_results"
" WHERE MchCod=%ld"
" GROUP BY Score"
" ORDER BY Score) AS Scores",
MchCod);
}
/*****************************************************************************/
/************** Get number of users per score in match results ***************/
/*****************************************************************************/
unsigned Mch_DB_GetUsrsPerScore (MYSQL_RES **mysql_res,long MchCod)
{
return (unsigned)
DB_QuerySELECT (mysql_res,"can not get scores",
"SELECT Score," // row[0]
"COUNT(*) AS NumUsrs" // row[1]
" FROM mch_results"
" WHERE MchCod=%ld"
" GROUP BY Score"
" ORDER BY Score DESC",
MchCod);
}

View File

@ -88,4 +88,11 @@ unsigned Mch_DB_GetElapsedTimeInQuestion (MYSQL_RES **mysql_res,
long MchCod,unsigned QstInd);
unsigned Mch_DB_GetElapsedTimeInMatch (MYSQL_RES **mysql_res,long MchCod);
//------------------------------- Players -------------------------------------
void Mch_DB_RemoveOldPlayers (void);
//----------------------------- Match results ---------------------------------
unsigned Mch_DB_GetMaxUsrs (long MchCod);
unsigned Mch_DB_GetUsrsPerScore (MYSQL_RES **mysql_res,long MchCod);
#endif

View File

@ -167,6 +167,8 @@ static void Med_AlertThirdPartyCookies (void);
static Med_Type_t Med_GetTypeFromExtAndMIME (const char *Extension,
const char *MIMEType);
static void Med_ErrorProcessingMediaFile (void);
/*****************************************************************************/
/********************** Media (image/video) constructor **********************/
/*****************************************************************************/
@ -839,7 +841,6 @@ static bool Med_DetectIfAnimated (struct Med_Media *Media,
static void Med_ProcessJPG (struct Med_Media *Media,
const char PathFileOrg[PATH_MAX + 1])
{
extern const char *Txt_The_file_could_not_be_processed_successfully;
char PathFileJPGTmp[PATH_MAX + 1]; // Full name of temporary processed file
/***** Convert original media to temporary JPG processed file
@ -856,7 +857,7 @@ static void Med_ProcessJPG (struct Med_Media *Media,
unlink (PathFileJPGTmp);
/* Show error alert */
Ale_ShowAlert (Ale_ERROR,Txt_The_file_could_not_be_processed_successfully);
Med_ErrorProcessingMediaFile ();
}
}
@ -867,7 +868,6 @@ static void Med_ProcessJPG (struct Med_Media *Media,
static void Med_ProcessGIF (struct Med_Media *Media,
const char PathFileOrg[PATH_MAX + 1])
{
extern const char *Txt_The_file_could_not_be_processed_successfully;
extern const char *Txt_The_size_of_the_file_exceeds_the_maximum_allowed_X;
struct stat FileStatus;
char PathFilePNGTmp[PATH_MAX + 1]; // Full name of temporary processed file
@ -899,7 +899,7 @@ static void Med_ProcessGIF (struct Med_Media *Media,
unlink (PathFilePNGTmp);
/* Show error alert */
Ale_ShowAlert (Ale_ERROR,Txt_The_file_could_not_be_processed_successfully);
Med_ErrorProcessingMediaFile ();
}
else // Success
Media->Status = Med_PROCESSED;
@ -911,7 +911,7 @@ static void Med_ProcessGIF (struct Med_Media *Media,
unlink (PathFilePNGTmp);
/* Show error alert */
Ale_ShowAlert (Ale_ERROR,Txt_The_file_could_not_be_processed_successfully);
Med_ErrorProcessingMediaFile ();
}
}
else // Size exceeded
@ -925,7 +925,7 @@ static void Med_ProcessGIF (struct Med_Media *Media,
}
else // Error getting file data
/* Show error alert */
Ale_ShowAlert (Ale_ERROR,Txt_The_file_could_not_be_processed_successfully);
Med_ErrorProcessingMediaFile ();
}
/*****************************************************************************/
@ -935,7 +935,6 @@ static void Med_ProcessGIF (struct Med_Media *Media,
static void Med_ProcessVideo (struct Med_Media *Media,
const char PathFileOrg[PATH_MAX + 1])
{
extern const char *Txt_The_file_could_not_be_processed_successfully;
extern const char *Txt_The_size_of_the_file_exceeds_the_maximum_allowed_X;
struct stat FileStatus;
char PathFileTmp[PATH_MAX + 1]; // Full name of temporary processed file
@ -954,7 +953,7 @@ static void Med_ProcessVideo (struct Med_Media *Media,
Media->Name,Med_Extensions[Media->Type]);
if (rename (PathFileOrg,PathFileTmp)) // Fail
/* Show error alert */
Ale_ShowAlert (Ale_ERROR,Txt_The_file_could_not_be_processed_successfully);
Med_ErrorProcessingMediaFile ();
else // Success
Media->Status = Med_PROCESSED;
}
@ -969,7 +968,7 @@ static void Med_ProcessVideo (struct Med_Media *Media,
}
else // Error getting file data
/* Show error alert */
Ale_ShowAlert (Ale_ERROR,Txt_The_file_could_not_be_processed_successfully);
Med_ErrorProcessingMediaFile ();
}
/*****************************************************************************/
@ -1364,7 +1363,7 @@ static bool Med_MoveTmpFileToDefDir (struct Med_Media *Media,
const char *Extension)
{
char PathFileTmp[PATH_MAX + 1]; // Full name of temporary processed file
char PathFile[PATH_MAX + 1]; // Full name of definitive processed file
char PathFile[PATH_MAX + 1]; // Full name of definitive processed file
/***** Temporary processed media file *****/
snprintf (PathFileTmp,sizeof (PathFileTmp),"%s/%s.%s",
@ -1377,7 +1376,7 @@ static bool Med_MoveTmpFileToDefDir (struct Med_Media *Media,
/***** Move JPG file *****/
if (rename (PathFileTmp,PathFile)) // Fail
{
Ale_ShowAlert (Ale_ERROR,"Can not move file.");
Med_ErrorProcessingMediaFile ();
return false;
}
@ -2113,3 +2112,15 @@ static Med_Type_t Med_GetTypeFromExtAndMIME (const char *Extension,
return Med_TYPE_NONE;
}
/*****************************************************************************/
/******* Write error message when a media file could not be processed ********/
/*****************************************************************************/
static void Med_ErrorProcessingMediaFile (void)
{
extern const char *Txt_The_file_could_not_be_processed_successfully;
Ale_ShowAlert (Ale_ERROR,Txt_The_file_could_not_be_processed_successfully);
}

View File

@ -2992,23 +2992,22 @@ static bool Svy_CheckIfAnswerExists (long QstCod,unsigned AnsInd)
static unsigned Svy_GetAnswersQst (long QstCod,MYSQL_RES **mysql_res)
{
unsigned long NumRows;
unsigned NumAnswers;
/***** Get answers of a question from database *****/
NumRows = DB_QuerySELECT (mysql_res,"can not get answers of a question",
"SELECT AnsInd," // row[0]
"NumUsrs," // row[1]
"Answer" // row[2]
" FROM svy_answers"
" WHERE QstCod=%ld"
" ORDER BY AnsInd",
QstCod);
NumAnswers = (unsigned)
DB_QuerySELECT (mysql_res,"can not get answers of a question",
"SELECT AnsInd," // row[0]
"NumUsrs," // row[1]
"Answer" // row[2]
" FROM svy_answers"
" WHERE QstCod=%ld"
" ORDER BY AnsInd",
QstCod);
if (!NumAnswers)
Err_WrongAnswerExit ();
/***** Count number of rows of result *****/
if (NumRows == 0)
Ale_ShowAlert (Ale_ERROR,"Error when getting answers of a question.");
return (unsigned) NumRows;
return NumAnswers;
}
/*****************************************************************************/

View File

@ -2699,7 +2699,7 @@ void Tst_GetAnswersQst (struct Tst_Question *Question,MYSQL_RES **mysql_res,
Shuffle ? "RAND()" :
"AnsInd");
if (!Question->Answer.NumOptions)
Ale_ShowAlert (Ale_ERROR,"Error when getting answers of a question.");
Err_WrongAnswerExit ();
}
/*****************************************************************************/

View File

@ -956,7 +956,6 @@ void Tml_Com_RemoveComGbl (void)
static void Tml_Com_RemoveComm (void)
{
extern const char *Txt_The_post_no_longer_exists;
extern const char *Txt_You_dont_have_permission_to_perform_this_action;
extern const char *Txt_Comment_removed;
struct Tml_Com_Comment Com;
@ -979,7 +978,7 @@ static void Tml_Com_RemoveComm (void)
if (!Usr_ItsMe (Com.UsrCod))
{
Med_MediaDestructor (&Com.Content.Media);
Ale_ShowAlert (Ale_ERROR,Txt_You_dont_have_permission_to_perform_this_action);
Err_NoPermission ();
return;
}

View File

@ -34,6 +34,7 @@
#include "swad_browser_database.h"
#include "swad_call_for_exam.h"
#include "swad_course.h"
#include "swad_error.h"
#include "swad_forum.h"
#include "swad_global.h"
#include "swad_hierarchy.h"
@ -1214,7 +1215,6 @@ void Tml_Not_RemoveNoteGbl (void)
static void Tml_Not_RemoveNote (void)
{
extern const char *Txt_The_post_no_longer_exists;
extern const char *Txt_You_dont_have_permission_to_perform_this_action;
extern const char *Txt_TIMELINE_Post_removed;
struct Tml_Not_Note Not;
@ -1232,7 +1232,7 @@ static void Tml_Not_RemoveNote (void)
/***** Trivial check 2: Am I the author of this note *****/
if (!Usr_ItsMe (Not.UsrCod))
{
Ale_ShowAlert (Ale_ERROR,Txt_You_dont_have_permission_to_perform_this_action);
Err_NoPermission ();
return;
}

View File

@ -348,7 +348,6 @@ static void Tml_Usr_PutDisabledIconFavSha (Tml_Usr_FavSha_t FavSha,
bool Tml_Usr_CheckIfICanFavSha (long Cod,long UsrCod)
{
extern const char *Txt_The_post_no_longer_exists;
extern const char *Txt_You_dont_have_permission_to_perform_this_action;
/***** Trivial check 1: note/comment code should be > 0 *****/
if (Cod <= 0)
@ -361,7 +360,7 @@ bool Tml_Usr_CheckIfICanFavSha (long Cod,long UsrCod)
I can not fav/share my own notes/comments *****/
if (!Gbl.Usrs.Me.Logged || Usr_ItsMe (UsrCod))
{
Ale_ShowAlert (Ale_ERROR,Txt_You_dont_have_permission_to_perform_this_action);
Err_NoPermission ();
return false;
}
@ -375,7 +374,6 @@ bool Tml_Usr_CheckIfICanFavSha (long Cod,long UsrCod)
bool Tml_Usr_CheckIfICanRemove (long Cod,long UsrCod)
{
extern const char *Txt_The_post_no_longer_exists;
extern const char *Txt_You_dont_have_permission_to_perform_this_action;
/***** Trivial check 1: note/comment code should be > 0 *****/
if (Cod <= 0)
@ -388,7 +386,7 @@ bool Tml_Usr_CheckIfICanRemove (long Cod,long UsrCod)
I can only remove my own notes/comments *****/
if (!Gbl.Usrs.Me.Logged || !Usr_ItsMe (UsrCod))
{
Ale_ShowAlert (Ale_ERROR,Txt_You_dont_have_permission_to_perform_this_action);
Err_NoPermission ();
return false;
}