From 17a2fa5cd311fcc01599b10506eec4576f99ce9d Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Tue, 14 Mar 2017 12:46:21 +0100 Subject: API: filtered Hidden room added. Deleted some redundant code. Reorganised some Methods. --- modules-available/locationinfo/api.inc.php | 519 +++++++++++++---------------- 1 file changed, 224 insertions(+), 295 deletions(-) (limited to 'modules-available/locationinfo/api.inc.php') diff --git a/modules-available/locationinfo/api.inc.php b/modules-available/locationinfo/api.inc.php index 62943ca7..a0f65cdb 100644 --- a/modules-available/locationinfo/api.inc.php +++ b/modules-available/locationinfo/api.inc.php @@ -7,7 +7,7 @@ function HandleParameters() { $getAction = Request::get('action', 0, 'string'); if ($getAction == "roominfo") { $roomIDs = Request::get('id', 0, 'string'); - $array = getMultipleInformations($roomIDs); + $array = filterIdList($roomIDs); $getCoords = Request::get('coords', 0, 'string'); if (empty($getCoords)) { $getCoords = '0'; @@ -15,329 +15,104 @@ function HandleParameters() { echo getRoomInfo($array, $getCoords); } elseif ($getAction == "openingtime") { $roomIDs = Request::get('id', 0, 'string'); - $array = getMultipleInformations($roomIDs); + $array = filterIdList($roomIDs); echo getOpeningTime($array); } elseif ($getAction == "config") { $getRoomID = Request::get('id', 0, 'int'); getConfig($getRoomID); } elseif ($getAction == "pcstates") { $roomIDs = Request::get('id', 0, 'string'); - $array = getMultipleInformations($roomIDs); + $array = filterIdList($roomIDs); echo getPcStates($array); } elseif ($getAction == "roomtree") { - $roomIDS = Request::get('id', 0, 'string'); - echo getRoomTree($roomIDS); + $roomIDs = Request::get('id', 0, 'string'); + $array = filterIdList($roomIDs); + echo getRoomTree($array); } elseif ($getAction == "calendar") { $roomIDs = Request::get('id', 0, 'string'); - $array = getMultipleInformations($roomIDs); + $array = filterIdList($roomIDs); echo getCalendar($array); } } -function getMultipleInformations($roomids) { +function filterIdList($roomids) { $idList = explode(',', $roomids); $filteredIdList = array_filter($idList, 'is_numeric'); + $filteredIdList = array_unique($filteredIdList); + $filteredIdList = filterHiddenRoom($filteredIdList); + return $filteredIdList; } -// ########## ########### -function getCalendar($idList) { - - //// Build SQL query for multiple ids. - $query = "SELECT locationid, l.serverid AS serverid, serverurl, servertype, credentials FROM `location_info` as l LEFT JOIN setting_location_info as s ON s.serverid = l.serverid WHERE locationid IN ("; - - $query .= implode(",", $idList); - - $query .= ") AND l.serverid = s.serverid ORDER BY servertype ASC, locationid ASC"; - - $dbquery = Database::simpleQuery($query); - - $serverList = array(); - - $first = true; - $lastservertype = ""; - while($dbresult=$dbquery->fetch(PDO::FETCH_ASSOC)) { - if (!isset($serverList[$dbresult['serverid']])) { - $serverList[$dbresult['serverid']] = array('credentials' => json_decode($dbresult['credentials'], true), 'url' => $dbresult['serverurl'], 'type' => $dbresult['servertype'], 'idlist' => array()); - } - $serverList[$dbresult['serverid']]['idlist'][] = $dbresult['locationid']; - } +function filterHiddenRoom($idArray) { + $filteredArray = array(); + if (!empty($idArray)) { + $query = "SELECT locationid, hidden FROM `location_info` WHERE locationid IN ("; + $query .= implode(",", $idArray); + $query .= ")"; - $resultarray = array(); - foreach ($serverList as $serverid => $server) { - $serverInstance = CourseBackend::getInstance($server['type']); - $setCred = $serverInstance->setCredentials($server['credentials'], $server['url'], $serverid); + $dbquery = Database::simpleQuery($query); - $calendarFromBackend = $serverInstance->fetchSchedule($server['idlist']); - $formattedArray = array(); - $resultarray = array(); - if ($calendarFromBackend === false || $setCred === false) { - $error['timestamp'] = time(); - $error['error'] = $serverInstance->getError(); - Database::exec("UPDATE `setting_location_info` Set error=:error WHERE serverid=:id", array('id' => $serverid, 'error' => json_encode($error, true))); - } else { - Database::exec("UPDATE `setting_location_info` Set error=NULL WHERE serverid=:id", array('id' => $serverid)); - } - if (is_array($calendarFromBackend)) { - foreach ($calendarFromBackend as $key => $value) { - $y['id'] = $key; - $y['calendar'] = $value; - $formattedArray[] = $y; + while($dbresult=$dbquery->fetch(PDO::FETCH_ASSOC)) { + if ($dbresult['hidden'] == false) { + $filteredArray[] = $dbresult['locationid']; } - $resultarray = array_merge($resultarray, $formattedArray); } } - return json_encode($resultarray, true); -} - -// ########## ########## - -// ################# OLD CALENDAR STUFF ######################################## -/* -function randomCalendarGenerator() { - $randNum = rand(3, 7); - - $result = array(); - - for ($i = 0; $i < $randNum; $i++) { - $c = array(); - $c['title'] = getRandomWord(); - - $randH = rand(8, 16); - $rand2 = $randH + 2; - $date = getdate(); - $mday = $date['mday'] + $i; - $todays = $date['year'] . "-" . $date['month'] . "-" . $mday . " " . $randH . ":00:00"; - $c['start'] = $todays; - $todaye = $date['year'] . "-" . $date['month'] . "-" . $mday . " " . $rand2 . ":00:00"; - $c['end'] = $todaye; - $result[] = $c; - } - return json_encode($result); -} - -function getRandomWord($len = 10) { - $word = array_merge(range('a', 'z'), range('A', 'Z')); - shuffle($word); - return substr(implode($word), 0, $len); -} - - -// TODO FILTER 2 weeks or some days only -function getCalendars($ids) { - $idList = getMultipleInformations($ids); - $calendars = array(); - - foreach ($idList as $id) { - $a['id'] = $id; - $a['calendar'] = json_decode(getCalendar($id), true); - $calendars[] = $a; - } - echo json_encode($calendars); -} - -function getCalendar($getRoomID) { - // TODO GET AND RETURN THE ACTUAL calendar - //echo randomCalendarGenerator(); - - $dbquery = Database::simpleQuery("SELECT calendar, lastcalendarupdate, serverid, serverroomid FROM `location_info` WHERE locationid=:locationID", array('locationID' => $getRoomID)); - - $calendar; - $lastupdate; - $serverid; - $serverroomid; - while($dbresult=$dbquery->fetch(PDO::FETCH_ASSOC)) { - $lastupdate = (int) $dbresult['lastcalendarupdate']; - $calendar = $dbresult['calendar']; - $serverid = $dbresult['serverid']; - $serverroomid = $dbresult['serverroomid']; - } - - $NOW = time(); - if ($lastupdate == 0 || $NOW - $lastupdate > 900) { - return updateCalendar($getRoomID, $serverid, $serverroomid); - } else { - return $calendar; - } -} - -function updateCalendar($locationid, $serverid, $serverroomid) { - // TODO CALL UpdateCalendar($serverid, $serverroomid); - $result = randomCalendarGenerator(); - // ^ replace with the actual call - - // Save in db and update timestamp - $NOW = time(); - Database::exec("UPDATE `location_info` Set calendar=:calendar, lastcalendarupdate=:now WHERE locationid=:id", array('id' => $locationid, 'calendar' => $result, 'now' => $NOW)); - - return $result; -} -*/ -// ######################################################################## - - -function getPcStates($idList) { - $pcStates = array(); - - $roominfoList = json_decode(getRoomInfo($idList), true); - foreach ($roominfoList as $roomInfo) { - $result['id'] = $roomInfo['id']; - $idle = 0; - $occupied = 0; - $off = 0; - $broken = 0; - - foreach ($roomInfo['computer'] as $computer) { - if ($computer['pcState'] == 0) { - $idle++; - } elseif($computer['pcState'] == 1) { - $occupied++; - } elseif($computer['pcState'] == 2) { - $off++; - } elseif($computer['pcState'] == 3) { - $broken++; - } - } - - $result['idle'] = $idle; - $result['occupied'] = $occupied; - $result['off'] = $off; - $result['broken'] = $broken; - $pcStates[] = $result; - } - return json_encode($pcStates); -} - -function getRoomTree($ids) { - $idList = getMultipleInformations($ids); - $idList = array_unique($idList); - $roomTree = array(); - $filteredIdList = array(); - foreach ($idList as $id) { - $dbresult = Database::queryFirst("SELECT locationname FROM `location` WHERE locationid=:locationID", array('locationID' => $id)); - - if (!in_array($id, $filteredIdList)) { - $a['id'] = $id; - $a['name'] = $dbresult['locationname']; - $filteredIdList[] = $id; - $a['childs'] = getChildsRecursive($id, $filteredIdList); - $roomTree[] = $a; - } - } - - return json_encode($roomTree); -} - -function getChildsRecursive($id, &$filteredIdList) { - $dbquery = Database::simpleQuery("SELECT locationid, locationname FROM `location` WHERE parentlocationid=:locationID", array('locationID' => $id)); - $array = array(); - $dbarray = array(); - - while($dbresult=$dbquery->fetch(PDO::FETCH_ASSOC)) { - $dbarray[] = $dbresult; - } - foreach ($dbarray as $db) { - $i = $db['locationid']; - - if (!in_array($i, $filteredIdList)) { - $a['id'] = $i; - $a['name'] = $db['locationname']; - $filteredIdList[] = $i; - $a['childs'] = getChildsRecursive($i, $filteredIdList); - $array[] = $a; - } - - } - - return $array; -} - -function getConfig($locationID) { - $dbresult = Database::queryFirst("SELECT l.locationname, li.config, li.serverroomid, s.servertype, s.serverurl FROM `location_info` AS li - RIGHT JOIN `location` AS l ON l.locationid=li.locationid - LEFT JOIN `setting_location_info` AS s ON s.serverid=li.serverid - WHERE l.locationid=:locationID", array('locationID' => $locationID)); - $config = array(); - - $config = json_decode($dbresult['config'], true); - $config['room'] = $dbresult['locationname']; - $date = getdate(); - $config['time'] = $date['year'] . "-" . $date['mon'] . "-" . $date['mday'] . " " . $date['hours'] . ":" . $date['minutes'] . ":" . $date['seconds']; - - if($dbresult['servertype'] === "Frontend") { - $config['calendarqueryurl'] = $dbresult['serverurl'] . "/" . $dbresult['serverroomid'] . ".json"; - } - - if (empty($config)) { - echo json_encode(array()); - } else { - echo json_encode($config, JSON_UNESCAPED_SLASHES); - } + return $filteredArray; } -/* -function checkIfHidden($locationID) { - $dbquery = Database::simpleQuery("SELECT hidden FROM `location_info` WHERE locationid = :locationID", array('locationID' => $locationID)); - - while($roominfo=$dbquery->fetch(PDO::FETCH_ASSOC)) { - $hidden = $roominfo['hidden']; - if ($hidden === '0') { - return false; - } else { - return true; - } - } - return false; -}*/ - // ########## ########## function getRoomInfo($idList, $coords = false) { $coordinates = (string)$coords; - // Build SQL Query for multiple ids. - $query = "SELECT m.locationid, machineuuid, position, logintime, lastseen, lastboot FROM `machine` as m LEFT JOIN location_info AS l ON l.locationid = m.locationid WHERE l.hidden = 0 AND m.locationid IN ("; + $dbresult = array(); - $query .= implode(",", $idList); - $query .= ")"; + if (!empty($idList)) { + // Build SQL Query for multiple ids. + $query = "SELECT m.locationid, machineuuid, position, logintime, lastseen, lastboot FROM `machine` as m LEFT JOIN location_info AS l ON l.locationid = m.locationid WHERE l.hidden = 0 AND m.locationid IN ("; - $query .= " ORDER BY m.locationid ASC"; + $query .= implode(",", $idList); + $query .= ")"; - // Execute query. - $dbquery = Database::simpleQuery($query); - $dbresult = array(); + $query .= " ORDER BY m.locationid ASC"; - $currentlocationid = 0; + // Execute query. + $dbquery = Database::simpleQuery($query); - $pclist = array(); + $currentlocationid = 0; - // Fetch db data. - while($dbdata=$dbquery->fetch(PDO::FETCH_ASSOC)) { + $pclist = array(); - // Set the id if the locationid changed. - if (!isset($dbresult[$dbdata['locationid']])) { - $dbresult[$dbdata['locationid']] = array('id' => $dbdata['locationid'], 'computer' => array()); - } + // Fetch db data. + while($dbdata=$dbquery->fetch(PDO::FETCH_ASSOC)) { - // Compact the pc data in one array. - $pc['id'] = $dbdata['machineuuid']; - if ($coordinates == '1' || $coordinates == 'true') { - $position = json_decode($dbdata['position'], true); - $pc['x'] = $position['gridCol']; - $pc['y'] = $position['gridRow']; - - if (isset($position['overlays'])) { - $pc['overlay'] = $position['overlays']; + // Set the id if the locationid changed. + if (!isset($dbresult[$dbdata['locationid']])) { + $dbresult[$dbdata['locationid']] = array('id' => $dbdata['locationid'], 'computer' => array()); } - else { - $pc['overlay'] = array(); + + // Compact the pc data in one array. + $pc['id'] = $dbdata['machineuuid']; + if ($coordinates == '1' || $coordinates == 'true') { + $position = json_decode($dbdata['position'], true); + $pc['x'] = $position['gridCol']; + $pc['y'] = $position['gridRow']; + + if (isset($position['overlays'])) { + $pc['overlay'] = $position['overlays']; + } + else { + $pc['overlay'] = array(); + } } - } - $pc['pcState'] = LocationInfo::getPcState($dbdata); + $pc['pcState'] = LocationInfo::getPcState($dbdata); - // Add the array to the computer list. - $dbresult[$dbdata['locationid']]['computer'][] = $pc; + // Add the array to the computer list. + $dbresult[$dbdata['locationid']]['computer'][] = $pc; + } } // The array keys are only used for the isset -> Return only the values. @@ -348,22 +123,25 @@ function getRoomInfo($idList, $coords = false) { // ########## ########## function getOpeningTime($idList) { - - // Build SQL Query for multiple ids. - $query = "SELECT locationid, openingtime FROM `location_info` WHERE locationid IN ("; - $query .= implode(",", $idList); - $query .= ")"; - - // Execute query. - $dbquery = Database::simpleQuery($query); $dbresult = array(); - $handledIds = array(); - while($dbdata=$dbquery->fetch(PDO::FETCH_ASSOC)) { - $data['id'] = $dbdata['locationid']; - $data['openingtime'] = json_decode($dbdata['openingtime'], true); - $handledIds[] = $data['id']; - $dbresult[] = $data; + + if (!empty($idList)) { + // Build SQL Query for multiple ids. + $query = "SELECT locationid, openingtime FROM `location_info` WHERE locationid IN ("; + $query .= implode(",", $idList); + $query .= ")"; + + // Execute query. + $dbquery = Database::simpleQuery($query); + $handledIds = array(); + while($dbdata=$dbquery->fetch(PDO::FETCH_ASSOC)) { + $data['id'] = $dbdata['locationid']; + $data['openingtime'] = json_decode($dbdata['openingtime'], true); + $handledIds[] = $data['id']; + $dbresult[] = $data; + } } + $finalArray = array(); $idList = array_diff($idList, $handledIds); foreach ($idList as $id) { @@ -455,3 +233,154 @@ function createBasicClosingTime() { } // ########## ########## + +function getConfig($locationID) { + $dbresult = Database::queryFirst("SELECT l.locationname, li.config, li.serverroomid, s.servertype, s.serverurl FROM `location_info` AS li + RIGHT JOIN `location` AS l ON l.locationid=li.locationid + LEFT JOIN `setting_location_info` AS s ON s.serverid=li.serverid + WHERE l.locationid=:locationID", array('locationID' => $locationID)); + $config = array(); + + $config = json_decode($dbresult['config'], true); + $config['room'] = $dbresult['locationname']; + $date = getdate(); + $config['time'] = $date['year'] . "-" . $date['mon'] . "-" . $date['mday'] . " " . $date['hours'] . ":" . $date['minutes'] . ":" . $date['seconds']; + + if($dbresult['servertype'] === "Frontend") { + $config['calendarqueryurl'] = $dbresult['serverurl'] . "/" . $dbresult['serverroomid'] . ".json"; + } + + if (empty($config)) { + echo json_encode(array()); + } else { + echo json_encode($config, JSON_UNESCAPED_SLASHES); + } +} + +function getPcStates($idList) { + $pcStates = array(); + + $roominfoList = json_decode(getRoomInfo($idList), true); + foreach ($roominfoList as $roomInfo) { + $result['id'] = $roomInfo['id']; + $idle = 0; + $occupied = 0; + $off = 0; + $broken = 0; + + foreach ($roomInfo['computer'] as $computer) { + if ($computer['pcState'] == 0) { + $idle++; + } elseif($computer['pcState'] == 1) { + $occupied++; + } elseif($computer['pcState'] == 2) { + $off++; + } elseif($computer['pcState'] == 3) { + $broken++; + } + } + + $result['idle'] = $idle; + $result['occupied'] = $occupied; + $result['off'] = $off; + $result['broken'] = $broken; + $pcStates[] = $result; + } + return json_encode($pcStates); +} + +function getRoomTree($idList) { + $roomTree = array(); + $filteredIdList = array(); + foreach ($idList as $id) { + $dbresult = Database::queryFirst("SELECT locationname FROM `location` WHERE locationid=:locationID", array('locationID' => $id)); + + if (!in_array($id, $filteredIdList)) { + $a['id'] = $id; + $a['name'] = $dbresult['locationname']; + $filteredIdList[] = $id; + $a['childs'] = getChildsRecursive($id, $filteredIdList); + $roomTree[] = $a; + } + } + + return json_encode($roomTree); +} + +function getChildsRecursive($id, &$filteredIdList) { + $dbquery = Database::simpleQuery("SELECT locationid, locationname FROM `location` WHERE parentlocationid=:locationID", array('locationID' => $id)); + $array = array(); + $dbarray = array(); + + while($dbresult=$dbquery->fetch(PDO::FETCH_ASSOC)) { + $dbarray[] = $dbresult; + } + foreach ($dbarray as $db) { + $i = $db['locationid']; + + if (!in_array($i, $filteredIdList)) { + $a['id'] = $i; + $a['name'] = $db['locationname']; + $filteredIdList[] = $i; + $a['childs'] = getChildsRecursive($i, $filteredIdList); + $array[] = $a; + } + + } + + return $array; +} + +// ########## ########### +function getCalendar($idList) { + + $serverList = array(); + + if (!empty($idList)) { + //// Build SQL query for multiple ids. + $query = "SELECT locationid, l.serverid AS serverid, serverurl, servertype, credentials FROM `location_info` as l LEFT JOIN setting_location_info as s ON s.serverid = l.serverid WHERE locationid IN ("; + + $query .= implode(",", $idList); + + $query .= ") AND l.serverid = s.serverid ORDER BY servertype ASC, locationid ASC"; + + $dbquery = Database::simpleQuery($query); + + $first = true; + $lastservertype = ""; + while($dbresult=$dbquery->fetch(PDO::FETCH_ASSOC)) { + if (!isset($serverList[$dbresult['serverid']])) { + $serverList[$dbresult['serverid']] = array('credentials' => json_decode($dbresult['credentials'], true), 'url' => $dbresult['serverurl'], 'type' => $dbresult['servertype'], 'idlist' => array()); + } + $serverList[$dbresult['serverid']]['idlist'][] = $dbresult['locationid']; + } + } + + $resultarray = array(); + foreach ($serverList as $serverid => $server) { + $serverInstance = CourseBackend::getInstance($server['type']); + $setCred = $serverInstance->setCredentials($server['credentials'], $server['url'], $serverid); + + $calendarFromBackend = $serverInstance->fetchSchedule($server['idlist']); + $formattedArray = array(); + $resultarray = array(); + if ($calendarFromBackend === false || $setCred === false) { + $error['timestamp'] = time(); + $error['error'] = $serverInstance->getError(); + Database::exec("UPDATE `setting_location_info` Set error=:error WHERE serverid=:id", array('id' => $serverid, 'error' => json_encode($error, true))); + } else { + Database::exec("UPDATE `setting_location_info` Set error=NULL WHERE serverid=:id", array('id' => $serverid)); + } + if (is_array($calendarFromBackend)) { + foreach ($calendarFromBackend as $key => $value) { + $y['id'] = $key; + $y['calendar'] = $value; + $formattedArray[] = $y; + } + $resultarray = array_merge($resultarray, $formattedArray); + } + } + return json_encode($resultarray, true); +} + +// ########## ########## -- cgit v1.2.3-55-g7522