diff options
Diffstat (limited to 'modules-available/locationinfo/inc/coursebackend.inc.php')
-rw-r--r-- | modules-available/locationinfo/inc/coursebackend.inc.php | 74 |
1 files changed, 56 insertions, 18 deletions
diff --git a/modules-available/locationinfo/inc/coursebackend.inc.php b/modules-available/locationinfo/inc/coursebackend.inc.php index 1fe87202..7162c885 100644 --- a/modules-available/locationinfo/inc/coursebackend.inc.php +++ b/modules-available/locationinfo/inc/coursebackend.inc.php @@ -47,17 +47,20 @@ abstract class CourseBackend foreach (glob(dirname(__FILE__) . '/coursebackend/coursebackend_*.inc.php', GLOB_NOSORT) as $file) { require_once $file; preg_match('#coursebackend_([^/\.]+)\.inc\.php$#i', $file, $out); - if (!class_exists('coursebackend_' . $out[1])) { - trigger_error("Backend type source unit $file doesn't seem to define class CourseBackend_{$out[1]}", E_USER_ERROR); + $className = 'CourseBackend_' . $out[1]; + if (!class_exists($className)) { + trigger_error("Backend type source unit $file doesn't seem to define class $className", E_USER_ERROR); } + if (!CONFIG_DEBUG && defined("$className::DEBUG") && constant("$className::DEBUG")) + continue; self::$backendTypes[$out[1]] = true; } } /** - * Get all known config module types. + * Get all known backend types. * - * @return array list of modules + * @return array list of backends */ public static function getList() { @@ -65,24 +68,30 @@ abstract class CourseBackend return array_keys(self::$backendTypes); } + public static function exists($backendType) + { + self::loadDb(); + return isset(self::$backendTypes[$backendType]); + } + /** - * Get fresh instance of ConfigModule subclass for given module type. + * Get fresh instance of CourseBackend subclass for given backend type. * - * @param string $moduleType name of module type - * @return \CourseBackend module instance + * @param string $backendType name of module type + * @return \CourseBackend|false module instance */ - public static function getInstance($moduleType) + public static function getInstance($backendType) { self::loadDb(); - if (!isset(self::$backendTypes[$moduleType])) { - error_log('Unknown module type: ' . $moduleType); + if (!isset(self::$backendTypes[$backendType])) { + error_log('Unknown module type: ' . $backendType); return false; } - if (!is_object(self::$backendTypes[$moduleType])) { - $class = "coursebackend_$moduleType"; - self::$backendTypes[$moduleType] = new $class; + if (!is_object(self::$backendTypes[$backendType])) { + $class = "coursebackend_$backendType"; + self::$backendTypes[$backendType] = new $class; } - return self::$backendTypes[$moduleType]; + return self::$backendTypes[$backendType]; } /** @@ -131,6 +140,19 @@ abstract class CourseBackend */ protected abstract function fetchSchedulesInternal($roomId); + private static function fixTime(&$start, &$end) + { + if (!preg_match('/^\d+-\d+-\d+T\d+:\d+:\d+$/', $start) || !preg_match('/^\d+-\d+-\d+T\d+:\d+:\d+$/', $start)) + return false; + $start = strtotime($start); + $end = strtotime($end); + if ($start >= $end) + return false; + $start = date('Y-m-d\TH:i:s', $start); + $end = date('Y-m-d\TH:i:s', $end); + return true; + } + /** * Method for fetching the schedule of the given rooms on a server. * @@ -184,18 +206,31 @@ abstract class CourseBackend return false; } - if ($this->getCacheTime() > 0) { - // Caching requested by backend, write to DB - foreach ($backendResponse as $serverRoomId => $calendar) { + foreach ($backendResponse as $serverRoomId => &$calendar) { + $calendar = array_values($calendar); + for ($i = 0; $i < count($calendar); ++$i) { + if (empty($calendar[$i]['title'])) { + $calendar[$i]['title'] = '-'; + } + if (!self::fixTime($calendar[$i]['start'], $calendar[$i]['end'])) { + error_log("Ignoring calendar entry '{$calendar[$i]['title']}' with bad time format"); + unset($calendar[$i]); + } + } + $calendar = array_values($calendar); + if ($this->getCacheTime() > 0) { + // Caching requested by backend, write to DB $value = json_encode($calendar); Database::simpleQuery("UPDATE locationinfo_locationconfig SET calendar = :ttable, lastcalendarupdate = :now - WHERE serverid = :serverid AND serverlocationid = :serverlocationid", array( + WHERE serverid = :serverid AND serverlocationid = :serverlocationid", array( 'serverid' => $this->serverId, 'serverlocationid' => $serverRoomId, 'ttable' => $value, 'now' => $NOW )); } + + unset($calendar); } // Add rooms that were requested to the final return value foreach ($remoteIds as $location => $serverRoomId) { @@ -302,6 +337,9 @@ abstract class CourseBackend $xml = new SimpleXMLElement($cleanresponse); } catch (Exception $e) { $this->error = 'Could not parse reply as XML, got ' . get_class($e) . ': ' . $e->getMessage(); + if (CONFIG_DEBUG) { + error_log($cleanresponse); + } return false; } $array = json_decode(json_encode((array)$xml), true); |