From 601faab88b09699ef454aac094b60de8a80a7168 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Mon, 20 Feb 2017 17:38:26 -0800 Subject: API: roominfo is now supporting mutliple id. --- modules-available/locationinfo/api.inc.php | 81 ++++++++++++++++++------------ 1 file changed, 48 insertions(+), 33 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 dd1e976f..78c9f197 100644 --- a/modules-available/locationinfo/api.inc.php +++ b/modules-available/locationinfo/api.inc.php @@ -6,12 +6,13 @@ function HandleParameters() { $getAction = Request::get('action', 0, 'string'); if ($getAction == "roominfo") { - $getRoomID = Request::get('id', 0, 'int'); + $roomIDs = Request::get('id', 0, 'string'); + $array = getMultipleInformations($roomIDs); $getCoords = Request::get('coords', 0, 'string'); if (empty($getCoords)) { $getCoords = '0'; } - getRoomInfoJson($getRoomID, $getCoords); + echo getRoomInfo($array, $getCoords); } elseif ($getAction == "openingtime") { $roomIDs = Request::get('id', 0, 'string'); $array = getMultipleInformations($roomIDs); @@ -230,52 +231,66 @@ function checkIfHidden($locationID) { return false; } -function getRoomInfoJson($locationID, $coords) { - $error = checkIfHidden($locationID); +// ########## ########## - $pcs = getPcInfos($locationID, $coords); +function getRoomInfo($idList, $coords) { - if (empty($pcs)) { - $error = true; - } + // Build SQL query for multiple ids. + $query = "SELECT locationid, machineuuid, position, logintime, lastseen, lastboot FROM `machine` WHERE "; + $or = false; + foreach($idList as $id) { + if (checkIfHidden($id)) { + continue; + } + if ($or) { + $query .= " OR "; + } - if ($error == true) { - echo "ERROR"; - } else { - echo $pcs; + $query .= "locationid = " . $id; + $or = true; } -} -function getPcInfos($locationID, $coords) { - if ($coords == '1') { - $dbquery = Database::simpleQuery("SELECT machineuuid, position, logintime, lastseen, lastboot FROM `machine` WHERE locationid = :locationID" , array('locationID' => $locationID)); - } else { - $dbquery = Database::simpleQuery("SELECT machineuuid, logintime, lastseen, lastboot FROM `machine` WHERE locationid = :locationID" , array('locationID' => $locationID)); - } + $query .= " ORDER BY locationid ASC"; + // Execute query. + $dbquery = Database::simpleQuery($query); + $dbresult = array(); - $pcs = array(); + $currentlocationid = 0; - while($pc=$dbquery->fetch(PDO::FETCH_ASSOC)) { + $lastentry; + $pclist = array(); + while($dbdata=$dbquery->fetch(PDO::FETCH_ASSOC)) { - $computer = array(); + // Add the data in the array if locationid changed + if ($dbdata['locationid'] != $currentlocationid && $currentlocationid != 0) { + $data['id'] = $currentlocationid; + $data['computer'] = $pclist; + $dbresult[] = $data; + $pclist = array(); + } - $computer['id'] = $pc['machineuuid']; + $pc['id'] = $dbdata['machineuuid']; - if ($coords == '1') { - $position = json_decode($pc['position'], true); - $computer['x'] = $position['gridCol']; - $computer['y'] = $position['gridRow']; - } + // Add coordinates if bool = true. + if ($coords == '1') { + $position = json_decode($dbdata['position'], true); + $pc['x'] = $position['gridCol']; + $pc['y'] = $position['gridRow']; + } - $computer['pcState'] = LocationInfo::getPcState($pc); + $pc['pcState'] = LocationInfo::getPcState($dbdata); + $pclist[] = $pc; - $pcs[] = $computer; + // Save the last entry to add this at the end. + $lastentry['id'] = $dbdata['locationid']; + $lastentry['computer'] = $pclist; + $currentlocationid = $dbdata['locationid']; } + $dbresult[] = $lastentry; - $str = json_encode($pcs, true); - - return $str; + return json_encode($dbresult, true); } +// ########## ########## // ########## ########## -- cgit v1.2.3-55-g7522