aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/calendar/Helper.php
blob: dd860070982fd6cf0c243dcfb5e136837156efec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php

namespace Studip\Calendar;

class Helper
{
    /**
     * Retrieves the time slot duration in the calendar for a specified calendar type
     * and either the current user or a specific user.
     *
     * @param string $calendar_type The calendar type for which to retrieve the slot duration.
     *     Valid values: 'week', 'day', 'week_group' (week group calendar), 'week_day' (day group calendar).
     *     Defaults to 'week'.
     * @param string $user_id The user for which to retrieve the slot duration. Defaults to an
     *     empty string which then in turn means the current users slot duration is retrieved.
     *
     * @return string The slot duration as a time string in the form HH:MM:SS.
     */
    public static function getCalendarSlotDuration(string $calendar_type = 'week', string $user_id = '') : string
    {
        $default_slot_duration = '00:30:00';

        $user_config = new \UserConfig($user_id ?: $GLOBALS['user']->id);
        $calendar_settings = $user_config->CALENDAR_SETTINGS;

        if (
            $calendar_type === 'week'
            && !empty($calendar_settings['step_week'])
        ) {
            $step_week = (int) $calendar_settings['step_week'];
            $hours = floor($step_week / 3600);
            $minutes = round(($step_week - $hours * 3600) / 60);
            return sprintf('%1$02u:%2$02u:00', $hours, $minutes);
        } elseif (
            $calendar_type === 'day'
            && !empty($calendar_settings['step_day'])
        ) {
            $step_day = (int) $calendar_settings['step_day'];
            $hours = floor($step_day / 3600);
            $minutes = round(($step_day - $hours * 3600) / 60);
            return sprintf('%1$02u:%2$02u:00', $hours, $minutes);
        } elseif (
            $calendar_type === 'week_group'
            && !empty($calendar_settings['step_week_group'])
        ) {
            $step_week = (int) $calendar_settings['step_week_group'];
            $hours = floor($step_week / 3600);
            $minutes = round(($step_week - $hours * 3600) / 60);
            return sprintf('%1$02u:%2$02u:00', $hours, $minutes);
        } elseif (
            $calendar_type === 'day_group'
            && !empty($calendar_settings['step_day_group'])
        ) {
            $step_day = (int) $calendar_settings['step_day_group'];
            $hours = floor($step_day / 3600);
            $minutes = round(($step_day - $hours * 3600) / 60);
            return sprintf('%1$02u:%2$02u:00', $hours, $minutes);
        }

        // An unknown slot type or no appropriate match before:
        // Return the default duration.
        return $default_slot_duration;
    }


    /**
     * Retrieves the default calendar date by various methods.
     *
     * @return \DateTime The default date for the calendar.
     *     This defaults to the current date if no other date
     *     can be retrieved.
     */
    public static function getDefaultCalendarDate() : \DateTime
    {
        $default_date = new \DateTime();
        if (\Request::submitted('date') || \Request::submitted('defaultDate')) {
            $parameter_name = 'date';
            if (\Request::submitted('defaultDate')) {
                $parameter_name = 'defaultDate';
            }
            $date = \Request::getDateTime($parameter_name, 'Y-m-d');
            if ($date instanceof \DateTime) {
                $default_date = $date;
                //Update the session value:
                $_SESSION['calendar_date'] = $default_date->format('Y-m-d');
            }
        } elseif (\Request::submitted('semester_id')) {
            //A semester-ID is set, but no specific date that would override it.
            //Use the first lecture week of the semester as default date.
            $semester_id = \Request::option('semester_id');
            $semester = \Semester::find($semester_id);
            if ($semester) {
                $default_date->setTimestamp($semester->vorles_beginn);
                //Update the session value:
                $_SESSION['calendar_date'] = $default_date->format('Y-m-d');
            }
        } elseif (!empty($_SESSION['calendar_date'])) {
            $date = \DateTime::createFromFormat(
                'Y-m-d',
                $_SESSION['calendar_date'],
                $default_date->getTimezone()
            );
            if ($date instanceof \DateTime) {
                $default_date = $date;
            }
        }
        $default_date->setTime(0,0,0);

        return $default_date;
    }

    /**
     * Constructs a Fullcalendar instance of the schedule for the current user.
     *
     * @param string $semester_id The ID of the semester to be used. Defaults to an empty string
     *     which in turn means that the current semester shall be used.
     *
     * @param bool $show_hidden_courses Whether to include hidden courses in the schedule (true)
     *     or not (false). Defaults to false.
     *
     * @return \Studip\Fullcalendar A fullcalendar instance for the schedule of the current user.
     */
    public static function getScheduleFullcalendar(
        string $semester_id = '',
        bool $show_hidden_courses = false
    ) : \Studip\Fullcalendar
    {
        if (!$semester_id) {
            $semester_id = \Semester::findCurrent()?->id ?? '';
        }
        $calendar_settings = \User::findCurrent()->getConfiguration()->CALENDAR_SETTINGS ?? [];

        return new \Studip\Fullcalendar(
            _('Stundenplan'),
            [
                'editable'    => false,
                'selectable'  => false,
                'dialog_size' => 'auto',
                'minTime'     => sprintf('%02u:00', $calendar_settings['start'] ?? 8),
                'maxTime'     => sprintf('%02u:00', $calendar_settings['end'] ?? 20),
                'allDaySlot'  => false,
                'header'      => [
                    'left' => '',
                    'right' => ''
                ],
                'views' => [
                    'timeGridWeek' => [
                        'columnHeaderFormat' => ['weekday' => 'long'],
                        'weekends'           => $calendar_settings['type_week'] === 'LONG',
                        'slotDuration'       => self::getCalendarSlotDuration('week'),
                    ]
                ],
                'defaultView' => 'timeGridWeek',
                'defaultDate' => date('Y-m-d'),
                'timeGridEventMinHeight' => 20,
                'eventSources' => [
                    [
                        'url' => \URLHelper::getURL(
                            'dispatch.php/calendar/schedule/data',
                            ['show_hidden' => $show_hidden_courses]
                        ),
                        'method' => 'GET',
                        'extraParams' => [
                            'semester_id' => $semester_id,
                            'full_semester_time_range' => false
                        ]
                    ]
                ]
            ]
        );
    }
}