MDL-60915 core_dml: fix miscellaneous incorrect recordset usage
[moodle.git] / lib / db / upgrade.php
blob376a3010071c2d7d5c1de40a7e6ec54961b5a728
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
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/>.
17 /**
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
38 * @category upgrade
39 * @copyright 2006 onwards Martin Dougiamas http://dougiamas.com
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 defined('MOODLE_INTERNAL') || die();
45 /**
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);
65 * }
67 * All plugins within Moodle (modules, blocks, reports...) support the existence of
68 * their own upgrade.php file, using the "Frankenstyle" component name as
69 * defined at {@link http://docs.moodle.org/dev/Frankenstyle}, for example:
70 * - {@link xmldb_page_upgrade($oldversion)}. (modules don't require the plugintype ("mod_") to be used.
71 * - {@link xmldb_auth_manual_upgrade($oldversion)}.
72 * - {@link xmldb_workshopform_accumulative_upgrade($oldversion)}.
73 * - ....
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) {
88 global $CFG, $DB;
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 (v3.0.0 right now).
96 if ($oldversion < 2015111600) {
97 // Just in case somebody hacks upgrade scripts or env, we really can not continue.
98 echo("You need to upgrade to 3.0.x or higher first!\n");
99 exit(1);
100 // Note this savepoint is 100% unreachable, but needed to pass the upgrade checks.
101 upgrade_main_savepoint(true, 2015111600);
104 if ($oldversion < 2016011300.01) {
106 // This is a big upgrade script. We create new table tag_coll and the field
107 // tag.tagcollid pointing to it.
109 // Define table tag_coll to be created.
110 $table = new xmldb_table('tag_coll');
112 // Adding fields to table tagcloud.
113 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
114 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null);
115 $table->add_field('isdefault', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
116 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
117 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '5', null, XMLDB_NOTNULL, null, '0');
118 $table->add_field('searchable', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
119 $table->add_field('customurl', XMLDB_TYPE_CHAR, '255', null, null, null, null);
121 // Adding keys to table tagcloud.
122 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
124 // Conditionally launch create table for tagcloud.
125 if (!$dbman->table_exists($table)) {
126 $dbman->create_table($table);
129 // Table {tag}.
130 // Define index name (unique) to be dropped form tag - we will replace it with index on (tagcollid,name) later.
131 $table = new xmldb_table('tag');
132 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
134 // Conditionally launch drop index name.
135 if ($dbman->index_exists($table, $index)) {
136 $dbman->drop_index($table, $index);
139 // Define field tagcollid to be added to tag, we create it as null first and will change to notnull later.
140 $table = new xmldb_table('tag');
141 $field = new xmldb_field('tagcollid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'userid');
143 // Conditionally launch add field tagcloudid.
144 if (!$dbman->field_exists($table, $field)) {
145 $dbman->add_field($table, $field);
148 // Main savepoint reached.
149 upgrade_main_savepoint(true, 2016011300.01);
152 if ($oldversion < 2016011300.02) {
153 // Create a default tag collection if not exists and update the field tag.tagcollid to point to it.
154 if (!$tcid = $DB->get_field_sql('SELECT id FROM {tag_coll} ORDER BY isdefault DESC, sortorder, id', null,
155 IGNORE_MULTIPLE)) {
156 $tcid = $DB->insert_record('tag_coll', array('isdefault' => 1, 'sortorder' => 0));
158 $DB->execute('UPDATE {tag} SET tagcollid = ? WHERE tagcollid IS NULL', array($tcid));
160 // Define index tagcollname (unique) to be added to tag.
161 $table = new xmldb_table('tag');
162 $index = new xmldb_index('tagcollname', XMLDB_INDEX_UNIQUE, array('tagcollid', 'name'));
163 $field = new xmldb_field('tagcollid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'userid');
165 // Conditionally launch add index tagcollname.
166 if (!$dbman->index_exists($table, $index)) {
167 // Launch change of nullability for field tagcollid.
168 $dbman->change_field_notnull($table, $field);
169 $dbman->add_index($table, $index);
172 // Define key tagcollid (foreign) to be added to tag.
173 $table = new xmldb_table('tag');
174 $key = new xmldb_key('tagcollid', XMLDB_KEY_FOREIGN, array('tagcollid'), 'tag_coll', array('id'));
176 // Launch add key tagcloudid.
177 $dbman->add_key($table, $key);
179 // Main savepoint reached.
180 upgrade_main_savepoint(true, 2016011300.02);
183 if ($oldversion < 2016011300.03) {
185 // Define table tag_area to be created.
186 $table = new xmldb_table('tag_area');
188 // Adding fields to table tag_area.
189 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
190 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
191 $table->add_field('itemtype', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
192 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
193 $table->add_field('tagcollid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
194 $table->add_field('callback', XMLDB_TYPE_CHAR, '100', null, null, null, null);
195 $table->add_field('callbackfile', XMLDB_TYPE_CHAR, '100', null, null, null, null);
197 // Adding keys to table tag_area.
198 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
199 $table->add_key('tagcollid', XMLDB_KEY_FOREIGN, array('tagcollid'), 'tag_coll', array('id'));
201 // Adding indexes to table tag_area.
202 $table->add_index('compitemtype', XMLDB_INDEX_UNIQUE, array('component', 'itemtype'));
204 // Conditionally launch create table for tag_area.
205 if (!$dbman->table_exists($table)) {
206 $dbman->create_table($table);
209 // Main savepoint reached.
210 upgrade_main_savepoint(true, 2016011300.03);
213 if ($oldversion < 2016011300.04) {
215 // Define index itemtype-itemid-tagid-tiuserid (unique) to be dropped form tag_instance.
216 $table = new xmldb_table('tag_instance');
217 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid', XMLDB_INDEX_UNIQUE,
218 array('itemtype', 'itemid', 'tagid', 'tiuserid'));
220 // Conditionally launch drop index itemtype-itemid-tagid-tiuserid.
221 if ($dbman->index_exists($table, $index)) {
222 $dbman->drop_index($table, $index);
225 // Main savepoint reached.
226 upgrade_main_savepoint(true, 2016011300.04);
229 if ($oldversion < 2016011300.05) {
231 $DB->execute("UPDATE {tag_instance} SET component = ? WHERE component IS NULL", array(''));
233 // Changing nullability of field component on table tag_instance to not null.
234 $table = new xmldb_table('tag_instance');
235 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'tagid');
237 // Launch change of nullability for field component.
238 $dbman->change_field_notnull($table, $field);
240 // Changing type of field itemtype on table tag_instance to char.
241 $table = new xmldb_table('tag_instance');
242 $field = new xmldb_field('itemtype', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'component');
244 // Launch change of type for field itemtype.
245 $dbman->change_field_type($table, $field);
247 // Main savepoint reached.
248 upgrade_main_savepoint(true, 2016011300.05);
251 if ($oldversion < 2016011300.06) {
253 // Define index taggeditem (unique) to be added to tag_instance.
254 $table = new xmldb_table('tag_instance');
255 $index = new xmldb_index('taggeditem', XMLDB_INDEX_UNIQUE, array('component', 'itemtype', 'itemid', 'tiuserid', 'tagid'));
257 // Conditionally launch add index taggeditem.
258 if (!$dbman->index_exists($table, $index)) {
259 $dbman->add_index($table, $index);
262 // Main savepoint reached.
263 upgrade_main_savepoint(true, 2016011300.06);
266 if ($oldversion < 2016011300.07) {
268 // Define index taglookup (not unique) to be added to tag_instance.
269 $table = new xmldb_table('tag_instance');
270 $index = new xmldb_index('taglookup', XMLDB_INDEX_NOTUNIQUE, array('itemtype', 'component', 'tagid', 'contextid'));
272 // Conditionally launch add index taglookup.
273 if (!$dbman->index_exists($table, $index)) {
274 $dbman->add_index($table, $index);
277 // Main savepoint reached.
278 upgrade_main_savepoint(true, 2016011300.07);
281 if ($oldversion < 2016011301.00) {
283 // Force uninstall of deleted tool.
284 if (!file_exists("$CFG->dirroot/webservice/amf")) {
285 // Remove capabilities.
286 capabilities_cleanup('webservice_amf');
287 // Remove all other associated config.
288 unset_all_config_for_plugin('webservice_amf');
290 upgrade_main_savepoint(true, 2016011301.00);
293 if ($oldversion < 2016011901.00) {
295 // Convert calendar_lookahead to nearest new value.
296 $transaction = $DB->start_delegated_transaction();
298 // Count all users who curretly have that preference set (for progress bar).
299 $total = $DB->count_records_select('user_preferences', "name = 'calendar_lookahead' AND value != '0'");
300 $pbar = new progress_bar('upgradecalendarlookahead', 500, true);
302 // Get all these users, one at a time.
303 $rs = $DB->get_recordset_select('user_preferences', "name = 'calendar_lookahead' AND value != '0'");
304 $i = 0;
305 foreach ($rs as $userpref) {
307 // Calculate and set new lookahead value.
308 if ($userpref->value > 90) {
309 $newvalue = 120;
310 } else if ($userpref->value > 60 and $userpref->value < 90) {
311 $newvalue = 90;
312 } else if ($userpref->value > 30 and $userpref->value < 60) {
313 $newvalue = 60;
314 } else if ($userpref->value > 21 and $userpref->value < 30) {
315 $newvalue = 30;
316 } else if ($userpref->value > 14 and $userpref->value < 21) {
317 $newvalue = 21;
318 } else if ($userpref->value > 7 and $userpref->value < 14) {
319 $newvalue = 14;
320 } else {
321 $newvalue = $userpref->value;
324 $DB->set_field('user_preferences', 'value', $newvalue, array('id' => $userpref->id));
326 // Update progress.
327 $i++;
328 $pbar->update($i, $total, "Upgrading user preference settings - $i/$total.");
330 $rs->close();
331 $transaction->allow_commit();
333 upgrade_main_savepoint(true, 2016011901.00);
336 if ($oldversion < 2016020200.00) {
338 // Define field isstandard to be added to tag.
339 $table = new xmldb_table('tag');
340 $field = new xmldb_field('isstandard', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'rawname');
342 // Conditionally launch add field isstandard.
343 if (!$dbman->field_exists($table, $field)) {
344 $dbman->add_field($table, $field);
347 // Define index tagcolltype (not unique) to be dropped form tag.
348 // 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.
349 $index = new xmldb_index('tagcolltype', XMLDB_INDEX_NOTUNIQUE, array('tagcollid', 'tagtype'));
351 // Conditionally launch drop index tagcolltype.
352 if ($dbman->index_exists($table, $index)) {
353 $dbman->drop_index($table, $index);
356 // Define index tagcolltype (not unique) to be added to tag.
357 $index = new xmldb_index('tagcolltype', XMLDB_INDEX_NOTUNIQUE, array('tagcollid', 'isstandard'));
359 // Conditionally launch add index tagcolltype.
360 if (!$dbman->index_exists($table, $index)) {
361 $dbman->add_index($table, $index);
364 // Define field tagtype to be dropped from tag.
365 $field = new xmldb_field('tagtype');
367 // Conditionally launch drop field tagtype and update isstandard.
368 if ($dbman->field_exists($table, $field)) {
369 $DB->execute("UPDATE {tag} SET isstandard=(CASE WHEN (tagtype = ?) THEN 1 ELSE 0 END)", array('official'));
370 $dbman->drop_field($table, $field);
373 // Main savepoint reached.
374 upgrade_main_savepoint(true, 2016020200.00);
377 if ($oldversion < 2016020201.00) {
379 // Define field showstandard to be added to tag_area.
380 $table = new xmldb_table('tag_area');
381 $field = new xmldb_field('showstandard', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'callbackfile');
383 // Conditionally launch add field showstandard.
384 if (!$dbman->field_exists($table, $field)) {
385 $dbman->add_field($table, $field);
388 // By default set user area to hide standard tags. 2 = core_tag_tag::HIDE_STANDARD (can not use constant here).
389 $DB->execute("UPDATE {tag_area} SET showstandard = ? WHERE itemtype = ? AND component = ?",
390 array(2, 'user', 'core'));
392 // Changing precision of field enabled on table tag_area to (1).
393 $table = new xmldb_table('tag_area');
394 $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'itemtype');
396 // Launch change of precision for field enabled.
397 $dbman->change_field_precision($table, $field);
399 // Main savepoint reached.
400 upgrade_main_savepoint(true, 2016020201.00);
403 if ($oldversion < 2016021500.00) {
404 $root = $CFG->tempdir . '/download';
405 if (is_dir($root)) {
406 // Fetch each repository type - include all repos, not just enabled.
407 $repositories = $DB->get_records('repository', array(), '', 'type');
409 foreach ($repositories as $id => $repository) {
410 $directory = $root . '/repository_' . $repository->type;
411 if (is_dir($directory)) {
412 fulldelete($directory);
417 // Main savepoint reached.
418 upgrade_main_savepoint(true, 2016021500.00);
421 if ($oldversion < 2016021501.00) {
422 // This could take a long time. Unfortunately, no way to know how long, and no way to do progress, so setting for 1 hour.
423 upgrade_set_timeout(3600);
425 // Define index userid-itemid (not unique) to be added to grade_grades_history.
426 $table = new xmldb_table('grade_grades_history');
427 $index = new xmldb_index('userid-itemid-timemodified', XMLDB_INDEX_NOTUNIQUE, array('userid', 'itemid', 'timemodified'));
429 // Conditionally launch add index userid-itemid.
430 if (!$dbman->index_exists($table, $index)) {
431 $dbman->add_index($table, $index);
434 // Main savepoint reached.
435 upgrade_main_savepoint(true, 2016021501.00);
438 if ($oldversion < 2016030103.00) {
440 // MDL-50887. Implement plugins infrastructure for antivirus and create ClamAV plugin.
441 // This routine moves core ClamAV configuration to plugin level.
443 // If clamav was configured and enabled, enable the plugin.
444 if (!empty($CFG->runclamonupload) && !empty($CFG->pathtoclam)) {
445 set_config('antiviruses', 'clamav');
446 } else {
447 set_config('antiviruses', '');
450 if (isset($CFG->runclamonupload)) {
451 // Just unset global configuration, we have already enabled the plugin
452 // which implies that ClamAV will be used for scanning uploaded files.
453 unset_config('runclamonupload');
455 // Move core ClamAV configuration settings to plugin.
456 if (isset($CFG->pathtoclam)) {
457 set_config('pathtoclam', $CFG->pathtoclam, 'antivirus_clamav');
458 unset_config('pathtoclam');
460 if (isset($CFG->quarantinedir)) {
461 set_config('quarantinedir', $CFG->quarantinedir, 'antivirus_clamav');
462 unset_config('quarantinedir');
464 if (isset($CFG->clamfailureonupload)) {
465 set_config('clamfailureonupload', $CFG->clamfailureonupload, 'antivirus_clamav');
466 unset_config('clamfailureonupload');
469 // Main savepoint reached.
470 upgrade_main_savepoint(true, 2016030103.00);
473 if ($oldversion < 2016030400.01) {
474 // Add the new services field.
475 $table = new xmldb_table('external_functions');
476 $field = new xmldb_field('services', XMLDB_TYPE_CHAR, '1333', null, null, null, null, 'capabilities');
478 // Conditionally launch add field services.
479 if (!$dbman->field_exists($table, $field)) {
480 $dbman->add_field($table, $field);
482 // Main savepoint reached.
483 upgrade_main_savepoint(true, 2016030400.01);
486 if ($oldversion < 2016041500.50) {
488 // Define table competency to be created.
489 $table = new xmldb_table('competency');
491 // Adding fields to table competency.
492 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
493 $table->add_field('shortname', XMLDB_TYPE_CHAR, '100', null, null, null, null);
494 $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
495 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
496 $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
497 $table->add_field('competencyframeworkid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
498 $table->add_field('parentid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
499 $table->add_field('path', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
500 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
501 $table->add_field('ruletype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
502 $table->add_field('ruleoutcome', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
503 $table->add_field('ruleconfig', XMLDB_TYPE_TEXT, null, null, null, null, null);
504 $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
505 $table->add_field('scaleconfiguration', XMLDB_TYPE_TEXT, null, null, null, null, null);
506 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
507 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
508 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
510 // Adding keys to table competency.
511 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
513 // Adding indexes to table competency.
514 $table->add_index('idnumberframework', XMLDB_INDEX_UNIQUE, array('competencyframeworkid', 'idnumber'));
515 $table->add_index('ruleoutcome', XMLDB_INDEX_NOTUNIQUE, array('ruleoutcome'));
517 // Conditionally launch create table for competency.
518 if (!$dbman->table_exists($table)) {
519 $dbman->create_table($table);
522 // Main savepoint reached.
523 upgrade_main_savepoint(true, 2016041500.50);
526 if ($oldversion < 2016041500.51) {
528 // Define table competency_coursecompsetting to be created.
529 $table = new xmldb_table('competency_coursecompsetting');
531 // Adding fields to table competency_coursecompsetting.
532 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
533 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
534 $table->add_field('pushratingstouserplans', XMLDB_TYPE_INTEGER, '2', null, null, null, null);
535 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
536 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
537 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
539 // Adding keys to table competency_coursecompsetting.
540 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
541 $table->add_key('courseidlink', XMLDB_KEY_FOREIGN_UNIQUE, array('courseid'), 'course', array('id'));
543 // Conditionally launch create table for competency_coursecompsetting.
544 if (!$dbman->table_exists($table)) {
545 $dbman->create_table($table);
548 // Main savepoint reached.
549 upgrade_main_savepoint(true, 2016041500.51);
552 if ($oldversion < 2016041500.52) {
554 // Define table competency_framework to be created.
555 $table = new xmldb_table('competency_framework');
557 // Adding fields to table competency_framework.
558 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
559 $table->add_field('shortname', XMLDB_TYPE_CHAR, '100', null, null, null, null);
560 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
561 $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
562 $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
563 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
564 $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '11', null, null, null, null);
565 $table->add_field('scaleconfiguration', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
566 $table->add_field('visible', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
567 $table->add_field('taxonomies', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
568 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
569 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
570 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
572 // Adding keys to table competency_framework.
573 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
575 // Adding indexes to table competency_framework.
576 $table->add_index('idnumber', XMLDB_INDEX_UNIQUE, array('idnumber'));
578 // Conditionally launch create table for competency_framework.
579 if (!$dbman->table_exists($table)) {
580 $dbman->create_table($table);
583 // Main savepoint reached.
584 upgrade_main_savepoint(true, 2016041500.52);
587 if ($oldversion < 2016041500.53) {
589 // Define table competency_coursecomp to be created.
590 $table = new xmldb_table('competency_coursecomp');
592 // Adding fields to table competency_coursecomp.
593 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
594 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
595 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
596 $table->add_field('ruleoutcome', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, null);
597 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
598 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
599 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
600 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
602 // Adding keys to table competency_coursecomp.
603 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
604 $table->add_key('courseidlink', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
605 $table->add_key('competencyid', XMLDB_KEY_FOREIGN, array('competencyid'), 'competency_competency', array('id'));
607 // Adding indexes to table competency_coursecomp.
608 $table->add_index('courseidruleoutcome', XMLDB_INDEX_NOTUNIQUE, array('courseid', 'ruleoutcome'));
609 $table->add_index('courseidcompetencyid', XMLDB_INDEX_UNIQUE, array('courseid', 'competencyid'));
611 // Conditionally launch create table for competency_coursecomp.
612 if (!$dbman->table_exists($table)) {
613 $dbman->create_table($table);
616 // Main savepoint reached.
617 upgrade_main_savepoint(true, 2016041500.53);
620 if ($oldversion < 2016041500.54) {
622 // Define table competency_plan to be created.
623 $table = new xmldb_table('competency_plan');
625 // Adding fields to table competency_plan.
626 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
627 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
628 $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
629 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
630 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
631 $table->add_field('templateid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
632 $table->add_field('origtemplateid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
633 $table->add_field('status', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null);
634 $table->add_field('duedate', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
635 $table->add_field('reviewerid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
636 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
637 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
638 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
640 // Adding keys to table competency_plan.
641 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
643 // Adding indexes to table competency_plan.
644 $table->add_index('useridstatus', XMLDB_INDEX_NOTUNIQUE, array('userid', 'status'));
645 $table->add_index('templateid', XMLDB_INDEX_NOTUNIQUE, array('templateid'));
646 $table->add_index('statusduedate', XMLDB_INDEX_NOTUNIQUE, array('status', 'duedate'));
648 // Conditionally launch create table for competency_plan.
649 if (!$dbman->table_exists($table)) {
650 $dbman->create_table($table);
653 // Main savepoint reached.
654 upgrade_main_savepoint(true, 2016041500.54);
657 if ($oldversion < 2016041500.55) {
659 // Define table competency_template to be created.
660 $table = new xmldb_table('competency_template');
662 // Adding fields to table competency_template.
663 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
664 $table->add_field('shortname', XMLDB_TYPE_CHAR, '100', null, null, null, null);
665 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
666 $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
667 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
668 $table->add_field('visible', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
669 $table->add_field('duedate', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
670 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
671 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
672 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
674 // Adding keys to table competency_template.
675 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
677 // Conditionally launch create table for competency_template.
678 if (!$dbman->table_exists($table)) {
679 $dbman->create_table($table);
682 // Main savepoint reached.
683 upgrade_main_savepoint(true, 2016041500.55);
686 if ($oldversion < 2016041500.56) {
688 // Define table competency_templatecomp to be created.
689 $table = new xmldb_table('competency_templatecomp');
691 // Adding fields to table competency_templatecomp.
692 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
693 $table->add_field('templateid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
694 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
695 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
696 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
697 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
698 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
700 // Adding keys to table competency_templatecomp.
701 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
702 $table->add_key('templateidlink', XMLDB_KEY_FOREIGN, array('templateid'), 'competency_template', array('id'));
703 $table->add_key('competencyid', XMLDB_KEY_FOREIGN, array('competencyid'), 'competency_competency', array('id'));
705 // Conditionally launch create table for competency_templatecomp.
706 if (!$dbman->table_exists($table)) {
707 $dbman->create_table($table);
710 // Main savepoint reached.
711 upgrade_main_savepoint(true, 2016041500.56);
714 if ($oldversion < 2016041500.57) {
716 // Define table competency_templatecohort to be created.
717 $table = new xmldb_table('competency_templatecohort');
719 // Adding fields to table competency_templatecohort.
720 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
721 $table->add_field('templateid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
722 $table->add_field('cohortid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
723 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
724 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
725 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
727 // Adding keys to table competency_templatecohort.
728 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
730 // Adding indexes to table competency_templatecohort.
731 $table->add_index('templateid', XMLDB_INDEX_NOTUNIQUE, array('templateid'));
732 $table->add_index('templatecohortids', XMLDB_INDEX_UNIQUE, array('templateid', 'cohortid'));
734 // Conditionally launch create table for competency_templatecohort.
735 if (!$dbman->table_exists($table)) {
736 $dbman->create_table($table);
739 // Main savepoint reached.
740 upgrade_main_savepoint(true, 2016041500.57);
743 if ($oldversion < 2016041500.58) {
745 // Define table competency_relatedcomp to be created.
746 $table = new xmldb_table('competency_relatedcomp');
748 // Adding fields to table competency_relatedcomp.
749 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
750 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
751 $table->add_field('relatedcompetencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
752 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
753 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
754 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
756 // Adding keys to table competency_relatedcomp.
757 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
759 // Conditionally launch create table for competency_relatedcomp.
760 if (!$dbman->table_exists($table)) {
761 $dbman->create_table($table);
764 // Main savepoint reached.
765 upgrade_main_savepoint(true, 2016041500.58);
768 if ($oldversion < 2016041500.59) {
770 // Define table competency_usercomp to be created.
771 $table = new xmldb_table('competency_usercomp');
773 // Adding fields to table competency_usercomp.
774 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
775 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
776 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
777 $table->add_field('status', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
778 $table->add_field('reviewerid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
779 $table->add_field('proficiency', XMLDB_TYPE_INTEGER, '2', null, null, null, null);
780 $table->add_field('grade', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
781 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
782 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
783 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
785 // Adding keys to table competency_usercomp.
786 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
788 // Adding indexes to table competency_usercomp.
789 $table->add_index('useridcompetency', XMLDB_INDEX_UNIQUE, array('userid', 'competencyid'));
791 // Conditionally launch create table for competency_usercomp.
792 if (!$dbman->table_exists($table)) {
793 $dbman->create_table($table);
796 // Main savepoint reached.
797 upgrade_main_savepoint(true, 2016041500.59);
800 if ($oldversion < 2016041500.60) {
802 // Define table competency_usercompcourse to be created.
803 $table = new xmldb_table('competency_usercompcourse');
805 // Adding fields to table competency_usercompcourse.
806 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
807 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
808 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
809 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
810 $table->add_field('proficiency', XMLDB_TYPE_INTEGER, '2', null, null, null, null);
811 $table->add_field('grade', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
812 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
813 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
814 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
816 // Adding keys to table competency_usercompcourse.
817 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
819 // Adding indexes to table competency_usercompcourse.
820 $table->add_index('useridcoursecomp', XMLDB_INDEX_UNIQUE, array('userid', 'courseid', 'competencyid'));
822 // Conditionally launch create table for competency_usercompcourse.
823 if (!$dbman->table_exists($table)) {
824 $dbman->create_table($table);
827 // Main savepoint reached.
828 upgrade_main_savepoint(true, 2016041500.60);
831 if ($oldversion < 2016041500.61) {
833 // Define table competency_usercompplan to be created.
834 $table = new xmldb_table('competency_usercompplan');
836 // Adding fields to table competency_usercompplan.
837 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
838 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
839 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
840 $table->add_field('planid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
841 $table->add_field('proficiency', XMLDB_TYPE_INTEGER, '2', null, null, null, null);
842 $table->add_field('grade', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
843 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
844 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
845 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
846 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
848 // Adding keys to table competency_usercompplan.
849 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
851 // Adding indexes to table competency_usercompplan.
852 $table->add_index('usercompetencyplan', XMLDB_INDEX_UNIQUE, array('userid', 'competencyid', 'planid'));
854 // Conditionally launch create table for competency_usercompplan.
855 if (!$dbman->table_exists($table)) {
856 $dbman->create_table($table);
859 // Main savepoint reached.
860 upgrade_main_savepoint(true, 2016041500.61);
863 if ($oldversion < 2016041500.62) {
865 // Define table competency_plancomp to be created.
866 $table = new xmldb_table('competency_plancomp');
868 // Adding fields to table competency_plancomp.
869 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
870 $table->add_field('planid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
871 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
872 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
873 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
874 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
875 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
877 // Adding keys to table competency_plancomp.
878 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
880 // Adding indexes to table competency_plancomp.
881 $table->add_index('planidcompetencyid', XMLDB_INDEX_UNIQUE, array('planid', 'competencyid'));
883 // Conditionally launch create table for competency_plancomp.
884 if (!$dbman->table_exists($table)) {
885 $dbman->create_table($table);
888 // Main savepoint reached.
889 upgrade_main_savepoint(true, 2016041500.62);
892 if ($oldversion < 2016041500.63) {
894 // Define table competency_evidence to be created.
895 $table = new xmldb_table('competency_evidence');
897 // Adding fields to table competency_evidence.
898 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
899 $table->add_field('usercompetencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
900 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
901 $table->add_field('action', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, null);
902 $table->add_field('actionuserid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
903 $table->add_field('descidentifier', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
904 $table->add_field('desccomponent', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
905 $table->add_field('desca', XMLDB_TYPE_TEXT, null, null, null, null, null);
906 $table->add_field('url', XMLDB_TYPE_CHAR, '255', null, null, null, null);
907 $table->add_field('grade', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
908 $table->add_field('note', XMLDB_TYPE_TEXT, null, null, null, null, null);
909 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
910 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
911 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
913 // Adding keys to table competency_evidence.
914 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
916 // Adding indexes to table competency_evidence.
917 $table->add_index('usercompetencyid', XMLDB_INDEX_NOTUNIQUE, array('usercompetencyid'));
919 // Conditionally launch create table for competency_evidence.
920 if (!$dbman->table_exists($table)) {
921 $dbman->create_table($table);
924 // Main savepoint reached.
925 upgrade_main_savepoint(true, 2016041500.63);
928 if ($oldversion < 2016041500.64) {
930 // Define table competency_userevidence to be created.
931 $table = new xmldb_table('competency_userevidence');
933 // Adding fields to table competency_userevidence.
934 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
935 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
936 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
937 $table->add_field('description', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
938 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null);
939 $table->add_field('url', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
940 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
941 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
942 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
944 // Adding keys to table competency_userevidence.
945 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
947 // Adding indexes to table competency_userevidence.
948 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
950 // Conditionally launch create table for competency_userevidence.
951 if (!$dbman->table_exists($table)) {
952 $dbman->create_table($table);
955 // Main savepoint reached.
956 upgrade_main_savepoint(true, 2016041500.64);
959 if ($oldversion < 2016041500.65) {
961 // Define table competency_userevidencecomp to be created.
962 $table = new xmldb_table('competency_userevidencecomp');
964 // Adding fields to table competency_userevidencecomp.
965 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
966 $table->add_field('userevidenceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
967 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
968 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
969 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
970 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
972 // Adding keys to table competency_userevidencecomp.
973 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
975 // Adding indexes to table competency_userevidencecomp.
976 $table->add_index('userevidenceid', XMLDB_INDEX_NOTUNIQUE, array('userevidenceid'));
977 $table->add_index('userevidencecompids', XMLDB_INDEX_UNIQUE, array('userevidenceid', 'competencyid'));
979 // Conditionally launch create table for competency_userevidencecomp.
980 if (!$dbman->table_exists($table)) {
981 $dbman->create_table($table);
984 // Main savepoint reached.
985 upgrade_main_savepoint(true, 2016041500.65);
988 if ($oldversion < 2016041500.66) {
990 // Define table competency_modulecomp to be created.
991 $table = new xmldb_table('competency_modulecomp');
993 // Adding fields to table competency_modulecomp.
994 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
995 $table->add_field('cmid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
996 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
997 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
998 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
999 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1000 $table->add_field('competencyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1001 $table->add_field('ruleoutcome', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, null);
1003 // Adding keys to table competency_modulecomp.
1004 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1005 $table->add_key('cmidkey', XMLDB_KEY_FOREIGN, array('cmid'), 'course_modules', array('id'));
1006 $table->add_key('competencyidkey', XMLDB_KEY_FOREIGN, array('competencyid'), 'competency_competency', array('id'));
1008 // Adding indexes to table competency_modulecomp.
1009 $table->add_index('cmidruleoutcome', XMLDB_INDEX_NOTUNIQUE, array('cmid', 'ruleoutcome'));
1010 $table->add_index('cmidcompetencyid', XMLDB_INDEX_UNIQUE, array('cmid', 'competencyid'));
1012 // Conditionally launch create table for competency_modulecomp.
1013 if (!$dbman->table_exists($table)) {
1014 $dbman->create_table($table);
1017 // Main savepoint reached.
1018 upgrade_main_savepoint(true, 2016041500.66);
1021 if ($oldversion < 2016042100.00) {
1022 // Update all countries to upper case.
1023 $DB->execute("UPDATE {user} SET country = UPPER(country)");
1024 // Main savepoint reached.
1025 upgrade_main_savepoint(true, 2016042100.00);
1028 if ($oldversion < 2016042600.01) {
1029 $deprecatedwebservices = [
1030 'moodle_course_create_courses',
1031 'moodle_course_get_courses',
1032 'moodle_enrol_get_enrolled_users',
1033 'moodle_enrol_get_users_courses',
1034 'moodle_enrol_manual_enrol_users',
1035 'moodle_file_get_files',
1036 'moodle_file_upload',
1037 'moodle_group_add_groupmembers',
1038 'moodle_group_create_groups',
1039 'moodle_group_delete_groupmembers',
1040 'moodle_group_delete_groups',
1041 'moodle_group_get_course_groups',
1042 'moodle_group_get_groupmembers',
1043 'moodle_group_get_groups',
1044 'moodle_message_send_instantmessages',
1045 'moodle_notes_create_notes',
1046 'moodle_role_assign',
1047 'moodle_role_unassign',
1048 'moodle_user_create_users',
1049 'moodle_user_delete_users',
1050 'moodle_user_get_course_participants_by_id',
1051 'moodle_user_get_users_by_courseid',
1052 'moodle_user_get_users_by_id',
1053 'moodle_user_update_users',
1054 'core_grade_get_definitions',
1055 'core_user_get_users_by_id',
1056 'moodle_webservice_get_siteinfo',
1057 'mod_forum_get_forum_discussions'
1060 list($insql, $params) = $DB->get_in_or_equal($deprecatedwebservices);
1061 $DB->delete_records_select('external_functions', "name $insql", $params);
1062 $DB->delete_records_select('external_services_functions', "functionname $insql", $params);
1063 // Main savepoint reached.
1064 upgrade_main_savepoint(true, 2016042600.01);
1067 if ($oldversion < 2016051300.00) {
1068 // Add a default competency rating scale.
1069 make_competence_scale();
1071 // Savepoint reached.
1072 upgrade_main_savepoint(true, 2016051300.00);
1075 if ($oldversion < 2016051700.01) {
1076 // This script is included in each major version upgrade process (3.0, 3.1) so make sure we don't run it twice.
1077 if (empty($CFG->upgrade_letterboundarycourses)) {
1078 // MDL-45390. If a grade is being displayed with letters and the grade boundaries are not being adhered to properly
1079 // then this course will also be frozen.
1080 // If the changes are accepted then the display of some grades may change.
1081 // This is here to freeze the gradebook in affected courses.
1082 upgrade_course_letter_boundary();
1084 // To skip running the same script on the upgrade to the next major version release.
1085 set_config('upgrade_letterboundarycourses', 1);
1087 // Main savepoint reached.
1088 upgrade_main_savepoint(true, 2016051700.01);
1091 // Moodle v3.1.0 release upgrade line.
1092 // Put any upgrade step following this.
1094 if ($oldversion < 2016081700.00) {
1096 // If someone is emotionally attached to it let's leave the config (basically the version) there.
1097 if (!file_exists($CFG->dirroot . '/report/search/classes/output/form.php')) {
1098 unset_all_config_for_plugin('report_search');
1101 // Savepoint reached.
1102 upgrade_main_savepoint(true, 2016081700.00);
1105 if ($oldversion < 2016081700.02) {
1106 // Default schedule values.
1107 $hour = 0;
1108 $minute = 0;
1110 // Get the old settings.
1111 if (isset($CFG->statsruntimestarthour)) {
1112 $hour = $CFG->statsruntimestarthour;
1114 if (isset($CFG->statsruntimestartminute)) {
1115 $minute = $CFG->statsruntimestartminute;
1118 // Retrieve the scheduled task record first.
1119 $stattask = $DB->get_record('task_scheduled', array('component' => 'moodle', 'classname' => '\core\task\stats_cron_task'));
1121 // Don't touch customised scheduling.
1122 if ($stattask && !$stattask->customised) {
1124 $nextruntime = mktime($hour, $minute, 0, date('m'), date('d'), date('Y'));
1125 if ($nextruntime < $stattask->lastruntime) {
1126 // Add 24 hours to the next run time.
1127 $newtime = new DateTime();
1128 $newtime->setTimestamp($nextruntime);
1129 $newtime->add(new DateInterval('P1D'));
1130 $nextruntime = $newtime->getTimestamp();
1132 $stattask->nextruntime = $nextruntime;
1133 $stattask->minute = $minute;
1134 $stattask->hour = $hour;
1135 $stattask->customised = 1;
1136 $DB->update_record('task_scheduled', $stattask);
1138 // These settings are no longer used.
1139 unset_config('statsruntimestarthour');
1140 unset_config('statsruntimestartminute');
1141 unset_config('statslastexecution');
1143 upgrade_main_savepoint(true, 2016081700.02);
1146 if ($oldversion < 2016082200.00) {
1147 // An upgrade step to remove any duplicate stamps, within the same context, in the question_categories table, and to
1148 // add a unique index to (contextid, stamp) to avoid future stamp duplication. See MDL-54864.
1150 // Extend the execution time limit of the script to 2 hours.
1151 upgrade_set_timeout(7200);
1153 // This SQL fetches the id of those records which have duplicate stamps within the same context.
1154 // This doesn't return the original record within the context, from which the duplicate stamps were likely created.
1155 $fromclause = "FROM (
1156 SELECT min(id) AS minid, contextid, stamp
1157 FROM {question_categories}
1158 GROUP BY contextid, stamp
1159 ) minid
1160 JOIN {question_categories} qc
1161 ON qc.contextid = minid.contextid AND qc.stamp = minid.stamp AND qc.id > minid.minid";
1163 // Get the total record count - used for the progress bar.
1164 $countduplicatessql = "SELECT count(qc.id) $fromclause";
1165 $total = $DB->count_records_sql($countduplicatessql);
1167 // Get the records themselves.
1168 $getduplicatessql = "SELECT qc.id $fromclause ORDER BY minid";
1169 $rs = $DB->get_recordset_sql($getduplicatessql);
1171 // For each duplicate, update the stamp to a new random value.
1172 $i = 0;
1173 $pbar = new progress_bar('updatequestioncategorystamp', 500, true);
1174 foreach ($rs as $record) {
1175 // Generate a new, unique stamp and update the record.
1176 do {
1177 $newstamp = make_unique_id_code();
1178 } while (isset($usedstamps[$newstamp]));
1179 $usedstamps[$newstamp] = 1;
1180 $DB->set_field('question_categories', 'stamp', $newstamp, array('id' => $record->id));
1182 // Update progress.
1183 $i++;
1184 $pbar->update($i, $total, "Updating duplicate question category stamp - $i/$total.");
1186 $rs->close();
1187 unset($usedstamps);
1189 // The uniqueness of each (contextid, stamp) pair is now guaranteed, so add the unique index to stop future duplicates.
1190 $table = new xmldb_table('question_categories');
1191 $index = new xmldb_index('contextidstamp', XMLDB_INDEX_UNIQUE, array('contextid', 'stamp'));
1192 if (!$dbman->index_exists($table, $index)) {
1193 $dbman->add_index($table, $index);
1196 // Savepoint reached.
1197 upgrade_main_savepoint(true, 2016082200.00);
1200 if ($oldversion < 2016091900.00) {
1202 // Removing the themes from core.
1203 $themes = array('base', 'canvas');
1205 foreach ($themes as $key => $theme) {
1206 if (check_dir_exists($CFG->dirroot . '/theme/' . $theme, false)) {
1207 // Ignore the themes that have been re-downloaded.
1208 unset($themes[$key]);
1212 if (!empty($themes)) {
1213 // Hacky emulation of plugin uninstallation.
1214 foreach ($themes as $theme) {
1215 unset_all_config_for_plugin('theme_' . $theme);
1219 // Main savepoint reached.
1220 upgrade_main_savepoint(true, 2016091900.00);
1223 if ($oldversion < 2016091900.02) {
1225 // Define index attemptstepid-name (unique) to be dropped from question_attempt_step_data.
1226 $table = new xmldb_table('question_attempt_step_data');
1227 $index = new xmldb_index('attemptstepid-name', XMLDB_INDEX_UNIQUE, array('attemptstepid', 'name'));
1229 // Conditionally launch drop index attemptstepid-name.
1230 if ($dbman->index_exists($table, $index)) {
1231 $dbman->drop_index($table, $index);
1234 // Main savepoint reached.
1235 upgrade_main_savepoint(true, 2016091900.02);
1238 if ($oldversion < 2016100300.00) {
1239 unset_config('enablecssoptimiser');
1241 upgrade_main_savepoint(true, 2016100300.00);
1244 if ($oldversion < 2016100501.00) {
1246 // Define field enddate to be added to course.
1247 $table = new xmldb_table('course');
1248 $field = new xmldb_field('enddate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'startdate');
1250 // Conditionally launch add field enddate.
1251 if (!$dbman->field_exists($table, $field)) {
1252 $dbman->add_field($table, $field);
1255 // Main savepoint reached.
1256 upgrade_main_savepoint(true, 2016100501.00);
1259 if ($oldversion < 2016101100.00) {
1260 // Define field component to be added to message.
1261 $table = new xmldb_table('message');
1262 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'timeusertodeleted');
1264 // Conditionally launch add field component.
1265 if (!$dbman->field_exists($table, $field)) {
1266 $dbman->add_field($table, $field);
1269 // Define field eventtype to be added to message.
1270 $field = new xmldb_field('eventtype', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'component');
1272 // Conditionally launch add field eventtype.
1273 if (!$dbman->field_exists($table, $field)) {
1274 $dbman->add_field($table, $field);
1277 // Main savepoint reached.
1278 upgrade_main_savepoint(true, 2016101100.00);
1282 if ($oldversion < 2016101101.00) {
1283 // Define field component to be added to message_read.
1284 $table = new xmldb_table('message_read');
1285 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'timeusertodeleted');
1287 // Conditionally launch add field component.
1288 if (!$dbman->field_exists($table, $field)) {
1289 $dbman->add_field($table, $field);
1292 // Define field eventtype to be added to message_read.
1293 $field = new xmldb_field('eventtype', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'component');
1295 // Conditionally launch add field eventtype.
1296 if (!$dbman->field_exists($table, $field)) {
1297 $dbman->add_field($table, $field);
1300 // Main savepoint reached.
1301 upgrade_main_savepoint(true, 2016101101.00);
1304 if ($oldversion < 2016101401.00) {
1305 // Clean up repository_alfresco config unless plugin has been manually installed.
1306 if (!file_exists($CFG->dirroot . '/repository/alfresco/lib.php')) {
1307 // Remove capabilities.
1308 capabilities_cleanup('repository_alfresco');
1309 // Clean config.
1310 unset_all_config_for_plugin('repository_alfresco');
1313 // Savepoint reached.
1314 upgrade_main_savepoint(true, 2016101401.00);
1317 if ($oldversion < 2016101401.02) {
1318 $table = new xmldb_table('external_tokens');
1319 $field = new xmldb_field('privatetoken', XMLDB_TYPE_CHAR, '64', null, null, null, null);
1321 // Conditionally add privatetoken field to the external_tokens table.
1322 if (!$dbman->field_exists($table, $field)) {
1323 $dbman->add_field($table, $field);
1326 // Main savepoint reached.
1327 upgrade_main_savepoint(true, 2016101401.02);
1330 if ($oldversion < 2016110202.00) {
1332 // Force uninstall of deleted authentication plugin.
1333 if (!file_exists("$CFG->dirroot/auth/radius")) {
1334 // Leave settings inplace if there are radius users.
1335 if (!$DB->record_exists('user', array('auth' => 'radius', 'deleted' => 0))) {
1336 // Remove all other associated config.
1337 unset_all_config_for_plugin('auth/radius');
1338 // The version number for radius is in this format.
1339 unset_all_config_for_plugin('auth_radius');
1342 upgrade_main_savepoint(true, 2016110202.00);
1345 if ($oldversion < 2016110300.00) {
1346 // Remove unused admin email setting.
1347 unset_config('emailonlyfromreplyaddress');
1349 // Main savepoint reached.
1350 upgrade_main_savepoint(true, 2016110300.00);
1353 if ($oldversion < 2016110500.00) {
1355 $oldplayers = [
1356 'vimeo' => null,
1357 'mp3' => ['.mp3'],
1358 'html5video' => ['.mov', '.mp4', '.m4v', '.mpeg', '.mpe', '.mpg', '.ogv', '.webm'],
1359 'flv' => ['.flv', '.f4v'],
1360 'html5audio' => ['.aac', '.flac', '.mp3', '.m4a', '.oga', '.ogg', '.wav'],
1361 'youtube' => null,
1362 'swf' => null,
1365 // Convert hardcoded media players to the settings of the new media player plugin type.
1366 if (get_config('core', 'media_plugins_sortorder') === false) {
1367 $enabledplugins = [];
1368 $videoextensions = [];
1369 $audioextensions = [];
1370 foreach ($oldplayers as $oldplayer => $extensions) {
1371 $settingname = 'core_media_enable_'.$oldplayer;
1372 if (!empty($CFG->$settingname)) {
1373 if ($extensions) {
1374 // VideoJS will be used for all media files players that were used previously.
1375 $enabledplugins['videojs'] = 'videojs';
1376 if ($oldplayer === 'mp3' || $oldplayer === 'html5audio') {
1377 $audioextensions += array_combine($extensions, $extensions);
1378 } else {
1379 $videoextensions += array_combine($extensions, $extensions);
1381 } else {
1382 // Enable youtube, vimeo and swf.
1383 $enabledplugins[$oldplayer] = $oldplayer;
1388 set_config('media_plugins_sortorder', join(',', $enabledplugins));
1390 // Configure VideoJS to match the existing players set up.
1391 if ($enabledplugins['videojs']) {
1392 $enabledplugins[] = 'videojs';
1393 set_config('audioextensions', join(',', $audioextensions), 'media_videojs');
1394 set_config('videoextensions', join(',', $videoextensions), 'media_videojs');
1395 $useflash = !empty($CFG->core_media_enable_flv) || !empty($CFG->core_media_enable_mp3);
1396 set_config('useflash', $useflash, 'media_videojs');
1397 if (empty($CFG->core_media_enable_youtube)) {
1398 // Normally YouTube is enabled in videojs, but if youtube converter was disabled before upgrade
1399 // disable it in videojs as well.
1400 set_config('youtube', false, 'media_videojs');
1405 // Unset old settings.
1406 foreach ($oldplayers as $oldplayer => $extensions) {
1407 unset_config('core_media_enable_' . $oldplayer);
1410 // Preset defaults if CORE_MEDIA_VIDEO_WIDTH and CORE_MEDIA_VIDEO_HEIGHT are specified in config.php .
1411 // After this upgrade step these constants will not be used any more.
1412 if (defined('CORE_MEDIA_VIDEO_WIDTH')) {
1413 set_config('media_default_width', CORE_MEDIA_VIDEO_WIDTH);
1415 if (defined('CORE_MEDIA_VIDEO_HEIGHT')) {
1416 set_config('media_default_height', CORE_MEDIA_VIDEO_HEIGHT);
1419 // Savepoint reached.
1420 upgrade_main_savepoint(true, 2016110500.00);
1423 if ($oldversion < 2016110600.00) {
1424 // Define a field 'deletioninprogress' in the 'course_modules' table, to background deletion tasks.
1425 $table = new xmldb_table('course_modules');
1426 $field = new xmldb_field('deletioninprogress', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'availability');
1428 // Conditionally launch add field 'deletioninprogress'.
1429 if (!$dbman->field_exists($table, $field)) {
1430 $dbman->add_field($table, $field);
1433 // Main savepoint reached.
1434 upgrade_main_savepoint(true, 2016110600.00);
1437 if ($oldversion < 2016112200.01) {
1439 // Define field requiredbytheme to be added to block_instances.
1440 $table = new xmldb_table('block_instances');
1441 $field = new xmldb_field('requiredbytheme', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'showinsubcontexts');
1443 // Conditionally launch add field requiredbytheme.
1444 if (!$dbman->field_exists($table, $field)) {
1445 $dbman->add_field($table, $field);
1448 // Main savepoint reached.
1449 upgrade_main_savepoint(true, 2016112200.01);
1451 if ($oldversion < 2016112200.02) {
1453 // Change the existing site level admin and settings blocks to be requiredbytheme which means they won't show in boost.
1454 $context = context_system::instance();
1455 $params = array('blockname' => 'settings', 'parentcontextid' => $context->id);
1456 $DB->set_field('block_instances', 'requiredbytheme', 1, $params);
1458 $params = array('blockname' => 'navigation', 'parentcontextid' => $context->id);
1459 $DB->set_field('block_instances', 'requiredbytheme', 1, $params);
1460 // Main savepoint reached.
1461 upgrade_main_savepoint(true, 2016112200.02);
1464 // Automatically generated Moodle v3.2.0 release upgrade line.
1465 // Put any upgrade step following this.
1467 if ($oldversion < 2016122800.00) {
1468 // Find all roles with the coursecreator archetype.
1469 $coursecreatorroleids = $DB->get_records('role', array('archetype' => 'coursecreator'), '', 'id');
1471 $context = context_system::instance();
1472 $capability = 'moodle/site:configview';
1474 foreach ($coursecreatorroleids as $roleid => $notused) {
1476 // Check that the capability has not already been assigned. If it has then it's either already set
1477 // to allow or specifically set to prohibit or prevent.
1478 if (!$DB->record_exists('role_capabilities', array('roleid' => $roleid, 'capability' => $capability))) {
1479 // Assign the capability.
1480 $cap = new stdClass();
1481 $cap->contextid = $context->id;
1482 $cap->roleid = $roleid;
1483 $cap->capability = $capability;
1484 $cap->permission = CAP_ALLOW;
1485 $cap->timemodified = time();
1486 $cap->modifierid = 0;
1488 $DB->insert_record('role_capabilities', $cap);
1492 // Main savepoint reached.
1493 upgrade_main_savepoint(true, 2016122800.00);
1496 if ($oldversion < 2017020200.01) {
1498 // Define index useridfrom_timeuserfromdeleted_notification (not unique) to be added to message.
1499 $table = new xmldb_table('message');
1500 $index = new xmldb_index('useridfrom_timeuserfromdeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'timeuserfromdeleted', 'notification'));
1502 // Conditionally launch add index useridfrom_timeuserfromdeleted_notification.
1503 if (!$dbman->index_exists($table, $index)) {
1504 $dbman->add_index($table, $index);
1507 // Define index useridto_timeusertodeleted_notification (not unique) to be added to message.
1508 $index = new xmldb_index('useridto_timeusertodeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridto', 'timeusertodeleted', 'notification'));
1510 // Conditionally launch add index useridto_timeusertodeleted_notification.
1511 if (!$dbman->index_exists($table, $index)) {
1512 $dbman->add_index($table, $index);
1515 $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
1517 // Conditionally launch drop index useridto.
1518 if ($dbman->index_exists($table, $index)) {
1519 $dbman->drop_index($table, $index);
1522 // Main savepoint reached.
1523 upgrade_main_savepoint(true, 2017020200.01);
1526 if ($oldversion < 2017020200.02) {
1528 // Define index useridfrom_timeuserfromdeleted_notification (not unique) to be added to message_read.
1529 $table = new xmldb_table('message_read');
1530 $index = new xmldb_index('useridfrom_timeuserfromdeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'timeuserfromdeleted', 'notification'));
1532 // Conditionally launch add index useridfrom_timeuserfromdeleted_notification.
1533 if (!$dbman->index_exists($table, $index)) {
1534 $dbman->add_index($table, $index);
1537 // Define index useridto_timeusertodeleted_notification (not unique) to be added to message_read.
1538 $index = new xmldb_index('useridto_timeusertodeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridto', 'timeusertodeleted', 'notification'));
1540 // Conditionally launch add index useridto_timeusertodeleted_notification.
1541 if (!$dbman->index_exists($table, $index)) {
1542 $dbman->add_index($table, $index);
1545 $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
1547 // Conditionally launch drop index useridto.
1548 if ($dbman->index_exists($table, $index)) {
1549 $dbman->drop_index($table, $index);
1552 // Main savepoint reached.
1553 upgrade_main_savepoint(true, 2017020200.02);
1556 if ($oldversion < 2017020901.00) {
1558 // Delete "orphaned" block positions. Note, the query does not use indexes (because there are none),
1559 // if it runs too long during upgrade you can comment this line - it will leave orphaned records
1560 // in the database but they won't bother you.
1561 upgrade_block_positions();
1563 // Main savepoint reached.
1564 upgrade_main_savepoint(true, 2017020901.00);
1567 if ($oldversion < 2017021300.00) {
1568 unset_config('loginpasswordautocomplete');
1569 upgrade_main_savepoint(true, 2017021300.00);
1572 if ($oldversion < 2017021400.00) {
1573 // Define field visibleoncoursepage to be added to course_modules.
1574 $table = new xmldb_table('course_modules');
1575 $field = new xmldb_field('visibleoncoursepage', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'visible');
1577 // Conditionally launch add field visibleoncoursepage.
1578 if (!$dbman->field_exists($table, $field)) {
1579 $dbman->add_field($table, $field);
1582 // Main savepoint reached.
1583 upgrade_main_savepoint(true, 2017021400.00);
1586 if ($oldversion < 2017030700.00) {
1588 // Define field priority to be added to event.
1589 $table = new xmldb_table('event');
1590 $field = new xmldb_field('priority', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'subscriptionid');
1592 // Conditionally launch add field priority.
1593 if (!$dbman->field_exists($table, $field)) {
1594 $dbman->add_field($table, $field);
1597 // Main savepoint reached.
1598 upgrade_main_savepoint(true, 2017030700.00);
1601 if ($oldversion < 2017031400.00) {
1603 // Define table file_conversion to be created.
1604 $table = new xmldb_table('file_conversion');
1606 // Adding fields to table file_conversion.
1607 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1608 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, 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, null);
1611 $table->add_field('sourcefileid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1612 $table->add_field('targetformat', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1613 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
1614 $table->add_field('statusmessage', XMLDB_TYPE_TEXT, null, null, null, null, null);
1615 $table->add_field('converter', XMLDB_TYPE_CHAR, '255', null, null, null, null);
1616 $table->add_field('destfileid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1617 $table->add_field('data', XMLDB_TYPE_TEXT, null, null, null, null, null);
1619 // Adding keys to table file_conversion.
1620 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1621 $table->add_key('sourcefileid', XMLDB_KEY_FOREIGN, array('sourcefileid'), 'files', array('id'));
1622 $table->add_key('destfileid', XMLDB_KEY_FOREIGN, array('destfileid'), 'files', array('id'));
1624 // Conditionally launch create table for file_conversion.
1625 if (!$dbman->table_exists($table)) {
1626 $dbman->create_table($table);
1629 // Main savepoint reached.
1630 upgrade_main_savepoint(true, 2017031400.00);
1633 if ($oldversion < 2017040400.00) {
1635 // If block_course_overview is no longer present, replace with block_myoverview.
1636 if (!file_exists($CFG->dirroot . '/blocks/course_overview/block_course_overview.php')) {
1637 $DB->set_field('block_instances', 'blockname', 'myoverview', array('blockname' => 'course_overview'));
1640 upgrade_main_savepoint(true, 2017040400.00);
1643 if ($oldversion < 2017040401.00) {
1645 // If block_course_overview is no longer present, remove it.
1646 // Note - we do not need to completely remove the block context etc because we
1647 // have replaced all occurrences of block_course_overview with block_myoverview
1648 // in the upgrade step above.
1649 if (!file_exists($CFG->dirroot . '/blocks/course_overview/block_course_overview.php')) {
1650 // Delete the block from the block table.
1651 $DB->delete_records('block', array('name' => 'course_overview'));
1652 // Remove capabilities.
1653 capabilities_cleanup('block_course_overview');
1654 // Clean config.
1655 unset_all_config_for_plugin('block_course_overview');
1658 upgrade_main_savepoint(true, 2017040401.00);
1661 if ($oldversion < 2017040402.00) {
1663 // Define fields to be added to the 'event' table.
1664 $table = new xmldb_table('event');
1665 $fieldtype = new xmldb_field('type', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, 0, 'instance');
1666 $fieldtimesort = new xmldb_field('timesort', XMLDB_TYPE_INTEGER, '10', null, false, null, null, 'timeduration');
1668 // Conditionally launch add field.
1669 if (!$dbman->field_exists($table, $fieldtype)) {
1670 $dbman->add_field($table, $fieldtype);
1673 // Conditionally launch add field.
1674 if (!$dbman->field_exists($table, $fieldtimesort)) {
1675 $dbman->add_field($table, $fieldtimesort);
1678 // Now, define the index we will be adding.
1679 $index = new xmldb_index('type-timesort', XMLDB_INDEX_NOTUNIQUE, array('type', 'timesort'));
1681 // Conditionally launch add index.
1682 if (!$dbman->index_exists($table, $index)) {
1683 $dbman->add_index($table, $index);
1686 upgrade_main_savepoint(true, 2017040402.00);
1689 if ($oldversion < 2017040700.01) {
1691 // Define table oauth2_issuer to be created.
1692 $table = new xmldb_table('oauth2_issuer');
1694 // Adding fields to table oauth2_issuer.
1695 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, 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);
1699 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1700 $table->add_field('image', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1701 $table->add_field('baseurl', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1702 $table->add_field('clientid', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1703 $table->add_field('clientsecret', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1704 $table->add_field('loginscopes', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1705 $table->add_field('loginscopesoffline', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1706 $table->add_field('loginparams', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1707 $table->add_field('loginparamsoffline', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1708 $table->add_field('alloweddomains', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1709 $table->add_field('scopessupported', XMLDB_TYPE_TEXT, null, null, null, null, null);
1710 $table->add_field('showonloginpage', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
1711 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
1712 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1714 // Adding keys to table oauth2_issuer.
1715 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1717 // Conditionally launch create table for oauth2_issuer.
1718 if (!$dbman->table_exists($table)) {
1719 $dbman->create_table($table);
1722 // Main savepoint reached.
1723 upgrade_main_savepoint(true, 2017040700.01);
1726 if ($oldversion < 2017040700.02) {
1728 // Define table oauth2_endpoint to be created.
1729 $table = new xmldb_table('oauth2_endpoint');
1731 // Adding fields to table oauth2_endpoint.
1732 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1733 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1734 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1735 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1736 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1737 $table->add_field('url', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1738 $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1740 // Adding keys to table oauth2_endpoint.
1741 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1742 $table->add_key('issuer_id_key', XMLDB_KEY_FOREIGN, array('issuerid'), 'oauth2_issuer', array('id'));
1744 // Conditionally launch create table for oauth2_endpoint.
1745 if (!$dbman->table_exists($table)) {
1746 $dbman->create_table($table);
1749 // Main savepoint reached.
1750 upgrade_main_savepoint(true, 2017040700.02);
1753 if ($oldversion < 2017040700.03) {
1755 // Define table oauth2_system_account to be created.
1756 $table = new xmldb_table('oauth2_system_account');
1758 // Adding fields to table oauth2_system_account.
1759 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1760 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1761 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1762 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1763 $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1764 $table->add_field('refreshtoken', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1765 $table->add_field('grantedscopes', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1766 $table->add_field('username', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1767 $table->add_field('email', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1769 // Adding keys to table oauth2_system_account.
1770 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1771 $table->add_key('issueridkey', XMLDB_KEY_FOREIGN_UNIQUE, array('issuerid'), 'oauth2_issuer', array('id'));
1773 // Conditionally launch create table for oauth2_system_account.
1774 if (!$dbman->table_exists($table)) {
1775 $dbman->create_table($table);
1778 // Main savepoint reached.
1779 upgrade_main_savepoint(true, 2017040700.03);
1782 if ($oldversion < 2017040700.04) {
1784 // Define table oauth2_user_field_mapping to be created.
1785 $table = new xmldb_table('oauth2_user_field_mapping');
1787 // Adding fields to table oauth2_user_field_mapping.
1788 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1789 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1790 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1791 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1792 $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1793 $table->add_field('externalfield', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1794 $table->add_field('internalfield', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1796 // Adding keys to table oauth2_user_field_mapping.
1797 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1798 $table->add_key('issuerkey', XMLDB_KEY_FOREIGN, array('issuerid'), 'oauth2_issuer', array('id'));
1799 $table->add_key('uniqinternal', XMLDB_KEY_UNIQUE, array('issuerid', 'internalfield'));
1801 // Conditionally launch create table for oauth2_user_field_mapping.
1802 if (!$dbman->table_exists($table)) {
1803 $dbman->create_table($table);
1806 // Main savepoint reached.
1807 upgrade_main_savepoint(true, 2017040700.04);
1810 if ($oldversion < 2017041801.00) {
1812 // Define table course_completion_defaults to be created.
1813 $table = new xmldb_table('course_completion_defaults');
1815 // Adding fields to table course_completion_defaults.
1816 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1817 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1818 $table->add_field('module', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1819 $table->add_field('completion', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1820 $table->add_field('completionview', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1821 $table->add_field('completionusegrade', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1822 $table->add_field('completionexpected', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1823 $table->add_field('customrules', XMLDB_TYPE_TEXT, null, null, null, null, null);
1825 // Adding keys to table course_completion_defaults.
1826 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1827 $table->add_key('module', XMLDB_KEY_FOREIGN, array('module'), 'modules', array('id'));
1828 $table->add_key('course', XMLDB_KEY_FOREIGN, array('course'), 'course', array('id'));
1830 // Adding indexes to table course_completion_defaults.
1831 $table->add_index('coursemodule', XMLDB_INDEX_UNIQUE, array('course', 'module'));
1833 // Conditionally launch create table for course_completion_defaults.
1834 if (!$dbman->table_exists($table)) {
1835 $dbman->create_table($table);
1838 upgrade_main_savepoint(true, 2017041801.00);
1841 if ($oldversion < 2017050500.01) {
1842 // Get the list of parent event IDs.
1843 $sql = "SELECT DISTINCT repeatid
1844 FROM {event}
1845 WHERE repeatid <> 0";
1846 $parentids = array_keys($DB->get_records_sql($sql));
1847 // Check if there are repeating events we need to process.
1848 if (!empty($parentids)) {
1849 // The repeat IDs of parent events should match their own ID.
1850 // So we need to update parent events that have non-matching IDs and repeat IDs.
1851 list($insql, $params) = $DB->get_in_or_equal($parentids);
1852 $updatesql = "UPDATE {event}
1853 SET repeatid = id
1854 WHERE id <> repeatid
1855 AND id $insql";
1856 $DB->execute($updatesql, $params);
1859 // Main savepoint reached.
1860 upgrade_main_savepoint(true, 2017050500.01);
1863 if ($oldversion < 2017050500.02) {
1864 // MDL-58684:
1865 // Remove all portfolio_tempdata records as these may contain serialized \file_system type objects, which are now unable to
1866 // be unserialized because of changes to the file storage API made in MDL-46375. Portfolio now stores an id reference to
1867 // files instead of the object.
1868 // These records are normally removed after a successful export, however, can be left behind if the user abandons the
1869 // export attempt (a stale record). Additionally, each stale record cannot be reused and is normally cleaned up by the cron
1870 // task core\task\portfolio_cron_task. Since the cron task tries to unserialize them, and generates a warning, we'll remove
1871 // all records here.
1872 $DB->delete_records_select('portfolio_tempdata', 'id > ?', [0]);
1874 // Main savepoint reached.
1875 upgrade_main_savepoint(true, 2017050500.02);
1878 if ($oldversion < 2017050900.01) {
1879 // Create adhoc task for upgrading of existing calendar events.
1880 $record = new \stdClass();
1881 $record->classname = '\core\task\refresh_mod_calendar_events_task';
1882 $record->component = 'core';
1884 // Next run time based from nextruntime computation in \core\task\manager::queue_adhoc_task().
1885 $nextruntime = time() - 1;
1886 $record->nextruntime = $nextruntime;
1887 $DB->insert_record('task_adhoc', $record);
1889 // Main savepoint reached.
1890 upgrade_main_savepoint(true, 2017050900.01);
1893 // Automatically generated Moodle v3.3.0 release upgrade line.
1894 // Put any upgrade step following this.
1896 if ($oldversion < 2017061201.00) {
1897 $table = new xmldb_table('course_sections');
1898 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'availability');
1900 // Define a field 'timemodified' in the 'course_sections' table.
1901 if (!$dbman->field_exists($table, $field)) {
1902 $dbman->add_field($table, $field);
1905 upgrade_main_savepoint(true, 2017061201.00);
1908 if ($oldversion < 2017061301.00) {
1909 // Check if the value of 'navcourselimit' is set to the old default value, if so, change it to the new default.
1910 if ($CFG->navcourselimit == 20) {
1911 set_config('navcourselimit', 10);
1914 // Main savepoint reached.
1915 upgrade_main_savepoint(true, 2017061301.00);
1918 if ($oldversion < 2017071000.00) {
1920 // Define field requireconfirmation to be added to oauth2_issuer.
1921 $table = new xmldb_table('oauth2_issuer');
1922 $field = new xmldb_field('requireconfirmation', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1', 'sortorder');
1924 // Conditionally launch add field requireconfirmation.
1925 if (!$dbman->field_exists($table, $field)) {
1926 $dbman->add_field($table, $field);
1929 // Main savepoint reached.
1930 upgrade_main_savepoint(true, 2017071000.00);
1933 if ($oldversion < 2017071001.00) {
1935 // Define field timemodified to be added to block_instances.
1936 $table = new xmldb_table('block_instances');
1937 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null,
1938 null, null, 'configdata');
1940 // Conditionally launch add field timemodified.
1941 if (!$dbman->field_exists($table, $field)) {
1942 $dbman->add_field($table, $field);
1944 // Set field to current time.
1945 $DB->set_field('block_instances', 'timemodified', time());
1947 // Changing nullability of field timemodified on table block_instances to not null.
1948 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL,
1949 null, null, 'configdata');
1951 // Launch change of nullability for field timemodified.
1952 $dbman->change_field_notnull($table, $field);
1954 // Define index timemodified (not unique) to be added to block_instances.
1955 $index = new xmldb_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1957 // Conditionally launch add index timemodified.
1958 if (!$dbman->index_exists($table, $index)) {
1959 $dbman->add_index($table, $index);
1963 // Define field timecreated to be added to block_instances.
1964 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, null,
1965 null, null, 'configdata');
1967 // Conditionally launch add field timecreated.
1968 if (!$dbman->field_exists($table, $field)) {
1969 $dbman->add_field($table, $field);
1971 // Set field to current time.
1972 $DB->set_field('block_instances', 'timecreated', time());
1974 // Changing nullability of field timecreated on table block_instances to not null.
1975 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL,
1976 null, null, 'configdata');
1978 // Launch change of nullability for field timecreated.
1979 $dbman->change_field_notnull($table, $field);
1982 // Main savepoint reached.
1983 upgrade_main_savepoint(true, 2017071001.00);
1986 if ($oldversion < 2017071100.00 ) {
1987 // Clean old upgrade setting not used anymore.
1988 unset_config('upgrade_minmaxgradestepignored');
1989 upgrade_main_savepoint(true, 2017071100.00);
1992 if ($oldversion < 2017072000.02) {
1994 // Define table analytics_models to be created.
1995 $table = new xmldb_table('analytics_models');
1997 // Adding fields to table analytics_models.
1998 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1999 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
2000 $table->add_field('trained', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
2001 $table->add_field('target', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2002 $table->add_field('indicators', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2003 $table->add_field('timesplitting', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2004 $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2005 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
2006 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2007 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2009 // Adding keys to table analytics_models.
2010 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2012 // Adding indexes to table analytics_models.
2013 $table->add_index('enabledandtrained', XMLDB_INDEX_NOTUNIQUE, array('enabled', 'trained'));
2015 // Conditionally launch create table for analytics_models.
2016 if (!$dbman->table_exists($table)) {
2017 $dbman->create_table($table);
2020 // Define table analytics_models_log to be created.
2021 $table = new xmldb_table('analytics_models_log');
2023 // Adding fields to table analytics_models_log.
2024 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2025 $table->add_field('modelid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2026 $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2027 $table->add_field('target', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2028 $table->add_field('indicators', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2029 $table->add_field('timesplitting', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2030 $table->add_field('score', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, '0');
2031 $table->add_field('info', XMLDB_TYPE_TEXT, null, null, null, null, null);
2032 $table->add_field('dir', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2033 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2034 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2036 // Adding keys to table analytics_models_log.
2037 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2039 // Adding indexes to table analytics_models_log.
2040 $table->add_index('modelid', XMLDB_INDEX_NOTUNIQUE, array('modelid'));
2042 // Conditionally launch create table for analytics_models_log.
2043 if (!$dbman->table_exists($table)) {
2044 $dbman->create_table($table);
2047 // Define table analytics_predictions to be created.
2048 $table = new xmldb_table('analytics_predictions');
2050 // Adding fields to table analytics_predictions.
2051 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2052 $table->add_field('modelid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2053 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2054 $table->add_field('sampleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2055 $table->add_field('rangeindex', XMLDB_TYPE_INTEGER, '5', null, XMLDB_NOTNULL, null, null);
2056 $table->add_field('prediction', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, null);
2057 $table->add_field('predictionscore', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
2058 $table->add_field('calculations', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2059 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2061 // Adding keys to table analytics_predictions.
2062 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2064 // Adding indexes to table analytics_predictions.
2065 $table->add_index('modelidandcontextid', XMLDB_INDEX_NOTUNIQUE, array('modelid', 'contextid'));
2067 // Conditionally launch create table for analytics_predictions.
2068 if (!$dbman->table_exists($table)) {
2069 $dbman->create_table($table);
2072 // Define table analytics_train_samples to be created.
2073 $table = new xmldb_table('analytics_train_samples');
2075 // Adding fields to table analytics_train_samples.
2076 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2077 $table->add_field('modelid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2078 $table->add_field('analysableid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2079 $table->add_field('timesplitting', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2080 $table->add_field('fileid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2081 $table->add_field('sampleids', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2082 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2084 // Adding keys to table analytics_train_samples.
2085 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2087 // Adding indexes to table analytics_train_samples.
2088 $table->add_index('modelidandanalysableidandtimesplitting', XMLDB_INDEX_NOTUNIQUE,
2089 array('modelid', 'analysableid', 'timesplitting'));
2091 // Conditionally launch create table for analytics_train_samples.
2092 if (!$dbman->table_exists($table)) {
2093 $dbman->create_table($table);
2096 // Define table analytics_predict_ranges to be created.
2097 $table = new xmldb_table('analytics_predict_ranges');
2099 // Adding fields to table analytics_predict_ranges.
2100 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2101 $table->add_field('modelid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2102 $table->add_field('analysableid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2103 $table->add_field('timesplitting', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2104 $table->add_field('rangeindex', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2105 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2107 // Adding keys to table analytics_predict_ranges.
2108 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2110 // Adding indexes to table analytics_predict_ranges.
2111 $table->add_index('modelidandanalysableidandtimesplitting', XMLDB_INDEX_NOTUNIQUE,
2112 array('modelid', 'analysableid', 'timesplitting'));
2114 // Conditionally launch create table for analytics_predict_ranges.
2115 if (!$dbman->table_exists($table)) {
2116 $dbman->create_table($table);
2119 // Define table analytics_used_files to be created.
2120 $table = new xmldb_table('analytics_used_files');
2122 // Adding fields to table analytics_used_files.
2123 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2124 $table->add_field('modelid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2125 $table->add_field('fileid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2126 $table->add_field('action', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
2127 $table->add_field('time', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2129 // Adding keys to table analytics_used_files.
2130 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2132 // Adding indexes to table analytics_used_files.
2133 $table->add_index('modelidandfileidandaction', XMLDB_INDEX_NOTUNIQUE, array('modelid', 'fileid', 'action'));
2135 // Conditionally launch create table for analytics_used_files.
2136 if (!$dbman->table_exists($table)) {
2137 $dbman->create_table($table);
2140 $now = time();
2141 $admin = get_admin();
2143 $targetname = '\core\analytics\target\course_dropout';
2144 if (!$DB->record_exists('analytics_models', array('target' => $targetname))) {
2145 // We can not use API calls to create the built-in models.
2146 $modelobj = new stdClass();
2147 $modelobj->target = $targetname;
2148 $modelobj->indicators = json_encode(array(
2149 '\mod_assign\analytics\indicator\cognitive_depth',
2150 '\mod_assign\analytics\indicator\social_breadth',
2151 '\mod_book\analytics\indicator\cognitive_depth',
2152 '\mod_book\analytics\indicator\social_breadth',
2153 '\mod_chat\analytics\indicator\cognitive_depth',
2154 '\mod_chat\analytics\indicator\social_breadth',
2155 '\mod_choice\analytics\indicator\cognitive_depth',
2156 '\mod_choice\analytics\indicator\social_breadth',
2157 '\mod_data\analytics\indicator\cognitive_depth',
2158 '\mod_data\analytics\indicator\social_breadth',
2159 '\mod_feedback\analytics\indicator\cognitive_depth',
2160 '\mod_feedback\analytics\indicator\social_breadth',
2161 '\mod_folder\analytics\indicator\cognitive_depth',
2162 '\mod_folder\analytics\indicator\social_breadth',
2163 '\mod_forum\analytics\indicator\cognitive_depth',
2164 '\mod_forum\analytics\indicator\social_breadth',
2165 '\mod_glossary\analytics\indicator\cognitive_depth',
2166 '\mod_glossary\analytics\indicator\social_breadth',
2167 '\mod_imscp\analytics\indicator\cognitive_depth',
2168 '\mod_imscp\analytics\indicator\social_breadth',
2169 '\mod_label\analytics\indicator\cognitive_depth',
2170 '\mod_label\analytics\indicator\social_breadth',
2171 '\mod_lesson\analytics\indicator\cognitive_depth',
2172 '\mod_lesson\analytics\indicator\social_breadth',
2173 '\mod_lti\analytics\indicator\cognitive_depth',
2174 '\mod_lti\analytics\indicator\social_breadth',
2175 '\mod_page\analytics\indicator\cognitive_depth',
2176 '\mod_page\analytics\indicator\social_breadth',
2177 '\mod_quiz\analytics\indicator\cognitive_depth',
2178 '\mod_quiz\analytics\indicator\social_breadth',
2179 '\mod_resource\analytics\indicator\cognitive_depth',
2180 '\mod_resource\analytics\indicator\social_breadth',
2181 '\mod_scorm\analytics\indicator\cognitive_depth',
2182 '\mod_scorm\analytics\indicator\social_breadth',
2183 '\mod_survey\analytics\indicator\cognitive_depth',
2184 '\mod_survey\analytics\indicator\social_breadth',
2185 '\mod_url\analytics\indicator\cognitive_depth',
2186 '\mod_url\analytics\indicator\social_breadth',
2187 '\mod_wiki\analytics\indicator\cognitive_depth',
2188 '\mod_wiki\analytics\indicator\social_breadth',
2189 '\mod_workshop\analytics\indicator\cognitive_depth',
2190 '\mod_workshop\analytics\indicator\social_breadth',
2192 $modelobj->version = $now;
2193 $modelobj->timecreated = $now;
2194 $modelobj->timemodified = $now;
2195 $modelobj->usermodified = $admin->id;
2196 $DB->insert_record('analytics_models', $modelobj);
2199 $targetname = '\core\analytics\target\no_teaching';
2200 if (!$DB->record_exists('analytics_models', array('target' => $targetname))) {
2201 $modelobj = new stdClass();
2202 $modelobj->enabled = 1;
2203 $modelobj->trained = 1;
2204 $modelobj->target = $targetname;
2205 $modelobj->indicators = json_encode(array('\core_course\analytics\indicator\no_teacher'));
2206 $modelobj->timesplitting = '\core\analytics\time_splitting\single_range';
2207 $modelobj->version = $now;
2208 $modelobj->timecreated = $now;
2209 $modelobj->timemodified = $now;
2210 $modelobj->usermodified = $admin->id;
2211 $DB->insert_record('analytics_models', $modelobj);
2214 // Main savepoint reached.
2215 upgrade_main_savepoint(true, 2017072000.02);
2218 if ($oldversion < 2017072700.01) {
2219 // Changing nullability of field email on table oauth2_system_account to null.
2220 $table = new xmldb_table('oauth2_system_account');
2221 $field = new xmldb_field('email', XMLDB_TYPE_TEXT, null, null, null, null, null, 'grantedscopes');
2223 // Launch change of nullability for field email.
2224 $dbman->change_field_notnull($table, $field);
2226 // Main savepoint reached.
2227 upgrade_main_savepoint(true, 2017072700.01);
2230 if ($oldversion < 2017072700.02) {
2232 // If the site was previously registered with http://hub.moodle.org change the registration to
2233 // point to https://moodle.net - this is the correct hub address using https protocol.
2234 $oldhuburl = "http://hub.moodle.org";
2235 $newhuburl = "https://moodle.net";
2236 $cleanoldhuburl = preg_replace('/[^A-Za-z0-9_-]/i', '', $oldhuburl);
2237 $cleannewhuburl = preg_replace('/[^A-Za-z0-9_-]/i', '', $newhuburl);
2239 // Update existing registration.
2240 $DB->execute("UPDATE {registration_hubs} SET hubname = ?, huburl = ? WHERE huburl = ?",
2241 ['Moodle.net', $newhuburl, $oldhuburl]);
2243 // Update settings of existing registration.
2244 $sqlnamelike = $DB->sql_like('name', '?');
2245 $entries = $DB->get_records_sql("SELECT * FROM {config_plugins} where plugin=? and " . $sqlnamelike,
2246 ['hub', '%' . $DB->sql_like_escape('_' . $cleanoldhuburl)]);
2247 foreach ($entries as $entry) {
2248 $newname = substr($entry->name, 0, -strlen($cleanoldhuburl)) . $cleannewhuburl;
2249 try {
2250 $DB->update_record('config_plugins', ['id' => $entry->id, 'name' => $newname]);
2251 } catch (dml_exception $e) {
2252 // Entry with new name already exists, remove the one with an old name.
2253 $DB->delete_records('config_plugins', ['id' => $entry->id]);
2257 // Update published courses.
2258 $DB->execute('UPDATE {course_published} SET huburl = ? WHERE huburl = ?', [$newhuburl, $oldhuburl]);
2260 // Main savepoint reached.
2261 upgrade_main_savepoint(true, 2017072700.02);
2264 if ($oldversion < 2017080700.01) {
2266 // Get the table by its previous name.
2267 $table = new xmldb_table('analytics_predict_ranges');
2268 if ($dbman->table_exists($table)) {
2270 // We can only accept this because we are in master.
2271 $DB->delete_records('analytics_predictions');
2272 $DB->delete_records('analytics_used_files', array('action' => 'predicted'));
2273 $DB->delete_records('analytics_predict_ranges');
2275 // Define field sampleids to be added to analytics_predict_ranges (renamed below to analytics_predict_samples).
2276 $field = new xmldb_field('sampleids', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'rangeindex');
2278 // Conditionally launch add field sampleids.
2279 if (!$dbman->field_exists($table, $field)) {
2280 $dbman->add_field($table, $field);
2283 // Define field timemodified to be added to analytics_predict_ranges (renamed below to analytics_predict_samples).
2284 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'timecreated');
2286 // Conditionally launch add field timemodified.
2287 if (!$dbman->field_exists($table, $field)) {
2288 $dbman->add_field($table, $field);
2291 // Rename the table to its new name.
2292 $dbman->rename_table($table, 'analytics_predict_samples');
2295 $table = new xmldb_table('analytics_predict_samples');
2297 $index = new xmldb_index('modelidandanalysableidandtimesplitting', XMLDB_INDEX_NOTUNIQUE,
2298 array('modelid', 'analysableid', 'timesplitting'));
2300 // Conditionally launch drop index.
2301 if ($dbman->index_exists($table, $index)) {
2302 $dbman->drop_index($table, $index);
2305 $index = new xmldb_index('modelidandanalysableidandtimesplittingandrangeindex', XMLDB_INDEX_NOTUNIQUE,
2306 array('modelid', 'analysableid', 'timesplitting', 'rangeindex'));
2308 // Conditionally launch add index.
2309 if (!$dbman->index_exists($table, $index)) {
2310 $dbman->add_index($table, $index);
2313 // Main savepoint reached.
2314 upgrade_main_savepoint(true, 2017080700.01);
2317 if ($oldversion < 2017082200.00) {
2318 $plugins = ['radius', 'fc', 'nntp', 'pam', 'pop3', 'imap'];
2320 foreach ($plugins as $plugin) {
2321 // Check to see if the plugin exists on disk.
2322 // If it does not, remove the config for it.
2323 if (!file_exists($CFG->dirroot . "/auth/{$plugin}/auth.php")) {
2324 // Clean config.
2325 unset_all_config_for_plugin("auth_{$plugin}");
2328 upgrade_main_savepoint(true, 2017082200.00);
2331 if ($oldversion < 2017082200.01) {
2333 // Define table analytics_indicator_calc to be created.
2334 $table = new xmldb_table('analytics_indicator_calc');
2336 // Adding fields to table analytics_indicator_calc.
2337 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2338 $table->add_field('starttime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2339 $table->add_field('endtime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2340 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2341 $table->add_field('sampleorigin', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2342 $table->add_field('sampleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2343 $table->add_field('indicator', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2344 $table->add_field('value', XMLDB_TYPE_NUMBER, '10, 2', null, null, null, null);
2345 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2347 // Adding keys to table analytics_indicator_calc.
2348 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2350 // Adding indexes to table analytics_indicator_calc.
2351 $table->add_index('starttime-endtime-contextid', XMLDB_INDEX_NOTUNIQUE, array('starttime', 'endtime', 'contextid'));
2353 // Conditionally launch create table for analytics_indicator_calc.
2354 if (!$dbman->table_exists($table)) {
2355 $dbman->create_table($table);
2358 // Main savepoint reached.
2359 upgrade_main_savepoint(true, 2017082200.01);
2362 if ($oldversion < 2017082300.01) {
2364 // This script in included in each major version upgrade process so make sure we don't run it twice.
2365 if (empty($CFG->linkcoursesectionsupgradescriptwasrun)) {
2366 // Check if the site is using a boost-based theme.
2367 // If value of 'linkcoursesections' is set to the old default value, change it to the new default.
2368 if (upgrade_theme_is_from_family('boost', $CFG->theme)) {
2369 set_config('linkcoursesections', 1);
2371 set_config('linkcoursesectionsupgradescriptwasrun', 1);
2374 // Main savepoint reached.
2375 upgrade_main_savepoint(true, 2017082300.01);
2378 if ($oldversion < 2017082500.00) {
2379 // Handle FKs for the table 'analytics_models_log'.
2380 $table = new xmldb_table('analytics_models_log');
2382 // Remove the existing index before adding FK (which creates an index).
2383 $index = new xmldb_index('modelid', XMLDB_INDEX_NOTUNIQUE, array('modelid'));
2385 // Conditionally launch drop index.
2386 if ($dbman->index_exists($table, $index)) {
2387 $dbman->drop_index($table, $index);
2390 // Now, add the FK.
2391 $key = new xmldb_key('modelid', XMLDB_KEY_FOREIGN, array('modelid'), 'analytics_models', array('id'));
2392 $dbman->add_key($table, $key);
2394 // Handle FKs for the table 'analytics_predictions'.
2395 $table = new xmldb_table('analytics_predictions');
2396 $key = new xmldb_key('modelid', XMLDB_KEY_FOREIGN, array('modelid'), 'analytics_models', array('id'));
2397 $dbman->add_key($table, $key);
2399 $key = new xmldb_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2400 $dbman->add_key($table, $key);
2402 // Handle FKs for the table 'analytics_train_samples'.
2403 $table = new xmldb_table('analytics_train_samples');
2404 $key = new xmldb_key('modelid', XMLDB_KEY_FOREIGN, array('modelid'), 'analytics_models', array('id'));
2405 $dbman->add_key($table, $key);
2407 $key = new xmldb_key('fileid', XMLDB_KEY_FOREIGN, array('fileid'), 'files', array('id'));
2408 $dbman->add_key($table, $key);
2410 // Handle FKs for the table 'analytics_predict_samples'.
2411 $table = new xmldb_table('analytics_predict_samples');
2412 $key = new xmldb_key('modelid', XMLDB_KEY_FOREIGN, array('modelid'), 'analytics_models', array('id'));
2413 $dbman->add_key($table, $key);
2415 // Handle FKs for the table 'analytics_used_files'.
2416 $table = new xmldb_table('analytics_used_files');
2417 $key = new xmldb_key('modelid', XMLDB_KEY_FOREIGN, array('modelid'), 'analytics_models', array('id'));
2418 $dbman->add_key($table, $key);
2420 $key = new xmldb_key('fileid', XMLDB_KEY_FOREIGN, array('fileid'), 'files', array('id'));
2421 $dbman->add_key($table, $key);
2423 // Handle FKs for the table 'analytics_indicator_calc'.
2424 $table = new xmldb_table('analytics_indicator_calc');
2425 $key = new xmldb_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2426 $dbman->add_key($table, $key);
2428 // Main savepoint reached.
2429 upgrade_main_savepoint(true, 2017082500.00);
2432 if ($oldversion < 2017082800.00) {
2434 // Changing type of field prediction on table analytics_predictions to number.
2435 $table = new xmldb_table('analytics_predictions');
2436 $field = new xmldb_field('prediction', XMLDB_TYPE_NUMBER, '10, 2', null, XMLDB_NOTNULL, null, null, 'rangeindex');
2438 // Launch change of type for field prediction.
2439 $dbman->change_field_type($table, $field);
2441 // Main savepoint reached.
2442 upgrade_main_savepoint(true, 2017082800.00);
2445 if ($oldversion < 2017090700.01) {
2447 // Define table analytics_prediction_actions to be created.
2448 $table = new xmldb_table('analytics_prediction_actions');
2450 // Adding fields to table analytics_prediction_actions.
2451 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2452 $table->add_field('predictionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2453 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2454 $table->add_field('actionname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2455 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2457 // Adding keys to table analytics_prediction_actions.
2458 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2459 $table->add_key('predictionid', XMLDB_KEY_FOREIGN, array('predictionid'), 'analytics_predictions', array('id'));
2460 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2462 // Adding indexes to table analytics_prediction_actions.
2463 $table->add_index('predictionidanduseridandactionname', XMLDB_INDEX_NOTUNIQUE,
2464 array('predictionid', 'userid', 'actionname'));
2466 // Conditionally launch create table for analytics_prediction_actions.
2467 if (!$dbman->table_exists($table)) {
2468 $dbman->create_table($table);
2471 // Main savepoint reached.
2472 upgrade_main_savepoint(true, 2017090700.01);
2475 if ($oldversion < 2017091200.00) {
2476 // Force all messages to be reindexed.
2477 set_config('core_message_message_sent_lastindexrun', '0', 'core_search');
2478 set_config('core_message_message_received_lastindexrun', '0', 'core_search');
2480 // Main savepoint reached.
2481 upgrade_main_savepoint(true, 2017091200.00);
2484 if ($oldversion < 2017091201.00) {
2485 // Define field userid to be added to task_adhoc.
2486 $table = new xmldb_table('task_adhoc');
2487 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customdata');
2489 // Conditionally launch add field userid.
2490 if (!$dbman->field_exists($table, $field)) {
2491 $dbman->add_field($table, $field);
2494 $key = new xmldb_key('useriduser', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2496 // Launch add key userid_user.
2497 $dbman->add_key($table, $key);
2499 // Main savepoint reached.
2500 upgrade_main_savepoint(true, 2017091201.00);
2503 if ($oldversion < 2017092201.00) {
2505 // Remove duplicate registrations.
2506 $newhuburl = "https://moodle.net";
2507 $registrations = $DB->get_records('registration_hubs', ['huburl' => $newhuburl], 'confirmed DESC, id ASC');
2508 if (count($registrations) > 1) {
2509 $reg = array_shift($registrations);
2510 $DB->delete_records_select('registration_hubs', 'huburl = ? AND id <> ?', [$newhuburl, $reg->id]);
2513 // Main savepoint reached.
2514 upgrade_main_savepoint(true, 2017092201.00);
2517 if ($oldversion < 2017092202.00) {
2519 if (!file_exists($CFG->dirroot . '/blocks/messages/block_messages.php')) {
2521 // Delete instances.
2522 $instances = $DB->get_records_list('block_instances', 'blockname', ['messages']);
2523 $instanceids = array_keys($instances);
2525 if (!empty($instanceids)) {
2526 $DB->delete_records_list('block_positions', 'blockinstanceid', $instanceids);
2527 $DB->delete_records_list('block_instances', 'id', $instanceids);
2528 list($sql, $params) = $DB->get_in_or_equal($instanceids, SQL_PARAMS_NAMED);
2529 $params['contextlevel'] = CONTEXT_BLOCK;
2530 $DB->delete_records_select('context', "contextlevel=:contextlevel AND instanceid " . $sql, $params);
2532 $preferences = array();
2533 foreach ($instances as $instanceid => $instance) {
2534 $preferences[] = 'block' . $instanceid . 'hidden';
2535 $preferences[] = 'docked_block_instance_' . $instanceid;
2537 $DB->delete_records_list('user_preferences', 'name', $preferences);
2540 // Delete the block from the block table.
2541 $DB->delete_records('block', array('name' => 'messages'));
2543 // Remove capabilities.
2544 capabilities_cleanup('block_messages');
2546 // Clean config.
2547 unset_all_config_for_plugin('block_messages');
2550 // Main savepoint reached.
2551 upgrade_main_savepoint(true, 2017092202.00);
2554 if ($oldversion < 2017092700.00) {
2556 // Rename several fields in registration data to match the names of the properties that are sent to moodle.net.
2557 $renames = [
2558 'site_address_httpsmoodlenet' => 'site_street_httpsmoodlenet',
2559 'site_region_httpsmoodlenet' => 'site_regioncode_httpsmoodlenet',
2560 'site_country_httpsmoodlenet' => 'site_countrycode_httpsmoodlenet'];
2561 foreach ($renames as $oldparamname => $newparamname) {
2562 try {
2563 $DB->execute("UPDATE {config_plugins} SET name = ? WHERE name = ? AND plugin = ?",
2564 [$newparamname, $oldparamname, 'hub']);
2565 } catch (dml_exception $e) {
2566 // Exception can happen if the config value with the new name already exists, ignore it and move on.
2570 // Main savepoint reached.
2571 upgrade_main_savepoint(true, 2017092700.00);
2574 if ($oldversion < 2017092900.00) {
2575 // Define field categoryid to be added to event.
2576 $table = new xmldb_table('event');
2577 $field = new xmldb_field('categoryid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'format');
2579 // Conditionally launch add field categoryid.
2580 if (!$dbman->field_exists($table, $field)) {
2581 $dbman->add_field($table, $field);
2584 // Add the categoryid key.
2585 $key = new xmldb_key('categoryid', XMLDB_KEY_FOREIGN, array('categoryid'), 'course_categories', array('id'));
2586 $dbman->add_key($table, $key);
2588 // Add a new index for groupid/courseid/categoryid/visible/userid.
2589 // Do this before we remove the old index.
2590 $index = new xmldb_index('groupid-courseid-categoryid-visible-userid', XMLDB_INDEX_NOTUNIQUE, array('groupid', 'courseid', 'categoryid', 'visible', 'userid'));
2591 if (!$dbman->index_exists($table, $index)) {
2592 $dbman->add_index($table, $index);
2595 // Drop the old index.
2596 $index = new xmldb_index('groupid-courseid-visible-userid', XMLDB_INDEX_NOTUNIQUE, array('groupid', 'courseid', 'visible', 'userid'));
2597 if ($dbman->index_exists($table, $index)) {
2598 $dbman->drop_index($table, $index);
2601 // Main savepoint reached.
2602 upgrade_main_savepoint(true, 2017092900.00);
2605 if ($oldversion < 2017100900.00) {
2606 // Add index on time modified to grade_outcomes_history, grade_categories_history,
2607 // grade_items_history, and scale_history.
2608 $table = new xmldb_table('grade_outcomes_history');
2609 $index = new xmldb_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
2611 if (!$dbman->index_exists($table, $index)) {
2612 $dbman->add_index($table, $index);
2615 $table = new xmldb_table('grade_items_history');
2616 $index = new xmldb_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
2618 if (!$dbman->index_exists($table, $index)) {
2619 $dbman->add_index($table, $index);
2622 $table = new xmldb_table('grade_categories_history');
2623 $index = new xmldb_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
2625 if (!$dbman->index_exists($table, $index)) {
2626 $dbman->add_index($table, $index);
2629 $table = new xmldb_table('scale_history');
2630 $index = new xmldb_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
2632 if (!$dbman->index_exists($table, $index)) {
2633 $dbman->add_index($table, $index);
2636 // Main savepoint reached.
2637 upgrade_main_savepoint(true, 2017100900.00);
2640 if ($oldversion < 2017101000.00) {
2642 // Define table analytics_used_analysables to be created.
2643 $table = new xmldb_table('analytics_used_analysables');
2645 // Adding fields to table analytics_used_analysables.
2646 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2647 $table->add_field('modelid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2648 $table->add_field('action', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
2649 $table->add_field('analysableid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2650 $table->add_field('timeanalysed', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2652 // Adding keys to table analytics_used_analysables.
2653 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2654 $table->add_key('modelid', XMLDB_KEY_FOREIGN, array('modelid'), 'analytics_models', array('id'));
2656 // Adding indexes to table analytics_used_analysables.
2657 $table->add_index('modelid-action', XMLDB_INDEX_NOTUNIQUE, array('modelid', 'action'));
2659 // Conditionally launch create table for analytics_used_analysables.
2660 if (!$dbman->table_exists($table)) {
2661 $dbman->create_table($table);
2664 // Main savepoint reached.
2665 upgrade_main_savepoint(true, 2017101000.00);
2668 if ($oldversion < 2017101000.01) {
2669 // Define field override to be added to course_modules_completion.
2670 $table = new xmldb_table('course_modules_completion');
2671 $field = new xmldb_field('overrideby', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'viewed');
2673 // Conditionally launch add field override.
2674 if (!$dbman->field_exists($table, $field)) {
2675 $dbman->add_field($table, $field);
2678 // Main savepoint reached.
2679 upgrade_main_savepoint(true, 2017101000.01);
2682 if ($oldversion < 2017101000.02) {
2683 // Define field 'timestart' to be added to 'analytics_predictions'.
2684 $table = new xmldb_table('analytics_predictions');
2685 $field = new xmldb_field('timestart', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'timecreated');
2687 // Conditionally launch add field 'timestart'.
2688 if (!$dbman->field_exists($table, $field)) {
2689 $dbman->add_field($table, $field);
2692 // Define field 'timeend' to be added to 'analytics_predictions'.
2693 $field = new xmldb_field('timeend', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'timestart');
2695 // Conditionally launch add field 'timeend'.
2696 if (!$dbman->field_exists($table, $field)) {
2697 $dbman->add_field($table, $field);
2700 // Main savepoint reached.
2701 upgrade_main_savepoint(true, 2017101000.02);
2704 if ($oldversion < 2017101200.00) {
2705 // Define table search_index_requests to be created.
2706 $table = new xmldb_table('search_index_requests');
2708 // Adding fields to table search_index_requests.
2709 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2710 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2711 $table->add_field('searcharea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2712 $table->add_field('timerequested', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2713 $table->add_field('partialarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2714 $table->add_field('partialtime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2716 // Adding keys to table search_index_requests.
2717 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2718 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2720 // Conditionally launch create table for search_index_requests.
2721 if (!$dbman->table_exists($table)) {
2722 $dbman->create_table($table);
2725 // Main savepoint reached.
2726 upgrade_main_savepoint(true, 2017101200.00);
2729 // Index modification upgrade step.
2730 if ($oldversion < 2017101300.01) {
2732 $table = new xmldb_table('analytics_used_files');
2734 // Define index modelidandfileidandaction (not unique) to be dropped form analytics_used_files.
2735 $index = new xmldb_index('modelidandfileidandaction', XMLDB_INDEX_NOTUNIQUE, array('modelid', 'fileid', 'action'));
2737 // Conditionally launch drop index modelidandfileidandaction.
2738 if ($dbman->index_exists($table, $index)) {
2739 $dbman->drop_index($table, $index);
2742 // Define index modelidandactionandfileid (not unique) to be dropped form analytics_used_files.
2743 $index = new xmldb_index('modelidandactionandfileid', XMLDB_INDEX_NOTUNIQUE, array('modelid', 'action', 'fileid'));
2745 // Conditionally launch add index modelidandactionandfileid.
2746 if (!$dbman->index_exists($table, $index)) {
2747 $dbman->add_index($table, $index);
2750 // Main savepoint reached.
2751 upgrade_main_savepoint(true, 2017101300.01);
2754 if ($oldversion < 2017101900.01) {
2756 $fs = get_file_storage();
2757 $models = $DB->get_records('analytics_models');
2758 foreach ($models as $model) {
2759 $files = $fs->get_directory_files(\context_system::instance()->id, 'analytics', 'unlabelled', $model->id,
2760 '/analysable/', true, true);
2761 foreach ($files as $file) {
2762 $file->delete();
2766 // Main savepoint reached.
2767 upgrade_main_savepoint(true, 2017101900.01);
2770 if ($oldversion < 2017101900.02) {
2771 // Create adhoc task for upgrading of existing calendar events.
2772 $record = new \stdClass();
2773 $record->classname = '\core\task\refresh_mod_calendar_events_task';
2774 $record->component = 'core';
2776 // Next run time based from nextruntime computation in \core\task\manager::queue_adhoc_task().
2777 $nextruntime = time() - 1;
2778 $record->nextruntime = $nextruntime;
2779 $DB->insert_record('task_adhoc', $record);
2781 // Main savepoint reached.
2782 upgrade_main_savepoint(true, 2017101900.02);
2785 if ($oldversion < 2017102100.01) {
2786 // We will need to force them onto ssl if loginhttps is set.
2787 if (!empty($CFG->loginhttps)) {
2788 set_config('overridetossl', 1);
2790 // Loginhttps should no longer be set.
2791 unset_config('loginhttps');
2793 // Main savepoint reached.
2794 upgrade_main_savepoint(true, 2017102100.01);
2797 if ($oldversion < 2017110300.01) {
2799 // Define field categoryid to be added to event_subscriptions.
2800 $table = new xmldb_table('event_subscriptions');
2801 $field = new xmldb_field('categoryid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'url');
2803 // Conditionally launch add field categoryid.
2804 if (!$dbman->field_exists($table, $field)) {
2805 $dbman->add_field($table, $field);
2808 // Main savepoint reached.
2809 upgrade_main_savepoint(true, 2017110300.01);
2812 // Automatically generated Moodle v3.4.0 release upgrade line.
2813 // Put any upgrade step following this.
2815 if ($oldversion < 2017111300.02) {
2817 // Define field basicauth to be added to oauth2_issuer.
2818 $table = new xmldb_table('oauth2_issuer');
2819 $field = new xmldb_field('basicauth', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'showonloginpage');
2821 // Conditionally launch add field basicauth.
2822 if (!$dbman->field_exists($table, $field)) {
2823 $dbman->add_field($table, $field);
2826 // Main savepoint reached.
2827 upgrade_main_savepoint(true, 2017111300.02);
2830 if ($oldversion < 2017111301.08) {
2832 // Fix old block configurations that use the deprecated (and now removed) object class.
2833 upgrade_fix_block_instance_configuration();
2835 // Main savepoint reached.
2836 upgrade_main_savepoint(true, 2017111301.08);
2839 if ($oldversion < 2017111302.12) {
2840 // Update default digital age consent map according to the current legislation on each country.
2841 $ageofdigitalconsentmap = implode(PHP_EOL, [
2842 '*, 16',
2843 'AT, 14',
2844 'ES, 14',
2845 'US, 13'
2847 set_config('agedigitalconsentmap', $ageofdigitalconsentmap);
2849 // Main savepoint reached.
2850 upgrade_main_savepoint(true, 2017111302.12);
2853 return true;