3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * This file keeps track of upgrades to Moodle.
21 * Sometimes, changes between versions involve
22 * alterations to database structures and other
23 * major things that may break installations.
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.
29 * If there's something it cannot do itself, it
30 * will tell you what you need to do.
32 * The commands in here will all be database-neutral,
33 * using the methods of database_manager class
35 * Please do not forget to use upgrade_set_timeout()
36 * before any action that may take longer time to finish.
40 * @copyright 2006 onwards Martin Dougiamas http://dougiamas.com
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 defined('MOODLE_INTERNAL') || die();
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
55 function xmldb_main_upgrade($oldversion) {
56 global $CFG, $USER, $DB, $OUTPUT;
58 require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
60 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
62 ////////////////////////////////////////
63 ///upgrade supported only from 1.9.x ///
64 ////////////////////////////////////////
66 if ($oldversion < 2008030600) {
67 //NOTE: this table was added much later later in dev cycle, but we need it here, upgrades from pre PR1 not supported
69 /// Define table upgrade_log to be created
70 $table = new xmldb_table('upgrade_log');
72 /// Adding fields to table upgrade_log
73 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
74 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
75 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
76 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
77 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
78 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
79 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
80 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
81 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
83 /// Adding keys to table upgrade_log
84 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
85 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
87 /// Adding indexes to table upgrade_log
88 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
89 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
91 /// Create table for upgrade_log
92 $dbman->create_table($table);
94 /// Main savepoint reached
95 upgrade_main_savepoint(true, 2008030600);
98 if ($oldversion < 2008030601) {
99 //NOTE: this table was added much later later in dev cycle, but we need it here, upgrades from pre PR1 not supported
101 /// Define table log_queries to be created
102 $table = new xmldb_table('log_queries');
104 /// Adding fields to table log_queries
105 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
106 $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
107 $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
108 $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
109 $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
110 $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
111 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
112 $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
113 $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
115 /// Adding keys to table log_queries
116 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
118 /// Conditionally launch create table for log_queries
119 $dbman->create_table($table);
121 /// Main savepoint reached
122 upgrade_main_savepoint(true, 2008030601);
125 if ($oldversion < 2008030602) {
126 @unlink($CFG->dataroot.'/cache/languages');
128 if (file_exists("$CFG->dataroot/lang")) {
129 // rename old lang directory so that the new and old langs do not mix
130 if (rename("$CFG->dataroot/lang", "$CFG->dataroot/oldlang")) {
131 $oldlang = "$CFG->dataroot/oldlang";
133 $oldlang = "$CFG->dataroot/lang";
138 // TODO: fetch previously installed languages ("*_utf8") found in $oldlang from moodle.org
139 upgrade_set_timeout(60*20); // this may take a while
142 // TODO: add some info file to $oldlang describing what to do with "$oldlang/*_utf8_local" dirs
145 // Main savepoint reached
146 upgrade_main_savepoint(true, 2008030602);
149 if ($oldversion < 2008030700) {
150 upgrade_set_timeout(60*20); // this may take a while
152 /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
153 $table = new xmldb_table('grade_letters');
154 $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
156 /// Launch drop index contextid-lowerboundary
157 if ($dbman->index_exists($table, $index)) {
158 $dbman->drop_index($table, $index);
161 /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
162 $table = new xmldb_table('grade_letters');
163 $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
165 /// Launch add index contextid-lowerboundary-letter
166 $dbman->add_index($table, $index);
168 /// Main savepoint reached
169 upgrade_main_savepoint(true, 2008030700);
172 if ($oldversion < 2008050100) {
173 // Update courses that used weekscss to weeks
174 $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
175 upgrade_main_savepoint(true, 2008050100);
178 if ($oldversion < 2008050200) {
179 // remove unused config options
180 unset_config('statsrolesupgraded');
181 upgrade_main_savepoint(true, 2008050200);
184 if ($oldversion < 2008050700) {
185 upgrade_set_timeout(60*20); // this may take a while
187 /// Fix minor problem caused by MDL-5482.
188 require_once($CFG->dirroot . '/question/upgrade.php');
189 question_fix_random_question_parents();
190 upgrade_main_savepoint(true, 2008050700);
193 if ($oldversion < 2008051201) {
194 echo $OUTPUT->notification('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
195 upgrade_set_timeout(60*20); // this may take a while
197 /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
198 $dbfamily = $DB->get_dbfamily();
199 if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
200 $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
203 /// Define index idnumber (not unique) to be dropped form user
204 $table = new xmldb_table('user');
205 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
207 /// Launch drop index idnumber
208 if ($dbman->index_exists($table, $index)) {
209 $dbman->drop_index($table, $index);
212 /// Changing precision of field idnumber on table user to (255)
213 $table = new xmldb_table('user');
214 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
216 /// Launch change of precision for field idnumber
217 $dbman->change_field_precision($table, $field);
219 /// Launch add index idnumber again
220 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
221 $dbman->add_index($table, $index);
223 /// Main savepoint reached
224 upgrade_main_savepoint(true, 2008051201);
227 if ($oldversion < 2008051203) {
228 $table = new xmldb_table('mnet_enrol_course');
229 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
230 $dbman->change_field_precision($table, $field);
231 upgrade_main_savepoint(true, 2008051203);
234 if ($oldversion < 2008063001) {
235 upgrade_set_timeout(60*20); // this may take a while
237 // table to be modified
238 $table = new xmldb_table('tag_instance');
240 $field = new xmldb_field('tiuserid');
241 if (!$dbman->field_exists($table, $field)) {
242 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'itemid');
243 $dbman->add_field($table, $field);
246 $index = new xmldb_index('itemtype-itemid-tagid');
247 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
248 if ($dbman->index_exists($table, $index)) {
249 $dbman->drop_index($table, $index);
251 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
252 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
253 if (!$dbman->index_exists($table, $index)) {
254 $dbman->add_index($table, $index);
257 /// Main savepoint reached
258 upgrade_main_savepoint(true, 2008063001);
261 if ($oldversion < 2008070300) {
262 $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
263 upgrade_main_savepoint(true, 2008070300);
266 if ($oldversion < 2008070701) {
268 /// Define table portfolio_instance to be created
269 $table = new xmldb_table('portfolio_instance');
271 /// Adding fields to table portfolio_instance
272 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
273 $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
274 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
275 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
277 /// Adding keys to table portfolio_instance
278 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
280 /// Conditionally launch create table for portfolio_instance
281 if (!$dbman->table_exists($table)) {
282 $dbman->create_table($table);
284 /// Define table portfolio_instance_config to be created
285 $table = new xmldb_table('portfolio_instance_config');
287 /// Adding fields to table portfolio_instance_config
288 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
289 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
290 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
291 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
293 /// Adding keys to table portfolio_instance_config
294 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
295 $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
297 /// Adding indexes to table portfolio_instance_config
298 $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
300 /// Conditionally launch create table for portfolio_instance_config
301 if (!$dbman->table_exists($table)) {
302 $dbman->create_table($table);
305 /// Define table portfolio_instance_user to be created
306 $table = new xmldb_table('portfolio_instance_user');
308 /// Adding fields to table portfolio_instance_user
309 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
310 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
311 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
312 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
313 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
315 /// Adding keys to table portfolio_instance_user
316 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
317 $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
318 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
320 /// Conditionally launch create table for portfolio_instance_user
321 if (!$dbman->table_exists($table)) {
322 $dbman->create_table($table);
325 /// Main savepoint reached
326 upgrade_main_savepoint(true, 2008070701);
329 if ($oldversion < 2008072400) {
330 /// Create the database tables for message_processors
331 $table = new xmldb_table('message_processors');
332 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
333 $table->add_field('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null);
334 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
335 $dbman->create_table($table);
337 /// delete old and create new fields
338 $table = new xmldb_table('message');
339 $field = new xmldb_field('messagetype');
340 $dbman->drop_field($table, $field);
343 $field = new xmldb_field('message');
344 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
345 $dbman->rename_field($table, $field, 'fullmessage');
346 $field = new xmldb_field('format');
347 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
348 $dbman->rename_field($table, $field, 'fullmessageformat');
350 /// new message fields
351 $field = new xmldb_field('subject');
352 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
353 $dbman->add_field($table, $field);
354 $field = new xmldb_field('fullmessagehtml');
355 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
356 $dbman->add_field($table, $field);
357 $field = new xmldb_field('smallmessage');
358 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
359 $dbman->add_field($table, $field);
362 $table = new xmldb_table('message_read');
363 $field = new xmldb_field('messagetype');
364 $dbman->drop_field($table, $field);
365 $field = new xmldb_field('mailed');
366 $dbman->drop_field($table, $field);
369 $field = new xmldb_field('message');
370 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
371 $dbman->rename_field($table, $field, 'fullmessage');
372 $field = new xmldb_field('format');
373 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
374 $dbman->rename_field($table, $field, 'fullmessageformat');
377 /// new message fields
378 $field = new xmldb_field('subject');
379 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
380 $dbman->add_field($table, $field);
381 $field = new xmldb_field('fullmessagehtml');
382 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
383 $dbman->add_field($table, $field);
384 $field = new xmldb_field('smallmessage');
385 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
386 $dbman->add_field($table, $field);
389 $table = new xmldb_table('message_working');
390 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
391 $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
392 $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
393 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
394 $dbman->create_table($table);
397 upgrade_main_savepoint(true, 2008072400);
400 if ($oldversion < 2008072800) {
402 /// Define field enablecompletion to be added to course
403 $table = new xmldb_table('course');
404 $field = new xmldb_field('enablecompletion');
405 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
407 /// Launch add field enablecompletion
408 if (!$dbman->field_exists($table,$field)) {
409 $dbman->add_field($table, $field);
412 /// Define field completion to be added to course_modules
413 $table = new xmldb_table('course_modules');
414 $field = new xmldb_field('completion');
415 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'groupmembersonly');
417 /// Launch add field completion
418 if (!$dbman->field_exists($table,$field)) {
419 $dbman->add_field($table, $field);
422 /// Define field completiongradeitemnumber to be added to course_modules
423 $field = new xmldb_field('completiongradeitemnumber');
424 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'completion');
426 /// Launch add field completiongradeitemnumber
427 if (!$dbman->field_exists($table,$field)) {
428 $dbman->add_field($table, $field);
431 /// Define field completionview to be added to course_modules
432 $field = new xmldb_field('completionview');
433 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiongradeitemnumber');
435 /// Launch add field completionview
436 if (!$dbman->field_exists($table,$field)) {
437 $dbman->add_field($table, $field);
440 /// Define field completionexpected to be added to course_modules
441 $field = new xmldb_field('completionexpected');
442 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionview');
444 /// Launch add field completionexpected
445 if (!$dbman->field_exists($table,$field)) {
446 $dbman->add_field($table, $field);
449 /// Define table course_modules_completion to be created
450 $table = new xmldb_table('course_modules_completion');
451 if (!$dbman->table_exists($table)) {
453 /// Adding fields to table course_modules_completion
454 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
455 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
456 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
457 $table->add_field('completionstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
458 $table->add_field('viewed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
459 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
461 /// Adding keys to table course_modules_completion
462 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
464 /// Adding indexes to table course_modules_completion
465 $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
466 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
468 /// Launch create table for course_modules_completion
469 $dbman->create_table($table);
472 /// Main savepoint reached
473 upgrade_main_savepoint(true, 2008072800);
476 if ($oldversion < 2008073000) {
478 /// Define table portfolio_log to be created
479 $table = new xmldb_table('portfolio_log');
481 /// Adding fields to table portfolio_log
482 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
483 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
484 $table->add_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
485 $table->add_field('portfolio', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
486 $table->add_field('caller_class', XMLDB_TYPE_CHAR, '150', null, XMLDB_NOTNULL, null, null);
487 $table->add_field('caller_file', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
488 $table->add_field('caller_sha1', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
489 $table->add_field('tempdataid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
490 $table->add_field('returnurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
491 $table->add_field('continueurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
493 /// Adding keys to table portfolio_log
494 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
495 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
496 $table->add_key('portfoliofk', XMLDB_KEY_FOREIGN, array('portfolio'), 'portfolio_instance', array('id'));
498 /// Conditionally launch create table for portfolio_log
499 if (!$dbman->table_exists($table)) {
500 $dbman->create_table($table);
503 /// Main savepoint reached
504 upgrade_main_savepoint(true, 2008073000);
507 if ($oldversion < 2008073104) {
508 /// Drop old table that might exist for some people
509 $table = new xmldb_table('message_providers');
510 if ($dbman->table_exists($table)) {
511 $dbman->drop_table($table);
514 /// Define table message_providers to be created
515 $table = new xmldb_table('message_providers');
517 /// Adding fields to table message_providers
518 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
519 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
520 $table->add_field('component', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
521 $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
523 /// Adding keys to table message_providers
524 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
526 /// Adding indexes to table message_providers
527 $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
529 /// Create table for message_providers
530 $dbman->create_table($table);
532 upgrade_main_savepoint(true, 2008073104);
535 if ($oldversion < 2008073111) {
536 /// Define table files to be created
537 $table = new xmldb_table('files');
539 /// Adding fields to table files
540 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
541 $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
542 $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
543 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
544 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
545 $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
546 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
547 $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
548 $table->add_field('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
549 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
550 $table->add_field('filesize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
551 $table->add_field('mimetype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
552 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
553 $table->add_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
554 $table->add_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null);
555 $table->add_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null);
556 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
557 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
559 /// Adding keys to table files
560 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
561 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
562 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
564 /// Adding indexes to table files
565 $table->add_index('component-filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('component', 'filearea', 'contextid', 'itemid'));
566 $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
567 $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
569 /// Conditionally launch create table for files
570 $dbman->create_table($table);
572 /// Main savepoint reached
573 upgrade_main_savepoint(true, 2008073111);
576 if ($oldversion < 2008073112) {
577 // Define field legacyfiles to be added to course
578 $table = new xmldb_table('course');
579 $field = new xmldb_field('legacyfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'maxbytes');
581 // Launch add field legacyfiles
582 $dbman->add_field($table, $field);
583 // enable legacy files in all courses
584 $DB->execute("UPDATE {course} SET legacyfiles = 2");
586 // Main savepoint reached
587 upgrade_main_savepoint(true, 2008073112);
590 if ($oldversion < 2008073113) {
591 /// move all course, backup and other files to new filepool based storage
592 upgrade_migrate_files_courses();
593 /// Main savepoint reached
594 upgrade_main_savepoint(true, 2008073113);
597 if ($oldversion < 2008073114) {
598 /// move all course, backup and other files to new filepool based storage
599 upgrade_migrate_files_blog();
600 /// Main savepoint reached
601 upgrade_main_savepoint(true, 2008073114);
604 if ($oldversion < 2008080400) {
605 // Add field ssl_jump_url to mnet application, and populate existing default applications
606 $table = new xmldb_table('mnet_application');
607 $field = new xmldb_field('sso_jump_url');
608 if (!$dbman->field_exists($table, $field)) {
609 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
610 $dbman->add_field($table, $field);
611 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
612 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
615 /// Main savepoint reached
616 upgrade_main_savepoint(true, 2008080400);
619 if ($oldversion < 2008080500) {
621 /// Define table portfolio_tempdata to be created
622 $table = new xmldb_table('portfolio_tempdata');
624 /// Adding fields to table portfolio_tempdata
625 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
626 $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
627 $table->add_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
628 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
629 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
631 /// Adding keys to table portfolio_tempdata
632 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
633 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
634 $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
636 /// Conditionally launch create table for portfolio_tempdata
637 if (!$dbman->table_exists($table)) {
638 $dbman->create_table($table);
641 /// Main savepoint reached
642 upgrade_main_savepoint(true, 2008080500);
645 if ($oldversion < 2008081500) {
646 /// Changing the type of all the columns that the question bank uses to store grades to be NUMBER(12, 7).
647 $table = new xmldb_table('question');
648 $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1.0000000', 'generalfeedback');
649 $dbman->change_field_type($table, $field);
650 upgrade_main_savepoint(true, 2008081500);
653 if ($oldversion < 2008081501) {
654 $table = new xmldb_table('question');
655 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0.1000000', 'defaultgrade');
656 $dbman->change_field_type($table, $field);
657 upgrade_main_savepoint(true, 2008081501);
660 if ($oldversion < 2008081502) {
661 $table = new xmldb_table('question_answers');
662 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'answer');
663 $dbman->change_field_type($table, $field);
664 upgrade_main_savepoint(true, 2008081502);
667 if ($oldversion < 2008081503) {
668 $table = new xmldb_table('question_sessions');
669 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'newgraded');
670 $dbman->change_field_type($table, $field);
671 upgrade_main_savepoint(true, 2008081503);
674 if ($oldversion < 2008081504) {
675 $table = new xmldb_table('question_states');
676 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'event');
677 $dbman->change_field_type($table, $field);
678 upgrade_main_savepoint(true, 2008081504);
681 if ($oldversion < 2008081505) {
682 $table = new xmldb_table('question_states');
683 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'grade');
684 $dbman->change_field_type($table, $field);
685 upgrade_main_savepoint(true, 2008081505);
688 if ($oldversion < 2008081506) {
689 $table = new xmldb_table('question_states');
690 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'raw_grade');
691 $dbman->change_field_type($table, $field);
692 upgrade_main_savepoint(true, 2008081506);
695 if ($oldversion < 2008081600) {
697 /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
698 unset_config('unicodedb');
700 /// Main savepoint reached
701 upgrade_main_savepoint(true, 2008081600);
704 if ($oldversion < 2008082602) {
706 /// Define table repository to be dropped
707 $table = new xmldb_table('repository');
709 /// Conditionally launch drop table for repository
710 if ($dbman->table_exists($table)) {
711 $dbman->drop_table($table);
714 /// Define table repository to be created
715 $table = new xmldb_table('repository');
717 /// Adding fields to table repository
718 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
719 $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
720 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1');
721 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
723 /// Adding keys to table repository
724 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
726 /// Conditionally launch create table for repository
727 if (!$dbman->table_exists($table)) {
728 $dbman->create_table($table);
731 /// Define table repository_instances to be created
732 $table = new xmldb_table('repository_instances');
734 /// Adding fields to table repository_instances
735 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
736 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
737 $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
738 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
739 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
740 $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null);
741 $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
742 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
743 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
744 $table->add_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
746 /// Adding keys to table repository_instances
747 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
749 /// Conditionally launch create table for repository_instances
750 if (!$dbman->table_exists($table)) {
751 $dbman->create_table($table);
754 /// Define table repository_instance_config to be created
755 $table = new xmldb_table('repository_instance_config');
757 /// Adding fields to table repository_instance_config
758 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
759 $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
760 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
761 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
763 /// Adding keys to table repository_instance_config
764 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
766 /// Conditionally launch create table for repository_instance_config
767 if (!$dbman->table_exists($table)) {
768 $dbman->create_table($table);
771 /// Main savepoint reached
772 upgrade_main_savepoint(true, 2008082602);
775 if ($oldversion < 2008082700) {
776 /// Add a new column to the question sessions table to record whether a
777 /// question has been flagged.
779 /// Define field flagged to be added to question_sessions
780 $table = new xmldb_table('question_sessions');
781 $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
783 /// Conditionally launch add field flagged
784 if (!$dbman->field_exists($table, $field)) {
785 $dbman->add_field($table, $field);
788 /// Main savepoint reached
789 upgrade_main_savepoint(true, 2008082700);
792 if ($oldversion < 2008082900) {
794 /// Changing precision of field parent_type on table mnet_rpc to (20)
795 $table = new xmldb_table('mnet_rpc');
796 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
798 /// Launch change of precision for field parent_type
799 $dbman->change_field_precision($table, $field);
801 /// Main savepoint reached
802 upgrade_main_savepoint(true, 2008082900);
805 // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
806 if ($oldversion < 2008091000) {
807 foreach (get_object_vars($CFG) as $name => $value) {
808 if (substr($name, strlen($name) - 8) !== '_version') {
811 $pluginname = substr($name, 0, strlen($name) - 8);
812 if (!strpos($pluginname, '_')) {
813 // Skip things like backup_version that don't contain an extra _
816 if ($pluginname == 'enrol_ldap_version') {
817 // Special case - this is something different from a plugin version number.
820 if (!preg_match('/^\d{10}$/', $value)) {
821 // Extra safety check, skip anything that does not look like a Moodle
822 // version number (10 digits).
825 set_config('version', $value, $pluginname);
828 upgrade_main_savepoint(true, 2008091000);
831 if ($oldversion < 2008092300) {
832 unset_config('editorspelling');
833 unset_config('editordictionary');
834 /// Main savepoint reached
835 upgrade_main_savepoint(true, 2008092300);
838 if ($oldversion < 2008101300) {
840 if (!get_config(NULL, 'statsruntimedays')) {
841 set_config('statsruntimedays', '31');
844 /// Main savepoint reached
845 upgrade_main_savepoint(true, 2008101300);
848 /// Drop the deprecated teacher, teachers, student and students columns from the course table.
849 if ($oldversion < 2008111200) {
850 $table = new xmldb_table('course');
852 /// Conditionally launch drop field teacher
853 $field = new xmldb_field('teacher');
854 if ($dbman->field_exists($table, $field)) {
855 $dbman->drop_field($table, $field);
858 /// Conditionally launch drop field teacher
859 $field = new xmldb_field('teachers');
860 if ($dbman->field_exists($table, $field)) {
861 $dbman->drop_field($table, $field);
864 /// Conditionally launch drop field teacher
865 $field = new xmldb_field('student');
866 if ($dbman->field_exists($table, $field)) {
867 $dbman->drop_field($table, $field);
870 /// Conditionally launch drop field teacher
871 $field = new xmldb_field('students');
872 if ($dbman->field_exists($table, $field)) {
873 $dbman->drop_field($table, $field);
876 /// Main savepoint reached
877 upgrade_main_savepoint(true, 2008111200);
880 /// Add a unique index to the role.name column.
881 if ($oldversion < 2008111800) {
883 /// Define index name (unique) to be added to role
884 $table = new xmldb_table('role');
885 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
887 /// Conditionally launch add index name
888 if (!$dbman->index_exists($table, $index)) {
889 $dbman->add_index($table, $index);
892 /// Main savepoint reached
893 upgrade_main_savepoint(true, 2008111800);
896 /// Add a unique index to the role.shortname column.
897 if ($oldversion < 2008111801) {
899 /// Define index shortname (unique) to be added to role
900 $table = new xmldb_table('role');
901 $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
903 /// Conditionally launch add index shortname
904 if (!$dbman->index_exists($table, $index)) {
905 $dbman->add_index($table, $index);
908 /// Main savepoint reached
909 upgrade_main_savepoint(true, 2008111801);
912 if ($oldversion < 2008120700) {
914 /// Changing precision of field shortname on table course_request to (100)
915 $table = new xmldb_table('course_request');
916 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
918 /// Before changing the field, drop dependent indexes
919 /// Define index shortname (not unique) to be dropped form course_request
920 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
921 /// Conditionally launch drop index shortname
922 if ($dbman->index_exists($table, $index)) {
923 $dbman->drop_index($table, $index);
926 /// Launch change of precision for field shortname
927 $dbman->change_field_precision($table, $field);
929 /// After changing the field, recreate dependent indexes
930 /// Define index shortname (not unique) to be added to course_request
931 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
932 /// Conditionally launch add index shortname
933 if (!$dbman->index_exists($table, $index)) {
934 $dbman->add_index($table, $index);
937 /// Main savepoint reached
938 upgrade_main_savepoint(true, 2008120700);
941 if ($oldversion < 2008120801) {
943 /// Changing precision of field shortname on table mnet_enrol_course to (100)
944 $table = new xmldb_table('mnet_enrol_course');
945 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
947 /// Launch change of precision for field shortname
948 $dbman->change_field_precision($table, $field);
950 /// Main savepoint reached
951 upgrade_main_savepoint(true, 2008120801);
954 if ($oldversion < 2008121701) {
956 /// Define field availablefrom to be added to course_modules
957 $table = new xmldb_table('course_modules');
958 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
960 /// Conditionally launch add field availablefrom
961 if (!$dbman->field_exists($table, $field)) {
962 $dbman->add_field($table, $field);
965 /// Define field availableuntil to be added to course_modules
966 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
968 /// Conditionally launch add field availableuntil
969 if (!$dbman->field_exists($table, $field)) {
970 $dbman->add_field($table, $field);
973 /// Define field showavailability to be added to course_modules
974 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
976 /// Conditionally launch add field showavailability
977 if (!$dbman->field_exists($table, $field)) {
978 $dbman->add_field($table, $field);
981 /// Define table course_modules_availability to be created
982 $table = new xmldb_table('course_modules_availability');
984 /// Adding fields to table course_modules_availability
985 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
986 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
987 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
988 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
989 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
990 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
991 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
993 /// Adding keys to table course_modules_availability
994 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
995 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
996 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
997 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
999 /// Conditionally launch create table for course_modules_availability
1000 if (!$dbman->table_exists($table)) {
1001 $dbman->create_table($table);
1004 /// Changes to modinfo mean we need to rebuild course cache
1005 require_once($CFG->dirroot . '/course/lib.php');
1006 rebuild_course_cache(0, true);
1008 /// Main savepoint reached
1009 upgrade_main_savepoint(true, 2008121701);
1012 if ($oldversion < 2009010500) {
1013 /// clean up config table a bit
1014 unset_config('session_error_counter');
1016 /// Main savepoint reached
1017 upgrade_main_savepoint(true, 2009010500);
1020 if ($oldversion < 2009010600) {
1022 /// Define field originalquestion to be dropped from question_states
1023 $table = new xmldb_table('question_states');
1024 $field = new xmldb_field('originalquestion');
1026 /// Conditionally launch drop field originalquestion
1027 if ($dbman->field_exists($table, $field)) {
1028 $dbman->drop_field($table, $field);
1031 /// Main savepoint reached
1032 upgrade_main_savepoint(true, 2009010600);
1035 if ($oldversion < 2009010601) {
1037 /// Changing precision of field ip on table log to (45)
1038 $table = new xmldb_table('log');
1039 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1041 /// Launch change of precision for field ip
1042 $dbman->change_field_precision($table, $field);
1044 /// Main savepoint reached
1045 upgrade_main_savepoint(true, 2009010601);
1048 if ($oldversion < 2009010602) {
1050 /// Changing precision of field lastip on table user to (45)
1051 $table = new xmldb_table('user');
1052 $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
1054 /// Launch change of precision for field lastip
1055 $dbman->change_field_precision($table, $field);
1057 /// Main savepoint reached
1058 upgrade_main_savepoint(true, 2009010602);
1061 if ($oldversion < 2009010603) {
1063 /// Changing precision of field ip_address on table mnet_host to (45)
1064 $table = new xmldb_table('mnet_host');
1065 $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
1067 /// Launch change of precision for field ip_address
1068 $dbman->change_field_precision($table, $field);
1070 /// Main savepoint reached
1071 upgrade_main_savepoint(true, 2009010603);
1074 if ($oldversion < 2009010604) {
1076 /// Changing precision of field ip on table mnet_log to (45)
1077 $table = new xmldb_table('mnet_log');
1078 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1080 /// Launch change of precision for field ip
1081 $dbman->change_field_precision($table, $field);
1083 /// Main savepoint reached
1084 upgrade_main_savepoint(true, 2009010604);
1087 if ($oldversion < 2009011000) {
1089 /// Changing nullability of field configdata on table block_instance to null
1090 $table = new xmldb_table('block_instance');
1091 $field = new xmldb_field('configdata');
1092 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
1094 /// Launch change of nullability for field configdata
1095 $dbman->change_field_notnull($table, $field);
1097 /// Main savepoint reached
1098 upgrade_main_savepoint(true, 2009011000);
1101 if ($oldversion < 2009011100) {
1102 /// Remove unused settings
1103 unset_config('zip');
1104 unset_config('unzip');
1105 unset_config('adminblocks_initialised');
1107 /// Main savepoint reached
1108 upgrade_main_savepoint(true, 2009011100);
1111 if ($oldversion < 2009011101) {
1112 /// Migrate backup settings to core plugin config table
1113 $configs = $DB->get_records('backup_config');
1114 foreach ($configs as $config) {
1115 set_config($config->name, $config->value, 'backup');
1118 /// Define table to be dropped
1119 $table = new xmldb_table('backup_config');
1121 /// Launch drop table for old backup config
1122 $dbman->drop_table($table);
1124 /// Main savepoint reached
1125 upgrade_main_savepoint(true, 2009011101);
1128 if ($oldversion < 2009011303) {
1130 /// Define table config_log to be created
1131 $table = new xmldb_table('config_log');
1133 /// Adding fields to table config_log
1134 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1135 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1136 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1137 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1138 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1139 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1140 $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1142 /// Adding keys to table config_log
1143 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1144 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1146 /// Adding indexes to table config_log
1147 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1149 /// Launch create table for config_log
1150 $dbman->create_table($table);
1152 /// Main savepoint reached
1153 upgrade_main_savepoint(true, 2009011303);
1156 if ($oldversion < 2009011900) {
1158 /// Define table sessions2 to be dropped
1159 $table = new xmldb_table('sessions2');
1161 /// Conditionally launch drop table for sessions
1162 if ($dbman->table_exists($table)) {
1163 $dbman->drop_table($table);
1166 /// Define table sessions to be dropped
1167 $table = new xmldb_table('sessions');
1169 /// Conditionally launch drop table for sessions
1170 if ($dbman->table_exists($table)) {
1171 $dbman->drop_table($table);
1174 /// Define table sessions to be created
1175 $table = new xmldb_table('sessions');
1177 /// Adding fields to table sessions
1178 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1179 $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1180 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
1181 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1182 $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
1183 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1184 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1185 $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1186 $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1188 /// Adding keys to table sessions
1189 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1190 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1192 /// Adding indexes to table sessions
1193 $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1194 $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1195 $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1196 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1198 /// Launch create table for sessions
1199 $dbman->create_table($table);
1201 /// Main savepoint reached
1202 upgrade_main_savepoint(true, 2009011900);
1205 if ($oldversion < 2009021800) {
1206 // Converting format of grade conditions, if any exist, to percentages.
1208 UPDATE {course_modules_availability} SET grademin=(
1209 SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
1210 /(gi.grademax-gi.grademin)
1211 FROM {grade_items} gi
1212 WHERE gi.id={course_modules_availability}.gradeitemid)
1213 WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1215 UPDATE {course_modules_availability} SET grademax=(
1216 SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
1217 /(gi.grademax-gi.grademin)
1218 FROM {grade_items} gi
1219 WHERE gi.id={course_modules_availability}.gradeitemid)
1220 WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1222 /// Main savepoint reached
1223 upgrade_main_savepoint(true, 2009021800);
1226 if ($oldversion < 2009021801) {
1227 /// Define field backuptype to be added to backup_log
1228 $table = new xmldb_table('backup_log');
1229 $field = new xmldb_field('backuptype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'info');
1230 /// Conditionally Launch add field backuptype and set all old records as 'scheduledbackup' records.
1231 if (!$dbman->field_exists($table, $field)) {
1232 $dbman->add_field($table, $field);
1233 $DB->execute("UPDATE {backup_log} SET backuptype='scheduledbackup'");
1236 /// Main savepoint reached
1237 upgrade_main_savepoint(true, 2009021801);
1240 /// Add default sort order for question types.
1241 if ($oldversion < 2009030300) {
1242 set_config('multichoice_sortorder', 1, 'question');
1243 set_config('truefalse_sortorder', 2, 'question');
1244 set_config('shortanswer_sortorder', 3, 'question');
1245 set_config('numerical_sortorder', 4, 'question');
1246 set_config('calculated_sortorder', 5, 'question');
1247 set_config('essay_sortorder', 6, 'question');
1248 set_config('match_sortorder', 7, 'question');
1249 set_config('randomsamatch_sortorder', 8, 'question');
1250 set_config('multianswer_sortorder', 9, 'question');
1251 set_config('description_sortorder', 10, 'question');
1252 set_config('random_sortorder', 11, 'question');
1253 set_config('missingtype_sortorder', 12, 'question');
1255 upgrade_main_savepoint(true, 2009030300);
1258 /// MDL-18132 replace the use a new Role allow switch settings page, instead of
1259 /// $CFG->allowuserswitchrolestheycantassign
1260 if ($oldversion < 2009032000) {
1261 /// First create the new table.
1262 $table = new xmldb_table('role_allow_switch');
1264 /// Adding fields to table role_allow_switch
1265 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1266 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1267 $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1269 /// Adding keys to table role_allow_switch
1270 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1271 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
1272 $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
1274 /// Adding indexes to table role_allow_switch
1275 $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1277 /// Conditionally launch create table for role_allow_switch
1278 if (!$dbman->table_exists($table)) {
1279 $dbman->create_table($table);
1282 /// Main savepoint reached
1283 upgrade_main_savepoint(true, 2009032000);
1286 if ($oldversion < 2009032001) {
1287 /// Copy from role_allow_assign into the new table.
1288 $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1289 SELECT roleid, allowassign FROM {role_allow_assign}');
1291 /// Unset the config variable used in 1.9.
1292 unset_config('allowuserswitchrolestheycantassign');
1294 /// Main savepoint reached
1295 upgrade_main_savepoint(true, 2009032001);
1298 if ($oldversion < 2009040300) {
1300 /// Define table filter_active to be created
1301 $table = new xmldb_table('filter_active');
1303 /// Adding fields to table filter_active
1304 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1305 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1306 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1307 $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1308 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1310 /// Adding keys to table filter_active
1311 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1312 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1314 /// Adding indexes to table filter_active
1315 $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1317 /// Conditionally launch create table for filter_active
1318 if (!$dbman->table_exists($table)) {
1319 $dbman->create_table($table);
1322 /// Main savepoint reached
1323 upgrade_main_savepoint(true, 2009040300);
1326 if ($oldversion < 2009040301) {
1328 /// Define table filter_config to be created
1329 $table = new xmldb_table('filter_config');
1331 /// Adding fields to table filter_config
1332 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1333 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1334 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1335 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1336 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1338 /// Adding keys to table filter_config
1339 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1340 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1342 /// Adding indexes to table filter_config
1343 $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1345 /// Conditionally launch create table for filter_config
1346 if (!$dbman->table_exists($table)) {
1347 $dbman->create_table($table);
1350 /// Main savepoint reached
1351 upgrade_main_savepoint(true, 2009040301);
1354 if ($oldversion < 2009040302) {
1355 /// Transfer current settings from $CFG->textfilters
1356 $disabledfilters = filter_get_all_installed();
1357 if (empty($CFG->textfilters)) {
1358 $activefilters = array();
1360 $activefilters = explode(',', $CFG->textfilters);
1362 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1364 foreach ($activefilters as $filter) {
1365 filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1367 unset($disabledfilters[$filter]);
1369 foreach ($disabledfilters as $filter => $notused) {
1370 filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1374 /// Main savepoint reached
1375 upgrade_main_savepoint(true, 2009040302);
1378 if ($oldversion < 2009040600) {
1379 /// Ensure that $CFG->stringfilters is set.
1380 if (empty($CFG->stringfilters)) {
1381 if (!empty($CFG->filterall)) {
1382 set_config('stringfilters', $CFG->textfilters);
1384 set_config('stringfilters', '');
1388 set_config('filterall', !empty($CFG->stringfilters));
1389 unset_config('textfilters');
1391 /// Main savepoint reached
1392 upgrade_main_savepoint(true, 2009040600);
1395 if ($oldversion < 2009041700) {
1396 /// To ensure the UI remains consistent with no behaviour change, any
1397 /// 'until' date in an activity condition should have 1 second subtracted
1398 /// (to go from 0:00 on the following day to 23:59 on the previous one).
1399 $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
1400 require_once($CFG->dirroot . '/course/lib.php');
1401 rebuild_course_cache(0, true);
1403 /// Main savepoint reached
1404 upgrade_main_savepoint(true, 2009041700);
1407 if ($oldversion < 2009042600) {
1408 /// Deleting orphaned messages from deleted users.
1409 require_once($CFG->dirroot.'/message/lib.php');
1410 /// Detect deleted users with messages sent(useridfrom) and not read
1411 if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
1413 JOIN {message} m ON m.useridfrom = u.id
1414 WHERE u.deleted = ?', array(1))) {
1415 foreach ($deletedusers as $deleteduser) {
1416 message_move_userfrom_unread2read($deleteduser->id); // move messages
1419 /// Main savepoint reached
1420 upgrade_main_savepoint(true, 2009042600);
1423 /// Dropping all enums/check contraints from core. MDL-18577
1424 if ($oldversion < 2009042700) {
1426 /// Changing list of values (enum) of field stattype on table stats_daily to none
1427 $table = new xmldb_table('stats_daily');
1428 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1430 /// Launch change of list of values for field stattype
1431 $dbman->drop_enum_from_field($table, $field);
1433 /// Changing list of values (enum) of field stattype on table stats_weekly to none
1434 $table = new xmldb_table('stats_weekly');
1435 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1437 /// Launch change of list of values for field stattype
1438 $dbman->drop_enum_from_field($table, $field);
1440 /// Changing list of values (enum) of field stattype on table stats_monthly to none
1441 $table = new xmldb_table('stats_monthly');
1442 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1444 /// Launch change of list of values for field stattype
1445 $dbman->drop_enum_from_field($table, $field);
1447 /// Changing list of values (enum) of field publishstate on table post to none
1448 $table = new xmldb_table('post');
1449 $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
1451 /// Launch change of list of values for field publishstate
1452 $dbman->drop_enum_from_field($table, $field);
1454 /// Main savepoint reached
1455 upgrade_main_savepoint(true, 2009042700);
1458 if ($oldversion < 2009043000) {
1459 unset_config('grade_report_showgroups');
1460 upgrade_main_savepoint(true, 2009043000);
1463 if ($oldversion < 2009050600) {
1464 /// Site front page blocks need to be moved due to page name change.
1465 $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
1467 /// Main savepoint reached
1468 upgrade_main_savepoint(true, 2009050600);
1471 if ($oldversion < 2009050601) {
1473 /// Define table block_instance to be renamed to block_instances
1474 $table = new xmldb_table('block_instance');
1476 /// Launch rename table for block_instance
1477 $dbman->rename_table($table, 'block_instances');
1479 /// Main savepoint reached
1480 upgrade_main_savepoint(true, 2009050601);
1483 if ($oldversion < 2009050602) {
1485 /// Define table block_instance to be renamed to block_instance_old
1486 $table = new xmldb_table('block_pinned');
1488 /// Launch rename table for block_instance
1489 $dbman->rename_table($table, 'block_pinned_old');
1491 /// Main savepoint reached
1492 upgrade_main_savepoint(true, 2009050602);
1495 if ($oldversion < 2009050603) {
1497 /// Define table block_instance_old to be created
1498 $table = new xmldb_table('block_instance_old');
1500 /// Adding fields to table block_instance_old
1501 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1502 $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1503 $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1504 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1505 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
1506 $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
1507 $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
1508 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1509 $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1511 /// Adding keys to table block_instance_old
1512 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1513 $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
1515 /// Adding indexes to table block_instance_old
1516 $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1517 $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
1519 /// Conditionally launch create table for block_instance_old
1520 if (!$dbman->table_exists($table)) {
1521 $dbman->create_table($table);
1524 /// Main savepoint reached
1525 upgrade_main_savepoint(true, 2009050603);
1528 if ($oldversion < 2009050604) {
1529 /// Copy current blocks data from block_instances to block_instance_old
1530 $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
1531 SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
1533 upgrade_main_savepoint(true, 2009050604);
1536 if ($oldversion < 2009050605) {
1538 /// Define field multiple to be dropped from block
1539 $table = new xmldb_table('block');
1540 $field = new xmldb_field('multiple');
1542 /// Conditionally launch drop field multiple
1543 if ($dbman->field_exists($table, $field)) {
1544 $dbman->drop_field($table, $field);
1547 /// Main savepoint reached
1548 upgrade_main_savepoint(true, 2009050605);
1551 if ($oldversion < 2009050606) {
1552 $table = new xmldb_table('block_instances');
1554 /// Rename field weight on table block_instances to defaultweight
1555 $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, 'position');
1556 $dbman->rename_field($table, $field, 'defaultweight');
1558 /// Rename field position on table block_instances to defaultregion
1559 $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
1560 $dbman->rename_field($table, $field, 'defaultregion');
1562 /// Main savepoint reached
1563 upgrade_main_savepoint(true, 2009050606);
1566 if ($oldversion < 2009050607) {
1567 /// Changing precision of field defaultregion on table block_instances to (16)
1568 $table = new xmldb_table('block_instances');
1569 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
1571 /// Launch change of precision for field defaultregion
1572 $dbman->change_field_precision($table, $field);
1574 /// Main savepoint reached
1575 upgrade_main_savepoint(true, 2009050607);
1578 if ($oldversion < 2009050608) {
1579 /// Change regions to the new notation
1580 $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1581 $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1582 $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1583 // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1584 // flex page course format. Hopefully this new value is an adequate alternative.
1586 /// Main savepoint reached
1587 upgrade_main_savepoint(true, 2009050608);
1590 if ($oldversion < 2009050609) {
1592 /// Define key blockname (unique) to be added to block
1593 $table = new xmldb_table('block');
1594 $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1596 /// Launch add key blockname
1597 $dbman->add_key($table, $key);
1599 /// Main savepoint reached
1600 upgrade_main_savepoint(true, 2009050609);
1603 if ($oldversion < 2009050610) {
1604 $table = new xmldb_table('block_instances');
1606 /// Define field blockname to be added to block_instances
1607 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
1608 if (!$dbman->field_exists($table, $field)) {
1609 $dbman->add_field($table, $field);
1612 /// Define field contextid to be added to block_instances
1613 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
1614 if (!$dbman->field_exists($table, $field)) {
1615 $dbman->add_field($table, $field);
1618 /// Define field showinsubcontexts to be added to block_instances
1619 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
1620 if (!$dbman->field_exists($table, $field)) {
1621 $dbman->add_field($table, $field);
1624 /// Define field subpagepattern to be added to block_instances
1625 $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
1626 if (!$dbman->field_exists($table, $field)) {
1627 $dbman->add_field($table, $field);
1630 /// Main savepoint reached
1631 upgrade_main_savepoint(true, 2009050610);
1634 if ($oldversion < 2009050611) {
1635 $table = new xmldb_table('block_instances');
1637 /// Fill in blockname from blockid
1638 $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1640 /// Set showinsubcontexts = 0 for all rows.
1641 $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1643 /// Main savepoint reached
1644 upgrade_main_savepoint(true, 2009050611);
1647 if ($oldversion < 2009050612) {
1649 /// Rename field pagetype on table block_instances to pagetypepattern
1650 $table = new xmldb_table('block_instances');
1651 $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
1653 /// Launch rename field pagetype
1654 $dbman->rename_field($table, $field, 'pagetypepattern');
1656 /// Main savepoint reached
1657 upgrade_main_savepoint(true, 2009050612);
1660 if ($oldversion < 2009050613) {
1661 /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1664 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
1665 $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1666 pagetypepattern = 'site-index',
1667 subpagepattern = NULL
1668 WHERE pagetypepattern = 'site-index'");
1671 $DB->execute("UPDATE {block_instances} SET
1675 JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1676 WHERE {course}.id = pageid
1678 pagetypepattern = 'course-view-*',
1679 subpagepattern = NULL
1680 WHERE pagetypepattern = 'course-view'");
1683 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1684 $DB->execute("UPDATE {block_instances} SET
1685 contextid = " . $syscontext->id . ",
1686 pagetypepattern = 'admin-*',
1687 subpagepattern = NULL
1688 WHERE pagetypepattern = 'admin'");
1691 $DB->execute("UPDATE {block_instances} SET
1695 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1696 WHERE {user}.id = pageid
1698 pagetypepattern = 'my-index',
1699 subpagepattern = NULL
1700 WHERE pagetypepattern = 'my-index'");
1703 $DB->execute("UPDATE {block_instances} SET
1704 contextid = " . $syscontext->id . ",
1705 pagetypepattern = 'tag-index',
1706 subpagepattern = pageid
1707 WHERE pagetypepattern = 'tag-index'");
1710 $DB->execute("UPDATE {block_instances} SET
1714 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1715 WHERE {user}.id = pageid
1717 pagetypepattern = 'blog-index',
1718 subpagepattern = NULL
1719 WHERE pagetypepattern = 'blog-view'");
1722 $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1723 foreach ($moduleswithblocks as $modname) {
1724 if (!$dbman->table_exists($modname)) {
1727 $DB->execute("UPDATE {block_instances} SET
1731 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1732 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1733 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1734 WHERE {{$modname}}.id = pageid
1736 pagetypepattern = 'blog-index',
1737 subpagepattern = NULL
1738 WHERE pagetypepattern = 'blog-view'");
1741 /// Main savepoint reached
1742 upgrade_main_savepoint(true, 2009050613);
1745 if ($oldversion < 2009050614) {
1746 /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
1747 $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
1749 /// Main savepoint reached
1750 upgrade_main_savepoint(true, 2009050614);
1753 if ($oldversion < 2009050615) {
1754 $table = new xmldb_table('block_instances');
1756 /// Arrived here, any block_instances record without blockname is one
1757 /// orphan block coming from 1.9. Just delete them. MDL-22503
1758 $DB->delete_records_select('block_instances', 'blockname IS NULL');
1760 /// Changing nullability of field blockname on table block_instances to not null
1761 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
1762 $dbman->change_field_notnull($table, $field);
1764 /// Changing nullability of field contextid on table block_instances to not null
1765 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
1766 $dbman->change_field_notnull($table, $field);
1768 /// Changing nullability of field showinsubcontexts on table block_instances to not null
1769 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
1770 $dbman->change_field_notnull($table, $field);
1772 /// Main savepoint reached
1773 upgrade_main_savepoint(true, 2009050615);
1776 if ($oldversion < 2009050616) {
1777 /// Add exiting sticky blocks.
1778 $blocks = $DB->get_records('block');
1779 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1780 $newregions = array(
1783 'c' => 'course-view-top',
1785 $stickyblocks = $DB->get_recordset('block_pinned_old');
1786 foreach ($stickyblocks as $stickyblock) {
1787 // Only if the block exists (avoid orphaned sticky blocks)
1788 if (!isset($blocks[$stickyblock->blockid]) || empty($blocks[$stickyblock->blockid]->name)) {
1791 $newblock = new stdClass();
1792 $newblock->blockname = $blocks[$stickyblock->blockid]->name;
1793 $newblock->contextid = $syscontext->id;
1794 $newblock->showinsubcontexts = 1;
1795 switch ($stickyblock->pagetype) {
1797 $newblock->pagetypepattern = 'course-view-*';
1800 $newblock->pagetypepattern = $stickyblock->pagetype;
1802 $newblock->defaultregion = $newregions[$stickyblock->position];
1803 $newblock->defaultweight = $stickyblock->weight;
1804 $newblock->configdata = $stickyblock->configdata;
1805 $newblock->visible = 1;
1806 $DB->insert_record('block_instances', $newblock);
1809 /// Main savepoint reached
1810 upgrade_main_savepoint(true, 2009050616);
1813 if ($oldversion < 2009050617) {
1815 /// Define table block_positions to be created
1816 $table = new xmldb_table('block_positions');
1818 /// Adding fields to table block_positions
1819 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1820 $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1821 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1822 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1823 $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1824 $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1825 $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1826 $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1828 /// Adding keys to table block_positions
1829 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1830 $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
1831 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1833 /// Adding indexes to table block_positions
1834 $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
1836 /// Conditionally launch create table for block_positions
1837 if (!$dbman->table_exists($table)) {
1838 $dbman->create_table($table);
1841 /// Main savepoint reached
1842 upgrade_main_savepoint(true, 2009050617);
1845 if ($oldversion < 2009050618) {
1846 /// And block instances with visible = 0, copy that information to block_positions
1847 $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
1848 SELECT bi.id, bi.contextid,
1849 CASE WHEN bi.pagetypepattern = 'course-view-*'
1850 THEN (SELECT " . $DB->sql_concat("'course-view-'", 'c.format') . "
1852 JOIN {context} ctx ON c.id = ctx.instanceid
1853 WHERE ctx.id = bi.contextid)
1854 ELSE bi.pagetypepattern END,
1855 CASE WHEN bi.subpagepattern IS NULL
1857 ELSE bi.subpagepattern END,
1858 0, bi.defaultregion, bi.defaultweight
1859 FROM {block_instances} bi
1860 WHERE bi.visible = 0 AND bi.pagetypepattern <> 'admin-*' AND bi.pagetypepattern IS NOT NULL");
1861 // note: MDL-25031 all block instances should have a pagetype pattern, NULL is not allowed,
1862 // if we manage to find out how NULLs get there we should fix them before this step
1864 /// Main savepoint reached
1865 upgrade_main_savepoint(true, 2009050618);
1868 if ($oldversion < 2009050619) {
1869 $table = new xmldb_table('block_instances');
1871 /// Define field blockid to be dropped from block_instances
1872 $field = new xmldb_field('blockid');
1873 if ($dbman->field_exists($table, $field)) {
1874 /// Before dropping the field, drop dependent indexes
1875 $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
1876 if ($dbman->index_exists($table, $index)) {
1877 /// Launch drop index blockid
1878 $dbman->drop_index($table, $index);
1880 $dbman->drop_field($table, $field);
1883 /// Define field pageid to be dropped from block_instances
1884 $field = new xmldb_field('pageid');
1885 if ($dbman->field_exists($table, $field)) {
1886 /// Before dropping the field, drop dependent indexes
1887 $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1888 if ($dbman->index_exists($table, $index)) {
1889 /// Launch drop index pageid
1890 $dbman->drop_index($table, $index);
1892 $dbman->drop_field($table, $field);
1895 /// Define field visible to be dropped from block_instances
1896 $field = new xmldb_field('visible');
1897 if ($dbman->field_exists($table, $field)) {
1898 $dbman->drop_field($table, $field);
1901 /// Main savepoint reached
1902 upgrade_main_savepoint(true, 2009050619);
1905 if ($oldversion < 2009051200) {
1906 /// Let's check the status of mandatory mnet_host records, fixing them
1907 /// and moving "orphan" users to default localhost record. MDL-16879
1908 echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
1909 upgrade_fix_incorrect_mnethostids();
1911 /// Main savepoint reached
1912 upgrade_main_savepoint(true, 2009051200);
1916 if ($oldversion < 2009051700) {
1917 /// migrate editor settings
1918 if (empty($CFG->htmleditor)) {
1919 set_config('texteditors', 'textarea');
1921 set_config('texteditors', 'tinymce,textarea');
1924 unset_config('htmleditor');
1925 unset_config('defaulthtmleditor');
1927 /// Main savepoint reached
1928 upgrade_main_savepoint(true, 2009051700);
1931 /// Repeat 2009050607 upgrade step, which Petr commented out because of XMLDB
1932 /// stupidity, so lots of people will have missed.
1933 if ($oldversion < 2009061600) {
1934 /// Changing precision of field defaultregion on table block_instances to (16)
1935 $table = new xmldb_table('block_instances');
1936 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
1938 /// Launch change of precision for field defaultregion
1939 $dbman->change_field_precision($table, $field);
1941 /// Main savepoint reached
1942 upgrade_main_savepoint(true, 2009061600);
1945 if ($oldversion < 2009061702) {
1946 // standardizing plugin names
1947 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
1948 foreach ($configs as $config) {
1949 unset_config($config->name, $config->plugin); /// unset old config
1950 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
1951 set_config($config->name, $config->value, $config->plugin); /// set new config
1955 upgrade_main_savepoint(true, 2009061702);
1958 if ($oldversion < 2009061703) {
1959 // standardizing plugin names
1960 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
1961 foreach ($configs as $config) {
1962 unset_config($config->name, $config->plugin); /// unset old config
1963 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
1964 set_config($config->name, $config->value, $config->plugin); /// set new config
1968 upgrade_main_savepoint(true, 2009061703);
1971 if ($oldversion < 2009061704) {
1972 // change component string in capability records to new "_" format
1973 if ($caps = $DB->get_records('capabilities')) {
1974 foreach ($caps as $cap) {
1975 $cap->component = str_replace('/', '_', $cap->component);
1976 $DB->update_record('capabilities', $cap);
1980 upgrade_main_savepoint(true, 2009061704);
1983 if ($oldversion < 2009063000) {
1984 // upgrade format of _with_advanced settings - quiz only
1985 // note: this can be removed later, not needed for upgrades from 1.9.x
1986 if ($quiz = get_config('quiz')) {
1987 foreach ($quiz as $name=>$value) {
1988 if (strpos($name, 'fix_') !== 0) {
1991 $newname = substr($name,4).'_adv';
1992 set_config($newname, $value, 'quiz');
1993 unset_config($name, 'quiz');
1996 upgrade_main_savepoint(true, 2009063000);
1999 if ($oldversion < 2009071000) {
2001 /// Rename field contextid on table block_instances to parentcontextid
2002 $table = new xmldb_table('block_instances');
2003 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
2005 /// Launch rename field parentcontextid
2006 $dbman->rename_field($table, $field, 'parentcontextid');
2008 /// Main savepoint reached
2009 upgrade_main_savepoint(true, 2009071000);
2012 if ($oldversion < 2009071600) {
2014 /// Define field summaryformat to be added to post
2015 $table = new xmldb_table('post');
2016 $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
2018 /// Conditionally launch add field summaryformat
2019 if (!$dbman->field_exists($table, $field)) {
2020 $dbman->add_field($table, $field);
2023 /// Main savepoint reached
2024 upgrade_main_savepoint(true, 2009071600);
2027 if ($oldversion < 2009072400) {
2029 /// Define table comments to be created
2030 $table = new xmldb_table('comments');
2032 /// Adding fields to table comments
2033 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2034 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2035 $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2036 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2037 $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2038 $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2039 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2040 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2042 /// Adding keys to table comments
2043 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2045 /// Conditionally launch create table for comments
2046 if (!$dbman->table_exists($table)) {
2047 $dbman->create_table($table);
2050 /// Main savepoint reached
2051 upgrade_main_savepoint(true, 2009072400);
2055 * This upgrade is to set up the new navigation blocks that have been developed
2056 * as part of Moodle 2.0
2057 * Now I [Sam Hemelryk] hit a conundrum while exploring how to go about this
2058 * as not only do we want to install the new blocks but we also want to set up
2059 * default instances of them, and at the same time remove instances of the blocks
2060 * that were/will-be outmoded by the two new navigation blocks.
2061 * After talking it through with Tim Hunt {@link http://moodle.org/mod/cvsadmin/view.php?conversationid=3112}
2062 * we decided that the best way to go about this was to put the bulk of the
2063 * upgrade operation into core upgrade `here` but to let the plugins block
2064 * still install the blocks.
2065 * This leaves one hairy end in that we will create block_instances within the
2066 * DB before the blocks themselves are created within the DB
2068 if ($oldversion < 2009082800) {
2070 echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2072 // Get the system context so we can set the block instances to it
2073 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2075 // An array to contain the new block instances we will create
2076 $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
2077 // The new global navigation block instance as a stdClass
2078 $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
2079 $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
2080 $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutely everywhere
2081 $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
2082 $newblockinstances['globalnavigation']->subpagetypepattern = null;
2083 $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
2084 $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
2085 $newblockinstances['globalnavigation']->configdata = '';
2086 // The new settings navigation block instance as a stdClass
2087 $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
2088 $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
2089 $newblockinstances['settingsnavigation']->showinsubcontexts = true;
2090 $newblockinstances['settingsnavigation']->pagetypepattern = '*';
2091 $newblockinstances['settingsnavigation']->subpagetypepattern = null;
2092 $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
2093 $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
2094 $newblockinstances['settingsnavigation']->configdata = '';
2096 // Blocks that are outmoded and for whom the bells will toll... by which I
2097 // mean we will delete all instances of
2098 $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
2099 $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
2100 unset($outmodedblocks);
2101 // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
2102 // cut down the number of delete queries we will need to run
2103 $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
2105 $contextids = array();
2106 $instanceids = array();
2107 // Iterate through all block instances
2108 foreach ($allblockinstances as $blockinstance) {
2109 if (!in_array($blockinstance->parentcontextid, $contextids)) {
2110 $contextids[] = $blockinstance->parentcontextid;
2112 // If we have over 1000 contexts clean them up and reset the array
2113 // this ensures we don't hit any nasty memory limits or such
2114 if (count($contextids) > 1000) {
2115 upgrade_cleanup_unwanted_block_contexts($contextids);
2116 $contextids = array();
2119 if (!in_array($blockinstance->id, $instanceids)) {
2120 $instanceids[] = $blockinstance->id;
2121 // If we have more than 1000 block instances now remove all block positions
2122 // and empty the array
2123 if (count($contextids) > 1000) {
2124 $instanceidstring = join(',',$instanceids);
2125 $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2126 $instanceids = array();
2131 upgrade_cleanup_unwanted_block_contexts($contextids);
2133 $instanceidstring = join(',',$instanceids);
2134 $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2136 unset($allblockinstances);
2138 unset($instanceids);
2139 unset($instanceidstring);
2141 // Now remove the actual block instance
2142 $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
2143 unset($outmodedblocksstring);
2145 // Insert the new block instances. Remember they have not been installed yet
2146 // however this should not be a problem
2147 foreach ($newblockinstances as $blockinstance) {
2148 $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
2149 // Ensure the block context is created.
2150 get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
2152 unset($newblockinstances);
2154 upgrade_main_savepoint(true, 2009082800);
2155 // The end of the navigation upgrade
2158 if ($oldversion < 2009100602) {
2159 /// Define table external_functions to be created
2160 $table = new xmldb_table('external_functions');
2162 /// Adding fields to table external_functions
2163 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2164 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2165 $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2166 $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2167 $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2168 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2170 /// Adding keys to table external_functions
2171 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2173 /// Adding indexes to table external_functions
2174 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2176 /// Launch create table for external_functions
2177 $dbman->create_table($table);
2179 /// Main savepoint reached
2180 upgrade_main_savepoint(true, 2009100602);
2183 if ($oldversion < 2009100603) {
2184 /// Define table external_services to be created
2185 $table = new xmldb_table('external_services');
2187 /// Adding fields to table external_services
2188 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2189 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2190 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2191 $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
2192 $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2193 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
2194 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2195 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2197 /// Adding keys to table external_services
2198 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2200 /// Adding indexes to table external_services
2201 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2203 /// Launch create table for external_services
2204 $dbman->create_table($table);
2206 /// Main savepoint reached
2207 upgrade_main_savepoint(true, 2009100603);
2210 if ($oldversion < 2009100604) {
2211 /// Define table external_services_functions to be created
2212 $table = new xmldb_table('external_services_functions');
2214 /// Adding fields to table external_services_functions
2215 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2216 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2217 $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2219 /// Adding keys to table external_services_functions
2220 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2221 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2223 /// Launch create table for external_services_functions
2224 $dbman->create_table($table);
2226 /// Main savepoint reached
2227 upgrade_main_savepoint(true, 2009100604);
2230 if ($oldversion < 2009100605) {
2231 /// Define table external_services_users to be created
2232 $table = new xmldb_table('external_services_users');
2234 /// Adding fields to table external_services_users
2235 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2236 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2237 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2238 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2239 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2240 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2242 /// Adding keys to table external_services_users
2243 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2244 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2245 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2247 /// Launch create table for external_services_users
2248 $dbman->create_table($table);
2250 /// Main savepoint reached
2251 upgrade_main_savepoint(true, 2009100605);
2254 if ($oldversion < 2009102600) {
2256 /// Define table external_tokens to be created
2257 $table = new xmldb_table('external_tokens');
2259 /// Adding fields to table external_tokens
2260 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2261 $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
2262 $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2263 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2264 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2265 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
2266 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2267 $table->add_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
2268 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2269 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2270 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2271 $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2273 /// Adding keys to table external_tokens
2274 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2275 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2276 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2277 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2278 $table->add_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
2280 /// Launch create table for external_tokens
2281 $dbman->create_table($table);
2283 /// Main savepoint reached
2284 upgrade_main_savepoint(true, 2009102600);
2287 if ($oldversion < 2009103000) {
2289 /// Define table blog_association to be created
2290 $table = new xmldb_table('blog_association');
2292 /// Adding fields to table blog_association
2293 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2294 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2295 $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2297 /// Adding keys to table blog_association
2298 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2299 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2300 $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
2302 /// Conditionally launch create table for blog_association
2303 if (!$dbman->table_exists($table)) {
2304 $dbman->create_table($table);
2307 /// Define table blog_external to be created
2308 $table = new xmldb_table('blog_external');
2310 /// Adding fields to table blog_external
2311 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2312 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2313 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2314 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2315 $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2316 $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2317 $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2318 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2319 $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2321 /// Adding keys to table blog_external
2322 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2323 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2325 /// Conditionally launch create table for blog_external
2326 if ($dbman->table_exists($table)) {
2327 // Delete the existing one first (comes from early dev version)
2328 $dbman->drop_table($table);
2330 $dbman->create_table($table);
2332 // now inform admins that some settings require attention after upgrade
2333 if (($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete)) {
2334 echo $OUTPUT->notification(get_string('bloglevelupgradenotice', 'admin'));
2339 $a->sitename = $site->fullname;
2340 $a->fixurl = "$CFG->wwwroot/$CFG->admin/bloglevelupgrade.php";
2342 $subject = get_string('bloglevelupgrade', 'admin');
2343 $description = get_string('bloglevelupgradedescription', 'admin', $a);
2345 // can not use messaging here because it is not configured yet!
2346 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2348 /// Main savepoint reached
2349 upgrade_main_savepoint(true, 2009103000);
2352 if ($oldversion < 2009110400) {
2353 // list of tables where we need to add new format field and convert texts
2354 $extendtables = array('course' => 'summary',
2355 'course_categories' => 'description',
2356 'course_categories' => 'description',
2357 'course_request' => 'summary',
2358 'grade_outcomes' => 'description',
2359 'groups' => 'description',
2360 'groupings' => 'description',
2361 'scale' => 'description',
2362 'user_info_field' => 'description',
2363 'user_info_field' => 'defaultdata',
2364 'user_info_data' => 'data');
2366 foreach ($extendtables as $tablestr => $fieldstr) {
2367 $formatfieldstr = $fieldstr.'format';
2369 $table = new xmldb_table($tablestr);
2370 $field = new xmldb_field($formatfieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', $fieldstr);
2371 // Check that the field doesn't already exists
2372 if (!$dbman->field_exists($table, $field)) {
2373 // Add the new field
2374 $dbman->add_field($table, $field);
2376 if ($CFG->texteditors !== 'textarea') {
2377 $rs = $DB->get_recordset($tablestr, array($formatfieldstr => FORMAT_MOODLE), '', "id,$fieldstr,$formatfieldstr");
2378 foreach ($rs as $rec) {
2379 $rec->$fieldstr = text_to_html($rec->$fieldstr, false, false, true);
2380 $rec->$formatfieldstr = FORMAT_HTML;
2381 $DB->update_record($tablestr, $rec);
2382 upgrade_set_timeout();
2390 unset($extendtables);
2392 upgrade_main_savepoint(true, 2009110400);
2395 if ($oldversion < 2009110401) {
2396 $table = new xmldb_table('user');
2398 // Change the precision of the description field first up.
2400 $field = new xmldb_field('description', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'url');
2401 $dbman->change_field_precision($table, $field);
2403 $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'description');
2404 // Check that the field doesn't already exists
2405 if (!$dbman->field_exists($table, $field)) {
2406 // Add the new field
2407 $dbman->add_field($table, $field);
2409 if ($CFG->texteditors !== 'textarea') {
2410 $rs = $DB->get_recordset('user', array('descriptionformat'=>FORMAT_MOODLE, 'deleted'=>0, 'htmleditor'=>1), '', "id,description,descriptionformat");
2411 foreach ($rs as $rec) {
2412 $rec->description = text_to_html($rec->description, false, false, true);
2413 $rec->descriptionformat = FORMAT_HTML;
2414 $DB->update_record('user', $rec);
2415 upgrade_set_timeout();
2420 upgrade_main_savepoint(true, 2009110401);
2423 if ($oldversion < 2009112400) {
2424 if (empty($CFG->passwordsaltmain)) {
2425 $subject = get_string('check_passwordsaltmain_name', 'report_security');
2426 $description = get_string('check_passwordsaltmain_warning', 'report_security');;
2427 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2429 upgrade_main_savepoint(true, 2009112400);
2432 if ($oldversion < 2010011200) {
2433 $table = new xmldb_table('grade_categories');
2434 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timemodified');
2436 if (!$dbman->field_exists($table, $field)) {
2437 $dbman->add_field($table, $field);
2440 upgrade_main_savepoint(true, 2010011200);
2443 if ($oldversion < 2010012500) {
2444 upgrade_fix_incorrect_mnethostids();
2445 upgrade_main_savepoint(true, 2010012500);
2448 if ($oldversion < 2010012600) {
2449 // do stuff to the mnet table
2450 $table = new xmldb_table('mnet_rpc');
2452 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2453 $dbman->rename_field($table, $field, 'plugintype');
2455 $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2456 $dbman->rename_field($table, $field, 'pluginname');
2458 $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
2459 if (!$dbman->field_exists($table, $field)) {
2460 $dbman->add_field($table, $field);
2463 $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
2464 if (!$dbman->field_exists($table, $field)) {
2465 $dbman->add_field($table, $field);
2468 $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
2469 if (!$dbman->field_exists($table, $field)) {
2470 $dbman->add_field($table, $field);
2473 /// Main savepoint reached
2474 upgrade_main_savepoint(true, 2010012600);
2477 if ($oldversion < 2010012900) {
2479 /// Define table mnet_remote_rpc to be created
2480 $table = new xmldb_table('mnet_remote_rpc');
2482 /// Adding fields to table mnet_remote_rpc
2483 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2484 $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2485 $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
2487 /// Adding keys to table mnet_remote_rpc
2488 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2490 /// Conditionally launch create table for mnet_remote_rpc
2491 if (!$dbman->table_exists($table)) {
2492 $dbman->create_table($table);
2496 /// Define table mnet_remote_service2rpc to be created
2497 $table = new xmldb_table('mnet_remote_service2rpc');
2499 /// Adding fields to table mnet_remote_service2rpc
2500 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2501 $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2502 $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2504 /// Adding keys to table mnet_remote_service2rpc
2505 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2507 /// Adding indexes to table mnet_remote_service2rpc
2508 $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2510 /// Conditionally launch create table for mnet_remote_service2rpc
2511 if (!$dbman->table_exists($table)) {
2512 $dbman->create_table($table);
2516 /// Rename field function_name on table mnet_rpc to functionname
2517 $table = new xmldb_table('mnet_rpc');
2518 $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
2520 /// Launch rename field function_name
2521 $dbman->rename_field($table, $field, 'functionname');
2524 /// Rename field xmlrpc_path on table mnet_rpc to xmlrpcpath
2525 $table = new xmldb_table('mnet_rpc');
2526 $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
2528 /// Launch rename field xmlrpc_path
2529 $dbman->rename_field($table, $field, 'xmlrpcpath');
2532 /// Main savepoint reached
2533 upgrade_main_savepoint(true, 2010012900);
2536 if ($oldversion < 2010012901) {
2538 /// Define field plugintype to be added to mnet_remote_rpc
2539 $table = new xmldb_table('mnet_remote_rpc');
2540 $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
2542 /// Conditionally launch add field plugintype
2543 if (!$dbman->field_exists($table, $field)) {
2544 $dbman->add_field($table, $field);
2547 /// Define field pluginname to be added to mnet_remote_rpc
2548 $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
2550 /// Conditionally launch add field pluginname
2551 if (!$dbman->field_exists($table, $field)) {
2552 $dbman->add_field($table, $field);
2555 /// Main savepoint reached
2556 upgrade_main_savepoint(true, 2010012901);
2559 if ($oldversion < 2010012902) {
2561 /// Define field enabled to be added to mnet_remote_rpc
2562 $table = new xmldb_table('mnet_remote_rpc');
2563 $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
2565 /// Conditionally launch add field enabled
2566 if (!$dbman->field_exists($table, $field)) {
2567 $dbman->add_field($table, $field);
2570 /// Main savepoint reached
2571 upgrade_main_savepoint(true, 2010012902);
2574 /// MDL-17863. Increase the portno column length on mnet_host to handle any port number
2575 if ($oldversion < 2010020100) {
2576 /// Changing precision of field portno on table mnet_host to (5)
2577 $table = new xmldb_table('mnet_host');
2578 $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
2580 /// Launch change of precision for field portno
2581 $dbman->change_field_precision($table, $field);
2583 upgrade_main_savepoint(true, 2010020100);
2586 if ($oldversion < 2010020300) {
2588 /// Define field timecreated to be added to user
2589 $table = new xmldb_table('user');
2590 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
2592 if (!$dbman->field_exists($table, $field)) {
2593 /// Launch add field timecreated
2594 $dbman->add_field($table, $field);
2596 $DB->execute("UPDATE {user} SET timecreated = firstaccess");
2598 $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
2601 upgrade_main_savepoint(true, 2010020300);
2604 // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
2605 if ($oldversion < 2010020301) {
2606 if ($preamble = $CFG->filter_tex_latexpreamble) {
2607 $preamble = preg_replace('/^ +/m', '', $preamble);
2608 set_config('filter_tex_latexpreamble', $preamble);
2610 upgrade_main_savepoint(true, 2010020301);
2613 if ($oldversion < 2010021400) {
2614 /// Changes to modinfo mean we need to rebuild course cache
2615 require_once($CFG->dirroot . '/course/lib.php');
2616 rebuild_course_cache(0, true);
2617 upgrade_main_savepoint(true, 2010021400);
2620 if ($oldversion < 2010021800) {
2621 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
2622 upgrade_main_savepoint(true, 2010021800);
2625 if ($oldversion < 2010031900) {
2626 // regeneration of sessions is always enabled, no need for this setting any more
2627 unset_config('regenloginsession');
2628 upgrade_main_savepoint(true, 2010031900);
2631 if ($oldversion < 2010033101.02) {
2633 /// Define table license to be created
2634 $table = new xmldb_table('license');
2636 /// Adding fields to table license
2637 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2638 $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2639 $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2640 $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2641 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
2642 $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2644 /// Adding keys to table license
2645 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2647 /// Conditionally launch create table for license
2648 if (!$dbman->table_exists($table)) {
2649 $dbman->create_table($table);
2651 $active_licenses = array();
2653 $license = new stdClass();
2655 // add unknown license
2656 $license->shortname = 'unknown';
2657 $license->fullname = 'Unknown license';
2658 $license->source = '';
2659 $license->enabled = 1;
2660 $license->version = '2010033100';
2661 $active_licenses[] = $license->shortname;
2662 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2663 if ($record->version < $license->version) {
2664 // update license record
2665 $license->enabled = $record->enabled;
2666 $license->id = $record->id;
2667 $DB->update_record('license', $license);
2670 $DB->insert_record('license', $license);
2673 // add all rights reserved license
2674 $license->shortname = 'allrightsreserved';
2675 $license->fullname = 'All rights reserved';
2676 $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
2677 $license->enabled = 1;
2678 $license->version = '2010033100';
2679 $active_licenses[] = $license->shortname;
2680 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2681 if ($record->version < $license->version) {
2682 // update license record
2683 $license->id = $record->id;
2684 $license->enabled = $record->enabled;
2685 $DB->update_record('license', $license);
2688 $DB->insert_record('license', $license);
2691 // add public domain license
2692 $license->shortname = 'public';
2693 $license->fullname = 'Public Domain';
2694 $license->source = 'http://creativecommons.org/licenses/publicdomain/';
2695 $license->enabled = 1;
2696 $license->version = '2010033100';
2697 $active_licenses[] = $license->shortname;
2698 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2699 if ($record->version < $license->version) {
2700 // update license record
2701 $license->enabled = $record->enabled;
2702 $license->id = $record->id;
2703 $DB->update_record('license', $license);
2706 $DB->insert_record('license', $license);
2709 // add creative commons license
2710 $license->shortname = 'cc';
2711 $license->fullname = 'Creative Commons';
2712 $license->source = 'http://creativecommons.org/licenses/by/3.0/';
2713 $license->enabled = 1;
2714 $license->version = '2010033100';
2715 $active_licenses[] = $license->shortname;
2716 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2717 if ($record->version < $license->version) {
2718 // update license record
2719 $license->enabled = $record->enabled;
2720 $license->id = $record->id;
2721 $DB->update_record('license', $license);
2724 $DB->insert_record('license', $license);
2727 // add creative commons no derivs license
2728 $license->shortname = 'cc-nd';
2729 $license->fullname = 'Creative Commons - NoDerivs';
2730 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
2731 $license->enabled = 1;
2732 $license->version = '2010033100';
2733 $active_licenses[] = $license->shortname;
2734 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2735 if ($record->version < $license->version) {
2736 // update license record
2737 $license->enabled = $record->enabled;
2738 $license->id = $record->id;
2739 $DB->update_record('license', $license);
2742 $DB->insert_record('license', $license);
2745 // add creative commons no commercial no derivs license
2746 $license->shortname = 'cc-nc-nd';
2747 $license->fullname = 'Creative Commons - No Commercial NoDerivs';
2748 $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
2749 $license->enabled = 1;
2750 $license->version = '2010033100';
2751 $active_licenses[] = $license->shortname;
2752 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2753 if ($record->version < $license->version) {
2754 // update license record
2755 $license->enabled = $record->enabled;
2756 $license->id = $record->id;
2757 $DB->update_record('license', $license);
2760 $DB->insert_record('license', $license);
2763 // add creative commons no commercial
2764 $license->shortname = 'cc-nc-nd';
2765 $license->shortname = 'cc-nc';
2766 $license->fullname = 'Creative Commons - No Commercial';
2767 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
2768 $license->enabled = 1;
2769 $license->version = '2010033100';
2770 $active_licenses[] = $license->shortname;
2771 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2772 if ($record->version < $license->version) {
2773 // update license record
2774 $license->enabled = $record->enabled;
2775 $license->id = $record->id;
2776 $DB->update_record('license', $license);
2779 $DB->insert_record('license', $license);
2782 // add creative commons no commercial sharealike
2783 $license->shortname = 'cc-nc-sa';
2784 $license->fullname = 'Creative Commons - No Commercial ShareAlike';
2785 $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
2786 $license->enabled = 1;
2787 $license->version = '2010033100';
2788 $active_licenses[] = $license->shortname;
2789 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2790 if ($record->version < $license->version) {
2791 // update license record
2792 $license->enabled = $record->enabled;
2793 $license->id = $record->id;
2794 $DB->update_record('license', $license);
2797 $DB->insert_record('license', $license);
2800 // add creative commons sharealike
2801 $license->shortname = 'cc-sa';
2802 $license->fullname = 'Creative Commons - ShareAlike';
2803 $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
2804 $license->enabled = 1;
2805 $license->version = '2010033100';
2806 $active_licenses[] = $license->shortname;
2807 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2808 if ($record->version < $license->version) {
2809 // update license record
2810 $license->enabled = $record->enabled;
2811 $license->id = $record->id;
2812 $DB->update_record('license', $license);
2815 $DB->insert_record('license', $license);
2818 set_config('licenses', implode(',', $active_licenses));
2819 /// set site default license
2820 set_config('sitedefaultlicense', 'allrightsreserved');
2822 /// Main savepoint reached
2823 upgrade_main_savepoint(true, 2010033101.02);
2826 if ($oldversion < 2010033102.00) {
2827 // rename course view capability to participate
2828 $params = array('view'=>'moodle/course:view', 'participate'=>'moodle/course:participate');
2829 $sql = "UPDATE {role_capabilities} SET capability = :participate WHERE capability = :view";
2830 $DB->execute($sql, $params);
2831 $sql = "UPDATE {capabilities} SET name = :participate WHERE name = :view";
2832 $DB->execute($sql, $params);
2833 // note: the view capability is readded again at the end of upgrade, but with different meaning
2834 upgrade_main_savepoint(true, 2010033102.00);
2837 if ($oldversion < 2010033102.01) {
2838 // Define field archetype to be added to role table
2839 $table = new xmldb_table('role');
2840 $field = new xmldb_field('archetype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'sortorder');
2841 $dbman->add_field($table, $field);
2842 upgrade_main_savepoint(true, 2010033102.01);
2845 if ($oldversion < 2010033102.02) {
2846 // Set archetype for existing roles and change admin role to manager role
2847 $sql = "SELECT r.*, rc.capability
2849 JOIN {role_capabilities} rc ON rc.roleid = r.id
2850 WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
2852 $params = array('syscontextid'=>SYSCONTEXTID, 'legacycaps'=>'moodle/legacy:%');
2853 $substart = strlen('moodle/legacy:');
2854 $roles = $DB->get_recordset_sql($sql, $params); // in theory could be multiple legacy flags in one role
2855 foreach ($roles as $role) {
2856 $role->archetype = substr($role->capability, $substart);
2857 unset($role->capability);
2858 if ($role->archetype === 'admin') {
2860 if ($DB->record_exists('role', array('shortname'=>'manager'))) {
2862 while($DB->record_exists('role', array('shortname'=>'manager'.$i))) {
2866 $role->archetype = 'manager';
2867 if ($role->shortname === 'admin') {
2868 $role->shortname = 'manager'.$i;
2869 $role->name = get_string('manager', 'role');
2870 $role->description = get_string('managerdescription', 'role');
2873 $DB->update_record('role', $role);
2877 upgrade_main_savepoint(true, 2010033102.02);
2880 if ($oldversion < 2010033102.03) {
2881 // Now pick site admins (===have manager role assigned at the system context)
2882 // and store them in the new $CFG->siteadmins setting as comma separated list
2883 $sql = "SELECT ra.id, ra.userid
2884 FROM {role_assignments} ra
2885 JOIN {role} r ON r.id = ra.roleid
2886 JOIN {user} u ON u.id = ra.userid
2887 WHERE ra.contextid = :syscontext AND r.archetype = 'manager' AND u.deleted = 0
2889 $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
2891 foreach ($ras as $ra) {
2892 $admins[$ra->userid] = $ra->userid;
2893 set_config('siteadmins', implode(',', $admins)); // better to save it repeatedly, we do need at least one admin
2894 $DB->delete_records('role_assignments', array('id'=>$ra->id));
2897 upgrade_main_savepoint(true, 2010033102.03);
2900 if ($oldversion < 2010033102.04) {
2901 // clean up the manager roles
2902 $managers = $DB->get_records('role', array('archetype'=>'manager'));
2903 foreach ($managers as $manager) {
2904 // now sanitize the capabilities and overrides
2905 $DB->delete_records('role_capabilities', array('capability'=>'moodle/site:config', 'roleid'=>$manager->id)); // only site admins may configure servers
2906 // note: doanything and legacy caps are deleted automatically, they get moodle/course:view later at the end of the upgrade
2908 // remove manager role assignments bellow the course context level - admin role was never intended for activities and blocks,
2909 // the problem is that those assignments would not be visible after upgrade and old style admins in activities make no sense anyway
2910 $DB->delete_records_select('role_assignments', "roleid = :manager AND contextid IN (SELECT id FROM {context} WHERE contextlevel > 50)", array('manager'=>$manager->id));
2912 // allow them to assign all roles except default user, guest and frontpage - users get these roles automatically on the fly when needed
2913 $DB->delete_records('role_allow_assign', array('roleid'=>$manager->id));
2914 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype <> 'user' AND archetype <> 'guest' AND archetype <> 'frontpage'");
2915 foreach ($roles as $role) {
2916 $record = (object)array('roleid'=>$manager->id, 'allowassign'=>$role->id);
2917 $DB->insert_record('role_allow_assign', $record);
2920 // allow them to override all roles
2921 $DB->delete_records('role_allow_override', array('roleid'=>$manager->id));
2922 $roles = $DB->get_records_sql("SELECT * FROM {role}");
2923 foreach ($roles as $role) {
2924 $record = (object)array('roleid'=>$manager->id, 'allowoverride'=>$role->id);
2925 $DB->insert_record('role_allow_override', $record);
2928 // allow them to switch to all following roles
2929 $DB->delete_records('role_allow_switch', array('roleid'=>$manager->id));
2930 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype IN ('student', 'teacher', 'editingteacher')");
2931 foreach ($roles as $role) {
2932 $record = (object)array('roleid'=>$manager->id, 'allowswitch'=>$role->id);
2933 $DB->insert_record('role_allow_switch', $record);
2937 upgrade_main_savepoint(true, 2010033102.04);
2940 if ($oldversion < 2010033102.05) {
2941 // remove course:view from all roles that are not used for enrolment, it does NOT belong there because it really means user is enrolled!
2942 $noenrolroles = $DB->get_records_select('role', "archetype IN ('guest', 'user', 'manager', 'coursecreator', 'frontpage')");
2943 foreach ($noenrolroles as $role) {
2944 $DB->delete_records('role_capabilities', array('roleid'=>$role->id, 'capability'=>'moodle/course:participate'));
2946 upgrade_main_savepoint(true, 2010033102.05);
2949 if ($oldversion < 2010033102.06) {
2950 // make sure there is nothing weird in default user role
2951 if (!empty($CFG->defaultuserroleid)) {
2952 if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
2953 if ($role->archetype !== '' and $role->archetype !== 'user') {
2954 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default authenticated user role (defaultuserroleid) value is invalid, setting cleared.');
2955 unset_config('defaultuserroleid');
2958 unset_config('defaultuserroleid');
2961 upgrade_main_savepoint(true, 2010033102.06);
2964 if ($oldversion < 2010033102.07) {
2965 if (!empty($CFG->displayloginfailures) and $CFG->displayloginfailures === 'teacher') {
2966 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Displaying of login failuters to teachers is not supported any more.');
2967 unset_config('displayloginfailures');
2969 upgrade_main_savepoint(true, 2010033102.07);
2972 if ($oldversion < 2010033102.08) {
2973 // make sure there are no problems in default guest role settings
2974 if (!empty($CFG->guestroleid)) {
2975 if ($role = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
2976 if ($role->archetype !== '' and $role->archetype !== 'guest') {
2977 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default guest role (guestroleid) value is invalid, setting cleared.');
2978 unset_config('guestroleid');
2981 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) does not exist, setting cleared.');
2982 unset_config('guestroleid');
2985 // remove all roles of the guest account - the only way to change it is to override the guest role, sorry
2986 // the guest account gets all the role assignments on the fly which works fine in has_capability(),
2987 $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE username = 'guest')");
2989 upgrade_main_savepoint(true, 2010033102.08);
2992 /// New table for storing which roles can be assigned in which contexts.
2993 if ($oldversion < 2010033102.09) {
2995 /// Define table role_context_levels to be created
2996 $table = new xmldb_table('role_context_levels');
2998 /// Adding fields to table role_context_levels
2999 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3000 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3001 $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3003 /// Adding keys to table role_context_levels
3004 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3005 $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
3006 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
3008 /// Conditionally launch create table for role_context_levels
3009 if (!$dbman->table_exists($table)) {
3010 $dbman->create_table($table);
3013 /// Main savepoint reached
3014 upgrade_main_savepoint(true, 2010033102.09);
3017 if ($oldversion < 2010033102.10) {
3018 // Now populate the role_context_levels table with the default values
3019 // NOTE: do not use accesslib methods here
3021 $rolecontextlevels = array();
3022 $defaults = array('manager' => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE),
3023 'coursecreator' => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT),
3024 'editingteacher' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3025 'teacher' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3026 'student' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3029 'frontpage' => array());
3031 $roles = $DB->get_records('role', array(), '', 'id, archetype');
3032 foreach ($roles as $role) {
3033 if (isset($defaults[$role->archetype])) {
3034 $rolecontextlevels[$role->id] = $defaults[$role->archetype];
3038 // add roles without archetypes, it may contain weird things, but we can not fix them
3039 list($narsql, $params) = $DB->get_in_or_equal(array_keys($defaults), SQL_PARAMS_NAMED, 'ar000', false);
3040 $sql = "SELECT DISTINCT ra.roleid, con.contextlevel
3041 FROM {role_assignments} ra
3042 JOIN {context} con ON ra.contextid = con.id
3043 JOIN {role} r ON r.id = ra.roleid
3044 WHERE r.archetype $narsql";
3045 $existingrolecontextlevels = $DB->get_recordset_sql($sql, $params);
3046 foreach ($existingrolecontextlevels as $rcl) {
3047 if (!isset($rolecontextlevels[$rcl->roleid])) {
3048 $rolecontextlevels[$rcl->roleid] = array();
3050 $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
3052 $existingrolecontextlevels->close();
3054 // Put the data into the database.
3055 $rcl = new stdClass();
3056 foreach ($rolecontextlevels as $roleid => $contextlevels) {
3057 $rcl->roleid = $roleid;
3058 foreach ($contextlevels as $level) {
3059 $rcl->contextlevel = $level;
3060 $DB->insert_record('role_context_levels', $rcl, false);
3068 unset($existingrolecontextlevels);
3069 unset($rolecontextlevels);
3071 // Main savepoint reached
3072 upgrade_main_savepoint(true, 2010033102.10);
3075 if ($oldversion < 2010040700) {
3076 // migrate old groupings --> groupmembersonly setting
3077 if (isset($CFG->enablegroupings)) {
3078 set_config('enablegroupmembersonly', $CFG->enablegroupings);
3079 unset_config('enablegroupings');
3082 // Main savepoint reached
3083 upgrade_main_savepoint(true, 2010040700);
3086 if ($oldversion < 2010040900) {
3088 // Changing the default of field lang on table user to good old "en"
3089 $table = new xmldb_table('user');
3090 $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en', 'country');
3092 // Launch change of default for field lang
3093 $dbman->change_field_default($table, $field);
3095 // update main site lang
3096 if (strpos($CFG->lang, '_utf8') !== false) {
3097 $lang = str_replace('_utf8', '', $CFG->lang);
3098 set_config('lang', $lang);
3102 if (!empty($CFG->langlist)) {
3103 $langs = explode(',', $CFG->langlist);
3104 foreach ($langs as $key=>$lang) {
3105 $lang = str_replace('_utf8', '', $lang);
3106 $langs[$key] = $lang;
3108 set_config('langlist', implode(',', $langs));
3111 // Main savepoint reached
3112 upgrade_main_savepoint(true, 2010040900);
3115 if ($oldversion < 2010040901) {
3117 // Remove "_utf8" suffix from all langs in user table
3118 $langs = $DB->get_records_sql("SELECT DISTINCT lang FROM {user} WHERE lang LIKE ?", array('%_utf8'));
3120 foreach ($langs as $lang=>$unused) {
3121 $newlang = str_replace('_utf8', '', $lang);
3122 $sql = "UPDATE {user} SET lang = :newlang WHERE lang = :lang";
3123 $DB->execute($sql, array('newlang'=>$newlang, 'lang'=>$lang));
3126 // Main savepoint reached
3127 upgrade_main_savepoint(true, 2010040901);
3130 if ($oldversion < 2010041301) {
3131 $sql = "UPDATE {block} SET name=? WHERE name=?";
3132 $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3133 $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3135 $sql = "UPDATE {block_instances} SET blockname=? WHERE blockname=?";
3136 $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3137 $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3138 upgrade_main_savepoint(true, 2010041301);
3141 if ($oldversion < 2010042100) {
3143 /// Define table backup_controllers to be created
3144 $table = new xmldb_table('backup_controllers');
3146 /// Adding fields to table backup_controllers
3147 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3148 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3149 $table->add_field('type', XMLDB_TYPE_CHAR, '6', null, XMLDB_NOTNULL, null, null);
3150 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3151 $table->add_field('format', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
3152 $table->add_field('interactive', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3153 $table->add_field('purpose', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3154 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3155 $table->add_field('status', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3156 $table->add_field('execution', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3157 $table->add_field('executiontime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3158 $table->add_field('checksum', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3159 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3160 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3161 $table->add_field('controller', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
3163 /// Adding keys to table backup_controllers
3164 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3165 $table->add_key('backupid_uk', XMLDB_KEY_UNIQUE, array('backupid'));
3166 $table->add_key('userid_fk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3168 /// Adding indexes to table backup_controllers
3169 $table->add_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3171 /// Conditionally launch create table for backup_controllers
3172 if (!$dbman->table_exists($table)) {
3173 $dbman->create_table($table);
3176 /// Define table backup_ids_template to be created
3177 $table = new xmldb_table('backup_ids_template');
3179 /// Adding fields to table backup_ids_template
3180 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3181 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3182 $table->add_field('itemname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
3183 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3184 $table->add_field('parentitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3186 /// Adding keys to table backup_ids_template
3187 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3188 $table->add_key('backupid_itemname_itemid_uk', XMLDB_KEY_UNIQUE, array('backupid', 'itemname', 'itemid'));
3190 /// Adding indexes to table backup_ids_template
3191 $table->add_index('backupid_parentitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'parentitemid'));
3193 /// Conditionally launch create table for backup_controllers
3194 if (!$dbman->table_exists($table)) {
3195 $dbman->create_table($table);
3198 /// Main savepoint reached
3199 upgrade_main_savepoint(true, 2010042100);
3202 if ($oldversion < 2010042301) {
3204 $table = new xmldb_table('course_sections');
3205 $field = new xmldb_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'section');
3207 if (!$dbman->field_exists($table, $field)) {
3208 $dbman->add_field($table, $field);
3211 upgrade_main_savepoint(true, 2010042301);
3214 if ($oldversion < 2010042302) {
3215 // Define table cohort to be created
3216 $table = new xmldb_table('cohort');
3218 // Adding fields to table cohort
3219 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3220 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3221 $table->add_field('name', XMLDB_TYPE_CHAR, '254', null, XMLDB_NOTNULL, null, null);
3222 $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3223 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
3224 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3225 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
3226 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3227 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3229 // Adding keys to table cohort
3230 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3231 $table->add_key('context', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3233 // Conditionally launch create table for cohort
3234 if (!$dbman->table_exists($table)) {
3235 $dbman->create_table($table);
3238 upgrade_main_savepoint(true, 2010042302);
3241 if ($oldversion < 2010042303) {
3242 // Define table cohort_members to be created
3243 $table = new xmldb_table('cohort_members');
3245 // Adding fields to table cohort_members
3246 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3247 $table->add_field('cohortid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3248 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3249 $table->add_field('timeadded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3251 // Adding keys to table cohort_members
3252 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3253 $table->add_key('cohortid', XMLDB_KEY_FOREIGN, array('cohortid'), 'cohort', array('id'));
3254 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3256 // Adding indexes to table cohort_members
3257 $table->add_index('cohortid-userid', XMLDB_INDEX_UNIQUE, array('cohortid', 'userid'));
3259 // Conditionally launch create table for cohort_members
3260 if (!$dbman->table_exists($table)) {
3261 $dbman->create_table($table);
3264 // Main savepoint reached
3265 upgrade_main_savepoint(true, 2010042303);
3268 if ($oldversion < 2010042800) {
3269 //drop the previously created ratings table
3270 $table = new xmldb_table('ratings');
3271 if ($dbman->table_exists($table)) {
3272 $dbman->drop_table($table);
3275 //create the rating table (replaces module specific rating implementations)
3276 $table = new xmldb_table('rating');
3277 if ($dbman->table_exists($table)) {
3278 $dbman->drop_table($table);
3281 /// Adding fields to table rating
3282 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3283 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3285 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3286 $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
3287 $table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3288 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3290 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3291 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3293 /// Adding keys to table rating
3294 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3295 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3296 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3298 /// Adding indexes to table rating
3299 $table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
3301 /// Create table for ratings
3302 if (!$dbman->table_exists($table)) {
3303 $dbman->create_table($table);
3306 upgrade_main_savepoint(true, 2010042800);
3309 if ($oldversion < 2010042801) {
3310 // migrating old comments block content
3311 $DB->execute("UPDATE {comments}
3312 SET contextid = (SELECT parentcontextid
3313 FROM {block_instances}
3314 WHERE id = {comments}.itemid AND blockname = 'comments'),
3315 commentarea = 'page_comments',
3317 WHERE commentarea = 'block_comments'
3319 AND EXISTS (SELECT 'x'
3320 FROM {block_instances}
3321 WHERE id = {comments}.itemid
3322 AND blockname = 'comments')");
3324 // remove all orphaned record
3325 $DB->delete_records('comments', array('commentarea'=>'block_comments'));
3326 upgrade_main_savepoint(true, 2010042801);
3329 if ($oldversion < 2010042802) { // Change backup_controllers->type to varchar10 (recreate dep. index)
3331 /// Define index typeitem_ix (not unique) to be dropped form backup_controllers
3332 $table = new xmldb_table('backup_controllers');
3333 $index = new xmldb_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3335 /// Conditionally launch drop index typeitem_ix
3336 if ($dbman->index_exists($table, $index)) {
3337 $dbman->drop_index($table, $index);
3340 /// Changing precision of field type on table backup_controllers to (10)
3341 $table = new xmldb_table('backup_controllers');
3342 $field = new xmldb_field('type', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'backupid');
3344 /// Launch change of precision for field type
3345 $dbman->change_field_precision($table, $field);
3347 /// Define index typeitem_ix (not unique) to be added to backup_controllers
3348 $table = new xmldb_table('backup_controllers');
3349 $index = new xmldb_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3351 /// Conditionally launch add index typeitem_ix
3352 if (!$dbman->index_exists($table, $index)) {
3353 $dbman->add_index($table, $index);
3356 /// Main savepoint reached
3357 upgrade_main_savepoint(true, 2010042802);
3360 if ($oldversion < 2010043000) { // Adding new course completion feature
3362 /// Add course completion tables
3363 /// Define table course_completion_aggr_methd to be created
3364 $table = new xmldb_table('course_completion_aggr_methd');
3366 /// Adding fields to table course_completion_aggr_methd
3367 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3368 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3369 $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, null);
3370 $table->add_field('method', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3371 $table->add_field('value', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3373 /// Adding keys to table course_completion_aggr_methd
3374 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3376 /// Adding indexes to table course_completion_aggr_methd
3377 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3378 $table->add_index('criteriatype', XMLDB_INDEX_NOTUNIQUE, array('criteriatype'));
3380 /// Conditionally launch create table for course_completion_aggr_methd
3381 if (!$dbman->table_exists($table)) {
3382 $dbman->create_table($table);
3386 /// Define table course_completion_criteria to be created
3387 $table = new xmldb_table('course_completion_criteria');
3389 /// Adding fields to table course_completion_criteria
3390 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3391 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3392 $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3393 $table->add_field('module', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3394 $table->add_field('moduleinstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3395 $table->add_field('courseinstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3396 $table->add_field('enrolperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3397 $table->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3398 $table->add_field('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3399 $table->add_field('role', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3401 /// Adding keys to table course_completion_criteria
3402 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3404 /// Adding indexes to table course_completion_criteria
3405 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3407 /// Conditionally launch create table for course_completion_criteria
3408 if (!$dbman->table_exists($table)) {
3409 $dbman->create_table($table);
3413 /// Define table course_completion_crit_compl to be created
3414 $table = new xmldb_table('course_completion_crit_compl');
3416 /// Adding fields to table course_completion_crit_compl
3417 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3418 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3419 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3420 $table->add_field('criteriaid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3421 $table->add_field('gradefinal', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3422 $table->add_field('unenroled', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3423 $table->add_field('deleted', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
3424 $table->add_field('timecompleted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3426 /// Adding keys to table course_completion_crit_compl
3427 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3429 /// Adding indexes to table course_completion_crit_compl
3430 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
3431 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3432 $table->add_index('criteriaid', XMLDB_INDEX_NOTUNIQUE, array('criteriaid'));
3433 $table->add_index('timecompleted', XMLDB_INDEX_NOTUNIQUE, array('timecompleted'));
3435 /// Conditionally launch create table for course_completion_crit_compl
3436 if (!$dbman->table_exists($table)) {
3437 $dbman->create_table($table);
3441 /// Define table course_completion_notify to be created
3442 $table = new xmldb_table('course_completion_notify');
3444 /// Adding fields to table course_completion_notify
3445 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3446 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3447 $table->add_field('role', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3448 $table->add_field('message', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
3449 $table->add_field('timesent', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3451 /// Adding keys to table course_completion_notify
3452 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3454 /// Adding indexes to table course_completion_notify
3455 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3457 /// Conditionally launch create table for course_completion_notify
3458 if (!$dbman->table_exists($table)) {
3459 $dbman->create_table($table);
3462 /// Define table course_completions to be created
3463 $table = new xmldb_table('course_completions');
3465 /// Adding fields to table course_completions
3466 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3467 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3468 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3469 $table->add_field('deleted', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
3470 $table->add_field('timenotified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3471 $table->add_field('timeenrolled', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3472 $table->add_field('timestarted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3473 $table->add_field('timecompleted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3474 $table->add_field('reaggregate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3476 /// Adding keys to table course_completions
3477 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3479 /// Adding indexes to table course_completions
3480 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
3481 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3482 $table->add_index('timecompleted', XMLDB_INDEX_NOTUNIQUE, array('timecompleted'));
3484 /// Conditionally launch create table for course_completions
3485 if (!$dbman->table_exists($table)) {
3486 $dbman->create_table($table);
3490 /// Add cols to course table
3491 /// Define field enablecompletion to be added to course
3492 $table = new xmldb_table('course');
3493 $field = new xmldb_field('enablecompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
3495 /// Conditionally launch add field enablecompletion
3496 if (!$dbman->field_exists($table, $field)) {
3497 $dbman->add_field($table, $field);
3500 /// Define field completionstartonenrol to be added to course
3501 $field = new xmldb_field('completionstartonenrol', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'enablecompletion');
3503 /// Conditionally launch add field completionstartonenrol
3504 if (!$dbman->field_exists($table, $field)) {
3505 $dbman->add_field($table, $field);
3508 /// Define field completionnotify to be added to course
3509 $field = new xmldb_field('completionnotify', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'enablecompletion');
3511 /// Conditionally launch add field completionnotify
3512 if (!$dbman->field_exists($table, $field)) {
3513 $dbman->add_field($table, $field);
3516 upgrade_main_savepoint(true, 2010043000);
3519 if ($oldversion < 2010043001) {
3521 /// Define table registration_hubs to be created
3522 $table = new xmldb_table('registration_hubs');
3524 /// Adding fields to table registration_hubs
3525 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3526 $table->add_field('token', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
3527 $table->add_field('hubname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
3528 $table->add_field('huburl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
3529 $table->add_field('confirmed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3531 /// Adding keys to table registration_hubs
3532 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3534 /// Conditionally launch create table for registration_hubs
3535 if (!$dbman->table_exists($table)) {
3536 $dbman->create_table($table);
3539 /// Main savepoint reached
3540 upgrade_main_savepoint(true, 2010043001);
3543 if ($oldversion < 2010050200) {
3545 /// Define table backup_logs to be created
3546 $table = new xmldb_table('backup_logs');
3548 /// Adding fields to table backup_logs
3549 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3550 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3551 $table->add_field('loglevel', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3552 $table->add_field('message', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
3553 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3555 /// Adding keys to table backup_logs
3556 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3557 $table->add_key('backupid', XMLDB_KEY_FOREIGN, array('backupid'), 'backup_controllers', array('backupid'));
3559 /// Adding indexes to table backup_logs
3560 $table->add_index('backupid-id', XMLDB_INDEX_UNIQUE, array('backupid', 'id'));
3562 /// Conditionally launch create table for backup_logs
3563 if (!$dbman->table_exists($table)) {
3564 $dbman->create_table($table);
3567 /// Drop some old backup tables, not used anymore
3569 /// Define table backup_files to be dropped
3570 $table = new xmldb_table('backup_files');
3572 /// Conditionally launch drop table for backup_files
3573 if ($dbman->table_exists($table)) {
3574 $dbman->drop_table($table);
3577 /// Define table backup_ids to be dropped
3578 $table = new xmldb_table('backup_ids');
3580 /// Conditionally launch drop table for backup_ids
3581 if ($dbman->table_exists($table)) {
3582 $dbman->drop_table($table);
3585 /// Main savepoint reached
3586 upgrade_main_savepoint(true, 2010050200);
3589 if ($oldversion < 2010050403) { // my_pages for My Moodle and Public Profile pages
3591 /// Define table my_pages to be created
3592 $table = new xmldb_table('my_pages');
3594 /// Adding fields to table my_pages
3595 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3596 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 0);
3597 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
3598 $table->add_field('private', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
3599 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, '0');
3602 /// Adding keys to table my_pages
3603 $table->add_key('id', XMLDB_KEY_PRIMARY, array('id'));
3605 /// Adding indexes to table my_pages
3606 $table->add_index('useridprivate', XMLDB_INDEX_NOTUNIQUE, array('userid', 'private'));
3608 /// Conditionally launch create table for my_pages
3609 if (!$dbman->table_exists($table)) {
3610 $dbman->create_table($table);
3613 /// Add two lines of data into this new table. These are the default pages.
3614 $mypage = new stdClass();
3615 $mypage->userid = NULL;
3616 $mypage->name = '__default';
3617 $mypage->private = 0;
3618 $mypage->sortorder = 0;
3619 if (!$DB->record_exists('my_pages', array('userid'=>NULL, 'private'=>0))) {
3620 $DB->insert_record('my_pages', $mypage);
3622 $mypage->private = 1;
3623 if (!$DB->record_exists('my_pages', array('userid'=>NULL, 'private'=>1))) {
3624 $DB->insert_record('my_pages', $mypage);
3627 /// This bit is a "illegal" hack, unfortunately, but there is not a better way to install default
3628 /// blocks right now, since the upgrade function need to be called after core AND plugins upgrade,
3629 /// and there is no such hook yet. Sigh.
3631 if ($mypage = $DB->get_record('my_pages', array('userid'=>NULL, 'private'=>1))) {
3632 if (!$DB->record_exists('block_instances', array('pagetypepattern'=>'my-index', 'parentcontextid'=>SITEID, 'subpagepattern'=>$mypage->id))) {
3634 // No default exist there yet, let's put a few into My Moodle so it's useful.
3636 $blockinstance = new stdClass;
3637 $blockinstance->parentcontextid = SYSCONTEXTID;
3638 $blockinstance->showinsubcontexts = 0;
3639 $blockinstance->pagetypepattern = 'my-index';
3640 $blockinstance->subpagepattern = $mypage->id;
3641 $blockinstance->configdata = '';
3643 $blockinstance->blockname = 'private_files';
3644 $blockinstance->defaultregion = 'side-post';
3645 $blockinstance->defaultweight = 0;
3646 $blockinstanceid = $DB->insert_record('block_instances', $blockinstance);
3647 get_context_instance(CONTEXT_BLOCK, $blockinstanceid);
3649 $blockinstance->blockname = 'online_users';
3650 $blockinstance->defaultregion = 'side-post';
3651 $blockinstance->defaultweight = 1;
3652 $blockinstanceid = $DB->insert_record('block_instances', $blockinstance);
3653 get_context_instance(CONTEXT_BLOCK, $blockinstanceid);
3655 $blockinstance->blockname = 'course_overview';
3656 $blockinstance->defaultregion = 'content';
3657 $blockinstance->defaultweight = 0;
3658 $blockinstanceid = $DB->insert_record('block_instances', $blockinstance);
3659 get_context_instance(CONTEXT_BLOCK, $blockinstanceid);
3663 /// Main savepoint reached
3664 upgrade_main_savepoint(true, 2010050403);
3667 if ($oldversion < 2010051500) {
3669 /// Fix a bad table name that existed for a few days in HEAD
3670 $table = new xmldb_table('published_courses');
3671 if ($dbman->table_exists($table)) {
3672 $dbman->drop_table($table);
3675 /// Define table course_published to be created
3676 $table = new xmldb_table('course_published');
3678 /// Adding fields to table course_published
3679 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3680 $table->add_field('hubid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3681 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3682 $table->add_field('timepublished', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3683 $table->add_field('enrollable', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
3684 $table->add_field('hubcourseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3686 /// Adding keys to table course_published
3687 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3689 /// Conditionally launch create table for course_published
3690 if (!$dbman->table_exists($table)) {
3691 $dbman->create_table($table);
3694 /// Main savepoint reached
3695 upgrade_main_savepoint(true, 2010051500);
3698 if ($oldversion < 2010051600) {
3700 /// Delete the blocks completely. All the contexts, instances etc were cleaned up above in 2009082800
3701 $DB->delete_records('block', array('name'=>'admin'));
3702 $DB->delete_records('block', array('name'=>'admin_tree'));
3704 /// Main savepoint reached
3705 upgrade_main_savepoint(true, 2010051600);
3708 if ($oldversion < 2010051800) {
3709 // switching to userid in config settings because user names are not unique and reliable enough
3710 if (!empty($CFG->courserequestnotify) and $CFG->courserequestnotify !== '$@NONE@$' and $CFG->courserequestnotify !== '$@ALL@$') {
3711 list($where, $params) = $DB->get_in_or_equal(explode(',', $CFG->courserequestnotify));
3712 $params[] = $CFG->mnet_localhost_id;
3713 $users = $DB->get_fieldset_select('user', 'id', "username $where AND mnethostid = ?", $params);
3715 set_config('courserequestnotify', implode(',', $users));
3717 set_config('courserequestnotify', '$@NONE@$');
3720 upgrade_main_savepoint(true, 2010051800);
3723 if ($oldversion < 2010051801) {
3724 // Update the notifyloginfailures setting.
3725 if ($CFG->notifyloginfailures == 'mainadmin') {
3726 if ($admins = explode(',', $CFG->siteadmins)) {
3727 $adminid = reset($admins);
3728 set_config('notifyloginfailures', $adminid);
3730 unset_config('notifyloginfailures'); // let them choose
3734 } else if ($CFG->notifyloginfailures == 'alladmins') {
3735 set_config('notifyloginfailures', '$@ALL@$');
3738 set_config('notifyloginfailures', '$@NONE@$');
3741 upgrade_main_savepoint(true, 2010051801);
3744 if ($oldversion < 2010052100) {
3745 // Switch to html purifier as default cleaning engine - KSES is really very bad
3746 if (empty($CFG->enablehtmlpurifier)) {
3747 unset_config('enablehtmlpurifier');
3749 upgrade_main_savepoint(true, 2010052100);
3752 if ($oldversion < 2010052200) {
3753 // Define field legacyfiles to be added to course - just in case we are upgrading from PR1
3754 $table = new xmldb_table('course');
3755 $field = new xmldb_field('legacyfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'maxbytes');
3757 // Conditionally launch add field legacyfiles
3758 if (!$dbman->field_exists($table, $field)) {
3759 $dbman->add_field($table, $field);
3760 // enable legacy files in all courses
3761 $DB->execute("UPDATE {course} SET legacyfiles = 2");
3764 // Main savepoint reached
3765 upgrade_main_savepoint(true, 2010052200);
3768 if ($oldversion < 2010052401) {
3770 /// Define field status to be added to course_published
3771 $table = new xmldb_table('course_published');
3772 $field = new xmldb_field('status', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0', 'hubcourseid');
3774 /// Conditionally launch add field status
3775 if (!$dbman->field_exists($table, $field)) {
3776 $dbman->add_field($table, $field);
3779 /// Define field timechecked to be added to course_published
3780 $table = new xmldb_table('course_published');
3781 $field = new xmldb_field('timechecked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'status');
3783 /// Conditionally launch add field timechecked
3784 if (!$dbman->field_exists($table, $field)) {
3785 $dbman->add_field($table, $field);
3788 /// Main savepoint reached
3789 upgrade_main_savepoint(true, 2010052401);
3792 if ($oldversion < 2010052700) {
3794 /// Define field summaryformat to be added to course sections table
3795 $table = new xmldb_table('course_sections');
3796 $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'summary');
3798 /// Conditionally launch add field summaryformat
3799 if (!$dbman->field_exists($table, $field)) {
3800 $dbman->add_field($table, $field);
3803 $DB->set_field('course_sections', 'summaryformat', 1, array()); // originally treated as HTML
3805 /// Main savepoint reached
3806 upgrade_main_savepoint(true, 2010052700);
3809 if ($oldversion < 2010052800) {
3810 /// Changes to modinfo mean we need to rebuild course cache
3811 require_once($CFG->dirroot . '/course/lib.php');
3812 rebuild_course_cache(0, true);
3813 upgrade_main_savepoint(true, 2010052800);
3816 if ($oldversion < 2010052801) {
3818 /// Define field sortorder to be added to files
3819 $table = new xmldb_table('files');
3820 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
3822 /// Conditionally launch add field sortorder
3823 if (!$dbman->field_exists($table, $field)) {
3824 $dbman->add_field($table, $field);
3827 /// Main savepoint reached
3828 upgrade_main_savepoint(true, 2010052801);
3831 if ($oldversion < 2010061900.01) {
3832 // Define table enrol to be created
3833 $table = new xmldb_table('enrol');
3835 // Adding fields to table enrol
3836 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3837 $table->add_field('enrol', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
3838 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3839 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3840 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3841 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3842 $table->add_field('enrolperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
3843 $table->add_field('enrolstartdate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
3844 $table->add_field('enrolenddate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
3845 $table->add_field('expirynotify', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0');
3846 $table->add_field('expirythreshold', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
3847 $table->add_field('notifyall', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0');
3848 $table->add_field('password', XMLDB_TYPE_CHAR, '50', null, null, null, null);
3849 $table->add_field('cost', XMLDB_TYPE_CHAR, '20', null, null, null, null);
3850 $table->add_field('currency', XMLDB_TYPE_CHAR, '3', null, null, null, null);
3851 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
3852 $table->add_field('customint1', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
3853 $table->add_field('customint2', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
3854 $table->add_field('customint3', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
3855 $table->add_field('customint4', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
3856 $table->add_field('customchar1', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3857 $table->add_field('customchar2', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3858 $table->add_field('customdec1', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null);
3859 $table->add_field('customdec2', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null);
3860 $table->add_field('customtext1', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
3861 $table->add_field('customtext2', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
3862 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3863 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3865 // Adding keys to table enrol
3866 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3867 $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
3869 // Adding indexes to table enrol
3870 $table->add_index('enrol', XMLDB_INDEX_NOTUNIQUE, array('enrol'));
3872 // launch create table for enrol
3873 $dbman->create_table($table);
3875 // Main savepoint reached
3876 upgrade_main_savepoint(true, 2010061900.01);
3879 if ($oldversion < 2010061900.02) {
3880 // Define table course_participant to be created
3881 $table = new xmldb_table('user_enrolments');
3883 // Adding fields to table course_participant
3884 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3885 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3886 $table->add_field('enrolid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3887 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3888 $table->add_field('timestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3889 $table->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2147483647');
3890 $table->add_field('modifierid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3891 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3892 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3894 // Adding keys to table course_participant
3895 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3896 $table->add_key('enrolid', XMLDB_KEY_FOREIGN, array('enrolid'), 'enrol', array('id'));
3897 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3898 $table->add_key('modifierid', XMLDB_KEY_FOREIGN, array('modifierid'), 'user', array('id'));
3901 // Adding indexes to table user_enrolments
3902 $table->add_index('enrolid-userid', XMLDB_INDEX_UNIQUE, array('enrolid', 'userid'));
3904 // Launch create table for course_participant
3905 $dbman->create_table($table);
3907 // Main savepoint reached
3908 upgrade_main_savepoint(true, 2010061900.02);
3911 if ($oldversion < 2010061900.03) {
3912 // Define field itemid to be added to role_assignments
3913 $table = new xmldb_table('role_assignments');
3914 $field = new xmldb_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'enrol');
3916 // Launch add field itemid
3917 $dbman->add_field($table, $field);
3919 // The new enrol plugins may assign one role several times in one context,
3920 // if we did not allow it we would have big problems with roles when unenrolling
3921 $table = new xmldb_table('role_assignments');
3922 $index = new xmldb_index('contextid-roleid-userid', XMLDB_INDEX_UNIQUE, array('contextid', 'roleid', 'userid'));
3924 // Conditionally launch drop index contextid-roleid-userid
3925 if ($dbman->index_exists($table, $index)) {
3926 $dbman->drop_index($table, $index);
3929 // Main savepoint reached
3930 upgrade_main_savepoint(true, 2010061900.03);
3933 if ($oldversion < 2010061900.04) {
3934 // there is no default course role any more, each enrol plugin has to handle it separately
3935 if (!empty($CFG->defaultcourseroleid)) {
3936 $sql = "UPDATE {course} SET defaultrole = :default WHERE defaultrole = 0";
3937 $params = array('default' => $CFG->defaultcourseroleid);
3938 $DB->execute($sql, $params);
3940 unset_config('defaultcourseroleid');
3942 // Main savepoint reached
3943 upgrade_main_savepoint(true, 2010061900.04);
3946 if ($oldversion < 2010061900.05) {
3947 // make sure enrol settings make actually sense and tweak defaults a bit
3949 $sqlempty = $DB->sql_empty();
3951 // set course->enrol to default value so that other upgrade code is simpler
3952 $defaultenrol = empty($CFG->enrol) ? 'manual' : $CFG->enrol;
3953 $sql = "UPDATE {course} SET enrol = ? WHERE enrol = '$sqlempty'";
3954 $DB->execute($sql, array($defaultenrol));
3955 unset_config('enrol');
3957 if (!isset($CFG->enrol_plugins_enabled) or empty($CFG->enrol_plugins_enabled)) {
3958 set_config('enrol_plugins_enabled', 'manual');
3960 $enabledplugins = explode(',', $CFG->enrol_plugins_enabled);
3961 $enabledplugins = array_unique($enabledplugins);
3962 set_config('enrol_plugins_enabled', implode(',', $enabledplugins));
3965 // Main savepoint reached
3966 upgrade_main_savepoint(true, 2010061900.05);
3969 if ($oldversion < 2010061900.06) {
3970 $sqlempty = $DB->sql_empty();
3971 $params = array('siteid'=>SITEID);
3973 // enable manual in all courses
3974 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, expirynotify, expirythreshold, notifyall, roleid, timecreated, timemodified)
3975 SELECT 'manual', 0, id, 0, enrolperiod, expirynotify, expirythreshold, notifystudents, defaultrole, timecreated, timemodified
3977 WHERE id <> :siteid";
3978 $DB->execute($sql, $params);