-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatabase.php
247 lines (195 loc) · 8.22 KB
/
database.php
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
<?php
// To allow this to be called directly or from admin/upgrade.php
if ( !isset($PDOX) ) {
require_once "../config.php";
$CURRENT_FILE = __FILE__;
require $CFG->dirroot."/admin/migrate-setup.php";
}
// Dropping tables
$DATABASE_UNINSTALL = array(
"drop table if exists {$CFG->dbprefix}peer_flag",
"drop table if exists {$CFG->dbprefix}peer_grade",
"drop table if exists {$CFG->dbprefix}peer_submit",
"drop table if exists {$CFG->dbprefix}peer_assn"
);
// Creating tables
$DATABASE_INSTALL = array(
array( "{$CFG->dbprefix}peer_assn",
"create table {$CFG->dbprefix}peer_assn (
assn_id INTEGER NOT NULL KEY AUTO_INCREMENT,
link_id INTEGER NOT NULL,
due_at TIMESTAMP NULL,
json TEXT NULL,
updated_at TIMESTAMP NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT `{$CFG->dbprefix}peer_assn_ibfk_1`
FOREIGN KEY (`link_id`)
REFERENCES `{$CFG->dbprefix}lti_link` (`link_id`)
ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE(link_id)
) ENGINE = InnoDB DEFAULT CHARSET=utf8"),
array( "{$CFG->dbprefix}peer_submit",
"create table {$CFG->dbprefix}peer_submit (
submit_id INTEGER NOT NULL KEY AUTO_INCREMENT,
assn_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
json TEXT NULL,
note TEXT NULL,
reflect TEXT NULL,
regrade TINYINT NULL,
inst_points DOUBLE NULL,
inst_note TEXT NULL,
inst_id INTEGER NULL,
peer_marks INTEGER DEFAULT 0,
rating INTEGER NULL, -- Aggregate rating
updated_at TIMESTAMP NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT `{$CFG->dbprefix}peer_submit_ibfk_1`
FOREIGN KEY (`user_id`)
REFERENCES `{$CFG->dbprefix}lti_user` (`user_id`)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `{$CFG->dbprefix}peer_submit_ibfk_2`
FOREIGN KEY (`assn_id`)
REFERENCES `{$CFG->dbprefix}peer_assn` (`assn_id`)
ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE(assn_id, user_id)
) ENGINE = InnoDB DEFAULT CHARSET=utf8") ,
array( "{$CFG->dbprefix}peer_text",
"create table {$CFG->dbprefix}peer_text (
text_id INTEGER NOT NULL KEY AUTO_INCREMENT,
assn_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
data TEXT NULL,
json TEXT NULL,
updated_at TIMESTAMP NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT `{$CFG->dbprefix}peer_text_ibfk_1`
FOREIGN KEY (`assn_id`)
REFERENCES `{$CFG->dbprefix}peer_assn` (`assn_id`)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `{$CFG->dbprefix}peer_text_ibfk_2`
FOREIGN KEY (`user_id`)
REFERENCES `{$CFG->dbprefix}lti_user` (`user_id`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET=utf8") ,
array( "{$CFG->dbprefix}peer_grade",
"create table {$CFG->dbprefix}peer_grade (
grade_id INTEGER NOT NULL KEY AUTO_INCREMENT,
submit_id INTEGER NOT NULL,
user_id INTEGER NOT NULL, -- The user doing the grading
points DOUBLE NULL,
rating TINYINT NULL, -- Individual rating on this assignment +1 or -1
note TEXT NULL,
json TEXT NULL,
updated_at TIMESTAMP NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT `{$CFG->dbprefix}peer_grade_ibfk_1`
FOREIGN KEY (`submit_id`)
REFERENCES `{$CFG->dbprefix}peer_submit` (`submit_id`)
ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE(submit_id, user_id)
) ENGINE = InnoDB DEFAULT CHARSET=utf8") ,
array( "{$CFG->dbprefix}peer_flag",
"create table {$CFG->dbprefix}peer_flag (
flag_id INTEGER NOT NULL KEY AUTO_INCREMENT,
submit_id INTEGER NOT NULL,
grade_id INTEGER NULL,
user_id INTEGER NOT NULL, -- The user doing the flagging
note TEXT NULL,
response TEXT NULL,
handled BOOLEAN NOT NULL DEFAULT FALSE,
respond_id INTEGER NULL, -- The responder's user_id
json TEXT NULL,
updated_at TIMESTAMP NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT `{$CFG->dbprefix}peer_flag_ibfk_1`
FOREIGN KEY (`submit_id`)
REFERENCES `{$CFG->dbprefix}peer_submit` (`submit_id`)
ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE(submit_id, grade_id, user_id)
) ENGINE = InnoDB DEFAULT CHARSET=utf8") ,
);
// Database upgrade
$DATABASE_UPGRADE = function($oldversion) {
global $CFG, $PDOX;
// Version 2014042200 improvements
if ( $oldversion < 2014042200 ) {
$sql= "ALTER TABLE {$CFG->dbprefix}peer_submit ADD regrade TINYINT NULL";
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryDie($sql);
}
// Support for instructor grading
if ( $oldversion < 201412222310 ) {
$sql= "ALTER TABLE {$CFG->dbprefix}peer_submit ADD inst_points DOUBLE NULL";
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryDie($sql);
$sql= "ALTER TABLE {$CFG->dbprefix}peer_submit ADD inst_note TEXT NULL";
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryDie($sql);
$sql= "ALTER TABLE {$CFG->dbprefix}peer_submit ADD inst_id INTEGER NULL";
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryDie($sql);
}
// Support for ratings
if ( $oldversion < 201602211200 ) {
$sql= "ALTER TABLE {$CFG->dbprefix}peer_submit ADD rating INTEGER NULL";
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryDie($sql);
$sql= "ALTER TABLE {$CFG->dbprefix}peer_grade ADD rating TINYINT NULL";
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryDie($sql);
}
// Clean up the date fields a few times...
// OK to run these more than once..
$current_date = \Tsugi\Util\U::conversion_time();
if ( $current_date < 201803010000 ) {
echo("Double checking that dates are correct for the peer grader...<br/>\n");
$sql= "UPDATE {$CFG->dbprefix}peer_assn SET due_at='1970-01-02 00:00:00' WHERE due_at < '1970-01-02 00:00:00'";
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryDie($sql);
$tables = array(
'peer_flag', 'peer_grade', 'peer_submit', 'peer_assn', 'lti_link');
foreach($tables as $table) {
$sql= "UPDATE {$CFG->dbprefix}{$table} SET updated_at='1970-01-02 00:00:00' WHERE updated_at < '1970-01-02 00:00:00'";
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryDie($sql);
$sql= "ALTER TABLE {$CFG->dbprefix}{$table} MODIFY updated_at TIMESTAMP NULL DEFAULT '1970-01-02 00:00:00'";
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryDie($sql);
$sql= "ALTER TABLE {$CFG->dbprefix}{$table} MODIFY created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP";
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryDie($sql);
}
$sql= "ALTER TABLE {$CFG->dbprefix}peer_assn MODIFY due_at TIMESTAMP NULL DEFAULT '1970-01-02 00:00:00'";
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryDie($sql);
}
if ( $oldversion < 201812291121 ) {
$sql= "ALTER TABLE {$CFG->dbprefix}peer_flag MODIFY respond_id INTEGER NULL";
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryDie($sql);
}
if ( $oldversion < 201901202122 ) {
$sql= "ALTER TABLE {$CFG->dbprefix}peer_submit ADD peer_marks INTEGER DEFAULT 0";
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryDie($sql);
}
return 201901202122;
}; // Don't forget the semicolon on anonymous functions :)
// Do the actual migration if we are not in admin/upgrade.php
if ( isset($CURRENT_FILE) ) {
include $CFG->dirroot."/admin/migrate-run.php";
}