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
|
<?php
class Schedule extends ModelBase
{
public function isEnabled() {
return $this->has('SCHEDULE');
}
private function isRoomFiltered($room)
{
if(!$this->has('SCHEDULE.ROOMFILTER'))
return false;
$rooms = $this->get('SCHEDULE.ROOMFILTER');
return !in_array($room, $rooms);
}
public function getSimulationOffset() {
return $this->get('SCHEDULE.SIMULATE_OFFSET', 0);
}
public function getScale() {
return floatval($this->get('SCHEDULE.SCALE', 7));
}
private function fetchSchedule()
{
$opts = array(
'http' => array(
'timeout' => 2,
'user_agent' => 'C3Voc Universal Streaming-Website Backend @ '.$_SERVER['HTTP_HOST'],
)
);
$context = stream_context_create($opts);
$schedule = file_get_contents($this->getScheduleUrl(), false, $context);
if(!$schedule)
throw new ScheduleException("Error Downloading Schedule from ".$this->getScheduleUrl());
return simplexml_load_string($schedule);
}
public function getSchedule()
{
if($schedule = $this->getCached())
return $schedule;
// download schedule-xml
$schedule = $this->fetchSchedule();
$mapping = $this->getScheduleToRoomSlugMapping();
$program = array();
// 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 = PHP_INT_MAX;
$dayend = 0;
foreach($day->room as $room)
{
$name = (string)$room['name'];
if($this->isRoomFiltered($name))
continue;
foreach($room->event as $event)
{
$start = strtotime((string)$event->date);
$duration = $this->strToDuration((string)$event->duration);
$end = $start + $duration;
$daystart = min($daystart, $start);
$dayend = max($dayend, $end);
}
}
$day['start'] = $daystart;
$day['end'] = $dayend;
}
$dayidx = 0;
foreach($schedule->day as $day)
{
$dayidx++;
$daystart = (int)$day['start'];
$dayend = (int)$day['end'];
$roomidx = 0;
foreach($day->room as $room)
{
$roomidx++;
$lastend = false;
$name = (string)$room['name'];
if($this->isRoomFiltered($name))
continue;
if(isset($mapping[$name]))
$name = $mapping[$name];
foreach($room->event as $event)
{
$start = strtotime((string)$event->date);
$duration = $this->strToDuration((string)$event->duration);
$end = $start + $duration;
if($lastend && $lastend < $start)
{
// synthesize pause event
$pauseduration = $start - $lastend;
$program[$name][] = array(
'special' => 'pause',
'title' => round($pauseduration / 60).' minutes pause',
'fstart' => date('c', $lastend),
'fend' => date('c', $start),
'start' => $lastend,
'end' => $start,
'duration' => $pauseduration,
);
}
else if(!$lastend && $daystart < $start)
{
$program[$name][] = array(
'special' => 'gap',
'fstart' => date('c', $daystart),
'fend' => date('c', $start),
'start' => $daystart,
'end' => $start,
'duration' => $start - $daystart,
);
}
$personnames = array();
foreach($event->persons->person as $person)
$personnames[] = (string)$person;
$program[$name][] = array(
'title' => (string)$event->title,
'speaker' => implode(', ', $personnames),
'fstart' => date('c', $start),
'fend' => date('c', $end),
'start' => $start,
'end' => $end,
'duration' => $duration,
);
$lastend = $end;
}
// synthesize daychange event
if(!$lastend) $lastend = $daystart;
if($lastend < $dayend)
{
$program[$name][] = array(
'special' => 'gap',
'fstart' => date('c', $lastend),
'fend' => date('c', $dayend),
'start' => $lastend,
'end' => $dayend,
'duration' => $dayend - $lastend,
);
}
if($dayidx < count($schedule->day))
{
$program[$name][] = array(
'special' => 'daychange',
'title' => 'Daychange from Day '.$dayidx.' to '.($dayidx+1),
'start' => $dayend,
'end' => (int)$schedule->day[$dayidx]['start'],
'duration' => 60*60,
);
}
}
}
return $this->doCache($program);
}
public function getDurationSum()
{
$sum = 0;
$schedule = $this->getSchedule();
foreach(reset($schedule) as $event)
$sum += $event['duration'];
return $sum;
}
private function strToDuration($str)
{
$parts = explode(':', $str);
return ((int)$parts[0] * 60 + (int)$parts[1]) * 60;
}
private function getScheduleUrl()
{
return $this->get('SCHEDULE.URL');
}
private function isCacheEnabled()
{
return $this->has('SCHEDULE.CACHE') && function_exists('apc_fetch') && function_exists('apc_store');
}
private function getCacheDuration()
{
return $this->get('SCHEDULE.CACHE', 60*10 /* 10 minutes */);
}
private $localCache = null;
private function getCached()
{
if($this->localCache)
return $this->localCache;
if(!$this->isCacheEnabled())
return null;
return apc_fetch($this->getCacheKey());
}
private function doCache($value)
{
$this->localCache = $value;
if(!$this->isCacheEnabled())
return $value;
apc_store($this->getCacheKey(), $value, $this->getCacheDuration());
return $value;
}
private function getCacheKey()
{
return 'SCHEDULE.'.$this->getScheduleUrl();
}
private function getScheduleToRoomSlugMapping()
{
$mapping = array();
foreach($this->get('ROOMS') as $slug => $room)
{
if(isset($room['SCHEDULE_NAME']))
$mapping[ $room['SCHEDULE_NAME'] ] = $slug;
}
return $mapping;
}
}
|