From f207d0e1c66f3c53520b5b9820cb56c3a9bd77a4 Mon Sep 17 00:00:00 2001 From: acanas Date: Thu, 21 Oct 2021 22:28:09 +0200 Subject: [PATCH] Version 21.39.1: Oct 21, 2021 Queries moved to module swad_survey_database. --- swad_changelog.h | 5 +- swad_survey.c | 126 ++++++------------------------------ swad_survey_database.c | 142 ++++++++++++++++++++++++++++++++++++++++- swad_survey_database.h | 15 ++++- 4 files changed, 179 insertions(+), 109 deletions(-) diff --git a/swad_changelog.h b/swad_changelog.h index ae624f08..045515d2 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -602,14 +602,15 @@ 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.39 (2021-10-21)" +#define Log_PLATFORM_VERSION "SWAD 21.39.1 (2021-10-21)" #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.39: Oct 21, 2021 Fixed bug in nicknamed. Reported by Ana Ollarves Reyes. (320487 lines) + Version 21.39.1: Oct 21, 2021 Queries moved to module swad_survey_database. (320540 lines) + Version 21.39: Oct 21, 2021 Fixed bug in nicknames. Reported by Ana Ollarves Reyes. (320487 lines) Version 21.38.1: Oct 20, 2021 Queries moved to module swad_survey_database. (320522 lines) Version 21.38: Oct 20, 2021 New module swad_survey_database for database queries related to surveys. (320457 lines) Version 21.37.1: Oct 20, 2021 Code refactoring in attendance. (320361 lines) diff --git a/swad_survey.c b/swad_survey.c index 487f5cd7..b4e28624 100644 --- a/swad_survey.c +++ b/swad_survey.c @@ -135,7 +135,6 @@ static void Svy_CreateSurvey (struct Svy_Survey *Svy,const char *Txt); static void Svy_UpdateSurvey (struct Svy_Survey *Svy,const char *Txt); static void Svy_CreateGrps (long SvyCod); static void Svy_GetAndWriteNamesOfGrpsAssociatedToSvy (struct Svy_Survey *Svy); -static bool Svy_CheckIfICanDoThisSurveyBasedOnGrps (long SvyCod); static void Svy_ShowFormEditOneQst (struct Svy_Surveys *Surveys, long SvyCod,struct Svy_Question *SvyQst, @@ -143,7 +142,6 @@ static void Svy_ShowFormEditOneQst (struct Svy_Surveys *Surveys, static void Svy_InitQst (struct Svy_Question *SvyQst); static void Svy_PutParamQstCod (long QstCod); static long Svy_GetParamQstCod (void); -static void Svy_DB_RemAnswersOfAQuestion (long QstCod); static Svy_AnswerType_t Svy_ConvertFromStrAnsTypDBToAnsTyp (const char *StrAnsTypeBD); static bool Svy_AllocateTextChoiceAnswer (struct Svy_Question *SvyQst,unsigned NumAns); static void Svy_FreeTextChoiceAnswers (struct Svy_Question *SvyQst,unsigned NumAnswers); @@ -1134,25 +1132,9 @@ void Svy_GetDataOfSurveyByCod (struct Svy_Survey *Svy) { MYSQL_RES *mysql_res; MYSQL_ROW row; - unsigned long NumRows; /***** Get data of survey from database *****/ - NumRows = DB_QuerySELECT (&mysql_res,"can not get survey data", - "SELECT SvyCod," // row[0] - "Scope," // row[1] - "Cod," // row[2] - "Hidden," // row[3] - "Roles," // row[4] - "UsrCod," // row[5] - "UNIX_TIMESTAMP(StartTime)," // row[6] - "UNIX_TIMESTAMP(EndTime)," // row[7] - "NOW() BETWEEN StartTime AND EndTime," // row[8] - "Title" - " FROM svy_surveys" - " WHERE SvyCod=%ld", - Svy->SvyCod); - - if (NumRows) // Survey found... + if (Svy_DB_GetDataOfSurveyByCod (&mysql_res,Svy->SvyCod)) // Survey found... { /* Get row */ row = mysql_fetch_row (mysql_res); @@ -1218,7 +1200,7 @@ void Svy_GetDataOfSurveyByCod (struct Svy_Survey *Svy) break; case HieLvl_CRS: // Course Svy->Status.IBelongToScope = Usr_CheckIfIBelongToCrs (Svy->Cod) && - Svy_CheckIfICanDoThisSurveyBasedOnGrps (Svy->SvyCod); + Svy_DB_CheckIfICanDoThisSurveyBasedOnGrps (Svy->SvyCod); break; } @@ -1371,13 +1353,8 @@ void Svy_GetNotifSurvey (char SummaryStr[Ntf_MAX_BYTES_SUMMARY + 1], SummaryStr[0] = '\0'; // Return nothing on error - /***** Build query *****/ - if (DB_QuerySELECT (&mysql_res,"can not get groups of a survey", - "SELECT Title," // row[0] - "Txt" // row[1] - " FROM svy_surveys" - " WHERE SvyCod=%ld", - SvyCod) == 1) + /***** Get title and text *****/ + if (Svy_DB_GetSurveyTitleAndText (&mysql_res,SvyCod)) { /***** Get row *****/ row = mysql_fetch_row (mysql_res); @@ -1485,35 +1462,20 @@ void Svy_RemoveSurvey (void) if (!Svy.Status.ICanEdit) Err_NoPermissionExit (); - /***** Remove all the users in this survey *****/ - DB_QueryDELETE ("can not remove users who are answered a survey", - "DELETE FROM svy_users" - " WHERE SvyCod=%ld", - Svy.SvyCod); + /***** Remove all users in this survey *****/ + Svy_DB_RemoveUsrsWhoHaveAnsweredSvy (Svy.SvyCod); - /***** Remove all the answers in this survey *****/ - DB_QueryDELETE ("can not remove answers of a survey", - "DELETE FROM svy_answers" - " USING svy_questions," - "svy_answers" - " WHERE svy_questions.SvyCod=%ld" - " AND svy_questions.QstCod=svy_answers.QstCod", - Svy.SvyCod); + /***** Remove all answers in this survey *****/ + Svy_DB_RemoveAnswersSvy (Svy.SvyCod); - /***** Remove all the questions in this survey *****/ - DB_QueryDELETE ("can not remove questions of a survey", - "DELETE FROM svy_questions" - " WHERE SvyCod=%ld", - Svy.SvyCod); + /***** Remove all questions in this survey *****/ + Svy_DB_RemoveQstsSvy (Svy.SvyCod); - /***** Remove all the groups of this survey *****/ - Svy_DB_RemoveAllGrpsAssociatedToSurvey (Svy.SvyCod); + /***** Remove all groups of this survey *****/ + Svy_DB_RemoveGrpsAssociatedToSurvey (Svy.SvyCod); /***** Remove survey *****/ - DB_QueryDELETE ("can not remove survey", - "DELETE FROM svy_surveys" - " WHERE SvyCod=%ld", - Svy.SvyCod); + Svy_DB_RemoveSvy (Svy.SvyCod); /***** Mark possible notifications as removed *****/ Ntf_DB_MarkNotifAsRemoved (Ntf_EVENT_SURVEY,Svy.SvyCod); @@ -1606,20 +1568,11 @@ void Svy_ResetSurvey (void) if (!Svy.Status.ICanEdit) Err_NoPermissionExit (); - /***** Remove all the users in this survey *****/ - DB_QueryDELETE ("can not remove users who are answered a survey", - "DELETE FROM svy_users" - " WHERE SvyCod=%ld", - Svy.SvyCod); + /***** Remove all users in this survey *****/ + Svy_DB_RemoveUsrsWhoHaveAnsweredSvy (Svy.SvyCod); - /***** Reset all the answers in this survey *****/ - DB_QueryUPDATE ("can not reset answers of a survey", - "UPDATE svy_answers," - "svy_questions" - " SET svy_answers.NumUsrs=0" - " WHERE svy_questions.SvyCod=%ld" - " AND svy_questions.QstCod=svy_answers.QstCod", - Svy.SvyCod); + /***** Reset all answers in this survey *****/ + Svy_DB_ResetAnswersSvy (Svy.SvyCod); /***** Write message to show the change made *****/ Ale_ShowAlert (Ale_SUCCESS,Txt_Survey_X_reset, @@ -2243,7 +2196,7 @@ static void Svy_UpdateSurvey (struct Svy_Survey *Svy,const char *Txt) /***** Update groups *****/ /* Remove old groups */ - Svy_DB_RemoveAllGrpsAssociatedToSurvey (Svy->SvyCod); + Svy_DB_RemoveGrpsAssociatedToSurvey (Svy->SvyCod); /* Create new groups */ if (Gbl.Crs.Grps.LstGrpsSel.NumGrps) @@ -2326,10 +2279,10 @@ static void Svy_GetAndWriteNamesOfGrpsAssociatedToSvy (struct Svy_Survey *Svy) if (NumRows >= 2) { - if (NumRow == NumRows-2) + if (NumRow == NumRows - 2) HTM_TxtF (" %s ",Txt_and); if (NumRows >= 3) - if (NumRow < NumRows-2) + if (NumRow < NumRows - 2) HTM_Txt (", "); } } @@ -2410,30 +2363,6 @@ void Svy_RemoveSurveys (HieLvl_Level_t Scope,long Cod) Cod); } -/*****************************************************************************/ -/************ Check if I belong to any of the groups of a survey *************/ -/*****************************************************************************/ - -static bool Svy_CheckIfICanDoThisSurveyBasedOnGrps (long SvyCod) - { - /***** Get if I can do a survey from database *****/ - return (DB_QueryCOUNT ("can not check if I can do a survey", - "SELECT COUNT(*)" - " FROM svy_surveys" - " WHERE SvyCod=%ld" - " AND (SvyCod NOT IN" - " (SELECT SvyCod" - " FROM svy_groups)" - " OR" - " SvyCod IN" - " (SELECT svy_groups.SvyCod" - " FROM grp_users," - "svy_groups" - " WHERE grp_users.UsrCod=%ld" - " AND grp_users.GrpCod=svy_groups.GrpCod))", - SvyCod,Gbl.Usrs.Me.UsrDat.UsrCod) != 0); - } - /*****************************************************************************/ /*********** Put a form to edit/create a question in survey *****************/ /*****************************************************************************/ @@ -2709,19 +2638,6 @@ static long Svy_GetParamQstCod (void) return Par_GetParToLong ("QstCod"); } -/*****************************************************************************/ -/********************* Remove answers of a survey question *******************/ -/*****************************************************************************/ - -static void Svy_DB_RemAnswersOfAQuestion (long QstCod) - { - /***** Remove answers *****/ - DB_QueryDELETE ("can not remove the answers of a question", - "DELETE FROM svy_answers" - " WHERE QstCod=%ld", - QstCod); - } - /*****************************************************************************/ /*********** Convert a string with the answer type to answer type ************/ /*****************************************************************************/ @@ -3452,7 +3368,7 @@ void Svy_RemoveQst (void) /***** Remove the question from all the tables *****/ /* Remove answers from this test question */ - Svy_DB_RemAnswersOfAQuestion (SvyQst.QstCod); + Svy_DB_RemoveAnswersQst (SvyQst.QstCod); /* Remove the question itself */ Svy_DB_RemoveQst (SvyQst.QstCod); diff --git a/swad_survey_database.c b/swad_survey_database.c index 769d0f83..f0a961f5 100644 --- a/swad_survey_database.c +++ b/swad_survey_database.c @@ -217,6 +217,44 @@ unsigned Svy_DB_GetListSurveys (MYSQL_RES **mysql_res, return NumSvys; } +/*****************************************************************************/ +/********************* Get survey data using its code ************************/ +/*****************************************************************************/ + +unsigned Svy_DB_GetDataOfSurveyByCod (MYSQL_RES **mysql_res,long SvyCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get survey data", + "SELECT SvyCod," // row[0] + "Scope," // row[1] + "Cod," // row[2] + "Hidden," // row[3] + "Roles," // row[4] + "UsrCod," // row[5] + "UNIX_TIMESTAMP(StartTime)," // row[6] + "UNIX_TIMESTAMP(EndTime)," // row[7] + "NOW() BETWEEN StartTime AND EndTime," // row[8] + "Title" + " FROM svy_surveys" + " WHERE SvyCod=%ld", + SvyCod); + } + +/*****************************************************************************/ +/********************* Get survey data using its code ************************/ +/*****************************************************************************/ + +unsigned Svy_DB_GetSurveyTitleAndText (MYSQL_RES **mysql_res,long SvyCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get groups of a survey", + "SELECT Title," // row[0] + "Txt" // row[1] + " FROM svy_surveys" + " WHERE SvyCod=%ld", + SvyCod); + } + /*****************************************************************************/ /********************** Get survey text from database ************************/ /*****************************************************************************/ @@ -584,11 +622,47 @@ unsigned Svy_DB_GetUsrsFromSurveyExceptMe (MYSQL_RES **mysql_res,long SvyCod) Sco_GetDBStrFromScope (HieLvl_CRS)); } +/*****************************************************************************/ +/****************************** Remove a survey ******************************/ +/*****************************************************************************/ + +void Svy_DB_RemoveSvy (long SvyCod) + { + DB_QueryDELETE ("can not remove survey", + "DELETE FROM svy_surveys" + " WHERE SvyCod=%ld", + SvyCod); + } + +/*****************************************************************************/ +/************ Check if I belong to any of the groups of a survey *************/ +/*****************************************************************************/ + +bool Svy_DB_CheckIfICanDoThisSurveyBasedOnGrps (long SvyCod) + { + return (DB_QueryCOUNT ("can not check if I can do a survey", + "SELECT COUNT(*)" + " FROM svy_surveys" + " WHERE SvyCod=%ld" + " AND (SvyCod NOT IN" + " (SELECT SvyCod" + " FROM svy_groups)" + " OR" + " SvyCod IN" + " (SELECT svy_groups.SvyCod" + " FROM grp_users," + "svy_groups" + " WHERE grp_users.UsrCod=%ld" + " AND grp_users.GrpCod=svy_groups.GrpCod))", + SvyCod, + Gbl.Usrs.Me.UsrDat.UsrCod) != 0); + } + /*****************************************************************************/ /************************* Remove groups of a survey *************************/ /*****************************************************************************/ -void Svy_DB_RemoveAllGrpsAssociatedToSurvey (long SvyCod) +void Svy_DB_RemoveGrpsAssociatedToSurvey (long SvyCod) { DB_QueryDELETE ("can not remove the groups associated to a survey", "DELETE FROM svy_groups" @@ -714,6 +788,33 @@ void Svy_DB_RemoveQst (long QstCod) QstCod); } +/*****************************************************************************/ +/********************* Remove all questions in a survey **********************/ +/*****************************************************************************/ + +void Svy_DB_RemoveQstsSvy (long SvyCod) + { + DB_QueryDELETE ("can not remove questions of a survey", + "DELETE FROM svy_questions" + " WHERE SvyCod=%ld", + SvyCod); + } + +/*****************************************************************************/ +/********************** Reset all answers in a survey ************************/ +/*****************************************************************************/ + +void Svy_DB_ResetAnswersSvy (long SvyCod) + { + DB_QueryUPDATE ("can not reset answers of a survey", + "UPDATE svy_answers," + "svy_questions" + " SET svy_answers.NumUsrs=0" + " WHERE svy_questions.SvyCod=%ld" + " AND svy_questions.QstCod=svy_answers.QstCod", + SvyCod); + } + /*****************************************************************************/ /************ Increase number of users who have marked one answer ************/ /*****************************************************************************/ @@ -770,6 +871,33 @@ unsigned Svy_DB_GetAnswersQst (MYSQL_RES **mysql_res,long QstCod) return NumAnswers; } +/*****************************************************************************/ +/********************* Remove answers of a survey question *******************/ +/*****************************************************************************/ + +void Svy_DB_RemoveAnswersQst (long QstCod) + { + DB_QueryDELETE ("can not remove the answers of a question", + "DELETE FROM svy_answers" + " WHERE QstCod=%ld", + QstCod); + } + +/*****************************************************************************/ +/*********************** Remove all answers in a survey **********************/ +/*****************************************************************************/ + +void Svy_DB_RemoveAnswersSvy (long SvyCod) + { + DB_QueryDELETE ("can not remove answers of a survey", + "DELETE FROM svy_answers" + " USING svy_questions," + "svy_answers" + " WHERE svy_questions.SvyCod=%ld" + " AND svy_questions.QstCod=svy_answers.QstCod", + SvyCod); + } + /*****************************************************************************/ /***************** Register that I have answered this survey *****************/ /*****************************************************************************/ @@ -813,3 +941,15 @@ unsigned Svy_DB_GetNumUsrsWhoHaveAnsweredSvy (long SvyCod) " WHERE SvyCod=%ld", SvyCod); } + +/*****************************************************************************/ +/******************** Remove all the users in this survey ********************/ +/*****************************************************************************/ + +void Svy_DB_RemoveUsrsWhoHaveAnsweredSvy (long SvyCod) + { + DB_QueryDELETE ("can not remove users who are answered a survey", + "DELETE FROM svy_users" + " WHERE SvyCod=%ld", + SvyCod); + } diff --git a/swad_survey_database.h b/swad_survey_database.h index e8d4f0a5..cc820b58 100644 --- a/swad_survey_database.h +++ b/swad_survey_database.h @@ -47,6 +47,8 @@ unsigned Svy_DB_GetListSurveys (MYSQL_RES **mysql_res, unsigned ScopesAllowed, unsigned HiddenAllowed, Dat_StartEndTime_t SelectedOrder); +unsigned Svy_DB_GetDataOfSurveyByCod (MYSQL_RES **mysql_res,long SvyCod); +unsigned Svy_DB_GetSurveyTitleAndText (MYSQL_RES **mysql_res,long SvyCod); void Svy_DB_GetSurveyTxt (long SvyCod,char Txt[Cns_MAX_BYTES_TEXT + 1]); bool Svy_DB_CheckIfSimilarSurveyExists (const struct Svy_Survey *Svy); unsigned Svy_DB_GetNumCrssWithCrsSurveys (HieLvl_Level_t Scope); @@ -54,8 +56,12 @@ unsigned Svy_DB_GetNumCrsSurveys (MYSQL_RES **mysql_res,HieLvl_Level_t Scope); double Svy_DB_GetNumQstsPerCrsSurvey (HieLvl_Level_t Scope); unsigned Svy_DB_GetUsrsFromSurveyExceptMe (MYSQL_RES **mysql_res,long SvyCod); +void Svy_DB_RemoveSvy (long SvyCod); + //---------------------------- Surveys groups --------------------------------- -void Svy_DB_RemoveAllGrpsAssociatedToSurvey (long SvyCod); +bool Svy_DB_CheckIfICanDoThisSurveyBasedOnGrps (long SvyCod); + +void Svy_DB_RemoveGrpsAssociatedToSurvey (long SvyCod); void Svy_DB_RemoveGroupsOfType (long GrpTypCod); void Svy_DB_RemoveGroup (long GrpCod); @@ -68,17 +74,24 @@ unsigned Svy_DB_GetSurveyQsts (MYSQL_RES **mysql_res,long SvyCod); unsigned Svy_DB_GetQstIndFromQstCod (long QstCod); void Svy_DB_RemoveQst (long QstCod); +void Svy_DB_RemoveQstsSvy (long SvyCod); //---------------------------- Surveys answers -------------------------------- +void Svy_DB_ResetAnswersSvy (long SvyCod); void Svy_DB_IncreaseAnswer (long QstCod,unsigned AnsInd); bool Svy_DB_CheckIfAnswerExists (long QstCod,unsigned AnsInd); unsigned Svy_DB_GetAnswersQst (MYSQL_RES **mysql_res,long QstCod); +void Svy_DB_RemoveAnswersQst (long QstCod); +void Svy_DB_RemoveAnswersSvy (long SvyCod); + //--------------------- Users who have answered surveys ----------------------- void Svy_DB_RegisterIHaveAnsweredSvy (long SvyCod); bool Svy_DB_CheckIfIHaveAnsweredSvy (long SvyCod); unsigned Svy_DB_GetNumUsrsWhoHaveAnsweredSvy (long SvyCod); +void Svy_DB_RemoveUsrsWhoHaveAnsweredSvy (long SvyCod); + #endif