From 22358705519aeb81002854b7d5caad5567f187cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Ca=C3=B1as=20Vargas?= Date: Thu, 31 Dec 2015 18:40:45 +0100 Subject: [PATCH] Version 15.87 --- sql/cambios.sql | 8 + sql/swad.sql | 20 ++- swad_action.c | 12 +- swad_action.h | 4 +- swad_changelog.h | 9 +- swad_database.c | 32 +++- swad_exam.c | 2 +- swad_file_browser.c | 16 +- swad_forum.c | 2 +- swad_notice.c | 2 +- swad_social.c | 415 ++++++++++++++++++++++++++++---------------- swad_social.h | 6 +- 12 files changed, 346 insertions(+), 182 deletions(-) diff --git a/sql/cambios.sql b/sql/cambios.sql index 17e18131b..575778419 100644 --- a/sql/cambios.sql +++ b/sql/cambios.sql @@ -11193,3 +11193,11 @@ ALTER TABLE social DROP COLUMN Status; ----- SWAD 15.82 (2015/12/29) ----- CREATE TABLE IF NOT EXISTS social_post (PstCod INT NOT NULL AUTO_INCREMENT,Content LONGTEXT NOT NULL,UNIQUE INDEX(PstCod),FULLTEXT(Content)) ENGINE = MYISAM; + +----- SWAD 15.87 (2015/12/31) ----- + +DROP TABLE IF EXISTS social_timeline; +CREATE TABLE IF NOT EXISTS social_timeline (SocCod BIGINT NOT NULL AUTO_INCREMENT,AuthorCod INT NOT NULL,PublisherCod INT NOT NULL,NotCod BIGINT NOT NULL,TimePublish DATETIME NOT NULL,UNIQUE INDEX(SocCod),INDEX(AuthorCod),INDEX(PublisherCod),INDEX(NotCod),INDEX(TimePublish)); +INSERT INTO social_timeline (AuthorCod,PublisherCod,NotCod,TimePublish) SELECT UsrCod,UsrCod,NotCod,TimeNote FROM social_notes ORDER BY NotCod; + + \ No newline at end of file diff --git a/sql/swad.sql b/sql/swad.sql index 77e5c75d9..085e2da0a 100644 --- a/sql/swad.sql +++ b/sql/swad.sql @@ -913,11 +913,11 @@ CREATE TABLE IF NOT EXISTS sessions ( UNIQUE INDEX(SessionId), INDEX(UsrCod)); -- --- Table social: stores social events (timeline) +-- Table social: stores social notes -- -CREATE TABLE IF NOT EXISTS social ( +CREATE TABLE IF NOT EXISTS social_notes ( NotCod BIGINT NOT NULL AUTO_INCREMENT, - SocialNote TINYINT NOT NULL, + NoteType TINYINT NOT NULL, UsrCod INT NOT NULL, CtyCod INT NOT NULL DEFAULT -1, InsCod INT NOT NULL DEFAULT -1, @@ -939,6 +939,20 @@ CREATE TABLE IF NOT EXISTS social_posts ( UNIQUE INDEX(PstCod), FULLTEXT(Content)) ENGINE = MYISAM; -- +-- Table social_posts: stores social timeline +-- +CREATE TABLE IF NOT EXISTS social_timeline ( + PubCod BIGINT NOT NULL AUTO_INCREMENT, + AuthorCod INT NOT NULL, + PublisherCod INT NOT NULL, + NotCod BIGINT NOT NULL, + TimePublish DATETIME NOT NULL, + UNIQUE INDEX(PubCod), + INDEX(AuthorCod), + INDEX(PublisherCod), + INDEX(NotCod), + INDEX(TimePublish)); +-- -- Table sta_degrees: stores statistics about degrees -- CREATE TABLE IF NOT EXISTS sta_degrees ( diff --git a/swad_action.c b/swad_action.c index dc8b5e84d..f676b1fd2 100644 --- a/swad_action.c +++ b/swad_action.c @@ -1004,8 +1004,8 @@ Social: 839. ActReqSocPst Write a public social post to be displayed in the timeline 840. ActRcvSocPst Receive a public social post to be displayed in the timeline - 841. ActReqRemSocNot Request the removal of a social note (only if it is a post) - 842. ActRemSocNot Remove a social note (only if it is a post) + 841. ActReqRemSocPub Request the removal of a social note (only if it is a post) + 842. ActRemSocPub Remove a social note (only if it is a post) 843. ActSeePubPrf Show a public user's profile 844. ActCal1stClkTim Calculate first click time from log and store into user's figures 845. ActCalNumClk Calculate number of clicks from log and store into user's figures @@ -2315,8 +2315,8 @@ struct Act_Actions Act_Actions[Act_NUM_ACTIONS] = /* ActReqSocPst */{1491,-1,TabSoc,ActSeeSocAct ,0x1FE,0x1FE,0x1FE,Act_CONTENT_NORM,Act_MAIN_WINDOW,NULL ,Soc_FormSocialPost ,NULL}, /* ActRcvSocPst */{1492,-1,TabSoc,ActSeeSocAct ,0x1FE,0x1FE,0x1FE,Act_CONTENT_NORM,Act_MAIN_WINDOW,NULL ,Soc_ReceiveSocialPost ,NULL}, - /* ActReqRemSocNot */{1494,-1,TabSoc,ActSeeSocAct ,0x1FE,0x1FE,0x1FE,Act_CONTENT_NORM,Act_MAIN_WINDOW,NULL ,Soc_RequestRemovalSocialNote ,NULL}, - /* ActRemSocNot */{1493,-1,TabSoc,ActSeeSocAct ,0x1FE,0x1FE,0x1FE,Act_CONTENT_NORM,Act_MAIN_WINDOW,NULL ,Soc_RemoveSocialNote ,NULL}, + /* ActReqRemSocPub */{1494,-1,TabSoc,ActSeeSocAct ,0x1FE,0x1FE,0x1FE,Act_CONTENT_NORM,Act_MAIN_WINDOW,NULL ,Soc_RequestRemovalSocialNote ,NULL}, + /* ActRemSocPub */{1493,-1,TabSoc,ActSeeSocAct ,0x1FE,0x1FE,0x1FE,Act_CONTENT_NORM,Act_MAIN_WINDOW,NULL ,Soc_RemoveSocialPublishing ,NULL}, /* ActSeePubPrf */{1402,-1,TabSoc,ActReqPubPrf ,0x1FF,0x1FF,0x1FF,Act_CONTENT_NORM,Act_MAIN_WINDOW,NULL ,Prf_GetUsrCodAndShowUserProfile,NULL}, /* ActCal1stClkTim */{1405,-1,TabSoc,ActReqPubPrf ,0x1FF,0x1FF,0x1FF,Act_CONTENT_NORM,Act_MAIN_WINDOW,NULL ,Prf_CalculateFirstClickTime ,NULL}, @@ -4157,8 +4157,8 @@ Act_Action_t Act_FromActCodToAction[1+Act_MAX_ACTION_COD] = // Do not reuse uniq ActSeeSocAct, // #1490 ActReqSocPst, // #1491 ActRcvSocPst, // #1492 - ActRemSocNot, // #1493 - ActReqRemSocNot, // #1494 + ActRemSocPub, // #1493 + ActReqRemSocPub, // #1494 }; /*****************************************************************************/ diff --git a/swad_action.h b/swad_action.h index e7ce9e68f..6868c1af1 100644 --- a/swad_action.h +++ b/swad_action.h @@ -1043,8 +1043,8 @@ typedef int Act_Action_t; // Must be a signed type, because -1 is used to indica // Secondary actions #define ActReqSocPst (ActLstClk+ 5) #define ActRcvSocPst (ActLstClk+ 6) -#define ActReqRemSocNot (ActLstClk+ 7) -#define ActRemSocNot (ActLstClk+ 8) +#define ActReqRemSocPub (ActLstClk+ 7) +#define ActRemSocPub (ActLstClk+ 8) #define ActSeePubPrf (ActLstClk+ 9) #define ActCal1stClkTim (ActLstClk+ 10) diff --git a/swad_changelog.h b/swad_changelog.h index 3d75c305f..953fa85ed 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -117,15 +117,18 @@ /****************************** Public constants *****************************/ /*****************************************************************************/ -#define Log_PLATFORM_VERSION "SWAD 15.86.1 (2015-12-31)" +#define Log_PLATFORM_VERSION "SWAD 15.87 (2015-12-31)" #define CSS_FILE "swad15.84.2.css" #define JS_FILE "swad15.77.7.js" // Number of lines (includes comments but not blank lines) has been got with the following command: // nl swad*.c swad*.h css/swad*.css py/swad*.py js/swad*.js soap/swad*.h sql/swad*.sql | tail -1 /* - Version 15.86.2: Dec 31, 2015 New database table to store users who write or share social notes. (? lines) - 1 change necessary in database: + Version 15.87: Dec 31, 2015 New database table to store users who write or share social notes. (189594 lines) + 3 changes necessary in database: +CREATE TABLE IF NOT EXISTS social_timeline (PubCod BIGINT NOT NULL AUTO_INCREMENT,AuthorCod INT NOT NULL,PublisherCod INT NOT NULL,NotCod BIGINT NOT NULL,TimePublish DATETIME NOT NULL,UNIQUE INDEX(PubCod),INDEX(AuthorCod),INDEX(PublisherCod),INDEX(NotCod),INDEX(TimePublish)); +INSERT INTO social_timeline (AuthorCod,PublisherCod,NotCod,TimePublish) SELECT UsrCod,UsrCod,NotCod,TimeNote FROM social_notes ORDER BY NotCod; +ALTER TABLE social_notes CHANGE COLUMN SocialNote NoteType TINYINT NOT NULL; Version 15.86.1: Dec 31, 2015 Change in database table with social notes. (189457 lines) 1 change necessary in database: diff --git a/swad_database.c b/swad_database.c index a9cd4ca65..6ba45a765 100644 --- a/swad_database.c +++ b/swad_database.c @@ -1942,7 +1942,7 @@ mysql> DESCRIBE social_notes; | Field | Type | Null | Key | Default | Extra | +------------+------------+------+-----+---------+----------------+ | NotCod | bigint(20) | NO | PRI | NULL | auto_increment | -| SocialNote | tinyint(4) | NO | MUL | NULL | | +| NoteType | tinyint(4) | NO | MUL | NULL | | | UsrCod | int(11) | NO | MUL | NULL | | | CtyCod | int(11) | NO | | -1 | | | InsCod | int(11) | NO | | -1 | | @@ -1956,7 +1956,7 @@ mysql> DESCRIBE social_notes; */ DB_CreateTable ("CREATE TABLE IF NOT EXISTS social_notes (" "NotCod BIGINT NOT NULL AUTO_INCREMENT," - "SocialNote TINYINT NOT NULL," + "NoteType TINYINT NOT NULL," "UsrCod INT NOT NULL," "CtyCod INT NOT NULL DEFAULT -1," "InsCod INT NOT NULL DEFAULT -1," @@ -1966,7 +1966,7 @@ mysql> DESCRIBE social_notes; "Cod INT NOT NULL DEFAULT -1," "TimeNote DATETIME NOT NULL," "UNIQUE INDEX(NotCod)," - "INDEX(SocialNote)," + "INDEX(NoteType)," "INDEX(UsrCod)," "INDEX(TimeNote))"); @@ -1987,6 +1987,32 @@ mysql> DESCRIBE social_posts; "UNIQUE INDEX(PstCod)," "FULLTEXT(Content)) ENGINE = MYISAM;"); + /***** Table social_timeline *****/ +/* +mysql> DESCRIBE social_timeline; ++--------------+------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++--------------+------------+------+-----+---------+----------------+ +| PubCod | bigint(20) | NO | PRI | NULL | auto_increment | +| AuthorCod | int(11) | NO | MUL | NULL | | +| PublisherCod | int(11) | NO | MUL | NULL | | +| NotCod | bigint(20) | NO | MUL | NULL | | +| TimePublish | datetime | NO | MUL | NULL | | ++--------------+------------+------+-----+---------+----------------+ +5 rows in set (0.00 sec) +*/ + DB_CreateTable ("CREATE TABLE IF NOT EXISTS social_timeline (" + "PubCod BIGINT NOT NULL AUTO_INCREMENT," + "AuthorCod INT NOT NULL," + "PublisherCod INT NOT NULL," + "NotCod BIGINT NOT NULL," + "TimePublish DATETIME NOT NULL," + "UNIQUE INDEX(PubCod)," + "INDEX(AuthorCod)," + "INDEX(PublisherCod)," + "INDEX(NotCod)," + "INDEX(TimePublish))"); + /***** Table sta_degrees *****/ /* mysql> DESCRIBE sta_degrees; diff --git a/swad_exam.c b/swad_exam.c index 4f4b1fe52..1d94c02f9 100644 --- a/swad_exam.c +++ b/swad_exam.c @@ -306,7 +306,7 @@ void Exa_ReceiveExamAnnouncement (void) Ntf_ShowAlertNumUsrsToBeNotifiedByEMail (NumUsrsToBeNotifiedByEMail); /***** Create a new social note about the new exam announcement *****/ - Soc_StoreSocialNote (Soc_NOTE_EXAM_ANNOUNCEMENT,ExaCod); + Soc_StoreAndPublishSocialNote (Soc_NOTE_EXAM_ANNOUNCEMENT,ExaCod); /***** Show exam announcement *****/ Exa_ListExamAnnouncementsEdit (); diff --git a/swad_file_browser.c b/swad_file_browser.c index 4359cba57..7596608f0 100644 --- a/swad_file_browser.c +++ b/swad_file_browser.c @@ -9684,28 +9684,28 @@ void Brw_ChgFileMetadata (void) switch (Gbl.FileBrowser.Type) { case Brw_ADMI_DOCUM_INS: - Soc_StoreSocialNote (Soc_NOTE_INS_DOC_PUB_FILE,FileMetadata.FilCod); + Soc_StoreAndPublishSocialNote (Soc_NOTE_INS_DOC_PUB_FILE,FileMetadata.FilCod); break; case Brw_ADMI_SHARE_INS: - Soc_StoreSocialNote (Soc_NOTE_INS_SHA_PUB_FILE,FileMetadata.FilCod); + Soc_StoreAndPublishSocialNote (Soc_NOTE_INS_SHA_PUB_FILE,FileMetadata.FilCod); break; case Brw_ADMI_DOCUM_CTR: - Soc_StoreSocialNote (Soc_NOTE_CTR_DOC_PUB_FILE,FileMetadata.FilCod); + Soc_StoreAndPublishSocialNote (Soc_NOTE_CTR_DOC_PUB_FILE,FileMetadata.FilCod); break; case Brw_ADMI_SHARE_CTR: - Soc_StoreSocialNote (Soc_NOTE_CTR_SHA_PUB_FILE,FileMetadata.FilCod); + Soc_StoreAndPublishSocialNote (Soc_NOTE_CTR_SHA_PUB_FILE,FileMetadata.FilCod); break; case Brw_ADMI_DOCUM_DEG: - Soc_StoreSocialNote (Soc_NOTE_DEG_DOC_PUB_FILE,FileMetadata.FilCod); + Soc_StoreAndPublishSocialNote (Soc_NOTE_DEG_DOC_PUB_FILE,FileMetadata.FilCod); break; case Brw_ADMI_SHARE_DEG: - Soc_StoreSocialNote (Soc_NOTE_DEG_SHA_PUB_FILE,FileMetadata.FilCod); + Soc_StoreAndPublishSocialNote (Soc_NOTE_DEG_SHA_PUB_FILE,FileMetadata.FilCod); break; case Brw_ADMI_DOCUM_CRS: - Soc_StoreSocialNote (Soc_NOTE_CRS_DOC_PUB_FILE,FileMetadata.FilCod); + Soc_StoreAndPublishSocialNote (Soc_NOTE_CRS_DOC_PUB_FILE,FileMetadata.FilCod); break; case Brw_ADMI_SHARE_CRS: - Soc_StoreSocialNote (Soc_NOTE_CRS_SHA_PUB_FILE,FileMetadata.FilCod); + Soc_StoreAndPublishSocialNote (Soc_NOTE_CRS_SHA_PUB_FILE,FileMetadata.FilCod); break; default: break; diff --git a/swad_forum.c b/swad_forum.c index 82aea9d18..4afff2da2 100644 --- a/swad_forum.c +++ b/swad_forum.c @@ -3836,7 +3836,7 @@ void For_RecForumPst (void) { case For_FORUM_GLOBAL_USRS: case For_FORUM_SWAD_USRS: - Soc_StoreSocialNote (Soc_NOTE_FORUM_POST,PstCod); + Soc_StoreAndPublishSocialNote (Soc_NOTE_FORUM_POST,PstCod); break; default: break; diff --git a/swad_notice.c b/swad_notice.c index 086754a6e..dd1d2de84 100644 --- a/swad_notice.c +++ b/swad_notice.c @@ -155,7 +155,7 @@ void Not_ReceiveNotice (void) Ntf_ShowAlertNumUsrsToBeNotifiedByEMail (NumUsrsToBeNotifiedByEMail); /***** Create a new social note about the new notice *****/ - Soc_StoreSocialNote (Soc_NOTE_NOTICE,NotCod); + Soc_StoreAndPublishSocialNote (Soc_NOTE_NOTICE,NotCod); } /*****************************************************************************/ diff --git a/swad_social.c b/swad_social.c index 2797f2c53..4bda6a00b 100644 --- a/swad_social.c +++ b/swad_social.c @@ -92,10 +92,19 @@ static const Act_Action_t Soc_DefaultActions[Soc_NUM_SOCIAL_NOTES] = /****************************** Internal types *******************************/ /*****************************************************************************/ +struct SocialPublishing + { + long PubCod; + long AuthorCod; + long PublisherCod; + long NotCod; + time_t DateTimeUTC; + }; + struct SocialNote { long NotCod; - Soc_SocialNote_t SocialNote; + Soc_NoteType_t NoteType; long UsrCod; long CtyCod; long InsCod; @@ -121,24 +130,28 @@ extern struct Globals Gbl; /*****************************************************************************/ static unsigned long Soc_ShowTimeline (const char *Query,Act_Action_t UpdateAction); -static Soc_SocialNote_t Soc_GetSocialNoteFromDB (const char *Str); -static void Soc_WriteSocialNote (const struct SocialNote *Soc, - struct UsrData *UsrDat, - bool PutIconRemove); +static void Soc_GetDataOfSocialPublishingFromRow (MYSQL_ROW row,struct SocialPublishing *SocPub); +static void Soc_WriteSocialNote (const struct SocialPublishing *SocPub, + const struct SocialNote *SocNot, + struct UsrData *UsrDat, + bool PutIconRemove); static void Soc_WriteNoteDate (time_t TimeUTC); -static void Soc_StartFormGoToAction (Soc_SocialNote_t SocialNote, +static void Soc_StartFormGoToAction (Soc_NoteType_t NoteType, long CrsCod,long Cod); -static void Soc_GetNoteSummary (const struct SocialNote *Soc, +static void Soc_GetNoteSummary (const struct SocialNote *SocNot, char *SummaryStr,unsigned MaxChars); static void Soc_PutLinkToWriteANewPost (void); static void Soc_GetAndWriteSocialPost (long PstCod); -static void Soc_PutFormToRemoveSocialNote (long NotCod); -static void Soc_PutHiddenParamNotCod (long NotCod); -static long Soc_GetParamNotCod (void); -static void Soc_GetDataOfSocialNoteByCod (struct SocialNote *Soc); -static void Soc_GetDataOfSocialNoteFromRow (MYSQL_ROW row,struct SocialNote *Soc); +static void Soc_PutFormToRemoveSocialPublishing (long PubCod); +static void Soc_PutHiddenParamPubCod (long NotCod); +static long Soc_GetParamPubCod (void); +static unsigned long Soc_GetNumPubsOfANote (long NotCod); +static void Soc_GetDataOfSocialPublishingByCod (struct SocialPublishing *SocPub); +static void Soc_GetDataOfSocialNoteByCod (struct SocialNote *SocNot); +static void Soc_GetDataOfSocialNoteFromRow (MYSQL_ROW row,struct SocialNote *SocNot); +static Soc_NoteType_t Soc_GetSocialNoteFromDB (const char *Str); /*****************************************************************************/ /*********** Show social activity (timeline) of a selected user **************/ @@ -148,13 +161,12 @@ void Soc_ShowUsrTimeline (long UsrCod) { char Query[512]; - /***** Build query to show timeline including the users I am following *****/ - sprintf (Query,"SELECT NotCod,SocialNote,UsrCod," - "CtyCod,InsCod,CtrCod,DegCod,CrsCod," - "Cod,UNIX_TIMESTAMP(TimeNote)" - " FROM social_notes" - " WHERE UsrCod='%ld'" - " ORDER BY NotCod DESC LIMIT 10", + /***** Build query to show timeline with publishing of a unique user *****/ + // Publisher code is set to -1 here to get only one row + sprintf (Query,"SELECT DISTINCTROW PubCod,AuthorCod,'-1',NotCod,UNIX_TIMESTAMP(TimePublish)" + " FROM social_timeline" + " WHERE PublisherCod='%ld'" + " ORDER BY PubCod DESC LIMIT 10", UsrCod); /***** Show timeline *****/ @@ -178,15 +190,14 @@ void Soc_ShowFollowingTimeline (void) Lay_ShowAlert (Lay_INFO,"Usted no sigue a ningún usuario."); // Need translation!!! /***** Build query to show timeline including the users I am following *****/ - sprintf (Query,"SELECT NotCod,SocialNote,UsrCod," - "CtyCod,InsCod,CtrCod,DegCod,CrsCod," - "Cod,UNIX_TIMESTAMP(TimeNote)" - " FROM social_notes" - " WHERE UsrCod IN" + // Publisher code is set to -1 here to get only one row + sprintf (Query,"SELECT DISTINCTROW PubCod,AuthorCod,'-1',NotCod,UNIX_TIMESTAMP(TimePublish)" + " FROM social_timeline" + " WHERE PublisherCod IN" " (SELECT '%ld'" " UNION" " SELECT FollowedCod FROM usr_follow WHERE FollowerCod='%ld')" - " ORDER BY NotCod DESC LIMIT 10", + " ORDER BY PubCod DESC LIMIT 10", Gbl.Usrs.Me.UsrDat.UsrCod, Gbl.Usrs.Me.UsrDat.UsrCod); @@ -205,16 +216,17 @@ static unsigned long Soc_ShowTimeline (const char *Query,Act_Action_t UpdateActi extern const char *Txt_Public_activity; MYSQL_RES *mysql_res; MYSQL_ROW row; - unsigned long NumNotes; - unsigned long NumNote; - struct SocialNote Soc; + unsigned long NumPublishings; + unsigned long NumPub; + struct SocialPublishing SocPub; + struct SocialNote SocNot; struct UsrData UsrDat; /***** Get timeline from database *****/ - NumNotes = DB_QuerySELECT (Query,&mysql_res,"can not get social notes"); + NumPublishings = DB_QuerySELECT (Query,&mysql_res,"can not get social notes"); /***** List my timeline *****/ - if (NumNotes) // Notes found + if (NumPublishings) // Publishings found in timeline { /***** Initialize structure with user's data *****/ Usr_UsrDataConstructor (&UsrDat); @@ -229,17 +241,19 @@ static unsigned long Soc_ShowTimeline (const char *Query,Act_Action_t UpdateActi /***** Start list *****/ fprintf (Gbl.F.Out,""); Lay_EndRoundFrame (); @@ -835,38 +871,56 @@ void Soc_RequestRemovalSocialNote (void) } /*****************************************************************************/ -/*************************** Remove a social note ****************************/ +/************************ Remove a social publishing *************************/ /*****************************************************************************/ -void Soc_RemoveSocialNote (void) +void Soc_RemoveSocialPublishing (void) { extern const char *Txt_Comment_removed; - struct SocialNote Soc; + struct SocialPublishing SocPub; + struct SocialNote SocNot; + unsigned long NumPubs; bool ICanRemove; char Query[128]; - /***** Get the code of the social note to remove *****/ - Soc.NotCod = Soc_GetParamNotCod (); + /***** Get the code of the social publishing to remove *****/ + SocPub.PubCod = Soc_GetParamPubCod (); + + /***** Get data of social publishing *****/ + Soc_GetDataOfSocialPublishingByCod (&SocPub); /***** Get data of social note *****/ - Soc_GetDataOfSocialNoteByCod (&Soc); + SocNot.NotCod = SocPub.NotCod; + Soc_GetDataOfSocialNoteByCod (&SocNot); ICanRemove = (Gbl.Usrs.Me.Logged && - Soc.UsrCod == Gbl.Usrs.Me.UsrDat.UsrCod && - Soc.SocialNote == Soc_NOTE_SOCIAL_POST); + SocPub.PublisherCod == Gbl.Usrs.Me.UsrDat.UsrCod && + SocNot.NoteType == Soc_NOTE_SOCIAL_POST); if (ICanRemove) { - /***** Remove social note *****/ - sprintf (Query,"DELETE FROM social_notes WHERE NotCod='%ld'", - Soc.NotCod); - DB_QueryDELETE (Query,"can not remove a social note"); + /***** Remove social publishing *****/ + sprintf (Query,"DELETE FROM social_timeline WHERE PubCod='%ld'", + SocPub.PubCod); + DB_QueryDELETE (Query,"can not remove a social publishing"); - /***** Remove social post *****/ - if (Soc.SocialNote == Soc_NOTE_SOCIAL_POST) + /***** Count number of times this note + is published in timeline after removal *****/ + NumPubs = Soc_GetNumPubsOfANote (SocNot.NotCod); + + if (NumPubs == 0) // This was the last publishing of this note { - sprintf (Query,"DELETE FROM social_posts WHERE PstCod='%ld'", - Soc.Cod); - DB_QueryDELETE (Query,"can not remove a social post"); + /***** Remove social note *****/ + sprintf (Query,"DELETE FROM social_notes WHERE NotCod='%ld'", + SocNot.NotCod); + DB_QueryDELETE (Query,"can not remove a social note"); + + /***** Remove social post *****/ + if (SocNot.NoteType == Soc_NOTE_SOCIAL_POST) + { + sprintf (Query,"DELETE FROM social_posts WHERE PstCod='%ld'", + SocNot.Cod); + DB_QueryDELETE (Query,"can not remove a social post"); + } } /***** Message of success *****/ @@ -878,76 +932,135 @@ void Soc_RemoveSocialNote (void) } /*****************************************************************************/ -/******************* Get assignment data using its code **********************/ +/*********** Get number of publishings in timeline of a note code ************/ /*****************************************************************************/ -static void Soc_GetDataOfSocialNoteByCod (struct SocialNote *Soc) +static unsigned long Soc_GetNumPubsOfANote (long NotCod) + { + char Query[128]; + + sprintf (Query,"SELECT COUNT(*) FROM social_timeline WHERE NotCod='%ld'", + NotCod); + return DB_QueryCOUNT (Query,"can not get number of publishing of a note"); + } + +/*****************************************************************************/ +/********* Get data of social publishing in timeline using its code **********/ +/*****************************************************************************/ + +static void Soc_GetDataOfSocialPublishingByCod (struct SocialPublishing *SocPub) + { + char Query[256]; + MYSQL_RES *mysql_res; + MYSQL_ROW row; + + /***** Get data of social publishing from database *****/ + sprintf (Query,"SELECT PubCod,AuthorCod,PublisherCod,NotCod,UNIX_TIMESTAMP(TimePublish)" + " FROM social_timeline" + " WHERE PubCod='%ld'", + SocPub->PubCod); + if (DB_QuerySELECT (Query,&mysql_res,"can not get data of social note")) + { + /***** Get social note *****/ + row = mysql_fetch_row (mysql_res); + Soc_GetDataOfSocialPublishingFromRow (row,SocPub); + } + else + { + /***** Reset fields of social publishing *****/ + SocPub->AuthorCod = -1L; + SocPub->PublisherCod = -1L; + SocPub->NotCod = -1L; + SocPub->DateTimeUTC = (time_t) 0; + } + } + +/*****************************************************************************/ +/**************** Get data of social note using its code *********************/ +/*****************************************************************************/ + +static void Soc_GetDataOfSocialNoteByCod (struct SocialNote *SocNot) { char Query[256]; MYSQL_RES *mysql_res; MYSQL_ROW row; /***** Get data of social note from database *****/ - sprintf (Query,"SELECT NotCod,SocialNote,UsrCod," + sprintf (Query,"SELECT NotCod,NoteType,UsrCod," "CtyCod,InsCod,CtrCod,DegCod,CrsCod," "Cod,UNIX_TIMESTAMP(TimeNote)" " FROM social_notes" " WHERE NotCod='%ld'", - Soc->NotCod); + SocNot->NotCod); if (DB_QuerySELECT (Query,&mysql_res,"can not get data of social note")) { /***** Get social note *****/ row = mysql_fetch_row (mysql_res); - Soc_GetDataOfSocialNoteFromRow (row,Soc); + Soc_GetDataOfSocialNoteFromRow (row,SocNot); } else { /***** Reset fields of social note *****/ - Soc->SocialNote = Soc_NOTE_UNKNOWN; - Soc->UsrCod = -1L; - Soc->CtyCod = - Soc->InsCod = - Soc->CtrCod = - Soc->DegCod = - Soc->CrsCod = -1L; - Soc->Cod = -1L; - Soc->DateTimeUTC = (time_t) 0; + SocNot->NoteType = Soc_NOTE_UNKNOWN; + SocNot->UsrCod = -1L; + SocNot->CtyCod = -1L; + SocNot->InsCod = -1L; + SocNot->CtrCod = -1L; + SocNot->DegCod = -1L; + SocNot->CrsCod = -1L; + SocNot->Cod = -1L; + SocNot->DateTimeUTC = (time_t) 0; } } /*****************************************************************************/ -/******************* Get assignment data using its code **********************/ +/*************** Get data of social note using its code **********************/ /*****************************************************************************/ -static void Soc_GetDataOfSocialNoteFromRow (MYSQL_ROW row,struct SocialNote *Soc) +static void Soc_GetDataOfSocialNoteFromRow (MYSQL_ROW row,struct SocialNote *SocNot) { /* Get social code (row[0]) */ - Soc->NotCod = Str_ConvertStrCodToLongCod (row[0]); + SocNot->NotCod = Str_ConvertStrCodToLongCod (row[0]); /* Get note type (row[1]) */ - Soc->SocialNote = Soc_GetSocialNoteFromDB ((const char *) row[1]); + SocNot->NoteType = Soc_GetSocialNoteFromDB ((const char *) row[1]); /* Get (from) user code (row[2]) */ - Soc->UsrCod = Str_ConvertStrCodToLongCod (row[2]); + SocNot->UsrCod = Str_ConvertStrCodToLongCod (row[2]); /* Get country code (row[3]) */ - Soc->CtyCod = Str_ConvertStrCodToLongCod (row[3]); + SocNot->CtyCod = Str_ConvertStrCodToLongCod (row[3]); /* Get institution code (row[4]) */ - Soc->InsCod = Str_ConvertStrCodToLongCod (row[4]); + SocNot->InsCod = Str_ConvertStrCodToLongCod (row[4]); /* Get centre code (row[5]) */ - Soc->CtrCod = Str_ConvertStrCodToLongCod (row[5]); + SocNot->CtrCod = Str_ConvertStrCodToLongCod (row[5]); /* Get degree code (row[6]) */ - Soc->DegCod = Str_ConvertStrCodToLongCod (row[6]); + SocNot->DegCod = Str_ConvertStrCodToLongCod (row[6]); /* Get course code (row[7]) */ - Soc->CrsCod = Str_ConvertStrCodToLongCod (row[7]); + SocNot->CrsCod = Str_ConvertStrCodToLongCod (row[7]); /* Get file/post... code (row[8]) */ - Soc->Cod = Str_ConvertStrCodToLongCod (row[8]); + SocNot->Cod = Str_ConvertStrCodToLongCod (row[8]); /* Get time of the note (row[9]) */ - Soc->DateTimeUTC = Dat_GetUNIXTimeFromStr (row[9]); + SocNot->DateTimeUTC = Dat_GetUNIXTimeFromStr (row[9]); + } + +/*****************************************************************************/ +/****** Get social note type from string number coming from database *********/ +/*****************************************************************************/ + +static Soc_NoteType_t Soc_GetSocialNoteFromDB (const char *Str) + { + unsigned UnsignedNum; + + if (sscanf (Str,"%u",&UnsignedNum) == 1) + if (UnsignedNum < Soc_NUM_SOCIAL_NOTES) + return (Soc_NoteType_t) UnsignedNum; + + return Soc_NOTE_UNKNOWN; } diff --git a/swad_social.h b/swad_social.h index 2d100577a..7400d97dc 100644 --- a/swad_social.h +++ b/swad_social.h @@ -75,7 +75,7 @@ typedef enum /* Profile tab */ - } Soc_SocialNote_t; + } Soc_NoteType_t; /*****************************************************************************/ /****************************** Public prototypes ****************************/ @@ -84,12 +84,12 @@ typedef enum void Soc_ShowUsrTimeline (long UsrCod); void Soc_ShowFollowingTimeline (void); -void Soc_StoreSocialNote (Soc_SocialNote_t SocialNote,long Cod); +void Soc_StoreAndPublishSocialNote (Soc_NoteType_t NoteType,long Cod); void Soc_FormSocialPost (void); void Soc_ReceiveSocialPost (void); void Soc_RequestRemovalSocialNote (void); -void Soc_RemoveSocialNote (void); +void Soc_RemoveSocialPublishing (void); #endif