Merge branch 'MDL-29538_usercond' of git://github.com/markn86/moodle
[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) {
78946b9b 88 global $CFG, $USER, $DB, $OUTPUT;
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
e9e4a4a6
PS
230 if ($oldversion < 2012030100.02) {
231 // migrate all numbers to signed - it should be safe to interrupt this and continue later
232 upgrade_mysql_fix_unsigned_columns();
233
234 // Main savepoint reached
235 upgrade_main_savepoint(true, 2012030100.02);
236 }
237
436650b0
PS
238 if ($oldversion < 2012030900.01) {
239 // migrate all texts and binaries to big size - it should be safe to interrupt this and continue later
240 upgrade_mysql_fix_lob_columns();
241
242 // Main savepoint reached
243 upgrade_main_savepoint(true, 2012030900.01);
244 }
245
9665ecd2
TH
246 if ($oldversion < 2012031500.01) {
247 // Upgrade old course_allowed_modules data to be permission overrides.
6f3570ff 248 if ($CFG->restrictmodulesfor === 'all') {
9665ecd2 249 $courses = $DB->get_records_menu('course', array(), 'id', 'id, 1');
6f3570ff 250 } else if ($CFG->restrictmodulesfor === 'requested') {
9665ecd2
TH
251 $courses = $DB->get_records_menu('course', array('retrictmodules' => 1), 'id', 'id, 1');
252 } else {
253 $courses = array();
254 }
255
256 if (!$dbman->table_exists('course_allowed_modules')) {
257 // Upgrade must already have been run on this server. This might happen,
258 // for example, during development of these changes.
259 $courses = array();
260 }
261
262 $modidtoname = $DB->get_records_menu('modules', array(), 'id', 'id, name');
263
264 $coursecount = count($courses);
265 if ($coursecount) {
266 $pbar = new progress_bar('allowedmods', 500, true);
267 $transaction = $DB->start_delegated_transaction();
268 }
269
270 $i = 0;
271 foreach ($courses as $courseid => $notused) {
272 $i += 1;
273 upgrade_set_timeout(60); // 1 minute per course should be fine.
274
275 $allowedmoduleids = $DB->get_records_menu('course_allowed_modules',
276 array('course' => $courseid), 'module', 'module, 1');
277 if (empty($allowedmoduleids)) {
278 // This seems to be the best match for backwards compatibility,
8149d624 279 // not necessarily with the old code in course_allowed_module function,
9665ecd2
TH
280 // but with the code that used to be in the coures settings form.
281 $allowedmoduleids = explode(',', $CFG->defaultallowedmodules);
282 $allowedmoduleids = array_combine($allowedmoduleids, $allowedmoduleids);
283 }
284
285 $context = context_course::instance($courseid);
286
287 list($roleids) = get_roles_with_cap_in_context($context, 'moodle/course:manageactivities');
288 list($managerroleids) = get_roles_with_cap_in_context($context, 'moodle/site:config');
289 foreach ($managerroleids as $roleid) {
290 unset($roleids[$roleid]);
291 }
292
293 foreach ($modidtoname as $modid => $modname) {
294 if (isset($allowedmoduleids[$modid])) {
295 // Module is allowed, no worries.
296 continue;
297 }
298
299 $capability = 'mod/' . $modname . ':addinstance';
300 foreach ($roleids as $roleid) {
301 assign_capability($capability, CAP_PREVENT, $roleid, $context);
302 }
303 }
304
305 $pbar->update($i, $coursecount, "Upgrading legacy course_allowed_modules data - $i/$coursecount.");
306 }
307
308 if ($coursecount) {
309 $transaction->allow_commit();
310 }
311
312 upgrade_main_savepoint(true, 2012031500.01);
313 }
314
315 if ($oldversion < 2012031500.02) {
316
317 // Define field retrictmodules to be dropped from course
318 $table = new xmldb_table('course');
4354486d 319 $field = new xmldb_field('restrictmodules');
9665ecd2
TH
320
321 // Conditionally launch drop field requested
322 if ($dbman->field_exists($table, $field)) {
323 $dbman->drop_field($table, $field);
324 }
325
326 upgrade_main_savepoint(true, 2012031500.02);
327 }
328
329 if ($oldversion < 2012031500.03) {
330
331 // Define table course_allowed_modules to be dropped
332 $table = new xmldb_table('course_allowed_modules');
333
334 // Conditionally launch drop table for course_allowed_modules
335 if ($dbman->table_exists($table)) {
336 $dbman->drop_table($table);
337 }
338
339 upgrade_main_savepoint(true, 2012031500.03);
340 }
341
342 if ($oldversion < 2012031500.04) {
343 // Clean up the old admin settings.
344 unset_config('restrictmodulesfor');
345 unset_config('restrictbydefault');
346 unset_config('defaultallowedmodules');
347
348 upgrade_main_savepoint(true, 2012031500.04);
349 }
e9e4a4a6 350
e63f95d7
PS
351 if ($oldversion < 2012032300.02) {
352 // Migrate the old admin debug setting.
353 if ($CFG->debug == 38911) {
354 set_config('debug', DEBUG_DEVELOPER);
355 } else if ($CFG->debug == 6143) {
356 set_config('debug', DEBUG_ALL);
357 }
358 upgrade_main_savepoint(true, 2012032300.02);
359 }
360
e7e0f8d2
DP
361 if ($oldversion < 2012042300.00) {
362 // This change makes the course_section index unique.
363
cf76b335 364 // xmldb does not allow changing index uniqueness - instead we must drop
e7e0f8d2
DP
365 // index then add it again
366 $table = new xmldb_table('course_sections');
cf76b335 367 $index = new xmldb_index('course_section', XMLDB_INDEX_NOTUNIQUE, array('course', 'section'));
e7e0f8d2
DP
368
369 // Conditionally launch drop index course_section
370 if ($dbman->index_exists($table, $index)) {
371 $dbman->drop_index($table, $index);
372 }
cf76b335 373
374 // Look for any duplicate course_sections entries. There should not be
375 // any but on some busy systems we found a few, maybe due to previous
376 // bugs.
377 $transaction = $DB->start_delegated_transaction();
378 $rs = $DB->get_recordset_sql('
379 SELECT DISTINCT
380 cs.id, cs.course
381 FROM
382 {course_sections} cs
383 INNER JOIN {course_sections} older
384 ON cs.course = older.course AND cs.section = older.section
385 AND older.id < cs.id');
386 foreach ($rs as $rec) {
387 $DB->delete_records('course_sections', array('id' => $rec->id));
026082b4
DP
388 // We can't use rebuild_course_cache() here because introducing sectioncache later
389 // so reset modinfo manually.
390 $DB->set_field('course', 'modinfo', null, array('id' => $rec->course));
cf76b335 391 }
392 $rs->close();
393 $transaction->allow_commit();
394
395 // Define index course_section (unique) to be added to course_sections
396 $index = new xmldb_index('course_section', XMLDB_INDEX_UNIQUE, array('course', 'section'));
397
398 // Conditionally launch add index course_section
399 if (!$dbman->index_exists($table, $index)) {
400 $dbman->add_index($table, $index);
401 }
402
403 // Main savepoint reached
e7e0f8d2 404 upgrade_main_savepoint(true, 2012042300.00);
cf76b335 405 }
296c0247 406
ede323e2
ARN
407 if ($oldversion < 2012042300.02) {
408 require_once($CFG->libdir . '/completion/completion_criteria.php');
409 // Delete orphaned criteria which were left when modules were removed
410 if ($DB->get_dbfamily() === 'mysql') {
411 $sql = "DELETE cc FROM {course_completion_criteria} cc
412 LEFT JOIN {course_modules} cm ON cm.id = cc.moduleinstance
413 WHERE cm.id IS NULL AND cc.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
414 } else {
415 $sql = "DELETE FROM {course_completion_criteria}
416 WHERE NOT EXISTS (
417 SELECT 'x' FROM {course_modules}
418 WHERE {course_modules}.id = {course_completion_criteria}.moduleinstance)
2ea2549e 419 AND {course_completion_criteria}.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
ede323e2
ARN
420 }
421 $DB->execute($sql);
422
423 // Main savepoint reached
424 upgrade_main_savepoint(true, 2012042300.02);
425 }
426
0dda790c 427 if ($oldversion < 2012050300.01) {
fa530687
PS
428 // Make sure deleted users do not have picture flag.
429 $DB->set_field('user', 'picture', 0, array('deleted'=>1, 'picture'=>1));
0dda790c 430 upgrade_main_savepoint(true, 2012050300.01);
fa530687
PS
431 }
432
0dda790c 433 if ($oldversion < 2012050300.02) {
4d254790
PS
434
435 // Changing precision of field picture on table user to (10)
436 $table = new xmldb_table('user');
437 $field = new xmldb_field('picture', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'secret');
438
439 // Launch change of precision for field picture
440 $dbman->change_field_precision($table, $field);
441
442 // Main savepoint reached
0dda790c 443 upgrade_main_savepoint(true, 2012050300.02);
4d254790
PS
444 }
445
1156d38d
DP
446 if ($oldversion < 2012050300.03) {
447
448 // Define field coursedisplay to be added to course
449 $table = new xmldb_table('course');
450 $field = new xmldb_field('coursedisplay', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'completionnotify');
451
452 // Conditionally launch add field coursedisplay
453 if (!$dbman->field_exists($table, $field)) {
454 $dbman->add_field($table, $field);
455 }
456
457 // Main savepoint reached
458 upgrade_main_savepoint(true, 2012050300.03);
459 }
460
b9bcdb54
DP
461 if ($oldversion < 2012050300.04) {
462
463 // Define table course_display to be dropped
464 $table = new xmldb_table('course_display');
465
466 // Conditionally launch drop table for course_display
467 if ($dbman->table_exists($table)) {
468 $dbman->drop_table($table);
469 }
470
471 // Main savepoint reached
472 upgrade_main_savepoint(true, 2012050300.04);
473 }
474
475 if ($oldversion < 2012050300.05) {
476
477 // Clean up removed admin setting.
478 unset_config('navlinkcoursesections');
479
480 upgrade_main_savepoint(true, 2012050300.05);
481 }
482
c119a2ee
PS
483 if ($oldversion < 2012050400.01) {
484
485 // Define index sortorder (not unique) to be added to course
486 $table = new xmldb_table('course');
487 $index = new xmldb_index('sortorder', XMLDB_INDEX_NOTUNIQUE, array('sortorder'));
488
489 // Conditionally launch add index sortorder
490 if (!$dbman->index_exists($table, $index)) {
491 $dbman->add_index($table, $index);
492 }
493
494 // Main savepoint reached
495 upgrade_main_savepoint(true, 2012050400.01);
496 }
497
bbb0a6eb
SH
498 if ($oldversion < 2012050400.02) {
499
9fa74379
DP
500 // Clean up removed admin setting.
501 unset_config('enablecourseajax');
502
bbb0a6eb 503 upgrade_main_savepoint(true, 2012050400.02);
9fa74379
DP
504 }
505
74b714df
ARN
506 if ($oldversion < 2012051100.01) {
507
508 // Define field idnumber to be added to groups
509 $table = new xmldb_table('groups');
510 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'courseid');
511 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
512
513 // Conditionally launch add field idnumber
514 if (!$dbman->field_exists($table, $field)) {
515 $dbman->add_field($table, $field);
516 }
517
518 // Conditionally launch add index idnumber
519 if (!$dbman->index_exists($table, $index)) {
520 $dbman->add_index($table, $index);
521 }
522
523 // Define field idnumber to be added to groupings
524 $table = new xmldb_table('groupings');
525 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'name');
526 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
527
528 // Conditionally launch add field idnumber
529 if (!$dbman->field_exists($table, $field)) {
530 $dbman->add_field($table, $field);
531 }
532
533 // Conditionally launch add index idnumber
534 if (!$dbman->index_exists($table, $index)) {
535 $dbman->add_index($table, $index);
536 }
537
538 // Main savepoint reached
539 upgrade_main_savepoint(true, 2012051100.01);
540 }
541
dd420aba
EL
542 if ($oldversion < 2012051100.03) {
543
ce4dfd27 544 // Amend course table to add sectioncache cache
545 $table = new xmldb_table('course');
546 $field = new xmldb_field('sectioncache', XMLDB_TYPE_TEXT, null, null, null, null, null, 'showgrades');
547 if (!$dbman->field_exists($table, $field)) {
548 $dbman->add_field($table, $field);
549 }
550
551 // Amend course_sections to add date, time and groupingid availability
552 // conditions and a setting about whether to show them
553 $table = new xmldb_table('course_sections');
dd420aba 554 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'visible');
ce4dfd27 555 if (!$dbman->field_exists($table, $field)) {
556 $dbman->add_field($table, $field);
557 }
dd420aba 558 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'availablefrom');
ce4dfd27 559 if (!$dbman->field_exists($table, $field)) {
560 $dbman->add_field($table, $field);
561 }
dd420aba 562 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'availableuntil');
ce4dfd27 563 // Conditionally launch add field showavailability
564 if (!$dbman->field_exists($table, $field)) {
565 $dbman->add_field($table, $field);
566 }
dd420aba 567 $field = new xmldb_field('groupingid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'showavailability');
ce4dfd27 568 // Conditionally launch add field groupingid
569 if (!$dbman->field_exists($table, $field)) {
570 $dbman->add_field($table, $field);
571 }
572
573 // Add course_sections_availability to add completion & grade availability conditions
574 $table = new xmldb_table('course_sections_availability');
575
576 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
577 $table->add_field('coursesectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
578 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
579 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', null, null, null, null);
580 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
581 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
582 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
583
584 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
dd420aba
EL
585 $table->add_key('coursesectionid', XMLDB_KEY_FOREIGN, array('coursesectionid'), 'course_sections', array('id'));
586 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
587 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
ce4dfd27 588
589 if (!$dbman->table_exists($table)) {
590 $dbman->create_table($table);
591 }
592
94dc3c7d 593 // Main savepoint reached
dd420aba 594 upgrade_main_savepoint(true, 2012051100.03);
ce4dfd27 595 }
596
33e657c7
SH
597 if ($oldversion < 2012051700.02) {
598 // Define table course_modules_avail_fields to be created
76af15bb
MN
599 $table = new xmldb_table('course_modules_avail_fields');
600
601 // Adding fields to table course_modules_avail_fields
33e657c7
SH
602 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
603 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
76af15bb 604 $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
33e657c7 605 $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
76af15bb
MN
606 $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
607 $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
608
609 // Adding keys to table course_modules_avail_fields
610 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
611 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
612
33e657c7 613 // Conditionally launch create table for course_modules_avail_fields
ce4dfd27 614 if (!$dbman->table_exists($table)) {
e01fbcf7
MN
615 $dbman->create_table($table);
616 }
617
618 // Define table section_modules_avail_fields to be created
619 $table = new xmldb_table('course_sections_avail_fields');
620
621 // Adding fields to table course_modules_section_fields
622 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
623 $table->add_field('coursesectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
624 $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
625 $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
626 $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
627 $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
628
629 // Adding keys to table course_modules_avail_fields
630 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
631 $table->add_key('coursesectionid', XMLDB_KEY_FOREIGN, array('coursesectionid'), 'course_sections', array('id'));
632
633 // Conditionally launch create table for section_modules_avail_fields
634 if (!$dbman->table_exists($table)) {
ce4dfd27 635 $dbman->create_table($table);
636 }
637
94dc3c7d 638 // Main savepoint reached
33e657c7 639 upgrade_main_savepoint(true, 2012051700.02);
ce4dfd27 640 }
641
67233725
DC
642 if ($oldversion < 2012052100.00) {
643
7051415c 644 // Define field referencefileid to be added to files.
67233725
DC
645 $table = new xmldb_table('files');
646
7051415c 647 // Define field referencefileid to be added to files.
67233725 648 $field = new xmldb_field('referencefileid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'sortorder');
7051415c
DC
649
650 // Conditionally launch add field referencefileid.
67233725
DC
651 if (!$dbman->field_exists($table, $field)) {
652 $dbman->add_field($table, $field);
653 }
654
7051415c 655 // Define field referencelastsync to be added to files.
67233725 656 $field = new xmldb_field('referencelastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencefileid');
7051415c
DC
657
658 // Conditionally launch add field referencelastsync.
67233725
DC
659 if (!$dbman->field_exists($table, $field)) {
660 $dbman->add_field($table, $field);
661 }
662
7051415c
DC
663 // Define field referencelifetime to be added to files.
664 $field = new xmldb_field('referencelifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencelastsync');
665
666 // Conditionally launch add field referencelifetime.
67233725
DC
667 if (!$dbman->field_exists($table, $field)) {
668 $dbman->add_field($table, $field);
669 }
670
671 $key = new xmldb_key('referencefileid', XMLDB_KEY_FOREIGN, array('referencefileid'), 'files_reference', array('id'));
672 // Launch add key referencefileid
673 $dbman->add_key($table, $key);
674
675 // Define table files_reference to be created.
676 $table = new xmldb_table('files_reference');
677
678 // Adding fields to table files_reference.
679 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
67233725
DC
680 $table->add_field('repositoryid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
681 $table->add_field('lastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
682 $table->add_field('lifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
683 $table->add_field('reference', XMLDB_TYPE_TEXT, null, null, null, null, null);
684
685 // Adding keys to table files_reference.
686 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
687 $table->add_key('repositoryid', XMLDB_KEY_FOREIGN, array('repositoryid'), 'repository_instances', array('id'));
688
689 // Conditionally launch create table for files_reference
690 if (!$dbman->table_exists($table)) {
691 $dbman->create_table($table);
692 }
693
694 // Main savepoint reached
695 upgrade_main_savepoint(true, 2012052100.00);
696 }
697
3f1bca9d 698 if ($oldversion < 2012052500.03) { // fix invalid course_completion_records MDL-27368
d2e3a1b4
DM
699 //first get all instances of duplicate records
700 $sql = 'SELECT userid, course FROM {course_completions} WHERE (deleted IS NULL OR deleted <> 1) GROUP BY userid, course HAVING (count(id) > 1)';
701 $duplicates = $DB->get_recordset_sql($sql, array());
702
703 foreach ($duplicates as $duplicate) {
704 $pointer = 0;
705 //now get all the records for this user/course
706 $sql = 'userid = ? AND course = ? AND (deleted IS NULL OR deleted <> 1)';
707 $completions = $DB->get_records_select('course_completions', $sql,
708 array($duplicate->userid, $duplicate->course), 'timecompleted DESC, timestarted DESC');
709 $needsupdate = false;
710 $origcompletion = null;
711 foreach ($completions as $completion) {
712 $pointer++;
713 if ($pointer === 1) { //keep 1st record but delete all others.
714 $origcompletion = $completion;
715 } else {
716 //we need to keep the "oldest" of all these fields as the valid completion record.
717 $fieldstocheck = array('timecompleted', 'timestarted', 'timeenrolled');
718 foreach ($fieldstocheck as $f) {
719 if ($origcompletion->$f > $completion->$f) {
720 $origcompletion->$f = $completion->$f;
721 $needsupdate = true;
722 }
723 }
724 $DB->delete_records('course_completions', array('id'=>$completion->id));
725 }
726 }
727 if ($needsupdate) {
728 $DB->update_record('course_completions', $origcompletion);
729 }
730 }
731
732 // Main savepoint reached
3f1bca9d 733 upgrade_main_savepoint(true, 2012052500.03);
d2e3a1b4 734 }
424a19b1 735
bd633dda 736 if ($oldversion < 2012052900.00) {
e6f55285
SH
737 // Clean up all duplicate records in the course_completions table in preparation
738 // for adding a new index there.
424a19b1
AB
739 upgrade_course_completion_remove_duplicates(
740 'course_completions',
741 array('userid', 'course'),
742 array('timecompleted', 'timestarted', 'timeenrolled')
743 );
744
e6f55285
SH
745 // Main savepoint reached
746 upgrade_main_savepoint(true, 2012052900.00);
747 }
748
749 if ($oldversion < 2012052900.01) {
750 // Add indexes to prevent new duplicates in the course_completions table.
424a19b1
AB
751 // Define index useridcourse (unique) to be added to course_completions
752 $table = new xmldb_table('course_completions');
753 $index = new xmldb_index('useridcourse', XMLDB_INDEX_UNIQUE, array('userid', 'course'));
754
424a19b1
AB
755 // Conditionally launch add index useridcourse
756 if (!$dbman->index_exists($table, $index)) {
757 $dbman->add_index($table, $index);
758 }
759
760 // Main savepoint reached
e6f55285 761 upgrade_main_savepoint(true, 2012052900.01);
424a19b1
AB
762 }
763
e6f55285
SH
764 if ($oldversion < 2012052900.02) {
765 // Clean up all duplicate records in the course_completion_crit_compl table in preparation
766 // for adding a new index there.
424a19b1
AB
767 upgrade_course_completion_remove_duplicates(
768 'course_completion_crit_compl',
769 array('userid', 'course', 'criteriaid'),
770 array('timecompleted')
771 );
772
e6f55285
SH
773 // Main savepoint reached
774 upgrade_main_savepoint(true, 2012052900.02);
775 }
424a19b1 776
e6f55285
SH
777 if ($oldversion < 2012052900.03) {
778 // Add indexes to prevent new duplicates in the course_completion_crit_compl table.
424a19b1
AB
779 // Define index useridcoursecriteraid (unique) to be added to course_completion_crit_compl
780 $table = new xmldb_table('course_completion_crit_compl');
781 $index = new xmldb_index('useridcoursecriteraid', XMLDB_INDEX_UNIQUE, array('userid', 'course', 'criteriaid'));
782
783 // Conditionally launch add index useridcoursecriteraid
784 if (!$dbman->index_exists($table, $index)) {
785 $dbman->add_index($table, $index);
786 }
787
788 // Main savepoint reached
e6f55285 789 upgrade_main_savepoint(true, 2012052900.03);
424a19b1
AB
790 }
791
e6f55285
SH
792 if ($oldversion < 2012052900.04) {
793 // Clean up all duplicate records in the course_completion_aggr_methd table in preparation
794 // for adding a new index there.
424a19b1
AB
795 upgrade_course_completion_remove_duplicates(
796 'course_completion_aggr_methd',
797 array('course', 'criteriatype')
798 );
799
e6f55285
SH
800 // Main savepoint reached
801 upgrade_main_savepoint(true, 2012052900.04);
802 }
803
804 if ($oldversion < 2012052900.05) {
805 // Add indexes to prevent new duplicates in the course_completion_aggr_methd table.
424a19b1
AB
806 // Define index coursecriteratype (unique) to be added to course_completion_aggr_methd
807 $table = new xmldb_table('course_completion_aggr_methd');
808 $index = new xmldb_index('coursecriteriatype', XMLDB_INDEX_UNIQUE, array('course', 'criteriatype'));
809
810 // Conditionally launch add index coursecriteratype
811 if (!$dbman->index_exists($table, $index)) {
812 $dbman->add_index($table, $index);
813 }
814
815 // Main savepoint reached
e6f55285 816 upgrade_main_savepoint(true, 2012052900.05);
424a19b1
AB
817 }
818
79c2b509 819 if ($oldversion < 2012060600.01) {
51003653
DM
820 // Add field referencehash to files_reference
821 $table = new xmldb_table('files_reference');
822 $field = new xmldb_field('referencehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'reference');
823 if (!$dbman->field_exists($table, $field)) {
824 $dbman->add_field($table, $field);
825 }
79c2b509 826 upgrade_main_savepoint(true, 2012060600.01);
51003653
DM
827 }
828
79c2b509 829 if ($oldversion < 2012060600.02) {
dccba8bc
DM
830 // Populate referencehash field with SHA1 hash of the reference - this shoudl affect only 2.3dev sites
831 // that were using the feature for testing. Production sites have the table empty.
832 $rs = $DB->get_recordset('files_reference', null, '', 'id, reference');
833 foreach ($rs as $record) {
834 $hash = sha1($record->reference);
835 $DB->set_field('files_reference', 'referencehash', $hash, array('id' => $record->id));
836 }
837 $rs->close();
838
79c2b509 839 upgrade_main_savepoint(true, 2012060600.02);
dccba8bc
DM
840 }
841
79c2b509 842 if ($oldversion < 2012060600.03) {
8f47a60e
DM
843 // Merge duplicate records in files_reference that were created during the development
844 // phase at 2.3dev sites. This is needed so we can create the unique index over
845 // (repositoryid, referencehash) fields.
846 $sql = "SELECT repositoryid, referencehash, MIN(id) AS minid
847 FROM {files_reference}
848 GROUP BY repositoryid, referencehash
4a5be0e9 849 HAVING COUNT(*) > 1";
8f47a60e
DM
850 $duprs = $DB->get_recordset_sql($sql);
851 foreach ($duprs as $duprec) {
852 // get the list of all ids in {files_reference} that need to be remapped
853 $dupids = $DB->get_records_select('files_reference', "repositoryid = ? AND referencehash = ? AND id > ?",
854 array($duprec->repositoryid, $duprec->referencehash, $duprec->minid), '', 'id');
855 $dupids = array_keys($dupids);
856 // relink records in {files} that are now referring to a duplicate record
857 // in {files_reference} to refer to the first one
858 list($subsql, $subparams) = $DB->get_in_or_equal($dupids);
859 $DB->set_field_select('files', 'referencefileid', $duprec->minid, "referencefileid $subsql", $subparams);
860 // and finally remove all orphaned records from {files_reference}
861 $DB->delete_records_list('files_reference', 'id', $dupids);
862 }
863 $duprs->close();
864
79c2b509 865 upgrade_main_savepoint(true, 2012060600.03);
8f47a60e
DM
866 }
867
79c2b509 868 if ($oldversion < 2012060600.04) {
6bbf31c3
DM
869 // Add a unique index over repositoryid and referencehash fields in files_reference table
870 $table = new xmldb_table('files_reference');
871 $index = new xmldb_index('uq_external_file', XMLDB_INDEX_UNIQUE, array('repositoryid', 'referencehash'));
872
873 if (!$dbman->index_exists($table, $index)) {
874 $dbman->add_index($table, $index);
875 }
876
79c2b509 877 upgrade_main_savepoint(true, 2012060600.04);
6bbf31c3
DM
878 }
879
a80b5a0c
DP
880 if ($oldversion < 2012061800.01) {
881
882 // Define field screenreader to be dropped from user
883 $table = new xmldb_table('user');
884 $field = new xmldb_field('ajax');
885
886 // Conditionally launch drop field screenreader
887 if ($dbman->field_exists($table, $field)) {
888 $dbman->drop_field($table, $field);
889 }
890
891 // Main savepoint reached
892 upgrade_main_savepoint(true, 2012061800.01);
893 }
894
088c374a 895 if ($oldversion < 2012062000.00) {
38ca2f1a
DM
896 // Add field newcontextid to backup_files_template
897 $table = new xmldb_table('backup_files_template');
898 $field = new xmldb_field('newcontextid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'info');
899
900 if (!$dbman->field_exists($table, $field)) {
901 $dbman->add_field($table, $field);
902 }
903
088c374a 904 upgrade_main_savepoint(true, 2012062000.00);
38ca2f1a
DM
905 }
906
088c374a 907 if ($oldversion < 2012062000.01) {
38ca2f1a
DM
908 // Add field newitemid to backup_files_template
909 $table = new xmldb_table('backup_files_template');
910 $field = new xmldb_field('newitemid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'newcontextid');
911
912 if (!$dbman->field_exists($table, $field)) {
913 $dbman->add_field($table, $field);
914 }
915
088c374a 916 upgrade_main_savepoint(true, 2012062000.01);
38ca2f1a
DM
917 }
918
44cb29a0
EL
919
920 // Moodle v2.3.0 release upgrade line
921 // Put any upgrade step following this
922
923
924 if ($oldversion < 2012062500.02) {
3f4a2999
925 // Drop some old backup tables, not used anymore
926
927 // Define table backup_files to be dropped
928 $table = new xmldb_table('backup_files');
929
930 // Conditionally launch drop table for backup_files
931 if ($dbman->table_exists($table)) {
932 $dbman->drop_table($table);
933 }
934
935 // Define table backup_ids to be dropped
936 $table = new xmldb_table('backup_ids');
937
938 // Conditionally launch drop table for backup_ids
939 if ($dbman->table_exists($table)) {
940 $dbman->drop_table($table);
941 }
942
943 // Main savepoint reached
44cb29a0 944 upgrade_main_savepoint(true, 2012062500.02);
3f4a2999
945 }
946
a80b5a0c 947
a4cdd6d2 948 return true;
51003653 949}