From b3dad62d7a5d50bf28fcab726deb6a18897babf3 Mon Sep 17 00:00:00 2001 From: acanas Date: Thu, 16 Sep 2021 17:10:32 +0200 Subject: [PATCH] Version 21.5.2: Sep 16, 2021 Queries moved to module swad_forum_database. --- swad_changelog.h | 3 +- swad_forum.c | 102 +++++---------------------------- swad_forum_database.c | 130 ++++++++++++++++++++++++++++++++++++++++++ swad_forum_database.h | 7 +++ 4 files changed, 154 insertions(+), 88 deletions(-) diff --git a/swad_changelog.h b/swad_changelog.h index 78b774d69..36a590eb8 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.5.1 (2021-09-15)" +#define Log_PLATFORM_VERSION "SWAD 21.5.2 (2021-09-16)" #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.5.2: Sep 16, 2021 Queries moved to module swad_forum_database. (315403 lines) Version 21.5.1: Sep 15, 2021 Queries moved to module swad_forum_database. (315348 lines) Version 21.5: Sep 15, 2021 New module swad_forum_database for database queries related to forums. (315354 lines) Version 21.4: Sep 15, 2021 New module swad_firewall_database for database queries related to firewall. (315286 lines) diff --git a/swad_forum.c b/swad_forum.c index e8677f7e7..9874bfb6a 100644 --- a/swad_forum.c +++ b/swad_forum.c @@ -596,14 +596,7 @@ void For_GetForumTypeAndLocationOfAPost (long PstCod,struct For_Forum *Forum) Forum->Location = -1L; /***** Check if there is a row with forum type *****/ - if (DB_QuerySELECT (&mysql_res,"can not get forum type and location", - "SELECT for_threads.ForumType," // row[0] - "for_threads.Location" // row[1] - " FROM for_posts," - "for_threads" - " WHERE for_posts.PstCod=%ld" - " AND for_posts.ThrCod=for_threads.ThrCod", - PstCod)) + if (For_DB_GetForumTypeAndLocationOfAPost (&mysql_res,PstCod)) { row = mysql_fetch_row (mysql_res); @@ -632,13 +625,7 @@ static time_t For_GetThrReadTime (long ThrCod) time_t ReadTimeUTC; /***** Get read time of a thread from database *****/ - if (DB_QuerySELECT (&mysql_res,"can not get date of reading" - " of a thread of a forum", - "SELECT UNIX_TIMESTAMP(ReadTime)" // row[0] - " FROM for_read" - " WHERE ThrCod=%ld" - " AND UsrCod=%ld", - ThrCod,Gbl.Usrs.Me.UsrDat.UsrCod)) + if (For_DB_GetThrReadTime (&mysql_res,ThrCod)) { /***** There is a row ==> get read time *****/ row = mysql_fetch_row (mysql_res); @@ -646,7 +633,8 @@ static time_t For_GetThrReadTime (long ThrCod) ReadTimeUTC = Dat_GetUNIXTimeFromStr (row[0]); } else - ReadTimeUTC = (time_t) 0; // If there is no row for this thread and current user, then current user has not read this thread + ReadTimeUTC = (time_t) 0; // If there is no row for this thread and current user, + // then current user has not read this thread /***** Free structure that stores the query result *****/ DB_FreeMySQLResult (&mysql_res); @@ -706,17 +694,8 @@ static void For_ShowPostsOfAThread (struct For_Forums *Forums, Hlp_COMMUNICATION_Forums_posts,Box_NOT_CLOSABLE); /***** Get posts of a thread from database *****/ - NumPsts = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get posts of a thread", - "SELECT PstCod," // row[0] - "UNIX_TIMESTAMP(CreatTime)" // row[1] - " FROM for_posts" - " WHERE ThrCod=%ld" - " ORDER BY PstCod", - Thread.ThrCod); - LastSubject[0] = '\0'; - if (NumPsts) // If there are posts... + if ((NumPsts = For_DB_GetPostsOfAThread (&mysql_res,Thread.ThrCod))) // If there are posts... { /***** Check if I can moderate posts in forum *****/ switch (Forums->Forum.Type) @@ -1024,15 +1003,7 @@ static void For_GetPstData (long PstCod,long *UsrCod,time_t *CreatTimeUTC, MYSQL_ROW row; /***** Get data of a post from database *****/ - if (DB_QuerySELECT (&mysql_res,"can not get data of a post", - "SELECT UsrCod," // row[0] - "UNIX_TIMESTAMP(CreatTime)," // row[1] - "Subject," // row[2] - "Content," // row[3] - "MedCod" // row[4] - " FROM for_posts" - " WHERE PstCod=%ld", - PstCod) != 1) + if (For_DB_GetPstData (&mysql_res,PstCod) != 1) Err_WrongPostExit (); /***** Get number of rows *****/ @@ -1071,12 +1042,7 @@ void For_GetSummaryAndContentForumPst (char SummaryStr[Ntf_MAX_BYTES_SUMMARY + 1 SummaryStr[0] = '\0'; // Return nothing on error /***** Get post subject and content from database *****/ - if (DB_QuerySELECT (&mysql_res,"can not get subject and content", - "SELECT Subject," // row[0] - "Content" // row[1] - " FROM for_posts" - " WHERE PstCod=%ld", - PstCod) == 1) + if (For_DB_GetPstSubjectAndContent (&mysql_res,PstCod) == 1) { /***** Get subject and content of the post *****/ row = mysql_fetch_row (mysql_res); @@ -2050,7 +2016,6 @@ static void For_ShowForumThreadsHighlightingOneThread (struct For_Forums *Forums extern const char *Txt_Unread_BR_msgs; extern const char *Txt_WriBRters; extern const char *Txt_ReaBRders; - char SubQuery[256]; MYSQL_RES *mysql_res; char FrameTitle[128 + For_MAX_BYTES_FORUM_NAME]; char ForumName[For_MAX_BYTES_FORUM_NAME + 1]; @@ -2066,42 +2031,7 @@ static void For_ShowForumThreadsHighlightingOneThread (struct For_Forums *Forums ForumName,Gbl.Prefs.Language,true); /***** Get threads of a forum from database *****/ - if (Forums->Forum.Location > 0) - sprintf (SubQuery," AND for_threads.Location=%ld", - Forums->Forum.Location); - else - SubQuery[0] = '\0'; - switch (Forums->ThreadsOrder) - { - case Dat_START_TIME: // First post time - NumThrs = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get thread of a forum", - "SELECT for_threads.ThrCod" // row[0] - " FROM for_threads," - "for_posts" - " WHERE for_threads.ForumType=%u" - "%s" - " AND for_threads.FirstPstCod=for_posts.PstCod" - " ORDER BY for_posts.CreatTime DESC", - (unsigned) Forums->Forum.Type, - SubQuery); - break; - case Dat_END_TIME: // Last post time - NumThrs = (unsigned) - DB_QuerySELECT (&mysql_res,"can not get thread of a forum", - "SELECT for_threads.ThrCod" // row[0] - " FROM for_threads," - "for_posts" - " WHERE for_threads.ForumType=%u" - "%s" - " AND for_threads.LastPstCod=for_posts.PstCod" - " ORDER BY for_posts.CreatTime DESC", - (unsigned) Forums->Forum.Type, - SubQuery); - break; - default: // Impossible - return; - } + NumThrs = For_DB_GetForumThreads (&mysql_res,Forums); /***** Compute variables related to pagination of threads *****/ PaginationThrs.NumItems = NumThrs; @@ -2197,7 +2127,7 @@ static void For_ShowForumThreadsHighlightingOneThread (struct For_Forums *Forums /***** Put a form to write the first post of a new thread *****/ HTM_SECTION_Begin (For_NEW_THREAD_SECTION_ID); - For_WriteFormForumPst (Forums,false,NULL); + For_WriteFormForumPst (Forums,false,NULL); HTM_SECTION_End (); /***** End box with threads of this forum ****/ @@ -2439,19 +2369,17 @@ static void For_GetThreadData (struct For_Thread *Thr) and the last message (row[1]) in this thread *****/ if (sscanf (row[0],"%ld",&(Thr->PstCod[Dat_START_TIME])) != 1) Err_WrongPostExit (); - if (sscanf (row[1],"%ld",&(Thr->PstCod[Dat_END_TIME])) != 1) + if (sscanf (row[1],"%ld",&(Thr->PstCod[Dat_END_TIME ])) != 1) Err_WrongPostExit (); - /***** Get the author of the first post in this thread (row[2]) *****/ + /***** Get the author of the first post in this thread (row[2]) + and the author of the last post in this thread (row[3]) *****/ Thr->UsrCod[Dat_START_TIME] = Str_ConvertStrCodToLongCod (row[2]); - - /***** Get the author of the last post in this thread (row[3]) *****/ Thr->UsrCod[Dat_END_TIME ] = Str_ConvertStrCodToLongCod (row[3]); - /***** Get the date of the first post in this thread (row[4]) *****/ + /***** Get the date of the first post in this thread (row[4]) + and the date of the last post in this thread (row[5]) *****/ Thr->WriteTime[Dat_START_TIME] = Dat_GetUNIXTimeFromStr (row[4]); - - /***** Get the date of the last post in this thread (row[5]) *****/ Thr->WriteTime[Dat_END_TIME ] = Dat_GetUNIXTimeFromStr (row[5]); /***** Get the subject of this thread (row[6]) *****/ @@ -2463,7 +2391,7 @@ static void For_GetThreadData (struct For_Thread *Thr) DB_FreeMySQLResult (&mysql_res); /***** Get if first or last message are enabled *****/ - for (Order = Dat_START_TIME; + for (Order = Dat_START_TIME; Order <= Dat_END_TIME; Order++) Thr->Enabled[Order] = For_DB_GetIfPstIsEnabled (Thr->PstCod[Order]); diff --git a/swad_forum_database.c b/swad_forum_database.c index a5e1f8bf4..b64933cbe 100644 --- a/swad_forum_database.c +++ b/swad_forum_database.c @@ -237,6 +237,56 @@ bool For_DB_GetIfForumPstExists (long PstCod) PstCod) != 0); // Post exists if it appears in table of forum posts } +/*****************************************************************************/ +/*************************** Get data of a forum post ************************/ +/*****************************************************************************/ + +unsigned For_DB_GetPstData (MYSQL_RES **mysql_res,long PstCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get data of a post", + "SELECT UsrCod," // row[0] + "UNIX_TIMESTAMP(CreatTime)," // row[1] + "Subject," // row[2] + "Content," // row[3] + "MedCod" // row[4] + " FROM for_posts" + " WHERE PstCod=%ld", + PstCod); + } + +/*****************************************************************************/ +/***************** Get subject and content for a forum post ******************/ +/*****************************************************************************/ + +unsigned For_DB_GetPstSubjectAndContent (MYSQL_RES **mysql_res,long PstCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get subject and content", + "SELECT Subject," // row[0] + "Content" // row[1] + " FROM for_posts" + " WHERE PstCod=%ld", + PstCod); + } + +/*****************************************************************************/ +/*************** Get the forum type and location of a post *******************/ +/*****************************************************************************/ + +unsigned For_DB_GetForumTypeAndLocationOfAPost (MYSQL_RES **mysql_res,long PstCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get forum type and location", + "SELECT for_threads.ForumType," // row[0] + "for_threads.Location" // row[1] + " FROM for_posts," + "for_threads" + " WHERE for_posts.PstCod=%ld" + " AND for_posts.ThrCod=for_threads.ThrCod", + PstCod); + } + /*****************************************************************************/ /*********************** Get number of posts from a user *********************/ /*****************************************************************************/ @@ -250,6 +300,22 @@ unsigned For_DB_GetNumPostsUsr (long UsrCod) UsrCod); } +/*****************************************************************************/ +/************************* Get posts of a thread *****************************/ +/*****************************************************************************/ + +unsigned For_DB_GetPostsOfAThread (MYSQL_RES **mysql_res,long ThrCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get posts of a thread", + "SELECT PstCod," // row[0] + "UNIX_TIMESTAMP(CreatTime)" // row[1] + " FROM for_posts" + " WHERE ThrCod=%ld" + " ORDER BY PstCod", + ThrCod); + } + /*****************************************************************************/ /*********** Get the number of posts in the thread than holds a post *********/ /*****************************************************************************/ @@ -344,6 +410,53 @@ void For_DB_UpdateThrLastPst (long ThrCod,long LastPstCod) ThrCod); } +/*****************************************************************************/ +/********** Show available threads of a forum highlighting a thread **********/ +/*****************************************************************************/ + +unsigned For_DB_GetForumThreads (MYSQL_RES **mysql_res, + const struct For_Forums *Forums) + { + char SubQuery[256]; + + /***** Get threads of a forum from database *****/ + if (Forums->Forum.Location > 0) + sprintf (SubQuery," AND for_threads.Location=%ld", + Forums->Forum.Location); + else + SubQuery[0] = '\0'; + + switch (Forums->ThreadsOrder) + { + case Dat_START_TIME: // First post time + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get thread of a forum", + "SELECT for_threads.ThrCod" // row[0] + " FROM for_threads," + "for_posts" + " WHERE for_threads.ForumType=%u" + "%s" + " AND for_threads.FirstPstCod=for_posts.PstCod" + " ORDER BY for_posts.CreatTime DESC", + (unsigned) Forums->Forum.Type, + SubQuery); + case Dat_END_TIME: // Last post time + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get thread of a forum", + "SELECT for_threads.ThrCod" // row[0] + " FROM for_threads," + "for_posts" + " WHERE for_threads.ForumType=%u" + "%s" + " AND for_threads.LastPstCod=for_posts.PstCod" + " ORDER BY for_posts.CreatTime DESC", + (unsigned) Forums->Forum.Type, + SubQuery); + default: // Impossible + return 0; + } + } + /*****************************************************************************/ /***************************** Get data of a thread **************************/ /*****************************************************************************/ @@ -545,6 +658,23 @@ unsigned For_DB_GetNumReadersOfThr (long ThrCod) ThrCod); } +/*****************************************************************************/ +/****************** Get thread read time for the current user ****************/ +/*****************************************************************************/ + +unsigned For_DB_GetThrReadTime (MYSQL_RES **mysql_res,long ThrCod) + { + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get date of reading" + " of a thread of a forum", + "SELECT UNIX_TIMESTAMP(ReadTime)" // row[0] + " FROM for_read" + " WHERE ThrCod=%ld" + " AND UsrCod=%ld", + ThrCod, + Gbl.Usrs.Me.UsrDat.UsrCod); + } + /*****************************************************************************/ /********************* Remove thread read status for a thread ****************/ /*****************************************************************************/ diff --git a/swad_forum_database.h b/swad_forum_database.h index 2e3a7d408..a41681694 100644 --- a/swad_forum_database.h +++ b/swad_forum_database.h @@ -50,7 +50,11 @@ long For_DB_InsertForumPst (long ThrCod,long UsrCod, void For_DB_UpdateNumUsrsNotifiedByEMailAboutPost (long PstCod, unsigned NumUsrsToBeNotifiedByEMail); bool For_DB_GetIfForumPstExists (long PstCod); +unsigned For_DB_GetPstData (MYSQL_RES **mysql_res,long PstCod); +unsigned For_DB_GetPstSubjectAndContent (MYSQL_RES **mysql_res,long PstCod); +unsigned For_DB_GetForumTypeAndLocationOfAPost (MYSQL_RES **mysql_res,long PstCod); unsigned For_DB_GetNumPostsUsr (long UsrCod); +unsigned For_DB_GetPostsOfAThread (MYSQL_RES **mysql_res,long ThrCod); unsigned For_DB_GetThreadAndNumPostsGivenPstCod (MYSQL_RES **mysql_res,long PstCod); void For_DB_RemovePst (long PstCod); void For_DB_RemoveThreadPsts (long ThrCod); @@ -60,6 +64,8 @@ long For_DB_InsertForumThread (const struct For_Forums *Forums, long FirstPstCod); void For_DB_UpdateThrFirstAndLastPst (long ThrCod,long FirstPstCod,long LastPstCod); void For_DB_UpdateThrLastPst (long ThrCod,long LastPstCod); +unsigned For_DB_GetForumThreads (MYSQL_RES **mysql_res, + const struct For_Forums *Forums); unsigned For_DB_GetThreadData (MYSQL_RES **mysql_res,long ThrCod); bool For_DB_CheckIfThrBelongsToForum (long ThrCod,const struct For_Forum *Forum); long For_DB_GetThrLastPst (long ThrCod); @@ -74,6 +80,7 @@ void For_DB_RemoveThread (long ThrCod); void For_DB_UpdateThrReadTime (long ThrCod, time_t CreatTimeUTCOfTheMostRecentPostRead); unsigned For_DB_GetNumReadersOfThr (long ThrCod); +unsigned For_DB_GetThrReadTime (MYSQL_RES **mysql_res,long ThrCod); void For_DB_RemoveThrFromReadThrs (long ThrCod); void For_DB_RemoveUsrFromReadThrs (long UsrCod);