From e6c745e487657bd2978728c88930b4a05d1e923c Mon Sep 17 00:00:00 2001 From: acanas Date: Mon, 22 May 2023 18:28:52 +0200 Subject: [PATCH] Version 22.118: May 22, 2023 Survey comments are stored in database. --- sql/swad.sql | 9 +++++++++ swad_changelog.h | 8 +++++++- swad_database.c | 19 ++++++++++++++++++ swad_survey.c | 45 +++++++++++++++++++++++++++++++++++++----- swad_survey_database.c | 31 +++++++++++++++++++++++++++++ swad_survey_database.h | 4 ++++ 6 files changed, 110 insertions(+), 6 deletions(-) diff --git a/sql/swad.sql b/sql/swad.sql index f2dfc714..3ea3d2d4 100644 --- a/sql/swad.sql +++ b/sql/swad.sql @@ -1413,6 +1413,15 @@ CREATE TABLE IF NOT EXISTS svy_answers ( NumUsrs INT NOT NULL DEFAULT 0, Answer TEXT NOT NULL, UNIQUE INDEX(QstCod,AnsInd)); + -- +-- Table svy_comments: stores the comments to the surveys +-- +CREATE TABLE IF NOT EXISTS svy_comments (" + ComCod INT NOT NULL AUTO_INCREMENT, + QstCod INT NOT NULL, + Comments TEXT NOT NULL, + UNIQUE INDEX(ComCod), + INDEX(QstCod,ComCod)); -- -- Table svy_groups: stores the groups associated to each survey -- diff --git a/swad_changelog.h b/swad_changelog.h index 7f87df62..ed42f882 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -629,10 +629,16 @@ TODO: Emilce Barrera Mesa: Podr TODO: Emilce Barrera Mesa: Mis estudiantes presentan muchas dificultades a la hora de poner la foto porque la plataforma es muy exigente respecto al fondo de la imagen. */ -#define Log_PLATFORM_VERSION "SWAD 22.117 (2023-05-22)" +#define Log_PLATFORM_VERSION "SWAD 22.118 (2023-05-22)" #define CSS_FILE "swad22.107.36.css" #define JS_FILE "swad22.49.js" /* + Version 22.118: May 22, 2023 Survey comments are stored in database. (337285 lines) + 1 change necessary in database: +CREATE TABLE IF NOT EXISTS svy_comments (ComCod INT NOT NULL AUTO_INCREMENT,QstCod INT NOT NULL,Comments TEXT NOT NULL,UNIQUE INDEX(ComCod),INDEX(QstCod,ComCod)); + If you want to use MyISAM: +ALTER TABLE svy_comments ENGINE=MyISAM; + Version 22.117: May 22, 2023 Teachers can allow comments in a survey question. (337196 lines) 1 change necessary in database: ALTER TABLE svy_questions ADD COLUMN CommentsAllowed ENUM('N','Y') NOT NULL DEFAULT 'N' AFTER AnsType; diff --git a/swad_database.c b/swad_database.c index 96b5026e..b488a2c2 100644 --- a/swad_database.c +++ b/swad_database.c @@ -3039,6 +3039,25 @@ mysql> DESCRIBE svy_answers; "Answer TEXT NOT NULL," // Cns_MAX_BYTES_TEXT "UNIQUE INDEX(QstCod,AnsInd))"); + /***** Table svy_comments *****/ +/* +mysql> DESCRIBE svy_comments; ++----------+------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++----------+------+------+-----+---------+----------------+ +| ComCod | int | NO | PRI | NULL | auto_increment | +| QstCod | int | NO | MUL | NULL | | +| Comments | text | NO | | NULL | | ++----------+------+------+-----+---------+----------------+ +3 rows in set (0,00 sec) +*/ + DB_CreateTable ("CREATE TABLE IF NOT EXISTS svy_comments (" + "ComCod INT NOT NULL AUTO_INCREMENT," + "QstCod INT NOT NULL," + "Comments TEXT NOT NULL," // Cns_MAX_BYTES_TEXT + "UNIQUE INDEX(ComCod)," + "INDEX(QstCod,ComCod)"); + /***** Table svy_groups *****/ /* mysql> DESCRIBE svy_groups; diff --git a/swad_survey.c b/swad_survey.c index bc303cb6..cf14d37d 100644 --- a/swad_survey.c +++ b/swad_survey.c @@ -3007,17 +3007,47 @@ static void Svy_WriteCommentsOfAQst (struct Svy_Survey *Svy, struct Svy_Question *SvyQst, bool PutFormAnswerSurvey) { + unsigned NumComments; + unsigned NumCom; + MYSQL_RES *mysql_res; + MYSQL_ROW row; + if (PutFormAnswerSurvey) { - HTM_TEXTAREA_Begin ("name=\"Comments\"" + HTM_TEXTAREA_Begin ("name=\"Com%010u\"" " cols=\"60\" rows=\"4\"" " class=\"INPUT_%s\"", + (unsigned) SvyQst->QstCod, The_GetSuffix ()); HTM_TEXTAREA_End (); } else if (Svy->Status.ICanViewComments) { - HTM_Txt ("Comentarios..."); // TODO + /***** Get comments of this question *****/ + NumComments = Svy_DB_GetCommentsQst (&mysql_res,SvyQst->QstCod); + + /***** Write the answers *****/ + if (NumComments) + { + HTM_OL_Begin (); + + /* Write one row for each user who has commented */ + for (NumCom = 0; + NumCom < NumComments; + NumCom++) + { + row = mysql_fetch_row (mysql_res); + + HTM_LI_Begin (NULL); + HTM_Txt (row[0]); + HTM_LI_End (); + } + + HTM_OL_End (); + } + + /***** Free structure that stores the query result *****/ + DB_FreeMySQLResult (&mysql_res); } } @@ -3223,6 +3253,7 @@ static void Svy_ReceiveAndStoreUserAnswersToASurvey (long SvyCod) const char *Ptr; char UnsignedStr[Cns_MAX_DECIMAL_DIGITS_UINT + 1]; unsigned AnsInd; + char Comments[Cns_MAX_BYTES_TEXT + 1]; /***** Get questions of this survey from database *****/ if ((NumQsts = Svy_DB_GetSurveyQstsCodes (&mysql_res,SvyCod))) @@ -3239,7 +3270,6 @@ static void Svy_ReceiveAndStoreUserAnswersToASurvey (long SvyCod) /* Get possible parameter with the user's answer */ snprintf (ParName,sizeof (ParName),"Ans%010u",(unsigned) QstCod); - // Lay_ShowAlert (Lay_INFO,ParName); Par_GetParMultiToText (ParName,StrAnswersIndexes, Svy_MAX_ANSWERS_PER_QUESTION * (Cns_MAX_DECIMAL_DIGITS_UINT + 1)); Ptr = StrAnswersIndexes; @@ -3251,10 +3281,15 @@ static void Svy_ReceiveAndStoreUserAnswersToASurvey (long SvyCod) // ==> store it in database Svy_DB_IncreaseAnswer (QstCod,AnsInd); } + + /* Get possible parameter with the user's comment */ + snprintf (ParName,sizeof (ParName),"Com%010u",(unsigned) QstCod); + Par_GetParAndChangeFormat (ParName,Comments,Cns_MAX_BYTES_TEXT, + Str_TO_RIGOROUS_HTML,true); + if (Comments[0]) + Svy_DB_CreateComments (QstCod,Comments); } } - else // This survey has no questions - Err_ShowErrorAndExit ("Error: this survey has no questions."); /***** Free structure that stores the query result *****/ DB_FreeMySQLResult (&mysql_res); diff --git a/swad_survey_database.c b/swad_survey_database.c index 2e252b84..82329688 100644 --- a/swad_survey_database.c +++ b/swad_survey_database.c @@ -1207,6 +1207,37 @@ void Svy_DB_RemoveAnswersSvysIn (HieLvl_Level_t Scope,long Cod) Cod); } + +/*****************************************************************************/ +/************** Create new comments for a given survey question **************/ +/*****************************************************************************/ + +void Svy_DB_CreateComments (long QstCod,const char *Comments) + { + DB_QueryINSERT ("can not create comments", + "INSERT INTO svy_comments" + " (QstCod,Comments)" + " VALUES" + " (%ld,'%s')", + QstCod, + Comments); + } + +/*****************************************************************************/ +/************* Get comments to a survey question from database ***************/ +/*****************************************************************************/ + +unsigned Svy_DB_GetCommentsQst (MYSQL_RES **mysql_res,long QstCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get comments of a question", + "SELECT Comments" // row[0] + " FROM svy_comments" + " WHERE QstCod=%ld" + " ORDER BY ComCod", + QstCod); + } + /*****************************************************************************/ /***************** Register that I have answered this survey *****************/ /*****************************************************************************/ diff --git a/swad_survey_database.h b/swad_survey_database.h index 3cc982b0..1b9f993d 100644 --- a/swad_survey_database.h +++ b/swad_survey_database.h @@ -107,6 +107,10 @@ void Svy_DB_RemoveAnswersQst (long QstCod); void Svy_DB_RemoveAnswersSvy (long SvyCod); void Svy_DB_RemoveAnswersSvysIn (HieLvl_Level_t Scope,long Cod); +//---------------------------- Surveys comments ------------------------------- +void Svy_DB_CreateComments (long QstCod,const char *Comments); +unsigned Svy_DB_GetCommentsQst (MYSQL_RES **mysql_res,long QstCod); + //--------------------- Users who have answered surveys ----------------------- void Svy_DB_RegisterIHaveAnsweredSvy (long SvyCod);