Merge branch 'wip-MDL-60167-master' of git://github.com/marinaglancy/moodle
[moodle.git] / lib / db / upgrade.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * This file keeps track of upgrades to Moodle.
19  *
20  * Sometimes, changes between versions involve
21  * alterations to database structures and other
22  * major things that may break installations.
23  *
24  * The upgrade function in this file will attempt
25  * to perform all the necessary actions to upgrade
26  * your older installation to the current version.
27  *
28  * If there's something it cannot do itself, it
29  * will tell you what you need to do.
30  *
31  * The commands in here will all be database-neutral,
32  * using the methods of database_manager class
33  *
34  * Please do not forget to use upgrade_set_timeout()
35  * before any action that may take longer time to finish.
36  *
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
41  */
43 defined('MOODLE_INTERNAL') || die();
45 /**
46  * Main upgrade tasks to be executed on Moodle version bump
47  *
48  * This function is automatically executed after one bump in the Moodle core
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.
51  *
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.
56  *
57  * Each upgrade step has a fixed structure, that can be summarised as follows:
58  *
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  * }
66  *
67  * All plugins within Moodle (modules, blocks, reports...) support the existence of
68  * their own upgrade.php file, using the "Frankenstyle" component name as
69  * defined at {@link http://docs.moodle.org/dev/Frankenstyle}, for example:
70  *     - {@link xmldb_page_upgrade($oldversion)}. (modules don't require the plugintype ("mod_") to be used.
71  *     - {@link xmldb_auth_manual_upgrade($oldversion)}.
72  *     - {@link xmldb_workshopform_accumulative_upgrade($oldversion)}.
73  *     - ....
74  *
75  * In order to keep the contents of this file reduced, it's allowed to create some helper
76  * functions to be used here in the {@link upgradelib.php} file at the same directory. Note
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.
79  *
80  * For more information, take a look to the documentation available:
81  *     - Data definition API: {@link http://docs.moodle.org/dev/Data_definition_API}
82  *     - Upgrade API: {@link http://docs.moodle.org/dev/Upgrade_API}
83  *
84  * @param int $oldversion
85  * @return bool always true
86  */
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);
102     }
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);
127         }
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);
137         }
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);
146         }
148         // Main savepoint reached.
149         upgrade_main_savepoint(true, 2016011300.01);
150     }
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));
157         }
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);
170         }
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);
181     }
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);
207         }
209         // Main savepoint reached.
210         upgrade_main_savepoint(true, 2016011300.03);
211     }
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);
223         }
225         // Main savepoint reached.
226         upgrade_main_savepoint(true, 2016011300.04);
227     }
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);
249     }
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);
260         }
262         // Main savepoint reached.
263         upgrade_main_savepoint(true, 2016011300.06);
264     }
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);
275         }
277         // Main savepoint reached.
278         upgrade_main_savepoint(true, 2016011300.07);
279     }
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');
289         }
290         upgrade_main_savepoint(true, 2016011301.00);
291     }
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;
322             }
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.");
329         }
330         $rs->close();
331         $transaction->allow_commit();
333         upgrade_main_savepoint(true, 2016011901.00);
334     }
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);
345         }
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);
354         }
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);
362         }
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);
371         }
373         // Main savepoint reached.
374         upgrade_main_savepoint(true, 2016020200.00);
375     }
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);
386         }
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);
401     }
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);
413                 }
414             }
415         }
417         // Main savepoint reached.
418         upgrade_main_savepoint(true, 2016021500.00);
419     }
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);
432         }
434         // Main savepoint reached.
435         upgrade_main_savepoint(true, 2016021501.00);
436     }
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', '');
448         }
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');
454         }
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');
459         }
460         if (isset($CFG->quarantinedir)) {
461             set_config('quarantinedir', $CFG->quarantinedir, 'antivirus_clamav');
462             unset_config('quarantinedir');
463         }
464         if (isset($CFG->clamfailureonupload)) {
465             set_config('clamfailureonupload', $CFG->clamfailureonupload, 'antivirus_clamav');
466             unset_config('clamfailureonupload');
467         }
469         // Main savepoint reached.
470         upgrade_main_savepoint(true, 2016030103.00);
471     }
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);
481         }
482         // Main savepoint reached.
483         upgrade_main_savepoint(true, 2016030400.01);
484     }
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);
520         }
522         // Main savepoint reached.
523         upgrade_main_savepoint(true, 2016041500.50);
524     }
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);
546         }
548         // Main savepoint reached.
549         upgrade_main_savepoint(true, 2016041500.51);
550     }
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);
581         }
583         // Main savepoint reached.
584         upgrade_main_savepoint(true, 2016041500.52);
585     }
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);
614         }
616         // Main savepoint reached.
617         upgrade_main_savepoint(true, 2016041500.53);
618     }
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);
651         }
653         // Main savepoint reached.
654         upgrade_main_savepoint(true, 2016041500.54);
655     }
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);
680         }
682         // Main savepoint reached.
683         upgrade_main_savepoint(true, 2016041500.55);
684     }
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);
708         }
710         // Main savepoint reached.
711         upgrade_main_savepoint(true, 2016041500.56);
712     }
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);
737         }
739         // Main savepoint reached.
740         upgrade_main_savepoint(true, 2016041500.57);
741     }
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);
762         }
764         // Main savepoint reached.
765         upgrade_main_savepoint(true, 2016041500.58);
766     }
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);
794         }
796         // Main savepoint reached.
797         upgrade_main_savepoint(true, 2016041500.59);
798     }
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);
825         }
827         // Main savepoint reached.
828         upgrade_main_savepoint(true, 2016041500.60);
829     }
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);
857         }
859         // Main savepoint reached.
860         upgrade_main_savepoint(true, 2016041500.61);
861     }
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);
886         }
888         // Main savepoint reached.
889         upgrade_main_savepoint(true, 2016041500.62);
890     }
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);
922         }
924         // Main savepoint reached.
925         upgrade_main_savepoint(true, 2016041500.63);
926     }
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);
953         }
955         // Main savepoint reached.
956         upgrade_main_savepoint(true, 2016041500.64);
957     }
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);
982         }
984         // Main savepoint reached.
985         upgrade_main_savepoint(true, 2016041500.65);
986     }
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);
1015         }
1017         // Main savepoint reached.
1018         upgrade_main_savepoint(true, 2016041500.66);
1019     }
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);
1026     }
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'
1058         ];
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);
1065     }
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);
1073     }
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);
1086         }
1087         // Main savepoint reached.
1088         upgrade_main_savepoint(true, 2016051700.01);
1089     }
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');
1099         }
1101         // Savepoint reached.
1102         upgrade_main_savepoint(true, 2016081700.00);
1103     }
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;
1113         }
1114         if (isset($CFG->statsruntimestartminute)) {
1115             $minute = $CFG->statsruntimestartminute;
1116         }
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();
1131             }
1132             $stattask->nextruntime = $nextruntime;
1133             $stattask->minute = $minute;
1134             $stattask->hour = $hour;
1135             $stattask->customised = 1;
1136             $DB->update_record('task_scheduled', $stattask);
1137         }
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);
1144     }
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.");
1185         }
1186         unset($usedstamps);
1188         // The uniqueness of each (contextid, stamp) pair is now guaranteed, so add the unique index to stop future duplicates.
1189         $table = new xmldb_table('question_categories');
1190         $index = new xmldb_index('contextidstamp', XMLDB_INDEX_UNIQUE, array('contextid', 'stamp'));
1191         if (!$dbman->index_exists($table, $index)) {
1192             $dbman->add_index($table, $index);
1193         }
1195         // Savepoint reached.
1196         upgrade_main_savepoint(true, 2016082200.00);
1197     }
1199     if ($oldversion < 2016091900.00) {
1201         // Removing the themes from core.
1202         $themes = array('base', 'canvas');
1204         foreach ($themes as $key => $theme) {
1205             if (check_dir_exists($CFG->dirroot . '/theme/' . $theme, false)) {
1206                 // Ignore the themes that have been re-downloaded.
1207                 unset($themes[$key]);
1208             }
1209         }
1211         if (!empty($themes)) {
1212             // Hacky emulation of plugin uninstallation.
1213             foreach ($themes as $theme) {
1214                 unset_all_config_for_plugin('theme_' . $theme);
1215             }
1216         }
1218         // Main savepoint reached.
1219         upgrade_main_savepoint(true, 2016091900.00);
1220     }
1222     if ($oldversion < 2016091900.02) {
1224         // Define index attemptstepid-name (unique) to be dropped from question_attempt_step_data.
1225         $table = new xmldb_table('question_attempt_step_data');
1226         $index = new xmldb_index('attemptstepid-name', XMLDB_INDEX_UNIQUE, array('attemptstepid', 'name'));
1228         // Conditionally launch drop index attemptstepid-name.
1229         if ($dbman->index_exists($table, $index)) {
1230             $dbman->drop_index($table, $index);
1231         }
1233         // Main savepoint reached.
1234         upgrade_main_savepoint(true, 2016091900.02);
1235     }
1237     if ($oldversion < 2016100300.00) {
1238         unset_config('enablecssoptimiser');
1240         upgrade_main_savepoint(true, 2016100300.00);
1241     }
1243     if ($oldversion < 2016100501.00) {
1245         // Define field enddate to be added to course.
1246         $table = new xmldb_table('course');
1247         $field = new xmldb_field('enddate', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'startdate');
1249         // Conditionally launch add field enddate.
1250         if (!$dbman->field_exists($table, $field)) {
1251             $dbman->add_field($table, $field);
1252         }
1254         // Main savepoint reached.
1255         upgrade_main_savepoint(true, 2016100501.00);
1256     }
1258     if ($oldversion < 2016101100.00) {
1259         // Define field component to be added to message.
1260         $table = new xmldb_table('message');
1261         $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'timeusertodeleted');
1263         // Conditionally launch add field component.
1264         if (!$dbman->field_exists($table, $field)) {
1265             $dbman->add_field($table, $field);
1266         }
1268         // Define field eventtype to be added to message.
1269         $field = new xmldb_field('eventtype', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'component');
1271         // Conditionally launch add field eventtype.
1272         if (!$dbman->field_exists($table, $field)) {
1273             $dbman->add_field($table, $field);
1274         }
1276         // Main savepoint reached.
1277         upgrade_main_savepoint(true, 2016101100.00);
1278     }
1281     if ($oldversion < 2016101101.00) {
1282         // Define field component to be added to message_read.
1283         $table = new xmldb_table('message_read');
1284         $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'timeusertodeleted');
1286         // Conditionally launch add field component.
1287         if (!$dbman->field_exists($table, $field)) {
1288             $dbman->add_field($table, $field);
1289         }
1291         // Define field eventtype to be added to message_read.
1292         $field = new xmldb_field('eventtype', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'component');
1294         // Conditionally launch add field eventtype.
1295         if (!$dbman->field_exists($table, $field)) {
1296             $dbman->add_field($table, $field);
1297         }
1299         // Main savepoint reached.
1300         upgrade_main_savepoint(true, 2016101101.00);
1301     }
1303     if ($oldversion < 2016101401.00) {
1304         // Clean up repository_alfresco config unless plugin has been manually installed.
1305         if (!file_exists($CFG->dirroot . '/repository/alfresco/lib.php')) {
1306             // Remove capabilities.
1307             capabilities_cleanup('repository_alfresco');
1308             // Clean config.
1309             unset_all_config_for_plugin('repository_alfresco');
1310         }
1312         // Savepoint reached.
1313         upgrade_main_savepoint(true, 2016101401.00);
1314     }
1316     if ($oldversion < 2016101401.02) {
1317         $table = new xmldb_table('external_tokens');
1318         $field = new xmldb_field('privatetoken', XMLDB_TYPE_CHAR, '64', null, null, null, null);
1320         // Conditionally add privatetoken field to the external_tokens table.
1321         if (!$dbman->field_exists($table, $field)) {
1322             $dbman->add_field($table, $field);
1323         }
1325         // Main savepoint reached.
1326         upgrade_main_savepoint(true, 2016101401.02);
1327     }
1329     if ($oldversion < 2016110202.00) {
1331         // Force uninstall of deleted authentication plugin.
1332         if (!file_exists("$CFG->dirroot/auth/radius")) {
1333             // Leave settings inplace if there are radius users.
1334             if (!$DB->record_exists('user', array('auth' => 'radius', 'deleted' => 0))) {
1335                 // Remove all other associated config.
1336                 unset_all_config_for_plugin('auth/radius');
1337                 // The version number for radius is in this format.
1338                 unset_all_config_for_plugin('auth_radius');
1339             }
1340         }
1341         upgrade_main_savepoint(true, 2016110202.00);
1342     }
1344     if ($oldversion < 2016110300.00) {
1345         // Remove unused admin email setting.
1346         unset_config('emailonlyfromreplyaddress');
1348         // Main savepoint reached.
1349         upgrade_main_savepoint(true, 2016110300.00);
1350     }
1352     if ($oldversion < 2016110500.00) {
1354         $oldplayers = [
1355             'vimeo' => null,
1356             'mp3' => ['.mp3'],
1357             'html5video' => ['.mov', '.mp4', '.m4v', '.mpeg', '.mpe', '.mpg', '.ogv', '.webm'],
1358             'flv' => ['.flv', '.f4v'],
1359             'html5audio' => ['.aac', '.flac', '.mp3', '.m4a', '.oga', '.ogg', '.wav'],
1360             'youtube' => null,
1361             'swf' => null,
1362         ];
1364         // Convert hardcoded media players to the settings of the new media player plugin type.
1365         if (get_config('core', 'media_plugins_sortorder') === false) {
1366             $enabledplugins = [];
1367             $videoextensions = [];
1368             $audioextensions = [];
1369             foreach ($oldplayers as $oldplayer => $extensions) {
1370                 $settingname = 'core_media_enable_'.$oldplayer;
1371                 if (!empty($CFG->$settingname)) {
1372                     if ($extensions) {
1373                         // VideoJS will be used for all media files players that were used previously.
1374                         $enabledplugins['videojs'] = 'videojs';
1375                         if ($oldplayer === 'mp3' || $oldplayer === 'html5audio') {
1376                             $audioextensions += array_combine($extensions, $extensions);
1377                         } else {
1378                             $videoextensions += array_combine($extensions, $extensions);
1379                         }
1380                     } else {
1381                         // Enable youtube, vimeo and swf.
1382                         $enabledplugins[$oldplayer] = $oldplayer;
1383                     }
1384                 }
1385             }
1387             set_config('media_plugins_sortorder', join(',', $enabledplugins));
1389             // Configure VideoJS to match the existing players set up.
1390             if ($enabledplugins['videojs']) {
1391                 $enabledplugins[] = 'videojs';
1392                 set_config('audioextensions', join(',', $audioextensions), 'media_videojs');
1393                 set_config('videoextensions', join(',', $videoextensions), 'media_videojs');
1394                 $useflash = !empty($CFG->core_media_enable_flv) || !empty($CFG->core_media_enable_mp3);
1395                 set_config('useflash', $useflash, 'media_videojs');
1396                 if (empty($CFG->core_media_enable_youtube)) {
1397                     // Normally YouTube is enabled in videojs, but if youtube converter was disabled before upgrade
1398                     // disable it in videojs as well.
1399                     set_config('youtube', false, 'media_videojs');
1400                 }
1401             }
1402         }
1404         // Unset old settings.
1405         foreach ($oldplayers as $oldplayer => $extensions) {
1406             unset_config('core_media_enable_' . $oldplayer);
1407         }
1409         // Preset defaults if CORE_MEDIA_VIDEO_WIDTH and CORE_MEDIA_VIDEO_HEIGHT are specified in config.php .
1410         // After this upgrade step these constants will not be used any more.
1411         if (defined('CORE_MEDIA_VIDEO_WIDTH')) {
1412             set_config('media_default_width', CORE_MEDIA_VIDEO_WIDTH);
1413         }
1414         if (defined('CORE_MEDIA_VIDEO_HEIGHT')) {
1415             set_config('media_default_height', CORE_MEDIA_VIDEO_HEIGHT);
1416         }
1418         // Savepoint reached.
1419         upgrade_main_savepoint(true, 2016110500.00);
1420     }
1422     if ($oldversion < 2016110600.00) {
1423         // Define a field 'deletioninprogress' in the 'course_modules' table, to background deletion tasks.
1424         $table = new xmldb_table('course_modules');
1425         $field = new xmldb_field('deletioninprogress', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'availability');
1427         // Conditionally launch add field 'deletioninprogress'.
1428         if (!$dbman->field_exists($table, $field)) {
1429             $dbman->add_field($table, $field);
1430         }
1432         // Main savepoint reached.
1433         upgrade_main_savepoint(true, 2016110600.00);
1434     }
1436     if ($oldversion < 2016112200.01) {
1438         // Define field requiredbytheme to be added to block_instances.
1439         $table = new xmldb_table('block_instances');
1440         $field = new xmldb_field('requiredbytheme', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'showinsubcontexts');
1442         // Conditionally launch add field requiredbytheme.
1443         if (!$dbman->field_exists($table, $field)) {
1444             $dbman->add_field($table, $field);
1445         }
1447         // Main savepoint reached.
1448         upgrade_main_savepoint(true, 2016112200.01);
1449     }
1450     if ($oldversion < 2016112200.02) {
1452         // Change the existing site level admin and settings blocks to be requiredbytheme which means they won't show in boost.
1453         $context = context_system::instance();
1454         $params = array('blockname' => 'settings', 'parentcontextid' => $context->id);
1455         $DB->set_field('block_instances', 'requiredbytheme', 1, $params);
1457         $params = array('blockname' => 'navigation', 'parentcontextid' => $context->id);
1458         $DB->set_field('block_instances', 'requiredbytheme', 1, $params);
1459         // Main savepoint reached.
1460         upgrade_main_savepoint(true, 2016112200.02);
1461     }
1463     // Automatically generated Moodle v3.2.0 release upgrade line.
1464     // Put any upgrade step following this.
1466     if ($oldversion < 2016122800.00) {
1467         // Find all roles with the coursecreator archetype.
1468         $coursecreatorroleids = $DB->get_records('role', array('archetype' => 'coursecreator'), '', 'id');
1470         $context = context_system::instance();
1471         $capability = 'moodle/site:configview';
1473         foreach ($coursecreatorroleids as $roleid => $notused) {
1475             // Check that the capability has not already been assigned. If it has then it's either already set
1476             // to allow or specifically set to prohibit or prevent.
1477             if (!$DB->record_exists('role_capabilities', array('roleid' => $roleid, 'capability' => $capability))) {
1478                 // Assign the capability.
1479                 $cap = new stdClass();
1480                 $cap->contextid    = $context->id;
1481                 $cap->roleid       = $roleid;
1482                 $cap->capability   = $capability;
1483                 $cap->permission   = CAP_ALLOW;
1484                 $cap->timemodified = time();
1485                 $cap->modifierid   = 0;
1487                 $DB->insert_record('role_capabilities', $cap);
1488             }
1489         }
1491         // Main savepoint reached.
1492         upgrade_main_savepoint(true, 2016122800.00);
1493     }
1495     if ($oldversion < 2017020200.01) {
1497         // Define index useridfrom_timeuserfromdeleted_notification (not unique) to be added to message.
1498         $table = new xmldb_table('message');
1499         $index = new xmldb_index('useridfrom_timeuserfromdeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'timeuserfromdeleted', 'notification'));
1501         // Conditionally launch add index useridfrom_timeuserfromdeleted_notification.
1502         if (!$dbman->index_exists($table, $index)) {
1503             $dbman->add_index($table, $index);
1504         }
1506         // Define index useridto_timeusertodeleted_notification (not unique) to be added to message.
1507         $index = new xmldb_index('useridto_timeusertodeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridto', 'timeusertodeleted', 'notification'));
1509         // Conditionally launch add index useridto_timeusertodeleted_notification.
1510         if (!$dbman->index_exists($table, $index)) {
1511             $dbman->add_index($table, $index);
1512         }
1514         $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
1516         // Conditionally launch drop index useridto.
1517         if ($dbman->index_exists($table, $index)) {
1518             $dbman->drop_index($table, $index);
1519         }
1521         // Main savepoint reached.
1522         upgrade_main_savepoint(true, 2017020200.01);
1523     }
1525     if ($oldversion < 2017020200.02) {
1527         // Define index useridfrom_timeuserfromdeleted_notification (not unique) to be added to message_read.
1528         $table = new xmldb_table('message_read');
1529         $index = new xmldb_index('useridfrom_timeuserfromdeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'timeuserfromdeleted', 'notification'));
1531         // Conditionally launch add index useridfrom_timeuserfromdeleted_notification.
1532         if (!$dbman->index_exists($table, $index)) {
1533             $dbman->add_index($table, $index);
1534         }
1536         // Define index useridto_timeusertodeleted_notification (not unique) to be added to message_read.
1537         $index = new xmldb_index('useridto_timeusertodeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridto', 'timeusertodeleted', 'notification'));
1539         // Conditionally launch add index useridto_timeusertodeleted_notification.
1540         if (!$dbman->index_exists($table, $index)) {
1541             $dbman->add_index($table, $index);
1542         }
1544         $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
1546         // Conditionally launch drop index useridto.
1547         if ($dbman->index_exists($table, $index)) {
1548             $dbman->drop_index($table, $index);
1549         }
1551         // Main savepoint reached.
1552         upgrade_main_savepoint(true, 2017020200.02);
1553     }
1555     if ($oldversion < 2017020901.00) {
1557         // Delete "orphaned" block positions. Note, the query does not use indexes (because there are none),
1558         // if it runs too long during upgrade you can comment this line - it will leave orphaned records
1559         // in the database but they won't bother you.
1560         upgrade_block_positions();
1562         // Main savepoint reached.
1563         upgrade_main_savepoint(true, 2017020901.00);
1564     }
1566     if ($oldversion < 2017021300.00) {
1567         unset_config('loginpasswordautocomplete');
1568         upgrade_main_savepoint(true, 2017021300.00);
1569     }
1571     if ($oldversion < 2017021400.00) {
1572         // Define field visibleoncoursepage to be added to course_modules.
1573         $table = new xmldb_table('course_modules');
1574         $field = new xmldb_field('visibleoncoursepage', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'visible');
1576         // Conditionally launch add field visibleoncoursepage.
1577         if (!$dbman->field_exists($table, $field)) {
1578             $dbman->add_field($table, $field);
1579         }
1581         // Main savepoint reached.
1582         upgrade_main_savepoint(true, 2017021400.00);
1583     }
1585     if ($oldversion < 2017030700.00) {
1587         // Define field priority to be added to event.
1588         $table = new xmldb_table('event');
1589         $field = new xmldb_field('priority', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'subscriptionid');
1591         // Conditionally launch add field priority.
1592         if (!$dbman->field_exists($table, $field)) {
1593             $dbman->add_field($table, $field);
1594         }
1596         // Main savepoint reached.
1597         upgrade_main_savepoint(true, 2017030700.00);
1598     }
1600     if ($oldversion < 2017031400.00) {
1602         // Define table file_conversion to be created.
1603         $table = new xmldb_table('file_conversion');
1605         // Adding fields to table file_conversion.
1606         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1607         $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1608         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1609         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1610         $table->add_field('sourcefileid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1611         $table->add_field('targetformat', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1612         $table->add_field('status', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
1613         $table->add_field('statusmessage', XMLDB_TYPE_TEXT, null, null, null, null, null);
1614         $table->add_field('converter', XMLDB_TYPE_CHAR, '255', null, null, null, null);
1615         $table->add_field('destfileid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1616         $table->add_field('data', XMLDB_TYPE_TEXT, null, null, null, null, null);
1618         // Adding keys to table file_conversion.
1619         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1620         $table->add_key('sourcefileid', XMLDB_KEY_FOREIGN, array('sourcefileid'), 'files', array('id'));
1621         $table->add_key('destfileid', XMLDB_KEY_FOREIGN, array('destfileid'), 'files', array('id'));
1623         // Conditionally launch create table for file_conversion.
1624         if (!$dbman->table_exists($table)) {
1625             $dbman->create_table($table);
1626         }
1628         // Main savepoint reached.
1629         upgrade_main_savepoint(true, 2017031400.00);
1630     }
1632     if ($oldversion < 2017040400.00) {
1634         // If block_course_overview is no longer present, replace with block_myoverview.
1635         if (!file_exists($CFG->dirroot . '/blocks/course_overview/block_course_overview.php')) {
1636             $DB->set_field('block_instances', 'blockname', 'myoverview', array('blockname' => 'course_overview'));
1637         }
1639         upgrade_main_savepoint(true, 2017040400.00);
1640     }
1642     if ($oldversion < 2017040401.00) {
1644         // If block_course_overview is no longer present, remove it.
1645         // Note - we do not need to completely remove the block context etc because we
1646         // have replaced all occurrences of block_course_overview with block_myoverview
1647         // in the upgrade step above.
1648         if (!file_exists($CFG->dirroot . '/blocks/course_overview/block_course_overview.php')) {
1649             // Delete the block from the block table.
1650             $DB->delete_records('block', array('name' => 'course_overview'));
1651             // Remove capabilities.
1652             capabilities_cleanup('block_course_overview');
1653             // Clean config.
1654             unset_all_config_for_plugin('block_course_overview');
1655         }
1657         upgrade_main_savepoint(true, 2017040401.00);
1658     }
1660     if ($oldversion < 2017040402.00) {
1662         // Define fields to be added to the 'event' table.
1663         $table = new xmldb_table('event');
1664         $fieldtype = new xmldb_field('type', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, 0, 'instance');
1665         $fieldtimesort = new xmldb_field('timesort', XMLDB_TYPE_INTEGER, '10', null, false, null, null, 'timeduration');
1667         // Conditionally launch add field.
1668         if (!$dbman->field_exists($table, $fieldtype)) {
1669             $dbman->add_field($table, $fieldtype);
1670         }
1672         // Conditionally launch add field.
1673         if (!$dbman->field_exists($table, $fieldtimesort)) {
1674             $dbman->add_field($table, $fieldtimesort);
1675         }
1677         // Now, define the index we will be adding.
1678         $index = new xmldb_index('type-timesort', XMLDB_INDEX_NOTUNIQUE, array('type', 'timesort'));
1680         // Conditionally launch add index.
1681         if (!$dbman->index_exists($table, $index)) {
1682             $dbman->add_index($table, $index);
1683         }
1685         upgrade_main_savepoint(true, 2017040402.00);
1686     }
1688     if ($oldversion < 2017040700.01) {
1690         // Define table oauth2_issuer to be created.
1691         $table = new xmldb_table('oauth2_issuer');
1693         // Adding fields to table oauth2_issuer.
1694         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1695         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1696         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1697         $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1698         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1699         $table->add_field('image', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1700         $table->add_field('baseurl', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1701         $table->add_field('clientid', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1702         $table->add_field('clientsecret', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1703         $table->add_field('loginscopes', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1704         $table->add_field('loginscopesoffline', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1705         $table->add_field('loginparams', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1706         $table->add_field('loginparamsoffline', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1707         $table->add_field('alloweddomains', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1708         $table->add_field('scopessupported', XMLDB_TYPE_TEXT, null, null, null, null, null);
1709         $table->add_field('showonloginpage', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
1710         $table->add_field('enabled', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
1711         $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1713         // Adding keys to table oauth2_issuer.
1714         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1716         // Conditionally launch create table for oauth2_issuer.
1717         if (!$dbman->table_exists($table)) {
1718             $dbman->create_table($table);
1719         }
1721         // Main savepoint reached.
1722         upgrade_main_savepoint(true, 2017040700.01);
1723     }
1725     if ($oldversion < 2017040700.02) {
1727         // Define table oauth2_endpoint to be created.
1728         $table = new xmldb_table('oauth2_endpoint');
1730         // Adding fields to table oauth2_endpoint.
1731         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1732         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1733         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1734         $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1735         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1736         $table->add_field('url', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1737         $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1739         // Adding keys to table oauth2_endpoint.
1740         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1741         $table->add_key('issuer_id_key', XMLDB_KEY_FOREIGN, array('issuerid'), 'oauth2_issuer', array('id'));
1743         // Conditionally launch create table for oauth2_endpoint.
1744         if (!$dbman->table_exists($table)) {
1745             $dbman->create_table($table);
1746         }
1748         // Main savepoint reached.
1749         upgrade_main_savepoint(true, 2017040700.02);
1750     }
1752     if ($oldversion < 2017040700.03) {
1754         // Define table oauth2_system_account to be created.
1755         $table = new xmldb_table('oauth2_system_account');
1757         // Adding fields to table oauth2_system_account.
1758         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1759         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1760         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1761         $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1762         $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1763         $table->add_field('refreshtoken', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1764         $table->add_field('grantedscopes', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1765         $table->add_field('username', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1766         $table->add_field('email', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1768         // Adding keys to table oauth2_system_account.
1769         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1770         $table->add_key('issueridkey', XMLDB_KEY_FOREIGN_UNIQUE, array('issuerid'), 'oauth2_issuer', array('id'));
1772         // Conditionally launch create table for oauth2_system_account.
1773         if (!$dbman->table_exists($table)) {
1774             $dbman->create_table($table);
1775         }
1777         // Main savepoint reached.
1778         upgrade_main_savepoint(true, 2017040700.03);
1779     }
1781     if ($oldversion < 2017040700.04) {
1783         // Define table oauth2_user_field_mapping to be created.
1784         $table = new xmldb_table('oauth2_user_field_mapping');
1786         // Adding fields to table oauth2_user_field_mapping.
1787         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1788         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1789         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1790         $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1791         $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1792         $table->add_field('externalfield', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1793         $table->add_field('internalfield', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1795         // Adding keys to table oauth2_user_field_mapping.
1796         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1797         $table->add_key('issuerkey', XMLDB_KEY_FOREIGN, array('issuerid'), 'oauth2_issuer', array('id'));
1798         $table->add_key('uniqinternal', XMLDB_KEY_UNIQUE, array('issuerid', 'internalfield'));
1800         // Conditionally launch create table for oauth2_user_field_mapping.
1801         if (!$dbman->table_exists($table)) {
1802             $dbman->create_table($table);
1803         }
1805         // Main savepoint reached.
1806         upgrade_main_savepoint(true, 2017040700.04);
1807     }
1809     if ($oldversion < 2017041801.00) {
1811         // Define table course_completion_defaults to be created.
1812         $table = new xmldb_table('course_completion_defaults');
1814         // Adding fields to table course_completion_defaults.
1815         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1816         $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1817         $table->add_field('module', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1818         $table->add_field('completion', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1819         $table->add_field('completionview', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1820         $table->add_field('completionusegrade', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1821         $table->add_field('completionexpected', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1822         $table->add_field('customrules', XMLDB_TYPE_TEXT, null, null, null, null, null);
1824         // Adding keys to table course_completion_defaults.
1825         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1826         $table->add_key('module', XMLDB_KEY_FOREIGN, array('module'), 'modules', array('id'));
1827         $table->add_key('course', XMLDB_KEY_FOREIGN, array('course'), 'course', array('id'));
1829         // Adding indexes to table course_completion_defaults.
1830         $table->add_index('coursemodule', XMLDB_INDEX_UNIQUE, array('course', 'module'));
1832         // Conditionally launch create table for course_completion_defaults.
1833         if (!$dbman->table_exists($table)) {
1834             $dbman->create_table($table);
1835         }
1837         upgrade_main_savepoint(true, 2017041801.00);
1838     }
1840     if ($oldversion < 2017050500.01) {
1841         // Get the list of parent event IDs.
1842         $sql = "SELECT DISTINCT repeatid
1843                            FROM {event}
1844                           WHERE repeatid <> 0";
1845         $parentids = array_keys($DB->get_records_sql($sql));
1846         // Check if there are repeating events we need to process.
1847         if (!empty($parentids)) {
1848             // The repeat IDs of parent events should match their own ID.
1849             // So we need to update parent events that have non-matching IDs and repeat IDs.
1850             list($insql, $params) = $DB->get_in_or_equal($parentids);
1851             $updatesql = "UPDATE {event}
1852                              SET repeatid = id
1853                            WHERE id <> repeatid
1854                                  AND id $insql";
1855             $DB->execute($updatesql, $params);
1856         }
1858         // Main savepoint reached.
1859         upgrade_main_savepoint(true, 2017050500.01);
1860     }
1862     if ($oldversion < 2017050500.02) {
1863         // MDL-58684:
1864         // Remove all portfolio_tempdata records as these may contain serialized \file_system type objects, which are now unable to
1865         // be unserialized because of changes to the file storage API made in MDL-46375. Portfolio now stores an id reference to
1866         // files instead of the object.
1867         // These records are normally removed after a successful export, however, can be left behind if the user abandons the
1868         // export attempt (a stale record). Additionally, each stale record cannot be reused and is normally cleaned up by the cron
1869         // task core\task\portfolio_cron_task. Since the cron task tries to unserialize them, and generates a warning, we'll remove
1870         // all records here.
1871         $DB->delete_records_select('portfolio_tempdata', 'id > ?', [0]);
1873         // Main savepoint reached.
1874         upgrade_main_savepoint(true, 2017050500.02);
1875     }
1877     if ($oldversion < 2017050900.01) {
1878         // Create adhoc task for upgrading of existing calendar events.
1879         $record = new \stdClass();
1880         $record->classname = '\core\task\refresh_mod_calendar_events_task';
1881         $record->component = 'core';
1883         // Next run time based from nextruntime computation in \core\task\manager::queue_adhoc_task().
1884         $nextruntime = time() - 1;
1885         $record->nextruntime = $nextruntime;
1886         $DB->insert_record('task_adhoc', $record);
1888         // Main savepoint reached.
1889         upgrade_main_savepoint(true, 2017050900.01);
1890     }
1892     // Automatically generated Moodle v3.3.0 release upgrade line.
1893     // Put any upgrade step following this.
1895     if ($oldversion < 2017061201.00) {
1896         $table = new xmldb_table('course_sections');
1897         $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'availability');
1899         // Define a field 'timemodified' in the 'course_sections' table.
1900         if (!$dbman->field_exists($table, $field)) {
1901             $dbman->add_field($table, $field);
1902         }
1904         upgrade_main_savepoint(true, 2017061201.00);
1905     }
1907     if ($oldversion < 2017061301.00) {
1908         // Check if the value of 'navcourselimit' is set to the old default value, if so, change it to the new default.
1909         if ($CFG->navcourselimit == 20) {
1910             set_config('navcourselimit', 10);
1911         }
1913         // Main savepoint reached.
1914         upgrade_main_savepoint(true, 2017061301.00);
1915     }
1917     if ($oldversion < 2017071000.00) {
1919         // Define field requireconfirmation to be added to oauth2_issuer.
1920         $table = new xmldb_table('oauth2_issuer');
1921         $field = new xmldb_field('requireconfirmation', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1', 'sortorder');
1923         // Conditionally launch add field requireconfirmation.
1924         if (!$dbman->field_exists($table, $field)) {
1925             $dbman->add_field($table, $field);
1926         }
1928         // Main savepoint reached.
1929         upgrade_main_savepoint(true, 2017071000.00);
1930     }
1932     if ($oldversion < 2017071001.00) {
1934         // Define field timemodified to be added to block_instances.
1935         $table = new xmldb_table('block_instances');
1936         $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null,
1937                 null, null, 'configdata');
1939         // Conditionally launch add field timemodified.
1940         if (!$dbman->field_exists($table, $field)) {
1941             $dbman->add_field($table, $field);
1943             // Set field to current time.
1944             $DB->set_field('block_instances', 'timemodified', time());
1946             // Changing nullability of field timemodified on table block_instances to not null.
1947             $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL,
1948                     null, null, 'configdata');
1950             // Launch change of nullability for field timemodified.
1951             $dbman->change_field_notnull($table, $field);
1953             // Define index timemodified (not unique) to be added to block_instances.
1954             $index = new xmldb_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1956             // Conditionally launch add index timemodified.
1957             if (!$dbman->index_exists($table, $index)) {
1958                 $dbman->add_index($table, $index);
1959             }
1960         }
1962         // Define field timecreated to be added to block_instances.
1963         $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, null,
1964                 null, null, 'configdata');
1966         // Conditionally launch add field timecreated.
1967         if (!$dbman->field_exists($table, $field)) {
1968             $dbman->add_field($table, $field);
1970             // Set field to current time.
1971             $DB->set_field('block_instances', 'timecreated', time());
1973             // Changing nullability of field timecreated on table block_instances to not null.
1974             $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL,
1975                     null, null, 'configdata');
1977             // Launch change of nullability for field timecreated.
1978             $dbman->change_field_notnull($table, $field);
1979         }
1981         // Main savepoint reached.
1982         upgrade_main_savepoint(true, 2017071001.00);
1983     }
1985     if ($oldversion < 2017071100.00 ) {
1986         // Clean old upgrade setting not used anymore.
1987         unset_config('upgrade_minmaxgradestepignored');
1988         upgrade_main_savepoint(true, 2017071100.00);
1989     }
1991     if ($oldversion < 2017072000.02) {
1993         // Define table analytics_models to be created.
1994         $table = new xmldb_table('analytics_models');
1996         // Adding fields to table analytics_models.
1997         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1998         $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1999         $table->add_field('trained', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
2000         $table->add_field('target', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2001         $table->add_field('indicators', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2002         $table->add_field('timesplitting', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2003         $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2004         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
2005         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2006         $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2008         // Adding keys to table analytics_models.
2009         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2011         // Adding indexes to table analytics_models.
2012         $table->add_index('enabledandtrained', XMLDB_INDEX_NOTUNIQUE, array('enabled', 'trained'));
2014         // Conditionally launch create table for analytics_models.
2015         if (!$dbman->table_exists($table)) {
2016             $dbman->create_table($table);
2017         }
2019         // Define table analytics_models_log to be created.
2020         $table = new xmldb_table('analytics_models_log');
2022         // Adding fields to table analytics_models_log.
2023         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2024         $table->add_field('modelid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2025         $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2026         $table->add_field('target', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2027         $table->add_field('indicators', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2028         $table->add_field('timesplitting', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2029         $table->add_field('score', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, '0');
2030         $table->add_field('info', XMLDB_TYPE_TEXT, null, null, null, null, null);
2031         $table->add_field('dir', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2032         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2033         $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2035         // Adding keys to table analytics_models_log.
2036         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2038         // Adding indexes to table analytics_models_log.
2039         $table->add_index('modelid', XMLDB_INDEX_NOTUNIQUE, array('modelid'));
2041         // Conditionally launch create table for analytics_models_log.
2042         if (!$dbman->table_exists($table)) {
2043             $dbman->create_table($table);
2044         }
2046         // Define table analytics_predictions to be created.
2047         $table = new xmldb_table('analytics_predictions');
2049         // Adding fields to table analytics_predictions.
2050         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2051         $table->add_field('modelid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2052         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2053         $table->add_field('sampleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2054         $table->add_field('rangeindex', XMLDB_TYPE_INTEGER, '5', null, XMLDB_NOTNULL, null, null);
2055         $table->add_field('prediction', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, null);
2056         $table->add_field('predictionscore', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
2057         $table->add_field('calculations', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2058         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2060         // Adding keys to table analytics_predictions.
2061         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2063         // Adding indexes to table analytics_predictions.
2064         $table->add_index('modelidandcontextid', XMLDB_INDEX_NOTUNIQUE, array('modelid', 'contextid'));
2066         // Conditionally launch create table for analytics_predictions.
2067         if (!$dbman->table_exists($table)) {
2068             $dbman->create_table($table);
2069         }
2071         // Define table analytics_train_samples to be created.
2072         $table = new xmldb_table('analytics_train_samples');
2074         // Adding fields to table analytics_train_samples.
2075         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2076         $table->add_field('modelid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2077         $table->add_field('analysableid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2078         $table->add_field('timesplitting', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2079         $table->add_field('fileid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2080         $table->add_field('sampleids', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
2081         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2083         // Adding keys to table analytics_train_samples.
2084         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2086         // Adding indexes to table analytics_train_samples.
2087         $table->add_index('modelidandanalysableidandtimesplitting', XMLDB_INDEX_NOTUNIQUE,
2088             array('modelid', 'analysableid', 'timesplitting'));
2090         // Conditionally launch create table for analytics_train_samples.
2091         if (!$dbman->table_exists($table)) {
2092             $dbman->create_table($table);
2093         }
2095         // Define table analytics_predict_ranges to be created.
2096         $table = new xmldb_table('analytics_predict_ranges');
2098         // Adding fields to table analytics_predict_ranges.
2099         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2100         $table->add_field('modelid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2101         $table->add_field('analysableid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2102         $table->add_field('timesplitting', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2103         $table->add_field('rangeindex', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2104         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2106         // Adding keys to table analytics_predict_ranges.
2107         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2109         // Adding indexes to table analytics_predict_ranges.
2110         $table->add_index('modelidandanalysableidandtimesplitting', XMLDB_INDEX_NOTUNIQUE,
2111             array('modelid', 'analysableid', 'timesplitting'));
2113         // Conditionally launch create table for analytics_predict_ranges.
2114         if (!$dbman->table_exists($table)) {
2115             $dbman->create_table($table);
2116         }
2118         // Define table analytics_used_files to be created.
2119         $table = new xmldb_table('analytics_used_files');
2121         // Adding fields to table analytics_used_files.
2122         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2123         $table->add_field('modelid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2124         $table->add_field('fileid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2125         $table->add_field('action', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
2126         $table->add_field('time', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2128         // Adding keys to table analytics_used_files.
2129         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2131         // Adding indexes to table analytics_used_files.
2132         $table->add_index('modelidandfileidandaction', XMLDB_INDEX_NOTUNIQUE, array('modelid', 'fileid', 'action'));
2134         // Conditionally launch create table for analytics_used_files.
2135         if (!$dbman->table_exists($table)) {
2136             $dbman->create_table($table);
2137         }
2139         $now = time();
2140         $admin = get_admin();
2142         $targetname = '\core\analytics\target\course_dropout';
2143         if (!$DB->record_exists('analytics_models', array('target' => $targetname))) {
2144             // We can not use API calls to create the built-in models.
2145             $modelobj = new stdClass();
2146             $modelobj->target = $targetname;
2147             $modelobj->indicators = json_encode(array(
2148                 '\mod_assign\analytics\indicator\cognitive_depth',
2149                 '\mod_assign\analytics\indicator\social_breadth',
2150                 '\mod_book\analytics\indicator\cognitive_depth',
2151                 '\mod_book\analytics\indicator\social_breadth',
2152                 '\mod_chat\analytics\indicator\cognitive_depth',
2153                 '\mod_chat\analytics\indicator\social_breadth',
2154                 '\mod_choice\analytics\indicator\cognitive_depth',
2155                 '\mod_choice\analytics\indicator\social_breadth',
2156                 '\mod_data\analytics\indicator\cognitive_depth',
2157                 '\mod_data\analytics\indicator\social_breadth',
2158                 '\mod_feedback\analytics\indicator\cognitive_depth',
2159                 '\mod_feedback\analytics\indicator\social_breadth',
2160                 '\mod_folder\analytics\indicator\cognitive_depth',
2161                 '\mod_folder\analytics\indicator\social_breadth',
2162                 '\mod_forum\analytics\indicator\cognitive_depth',
2163                 '\mod_forum\analytics\indicator\social_breadth',
2164                 '\mod_glossary\analytics\indicator\cognitive_depth',
2165                 '\mod_glossary\analytics\indicator\social_breadth',
2166                 '\mod_imscp\analytics\indicator\cognitive_depth',
2167                 '\mod_imscp\analytics\indicator\social_breadth',
2168                 '\mod_label\analytics\indicator\cognitive_depth',
2169                 '\mod_label\analytics\indicator\social_breadth',
2170                 '\mod_lesson\analytics\indicator\cognitive_depth',
2171                 '\mod_lesson\analytics\indicator\social_breadth',
2172                 '\mod_lti\analytics\indicator\cognitive_depth',
2173                 '\mod_lti\analytics\indicator\social_breadth',
2174                 '\mod_page\analytics\indicator\cognitive_depth',
2175                 '\mod_page\analytics\indicator\social_breadth',
2176                 '\mod_quiz\analytics\indicator\cognitive_depth',
2177                 '\mod_quiz\analytics\indicator\social_breadth',
2178                 '\mod_resource\analytics\indicator\cognitive_depth',
2179                 '\mod_resource\analytics\indicator\social_breadth',
2180                 '\mod_scorm\analytics\indicator\cognitive_depth',
2181                 '\mod_scorm\analytics\indicator\social_breadth',
2182                 '\mod_survey\analytics\indicator\cognitive_depth',
2183                 '\mod_survey\analytics\indicator\social_breadth',
2184                 '\mod_url\analytics\indicator\cognitive_depth',
2185                 '\mod_url\analytics\indicator\social_breadth',
2186                 '\mod_wiki\analytics\indicator\cognitive_depth',
2187                 '\mod_wiki\analytics\indicator\social_breadth',
2188                 '\mod_workshop\analytics\indicator\cognitive_depth',
2189                 '\mod_workshop\analytics\indicator\social_breadth',
2190             ));
2191             $modelobj->version = $now;
2192             $modelobj->timecreated = $now;
2193             $modelobj->timemodified = $now;
2194             $modelobj->usermodified = $admin->id;
2195             $DB->insert_record('analytics_models', $modelobj);
2196         }
2198         $targetname = '\core\analytics\target\no_teaching';
2199         if (!$DB->record_exists('analytics_models', array('target' => $targetname))) {
2200             $modelobj = new stdClass();
2201             $modelobj->enabled = 1;
2202             $modelobj->trained = 1;
2203             $modelobj->target = $targetname;
2204             $modelobj->indicators = json_encode(array('\core_course\analytics\indicator\no_teacher'));
2205             $modelobj->timesplitting = '\core\analytics\time_splitting\single_range';
2206             $modelobj->version = $now;
2207             $modelobj->timecreated = $now;
2208             $modelobj->timemodified = $now;
2209             $modelobj->usermodified = $admin->id;
2210             $DB->insert_record('analytics_models', $modelobj);
2211         }
2213         // Main savepoint reached.
2214         upgrade_main_savepoint(true, 2017072000.02);
2215     }
2217     if ($oldversion < 2017072700.01) {
2218         // Changing nullability of field email on table oauth2_system_account to null.
2219         $table = new xmldb_table('oauth2_system_account');
2220         $field = new xmldb_field('email', XMLDB_TYPE_TEXT, null, null, null, null, null, 'grantedscopes');
2222         // Launch change of nullability for field email.
2223         $dbman->change_field_notnull($table, $field);
2225         // Main savepoint reached.
2226         upgrade_main_savepoint(true, 2017072700.01);
2227     }
2229     if ($oldversion < 2017072700.02) {
2231         // If the site was previously registered with http://hub.moodle.org change the registration to
2232         // point to https://moodle.net - this is the correct hub address using https protocol.
2233         $oldhuburl = "http://hub.moodle.org";
2234         $newhuburl = "https://moodle.net";
2235         $cleanoldhuburl = preg_replace('/[^A-Za-z0-9_-]/i', '', $oldhuburl);
2236         $cleannewhuburl = preg_replace('/[^A-Za-z0-9_-]/i', '', $newhuburl);
2238         // Update existing registration.
2239         $DB->execute("UPDATE {registration_hubs} SET hubname = ?, huburl = ? WHERE huburl = ?",
2240             ['Moodle.net', $newhuburl, $oldhuburl]);
2242         // Update settings of existing registration.
2243         $sqlnamelike = $DB->sql_like('name', '?');
2244         $entries = $DB->get_records_sql("SELECT * FROM {config_plugins} where plugin=? and " . $sqlnamelike,
2245             ['hub', '%' . $DB->sql_like_escape('_' . $cleanoldhuburl)]);
2246         foreach ($entries as $entry) {
2247             $newname = substr($entry->name, 0, -strlen($cleanoldhuburl)) . $cleannewhuburl;
2248             try {
2249                 $DB->update_record('config_plugins', ['id' => $entry->id, 'name' => $newname]);
2250             } catch (dml_exception $e) {
2251                 // Entry with new name already exists, remove the one with an old name.
2252                 $DB->delete_records('config_plugins', ['id' => $entry->id]);
2253             }
2254         }
2256         // Update published courses.
2257         $DB->execute('UPDATE {course_published} SET huburl = ? WHERE huburl = ?', [$newhuburl, $oldhuburl]);
2259         // Main savepoint reached.
2260         upgrade_main_savepoint(true, 2017072700.02);
2261     }
2263     if ($oldversion < 2017080700.01) {
2265         // Get the table by its previous name.
2266         $table = new xmldb_table('analytics_predict_ranges');
2267         if ($dbman->table_exists($table)) {
2269             // We can only accept this because we are in master.
2270             $DB->delete_records('analytics_predictions');
2271             $DB->delete_records('analytics_used_files', array('action' => 'predicted'));
2272             $DB->delete_records('analytics_predict_ranges');
2274             // Define field sampleids to be added to analytics_predict_ranges (renamed below to analytics_predict_samples).
2275             $field = new xmldb_field('sampleids', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'rangeindex');
2277             // Conditionally launch add field sampleids.
2278             if (!$dbman->field_exists($table, $field)) {
2279                 $dbman->add_field($table, $field);
2280             }
2282             // Define field timemodified to be added to analytics_predict_ranges (renamed below to analytics_predict_samples).
2283             $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'timecreated');
2285             // Conditionally launch add field timemodified.
2286             if (!$dbman->field_exists($table, $field)) {
2287                 $dbman->add_field($table, $field);
2288             }
2290             // Rename the table to its new name.
2291             $dbman->rename_table($table, 'analytics_predict_samples');
2292         }
2294         $table = new xmldb_table('analytics_predict_samples');
2296         $index = new xmldb_index('modelidandanalysableidandtimesplitting', XMLDB_INDEX_NOTUNIQUE,
2297             array('modelid', 'analysableid', 'timesplitting'));
2299         // Conditionally launch drop index.
2300         if ($dbman->index_exists($table, $index)) {
2301             $dbman->drop_index($table, $index);
2302         }
2304         $index = new xmldb_index('modelidandanalysableidandtimesplittingandrangeindex', XMLDB_INDEX_NOTUNIQUE,
2305             array('modelid', 'analysableid', 'timesplitting', 'rangeindex'));
2307         // Conditionally launch add index.
2308         if (!$dbman->index_exists($table, $index)) {
2309             $dbman->add_index($table, $index);
2310         }
2312         // Main savepoint reached.
2313         upgrade_main_savepoint(true, 2017080700.01);
2314     }
2316     if ($oldversion < 2017082200.00) {
2317         $plugins = ['radius', 'fc', 'nntp', 'pam', 'pop3', 'imap'];
2319         foreach ($plugins as $plugin) {
2320             // Check to see if the plugin exists on disk.
2321             // If it does not, remove the config for it.
2322             if (!file_exists($CFG->dirroot . "/auth/{$plugin}/auth.php")) {
2323                 // Clean config.
2324                 unset_all_config_for_plugin("auth_{$plugin}");
2325             }
2326         }
2327         upgrade_main_savepoint(true, 2017082200.00);
2328     }
2330     if ($oldversion < 2017082200.01) {
2332         // Define table analytics_indicator_calc to be created.
2333         $table = new xmldb_table('analytics_indicator_calc');
2335         // Adding fields to table analytics_indicator_calc.
2336         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2337         $table->add_field('starttime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2338         $table->add_field('endtime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2339         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2340         $table->add_field('sampleorigin', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2341         $table->add_field('sampleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2342         $table->add_field('indicator', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2343         $table->add_field('value', XMLDB_TYPE_NUMBER, '10, 2', null, null, null, null);
2344         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2346         // Adding keys to table analytics_indicator_calc.
2347         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2349         // Adding indexes to table analytics_indicator_calc.
2350         $table->add_index('starttime-endtime-contextid', XMLDB_INDEX_NOTUNIQUE, array('starttime', 'endtime', 'contextid'));
2352         // Conditionally launch create table for analytics_indicator_calc.
2353         if (!$dbman->table_exists($table)) {
2354             $dbman->create_table($table);
2355         }
2357         // Main savepoint reached.
2358         upgrade_main_savepoint(true, 2017082200.01);
2359     }
2361     if ($oldversion < 2017082300.01) {
2363         // This script in included in each major version upgrade process so make sure we don't run it twice.
2364         if (empty($CFG->linkcoursesectionsupgradescriptwasrun)) {
2365             // Check if the site is using a boost-based theme.
2366             // If value of 'linkcoursesections' is set to the old default value, change it to the new default.
2367             if (upgrade_theme_is_from_family('boost', $CFG->theme)) {
2368                 set_config('linkcoursesections', 1);
2369             }
2370             set_config('linkcoursesectionsupgradescriptwasrun', 1);
2371         }
2373         // Main savepoint reached.
2374         upgrade_main_savepoint(true, 2017082300.01);
2375     }
2377     if ($oldversion < 2017082500.00) {
2378         // Handle FKs for the table 'analytics_models_log'.
2379         $table = new xmldb_table('analytics_models_log');
2381         // Remove the existing index before adding FK (which creates an index).
2382         $index = new xmldb_index('modelid', XMLDB_INDEX_NOTUNIQUE, array('modelid'));
2384         // Conditionally launch drop index.
2385         if ($dbman->index_exists($table, $index)) {
2386             $dbman->drop_index($table, $index);
2387         }
2389         // Now, add the FK.
2390         $key = new xmldb_key('modelid', XMLDB_KEY_FOREIGN, array('modelid'), 'analytics_models', array('id'));
2391         $dbman->add_key($table, $key);
2393         // Handle FKs for the table 'analytics_predictions'.
2394         $table = new xmldb_table('analytics_predictions');
2395         $key = new xmldb_key('modelid', XMLDB_KEY_FOREIGN, array('modelid'), 'analytics_models', array('id'));
2396         $dbman->add_key($table, $key);
2398         $key = new xmldb_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2399         $dbman->add_key($table, $key);
2401         // Handle FKs for the table 'analytics_train_samples'.
2402         $table = new xmldb_table('analytics_train_samples');
2403         $key = new xmldb_key('modelid', XMLDB_KEY_FOREIGN, array('modelid'), 'analytics_models', array('id'));
2404         $dbman->add_key($table, $key);
2406         $key = new xmldb_key('fileid', XMLDB_KEY_FOREIGN, array('fileid'), 'files', array('id'));
2407         $dbman->add_key($table, $key);
2409         // Handle FKs for the table 'analytics_predict_samples'.
2410         $table = new xmldb_table('analytics_predict_samples');
2411         $key = new xmldb_key('modelid', XMLDB_KEY_FOREIGN, array('modelid'), 'analytics_models', array('id'));
2412         $dbman->add_key($table, $key);
2414         // Handle FKs for the table 'analytics_used_files'.
2415         $table = new xmldb_table('analytics_used_files');
2416         $key = new xmldb_key('modelid', XMLDB_KEY_FOREIGN, array('modelid'), 'analytics_models', array('id'));
2417         $dbman->add_key($table, $key);
2419         $key = new xmldb_key('fileid', XMLDB_KEY_FOREIGN, array('fileid'), 'files', array('id'));
2420         $dbman->add_key($table, $key);
2422         // Handle FKs for the table 'analytics_indicator_calc'.
2423         $table = new xmldb_table('analytics_indicator_calc');
2424         $key = new xmldb_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2425         $dbman->add_key($table, $key);
2427         // Main savepoint reached.
2428         upgrade_main_savepoint(true, 2017082500.00);
2429     }
2431     if ($oldversion < 2017082800.00) {
2433         // Changing type of field prediction on table analytics_predictions to number.
2434         $table = new xmldb_table('analytics_predictions');
2435         $field = new xmldb_field('prediction', XMLDB_TYPE_NUMBER, '10, 2', null, XMLDB_NOTNULL, null, null, 'rangeindex');
2437         // Launch change of type for field prediction.
2438         $dbman->change_field_type($table, $field);
2440         // Main savepoint reached.
2441         upgrade_main_savepoint(true, 2017082800.00);
2442     }
2444     if ($oldversion < 2017090700.01) {
2446         // Define table analytics_prediction_actions to be created.
2447         $table = new xmldb_table('analytics_prediction_actions');
2449         // Adding fields to table analytics_prediction_actions.
2450         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2451         $table->add_field('predictionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2452         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2453         $table->add_field('actionname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2454         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2456         // Adding keys to table analytics_prediction_actions.
2457         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2458         $table->add_key('predictionid', XMLDB_KEY_FOREIGN, array('predictionid'), 'analytics_predictions', array('id'));
2459         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2461         // Adding indexes to table analytics_prediction_actions.
2462         $table->add_index('predictionidanduseridandactionname', XMLDB_INDEX_NOTUNIQUE,
2463             array('predictionid', 'userid', 'actionname'));
2465         // Conditionally launch create table for analytics_prediction_actions.
2466         if (!$dbman->table_exists($table)) {
2467             $dbman->create_table($table);
2468         }
2470         // Main savepoint reached.
2471         upgrade_main_savepoint(true, 2017090700.01);
2472     }
2474     if ($oldversion < 2017091200.00) {
2475         // Force all messages to be reindexed.
2476         set_config('core_message_message_sent_lastindexrun', '0', 'core_search');
2477         set_config('core_message_message_received_lastindexrun', '0', 'core_search');
2479         // Main savepoint reached.
2480         upgrade_main_savepoint(true, 2017091200.00);
2481     }
2483     if ($oldversion < 2017091201.00) {
2484         // Define field userid to be added to task_adhoc.
2485         $table = new xmldb_table('task_adhoc');
2486         $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customdata');
2488         // Conditionally launch add field userid.
2489         if (!$dbman->field_exists($table, $field)) {
2490             $dbman->add_field($table, $field);
2491         }
2493         $key = new xmldb_key('useriduser', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2495         // Launch add key userid_user.
2496         $dbman->add_key($table, $key);
2498         // Main savepoint reached.
2499         upgrade_main_savepoint(true, 2017091201.00);
2500     }
2502     if ($oldversion < 2017092201.00) {
2504         // Remove duplicate registrations.
2505         $newhuburl = "https://moodle.net";
2506         $registrations = $DB->get_records('registration_hubs', ['huburl' => $newhuburl], 'confirmed DESC, id ASC');
2507         if (count($registrations) > 1) {
2508             $reg = array_shift($registrations);
2509             $DB->delete_records_select('registration_hubs', 'huburl = ? AND id <> ?', [$newhuburl, $reg->id]);
2510         }
2512         // Main savepoint reached.
2513         upgrade_main_savepoint(true, 2017092201.00);
2514     }
2516     return true;