swad-core/swad_scope.c

354 lines
11 KiB
C
Raw Normal View History

2014-12-01 23:55:08 +01:00
// swad_scope.c: scope (platform, centre, degree, course...)
/*
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.
2019-01-07 21:52:19 +01:00
Copyright (C) 1999-2019 Antonio Ca<EFBFBD>as Vargas
2014-12-01 23:55:08 +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 *********************************/
/*****************************************************************************/
2015-11-23 00:44:39 +01:00
#include <string.h> // For string functions
2014-12-01 23:55:08 +01:00
#include "swad_config.h"
#include "swad_global.h"
#include "swad_parameter.h"
#include "swad_scope.h"
/*****************************************************************************/
/****************************** Public constants *****************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Internal constants ****************************/
/*****************************************************************************/
/*****************************************************************************/
/****************************** Internal types *******************************/
/*****************************************************************************/
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/************************* Internal global variables *************************/
/*****************************************************************************/
/*****************************************************************************/
/***************************** Internal prototypes ***************************/
/*****************************************************************************/
/*****************************************************************************/
/** Put a selector to choice between ranges when getting users for listing ***/
/*****************************************************************************/
2016-06-24 20:34:58 +02:00
void Sco_PutSelectorScope (const char *ParamName,bool SendOnChange)
2014-12-01 23:55:08 +01:00
{
extern const char *Txt_System;
extern const char *Txt_Country;
extern const char *Txt_Institution;
extern const char *Txt_Centre;
extern const char *Txt_Degree;
extern const char *Txt_Course;
2019-04-03 20:57:04 +02:00
Hie_Level_t Scope;
2014-12-01 23:55:08 +01:00
bool WriteScope;
2016-12-20 02:18:50 +01:00
fprintf (Gbl.F.Out,"<select id=\"%s\" name=\"%s\"",ParamName,ParamName);
2014-12-01 23:55:08 +01:00
if (SendOnChange)
2015-10-22 14:49:48 +02:00
fprintf (Gbl.F.Out," onchange=\"document.getElementById('%s').submit();\"",
2016-01-14 10:31:09 +01:00
Gbl.Form.Id);
2014-12-01 23:55:08 +01:00
fprintf (Gbl.F.Out,">");
2019-04-03 20:57:04 +02:00
for (Scope = (Hie_Level_t) 0;
Scope < Hie_NUM_LEVELS;
2014-12-01 23:55:08 +01:00
Scope++)
if ((Gbl.Scope.Allowed & (1 << Scope)))
{
/* Don't put forbidden options in selectable list */
WriteScope = false;
switch (Scope)
{
2019-04-03 20:57:04 +02:00
case Hie_SYS:
2014-12-01 23:55:08 +01:00
WriteScope = true;
break;
2019-04-03 20:57:04 +02:00
case Hie_CTY:
if (Gbl.Hierarchy.Cty.CtyCod > 0)
2014-12-01 23:55:08 +01:00
WriteScope = true;
break;
2019-04-03 20:57:04 +02:00
case Hie_INS:
if (Gbl.Hierarchy.Ins.InsCod > 0)
2014-12-01 23:55:08 +01:00
WriteScope = true;
break;
2019-04-03 20:57:04 +02:00
case Hie_CTR:
if (Gbl.Hierarchy.Ctr.CtrCod > 0)
2014-12-01 23:55:08 +01:00
WriteScope = true;
break;
2019-04-03 20:57:04 +02:00
case Hie_DEG:
if (Gbl.Hierarchy.Deg.DegCod > 0)
2014-12-01 23:55:08 +01:00
WriteScope = true;
break;
2019-04-03 20:57:04 +02:00
case Hie_CRS:
if (Gbl.Hierarchy.Level == Hie_CRS)
2014-12-01 23:55:08 +01:00
WriteScope = true;
break;
default:
2018-10-24 23:03:11 +02:00
Lay_WrongScopeExit ();
2014-12-01 23:55:08 +01:00
break;
}
if (WriteScope)
{
/***** Write allowed option *****/
fprintf (Gbl.F.Out,"<option value=\"%u\"",(unsigned) Scope);
if (Gbl.Scope.Current == Scope)
fprintf (Gbl.F.Out," selected=\"selected\"");
fprintf (Gbl.F.Out,">");
switch (Scope)
{
2019-04-03 20:57:04 +02:00
case Hie_SYS:
2014-12-01 23:55:08 +01:00
fprintf (Gbl.F.Out,"%s: %s",
2015-07-24 11:34:39 +02:00
Txt_System,Cfg_PLATFORM_SHORT_NAME);
2014-12-01 23:55:08 +01:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CTY:
2014-12-01 23:55:08 +01:00
fprintf (Gbl.F.Out,"%s: %s",
Txt_Country,
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Cty.Name[Gbl.Prefs.Language]);
2014-12-01 23:55:08 +01:00
break;
2019-04-03 20:57:04 +02:00
case Hie_INS:
2014-12-01 23:55:08 +01:00
fprintf (Gbl.F.Out,"%s: %s",
Txt_Institution,
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ins.ShrtName);
2014-12-01 23:55:08 +01:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CTR:
2014-12-01 23:55:08 +01:00
fprintf (Gbl.F.Out,"%s: %s",
Txt_Centre,
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ctr.ShrtName);
2014-12-01 23:55:08 +01:00
break;
2019-04-03 20:57:04 +02:00
case Hie_DEG:
2014-12-01 23:55:08 +01:00
fprintf (Gbl.F.Out,"%s: %s",
Txt_Degree,
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Deg.ShrtName);
2014-12-01 23:55:08 +01:00
break;
2019-04-03 20:57:04 +02:00
case Hie_CRS:
2014-12-01 23:55:08 +01:00
fprintf (Gbl.F.Out,"%s: %s",
Txt_Course,
2019-04-04 10:45:15 +02:00
Gbl.Hierarchy.Crs.ShrtName);
2014-12-01 23:55:08 +01:00
break;
default:
2018-10-24 23:03:11 +02:00
Lay_WrongScopeExit ();
2014-12-01 23:55:08 +01:00
break;
}
fprintf (Gbl.F.Out,"</option>");
}
}
fprintf (Gbl.F.Out,"</select>");
}
/*****************************************************************************/
2016-11-27 14:10:31 +01:00
/********************** Put hidden parameter scope ***************************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2019-04-03 20:57:04 +02:00
void Sco_PutParamScope (const char *ParamName,Hie_Level_t Scope)
2014-12-01 23:55:08 +01:00
{
2016-06-24 20:34:58 +02:00
Par_PutHiddenParamUnsigned (ParamName,(unsigned) Scope);
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
2016-11-27 14:10:31 +01:00
/*************************** Get parameter scope *****************************/
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
2016-06-24 20:34:58 +02:00
void Sco_GetScope (const char *ParamName)
2014-12-01 23:55:08 +01:00
{
2017-01-29 12:42:19 +01:00
/***** Get parameter with scope *****/
2019-04-03 20:57:04 +02:00
Gbl.Scope.Current = (Hie_Level_t)
2017-01-29 21:41:08 +01:00
Par_GetParToUnsignedLong (ParamName,
0,
2019-04-03 20:57:04 +02:00
Hie_NUM_LEVELS - 1,
(unsigned long) Hie_UNK);
2016-11-27 14:10:31 +01:00
/***** Adjust scope avoiding impossible or forbidden scopes *****/
Sco_AdjustScope ();
}
/*****************************************************************************/
/*********** Adjust scope avoiding impossible or forbidden scopes ************/
/*****************************************************************************/
void Sco_AdjustScope (void)
{
/***** Is scope is unknow, use default scope *****/
2019-04-03 20:57:04 +02:00
if (Gbl.Scope.Current == Hie_UNK)
2016-06-24 20:34:58 +02:00
Gbl.Scope.Current = Gbl.Scope.Default;
2014-12-01 23:55:08 +01:00
2016-06-24 20:34:58 +02:00
/***** Avoid impossible scopes *****/
2019-04-04 10:45:15 +02:00
if (Gbl.Scope.Current == Hie_CRS && Gbl.Hierarchy.Crs.CrsCod <= 0)
2019-04-03 20:57:04 +02:00
Gbl.Scope.Current = Hie_DEG;
2014-12-01 23:55:08 +01:00
2019-04-03 20:57:04 +02:00
if (Gbl.Scope.Current == Hie_DEG && Gbl.Hierarchy.Deg.DegCod <= 0)
Gbl.Scope.Current = Hie_CTR;
2014-12-01 23:55:08 +01:00
2019-04-03 20:57:04 +02:00
if (Gbl.Scope.Current == Hie_CTR && Gbl.Hierarchy.Ctr.CtrCod <= 0)
Gbl.Scope.Current = Hie_INS;
2014-12-01 23:55:08 +01:00
2019-04-03 20:57:04 +02:00
if (Gbl.Scope.Current == Hie_INS && Gbl.Hierarchy.Ins.InsCod <= 0)
Gbl.Scope.Current = Hie_CTY;
2014-12-01 23:55:08 +01:00
2019-04-03 20:57:04 +02:00
if (Gbl.Scope.Current == Hie_CTY && Gbl.Hierarchy.Cty.CtyCod <= 0)
Gbl.Scope.Current = Hie_SYS;
2014-12-01 23:55:08 +01:00
2016-06-24 20:34:58 +02:00
/***** Avoid forbidden scopes *****/
if ((Gbl.Scope.Allowed & (1 << Gbl.Scope.Current)) == 0)
2019-04-03 20:57:04 +02:00
Gbl.Scope.Current = Hie_UNK;
2014-12-01 23:55:08 +01:00
}
/*****************************************************************************/
/****************** Set allowed scopes when listing guests *******************/
/*****************************************************************************/
void Sco_SetScopesForListingGuests (void)
{
2017-06-04 18:18:54 +02:00
switch (Gbl.Usrs.Me.Role.Logged)
2014-12-01 23:55:08 +01:00
{
2015-04-07 21:44:24 +02:00
case Rol_CTR_ADM:
2019-04-03 20:57:04 +02:00
Gbl.Scope.Allowed = 1 << Hie_CTR;
Gbl.Scope.Default = Hie_CTR;
2014-12-01 23:55:08 +01:00
break;
2015-04-07 21:44:24 +02:00
case Rol_INS_ADM:
2019-04-03 20:57:04 +02:00
Gbl.Scope.Allowed = 1 << Hie_INS |
1 << Hie_CTR;
Gbl.Scope.Default = Hie_INS;
2014-12-01 23:55:08 +01:00
break;
2015-04-07 21:44:24 +02:00
case Rol_SYS_ADM:
2019-04-03 20:57:04 +02:00
Gbl.Scope.Allowed = 1 << Hie_SYS |
1 << Hie_CTY |
1 << Hie_INS |
1 << Hie_CTR;
Gbl.Scope.Default = Hie_SYS;
2014-12-01 23:55:08 +01:00
break;
default:
Gbl.Scope.Allowed = 0;
2019-04-03 20:57:04 +02:00
Gbl.Scope.Default = Hie_UNK;
2014-12-01 23:55:08 +01:00
break;
}
}
/*****************************************************************************/
/**************** Set allowed scopes when listing students *******************/
/*****************************************************************************/
void Sco_SetScopesForListingStudents (void)
{
2019-04-03 20:57:04 +02:00
Gbl.Scope.Default = Hie_CRS;
2017-06-04 18:18:54 +02:00
switch (Gbl.Usrs.Me.Role.Logged)
2014-12-01 23:55:08 +01:00
{
2017-05-18 19:13:41 +02:00
case Rol_STD:
2017-05-22 20:37:46 +02:00
case Rol_NET:
2017-05-18 19:13:41 +02:00
case Rol_TCH:
2019-04-03 20:57:04 +02:00
Gbl.Scope.Allowed = 1 << Hie_CRS;
2014-12-01 23:55:08 +01:00
break;
2015-04-07 21:44:24 +02:00
case Rol_DEG_ADM:
2019-04-03 20:57:04 +02:00
Gbl.Scope.Allowed = 1 << Hie_DEG |
1 << Hie_CRS;
2015-09-20 19:20:05 +02:00
break;
case Rol_CTR_ADM:
2019-04-03 20:57:04 +02:00
Gbl.Scope.Allowed = 1 << Hie_CTR |
1 << Hie_DEG |
1 << Hie_CRS;
2015-09-20 19:20:05 +02:00
break;
case Rol_INS_ADM:
2019-04-03 20:57:04 +02:00
Gbl.Scope.Allowed = 1 << Hie_INS |
1 << Hie_CTR |
1 << Hie_DEG |
1 << Hie_CRS;
2014-12-01 23:55:08 +01:00
break;
2015-04-07 21:44:24 +02:00
case Rol_SYS_ADM:
2019-04-03 20:57:04 +02:00
Gbl.Scope.Allowed = 1 << Hie_SYS |
1 << Hie_CTY |
1 << Hie_INS |
1 << Hie_CTR |
1 << Hie_DEG |
1 << Hie_CRS;
2014-12-01 23:55:08 +01:00
break;
default:
Gbl.Scope.Allowed = 0;
2019-04-03 20:57:04 +02:00
Gbl.Scope.Default = Hie_UNK;
2014-12-01 23:55:08 +01:00
break;
}
}
2015-11-23 00:44:39 +01:00
/*****************************************************************************/
2016-10-27 15:06:11 +02:00
/*********************** Get scope from unsigned string **********************/
2015-11-23 00:44:39 +01:00
/*****************************************************************************/
2019-04-03 20:57:04 +02:00
Hie_Level_t Sco_GetScopeFromUnsignedStr (const char *UnsignedStr)
2015-11-23 00:44:39 +01:00
{
unsigned UnsignedNum;
if (sscanf (UnsignedStr,"%u",&UnsignedNum) == 1)
2019-04-03 20:57:04 +02:00
if (UnsignedNum < Hie_NUM_LEVELS)
return (Hie_Level_t) UnsignedNum;
2015-11-23 00:44:39 +01:00
2019-04-03 20:57:04 +02:00
return Hie_UNK;
2015-11-23 00:44:39 +01:00
}
2016-10-27 15:06:11 +02:00
/*****************************************************************************/
/*********************** Get scope from database string **********************/
/*****************************************************************************/
2019-04-03 20:57:04 +02:00
Hie_Level_t Sco_GetScopeFromDBStr (const char *ScopeDBStr)
2016-10-27 15:06:11 +02:00
{
2019-04-03 20:57:04 +02:00
Hie_Level_t Scope;
2016-10-27 15:06:11 +02:00
2019-04-03 20:57:04 +02:00
for (Scope = Hie_UNK;
Scope < Hie_NUM_LEVELS;
2016-10-27 15:06:11 +02:00
Scope++)
2019-04-01 23:15:17 +02:00
if (!strcmp (Sco_GetDBStrFromScope (Scope),ScopeDBStr))
2016-10-27 15:06:11 +02:00
return Scope;
2019-04-03 20:57:04 +02:00
return Hie_UNK;
2016-10-27 15:06:11 +02:00
}
2019-04-01 23:15:17 +02:00
/*****************************************************************************/
/*********************** Get scope from database string **********************/
/*****************************************************************************/
2019-04-03 20:57:04 +02:00
const char *Sco_GetDBStrFromScope (Hie_Level_t Scope)
2019-04-01 23:15:17 +02:00
{
2019-04-03 20:57:04 +02:00
static const char *Sco_ScopeDB[Hie_NUM_LEVELS] =
2019-04-01 23:15:17 +02:00
{
2019-04-03 20:57:04 +02:00
"Unk", // Hie_UNK
"Sys", // Hie_SYS
"Cty", // Hie_CTY
"Ins", // Hie_INS
"Ctr", // Hie_CTR
"Deg", // Hie_DEG
"Crs", // Hie_CRS
2019-04-01 23:15:17 +02:00
};
2019-04-03 20:57:04 +02:00
if (Scope >= Hie_NUM_LEVELS)
Scope = Hie_UNK;
2019-04-01 23:15:17 +02:00
return Sco_ScopeDB[Scope];
}