swad-core/swad_calendar.c

402 lines
14 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.
2017-01-13 01:51:23 +01:00
Copyright (C) 1999-2017 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
#include "swad_exam.h"
2015-11-21 20:23:28 +01:00
#include "swad_calendar.h"
#include "swad_database.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"
#include "swad_preference.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);
static void Cal_PutIconToPrintCalendarSys (void);
static void Cal_PutIconToPrintCalendarCty (void);
static void Cal_PutIconToPrintCalendarIns (void);
static void Cal_PutIconToPrintCalendarCtr (void);
static void Cal_PutIconToPrintCalendarDeg (void);
static void Cal_PutIconToPrintCalendarCrs (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)
{
2016-11-13 01:21:35 +01:00
extern const char *Hlp_PROFILE_Preferences_calendar;
2015-11-21 20:23:28 +01:00
extern const char *Txt_Calendar;
2015-11-22 00:40:23 +01:00
2016-11-13 01:21:35 +01:00
Lay_StartRoundFrame (NULL,Txt_Calendar,
Cal_PutIconsFirstDayOfWeek,
Hlp_PROFILE_Preferences_calendar);
2017-05-05 13:42:59 +02:00
Cal_ShowFormToSelFirstDayOfWeek (ActChg1stDay,NULL,"ICO25x25B");
2016-11-07 12:48:40 +01:00
Lay_EndRoundFrame ();
2015-11-22 00:40:23 +01:00
}
/*****************************************************************************/
2016-11-07 12:48:40 +01:00
/*********** Put contextual icons in first-day-of-week preference ************/
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 *****/
Gbl.Stat.FigureType = Sta_FIRST_DAY_OF_WEEK;
Sta_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
/*****************************************************************************/
2016-12-04 12:42:45 +01:00
void Cal_ShowFormToSelFirstDayOfWeek (Act_Action_t Action,void (*FuncParams) (),
const char *ClassIcon)
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;
2016-12-23 22:19:03 +01:00
fprintf (Gbl.F.Out,"<div class=\"PREF_CONTAINER\">");
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");
2015-11-22 00:40:23 +01:00
Act_FormStart (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 ();
2015-11-21 20:23:28 +01:00
fprintf (Gbl.F.Out,"<input type=\"image\" src=\"%s/first-day-of-week-%u-64x64.png\""
2016-12-24 13:46:29 +01:00
" alt=\"%s\" title=\"%s: %s\" class=\"%s\" />",
2015-11-21 20:23:28 +01:00
Gbl.Prefs.IconsURL,
FirstDayOfWeek,
Txt_DAYS_SMALL[FirstDayOfWeek],
2015-11-22 00:40:23 +01:00
Txt_First_day_of_the_week,Txt_DAYS_SMALL[FirstDayOfWeek],
ClassIcon);
2015-11-21 20:23:28 +01:00
Act_FormEnd ();
2016-12-04 23:09:28 +01:00
fprintf (Gbl.F.Out,"</div>");
2015-11-21 20:23:28 +01:00
}
2016-12-04 23:09:28 +01:00
fprintf (Gbl.F.Out,"</div>");
2015-11-21 20:23:28 +01:00
}
/*****************************************************************************/
/************************* Change first day of week **************************/
/*****************************************************************************/
void Cal_ChangeFirstDayOfWeek (void)
{
char Query[512];
/***** 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)
{
2017-03-24 01:09:27 +01:00
sprintf (Query,"UPDATE usr_data SET FirstDayOfWeek=%u"
" WHERE UsrCod=%ld",
2015-11-21 20:23:28 +01:00
Gbl.Prefs.FirstDayOfWeek,
Gbl.Usrs.Me.UsrDat.UsrCod);
DB_QueryUPDATE (Query,"can not update your preference about first day of week");
}
/***** Set preferences from current IP *****/
Pre_SetPrefsFromIP ();
}
/*****************************************************************************/
/*********************** 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)
{
2017-01-28 15:58:46 +01:00
extern const char *Txt_STR_LANG_ID[1 + Txt_NUM_LANGUAGES];
2017-03-13 14:22:36 +01:00
char ParamsStr[256 + 256 + Ses_BYTES_SESSION_ID + 256];
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,
2015-10-30 22:12:28 +01:00
Gbl.CurrentCtr.Ctr.PlcCod,
2016-07-08 12:43:48 +02:00
Cfg_URL_SWAD_CGI,
2015-12-07 23:13:08 +01:00
Txt_STR_LANG_ID[Gbl.Prefs.Language]);
2017-02-09 14:32:19 +01:00
Act_SetParamsForm (ParamsStr,Cal_GetActionToSeeCalendar (),true);
2016-06-01 19:34:44 +02:00
fprintf (Gbl.F.Out,"'%s',",ParamsStr);
2016-06-03 10:37:00 +02:00
Act_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
}
2017-02-09 14:32:19 +01:00
/*****************************************************************************/
/********** Get action to see calendar depending on current scope ************/
/*****************************************************************************/
Act_Action_t Cal_GetActionToSeeCalendar ()
{
if (Gbl.CurrentCrs.Crs.CrsCod > 0) // Course selected
return ActSeeCalCrs;
if (Gbl.CurrentDeg.Deg.DegCod > 0) // Degree selected
return ActSeeCalDeg;
if (Gbl.CurrentCtr.Ctr.CtrCod > 0) // Centre selected
return ActSeeCalCtr;
if (Gbl.CurrentIns.Ins.InsCod > 0) // Institution selected
return ActSeeCalIns;
if (Gbl.CurrentCty.Cty.CtyCod > 0) // Country selected
return ActSeeCalCty;
return ActSeeCalSys;
}
2015-01-04 14:43:03 +01:00
/*****************************************************************************/
/************************ Draw an academic calendar **************************/
/*****************************************************************************/
2017-02-09 03:09:24 +01:00
void Cal_DrawCalendarSys (void)
{
Cal_DrawCalendar (ActSeeCalSys,ActChgCalSys1stDay,Cal_PutIconToPrintCalendarSys,false);
}
void Cal_DrawCalendarCty (void)
{
Cal_DrawCalendar (ActSeeCalCty,ActChgCalCty1stDay,Cal_PutIconToPrintCalendarCty,false);
}
void Cal_DrawCalendarIns (void)
{
Cal_DrawCalendar (ActSeeCalIns,ActChgCalIns1stDay,Cal_PutIconToPrintCalendarIns,false);
}
void Cal_DrawCalendarCtr (void)
{
Cal_DrawCalendar (ActSeeCalCtr,ActChgCalCtr1stDay,Cal_PutIconToPrintCalendarCtr,false);
}
void Cal_DrawCalendarDeg (void)
{
Cal_DrawCalendar (ActSeeCalDeg,ActChgCalDeg1stDay,Cal_PutIconToPrintCalendarDeg,false);
}
void Cal_DrawCalendarCrs (void)
{
Cal_DrawCalendar (ActSeeCalCrs,ActChgCalCrs1stDay,Cal_PutIconToPrintCalendarCrs,false);
}
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
{
2017-02-13 12:08:11 +01:00
extern const char *Hlp_Calendar;
2017-01-28 15:58:46 +01:00
extern const char *Txt_STR_LANG_ID[1 + Txt_NUM_LANGUAGES];
2017-03-13 14:22:36 +01:00
char ParamsStr[256 + 256 + Ses_BYTES_SESSION_ID + 256];
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 ();
}
2016-03-18 22:17:35 +01:00
/***** Start frame *****/
2016-12-10 01:21:02 +01:00
Lay_StartRoundFrame (NULL,NULL,
2017-02-09 03:09:24 +01:00
FunctionToDrawContextualIcons,
2016-12-10 01:21:02 +01:00
PrintView ? NULL :
2017-02-13 12:08:11 +01:00
Hlp_Calendar);
2016-03-18 22:17:35 +01:00
Lay_WriteHeaderClassPhoto (PrintView,false,
2015-01-04 14:43:03 +01:00
Gbl.CurrentIns.Ins.InsCod,
Gbl.CurrentDeg.Deg.DegCod,
Gbl.CurrentCrs.Crs.CrsCod);
/***** Draw several months *****/
2015-11-22 00:40:23 +01:00
/* Show form to change first day of week */
if (!PrintView)
2017-02-09 03:09:24 +01:00
Cal_ShowFormToSelFirstDayOfWeek (ActionChangeCalendar1stDay,NULL,"ICO25x25");
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,
2015-10-30 22:12:28 +01:00
Gbl.CurrentCtr.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,
2015-12-07 23:13:08 +01:00
Txt_STR_LANG_ID[Gbl.Prefs.Language]);
2017-02-09 03:09:24 +01:00
Act_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);
2016-06-03 10:37:00 +02:00
Act_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
2015-01-04 14:43:03 +01:00
/***** End frame *****/
2016-03-18 21:40:26 +01:00
Lay_EndRoundFrame ();
}
/*****************************************************************************/
2016-11-06 15:36:26 +01:00
/************************ Put icon to print calendar *************************/
2016-03-18 21:40:26 +01:00
/*****************************************************************************/
2017-02-09 03:09:24 +01:00
static void Cal_PutIconToPrintCalendarSys (void)
{
2017-04-30 23:48:48 +02:00
Lay_PutContextualIconToPrint (ActPrnCalSys,NULL);
2017-02-09 03:09:24 +01:00
}
static void Cal_PutIconToPrintCalendarCty (void)
{
2017-04-30 23:48:48 +02:00
Lay_PutContextualIconToPrint (ActPrnCalCty,NULL);
2017-02-09 03:09:24 +01:00
}
static void Cal_PutIconToPrintCalendarIns (void)
{
2017-03-25 21:40:14 +01:00
extern const char *Txt_Holidays;
2017-02-09 03:09:24 +01:00
2017-03-25 22:34:15 +01:00
/***** Print calendar *****/
2017-04-30 23:48:48 +02:00
Lay_PutContextualIconToPrint (ActPrnCalIns,NULL);
2017-03-25 21:40:14 +01:00
2017-03-25 22:34:15 +01:00
/***** View holidays *****/
if (Gbl.Hlds.Num || // There are holidays
Gbl.Usrs.Me.LoggedRole >= Rol_INS_ADM) // Institution admin or system admin
2017-04-17 19:03:21 +02:00
Lay_PutContextualLink (ActSeeHld,NULL,NULL,
2017-03-25 21:40:14 +01:00
"holiday64x64.png",
Txt_Holidays,NULL,
NULL);
2017-02-09 03:09:24 +01:00
}
static void Cal_PutIconToPrintCalendarCtr (void)
{
2017-04-30 23:48:48 +02:00
Lay_PutContextualIconToPrint (ActPrnCalCtr,NULL);
2017-02-09 03:09:24 +01:00
}
static void Cal_PutIconToPrintCalendarDeg (void)
{
2017-04-30 23:48:48 +02:00
Lay_PutContextualIconToPrint (ActPrnCalDeg,NULL);
2017-02-09 03:09:24 +01:00
}
static void Cal_PutIconToPrintCalendarCrs (void)
2016-03-18 21:40:26 +01:00
{
2017-04-30 23:48:48 +02:00
Lay_PutContextualIconToPrint (ActPrnCalCrs,NULL);
2015-01-04 14:43:03 +01:00
}