diff --git a/swad_changelog.h b/swad_changelog.h index 6dfc72032..cdb92571a 100644 --- a/swad_changelog.h +++ b/swad_changelog.h @@ -553,7 +553,7 @@ enscript -2 --landscape --color --file-align=2 --highlight --line-numbers -o - * En OpenSWAD: ps2pdf source.ps destination.pdf */ -#define Log_PLATFORM_VERSION "SWAD 20.36.34 (2021-03-01)" +#define Log_PLATFORM_VERSION "SWAD 20.36.36 (2021-03-01)" #define CSS_FILE "swad20.33.9.css" #define JS_FILE "swad20.6.2.js" /* @@ -601,6 +601,8 @@ TODO: DNI de un estudiante sale err TODO: BUG: Cuando un tipo de grupo sólo tiene un grupo, inscribirse es voluntario, el estudiante sólo puede pertenecer a un grupo, y se inscribe en él, debería poder desapuntarse. Ahora no puede. TODO: Salvador Romero Cortés: @acanas opción para editar posts + Version 20.36.36: Mar 01, 2021 Query moved to module swad_timeline_database. (305429 lines) + Version 20.36.35: Mar 01, 2021 Code refactoring in timeline favourites. (305411 lines) Version 20.36.34: Mar 01, 2021 Function moved to module swad_timeline_database. (305446 lines) Version 20.36.33: Mar 01, 2021 Function moved to module swad_timeline_database. (305446 lines) Version 20.36.32: Mar 01, 2021 Query moved to module swad_timeline_database. (305444 lines) diff --git a/swad_timeline_comment.c b/swad_timeline_comment.c index e551c220e..ec66e94c2 100644 --- a/swad_timeline_comment.c +++ b/swad_timeline_comment.c @@ -1016,7 +1016,8 @@ static void TL_Com_GetDataOfCommentFromRow (MYSQL_ROW row, Med_GetMediaDataByCod (&Com->Content.Media); /***** Get number of times this comment has been favourited *****/ - Com->NumFavs = TL_DB_GetNumTimesACommHasBeenFav (Com); + Com->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_COMM, + Com->PubCod,Com->UsrCod); } /*****************************************************************************/ diff --git a/swad_timeline_database.c b/swad_timeline_database.c index 69a2a593c..273e2e0e5 100644 --- a/swad_timeline_database.c +++ b/swad_timeline_database.c @@ -40,6 +40,17 @@ /************************* Private constants and types ***********************/ /*****************************************************************************/ +static const char *TL_DB_Table[TL_Fav_NUM_WHAT_TO_FAV] = + { + [TL_Fav_NOTE] = "tl_notes_fav", + [TL_Fav_COMM] = "tl_comments_fav", + }; +static const char *TL_DB_Field[TL_Fav_NUM_WHAT_TO_FAV] = + { + [TL_Fav_NOTE] = "NotCod", + [TL_Fav_COMM] = "PubCod", + }; + /*****************************************************************************/ /************** External global variables from others modules ****************/ /*****************************************************************************/ @@ -54,17 +65,8 @@ extern struct Globals Gbl; /***************************** Private prototypes ****************************/ /*****************************************************************************/ -/****************************** Publications *********************************/ static long TL_DB_GetMedCodFromPub (long PubCod,const char *DBTable); -/******************************* Favourites **********************************/ -static bool TL_DB_CheckIfFavedByUsr (const char *Table,const char *Field, - long Cod,long UsrCod); -static unsigned TL_DB_GetNumTimesHasBeenFav (const char *Table,const char *Field, - long Cod,long UsrCod); -static void TL_DB_MarkAsFav (const char *Table,const char *Field,long Cod); -static void TL_DB_UnmarkAsFav (const char *Table,const char *Field,long Cod); - /*****************************************************************************/ /********************* Get data of note using its code ***********************/ /*****************************************************************************/ @@ -861,45 +863,23 @@ void TL_DB_UpdateFirstLastPubCodsInSession (long FirstPubCod) /****************** Check if a user has favourited a note ********************/ /*****************************************************************************/ -bool TL_DB_CheckIfNoteIsFavedByUsr (long NotCod,long UsrCod) - { - return TL_DB_CheckIfFavedByUsr ("tl_notes_fav","NotCod",NotCod,UsrCod); - } - -bool TL_DB_CheckIfCommIsFavedByUsr (long PubCod,long UsrCod) - { - return TL_DB_CheckIfFavedByUsr ("tl_comments_fav","PubCod",PubCod,UsrCod); - } - -static bool TL_DB_CheckIfFavedByUsr (const char *Table,const char *Field, - long Cod,long UsrCod) +bool TL_DB_CheckIfFavedByUsr (TL_Fav_WhatToFav_t WhatToFav, + long Cod,long UsrCod) { /***** Check if a user has favourited a note/comment from database *****/ return (DB_QueryCOUNT ("can not check if a user has favourited", "SELECT COUNT(*) FROM %s" " WHERE PubCod=%ld AND UsrCod=%ld", - Table, - Field,Cod,UsrCod) != 0); + TL_DB_Table[WhatToFav], + TL_DB_Field[WhatToFav],Cod,UsrCod) != 0); } /*****************************************************************************/ /********* Get number of times a note/comment has been favourited ************/ /*****************************************************************************/ -unsigned TL_DB_GetNumTimesANoteHasBeenFav (const struct TL_Not_Note *Not) - { - return TL_DB_GetNumTimesHasBeenFav ("tl_notes_fav","NotCod", - Not->NotCod,Not->UsrCod); - } - -unsigned TL_DB_GetNumTimesACommHasBeenFav (const struct TL_Com_Comment *Com) - { - return TL_DB_GetNumTimesHasBeenFav ("tl_comments_fav","PubCod", - Com->PubCod,Com->UsrCod); - } - -static unsigned TL_DB_GetNumTimesHasBeenFav (const char *Table,const char *Field, - long Cod,long UsrCod) +unsigned TL_DB_GetNumTimesHasBeenFav (TL_Fav_WhatToFav_t WhatToFav, + long Cod,long UsrCod) { /***** Get number of times (users) a note/comment has been favourited *****/ return (unsigned) @@ -907,8 +887,8 @@ static unsigned TL_DB_GetNumTimesHasBeenFav (const char *Table,const char *Field "SELECT COUNT(*) FROM %s" " WHERE %s=%ld" " AND UsrCod<>%ld", // Extra check - Table, - Field,Cod, + TL_DB_Table[WhatToFav], + TL_DB_Field[WhatToFav],Cod, UsrCod); // The author } @@ -921,17 +901,6 @@ unsigned TL_DB_GetListUsrsHaveFaved (TL_Fav_WhatToFav_t WhatToFav, unsigned MaxUsrs, MYSQL_RES **mysql_res) { - const char *Table[TL_Fav_NUM_WHAT_TO_FAV] = - { - [TL_Fav_NOTE] = "tl_notes_fav", - [TL_Fav_COMM] = "tl_comments_fav", - }; - const char *Field[TL_Fav_NUM_WHAT_TO_FAV] = - { - [TL_Fav_NOTE] = "NotCod", - [TL_Fav_COMM] = "PubCod", - }; - /***** Get list of users who have marked a note/comment as favourite from database *****/ return (unsigned) DB_QuerySELECT (mysql_res,"can not get favers", @@ -940,8 +909,8 @@ unsigned TL_DB_GetListUsrsHaveFaved (TL_Fav_WhatToFav_t WhatToFav, " WHERE %s=%ld" " AND UsrCod<>%ld" // Extra check " ORDER BY FavCod LIMIT %u", - Table[WhatToFav], - Field[WhatToFav],Cod, + TL_DB_Table[WhatToFav], + TL_DB_Field[WhatToFav],Cod, UsrCod, MaxUsrs); } @@ -950,17 +919,7 @@ unsigned TL_DB_GetListUsrsHaveFaved (TL_Fav_WhatToFav_t WhatToFav, /**************** Mark note/comment as favourite in database *****************/ /*****************************************************************************/ -void TL_DB_MarkNoteAsFav (long NotCod) - { - TL_DB_MarkAsFav ("tl_notes_fav","NotCod",NotCod); - } - -void TL_DB_MarkCommAsFav (long PubCod) - { - TL_DB_MarkAsFav ("tl_comments_fav","PubCod",PubCod); - } - -static void TL_DB_MarkAsFav (const char *Table,const char *Field,long Cod) +void TL_DB_MarkAsFav (TL_Fav_WhatToFav_t WhatToFav,long Cod) { /***** Insert in favourited in database *****/ DB_QueryINSERT ("can not favourite comment", @@ -968,8 +927,8 @@ static void TL_DB_MarkAsFav (const char *Table,const char *Field,long Cod) " (%s,UsrCod,TimeFav)" " VALUES" " (%ld,%ld,NOW())", - Table, - Field, + TL_DB_Table[WhatToFav], + TL_DB_Field[WhatToFav], Cod,Gbl.Usrs.Me.UsrDat.UsrCod); } @@ -977,24 +936,14 @@ static void TL_DB_MarkAsFav (const char *Table,const char *Field,long Cod) /*************** Unmark note/comment as favourite in database ****************/ /*****************************************************************************/ -void TL_DB_UnmarkNoteAsFav (long NotCod) - { - TL_DB_UnmarkAsFav ("tl_notes_fav","NotCod",NotCod); - } - -void TL_DB_UnmarkCommAsFav (long PubCod) - { - TL_DB_UnmarkAsFav ("tl_comments_fav","PubCod",PubCod); - } - -static void TL_DB_UnmarkAsFav (const char *Table,const char *Field,long Cod) +void TL_DB_UnmarkAsFav (TL_Fav_WhatToFav_t WhatToFav,long Cod) { /***** Delete the mark as favourite from database *****/ DB_QueryDELETE ("can not unfavourite", "DELETE FROM %s" " WHERE %s=%ld AND UsrCod=%ld", - Table, - Field,Cod,Gbl.Usrs.Me.UsrDat.UsrCod); + TL_DB_Table[WhatToFav], + TL_DB_Field[WhatToFav],Cod,Gbl.Usrs.Me.UsrDat.UsrCod); } /*****************************************************************************/ @@ -1031,6 +980,29 @@ unsigned TL_DB_GetNumTimesANoteHasBeenShared (struct TL_Not_Note *Not) (unsigned) TL_Pub_SHARED_NOTE); } +/*****************************************************************************/ +/***************** Get list of users who have shared a note ******************/ +/*****************************************************************************/ + +unsigned TL_DB_GetListUsrsHaveShared (long NotCod,long UsrCod, + unsigned MaxUsrs, + MYSQL_RES **mysql_res) + { + /***** Get list of users who have shared a note from database *****/ + return (unsigned) + DB_QuerySELECT (mysql_res,"can not get users", + "SELECT PublisherCod" // row[0] + " FROM tl_pubs" + " WHERE NotCod=%ld" + " AND PublisherCod<>%ld" + " AND PubType=%u" + " ORDER BY PubCod LIMIT %u", + NotCod, + UsrCod, + (unsigned) TL_Pub_SHARED_NOTE, + MaxUsrs); + } + /*****************************************************************************/ /****************** Remove shared publication from database ******************/ /*****************************************************************************/ diff --git a/swad_timeline_database.h b/swad_timeline_database.h index 15c2c7b29..edd237823 100644 --- a/swad_timeline_database.h +++ b/swad_timeline_database.h @@ -113,22 +113,23 @@ void TL_DB_UpdateLastPubCodInSession (void); void TL_DB_UpdateFirstLastPubCodsInSession (long FirstPubCod); /****************************** Favourites ***********************************/ -bool TL_DB_CheckIfNoteIsFavedByUsr (long NotCod,long UsrCod); -bool TL_DB_CheckIfCommIsFavedByUsr (long PubCod,long UsrCod); -unsigned TL_DB_GetNumTimesANoteHasBeenFav (const struct TL_Not_Note *Not); -unsigned TL_DB_GetNumTimesACommHasBeenFav (const struct TL_Com_Comment *Com); +bool TL_DB_CheckIfFavedByUsr (TL_Fav_WhatToFav_t WhatToFav, + long Cod,long UsrCod); +unsigned TL_DB_GetNumTimesHasBeenFav (TL_Fav_WhatToFav_t WhatToFav, + long Cod,long UsrCod); unsigned TL_DB_GetListUsrsHaveFaved (TL_Fav_WhatToFav_t WhatToFav, long Cod,long UsrCod, unsigned MaxUsrs, MYSQL_RES **mysql_res); -void TL_DB_MarkNoteAsFav (long NotCod); -void TL_DB_MarkCommAsFav (long PubCod); -void TL_DB_UnmarkNoteAsFav (long NotCod); -void TL_DB_UnmarkCommAsFav (long PubCod); +void TL_DB_MarkAsFav (TL_Fav_WhatToFav_t WhatToFav,long Cod); +void TL_DB_UnmarkAsFav (TL_Fav_WhatToFav_t WhatToFav,long Cod); /******************************** Shared *************************************/ bool TL_DB_CheckIfNoteIsSharedByUsr (long NotCod,long UsrCod); unsigned TL_DB_GetNumTimesANoteHasBeenShared (struct TL_Not_Note *Not); +unsigned TL_DB_GetListUsrsHaveShared (long NotCod,long UsrCod, + unsigned MaxUsrs, + MYSQL_RES **mysql_res); void TL_DB_RemoveSharedPub (long NotCod); #endif diff --git a/swad_timeline_favourite.c b/swad_timeline_favourite.c index abfed30f4..d0cad7790 100644 --- a/swad_timeline_favourite.c +++ b/swad_timeline_favourite.c @@ -170,14 +170,15 @@ static void TL_Fav_FavNote (struct TL_Not_Note *Not) { if (Gbl.Usrs.Me.Logged && // I am logged... !Usr_ItsMe (Not->UsrCod)) // ...but I am not the author - if (!TL_DB_CheckIfNoteIsFavedByUsr (Not->NotCod, - Gbl.Usrs.Me.UsrDat.UsrCod)) // I have not yet favourited the note + if (!TL_DB_CheckIfFavedByUsr (TL_Fav_NOTE,Not->NotCod, + Gbl.Usrs.Me.UsrDat.UsrCod)) // I have not yet favourited the note { /***** Mark note as favourite in database *****/ - TL_DB_MarkNoteAsFav (Not->NotCod); + TL_DB_MarkAsFav (TL_Fav_NOTE,Not->NotCod); /***** Update number of times this note is favourited *****/ - TL_DB_GetNumTimesANoteHasBeenFav (Not); + Not->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_NOTE, + Not->NotCod,Not->UsrCod); /***** Create notification about favourite post for the author of the post *****/ @@ -201,14 +202,15 @@ static void TL_Fav_UnfNote (struct TL_Not_Note *Not) if (Not->NumFavs && Gbl.Usrs.Me.Logged && // I am logged... !Usr_ItsMe (Not->UsrCod)) // ...but I am not the author - if (TL_DB_CheckIfNoteIsFavedByUsr (Not->NotCod, - Gbl.Usrs.Me.UsrDat.UsrCod)) // I have favourited the note + if (TL_DB_CheckIfFavedByUsr (TL_Fav_NOTE,Not->NotCod, + Gbl.Usrs.Me.UsrDat.UsrCod)) // I have favourited the note { /***** Delete the mark as favourite from database *****/ - TL_DB_UnmarkNoteAsFav (Not->NotCod); + TL_DB_UnmarkAsFav (TL_Fav_NOTE,Not->NotCod); /***** Update number of times this note is favourited *****/ - TL_DB_GetNumTimesANoteHasBeenFav (Not); + Not->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_NOTE, + Not->NotCod,Not->UsrCod); /***** Mark possible notifications on this note as removed *****/ OriginalPubCod = TL_DB_GetPubCodOfOriginalNote (Not->NotCod); @@ -313,17 +315,18 @@ static void TL_Fav_FavComment (struct TL_Com_Comment *Com) if (Com->PubCod > 0) if (!Usr_ItsMe (Com->UsrCod)) // I am not the author - if (!TL_DB_CheckIfCommIsFavedByUsr (Com->PubCod, - Gbl.Usrs.Me.UsrDat.UsrCod)) // I have not yet favourited the comment + if (!TL_DB_CheckIfFavedByUsr (TL_Fav_COMM,Com->PubCod, + Gbl.Usrs.Me.UsrDat.UsrCod)) // I have not yet favourited the comment { /***** Mark comment as favourite in database *****/ - TL_DB_MarkCommAsFav (Com->PubCod); + TL_DB_MarkAsFav (TL_Fav_COMM,Com->PubCod); - /* Update number of times this comment is favourited */ - TL_DB_GetNumTimesACommHasBeenFav (Com); + /***** Update number of times this comment is favourited *****/ + Com->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_COMM, + Com->PubCod,Com->UsrCod); - /**** Create notification about favourite post - for the author of the post ***/ + /***** Create notification about favourite post + for the author of the post *****/ TL_Ntf_CreateNotifToAuthor (Com->UsrCod,Com->PubCod, Ntf_EVENT_TIMELINE_FAV); } @@ -344,14 +347,15 @@ static void TL_Fav_UnfComment (struct TL_Com_Comment *Com) if (Com->PubCod > 0) if (Com->NumFavs && !Usr_ItsMe (Com->UsrCod)) // I am not the author - if (TL_DB_CheckIfCommIsFavedByUsr (Com->PubCod, - Gbl.Usrs.Me.UsrDat.UsrCod)) // I have favourited the comment + if (TL_DB_CheckIfFavedByUsr (TL_Fav_COMM,Com->PubCod, + Gbl.Usrs.Me.UsrDat.UsrCod)) // I have favourited the comment { /***** Delete the mark as favourite from database *****/ - TL_DB_UnmarkCommAsFav (Com->PubCod); + TL_DB_UnmarkAsFav (TL_Fav_COMM,Com->PubCod); /***** Update number of times this comment is favourited *****/ - TL_DB_GetNumTimesACommHasBeenFav (Com); + Com->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_COMM, + Com->PubCod,Com->UsrCod); /***** Mark possible notifications on this comment as removed *****/ Ntf_MarkNotifAsRemoved (Ntf_EVENT_TIMELINE_FAV,Com->PubCod); @@ -413,7 +417,8 @@ static void TL_Fav_PutFormToFavUnfNote (long NotCod) }; /***** Form and icon to fav/unfav note *****/ - TL_Frm_FormFavSha (&Form[TL_DB_CheckIfNoteIsFavedByUsr (NotCod,Gbl.Usrs.Me.UsrDat.UsrCod)]); + TL_Frm_FormFavSha (&Form[TL_DB_CheckIfFavedByUsr (TL_Fav_NOTE,NotCod, + Gbl.Usrs.Me.UsrDat.UsrCod)]); } /*****************************************************************************/ @@ -445,7 +450,8 @@ static void TL_Fav_PutFormToFavUnfComm (long PubCod) }; /***** Form and icon to fav/unfav *****/ - TL_Frm_FormFavSha (&Form[TL_DB_CheckIfNoteIsFavedByUsr (PubCod,Gbl.Usrs.Me.UsrDat.UsrCod)]); + TL_Frm_FormFavSha (&Form[TL_DB_CheckIfFavedByUsr (TL_Fav_COMM,PubCod, + Gbl.Usrs.Me.UsrDat.UsrCod)]); } /*****************************************************************************/ diff --git a/swad_timeline_note.c b/swad_timeline_note.c index e6b411cb6..62af207ce 100644 --- a/swad_timeline_note.c +++ b/swad_timeline_note.c @@ -89,8 +89,8 @@ static void TL_Not_PutFormGoToAction (const struct TL_Not_Note *Not, const struct For_Forums *Forums); static void TL_Not_WriteButtonsAndComments (const struct TL_Timeline *Timeline, - const struct TL_Not_Note *Not, - const struct UsrData *UsrDat); + const struct TL_Not_Note *Not, + const struct UsrData *UsrDat); static void TL_Not_WriteButtonToAddAComment (const struct TL_Not_Note *Not, const char IdNewComment[Frm_MAX_BYTES_ID + 1]); static void TL_Not_WriteFavShaRemAndComments (const struct TL_Timeline *Timeline, @@ -724,8 +724,8 @@ void TL_Not_GetNoteSummary (const struct TL_Not_Note *Not, /*****************************************************************************/ static void TL_Not_WriteButtonsAndComments (const struct TL_Timeline *Timeline, - const struct TL_Not_Note *Not, - const struct UsrData *UsrDat) // Author + const struct TL_Not_Note *Not, + const struct UsrData *UsrDat) // Author { char IdNewComment[Frm_MAX_BYTES_ID + 1]; @@ -1294,7 +1294,8 @@ static void TL_Not_GetDataOfNoteFromRow (MYSQL_ROW row,struct TL_Not_Note *Not) Not->NumShared = TL_DB_GetNumTimesANoteHasBeenShared (Not); /***** Get number of times this note has been favourited *****/ - Not->NumFavs = TL_DB_GetNumTimesANoteHasBeenFav (Not); + Not->NumFavs = TL_DB_GetNumTimesHasBeenFav (TL_Fav_NOTE, + Not->NotCod,Not->UsrCod); } /*****************************************************************************/ diff --git a/swad_timeline_share.c b/swad_timeline_share.c index 09055a058..40b13df51 100644 --- a/swad_timeline_share.c +++ b/swad_timeline_share.c @@ -134,11 +134,11 @@ static void TL_Sha_ShaNote (struct TL_Not_Note *Not) Pub.PubType = TL_Pub_SHARED_NOTE; TL_Pub_PublishPubInTimeline (&Pub); // Set Pub.PubCod - /* Update number of times this note is shared */ + /***** Update number of times this note is shared *****/ Not->NumShared = TL_DB_GetNumTimesANoteHasBeenShared (Not); - /**** Create notification about shared post - for the author of the post ***/ + /***** Create notification about shared post + for the author of the post *****/ OriginalPubCod = TL_DB_GetPubCodOfOriginalNote (Not->NotCod); if (OriginalPubCod > 0) TL_Ntf_CreateNotifToAuthor (Not->UsrCod,OriginalPubCod, @@ -284,17 +284,10 @@ static void TL_Sha_ShowUsrsWhoHaveSharedNote (const struct TL_Not_Note *Not, /***** Get users who have shared this note *****/ if (Not->NumShared) NumFirstUsrs = - (unsigned) DB_QuerySELECT (&mysql_res,"can not get users", - "SELECT PublisherCod FROM tl_pubs" - " WHERE NotCod=%ld" - " AND PublisherCod<>%ld" - " AND PubType=%u" - " ORDER BY PubCod LIMIT %u", - Not->NotCod, - Not->UsrCod, - (unsigned) TL_Pub_SHARED_NOTE, - HowManyUsrs == TL_Usr_SHOW_FEW_USRS ? TL_Usr_DEF_USRS_SHOWN : - TL_Usr_MAX_USRS_SHOWN); + TL_DB_GetListUsrsHaveShared (Not->NotCod,Not->UsrCod, + HowManyUsrs == TL_Usr_SHOW_FEW_USRS ? TL_Usr_DEF_USRS_SHOWN : + TL_Usr_MAX_USRS_SHOWN, + &mysql_res); else NumFirstUsrs = 0;