eaa12d32129437ec3f8a726476fe34130eba0872
[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('targetversion', XMLDB_TYPE_CHAR, '100', null, null, null, null);
78         $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
79         $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
80         $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
81         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
82         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
84     /// Adding keys to table upgrade_log
85         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
86         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
88     /// Adding indexes to table upgrade_log
89         $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
90         $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
92     /// Create table for upgrade_log
93         $dbman->create_table($table);
95     /// Main savepoint reached
96         upgrade_main_savepoint(true, 2008030600);
97     }
99     if ($oldversion < 2008030601) {
100         //NOTE: this table was added much later later in dev cycle, but we need it here, upgrades from pre PR1 not supported
102     /// Define table log_queries to be created
103         $table = new xmldb_table('log_queries');
105     /// Adding fields to table log_queries
106         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
107         $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
108         $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
109         $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
110         $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
111         $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
112         $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
113         $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
114         $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
116     /// Adding keys to table log_queries
117         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
119     /// Conditionally launch create table for log_queries
120         $dbman->create_table($table);
122     /// Main savepoint reached
123         upgrade_main_savepoint(true, 2008030601);
124     }
126     if ($oldversion < 2008030602) {
127         @unlink($CFG->dataroot.'/cache/languages');
129         if (file_exists("$CFG->dataroot/lang")) {
130             // rename old lang directory so that the new and old langs do not mix
131             if (rename("$CFG->dataroot/lang", "$CFG->dataroot/oldlang")) {
132                 $oldlang = "$CFG->dataroot/oldlang";
133             } else {
134                 $oldlang = "$CFG->dataroot/lang";
135             }
136         } else {
137             $oldlang = '';
138         }
139         // TODO: fetch previously installed languages ("*_utf8") found in $oldlang from moodle.org
140         upgrade_set_timeout(60*20); // this may take a while
143         // TODO: add some info file to $oldlang describing what to do with "$oldlang/*_utf8_local" dirs
146         // Main savepoint reached
147         upgrade_main_savepoint(true, 2008030602);
148     }
150     if ($oldversion < 2008030700) {
151         upgrade_set_timeout(60*20); // this may take a while
153     /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
154         $table = new xmldb_table('grade_letters');
155         $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
157     /// Launch drop index contextid-lowerboundary
158         if ($dbman->index_exists($table, $index)) {
159             $dbman->drop_index($table, $index);
160         }
162     /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
163         $table = new xmldb_table('grade_letters');
164         $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
166     /// Launch add index contextid-lowerboundary-letter
167         $dbman->add_index($table, $index);
169     /// Main savepoint reached
170         upgrade_main_savepoint(true, 2008030700);
171     }
173     if ($oldversion < 2008050100) {
174         // Update courses that used weekscss to weeks
175         $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
176         upgrade_main_savepoint(true, 2008050100);
177     }
179     if ($oldversion < 2008050200) {
180         // remove unused config options
181         unset_config('statsrolesupgraded');
182         upgrade_main_savepoint(true, 2008050200);
183     }
185     if ($oldversion < 2008050700) {
186         upgrade_set_timeout(60*20); // this may take a while
188     /// Fix minor problem caused by MDL-5482.
189         require_once($CFG->dirroot . '/question/upgrade.php');
190         question_fix_random_question_parents();
191         upgrade_main_savepoint(true, 2008050700);
192     }
194     if ($oldversion < 2008051201) {
195         echo $OUTPUT->notification('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
196         upgrade_set_timeout(60*20); // this may take a while
198     /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
199         $dbfamily = $DB->get_dbfamily();
200         if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
201             $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
202         }
204     /// Define index idnumber (not unique) to be dropped form user
205         $table = new xmldb_table('user');
206         $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
208     /// Launch drop index idnumber
209         if ($dbman->index_exists($table, $index)) {
210             $dbman->drop_index($table, $index);
211         }
213     /// Changing precision of field idnumber on table user to (255)
214         $table = new xmldb_table('user');
215         $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
217     /// Launch change of precision for field idnumber
218         $dbman->change_field_precision($table, $field);
220     /// Launch add index idnumber again
221         $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
222         $dbman->add_index($table, $index);
224     /// Main savepoint reached
225         upgrade_main_savepoint(true, 2008051201);
226     }
228     if ($oldversion < 2008051203) {
229         $table = new xmldb_table('mnet_enrol_course');
230         $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
231         $dbman->change_field_precision($table, $field);
232         upgrade_main_savepoint(true, 2008051203);
233     }
235     if ($oldversion < 2008063001) {
236         upgrade_set_timeout(60*20); // this may take a while
238         // table to be modified
239         $table = new xmldb_table('tag_instance');
240         // add field
241         $field = new xmldb_field('tiuserid');
242         if (!$dbman->field_exists($table, $field)) {
243             $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
244             $dbman->add_field($table, $field);
245         }
246         // modify index
247         $index = new xmldb_index('itemtype-itemid-tagid');
248         $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
249         if ($dbman->index_exists($table, $index)) {
250             $dbman->drop_index($table, $index);
251         }
252         $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
253         $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
254         if (!$dbman->index_exists($table, $index)) {
255             $dbman->add_index($table, $index);
256         }
258         /// Main savepoint reached
259         upgrade_main_savepoint(true, 2008063001);
260     }
262     if ($oldversion < 2008070300) {
263         $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
264         upgrade_main_savepoint(true, 2008070300);
265     }
267     if ($oldversion < 2008070701) {
269     /// Define table portfolio_instance to be created
270         $table = new xmldb_table('portfolio_instance');
272     /// Adding fields to table portfolio_instance
273         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
274         $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
275         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
276         $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
278     /// Adding keys to table portfolio_instance
279         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
281     /// Conditionally launch create table for portfolio_instance
282         if (!$dbman->table_exists($table)) {
283             $dbman->create_table($table);
284         }
285   /// Define table portfolio_instance_config to be created
286         $table = new xmldb_table('portfolio_instance_config');
288     /// Adding fields to table portfolio_instance_config
289         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
290         $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
291         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
292         $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
294     /// Adding keys to table portfolio_instance_config
295         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
296         $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
298     /// Adding indexes to table portfolio_instance_config
299         $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
301     /// Conditionally launch create table for portfolio_instance_config
302         if (!$dbman->table_exists($table)) {
303             $dbman->create_table($table);
304         }
306    /// Define table portfolio_instance_user to be created
307         $table = new xmldb_table('portfolio_instance_user');
309     /// Adding fields to table portfolio_instance_user
310         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
311         $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
312         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
313         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
314         $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
316     /// Adding keys to table portfolio_instance_user
317         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
318         $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
319         $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
321     /// Conditionally launch create table for portfolio_instance_user
322         if (!$dbman->table_exists($table)) {
323             $dbman->create_table($table);
324         }
326     /// Main savepoint reached
327         upgrade_main_savepoint(true, 2008070701);
328     }
330     if ($oldversion < 2008072400) {
331     /// Create the database tables for message_processors
332         $table = new xmldb_table('message_processors');
333         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
334         $table->add_field('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null);
335         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
336         $dbman->create_table($table);
338     /// delete old and create new fields
339         $table = new xmldb_table('message');
340         $field = new xmldb_field('messagetype');
341         $dbman->drop_field($table, $field);
343     /// fields to rename
344         $field = new xmldb_field('message');
345         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
346         $dbman->rename_field($table, $field, 'fullmessage');
347         $field = new xmldb_field('format');
348         $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
349         $dbman->rename_field($table, $field, 'fullmessageformat');
351     /// new message fields
352         $field = new xmldb_field('subject');
353         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
354         $dbman->add_field($table, $field);
355         $field = new xmldb_field('fullmessagehtml');
356         $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
357         $dbman->add_field($table, $field);
358         $field = new xmldb_field('smallmessage');
359         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
360         $dbman->add_field($table, $field);
363         $table = new xmldb_table('message_read');
364         $field = new xmldb_field('messagetype');
365         $dbman->drop_field($table, $field);
366         $field = new xmldb_field('mailed');
367         $dbman->drop_field($table, $field);
369     /// fields to rename
370         $field = new xmldb_field('message');
371         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
372         $dbman->rename_field($table, $field, 'fullmessage');
373         $field = new xmldb_field('format');
374         $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
375         $dbman->rename_field($table, $field, 'fullmessageformat');
378     /// new message fields
379         $field = new xmldb_field('subject');
380         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
381         $dbman->add_field($table, $field);
382         $field = new xmldb_field('fullmessagehtml');
383         $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
384         $dbman->add_field($table, $field);
385         $field = new xmldb_field('smallmessage');
386         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
387         $dbman->add_field($table, $field);
389     /// new table
390         $table = new xmldb_table('message_working');
391         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
392         $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
393         $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
394         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
395         $dbman->create_table($table);
398         upgrade_main_savepoint(true, 2008072400);
399     }
401     if ($oldversion < 2008072800) {
403     /// Define field enablecompletion to be added to course
404         $table = new xmldb_table('course');
405         $field = new xmldb_field('enablecompletion');
406         $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
408     /// Launch add field enablecompletion
409         if (!$dbman->field_exists($table,$field)) {
410             $dbman->add_field($table, $field);
411         }
413     /// Define field completion to be added to course_modules
414         $table = new xmldb_table('course_modules');
415         $field = new xmldb_field('completion');
416         $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'groupmembersonly');
418     /// Launch add field completion
419         if (!$dbman->field_exists($table,$field)) {
420             $dbman->add_field($table, $field);
421         }
423     /// Define field completiongradeitemnumber to be added to course_modules
424         $field = new xmldb_field('completiongradeitemnumber');
425         $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'completion');
427     /// Launch add field completiongradeitemnumber
428         if (!$dbman->field_exists($table,$field)) {
429             $dbman->add_field($table, $field);
430         }
432     /// Define field completionview to be added to course_modules
433         $field = new xmldb_field('completionview');
434         $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiongradeitemnumber');
436     /// Launch add field completionview
437         if (!$dbman->field_exists($table,$field)) {
438             $dbman->add_field($table, $field);
439         }
441     /// Define field completionexpected to be added to course_modules
442         $field = new xmldb_field('completionexpected');
443         $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionview');
445     /// Launch add field completionexpected
446         if (!$dbman->field_exists($table,$field)) {
447             $dbman->add_field($table, $field);
448         }
450    /// Define table course_modules_completion to be created
451         $table = new xmldb_table('course_modules_completion');
452         if (!$dbman->table_exists($table)) {
454         /// Adding fields to table course_modules_completion
455             $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
456             $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
457             $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
458             $table->add_field('completionstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
459             $table->add_field('viewed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
460             $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
462         /// Adding keys to table course_modules_completion
463             $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
465         /// Adding indexes to table course_modules_completion
466             $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
467             $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
469         /// Launch create table for course_modules_completion
470             $dbman->create_table($table);
471         }
473         /// Main savepoint reached
474         upgrade_main_savepoint(true, 2008072800);
475     }
477     if ($oldversion < 2008073000) {
479     /// Define table portfolio_log to be created
480         $table = new xmldb_table('portfolio_log');
482     /// Adding fields to table portfolio_log
483         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
484         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
485         $table->add_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
486         $table->add_field('portfolio', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
487         $table->add_field('caller_class', XMLDB_TYPE_CHAR, '150', null, XMLDB_NOTNULL, null, null);
488         $table->add_field('caller_file', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
489         $table->add_field('caller_sha1', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
490         $table->add_field('tempdataid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
491         $table->add_field('returnurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
492         $table->add_field('continueurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
494     /// Adding keys to table portfolio_log
495         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
496         $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
497         $table->add_key('portfoliofk', XMLDB_KEY_FOREIGN, array('portfolio'), 'portfolio_instance', array('id'));
499     /// Conditionally launch create table for portfolio_log
500         if (!$dbman->table_exists($table)) {
501             $dbman->create_table($table);
502         }
504     /// Main savepoint reached
505         upgrade_main_savepoint(true, 2008073000);
506     }
508     if ($oldversion < 2008073104) {
509     /// Drop old table that might exist for some people
510         $table = new xmldb_table('message_providers');
511         if ($dbman->table_exists($table)) {
512             $dbman->drop_table($table);
513         }
515     /// Define table message_providers to be created
516         $table = new xmldb_table('message_providers');
518     /// Adding fields to table message_providers
519         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
520         $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
521         $table->add_field('component', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
522         $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
524     /// Adding keys to table message_providers
525         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
527     /// Adding indexes to table message_providers
528         $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
530     /// Create table for message_providers
531         $dbman->create_table($table);
533         upgrade_main_savepoint(true, 2008073104);
534     }
536     if ($oldversion < 2008073111) {
537     /// Define table files to be created
538         $table = new xmldb_table('files');
540     /// Adding fields to table files
541         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
542         $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
543         $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
544         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
545         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
546         $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
547         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
548         $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
549         $table->add_field('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
550         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
551         $table->add_field('filesize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
552         $table->add_field('mimetype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
553         $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
554         $table->add_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
555         $table->add_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null);
556         $table->add_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null);
557         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
558         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
560     /// Adding keys to table files
561         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
562         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
563         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
565     /// Adding indexes to table files
566         $table->add_index('component-filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('component', 'filearea', 'contextid', 'itemid'));
567         $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
568         $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
570     /// Conditionally launch create table for files
571         $dbman->create_table($table);
573     /// Main savepoint reached
574         upgrade_main_savepoint(true, 2008073111);
575     }
577     if ($oldversion < 2008073112) {
578         // Define field legacyfiles to be added to course
579         $table = new xmldb_table('course');
580         $field = new xmldb_field('legacyfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'maxbytes');
582         // Launch add field legacyfiles
583         $dbman->add_field($table, $field);
584         // enable legacy files in all courses
585         $DB->execute("UPDATE {course} SET legacyfiles = 2");
587         // Main savepoint reached
588         upgrade_main_savepoint(true, 2008073112);
589     }
591     if ($oldversion < 2008073113) {
592     /// move all course, backup and other files to new filepool based storage
593         upgrade_migrate_files_courses();
594     /// Main savepoint reached
595         upgrade_main_savepoint(true, 2008073113);
596     }
598     if ($oldversion < 2008073114) {
599     /// move all course, backup and other files to new filepool based storage
600         upgrade_migrate_files_blog();
601     /// Main savepoint reached
602         upgrade_main_savepoint(true, 2008073114);
603     }
605     if ($oldversion < 2008080400) {
606         // Add field ssl_jump_url to mnet application, and populate existing default applications
607         $table = new xmldb_table('mnet_application');
608         $field = new xmldb_field('sso_jump_url');
609         if (!$dbman->field_exists($table, $field)) {
610             $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
611             $dbman->add_field($table, $field);
612             $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
613             $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
614         }
616         /// Main savepoint reached
617         upgrade_main_savepoint(true, 2008080400);
618     }
620     if ($oldversion < 2008080500) {
622     /// Define table portfolio_tempdata to be created
623         $table = new xmldb_table('portfolio_tempdata');
625     /// Adding fields to table portfolio_tempdata
626         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
627         $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
628         $table->add_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
629         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
630         $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
632     /// Adding keys to table portfolio_tempdata
633         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
634         $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
635         $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
637     /// Conditionally launch create table for portfolio_tempdata
638         if (!$dbman->table_exists($table)) {
639             $dbman->create_table($table);
640         }
642     /// Main savepoint reached
643         upgrade_main_savepoint(true, 2008080500);
644     }
646     if ($oldversion < 2008081500) {
647     /// Changing the type of all the columns that the question bank uses to store grades to be NUMBER(12, 7).
648         $table = new xmldb_table('question');
649         $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1.0000000', 'generalfeedback');
650         $dbman->change_field_type($table, $field);
651         upgrade_main_savepoint(true, 2008081500);
652     }
654     if ($oldversion < 2008081501) {
655         $table = new xmldb_table('question');
656         $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0.1000000', 'defaultgrade');
657         $dbman->change_field_type($table, $field);
658         upgrade_main_savepoint(true, 2008081501);
659     }
661     if ($oldversion < 2008081502) {
662         $table = new xmldb_table('question_answers');
663         $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'answer');
664         $dbman->change_field_type($table, $field);
665         upgrade_main_savepoint(true, 2008081502);
666     }
668     if ($oldversion < 2008081503) {
669         $table = new xmldb_table('question_sessions');
670         $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'newgraded');
671         $dbman->change_field_type($table, $field);
672         upgrade_main_savepoint(true, 2008081503);
673     }
675     if ($oldversion < 2008081504) {
676         $table = new xmldb_table('question_states');
677         $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'event');
678         $dbman->change_field_type($table, $field);
679         upgrade_main_savepoint(true, 2008081504);
680     }
682     if ($oldversion < 2008081505) {
683         $table = new xmldb_table('question_states');
684         $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'grade');
685         $dbman->change_field_type($table, $field);
686         upgrade_main_savepoint(true, 2008081505);
687     }
689     if ($oldversion < 2008081506) {
690         $table = new xmldb_table('question_states');
691         $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'raw_grade');
692         $dbman->change_field_type($table, $field);
693         upgrade_main_savepoint(true, 2008081506);
694     }
696     if ($oldversion < 2008081600) {
698     /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
699         unset_config('unicodedb');
701     /// Main savepoint reached
702         upgrade_main_savepoint(true, 2008081600);
703     }
705     if ($oldversion < 2008082602) {
707     /// Define table repository to be dropped
708         $table = new xmldb_table('repository');
710     /// Conditionally launch drop table for repository
711         if ($dbman->table_exists($table)) {
712             $dbman->drop_table($table);
713         }
715     /// Define table repository to be created
716         $table = new xmldb_table('repository');
718     /// Adding fields to table repository
719         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
720         $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
721         $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1');
722         $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
724     /// Adding keys to table repository
725         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
727     /// Conditionally launch create table for repository
728         if (!$dbman->table_exists($table)) {
729             $dbman->create_table($table);
730         }
732     /// Define table repository_instances to be created
733         $table = new xmldb_table('repository_instances');
735     /// Adding fields to table repository_instances
736         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
737         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
738         $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
739         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
740         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
741         $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null);
742         $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
743         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
744         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
745         $table->add_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
747     /// Adding keys to table repository_instances
748         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
750     /// Conditionally launch create table for repository_instances
751         if (!$dbman->table_exists($table)) {
752             $dbman->create_table($table);
753         }
755     /// Define table repository_instance_config to be created
756         $table = new xmldb_table('repository_instance_config');
758     /// Adding fields to table repository_instance_config
759         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
760         $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
761         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
762         $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
764     /// Adding keys to table repository_instance_config
765         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
767     /// Conditionally launch create table for repository_instance_config
768         if (!$dbman->table_exists($table)) {
769             $dbman->create_table($table);
770         }
772     /// Main savepoint reached
773         upgrade_main_savepoint(true, 2008082602);
774     }
776     if ($oldversion < 2008082700) {
777     /// Add a new column to the question sessions table to record whether a
778     /// question has been flagged.
780     /// Define field flagged to be added to question_sessions
781         $table = new xmldb_table('question_sessions');
782         $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
784     /// Conditionally launch add field flagged
785         if (!$dbman->field_exists($table, $field)) {
786             $dbman->add_field($table, $field);
787         }
789     /// Main savepoint reached
790         upgrade_main_savepoint(true, 2008082700);
791     }
793     if ($oldversion < 2008082900) {
795     /// Changing precision of field parent_type on table mnet_rpc to (20)
796         $table = new xmldb_table('mnet_rpc');
797         $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
799     /// Launch change of precision for field parent_type
800         $dbman->change_field_precision($table, $field);
802     /// Main savepoint reached
803         upgrade_main_savepoint(true, 2008082900);
804     }
806     // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
807     if ($oldversion < 2008091000) {
808         foreach (get_object_vars($CFG) as $name => $value) {
809             if (substr($name, strlen($name) - 8) !== '_version') {
810                 continue;
811             }
812             $pluginname = substr($name, 0, strlen($name) - 8);
813             if (!strpos($pluginname, '_')) {
814                 // Skip things like backup_version that don't contain an extra _
815                 continue;
816             }
817             if ($pluginname == 'enrol_ldap_version') {
818                 // Special case - this is something different from a plugin version number.
819                 continue;
820             }
821             if (!preg_match('/^\d{10}$/', $value)) {
822                 // Extra safety check, skip anything that does not look like a Moodle
823                 // version number (10 digits).
824                 continue;
825             }
826             set_config('version', $value, $pluginname);
827             unset_config($name);
828         }
829         upgrade_main_savepoint(true, 2008091000);
830     }
832     if ($oldversion < 2008092300) {
833         unset_config('editorspelling');
834         unset_config('editordictionary');
835     /// Main savepoint reached
836         upgrade_main_savepoint(true, 2008092300);
837     }
839     if ($oldversion < 2008101300) {
841         if (!get_config(NULL, 'statsruntimedays')) {
842             set_config('statsruntimedays', '31');
843         }
845     /// Main savepoint reached
846         upgrade_main_savepoint(true, 2008101300);
847     }
849     /// Drop the deprecated teacher, teachers, student and students columns from the course table.
850     if ($oldversion < 2008111200) {
851         $table = new xmldb_table('course');
853     /// Conditionally launch drop field teacher
854         $field = new xmldb_field('teacher');
855         if ($dbman->field_exists($table, $field)) {
856             $dbman->drop_field($table, $field);
857         }
859     /// Conditionally launch drop field teacher
860         $field = new xmldb_field('teachers');
861         if ($dbman->field_exists($table, $field)) {
862             $dbman->drop_field($table, $field);
863         }
865     /// Conditionally launch drop field teacher
866         $field = new xmldb_field('student');
867         if ($dbman->field_exists($table, $field)) {
868             $dbman->drop_field($table, $field);
869         }
871     /// Conditionally launch drop field teacher
872         $field = new xmldb_field('students');
873         if ($dbman->field_exists($table, $field)) {
874             $dbman->drop_field($table, $field);
875         }
877     /// Main savepoint reached
878         upgrade_main_savepoint(true, 2008111200);
879     }
881 /// Add a unique index to the role.name column.
882     if ($oldversion < 2008111800) {
884     /// Define index name (unique) to be added to role
885         $table = new xmldb_table('role');
886         $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
888     /// Conditionally launch add index name
889         if (!$dbman->index_exists($table, $index)) {
890             $dbman->add_index($table, $index);
891         }
893     /// Main savepoint reached
894         upgrade_main_savepoint(true, 2008111800);
895     }
897 /// Add a unique index to the role.shortname column.
898     if ($oldversion < 2008111801) {
900     /// Define index shortname (unique) to be added to role
901         $table = new xmldb_table('role');
902         $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
904     /// Conditionally launch add index shortname
905         if (!$dbman->index_exists($table, $index)) {
906             $dbman->add_index($table, $index);
907         }
909     /// Main savepoint reached
910         upgrade_main_savepoint(true, 2008111801);
911     }
913     if ($oldversion < 2008120700) {
915     /// Changing precision of field shortname on table course_request to (100)
916         $table = new xmldb_table('course_request');
917         $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
919     /// Before changing the field, drop dependent indexes
920     /// Define index shortname (not unique) to be dropped form course_request
921         $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
922     /// Conditionally launch drop index shortname
923         if ($dbman->index_exists($table, $index)) {
924             $dbman->drop_index($table, $index);
925         }
927     /// Launch change of precision for field shortname
928         $dbman->change_field_precision($table, $field);
930     /// After changing the field, recreate dependent indexes
931     /// Define index shortname (not unique) to be added to course_request
932         $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
933     /// Conditionally launch add index shortname
934         if (!$dbman->index_exists($table, $index)) {
935             $dbman->add_index($table, $index);
936         }
938     /// Main savepoint reached
939         upgrade_main_savepoint(true, 2008120700);
940     }
942     if ($oldversion < 2008120801) {
944     /// Changing precision of field shortname on table mnet_enrol_course to (100)
945         $table = new xmldb_table('mnet_enrol_course');
946         $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
948     /// Launch change of precision for field shortname
949         $dbman->change_field_precision($table, $field);
951     /// Main savepoint reached
952         upgrade_main_savepoint(true, 2008120801);
953     }
955     if ($oldversion < 2008121701) {
957     /// Define field availablefrom to be added to course_modules
958         $table = new xmldb_table('course_modules');
959         $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
961     /// Conditionally launch add field availablefrom
962         if (!$dbman->field_exists($table, $field)) {
963             $dbman->add_field($table, $field);
964         }
966     /// Define field availableuntil to be added to course_modules
967         $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
969     /// Conditionally launch add field availableuntil
970         if (!$dbman->field_exists($table, $field)) {
971             $dbman->add_field($table, $field);
972         }
974     /// Define field showavailability to be added to course_modules
975         $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
977     /// Conditionally launch add field showavailability
978         if (!$dbman->field_exists($table, $field)) {
979             $dbman->add_field($table, $field);
980         }
982     /// Define table course_modules_availability to be created
983         $table = new xmldb_table('course_modules_availability');
985     /// Adding fields to table course_modules_availability
986         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
987         $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
988         $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
989         $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
990         $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
991         $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
992         $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
994     /// Adding keys to table course_modules_availability
995         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
996         $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
997         $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
998         $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
1000     /// Conditionally launch create table for course_modules_availability
1001         if (!$dbman->table_exists($table)) {
1002             $dbman->create_table($table);
1003         }
1005     /// Changes to modinfo mean we need to rebuild course cache
1006         require_once($CFG->dirroot . '/course/lib.php');
1007         rebuild_course_cache(0, true);
1009     /// Main savepoint reached
1010         upgrade_main_savepoint(true, 2008121701);
1011     }
1013     if ($oldversion < 2009010500) {
1014     /// clean up config table a bit
1015         unset_config('session_error_counter');
1017     /// Main savepoint reached
1018         upgrade_main_savepoint(true, 2009010500);
1019     }
1021     if ($oldversion < 2009010600) {
1023     /// Define field originalquestion to be dropped from question_states
1024         $table = new xmldb_table('question_states');
1025         $field = new xmldb_field('originalquestion');
1027     /// Conditionally launch drop field originalquestion
1028         if ($dbman->field_exists($table, $field)) {
1029             $dbman->drop_field($table, $field);
1030         }
1032     /// Main savepoint reached
1033         upgrade_main_savepoint(true, 2009010600);
1034     }
1036     if ($oldversion < 2009010601) {
1038     /// Changing precision of field ip on table log to (45)
1039         $table = new xmldb_table('log');
1040         $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1042     /// Launch change of precision for field ip
1043         $dbman->change_field_precision($table, $field);
1045     /// Main savepoint reached
1046         upgrade_main_savepoint(true, 2009010601);
1047     }
1049     if ($oldversion < 2009010602) {
1051     /// Changing precision of field lastip on table user to (45)
1052         $table = new xmldb_table('user');
1053         $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
1055     /// Launch change of precision for field lastip
1056         $dbman->change_field_precision($table, $field);
1058     /// Main savepoint reached
1059         upgrade_main_savepoint(true, 2009010602);
1060     }
1062     if ($oldversion < 2009010603) {
1064     /// Changing precision of field ip_address on table mnet_host to (45)
1065         $table = new xmldb_table('mnet_host');
1066         $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
1068     /// Launch change of precision for field ip_address
1069         $dbman->change_field_precision($table, $field);
1071     /// Main savepoint reached
1072         upgrade_main_savepoint(true, 2009010603);
1073     }
1075     if ($oldversion < 2009010604) {
1077     /// Changing precision of field ip on table mnet_log to (45)
1078         $table = new xmldb_table('mnet_log');
1079         $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1081     /// Launch change of precision for field ip
1082         $dbman->change_field_precision($table, $field);
1084     /// Main savepoint reached
1085         upgrade_main_savepoint(true, 2009010604);
1086     }
1088     if ($oldversion < 2009011000) {
1090     /// Changing nullability of field configdata on table block_instance to null
1091         $table = new xmldb_table('block_instance');
1092         $field = new xmldb_field('configdata');
1093         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
1095     /// Launch change of nullability for field configdata
1096         $dbman->change_field_notnull($table, $field);
1098     /// Main savepoint reached
1099         upgrade_main_savepoint(true, 2009011000);
1100     }
1102     if ($oldversion < 2009011100) {
1103     /// Remove unused settings
1104         unset_config('zip');
1105         unset_config('unzip');
1106         unset_config('adminblocks_initialised');
1108     /// Main savepoint reached
1109         upgrade_main_savepoint(true, 2009011100);
1110     }
1112     if ($oldversion < 2009011101) {
1113     /// Migrate backup settings to core plugin config table
1114         $configs = $DB->get_records('backup_config');
1115         foreach ($configs as $config) {
1116             set_config($config->name, $config->value, 'backup');
1117         }
1119     /// Define table to be dropped
1120         $table = new xmldb_table('backup_config');
1122     /// Launch drop table for old backup config
1123         $dbman->drop_table($table);
1125     /// Main savepoint reached
1126         upgrade_main_savepoint(true, 2009011101);
1127     }
1129     if ($oldversion < 2009011303) {
1131     /// Define table config_log to be created
1132         $table = new xmldb_table('config_log');
1134     /// Adding fields to table config_log
1135         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1136         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1137         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1138         $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1139         $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1140         $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1141         $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1143     /// Adding keys to table config_log
1144         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1145         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1147     /// Adding indexes to table config_log
1148         $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1150     /// Launch create table for config_log
1151         $dbman->create_table($table);
1153     /// Main savepoint reached
1154         upgrade_main_savepoint(true, 2009011303);
1155     }
1157     if ($oldversion < 2009011900) {
1159     /// Define table sessions2 to be dropped
1160         $table = new xmldb_table('sessions2');
1162     /// Conditionally launch drop table for sessions
1163         if ($dbman->table_exists($table)) {
1164             $dbman->drop_table($table);
1165         }
1167     /// Define table sessions to be dropped
1168         $table = new xmldb_table('sessions');
1170     /// Conditionally launch drop table for sessions
1171         if ($dbman->table_exists($table)) {
1172             $dbman->drop_table($table);
1173         }
1175     /// Define table sessions to be created
1176         $table = new xmldb_table('sessions');
1178     /// Adding fields to table sessions
1179         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1180         $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1181         $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
1182         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1183         $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
1184         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1185         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1186         $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1187         $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1189     /// Adding keys to table sessions
1190         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1191         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1193     /// Adding indexes to table sessions
1194         $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1195         $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1196         $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1197         $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1199     /// Launch create table for sessions
1200         $dbman->create_table($table);
1202     /// Main savepoint reached
1203         upgrade_main_savepoint(true, 2009011900);
1204     }
1206     if ($oldversion < 2009021800) {
1207         // Converting format of grade conditions, if any exist, to percentages.
1208         $DB->execute("
1209 UPDATE {course_modules_availability} SET grademin=(
1210     SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
1211         /(gi.grademax-gi.grademin)
1212     FROM {grade_items} gi
1213     WHERE gi.id={course_modules_availability}.gradeitemid)
1214 WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1215         $DB->execute("
1216 UPDATE {course_modules_availability} SET grademax=(
1217     SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
1218         /(gi.grademax-gi.grademin)
1219     FROM {grade_items} gi
1220     WHERE gi.id={course_modules_availability}.gradeitemid)
1221 WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1223     /// Main savepoint reached
1224         upgrade_main_savepoint(true, 2009021800);
1225     }
1227     if ($oldversion < 2009021801) {
1228     /// Define field backuptype to be added to backup_log
1229         $table = new xmldb_table('backup_log');
1230         $field = new xmldb_field('backuptype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'info');
1231     /// Conditionally Launch add field backuptype and set all old records as 'scheduledbackup' records.
1232         if (!$dbman->field_exists($table, $field)) {
1233             // Set the default we want applied to any existing records
1234             $field->setDefault('scheduledbackup');
1235             // Add the field to the database
1236             $dbman->add_field($table, $field);
1237             // Remove the default
1238             $field->setDefault(null);
1239             // Update the database to remove the default
1240             $dbman->change_field_default($table, $field);
1241         }
1243     /// Main savepoint reached
1244         upgrade_main_savepoint(true, 2009021801);
1245     }
1247     /// Add default sort order for question types.
1248     if ($oldversion < 2009030300) {
1249         set_config('multichoice_sortorder', 1, 'question');
1250         set_config('truefalse_sortorder', 2, 'question');
1251         set_config('shortanswer_sortorder', 3, 'question');
1252         set_config('numerical_sortorder', 4, 'question');
1253         set_config('calculated_sortorder', 5, 'question');
1254         set_config('essay_sortorder', 6, 'question');
1255         set_config('match_sortorder', 7, 'question');
1256         set_config('randomsamatch_sortorder', 8, 'question');
1257         set_config('multianswer_sortorder', 9, 'question');
1258         set_config('description_sortorder', 10, 'question');
1259         set_config('random_sortorder', 11, 'question');
1260         set_config('missingtype_sortorder', 12, 'question');
1262         upgrade_main_savepoint(true, 2009030300);
1263     }
1265     /// MDL-18132 replace the use a new Role allow switch settings page, instead of
1266     /// $CFG->allowuserswitchrolestheycantassign
1267     if ($oldversion < 2009032000) {
1268     /// First create the new table.
1269             $table = new xmldb_table('role_allow_switch');
1271     /// Adding fields to table role_allow_switch
1272         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1273         $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1274         $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1276     /// Adding keys to table role_allow_switch
1277         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1278         $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
1279         $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
1281     /// Adding indexes to table role_allow_switch
1282         $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1284     /// Conditionally launch create table for role_allow_switch
1285         if (!$dbman->table_exists($table)) {
1286             $dbman->create_table($table);
1287         }
1289     /// Main savepoint reached
1290         upgrade_main_savepoint(true, 2009032000);
1291     }
1293     if ($oldversion < 2009032001) {
1294     /// Copy from role_allow_assign into the new table.
1295         $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1296                 SELECT roleid, allowassign FROM {role_allow_assign}');
1298     /// Unset the config variable used in 1.9.
1299         unset_config('allowuserswitchrolestheycantassign');
1301     /// Main savepoint reached
1302         upgrade_main_savepoint(true, 2009032001);
1303     }
1305     if ($oldversion < 2009040300) {
1307     /// Define table filter_active to be created
1308         $table = new xmldb_table('filter_active');
1310     /// Adding fields to table filter_active
1311         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1312         $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1313         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1314         $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1315         $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1317     /// Adding keys to table filter_active
1318         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1319         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1321     /// Adding indexes to table filter_active
1322         $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1324     /// Conditionally launch create table for filter_active
1325         if (!$dbman->table_exists($table)) {
1326             $dbman->create_table($table);
1327         }
1329     /// Main savepoint reached
1330         upgrade_main_savepoint(true, 2009040300);
1331     }
1333     if ($oldversion < 2009040301) {
1335     /// Define table filter_config to be created
1336         $table = new xmldb_table('filter_config');
1338     /// Adding fields to table filter_config
1339         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1340         $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1341         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1342         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1343         $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1345     /// Adding keys to table filter_config
1346         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1347         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1349     /// Adding indexes to table filter_config
1350         $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1352     /// Conditionally launch create table for filter_config
1353         if (!$dbman->table_exists($table)) {
1354             $dbman->create_table($table);
1355         }
1357     /// Main savepoint reached
1358         upgrade_main_savepoint(true, 2009040301);
1359     }
1361     if ($oldversion < 2009040302) {
1362     /// Transfer current settings from $CFG->textfilters
1363         $disabledfilters = filter_get_all_installed();
1364         if (empty($CFG->textfilters)) {
1365             $activefilters = array();
1366         } else {
1367             $activefilters = explode(',', $CFG->textfilters);
1368         }
1369         $syscontext = get_context_instance(CONTEXT_SYSTEM);
1370         $sortorder = 1;
1371         foreach ($activefilters as $filter) {
1372             filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1373             $sortorder += 1;
1374             unset($disabledfilters[$filter]);
1375         }
1376         foreach ($disabledfilters as $filter => $notused) {
1377             filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1378             $sortorder += 1;
1379         }
1381     /// Main savepoint reached
1382         upgrade_main_savepoint(true, 2009040302);
1383     }
1385     if ($oldversion < 2009040600) {
1386     /// Ensure that $CFG->stringfilters is set.
1387         if (empty($CFG->stringfilters)) {
1388             if (!empty($CFG->filterall)) {
1389                 set_config('stringfilters', $CFG->textfilters);
1390             } else {
1391                 set_config('stringfilters', '');
1392             }
1393         }
1395         set_config('filterall', !empty($CFG->stringfilters));
1396         unset_config('textfilters');
1398     /// Main savepoint reached
1399         upgrade_main_savepoint(true, 2009040600);
1400     }
1402     if ($oldversion < 2009041700) {
1403     /// To ensure the UI remains consistent with no behaviour change, any
1404     /// 'until' date in an activity condition should have 1 second subtracted
1405     /// (to go from 0:00 on the following day to 23:59 on the previous one).
1406         $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
1407         require_once($CFG->dirroot . '/course/lib.php');
1408         rebuild_course_cache(0, true);
1410     /// Main savepoint reached
1411         upgrade_main_savepoint(true, 2009041700);
1412     }
1414     if ($oldversion < 2009042600) {
1415     /// Deleting orphaned messages from deleted users.
1416         require_once($CFG->dirroot.'/message/lib.php');
1417     /// Detect deleted users with messages sent(useridfrom) and not read
1418         if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
1419                                                     FROM {user} u
1420                                                     JOIN {message} m ON m.useridfrom = u.id
1421                                                    WHERE u.deleted = ?', array(1))) {
1422             foreach ($deletedusers as $deleteduser) {
1423                 message_move_userfrom_unread2read($deleteduser->id); // move messages
1424             }
1425         }
1426     /// Main savepoint reached
1427         upgrade_main_savepoint(true, 2009042600);
1428     }
1430     /// Dropping all enums/check contraints from core. MDL-18577
1431     if ($oldversion < 2009042700) {
1433     /// Changing list of values (enum) of field stattype on table stats_daily to none
1434         $table = new xmldb_table('stats_daily');
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_weekly to none
1441         $table = new xmldb_table('stats_weekly');
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 stattype on table stats_monthly to none
1448         $table = new xmldb_table('stats_monthly');
1449         $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1451     /// Launch change of list of values for field stattype
1452         $dbman->drop_enum_from_field($table, $field);
1454     /// Changing list of values (enum) of field publishstate on table post to none
1455         $table = new xmldb_table('post');
1456         $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
1458     /// Launch change of list of values for field publishstate
1459         $dbman->drop_enum_from_field($table, $field);
1461     /// Main savepoint reached
1462         upgrade_main_savepoint(true, 2009042700);
1463     }
1465     if ($oldversion < 2009043000) {
1466         unset_config('grade_report_showgroups');
1467         upgrade_main_savepoint(true, 2009043000);
1468     }
1470     if ($oldversion < 2009050600) {
1471     /// Site front page blocks need to be moved due to page name change.
1472         $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
1474     /// Main savepoint reached
1475         upgrade_main_savepoint(true, 2009050600);
1476     }
1478     if ($oldversion < 2009050601) {
1480     /// Define table block_instance to be renamed to block_instances
1481         $table = new xmldb_table('block_instance');
1483     /// Launch rename table for block_instance
1484         $dbman->rename_table($table, 'block_instances');
1486     /// Main savepoint reached
1487         upgrade_main_savepoint(true, 2009050601);
1488     }
1490     if ($oldversion < 2009050602) {
1492     /// Define table block_instance to be renamed to block_instance_old
1493         $table = new xmldb_table('block_pinned');
1495     /// Launch rename table for block_instance
1496         $dbman->rename_table($table, 'block_pinned_old');
1498     /// Main savepoint reached
1499         upgrade_main_savepoint(true, 2009050602);
1500     }
1502     if ($oldversion < 2009050603) {
1504     /// Define table block_instance_old to be created
1505         $table = new xmldb_table('block_instance_old');
1507     /// Adding fields to table block_instance_old
1508         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1509         $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1510         $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1511         $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1512         $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
1513         $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
1514         $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
1515         $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1516         $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1518     /// Adding keys to table block_instance_old
1519         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1520         $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
1522     /// Adding indexes to table block_instance_old
1523         $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1524         $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
1526     /// Conditionally launch create table for block_instance_old
1527         if (!$dbman->table_exists($table)) {
1528             $dbman->create_table($table);
1529         }
1531     /// Main savepoint reached
1532         upgrade_main_savepoint(true, 2009050603);
1533     }
1535     if ($oldversion < 2009050604) {
1536     /// Copy current blocks data from block_instances to block_instance_old
1537         $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
1538             SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
1540         upgrade_main_savepoint(true, 2009050604);
1541     }
1543     if ($oldversion < 2009050605) {
1545     /// Define field multiple to be dropped from block
1546         $table = new xmldb_table('block');
1547         $field = new xmldb_field('multiple');
1549     /// Conditionally launch drop field multiple
1550         if ($dbman->field_exists($table, $field)) {
1551             $dbman->drop_field($table, $field);
1552         }
1554     /// Main savepoint reached
1555         upgrade_main_savepoint(true, 2009050605);
1556     }
1558     if ($oldversion < 2009050606) {
1559         $table = new xmldb_table('block_instances');
1561     /// Rename field weight on table block_instances to defaultweight
1562         $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, 'position');
1563         $dbman->rename_field($table, $field, 'defaultweight');
1565     /// Rename field position on table block_instances to defaultregion
1566         $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
1567         $dbman->rename_field($table, $field, 'defaultregion');
1569         /// Main savepoint reached
1570         upgrade_main_savepoint(true, 2009050606);
1571     }
1573     if ($oldversion < 2009050607) {
1574     /// Changing precision of field defaultregion on table block_instances to (16)
1575         $table = new xmldb_table('block_instances');
1576         $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
1578     /// Launch change of precision for field defaultregion
1579         $dbman->change_field_precision($table, $field);
1581     /// Main savepoint reached
1582         upgrade_main_savepoint(true, 2009050607);
1583     }
1585     if ($oldversion < 2009050608) {
1586     /// Change regions to the new notation
1587         $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1588         $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1589         $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1590         // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1591         // flex page course format. Hopefully this new value is an adequate alternative.
1593     /// Main savepoint reached
1594         upgrade_main_savepoint(true, 2009050608);
1595     }
1597     if ($oldversion < 2009050609) {
1599     /// Define key blockname (unique) to be added to block
1600         $table = new xmldb_table('block');
1601         $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1603     /// Launch add key blockname
1604         $dbman->add_key($table, $key);
1606     /// Main savepoint reached
1607         upgrade_main_savepoint(true, 2009050609);
1608     }
1610     if ($oldversion < 2009050610) {
1611         $table = new xmldb_table('block_instances');
1613     /// Define field blockname to be added to block_instances
1614         $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
1615         if (!$dbman->field_exists($table, $field)) {
1616             $dbman->add_field($table, $field);
1617         }
1619     /// Define field contextid to be added to block_instances
1620         $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
1621         if (!$dbman->field_exists($table, $field)) {
1622             $dbman->add_field($table, $field);
1623         }
1625     /// Define field showinsubcontexts to be added to block_instances
1626         $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
1627         if (!$dbman->field_exists($table, $field)) {
1628             $dbman->add_field($table, $field);
1629         }
1631     /// Define field subpagepattern to be added to block_instances
1632         $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
1633         if (!$dbman->field_exists($table, $field)) {
1634             $dbman->add_field($table, $field);
1635         }
1637     /// Main savepoint reached
1638         upgrade_main_savepoint(true, 2009050610);
1639     }
1641     if ($oldversion < 2009050611) {
1642         $table = new xmldb_table('block_instances');
1644     /// Fill in blockname from blockid
1645         $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1647     /// Set showinsubcontexts = 0 for all rows.
1648         $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1650     /// Main savepoint reached
1651         upgrade_main_savepoint(true, 2009050611);
1652     }
1654     if ($oldversion < 2009050612) {
1656     /// Rename field pagetype on table block_instances to pagetypepattern
1657         $table = new xmldb_table('block_instances');
1658         $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
1660     /// Launch rename field pagetype
1661         $dbman->rename_field($table, $field, 'pagetypepattern');
1663     /// Main savepoint reached
1664         upgrade_main_savepoint(true, 2009050612);
1665     }
1667     if ($oldversion < 2009050613) {
1668     /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1670     /// site-index
1671         $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
1672         $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1673                                                    pagetypepattern = 'site-index',
1674                                                    subpagepattern = NULL
1675                       WHERE pagetypepattern = 'site-index'");
1677     /// course-view
1678         $DB->execute("UPDATE {block_instances} SET
1679                         contextid = (
1680                             SELECT {context}.id
1681                             FROM {context}
1682                             JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1683                             WHERE {course}.id = pageid
1684                         ),
1685                        pagetypepattern = 'course-view-*',
1686                        subpagepattern = NULL
1687                       WHERE pagetypepattern = 'course-view'");
1689     /// admin
1690         $syscontext = get_context_instance(CONTEXT_SYSTEM);
1691         $DB->execute("UPDATE {block_instances} SET
1692                         contextid = " . $syscontext->id . ",
1693                         pagetypepattern = 'admin-*',
1694                         subpagepattern = NULL
1695                       WHERE pagetypepattern = 'admin'");
1697     /// my-index
1698         $DB->execute("UPDATE {block_instances} SET
1699                         contextid = (
1700                             SELECT {context}.id
1701                             FROM {context}
1702                             JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1703                             WHERE {user}.id = pageid
1704                         ),
1705                         pagetypepattern = 'my-index',
1706                         subpagepattern = NULL
1707                       WHERE pagetypepattern = 'my-index'");
1709     /// tag-index
1710         $DB->execute("UPDATE {block_instances} SET
1711                         contextid = " . $syscontext->id . ",
1712                         pagetypepattern = 'tag-index',
1713                         subpagepattern = pageid
1714                       WHERE pagetypepattern = 'tag-index'");
1716     /// blog-view
1717         $DB->execute("UPDATE {block_instances} SET
1718                         contextid = (
1719                             SELECT {context}.id
1720                             FROM {context}
1721                             JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1722                             WHERE {user}.id = pageid
1723                         ),
1724                         pagetypepattern = 'blog-index',
1725                         subpagepattern = NULL
1726                       WHERE pagetypepattern = 'blog-view'");
1728     /// mod-xxx-view
1729         $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1730         foreach ($moduleswithblocks as $modname) {
1731             if (!$dbman->table_exists($modname)) {
1732                 continue;
1733             }
1734             $DB->execute("UPDATE {block_instances} SET
1735                             contextid = (
1736                                 SELECT {context}.id
1737                                 FROM {context}
1738                                 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1739                                 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1740                                 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1741                                 WHERE {{$modname}}.id = pageid
1742                             ),
1743                             pagetypepattern = 'blog-index',
1744                             subpagepattern = NULL
1745                           WHERE pagetypepattern = 'blog-view'");
1746         }
1748     /// Main savepoint reached
1749         upgrade_main_savepoint(true, 2009050613);
1750     }
1752     if ($oldversion < 2009050614) {
1753     /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
1754         $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
1756     /// Main savepoint reached
1757         upgrade_main_savepoint(true, 2009050614);
1758     }
1760     if ($oldversion < 2009050615) {
1761         $table = new xmldb_table('block_instances');
1763     /// Arrived here, any block_instances record without blockname is one
1764     /// orphan block coming from 1.9. Just delete them. MDL-22503
1765         $DB->delete_records_select('block_instances', 'blockname IS NULL');
1767     /// Changing nullability of field blockname on table block_instances to not null
1768         $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
1769         $dbman->change_field_notnull($table, $field);
1771     /// Changing nullability of field contextid on table block_instances to not null
1772         $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
1773         $dbman->change_field_notnull($table, $field);
1775     /// Changing nullability of field showinsubcontexts on table block_instances to not null
1776         $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
1777         $dbman->change_field_notnull($table, $field);
1779     /// Main savepoint reached
1780         upgrade_main_savepoint(true, 2009050615);
1781     }
1783     if ($oldversion < 2009050616) {
1784     /// Add exiting sticky blocks.
1785         $blocks = $DB->get_records('block');
1786         $syscontext = get_context_instance(CONTEXT_SYSTEM);
1787         $newregions = array(
1788             'l' => 'side-pre',
1789             'r' => 'side-post',
1790             'c' => 'course-view-top',
1791         );
1792         $stickyblocks = $DB->get_recordset('block_pinned_old');
1793         foreach ($stickyblocks as $stickyblock) {
1794             // Only if the block exists (avoid orphaned sticky blocks)
1795             if (!isset($blocks[$stickyblock->blockid]) || empty($blocks[$stickyblock->blockid]->name)) {
1796                 continue;
1797             }
1798             $newblock = new stdClass();
1799             $newblock->blockname = $blocks[$stickyblock->blockid]->name;
1800             $newblock->contextid = $syscontext->id;
1801             $newblock->showinsubcontexts = 1;
1802             switch ($stickyblock->pagetype) {
1803                 case 'course-view':
1804                     $newblock->pagetypepattern = 'course-view-*';
1805                     break;
1806                 default:
1807                     $newblock->pagetypepattern = $stickyblock->pagetype;
1808             }
1809             $newblock->defaultregion = $newregions[$stickyblock->position];
1810             $newblock->defaultweight = $stickyblock->weight;
1811             $newblock->configdata = $stickyblock->configdata;
1812             $newblock->visible = 1;
1813             $DB->insert_record('block_instances', $newblock);
1814         }
1816     /// Main savepoint reached
1817         upgrade_main_savepoint(true, 2009050616);
1818     }
1820     if ($oldversion < 2009050617) {
1822     /// Define table block_positions to be created
1823         $table = new xmldb_table('block_positions');
1825     /// Adding fields to table block_positions
1826         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1827         $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1828         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1829         $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1830         $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1831         $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1832         $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1833         $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1835     /// Adding keys to table block_positions
1836         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1837         $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
1838         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1840     /// Adding indexes to table block_positions
1841         $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
1843     /// Conditionally launch create table for block_positions
1844         if (!$dbman->table_exists($table)) {
1845             $dbman->create_table($table);
1846         }
1848     /// Main savepoint reached
1849         upgrade_main_savepoint(true, 2009050617);
1850     }
1852     if ($oldversion < 2009050618) {
1853     /// And block instances with visible = 0, copy that information to block_positions
1854         $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
1855                 SELECT bi.id, bi.contextid,
1856                        CASE WHEN bi.pagetypepattern = 'course-view-*'
1857                            THEN (SELECT " . $DB->sql_concat("'course-view-'", 'c.format') . "
1858                                    FROM {course} c
1859                                    JOIN {context} ctx ON c.id = ctx.instanceid
1860                                   WHERE ctx.id = bi.contextid)
1861                            ELSE bi.pagetypepattern END,
1862                        CASE WHEN bi.subpagepattern IS NULL
1863                            THEN '" . $DB->sql_empty() . "'
1864                            ELSE bi.subpagepattern END,
1865                        0, bi.defaultregion, bi.defaultweight
1866                   FROM {block_instances} bi
1867                  WHERE bi.visible = 0 AND bi.pagetypepattern <> 'admin-*' AND bi.pagetypepattern IS NOT NULL");
1868         // note: MDL-25031 all block instances should have a pagetype pattern, NULL is not allowed,
1869         //       if we manage to find out how NULLs get there we should fix them before this step
1871     /// Main savepoint reached
1872         upgrade_main_savepoint(true, 2009050618);
1873     }
1875     if ($oldversion < 2009050619) {
1876         $table = new xmldb_table('block_instances');
1878     /// Define field blockid to be dropped from block_instances
1879         $field = new xmldb_field('blockid');
1880         if ($dbman->field_exists($table, $field)) {
1881         /// Before dropping the field, drop dependent indexes
1882             $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
1883             if ($dbman->index_exists($table, $index)) {
1884             /// Launch drop index blockid
1885                 $dbman->drop_index($table, $index);
1886             }
1887             $dbman->drop_field($table, $field);
1888         }
1890     /// Define field pageid to be dropped from block_instances
1891         $field = new xmldb_field('pageid');
1892         if ($dbman->field_exists($table, $field)) {
1893         /// Before dropping the field, drop dependent indexes
1894             $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1895             if ($dbman->index_exists($table, $index)) {
1896             /// Launch drop index pageid
1897                 $dbman->drop_index($table, $index);
1898             }
1899             $dbman->drop_field($table, $field);
1900         }
1902     /// Define field visible to be dropped from block_instances
1903         $field = new xmldb_field('visible');
1904         if ($dbman->field_exists($table, $field)) {
1905             $dbman->drop_field($table, $field);
1906         }
1908     /// Main savepoint reached
1909         upgrade_main_savepoint(true, 2009050619);
1910     }
1912     if ($oldversion < 2009051200) {
1913     /// Let's check the status of mandatory mnet_host records, fixing them
1914     /// and moving "orphan" users to default localhost record. MDL-16879
1915         echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
1916         upgrade_fix_incorrect_mnethostids();
1918     /// Main savepoint reached
1919         upgrade_main_savepoint(true, 2009051200);
1920     }
1923     if ($oldversion < 2009051700) {
1924     /// migrate editor settings
1925         if (empty($CFG->htmleditor)) {
1926             set_config('texteditors', 'textarea');
1927         } else {
1928             set_config('texteditors', 'tinymce,textarea');
1929         }
1931         unset_config('htmleditor');
1932         unset_config('defaulthtmleditor');
1934     /// Main savepoint reached
1935         upgrade_main_savepoint(true, 2009051700);
1936     }
1938     /// Repeat 2009050607 upgrade step, which Petr commented out because of XMLDB
1939     /// stupidity, so lots of people will have missed.
1940     if ($oldversion < 2009061600) {
1941     /// Changing precision of field defaultregion on table block_instances to (16)
1942         $table = new xmldb_table('block_instances');
1943         $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
1945     /// Launch change of precision for field defaultregion
1946         $dbman->change_field_precision($table, $field);
1948     /// Main savepoint reached
1949         upgrade_main_savepoint(true, 2009061600);
1950     }
1952     if ($oldversion < 2009061702) {
1953         // standardizing plugin names
1954         if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
1955             foreach ($configs as $config) {
1956                 unset_config($config->name, $config->plugin); /// unset old config
1957                 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
1958                 set_config($config->name, $config->value, $config->plugin); /// set new config
1959             }
1960         }
1961         unset($configs);
1962         upgrade_main_savepoint(true, 2009061702);
1963     }
1965     if ($oldversion < 2009061703) {
1966         // standardizing plugin names
1967         if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
1968             foreach ($configs as $config) {
1969                 unset_config($config->name, $config->plugin); /// unset old config
1970                 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
1971                 set_config($config->name, $config->value, $config->plugin); /// set new config
1972             }
1973         }
1974         unset($configs);
1975         upgrade_main_savepoint(true, 2009061703);
1976     }
1978     if ($oldversion < 2009061704) {
1979         // change component string in capability records to new "_" format
1980         if ($caps = $DB->get_records('capabilities')) {
1981             foreach ($caps as $cap) {
1982                 $cap->component = str_replace('/', '_', $cap->component);
1983                 $DB->update_record('capabilities', $cap);
1984             }
1985         }
1986         unset($caps);
1987         upgrade_main_savepoint(true, 2009061704);
1988     }
1990     if ($oldversion < 2009063000) {
1991         // upgrade format of _with_advanced settings - quiz only
1992         // note: this can be removed later, not needed for upgrades from 1.9.x
1993         if ($quiz = get_config('quiz')) {
1994             foreach ($quiz as $name=>$value) {
1995                 if (strpos($name, 'fix_') !== 0) {
1996                     continue;
1997                 }
1998                 $newname = substr($name,4).'_adv';
1999                 set_config($newname, $value, 'quiz');
2000                 unset_config($name, 'quiz');
2001             }
2002         }
2003         upgrade_main_savepoint(true, 2009063000);
2004     }
2006     if ($oldversion < 2009071000) {
2008     /// Rename field contextid on table block_instances to parentcontextid
2009         $table = new xmldb_table('block_instances');
2010         $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
2012     /// Launch rename field parentcontextid
2013         $dbman->rename_field($table, $field, 'parentcontextid');
2015     /// Main savepoint reached
2016         upgrade_main_savepoint(true, 2009071000);
2017     }
2019     if ($oldversion < 2009071600) {
2021     /// Define field summaryformat to be added to post
2022         $table = new xmldb_table('post');
2023         $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
2025     /// Conditionally launch add field summaryformat
2026         if (!$dbman->field_exists($table, $field)) {
2027             $dbman->add_field($table, $field);
2028         }
2030     /// Main savepoint reached
2031         upgrade_main_savepoint(true, 2009071600);
2032     }
2034     if ($oldversion < 2009072400) {
2036     /// Define table comments to be created
2037         $table = new xmldb_table('comments');
2039     /// Adding fields to table comments
2040         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2041         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2042         $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2043         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2044         $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2045         $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2046         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2047         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2049     /// Adding keys to table comments
2050         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2052     /// Conditionally launch create table for comments
2053         if (!$dbman->table_exists($table)) {
2054             $dbman->create_table($table);
2055         }
2057     /// Main savepoint reached
2058         upgrade_main_savepoint(true, 2009072400);
2059     }
2061     /**
2062      * This upgrade is to set up the new navigation blocks that have been developed
2063      * as part of Moodle 2.0
2064      * Now I [Sam Hemelryk] hit a conundrum while exploring how to go about this
2065      * as not only do we want to install the new blocks but we also want to set up
2066      * default instances of them, and at the same time remove instances of the blocks
2067      * that were/will-be outmoded by the two new navigation blocks.
2068      * After talking it through with Tim Hunt {@link http://moodle.org/mod/cvsadmin/view.php?conversationid=3112}
2069      * we decided that the best way to go about this was to put the bulk of the
2070      * upgrade operation into core upgrade `here` but to let the plugins block
2071      * still install the blocks.
2072      * This leaves one hairy end in that we will create block_instances within the
2073      * DB before the blocks themselves are created within the DB
2074      */
2075     if ($oldversion < 2009082800) {
2077         echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2079         // Get the system context so we can set the block instances to it
2080         $syscontext = get_context_instance(CONTEXT_SYSTEM);
2082         // An array to contain the new block instances we will create
2083         $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
2084         // The new global navigation block instance as a stdClass
2085         $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
2086         $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
2087         $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutely everywhere
2088         $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
2089         $newblockinstances['globalnavigation']->subpagetypepattern = null;
2090         $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
2091         $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
2092         $newblockinstances['globalnavigation']->configdata = '';
2093         // The new settings navigation block instance as a stdClass
2094         $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
2095         $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
2096         $newblockinstances['settingsnavigation']->showinsubcontexts = true;
2097         $newblockinstances['settingsnavigation']->pagetypepattern = '*';
2098         $newblockinstances['settingsnavigation']->subpagetypepattern = null;
2099         $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
2100         $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
2101         $newblockinstances['settingsnavigation']->configdata = '';
2103         // Blocks that are outmoded and for whom the bells will toll... by which I
2104         // mean we will delete all instances of
2105         $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
2106         $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
2107         unset($outmodedblocks);
2108         // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
2109         // cut down the number of delete queries we will need to run
2110         $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
2112         $contextids = array();
2113         $instanceids = array();
2114         // Iterate through all block instances
2115         foreach ($allblockinstances as $blockinstance) {
2116             if (!in_array($blockinstance->parentcontextid, $contextids)) {
2117                 $contextids[] = $blockinstance->parentcontextid;
2119                 // If we have over 1000 contexts clean them up and reset the array
2120                 // this ensures we don't hit any nasty memory limits or such
2121                 if (count($contextids) > 1000) {
2122                     upgrade_cleanup_unwanted_block_contexts($contextids);
2123                     $contextids = array();
2124                 }
2125             }
2126             if (!in_array($blockinstance->id, $instanceids)) {
2127                 $instanceids[] = $blockinstance->id;
2128                 // If we have more than 1000 block instances now remove all block positions
2129                 // and empty the array
2130                 if (count($instanceids) > 1000) {
2131                     $instanceidstring = join(',',$instanceids);
2132                     $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2133                     $instanceids = array();
2134                 }
2135             }
2136         }
2138         upgrade_cleanup_unwanted_block_contexts($contextids);
2140         if ($instanceids) {
2141             $instanceidstring = join(',',$instanceids);
2142             $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2143         }
2145         unset($allblockinstances);
2146         unset($contextids);
2147         unset($instanceids);
2148         unset($instanceidstring);
2150         // Now remove the actual block instance
2151         $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
2152         unset($outmodedblocksstring);
2154         // Insert the new block instances. Remember they have not been installed yet
2155         // however this should not be a problem
2156         foreach ($newblockinstances as $blockinstance) {
2157             $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
2158             // Ensure the block context is created.
2159             get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
2160         }
2161         unset($newblockinstances);
2163         upgrade_main_savepoint(true, 2009082800);
2164         // The end of the navigation upgrade
2165     }
2167     if ($oldversion < 2009100602) {
2168     /// Define table external_functions to be created
2169         $table = new xmldb_table('external_functions');
2171     /// Adding fields to table external_functions
2172         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2173         $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2174         $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2175         $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2176         $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2177         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2179     /// Adding keys to table external_functions
2180         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2182     /// Adding indexes to table external_functions
2183         $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2185     /// Launch create table for external_functions
2186         $dbman->create_table($table);
2188     /// Main savepoint reached
2189         upgrade_main_savepoint(true, 2009100602);
2190     }
2192     if ($oldversion < 2009100603) {
2193         /// Define table external_services to be created
2194         $table = new xmldb_table('external_services');
2196     /// Adding fields to table external_services
2197         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2198         $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2199         $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2200         $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
2201         $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2202         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
2203         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2204         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2206     /// Adding keys to table external_services
2207         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2209     /// Adding indexes to table external_services
2210         $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2212     /// Launch create table for external_services
2213         $dbman->create_table($table);
2215     /// Main savepoint reached
2216         upgrade_main_savepoint(true, 2009100603);
2217     }
2219     if ($oldversion < 2009100604) {
2220     /// Define table external_services_functions to be created
2221         $table = new xmldb_table('external_services_functions');
2223     /// Adding fields to table external_services_functions
2224         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2225         $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2226         $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2228     /// Adding keys to table external_services_functions
2229         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2230         $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2232     /// Launch create table for external_services_functions
2233         $dbman->create_table($table);
2235     /// Main savepoint reached
2236         upgrade_main_savepoint(true, 2009100604);
2237     }
2239     if ($oldversion < 2009100605) {
2240     /// Define table external_services_users to be created
2241         $table = new xmldb_table('external_services_users');
2243     /// Adding fields to table external_services_users
2244         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2245         $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2246         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2247         $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2248         $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2249         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2251     /// Adding keys to table external_services_users
2252         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2253         $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2254         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2256     /// Launch create table for external_services_users
2257         $dbman->create_table($table);
2259     /// Main savepoint reached
2260         upgrade_main_savepoint(true, 2009100605);
2261     }
2263     if ($oldversion < 2009102600) {
2265     /// Define table external_tokens to be created
2266         $table = new xmldb_table('external_tokens');
2268     /// Adding fields to table external_tokens
2269         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2270         $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
2271         $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2272         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2273         $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2274         $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
2275         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2276         $table->add_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
2277         $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2278         $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2279         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2280         $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2282     /// Adding keys to table external_tokens
2283         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2284         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2285         $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2286         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2287         $table->add_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
2289     /// Launch create table for external_tokens
2290         $dbman->create_table($table);
2292     /// Main savepoint reached
2293         upgrade_main_savepoint(true, 2009102600);
2294     }
2296    if ($oldversion < 2009103000) {
2298     /// Define table blog_association to be created
2299         $table = new xmldb_table('blog_association');
2301     /// Adding fields to table blog_association
2302         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2303         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2304         $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2306     /// Adding keys to table blog_association
2307         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2308         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2309         $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
2311     /// Conditionally launch create table for blog_association
2312         if (!$dbman->table_exists($table)) {
2313             $dbman->create_table($table);
2314         }
2316 /// Define table blog_external to be created
2317         $table = new xmldb_table('blog_external');
2319     /// Adding fields to table blog_external
2320         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2321         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2322         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2323         $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2324         $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2325         $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2326         $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2327         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2328         $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2330     /// Adding keys to table blog_external
2331         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2332         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2334     /// Conditionally launch create table for blog_external
2335         if ($dbman->table_exists($table)) {
2336             // Delete the existing one first (comes from early dev version)
2337             $dbman->drop_table($table);
2338         }
2339         $dbman->create_table($table);
2341         // now inform admins that some settings require attention after upgrade
2342         if (($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete)) {
2343             echo $OUTPUT->notification(get_string('bloglevelupgradenotice', 'admin'));
2345             $site = get_site();
2347             $a = new StdClass;
2348             $a->sitename = $site->fullname;
2349             $a->fixurl   = "$CFG->wwwroot/$CFG->admin/bloglevelupgrade.php";
2351             $subject = get_string('bloglevelupgrade', 'admin');
2352             $description = get_string('bloglevelupgradedescription', 'admin', $a);
2354             // can not use messaging here because it is not configured yet!
2355             upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2356         }
2357     /// Main savepoint reached
2358         upgrade_main_savepoint(true, 2009103000);
2359     }
2361     if ($oldversion < 2009110400) {
2362         // list of tables where we need to add new format field and convert texts
2363         $extendtables = array('course'              => 'summary',
2364                               'course_categories'   => 'description',
2365                               'course_categories'   => 'description',
2366                               'course_request'      => 'summary',
2367                               'grade_outcomes'      => 'description',
2368                               'groups'              => 'description',
2369                               'groupings'           => 'description',
2370                               'scale'               => 'description',
2371                               'user_info_field'     => 'description',
2372                               'user_info_field'     => 'defaultdata',
2373                               'user_info_data'      => 'data');
2375         foreach ($extendtables as $tablestr => $fieldstr) {
2376             $formatfieldstr = $fieldstr.'format';
2378             $table = new xmldb_table($tablestr);
2379             $field = new xmldb_field($formatfieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', $fieldstr);
2380             // Check that the field doesn't already exists
2381             if (!$dbman->field_exists($table, $field)) {
2382                 // Add the new field
2383                 $dbman->add_field($table, $field);
2384             }
2385             if ($CFG->texteditors !== 'textarea') {
2386                 $rs = $DB->get_recordset($tablestr, array($formatfieldstr => FORMAT_MOODLE), '', "id,$fieldstr,$formatfieldstr");
2387                 foreach ($rs as $rec) {
2388                     $rec->$fieldstr       = text_to_html($rec->$fieldstr, false, false, true);
2389                     $rec->$formatfieldstr = FORMAT_HTML;
2390                     $DB->update_record($tablestr, $rec);
2391                     upgrade_set_timeout();
2392                 }
2393                 $rs->close();
2394                 unset($rs);
2395             }
2396         }
2398         unset($rec);
2399         unset($extendtables);
2401         upgrade_main_savepoint(true, 2009110400);
2402     }
2404     if ($oldversion < 2009110401) {
2405         $table = new xmldb_table('user');
2407         // Change the precision of the description field first up.
2408         // This may grow!
2409         $field = new xmldb_field('description', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'url');
2410         $dbman->change_field_precision($table, $field);
2412         $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'description');
2413         // Check that the field doesn't already exists
2414         if (!$dbman->field_exists($table, $field)) {
2415             // Add the new field
2416             $dbman->add_field($table, $field);
2417         }
2418         if ($CFG->texteditors !== 'textarea') {
2419             $rs = $DB->get_recordset('user', array('descriptionformat'=>FORMAT_MOODLE, 'deleted'=>0, 'htmleditor'=>1), '', "id,description,descriptionformat");
2420             foreach ($rs as $rec) {
2421                 $rec->description       = text_to_html($rec->description, false, false, true);
2422                 $rec->descriptionformat = FORMAT_HTML;
2423                 $DB->update_record('user', $rec);
2424                 upgrade_set_timeout();
2425             }
2426             $rs->close();
2427         }
2429         upgrade_main_savepoint(true, 2009110401);
2430     }
2432     if ($oldversion < 2009112400) {
2433         if (empty($CFG->passwordsaltmain)) {
2434             $subject = get_string('check_passwordsaltmain_name', 'report_security');
2435             $description = get_string('check_passwordsaltmain_warning', 'report_security');;
2436             upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2437         }
2438         upgrade_main_savepoint(true, 2009112400);
2439     }
2441     if ($oldversion < 2010011200) {
2442         $table = new xmldb_table('grade_categories');
2443         $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timemodified');
2445         if (!$dbman->field_exists($table, $field)) {
2446             $dbman->add_field($table, $field);
2447         }
2449         upgrade_main_savepoint(true, 2010011200);
2450     }
2452     if ($oldversion < 2010012500) {
2453         upgrade_fix_incorrect_mnethostids();
2454         upgrade_main_savepoint(true, 2010012500);
2455     }
2457     if ($oldversion < 2010012600) {
2458         // do stuff to the mnet table
2459         $table = new xmldb_table('mnet_rpc');
2461         $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2462         $dbman->rename_field($table, $field, 'plugintype');
2464         $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2465         $dbman->rename_field($table, $field, 'pluginname');
2467         $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
2468         if (!$dbman->field_exists($table, $field)) {
2469             $dbman->add_field($table, $field);
2470         }
2472         $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
2473         if (!$dbman->field_exists($table, $field)) {
2474             $dbman->add_field($table, $field);
2475         }
2477         $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
2478         if (!$dbman->field_exists($table, $field)) {
2479             $dbman->add_field($table, $field);
2480         }
2482     /// Main savepoint reached
2483         upgrade_main_savepoint(true, 2010012600);
2484     }
2486     if ($oldversion < 2010012900) {
2488     /// Define table mnet_remote_rpc to be created
2489         $table = new xmldb_table('mnet_remote_rpc');
2491     /// Adding fields to table mnet_remote_rpc
2492         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2493         $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2494         $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
2496     /// Adding keys to table mnet_remote_rpc
2497         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2499     /// Conditionally launch create table for mnet_remote_rpc
2500         if (!$dbman->table_exists($table)) {
2501             $dbman->create_table($table);
2502         }
2505     /// Define table mnet_remote_service2rpc to be created
2506         $table = new xmldb_table('mnet_remote_service2rpc');
2508     /// Adding fields to table mnet_remote_service2rpc
2509         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2510         $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2511         $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2513     /// Adding keys to table mnet_remote_service2rpc
2514         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2516     /// Adding indexes to table mnet_remote_service2rpc
2517         $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2519     /// Conditionally launch create table for mnet_remote_service2rpc
2520         if (!$dbman->table_exists($table)) {
2521             $dbman->create_table($table);
2522         }
2525     /// Rename field function_name on table mnet_rpc to functionname
2526         $table = new xmldb_table('mnet_rpc');
2527         $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
2529     /// Launch rename field function_name
2530         $dbman->rename_field($table, $field, 'functionname');
2533     /// Rename field xmlrpc_path on table mnet_rpc to xmlrpcpath
2534         $table = new xmldb_table('mnet_rpc');
2535         $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
2537     /// Launch rename field xmlrpc_path
2538         $dbman->rename_field($table, $field, 'xmlrpcpath');
2541     /// Main savepoint reached
2542         upgrade_main_savepoint(true, 2010012900);
2543     }
2545     if ($oldversion < 2010012901) {
2547         /// Define field plugintype to be added to mnet_remote_rpc
2548         $table = new xmldb_table('mnet_remote_rpc');
2549         $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
2551         /// Conditionally launch add field plugintype
2552         if (!$dbman->field_exists($table, $field)) {
2553             $dbman->add_field($table, $field);
2554         }
2556     /// Define field pluginname to be added to mnet_remote_rpc
2557         $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
2559     /// Conditionally launch add field pluginname
2560         if (!$dbman->field_exists($table, $field)) {
2561             $dbman->add_field($table, $field);
2562         }
2564         /// Main savepoint reached
2565         upgrade_main_savepoint(true, 2010012901);
2566     }
2568     if ($oldversion < 2010012902) {
2570     /// Define field enabled to be added to mnet_remote_rpc
2571         $table = new xmldb_table('mnet_remote_rpc');
2572         $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
2574     /// Conditionally launch add field enabled
2575         if (!$dbman->field_exists($table, $field)) {
2576             $dbman->add_field($table, $field);
2577         }
2579         /// Main savepoint reached
2580         upgrade_main_savepoint(true, 2010012902);
2581     }
2583     /// MDL-17863. Increase the portno column length on mnet_host to handle any port number
2584     if ($oldversion < 2010020100) {
2585     /// Changing precision of field portno on table mnet_host to (5)
2586         $table = new xmldb_table('mnet_host');
2587         $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
2589     /// Launch change of precision for field portno
2590         $dbman->change_field_precision($table, $field);
2592         upgrade_main_savepoint(true, 2010020100);
2593     }
2595     if ($oldversion < 2010020300) {
2597     /// Define field timecreated to be added to user
2598         $table = new xmldb_table('user');
2599         $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
2601         if (!$dbman->field_exists($table, $field)) {
2602         /// Launch add field timecreated
2603             $dbman->add_field($table, $field);
2605             $DB->execute("UPDATE {user} SET timecreated = firstaccess");
2607             $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
2608             $DB->execute($sql);
2609         }
2610         upgrade_main_savepoint(true, 2010020300);
2611     }
2613     // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
2614     if ($oldversion < 2010020301) {
2615         if ($preamble = $CFG->filter_tex_latexpreamble) {
2616             $preamble = preg_replace('/^ +/m', '', $preamble);
2617             set_config('filter_tex_latexpreamble', $preamble);
2618         }
2619         upgrade_main_savepoint(true, 2010020301);
2620     }
2622     if ($oldversion < 2010021400) {
2623     /// Changes to modinfo mean we need to rebuild course cache
2624         require_once($CFG->dirroot . '/course/lib.php');
2625         rebuild_course_cache(0, true);
2626         upgrade_main_savepoint(true, 2010021400);
2627     }
2629     if ($oldversion < 2010021800) {
2630         $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
2631         upgrade_main_savepoint(true, 2010021800);
2632     }
2634     if ($oldversion < 2010031900) {
2635         // regeneration of sessions is always enabled, no need for this setting any more
2636         unset_config('regenloginsession');
2637         upgrade_main_savepoint(true, 2010031900);
2638     }
2640     if ($oldversion < 2010033101.02) {
2642     /// Define table license to be created
2643         $table = new xmldb_table('license');
2645     /// Adding fields to table license
2646         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2647         $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2648         $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2649         $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2650         $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
2651         $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2653     /// Adding keys to table license
2654         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2656     /// Conditionally launch create table for license
2657         if (!$dbman->table_exists($table)) {
2658             $dbman->create_table($table);
2659         }
2660         $active_licenses = array();
2662         $license = new stdClass();
2664         // add unknown license
2665         $license->shortname = 'unknown';
2666         $license->fullname = 'Unknown license';
2667         $license->source = '';
2668         $license->enabled = 1;
2669         $license->version = '2010033100';
2670         $active_licenses[] = $license->shortname;
2671         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2672             if ($record->version < $license->version) {
2673                 // update license record
2674                 $license->enabled = $record->enabled;
2675                 $license->id = $record->id;
2676                 $DB->update_record('license', $license);
2677             }
2678         } else {
2679             $DB->insert_record('license', $license);
2680         }
2682         // add all rights reserved license
2683         $license->shortname = 'allrightsreserved';
2684         $license->fullname = 'All rights reserved';
2685         $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
2686         $license->enabled = 1;
2687         $license->version = '2010033100';
2688         $active_licenses[] = $license->shortname;
2689         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2690             if ($record->version < $license->version) {
2691                 // update license record
2692                 $license->id = $record->id;
2693                 $license->enabled = $record->enabled;
2694                 $DB->update_record('license', $license);
2695             }
2696         } else {
2697             $DB->insert_record('license', $license);
2698         }
2700         // add public domain license
2701         $license->shortname = 'public';
2702         $license->fullname = 'Public Domain';
2703         $license->source = 'http://creativecommons.org/licenses/publicdomain/';
2704         $license->enabled = 1;
2705         $license->version = '2010033100';
2706         $active_licenses[] = $license->shortname;
2707         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2708             if ($record->version < $license->version) {
2709                 // update license record
2710                 $license->enabled = $record->enabled;
2711                 $license->id = $record->id;
2712                 $DB->update_record('license', $license);
2713             }
2714         } else {
2715             $DB->insert_record('license', $license);
2716         }
2718         // add creative commons license
2719         $license->shortname = 'cc';
2720         $license->fullname = 'Creative Commons';
2721         $license->source = 'http://creativecommons.org/licenses/by/3.0/';
2722         $license->enabled = 1;
2723         $license->version = '2010033100';
2724         $active_licenses[] = $license->shortname;
2725         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2726             if ($record->version < $license->version) {
2727                 // update license record
2728                 $license->enabled = $record->enabled;
2729                 $license->id = $record->id;
2730                 $DB->update_record('license', $license);
2731             }
2732         } else {
2733             $DB->insert_record('license', $license);
2734         }
2736         // add creative commons no derivs license
2737         $license->shortname = 'cc-nd';
2738         $license->fullname = 'Creative Commons - NoDerivs';
2739         $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
2740         $license->enabled = 1;
2741         $license->version = '2010033100';
2742         $active_licenses[] = $license->shortname;
2743         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2744             if ($record->version < $license->version) {
2745                 // update license record
2746                 $license->enabled = $record->enabled;
2747                 $license->id = $record->id;
2748                 $DB->update_record('license', $license);
2749             }
2750         } else {
2751             $DB->insert_record('license', $license);
2752         }
2754         // add creative commons no commercial no derivs license
2755         $license->shortname = 'cc-nc-nd';
2756         $license->fullname = 'Creative Commons - No Commercial NoDerivs';
2757         $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
2758         $license->enabled = 1;
2759         $license->version = '2010033100';
2760         $active_licenses[] = $license->shortname;
2761         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2762             if ($record->version < $license->version) {
2763                 // update license record
2764                 $license->enabled = $record->enabled;
2765                 $license->id = $record->id;
2766                 $DB->update_record('license', $license);
2767             }
2768         } else {
2769             $DB->insert_record('license', $license);
2770         }
2772         // add creative commons no commercial
2773         $license->shortname = 'cc-nc-nd';
2774         $license->shortname = 'cc-nc';
2775         $license->fullname = 'Creative Commons - No Commercial';
2776         $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
2777         $license->enabled = 1;
2778         $license->version = '2010033100';
2779         $active_licenses[] = $license->shortname;
2780         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2781             if ($record->version < $license->version) {
2782                 // update license record
2783                 $license->enabled = $record->enabled;
2784                 $license->id = $record->id;
2785                 $DB->update_record('license', $license);
2786             }
2787         } else {
2788             $DB->insert_record('license', $license);
2789         }
2791         // add creative commons no commercial sharealike
2792         $license->shortname = 'cc-nc-sa';
2793         $license->fullname = 'Creative Commons - No Commercial ShareAlike';
2794         $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
2795         $license->enabled = 1;
2796         $license->version = '2010033100';
2797         $active_licenses[] = $license->shortname;
2798         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2799             if ($record->version < $license->version) {
2800                 // update license record
2801                 $license->enabled = $record->enabled;
2802                 $license->id = $record->id;
2803                 $DB->update_record('license', $license);
2804             }
2805         } else {
2806             $DB->insert_record('license', $license);
2807         }
2809         // add creative commons sharealike
2810         $license->shortname = 'cc-sa';
2811         $license->fullname = 'Creative Commons - ShareAlike';
2812         $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
2813         $license->enabled = 1;
2814         $license->version = '2010033100';
2815         $active_licenses[] = $license->shortname;
2816         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2817             if ($record->version < $license->version) {
2818                 // update license record
2819                 $license->enabled = $record->enabled;
2820                 $license->id = $record->id;
2821                 $DB->update_record('license', $license);
2822             }
2823         } else {
2824             $DB->insert_record('license', $license);
2825         }
2827         set_config('licenses', implode(',', $active_licenses));
2828     /// set site default license
2829         set_config('sitedefaultlicense', 'allrightsreserved');
2831     /// Main savepoint reached
2832         upgrade_main_savepoint(true, 2010033101.02);
2833     }
2835     if ($oldversion < 2010033102.00) {
2836         // rename course view capability to participate
2837         $params = array('viewcap'=>'moodle/course:view', 'participatecap'=>'moodle/course:participate');
2838         $sql = "UPDATE {role_capabilities} SET capability = :participatecap WHERE capability = :viewcap";
2839         $DB->execute($sql, $params);
2840         $sql = "UPDATE {capabilities} SET name = :participatecap WHERE name = :viewcap";
2841         $DB->execute($sql, $params);
2842         // note: the view capability is readded again at the end of upgrade, but with different meaning
2843         upgrade_main_savepoint(true, 2010033102.00);
2844     }
2846     if ($oldversion < 2010033102.01) {
2847         // Define field archetype to be added to role table
2848         $table = new xmldb_table('role');
2849         $field = new xmldb_field('archetype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'sortorder');
2850         $dbman->add_field($table, $field);
2851         upgrade_main_savepoint(true, 2010033102.01);
2852     }
2854     if ($oldversion < 2010033102.02) {
2855         // Set archetype for existing roles and change admin role to manager role
2856         $sql = "SELECT r.*, rc.capability
2857                   FROM {role} r
2858                   JOIN {role_capabilities} rc ON rc.roleid = r.id
2859                  WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
2860               ORDER BY r.id";
2861         $params = array('syscontextid'=>SYSCONTEXTID, 'legacycaps'=>'moodle/legacy:%');
2862         $substart = strlen('moodle/legacy:');
2863         $roles = $DB->get_recordset_sql($sql, $params); // in theory could be multiple legacy flags in one role
2864         foreach ($roles as $role) {
2865             $role->archetype = substr($role->capability, $substart);
2866             unset($role->capability);
2867             if ($role->archetype === 'admin') {
2868                 $i = '';
2869                 if ($DB->record_exists('role', array('shortname'=>'manager')) or $DB->record_exists('role', array('name'=>get_string('manager', 'role')))) {
2870                     $i = 2;
2871                     while($DB->record_exists('role', array('shortname'=>'manager'.$i)) or $DB->record_exists('role', array('name'=>get_string('manager', 'role').$i))) {
2872                         $i++;
2873                     }
2874                 }
2875                 $role->archetype = 'manager';
2876                 if ($role->shortname === 'admin') {
2877                     $role->shortname   = 'manager'.$i;
2878                     $role->name        = get_string('manager', 'role').$i;
2879                     $role->description = get_string('managerdescription', 'role');
2880                 }
2881             }
2882             $DB->update_record('role', $role);
2883         }
2884         $roles->close();
2886         upgrade_main_savepoint(true, 2010033102.02);
2887     }
2889     if ($oldversion < 2010033102.03) {
2890         // Now pick site admins (===have manager role assigned at the system context)
2891         // and store them in the new $CFG->siteadmins setting as comma separated list
2892         $sql = "SELECT ra.id, ra.userid
2893                   FROM {role_assignments} ra
2894                   JOIN {role} r ON r.id = ra.roleid
2895                   JOIN {user} u ON u.id = ra.userid
2896                  WHERE ra.contextid = :syscontext AND r.archetype = 'manager' AND u.deleted = 0
2897               ORDER BY ra.id";
2898         $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
2899         $admins = array();
2900         foreach ($ras as $ra) {
2901             $admins[$ra->userid] = $ra->userid;
2902             set_config('siteadmins', implode(',', $admins)); // better to save it repeatedly, we do need at least one admin
2903             $DB->delete_records('role_assignments', array('id'=>$ra->id));
2904         }
2906         upgrade_main_savepoint(true, 2010033102.03);
2907     }
2909     if ($oldversion < 2010033102.04) {
2910         // clean up the manager roles
2911         $managers = $DB->get_records('role', array('archetype'=>'manager'));
2912         foreach ($managers as $manager) {
2913             // now sanitize the capabilities and overrides
2914             $DB->delete_records('role_capabilities', array('capability'=>'moodle/site:config', 'roleid'=>$manager->id)); // only site admins may configure servers
2915             // note: doanything and legacy caps are deleted automatically, they get moodle/course:view later at the end of the upgrade
2917             // remove manager role assignments bellow the course context level - admin role was never intended for activities and blocks,
2918             // the problem is that those assignments would not be visible after upgrade and old style admins in activities make no sense anyway
2919             $DB->delete_records_select('role_assignments', "roleid = :manager AND contextid IN (SELECT id FROM {context} WHERE contextlevel > 50)", array('manager'=>$manager->id));
2921             // allow them to assign all roles except default user, guest and frontpage - users get these roles automatically on the fly when needed
2922             $DB->delete_records('role_allow_assign', array('roleid'=>$manager->id));
2923             $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype <> 'user' AND archetype <> 'guest' AND archetype <> 'frontpage'");
2924             foreach ($roles as $role) {
2925                 $record = (object)array('roleid'=>$manager->id, 'allowassign'=>$role->id);
2926                 $DB->insert_record('role_allow_assign', $record);
2927             }
2929             // allow them to override all roles
2930             $DB->delete_records('role_allow_override', array('roleid'=>$manager->id));
2931             $roles = $DB->get_records_sql("SELECT * FROM {role}");
2932             foreach ($roles as $role) {
2933                 $record = (object)array('roleid'=>$manager->id, 'allowoverride'=>$role->id);
2934                 $DB->insert_record('role_allow_override', $record);
2935             }
2937             // allow them to switch to all following roles
2938             $DB->delete_records('role_allow_switch', array('roleid'=>$manager->id));
2939             $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype IN ('student', 'teacher', 'editingteacher')");
2940             foreach ($roles as $role) {
2941                 $record = (object)array('roleid'=>$manager->id, 'allowswitch'=>$role->id);
2942                 $DB->insert_record('role_allow_switch', $record);
2943             }
2944         }
2946         upgrade_main_savepoint(true, 2010033102.04);
2947     }
2949     if ($oldversion < 2010033102.05) {
2950         // remove course:view from all roles that are not used for enrolment, it does NOT belong there because it really means user is enrolled!
2951         $noenrolroles = $DB->get_records_select('role', "archetype IN ('guest', 'user', 'manager', 'coursecreator', 'frontpage')");
2952         foreach ($noenrolroles as $role) {
2953             $DB->delete_records('role_capabilities', array('roleid'=>$role->id, 'capability'=>'moodle/course:participate'));
2954         }
2955         upgrade_main_savepoint(true, 2010033102.05);
2956     }
2958     if ($oldversion < 2010033102.06) {
2959         // make sure there is nothing weird in default user role
2960         if (!empty($CFG->defaultuserroleid)) {
2961             if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
2962                 if ($role->archetype !== '' and $role->archetype !== 'user') {
2963                     upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default authenticated user role (defaultuserroleid) value is invalid, setting cleared.');
2964                     unset_config('defaultuserroleid');
2965                 }
2966             } else {
2967                 unset_config('defaultuserroleid');
2968             }
2969         }
2970         upgrade_main_savepoint(true, 2010033102.06);
2971     }
2973     if ($oldversion < 2010033102.07) {
2974         if (!empty($CFG->displayloginfailures) and $CFG->displayloginfailures === 'teacher') {
2975             upgrade_log(UPGRADE_LOG_NOTICE, null, 'Displaying of login failuters to teachers is not supported any more.');
2976             unset_config('displayloginfailures');
2977         }
2978         upgrade_main_savepoint(true, 2010033102.07);
2979     }
2981     if ($oldversion < 2010033102.08) {
2982         // make sure there are no problems in default guest role settings
2983         if (!empty($CFG->guestroleid)) {
2984             if ($role = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
2985                 if ($role->archetype !== '' and $role->archetype !== 'guest') {
2986                     upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default guest role (guestroleid) value is invalid, setting cleared.');
2987                     unset_config('guestroleid');
2988                 }
2989             } else {
2990                 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) does not exist, setting cleared.');
2991                 unset_config('guestroleid');
2992             }
2993         }
2994         // remove all roles of the guest account - the only way to change it is to override the guest role, sorry
2995         // the guest account gets all the role assignments on the fly which works fine in has_capability(),
2996         $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE username = 'guest')");
2998         upgrade_main_savepoint(true, 2010033102.08);
2999     }
3001     /// New table for storing which roles can be assigned in which contexts.
3002     if ($oldversion < 2010033102.09) {
3004     /// Define table role_context_levels to be created
3005         $table = new xmldb_table('role_context_levels');
3007     /// Adding fields to table role_context_levels
3008         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3009         $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3010         $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3012     /// Adding keys to table role_context_levels
3013         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3014         $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
3015         $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
3017     /// Conditionally launch create table for role_context_levels
3018         if (!$dbman->table_exists($table)) {
3019             $dbman->create_table($table);
3020         }
3022     /// Main savepoint reached
3023         upgrade_main_savepoint(true, 2010033102.09);
3024     }
3026     if ($oldversion < 2010033102.10) {
3027         // Now populate the role_context_levels table with the default values
3028         // NOTE: do not use accesslib methods here
3030         $rolecontextlevels = array();
3031         $defaults = array('manager'        => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE),
3032                           'coursecreator'  => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT),
3033                           'editingteacher' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3034                           'teacher'        => array(CONTEXT_COURSE, CONTEXT_MODULE),
3035                           'student'        => array(CONTEXT_COURSE, CONTEXT_MODULE),
3036                           'guest'          => array(),
3037                           'user'           => array(),
3038                           'frontpage'      => array());
3040         $roles = $DB->get_records('role', array(), '', 'id, archetype');
3041         foreach ($roles as $role) {
3042             if (isset($defaults[$role->archetype])) {
3043                 $rolecontextlevels[$role->id] = $defaults[$role->archetype];
3044             }
3045         }
3047         // add roles without archetypes, it may contain weird things, but we can not fix them
3048         list($narsql, $params) = $DB->get_in_or_equal(array_keys($defaults), SQL_PARAMS_NAMED, 'ar', false);
3049         $sql = "SELECT DISTINCT ra.roleid, con.contextlevel
3050                   FROM {role_assignments} ra
3051                   JOIN {context} con ON ra.contextid = con.id
3052                   JOIN {role} r ON r.id = ra.roleid
3053                  WHERE r.archetype $narsql";
3054         $existingrolecontextlevels = $DB->get_recordset_sql($sql, $params);
3055         foreach ($existingrolecontextlevels as $rcl) {
3056             if (!isset($rolecontextlevels[$rcl->roleid])) {
3057                 $rolecontextlevels[$rcl->roleid] = array();
3058             }
3059             $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
3060         }
3061         $existingrolecontextlevels->close();
3063         // Put the data into the database.
3064         $rcl = new stdClass();
3065         foreach ($rolecontextlevels as $roleid => $contextlevels) {
3066             $rcl->roleid = $roleid;
3067             foreach ($contextlevels as $level) {
3068                 $rcl->contextlevel = $level;
3069                 $DB->insert_record('role_context_levels', $rcl, false);
3070             }
3071         }
3073         // release memory!!
3074         unset($roles);
3075         unset($defaults);
3076         unset($rcl);
3077         unset($existingrolecontextlevels);
3078         unset($rolecontextlevels);
3080         // Main savepoint reached
3081         upgrade_main_savepoint(true, 2010033102.10);
3082     }
3084     if ($oldversion < 2010040700) {
3085         // migrate old groupings --> groupmembersonly setting
3086         if (isset($CFG->enablegroupings)) {
3087             set_config('enablegroupmembersonly', $CFG->enablegroupings);
3088             unset_config('enablegroupings');
3089         }
3091         // Main savepoint reached
3092         upgrade_main_savepoint(true, 2010040700);
3093     }
3095     if ($oldversion < 2010040900) {
3097         // Changing the default of field lang on table user to good old "en"
3098         $table = new xmldb_table('user');
3099         $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en', 'country');
3101         // Launch change of default for field lang
3102         $dbman->change_field_default($table, $field);
3104         // update main site lang
3105         if (strpos($CFG->lang, '_utf8') !== false) {
3106             $lang = str_replace('_utf8', '', $CFG->lang);
3107             set_config('lang', $lang);
3108         }
3110         // tweak langlist
3111         if (!empty($CFG->langlist)) {
3112             $langs = explode(',', $CFG->langlist);
3113             foreach ($langs as $key=>$lang) {
3114                 $lang = str_replace('_utf8', '', $lang);
3115                 $langs[$key] = $lang;
3116             }
3117             set_config('langlist', implode(',', $langs));
3118         }
3120         // Main savepoint reached
3121         upgrade_main_savepoint(true, 2010040900);
3122     }
3124     if ($oldversion < 2010040901) {
3126         // Remove "_utf8" suffix from all langs in user table
3127         $langs = $DB->get_records_sql("SELECT DISTINCT lang FROM {user} WHERE lang LIKE ?", array('%_utf8'));
3129         foreach ($langs as $lang=>$unused) {
3130             $newlang = str_replace('_utf8', '', $lang);
3131             $sql = "UPDATE {user} SET lang = :newlang WHERE lang = :lang";
3132             $DB->execute($sql, array('newlang'=>$newlang, 'lang'=>$lang));
3133         }
3135         // Main savepoint reached
3136         upgrade_main_savepoint(true, 2010040901);
3137     }
3139     if ($oldversion < 2010041301) {
3140         $sql = "UPDATE {block} SET name=? WHERE name=?";
3141         $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3142         $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3144         $sql = "UPDATE {block_instances} SET blockname=? WHERE blockname=?";
3145         $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3146         $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3147         upgrade_main_savepoint(true, 2010041301);
3148     }
3150     if ($oldversion < 2010042100) {
3152     /// Define table backup_controllers to be created
3153         $table = new xmldb_table('backup_controllers');
3155     /// Adding fields to table backup_controllers
3156         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3157         $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3158         $table->add_field('type', XMLDB_TYPE_CHAR, '6', null, XMLDB_NOTNULL, null, null);
3159         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3160         $table->add_field('format', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
3161         $table->add_field('interactive', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3162         $table->add_field('purpose', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3163         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3164         $table->add_field('status', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3165         $table->add_field('execution', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3166         $table->add_field('executiontime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3167         $table->add_field('checksum', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3168         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3169         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3170         $table->add_field('controller', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
3172     /// Adding keys to table backup_controllers
3173         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3174         $table->add_key('backupid_uk', XMLDB_KEY_UNIQUE, array('backupid'));
3175         $table->add_key('userid_fk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3177     /// Adding indexes to table backup_controllers
3178         $table->add_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3180     /// Conditionally launch create table for backup_controllers
3181         if (!$dbman->table_exists($table)) {
3182             $dbman->create_table($table);
3183         }
3185     /// Define table backup_ids_template to be created
3186         $table = new xmldb_table('backup_ids_template');
3188     /// Adding fields to table backup_ids_template
3189         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3190         $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3191         $table->add_field('itemname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
3192         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3193         $table->add_field('parentitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3195     /// Adding keys to table backup_ids_template
3196         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3197         $table->add_key('backupid_itemname_itemid_uk', XMLDB_KEY_UNIQUE, array('backupid', 'itemname', 'itemid'));
3199     /// Adding indexes to table backup_ids_template
3200         $table->add_index('backupid_parentitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'parentitemid'));
3202     /// Conditionally launch create table for backup_controllers
3203         if (!$dbman->table_exists($table)) {
3204             $dbman->create_table($table);
3205         }
3207     /// Main savepoint reached
3208         upgrade_main_savepoint(true, 2010042100);
3209     }
3211     if ($oldversion < 2010042301) {
3213         $table = new xmldb_table('course_sections');
3214         $field = new xmldb_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'section');
3216         if (!$dbman->field_exists($table, $field)) {
3217             $dbman->add_field($table, $field);
3218         }
3220         upgrade_main_savepoint(true, 2010042301);
3221     }
3223     if ($oldversion < 2010042302) {
3224         // Define table cohort to be created
3225         $table = new xmldb_table('cohort');
3227         // Adding fields to table cohort
3228         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3229         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3230         $table->add_field('name', XMLDB_TYPE_CHAR, '254', null, XMLDB_NOTNULL, null, null);
3231         $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3232         $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
3233         $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3234         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
3235         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3236         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3238         // Adding keys to table cohort
3239         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3240         $table->add_key('context', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3242         // Conditionally launch create table for cohort
3243         if (!$dbman->table_exists($table)) {
3244             $dbman->create_table($table);
3245         }
3247         upgrade_main_savepoint(true, 2010042302);
3248     }
3250     if ($oldversion < 2010042303) {
3251         // Define table cohort_members to be created
3252         $table = new xmldb_table('cohort_members');
3254         // Adding fields to table cohort_members
3255         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3256         $table->add_field('cohortid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3257         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3258         $table->add_field('timeadded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3260         // Adding keys to table cohort_members
3261         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3262         $table->add_key('cohortid', XMLDB_KEY_FOREIGN, array('cohortid'), 'cohort', array('id'));
3263         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3265         // Adding indexes to table cohort_members
3266         $table->add_index('cohortid-userid', XMLDB_INDEX_UNIQUE, array('cohortid', 'userid'));
3268         // Conditionally launch create table for cohort_members
3269         if (!$dbman->table_exists($table)) {
3270             $dbman->create_table($table);
3271         }
3273         // Main savepoint reached
3274         upgrade_main_savepoint(true, 2010042303);
3275     }
3277     if ($oldversion < 2010042800) {
3278         //drop the previously created ratings table
3279         $table = new xmldb_table('ratings');
3280         if ($dbman->table_exists($table)) {
3281             $dbman->drop_table($table);
3282         }
3284         //create the rating table (replaces module specific rating implementations)
3285         $table = new xmldb_table('rating');
3286         if ($dbman->table_exists($table)) {
3287             $dbman->drop_table($table);
3288         }
3290     /// Adding fields to table rating
3291         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3292         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3294         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3295         $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
3296         $table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3297         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3299         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3300         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3302     /// Adding keys to table rating
3303         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3304         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3305         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3307     /// Adding indexes to table rating
3308         $table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
3310     /// Create table for ratings
3311         if (!$dbman->table_exists($table)) {
3312             $dbman->create_table($table);
3313         }
3315         upgrade_main_savepoint(true, 2010042800);
3316     }
3318     if ($oldversion < 2010042801) {
3319         // migrating old comments block content
3320         $DB->execute("UPDATE {comments}
3321                          SET contextid = (SELECT parentcontextid
3322                                             FROM {block_instances}
3323                                            WHERE id = {comments}.itemid AND blockname = 'comments'),
3324                              commentarea = 'page_comments',
3325                              itemid = 0
3326                        WHERE commentarea = 'block_comments'
3327                              AND itemid != 0
3328                              AND EXISTS (SELECT 'x'
3329                                            FROM {block_instances}
3330                                           WHERE id = {comments}.itemid
3331                                                 AND blockname = 'comments')");
3333         // remove all orphaned record
3334         $DB->delete_records('comments', array('commentarea'=>'block_comments'));
3335         upgrade_main_savepoint(true, 2010042801);
3336     }
3338     if ($oldversion < 2010042802) { // Change backup_controllers->type to varchar10 (recreate dep. index)
3340     /// Define index typeitem_ix (not unique) to be dropped form backup_controllers
3341         $table = new xmldb_table('backup_controllers');
3342         $index = new xmldb_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3344     /// Conditionally launch drop index typeitem_ix
3345         if ($dbman->index_exists($table, $index)) {
3346             $dbman->drop_index($table, $index);
3347         }
3349     /// Changing precision of field type on table backup_controllers to (10)
3350         $table = new xmldb_table('backup_controllers');
3351         $field = new xmldb_field('type', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'backupid');
3353     /// Launch change of precision for field type
3354         $dbman->change_field_precision($table, $field);
3356     /// Define index typeitem_ix (not unique) to be added to backup_controllers
3357         $table = new xmldb_table('backup_controllers');
3358         $index = new xmldb_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3360     /// Conditionally launch add index typeitem_ix
3361         if (!$dbman->index_exists($table, $index)) {
3362             $dbman->add_index($table, $index);
3363         }
3365     /// Main savepoint reached
3366         upgrade_main_savepoint(true, 2010042802);
3367     }
3369     if ($oldversion < 2010043000) {  // Adding new course completion feature
3371     /// Add course completion tables
3372     /// Define table course_completion_aggr_methd to be created
3373         $table = new xmldb_table('course_completion_aggr_methd');
3375     /// Adding fields to table course_completion_aggr_methd
3376         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3377         $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3378         $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, null);
3379         $table->add_field('method', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3380         $table->add_field('value', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3382     /// Adding keys to table course_completion_aggr_methd
3383         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3385     /// Adding indexes to table course_completion_aggr_methd
3386         $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3387         $table->add_index('criteriatype', XMLDB_INDEX_NOTUNIQUE, array('criteriatype'));
3389     /// Conditionally launch create table for course_completion_aggr_methd
3390         if (!$dbman->table_exists($table)) {
3391             $dbman->create_table($table);
3392         }
3395     /// Define table course_completion_criteria to be created
3396         $table = new xmldb_table('course_completion_criteria');
3398     /// Adding fields to table course_completion_criteria
3399         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3400         $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3401         $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3402         $table->add_field('module', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3403         $table->add_field('moduleinstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3404         $table->add_field('courseinstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3405         $table->add_field('enrolperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3406         $table->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3407         $table->add_field('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3408         $table->add_field('role', XMLDB_TYPE_INTEGER, '10', XMLDB_UNS