aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Semester.class.php
blob: 10b312b504c01f7f6aa64accf7afc4df7e2bd9ab (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php

/**
 * Semester.class.php
 * model class for table semester_data
 *
 * 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.
 *
 * @author      André Noack <noack@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 *
 * @property string semester_id database column
 * @property string id alias column for semester_id
 * @property string name database column
 * @property string description database column
 * @property string semester_token database column
 * @property string beginn database column
 * @property string ende database column
 * @property string vorles_beginn database column
 * @property string vorles_ende database column
 * @property string first_sem_week computed column
 * @property string last_sem_week computed column
 * @property string past computed column
 */
class Semester extends SimpleORMap
{
    /**
     * Configures this model.
     *
     * @param array $config
     */
    protected static function configure($config = [])
    {
        $config['db_table'] = 'semester_data';

        $config['additional_fields']['first_sem_week']['get'] = 'getFirstSemesterWeek';
        $config['additional_fields']['last_sem_week']['get'] = 'getLastSemesterWeek';
        $config['additional_fields']['current']['get'] = 'isCurrent';
        $config['additional_fields']['past']['get'] = 'isPast';

        $config['additional_fields']['absolute_seminars_count'] = [
            'get' => 'seminarCounter',
            'set' => false,
        ];
        $config['additional_fields']['duration_seminars_count'] = [
            'get' => 'seminarCounter',
            'set' => false,
        ];
        $config['additional_fields']['continuous_seminars_count'] = [
            'get' => 'seminarCounter',
            'set' => false,
        ];

        $config['alias_fields']['token'] = 'semester_token';

        $config['registered_callbacks']['after_store'][] = 'refreshCache';
        $config['registered_callbacks']['after_delete'][] = 'refreshCache';

        $config['i18n_fields']['name'] = true;
        $config['i18n_fields']['description'] = true;
        $config['i18n_fields']['semester_token'] = true;

        parent::configure($config);
    }

    /**
     * cache
     */
    private static $semester_cache;
    private static $current_semester;


    /**
     * returns semester object for given id or null
     * @param string $id
     * @return NULL|Semester
     */
    public static function find($id)
    {
        $semester_cache = self::getAll();
        return $semester_cache[$id] ?: null;
    }

    /**
     * returns Semester for given timestamp
     * @param integer $timestamp
     * @return null|Semester
     */
    public static function findByTimestamp($timestamp)
    {
        foreach (self::getAll() as $semester) {
            if ($timestamp >= $semester->beginn && $timestamp <= $semester->ende) {
                return $semester;
            }
        }
        return null;
    }

    /**
     * returns following Semester for given timestamp
     * @param integer $timestamp
     * @return null|Semester
     */
    public static function findNext($timestamp = null)
    {
        $timestamp = $timestamp ?: time();
        $semester = self::findByTimestamp($timestamp);
        if ($semester) {
            return self::findByTimestamp((int)$semester->ende + 1);
        }

        return null;
    }

    /**
     * returns current Semester
     */
    public static function findCurrent()
    {
        self::getAll();
        return self::$current_semester;
    }

    /**
     * Return a specially orderd array of all semesters
     */
    public static function findAllVisible($with_before_first = true): array
    {
        return array_values(
            array_filter(self::getAllAsArray(), function ($semester, $key) use ($with_before_first) {
                return $GLOBALS['perm']->have_perm('admin')
                    || !empty($semester['visible'])
                    || ((int)$key === 0 && $with_before_first);
            }, ARRAY_FILTER_USE_BOTH)
        );
    }

    /**
     * returns array of all existing semester objects
     * orderd by begin
     * @param boolean $force_reload
     * @return array
     */
    public static function getAll($force_reload = false)
    {
        if (!is_array(self::$semester_cache) || $force_reload) {
            self::$semester_cache = [];
            if (!$force_reload) {
                $cache = StudipCacheFactory::getCache();
                $semester_data_array = unserialize($cache->read('DB_SEMESTER_DATA'));
                if ($semester_data_array) {
                    foreach ($semester_data_array as $semester_data) {
                        $semester = self::buildExisting($semester_data);
                        self::$semester_cache[$semester->getId()] = $semester;
                        if ($semester->isCurrent()) {
                            self::$current_semester = $semester;
                        }
                    }
                }
            }
            if (!count(self::$semester_cache)) {
                $semester_data = [];
                foreach (self::findBySql('1 ORDER BY beginn') as $semester) {
                    self::$semester_cache[$semester->getId()] = $semester;
                    if ($semester->isCurrent()) {
                        self::$current_semester = $semester;
                    }
                    $semester_data[] = $semester->toRawArray();
                }
                $cache = StudipCacheFactory::getCache();
                $cache->write('DB_SEMESTER_DATA', serialize($semester_data));
            }
        }
        return self::$semester_cache;
    }

    /**
     * Returns a list of all semesters as array, optionally with an entry at
     * the beginning that represents the time before the first semester in the
     * system.
     *
     * @param boolean $with_before_first Show the optional first entry as described above
     * @param boolean $force_reload
     * @return array
     */
    public static function getAllAsArray($with_before_first = true, $force_reload = false)
    {
        $result = array_map(function ($semester) {
            return $semester->toArray();
        }, self::getAll($force_reload));
        $result = array_values($result);

        if ($with_before_first) {
            array_unshift($result, [
                'name' => _('abgelaufene Semester'),
                'past' => true,
            ]);
        }

        return $result;
    }

    /**
     * returns the index for a given semester id, in an array returned from self::getAllAsArray(), beware of second parameter
     *
     * @param $semester_id
     * @param bool $with_before_first
     * @param bool $only_visible
     * @return bool|int
     * @deprecated ASK YOURSELF WHAT THE F!!! YOU ARE DOING
     */
    public static function getIndexById($semester_id, $with_before_first = true, $only_visible = false)
    {
        if($only_visible) {
            $semesters = self::findAllVisible($with_before_first);
        } else {
            $semesters = self::getAllAsArray($with_before_first);
        }
        foreach ($semesters as $index => $semester) {
            if (@$semester['semester_id'] === $semester_id) {
                return $index;
            }
        }
        return false;
    }

    /**
     * Returns an html fragment with a semester select-box
     *
     * @param array $select_attributes
     * @param integer $default
     * @param string $option_value
     * @param boolean $include_all
     * @param boolean $use_semester_id
     * @return string
     */
    public static function getSemesterSelector(
        $select_attributes = null,
        $default = 0,
        $option_value = 'semester_id',
        $include_all = true,
        $use_semester_id = true
    )
    {
        $semester = Semester::findAllVisible();

        unset($semester[0]);

        if ($include_all) {
            $semester['all'] = [
                'name' => _('alle'),
                'semester_id' => 0
            ];
        }
        $semester = array_reverse($semester, true);

        if (!$select_attributes['name']) {
            $select_attributes['name'] = 'sem_select';
        }

        $template = $GLOBALS['template_factory']->open('shared/semester-selector');
        $template->semesters = $semester;
        $template->select_attributes = $select_attributes;
        $template->default = $default;
        $template->option_value = $option_value;
        $template->use_semester_id = $use_semester_id;
        return $template->render();
    }

    /**
     * Caches seminar counts
     */
    protected $seminar_counts = null;

    /**
     * Counts the number of different seminar types in this semester.
     * This method caches the result in $seminar_counts so the db
     * will only be queried once per semester.
     *
     * @param String $field Name of the seminar (/additional_fields) type
     * @return int The count of seminars of this type
     */
    protected function seminarCounter($field)
    {
        if ($this->seminar_counts === null) {
            $query = "
                SELECT SUM(IF(semester_courses.semester_id IS NULL, 1, 0)) AS continuous,
                       0 AS duration,
                       SUM(IF(semester_courses.semester_id IS NOT NULL, 1, 0)) AS absolute
                FROM seminare
                    LEFT JOIN semester_courses ON (seminare.Seminar_id = semester_courses.course_id)
                WHERE start_time <= :beginn
                    AND (semester_courses.semester_id IS NULL OR semester_courses.semester_id = :semester_id)
            ";
            $statement = DBManager::get()->prepare($query);
            $statement->bindValue(':beginn', $this['beginn']);
            $statement->bindValue(':semester_id', $this['semester_id']);
            $statement->execute();
            $this->seminar_counts = $statement->fetch(PDO::FETCH_ASSOC);
        }

        $index = str_replace('_seminars_count', '', $field);
        return (int)$this->seminar_counts[$index];
    }

    /**
     * Returns the calendar week number of the first week of the lecture
     * period.
     *
     * @return int Calendar week number of the first week of lecture
     */
    public function getFirstSemesterWeek()
    {
        return (int)strftime('%W', $this['vorles_beginn']);
    }

    /**
     * Returns the calendar week number of the last week of the lecture
     * period.
     *
     * @return int Calendar week number of the last week of lecture
     */
    public function getLastSemesterWeek()
    {
        return (int)strftime('%W', $this['vorles_ende']);
    }

    /**
     * Return whether this semester is in the past.
     *
     * @return bool Indicating whether this semester is in the past
     */
    public function isPast()
    {
        return $this->ende < time();
    }

    /**
     * Returns whether this semester is the current semester.
     *
     * @return bool Indicating if this is the current semester
     */
    public function isCurrent()
    {
        return time() >= $this->beginn && time() < $this->ende;
    }

    /**
     * Returns the start week dates for this semester (and other
     * semesters if $end_semester is given).
     *
     * @param Semester $end_semester end semester, default is $this
     * @return array containing the start weeks
     */
    public function getStartWeeks(?Semester $end_semester = null)
    {
        if (!$end_semester) {
            $end_semester = $this;
        }

        $timestamp = $this->getCorrectedLectureBegin();
        $end_date = $end_semester->vorles_ende;

        $i = 0;

        $start_weeks = [];
        while ($timestamp < $end_date) {
            $start_weeks[$i] = sprintf(
                _('%u. Semesterwoche (ab %s)'),
                $i + 1,
                strftime('%x', $timestamp));

            $i += 1;

            $timestamp = strtotime('+1 week', $timestamp);
        }

        return $start_weeks;
    }

    /**
     * Returns the corrected begin of lectures which ensures that the begin
     * is always on a monday.
     *
     * @return int unix timestamp of correct begin of lectures
     */
    public function getCorrectedLectureBegin()
    {
        $dow = (int)date('w', $this->vorles_beginn);

        // Date is already on a monday
        if ($dow === 1) {
            return $this->vorles_beginn;
        }

        // Saturday or sunday: return next monday
        if ($dow === 0 || $dow === 6) {
            return strtotime('next monday', $this->vorles_beginn);
        }

        // Otherwise return last monday
        return strtotime('last monday', $this->vorles_beginn);
    }


    /**
     * returns "Semesterwoche" for a given timestamp
     * @param integer $timestamp
     * @return number|boolean
     */
    public function getSemWeekNumber($timestamp)
    {
        $current_sem_week = (int)strftime('%W', $timestamp);
        if (strftime('%Y', $timestamp) > strftime('%Y', $this->vorles_beginn)) {
            $current_sem_week += 52;
        }
        if ($this->last_sem_week < $this->first_sem_week) {
            $last_sem_week = (int)$this->last_sem_week + 52;
        } else {
            $last_sem_week = $this->last_sem_week;
        }
        if ($current_sem_week >= $this->first_sem_week && $current_sem_week <= $last_sem_week) {
            return $current_sem_week - $this->first_sem_week + 1;
        }

        return false;
    }

    /**
     * Returns an array representation of this semester.
     *
     * @param mixed $only_these_fields List of fields to extract
     * @return array represenation
     */
    public function toArray($only_these_fields = null)
    {
        if (!isset($only_these_fields)) {
            $fields = array_flip(array_diff($this->known_slots, array_keys($this->relations)));
            unset($fields['absolute_seminars_count']);
            unset($fields['duration_seminars_count']);
            unset($fields['continuous_seminars_count']);
            $only_these_fields = array_flip($fields);
        }
        return parent::toArray($only_these_fields);
    }

    /**
     * Flushes the cache just after storing and deleting a semester
     */
    public function refreshCache()
    {
        StudipCacheFactory::getCache()->expire('DB_SEMESTER_DATA');
    }
}