diff options
Diffstat (limited to 'modules-available/locationinfo/api.inc.php')
-rw-r--r-- | modules-available/locationinfo/api.inc.php | 85 |
1 files changed, 9 insertions, 76 deletions
diff --git a/modules-available/locationinfo/api.inc.php b/modules-available/locationinfo/api.inc.php index d3ff9ebd..24919ba1 100644 --- a/modules-available/locationinfo/api.inc.php +++ b/modules-available/locationinfo/api.inc.php @@ -14,7 +14,7 @@ function HandleParameters() $get = Request::get('get', 0, 'string'); $uuid = Request::get('uuid', false, 'string'); - $output = false; + $output = null; if ($get === "timestamp") { $output = array('ts' => getLastChangeTs($uuid)); } elseif ($get === "machines") { @@ -24,7 +24,7 @@ function HandleParameters() $output = array_values($output); } elseif ($get === "config") { $type = InfoPanel::getConfig($uuid, $output); - if ($type === false) { + if ($type === null) { http_response_code(404); die('Panel not found'); } @@ -36,9 +36,9 @@ function HandleParameters() $output = getLocationTree($locationIds); } elseif ($get === "calendar") { $locationIds = LocationInfo::getLocationsOr404($uuid); - $output = getCalendar($locationIds); + $output = LocationInfo::getCalendar($locationIds); } - if ($output !== false) { + if ($output !== null) { Header('Content-Type: application/json; charset=utf-8'); echo json_encode($output); } else { @@ -59,7 +59,7 @@ function HandleParameters() * @param string $paneluuid panels uuid * @return int UNIX_TIMESTAMP */ -function getLastChangeTs($paneluuid) +function getLastChangeTs(string $paneluuid): int { $panel = Database::queryFirst('SELECT lastchange, locationids FROM locationinfo_panel WHERE paneluuid = :paneluuid', compact('paneluuid')); @@ -84,7 +84,7 @@ function getLastChangeTs($paneluuid) * @param int[] $idList list of the location ids. * @return array aggregated PC states */ -function getPcStates($idList, $paneluuid) +function getPcStates(array $idList, string $paneluuid): array { $pcStates = array(); foreach ($idList as $id) { @@ -130,18 +130,17 @@ function getPcStates($idList, $paneluuid) * @param int[] $idList Array list of the locations. * @return array location tree data */ -function getLocationTree($idList) +function getLocationTree(array $idList): array { if (in_array(0, $idList)) { return array_values(Location::getTree()); } $locations = Location::getTree(); - $ret = findLocations($locations, $idList); - return $ret; + return findLocations($locations, $idList); } -function findLocations($locations, $idList) +function findLocations(array $locations, array $idList): array { $ret = array(); foreach ($locations as $location) { @@ -153,69 +152,3 @@ function findLocations($locations, $idList) } return $ret; } - -// ########## <Calendar> ########### -/** - * Gets the calendar of the given ids. - * - * @param int[] $idList list with the location ids. - * @return array Calendar. - */ -function getCalendar($idList) -{ - if (empty($idList)) - return []; - - // Build SQL query for multiple ids. - $query = "SELECT l.locationid, l.serverid, l.serverlocationid, s.servertype, s.credentials - FROM `locationinfo_locationconfig` AS l - INNER JOIN locationinfo_coursebackend AS s ON (s.serverid = l.serverid) - WHERE l.locationid IN (:idlist) - ORDER BY s.servertype ASC"; - $dbquery = Database::simpleQuery($query, array('idlist' => array_values($idList))); - - $serverList = array(); - while ($dbresult = $dbquery->fetch(PDO::FETCH_ASSOC)) { - if (!isset($serverList[$dbresult['serverid']])) { - $serverList[$dbresult['serverid']] = array( - 'credentials' => json_decode($dbresult['credentials'], true), - 'type' => $dbresult['servertype'], - 'idlist' => array() - ); - } - $serverList[$dbresult['serverid']]['idlist'][] = $dbresult['locationid']; - } - - $resultArray = array(); - foreach ($serverList as $serverid => $server) { - $serverInstance = CourseBackend::getInstance($server['type']); - if ($serverInstance === false) { - EventLog::warning('Cannot fetch schedule for location (' . implode(', ', $server['idlist']) . ')' - . ': Backend type ' . $server['type'] . ' unknown. Disabling location.'); - Database::exec("UPDATE locationinfo_locationconfig SET serverid = NULL WHERE locationid IN (:lid)", - array('lid' => $server['idlist'])); - continue; - } - $credentialsOk = $serverInstance->setCredentials($serverid, $server['credentials']); - - if ($credentialsOk) { - $calendarFromBackend = $serverInstance->fetchSchedule($server['idlist']); - } else { - $calendarFromBackend = array(); - } - - LocationInfo::setServerError($serverid, $serverInstance->getErrors()); - - if (is_array($calendarFromBackend)) { - foreach ($calendarFromBackend as $key => $value) { - $resultArray[] = array( - 'id' => $key, - 'calendar' => $value, - ); - } - } - } - return $resultArray; -} - -// ########## </Calendar> ########## |