2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This file keeps track of upgrades to Moodle.
20 * Sometimes, changes between versions involve
21 * alterations to database structures and other
22 * major things that may break installations.
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.
28 * If there's something it cannot do itself, it
29 * will tell you what you need to do.
31 * The commands in here will all be database-neutral,
32 * using the methods of database_manager class
34 * Please do not forget to use upgrade_set_timeout()
35 * before any action that may take longer time to finish.
37 * @package core_install
39 * @copyright 2006 onwards Martin Dougiamas http://dougiamas.com
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 defined('MOODLE_INTERNAL') || die();
46 * Main upgrade tasks to be executed on Moodle version bump
48 * This function is automatically executed after one bump in the Moodle core
49 * version is detected. It's in charge of performing the required tasks
50 * to raise core from the previous version to the next one.
52 * It's a collection of ordered blocks of code, named "upgrade steps",
53 * each one performing one isolated (from the rest of steps) task. Usually
54 * tasks involve creating new DB objects or performing manipulation of the
55 * information for cleanup/fixup purposes.
57 * Each upgrade step has a fixed structure, that can be summarised as follows:
59 * if ($oldversion < XXXXXXXXXX.XX) {
60 * // Explanation of the update step, linking to issue in the Tracker if necessary
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);
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)}.
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
77 * that such a file must be manually included from upgrade.php, and there are some restrictions
78 * about what can be used within it.
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}
84 * @param int $oldversion
85 * @return bool always true
87 function xmldb_main_upgrade($oldversion) {
90 require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions.
92 $dbman = $DB->get_manager(); // Loads ddl manager and xmldb classes.
94 // Always keep this upgrade step with version being the minimum
95 // allowed version to upgrade from (v2.7.0 right now).
96 if ($oldversion < 2014051200) {
97 // Just in case somebody hacks upgrade scripts or env, we really can not continue.
98 echo("You need to upgrade to 2.7.x or higher first!\n");
100 // Note this savepoint is 100% unreachable, but needed to pass the upgrade checks.
101 upgrade_main_savepoint(true, 2014051200);
104 // MDL-32543 Make sure that the log table has correct length for action and url fields.
105 if ($oldversion < 2014051200.02) {
107 $table = new xmldb_table('log');
109 $columns = $DB->get_columns('log');
110 if ($columns['action']->max_length < 40) {
111 $index1 = new xmldb_index('course-module-action', XMLDB_INDEX_NOTUNIQUE, array('course', 'module', 'action'));
112 if ($dbman->index_exists($table, $index1)) {
113 $dbman->drop_index($table, $index1);
115 $index2 = new xmldb_index('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
116 if ($dbman->index_exists($table, $index2)) {
117 $dbman->drop_index($table, $index2);
119 $field = new xmldb_field('action', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'cmid');
120 $dbman->change_field_precision($table, $field);
121 $dbman->add_index($table, $index1);
122 $dbman->add_index($table, $index2);
125 if ($columns['url']->max_length < 100) {
126 $field = new xmldb_field('url', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'action');
127 $dbman->change_field_precision($table, $field);
130 upgrade_main_savepoint(true, 2014051200.02);
133 if ($oldversion < 2014060300.00) {
134 $gspath = get_config('assignfeedback_editpdf', 'gspath');
135 if ($gspath !== false) {
136 set_config('pathtogs', $gspath);
137 unset_config('gspath', 'assignfeedback_editpdf');
139 upgrade_main_savepoint(true, 2014060300.00);
142 if ($oldversion < 2014061000.00) {
143 // Fixing possible wrong MIME type for Publisher files.
144 $filetypes = array('%.pub'=>'application/x-mspublisher');
145 upgrade_mimetypes($filetypes);
146 upgrade_main_savepoint(true, 2014061000.00);
149 if ($oldversion < 2014062600.01) {
150 // We only want to delete DragMath if the directory no longer exists. If the directory
151 // is present then it means it has been restored, so do not perform the uninstall.
152 if (!check_dir_exists($CFG->libdir . '/editor/tinymce/plugins/dragmath', false)) {
153 // Purge DragMath plugin which is incompatible with GNU GPL license.
154 unset_all_config_for_plugin('tinymce_dragmath');
157 // Main savepoint reached.
158 upgrade_main_savepoint(true, 2014062600.01);
161 // Switch the order of the fields in the files_reference index, to improve the performance of search_references.
162 if ($oldversion < 2014070100.00) {
163 $table = new xmldb_table('files_reference');
164 $index = new xmldb_index('uq_external_file', XMLDB_INDEX_UNIQUE, array('repositoryid', 'referencehash'));
165 if ($dbman->index_exists($table, $index)) {
166 $dbman->drop_index($table, $index);
168 upgrade_main_savepoint(true, 2014070100.00);
171 if ($oldversion < 2014070101.00) {
172 $table = new xmldb_table('files_reference');
173 $index = new xmldb_index('uq_external_file', XMLDB_INDEX_UNIQUE, array('referencehash', 'repositoryid'));
174 if (!$dbman->index_exists($table, $index)) {
175 $dbman->add_index($table, $index);
177 upgrade_main_savepoint(true, 2014070101.00);
180 if ($oldversion < 2014072400.01) {
181 $table = new xmldb_table('user_devices');
182 $oldindex = new xmldb_index('pushid-platform', XMLDB_KEY_UNIQUE, array('pushid', 'platform'));
183 if ($dbman->index_exists($table, $oldindex)) {
184 $key = new xmldb_key('pushid-platform', XMLDB_KEY_UNIQUE, array('pushid', 'platform'));
185 $dbman->drop_key($table, $key);
187 upgrade_main_savepoint(true, 2014072400.01);
190 if ($oldversion < 2014080801.00) {
192 // Define index behaviour (not unique) to be added to question_attempts.
193 $table = new xmldb_table('question_attempts');
194 $index = new xmldb_index('behaviour', XMLDB_INDEX_NOTUNIQUE, array('behaviour'));
196 // Conditionally launch add index behaviour.
197 if (!$dbman->index_exists($table, $index)) {
198 $dbman->add_index($table, $index);
201 // Main savepoint reached.
202 upgrade_main_savepoint(true, 2014080801.00);
205 if ($oldversion < 2014082900.01) {
206 // Fixing possible wrong MIME type for 7-zip and Rar files.
208 '%.7z' => 'application/x-7z-compressed',
209 '%.rar' => 'application/x-rar-compressed');
210 upgrade_mimetypes($filetypes);
211 upgrade_main_savepoint(true, 2014082900.01);
214 if ($oldversion < 2014082900.02) {
215 // Replace groupmembersonly usage with new availability system.
216 $transaction = $DB->start_delegated_transaction();
217 if ($CFG->enablegroupmembersonly) {
218 // If it isn't already enabled, we need to enable availability.
219 if (!$CFG->enableavailability) {
220 set_config('enableavailability', 1);
223 // Count all course-modules with groupmembersonly set (for progress
225 $total = $DB->count_records('course_modules', array('groupmembersonly' => 1));
226 $pbar = new progress_bar('upgradegroupmembersonly', 500, true);
228 // Get all these course-modules, one at a time.
229 $rs = $DB->get_recordset('course_modules', array('groupmembersonly' => 1),
232 foreach ($rs as $cm) {
233 // Calculate and set new availability value.
234 $availability = upgrade_group_members_only($cm->groupingid, $cm->availability);
235 $DB->set_field('course_modules', 'availability', $availability,
236 array('id' => $cm->id));
240 $pbar->update($i, $total, "Upgrading groupmembersonly settings - $i/$total.");
245 // Define field groupmembersonly to be dropped from course_modules.
246 $table = new xmldb_table('course_modules');
247 $field = new xmldb_field('groupmembersonly');
249 // Conditionally launch drop field groupmembersonly.
250 if ($dbman->field_exists($table, $field)) {
251 $dbman->drop_field($table, $field);
254 // Unset old config variable.
255 unset_config('enablegroupmembersonly');
256 $transaction->allow_commit();
258 upgrade_main_savepoint(true, 2014082900.02);
261 if ($oldversion < 2014100100.00) {
263 // Define table messageinbound_handlers to be created.
264 $table = new xmldb_table('messageinbound_handlers');
266 // Adding fields to table messageinbound_handlers.
267 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
268 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
269 $table->add_field('classname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
270 $table->add_field('defaultexpiration', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '86400');
271 $table->add_field('validateaddress', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1');
272 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
274 // Adding keys to table messageinbound_handlers.
275 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
276 $table->add_key('classname', XMLDB_KEY_UNIQUE, array('classname'));
278 // Conditionally launch create table for messageinbound_handlers.
279 if (!$dbman->table_exists($table)) {
280 $dbman->create_table($table);
283 // Define table messageinbound_datakeys to be created.
284 $table = new xmldb_table('messageinbound_datakeys');
286 // Adding fields to table messageinbound_datakeys.
287 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
288 $table->add_field('handler', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
289 $table->add_field('datavalue', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
290 $table->add_field('datakey', XMLDB_TYPE_CHAR, '64', null, null, null, null);
291 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
292 $table->add_field('expires', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
294 // Adding keys to table messageinbound_datakeys.
295 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
296 $table->add_key('handler_datavalue', XMLDB_KEY_UNIQUE, array('handler', 'datavalue'));
297 $table->add_key('handler', XMLDB_KEY_FOREIGN, array('handler'), 'messageinbound_handlers', array('id'));
299 // Conditionally launch create table for messageinbound_datakeys.
300 if (!$dbman->table_exists($table)) {
301 $dbman->create_table($table);
304 // Main savepoint reached.
305 upgrade_main_savepoint(true, 2014100100.00);
308 if ($oldversion < 2014100600.01) {
309 // Define field aggregationstatus to be added to grade_grades.
310 $table = new xmldb_table('grade_grades');
311 $field = new xmldb_field('aggregationstatus', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, 'unknown', 'timemodified');
313 // Conditionally launch add field aggregationstatus.
314 if (!$dbman->field_exists($table, $field)) {
315 $dbman->add_field($table, $field);
318 $field = new xmldb_field('aggregationweight', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'aggregationstatus');
320 // Conditionally launch add field aggregationweight.
321 if (!$dbman->field_exists($table, $field)) {
322 $dbman->add_field($table, $field);
325 // Define field aggregationcoef2 to be added to grade_items.
326 $table = new xmldb_table('grade_items');
327 $field = new xmldb_field('aggregationcoef2', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, '0', 'aggregationcoef');
329 // Conditionally launch add field aggregationcoef2.
330 if (!$dbman->field_exists($table, $field)) {
331 $dbman->add_field($table, $field);
334 $field = new xmldb_field('weightoverride', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'needsupdate');
336 // Conditionally launch add field weightoverride.
337 if (!$dbman->field_exists($table, $field)) {
338 $dbman->add_field($table, $field);
341 // Main savepoint reached.
342 upgrade_main_savepoint(true, 2014100600.01);
345 if ($oldversion < 2014100600.02) {
347 // Define field aggregationcoef2 to be added to grade_items_history.
348 $table = new xmldb_table('grade_items_history');
349 $field = new xmldb_field('aggregationcoef2', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, '0', 'aggregationcoef');
351 // Conditionally launch add field aggregationcoef2.
352 if (!$dbman->field_exists($table, $field)) {
353 $dbman->add_field($table, $field);
356 // Main savepoint reached.
357 upgrade_main_savepoint(true, 2014100600.02);
360 if ($oldversion < 2014100600.03) {
362 // Define field weightoverride to be added to grade_items_history.
363 $table = new xmldb_table('grade_items_history');
364 $field = new xmldb_field('weightoverride', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'decimals');
366 // Conditionally launch add field weightoverride.
367 if (!$dbman->field_exists($table, $field)) {
368 $dbman->add_field($table, $field);
371 // Main savepoint reached.
372 upgrade_main_savepoint(true, 2014100600.03);
374 if ($oldversion < 2014100600.04) {
375 // Set flags so we can display a notice on all courses that might
376 // be affected by the uprade to natural aggregation.
377 if (!get_config('grades_sumofgrades_upgrade_flagged', 'core')) {
378 // 13 == SUM_OF_GRADES.
379 $sql = 'SELECT DISTINCT courseid
380 FROM {grade_categories}
381 WHERE aggregation = ?';
382 $courses = $DB->get_records_sql($sql, array(13));
384 foreach ($courses as $course) {
385 set_config('show_sumofgrades_upgrade_' . $course->courseid, 1);
386 // Set each of the grade items to needing an update so that when the user visits the grade reports the
387 // figures will be updated.
388 $DB->set_field('grade_items', 'needsupdate', 1, array('courseid' => $course->courseid));
391 set_config('grades_sumofgrades_upgrade_flagged', 1);
394 // Main savepoint reached.
395 upgrade_main_savepoint(true, 2014100600.04);
398 if ($oldversion < 2014100700.00) {
400 // Define table messageinbound_messagelist to be created.
401 $table = new xmldb_table('messageinbound_messagelist');
403 // Adding fields to table messageinbound_messagelist.
404 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
405 $table->add_field('messageid', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
406 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
407 $table->add_field('address', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
408 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
410 // Adding keys to table messageinbound_messagelist.
411 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
412 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
414 // Conditionally launch create table for messageinbound_messagelist.
415 if (!$dbman->table_exists($table)) {
416 $dbman->create_table($table);
419 // Main savepoint reached.
420 upgrade_main_savepoint(true, 2014100700.00);
423 if ($oldversion < 2014100700.01) {
425 // Define field visible to be added to cohort.
426 $table = new xmldb_table('cohort');
427 $field = new xmldb_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'descriptionformat');
429 // Conditionally launch add field visible.
430 if (!$dbman->field_exists($table, $field)) {
431 $dbman->add_field($table, $field);
434 // Main savepoint reached.
435 upgrade_main_savepoint(true, 2014100700.01);
438 if ($oldversion < 2014100800.00) {
439 // Remove qformat_learnwise (unless it has manually been added back).
440 if (!file_exists($CFG->dirroot . '/question/format/learnwise/format.php')) {
441 unset_all_config_for_plugin('qformat_learnwise');
444 // Main savepoint reached.
445 upgrade_main_savepoint(true, 2014100800.00);
448 if ($oldversion < 2014101001.00) {
449 // Some blocks added themselves to the my/ home page, but they did not declare the
450 // subpage of the default my home page. While the upgrade script has been fixed, this
451 // upgrade script will fix the data that was wrongly added.
453 // We only proceed if we can find the right entry from my_pages. Private => 1 refers to
454 // the constant value MY_PAGE_PRIVATE.
455 if ($systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => 1))) {
457 // Select the blocks there could have been automatically added. showinsubcontexts is hardcoded to 0
458 // because it is possible for administrators to have forced it on the my/ page by adding it to the
459 // system directly rather than updating the default my/ page.
460 $blocks = array('course_overview', 'private_files', 'online_users', 'badges', 'calendar_month', 'calendar_upcoming');
461 list($blocksql, $blockparams) = $DB->get_in_or_equal($blocks, SQL_PARAMS_NAMED);
462 $select = "parentcontextid = :contextid
463 AND pagetypepattern = :page
464 AND showinsubcontexts = 0
465 AND subpagepattern IS NULL
466 AND blockname $blocksql";
468 'contextid' => context_system::instance()->id,
471 $params = array_merge($params, $blockparams);
473 $DB->set_field_select(
482 // Main savepoint reached.
483 upgrade_main_savepoint(true, 2014101001.00);
486 if ($oldversion < 2014102000.00) {
488 // Define field aggregatesubcats to be dropped from grade_categories.
489 $table = new xmldb_table('grade_categories');
490 $field = new xmldb_field('aggregatesubcats');
492 // Conditionally launch drop field aggregatesubcats.
493 if ($dbman->field_exists($table, $field)) {
495 $sql = 'SELECT DISTINCT courseid
496 FROM {grade_categories}
497 WHERE aggregatesubcats = ?';
498 $courses = $DB->get_records_sql($sql, array(1));
500 foreach ($courses as $course) {
501 set_config('show_aggregatesubcats_upgrade_' . $course->courseid, 1);
502 // Set each of the grade items to needing an update so that when the user visits the grade reports the
503 // figures will be updated.
504 $DB->set_field('grade_items', 'needsupdate', 1, array('courseid' => $course->courseid));
508 $dbman->drop_field($table, $field);
511 // Main savepoint reached.
512 upgrade_main_savepoint(true, 2014102000.00);
515 if ($oldversion < 2014110300.00) {
516 // Run script restoring missing folder records for draft file areas.
517 upgrade_fix_missing_root_folders_draft();
519 // Main savepoint reached.
520 upgrade_main_savepoint(true, 2014110300.00);
523 // Moodle v2.8.0 release upgrade line.
524 // Put any upgrade step following this.
526 if ($oldversion < 2014111000.00) {
527 // Coming from 2.7 or older, we need to flag the step minmaxgrade to be ignored.
528 set_config('upgrade_minmaxgradestepignored', 1);
529 // Coming from 2.7 or older, we need to flag the step for changing calculated grades to be regraded.
530 set_config('upgrade_calculatedgradeitemsonlyregrade', 1);
532 // Main savepoint reached.
533 upgrade_main_savepoint(true, 2014111000.00);
536 if ($oldversion < 2014120100.00) {
538 // Define field sslverification to be added to mnet_host.
539 $table = new xmldb_table('mnet_host');
540 $field = new xmldb_field('sslverification', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'applicationid');
542 // Conditionally launch add field sslverification.
543 if (!$dbman->field_exists($table, $field)) {
544 $dbman->add_field($table, $field);
547 // Main savepoint reached.
548 upgrade_main_savepoint(true, 2014120100.00);
551 if ($oldversion < 2014120101.00) {
553 // Define field component to be added to comments.
554 $table = new xmldb_table('comments');
555 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'contextid');
557 // Conditionally launch add field component.
558 if (!$dbman->field_exists($table, $field)) {
559 $dbman->add_field($table, $field);
562 // Main savepoint reached.
563 upgrade_main_savepoint(true, 2014120101.00);
566 if ($oldversion < 2014120102.00) {
568 // Define table user_password_history to be created.
569 $table = new xmldb_table('user_password_history');
571 // Adding fields to table user_password_history.
572 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
573 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
574 $table->add_field('hash', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
575 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
577 // Adding keys to table user_password_history.
578 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
579 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
581 // Conditionally launch create table for user_password_history.
582 if (!$dbman->table_exists($table)) {
583 $dbman->create_table($table);
586 // Main savepoint reached.
587 upgrade_main_savepoint(true, 2014120102.00);
590 if ($oldversion < 2015010800.01) {
592 // Make sure the private files handler is not set to expire.
593 $DB->set_field('messageinbound_handlers', 'defaultexpiration', 0,
594 array('classname' => '\core\message\inbound\private_files_handler'));
596 // Main savepoint reached.
597 upgrade_main_savepoint(true, 2015010800.01);
601 if ($oldversion < 2015012600.00) {
603 // If the site is using internal and external storage, or just external
604 // storage, and the external path specified is empty we change the setting
605 // to internal only. That is how the backup code is handling this
607 $storage = (int) get_config('backup', 'backup_auto_storage');
608 $folder = get_config('backup', 'backup_auto_destination');
609 if ($storage !== 0 && empty($folder)) {
610 set_config('backup_auto_storage', 0, 'backup');
613 // Main savepoint reached.
614 upgrade_main_savepoint(true, 2015012600.00);
617 if ($oldversion < 2015012600.01) {
619 // Convert calendar_lookahead to nearest new value.
620 $value = $DB->get_field('config', 'value', array('name' => 'calendar_lookahead'));
622 set_config('calendar_lookahead', '120');
623 } else if ($value > 60 and $value < 90) {
624 set_config('calendar_lookahead', '90');
625 } else if ($value > 30 and $value < 60) {
626 set_config('calendar_lookahead', '60');
627 } else if ($value > 21 and $value < 30) {
628 set_config('calendar_lookahead', '30');
629 } else if ($value > 14 and $value < 21) {
630 set_config('calendar_lookahead', '21');
631 } else if ($value > 7 and $value < 14) {
632 set_config('calendar_lookahead', '14');
635 // Main savepoint reached.
636 upgrade_main_savepoint(true, 2015012600.01);
639 if ($oldversion < 2015021100.00) {
641 // Define field timemodified to be added to registration_hubs.
642 $table = new xmldb_table('registration_hubs');
643 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'secret');
645 // Conditionally launch add field timemodified.
646 if (!$dbman->field_exists($table, $field)) {
647 $dbman->add_field($table, $field);
650 // Main savepoint reached.
651 upgrade_main_savepoint(true, 2015021100.00);
654 if ($oldversion < 2015022401.00) {
656 // Define index useridfromto (not unique) to be added to message.
657 $table = new xmldb_table('message');
658 $index = new xmldb_index('useridfromto', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'useridto'));
660 // Conditionally launch add index useridfromto.
661 if (!$dbman->index_exists($table, $index)) {
662 $dbman->add_index($table, $index);
665 // Define index useridfromto (not unique) to be added to message_read.
666 $table = new xmldb_table('message_read');
667 $index = new xmldb_index('useridfromto', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'useridto'));
669 // Conditionally launch add index useridfromto.
670 if (!$dbman->index_exists($table, $index)) {
671 $dbman->add_index($table, $index);
674 // Main savepoint reached.
675 upgrade_main_savepoint(true, 2015022401.00);
678 if ($oldversion < 2015022500.00) {
679 $table = new xmldb_table('user_devices');
680 $index = new xmldb_index('uuid-userid', XMLDB_INDEX_NOTUNIQUE, array('uuid', 'userid'));
681 if (!$dbman->index_exists($table, $index)) {
682 $dbman->add_index($table, $index);
684 upgrade_main_savepoint(true, 2015022500.00);
687 if ($oldversion < 2015030400.00) {
688 // We have long since switched to storing timemodified per hub rather than a single 'registered' timestamp.
689 unset_config('registered');
690 upgrade_main_savepoint(true, 2015030400.00);
693 if ($oldversion < 2015031100.00) {
694 // Unset old config variable.
695 unset_config('enabletgzbackups');
697 upgrade_main_savepoint(true, 2015031100.00);
700 if ($oldversion < 2015031400.00) {
702 // Define index useridfrom (not unique) to be dropped form message.
703 $table = new xmldb_table('message');
704 $index = new xmldb_index('useridfrom', XMLDB_INDEX_NOTUNIQUE, array('useridfrom'));
706 // Conditionally launch drop index useridfrom.
707 if ($dbman->index_exists($table, $index)) {
708 $dbman->drop_index($table, $index);
711 // Define index useridfrom (not unique) to be dropped form message_read.
712 $table = new xmldb_table('message_read');
713 $index = new xmldb_index('useridfrom', XMLDB_INDEX_NOTUNIQUE, array('useridfrom'));
715 // Conditionally launch drop index useridfrom.
716 if ($dbman->index_exists($table, $index)) {
717 $dbman->drop_index($table, $index);
720 // Main savepoint reached.
721 upgrade_main_savepoint(true, 2015031400.00);
724 if ($oldversion < 2015031900.01) {
725 unset_config('crontime', 'registration');
726 upgrade_main_savepoint(true, 2015031900.01);
729 if ($oldversion < 2015032000.00) {
730 $table = new xmldb_table('badge_criteria');
732 $field = new xmldb_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
733 // Conditionally add description field to the badge_criteria table.
734 if (!$dbman->field_exists($table, $field)) {
735 $dbman->add_field($table, $field);
738 $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, 2, null, XMLDB_NOTNULL, null, 0);
739 // Conditionally add description format field to the badge_criteria table.
740 if (!$dbman->field_exists($table, $field)) {
741 $dbman->add_field($table, $field);
744 upgrade_main_savepoint(true, 2015032000.00);
747 if ($oldversion < 2015040200.01) {
748 // Force uninstall of deleted tool.
749 if (!file_exists("$CFG->dirroot/$CFG->admin/tool/timezoneimport")) {
750 // Remove capabilities.
751 capabilities_cleanup('tool_timezoneimport');
752 // Remove all other associated config.
753 unset_all_config_for_plugin('tool_timezoneimport');
755 upgrade_main_savepoint(true, 2015040200.01);
758 if ($oldversion < 2015040200.02) {
759 // Define table timezone to be dropped.
760 $table = new xmldb_table('timezone');
761 // Conditionally launch drop table for timezone.
762 if ($dbman->table_exists($table)) {
763 $dbman->drop_table($table);
765 upgrade_main_savepoint(true, 2015040200.02);
768 if ($oldversion < 2015040200.03) {
769 if (isset($CFG->timezone) and $CFG->timezone == 99) {
770 // Migrate to real server timezone.
771 unset_config('timezone');
773 upgrade_main_savepoint(true, 2015040200.03);
776 if ($oldversion < 2015040700.01) {
777 $DB->delete_records('config_plugins', array('name' => 'requiremodintro'));
778 upgrade_main_savepoint(true, 2015040700.01);
781 if ($oldversion < 2015040900.01) {
782 // Add "My grades" to the user menu.
783 $oldconfig = get_config('core', 'customusermenuitems');
784 if (strpos("mygrades,grades|/grade/report/mygrades.php|grades", $oldconfig) === false) {
785 $newconfig = "mygrades,grades|/grade/report/mygrades.php|grades\n" . $oldconfig;
786 set_config('customusermenuitems', $newconfig);
789 upgrade_main_savepoint(true, 2015040900.01);
792 if ($oldversion < 2015040900.02) {
793 // Update the default user menu (add preferences, remove my files and my badges).
794 $oldconfig = get_config('core', 'customusermenuitems');
796 // Add "My preferences" at the end.
797 if (strpos($oldconfig, "mypreferences,moodle|/user/preference.php|preferences") === false) {
798 $newconfig = $oldconfig . "\nmypreferences,moodle|/user/preferences.php|preferences";
800 $newconfig = $oldconfig;
803 $newconfig = str_replace("myfiles,moodle|/user/files.php|download", "", $newconfig);
805 $newconfig = str_replace("mybadges,badges|/badges/mybadges.php|award", "", $newconfig);
807 $newconfig = preg_replace('/\n+/', "\n", $newconfig);
808 $newconfig = preg_replace('/(\r\n)+/', "\n", $newconfig);
809 set_config('customusermenuitems', $newconfig);
811 upgrade_main_savepoint(true, 2015040900.02);
814 if ($oldversion < 2015050400.00) {
815 $config = get_config('core', 'customusermenuitems');
817 // Change "My preferences" in the user menu to "Preferences".
818 $config = str_replace("mypreferences,moodle|/user/preferences.php|preferences",
819 "preferences,moodle|/user/preferences.php|preferences", $config);
821 // Change "My grades" in the user menu to "Grades".
822 $config = str_replace("mygrades,grades|/grade/report/mygrades.php|grades",
823 "grades,grades|/grade/report/mygrades.php|grades", $config);
825 set_config('customusermenuitems', $config);
827 upgrade_main_savepoint(true, 2015050400.00);
830 if ($oldversion < 2015050401.00) {
831 // Make sure we have messages in the user menu because it's no longer in the nav tree.
832 $oldconfig = get_config('core', 'customusermenuitems');
833 $messagesconfig = "messages,message|/message/index.php|message";
834 $preferencesconfig = "preferences,moodle|/user/preferences.php|preferences";
837 if (strpos($oldconfig, $messagesconfig) === false) {
838 // See if preferences exists.
839 if (strpos($oldconfig, "preferences,moodle|/user/preferences.php|preferences") !== false) {
840 // Insert it before preferences.
841 $newconfig = str_replace($preferencesconfig, $messagesconfig . "\n" . $preferencesconfig, $oldconfig);
843 // Custom config - we can only insert it at the end.
844 $newconfig = $oldconfig . "\n" . $messagesconfig;
846 set_config('customusermenuitems', $newconfig);
849 upgrade_main_savepoint(true, 2015050401.00);
852 // Moodle v2.9.0 release upgrade line.
853 // Put any upgrade step following this.
855 if ($oldversion < 2015060400.02) {
857 // Sites that were upgrading from 2.7 and older will ignore this step.
858 if (empty($CFG->upgrade_minmaxgradestepignored)) {
860 upgrade_minmaxgrade();
862 // Flags this upgrade step as already run to prevent it from running multiple times.
863 set_config('upgrade_minmaxgradestepignored', 1);
866 upgrade_main_savepoint(true, 2015060400.02);
869 if ($oldversion < 2015061900.00) {
870 // MDL-49257. Changed the algorithm of calculating automatic weights of extra credit items.
872 // Before the change, in case when grade category (in "Natural" agg. method) had items with
873 // overridden weights, the automatic weight of extra credit items was illogical.
874 // In order to prevent grades changes after the upgrade we need to freeze gradebook calculation
875 // for the affected courses.
877 // This script in included in each major version upgrade process so make sure we don't run it twice.
878 if (empty($CFG->upgrade_extracreditweightsstepignored)) {
879 upgrade_extra_credit_weightoverride();
881 // To skip running the same script on the upgrade to the next major release.
882 set_config('upgrade_extracreditweightsstepignored', 1);
885 // Main savepoint reached.
886 upgrade_main_savepoint(true, 2015061900.00);
889 if ($oldversion < 2015062500.01) {
890 // MDL-48239. Changed calculated grade items so that the maximum and minimum grade can be set.
892 // If the changes are accepted and a regrade is done on the gradebook then some grades may change significantly.
893 // This is here to freeze the gradebook in affected courses.
895 // This script is included in each major version upgrade process so make sure we don't run it twice.
896 if (empty($CFG->upgrade_calculatedgradeitemsignored)) {
897 upgrade_calculated_grade_items();
899 // To skip running the same script on the upgrade to the next major release.
900 set_config('upgrade_calculatedgradeitemsignored', 1);
901 // This config value is never used again.
902 unset_config('upgrade_calculatedgradeitemsonlyregrade');
905 // Main savepoint reached.
906 upgrade_main_savepoint(true, 2015062500.01);
909 if ($oldversion < 2015081300.01) {
911 // Define field importtype to be added to grade_import_values.
912 $table = new xmldb_table('grade_import_values');
913 $field = new xmldb_field('importonlyfeedback', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'importer');
915 // Conditionally launch add field importtype.
916 if (!$dbman->field_exists($table, $field)) {
917 $dbman->add_field($table, $field);
920 // Main savepoint reached.
921 upgrade_main_savepoint(true, 2015081300.01);
924 if ($oldversion < 2015082400.00) {
926 // Define table webdav_locks to be dropped.
927 $table = new xmldb_table('webdav_locks');
929 // Conditionally launch drop table for webdav_locks.
930 if ($dbman->table_exists($table)) {
931 $dbman->drop_table($table);
934 // Main savepoint reached.
935 upgrade_main_savepoint(true, 2015082400.00);
938 if ($oldversion < 2015090200.00) {
939 $table = new xmldb_table('message');
941 // Define the deleted fields to be added to the message tables.
942 $field1 = new xmldb_field('timeuserfromdeleted', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0',
944 $field2 = new xmldb_field('timeusertodeleted', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0',
946 $oldindex = new xmldb_index('useridfromto', XMLDB_INDEX_NOTUNIQUE,
947 array('useridfrom', 'useridto'));
948 $newindex = new xmldb_index('useridfromtodeleted', XMLDB_INDEX_NOTUNIQUE,
949 array('useridfrom', 'useridto', 'timeuserfromdeleted', 'timeusertodeleted'));
951 // Conditionally launch add field timeuserfromdeleted.
952 if (!$dbman->field_exists($table, $field1)) {
953 $dbman->add_field($table, $field1);
956 // Conditionally launch add field timeusertodeleted.
957 if (!$dbman->field_exists($table, $field2)) {
958 $dbman->add_field($table, $field2);
961 // Conditionally launch drop index useridfromto.
962 if ($dbman->index_exists($table, $oldindex)) {
963 $dbman->drop_index($table, $oldindex);
966 // Conditionally launch add index useridfromtodeleted.
967 if (!$dbman->index_exists($table, $newindex)) {
968 $dbman->add_index($table, $newindex);
971 // Now add them to the message_read table.
972 $table = new xmldb_table('message_read');
974 // Conditionally launch add field timeuserfromdeleted.
975 if (!$dbman->field_exists($table, $field1)) {
976 $dbman->add_field($table, $field1);
979 // Conditionally launch add field timeusertodeleted.
980 if (!$dbman->field_exists($table, $field2)) {
981 $dbman->add_field($table, $field2);
984 // Conditionally launch drop index useridfromto.
985 if ($dbman->index_exists($table, $oldindex)) {
986 $dbman->drop_index($table, $oldindex);
989 // Conditionally launch add index useridfromtodeleted.
990 if (!$dbman->index_exists($table, $newindex)) {
991 $dbman->add_index($table, $newindex);
994 // Main savepoint reached.
995 upgrade_main_savepoint(true, 2015090200.00);
998 if ($oldversion < 2015090801.00) {
999 // This upgrade script merges all tag instances pointing to the same course tag.
1000 // User id is no longer used for those tag instances.
1001 upgrade_course_tags();
1003 // If configuration variable "Show course tags" is set, disable the block
1004 // 'tags' because it can not be used for tagging courses any more.
1005 if (!empty($CFG->block_tags_showcoursetags)) {
1006 if ($record = $DB->get_record('block', array('name' => 'tags'), 'id, visible')) {
1007 if ($record->visible) {
1008 $DB->update_record('block', array('id' => $record->id, 'visible' => 0));
1013 // Define index idname (unique) to be dropped form tag (it's really weird).
1014 $table = new xmldb_table('tag');
1015 $index = new xmldb_index('idname', XMLDB_INDEX_UNIQUE, array('id', 'name'));
1017 // Conditionally launch drop index idname.
1018 if ($dbman->index_exists($table, $index)) {
1019 $dbman->drop_index($table, $index);
1022 // Main savepoint reached.
1023 upgrade_main_savepoint(true, 2015090801.00);
1026 if ($oldversion < 2015092200.00) {
1027 // Define index qtype (not unique) to be added to question.
1028 $table = new xmldb_table('question');
1029 $index = new xmldb_index('qtype', XMLDB_INDEX_NOTUNIQUE, array('qtype'));
1031 // Conditionally launch add index qtype.
1032 if (!$dbman->index_exists($table, $index)) {
1033 $dbman->add_index($table, $index);
1036 // Main savepoint reached.
1037 upgrade_main_savepoint(true, 2015092200.00);
1040 if ($oldversion < 2015092900.00) {
1041 // Rename backup_auto_keep setting to backup_auto_max_kept.
1042 $keep = get_config('backup', 'backup_auto_keep');
1043 if ($keep !== false) {
1044 set_config('backup_auto_max_kept', $keep, 'backup');
1045 unset_config('backup_auto_keep', 'backup');
1048 // Main savepoint reached.
1049 upgrade_main_savepoint(true, 2015092900.00);
1052 if ($oldversion < 2015100600.00) {
1054 // Define index notification (not unique) to be added to message_read.
1055 $table = new xmldb_table('message_read');
1056 $index = new xmldb_index('notificationtimeread', XMLDB_INDEX_NOTUNIQUE, array('notification', 'timeread'));
1058 // Conditionally launch add index notification.
1059 if (!$dbman->index_exists($table, $index)) {
1060 $dbman->add_index($table, $index);
1063 // Main savepoint reached.
1064 upgrade_main_savepoint(true, 2015100600.00);
1067 if ($oldversion < 2015100800.01) {
1068 // The only flag for preventing all plugins installation features is
1069 // now $CFG->disableupdateautodeploy in config.php.
1070 unset_config('updateautodeploy');
1071 upgrade_main_savepoint(true, 2015100800.01);
1074 // Moodle v3.0.0 release upgrade line.
1075 // Put any upgrade step following this.
1077 if ($oldversion < 2016011300.01) {
1079 // This is a big upgrade script. We create new table tag_coll and the field
1080 // tag.tagcollid pointing to it.
1082 // Define table tag_coll to be created.
1083 $table = new xmldb_table('tag_coll');
1085 // Adding fields to table tagcloud.
1086 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1087 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null);
1088 $table->add_field('isdefault', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
1089 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1090 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '5', null, XMLDB_NOTNULL, null, '0');
1091 $table->add_field('searchable', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
1092 $table->add_field('customurl', XMLDB_TYPE_CHAR, '255', null, null, null, null);
1094 // Adding keys to table tagcloud.
1095 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1097 // Conditionally launch create table for tagcloud.
1098 if (!$dbman->table_exists($table)) {
1099 $dbman->create_table($table);
1103 // Define index name (unique) to be dropped form tag - we will replace it with index on (tagcollid,name) later.
1104 $table = new xmldb_table('tag');
1105 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
1107 // Conditionally launch drop index name.
1108 if ($dbman->index_exists($table, $index)) {
1109 $dbman->drop_index($table, $index);
1112 // Define field tagcollid to be added to tag, we create it as null first and will change to notnull later.
1113 $table = new xmldb_table('tag');
1114 $field = new xmldb_field('tagcollid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'userid');
1116 // Conditionally launch add field tagcloudid.
1117 if (!$dbman->field_exists($table, $field)) {
1118 $dbman->add_field($table, $field);
1121 // Main savepoint reached.
1122 upgrade_main_savepoint(true, 2016011300.01);
1125 if ($oldversion < 2016011300.02) {
1126 // Create a default tag collection if not exists and update the field tag.tagcollid to point to it.
1127 if (!$tcid = $DB->get_field_sql('SELECT id FROM {tag_coll} ORDER BY isdefault DESC, sortorder, id', null,
1129 $tcid = $DB->insert_record('tag_coll', array('isdefault' => 1, 'sortorder' => 0));
1131 $DB->execute('UPDATE {tag} SET tagcollid = ? WHERE tagcollid IS NULL', array($tcid));
1133 // Define index tagcollname (unique) to be added to tag.
1134 $table = new xmldb_table('tag');
1135 $index = new xmldb_index('tagcollname', XMLDB_INDEX_UNIQUE, array('tagcollid', 'name'));
1136 $field = new xmldb_field('tagcollid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'userid');
1138 // Conditionally launch add index tagcollname.
1139 if (!$dbman->index_exists($table, $index)) {
1140 // Launch change of nullability for field tagcollid.
1141 $dbman->change_field_notnull($table, $field);
1142 $dbman->add_index($table, $index);
1145 // Define key tagcollid (foreign) to be added to tag.
1146 $table = new xmldb_table('tag');
1147 $key = new xmldb_key('tagcollid', XMLDB_KEY_FOREIGN, array('tagcollid'), 'tag_coll', array('id'));
1149 // Launch add key tagcloudid.
1150 $dbman->add_key($table, $key);
1152 // Main savepoint reached.
1153 upgrade_main_savepoint(true, 2016011300.02);
1156 if ($oldversion < 2016011300.03) {
1158 // Define table tag_area to be created.
1159 $table = new xmldb_table('tag_area');
1161 // Adding fields to table tag_area.
1162 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1163 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1164 $table->add_field('itemtype', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1165 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
1166 $table->add_field('tagcollid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1167 $table->add_field('callback', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1168 $table->add_field('callbackfile', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1170 // Adding keys to table tag_area.
1171 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1172 $table->add_key('tagcollid', XMLDB_KEY_FOREIGN, array('tagcollid'), 'tag_coll', array('id'));
1174 // Adding indexes to table tag_area.
1175 $table->add_index('compitemtype', XMLDB_INDEX_UNIQUE, array('component', 'itemtype'));
1177 // Conditionally launch create table for tag_area.
1178 if (!$dbman->table_exists($table)) {
1179 $dbman->create_table($table);
1182 // Main savepoint reached.
1183 upgrade_main_savepoint(true, 2016011300.03);
1186 if ($oldversion < 2016011300.04) {
1188 // Define index itemtype-itemid-tagid-tiuserid (unique) to be dropped form tag_instance.
1189 $table = new xmldb_table('tag_instance');
1190 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid', XMLDB_INDEX_UNIQUE,
1191 array('itemtype', 'itemid', 'tagid', 'tiuserid'));
1193 // Conditionally launch drop index itemtype-itemid-tagid-tiuserid.
1194 if ($dbman->index_exists($table, $index)) {
1195 $dbman->drop_index($table, $index);
1198 // Main savepoint reached.
1199 upgrade_main_savepoint(true, 2016011300.04);
1202 if ($oldversion < 2016011300.05) {
1204 $DB->execute("UPDATE {tag_instance} SET component = ? WHERE component IS NULL", array(''));
1206 // Changing nullability of field component on table tag_instance to not null.
1207 $table = new xmldb_table('tag_instance');
1208 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'tagid');
1210 // Launch change of nullability for field component.
1211 $dbman->change_field_notnull($table, $field);
1213 // Changing type of field itemtype on table tag_instance to char.
1214 $table = new xmldb_table('tag_instance');
1215 $field = new xmldb_field('itemtype', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'component');
1217 // Launch change of type for field itemtype.
1218 $dbman->change_field_type($table, $field);
1220 // Main savepoint reached.
1221 upgrade_main_savepoint(true, 2016011300.05);
1224 if ($oldversion < 2016011300.06) {
1226 // Define index taggeditem (unique) to be added to tag_instance.
1227 $table = new xmldb_table('tag_instance');
1228 $index = new xmldb_index('taggeditem', XMLDB_INDEX_UNIQUE, array('component', 'itemtype', 'itemid', 'tiuserid', 'tagid'));
1230 // Conditionally launch add index taggeditem.
1231 if (!$dbman->index_exists($table, $index)) {
1232 $dbman->add_index($table, $index);
1235 // Main savepoint reached.
1236 upgrade_main_savepoint(true, 2016011300.06);
1239 if ($oldversion < 2016011300.07) {
1241 // Define index taglookup (not unique) to be added to tag_instance.
1242 $table = new xmldb_table('tag_instance');
1243 $index = new xmldb_index('taglookup', XMLDB_INDEX_NOTUNIQUE, array('itemtype', 'component', 'tagid', 'contextid'));
1245 // Conditionally launch add index taglookup.
1246 if (!$dbman->index_exists($table, $index)) {
1247 $dbman->add_index($table, $index);
1250 // Main savepoint reached.
1251 upgrade_main_savepoint(true, 2016011300.07);
1254 if ($oldversion < 2016011301.00) {
1256 // Force uninstall of deleted tool.
1257 if (!file_exists("$CFG->dirroot/webservice/amf")) {
1258 // Remove capabilities.
1259 capabilities_cleanup('webservice_amf');
1260 // Remove all other associated config.
1261 unset_all_config_for_plugin('webservice_amf');
1263 upgrade_main_savepoint(true, 2016011301.00);
1266 if ($oldversion < 2016011901.00) {
1268 // Convert calendar_lookahead to nearest new value.
1269 $transaction = $DB->start_delegated_transaction();
1271 // Count all users who curretly have that preference set (for progress bar).
1272 $total = $DB->count_records_select('user_preferences', "name = 'calendar_lookahead' AND value != '0'");
1273 $pbar = new progress_bar('upgradecalendarlookahead', 500, true);
1275 // Get all these users, one at a time.
1276 $rs = $DB->get_recordset_select('user_preferences', "name = 'calendar_lookahead' AND value != '0'");
1278 foreach ($rs as $userpref) {
1280 // Calculate and set new lookahead value.
1281 if ($userpref->value > 90) {
1283 } else if ($userpref->value > 60 and $userpref->value < 90) {
1285 } else if ($userpref->value > 30 and $userpref->value < 60) {
1287 } else if ($userpref->value > 21 and $userpref->value < 30) {
1289 } else if ($userpref->value > 14 and $userpref->value < 21) {
1291 } else if ($userpref->value > 7 and $userpref->value < 14) {
1294 $newvalue = $userpref->value;
1297 $DB->set_field('user_preferences', 'value', $newvalue, array('id' => $userpref->id));
1301 $pbar->update($i, $total, "Upgrading user preference settings - $i/$total.");
1304 $transaction->allow_commit();
1306 upgrade_main_savepoint(true, 2016011901.00);
1309 if ($oldversion < 2016020200.00) {
1311 // Define field isstandard to be added to tag.
1312 $table = new xmldb_table('tag');
1313 $field = new xmldb_field('isstandard', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'rawname');
1315 // Conditionally launch add field isstandard.
1316 if (!$dbman->field_exists($table, $field)) {
1317 $dbman->add_field($table, $field);
1320 // Define index tagcolltype (not unique) to be dropped form tag.
1321 // This index is no longer created however it was present at some point and it's better to be safe and try to drop it.
1322 $index = new xmldb_index('tagcolltype', XMLDB_INDEX_NOTUNIQUE, array('tagcollid', 'tagtype'));
1324 // Conditionally launch drop index tagcolltype.
1325 if ($dbman->index_exists($table, $index)) {
1326 $dbman->drop_index($table, $index);
1329 // Define index tagcolltype (not unique) to be added to tag.
1330 $index = new xmldb_index('tagcolltype', XMLDB_INDEX_NOTUNIQUE, array('tagcollid', 'isstandard'));
1332 // Conditionally launch add index tagcolltype.
1333 if (!$dbman->index_exists($table, $index)) {
1334 $dbman->add_index($table, $index);
1337 // Define field tagtype to be dropped from tag.
1338 $field = new xmldb_field('tagtype');
1340 // Conditionally launch drop field tagtype and update isstandard.
1341 if ($dbman->field_exists($table, $field)) {
1342 $DB->execute("UPDATE {tag} SET isstandard=(CASE WHEN (tagtype = ?) THEN 1 ELSE 0 END)", array('official'));
1343 $dbman->drop_field($table, $field);
1346 // Main savepoint reached.
1347 upgrade_main_savepoint(true, 2016020200.00);
1350 if ($oldversion < 2016020201.00) {
1352 // Define field showstandard to be added to tag_area.
1353 $table = new xmldb_table('tag_area');
1354 $field = new xmldb_field('showstandard', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'callbackfile');
1356 // Conditionally launch add field showstandard.
1357 if (!$dbman->field_exists($table, $field)) {
1358 $dbman->add_field($table, $field);
1361 // By default set user area to hide standard tags. 2 = core_tag_tag::HIDE_STANDARD (can not use constant here).
1362 $DB->execute("UPDATE {tag_area} SET showstandard = ? WHERE itemtype = ? AND component = ?",
1363 array(2, 'user', 'core'));
1365 // Changing precision of field enabled on table tag_area to (1).
1366 $table = new xmldb_table('tag_area');
1367 $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'itemtype');
1369 // Launch change of precision for field enabled.
1370 $dbman->change_field_precision($table, $field);
1372 // Main savepoint reached.
1373 upgrade_main_savepoint(true, 2016020201.00);
1376 if ($oldversion < 2016021500.00) {
1377 $root = $CFG->tempdir . '/download';
1378 if (is_dir($root)) {
1379 // Fetch each repository type - include all repos, not just enabled.
1380 $repositories = $DB->get_records('repository', array(), '', 'type');
1382 foreach ($repositories as $id => $repository) {
1383 $directory = $root . '/repository_' . $repository->type;
1384 if (is_dir($directory)) {
1385 fulldelete($directory);
1390 // Main savepoint reached.
1391 upgrade_main_savepoint(true, 2016021500.00);
1394 if ($oldversion < 2016021501.00) {
1395 // This could take a long time. Unfortunately, no way to know how long, and no way to do progress, so setting for 1 hour.
1396 upgrade_set_timeout(3600);
1398 // Define index userid-itemid (not unique) to be added to grade_grades_history.
1399 $table = new xmldb_table('grade_grades_history');
1400 $index = new xmldb_index('userid-itemid-timemodified', XMLDB_INDEX_NOTUNIQUE, array('userid', 'itemid', 'timemodified'));
1402 // Conditionally launch add index userid-itemid.
1403 if (!$dbman->index_exists($table, $index)) {
1404 $dbman->add_index($table, $index);
1407 // Main savepoint reached.
1408 upgrade_main_savepoint(true, 2016021501.00);
1411 if ($oldversion < 2016030103.00) {
1413 // MDL-50887. Implement plugins infrastructure for antivirus and create ClamAV plugin.
1414 // This routine moves core ClamAV configuration to plugin level.
1416 // If clamav was configured and enabled, enable the plugin.
1417 if (!empty($CFG->runclamonupload) && !empty($CFG->pathtoclam)) {
1418 set_config('antiviruses', 'clamav');
1420 set_config('antiviruses', '');
1423 if (isset($CFG->runclamonupload)) {
1424 // Just unset global configuration, we have already enabled the plugin
1425 // which implies that ClamAV will be used for scanning uploaded files.
1426 unset_config('runclamonupload');
1428 // Move core ClamAV configuration settings to plugin.
1429 if (isset($CFG->pathtoclam)) {
1430 set_config('pathtoclam', $CFG->pathtoclam, 'antivirus_clamav');
1431 unset_config('pathtoclam');
1433 if (isset($CFG->quarantinedir)) {
1434 set_config('quarantinedir', $CFG->quarantinedir, 'antivirus_clamav');
1435 unset_config('quarantinedir');
1437 if (isset($CFG->clamfailureonupload)) {
1438 set_config('clamfailureonupload', $CFG->clamfailureonupload, 'antivirus_clamav');
1439 unset_config('clamfailureonupload');
1442 // Main savepoint reached.
1443 upgrade_main_savepoint(true, 2016030103.00);
1446 if ($oldversion < 2016030400.01) {
1447 // Add the new services field.
1448 $table = new xmldb_table('external_functions');
1449 $field = new xmldb_field('services', XMLDB_TYPE_CHAR, '1333', null, null, null, null, 'capabilities');
1451 // Conditionally launch add field services.
1452 if (!$dbman->field_exists($table, $field)) {
1453 $dbman->add_field($table, $field);
1455 // Main savepoint reached.
1456 upgrade_main_savepoint(true, 2016030400.01);
1459 if ($oldversion < 2016041500.50) {
1461 // Define table competency to be created.
1462 $table = new xmldb_table('competency');
1464 // Adding fields to table competency.
1465 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1466 $table->add_field('shortname', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1467 $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
1468 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
1469 $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1470 $table->add_field('competencyframeworkid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1471 $table->add_field('parentid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1472 $table->add_field('path', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1473 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1474 $table->add_field('ruletype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1475 $table->add_field('ruleoutcome', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
1476 $table->add_field('ruleconfig', XMLDB_TYPE_TEXT, null, null, null, null, null);
1477 $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1478 $table->add_field('scaleconfiguration', XMLDB_TYPE_TEXT, null, null, null, null, null);
1479 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1480 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1481 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1483 // Adding keys to table competency.
1484 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1486 // Adding indexes to table competency.
1487 $table->add_index('idnumberframework', XMLDB_INDEX_UNIQUE, array('competencyframeworkid', 'idnumber'));
1488 $table->add_index('ruleoutcome', XMLDB_INDEX_NOTUNIQUE, array('ruleoutcome'));
1490 // Conditionally launch create table for competency.
1491 if (!$dbman->table_exists($table)) {
1492 $dbman->create_table($table);
1495 // Main savepoint reached.
1496 upgrade_main_savepoint(true, 2016041500.50);
1499 if ($oldversion < 2016041500.51) {
1501 // Define table competency_coursecompsetting to be created.
1502 $table = new xmldb_table('competency_coursecompsetting');
1504 // Adding fields to table competency_coursecompsetting.
1505 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1506 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1507 $table->add_field('pushratingstouserplans', XMLDB_TYPE_INTEGER, '2', null, null, null, null);
1508 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1509 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1510 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1512 // Adding keys to table competency_coursecompsetting.
1513 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1514 $table->add_key('courseidlink', XMLDB_KEY_FOREIGN_UNIQUE, array('courseid'), 'course', array('id'));
1516 // Conditionally launch create table for competency_coursecompsetting.
1517 if (!$dbman->table_exists($table)) {
1518 $dbman->create_table($table);
1521 // Main savepoint reached.
1522 upgrade_main_savepoint(true, 2016041500.51);
1525 if ($oldversion < 2016041500.52) {
1527 // Define table competency_framework to be created.
1528 $table = new xmldb_table('competency_framework');
1530 // Adding fields to table competency_framework.
1531 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1532 $table->add_field('shortname', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1533 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1534 $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1535 $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
1536 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
1537 $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '11', null, null, null, null);
1538 $table->add_field('scaleconfiguration', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1539 $table->add_field('visible', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
1540 $table->add_field('taxonomies', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1541 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1542 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1543 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1545 // Adding keys to table competency_framework.
1546 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1548 // Adding indexes to table competency_framework.
1549 $table->add_index('idnumber', XMLDB_INDEX_UNIQUE, array('idnumber'));
1551 // Conditionally launch create table for competency_framework.
1552 if (!$dbman->table_exists($table)) {
1553 $dbman->create_table($table);
1556 // Main savepoint reached.
1557 upgrade_main_savepoint(true, 2016041500.52);
1560 if ($oldversion < 2016041500.53) {
1562 // Define table competency_coursecomp to be created.
1563 $table = new xmldb_table('competency_coursecomp');
1565 // Adding fields to table competency_coursecomp.
1566 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1567 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1568 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1569 $table->add_field('ruleoutcome', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, null);
1570 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1571 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1572 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1573 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1575 // Adding keys to table competency_coursecomp.
1576 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1577 $table->add_key('courseidlink', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1578 $table->add_key('competencyid', XMLDB_KEY_FOREIGN, array('competencyid'), 'competency_competency', array('id'));
1580 // Adding indexes to table competency_coursecomp.
1581 $table->add_index('courseidruleoutcome', XMLDB_INDEX_NOTUNIQUE, array('courseid', 'ruleoutcome'));
1582 $table->add_index('courseidcompetencyid', XMLDB_INDEX_UNIQUE, array('courseid', 'competencyid'));
1584 // Conditionally launch create table for competency_coursecomp.
1585 if (!$dbman->table_exists($table)) {
1586 $dbman->create_table($table);
1589 // Main savepoint reached.
1590 upgrade_main_savepoint(true, 2016041500.53);
1593 if ($oldversion < 2016041500.54) {
1595 // Define table competency_plan to be created.
1596 $table = new xmldb_table('competency_plan');
1598 // Adding fields to table competency_plan.
1599 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1600 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1601 $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
1602 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
1603 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1604 $table->add_field('templateid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1605 $table->add_field('origtemplateid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1606 $table->add_field('status', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null);
1607 $table->add_field('duedate', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
1608 $table->add_field('reviewerid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1609 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1610 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1611 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1613 // Adding keys to table competency_plan.
1614 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1616 // Adding indexes to table competency_plan.
1617 $table->add_index('useridstatus', XMLDB_INDEX_NOTUNIQUE, array('userid', 'status'));
1618 $table->add_index('templateid', XMLDB_INDEX_NOTUNIQUE, array('templateid'));
1619 $table->add_index('statusduedate', XMLDB_INDEX_NOTUNIQUE, array('status', 'duedate'));
1621 // Conditionally launch create table for competency_plan.
1622 if (!$dbman->table_exists($table)) {
1623 $dbman->create_table($table);
1626 // Main savepoint reached.
1627 upgrade_main_savepoint(true, 2016041500.54);
1630 if ($oldversion < 2016041500.55) {
1632 // Define table competency_template to be created.
1633 $table = new xmldb_table('competency_template');
1635 // Adding fields to table competency_template.
1636 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1637 $table->add_field('shortname', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1638 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1639 $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
1640 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
1641 $table->add_field('visible', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
1642 $table->add_field('duedate', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1643 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1644 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1645 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1647 // Adding keys to table competency_template.
1648 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1650 // Conditionally launch create table for competency_template.
1651 if (!$dbman->table_exists($table)) {
1652 $dbman->create_table($table);
1655 // Main savepoint reached.
1656 upgrade_main_savepoint(true, 2016041500.55);
1659 if ($oldversion < 2016041500.56) {
1661 // Define table competency_templatecomp to be created.
1662 $table = new xmldb_table('competency_templatecomp');
1664 // Adding fields to table competency_templatecomp.
1665 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1666 $table->add_field('templateid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1667 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1668 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1669 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1670 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1671 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1673 // Adding keys to table competency_templatecomp.
1674 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1675 $table->add_key('templateidlink', XMLDB_KEY_FOREIGN, array('templateid'), 'competency_template', array('id'));
1676 $table->add_key('competencyid', XMLDB_KEY_FOREIGN, array('competencyid'), 'competency_competency', array('id'));
1678 // Conditionally launch create table for competency_templatecomp.
1679 if (!$dbman->table_exists($table)) {
1680 $dbman->create_table($table);
1683 // Main savepoint reached.
1684 upgrade_main_savepoint(true, 2016041500.56);
1687 if ($oldversion < 2016041500.57) {
1689 // Define table competency_templatecohort to be created.
1690 $table = new xmldb_table('competency_templatecohort');
1692 // Adding fields to table competency_templatecohort.
1693 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1694 $table->add_field('templateid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1695 $table->add_field('cohortid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1696 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1697 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1698 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1700 // Adding keys to table competency_templatecohort.
1701 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1703 // Adding indexes to table competency_templatecohort.
1704 $table->add_index('templateid', XMLDB_INDEX_NOTUNIQUE, array('templateid'));
1705 $table->add_index('templatecohortids', XMLDB_INDEX_UNIQUE, array('templateid', 'cohortid'));
1707 // Conditionally launch create table for competency_templatecohort.
1708 if (!$dbman->table_exists($table)) {
1709 $dbman->create_table($table);
1712 // Main savepoint reached.
1713 upgrade_main_savepoint(true, 2016041500.57);
1716 if ($oldversion < 2016041500.58) {
1718 // Define table competency_relatedcomp to be created.
1719 $table = new xmldb_table('competency_relatedcomp');
1721 // Adding fields to table competency_relatedcomp.
1722 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1723 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1724 $table->add_field('relatedcompetencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1725 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1726 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1727 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1729 // Adding keys to table competency_relatedcomp.
1730 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1732 // Conditionally launch create table for competency_relatedcomp.
1733 if (!$dbman->table_exists($table)) {
1734 $dbman->create_table($table);
1737 // Main savepoint reached.
1738 upgrade_main_savepoint(true, 2016041500.58);
1741 if ($oldversion < 2016041500.59) {
1743 // Define table competency_usercomp to be created.
1744 $table = new xmldb_table('competency_usercomp');
1746 // Adding fields to table competency_usercomp.
1747 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1748 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1749 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1750 $table->add_field('status', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
1751 $table->add_field('reviewerid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1752 $table->add_field('proficiency', XMLDB_TYPE_INTEGER, '2', null, null, null, null);
1753 $table->add_field('grade', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1754 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1755 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1756 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1758 // Adding keys to table competency_usercomp.
1759 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1761 // Adding indexes to table competency_usercomp.
1762 $table->add_index('useridcompetency', XMLDB_INDEX_UNIQUE, array('userid', 'competencyid'));
1764 // Conditionally launch create table for competency_usercomp.
1765 if (!$dbman->table_exists($table)) {
1766 $dbman->create_table($table);
1769 // Main savepoint reached.
1770 upgrade_main_savepoint(true, 2016041500.59);
1773 if ($oldversion < 2016041500.60) {
1775 // Define table competency_usercompcourse to be created.
1776 $table = new xmldb_table('competency_usercompcourse');
1778 // Adding fields to table competency_usercompcourse.
1779 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1780 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1781 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1782 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1783 $table->add_field('proficiency', XMLDB_TYPE_INTEGER, '2', null, null, null, null);
1784 $table->add_field('grade', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1785 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1786 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1787 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1789 // Adding keys to table competency_usercompcourse.
1790 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1792 // Adding indexes to table competency_usercompcourse.
1793 $table->add_index('useridcoursecomp', XMLDB_INDEX_UNIQUE, array('userid', 'courseid', 'competencyid'));
1795 // Conditionally launch create table for competency_usercompcourse.
1796 if (!$dbman->table_exists($table)) {
1797 $dbman->create_table($table);
1800 // Main savepoint reached.
1801 upgrade_main_savepoint(true, 2016041500.60);
1804 if ($oldversion < 2016041500.61) {
1806 // Define table competency_usercompplan to be created.
1807 $table = new xmldb_table('competency_usercompplan');
1809 // Adding fields to table competency_usercompplan.
1810 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1811 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1812 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1813 $table->add_field('planid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1814 $table->add_field('proficiency', XMLDB_TYPE_INTEGER, '2', null, null, null, null);
1815 $table->add_field('grade', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1816 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1817 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1818 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1819 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1821 // Adding keys to table competency_usercompplan.
1822 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1824 // Adding indexes to table competency_usercompplan.
1825 $table->add_index('usercompetencyplan', XMLDB_INDEX_UNIQUE, array('userid', 'competencyid', 'planid'));
1827 // Conditionally launch create table for competency_usercompplan.
1828 if (!$dbman->table_exists($table)) {
1829 $dbman->create_table($table);
1832 // Main savepoint reached.
1833 upgrade_main_savepoint(true, 2016041500.61);
1836 if ($oldversion < 2016041500.62) {
1838 // Define table competency_plancomp to be created.
1839 $table = new xmldb_table('competency_plancomp');
1841 // Adding fields to table competency_plancomp.
1842 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1843 $table->add_field('planid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1844 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1845 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1846 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1847 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1848 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1850 // Adding keys to table competency_plancomp.
1851 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1853 // Adding indexes to table competency_plancomp.
1854 $table->add_index('planidcompetencyid', XMLDB_INDEX_UNIQUE, array('planid', 'competencyid'));
1856 // Conditionally launch create table for competency_plancomp.
1857 if (!$dbman->table_exists($table)) {
1858 $dbman->create_table($table);
1861 // Main savepoint reached.
1862 upgrade_main_savepoint(true, 2016041500.62);
1865 if ($oldversion < 2016041500.63) {
1867 // Define table competency_evidence to be created.
1868 $table = new xmldb_table('competency_evidence');
1870 // Adding fields to table competency_evidence.
1871 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1872 $table->add_field('usercompetencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1873 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1874 $table->add_field('action', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, null);
1875 $table->add_field('actionuserid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1876 $table->add_field('descidentifier', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1877 $table->add_field('desccomponent', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1878 $table->add_field('desca', XMLDB_TYPE_TEXT, null, null, null, null, null);
1879 $table->add_field('url', XMLDB_TYPE_CHAR, '255', null, null, null, null);
1880 $table->add_field('grade', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1881 $table->add_field('note', XMLDB_TYPE_TEXT, null, null, null, null, null);
1882 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1883 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1884 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1886 // Adding keys to table competency_evidence.
1887 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1889 // Adding indexes to table competency_evidence.
1890 $table->add_index('usercompetencyid', XMLDB_INDEX_NOTUNIQUE, array('usercompetencyid'));
1892 // Conditionally launch create table for competency_evidence.
1893 if (!$dbman->table_exists($table)) {
1894 $dbman->create_table($table);
1897 // Main savepoint reached.
1898 upgrade_main_savepoint(true, 2016041500.63);
1901 if ($oldversion < 2016041500.64) {
1903 // Define table competency_userevidence to be created.
1904 $table = new xmldb_table('competency_userevidence');
1906 // Adding fields to table competency_userevidence.
1907 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1908 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1909 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1910 $table->add_field('description', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1911 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null);
1912 $table->add_field('url', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1913 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1914 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1915 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1917 // Adding keys to table competency_userevidence.
1918 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1920 // Adding indexes to table competency_userevidence.
1921 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
1923 // Conditionally launch create table for competency_userevidence.
1924 if (!$dbman->table_exists($table)) {
1925 $dbman->create_table($table);
1928 // Main savepoint reached.
1929 upgrade_main_savepoint(true, 2016041500.64);
1932 if ($oldversion < 2016041500.65) {
1934 // Define table competency_userevidencecomp to be created.
1935 $table = new xmldb_table('competency_userevidencecomp');
1937 // Adding fields to table competency_userevidencecomp.
1938 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1939 $table->add_field('userevidenceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1940 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1941 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1942 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1943 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1945 // Adding keys to table competency_userevidencecomp.
1946 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1948 // Adding indexes to table competency_userevidencecomp.
1949 $table->add_index('userevidenceid', XMLDB_INDEX_NOTUNIQUE, array('userevidenceid'));
1950 $table->add_index('userevidencecompids', XMLDB_INDEX_UNIQUE, array('userevidenceid', 'competencyid'));
1952 // Conditionally launch create table for competency_userevidencecomp.
1953 if (!$dbman->table_exists($table)) {
1954 $dbman->create_table($table);
1957 // Main savepoint reached.
1958 upgrade_main_savepoint(true, 2016041500.65);
1961 if ($oldversion < 2016041500.66) {
1963 // Define table competency_modulecomp to be created.
1964 $table = new xmldb_table('competency_modulecomp');
1966 // Adding fields to table competency_modulecomp.
1967 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1968 $table->add_field('cmid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1969 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1970 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1971 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1972 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1973 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1974 $table->add_field('ruleoutcome', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, null);
1976 // Adding keys to table competency_modulecomp.
1977 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1978 $table->add_key('cmidkey', XMLDB_KEY_FOREIGN, array('cmid'), 'course_modules', array('id'));
1979 $table->add_key('competencyidkey', XMLDB_KEY_FOREIGN, array('competencyid'), 'competency_competency', array('id'));
1981 // Adding indexes to table competency_modulecomp.
1982 $table->add_index('cmidruleoutcome', XMLDB_INDEX_NOTUNIQUE, array('cmid', 'ruleoutcome'));
1983 $table->add_index('cmidcompetencyid', XMLDB_INDEX_UNIQUE, array('cmid', 'competencyid'));
1985 // Conditionally launch create table for competency_modulecomp.
1986 if (!$dbman->table_exists($table)) {
1987 $dbman->create_table($table);
1990 // Main savepoint reached.
1991 upgrade_main_savepoint(true, 2016041500.66);
1994 if ($oldversion < 2016042100.00) {
1995 // Update all countries to upper case.
1996 $DB->execute("UPDATE {user} SET country = UPPER(country)");
1997 // Main savepoint reached.
1998 upgrade_main_savepoint(true, 2016042100.00);
2001 if ($oldversion < 2016042600.01) {
2002 $deprecatedwebservices = [
2003 'moodle_course_create_courses',
2004 'moodle_course_get_courses',
2005 'moodle_enrol_get_enrolled_users',
2006 'moodle_enrol_get_users_courses',
2007 'moodle_enrol_manual_enrol_users',
2008 'moodle_file_get_files',
2009 'moodle_file_upload',
2010 'moodle_group_add_groupmembers',
2011 'moodle_group_create_groups',
2012 'moodle_group_delete_groupmembers',
2013 'moodle_group_delete_groups',
2014 'moodle_group_get_course_groups',
2015 'moodle_group_get_groupmembers',
2016 'moodle_group_get_groups',
2017 'moodle_message_send_instantmessages',
2018 'moodle_notes_create_notes',
2019 'moodle_role_assign',
2020 'moodle_role_unassign',
2021 'moodle_user_create_users',
2022 'moodle_user_delete_users',
2023 'moodle_user_get_course_participants_by_id',
2024 'moodle_user_get_users_by_courseid',
2025 'moodle_user_get_users_by_id',
2026 'moodle_user_update_users',
2027 'core_grade_get_definitions',
2028 'core_user_get_users_by_id',
2029 'moodle_webservice_get_siteinfo',
2030 'mod_forum_get_forum_discussions'
2033 list($insql, $params) = $DB->get_in_or_equal($deprecatedwebservices);
2034 $DB->delete_records_select('external_functions', "name $insql", $params);
2035 $DB->delete_records_select('external_services_functions', "functionname $insql", $params);
2036 // Main savepoint reached.
2037 upgrade_main_savepoint(true, 2016042600.01);
2040 if ($oldversion < 2016051300.00) {
2041 // Add a default competency rating scale.
2042 make_competence_scale();
2044 // Savepoint reached.
2045 upgrade_main_savepoint(true, 2016051300.00);
2048 if ($oldversion < 2016051700.01) {
2049 // This script is included in each major version upgrade process (3.0, 3.1) so make sure we don't run it twice.
2050 if (empty($CFG->upgrade_letterboundarycourses)) {
2051 // MDL-45390. If a grade is being displayed with letters and the grade boundaries are not being adhered to properly
2052 // then this course will also be frozen.
2053 // If the changes are accepted then the display of some grades may change.
2054 // This is here to freeze the gradebook in affected courses.
2055 upgrade_course_letter_boundary();
2057 // To skip running the same script on the upgrade to the next major version release.
2058 set_config('upgrade_letterboundarycourses', 1);
2060 // Main savepoint reached.
2061 upgrade_main_savepoint(true, 2016051700.01);
2064 // Moodle v3.1.0 release upgrade line.
2065 // Put any upgrade step following this.
2067 if ($oldversion < 2016081700.00) {
2069 // If someone is emotionally attached to it let's leave the config (basically the version) there.
2070 if (!file_exists($CFG->dirroot . '/report/search/classes/output/form.php')) {
2071 unset_all_config_for_plugin('report_search');
2074 // Savepoint reached.
2075 upgrade_main_savepoint(true, 2016081700.00);
2078 if ($oldversion < 2016081700.02) {
2079 // Default schedule values.
2083 // Get the old settings.
2084 if (isset($CFG->statsruntimestarthour)) {
2085 $hour = $CFG->statsruntimestarthour;
2087 if (isset($CFG->statsruntimestartminute)) {
2088 $minute = $CFG->statsruntimestartminute;
2091 // Retrieve the scheduled task record first.
2092 $stattask = $DB->get_record('task_scheduled', array('component' => 'moodle', 'classname' => '\core\task\stats_cron_task'));
2094 // Don't touch customised scheduling.
2095 if ($stattask && !$stattask->customised) {
2097 $nextruntime = mktime($hour, $minute, 0, date('m'), date('d'), date('Y'));
2098 if ($nextruntime < $stattask->lastruntime) {
2099 // Add 24 hours to the next run time.
2100 $newtime = new DateTime();
2101 $newtime->setTimestamp($nextruntime);
2102 $newtime->add(new DateInterval('P1D'));
2103 $nextruntime = $newtime->getTimestamp();
2105 $stattask->nextruntime = $nextruntime;
2106 $stattask->minute = $minute;
2107 $stattask->hour = $hour;
2108 $stattask->customised = 1;
2109 $DB->update_record('task_scheduled', $stattask);
2111 // These settings are no longer used.
2112 unset_config('statsruntimestarthour');
2113 unset_config('statsruntimestartminute');
2114 unset_config('statslastexecution');
2116 upgrade_main_savepoint(true, 2016081700.02);
2119 if ($oldversion < 2016082200.00) {
2120 // An upgrade step to remove any duplicate stamps, within the same context, in the question_categories table, and to
2121 // add a unique index to (contextid, stamp) to avoid future stamp duplication. See MDL-54864.
2123 // Extend the execution time limit of the script to 2 hours.
2124 upgrade_set_timeout(7200);
2126 // This SQL fetches the id of those records which have duplicate stamps within the same context.
2127 // This doesn't return the original record within the context, from which the duplicate stamps were likely created.
2128 $fromclause = "FROM (
2129 SELECT min(id) AS minid, contextid, stamp
2130 FROM {question_categories}
2131 GROUP BY contextid, stamp
2133 JOIN {question_categories} qc
2134 ON qc.contextid = minid.contextid AND qc.stamp = minid.stamp AND qc.id > minid.minid";
2136 // Get the total record count - used for the progress bar.
2137 $countduplicatessql = "SELECT count(qc.id) $fromclause";
2138 $total = $DB->count_records_sql($countduplicatessql);
2140 // Get the records themselves.
2141 $getduplicatessql = "SELECT qc.id $fromclause ORDER BY minid";
2142 $rs = $DB->get_recordset_sql($getduplicatessql);
2144 // For each duplicate, update the stamp to a new random value.
2146 $pbar = new progress_bar('updatequestioncategorystamp', 500, true);
2147 foreach ($rs as $record) {
2148 // Generate a new, unique stamp and update the record.
2150 $newstamp = make_unique_id_code();
2151 } while (isset($usedstamps[$newstamp]));
2152 $usedstamps[$newstamp] = 1;
2153 $DB->set_field('question_categories', 'stamp', $newstamp, array('id' => $record->id));
2157 $pbar->update($i, $total, "Updating duplicate question category stamp - $i/$total.");
2161 // The uniqueness of each (contextid, stamp) pair is now guaranteed, so add the unique index to stop future duplicates.
2162 $table = new xmldb_table('question_categories');
2163 $index = new xmldb_index('contextidstamp', XMLDB_INDEX_UNIQUE, array('contextid', 'stamp'));
2164 if (!$dbman->index_exists($table, $index)) {
2165 $dbman->add_index($table, $index);
2168 // Savepoint reached.
2169 upgrade_main_savepoint(true, 2016082200.00);
2172 if ($oldversion < 2016091900.00) {
2174 // Removing the themes from core.
2175 $themes = array('base', 'canvas');
2177 foreach ($themes as $key => $theme) {
2178 if (check_dir_exists($CFG->dirroot . '/theme/' . $theme, false)) {
2179 // Ignore the themes that have been re-downloaded.
2180 unset($themes[$key]);
2184 if (!empty($themes)) {
2185 // Hacky emulation of plugin uninstallation.
2186 foreach ($themes as $theme) {
2187 unset_all_config_for_plugin('theme_' . $theme);
2191 // Main savepoint reached.
2192 upgrade_main_savepoint(true, 2016091900.00);
2195 if ($oldversion < 2016091900.02) {
2197 // Define index attemptstepid-name (unique) to be dropped from question_attempt_step_data.
2198 $table = new xmldb_table('question_attempt_step_data');
2199 $index = new xmldb_index('attemptstepid-name', XMLDB_INDEX_UNIQUE, array('attemptstepid', 'name'));
2201 // Conditionally launch drop index attemptstepid-name.
2202 if ($dbman->index_exists($table, $index)) {
2203 $dbman->drop_index($table, $index);
2206 // Main savepoint reached.
2207 upgrade_main_savepoint(true, 2016091900.02);
2210 if ($oldversion < 2016100300.00) {
2211 unset_config('enablecssoptimiser');
2213 upgrade_main_savepoint(true, 2016100300.00);
2216 if ($oldversion < 2016100501.00) {
2218 // Define field enddate to be added to course.
2219 $table = new xmldb_table('course');
2220 $field = new xmldb_field('enddate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'startdate');
2222 // Conditionally launch add field enddate.
2223 if (!$dbman->field_exists($table, $field)) {
2224 $dbman->add_field($table, $field);
2227 // Main savepoint reached.
2228 upgrade_main_savepoint(true, 2016100501.00);
2231 if ($oldversion < 2016101100.00) {
2232 // Define field component to be added to message.
2233 $table = new xmldb_table('message');
2234 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'timeusertodeleted');
2236 // Conditionally launch add field component.
2237 if (!$dbman->field_exists($table, $field)) {
2238 $dbman->add_field($table, $field);
2241 // Define field eventtype to be added to message.
2242 $field = new xmldb_field('eventtype', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'component');
2244 // Conditionally launch add field eventtype.
2245 if (!$dbman->field_exists($table, $field)) {
2246 $dbman->add_field($table, $field);
2249 // Main savepoint reached.
2250 upgrade_main_savepoint(true, 2016101100.00);
2254 if ($oldversion < 2016101101.00) {
2255 // Define field component to be added to message_read.
2256 $table = new xmldb_table('message_read');
2257 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'timeusertodeleted');
2259 // Conditionally launch add field component.
2260 if (!$dbman->field_exists($table, $field)) {
2261 $dbman->add_field($table, $field);
2264 // Define field eventtype to be added to message_read.
2265 $field = new xmldb_field('eventtype', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'component');
2267 // Conditionally launch add field eventtype.
2268 if (!$dbman->field_exists($table, $field)) {
2269 $dbman->add_field($table, $field);
2272 // Main savepoint reached.
2273 upgrade_main_savepoint(true, 2016101101.00);
2276 if ($oldversion < 2016101401.00) {
2277 // Clean up repository_alfresco config unless plugin has been manually installed.
2278 if (!file_exists($CFG->dirroot . '/repository/alfresco/lib.php')) {
2279 // Remove capabilities.
2280 capabilities_cleanup('repository_alfresco');
2282 unset_all_config_for_plugin('repository_alfresco');
2285 // Savepoint reached.
2286 upgrade_main_savepoint(true, 2016101401.00);
2289 if ($oldversion < 2016101401.02) {
2290 $table = new xmldb_table('external_tokens');
2291 $field = new xmldb_field('privatetoken', XMLDB_TYPE_CHAR, '64', null, null, null, null);
2293 // Conditionally add privatetoken field to the external_tokens table.
2294 if (!$dbman->field_exists($table, $field)) {
2295 $dbman->add_field($table, $field);
2298 // Main savepoint reached.
2299 upgrade_main_savepoint(true, 2016101401.02);
2302 if ($oldversion < 2016110202.00) {
2304 // Force uninstall of deleted authentication plugin.
2305 if (!file_exists("$CFG->dirroot/auth/radius")) {
2306 // Leave settings inplace if there are radius users.
2307 if (!$DB->record_exists('user', array('auth' => 'radius', 'deleted' => 0))) {
2308 // Remove all other associated config.
2309 unset_all_config_for_plugin('auth/radius');
2310 // The version number for radius is in this format.
2311 unset_all_config_for_plugin('auth_radius');
2314 upgrade_main_savepoint(true, 2016110202.00);
2317 if ($oldversion < 2016110300.00) {
2318 // Remove unused admin email setting.
2319 unset_config('emailonlyfromreplyaddress');
2321 // Main savepoint reached.
2322 upgrade_main_savepoint(true, 2016110300.00);
2325 if ($oldversion < 2016110500.00) {
2330 'html5video' => ['.mov', '.mp4', '.m4v', '.mpeg', '.mpe', '.mpg', '.ogv', '.webm'],
2331 'flv' => ['.flv', '.f4v'],
2332 'html5audio' => ['.aac', '.flac', '.mp3', '.m4a', '.oga', '.ogg', '.wav'],
2337 // Convert hardcoded media players to the settings of the new media player plugin type.
2338 if (get_config('core', 'media_plugins_sortorder') === false) {
2339 $enabledplugins = [];
2340 $videoextensions = [];
2341 $audioextensions = [];
2342 foreach ($oldplayers as $oldplayer => $extensions) {
2343 $settingname = 'core_media_enable_'.$oldplayer;
2344 if (!empty($CFG->$settingname)) {
2346 // VideoJS will be used for all media files players that were used previously.
2347 $enabledplugins['videojs'] = 'videojs';
2348 if ($oldplayer === 'mp3' || $oldplayer === 'html5audio') {
2349 $audioextensions += array_combine($extensions, $extensions);
2351 $videoextensions += array_combine($extensions, $extensions);
2354 // Enable youtube, vimeo and swf.
2355 $enabledplugins[$oldplayer] = $oldplayer;
2360 set_config('media_plugins_sortorder', join(',', $enabledplugins));
2362 // Configure VideoJS to match the existing players set up.
2363 if ($enabledplugins['videojs']) {
2364 $enabledplugins[] = 'videojs';
2365 set_config('audioextensions', join(',', $audioextensions), 'media_videojs');
2366 set_config('videoextensions', join(',', $videoextensions), 'media_videojs');
2367 $useflash = !empty($CFG->core_media_enable_flv) || !empty($CFG->core_media_enable_mp3);
2368 set_config('useflash', $useflash, 'media_videojs');
2369 if (empty($CFG->core_media_enable_youtube)) {
2370 // Normally YouTube is enabled in videojs, but if youtube converter was disabled before upgrade
2371 // disable it in videojs as well.
2372 set_config('youtube', false, 'media_videojs');
2377 // Unset old settings.
2378 foreach ($oldplayers as $oldplayer => $extensions) {
2379 unset_config('core_media_enable_' . $oldplayer);
2382 // Preset defaults if CORE_MEDIA_VIDEO_WIDTH and CORE_MEDIA_VIDEO_HEIGHT are specified in config.php .
2383 // After this upgrade step these constants will not be used any more.
2384 if (defined('CORE_MEDIA_VIDEO_WIDTH')) {
2385 set_config('media_default_width', CORE_MEDIA_VIDEO_WIDTH);
2387 if (defined('CORE_MEDIA_VIDEO_HEIGHT')) {
2388 set_config('media_default_height', CORE_MEDIA_VIDEO_HEIGHT);
2391 // Savepoint reached.
2392 upgrade_main_savepoint(true, 2016110500.00);
2395 if ($oldversion < 2016110600.00) {
2396 // Define a field 'deletioninprogress' in the 'course_modules' table, to background deletion tasks.
2397 $table = new xmldb_table('course_modules');
2398 $field = new xmldb_field('deletioninprogress', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'availability');
2400 // Conditionally launch add field 'deletioninprogress'.
2401 if (!$dbman->field_exists($table, $field)) {
2402 $dbman->add_field($table, $field);
2405 // Main savepoint reached.
2406 upgrade_main_savepoint(true, 2016110600.00);
2409 if ($oldversion < 2016112200.01) {
2411 // Define field requiredbytheme to be added to block_instances.
2412 $table = new xmldb_table('block_instances');
2413 $field = new xmldb_field('requiredbytheme', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'showinsubcontexts');
2415 // Conditionally launch add field requiredbytheme.
2416 if (!$dbman->field_exists($table, $field)) {
2417 $dbman->add_field($table, $field);
2420 // Main savepoint reached.
2421 upgrade_main_savepoint(true, 2016112200.01);
2423 if ($oldversion < 2016112200.02) {
2425 // Change the existing site level admin and settings blocks to be requiredbytheme which means they won't show in boost.
2426 $context = context_system::instance();
2427 $params = array('blockname' => 'settings', 'parentcontextid' => $context->id);
2428 $DB->set_field('block_instances', 'requiredbytheme', 1, $params);
2430 $params = array('blockname' => 'navigation', 'parentcontextid' => $context->id);
2431 $DB->set_field('block_instances', 'requiredbytheme', 1, $params);
2432 // Main savepoint reached.
2433 upgrade_main_savepoint(true, 2016112200.02);
2436 // Automatically generated Moodle v3.2.0 release upgrade line.
2437 // Put any upgrade step following this.
2439 if ($oldversion < 2016122800.00) {
2440 // Find all roles with the coursecreator archetype.
2441 $coursecreatorroleids = $DB->get_records('role', array('archetype' => 'coursecreator'), '', 'id');
2443 $context = context_system::instance();
2444 $capability = 'moodle/site:configview';
2446 foreach ($coursecreatorroleids as $roleid => $notused) {
2448 // Check that the capability has not already been assigned. If it has then it's either already set
2449 // to allow or specifically set to prohibit or prevent.
2450 if (!$DB->record_exists('role_capabilities', array('roleid' => $roleid, 'capability' => $capability))) {
2451 // Assign the capability.
2452 $cap = new stdClass();
2453 $cap->contextid = $context->id;
2454 $cap->roleid = $roleid;
2455 $cap->capability = $capability;
2456 $cap->permission = CAP_ALLOW;
2457 $cap->timemodified = time();
2458 $cap->modifierid = 0;
2460 $DB->insert_record('role_capabilities', $cap);
2464 // Main savepoint reached.
2465 upgrade_main_savepoint(true, 2016122800.00);
2468 if ($oldversion < 2017020200.01) {
2470 // Define index useridfrom_timeuserfromdeleted_notification (not unique) to be added to message.
2471 $table = new xmldb_table('message');
2472 $index = new xmldb_index('useridfrom_timeuserfromdeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'timeuserfromdeleted', 'notification'));
2474 // Conditionally launch add index useridfrom_timeuserfromdeleted_notification.
2475 if (!$dbman->index_exists($table, $index)) {
2476 $dbman->add_index($table, $index);
2479 // Define index useridto_timeusertodeleted_notification (not unique) to be added to message.
2480 $index = new xmldb_index('useridto_timeusertodeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridto', 'timeusertodeleted', 'notification'));
2482 // Conditionally launch add index useridto_timeusertodeleted_notification.
2483 if (!$dbman->index_exists($table, $index)) {
2484 $dbman->add_index($table, $index);
2487 $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
2489 // Conditionally launch drop index useridto.
2490 if ($dbman->index_exists($table, $index)) {
2491 $dbman->drop_index($table, $index);
2494 // Main savepoint reached.
2495 upgrade_main_savepoint(true, 2017020200.01);
2498 if ($oldversion < 2017020200.02) {
2500 // Define index useridfrom_timeuserfromdeleted_notification (not unique) to be added to message_read.
2501 $table = new xmldb_table('message_read');
2502 $index = new xmldb_index('useridfrom_timeuserfromdeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'timeuserfromdeleted', 'notification'));
2504 // Conditionally launch add index useridfrom_timeuserfromdeleted_notification.
2505 if (!$dbman->index_exists($table, $index)) {
2506 $dbman->add_index($table, $index);
2509 // Define index useridto_timeusertodeleted_notification (not unique) to be added to message_read.
2510 $index = new xmldb_index('useridto_timeusertodeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridto', 'timeusertodeleted', 'notification'));
2512 // Conditionally launch add index useridto_timeusertodeleted_notification.
2513 if (!$dbman->index_exists($table, $index)) {
2514 $dbman->add_index($table, $index);
2517 $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
2519 // Conditionally launch drop index useridto.
2520 if ($dbman->index_exists($table, $index)) {
2521 $dbman->drop_index($table, $index);
2524 // Main savepoint reached.
2525 upgrade_main_savepoint(true, 2017020200.02);
2528 if ($oldversion < 2017020901.00) {
2530 // Delete "orphaned" block positions. Note, the query does not use indexes (because there are none),
2531 // if it runs too long during upgrade you can comment this line - it will leave orphaned records
2532 // in the database but they won't bother you.
2533 upgrade_block_positions();
2535 // Main savepoint reached.
2536 upgrade_main_savepoint(true, 2017020901.00);
2539 if ($oldversion < 2017021300.00) {
2540 unset_config('loginpasswordautocomplete');
2541 upgrade_main_savepoint(true, 2017021300.00);
2544 if ($oldversion < 2017021400.00) {
2545 // Define field visibleoncoursepage to be added to course_modules.
2546 $table = new xmldb_table('course_modules');
2547 $field = new xmldb_field('visibleoncoursepage', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'visible');
2549 // Conditionally launch add field visibleoncoursepage.
2550 if (!$dbman->field_exists($table, $field)) {
2551 $dbman->add_field($table, $field);
2554 // Main savepoint reached.
2555 upgrade_main_savepoint(true, 2017021400.00);
2558 if ($oldversion < 2017030700.00) {
2560 // Define field priority to be added to event.
2561 $table = new xmldb_table('event');
2562 $field = new xmldb_field('priority', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'subscriptionid');
2564 // Conditionally launch add field priority.
2565 if (!$dbman->field_exists($table, $field)) {
2566 $dbman->add_field($table, $field);
2569 // Main savepoint reached.
2570 upgrade_main_savepoint(true, 2017030700.00);
2573 if ($oldversion < 2017031400.00) {
2575 // Define table file_conversion to be created.
2576 $table = new xmldb_table('file_conversion');
2578 // Adding fields to table file_conversion.
2579 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2580 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2581 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2582 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2583 $table->add_field('sourcefileid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2584 $table->add_field('targetformat', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2585 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
2586 $table->add_field('statusmessage', XMLDB_TYPE_TEXT, null, null, null, null, null);
2587 $table->add_field('converter', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2588 $table->add_field('destfileid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
2589 $table->add_field('data', XMLDB_TYPE_TEXT, null, null, null, null, null);
2591 // Adding keys to table file_conversion.
2592 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2593 $table->add_key('sourcefileid', XMLDB_KEY_FOREIGN, array('sourcefileid'), 'files', array('id'));
2594 $table->add_key('destfileid', XMLDB_KEY_FOREIGN, array('destfileid'), 'files', array('id'));
2596 // Conditionally launch create table for file_conversion.
2597 if (!$dbman->table_exists($table)) {
2598 $dbman->create_table($table);
2601 // Main savepoint reached.
2602 upgrade_main_savepoint(true, 2017031400.00);
2605 if ($oldversion < 2017040400.00) {
2607 // If block_course_overview is no longer present, replace with block_myoverview.
2608 if (!file_exists($CFG->dirroot . '/blocks/course_overview/block_course_overview.php')) {
2609 $DB->set_field('block_instances', 'blockname', 'myoverview', array('blockname' => 'course_overview'));
2612 upgrade_main_savepoint(true, 2017040400.00);
2615 if ($oldversion < 2017040401.00) {
2617 // If block_course_overview is no longer present, remove it.
2618 // Note - we do not need to completely remove the block context etc because we
2619 // have replaced all occurrences of block_course_overview with block_myoverview
2620 // in the upgrade step above.
2621 if (!file_exists($CFG->dirroot . '/blocks/course_overview/block_course_overview.php')) {
2622 // Delete the block from the block table.
2623 $DB->delete_records('block', array('name' => 'course_overview'));
2624 // Remove capabilities.
2625 capabilities_cleanup('block_course_overview');
2627 unset_all_config_for_plugin('block_course_overview');
2630 upgrade_main_savepoint(true, 2017040401.00);
2633 if ($oldversion < 2017040402.00) {
2635 // Define fields to be added to the 'event' table.
2636 $table = new xmldb_table('event');
2637 $fieldtype = new xmldb_field('type', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, 0, 'instance');
2638 $fieldtimesort = new xmldb_field('timesort', XMLDB_TYPE_INTEGER, '10', null, false, null, null, 'timeduration');
2640 // Conditionally launch add field.
2641 if (!$dbman->field_exists($table, $fieldtype)) {
2642 $dbman->add_field($table, $fieldtype);
2645 // Conditionally launch add field.
2646 if (!$dbman->field_exists($table, $fieldtimesort)) {
2647 $dbman->add_field($table, $fieldtimesort);
2650 // Now, define the index we will be adding.
2651 $index = new xmldb_index('type-timesort', XMLDB_INDEX_NOTUNIQUE, array('type', 'timesort'));
2653 // Conditionally launch add index.
2654 if (!$dbman->index_exists($table, $index)) {
2655 $dbman->add_index($table, $index);
2658 upgrade_main_savepoint(true, 2017040402.00);
2661 if ($oldversion < 2017040700.01) {
2663 // Define table oauth2_issuer to be created.
2664 $table = new xmldb_table('oauth2_issuer');
2666 // Adding fields to table oauth2_issuer.
2667 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2668 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2669 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2670 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2671 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2672 $table->add_field('image', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2673 $table->add_field('baseurl', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2674 $table->add_field('clientid', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2675 $table->add_field('clientsecret', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2676 $table->add_field('loginscopes', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2677 $table->add_field('loginscopesoffline', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2678 $table->add_field('loginparams', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2679 $table->add_field('loginparamsoffline', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2680 $table->add_field('alloweddomains', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2681 $table->add_field('scopessupported', XMLDB_TYPE_TEXT, null, null, null, null, null);
2682 $table->add_field('showonloginpage', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
2683 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
2684 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2686 // Adding keys to table oauth2_issuer.
2687 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2689 // Conditionally launch create table for oauth2_issuer.
2690 if (!$dbman->table_exists($table)) {
2691 $dbman->create_table($table);
2694 // Main savepoint reached.
2695 upgrade_main_savepoint(true, 2017040700.01);
2698 if ($oldversion < 2017040700.02) {
2700 // Define table oauth2_endpoint to be created.
2701 $table = new xmldb_table('oauth2_endpoint');
2703 // Adding fields to table oauth2_endpoint.
2704 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2705 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2706 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2707 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2708 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2709 $table->add_field('url', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2710 $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2712 // Adding keys to table oauth2_endpoint.
2713 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2714 $table->add_key('issuer_id_key', XMLDB_KEY_FOREIGN, array('issuerid'), 'oauth2_issuer', array('id'));
2716 // Conditionally launch create table for oauth2_endpoint.
2717 if (!$dbman->table_exists($table)) {
2718 $dbman->create_table($table);
2721 // Main savepoint reached.
2722 upgrade_main_savepoint(true, 2017040700.02);
2725 if ($oldversion < 2017040700.03) {
2727 // Define table oauth2_system_account to be created.
2728 $table = new xmldb_table('oauth2_system_account');
2730 // Adding fields to table oauth2_system_account.
2731 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2732 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2733 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2734 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2735 $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2736 $table->add_field('refreshtoken', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2737 $table->add_field('grantedscopes', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2738 $table->add_field('username', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2739 $table->add_field('email', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2741 // Adding keys to table oauth2_system_account.
2742 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2743 $table->add_key('issueridkey', XMLDB_KEY_FOREIGN_UNIQUE, array('issuerid'), 'oauth2_issuer', array('id'));
2745 // Conditionally launch create table for oauth2_system_account.
2746 if (!$dbman->table_exists($table)) {
2747 $dbman->create_table($table);
2750 // Main savepoint reached.
2751 upgrade_main_savepoint(true, 2017040700.03);
2754 if ($oldversion < 2017040700.04) {
2756 // Define table oauth2_user_field_mapping to be created.
2757 $table = new xmldb_table('oauth2_user_field_mapping');
2759 // Adding fields to table oauth2_user_field_mapping.
2760 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2761 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2762 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2763 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2764 $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2765 $table->add_field('externalfield', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
2766 $table->add_field('internalfield', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
2768 // Adding keys to table oauth2_user_field_mapping.
2769 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2770 $table->add_key('issuerkey', XMLDB_KEY_FOREIGN, array('issuerid'), 'oauth2_issuer', array('id'));
2771 $table->add_key('uniqinternal', XMLDB_KEY_UNIQUE, array('issuerid', 'internalfield'));
2773 // Conditionally launch create table for oauth2_user_field_mapping.
2774 if (!$dbman->table_exists($table)) {
2775 $dbman->create_table($table);
2778 // Main savepoint reached.
2779 upgrade_main_savepoint(true, 2017040700.04);
2782 if ($oldversion < 2017041801.00) {
2784 // Define table course_completion_defaults to be created.
2785 $table = new xmldb_table('course_completion_defaults');
2787 // Adding fields to table course_completion_defaults.
2788 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2789 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2790 $table->add_field('module', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2791 $table->add_field('completion', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
2792 $table->add_field('completionview', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
2793 $table->add_field('completionusegrade', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
2794 $table->add_field('completionexpected', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2795 $table->add_field('customrules', XMLDB_TYPE_TEXT, null, null, null, null, null);
2797 // Adding keys to table course_completion_defaults.
2798 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2799 $table->add_key('module', XMLDB_KEY_FOREIGN, array('module'), 'modules', array('id'));
2800 $table->add_key('course', XMLDB_KEY_FOREIGN, array('course'), 'course', array('id'));
2802 // Adding indexes to table course_completion_defaults.
2803 $table->add_index('coursemodule', XMLDB_INDEX_UNIQUE, array('course', 'module'));
2805 // Conditionally launch create table for course_completion_defaults.
2806 if (!$dbman->table_exists($table)) {
2807 $dbman->create_table($table);
2810 upgrade_main_savepoint(true, 2017041801.00);
2813 if ($oldversion < 2017050500.01) {
2814 // Get the list of parent event IDs.
2815 $sql = "SELECT DISTINCT repeatid
2817 WHERE repeatid <> 0";
2818 $parentids = array_keys($DB->get_records_sql($sql));
2819 // Check if there are repeating events we need to process.
2820 if (!empty($parentids)) {
2821 // The repeat IDs of parent events should match their own ID.
2822 // So we need to update parent events that have non-matching IDs and repeat IDs.
2823 list($insql, $params) = $DB->get_in_or_equal($parentids);
2824 $updatesql = "UPDATE {event}
2826 WHERE id <> repeatid
2828 $DB->execute($updatesql, $params);
2831 // Main savepoint reached.
2832 upgrade_main_savepoint(true, 2017050500.01);
2835 if ($oldversion < 2017050500.02) {
2837 // Remove all portfolio_tempdata records as these may contain serialized \file_system type objects, which are now unable to
2838 // be unserialized because of changes to the file storage API made in MDL-46375. Portfolio now stores an id reference to
2839 // files instead of the object.
2840 // These records are normally removed after a successful export, however, can be left behind if the user abandons the
2841 // export attempt (a stale record). Additionally, each stale record cannot be reused and is normally cleaned up by the cron
2842 // task core\task\portfolio_cron_task. Since the cron task tries to unserialize them, and generates a warning, we'll remove
2843 // all records here.
2844 $DB->delete_records_select('portfolio_tempdata', 'id > ?', [0]);
2846 // Main savepoint reached.
2847 upgrade_main_savepoint(true, 2017050500.02);
2850 if ($oldversion < 2017050900.01) {
2851 // Create adhoc task for upgrading of existing calendar events.
2852 $record = new \stdClass();
2853 $record->classname = '\core\task\refresh_mod_calendar_events_task';
2854 $record->component = 'core';
2856 // Next run time based from nextruntime computation in \core\task\manager::queue_adhoc_task().
2857 $nextruntime = time() - 1;
2858 $record->nextruntime = $nextruntime;
2859 $DB->insert_record('task_adhoc', $record);
2861 // Main savepoint reached.
2862 upgrade_main_savepoint(true, 2017050900.01);
2865 // Automatically generated Moodle v3.3.0 release upgrade line.
2866 // Put any upgrade step following this.
2868 if ($oldversion < 2017061201.00) {
2869 $table = new xmldb_table('course_sections');
2870 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'availability');
2872 // Define a field 'timemodified' in the 'course_sections' table.
2873 if (!$dbman->field_exists($table, $field)) {
2874 $dbman->add_field($table, $field);
2877 upgrade_main_savepoint(true, 2017061201.00);
2880 if ($oldversion < 2017061301.00) {
2881 // Check if the value of 'navcourselimit' is set to the old default value, if so, change it to the new default.
2882 if ($CFG->navcourselimit == 20) {
2883 set_config('navcourselimit', 10);
2886 // Main savepoint reached.
2887 upgrade_main_savepoint(true, 2017061301.00);