diff options
author | MaZderMind | 2015-03-30 21:18:42 +0200 |
---|---|---|
committer | MaZderMind | 2015-03-30 21:18:42 +0200 |
commit | 785e40eae22ab50c48cde894663fc6f70187bb2d (patch) | |
tree | 84b1a96584dfda868a1f9db9b04438ef906e7809 | |
parent | fc4cf5793cbe6561347067eb7b25c60a6fc50f77 (diff) |
MVT Implementation of the schedule
-rw-r--r-- | lib/Exceptions.php | 1 | ||||
-rw-r--r-- | model/Room.php | 4 | ||||
-rw-r--r-- | model/Schedule.php | 124 |
3 files changed, 98 insertions, 31 deletions
diff --git a/lib/Exceptions.php b/lib/Exceptions.php index d5a4600..f6f9d24 100644 --- a/lib/Exceptions.php +++ b/lib/Exceptions.php @@ -1,3 +1,4 @@ <?php class NotFoundException extends Exception {} +class ScheduleException extends Exception {} diff --git a/model/Room.php b/model/Room.php index f428d87..156576c 100644 --- a/model/Room.php +++ b/model/Room.php @@ -29,6 +29,10 @@ class Room extends ModelBase return $this->get('ROOMS.'.$this->getSlug().'.STREAM', $this->getSlug()); } + public function getScheduleName() { + return $this->get('ROOMS.'.$this->getSlug().'.SCHEDULE_NAME', $this->getSlug()); + } + public function getDisplay() { return $this->get('ROOMS.'.$this->getSlug().'.DISPLAY', $this->getSlug()); } diff --git a/model/Schedule.php b/model/Schedule.php index ca4881d..e6aef7c 100644 --- a/model/Schedule.php +++ b/model/Schedule.php @@ -2,29 +2,20 @@ class Schedule extends ModelBase { + public function isEnabled() { + return $this->has('SCHEDULE'); + } + public function getSimulationOffset() { return $this->get('SCHEDULE.SIMULATE_OFFSET', 0); } - private function strtoduration($str) - { - $parts = explode(':', $str); - return ((int)$parts[0] * 60 + (int)$parts[1]) * 60; + public function getScale() { + return floatval(get('SCHEDULE.SCALE', 7)); } - function program() + private function fetchSchedule() { - if(!has('SCHEDULE')) - return; - - if(has('SCHEDULE.CACHE') && function_exists('apc_fetch')) - { - $program = apc_fetch('SCHEDULE.CACHE'); - if($program) return $program; - } - - - $program = array(); $opts = array( 'http' => array( 'timeout' => 2, @@ -32,13 +23,24 @@ class Schedule extends ModelBase ) ); $context = stream_context_create($opts); - $schedule = file_get_contents(get('SCHEDULE.URL'), false, $context); + $schedule = file_get_contents($this->getScheduleUrl(), false, $context); - // failed, give up if(!$schedule) - return array(); + throw new ScheduleException("Error Downloading Schedule from ".$this->getScheduleUrl()); + + return simplexml_load_string($schedule); + } - $schedule = 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 @@ -54,7 +56,7 @@ class Schedule extends ModelBase foreach($room->event as $event) { $start = strtotime((string)$event->date); - $duration = strtoduration((string)$event->duration); + $duration = $this->strToDuration((string)$event->duration); $end = $start + $duration; $daystart = min($daystart, $start); @@ -78,14 +80,15 @@ class Schedule extends ModelBase { $roomidx++; $lastend = false; + $name = (string)$room['name']; - if(isset($GLOBALS['CONFIG']['FAHRPLAN_ROOM_MAPPING'][$name])) - $name = $GLOBALS['CONFIG']['FAHRPLAN_ROOM_MAPPING'][$name]; + if(isset($mapping[$name])) + $name = $mapping[$name]; foreach($room->event as $event) { $start = strtotime((string)$event->date); - $duration = strtoduration((string)$event->duration); + $duration = $this->strToDuration((string)$event->duration); $end = $start + $duration; if($lastend && $lastend < $start) @@ -167,15 +170,74 @@ class Schedule extends ModelBase } } - if(has('SCHEDULE.CACHE') && function_exists('apc_store')) + return $this->doCache($program); + } + + + public function getDurationSum() + { + $sum = 0; + foreach(reset($this->getSchedule()) 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 get('SCHEDULE.URL'); + } + + private function isCacheEnabled() + { + return has('SCHEDULE.CACHE') && function_exists('apc_fetch') && function_exists('apc_store'); + } + + private function getCacheDuration() + { + return 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('SCHEDULE.CACHE'); + } + + private function doCache($value) + { + $this->localCache = $value; + + if(!$this->isCacheEnabled()) + return $value; + + apc_store('SCHEDULE.CACHE', $value, $this->getCacheDuration()); + return $value; + } + + private function getScheduleToRoomSlugMapping() + { + $mapping = array(); + foreach($this->get('ROOMS') as $slug => $room) { - apc_store( - 'SCHEDULE.CACHE', - $program, - get('SCHEDULE.CACHE') - ); + if(isset($room['SCHEDULE_NAME'])) + $mapping[ $room['SCHEDULE_NAME'] ] = $slug; } - return $program; + return $mapping; } } |