swad-core/swad_calendar.c

334 lines
12 KiB
C
Raw Normal View History

2015-01-04 14:43:03 +01:00
// swad_calendar.c: Draw month and calendar
/*
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
2015-01-04 14:43:03 +01: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 ***********************************/
/*****************************************************************************/
#include <string.h> // For string functions
2017-06-10 21:38:10 +02:00
#include "swad_box.h"
2015-11-21 20:23:28 +01:00
#include "swad_calendar.h"
#include "swad_database.h"
2017-06-10 21:38:10 +02:00
#include "swad_exam.h"
2018-11-09 20:47:39 +01:00
#include "swad_form.h"
2015-01-04 14:43:03 +01:00
#include "swad_global.h"
2015-11-21 20:23:28 +01:00
#include "swad_parameter.h"
2019-03-26 11:53:21 +01:00
#include "swad_setting.h"
2015-01-04 14:43:03 +01:00
/*****************************************************************************/
/************** External global variables from others modules ****************/
/*****************************************************************************/
extern struct Globals Gbl;
/*****************************************************************************/
/**************************** Private constants ******************************/
/*****************************************************************************/
2015-11-21 20:23:28 +01:00
const bool Cal_DayIsValidAsFirstDayOfWeek[7] =
{
true, // 0: monday
false, // 1: tuesday
false, // 2: wednesday
false, // 3: thursday
false, // 4: friday
false, // 5: saturday
true, // 6: sunday
};
2015-01-04 14:43:03 +01:00
/*****************************************************************************/
/***************************** Private prototypes ****************************/
/*****************************************************************************/
2016-11-07 12:48:40 +01:00
static void Cal_PutIconsFirstDayOfWeek (void);
2015-11-22 00:40:23 +01:00
2015-11-21 20:23:28 +01:00
static unsigned Cal_GetParamFirstDayOfWeek (void);
2017-02-09 03:09:24 +01:00
static void Cal_DrawCalendar (Act_Action_t ActionSeeCalendar,
Act_Action_t ActionChangeCalendar1stDay,
void (*FunctionToDrawContextualIcons) (void),
bool PrintView);
2019-02-15 13:39:37 +01:00
static void Cal_PutIconsCalendar (void);
2016-03-18 21:40:26 +01:00
2015-11-21 20:23:28 +01:00
/*****************************************************************************/
/************** Put icons to select the first day of the week ****************/
/*****************************************************************************/
void Cal_PutIconsToSelectFirstDayOfWeek (void)
{
2019-03-26 11:53:21 +01:00
extern const char *Hlp_PROFILE_Settings_calendar;
2015-11-21 20:23:28 +01:00
extern const char *Txt_Calendar;
2015-11-22 00:40:23 +01:00
2017-06-10 21:38:10 +02:00
Box_StartBox (NULL,Txt_Calendar,Cal_PutIconsFirstDayOfWeek,
2019-03-26 11:53:21 +01:00
Hlp_PROFILE_Settings_calendar,Box_NOT_CLOSABLE);
Set_StartSettingsHead ();
2019-01-12 03:00:59 +01:00
Cal_ShowFormToSelFirstDayOfWeek (ActChg1stDay,NULL);
2019-03-26 11:53:21 +01:00
Set_EndSettingsHead ();
2017-06-10 21:38:10 +02:00
Box_EndBox ();
2015-11-22 00:40:23 +01:00
}
/*****************************************************************************/
2019-03-26 11:53:21 +01:00
/************ Put contextual icons in first-day-of-week setting **************/
2015-11-22 00:40:23 +01:00
/*****************************************************************************/
2016-11-07 12:48:40 +01:00
static void Cal_PutIconsFirstDayOfWeek (void)
2015-11-22 00:40:23 +01:00
{
2016-11-07 12:48:40 +01:00
/***** Put icon to show a figure *****/
2019-02-12 14:46:14 +01:00
Gbl.Figures.FigureType = Fig_FIRST_DAY_OF_WEEK;
Fig_PutIconToShowFigure ();
2015-11-22 00:40:23 +01:00
}
/*****************************************************************************/
2016-11-07 12:48:40 +01:00
/************** Show form to select the first day of the week ****************/
2015-11-22 00:40:23 +01:00
/*****************************************************************************/
2019-01-12 03:00:59 +01:00
void Cal_ShowFormToSelFirstDayOfWeek (Act_Action_t Action,void (*FuncParams) ())
2015-11-22 00:40:23 +01:00
{
2015-11-21 20:23:28 +01:00
extern const char *Txt_First_day_of_the_week;
extern const char *Txt_DAYS_SMALL[7];
unsigned FirstDayOfWeek;
2019-01-12 03:00:59 +01:00
char Icon[32 + 1];
2015-11-21 20:23:28 +01:00
2019-03-26 11:53:21 +01:00
Set_StartOneSettingSelector ();
2015-11-21 20:23:28 +01:00
for (FirstDayOfWeek = 0; // Monday
FirstDayOfWeek <= 6; // Sunday
FirstDayOfWeek++)
if (Cal_DayIsValidAsFirstDayOfWeek[FirstDayOfWeek])
{
2016-12-23 22:19:03 +01:00
fprintf (Gbl.F.Out,"<div class=\"%s\">",
2015-11-22 00:52:55 +01:00
FirstDayOfWeek == Gbl.Prefs.FirstDayOfWeek ? "PREF_ON" :
"PREF_OFF");
2018-11-09 20:47:39 +01:00
Frm_StartForm (Action);
2015-11-21 20:23:28 +01:00
Par_PutHiddenParamUnsigned ("FirstDayOfWeek",FirstDayOfWeek);
2016-12-04 12:42:45 +01:00
if (FuncParams) // Extra parameters depending on the action
FuncParams ();
2019-01-12 03:00:59 +01:00
snprintf (Gbl.Title,sizeof (Gbl.Title),
"%s: %s",
Txt_First_day_of_the_week,Txt_DAYS_SMALL[FirstDayOfWeek]);
snprintf (Icon,sizeof (Icon),
"first-day-of-week-%u.png",
FirstDayOfWeek);
2019-03-26 11:53:21 +01:00
Ico_PutSettingIconLink (Icon,Gbl.Title);
2018-11-09 20:47:39 +01:00
Frm_EndForm ();
2016-12-04 23:09:28 +01:00
fprintf (Gbl.F.Out,"</div>");
2015-11-21 20:23:28 +01:00
}
2019-03-26 11:53:21 +01:00
Set_EndOneSettingSelector ();
2015-11-21 20:23:28 +01:00
}
/*****************************************************************************/
/************************* Change first day of week **************************/
/*****************************************************************************/
void Cal_ChangeFirstDayOfWeek (void)
{
/***** Get param with icon set *****/
Gbl.Prefs.FirstDayOfWeek = Cal_GetParamFirstDayOfWeek ();
2017-05-04 02:19:23 +02:00
/***** Store icon first day of week database *****/
2015-11-21 20:23:28 +01:00
if (Gbl.Usrs.Me.Logged)
2019-03-26 11:53:21 +01:00
DB_QueryUPDATE ("can not update your setting about first day of week",
2018-11-03 12:16:40 +01:00
"UPDATE usr_data SET FirstDayOfWeek=%u"
" WHERE UsrCod=%ld",
Gbl.Prefs.FirstDayOfWeek,
Gbl.Usrs.Me.UsrDat.UsrCod);
2015-11-21 20:23:28 +01:00
2019-03-26 11:53:21 +01:00
/***** Set settings from current IP *****/
Set_SetSettingsFromIP ();
2015-11-21 20:23:28 +01:00
}
/*****************************************************************************/
/*********************** Get parameter with icon set *************************/
/*****************************************************************************/
static unsigned Cal_GetParamFirstDayOfWeek (void)
{
2017-01-29 12:42:19 +01:00
unsigned FirstDayOfWeek;
2015-11-21 20:23:28 +01:00
2017-01-29 21:41:08 +01:00
FirstDayOfWeek = (unsigned)
Par_GetParToUnsignedLong ("FirstDayOfWeek",
0,
6,
Cal_FIRST_DAY_OF_WEEK_DEFAULT);
2017-01-29 12:42:19 +01:00
if (!Cal_DayIsValidAsFirstDayOfWeek[FirstDayOfWeek])
FirstDayOfWeek = Cal_FIRST_DAY_OF_WEEK_DEFAULT;
2015-11-21 20:23:28 +01:00
return FirstDayOfWeek;
}
2017-05-04 11:03:44 +02:00
/*****************************************************************************/
/******************** Get first day of week from string **********************/
/*****************************************************************************/
unsigned Cal_GetFirstDayOfWeekFromStr (const char *Str)
{
unsigned UnsignedNum;
if (sscanf (Str,"%u",&UnsignedNum) == 1)
if (Cal_DayIsValidAsFirstDayOfWeek[UnsignedNum])
return (Dat_Format_t) UnsignedNum;
return Cal_FIRST_DAY_OF_WEEK_DEFAULT;
}
2015-01-04 14:43:03 +01:00
/*****************************************************************************/
/***************************** Draw current month ****************************/
/*****************************************************************************/
void Cal_DrawCurrentMonth (void)
{
2018-12-08 16:43:13 +01:00
extern const char *Lan_STR_LANG_ID[1 + Lan_NUM_LANGUAGES];
2018-11-09 20:47:39 +01:00
char ParamsStr[Frm_MAX_BYTES_PARAMS_STR];
2015-10-29 17:46:48 +01:00
2015-01-04 14:43:03 +01:00
/***** Get list of holidays *****/
if (!Gbl.Hlds.LstIsRead)
{
2017-01-29 12:42:19 +01:00
Gbl.Hlds.SelectedOrder = Hld_ORDER_BY_START_DATE;
2015-01-04 14:43:03 +01:00
Hld_GetListHolidays ();
}
2015-10-29 17:46:48 +01:00
/***** Draw the month in JavaScript *****/
/* JavaScript will write HTML here */
2015-10-29 14:56:01 +01:00
fprintf (Gbl.F.Out,"<div id=\"CurrentMonth\">"
2015-10-29 17:46:48 +01:00
"</div>");
/* Write script to draw the month */
2015-10-30 22:12:28 +01:00
fprintf (Gbl.F.Out,"<script type=\"text/javascript\">"
" Gbl_HTMLContent = '';"
2015-11-21 14:30:53 +01:00
" DrawCurrentMonth ('CurrentMonth',%u,%ld,%ld,'%s/%s',",
2015-11-21 21:00:18 +01:00
Gbl.Prefs.FirstDayOfWeek,
2015-10-29 22:59:40 +01:00
(long) Gbl.StartExecutionTimeUTC,
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ctr.PlcCod,
2016-07-08 12:43:48 +02:00
Cfg_URL_SWAD_CGI,
2018-12-08 16:43:13 +01:00
Lan_STR_LANG_ID[Gbl.Prefs.Language]);
2019-02-15 13:39:37 +01:00
Frm_SetParamsForm (ParamsStr,ActSeeCal,true);
2016-06-01 19:34:44 +02:00
fprintf (Gbl.F.Out,"'%s',",ParamsStr);
2018-11-09 20:47:39 +01:00
Frm_SetParamsForm (ParamsStr,ActSeeDatExaAnn,true);
2015-10-30 22:12:28 +01:00
fprintf (Gbl.F.Out,"'%s');"
2016-06-01 19:34:44 +02:00
"</script>",ParamsStr);
2015-01-04 14:43:03 +01:00
}
/*****************************************************************************/
/************************ Draw an academic calendar **************************/
/*****************************************************************************/
2017-02-09 03:09:24 +01:00
2019-02-15 13:39:37 +01:00
void Cal_ShowCalendar (void)
2017-02-09 03:09:24 +01:00
{
2019-02-15 13:39:37 +01:00
Cal_DrawCalendar (ActSeeCal,ActChgCal1stDay,Cal_PutIconsCalendar,false);
2017-02-09 03:09:24 +01:00
}
void Cal_PrintCalendar (void)
{
Cal_DrawCalendar (ActUnk,ActUnk,NULL,true);
}
static void Cal_DrawCalendar (Act_Action_t ActionSeeCalendar,
Act_Action_t ActionChangeCalendar1stDay,
void (*FunctionToDrawContextualIcons) (void),
bool PrintView)
2015-01-04 14:43:03 +01:00
{
2019-03-26 13:20:21 +01:00
extern const char *Hlp_START_Calendar;
2018-12-08 16:43:13 +01:00
extern const char *Lan_STR_LANG_ID[1 + Lan_NUM_LANGUAGES];
2018-11-09 20:47:39 +01:00
char ParamsStr[Frm_MAX_BYTES_PARAMS_STR];
2015-10-30 17:05:33 +01:00
2015-01-04 14:43:03 +01:00
/***** Get list of holidays *****/
if (!Gbl.Hlds.LstIsRead)
{
2017-01-29 12:42:19 +01:00
Gbl.Hlds.SelectedOrder = Hld_ORDER_BY_START_DATE;
2015-01-04 14:43:03 +01:00
Hld_GetListHolidays ();
}
2017-06-12 14:16:33 +02:00
/***** Start box *****/
2017-06-10 21:38:10 +02:00
Box_StartBox (NULL,NULL,FunctionToDrawContextualIcons,
2017-06-11 22:26:40 +02:00
PrintView ? NULL :
2019-03-26 13:20:21 +01:00
Hlp_START_Calendar,Box_NOT_CLOSABLE);
2016-03-18 22:17:35 +01:00
Lay_WriteHeaderClassPhoto (PrintView,false,
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ins.InsCod,
Gbl.Hierarchy.Deg.DegCod,
2019-04-04 10:45:15 +02:00
Gbl.Hierarchy.Crs.CrsCod);
2015-01-04 14:43:03 +01:00
2019-02-25 15:14:28 +01:00
/***** Preference selector to change first day of week *****/
2015-11-22 00:40:23 +01:00
if (!PrintView)
2019-02-25 15:14:28 +01:00
{
2019-03-26 11:53:21 +01:00
Set_StartSettingsHead ();
2019-01-12 03:00:59 +01:00
Cal_ShowFormToSelFirstDayOfWeek (ActionChangeCalendar1stDay,NULL);
2019-03-26 11:53:21 +01:00
Set_EndSettingsHead ();
2019-02-25 15:14:28 +01:00
}
2015-11-22 00:40:23 +01:00
2019-02-25 15:14:28 +01:00
/***** Draw several months *****/
2015-11-22 00:40:23 +01:00
/* JavaScript will write HTML here */
fprintf (Gbl.F.Out,"<div id=\"calendar\">"
2015-10-30 17:05:33 +01:00
"</div>");
/* Write script to draw the month */
2015-10-30 22:12:28 +01:00
fprintf (Gbl.F.Out,"<script type=\"text/javascript\">"
" Gbl_HTMLContent = '';"
2015-11-21 14:30:53 +01:00
" Cal_DrawCalendar('calendar',%u,%ld,%ld,%s,'%s/%s',",
2015-11-21 21:00:18 +01:00
Gbl.Prefs.FirstDayOfWeek,
2015-10-30 17:05:33 +01:00
(long) Gbl.StartExecutionTimeUTC,
2019-04-03 20:57:04 +02:00
Gbl.Hierarchy.Ctr.PlcCod,
2017-02-09 03:09:24 +01:00
PrintView ? "true" :
"false",
2016-07-08 12:43:48 +02:00
Cfg_URL_SWAD_CGI,
2018-12-08 16:43:13 +01:00
Lan_STR_LANG_ID[Gbl.Prefs.Language]);
2018-11-09 20:47:39 +01:00
Frm_SetParamsForm (ParamsStr,ActionSeeCalendar,true);
2015-10-30 22:12:28 +01:00
fprintf (Gbl.F.Out,"'%s',",
2016-06-01 19:34:44 +02:00
ParamsStr);
2018-11-09 20:47:39 +01:00
Frm_SetParamsForm (ParamsStr,ActSeeDatExaAnn,true);
2015-10-30 22:12:28 +01:00
fprintf (Gbl.F.Out,"'%s');"
"</script>",
2016-06-01 19:34:44 +02:00
ParamsStr);
2015-10-30 17:05:33 +01:00
2017-06-12 14:16:33 +02:00
/***** End box *****/
2017-06-10 21:38:10 +02:00
Box_EndBox ();
2016-03-18 21:40:26 +01:00
}
/*****************************************************************************/
2019-02-15 13:39:37 +01:00
/******************** Put contextual icons in calendar ***********************/
2016-03-18 21:40:26 +01:00
/*****************************************************************************/
2019-02-15 13:39:37 +01:00
static void Cal_PutIconsCalendar (void)
2017-02-09 03:09:24 +01:00
{
2017-03-25 22:34:15 +01:00
/***** Print calendar *****/
2019-02-15 13:39:37 +01:00
Ico_PutContextualIconToPrint (ActPrnCal,NULL);
2017-03-25 21:40:14 +01:00
2017-03-25 22:34:15 +01:00
/***** View holidays *****/
2019-04-03 20:57:04 +02:00
if (Gbl.Hierarchy.Level == Hie_INS && // Institution selected
2019-02-15 10:50:30 +01:00
(Gbl.Hlds.Num || // There are holidays
Gbl.Usrs.Me.Role.Logged >= Rol_INS_ADM)) // Institution admin or system admin
2019-02-15 13:39:37 +01:00
Hld_PutIconToSeeHlds ();
}
/*****************************************************************************/
/************************** Put icon to see calendar *************************/
/*****************************************************************************/
void Cal_PutIconToSeeCalendar (void)
{
extern const char *Txt_Calendar;
Lay_PutContextualLinkOnlyIcon (ActSeeCal,NULL,NULL,
"calendar.svg",
Txt_Calendar);
2017-02-09 03:09:24 +01:00
}