diff --git a/swad_center.c b/swad_center.c index 048ac051..0b12fd4b 100644 --- a/swad_center.c +++ b/swad_center.c @@ -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); diff --git a/swad_changelog.h b/swad_changelog.h index 0b143aa7..006d407d 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -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) diff --git a/swad_error.c b/swad_error.c index f9388802..525eb102 100644 --- a/swad_error.c +++ b/swad_error.c @@ -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 ****/ /*****************************************************************************/ diff --git a/swad_error.h b/swad_error.h index 56f79bb9..097cf9a5 100644 --- a/swad_error.h +++ b/swad_error.h @@ -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); diff --git a/swad_exam_database.c b/swad_exam_database.c index 4846d4bb..64eede85 100644 --- a/swad_exam_database.c +++ b/swad_exam_database.c @@ -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; } diff --git a/swad_match.c b/swad_match.c index 6f2266ba..b3fe1a9d 100644 --- a/swad_match.c +++ b/swad_match.c @@ -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 TSStatus = 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); + } + diff --git a/swad_survey.c b/swad_survey.c index 28553959..314a7b28 100644 --- a/swad_survey.c +++ b/swad_survey.c @@ -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; } /*****************************************************************************/ diff --git a/swad_test.c b/swad_test.c index 5dff5649..db5759e4 100644 --- a/swad_test.c +++ b/swad_test.c @@ -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 (); } /*****************************************************************************/ diff --git a/swad_timeline_comment.c b/swad_timeline_comment.c index 66bbc1d5..e2c2f1ad 100644 --- a/swad_timeline_comment.c +++ b/swad_timeline_comment.c @@ -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; } diff --git a/swad_timeline_note.c b/swad_timeline_note.c index c605a037..6d665c78 100644 --- a/swad_timeline_note.c +++ b/swad_timeline_note.c @@ -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; } diff --git a/swad_timeline_user.c b/swad_timeline_user.c index b951f333..93930ad7 100644 --- a/swad_timeline_user.c +++ b/swad_timeline_user.c @@ -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; }