swad-core/swad_privacy.c

388 lines
14 KiB
C
Raw Normal View History

2015-10-16 02:24:29 +02:00
// swad_privacy.c: users' photo and public profile visibility
/*
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-2023 Antonio Ca<EFBFBD>as Vargas
2015-10-16 02:24:29 +02:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public 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 ***********************************/
/*****************************************************************************/
#define _GNU_SOURCE // For asprintf
#include <stdio.h> // For asprintf
#include <stdlib.h> // For free
2015-10-16 02:24:29 +02:00
#include <string.h>
#include "swad_action.h"
#include "swad_action_list.h"
#include "swad_alert.h"
2017-06-10 21:38:10 +02:00
#include "swad_box.h"
#include "swad_enrolment_database.h"
#include "swad_error.h"
2020-04-14 17:15:17 +02:00
#include "swad_figure.h"
2018-11-09 20:47:39 +01:00
#include "swad_form.h"
2015-10-16 02:24:29 +02:00
#include "swad_global.h"
2019-10-23 19:05:05 +02:00
#include "swad_HTML.h"
2015-10-16 02:24:29 +02:00
#include "swad_parameter.h"
#include "swad_privacy.h"
#include "swad_theme.h"
#include "swad_user_database.h"
2015-10-16 02:24:29 +02:00
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/****************************** Public constants *****************************/
/*****************************************************************************/
/***** Visibility (who can see user's photo or public profile) *****/
const char *Pri_VisibilityDB[Pri_NUM_OPTIONS_PRIVACY] =
{
2019-11-22 01:04:03 +01:00
[Pri_VISIBILITY_UNKNOWN] = "unknown",
[Pri_VISIBILITY_USER ] = "user",
[Pri_VISIBILITY_COURSE ] = "course",
[Pri_VISIBILITY_SYSTEM ] = "system",
[Pri_VISIBILITY_WORLD ] = "world",
2015-10-16 02:24:29 +02:00
};
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
2018-10-09 00:42:05 +02:00
#define Pri_PRIVACY_ID "privacy"
2015-10-16 02:24:29 +02:00
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
2020-04-08 03:41:05 +02:00
static void Pri_PutIconsPrivacy (__attribute__((unused)) void *Args);
2016-11-07 13:56:39 +01:00
2015-12-24 19:30:45 +01:00
static void Pri_PutFormVisibility (const char *TxtLabel,
Act_Action_t Action,const char *ParName,
2016-01-04 16:30:50 +01:00
Pri_Visibility_t CurrentVisibilityInDB,
unsigned MaskAllowedVisibility);
2015-10-16 02:24:29 +02:00
static void Pri_GetAndShowNumUsrsPerPrivacyForAnObject (const char *TxtObject,
const char *FldName,
unsigned MaskAllowedVisibility);
2015-10-16 02:24:29 +02:00
/*****************************************************************************/
/******************************* Edit my privacy *****************************/
/*****************************************************************************/
void Pri_EditMyPrivacy (void)
{
2019-03-26 11:53:21 +01:00
extern const char *Hlp_PROFILE_Settings_privacy;
extern const char *Txt_Please_check_your_privacy_settings;
2015-10-16 02:24:29 +02:00
extern const char *Txt_Privacy;
extern const char *Txt_Photo;
2019-03-22 15:21:46 +01:00
extern const char *Txt_Basic_public_profile;
extern const char *Txt_Extended_public_profile;
2017-02-27 19:52:04 +01:00
extern const char *Txt_Timeline;
2015-10-16 02:24:29 +02:00
/***** Begin section with settings on privacy *****/
2019-10-26 01:56:36 +02:00
HTM_SECTION_Begin (Pri_PRIVACY_ID);
2018-10-09 00:42:05 +02:00
/***** If any of my settings about privacy is unknown *****/
if (Gbl.Usrs.Me.UsrDat.PhotoVisibility == Pri_VISIBILITY_UNKNOWN ||
Gbl.Usrs.Me.UsrDat.BaPrfVisibility == Pri_VISIBILITY_UNKNOWN ||
Gbl.Usrs.Me.UsrDat.ExPrfVisibility == Pri_VISIBILITY_UNKNOWN)
Ale_ShowAlert (Ale_WARNING,Txt_Please_check_your_privacy_settings);
/***** Begin box and table *****/
Box_BoxTableBegin (NULL,Txt_Privacy,
Pri_PutIconsPrivacy,NULL,
Hlp_PROFILE_Settings_privacy,Box_NOT_CLOSABLE,2);
/***** Edit photo visibility *****/
Pri_PutFormVisibility (Txt_Photo,
ActChgPriPho,"VisPho",
Gbl.Usrs.Me.UsrDat.PhotoVisibility,
Pri_PHOTO_ALLOWED_VIS);
/***** Edit basic public profile visibility *****/
Pri_PutFormVisibility (Txt_Basic_public_profile,
ActChgBasPriPrf,"VisBasPrf",
Gbl.Usrs.Me.UsrDat.BaPrfVisibility,
Pri_BASIC_PROFILE_ALLOWED_VIS);
/***** Edit extended public profile visibility *****/
Pri_PutFormVisibility (Txt_Extended_public_profile,
ActChgExtPriPrf,"VisExtPrf",
Gbl.Usrs.Me.UsrDat.ExPrfVisibility,
Pri_EXTENDED_PROFILE_ALLOWED_VIS);
/***** Edit public activity (timeline) visibility *****/
Pri_PutFormVisibility (Txt_Timeline,
ActUnk,"VisTml",
Pri_VISIBILITY_SYSTEM,
Pri_TIMELINE_ALLOWED_VIS);
/***** End table and box *****/
Box_BoxTableEnd ();
2018-10-09 00:42:05 +02:00
2019-03-26 11:53:21 +01:00
/***** End section with settings on privacy *****/
2019-10-26 01:56:36 +02:00
HTM_SECTION_End ();
2016-11-07 13:56:39 +01:00
}
/*****************************************************************************/
2019-03-26 11:53:21 +01:00
/****************** Put contextual icons in privacy setting ******************/
2016-11-07 13:56:39 +01:00
/*****************************************************************************/
2020-04-08 03:41:05 +02:00
static void Pri_PutIconsPrivacy (__attribute__((unused)) void *Args)
2016-11-07 13:56:39 +01:00
{
2020-04-08 03:41:05 +02:00
/***** Put icon to show a figure *****/
Fig_PutIconToShowFigure (Fig_PRIVACY);
2015-10-16 02:24:29 +02:00
}
/*****************************************************************************/
/************************** Select photo visibility **************************/
/*****************************************************************************/
2015-12-24 19:30:45 +01:00
static void Pri_PutFormVisibility (const char *TxtLabel,
Act_Action_t Action,const char *ParName,
2016-01-04 16:30:50 +01:00
Pri_Visibility_t CurrentVisibilityInDB,
unsigned MaskAllowedVisibility)
2015-10-16 02:24:29 +02:00
{
extern const char *Txt_PRIVACY_OPTIONS[Pri_NUM_OPTIONS_PRIVACY];
Pri_Visibility_t Visibility;
2019-10-23 19:05:05 +02:00
HTM_TR_Begin (NULL);
2019-10-07 17:36:41 +02:00
/***** Select visibility *****/
HTM_TD_Begin ("class=\"RT FORM_IN_%s\"",The_GetSuffix ());
HTM_TxtColon (TxtLabel);
HTM_TD_End ();
/***** Form with list of options *****/
HTM_TD_Begin ("class=\"LT\"");
if (Action != ActUnk)
Frm_BeginFormAnchor (Action,Pri_PRIVACY_ID);
HTM_UL_Begin ("class=\"PRI_LIST LIST_LEFT\"");
for (Visibility = Pri_VISIBILITY_USER;
Visibility <= Pri_VISIBILITY_WORLD;
Visibility++)
if (MaskAllowedVisibility & (1 << Visibility))
{
if (Visibility == CurrentVisibilityInDB)
HTM_LI_Begin ("class=\"DAT_STRONG_%s BG_HIGHLIGHT\"",
The_GetSuffix ());
else
HTM_LI_Begin ("class=\"DAT_%s\"",
The_GetSuffix ());
HTM_LABEL_Begin (NULL);
HTM_INPUT_RADIO (ParName,Action == ActUnk ? HTM_DONT_SUBMIT_ON_CLICK :
HTM_SUBMIT_ON_CLICK,
"value=\"%u\"%s%s",
(unsigned) Visibility,
Visibility == CurrentVisibilityInDB ? " checked=\"checked\"" :
"",
Action == ActUnk ? " disabled=\"disabled\"" :
"");
HTM_Txt (Txt_PRIVACY_OPTIONS[Visibility]);
HTM_LABEL_End ();
HTM_LI_End ();
}
/***** End list and form *****/
HTM_UL_End ();
if (Action != ActUnk)
Frm_EndForm ();
HTM_TD_End ();
2019-10-23 19:05:05 +02:00
HTM_TR_End ();
2015-10-16 02:24:29 +02:00
}
/*****************************************************************************/
/************************ Get visibility from string *************************/
/*****************************************************************************/
2016-01-18 10:21:18 +01:00
Pri_Visibility_t Pri_GetVisibilityFromStr (const char *Str)
2015-10-16 02:24:29 +02:00
{
Pri_Visibility_t Visibility;
2019-12-15 20:02:34 +01:00
for (Visibility = (Pri_Visibility_t) 0;
Visibility <= (Pri_Visibility_t) (Pri_NUM_OPTIONS_PRIVACY - 1);
2015-10-16 02:24:29 +02:00
Visibility++)
if (!strcasecmp (Str,Pri_VisibilityDB[Visibility]))
return Visibility;
2016-01-18 10:21:18 +01:00
return Pri_VISIBILITY_UNKNOWN;
2015-10-16 02:24:29 +02:00
}
/*****************************************************************************/
/**************** Get parameter with visibility from form ********************/
/*****************************************************************************/
Pri_Visibility_t Pri_GetParVisibility (const char *ParName,
unsigned MaskAllowedVisibility)
2015-10-16 02:24:29 +02:00
{
2019-03-23 13:10:31 +01:00
Pri_Visibility_t Visibility;
Visibility = (Pri_Visibility_t) Par_GetParUnsignedLong (ParName,
0,
Pri_NUM_OPTIONS_PRIVACY - 1,
(unsigned long) Pri_VISIBILITY_UNKNOWN);
2019-03-23 13:10:31 +01:00
return (MaskAllowedVisibility & (1 << Visibility)) ? Visibility :
Pri_VISIBILITY_UNKNOWN;
2015-10-16 02:24:29 +02:00
}
/*****************************************************************************/
/*********** Check if user's photo of public profile can be shown ************/
/*****************************************************************************/
// Returns true if it can be shown and false if not.
bool Pri_ShowingIsAllowed (Pri_Visibility_t Visibility,struct Usr_Data *UsrDat)
2015-10-16 02:24:29 +02:00
{
2018-10-10 14:03:06 +02:00
/***** I always can see my things *****/
if (Usr_ItsMe (UsrDat->UsrCod) == Usr_ME)
2016-06-12 19:55:33 +02:00
return true;
2015-11-15 02:24:48 +01:00
/***** System admins always can see others' profiles *****/
2017-06-04 18:18:54 +02:00
if (Gbl.Usrs.Me.Role.Logged == Rol_SYS_ADM)
2015-11-15 02:24:48 +01:00
return true;
2015-10-16 02:24:29 +02:00
/***** Check if I can see the other's photo *****/
switch (Visibility)
{
2016-01-18 10:21:18 +01:00
case Pri_VISIBILITY_UNKNOWN:
2016-06-12 19:55:33 +02:00
return false; // It's not me
2017-01-28 15:58:46 +01:00
case Pri_VISIBILITY_USER: // Only visible
// by me and my teachers if I am a student
// or me and my students if I am a teacher
2016-12-13 13:32:19 +01:00
// Do both users share the same course but whit different role?
return Enr_DB_CheckIfUsrSharesAnyOfMyCrsWithDifferentRole (UsrDat->UsrCod);
2015-10-16 02:24:29 +02:00
case Pri_VISIBILITY_COURSE: // Visible by users sharing courses with me
2016-12-13 13:32:19 +01:00
// Do both users share the same course?
return Enr_CheckIfUsrSharesAnyOfMyCrs (UsrDat);
2015-10-16 02:24:29 +02:00
case Pri_VISIBILITY_SYSTEM: // Visible by any user logged in platform
2015-11-15 02:24:48 +01:00
return Gbl.Usrs.Me.Logged;
2015-10-16 02:24:29 +02:00
case Pri_VISIBILITY_WORLD: // Public, visible by everyone, even unlogged visitors
2015-11-15 02:24:48 +01:00
return true;
2015-10-16 02:24:29 +02:00
}
2015-11-15 02:24:48 +01:00
return false; // Never reached. To avoid warning
2015-10-16 02:24:29 +02:00
}
/*****************************************************************************/
/********** Get and show number of users who have chosen a privacy ***********/
/*****************************************************************************/
void Pri_GetAndShowNumUsrsPerPrivacy (void)
{
extern const char *Hlp_ANALYTICS_Figures_privacy;
extern const char *Txt_Photo;
extern const char *Txt_Basic_public_profile;
extern const char *Txt_Extended_public_profile;
extern const char *Txt_FIGURE_TYPES[Fig_NUM_FIGURES];
/***** Begin box and table *****/
Box_BoxTableBegin (NULL,Txt_FIGURE_TYPES[Fig_PRIVACY],
NULL,NULL,
Hlp_ANALYTICS_Figures_privacy,Box_NOT_CLOSABLE,2);
/***** Privacy for photo *****/
Pri_GetAndShowNumUsrsPerPrivacyForAnObject (Txt_Photo,
"PhotoVisibility",
Pri_PHOTO_ALLOWED_VIS);
/***** Privacy for public profile *****/
Pri_GetAndShowNumUsrsPerPrivacyForAnObject (Txt_Basic_public_profile,
"BaPrfVisibility",
Pri_BASIC_PROFILE_ALLOWED_VIS);
Pri_GetAndShowNumUsrsPerPrivacyForAnObject (Txt_Extended_public_profile,
"ExPrfVisibility",
Pri_EXTENDED_PROFILE_ALLOWED_VIS);
/***** End table and box *****/
Box_BoxTableEnd ();
}
/*****************************************************************************/
/********** Get and show number of users who have chosen a privacy ***********/
/*****************************************************************************/
static void Pri_GetAndShowNumUsrsPerPrivacyForAnObject (const char *TxtObject,
const char *FldName,
unsigned MaskAllowedVisibility)
{
extern const char *Pri_VisibilityDB[Pri_NUM_OPTIONS_PRIVACY];
extern const char *Txt_Number_of_users;
extern const char *Txt_PERCENT_of_users;
extern const char *Txt_PRIVACY_OPTIONS[Pri_NUM_OPTIONS_PRIVACY];
Pri_Visibility_t Visibility;
char *SubQuery;
unsigned NumUsrs[Pri_NUM_OPTIONS_PRIVACY];
unsigned NumUsrsTotal = 0;
/***** Heading row *****/
HTM_TR_Begin (NULL);
HTM_TH (TxtObject ,HTM_HEAD_LEFT);
HTM_TH (Txt_Number_of_users ,HTM_HEAD_RIGHT);
HTM_TH (Txt_PERCENT_of_users,HTM_HEAD_RIGHT);
HTM_TR_End ();
/***** For each privacy option... *****/
for (Visibility = (Pri_Visibility_t) 0;
Visibility <= (Pri_Visibility_t) (Pri_NUM_OPTIONS_PRIVACY - 1);
Visibility++)
if (MaskAllowedVisibility & (1 << Visibility))
{
/* Get the number of users who have chosen this privacy option from database */
if (asprintf (&SubQuery,"usr_data.%s='%s'",
FldName,Pri_VisibilityDB[Visibility]) < 0)
Err_NotEnoughMemoryExit ();
NumUsrs[Visibility] = Usr_DB_GetNumUsrsWhoChoseAnOption (SubQuery);
free (SubQuery);
/* Update total number of users */
NumUsrsTotal += NumUsrs[Visibility];
}
/***** Write number of users who have chosen each privacy option *****/
for (Visibility = (Pri_Visibility_t) 0;
Visibility <= (Pri_Visibility_t) (Pri_NUM_OPTIONS_PRIVACY - 1);
Visibility++)
if (MaskAllowedVisibility & (1 << Visibility))
{
HTM_TR_Begin (NULL);
HTM_TD_Begin ("class=\"LM DAT_%s\"",The_GetSuffix ());
HTM_Txt (Txt_PRIVACY_OPTIONS[Visibility]);
HTM_TD_End ();
HTM_TD_Begin ("class=\"RM DAT_%s\"",The_GetSuffix ());
HTM_Unsigned (NumUsrs[Visibility]);
HTM_TD_End ();
HTM_TD_Begin ("class=\"RM DAT_%s\"",The_GetSuffix ());
HTM_Percentage (NumUsrsTotal ? (double) NumUsrs[Visibility] * 100.0 /
(double) NumUsrsTotal :
0.0);
HTM_TD_End ();
HTM_TR_End ();
}
}