aboutsummaryrefslogtreecommitdiff
path: root/lib/dates.inc.php
blob: acd5109902bd10c7bb3a3a842c1ad44362b04bda (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
 * dates.inc.php - basale Routinen zur Terminveraltung.
 * Copyright (C) 2001 Stefan Suchi <suchi@gmx.de>, André Noack <anoack@mcis.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 **/

require_once 'lib/calendar_functions.inc.php';

/**
 * getWeekday liefert einen String mit einem Tagesnamen.
 *
 * @param int $day_num integer PHP-konformer Tag (0-6)
 * @param bool $short boolean Wenn gesetzt wird der Tag verkürzt zurückgegeben.
 *
 * @throws Exception
 */
function getWeekday(int $day_num, bool $short = true): string
{
    if ($day_num < 0 || $day_num > 6) {
        throw new Exception('Invalid day number');
    }
    return match($day_num) {
        0 => $short ? _('So') : _('Sonntag'),
        1 => $short ? _('Mo') : _('Montag'),
        2 => $short ? _('Di') : _('Dienstag'),
        3 => $short ? _('Mi') : _('Mittwoch'),
        4 => $short ? _('Do') : _('Donnerstag'),
        5 => $short ? _('Fr') : _('Freitag'),
        6 => $short ? _('Sa') : _('Samstag'),
    };
}


/**
 * getMonthName returns the localized name of a certain month in
 * either the abbreviated form (default) or as the actual name.
 *
 * @param string $month Month number
 * @param bool $short Display the abbreviated version or the actual
 *                    name of the month (defaults to abbreviated)
 * @return string Month name
 * @throws Exception when passed an invalid month number
 */
function getMonthName(string $month, bool $short = true): string
{
    $month = (int)$month;

    $months = [
        1  => [_('Januar'), _('Jan.')],
        2  => [_('Februar'), _('Feb.')],
        3  => [_('März'), _('März')],
        4  => [_('April'), _('Apr.')],
        5  => [_('Mai'), _('Mai')],
        6  => [_('Juni'), _('Juni')],
        7  => [_('Juli'), _('Juli')],
        8  => [_('August'), _('Aug.')],
        9  => [_('September'), _('Sep.')],
        10 => [_('Oktober'), _('Okt.')],
        11 => [_('November'), _('Nov.')],
        12 => [_('Dezember'), _('Dez.')],
    ];
    if (!isset($months[$month])) {
        throw new Exception("Invalid month '{$month}'");
    }
    return $months[$month][(int)$short];
}

function leadingZero(string $num): string
{
    if ($num == '') return '00';
    if (mb_strlen($num) < 2) {
        return '0' . $num;
    } else {
        return $num;
    }
}


/**
 * The function shrink_dates expects an array of dates where the start_time and the end_time is noted
 * and creates a compressed version (spanning f.e. multiple dates).
 *
 * Returns an array, where each element is one condensed entry. (f.e. 10.6 - 14.6 8:00 - 12:00)
 */
function shrink_dates(array $dates): array
{
    $ret = [];

    // First step: Clean out all duplicate dates (the dates are sorted)
    foreach ($dates as $key => $date) {
        if (isset($dates[$key + 1])) {
            if ($dates[$key + 1]['start_time'] === $date['start_time']
                && $dates[$key + 1]['end_time'] === $date['end_time']) {
                unset($dates[$key]);
            }
        }
    }

    // Second step: Make sure the dates are still ordered by start- and end-time without any holes
    usort($dates, function ($a, $b) {
        if ($a['start_time'] === $b['start_time']) {
            if ($a['end_time'] === $b['end_time']) return 0;
            return ($a['end_time'] > $b['end_time']) ? 1 : -1;
        }

        return ($a['start_time'] > $b['start_time']) ? 1 : -1;
    });

    // Third step: Check which dates are follow-ups
    for ($i = 1; $i < sizeof($dates); $i++) {
        if (((date('G', $dates[$i - 1]['start_time'])) === date('G', $dates[$i]['start_time']))
            && ((date('i', $dates[$i - 1]['start_time'])) === date('i', $dates[$i]['start_time']))
            && ((date('G', $dates[$i - 1]['end_time'])) === date('G', $dates[$i]['end_time']))
            && ((date('i', $dates[$i - 1]['end_time'])) === date('i', $dates[$i]['end_time']))) {
            $dates[$i]['time_match'] = true;
        }

        if (((date('z', $dates[$i]['start_time']) - 1) === date('z', $dates[$i - 1]['start_time']))
            || ((date('z', $dates[$i]['start_time']) == 0) && (date('j', $dates[$i - 1]['start_time']) == 0))) {
            if (!empty($dates[$i]['time_match'])) {
                $dates[$i]['conjuncted'] = true;
            }
        }
    }

    // Fourth step: aggregate the dates with follow-ups
    $return_string = '';
    // create text-output
    for ($i = 0; $i < count($dates); $i++) {
        $conjuncted = true;
        if (empty($dates[$i]['conjuncted'])) {
            $conjuncted = false;
        }

        if (empty($dates[$i]['conjuncted']) || empty($dates[$i + 1]['conjuncted'])) {
            $return_string .= strftime(' %A, %d.%m.%Y', $dates[$i]['start_time']);
        }

        if (!$conjuncted && !empty($dates[$i + 1]['conjuncted'])) {
            $return_string .= ' -';
            $conjuncted    = true;
        } else if (empty($dates[$i + 1]['conjuncted']) && !empty($dates[$i + 1]['time_match'])) {
            $return_string .= ',';
        }

        if (empty($dates[$i + 1]['time_match'])) {
            // check if the current date is for a whole day
            if ((($dates[$i]['end_time'] - $dates[$i]['start_time']) / 60 / 60) > 23) {
                $return_string .= ' (' . _('ganztägig') . ')';
            } else {
                $return_string .= ' ' . date('H:i', $dates[$i]['start_time']);
                if (date('H:i', $dates[$i]['start_time']) != date('H:i', $dates[$i]['end_time'])) {
                    $return_string .= ' - ' . date('H:i', $dates[$i]['end_time']);
                }
            }
        }

        if ($return_string != '' && empty($dates[$i + 1]['conjuncted']) && empty($dates[$i + 1]['time_match'])) {
            $ret[]         = $return_string;
            $return_string = '';
        }
    }

    return $ret;
}

/**
 * The preliminary meeting function checks whether there is a preliminary meeting and in this case,
 * returns the corresponding preliminary meeting location. Otherwise FALSE is returned.
 *
 * @param string $seminar_id
 * @param string $type
 * @return false|string|null
 */

function vorbesprechung(string $seminar_id, string $type = 'standard'): false|string|null
{
    $termin_id = DBManager::get()->fetchColumn(
        "SELECT termin_id FROM termine WHERE range_id = ? AND date_typ = '2' ORDER BY date",
        [$seminar_id]
    );

    if (!$termin_id) {
        return false;
    }

    $termin = new CourseDate($termin_id);
    $ret = (string) $termin;
    if (!empty($termin->room_booking->resource)) {
        $ret .= ', '._("Ort:").' ';
        switch ($type) {
            case 'export':
                $ret .= $termin->room_booking->resource->name;
                break;

            case 'standard':
            default:
                $ret .= '<a href="' . $termin->room_booking->resource->getActionLink('show') . '" data-dialog>'
                    . htmlReady($termin->room_booking->resource->name) . '</a>';
                break;
        }
    }
    return $ret;
}

/**
 * a small helper funktion to get the type query for "Sitzungstermine"
 * (this dates are important to get the regularly, presence dates
 * for a seminar
 *
 * @return string  the SQL-clause to select only the "Sitzungstermine"
 *
 */
function getPresenceTypeClause(): string
{
    $i          = 0;
    $typ_clause = "(";
    foreach ($GLOBALS['TERMIN_TYP'] as $key => $val) {
        if ($val['sitzung']) {
            if ($i) {
                $typ_clause .= ", ";
            }
            $typ_clause .= "'" . $key . "' ";
            $i++;
        }
    }
    $typ_clause .= ")";

    return $typ_clause;
}

/**
 * Return an array of room snippets, possibly linked
 *
 * @param array $rooms an associative array of rooms
 * @param bool $html true if you want links, otherwise false
 *
 * @return array  an array of (formatted) room snippets
 */
function getFormattedRooms(array $rooms, bool $link = false): array
{
    $room_list = [];

    foreach ($rooms as $room_id => $count) {
        if ($room_id && Config::get()->RESOURCES_ENABLE) {
            $room = Room::find($room_id);
            if ($link) {
                $room_list[] = '<a href="' . $room->getActionLink() . '" data-dialog="1">'
                    . htmlReady($room->name) . '</a>';
            } else {
                $room_list[] = htmlReady($room->name);
            }
        }
    }

    return $room_list;
}

/**
 * Return an array of room snippets without any formatting
 *
 * @param array $rooms an associative array of rooms
 *
 * @return array  an array of room snippets
 */
function getPlainRooms(array $rooms): array
{
    $room_list = [];

    foreach ($rooms as $room_id => $count) {
        if ($room_id) {
            $room        = Room::find($room_id);
            $room_list[] = $room->name;
        }
    }

    return $room_list;
}


/**
 * @param string $comment
 * @param array $dates SingleDate
 */
function raumzeit_send_cancel_message($comment, $dates)
{
    if (!is_array($dates)) {
        $dates = [$dates];
    }
    $course = Course::find($dates[0]->range_id);
    if ($course) {
        $subject = sprintf(_('[%s] Terminausfall'), $course->name);
        $recipients = $course->members->pluck('username');
        $lecturers = $course->members->findBy('status', 'dozent')->pluck('nachname');
        $message = sprintf(
            ngettext(
                _('In der Veranstaltung %s fällt der folgende Termine aus:'),
                _('In der Veranstaltung %s fallen die folgenden Termine aus:'),
                count($dates)
            ),
            $course->name . ' (' . implode(',', $lecturers) . ') ' . $course->start_semester->name
        );
        $message .= "\n\n- ";
        $message .= implode("\n- " , array_map(fn($a) => (string) $a, $dates));
        if ($comment) {
            $message .= "\n\n" . $comment;
        }
        $msg = new messaging();
        return $msg->insert_message($message, $recipients, '____%system%____', '', '', '', '', $subject, true);
    }
}