MDL-37939 Course: Ensure that the cm->section is set when moving modules
[moodle.git] / lib / db / upgrade.php
CommitLineData
5b4a78e2 1<?php
5b4a78e2 2// This file is part of Moodle - http://moodle.org/
4e423cbf 3//
5b4a78e2
PS
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.
4e423cbf 8//
5b4a78e2
PS
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.
4e423cbf 13//
5b4a78e2
PS
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 * This file keeps track of upgrades to Moodle.
19 *
20 * Sometimes, changes between versions involve
21 * alterations to database structures and other
22 * major things that may break installations.
23 *
24 * The upgrade function in this file will attempt
25 * to perform all the necessary actions to upgrade
26 * your older installation to the current version.
27 *
28 * If there's something it cannot do itself, it
29 * will tell you what you need to do.
30 *
31 * The commands in here will all be database-neutral,
32 * using the methods of database_manager class
33 *
34 * Please do not forget to use upgrade_set_timeout()
35 * before any action that may take longer time to finish.
36 *
39b90b51
EL
37 * @package core_install
38 * @category upgrade
39 * @copyright 2006 onwards Martin Dougiamas http://dougiamas.com
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5b4a78e2
PS
41 */
42
43defined('MOODLE_INTERNAL') || die();
4e423cbf 44
3406acde 45/**
39b90b51
EL
46 * Main upgrade tasks to be executed on Moodle version bump
47 *
48 * This function is automatically executed after one bump in the Moodle core
f9488a6f 49 * version is detected. It's in charge of performing the required tasks
39b90b51
EL
50 * to raise core from the previous version to the next one.
51 *
52 * It's a collection of ordered blocks of code, named "upgrade steps",
f9488a6f 53 * each one performing one isolated (from the rest of steps) task. Usually
39b90b51
EL
54 * tasks involve creating new DB objects or performing manipulation of the
55 * information for cleanup/fixup purposes.
56 *
57 * Each upgrade step has a fixed structure, that can be summarised as follows:
58 *
59 * if ($oldversion < XXXXXXXXXX.XX) {
f9488a6f 60 * // Explanation of the update step, linking to issue in the Tracker if necessary
39b90b51
EL
61 * upgrade_set_timeout(XX); // Optional for big tasks
62 * // Code to execute goes here, usually the XMLDB Editor will
63 * // help you here. See {@link http://docs.moodle.org/dev/XMLDB_editor}.
64 * upgrade_main_savepoint(true, XXXXXXXXXX.XX);
65 * }
66 *
67 * All plugins within Moodle (modules, blocks, reports...) support the existence of
68 * their own upgrade.php file, using the "Frankenstyle" component name as
69 * defined at {@link http://docs.moodle.org/dev/Frankenstyle}, for example:
70 * - {@link xmldb_page_upgrade($oldversion)}. (modules don't require the plugintype ("mod_") to be used.
71 * - {@link xmldb_auth_manual_upgrade($oldversion)}.
72 * - {@link xmldb_workshopform_accumulative_upgrade($oldversion)}.
73 * - ....
74 *
75 * In order to keep the contents of this file reduced, it's allowed to create some helper
76 * functions to be used here in the {@link upgradelib.php} file at the same directory. Note
f9488a6f 77 * that such a file must be manually included from upgrade.php, and there are some restrictions
39b90b51
EL
78 * about what can be used within it.
79 *
80 * For more information, take a look to the documentation available:
81 * - Data definition API: {@link http://docs.moodle.org/dev/Data_definition_API}
82 * - Upgrade API: {@link http://docs.moodle.org/dev/Upgrade_API}
3406acde 83 *
3406acde 84 * @param int $oldversion
5b4a78e2 85 * @return bool always true
3406acde 86 */
775f811a 87function xmldb_main_upgrade($oldversion) {
fcda7dbb 88 global $CFG, $USER, $DB, $OUTPUT, $SITE;
4e423cbf 89
4f12838e 90 require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
13a0d3d3 91
46d318bc 92 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
f33e1ed4 93
5c79b8ed
PS
94 if ($oldversion < 2011120500) {
95 // just in case somebody hacks upgrade scripts or env, we really can not continue
96 echo("You need to upgrade to 2.2.x first!\n");
97 exit(1);
ae95489d
EL
98 // Note this savepoint is 100% unreachable, but needed to pass the upgrade checks
99 upgrade_main_savepoint(true, 2011120500);
5c79b8ed
PS
100 }
101
39b90b51
EL
102 // Moodle v2.2.0 release upgrade line
103 // Put any upgrade step following this
da4aa3e4 104
43cf3165
EL
105 if ($oldversion < 2011120500.02) {
106
107 upgrade_set_timeout(60*20); // This may take a while
108 // MDL-28180. Some missing restrictions in certain backup & restore operations
109 // were causing incorrect duplicates in the course_completion_aggr_methd table.
110 // This upgrade step takes rid of them.
111 $sql = 'SELECT course, criteriatype, MIN(id) AS minid
112 FROM {course_completion_aggr_methd}
113 GROUP BY course, criteriatype
114 HAVING COUNT(*) > 1';
115 $duprs = $DB->get_recordset_sql($sql);
116 foreach ($duprs as $duprec) {
117 // We need to handle NULLs in criteriatype diferently
118 if (is_null($duprec->criteriatype)) {
119 $where = 'course = ? AND criteriatype IS NULL AND id > ?';
120 $params = array($duprec->course, $duprec->minid);
121 } else {
122 $where = 'course = ? AND criteriatype = ? AND id > ?';
123 $params = array($duprec->course, $duprec->criteriatype, $duprec->minid);
124 }
125 $DB->delete_records_select('course_completion_aggr_methd', $where, $params);
126 }
127 $duprs->close();
128
129 // Main savepoint reached
130 upgrade_main_savepoint(true, 2011120500.02);
131 }
132
52a23063 133 if ($oldversion < 2011120500.03) {
8e54ce97
AD
134
135 // Changing precision of field value on table user_preferences to (1333)
136 $table = new xmldb_table('user_preferences');
137 $field = new xmldb_field('value', XMLDB_TYPE_CHAR, '1333', null, XMLDB_NOTNULL, null, null, 'name');
138
139 // Launch change of precision for field value
140 $dbman->change_field_precision($table, $field);
141
142 // Main savepoint reached
52a23063 143 upgrade_main_savepoint(true, 2011120500.03);
8e54ce97
AD
144 }
145
91331b6b
MG
146 if ($oldversion < 2012020200.03) {
147
148 // Define index rolecontext (not unique) to be added to role_assignments
149 $table = new xmldb_table('role_assignments');
150 $index = new xmldb_index('rolecontext', XMLDB_INDEX_NOTUNIQUE, array('roleid', 'contextid'));
151
152 // Conditionally launch add index rolecontext
153 if (!$dbman->index_exists($table, $index)) {
154 $dbman->add_index($table, $index);
155 }
156
157 // Define index usercontextrole (not unique) to be added to role_assignments
158 $index = new xmldb_index('usercontextrole', XMLDB_INDEX_NOTUNIQUE, array('userid', 'contextid', 'roleid'));
159
160 // Conditionally launch add index usercontextrole
161 if (!$dbman->index_exists($table, $index)) {
162 $dbman->add_index($table, $index);
163 }
164
165 // Main savepoint reached
166 upgrade_main_savepoint(true, 2012020200.03);
167 }
168
8900213b
AD
169 if ($oldversion < 2012020200.06) {
170 // Previously we always allowed users to override their email address via the messaging system
171 // We have now added a setting to allow admins to turn this this ability on and off
172 // While this setting defaults to 0 (off) we're setting it to 1 (on) to maintain the behaviour for upgrading sites
173 set_config('messagingallowemailoverride', 1);
174
175 // Main savepoint reached
176 upgrade_main_savepoint(true, 2012020200.06);
177 }
178
bc5fbbb3 179 if ($oldversion < 2012021700.01) {
669bdcab
RT
180 // Changing precision of field uniquehash on table post to 255
181 $table = new xmldb_table('post');
182 $field = new xmldb_field('uniquehash', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'content');
183
184 // Launch change of precision for field uniquehash
185 $dbman->change_field_precision($table, $field);
186
187 // Main savepoint reached
bc5fbbb3 188 upgrade_main_savepoint(true, 2012021700.01);
669bdcab
RT
189 }
190
aa803ceb
RT
191 if ($oldversion < 2012021700.02) {
192 // Somewhere before 1.9 summary and content column in post table were not null. In 1.9+
193 // not null became false.
194 $columns = $DB->get_columns('post');
195
196 // Fix discrepancies in summary field after upgrade from 1.9
197 if (array_key_exists('summary', $columns) && $columns['summary']->not_null != false) {
198 $table = new xmldb_table('post');
199 $summaryfield = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'subject');
5f5ee191 200
aa803ceb
RT
201 if ($dbman->field_exists($table, $summaryfield)) {
202 $dbman->change_field_notnull($table, $summaryfield);
203 }
204
205 }
206
207 // Fix discrepancies in content field after upgrade from 1.9
208 if (array_key_exists('content', $columns) && $columns['content']->not_null != false) {
209 $table = new xmldb_table('post');
210 $contentfield = new xmldb_field('content', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'summary');
211
212 if ($dbman->field_exists($table, $contentfield)) {
213 $dbman->change_field_notnull($table, $contentfield);
214 }
215
216 }
217
218 upgrade_main_savepoint(true, 2012021700.02);
219 }
220
f8822462
EL
221 // The ability to backup user (private) files is out completely - MDL-29248
222 if ($oldversion < 2012030100.01) {
223 unset_config('backup_general_user_files', 'backup');
224 unset_config('backup_general_user_files_locked', 'backup');
225 unset_config('backup_auto_user_files', 'backup');
226
227 upgrade_main_savepoint(true, 2012030100.01);
228 }
229
436650b0 230 if ($oldversion < 2012030900.01) {
4df8d192
PS
231 // Migrate all numbers to signed & all texts and binaries to big size.
232 // It should be safe to interrupt this and continue later.
233 upgrade_mysql_fix_unsigned_and_lob_columns();
436650b0
PS
234
235 // Main savepoint reached
236 upgrade_main_savepoint(true, 2012030900.01);
237 }
238
9665ecd2
TH
239 if ($oldversion < 2012031500.01) {
240 // Upgrade old course_allowed_modules data to be permission overrides.
6f3570ff 241 if ($CFG->restrictmodulesfor === 'all') {
9665ecd2 242 $courses = $DB->get_records_menu('course', array(), 'id', 'id, 1');
6f3570ff 243 } else if ($CFG->restrictmodulesfor === 'requested') {
b06143c5 244 $courses = $DB->get_records_menu('course', array('restrictmodules' => 1), 'id', 'id, 1');
9665ecd2
TH
245 } else {
246 $courses = array();
247 }
248
249 if (!$dbman->table_exists('course_allowed_modules')) {
250 // Upgrade must already have been run on this server. This might happen,
251 // for example, during development of these changes.
252 $courses = array();
253 }
254
255 $modidtoname = $DB->get_records_menu('modules', array(), 'id', 'id, name');
256
257 $coursecount = count($courses);
258 if ($coursecount) {
259 $pbar = new progress_bar('allowedmods', 500, true);
260 $transaction = $DB->start_delegated_transaction();
261 }
262
263 $i = 0;
264 foreach ($courses as $courseid => $notused) {
265 $i += 1;
266 upgrade_set_timeout(60); // 1 minute per course should be fine.
267
268 $allowedmoduleids = $DB->get_records_menu('course_allowed_modules',
269 array('course' => $courseid), 'module', 'module, 1');
270 if (empty($allowedmoduleids)) {
271 // This seems to be the best match for backwards compatibility,
8149d624 272 // not necessarily with the old code in course_allowed_module function,
9665ecd2
TH
273 // but with the code that used to be in the coures settings form.
274 $allowedmoduleids = explode(',', $CFG->defaultallowedmodules);
275 $allowedmoduleids = array_combine($allowedmoduleids, $allowedmoduleids);
276 }
277
278 $context = context_course::instance($courseid);
279
280 list($roleids) = get_roles_with_cap_in_context($context, 'moodle/course:manageactivities');
281 list($managerroleids) = get_roles_with_cap_in_context($context, 'moodle/site:config');
282 foreach ($managerroleids as $roleid) {
283 unset($roleids[$roleid]);
284 }
285
286 foreach ($modidtoname as $modid => $modname) {
287 if (isset($allowedmoduleids[$modid])) {
288 // Module is allowed, no worries.
289 continue;
290 }
291
292 $capability = 'mod/' . $modname . ':addinstance';
293 foreach ($roleids as $roleid) {
294 assign_capability($capability, CAP_PREVENT, $roleid, $context);
295 }
296 }
297
298 $pbar->update($i, $coursecount, "Upgrading legacy course_allowed_modules data - $i/$coursecount.");
299 }
300
301 if ($coursecount) {
302 $transaction->allow_commit();
303 }
304
305 upgrade_main_savepoint(true, 2012031500.01);
306 }
307
308 if ($oldversion < 2012031500.02) {
309
b06143c5 310 // Define field restrictmodules to be dropped from course
9665ecd2 311 $table = new xmldb_table('course');
4354486d 312 $field = new xmldb_field('restrictmodules');
9665ecd2
TH
313
314 // Conditionally launch drop field requested
315 if ($dbman->field_exists($table, $field)) {
316 $dbman->drop_field($table, $field);
317 }
318
319 upgrade_main_savepoint(true, 2012031500.02);
320 }
321
322 if ($oldversion < 2012031500.03) {
323
324 // Define table course_allowed_modules to be dropped
325 $table = new xmldb_table('course_allowed_modules');
326
327 // Conditionally launch drop table for course_allowed_modules
328 if ($dbman->table_exists($table)) {
329 $dbman->drop_table($table);
330 }
331
332 upgrade_main_savepoint(true, 2012031500.03);
333 }
334
335 if ($oldversion < 2012031500.04) {
336 // Clean up the old admin settings.
337 unset_config('restrictmodulesfor');
338 unset_config('restrictbydefault');
339 unset_config('defaultallowedmodules');
340
341 upgrade_main_savepoint(true, 2012031500.04);
342 }
e9e4a4a6 343
e63f95d7
PS
344 if ($oldversion < 2012032300.02) {
345 // Migrate the old admin debug setting.
346 if ($CFG->debug == 38911) {
347 set_config('debug', DEBUG_DEVELOPER);
348 } else if ($CFG->debug == 6143) {
349 set_config('debug', DEBUG_ALL);
350 }
351 upgrade_main_savepoint(true, 2012032300.02);
352 }
353
e7e0f8d2
DP
354 if ($oldversion < 2012042300.00) {
355 // This change makes the course_section index unique.
356
cf76b335 357 // xmldb does not allow changing index uniqueness - instead we must drop
e7e0f8d2
DP
358 // index then add it again
359 $table = new xmldb_table('course_sections');
cf76b335 360 $index = new xmldb_index('course_section', XMLDB_INDEX_NOTUNIQUE, array('course', 'section'));
e7e0f8d2
DP
361
362 // Conditionally launch drop index course_section
363 if ($dbman->index_exists($table, $index)) {
364 $dbman->drop_index($table, $index);
365 }
cf76b335 366
367 // Look for any duplicate course_sections entries. There should not be
368 // any but on some busy systems we found a few, maybe due to previous
369 // bugs.
370 $transaction = $DB->start_delegated_transaction();
371 $rs = $DB->get_recordset_sql('
372 SELECT DISTINCT
373 cs.id, cs.course
374 FROM
375 {course_sections} cs
376 INNER JOIN {course_sections} older
377 ON cs.course = older.course AND cs.section = older.section
378 AND older.id < cs.id');
379 foreach ($rs as $rec) {
380 $DB->delete_records('course_sections', array('id' => $rec->id));
026082b4
DP
381 // We can't use rebuild_course_cache() here because introducing sectioncache later
382 // so reset modinfo manually.
383 $DB->set_field('course', 'modinfo', null, array('id' => $rec->course));
cf76b335 384 }
385 $rs->close();
386 $transaction->allow_commit();
387
388 // Define index course_section (unique) to be added to course_sections
389 $index = new xmldb_index('course_section', XMLDB_INDEX_UNIQUE, array('course', 'section'));
390
391 // Conditionally launch add index course_section
392 if (!$dbman->index_exists($table, $index)) {
393 $dbman->add_index($table, $index);
394 }
395
396 // Main savepoint reached
e7e0f8d2 397 upgrade_main_savepoint(true, 2012042300.00);
cf76b335 398 }
296c0247 399
ede323e2
ARN
400 if ($oldversion < 2012042300.02) {
401 require_once($CFG->libdir . '/completion/completion_criteria.php');
402 // Delete orphaned criteria which were left when modules were removed
403 if ($DB->get_dbfamily() === 'mysql') {
404 $sql = "DELETE cc FROM {course_completion_criteria} cc
405 LEFT JOIN {course_modules} cm ON cm.id = cc.moduleinstance
406 WHERE cm.id IS NULL AND cc.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
407 } else {
408 $sql = "DELETE FROM {course_completion_criteria}
409 WHERE NOT EXISTS (
410 SELECT 'x' FROM {course_modules}
411 WHERE {course_modules}.id = {course_completion_criteria}.moduleinstance)
2ea2549e 412 AND {course_completion_criteria}.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
ede323e2
ARN
413 }
414 $DB->execute($sql);
415
416 // Main savepoint reached
417 upgrade_main_savepoint(true, 2012042300.02);
418 }
419
0dda790c 420 if ($oldversion < 2012050300.01) {
fa530687
PS
421 // Make sure deleted users do not have picture flag.
422 $DB->set_field('user', 'picture', 0, array('deleted'=>1, 'picture'=>1));
0dda790c 423 upgrade_main_savepoint(true, 2012050300.01);
fa530687
PS
424 }
425
0dda790c 426 if ($oldversion < 2012050300.02) {
4d254790
PS
427
428 // Changing precision of field picture on table user to (10)
429 $table = new xmldb_table('user');
430 $field = new xmldb_field('picture', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'secret');
431
432 // Launch change of precision for field picture
433 $dbman->change_field_precision($table, $field);
434
435 // Main savepoint reached
0dda790c 436 upgrade_main_savepoint(true, 2012050300.02);
4d254790
PS
437 }
438
1156d38d
DP
439 if ($oldversion < 2012050300.03) {
440
441 // Define field coursedisplay to be added to course
442 $table = new xmldb_table('course');
443 $field = new xmldb_field('coursedisplay', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'completionnotify');
444
445 // Conditionally launch add field coursedisplay
446 if (!$dbman->field_exists($table, $field)) {
447 $dbman->add_field($table, $field);
448 }
449
450 // Main savepoint reached
451 upgrade_main_savepoint(true, 2012050300.03);
452 }
453
b9bcdb54
DP
454 if ($oldversion < 2012050300.04) {
455
456 // Define table course_display to be dropped
457 $table = new xmldb_table('course_display');
458
459 // Conditionally launch drop table for course_display
460 if ($dbman->table_exists($table)) {
461 $dbman->drop_table($table);
462 }
463
464 // Main savepoint reached
465 upgrade_main_savepoint(true, 2012050300.04);
466 }
467
468 if ($oldversion < 2012050300.05) {
469
470 // Clean up removed admin setting.
471 unset_config('navlinkcoursesections');
472
473 upgrade_main_savepoint(true, 2012050300.05);
474 }
475
c119a2ee
PS
476 if ($oldversion < 2012050400.01) {
477
478 // Define index sortorder (not unique) to be added to course
479 $table = new xmldb_table('course');
480 $index = new xmldb_index('sortorder', XMLDB_INDEX_NOTUNIQUE, array('sortorder'));
481
482 // Conditionally launch add index sortorder
483 if (!$dbman->index_exists($table, $index)) {
484 $dbman->add_index($table, $index);
485 }
486
487 // Main savepoint reached
488 upgrade_main_savepoint(true, 2012050400.01);
489 }
490
bbb0a6eb
SH
491 if ($oldversion < 2012050400.02) {
492
9fa74379
DP
493 // Clean up removed admin setting.
494 unset_config('enablecourseajax');
495
bbb0a6eb 496 upgrade_main_savepoint(true, 2012050400.02);
9fa74379
DP
497 }
498
74b714df
ARN
499 if ($oldversion < 2012051100.01) {
500
501 // Define field idnumber to be added to groups
502 $table = new xmldb_table('groups');
503 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'courseid');
504 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
505
506 // Conditionally launch add field idnumber
507 if (!$dbman->field_exists($table, $field)) {
508 $dbman->add_field($table, $field);
509 }
510
511 // Conditionally launch add index idnumber
512 if (!$dbman->index_exists($table, $index)) {
513 $dbman->add_index($table, $index);
514 }
515
516 // Define field idnumber to be added to groupings
517 $table = new xmldb_table('groupings');
518 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'name');
519 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
520
521 // Conditionally launch add field idnumber
522 if (!$dbman->field_exists($table, $field)) {
523 $dbman->add_field($table, $field);
524 }
525
526 // Conditionally launch add index idnumber
527 if (!$dbman->index_exists($table, $index)) {
528 $dbman->add_index($table, $index);
529 }
530
531 // Main savepoint reached
532 upgrade_main_savepoint(true, 2012051100.01);
533 }
534
dd420aba
EL
535 if ($oldversion < 2012051100.03) {
536
ce4dfd27 537 // Amend course table to add sectioncache cache
538 $table = new xmldb_table('course');
539 $field = new xmldb_field('sectioncache', XMLDB_TYPE_TEXT, null, null, null, null, null, 'showgrades');
540 if (!$dbman->field_exists($table, $field)) {
541 $dbman->add_field($table, $field);
542 }
543
544 // Amend course_sections to add date, time and groupingid availability
545 // conditions and a setting about whether to show them
546 $table = new xmldb_table('course_sections');
dd420aba 547 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'visible');
ce4dfd27 548 if (!$dbman->field_exists($table, $field)) {
549 $dbman->add_field($table, $field);
550 }
dd420aba 551 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'availablefrom');
ce4dfd27 552 if (!$dbman->field_exists($table, $field)) {
553 $dbman->add_field($table, $field);
554 }
dd420aba 555 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'availableuntil');
ce4dfd27 556 // Conditionally launch add field showavailability
557 if (!$dbman->field_exists($table, $field)) {
558 $dbman->add_field($table, $field);
559 }
dd420aba 560 $field = new xmldb_field('groupingid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'showavailability');
ce4dfd27 561 // Conditionally launch add field groupingid
562 if (!$dbman->field_exists($table, $field)) {
563 $dbman->add_field($table, $field);
564 }
565
566 // Add course_sections_availability to add completion & grade availability conditions
567 $table = new xmldb_table('course_sections_availability');
568
569 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
570 $table->add_field('coursesectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
571 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
572 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', null, null, null, null);
573 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
574 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
575 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
576
577 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
dd420aba
EL
578 $table->add_key('coursesectionid', XMLDB_KEY_FOREIGN, array('coursesectionid'), 'course_sections', array('id'));
579 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
580 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
ce4dfd27 581
582 if (!$dbman->table_exists($table)) {
583 $dbman->create_table($table);
584 }
585
94dc3c7d 586 // Main savepoint reached
dd420aba 587 upgrade_main_savepoint(true, 2012051100.03);
ce4dfd27 588 }
589
67233725
DC
590 if ($oldversion < 2012052100.00) {
591
7051415c 592 // Define field referencefileid to be added to files.
67233725
DC
593 $table = new xmldb_table('files');
594
7051415c 595 // Define field referencefileid to be added to files.
67233725 596 $field = new xmldb_field('referencefileid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'sortorder');
7051415c
DC
597
598 // Conditionally launch add field referencefileid.
67233725
DC
599 if (!$dbman->field_exists($table, $field)) {
600 $dbman->add_field($table, $field);
601 }
602
7051415c 603 // Define field referencelastsync to be added to files.
67233725 604 $field = new xmldb_field('referencelastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencefileid');
7051415c
DC
605
606 // Conditionally launch add field referencelastsync.
67233725
DC
607 if (!$dbman->field_exists($table, $field)) {
608 $dbman->add_field($table, $field);
609 }
610
7051415c
DC
611 // Define field referencelifetime to be added to files.
612 $field = new xmldb_field('referencelifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencelastsync');
613
614 // Conditionally launch add field referencelifetime.
67233725
DC
615 if (!$dbman->field_exists($table, $field)) {
616 $dbman->add_field($table, $field);
617 }
618
619 $key = new xmldb_key('referencefileid', XMLDB_KEY_FOREIGN, array('referencefileid'), 'files_reference', array('id'));
620 // Launch add key referencefileid
621 $dbman->add_key($table, $key);
622
623 // Define table files_reference to be created.
624 $table = new xmldb_table('files_reference');
625
626 // Adding fields to table files_reference.
627 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
67233725
DC
628 $table->add_field('repositoryid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
629 $table->add_field('lastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
630 $table->add_field('lifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
631 $table->add_field('reference', XMLDB_TYPE_TEXT, null, null, null, null, null);
632
633 // Adding keys to table files_reference.
634 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
635 $table->add_key('repositoryid', XMLDB_KEY_FOREIGN, array('repositoryid'), 'repository_instances', array('id'));
636
637 // Conditionally launch create table for files_reference
638 if (!$dbman->table_exists($table)) {
639 $dbman->create_table($table);
640 }
641
642 // Main savepoint reached
643 upgrade_main_savepoint(true, 2012052100.00);
644 }
645
3f1bca9d 646 if ($oldversion < 2012052500.03) { // fix invalid course_completion_records MDL-27368
d2e3a1b4
DM
647 //first get all instances of duplicate records
648 $sql = 'SELECT userid, course FROM {course_completions} WHERE (deleted IS NULL OR deleted <> 1) GROUP BY userid, course HAVING (count(id) > 1)';
649 $duplicates = $DB->get_recordset_sql($sql, array());
650
651 foreach ($duplicates as $duplicate) {
652 $pointer = 0;
653 //now get all the records for this user/course
654 $sql = 'userid = ? AND course = ? AND (deleted IS NULL OR deleted <> 1)';
655 $completions = $DB->get_records_select('course_completions', $sql,
656 array($duplicate->userid, $duplicate->course), 'timecompleted DESC, timestarted DESC');
657 $needsupdate = false;
658 $origcompletion = null;
659 foreach ($completions as $completion) {
660 $pointer++;
661 if ($pointer === 1) { //keep 1st record but delete all others.
662 $origcompletion = $completion;
663 } else {
664 //we need to keep the "oldest" of all these fields as the valid completion record.
665 $fieldstocheck = array('timecompleted', 'timestarted', 'timeenrolled');
666 foreach ($fieldstocheck as $f) {
667 if ($origcompletion->$f > $completion->$f) {
668 $origcompletion->$f = $completion->$f;
669 $needsupdate = true;
670 }
671 }
672 $DB->delete_records('course_completions', array('id'=>$completion->id));
673 }
674 }
675 if ($needsupdate) {
676 $DB->update_record('course_completions', $origcompletion);
677 }
678 }
679
680 // Main savepoint reached
3f1bca9d 681 upgrade_main_savepoint(true, 2012052500.03);
d2e3a1b4 682 }
424a19b1 683
bd633dda 684 if ($oldversion < 2012052900.00) {
e6f55285
SH
685 // Clean up all duplicate records in the course_completions table in preparation
686 // for adding a new index there.
424a19b1
AB
687 upgrade_course_completion_remove_duplicates(
688 'course_completions',
689 array('userid', 'course'),
690 array('timecompleted', 'timestarted', 'timeenrolled')
691 );
692
e6f55285
SH
693 // Main savepoint reached
694 upgrade_main_savepoint(true, 2012052900.00);
695 }
696
697 if ($oldversion < 2012052900.01) {
698 // Add indexes to prevent new duplicates in the course_completions table.
424a19b1
AB
699 // Define index useridcourse (unique) to be added to course_completions
700 $table = new xmldb_table('course_completions');
701 $index = new xmldb_index('useridcourse', XMLDB_INDEX_UNIQUE, array('userid', 'course'));
702
424a19b1
AB
703 // Conditionally launch add index useridcourse
704 if (!$dbman->index_exists($table, $index)) {
705 $dbman->add_index($table, $index);
706 }
707
708 // Main savepoint reached
e6f55285 709 upgrade_main_savepoint(true, 2012052900.01);
424a19b1
AB
710 }
711
e6f55285
SH
712 if ($oldversion < 2012052900.02) {
713 // Clean up all duplicate records in the course_completion_crit_compl table in preparation
714 // for adding a new index there.
424a19b1
AB
715 upgrade_course_completion_remove_duplicates(
716 'course_completion_crit_compl',
717 array('userid', 'course', 'criteriaid'),
718 array('timecompleted')
719 );
720
e6f55285
SH
721 // Main savepoint reached
722 upgrade_main_savepoint(true, 2012052900.02);
723 }
424a19b1 724
e6f55285
SH
725 if ($oldversion < 2012052900.03) {
726 // Add indexes to prevent new duplicates in the course_completion_crit_compl table.
424a19b1
AB
727 // Define index useridcoursecriteraid (unique) to be added to course_completion_crit_compl
728 $table = new xmldb_table('course_completion_crit_compl');
729 $index = new xmldb_index('useridcoursecriteraid', XMLDB_INDEX_UNIQUE, array('userid', 'course', 'criteriaid'));
730
731 // Conditionally launch add index useridcoursecriteraid
732 if (!$dbman->index_exists($table, $index)) {
733 $dbman->add_index($table, $index);
734 }
735
736 // Main savepoint reached
e6f55285 737 upgrade_main_savepoint(true, 2012052900.03);
424a19b1
AB
738 }
739
e6f55285
SH
740 if ($oldversion < 2012052900.04) {
741 // Clean up all duplicate records in the course_completion_aggr_methd table in preparation
742 // for adding a new index there.
424a19b1
AB
743 upgrade_course_completion_remove_duplicates(
744 'course_completion_aggr_methd',
745 array('course', 'criteriatype')
746 );
747
e6f55285
SH
748 // Main savepoint reached
749 upgrade_main_savepoint(true, 2012052900.04);
750 }
751
752 if ($oldversion < 2012052900.05) {
753 // Add indexes to prevent new duplicates in the course_completion_aggr_methd table.
424a19b1
AB
754 // Define index coursecriteratype (unique) to be added to course_completion_aggr_methd
755 $table = new xmldb_table('course_completion_aggr_methd');
756 $index = new xmldb_index('coursecriteriatype', XMLDB_INDEX_UNIQUE, array('course', 'criteriatype'));
757
758 // Conditionally launch add index coursecriteratype
759 if (!$dbman->index_exists($table, $index)) {
760 $dbman->add_index($table, $index);
761 }
762
763 // Main savepoint reached
e6f55285 764 upgrade_main_savepoint(true, 2012052900.05);
424a19b1
AB
765 }
766
79c2b509 767 if ($oldversion < 2012060600.01) {
51003653
DM
768 // Add field referencehash to files_reference
769 $table = new xmldb_table('files_reference');
770 $field = new xmldb_field('referencehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'reference');
771 if (!$dbman->field_exists($table, $field)) {
772 $dbman->add_field($table, $field);
773 }
79c2b509 774 upgrade_main_savepoint(true, 2012060600.01);
51003653
DM
775 }
776
79c2b509 777 if ($oldversion < 2012060600.02) {
dccba8bc
DM
778 // Populate referencehash field with SHA1 hash of the reference - this shoudl affect only 2.3dev sites
779 // that were using the feature for testing. Production sites have the table empty.
780 $rs = $DB->get_recordset('files_reference', null, '', 'id, reference');
781 foreach ($rs as $record) {
782 $hash = sha1($record->reference);
783 $DB->set_field('files_reference', 'referencehash', $hash, array('id' => $record->id));
784 }
785 $rs->close();
786
79c2b509 787 upgrade_main_savepoint(true, 2012060600.02);
dccba8bc
DM
788 }
789
79c2b509 790 if ($oldversion < 2012060600.03) {
8f47a60e
DM
791 // Merge duplicate records in files_reference that were created during the development
792 // phase at 2.3dev sites. This is needed so we can create the unique index over
793 // (repositoryid, referencehash) fields.
794 $sql = "SELECT repositoryid, referencehash, MIN(id) AS minid
795 FROM {files_reference}
796 GROUP BY repositoryid, referencehash
4a5be0e9 797 HAVING COUNT(*) > 1";
8f47a60e
DM
798 $duprs = $DB->get_recordset_sql($sql);
799 foreach ($duprs as $duprec) {
800 // get the list of all ids in {files_reference} that need to be remapped
801 $dupids = $DB->get_records_select('files_reference', "repositoryid = ? AND referencehash = ? AND id > ?",
802 array($duprec->repositoryid, $duprec->referencehash, $duprec->minid), '', 'id');
803 $dupids = array_keys($dupids);
804 // relink records in {files} that are now referring to a duplicate record
805 // in {files_reference} to refer to the first one
806 list($subsql, $subparams) = $DB->get_in_or_equal($dupids);
807 $DB->set_field_select('files', 'referencefileid', $duprec->minid, "referencefileid $subsql", $subparams);
808 // and finally remove all orphaned records from {files_reference}
809 $DB->delete_records_list('files_reference', 'id', $dupids);
810 }
811 $duprs->close();
812
79c2b509 813 upgrade_main_savepoint(true, 2012060600.03);
8f47a60e
DM
814 }
815
79c2b509 816 if ($oldversion < 2012060600.04) {
6bbf31c3
DM
817 // Add a unique index over repositoryid and referencehash fields in files_reference table
818 $table = new xmldb_table('files_reference');
819 $index = new xmldb_index('uq_external_file', XMLDB_INDEX_UNIQUE, array('repositoryid', 'referencehash'));
820
821 if (!$dbman->index_exists($table, $index)) {
822 $dbman->add_index($table, $index);
823 }
824
79c2b509 825 upgrade_main_savepoint(true, 2012060600.04);
6bbf31c3
DM
826 }
827
a80b5a0c
DP
828 if ($oldversion < 2012061800.01) {
829
830 // Define field screenreader to be dropped from user
831 $table = new xmldb_table('user');
832 $field = new xmldb_field('ajax');
833
834 // Conditionally launch drop field screenreader
835 if ($dbman->field_exists($table, $field)) {
836 $dbman->drop_field($table, $field);
837 }
838
839 // Main savepoint reached
840 upgrade_main_savepoint(true, 2012061800.01);
841 }
842
088c374a 843 if ($oldversion < 2012062000.00) {
38ca2f1a
DM
844 // Add field newcontextid to backup_files_template
845 $table = new xmldb_table('backup_files_template');
846 $field = new xmldb_field('newcontextid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'info');
847
848 if (!$dbman->field_exists($table, $field)) {
849 $dbman->add_field($table, $field);
850 }
851
088c374a 852 upgrade_main_savepoint(true, 2012062000.00);
38ca2f1a
DM
853 }
854
088c374a 855 if ($oldversion < 2012062000.01) {
38ca2f1a
DM
856 // Add field newitemid to backup_files_template
857 $table = new xmldb_table('backup_files_template');
858 $field = new xmldb_field('newitemid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'newcontextid');
859
860 if (!$dbman->field_exists($table, $field)) {
861 $dbman->add_field($table, $field);
862 }
863
088c374a 864 upgrade_main_savepoint(true, 2012062000.01);
38ca2f1a
DM
865 }
866
1a76bbe2
EL
867
868 // Moodle v2.3.0 release upgrade line
869 // Put any upgrade step following this
870
871
872 if ($oldversion < 2012062500.02) {
93fd7cb1
873 // Drop some old backup tables, not used anymore
874
875 // Define table backup_files to be dropped
876 $table = new xmldb_table('backup_files');
877
878 // Conditionally launch drop table for backup_files
879 if ($dbman->table_exists($table)) {
880 $dbman->drop_table($table);
881 }
882
883 // Define table backup_ids to be dropped
884 $table = new xmldb_table('backup_ids');
885
886 // Conditionally launch drop table for backup_ids
887 if ($dbman->table_exists($table)) {
888 $dbman->drop_table($table);
889 }
890
891 // Main savepoint reached
1a76bbe2 892 upgrade_main_savepoint(true, 2012062500.02);
93fd7cb1
893 }
894
95896749
895 if ($oldversion < 2012062501.01) {
896
897 // Define index component-itemid-userid (not unique) to be added to role_assignments
898 $table = new xmldb_table('role_assignments');
899 $index = new xmldb_index('component-itemid-userid', XMLDB_INDEX_NOTUNIQUE, array('component', 'itemid', 'userid'));
900
901 // Conditionally launch add index component-itemid-userid
902 if (!$dbman->index_exists($table, $index)) {
903 $dbman->add_index($table, $index);
904 }
905
906 // Main savepoint reached
907 upgrade_main_savepoint(true, 2012062501.01);
908 }
909
82a0137a
FM
910 if ($oldversion < 2012062501.04) {
911
912 // Saves orphaned questions from the Dark Side
913 upgrade_save_orphaned_questions();
914
915 // Main savepoint reached
916 upgrade_main_savepoint(true, 2012062501.04);
917 }
a80b5a0c 918
fcda7dbb 919 if ($oldversion < 2012062501.06) {
669f2747
AA
920
921 // Handle events with empty eventtype MDL-32827
922
923 $DB->set_field('event', 'eventtype', 'site', array('eventtype' => '', 'courseid' => $SITE->id));
924 $DB->set_field_select('event', 'eventtype', 'due', "eventtype = '' AND courseid != 0 AND groupid = 0 AND (modulename = 'assignment' OR modulename = 'assign')");
925 $DB->set_field_select('event', 'eventtype', 'course', "eventtype = '' AND courseid != 0 AND groupid = 0");
926 $DB->set_field_select('event', 'eventtype', 'group', "eventtype = '' AND groupid != 0");
927 $DB->set_field_select('event', 'eventtype', 'user', "eventtype = '' AND userid != 0");
928
fcda7dbb
AA
929 // Main savepoint reached
930 upgrade_main_savepoint(true, 2012062501.06);
931 }
932
b97532dd
PS
933 if ($oldversion < 2012062501.08) {
934 // Drop obsolete question upgrade field that should have been added to the install.xml.
935 $table = new xmldb_table('question');
936 $field = new xmldb_field('oldquestiontextformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
937
938 if ($dbman->field_exists($table, $field)) {
939 $dbman->drop_field($table, $field);
940 }
941
942 upgrade_main_savepoint(true, 2012062501.08);
943 }
944
a334d8f9
AB
945 if ($oldversion < 2012062501.14) {
946 $subquery = 'SELECT b.id FROM {blog_external} b where b.id = ' . $DB->sql_cast_char2int('{post}.content', true);
c62ccd74
AD
947 $sql = 'DELETE FROM {post}
948 WHERE {post}.module = \'blog_external\'
949 AND NOT EXISTS (' . $subquery . ')
950 AND ' . $DB->sql_isnotempty('post', 'uniquehash', false, false);
951 $DB->execute($sql);
a334d8f9 952 upgrade_main_savepoint(true, 2012062501.14);
c62ccd74
AD
953 }
954
6b69bf46 955 if ($oldversion < 2012062502.03) {
7ae2136b
FM
956 // Some folders still have a sortorder set, which is used for main files but is not
957 // supported by the folder resource. We reset the value here.
958 $sql = 'UPDATE {files} SET sortorder = ? WHERE component = ? AND filearea = ? AND sortorder <> ?';
959 $DB->execute($sql, array(0, 'mod_folder', 'content', 0));
960
961 // Main savepoint reached.
6b69bf46 962 upgrade_main_savepoint(true, 2012062502.03);
7ae2136b
FM
963 }
964
0c09da65
AG
965 if ($oldversion < 2012062502.07) {
966 // Find all orphaned blog associations that might exist.
967 $sql = "SELECT ba.id
968 FROM {blog_association} ba
969 LEFT JOIN {post} p
970 ON p.id = ba.blogid
971 WHERE p.id IS NULL";
972 $orphanedrecordids = $DB->get_records_sql($sql);
973 // Now delete these associations.
974 foreach ($orphanedrecordids as $orphanedrecord) {
975 $DB->delete_records('blog_association', array('id' => $orphanedrecord->id));
976 }
977
978 upgrade_main_savepoint(true, 2012062502.07);
979 }
980
fb5d7df0
PS
981 if ($oldversion < 2012062503.07) {
982 // Remove "_utf8" suffix from all langs in course table.
983 $langs = $DB->get_records_sql("SELECT DISTINCT lang FROM {course} WHERE lang LIKE ?", array('%_utf8'));
984
985 foreach ($langs as $lang=>$unused) {
986 $newlang = str_replace('_utf8', '', $lang);
987 $sql = "UPDATE {course} SET lang = :newlang WHERE lang = :lang";
988 $DB->execute($sql, array('newlang'=>$newlang, 'lang'=>$lang));
989 }
990
991 // Main savepoint reached.
992 upgrade_main_savepoint(true, 2012062503.07);
993 }
994
a4cdd6d2 995 return true;
51003653 996}