Merge branch 'MDL-59890-master' of git://github.com/andrewnicols/moodle
[moodle.git] / lib / db / upgrade.php
CommitLineData
5b4a78e2 1<?php
5b4a78e2 2// This file is part of Moodle - http://moodle.org/
4e423cbf 3//
5b4a78e2
PS
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
4e423cbf 8//
5b4a78e2
PS
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
4e423cbf 13//
5b4a78e2
PS
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This file keeps track of upgrades to Moodle.
19 *
20 * Sometimes, changes between versions involve
21 * alterations to database structures and other
22 * major things that may break installations.
23 *
24 * The upgrade function in this file will attempt
25 * to perform all the necessary actions to upgrade
26 * your older installation to the current version.
27 *
28 * If there's something it cannot do itself, it
29 * will tell you what you need to do.
30 *
31 * The commands in here will all be database-neutral,
32 * using the methods of database_manager class
33 *
34 * Please do not forget to use upgrade_set_timeout()
35 * before any action that may take longer time to finish.
36 *
39b90b51
EL
37 * @package core_install
38 * @category upgrade
39 * @copyright 2006 onwards Martin Dougiamas http://dougiamas.com
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5b4a78e2
PS
41 */
42
43defined('MOODLE_INTERNAL') || die();
4e423cbf 44
3406acde 45/**
39b90b51
EL
46 * Main upgrade tasks to be executed on Moodle version bump
47 *
48 * This function is automatically executed after one bump in the Moodle core
f9488a6f 49 * version is detected. It's in charge of performing the required tasks
39b90b51
EL
50 * to raise core from the previous version to the next one.
51 *
52 * It's a collection of ordered blocks of code, named "upgrade steps",
f9488a6f 53 * each one performing one isolated (from the rest of steps) task. Usually
39b90b51
EL
54 * tasks involve creating new DB objects or performing manipulation of the
55 * information for cleanup/fixup purposes.
56 *
57 * Each upgrade step has a fixed structure, that can be summarised as follows:
58 *
59 * if ($oldversion < XXXXXXXXXX.XX) {
f9488a6f 60 * // Explanation of the update step, linking to issue in the Tracker if necessary
39b90b51
EL
61 * upgrade_set_timeout(XX); // Optional for big tasks
62 * // Code to execute goes here, usually the XMLDB Editor will
63 * // help you here. See {@link http://docs.moodle.org/dev/XMLDB_editor}.
64 * upgrade_main_savepoint(true, XXXXXXXXXX.XX);
65 * }
66 *
67 * All plugins within Moodle (modules, blocks, reports...) support the existence of
68 * their own upgrade.php file, using the "Frankenstyle" component name as
69 * defined at {@link http://docs.moodle.org/dev/Frankenstyle}, for example:
70 * - {@link xmldb_page_upgrade($oldversion)}. (modules don't require the plugintype ("mod_") to be used.
71 * - {@link xmldb_auth_manual_upgrade($oldversion)}.
72 * - {@link xmldb_workshopform_accumulative_upgrade($oldversion)}.
73 * - ....
74 *
75 * In order to keep the contents of this file reduced, it's allowed to create some helper
76 * functions to be used here in the {@link upgradelib.php} file at the same directory. Note
f9488a6f 77 * that such a file must be manually included from upgrade.php, and there are some restrictions
39b90b51
EL
78 * about what can be used within it.
79 *
80 * For more information, take a look to the documentation available:
81 * - Data definition API: {@link http://docs.moodle.org/dev/Data_definition_API}
82 * - Upgrade API: {@link http://docs.moodle.org/dev/Upgrade_API}
3406acde 83 *
3406acde 84 * @param int $oldversion
5b4a78e2 85 * @return bool always true
3406acde 86 */
775f811a 87function xmldb_main_upgrade($oldversion) {
e8c82aac 88 global $CFG, $DB;
4e423cbf 89
e8c82aac 90 require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions.
13a0d3d3 91
e8c82aac 92 $dbman = $DB->get_manager(); // Loads ddl manager and xmldb classes.
f33e1ed4 93
e8c82aac 94 // Always keep this upgrade step with version being the minimum
29af7b0b
EL
95 // allowed version to upgrade from (v3.0.0 right now).
96 if ($oldversion < 2015111600) {
e8c82aac 97 // Just in case somebody hacks upgrade scripts or env, we really can not continue.
29af7b0b 98 echo("You need to upgrade to 3.0.x or higher first!\n");
5c79b8ed 99 exit(1);
e8c82aac 100 // Note this savepoint is 100% unreachable, but needed to pass the upgrade checks.
29af7b0b 101 upgrade_main_savepoint(true, 2015111600);
5c79b8ed
PS
102 }
103
67f68027 104 if ($oldversion < 2016011300.01) {
c026a28d
MG
105
106 // This is a big upgrade script. We create new table tag_coll and the field
107 // tag.tagcollid pointing to it.
108
109 // Define table tag_coll to be created.
110 $table = new xmldb_table('tag_coll');
111
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);
120
121 // Adding keys to table tagcloud.
122 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
123
124 // Conditionally launch create table for tagcloud.
125 if (!$dbman->table_exists($table)) {
126 $dbman->create_table($table);
127 }
128
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'));
133
134 // Conditionally launch drop index name.
135 if ($dbman->index_exists($table, $index)) {
136 $dbman->drop_index($table, $index);
137 }
138
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');
142
143 // Conditionally launch add field tagcloudid.
144 if (!$dbman->field_exists($table, $field)) {
145 $dbman->add_field($table, $field);
146 }
147
148 // Main savepoint reached.
67f68027 149 upgrade_main_savepoint(true, 2016011300.01);
c026a28d
MG
150 }
151
67f68027 152 if ($oldversion < 2016011300.02) {
c026a28d
MG
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));
159
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');
164
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 }
171
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'));
175
176 // Launch add key tagcloudid.
177 $dbman->add_key($table, $key);
178
c026a28d 179 // Main savepoint reached.
67f68027 180 upgrade_main_savepoint(true, 2016011300.02);
c026a28d
MG
181 }
182
67f68027 183 if ($oldversion < 2016011300.03) {
c026a28d
MG
184
185 // Define table tag_area to be created.
186 $table = new xmldb_table('tag_area');
187
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);
196
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'));
200
201 // Adding indexes to table tag_area.
202 $table->add_index('compitemtype', XMLDB_INDEX_UNIQUE, array('component', 'itemtype'));
203
204 // Conditionally launch create table for tag_area.
205 if (!$dbman->table_exists($table)) {
206 $dbman->create_table($table);
207 }
208
209 // Main savepoint reached.
67f68027 210 upgrade_main_savepoint(true, 2016011300.03);
c026a28d
MG
211 }
212
67f68027 213 if ($oldversion < 2016011300.04) {
c026a28d
MG
214
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'));
219
220 // Conditionally launch drop index itemtype-itemid-tagid-tiuserid.
221 if ($dbman->index_exists($table, $index)) {
222 $dbman->drop_index($table, $index);
223 }
224
225 // Main savepoint reached.
67f68027 226 upgrade_main_savepoint(true, 2016011300.04);
c026a28d
MG
227 }
228
67f68027 229 if ($oldversion < 2016011300.05) {
c026a28d
MG
230
231 $DB->execute("UPDATE {tag_instance} SET component = ? WHERE component IS NULL", array(''));
232
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');
236
237 // Launch change of nullability for field component.
238 $dbman->change_field_notnull($table, $field);
239
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');
243
244 // Launch change of type for field itemtype.
245 $dbman->change_field_type($table, $field);
246
247 // Main savepoint reached.
67f68027 248 upgrade_main_savepoint(true, 2016011300.05);
c026a28d
MG
249 }
250
67f68027 251 if ($oldversion < 2016011300.06) {
c026a28d
MG
252
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'));
256
257 // Conditionally launch add index taggeditem.
258 if (!$dbman->index_exists($table, $index)) {
259 $dbman->add_index($table, $index);
260 }
261
262 // Main savepoint reached.
67f68027 263 upgrade_main_savepoint(true, 2016011300.06);
c026a28d
MG
264 }
265
67f68027 266 if ($oldversion < 2016011300.07) {
c026a28d
MG
267
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'));
271
272 // Conditionally launch add index taglookup.
273 if (!$dbman->index_exists($table, $index)) {
274 $dbman->add_index($table, $index);
275 }
276
277 // Main savepoint reached.
67f68027 278 upgrade_main_savepoint(true, 2016011300.07);
c026a28d
MG
279 }
280
67f68027 281 if ($oldversion < 2016011301.00) {
a73409d3
CB
282
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 }
67f68027 290 upgrade_main_savepoint(true, 2016011301.00);
a73409d3
CB
291 }
292
d3220ca3 293 if ($oldversion < 2016011901.00) {
a7213391
SB
294
295 // Convert calendar_lookahead to nearest new value.
296 $transaction = $DB->start_delegated_transaction();
297
298 // Count all users who curretly have that preference set (for progress bar).
668aca18 299 $total = $DB->count_records_select('user_preferences', "name = 'calendar_lookahead' AND value != '0'");
a7213391
SB
300 $pbar = new progress_bar('upgradecalendarlookahead', 500, true);
301
302 // Get all these users, one at a time.
668aca18 303 $rs = $DB->get_recordset_select('user_preferences', "name = 'calendar_lookahead' AND value != '0'");
a7213391
SB
304 $i = 0;
305 foreach ($rs as $userpref) {
306
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 }
323
324 $DB->set_field('user_preferences', 'value', $newvalue, array('id' => $userpref->id));
325
326 // Update progress.
327 $i++;
328 $pbar->update($i, $total, "Upgrading user preference settings - $i/$total.");
329 }
330 $rs->close();
331 $transaction->allow_commit();
332
d3220ca3 333 upgrade_main_savepoint(true, 2016011901.00);
a7213391 334 }
e11d7380
MG
335
336 if ($oldversion < 2016020200.00) {
337
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');
341
342 // Conditionally launch add field isstandard.
343 if (!$dbman->field_exists($table, $field)) {
344 $dbman->add_field($table, $field);
345 }
346
347 // Define index tagcolltype (not unique) to be dropped form tag.
2b1c3e4d 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.
e11d7380
MG
349 $index = new xmldb_index('tagcolltype', XMLDB_INDEX_NOTUNIQUE, array('tagcollid', 'tagtype'));
350
351 // Conditionally launch drop index tagcolltype.
352 if ($dbman->index_exists($table, $index)) {
353 $dbman->drop_index($table, $index);
354 }
355
356 // Define index tagcolltype (not unique) to be added to tag.
357 $index = new xmldb_index('tagcolltype', XMLDB_INDEX_NOTUNIQUE, array('tagcollid', 'isstandard'));
358
359 // Conditionally launch add index tagcolltype.
360 if (!$dbman->index_exists($table, $index)) {
361 $dbman->add_index($table, $index);
362 }
363
364 // Define field tagtype to be dropped from tag.
365 $field = new xmldb_field('tagtype');
366
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 }
372
373 // Main savepoint reached.
374 upgrade_main_savepoint(true, 2016020200.00);
375 }
376
4be9c7ad
MG
377 if ($oldversion < 2016020201.00) {
378
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');
382
383 // Conditionally launch add field showstandard.
384 if (!$dbman->field_exists($table, $field)) {
385 $dbman->add_field($table, $field);
386 }
387
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'));
391
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');
395
396 // Launch change of precision for field enabled.
397 $dbman->change_field_precision($table, $field);
398
399 // Main savepoint reached.
400 upgrade_main_savepoint(true, 2016020201.00);
401 }
402
a9e12347
AN
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');
408
409 foreach ($repositories as $id => $repository) {
410 $directory = $root . '/repository_' . $repository->type;
411 if (is_dir($directory)) {
412 fulldelete($directory);
413 }
414 }
415 }
416
417 // Main savepoint reached.
418 upgrade_main_savepoint(true, 2016021500.00);
419 }
420
c06749bd 421 if ($oldversion < 2016021501.00) {
201a9093
EM
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);
424
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'));
428
429 // Conditionally launch add index userid-itemid.
430 if (!$dbman->index_exists($table, $index)) {
431 $dbman->add_index($table, $index);
432 }
433
434 // Main savepoint reached.
c06749bd 435 upgrade_main_savepoint(true, 2016021501.00);
201a9093
EM
436 }
437
73911de5 438 if ($oldversion < 2016030103.00) {
7d19e0e3
RK
439
440 // MDL-50887. Implement plugins infrastructure for antivirus and create ClamAV plugin.
441 // This routine moves core ClamAV configuration to plugin level.
442
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 }
449
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 }
468
469 // Main savepoint reached.
73911de5 470 upgrade_main_savepoint(true, 2016030103.00);
7d19e0e3
RK
471 }
472
186eba1b
JL
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');
477
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 }
485
a4e659c7
FM
486 if ($oldversion < 2016041500.50) {
487
488 // Define table competency to be created.
489 $table = new xmldb_table('competency');
490
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);
509
510 // Adding keys to table competency.
511 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
512
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'));
516
517 // Conditionally launch create table for competency.
518 if (!$dbman->table_exists($table)) {
519 $dbman->create_table($table);
520 }
521
522 // Main savepoint reached.
523 upgrade_main_savepoint(true, 2016041500.50);
524 }
525
526 if ($oldversion < 2016041500.51) {
527
528 // Define table competency_coursecompsetting to be created.
529 $table = new xmldb_table('competency_coursecompsetting');
530
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);
538
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'));
542
543 // Conditionally launch create table for competency_coursecompsetting.
544 if (!$dbman->table_exists($table)) {
545 $dbman->create_table($table);
546 }
547
548 // Main savepoint reached.
549 upgrade_main_savepoint(true, 2016041500.51);
550 }
551
552 if ($oldversion < 2016041500.52) {
553
554 // Define table competency_framework to be created.
555 $table = new xmldb_table('competency_framework');
556
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);
571
572 // Adding keys to table competency_framework.
573 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
574
575 // Adding indexes to table competency_framework.
576 $table->add_index('idnumber', XMLDB_INDEX_UNIQUE, array('idnumber'));
577
578 // Conditionally launch create table for competency_framework.
579 if (!$dbman->table_exists($table)) {
580 $dbman->create_table($table);
581 }
582
583 // Main savepoint reached.
584 upgrade_main_savepoint(true, 2016041500.52);
585 }
586
587 if ($oldversion < 2016041500.53) {
588
589 // Define table competency_coursecomp to be created.
590 $table = new xmldb_table('competency_coursecomp');
591
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);
601
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'));
606
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'));
610
611 // Conditionally launch create table for competency_coursecomp.
612 if (!$dbman->table_exists($table)) {
613 $dbman->create_table($table);
614 }
615
616 // Main savepoint reached.
617 upgrade_main_savepoint(true, 2016041500.53);
618 }
619
620 if ($oldversion < 2016041500.54) {
621
622 // Define table competency_plan to be created.
623 $table = new xmldb_table('competency_plan');
624
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);
639
640 // Adding keys to table competency_plan.
641 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
642
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'));
647
648 // Conditionally launch create table for competency_plan.
649 if (!$dbman->table_exists($table)) {
650 $dbman->create_table($table);
651 }
652
653 // Main savepoint reached.
654 upgrade_main_savepoint(true, 2016041500.54);
655 }
656
657 if ($oldversion < 2016041500.55) {
658
659 // Define table competency_template to be created.
660 $table = new xmldb_table('competency_template');
661
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);
673
674 // Adding keys to table competency_template.
675 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
676
677 // Conditionally launch create table for competency_template.
678 if (!$dbman->table_exists($table)) {
679 $dbman->create_table($table);
680 }
681
682 // Main savepoint reached.
683 upgrade_main_savepoint(true, 2016041500.55);
684 }
685
686 if ($oldversion < 2016041500.56) {
687
688 // Define table competency_templatecomp to be created.
689 $table = new xmldb_table('competency_templatecomp');
690
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);
699
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'));
704
705 // Conditionally launch create table for competency_templatecomp.
706 if (!$dbman->table_exists($table)) {
707 $dbman->create_table($table);
708 }
709
710 // Main savepoint reached.
711 upgrade_main_savepoint(true, 2016041500.56);
712 }
713
714 if ($oldversion < 2016041500.57) {
715
716 // Define table competency_templatecohort to be created.
717 $table = new xmldb_table('competency_templatecohort');
718
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);
726
727 // Adding keys to table competency_templatecohort.
728 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
729
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'));
733
734 // Conditionally launch create table for competency_templatecohort.
735 if (!$dbman->table_exists($table)) {
736 $dbman->create_table($table);
737 }
738
739 // Main savepoint reached.
740 upgrade_main_savepoint(true, 2016041500.57);
741 }
742
743 if ($oldversion < 2016041500.58) {
744
745 // Define table competency_relatedcomp to be created.
746 $table = new xmldb_table('competency_relatedcomp');
747
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);
755
756 // Adding keys to table competency_relatedcomp.
757 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
758
759 // Conditionally launch create table for competency_relatedcomp.
760 if (!$dbman->table_exists($table)) {
761 $dbman->create_table($table);
762 }
763
764 // Main savepoint reached.
765 upgrade_main_savepoint(true, 2016041500.58);
766 }
767
768 if ($oldversion < 2016041500.59) {
769
770 // Define table competency_usercomp to be created.
771 $table = new xmldb_table('competency_usercomp');
772
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);
784
785 // Adding keys to table competency_usercomp.
786 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
787
788 // Adding indexes to table competency_usercomp.
789 $table->add_index('useridcompetency', XMLDB_INDEX_UNIQUE, array('userid', 'competencyid'));
790
791 // Conditionally launch create table for competency_usercomp.
792 if (!$dbman->table_exists($table)) {
793 $dbman->create_table($table);
794 }
795
796 // Main savepoint reached.
797 upgrade_main_savepoint(true, 2016041500.59);
798 }
799
800 if ($oldversion < 2016041500.60) {
801
802 // Define table competency_usercompcourse to be created.
803 $table = new xmldb_table('competency_usercompcourse');
804
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);
815
816 // Adding keys to table competency_usercompcourse.
817 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
818
819 // Adding indexes to table competency_usercompcourse.
820 $table->add_index('useridcoursecomp', XMLDB_INDEX_UNIQUE, array('userid', 'courseid', 'competencyid'));
821
822 // Conditionally launch create table for competency_usercompcourse.
823 if (!$dbman->table_exists($table)) {
824 $dbman->create_table($table);
825 }
826
827 // Main savepoint reached.
828 upgrade_main_savepoint(true, 2016041500.60);
829 }
830
831 if ($oldversion < 2016041500.61) {
832
833 // Define table competency_usercompplan to be created.
834 $table = new xmldb_table('competency_usercompplan');
835
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);
847
848 // Adding keys to table competency_usercompplan.
849 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
850
851 // Adding indexes to table competency_usercompplan.
852 $table->add_index('usercompetencyplan', XMLDB_INDEX_UNIQUE, array('userid', 'competencyid', 'planid'));
853
854 // Conditionally launch create table for competency_usercompplan.
855 if (!$dbman->table_exists($table)) {
856 $dbman->create_table($table);
857 }
858
859 // Main savepoint reached.
860 upgrade_main_savepoint(true, 2016041500.61);
861 }
862
863 if ($oldversion < 2016041500.62) {
864
865 // Define table competency_plancomp to be created.
866 $table = new xmldb_table('competency_plancomp');
867
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);
876
877 // Adding keys to table competency_plancomp.
878 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
879
880 // Adding indexes to table competency_plancomp.
881 $table->add_index('planidcompetencyid', XMLDB_INDEX_UNIQUE, array('planid', 'competencyid'));
882
883 // Conditionally launch create table for competency_plancomp.
884 if (!$dbman->table_exists($table)) {
885 $dbman->create_table($table);
886 }
887
888 // Main savepoint reached.
889 upgrade_main_savepoint(true, 2016041500.62);
890 }
891
892 if ($oldversion < 2016041500.63) {
893
894 // Define table competency_evidence to be created.
895 $table = new xmldb_table('competency_evidence');
896
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);
912
913 // Adding keys to table competency_evidence.
914 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
915
916 // Adding indexes to table competency_evidence.
917 $table->add_index('usercompetencyid', XMLDB_INDEX_NOTUNIQUE, array('usercompetencyid'));
918
919 // Conditionally launch create table for competency_evidence.
920 if (!$dbman->table_exists($table)) {
921 $dbman->create_table($table);
922 }
923
924 // Main savepoint reached.
925 upgrade_main_savepoint(true, 2016041500.63);
926 }
927
928 if ($oldversion < 2016041500.64) {
929
930 // Define table competency_userevidence to be created.
931 $table = new xmldb_table('competency_userevidence');
932
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);
943
944 // Adding keys to table competency_userevidence.
945 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
946
947 // Adding indexes to table competency_userevidence.
948 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
949
950 // Conditionally launch create table for competency_userevidence.
951 if (!$dbman->table_exists($table)) {
952 $dbman->create_table($table);
953 }
954
955 // Main savepoint reached.
956 upgrade_main_savepoint(true, 2016041500.64);
957 }
958
959 if ($oldversion < 2016041500.65) {
960
961 // Define table competency_userevidencecomp to be created.
962 $table = new xmldb_table('competency_userevidencecomp');
963
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);
971
972 // Adding keys to table competency_userevidencecomp.
973 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
974
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'));
978
979 // Conditionally launch create table for competency_userevidencecomp.
980 if (!$dbman->table_exists($table)) {
981 $dbman->create_table($table);
982 }
983
984 // Main savepoint reached.
985 upgrade_main_savepoint(true, 2016041500.65);
986 }
987
988 if ($oldversion < 2016041500.66) {
989
990 // Define table competency_modulecomp to be created.
991 $table = new xmldb_table('competency_modulecomp');
992
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);
1002
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'));
1007
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'));
1011
1012 // Conditionally launch create table for competency_modulecomp.
1013 if (!$dbman->table_exists($table)) {
1014 $dbman->create_table($table);
1015 }
1016
1017 // Main savepoint reached.
1018 upgrade_main_savepoint(true, 2016041500.66);
1019 }
1020
6ab41abd 1021 if ($oldversion < 2016042100.00) {
4ce09314
SL
1022 // Update all countries to upper case.
1023 $DB->execute("UPDATE {user} SET country = UPPER(country)");
1024 // Main savepoint reached.
6ab41abd 1025 upgrade_main_savepoint(true, 2016042100.00);
4ce09314 1026 }
f23e9b6b
CB
1027
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 ];
1059
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 }
1066
8de0b1ab
DW
1067 if ($oldversion < 2016051300.00) {
1068 // Add a default competency rating scale.
1069 make_competence_scale();
1070
1071 // Savepoint reached.
1072 upgrade_main_savepoint(true, 2016051300.00);
1073 }
1074
405b90bc
AG
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)) {
41abbbbd 1078 // MDL-45390. If a grade is being displayed with letters and the grade boundaries are not being adhered to properly
405b90bc
AG
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();
1083
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 }
1090
4da854a6
EL
1091 // Moodle v3.1.0 release upgrade line.
1092 // Put any upgrade step following this.
1093
40a952bf 1094 if ($oldversion < 2016081700.00) {
6a4c2146
DM
1095
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 }
1100
1101 // Savepoint reached.
40a952bf 1102 upgrade_main_savepoint(true, 2016081700.00);
6a4c2146
DM
1103 }
1104
4ae06911
AG
1105 if ($oldversion < 2016081700.02) {
1106 // Default schedule values.
1107 $hour = 0;
1108 $minute = 0;
1109
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 }
1117
1118 // Retrieve the scheduled task record first.
1119 $stattask = $DB->get_record('task_scheduled', array('component' => 'moodle', 'classname' => '\core\task\stats_cron_task'));
1120
1121 // Don't touch customised scheduling.
1122 if ($stattask && !$stattask->customised) {
1123
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');
1142
1143 upgrade_main_savepoint(true, 2016081700.02);
1144 }
1145
cfbba4a9 1146 if ($oldversion < 2016082200.00) {
d7d4a097
JD
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.
1149
1150 // Extend the execution time limit of the script to 2 hours.
1151 upgrade_set_timeout(7200);
1152
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";
1162
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);
1166
1167 // Get the records themselves.
1168 $getduplicatessql = "SELECT qc.id $fromclause ORDER BY minid";
1169 $rs = $DB->get_recordset_sql($getduplicatessql);
1170
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));
1181
1182 // Update progress.
1183 $i++;
1184 $pbar->update($i, $total, "Updating duplicate question category stamp - $i/$total.");
1185 }
1186 unset($usedstamps);
1187
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 }
1194
1195 // Savepoint reached.
cfbba4a9 1196 upgrade_main_savepoint(true, 2016082200.00);
d7d4a097
JD
1197 }
1198
644fcbb6
DW
1199 if ($oldversion < 2016091900.00) {
1200
1201 // Removing the themes from core.
1202 $themes = array('base', 'canvas');
1203
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 }
1210
1211 if (!empty($themes)) {
644fcbb6
DW
1212 // Hacky emulation of plugin uninstallation.
1213 foreach ($themes as $theme) {
1214 unset_all_config_for_plugin('theme_' . $theme);
1215 }
1216 }
1217
1218 // Main savepoint reached.
1219 upgrade_main_savepoint(true, 2016091900.00);
1220 }
1221
cecba1d9 1222 if ($oldversion < 2016091900.02) {
e7ba25f1
MG
1223
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'));
1227
1228 // Conditionally launch drop index attemptstepid-name.
1229 if ($dbman->index_exists($table, $index)) {
1230 $dbman->drop_index($table, $index);
1231 }
1232
1233 // Main savepoint reached.
cecba1d9 1234 upgrade_main_savepoint(true, 2016091900.02);
e7ba25f1
MG
1235 }
1236
b8c241b1 1237 if ($oldversion < 2016100300.00) {
81c9e018
DP
1238 unset_config('enablecssoptimiser');
1239
b8c241b1 1240 upgrade_main_savepoint(true, 2016100300.00);
81c9e018
DP
1241 }
1242
adc77f07 1243 if ($oldversion < 2016100501.00) {
fbcdb0d7
DNA
1244
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');
1248
1249 // Conditionally launch add field enddate.
1250 if (!$dbman->field_exists($table, $field)) {
1251 $dbman->add_field($table, $field);
1252 }
1253
1254 // Main savepoint reached.
adc77f07 1255 upgrade_main_savepoint(true, 2016100501.00);
fbcdb0d7
DNA
1256 }
1257
c8303131 1258 if ($oldversion < 2016101100.00) {
3274d5ca
RW
1259 // Define field component to be added to message.
1260 $table = new xmldb_table('message');
f8e4145f 1261 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'timeusertodeleted');
3274d5ca
RW
1262
1263 // Conditionally launch add field component.
1264 if (!$dbman->field_exists($table, $field)) {
1265 $dbman->add_field($table, $field);
1266 }
1267
1268 // Define field eventtype to be added to message.
1269 $field = new xmldb_field('eventtype', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'component');
1270
1271 // Conditionally launch add field eventtype.
1272 if (!$dbman->field_exists($table, $field)) {
1273 $dbman->add_field($table, $field);
1274 }
1275
f8e4145f 1276 // Main savepoint reached.
c8303131 1277 upgrade_main_savepoint(true, 2016101100.00);
f8e4145f
RW
1278 }
1279
69cbe359 1280
c8303131 1281 if ($oldversion < 2016101101.00) {
3274d5ca
RW
1282 // Define field component to be added to message_read.
1283 $table = new xmldb_table('message_read');
f8e4145f 1284 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'timeusertodeleted');
3274d5ca
RW
1285
1286 // Conditionally launch add field component.
1287 if (!$dbman->field_exists($table, $field)) {
1288 $dbman->add_field($table, $field);
1289 }
1290
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');
1293
1294 // Conditionally launch add field eventtype.
1295 if (!$dbman->field_exists($table, $field)) {
1296 $dbman->add_field($table, $field);
1297 }
1298
1299 // Main savepoint reached.
c8303131 1300 upgrade_main_savepoint(true, 2016101101.00);
3274d5ca
RW
1301 }
1302
0a51a209
DP
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 }
1311
1312 // Savepoint reached.
1313 upgrade_main_savepoint(true, 2016101401.00);
1314 }
1315
4455ff2c 1316 if ($oldversion < 2016101401.02) {
69cbe359
JL
1317 $table = new xmldb_table('external_tokens');
1318 $field = new xmldb_field('privatetoken', XMLDB_TYPE_CHAR, '64', null, null, null, null);
1319
1320 // Conditionally add privatetoken field to the external_tokens table.
1321 if (!$dbman->field_exists($table, $field)) {
1322 $dbman->add_field($table, $field);
1323 }
326d8619 1324
69cbe359 1325 // Main savepoint reached.
4455ff2c 1326 upgrade_main_savepoint(true, 2016101401.02);
69cbe359
JL
1327 }
1328
4d27d62d 1329 if ($oldversion < 2016110202.00) {
159c2c91
AG
1330
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 }
4d27d62d 1341 upgrade_main_savepoint(true, 2016110202.00);
159c2c91
AG
1342 }
1343
bc378b50 1344 if ($oldversion < 2016110300.00) {
9715f61a
AG
1345 // Remove unused admin email setting.
1346 unset_config('emailonlyfromreplyaddress');
1347
1348 // Main savepoint reached.
bc378b50 1349 upgrade_main_savepoint(true, 2016110300.00);
9715f61a
AG
1350 }
1351
b23b9a5b 1352 if ($oldversion < 2016110500.00) {
fab11235
MG
1353
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 ];
1363
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 }
1386
1387 set_config('media_plugins_sortorder', join(',', $enabledplugins));
1388
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 }
1403
1404 // Unset old settings.
1405 foreach ($oldplayers as $oldplayer => $extensions) {
1406 unset_config('core_media_enable_' . $oldplayer);
1407 }
1408
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 }
1417
1418 // Savepoint reached.
b23b9a5b 1419 upgrade_main_savepoint(true, 2016110500.00);
fab11235
MG
1420 }
1421
90abff01 1422 if ($oldversion < 2016110600.00) {
048f909b
JD
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');
1426
1427 // Conditionally launch add field 'deletioninprogress'.
1428 if (!$dbman->field_exists($table, $field)) {
1429 $dbman->add_field($table, $field);
1430 }
1431
1432 // Main savepoint reached.
90abff01 1433 upgrade_main_savepoint(true, 2016110600.00);
048f909b
JD
1434 }
1435
44c3d077 1436 if ($oldversion < 2016112200.01) {
51718b75
DW
1437
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');
1441
1442 // Conditionally launch add field requiredbytheme.
1443 if (!$dbman->field_exists($table, $field)) {
1444 $dbman->add_field($table, $field);
1445 }
1446
1447 // Main savepoint reached.
44c3d077 1448 upgrade_main_savepoint(true, 2016112200.01);
51718b75 1449 }
44c3d077 1450 if ($oldversion < 2016112200.02) {
45ed4271
DW
1451
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);
1456
1457 $params = array('blockname' => 'navigation', 'parentcontextid' => $context->id);
1458 $DB->set_field('block_instances', 'requiredbytheme', 1, $params);
1459 // Main savepoint reached.
44c3d077 1460 upgrade_main_savepoint(true, 2016112200.02);
45ed4271 1461 }
51718b75 1462
75c57a08
EL
1463 // Automatically generated Moodle v3.2.0 release upgrade line.
1464 // Put any upgrade step following this.
1465
66b68d63 1466 if ($oldversion < 2016122800.00) {
b1eb88a4
AG
1467 // Find all roles with the coursecreator archetype.
1468 $coursecreatorroleids = $DB->get_records('role', array('archetype' => 'coursecreator'), '', 'id');
1469
1470 $context = context_system::instance();
1471 $capability = 'moodle/site:configview';
1472
1473 foreach ($coursecreatorroleids as $roleid => $notused) {
1474
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;
1486
1487 $DB->insert_record('role_capabilities', $cap);
1488 }
1489 }
b1eb88a4
AG
1490
1491 // Main savepoint reached.
66b68d63 1492 upgrade_main_savepoint(true, 2016122800.00);
b1eb88a4
AG
1493 }
1494
98be2d20
RW
1495 if ($oldversion < 2017020200.01) {
1496
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'));
1500
1501 // Conditionally launch add index useridfrom_timeuserfromdeleted_notification.
1502 if (!$dbman->index_exists($table, $index)) {
1503 $dbman->add_index($table, $index);
1504 }
1505
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'));
1508
1509 // Conditionally launch add index useridto_timeusertodeleted_notification.
1510 if (!$dbman->index_exists($table, $index)) {
1511 $dbman->add_index($table, $index);
1512 }
1513
1514 $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
1515
1516 // Conditionally launch drop index useridto.
1517 if ($dbman->index_exists($table, $index)) {
1518 $dbman->drop_index($table, $index);
1519 }
1520
1521 // Main savepoint reached.
1522 upgrade_main_savepoint(true, 2017020200.01);
1523 }
1524
1525 if ($oldversion < 2017020200.02) {
1526
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'));
1530
1531 // Conditionally launch add index useridfrom_timeuserfromdeleted_notification.
1532 if (!$dbman->index_exists($table, $index)) {
1533 $dbman->add_index($table, $index);
1534 }
1535
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'));
1538
1539 // Conditionally launch add index useridto_timeusertodeleted_notification.
1540 if (!$dbman->index_exists($table, $index)) {
1541 $dbman->add_index($table, $index);
1542 }
1543
1544 $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
1545
1546 // Conditionally launch drop index useridto.
1547 if ($dbman->index_exists($table, $index)) {
1548 $dbman->drop_index($table, $index);
1549 }
1550
1551 // Main savepoint reached.
1552 upgrade_main_savepoint(true, 2017020200.02);
1553 }
1554
a5722727
MG
1555 if ($oldversion < 2017020901.00) {
1556
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();
1561
1562 // Main savepoint reached.
1563 upgrade_main_savepoint(true, 2017020901.00);
1564 }
1565
f5af2e68 1566 if ($oldversion < 2017021300.00) {
83806582 1567 unset_config('loginpasswordautocomplete');
f5af2e68 1568 upgrade_main_savepoint(true, 2017021300.00);
83806582
JT
1569 }
1570
8341055e
MG
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');
1575
1576 // Conditionally launch add field visibleoncoursepage.
1577 if (!$dbman->field_exists($table, $field)) {
1578 $dbman->add_field($table, $field);
1579 }
1580
1581 // Main savepoint reached.
1582 upgrade_main_savepoint(true, 2017021400.00);
1583 }
1584
342af35a 1585 if ($oldversion < 2017030700.00) {
ca75ec4f
JP
1586
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');
1590
1591 // Conditionally launch add field priority.
1592 if (!$dbman->field_exists($table, $field)) {
1593 $dbman->add_field($table, $field);
1594 }
1595
ca75ec4f 1596 // Main savepoint reached.
342af35a 1597 upgrade_main_savepoint(true, 2017030700.00);
ca75ec4f
JP
1598 }
1599
732bd131
DP
1600 if ($oldversion < 2017031400.00) {
1601
34df779a
AN
1602 // Define table file_conversion to be created.
1603 $table = new xmldb_table('file_conversion');
1604
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);
1617
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'));
1622
1623 // Conditionally launch create table for file_conversion.
1624 if (!$dbman->table_exists($table)) {
1625 $dbman->create_table($table);
1626 }
1627
1628 // Main savepoint reached.
732bd131 1629 upgrade_main_savepoint(true, 2017031400.00);
34df779a
AN
1630 }
1631
2f090f46
EL
1632 if ($oldversion < 2017040400.00) {
1633
4671ae63 1634 // If block_course_overview is no longer present, replace with block_myoverview.
2f090f46
EL
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 }
1638
1639 upgrade_main_savepoint(true, 2017040400.00);
1640 }
1641
1642 if ($oldversion < 2017040401.00) {
1643
4671ae63 1644 // If block_course_overview is no longer present, remove it.
2f090f46 1645 // Note - we do not need to completely remove the block context etc because we
4671ae63
RW
1646 // have replaced all occurrences of block_course_overview with block_myoverview
1647 // in the upgrade step above.
2f090f46
EL
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 }
1656
1657 upgrade_main_savepoint(true, 2017040401.00);
1658 }
1659
1660 if ($oldversion < 2017040402.00) {
1661
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');
1666
1667 // Conditionally launch add field.
1668 if (!$dbman->field_exists($table, $fieldtype)) {
1669 $dbman->add_field($table, $fieldtype);
1670 }
1671
1672 // Conditionally launch add field.
1673 if (!$dbman->field_exists($table, $fieldtimesort)) {
1674 $dbman->add_field($table, $fieldtimesort);
1675 }
1676
1677 // Now, define the index we will be adding.
1678 $index = new xmldb_index('type-timesort', XMLDB_INDEX_NOTUNIQUE, array('type', 'timesort'));
1679
1680 // Conditionally launch add index.
1681 if (!$dbman->index_exists($table, $index)) {
1682 $dbman->add_index($table, $index);
1683 }
1684
1685 upgrade_main_savepoint(true, 2017040402.00);
1686 }
1687
2f090f46 1688 if ($oldversion < 2017040700.01) {
60237253 1689
2b09b2da 1690 // Define table oauth2_issuer to be created.
60237253
DW
1691 $table = new xmldb_table('oauth2_issuer');
1692
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);
485a22fc
DW
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);
c21a66e4 1707 $table->add_field('alloweddomains', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
60237253
DW
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');
eca128bf 1710 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
60237253
DW
1711 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1712
1713 // Adding keys to table oauth2_issuer.
1714 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1715
1716 // Conditionally launch create table for oauth2_issuer.
1717 if (!$dbman->table_exists($table)) {
1718 $dbman->create_table($table);
1719 }
1720
1721 // Main savepoint reached.
2f090f46 1722 upgrade_main_savepoint(true, 2017040700.01);
60237253
DW
1723 }
1724
2f090f46 1725 if ($oldversion < 2017040700.02) {
60237253
DW
1726
1727 // Define table oauth2_endpoint to be created.
1728 $table = new xmldb_table('oauth2_endpoint');
1729
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);
1738
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'));
1742
1743 // Conditionally launch create table for oauth2_endpoint.
1744 if (!$dbman->table_exists($table)) {
1745 $dbman->create_table($table);
1746 }
1747
1748 // Main savepoint reached.
2f090f46 1749 upgrade_main_savepoint(true, 2017040700.02);
60237253
DW
1750 }
1751
2f090f46 1752 if ($oldversion < 2017040700.03) {
60237253
DW
1753
1754 // Define table oauth2_system_account to be created.
1755 $table = new xmldb_table('oauth2_system_account');
1756
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);
28dddbc1
DW
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);
60237253
DW
1767
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'));
1771
1772 // Conditionally launch create table for oauth2_system_account.
1773 if (!$dbman->table_exists($table)) {
1774 $dbman->create_table($table);
1775 }
1776
1777 // Main savepoint reached.
2f090f46 1778 upgrade_main_savepoint(true, 2017040700.03);
60237253
DW
1779 }
1780
2f090f46 1781 if ($oldversion < 2017040700.04) {
60237253 1782
2b09b2da 1783 // Define table oauth2_user_field_mapping to be created.
8445556b
DW
1784 $table = new xmldb_table('oauth2_user_field_mapping');
1785
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);
1794
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'));
8445556b
DW
1798 $table->add_key('uniqinternal', XMLDB_KEY_UNIQUE, array('issuerid', 'internalfield'));
1799
1800 // Conditionally launch create table for oauth2_user_field_mapping.
1801 if (!$dbman->table_exists($table)) {
1802 $dbman->create_table($table);
1803 }
1804
60237253 1805 // Main savepoint reached.
2f090f46 1806 upgrade_main_savepoint(true, 2017040700.04);
e6661428
JP
1807 }
1808
7f53e8aa
MG
1809 if ($oldversion < 2017041801.00) {
1810
1811 // Define table course_completion_defaults to be created.
1812 $table = new xmldb_table('course_completion_defaults');
1813
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);
1823
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'));
1828
1829 // Adding indexes to table course_completion_defaults.
1830 $table->add_index('coursemodule', XMLDB_INDEX_UNIQUE, array('course', 'module'));
1831
1832 // Conditionally launch create table for course_completion_defaults.
1833 if (!$dbman->table_exists($table)) {
1834 $dbman->create_table($table);
1835 }
1836
1837 upgrade_main_savepoint(true, 2017041801.00);
1838 }
1839
c3b1178d
JP
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 }
1857
1858 // Main savepoint reached.
1859 upgrade_main_savepoint(true, 2017050500.01);
1860 }
1861
021a1439
JD
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]);
1872
1873 // Main savepoint reached.
1874 upgrade_main_savepoint(true, 2017050500.02);
1875 }
1876
79b80ee5
CB
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';
1882
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);
1887
1888 // Main savepoint reached.
1889 upgrade_main_savepoint(true, 2017050900.01);
1890 }
1891
5e272283
EL
1892 // Automatically generated Moodle v3.3.0 release upgrade line.
1893 // Put any upgrade step following this.
1894
a52d3abb 1895 if ($oldversion < 2017061201.00) {
4ddf7c60
DG
1896 $table = new xmldb_table('course_sections');
1897 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'availability');
1898
f25e2b1d 1899 // Define a field 'timemodified' in the 'course_sections' table.
4ddf7c60
DG
1900 if (!$dbman->field_exists($table, $field)) {
1901 $dbman->add_field($table, $field);
1902 }
1903
a52d3abb 1904 upgrade_main_savepoint(true, 2017061201.00);
4ddf7c60
DG
1905 }
1906
56990700 1907 if ($oldversion < 2017061301.00) {
c8f2e0e9
MN
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 }
1912
1913 // Main savepoint reached.
56990700 1914 upgrade_main_savepoint(true, 2017061301.00);
c8f2e0e9
MN
1915 }
1916
04d1f776 1917 if ($oldversion < 2017071000.00) {
859e2033
DW
1918
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');
1922
1923 // Conditionally launch add field requireconfirmation.
1924 if (!$dbman->field_exists($table, $field)) {
1925 $dbman->add_field($table, $field);
1926 }
1927
1928 // Main savepoint reached.
04d1f776 1929 upgrade_main_savepoint(true, 2017071000.00);
859e2033
DW
1930 }
1931
1a54672f 1932 if ($oldversion < 2017071001.00) {
557554f9 1933
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');
1938
1939 // Conditionally launch add field timemodified.
1940 if (!$dbman->field_exists($table, $field)) {
1941 $dbman->add_field($table, $field);
1942
1943 // Set field to current time.
1944 $DB->set_field('block_instances', 'timemodified', time());
1945
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');
1949
1950 // Launch change of nullability for field timemodified.
1951 $dbman->change_field_notnull($table, $field);
1952
1953 // Define index timemodified (not unique) to be added to block_instances.
1954 $index = new xmldb_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1955
1956 // Conditionally launch add index timemodified.
1957 if (!$dbman->index_exists($table, $index)) {
1958 $dbman->add_index($table, $index);
1959 }
1960 }
1961
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');
1965
1966 // Conditionally launch add field timecreated.
1967 if (!$dbman->field_exists($table, $field)) {
1968 $dbman->add_field($table, $field);
1969
1970 // Set field to current time.
1971 $DB->set_field('block_instances', 'timecreated', time());
1972
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');
1976
1977 // Launch change of nullability for field timecreated.
1978 $dbman->change_field_notnull($table, $field);
1979 }
1980
1981 // Main savepoint reached.
1a54672f 1982 upgrade_main_savepoint(true, 2017071001.00);
557554f9 1983 }
1984
f84bdb43 1985 if ($oldversion < 2017071100.00 ) {
f83d212b
EL
1986 // Clean old upgrade setting not used anymore.
1987 unset_config('upgrade_minmaxgradestepignored');
f84bdb43 1988 upgrade_main_savepoint(true, 2017071100.00);
f83d212b
EL
1989 }
1990
e10b29ed 1991 if ($oldversion < 2017072000.02) {
8473b735
DM
1992
1993 // Define table analytics_models to be created.
1994 $table = new xmldb_table('analytics_models');
1995
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);
2007
2008 // Adding keys to table analytics_models.
2009 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2010
2011 // Adding indexes to table analytics_models.
2012 $table->add_index('enabledandtrained', XMLDB_INDEX_NOTUNIQUE, array('enabled', 'trained'));
2013
2014 // Conditionally launch create table for analytics_models.
2015 if (!$dbman->table_exists($table)) {
2016 $dbman->create_table($table);
2017 }
2018
2019 // Define table analytics_models_log to be created.
2020 $table = new xmldb_table('analytics_models_log');
2021
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);
2034
2035 // Adding keys to table analytics_models_log.
2036 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2037
2038 // Adding indexes to table analytics_models_log.
2039 $table->add_index('modelid', XMLDB_INDEX_NOTUNIQUE, array('modelid'));
2040
2041 // Conditionally launch create table for analytics_models_log.
2042 if (!$dbman->table_exists($table)) {
2043 $dbman->create_table($table);
2044 }
2045
2046 // Define table analytics_predictions to be created.
2047 $table = new xmldb_table('analytics_predictions');
2048
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');
2059
2060 // Adding keys to table analytics_predictions.
2061 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2062
2063 // Adding indexes to table analytics_predictions.
2064 $table->add_index('modelidandcontextid', XMLDB_INDEX_NOTUNIQUE, array('modelid', 'contextid'));
2065
2066 // Conditionally launch create table for analytics_predictions.
2067 if (!$dbman->table_exists($table)) {
2068 $dbman->create_table($table);
2069 }
2070
2071 // Define table analytics_train_samples to be created.
2072 $table = new xmldb_table('analytics_train_samples');
2073
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);
2834ea96 2078 $table->add_field('timesplitting', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
8473b735
DM
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');
2082
2083 // Adding keys to table analytics_train_samples.
2084 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2085
2086 // Adding indexes to table analytics_train_samples.
413f19bc
DM
2087 $table->add_index('modelidandanalysableidandtimesplitting', XMLDB_INDEX_NOTUNIQUE,
2088 array('modelid', 'analysableid', 'timesplitting'));
8473b735
DM
2089
2090 // Conditionally launch create table for analytics_train_samples.
2091 if (!$dbman->table_exists($table)) {
2092 $dbman->create_table($table);
2093 }
2094
2095 // Define table analytics_predict_ranges to be created.
2096 $table = new xmldb_table('analytics_predict_ranges');
2097
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);
2834ea96 2102 $table->add_field('timesplitting', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
8473b735
DM
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');
2105
2106 // Adding keys to table analytics_predict_ranges.
2107 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2108
2109 // Adding indexes to table analytics_predict_ranges.
413f19bc
DM
2110 $table->add_index('modelidandanalysableidandtimesplitting', XMLDB_INDEX_NOTUNIQUE,
2111 array('modelid', 'analysableid', 'timesplitting'));
8473b735
DM
2112
2113 // Conditionally launch create table for analytics_predict_ranges.
2114 if (!$dbman->table_exists($table)) {
2115 $dbman->create_table($table);
2116 }
2117
2118 // Define table analytics_used_files to be created.
2119 $table = new xmldb_table('analytics_used_files');
2120
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');
2127
2128 // Adding keys to table analytics_used_files.
2129 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2130
2131 // Adding indexes to table analytics_used_files.
2132 $table->add_index('modelidandfileidandaction', XMLDB_INDEX_NOTUNIQUE, array('modelid', 'fileid', 'action'));
2133
2134 // Conditionally launch create table for analytics_used_files.
2135 if (!$dbman->table_exists($table)) {
2136 $dbman->create_table($table);
2137 }
2138
ee345551
DM
2139 $now = time();
2140 $admin = get_admin();
e10b29ed
DM
2141
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 }
2197
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 }
e709e544 2212
8473b735 2213 // Main savepoint reached.
e10b29ed 2214 upgrade_main_savepoint(true, 2017072000.02);
8473b735
DM
2215 }
2216
3fa588c6
JD
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');
2221
2222 // Launch change of nullability for field email.
2223 $dbman->change_field_notnull($table, $field);
2224
2225 // Main savepoint reached.
2226 upgrade_main_savepoint(true, 2017072700.01);
2227 }
2228
2c04945c 2229 if ($oldversion < 2017072700.02) {
7d792aee
MG
2230
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);
2237
2238 // Update existing registration.
2239 $DB->execute("UPDATE {registration_hubs} SET hubname = ?, huburl = ? WHERE huburl = ?",
2240 ['Moodle.net', $newhuburl, $oldhuburl]);
2241
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;
9d18cfd0
MG
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 }
7d792aee
MG
2254 }
2255
2256 // Update published courses.
2257 $DB->execute('UPDATE {course_published} SET huburl = ? WHERE huburl = ?', [$newhuburl, $oldhuburl]);
2258
2259 // Main savepoint reached.
2c04945c 2260 upgrade_main_savepoint(true, 2017072700.02);
7d792aee
MG
2261 }
2262
d5a99c45 2263 if ($oldversion < 2017080700.01) {
00da1e60
DM
2264
2265 // Get the table by its previous name.
2266 $table = new xmldb_table('analytics_predict_ranges');
2267 if ($dbman->table_exists($table)) {
2268
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');
2273
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');
2276
2277 // Conditionally launch add field sampleids.
2278 if (!$dbman->field_exists($table, $field)) {
2279 $dbman->add_field($table, $field);
2280 }
2281
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');
2284
2285 // Conditionally launch add field timemodified.
2286 if (!$dbman->field_exists($table, $field)) {
2287 $dbman->add_field($table, $field);
2288 }
2289
2290 // Rename the table to its new name.
2291 $dbman->rename_table($table, 'analytics_predict_samples');
2292 }
2293
d5a99c45
DM
2294 $table = new xmldb_table('analytics_predict_samples');
2295
2296 $index = new xmldb_index('modelidandanalysableidandtimesplitting', XMLDB_INDEX_NOTUNIQUE,
2297 array('modelid', 'analysableid', 'timesplitting'));
2298
2299 // Conditionally launch drop index.
2300 if ($dbman->index_exists($table, $index)) {
2301 $dbman->drop_index($table, $index);
2302 }
2303
2304 $index = new xmldb_index('modelidandanalysableidandtimesplittingandrangeindex', XMLDB_INDEX_NOTUNIQUE,
2305 array('modelid', 'analysableid', 'timesplitting', 'rangeindex'));
2306
2307 // Conditionally launch add index.
2308 if (!$dbman->index_exists($table, $index)) {
2309 $dbman->add_index($table, $index);
2310 }
2311
00da1e60 2312 // Main savepoint reached.
d5a99c45 2313 upgrade_main_savepoint(true, 2017080700.01);
00da1e60
DM
2314 }
2315
1bf96e23
AN
2316 if ($oldversion < 2017082200.00) {
2317 $plugins = ['radius', 'fc', 'nntp', 'pam', 'pop3', 'imap'];
2318
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 }
2329
a7417383 2330 if ($oldversion < 2017082200.01) {
0690a271
DM
2331
2332 // Define table analytics_indicator_calc to be created.
2333 $table = new xmldb_table('analytics_indicator_calc');
2334
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);
2345
2346 // Adding keys to table analytics_indicator_calc.
2347 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2348
2349 // Adding indexes to table analytics_indicator_calc.
2350 $table->add_index('starttime-endtime-contextid', XMLDB_INDEX_NOTUNIQUE, array('starttime', 'endtime', 'contextid'));
2351
2352 // Conditionally launch create table for analytics_indicator_calc.
2353 if (!$dbman->table_exists($table)) {
2354 $dbman->create_table($table);
2355 }
2356
2357 // Main savepoint reached.
a7417383 2358 upgrade_main_savepoint(true, 2017082200.01);
0690a271
DM
2359 }
2360
fc7467e5 2361 if ($oldversion < 2017082300.01) {
e46fde42
JO
2362
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 }
2372
2373 // Main savepoint reached.
fc7467e5 2374 upgrade_main_savepoint(true, 2017082300.01);
e46fde42
JO
2375 }
2376
7948dae1
MN
2377 if ($oldversion < 2017082500.00) {
2378 // Handle FKs for the table 'analytics_models_log'.
2379 $table = new xmldb_table('analytics_models_log');
2380
2381 // Remove the existing index before adding FK (which creates an index).
2382 $index = new xmldb_index('modelid', XMLDB_INDEX_NOTUNIQUE, array('modelid'));
2383
2384 // Conditionally launch drop index.
2385 if ($dbman->index_exists($table, $index)) {
2386 $dbman->drop_index($table, $index);
2387 }
2388
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);
2392
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);
2397
2398 $key = new xmldb_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2399 $dbman->add_key($table, $key);
2400
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);
2405
2406 $key = new xmldb_key('fileid', XMLDB_KEY_FOREIGN, array('fileid'), 'files', array('id'));
2407 $dbman->add_key($table, $key);
2408
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);
2413
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);
2418
2419 $key = new xmldb_key('fileid', XMLDB_KEY_FOREIGN, array('fileid'), 'files', array('id'));
2420 $dbman->add_key($table, $key);
2421
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);
2426
2427 // Main savepoint reached.
2428 upgrade_main_savepoint(true, 2017082500.00);
2429 }
2430
25f24df7 2431 if ($oldversion < 2017082800.00) {
0f5fa169
DM
2432
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');
2436
2437 // Launch change of type for field prediction.
2438 $dbman->change_field_type($table, $field);
2439
2440 // Main savepoint reached.
25f24df7 2441 upgrade_main_savepoint(true, 2017082800.00);
0f5fa169
DM
2442 }
2443
025363d1
DM
2444 if ($oldversion < 2017090700.01) {
2445
2446 // Define table analytics_prediction_actions to be created.
2447 $table = new xmldb_table('analytics_prediction_actions');
2448
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);
2455
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'));
2460
2461 // Adding indexes to table analytics_prediction_actions.
2462 $table->add_index('predictionidanduseridandactionname', XMLDB_INDEX_NOTUNIQUE,
2463 array('predictionid', 'userid', 'actionname'));
2464
2465 // Conditionally launch create table for analytics_prediction_actions.
2466 if (!$dbman->table_exists($table)) {
2467 $dbman->create_table($table);
2468 }
2469
2470 // Main savepoint reached.
2471 upgrade_main_savepoint(true, 2017090700.01);
2472 }
2473
77d2c7b9 2474 if ($oldversion < 2017091200.00) {
d08f1770
DM
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');
2478
2479 // Main savepoint reached.
77d2c7b9 2480 upgrade_main_savepoint(true, 2017091200.00);
d08f1770
DM
2481 }
2482
59f431e8 2483 if ($oldversion < 2017091201.00) {
7e4c4b6f
AN
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');
2487
2488 // Conditionally launch add field userid.
2489 if (!$dbman->field_exists($table, $field)) {
2490 $dbman->add_field($table, $field);
2491 }
2492
2493 $key = new xmldb_key('useriduser', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2494
2495 // Launch add key userid_user.
2496 $dbman->add_key($table, $key);
2497
2498 // Main savepoint reached.
59f431e8 2499 upgrade_main_savepoint(true, 2017091201.00);
7e4c4b6f
AN
2500 }
2501
9d18cfd0
MG
2502 if ($oldversion < 2017092201.00) {
2503
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 }
2511
2512 // Main savepoint reached.
2513 upgrade_main_savepoint(true, 2017092201.00);
2514 }
2515
18348b07 2516 if ($oldversion < 2017092202.00) {
f3a3e234
AA
2517
2518 if (!file_exists($CFG->dirroot . '/blocks/messages/block_messages.php')) {
2519
2520 // Delete instances.
2521 $instances = $DB->get_records_list('block_instances', 'blockname', ['messages']);
2522 $instanceids = array_keys($instances);
2523
2524 if (!empty($instanceids)) {
2525 $DB->delete_records_list('block_positions', 'blockinstanceid', $instanceids);
2526 $DB->delete_records_list('block_instances', 'id', $instanceids);
2527 list($sql, $params) = $DB->get_in_or_equal($instanceids, SQL_PARAMS_NAMED);
2528 $params['contextlevel'] = CONTEXT_BLOCK;
2529 $DB->delete_records_select('context', "contextlevel=:contextlevel AND instanceid " . $sql, $params);
2530
2531 $preferences = array();
2532 foreach ($instances as $instanceid => $instance) {
2533 $preferences[] = 'block' . $instanceid . 'hidden';
2534 $preferences[] = 'docked_block_instance_' . $instanceid;
2535 }
2536 $DB->delete_records_list('user_preferences', 'name', $preferences);
2537 }
2538
2539 // Delete the block from the block table.
2540 $DB->delete_records('block', array('name' => 'messages'));
2541
2542 // Remove capabilities.
2543 capabilities_cleanup('block_messages');
2544
2545 // Clean config.
2546 unset_all_config_for_plugin('block_messages');
2547 }
2548
839aa86f 2549 // Main savepoint reached.
18348b07 2550 upgrade_main_savepoint(true, 2017092202.00);
f3a3e234
AA
2551 }
2552
839aa86f
AN
2553 if ($oldversion < 2017092700.00) {
2554
ef467fff
MG
2555 // Rename several fields in registration data to match the names of the properties that are sent to moodle.net.
2556 $renames = [
2557 'site_address_httpsmoodlenet' => 'site_street_httpsmoodlenet',
2558 'site_region_httpsmoodlenet' => 'site_regioncode_httpsmoodlenet',
2559 'site_country_httpsmoodlenet' => 'site_countrycode_httpsmoodlenet'];
2560 foreach ($renames as $oldparamname => $newparamname) {
2561 try {
2562 $DB->execute("UPDATE {config_plugins} SET name = ? WHERE name = ? AND plugin = ?",
2563 [$newparamname, $oldparamname, 'hub']);
2564 } catch (dml_exception $e) {
2565 // Exception can happen if the config value with the new name already exists, ignore it and move on.
2566 }
2567 }
2568
2569 // Main savepoint reached.
839aa86f 2570 upgrade_main_savepoint(true, 2017092700.00);
ef467fff
MG
2571 }
2572
7b1e5e6f
AN
2573 if ($oldversion < 2017092900.00) {
2574 // Define field categoryid to be added to event.
2575 $table = new xmldb_table('event');
2576 $field = new xmldb_field('categoryid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'format');
2577
2578 // Conditionally launch add field categoryid.
2579 if (!$dbman->field_exists($table, $field)) {
2580 $dbman->add_field($table, $field);
2581 }
2582
2583 // Add the categoryid key.
2584 $key = new xmldb_key('categoryid', XMLDB_KEY_FOREIGN, array('categoryid'), 'course_categories', array('id'));
2585 $dbman->add_key($table, $key);
2586
2587 // Add a new index for groupid/courseid/categoryid/visible/userid.
2588 // Do this before we remove the old index.
2589 $index = new xmldb_index('groupid-courseid-categoryid-visible-userid', XMLDB_INDEX_NOTUNIQUE, array('groupid', 'courseid', 'categoryid', 'visible', 'userid'));
2590 if (!$dbman->index_exists($table, $index)) {
2591 $dbman->add_index($table, $index);
2592 }
2593
2594 // Drop the old index.
2595 $index = new xmldb_index('groupid-courseid-visible-userid', XMLDB_INDEX_NOTUNIQUE, array('groupid', 'courseid', 'visible', 'userid'));
2596 if ($dbman->index_exists($table, $index)) {
2597 $dbman->drop_index($table, $index);
2598 }
2599
2600 // Main savepoint reached.
2601 upgrade_main_savepoint(true, 2017092900.00);
2602 }
2603
a4cdd6d2 2604 return true;
51003653 2605}