3 // This file keeps track of upgrades to Moodle.
5 // Sometimes, changes between versions involve
6 // alterations to database structures and other
7 // major things that may break installations.
9 // The upgrade function in this file will attempt
10 // to perform all the necessary actions to upgrade
11 // your older installtion to the current version.
13 // If there's something it cannot do itself, it
14 // will tell you what you need to do.
16 // The commands in here will all be database-neutral,
17 // using the methods of database_manager class
19 // Please do not forget to use upgrade_set_timeout()
20 // before any action that may take longer time to finish.
24 * @global stdClass $CFG
25 * @global stdClass $USER
26 * @global moodle_database $DB
27 * @global core_renderer $OUTPUT
28 * @param int $oldversion
31 function xmldb_main_upgrade($oldversion) {
32 global $CFG, $USER, $DB, $OUTPUT;
34 require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
38 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
40 ////////////////////////////////////////
41 ///upgrade supported only from 1.9.x ///
42 ////////////////////////////////////////
44 if ($result && $oldversion < 2008030600) {
45 //NOTE: this table was added much later, that is why this step is repeated later in this file
47 /// Define table upgrade_log to be created
48 $table = new xmldb_table('upgrade_log');
50 /// Adding fields to table upgrade_log
51 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
52 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
53 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
54 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
55 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
56 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
57 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
58 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
59 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
61 /// Adding keys to table upgrade_log
62 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
63 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
65 /// Adding indexes to table upgrade_log
66 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
67 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
69 /// Create table for upgrade_log
70 $dbman->create_table($table);
72 /// Main savepoint reached
73 upgrade_main_savepoint($result, 2008030600);
76 if ($result && $oldversion < 2008030601) {
77 @unlink($CFG->dataroot.'/cache/languages');
79 // rename old lang directory so that the new and old langs do not mix
80 if (rename("$CFG->dataroot/lang", "$CFG->dataroot/oldlang")) {
81 $oldlang = "$CFG->dataroot/oldlang";
83 $oldlang = "$CFG->dataroot/lang";
85 // TODO: fetch previously installed languages ("*_utf8") found in $oldlang from moodle.org
86 upgrade_set_timeout(60*20); // this may take a while
89 // TODO: add some info file to $oldlang describing what to do with "$oldlang/*_utf8_local" dirs
92 // Main savepoint reached
93 upgrade_main_savepoint($result, 2008030601);
96 if ($result && $oldversion < 2008030700) {
97 upgrade_set_timeout(60*20); // this may take a while
99 /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
100 $table = new xmldb_table('grade_letters');
101 $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
103 /// Launch drop index contextid-lowerboundary
104 $dbman->drop_index($table, $index);
106 /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
107 $table = new xmldb_table('grade_letters');
108 $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
110 /// Launch add index contextid-lowerboundary-letter
111 $dbman->add_index($table, $index);
113 /// Main savepoint reached
114 upgrade_main_savepoint($result, 2008030700);
117 if ($result && $oldversion < 2008050100) {
118 // Update courses that used weekscss to weeks
119 $result = $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
120 upgrade_main_savepoint($result, 2008050100);
123 if ($result && $oldversion < 2008050200) {
124 // remove unused config options
125 unset_config('statsrolesupgraded');
126 upgrade_main_savepoint($result, 2008050200);
129 if ($result && $oldversion < 2008050700) {
130 upgrade_set_timeout(60*20); // this may take a while
132 /// Fix minor problem caused by MDL-5482.
133 require_once($CFG->dirroot . '/question/upgrade.php');
134 $result = $result && question_fix_random_question_parents();
135 upgrade_main_savepoint($result, 2008050700);
138 if ($result && $oldversion < 2008051201) {
139 echo $OUTPUT->notification('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
140 upgrade_set_timeout(60*20); // this may take a while
142 /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
143 $dbfamily = $DB->get_dbfamily();
144 if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
145 $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
148 /// Define index idnumber (not unique) to be dropped form user
149 $table = new xmldb_table('user');
150 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
152 /// Launch drop index idnumber
153 if ($dbman->index_exists($table, $index)) {
154 $dbman->drop_index($table, $index);
157 /// Changing precision of field idnumber on table user to (255)
158 $table = new xmldb_table('user');
159 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
161 /// Launch change of precision for field idnumber
162 $dbman->change_field_precision($table, $field);
164 /// Launch add index idnumber again
165 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
166 $dbman->add_index($table, $index);
168 /// Main savepoint reached
169 upgrade_main_savepoint($result, 2008051201);
172 if ($result && $oldversion < 2008051202) {
173 $log_action = new object();
174 $log_action->module = 'course';
175 $log_action->action = 'unenrol';
176 $log_action->mtable = 'course';
177 $log_action->field = 'fullname';
178 if (!$DB->record_exists('log_display', array('action'=>'unenrol', 'module'=>'course'))) {
179 $result = $result && $DB->insert_record('log_display', $log_action);
181 upgrade_main_savepoint($result, 2008051202);
184 if ($result && $oldversion < 2008051203) {
185 $table = new xmldb_table('mnet_enrol_course');
186 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
187 $dbman->change_field_precision($table, $field);
188 upgrade_main_savepoint($result, 2008051203);
191 if ($result && $oldversion < 2008063001) {
192 upgrade_set_timeout(60*20); // this may take a while
194 // table to be modified
195 $table = new xmldb_table('tag_instance');
197 $field = new xmldb_field('tiuserid');
198 if (!$dbman->field_exists($table, $field)) {
199 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
200 $dbman->add_field($table, $field);
203 $index = new xmldb_index('itemtype-itemid-tagid');
204 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
205 if ($dbman->index_exists($table, $index)) {
206 $dbman->drop_index($table, $index);
208 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
209 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
210 if (!$dbman->index_exists($table, $index)) {
211 $dbman->add_index($table, $index);
214 /// Main savepoint reached
215 upgrade_main_savepoint($result, 2008063001);
218 if ($result && $oldversion < 2008070300) {
219 $result = $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
220 upgrade_main_savepoint($result, 2008070300);
223 if ($result && $oldversion < 2008070701) {
225 /// Define table portfolio_instance to be created
226 $table = new xmldb_table('portfolio_instance');
228 /// Adding fields to table portfolio_instance
229 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
230 $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
231 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
232 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
234 /// Adding keys to table portfolio_instance
235 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
237 /// Conditionally launch create table for portfolio_instance
238 if (!$dbman->table_exists($table)) {
239 $dbman->create_table($table);
241 /// Define table portfolio_instance_config to be created
242 $table = new xmldb_table('portfolio_instance_config');
244 /// Adding fields to table portfolio_instance_config
245 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
246 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
247 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
248 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
250 /// Adding keys to table portfolio_instance_config
251 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
252 $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
254 /// Adding indexes to table portfolio_instance_config
255 $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
257 /// Conditionally launch create table for portfolio_instance_config
258 if (!$dbman->table_exists($table)) {
259 $dbman->create_table($table);
262 /// Define table portfolio_instance_user to be created
263 $table = new xmldb_table('portfolio_instance_user');
265 /// Adding fields to table portfolio_instance_user
266 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
267 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
268 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
269 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
270 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
272 /// Adding keys to table portfolio_instance_user
273 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
274 $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
275 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
277 /// Conditionally launch create table for portfolio_instance_user
278 if (!$dbman->table_exists($table)) {
279 $dbman->create_table($table);
282 /// Main savepoint reached
283 upgrade_main_savepoint($result, 2008070701);
286 if ($result && $oldversion < 2008072400) {
287 /// Create the database tables for message_processors
288 $table = new xmldb_table('message_processors');
289 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
290 $table->add_field('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null);
291 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
292 $dbman->create_table($table);
294 /// delete old and create new fields
295 $table = new xmldb_table('message');
296 $field = new xmldb_field('messagetype');
297 $dbman->drop_field($table, $field);
300 $field = new xmldb_field('message');
301 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
302 $dbman->rename_field($table, $field, 'fullmessage');
303 $field = new xmldb_field('format');
304 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
305 $dbman->rename_field($table, $field, 'fullmessageformat');
307 /// new message fields
308 $field = new xmldb_field('subject');
309 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
310 $dbman->add_field($table, $field);
311 $field = new xmldb_field('fullmessagehtml');
312 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
313 $dbman->add_field($table, $field);
314 $field = new xmldb_field('smallmessage');
315 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
316 $dbman->add_field($table, $field);
319 $table = new xmldb_table('message_read');
320 $field = new xmldb_field('messagetype');
321 $dbman->drop_field($table, $field);
322 $field = new xmldb_field('mailed');
323 $dbman->drop_field($table, $field);
326 $field = new xmldb_field('message');
327 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
328 $dbman->rename_field($table, $field, 'fullmessage');
329 $field = new xmldb_field('format');
330 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
331 $dbman->rename_field($table, $field, 'fullmessageformat');
334 /// new message fields
335 $field = new xmldb_field('subject');
336 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
337 $dbman->add_field($table, $field);
338 $field = new xmldb_field('fullmessagehtml');
339 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
340 $dbman->add_field($table, $field);
341 $field = new xmldb_field('smallmessage');
342 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
343 $dbman->add_field($table, $field);
346 $table = new xmldb_table('message_working');
347 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
348 $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
349 $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
350 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
351 $dbman->create_table($table);
354 upgrade_main_savepoint($result, 2008072400);
357 if ($result && $oldversion < 2008072800) {
359 /// Define field enablecompletion to be added to course
360 $table = new xmldb_table('course');
361 $field = new xmldb_field('enablecompletion');
362 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
364 /// Launch add field enablecompletion
365 if (!$dbman->field_exists($table,$field)) {
366 $dbman->add_field($table, $field);
369 /// Define field completion to be added to course_modules
370 $table = new xmldb_table('course_modules');
371 $field = new xmldb_field('completion');
372 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'groupmembersonly');
374 /// Launch add field completion
375 if (!$dbman->field_exists($table,$field)) {
376 $dbman->add_field($table, $field);
379 /// Define field completiongradeitemnumber to be added to course_modules
380 $field = new xmldb_field('completiongradeitemnumber');
381 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'completion');
383 /// Launch add field completiongradeitemnumber
384 if (!$dbman->field_exists($table,$field)) {
385 $dbman->add_field($table, $field);
388 /// Define field completionview to be added to course_modules
389 $field = new xmldb_field('completionview');
390 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiongradeitemnumber');
392 /// Launch add field completionview
393 if (!$dbman->field_exists($table,$field)) {
394 $dbman->add_field($table, $field);
397 /// Define field completionexpected to be added to course_modules
398 $field = new xmldb_field('completionexpected');
399 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionview');
401 /// Launch add field completionexpected
402 if (!$dbman->field_exists($table,$field)) {
403 $dbman->add_field($table, $field);
406 /// Define table course_modules_completion to be created
407 $table = new xmldb_table('course_modules_completion');
408 if (!$dbman->table_exists($table)) {
410 /// Adding fields to table course_modules_completion
411 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
412 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
413 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
414 $table->add_field('completionstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
415 $table->add_field('viewed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
416 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
418 /// Adding keys to table course_modules_completion
419 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
421 /// Adding indexes to table course_modules_completion
422 $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
423 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
425 /// Launch create table for course_modules_completion
426 $dbman->create_table($table);
429 /// Main savepoint reached
430 upgrade_main_savepoint($result, 2008072800);
433 if ($result && $oldversion < 2008073000) {
435 /// Define table portfolio_log to be created
436 $table = new xmldb_table('portfolio_log');
438 /// Adding fields to table portfolio_log
439 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
440 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
441 $table->add_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
442 $table->add_field('portfolio', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
443 $table->add_field('caller_class', XMLDB_TYPE_CHAR, '150', null, XMLDB_NOTNULL, null, null);
444 $table->add_field('caller_file', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
445 $table->add_field('caller_sha1', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
447 /// Adding keys to table portfolio_log
448 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
449 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
450 $table->add_key('portfoliofk', XMLDB_KEY_FOREIGN, array('portfolio'), 'portfolio_instance', array('id'));
452 /// Conditionally launch create table for portfolio_log
453 if (!$dbman->table_exists($table)) {
454 $dbman->create_table($table);
457 /// Main savepoint reached
458 upgrade_main_savepoint($result, 2008073000);
461 if ($result && $oldversion < 2008073104) {
462 /// Drop old table that might exist for some people
463 $table = new xmldb_table('message_providers');
464 if ($dbman->table_exists($table)) {
465 $dbman->drop_table($table);
468 /// Define table message_providers to be created
469 $table = new xmldb_table('message_providers');
471 /// Adding fields to table message_providers
472 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
473 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
474 $table->add_field('component', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
475 $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
477 /// Adding keys to table message_providers
478 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
480 /// Adding indexes to table message_providers
481 $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
483 /// Create table for message_providers
484 $dbman->create_table($table);
486 upgrade_main_savepoint($result, 2008073104);
489 if ($result && $oldversion < 2008073111) {
490 /// Define table files to be created
491 $table = new xmldb_table('files');
493 /// Adding fields to table files
494 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
495 $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
496 $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
497 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
498 $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
499 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
500 $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
501 $table->add_field('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
502 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
503 $table->add_field('filesize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
504 $table->add_field('mimetype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
505 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
506 $table->add_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
507 $table->add_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null);
508 $table->add_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null);
509 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
510 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
512 /// Adding keys to table files
513 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
514 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
515 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
517 /// Adding indexes to table files
518 $table->add_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid'));
519 $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
520 $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
522 /// Conditionally launch create table for files
523 $dbman->create_table($table);
525 /// Main savepoint reached
526 upgrade_main_savepoint($result, 2008073111);
529 if ($result && $oldversion < 2008073113) {
530 /// move all course, backup and other files to new filepool based storage
531 upgrade_migrate_files_courses();
532 /// Main savepoint reached
533 upgrade_main_savepoint($result, 2008073113);
536 if ($result && $oldversion < 2008073114) {
537 /// move all course, backup and other files to new filepool based storage
538 upgrade_migrate_files_blog();
539 /// Main savepoint reached
540 upgrade_main_savepoint($result, 2008073114);
543 if ($result && $oldversion < 2008080400) {
544 // Add field ssl_jump_url to mnet application, and populate existing default applications
545 $table = new xmldb_table('mnet_application');
546 $field = new xmldb_field('sso_jump_url');
547 if (!$dbman->field_exists($table, $field)) {
548 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
549 $dbman->add_field($table, $field);
550 $result = $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
551 $result = $result && $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
554 /// Main savepoint reached
555 upgrade_main_savepoint($result, 2008080400);
558 if ($result && $oldversion < 2008080500) {
560 /// Define table portfolio_tempdata to be created
561 $table = new xmldb_table('portfolio_tempdata');
563 /// Adding fields to table portfolio_tempdata
564 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
565 $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
567 /// Adding keys to table portfolio_tempdata
568 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
570 /// Conditionally launch create table for portfolio_tempdata
571 if (!$dbman->table_exists($table)) {
572 $dbman->create_table($table);
575 /// Main savepoint reached
576 upgrade_main_savepoint($result, 2008080500);
579 if ($result && $oldversion < 2008080600) {
581 $DB->delete_records('portfolio_tempdata'); // there shouldnt' be any, and it will cause problems with this upgrade.
582 /// Define field expirytime to be added to portfolio_tempdata
583 $table = new xmldb_table('portfolio_tempdata');
584 $field = new xmldb_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'data');
586 /// Conditionally launch add field expirytime
587 if (!$dbman->field_exists($table, $field)) {
588 $dbman->add_field($table, $field);
591 /// Main savepoint reached
592 upgrade_main_savepoint($result, 2008080600);
595 if ($result && $oldversion < 2008081500) {
596 /// Changing the type of all the columns that the question bank uses to store grades to be NUMBER(12, 7).
597 $table = new xmldb_table('question');
598 $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'generalfeedback');
599 $dbman->change_field_type($table, $field);
600 upgrade_main_savepoint($result, 2008081500);
603 if ($result && $oldversion < 2008081501) {
604 $table = new xmldb_table('question');
605 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'defaultgrade');
606 $dbman->change_field_type($table, $field);
607 upgrade_main_savepoint($result, 2008081501);
610 if ($result && $oldversion < 2008081502) {
611 $table = new xmldb_table('question_answers');
612 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'answer');
613 $dbman->change_field_type($table, $field);
614 upgrade_main_savepoint($result, 2008081502);
617 if ($result && $oldversion < 2008081503) {
618 $table = new xmldb_table('question_sessions');
619 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'newgraded');
620 $dbman->change_field_type($table, $field);
621 upgrade_main_savepoint($result, 2008081503);
624 if ($result && $oldversion < 2008081504) {
625 $table = new xmldb_table('question_states');
626 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'event');
627 $dbman->change_field_type($table, $field);
628 upgrade_main_savepoint($result, 2008081504);
631 if ($result && $oldversion < 2008081505) {
632 $table = new xmldb_table('question_states');
633 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'grade');
634 $dbman->change_field_type($table, $field);
635 upgrade_main_savepoint($result, 2008081505);
638 if ($result && $oldversion < 2008081506) {
639 $table = new xmldb_table('question_states');
640 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'raw_grade');
641 $dbman->change_field_type($table, $field);
642 upgrade_main_savepoint($result, 2008081506);
645 if ($result && $oldversion < 2008081600) {
647 /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
648 unset_config('unicodedb');
650 /// Main savepoint reached
651 upgrade_main_savepoint($result, 2008081600);
654 if ($result && $oldversion < 2008081900) {
655 /// Define field userid to be added to portfolio_tempdata
656 $table = new xmldb_table('portfolio_tempdata');
657 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'expirytime');
659 /// Conditionally launch add field userid
660 if (!$dbman->field_exists($table, $field)) {
661 $dbman->add_field($table, $field);
663 $DB->set_field('portfolio_tempdata', 'userid', 0);
664 /// now change it to be notnull
666 /// Changing nullability of field userid on table portfolio_tempdata to not null
667 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'expirytime');
669 /// Launch change of nullability for field userid
670 $dbman->change_field_notnull($table, $field);
672 /// Define key userfk (foreign) to be added to portfolio_tempdata
673 $table = new xmldb_table('portfolio_tempdata');
674 $key = new xmldb_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
676 /// Launch add key userfk
677 $dbman->add_key($table, $key);
679 upgrade_main_savepoint($result, 2008081900);
681 if ($result && $oldversion < 2008082602) {
683 /// Define table repository to be dropped
684 $table = new xmldb_table('repository');
686 /// Conditionally launch drop table for repository
687 if ($dbman->table_exists($table)) {
688 $dbman->drop_table($table);
691 /// Define table repository to be created
692 $table = new xmldb_table('repository');
694 /// Adding fields to table repository
695 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
696 $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
697 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1');
698 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
700 /// Adding keys to table repository
701 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
703 /// Conditionally launch create table for repository
704 if (!$dbman->table_exists($table)) {
705 $dbman->create_table($table);
707 /// Define table repository_instances to be created
708 $table = new xmldb_table('repository_instances');
710 /// Adding fields to table repository_instances
711 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
712 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
713 $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
714 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
715 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
716 $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null);
717 $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
718 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
719 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
721 /// Adding keys to table repository_instances
722 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
724 /// Conditionally launch create table for repository_instances
725 if (!$dbman->table_exists($table)) {
726 $dbman->create_table($table);
729 /// Define table repository_instance_config to be created
730 $table = new xmldb_table('repository_instance_config');
732 /// Adding fields to table repository_instance_config
733 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
734 $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
735 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
736 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
738 /// Adding keys to table repository_instance_config
739 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
741 /// Conditionally launch create table for repository_instance_config
742 if (!$dbman->table_exists($table)) {
743 $dbman->create_table($table);
746 /// Main savepoint reached
747 upgrade_main_savepoint($result, 2008082602);
750 if ($result && $oldversion < 2008082700) {
751 /// Add a new column to the question sessions table to record whether a
752 /// question has been flagged.
754 /// Define field flagged to be added to question_sessions
755 $table = new xmldb_table('question_sessions');
756 $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
758 /// Conditionally launch add field flagged
759 if (!$dbman->field_exists($table, $field)) {
760 $dbman->add_field($table, $field);
763 /// Main savepoint reached
764 upgrade_main_savepoint($result, 2008082700);
767 if ($result && $oldversion < 2008082900) {
769 /// Changing precision of field parent_type on table mnet_rpc to (20)
770 $table = new xmldb_table('mnet_rpc');
771 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
773 /// Launch change of precision for field parent_type
774 $dbman->change_field_precision($table, $field);
776 /// Main savepoint reached
777 upgrade_main_savepoint($result, 2008082900);
780 if ($result && $oldversion < 2008090108) {
781 $repo = new object();
782 $repo->type = 'upload';
784 $repo->sortorder = 1;
785 if (!$DB->record_exists('repository', array('type'=>'upload'))) {
786 $typeid = $DB->insert_record('repository', $repo);
788 $record = $DB->get_record('repository', array('type'=>'upload'));
789 $typeid = $record->id;
791 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
792 $instance = new object();
793 $instance->name = get_string('repositoryname', 'repository_upload');
794 $instance->typeid = $typeid;
795 $instance->userid = 0;
796 $instance->contextid = SITEID;
797 $instance->timecreated = time();
798 $instance->timemodified = time();
799 $result = $result && $DB->insert_record('repository_instances', $instance);
801 $repo->type = 'local';
803 $repo->sortorder = 1;
804 if (!$DB->record_exists('repository', array('type'=>'local'))) {
805 $typeid = $DB->insert_record('repository', $repo);
807 $record = $DB->get_record('repository', array('type'=>'local'));
808 $typeid = $record->id;
810 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
811 $instance = new object();
812 $instance->name = get_string('repositoryname', 'repository_local');
813 $instance->typeid = $typeid;
814 $instance->userid = 0;
815 $instance->contextid = SITEID;
816 $instance->timecreated = time();
817 $instance->timemodified = time();
818 $result = $result && $DB->insert_record('repository_instances', $instance);
821 upgrade_main_savepoint($result, 2008090108);
824 // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
825 if ($result && $oldversion < 2008091000) {
826 foreach (get_object_vars($CFG) as $name => $value) {
827 if (substr($name, strlen($name) - 8) !== '_version') {
830 $pluginname = substr($name, 0, strlen($name) - 8);
831 if (!strpos($pluginname, '_')) {
832 // Skip things like backup_version that don't contain an extra _
835 if ($pluginname == 'enrol_ldap_version') {
836 // Special case - this is something different from a plugin version number.
839 if (!preg_match('/^\d{10}$/', $value)) {
840 // Extra safety check, skip anything that does not look like a Moodle
841 // version number (10 digits).
844 $result = $result && set_config('version', $value, $pluginname);
845 $result = $result && unset_config($name);
847 upgrade_main_savepoint($result, 2008091000);
850 //Add a readonly field to the repository_instances table
851 //in order to support instance created automatically by a repository plugin
852 if ($result && $oldversion < 2008091611) {
854 /// Define field readonly to be added to repository_instances
855 $table = new xmldb_table('repository_instances');
856 $field = new xmldb_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
858 /// Conditionally launch add field readonly
859 if (!$dbman->field_exists($table, $field)) {
860 $dbman->add_field($table, $field);
863 /// Main savepoint reached
864 upgrade_main_savepoint($result, 2008091611);
867 if ($result && $oldversion < 2008092300) {
868 unset_config('editorspelling');
869 unset_config('editordictionary');
870 /// Main savepoint reached
871 upgrade_main_savepoint($result, 2008092300);
874 if ($result && $oldversion < 2008101300) {
876 if (!get_config(NULL, 'statsruntimedays')) {
877 set_config('statsruntimedays', '31');
880 /// Main savepoint reached
881 upgrade_main_savepoint($result, 2008101300);
884 /// New table for storing which roles can be assigned in which contexts.
885 if ($result && $oldversion < 2008110601) {
887 /// Define table role_context_levels to be created
888 $table = new xmldb_table('role_context_levels');
890 /// Adding fields to table role_context_levels
891 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
892 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
893 $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
895 /// Adding keys to table role_context_levels
896 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
897 $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
898 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
900 /// Conditionally launch create table for role_context_levels
901 if (!$dbman->table_exists($table)) {
902 $dbman->create_table($table);
905 /// Main savepoint reached
906 upgrade_main_savepoint($result, 2008110601);
909 /// Now populate the role_context_levels table with the defaults that match
910 /// moodle_install_roles, and any other combinations that exist in this system.
911 if ($result && $oldversion < 2008110602) {
912 $roleids = $DB->get_records_menu('role', array(), '', 'shortname,id');
914 /// Defaults, should match moodle_install_roles.
915 $rolecontextlevels = array();
916 if (isset($roleids['coursecreator'])) {
917 $rolecontextlevels[$roleids['coursecreator']] = get_default_contextlevels('coursecreator');
919 if (isset($roleids['editingteacher'])) {
920 $rolecontextlevels[$roleids['editingteacher']] = get_default_contextlevels('editingteacher');
922 if (isset($roleids['teacher'])) {
923 $rolecontextlevels[$roleids['teacher']] = get_default_contextlevels('teacher');
925 if (isset($roleids['student'])) {
926 $rolecontextlevels[$roleids['student']] = get_default_contextlevels('student');
928 if (isset($roleids['guest'])) {
929 $rolecontextlevels[$roleids['guest']] = get_default_contextlevels('guest');
931 if (isset($roleids['user'])) {
932 $rolecontextlevels[$roleids['user']] = get_default_contextlevels('user');
935 /// See what other role assignments are in this database, extend the allowed
936 /// lists to allow them too.
937 $existingrolecontextlevels = $DB->get_recordset_sql('SELECT DISTINCT ra.roleid, con.contextlevel FROM
938 {role_assignments} ra JOIN {context} con ON ra.contextid = con.id');
939 foreach ($existingrolecontextlevels as $rcl) {
940 if (!isset($rolecontextlevels[$rcl->roleid])) {
941 $rolecontextlevels[$rcl->roleid] = array($rcl->contextlevel);
942 } else if (!in_array($rcl->contextlevel, $rolecontextlevels[$rcl->roleid])) {
943 $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
947 /// Put the data into the database.
948 foreach ($rolecontextlevels as $roleid => $contextlevels) {
949 set_role_contextlevels($roleid, $contextlevels);
952 /// Main savepoint reached
953 upgrade_main_savepoint($result, 2008110602);
956 /// Drop the deprecated teacher, teachers, student and students columns from the course table.
957 if ($result && $oldversion < 2008111200) {
958 $table = new xmldb_table('course');
960 /// Conditionally launch drop field teacher
961 $field = new xmldb_field('teacher');
962 if ($dbman->field_exists($table, $field)) {
963 $dbman->drop_field($table, $field);
966 /// Conditionally launch drop field teacher
967 $field = new xmldb_field('teachers');
968 if ($dbman->field_exists($table, $field)) {
969 $dbman->drop_field($table, $field);
972 /// Conditionally launch drop field teacher
973 $field = new xmldb_field('student');
974 if ($dbman->field_exists($table, $field)) {
975 $dbman->drop_field($table, $field);
978 /// Conditionally launch drop field teacher
979 $field = new xmldb_field('students');
980 if ($dbman->field_exists($table, $field)) {
981 $dbman->drop_field($table, $field);
984 /// Main savepoint reached
985 upgrade_main_savepoint($result, 2008111200);
988 /// Add a unique index to the role.name column.
989 if ($result && $oldversion < 2008111800) {
991 /// Define index name (unique) to be added to role
992 $table = new xmldb_table('role');
993 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
995 /// Conditionally launch add index name
996 if (!$dbman->index_exists($table, $index)) {
997 $dbman->add_index($table, $index);
1000 /// Main savepoint reached
1001 upgrade_main_savepoint($result, 2008111800);
1004 /// Add a unique index to the role.shortname column.
1005 if ($result && $oldversion < 2008111801) {
1007 /// Define index shortname (unique) to be added to role
1008 $table = new xmldb_table('role');
1009 $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
1011 /// Conditionally launch add index shortname
1012 if (!$dbman->index_exists($table, $index)) {
1013 $dbman->add_index($table, $index);
1016 /// Main savepoint reached
1017 upgrade_main_savepoint($result, 2008111801);
1020 if ($result && $oldversion < 2008120700) {
1022 /// Changing precision of field shortname on table course_request to (100)
1023 $table = new xmldb_table('course_request');
1024 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
1026 /// Before changing the field, drop dependent indexes
1027 /// Define index shortname (not unique) to be dropped form course_request
1028 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
1029 /// Conditionally launch drop index shortname
1030 if ($dbman->index_exists($table, $index)) {
1031 $dbman->drop_index($table, $index);
1034 /// Launch change of precision for field shortname
1035 $dbman->change_field_precision($table, $field);
1037 /// After changing the field, recreate dependent indexes
1038 /// Define index shortname (not unique) to be added to course_request
1039 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
1040 /// Conditionally launch add index shortname
1041 if (!$dbman->index_exists($table, $index)) {
1042 $dbman->add_index($table, $index);
1045 /// Main savepoint reached
1046 upgrade_main_savepoint($result, 2008120700);
1049 if ($result && $oldversion < 2008120801) {
1051 /// Changing precision of field shortname on table mnet_enrol_course to (100)
1052 $table = new xmldb_table('mnet_enrol_course');
1053 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
1055 /// Launch change of precision for field shortname
1056 $dbman->change_field_precision($table, $field);
1058 /// Main savepoint reached
1059 upgrade_main_savepoint($result, 2008120801);
1062 if ($result && $oldversion < 2008121701) {
1064 /// Define field availablefrom to be added to course_modules
1065 $table = new xmldb_table('course_modules');
1066 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
1068 /// Conditionally launch add field availablefrom
1069 if (!$dbman->field_exists($table, $field)) {
1070 $dbman->add_field($table, $field);
1073 /// Define field availableuntil to be added to course_modules
1074 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
1076 /// Conditionally launch add field availableuntil
1077 if (!$dbman->field_exists($table, $field)) {
1078 $dbman->add_field($table, $field);
1081 /// Define field showavailability to be added to course_modules
1082 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
1084 /// Conditionally launch add field showavailability
1085 if (!$dbman->field_exists($table, $field)) {
1086 $dbman->add_field($table, $field);
1089 /// Define table course_modules_availability to be created
1090 $table = new xmldb_table('course_modules_availability');
1092 /// Adding fields to table course_modules_availability
1093 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1094 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1095 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1096 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
1097 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1098 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1099 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1101 /// Adding keys to table course_modules_availability
1102 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1103 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
1104 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
1105 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
1107 /// Conditionally launch create table for course_modules_availability
1108 if (!$dbman->table_exists($table)) {
1109 $dbman->create_table($table);
1112 /// Changes to modinfo mean we need to rebuild course cache
1113 require_once($CFG->dirroot . '/course/lib.php');
1114 rebuild_course_cache(0, true);
1116 /// For developer upgrades, turn on the conditional activities and completion
1117 /// features automatically (to gain more testing)
1118 //TODO: remove before 2.0 final!
1119 if (debugging('', DEBUG_DEVELOPER)) {
1120 set_config('enableavailability', 1);
1121 set_config('enablecompletion', 1);
1124 /// Main savepoint reached
1125 upgrade_main_savepoint($result, 2008121701);
1128 if ($result && $oldversion < 2009010500) {
1129 /// clean up config table a bit
1130 unset_config('session_error_counter');
1132 /// Main savepoint reached
1133 upgrade_main_savepoint($result, 2009010500);
1136 if ($result && $oldversion < 2009010600) {
1138 /// Define field originalquestion to be dropped from question_states
1139 $table = new xmldb_table('question_states');
1140 $field = new xmldb_field('originalquestion');
1142 /// Conditionally launch drop field originalquestion
1143 if ($dbman->field_exists($table, $field)) {
1144 $dbman->drop_field($table, $field);
1147 /// Main savepoint reached
1148 upgrade_main_savepoint($result, 2009010600);
1151 if ($result && $oldversion < 2009010601) {
1153 /// Changing precision of field ip on table log to (45)
1154 $table = new xmldb_table('log');
1155 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1157 /// Launch change of precision for field ip
1158 $dbman->change_field_precision($table, $field);
1160 /// Main savepoint reached
1161 upgrade_main_savepoint($result, 2009010601);
1164 if ($result && $oldversion < 2009010602) {
1166 /// Changing precision of field lastip on table user to (45)
1167 $table = new xmldb_table('user');
1168 $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
1170 /// Launch change of precision for field lastip
1171 $dbman->change_field_precision($table, $field);
1173 /// Main savepoint reached
1174 upgrade_main_savepoint($result, 2009010602);
1177 if ($result && $oldversion < 2009010603) {
1179 /// Changing precision of field ip_address on table mnet_host to (45)
1180 $table = new xmldb_table('mnet_host');
1181 $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
1183 /// Launch change of precision for field ip_address
1184 $dbman->change_field_precision($table, $field);
1186 /// Main savepoint reached
1187 upgrade_main_savepoint($result, 2009010603);
1190 if ($result && $oldversion < 2009010604) {
1192 /// Changing precision of field ip on table mnet_log to (45)
1193 $table = new xmldb_table('mnet_log');
1194 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1196 /// Launch change of precision for field ip
1197 $dbman->change_field_precision($table, $field);
1199 /// Main savepoint reached
1200 upgrade_main_savepoint($result, 2009010604);
1203 if ($result && $oldversion < 2009010800) {
1204 /// Update the notifyloginfailures setting.
1205 if ($CFG->notifyloginfailures == 'mainadmin') {
1206 set_config('notifyloginfailures', get_admin()->username);
1207 } else if ($CFG->notifyloginfailures == 'alladmins') {
1208 set_config('notifyloginfailures', '$@ALL@$');
1210 set_config('notifyloginfailures', '');
1213 /// Main savepoint reached
1214 upgrade_main_savepoint($result, 2009010800);
1217 if ($result && $oldversion < 2009011000) {
1219 /// Changing nullability of field configdata on table block_instance to null
1220 $table = new xmldb_table('block_instance');
1221 $field = new xmldb_field('configdata');
1222 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
1224 /// Launch change of nullability for field configdata
1225 $dbman->change_field_notnull($table, $field);
1227 /// Main savepoint reached
1228 upgrade_main_savepoint($result, 2009011000);
1231 if ($result && $oldversion < 2009011100) {
1232 /// Remove unused settings
1233 unset_config('zip');
1234 unset_config('unzip');
1235 unset_config('adminblocks_initialised');
1237 /// Main savepoint reached
1238 upgrade_main_savepoint($result, 2009011100);
1241 if ($result && $oldversion < 2009011101) {
1242 /// Migrate backup settings to core plugin config table
1243 $configs = $DB->get_records('backup_config');
1244 foreach ($configs as $config) {
1245 set_config($config->name, $config->value, 'backup');
1248 /// Define table to be dropped
1249 $table = new xmldb_table('backup_config');
1251 /// Launch drop table for old backup config
1252 $dbman->drop_table($table);
1254 /// Main savepoint reached
1255 upgrade_main_savepoint($result, 2009011101);
1258 if ($result && $oldversion < 2009011303) {
1260 /// Define table config_log to be created
1261 $table = new xmldb_table('config_log');
1263 /// Adding fields to table config_log
1264 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1265 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1266 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1267 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1268 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1269 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1270 $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1272 /// Adding keys to table config_log
1273 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1274 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1276 /// Adding indexes to table config_log
1277 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1279 /// Launch create table for config_log
1280 $dbman->create_table($table);
1282 /// Main savepoint reached
1283 upgrade_main_savepoint($result, 2009011303);
1286 if ($result && $oldversion < 2009011900) {
1288 /// Define table sessions2 to be dropped
1289 $table = new xmldb_table('sessions2');
1291 /// Conditionally launch drop table for sessions
1292 if ($dbman->table_exists($table)) {
1293 $dbman->drop_table($table);
1296 /// Define table sessions to be dropped
1297 $table = new xmldb_table('sessions');
1299 /// Conditionally launch drop table for sessions
1300 if ($dbman->table_exists($table)) {
1301 $dbman->drop_table($table);
1304 /// Define table sessions to be created
1305 $table = new xmldb_table('sessions');
1307 /// Adding fields to table sessions
1308 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1309 $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1310 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
1311 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1312 $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
1313 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1314 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1315 $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1316 $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1318 /// Adding keys to table sessions
1319 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1320 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1322 /// Adding indexes to table sessions
1323 $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1324 $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1325 $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1326 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1328 /// Launch create table for sessions
1329 $dbman->create_table($table);
1331 /// Main savepoint reached
1332 upgrade_main_savepoint($result, 2009011900);
1335 if ($result && $oldversion < 2009012901) {
1336 // NOTE: this table may already exist, see beginning of this file ;-)
1338 /// Define table upgrade_log to be created
1339 $table = new xmldb_table('upgrade_log');
1341 /// Adding fields to table upgrade_log
1342 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1343 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1344 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1345 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1346 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1347 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1348 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1349 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1350 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1352 /// Adding keys to table upgrade_log
1353 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1354 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1356 /// Adding indexes to table upgrade_log
1357 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1358 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
1360 /// Conditionally launch create table for upgrade_log
1361 if (!$dbman->table_exists($table)) {
1362 $dbman->create_table($table);
1365 /// Main savepoint reached
1366 upgrade_main_savepoint($result, 2009012901);
1369 if ($result && $oldversion < 2009021800) {
1370 // Converting format of grade conditions, if any exist, to percentages.
1372 UPDATE {course_modules_availability} SET grademin=(
1373 SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
1374 /(gi.grademax-gi.grademin)
1375 FROM {grade_items} gi
1376 WHERE gi.id={course_modules_availability}.gradeitemid)
1377 WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1379 UPDATE {course_modules_availability} SET grademax=(
1380 SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
1381 /(gi.grademax-gi.grademin)
1382 FROM {grade_items} gi
1383 WHERE gi.id={course_modules_availability}.gradeitemid)
1384 WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1386 /// Main savepoint reached
1387 upgrade_main_savepoint($result, 2009021800);
1389 if ($result && $oldversion < 2009021801) {
1390 /// Define field backuptype to be added to backup_log
1391 $table = new xmldb_table('backup_log');
1392 $field = new xmldb_field('backuptype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'info');
1393 /// Conditionally Launch add field backuptype and set all old records as 'scheduledbackup' records.
1394 if (!$dbman->field_exists($table, $field)) {
1395 $dbman->add_field($table, $field);
1396 $DB->execute("UPDATE {backup_log} SET backuptype='scheduledbackup'");
1399 /// Main savepoint reached
1400 upgrade_main_savepoint($result, 2009021801);
1402 /// Add default sort order for question types.
1403 if ($result && $oldversion < 2009030300) {
1404 set_config('multichoice_sortorder', 1, 'question');
1405 set_config('truefalse_sortorder', 2, 'question');
1406 set_config('shortanswer_sortorder', 3, 'question');
1407 set_config('numerical_sortorder', 4, 'question');
1408 set_config('calculated_sortorder', 5, 'question');
1409 set_config('essay_sortorder', 6, 'question');
1410 set_config('match_sortorder', 7, 'question');
1411 set_config('randomsamatch_sortorder', 8, 'question');
1412 set_config('multianswer_sortorder', 9, 'question');
1413 set_config('description_sortorder', 10, 'question');
1414 set_config('random_sortorder', 11, 'question');
1415 set_config('missingtype_sortorder', 12, 'question');
1417 upgrade_main_savepoint($result, 2009030300);
1419 if ($result && $oldversion < 2009030501) {
1420 /// setup default repository plugins
1421 require_once($CFG->dirroot . '/repository/lib.php');
1422 repository_setup_default_plugins();
1423 /// Main savepoint reached
1424 upgrade_main_savepoint($result, 2009030501);
1427 /// MDL-18132 replace the use a new Role allow switch settings page, instead of
1428 /// $CFG->allowuserswitchrolestheycantassign
1429 if ($result && $oldversion < 2009032000) {
1430 /// First create the new table.
1431 $table = new xmldb_table('role_allow_switch');
1433 /// Adding fields to table role_allow_switch
1434 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1435 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1436 $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1438 /// Adding keys to table role_allow_switch
1439 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1440 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
1441 $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
1443 /// Adding indexes to table role_allow_switch
1444 $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1446 /// Conditionally launch create table for role_allow_switch
1447 if (!$dbman->table_exists($table)) {
1448 $dbman->create_table($table);
1451 /// Main savepoint reached
1452 upgrade_main_savepoint($result, 2009032000);
1455 if ($result && $oldversion < 2009032001) {
1456 /// Copy from role_allow_assign into the new table.
1457 $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1458 SELECT roleid, allowassign FROM {role_allow_assign}');
1460 /// Unset the config variable used in 1.9.
1461 unset_config('allowuserswitchrolestheycantassign');
1463 /// Main savepoint reached
1464 upgrade_main_savepoint($result, 2009032001);
1467 if ($result && $oldversion < 2009033100) {
1468 require_once("$CFG->dirroot/filter/tex/lib.php");
1469 filter_tex_updatedcallback(null);
1470 /// Main savepoint reached
1471 upgrade_main_savepoint($result, 2009033100);
1474 if ($result && $oldversion < 2009040300) {
1476 /// Define table filter_active to be created
1477 $table = new xmldb_table('filter_active');
1479 /// Adding fields to table filter_active
1480 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1481 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1482 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1483 $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1484 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1486 /// Adding keys to table filter_active
1487 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1488 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1490 /// Adding indexes to table filter_active
1491 $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1493 /// Conditionally launch create table for filter_active
1494 if (!$dbman->table_exists($table)) {
1495 $dbman->create_table($table);
1498 /// Main savepoint reached
1499 upgrade_main_savepoint($result, 2009040300);
1502 if ($result && $oldversion < 2009040301) {
1504 /// Define table filter_config to be created
1505 $table = new xmldb_table('filter_config');
1507 /// Adding fields to table filter_config
1508 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1509 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1510 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1511 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1512 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1514 /// Adding keys to table filter_config
1515 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1516 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1518 /// Adding indexes to table filter_config
1519 $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1521 /// Conditionally launch create table for filter_config
1522 if (!$dbman->table_exists($table)) {
1523 $dbman->create_table($table);
1526 /// Main savepoint reached
1527 upgrade_main_savepoint($result, 2009040301);
1530 if ($result && $oldversion < 2009040302) {
1531 /// Transfer current settings from $CFG->textfilters
1532 $disabledfilters = filter_get_all_installed();
1533 if (empty($CFG->textfilters)) {
1534 $activefilters = array();
1536 $activefilters = explode(',', $CFG->textfilters);
1538 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1540 foreach ($activefilters as $filter) {
1541 filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1543 unset($disabledfilters[$filter]);
1545 foreach ($disabledfilters as $filter => $notused) {
1546 filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1550 /// Main savepoint reached
1551 upgrade_main_savepoint($result, 2009040302);
1554 if ($result && $oldversion < 2009040600) {
1555 /// Ensure that $CFG->stringfilters is set.
1556 if (empty($CFG->stringfilters)) {
1557 if (!empty($CFG->filterall)) {
1558 set_config('stringfilters', $CFG->textfilters);
1560 set_config('stringfilters', '');
1564 set_config('filterall', !empty($CFG->stringfilters));
1565 unset_config('textfilters');
1567 /// Main savepoint reached
1568 upgrade_main_savepoint($result, 2009040600);
1571 if ($result && $oldversion < 2009041700) {
1572 /// To ensure the UI remains consistent with no behaviour change, any
1573 /// 'until' date in an activity condition should have 1 second subtracted
1574 /// (to go from 0:00 on the following day to 23:59 on the previous one).
1575 $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
1576 require_once($CFG->dirroot . '/course/lib.php');
1577 rebuild_course_cache(0, true);
1579 /// Main savepoint reached
1580 upgrade_main_savepoint($result, 2009041700);
1583 if ($result && $oldversion < 2009042600) {
1584 /// Deleting orphaned messages from deleted users.
1585 require_once($CFG->dirroot.'/message/lib.php');
1586 /// Detect deleted users with messages sent(useridfrom) and not read
1587 if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
1589 JOIN {message} m ON m.useridfrom = u.id
1590 WHERE u.deleted = ?', array(1))) {
1591 foreach ($deletedusers as $deleteduser) {
1592 message_move_userfrom_unread2read($deleteduser->id); // move messages
1595 /// Main savepoint reached
1596 upgrade_main_savepoint($result, 2009042600);
1599 /// Dropping all enums/check contraints from core. MDL-18577
1600 if ($result && $oldversion < 2009042700) {
1602 /// Changing list of values (enum) of field stattype on table stats_daily to none
1603 $table = new xmldb_table('stats_daily');
1604 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1606 /// Launch change of list of values for field stattype
1607 $dbman->drop_enum_from_field($table, $field);
1609 /// Changing list of values (enum) of field stattype on table stats_weekly to none
1610 $table = new xmldb_table('stats_weekly');
1611 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1613 /// Launch change of list of values for field stattype
1614 $dbman->drop_enum_from_field($table, $field);
1616 /// Changing list of values (enum) of field stattype on table stats_monthly to none
1617 $table = new xmldb_table('stats_monthly');
1618 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1620 /// Launch change of list of values for field stattype
1621 $dbman->drop_enum_from_field($table, $field);
1623 /// Changing list of values (enum) of field publishstate on table post to none
1624 $table = new xmldb_table('post');
1625 $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
1627 /// Launch change of list of values for field publishstate
1628 $dbman->drop_enum_from_field($table, $field);
1630 /// Main savepoint reached
1631 upgrade_main_savepoint($result, 2009042700);
1634 if ($result && $oldversion < 2009043000) {
1635 unset_config('grade_report_showgroups');
1636 upgrade_main_savepoint($result, 2009043000);
1639 if ($result && $oldversion < 2009050600) {
1640 /// Site front page blocks need to be moved due to page name change.
1641 $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
1643 /// Main savepoint reached
1644 upgrade_main_savepoint($result, 2009050600);
1647 if ($result && $oldversion < 2009050601) {
1649 /// Define table block_instance to be renamed to block_instances
1650 $table = new xmldb_table('block_instance');
1652 /// Launch rename table for block_instance
1653 $dbman->rename_table($table, 'block_instances');
1655 /// Main savepoint reached
1656 upgrade_main_savepoint($result, 2009050601);
1659 if ($result && $oldversion < 2009050602) {
1661 /// Define table block_instance to be renamed to block_instance_old
1662 $table = new xmldb_table('block_pinned');
1664 /// Launch rename table for block_instance
1665 $dbman->rename_table($table, 'block_pinned_old');
1667 /// Main savepoint reached
1668 upgrade_main_savepoint($result, 2009050602);
1671 if ($result && $oldversion < 2009050603) {
1673 /// Define table block_instance_old to be created
1674 $table = new xmldb_table('block_instance_old');
1676 /// Adding fields to table block_instance_old
1677 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1678 $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1679 $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1680 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1681 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
1682 $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
1683 $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
1684 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1685 $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1687 /// Adding keys to table block_instance_old
1688 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1689 $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
1691 /// Adding indexes to table block_instance_old
1692 $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1693 $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
1695 /// Conditionally launch create table for block_instance_old
1696 if (!$dbman->table_exists($table)) {
1697 $dbman->create_table($table);
1700 /// Main savepoint reached
1701 upgrade_main_savepoint($result, 2009050603);
1704 if ($result && $oldversion < 2009050604) {
1705 /// Copy current blocks data from block_instances to block_instance_old
1706 $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
1707 SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
1709 upgrade_main_savepoint($result, 2009050604);
1712 if ($result && $oldversion < 2009050605) {
1714 /// Define field multiple to be dropped from block
1715 $table = new xmldb_table('block');
1716 $field = new xmldb_field('multiple');
1718 /// Conditionally launch drop field multiple
1719 if ($dbman->field_exists($table, $field)) {
1720 $dbman->drop_field($table, $field);
1723 /// Main savepoint reached
1724 upgrade_main_savepoint($result, 2009050605);
1727 if ($result && $oldversion < 2009050606) {
1728 $table = new xmldb_table('block_instances');
1730 /// Rename field weight on table block_instances to defaultweight
1731 $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'position');
1732 $dbman->rename_field($table, $field, 'defaultweight');
1734 /// Rename field position on table block_instances to defaultregion
1735 $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
1736 $dbman->rename_field($table, $field, 'defaultregion');
1738 /// Main savepoint reached
1739 upgrade_main_savepoint($result, 2009050606);
1742 if ($result && $oldversion < 2009050607) {
1743 /// Changing precision of field defaultregion on table block_instances to (16)
1744 $table = new xmldb_table('block_instances');
1745 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
1747 /// Launch change of precision for field defaultregion
1748 $dbman->change_field_precision($table, $field);
1750 /// Main savepoint reached
1751 upgrade_main_savepoint($result, 2009050607);
1754 if ($result && $oldversion < 2009050608) {
1755 /// Change regions to the new notation
1756 $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1757 $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1758 $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1759 // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1760 // flex page course format. Hopefully this new value is an adequate alternative.
1762 /// Main savepoint reached
1763 upgrade_main_savepoint($result, 2009050608);
1766 if ($result && $oldversion < 2009050609) {
1768 /// Define key blockname (unique) to be added to block
1769 $table = new xmldb_table('block');
1770 $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1772 /// Launch add key blockname
1773 $dbman->add_key($table, $key);
1775 /// Main savepoint reached
1776 upgrade_main_savepoint($result, 2009050609);
1779 if ($result && $oldversion < 2009050610) {
1780 $table = new xmldb_table('block_instances');
1782 /// Define field blockname to be added to block_instances
1783 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
1784 if (!$dbman->field_exists($table, $field)) {
1785 $dbman->add_field($table, $field);
1788 /// Define field contextid to be added to block_instances
1789 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
1790 if (!$dbman->field_exists($table, $field)) {
1791 $dbman->add_field($table, $field);
1794 /// Define field showinsubcontexts to be added to block_instances
1795 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
1796 if (!$dbman->field_exists($table, $field)) {
1797 $dbman->add_field($table, $field);
1800 /// Define field subpagepattern to be added to block_instances
1801 $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
1802 if (!$dbman->field_exists($table, $field)) {
1803 $dbman->add_field($table, $field);
1806 /// Main savepoint reached
1807 upgrade_main_savepoint($result, 2009050610);
1810 if ($result && $oldversion < 2009050611) {
1811 $table = new xmldb_table('block_instances');
1813 /// Fill in blockname from blockid
1814 $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1816 /// Set showinsubcontexts = 0 for all rows.
1817 $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1819 /// Main savepoint reached
1820 upgrade_main_savepoint($result, 2009050611);
1823 if ($result && $oldversion < 2009050612) {
1825 /// Rename field pagetype on table block_instances to pagetypepattern
1826 $table = new xmldb_table('block_instances');
1827 $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
1829 /// Launch rename field pagetype
1830 $dbman->rename_field($table, $field, 'pagetypepattern');
1832 /// Main savepoint reached
1833 upgrade_main_savepoint($result, 2009050612);
1836 if ($result && $oldversion < 2009050613) {
1837 /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1840 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
1841 $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1842 pagetypepattern = 'site-index',
1843 subpagepattern = NULL
1844 WHERE pagetypepattern = 'site-index'");
1847 $DB->execute("UPDATE {block_instances} SET
1851 JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1852 WHERE {course}.id = pageid
1854 pagetypepattern = 'course-view-*',
1855 subpagepattern = NULL
1856 WHERE pagetypepattern = 'course-view'");
1859 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1860 $DB->execute("UPDATE {block_instances} SET
1861 contextid = " . $syscontext->id . ",
1862 pagetypepattern = 'admin-*',
1863 subpagepattern = NULL
1864 WHERE pagetypepattern = 'admin'");
1867 $DB->execute("UPDATE {block_instances} SET
1871 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1872 WHERE {user}.id = pageid
1874 pagetypepattern = 'my-index',
1875 subpagepattern = NULL
1876 WHERE pagetypepattern = 'my-index'");
1879 $DB->execute("UPDATE {block_instances} SET
1880 contextid = " . $syscontext->id . ",
1881 pagetypepattern = 'tag-index',
1882 subpagepattern = pageid
1883 WHERE pagetypepattern = 'tag-index'");
1886 $DB->execute("UPDATE {block_instances} SET
1890 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1891 WHERE {user}.id = pageid
1893 pagetypepattern = 'blog-index',
1894 subpagepattern = NULL
1895 WHERE pagetypepattern = 'blog-view'");
1898 $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1899 foreach ($moduleswithblocks as $modname) {
1900 if (!$dbman->table_exists($modname)) {
1903 $DB->execute("UPDATE {block_instances} SET
1907 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1908 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1909 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1910 WHERE {{$modname}}.id = pageid
1912 pagetypepattern = 'blog-index',
1913 subpagepattern = NULL
1914 WHERE pagetypepattern = 'blog-view'");
1917 /// Main savepoint reached
1918 upgrade_main_savepoint($result, 2009050613);
1921 if ($result && $oldversion < 2009050614) {
1922 /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
1923 $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
1925 /// Main savepoint reached
1926 upgrade_main_savepoint($result, 2009050614);
1929 if ($result && $oldversion < 2009050615) {
1930 $table = new xmldb_table('block_instances');
1932 /// Changing nullability of field blockname on table block_instances to not null
1933 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
1934 $dbman->change_field_notnull($table, $field);
1936 /// Changing nullability of field contextid on table block_instances to not null
1937 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
1938 $dbman->change_field_notnull($table, $field);
1940 /// Changing nullability of field showinsubcontexts on table block_instances to not null
1941 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
1942 $dbman->change_field_notnull($table, $field);
1944 /// Main savepoint reached
1945 upgrade_main_savepoint($result, 2009050615);
1948 if ($result && $oldversion < 2009050616) {
1949 /// Add exiting sticky blocks.
1950 $blocks = $DB->get_records('block');
1951 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1952 $newregions = array(
1955 'c' => 'course-view-top',
1957 $stickyblocks = $DB->get_recordset('block_pinned_old');
1958 foreach ($stickyblocks as $stickyblock) {
1959 $newblock = new object();
1960 $newblock->blockname = $blocks[$stickyblock->blockid]->name;
1961 $newblock->contextid = $syscontext->id;
1962 $newblock->showinsubcontexts = 1;
1963 switch ($stickyblock->pagetype) {
1965 $newblock->pagetypepattern = 'course-view-*';
1968 $newblock->pagetypepattern = $stickyblock->pagetype;
1970 $newblock->defaultregion = $newregions[$stickyblock->position];
1971 $newblock->defaultweight = $stickyblock->weight;
1972 $newblock->configdata = $stickyblock->configdata;
1973 $newblock->visible = 1;
1974 $DB->insert_record('block_instances', $newblock);
1977 /// Main savepoint reached
1978 upgrade_main_savepoint($result, 2009050616);
1981 if ($result && $oldversion < 2009050617) {
1983 /// Define table block_positions to be created
1984 $table = new xmldb_table('block_positions');
1986 /// Adding fields to table block_positions
1987 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1988 $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1989 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1990 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1991 $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1992 $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1');
1993 $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1994 $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1996 /// Adding keys to table block_positions
1997 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1998 $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
1999 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2001 /// Adding indexes to table block_positions
2002 $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
2004 /// Conditionally launch create table for block_positions
2005 if (!$dbman->table_exists($table)) {
2006 $dbman->create_table($table);
2009 /// Main savepoint reached
2010 upgrade_main_savepoint($result, 2009050617);
2013 if ($result && $oldversion < 2009050618) {
2014 /// And block instances with visible = 0, copy that information to block_positions
2015 $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
2016 SELECT id, contextid,
2017 CASE WHEN pagetypepattern = 'course-view-*' THEN
2018 (SELECT " . $DB->sql_concat("'course-view-'", 'format') . "
2020 JOIN {context} ON {course}.id = {context}.instanceid
2021 WHERE {context}.id = contextid)
2022 ELSE pagetypepattern END,
2023 CASE WHEN subpagepattern IS NULL THEN ''
2024 ELSE subpagepattern END,
2025 0, defaultregion, defaultweight
2026 FROM {block_instances} WHERE visible = 0 AND pagetypepattern <> 'admin-*'");
2028 /// Main savepoint reached
2029 upgrade_main_savepoint($result, 2009050618);
2032 if ($result && $oldversion < 2009050619) {
2033 $table = new xmldb_table('block_instances');
2035 /// Define field blockid to be dropped from block_instances
2036 $field = new xmldb_field('blockid');
2037 if ($dbman->field_exists($table, $field)) {
2038 /// Before dropping the field, drop dependent indexes
2039 $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
2040 if ($dbman->index_exists($table, $index)) {
2041 /// Launch drop index blockid
2042 $dbman->drop_index($table, $index);
2044 $dbman->drop_field($table, $field);
2047 /// Define field pageid to be dropped from block_instances
2048 $field = new xmldb_field('pageid');
2049 if ($dbman->field_exists($table, $field)) {
2050 /// Before dropping the field, drop dependent indexes
2051 $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
2052 if ($dbman->index_exists($table, $index)) {
2053 /// Launch drop index pageid
2054 $dbman->drop_index($table, $index);
2056 $dbman->drop_field($table, $field);
2059 /// Define field visible to be dropped from block_instances
2060 $field = new xmldb_field('visible');
2061 if ($dbman->field_exists($table, $field)) {
2062 $dbman->drop_field($table, $field);
2065 /// Main savepoint reached
2066 upgrade_main_savepoint($result, 2009050619);
2069 if ($result && $oldversion < 2009051200) {
2070 /// Let's check the status of mandatory mnet_host records, fixing them
2071 /// and moving "orphan" users to default localhost record. MDL-16879
2072 echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
2073 upgrade_fix_incorrect_mnethostids();
2075 /// Main savepoint reached
2076 upgrade_main_savepoint($result, 2009051200);
2080 if ($result && $oldversion < 2009051700) {
2081 /// migrate editor settings
2082 if (empty($CFG->htmleditor)) {
2083 set_config('texteditors', 'textarea');
2085 set_config('texteditors', 'tinymce,textarea');
2088 unset_config('htmleditor');
2089 unset_config('defaulthtmleditor');
2091 /// Main savepoint reached
2092 upgrade_main_savepoint($result, 2009051700);
2095 if ($result && $oldversion < 2009060200) {
2096 /// Define table files_cleanup to be dropped - not needed
2097 $table = new xmldb_table('files_cleanup');
2099 /// Conditionally launch drop table for files_cleanup
2100 if ($dbman->table_exists($table)) {
2101 $dbman->drop_table($table);
2104 /// Main savepoint reached
2105 upgrade_main_savepoint($result, 2009060200);
2108 if ($result && $oldversion < 2009061300) {
2109 //TODO: copy this to the very beginning of this upgrade script so that we may log upgrade queries
2111 /// Define table log_queries to be created
2112 $table = new xmldb_table('log_queries');
2114 /// Adding fields to table log_queries
2115 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2116 $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2117 $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
2118 $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
2119 $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2120 $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2121 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2122 $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
2123 $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2125 /// Adding keys to table log_queries
2126 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2128 /// Conditionally launch create table for log_queries
2129 if (!$dbman->table_exists($table)) {
2130 $dbman->create_table($table);
2133 /// Main savepoint reached
2134 upgrade_main_savepoint($result, 2009061300);
2137 /// Repeat 2009050607 upgrade step, which Petr commented out becuase of XMLDB
2138 /// stupidity, so lots of peopel will have missed.
2139 if ($result && $oldversion < 2009061600) {
2140 /// Changing precision of field defaultregion on table block_instances to (16)
2141 $table = new xmldb_table('block_instances');
2142 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
2144 /// Launch change of precision for field defaultregion
2145 $dbman->change_field_precision($table, $field);
2147 /// Main savepoint reached
2148 upgrade_main_savepoint($result, 2009061600);
2151 if ($result && $oldversion < 2009061702) {
2152 // standardizing plugin names
2153 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
2154 foreach ($configs as $config) {
2155 $result = $result && unset_config($config->name, $config->plugin); /// unset old config
2156 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
2157 $result = $result && set_config($config->name, $config->value, $config->plugin); /// set new config
2161 upgrade_main_savepoint($result, 2009061702);
2164 if ($result && $oldversion < 2009061703) {
2165 // standardizing plugin names
2166 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
2167 foreach ($configs as $config) {
2168 $result = $result && unset_config($config->name, $config->plugin); /// unset old config
2169 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
2170 $result = $result && set_config($config->name, $config->value, $config->plugin); /// set new config
2174 upgrade_main_savepoint($result, 2009061703);
2177 if ($result && $oldversion < 2009061704) {
2178 // change component string in capability records to new "_" format
2179 if ($caps = $DB->get_records('capabilities')) {
2180 foreach ($caps as $cap) {
2181 $cap->component = str_replace('/', '_', $cap->component);
2182 $DB->update_record('capabilities', $cap);
2186 upgrade_main_savepoint($result, 2009061704);
2189 if ($result && $oldversion < 2009061705) {
2190 // change component string in events_handlers records to new "_" format
2191 if ($handlers = $DB->get_records('events_handlers')) {
2192 foreach ($handlers as $handler) {
2193 $handler->handlermodule = str_replace('/', '_', $handler->handlermodule);
2194 $DB->update_record('events_handlers', $handler);
2198 upgrade_main_savepoint($result, 2009061705);
2201 if ($result && $oldversion < 2009061706) {
2202 // change component string in message_providers records to new "_" format
2203 if ($mps = $DB->get_records('message_providers')) {
2204 foreach ($mps as $mp) {
2205 $mp->component = str_replace('/', '_', $mp->component);
2206 $DB->update_record('message_providers', $cap);
2210 upgrade_main_savepoint($result, 2009061706);
2213 if ($result && $oldversion < 2009063000) {
2214 // upgrade format of _with_advanced settings - quiz only
2215 // note: this can be removed later, not needed for upgrades from 1.9.x
2216 if ($quiz = get_config('quiz')) {
2217 foreach ($quiz as $name=>$value) {
2218 if (strpos($name, 'fix_') !== 0) {
2221 $newname = substr($name,4).'_adv';
2222 set_config($newname, $value, 'quiz');
2223 unset_config($name, 'quiz');
2226 upgrade_main_savepoint($result, 2009063000);
2229 if ($result && $oldversion < 2009071000) {
2231 /// Rename field contextid on table block_instances to parentcontextid
2232 $table = new xmldb_table('block_instances');
2233 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
2235 /// Launch rename field parentcontextid
2236 $dbman->rename_field($table, $field, 'parentcontextid');
2238 /// Main savepoint reached
2239 upgrade_main_savepoint($result, 2009071000);
2242 if ($result && $oldversion < 2009071300) {
2244 /// Create contexts for every block. In the past, only non-sticky course block had contexts.
2245 /// This is a copy of the code in create_contexts.
2246 $sql = "INSERT INTO {context} (contextlevel, instanceid)
2247 SELECT " . CONTEXT_BLOCK . ", bi.id
2248 FROM {block_instances} bi
2249 WHERE NOT EXISTS (SELECT 'x'
2251 WHERE bi.id = ctx.instanceid AND ctx.contextlevel=" . CONTEXT_BLOCK . ")";
2254 /// TODO MDL-19776 We should not really use API funcitons in upgrade.
2255 /// If MDL-19776 is done, we can remove this whole upgrade block.
2256 build_context_path();
2258 /// Main savepoint reached
2259 upgrade_main_savepoint($result, 2009071300);
2262 if ($result && $oldversion < 2009071600) {
2264 /// Define field summaryformat to be added to post
2265 $table = new xmldb_table('post');
2266 $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
2268 /// Conditionally launch add field summaryformat
2269 if (!$dbman->field_exists($table, $field)) {
2270 $dbman->add_field($table, $field);
2273 /// Main savepoint reached
2274 upgrade_main_savepoint($result, 2009071600);
2277 if ($result && $oldversion < 2009072400) {
2279 /// Define table comments to be created
2280 $table = new xmldb_table('comments');
2282 /// Adding fields to table comments
2283 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2284 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2285 $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2286 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2287 $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2288 $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2289 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2290 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2292 /// Adding keys to table comments
2293 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2295 /// Conditionally launch create table for comments
2296 if (!$dbman->table_exists($table)) {
2297 $dbman->create_table($table);
2300 /// Main savepoint reached
2301 upgrade_main_savepoint($result, 2009072400);
2305 * This upgrade is to set up the new navigation blocks that have been developed
2306 * as part of Moodle 2.0
2307 * Now I [Sam Hemelryk] hit a conundrum while exploring how to go about this
2308 * as not only do we want to install the new blocks but we also want to set up
2309 * default instances of them, and at the same time remove instances of the blocks
2310 * that were/will-be outmoded by the two new navigation blocks.
2311 * After talking it through with Tim Hunt {@link http://moodle.org/mod/cvsadmin/view.php?conversationid=3112}
2312 * we decided that the best way to go about this was to put the bulk of the
2313 * upgrade operation into core upgrade `here` but to let the plugins block
2314 * still install the blocks.
2315 * This leaves one hairy end in that we will create block_instances within the
2316 * DB before the blocks themselves are created within the DB
2318 if ($result && $oldversion < 2009082800) {
2320 echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2322 // Get the system context so we can set the block instances to it
2323 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2325 // An array to contain the new block instances we will create
2326 $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
2327 // The new global navigation block instance as a stdClass
2328 $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
2329 $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
2330 $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutly everywhere
2331 $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
2332 $newblockinstances['globalnavigation']->subpagetypepattern = null;
2333 $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
2334 $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
2335 $newblockinstances['globalnavigation']->configdata = '';
2336 // The new settings navigation block instance as a stdClass
2337 $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
2338 $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
2339 $newblockinstances['settingsnavigation']->showinsubcontexts = true;
2340 $newblockinstances['settingsnavigation']->pagetypepattern = '*';
2341 $newblockinstances['settingsnavigation']->subpagetypepattern = null;
2342 $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
2343 $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
2344 $newblockinstances['settingsnavigation']->configdata = '';
2346 // Blocks that are outmoded and for whom the bells will toll... by which I
2347 // mean we will delete all instances of
2348 $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
2349 $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
2350 unset($outmodedblocks);
2351 // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
2352 // cut down the number of delete queries we will need to run
2353 $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
2355 $contextids = array();
2356 $instanceids = array();
2357 // Iterate through all block instances
2358 foreach ($allblockinstances as $blockinstance) {
2359 if (!in_array($blockinstance->parentcontextid, $contextids)) {
2360 $contextids[] = $blockinstance->parentcontextid;
2362 // If we have over 1000 contexts clean them up and reset the array
2363 // this ensures we don't hit any nasty memory limits or such
2364 if (count($contextids) > 1000) {
2365 $result = $result && upgrade_cleanup_unwanted_block_contexts($contextids);
2366 $contextids = array();
2369 if (!in_array($blockinstance->id, $instanceids)) {
2370 $instanceids[] = $blockinstance->id;
2371 // If we have more than 1000 block instances now remove all block positions
2372 // and empty the array
2373 if (count($contextids) > 1000) {
2374 $instanceidstring = join(',',$instanceids);
2375 $result = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2376 $instanceids = array();
2381 $result = $result && upgrade_cleanup_unwanted_block_contexts($contextids);
2383 $instanceidstring = join(',',$instanceids);
2384 $outcome1 = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2386 unset($allblockinstances);
2388 unset($instanceids);
2389 unset($instanceidstring);
2391 // Now remove the actual block instance
2392 $result = $result && $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
2393 unset($outmodedblocksstring);
2395 // Insert the new block instances. Remember they have not been installed yet
2396 // however this should not be a problem
2397 foreach ($newblockinstances as $blockinstance) {
2398 $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
2399 // Ensure the block context is created.
2400 get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
2402 unset($newblockinstances);
2404 upgrade_main_savepoint($result, 2009082800);
2405 // The end of the navigation upgrade
2408 if ($result && $oldversion < 2009090800){
2409 //insert new record for log_display table
2410 //used to record tag update.
2411 if (!$DB->record_exists('log_display', array('action'=>'update', 'module'=>'tag'))) {
2412 $log_action = new object();
2413 $log_action->module = 'tag';
2414 $log_action->action = 'update';
2415 $log_action->mtable = 'tag';
2416 $log_action->field = 'name';
2418 $result = $result && $DB->insert_record('log_display', $log_action);
2420 upgrade_main_savepoint($result, 2009090800);
2423 if ($result && $oldversion < 2009100601) {
2424 // drop all previous tables defined during the dev phase
2425 $dropold = array('external_services_users', 'external_services_functions', 'external_services', 'external_functions');
2426 foreach ($dropold as $tablename) {
2427 $table = new xmldb_table($tablename);
2428 if ($dbman->table_exists($table)) {
2429 $dbman->drop_table($table);
2432 upgrade_main_savepoint($result, 2009100601);
2435 if ($result && $oldversion < 2009100602) {
2436 /// Define table external_functions to be created
2437 $table = new xmldb_table('external_functions');
2439 /// Adding fields to table external_functions
2440 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2441 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2442 $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2443 $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2444 $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2445 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2447 /// Adding keys to table external_functions
2448 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2450 /// Adding indexes to table external_functions
2451 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2453 /// Launch create table for external_functions
2454 $dbman->create_table($table);
2456 /// Main savepoint reached
2457 upgrade_main_savepoint($result, 2009100602);
2460 if ($result && $oldversion < 2009100603) {
2461 /// Define table external_services to be created
2462 $table = new xmldb_table('external_services');
2464 /// Adding fields to table external_services
2465 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2466 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2467 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2468 $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
2469 $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2470 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
2472 /// Adding keys to table external_services
2473 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2475 /// Adding indexes to table external_services
2476 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2478 /// Launch create table for external_services
2479 $dbman->create_table($table);
2481 /// Main savepoint reached
2482 upgrade_main_savepoint($result, 2009100603);
2485 if ($result && $oldversion < 2009100604) {
2486 /// Define table external_services_functions to be created
2487 $table = new xmldb_table('external_services_functions');
2489 /// Adding fields to table external_services_functions
2490 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2491 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2492 $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2494 /// Adding keys to table external_services_functions
2495 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2496 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2498 /// Launch create table for external_services_functions
2499 $dbman->create_table($table);
2501 /// Main savepoint reached
2502 upgrade_main_savepoint($result, 2009100604);
2505 if ($result && $oldversion < 2009100605) {
2506 /// Define table external_services_users to be created
2507 $table = new xmldb_table('external_services_users');
2509 /// Adding fields to table external_services_users
2510 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2511 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2512 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2513 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2514 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2515 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2517 /// Adding keys to table external_services_users
2518 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2519 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2520 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2522 /// Launch create table for external_services_users
2523 $dbman->create_table($table);
2525 /// Main savepoint reached
2526 upgrade_main_savepoint($result, 2009100605);
2529 if ($result && $oldversion < 2009102600) {
2531 /// Define table external_tokens to be created
2532 $table = new xmldb_table('external_tokens');
2534 /// Adding fields to table external_tokens
2535 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2536 $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
2537 $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2538 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2539 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2540 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
2541 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2542 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2543 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2544 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2545 $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2547 /// Adding keys to table external_tokens
2548 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2549 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2550 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2551 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2553 /// Launch create table for external_tokens
2554 $dbman->create_table($table);
2556 /// Main savepoint reached
2557 upgrade_main_savepoint($result, 2009102600);
2560 if ($result && $oldversion < 2009103000) {
2562 /// Define table blog_association to be created
2563 $table = new xmldb_table('blog_association');
2565 /// Adding fields to table blog_association
2566 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2567 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2568 $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2570 /// Adding keys to table blog_association
2571 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2572 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2573 $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
2575 /// Conditionally launch create table for blog_association
2576 if (!$dbman->table_exists($table)) {
2577 $dbman->create_table($table);
2580 /// Define table blog_external to be created
2581 $table = new xmldb_table('blog_external');
2583 /// Adding fields to table blog_external
2584 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2585 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2586 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2587 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2588 $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2589 $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2590 $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2591 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2592 $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2594 /// Adding keys to table blog_external
2595 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2596 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2598 /// Conditionally launch create table for blog_external
2599 if ($dbman->table_exists($table)) {
2600 // Delete the existing one first (comes from early dev version)
2601 $dbman->drop_table($table);
2603 $dbman->create_table($table);
2605 // Print notice about need to upgrade bloglevel
2606 if (($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete)) {
2607 echo $OUTPUT->notification(get_string('bloglevelupgradenotice', 'admin'));
2609 // email admins about the need to upgrade their system using the admin/bloglevelupgrade.php script
2610 $admins = get_admins();
2614 $a->sitename = $site->fullname;
2615 $a->fixurl = "$CFG->wwwroot/$CFG->admin/bloglevelupgrade.php";
2617 $subject = get_string('bloglevelupgrade', 'admin');
2618 $description = get_string('bloglevelupgradedescription', 'admin', $a);
2620 // can not use messaging here because it is not configured yet!
2621 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2623 /// Main savepoint reached
2624 upgrade_main_savepoint($result, 2009103000);
2627 if ($result && $oldversion < 2009110400) {
2629 // An array used to store the table name and keys of summary and trust fields
2631 $extendtables = array();
2632 $extendtables['course'] = array('summaryformat');
2633 $extendtables['course_categories'] = array('descriptionformat');
2634 $extendtables['course_request'] = array('summaryformat');
2635 $extendtables['grade_outcomes'] = array('descriptionformat');
2636 $extendtables['groups'] = array('descriptionformat');
2637 $extendtables['groupings'] = array('descriptionformat');
2638 $extendtables['scale'] = array('descriptionformat');
2639 $extendtables['user'] = array('descriptionformat');
2640 $extendtables['user_info_field'] = array('descriptionformat', 'defaultdataformat');
2641 $extendtables['user_info_data'] = array('dataformat');
2643 foreach ($extendtables as $tablestr=>$newfields) {
2644 $table = new xmldb_table($tablestr);
2645 foreach ($newfields as $fieldstr) {
2646 $field = new xmldb_field($fieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2647 // Check that the field doesn't already exists
2648 if (!$dbman->field_exists($table, $field)) {
2649 // Add the new field
2650 $dbman->add_field($table, $field);
2651 // Update the field if the text contains the default FORMAT_MOODLE to FORMAT_HTML
2652 if (($pos = strpos($fieldstr, 'format'))>0) {
2653 upgrade_set_timeout(60*20); // this may take a little while
2654 $params = array(FORMAT_HTML, '<p%', '%<br />%', FORMAT_MOODLE);
2655 $textfield = substr($fieldstr, 0, $pos);
2656 $DB->execute('UPDATE {'.$tablestr.'} SET '.$fieldstr.'=? WHERE ('.$textfield.' LIKE ? OR '.$textfield.' LIKE ?) AND '.$fieldstr.'=?', $params);
2662 unset($extendtables);
2664 upgrade_main_savepoint($result, 2009110400);
2667 if ($result && $oldversion < 2009110605) {
2669 /// Define field timecreated to be added to external_services
2670 $table = new xmldb_table('external_services');
2671 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'component');
2673 /// Conditionally launch add field timecreated
2674 if (!$dbman->field_exists($table, $field)) {
2675 $dbman->add_field($table, $field);
2678 /// Define field timemodified to be added to external_services
2679 $table = new xmldb_table('external_services');
2680 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'timecreated');
2682 /// Conditionally launch add field timemodified
2683 if (!$dbman->field_exists($table, $field)) {
2684 $dbman->add_field($table, $field);
2687 /// Main savepoint reached
2688 upgrade_main_savepoint($result, 2009110605);
2691 if ($result && $oldversion < 2009111600) {
2693 /// Define field instance to be added to portfolio_tempdata
2694 $table = new xmldb_table('portfolio_tempdata');
2695 $field = new xmldb_field('instance', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'userid');
2697 /// Conditionally launch add field instance
2698 if (!$dbman->field_exists($table, $field)) {
2699 $dbman->add_field($table, $field);
2702 $key = new xmldb_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
2704 /// Launch add key instancefk
2705 $dbman->add_key($table, $key);
2707 /// Main savepoint reached
2708 upgrade_main_savepoint($result, 2009111600);
2711 if ($result && $oldversion < 2009111700) {
2713 /// Define field tempdataid to be added to portfolio_log
2714 $table = new xmldb_table('portfolio_log');
2715 $field = new xmldb_field('tempdataid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'caller_sha1');
2717 /// Conditionally launch add field tempdataid
2718 if (!$dbman->field_exists($table, $field)) {
2719 $dbman->add_field($table, $field);
2722 /// Main savepoint reached
2723 upgrade_main_savepoint($result, 2009111700);
2726 if ($result && $oldversion < 2009111701) {
2728 /// Define field returnurl to be added to portfolio_log
2729 $table = new xmldb_table('portfolio_log');
2730 $field = new xmldb_field('returnurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'tempdataid');
2732 /// Conditionally launch add field returnurl
2733 if (!$dbman->field_exists($table, $field)) {
2734 $dbman->add_field($table, $field);
2737 /// Main savepoint reached
2738 upgrade_main_savepoint($result, 2009111701);
2741 if ($result && $oldversion < 2009111702) {
2743 /// Define field continueurl to be added to portfolio_log
2744 $table = new xmldb_table('portfolio_log');
2745 $field = new xmldb_field('continueurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'returnurl');
2747 /// Conditionally launch add field continueurl
2748 if (!$dbman->field_exists($table, $field)) {
2749 $dbman->add_field($table, $field);
2752 /// Main savepoint reached
2753 upgrade_main_savepoint($result, 2009111702);
2756 if ($result && $oldversion < 2009112400) {
2757 if (empty($CFG->passwordsaltmain)) {
2758 $subject = get_string('check_passwordsaltmain_name', 'report_security');
2759 $description = get_string('check_passwordsaltmain_warning', 'report_security');;
2760 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2762 upgrade_main_savepoint($result, 2009112400);
2765 if ($result && $oldversion < 2010010601) {
2767 /// Define field creatorid to be added to external_tokens
2768 $table = new xmldb_table('external_tokens');
2769 $field = new xmldb_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'contextid');
2771 /// Conditionally launch add field creatorid
2772 if (!$dbman->field_exists($table, $field)) {
2773 $dbman->add_field($table, $field);
2776 /// Define key creatorid (foreign) to be added to external_tokens
2777 $table = new xmldb_table('external_tokens');
2778 $key = new xmldb_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
2780 /// Launch add key creatorid
2781 $dbman->add_key($table, $key);
2783 /// Main savepoint reached
2784 upgrade_main_savepoint($result, 2010010601);
2787 if ($result && $oldversion < 2010011200) {
2788 $table = new xmldb_table('grade_categories');
2789 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
2791 if (!$dbman->field_exists($table, $field)) {
2792 $dbman->add_field($table, $field);
2795 upgrade_main_savepoint($result, 2010011200);
2799 if ($result && $oldversion < 2010012500) {
2800 upgrade_fix_incorrect_mnethostids();
2801 upgrade_main_savepoint($result, 2010012500);
2804 if ($result && $oldversion < 2010012600) {
2805 // do stuff to the mnet table
2806 $table = new xmldb_table('mnet_rpc');
2808 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2809 $dbman->rename_field($table, $field, 'plugintype');
2811 $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2812 $dbman->rename_field($table, $field, 'pluginname');
2814 $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
2815 if (!$dbman->field_exists($table, $field)) {
2816 $dbman->add_field($table, $field);
2819 $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
2820 if (!$dbman->field_exists($table, $field)) {
2821 $dbman->add_field($table, $field);
2824 $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
2825 if (!$dbman->field_exists($table, $field)) {
2826 $dbman->add_field($table, $field);
2829 /// Main savepoint reached
2830 upgrade_main_savepoint($result, 2010012600);
2833 if ($result && $oldversion < 2010012900) {
2835 /// Define table mnet_remote_rpc to be created
2836 $table = new xmldb_table('mnet_remote_rpc');
2838 /// Adding fields to table mnet_remote_rpc
2839 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2840 $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2841 $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
2843 /// Adding keys to table mnet_remote_rpc
2844 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2846 /// Conditionally launch create table for mnet_remote_rpc
2847 if (!$dbman->table_exists($table)) {
2848 $dbman->create_table($table);
2852 /// Define table mnet_remote_service2rpc to be created
2853 $table = new xmldb_table('mnet_remote_service2rpc');
2855 /// Adding fields to table mnet_remote_service2rpc
2856 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2857 $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2858 $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2860 /// Adding keys to table mnet_remote_service2rpc
2861 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2863 /// Adding indexes to table mnet_remote_service2rpc
2864 $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2866 /// Conditionally launch create table for mnet_remote_service2rpc
2867 if (!$dbman->table_exists($table)) {
2868 $dbman->create_table($table);
2872 /// Rename field function_name on table mnet_rpc to functionname
2873 $table = new xmldb_table('mnet_rpc');
2874 $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
2876 /// Launch rename field function_name
2877 $dbman->rename_field($table, $field, 'functionname');
2880 /// Rename field xmlrpc_path on table mnet_rpc to xmlrpcpath
2881 $table = new xmldb_table('mnet_rpc');
2882 $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
2884 /// Launch rename field xmlrpc_path
2885 $dbman->rename_field($table, $field, 'xmlrpcpath');
2888 /// Main savepoint reached
2889 upgrade_main_savepoint($result, 2010012900);
2892 if ($result && $oldversion < 2010012901) {
2894 /// Define field plugintype to be added to mnet_remote_rpc
2895 $table = new xmldb_table('mnet_remote_rpc');
2896 $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
2898 /// Conditionally launch add field plugintype
2899 if (!$dbman->field_exists($table, $field)) {
2900 $dbman->add_field($table, $field);
2903 /// Define field pluginname to be added to mnet_remote_rpc
2904 $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
2906 /// Conditionally launch add field pluginname
2907 if (!$dbman->field_exists($table, $field)) {
2908 $dbman->add_field($table, $field);
2911 /// Main savepoint reached
2912 upgrade_main_savepoint($result, 2010012901);
2915 if ($result && $oldversion < 2010012902) {
2917 /// Define field enabled to be added to mnet_remote_rpc
2918 $table = new xmldb_table('mnet_remote_rpc');
2919 $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
2921 /// Conditionally launch add field enabled
2922 if (!$dbman->field_exists($table, $field)) {
2923 $dbman->add_field($table, $field);
2926 /// Main savepoint reached
2927 upgrade_main_savepoint($result, 2010012902);
2930 /// MDL-17863. Increase the portno column length on mnet_host to handle any port number
2931 if ($result && $oldversion < 2010020100) {
2932 /// Changing precision of field portno on table mnet_host to (5)
2933 $table = new xmldb_table('mnet_host');
2934 $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
2936 /// Launch change of precision for field portno
2937 $dbman->change_field_precision($table, $field);
2939 upgrade_main_savepoint($result, 2010020100);
2942 if ($result && $oldversion < 2010020300) {
2944 /// Define field timecreated to be added to user
2945 $table = new xmldb_table('user');
2946 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
2948 if (!$dbman->field_exists($table, $field)) {
2949 /// Launch add field timecreated
2950 $dbman->add_field($table, $field);
2952 $DB->execute("UPDATE {user} SET timecreated = firstaccess");
2954 $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
2957 upgrade_main_savepoint($result, 2010020300);
2960 // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
2961 if ($result && $oldversion < 2010020301) {
2962 if ($preamble = $CFG->filter_tex_latexpreamble) {
2963 $preamble = preg_replace('/^ +/m', '', $preamble);
2964 set_config('filter_tex_latexpreamble', $preamble);
2966 upgrade_main_savepoint($result, 2010020301);
2969 if ($result && $oldversion < 2010021400) {
2970 /// Changes to modinfo mean we need to rebuild course cache
2971 require_once($CFG->dirroot . '/course/lib.php');
2972 rebuild_course_cache(0, true);
2973 upgrade_main_savepoint($result, 2010021400);
2976 if ($result && $oldversion < 2010021800) {
2977 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
2978 upgrade_main_savepoint($result, 2010021800);
2981 if ($result && $oldversion < 2010031900) {
2982 // regeneration of sessions is always enabled, no need for this setting any more
2983 unset_config('regenloginsession');
2984 upgrade_main_savepoint($result, 2010031900);
2988 if ($result && $oldversion < 2010032400) {
2989 // Upgrade all of those using the standardold theme to the use the standard
2991 if ($CFG->theme == 'standardold') {
2992 // The config setting that affects the whole site
2993 set_config('theme', 'standard');
2995 // Course Categories
2996 $DB->execute('UPDATE {course_categories} SET theme=? WHERE theme=?', array('standard', 'standardold'));
2998 $DB->execute('UPDATE {course} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3000 $DB->execute('UPDATE {user} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3001 upgrade_main_savepoint($result, 2010032400);
3004 if ($result && $oldversion < 2010033101.01) {
3006 /// Define field source to be added to files
3007 $table = new xmldb_table('files');
3009 $field = new xmldb_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'status');
3011 /// Conditionally launch add field source
3012 if (!$dbman->field_exists($table, $field)) {
3013 $dbman->add_field($table, $field);
3016 $field = new xmldb_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'source');
3018 /// Conditionally launch add field author
3019 if (!$dbman->field_exists($table, $field)) {
3020 $dbman->add_field($table, $field);
3023 $field = new xmldb_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'author');
3025 /// Conditionally launch add field license
3026 if (!$dbman->field_exists($table, $field)) {
3027 $dbman->add_field($table, $field);
3030 upgrade_main_savepoint($result, 2010033101.01);
3033 if ($result && $oldversion < 2010033101.02) {
3035 /// Define table license to be created
3036 $table = new xmldb_table('license');
3038 /// Adding fields to table license
3039 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3040 $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3041 $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
3042 $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3043 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
3044 $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
3046 /// Adding keys to table license
3047 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3049 /// Conditionally launch create table for license
3050 if (!$dbman->table_exists($table)) {
3051 $dbman->create_table($table);
3053 $active_licenses = array();
3055 $license = new stdclass;
3057 // add unknown license
3058 $license->shortname = 'unknown';
3059 $license->fullname = 'Unknown license';
3060 $license->source = '';
3061 $license->enabled = 1;
3062 $license->version = '2010033100';
3063 $active_licenses[] = $license->shortname;
3064 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3065 if ($record->version < $license->version) {
3066 // update license record
3067 $license->enabled = $record->enabled;
3068 $license->id = $record->id;
3069 $DB->update_record('license', $license);
3072 $DB->insert_record('license', $license);
3075 // add all rights reserved license
3076 $license->shortname = 'allrightsreserved';
3077 $license->fullname = 'All rights reserved';
3078 $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
3079 $license->enabled = 1;
3080 $license->version = '2010033100';
3081 $active_licenses[] = $license->shortname;
3082 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3083 if ($record->version < $license->version) {
3084 // update license record
3085 $license->id = $record->id;
3086 $license->enabled = $record->enabled;
3087 $DB->update_record('license', $license);
3090 $DB->insert_record('license', $license);
3093 // add public domain license
3094 $license->shortname = 'public';
3095 $license->fullname = 'Public Domain';
3096 $license->source = 'http://creativecommons.org/licenses/publicdomain/';
3097 $license->enabled = 1;
3098 $license->version = '2010033100';
3099 $active_licenses[] = $license->shortname;
3100 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3101 if ($record->version < $license->version) {
3102 // update license record
3103 $license->enabled = $record->enabled;
3104 $license->id = $record->id;
3105 $DB->update_record('license', $license);
3108 $DB->insert_record('license', $license);
3111 // add creative commons license
3112 $license->shortname = 'cc';
3113 $license->fullname = 'Creative Commons';
3114 $license->source = 'http://creativecommons.org/licenses/by/3.0/';
3115 $license->enabled = 1;
3116 $license->version = '2010033100';
3117 $active_licenses[] = $license->shortname;
3118 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3119 if ($record->version < $license->version) {
3120 // update license record
3121 $license->enabled = $record->enabled;
3122 $license->id = $record->id;
3123 $DB->update_record('license', $license);
3126 $DB->insert_record('license', $license);
3129 // add creative commons no derivs license
3130 $license->shortname = 'cc-nd';
3131 $license->fullname = 'Creative Commons - NoDerivs';
3132 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3133 $license->enabled = 1;
3134 $license->version = '2010033100';
3135 $active_licenses[] = $license->shortname;
3136 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3137 if ($record->version < $license->version) {
3138 // update license record
3139 $license->enabled = $record->enabled;
3140 $license->id = $record->id;
3141 $DB->update_record('license', $license);
3144 $DB->insert_record('license', $license);
3147 // add creative commons no commercial no derivs license
3148 $license->shortname = 'cc-nc-nd';
3149 $license->fullname = 'Creative Commons - No Commercial NoDerivs';
3150 $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
3151 $license->enabled = 1;
3152 $license->version = '2010033100';
3153 $active_licenses[] = $license->shortname;
3154 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3155 if ($record->version < $license->version) {
3156 // update license record
3157 $license->enabled = $record->enabled;
3158 $license->id = $record->id;
3159 $DB->update_record('license', $license);
3162 $DB->insert_record('license', $license);
3165 // add creative commons no commercial
3166 $license->shortname = 'cc-nc-nd';
3167 $license->shortname = 'cc-nc';
3168 $license->fullname = 'Creative Commons - No Commercial';
3169 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3170 $license->enabled = 1;
3171 $license->version = '2010033100';
3172 $active_licenses[] = $license->shortname;
3173 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3174 if ($record->version < $license->version) {
3175 // update license record
3176 $license->enabled = $record->enabled;
3177 $license->id = $record->id;
3178 $DB->update_record('license', $license);
3181 $DB->insert_record('license', $license);
3184 // add creative commons no commercial sharealike
3185 $license->shortname = 'cc-nc-sa';
3186 $license->fullname = 'Creative Commons - No Commercial ShareAlike';
3187 $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
3188 $license->enabled = 1;
3189 $license->version = '2010033100';
3190 $active_licenses[] = $license->shortname;
3191 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3192 if ($record->version < $license->version) {
3193 // update license record
3194 $license->enabled = $record->enabled;
3195 $license->id = $record->id;
3196 $DB->update_record('license', $license);
3199 $DB->insert_record('license', $license);
3202 // add creative commons sharealike
3203 $license->shortname = 'cc-sa';
3204 $license->fullname = 'Creative Commons - ShareAlike';
3205 $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
3206 $license->enabled = 1;
3207 $license->version = '2010033100';
3208 $active_licenses[] = $license->shortname;
3209 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3210 if ($record->version < $license->version) {
3211 // update license record
3212 $license->enabled = $record->enabled;
3213 $license->id = $record->id;
3214 $DB->update_record('license', $license);
3217 $DB->insert_record('license', $license);
3220 set_config('licenses', implode(',', $active_licenses));
3221 /// set site default license
3222 set_config('sitedefaultlicense', 'allrightsreserved');
3224 /// Main savepoint reached
3225 upgrade_main_savepoint($result, 2010033101.02);
3228 if ($result && $oldversion < 2010033102.00) {
3229 // rename course view capability to participate
3230 $params = array('view'=>'moodle/course:view', 'participate'=>'moodle/course:participate');
3231 $sql = "UPDATE {role_capabilities} SET capability = :participate WHERE capability = :view";
3232 $DB->execute($sql, $params);
3233 $sql = "UPDATE {capabilities} SET name = :participate WHERE name = :view";
3234 $DB->execute($sql, $params);
3235 // note: the view capability is readded again at the end of upgrade, but with different meaning
3236 upgrade_main_savepoint($result, 2010033102.00);
3239 if ($result && $oldversion < 2010033102.01) {
3240 // Define field archetype to be added to role table
3241 $table = new xmldb_table('role');
3242 $field = new xmldb_field('archetype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'sortorder');
3243 $dbman->add_field($table, $field);
3244 upgrade_main_savepoint($result, 2010033102.01);
3247 if ($result && $oldversion < 2010033102.02) {
3248 // Set archetype for existing roles and change admin role to manager role
3249 $sql = "SELECT r.*, rc.capability
3251 JOIN {role_capabilities} rc ON rc.roleid = r.id
3252 WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
3254 $params = array('syscontextid'=>SYSCONTEXTID, 'legacycaps'=>'moodle/legacy:%');
3255 $substart = strlen('moodle/legacy:');
3256 $roles = $DB->get_recordset_sql($sql, $params); // in theory could be multiple legacy flags in one role
3257 foreach ($roles as $role) {
3258 $role->archetype = substr($role->capability, $substart);
3259 unset($role->capability);
3260 if ($role->archetype === 'admin') {
3261 $role->archetype = 'manager';
3262 if ($role->shortname === 'admin') {
3263 $role->shortname = 'manager';
3264 $role->name = get_string('manager', 'role');
3265 $role->description = get_string('managerdescription', 'role');
3268 $DB->update_record('role', $role);
3272 upgrade_main_savepoint($result, 2010033102.02);
3275 if ($result && $oldversion < 2010033102.03) {
3276 // Now pick site admins (===have manager role assigned at the system context)
3277 // and store them in the new $CFG->siteadmins setting as comma separated list
3278 $sql = "SELECT ra.id, ra.userid
3279 FROM {role_assignments} ra
3280 JOIN {role} r ON r.id = ra.roleid
3281 JOIN {user} u ON u.id = ra.userid
3282 WHERE ra.contextid = :syscontext AND r.archetype = 'manager' AND u.deleted = 0
3284 $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
3286 foreach ($ras as $ra) {
3287 $admins[$ra->userid] = $ra->userid;
3288 set_config('siteadmins', implode(',', $admins)); // better to save it repeatedly, we do need at least one admin
3289 $DB->delete_records('role_assignments', array('id'=>$ra->id));
3292 upgrade_main_savepoint($result, 2010033102.03);
3295 if ($result && $oldversion < 2010033102.04) {
3296 // clean up the manager roles
3297 $managers = $DB->get_records('role', array('archetype'=>'manager'));
3298 foreach ($managers as $manager) {
3299 // now sanitize the capabilities and overrides
3300 $DB->delete_records('role_capabilities', array('capability'=>'moodle/site:config', 'roleid'=>$manager->id)); // only site admins may configure servers
3301 // note: doanything and legacy caps are deleted automatically, they get moodle/course:view later at the end of the upgrade
3303 // set usable contexts
3304 $DB->delete_records('role_context_levels', array('roleid'=>$manager->id));
3305 $assignlevels = array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE);
3306 foreach ($assignlevels as $assignlevel) {
3307 $record = (object)array('roleid'=>$manager->id, 'contextlevel'=>$assignlevel);
3308 $DB->insert_record('role_context_levels', $record);
3311 // remove manager role assignments bellow the course context level - admin role was never intended for activities and blocks,
3312 // the problem is that those assignments would not be visible after upgrade and old style admins in activities make no sense anyway
3313 $DB->delete_records_select('role_assignments', "roleid = :manager AND contextid IN (SELECT id FROM {context} WHERE contextlevel > 50)", array('manager'=>$manager->id));
3315 // allow them to assign all roles except default user, guest and frontpage - users get these roles automatically on the fly when needed
3316 $DB->delete_records('role_allow_assign', array('roleid'=>$manager->id));
3317 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype <> 'user' AND archetype <> 'guest' AND archetype <> 'frontpage'");
3318 foreach ($roles as $role) {
3319 $record = (object)array('roleid'=>$manager->id, 'allowassign'=>$role->id);
3320 $DB->insert_record('role_allow_assign', $record);
3323 // allow them to override all roles
3324 $DB->delete_records('role_allow_override', array('roleid'=>$manager->id));
3325 $roles = $DB->get_records_sql("SELECT * FROM {role}");
3326 foreach ($roles as $role) {
3327 $record = (object)array('roleid'=>$manager->id, 'allowoverride'=>$role->id);
3328 $DB->insert_record('role_allow_override', $record);
3331 // allow them to switch to all following roles
3332 $DB->delete_records('role_allow_switch', array('roleid'=>$manager->id));
3333 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype IN ('student', 'teacher', 'editingteacher')");
3334 foreach ($roles as $role) {
3335 $record = (object)array('roleid'=>$manager->id, 'allowswitch'=>$role->id);
3336 $DB->insert_record('role_allow_switch', $record);
3340 upgrade_main_savepoint($result, 2010033102.04);
3343 if ($result && $oldversion < 2010033102.05) {
3344 // remove course:view from all roles that are not used for enrolment, it does NOT belong there because it really means user is enrolled!
3345 $noenrolroles = $DB->get_records_select('role', "archetype IN ('guest', 'user', 'manager', 'coursecreator', 'frontpage')");
3346 foreach ($noenrolroles as $role) {
3347 $DB->delete_records('role_capabilities', array('roleid'=>$role->id, 'capability'=>'moodle/course:participate'));
3349 upgrade_main_savepoint($result, 2010033102.05);
3352 if ($result && $oldversion < 2010033102.06) {
3353 // make sure there is nothing weird in default user role
3354 if (!empty($CFG->defaultuserroleid)) {
3355 if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
3356 if ($role->archetype !== '' and $role->archetype !== 'user') {
3357 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default authenticated user role (defaultuserroleid) value is invalid, setting cleared.');
3358 unset_config('defaultuserroleid');
3361 unset_config('defaultuserroleid');
3364 upgrade_main_savepoint($result, 2010033102.06);
3367 if ($result && $oldversion < 2010033102.07) {
3368 if (!empty($CFG->displayloginfailures) and $CFG->displayloginfailures === 'teacher') {
3369 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Displaying of login failuters to teachers is not supported any more.');
3370 unset_config('displayloginfailures');
3372 upgrade_main_savepoint($result, 2010033102.07);
3375 if ($result && $oldversion < 2010033102.08) {
3376 // make sure there are no problems in default guest role settings
3377 if (!empty($CFG->guestroleid)) {
3378 if ($role = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
3379 if ($role->archetype !== '' and $role->archetype !== 'guest') {
3380 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default guest role (guestroleid) value is invalid, setting cleared.');
3381 unset_config('guestroleid');
3384 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) doeas not exist, setting cleared.');
3385 unset_config('guestroleid');
3388 // remove all roles of the guest account - the only way to change it is to override the guest role, sorry
3389 // the guest account gets all the role assignemnts on the fly whcih works fine in has_capability(),
3390 $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE username = 'guest')");
3392 upgrade_main_savepoint($result, 2010033102.08);
3395 if ($result && $oldversion < 2010033102.09) {
3396 // For MDL-17501. Ensure that any role that has moodle/course:update also has moodle/course:visibility.
3397 // Get the roles with 'moodle/course:update'.
3398 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
3399 $roles = get_roles_with_capability('moodle/course:update', CAP_ALLOW, $systemcontext);
3401 // Give those roles 'moodle/course:visibility'.
3402 foreach ($roles as $role) {
3403 assign_capability('moodle/course:visibility', CAP_ALLOW, $role->id, $systemcontext->id);
3406 // Force all sessions to refresh access data.
3407 mark_context_dirty($systemcontext->path);
3409 // Main savepoint reached
3410 upgrade_main_savepoint($result, 2010033102.09);
3413 if ($result && $oldversion < 2010040700) {
3414 // migrate old groupings --> groupmembersonly setting
3415 if (isset($CFG->enablegroupings)) {
3416 set_config('enablegroupmembersonly', $CFG->enablegroupings);
3417 unset_config('enablegroupings');
3420 // Main savepoint reached
3421 upgrade_main_savepoint($result, 2010040700);
3424 if ($result && $oldversion < 2010040900) {
3426 // Changing the default of field lang on table user to good old "en"
3427 $table = new xmldb_table('user');
3428 $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en', 'country');
3430 // Launch change of default for field lang
3431 $dbman->change_field_default($table, $field);
3433 // update main site lang
3434 if (strpos($CFG->lang, '_utf8') !== false) {
3435 $lang = str_replace('_utf8', '', $CFG->lang);
3436 set_config('lang', $lang);
3440 if (!empty($CFG->langlist)) {
3441 $langs = explode(',', $CFG->langlist);
3442 foreach ($langs as $key=>$lang) {
3443 $lang = str_replace('_utf8', '', $lang);
3444 $langs[$key] = $lang;
3446 set_config('langlist', implode(',', $langs));
3449 // Main savepoint reached
3450 upgrade_main_savepoint($result, 2010040900);
3453 if ($result && $oldversion < 2010040901) {
3455 // Remove "_utf8" suffix from all langs in user table
3456 $langs = $DB->get_records_sql("SELECT DISTINCT lang FROM {user} WHERE lang LIKE ?", array('%_utf8'));
3458 foreach ($langs as $lang=>$unused) {
3459 $newlang = str_replace('_utf8', '', $lang);
3460 $sql = "UPDATE {user} SET lang = :newlang WHERE lang = :lang";
3461 $DB->execute($sql, array('newlang'=>$newlang, 'lang'=>$lang));
3464 // Main savepoint reached
3465 upgrade_main_savepoint($result, 2010040901);
3468 if ($result && $oldversion < 2010041301) {
3469 $sql = "UPDATE {block} SET name=? WHERE name=?";
3470 $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3471 $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3473 $sql = "UPDATE {block_instances} SET blockname=? WHERE blockname=?";
3474 $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3475 $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3476 upgrade_main_savepoint($result, 2010041301);
3479 if ($result && $oldversion < 2010042100) {
3481 /// Define table backup_controllers to be created
3482 $table = new xmldb_table('backup_controllers');
3484 /// Adding fields to table backup_controllers
3485 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3486 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3487 $table->add_field('type', XMLDB_TYPE_CHAR, '6', null, XMLDB_NOTNULL, null, null);
3488 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3489 $table->add_field('format', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
3490 $table->add_field('interactive', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3491 $table->add_field('purpose', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3492 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3493 $table->add_field('status', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3494 $table->add_field('execution', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3495 $table->add_field('executiontime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3496 $table->add_field('checksum', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3497 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3498 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3499 $table->add_field('controller', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
3501 /// Adding keys to table backup_controllers
3502 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3503 $table->add_key('backupid_uk', XMLDB_KEY_UNIQUE, array('backupid'));
3504 $table->add_key('userid_fk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3506 /// Adding indexes to table backup_controllers
3507 $table->add_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3509 /// Conditionally launch create table for backup_controllers
3510 if (!$dbman->table_exists($table)) {
3511 $dbman->create_table($table);
3514 /// Define table backup_ids_template to be created
3515 $table = new xmldb_table('backup_ids_template');
3517 /// Adding fields to table backup_ids_template
3518 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3519 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3520 $table->add_field('itemname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
3521 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3522 $table->add_field('parentitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3524 /// Adding keys to table backup_ids_template
3525 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3526 $table->add_key('backupid_itemname_itemid_uk', XMLDB_KEY_UNIQUE, array('backupid', 'itemname', 'itemid'));
3528 /// Adding indexes to table backup_ids_template
3529 $table->add_index('backupid_parentitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'parentitemid'));
3531 /// Conditionally launch create table for backup_controllers
3532 if (!$dbman->table_exists($table)) {
3533 $dbman->create_table($table);
3536 /// Main savepoint reached
3537 upgrade_main_savepoint($result, 2010042100);
3540 if ($result && $oldversion < 2010042301) {
3542 $table = new xmldb_table('course_sections');
3543 $field = new xmldb_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'section');
3545 if (!$dbman->field_exists($table, $field)) {
3546 $dbman->add_field($table, $field);
3549 upgrade_main_savepoint($result, 2010042301);
3552 if ($result && $oldversion < 2010042302) {
3553 // Define table cohort to be created
3554 $table = new xmldb_table('cohort');
3556 // Adding fields to table cohort
3557 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3558 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3559 $table->add_field('name', XMLDB_TYPE_CHAR, '254', null, XMLDB_NOTNULL, null, null);
3560 $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3561 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
3562 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3563 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3564 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3565 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3567 // Adding keys to table cohort
3568 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3569 $table->add_key('context', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3571 // Conditionally launch create table for cohort
3572 if (!$dbman->table_exists($table)) {
3573 $dbman->create_table($table);
3576 upgrade_main_savepoint($result, 2010042302);
3579 if ($result && $oldversion < 2010042303) {
3580 // Define table cohort_members to be created
3581 $table = new xmldb_table('cohort_members');
3583 // Adding fields to table cohort_members
3584 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3585 $table->add_field('cohortid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3586 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3587 $table->add_field('timeadded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3589 // Adding keys to table cohort_members
3590 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3591 $table->add_key('cohortid', XMLDB_KEY_FOREIGN, array('cohortid'), 'cohort', array('id'));
3592 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3594 // Adding indexes to table cohort_members
3595 $table->add_index('cohortid-userid', XMLDB_INDEX_UNIQUE, array('cohortid', 'userid'));
3597 // Conditionally launch create table for cohort_members
3598 if (!$dbman->table_exists($table)) {
3599 $dbman->create_table($table);
3602 // Main savepoint reached
3603 upgrade_main_savepoint($result, 2010042303);
3606 if ($result && $oldversion < 2010042800) {
3607 //drop the previously created ratings table
3608 $table = new xmldb_table('ratings');
3609 if ($dbman->table_exists($table)) {
3610 $dbman->drop_table($table);
3613 //create the rating table (replaces module specific rating implementations)
3614 $table = new xmldb_table('rating');
3615 if ($dbman->table_exists($table)) {
3616 $dbman->drop_table($table);
3619 /// Adding fields to table rating
3620 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3621 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3623 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3624 $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
3625 $table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3626 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3628 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3629 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3631 /// Adding keys to table rating
3632 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3633 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3634 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3636 /// Adding indexes to table rating
3637 $table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
3639 /// Create table for ratings
3640 if (!$dbman->table_exists($table)) {
3641 $dbman->create_table($table);
3644 upgrade_main_savepoint($result, 2010042800);
3647 if ($result && $oldversion < 2010042801) {
3648 // migrating old comments block content
3650 UPDATE {comments} SET contextid = (
3651 SELECT parentcontextid FROM {block_instances}
3652 WHERE id = {comments}.itemid AND blockname = 'comments'
3654 commentarea = 'page_comments',
3656 WHERE commentarea = 'block_comments'
3658 AND EXISTS (SELECT 'x'
3659 FROM {block_instances}
3660 WHERE id = {comments}.itemid
3661 AND blockname = 'comments')");
3663 // remove all orphaned record
3664 $DB->delete_records('comments', array('commentarea'=>'block_comments'));
3665 upgrade_main_savepoint($result, 2010042801);
3671 //TODO: Cleanup before the 2.0 release - we do not want to drag along these dev machine fixes forever
3672 // 1/ remove the automatic enabling of completion lib if debug enabled ( in 2008121701 block)
3673 // 2/ move 2009061300 block to the top of the file so that we may log upgrade queries
3674 // 3/ remove 2010033101 block
3675 // 4/ remove 2010032400 block