From 2ca95f58b77ee34743dcc1a84335a5ef53730db2 Mon Sep 17 00:00:00 2001 From: Dirk Riestere Date: Thu, 2 Mar 2017 23:32:22 +0100 Subject: Bugfixes und Code verschönert --- .../locationinfo/inc/coursebackend.inc.php | 183 ++++++++++---------- .../coursebackend/coursebackend_davinci.inc.php | 192 +++++++++++---------- .../coursebackend/coursebackend_hisinone.inc.php | 38 ++-- 3 files changed, 217 insertions(+), 196 deletions(-) (limited to 'modules-available/locationinfo') diff --git a/modules-available/locationinfo/inc/coursebackend.inc.php b/modules-available/locationinfo/inc/coursebackend.inc.php index a3e7bf96..dbc99e10 100644 --- a/modules-available/locationinfo/inc/coursebackend.inc.php +++ b/modules-available/locationinfo/inc/coursebackend.inc.php @@ -12,14 +12,15 @@ abstract class CourseBackend /** * @var array list of known backends - * $error boolean true if there was an error - * $errormsg string with the error message + * $error boolean true if there was an error + * $errormsg string with the error message */ private static $backendTypes = false; - public $error; - public $errormsg; + public $error; + public $errormsg; + public $serverID; - /** + /** * Load all known backend types. This is done * by including *.inc.php from inc/coursebackend/. */ @@ -80,106 +81,108 @@ abstract class CourseBackend public abstract function getDisplayName(); - - /** - * @returns array with parameter name as key and type as value - */ - public abstract function getCredentials(); - - /** - * @return boolean true if the connection works, false otherwise - */ - public abstract function checkConection(); - - /** - * uses json to setCredentials, the json must follow the form given in - * getCredentials - * @param json $json jsonarray with the credentials - * @param string $url adress of the server - * @param int $serverID ID of the server - * @returns void - */ - public abstract function setCredentials($json, $url, $serverID); - - /** + /** + * @returns array with parameter name as key and type as value + */ + public abstract function getCredentials(); + + /** + * @return boolean true if the connection works, false otherwise + */ + public abstract function checkConnection(); + + /** + * uses json to setCredentials, the json must follow the form given in + * getCredentials + * + * @param string $json jsonarray with the credentials + * @param string $url address of the server + * @param int $serverID ID of the server + * @returns void + */ + public abstract function setCredentials($json, $url, $serverID); + + /** * @return int desired caching time of results, in seconds. 0 = no caching */ public abstract function getCacheTime(); - - /** - * @return int age after which ttables are no longer refreshed should be - * greater then CacheTime - */ - public abstract function getRefreshTime(); - - /** + + /** + * @return int age after which timetables are no longer refreshed should be + * greater then CacheTime + */ + public abstract function getRefreshTime(); + + /** * Internal version of fetch, to be overridden by subclasses. + * * @param $roomIds - * @return array a multidemensional array that uses the roomID as key - * and has the sheduls as string in the value + * @return array a multidimensional array that uses the roomID as key + * and has the schedules as string in the value */ protected abstract function fetchSchedulesInternal($roomId); /** * Method for fetching the schedule of the given rooms on a server. + * * @param int $roomId int of room ID to fetch - * @param int $serverid id of the server + * @param int $serverid id of the server * @return string|bool some jsonstring as result, or false on error */ public final function fetchSchedule($roomIDs) { - $sqlr=implode(",", $roomIDs); - $sqlr = '('.$sqlr.')'; - $q = "SELECT locationid, calendar, serverroomid, lastcalendarupdate FROM location_info WHERE locationid In ".$sqlr; - $dbquery1 = Database::simpleQuery($q); - foreach ($dbquery1->fetchAll(PDO::FETCH_ASSOC) as $row){ - $sroomID = $row['serverroomid']; - $lastUpdate = $row['lastcalendarupdate']; - $calendar = $row['calendar']; - //Check if in cache if lastUpdate is null then it is interpreted as 1970 - if(strtotime($lastUpdate) > strtotime("-".$this->getCacheTime()."seconds") && $this->getCacheTime()>0) { - $result[$row['locationid']]=$calendar; - } - else { - $sroomIDs[$row['locationid']] = $sroomID; - } - - } - //Check if we should refresh other rooms recently requested by front ends - if ($this->getCacheTime()>0&&$this->getRefreshTime()>0) { - $dbquery4 = Database::simpleQuery("SELECT locationid ,serverroomid, lastcalendarupdate FROM location_info WHERE serverid= :id", array('id' => $this->serverID)); - foreach($dbquery4->fetchAll(PDO::FETCH_COLUMN) as $row){ - if(strtotime($row['lastcalendarupdate'])>strtotime("-".$this->getRefreshTime()."seconds")&&strtotime($row['lastcalendarupdate'])> strtotime("-".$this->getCacheTime()."seconds")){ - $sroomIDs[$row['locationid']] = $row['serverroomid']; - } - } - } - $results = $this->fetchSchedulesInternal($sroomIDs); - foreach ($sroomIDs as $location => $serverroom){ - $newresult[$location] = $results[$serverroom]; - } - - if($this->getCacheTime()>0){ - foreach ($newresult as $key => $value) { - $now = strtotime('Now'); - $dbquery1 = Database::simpleQuery("UPDATE location_info SET calendar = :ttable, lastcalendarupdate = :now WHERE locationid = :id ", array('id' => $key,'ttable' => $value,'now'=> $now)); - } - } - //get all sheduls that are wanted from roomIDs - foreach($roomIDs as $id){ - $result[$id] = $newresult[$id]; - } - return $result; + $sqlr = implode(",", $roomIDs); + $sqlr = '(' . $sqlr . ')'; + $q = "SELECT locationid, calendar, serverroomid, lastcalendarupdate FROM location_info WHERE locationid IN " . $sqlr; + $dbquery1 = Database::simpleQuery($q); + foreach ($dbquery1->fetchAll(PDO::FETCH_ASSOC) as $row) { + $sroomID = $row['serverroomid']; + $lastUpdate = $row['lastcalendarupdate']; + $calendar = $row['calendar']; + //Check if in cache if lastUpdate is null then it is interpreted as 1970 + if (strtotime($lastUpdate) > strtotime("-" . $this->getCacheTime() . "seconds") && $this->getCacheTime() > 0) { + $result[$row['locationid']] = $calendar; + } else { + $sroomIDs[$row['locationid']] = $sroomID; + } + + } + //Check if we should refresh other rooms recently requested by front ends + if ($this->getCacheTime() > 0 && $this->getRefreshTime() > 0) { + $dbquery4 = Database::simpleQuery("SELECT locationid ,serverroomid, lastcalendarupdate FROM location_info WHERE serverid= :id", array('id' => $this->serverID)); + foreach ($dbquery4->fetchAll(PDO::FETCH_COLUMN) as $row) { + if (strtotime($row['lastcalendarupdate']) > strtotime("-" . $this->getRefreshTime() . "seconds") && strtotime($row['lastcalendarupdate']) > strtotime("-" . $this->getCacheTime() . "seconds")) { + $sroomIDs[$row['locationid']] = $row['serverroomid']; + } + } + } + $results = $this->fetchSchedulesInternal($sroomIDs); + foreach ($sroomIDs as $location => $serverroom) { + $newresult[$location] = $results[$serverroom]; + } + + if ($this->getCacheTime() > 0) { + foreach ($newresult as $key => $value) { + $now = strtotime('Now'); + Database::simpleQuery("UPDATE location_info SET calendar = :ttable, lastcalendarupdate = :now WHERE locationid = :id ", array('id' => $key, 'ttable' => $value, 'now' => $now)); + } + } + //get all schedules that are wanted from roomIDs + foreach ($roomIDs as $id) { + $result[$id] = $newresult[$id]; + } + return $result; + } + + /** + * @return false if there was no error string with error message if there was one + */ + public final function getError() + { + if ($this->error) { + return $this->errormsg; + } + return false; } - - /** - * @return false if there was no error string with error message if there was one - */ - public final function getError(){ - if($this->error){ - return $this->errormsg; - } - return false; - } } diff --git a/modules-available/locationinfo/inc/coursebackend/coursebackend_davinci.inc.php b/modules-available/locationinfo/inc/coursebackend/coursebackend_davinci.inc.php index c62ad4f4..ce549c72 100644 --- a/modules-available/locationinfo/inc/coursebackend/coursebackend_davinci.inc.php +++ b/modules-available/locationinfo/inc/coursebackend/coursebackend_davinci.inc.php @@ -1,97 +1,109 @@ location = $location."/DAVINCIIS.dll?"; - $this->serverID = $serverID; - //Davinci doesn't have credentials - } - - public function checkConection(){ - $this->fetchSchedulesInternal('B206'); - return $this->error; - } - public function getCredentials(){ - $return = array(); - return $return; - } - public function getDisplayName(){ - return'Davinci'; - } - public function getCacheTime(){ - return 0; - } - public function getRefreshTime(){ - return 0; - } - private function toArray($response){ + private $location; + + public function setCredentials($json, $location, $serverID) + { + $this->location = $location . "/DAVINCIIS.dll?"; + $this->serverID = $serverID; + //Davinci doesn't have credentials + } + + public function checkConnection() + { + $this->fetchSchedulesInternal('B206'); + return $this->error; + } + + public function getCredentials() + { + $return = array(); + return $return; + } + + public function getDisplayName() + { + return 'Davinci'; + } + + public function getCacheTime() + { + return 0; + } - $cleanresponse = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response); - $xml = new SimpleXMLElement($cleanresponse); - $array = json_decode(json_encode((array)$xml), TRUE); - return $array; - } - private function fetchArray($roomId){ - $startdate= new DateTime('monday this week'); - $enddate = new DateTime('sunday'); - $url= $this->location."content=xml&type=room&name=".$roomId."&startdate=".$startdate->format('d.m.Y')."&enddate=".$enddate->format('d.m.Y'); - $ch=curl_init(); - $options = array( - CURLOPT_RETURNTRANSFER => true, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_SSL_VERIFYHOST => false, - CURLOPT_SSL_VERIFYPEER => false, - CURLOPT_URL => $url , - ); + public function getRefreshTime() + { + return 0; + } - curl_setopt_array($ch, $options); - $output=curl_exec($ch); - if( $output === false) - { - $this->error = true; - $this->errormsg = 'Curl error: ' . curl_error($soap_do); - return 'Curl error: ' . curl_error($soap_do); - } - else - { - $this->error = false; - $this->errormsg =""; - ///Operation completed successfully - } - curl_close($ch); - return $this->toArray($output); - - } - public function fetchSchedulesInternal($roomIds){ - $shedules = []; - foreach ($roomIds as $locationId => $sroomId) { - $return = $this->fetchArray($sroomId); - $lessons = $return['Lessons']['Lesson']; - $timetable =[]; - foreach ($lessons as $lesson) { - $date = $lesson['Date']; - $date = substr($date,0,4).'-'.substr($date,4,2).'-'.substr($date,6,2); - $start = $lesson['Start']; - $start = substr($start,0,2).':'.substr($start,2,2); - $end = $lesson['Finish']; - $end = substr($end,0,2).':'.substr($end,2,2); - $subject = $lesson['Subject']; - $json = array( - 'title' => $subject, - 'start' => $date." ".$start.':00', - 'end' => $date." ".$end.':00' - ); - array_push($timetable,$json); - } - $timetable= json_encode($timetable); - $shedules[$locationId] = $timetable; - } - return $shedules; - } + private function toArray($response) + { + $cleanresponse = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response); + $xml = new SimpleXMLElement($cleanresponse); + $array = json_decode(json_encode((array)$xml), true); + return $array; + } + + private function fetchArray($roomId) + { + $startDate = new DateTime('monday this week'); + $endDate = new DateTime('sunday'); + $url = $this->location . "content=xml&type=room&name=" . $roomId . "&startdate=" . $startDate->format('d.m.Y') . "&enddate=" . $endDate->format('d.m.Y'); + $ch = curl_init(); + $options = array( + CURLOPT_RETURNTRANSFER => true, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_SSL_VERIFYHOST => false, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_URL => $url, + ); + + curl_setopt_array($ch, $options); + $output = curl_exec($ch); + if ($output === false) { + $this->error = true; + $this->errormsg = 'Curl error: ' . curl_error($ch); + return 'Curl error: ' . curl_error($ch); + } else { + $this->error = false; + $this->errormsg = ""; + ///Operation completed successfully + } + curl_close($ch); + return $this->toArray($output); + + } + + public function fetchSchedulesInternal($roomIds) + { + $schedules = []; + foreach ($roomIds as $sroomId) { + $return = $this->fetchArray($sroomId); + $lessons = $return['Lessons']['Lesson']; + $timetable = []; + foreach ($lessons as $lesson) { + $date = $lesson['Date']; + $date = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6, 2); + $start = $lesson['Start']; + $start = substr($start, 0, 2) . ':' . substr($start, 2, 2); + $end = $lesson['Finish']; + $end = substr($end, 0, 2) . ':' . substr($end, 2, 2); + $subject = $lesson['Subject']; + $json = array( + 'title' => $subject, + 'start' => $date . " " . $start . ':00', + 'end' => $date . " " . $end . ':00' + ); + array_push($timetable, $json); + } + $timetable = json_encode($timetable); + $schedules[$sroomId] = $timetable; + } + return $schedules; + } } -?> + diff --git a/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php b/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php index 157a5bf6..b223a69b 100644 --- a/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php +++ b/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php @@ -6,7 +6,6 @@ class CourseBackend_HisInOne extends CourseBackend private $password; private $location; private $open; - public $serverID; //Sets the location and the login information of this client public function setCredentials($json,$location,$serverID) { @@ -24,7 +23,7 @@ class CourseBackend_HisInOne extends CourseBackend $this->serverID = $serverID; } - public function checkConection(){ + public function checkConnection(){ $this->findUnit(42); return $this->error; } @@ -71,7 +70,7 @@ class CourseBackend_HisInOne extends CourseBackend public function findUnit($roomID){ $termyear = date('Y'); $termtype = date('n'); - if($termtype > 3 && termtype < 10){ + if($termtype > 3 && $termtype < 10){ $termtype = 2; } elseif ($termtype > 10) { @@ -114,13 +113,14 @@ class CourseBackend_HisInOne extends CourseBackend if(isset($respons2['soapenvBody']['soapenvFault'])){ $this->error = true; $this->errormsg =$respons2['soapenvBody']['soapenvFault']['faultcode']." ".$respons2['soapenvBody']['soapenvFault']['faultstring']; - return; + return false; } else{ $this->error = false; } if($this->open){ $units = $respons2['soapenvBody']['hisfindUnitResponse']['hisunits']['hisunit']; + $id = []; foreach ($units as $unit) { $id[]= $unit['hisid']; } @@ -163,7 +163,7 @@ class CourseBackend_HisInOne extends CourseBackend if(isset($respons2['soapenvBody']['soapenvFault'])){ $this->error = true; $this->errormsg =$respons2['soapenvBody']['soapenvFault']['faultcode']." ".$respons2['soapenvBody']['soapenvFault']['faultstring']; - return; + return false; } else{ $this->error = false; @@ -211,15 +211,20 @@ class CourseBackend_HisInOne extends CourseBackend curl_close($soap_do); return $output; } - - //Request for a timetable with roomid as int - public function getJson($param){ + + /** + * Request for a timetable + * @param $param int the roomid + * @return string the timetable as json + */ + public function getJson($param){ //get all eventIDs in a given room $eventIDs = $this-> findUnit($param); if($this->error == true){ return $this->errormsg; } //get all information on each event + $events = []; foreach ($eventIDs as $each_event) { $events[] = $this->readUnit((int) $each_event); if($this->error == true){ @@ -294,17 +299,18 @@ class CourseBackend_HisInOne extends CourseBackend //Request for a timetable with roomids as array public function fetchSchedulesInternal($param){ //get all eventIDs in a given room + $eventIDs = []; foreach ($param as $ID) { - $eventIDs = $this-> findUnit($ID); - if($this->error == true){ - return $this->errormsg; - } - } - $eventIDs = array_unique($eventIDs); - + $eventIDs = array_merge($eventIDs, $this->findUnit($ID)); + var_dump($eventIDs); + $eventIDs = array_unique($eventIDs); + if ($this->error == true) { + return $this->errormsg; + } + } //get all information on each event foreach ($eventIDs as $each_event) { - $events[] = $this->readUnit((int) $each_event); + $events[] = $this->readUnit(intval($each_event)); if($this->error == true){ return $this->errormsg; } -- cgit v1.2.3-55-g7522