summaryrefslogtreecommitdiffstats
path: root/modules-available/roomplanner/install.inc.php
blob: 05fd7589bbe7811a25706d9864d606b65cdb9afb (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
<?php


$res = array();

$res[] = tableCreate('location_roomplan', "
	`locationid` INT(11) NOT NULL,
	`managerip` varchar(45) CHARACTER SET ascii DEFAULT '',
	`tutoruuid` char(36) CHARACTER SET ascii DEFAULT NULL,
	`roomplan`   BLOB	 DEFAULT NULL,
	PRIMARY KEY (`locationid`),
 	KEY `tutoruuid` (`tutoruuid`),
 	KEY `managerip` (`managerip`)");

if (!tableHasColumn('location_roomplan', 'managerip')) {
	$ret = Database::exec("ALTER TABLE `location_roomplan` ADD COLUMN `managerip` varchar(45) CHARACTER SET ascii DEFAULT '' AFTER locationid,"
		. " ADD KEY `managerip` (`managerip`)") !== false;
	if ($ret === false) {
		finalResponse(UPDATE_FAILED, 'Adding managerip to location_roomplan failed: ' . Database::lastError());
	}
	$res[] = UPDATE_DONE;
}
if (!tableHasColumn('location_roomplan', 'tutoruuid')) {
	$ret = Database::exec("ALTER TABLE `location_roomplan` ADD COLUMN `tutoruuid` char(36) CHARACTER SET ascii DEFAULT NULL AFTER managerip,"
		. " ADD KEY `tutoruuid` (`tutoruuid`)") !== false;
	if ($ret === false) {
		finalResponse(UPDATE_FAILED, 'Adding tutoruuid to location_roomplan failed: ' . Database::lastError());
	}
	$res[] = UPDATE_DONE;
}

if (in_array(UPDATE_DONE, $res)) {
	Database::exec("ALTER TABLE `location_roomplan`
		ADD CONSTRAINT `location_roomplan_ibfk_1` FOREIGN KEY (`locationid`) REFERENCES `location` (`locationid`) ON DELETE CASCADE");
	Database::exec("ALTER TABLE `location_roomplan`
		ADD CONSTRAINT `location_roomplan_ibfk_2` FOREIGN KEY (`tutoruuid`) REFERENCES `machine` (`machineuuid`) ON DELETE SET NULL ON UPDATE CASCADE");
}

// 2017-11-30: Refactor to runmode
// managerip, dedicatedmgr,  --> runmode
if (tableHasColumn('location_roomplan', 'dedicatedmgr')) {
	if (!tableExists('runmode') || !tableExists('machine')) {
		$res[] = UPDATE_RETRY;
	} else {
		$ret = Database::simpleQuery('SELECT lr.locationid, lr.managerip, lr.dedicatedmgr, m.machineuuid
			FROM location_roomplan lr INNER JOIN machine m ON (m.clientip = lr.managerip)');
		if ($ret === false) {
			$res[] = UPDATE_FAILED;
		} else {
			foreach ($ret as $row) {
				$dedi = $row['dedicatedmgr'] != 0;
				$data = json_encode(array('dedicatedmgr' => $dedi));
				Database::exec("INSERT IGNORE INTO runmode (machineuuid, module, modeid, modedata, isclient)
				VALUES (:machineuuid, 'roomplanner', :locationid, :modedata, :isclient)", array(
					'machineuuid' => $row['machineuuid'],
					'locationid' => $row['locationid'],
					'modedata' => $data,
					'isclient' => ($dedi ? 0 : 1)
				));
			}
			Database::exec('ALTER TABLE location_roomplan DROP COLUMN dedicatedmgr');
			$res[] = UPDATE_DONE;
		}
	}
}

responseFromArray($res);