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
|
<?php
/**
* 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 Rasmus Fuhse <fuhse@data-quest.de>
* @copyright 2014 Stud.IP Core-Group
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*
* @property string $id alias column for termin_id
* @property string $termin_id database column
* @property string $range_id database column
* @property string $autor_id database column
* @property string $content database column
* @property int $date database column
* @property int $end_time database column
* @property int $mkdate database column
* @property int $chdate database column
* @property int $date_typ database column
* @property string|null $raum database column
* @property string|null $metadate_id database column
* @property string $resource_id database column
* @property User $author belongs_to User
* @property Course $course belongs_to Course
* @property SeminarCycleDate|null $cycle belongs_to SeminarCycleDate
* @property-read mixed $topics additional field
* @property-read mixed $statusgruppen additional field
* @property-read mixed $dozenten additional field
* @property-read mixed $room_booking additional field
* @property-read mixed $room_request additional field
*/
class CourseExDate extends SimpleORMap implements PrivacyObject, Event
{
/**
* Configures this model.
*
* @param Array $config Configuration array
*/
protected static function configure($config = [])
{
$config['db_table'] = 'ex_termine';
$config['belongs_to']['author'] = [
'class_name' => User::class,
'foreign_key' => 'autor_id'
];
$config['belongs_to']['course'] = [
'class_name' => Course::class,
'foreign_key' => 'range_id'
];
$config['belongs_to']['cycle'] = [
'class_name' => SeminarCycleDate::class,
'foreign_key' => 'metadate_id'
];
$dummy_relation = function () { return new SimpleCollection(); };
$dummy_null = function () { return null; };
$config['additional_fields']['topics']['get'] = $dummy_relation;
$config['additional_fields']['statusgruppen']['get'] = $dummy_relation;
$config['additional_fields']['dozenten']['get'] = $dummy_relation;
$config['additional_fields']['room_booking']['get'] = $dummy_null;
$config['additional_fields']['room_request']['get'] = $dummy_null;
$config['default_values']['date_typ'] = 1;
parent::configure($config);
}
const FORMAT_DEFAULT = 'default';
const FORMAT_VERBOSE = 'verbose';
/**
* Returns course dates by course id
*
* @param String $seminar_id Id of the course
* @return array with the associated dates
*/
public static function findBySeminar_id($seminar_id)
{
return self::findByRange_id($seminar_id);
}
/**
* Return course dates by range id (which is in many cases the course id)
*
* @param String $seminar_id Id of the course
* @param String $order_by Optional order definition
* @return array with the associated dates
*/
public static function findByRange_id($seminar_id, $order_by = 'ORDER BY date')
{
return parent::findByRange_id($seminar_id, $order_by);
}
/**
* Returns the name of the assigned room for this date.
*
* @return String that is always empty
*/
public function getRoomName()
{
return '';
}
/**
* Returns the assigned room for this date as an object.
*
* @return null. always. canceled dates need no room.
*/
public function getRoom()
{
return null;
}
/**
* Returns the name of the type of this date.
*
* @param String containing the type name
*/
public function getTypeName()
{
return $GLOBALS['TERMIN_TYP'][$this->date_typ]['name'];
}
/**
* Returns the full qualified name of this date.
*
* @param String $format Optional format type (only 'default' and
* 'verbose' are supported by now)
* @return String containing the full name of this date.
*/
public function getFullName($format = 'default')
{
if (!$this->date || !in_array($format, ['default', 'verbose'])) {
return '';
}
$latter_template = $format === 'verbose'
? _('%R Uhr')
: '%R';
if (($this->end_time - $this->date) / 60 / 60 > 23) {
return strftime('%a., %x' . ' (' . _('ganztägig') . ')' , $this->date) . " (" . _("fällt aus") . ")";
}
return strftime('%a., %x, %R', $this->date) . ' - '
. strftime($latter_template, $this->end_time)
. ' (' . _('fällt aus') . ')';
}
/**
* Converts a CourseExDate Entry to a CourseDate Entry
* returns instance of the new CourseDate or NULL
* @return Object CourseDate
*/
public function unCancelDate()
{
//NOTE: If you modify this method make sure the changes
//are also inserted in SingleDateDB::storeSingleDate
//and CourseDate::cancelDate to keep the behavior consistent
//across Stud.IP!
//These statements are used below to update the relations
//of this ex-date.
$db = DBManager::get();
$groups_stmt = $db->prepare(
"UPDATE termin_related_groups
SET termin_id = :termin_id
WHERE termin_id = :ex_termin_id;"
);
$persons_stmt = $db->prepare(
"UPDATE termin_related_persons
SET range_id = :termin_id
WHERE range_id = :ex_termin_id;"
);
$ex_date = $this->toArray();
//REMOVE content
unset($ex_date['content']);
$date = new CourseDate();
$date->setData($ex_date);
$date->setId($date->getNewId());
if ($date->store()) {
//Update the relations to the ex-date so that they
//use the ID of the new date.
$groups_stmt->execute(
[
'termin_id' => $date->id,
'ex_termin_id' => $this->id
]
);
$persons_stmt->execute(
[
'termin_id' => $date->id,
'ex_termin_id' => $this->id
]
);
//After we updated the relations so that they refer to the
//new date we can delete this ex-date and return the date:
StudipLog::log('SEM_UNDELETE_SINGLEDATE', $this->termin_id, $this->range_id, 'Cycle_id: ' . $this->metadate_id);
$this->delete();
return $date;
}
return null;
}
/**
* Export available data of a given user into a storage object
* (an instance of the StoredUserData class) for that user.
*
* @param StoredUserData $storage object to store data into
*/
public static function exportUserData(StoredUserData $storage)
{
$sorm = self::findBySQL("autor_id = ?", [$storage->user_id]);
if ($sorm) {
$field_data = [];
foreach ($sorm as $row) {
$field_data[] = $row->toRawArray();
}
if ($field_data) {
$storage->addTabularData(_('ausgefallende Termine'), 'ex_termine', $field_data);
}
}
}
/**
* @return string A string representation of the course date.
*/
public function __toString() : string
{
return sprintf(
_('Ausgefallener Termin am %1$s, %2$s von %3$s bis %4$s Uhr'),
strftime('%A', $this->date),
strftime('%x', $this->date),
date('H:i', $this->date),
date('H:i', $this->end_time)
);
}
//Start of Event interface implementation.
public static function getEvents(DateTime $begin, DateTime $end, string $range_id): array
{
return self::findBySQL(
"JOIN `seminar_user`
ON `seminar_user`.`seminar_id` = `ex_termine`.`range_id`
WHERE `seminar_user`.`user_id` = :user_id
AND `ex_termine`.`date` BETWEEN :begin AND :end
AND (
IFNULL(`ex_termine`.`metadate_id`, '') = ''
OR `ex_termine`.`metadate_id` NOT IN (
SELECT `metadate_id`
FROM `schedule_seminare`
WHERE `user_id` = :user_id
AND `visible` = 0
)
)
ORDER BY date",
[
'begin' => $begin->getTimestamp(),
'end' => $end->getTimestamp(),
'user_id' => $range_id
]
);
}
//Event interface implementation:
public function getObjectId() : string
{
return (string) $this->id;
}
public function getPrimaryObjectID(): string
{
return $this->range_id;
}
public function getObjectClass(): string
{
return static::class;
}
public function getTitle(): string
{
return sprintf('%s (fällt aus)', $this->course->name ?? '');
}
public function getBegin(): DateTime
{
$begin = new DateTime();
$begin->setTimestamp($this->date);
return $begin;
}
public function getEnd(): DateTime
{
$end = new DateTime();
$end->setTimestamp($this->end_time);
return $end;
}
public function getDuration(): DateInterval
{
$begin = $this->getBegin();
$end = $this->getEnd();
return $end->diff($begin);
}
public function getLocation(): string
{
return '';
}
public function getUniqueId(): string
{
return sprintf('Stud.IP-SEM-%1$s@%2$s', $this->id, $_SERVER['SERVER_NAME']);
}
public function getDescription(): string
{
return trim($this->getValue('content'));
}
public function getAdditionalDescriptions(): array
{
$descriptions = [];
if (count($this->dozenten) > 0) {
$descriptions[_('Durchführende Lehrende')] = implode(', ', $this->dozenten->getFullName());
}
if (count($this->statusgruppen) > 0) {
$descriptions[_('Beteiligte Gruppen')] = implode(', ', $this->statusgruppen->getValue('name'));
}
return $descriptions;
}
public function isAllDayEvent(): bool
{
//Course dates are never all day events.
return false;
}
public function isWritable(string $user_id): bool
{
return $GLOBALS['perm']->have_studip_perm('dozent', $this->range_id, $user_id);
}
public function getCreationDate(): DateTime
{
$mkdate = new DateTime();
$mkdate->setTimestamp($this->mkdate);
return $mkdate;
}
public function getModificationDate(): DateTime
{
$chdate = new DateTime();
$chdate->setTimestamp($this->chdate);
return $chdate;
}
public function getImportDate(): DateTime
{
return $this->getCreationDate();
}
public function getAuthor(): ?User
{
return $this->author;
}
public function getEditor(): ?User
{
return null;
}
public function toEventData(string $user_id): \Studip\Calendar\EventData
{
$begin = new DateTime();
$begin->setTimestamp($this->date);
$end = new DateTime();
$end->setTimestamp($this->end_time);
$studip_view_urls = [];
if ($GLOBALS['perm']->have_studip_perm('user', $this->range_id, $user_id)) {
$studip_view_urls['show'] = URLHelper::getURL('dispatch.php/course/details', ['cid' => $this->range_id, 'link_to_course' => '1']);
}
return new \Studip\Calendar\EventData(
$begin,
$end,
$this->getTitle(),
[],
'#000000',
'#aaaaaa',
$this->isWritable($user_id),
CourseExDate::class,
$this->id,
Course::class,
$this->range_id,
'course',
$this->range_id,
$studip_view_urls,
[],
'seminar',
'rgba(0,0,0,0)'
);
}
//End of Event interface implementation.
}
|