c467d2d3edff62a35ade9da383446f728644b7e8
[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 < 2012030100.02) {
231         // migrate all numbers to signed - it should be safe to interrupt this and continue later
232         upgrade_mysql_fix_unsigned_columns();
234         // Main savepoint reached
235         upgrade_main_savepoint(true, 2012030100.02);
236     }
238     if ($oldversion < 2012030900.01) {
239         // migrate all texts and binaries to big size - it should be safe to interrupt this and continue later
240         upgrade_mysql_fix_lob_columns();
242         // Main savepoint reached
243         upgrade_main_savepoint(true, 2012030900.01);
244     }
246     if ($oldversion < 2012031500.01) {
247         // Upgrade old course_allowed_modules data to be permission overrides.
248         if ($CFG->restrictmodulesfor === 'all') {
249             $courses = $DB->get_records_menu('course', array(), 'id', 'id, 1');
250         } else if ($CFG->restrictmodulesfor === 'requested') {
251             $courses = $DB->get_records_menu('course', array('restrictmodules' => 1), 'id', 'id, 1');
252         } else {
253             $courses = array();
254         }
256         if (!$dbman->table_exists('course_allowed_modules')) {
257             // Upgrade must already have been run on this server. This might happen,
258             // for example, during development of these changes.
259             $courses = array();
260         }
262         $modidtoname = $DB->get_records_menu('modules', array(), 'id', 'id, name');
264         $coursecount = count($courses);
265         if ($coursecount) {
266             $pbar = new progress_bar('allowedmods', 500, true);
267             $transaction = $DB->start_delegated_transaction();
268         }
270         $i = 0;
271         foreach ($courses as $courseid => $notused) {
272             $i += 1;
273             upgrade_set_timeout(60); // 1 minute per course should be fine.
275             $allowedmoduleids = $DB->get_records_menu('course_allowed_modules',
276             array('course' => $courseid), 'module', 'module, 1');
277             if (empty($allowedmoduleids)) {
278                 // This seems to be the best match for backwards compatibility,
279                 // not necessarily with the old code in course_allowed_module function,
280                 // but with the code that used to be in the coures settings form.
281                 $allowedmoduleids = explode(',', $CFG->defaultallowedmodules);
282                 $allowedmoduleids = array_combine($allowedmoduleids, $allowedmoduleids);
283             }
285             $context = context_course::instance($courseid);
287             list($roleids) = get_roles_with_cap_in_context($context, 'moodle/course:manageactivities');
288             list($managerroleids) = get_roles_with_cap_in_context($context, 'moodle/site:config');
289             foreach ($managerroleids as $roleid) {
290                 unset($roleids[$roleid]);
291             }
293             foreach ($modidtoname as $modid => $modname) {
294                 if (isset($allowedmoduleids[$modid])) {
295                     // Module is allowed, no worries.
296                     continue;
297                 }
299                 $capability = 'mod/' . $modname . ':addinstance';
300                 foreach ($roleids as $roleid) {
301                     assign_capability($capability, CAP_PREVENT, $roleid, $context);
302                 }
303             }
305             $pbar->update($i, $coursecount, "Upgrading legacy course_allowed_modules data - $i/$coursecount.");
306         }
308         if ($coursecount) {
309             $transaction->allow_commit();
310         }
312         upgrade_main_savepoint(true, 2012031500.01);
313     }
315     if ($oldversion < 2012031500.02) {
317         // Define field restrictmodules to be dropped from course
318         $table = new xmldb_table('course');
319         $field = new xmldb_field('restrictmodules');
321         // Conditionally launch drop field requested
322         if ($dbman->field_exists($table, $field)) {
323             $dbman->drop_field($table, $field);
324         }
326         upgrade_main_savepoint(true, 2012031500.02);
327     }
329     if ($oldversion < 2012031500.03) {
331         // Define table course_allowed_modules to be dropped
332         $table = new xmldb_table('course_allowed_modules');
334         // Conditionally launch drop table for course_allowed_modules
335         if ($dbman->table_exists($table)) {
336             $dbman->drop_table($table);
337         }
339         upgrade_main_savepoint(true, 2012031500.03);
340     }
342     if ($oldversion < 2012031500.04) {
343         // Clean up the old admin settings.
344         unset_config('restrictmodulesfor');
345         unset_config('restrictbydefault');
346         unset_config('defaultallowedmodules');
348         upgrade_main_savepoint(true, 2012031500.04);
349     }
351     if ($oldversion < 2012032300.02) {
352         // Migrate the old admin debug setting.
353         if ($CFG->debug == 38911) {
354             set_config('debug', DEBUG_DEVELOPER);
355         } else if ($CFG->debug == 6143) {
356             set_config('debug', DEBUG_ALL);
357         }
358         upgrade_main_savepoint(true, 2012032300.02);
359     }
361     if ($oldversion < 2012042300.00) {
362         // This change makes the course_section index unique.
364         // xmldb does not allow changing index uniqueness - instead we must drop
365         // index then add it again
366         $table = new xmldb_table('course_sections');
367         $index = new xmldb_index('course_section', XMLDB_INDEX_NOTUNIQUE, array('course', 'section'));
369         // Conditionally launch drop index course_section
370         if ($dbman->index_exists($table, $index)) {
371             $dbman->drop_index($table, $index);
372         }
374         // Look for any duplicate course_sections entries. There should not be
375         // any but on some busy systems we found a few, maybe due to previous
376         // bugs.
377         $transaction = $DB->start_delegated_transaction();
378         $rs = $DB->get_recordset_sql('
379                 SELECT DISTINCT
380                     cs.id, cs.course
381                 FROM
382                     {course_sections} cs
383                     INNER JOIN {course_sections} older
384                         ON cs.course = older.course AND cs.section = older.section
385                         AND older.id < cs.id');
386         foreach ($rs as $rec) {
387             $DB->delete_records('course_sections', array('id' => $rec->id));
388             // We can't use rebuild_course_cache() here because introducing sectioncache later
389             // so reset modinfo manually.
390             $DB->set_field('course', 'modinfo', null, array('id' => $rec->course));
391         }
392         $rs->close();
393         $transaction->allow_commit();
395         // Define index course_section (unique) to be added to course_sections
396         $index = new xmldb_index('course_section', XMLDB_INDEX_UNIQUE, array('course', 'section'));
398         // Conditionally launch add index course_section
399         if (!$dbman->index_exists($table, $index)) {
400             $dbman->add_index($table, $index);
401         }
403         // Main savepoint reached
404         upgrade_main_savepoint(true, 2012042300.00);
405     }
407     if ($oldversion < 2012042300.02) {
408         require_once($CFG->dirroot.'/completion/criteria/completion_criteria.php');
409         // Delete orphaned criteria which were left when modules were removed
410         if ($DB->get_dbfamily() === 'mysql') {
411             $sql = "DELETE cc FROM {course_completion_criteria} cc
412                     LEFT JOIN {course_modules} cm ON cm.id = cc.moduleinstance
413                     WHERE cm.id IS NULL AND cc.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
414         } else {
415             $sql = "DELETE FROM {course_completion_criteria}
416                     WHERE NOT EXISTS (
417                         SELECT 'x' FROM {course_modules}
418                         WHERE {course_modules}.id = {course_completion_criteria}.moduleinstance)
419                     AND {course_completion_criteria}.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
420         }
421         $DB->execute($sql);
423         // Main savepoint reached
424         upgrade_main_savepoint(true, 2012042300.02);
425     }
427     if ($oldversion < 2012050300.01) {
428         // Make sure deleted users do not have picture flag.
429         $DB->set_field('user', 'picture', 0, array('deleted'=>1, 'picture'=>1));
430         upgrade_main_savepoint(true, 2012050300.01);
431     }
433     if ($oldversion < 2012050300.02) {
435         // Changing precision of field picture on table user to (10)
436         $table = new xmldb_table('user');
437         $field = new xmldb_field('picture', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'secret');
439         // Launch change of precision for field picture
440         $dbman->change_field_precision($table, $field);
442         // Main savepoint reached
443         upgrade_main_savepoint(true, 2012050300.02);
444     }
446     if ($oldversion < 2012050300.03) {
448         // Define field coursedisplay to be added to course
449         $table = new xmldb_table('course');
450         $field = new xmldb_field('coursedisplay', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'completionnotify');
452         // Conditionally launch add field coursedisplay
453         if (!$dbman->field_exists($table, $field)) {
454             $dbman->add_field($table, $field);
455         }
457         // Main savepoint reached
458         upgrade_main_savepoint(true, 2012050300.03);
459     }
461     if ($oldversion < 2012050300.04) {
463         // Define table course_display to be dropped
464         $table = new xmldb_table('course_display');
466         // Conditionally launch drop table for course_display
467         if ($dbman->table_exists($table)) {
468             $dbman->drop_table($table);
469         }
471         // Main savepoint reached
472         upgrade_main_savepoint(true, 2012050300.04);
473     }
475     if ($oldversion < 2012050300.05) {
477         // Clean up removed admin setting.
478         unset_config('navlinkcoursesections');
480         upgrade_main_savepoint(true, 2012050300.05);
481     }
483     if ($oldversion < 2012050400.01) {
485         // Define index sortorder (not unique) to be added to course
486         $table = new xmldb_table('course');
487         $index = new xmldb_index('sortorder', XMLDB_INDEX_NOTUNIQUE, array('sortorder'));
489         // Conditionally launch add index sortorder
490         if (!$dbman->index_exists($table, $index)) {
491             $dbman->add_index($table, $index);
492         }
494         // Main savepoint reached
495         upgrade_main_savepoint(true, 2012050400.01);
496     }
498     if ($oldversion < 2012050400.02) {
500         // Clean up removed admin setting.
501         unset_config('enablecourseajax');
503         upgrade_main_savepoint(true, 2012050400.02);
504     }
506     if ($oldversion < 2012051100.01) {
508         // Define field idnumber to be added to groups
509         $table = new xmldb_table('groups');
510         $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'courseid');
511         $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
513         // Conditionally launch add field idnumber
514         if (!$dbman->field_exists($table, $field)) {
515             $dbman->add_field($table, $field);
516         }
518         // Conditionally launch add index idnumber
519         if (!$dbman->index_exists($table, $index)) {
520             $dbman->add_index($table, $index);
521         }
523         // Define field idnumber to be added to groupings
524         $table = new xmldb_table('groupings');
525         $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'name');
526         $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
528         // Conditionally launch add field idnumber
529         if (!$dbman->field_exists($table, $field)) {
530             $dbman->add_field($table, $field);
531         }
533         // Conditionally launch add index idnumber
534         if (!$dbman->index_exists($table, $index)) {
535             $dbman->add_index($table, $index);
536         }
538         // Main savepoint reached
539         upgrade_main_savepoint(true, 2012051100.01);
540     }
542     if ($oldversion < 2012051100.03) {
544         // Amend course table to add sectioncache cache
545         $table = new xmldb_table('course');
546         $field = new xmldb_field('sectioncache', XMLDB_TYPE_TEXT, null, null, null, null, null, 'showgrades');
547         if (!$dbman->field_exists($table, $field)) {
548             $dbman->add_field($table, $field);
549         }
551         // Amend course_sections to add date, time and groupingid availability
552         // conditions and a setting about whether to show them
553         $table = new xmldb_table('course_sections');
554         $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'visible');
555         if (!$dbman->field_exists($table, $field)) {
556             $dbman->add_field($table, $field);
557         }
558         $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'availablefrom');
559         if (!$dbman->field_exists($table, $field)) {
560             $dbman->add_field($table, $field);
561         }
562         $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'availableuntil');
563         // Conditionally launch add field showavailability
564         if (!$dbman->field_exists($table, $field)) {
565             $dbman->add_field($table, $field);
566         }
567         $field = new xmldb_field('groupingid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'showavailability');
568         // Conditionally launch add field groupingid
569         if (!$dbman->field_exists($table, $field)) {
570             $dbman->add_field($table, $field);
571         }
573         // Add course_sections_availability to add completion & grade availability conditions
574         $table = new xmldb_table('course_sections_availability');
576         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
577         $table->add_field('coursesectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
578         $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
579         $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', null, null, null, null);
580         $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
581         $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
582         $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
584         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
585         $table->add_key('coursesectionid', XMLDB_KEY_FOREIGN, array('coursesectionid'), 'course_sections', array('id'));
586         $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
587         $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
589         if (!$dbman->table_exists($table)) {
590             $dbman->create_table($table);
591         }
593         // Main savepoint reached
594         upgrade_main_savepoint(true, 2012051100.03);
595     }
597     if ($oldversion < 2012052100.00) {
599         // Define field referencefileid to be added to files.
600         $table = new xmldb_table('files');
602         // Define field referencefileid to be added to files.
603         $field = new xmldb_field('referencefileid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'sortorder');
605         // Conditionally launch add field referencefileid.
606         if (!$dbman->field_exists($table, $field)) {
607             $dbman->add_field($table, $field);
608         }
610         // Define field referencelastsync to be added to files.
611         $field = new xmldb_field('referencelastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencefileid');
613         // Conditionally launch add field referencelastsync.
614         if (!$dbman->field_exists($table, $field)) {
615             $dbman->add_field($table, $field);
616         }
618         // Define field referencelifetime to be added to files.
619         $field = new xmldb_field('referencelifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencelastsync');
621         // Conditionally launch add field referencelifetime.
622         if (!$dbman->field_exists($table, $field)) {
623             $dbman->add_field($table, $field);
624         }
626         $key = new xmldb_key('referencefileid', XMLDB_KEY_FOREIGN, array('referencefileid'), 'files_reference', array('id'));
627         // Launch add key referencefileid
628         $dbman->add_key($table, $key);
630         // Define table files_reference to be created.
631         $table = new xmldb_table('files_reference');
633         // Adding fields to table files_reference.
634         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
635         $table->add_field('repositoryid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
636         $table->add_field('lastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
637         $table->add_field('lifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
638         $table->add_field('reference', XMLDB_TYPE_TEXT, null, null, null, null, null);
640         // Adding keys to table files_reference.
641         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
642         $table->add_key('repositoryid', XMLDB_KEY_FOREIGN, array('repositoryid'), 'repository_instances', array('id'));
644         // Conditionally launch create table for files_reference
645         if (!$dbman->table_exists($table)) {
646             $dbman->create_table($table);
647         }
649         // Main savepoint reached
650         upgrade_main_savepoint(true, 2012052100.00);
651     }
653     if ($oldversion < 2012052500.03) { // fix invalid course_completion_records MDL-27368
654         //first get all instances of duplicate records
655         $sql = 'SELECT userid, course FROM {course_completions} WHERE (deleted IS NULL OR deleted <> 1) GROUP BY userid, course HAVING (count(id) > 1)';
656         $duplicates = $DB->get_recordset_sql($sql, array());
658         foreach ($duplicates as $duplicate) {
659             $pointer = 0;
660             //now get all the records for this user/course
661             $sql = 'userid = ? AND course = ? AND (deleted IS NULL OR deleted <> 1)';
662             $completions = $DB->get_records_select('course_completions', $sql,
663                 array($duplicate->userid, $duplicate->course), 'timecompleted DESC, timestarted DESC');
664             $needsupdate = false;
665             $origcompletion = null;
666             foreach ($completions as $completion) {
667                 $pointer++;
668                 if ($pointer === 1) { //keep 1st record but delete all others.
669                     $origcompletion = $completion;
670                 } else {
671                     //we need to keep the "oldest" of all these fields as the valid completion record.
672                     $fieldstocheck = array('timecompleted', 'timestarted', 'timeenrolled');
673                     foreach ($fieldstocheck as $f) {
674                         if ($origcompletion->$f > $completion->$f) {
675                             $origcompletion->$f = $completion->$f;
676                             $needsupdate = true;
677                         }
678                     }
679                     $DB->delete_records('course_completions', array('id'=>$completion->id));
680                 }
681             }
682             if ($needsupdate) {
683                 $DB->update_record('course_completions', $origcompletion);
684             }
685         }
687         // Main savepoint reached
688         upgrade_main_savepoint(true, 2012052500.03);
689     }
691     if ($oldversion < 2012052900.00) {
692         // Clean up all duplicate records in the course_completions table in preparation
693         // for adding a new index there.
694         upgrade_course_completion_remove_duplicates(
695             'course_completions',
696             array('userid', 'course'),
697             array('timecompleted', 'timestarted', 'timeenrolled')
698         );
700         // Main savepoint reached
701         upgrade_main_savepoint(true, 2012052900.00);
702     }
704     if ($oldversion < 2012052900.01) {
705         // Add indexes to prevent new duplicates in the course_completions table.
706         // Define index useridcourse (unique) to be added to course_completions
707         $table = new xmldb_table('course_completions');
708         $index = new xmldb_index('useridcourse', XMLDB_INDEX_UNIQUE, array('userid', 'course'));
710         // Conditionally launch add index useridcourse
711         if (!$dbman->index_exists($table, $index)) {
712             $dbman->add_index($table, $index);
713         }
715         // Main savepoint reached
716         upgrade_main_savepoint(true, 2012052900.01);
717     }
719     if ($oldversion < 2012052900.02) {
720         // Clean up all duplicate records in the course_completion_crit_compl table in preparation
721         // for adding a new index there.
722         upgrade_course_completion_remove_duplicates(
723             'course_completion_crit_compl',
724             array('userid', 'course', 'criteriaid'),
725             array('timecompleted')
726         );
728         // Main savepoint reached
729         upgrade_main_savepoint(true, 2012052900.02);
730     }
732     if ($oldversion < 2012052900.03) {
733         // Add indexes to prevent new duplicates in the course_completion_crit_compl table.
734         // Define index useridcoursecriteraid (unique) to be added to course_completion_crit_compl
735         $table = new xmldb_table('course_completion_crit_compl');
736         $index = new xmldb_index('useridcoursecriteraid', XMLDB_INDEX_UNIQUE, array('userid', 'course', 'criteriaid'));
738         // Conditionally launch add index useridcoursecriteraid
739         if (!$dbman->index_exists($table, $index)) {
740             $dbman->add_index($table, $index);
741         }
743         // Main savepoint reached
744         upgrade_main_savepoint(true, 2012052900.03);
745     }
747     if ($oldversion < 2012052900.04) {
748         // Clean up all duplicate records in the course_completion_aggr_methd table in preparation
749         // for adding a new index there.
750         upgrade_course_completion_remove_duplicates(
751             'course_completion_aggr_methd',
752             array('course', 'criteriatype')
753         );
755         // Main savepoint reached
756         upgrade_main_savepoint(true, 2012052900.04);
757     }
759     if ($oldversion < 2012052900.05) {
760         // Add indexes to prevent new duplicates in the course_completion_aggr_methd table.
761         // Define index coursecriteratype (unique) to be added to course_completion_aggr_methd
762         $table = new xmldb_table('course_completion_aggr_methd');
763         $index = new xmldb_index('coursecriteriatype', XMLDB_INDEX_UNIQUE, array('course', 'criteriatype'));
765         // Conditionally launch add index coursecriteratype
766         if (!$dbman->index_exists($table, $index)) {
767             $dbman->add_index($table, $index);
768         }
770         // Main savepoint reached
771         upgrade_main_savepoint(true, 2012052900.05);
772     }
774     if ($oldversion < 2012060600.01) {
775         // Add field referencehash to files_reference
776         $table = new xmldb_table('files_reference');
777         $field = new xmldb_field('referencehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'reference');
778         if (!$dbman->field_exists($table, $field)) {
779             $dbman->add_field($table, $field);
780         }
781         upgrade_main_savepoint(true, 2012060600.01);
782     }
784     if ($oldversion < 2012060600.02) {
785         // Populate referencehash field with SHA1 hash of the reference - this shoudl affect only 2.3dev sites
786         // that were using the feature for testing. Production sites have the table empty.
787         $rs = $DB->get_recordset('files_reference', null, '', 'id, reference');
788         foreach ($rs as $record) {
789             $hash = sha1($record->reference);
790             $DB->set_field('files_reference', 'referencehash', $hash, array('id' => $record->id));
791         }
792         $rs->close();
794         upgrade_main_savepoint(true, 2012060600.02);
795     }
797     if ($oldversion < 2012060600.03) {
798         // Merge duplicate records in files_reference that were created during the development
799         // phase at 2.3dev sites. This is needed so we can create the unique index over
800         // (repositoryid, referencehash) fields.
801         $sql = "SELECT repositoryid, referencehash, MIN(id) AS minid
802                   FROM {files_reference}
803               GROUP BY repositoryid, referencehash
804                 HAVING COUNT(*) > 1";
805         $duprs = $DB->get_recordset_sql($sql);
806         foreach ($duprs as $duprec) {
807             // get the list of all ids in {files_reference} that need to be remapped
808             $dupids = $DB->get_records_select('files_reference', "repositoryid = ? AND referencehash = ? AND id > ?",
809                 array($duprec->repositoryid, $duprec->referencehash, $duprec->minid), '', 'id');
810             $dupids = array_keys($dupids);
811             // relink records in {files} that are now referring to a duplicate record
812             // in {files_reference} to refer to the first one
813             list($subsql, $subparams) = $DB->get_in_or_equal($dupids);
814             $DB->set_field_select('files', 'referencefileid', $duprec->minid, "referencefileid $subsql", $subparams);
815             // and finally remove all orphaned records from {files_reference}
816             $DB->delete_records_list('files_reference', 'id', $dupids);
817         }
818         $duprs->close();
820         upgrade_main_savepoint(true, 2012060600.03);
821     }
823     if ($oldversion < 2012060600.04) {
824         // Add a unique index over repositoryid and referencehash fields in files_reference table
825         $table = new xmldb_table('files_reference');
826         $index = new xmldb_index('uq_external_file', XMLDB_INDEX_UNIQUE, array('repositoryid', 'referencehash'));
828         if (!$dbman->index_exists($table, $index)) {
829             $dbman->add_index($table, $index);
830         }
832         upgrade_main_savepoint(true, 2012060600.04);
833     }
835     if ($oldversion < 2012061800.01) {
837         // Define field screenreader to be dropped from user
838         $table = new xmldb_table('user');
839         $field = new xmldb_field('ajax');
841         // Conditionally launch drop field screenreader
842         if ($dbman->field_exists($table, $field)) {
843             $dbman->drop_field($table, $field);
844         }
846         // Main savepoint reached
847         upgrade_main_savepoint(true, 2012061800.01);
848     }
850     if ($oldversion < 2012062000.00) {
851         // Add field newcontextid to backup_files_template
852         $table = new xmldb_table('backup_files_template');
853         $field = new xmldb_field('newcontextid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'info');
855         if (!$dbman->field_exists($table, $field)) {
856             $dbman->add_field($table, $field);
857         }
859         upgrade_main_savepoint(true, 2012062000.00);
860     }
862     if ($oldversion < 2012062000.01) {
863         // Add field newitemid to backup_files_template
864         $table = new xmldb_table('backup_files_template');
865         $field = new xmldb_field('newitemid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'newcontextid');
867         if (!$dbman->field_exists($table, $field)) {
868             $dbman->add_field($table, $field);
869         }
871         upgrade_main_savepoint(true, 2012062000.01);
872     }
875     // Moodle v2.3.0 release upgrade line
876     // Put any upgrade step following this
879     if ($oldversion < 2012062500.02) {
880         // Drop some old backup tables, not used anymore
882         // Define table backup_files to be dropped
883         $table = new xmldb_table('backup_files');
885         // Conditionally launch drop table for backup_files
886         if ($dbman->table_exists($table)) {
887             $dbman->drop_table($table);
888         }
890         // Define table backup_ids to be dropped
891         $table = new xmldb_table('backup_ids');
893         // Conditionally launch drop table for backup_ids
894         if ($dbman->table_exists($table)) {
895             $dbman->drop_table($table);
896         }
898         // Main savepoint reached
899         upgrade_main_savepoint(true, 2012062500.02);
900     }
902     if ($oldversion < 2012070600.04) {
903         // Define table course_modules_avail_fields to be created
904         $table = new xmldb_table('course_modules_avail_fields');
906         // Adding fields to table course_modules_avail_fields
907         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
908         $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
909         $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
910         $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
911         $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
912         $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
914         // Adding keys to table course_modules_avail_fields
915         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
916         $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
918         // Conditionally launch create table for course_modules_avail_fields
919         if (!$dbman->table_exists($table)) {
920             $dbman->create_table($table);
921         }
923         // Main savepoint reached
924         upgrade_main_savepoint(true, 2012070600.04);
925     }
927     if ($oldversion < 2012070600.05) {
928         // Define table course_sections_avail_fields to be created
929         $table = new xmldb_table('course_sections_avail_fields');
931         // Adding fields to table course_sections_avail_fields
932         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
933         $table->add_field('coursesectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
934         $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
935         $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
936         $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
937         $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
939         // Adding keys to table course_sections_avail_fields
940         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
941         $table->add_key('coursesectionid', XMLDB_KEY_FOREIGN, array('coursesectionid'), 'course_sections', array('id'));
943         // Conditionally launch create table for course_sections_avail_fields
944         if (!$dbman->table_exists($table)) {
945             $dbman->create_table($table);
946         }
948         // Main savepoint reached
949         upgrade_main_savepoint(true, 2012070600.05);
950     }
952     if ($oldversion < 2012070600.06) {
954         // Drop "deleted" fields
955         $table = new xmldb_table('course_completions');
956         $field = new xmldb_field('timenotified');
957         $field = new xmldb_field('deleted');
959         // Conditionally launch drop field deleted from course_completions
960         if ($dbman->field_exists($table, $field)) {
961             $dbman->drop_field($table, $field);
962         }
964         $field = new xmldb_field('timenotified');
965         // Conditionally launch drop field timenotified from course_completions
966         if ($dbman->field_exists($table, $field)) {
967             $dbman->drop_field($table, $field);
968         }
970         // Main savepoint reached
971         upgrade_main_savepoint(true, 2012070600.06);
972     }
974     if ($oldversion < 2012070600.07) {
975         $table = new xmldb_table('course_completion_crit_compl');
976         $field = new xmldb_field('deleted');
978         // Conditionally launch drop field deleted from course_completion_crit_compl
979         if ($dbman->field_exists($table, $field)) {
980             $dbman->drop_field($table, $field);
981         }
982         // Main savepoint reached
983         upgrade_main_savepoint(true, 2012070600.07);
984     }
986     if ($oldversion < 2012070600.08) {
988         // Drop unused table "course_completion_notify"
989         $table = new xmldb_table('course_completion_notify');
991         // Conditionally launch drop table course_completion_notify
992         if ($dbman->table_exists($table)) {
993             $dbman->drop_table($table);
994         }
996         // Main savepoint reached
997         upgrade_main_savepoint(true, 2012070600.08);
998      }
1000     if ($oldversion < 2012070600.09) {
1002         // Define index path (not unique) to be added to context
1003         $table = new xmldb_table('context');
1004         $index = new xmldb_index('path', XMLDB_INDEX_NOTUNIQUE, array('path'), array('varchar_pattern_ops'));
1006         // Recreate index with new pattern hint
1007         if ($DB->get_dbfamily() === 'postgres') {
1008             if ($dbman->index_exists($table, $index)) {
1009                 $dbman->drop_index($table, $index);
1010             }
1011             $dbman->add_index($table, $index);
1012         }
1014         // Main savepoint reached
1015         upgrade_main_savepoint(true, 2012070600.09);
1016     }
1018     if ($oldversion < 2012070600.10) {
1020         // Define index name (unique) to be dropped form role
1021         $table = new xmldb_table('role');
1022         $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
1024         // Conditionally launch drop index name
1025         if ($dbman->index_exists($table, $index)) {
1026             $dbman->drop_index($table, $index);
1027         }
1029         // Main savepoint reached
1030         upgrade_main_savepoint(true, 2012070600.10);
1031     }
1033     if ($oldversion < 2012070600.11) {
1035         // Define index component-itemid-userid (not unique) to be added to role_assignments
1036         $table = new xmldb_table('role_assignments');
1037         $index = new xmldb_index('component-itemid-userid', XMLDB_INDEX_NOTUNIQUE, array('component', 'itemid', 'userid'));
1039         // Conditionally launch add index component-itemid-userid
1040         if (!$dbman->index_exists($table, $index)) {
1041             $dbman->add_index($table, $index);
1042         }
1044         // Main savepoint reached
1045         upgrade_main_savepoint(true, 2012070600.11);
1046     }
1048     if ($oldversion < 2012071900.01) {
1049         // Cleanup after simpeltests tool
1050         capabilities_cleanup('tool_unittest');
1051         unset_all_config_for_plugin('tool_unittest');
1053         upgrade_main_savepoint(true, 2012071900.01);
1054     }
1056     if ($oldversion < 2012072400.00) {
1057         // Remove obsolete xhtml strict setting - use THEME->doctype in theme config if necessary,
1058         // see theme_config->doctype in lib/outputlib.php for more details.
1059         unset_config('xmlstrictheaders');
1060         upgrade_main_savepoint(true, 2012072400.00);
1061     }
1063     if ($oldversion < 2012072401.00) {
1065         // Saves orphaned questions from the Dark Side
1066         upgrade_save_orphaned_questions();
1068         // Main savepoint reached
1069         upgrade_main_savepoint(true, 2012072401.00);
1070     }
1072     if ($oldversion < 2012072600.01) {
1073         // Handle events with empty eventtype //MDL-32827
1075         $DB->set_field('event', 'eventtype', 'site', array('eventtype' => '', 'courseid' => $SITE->id));
1076         $DB->set_field_select('event', 'eventtype', 'due', "eventtype = '' AND courseid != 0 AND groupid = 0 AND (modulename = 'assignment' OR modulename = 'assign')");
1077         $DB->set_field_select('event', 'eventtype', 'course', "eventtype = '' AND courseid != 0 AND groupid = 0");
1078         $DB->set_field_select('event', 'eventtype', 'group', "eventtype = '' AND groupid != 0");
1079         $DB->set_field_select('event', 'eventtype', 'user', "eventtype = '' AND userid != 0");
1081         // Main savepoint reached
1082         upgrade_main_savepoint(true, 2012072600.01);
1083     }
1085     if ($oldversion < 2012080200.02) {
1086         // Drop obsolete question upgrade field that should have been added to the install.xml.
1087         $table = new xmldb_table('question');
1088         $field = new xmldb_field('oldquestiontextformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
1090         if ($dbman->field_exists($table, $field)) {
1091             $dbman->drop_field($table, $field);
1092         }
1094         upgrade_main_savepoint(true, 2012080200.02);
1095     }
1097     if ($oldversion < 2012081400.01) {
1098         // Move the ability to disable blogs to its own setting MDL-25012.
1100         if (isset($CFG->bloglevel)) {
1101             // Only change settings if existing setting was set.
1102             if (empty($CFG->bloglevel)) {
1103                 set_config('enableblogs', 0);
1104                 // Now set the bloglevel to a valid setting as the disabled setting has been removed.
1105                 // This prevents confusing results when users enable the blog system in future.
1106                 set_config('bloglevel', BLOG_USER_LEVEL);
1107             } else {
1108                 set_config('enableblogs', 1);
1109             }
1110         }
1112         // Main savepoint reached
1113         upgrade_main_savepoint(true, 2012081400.01);
1114     }
1116     if ($oldversion < 2012081600.01) {
1117         // Delete removed setting - Google Maps API V2 will not work in 2013.
1118         unset_config('googlemapkey');
1119         upgrade_main_savepoint(true, 2012081600.01);
1120     }
1122     if ($oldversion < 2012082300.01) {
1123         // Add more custom enrol fields.
1124         $table = new xmldb_table('enrol');
1125         $field = new xmldb_field('customint5', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint4');
1127         if (!$dbman->field_exists($table, $field)) {
1128             $dbman->add_field($table, $field);
1129         }
1131         $field = new xmldb_field('customint6', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint5');
1132         if (!$dbman->field_exists($table, $field)) {
1133             $dbman->add_field($table, $field);
1134         }
1136         $field = new xmldb_field('customint7', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint6');
1137         if (!$dbman->field_exists($table, $field)) {
1138             $dbman->add_field($table, $field);
1139         }
1141         $field = new xmldb_field('customint8', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint7');
1142         if (!$dbman->field_exists($table, $field)) {
1143             $dbman->add_field($table, $field);
1144         }
1146         $field = new xmldb_field('customchar3', XMLDB_TYPE_CHAR, '1333', null, null, null, null, 'customchar2');
1147         if (!$dbman->field_exists($table, $field)) {
1148             $dbman->add_field($table, $field);
1149         }
1151         $field = new xmldb_field('customtext3', XMLDB_TYPE_TEXT, null, null, null, null, null, 'customtext2');
1152         if (!$dbman->field_exists($table, $field)) {
1153             $dbman->add_field($table, $field);
1154         }
1156         $field = new xmldb_field('customtext4', XMLDB_TYPE_TEXT, null, null, null, null, null, 'customtext3');
1157         if (!$dbman->field_exists($table, $field)) {
1158             $dbman->add_field($table, $field);
1159         }
1161         // Main savepoint reached.
1162         upgrade_main_savepoint(true, 2012082300.01);
1163     }
1165     if ($oldversion < 2012082300.02) {
1166         // Define field component to be added to groups_members
1167         $table = new xmldb_table('groups_members');
1168         $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'timeadded');
1170         // Conditionally launch add field component
1171         if (!$dbman->field_exists($table, $field)) {
1172             $dbman->add_field($table, $field);
1173         }
1175         // Define field itemid to be added to groups_members
1176         $field = new xmldb_field('itemid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'component');
1178         // Conditionally launch add field itemid
1179         if (!$dbman->field_exists($table, $field)) {
1180             $dbman->add_field($table, $field);
1181         }
1183         // Main savepoint reached
1184         upgrade_main_savepoint(true, 2012082300.02);
1185     }
1187     if ($oldversion < 2012090500.00) {
1188         $subquery = 'SELECT b.id FROM {blog_external} b where b.id = ' . $DB->sql_cast_char2int('{post}.content', true);
1189         $sql = 'DELETE FROM {post}
1190                       WHERE {post}.module = \'blog_external\'
1191                             AND NOT EXISTS (' . $subquery . ')
1192                             AND ' . $DB->sql_isnotempty('post', 'uniquehash', false, false);
1193         $DB->execute($sql);
1194         upgrade_main_savepoint(true, 2012090500.00);
1195     }
1197     if ($oldversion < 2012090700.01) {
1198         // Add a category field in the course_request table
1199         $table = new xmldb_table('course_request');
1200         $field = new xmldb_field('category', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, 'summaryformat');
1201         if (!$dbman->field_exists($table, $field)) {
1202             $dbman->add_field($table, $field);
1203         }
1205         // Main savepoint reached.
1206         upgrade_main_savepoint(true, 2012090700.01);
1207     }
1209     if ($oldversion < 2012091700.00) {
1211         // Dropping screenreader field from user.
1212         $table = new xmldb_table('user');
1213         $field = new xmldb_field('screenreader');
1215         if ($dbman->field_exists($table, $field)) {
1216             $dbman->drop_field($table, $field);
1217         }
1219         // Main savepoint reached.
1220         upgrade_main_savepoint(true, 2012091700.00);
1221     }
1223     if ($oldversion < 2012092100.01) {
1224         // Some folders still have a sortorder set, which is used for main files but is not
1225         // supported by the folder resource. We reset the value here.
1226         $sql = 'UPDATE {files} SET sortorder = ? WHERE component = ? AND filearea = ? AND sortorder <> ?';
1227         $DB->execute($sql, array(0, 'mod_folder', 'content', 0));
1229         // Main savepoint reached.
1230         upgrade_main_savepoint(true, 2012092100.01);
1231     }
1233     if ($oldversion < 2012092600.00) {
1234         // Define index idname (unique) to be added to tag
1235         $table = new xmldb_table('tag');
1236         $index = new xmldb_index('idname', XMLDB_INDEX_UNIQUE, array('id', 'name'));
1238         // Conditionally launch add index idname
1239         if (!$dbman->index_exists($table, $index)) {
1240             $dbman->add_index($table, $index);
1241         }
1243         // Main savepoint reached
1244         upgrade_main_savepoint(true, 2012092600.00);
1245     }
1247     if ($oldversion < 2012101500.01) {
1248         // Find all orphaned blog associations that might exist.
1249         $sql = "SELECT ba.id
1250                   FROM {blog_association} ba
1251              LEFT JOIN {post} p
1252                     ON p.id = ba.blogid
1253                  WHERE p.id IS NULL";
1254         $orphanedrecordids = $DB->get_records_sql($sql);
1255         // Now delete these associations.
1256         foreach ($orphanedrecordids as $orphanedrecord) {
1257             $DB->delete_records('blog_association', array('id' => $orphanedrecord->id));
1258         }
1260         upgrade_main_savepoint(true, 2012101500.01);
1261     }
1263     if ($oldversion < 2012101800.02) {
1264         // Renaming backups using previous file naming convention.
1265         upgrade_rename_old_backup_files_using_shortname();
1267         // Main savepoint reached.
1268         upgrade_main_savepoint(true, 2012101800.02);
1269     }
1271     if ($oldversion < 2012103001.00) {
1272         // create new event_subscriptions table
1273         $table = new xmldb_table('event_subscriptions');
1274         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1275         $table->add_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1276         $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1277         $table->add_field('groupid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1278         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1279         $table->add_field('pollinterval', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1280         $table->add_field('lastupdated', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
1281         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1282         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1283         if (!$dbman->table_exists($table)) {
1284             $dbman->create_table($table);
1285         }
1286         // Main savepoint reached
1287         upgrade_main_savepoint(true, 2012103001.00);
1288     }
1290     if ($oldversion < 2012103002.00) {
1291         // Add subscription field to the event table
1292         $table = new xmldb_table('event');
1293         $field = new xmldb_field('subscriptionid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'timemodified');
1295         // Conditionally launch add field subscriptionid
1296         if (!$dbman->field_exists($table, $field)) {
1297             $dbman->add_field($table, $field);
1298         }
1299         upgrade_main_savepoint(true, 2012103002.00);
1300     }
1302     if ($oldversion < 2012103003.00) {
1303         // Fix uuid field in event table to match RFC-2445 UID property
1304         $table = new xmldb_table('event');
1305         $field = new xmldb_field('uuid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'visible');
1306         if ($dbman->field_exists($table, $field)) {
1307             // Changing precision of field uuid on table event to (255)
1308             $dbman->change_field_precision($table, $field);
1309         }
1310         // Main savepoint reached
1311         upgrade_main_savepoint(true, 2012103003.00);
1312     }
1314     if ($oldversion < 2012110200.00) {
1316         // Define table course_format_options to be created
1317         $table = new xmldb_table('course_format_options');
1319         // Adding fields to table course_format_options
1320         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1321         $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1322         $table->add_field('format', XMLDB_TYPE_CHAR, '21', null, XMLDB_NOTNULL, null, null);
1323         $table->add_field('sectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'format');
1324         $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1325         $table->add_field('value', XMLDB_TYPE_TEXT, null, null, null, null, null);
1327         // Adding keys to table course_format_options
1328         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1329         $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1331         // Adding indexes to table course_format_options
1332         $table->add_index('formatoption', XMLDB_INDEX_UNIQUE, array('courseid', 'format', 'sectionid', 'name'));
1334         // Conditionally launch create table for course_format_options
1335         if (!$dbman->table_exists($table)) {
1336             $dbman->create_table($table);
1337         }
1339         // Changing type of field format on table course to char with length 21
1340         $table = new xmldb_table('course');
1341         $field = new xmldb_field('format', XMLDB_TYPE_CHAR, '21', null, XMLDB_NOTNULL, null, 'topics', 'summaryformat');
1343         // Launch change of type for field format
1344         $dbman->change_field_type($table, $field);
1346         // Main savepoint reached
1347         upgrade_main_savepoint(true, 2012110200.00);
1348     }
1350     return true;