MDL-53700 competency: Migrate capabilities to core
[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
EL
94 // Always keep this upgrade step with version being the minimum
95 // allowed version to upgrade from (v2.7.0 right now).
96 if ($oldversion < 2014051200) {
97 // Just in case somebody hacks upgrade scripts or env, we really can not continue.
98 echo("You need to upgrade to 2.7.x or higher first!\n");
5c79b8ed 99 exit(1);
e8c82aac
EL
100 // Note this savepoint is 100% unreachable, but needed to pass the upgrade checks.
101 upgrade_main_savepoint(true, 2014051200);
5c79b8ed
PS
102 }
103
d277c441 104 // MDL-32543 Make sure that the log table has correct length for action and url fields.
9febfdc5 105 if ($oldversion < 2014051200.02) {
d277c441
DNA
106
107 $table = new xmldb_table('log');
108
109 $columns = $DB->get_columns('log');
110 if ($columns['action']->max_length < 40) {
111 $index1 = new xmldb_index('course-module-action', XMLDB_INDEX_NOTUNIQUE, array('course', 'module', 'action'));
112 if ($dbman->index_exists($table, $index1)) {
113 $dbman->drop_index($table, $index1);
114 }
115 $index2 = new xmldb_index('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
116 if ($dbman->index_exists($table, $index2)) {
117 $dbman->drop_index($table, $index2);
118 }
119 $field = new xmldb_field('action', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'cmid');
120 $dbman->change_field_precision($table, $field);
121 $dbman->add_index($table, $index1);
122 $dbman->add_index($table, $index2);
123 }
124
125 if ($columns['url']->max_length < 100) {
126 $field = new xmldb_field('url', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'action');
127 $dbman->change_field_precision($table, $field);
128 }
129
9febfdc5 130 upgrade_main_savepoint(true, 2014051200.02);
d277c441
DNA
131 }
132
1bce3a70
RT
133 if ($oldversion < 2014060300.00) {
134 $gspath = get_config('assignfeedback_editpdf', 'gspath');
135 if ($gspath !== false) {
136 set_config('pathtogs', $gspath);
137 unset_config('gspath', 'assignfeedback_editpdf');
138 }
139 upgrade_main_savepoint(true, 2014060300.00);
140 }
141
b5725a97
JF
142 if ($oldversion < 2014061000.00) {
143 // Fixing possible wrong MIME type for Publisher files.
144 $filetypes = array('%.pub'=>'application/x-mspublisher');
145 upgrade_mimetypes($filetypes);
146 upgrade_main_savepoint(true, 2014061000.00);
147 }
148
1170df12 149 if ($oldversion < 2014062600.01) {
e3e4ea97
MN
150 // We only want to delete DragMath if the directory no longer exists. If the directory
151 // is present then it means it has been restored, so do not perform the uninstall.
152 if (!check_dir_exists($CFG->libdir . '/editor/tinymce/plugins/dragmath', false)) {
153 // Purge DragMath plugin which is incompatible with GNU GPL license.
154 unset_all_config_for_plugin('tinymce_dragmath');
155 }
1170df12
PS
156
157 // Main savepoint reached.
158 upgrade_main_savepoint(true, 2014062600.01);
159 }
160
463882d0 161 // Switch the order of the fields in the files_reference index, to improve the performance of search_references.
9b192823 162 if ($oldversion < 2014070100.00) {
463882d0
DS
163 $table = new xmldb_table('files_reference');
164 $index = new xmldb_index('uq_external_file', XMLDB_INDEX_UNIQUE, array('repositoryid', 'referencehash'));
165 if ($dbman->index_exists($table, $index)) {
166 $dbman->drop_index($table, $index);
167 }
9b192823 168 upgrade_main_savepoint(true, 2014070100.00);
463882d0 169 }
9b192823
DP
170
171 if ($oldversion < 2014070101.00) {
463882d0
DS
172 $table = new xmldb_table('files_reference');
173 $index = new xmldb_index('uq_external_file', XMLDB_INDEX_UNIQUE, array('referencehash', 'repositoryid'));
174 if (!$dbman->index_exists($table, $index)) {
175 $dbman->add_index($table, $index);
176 }
9b192823 177 upgrade_main_savepoint(true, 2014070101.00);
463882d0
DS
178 }
179
a1bacaf1
JL
180 if ($oldversion < 2014072400.01) {
181 $table = new xmldb_table('user_devices');
182 $oldindex = new xmldb_index('pushid-platform', XMLDB_KEY_UNIQUE, array('pushid', 'platform'));
183 if ($dbman->index_exists($table, $oldindex)) {
184 $key = new xmldb_key('pushid-platform', XMLDB_KEY_UNIQUE, array('pushid', 'platform'));
185 $dbman->drop_key($table, $key);
186 }
187 upgrade_main_savepoint(true, 2014072400.01);
188 }
189
bbea9369
TH
190 if ($oldversion < 2014080801.00) {
191
192 // Define index behaviour (not unique) to be added to question_attempts.
193 $table = new xmldb_table('question_attempts');
194 $index = new xmldb_index('behaviour', XMLDB_INDEX_NOTUNIQUE, array('behaviour'));
195
196 // Conditionally launch add index behaviour.
197 if (!$dbman->index_exists($table, $index)) {
198 $dbman->add_index($table, $index);
199 }
200
201 // Main savepoint reached.
202 upgrade_main_savepoint(true, 2014080801.00);
203 }
204
cfa5bd1f
MS
205 if ($oldversion < 2014082900.01) {
206 // Fixing possible wrong MIME type for 7-zip and Rar files.
207 $filetypes = array(
208 '%.7z' => 'application/x-7z-compressed',
209 '%.rar' => 'application/x-rar-compressed');
210 upgrade_mimetypes($filetypes);
211 upgrade_main_savepoint(true, 2014082900.01);
212 }
213
061e6b28 214 if ($oldversion < 2014082900.02) {
215 // Replace groupmembersonly usage with new availability system.
216 $transaction = $DB->start_delegated_transaction();
217 if ($CFG->enablegroupmembersonly) {
218 // If it isn't already enabled, we need to enable availability.
219 if (!$CFG->enableavailability) {
220 set_config('enableavailability', 1);
221 }
222
223 // Count all course-modules with groupmembersonly set (for progress
224 // bar).
225 $total = $DB->count_records('course_modules', array('groupmembersonly' => 1));
226 $pbar = new progress_bar('upgradegroupmembersonly', 500, true);
227
228 // Get all these course-modules, one at a time.
229 $rs = $DB->get_recordset('course_modules', array('groupmembersonly' => 1),
230 'course, id');
231 $i = 0;
232 foreach ($rs as $cm) {
233 // Calculate and set new availability value.
234 $availability = upgrade_group_members_only($cm->groupingid, $cm->availability);
235 $DB->set_field('course_modules', 'availability', $availability,
236 array('id' => $cm->id));
237
238 // Update progress.
239 $i++;
240 $pbar->update($i, $total, "Upgrading groupmembersonly settings - $i/$total.");
241 }
242 $rs->close();
243 }
244
245 // Define field groupmembersonly to be dropped from course_modules.
246 $table = new xmldb_table('course_modules');
247 $field = new xmldb_field('groupmembersonly');
248
249 // Conditionally launch drop field groupmembersonly.
250 if ($dbman->field_exists($table, $field)) {
251 $dbman->drop_field($table, $field);
252 }
253
254 // Unset old config variable.
255 unset_config('enablegroupmembersonly');
256 $transaction->allow_commit();
257
258 upgrade_main_savepoint(true, 2014082900.02);
259 }
260
db34f8cb 261 if ($oldversion < 2014100100.00) {
6c0bfb14
AN
262
263 // Define table messageinbound_handlers to be created.
264 $table = new xmldb_table('messageinbound_handlers');
265
266 // Adding fields to table messageinbound_handlers.
267 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
268 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
269 $table->add_field('classname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
270 $table->add_field('defaultexpiration', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '86400');
271 $table->add_field('validateaddress', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1');
272 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
273
274 // Adding keys to table messageinbound_handlers.
275 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
276 $table->add_key('classname', XMLDB_KEY_UNIQUE, array('classname'));
277
278 // Conditionally launch create table for messageinbound_handlers.
279 if (!$dbman->table_exists($table)) {
280 $dbman->create_table($table);
281 }
282
283 // Define table messageinbound_datakeys to be created.
284 $table = new xmldb_table('messageinbound_datakeys');
285
286 // Adding fields to table messageinbound_datakeys.
287 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
288 $table->add_field('handler', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
289 $table->add_field('datavalue', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
290 $table->add_field('datakey', XMLDB_TYPE_CHAR, '64', null, null, null, null);
291 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
292 $table->add_field('expires', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
293
294 // Adding keys to table messageinbound_datakeys.
295 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
296 $table->add_key('handler_datavalue', XMLDB_KEY_UNIQUE, array('handler', 'datavalue'));
297 $table->add_key('handler', XMLDB_KEY_FOREIGN, array('handler'), 'messageinbound_handlers', array('id'));
298
299 // Conditionally launch create table for messageinbound_datakeys.
300 if (!$dbman->table_exists($table)) {
301 $dbman->create_table($table);
302 }
303
304 // Main savepoint reached.
db34f8cb 305 upgrade_main_savepoint(true, 2014100100.00);
6c0bfb14
AN
306 }
307
f5bacd65 308 if ($oldversion < 2014100600.01) {
a1740d7b
DW
309 // Define field aggregationstatus to be added to grade_grades.
310 $table = new xmldb_table('grade_grades');
311 $field = new xmldb_field('aggregationstatus', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, 'unknown', 'timemodified');
312
313 // Conditionally launch add field aggregationstatus.
314 if (!$dbman->field_exists($table, $field)) {
315 $dbman->add_field($table, $field);
316 }
317
318 $field = new xmldb_field('aggregationweight', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'aggregationstatus');
319
320 // Conditionally launch add field aggregationweight.
321 if (!$dbman->field_exists($table, $field)) {
322 $dbman->add_field($table, $field);
323 }
324
677bc073
JO
325 // Define field aggregationcoef2 to be added to grade_items.
326 $table = new xmldb_table('grade_items');
dcfc196a 327 $field = new xmldb_field('aggregationcoef2', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, '0', 'aggregationcoef');
677bc073
JO
328
329 // Conditionally launch add field aggregationcoef2.
330 if (!$dbman->field_exists($table, $field)) {
331 $dbman->add_field($table, $field);
332 }
333
45da5361 334 $field = new xmldb_field('weightoverride', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'needsupdate');
dcfc196a
AG
335
336 // Conditionally launch add field weightoverride.
45da5361
AD
337 if (!$dbman->field_exists($table, $field)) {
338 $dbman->add_field($table, $field);
339 }
340
dcfc196a 341 // Main savepoint reached.
f5bacd65 342 upgrade_main_savepoint(true, 2014100600.01);
45da5361 343 }
677bc073 344
f5bacd65 345 if ($oldversion < 2014100600.02) {
e8e1d0a3
FM
346
347 // Define field aggregationcoef2 to be added to grade_items_history.
348 $table = new xmldb_table('grade_items_history');
349 $field = new xmldb_field('aggregationcoef2', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, '0', 'aggregationcoef');
350
351 // Conditionally launch add field aggregationcoef2.
352 if (!$dbman->field_exists($table, $field)) {
353 $dbman->add_field($table, $field);
354 }
355
356 // Main savepoint reached.
f5bacd65 357 upgrade_main_savepoint(true, 2014100600.02);
e8e1d0a3
FM
358 }
359
f5bacd65 360 if ($oldversion < 2014100600.03) {
e8e1d0a3
FM
361
362 // Define field weightoverride to be added to grade_items_history.
363 $table = new xmldb_table('grade_items_history');
364 $field = new xmldb_field('weightoverride', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'decimals');
365
366 // Conditionally launch add field weightoverride.
367 if (!$dbman->field_exists($table, $field)) {
368 $dbman->add_field($table, $field);
369 }
370
371 // Main savepoint reached.
f5bacd65 372 upgrade_main_savepoint(true, 2014100600.03);
e8e1d0a3 373 }
f5bacd65 374 if ($oldversion < 2014100600.04) {
a9937aec
DW
375 // Set flags so we can display a notice on all courses that might
376 // be affected by the uprade to natural aggregation.
377 if (!get_config('grades_sumofgrades_upgrade_flagged', 'core')) {
378 // 13 == SUM_OF_GRADES.
379 $sql = 'SELECT DISTINCT courseid
380 FROM {grade_categories}
381 WHERE aggregation = ?';
382 $courses = $DB->get_records_sql($sql, array(13));
383
384 foreach ($courses as $course) {
385 set_config('show_sumofgrades_upgrade_' . $course->courseid, 1);
7970c217
AG
386 // Set each of the grade items to needing an update so that when the user visits the grade reports the
387 // figures will be updated.
388 $DB->set_field('grade_items', 'needsupdate', 1, array('courseid' => $course->courseid));
a9937aec
DW
389 }
390
391 set_config('grades_sumofgrades_upgrade_flagged', 1);
392 }
393
394 // Main savepoint reached.
f5bacd65 395 upgrade_main_savepoint(true, 2014100600.04);
a9937aec 396 }
e8e1d0a3 397
94cdabcd 398 if ($oldversion < 2014100700.00) {
77c0a68d
AN
399
400 // Define table messageinbound_messagelist to be created.
401 $table = new xmldb_table('messageinbound_messagelist');
402
403 // Adding fields to table messageinbound_messagelist.
404 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
405 $table->add_field('messageid', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
406 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
407 $table->add_field('address', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
408 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
409
410 // Adding keys to table messageinbound_messagelist.
411 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
412 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
413
414 // Conditionally launch create table for messageinbound_messagelist.
415 if (!$dbman->table_exists($table)) {
416 $dbman->create_table($table);
417 }
418
419 // Main savepoint reached.
94cdabcd 420 upgrade_main_savepoint(true, 2014100700.00);
77c0a68d
AN
421 }
422
4a813f39 423 if ($oldversion < 2014100700.01) {
80f98467
MG
424
425 // Define field visible to be added to cohort.
426 $table = new xmldb_table('cohort');
427 $field = new xmldb_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'descriptionformat');
428
429 // Conditionally launch add field visible.
430 if (!$dbman->field_exists($table, $field)) {
431 $dbman->add_field($table, $field);
432 }
433
434 // Main savepoint reached.
4a813f39 435 upgrade_main_savepoint(true, 2014100700.01);
80f98467
MG
436 }
437
70b37098 438 if ($oldversion < 2014100800.00) {
a75fa4c0
TH
439 // Remove qformat_learnwise (unless it has manually been added back).
440 if (!file_exists($CFG->dirroot . '/question/format/learnwise/format.php')) {
441 unset_all_config_for_plugin('qformat_learnwise');
442 }
443
444 // Main savepoint reached.
70b37098 445 upgrade_main_savepoint(true, 2014100800.00);
a75fa4c0
TH
446 }
447
00fd3c1a
FM
448 if ($oldversion < 2014101001.00) {
449 // Some blocks added themselves to the my/ home page, but they did not declare the
450 // subpage of the default my home page. While the upgrade script has been fixed, this
451 // upgrade script will fix the data that was wrongly added.
452
453 // We only proceed if we can find the right entry from my_pages. Private => 1 refers to
454 // the constant value MY_PAGE_PRIVATE.
455 if ($systempage = $DB->get_record('my_pages', array('userid' => null, 'private' => 1))) {
456
457 // Select the blocks there could have been automatically added. showinsubcontexts is hardcoded to 0
458 // because it is possible for administrators to have forced it on the my/ page by adding it to the
459 // system directly rather than updating the default my/ page.
460 $blocks = array('course_overview', 'private_files', 'online_users', 'badges', 'calendar_month', 'calendar_upcoming');
461 list($blocksql, $blockparams) = $DB->get_in_or_equal($blocks, SQL_PARAMS_NAMED);
462 $select = "parentcontextid = :contextid
463 AND pagetypepattern = :page
464 AND showinsubcontexts = 0
465 AND subpagepattern IS NULL
466 AND blockname $blocksql";
467 $params = array(
468 'contextid' => context_system::instance()->id,
469 'page' => 'my-index'
470 );
471 $params = array_merge($params, $blockparams);
472
473 $DB->set_field_select(
474 'block_instances',
475 'subpagepattern',
476 $systempage->id,
477 $select,
478 $params
479 );
480 }
481
482 // Main savepoint reached.
483 upgrade_main_savepoint(true, 2014101001.00);
484 }
485
47d6e6a7
DW
486 if ($oldversion < 2014102000.00) {
487
488 // Define field aggregatesubcats to be dropped from grade_categories.
489 $table = new xmldb_table('grade_categories');
490 $field = new xmldb_field('aggregatesubcats');
491
492 // Conditionally launch drop field aggregatesubcats.
493 if ($dbman->field_exists($table, $field)) {
494
495 $sql = 'SELECT DISTINCT courseid
496 FROM {grade_categories}
497 WHERE aggregatesubcats = ?';
498 $courses = $DB->get_records_sql($sql, array(1));
499
500 foreach ($courses as $course) {
501 set_config('show_aggregatesubcats_upgrade_' . $course->courseid, 1);
502 // Set each of the grade items to needing an update so that when the user visits the grade reports the
503 // figures will be updated.
504 $DB->set_field('grade_items', 'needsupdate', 1, array('courseid' => $course->courseid));
505 }
506
507
508 $dbman->drop_field($table, $field);
509 }
510
511 // Main savepoint reached.
512 upgrade_main_savepoint(true, 2014102000.00);
513 }
514
219ebf9b 515 if ($oldversion < 2014110300.00) {
941de296
MG
516 // Run script restoring missing folder records for draft file areas.
517 upgrade_fix_missing_root_folders_draft();
518
519 // Main savepoint reached.
219ebf9b 520 upgrade_main_savepoint(true, 2014110300.00);
941de296
MG
521 }
522
147bae8f
EL
523 // Moodle v2.8.0 release upgrade line.
524 // Put any upgrade step following this.
525
ebea19cb
FM
526 if ($oldversion < 2014111000.00) {
527 // Coming from 2.7 or older, we need to flag the step minmaxgrade to be ignored.
528 set_config('upgrade_minmaxgradestepignored', 1);
316c560c
AG
529 // Coming from 2.7 or older, we need to flag the step for changing calculated grades to be regraded.
530 set_config('upgrade_calculatedgradeitemsonlyregrade', 1);
d8ee5712
DM
531
532 // Main savepoint reached.
533 upgrade_main_savepoint(true, 2014111000.00);
ebea19cb 534 }
6c4e70a6 535
91bae397 536 if ($oldversion < 2014120100.00) {
93539c44
FM
537
538 // Define field sslverification to be added to mnet_host.
539 $table = new xmldb_table('mnet_host');
540 $field = new xmldb_field('sslverification', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'applicationid');
541
542 // Conditionally launch add field sslverification.
543 if (!$dbman->field_exists($table, $field)) {
544 $dbman->add_field($table, $field);
545 }
546
547 // Main savepoint reached.
91bae397 548 upgrade_main_savepoint(true, 2014120100.00);
93539c44
FM
549 }
550
b2ab6cc3 551 if ($oldversion < 2014120101.00) {
93539c44 552
686d0561
SH
553 // Define field component to be added to comments.
554 $table = new xmldb_table('comments');
555 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'contextid');
556
557 // Conditionally launch add field component.
558 if (!$dbman->field_exists($table, $field)) {
559 $dbman->add_field($table, $field);
560 }
561
562 // Main savepoint reached.
b2ab6cc3 563 upgrade_main_savepoint(true, 2014120101.00);
686d0561 564 }
6c4e70a6
SH
565
566 if ($oldversion < 2014120102.00) {
1d658535
PS
567
568 // Define table user_password_history to be created.
569 $table = new xmldb_table('user_password_history');
570
571 // Adding fields to table user_password_history.
572 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
573 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
574 $table->add_field('hash', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
575 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
576
577 // Adding keys to table user_password_history.
578 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
579 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
580
581 // Conditionally launch create table for user_password_history.
582 if (!$dbman->table_exists($table)) {
583 $dbman->create_table($table);
584 }
585
586 // Main savepoint reached.
6c4e70a6 587 upgrade_main_savepoint(true, 2014120102.00);
1d658535
PS
588 }
589
c93e6c5c
AA
590 if ($oldversion < 2015010800.01) {
591
592 // Make sure the private files handler is not set to expire.
593 $DB->set_field('messageinbound_handlers', 'defaultexpiration', 0,
594 array('classname' => '\core\message\inbound\private_files_handler'));
595
596 // Main savepoint reached.
597 upgrade_main_savepoint(true, 2015010800.01);
94b8c636
FM
598
599 }
600
e6b18165 601 if ($oldversion < 2015012600.00) {
94b8c636
FM
602
603 // If the site is using internal and external storage, or just external
604 // storage, and the external path specified is empty we change the setting
605 // to internal only. That is how the backup code is handling this
606 // misconfiguration.
6c855719
MN
607 $storage = (int) get_config('backup', 'backup_auto_storage');
608 $folder = get_config('backup', 'backup_auto_destination');
94b8c636
FM
609 if ($storage !== 0 && empty($folder)) {
610 set_config('backup_auto_storage', 0, 'backup');
611 }
612
613 // Main savepoint reached.
e6b18165 614 upgrade_main_savepoint(true, 2015012600.00);
c93e6c5c
AA
615 }
616
5bc12ca2
SB
617 if ($oldversion < 2015012600.01) {
618
619 // Convert calendar_lookahead to nearest new value.
620 $value = $DB->get_field('config', 'value', array('name' => 'calendar_lookahead'));
621 if ($value > 90) {
622 set_config('calendar_lookahead', '120');
623 } else if ($value > 60 and $value < 90) {
624 set_config('calendar_lookahead', '90');
625 } else if ($value > 30 and $value < 60) {
626 set_config('calendar_lookahead', '60');
627 } else if ($value > 21 and $value < 30) {
628 set_config('calendar_lookahead', '30');
629 } else if ($value > 14 and $value < 21) {
630 set_config('calendar_lookahead', '21');
631 } else if ($value > 7 and $value < 14) {
632 set_config('calendar_lookahead', '14');
633 }
634
635 // Main savepoint reached.
636 upgrade_main_savepoint(true, 2015012600.01);
637 }
806c06fc 638
d3dac400 639 if ($oldversion < 2015021100.00) {
806c06fc
AD
640
641 // Define field timemodified to be added to registration_hubs.
642 $table = new xmldb_table('registration_hubs');
d3dac400 643 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'secret');
806c06fc
AD
644
645 // Conditionally launch add field timemodified.
646 if (!$dbman->field_exists($table, $field)) {
647 $dbman->add_field($table, $field);
648 }
649
650 // Main savepoint reached.
d3dac400 651 upgrade_main_savepoint(true, 2015021100.00);
806c06fc
AD
652 }
653
e6c453bb 654 if ($oldversion < 2015022401.00) {
8b52863f
AN
655
656 // Define index useridfromto (not unique) to be added to message.
657 $table = new xmldb_table('message');
658 $index = new xmldb_index('useridfromto', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'useridto'));
659
660 // Conditionally launch add index useridfromto.
661 if (!$dbman->index_exists($table, $index)) {
662 $dbman->add_index($table, $index);
663 }
664
665 // Define index useridfromto (not unique) to be added to message_read.
666 $table = new xmldb_table('message_read');
667 $index = new xmldb_index('useridfromto', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'useridto'));
668
669 // Conditionally launch add index useridfromto.
670 if (!$dbman->index_exists($table, $index)) {
671 $dbman->add_index($table, $index);
672 }
673
674 // Main savepoint reached.
e6c453bb 675 upgrade_main_savepoint(true, 2015022401.00);
8b52863f
AN
676 }
677
db23cd08 678 if ($oldversion < 2015022500.00) {
3221718e
JL
679 $table = new xmldb_table('user_devices');
680 $index = new xmldb_index('uuid-userid', XMLDB_INDEX_NOTUNIQUE, array('uuid', 'userid'));
681 if (!$dbman->index_exists($table, $index)) {
682 $dbman->add_index($table, $index);
683 }
db23cd08 684 upgrade_main_savepoint(true, 2015022500.00);
3221718e
JL
685 }
686
858e92fe
AD
687 if ($oldversion < 2015030400.00) {
688 // We have long since switched to storing timemodified per hub rather than a single 'registered' timestamp.
689 unset_config('registered');
690 upgrade_main_savepoint(true, 2015030400.00);
691 }
692
42d81ef7 693 if ($oldversion < 2015031100.00) {
c803fa68
EM
694 // Unset old config variable.
695 unset_config('enabletgzbackups');
696
42d81ef7 697 upgrade_main_savepoint(true, 2015031100.00);
c803fa68
EM
698 }
699
1f50bbf1
AN
700 if ($oldversion < 2015031400.00) {
701
702 // Define index useridfrom (not unique) to be dropped form message.
703 $table = new xmldb_table('message');
704 $index = new xmldb_index('useridfrom', XMLDB_INDEX_NOTUNIQUE, array('useridfrom'));
705
706 // Conditionally launch drop index useridfrom.
707 if ($dbman->index_exists($table, $index)) {
708 $dbman->drop_index($table, $index);
709 }
710
711 // Define index useridfrom (not unique) to be dropped form message_read.
712 $table = new xmldb_table('message_read');
713 $index = new xmldb_index('useridfrom', XMLDB_INDEX_NOTUNIQUE, array('useridfrom'));
714
715 // Conditionally launch drop index useridfrom.
716 if ($dbman->index_exists($table, $index)) {
717 $dbman->drop_index($table, $index);
718 }
719
720 // Main savepoint reached.
721 upgrade_main_savepoint(true, 2015031400.00);
722 }
723
24aac597
AD
724 if ($oldversion < 2015031900.01) {
725 unset_config('crontime', 'registration');
726 upgrade_main_savepoint(true, 2015031900.01);
727 }
728
3784d3be
YB
729 if ($oldversion < 2015032000.00) {
730 $table = new xmldb_table('badge_criteria');
3784d3be 731
fffeb03f 732 $field = new xmldb_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
3784d3be
YB
733 // Conditionally add description field to the badge_criteria table.
734 if (!$dbman->field_exists($table, $field)) {
735 $dbman->add_field($table, $field);
736 }
737
fffeb03f
YB
738 $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, 2, null, XMLDB_NOTNULL, null, 0);
739 // Conditionally add description format field to the badge_criteria table.
740 if (!$dbman->field_exists($table, $field)) {
741 $dbman->add_field($table, $field);
742 }
743
3784d3be
YB
744 upgrade_main_savepoint(true, 2015032000.00);
745 }
746
d6e7a63d
PS
747 if ($oldversion < 2015040200.01) {
748 // Force uninstall of deleted tool.
749 if (!file_exists("$CFG->dirroot/$CFG->admin/tool/timezoneimport")) {
750 // Remove capabilities.
751 capabilities_cleanup('tool_timezoneimport');
752 // Remove all other associated config.
753 unset_all_config_for_plugin('tool_timezoneimport');
754 }
755 upgrade_main_savepoint(true, 2015040200.01);
756 }
757
758 if ($oldversion < 2015040200.02) {
759 // Define table timezone to be dropped.
760 $table = new xmldb_table('timezone');
761 // Conditionally launch drop table for timezone.
762 if ($dbman->table_exists($table)) {
763 $dbman->drop_table($table);
764 }
765 upgrade_main_savepoint(true, 2015040200.02);
766 }
767
768 if ($oldversion < 2015040200.03) {
769 if (isset($CFG->timezone) and $CFG->timezone == 99) {
770 // Migrate to real server timezone.
771 unset_config('timezone');
772 }
773 upgrade_main_savepoint(true, 2015040200.03);
774 }
775
75ece778 776 if ($oldversion < 2015040700.01) {
6398ff53 777 $DB->delete_records('config_plugins', array('name' => 'requiremodintro'));
75ece778 778 upgrade_main_savepoint(true, 2015040700.01);
6398ff53
AH
779 }
780
72324523 781 if ($oldversion < 2015040900.01) {
c9451960 782 // Add "My grades" to the user menu.
e4522f88
DW
783 $oldconfig = get_config('core', 'customusermenuitems');
784 if (strpos("mygrades,grades|/grade/report/mygrades.php|grades", $oldconfig) === false) {
2b7b0bae 785 $newconfig = "mygrades,grades|/grade/report/mygrades.php|grades\n" . $oldconfig;
e4522f88
DW
786 set_config('customusermenuitems', $newconfig);
787 }
c9451960 788
72324523 789 upgrade_main_savepoint(true, 2015040900.01);
c9451960
AG
790 }
791
72324523 792 if ($oldversion < 2015040900.02) {
e4522f88
DW
793 // Update the default user menu (add preferences, remove my files and my badges).
794 $oldconfig = get_config('core', 'customusermenuitems');
795
796 // Add "My preferences" at the end.
797 if (strpos($oldconfig, "mypreferences,moodle|/user/preference.php|preferences") === false) {
798 $newconfig = $oldconfig . "\nmypreferences,moodle|/user/preferences.php|preferences";
799 } else {
800 $newconfig = $oldconfig;
801 }
802 // Remove my files.
803 $newconfig = str_replace("myfiles,moodle|/user/files.php|download", "", $newconfig);
804 // Remove my badges.
805 $newconfig = str_replace("mybadges,badges|/badges/mybadges.php|award", "", $newconfig);
806 // Remove holes.
807 $newconfig = preg_replace('/\n+/', "\n", $newconfig);
808 $newconfig = preg_replace('/(\r\n)+/', "\n", $newconfig);
809 set_config('customusermenuitems', $newconfig);
810
72324523 811 upgrade_main_savepoint(true, 2015040900.02);
e4522f88
DW
812 }
813
4887d152
JO
814 if ($oldversion < 2015050400.00) {
815 $config = get_config('core', 'customusermenuitems');
816
817 // Change "My preferences" in the user menu to "Preferences".
818 $config = str_replace("mypreferences,moodle|/user/preferences.php|preferences",
819 "preferences,moodle|/user/preferences.php|preferences", $config);
820
821 // Change "My grades" in the user menu to "Grades".
822 $config = str_replace("mygrades,grades|/grade/report/mygrades.php|grades",
823 "grades,grades|/grade/report/mygrades.php|grades", $config);
824
825 set_config('customusermenuitems', $config);
826
827 upgrade_main_savepoint(true, 2015050400.00);
828 }
829
6df7f200 830 if ($oldversion < 2015050401.00) {
3c568cd3
DW
831 // Make sure we have messages in the user menu because it's no longer in the nav tree.
832 $oldconfig = get_config('core', 'customusermenuitems');
35ea78d2
JO
833 $messagesconfig = "messages,message|/message/index.php|message";
834 $preferencesconfig = "preferences,moodle|/user/preferences.php|preferences";
3c568cd3
DW
835
836 // See if it exists.
35ea78d2
JO
837 if (strpos($oldconfig, $messagesconfig) === false) {
838 // See if preferences exists.
839 if (strpos($oldconfig, "preferences,moodle|/user/preferences.php|preferences") !== false) {
840 // Insert it before preferences.
841 $newconfig = str_replace($preferencesconfig, $messagesconfig . "\n" . $preferencesconfig, $oldconfig);
3c568cd3
DW
842 } else {
843 // Custom config - we can only insert it at the end.
35ea78d2 844 $newconfig = $oldconfig . "\n" . $messagesconfig;
3c568cd3
DW
845 }
846 set_config('customusermenuitems', $newconfig);
847 }
848
d3f1cde5 849 upgrade_main_savepoint(true, 2015050401.00);
3c568cd3 850 }
6df7f200 851
b758ab30
EL
852 // Moodle v2.9.0 release upgrade line.
853 // Put any upgrade step following this.
854
408e3812 855 if ($oldversion < 2015060400.02) {
ebea19cb
FM
856
857 // Sites that were upgrading from 2.7 and older will ignore this step.
858 if (empty($CFG->upgrade_minmaxgradestepignored)) {
859
860 upgrade_minmaxgrade();
861
862 // Flags this upgrade step as already run to prevent it from running multiple times.
863 set_config('upgrade_minmaxgradestepignored', 1);
c07775df
EM
864 }
865
408e3812 866 upgrade_main_savepoint(true, 2015060400.02);
c07775df
EM
867 }
868
156d0486
MG
869 if ($oldversion < 2015061900.00) {
870 // MDL-49257. Changed the algorithm of calculating automatic weights of extra credit items.
871
872 // Before the change, in case when grade category (in "Natural" agg. method) had items with
873 // overridden weights, the automatic weight of extra credit items was illogical.
874 // In order to prevent grades changes after the upgrade we need to freeze gradebook calculation
875 // for the affected courses.
876
877 // This script in included in each major version upgrade process so make sure we don't run it twice.
878 if (empty($CFG->upgrade_extracreditweightsstepignored)) {
879 upgrade_extra_credit_weightoverride();
880
881 // To skip running the same script on the upgrade to the next major release.
882 set_config('upgrade_extracreditweightsstepignored', 1);
883 }
884
885 // Main savepoint reached.
886 upgrade_main_savepoint(true, 2015061900.00);
887 }
888
4d4dcc27
AG
889 if ($oldversion < 2015062500.01) {
890 // MDL-48239. Changed calculated grade items so that the maximum and minimum grade can be set.
891
892 // If the changes are accepted and a regrade is done on the gradebook then some grades may change significantly.
893 // This is here to freeze the gradebook in affected courses.
894
895 // This script is included in each major version upgrade process so make sure we don't run it twice.
896 if (empty($CFG->upgrade_calculatedgradeitemsignored)) {
897 upgrade_calculated_grade_items();
898
899 // To skip running the same script on the upgrade to the next major release.
900 set_config('upgrade_calculatedgradeitemsignored', 1);
4359caa2
DW
901 // This config value is never used again.
902 unset_config('upgrade_calculatedgradeitemsonlyregrade');
4d4dcc27
AG
903 }
904
905 // Main savepoint reached.
906 upgrade_main_savepoint(true, 2015062500.01);
907 }
908
0305798c
AG
909 if ($oldversion < 2015081300.01) {
910
911 // Define field importtype to be added to grade_import_values.
912 $table = new xmldb_table('grade_import_values');
913 $field = new xmldb_field('importonlyfeedback', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'importer');
914
915 // Conditionally launch add field importtype.
916 if (!$dbman->field_exists($table, $field)) {
917 $dbman->add_field($table, $field);
918 }
919
920 // Main savepoint reached.
921 upgrade_main_savepoint(true, 2015081300.01);
922 }
923
482ebc17 924 if ($oldversion < 2015082400.00) {
04c24a55
AN
925
926 // Define table webdav_locks to be dropped.
927 $table = new xmldb_table('webdav_locks');
928
929 // Conditionally launch drop table for webdav_locks.
930 if ($dbman->table_exists($table)) {
931 $dbman->drop_table($table);
932 }
933
934 // Main savepoint reached.
482ebc17 935 upgrade_main_savepoint(true, 2015082400.00);
04c24a55
AN
936 }
937
e45f622b 938 if ($oldversion < 2015090200.00) {
1efd3238
MN
939 $table = new xmldb_table('message');
940
941 // Define the deleted fields to be added to the message tables.
942 $field1 = new xmldb_field('timeuserfromdeleted', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0',
943 'timecreated');
944 $field2 = new xmldb_field('timeusertodeleted', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0',
945 'timecreated');
946 $oldindex = new xmldb_index('useridfromto', XMLDB_INDEX_NOTUNIQUE,
947 array('useridfrom', 'useridto'));
948 $newindex = new xmldb_index('useridfromtodeleted', XMLDB_INDEX_NOTUNIQUE,
949 array('useridfrom', 'useridto', 'timeuserfromdeleted', 'timeusertodeleted'));
950
951 // Conditionally launch add field timeuserfromdeleted.
952 if (!$dbman->field_exists($table, $field1)) {
953 $dbman->add_field($table, $field1);
954 }
955
956 // Conditionally launch add field timeusertodeleted.
957 if (!$dbman->field_exists($table, $field2)) {
958 $dbman->add_field($table, $field2);
959 }
960
961 // Conditionally launch drop index useridfromto.
962 if ($dbman->index_exists($table, $oldindex)) {
963 $dbman->drop_index($table, $oldindex);
964 }
965
966 // Conditionally launch add index useridfromtodeleted.
967 if (!$dbman->index_exists($table, $newindex)) {
968 $dbman->add_index($table, $newindex);
969 }
970
971 // Now add them to the message_read table.
972 $table = new xmldb_table('message_read');
973
974 // Conditionally launch add field timeuserfromdeleted.
975 if (!$dbman->field_exists($table, $field1)) {
976 $dbman->add_field($table, $field1);
977 }
978
979 // Conditionally launch add field timeusertodeleted.
980 if (!$dbman->field_exists($table, $field2)) {
981 $dbman->add_field($table, $field2);
982 }
983
984 // Conditionally launch drop index useridfromto.
985 if ($dbman->index_exists($table, $oldindex)) {
986 $dbman->drop_index($table, $oldindex);
987 }
988
989 // Conditionally launch add index useridfromtodeleted.
990 if (!$dbman->index_exists($table, $newindex)) {
991 $dbman->add_index($table, $newindex);
992 }
993
994 // Main savepoint reached.
e45f622b 995 upgrade_main_savepoint(true, 2015090200.00);
1efd3238
MN
996 }
997
9e66dff0 998 if ($oldversion < 2015090801.00) {
0d1e5456
MG
999 // This upgrade script merges all tag instances pointing to the same course tag.
1000 // User id is no longer used for those tag instances.
1001 upgrade_course_tags();
1002
1003 // If configuration variable "Show course tags" is set, disable the block
1004 // 'tags' because it can not be used for tagging courses any more.
1005 if (!empty($CFG->block_tags_showcoursetags)) {
1006 if ($record = $DB->get_record('block', array('name' => 'tags'), 'id, visible')) {
1007 if ($record->visible) {
1008 $DB->update_record('block', array('id' => $record->id, 'visible' => 0));
1009 }
1010 }
1011 }
1012
1013 // Define index idname (unique) to be dropped form tag (it's really weird).
1014 $table = new xmldb_table('tag');
1015 $index = new xmldb_index('idname', XMLDB_INDEX_UNIQUE, array('id', 'name'));
1016
1017 // Conditionally launch drop index idname.
1018 if ($dbman->index_exists($table, $index)) {
1019 $dbman->drop_index($table, $index);
1020 }
1021
1022 // Main savepoint reached.
9e66dff0 1023 upgrade_main_savepoint(true, 2015090801.00);
0d1e5456
MG
1024 }
1025
16d08d46 1026 if ($oldversion < 2015092200.00) {
581e6637
DS
1027 // Define index qtype (not unique) to be added to question.
1028 $table = new xmldb_table('question');
1029 $index = new xmldb_index('qtype', XMLDB_INDEX_NOTUNIQUE, array('qtype'));
1030
1031 // Conditionally launch add index qtype.
1032 if (!$dbman->index_exists($table, $index)) {
1033 $dbman->add_index($table, $index);
1034 }
1035
1036 // Main savepoint reached.
16d08d46 1037 upgrade_main_savepoint(true, 2015092200.00);
581e6637
DS
1038 }
1039
a3b1172d 1040 if ($oldversion < 2015092900.00) {
2b7c85da
JPG
1041 // Rename backup_auto_keep setting to backup_auto_max_kept.
1042 $keep = get_config('backup', 'backup_auto_keep');
1043 if ($keep !== false) {
1044 set_config('backup_auto_max_kept', $keep, 'backup');
1045 unset_config('backup_auto_keep', 'backup');
1046 }
1047
1048 // Main savepoint reached.
a3b1172d 1049 upgrade_main_savepoint(true, 2015092900.00);
2b7c85da
JPG
1050 }
1051
05e6215c 1052 if ($oldversion < 2015100600.00) {
60b69e4d
PFO
1053
1054 // Define index notification (not unique) to be added to message_read.
1055 $table = new xmldb_table('message_read');
1056 $index = new xmldb_index('notificationtimeread', XMLDB_INDEX_NOTUNIQUE, array('notification', 'timeread'));
1057
1058 // Conditionally launch add index notification.
1059 if (!$dbman->index_exists($table, $index)) {
1060 $dbman->add_index($table, $index);
1061 }
1062
1063 // Main savepoint reached.
05e6215c 1064 upgrade_main_savepoint(true, 2015100600.00);
60b69e4d
PFO
1065 }
1066
b0fc7898
DM
1067 if ($oldversion < 2015100800.01) {
1068 // The only flag for preventing all plugins installation features is
1069 // now $CFG->disableupdateautodeploy in config.php.
1070 unset_config('updateautodeploy');
1071 upgrade_main_savepoint(true, 2015100800.01);
1072 }
1073
6d29c4ac
EL
1074 // Moodle v3.0.0 release upgrade line.
1075 // Put any upgrade step following this.
1076
67f68027 1077 if ($oldversion < 2016011300.01) {
c026a28d
MG
1078
1079 // This is a big upgrade script. We create new table tag_coll and the field
1080 // tag.tagcollid pointing to it.
1081
1082 // Define table tag_coll to be created.
1083 $table = new xmldb_table('tag_coll');
1084
1085 // Adding fields to table tagcloud.
1086 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1087 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null);
1088 $table->add_field('isdefault', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
1089 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1090 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '5', null, XMLDB_NOTNULL, null, '0');
1091 $table->add_field('searchable', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
1092 $table->add_field('customurl', XMLDB_TYPE_CHAR, '255', null, null, null, null);
1093
1094 // Adding keys to table tagcloud.
1095 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1096
1097 // Conditionally launch create table for tagcloud.
1098 if (!$dbman->table_exists($table)) {
1099 $dbman->create_table($table);
1100 }
1101
1102 // Table {tag}.
1103 // Define index name (unique) to be dropped form tag - we will replace it with index on (tagcollid,name) later.
1104 $table = new xmldb_table('tag');
1105 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
1106
1107 // Conditionally launch drop index name.
1108 if ($dbman->index_exists($table, $index)) {
1109 $dbman->drop_index($table, $index);
1110 }
1111
1112 // Define field tagcollid to be added to tag, we create it as null first and will change to notnull later.
1113 $table = new xmldb_table('tag');
1114 $field = new xmldb_field('tagcollid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'userid');
1115
1116 // Conditionally launch add field tagcloudid.
1117 if (!$dbman->field_exists($table, $field)) {
1118 $dbman->add_field($table, $field);
1119 }
1120
1121 // Main savepoint reached.
67f68027 1122 upgrade_main_savepoint(true, 2016011300.01);
c026a28d
MG
1123 }
1124
67f68027 1125 if ($oldversion < 2016011300.02) {
c026a28d
MG
1126 // Create a default tag collection if not exists and update the field tag.tagcollid to point to it.
1127 if (!$tcid = $DB->get_field_sql('SELECT id FROM {tag_coll} ORDER BY isdefault DESC, sortorder, id', null,
1128 IGNORE_MULTIPLE)) {
1129 $tcid = $DB->insert_record('tag_coll', array('isdefault' => 1, 'sortorder' => 0));
1130 }
1131 $DB->execute('UPDATE {tag} SET tagcollid = ? WHERE tagcollid IS NULL', array($tcid));
1132
1133 // Define index tagcollname (unique) to be added to tag.
1134 $table = new xmldb_table('tag');
1135 $index = new xmldb_index('tagcollname', XMLDB_INDEX_UNIQUE, array('tagcollid', 'name'));
1136 $field = new xmldb_field('tagcollid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'userid');
1137
1138 // Conditionally launch add index tagcollname.
1139 if (!$dbman->index_exists($table, $index)) {
1140 // Launch change of nullability for field tagcollid.
1141 $dbman->change_field_notnull($table, $field);
1142 $dbman->add_index($table, $index);
1143 }
1144
1145 // Define key tagcollid (foreign) to be added to tag.
1146 $table = new xmldb_table('tag');
1147 $key = new xmldb_key('tagcollid', XMLDB_KEY_FOREIGN, array('tagcollid'), 'tag_coll', array('id'));
1148
1149 // Launch add key tagcloudid.
1150 $dbman->add_key($table, $key);
1151
1152 // Define index tagcolltype (not unique) to be added to tag.
1153 $table = new xmldb_table('tag');
1154 $index = new xmldb_index('tagcolltype', XMLDB_INDEX_NOTUNIQUE, array('tagcollid', 'tagtype'));
1155
1156 // Conditionally launch add index tagcolltype.
1157 if (!$dbman->index_exists($table, $index)) {
1158 $dbman->add_index($table, $index);
1159 }
1160
1161 // Main savepoint reached.
67f68027 1162 upgrade_main_savepoint(true, 2016011300.02);
c026a28d
MG
1163 }
1164
67f68027 1165 if ($oldversion < 2016011300.03) {
c026a28d
MG
1166
1167 // Define table tag_area to be created.
1168 $table = new xmldb_table('tag_area');
1169
1170 // Adding fields to table tag_area.
1171 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1172 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1173 $table->add_field('itemtype', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1174 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1');
1175 $table->add_field('tagcollid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1176 $table->add_field('callback', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1177 $table->add_field('callbackfile', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1178
1179 // Adding keys to table tag_area.
1180 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1181 $table->add_key('tagcollid', XMLDB_KEY_FOREIGN, array('tagcollid'), 'tag_coll', array('id'));
1182
1183 // Adding indexes to table tag_area.
1184 $table->add_index('compitemtype', XMLDB_INDEX_UNIQUE, array('component', 'itemtype'));
1185
1186 // Conditionally launch create table for tag_area.
1187 if (!$dbman->table_exists($table)) {
1188 $dbman->create_table($table);
1189 }
1190
1191 // Main savepoint reached.
67f68027 1192 upgrade_main_savepoint(true, 2016011300.03);
c026a28d
MG
1193 }
1194
67f68027 1195 if ($oldversion < 2016011300.04) {
c026a28d
MG
1196
1197 // Define index itemtype-itemid-tagid-tiuserid (unique) to be dropped form tag_instance.
1198 $table = new xmldb_table('tag_instance');
1199 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid', XMLDB_INDEX_UNIQUE,
1200 array('itemtype', 'itemid', 'tagid', 'tiuserid'));
1201
1202 // Conditionally launch drop index itemtype-itemid-tagid-tiuserid.
1203 if ($dbman->index_exists($table, $index)) {
1204 $dbman->drop_index($table, $index);
1205 }
1206
1207 // Main savepoint reached.
67f68027 1208 upgrade_main_savepoint(true, 2016011300.04);
c026a28d
MG
1209 }
1210
67f68027 1211 if ($oldversion < 2016011300.05) {
c026a28d
MG
1212
1213 $DB->execute("UPDATE {tag_instance} SET component = ? WHERE component IS NULL", array(''));
1214
1215 // Changing nullability of field component on table tag_instance to not null.
1216 $table = new xmldb_table('tag_instance');
1217 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'tagid');
1218
1219 // Launch change of nullability for field component.
1220 $dbman->change_field_notnull($table, $field);
1221
1222 // Changing type of field itemtype on table tag_instance to char.
1223 $table = new xmldb_table('tag_instance');
1224 $field = new xmldb_field('itemtype', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'component');
1225
1226 // Launch change of type for field itemtype.
1227 $dbman->change_field_type($table, $field);
1228
1229 // Main savepoint reached.
67f68027 1230 upgrade_main_savepoint(true, 2016011300.05);
c026a28d
MG
1231 }
1232
67f68027 1233 if ($oldversion < 2016011300.06) {
c026a28d
MG
1234
1235 // Define index taggeditem (unique) to be added to tag_instance.
1236 $table = new xmldb_table('tag_instance');
1237 $index = new xmldb_index('taggeditem', XMLDB_INDEX_UNIQUE, array('component', 'itemtype', 'itemid', 'tiuserid', 'tagid'));
1238
1239 // Conditionally launch add index taggeditem.
1240 if (!$dbman->index_exists($table, $index)) {
1241 $dbman->add_index($table, $index);
1242 }
1243
1244 // Main savepoint reached.
67f68027 1245 upgrade_main_savepoint(true, 2016011300.06);
c026a28d
MG
1246 }
1247
67f68027 1248 if ($oldversion < 2016011300.07) {
c026a28d
MG
1249
1250 // Define index taglookup (not unique) to be added to tag_instance.
1251 $table = new xmldb_table('tag_instance');
1252 $index = new xmldb_index('taglookup', XMLDB_INDEX_NOTUNIQUE, array('itemtype', 'component', 'tagid', 'contextid'));
1253
1254 // Conditionally launch add index taglookup.
1255 if (!$dbman->index_exists($table, $index)) {
1256 $dbman->add_index($table, $index);
1257 }
1258
1259 // Main savepoint reached.
67f68027 1260 upgrade_main_savepoint(true, 2016011300.07);
c026a28d
MG
1261 }
1262
67f68027 1263 if ($oldversion < 2016011301.00) {
a73409d3
CB
1264
1265 // Force uninstall of deleted tool.
1266 if (!file_exists("$CFG->dirroot/webservice/amf")) {
1267 // Remove capabilities.
1268 capabilities_cleanup('webservice_amf');
1269 // Remove all other associated config.
1270 unset_all_config_for_plugin('webservice_amf');
1271 }
67f68027 1272 upgrade_main_savepoint(true, 2016011301.00);
a73409d3
CB
1273 }
1274
d3220ca3 1275 if ($oldversion < 2016011901.00) {
a7213391
SB
1276
1277 // Convert calendar_lookahead to nearest new value.
1278 $transaction = $DB->start_delegated_transaction();
1279
1280 // Count all users who curretly have that preference set (for progress bar).
668aca18 1281 $total = $DB->count_records_select('user_preferences', "name = 'calendar_lookahead' AND value != '0'");
a7213391
SB
1282 $pbar = new progress_bar('upgradecalendarlookahead', 500, true);
1283
1284 // Get all these users, one at a time.
668aca18 1285 $rs = $DB->get_recordset_select('user_preferences', "name = 'calendar_lookahead' AND value != '0'");
a7213391
SB
1286 $i = 0;
1287 foreach ($rs as $userpref) {
1288
1289 // Calculate and set new lookahead value.
1290 if ($userpref->value > 90) {
1291 $newvalue = 120;
1292 } else if ($userpref->value > 60 and $userpref->value < 90) {
1293 $newvalue = 90;
1294 } else if ($userpref->value > 30 and $userpref->value < 60) {
1295 $newvalue = 60;
1296 } else if ($userpref->value > 21 and $userpref->value < 30) {
1297 $newvalue = 30;
1298 } else if ($userpref->value > 14 and $userpref->value < 21) {
1299 $newvalue = 21;
1300 } else if ($userpref->value > 7 and $userpref->value < 14) {
1301 $newvalue = 14;
1302 } else {
1303 $newvalue = $userpref->value;
1304 }
1305
1306 $DB->set_field('user_preferences', 'value', $newvalue, array('id' => $userpref->id));
1307
1308 // Update progress.
1309 $i++;
1310 $pbar->update($i, $total, "Upgrading user preference settings - $i/$total.");
1311 }
1312 $rs->close();
1313 $transaction->allow_commit();
1314
d3220ca3 1315 upgrade_main_savepoint(true, 2016011901.00);
a7213391 1316 }
e11d7380
MG
1317
1318 if ($oldversion < 2016020200.00) {
1319
1320 // Define field isstandard to be added to tag.
1321 $table = new xmldb_table('tag');
1322 $field = new xmldb_field('isstandard', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'rawname');
1323
1324 // Conditionally launch add field isstandard.
1325 if (!$dbman->field_exists($table, $field)) {
1326 $dbman->add_field($table, $field);
1327 }
1328
1329 // Define index tagcolltype (not unique) to be dropped form tag.
1330 $index = new xmldb_index('tagcolltype', XMLDB_INDEX_NOTUNIQUE, array('tagcollid', 'tagtype'));
1331
1332 // Conditionally launch drop index tagcolltype.
1333 if ($dbman->index_exists($table, $index)) {
1334 $dbman->drop_index($table, $index);
1335 }
1336
1337 // Define index tagcolltype (not unique) to be added to tag.
1338 $index = new xmldb_index('tagcolltype', XMLDB_INDEX_NOTUNIQUE, array('tagcollid', 'isstandard'));
1339
1340 // Conditionally launch add index tagcolltype.
1341 if (!$dbman->index_exists($table, $index)) {
1342 $dbman->add_index($table, $index);
1343 }
1344
1345 // Define field tagtype to be dropped from tag.
1346 $field = new xmldb_field('tagtype');
1347
1348 // Conditionally launch drop field tagtype and update isstandard.
1349 if ($dbman->field_exists($table, $field)) {
1350 $DB->execute("UPDATE {tag} SET isstandard=(CASE WHEN (tagtype = ?) THEN 1 ELSE 0 END)", array('official'));
1351 $dbman->drop_field($table, $field);
1352 }
1353
1354 // Main savepoint reached.
1355 upgrade_main_savepoint(true, 2016020200.00);
1356 }
1357
4be9c7ad
MG
1358 if ($oldversion < 2016020201.00) {
1359
1360 // Define field showstandard to be added to tag_area.
1361 $table = new xmldb_table('tag_area');
1362 $field = new xmldb_field('showstandard', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'callbackfile');
1363
1364 // Conditionally launch add field showstandard.
1365 if (!$dbman->field_exists($table, $field)) {
1366 $dbman->add_field($table, $field);
1367 }
1368
1369 // By default set user area to hide standard tags. 2 = core_tag_tag::HIDE_STANDARD (can not use constant here).
1370 $DB->execute("UPDATE {tag_area} SET showstandard = ? WHERE itemtype = ? AND component = ?",
1371 array(2, 'user', 'core'));
1372
1373 // Changing precision of field enabled on table tag_area to (1).
1374 $table = new xmldb_table('tag_area');
1375 $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'itemtype');
1376
1377 // Launch change of precision for field enabled.
1378 $dbman->change_field_precision($table, $field);
1379
1380 // Main savepoint reached.
1381 upgrade_main_savepoint(true, 2016020201.00);
1382 }
1383
a9e12347
AN
1384 if ($oldversion < 2016021500.00) {
1385 $root = $CFG->tempdir . '/download';
1386 if (is_dir($root)) {
1387 // Fetch each repository type - include all repos, not just enabled.
1388 $repositories = $DB->get_records('repository', array(), '', 'type');
1389
1390 foreach ($repositories as $id => $repository) {
1391 $directory = $root . '/repository_' . $repository->type;
1392 if (is_dir($directory)) {
1393 fulldelete($directory);
1394 }
1395 }
1396 }
1397
1398 // Main savepoint reached.
1399 upgrade_main_savepoint(true, 2016021500.00);
1400 }
1401
c06749bd 1402 if ($oldversion < 2016021501.00) {
201a9093
EM
1403 // This could take a long time. Unfortunately, no way to know how long, and no way to do progress, so setting for 1 hour.
1404 upgrade_set_timeout(3600);
1405
1406 // Define index userid-itemid (not unique) to be added to grade_grades_history.
1407 $table = new xmldb_table('grade_grades_history');
1408 $index = new xmldb_index('userid-itemid-timemodified', XMLDB_INDEX_NOTUNIQUE, array('userid', 'itemid', 'timemodified'));
1409
1410 // Conditionally launch add index userid-itemid.
1411 if (!$dbman->index_exists($table, $index)) {
1412 $dbman->add_index($table, $index);
1413 }
1414
1415 // Main savepoint reached.
c06749bd 1416 upgrade_main_savepoint(true, 2016021501.00);
201a9093
EM
1417 }
1418
73911de5 1419 if ($oldversion < 2016030103.00) {
7d19e0e3
RK
1420
1421 // MDL-50887. Implement plugins infrastructure for antivirus and create ClamAV plugin.
1422 // This routine moves core ClamAV configuration to plugin level.
1423
1424 // If clamav was configured and enabled, enable the plugin.
1425 if (!empty($CFG->runclamonupload) && !empty($CFG->pathtoclam)) {
1426 set_config('antiviruses', 'clamav');
1427 } else {
1428 set_config('antiviruses', '');
1429 }
1430
1431 if (isset($CFG->runclamonupload)) {
1432 // Just unset global configuration, we have already enabled the plugin
1433 // which implies that ClamAV will be used for scanning uploaded files.
1434 unset_config('runclamonupload');
1435 }
1436 // Move core ClamAV configuration settings to plugin.
1437 if (isset($CFG->pathtoclam)) {
1438 set_config('pathtoclam', $CFG->pathtoclam, 'antivirus_clamav');
1439 unset_config('pathtoclam');
1440 }
1441 if (isset($CFG->quarantinedir)) {
1442 set_config('quarantinedir', $CFG->quarantinedir, 'antivirus_clamav');
1443 unset_config('quarantinedir');
1444 }
1445 if (isset($CFG->clamfailureonupload)) {
1446 set_config('clamfailureonupload', $CFG->clamfailureonupload, 'antivirus_clamav');
1447 unset_config('clamfailureonupload');
1448 }
1449
1450 // Main savepoint reached.
73911de5 1451 upgrade_main_savepoint(true, 2016030103.00);
7d19e0e3
RK
1452 }
1453
186eba1b
JL
1454 if ($oldversion < 2016030400.01) {
1455 // Add the new services field.
1456 $table = new xmldb_table('external_functions');
1457 $field = new xmldb_field('services', XMLDB_TYPE_CHAR, '1333', null, null, null, null, 'capabilities');
1458
1459 // Conditionally launch add field services.
1460 if (!$dbman->field_exists($table, $field)) {
1461 $dbman->add_field($table, $field);
1462 }
1463 // Main savepoint reached.
1464 upgrade_main_savepoint(true, 2016030400.01);
1465 }
1466
a4cdd6d2 1467 return true;
51003653 1468}