MDL-41756 (3) quiz statistics : test variant stats
[moodle.git] / lib / db / upgrade.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * This file keeps track of upgrades to Moodle.
19  *
20  * Sometimes, changes between versions involve
21  * alterations to database structures and other
22  * major things that may break installations.
23  *
24  * The upgrade function in this file will attempt
25  * to perform all the necessary actions to upgrade
26  * your older installation to the current version.
27  *
28  * If there's something it cannot do itself, it
29  * will tell you what you need to do.
30  *
31  * The commands in here will all be database-neutral,
32  * using the methods of database_manager class
33  *
34  * Please do not forget to use upgrade_set_timeout()
35  * before any action that may take longer time to finish.
36  *
37  * @package   core_install
38  * @category  upgrade
39  * @copyright 2006 onwards Martin Dougiamas  http://dougiamas.com
40  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41  */
43 defined('MOODLE_INTERNAL') || die();
45 /**
46  * Main upgrade tasks to be executed on Moodle version bump
47  *
48  * This function is automatically executed after one bump in the Moodle core
49  * version is detected. It's in charge of performing the required tasks
50  * to raise core from the previous version to the next one.
51  *
52  * It's a collection of ordered blocks of code, named "upgrade steps",
53  * each one performing one isolated (from the rest of steps) task. Usually
54  * tasks involve creating new DB objects or performing manipulation of the
55  * information for cleanup/fixup purposes.
56  *
57  * Each upgrade step has a fixed structure, that can be summarised as follows:
58  *
59  * if ($oldversion < XXXXXXXXXX.XX) {
60  *     // Explanation of the update step, linking to issue in the Tracker if necessary
61  *     upgrade_set_timeout(XX); // Optional for big tasks
62  *     // Code to execute goes here, usually the XMLDB Editor will
63  *     // help you here. See {@link http://docs.moodle.org/dev/XMLDB_editor}.
64  *     upgrade_main_savepoint(true, XXXXXXXXXX.XX);
65  * }
66  *
67  * All plugins within Moodle (modules, blocks, reports...) support the existence of
68  * their own upgrade.php file, using the "Frankenstyle" component name as
69  * defined at {@link http://docs.moodle.org/dev/Frankenstyle}, for example:
70  *     - {@link xmldb_page_upgrade($oldversion)}. (modules don't require the plugintype ("mod_") to be used.
71  *     - {@link xmldb_auth_manual_upgrade($oldversion)}.
72  *     - {@link xmldb_workshopform_accumulative_upgrade($oldversion)}.
73  *     - ....
74  *
75  * In order to keep the contents of this file reduced, it's allowed to create some helper
76  * functions to be used here in the {@link upgradelib.php} file at the same directory. Note
77  * that such a file must be manually included from upgrade.php, and there are some restrictions
78  * about what can be used within it.
79  *
80  * For more information, take a look to the documentation available:
81  *     - Data definition API: {@link http://docs.moodle.org/dev/Data_definition_API}
82  *     - Upgrade API: {@link http://docs.moodle.org/dev/Upgrade_API}
83  *
84  * @param int $oldversion
85  * @return bool always true
86  */
87 function xmldb_main_upgrade($oldversion) {
88     global $CFG, $USER, $DB, $OUTPUT, $SITE;
90     require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
92     $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
94     if ($oldversion < 2011120500) {
95         // just in case somebody hacks upgrade scripts or env, we really can not continue
96         echo("You need to upgrade to 2.2.x first!\n");
97         exit(1);
98         // Note this savepoint is 100% unreachable, but needed to pass the upgrade checks
99         upgrade_main_savepoint(true, 2011120500);
100     }
102     // Moodle v2.2.0 release upgrade line
103     // Put any upgrade step following this
105     if ($oldversion < 2011120500.02) {
107         upgrade_set_timeout(60*20); // This may take a while
108         // MDL-28180. Some missing restrictions in certain backup & restore operations
109         // were causing incorrect duplicates in the course_completion_aggr_methd table.
110         // This upgrade step takes rid of them.
111         $sql = 'SELECT course, criteriatype, MIN(id) AS minid
112                   FROM {course_completion_aggr_methd}
113               GROUP BY course, criteriatype
114                 HAVING COUNT(*) > 1';
115         $duprs = $DB->get_recordset_sql($sql);
116         foreach ($duprs as $duprec) {
117             // We need to handle NULLs in criteriatype diferently
118             if (is_null($duprec->criteriatype)) {
119                 $where = 'course = ? AND criteriatype IS NULL AND id > ?';
120                 $params = array($duprec->course, $duprec->minid);
121             } else {
122                 $where = 'course = ? AND criteriatype = ? AND id > ?';
123                 $params = array($duprec->course, $duprec->criteriatype, $duprec->minid);
124             }
125             $DB->delete_records_select('course_completion_aggr_methd', $where, $params);
126         }
127         $duprs->close();
129         // Main savepoint reached
130         upgrade_main_savepoint(true, 2011120500.02);
131     }
133     if ($oldversion < 2011120500.03) {
135         // Changing precision of field value on table user_preferences to (1333)
136         $table = new xmldb_table('user_preferences');
137         $field = new xmldb_field('value', XMLDB_TYPE_CHAR, '1333', null, XMLDB_NOTNULL, null, null, 'name');
139         // Launch change of precision for field value
140         $dbman->change_field_precision($table, $field);
142         // Main savepoint reached
143         upgrade_main_savepoint(true, 2011120500.03);
144     }
146     if ($oldversion < 2012020200.03) {
148         // Define index rolecontext (not unique) to be added to role_assignments
149         $table = new xmldb_table('role_assignments');
150         $index = new xmldb_index('rolecontext', XMLDB_INDEX_NOTUNIQUE, array('roleid', 'contextid'));
152         // Conditionally launch add index rolecontext
153         if (!$dbman->index_exists($table, $index)) {
154             $dbman->add_index($table, $index);
155         }
157         // Define index usercontextrole (not unique) to be added to role_assignments
158         $index = new xmldb_index('usercontextrole', XMLDB_INDEX_NOTUNIQUE, array('userid', 'contextid', 'roleid'));
160         // Conditionally launch add index usercontextrole
161         if (!$dbman->index_exists($table, $index)) {
162             $dbman->add_index($table, $index);
163         }
165         // Main savepoint reached
166         upgrade_main_savepoint(true, 2012020200.03);
167     }
169     if ($oldversion < 2012020200.06) {
170         // Previously we always allowed users to override their email address via the messaging system
171         // We have now added a setting to allow admins to turn this this ability on and off
172         // While this setting defaults to 0 (off) we're setting it to 1 (on) to maintain the behaviour for upgrading sites
173         set_config('messagingallowemailoverride', 1);
175         // Main savepoint reached
176         upgrade_main_savepoint(true, 2012020200.06);
177     }
179     if ($oldversion < 2012021700.01) {
180         // Changing precision of field uniquehash on table post to 255
181         $table = new xmldb_table('post');
182         $field = new xmldb_field('uniquehash', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'content');
184         // Launch change of precision for field uniquehash
185         $dbman->change_field_precision($table, $field);
187         // Main savepoint reached
188         upgrade_main_savepoint(true, 2012021700.01);
189     }
191     if ($oldversion < 2012021700.02) {
192         // Somewhere before 1.9 summary and content column in post table were not null. In 1.9+
193         // not null became false.
194         $columns = $DB->get_columns('post');
196         // Fix discrepancies in summary field after upgrade from 1.9
197         if (array_key_exists('summary', $columns) && $columns['summary']->not_null != false) {
198             $table = new xmldb_table('post');
199             $summaryfield = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'subject');
201             if ($dbman->field_exists($table, $summaryfield)) {
202                 $dbman->change_field_notnull($table, $summaryfield);
203             }
205         }
207         // Fix discrepancies in content field after upgrade from 1.9
208         if (array_key_exists('content', $columns) && $columns['content']->not_null != false) {
209             $table = new xmldb_table('post');
210             $contentfield = new xmldb_field('content', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'summary');
212             if ($dbman->field_exists($table, $contentfield)) {
213                 $dbman->change_field_notnull($table, $contentfield);
214             }
216         }
218         upgrade_main_savepoint(true, 2012021700.02);
219     }
221     // The ability to backup user (private) files is out completely - MDL-29248
222     if ($oldversion < 2012030100.01) {
223         unset_config('backup_general_user_files', 'backup');
224         unset_config('backup_general_user_files_locked', 'backup');
225         unset_config('backup_auto_user_files', 'backup');
227         upgrade_main_savepoint(true, 2012030100.01);
228     }
230     if ($oldversion < 2012030900.01) {
231         // Migrate all numbers to signed & all texts and binaries to big size.
232         // It should be safe to interrupt this and continue later.
233         upgrade_mysql_fix_unsigned_and_lob_columns();
235         // Main savepoint reached
236         upgrade_main_savepoint(true, 2012030900.01);
237     }
239     if ($oldversion < 2012031500.01) {
240         // Upgrade old course_allowed_modules data to be permission overrides.
241         if ($CFG->restrictmodulesfor === 'all') {
242             $courses = $DB->get_records_menu('course', array(), 'id', 'id, 1');
243         } else if ($CFG->restrictmodulesfor === 'requested') {
244             $courses = $DB->get_records_menu('course', array('restrictmodules' => 1), 'id', 'id, 1');
245         } else {
246             $courses = array();
247         }
249         if (!$dbman->table_exists('course_allowed_modules')) {
250             // Upgrade must already have been run on this server. This might happen,
251             // for example, during development of these changes.
252             $courses = array();
253         }
255         $modidtoname = $DB->get_records_menu('modules', array(), 'id', 'id, name');
257         $coursecount = count($courses);
258         if ($coursecount) {
259             $pbar = new progress_bar('allowedmods', 500, true);
260             $transaction = $DB->start_delegated_transaction();
261         }
263         $i = 0;
264         foreach ($courses as $courseid => $notused) {
265             $i += 1;
266             upgrade_set_timeout(60); // 1 minute per course should be fine.
268             $allowedmoduleids = $DB->get_records_menu('course_allowed_modules',
269             array('course' => $courseid), 'module', 'module, 1');
270             if (empty($allowedmoduleids)) {
271                 // This seems to be the best match for backwards compatibility,
272                 // not necessarily with the old code in course_allowed_module function,
273                 // but with the code that used to be in the coures settings form.
274                 $allowedmoduleids = explode(',', $CFG->defaultallowedmodules);
275                 $allowedmoduleids = array_combine($allowedmoduleids, $allowedmoduleids);
276             }
278             $context = context_course::instance($courseid);
280             list($roleids) = get_roles_with_cap_in_context($context, 'moodle/course:manageactivities');
281             list($managerroleids) = get_roles_with_cap_in_context($context, 'moodle/site:config');
282             foreach ($managerroleids as $roleid) {
283                 unset($roleids[$roleid]);
284             }
286             foreach ($modidtoname as $modid => $modname) {
287                 if (isset($allowedmoduleids[$modid])) {
288                     // Module is allowed, no worries.
289                     continue;
290                 }
292                 $capability = 'mod/' . $modname . ':addinstance';
293                 foreach ($roleids as $roleid) {
294                     assign_capability($capability, CAP_PREVENT, $roleid, $context);
295                 }
296             }
298             $pbar->update($i, $coursecount, "Upgrading legacy course_allowed_modules data - $i/$coursecount.");
299         }
301         if ($coursecount) {
302             $transaction->allow_commit();
303         }
305         upgrade_main_savepoint(true, 2012031500.01);
306     }
308     if ($oldversion < 2012031500.02) {
310         // Define field restrictmodules to be dropped from course
311         $table = new xmldb_table('course');
312         $field = new xmldb_field('restrictmodules');
314         // Conditionally launch drop field requested
315         if ($dbman->field_exists($table, $field)) {
316             $dbman->drop_field($table, $field);
317         }
319         upgrade_main_savepoint(true, 2012031500.02);
320     }
322     if ($oldversion < 2012031500.03) {
324         // Define table course_allowed_modules to be dropped
325         $table = new xmldb_table('course_allowed_modules');
327         // Conditionally launch drop table for course_allowed_modules
328         if ($dbman->table_exists($table)) {
329             $dbman->drop_table($table);
330         }
332         upgrade_main_savepoint(true, 2012031500.03);
333     }
335     if ($oldversion < 2012031500.04) {
336         // Clean up the old admin settings.
337         unset_config('restrictmodulesfor');
338         unset_config('restrictbydefault');
339         unset_config('defaultallowedmodules');
341         upgrade_main_savepoint(true, 2012031500.04);
342     }
344     if ($oldversion < 2012032300.02) {
345         // Migrate the old admin debug setting.
346         if ($CFG->debug == 38911) {
347             set_config('debug', DEBUG_DEVELOPER);
348         } else if ($CFG->debug == 6143) {
349             set_config('debug', DEBUG_ALL);
350         }
351         upgrade_main_savepoint(true, 2012032300.02);
352     }
354     if ($oldversion < 2012042300.00) {
355         // This change makes the course_section index unique.
357         // xmldb does not allow changing index uniqueness - instead we must drop
358         // index then add it again
359         $table = new xmldb_table('course_sections');
360         $index = new xmldb_index('course_section', XMLDB_INDEX_NOTUNIQUE, array('course', 'section'));
362         // Conditionally launch drop index course_section
363         if ($dbman->index_exists($table, $index)) {
364             $dbman->drop_index($table, $index);
365         }
367         // Look for any duplicate course_sections entries. There should not be
368         // any but on some busy systems we found a few, maybe due to previous
369         // bugs.
370         $transaction = $DB->start_delegated_transaction();
371         $rs = $DB->get_recordset_sql('
372                 SELECT DISTINCT
373                     cs.id, cs.course
374                 FROM
375                     {course_sections} cs
376                     INNER JOIN {course_sections} older
377                         ON cs.course = older.course AND cs.section = older.section
378                         AND older.id < cs.id');
379         foreach ($rs as $rec) {
380             $DB->delete_records('course_sections', array('id' => $rec->id));
381             // We can't use rebuild_course_cache() here because introducing sectioncache later
382             // so reset modinfo manually.
383             $DB->set_field('course', 'modinfo', null, array('id' => $rec->course));
384         }
385         $rs->close();
386         $transaction->allow_commit();
388         // Define index course_section (unique) to be added to course_sections
389         $index = new xmldb_index('course_section', XMLDB_INDEX_UNIQUE, array('course', 'section'));
391         // Conditionally launch add index course_section
392         if (!$dbman->index_exists($table, $index)) {
393             $dbman->add_index($table, $index);
394         }
396         // Main savepoint reached
397         upgrade_main_savepoint(true, 2012042300.00);
398     }
400     if ($oldversion < 2012042300.02) {
401         require_once($CFG->dirroot.'/completion/criteria/completion_criteria.php');
402         // Delete orphaned criteria which were left when modules were removed
403         if ($DB->get_dbfamily() === 'mysql') {
404             $sql = "DELETE cc FROM {course_completion_criteria} cc
405                     LEFT JOIN {course_modules} cm ON cm.id = cc.moduleinstance
406                     WHERE cm.id IS NULL AND cc.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
407         } else {
408             $sql = "DELETE FROM {course_completion_criteria}
409                     WHERE NOT EXISTS (
410                         SELECT 'x' FROM {course_modules}
411                         WHERE {course_modules}.id = {course_completion_criteria}.moduleinstance)
412                     AND {course_completion_criteria}.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
413         }
414         $DB->execute($sql);
416         // Main savepoint reached
417         upgrade_main_savepoint(true, 2012042300.02);
418     }
420     if ($oldversion < 2012050300.01) {
421         // Make sure deleted users do not have picture flag.
422         $DB->set_field('user', 'picture', 0, array('deleted'=>1, 'picture'=>1));
423         upgrade_main_savepoint(true, 2012050300.01);
424     }
426     if ($oldversion < 2012050300.02) {
428         // Changing precision of field picture on table user to (10)
429         $table = new xmldb_table('user');
430         $field = new xmldb_field('picture', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'secret');
432         // Launch change of precision for field picture
433         $dbman->change_field_precision($table, $field);
435         // Main savepoint reached
436         upgrade_main_savepoint(true, 2012050300.02);
437     }
439     if ($oldversion < 2012050300.03) {
441         // Define field coursedisplay to be added to course
442         $table = new xmldb_table('course');
443         $field = new xmldb_field('coursedisplay', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'completionnotify');
445         // Conditionally launch add field coursedisplay
446         if (!$dbman->field_exists($table, $field)) {
447             $dbman->add_field($table, $field);
448         }
450         // Main savepoint reached
451         upgrade_main_savepoint(true, 2012050300.03);
452     }
454     if ($oldversion < 2012050300.04) {
456         // Define table course_display to be dropped
457         $table = new xmldb_table('course_display');
459         // Conditionally launch drop table for course_display
460         if ($dbman->table_exists($table)) {
461             $dbman->drop_table($table);
462         }
464         // Main savepoint reached
465         upgrade_main_savepoint(true, 2012050300.04);
466     }
468     if ($oldversion < 2012050300.05) {
470         // Clean up removed admin setting.
471         unset_config('navlinkcoursesections');
473         upgrade_main_savepoint(true, 2012050300.05);
474     }
476     if ($oldversion < 2012050400.01) {
478         // Define index sortorder (not unique) to be added to course
479         $table = new xmldb_table('course');
480         $index = new xmldb_index('sortorder', XMLDB_INDEX_NOTUNIQUE, array('sortorder'));
482         // Conditionally launch add index sortorder
483         if (!$dbman->index_exists($table, $index)) {
484             $dbman->add_index($table, $index);
485         }
487         // Main savepoint reached
488         upgrade_main_savepoint(true, 2012050400.01);
489     }
491     if ($oldversion < 2012050400.02) {
493         // Clean up removed admin setting.
494         unset_config('enablecourseajax');
496         upgrade_main_savepoint(true, 2012050400.02);
497     }
499     if ($oldversion < 2012051100.01) {
501         // Define field idnumber to be added to groups
502         $table = new xmldb_table('groups');
503         $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'courseid');
504         $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
506         // Conditionally launch add field idnumber
507         if (!$dbman->field_exists($table, $field)) {
508             $dbman->add_field($table, $field);
509         }
511         // Conditionally launch add index idnumber
512         if (!$dbman->index_exists($table, $index)) {
513             $dbman->add_index($table, $index);
514         }
516         // Define field idnumber to be added to groupings
517         $table = new xmldb_table('groupings');
518         $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'name');
519         $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
521         // Conditionally launch add field idnumber
522         if (!$dbman->field_exists($table, $field)) {
523             $dbman->add_field($table, $field);
524         }
526         // Conditionally launch add index idnumber
527         if (!$dbman->index_exists($table, $index)) {
528             $dbman->add_index($table, $index);
529         }
531         // Main savepoint reached
532         upgrade_main_savepoint(true, 2012051100.01);
533     }
535     if ($oldversion < 2012051100.03) {
537         // Amend course table to add sectioncache cache
538         $table = new xmldb_table('course');
539         $field = new xmldb_field('sectioncache', XMLDB_TYPE_TEXT, null, null, null, null, null, 'showgrades');
540         if (!$dbman->field_exists($table, $field)) {
541             $dbman->add_field($table, $field);
542         }
544         // Amend course_sections to add date, time and groupingid availability
545         // conditions and a setting about whether to show them
546         $table = new xmldb_table('course_sections');
547         $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'visible');
548         if (!$dbman->field_exists($table, $field)) {
549             $dbman->add_field($table, $field);
550         }
551         $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'availablefrom');
552         if (!$dbman->field_exists($table, $field)) {
553             $dbman->add_field($table, $field);
554         }
555         $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'availableuntil');
556         // Conditionally launch add field showavailability
557         if (!$dbman->field_exists($table, $field)) {
558             $dbman->add_field($table, $field);
559         }
560         $field = new xmldb_field('groupingid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'showavailability');
561         // Conditionally launch add field groupingid
562         if (!$dbman->field_exists($table, $field)) {
563             $dbman->add_field($table, $field);
564         }
566         // Add course_sections_availability to add completion & grade availability conditions
567         $table = new xmldb_table('course_sections_availability');
569         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
570         $table->add_field('coursesectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
571         $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
572         $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', null, null, null, null);
573         $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
574         $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
575         $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
577         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
578         $table->add_key('coursesectionid', XMLDB_KEY_FOREIGN, array('coursesectionid'), 'course_sections', array('id'));
579         $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
580         $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
582         if (!$dbman->table_exists($table)) {
583             $dbman->create_table($table);
584         }
586         // Main savepoint reached
587         upgrade_main_savepoint(true, 2012051100.03);
588     }
590     if ($oldversion < 2012052100.00) {
592         // Define field referencefileid to be added to files.
593         $table = new xmldb_table('files');
595         // Define field referencefileid to be added to files.
596         $field = new xmldb_field('referencefileid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'sortorder');
598         // Conditionally launch add field referencefileid.
599         if (!$dbman->field_exists($table, $field)) {
600             $dbman->add_field($table, $field);
601         }
603         // Define field referencelastsync to be added to files.
604         $field = new xmldb_field('referencelastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencefileid');
606         // Conditionally launch add field referencelastsync.
607         if (!$dbman->field_exists($table, $field)) {
608             $dbman->add_field($table, $field);
609         }
611         // Define field referencelifetime to be added to files.
612         $field = new xmldb_field('referencelifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencelastsync');
614         // Conditionally launch add field referencelifetime.
615         if (!$dbman->field_exists($table, $field)) {
616             $dbman->add_field($table, $field);
617         }
619         $key = new xmldb_key('referencefileid', XMLDB_KEY_FOREIGN, array('referencefileid'), 'files_reference', array('id'));
620         // Launch add key referencefileid
621         $dbman->add_key($table, $key);
623         // Define table files_reference to be created.
624         $table = new xmldb_table('files_reference');
626         // Adding fields to table files_reference.
627         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
628         $table->add_field('repositoryid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
629         $table->add_field('lastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
630         $table->add_field('lifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
631         $table->add_field('reference', XMLDB_TYPE_TEXT, null, null, null, null, null);
633         // Adding keys to table files_reference.
634         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
635         $table->add_key('repositoryid', XMLDB_KEY_FOREIGN, array('repositoryid'), 'repository_instances', array('id'));
637         // Conditionally launch create table for files_reference
638         if (!$dbman->table_exists($table)) {
639             $dbman->create_table($table);
640         }
642         // Main savepoint reached
643         upgrade_main_savepoint(true, 2012052100.00);
644     }
646     if ($oldversion < 2012052500.03) { // fix invalid course_completion_records MDL-27368
647         //first get all instances of duplicate records
648         $sql = 'SELECT userid, course FROM {course_completions} WHERE (deleted IS NULL OR deleted <> 1) GROUP BY userid, course HAVING (count(id) > 1)';
649         $duplicates = $DB->get_recordset_sql($sql, array());
651         foreach ($duplicates as $duplicate) {
652             $pointer = 0;
653             //now get all the records for this user/course
654             $sql = 'userid = ? AND course = ? AND (deleted IS NULL OR deleted <> 1)';
655             $completions = $DB->get_records_select('course_completions', $sql,
656                 array($duplicate->userid, $duplicate->course), 'timecompleted DESC, timestarted DESC');
657             $needsupdate = false;
658             $origcompletion = null;
659             foreach ($completions as $completion) {
660                 $pointer++;
661                 if ($pointer === 1) { //keep 1st record but delete all others.
662                     $origcompletion = $completion;
663                 } else {
664                     //we need to keep the "oldest" of all these fields as the valid completion record.
665                     $fieldstocheck = array('timecompleted', 'timestarted', 'timeenrolled');
666                     foreach ($fieldstocheck as $f) {
667                         if ($origcompletion->$f > $completion->$f) {
668                             $origcompletion->$f = $completion->$f;
669                             $needsupdate = true;
670                         }
671                     }
672                     $DB->delete_records('course_completions', array('id'=>$completion->id));
673                 }
674             }
675             if ($needsupdate) {
676                 $DB->update_record('course_completions', $origcompletion);
677             }
678         }
680         // Main savepoint reached
681         upgrade_main_savepoint(true, 2012052500.03);
682     }
684     if ($oldversion < 2012052900.00) {
685         // Clean up all duplicate records in the course_completions table in preparation
686         // for adding a new index there.
687         upgrade_course_completion_remove_duplicates(
688             'course_completions',
689             array('userid', 'course'),
690             array('timecompleted', 'timestarted', 'timeenrolled')
691         );
693         // Main savepoint reached
694         upgrade_main_savepoint(true, 2012052900.00);
695     }
697     if ($oldversion < 2012052900.01) {
698         // Add indexes to prevent new duplicates in the course_completions table.
699         // Define index useridcourse (unique) to be added to course_completions
700         $table = new xmldb_table('course_completions');
701         $index = new xmldb_index('useridcourse', XMLDB_INDEX_UNIQUE, array('userid', 'course'));
703         // Conditionally launch add index useridcourse
704         if (!$dbman->index_exists($table, $index)) {
705             $dbman->add_index($table, $index);
706         }
708         // Main savepoint reached
709         upgrade_main_savepoint(true, 2012052900.01);
710     }
712     if ($oldversion < 2012052900.02) {
713         // Clean up all duplicate records in the course_completion_crit_compl table in preparation
714         // for adding a new index there.
715         upgrade_course_completion_remove_duplicates(
716             'course_completion_crit_compl',
717             array('userid', 'course', 'criteriaid'),
718             array('timecompleted')
719         );
721         // Main savepoint reached
722         upgrade_main_savepoint(true, 2012052900.02);
723     }
725     if ($oldversion < 2012052900.03) {
726         // Add indexes to prevent new duplicates in the course_completion_crit_compl table.
727         // Define index useridcoursecriteraid (unique) to be added to course_completion_crit_compl
728         $table = new xmldb_table('course_completion_crit_compl');
729         $index = new xmldb_index('useridcoursecriteraid', XMLDB_INDEX_UNIQUE, array('userid', 'course', 'criteriaid'));
731         // Conditionally launch add index useridcoursecriteraid
732         if (!$dbman->index_exists($table, $index)) {
733             $dbman->add_index($table, $index);
734         }
736         // Main savepoint reached
737         upgrade_main_savepoint(true, 2012052900.03);
738     }
740     if ($oldversion < 2012052900.04) {
741         // Clean up all duplicate records in the course_completion_aggr_methd table in preparation
742         // for adding a new index there.
743         upgrade_course_completion_remove_duplicates(
744             'course_completion_aggr_methd',
745             array('course', 'criteriatype')
746         );
748         // Main savepoint reached
749         upgrade_main_savepoint(true, 2012052900.04);
750     }
752     if ($oldversion < 2012052900.05) {
753         // Add indexes to prevent new duplicates in the course_completion_aggr_methd table.
754         // Define index coursecriteratype (unique) to be added to course_completion_aggr_methd
755         $table = new xmldb_table('course_completion_aggr_methd');
756         $index = new xmldb_index('coursecriteriatype', XMLDB_INDEX_UNIQUE, array('course', 'criteriatype'));
758         // Conditionally launch add index coursecriteratype
759         if (!$dbman->index_exists($table, $index)) {
760             $dbman->add_index($table, $index);
761         }
763         // Main savepoint reached
764         upgrade_main_savepoint(true, 2012052900.05);
765     }
767     if ($oldversion < 2012060600.01) {
768         // Add field referencehash to files_reference
769         $table = new xmldb_table('files_reference');
770         $field = new xmldb_field('referencehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'reference');
771         if (!$dbman->field_exists($table, $field)) {
772             $dbman->add_field($table, $field);
773         }
774         upgrade_main_savepoint(true, 2012060600.01);
775     }
777     if ($oldversion < 2012060600.02) {
778         // Populate referencehash field with SHA1 hash of the reference - this shoudl affect only 2.3dev sites
779         // that were using the feature for testing. Production sites have the table empty.
780         $rs = $DB->get_recordset('files_reference', null, '', 'id, reference');
781         foreach ($rs as $record) {
782             $hash = sha1($record->reference);
783             $DB->set_field('files_reference', 'referencehash', $hash, array('id' => $record->id));
784         }
785         $rs->close();
787         upgrade_main_savepoint(true, 2012060600.02);
788     }
790     if ($oldversion < 2012060600.03) {
791         // Merge duplicate records in files_reference that were created during the development
792         // phase at 2.3dev sites. This is needed so we can create the unique index over
793         // (repositoryid, referencehash) fields.
794         $sql = "SELECT repositoryid, referencehash, MIN(id) AS minid
795                   FROM {files_reference}
796               GROUP BY repositoryid, referencehash
797                 HAVING COUNT(*) > 1";
798         $duprs = $DB->get_recordset_sql($sql);
799         foreach ($duprs as $duprec) {
800             // get the list of all ids in {files_reference} that need to be remapped
801             $dupids = $DB->get_records_select('files_reference', "repositoryid = ? AND referencehash = ? AND id > ?",
802                 array($duprec->repositoryid, $duprec->referencehash, $duprec->minid), '', 'id');
803             $dupids = array_keys($dupids);
804             // relink records in {files} that are now referring to a duplicate record
805             // in {files_reference} to refer to the first one
806             list($subsql, $subparams) = $DB->get_in_or_equal($dupids);
807             $DB->set_field_select('files', 'referencefileid', $duprec->minid, "referencefileid $subsql", $subparams);
808             // and finally remove all orphaned records from {files_reference}
809             $DB->delete_records_list('files_reference', 'id', $dupids);
810         }
811         $duprs->close();
813         upgrade_main_savepoint(true, 2012060600.03);
814     }
816     if ($oldversion < 2012060600.04) {
817         // Add a unique index over repositoryid and referencehash fields in files_reference table
818         $table = new xmldb_table('files_reference');
819         $index = new xmldb_index('uq_external_file', XMLDB_INDEX_UNIQUE, array('repositoryid', 'referencehash'));
821         if (!$dbman->index_exists($table, $index)) {
822             $dbman->add_index($table, $index);
823         }
825         upgrade_main_savepoint(true, 2012060600.04);
826     }
828     if ($oldversion < 2012061800.01) {
830         // Define field screenreader to be dropped from user
831         $table = new xmldb_table('user');
832         $field = new xmldb_field('ajax');
834         // Conditionally launch drop field screenreader
835         if ($dbman->field_exists($table, $field)) {
836             $dbman->drop_field($table, $field);
837         }
839         // Main savepoint reached
840         upgrade_main_savepoint(true, 2012061800.01);
841     }
843     if ($oldversion < 2012062000.00) {
844         // Add field newcontextid to backup_files_template
845         $table = new xmldb_table('backup_files_template');
846         $field = new xmldb_field('newcontextid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'info');
848         if (!$dbman->field_exists($table, $field)) {
849             $dbman->add_field($table, $field);
850         }
852         upgrade_main_savepoint(true, 2012062000.00);
853     }
855     if ($oldversion < 2012062000.01) {
856         // Add field newitemid to backup_files_template
857         $table = new xmldb_table('backup_files_template');
858         $field = new xmldb_field('newitemid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'newcontextid');
860         if (!$dbman->field_exists($table, $field)) {
861             $dbman->add_field($table, $field);
862         }
864         upgrade_main_savepoint(true, 2012062000.01);
865     }
867     // Moodle v2.3.0 release upgrade line
868     // Put any upgrade step following this
870     if ($oldversion < 2012062500.02) {
871         // Drop some old backup tables, not used anymore
873         // Define table backup_files to be dropped
874         $table = new xmldb_table('backup_files');
876         // Conditionally launch drop table for backup_files
877         if ($dbman->table_exists($table)) {
878             $dbman->drop_table($table);
879         }
881         // Define table backup_ids to be dropped
882         $table = new xmldb_table('backup_ids');
884         // Conditionally launch drop table for backup_ids
885         if ($dbman->table_exists($table)) {
886             $dbman->drop_table($table);
887         }
889         // Main savepoint reached
890         upgrade_main_savepoint(true, 2012062500.02);
891     }
893     if ($oldversion < 2012070600.04) {
894         // Define table course_modules_avail_fields to be created
895         $table = new xmldb_table('course_modules_avail_fields');
897         // Adding fields to table course_modules_avail_fields
898         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
899         $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
900         $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
901         $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
902         $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
903         $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
905         // Adding keys to table course_modules_avail_fields
906         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
907         $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
909         // Conditionally launch create table for course_modules_avail_fields
910         if (!$dbman->table_exists($table)) {
911             $dbman->create_table($table);
912         }
914         // Main savepoint reached
915         upgrade_main_savepoint(true, 2012070600.04);
916     }
918     if ($oldversion < 2012070600.05) {
919         // Define table course_sections_avail_fields to be created
920         $table = new xmldb_table('course_sections_avail_fields');
922         // Adding fields to table course_sections_avail_fields
923         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
924         $table->add_field('coursesectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
925         $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
926         $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
927         $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
928         $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
930         // Adding keys to table course_sections_avail_fields
931         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
932         $table->add_key('coursesectionid', XMLDB_KEY_FOREIGN, array('coursesectionid'), 'course_sections', array('id'));
934         // Conditionally launch create table for course_sections_avail_fields
935         if (!$dbman->table_exists($table)) {
936             $dbman->create_table($table);
937         }
939         // Main savepoint reached
940         upgrade_main_savepoint(true, 2012070600.05);
941     }
943     if ($oldversion < 2012070600.06) {
945         // Drop "deleted" fields
946         $table = new xmldb_table('course_completions');
947         $field = new xmldb_field('timenotified');
948         $field = new xmldb_field('deleted');
950         // Conditionally launch drop field deleted from course_completions
951         if ($dbman->field_exists($table, $field)) {
952             $dbman->drop_field($table, $field);
953         }
955         $field = new xmldb_field('timenotified');
956         // Conditionally launch drop field timenotified from course_completions
957         if ($dbman->field_exists($table, $field)) {
958             $dbman->drop_field($table, $field);
959         }
961         // Main savepoint reached
962         upgrade_main_savepoint(true, 2012070600.06);
963     }
965     if ($oldversion < 2012070600.07) {
966         $table = new xmldb_table('course_completion_crit_compl');
967         $field = new xmldb_field('deleted');
969         // Conditionally launch drop field deleted from course_completion_crit_compl
970         if ($dbman->field_exists($table, $field)) {
971             $dbman->drop_field($table, $field);
972         }
973         // Main savepoint reached
974         upgrade_main_savepoint(true, 2012070600.07);
975     }
977     if ($oldversion < 2012070600.08) {
979         // Drop unused table "course_completion_notify"
980         $table = new xmldb_table('course_completion_notify');
982         // Conditionally launch drop table course_completion_notify
983         if ($dbman->table_exists($table)) {
984             $dbman->drop_table($table);
985         }
987         // Main savepoint reached
988         upgrade_main_savepoint(true, 2012070600.08);
989      }
991     if ($oldversion < 2012070600.09) {
993         // Define index path (not unique) to be added to context
994         $table = new xmldb_table('context');
995         $index = new xmldb_index('path', XMLDB_INDEX_NOTUNIQUE, array('path'), array('varchar_pattern_ops'));
997         // Recreate index with new pattern hint
998         if ($DB->get_dbfamily() === 'postgres') {
999             if ($dbman->index_exists($table, $index)) {
1000                 $dbman->drop_index($table, $index);
1001             }
1002             $dbman->add_index($table, $index);
1003         }
1005         // Main savepoint reached
1006         upgrade_main_savepoint(true, 2012070600.09);
1007     }
1009     if ($oldversion < 2012070600.10) {
1011         // Define index name (unique) to be dropped form role
1012         $table = new xmldb_table('role');
1013         $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
1015         // Conditionally launch drop index name
1016         if ($dbman->index_exists($table, $index)) {
1017             $dbman->drop_index($table, $index);
1018         }
1020         // Main savepoint reached
1021         upgrade_main_savepoint(true, 2012070600.10);
1022     }
1024     if ($oldversion < 2012070600.11) {
1026         // Define index component-itemid-userid (not unique) to be added to role_assignments
1027         $table = new xmldb_table('role_assignments');
1028         $index = new xmldb_index('component-itemid-userid', XMLDB_INDEX_NOTUNIQUE, array('component', 'itemid', 'userid'));
1030         // Conditionally launch add index component-itemid-userid
1031         if (!$dbman->index_exists($table, $index)) {
1032             $dbman->add_index($table, $index);
1033         }
1035         // Main savepoint reached
1036         upgrade_main_savepoint(true, 2012070600.11);
1037     }
1039     if ($oldversion < 2012071900.01) {
1040         // Cleanup after simpeltests tool
1041         capabilities_cleanup('tool_unittest');
1042         unset_all_config_for_plugin('tool_unittest');
1044         upgrade_main_savepoint(true, 2012071900.01);
1045     }
1047     if ($oldversion < 2012072400.00) {
1048         // Remove obsolete xhtml strict setting - use THEME->doctype in theme config if necessary,
1049         // see theme_config->doctype in lib/outputlib.php for more details.
1050         unset_config('xmlstrictheaders');
1051         upgrade_main_savepoint(true, 2012072400.00);
1052     }
1054     if ($oldversion < 2012072401.00) {
1056         // Saves orphaned questions from the Dark Side
1057         upgrade_save_orphaned_questions();
1059         // Main savepoint reached
1060         upgrade_main_savepoint(true, 2012072401.00);
1061     }
1063     if ($oldversion < 2012072600.01) {
1064         // Handle events with empty eventtype //MDL-32827
1066         $DB->set_field('event', 'eventtype', 'site', array('eventtype' => '', 'courseid' => $SITE->id));
1067         $DB->set_field_select('event', 'eventtype', 'due', "eventtype = '' AND courseid != 0 AND groupid = 0 AND (modulename = 'assignment' OR modulename = 'assign')");
1068         $DB->set_field_select('event', 'eventtype', 'course', "eventtype = '' AND courseid != 0 AND groupid = 0");
1069         $DB->set_field_select('event', 'eventtype', 'group', "eventtype = '' AND groupid != 0");
1070         $DB->set_field_select('event', 'eventtype', 'user', "eventtype = '' AND userid != 0");
1072         // Main savepoint reached
1073         upgrade_main_savepoint(true, 2012072600.01);
1074     }
1076     if ($oldversion < 2012080200.02) {
1077         // Drop obsolete question upgrade field that should have been added to the install.xml.
1078         $table = new xmldb_table('question');
1079         $field = new xmldb_field('oldquestiontextformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
1081         if ($dbman->field_exists($table, $field)) {
1082             $dbman->drop_field($table, $field);
1083         }
1085         upgrade_main_savepoint(true, 2012080200.02);
1086     }
1088     if ($oldversion < 2012081400.01) {
1089         // Move the ability to disable blogs to its own setting MDL-25012.
1091         if (isset($CFG->bloglevel)) {
1092             // Only change settings if existing setting was set.
1093             if (empty($CFG->bloglevel)) {
1094                 set_config('enableblogs', 0);
1095                 // Now set the bloglevel to a valid setting as the disabled setting has been removed.
1096                 // This prevents confusing results when users enable the blog system in future.
1097                 set_config('bloglevel', BLOG_USER_LEVEL);
1098             } else {
1099                 set_config('enableblogs', 1);
1100             }
1101         }
1103         // Main savepoint reached
1104         upgrade_main_savepoint(true, 2012081400.01);
1105     }
1107     if ($oldversion < 2012081600.01) {
1108         // Delete removed setting - Google Maps API V2 will not work in 2013.
1109         unset_config('googlemapkey');
1110         upgrade_main_savepoint(true, 2012081600.01);
1111     }
1113     if ($oldversion < 2012082300.01) {
1114         // Add more custom enrol fields.
1115         $table = new xmldb_table('enrol');
1116         $field = new xmldb_field('customint5', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint4');
1118         if (!$dbman->field_exists($table, $field)) {
1119             $dbman->add_field($table, $field);
1120         }
1122         $field = new xmldb_field('customint6', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint5');
1123         if (!$dbman->field_exists($table, $field)) {
1124             $dbman->add_field($table, $field);
1125         }
1127         $field = new xmldb_field('customint7', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint6');
1128         if (!$dbman->field_exists($table, $field)) {
1129             $dbman->add_field($table, $field);
1130         }
1132         $field = new xmldb_field('customint8', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint7');
1133         if (!$dbman->field_exists($table, $field)) {
1134             $dbman->add_field($table, $field);
1135         }
1137         $field = new xmldb_field('customchar3', XMLDB_TYPE_CHAR, '1333', null, null, null, null, 'customchar2');
1138         if (!$dbman->field_exists($table, $field)) {
1139             $dbman->add_field($table, $field);
1140         }
1142         $field = new xmldb_field('customtext3', XMLDB_TYPE_TEXT, null, null, null, null, null, 'customtext2');
1143         if (!$dbman->field_exists($table, $field)) {
1144             $dbman->add_field($table, $field);
1145         }
1147         $field = new xmldb_field('customtext4', XMLDB_TYPE_TEXT, null, null, null, null, null, 'customtext3');
1148         if (!$dbman->field_exists($table, $field)) {
1149             $dbman->add_field($table, $field);
1150         }
1152         // Main savepoint reached.
1153         upgrade_main_savepoint(true, 2012082300.01);
1154     }
1156     if ($oldversion < 2012082300.02) {
1157         // Define field component to be added to groups_members
1158         $table = new xmldb_table('groups_members');
1159         $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'timeadded');
1161         // Conditionally launch add field component
1162         if (!$dbman->field_exists($table, $field)) {
1163             $dbman->add_field($table, $field);
1164         }
1166         // Define field itemid to be added to groups_members
1167         $field = new xmldb_field('itemid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'component');
1169         // Conditionally launch add field itemid
1170         if (!$dbman->field_exists($table, $field)) {
1171             $dbman->add_field($table, $field);
1172         }
1174         // Main savepoint reached
1175         upgrade_main_savepoint(true, 2012082300.02);
1176     }
1178     if ($oldversion < 2012090500.00) {
1179         $subquery = 'SELECT b.id FROM {blog_external} b where b.id = ' . $DB->sql_cast_char2int('{post}.content', true);
1180         $sql = 'DELETE FROM {post}
1181                       WHERE {post}.module = \'blog_external\'
1182                             AND NOT EXISTS (' . $subquery . ')
1183                             AND ' . $DB->sql_isnotempty('post', 'uniquehash', false, false);
1184         $DB->execute($sql);
1185         upgrade_main_savepoint(true, 2012090500.00);
1186     }
1188     if ($oldversion < 2012090700.01) {
1189         // Add a category field in the course_request table
1190         $table = new xmldb_table('course_request');
1191         $field = new xmldb_field('category', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, 'summaryformat');
1192         if (!$dbman->field_exists($table, $field)) {
1193             $dbman->add_field($table, $field);
1194         }
1196         // Main savepoint reached.
1197         upgrade_main_savepoint(true, 2012090700.01);
1198     }
1200     if ($oldversion < 2012091700.00) {
1202         // Dropping screenreader field from user.
1203         $table = new xmldb_table('user');
1204         $field = new xmldb_field('screenreader');
1206         if ($dbman->field_exists($table, $field)) {
1207             $dbman->drop_field($table, $field);
1208         }
1210         // Main savepoint reached.
1211         upgrade_main_savepoint(true, 2012091700.00);
1212     }
1214     if ($oldversion < 2012092100.01) {
1215         // Some folders still have a sortorder set, which is used for main files but is not
1216         // supported by the folder resource. We reset the value here.
1217         $sql = 'UPDATE {files} SET sortorder = ? WHERE component = ? AND filearea = ? AND sortorder <> ?';
1218         $DB->execute($sql, array(0, 'mod_folder', 'content', 0));
1220         // Main savepoint reached.
1221         upgrade_main_savepoint(true, 2012092100.01);
1222     }
1224     if ($oldversion < 2012092600.00) {
1225         // Define index idname (unique) to be added to tag
1226         $table = new xmldb_table('tag');
1227         $index = new xmldb_index('idname', XMLDB_INDEX_UNIQUE, array('id', 'name'));
1229         // Conditionally launch add index idname
1230         if (!$dbman->index_exists($table, $index)) {
1231             $dbman->add_index($table, $index);
1232         }
1234         // Main savepoint reached
1235         upgrade_main_savepoint(true, 2012092600.00);
1236     }
1238     if ($oldversion < 2012101500.01) {
1239         // Find all orphaned blog associations that might exist.
1240         $sql = "SELECT ba.id
1241                   FROM {blog_association} ba
1242              LEFT JOIN {post} p
1243                     ON p.id = ba.blogid
1244                  WHERE p.id IS NULL";
1245         $orphanedrecordids = $DB->get_records_sql($sql);
1246         // Now delete these associations.
1247         foreach ($orphanedrecordids as $orphanedrecord) {
1248             $DB->delete_records('blog_association', array('id' => $orphanedrecord->id));
1249         }
1251         upgrade_main_savepoint(true, 2012101500.01);
1252     }
1254     if ($oldversion < 2012101800.02) {
1255         // Renaming backups using previous file naming convention.
1256         upgrade_rename_old_backup_files_using_shortname();
1258         // Main savepoint reached.
1259         upgrade_main_savepoint(true, 2012101800.02);
1260     }
1262     if ($oldversion < 2012103001.00) {
1263         // create new event_subscriptions table
1264         $table = new xmldb_table('event_subscriptions');
1265         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1266         $table->add_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1267         $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1268         $table->add_field('groupid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1269         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1270         $table->add_field('pollinterval', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1271         $table->add_field('lastupdated', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1272         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1273         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1274         if (!$dbman->table_exists($table)) {
1275             $dbman->create_table($table);
1276         }
1277         // Main savepoint reached
1278         upgrade_main_savepoint(true, 2012103001.00);
1279     }
1281     if ($oldversion < 2012103002.00) {
1282         // Add subscription field to the event table
1283         $table = new xmldb_table('event');
1284         $field = new xmldb_field('subscriptionid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'timemodified');
1286         // Conditionally launch add field subscriptionid
1287         if (!$dbman->field_exists($table, $field)) {
1288             $dbman->add_field($table, $field);
1289         }
1290         upgrade_main_savepoint(true, 2012103002.00);
1291     }
1293     if ($oldversion < 2012103003.00) {
1294         // Fix uuid field in event table to match RFC-2445 UID property.
1295         $table = new xmldb_table('event');
1296         $field = new xmldb_field('uuid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'visible');
1297         // The column already exists, so make sure there are no nulls (crazy mysql).
1298         $DB->set_field_select('event', 'uuid', '', "uuid IS NULL");
1299         // Changing precision of field uuid on table event to (255).
1300         $dbman->change_field_precision($table, $field);
1301         // Main savepoint reached
1302         upgrade_main_savepoint(true, 2012103003.00);
1303     }
1305     if ($oldversion < 2012110200.00) {
1307         // Define table course_format_options to be created
1308         $table = new xmldb_table('course_format_options');
1310         // Adding fields to table course_format_options
1311         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1312         $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1313         $table->add_field('format', XMLDB_TYPE_CHAR, '21', null, XMLDB_NOTNULL, null, null);
1314         $table->add_field('sectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'format');
1315         $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1316         $table->add_field('value', XMLDB_TYPE_TEXT, null, null, null, null, null);
1318         // Adding keys to table course_format_options
1319         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1320         $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1322         // Adding indexes to table course_format_options
1323         $table->add_index('formatoption', XMLDB_INDEX_UNIQUE, array('courseid', 'format', 'sectionid', 'name'));
1325         // Conditionally launch create table for course_format_options
1326         if (!$dbman->table_exists($table)) {
1327             $dbman->create_table($table);
1328         }
1330         // Changing type of field format on table course to char with length 21
1331         $table = new xmldb_table('course');
1332         $field = new xmldb_field('format', XMLDB_TYPE_CHAR, '21', null, XMLDB_NOTNULL, null, 'topics', 'summaryformat');
1334         // Launch change of type for field format
1335         $dbman->change_field_type($table, $field);
1337         // Main savepoint reached
1338         upgrade_main_savepoint(true, 2012110200.00);
1339     }
1341     if ($oldversion < 2012110201.00) {
1343         // Copy fields 'coursedisplay', 'numsections', 'hiddensections' from table {course}
1344         // to table {course_format_options} as the additional format options
1345         $fields = array();
1346         $table = new xmldb_table('course');
1347         foreach (array('coursedisplay', 'numsections', 'hiddensections') as $fieldname) {
1348             // first check that fields still exist
1349             $field = new xmldb_field($fieldname);
1350             if ($dbman->field_exists($table, $field)) {
1351                 $fields[] = $fieldname;
1352             }
1353         }
1355         if (!empty($fields)) {
1356             $transaction = $DB->start_delegated_transaction();
1357             $rs = $DB->get_recordset_sql('SELECT id, format, '. join(',', $fields).'
1358                 FROM {course}
1359                 WHERE format <> ? AND format <> ?',
1360                 array('scorm', 'social'));
1361             // (do not copy fields from scrom and social formats, we already know that they are not used)
1362             foreach ($rs as $rec) {
1363                 foreach ($fields as $field) {
1364                     try {
1365                         $DB->insert_record('course_format_options',
1366                                 array(
1367                                     'courseid'  => $rec->id,
1368                                     'format'    => $rec->format,
1369                                     'sectionid' => 0,
1370                                     'name'      => $field,
1371                                     'value'     => $rec->$field
1372                                 ));
1373                     } catch (dml_exception $e) {
1374                         // index 'courseid,format,sectionid,name' violation
1375                         // continue; the entry in course_format_options already exists, use it
1376                     }
1377                 }
1378             }
1379             $rs->close();
1380             $transaction->allow_commit();
1382             // Drop fields from table course
1383             foreach ($fields as $fieldname) {
1384                 $field = new xmldb_field($fieldname);
1385                 $dbman->drop_field($table, $field);
1386             }
1387         }
1389         // Main savepoint reached
1390         upgrade_main_savepoint(true, 2012110201.00);
1391     }
1393     if ($oldversion < 2012110700.01) {
1395         // Define field caller_component to be added to portfolio_log.
1396         $table = new xmldb_table('portfolio_log');
1397         $field = new xmldb_field('caller_component', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'caller_file');
1399         // Conditionally launch add field caller_component.
1400         if (!$dbman->field_exists($table, $field)) {
1401             $dbman->add_field($table, $field);
1402         }
1404         // Main savepoint reached.
1405         upgrade_main_savepoint(true, 2012110700.01);
1406     }
1408     if ($oldversion < 2012111200.00) {
1410         // Define table temp_enroled_template to be created
1411         $table = new xmldb_table('temp_enroled_template');
1413         // Adding fields to table temp_enroled_template
1414         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1415         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1416         $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1417         $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1419         // Adding keys to table temp_enroled_template
1420         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1422         // Adding indexes to table temp_enroled_template
1423         $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
1424         $table->add_index('courseid', XMLDB_INDEX_NOTUNIQUE, array('courseid'));
1425         $table->add_index('roleid', XMLDB_INDEX_NOTUNIQUE, array('roleid'));
1427         // Conditionally launch create table for temp_enroled_template
1428         if (!$dbman->table_exists($table)) {
1429             $dbman->create_table($table);
1430         }
1432         // Define table temp_log_template to be created
1433         $table = new xmldb_table('temp_log_template');
1435         // Adding fields to table temp_log_template
1436         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1437         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1438         $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1439         $table->add_field('action', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
1441         // Adding keys to table temp_log_template
1442         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1444         // Adding indexes to table temp_log_template
1445         $table->add_index('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
1446         $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
1447         $table->add_index('user', XMLDB_INDEX_NOTUNIQUE, array('userid'));
1448         $table->add_index('usercourseaction', XMLDB_INDEX_NOTUNIQUE, array('userid', 'course', 'action'));
1450         // Conditionally launch create table for temp_log_template
1451         if (!$dbman->table_exists($table)) {
1452             $dbman->create_table($table);
1453         }
1455         // Main savepoint reached
1456         upgrade_main_savepoint(true, 2012111200.00);
1457     }
1459     if ($oldversion < 2012111200.01) {
1460         // Force the rebuild of the cache of every courses, some cached information could contain wrong icon references.
1461         $DB->execute('UPDATE {course} set modinfo = ?, sectioncache = ?', array(null, null));
1463         // Main savepoint reached.
1464         upgrade_main_savepoint(true, 2012111200.01);
1465     }
1467     if ($oldversion < 2012111601.01) {
1468         // Clea up after old shared memory caching support.
1469         unset_config('cachetype');
1470         unset_config('rcache');
1471         unset_config('rcachettl');
1472         unset_config('intcachemax');
1473         unset_config('memcachedhosts');
1474         unset_config('memcachedpconn');
1476         // Main savepoint reached.
1477         upgrade_main_savepoint(true, 2012111601.01);
1478     }
1480     if ($oldversion < 2012112100.00) {
1482         // Define field eventtype to be added to event_subscriptions.
1483         $table = new xmldb_table('event_subscriptions');
1484         $field = new xmldb_field('eventtype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'userid');
1486         // Conditionally launch add field eventtype.
1487         if (!$dbman->field_exists($table, $field)) {
1488             $dbman->add_field($table, $field);
1489         }
1491         // Main savepoint reached.
1492         upgrade_main_savepoint(true, 2012112100.00);
1493     }
1495     // Moodle v2.4.0 release upgrade line
1496     // Put any upgrade step following this
1498     if ($oldversion < 2012120300.01) {
1499         // Make sure site-course has format='site' //MDL-36840
1501         if ($SITE->format !== 'site') {
1502             $DB->set_field('course', 'format', 'site', array('id' => $SITE->id));
1503             $SITE->format = 'site';
1504         }
1506         // Main savepoint reached
1507         upgrade_main_savepoint(true, 2012120300.01);
1508     }
1510     if ($oldversion < 2012120300.04) {
1511         // Remove "_utf8" suffix from all langs in course table.
1512         $langs = $DB->get_records_sql("SELECT DISTINCT lang FROM {course} WHERE lang LIKE ?", array('%_utf8'));
1514         foreach ($langs as $lang=>$unused) {
1515             $newlang = str_replace('_utf8', '', $lang);
1516             $sql = "UPDATE {course} SET lang = :newlang WHERE lang = :lang";
1517             $DB->execute($sql, array('newlang'=>$newlang, 'lang'=>$lang));
1518         }
1520         // Main savepoint reached.
1521         upgrade_main_savepoint(true, 2012120300.04);
1522     }
1524     if ($oldversion < 2012123000.00) {
1525         // Purge removed module filters and all their settings.
1527         $tables = array('filter_active', 'filter_config');
1528         foreach ($tables as $table) {
1529             $DB->delete_records_select($table, "filter LIKE 'mod/%'");
1530             $filters = $DB->get_records_sql("SELECT DISTINCT filter FROM {{$table}} WHERE filter LIKE 'filter/%'");
1531             foreach ($filters as $filter) {
1532                 $DB->set_field($table, 'filter', substr($filter->filter, 7), array('filter'=>$filter->filter));
1533             }
1534         }
1536         $configs = array('stringfilters', 'filterall');
1537         foreach ($configs as $config) {
1538             if ($filters = get_config(null, $config)) {
1539                 $filters = explode(',', $filters);
1540                 $newfilters = array();
1541                 foreach($filters as $filter) {
1542                     if (strpos($filter, '/') === false) {
1543                         $newfilters[] = $filter;
1544                     } else if (strpos($filter, 'filter/') === 0) {
1545                         $newfilters[] = substr($filter, 7);
1546                     }
1547                 }
1548                 $filters = implode(',', $newfilters);
1549                 set_config($config, $filters);
1550             }
1551         }
1553         unset($tables);
1554         unset($table);
1555         unset($configs);
1556         unset($newfilters);
1557         unset($filters);
1558         unset($filter);
1560         // Main savepoint reached.
1561         upgrade_main_savepoint(true, 2012123000.00);
1562     }
1564     if ($oldversion < 2013021100.01) {
1566         // Changing precision of field password on table user to (255).
1567         $table = new xmldb_table('user');
1568         $field = new xmldb_field('password', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'username');
1570         // Launch change of precision for field password.
1571         $dbman->change_field_precision($table, $field);
1573         // Main savepoint reached.
1574         upgrade_main_savepoint(true, 2013021100.01);
1575     }
1577     if ($oldversion < 2013021800.00) {
1578         // Add the site identifier to the cache config's file.
1579         $siteidentifier = $DB->get_field('config', 'value', array('name' => 'siteidentifier'));
1580         cache_helper::update_site_identifier($siteidentifier);
1582         // Main savepoint reached.
1583         upgrade_main_savepoint(true, 2013021800.00);
1584     }
1586     if ($oldversion < 2013021801.00) {
1587         // Fixing possible wrong MIME types for SMART Notebook files.
1588         $extensions = array('%.gallery', '%.galleryitem', '%.gallerycollection', '%.nbk', '%.notebook', '%.xbk');
1589         $select = $DB->sql_like('filename', '?', false);
1590         foreach ($extensions as $extension) {
1591             $DB->set_field_select(
1592                 'files',
1593                 'mimetype',
1594                 'application/x-smarttech-notebook',
1595                 $select,
1596                 array($extension)
1597             );
1598         }
1599         upgrade_main_savepoint(true, 2013021801.00);
1600     }
1602     if ($oldversion < 2013021801.01) {
1603         // This upgrade step is re-written under MDL-38228 (see below).
1604         /*
1605         // Retrieve the list of course_sections as a recordset to save memory
1606         $coursesections = $DB->get_recordset('course_sections', null, 'course, id', 'id, course, sequence');
1607         foreach ($coursesections as $coursesection) {
1608             // Retrieve all of the actual modules in this course and section combination to reduce DB calls
1609             $actualsectionmodules = $DB->get_records('course_modules',
1610                     array('course' => $coursesection->course, 'section' => $coursesection->id), '', 'id, section');
1612             // Break out the current sequence so that we can compare it
1613             $currentsequence = explode(',', $coursesection->sequence);
1614             $newsequence = array();
1616             // Check each of the modules in the current sequence
1617             foreach ($currentsequence as $module) {
1618                 if (isset($actualsectionmodules[$module])) {
1619                     $newsequence[] = $module;
1620                     // We unset the actualsectionmodules so that we don't get duplicates and that we can add orphaned
1621                     // modules later
1622                     unset($actualsectionmodules[$module]);
1623                 }
1624             }
1626             // Append any modules which have somehow been orphaned
1627             foreach ($actualsectionmodules as $module) {
1628                 $newsequence[] = $module->id;
1629             }
1631             // Piece it all back together
1632             $sequence = implode(',', $newsequence);
1634             // Only update if there have been changes
1635             if ($sequence !== $coursesection->sequence) {
1636                 $coursesection->sequence = $sequence;
1637                 $DB->update_record('course_sections', $coursesection);
1639                 // And clear the sectioncache and modinfo cache - they'll be regenerated on next use
1640                 $course = new stdClass();
1641                 $course->id = $coursesection->course;
1642                 $course->sectioncache = null;
1643                 $course->modinfo = null;
1644                 $DB->update_record('course', $course);
1645             }
1646         }
1647         $coursesections->close();
1648         */
1649         // Main savepoint reached.
1650         upgrade_main_savepoint(true, 2013021801.01);
1651     }
1653     if ($oldversion < 2013021902.00) {
1654         // ISO country change: Netherlands Antilles is split into BQ, CW & SX
1655         // http://www.iso.org/iso/iso_3166-1_newsletter_vi-8_split_of_the_dutch_antilles_final-en.pdf
1656         $sql = "UPDATE {user} SET country = '' WHERE country = ?";
1657         $DB->execute($sql, array('AN'));
1659         upgrade_main_savepoint(true, 2013021902.00);
1660     }
1662     if ($oldversion < 2013022600.00) {
1663         // Delete entries regarding invalid 'interests' option which breaks course.
1664         $DB->delete_records('course_sections_avail_fields', array('userfield' => 'interests'));
1665         $DB->delete_records('course_modules_avail_fields', array('userfield' => 'interests'));
1666         // Clear course cache (will be rebuilt on first visit) in case of changes to these.
1667         $DB->execute('UPDATE {course} set modinfo = ?, sectioncache = ?', array(null, null));
1669         upgrade_main_savepoint(true, 2013022600.00);
1670     }
1672     // Add index to field "timemodified" for grade_grades_history table.
1673     if ($oldversion < 2013030400.00) {
1674         $table = new xmldb_table('grade_grades_history');
1675         $field = new xmldb_field('timemodified');
1677         if ($dbman->field_exists($table, $field)) {
1678             $index = new xmldb_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1679             if (!$dbman->index_exists($table, $index)) {
1680                 $dbman->add_index($table, $index);
1681             }
1682         }
1684         // Main savepoint reached.
1685         upgrade_main_savepoint(true, 2013030400.00);
1686     }
1688     if ($oldversion < 2013030400.02) {
1689         // Cleanup qformat blackboard settings.
1690         unset_all_config_for_plugin('qformat_blackboard');
1692         upgrade_main_savepoint(true, 2013030400.02);
1693     }
1695     // This is checking to see if the site has been running a specific version with a bug in it
1696     // because this upgrade step is slow and is only needed if the site has been running with the affected versions.
1697     if ($oldversion >= 2012062504.08 && $oldversion < 2012062504.13) {
1698         // This upgrade step is re-written under MDL-38228 (see below).
1700         /*
1701         // Retrieve the list of course_sections as a recordset to save memory.
1702         // This is to fix a regression caused by MDL-37939.
1703         // In this case the upgrade step is fixing records where:
1704         // The data in course_sections.sequence contains the correct module id
1705         // The section field for on the course modules table may have been updated to point to the incorrect id.
1707         // This query is looking for sections where the sequence is not in sync with the course_modules table.
1708         // The syntax for the like query is looking for a value in a comma separated list.
1709         // It adds a comma to either site of the list and then searches for LIKE '%,id,%'.
1710         $sequenceconcat = $DB->sql_concat("','", 's.sequence', "','");
1711         $moduleconcat = $DB->sql_concat("'%,'", 'cm.id', "',%'");
1712         $sql = 'SELECT s2.id, s2.course, s2.sequence
1713                 FROM {course_sections} s2
1714                 JOIN(
1715                     SELECT DISTINCT s.id
1716                     FROM
1717                     {course_modules} cm
1718                     JOIN {course_sections} s
1719                     ON
1720                         cm.course = s.course
1721                     WHERE cm.section != s.id AND ' . $sequenceconcat . ' LIKE ' . $moduleconcat . '
1722                 ) d
1723                 ON s2.id = d.id';
1724         $coursesections = $DB->get_recordset_sql($sql);
1726         foreach ($coursesections as $coursesection) {
1727             // Retrieve all of the actual modules in this course and section combination to reduce DB calls.
1728             $actualsectionmodules = $DB->get_records('course_modules',
1729                     array('course' => $coursesection->course, 'section' => $coursesection->id), '', 'id, section');
1731             // Break out the current sequence so that we can compare it.
1732             $currentsequence = explode(',', $coursesection->sequence);
1733             $orphanlist = array();
1735             // Check each of the modules in the current sequence.
1736             foreach ($currentsequence as $cmid) {
1737                 if (!empty($cmid) && !isset($actualsectionmodules[$cmid])) {
1738                     $orphanlist[] = $cmid;
1739                 }
1740             }
1742             if (!empty($orphanlist)) {
1743                 list($sql, $params) = $DB->get_in_or_equal($orphanlist, SQL_PARAMS_NAMED);
1744                 $sql = "id $sql";
1746                 $DB->set_field_select('course_modules', 'section', $coursesection->id, $sql, $params);
1748                 // And clear the sectioncache and modinfo cache - they'll be regenerated on next use.
1749                 $course = new stdClass();
1750                 $course->id = $coursesection->course;
1751                 $course->sectioncache = null;
1752                 $course->modinfo = null;
1753                 $DB->update_record('course', $course);
1754             }
1755         }
1756         $coursesections->close();
1758         // No savepoint needed for this change.
1759          */
1760     }
1762     if ($oldversion < 2013032200.01) {
1763         // GD is now always available
1764         set_config('gdversion', 2);
1766         upgrade_main_savepoint(true, 2013032200.01);
1767     }
1769     if ($oldversion < 2013032600.03) {
1770         // Fixing possible wrong MIME type for MIME HTML (MHTML) files.
1771         $extensions = array('%.mht', '%.mhtml');
1772         $select = $DB->sql_like('filename', '?', false);
1773         foreach ($extensions as $extension) {
1774             $DB->set_field_select(
1775                 'files',
1776                 'mimetype',
1777                 'message/rfc822',
1778                 $select,
1779                 array($extension)
1780             );
1781         }
1782         upgrade_main_savepoint(true, 2013032600.03);
1783     }
1785     if ($oldversion < 2013032600.04) {
1786         // MDL-31983 broke the quiz version number. Fix it.
1787         $DB->set_field('modules', 'version', '2013021500',
1788                 array('name' => 'quiz', 'version' => '2013310100'));
1789         upgrade_main_savepoint(true, 2013032600.04);
1790     }
1792     if ($oldversion < 2013040200.00) {
1793         // Add openbadges tables.
1795         // Define table 'badge' to be created.
1796         $table = new xmldb_table('badge');
1798         // Adding fields to table 'badge'.
1799         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1800         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'id');
1801         $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null, 'name');
1802         $table->add_field('image', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'description');
1803         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'image');
1804         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'timecreated');
1805         $table->add_field('usercreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'timemodified');
1806         $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'usercreated');
1807         $table->add_field('issuername', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'usermodified');
1808         $table->add_field('issuerurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'issuername');
1809         $table->add_field('issuercontact', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'issuerurl');
1810         $table->add_field('expiredate', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'issuercontact');
1811         $table->add_field('expireperiod', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'expiredate');
1812         $table->add_field('type', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'expireperiod');
1813         $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'type');
1814         $table->add_field('message', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'courseid');
1815         $table->add_field('messagesubject', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'message');
1816         $table->add_field('attachment', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'messagesubject');
1817         $table->add_field('notification', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'attachment');
1818         $table->add_field('status', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'notification');
1819         $table->add_field('nextcron', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'status');
1821         // Adding keys to table 'badge'.
1822         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1823         $table->add_key('fk_courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1824         $table->add_key('fk_usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
1825         $table->add_key('fk_usercreated', XMLDB_KEY_FOREIGN, array('usercreated'), 'user', array('id'));
1827         // Adding indexes to table 'badge'.
1828         $table->add_index('type', XMLDB_INDEX_NOTUNIQUE, array('type'));
1830         // Conditionally launch create table for 'badge'.
1831         if (!$dbman->table_exists($table)) {
1832             $dbman->create_table($table);
1833         }
1835         // Define table 'badge_criteria' to be created.
1836         $table = new xmldb_table('badge_criteria');
1838         // Adding fields to table 'badge_criteria'.
1839         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1840         $table->add_field('badgeid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
1841         $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'badgeid');
1842         $table->add_field('method', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'criteriatype');
1844         // Adding keys to table 'badge_criteria'.
1845         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1846         $table->add_key('fk_badgeid', XMLDB_KEY_FOREIGN, array('badgeid'), 'badge', array('id'));
1848         // Adding indexes to table 'badge_criteria'.
1849         $table->add_index('criteriatype', XMLDB_INDEX_NOTUNIQUE, array('criteriatype'));
1850         $table->add_index('badgecriteriatype', XMLDB_INDEX_UNIQUE, array('badgeid', 'criteriatype'));
1852         // Conditionally launch create table for 'badge_criteria'.
1853         if (!$dbman->table_exists($table)) {
1854             $dbman->create_table($table);
1855         }
1857         // Define table 'badge_criteria_param' to be created.
1858         $table = new xmldb_table('badge_criteria_param');
1860         // Adding fields to table 'badge_criteria_param'.
1861         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1862         $table->add_field('critid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id');
1863         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'critid');
1864         $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'name');
1866         // Adding keys to table 'badge_criteria_param'.
1867         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1868         $table->add_key('fk_critid', XMLDB_KEY_FOREIGN, array('critid'), 'badge_criteria', array('id'));
1870         // Conditionally launch create table for 'badge_criteria_param'.
1871         if (!$dbman->table_exists($table)) {
1872             $dbman->create_table($table);
1873         }
1875         // Define table 'badge_issued' to be created.
1876         $table = new xmldb_table('badge_issued');
1878         // Adding fields to table 'badge_issued'.
1879         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1880         $table->add_field('badgeid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
1881         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'badgeid');
1882         $table->add_field('uniquehash', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'userid');
1883         $table->add_field('dateissued', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'uniquehash');
1884         $table->add_field('dateexpire', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'dateissued');
1885         $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'dateexpire');
1886         $table->add_field('issuernotified', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'visible');
1888         // Adding keys to table 'badge_issued'.
1889         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1890         $table->add_key('fk_badgeid', XMLDB_KEY_FOREIGN, array('badgeid'), 'badge', array('id'));
1891         $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1893         $table->add_index('badgeuser', XMLDB_INDEX_UNIQUE, array('badgeid', 'userid'));
1895         // Conditionally launch create table for 'badge_issued'.
1896         if (!$dbman->table_exists($table)) {
1897             $dbman->create_table($table);
1898         }
1900         // Define table 'badge_criteria_met' to be created.
1901         $table = new xmldb_table('badge_criteria_met');
1903         // Adding fields to table 'badge_criteria_met'.
1904         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1905         $table->add_field('issuedid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'id');
1906         $table->add_field('critid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'issuedid');
1907         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'critid');
1908         $table->add_field('datemet', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'userid');
1910         // Adding keys to table 'badge_criteria_met'
1911         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1912         $table->add_key('fk_critid', XMLDB_KEY_FOREIGN, array('critid'), 'badge_criteria', array('id'));
1913         $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1914         $table->add_key('fk_issuedid', XMLDB_KEY_FOREIGN, array('issuedid'), 'badge_issued', array('id'));
1916         // Conditionally launch create table for 'badge_criteria_met'.
1917         if (!$dbman->table_exists($table)) {
1918             $dbman->create_table($table);
1919         }
1921         // Define table 'badge_manual_award' to be created.
1922         $table = new xmldb_table('badge_manual_award');
1924         // Adding fields to table 'badge_manual_award'.
1925         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1926         $table->add_field('badgeid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id');
1927         $table->add_field('recipientid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'badgeid');
1928         $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'recipientid');
1929         $table->add_field('issuerrole', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'issuerid');
1930         $table->add_field('datemet', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'issuerrole');
1932         // Adding keys to table 'badge_manual_award'.
1933         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1934         $table->add_key('fk_badgeid', XMLDB_KEY_FOREIGN, array('badgeid'), 'badge', array('id'));
1935         $table->add_key('fk_recipientid', XMLDB_KEY_FOREIGN, array('recipientid'), 'user', array('id'));
1936         $table->add_key('fk_issuerid', XMLDB_KEY_FOREIGN, array('issuerid'), 'user', array('id'));
1937         $table->add_key('fk_issuerrole', XMLDB_KEY_FOREIGN, array('issuerrole'), 'role', array('id'));
1939         // Conditionally launch create table for 'badge_manual_award'.
1940         if (!$dbman->table_exists($table)) {
1941             $dbman->create_table($table);
1942         }
1944         // Define table 'badge_backpack' to be created.
1945         $table = new xmldb_table('badge_backpack');
1947         // Adding fields to table 'badge_backpack'.
1948         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1949         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
1950         $table->add_field('email', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'userid');
1951         $table->add_field('backpackurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'email');
1952         $table->add_field('backpackuid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'backpackurl');
1953         $table->add_field('backpackgid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'backpackuid');
1954         $table->add_field('autosync', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'backpackgid');
1955         $table->add_field('password', XMLDB_TYPE_CHAR, '50', null, null, null, null, 'autosync');
1957         // Adding keys to table 'badge_backpack'.
1958         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1959         $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1961         // Conditionally launch create table for 'badge_backpack'.
1962         if (!$dbman->table_exists($table)) {
1963             $dbman->create_table($table);
1964         }
1966         // Main savepoint reached.
1967         upgrade_main_savepoint(true, 2013040200.00);
1968     }
1970     if ($oldversion < 2013040201.00) {
1971         // Convert name field in event table to text type as RFC-2445 doesn't have any limitation on it.
1972         $table = new xmldb_table('event');
1973         $field = new xmldb_field('name', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1974         if ($dbman->field_exists($table, $field)) {
1975             $dbman->change_field_type($table, $field);
1976         }
1977         // Main savepoint reached.
1978         upgrade_main_savepoint(true, 2013040201.00);
1979     }
1981     if ($oldversion < 2013040300.01) {
1983         // Define field completionstartonenrol to be dropped from course.
1984         $table = new xmldb_table('course');
1985         $field = new xmldb_field('completionstartonenrol');
1987         // Conditionally launch drop field completionstartonenrol.
1988         if ($dbman->field_exists($table, $field)) {
1989             $dbman->drop_field($table, $field);
1990         }
1992         // Main savepoint reached.
1993         upgrade_main_savepoint(true, 2013040300.01);
1994     }
1996     if ($oldversion < 2013041200.00) {
1997         // MDL-29877 Some bad restores created grade items with no category information.
1998         $sql = "UPDATE {grade_items}
1999                    SET categoryid = courseid
2000                  WHERE itemtype <> 'course' and itemtype <> 'category'
2001                        AND categoryid IS NULL";
2002         $DB->execute($sql);
2003         upgrade_main_savepoint(true, 2013041200.00);
2004     }
2006     if ($oldversion < 2013041600.00) {
2007         // Copy constants from /course/lib.php instead of including the whole library:
2008         $c = array( 'FRONTPAGENEWS'                 => 0,
2009                     'FRONTPAGECOURSELIST'           => 1,
2010                     'FRONTPAGECATEGORYNAMES'        => 2,
2011                     'FRONTPAGETOPICONLY'            => 3,
2012                     'FRONTPAGECATEGORYCOMBO'        => 4,
2013                     'FRONTPAGEENROLLEDCOURSELIST'   => 5,
2014                     'FRONTPAGEALLCOURSELIST'        => 6,
2015                     'FRONTPAGECOURSESEARCH'         => 7);
2016         // Update frontpage settings $CFG->frontpage and $CFG->frontpageloggedin. In 2.4 there was too much of hidden logic about them.
2017         // This script tries to make sure that with the new (more user-friendly) frontpage settings the frontpage looks as similar as possible to what it was before upgrade.
2018         $ncourses = $DB->count_records('course');
2019         foreach (array('frontpage', 'frontpageloggedin') as $configkey) {
2020             if ($frontpage = explode(',', $CFG->{$configkey})) {
2021                 $newfrontpage = array();
2022                 foreach ($frontpage as $v) {
2023                     switch ($v) {
2024                         case $c['FRONTPAGENEWS']:
2025                             // Not related to course listings, leave as it is.
2026                             $newfrontpage[] = $c['FRONTPAGENEWS'];
2027                             break;
2028                         case $c['FRONTPAGECOURSELIST']:
2029                             if ($configkey === 'frontpageloggedin' && empty($CFG->disablemycourses)) {
2030                                 // In 2.4 unless prohibited in config, the "list of courses" was considered "list of enrolled courses" plus course search box.
2031                                 $newfrontpage[] = $c['FRONTPAGEENROLLEDCOURSELIST'];
2032                             } else if ($ncourses <= 200) {
2033                                 // Still list of courses was only displayed in there were less than 200 courses in system. Otherwise - search box only.
2034                                 $newfrontpage[] = $c['FRONTPAGEALLCOURSELIST'];
2035                                 break; // skip adding search box
2036                             }
2037                             if (!in_array($c['FRONTPAGECOURSESEARCH'], $newfrontpage)) {
2038                                 $newfrontpage[] = $c['FRONTPAGECOURSESEARCH'];
2039                             }
2040                             break;
2041                         case $c['FRONTPAGECATEGORYNAMES']:
2042                             // In 2.4 search box was displayed automatically after categories list. In 2.5 it is displayed as a separate setting.
2043                             $newfrontpage[] = $c['FRONTPAGECATEGORYNAMES'];
2044                             if (!in_array($c['FRONTPAGECOURSESEARCH'], $newfrontpage)) {
2045                                 $newfrontpage[] = $c['FRONTPAGECOURSESEARCH'];
2046                             }
2047                             break;
2048                         case $c['FRONTPAGECATEGORYCOMBO']:
2049                             $maxcourses = empty($CFG->numcoursesincombo) ? 500 : $CFG->numcoursesincombo;
2050                             // In 2.4 combo list was not displayed if there are more than $CFG->numcoursesincombo courses in the system.
2051                             if ($ncourses < $maxcourses) {
2052                                 $newfrontpage[] = $c['FRONTPAGECATEGORYCOMBO'];
2053                             }
2054                             if (!in_array($c['FRONTPAGECOURSESEARCH'], $newfrontpage)) {
2055                                 $newfrontpage[] = $c['FRONTPAGECOURSESEARCH'];
2056                             }
2057                             break;
2058                     }
2059                 }
2060                 set_config($configkey, join(',', $newfrontpage));
2061             }
2062         }
2063         // $CFG->numcoursesincombo no longer affects whether the combo list is displayed. Setting is deprecated.
2064         unset_config('numcoursesincombo');
2066         upgrade_main_savepoint(true, 2013041600.00);
2067     }
2069     if ($oldversion < 2013041601.00) {
2070         // Create a new 'badge_external' table first.
2071         // Define table 'badge_external' to be created.
2072         $table = new xmldb_table('badge_external');
2074         // Adding fields to table 'badge_external'.
2075         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
2076         $table->add_field('backpackid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id');
2077         $table->add_field('collectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'backpackid');
2079         // Adding keys to table 'badge_external'.
2080         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2081         $table->add_key('fk_backpackid', XMLDB_KEY_FOREIGN, array('backpackid'), 'badge_backpack', array('id'));
2083         // Conditionally launch create table for 'badge_external'.
2084         if (!$dbman->table_exists($table)) {
2085             $dbman->create_table($table);
2086         }
2088         // Perform user data migration.
2089         $usercollections = $DB->get_records('badge_backpack');
2090         foreach ($usercollections as $usercollection) {
2091             $collection = new stdClass();
2092             $collection->backpackid = $usercollection->id;
2093             $collection->collectionid = $usercollection->backpackgid;
2094             $DB->insert_record('badge_external', $collection);
2095         }
2097         // Finally, drop the column.
2098         // Define field backpackgid to be dropped from 'badge_backpack'.
2099         $table = new xmldb_table('badge_backpack');
2100         $field = new xmldb_field('backpackgid');
2102         // Conditionally launch drop field backpackgid.
2103         if ($dbman->field_exists($table, $field)) {
2104             $dbman->drop_field($table, $field);
2105         }
2107         // Main savepoint reached.
2108         upgrade_main_savepoint(true, 2013041601.00);
2109     }
2111     if ($oldversion < 2013041601.01) {
2112         // Changing the default of field descriptionformat on table user to 1.
2113         $table = new xmldb_table('user');
2114         $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1', 'description');
2116         // Launch change of default for field descriptionformat.
2117         $dbman->change_field_default($table, $field);
2119         // Main savepoint reached.
2120         upgrade_main_savepoint(true, 2013041601.01);
2121     }
2123     if ($oldversion < 2013041900.00) {
2124         require_once($CFG->dirroot . '/cache/locallib.php');
2125         // The features bin needs updating.
2126         cache_config_writer::update_default_config_stores();
2127         // Main savepoint reached.
2128         upgrade_main_savepoint(true, 2013041900.00);
2129     }
2131     if ($oldversion < 2013042300.00) {
2132         // Adding index to unreadmessageid field of message_working table (MDL-34933)
2133         $table = new xmldb_table('message_working');
2134         $index = new xmldb_index('unreadmessageid_idx', XMLDB_INDEX_NOTUNIQUE, array('unreadmessageid'));
2136         // Conditionally launch add index unreadmessageid
2137         if (!$dbman->index_exists($table, $index)) {
2138             $dbman->add_index($table, $index);
2139         }
2141         // Main savepoint reached.
2142         upgrade_main_savepoint(true, 2013042300.00);
2143     }
2145     // Moodle v2.5.0 release upgrade line.
2146     // Put any upgrade step following this.
2148     if ($oldversion < 2013051400.01) {
2149         // Fix incorrect cc-nc url. Unfortunately the license 'plugins' do
2150         // not give a mechanism to do this.
2152         $sql = "UPDATE {license}
2153                    SET source = :url, version = :newversion
2154                  WHERE shortname = :shortname AND version = :oldversion";
2156         $params = array(
2157             'url' => 'http://creativecommons.org/licenses/by-nc/3.0/',
2158             'shortname' => 'cc-nc',
2159             'newversion' => '2013051500',
2160             'oldversion' => '2010033100'
2161         );
2163         $DB->execute($sql, $params);
2165         // Main savepoint reached.
2166         upgrade_main_savepoint(true, 2013051400.01);
2167     }
2169     if ($oldversion < 2013061400.01) {
2170         // Clean up old tokens which haven't been deleted.
2171         $DB->execute("DELETE FROM {user_private_key} WHERE NOT EXISTS
2172                          (SELECT 'x' FROM {user} WHERE deleted = 0 AND id = userid)");
2174         // Main savepoint reached.
2175         upgrade_main_savepoint(true, 2013061400.01);
2176     }
2178     if ($oldversion < 2013061700.00) {
2179         // MDL-40103: Remove unused template tables from the database.
2180         // These are now created inline with xmldb_table.
2182         $tablestocleanup = array('temp_enroled_template','temp_log_template','backup_files_template','backup_ids_template');
2183         $dbman = $DB->get_manager();
2185         foreach ($tablestocleanup as $table) {
2186             $xmltable = new xmldb_table($table);
2187             if ($dbman->table_exists($xmltable)) {
2188                 $dbman->drop_table($xmltable);
2189             }
2190         }
2192         // Main savepoint reached.
2193         upgrade_main_savepoint(true, 2013061700.00);
2194     }
2196     if ($oldversion < 2013070800.00) {
2198         // Remove orphan repository instances.
2199         if ($DB->get_dbfamily() === 'mysql') {
2200             $sql = "DELETE {repository_instances} FROM {repository_instances}
2201                     LEFT JOIN {context} ON {context}.id = {repository_instances}.contextid
2202                     WHERE {context}.id IS NULL";
2203         } else {
2204             $sql = "DELETE FROM {repository_instances}
2205                     WHERE NOT EXISTS (
2206                         SELECT 'x' FROM {context}
2207                         WHERE {context}.id = {repository_instances}.contextid)";
2208         }
2209         $DB->execute($sql);
2211         // Main savepoint reached.
2212         upgrade_main_savepoint(true, 2013070800.00);
2213     }
2215     if ($oldversion < 2013070800.01) {
2217         // Define field lastnamephonetic to be added to user.
2218         $table = new xmldb_table('user');
2219         $field = new xmldb_field('lastnamephonetic', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'imagealt');
2220         $index = new xmldb_index('lastnamephonetic', XMLDB_INDEX_NOTUNIQUE, array('lastnamephonetic'));
2222         // Conditionally launch add field lastnamephonetic.
2223         if (!$dbman->field_exists($table, $field)) {
2224             $dbman->add_field($table, $field);
2225             $dbman->add_index($table, $index);
2226         }
2228         // Define field firstnamephonetic to be added to user.
2229         $table = new xmldb_table('user');
2230         $field = new xmldb_field('firstnamephonetic', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'lastnamephonetic');
2231         $index = new xmldb_index('firstnamephonetic', XMLDB_INDEX_NOTUNIQUE, array('firstnamephonetic'));
2233         // Conditionally launch add field firstnamephonetic.
2234         if (!$dbman->field_exists($table, $field)) {
2235             $dbman->add_field($table, $field);
2236             $dbman->add_index($table, $index);
2237         }
2239         // Define field alternatename to be added to user.
2240         $table = new xmldb_table('user');
2241         $field = new xmldb_field('middlename', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'firstnamephonetic');
2242         $index = new xmldb_index('middlename', XMLDB_INDEX_NOTUNIQUE, array('middlename'));
2244         // Conditionally launch add field firstnamephonetic.
2245         if (!$dbman->field_exists($table, $field)) {
2246             $dbman->add_field($table, $field);
2247             $dbman->add_index($table, $index);
2248         }
2250         // Define field alternatename to be added to user.
2251         $table = new xmldb_table('user');
2252         $field = new xmldb_field('alternatename', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'middlename');
2253         $index = new xmldb_index('alternatename', XMLDB_INDEX_NOTUNIQUE, array('alternatename'));
2255         // Conditionally launch add field alternatename.
2256         if (!$dbman->field_exists($table, $field)) {
2257             $dbman->add_field($table, $field);
2258             $dbman->add_index($table, $index);
2259         }
2261         // Main savepoint reached.
2262         upgrade_main_savepoint(true, 2013070800.01);
2263     }
2265     if ($oldversion < 2013071500.01) {
2266         // The enrol_authorize plugin has been removed, if there are no records
2267         // and no plugin files then remove the plugin data.
2268         $enrolauthorize = new xmldb_table('enrol_authorize');
2269         $enrolauthorizerefunds = new xmldb_table('enrol_authorize_refunds');
2271         if (!file_exists($CFG->dirroot.'/enrol/authorize/version.php') &&
2272             $dbman->table_exists($enrolauthorize) &&
2273             $dbman->table_exists($enrolauthorizerefunds)) {
2275             $enrolauthorizecount = $DB->count_records('enrol_authorize');
2276             $enrolauthorizerefundcount = $DB->count_records('enrol_authorize_refunds');
2278             if (empty($enrolauthorizecount) && empty($enrolauthorizerefundcount)) {
2280                 // Drop the database tables.
2281                 $dbman->drop_table($enrolauthorize);
2282                 $dbman->drop_table($enrolauthorizerefunds);
2284                 // Drop the message provider and associated data manually.
2285                 $DB->delete_records('message_providers', array('component' => 'enrol_authorize'));
2286                 $DB->delete_records_select('config_plugins', "plugin = 'message' AND ".$DB->sql_like('name', '?', false), array("%_provider_enrol_authorize_%"));
2287                 $DB->delete_records_select('user_preferences', $DB->sql_like('name', '?', false), array("message_provider_enrol_authorize_%"));
2289                 // Remove capabilities.
2290                 capabilities_cleanup('enrol_authorize');
2292                 // Remove all other associated config.
2293                 unset_all_config_for_plugin('enrol_authorize');
2294             }
2295         }
2296         upgrade_main_savepoint(true, 2013071500.01);
2297     }
2299     if ($oldversion < 2013071500.02) {
2300         // Define field attachment to be dropped from badge.
2301         $table = new xmldb_table('badge');
2302         $field = new xmldb_field('image');
2304         // Conditionally launch drop field eventtype.
2305         if ($dbman->field_exists($table, $field)) {
2306             $dbman->drop_field($table, $field);
2307         }
2309         upgrade_main_savepoint(true, 2013071500.02);
2310     }
2312     if ($oldversion < 2013072600.01) {
2313         upgrade_mssql_nvarcharmax();
2314         upgrade_mssql_varbinarymax();
2316         upgrade_main_savepoint(true, 2013072600.01);
2317     }
2319     if ($oldversion < 2013081200.00) {
2320         // Define field uploadfiles to be added to external_services.
2321         $table = new xmldb_table('external_services');
2322         $field = new xmldb_field('uploadfiles', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'downloadfiles');
2324         // Conditionally launch add field uploadfiles.
2325         if (!$dbman->field_exists($table, $field)) {
2326             $dbman->add_field($table, $field);
2327         }
2329         // Main savepoint reached.
2330         upgrade_main_savepoint(true, 2013081200.00);
2331     }
2333     if ($oldversion < 2013082300.01) {
2334         // Define the table 'backup_logs' and the field 'message' which we will be changing from a char to a text field.
2335         $table = new xmldb_table('backup_logs');
2336         $field = new xmldb_field('message', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'loglevel');
2338         // Perform the change.
2339         $dbman->change_field_type($table, $field);
2341         // Main savepoint reached.
2342         upgrade_main_savepoint(true, 2013082300.01);
2343     }
2345     // Convert SCORM course format courses to singleactivity.
2346     if ($oldversion < 2013082700.00) {
2347         // First set relevant singleactivity settings.
2348         $formatoptions = new stdClass();
2349         $formatoptions->format = 'singleactivity';
2350         $formatoptions->sectionid = 0;
2351         $formatoptions->name = 'activitytype';
2352         $formatoptions->value = 'scorm';
2354         $courses = $DB->get_recordset('course', array('format' => 'scorm'), 'id');
2355         foreach ($courses as $course) {
2356             $formatoptions->courseid = $course->id;
2357             $DB->insert_record('course_format_options', $formatoptions);
2358         }
2359         $courses->close();
2361         // Now update course format for these courses.
2362         $sql = "UPDATE {course}
2363                    SET format = 'singleactivity', modinfo = '', sectioncache = ''
2364                  WHERE format = 'scorm'";
2365         $DB->execute($sql);
2366         upgrade_main_savepoint(true, 2013082700.00);
2367     }
2369     if ($oldversion < 2013090500.01) {
2370         // Define field calendartype to be added to course.
2371         $table = new xmldb_table('course');
2372         $field = new xmldb_field('calendartype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
2374         // Conditionally launch add field calendartype.
2375         if (!$dbman->field_exists($table, $field)) {
2376             $dbman->add_field($table, $field);
2377         }
2379         // Define field calendartype to be added to user.
2380         $table = new xmldb_table('user');
2381         $field = new xmldb_field('calendartype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'gregorian');
2383         // Conditionally launch add field calendartype.
2384         if (!$dbman->field_exists($table, $field)) {
2385             $dbman->add_field($table, $field);
2386         }
2388         // Main savepoint reached.
2389         upgrade_main_savepoint(true, 2013090500.01);
2390     }
2392     if ($oldversion < 2013091000.02) {
2394         // Define field cacherev to be added to course.
2395         $table = new xmldb_table('course');
2396         $field = new xmldb_field('cacherev', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'completionnotify');
2398         // Conditionally launch add field cacherev.
2399         if (!$dbman->field_exists($table, $field)) {
2400             $dbman->add_field($table, $field);
2401         }
2403         // Main savepoint reached.
2404         upgrade_main_savepoint(true, 2013091000.02);
2405     }
2407     if ($oldversion < 2013091000.03) {
2409         // Define field modinfo to be dropped from course.
2410         $table = new xmldb_table('course');
2411         $field = new xmldb_field('modinfo');
2413         // Conditionally launch drop field modinfo.
2414         if ($dbman->field_exists($table, $field)) {
2415             $dbman->drop_field($table, $field);
2416         }
2418         // Define field sectioncache to be dropped from course.
2419         $field = new xmldb_field('sectioncache');
2421         // Conditionally launch drop field sectioncache.
2422         if ($dbman->field_exists($table, $field)) {
2423             $dbman->drop_field($table, $field);
2424         }
2426         // Main savepoint reached.
2427         upgrade_main_savepoint(true, 2013091000.03);
2428     }
2430     if ($oldversion < 2013091300.01) {
2432         $table = new xmldb_table('user');
2434         // Changing precision of field institution on table user to (255).
2435         $field = new xmldb_field('institution', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'phone2');
2437         // Launch change of precision for field institution.
2438         $dbman->change_field_precision($table, $field);
2440         // Changing precision of field department on table user to (255).
2441         $field = new xmldb_field('department', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'institution');
2443         // Launch change of precision for field department.
2444         $dbman->change_field_precision($table, $field);
2446         // Changing precision of field address on table user to (255).
2447         $field = new xmldb_field('address', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'department');
2449         // Launch change of precision for field address.
2450         $dbman->change_field_precision($table, $field);
2452         // Main savepoint reached.
2453         upgrade_main_savepoint(true, 2013091300.01);
2454     }
2456     if ($oldversion < 2013092000.01) {
2458         // Define table question_statistics to be created.
2459         $table = new xmldb_table('question_statistics');
2461         // Adding fields to table question_statistics.
2462         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2463         $table->add_field('hashcode', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2464         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2465         $table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2466         $table->add_field('slot', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
2467         $table->add_field('subquestion', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
2468         $table->add_field('s', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2469         $table->add_field('effectiveweight', XMLDB_TYPE_NUMBER, '15, 5', null, null, null, null);
2470         $table->add_field('negcovar', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
2471         $table->add_field('discriminationindex', XMLDB_TYPE_NUMBER, '15, 5', null, null, null, null);
2472         $table->add_field('discriminativeefficiency', XMLDB_TYPE_NUMBER, '15, 5', null, null, null, null);
2473         $table->add_field('sd', XMLDB_TYPE_NUMBER, '15, 10', null, null, null, null);
2474         $table->add_field('facility', XMLDB_TYPE_NUMBER, '15, 10', null, null, null, null);
2475         $table->add_field('subquestions', XMLDB_TYPE_TEXT, null, null, null, null, null);
2476         $table->add_field('maxmark', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null);
2477         $table->add_field('positions', XMLDB_TYPE_TEXT, null, null, null, null, null);
2478         $table->add_field('randomguessscore', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null);
2480         // Adding keys to table question_statistics.
2481         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2483         // Conditionally launch create table for question_statistics.
2484         if (!$dbman->table_exists($table)) {
2485             $dbman->create_table($table);
2486         }
2488         // Define table question_response_analysis to be created.
2489         $table = new xmldb_table('question_response_analysis');
2491         // Adding fields to table question_response_analysis.
2492         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2493         $table->add_field('hashcode', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2494         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2495         $table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2496         $table->add_field('subqid', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2497         $table->add_field('aid', XMLDB_TYPE_CHAR, '100', null, null, null, null);
2498         $table->add_field('response', XMLDB_TYPE_TEXT, null, null, null, null, null);
2499         $table->add_field('rcount', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
2500         $table->add_field('credit', XMLDB_TYPE_NUMBER, '15, 5', null, XMLDB_NOTNULL, null, null);
2502         // Adding keys to table question_response_analysis.
2503         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2505         // Conditionally launch create table for question_response_analysis.
2506         if (!$dbman->table_exists($table)) {
2507             $dbman->create_table($table);
2508         }
2510         // Main savepoint reached.
2511         upgrade_main_savepoint(true, 2013092000.01);
2512     }
2514     if ($oldversion < 2013092001.01) {
2515         // Force uninstall of deleted tool.
2516         if (!file_exists("$CFG->dirroot/$CFG->admin/tool/bloglevelupgrade")) {
2517             // Remove capabilities.
2518             capabilities_cleanup('tool_bloglevelupgrade');
2519             // Remove all other associated config.
2520             unset_all_config_for_plugin('tool_bloglevelupgrade');
2521         }
2522         upgrade_main_savepoint(true, 2013092001.01);
2523     }
2525     if ($oldversion < 2013092001.02) {
2526         // Define field version to be dropped from modules.
2527         $table = new xmldb_table('modules');
2528         $field = new xmldb_field('version');
2530         // Conditionally launch drop field version.
2531         if ($dbman->field_exists($table, $field)) {
2532             // Migrate all plugin version info to config_plugins table.
2533             $modules = $DB->get_records('modules');
2534             foreach ($modules as $module) {
2535                 set_config('version', $module->version, 'mod_'.$module->name);
2536             }
2537             unset($modules);
2539             $dbman->drop_field($table, $field);
2540         }
2542         // Define field version to be dropped from block.
2543         $table = new xmldb_table('block');
2544         $field = new xmldb_field('version');
2546         // Conditionally launch drop field version.
2547         if ($dbman->field_exists($table, $field)) {
2548             $blocks = $DB->get_records('block');
2549             foreach ($blocks as $block) {
2550                 set_config('version', $block->version, 'block_'.$block->name);
2551             }
2552             unset($blocks);
2554             $dbman->drop_field($table, $field);
2555         }
2557         // Main savepoint reached.
2558         upgrade_main_savepoint(true, 2013092001.02);
2559     }
2561     if ($oldversion < 2013092700.01) {
2563         $table = new xmldb_table('files');
2565         // Define field referencelastsync to be dropped from files.
2566         $field = new xmldb_field('referencelastsync');
2568         // Conditionally launch drop field referencelastsync.
2569         if ($dbman->field_exists($table, $field)) {
2570             $dbman->drop_field($table, $field);
2571         }
2573         // Define field referencelifetime to be dropped from files.
2574         $field = new xmldb_field('referencelifetime');
2576         // Conditionally launch drop field referencelifetime.
2577         if ($dbman->field_exists($table, $field)) {
2578             $dbman->drop_field($table, $field);
2579         }
2581         // Main savepoint reached.
2582         upgrade_main_savepoint(true, 2013092700.01);
2583     }
2585     if ($oldversion < 2013100400.01) {
2586         // Add user_devices core table.
2588         // Define field id to be added to user_devices.
2589         $table = new xmldb_table('user_devices');
2591         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
2592         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
2593         $table->add_field('appid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null, 'userid');
2594         $table->add_field('name', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'appid');
2595         $table->add_field('model', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'name');
2596         $table->add_field('platform', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'model');
2597         $table->add_field('version', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'platform');
2598         $table->add_field('pushid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'version');
2599         $table->add_field('uuid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'pushid');
2600         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'uuid');
2601         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'timecreated');
2603         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2604         $table->add_key('pushid-userid', XMLDB_KEY_UNIQUE, array('pushid', 'userid'));
2605         $table->add_key('pushid-platform', XMLDB_KEY_UNIQUE, array('pushid', 'platform'));
2606         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2608         if (!$dbman->table_exists($table)) {
2609             $dbman->create_table($table);
2610         }
2612         // Main savepoint reached.
2613         upgrade_main_savepoint(true, 2013100400.01);
2614     }
2616     if ($oldversion < 2013100800.00) {
2618         // Define field maxfraction to be added to question_attempts.
2619         $table = new xmldb_table('question_attempts');
2620         $field = new xmldb_field('maxfraction', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '1', 'minfraction');
2622         // Conditionally launch add field maxfraction.
2623         if (!$dbman->field_exists($table, $field)) {
2624             $dbman->add_field($table, $field);
2625         }
2627         // Main savepoint reached.
2628         upgrade_main_savepoint(true, 2013100800.00);
2629     }
2631     if ($oldversion < 2013100800.01) {
2632         // Create a new 'user_password_resets' table.
2633         $table = new xmldb_table('user_password_resets');
2634         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
2635         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null);
2636         $table->add_field('timerequested', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null);
2637         $table->add_field('timererequested', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, null);
2638         $table->add_field('token', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, null);
2640         // Adding keys to table.
2641         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2642         $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2644         // Conditionally launch create table.
2645         if (!$dbman->table_exists($table)) {
2646             $dbman->create_table($table);
2647         }
2648         upgrade_main_savepoint(true, 2013100800.01);
2649     }
2651     if ($oldversion < 2013100800.02) {
2652         $sql = "INSERT INTO {user_preferences}(userid, name, value)
2653                 SELECT id, 'htmleditor', 'textarea' FROM {user} u where u.htmleditor = 0";
2654         $DB->execute($sql);
2656         // Define field htmleditor to be dropped from user
2657         $table = new xmldb_table('user');
2658         $field = new xmldb_field('htmleditor');
2660         // Conditionally launch drop field requested
2661         if ($dbman->field_exists($table, $field)) {
2662             $dbman->drop_field($table, $field);
2663         }
2664         // Main savepoint reached.
2665         upgrade_main_savepoint(true, 2013100800.02);
2666     }
2668     if ($oldversion < 2013100900.00) {
2670         // Define field lifetime to be dropped from files_reference.
2671         $table = new xmldb_table('files_reference');
2672         $field = new xmldb_field('lifetime');
2674         // Conditionally launch drop field lifetime.
2675         if ($dbman->field_exists($table, $field)) {
2676             $dbman->drop_field($table, $field);
2677         }
2679         // Main savepoint reached.
2680         upgrade_main_savepoint(true, 2013100900.00);
2681     }
2683     if ($oldversion < 2013100901.00) {
2684         // Fixing possible wrong MIME type for Java Network Launch Protocol (JNLP) files.
2685         $select = $DB->sql_like('filename', '?', false);
2686         $DB->set_field_select(
2687             'files',
2688             'mimetype',
2689             'application/x-java-jnlp-file',
2690             $select,
2691             array('%.jnlp')
2692         );
2694         // Main savepoint reached.
2695         upgrade_main_savepoint(true, 2013100901.00);
2696     }
2698     if ($oldversion < 2013102100.00) {
2699         // Changing default value for the status of a course backup.
2700         $table = new xmldb_table('backup_courses');
2701         $field = new xmldb_field('laststatus', XMLDB_TYPE_CHAR, '1', null, XMLDB_NOTNULL, null, '5', 'lastendtime');
2703         // Launch change of precision for field value
2704         $dbman->change_field_precision($table, $field);
2706         // Main savepoint reached.
2707         upgrade_main_savepoint(true, 2013102100.00);
2708     }
2710     if ($oldversion < 2013102201.00) {
2711         $params = array('plugin' => 'editor_atto', 'name' => 'version');
2712         $attoversion = $DB->get_record('config_plugins',
2713                                        $params,
2714                                        'value',
2715                                        IGNORE_MISSING);
2717         if ($attoversion) {
2718             $attoversion = floatval($attoversion->value);
2719         }
2720         // Only these versions that were part of 2.6 beta should be removed.
2721         // Manually installed versions of 2.5 - or later releases for 2.6 installed
2722         // via the plugins DB should not be uninstalled.
2723         if ($attoversion && $attoversion > 2013051500.00 && $attoversion < 2013102201.00) {
2724             // Remove all other associated config.
2725             unset_all_config_for_plugin('editor_atto');
2726             unset_all_config_for_plugin('atto_bold');
2727             unset_all_config_for_plugin('atto_clear');
2728             unset_all_config_for_plugin('atto_html');
2729             unset_all_config_for_plugin('atto_image');
2730             unset_all_config_for_plugin('atto_indent');
2731             unset_all_config_for_plugin('atto_italic');
2732             unset_all_config_for_plugin('atto_link');
2733             unset_all_config_for_plugin('atto_media');
2734             unset_all_config_for_plugin('atto_orderedlist');
2735             unset_all_config_for_plugin('atto_outdent');
2736             unset_all_config_for_plugin('atto_strike');
2737             unset_all_config_for_plugin('atto_title');
2738             unset_all_config_for_plugin('atto_underline');
2739             unset_all_config_for_plugin('atto_unlink');
2740             unset_all_config_for_plugin('atto_unorderedlist');
2742         }
2744         // Main savepoint reached.
2745         upgrade_main_savepoint(true, 2013102201.00);
2746     }
2748     if ($oldversion < 2013102500.01) {
2749         // Find all fileareas that have missing root folder entry and add the root folder entry.
2750         if (empty($CFG->filesrootrecordsfixed)) {
2751             $sql = "SELECT distinct f1.contextid, f1.component, f1.filearea, f1.itemid
2752                 FROM {files} f1 left JOIN {files} f2
2753                     ON f1.contextid = f2.contextid
2754                     AND f1.component = f2.component
2755                     AND f1.filearea = f2.filearea
2756                     AND f1.itemid = f2.itemid
2757                     AND f2.filename = '.'
2758                     AND f2.filepath = '/'
2759                 WHERE (f1.component <> 'user' or f1.filearea <> 'draft')
2760                 and f2.id is null";
2761             $rs = $DB->get_recordset_sql($sql);
2762             $defaults = array('filepath' => '/',
2763                             'filename' => '.',
2764                             'userid' => $USER->id,
2765                             'filesize' => 0,
2766                             'timecreated' => time(),
2767                             'timemodified' => time(),
2768                             'contenthash' => sha1(''));
2769             foreach ($rs as $r) {
2770                 $pathhash = sha1("/$r->contextid/$r->component/$r->filearea/$r->itemid".'/.');
2771                 $DB->insert_record('files', (array)$r + $defaults +
2772                         array('pathnamehash' => $pathhash));
2773             }
2774             $rs->close();
2775             // To skip running the same script on the upgrade to the next major release.
2776             set_config('filesrootrecordsfixed', 1);
2777         }
2779         // Main savepoint reached.
2780         upgrade_main_savepoint(true, 2013102500.01);
2781     }
2783     if ($oldversion < 2013110500.01) {
2784         // MDL-38228. Corrected course_modules upgrade script instead of 2013021801.01.
2786         // This upgrade script fixes the mismatches between DB fields course_modules.section
2787         // and course_sections.sequence. It makes sure that each module is included
2788         // in the sequence of at least one section.
2789         // There is also a separate script for admins: admin/cli/fix_course_sortorder.php
2791         // This script in included in each major version upgrade process so make sure we don't run it twice.
2792         if (empty($CFG->movingmoduleupgradescriptwasrun)) {
2793             upgrade_course_modules_sequences();
2795             // To skip running the same script on the upgrade to the next major release.
2796             set_config('movingmoduleupgradescriptwasrun', 1);
2797         }
2799         // Main savepoint reached.
2800         upgrade_main_savepoint(true, 2013110500.01);
2801     }
2803     if ($oldversion < 2013110600.01) {
2805         if (!file_exists($CFG->dirroot . '/theme/mymobile')) {
2806             // Replace the mymobile settings.
2807             $DB->set_field('course', 'theme', 'clean', array('theme' => 'mymobile'));
2808             $DB->set_field('course_categories', 'theme', 'clean', array('theme' => 'mymobile'));
2809             $DB->set_field('user', 'theme', 'clean', array('theme' => 'mymobile'));
2810             $DB->set_field('mnet_host', 'theme', 'clean', array('theme' => 'mymobile'));
2812             // Replace the theme configs.
2813             if (get_config('core', 'theme') === 'mymobile') {
2814                 set_config('theme', 'clean');
2815             }
2816             if (get_config('core', 'thememobile') === 'mymobile') {
2817                 set_config('thememobile', 'clean');
2818             }
2819             if (get_config('core', 'themelegacy') === 'mymobile') {
2820                 set_config('themelegacy', 'clean');
2821             }
2822             if (get_config('core', 'themetablet') === 'mymobile') {
2823                 set_config('themetablet', 'clean');
2824             }
2826             // Hacky emulation of plugin uninstallation.
2827             unset_all_config_for_plugin('theme_mymobile');
2828         }
2830         // Main savepoint reached.
2831         upgrade_main_savepoint(true, 2013110600.01);
2832     }
2834     if ($oldversion < 2013110600.02) {
2836         // If the user is logged in, we ensure that the alternate name fields are present
2837         // in the session. It will not be the case when upgrading from 2.5 downwards.
2838         if (!empty($USER->id)) {
2839             $refreshuser = $DB->get_record('user', array('id' => $USER->id));
2840             $fields = array('firstnamephonetic', 'lastnamephonetic', 'middlename', 'alternatename', 'firstname', 'lastname');
2841             foreach ($fields as $field) {
2842                 $USER->{$field} = $refreshuser->{$field};
2843             }
2844         }
2846         // Main savepoint reached.
2847         upgrade_main_savepoint(true, 2013110600.02);
2848     }
2850     // Moodle v2.6.0 release upgrade line.
2851     // Put any upgrade step following this.
2852     if ($oldversion < 2013111800.01) {
2854         // Delete notes of deleted courses.
2855         $sql = "DELETE FROM {post}
2856                  WHERE NOT EXISTS (SELECT {course}.id FROM {course}
2857                                     WHERE {course}.id = {post}.courseid)
2858                        AND {post}.module = ?";
2859         $DB->execute($sql, array('notes'));
2861         // Main savepoint reached.
2862         upgrade_main_savepoint(true, 2013111800.01);
2863     }
2865     if ($oldversion < 2013122400.01) {
2866         // Purge stored passwords from config_log table, ideally this should be in each plugin
2867         // but that would complicate backporting...
2868         $items = array(
2869             'core/cronremotepassword', 'core/proxypassword', 'core/smtppass', 'core/jabberpassword',
2870             'enrol_database/dbpass', 'enrol_ldap/bind_pw', 'url/secretphrase');
2871         foreach ($items as $item) {
2872             list($plugin, $name) = explode('/', $item);
2873             $sqlcomparevalue =  $DB->sql_compare_text('value');
2874             $sqlcompareoldvalue = $DB->sql_compare_text('oldvalue');
2875             if ($plugin === 'core') {
2876                 $sql = "UPDATE {config_log}
2877                            SET value = :value
2878                          WHERE name = :name AND plugin IS NULL AND $sqlcomparevalue <> :empty";
2879                 $params = array('value' => '********', 'name' => $name, 'empty' => '');
2880                 $DB->execute($sql, $params);
2882                 $sql = "UPDATE {config_log}
2883                            SET oldvalue = :value
2884                          WHERE name = :name AND plugin IS NULL AND $sqlcompareoldvalue <> :empty";
2885                 $params = array('value' => '********', 'name' => $name, 'empty' => '');
2886                 $DB->execute($sql, $params);
2888             } else {
2889                 $sql = "UPDATE {config_log}
2890                            SET value = :value
2891                          WHERE name = :name AND plugin = :plugin AND $sqlcomparevalue <> :empty";
2892                 $params = array('value' => '********', 'name' => $name, 'plugin' => $plugin, 'empty' => '');
2893                 $DB->execute($sql, $params);
2895                 $sql = "UPDATE {config_log}
2896                            SET oldvalue = :value
2897                          WHERE name = :name AND plugin = :plugin AND  $sqlcompareoldvalue <> :empty";
2898                 $params = array('value' => '********', 'name' => $name, 'plugin' => $plugin, 'empty' => '');
2899                 $DB->execute($sql, $params);
2900             }
2901         }
2902         // Main savepoint reached.
2903         upgrade_main_savepoint(true, 2013122400.01);
2904     }
2906     if ($oldversion < 2014011000.01) {
2908         // Define table cache_text to be dropped.
2909         $table = new xmldb_table('cache_text');
2911         // Conditionally launch drop table for cache_text.
2912         if ($dbman->table_exists($table)) {
2913             $dbman->drop_table($table);
2914         }
2916         unset_config('cachetext');
2918         // Main savepoint reached.
2919         upgrade_main_savepoint(true, 2014011000.01);
2920     }
2922     if ($oldversion < 2014011701.00) {
2923         // Fix gradebook sortorder duplicates.
2924         upgrade_grade_item_fix_sortorder();
2926         // Main savepoint reached.
2927         upgrade_main_savepoint(true, 2014011701.00);
2928     }
2930     if ($oldversion < 2014012300.01) {
2932         // Define field variant to be added to question_statistics.
2933         $table = new xmldb_table('question_statistics');
2934         $field = new xmldb_field('variant', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'subquestion');
2936         // Conditionally launch add field variant.
2937         if (!$dbman->field_exists($table, $field)) {
2938             $dbman->add_field($table, $field);
2939         }
2941         // Main savepoint reached.
2942         upgrade_main_savepoint(true, 2014012300.01);
2943     }
2945     return true;