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
|
<?php
/*
* Copyright (c) 2012 Rasmus Fuhse <fuhse@data-quest.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.
*/
if (isset($GLOBALS['SEM_TYPE'])) {
$GLOBALS['SEM_TYPE_OLD_VAR'] = $GLOBALS['SEM_TYPE'];
}
/**
* Class to define and manage attributes of seminar types.
* Usually all sem-types are stored in a global variable $SEM_TYPE which is
* an array of SemType objects.
*
* SemType::getTypes() gets you all seminar types in an array.
*
* This class only represents the name of the type and gives a relation to a
* sem_class.
*/
class SemType implements ArrayAccess
{
protected $data = [];
static protected $sem_types = null;
/**
* Constructor can be set with integer of sem_class_id or an array of
* the old $SEM_CLASS style.
* @param integer | array $data
*/
public function __construct($data) {
$db = DBManager::get();
if (is_int($data)) {
$statement = $db->prepare("SELECT * FROM sem_types WHERE id = :id ");
$statement->execute(['id' => $data]);
$this->data = $statement->fetch(PDO::FETCH_ASSOC);
} else {
$this->data = $data;
}
}
/**
* Returns the number of seminars of this sem_type in Stud.IP
* @return integer
*/
public function countSeminars() {
$db = DBManager::get();
$statement = $db->prepare("SELECT COUNT(*) FROM seminare WHERE status = :sem_type ");
$statement->execute(['sem_type' => $this->data['id']]);
return (int) $statement->fetch(PDO::FETCH_COLUMN, 0);
}
/**
* stores all data in the database
* @return boolean success
*/
public function store() {
$db = DBManager::get();
$statement = $db->prepare(
"UPDATE sem_types " .
"SET name = :name, " .
"class = :class, " .
"chdate = UNIX_TIMESTAMP() " .
"WHERE id = :id ".
"");
StudipCacheFactory::getCache()->expire('DB_SEM_TYPES_ARRAY');
return $statement->execute([
'id' => $this->data['id'],
'name' => $this->data['name'],
'class' => $this->data['class']
]);
}
/**
* Deletes the sem_type-object. Will only delete,
* if there are no seminars in this sem_type.
* Remember to refresh the global $SEM_TYPE array.
* @return boolean : success of deletion
*/
public function delete() {
if ($this->countSeminars() === 0) {
$db = DBManager::get();
$statement = $db->prepare("
DELETE FROM sem_types
WHERE id = :id
");
StudipCacheFactory::getCache()->expire('DB_SEM_TYPES_ARRAY');
return $statement->execute([
'id' => $this->data['id']
]);
} else {
return false;
}
}
/**
* Sets an attribute of sem_type->data
* @param string $offset
* @param mixed $value
*/
public function set($offset, $value) {
$this->data[$offset] = $value;
}
public function getClass() {
return $GLOBALS['SEM_CLASS'][$this->data['class']] ?? SemClass::getDefaultSemClass();
}
/***************************************************************************
* ArrayAccess methods *
***************************************************************************/
/**
* deprecated, does nothing, should not be used
* @param string $offset
* @param mixed $value
*
* @todo Add void return type when Stud.IP requires PHP8 minimal
*/
#[ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
}
/**
* Compatibility function with old $SEM_TYPE variable for plugins. Maps the
* new array-structure to the old boolean values.
* @param integer $offset: name of attribute
* @return boolean|(localized)string
*
* @todo Add mixed return type when Stud.IP requires PHP8 minimal
*/
#[ReturnTypeWillChange]
public function offsetGet($offset)
{
switch ($offset) {
case "name":
return gettext($this->data['name']);
case in_array($offset, ["title_dozent", "title_tutor", "title_autor"]):
$sem_class = $this->getClass();
$title = [$sem_class[$offset], $sem_class[$offset.'_plural']];
return $title[0] || $title[1] ? $title : $this->data[$offset];
}
//ansonsten
return $this->data[$offset];
}
/**
* ArrayAccess method to check if an attribute exists.
* @param mixed $offset
* @return bool
*
* @todo Add bool return type when Stud.IP requires PHP8 minimal
*/
#[ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}
/**
* deprecated, does nothing, should not be used
* @param string $offset
*
* @todo Add void return type when Stud.IP requires PHP8 minimal
*/
#[ReturnTypeWillChange]
public function offsetUnset($offset)
{
}
/***************************************************************************
* static methods *
***************************************************************************/
/**
* Returns an array of all SemTypes in Stud.IP. Equivalent to global
* $SEM_TYPE variable. This variable is statically stored in this class.
* @return array of SemType
*/
static public function getTypes() {
if (!is_array(self::$sem_types)) {
$db = DBManager::get();
self::$sem_types = [];
$cache = StudipCacheFactory::getCache();
$types_array = unserialize($cache->read('DB_SEM_TYPES_ARRAY'));
if (!$types_array) {
try {
$statement = $db->prepare(
"SELECT * FROM sem_types ORDER BY id ASC "
);
$statement->execute();
$types_array = $statement->fetchAll(PDO::FETCH_ASSOC);
if ($types_array) {
$cache = StudipCacheFactory::getCache();
$cache->write('DB_SEM_TYPES_ARRAY', serialize($types_array));
}
} catch (PDOException $e) {
//for use without or before migration 92
$type_array = $GLOBALS['SEM_TYPE_OLD_VAR'];
if (is_array($type_array)) {
ksort($type_array);
foreach ($type_array as $id => $type) {
self::$sem_types[$id] = new SemType($type);
}
} else {
self::$sem_types[1] = new SemType(['name' => 'default', 'class' => 1, 'id' => 1]);
}
}
}
foreach ($types_array as $sem_type) {
self::$sem_types[$sem_type['id']] = new SemType($sem_type);
}
}
return self::$sem_types;
}
static public function refreshTypes() {
self::$sem_types = null;
StudipCacheFactory::getCache()->expire('DB_SEM_TYPES_ARRAY');
return self::getTypes();
}
/**
* Gets all SemTypes that are allowed as group parents.
* @return array
*/
public static function getGroupingSemTypes()
{
return SimpleCollection::createFromArray(array_flatten(SemClass::getGroupClasses()->getSemTypes()))->pluck('id');
}
/**
* Gets all SemTypes that are allowed as group parents.
* @return array
*/
public static function getNonGroupingSemTypes()
{
$non_grouping = SimpleCollection::createFromArray(SemClass::getClasses())->findBy('is_group', false)->findBy('studygroup_mode', false);
return SimpleCollection::createFromArray(array_flatten($non_grouping->getSemTypes()))->pluck('id');
}
/**
* Static method only to keep the translationstrings of the values. It is
* never used within the system.
*/
static private function localization() {
_("Vorlesung");
_("Seminar");
_("Übung");
_("Praktikum");
_("Colloquium");
_("Kolloquium");
_("Forschungsgruppe");
_("sonstige");
_("Gremium");
_("Projektgruppe");
_("Kulturforum");
_("Veranstaltungsboard");
_("Studiengruppe");
}
}
|