MDL-25942 fix another Oracle compatibility issue
[moodle.git] / lib / db / upgrade.php
blob1be183d639b7ccca4ed22d9c4499a21d661c836f
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This file keeps track of upgrades to Moodle.
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.
38 * @package core
39 * @subpackage admin
40 * @copyright 2006 onwards Martin Dougiamas http://dougiamas.com
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 defined('MOODLE_INTERNAL') || die();
46 /**
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";
132 } else {
133 $oldlang = "$CFG->dataroot/lang";
135 } else {
136 $oldlang = '';
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');
239 // add field
240 $field = new xmldb_field('tiuserid');
241 if (!$dbman->field_exists($table, $field)) {
242 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
243 $dbman->add_field($table, $field);
245 // modify index
246 $index = new xmldb_index('itemtype-itemid-tagid');
247 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
248 if ($dbman->index_exists($table, $index)) {
249 $dbman->drop_index($table, $index);
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);
342 /// fields to rename
343 $field = new xmldb_field('message');
344 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
345 $dbman->rename_field($table, $field, 'fullmessage');
346 $field = new xmldb_field('format');
347 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
348 $dbman->rename_field($table, $field, 'fullmessageformat');
350 /// new message fields
351 $field = new xmldb_field('subject');
352 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
353 $dbman->add_field($table, $field);
354 $field = new xmldb_field('fullmessagehtml');
355 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
356 $dbman->add_field($table, $field);
357 $field = new xmldb_field('smallmessage');
358 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
359 $dbman->add_field($table, $field);
362 $table = new xmldb_table('message_read');
363 $field = new xmldb_field('messagetype');
364 $dbman->drop_field($table, $field);
365 $field = new xmldb_field('mailed');
366 $dbman->drop_field($table, $field);
368 /// fields to rename
369 $field = new xmldb_field('message');
370 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
371 $dbman->rename_field($table, $field, 'fullmessage');
372 $field = new xmldb_field('format');
373 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
374 $dbman->rename_field($table, $field, 'fullmessageformat');
377 /// new message fields
378 $field = new xmldb_field('subject');
379 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
380 $dbman->add_field($table, $field);
381 $field = new xmldb_field('fullmessagehtml');
382 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
383 $dbman->add_field($table, $field);
384 $field = new xmldb_field('smallmessage');
385 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
386 $dbman->add_field($table, $field);
388 /// new table
389 $table = new xmldb_table('message_working');
390 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
391 $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
392 $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
393 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
394 $dbman->create_table($table);
397 upgrade_main_savepoint(true, 2008072400);
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') {
809 continue;
811 $pluginname = substr($name, 0, strlen($name) - 8);
812 if (!strpos($pluginname, '_')) {
813 // Skip things like backup_version that don't contain an extra _
814 continue;
816 if ($pluginname == 'enrol_ldap_version') {
817 // Special case - this is something different from a plugin version number.
818 continue;
820 if (!preg_match('/^\d{10}$/', $value)) {
821 // Extra safety check, skip anything that does not look like a Moodle
822 // version number (10 digits).
823 continue;
825 set_config('version', $value, $pluginname);
826 unset_config($name);
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.
1207 $DB->execute("
1208 UPDATE {course_modules_availability} SET grademin=(
1209 SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
1210 /(gi.grademax-gi.grademin)
1211 FROM {grade_items} gi
1212 WHERE gi.id={course_modules_availability}.gradeitemid)
1213 WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1214 $DB->execute("
1215 UPDATE {course_modules_availability} SET grademax=(
1216 SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
1217 /(gi.grademax-gi.grademin)
1218 FROM {grade_items} gi
1219 WHERE gi.id={course_modules_availability}.gradeitemid)
1220 WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1222 /// Main savepoint reached
1223 upgrade_main_savepoint(true, 2009021800);
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();
1359 } else {
1360 $activefilters = explode(',', $CFG->textfilters);
1362 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1363 $sortorder = 1;
1364 foreach ($activefilters as $filter) {
1365 filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1366 $sortorder += 1;
1367 unset($disabledfilters[$filter]);
1369 foreach ($disabledfilters as $filter => $notused) {
1370 filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1371 $sortorder += 1;
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);
1383 } else {
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
1412 FROM {user} u
1413 JOIN {message} m ON m.useridfrom = u.id
1414 WHERE u.deleted = ?', array(1))) {
1415 foreach ($deletedusers as $deleteduser) {
1416 message_move_userfrom_unread2read($deleteduser->id); // move messages
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
1663 /// site-index
1664 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
1665 $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1666 pagetypepattern = 'site-index',
1667 subpagepattern = NULL
1668 WHERE pagetypepattern = 'site-index'");
1670 /// course-view
1671 $DB->execute("UPDATE {block_instances} SET
1672 contextid = (
1673 SELECT {context}.id
1674 FROM {context}
1675 JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1676 WHERE {course}.id = pageid
1678 pagetypepattern = 'course-view-*',
1679 subpagepattern = NULL
1680 WHERE pagetypepattern = 'course-view'");
1682 /// admin
1683 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1684 $DB->execute("UPDATE {block_instances} SET
1685 contextid = " . $syscontext->id . ",
1686 pagetypepattern = 'admin-*',
1687 subpagepattern = NULL
1688 WHERE pagetypepattern = 'admin'");
1690 /// my-index
1691 $DB->execute("UPDATE {block_instances} SET
1692 contextid = (
1693 SELECT {context}.id
1694 FROM {context}
1695 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1696 WHERE {user}.id = pageid
1698 pagetypepattern = 'my-index',
1699 subpagepattern = NULL
1700 WHERE pagetypepattern = 'my-index'");
1702 /// tag-index
1703 $DB->execute("UPDATE {block_instances} SET
1704 contextid = " . $syscontext->id . ",
1705 pagetypepattern = 'tag-index',
1706 subpagepattern = pageid
1707 WHERE pagetypepattern = 'tag-index'");
1709 /// blog-view
1710 $DB->execute("UPDATE {block_instances} SET
1711 contextid = (
1712 SELECT {context}.id
1713 FROM {context}
1714 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1715 WHERE {user}.id = pageid
1717 pagetypepattern = 'blog-index',
1718 subpagepattern = NULL
1719 WHERE pagetypepattern = 'blog-view'");
1721 /// mod-xxx-view
1722 $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1723 foreach ($moduleswithblocks as $modname) {
1724 if (!$dbman->table_exists($modname)) {
1725 continue;
1727 $DB->execute("UPDATE {block_instances} SET
1728 contextid = (
1729 SELECT {context}.id
1730 FROM {context}
1731 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1732 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1733 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1734 WHERE {{$modname}}.id = pageid
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(
1781 'l' => 'side-pre',
1782 'r' => 'side-post',
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)) {
1789 continue;
1791 $newblock = new stdClass();
1792 $newblock->blockname = $blocks[$stickyblock->blockid]->name;
1793 $newblock->contextid = $syscontext->id;
1794 $newblock->showinsubcontexts = 1;
1795 switch ($stickyblock->pagetype) {
1796 case 'course-view':
1797 $newblock->pagetypepattern = 'course-view-*';
1798 break;
1799 default:
1800 $newblock->pagetypepattern = $stickyblock->pagetype;
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') . "
1851 FROM {course} c
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
1856 THEN ''
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');
1920 } else {
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
1954 unset($configs);
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
1967 unset($configs);
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);
1979 unset($caps);
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) {
1989 continue;
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);
2137 unset($contextids);
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'));
2336 $site = get_site();
2338 $a = new StdClass;
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();
2384 $rs->close();
2385 unset($rs);
2389 unset($rec);
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.
2399 // This may grow!
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();
2417 $rs->close();
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";
2599 $DB->execute($sql);
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);
2669 } else {
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);
2687 } else {
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);
2705 } else {
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);
2723 } else {
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);
2741 } else {
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);
2759 } else {
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);
2778 } else {
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);
2796 } else {
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);
2814 } else {
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('viewcap'=>'moodle/course:view', 'participatecap'=>'moodle/course:participate');
2829 $sql = "UPDATE {role_capabilities} SET capability = :participatecap WHERE capability = :viewcap";
2830 $DB->execute($sql, $params);
2831 $sql = "UPDATE {capabilities} SET name = :participatecap WHERE name = :viewcap";
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
2848 FROM {role} r
2849 JOIN {role_capabilities} rc ON rc.roleid = r.id
2850 WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
2851 ORDER BY r.id";
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') {
2859 $i = '';
2860 if ($DB->record_exists('role', array('shortname'=>'manager'))) {
2861 $i = 2;
2862 while($DB->record_exists('role', array('shortname'=>'manager'.$i))) {
2863 $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);
2875 $roles->close();
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
2888 ORDER BY ra.id";
2889 $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
2890 $admins = array();
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');
2957 } else {
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');
2980 } else {
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),
3027 'guest' => array(),
3028 'user' => array(),
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);
3064 // release memory!!
3065 unset($roles);
3066 unset($defaults);
3067 unset($rcl);
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);
3101 // tweak langlist
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',
3316 itemid = 0
3317 WHERE commentarea = 'block_comments'
3318 AND itemid != 0
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);
3714 if ($users) {
3715 set_config('courserequestnotify', implode(',', $users));
3716 } else {
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);
3729 } else {
3730 unset_config('notifyloginfailures'); // let them choose
3732 unset($admins);
3734 } else if ($CFG->notifyloginfailures == 'alladmins') {
3735 set_config('notifyloginfailures', '$@ALL@$');
3737 } else {
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 = :defaultrole WHERE defaultrole = 0";
3937 $params = array('defaultrole' => $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');
3959 } else {
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
3976 FROM {course}
3977 WHERE id <> :siteid";
3978 $DB->execute($sql, $params);
3980 // enable self enrol only when course enrollable
3981 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
3982 notifyall, password, roleid, timecreated, timemodified)
3983 SELECT 'self', 0, id, 1, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
3984 notifystudents, password, defaultrole, timecreated, timemodified
3985 FROM {course}
3986 WHERE enrollable = 1 AND id <> :siteid";
3987 $DB->execute($sql, $params);
3989 // enable guest access if previously allowed - separately with or without password
3990 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, timecreated, timemodified)
3991 SELECT 'guest', 0, id, 2, timecreated, timemodified
3992 FROM {course}
3993 WHERE guest = 1 AND id <> :siteid";
3994 $DB->execute($sql, $params);
3995 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, password, timecreated, timemodified)
3996 SELECT 'guest', 0, id, 2, password, timecreated, timemodified
3997 FROM {course}
3998 WHERE guest = 2 and password <> '$sqlempty' AND id <> :siteid";
3999 $DB->execute($sql, $params);
4001 upgrade_main_savepoint(true, 2010061900.06);
4004 if ($oldversion < 2010061900.07) {
4005 // now migrate old style "interactive" enrol plugins - we know them by looking into course.enrol
4006 $params = array('siteid'=>SITEID);
4007 $enabledplugins = explode(',', $CFG->enrol_plugins_enabled);
4008 $usedplugins = $DB->get_fieldset_sql("SELECT DISTINCT enrol FROM {course}");
4009 foreach ($usedplugins as $plugin) {
4010 if ($plugin === 'manual') {
4011 continue;
4013 $enabled = in_array($plugin, $enabledplugins) ? 0 : 1; // 0 means active, 1 disabled
4014 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
4015 notifyall, password, cost, currency, roleid, timecreated, timemodified)
4016 SELECT enrol, $enabled, id, 4, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
4017 notifystudents, password, cost, currency, defaultrole, timecreated, timemodified
4018 FROM {course}
4019 WHERE enrol = :plugin AND id <> :siteid";
4020 $params['plugin'] = $plugin;
4021 $DB->execute($sql, $params);
4023 upgrade_main_savepoint(true, 2010061900.07);
4026 if ($oldversion < 2010061900.08) {
4027 // now migrate the rest - these plugins are not in course.enrol, instead we just look for suspicious role assignments,
4028 // unfortunately old enrol plugins were doing sometimes weird role assignments :-(
4030 // enabled
4031 $enabledplugins = explode(',', $CFG->enrol_plugins_enabled);
4032 list($sqlenabled, $params) = $DB->get_in_or_equal($enabledplugins, SQL_PARAMS_NAMED, 'ena00');
4033 $params['siteid'] = SITEID;
4034 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
4035 notifyall, password, cost, currency, roleid, timecreated, timemodified)
4036 SELECT DISTINCT ra.enrol, 0, c.id, 5, c.enrolperiod, c.enrolstartdate, c.enrolenddate, c.expirynotify, c.expirythreshold,
4037 c.notifystudents, c.password, c.cost, c.currency, c.defaultrole, c.timecreated, c.timemodified
4038 FROM {course} c
4039 JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = 50)
4040 JOIN {role_assignments} ra ON (ra.contextid = ctx.id)
4041 WHERE c.id <> :siteid AND ra.enrol $sqlenabled";
4042 $processed = $DB->get_fieldset_sql("SELECT DISTINCT enrol FROM {enrol}");
4043 if ($processed) {
4044 list($sqlnotprocessed, $params2) = $DB->get_in_or_equal($processed, SQL_PARAMS_NAMED, 'np00', false);
4045 $params = array_merge($params, $params2);
4046 $sql = "$sql AND ra.enrol $sqlnotprocessed";
4048 $DB->execute($sql, $params);
4050 // disabled
4051 $params = array('siteid' => SITEID);
4052 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
4053 notifyall, password, cost, currency, roleid, timecreated, timemodified)
4054 SELECT DISTINCT ra.enrol, 1, c.id, 5, c.enrolperiod, c.enrolstartdate, c.enrolenddate, c.expirynotify, c.expirythreshold,
4055 c.notifystudents, c.password, c.cost, c.currency, c.defaultrole, c.timecreated, c.timemodified
4056 FROM {course} c
4057 JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = 50)
4058 JOIN {role_assignments} ra ON (ra.contextid = ctx.id)
4059 WHERE c.id <> :siteid";
4060 $processed = $DB->get_fieldset_sql("SELECT DISTINCT enrol FROM {enrol}");
4061 if ($processed) {
4062 list($sqlnotprocessed, $params2) = $DB->get_in_or_equal($processed, SQL_PARAMS_NAMED, 'np00', false);
4063 $params = array_merge($params, $params2);
4064 $sql = "$sql AND ra.enrol $sqlnotprocessed";
4066 $DB->execute($sql, $params);
4068 upgrade_main_savepoint(true, 2010061900.08);
4071 if ($oldversion < 2010061900.09) {
4072 // unfortunately there may be still some leftovers
4073 // after reconfigured, uninstalled or borked enrol plugins,
4074 // unfortunately this may be a bit slow - but there should not be many of these
4075 $sqlempty = $DB->sql_empty();
4076 $sql = "SELECT DISTINCT c.id AS courseid, ra.enrol, c.timecreated, c.timemodified
4077 FROM {course} c
4078 JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = 50)
4079 JOIN {role_assignments} ra ON (ra.contextid = ctx.id AND ra.enrol <> '$sqlempty')
4080 LEFT JOIN {enrol} e ON (e.courseid = c.id AND e.enrol = ra.enrol)
4081 WHERE c.id <> :siteid AND e.id IS NULL";
4082 $params = array('siteid'=>SITEID);
4083 $rs = $DB->get_recordset_sql($sql, $params);
4084 foreach ($rs as $enrol) {
4085 $enrol->status = 1; // better disable them
4086 $DB->insert_record('enrol', $enrol);
4088 $rs->close();
4089 upgrade_main_savepoint(true, 2010061900.09);
4092 if ($oldversion < 2010061900.10) {
4093 // migrate existing setup of meta courses
4094 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, customint1)
4095 SELECT 'meta', 0, parent_course, 5, child_course
4096 FROM {course_meta}";
4097 $DB->execute($sql);
4099 upgrade_main_savepoint(true, 2010061900.10);
4102 if ($oldversion < 2010061900.11) {
4103 // nuke any old role assignments+enrolments in previous meta courses, we have to start from scratch
4104 $select = "SELECT ctx.id
4105 FROM {context} ctx
4106 JOIN {course} c ON (c.id = ctx.instanceid AND ctx.contextlevel = 50 AND c.metacourse = 1)";
4107 $DB->delete_records_select('role_assignments', "contextid IN ($select) AND enrol = 'manual'");
4109 // course_meta to be dropped - we use enrol_meta plugin instead now
4110 $table = new xmldb_table('course_meta');
4112 // Launch drop table for course_meta
4113 $dbman->drop_table($table);
4115 // Main savepoint reached
4116 upgrade_main_savepoint(true, 2010061900.11);
4119 if ($oldversion < 2010061900.12) {
4120 // finally remove all obsolete fields from the course table - yay!
4121 // all the information was migrated to the enrol table
4123 // Define field guest to be dropped from course
4124 $table = new xmldb_table('course');
4125 $field = new xmldb_field('guest');
4127 // Conditionally launch drop field guest
4128 if ($dbman->field_exists($table, $field)) {
4129 $dbman->drop_field($table, $field);
4132 // Define field password to be dropped from course
4133 $table = new xmldb_table('course');
4134 $field = new xmldb_field('password');
4136 // Conditionally launch drop field password
4137 if ($dbman->field_exists($table, $field)) {
4138 $dbman->drop_field($table, $field);
4141 // Define field enrolperiod to be dropped from course
4142 $table = new xmldb_table('course');
4143 $field = new xmldb_field('enrolperiod');
4145 // Conditionally launch drop field enrolperiod
4146 if ($dbman->field_exists($table, $field)) {
4147 $dbman->drop_field($table, $field);
4150 // Define field cost to be dropped from course
4151 $table = new xmldb_table('course');
4152 $field = new xmldb_field('cost');
4154 // Conditionally launch drop field cost
4155 if ($dbman->field_exists($table, $field)) {
4156 $dbman->drop_field($table, $field);
4159 // Define field currency to be dropped from course
4160 $table = new xmldb_table('course');
4161 $field = new xmldb_field('currency');
4163 // Conditionally launch drop field currency
4164 if ($dbman->field_exists($table, $field)) {
4165 $dbman->drop_field($table, $field);
4168 // Define field metacourse to be dropped from course
4169 $table = new xmldb_table('course');
4170 $field = new xmldb_field('metacourse');
4172 // Conditionally launch drop field metacourse
4173 if ($dbman->field_exists($table, $field)) {
4174 $dbman->drop_field($table, $field);
4177 // Define field expirynotify to be dropped from course
4178 $table = new xmldb_table('course');
4179 $field = new xmldb_field('expirynotify');
4181 // Conditionally launch drop field expirynotify
4182 if ($dbman->field_exists($table, $field)) {
4183 $dbman->drop_field($table, $field);
4186 // Define field expirythreshold to be dropped from course
4187 $table = new xmldb_table('course');
4188 $field = new xmldb_field('expirythreshold');
4190 // Conditionally launch drop field expirythreshold
4191 if ($dbman->field_exists($table, $field)) {
4192 $dbman->drop_field($table, $field);
4195 // Define field notifystudents to be dropped from course
4196 $table = new xmldb_table('course');
4197 $field = new xmldb_field('notifystudents');
4199 // Conditionally launch drop field notifystudents
4200 if ($dbman->field_exists($table, $field)) {
4201 $dbman->drop_field($table, $field);
4204 // Define field enrollable to be dropped from course
4205 $table = new xmldb_table('course');
4206 $field = new xmldb_field('enrollable');
4208 // Conditionally launch drop field enrollable
4209 if ($dbman->field_exists($table, $field)) {
4210 $dbman->drop_field($table, $field);
4213 // Define field enrolstartdate to be dropped from course
4214 $table = new xmldb_table('course');
4215 $field = new xmldb_field('enrolstartdate');
4217 // Conditionally launch drop field enrolstartdate
4218 if ($dbman->field_exists($table, $field)) {
4219 $dbman->drop_field($table, $field);
4222 // Define field enrolenddate to be dropped from course
4223 $table = new xmldb_table('course');
4224 $field = new xmldb_field('enrolenddate');
4226 // Conditionally launch drop field enrolenddate
4227 if ($dbman->field_exists($table, $field)) {
4228 $dbman->drop_field($table, $field);
4231 // Define field enrol to be dropped from course
4232 $table = new xmldb_table('course');
4233 $field = new xmldb_field('enrol');
4235 // Conditionally launch drop field enrol
4236 if ($dbman->field_exists($table, $field)) {
4237 $dbman->drop_field($table, $field);
4240 // Define field defaultrole to be dropped from course
4241 $table = new xmldb_table('course');
4242 $field = new xmldb_field('defaultrole');
4244 // Conditionally launch drop field defaultrole
4245 if ($dbman->field_exists($table, $field)) {
4246 $dbman->drop_field($table, $field);
4249 upgrade_main_savepoint(true, 2010061900.12);
4252 if ($oldversion < 2010061900.13) {
4253 // Define field visibleold to be added to course_categories
4254 $table = new xmldb_table('course_categories');
4255 $field = new xmldb_field('visibleold', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'visible');
4257 // Launch add field visibleold
4258 $dbman->add_field($table, $field);
4260 // Main savepoint reached
4261 upgrade_main_savepoint(true, 2010061900.13);
4264 if ($oldversion < 2010061900.14) {
4265 // keep previous visible state
4266 $DB->execute("UPDATE {course_categories} SET visibleold = visible");
4268 // make sure all subcategories of hidden categories are hidden too, do not rely on category path yet
4269 $sql = "SELECT c.id
4270 FROM {course_categories} c
4271 JOIN {course_categories} pc ON (pc.id = c.parent AND pc.visible = 0)
4272 WHERE c.visible = 1";
4273 while ($categories = $DB->get_records_sql($sql)) {
4274 foreach ($categories as $cat) {
4275 $DB->set_field('course_categories', 'visible', 0, array('id'=>$cat->id));
4278 upgrade_main_savepoint(true, 2010061900.14);
4281 if ($oldversion < 2010061900.15) {
4282 // Define field visibleold to be added to course
4283 $table = new xmldb_table('course');
4284 $field = new xmldb_field('visibleold', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'visible');
4286 // Launch add field visibleold
4287 $dbman->add_field($table, $field);
4289 // Main savepoint reached
4290 upgrade_main_savepoint(true, 2010061900.15);
4293 if ($oldversion < 2010061900.16) {
4294 // keep previous visible state
4295 $DB->execute("UPDATE {course} SET visibleold = visible");
4297 // make sure all courses in hidden categories are hidden
4298 $DB->execute("UPDATE {course} SET visible = 0 WHERE category IN (SELECT id FROM {course_categories} WHERE visible = 0)");
4300 upgrade_main_savepoint(true, 2010061900.16);
4303 if ($oldversion < 2010061900.20) {
4304 // now set up the enrolments - look for roles with course:participate only at course context - the category enrolments are synchronised later by archetype and new capability
4306 $syscontext = get_context_instance(CONTEXT_SYSTEM);
4307 $params = array('syscontext'=>$syscontext->id, 'participate'=>'moodle/course:participate');
4308 $roles = $DB->get_fieldset_sql("SELECT DISTINCT roleid FROM {role_capabilities} WHERE contextid = :syscontext AND capability = :participate AND permission = 1", $params);
4309 if ($roles) {
4310 list($sqlroles, $params) = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED, 'r00');
4312 $sql = "INSERT INTO {user_enrolments} (status, enrolid, userid, timestart, timeend, modifierid, timecreated, timemodified)
4314 SELECT 0, e.id, ra.userid, MIN(ra.timestart), MIN(ra.timeend), 0, MIN(ra.timemodified), MAX(ra.timemodified)
4315 FROM {role_assignments} ra
4316 JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = 50)
4317 JOIN {enrol} e ON (e.enrol = ra.enrol AND e.courseid = c.instanceid)
4318 JOIN {user} u ON u.id = ra.userid
4319 WHERE u.deleted = 0 AND ra.roleid $sqlroles
4320 GROUP BY e.id, ra.userid";
4321 $DB->execute($sql, $params);
4324 upgrade_main_savepoint(true, 2010061900.20);
4327 if ($oldversion < 2010061900.21) {
4328 // hidden is completely removed, timestart+timeend are now in the user_enrolments table
4330 // Define field hidden to be dropped from role_assignments
4331 $table = new xmldb_table('role_assignments');
4332 $field = new xmldb_field('hidden');
4334 // Conditionally launch drop field hidden
4335 if ($dbman->field_exists($table, $field)) {
4336 $dbman->drop_field($table, $field);
4339 // Define field timestart to be dropped from role_assignments
4340 $table = new xmldb_table('role_assignments');
4341 $field = new xmldb_field('timestart');
4343 // Conditionally launch drop field timestart
4344 if ($dbman->field_exists($table, $field)) {
4345 $dbman->drop_field($table, $field);
4348 // Define field timeend to be dropped from role_assignments
4349 $table = new xmldb_table('role_assignments');
4350 $field = new xmldb_field('timeend');
4352 // Conditionally launch drop field timeend
4353 if ($dbman->field_exists($table, $field)) {
4354 $dbman->drop_field($table, $field);
4357 // Main savepoint reached
4358 upgrade_main_savepoint(true, 2010061900.21);
4361 if ($oldversion < 2010061900.22) {
4362 // Rename field enrol on table role_assignments to component and update content
4364 $table = new xmldb_table('role_assignments');
4365 $field = new xmldb_field('enrol', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'modifierid');
4367 // Launch rename field enrol
4368 $dbman->rename_field($table, $field, 'component');
4370 // Changing precision of field component on table role_assignments to (100)
4371 $table = new xmldb_table('role_assignments');
4372 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'modifierid');
4374 // Launch change of precision for field component
4375 $dbman->change_field_precision($table, $field);
4377 // Manual is a special case - we use empty string instead now
4378 $params = array('empty'=>$DB->sql_empty(), 'manual'=>'manual');
4379 $sql = "UPDATE {role_assignments}
4380 SET component = :empty
4381 WHERE component = :manual";
4382 $DB->execute($sql, $params);
4384 // Now migrate to real enrol component names
4385 $params = array('empty'=>$DB->sql_empty());
4386 $concat = $DB->sql_concat("'enrol_'", 'component');
4387 $sql = "UPDATE {role_assignments}
4388 SET component = $concat
4389 WHERE component <> :empty
4390 AND contextid IN (
4391 SELECT id
4392 FROM {context}
4393 WHERE contextlevel >= 50)";
4394 $DB->execute($sql, $params);
4396 // Now migrate to real auth component names
4397 $params = array('empty'=>$DB->sql_empty());
4398 $concat = $DB->sql_concat("'auth_'", 'component');
4399 $sql = "UPDATE {role_assignments}
4400 SET component = $concat
4401 WHERE component <> :empty
4402 AND contextid IN (
4403 SELECT id
4404 FROM {context}
4405 WHERE contextlevel < 50)";
4406 $DB->execute($sql, $params);
4408 // Main savepoint reached
4409 upgrade_main_savepoint(true, 2010061900.22);
4412 if ($oldversion < 2010061900.23) {
4413 // add proper itemid to role assignments that were added by enrolment plugins
4414 $sql = "UPDATE {role_assignments}
4415 SET itemid = (SELECT MIN({enrol}.id)
4416 FROM {enrol}
4417 JOIN {context} ON ({context}.contextlevel = 50 AND {context}.instanceid = {enrol}.courseid)
4418 WHERE {role_assignments}.component = ".$DB->sql_concat("'enrol_'", "{enrol}.enrol")." AND {context}.id = {role_assignments}.contextid)
4419 WHERE component <> 'enrol_manual' AND component LIKE 'enrol_%'";
4420 $DB->execute($sql);
4421 // Main savepoint reached
4422 upgrade_main_savepoint(true, 2010061900.23);
4425 if ($oldversion < 2010061900.30) {
4426 // make new list of active enrol plugins - order is important, meta should be always last, manual first
4427 $enabledplugins = explode(',', $CFG->enrol_plugins_enabled);
4428 $enabledplugins = array_merge(array('manual', 'guest', 'self', 'cohort'), $enabledplugins);
4429 if ($DB->record_exists('enrol', array('enrol'=>'meta'))) {
4430 $enabledplugins[] = 'meta';
4432 $enabledplugins = array_unique($enabledplugins);
4433 set_config('enrol_plugins_enabled', implode(',', $enabledplugins));
4435 // Main savepoint reached
4436 upgrade_main_savepoint(true, 2010061900.30);
4439 if ($oldversion < 2010061900.31) {
4440 // finalise all new enrol settings and cleanup old settings
4442 // legacy allowunenrol was deprecated in 1.9 already
4443 unset_config('allwunenroll');
4445 // obsolete course presets
4446 unset_config('metacourse', 'moodlecourse');
4447 unset_config('enrol', 'moodlecourse');
4448 unset_config('enrollable', 'moodlecourse');
4449 unset_config('enrolperiod', 'moodlecourse');
4450 unset_config('expirynotify', 'moodlecourse');
4451 unset_config('notifystudents', 'moodlecourse');
4452 unset_config('expirythreshold', 'moodlecourse');
4453 unset_config('enrolpassword', 'moodlecourse');
4454 unset_config('guest', 'moodlecourse');
4456 unset_config('backup_sche_metacourse', 'backup');
4458 unset_config('lastexpirynotify');
4460 // hidden course categories now prevent only browsing, courses are accessible if you know the URL and course is visible
4461 unset_config('allowvisiblecoursesinhiddencategories');
4463 if (isset($CFG->coursemanager)) {
4464 set_config('coursecontact', $CFG->coursemanager);
4465 unset_config('coursemanager');
4468 // migrate plugin settings - the problem here is we are splitting manual into three different plugins
4469 if (isset($CFG->enrol_manual_usepasswordpolicy)) {
4470 set_config('usepasswordpolicy', $CFG->enrol_manual_usepasswordpolicy, 'enrol_guest');
4471 set_config('usepasswordpolicy', $CFG->enrol_manual_usepasswordpolicy, 'enrol_self');
4472 set_config('groupenrolmentkeypolicy', $CFG->enrol_manual_usepasswordpolicy);
4473 unset_config('enrol_manual_usepasswordpolicy');
4475 if (isset($CFG->enrol_manual_requirekey)) {
4476 set_config('requirepassword', $CFG->enrol_manual_requirekey, 'enrol_guest');
4477 set_config('requirepassword', $CFG->enrol_manual_requirekey, 'enrol_self');
4478 unset_config('enrol_manual_requirekey');
4480 if (isset($CFG->enrol_manual_showhint)) {
4481 set_config('showhint', $CFG->enrol_manual_showhint, 'enrol_guest');
4482 set_config('showhint', $CFG->enrol_manual_showhint, 'enrol_self');
4483 unset_config('enrol_manual_showhint');
4486 upgrade_main_savepoint(true, 2010061900.31);
4489 if ($oldversion < 2010061900.32) {
4490 // MDL-22797 course completion has to be updated to use new enrol framework, it will not be enabled in final 2.0
4491 set_config('enableavailability', 0);
4492 set_config('enablecompletion', 0);
4493 upgrade_main_savepoint(true, 2010061900.32);
4496 if ($oldversion < 2010062101) {
4498 /// Define field huburl to be dropped from course_published
4499 $table = new xmldb_table('course_published');
4500 $field = new xmldb_field('hubid');
4502 /// Conditionally launch drop field huburl
4503 if ($dbman->field_exists($table, $field)) {
4504 $dbman->drop_field($table, $field);
4507 /// Define field huburl to be added to course_published
4508 $table = new xmldb_table('course_published');
4509 $field = new xmldb_field('huburl', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'id');
4511 /// Conditionally launch add field huburl
4512 if (!$dbman->field_exists($table, $field)) {
4513 $dbman->add_field($table, $field);
4516 /// Main savepoint reached
4517 upgrade_main_savepoint(true, 2010062101);
4520 if ($oldversion < 2010070300) {
4521 //TODO: this is a temporary hack for upgrade from PR3, to be removed later
4523 // Define field component to be added to files
4524 $table = new xmldb_table('files');
4525 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'contextid');
4527 // Conditionally upgrade from PR3
4528 if (!$dbman->field_exists($table, $field)) {
4529 $dbman->add_field($table, $field);
4530 $index = new xmldb_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid'));
4531 $dbman->drop_index($table, $index);
4532 $index = new xmldb_index('component-filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('component', 'filearea', 'contextid', 'itemid'));
4533 $dbman->add_index($table, $index);
4535 // Rename areas as add proper component
4536 $areas = $DB->get_fieldset_sql("SELECT DISTINCT filearea FROM {files}");
4537 if ($areas) {
4538 // fix incorrect itemids
4539 $DB->execute("UPDATE {files} SET itemid = 0 WHERE filearea = 'category_description'"); // context identifies instances
4540 $DB->execute("UPDATE {files} SET itemid = 0 WHERE filearea = 'user_profile'"); // context identifies instances
4541 $DB->execute("UPDATE {files} SET itemid = 0 WHERE filearea = 'block_html'"); // context identifies instances
4542 foreach ($areas as $area) {
4543 // rename areas
4544 if ($area === 'course_backup') {
4545 $area = 'backup_course';
4546 } else if ($area === 'section_backup') {
4547 $area = 'backup_section';
4548 } else if ($area === 'activity_backup') {
4549 $area = 'backup_activity';
4550 } else if ($area === 'category_description') {
4551 $area = 'coursecat_description';
4553 if ($area === 'block_html') {
4554 $component = 'block_html';
4555 $filearea = 'content';
4556 } else {
4557 list($component, $filearea) = explode('_', $area, 2);
4558 // note this is just a hack which guesses plugin from old PRE3 files code, the whole point of adding component is to get rid of this guessing
4559 if (file_exists("$CFG->dirroot/mod/$component/lib.php")) {
4560 $component = 'mod_'.$component;
4563 $DB->execute("UPDATE {files} SET component = :component, filearea = :filearea WHERE filearea = :area", array('component'=>$component, 'filearea'=>$filearea, 'area'=>$area));
4565 // Update all hashes
4566 $rs = $DB->get_recordset('files', array());
4567 foreach ($rs as $file) {
4568 $pathnamehash = sha1("/$file->contextid/$file->component/$file->filearea/$file->itemid".$file->filepath.$file->filename);
4569 $DB->set_field('files', 'pathnamehash', $pathnamehash, array('id'=>$file->id));
4571 $rs->close();
4575 // Main savepoint reached
4576 upgrade_main_savepoint(true, 2010070300);
4579 if ($oldversion < 2010070500) {
4581 /// Define field operation to be added to backup_controllers
4582 $table = new xmldb_table('backup_controllers');
4583 $field = new xmldb_field('operation', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'backup', 'backupid');
4585 /// Conditionally launch add field operation
4586 if (!$dbman->field_exists($table, $field)) {
4587 $dbman->add_field($table, $field);
4590 /// Main savepoint reached
4591 upgrade_main_savepoint(true, 2010070500);
4594 if ($oldversion < 2010070501) {
4596 /// Define field suspended to be added to user
4597 $table = new xmldb_table('user');
4598 $field = new xmldb_field('suspended', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'deleted');
4600 /// Conditionally launch add field suspended
4601 if (!$dbman->field_exists($table, $field)) {
4602 $dbman->add_field($table, $field);
4605 /// Main savepoint reached
4606 upgrade_main_savepoint(true, 2010070501);
4609 if ($oldversion < 2010070502) {
4611 /// Define field newitemid to be added to backup_ids_template
4612 $table = new xmldb_table('backup_ids_template');
4613 $field = new xmldb_field('newitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'itemid');
4614 /// Conditionally launch add field newitemid
4615 if (!$dbman->field_exists($table, $field)) {
4616 $dbman->add_field($table, $field);
4619 /// Define field info to be added to backup_ids_template
4620 $table = new xmldb_table('backup_ids_template');
4621 $field = new xmldb_field('info', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'parentitemid');
4622 /// Conditionally launch add field info
4623 if (!$dbman->field_exists($table, $field)) {
4624 $dbman->add_field($table, $field);
4627 /// Main savepoint reached
4628 upgrade_main_savepoint(true, 2010070502);
4631 if ($oldversion < 2010070601) {
4632 // delete loan calc if not used - it was moved to contrib
4633 if (!file_exists("$CFG->dirroot/blocks/loancalc/version.php")) {
4634 if (!$DB->record_exists('block_instances', array('blockname'=>'loancalc'))) {
4635 $DB->delete_records('block', array('name'=>'loancalc'));
4638 upgrade_main_savepoint(true, 2010070601);
4641 if ($oldversion < 2010070602) {
4642 // delete exercise if not used and not installed - now in contrib (do not use adminlib uninstall functions, they may change)
4643 if (!file_exists("$CFG->dirroot/mod/exercise/version.php")) {
4644 if ($module = $DB->get_record('modules', array('name'=>'exercise'))) {
4645 if (!$DB->record_exists('course_modules', array('module'=>$module->id))) {
4646 //purge capabilities
4647 $DB->delete_records_select('capabilities', "name LIKE ?", array('mod/exercise:%'));
4648 $DB->delete_records_select('role_capabilities', "capability LIKE ?", array('mod/exercise:%'));
4649 $tables = array('exercise', 'exercise_submissions', 'exercise_assessments', 'exercise_elements', 'exercise_rubrics', 'exercise_grades');
4650 foreach ($tables as $tname) {
4651 $table = new xmldb_table($tname);
4652 if ($dbman->table_exists($table)) {
4653 $dbman->drop_table($table);
4656 $DB->delete_records('event', array('modulename' => 'exercise'));
4657 $DB->delete_records('log', array('module' => 'exercise'));
4658 $DB->delete_records('modules', array('name'=>'exercise'));
4662 upgrade_main_savepoint(true, 2010070602);
4665 if ($oldversion < 2010070603) {
4666 // delete journal if not used and not installed - now in contrib (do not use adminlib uninstall functions, they may change)
4667 if (!file_exists("$CFG->dirroot/mod/journal/version.php")) {
4668 if ($module = $DB->get_record('modules', array('name'=>'journal'))) {
4669 if (!$DB->record_exists('course_modules', array('module'=>$module->id))) {
4670 //purge capabilities
4671 $DB->delete_records_select('capabilities', "name LIKE ?", array('mod/journal:%'));
4672 $DB->delete_records_select('role_capabilities', "capability LIKE ?", array('mod/journal:%'));
4673 $tables = array('journal', 'journal_entries');
4674 foreach ($tables as $tname) {
4675 $table = new xmldb_table($tname);
4676 if ($dbman->table_exists($table)) {
4677 $dbman->drop_table($table);
4680 $DB->delete_records('event', array('modulename' => 'journal'));
4681 $DB->delete_records('log', array('module' => 'journal'));
4682 $DB->delete_records('modules', array('name'=>'journal'));
4683 unset_config('journal_initialdisable');
4687 upgrade_main_savepoint(true, 2010070603);
4690 if ($oldversion < 2010070604) {
4691 // delete lams if not used and not installed - now in contrib (do not use adminlib uninstall functions, they may change)
4692 if (!file_exists("$CFG->dirroot/mod/lams/version.php")) {
4693 if ($module = $DB->get_record('modules', array('name'=>'lams'))) {
4694 if (!$DB->record_exists('course_modules', array('module'=>$module->id))) {
4695 //purge capabilities
4696 $DB->delete_records_select('capabilities', "name LIKE ?", array('mod/lams:%'));
4697 $DB->delete_records_select('role_capabilities', "capability LIKE ?", array('mod/lams:%'));
4698 $tables = array('lams', '');
4699 foreach ($tables as $tname) {
4700 $table = new xmldb_table($tname);
4701 if ($dbman->table_exists($table)) {
4702 $dbman->drop_table($table);
4705 $DB->delete_records('event', array('modulename' => 'lams'));
4706 $DB->delete_records('log', array('module' => 'lams'));
4707 $DB->delete_records('modules', array('name'=>'lams'));
4708 unset_config('lams_initialdisable');
4712 upgrade_main_savepoint(true, 2010070604);
4715 if ($oldversion < 2010070801) {
4716 /// Before changing the field, drop dependent indexes
4717 /// Define index shortname (not unique) to be dropped form course_request
4718 $table = new xmldb_table('user');
4719 $index = new xmldb_index('city', XMLDB_INDEX_NOTUNIQUE, array('city'));
4720 /// Conditionally launch drop index shortname
4721 if ($dbman->index_exists($table, $index)) {
4722 $dbman->drop_index($table, $index);
4724 /// Changing precision of field city on table user to (120)
4725 $field = new xmldb_field('city', XMLDB_TYPE_CHAR, '120', null, XMLDB_NOTNULL, null, null, 'address');
4727 /// Launch change of precision for field city
4728 $dbman->change_field_precision($table, $field);
4730 /// Conditionally launch add index typeitem_ix
4731 if (!$dbman->index_exists($table, $index)) {
4732 $dbman->add_index($table, $index);
4734 /// Main savepoint reached
4735 upgrade_main_savepoint(true, 2010070801);
4738 if ($oldversion < 2010071000) {
4739 //purge unused editor settings
4740 unset_config('editorbackgroundcolor');
4741 unset_config('editorfontfamily');
4742 unset_config('editorfontsize');
4743 unset_config('editorkillword');
4744 unset_config('editorhidebuttons');
4745 unset_config('editorfontlist');
4747 upgrade_main_savepoint(true, 2010071000);
4750 if ($oldversion < 2010071001) {
4751 // purge obsolete stats settings
4752 unset_config('statscatdepth');
4753 upgrade_main_savepoint(true, 2010071001);
4756 if ($oldversion < 2010071100) {
4757 // move user icons to file storage pool
4758 upgrade_migrate_user_icons();
4759 upgrade_main_savepoint(true, 2010071100);
4762 if ($oldversion < 2010071101) {
4763 // move user icons to file storage pool
4764 upgrade_migrate_group_icons();
4765 upgrade_main_savepoint(true, 2010071101);
4768 if ($oldversion < 2010071300) {
4769 // Define field timecreated to be added to user_enrolments
4770 $table = new xmldb_table('user_enrolments');
4771 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'modifierid');
4773 // Launch add field timecreated
4774 if (!$dbman->field_exists($table, $field)) {
4775 $dbman->add_field($table, $field);
4778 // now try to guess the time created
4779 $sql = "UPDATE {user_enrolments} SET timecreated = timemodified WHERE timecreated = 0";
4780 $DB->execute($sql);
4781 $sql = "UPDATE {user_enrolments} SET timecreated = timestart WHERE timestart <> 0 AND timestart < timemodified";
4782 $DB->execute($sql);
4784 upgrade_main_savepoint(true, 2010071300);
4787 if ($oldversion < 2010071700) { // Make itemname bigger (160cc) to store component+filearea
4789 $table = new xmldb_table('backup_ids_template');
4790 // Define key backupid_itemname_itemid_uk (unique) to be dropped form backup_ids_template
4791 $key = new xmldb_key('backupid_itemname_itemid_uk', XMLDB_KEY_UNIQUE, array('backupid', 'itemname', 'itemid'));
4792 // Define index backupid_parentitemid_ix (not unique) to be dropped form backup_ids_template
4793 $index = new xmldb_index('backupid_parentitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'parentitemid'));
4794 // Define field itemname to be 160cc
4795 $field = new xmldb_field('itemname', XMLDB_TYPE_CHAR, '160', null, XMLDB_NOTNULL, null, null, 'backupid');
4797 // Launch drop key backupid_itemname_itemid_uk
4798 $dbman->drop_key($table, $key);
4799 // Conditionally launch drop index backupid_parentitemid_ix
4800 if ($dbman->index_exists($table, $index)) {
4801 $dbman->drop_index($table, $index);
4804 // Changing precision of field itemname on table backup_ids_template to (160)
4805 $dbman->change_field_precision($table, $field);
4807 // Launch add key backupid_itemname_itemid_uk
4808 $dbman->add_key($table, $key);
4809 // Conditionally launch add index backupid_parentitemid_ix
4810 if (!$dbman->index_exists($table, $index)) {
4811 $dbman->add_index($table, $index);
4814 // Main savepoint reached
4815 upgrade_main_savepoint(true, 2010071700);
4818 if ($oldversion < 2010071701) {
4819 // Drop legacy core tables that now belongs to mnetservice_enrol plugin
4820 // Upgrade procedure not needed as the tables are used for caching purposes only
4821 $tables = array('mnet_enrol_course', 'mnet_enrol_assignments');
4822 foreach ($tables as $tname) {
4823 $table = new xmldb_table($tname);
4824 if ($dbman->table_exists($table)) {
4825 $dbman->drop_table($table);
4829 upgrade_main_savepoint(true, 2010071701);
4832 if ($oldversion < 2010071800) {
4834 // Define table backup_files_template to be created
4835 $table = new xmldb_table('backup_files_template');
4837 // Adding fields to table backup_files_template
4838 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
4839 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
4840 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
4841 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
4842 $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
4843 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
4844 $table->add_field('info', XMLDB_TYPE_TEXT, 'medium', null, null, null, null);
4846 // Adding keys to table backup_files_template
4847 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
4849 // Adding indexes to table backup_files_template
4850 $table->add_index('backupid_contextid_component_filearea_itemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'contextid', 'component', 'filearea', 'itemid'));
4852 // Conditionally launch create table for backup_files_template
4853 if (!$dbman->table_exists($table)) {
4854 $dbman->create_table($table);
4857 // Main savepoint reached
4858 upgrade_main_savepoint(true, 2010071800);
4861 if ($oldversion < 2010072300) {
4863 // Define field capabilities to be added to external_functions
4864 $table = new xmldb_table('external_functions');
4865 $field = new xmldb_field('capabilities', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'component');
4867 // Conditionally launch add field capabilities
4868 if (!$dbman->field_exists($table, $field)) {
4869 $dbman->add_field($table, $field);
4872 // Main savepoint reached
4873 upgrade_main_savepoint(true, 2010072300);
4876 if ($oldversion < 2010072700) {
4878 // Define index backupid_itemname_newitemid_ix (not unique) to be added to backup_ids_template
4879 $table = new xmldb_table('backup_ids_template');
4880 $index = new xmldb_index('backupid_itemname_newitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'newitemid'));
4882 // Conditionally launch add index backupid_itemname_newitemid_ix
4883 if (!$dbman->index_exists($table, $index)) {
4884 $dbman->add_index($table, $index);
4887 // Main savepoint reached
4888 upgrade_main_savepoint(true, 2010072700);
4891 if ($oldversion < 2010080303) {
4892 $rs = $DB->get_recordset_sql('SELECT i.id, i.name, r.type FROM {repository_instances} i, {repository} r WHERE i.typeid = r.id');
4893 foreach ($rs as $record) {
4894 if ($record->name == $record->type) {
4895 // repository_instances was saving type name as in name field
4896 // which should be empty, the repository api will try to find
4897 // instance name from language files
4898 $DB->set_field('repository_instances', 'name', '');
4901 $rs->close();
4902 upgrade_main_savepoint(true, 2010080303);
4905 if ($oldversion < 2010080305) {
4906 // first drop all log display actions, we will recreate them automatically later
4907 $DB->delete_records('log_display', array());
4909 // Define field component to be added to log_display
4910 $table = new xmldb_table('log_display');
4911 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'field');
4913 // Launch add field component
4914 if (!$dbman->field_exists($table, $field)) {
4915 $dbman->add_field($table, $field);
4918 // Main savepoint reached
4919 upgrade_main_savepoint(true, 2010080305);
4922 if ($oldversion < 2010080900) {
4924 /// Define field generalfeedbackformat to be added to question
4925 $table = new xmldb_table('question');
4926 $field = new xmldb_field('generalfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'generalfeedback');
4928 /// Conditionally launch add field generalfeedbackformat
4929 if (!$dbman->field_exists($table, $field)) {
4930 $dbman->add_field($table, $field);
4933 /// Upgrading the text formats in some question types depends on the
4934 /// questiontextformat field, but the question type upgrade only runs
4935 /// after the code below has messed around with the questiontextformat
4936 /// value. Therefore, we need to create a new column to store the old value.
4937 /// The column should be dropped in Moodle 2.1.
4938 /// Define field oldquestiontextformat to be added to question
4939 $field = new xmldb_field('oldquestiontextformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'generalfeedback');
4941 /// Conditionally launch add field oldquestiontextformat
4942 if (!$dbman->field_exists($table, $field)) {
4943 $dbman->add_field($table, $field);
4946 /// Define field infoformat to be added to question_categories
4947 $table = new xmldb_table('question_categories');
4948 $field = new xmldb_field('infoformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'info');
4950 /// Conditionally launch add field infoformat
4951 if (!$dbman->field_exists($table, $field)) {
4952 $dbman->add_field($table, $field);
4955 /// Define field answerformat to be added to question_answers
4956 $table = new xmldb_table('question_answers');
4957 $field = new xmldb_field('answerformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'answer');
4959 /// Conditionally launch add field answerformat
4960 if (!$dbman->field_exists($table, $field)) {
4961 $dbman->add_field($table, $field);
4964 /// Define field feedbackformat to be added to question_answers
4965 $field = new xmldb_field('feedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'feedback');
4967 /// Conditionally launch add field feedbackformat
4968 if (!$dbman->field_exists($table, $field)) {
4969 $dbman->add_field($table, $field);
4972 /// Define field manualcommentformat to be added to question_sessions
4973 $table = new xmldb_table('question_sessions');
4974 $field = new xmldb_field('manualcommentformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
4976 /// Conditionally launch add field manualcommentformat
4977 if (!$dbman->field_exists($table, $field)) {
4978 $dbman->add_field($table, $field);
4981 /// Main savepoint reached
4982 upgrade_main_savepoint(true, 2010080900);
4985 /// updating question image
4986 if ($oldversion < 2010080901) {
4987 $fs = get_file_storage();
4989 // Define field image to be dropped from question
4990 $table = new xmldb_table('question');
4991 $field = new xmldb_field('image');
4993 // Conditionally launch drop field image
4994 if ($dbman->field_exists($table, $field)) {
4996 $rs = $DB->get_recordset('question');
4997 $textlib = textlib_get_instance();
4999 foreach ($rs as $question) {
5000 if (empty($question->image)) {
5001 continue;
5003 if (!$category = $DB->get_record('question_categories', array('id'=>$question->category))) {
5004 continue;
5006 $categorycontext = get_context_instance_by_id($category->contextid);
5007 // question files are stored in course level
5008 // so we have to find course context
5009 switch ($categorycontext->contextlevel){
5010 case CONTEXT_COURSE :
5011 $context = $categorycontext;
5012 break;
5013 case CONTEXT_MODULE :
5014 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$categorycontext->instanceid));
5015 $context = get_context_instance(CONTEXT_COURSE, $courseid);
5016 break;
5017 case CONTEXT_COURSECAT :
5018 case CONTEXT_SYSTEM :
5019 $context = get_system_context();
5020 break;
5021 default :
5022 continue;
5024 if ($textlib->substr($textlib->strtolower($question->image), 0, 7) == 'http://') {
5025 // it is a link, appending to existing question text
5026 $question->questiontext .= ' <img src="' . $question->image . '" />';
5027 $question->image = '';
5028 // update question record
5029 $DB->update_record('question', $question);
5030 } else {
5031 $filename = basename($question->image);
5032 $filepath = dirname($question->image);
5033 if (empty($filepath) or $filepath == '.' or $filepath == '/') {
5034 $filepath = '/';
5035 } else {
5036 // append /
5037 $filepath = '/'.trim($filepath, './@#$ ').'/';
5040 // course files already moved to file pool by previous upgrade block
5041 // so we just create copy from course_legacy area
5042 if ($image = $fs->get_file($context->id, 'course', 'legacy', 0, $filepath, $filename)) {
5043 // move files to file pool
5044 $file_record = array(
5045 'contextid'=>$category->contextid,
5046 'component'=>'question',
5047 'filearea'=>'questiontext',
5048 'itemid'=>$question->id
5050 $fs->create_file_from_storedfile($file_record, $image);
5051 $question->questiontext .= ' <img src="@@PLUGINFILE@@' . $filepath . $filename . '" />';
5052 $question->image = '';
5053 // update question record
5054 $DB->update_record('question', $question);
5058 $rs->close();
5060 $dbman->drop_field($table, $field);
5063 // Update question_answers.
5064 // In question_answers.feedback was previously always treated as
5065 // FORMAT_HTML in calculated, multianswer, multichoice, numerical,
5066 // shortanswer and truefalse; and
5067 // FORMAT_MOODLE in essay (despite being edited using the HTML editor)
5068 // So essay feedback needs to be converted to HTML unless $CFG->texteditors == 'textarea'.
5069 // For all question types except multichoice,
5070 // question_answers.answer is FORMAT_PLAIN and does not need to be changed.
5071 // For multichoice, question_answers.answer is FORMAT_MOODLE, and should
5072 // stay that way, at least for now.
5073 $rs = $DB->get_recordset_sql('
5074 SELECT qa.*, q.qtype
5075 FROM {question_answers} qa
5076 JOIN {question} q ON qa.question = q.id');
5077 foreach ($rs as $record) {
5078 // Convert question_answers.answer
5079 if ($record->qtype !== 'multichoice') {
5080 $record->answerformat = FORMAT_PLAIN;
5081 } else {
5082 $record->answerformat = FORMAT_MOODLE;
5085 // Convert question_answers.feedback
5086 if ($CFG->texteditors !== 'textarea') {
5087 if ($record->qtype == 'essay') {
5088 $record->feedback = text_to_html($record->feedback, false, false, true);
5090 $record->feedbackformat = FORMAT_HTML;
5091 } else {
5092 $record->feedbackformat = FORMAT_MOODLE;
5095 $DB->update_record('question_answers', $record);
5097 $rs->close();
5099 // In the question table, the code previously used questiontextformat
5100 // for both question text and general feedback. We need to copy the
5101 // values into the new column.
5102 // Then we need to convert FORMAT_MOODLE to FORMAT_HTML (depending on
5103 // $CFG->texteditors).
5104 $DB->execute('
5105 UPDATE {question}
5106 SET generalfeedbackformat = questiontextformat');
5107 // Also save the old questiontextformat, so that plugins that need it
5108 // can access it.
5109 $DB->execute('
5110 UPDATE {question}
5111 SET oldquestiontextformat = questiontextformat');
5112 // Now covert FORMAT_MOODLE content, if necssary.
5113 if ($CFG->texteditors !== 'textarea') {
5114 $rs = $DB->get_recordset('question', array('questiontextformat'=>FORMAT_MOODLE));
5115 foreach ($rs as $record) {
5116 $record->questiontext = text_to_html($record->questiontext, false, false, true);
5117 $record->questiontextformat = FORMAT_HTML;
5118 $record->generalfeedback = text_to_html($record->generalfeedback, false, false, true);
5119 $record->generalfeedbackformat = FORMAT_HTML;
5120 $DB->update_record('question', $record);
5122 $rs->close();
5125 // In the past, question_sessions.manualcommentformat was always treated
5126 // as FORMAT_HTML.
5127 $DB->set_field('question_sessions', 'manualcommentformat', FORMAT_HTML);
5129 // Main savepoint reached
5130 upgrade_main_savepoint(true, 2010080901);
5133 if ($oldversion < 2010082502) {
5134 // migrate file pool xx/xx/xx directory structure to xx/xx in older 2.0dev installs
5135 upgrade_simplify_overkill_pool_structure();
5136 upgrade_main_savepoint(true, 2010082502);
5139 if ($oldversion < 2010091303) {
5140 // drop all test tables from old xmldb test suite
5141 $table = new xmldb_table('testtable');
5142 if ($dbman->table_exists($table)) {
5143 $dbman->drop_table($table);
5145 $table = new xmldb_table('anothertest');
5146 if ($dbman->table_exists($table)) {
5147 $dbman->drop_table($table);
5149 $table = new xmldb_table('newnameforthetable');
5150 if ($dbman->table_exists($table)) {
5151 $dbman->drop_table($table);
5153 upgrade_main_savepoint(true, 2010091303);
5156 if ($oldversion < 2010091500) {
5158 // Changing precision of field token on table registration_hubs to (255)
5159 $table = new xmldb_table('registration_hubs');
5160 $field = new xmldb_field('token', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'id');
5162 // Launch change of precision for field token
5163 $dbman->change_field_precision($table, $field);
5165 // Main savepoint reached
5166 upgrade_main_savepoint(true, 2010091500);
5169 if ($oldversion < 2010091501) {
5170 // This index used to exist in Moodle 1.9 and was never dropped in the upgrade above.
5171 // Drop it now, or it breaks the following alter column.
5173 // Define index pagetypepattern (not unique) to be dropped form block_instances
5174 $table = new xmldb_table('block_instances');
5175 $index = new xmldb_index('pagetypepattern', XMLDB_INDEX_NOTUNIQUE, array('pagetypepattern'));
5177 // Conditionally launch drop index pagetypepattern
5178 if ($dbman->index_exists($table, $index)) {
5179 $dbman->drop_index($table, $index);
5182 // Main savepoint reached
5183 upgrade_main_savepoint(true, 2010091501);
5186 if ($oldversion < 2010091502) {
5187 // Need to drop the index before we can alter the column precision in the next step.
5189 // Define index parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern (not unique) to be dropped form block_instances
5190 $table = new xmldb_table('block_instances');
5191 $index = new xmldb_index('parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern', XMLDB_INDEX_NOTUNIQUE, array('parentcontextid', 'showinsubcontexts', 'pagetypepattern', 'subpagepattern'));
5193 // Conditionally launch drop index parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern
5194 if ($dbman->index_exists($table, $index)) {
5195 $dbman->drop_index($table, $index);
5198 // Main savepoint reached
5199 upgrade_main_savepoint(true, 2010091502);
5202 if ($oldversion < 2010091503) {
5204 // Changing precision of field pagetypepattern on table block_instances to (64)
5205 $table = new xmldb_table('block_instances');
5206 $field = new xmldb_field('pagetypepattern', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null, 'showinsubcontexts');
5208 // Launch change of precision for field pagetypepattern
5209 $dbman->change_field_precision($table, $field);
5211 // Main savepoint reached
5212 upgrade_main_savepoint(true, 2010091503);
5215 if ($oldversion < 2010091504) {
5216 // Now add the index back.
5218 // Define index parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern (not unique) to be added to block_instances
5219 $table = new xmldb_table('block_instances');
5220 $index = new xmldb_index('parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern', XMLDB_INDEX_NOTUNIQUE, array('parentcontextid', 'showinsubcontexts', 'pagetypepattern', 'subpagepattern'));
5222 // Conditionally launch add index parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern
5223 if (!$dbman->index_exists($table, $index)) {
5224 $dbman->add_index($table, $index);
5227 // Main savepoint reached
5228 upgrade_main_savepoint(true, 2010091504);
5231 if ($oldversion < 2010091505) {
5232 // drop all events queued from 1.9, unfortunately we can not process them because the serialisation of data changed
5233 // also the events format was changed....
5234 $DB->delete_records('events_queue_handlers', array());
5235 $DB->delete_records('events_queue', array());
5237 //reset all status fields too
5238 $DB->set_field('events_handlers', 'status', 0, array());
5240 upgrade_main_savepoint(true, 2010091505);
5243 if ($oldversion < 2010091506) {
5244 // change component string in events_handlers records to new "_" format
5245 if ($handlers = $DB->get_records('events_handlers')) {
5246 foreach ($handlers as $handler) {
5247 $handler->handlermodule = str_replace('/', '_', $handler->handlermodule);
5248 $DB->update_record('events_handlers', $handler);
5251 unset($handlers);
5252 upgrade_main_savepoint(true, 2010091506);
5255 if ($oldversion < 2010091507) {
5257 // Define index eventname-handlermodule (unique) to be dropped form events_handlers
5258 $table = new xmldb_table('events_handlers');
5259 $index = new xmldb_index('eventname-handlermodule', XMLDB_INDEX_UNIQUE, array('eventname', 'handlermodule'));
5261 // Conditionally launch drop index eventname-handlermodule
5262 if ($dbman->index_exists($table, $index)) {
5263 $dbman->drop_index($table, $index);
5266 // Main savepoint reached
5267 upgrade_main_savepoint(true, 2010091507);
5270 if ($oldversion < 2010091508) {
5272 // Rename field handlermodule on table events_handlers to component
5273 $table = new xmldb_table('events_handlers');
5274 $field = new xmldb_field('handlermodule', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null, 'eventname');
5276 // Launch rename field handlermodule
5277 $dbman->rename_field($table, $field, 'component');
5279 // Main savepoint reached
5280 upgrade_main_savepoint(true, 2010091508);
5283 if ($oldversion < 2010091509) {
5285 // Define index eventname-component (unique) to be added to events_handlers
5286 $table = new xmldb_table('events_handlers');
5287 $index = new xmldb_index('eventname-component', XMLDB_INDEX_UNIQUE, array('eventname', 'component'));
5289 // Conditionally launch add index eventname-component
5290 if (!$dbman->index_exists($table, $index)) {
5291 $dbman->add_index($table, $index);
5294 // Main savepoint reached
5295 upgrade_main_savepoint(true, 2010091509);
5298 if ($oldversion < 2010091510) {
5300 // Define field internal to be added to events_handlers
5301 $table = new xmldb_table('events_handlers');
5302 $field = new xmldb_field('internal', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'status');
5304 // Conditionally launch add field internal
5305 if (!$dbman->field_exists($table, $field)) {
5306 $dbman->add_field($table, $field);
5309 // Main savepoint reached
5310 upgrade_main_savepoint(true, 2010091510);
5313 if ($oldversion < 2010091700) {
5314 // Fix MNet sso_jump_url for Moodle application
5315 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php',
5316 array('name' => 'moodle', 'sso_jump_url' => '/auth/mnet/land.php'));
5317 upgrade_main_savepoint(true, 2010091700);
5320 if ($oldversion < 2010092000) {
5321 // drop multiple field again because it was still in install.xml in 2.0dev
5323 // Define field multiple to be dropped from block
5324 $table = new xmldb_table('block');
5325 $field = new xmldb_field('multiple');
5327 // Conditionally launch drop field multiple
5328 if ($dbman->field_exists($table, $field)) {
5329 $dbman->drop_field($table, $field);
5332 // Main savepoint reached
5333 upgrade_main_savepoint(true, 2010092000);
5336 if ($oldversion < 2010101300) {
5337 // Fix MDL-24641 : the registered language should not be empty otherwise cron will fail
5338 $registeredhubs = $DB->get_records('registration_hubs', array('confirmed' => 1));
5339 if (!empty($registeredhubs)) {
5340 foreach ($registeredhubs as $hub) {
5341 $cleanhuburl = clean_param($hub->huburl, PARAM_ALPHANUMEXT);
5342 $sitelanguage = get_config('hub', 'site_language_' . $cleanhuburl);
5343 if (empty($sitelanguage)) {
5344 set_config('site_language_' . $cleanhuburl, current_language(), 'hub');
5348 upgrade_main_savepoint(true, 2010101300);
5351 //MDL-24721 -add hidden column to grade_categories. This was done previously but it wasn't included in
5352 //install.xml so there are 2.0 sites that are missing it.
5353 if ($oldversion < 2010101900) {
5354 $table = new xmldb_table('grade_categories');
5355 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timemodified');
5357 if (!$dbman->field_exists($table, $field)) {
5358 $dbman->add_field($table, $field);
5361 upgrade_main_savepoint(true, 2010101900);
5364 // new format of the emoticons setting
5365 if ($oldversion < 2010102300) {
5366 unset($CFG->emoticons);
5367 $DB->delete_records('config', array('name' => 'emoticons'));
5368 $DB->delete_records('cache_text'); // changed md5 hash calculation
5369 upgrade_main_savepoint(true, 2010102300);
5372 //MDL-24771
5373 if ($oldversion < 2010102601) {
5375 $fieldnotification = new xmldb_field('notification', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, 0, 'smallmessage');
5376 $fieldcontexturl = new xmldb_field('contexturl', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'notification');
5377 $fieldcontexturlname = new xmldb_field('contexturlname', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'contexturl');
5378 $fieldstoadd = array($fieldnotification, $fieldcontexturl, $fieldcontexturlname);
5380 $tablestomodify = array(new xmldb_table('message'), new xmldb_table('message_read'));
5382 foreach($tablestomodify as $table) {
5383 foreach($fieldstoadd as $field) {
5384 if (!$dbman->field_exists($table, $field)) {
5385 $dbman->add_field($table, $field);
5390 upgrade_main_savepoint(true, 2010102601);
5393 // MDL-24694 needs increasing size of user_preferences.name(varchar[50]) field due to
5394 // long preferences names for messaging which need components parts within the name
5395 // eg: 'message_provider_mod_assignment_assignments_loggedin'
5396 if ($oldversion < 2010102602) {
5398 // Define index userid-name (unique) to be dropped form user_preferences
5399 $table = new xmldb_table('user_preferences');
5400 $index = new xmldb_index('userid-name', XMLDB_INDEX_UNIQUE, array('userid', 'name'));
5402 // Conditionally launch drop index userid-name
5403 if ($dbman->index_exists($table, $index)) {
5404 $dbman->drop_index($table, $index);
5407 // Changing precision of field name on table user_preferences to (255)
5408 $field = new xmldb_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'userid');
5410 // Launch change of precision for field name
5411 $dbman->change_field_precision($table, $field);
5413 // Conditionally launch add index userid-name
5414 if (!$dbman->index_exists($table, $index)) {
5415 $dbman->add_index($table, $index);
5418 // Main savepoint reached
5419 upgrade_main_savepoint(true, 2010102602);
5422 if ($oldversion < 2010102700) {
5424 $table = new xmldb_table('post');
5425 $field = new xmldb_field('uniquehash', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null, 'content');
5426 // Launch change of precision for field name
5427 $dbman->change_field_precision($table, $field);
5429 // Main savepoint reached
5430 upgrade_main_savepoint(true, 2010102700);
5433 if ($oldversion < 2010110200) {
5435 // fix tags itemtype for wiki
5436 $sql = "UPDATE {tag_instance}
5437 SET itemtype = 'wiki_pages'
5438 WHERE itemtype = 'wiki_page'";
5439 $DB->execute($sql);
5441 echo $OUTPUT->notification('Updating tags itemtype', 'notifysuccess');
5443 // Main savepoint reached
5444 upgrade_main_savepoint(true, 2010110200);
5447 //remove forum_logblocked from config. No longer required after user->emailstop was removed
5448 if ($oldversion < 2010110500) {
5449 unset_config('forum_logblocked');
5450 upgrade_main_savepoint(true, 2010110500);
5453 if ($oldversion < 2010110800) {
5454 // convert $CFG->disablecourseajax to $CFG->enablecourseajax
5455 $disabledcourseajax = get_config('disablecourseajax', 0);
5456 if ($disabledcourseajax) {
5457 set_config('enablecourseajax', 0);
5458 } else {
5459 set_config('enablecourseajax', 1);
5461 unset_config('disablecourseajax');
5463 upgrade_main_savepoint(true, 2010110800);
5466 if ($oldversion < 2010111000) {
5468 // Clean up the old scheduled backup settings that are no longer relevant
5469 update_fix_automated_backup_config();
5470 upgrade_main_savepoint(true, 2010111000);
5473 if ($oldversion < 2010111702) {
5475 // Clean up the old experimental split restore no loger used
5476 unset_config('experimentalsplitrestore');
5478 upgrade_main_savepoint(true, 2010111702);
5481 if ($oldversion < 2010121401) {
5483 // Define table profiling to be created
5484 $table = new xmldb_table('profiling');
5486 // Adding fields to table profiling
5487 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
5488 $table->add_field('runid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
5489 $table->add_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
5490 $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
5491 $table->add_field('totalexecutiontime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
5492 $table->add_field('totalcputime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
5493 $table->add_field('totalcalls', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
5494 $table->add_field('totalmemory', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
5495 $table->add_field('runreference', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
5496 $table->add_field('runcomment', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
5497 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
5499 // Adding keys to table profiling
5500 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
5501 $table->add_key('runid_uk', XMLDB_KEY_UNIQUE, array('runid'));
5503 // Adding indexes to table profiling
5504 $table->add_index('url_runreference_ix', XMLDB_INDEX_NOTUNIQUE, array('url', 'runreference'));
5505 $table->add_index('timecreated_runreference_ix', XMLDB_INDEX_NOTUNIQUE, array('timecreated', 'runreference'));
5507 // Conditionally launch create table for profiling
5508 if (!$dbman->table_exists($table)) {
5509 $dbman->create_table($table);
5512 // Main savepoint reached
5513 upgrade_main_savepoint(true, 2010121401);
5516 if ($oldversion < 2011011401) {
5517 $columns = $DB->get_columns('block_instances');
5519 // Check if we need to fix the default weight column
5520 if (array_key_exists('defaultweight', $columns) && $columns['defaultweight']->max_length != 10) {
5521 // Fix discrepancies in the block_instances table after upgrade from 1.9
5522 $table = new xmldb_table('block_instances');
5524 // defaultweight is smallint(3) after upgrade should be bigint 10
5525 // Also fixed in earlier upgrade code
5526 $field = new xmldb_field('defaultweight', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, 'defaultregion');
5527 if ($dbman->field_exists($table, $field)) {
5528 $dbman->change_field_type($table, $field);
5531 // add missing key `blocinst_par_ix` (`parentcontextid`)
5532 $index = new xmldb_index('parentcontextid', XMLDB_INDEX_NOTUNIQUE, array('parentcontextid'));
5533 if (!$dbman->index_exists($table, $index)) {
5534 $dbman->add_index($table, $index);
5538 // Main savepoint reached
5539 upgrade_main_savepoint(true, 2011011401);
5542 if ($oldversion < 2011011402) {
5543 // Fix discrepancies in the block_positions table after upgrade from 1.9
5544 $table = new xmldb_table('block_positions');
5545 $columns = $DB->get_columns('block_positions');
5547 // Check if we need to fix the blockinstanceid field
5548 if (array_key_exists('blockinstanceid', $columns) && empty($columns['blockinstanceid']->unsigned)) {
5549 // Fix blockinstanceid
5550 // First remove the indexs on the field
5551 $indexone = new xmldb_index('blockinstanceid', XMLDB_INDEX_NOTUNIQUE, array('blockinstanceid'));
5552 $indexall = new xmldb_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid','contextid','pagetype','subpage'));
5553 if ($dbman->index_exists($table, $indexone)) {
5554 $dbman->drop_index($table, $indexone);
5556 if ($dbman->index_exists($table, $indexall)) {
5557 $dbman->drop_index($table, $indexall);
5559 // blockinstanceid should be unsigned
5560 // Also fixed in earlier upgrade code
5561 $field = new xmldb_field('blockinstanceid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'id');
5562 if ($dbman->field_exists($table, $field)) {
5563 $dbman->change_field_unsigned($table, $field);
5566 // Add the indexs back in
5567 $dbman->add_index($table, $indexone);
5568 $dbman->add_index($table, $indexall);
5571 // Check if the visible field needs fixing.
5572 if (array_key_exists('visible', $columns) && !empty($columns['visible']->has_default)) {
5573 // visible shouldn't have a default
5574 // Also fixed in earlier upgrade code
5575 $field = new xmldb_field('visible', XMLDB_TYPE_INTEGER, 4, null, XMLDB_NOTNULL, null, null, 'subpage');
5576 if ($dbman->field_exists($table, $field)) {
5577 $dbman->change_field_default($table, $field);
5581 // Main savepoint reached
5582 upgrade_main_savepoint(true, 2011011402);
5585 if ($oldversion < 2011011403) {
5586 $columns = $DB->get_columns('grade_categories');
5587 // Check if we need to fix the hidden field
5588 if (array_key_exists('hidden', $columns) && $columns['hidden']->max_length != 1) {
5589 // Fix discrepancies in the grade_categories table after upgrade from 1.9
5590 $table = new xmldb_table('grade_categories');
5592 // hidden should be tinyint(1)
5593 // Also fixed in earlier upgrade code
5594 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timemodified');
5595 if ($dbman->field_exists($table, $field)) {
5596 $dbman->change_field_precision($table, $field);
5600 // Main savepoint reached
5601 upgrade_main_savepoint(true, 2011011403);
5604 if ($oldversion < 2011011404) {
5605 // Fix discrepancies in the message table after upgrade from 1.9
5606 $columns = $DB->get_columns('message');
5607 $table = new xmldb_table('message');
5609 // Check if we need to fix the useridfrom field
5610 if (array_key_exists('useridfrom', $columns) && empty($columns['useridfrom']->unsigned)) {
5611 // useridfrom should be unsigned
5612 $field = new xmldb_field('useridfrom', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'id');
5613 $index = new xmldb_index('useridfrom', XMLDB_INDEX_NOTUNIQUE, array('useridfrom'));
5614 if ($dbman->index_exists($table, $index)) {
5615 $dbman->drop_index($table, $index);
5617 if ($dbman->field_exists($table, $field)) {
5618 $dbman->change_field_unsigned($table, $field);
5620 $dbman->add_index($table, $index);
5623 // Check if we need to fix the useridto field
5624 if (array_key_exists('useridto', $columns) && empty($columns['useridto']->unsigned)) {
5625 // useridto should be unsigned
5626 $field = new xmldb_field('useridto', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'useridfrom');
5627 $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
5628 if ($dbman->index_exists($table, $index)) {
5629 $dbman->drop_index($table, $index);
5631 if ($dbman->field_exists($table, $field)) {
5632 $dbman->change_field_unsigned($table, $field);
5634 $dbman->add_index($table, $index);
5637 // Check if we need to fix the notification field
5638 if (array_key_exists('notification', $columns) && !empty($columns['notification']->not_null)) {
5639 // notification should allow null
5640 // Fixed in earlier upgrade code
5641 $field = new xmldb_field('notification', XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, null, null, 0, 'smallmessage');
5642 if ($dbman->field_exists($table, $field)) {
5643 $dbman->change_field_notnull($table, $field);
5647 // Check if we need to fix the contexturl field
5648 if (array_key_exists('contexturl', $columns) && strpos($columns['contexturl']->type, 'text') === false) {
5649 // contexturl should be text
5650 // Fixed in earlier upgrade code
5651 $field = new xmldb_field('contexturl', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'notification');
5652 if ($dbman->field_exists($table, $field)) {
5653 $dbman->change_field_type($table, $field);
5657 // Check if we need to fix the contexturl field
5658 if (array_key_exists('contexturlname', $columns) && strpos($columns['contexturlname']->type, 'text') === false) {
5659 // contexturlname should be text
5660 // Fixed in earlier upgrade code
5661 $field = new xmldb_field('contexturlname', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'contexturl');
5662 if ($dbman->field_exists($table, $field)) {
5663 $dbman->change_field_type($table, $field);
5667 // Main savepoint reached
5668 upgrade_main_savepoint(true, 2011011404);
5671 if ($oldversion < 2011011405) {
5672 // Fix discrepancies in the message_read table after upgrade from 1.9
5673 $columns = $DB->get_columns('message_read');
5674 $table = new xmldb_table('message_read');
5676 // Check if we need to fix the useridfrom field
5677 if (array_key_exists('useridfrom', $columns) && empty($columns['useridfrom']->unsigned)) {
5678 // useridfrom should be unsigned
5679 $field = new xmldb_field('useridfrom', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'id');
5680 $index = new xmldb_index('useridfrom', XMLDB_INDEX_NOTUNIQUE, array('useridfrom'));
5681 if ($dbman->index_exists($table, $index)) {
5682 $dbman->drop_index($table, $index);
5684 if ($dbman->field_exists($table, $field)) {
5685 $dbman->change_field_unsigned($table, $field);
5687 $dbman->add_index($table, $index);
5690 // Check if we need to fix the useridto field
5691 if (array_key_exists('useridto', $columns) && empty($columns['useridto']->unsigned)) {
5692 // useridto should be unsigned
5693 $field = new xmldb_field('useridto', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'useridfrom');
5694 $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
5695 if ($dbman->index_exists($table, $index)) {
5696 $dbman->drop_index($table, $index);
5698 if ($dbman->field_exists($table, $field)) {
5699 $dbman->change_field_unsigned($table, $field);
5701 $dbman->add_index($table, $index);
5704 // Check if we need to fix the notification field
5705 if (array_key_exists('notification', $columns) && !empty($columns['notification']->not_null)) {
5706 // notification should allow null
5707 // Fixed in earlier upgrade code
5708 $field = new xmldb_field('notification', XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, null, null, 0, 'smallmessage');
5709 if ($dbman->field_exists($table, $field)) {
5710 $dbman->change_field_notnull($table, $field);
5714 // Check if we need to fix the contexturl field
5715 if (array_key_exists('contexturl', $columns) && strpos($columns['contexturl']->type, 'text') === false) {
5716 // contexturl should be text
5717 // Fixed in earlier upgrade code
5718 $field = new xmldb_field('contexturl', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'notification');
5719 if ($dbman->field_exists($table, $field)) {
5720 $dbman->change_field_type($table, $field);
5724 // Check if we need to fix the contexturl field
5725 if (array_key_exists('contexturlname', $columns) && strpos($columns['contexturlname']->type, 'text') === false) {
5726 // contexturlname should be text
5727 // Fixed in earlier upgrade code
5728 $field = new xmldb_field('contexturlname', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'contexturl');
5729 if ($dbman->field_exists($table, $field)) {
5730 $dbman->change_field_type($table, $field);
5734 // Main savepoint reached
5735 upgrade_main_savepoint(true, 2011011405);
5738 if ($oldversion < 2011011406) {
5739 // Fix discrepancies in the my_pages table after upgrade from 1.9
5740 $columns = $DB->get_columns('my_pages');
5741 $table = new xmldb_table('my_pages');
5743 // Check if we need to fix the private column
5744 if (array_key_exists('private', $columns) && $columns['private']->default_value != '1') {
5745 // private should be default 1
5746 // Fixed in earlier upgrade code
5747 $field = new xmldb_field('private', XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 1, 'name');
5748 $index = new xmldb_index('user_idx', XMLDB_INDEX_NOTUNIQUE, array('userid','private'));
5749 if ($dbman->index_exists($table, $index)) {
5750 $dbman->drop_index($table, $index);
5752 if ($dbman->field_exists($table, $field)) {
5753 $dbman->change_field_default($table, $field);
5755 $dbman->add_index($table, $index);
5758 // Check if we need to fix the sortorder field
5759 if (array_key_exists('sortorder', $columns) && !empty($columns['sortorder']->unsigned)) {
5760 // Sortorder should not be unsigned
5761 // Fixed in earlier upgrade code
5762 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, 6, null, XMLDB_NOTNULL, null, 0, 'private');
5763 if ($dbman->field_exists($table, $field)) {
5764 $dbman->change_field_notnull($table, $field);
5768 upgrade_main_savepoint(true, 2011011406);
5771 if ($oldversion < 2011011407) {
5772 // Check if we need to fix post.uniquehash
5773 $columns = $DB->get_columns('my_pages');
5774 if (array_key_exists('uniquehash', $columns) && $columns['uniquehash']->max_length != 128) {
5775 // Fix discrepancies in the post table after upgrade from 1.9
5776 $table = new xmldb_table('post');
5778 // Uniquehash should be 128 chars
5779 // Fixed in earlier upgrade code
5780 $field = new xmldb_field('uniquehash', XMLDB_TYPE_CHAR, 128, null, XMLDB_NOTNULL, null, null, 'content');
5781 if ($dbman->field_exists($table, $field)) {
5782 $dbman->change_field_precision($table, $field);
5786 upgrade_main_savepoint(true, 2011011407);
5789 if ($oldversion < 2011011408) {
5790 // Fix question in the post table after upgrade from 1.9
5791 $columns = $DB->get_columns('question');
5792 $table = new xmldb_table('question');
5794 // Check if we need to fix default grade
5795 if (array_key_exists('defaultgrade', $columns) && (
5796 empty($columns['defaultgrade']->unsigned) ||
5797 empty($columns['defaultgrade']->not_null) ||
5798 $columns['defaultgrade']->default_value !== '1.0000000')) {
5799 // defaultgrade should be unsigned NOT NULL DEFAULT '1.0000000'
5800 // Fixed in earlier upgrade code
5801 $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1.0000000', 'generalfeedbackformat');
5802 if ($dbman->field_exists($table, $field)) {
5803 $dbman->change_field_default($table, $field);
5807 // Check if we need to fix penalty
5808 if (array_key_exists('penalty', $columns) && (empty($columns['penalty']->not_null) || $columns['penalty']->default_value !== '0.1000000')) {
5809 // penalty should be NOT NULL DEFAULT '0.1000000'
5810 // Fixed in earlier upgrade code
5811 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0.1000000', 'defaultgrade');
5812 if ($dbman->field_exists($table, $field)) {
5813 $dbman->change_field_default($table, $field);
5817 upgrade_main_savepoint(true, 2011011408);
5820 if ($oldversion < 2011011409) {
5821 // Fix question_answers in the post table after upgrade from 1.9
5822 $columns = $DB->get_columns('question_answers');
5823 $table = new xmldb_table('question_answers');
5825 if (array_key_exists('fraction', $columns) && empty($columns['fraction']->not_null)) {
5826 // fraction should be NOT NULL DEFAULT '0.0000000',
5827 // Fixed in earlier upgrade code
5828 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'feedback');
5829 if ($dbman->field_exists($table, $field)) {
5830 $dbman->change_field_default($table, $field);
5834 upgrade_main_savepoint(true, 2011011409);
5837 if ($oldversion < 2011011410) {
5838 // Fix question_sessions in the post table after upgrade from 1.9
5839 $columns = $DB->get_columns('question_sessions');
5840 $table = new xmldb_table('question_sessions');
5842 // Check if we need to fix sumpenalty
5843 if (array_key_exists('sumpenalty', $columns) && empty($columns['sumpenalty']->not_null)) {
5844 // sumpenalty should be NOT NULL DEFAULT '0.0000000',
5845 // Fixed in earlier upgrade code
5846 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'newgraded');
5847 if ($dbman->field_exists($table, $field)) {
5848 $dbman->change_field_default($table, $field);
5852 upgrade_main_savepoint(true, 2011011410);
5855 if ($oldversion < 2011011411) {
5856 // Fix question_states in the post table after upgrade from 1.9
5857 $columns = $DB->get_columns('question_states');
5858 $table = new xmldb_table('question_states');
5860 // Check if we need to fix grade
5861 if (array_key_exists('grade', $columns) && empty($columns['grade']->not_null)) {
5862 // grade should be NOT NULL DEFAULT '0.0000000',
5863 // Fixed in earlier upgrade code
5864 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'event');
5865 if ($dbman->field_exists($table, $field)) {
5866 $dbman->change_field_default($table, $field);
5870 // Check if we need to fix raw_grade
5871 if (array_key_exists('raw_grade', $columns) && empty($columns['raw_grade']->not_null)) {
5872 // raw_grade should be NOT NULL DEFAULT '0.0000000',
5873 // Fixed in earlier upgrade code
5874 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'grade');
5875 if ($dbman->field_exists($table, $field)) {
5876 $dbman->change_field_default($table, $field);
5880 // Check if we need to fix raw_grade
5881 if (array_key_exists('penalty', $columns) && empty($columns['penalty']->not_null)) {
5882 // penalty should be NOT NULL DEFAULT '0.0000000',
5883 // Fixed in earlier upgrade code
5884 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'raw_grade');
5885 if ($dbman->field_exists($table, $field)) {
5886 $dbman->change_field_default($table, $field);
5890 upgrade_main_savepoint(true, 2011011411);
5893 if ($oldversion < 2011011412) {
5894 // Fix tag_instance in the post table after upgrade from 1.9
5895 $columns = $DB->get_columns('tag_instance');
5896 $table = new xmldb_table('tag_instance');
5898 // Check if we need to fix tiuserid
5899 if (array_key_exists('tiuserid', $columns) && !empty($columns['tiuserid']->has_default)) {
5900 // tiuserid should have no default
5901 // Fixed in earlier upgrade code
5902 $field = new xmldb_field('tiuserid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
5903 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid', XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
5904 if ($dbman->index_exists($table, $index)) {
5905 $dbman->drop_index($table, $index);
5907 if ($dbman->field_exists($table, $field)) {
5908 $dbman->change_field_default($table, $field);
5910 $dbman->add_index($table, $index);
5913 upgrade_main_savepoint(true, 2011011412);
5916 if ($oldversion < 2011011413) {
5917 // Fix user_info_field in the post table after upgrade from 1.9
5918 $table = new xmldb_table('user_info_field');
5920 // Missing field descriptionformat
5921 // Fixed in earlier upgrade code
5922 $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, 2, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'description');
5923 if (!$dbman->field_exists($table, $field)) {
5924 $dbman->add_field($table, $field);
5927 upgrade_main_savepoint(true, 2011011413);
5930 if ($oldversion < 2011011414) {
5931 // Drop the adodb_logsql table if it exists... it was never actually used anyway.
5932 $table = new xmldb_table('adodb_logsql');
5934 if ($dbman->table_exists($table)) {
5935 $dbman->drop_table($table);
5938 upgrade_main_savepoint(true, 2011011414);
5941 if ($oldversion < 2011011415) {
5942 //create the rating table indexes if required
5943 $table = new xmldb_table('rating');
5945 $index = new xmldb_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
5946 if (!$dbman->index_exists($table, $index)) {
5947 $dbman->add_index($table, $index);
5949 $key = new xmldb_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
5950 $dbman->add_key($table, $key);
5952 $key = new xmldb_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
5953 $dbman->add_key($table, $key);
5956 upgrade_main_savepoint(true, 2011011415);
5959 if ($oldversion < 2011012400) {
5960 // Clean up the old progress tracked roles setting, no longer used (replaced by enrolment)
5961 unset_config('progresstrackedroles');
5962 upgrade_main_savepoint(true, 2011012400);
5965 if ($oldversion < 2011012500) {
5966 $columns = $DB->get_columns('tag_instance');
5967 $table = new xmldb_table('tag_instance');
5969 // Drop and recreate index if tiuserid doesn't have default value
5970 if (array_key_exists('tiuserid', $columns) && empty($columns['tiuserid']->has_default)) {
5971 // Define index itemtype-itemid-tagid-tiuserid (unique) to be dropped form tag_instance
5972 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid', XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
5973 // Conditionally launch drop index itemtype-itemid-tagid-tiuserid
5974 if ($dbman->index_exists($table, $index)) {
5975 $dbman->drop_index($table, $index);
5978 // Changing the default of field tiuserid on table tag_instance to 0
5979 $field = new xmldb_field('tiuserid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'itemid');
5981 // Launch change of default for field tiuserid
5982 $dbman->change_field_default($table, $field);
5984 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid', XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
5986 // Conditionally launch add index itemtype-itemid-tagid-tiuserid
5987 if (!$dbman->index_exists($table, $index)) {
5988 $dbman->add_index($table, $index);
5992 // Main savepoint reached
5993 upgrade_main_savepoint(true, 2011012500);
5996 if ($oldversion < 2011012501) {
5997 //add the index userfieldidx (not unique) to user_info_data
5998 $table = new xmldb_table('user_info_data');
5999 $index = new xmldb_index('userfieldidx', XMLDB_INDEX_NOTUNIQUE, array('userid', 'fieldid'));
6001 if (!$dbman->index_exists($table, $index)) {
6002 $dbman->add_index($table, $index);
6005 upgrade_main_savepoint(true, 2011012501);
6008 return true;
6011 //TODO: Cleanup before the 2.0 release - we do not want to drag along these dev machine fixes forever
6012 // 1/ drop block_pinned_old table here and in install.xml
6013 // 2/ drop block_instance_old table here and in install.xml
6015 //TODO: AFTER 2.0 remove the column user->emailstop and the user preference "message_showmessagewindow"