-
Notifications
You must be signed in to change notification settings - Fork 109
/
Schedule.php
382 lines (313 loc) · 9.53 KB
/
Schedule.php
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
<?php
class Schedule
{
private $conference;
private $program;
private $mapping = null;
public function __construct($conference)
{
$this->conference = $conference;
}
public function getConference() {
return $this->conference;
}
public function isEnabled() {
return $this->getConference()->has('SCHEDULE');
}
private function isRoomFiltered($room)
{
if(!$this->getConference()->has('SCHEDULE.ROOMFILTER'))
return false;
$rooms = $this->getConference()->get('SCHEDULE.ROOMFILTER');
return !in_array($room, $rooms);
}
public function getSimulationOffset() {
return $this->getConference()->get('SCHEDULE.SIMULATE_OFFSET', 0);
}
public function getDayIndexOffset() {
return $this->getConference()->get('SCHEDULE.INDEX_OFFSET', 0);
}
public function getScale() {
return floatval($this->getConference()->get('SCHEDULE.SCALE', 7));
}
public function isRoomMapped($scheduleRoom) {
$mapping = $this->getScheduleToRoomSlugMapping();
return isset( $mapping[$scheduleRoom] );
}
public function isOptout($event) {
if (isset($event->recording)) {
return $event->recording->optout == 'true';
}
return false;
}
public function getMappedRoom($scheduleRoom) {
$mapping = $this->getScheduleToRoomSlugMapping();
$room = isset($mapping[$scheduleRoom]) ? $mapping[$scheduleRoom] : "";
return $this->getConference()->getRoomIfExists( $room );
}
public function getScheduleDisplayTime($basetime = null)
{
if(is_null($basetime)) {
$basetime = time();
}
return $basetime + $this->getSimulationOffset();
}
private function fetchSchedule()
{
try {
$schedule = file_get_contents($this->getScheduleCache());
if(!$schedule)
return null;
return simplexml_load_string($schedule);
} catch (ErrorException $e) {
return null;
}
}
public function getRoomSchedule($roomName, $roomGuid) {
// build program, if not realy set
if (!isset($this->program)) {
$this->getSchedule();
}
return $this->program[$roomName];
}
public function getSchedule()
{
// reusue already computed schedule, if set.
if (isset($this->program)) {
return $this->program;
}
// download schedule-xml
$schedule = $this->fetchSchedule();
// not failing gracefully here will result in a broken page in case
// no schedule is present
if(!$schedule)
return [];
$program = array();
$rooms = array();
$day_idx_offset = $this->getDayIndexOffset();
// re-calculate day-ends
// some schedules have long gaps before the first talk or talks that expand beyond the dayend
// (fiffkon, i look at you)
// so to be on the safer side we calculate our own daystart/end here
foreach($schedule->day as $day)
{
$daystart = new DateTimeImmutable('+100 year');
$dayend = new DateTimeImmutable('-100 year');
foreach($day->room as $room)
{
$roomName = (string)$room['name'];
if($this->isRoomFiltered($roomName))
continue;
if(!in_array($roomName, $rooms))
$rooms[] = $roomName;
foreach($room->event as $event)
{
$start = new DateTimeImmutable((string)$event->date);
$interval = $this->strToInterval((string)$event->duration);
$end = $start->add($interval);
$daystart = $start < $daystart ? $start : $daystart;
$dayend = $end > $dayend ? $end : $dayend;
}
}
// stringify again to store in simplexml
$day['start'] = $daystart->format('c');
$day['end'] = $dayend->format('c');
}
$daysSorted = [];
foreach($schedule->day as $day)
{
$daysSorted[] = $day;
}
usort($daysSorted, function($a, $b): int {
return strcmp($a['start'], $b['start']);
});
$dayidx = 0;
foreach($daysSorted as $day)
{
$dayidx++;
$daystart = new DateTimeImmutable($day['start']);
$dayend = new DateTimeImmutable($day['end']);
$roomidx = 0;
foreach($rooms as $roomName)
{
$roomidx++;
$laststart = NULL;
$lastend = NULL;
if($this->isRoomFiltered($roomName))
continue;
$result = $day->xpath("room[@name='".$roomName."']");
// if this room has no events on this day -> add long gap
if(!$result) {
$nextday = $schedule->day[$dayidx];
// but not if it is the last day
if(!$nextday) {
continue;
}
$gap = $this->makeEvent($daystart, $dayend);
$gap['special'] = 'gap';
$program[$roomName][] = $gap;
$end = new DateTimeImmutable($nextday['start']);
$daychange = $this->makeEvent($dayend, $end);
$daychange['special'] = 'daychange';
$daychange['title'] = 'Daychange from Day '.($dayidx+$day_idx_offset).' to '.($dayidx+1+$day_idx_offset);
$daychange['duration'] = 3600;
$program[$roomName][] = $daychange;
continue;
}
$room = $result[0];
$eventsSorted = [];
foreach($room->event as $event)
{
$eventsSorted[] = $event;
}
usort($eventsSorted, function($a, $b) {
$a_start = (string)$a->date;
$b_start = (string)$b->date;
return strcmp($a_start, $b_start);
});
foreach($eventsSorted as $event)
{
$start = new DateTimeImmutable((string)$event->date);
$interval = $this->strToInterval((string)$event->duration);
$end = $start->add($interval);
// skip duplicate events in fahrplan source
if ( $laststart == $start )
continue;
if($lastend && $lastend < $start)
{
// pause between talks
$pause = $this->makeEvent($lastend, $start);
$pause['special'] = 'pause';
$pause['title'] = round($pause['duration'] / 60).' minutes pause';
$pause['room_known'] = $this->isRoomMapped($roomName);
$program[$roomName][] = $pause;
}
else if(!$lastend && $daystart < $start)
{
// gap before first talk
$gap = $this->makeEvent($daystart, $start);
$gap['special'] = 'gap';
$gap['room_known'] = $this->isRoomMapped($roomName);
$program[$roomName][] = $gap;
}
$personnames = array();
if(isset($event->persons)) foreach($event->persons->person as $person)
$personnames[] = (string)$person;
// normal talk
$talk = $this->makeEvent($start, $end);
$talk['guid'] = (string)$event['guid'];
$talk['track'] = (string)$event->track;
$talk['title'] = (string)$event->title;
$talk['speaker'] = implode(', ', $personnames);
$talk['room_known'] = $this->isRoomMapped($roomName);
$talk['optout'] = $this->isOptout($event);
$talk['url'] = (string)$event->url;
$program[$roomName][] = $talk;
$laststart = $start;
$lastend = $end;
}
if(!$lastend) $lastend = $daystart;
if($lastend < $dayend)
{
// gap after last talk
$gap = $this->makeEvent($lastend, $dayend);
$gap['special'] = 'gap';
$program[$roomName][] = $gap;
}
if($dayidx < count($schedule->day))
{
// daychange
$end = new DateTimeImmutable($schedule->day[$dayidx]['start']);
$daychange = $this->makeEvent($dayend, $end);
$daychange['special'] = 'daychange';
$daychange['title'] = 'Daychange from Day '.($dayidx+$day_idx_offset).' to '.($dayidx+1+$day_idx_offset);
$daychange['duration'] = 3600;
$program[$roomName][] = $daychange;
}
}
}
$mapping = $this->getScheduleToRoomSlugMapping();
if($this->getConference()->has('SCHEDULE.ROOMFILTER'))
{
// determine roomfilter
$roomfilter = $this->getConference()->get('SCHEDULE.ROOMFILTER');
// map roomfilter-rooms to room-slugs
$roomfilter = array_map(function($e) use ($mapping) {
if(isset($mapping[$e]))
return $mapping[$e];
return $e;
}, $roomfilter);
// sort according to roomfilter ordering
uksort($program, function($a, $b) use ($roomfilter) {
return array_search($a, $roomfilter) - array_search($b, $roomfilter);
});
}
$this->program = $program;
return $this->program;
}
private function makeEvent(DateTimeImmutable $from, DateTimeImmutable $to): array {
return array(
'fstart' => $from->format('c'),
'fend' => $to->format('c'),
'tstart' => $from->format('H:i'),
'tend' => $to->format('H:i'),
'start' => $from->getTimestamp(),
'end' => $to->getTimestamp(),
'offset' => $from->getOffset(),
'duration' => $to->getTimestamp() - $from->getTimestamp(),
);
}
private function intervalToDuration(DateInterval $interval): int {
$one = new DateTimeImmutable();
$two = $one->add($interval);
return $two->getTimestamp() - $one->getTimestamp();
}
private function strToInterval(string $str): DateInterval
{
$parts = explode(':', $str);
return new DateInterval('PT'.$parts[0].'H'.$parts[1].'M');
}
public function getDurationSum()
{
$sum = 0;
$schedule = $this->getSchedule();
if ($schedule) {
foreach(reset($schedule) as $event) {
$sum += $event['duration'];
}
}
return $sum;
}
public function getScheduleUrl()
{
return $this->getConference()->get('SCHEDULE.URL');
}
public function getScheduleCache()
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
return sprintf('C:\tmp\schedule-cache-%s.xml', $this->getConference()->getSlug());
} else {
return sprintf('/tmp/schedule-cache-%s.xml', $this->getConference()->getSlug());
}
}
public function getScheduleToRoomSlugMapping()
{
if (!$this->mapping) {
$this->mapping = array();
foreach($this->getConference()->get('ROOMS') as $slug => $room)
{
// json has 'name', config.php has 'SCHEDULE_NAME' and 'DISPLAY'
$key = $slug;
if (isset($room['name'])) {
$key = $room['name'];
} elseif (isset($room['SCHEDULE_NAME'])) {
$key = $room['SCHEDULE_NAME'];
} elseif (isset($room['DISPLAY'])) {
$key = $room['DISPLAY'];
}
$this->mapping[$key] = $slug;
}
}
return $this->mapping;
}
}