summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/inc/locationinfo.inc.php
blob: fa97a0c600922b8a78ffa56f593cff09468e05cd (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
<?php

class LocationInfo
{

	/**
	 * Gets the pc data and returns it's state.
	 *
	 * @param array $pc The pc data from the db. Array('logintime' =>, 'lastseen' =>, 'lastboot' =>)
	 * @return int pc state
	 */
	public static function getPcState($pc)
	{
		/*   pcState:
		 *  [0] =  IDLE (NOT IN USE)
		 *  [1] = OCCUPIED (IN USE)
		 *  [2] = OFF
		 *  [3] = 10 days offline (BROKEN?)
		 */
		// TODO USE STATE NAME instead of numbers

		$logintime = (int)$pc['logintime'];
		$lastseen = (int)$pc['lastseen'];
		$lastboot = (int)$pc['lastboot'];
		$NOW = time();

		if ($NOW - $lastseen > 14 * 86400) {
			return "BROKEN";
		} elseif (($NOW - $lastseen > 610) || $lastboot === 0) {
			return "OFF";
		} elseif ($logintime === 0) {
			return "IDLE";
		} elseif ($logintime > 0) {
			return "OCCUPIED";
		}
		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 `locationinfo_coursebackend` 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 `locationinfo_coursebackend` SET error = :error
					WHERE serverid = :id", array('id' => $serverId, 'error' => $error));
		}
	}

	/**
	 * Creates and returns a default config for room that didn't saved a config yet.
	 *
	 * @return array Return a default config.
	 */
	public static function defaultPanelConfig()
	{
		return array(
			'language' => 'en',
			'mode' => 1,
			'vertical' => false,
			'eco' => false,
			'scaledaysauto' => true,
			'daystoshow' => 7,
			'rotation' => 0,
			'scale' => 50,
			'switchtime' => 20,
			'calupdate' => 30,
			'roomupdate' => 15,
			'configupdate' => 180,
		);
	}

}