summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2017-04-18 15:53:07 +0200
committerSimon Rettberg2017-04-18 15:53:07 +0200
commit865db83c68f919d2ecf6fa5c0fed37f70541ba15 (patch)
treec1f10a2d4a8a30edf8881d7b2ac7f093a1f3795e
parent[locations] Make Location::getTree() public (diff)
downloadslx-admin-865db83c68f919d2ecf6fa5c0fed37f70541ba15.tar.gz
slx-admin-865db83c68f919d2ecf6fa5c0fed37f70541ba15.tar.xz
slx-admin-865db83c68f919d2ecf6fa5c0fed37f70541ba15.zip
[locationinfo] Use Location helper for getRoomTree, check backend validity in getCalendar
-rw-r--r--modules-available/locationinfo/api.inc.php121
-rw-r--r--modules-available/locationinfo/frontend/panel.html64
-rw-r--r--modules-available/locationinfo/inc/locationinfo.inc.php24
-rw-r--r--modules-available/locationinfo/page.inc.php27
4 files changed, 75 insertions, 161 deletions
diff --git a/modules-available/locationinfo/api.inc.php b/modules-available/locationinfo/api.inc.php
index 4792a103..55b220c3 100644
--- a/modules-available/locationinfo/api.inc.php
+++ b/modules-available/locationinfo/api.inc.php
@@ -97,20 +97,15 @@ function getRoomInfo($idList, $coords = false)
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 = "SELECT l.locationid, m.machineuuid, m.position, m.logintime, m.lastseen, m.lastboot FROM location_info AS l
+ LEFT JOIN `machine` AS m ON l.locationid = m.locationid WHERE l.hidden = 0 AND l.locationid IN (";
$query .= implode(",", $idList);
$query .= ")";
- $query .= " ORDER BY m.locationid ASC";
-
// Execute query.
$dbquery = Database::simpleQuery($query);
- $currentlocationid = 0;
-
- $pclist = array();
-
// Fetch db data.
while ($dbdata = $dbquery->fetch(PDO::FETCH_ASSOC)) {
@@ -119,6 +114,10 @@ function getRoomInfo($idList, $coords = false)
$dbresult[$dbdata['locationid']] = array('id' => $dbdata['locationid'], 'computer' => array());
}
+ // Left join, no data
+ if (empty($dbdata['machineuuid']))
+ continue;
+
// Compact the pc data in one array.
$pc['id'] = $dbdata['machineuuid'];
if ($coordinates == '1' || $coordinates == 'true') {
@@ -379,58 +378,28 @@ function getPcStates($idList)
/**
* Gets the room tree of the given locations.
*
- * @param $idList Array list of the locations.
+ * @param int[] $idList Array list of the locations.
* @return string Room tree JSON.
*/
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;
- }
- }
+ $locations = Location::getTree();
- return json_encode($roomTree);
+ $ret = findRooms($locations, $idList);
+ return json_encode($ret);
}
-/**
- * Recursively gets the Childs of a location.
- *
- * @param $id Location id you want all childs from.
- * @param $filteredIdList Pointer to the filtered list to avoid double ids.
- * @return array List with all the Childs.
- */
-function getChildsRecursive($id, &$filteredIdList)
+function findRooms($locations, $idList)
{
- $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;
+ $ret = array();
+ foreach ($locations as $location) {
+ if (in_array($location['locationid'], $idList)) {
+ $ret[] = $location;
+ } elseif (!empty($location['children'])) {
+ $ret = array_merge($ret, findRooms($location['children'], $idList));
}
-
}
-
- return $array;
+ return $ret;
}
// ########## <Calendar> ###########
@@ -446,19 +415,15 @@ function getCalendar($idList)
if (!empty($idList)) {
// Build SQL query for multiple ids.
- $query = "SELECT locationid, l.serverid AS serverid, serverurl, servertype, credentials
+ $qs = '?' . str_repeat(',?', count($idList) - 1);
+ $query = "SELECT l.locationid, l.serverid, l.serverroomid, s.serverurl, s.servertype, s.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";
+ INNER JOIN setting_location_info AS s ON (s.serverid = l.serverid)
+ WHERE l.hidden = 0 AND l.locationid IN ($qs)
+ ORDER BY s.servertype ASC";
- $dbquery = Database::simpleQuery($query);
+ $dbquery = Database::simpleQuery($query, array_values($idList));
- $first = true;
- $lastservertype = "";
while ($dbresult = $dbquery->fetch(PDO::FETCH_ASSOC)) {
if (!isset($serverList[$dbresult['serverid']])) {
$serverList[$dbresult['serverid']] = array(
@@ -472,36 +437,36 @@ function getCalendar($idList)
}
}
- $resultarray = array();
+ $resultArray = array();
foreach ($serverList as $serverid => $server) {
$serverInstance = CourseBackend::getInstance($server['type']);
- $setCred = $serverInstance->setCredentials($server['credentials'], $server['url'], $serverid);
-
- $calendarFromBackend = array();
- if ($setCred) {
- $calendarFromBackend = $serverInstance->fetchSchedule($server['idlist']);
+ if ($serverInstance === false) {
+ EventLog::warning('Cannot fetch schedule for locationid ' . $server['locationid']
+ . ': Backend type ' . $server['type'] . ' unknown. Disabling location.');
+ Database::exec("UPDATE location_info SET serverid = 0 WHERE locationid = :lid",
+ array('lid' => $server['locationid']));
+ continue;
}
+ $credentialsOk = $serverInstance->setCredentials($server['credentials'], $server['url'], $serverid);
- $formattedArray = 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)));
+ if ($credentialsOk) {
+ $calendarFromBackend = $serverInstance->fetchSchedule($server['idlist']);
} else {
- Database::exec("UPDATE `setting_location_info` SET error=NULL WHERE serverid=:id",
- array('id' => $serverid));
+ $calendarFromBackend = array();
}
+
+ LocationInfo::setServerError($serverid, $serverInstance->getError());
+
if (is_array($calendarFromBackend)) {
foreach ($calendarFromBackend as $key => $value) {
- $y['id'] = $key;
- $y['calendar'] = $value;
- $formattedArray[] = $y;
+ $resultArray[] = array(
+ 'id' => $key,
+ 'calendar' => $value,
+ );
}
- $resultarray = array_merge($resultarray, $formattedArray);
}
}
- return json_encode($resultarray, true);
+ return json_encode($resultArray);
}
// ########## </Calendar> ##########
diff --git a/modules-available/locationinfo/frontend/panel.html b/modules-available/locationinfo/frontend/panel.html
index 89d1c144..cd4db81f 100644
--- a/modules-available/locationinfo/frontend/panel.html
+++ b/modules-available/locationinfo/frontend/panel.html
@@ -259,7 +259,7 @@
for (var t = 0; t < ids.length; t++) {
for (var i = 0; i < json.length; i++) {
- if (ids[t] == json[i].id) {
+ if (ids[t] == json[i].locationid) {
var el = generateObject(json[i], ($("#main")), true);
}
@@ -277,13 +277,12 @@
*/
function generateObject(json, myParent, outermost) {
var obj;
- if (json.childs.length == 0) {
- obj = generateChild(myParent, json.id, json.name, outermost);
+ if (!json.children || json.children.length == 0) {
+ obj = generateChild(myParent, json.locationid, json.locationname, outermost);
} else {
- obj = generateParent(myParent, json.id, json.name, outermost);
- for (var i = 0; i < json.childs.length; i++) {
-
- generateObject(json.childs[i], $("#parent_" + json.id));
+ obj = generateParent(myParent, json.locationid, json.locationname, outermost);
+ for (var i = 0; i < json.children.length; i++) {
+ generateObject(json.children[i], $("#parent_" + json.locationid));
}
}
return obj;
@@ -291,57 +290,6 @@
}
/**
- * Retruns the number of childs of the current node (inklusive childs of childs)
- * @param json Json of the room tree
- * @returns number of childs of the current node
- */
- function getAllchildsRekusivly(json) {
- var childs = 0;
- for (var i = 0; i < json.childs.length; i++) {
- childs++;
- if (json.childs[i].childs.length != 0) {
- childs = getAllchildsRekusivly(json.childs[i]);
- }
- }
- return childs;
- }
-
- /**
- * Retruns the number of leaveschilds of the current node (inklusive leaves childs of childs)
- * @param json Json of the room tree
- * @returns number of leaveschilds of the current node
- */
- function getChildsRekusivly(json) {
- var childs = 0;
- for (var i = 0; i < json.childs.length; i++) {
- if (json.childs[i].childs.length == 0) {
- childs++;
- } else {
- childs = getChildsRekusivly(json.childs[i]);
- }
-
- }
- return childs;
- }
-
- /**
- * Retruns the number of leaves childs of the current node
- * @param json Json of the room tree
- * @returns number of leaveschilds of the current node
- */
- function getChilds(json) {
-
- var childs = 0;
- for (var i = 0; i < json.childs.length; i++) {
- if (json.childs[i].childs.length == 0) {
- childs++;
- }
- }
- return childs;
- }
-
-
- /**
* Helper function to generate id string used in query functions
* @param list A string, wicht contains ids or not(for now)
* @param id An ID which should be added to the list
diff --git a/modules-available/locationinfo/inc/locationinfo.inc.php b/modules-available/locationinfo/inc/locationinfo.inc.php
index 9bd5ff5b..7617d143 100644
--- a/modules-available/locationinfo/inc/locationinfo.inc.php
+++ b/modules-available/locationinfo/inc/locationinfo.inc.php
@@ -36,4 +36,28 @@ class LocationInfo
return -1;
}
+ /**
+ * Set current error message of given server. Pass null or false to clear.
+ *
+ * @param int $serverId id of server
+ * @param string $message error message to set, null or false clears error.
+ */
+ public static function setServerError($serverId, $message)
+ {
+ if ($message === false || $message === null) {
+ Database::exec("UPDATE `setting_location_info` SET error = NULL
+ WHERE serverid = :id", array('id' => $serverId));
+ } else {
+ if (empty($message)) {
+ $message = '<empty error message>';
+ }
+ $error = json_encode(array(
+ 'timestamp' => time(),
+ 'error' => (string)$message
+ ));
+ Database::exec("UPDATE `setting_location_info` SET error = :error
+ WHERE serverid = :id", array('id' => $serverId, 'error' => $error));
+ }
+ }
+
}
diff --git a/modules-available/locationinfo/page.inc.php b/modules-available/locationinfo/page.inc.php
index d5f98cce..31702dff 100644
--- a/modules-available/locationinfo/page.inc.php
+++ b/modules-available/locationinfo/page.inc.php
@@ -286,7 +286,7 @@ class Page_LocationInfo extends Page
$serverInstance = CourseBackend::getInstance($dbresult['servertype']);
if ($serverInstance === false) {
- $this->setServerError($serverid, 'Unknown backend type: ' . $dbresult['servertype']);
+ LocationInfo::setServerError($serverid, 'Unknown backend type: ' . $dbresult['servertype']);
return;
}
$credentialsOk = $serverInstance->setCredentials(json_decode($dbresult['credentials'], true), $dbresult['serverurl'], $serverid);
@@ -295,32 +295,9 @@ class Page_LocationInfo extends Page
$connectionOk = $serverInstance->checkConnection();
}
- if (!$credentialsOk || !$connectionOk) {
- $this->setServerError($serverid, $serverInstance->getError());
- } else {
- $this->setServerError($serverid, null);
- }
+ LocationInfo::setServerError($serverid, $serverInstance->getError());
}
- /**
- * Set current error message of given server. Pass empty message to clear.
- * @param int $serverid id of server
- * @param string $message error message to set
- */
- private function setServerError($serverid, $message)
- {
- if (empty($message)) {
- Database::exec("UPDATE `setting_location_info` SET error = NULL
- WHERE serverid = :id", array('id' => $serverid));
- } else {
- $error = json_encode(array(
- 'timestamp' => time(),
- 'error' => $message
- ));
- Database::exec("UPDATE `setting_location_info` SET error = :error
- WHERE serverid = :id", array('id' => $serverid, 'error' => $error));
- }
- }
/**
* Sets the new hidden value and checks childs and parents.