blob: ab058e11eb77d7de129c906e006b6aeef6ba85a3 (
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
|
<?php
/**
* SemesterHoliday.php
* model class for table semester_holiday
*
* 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 $id alias column for holiday_id
* @property string $holiday_id database column
* @property string $semester_id database column
* @property string $name database column
* @property string $description database column
* @property int|null $beginn database column
* @property int $ende database column
* @property int|null $mkdate database column
* @property int|null $chdate database column
* @property mixed $current additional field
*/
class SemesterHoliday extends SimpleORMap
{
/**
* Configures this model.
*
* @param Array $config
*/
protected static function configure($config = [])
{
$config['db_table'] = 'semester_holiday';
$config['additional_fields']['current'] = true;
parent::configure($config);
}
/**
* cache
*/
private static $holiday_cache;
/**
* returns SemesterHoliday object for given id or null
* @param string $id
* @return NULL|SemesterHoliday
*/
public static function find($id)
{
$holiday_cache = self::getAll();
return $holiday_cache[$id] ?: null;
}
/**
* returns all SemesterHoliday between given timestamps (starting AND ending within given timestamps)
* @param integer $timestamp_start
* @param integer $timestamp_end
* @return array of SemesterHoliday
*/
public static function findByTimestampRange($timestamp_start, $timestamp_end)
{
$ret = [];
if ($timestamp_start < $timestamp_end) {
foreach(self::getAll() as $holiday) {
if ($holiday->beginn >= $timestamp_start && $holiday->ende <= $timestamp_end) {
$ret[] = $holiday;
}
}
}
return $ret;
}
/**
* returns all SemesterHolidays for given semester
*
* @param mixed semester object or id as string or assoc array
* @return array of SemesterHoliday
*/
public static function findBySemester($semester)
{
$semester = Semester::toObject($semester);
return self::findByTimestampRange($semester->beginn, $semester->ende);
}
/**
* returns array of all existing SemesterHoliday objects
* orderd by begin
* @param boolean $force_reload
* @return array
*/
public static function getAll($force_reload = false)
{
if (!is_array(self::$holiday_cache) || $force_reload) {
self::$holiday_cache = [];
foreach(self::findBySql('1 ORDER BY beginn') as $holiday){
self::$holiday_cache[$holiday->getId()] = $holiday;
}
}
return self::$holiday_cache;
}
/**
* Returns whether we currently have this holidays (yay).
*
* @return bool
*/
public function getcurrent()
{
return $this->beginn < time() && $this->ende > time();
}
/**
* Returns if a given date is a holiday.
*
* @param int $time Timestamp to check
* @param bool $check_vacation_only Defines whether to check only vacation
* times or against all holidays
* @return mixed false if no holiday was found, an array with the name and
* the "col" value of the holiday otherwise
*/
public static function isHoliday($time, $check_vacation_only = true)
{
// Check all defined vaciation times
foreach (SemesterHoliday::getAll() as $val) {
if ($val->beginn <= $time && $val->ende >= $time) {
return [
'name' => $val->name,
'col' => 3,
];
}
}
// Check all other holidays
if (!$check_vacation_only) {
return holiday($time);
}
// Nothing found
return false;
}
}
|