Merge branch 'MDL-37621-master' of git://github.com/damyon/moodle
[moodle.git] / mod / assign / db / upgrade.php
CommitLineData
bbd0e548
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Upgrade code for install
19 *
20 * @package mod_assign
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25/**
26 * upgrade this assignment instance - this function could be skipped but it will be needed later
27 * @param int $oldversion The old version of the assign module
28 * @return bool
29 */
30function xmldb_assign_upgrade($oldversion) {
3f7b501e
SH
31 global $CFG, $DB;
32
33 $dbman = $DB->get_manager();
34
75f87a57 35 if ($oldversion < 2012051700) {
3f7b501e 36
12a1a0da 37 // Define field to be added to assign.
3f7b501e 38 $table = new xmldb_table('assign');
e5403f8c
DW
39 $field = new xmldb_field('sendlatenotifications', XMLDB_TYPE_INTEGER, '2', null,
40 XMLDB_NOTNULL, null, '0', 'sendnotifications');
3f7b501e 41
12a1a0da 42 // Conditionally launch add field.
3f7b501e
SH
43 if (!$dbman->field_exists($table, $field)) {
44 $dbman->add_field($table, $field);
45 }
46
47 // Assign savepoint reached.
48 upgrade_mod_savepoint(true, 2012051700, 'assign');
75f87a57 49 }
d3d1fec2 50
12a1a0da
DW
51 // Moodle v2.3.0 release upgrade line.
52 // Put any upgrade step following this.
d3d1fec2 53
94f26900
DW
54 if ($oldversion < 2012071800) {
55
12a1a0da 56 // Define field requiresubmissionstatement to be added to assign.
94f26900 57 $table = new xmldb_table('assign');
e5403f8c
DW
58 $field = new xmldb_field('requiresubmissionstatement', XMLDB_TYPE_INTEGER, '2', null,
59 XMLDB_NOTNULL, null, '0', 'timemodified');
94f26900 60
12a1a0da
DW
61 // Conditionally launch add field requiresubmissionstatement.
62
94f26900
DW
63 if (!$dbman->field_exists($table, $field)) {
64 $dbman->add_field($table, $field);
65 }
66
67 // Assign savepoint reached.
68 upgrade_mod_savepoint(true, 2012071800, 'assign');
69 }
3a66d425 70
79ed4d84
DW
71 if ($oldversion < 2012081600) {
72
12a1a0da 73 // Define field to be added to assign.
79ed4d84 74 $table = new xmldb_table('assign');
e5403f8c
DW
75 $field = new xmldb_field('completionsubmit', XMLDB_TYPE_INTEGER, '2', null,
76 XMLDB_NOTNULL, null, '0', 'timemodified');
79ed4d84 77
12a1a0da 78 // Conditionally launch add field.
79ed4d84
DW
79 if (!$dbman->field_exists($table, $field)) {
80 $dbman->add_field($table, $field);
81 }
82
83 // Assign savepoint reached.
84 upgrade_mod_savepoint(true, 2012081600, 'assign');
85 }
3f7b501e 86
9e795179
DW
87 // Individual extension dates support.
88 if ($oldversion < 2012082100) {
89
259208e0 90 // Define field cutoffdate to be added to assign.
9e795179
DW
91 $table = new xmldb_table('assign');
92 $field = new xmldb_field('cutoffdate', XMLDB_TYPE_INTEGER, '10', null,
93 XMLDB_NOTNULL, null, '0', 'completionsubmit');
94
259208e0 95 // Conditionally launch add field cutoffdate.
9e795179
DW
96 if (!$dbman->field_exists($table, $field)) {
97 $dbman->add_field($table, $field);
98 }
99 // If prevent late is on - set cutoffdate to due date.
100
101 // Now remove the preventlatesubmissions column.
102 $field = new xmldb_field('preventlatesubmissions', XMLDB_TYPE_INTEGER, '2', null,
103 XMLDB_NOTNULL, null, '0', 'nosubmissions');
104 if ($dbman->field_exists($table, $field)) {
105 // Set the cutoffdate to the duedate if preventlatesubmissions was enabled.
106 $sql = 'UPDATE {assign} SET cutoffdate = duedate WHERE preventlatesubmissions = 1';
107 $DB->execute($sql);
108
109 $dbman->drop_field($table, $field);
110 }
111
e5403f8c 112 // Define field extensionduedate to be added to assign_grades.
9e795179 113 $table = new xmldb_table('assign_grades');
e5403f8c
DW
114 $field = new xmldb_field('extensionduedate', XMLDB_TYPE_INTEGER, '10', null,
115 XMLDB_NOTNULL, null, '0', 'mailed');
9e795179 116
e5403f8c 117 // Conditionally launch add field extensionduedate.
9e795179
DW
118 if (!$dbman->field_exists($table, $field)) {
119 $dbman->add_field($table, $field);
120 }
121
122 // Assign savepoint reached.
123 upgrade_mod_savepoint(true, 2012082100, 'assign');
124 }
125
12a1a0da
DW
126 // Team assignment support.
127 if ($oldversion < 2012082300) {
128
129 // Define field to be added to assign.
130 $table = new xmldb_table('assign');
6f050c7d 131 $field = new xmldb_field('teamsubmission', XMLDB_TYPE_INTEGER, '2', null,
12a1a0da
DW
132 XMLDB_NOTNULL, null, '0', 'cutoffdate');
133
134 // Conditionally launch add field.
135 if (!$dbman->field_exists($table, $field)) {
136 $dbman->add_field($table, $field);
137 }
6f050c7d 138 $field = new xmldb_field('requireallteammemberssubmit', XMLDB_TYPE_INTEGER, '2', null,
12a1a0da
DW
139 XMLDB_NOTNULL, null, '0', 'teamsubmission');
140 // Conditionally launch add field.
141 if (!$dbman->field_exists($table, $field)) {
142 $dbman->add_field($table, $field);
143 }
6f050c7d 144 $field = new xmldb_field('teamsubmissiongroupingid', XMLDB_TYPE_INTEGER, '10', null,
12a1a0da
DW
145 XMLDB_NOTNULL, null, '0', 'requireallteammemberssubmit');
146 // Conditionally launch add field.
147 if (!$dbman->field_exists($table, $field)) {
148 $dbman->add_field($table, $field);
149 }
e5403f8c
DW
150 $index = new xmldb_index('teamsubmissiongroupingid',
151 XMLDB_INDEX_NOTUNIQUE,
152 array('teamsubmissiongroupingid'));
12a1a0da
DW
153 // Conditionally launch add index.
154 if (!$dbman->index_exists($table, $index)) {
155 $dbman->add_index($table, $index);
156 }
157 $table = new xmldb_table('assign_submission');
e5403f8c
DW
158 $field = new xmldb_field('groupid', XMLDB_TYPE_INTEGER, '10', null,
159 XMLDB_NOTNULL, null, '0', 'status');
12a1a0da
DW
160 // Conditionally launch add field.
161 if (!$dbman->field_exists($table, $field)) {
162 $dbman->add_field($table, $field);
163 }
164 upgrade_mod_savepoint(true, 2012082300, 'assign');
165 }
b473171a
DW
166 if ($oldversion < 2012082400) {
167
e5403f8c 168 // Define table assign_user_mapping to be created.
b473171a
DW
169 $table = new xmldb_table('assign_user_mapping');
170
e5403f8c 171 // Adding fields to table assign_user_mapping.
b473171a 172 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
6f050c7d
EL
173 $table->add_field('assignment', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
174 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
b473171a 175
e5403f8c 176 // Adding keys to table assign_user_mapping.
b473171a 177 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
b473171a 178 $table->add_key('assignment', XMLDB_KEY_FOREIGN, array('assignment'), 'assign', array('id'));
6f050c7d 179 $table->add_key('user', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
b473171a 180
e5403f8c 181 // Conditionally launch create table for assign_user_mapping.
b473171a
DW
182 if (!$dbman->table_exists($table)) {
183 $dbman->create_table($table);
184 }
185
e5403f8c 186 // Define field blindmarking to be added to assign.
b473171a 187 $table = new xmldb_table('assign');
e5403f8c
DW
188 $field = new xmldb_field('blindmarking', XMLDB_TYPE_INTEGER, '2', null,
189 XMLDB_NOTNULL, null, '0', 'teamsubmissiongroupingid');
6f050c7d 190
b473171a
DW
191 if (!$dbman->field_exists($table, $field)) {
192 $dbman->add_field($table, $field);
193 }
194
e5403f8c 195 // Define field revealidentities to be added to assign.
b473171a 196 $table = new xmldb_table('assign');
e5403f8c
DW
197 $field = new xmldb_field('revealidentities', XMLDB_TYPE_INTEGER, '2', null,
198 XMLDB_NOTNULL, null, '0', 'blindmarking');
6f050c7d 199
b473171a
DW
200 if (!$dbman->field_exists($table, $field)) {
201 $dbman->add_field($table, $field);
202 }
6f050c7d 203
e5403f8c 204 // Assignment savepoint reached.
b473171a
DW
205 upgrade_mod_savepoint(true, 2012082400, 'assign');
206 }
12a1a0da 207
e5403f8c 208 // Moodle v2.4.0 release upgrade line.
3e69c074 209 // Put any upgrade step following this.
3e69c074 210
df211804 211 if ($oldversion < 2013030600) {
4a4f590e
DW
212 upgrade_set_timeout(60*20);
213
214 // Some assignments (upgraded from 2.2 assignment) have duplicate entries in the assign_submission
215 // and assign_grades tables for a single user. This needs to be cleaned up before we can add the unique indexes
216 // below.
217
218 // Only do this cleanup if the attempt number field has not been added to the table yet.
219 $table = new xmldb_table('assign_submission');
220 $field = new xmldb_field('attemptnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'groupid');
221 if (!$dbman->field_exists($table, $field)) {
222 // OK safe to cleanup duplicates here.
223
224 $sql = 'SELECT assignment, userid, groupid from {assign_submission} GROUP BY assignment, userid, groupid HAVING (count(id) > 1)';
225 $badrecords = $DB->get_recordset_sql($sql);
226
227 foreach ($badrecords as $badrecord) {
228 $params = array('userid'=>$badrecord->userid,
229 'groupid'=>$badrecord->groupid,
230 'assignment'=>$badrecord->assignment);
231 $duplicates = $DB->get_records('assign_submission', $params, 'timemodified DESC', 'id, timemodified');
232 if ($duplicates) {
233 // Take the first (last updated) entry out of the list so it doesn't get deleted.
234 $valid = array_shift($duplicates);
235 $deleteids = array();
236 foreach ($duplicates as $duplicate) {
237 $deleteids[] = $duplicate->id;
238 }
239
240 list($sqlids, $sqlidparams) = $DB->get_in_or_equal($deleteids);
241 $DB->delete_records_select('assign_submission', 'id ' . $sqlids, $sqlidparams);
242 }
243 }
244
245 $badrecords->close();
246 }
247
248 // Same cleanup required for assign_grades
249 // Only do this cleanup if the attempt number field has not been added to the table yet.
250 $table = new xmldb_table('assign_grades');
251 $field = new xmldb_field('attemptnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'grade');
252 if (!$dbman->field_exists($table, $field)) {
253 // OK safe to cleanup duplicates here.
254
255 $sql = 'SELECT assignment, userid from {assign_grades} GROUP BY assignment, userid HAVING (count(id) > 1)';
256 $badrecords = $DB->get_recordset_sql($sql);
257
258 foreach ($badrecords as $badrecord) {
259 $params = array('userid'=>$badrecord->userid,
260 'assignment'=>$badrecord->assignment);
261 $duplicates = $DB->get_records('assign_grades', $params, 'timemodified DESC', 'id, timemodified');
262 if ($duplicates) {
263 // Take the first (last updated) entry out of the list so it doesn't get deleted.
264 $valid = array_shift($duplicates);
265 $deleteids = array();
266 foreach ($duplicates as $duplicate) {
267 $deleteids[] = $duplicate->id;
268 }
269
270 list($sqlids, $sqlidparams) = $DB->get_in_or_equal($deleteids);
271 $DB->delete_records_select('assign_grades', 'id ' . $sqlids, $sqlidparams);
272 }
273 }
274
275 $badrecords->close();
276 }
277
df211804
DW
278 // Define table assign_user_flags to be created.
279 $table = new xmldb_table('assign_user_flags');
280
281 // Adding fields to table assign_user_flags.
282 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
283 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
284 $table->add_field('assignment', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
285 $table->add_field('locked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
286 $table->add_field('mailed', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
287 $table->add_field('extensionduedate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
288
289 // Adding keys to table assign_user_flags.
290 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
291 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
292 $table->add_key('assignment', XMLDB_KEY_FOREIGN, array('assignment'), 'assign', array('id'));
293
294 // Adding indexes to table assign_user_flags.
295 $table->add_index('mailed', XMLDB_INDEX_NOTUNIQUE, array('mailed'));
296
297 // Conditionally launch create table for assign_user_flags.
298 if (!$dbman->table_exists($table)) {
299 $dbman->create_table($table);
df211804 300
4a4f590e
DW
301 // Copy the flags from the old table to the new one.
302 $sql = 'INSERT INTO {assign_user_flags}
303 (assignment, userid, locked, mailed, extensionduedate)
304 SELECT assignment, userid, locked, mailed, extensionduedate
305 FROM {assign_grades}';
306 $DB->execute($sql);
307 }
df211804
DW
308
309 // And delete the old columns.
310 // Define index mailed (not unique) to be dropped form assign_grades.
311 $table = new xmldb_table('assign_grades');
312 $index = new xmldb_index('mailed', XMLDB_INDEX_NOTUNIQUE, array('mailed'));
313
314 // Conditionally launch drop index mailed.
315 if ($dbman->index_exists($table, $index)) {
316 $dbman->drop_index($table, $index);
317 }
318
319 // Define field locked to be dropped from assign_grades.
320 $table = new xmldb_table('assign_grades');
321 $field = new xmldb_field('locked');
322
323 // Conditionally launch drop field locked.
324 if ($dbman->field_exists($table, $field)) {
325 $dbman->drop_field($table, $field);
326 }
327
328 // Define field mailed to be dropped from assign_grades.
329 $table = new xmldb_table('assign_grades');
330 $field = new xmldb_field('mailed');
331
332 // Conditionally launch drop field mailed.
333 if ($dbman->field_exists($table, $field)) {
334 $dbman->drop_field($table, $field);
335 }
336
337 // Define field extensionduedate to be dropped from assign_grades.
338 $table = new xmldb_table('assign_grades');
339 $field = new xmldb_field('extensionduedate');
340
341 // Conditionally launch drop field extensionduedate.
342 if ($dbman->field_exists($table, $field)) {
343 $dbman->drop_field($table, $field);
344 }
345
346 // Define field attemptreopenmethod to be added to assign.
347 $table = new xmldb_table('assign');
348 $field = new xmldb_field('attemptreopenmethod', XMLDB_TYPE_CHAR, '10', null,
349 XMLDB_NOTNULL, null, 'none', 'revealidentities');
350
351 // Conditionally launch add field attemptreopenmethod.
352 if (!$dbman->field_exists($table, $field)) {
353 $dbman->add_field($table, $field);
354 }
355
356 // Define field maxattempts to be added to assign.
357 $table = new xmldb_table('assign');
358 $field = new xmldb_field('maxattempts', XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, '-1', 'attemptreopenmethod');
359
360 // Conditionally launch add field maxattempts.
361 if (!$dbman->field_exists($table, $field)) {
362 $dbman->add_field($table, $field);
363 }
364
365 // Define field attemptnumber to be added to assign_submission.
366 $table = new xmldb_table('assign_submission');
367 $field = new xmldb_field('attemptnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'groupid');
368
369 // Conditionally launch add field attemptnumber.
370 if (!$dbman->field_exists($table, $field)) {
371 $dbman->add_field($table, $field);
372 }
373
c9c34c7a
DW
374 // Define index attemptnumber (not unique) to be added to assign_submission.
375 $table = new xmldb_table('assign_submission');
376 $index = new xmldb_index('attemptnumber', XMLDB_INDEX_NOTUNIQUE, array('attemptnumber'));
377 // Conditionally launch add index attemptnumber.
378 if (!$dbman->index_exists($table, $index)) {
379 $dbman->add_index($table, $index);
380 }
381
df211804
DW
382 // Define field attemptnumber to be added to assign_grades.
383 $table = new xmldb_table('assign_grades');
384 $field = new xmldb_field('attemptnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'grade');
385
386 // Conditionally launch add field attemptnumber.
387 if (!$dbman->field_exists($table, $field)) {
388 $dbman->add_field($table, $field);
389 }
390
391 // Define index attemptnumber (not unique) to be added to assign_grades.
392 $table = new xmldb_table('assign_grades');
393 $index = new xmldb_index('attemptnumber', XMLDB_INDEX_NOTUNIQUE, array('attemptnumber'));
394
395 // Conditionally launch add index attemptnumber.
396 if (!$dbman->index_exists($table, $index)) {
397 $dbman->add_index($table, $index);
398 }
399
400 // Define index uniqueattemptsubmission (unique) to be added to assign_submission.
401 $table = new xmldb_table('assign_submission');
402 $index = new xmldb_index('uniqueattemptsubmission',
403 XMLDB_INDEX_UNIQUE,
404 array('assignment', 'userid', 'groupid', 'attemptnumber'));
405
406 // Conditionally launch add index uniqueattempt.
407 if (!$dbman->index_exists($table, $index)) {
408 $dbman->add_index($table, $index);
409 }
410
411 // Define index uniqueattemptgrade (unique) to be added to assign_grades.
412 $table = new xmldb_table('assign_grades');
413 $index = new xmldb_index('uniqueattemptgrade', XMLDB_INDEX_UNIQUE, array('assignment', 'userid', 'attemptnumber'));
414
415 // Conditionally launch add index uniqueattempt.
416 if (!$dbman->index_exists($table, $index)) {
417 $dbman->add_index($table, $index);
418 }
419
420 // Module assign savepoint reached.
421 upgrade_mod_savepoint(true, 2013030600, 'assign');
422 }
423
c9e54743 424 // Moodle v2.5.0 release upgrade line.
3e69c074 425 // Put any upgrade step following this.
c9e54743
EL
426
427
bbd0e548
DW
428 return true;
429}
430
431