summaryrefslogtreecommitdiffstats
path: root/modules-available/rebootcontrol/inc/rebootqueries.inc.php
blob: 063b36e4a3911cf1dc96e6122dfbe7ae5a197701 (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
<?php

class RebootQueries
{

	// Get Client+IP+CurrentVM+CurrentUser+Location to fill the table
	public static function getMachineTable($locationId) {
		$queryArgs = array('cutoff' => strtotime('-30 days'));
		if ($locationId === 0) {
			$where = 'machine.locationid IS NULL';
		} else {
			$where = 'machine.locationid = :locationid';
			$queryArgs['locationid'] = $locationId;
		}
		$leftJoin = '';
		$sessionField = 'machine.currentsession';
		if (Module::get('dozmod') !== false) {
			// SELECT lectureid, displayname FROM sat.lecture WHERE lectureid = :lectureid
			$leftJoin = 'LEFT JOIN sat.lecture ON (lecture.lectureid = machine.currentsession)';
			$sessionField = 'IFNULL(lecture.displayname, machine.currentsession) AS currentsession';
		}
		$res = Database::simpleQuery("
			SELECT machine.machineuuid, machine.hostname, machine.clientip,
				machine.lastboot, machine.lastseen, machine.logintime, machine.state,
				$sessionField, machine.currentuser, machine.locationid
			FROM machine 
			$leftJoin
			WHERE $where AND machine.lastseen > :cutoff", $queryArgs);
		$ret = $res->fetchAll(PDO::FETCH_ASSOC);
		foreach ($ret as &$row) {
			if ($row['state'] === 'IDLE' || $row['state'] === 'OCCUPIED') {
				$row['status'] = 1;
			} else {
				$row['status'] = 0;
			}
			if ($row['state'] !== 'OCCUPIED') {
				$row['currentuser'] = '';
				$row['currentsession'] = '';
			}
		}
		return $ret;
	}

	/**
	 * Get machines by list of UUIDs
	 * @param string[] $list list of system UUIDs
	 * @return array list of machines with machineuuid, hostname, clientip, state and locationid
	 */
	public static function getMachinesByUuid($list)
	{
		if (empty($list))
			return array();
		$res = Database::simpleQuery("SELECT machineuuid, hostname, clientip, state, locationid FROM machine
				WHERE machineuuid IN (:list)", compact('list'));
		return $res->fetchAll(PDO::FETCH_ASSOC);
	}

}