streaming-website/model/Schedule.php

266 lines
5.6 KiB
PHP
Raw Normal View History

<?php
class Schedule extends ModelBase
{
2015-03-30 19:18:42 +00:00
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);
}
2015-03-30 19:18:42 +00:00
public function getScale() {
2015-03-31 15:16:40 +00:00
return floatval($this->get('SCHEDULE.SCALE', 7));
}
2015-03-30 19:18:42 +00:00
private function fetchSchedule()
{
$opts = array(
'http' => array(
'timeout' => 2,
'user_agent' => 'C3Voc Universal Streaming-Website Backend @ '.$_SERVER['HTTP_HOST'],
)
);
$context = stream_context_create($opts);
2015-03-30 19:18:42 +00:00
$schedule = file_get_contents($this->getScheduleUrl(), false, $context);
if(!$schedule)
2015-03-30 19:18:42 +00:00
throw new ScheduleException("Error Downloading Schedule from ".$this->getScheduleUrl());
return simplexml_load_string($schedule);
}
2015-03-30 19:18:42 +00:00
public function getSchedule()
{
if($schedule = $this->getCached())
return $schedule;
// download schedule-xml
$schedule = $this->fetchSchedule();
2015-03-30 19:18:42 +00:00
$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);
2015-03-30 19:18:42 +00:00
$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;
2015-03-30 19:18:42 +00:00
$name = (string)$room['name'];
if($this->isRoomFiltered($name))
continue;
2015-03-30 19:18:42 +00:00
if(isset($mapping[$name]))
$name = $mapping[$name];
foreach($room->event as $event)
{
$start = strtotime((string)$event->date);
2015-03-30 19:18:42 +00:00
$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,
);
}
}
}
2015-03-30 19:18:42 +00:00
return $this->doCache($program);
}
public function getDurationSum()
{
$sum = 0;
$schedule = $this->getSchedule();
foreach(reset($schedule) as $event)
2015-03-30 19:18:42 +00:00
$sum += $event['duration'];
return $sum;
}
private function strToDuration($str)
{
$parts = explode(':', $str);
return ((int)$parts[0] * 60 + (int)$parts[1]) * 60;
}
private function getScheduleUrl()
{
2015-04-01 19:04:00 +00:00
return $this->get('SCHEDULE.URL');
2015-03-30 19:18:42 +00:00
}
private function isCacheEnabled()
{
2015-03-31 15:16:40 +00:00
return $this->has('SCHEDULE.CACHE') && function_exists('apc_fetch') && function_exists('apc_store');
2015-03-30 19:18:42 +00:00
}
private function getCacheDuration()
{
2015-03-31 15:16:40 +00:00
return $this->get('SCHEDULE.CACHE', 60*10 /* 10 minutes */);
2015-03-30 19:18:42 +00:00
}
private $localCache = null;
private function getCached()
{
if($this->localCache)
return $this->localCache;
if(!$this->isCacheEnabled())
return null;
2015-04-01 19:03:51 +00:00
return apc_fetch($this->getCacheKey());
2015-03-30 19:18:42 +00:00
}
private function doCache($value)
{
$this->localCache = $value;
if(!$this->isCacheEnabled())
return $value;
2015-04-01 19:03:51 +00:00
apc_store($this->getCacheKey(), $value, $this->getCacheDuration());
2015-03-30 19:18:42 +00:00
return $value;
}
2015-04-01 19:03:51 +00:00
private function getCacheKey()
{
return 'SCHEDULE.'.$this->getScheduleUrl();
}
2015-03-30 19:18:42 +00:00
private function getScheduleToRoomSlugMapping()
{
$mapping = array();
foreach($this->get('ROOMS') as $slug => $room)
{
2015-03-30 19:18:42 +00:00
if(isset($room['SCHEDULE_NAME']))
$mapping[ $room['SCHEDULE_NAME'] ] = $slug;
}
2015-03-30 19:18:42 +00:00
return $mapping;
}
}