swad-core/swad_follow.c

474 lines
16 KiB
C
Raw Normal View History

2015-03-18 01:06:43 +01:00
// swad_follow.c: user's followers and followed
/*
SWAD (Shared Workspace At a Distance),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2015 Antonio Ca<EFBFBD>as Vargas
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General 3 License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/*********************************** Headers *********************************/
/*****************************************************************************/
#include <stdio.h> // For sprintf
#include "swad_bool.h"
#include "swad_database.h"
#include "swad_follow.h"
#include "swad_global.h"
2015-03-18 02:11:23 +01:00
#include "swad_profile.h"
2015-03-18 01:06:43 +01:00
#include "swad_user.h"
/*****************************************************************************/
/****************************** Public constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
2015-03-19 12:32:45 +01:00
#define Fol_NUM_COLUMNS_FOLLOW 3
2015-03-18 01:06:43 +01:00
/*****************************************************************************/
/****************************** Internal types *******************************/
/*****************************************************************************/
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/************************* Internal global variables *************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
2015-03-19 00:28:37 +01:00
static unsigned Fol_GetNumFollowing (long UsrCod);
static unsigned Fol_GetNumFollowers (long UsrCod);
2015-03-19 01:59:08 +01:00
static void Fol_ShowFollowedOrFollowed (const struct UsrData *UsrDat);
2015-03-19 00:28:37 +01:00
2015-03-18 01:06:43 +01:00
/*****************************************************************************/
/*************** Check if a user is a follower of another user ***************/
/*****************************************************************************/
bool Fol_CheckUsrIsFollowerOf (long FollowerCod,long FollowedCod)
{
char Query[256];
if (FollowerCod == FollowedCod)
return false;
/***** Check if a user is a follower of another user *****/
sprintf (Query,"SELECT COUNT(*) FROM usr_follow"
" WHERE FollowerCod='%ld' AND FollowedCod='%ld'",
FollowerCod,FollowedCod);
return (DB_QueryCOUNT (Query,"can not get if a user is a follower of another one") != 0);
}
2015-03-19 00:28:37 +01:00
/*****************************************************************************/
/**************** Show following and followers of a user *********************/
/*****************************************************************************/
2015-03-19 01:59:08 +01:00
void Fol_ShowFollowingAndFollowers (const struct UsrData *UsrDat)
2015-03-19 00:28:37 +01:00
{
extern const char *The_ClassFormul[The_NUM_THEMES];
extern const char *Txt_Following;
extern const char *Txt_Followers;
2015-03-19 01:59:08 +01:00
unsigned Following = Fol_GetNumFollowing (UsrDat->UsrCod);
unsigned Followers = Fol_GetNumFollowers (UsrDat->UsrCod);
2015-03-19 00:28:37 +01:00
/***** Start table *****/
fprintf (Gbl.F.Out,"<table class=\"CELLS_PAD_4\" style=\"margin:0 auto;\">"
"<tr>");
2015-03-19 12:32:45 +01:00
/***** Followed users *****/
2015-03-19 00:28:37 +01:00
fprintf (Gbl.F.Out,"<td style=\"min-width:100px;"
" text-align:center; vertical-align:top;\">"
2015-03-19 01:59:08 +01:00
"<div class=\"FOLLOW\">");
2015-03-19 12:32:45 +01:00
if (Following)
{
/* Form to list followed users */
Act_FormStart (ActSeeFlg);
Usr_PutParamOtherUsrCodEncrypted (UsrDat->EncryptedUsrCod);
Act_LinkFormSubmit (Txt_Following,"FOLLOW");
}
fprintf (Gbl.F.Out,"%u",Following);
if (Following)
{
fprintf (Gbl.F.Out,"</a>");
Act_FormEnd ();
}
2015-03-19 01:59:08 +01:00
fprintf (Gbl.F.Out,"</div>"
2015-03-19 00:28:37 +01:00
"<div class=\"%s\">"
2015-03-19 01:59:08 +01:00
"%s</div>"
2015-03-19 00:28:37 +01:00
"</td>",
The_ClassFormul[Gbl.Prefs.Theme],
2015-03-19 01:59:08 +01:00
Txt_Following);
2015-03-19 00:28:37 +01:00
/***** Followers *****/
fprintf (Gbl.F.Out,"<td style=\"min-width:100px;"
" text-align:center; vertical-align:top;\">"
2015-03-19 01:59:08 +01:00
"<div class=\"FOLLOW\">");
2015-03-19 12:32:45 +01:00
if (Followers)
{
/* Form to list followers */
Act_FormStart (ActSeeFlr);
Usr_PutParamOtherUsrCodEncrypted (UsrDat->EncryptedUsrCod);
Act_LinkFormSubmit (Txt_Followers,"FOLLOW");
}
fprintf (Gbl.F.Out,"%u",Followers);
if (Followers)
{
fprintf (Gbl.F.Out,"</a>");
Act_FormEnd ();
}
2015-03-19 01:59:08 +01:00
fprintf (Gbl.F.Out,"</div>"
2015-03-19 00:28:37 +01:00
"<div class=\"%s\">"
2015-03-19 01:59:08 +01:00
"%s</div>"
2015-03-19 00:28:37 +01:00
"</td>",
The_ClassFormul[Gbl.Prefs.Theme],
2015-03-19 01:59:08 +01:00
Txt_Followers);
2015-03-19 00:28:37 +01:00
/***** End table *****/
fprintf (Gbl.F.Out,"</tr>"
"</table>");
}
/*****************************************************************************/
/*************************** Get number of followed **************************/
/*****************************************************************************/
static unsigned Fol_GetNumFollowing (long UsrCod)
{
char Query[128];
/***** Check if a user is a follower of another user *****/
sprintf (Query,"SELECT COUNT(*) FROM usr_follow WHERE FollowerCod='%ld'",
UsrCod);
return DB_QueryCOUNT (Query,"can not get number of followed");
}
/*****************************************************************************/
/************************** Get number of followers **************************/
/*****************************************************************************/
static unsigned Fol_GetNumFollowers (long UsrCod)
{
char Query[128];
/***** Check if a user is a follower of another user *****/
sprintf (Query,"SELECT COUNT(*) FROM usr_follow WHERE FollowedCod='%ld'",
UsrCod);
return DB_QueryCOUNT (Query,"can not get number of followers");
}
2015-03-18 01:06:43 +01:00
2015-03-19 01:59:08 +01:00
/*****************************************************************************/
/***************************** List followed users ***************************/
/*****************************************************************************/
void Fol_ListFollowing (void)
{
extern const char *Txt_User_not_found_or_you_do_not_have_permission_;
extern const char *Txt_Following;
char Query[256];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumUsrs;
unsigned NumUsr;
struct UsrData UsrDat;
/***** Get user to view user he/she follows *****/
if (Usr_GetParamOtherUsrCodEncryptedAndGetUsrData ())
{
/***** Show user's profile *****/
if (Prf_ShowUserProfile ())
{
/***** Check if a user is a follower of another user *****/
sprintf (Query,"SELECT FollowedCod FROM usr_follow"
" WHERE FollowerCod='%ld' ORDER BY FollowTime DESC",
Gbl.Usrs.Other.UsrDat.UsrCod);
NumUsrs = (unsigned) DB_QuerySELECT (Query,&mysql_res,"can not get followed users");
if (NumUsrs)
{
/***** Initialize structure with user's data *****/
Usr_UsrDataConstructor (&UsrDat);
/***** Start listing *****/
Lay_StartRoundFrameTable10 (NULL,2,Txt_Following);
for (NumUsr = 0;
NumUsr < NumUsrs;
NumUsr++)
{
/***** Get user and number of clicks *****/
row = mysql_fetch_row (mysql_res);
/* Get user's code (row[0]) */
UsrDat.UsrCod = Str_ConvertStrCodToLongCod (row[0]);
Usr_GetAllUsrDataFromUsrCod (&UsrDat);
/***** Show user *****/
2015-03-19 12:32:45 +01:00
if ((NumUsr % Fol_NUM_COLUMNS_FOLLOW) == 0)
2015-03-19 01:59:08 +01:00
fprintf (Gbl.F.Out,"<tr>");
Fol_ShowFollowedOrFollowed (&UsrDat);
2015-03-19 12:32:45 +01:00
if ((NumUsr % Fol_NUM_COLUMNS_FOLLOW) == (Fol_NUM_COLUMNS_FOLLOW-1) ||
NumUsr == NumUsrs - 1)
2015-03-19 01:59:08 +01:00
fprintf (Gbl.F.Out,"</tr>");
}
/***** End listing *****/
Lay_EndRoundFrameTable10 ();
/***** Free memory used for user's data *****/
Usr_UsrDataDestructor (&UsrDat);
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
else
Lay_ShowAlert (Lay_WARNING,Txt_User_not_found_or_you_do_not_have_permission_);
}
else
Lay_ShowAlert (Lay_WARNING,Txt_User_not_found_or_you_do_not_have_permission_);
}
/*****************************************************************************/
/******************************* List followers ******************************/
/*****************************************************************************/
void Fol_ListFollowers (void)
{
extern const char *Txt_User_not_found_or_you_do_not_have_permission_;
extern const char *Txt_Followers;
char Query[256];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumUsrs;
unsigned NumUsr;
struct UsrData UsrDat;
/***** Get user to view user he/she follows *****/
if (Usr_GetParamOtherUsrCodEncryptedAndGetUsrData ())
{
/***** Show user's profile *****/
if (Prf_ShowUserProfile ())
{
/***** Check if a user is a follower of another user *****/
sprintf (Query,"SELECT FollowerCod FROM usr_follow"
" WHERE FollowedCod='%ld' ORDER BY FollowTime DESC",
Gbl.Usrs.Other.UsrDat.UsrCod);
NumUsrs = (unsigned) DB_QuerySELECT (Query,&mysql_res,"can not get followers");
if (NumUsrs)
{
/***** Initialize structure with user's data *****/
Usr_UsrDataConstructor (&UsrDat);
/***** Start listing *****/
Lay_StartRoundFrameTable10 (NULL,2,Txt_Followers);
for (NumUsr = 0;
NumUsr < NumUsrs;
NumUsr++)
{
/***** Get user and number of clicks *****/
row = mysql_fetch_row (mysql_res);
/* Get user's code (row[0]) */
UsrDat.UsrCod = Str_ConvertStrCodToLongCod (row[0]);
Usr_GetAllUsrDataFromUsrCod (&UsrDat);
/***** Show user *****/
2015-03-19 12:32:45 +01:00
if ((NumUsr % Fol_NUM_COLUMNS_FOLLOW) == 0)
2015-03-19 01:59:08 +01:00
fprintf (Gbl.F.Out,"<tr>");
Fol_ShowFollowedOrFollowed (&UsrDat);
2015-03-19 12:32:45 +01:00
if ((NumUsr % Fol_NUM_COLUMNS_FOLLOW) == (Fol_NUM_COLUMNS_FOLLOW-1) ||
NumUsr == NumUsrs - 1)
2015-03-19 01:59:08 +01:00
fprintf (Gbl.F.Out,"</tr>");
}
/***** End listing *****/
Lay_EndRoundFrameTable10 ();
/***** Free memory used for user's data *****/
Usr_UsrDataDestructor (&UsrDat);
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
else
Lay_ShowAlert (Lay_WARNING,Txt_User_not_found_or_you_do_not_have_permission_);
}
else
Lay_ShowAlert (Lay_WARNING,Txt_User_not_found_or_you_do_not_have_permission_);
}
/*****************************************************************************/
/************** Show user's photo and nickname in ranking list ***************/
/*****************************************************************************/
static void Fol_ShowFollowedOrFollowed (const struct UsrData *UsrDat)
{
extern const char *Txt_View_public_profile;
2015-03-19 12:32:45 +01:00
extern const char *Txt_Unfollow;
extern const char *Txt_Follow;
2015-03-19 01:59:08 +01:00
bool ShowPhoto;
char PhotoURL[PATH_MAX+1];
bool Visible = Pri_ShowIsAllowed (UsrDat->ProfileVisibility,UsrDat->UsrCod);
2015-03-19 12:32:45 +01:00
/***** Put form to follow / unfollow *****/
fprintf (Gbl.F.Out,"<td style=\"width:50px; height:50px;"
" text-align:right;\">");
if (Visible &&
Gbl.Usrs.Me.Logged &&
Gbl.Usrs.Me.UsrDat.UsrCod != UsrDat->UsrCod)
{
if (Fol_CheckUsrIsFollowerOf (Gbl.Usrs.Me.UsrDat.UsrCod,UsrDat->UsrCod))
{
Act_FormStart (ActUnfUsr);
Usr_PutParamOtherUsrCodEncrypted (UsrDat->EncryptedUsrCod);
Act_LinkFormSubmit (Txt_Unfollow,NULL);
fprintf (Gbl.F.Out,"<div class=\"ICON_HIGHLIGHT\">"
"<img src=\"%s/unfollow16x16.gif\""
" style=\"width:16px; height:16px; padding:0 2px;\" alt=\"%s\" />"
"</div>"
"</a>",
Gbl.Prefs.IconsURL,
Txt_Unfollow);
Act_FormEnd ();
}
else
{
Act_FormStart (ActFolUsr);
Usr_PutParamOtherUsrCodEncrypted (UsrDat->EncryptedUsrCod);
Act_LinkFormSubmit (Txt_Follow,NULL);
fprintf (Gbl.F.Out,"<div class=\"ICON_HIGHLIGHT\">"
"<img src=\"%s/follow16x16.gif\""
" style=\"width:16px; height:16px; padding:0 2px;\" alt=\"%s\" />"
"</div>"
"</a>",
Gbl.Prefs.IconsURL,
Txt_Follow);
Act_FormEnd ();
}
}
fprintf (Gbl.F.Out,"</td>");
2015-03-19 01:59:08 +01:00
/***** Check if I can see the public profile *****/
2015-03-19 12:32:45 +01:00
fprintf (Gbl.F.Out,"<td style=\"width:40px; height:50px;"
" text-align:center;\">");
2015-03-19 01:59:08 +01:00
if (Visible)
{
/***** User's photo *****/
ShowPhoto = Pho_ShowUsrPhotoIsAllowed (UsrDat,PhotoURL);
Pho_ShowUsrPhoto (UsrDat,ShowPhoto ? PhotoURL :
NULL,
2015-03-19 12:32:45 +01:00
"PHOTO36x48",Pho_ZOOM);
2015-03-19 01:59:08 +01:00
}
2015-03-19 12:32:45 +01:00
fprintf (Gbl.F.Out,"</td>");
2015-03-19 01:59:08 +01:00
/***** Put form to go to public profile *****/
2015-03-19 12:32:45 +01:00
fprintf (Gbl.F.Out,"<td style=\"min-width:150px; height:50px;"
" text-align:left;\">");
if (Visible &&
UsrDat->Nickname[0])
2015-03-19 01:59:08 +01:00
{
Act_FormStart (ActSeePubPrf);
Usr_PutParamOtherUsrCodEncrypted (UsrDat->EncryptedUsrCod);
2015-03-19 12:32:45 +01:00
Act_LinkFormSubmit (Txt_View_public_profile,"DAT");
Usr_RestrictLengthAndWriteName (UsrDat,20);
2015-03-19 01:59:08 +01:00
fprintf (Gbl.F.Out,"</a>");
Act_FormEnd ();
}
fprintf (Gbl.F.Out,"</td>");
}
2015-03-18 01:06:43 +01:00
/*****************************************************************************/
/***************************** Follow another user ***************************/
/*****************************************************************************/
void Fol_FollowUsr (void)
{
extern const char *Txt_User_not_found_or_you_do_not_have_permission_;
char Query[256];
2015-03-18 02:11:23 +01:00
bool Error;
2015-03-18 01:06:43 +01:00
2015-03-19 01:59:08 +01:00
/***** Get user to be followed *****/
2015-03-18 01:06:43 +01:00
if (Usr_GetParamOtherUsrCodEncryptedAndGetUsrData ())
{
if (!Fol_CheckUsrIsFollowerOf (Gbl.Usrs.Me.UsrDat.UsrCod,
Gbl.Usrs.Other.UsrDat.UsrCod))
{
/***** Follow user in database *****/
sprintf (Query,"REPLACE INTO usr_follow"
" (FollowerCod,FollowedCod,FollowTime)"
" VALUES ('%ld','%ld',NOW())",
Gbl.Usrs.Me.UsrDat.UsrCod,
Gbl.Usrs.Other.UsrDat.UsrCod);
DB_QueryREPLACE (Query,"can not follow user");
}
2015-03-18 02:11:23 +01:00
/***** Show user's profile again *****/
Error = !Prf_ShowUserProfile ();
2015-03-18 01:06:43 +01:00
}
else
2015-03-18 02:11:23 +01:00
Error = true;
if (Error)
2015-03-18 01:06:43 +01:00
Lay_ShowAlert (Lay_WARNING,Txt_User_not_found_or_you_do_not_have_permission_);
}
/*****************************************************************************/
/***************************** Unfollow another user *************************/
/*****************************************************************************/
void Fol_UnfollowUsr (void)
{
extern const char *Txt_User_not_found_or_you_do_not_have_permission_;
char Query[256];
2015-03-18 02:11:23 +01:00
bool Error;
2015-03-18 01:06:43 +01:00
2015-03-19 01:59:08 +01:00
/***** Get user to be unfollowed *****/
2015-03-18 01:06:43 +01:00
if (Usr_GetParamOtherUsrCodEncryptedAndGetUsrData ())
{
if (Fol_CheckUsrIsFollowerOf (Gbl.Usrs.Me.UsrDat.UsrCod,
Gbl.Usrs.Other.UsrDat.UsrCod))
{
/***** Follow user in database *****/
sprintf (Query,"DELETE FROM usr_follow"
" WHERE FollowerCod='%ld' AND FollowedCod='%ld'",
Gbl.Usrs.Me.UsrDat.UsrCod,
Gbl.Usrs.Other.UsrDat.UsrCod);
DB_QueryREPLACE (Query,"can not unfollow user");
}
2015-03-18 02:11:23 +01:00
/***** Show user's profile again *****/
Error = !Prf_ShowUserProfile ();
2015-03-18 01:06:43 +01:00
}
else
2015-03-18 02:11:23 +01:00
Error = true;
if (Error)
2015-03-18 01:06:43 +01:00
Lay_ShowAlert (Lay_WARNING,Txt_User_not_found_or_you_do_not_have_permission_);
}