Merge branch 'wip-MDL-39432-m25-i' of git://github.com/samhemelryk/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
DW
208 // Moodle v2.4.0 release upgrade line.
209 // Put any upgrade step following this.
6b9dfe73 210
df211804
DW
211 if ($oldversion < 2013030600) {
212 // Define table assign_user_flags to be created.
213 $table = new xmldb_table('assign_user_flags');
214
215 // Adding fields to table assign_user_flags.
216 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
217 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
218 $table->add_field('assignment', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
219 $table->add_field('locked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
220 $table->add_field('mailed', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
221 $table->add_field('extensionduedate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
222
223 // Adding keys to table assign_user_flags.
224 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
225 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
226 $table->add_key('assignment', XMLDB_KEY_FOREIGN, array('assignment'), 'assign', array('id'));
227
228 // Adding indexes to table assign_user_flags.
229 $table->add_index('mailed', XMLDB_INDEX_NOTUNIQUE, array('mailed'));
230
231 // Conditionally launch create table for assign_user_flags.
232 if (!$dbman->table_exists($table)) {
233 $dbman->create_table($table);
234 }
235
236 // Copy the flags from the old table to the new one.
237 $sql = 'INSERT INTO {assign_user_flags}
238 (assignment, userid, locked, mailed, extensionduedate)
239 SELECT assignment, userid, locked, mailed, extensionduedate
240 FROM {assign_grades}';
241 $DB->execute($sql);
242
243 // And delete the old columns.
244 // Define index mailed (not unique) to be dropped form assign_grades.
245 $table = new xmldb_table('assign_grades');
246 $index = new xmldb_index('mailed', XMLDB_INDEX_NOTUNIQUE, array('mailed'));
247
248 // Conditionally launch drop index mailed.
249 if ($dbman->index_exists($table, $index)) {
250 $dbman->drop_index($table, $index);
251 }
252
253 // Define field locked to be dropped from assign_grades.
254 $table = new xmldb_table('assign_grades');
255 $field = new xmldb_field('locked');
256
257 // Conditionally launch drop field locked.
258 if ($dbman->field_exists($table, $field)) {
259 $dbman->drop_field($table, $field);
260 }
261
262 // Define field mailed to be dropped from assign_grades.
263 $table = new xmldb_table('assign_grades');
264 $field = new xmldb_field('mailed');
265
266 // Conditionally launch drop field mailed.
267 if ($dbman->field_exists($table, $field)) {
268 $dbman->drop_field($table, $field);
269 }
270
271 // Define field extensionduedate to be dropped from assign_grades.
272 $table = new xmldb_table('assign_grades');
273 $field = new xmldb_field('extensionduedate');
274
275 // Conditionally launch drop field extensionduedate.
276 if ($dbman->field_exists($table, $field)) {
277 $dbman->drop_field($table, $field);
278 }
279
280 // Define field attemptreopenmethod to be added to assign.
281 $table = new xmldb_table('assign');
282 $field = new xmldb_field('attemptreopenmethod', XMLDB_TYPE_CHAR, '10', null,
283 XMLDB_NOTNULL, null, 'none', 'revealidentities');
284
285 // Conditionally launch add field attemptreopenmethod.
286 if (!$dbman->field_exists($table, $field)) {
287 $dbman->add_field($table, $field);
288 }
289
290 // Define field maxattempts to be added to assign.
291 $table = new xmldb_table('assign');
292 $field = new xmldb_field('maxattempts', XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, '-1', 'attemptreopenmethod');
293
294 // Conditionally launch add field maxattempts.
295 if (!$dbman->field_exists($table, $field)) {
296 $dbman->add_field($table, $field);
297 }
298
299 // Define field attemptnumber to be added to assign_submission.
300 $table = new xmldb_table('assign_submission');
301 $field = new xmldb_field('attemptnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'groupid');
302
303 // Conditionally launch add field attemptnumber.
304 if (!$dbman->field_exists($table, $field)) {
305 $dbman->add_field($table, $field);
306 }
307
c9c34c7a
DW
308 // Define index attemptnumber (not unique) to be added to assign_submission.
309 $table = new xmldb_table('assign_submission');
310 $index = new xmldb_index('attemptnumber', XMLDB_INDEX_NOTUNIQUE, array('attemptnumber'));
311 // Conditionally launch add index attemptnumber.
312 if (!$dbman->index_exists($table, $index)) {
313 $dbman->add_index($table, $index);
314 }
315
df211804
DW
316 // Define field attemptnumber to be added to assign_grades.
317 $table = new xmldb_table('assign_grades');
318 $field = new xmldb_field('attemptnumber', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'grade');
319
320 // Conditionally launch add field attemptnumber.
321 if (!$dbman->field_exists($table, $field)) {
322 $dbman->add_field($table, $field);
323 }
324
325 // Define index attemptnumber (not unique) to be added to assign_grades.
326 $table = new xmldb_table('assign_grades');
327 $index = new xmldb_index('attemptnumber', XMLDB_INDEX_NOTUNIQUE, array('attemptnumber'));
328
329 // Conditionally launch add index attemptnumber.
330 if (!$dbman->index_exists($table, $index)) {
331 $dbman->add_index($table, $index);
332 }
333
334 // Define index uniqueattemptsubmission (unique) to be added to assign_submission.
335 $table = new xmldb_table('assign_submission');
336 $index = new xmldb_index('uniqueattemptsubmission',
337 XMLDB_INDEX_UNIQUE,
338 array('assignment', 'userid', 'groupid', 'attemptnumber'));
339
340 // Conditionally launch add index uniqueattempt.
341 if (!$dbman->index_exists($table, $index)) {
342 $dbman->add_index($table, $index);
343 }
344
345 // Define index uniqueattemptgrade (unique) to be added to assign_grades.
346 $table = new xmldb_table('assign_grades');
347 $index = new xmldb_index('uniqueattemptgrade', XMLDB_INDEX_UNIQUE, array('assignment', 'userid', 'attemptnumber'));
348
349 // Conditionally launch add index uniqueattempt.
350 if (!$dbman->index_exists($table, $index)) {
351 $dbman->add_index($table, $index);
352 }
353
354 // Module assign savepoint reached.
355 upgrade_mod_savepoint(true, 2013030600, 'assign');
356 }
357
bbd0e548
DW
358 return true;
359}
360
361