summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/api.inc.php
blob: ebea0ccbdc5d23838a30e021e2445b8963388521 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php

HandleParameters();

/**
 * Handles the API paramenters.
 */
function HandleParameters()
{

	$get = Request::get('get', 0, 'string');
	$uuid = Request::get('uuid', false, 'string');
	$output = false;
	if ($get === "machines") {
		$locationIds = getLocationsOr404($uuid);
		$output = array();
		appendMachineData($output, $locationIds, false);
		$output = array_values($output);
	} elseif ($get === "timestamp") {
		$output = array('ts' => getLastChangeTs($uuid));
	} elseif ($get === "config") {
		$output = getConfig($uuid);
	} elseif ($get === "pcstates") {
		$locationIds = getLocationsOr404($uuid);
		$output = getPcStates($locationIds);
	} elseif ($get === "locationtree") {
		$locationIds = getLocationsOr404($uuid);
		$output = getLocationTree($locationIds);
	} elseif ($get === "calendar") {
		$locationIds = getLocationsOr404($uuid);
		$output = getCalendar($locationIds);
	}
	if ($output !== false) {
		echo json_encode($output);
	} else {
		http_response_code(404);
		echo 'Unknown get option';
	}
}

/**
 * Return list of locationids associated with given panel.
 * @param string $paneluuid panel
 * @return int[] locationIds
 */
function getLocationsOr404($paneluuid)
{
	$panel = Database::queryFirst('SELECT locationids FROM locationinfo_panel WHERE paneluuid = :paneluuid',
		compact('paneluuid'));
	if ($panel !== false) {
		return array_map('intval', explode(',', $panel['locationids']));
	}
	http_response_code(404);
	die('Panel not found');
}

// ########## <Locationinfo> ##########
/**
 * Gets the location info of the given locations.
 * Append to passed array which is expected to
 * map location ids to properties of that location.
 * A new key 'machines' will be created in each
 * entry of $array that will take all the machine data.
 *
 * @param array $array location list to populate with machine data
 * @param bool $withPosition Defines if coords should be included or not.
 */
function appendMachineData(&$array, $idList = false, $withPosition = false)
{
	if (empty($array) && $idList === false)
		return;
	if ($idList === false) {
		$idList = array_keys($array);
	}

		$positionCol = $withPosition ? 'm.position,' : '';
	$query = "SELECT m.locationid, m.machineuuid, $positionCol m.logintime, m.lastseen, m.lastboot FROM machine m
				WHERE m.locationid IN (:idlist)";
	$dbquery = Database::simpleQuery($query, array('idlist' => $idList));

	// Iterate over matching machines
	while ($row = $dbquery->fetch(PDO::FETCH_ASSOC)) {
		settype($row['locationid'], 'int');
		if (!isset($array[$row['locationid']])) {
			$array[$row['locationid']] = array('id' => $row['locationid'], 'machines' => array());
		}
		if (!isset($array[$row['locationid']]['machines'])) {
			$array[$row['locationid']]['machines'] = array();
		}
		// Compact the pc data in one array.
		$pc = array('id' => $row['machineuuid']);
		if ($withPosition && !empty($row['position'])) {
			$position = json_decode($row['position'], true);
			if (isset($position['gridCol']) && isset($position['gridRow'])) {
				$pc['x'] = $position['gridCol'];
				$pc['y'] = $position['gridRow'];
				if (!empty($position['overlays']) && is_array($position['overlays'])) {
					$pc['overlays'] = $position['overlays'];
				}
			}
		}
		$pc['pcState'] = LocationInfo::getPcState($row);

		// Add the array to the machines list.
		$array[$row['locationid']]['machines'][] = $pc;
	}
}

// ########## </Locationinfo> ###########

/**
 * Returns all the passed location ids and appends
 * all their direct and indirect parent location ids.
 *
 * @param int[] $idList location ids
 * @return  int[] more location ids
 */
function getLocationsWithParents($idList)
{
	$locations = Location::getLocationsAssoc();
	$allIds = $idList;
	foreach ($idList as $id) {
		if (isset($locations[$id]) && isset($locations[$id]['parents'])) {
			$allIds = array_merge($allIds, $locations[$id]['parents']);
		}
	}
	return array_map('intval', $allIds);
}

// ########## <Openingtime> ##########
/**
 * Gets the Opening time of the given locations.
 *
 * @param int[] $idList list of locations
 * @return int modification timestamp of most recently updated locationconfig
 */
function appendOpeningTimes(&$array, $idList)
{
	// First, lets get all the parent ids for the given locations
	// in case we need to get inherited opening times
	$allIds = getLocationsWithParents($idList);
	if (empty($allIds))
		return 0;
	$latest = 0;
	$res = Database::simpleQuery("SELECT locationid, openingtime, lastchange FROM locationinfo_locationconfig
		WHERE locationid IN (:lids)", array('lids' => $allIds));
	$openingTimes = array();
	while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
		$openingTimes[(int)$row['locationid']] = $row;
		if (in_array($row['locationid'], $idList)) {
			$latest = max($row['lastchange'], $latest);
		}
	}
	// Now we got all the calendars for locations and parents
	// Iterate over the locations we're actually interested in
	$locations = Location::getLocationsAssoc();
	foreach ($idList as $locationId) {
		// Start checking at actual location...
		$currentId = $locationId;
		while ($currentId !== 0) {
			if (!empty($openingTimes[$currentId]['openingtime'])) {
				$cal = json_decode($openingTimes[$currentId]['openingtime'], true);
				if (is_array($cal)) {
					$cal = formatOpeningtime($cal);
				}
				if (!empty($cal)) {
					// Got a valid calendar
					if (!isset($array[$locationId])) {
						$array[$locationId] = array('id' => $locationId);
					}
					$array[$locationId]['openingtime'] = $cal;
					break;
				}
			}
			// Keep trying with parent
			$currentId = $locations[$currentId]['parentlocationid'];
		}
	}
	return $latest;
}

/**
 * Format the openingtime in the frontend needed format.
 * One key per week day, wich contains an array of {
 * 'HourOpen' => hh, 'MinutesOpen' => mm,
 * 'HourClose' => hh, 'MinutesClose' => mm }
 *
 * @param array $openingtime The opening time in the db saved format.
 * @return mixed The opening time in the frontend needed format.
 */
function formatOpeningtime($openingtime)
{
	$result = array();
	foreach ($openingtime as $entry) {
		$openTime = explode(':', $entry['openingtime']);
		$closeTime = explode(':', $entry['closingtime']);
		if (count($openTime) !== 2 || count($closeTime) !== 2)
			continue;
		$convertedTime = array(
			'HourOpen' => $openTime[0],
			'MinutesOpen' => $openTime[1],
			'HourClose' => $closeTime[0],
			'MinutesClose' => $closeTime[1],
		);
		foreach ($entry['days'] as $day) {
			if (!isset($result[$day])) {
				$result[$day] = array();
			}
			$result[$day][] = $convertedTime;
		}
	}
	return $result;
}

// ########## </Openingtime> ##########
/**
 * Gets the config of the location.
 *
 * @param int $locationID ID of the location
 * @return array configuration struct
 */
function getConfig($paneluuid)
{
	$panel = Database::queryFirst('SELECT panelconfig, paneltype, locationids, lastchange FROM locationinfo_panel WHERE paneluuid = :paneluuid',
		compact('paneluuid'));

	if ($panel === false || empty($panel['locationids'])) {
		http_response_code(404);
		die('Panel not found');
	}

	$config = LocationInfo::defaultPanelConfig($panel['paneltype']);
	$locations = Location::getLocationsAssoc();
	$overrides = false;

	if (!empty($panel['panelconfig'])) {
		$json = json_decode($panel['panelconfig'], true);
		if (is_array($json)) {
			if (isset($json['overrides']) && is_array($json['overrides'])) {
				$overrides = $json['overrides'];
			}
			unset($json['overrides']);
			$config = $json + $config;
		}
	}
	$config['locations'] = array();
	$lids = array_map('intval', explode(',', $panel['locationids']));
	foreach ($lids as $lid) {
		$config['locations'][$lid] = array(
			'id' => $lid,
			'name' => isset($locations[$lid]) ? $locations[$lid]['locationname'] : 'noname00.pas',
		);
		if (isset($overrides[$lid]) && is_array($overrides[$lid])) {
			$config['locations'][$lid]['overrides'] = $overrides[$lid];
		}
	}
	appendMachineData($config['locations'], $lids, true);
	$locChange = appendOpeningTimes($config['locations'], $lids);

	$config['ts'] = max($panel['lastchange'], $locChange);
	$config['locations'] = array_values($config['locations']);
	$config['time'] = date('Y-m-d H:i:s');

	return $config;
}

/**
 * Get last config modification timestamp for given panel. This checks
 * the modification of the panel config itself as well as all involved locations
 *
 * @param $paneluuid
 * @return mixed
 */
function getLastChangeTs($paneluuid)
{
	$panel = Database::queryFirst('SELECT paneltype, locationids, lastchange FROM locationinfo_panel WHERE paneluuid = :paneluuid',
		compact('paneluuid'));
	if ($panel === false) {
		http_response_code(404);
		die('Panel not found');
	}
	$latest = $panel['lastchange'];
	// TODO: summary
	$lids = explode(',', $panel['locationids']);
	$res = Database::simpleQuery('SELECT lastchange FROM locationinfo_locationconfig WHERE locationid IN (:lids)',
		compact('lids'));
	while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
		$latest = max($row['lastchange'], $latest);
	}
	return (int)$latest;
}

/**
 * Gets the pc states of the given locations.
 *
 * @param int[] $idList list of the location ids.
 * @return array aggregated PC states
 */
function getPcStates($idList)
{
	$pcStates = array();
	foreach ($idList as $id) {
		$pcStates[$id] = array(
			'id' => $id,
			'idle' => 0,
			'occupied' => 0,
			'off' => 0,
			'broken' => 0,
		);
	}

	$locationInfoList = array();
	appendMachineData($locationInfoList, $idList);
	foreach ($locationInfoList as $locationInfo) {
		$id = $locationInfo['id'];
		foreach ($locationInfo['machines'] as $pc) {
			$key = strtolower($pc['pcState']);
			if (isset($pcStates[$id][$key])) {
				$pcStates[$id][$key]++;
			}
		}
	}

	return array_values($pcStates);
}

/**
 * Gets the location tree of the given locations.
 *
 * @param int[] $idList Array list of the locations.
 * @return array location tree data
 */
function getLocationTree($idList)
{
	if (in_array(0, $idList)) {
		return array_values(Location::getTree());
	}
	$locations = Location::getTree();

	$ret = findLocations($locations, $idList);
	return $ret;
}

function findLocations($locations, $idList)
{
	$ret = array();
	foreach ($locations as $location) {
		if (in_array($location['locationid'], $idList)) {
			$ret[] = $location;
		} elseif (!empty($location['children'])) {
			$ret = array_merge($ret, findLocations($location['children'], $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 locationid ' . $server['locationid']
				. ': Backend type ' . $server['type'] . ' unknown. Disabling location.');
			Database::exec("UPDATE locationinfo_locationconfig SET serverid = 0 WHERE locationid = :lid",
				array('lid' => $server['locationid']));
			continue;
		}
		$credentialsOk = $serverInstance->setCredentials($serverid, $server['credentials']);

		if ($credentialsOk) {
			$calendarFromBackend = $serverInstance->fetchSchedule($server['idlist']);
		} else {
			$calendarFromBackend = array();
		}

		LocationInfo::setServerError($serverid, $serverInstance->getError());

		if (is_array($calendarFromBackend)) {
			foreach ($calendarFromBackend as $key => $value) {
				$resultArray[] = array(
					'id' => $key,
					'calendar' => $value,
				);
			}
		}
	}
	return $resultArray;
}

// ########## </Calendar> ##########