swad-core/swad_role.c

457 lines
16 KiB
C
Raw Normal View History

2014-12-12 20:25:00 +01:00
// swad_role.c: user's roles
/*
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.
2017-01-13 01:51:23 +01:00
Copyright (C) 1999-2017 Antonio Ca<EFBFBD>as Vargas
2014-12-12 20:25:00 +01:00
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 "swad_database.h"
#include "swad_global.h"
#include "swad_parameter.h"
#include "swad_role.h"
2016-12-13 13:32:19 +01:00
#include "swad_role_type.h"
2014-12-12 20:25:00 +01:00
/*****************************************************************************/
/****************************** Public constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/****************************** Internal types *******************************/
/*****************************************************************************/
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/************************* Internal global variables *************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
/*****************************************************************************/
/****************** Get number of available roles for me *********************/
/*****************************************************************************/
unsigned Rol_GetNumAvailableRoles (void)
{
Rol_Role_t Role;
unsigned NumAvailableRoles = 0;
2015-04-07 21:44:24 +02:00
for (Role = Rol__GUEST_;
2014-12-12 20:25:00 +01:00
Role < Rol_NUM_ROLES;
Role++)
if (Gbl.Usrs.Me.AvailableRoles & (1 << Role))
NumAvailableRoles++;
return NumAvailableRoles;
}
/*****************************************************************************/
/************ Get maximum role of a user in all his/her courses **************/
/*****************************************************************************/
Rol_Role_t Rol_GetMaxRole (unsigned Roles)
{
2015-04-07 21:44:24 +02:00
if (Roles & (1 << Rol_TEACHER))
return Rol_TEACHER;
if (Roles & (1 << Rol_STUDENT))
return Rol_STUDENT;
return Rol__GUEST_;
2014-12-12 20:25:00 +01:00
}
/*****************************************************************************/
/******************* Get my maximum role in a institution ********************/
/*****************************************************************************/
Rol_Role_t Rol_GetMyMaxRoleInIns (long InsCod)
{
unsigned NumMyIns;
if (InsCod > 0)
{
/***** Fill the list with the institutions I belong to (if not already filled) *****/
2016-10-28 10:03:37 +02:00
Usr_GetMyInstits ();
2014-12-12 20:25:00 +01:00
/***** Check if the institution passed as parameter is any of my institutions *****/
for (NumMyIns = 0;
2016-10-28 10:03:37 +02:00
NumMyIns < Gbl.Usrs.Me.MyInss.Num;
2014-12-12 20:25:00 +01:00
NumMyIns++)
2016-10-28 10:03:37 +02:00
if (Gbl.Usrs.Me.MyInss.Inss[NumMyIns].InsCod == InsCod)
return Gbl.Usrs.Me.MyInss.Inss[NumMyIns].MaxRole;
2015-04-07 21:44:24 +02:00
return Rol__GUEST_;
2014-12-12 20:25:00 +01:00
}
2015-04-07 21:44:24 +02:00
return Rol_UNKNOWN; // No degree
2014-12-12 20:25:00 +01:00
}
/*****************************************************************************/
/********************** Get my maximum role in a centre **********************/
/*****************************************************************************/
Rol_Role_t Rol_GetMyMaxRoleInCtr (long CtrCod)
{
unsigned NumMyCtr;
if (CtrCod > 0)
{
/***** Fill the list with the centres I belong to (if not already filled) *****/
Usr_GetMyCentres ();
/***** Check if the centre passed as parameter is any of my centres *****/
for (NumMyCtr = 0;
2016-10-28 10:03:37 +02:00
NumMyCtr < Gbl.Usrs.Me.MyCtrs.Num;
2014-12-12 20:25:00 +01:00
NumMyCtr++)
2016-10-28 10:03:37 +02:00
if (Gbl.Usrs.Me.MyCtrs.Ctrs[NumMyCtr].CtrCod == CtrCod)
return Gbl.Usrs.Me.MyCtrs.Ctrs[NumMyCtr].MaxRole;
2015-04-07 21:44:24 +02:00
return Rol__GUEST_;
2014-12-12 20:25:00 +01:00
}
2015-04-07 21:44:24 +02:00
return Rol_UNKNOWN; // No centre
2014-12-12 20:25:00 +01:00
}
/*****************************************************************************/
/********************** Get my maximum role in a degree **********************/
/*****************************************************************************/
Rol_Role_t Rol_GetMyMaxRoleInDeg (long DegCod)
{
unsigned NumMyDeg;
if (DegCod > 0)
{
/***** Fill the list with the degrees I belong to (if not already filled) *****/
Usr_GetMyDegrees ();
/***** Check if the degree passed as parameter is any of my degrees *****/
for (NumMyDeg = 0;
2016-10-28 10:03:37 +02:00
NumMyDeg < Gbl.Usrs.Me.MyDegs.Num;
2014-12-12 20:25:00 +01:00
NumMyDeg++)
2016-10-28 10:03:37 +02:00
if (Gbl.Usrs.Me.MyDegs.Degs[NumMyDeg].DegCod == DegCod)
return Gbl.Usrs.Me.MyDegs.Degs[NumMyDeg].MaxRole;
2015-04-07 21:44:24 +02:00
return Rol__GUEST_;
2014-12-12 20:25:00 +01:00
}
2015-04-07 21:44:24 +02:00
return Rol_UNKNOWN; // No degree
2014-12-12 20:25:00 +01:00
}
/*****************************************************************************/
/*************************** Get my role in a course *************************/
/*****************************************************************************/
Rol_Role_t Rol_GetMyRoleInCrs (long CrsCod)
{
unsigned NumMyCrs;
if (CrsCod > 0)
{
/***** Fill the list with the courses I belong to (if not already filled) *****/
Usr_GetMyCourses ();
/***** Check if the course passed as parameter is any of my courses *****/
for (NumMyCrs = 0;
2016-10-28 10:03:37 +02:00
NumMyCrs < Gbl.Usrs.Me.MyCrss.Num;
2014-12-12 20:25:00 +01:00
NumMyCrs++)
2016-10-28 10:03:37 +02:00
if (Gbl.Usrs.Me.MyCrss.Crss[NumMyCrs].CrsCod == CrsCod)
return Gbl.Usrs.Me.MyCrss.Crss[NumMyCrs].Role;
2015-04-07 21:44:24 +02:00
return Rol__GUEST_;
2014-12-12 20:25:00 +01:00
}
2015-04-07 21:44:24 +02:00
return Rol_UNKNOWN; // No course
2014-12-12 20:25:00 +01:00
}
/*****************************************************************************/
/********************** Get role of a user in a course ***********************/
/*****************************************************************************/
Rol_Role_t Rol_GetRoleInCrs (long CrsCod,long UsrCod)
{
char Query[256];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
Rol_Role_t Role;
if (CrsCod > 0)
{
/***** Get rol of a user in a course from database.
The result of the query will have one row or none *****/
sprintf (Query,"SELECT Role FROM crs_usr"
2017-03-24 01:09:27 +01:00
" WHERE CrsCod=%ld AND UsrCod=%ld",
2014-12-12 20:25:00 +01:00
CrsCod,UsrCod);
if (DB_QuerySELECT (Query,&mysql_res,"can not get the role of a user in a course") == 1) // User belongs to the course
{
row = mysql_fetch_row (mysql_res);
Role = Rol_ConvertUnsignedStrToRole (row[0]);
}
else // User does not belong to the course
2015-04-07 21:44:24 +02:00
Role = Rol_UNKNOWN;
2014-12-12 20:25:00 +01:00
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
else // No course
2015-04-07 21:44:24 +02:00
Role = Rol_UNKNOWN;
2014-12-12 20:25:00 +01:00
return Role;
}
/*****************************************************************************/
/**************** Get roles of a user in all his/her courses *****************/
/*****************************************************************************/
2016-12-13 13:32:19 +01:00
// Roles >=0 ==> already filled/calculated ==> nothing to do
// Roles <0 ==> not yet filled/calculated ==> get roles
2014-12-12 20:25:00 +01:00
2016-12-13 13:32:19 +01:00
void Rol_GetRolesInAllCrssIfNotYetGot (struct UsrData *UsrDat)
2014-12-12 20:25:00 +01:00
{
2016-12-13 13:32:19 +01:00
char Query[128];
2014-12-12 20:25:00 +01:00
MYSQL_RES *mysql_res;
MYSQL_ROW row;
unsigned NumRole;
unsigned NumRoles;
2016-12-13 13:32:19 +01:00
/***** If roles is already filled ==> nothing to do *****/
if (UsrDat->Roles < 0) // Not yet filled
{
/***** Get distinct roles in all courses of the user from database *****/
2017-03-24 01:09:27 +01:00
sprintf (Query,"SELECT DISTINCT(Role) FROM crs_usr WHERE UsrCod=%ld",
2016-12-13 13:32:19 +01:00
UsrDat->UsrCod);
NumRoles = (unsigned) DB_QuerySELECT (Query,&mysql_res,
"can not get the roles of a user"
" in all his/her courses");
for (NumRole = 0, UsrDat->Roles = 0;
NumRole < NumRoles;
NumRole++)
{
row = mysql_fetch_row (mysql_res);
UsrDat->Roles |= (int) (1 << Rol_ConvertUnsignedStrToRole (row[0]));
}
2014-12-12 20:25:00 +01:00
2016-12-13 13:32:19 +01:00
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
}
2014-12-12 20:25:00 +01:00
}
/*****************************************************************************/
/********************** Get role from unsigned string ************************/
/*****************************************************************************/
Rol_Role_t Rol_ConvertUnsignedStrToRole (const char *UnsignedStr)
{
unsigned UnsignedNum;
if (sscanf (UnsignedStr,"%u",&UnsignedNum) == 1)
2015-04-07 21:44:24 +02:00
return (UnsignedNum >= Rol_NUM_ROLES) ? Rol_UNKNOWN :
2014-12-12 20:25:00 +01:00
(Rol_Role_t) UnsignedNum;
2015-04-07 21:44:24 +02:00
return Rol_UNKNOWN;
2014-12-12 20:25:00 +01:00
}
/*****************************************************************************/
/****** Get roles (several bits can be activated) from unsigned string *******/
/*****************************************************************************/
unsigned Rol_ConvertUnsignedStrToRoles (const char *UnsignedStr)
{
unsigned UnsignedNum;
if (sscanf (UnsignedStr,"%u",&UnsignedNum) == 1)
return UnsignedNum;
return 0;
}
/*****************************************************************************/
/*********************** Put a form to change my role ************************/
/*****************************************************************************/
2016-12-10 18:32:35 +01:00
void Rol_PutFormToChangeMyRole (void)
2014-12-12 20:25:00 +01:00
{
2015-03-12 14:45:40 +01:00
extern const char *Txt_ROLES_SINGUL_Abc[Rol_NUM_ROLES][Usr_NUM_SEXS];
2014-12-12 20:25:00 +01:00
Rol_Role_t Role;
Act_FormStart (ActChgMyRol);
2016-12-27 16:45:37 +01:00
fprintf (Gbl.F.Out,"<select name=\"MyRole\" class=\"SEL_ROLE\""
2016-12-10 18:32:35 +01:00
" onchange=\"document.getElementById('%s').submit();\">",
2016-01-14 10:31:09 +01:00
Gbl.Form.Id);
2015-04-07 21:44:24 +02:00
for (Role = Rol__GUEST_;
2014-12-12 20:25:00 +01:00
Role < Rol_NUM_ROLES;
Role++)
if (Gbl.Usrs.Me.AvailableRoles & (1 << Role))
{
fprintf (Gbl.F.Out,"<option value=\"%u\"",(unsigned) Role);
if (Role == Gbl.Usrs.Me.LoggedRole)
fprintf (Gbl.F.Out," selected=\"selected\"");
fprintf (Gbl.F.Out,">%s</option>",
2015-03-12 14:45:40 +01:00
Txt_ROLES_SINGUL_Abc[Role][Gbl.Usrs.Me.UsrDat.Sex]);
2014-12-12 20:25:00 +01:00
}
2015-03-13 00:16:02 +01:00
fprintf (Gbl.F.Out,"</select>");
Act_FormEnd ();
2014-12-12 20:25:00 +01:00
}
/*****************************************************************************/
/****************************** Change my role *******************************/
/*****************************************************************************/
void Rol_ChangeMyRole (void)
{
2017-01-29 12:42:19 +01:00
Rol_Role_t NewRole;
2014-12-12 20:25:00 +01:00
/***** Get parameter with the new logged role ******/
2017-01-29 21:41:08 +01:00
NewRole = (Rol_Role_t)
Par_GetParToUnsignedLong ("MyRole",
0,
Rol_NUM_ROLES - 1,
(unsigned long) Rol_UNKNOWN);
2017-01-29 12:42:19 +01:00
if (NewRole != Rol_UNKNOWN)
2014-12-12 20:25:00 +01:00
{
/* Check if new role is allowed for me */
2017-01-29 12:42:19 +01:00
if (!(Gbl.Usrs.Me.AvailableRoles & (1 << NewRole)))
2014-12-12 20:25:00 +01:00
return;
2017-01-29 12:42:19 +01:00
/* New role is correct and is allowed for me */
if (NewRole != Gbl.Usrs.Me.LoggedRole)
{
/* New role is distinct to current role,
so change my role... */
Gbl.Usrs.Me.LoggedRole = NewRole;
Gbl.Usrs.Me.RoleHasChanged = true;
2014-12-12 20:25:00 +01:00
2017-01-30 19:02:13 +01:00
/* ...update logged role in session... */
2017-01-29 12:42:19 +01:00
Ses_UpdateSessionDataInDB ();
2017-01-30 19:02:13 +01:00
/* ...and update logged role in list of connected */
Con_UpdateMeInConnectedList ();
2017-01-29 12:42:19 +01:00
}
2014-12-12 20:25:00 +01:00
}
}
/*****************************************************************************/
/********************* Write selector of users' roles ************************/
/*****************************************************************************/
2016-03-24 15:09:52 +01:00
void Rol_WriteSelectorRoles (unsigned RolesAllowed,unsigned RolesSelected,
2016-03-24 16:49:36 +01:00
bool Disabled,bool SendOnChange)
2014-12-12 20:25:00 +01:00
{
extern const char *Txt_ROLES_PLURAL_abc[Rol_NUM_ROLES][Usr_NUM_SEXS];
Rol_Role_t Role;
2015-11-01 21:19:33 +01:00
for (Role = Rol_UNKNOWN;
Role <= Rol_SYS_ADM;
2014-12-12 20:25:00 +01:00
Role++)
2015-11-01 21:19:33 +01:00
if ((RolesAllowed & (1 << Role)))
{
2016-12-20 02:18:50 +01:00
fprintf (Gbl.F.Out,"<label>"
"<input type=\"checkbox\" name=\"Role\" value=\"%u\"",
2015-11-01 21:19:33 +01:00
(unsigned) Role);
2016-03-24 15:09:52 +01:00
2015-11-01 21:19:33 +01:00
if ((RolesSelected & (1 << Role)))
fprintf (Gbl.F.Out," checked=\"checked\"");
2016-03-24 15:09:52 +01:00
2016-03-24 16:49:36 +01:00
if (Disabled)
fprintf (Gbl.F.Out," disabled=\"disabled\"");
2016-03-24 15:09:52 +01:00
if (SendOnChange)
fprintf (Gbl.F.Out," onchange=\"document.getElementById('%s').submit();\"",
Gbl.Form.Id);
2016-12-25 21:43:59 +01:00
fprintf (Gbl.F.Out," />"
"%s"
"</label>"
2016-12-20 02:18:50 +01:00
"<br />",
2015-11-01 21:19:33 +01:00
Txt_ROLES_PLURAL_abc[Role][Usr_SEX_UNKNOWN]);
}
2014-12-12 20:25:00 +01:00
}
2016-03-24 15:09:52 +01:00
/*****************************************************************************/
/******************** Put hidden param with users' roles *********************/
/*****************************************************************************/
2016-03-24 16:49:36 +01:00
void Rol_PutHiddenParamRoles (unsigned Roles)
2016-03-24 15:09:52 +01:00
{
fprintf (Gbl.F.Out,"<input type=\"hidden\" name=\"Roles\" value=\"%u\" />",
2016-03-24 16:49:36 +01:00
Roles);
2016-03-24 15:09:52 +01:00
}
2014-12-12 20:25:00 +01:00
/*****************************************************************************/
/************************* Get selected users' roles *************************/
/*****************************************************************************/
2016-03-24 16:49:36 +01:00
unsigned Rol_GetSelectedRoles (void)
2014-12-12 20:25:00 +01:00
{
2017-01-29 12:42:19 +01:00
char StrRoles[Rol_NUM_ROLES * (10 + 1)];
2014-12-12 20:25:00 +01:00
const char *Ptr;
2017-01-28 15:58:46 +01:00
char UnsignedStr[10 + 1];
2014-12-12 20:25:00 +01:00
Rol_Role_t Role;
2016-03-24 16:49:36 +01:00
unsigned Roles;
/***** Try to get param "Roles" with multiple roles *****/
2017-01-29 21:41:08 +01:00
Roles = (unsigned)
Par_GetParToUnsignedLong ("Roles",
0, // 000...000
(1 << Rol_NUM_ROLES) - 1, // 111...111
0); // 000...000
2014-12-12 20:25:00 +01:00
2016-03-24 16:49:36 +01:00
/***** Try to get multiple param "Role" *****/
2017-01-29 12:42:19 +01:00
Par_GetParMultiToText ("Role",StrRoles,Rol_NUM_ROLES * (10 + 1));
2014-12-12 20:25:00 +01:00
for (Ptr = StrRoles;
*Ptr;)
{
Par_GetNextStrUntilSeparParamMult (&Ptr,UnsignedStr,10);
2017-01-29 12:42:19 +01:00
Role = Rol_ConvertUnsignedStrToRole (UnsignedStr);
if (Role != Rol_UNKNOWN)
Roles |= (1 << Role);
2014-12-12 20:25:00 +01:00
}
2016-03-24 16:49:36 +01:00
return Roles;
2014-12-12 20:25:00 +01:00
}
2014-12-12 22:39:55 +01:00
/*****************************************************************************/
/************ Get requested role of a user in current course *****************/
/*****************************************************************************/
Rol_Role_t Rol_GetRequestedRole (long UsrCod)
{
char Query[256];
MYSQL_RES *mysql_res;
MYSQL_ROW row;
2015-04-07 21:44:24 +02:00
Rol_Role_t Role = Rol_UNKNOWN;
2014-12-12 22:39:55 +01:00
/***** Get requested role from database *****/
sprintf (Query,"SELECT Role FROM crs_usr_requests"
2017-03-24 01:09:27 +01:00
" WHERE CrsCod=%ld AND UsrCod=%ld",
2014-12-12 22:39:55 +01:00
Gbl.CurrentCrs.Crs.CrsCod,UsrCod);
if (DB_QuerySELECT (Query,&mysql_res,"can not get requested role"))
{
/***** Get role *****/
row = mysql_fetch_row (mysql_res);
Role = Rol_ConvertUnsignedStrToRole (row[0]);
}
/***** Free structure that stores the query result *****/
DB_FreeMySQLResult (&mysql_res);
return Role;
}