lib-db-upgrade MDL-24490 user description now big text
[moodle.git] / lib / db / upgrade.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * This file keeps track of upgrades to Moodle.
20  *
21  * Sometimes, changes between versions involve
22  * alterations to database structures and other
23  * major things that may break installations.
24  *
25  * The upgrade function in this file will attempt
26  * to perform all the necessary actions to upgrade
27  * your older installation to the current version.
28  *
29  * If there's something it cannot do itself, it
30  * will tell you what you need to do.
31  *
32  * The commands in here will all be database-neutral,
33  * using the methods of database_manager class
34  *
35  * Please do not forget to use upgrade_set_timeout()
36  * before any action that may take longer time to finish.
37  *
38  * @package    core
39  * @subpackage admin
40  * @copyright  2006 onwards Martin Dougiamas  http://dougiamas.com
41  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42  */
44 defined('MOODLE_INTERNAL') || die();
46 /**
47  *
48  * @global stdClass $CFG
49  * @global stdClass $USER
50  * @global moodle_database $DB
51  * @global core_renderer $OUTPUT
52  * @param int $oldversion
53  * @return bool always true
54  */
55 function xmldb_main_upgrade($oldversion) {
56     global $CFG, $USER, $DB, $OUTPUT;
58     require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
60     $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
62     ////////////////////////////////////////
63     ///upgrade supported only from 1.9.x ///
64     ////////////////////////////////////////
66     if ($oldversion < 2008030600) {
67         //NOTE: this table was added much later later in dev cycle, but we need it here, upgrades from pre PR1 not supported
69     /// Define table upgrade_log to be created
70         $table = new xmldb_table('upgrade_log');
72     /// Adding fields to table upgrade_log
73         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
74         $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
75         $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
76         $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
77         $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
78         $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
79         $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
80         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
81         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
83     /// Adding keys to table upgrade_log
84         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
85         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
87     /// Adding indexes to table upgrade_log
88         $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
89         $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
91     /// Create table for upgrade_log
92         $dbman->create_table($table);
94     /// Main savepoint reached
95         upgrade_main_savepoint(true, 2008030600);
96     }
98     if ($oldversion < 2008030601) {
99         //NOTE: this table was added much later later in dev cycle, but we need it here, upgrades from pre PR1 not supported
101     /// Define table log_queries to be created
102         $table = new xmldb_table('log_queries');
104     /// Adding fields to table log_queries
105         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
106         $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
107         $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
108         $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
109         $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
110         $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
111         $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
112         $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
113         $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
115     /// Adding keys to table log_queries
116         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
118     /// Conditionally launch create table for log_queries
119         $dbman->create_table($table);
121     /// Main savepoint reached
122         upgrade_main_savepoint(true, 2008030601);
123     }
125     if ($oldversion < 2008030602) {
126         @unlink($CFG->dataroot.'/cache/languages');
128         if (file_exists("$CFG->dataroot/lang")) {
129             // rename old lang directory so that the new and old langs do not mix
130             if (rename("$CFG->dataroot/lang", "$CFG->dataroot/oldlang")) {
131                 $oldlang = "$CFG->dataroot/oldlang";
132             } else {
133                 $oldlang = "$CFG->dataroot/lang";
134             }
135         } else {
136             $oldlang = '';
137         }
138         // TODO: fetch previously installed languages ("*_utf8") found in $oldlang from moodle.org
139         upgrade_set_timeout(60*20); // this may take a while
142         // TODO: add some info file to $oldlang describing what to do with "$oldlang/*_utf8_local" dirs
145         // Main savepoint reached
146         upgrade_main_savepoint(true, 2008030602);
147     }
149     if ($oldversion < 2008030700) {
150         upgrade_set_timeout(60*20); // this may take a while
152     /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
153         $table = new xmldb_table('grade_letters');
154         $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
156     /// Launch drop index contextid-lowerboundary
157         if ($dbman->index_exists($table, $index)) {
158             $dbman->drop_index($table, $index);
159         }
161     /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
162         $table = new xmldb_table('grade_letters');
163         $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
165     /// Launch add index contextid-lowerboundary-letter
166         $dbman->add_index($table, $index);
168     /// Main savepoint reached
169         upgrade_main_savepoint(true, 2008030700);
170     }
172     if ($oldversion < 2008050100) {
173         // Update courses that used weekscss to weeks
174         $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
175         upgrade_main_savepoint(true, 2008050100);
176     }
178     if ($oldversion < 2008050200) {
179         // remove unused config options
180         unset_config('statsrolesupgraded');
181         upgrade_main_savepoint(true, 2008050200);
182     }
184     if ($oldversion < 2008050700) {
185         upgrade_set_timeout(60*20); // this may take a while
187     /// Fix minor problem caused by MDL-5482.
188         require_once($CFG->dirroot . '/question/upgrade.php');
189         question_fix_random_question_parents();
190         upgrade_main_savepoint(true, 2008050700);
191     }
193     if ($oldversion < 2008051201) {
194         echo $OUTPUT->notification('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
195         upgrade_set_timeout(60*20); // this may take a while
197     /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
198         $dbfamily = $DB->get_dbfamily();
199         if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
200             $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
201         }
203     /// Define index idnumber (not unique) to be dropped form user
204         $table = new xmldb_table('user');
205         $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
207     /// Launch drop index idnumber
208         if ($dbman->index_exists($table, $index)) {
209             $dbman->drop_index($table, $index);
210         }
212     /// Changing precision of field idnumber on table user to (255)
213         $table = new xmldb_table('user');
214         $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
216     /// Launch change of precision for field idnumber
217         $dbman->change_field_precision($table, $field);
219     /// Launch add index idnumber again
220         $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
221         $dbman->add_index($table, $index);
223     /// Main savepoint reached
224         upgrade_main_savepoint(true, 2008051201);
225     }
227     if ($oldversion < 2008051203) {
228         $table = new xmldb_table('mnet_enrol_course');
229         $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
230         $dbman->change_field_precision($table, $field);
231         upgrade_main_savepoint(true, 2008051203);
232     }
234     if ($oldversion < 2008063001) {
235         upgrade_set_timeout(60*20); // this may take a while
237         // table to be modified
238         $table = new xmldb_table('tag_instance');
239         // add field
240         $field = new xmldb_field('tiuserid');
241         if (!$dbman->field_exists($table, $field)) {
242             $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
243             $dbman->add_field($table, $field);
244         }
245         // modify index
246         $index = new xmldb_index('itemtype-itemid-tagid');
247         $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
248         if ($dbman->index_exists($table, $index)) {
249             $dbman->drop_index($table, $index);
250         }
251         $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
252         $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
253         if (!$dbman->index_exists($table, $index)) {
254             $dbman->add_index($table, $index);
255         }
257         /// Main savepoint reached
258         upgrade_main_savepoint(true, 2008063001);
259     }
261     if ($oldversion < 2008070300) {
262         $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
263         upgrade_main_savepoint(true, 2008070300);
264     }
266     if ($oldversion < 2008070701) {
268     /// Define table portfolio_instance to be created
269         $table = new xmldb_table('portfolio_instance');
271     /// Adding fields to table portfolio_instance
272         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
273         $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
274         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
275         $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
277     /// Adding keys to table portfolio_instance
278         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
280     /// Conditionally launch create table for portfolio_instance
281         if (!$dbman->table_exists($table)) {
282             $dbman->create_table($table);
283         }
284   /// Define table portfolio_instance_config to be created
285         $table = new xmldb_table('portfolio_instance_config');
287     /// Adding fields to table portfolio_instance_config
288         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
289         $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
290         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
291         $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
293     /// Adding keys to table portfolio_instance_config
294         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
295         $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
297     /// Adding indexes to table portfolio_instance_config
298         $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
300     /// Conditionally launch create table for portfolio_instance_config
301         if (!$dbman->table_exists($table)) {
302             $dbman->create_table($table);
303         }
305    /// Define table portfolio_instance_user to be created
306         $table = new xmldb_table('portfolio_instance_user');
308     /// Adding fields to table portfolio_instance_user
309         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
310         $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
311         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
312         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
313         $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
315     /// Adding keys to table portfolio_instance_user
316         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
317         $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
318         $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
320     /// Conditionally launch create table for portfolio_instance_user
321         if (!$dbman->table_exists($table)) {
322             $dbman->create_table($table);
323         }
325     /// Main savepoint reached
326         upgrade_main_savepoint(true, 2008070701);
327     }
329     if ($oldversion < 2008072400) {
330     /// Create the database tables for message_processors
331         $table = new xmldb_table('message_processors');
332         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
333         $table->add_field('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null);
334         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
335         $dbman->create_table($table);
337     /// delete old and create new fields
338         $table = new xmldb_table('message');
339         $field = new xmldb_field('messagetype');
340         $dbman->drop_field($table, $field);
342     /// fields to rename
343         $field = new xmldb_field('message');
344         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
345         $dbman->rename_field($table, $field, 'fullmessage');
346         $field = new xmldb_field('format');
347         $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
348         $dbman->rename_field($table, $field, 'fullmessageformat');
350     /// new message fields
351         $field = new xmldb_field('subject');
352         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
353         $dbman->add_field($table, $field);
354         $field = new xmldb_field('fullmessagehtml');
355         $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
356         $dbman->add_field($table, $field);
357         $field = new xmldb_field('smallmessage');
358         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
359         $dbman->add_field($table, $field);
362         $table = new xmldb_table('message_read');
363         $field = new xmldb_field('messagetype');
364         $dbman->drop_field($table, $field);
365         $field = new xmldb_field('mailed');
366         $dbman->drop_field($table, $field);
368     /// fields to rename
369         $field = new xmldb_field('message');
370         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
371         $dbman->rename_field($table, $field, 'fullmessage');
372         $field = new xmldb_field('format');
373         $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
374         $dbman->rename_field($table, $field, 'fullmessageformat');
377     /// new message fields
378         $field = new xmldb_field('subject');
379         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
380         $dbman->add_field($table, $field);
381         $field = new xmldb_field('fullmessagehtml');
382         $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
383         $dbman->add_field($table, $field);
384         $field = new xmldb_field('smallmessage');
385         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
386         $dbman->add_field($table, $field);
388     /// new table
389         $table = new xmldb_table('message_working');
390         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
391         $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
392         $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
393         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
394         $dbman->create_table($table);
397         upgrade_main_savepoint(true, 2008072400);
398     }
400     if ($oldversion < 2008072800) {
402     /// Define field enablecompletion to be added to course
403         $table = new xmldb_table('course');
404         $field = new xmldb_field('enablecompletion');
405         $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
407     /// Launch add field enablecompletion
408         if (!$dbman->field_exists($table,$field)) {
409             $dbman->add_field($table, $field);
410         }
412     /// Define field completion to be added to course_modules
413         $table = new xmldb_table('course_modules');
414         $field = new xmldb_field('completion');
415         $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'groupmembersonly');
417     /// Launch add field completion
418         if (!$dbman->field_exists($table,$field)) {
419             $dbman->add_field($table, $field);
420         }
422     /// Define field completiongradeitemnumber to be added to course_modules
423         $field = new xmldb_field('completiongradeitemnumber');
424         $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'completion');
426     /// Launch add field completiongradeitemnumber
427         if (!$dbman->field_exists($table,$field)) {
428             $dbman->add_field($table, $field);
429         }
431     /// Define field completionview to be added to course_modules
432         $field = new xmldb_field('completionview');
433         $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiongradeitemnumber');
435     /// Launch add field completionview
436         if (!$dbman->field_exists($table,$field)) {
437             $dbman->add_field($table, $field);
438         }
440     /// Define field completionexpected to be added to course_modules
441         $field = new xmldb_field('completionexpected');
442         $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionview');
444     /// Launch add field completionexpected
445         if (!$dbman->field_exists($table,$field)) {
446             $dbman->add_field($table, $field);
447         }
449    /// Define table course_modules_completion to be created
450         $table = new xmldb_table('course_modules_completion');
451         if (!$dbman->table_exists($table)) {
453         /// Adding fields to table course_modules_completion
454             $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
455             $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
456             $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
457             $table->add_field('completionstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
458             $table->add_field('viewed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
459             $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
461         /// Adding keys to table course_modules_completion
462             $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
464         /// Adding indexes to table course_modules_completion
465             $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
466             $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
468         /// Launch create table for course_modules_completion
469             $dbman->create_table($table);
470         }
472         /// Main savepoint reached
473         upgrade_main_savepoint(true, 2008072800);
474     }
476     if ($oldversion < 2008073000) {
478     /// Define table portfolio_log to be created
479         $table = new xmldb_table('portfolio_log');
481     /// Adding fields to table portfolio_log
482         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
483         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
484         $table->add_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
485         $table->add_field('portfolio', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
486         $table->add_field('caller_class', XMLDB_TYPE_CHAR, '150', null, XMLDB_NOTNULL, null, null);
487         $table->add_field('caller_file', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
488         $table->add_field('caller_sha1', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
489         $table->add_field('tempdataid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
490         $table->add_field('returnurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
491         $table->add_field('continueurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
493     /// Adding keys to table portfolio_log
494         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
495         $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
496         $table->add_key('portfoliofk', XMLDB_KEY_FOREIGN, array('portfolio'), 'portfolio_instance', array('id'));
498     /// Conditionally launch create table for portfolio_log
499         if (!$dbman->table_exists($table)) {
500             $dbman->create_table($table);
501         }
503     /// Main savepoint reached
504         upgrade_main_savepoint(true, 2008073000);
505     }
507     if ($oldversion < 2008073104) {
508     /// Drop old table that might exist for some people
509         $table = new xmldb_table('message_providers');
510         if ($dbman->table_exists($table)) {
511             $dbman->drop_table($table);
512         }
514     /// Define table message_providers to be created
515         $table = new xmldb_table('message_providers');
517     /// Adding fields to table message_providers
518         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
519         $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
520         $table->add_field('component', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
521         $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
523     /// Adding keys to table message_providers
524         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
526     /// Adding indexes to table message_providers
527         $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
529     /// Create table for message_providers
530         $dbman->create_table($table);
532         upgrade_main_savepoint(true, 2008073104);
533     }
535     if ($oldversion < 2008073111) {
536     /// Define table files to be created
537         $table = new xmldb_table('files');
539     /// Adding fields to table files
540         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
541         $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
542         $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
543         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
544         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
545         $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
546         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
547         $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
548         $table->add_field('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
549         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
550         $table->add_field('filesize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
551         $table->add_field('mimetype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
552         $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
553         $table->add_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
554         $table->add_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null);
555         $table->add_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null);
556         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
557         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
559     /// Adding keys to table files
560         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
561         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
562         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
564     /// Adding indexes to table files
565         $table->add_index('component-filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('component', 'filearea', 'contextid', 'itemid'));
566         $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
567         $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
569     /// Conditionally launch create table for files
570         $dbman->create_table($table);
572     /// Main savepoint reached
573         upgrade_main_savepoint(true, 2008073111);
574     }
576     if ($oldversion < 2008073112) {
577         // Define field legacyfiles to be added to course
578         $table = new xmldb_table('course');
579         $field = new xmldb_field('legacyfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'maxbytes');
581         // Launch add field legacyfiles
582         $dbman->add_field($table, $field);
583         // enable legacy files in all courses
584         $DB->execute("UPDATE {course} SET legacyfiles = 2");
586         // Main savepoint reached
587         upgrade_main_savepoint(true, 2008073112);
588     }
590     if ($oldversion < 2008073113) {
591     /// move all course, backup and other files to new filepool based storage
592         upgrade_migrate_files_courses();
593     /// Main savepoint reached
594         upgrade_main_savepoint(true, 2008073113);
595     }
597     if ($oldversion < 2008073114) {
598     /// move all course, backup and other files to new filepool based storage
599         upgrade_migrate_files_blog();
600     /// Main savepoint reached
601         upgrade_main_savepoint(true, 2008073114);
602     }
604     if ($oldversion < 2008080400) {
605         // Add field ssl_jump_url to mnet application, and populate existing default applications
606         $table = new xmldb_table('mnet_application');
607         $field = new xmldb_field('sso_jump_url');
608         if (!$dbman->field_exists($table, $field)) {
609             $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
610             $dbman->add_field($table, $field);
611             $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
612             $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
613         }
615         /// Main savepoint reached
616         upgrade_main_savepoint(true, 2008080400);
617     }
619     if ($oldversion < 2008080500) {
621     /// Define table portfolio_tempdata to be created
622         $table = new xmldb_table('portfolio_tempdata');
624     /// Adding fields to table portfolio_tempdata
625         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
626         $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
627         $table->add_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
628         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
629         $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
631     /// Adding keys to table portfolio_tempdata
632         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
633         $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
634         $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
636     /// Conditionally launch create table for portfolio_tempdata
637         if (!$dbman->table_exists($table)) {
638             $dbman->create_table($table);
639         }
641     /// Main savepoint reached
642         upgrade_main_savepoint(true, 2008080500);
643     }
645     if ($oldversion < 2008081500) {
646     /// Changing the type of all the columns that the question bank uses to store grades to be NUMBER(12, 7).
647         $table = new xmldb_table('question');
648         $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'generalfeedback');
649         $dbman->change_field_type($table, $field);
650         upgrade_main_savepoint(true, 2008081500);
651     }
653     if ($oldversion < 2008081501) {
654         $table = new xmldb_table('question');
655         $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'defaultgrade');
656         $dbman->change_field_type($table, $field);
657         upgrade_main_savepoint(true, 2008081501);
658     }
660     if ($oldversion < 2008081502) {
661         $table = new xmldb_table('question_answers');
662         $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'answer');
663         $dbman->change_field_type($table, $field);
664         upgrade_main_savepoint(true, 2008081502);
665     }
667     if ($oldversion < 2008081503) {
668         $table = new xmldb_table('question_sessions');
669         $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'newgraded');
670         $dbman->change_field_type($table, $field);
671         upgrade_main_savepoint(true, 2008081503);
672     }
674     if ($oldversion < 2008081504) {
675         $table = new xmldb_table('question_states');
676         $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'event');
677         $dbman->change_field_type($table, $field);
678         upgrade_main_savepoint(true, 2008081504);
679     }
681     if ($oldversion < 2008081505) {
682         $table = new xmldb_table('question_states');
683         $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'grade');
684         $dbman->change_field_type($table, $field);
685         upgrade_main_savepoint(true, 2008081505);
686     }
688     if ($oldversion < 2008081506) {
689         $table = new xmldb_table('question_states');
690         $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'raw_grade');
691         $dbman->change_field_type($table, $field);
692         upgrade_main_savepoint(true, 2008081506);
693     }
695     if ($oldversion < 2008081600) {
697     /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
698         unset_config('unicodedb');
700     /// Main savepoint reached
701         upgrade_main_savepoint(true, 2008081600);
702     }
704     if ($oldversion < 2008082602) {
706     /// Define table repository to be dropped
707         $table = new xmldb_table('repository');
709     /// Conditionally launch drop table for repository
710         if ($dbman->table_exists($table)) {
711             $dbman->drop_table($table);
712         }
714     /// Define table repository to be created
715         $table = new xmldb_table('repository');
717     /// Adding fields to table repository
718         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
719         $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
720         $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1');
721         $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
723     /// Adding keys to table repository
724         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
726     /// Conditionally launch create table for repository
727         if (!$dbman->table_exists($table)) {
728             $dbman->create_table($table);
729         }
731     /// Define table repository_instances to be created
732         $table = new xmldb_table('repository_instances');
734     /// Adding fields to table repository_instances
735         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
736         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
737         $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
738         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
739         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
740         $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null);
741         $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
742         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
743         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
744         $table->add_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
746     /// Adding keys to table repository_instances
747         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
749     /// Conditionally launch create table for repository_instances
750         if (!$dbman->table_exists($table)) {
751             $dbman->create_table($table);
752         }
754     /// Define table repository_instance_config to be created
755         $table = new xmldb_table('repository_instance_config');
757     /// Adding fields to table repository_instance_config
758         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
759         $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
760         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
761         $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
763     /// Adding keys to table repository_instance_config
764         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
766     /// Conditionally launch create table for repository_instance_config
767         if (!$dbman->table_exists($table)) {
768             $dbman->create_table($table);
769         }
771     /// Main savepoint reached
772         upgrade_main_savepoint(true, 2008082602);
773     }
775     if ($oldversion < 2008082700) {
776     /// Add a new column to the question sessions table to record whether a
777     /// question has been flagged.
779     /// Define field flagged to be added to question_sessions
780         $table = new xmldb_table('question_sessions');
781         $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
783     /// Conditionally launch add field flagged
784         if (!$dbman->field_exists($table, $field)) {
785             $dbman->add_field($table, $field);
786         }
788     /// Main savepoint reached
789         upgrade_main_savepoint(true, 2008082700);
790     }
792     if ($oldversion < 2008082900) {
794     /// Changing precision of field parent_type on table mnet_rpc to (20)
795         $table = new xmldb_table('mnet_rpc');
796         $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
798     /// Launch change of precision for field parent_type
799         $dbman->change_field_precision($table, $field);
801     /// Main savepoint reached
802         upgrade_main_savepoint(true, 2008082900);
803     }
805     // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
806     if ($oldversion < 2008091000) {
807         foreach (get_object_vars($CFG) as $name => $value) {
808             if (substr($name, strlen($name) - 8) !== '_version') {
809                 continue;
810             }
811             $pluginname = substr($name, 0, strlen($name) - 8);
812             if (!strpos($pluginname, '_')) {
813                 // Skip things like backup_version that don't contain an extra _
814                 continue;
815             }
816             if ($pluginname == 'enrol_ldap_version') {
817                 // Special case - this is something different from a plugin version number.
818                 continue;
819             }
820             if (!preg_match('/^\d{10}$/', $value)) {
821                 // Extra safety check, skip anything that does not look like a Moodle
822                 // version number (10 digits).
823                 continue;
824             }
825             set_config('version', $value, $pluginname);
826             unset_config($name);
827         }
828         upgrade_main_savepoint(true, 2008091000);
829     }
831     if ($oldversion < 2008092300) {
832         unset_config('editorspelling');
833         unset_config('editordictionary');
834     /// Main savepoint reached
835         upgrade_main_savepoint(true, 2008092300);
836     }
838     if ($oldversion < 2008101300) {
840         if (!get_config(NULL, 'statsruntimedays')) {
841             set_config('statsruntimedays', '31');
842         }
844     /// Main savepoint reached
845         upgrade_main_savepoint(true, 2008101300);
846     }
848     /// Drop the deprecated teacher, teachers, student and students columns from the course table.
849     if ($oldversion < 2008111200) {
850         $table = new xmldb_table('course');
852     /// Conditionally launch drop field teacher
853         $field = new xmldb_field('teacher');
854         if ($dbman->field_exists($table, $field)) {
855             $dbman->drop_field($table, $field);
856         }
858     /// Conditionally launch drop field teacher
859         $field = new xmldb_field('teachers');
860         if ($dbman->field_exists($table, $field)) {
861             $dbman->drop_field($table, $field);
862         }
864     /// Conditionally launch drop field teacher
865         $field = new xmldb_field('student');
866         if ($dbman->field_exists($table, $field)) {
867             $dbman->drop_field($table, $field);
868         }
870     /// Conditionally launch drop field teacher
871         $field = new xmldb_field('students');
872         if ($dbman->field_exists($table, $field)) {
873             $dbman->drop_field($table, $field);
874         }
876     /// Main savepoint reached
877         upgrade_main_savepoint(true, 2008111200);
878     }
880 /// Add a unique index to the role.name column.
881     if ($oldversion < 2008111800) {
883     /// Define index name (unique) to be added to role
884         $table = new xmldb_table('role');
885         $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
887     /// Conditionally launch add index name
888         if (!$dbman->index_exists($table, $index)) {
889             $dbman->add_index($table, $index);
890         }
892     /// Main savepoint reached
893         upgrade_main_savepoint(true, 2008111800);
894     }
896 /// Add a unique index to the role.shortname column.
897     if ($oldversion < 2008111801) {
899     /// Define index shortname (unique) to be added to role
900         $table = new xmldb_table('role');
901         $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
903     /// Conditionally launch add index shortname
904         if (!$dbman->index_exists($table, $index)) {
905             $dbman->add_index($table, $index);
906         }
908     /// Main savepoint reached
909         upgrade_main_savepoint(true, 2008111801);
910     }
912     if ($oldversion < 2008120700) {
914     /// Changing precision of field shortname on table course_request to (100)
915         $table = new xmldb_table('course_request');
916         $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
918     /// Before changing the field, drop dependent indexes
919     /// Define index shortname (not unique) to be dropped form course_request
920         $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
921     /// Conditionally launch drop index shortname
922         if ($dbman->index_exists($table, $index)) {
923             $dbman->drop_index($table, $index);
924         }
926     /// Launch change of precision for field shortname
927         $dbman->change_field_precision($table, $field);
929     /// After changing the field, recreate dependent indexes
930     /// Define index shortname (not unique) to be added to course_request
931         $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
932     /// Conditionally launch add index shortname
933         if (!$dbman->index_exists($table, $index)) {
934             $dbman->add_index($table, $index);
935         }
937     /// Main savepoint reached
938         upgrade_main_savepoint(true, 2008120700);
939     }
941     if ($oldversion < 2008120801) {
943     /// Changing precision of field shortname on table mnet_enrol_course to (100)
944         $table = new xmldb_table('mnet_enrol_course');
945         $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
947     /// Launch change of precision for field shortname
948         $dbman->change_field_precision($table, $field);
950     /// Main savepoint reached
951         upgrade_main_savepoint(true, 2008120801);
952     }
954     if ($oldversion < 2008121701) {
956     /// Define field availablefrom to be added to course_modules
957         $table = new xmldb_table('course_modules');
958         $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
960     /// Conditionally launch add field availablefrom
961         if (!$dbman->field_exists($table, $field)) {
962             $dbman->add_field($table, $field);
963         }
965     /// Define field availableuntil to be added to course_modules
966         $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
968     /// Conditionally launch add field availableuntil
969         if (!$dbman->field_exists($table, $field)) {
970             $dbman->add_field($table, $field);
971         }
973     /// Define field showavailability to be added to course_modules
974         $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
976     /// Conditionally launch add field showavailability
977         if (!$dbman->field_exists($table, $field)) {
978             $dbman->add_field($table, $field);
979         }
981     /// Define table course_modules_availability to be created
982         $table = new xmldb_table('course_modules_availability');
984     /// Adding fields to table course_modules_availability
985         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
986         $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
987         $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
988         $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
989         $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
990         $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
991         $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
993     /// Adding keys to table course_modules_availability
994         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
995         $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
996         $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
997         $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
999     /// Conditionally launch create table for course_modules_availability
1000         if (!$dbman->table_exists($table)) {
1001             $dbman->create_table($table);
1002         }
1004     /// Changes to modinfo mean we need to rebuild course cache
1005         require_once($CFG->dirroot . '/course/lib.php');
1006         rebuild_course_cache(0, true);
1008     /// Main savepoint reached
1009         upgrade_main_savepoint(true, 2008121701);
1010     }
1012     if ($oldversion < 2009010500) {
1013     /// clean up config table a bit
1014         unset_config('session_error_counter');
1016     /// Main savepoint reached
1017         upgrade_main_savepoint(true, 2009010500);
1018     }
1020     if ($oldversion < 2009010600) {
1022     /// Define field originalquestion to be dropped from question_states
1023         $table = new xmldb_table('question_states');
1024         $field = new xmldb_field('originalquestion');
1026     /// Conditionally launch drop field originalquestion
1027         if ($dbman->field_exists($table, $field)) {
1028             $dbman->drop_field($table, $field);
1029         }
1031     /// Main savepoint reached
1032         upgrade_main_savepoint(true, 2009010600);
1033     }
1035     if ($oldversion < 2009010601) {
1037     /// Changing precision of field ip on table log to (45)
1038         $table = new xmldb_table('log');
1039         $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1041     /// Launch change of precision for field ip
1042         $dbman->change_field_precision($table, $field);
1044     /// Main savepoint reached
1045         upgrade_main_savepoint(true, 2009010601);
1046     }
1048     if ($oldversion < 2009010602) {
1050     /// Changing precision of field lastip on table user to (45)
1051         $table = new xmldb_table('user');
1052         $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
1054     /// Launch change of precision for field lastip
1055         $dbman->change_field_precision($table, $field);
1057     /// Main savepoint reached
1058         upgrade_main_savepoint(true, 2009010602);
1059     }
1061     if ($oldversion < 2009010603) {
1063     /// Changing precision of field ip_address on table mnet_host to (45)
1064         $table = new xmldb_table('mnet_host');
1065         $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
1067     /// Launch change of precision for field ip_address
1068         $dbman->change_field_precision($table, $field);
1070     /// Main savepoint reached
1071         upgrade_main_savepoint(true, 2009010603);
1072     }
1074     if ($oldversion < 2009010604) {
1076     /// Changing precision of field ip on table mnet_log to (45)
1077         $table = new xmldb_table('mnet_log');
1078         $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1080     /// Launch change of precision for field ip
1081         $dbman->change_field_precision($table, $field);
1083     /// Main savepoint reached
1084         upgrade_main_savepoint(true, 2009010604);
1085     }
1087     if ($oldversion < 2009011000) {
1089     /// Changing nullability of field configdata on table block_instance to null
1090         $table = new xmldb_table('block_instance');
1091         $field = new xmldb_field('configdata');
1092         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
1094     /// Launch change of nullability for field configdata
1095         $dbman->change_field_notnull($table, $field);
1097     /// Main savepoint reached
1098         upgrade_main_savepoint(true, 2009011000);
1099     }
1101     if ($oldversion < 2009011100) {
1102     /// Remove unused settings
1103         unset_config('zip');
1104         unset_config('unzip');
1105         unset_config('adminblocks_initialised');
1107     /// Main savepoint reached
1108         upgrade_main_savepoint(true, 2009011100);
1109     }
1111     if ($oldversion < 2009011101) {
1112     /// Migrate backup settings to core plugin config table
1113         $configs = $DB->get_records('backup_config');
1114         foreach ($configs as $config) {
1115             set_config($config->name, $config->value, 'backup');
1116         }
1118     /// Define table to be dropped
1119         $table = new xmldb_table('backup_config');
1121     /// Launch drop table for old backup config
1122         $dbman->drop_table($table);
1124     /// Main savepoint reached
1125         upgrade_main_savepoint(true, 2009011101);
1126     }
1128     if ($oldversion < 2009011303) {
1130     /// Define table config_log to be created
1131         $table = new xmldb_table('config_log');
1133     /// Adding fields to table config_log
1134         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1135         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1136         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1137         $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1138         $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1139         $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1140         $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1142     /// Adding keys to table config_log
1143         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1144         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1146     /// Adding indexes to table config_log
1147         $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1149     /// Launch create table for config_log
1150         $dbman->create_table($table);
1152     /// Main savepoint reached
1153         upgrade_main_savepoint(true, 2009011303);
1154     }
1156     if ($oldversion < 2009011900) {
1158     /// Define table sessions2 to be dropped
1159         $table = new xmldb_table('sessions2');
1161     /// Conditionally launch drop table for sessions
1162         if ($dbman->table_exists($table)) {
1163             $dbman->drop_table($table);
1164         }
1166     /// Define table sessions to be dropped
1167         $table = new xmldb_table('sessions');
1169     /// Conditionally launch drop table for sessions
1170         if ($dbman->table_exists($table)) {
1171             $dbman->drop_table($table);
1172         }
1174     /// Define table sessions to be created
1175         $table = new xmldb_table('sessions');
1177     /// Adding fields to table sessions
1178         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1179         $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1180         $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
1181         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1182         $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
1183         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1184         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1185         $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1186         $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1188     /// Adding keys to table sessions
1189         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1190         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1192     /// Adding indexes to table sessions
1193         $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1194         $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1195         $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1196         $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1198     /// Launch create table for sessions
1199         $dbman->create_table($table);
1201     /// Main savepoint reached
1202         upgrade_main_savepoint(true, 2009011900);
1203     }
1205     if ($oldversion < 2009021800) {
1206         // Converting format of grade conditions, if any exist, to percentages.
1207         $DB->execute("
1208 UPDATE {course_modules_availability} SET grademin=(
1209     SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
1210         /(gi.grademax-gi.grademin)
1211     FROM {grade_items} gi
1212     WHERE gi.id={course_modules_availability}.gradeitemid)
1213 WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1214         $DB->execute("
1215 UPDATE {course_modules_availability} SET grademax=(
1216     SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
1217         /(gi.grademax-gi.grademin)
1218     FROM {grade_items} gi
1219     WHERE gi.id={course_modules_availability}.gradeitemid)
1220 WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1222     /// Main savepoint reached
1223         upgrade_main_savepoint(true, 2009021800);
1224     }
1226     if ($oldversion < 2009021801) {
1227     /// Define field backuptype to be added to backup_log
1228         $table = new xmldb_table('backup_log');
1229         $field = new xmldb_field('backuptype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'info');
1230     /// Conditionally Launch add field backuptype and set all old records as 'scheduledbackup' records.
1231         if (!$dbman->field_exists($table, $field)) {
1232             $dbman->add_field($table, $field);
1233             $DB->execute("UPDATE {backup_log} SET backuptype='scheduledbackup'");
1234         }
1236     /// Main savepoint reached
1237         upgrade_main_savepoint(true, 2009021801);
1238     }
1240     /// Add default sort order for question types.
1241     if ($oldversion < 2009030300) {
1242         set_config('multichoice_sortorder', 1, 'question');
1243         set_config('truefalse_sortorder', 2, 'question');
1244         set_config('shortanswer_sortorder', 3, 'question');
1245         set_config('numerical_sortorder', 4, 'question');
1246         set_config('calculated_sortorder', 5, 'question');
1247         set_config('essay_sortorder', 6, 'question');
1248         set_config('match_sortorder', 7, 'question');
1249         set_config('randomsamatch_sortorder', 8, 'question');
1250         set_config('multianswer_sortorder', 9, 'question');
1251         set_config('description_sortorder', 10, 'question');
1252         set_config('random_sortorder', 11, 'question');
1253         set_config('missingtype_sortorder', 12, 'question');
1255         upgrade_main_savepoint(true, 2009030300);
1256     }
1258     /// MDL-18132 replace the use a new Role allow switch settings page, instead of
1259     /// $CFG->allowuserswitchrolestheycantassign
1260     if ($oldversion < 2009032000) {
1261     /// First create the new table.
1262             $table = new xmldb_table('role_allow_switch');
1264     /// Adding fields to table role_allow_switch
1265         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1266         $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1267         $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1269     /// Adding keys to table role_allow_switch
1270         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1271         $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
1272         $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
1274     /// Adding indexes to table role_allow_switch
1275         $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1277     /// Conditionally launch create table for role_allow_switch
1278         if (!$dbman->table_exists($table)) {
1279             $dbman->create_table($table);
1280         }
1282     /// Main savepoint reached
1283         upgrade_main_savepoint(true, 2009032000);
1284     }
1286     if ($oldversion < 2009032001) {
1287     /// Copy from role_allow_assign into the new table.
1288         $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1289                 SELECT roleid, allowassign FROM {role_allow_assign}');
1291     /// Unset the config variable used in 1.9.
1292         unset_config('allowuserswitchrolestheycantassign');
1294     /// Main savepoint reached
1295         upgrade_main_savepoint(true, 2009032001);
1296     }
1298     if ($oldversion < 2009040300) {
1300     /// Define table filter_active to be created
1301         $table = new xmldb_table('filter_active');
1303     /// Adding fields to table filter_active
1304         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1305         $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1306         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1307         $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1308         $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1310     /// Adding keys to table filter_active
1311         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1312         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1314     /// Adding indexes to table filter_active
1315         $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1317     /// Conditionally launch create table for filter_active
1318         if (!$dbman->table_exists($table)) {
1319             $dbman->create_table($table);
1320         }
1322     /// Main savepoint reached
1323         upgrade_main_savepoint(true, 2009040300);
1324     }
1326     if ($oldversion < 2009040301) {
1328     /// Define table filter_config to be created
1329         $table = new xmldb_table('filter_config');
1331     /// Adding fields to table filter_config
1332         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1333         $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1334         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1335         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1336         $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1338     /// Adding keys to table filter_config
1339         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1340         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1342     /// Adding indexes to table filter_config
1343         $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1345     /// Conditionally launch create table for filter_config
1346         if (!$dbman->table_exists($table)) {
1347             $dbman->create_table($table);
1348         }
1350     /// Main savepoint reached
1351         upgrade_main_savepoint(true, 2009040301);
1352     }
1354     if ($oldversion < 2009040302) {
1355     /// Transfer current settings from $CFG->textfilters
1356         $disabledfilters = filter_get_all_installed();
1357         if (empty($CFG->textfilters)) {
1358             $activefilters = array();
1359         } else {
1360             $activefilters = explode(',', $CFG->textfilters);
1361         }
1362         $syscontext = get_context_instance(CONTEXT_SYSTEM);
1363         $sortorder = 1;
1364         foreach ($activefilters as $filter) {
1365             filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1366             $sortorder += 1;
1367             unset($disabledfilters[$filter]);
1368         }
1369         foreach ($disabledfilters as $filter => $notused) {
1370             filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1371             $sortorder += 1;
1372         }
1374     /// Main savepoint reached
1375         upgrade_main_savepoint(true, 2009040302);
1376     }
1378     if ($oldversion < 2009040600) {
1379     /// Ensure that $CFG->stringfilters is set.
1380         if (empty($CFG->stringfilters)) {
1381             if (!empty($CFG->filterall)) {
1382                 set_config('stringfilters', $CFG->textfilters);
1383             } else {
1384                 set_config('stringfilters', '');
1385             }
1386         }
1388         set_config('filterall', !empty($CFG->stringfilters));
1389         unset_config('textfilters');
1391     /// Main savepoint reached
1392         upgrade_main_savepoint(true, 2009040600);
1393     }
1395     if ($oldversion < 2009041700) {
1396     /// To ensure the UI remains consistent with no behaviour change, any
1397     /// 'until' date in an activity condition should have 1 second subtracted
1398     /// (to go from 0:00 on the following day to 23:59 on the previous one).
1399         $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
1400         require_once($CFG->dirroot . '/course/lib.php');
1401         rebuild_course_cache(0, true);
1403     /// Main savepoint reached
1404         upgrade_main_savepoint(true, 2009041700);
1405     }
1407     if ($oldversion < 2009042600) {
1408     /// Deleting orphaned messages from deleted users.
1409         require_once($CFG->dirroot.'/message/lib.php');
1410     /// Detect deleted users with messages sent(useridfrom) and not read
1411         if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
1412                                                     FROM {user} u
1413                                                     JOIN {message} m ON m.useridfrom = u.id
1414                                                    WHERE u.deleted = ?', array(1))) {
1415             foreach ($deletedusers as $deleteduser) {
1416                 message_move_userfrom_unread2read($deleteduser->id); // move messages
1417             }
1418         }
1419     /// Main savepoint reached
1420         upgrade_main_savepoint(true, 2009042600);
1421     }
1423     /// Dropping all enums/check contraints from core. MDL-18577
1424     if ($oldversion < 2009042700) {
1426     /// Changing list of values (enum) of field stattype on table stats_daily to none
1427         $table = new xmldb_table('stats_daily');
1428         $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1430     /// Launch change of list of values for field stattype
1431         $dbman->drop_enum_from_field($table, $field);
1433     /// Changing list of values (enum) of field stattype on table stats_weekly to none
1434         $table = new xmldb_table('stats_weekly');
1435         $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1437     /// Launch change of list of values for field stattype
1438         $dbman->drop_enum_from_field($table, $field);
1440     /// Changing list of values (enum) of field stattype on table stats_monthly to none
1441         $table = new xmldb_table('stats_monthly');
1442         $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1444     /// Launch change of list of values for field stattype
1445         $dbman->drop_enum_from_field($table, $field);
1447     /// Changing list of values (enum) of field publishstate on table post to none
1448         $table = new xmldb_table('post');
1449         $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
1451     /// Launch change of list of values for field publishstate
1452         $dbman->drop_enum_from_field($table, $field);
1454     /// Main savepoint reached
1455         upgrade_main_savepoint(true, 2009042700);
1456     }
1458     if ($oldversion < 2009043000) {
1459         unset_config('grade_report_showgroups');
1460         upgrade_main_savepoint(true, 2009043000);
1461     }
1463     if ($oldversion < 2009050600) {
1464     /// Site front page blocks need to be moved due to page name change.
1465         $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
1467     /// Main savepoint reached
1468         upgrade_main_savepoint(true, 2009050600);
1469     }
1471     if ($oldversion < 2009050601) {
1473     /// Define table block_instance to be renamed to block_instances
1474         $table = new xmldb_table('block_instance');
1476     /// Launch rename table for block_instance
1477         $dbman->rename_table($table, 'block_instances');
1479     /// Main savepoint reached
1480         upgrade_main_savepoint(true, 2009050601);
1481     }
1483     if ($oldversion < 2009050602) {
1485     /// Define table block_instance to be renamed to block_instance_old
1486         $table = new xmldb_table('block_pinned');
1488     /// Launch rename table for block_instance
1489         $dbman->rename_table($table, 'block_pinned_old');
1491     /// Main savepoint reached
1492         upgrade_main_savepoint(true, 2009050602);
1493     }
1495     if ($oldversion < 2009050603) {
1497     /// Define table block_instance_old to be created
1498         $table = new xmldb_table('block_instance_old');
1500     /// Adding fields to table block_instance_old
1501         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1502         $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1503         $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1504         $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1505         $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
1506         $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
1507         $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
1508         $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1509         $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1511     /// Adding keys to table block_instance_old
1512         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1513         $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
1515     /// Adding indexes to table block_instance_old
1516         $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1517         $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
1519     /// Conditionally launch create table for block_instance_old
1520         if (!$dbman->table_exists($table)) {
1521             $dbman->create_table($table);
1522         }
1524     /// Main savepoint reached
1525         upgrade_main_savepoint(true, 2009050603);
1526     }
1528     if ($oldversion < 2009050604) {
1529     /// Copy current blocks data from block_instances to block_instance_old
1530         $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
1531             SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
1533         upgrade_main_savepoint(true, 2009050604);
1534     }
1536     if ($oldversion < 2009050605) {
1538     /// Define field multiple to be dropped from block
1539         $table = new xmldb_table('block');
1540         $field = new xmldb_field('multiple');
1542     /// Conditionally launch drop field multiple
1543         if ($dbman->field_exists($table, $field)) {
1544             $dbman->drop_field($table, $field);
1545         }
1547     /// Main savepoint reached
1548         upgrade_main_savepoint(true, 2009050605);
1549     }
1551     if ($oldversion < 2009050606) {
1552         $table = new xmldb_table('block_instances');
1554     /// Rename field weight on table block_instances to defaultweight
1555         $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'position');
1556         $dbman->rename_field($table, $field, 'defaultweight');
1558     /// Rename field position on table block_instances to defaultregion
1559         $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
1560         $dbman->rename_field($table, $field, 'defaultregion');
1562         /// Main savepoint reached
1563         upgrade_main_savepoint(true, 2009050606);
1564     }
1566     if ($oldversion < 2009050607) {
1567     /// Changing precision of field defaultregion on table block_instances to (16)
1568         $table = new xmldb_table('block_instances');
1569         $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
1571     /// Launch change of precision for field defaultregion
1572         $dbman->change_field_precision($table, $field);
1574     /// Main savepoint reached
1575         upgrade_main_savepoint(true, 2009050607);
1576     }
1578     if ($oldversion < 2009050608) {
1579     /// Change regions to the new notation
1580         $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1581         $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1582         $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1583         // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1584         // flex page course format. Hopefully this new value is an adequate alternative.
1586     /// Main savepoint reached
1587         upgrade_main_savepoint(true, 2009050608);
1588     }
1590     if ($oldversion < 2009050609) {
1592     /// Define key blockname (unique) to be added to block
1593         $table = new xmldb_table('block');
1594         $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1596     /// Launch add key blockname
1597         $dbman->add_key($table, $key);
1599     /// Main savepoint reached
1600         upgrade_main_savepoint(true, 2009050609);
1601     }
1603     if ($oldversion < 2009050610) {
1604         $table = new xmldb_table('block_instances');
1606     /// Define field blockname to be added to block_instances
1607         $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
1608         if (!$dbman->field_exists($table, $field)) {
1609             $dbman->add_field($table, $field);
1610         }
1612     /// Define field contextid to be added to block_instances
1613         $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
1614         if (!$dbman->field_exists($table, $field)) {
1615             $dbman->add_field($table, $field);
1616         }
1618     /// Define field showinsubcontexts to be added to block_instances
1619         $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
1620         if (!$dbman->field_exists($table, $field)) {
1621             $dbman->add_field($table, $field);
1622         }
1624     /// Define field subpagepattern to be added to block_instances
1625         $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
1626         if (!$dbman->field_exists($table, $field)) {
1627             $dbman->add_field($table, $field);
1628         }
1630     /// Main savepoint reached
1631         upgrade_main_savepoint(true, 2009050610);
1632     }
1634     if ($oldversion < 2009050611) {
1635         $table = new xmldb_table('block_instances');
1637     /// Fill in blockname from blockid
1638         $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1640     /// Set showinsubcontexts = 0 for all rows.
1641         $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1643     /// Main savepoint reached
1644         upgrade_main_savepoint(true, 2009050611);
1645     }
1647     if ($oldversion < 2009050612) {
1649     /// Rename field pagetype on table block_instances to pagetypepattern
1650         $table = new xmldb_table('block_instances');
1651         $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
1653     /// Launch rename field pagetype
1654         $dbman->rename_field($table, $field, 'pagetypepattern');
1656     /// Main savepoint reached
1657         upgrade_main_savepoint(true, 2009050612);
1658     }
1660     if ($oldversion < 2009050613) {
1661     /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1663     /// site-index
1664         $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
1665         $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1666                                                    pagetypepattern = 'site-index',
1667                                                    subpagepattern = NULL
1668                       WHERE pagetypepattern = 'site-index'");
1670     /// course-view
1671         $DB->execute("UPDATE {block_instances} SET
1672                         contextid = (
1673                             SELECT {context}.id
1674                             FROM {context}
1675                             JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1676                             WHERE {course}.id = pageid
1677                         ),
1678                        pagetypepattern = 'course-view-*',
1679                        subpagepattern = NULL
1680                       WHERE pagetypepattern = 'course-view'");
1682     /// admin
1683         $syscontext = get_context_instance(CONTEXT_SYSTEM);
1684         $DB->execute("UPDATE {block_instances} SET
1685                         contextid = " . $syscontext->id . ",
1686                         pagetypepattern = 'admin-*',
1687                         subpagepattern = NULL
1688                       WHERE pagetypepattern = 'admin'");
1690     /// my-index
1691         $DB->execute("UPDATE {block_instances} SET
1692                         contextid = (
1693                             SELECT {context}.id
1694                             FROM {context}
1695                             JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1696                             WHERE {user}.id = pageid
1697                         ),
1698                         pagetypepattern = 'my-index',
1699                         subpagepattern = NULL
1700                       WHERE pagetypepattern = 'my-index'");
1702     /// tag-index
1703         $DB->execute("UPDATE {block_instances} SET
1704                         contextid = " . $syscontext->id . ",
1705                         pagetypepattern = 'tag-index',
1706                         subpagepattern = pageid
1707                       WHERE pagetypepattern = 'tag-index'");
1709     /// blog-view
1710         $DB->execute("UPDATE {block_instances} SET
1711                         contextid = (
1712                             SELECT {context}.id
1713                             FROM {context}
1714                             JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1715                             WHERE {user}.id = pageid
1716                         ),
1717                         pagetypepattern = 'blog-index',
1718                         subpagepattern = NULL
1719                       WHERE pagetypepattern = 'blog-view'");
1721     /// mod-xxx-view
1722         $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1723         foreach ($moduleswithblocks as $modname) {
1724             if (!$dbman->table_exists($modname)) {
1725                 continue;
1726             }
1727             $DB->execute("UPDATE {block_instances} SET
1728                             contextid = (
1729                                 SELECT {context}.id
1730                                 FROM {context}
1731                                 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1732                                 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1733                                 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1734                                 WHERE {{$modname}}.id = pageid
1735                             ),
1736                             pagetypepattern = 'blog-index',
1737                             subpagepattern = NULL
1738                           WHERE pagetypepattern = 'blog-view'");
1739         }
1741     /// Main savepoint reached
1742         upgrade_main_savepoint(true, 2009050613);
1743     }
1745     if ($oldversion < 2009050614) {
1746     /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
1747         $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
1749     /// Main savepoint reached
1750         upgrade_main_savepoint(true, 2009050614);
1751     }
1753     if ($oldversion < 2009050615) {
1754         $table = new xmldb_table('block_instances');
1756     /// Arrived here, any block_instances record without blockname is one
1757     /// orphan block coming from 1.9. Just delete them. MDL-22503
1758         $DB->delete_records_select('block_instances', 'blockname IS NULL');
1760     /// Changing nullability of field blockname on table block_instances to not null
1761         $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
1762         $dbman->change_field_notnull($table, $field);
1764     /// Changing nullability of field contextid on table block_instances to not null
1765         $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
1766         $dbman->change_field_notnull($table, $field);
1768     /// Changing nullability of field showinsubcontexts on table block_instances to not null
1769         $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
1770         $dbman->change_field_notnull($table, $field);
1772     /// Main savepoint reached
1773         upgrade_main_savepoint(true, 2009050615);
1774     }
1776     if ($oldversion < 2009050616) {
1777     /// Add exiting sticky blocks.
1778         $blocks = $DB->get_records('block');
1779         $syscontext = get_context_instance(CONTEXT_SYSTEM);
1780         $newregions = array(
1781             'l' => 'side-pre',
1782             'r' => 'side-post',
1783             'c' => 'course-view-top',
1784         );
1785         $stickyblocks = $DB->get_recordset('block_pinned_old');
1786         foreach ($stickyblocks as $stickyblock) {
1787             // Only if the block exists (avoid orphaned sticky blocks)
1788             if (!isset($blocks[$stickyblock->blockid]) || empty($blocks[$stickyblock->blockid]->name)) {
1789                 continue;
1790             }
1791             $newblock = new stdClass();
1792             $newblock->blockname = $blocks[$stickyblock->blockid]->name;
1793             $newblock->contextid = $syscontext->id;
1794             $newblock->showinsubcontexts = 1;
1795             switch ($stickyblock->pagetype) {
1796                 case 'course-view':
1797                     $newblock->pagetypepattern = 'course-view-*';
1798                     break;
1799                 default:
1800                     $newblock->pagetypepattern = $stickyblock->pagetype;
1801             }
1802             $newblock->defaultregion = $newregions[$stickyblock->position];
1803             $newblock->defaultweight = $stickyblock->weight;
1804             $newblock->configdata = $stickyblock->configdata;
1805             $newblock->visible = 1;
1806             $DB->insert_record('block_instances', $newblock);
1807         }
1809     /// Main savepoint reached
1810         upgrade_main_savepoint(true, 2009050616);
1811     }
1813     if ($oldversion < 2009050617) {
1815     /// Define table block_positions to be created
1816         $table = new xmldb_table('block_positions');
1818     /// Adding fields to table block_positions
1819         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1820         $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1821         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1822         $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1823         $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1824         $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1');
1825         $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1826         $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1828     /// Adding keys to table block_positions
1829         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1830         $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
1831         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1833     /// Adding indexes to table block_positions
1834         $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
1836     /// Conditionally launch create table for block_positions
1837         if (!$dbman->table_exists($table)) {
1838             $dbman->create_table($table);
1839         }
1841     /// Main savepoint reached
1842         upgrade_main_savepoint(true, 2009050617);
1843     }
1845     if ($oldversion < 2009050618) {
1846     /// And block instances with visible = 0, copy that information to block_positions
1847         $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
1848                 SELECT id, contextid,
1849                 CASE WHEN pagetypepattern = 'course-view-*' THEN
1850                         (SELECT " . $DB->sql_concat("'course-view-'", 'format') . "
1851                         FROM {course}
1852                         JOIN {context} ON {course}.id = {context}.instanceid
1853                         WHERE {context}.id = contextid)
1854                     ELSE pagetypepattern END,
1855                 CASE WHEN subpagepattern IS NULL THEN ''
1856                     ELSE subpagepattern END,
1857                 0, defaultregion, defaultweight
1858                 FROM {block_instances} WHERE visible = 0 AND pagetypepattern <> 'admin-*'");
1860     /// Main savepoint reached
1861         upgrade_main_savepoint(true, 2009050618);
1862     }
1864     if ($oldversion < 2009050619) {
1865         $table = new xmldb_table('block_instances');
1867     /// Define field blockid to be dropped from block_instances
1868         $field = new xmldb_field('blockid');
1869         if ($dbman->field_exists($table, $field)) {
1870         /// Before dropping the field, drop dependent indexes
1871             $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
1872             if ($dbman->index_exists($table, $index)) {
1873             /// Launch drop index blockid
1874                 $dbman->drop_index($table, $index);
1875             }
1876             $dbman->drop_field($table, $field);
1877         }
1879     /// Define field pageid to be dropped from block_instances
1880         $field = new xmldb_field('pageid');
1881         if ($dbman->field_exists($table, $field)) {
1882         /// Before dropping the field, drop dependent indexes
1883             $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1884             if ($dbman->index_exists($table, $index)) {
1885             /// Launch drop index pageid
1886                 $dbman->drop_index($table, $index);
1887             }
1888             $dbman->drop_field($table, $field);
1889         }
1891     /// Define field visible to be dropped from block_instances
1892         $field = new xmldb_field('visible');
1893         if ($dbman->field_exists($table, $field)) {
1894             $dbman->drop_field($table, $field);
1895         }
1897     /// Main savepoint reached
1898         upgrade_main_savepoint(true, 2009050619);
1899     }
1901     if ($oldversion < 2009051200) {
1902     /// Let's check the status of mandatory mnet_host records, fixing them
1903     /// and moving "orphan" users to default localhost record. MDL-16879
1904         echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
1905         upgrade_fix_incorrect_mnethostids();
1907     /// Main savepoint reached
1908         upgrade_main_savepoint(true, 2009051200);
1909     }
1912     if ($oldversion < 2009051700) {
1913     /// migrate editor settings
1914         if (empty($CFG->htmleditor)) {
1915             set_config('texteditors', 'textarea');
1916         } else {
1917             set_config('texteditors', 'tinymce,textarea');
1918         }
1920         unset_config('htmleditor');
1921         unset_config('defaulthtmleditor');
1923     /// Main savepoint reached
1924         upgrade_main_savepoint(true, 2009051700);
1925     }
1927     /// Repeat 2009050607 upgrade step, which Petr commented out because of XMLDB
1928     /// stupidity, so lots of people will have missed.
1929     if ($oldversion < 2009061600) {
1930     /// Changing precision of field defaultregion on table block_instances to (16)
1931         $table = new xmldb_table('block_instances');
1932         $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
1934     /// Launch change of precision for field defaultregion
1935         $dbman->change_field_precision($table, $field);
1937     /// Main savepoint reached
1938         upgrade_main_savepoint(true, 2009061600);
1939     }
1941     if ($oldversion < 2009061702) {
1942         // standardizing plugin names
1943         if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
1944             foreach ($configs as $config) {
1945                 unset_config($config->name, $config->plugin); /// unset old config
1946                 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
1947                 set_config($config->name, $config->value, $config->plugin); /// set new config
1948             }
1949         }
1950         unset($configs);
1951         upgrade_main_savepoint(true, 2009061702);
1952     }
1954     if ($oldversion < 2009061703) {
1955         // standardizing plugin names
1956         if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
1957             foreach ($configs as $config) {
1958                 unset_config($config->name, $config->plugin); /// unset old config
1959                 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
1960                 set_config($config->name, $config->value, $config->plugin); /// set new config
1961             }
1962         }
1963         unset($configs);
1964         upgrade_main_savepoint(true, 2009061703);
1965     }
1967     if ($oldversion < 2009061704) {
1968         // change component string in capability records to new "_" format
1969         if ($caps = $DB->get_records('capabilities')) {
1970             foreach ($caps as $cap) {
1971                 $cap->component = str_replace('/', '_', $cap->component);
1972                 $DB->update_record('capabilities', $cap);
1973             }
1974         }
1975         unset($caps);
1976         upgrade_main_savepoint(true, 2009061704);
1977     }
1979     if ($oldversion < 2009063000) {
1980         // upgrade format of _with_advanced settings - quiz only
1981         // note: this can be removed later, not needed for upgrades from 1.9.x
1982         if ($quiz = get_config('quiz')) {
1983             foreach ($quiz as $name=>$value) {
1984                 if (strpos($name, 'fix_') !== 0) {
1985                     continue;
1986                 }
1987                 $newname = substr($name,4).'_adv';
1988                 set_config($newname, $value, 'quiz');
1989                 unset_config($name, 'quiz');
1990             }
1991         }
1992         upgrade_main_savepoint(true, 2009063000);
1993     }
1995     if ($oldversion < 2009071000) {
1997     /// Rename field contextid on table block_instances to parentcontextid
1998         $table = new xmldb_table('block_instances');
1999         $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
2001     /// Launch rename field parentcontextid
2002         $dbman->rename_field($table, $field, 'parentcontextid');
2004     /// Main savepoint reached
2005         upgrade_main_savepoint(true, 2009071000);
2006     }
2008     if ($oldversion < 2009071600) {
2010     /// Define field summaryformat to be added to post
2011         $table = new xmldb_table('post');
2012         $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
2014     /// Conditionally launch add field summaryformat
2015         if (!$dbman->field_exists($table, $field)) {
2016             $dbman->add_field($table, $field);
2017         }
2019     /// Main savepoint reached
2020         upgrade_main_savepoint(true, 2009071600);
2021     }
2023     if ($oldversion < 2009072400) {
2025     /// Define table comments to be created
2026         $table = new xmldb_table('comments');
2028     /// Adding fields to table comments
2029         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2030         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2031         $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2032         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2033         $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2034         $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2035         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2036         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2038     /// Adding keys to table comments
2039         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2041     /// Conditionally launch create table for comments
2042         if (!$dbman->table_exists($table)) {
2043             $dbman->create_table($table);
2044         }
2046     /// Main savepoint reached
2047         upgrade_main_savepoint(true, 2009072400);
2048     }
2050     /**
2051      * This upgrade is to set up the new navigation blocks that have been developed
2052      * as part of Moodle 2.0
2053      * Now I [Sam Hemelryk] hit a conundrum while exploring how to go about this
2054      * as not only do we want to install the new blocks but we also want to set up
2055      * default instances of them, and at the same time remove instances of the blocks
2056      * that were/will-be outmoded by the two new navigation blocks.
2057      * After talking it through with Tim Hunt {@link http://moodle.org/mod/cvsadmin/view.php?conversationid=3112}
2058      * we decided that the best way to go about this was to put the bulk of the
2059      * upgrade operation into core upgrade `here` but to let the plugins block
2060      * still install the blocks.
2061      * This leaves one hairy end in that we will create block_instances within the
2062      * DB before the blocks themselves are created within the DB
2063      */
2064     if ($oldversion < 2009082800) {
2066         echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2068         // Get the system context so we can set the block instances to it
2069         $syscontext = get_context_instance(CONTEXT_SYSTEM);
2071         // An array to contain the new block instances we will create
2072         $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
2073         // The new global navigation block instance as a stdClass
2074         $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
2075         $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
2076         $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutely everywhere
2077         $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
2078         $newblockinstances['globalnavigation']->subpagetypepattern = null;
2079         $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
2080         $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
2081         $newblockinstances['globalnavigation']->configdata = '';
2082         // The new settings navigation block instance as a stdClass
2083         $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
2084         $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
2085         $newblockinstances['settingsnavigation']->showinsubcontexts = true;
2086         $newblockinstances['settingsnavigation']->pagetypepattern = '*';
2087         $newblockinstances['settingsnavigation']->subpagetypepattern = null;
2088         $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
2089         $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
2090         $newblockinstances['settingsnavigation']->configdata = '';
2092         // Blocks that are outmoded and for whom the bells will toll... by which I
2093         // mean we will delete all instances of
2094         $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
2095         $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
2096         unset($outmodedblocks);
2097         // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
2098         // cut down the number of delete queries we will need to run
2099         $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
2101         $contextids = array();
2102         $instanceids = array();
2103         // Iterate through all block instances
2104         foreach ($allblockinstances as $blockinstance) {
2105             if (!in_array($blockinstance->parentcontextid, $contextids)) {
2106                 $contextids[] = $blockinstance->parentcontextid;
2108                 // If we have over 1000 contexts clean them up and reset the array
2109                 // this ensures we don't hit any nasty memory limits or such
2110                 if (count($contextids) > 1000) {
2111                     upgrade_cleanup_unwanted_block_contexts($contextids);
2112                     $contextids = array();
2113                 }
2114             }
2115             if (!in_array($blockinstance->id, $instanceids)) {
2116                 $instanceids[] = $blockinstance->id;
2117                 // If we have more than 1000 block instances now remove all block positions
2118                 // and empty the array
2119                 if (count($contextids) > 1000) {
2120                     $instanceidstring = join(',',$instanceids);
2121                     $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2122                     $instanceids = array();
2123                 }
2124             }
2125         }
2127         upgrade_cleanup_unwanted_block_contexts($contextids);
2129         $instanceidstring = join(',',$instanceids);
2130         $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2132         unset($allblockinstances);
2133         unset($contextids);
2134         unset($instanceids);
2135         unset($instanceidstring);
2137         // Now remove the actual block instance
2138         $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
2139         unset($outmodedblocksstring);
2141         // Insert the new block instances. Remember they have not been installed yet
2142         // however this should not be a problem
2143         foreach ($newblockinstances as $blockinstance) {
2144             $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
2145             // Ensure the block context is created.
2146             get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
2147         }
2148         unset($newblockinstances);
2150         upgrade_main_savepoint(true, 2009082800);
2151         // The end of the navigation upgrade
2152     }
2154     if ($oldversion < 2009100602) {
2155     /// Define table external_functions to be created
2156         $table = new xmldb_table('external_functions');
2158     /// Adding fields to table external_functions
2159         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2160         $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2161         $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2162         $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2163         $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2164         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2166     /// Adding keys to table external_functions
2167         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2169     /// Adding indexes to table external_functions
2170         $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2172     /// Launch create table for external_functions
2173         $dbman->create_table($table);
2175     /// Main savepoint reached
2176         upgrade_main_savepoint(true, 2009100602);
2177     }
2179     if ($oldversion < 2009100603) {
2180         /// Define table external_services to be created
2181         $table = new xmldb_table('external_services');
2183     /// Adding fields to table external_services
2184         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2185         $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2186         $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2187         $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
2188         $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2189         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
2190         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2191         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2193     /// Adding keys to table external_services
2194         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2196     /// Adding indexes to table external_services
2197         $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2199     /// Launch create table for external_services
2200         $dbman->create_table($table);
2202     /// Main savepoint reached
2203         upgrade_main_savepoint(true, 2009100603);
2204     }
2206     if ($oldversion < 2009100604) {
2207     /// Define table external_services_functions to be created
2208         $table = new xmldb_table('external_services_functions');
2210     /// Adding fields to table external_services_functions
2211         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2212         $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2213         $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2215     /// Adding keys to table external_services_functions
2216         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2217         $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2219     /// Launch create table for external_services_functions
2220         $dbman->create_table($table);
2222     /// Main savepoint reached
2223         upgrade_main_savepoint(true, 2009100604);
2224     }
2226     if ($oldversion < 2009100605) {
2227     /// Define table external_services_users to be created
2228         $table = new xmldb_table('external_services_users');
2230     /// Adding fields to table external_services_users
2231         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2232         $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2233         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2234         $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2235         $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2236         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2238     /// Adding keys to table external_services_users
2239         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2240         $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2241         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2243     /// Launch create table for external_services_users
2244         $dbman->create_table($table);
2246     /// Main savepoint reached
2247         upgrade_main_savepoint(true, 2009100605);
2248     }
2250     if ($oldversion < 2009102600) {
2252     /// Define table external_tokens to be created
2253         $table = new xmldb_table('external_tokens');
2255     /// Adding fields to table external_tokens
2256         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2257         $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
2258         $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2259         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2260         $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2261         $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
2262         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2263         $table->add_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
2264         $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2265         $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2266         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2267         $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2269     /// Adding keys to table external_tokens
2270         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2271         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2272         $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2273         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2274         $table->add_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
2276     /// Launch create table for external_tokens
2277         $dbman->create_table($table);
2279     /// Main savepoint reached
2280         upgrade_main_savepoint(true, 2009102600);
2281     }
2283    if ($oldversion < 2009103000) {
2285     /// Define table blog_association to be created
2286         $table = new xmldb_table('blog_association');
2288     /// Adding fields to table blog_association
2289         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2290         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2291         $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2293     /// Adding keys to table blog_association
2294         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2295         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2296         $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
2298     /// Conditionally launch create table for blog_association
2299         if (!$dbman->table_exists($table)) {
2300             $dbman->create_table($table);
2301         }
2303 /// Define table blog_external to be created
2304         $table = new xmldb_table('blog_external');
2306     /// Adding fields to table blog_external
2307         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2308         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2309         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2310         $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2311         $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2312         $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2313         $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2314         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2315         $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2317     /// Adding keys to table blog_external
2318         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2319         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2321     /// Conditionally launch create table for blog_external
2322         if ($dbman->table_exists($table)) {
2323             // Delete the existing one first (comes from early dev version)
2324             $dbman->drop_table($table);
2325         }
2326         $dbman->create_table($table);
2328         // now inform admins that some settings require attention after upgrade
2329         if (($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete)) {
2330             echo $OUTPUT->notification(get_string('bloglevelupgradenotice', 'admin'));
2332             $site = get_site();
2334             $a = new StdClass;
2335             $a->sitename = $site->fullname;
2336             $a->fixurl   = "$CFG->wwwroot/$CFG->admin/bloglevelupgrade.php";
2338             $subject = get_string('bloglevelupgrade', 'admin');
2339             $description = get_string('bloglevelupgradedescription', 'admin', $a);
2341             // can not use messaging here because it is not configured yet!
2342             upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2343         }
2344     /// Main savepoint reached
2345         upgrade_main_savepoint(true, 2009103000);
2346     }
2348     if ($oldversion < 2009110400) {
2349         // list of tables where we need to add new format field and convert texts
2350         $extendtables = array('course' => 'summary',
2351                               'course_categories' => 'description',
2352                               'course_categories' => 'description',
2353                               'course_request' => 'summary',
2354                               'grade_outcomes' => 'description',
2355                               'groups' => 'description',
2356                               'groupings' => 'description',
2357                               'scale' => 'description',
2358                               'user_info_field' => 'description',
2359                               'user_info_field' => 'defaultdata',
2360                               'user_info_data' => 'data');
2362         foreach ($extendtables as $tablestr=>$fieldstr) {
2363             $formatfieldstr = $fieldstr.'format';
2365             $table = new xmldb_table($tablestr);
2366             $field = new xmldb_field($formatfieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', $fieldstr);
2367             // Check that the field doesn't already exists
2368             if (!$dbman->field_exists($table, $field)) {
2369                 // Add the new field
2370                 $dbman->add_field($table, $field);
2371             }
2372             if ($CFG->texteditors !== 'textarea') {
2373                 $rs = $DB->get_recordset($tablestr, array($formatfieldstr=>FORMAT_MOODLE), '', "id,$fieldstr,$formatfieldstr");
2374                 foreach ($rs as $rec) {
2375                     $rec->$fieldstr       = text_to_html($rec->$fieldstr, false, false, true);
2376                     $rec->$formatfieldstr = FORMAT_HTML;
2377                     $DB->update_record($tablestr, $rec);
2378                     upgrade_set_timeout();
2379                 }
2380                 $rs->close();
2381             }
2382         }
2384         unset($rec);
2385         unset($extendtables);
2387         upgrade_main_savepoint(true, 2009110400);
2388     }
2390     if ($oldversion < 2009110401) {
2391         $table = new xmldb_table('user');
2393         // Change the precision of the description field first up.
2394         // This may grow!
2395         $field = new xmldb_field('description', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'url');
2396         $dbman->change_field_precision($table, $field);
2398         $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'description');
2399         // Check that the field doesn't already exists
2400         if (!$dbman->field_exists($table, $field)) {
2401             // Add the new field
2402             $dbman->add_field($table, $field);
2403         }
2404         if ($CFG->texteditors !== 'textarea') {
2405             $rs = $DB->get_recordset('user', array('descriptionformat'=>FORMAT_MOODLE, 'deleted'=>0, 'htmleditor'=>1), '', "id,description,descriptionformat");
2406             foreach ($rs as $rec) {
2407                 $rec->description       = text_to_html($rec->description, false, false, true);
2408                 $rec->descriptionformat = FORMAT_HTML;
2409                 $DB->update_record('user', $rec);
2410                 upgrade_set_timeout();
2411             }
2412             $rs->close();
2413         }
2415         upgrade_main_savepoint(true, 2009110401);
2416     }
2418     if ($oldversion < 2009112400) {
2419         if (empty($CFG->passwordsaltmain)) {
2420             $subject = get_string('check_passwordsaltmain_name', 'report_security');
2421             $description = get_string('check_passwordsaltmain_warning', 'report_security');;
2422             upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2423         }
2424         upgrade_main_savepoint(true, 2009112400);
2425     }
2427     if ($oldversion < 2010011200) {
2428         $table = new xmldb_table('grade_categories');
2429         $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
2431         if (!$dbman->field_exists($table, $field)) {
2432             $dbman->add_field($table, $field);
2433         }
2435         upgrade_main_savepoint(true, 2010011200);
2436     }
2438     if ($oldversion < 2010012500) {
2439         upgrade_fix_incorrect_mnethostids();
2440         upgrade_main_savepoint(true, 2010012500);
2441     }
2443     if ($oldversion < 2010012600) {
2444         // do stuff to the mnet table
2445         $table = new xmldb_table('mnet_rpc');
2447         $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2448         $dbman->rename_field($table, $field, 'plugintype');
2450         $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2451         $dbman->rename_field($table, $field, 'pluginname');
2453         $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
2454         if (!$dbman->field_exists($table, $field)) {
2455             $dbman->add_field($table, $field);
2456         }
2458         $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
2459         if (!$dbman->field_exists($table, $field)) {
2460             $dbman->add_field($table, $field);
2461         }
2463         $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
2464         if (!$dbman->field_exists($table, $field)) {
2465             $dbman->add_field($table, $field);
2466         }
2468     /// Main savepoint reached
2469         upgrade_main_savepoint(true, 2010012600);
2470     }
2472     if ($oldversion < 2010012900) {
2474     /// Define table mnet_remote_rpc to be created
2475         $table = new xmldb_table('mnet_remote_rpc');
2477     /// Adding fields to table mnet_remote_rpc
2478         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2479         $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2480         $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
2482     /// Adding keys to table mnet_remote_rpc
2483         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2485     /// Conditionally launch create table for mnet_remote_rpc
2486         if (!$dbman->table_exists($table)) {
2487             $dbman->create_table($table);
2488         }
2491     /// Define table mnet_remote_service2rpc to be created
2492         $table = new xmldb_table('mnet_remote_service2rpc');
2494     /// Adding fields to table mnet_remote_service2rpc
2495         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2496         $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2497         $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2499     /// Adding keys to table mnet_remote_service2rpc
2500         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2502     /// Adding indexes to table mnet_remote_service2rpc
2503         $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2505     /// Conditionally launch create table for mnet_remote_service2rpc
2506         if (!$dbman->table_exists($table)) {
2507             $dbman->create_table($table);
2508         }
2511     /// Rename field function_name on table mnet_rpc to functionname
2512         $table = new xmldb_table('mnet_rpc');
2513         $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
2515     /// Launch rename field function_name
2516         $dbman->rename_field($table, $field, 'functionname');
2519     /// Rename field xmlrpc_path on table mnet_rpc to xmlrpcpath
2520         $table = new xmldb_table('mnet_rpc');
2521         $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
2523     /// Launch rename field xmlrpc_path
2524         $dbman->rename_field($table, $field, 'xmlrpcpath');
2527     /// Main savepoint reached
2528         upgrade_main_savepoint(true, 2010012900);
2529     }
2531     if ($oldversion < 2010012901) {
2533         /// Define field plugintype to be added to mnet_remote_rpc
2534         $table = new xmldb_table('mnet_remote_rpc');
2535         $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
2537         /// Conditionally launch add field plugintype
2538         if (!$dbman->field_exists($table, $field)) {
2539             $dbman->add_field($table, $field);
2540         }
2542     /// Define field pluginname to be added to mnet_remote_rpc
2543         $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
2545     /// Conditionally launch add field pluginname
2546         if (!$dbman->field_exists($table, $field)) {
2547             $dbman->add_field($table, $field);
2548         }
2550         /// Main savepoint reached
2551         upgrade_main_savepoint(true, 2010012901);
2552     }
2554     if ($oldversion < 2010012902) {
2556     /// Define field enabled to be added to mnet_remote_rpc
2557         $table = new xmldb_table('mnet_remote_rpc');
2558         $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
2560     /// Conditionally launch add field enabled
2561         if (!$dbman->field_exists($table, $field)) {
2562             $dbman->add_field($table, $field);
2563         }
2565         /// Main savepoint reached
2566         upgrade_main_savepoint(true, 2010012902);
2567     }
2569     /// MDL-17863. Increase the portno column length on mnet_host to handle any port number
2570     if ($oldversion < 2010020100) {
2571     /// Changing precision of field portno on table mnet_host to (5)
2572         $table = new xmldb_table('mnet_host');
2573         $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
2575     /// Launch change of precision for field portno
2576         $dbman->change_field_precision($table, $field);
2578         upgrade_main_savepoint(true, 2010020100);
2579     }
2581     if ($oldversion < 2010020300) {
2583     /// Define field timecreated to be added to user
2584         $table = new xmldb_table('user');
2585         $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
2587         if (!$dbman->field_exists($table, $field)) {
2588         /// Launch add field timecreated
2589             $dbman->add_field($table, $field);
2591             $DB->execute("UPDATE {user} SET timecreated = firstaccess");
2593             $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
2594             $DB->execute($sql);
2595         }
2596         upgrade_main_savepoint(true, 2010020300);
2597     }
2599     // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
2600     if ($oldversion < 2010020301) {
2601         if ($preamble = $CFG->filter_tex_latexpreamble) {
2602             $preamble = preg_replace('/^ +/m', '', $preamble);
2603             set_config('filter_tex_latexpreamble', $preamble);
2604         }
2605         upgrade_main_savepoint(true, 2010020301);
2606     }
2608     if ($oldversion < 2010021400) {
2609     /// Changes to modinfo mean we need to rebuild course cache
2610         require_once($CFG->dirroot . '/course/lib.php');
2611         rebuild_course_cache(0, true);
2612         upgrade_main_savepoint(true, 2010021400);
2613     }
2615     if ($oldversion < 2010021800) {
2616         $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
2617         upgrade_main_savepoint(true, 2010021800);
2618     }
2620     if ($oldversion < 2010031900) {
2621         // regeneration of sessions is always enabled, no need for this setting any more
2622         unset_config('regenloginsession');
2623         upgrade_main_savepoint(true, 2010031900);
2624     }
2626     if ($oldversion < 2010033101.02) {
2628     /// Define table license to be created
2629         $table = new xmldb_table('license');
2631     /// Adding fields to table license
2632         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2633         $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2634         $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2635         $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2636         $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
2637         $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2639     /// Adding keys to table license
2640         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2642     /// Conditionally launch create table for license
2643         if (!$dbman->table_exists($table)) {
2644             $dbman->create_table($table);
2645         }
2646         $active_licenses = array();
2648         $license = new stdClass();
2650         // add unknown license
2651         $license->shortname = 'unknown';
2652         $license->fullname = 'Unknown license';
2653         $license->source = '';
2654         $license->enabled = 1;
2655         $license->version = '2010033100';
2656         $active_licenses[] = $license->shortname;
2657         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2658             if ($record->version < $license->version) {
2659                 // update license record
2660                 $license->enabled = $record->enabled;
2661                 $license->id = $record->id;
2662                 $DB->update_record('license', $license);
2663             }
2664         } else {
2665             $DB->insert_record('license', $license);
2666         }
2668         // add all rights reserved license
2669         $license->shortname = 'allrightsreserved';
2670         $license->fullname = 'All rights reserved';
2671         $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
2672         $license->enabled = 1;
2673         $license->version = '2010033100';
2674         $active_licenses[] = $license->shortname;
2675         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2676             if ($record->version < $license->version) {
2677                 // update license record
2678                 $license->id = $record->id;
2679                 $license->enabled = $record->enabled;
2680                 $DB->update_record('license', $license);
2681             }
2682         } else {
2683             $DB->insert_record('license', $license);
2684         }
2686         // add public domain license
2687         $license->shortname = 'public';
2688         $license->fullname = 'Public Domain';
2689         $license->source = 'http://creativecommons.org/licenses/publicdomain/';
2690         $license->enabled = 1;
2691         $license->version = '2010033100';
2692         $active_licenses[] = $license->shortname;
2693         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2694             if ($record->version < $license->version) {
2695                 // update license record
2696                 $license->enabled = $record->enabled;
2697                 $license->id = $record->id;
2698                 $DB->update_record('license', $license);
2699             }
2700         } else {
2701             $DB->insert_record('license', $license);
2702         }
2704         // add creative commons license
2705         $license->shortname = 'cc';
2706         $license->fullname = 'Creative Commons';
2707         $license->source = 'http://creativecommons.org/licenses/by/3.0/';
2708         $license->enabled = 1;
2709         $license->version = '2010033100';
2710         $active_licenses[] = $license->shortname;
2711         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2712             if ($record->version < $license->version) {
2713                 // update license record
2714                 $license->enabled = $record->enabled;
2715                 $license->id = $record->id;
2716                 $DB->update_record('license', $license);
2717             }
2718         } else {
2719             $DB->insert_record('license', $license);
2720         }
2722         // add creative commons no derivs license
2723         $license->shortname = 'cc-nd';
2724         $license->fullname = 'Creative Commons - NoDerivs';
2725         $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
2726         $license->enabled = 1;
2727         $license->version = '2010033100';
2728         $active_licenses[] = $license->shortname;
2729         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2730             if ($record->version < $license->version) {
2731                 // update license record
2732                 $license->enabled = $record->enabled;
2733                 $license->id = $record->id;
2734                 $DB->update_record('license', $license);
2735             }
2736         } else {
2737             $DB->insert_record('license', $license);
2738         }
2740         // add creative commons no commercial no derivs license
2741         $license->shortname = 'cc-nc-nd';
2742         $license->fullname = 'Creative Commons - No Commercial NoDerivs';
2743         $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
2744         $license->enabled = 1;
2745         $license->version = '2010033100';
2746         $active_licenses[] = $license->shortname;
2747         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2748             if ($record->version < $license->version) {
2749                 // update license record
2750                 $license->enabled = $record->enabled;
2751                 $license->id = $record->id;
2752                 $DB->update_record('license', $license);
2753             }
2754         } else {
2755             $DB->insert_record('license', $license);
2756         }
2758         // add creative commons no commercial
2759         $license->shortname = 'cc-nc-nd';
2760         $license->shortname = 'cc-nc';
2761         $license->fullname = 'Creative Commons - No Commercial';
2762         $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
2763         $license->enabled = 1;
2764         $license->version = '2010033100';
2765         $active_licenses[] = $license->shortname;
2766         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2767             if ($record->version < $license->version) {
2768                 // update license record
2769                 $license->enabled = $record->enabled;
2770                 $license->id = $record->id;
2771                 $DB->update_record('license', $license);
2772             }
2773         } else {
2774             $DB->insert_record('license', $license);
2775         }
2777         // add creative commons no commercial sharealike
2778         $license->shortname = 'cc-nc-sa';
2779         $license->fullname = 'Creative Commons - No Commercial ShareAlike';
2780         $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
2781         $license->enabled = 1;
2782         $license->version = '2010033100';
2783         $active_licenses[] = $license->shortname;
2784         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2785             if ($record->version < $license->version) {
2786                 // update license record
2787                 $license->enabled = $record->enabled;
2788                 $license->id = $record->id;
2789                 $DB->update_record('license', $license);
2790             }
2791         } else {
2792             $DB->insert_record('license', $license);
2793         }
2795         // add creative commons sharealike
2796         $license->shortname = 'cc-sa';
2797         $license->fullname = 'Creative Commons - ShareAlike';
2798         $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
2799         $license->enabled = 1;
2800         $license->version = '2010033100';
2801         $active_licenses[] = $license->shortname;
2802         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2803             if ($record->version < $license->version) {
2804                 // update license record
2805                 $license->enabled = $record->enabled;
2806                 $license->id = $record->id;
2807                 $DB->update_record('license', $license);
2808             }
2809         } else {
2810             $DB->insert_record('license', $license);
2811         }
2813         set_config('licenses', implode(',', $active_licenses));
2814     /// set site default license
2815         set_config('sitedefaultlicense', 'allrightsreserved');
2817     /// Main savepoint reached
2818         upgrade_main_savepoint(true, 2010033101.02);
2819     }
2821     if ($oldversion < 2010033102.00) {
2822         // rename course view capability to participate
2823         $params = array('view'=>'moodle/course:view', 'participate'=>'moodle/course:participate');
2824         $sql = "UPDATE {role_capabilities} SET capability = :participate WHERE capability = :view";
2825         $DB->execute($sql, $params);
2826         $sql = "UPDATE {capabilities} SET name = :participate WHERE name = :view";
2827         $DB->execute($sql, $params);
2828         // note: the view capability is readded again at the end of upgrade, but with different meaning
2829         upgrade_main_savepoint(true, 2010033102.00);
2830     }
2832     if ($oldversion < 2010033102.01) {
2833         // Define field archetype to be added to role table
2834         $table = new xmldb_table('role');
2835         $field = new xmldb_field('archetype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'sortorder');
2836         $dbman->add_field($table, $field);
2837         upgrade_main_savepoint(true, 2010033102.01);
2838     }
2840     if ($oldversion < 2010033102.02) {
2841         // Set archetype for existing roles and change admin role to manager role
2842         $sql = "SELECT r.*, rc.capability
2843                   FROM {role} r
2844                   JOIN {role_capabilities} rc ON rc.roleid = r.id
2845                  WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
2846               ORDER BY r.id";
2847         $params = array('syscontextid'=>SYSCONTEXTID, 'legacycaps'=>'moodle/legacy:%');
2848         $substart = strlen('moodle/legacy:');
2849         $roles = $DB->get_recordset_sql($sql, $params); // in theory could be multiple legacy flags in one role
2850         foreach ($roles as $role) {
2851             $role->archetype = substr($role->capability, $substart);
2852             unset($role->capability);
2853             if ($role->archetype === 'admin') {
2854                 $role->archetype = 'manager';
2855                 if ($role->shortname === 'admin') {
2856                     $role->shortname   = 'manager';
2857                     $role->name        = get_string('manager', 'role');
2858                     $role->description = get_string('managerdescription', 'role');
2859                 }
2860             }
2861             $DB->update_record('role', $role);
2862         }
2863         $roles->close();
2865         upgrade_main_savepoint(true, 2010033102.02);
2866     }
2868     if ($oldversion < 2010033102.03) {
2869         // Now pick site admins (===have manager role assigned at the system context)
2870         // and store them in the new $CFG->siteadmins setting as comma separated list
2871         $sql = "SELECT ra.id, ra.userid
2872                   FROM {role_assignments} ra
2873                   JOIN {role} r ON r.id = ra.roleid
2874                   JOIN {user} u ON u.id = ra.userid
2875                  WHERE ra.contextid = :syscontext AND r.archetype = 'manager' AND u.deleted = 0
2876               ORDER BY ra.id";
2877         $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
2878         $admins = array();
2879         foreach ($ras as $ra) {
2880             $admins[$ra->userid] = $ra->userid;
2881             set_config('siteadmins', implode(',', $admins)); // better to save it repeatedly, we do need at least one admin
2882             $DB->delete_records('role_assignments', array('id'=>$ra->id));
2883         }
2885         upgrade_main_savepoint(true, 2010033102.03);
2886     }
2888     if ($oldversion < 2010033102.04) {
2889         // clean up the manager roles
2890         $managers = $DB->get_records('role', array('archetype'=>'manager'));
2891         foreach ($managers as $manager) {
2892             // now sanitize the capabilities and overrides
2893             $DB->delete_records('role_capabilities', array('capability'=>'moodle/site:config', 'roleid'=>$manager->id)); // only site admins may configure servers
2894             // note: doanything and legacy caps are deleted automatically, they get moodle/course:view later at the end of the upgrade
2896             // remove manager role assignments bellow the course context level - admin role was never intended for activities and blocks,
2897             // the problem is that those assignments would not be visible after upgrade and old style admins in activities make no sense anyway
2898             $DB->delete_records_select('role_assignments', "roleid = :manager AND contextid IN (SELECT id FROM {context} WHERE contextlevel > 50)", array('manager'=>$manager->id));
2900             // allow them to assign all roles except default user, guest and frontpage - users get these roles automatically on the fly when needed
2901             $DB->delete_records('role_allow_assign', array('roleid'=>$manager->id));
2902             $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype <> 'user' AND archetype <> 'guest' AND archetype <> 'frontpage'");
2903             foreach ($roles as $role) {
2904                 $record = (object)array('roleid'=>$manager->id, 'allowassign'=>$role->id);
2905                 $DB->insert_record('role_allow_assign', $record);
2906             }
2908             // allow them to override all roles
2909             $DB->delete_records('role_allow_override', array('roleid'=>$manager->id));
2910             $roles = $DB->get_records_sql("SELECT * FROM {role}");
2911             foreach ($roles as $role) {
2912                 $record = (object)array('roleid'=>$manager->id, 'allowoverride'=>$role->id);
2913                 $DB->insert_record('role_allow_override', $record);
2914             }
2916             // allow them to switch to all following roles
2917             $DB->delete_records('role_allow_switch', array('roleid'=>$manager->id));
2918             $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype IN ('student', 'teacher', 'editingteacher')");
2919             foreach ($roles as $role) {
2920                 $record = (object)array('roleid'=>$manager->id, 'allowswitch'=>$role->id);
2921                 $DB->insert_record('role_allow_switch', $record);
2922             }
2923         }
2925         upgrade_main_savepoint(true, 2010033102.04);
2926     }
2928     if ($oldversion < 2010033102.05) {
2929         // remove course:view from all roles that are not used for enrolment, it does NOT belong there because it really means user is enrolled!
2930         $noenrolroles = $DB->get_records_select('role', "archetype IN ('guest', 'user', 'manager', 'coursecreator', 'frontpage')");
2931         foreach ($noenrolroles as $role) {
2932             $DB->delete_records('role_capabilities', array('roleid'=>$role->id, 'capability'=>'moodle/course:participate'));
2933         }
2934         upgrade_main_savepoint(true, 2010033102.05);
2935     }
2937     if ($oldversion < 2010033102.06) {
2938         // make sure there is nothing weird in default user role
2939         if (!empty($CFG->defaultuserroleid)) {
2940             if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
2941                 if ($role->archetype !== '' and $role->archetype !== 'user') {
2942                     upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default authenticated user role (defaultuserroleid) value is invalid, setting cleared.');
2943                     unset_config('defaultuserroleid');
2944                 }
2945             } else {
2946                 unset_config('defaultuserroleid');
2947             }
2948         }
2949         upgrade_main_savepoint(true, 2010033102.06);
2950     }
2952     if ($oldversion < 2010033102.07) {
2953         if (!empty($CFG->displayloginfailures) and $CFG->displayloginfailures === 'teacher') {
2954             upgrade_log(UPGRADE_LOG_NOTICE, null, 'Displaying of login failuters to teachers is not supported any more.');
2955             unset_config('displayloginfailures');
2956         }
2957         upgrade_main_savepoint(true, 2010033102.07);
2958     }
2960     if ($oldversion < 2010033102.08) {
2961         // make sure there are no problems in default guest role settings
2962         if (!empty($CFG->guestroleid)) {
2963             if ($role = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
2964                 if ($role->archetype !== '' and $role->archetype !== 'guest') {
2965                     upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default guest role (guestroleid) value is invalid, setting cleared.');
2966                     unset_config('guestroleid');
2967                 }
2968             } else {
2969                 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) does not exist, setting cleared.');
2970                 unset_config('guestroleid');
2971             }
2972         }
2973         // remove all roles of the guest account - the only way to change it is to override the guest role, sorry
2974         // the guest account gets all the role assignments on the fly which works fine in has_capability(),
2975         $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE username = 'guest')");
2977         upgrade_main_savepoint(true, 2010033102.08);
2978     }
2980     /// New table for storing which roles can be assigned in which contexts.
2981     if ($oldversion < 2010033102.09) {
2983     /// Define table role_context_levels to be created
2984         $table = new xmldb_table('role_context_levels');
2986     /// Adding fields to table role_context_levels
2987         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2988         $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2989         $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2991     /// Adding keys to table role_context_levels
2992         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2993         $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
2994         $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
2996     /// Conditionally launch create table for role_context_levels
2997         if (!$dbman->table_exists($table)) {
2998             $dbman->create_table($table);
2999         }
3001     /// Main savepoint reached
3002         upgrade_main_savepoint(true, 2010033102.09);
3003     }
3005     if ($oldversion < 2010033102.10) {
3006         // Now populate the role_context_levels table with the default values
3007         // NOTE: do not use accesslib methods here
3009         $rolecontextlevels = array();
3010         $defaults = array('manager'        => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE),
3011                           'coursecreator'  => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT),
3012                           'editingteacher' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3013                           'teacher'        => array(CONTEXT_COURSE, CONTEXT_MODULE),
3014                           'student'        => array(CONTEXT_COURSE, CONTEXT_MODULE),
3015                           'guest'          => array(),
3016                           'user'           => array(),
3017                           'frontpage'      => array());
3019         $roles = $DB->get_records('role', array(), '', 'id, archetype');
3020         foreach ($roles as $role) {
3021             if (isset($defaults[$role->archetype])) {
3022                 $rolecontextlevels[$role->id] = $defaults[$role->archetype];
3023             }
3024         }
3026         // add roles without archetypes, it may contain weird things, but we can not fix them
3027         list($narsql, $params) = $DB->get_in_or_equal(array_keys($defaults), SQL_PARAMS_NAMED, 'ar000', false);
3028         $sql = "SELECT DISTINCT ra.roleid, con.contextlevel
3029                   FROM {role_assignments} ra
3030                   JOIN {context} con ON ra.contextid = con.id
3031                   JOIN {role} r ON r.id = ra.roleid
3032                  WHERE r.archetype $narsql";
3033         $existingrolecontextlevels = $DB->get_recordset_sql($sql, $params);
3034         foreach ($existingrolecontextlevels as $rcl) {
3035             if (!isset($rolecontextlevels[$rcl->roleid])) {
3036                 $rolecontextlevels[$rcl->roleid] = array();
3037             }
3038             $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
3039         }
3040         $existingrolecontextlevels->close();
3042         // Put the data into the database.
3043         $rcl = new stdClass();
3044         foreach ($rolecontextlevels as $roleid => $contextlevels) {
3045             $rcl->roleid = $roleid;
3046             foreach ($contextlevels as $level) {
3047                 $rcl->contextlevel = $level;
3048                 $DB->insert_record('role_context_levels', $rcl, false);
3049             }
3050         }
3052         // release memory!!
3053         unset($roles);
3054         unset($defaults);
3055         unset($rcl);
3056         unset($existingrolecontextlevels);
3057         unset($rolecontextlevels);
3059         // Main savepoint reached
3060         upgrade_main_savepoint(true, 2010033102.10);
3061     }
3063     if ($oldversion < 2010040700) {
3064         // migrate old groupings --> groupmembersonly setting
3065         if (isset($CFG->enablegroupings)) {
3066             set_config('enablegroupmembersonly', $CFG->enablegroupings);
3067             unset_config('enablegroupings');
3068         }
3070         // Main savepoint reached
3071         upgrade_main_savepoint(true, 2010040700);
3072     }
3074     if ($oldversion < 2010040900) {
3076         // Changing the default of field lang on table user to good old "en"
3077         $table = new xmldb_table('user');
3078         $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en', 'country');
3080         // Launch change of default for field lang
3081         $dbman->change_field_default($table, $field);
3083         // update main site lang
3084         if (strpos($CFG->lang, '_utf8') !== false) {
3085             $lang = str_replace('_utf8', '', $CFG->lang);
3086             set_config('lang', $lang);
3087         }
3089         // tweak langlist
3090         if (!empty($CFG->langlist)) {
3091             $langs = explode(',', $CFG->langlist);
3092             foreach ($langs as $key=>$lang) {
3093                 $lang = str_replace('_utf8', '', $lang);
3094                 $langs[$key] = $lang;
3095             }
3096             set_config('langlist', implode(',', $langs));
3097         }
3099         // Main savepoint reached
3100         upgrade_main_savepoint(true, 2010040900);
3101     }
3103     if ($oldversion < 2010040901) {
3105         // Remove "_utf8" suffix from all langs in user table
3106         $langs = $DB->get_records_sql("SELECT DISTINCT lang FROM {user} WHERE lang LIKE ?", array('%_utf8'));
3108         foreach ($langs as $lang=>$unused) {
3109             $newlang = str_replace('_utf8', '', $lang);
3110             $sql = "UPDATE {user} SET lang = :newlang WHERE lang = :lang";
3111             $DB->execute($sql, array('newlang'=>$newlang, 'lang'=>$lang));
3112         }
3114         // Main savepoint reached
3115         upgrade_main_savepoint(true, 2010040901);
3116     }
3118     if ($oldversion < 2010041301) {
3119         $sql = "UPDATE {block} SET name=? WHERE name=?";
3120         $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3121         $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3123         $sql = "UPDATE {block_instances} SET blockname=? WHERE blockname=?";
3124         $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3125         $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3126         upgrade_main_savepoint(true, 2010041301);
3127     }
3129     if ($oldversion < 2010042100) {
3131     /// Define table backup_controllers to be created
3132         $table = new xmldb_table('backup_controllers');
3134     /// Adding fields to table backup_controllers
3135         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3136         $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3137         $table->add_field('type', XMLDB_TYPE_CHAR, '6', null, XMLDB_NOTNULL, null, null);
3138         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3139         $table->add_field('format', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
3140         $table->add_field('interactive', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3141         $table->add_field('purpose', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3142         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3143         $table->add_field('status', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3144         $table->add_field('execution', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3145         $table->add_field('executiontime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3146         $table->add_field('checksum', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3147         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3148         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3149         $table->add_field('controller', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
3151     /// Adding keys to table backup_controllers
3152         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3153         $table->add_key('backupid_uk', XMLDB_KEY_UNIQUE, array('backupid'));
3154         $table->add_key('userid_fk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3156     /// Adding indexes to table backup_controllers
3157         $table->add_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3159     /// Conditionally launch create table for backup_controllers
3160         if (!$dbman->table_exists($table)) {
3161             $dbman->create_table($table);
3162         }
3164     /// Define table backup_ids_template to be created
3165         $table = new xmldb_table('backup_ids_template');
3167     /// Adding fields to table backup_ids_template
3168         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3169         $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3170         $table->add_field('itemname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
3171         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3172         $table->add_field('parentitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3174     /// Adding keys to table backup_ids_template
3175         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3176         $table->add_key('backupid_itemname_itemid_uk', XMLDB_KEY_UNIQUE, array('backupid', 'itemname', 'itemid'));
3178     /// Adding indexes to table backup_ids_template
3179         $table->add_index('backupid_parentitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'parentitemid'));
3181     /// Conditionally launch create table for backup_controllers
3182         if (!$dbman->table_exists($table)) {
3183             $dbman->create_table($table);
3184         }
3186     /// Main savepoint reached
3187         upgrade_main_savepoint(true, 2010042100);
3188     }
3190     if ($oldversion < 2010042301) {
3192         $table = new xmldb_table('course_sections');
3193         $field = new xmldb_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'section');
3195         if (!$dbman->field_exists($table, $field)) {
3196             $dbman->add_field($table, $field);
3197         }
3199         upgrade_main_savepoint(true, 2010042301);
3200     }
3202     if ($oldversion < 2010042302) {
3203         // Define table cohort to be created
3204         $table = new xmldb_table('cohort');
3206         // Adding fields to table cohort
3207         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3208         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3209         $table->add_field('name', XMLDB_TYPE_CHAR, '254', null, XMLDB_NOTNULL, null, null);
3210         $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3211         $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
3212         $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3213         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
3214         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3215         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3217         // Adding keys to table cohort
3218         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3219         $table->add_key('context', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3221         // Conditionally launch create table for cohort
3222         if (!$dbman->table_exists($table)) {
3223             $dbman->create_table($table);
3224         }
3226         upgrade_main_savepoint(true, 2010042302);
3227     }
3229     if ($oldversion < 2010042303) {
3230         // Define table cohort_members to be created
3231         $table = new xmldb_table('cohort_members');
3233         // Adding fields to table cohort_members
3234         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3235         $table->add_field('cohortid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3236         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3237         $table->add_field('timeadded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3239         // Adding keys to table cohort_members
3240         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3241         $table->add_key('cohortid', XMLDB_KEY_FOREIGN, array('cohortid'), 'cohort', array('id'));
3242         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3244         // Adding indexes to table cohort_members
3245         $table->add_index('cohortid-userid', XMLDB_INDEX_UNIQUE, array('cohortid', 'userid'));
3247         // Conditionally launch create table for cohort_members
3248         if (!$dbman->table_exists($table)) {
3249             $dbman->create_table($table);
3250         }
3252         // Main savepoint reached
3253         upgrade_main_savepoint(true, 2010042303);
3254     }
3256     if ($oldversion < 2010042800) {
3257         //drop the previously created ratings table
3258         $table = new xmldb_table('ratings');
3259         if ($dbman->table_exists($table)) {
3260             $dbman->drop_table($table);
3261         }
3263         //create the rating table (replaces module specific rating implementations)
3264         $table = new xmldb_table('rating');
3265         if ($dbman->table_exists($table)) {
3266             $dbman->drop_table($table);
3267         }
3269     /// Adding fields to table rating
3270         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3271         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3273         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3274         $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
3275         $table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3276         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3278         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3279         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3281     /// Adding keys to table rating
3282         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3283         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3284         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3286     /// Adding indexes to table rating
3287         $table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
3289     /// Create table for ratings
3290         if (!$dbman->table_exists($table)) {
3291             $dbman->create_table($table);
3292         }
3294         upgrade_main_savepoint(true, 2010042800);
3295     }
3297     if ($oldversion < 2010042801) {
3298         // migrating old comments block content
3299         $DB->execute("UPDATE {comments}
3300                          SET contextid = (SELECT parentcontextid
3301                                             FROM {block_instances}
3302                                            WHERE id = {comments}.itemid AND blockname = 'comments'),
3303                              commentarea = 'page_comments',
3304                              itemid = 0
3305                        WHERE commentarea = 'block_comments'
3306                              AND itemid != 0
3307                              AND EXISTS (SELECT 'x'
3308                                            FROM {block_instances}
3309                                           WHERE id = {comments}.itemid
3310                                                 AND blockname = 'comments')");
3312         // remove all orphaned record
3313         $DB->delete_records('comments', array('commentarea'=>'block_comments'));
3314         upgrade_main_savepoint(true, 2010042801);
3315     }
3317     if ($oldversion < 2010042802) { // Change backup_controllers->type to varchar10 (recreate dep. index)
3319     /// Define index typeitem_ix (not unique) to be dropped form backup_controllers
3320         $table = new xmldb_table('backup_controllers');
3321         $index = new xmldb_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3323     /// Conditionally launch drop index typeitem_ix
3324         if ($dbman->index_exists($table, $index)) {
3325             $dbman->drop_index($table, $index);
3326         }
3328     /// Changing precision of field type on table backup_controllers to (10)
3329         $table = new xmldb_table('backup_controllers');
3330         $field = new xmldb_field('type', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'backupid');
3332     /// Launch change of precision for field type
3333         $dbman->change_field_precision($table, $field);
3335     /// Define index typeitem_ix (not unique) to be added to backup_controllers
3336         $table = new xmldb_table('backup_controllers');
3337         $index = new xmldb_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3339     /// Conditionally launch add index typeitem_ix
3340         if (!$dbman->index_exists($table, $index)) {
3341             $dbman->add_index($table, $index);
3342         }
3344     /// Main savepoint reached
3345         upgrade_main_savepoint(true, 2010042802);
3346     }
3348     if ($oldversion < 2010043000) {  // Adding new course completion feature
3350     /// Add course completion tables
3351     /// Define table course_completion_aggr_methd to be created
3352         $table = new xmldb_table('course_completion_aggr_methd');
3354     /// Adding fields to table course_completion_aggr_methd
3355         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3356         $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3357         $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, null);
3358         $table->add_field('method', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3359         $table->add_field('value', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3361     /// Adding keys to table course_completion_aggr_methd
3362         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3364     /// Adding indexes to table course_completion_aggr_methd
3365         $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3366         $table->add_index('criteriatype', XMLDB_INDEX_NOTUNIQUE, array('criteriatype'));
3368     /// Conditionally launch create table for course_completion_aggr_methd
3369         if (!$dbman->table_exists($table)) {
3370             $dbman->create_table($table);
3371         }
3374     /// Define table course_completion_criteria to be created
3375         $table = new xmldb_table('course_completion_criteria');
3377     /// Adding fields to table course_completion_criteria
3378         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3379         $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3380         $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3381         $table->add_field('module', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3382         $table->add_field('moduleinstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3383         $table->add_field('courseinstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3384         $table->add_field('enrolperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3385         $table->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3386         $table->add_field('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3387         $table->add_field('role', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3389     /// Adding keys to table course_completion_criteria
3390         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3392     /// Adding indexes to table course_completion_criteria
3393         $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3395     /// Conditionally launch create table for course_completion_criteria
3396         if (!$dbman->table_exists($table)) {
3397             $dbman->create_table($table);
3398         }
3401     /// Define table course_completion_crit_compl to be created
3402         $table = new xmldb_table('course_completion_crit_compl');
3404     /// Adding fields to table course_completion_crit_compl
3405         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3406         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3407         $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3408         $table->add_field('criteriaid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3409         $table->add_field('gradefinal', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3410         $table->add_field('unenroled', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3411         $table->add_field('deleted', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);