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

$res = array();

$t1 = $res[] = tableCreate('locationinfo_locationconfig', '
	`locationid` INT(11) NOT NULL,
	`serverid` INT(10) UNSIGNED,
	`serverlocationid` VARCHAR(150),
	`calendar` BLOB,
	`lastcalendarupdate` INT(10) UNSIGNED NOT NULL DEFAULT 0,
	`lastuse` INT(10) UNSIGNED NOT NULL DEFAULT 0,
	`lastchange` int(10) UNSIGNED NOT NULL DEFAULT 0,
	PRIMARY KEY (`locationid`)
');

$t2 = $res[] = tableCreate('locationinfo_coursebackend', '
	`serverid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`servername` VARCHAR(200) NOT NULL,
	`servertype` VARCHAR(100) NOT NULL,
	`credentials` BLOB,
	`error` VARCHAR(500),
	PRIMARY KEY (`serverid`),
	KEY `servername` (`servername`)
');

$t3 = $res[] = tableCreate('locationinfo_panel', "
	`paneluuid` char(36) CHARACTER SET ascii NOT NULL,
	`panelname` varchar(30) NOT NULL,
	`locationids` varchar(100) CHARACTER SET ascii NOT NULL,
	`paneltype` enum('DEFAULT','SUMMARY', 'URL') NOT NULL,
	`panelconfig` blob NOT NULL,
	`lastchange` int(10) UNSIGNED NOT NULL DEFAULT 0,
	PRIMARY KEY (`paneluuid`),
	KEY `panelname` (`panelname`)
");

// 2019-03-06: Add logging table
$res[] = tableCreate('locationinfo_backendlog', "
	`logid` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`serverid` int(10) UNSIGNED NOT NULL,
	`dateline` int(10) UNSIGNED NOT NULL,
	`message` varchar(500) NOT NULL,
	PRIMARY KEY (`logid`),
	KEY (`serverid`)
");

// Update

if ($t1 === UPDATE_NOOP) {
	if ($t3 === UPDATE_DONE) {
		// Upgrade from old beta version - convert panels
		Database::exec('INSERT INTO locationinfo_panel (paneluuid, panelname, locationids, paneltype, panelconfig, lastchange)'
			. " SELECT UUID(), Concat('Import: ', l.locationname), o.locationid, 'DEFAULT', o.config, 0 "
			. " FROM locationinfo_locationconfig o INNER JOIN location l USING (locationid)"
			. ' WHERE Length(o.config) > 10');
	}
	Database::exec("ALTER TABLE locationinfo_locationconfig CHANGE `serverid` `serverid` INT(10) UNSIGNED NULL");
	tableDropColumn('locationinfo_locationconfig', 'hidden');
	tableDropColumn('locationinfo_locationconfig', 'config');
	if (!tableHasColumn('locationinfo_locationconfig', 'lastchange')) {
		$ret = Database::exec('ALTER TABLE locationinfo_locationconfig ADD `lastchange` INT(10) UNSIGNED NOT NULL DEFAULT 0');
		if ($ret === false) {
			finalResponse(UPDATE_FAILED, 'Could not add lastchange field');
		} elseif ($ret > 0) {
			$res[] = UPDATE_DONE;
		}
	}
}
if ($t1 === UPDATE_DONE || $t2 === UPDATE_DONE) {
	Database::exec('UPDATE locationinfo_locationconfig SET serverid = NULL WHERE serverid = 0');
	Database::exec('ALTER TABLE `locationinfo_locationconfig` ADD CONSTRAINT `locationinfo_locationconfig_ibfk_1` FOREIGN KEY ( `serverid` )
			REFERENCES `locationinfo_coursebackend` (`serverid`) ON DELETE SET NULL ON UPDATE CASCADE');
}
if ($t1 === UPDATE_DONE) {
	if (false === Database::exec('ALTER TABLE `locationinfo_locationconfig` ADD CONSTRAINT `locationinfo_locationconfig_ibfk_2` FOREIGN KEY ( `locationid` )
			REFERENCES `location` (`locationid`) ON DELETE CASCADE ON UPDATE CASCADE')) {
		$res[] = UPDATE_RETRY;
	}
}

if ($t3 === UPDATE_NOOP) {
	Database::exec("ALTER TABLE `locationinfo_panel` CHANGE `paneltype`
		`paneltype` ENUM('DEFAULT', 'SUMMARY', 'URL') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL");
	// 2017-12-02 expand locationids column
	Database::exec("ALTER TABLE `locationinfo_panel` CHANGE `locationids`
		`locationids` varchar(100) CHARACTER SET ascii NOT NULL");
}

// 2017-07-26 Add servername key
Database::exec("ALTER TABLE `locationinfo_coursebackend` ADD KEY `servername` (`servername`)");

// 2019-02-20 Add lastuse column
if (!tableHasColumn('locationinfo_locationconfig', 'lastuse')) {
	if (Database::exec("ALTER TABLE locationinfo_locationconfig
			ADD `lastuse` INT(10) UNSIGNED NOT NULL DEFAULT 0 AFTER `lastcalendarupdate`") === false) {
		finalResponse(UPDATE_FAILED, 'Could not add lastuse column');
	}
	$res[] = UPDATE_DONE;
}

// 2019-03-06: Add logging table constraint
if (tableGetConstraints('locationinfo_backendlog', 'serverid',
		'locationinfo_coursebackend', 'serverid') === false) {
	$res[] = tableAddConstraint('locationinfo_backendlog', 'serverid',
		'locationinfo_coursebackend', 'serverid', 'ON UPDATE CASCADE ON DELETE CASCADE');
}

// Create response for browser

if (in_array(UPDATE_RETRY, $res)) {
	finalResponse(UPDATE_RETRY, 'Please retry: ' . Database::lastError());
}
if (in_array(UPDATE_DONE, $res)) {
	finalResponse(UPDATE_DONE, 'Tables created successfully');
}

finalResponse(UPDATE_NOOP, 'Everything already up to date');