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 if ($dbman->index_exists($table, $index)) {
105 $dbman->drop_index($table, $index);
108 /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
109 $table = new xmldb_table('grade_letters');
110 $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
112 /// Launch add index contextid-lowerboundary-letter
113 $dbman->add_index($table, $index);
115 /// Main savepoint reached
116 upgrade_main_savepoint($result, 2008030700);
119 if ($result && $oldversion < 2008050100) {
120 // Update courses that used weekscss to weeks
121 $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
122 upgrade_main_savepoint($result, 2008050100);
125 if ($result && $oldversion < 2008050200) {
126 // remove unused config options
127 unset_config('statsrolesupgraded');
128 upgrade_main_savepoint($result, 2008050200);
131 if ($result && $oldversion < 2008050700) {
132 upgrade_set_timeout(60*20); // this may take a while
134 /// Fix minor problem caused by MDL-5482.
135 require_once($CFG->dirroot . '/question/upgrade.php');
136 question_fix_random_question_parents();
137 upgrade_main_savepoint($result, 2008050700);
140 if ($result && $oldversion < 2008051201) {
141 echo $OUTPUT->notification('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
142 upgrade_set_timeout(60*20); // this may take a while
144 /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
145 $dbfamily = $DB->get_dbfamily();
146 if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
147 $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
150 /// Define index idnumber (not unique) to be dropped form user
151 $table = new xmldb_table('user');
152 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
154 /// Launch drop index idnumber
155 if ($dbman->index_exists($table, $index)) {
156 $dbman->drop_index($table, $index);
159 /// Changing precision of field idnumber on table user to (255)
160 $table = new xmldb_table('user');
161 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
163 /// Launch change of precision for field idnumber
164 $dbman->change_field_precision($table, $field);
166 /// Launch add index idnumber again
167 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
168 $dbman->add_index($table, $index);
170 /// Main savepoint reached
171 upgrade_main_savepoint($result, 2008051201);
174 if ($result && $oldversion < 2008051202) {
175 $log_action = new object();
176 $log_action->module = 'course';
177 $log_action->action = 'unenrol';
178 $log_action->mtable = 'course';
179 $log_action->field = 'fullname';
180 if (!$DB->record_exists('log_display', array('action'=>'unenrol', 'module'=>'course'))) {
181 $DB->insert_record('log_display', $log_action);
183 upgrade_main_savepoint($result, 2008051202);
186 if ($result && $oldversion < 2008051203) {
187 $table = new xmldb_table('mnet_enrol_course');
188 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
189 $dbman->change_field_precision($table, $field);
190 upgrade_main_savepoint($result, 2008051203);
193 if ($result && $oldversion < 2008063001) {
194 upgrade_set_timeout(60*20); // this may take a while
196 // table to be modified
197 $table = new xmldb_table('tag_instance');
199 $field = new xmldb_field('tiuserid');
200 if (!$dbman->field_exists($table, $field)) {
201 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
202 $dbman->add_field($table, $field);
205 $index = new xmldb_index('itemtype-itemid-tagid');
206 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
207 if ($dbman->index_exists($table, $index)) {
208 $dbman->drop_index($table, $index);
210 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
211 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
212 if (!$dbman->index_exists($table, $index)) {
213 $dbman->add_index($table, $index);
216 /// Main savepoint reached
217 upgrade_main_savepoint($result, 2008063001);
220 if ($result && $oldversion < 2008070300) {
221 $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
222 upgrade_main_savepoint($result, 2008070300);
225 if ($result && $oldversion < 2008070701) {
227 /// Define table portfolio_instance to be created
228 $table = new xmldb_table('portfolio_instance');
230 /// Adding fields to table portfolio_instance
231 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
232 $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
233 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
234 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
236 /// Adding keys to table portfolio_instance
237 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
239 /// Conditionally launch create table for portfolio_instance
240 if (!$dbman->table_exists($table)) {
241 $dbman->create_table($table);
243 /// Define table portfolio_instance_config to be created
244 $table = new xmldb_table('portfolio_instance_config');
246 /// Adding fields to table portfolio_instance_config
247 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
248 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
249 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
250 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
252 /// Adding keys to table portfolio_instance_config
253 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
254 $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
256 /// Adding indexes to table portfolio_instance_config
257 $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
259 /// Conditionally launch create table for portfolio_instance_config
260 if (!$dbman->table_exists($table)) {
261 $dbman->create_table($table);
264 /// Define table portfolio_instance_user to be created
265 $table = new xmldb_table('portfolio_instance_user');
267 /// Adding fields to table portfolio_instance_user
268 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
269 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
270 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
271 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
272 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
274 /// Adding keys to table portfolio_instance_user
275 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
276 $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
277 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
279 /// Conditionally launch create table for portfolio_instance_user
280 if (!$dbman->table_exists($table)) {
281 $dbman->create_table($table);
284 /// Main savepoint reached
285 upgrade_main_savepoint($result, 2008070701);
288 if ($result && $oldversion < 2008072400) {
289 /// Create the database tables for message_processors
290 $table = new xmldb_table('message_processors');
291 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
292 $table->add_field('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null);
293 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
294 $dbman->create_table($table);
296 /// delete old and create new fields
297 $table = new xmldb_table('message');
298 $field = new xmldb_field('messagetype');
299 $dbman->drop_field($table, $field);
302 $field = new xmldb_field('message');
303 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
304 $dbman->rename_field($table, $field, 'fullmessage');
305 $field = new xmldb_field('format');
306 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
307 $dbman->rename_field($table, $field, 'fullmessageformat');
309 /// new message fields
310 $field = new xmldb_field('subject');
311 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
312 $dbman->add_field($table, $field);
313 $field = new xmldb_field('fullmessagehtml');
314 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
315 $dbman->add_field($table, $field);
316 $field = new xmldb_field('smallmessage');
317 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
318 $dbman->add_field($table, $field);
321 $table = new xmldb_table('message_read');
322 $field = new xmldb_field('messagetype');
323 $dbman->drop_field($table, $field);
324 $field = new xmldb_field('mailed');
325 $dbman->drop_field($table, $field);
328 $field = new xmldb_field('message');
329 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
330 $dbman->rename_field($table, $field, 'fullmessage');
331 $field = new xmldb_field('format');
332 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
333 $dbman->rename_field($table, $field, 'fullmessageformat');
336 /// new message fields
337 $field = new xmldb_field('subject');
338 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
339 $dbman->add_field($table, $field);
340 $field = new xmldb_field('fullmessagehtml');
341 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
342 $dbman->add_field($table, $field);
343 $field = new xmldb_field('smallmessage');
344 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
345 $dbman->add_field($table, $field);
348 $table = new xmldb_table('message_working');
349 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
350 $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
351 $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
352 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
353 $dbman->create_table($table);
356 upgrade_main_savepoint($result, 2008072400);
359 if ($result && $oldversion < 2008072800) {
361 /// Define field enablecompletion to be added to course
362 $table = new xmldb_table('course');
363 $field = new xmldb_field('enablecompletion');
364 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
366 /// Launch add field enablecompletion
367 if (!$dbman->field_exists($table,$field)) {
368 $dbman->add_field($table, $field);
371 /// Define field completion to be added to course_modules
372 $table = new xmldb_table('course_modules');
373 $field = new xmldb_field('completion');
374 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'groupmembersonly');
376 /// Launch add field completion
377 if (!$dbman->field_exists($table,$field)) {
378 $dbman->add_field($table, $field);
381 /// Define field completiongradeitemnumber to be added to course_modules
382 $field = new xmldb_field('completiongradeitemnumber');
383 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'completion');
385 /// Launch add field completiongradeitemnumber
386 if (!$dbman->field_exists($table,$field)) {
387 $dbman->add_field($table, $field);
390 /// Define field completionview to be added to course_modules
391 $field = new xmldb_field('completionview');
392 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiongradeitemnumber');
394 /// Launch add field completionview
395 if (!$dbman->field_exists($table,$field)) {
396 $dbman->add_field($table, $field);
399 /// Define field completionexpected to be added to course_modules
400 $field = new xmldb_field('completionexpected');
401 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionview');
403 /// Launch add field completionexpected
404 if (!$dbman->field_exists($table,$field)) {
405 $dbman->add_field($table, $field);
408 /// Define table course_modules_completion to be created
409 $table = new xmldb_table('course_modules_completion');
410 if (!$dbman->table_exists($table)) {
412 /// Adding fields to table course_modules_completion
413 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
414 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
415 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
416 $table->add_field('completionstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
417 $table->add_field('viewed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
418 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
420 /// Adding keys to table course_modules_completion
421 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
423 /// Adding indexes to table course_modules_completion
424 $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
425 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
427 /// Launch create table for course_modules_completion
428 $dbman->create_table($table);
431 /// Main savepoint reached
432 upgrade_main_savepoint($result, 2008072800);
435 if ($result && $oldversion < 2008073000) {
437 /// Define table portfolio_log to be created
438 $table = new xmldb_table('portfolio_log');
440 /// Adding fields to table portfolio_log
441 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
442 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
443 $table->add_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
444 $table->add_field('portfolio', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
445 $table->add_field('caller_class', XMLDB_TYPE_CHAR, '150', null, XMLDB_NOTNULL, null, null);
446 $table->add_field('caller_file', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
447 $table->add_field('caller_sha1', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
449 /// Adding keys to table portfolio_log
450 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
451 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
452 $table->add_key('portfoliofk', XMLDB_KEY_FOREIGN, array('portfolio'), 'portfolio_instance', array('id'));
454 /// Conditionally launch create table for portfolio_log
455 if (!$dbman->table_exists($table)) {
456 $dbman->create_table($table);
459 /// Main savepoint reached
460 upgrade_main_savepoint($result, 2008073000);
463 if ($result && $oldversion < 2008073104) {
464 /// Drop old table that might exist for some people
465 $table = new xmldb_table('message_providers');
466 if ($dbman->table_exists($table)) {
467 $dbman->drop_table($table);
470 /// Define table message_providers to be created
471 $table = new xmldb_table('message_providers');
473 /// Adding fields to table message_providers
474 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
475 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
476 $table->add_field('component', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
477 $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
479 /// Adding keys to table message_providers
480 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
482 /// Adding indexes to table message_providers
483 $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
485 /// Create table for message_providers
486 $dbman->create_table($table);
488 upgrade_main_savepoint($result, 2008073104);
491 if ($result && $oldversion < 2008073111) {
492 /// Define table files to be created
493 $table = new xmldb_table('files');
495 /// Adding fields to table files
496 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
497 $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
498 $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
499 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
500 $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
501 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
502 $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
503 $table->add_field('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
504 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
505 $table->add_field('filesize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
506 $table->add_field('mimetype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
507 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
508 $table->add_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
509 $table->add_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null);
510 $table->add_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null);
511 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
512 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
514 /// Adding keys to table files
515 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
516 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
517 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
519 /// Adding indexes to table files
520 $table->add_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid'));
521 $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
522 $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
524 /// Conditionally launch create table for files
525 $dbman->create_table($table);
527 /// Main savepoint reached
528 upgrade_main_savepoint($result, 2008073111);
531 if ($result && $oldversion < 2008073112) {
532 // Define field legacyfiles to be added to course
533 $table = new xmldb_table('course');
534 $field = new xmldb_field('legacyfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'maxbytes');
536 // Launch add field legacyfiles
537 $dbman->add_field($table, $field);
538 // enable legacy files in all courses
539 $DB->execute("UPDATE {course} SET legacyfiles = 2");
541 // Main savepoint reached
542 upgrade_main_savepoint($result, 2008073112);
545 if ($result && $oldversion < 2008073113) {
546 /// move all course, backup and other files to new filepool based storage
547 upgrade_migrate_files_courses();
548 /// Main savepoint reached
549 upgrade_main_savepoint($result, 2008073113);
552 if ($result && $oldversion < 2008073114) {
553 /// move all course, backup and other files to new filepool based storage
554 upgrade_migrate_files_blog();
555 /// Main savepoint reached
556 upgrade_main_savepoint($result, 2008073114);
559 if ($result && $oldversion < 2008080400) {
560 // Add field ssl_jump_url to mnet application, and populate existing default applications
561 $table = new xmldb_table('mnet_application');
562 $field = new xmldb_field('sso_jump_url');
563 if (!$dbman->field_exists($table, $field)) {
564 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
565 $dbman->add_field($table, $field);
566 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
567 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
570 /// Main savepoint reached
571 upgrade_main_savepoint($result, 2008080400);
574 if ($result && $oldversion < 2008080500) {
576 /// Define table portfolio_tempdata to be created
577 $table = new xmldb_table('portfolio_tempdata');
579 /// Adding fields to table portfolio_tempdata
580 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
581 $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
583 /// Adding keys to table portfolio_tempdata
584 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
586 /// Conditionally launch create table for portfolio_tempdata
587 if (!$dbman->table_exists($table)) {
588 $dbman->create_table($table);
591 /// Main savepoint reached
592 upgrade_main_savepoint($result, 2008080500);
595 if ($result && $oldversion < 2008080600) {
597 $DB->delete_records('portfolio_tempdata'); // there shouldnt' be any, and it will cause problems with this upgrade.
598 /// Define field expirytime to be added to portfolio_tempdata
599 $table = new xmldb_table('portfolio_tempdata');
600 $field = new xmldb_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'data');
602 /// Conditionally launch add field expirytime
603 if (!$dbman->field_exists($table, $field)) {
604 $dbman->add_field($table, $field);
607 /// Main savepoint reached
608 upgrade_main_savepoint($result, 2008080600);
611 if ($result && $oldversion < 2008081500) {
612 /// Changing the type of all the columns that the question bank uses to store grades to be NUMBER(12, 7).
613 $table = new xmldb_table('question');
614 $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'generalfeedback');
615 $dbman->change_field_type($table, $field);
616 upgrade_main_savepoint($result, 2008081500);
619 if ($result && $oldversion < 2008081501) {
620 $table = new xmldb_table('question');
621 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'defaultgrade');
622 $dbman->change_field_type($table, $field);
623 upgrade_main_savepoint($result, 2008081501);
626 if ($result && $oldversion < 2008081502) {
627 $table = new xmldb_table('question_answers');
628 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'answer');
629 $dbman->change_field_type($table, $field);
630 upgrade_main_savepoint($result, 2008081502);
633 if ($result && $oldversion < 2008081503) {
634 $table = new xmldb_table('question_sessions');
635 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'newgraded');
636 $dbman->change_field_type($table, $field);
637 upgrade_main_savepoint($result, 2008081503);
640 if ($result && $oldversion < 2008081504) {
641 $table = new xmldb_table('question_states');
642 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'event');
643 $dbman->change_field_type($table, $field);
644 upgrade_main_savepoint($result, 2008081504);
647 if ($result && $oldversion < 2008081505) {
648 $table = new xmldb_table('question_states');
649 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'grade');
650 $dbman->change_field_type($table, $field);
651 upgrade_main_savepoint($result, 2008081505);
654 if ($result && $oldversion < 2008081506) {
655 $table = new xmldb_table('question_states');
656 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'raw_grade');
657 $dbman->change_field_type($table, $field);
658 upgrade_main_savepoint($result, 2008081506);
661 if ($result && $oldversion < 2008081600) {
663 /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
664 unset_config('unicodedb');
666 /// Main savepoint reached
667 upgrade_main_savepoint($result, 2008081600);
670 if ($result && $oldversion < 2008081900) {
671 /// Define field userid to be added to portfolio_tempdata
672 $table = new xmldb_table('portfolio_tempdata');
673 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'expirytime');
675 /// Conditionally launch add field userid
676 if (!$dbman->field_exists($table, $field)) {
677 $dbman->add_field($table, $field);
679 $DB->set_field('portfolio_tempdata', 'userid', 0);
680 /// now change it to be notnull
682 /// Changing nullability of field userid on table portfolio_tempdata to not null
683 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'expirytime');
685 /// Launch change of nullability for field userid
686 $dbman->change_field_notnull($table, $field);
688 /// Define key userfk (foreign) to be added to portfolio_tempdata
689 $table = new xmldb_table('portfolio_tempdata');
690 $key = new xmldb_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
692 /// Launch add key userfk
693 $dbman->add_key($table, $key);
695 upgrade_main_savepoint($result, 2008081900);
697 if ($result && $oldversion < 2008082602) {
699 /// Define table repository to be dropped
700 $table = new xmldb_table('repository');
702 /// Conditionally launch drop table for repository
703 if ($dbman->table_exists($table)) {
704 $dbman->drop_table($table);
707 /// Define table repository to be created
708 $table = new xmldb_table('repository');
710 /// Adding fields to table repository
711 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
712 $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
713 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1');
714 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
716 /// Adding keys to table repository
717 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
719 /// Conditionally launch create table for repository
720 if (!$dbman->table_exists($table)) {
721 $dbman->create_table($table);
723 /// Define table repository_instances to be created
724 $table = new xmldb_table('repository_instances');
726 /// Adding fields to table repository_instances
727 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
728 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
729 $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
730 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
731 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
732 $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null);
733 $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
734 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
735 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
737 /// Adding keys to table repository_instances
738 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
740 /// Conditionally launch create table for repository_instances
741 if (!$dbman->table_exists($table)) {
742 $dbman->create_table($table);
745 /// Define table repository_instance_config to be created
746 $table = new xmldb_table('repository_instance_config');
748 /// Adding fields to table repository_instance_config
749 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
750 $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
751 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
752 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
754 /// Adding keys to table repository_instance_config
755 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
757 /// Conditionally launch create table for repository_instance_config
758 if (!$dbman->table_exists($table)) {
759 $dbman->create_table($table);
762 /// Main savepoint reached
763 upgrade_main_savepoint($result, 2008082602);
766 if ($result && $oldversion < 2008082700) {
767 /// Add a new column to the question sessions table to record whether a
768 /// question has been flagged.
770 /// Define field flagged to be added to question_sessions
771 $table = new xmldb_table('question_sessions');
772 $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
774 /// Conditionally launch add field flagged
775 if (!$dbman->field_exists($table, $field)) {
776 $dbman->add_field($table, $field);
779 /// Main savepoint reached
780 upgrade_main_savepoint($result, 2008082700);
783 if ($result && $oldversion < 2008082900) {
785 /// Changing precision of field parent_type on table mnet_rpc to (20)
786 $table = new xmldb_table('mnet_rpc');
787 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
789 /// Launch change of precision for field parent_type
790 $dbman->change_field_precision($table, $field);
792 /// Main savepoint reached
793 upgrade_main_savepoint($result, 2008082900);
796 if ($result && $oldversion < 2008090108) {
797 $repo = new object();
798 $repo->type = 'upload';
800 $repo->sortorder = 1;
801 if (!$DB->record_exists('repository', array('type'=>'upload'))) {
802 $typeid = $DB->insert_record('repository', $repo);
804 $record = $DB->get_record('repository', array('type'=>'upload'));
805 $typeid = $record->id;
807 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
808 $instance = new object();
809 $instance->name = get_string('repositoryname', 'repository_upload');
810 $instance->typeid = $typeid;
811 $instance->userid = 0;
812 $instance->contextid = SITEID;
813 $instance->timecreated = time();
814 $instance->timemodified = time();
815 $DB->insert_record('repository_instances', $instance);
817 $repo->type = 'local';
819 $repo->sortorder = 1;
820 if (!$DB->record_exists('repository', array('type'=>'local'))) {
821 $typeid = $DB->insert_record('repository', $repo);
823 $record = $DB->get_record('repository', array('type'=>'local'));
824 $typeid = $record->id;
826 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
827 $instance = new object();
828 $instance->name = get_string('repositoryname', 'repository_local');
829 $instance->typeid = $typeid;
830 $instance->userid = 0;
831 $instance->contextid = SITEID;
832 $instance->timecreated = time();
833 $instance->timemodified = time();
834 $DB->insert_record('repository_instances', $instance);
837 upgrade_main_savepoint($result, 2008090108);
840 // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
841 if ($result && $oldversion < 2008091000) {
842 foreach (get_object_vars($CFG) as $name => $value) {
843 if (substr($name, strlen($name) - 8) !== '_version') {
846 $pluginname = substr($name, 0, strlen($name) - 8);
847 if (!strpos($pluginname, '_')) {
848 // Skip things like backup_version that don't contain an extra _
851 if ($pluginname == 'enrol_ldap_version') {
852 // Special case - this is something different from a plugin version number.
855 if (!preg_match('/^\d{10}$/', $value)) {
856 // Extra safety check, skip anything that does not look like a Moodle
857 // version number (10 digits).
860 set_config('version', $value, $pluginname);
863 upgrade_main_savepoint($result, 2008091000);
866 //Add a readonly field to the repository_instances table
867 //in order to support instance created automatically by a repository plugin
868 if ($result && $oldversion < 2008091611) {
870 /// Define field readonly to be added to repository_instances
871 $table = new xmldb_table('repository_instances');
872 $field = new xmldb_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
874 /// Conditionally launch add field readonly
875 if (!$dbman->field_exists($table, $field)) {
876 $dbman->add_field($table, $field);
879 /// Main savepoint reached
880 upgrade_main_savepoint($result, 2008091611);
883 if ($result && $oldversion < 2008092300) {
884 unset_config('editorspelling');
885 unset_config('editordictionary');
886 /// Main savepoint reached
887 upgrade_main_savepoint($result, 2008092300);
890 if ($result && $oldversion < 2008101300) {
892 if (!get_config(NULL, 'statsruntimedays')) {
893 set_config('statsruntimedays', '31');
896 /// Main savepoint reached
897 upgrade_main_savepoint($result, 2008101300);
900 /// Drop the deprecated teacher, teachers, student and students columns from the course table.
901 if ($result && $oldversion < 2008111200) {
902 $table = new xmldb_table('course');
904 /// Conditionally launch drop field teacher
905 $field = new xmldb_field('teacher');
906 if ($dbman->field_exists($table, $field)) {
907 $dbman->drop_field($table, $field);
910 /// Conditionally launch drop field teacher
911 $field = new xmldb_field('teachers');
912 if ($dbman->field_exists($table, $field)) {
913 $dbman->drop_field($table, $field);
916 /// Conditionally launch drop field teacher
917 $field = new xmldb_field('student');
918 if ($dbman->field_exists($table, $field)) {
919 $dbman->drop_field($table, $field);
922 /// Conditionally launch drop field teacher
923 $field = new xmldb_field('students');
924 if ($dbman->field_exists($table, $field)) {
925 $dbman->drop_field($table, $field);
928 /// Main savepoint reached
929 upgrade_main_savepoint($result, 2008111200);
932 /// Add a unique index to the role.name column.
933 if ($result && $oldversion < 2008111800) {
935 /// Define index name (unique) to be added to role
936 $table = new xmldb_table('role');
937 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
939 /// Conditionally launch add index name
940 if (!$dbman->index_exists($table, $index)) {
941 $dbman->add_index($table, $index);
944 /// Main savepoint reached
945 upgrade_main_savepoint($result, 2008111800);
948 /// Add a unique index to the role.shortname column.
949 if ($result && $oldversion < 2008111801) {
951 /// Define index shortname (unique) to be added to role
952 $table = new xmldb_table('role');
953 $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
955 /// Conditionally launch add index shortname
956 if (!$dbman->index_exists($table, $index)) {
957 $dbman->add_index($table, $index);
960 /// Main savepoint reached
961 upgrade_main_savepoint($result, 2008111801);
964 if ($result && $oldversion < 2008120700) {
966 /// Changing precision of field shortname on table course_request to (100)
967 $table = new xmldb_table('course_request');
968 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
970 /// Before changing the field, drop dependent indexes
971 /// Define index shortname (not unique) to be dropped form course_request
972 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
973 /// Conditionally launch drop index shortname
974 if ($dbman->index_exists($table, $index)) {
975 $dbman->drop_index($table, $index);
978 /// Launch change of precision for field shortname
979 $dbman->change_field_precision($table, $field);
981 /// After changing the field, recreate dependent indexes
982 /// Define index shortname (not unique) to be added to course_request
983 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
984 /// Conditionally launch add index shortname
985 if (!$dbman->index_exists($table, $index)) {
986 $dbman->add_index($table, $index);
989 /// Main savepoint reached
990 upgrade_main_savepoint($result, 2008120700);
993 if ($result && $oldversion < 2008120801) {
995 /// Changing precision of field shortname on table mnet_enrol_course to (100)
996 $table = new xmldb_table('mnet_enrol_course');
997 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
999 /// Launch change of precision for field shortname
1000 $dbman->change_field_precision($table, $field);
1002 /// Main savepoint reached
1003 upgrade_main_savepoint($result, 2008120801);
1006 if ($result && $oldversion < 2008121701) {
1008 /// Define field availablefrom to be added to course_modules
1009 $table = new xmldb_table('course_modules');
1010 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
1012 /// Conditionally launch add field availablefrom
1013 if (!$dbman->field_exists($table, $field)) {
1014 $dbman->add_field($table, $field);
1017 /// Define field availableuntil to be added to course_modules
1018 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
1020 /// Conditionally launch add field availableuntil
1021 if (!$dbman->field_exists($table, $field)) {
1022 $dbman->add_field($table, $field);
1025 /// Define field showavailability to be added to course_modules
1026 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
1028 /// Conditionally launch add field showavailability
1029 if (!$dbman->field_exists($table, $field)) {
1030 $dbman->add_field($table, $field);
1033 /// Define table course_modules_availability to be created
1034 $table = new xmldb_table('course_modules_availability');
1036 /// Adding fields to table course_modules_availability
1037 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1038 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1039 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1040 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
1041 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1042 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1043 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1045 /// Adding keys to table course_modules_availability
1046 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1047 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
1048 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
1049 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
1051 /// Conditionally launch create table for course_modules_availability
1052 if (!$dbman->table_exists($table)) {
1053 $dbman->create_table($table);
1056 /// Changes to modinfo mean we need to rebuild course cache
1057 require_once($CFG->dirroot . '/course/lib.php');
1058 rebuild_course_cache(0, true);
1060 /// Main savepoint reached
1061 upgrade_main_savepoint($result, 2008121701);
1064 if ($result && $oldversion < 2009010500) {
1065 /// clean up config table a bit
1066 unset_config('session_error_counter');
1068 /// Main savepoint reached
1069 upgrade_main_savepoint($result, 2009010500);
1072 if ($result && $oldversion < 2009010600) {
1074 /// Define field originalquestion to be dropped from question_states
1075 $table = new xmldb_table('question_states');
1076 $field = new xmldb_field('originalquestion');
1078 /// Conditionally launch drop field originalquestion
1079 if ($dbman->field_exists($table, $field)) {
1080 $dbman->drop_field($table, $field);
1083 /// Main savepoint reached
1084 upgrade_main_savepoint($result, 2009010600);
1087 if ($result && $oldversion < 2009010601) {
1089 /// Changing precision of field ip on table log to (45)
1090 $table = new xmldb_table('log');
1091 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1093 /// Launch change of precision for field ip
1094 $dbman->change_field_precision($table, $field);
1096 /// Main savepoint reached
1097 upgrade_main_savepoint($result, 2009010601);
1100 if ($result && $oldversion < 2009010602) {
1102 /// Changing precision of field lastip on table user to (45)
1103 $table = new xmldb_table('user');
1104 $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
1106 /// Launch change of precision for field lastip
1107 $dbman->change_field_precision($table, $field);
1109 /// Main savepoint reached
1110 upgrade_main_savepoint($result, 2009010602);
1113 if ($result && $oldversion < 2009010603) {
1115 /// Changing precision of field ip_address on table mnet_host to (45)
1116 $table = new xmldb_table('mnet_host');
1117 $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
1119 /// Launch change of precision for field ip_address
1120 $dbman->change_field_precision($table, $field);
1122 /// Main savepoint reached
1123 upgrade_main_savepoint($result, 2009010603);
1126 if ($result && $oldversion < 2009010604) {
1128 /// Changing precision of field ip on table mnet_log to (45)
1129 $table = new xmldb_table('mnet_log');
1130 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1132 /// Launch change of precision for field ip
1133 $dbman->change_field_precision($table, $field);
1135 /// Main savepoint reached
1136 upgrade_main_savepoint($result, 2009010604);
1139 if ($result && $oldversion < 2009011000) {
1141 /// Changing nullability of field configdata on table block_instance to null
1142 $table = new xmldb_table('block_instance');
1143 $field = new xmldb_field('configdata');
1144 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
1146 /// Launch change of nullability for field configdata
1147 $dbman->change_field_notnull($table, $field);
1149 /// Main savepoint reached
1150 upgrade_main_savepoint($result, 2009011000);
1153 if ($result && $oldversion < 2009011100) {
1154 /// Remove unused settings
1155 unset_config('zip');
1156 unset_config('unzip');
1157 unset_config('adminblocks_initialised');
1159 /// Main savepoint reached
1160 upgrade_main_savepoint($result, 2009011100);
1163 if ($result && $oldversion < 2009011101) {
1164 /// Migrate backup settings to core plugin config table
1165 $configs = $DB->get_records('backup_config');
1166 foreach ($configs as $config) {
1167 set_config($config->name, $config->value, 'backup');
1170 /// Define table to be dropped
1171 $table = new xmldb_table('backup_config');
1173 /// Launch drop table for old backup config
1174 $dbman->drop_table($table);
1176 /// Main savepoint reached
1177 upgrade_main_savepoint($result, 2009011101);
1180 if ($result && $oldversion < 2009011303) {
1182 /// Define table config_log to be created
1183 $table = new xmldb_table('config_log');
1185 /// Adding fields to table config_log
1186 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1187 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1188 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1189 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1190 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1191 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1192 $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1194 /// Adding keys to table config_log
1195 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1196 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1198 /// Adding indexes to table config_log
1199 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1201 /// Launch create table for config_log
1202 $dbman->create_table($table);
1204 /// Main savepoint reached
1205 upgrade_main_savepoint($result, 2009011303);
1208 if ($result && $oldversion < 2009011900) {
1210 /// Define table sessions2 to be dropped
1211 $table = new xmldb_table('sessions2');
1213 /// Conditionally launch drop table for sessions
1214 if ($dbman->table_exists($table)) {
1215 $dbman->drop_table($table);
1218 /// Define table sessions to be dropped
1219 $table = new xmldb_table('sessions');
1221 /// Conditionally launch drop table for sessions
1222 if ($dbman->table_exists($table)) {
1223 $dbman->drop_table($table);
1226 /// Define table sessions to be created
1227 $table = new xmldb_table('sessions');
1229 /// Adding fields to table sessions
1230 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1231 $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1232 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
1233 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1234 $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
1235 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1236 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1237 $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1238 $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1240 /// Adding keys to table sessions
1241 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1242 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1244 /// Adding indexes to table sessions
1245 $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1246 $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1247 $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1248 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1250 /// Launch create table for sessions
1251 $dbman->create_table($table);
1253 /// Main savepoint reached
1254 upgrade_main_savepoint($result, 2009011900);
1257 if ($result && $oldversion < 2009012901) {
1258 // NOTE: this table may already exist, see beginning of this file ;-)
1260 /// Define table upgrade_log to be created
1261 $table = new xmldb_table('upgrade_log');
1263 /// Adding fields to table upgrade_log
1264 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1265 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1266 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1267 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1268 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1269 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1270 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1271 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1272 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1274 /// Adding keys to table upgrade_log
1275 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1276 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1278 /// Adding indexes to table upgrade_log
1279 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1280 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
1282 /// Conditionally launch create table for upgrade_log
1283 if (!$dbman->table_exists($table)) {
1284 $dbman->create_table($table);
1287 /// Main savepoint reached
1288 upgrade_main_savepoint($result, 2009012901);
1291 if ($result && $oldversion < 2009021800) {
1292 // Converting format of grade conditions, if any exist, to percentages.
1294 UPDATE {course_modules_availability} SET grademin=(
1295 SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
1296 /(gi.grademax-gi.grademin)
1297 FROM {grade_items} gi
1298 WHERE gi.id={course_modules_availability}.gradeitemid)
1299 WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1301 UPDATE {course_modules_availability} SET grademax=(
1302 SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
1303 /(gi.grademax-gi.grademin)
1304 FROM {grade_items} gi
1305 WHERE gi.id={course_modules_availability}.gradeitemid)
1306 WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1308 /// Main savepoint reached
1309 upgrade_main_savepoint($result, 2009021800);
1311 if ($result && $oldversion < 2009021801) {
1312 /// Define field backuptype to be added to backup_log
1313 $table = new xmldb_table('backup_log');
1314 $field = new xmldb_field('backuptype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'info');
1315 /// Conditionally Launch add field backuptype and set all old records as 'scheduledbackup' records.
1316 if (!$dbman->field_exists($table, $field)) {
1317 $dbman->add_field($table, $field);
1318 $DB->execute("UPDATE {backup_log} SET backuptype='scheduledbackup'");
1321 /// Main savepoint reached
1322 upgrade_main_savepoint($result, 2009021801);
1324 /// Add default sort order for question types.
1325 if ($result && $oldversion < 2009030300) {
1326 set_config('multichoice_sortorder', 1, 'question');
1327 set_config('truefalse_sortorder', 2, 'question');
1328 set_config('shortanswer_sortorder', 3, 'question');
1329 set_config('numerical_sortorder', 4, 'question');
1330 set_config('calculated_sortorder', 5, 'question');
1331 set_config('essay_sortorder', 6, 'question');
1332 set_config('match_sortorder', 7, 'question');
1333 set_config('randomsamatch_sortorder', 8, 'question');
1334 set_config('multianswer_sortorder', 9, 'question');
1335 set_config('description_sortorder', 10, 'question');
1336 set_config('random_sortorder', 11, 'question');
1337 set_config('missingtype_sortorder', 12, 'question');
1339 upgrade_main_savepoint($result, 2009030300);
1341 if ($result && $oldversion < 2009030501) {
1342 /// setup default repository plugins
1343 require_once($CFG->dirroot . '/repository/lib.php');
1344 repository_setup_default_plugins();
1345 /// Main savepoint reached
1346 upgrade_main_savepoint($result, 2009030501);
1349 /// MDL-18132 replace the use a new Role allow switch settings page, instead of
1350 /// $CFG->allowuserswitchrolestheycantassign
1351 if ($result && $oldversion < 2009032000) {
1352 /// First create the new table.
1353 $table = new xmldb_table('role_allow_switch');
1355 /// Adding fields to table role_allow_switch
1356 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1357 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1358 $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1360 /// Adding keys to table role_allow_switch
1361 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1362 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
1363 $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
1365 /// Adding indexes to table role_allow_switch
1366 $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1368 /// Conditionally launch create table for role_allow_switch
1369 if (!$dbman->table_exists($table)) {
1370 $dbman->create_table($table);
1373 /// Main savepoint reached
1374 upgrade_main_savepoint($result, 2009032000);
1377 if ($result && $oldversion < 2009032001) {
1378 /// Copy from role_allow_assign into the new table.
1379 $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1380 SELECT roleid, allowassign FROM {role_allow_assign}');
1382 /// Unset the config variable used in 1.9.
1383 unset_config('allowuserswitchrolestheycantassign');
1385 /// Main savepoint reached
1386 upgrade_main_savepoint($result, 2009032001);
1389 if ($result && $oldversion < 2009033100) {
1390 require_once("$CFG->dirroot/filter/tex/lib.php");
1391 filter_tex_updatedcallback(null);
1392 /// Main savepoint reached
1393 upgrade_main_savepoint($result, 2009033100);
1396 if ($result && $oldversion < 2009040300) {
1398 /// Define table filter_active to be created
1399 $table = new xmldb_table('filter_active');
1401 /// Adding fields to table filter_active
1402 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1403 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1404 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1405 $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1406 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1408 /// Adding keys to table filter_active
1409 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1410 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1412 /// Adding indexes to table filter_active
1413 $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1415 /// Conditionally launch create table for filter_active
1416 if (!$dbman->table_exists($table)) {
1417 $dbman->create_table($table);
1420 /// Main savepoint reached
1421 upgrade_main_savepoint($result, 2009040300);
1424 if ($result && $oldversion < 2009040301) {
1426 /// Define table filter_config to be created
1427 $table = new xmldb_table('filter_config');
1429 /// Adding fields to table filter_config
1430 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1431 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1432 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1433 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1434 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1436 /// Adding keys to table filter_config
1437 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1438 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1440 /// Adding indexes to table filter_config
1441 $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1443 /// Conditionally launch create table for filter_config
1444 if (!$dbman->table_exists($table)) {
1445 $dbman->create_table($table);
1448 /// Main savepoint reached
1449 upgrade_main_savepoint($result, 2009040301);
1452 if ($result && $oldversion < 2009040302) {
1453 /// Transfer current settings from $CFG->textfilters
1454 $disabledfilters = filter_get_all_installed();
1455 if (empty($CFG->textfilters)) {
1456 $activefilters = array();
1458 $activefilters = explode(',', $CFG->textfilters);
1460 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1462 foreach ($activefilters as $filter) {
1463 filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1465 unset($disabledfilters[$filter]);
1467 foreach ($disabledfilters as $filter => $notused) {
1468 filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1472 /// Main savepoint reached
1473 upgrade_main_savepoint($result, 2009040302);
1476 if ($result && $oldversion < 2009040600) {
1477 /// Ensure that $CFG->stringfilters is set.
1478 if (empty($CFG->stringfilters)) {
1479 if (!empty($CFG->filterall)) {
1480 set_config('stringfilters', $CFG->textfilters);
1482 set_config('stringfilters', '');
1486 set_config('filterall', !empty($CFG->stringfilters));
1487 unset_config('textfilters');
1489 /// Main savepoint reached
1490 upgrade_main_savepoint($result, 2009040600);
1493 if ($result && $oldversion < 2009041700) {
1494 /// To ensure the UI remains consistent with no behaviour change, any
1495 /// 'until' date in an activity condition should have 1 second subtracted
1496 /// (to go from 0:00 on the following day to 23:59 on the previous one).
1497 $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
1498 require_once($CFG->dirroot . '/course/lib.php');
1499 rebuild_course_cache(0, true);
1501 /// Main savepoint reached
1502 upgrade_main_savepoint($result, 2009041700);
1505 if ($result && $oldversion < 2009042600) {
1506 /// Deleting orphaned messages from deleted users.
1507 require_once($CFG->dirroot.'/message/lib.php');
1508 /// Detect deleted users with messages sent(useridfrom) and not read
1509 if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
1511 JOIN {message} m ON m.useridfrom = u.id
1512 WHERE u.deleted = ?', array(1))) {
1513 foreach ($deletedusers as $deleteduser) {
1514 message_move_userfrom_unread2read($deleteduser->id); // move messages
1517 /// Main savepoint reached
1518 upgrade_main_savepoint($result, 2009042600);
1521 /// Dropping all enums/check contraints from core. MDL-18577
1522 if ($result && $oldversion < 2009042700) {
1524 /// Changing list of values (enum) of field stattype on table stats_daily to none
1525 $table = new xmldb_table('stats_daily');
1526 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1528 /// Launch change of list of values for field stattype
1529 $dbman->drop_enum_from_field($table, $field);
1531 /// Changing list of values (enum) of field stattype on table stats_weekly to none
1532 $table = new xmldb_table('stats_weekly');
1533 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1535 /// Launch change of list of values for field stattype
1536 $dbman->drop_enum_from_field($table, $field);
1538 /// Changing list of values (enum) of field stattype on table stats_monthly to none
1539 $table = new xmldb_table('stats_monthly');
1540 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1542 /// Launch change of list of values for field stattype
1543 $dbman->drop_enum_from_field($table, $field);
1545 /// Changing list of values (enum) of field publishstate on table post to none
1546 $table = new xmldb_table('post');
1547 $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
1549 /// Launch change of list of values for field publishstate
1550 $dbman->drop_enum_from_field($table, $field);
1552 /// Main savepoint reached
1553 upgrade_main_savepoint($result, 2009042700);
1556 if ($result && $oldversion < 2009043000) {
1557 unset_config('grade_report_showgroups');
1558 upgrade_main_savepoint($result, 2009043000);
1561 if ($result && $oldversion < 2009050600) {
1562 /// Site front page blocks need to be moved due to page name change.
1563 $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
1565 /// Main savepoint reached
1566 upgrade_main_savepoint($result, 2009050600);
1569 if ($result && $oldversion < 2009050601) {
1571 /// Define table block_instance to be renamed to block_instances
1572 $table = new xmldb_table('block_instance');
1574 /// Launch rename table for block_instance
1575 $dbman->rename_table($table, 'block_instances');
1577 /// Main savepoint reached
1578 upgrade_main_savepoint($result, 2009050601);
1581 if ($result && $oldversion < 2009050602) {
1583 /// Define table block_instance to be renamed to block_instance_old
1584 $table = new xmldb_table('block_pinned');
1586 /// Launch rename table for block_instance
1587 $dbman->rename_table($table, 'block_pinned_old');
1589 /// Main savepoint reached
1590 upgrade_main_savepoint($result, 2009050602);
1593 if ($result && $oldversion < 2009050603) {
1595 /// Define table block_instance_old to be created
1596 $table = new xmldb_table('block_instance_old');
1598 /// Adding fields to table block_instance_old
1599 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1600 $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1601 $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1602 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1603 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
1604 $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
1605 $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
1606 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1607 $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1609 /// Adding keys to table block_instance_old
1610 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1611 $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
1613 /// Adding indexes to table block_instance_old
1614 $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1615 $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
1617 /// Conditionally launch create table for block_instance_old
1618 if (!$dbman->table_exists($table)) {
1619 $dbman->create_table($table);
1622 /// Main savepoint reached
1623 upgrade_main_savepoint($result, 2009050603);
1626 if ($result && $oldversion < 2009050604) {
1627 /// Copy current blocks data from block_instances to block_instance_old
1628 $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
1629 SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
1631 upgrade_main_savepoint($result, 2009050604);
1634 if ($result && $oldversion < 2009050605) {
1636 /// Define field multiple to be dropped from block
1637 $table = new xmldb_table('block');
1638 $field = new xmldb_field('multiple');
1640 /// Conditionally launch drop field multiple
1641 if ($dbman->field_exists($table, $field)) {
1642 $dbman->drop_field($table, $field);
1645 /// Main savepoint reached
1646 upgrade_main_savepoint($result, 2009050605);
1649 if ($result && $oldversion < 2009050606) {
1650 $table = new xmldb_table('block_instances');
1652 /// Rename field weight on table block_instances to defaultweight
1653 $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'position');
1654 $dbman->rename_field($table, $field, 'defaultweight');
1656 /// Rename field position on table block_instances to defaultregion
1657 $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
1658 $dbman->rename_field($table, $field, 'defaultregion');
1660 /// Main savepoint reached
1661 upgrade_main_savepoint($result, 2009050606);
1664 if ($result && $oldversion < 2009050607) {
1665 /// Changing precision of field defaultregion on table block_instances to (16)
1666 $table = new xmldb_table('block_instances');
1667 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
1669 /// Launch change of precision for field defaultregion
1670 $dbman->change_field_precision($table, $field);
1672 /// Main savepoint reached
1673 upgrade_main_savepoint($result, 2009050607);
1676 if ($result && $oldversion < 2009050608) {
1677 /// Change regions to the new notation
1678 $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1679 $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1680 $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1681 // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1682 // flex page course format. Hopefully this new value is an adequate alternative.
1684 /// Main savepoint reached
1685 upgrade_main_savepoint($result, 2009050608);
1688 if ($result && $oldversion < 2009050609) {
1690 /// Define key blockname (unique) to be added to block
1691 $table = new xmldb_table('block');
1692 $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1694 /// Launch add key blockname
1695 $dbman->add_key($table, $key);
1697 /// Main savepoint reached
1698 upgrade_main_savepoint($result, 2009050609);
1701 if ($result && $oldversion < 2009050610) {
1702 $table = new xmldb_table('block_instances');
1704 /// Define field blockname to be added to block_instances
1705 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
1706 if (!$dbman->field_exists($table, $field)) {
1707 $dbman->add_field($table, $field);
1710 /// Define field contextid to be added to block_instances
1711 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
1712 if (!$dbman->field_exists($table, $field)) {
1713 $dbman->add_field($table, $field);
1716 /// Define field showinsubcontexts to be added to block_instances
1717 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
1718 if (!$dbman->field_exists($table, $field)) {
1719 $dbman->add_field($table, $field);
1722 /// Define field subpagepattern to be added to block_instances
1723 $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
1724 if (!$dbman->field_exists($table, $field)) {
1725 $dbman->add_field($table, $field);
1728 /// Main savepoint reached
1729 upgrade_main_savepoint($result, 2009050610);
1732 if ($result && $oldversion < 2009050611) {
1733 $table = new xmldb_table('block_instances');
1735 /// Fill in blockname from blockid
1736 $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1738 /// Set showinsubcontexts = 0 for all rows.
1739 $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1741 /// Main savepoint reached
1742 upgrade_main_savepoint($result, 2009050611);
1745 if ($result && $oldversion < 2009050612) {
1747 /// Rename field pagetype on table block_instances to pagetypepattern
1748 $table = new xmldb_table('block_instances');
1749 $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
1751 /// Launch rename field pagetype
1752 $dbman->rename_field($table, $field, 'pagetypepattern');
1754 /// Main savepoint reached
1755 upgrade_main_savepoint($result, 2009050612);
1758 if ($result && $oldversion < 2009050613) {
1759 /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1762 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
1763 $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1764 pagetypepattern = 'site-index',
1765 subpagepattern = NULL
1766 WHERE pagetypepattern = 'site-index'");
1769 $DB->execute("UPDATE {block_instances} SET
1773 JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1774 WHERE {course}.id = pageid
1776 pagetypepattern = 'course-view-*',
1777 subpagepattern = NULL
1778 WHERE pagetypepattern = 'course-view'");
1781 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1782 $DB->execute("UPDATE {block_instances} SET
1783 contextid = " . $syscontext->id . ",
1784 pagetypepattern = 'admin-*',
1785 subpagepattern = NULL
1786 WHERE pagetypepattern = 'admin'");
1789 $DB->execute("UPDATE {block_instances} SET
1793 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1794 WHERE {user}.id = pageid
1796 pagetypepattern = 'my-index',
1797 subpagepattern = NULL
1798 WHERE pagetypepattern = 'my-index'");
1801 $DB->execute("UPDATE {block_instances} SET
1802 contextid = " . $syscontext->id . ",
1803 pagetypepattern = 'tag-index',
1804 subpagepattern = pageid
1805 WHERE pagetypepattern = 'tag-index'");
1808 $DB->execute("UPDATE {block_instances} SET
1812 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1813 WHERE {user}.id = pageid
1815 pagetypepattern = 'blog-index',
1816 subpagepattern = NULL
1817 WHERE pagetypepattern = 'blog-view'");
1820 $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1821 foreach ($moduleswithblocks as $modname) {
1822 if (!$dbman->table_exists($modname)) {
1825 $DB->execute("UPDATE {block_instances} SET
1829 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1830 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1831 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1832 WHERE {{$modname}}.id = pageid
1834 pagetypepattern = 'blog-index',
1835 subpagepattern = NULL
1836 WHERE pagetypepattern = 'blog-view'");
1839 /// Main savepoint reached
1840 upgrade_main_savepoint($result, 2009050613);
1843 if ($result && $oldversion < 2009050614) {
1844 /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
1845 $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
1847 /// Main savepoint reached
1848 upgrade_main_savepoint($result, 2009050614);
1851 if ($result && $oldversion < 2009050615) {
1852 $table = new xmldb_table('block_instances');
1854 /// Arrived here, any block_instances record without blockname is one
1855 /// orphan block coming from 1.9. Just delete them. MDL-22503
1856 $DB->delete_records_select('block_instances', 'blockname IS NULL');
1858 /// Changing nullability of field blockname on table block_instances to not null
1859 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
1860 $dbman->change_field_notnull($table, $field);
1862 /// Changing nullability of field contextid on table block_instances to not null
1863 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
1864 $dbman->change_field_notnull($table, $field);
1866 /// Changing nullability of field showinsubcontexts on table block_instances to not null
1867 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
1868 $dbman->change_field_notnull($table, $field);
1870 /// Main savepoint reached
1871 upgrade_main_savepoint($result, 2009050615);
1874 if ($result && $oldversion < 2009050616) {
1875 /// Add exiting sticky blocks.
1876 $blocks = $DB->get_records('block');
1877 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1878 $newregions = array(
1881 'c' => 'course-view-top',
1883 $stickyblocks = $DB->get_recordset('block_pinned_old');
1884 foreach ($stickyblocks as $stickyblock) {
1885 $newblock = new object();
1886 $newblock->blockname = $blocks[$stickyblock->blockid]->name;
1887 $newblock->contextid = $syscontext->id;
1888 $newblock->showinsubcontexts = 1;
1889 switch ($stickyblock->pagetype) {
1891 $newblock->pagetypepattern = 'course-view-*';
1894 $newblock->pagetypepattern = $stickyblock->pagetype;
1896 $newblock->defaultregion = $newregions[$stickyblock->position];
1897 $newblock->defaultweight = $stickyblock->weight;
1898 $newblock->configdata = $stickyblock->configdata;
1899 $newblock->visible = 1;
1900 $DB->insert_record('block_instances', $newblock);
1903 /// Main savepoint reached
1904 upgrade_main_savepoint($result, 2009050616);
1907 if ($result && $oldversion < 2009050617) {
1909 /// Define table block_positions to be created
1910 $table = new xmldb_table('block_positions');
1912 /// Adding fields to table block_positions
1913 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1914 $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1915 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1916 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1917 $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1918 $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1');
1919 $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1920 $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1922 /// Adding keys to table block_positions
1923 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1924 $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
1925 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1927 /// Adding indexes to table block_positions
1928 $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
1930 /// Conditionally launch create table for block_positions
1931 if (!$dbman->table_exists($table)) {
1932 $dbman->create_table($table);
1935 /// Main savepoint reached
1936 upgrade_main_savepoint($result, 2009050617);
1939 if ($result && $oldversion < 2009050618) {
1940 /// And block instances with visible = 0, copy that information to block_positions
1941 $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
1942 SELECT id, contextid,
1943 CASE WHEN pagetypepattern = 'course-view-*' THEN
1944 (SELECT " . $DB->sql_concat("'course-view-'", 'format') . "
1946 JOIN {context} ON {course}.id = {context}.instanceid
1947 WHERE {context}.id = contextid)
1948 ELSE pagetypepattern END,
1949 CASE WHEN subpagepattern IS NULL THEN ''
1950 ELSE subpagepattern END,
1951 0, defaultregion, defaultweight
1952 FROM {block_instances} WHERE visible = 0 AND pagetypepattern <> 'admin-*'");
1954 /// Main savepoint reached
1955 upgrade_main_savepoint($result, 2009050618);
1958 if ($result && $oldversion < 2009050619) {
1959 $table = new xmldb_table('block_instances');
1961 /// Define field blockid to be dropped from block_instances
1962 $field = new xmldb_field('blockid');
1963 if ($dbman->field_exists($table, $field)) {
1964 /// Before dropping the field, drop dependent indexes
1965 $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
1966 if ($dbman->index_exists($table, $index)) {
1967 /// Launch drop index blockid
1968 $dbman->drop_index($table, $index);
1970 $dbman->drop_field($table, $field);
1973 /// Define field pageid to be dropped from block_instances
1974 $field = new xmldb_field('pageid');
1975 if ($dbman->field_exists($table, $field)) {
1976 /// Before dropping the field, drop dependent indexes
1977 $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1978 if ($dbman->index_exists($table, $index)) {
1979 /// Launch drop index pageid
1980 $dbman->drop_index($table, $index);
1982 $dbman->drop_field($table, $field);
1985 /// Define field visible to be dropped from block_instances
1986 $field = new xmldb_field('visible');
1987 if ($dbman->field_exists($table, $field)) {
1988 $dbman->drop_field($table, $field);
1991 /// Main savepoint reached
1992 upgrade_main_savepoint($result, 2009050619);
1995 if ($result && $oldversion < 2009051200) {
1996 /// Let's check the status of mandatory mnet_host records, fixing them
1997 /// and moving "orphan" users to default localhost record. MDL-16879
1998 echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
1999 upgrade_fix_incorrect_mnethostids();
2001 /// Main savepoint reached
2002 upgrade_main_savepoint($result, 2009051200);
2006 if ($result && $oldversion < 2009051700) {
2007 /// migrate editor settings
2008 if (empty($CFG->htmleditor)) {
2009 set_config('texteditors', 'textarea');
2011 set_config('texteditors', 'tinymce,textarea');
2014 unset_config('htmleditor');
2015 unset_config('defaulthtmleditor');
2017 /// Main savepoint reached
2018 upgrade_main_savepoint($result, 2009051700);
2021 if ($result && $oldversion < 2009060200) {
2022 /// Define table files_cleanup to be dropped - not needed
2023 $table = new xmldb_table('files_cleanup');
2025 /// Conditionally launch drop table for files_cleanup
2026 if ($dbman->table_exists($table)) {
2027 $dbman->drop_table($table);
2030 /// Main savepoint reached
2031 upgrade_main_savepoint($result, 2009060200);
2034 if ($result && $oldversion < 2009061300) {
2035 //TODO: copy this to the very beginning of this upgrade script so that we may log upgrade queries
2037 /// Define table log_queries to be created
2038 $table = new xmldb_table('log_queries');
2040 /// Adding fields to table log_queries
2041 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2042 $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2043 $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
2044 $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
2045 $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2046 $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2047 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2048 $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
2049 $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2051 /// Adding keys to table log_queries
2052 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2054 /// Conditionally launch create table for log_queries
2055 if (!$dbman->table_exists($table)) {
2056 $dbman->create_table($table);
2059 /// Main savepoint reached
2060 upgrade_main_savepoint($result, 2009061300);
2063 /// Repeat 2009050607 upgrade step, which Petr commented out becuase of XMLDB
2064 /// stupidity, so lots of peopel will have missed.
2065 if ($result && $oldversion < 2009061600) {
2066 /// Changing precision of field defaultregion on table block_instances to (16)
2067 $table = new xmldb_table('block_instances');
2068 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
2070 /// Launch change of precision for field defaultregion
2071 $dbman->change_field_precision($table, $field);
2073 /// Main savepoint reached
2074 upgrade_main_savepoint($result, 2009061600);
2077 if ($result && $oldversion < 2009061702) {
2078 // standardizing plugin names
2079 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
2080 foreach ($configs as $config) {
2081 unset_config($config->name, $config->plugin); /// unset old config
2082 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
2083 set_config($config->name, $config->value, $config->plugin); /// set new config
2087 upgrade_main_savepoint($result, 2009061702);
2090 if ($result && $oldversion < 2009061703) {
2091 // standardizing plugin names
2092 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
2093 foreach ($configs as $config) {
2094 unset_config($config->name, $config->plugin); /// unset old config
2095 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
2096 set_config($config->name, $config->value, $config->plugin); /// set new config
2100 upgrade_main_savepoint($result, 2009061703);
2103 if ($result && $oldversion < 2009061704) {
2104 // change component string in capability records to new "_" format
2105 if ($caps = $DB->get_records('capabilities')) {
2106 foreach ($caps as $cap) {
2107 $cap->component = str_replace('/', '_', $cap->component);
2108 $DB->update_record('capabilities', $cap);
2112 upgrade_main_savepoint($result, 2009061704);
2115 if ($result && $oldversion < 2009061705) {
2116 // change component string in events_handlers records to new "_" format
2117 if ($handlers = $DB->get_records('events_handlers')) {
2118 foreach ($handlers as $handler) {
2119 $handler->handlermodule = str_replace('/', '_', $handler->handlermodule);
2120 $DB->update_record('events_handlers', $handler);
2124 upgrade_main_savepoint($result, 2009061705);
2127 if ($result && $oldversion < 2009061706) {
2128 // change component string in message_providers records to new "_" format
2129 if ($mps = $DB->get_records('message_providers')) {
2130 foreach ($mps as $mp) {
2131 $mp->component = str_replace('/', '_', $mp->component);
2132 $DB->update_record('message_providers', $cap);
2136 upgrade_main_savepoint($result, 2009061706);
2139 if ($result && $oldversion < 2009063000) {
2140 // upgrade format of _with_advanced settings - quiz only
2141 // note: this can be removed later, not needed for upgrades from 1.9.x
2142 if ($quiz = get_config('quiz')) {
2143 foreach ($quiz as $name=>$value) {
2144 if (strpos($name, 'fix_') !== 0) {
2147 $newname = substr($name,4).'_adv';
2148 set_config($newname, $value, 'quiz');
2149 unset_config($name, 'quiz');
2152 upgrade_main_savepoint($result, 2009063000);
2155 if ($result && $oldversion < 2009071000) {
2157 /// Rename field contextid on table block_instances to parentcontextid
2158 $table = new xmldb_table('block_instances');
2159 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
2161 /// Launch rename field parentcontextid
2162 $dbman->rename_field($table, $field, 'parentcontextid');
2164 /// Main savepoint reached
2165 upgrade_main_savepoint($result, 2009071000);
2168 if ($result && $oldversion < 2009071300) {
2170 /// Create contexts for every block. In the past, only non-sticky course block had contexts.
2171 /// This is a copy of the code in create_contexts.
2172 $sql = "INSERT INTO {context} (contextlevel, instanceid)
2173 SELECT " . CONTEXT_BLOCK . ", bi.id
2174 FROM {block_instances} bi
2175 WHERE NOT EXISTS (SELECT 'x'
2177 WHERE bi.id = ctx.instanceid AND ctx.contextlevel=" . CONTEXT_BLOCK . ")";
2180 /// TODO MDL-19776 We should not really use API funcitons in upgrade.
2181 /// If MDL-19776 is done, we can remove this whole upgrade block.
2182 build_context_path();
2184 /// Main savepoint reached
2185 upgrade_main_savepoint($result, 2009071300);
2188 if ($result && $oldversion < 2009071600) {
2190 /// Define field summaryformat to be added to post
2191 $table = new xmldb_table('post');
2192 $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
2194 /// Conditionally launch add field summaryformat
2195 if (!$dbman->field_exists($table, $field)) {
2196 $dbman->add_field($table, $field);
2199 /// Main savepoint reached
2200 upgrade_main_savepoint($result, 2009071600);
2203 if ($result && $oldversion < 2009072400) {
2205 /// Define table comments to be created
2206 $table = new xmldb_table('comments');
2208 /// Adding fields to table comments
2209 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2210 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2211 $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2212 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2213 $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2214 $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2215 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2216 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2218 /// Adding keys to table comments
2219 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2221 /// Conditionally launch create table for comments
2222 if (!$dbman->table_exists($table)) {
2223 $dbman->create_table($table);
2226 /// Main savepoint reached
2227 upgrade_main_savepoint($result, 2009072400);
2231 * This upgrade is to set up the new navigation blocks that have been developed
2232 * as part of Moodle 2.0
2233 * Now I [Sam Hemelryk] hit a conundrum while exploring how to go about this
2234 * as not only do we want to install the new blocks but we also want to set up
2235 * default instances of them, and at the same time remove instances of the blocks
2236 * that were/will-be outmoded by the two new navigation blocks.
2237 * After talking it through with Tim Hunt {@link http://moodle.org/mod/cvsadmin/view.php?conversationid=3112}
2238 * we decided that the best way to go about this was to put the bulk of the
2239 * upgrade operation into core upgrade `here` but to let the plugins block
2240 * still install the blocks.
2241 * This leaves one hairy end in that we will create block_instances within the
2242 * DB before the blocks themselves are created within the DB
2244 if ($result && $oldversion < 2009082800) {
2246 echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2248 // Get the system context so we can set the block instances to it
2249 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2251 // An array to contain the new block instances we will create
2252 $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
2253 // The new global navigation block instance as a stdClass
2254 $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
2255 $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
2256 $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutly everywhere
2257 $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
2258 $newblockinstances['globalnavigation']->subpagetypepattern = null;
2259 $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
2260 $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
2261 $newblockinstances['globalnavigation']->configdata = '';
2262 // The new settings navigation block instance as a stdClass
2263 $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
2264 $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
2265 $newblockinstances['settingsnavigation']->showinsubcontexts = true;
2266 $newblockinstances['settingsnavigation']->pagetypepattern = '*';
2267 $newblockinstances['settingsnavigation']->subpagetypepattern = null;
2268 $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
2269 $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
2270 $newblockinstances['settingsnavigation']->configdata = '';
2272 // Blocks that are outmoded and for whom the bells will toll... by which I
2273 // mean we will delete all instances of
2274 $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
2275 $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
2276 unset($outmodedblocks);
2277 // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
2278 // cut down the number of delete queries we will need to run
2279 $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
2281 $contextids = array();
2282 $instanceids = array();
2283 // Iterate through all block instances
2284 foreach ($allblockinstances as $blockinstance) {
2285 if (!in_array($blockinstance->parentcontextid, $contextids)) {
2286 $contextids[] = $blockinstance->parentcontextid;
2288 // If we have over 1000 contexts clean them up and reset the array
2289 // this ensures we don't hit any nasty memory limits or such
2290 if (count($contextids) > 1000) {
2291 upgrade_cleanup_unwanted_block_contexts($contextids);
2292 $contextids = array();
2295 if (!in_array($blockinstance->id, $instanceids)) {
2296 $instanceids[] = $blockinstance->id;
2297 // If we have more than 1000 block instances now remove all block positions
2298 // and empty the array
2299 if (count($contextids) > 1000) {
2300 $instanceidstring = join(',',$instanceids);
2301 $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2302 $instanceids = array();
2307 upgrade_cleanup_unwanted_block_contexts($contextids);
2309 $instanceidstring = join(',',$instanceids);
2310 $outcome1 = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2312 unset($allblockinstances);
2314 unset($instanceids);
2315 unset($instanceidstring);
2317 // Now remove the actual block instance
2318 $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
2319 unset($outmodedblocksstring);
2321 // Insert the new block instances. Remember they have not been installed yet
2322 // however this should not be a problem
2323 foreach ($newblockinstances as $blockinstance) {
2324 $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
2325 // Ensure the block context is created.
2326 get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
2328 unset($newblockinstances);
2330 upgrade_main_savepoint($result, 2009082800);
2331 // The end of the navigation upgrade
2334 if ($result && $oldversion < 2009090800){
2335 //insert new record for log_display table
2336 //used to record tag update.
2337 if (!$DB->record_exists('log_display', array('action'=>'update', 'module'=>'tag'))) {
2338 $log_action = new object();
2339 $log_action->module = 'tag';
2340 $log_action->action = 'update';
2341 $log_action->mtable = 'tag';
2342 $log_action->field = 'name';
2344 $result = $result && $DB->insert_record('log_display', $log_action);
2346 upgrade_main_savepoint($result, 2009090800);
2349 if ($result && $oldversion < 2009100601) {
2350 // drop all previous tables defined during the dev phase
2351 $dropold = array('external_services_users', 'external_services_functions', 'external_services', 'external_functions');
2352 foreach ($dropold as $tablename) {
2353 $table = new xmldb_table($tablename);
2354 if ($dbman->table_exists($table)) {
2355 $dbman->drop_table($table);
2358 upgrade_main_savepoint($result, 2009100601);
2361 if ($result && $oldversion < 2009100602) {
2362 /// Define table external_functions to be created
2363 $table = new xmldb_table('external_functions');
2365 /// Adding fields to table external_functions
2366 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2367 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2368 $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2369 $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2370 $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2371 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2373 /// Adding keys to table external_functions
2374 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2376 /// Adding indexes to table external_functions
2377 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2379 /// Launch create table for external_functions
2380 $dbman->create_table($table);
2382 /// Main savepoint reached
2383 upgrade_main_savepoint($result, 2009100602);
2386 if ($result && $oldversion < 2009100603) {
2387 /// Define table external_services to be created
2388 $table = new xmldb_table('external_services');
2390 /// Adding fields to table external_services
2391 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2392 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2393 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2394 $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
2395 $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2396 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
2398 /// Adding keys to table external_services
2399 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2401 /// Adding indexes to table external_services
2402 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2404 /// Launch create table for external_services
2405 $dbman->create_table($table);
2407 /// Main savepoint reached
2408 upgrade_main_savepoint($result, 2009100603);
2411 if ($result && $oldversion < 2009100604) {
2412 /// Define table external_services_functions to be created
2413 $table = new xmldb_table('external_services_functions');
2415 /// Adding fields to table external_services_functions
2416 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2417 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2418 $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2420 /// Adding keys to table external_services_functions
2421 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2422 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2424 /// Launch create table for external_services_functions
2425 $dbman->create_table($table);
2427 /// Main savepoint reached
2428 upgrade_main_savepoint($result, 2009100604);
2431 if ($result && $oldversion < 2009100605) {
2432 /// Define table external_services_users to be created
2433 $table = new xmldb_table('external_services_users');
2435 /// Adding fields to table external_services_users
2436 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2437 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2438 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2439 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2440 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2441 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2443 /// Adding keys to table external_services_users
2444 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2445 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2446 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2448 /// Launch create table for external_services_users
2449 $dbman->create_table($table);
2451 /// Main savepoint reached
2452 upgrade_main_savepoint($result, 2009100605);
2455 if ($result && $oldversion < 2009102600) {
2457 /// Define table external_tokens to be created
2458 $table = new xmldb_table('external_tokens');
2460 /// Adding fields to table external_tokens
2461 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2462 $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
2463 $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2464 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2465 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2466 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
2467 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2468 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2469 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2470 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2471 $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2473 /// Adding keys to table external_tokens
2474 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2475 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2476 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2477 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2479 /// Launch create table for external_tokens
2480 $dbman->create_table($table);
2482 /// Main savepoint reached
2483 upgrade_main_savepoint($result, 2009102600);
2486 if ($result && $oldversion < 2009103000) {
2488 /// Define table blog_association to be created
2489 $table = new xmldb_table('blog_association');
2491 /// Adding fields to table blog_association
2492 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2493 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2494 $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2496 /// Adding keys to table blog_association
2497 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2498 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2499 $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
2501 /// Conditionally launch create table for blog_association
2502 if (!$dbman->table_exists($table)) {
2503 $dbman->create_table($table);
2506 /// Define table blog_external to be created
2507 $table = new xmldb_table('blog_external');
2509 /// Adding fields to table blog_external
2510 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2511 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2512 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2513 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2514 $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2515 $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2516 $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2517 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2518 $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2520 /// Adding keys to table blog_external
2521 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2522 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2524 /// Conditionally launch create table for blog_external
2525 if ($dbman->table_exists($table)) {
2526 // Delete the existing one first (comes from early dev version)
2527 $dbman->drop_table($table);
2529 $dbman->create_table($table);
2531 // now inform admins that some settings require attention after upgrade
2532 if (($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete)) {
2533 echo $OUTPUT->notification(get_string('bloglevelupgradenotice', 'admin'));
2538 $a->sitename = $site->fullname;
2539 $a->fixurl = "$CFG->wwwroot/$CFG->admin/bloglevelupgrade.php";
2541 $subject = get_string('bloglevelupgrade', 'admin');
2542 $description = get_string('bloglevelupgradedescription', 'admin', $a);
2544 // can not use messaging here because it is not configured yet!
2545 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2547 /// Main savepoint reached
2548 upgrade_main_savepoint($result, 2009103000);
2551 if ($result && $oldversion < 2009110400) {
2553 // An array used to store the table name and keys of summary and trust fields
2555 $extendtables = array();
2556 $extendtables['course'] = array('summaryformat');
2557 $extendtables['course_categories'] = array('descriptionformat');
2558 $extendtables['course_request'] = array('summaryformat');
2559 $extendtables['grade_outcomes'] = array('descriptionformat');
2560 $extendtables['groups'] = array('descriptionformat');
2561 $extendtables['groupings'] = array('descriptionformat');
2562 $extendtables['scale'] = array('descriptionformat');
2563 $extendtables['user'] = array('descriptionformat');
2564 $extendtables['user_info_field'] = array('descriptionformat', 'defaultdataformat');
2565 $extendtables['user_info_data'] = array('dataformat');
2567 foreach ($extendtables as $tablestr=>$newfields) {
2568 $table = new xmldb_table($tablestr);
2569 foreach ($newfields as $fieldstr) {
2570 $field = new xmldb_field($fieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2571 // Check that the field doesn't already exists
2572 if (!$dbman->field_exists($table, $field)) {
2573 // Add the new field
2574 $dbman->add_field($table, $field);
2575 // Update the field if the text contains the default FORMAT_MOODLE to FORMAT_HTML
2576 if (($pos = strpos($fieldstr, 'format'))>0) {
2577 upgrade_set_timeout(60*20); // this may take a little while
2578 $params = array(FORMAT_HTML, '<p%', '%<br />%', FORMAT_MOODLE);
2579 $textfield = substr($fieldstr, 0, $pos);
2580 $DB->execute('UPDATE {'.$tablestr.'} SET '.$fieldstr.'=? WHERE ('.$textfield.' LIKE ? OR '.$textfield.' LIKE ?) AND '.$fieldstr.'=?', $params);
2586 unset($extendtables);
2588 upgrade_main_savepoint($result, 2009110400);
2591 if ($result && $oldversion < 2009110605) {
2593 /// Define field timecreated to be added to external_services
2594 $table = new xmldb_table('external_services');
2595 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'component');
2597 /// Conditionally launch add field timecreated
2598 if (!$dbman->field_exists($table, $field)) {
2599 $dbman->add_field($table, $field);
2602 /// Define field timemodified to be added to external_services
2603 $table = new xmldb_table('external_services');
2604 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'timecreated');
2606 /// Conditionally launch add field timemodified
2607 if (!$dbman->field_exists($table, $field)) {
2608 $dbman->add_field($table, $field);
2611 /// Main savepoint reached
2612 upgrade_main_savepoint($result, 2009110605);
2615 if ($result && $oldversion < 2009111600) {
2617 /// Define field instance to be added to portfolio_tempdata
2618 $table = new xmldb_table('portfolio_tempdata');
2619 $field = new xmldb_field('instance', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'userid');
2621 /// Conditionally launch add field instance
2622 if (!$dbman->field_exists($table, $field)) {
2623 $dbman->add_field($table, $field);
2626 $key = new xmldb_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
2628 /// Launch add key instancefk
2629 $dbman->add_key($table, $key);
2631 /// Main savepoint reached
2632 upgrade_main_savepoint($result, 2009111600);
2635 if ($result && $oldversion < 2009111700) {
2637 /// Define field tempdataid to be added to portfolio_log
2638 $table = new xmldb_table('portfolio_log');
2639 $field = new xmldb_field('tempdataid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'caller_sha1');
2641 /// Conditionally launch add field tempdataid
2642 if (!$dbman->field_exists($table, $field)) {
2643 $dbman->add_field($table, $field);
2646 /// Main savepoint reached
2647 upgrade_main_savepoint($result, 2009111700);
2650 if ($result && $oldversion < 2009111701) {
2652 /// Define field returnurl to be added to portfolio_log
2653 $table = new xmldb_table('portfolio_log');
2654 $field = new xmldb_field('returnurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'tempdataid');
2656 /// Conditionally launch add field returnurl
2657 if (!$dbman->field_exists($table, $field)) {
2658 $dbman->add_field($table, $field);
2661 /// Main savepoint reached
2662 upgrade_main_savepoint($result, 2009111701);
2665 if ($result && $oldversion < 2009111702) {
2667 /// Define field continueurl to be added to portfolio_log
2668 $table = new xmldb_table('portfolio_log');
2669 $field = new xmldb_field('continueurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'returnurl');
2671 /// Conditionally launch add field continueurl
2672 if (!$dbman->field_exists($table, $field)) {
2673 $dbman->add_field($table, $field);
2676 /// Main savepoint reached
2677 upgrade_main_savepoint($result, 2009111702);
2680 if ($result && $oldversion < 2009112400) {
2681 if (empty($CFG->passwordsaltmain)) {
2682 $subject = get_string('check_passwordsaltmain_name', 'report_security');
2683 $description = get_string('check_passwordsaltmain_warning', 'report_security');;
2684 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2686 upgrade_main_savepoint($result, 2009112400);
2689 if ($result && $oldversion < 2010010601) {
2691 /// Define field creatorid to be added to external_tokens
2692 $table = new xmldb_table('external_tokens');
2693 $field = new xmldb_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'contextid');
2695 /// Conditionally launch add field creatorid
2696 if (!$dbman->field_exists($table, $field)) {
2697 $dbman->add_field($table, $field);
2700 /// Define key creatorid (foreign) to be added to external_tokens
2701 $table = new xmldb_table('external_tokens');
2702 $key = new xmldb_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
2704 /// Launch add key creatorid
2705 $dbman->add_key($table, $key);
2707 /// Main savepoint reached
2708 upgrade_main_savepoint($result, 2010010601);
2711 if ($result && $oldversion < 2010011200) {
2712 $table = new xmldb_table('grade_categories');
2713 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
2715 if (!$dbman->field_exists($table, $field)) {
2716 $dbman->add_field($table, $field);
2719 upgrade_main_savepoint($result, 2010011200);
2723 if ($result && $oldversion < 2010012500) {
2724 upgrade_fix_incorrect_mnethostids();
2725 upgrade_main_savepoint($result, 2010012500);
2728 if ($result && $oldversion < 2010012600) {
2729 // do stuff to the mnet table
2730 $table = new xmldb_table('mnet_rpc');
2732 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2733 $dbman->rename_field($table, $field, 'plugintype');
2735 $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2736 $dbman->rename_field($table, $field, 'pluginname');
2738 $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
2739 if (!$dbman->field_exists($table, $field)) {
2740 $dbman->add_field($table, $field);
2743 $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
2744 if (!$dbman->field_exists($table, $field)) {
2745 $dbman->add_field($table, $field);
2748 $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
2749 if (!$dbman->field_exists($table, $field)) {
2750 $dbman->add_field($table, $field);
2753 /// Main savepoint reached
2754 upgrade_main_savepoint($result, 2010012600);
2757 if ($result && $oldversion < 2010012900) {
2759 /// Define table mnet_remote_rpc to be created
2760 $table = new xmldb_table('mnet_remote_rpc');
2762 /// Adding fields to table mnet_remote_rpc
2763 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2764 $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2765 $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
2767 /// Adding keys to table mnet_remote_rpc
2768 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2770 /// Conditionally launch create table for mnet_remote_rpc
2771 if (!$dbman->table_exists($table)) {
2772 $dbman->create_table($table);
2776 /// Define table mnet_remote_service2rpc to be created
2777 $table = new xmldb_table('mnet_remote_service2rpc');
2779 /// Adding fields to table mnet_remote_service2rpc
2780 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2781 $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2782 $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2784 /// Adding keys to table mnet_remote_service2rpc
2785 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2787 /// Adding indexes to table mnet_remote_service2rpc
2788 $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2790 /// Conditionally launch create table for mnet_remote_service2rpc
2791 if (!$dbman->table_exists($table)) {
2792 $dbman->create_table($table);
2796 /// Rename field function_name on table mnet_rpc to functionname
2797 $table = new xmldb_table('mnet_rpc');
2798 $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
2800 /// Launch rename field function_name
2801 $dbman->rename_field($table, $field, 'functionname');
2804 /// Rename field xmlrpc_path on table mnet_rpc to xmlrpcpath
2805 $table = new xmldb_table('mnet_rpc');
2806 $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
2808 /// Launch rename field xmlrpc_path
2809 $dbman->rename_field($table, $field, 'xmlrpcpath');
2812 /// Main savepoint reached
2813 upgrade_main_savepoint($result, 2010012900);
2816 if ($result && $oldversion < 2010012901) {
2818 /// Define field plugintype to be added to mnet_remote_rpc
2819 $table = new xmldb_table('mnet_remote_rpc');
2820 $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
2822 /// Conditionally launch add field plugintype
2823 if (!$dbman->field_exists($table, $field)) {
2824 $dbman->add_field($table, $field);
2827 /// Define field pluginname to be added to mnet_remote_rpc
2828 $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
2830 /// Conditionally launch add field pluginname
2831 if (!$dbman->field_exists($table, $field)) {
2832 $dbman->add_field($table, $field);
2835 /// Main savepoint reached
2836 upgrade_main_savepoint($result, 2010012901);
2839 if ($result && $oldversion < 2010012902) {
2841 /// Define field enabled to be added to mnet_remote_rpc
2842 $table = new xmldb_table('mnet_remote_rpc');
2843 $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
2845 /// Conditionally launch add field enabled
2846 if (!$dbman->field_exists($table, $field)) {
2847 $dbman->add_field($table, $field);
2850 /// Main savepoint reached
2851 upgrade_main_savepoint($result, 2010012902);
2854 /// MDL-17863. Increase the portno column length on mnet_host to handle any port number
2855 if ($result && $oldversion < 2010020100) {
2856 /// Changing precision of field portno on table mnet_host to (5)
2857 $table = new xmldb_table('mnet_host');
2858 $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
2860 /// Launch change of precision for field portno
2861 $dbman->change_field_precision($table, $field);
2863 upgrade_main_savepoint($result, 2010020100);
2866 if ($result && $oldversion < 2010020300) {
2868 /// Define field timecreated to be added to user
2869 $table = new xmldb_table('user');
2870 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
2872 if (!$dbman->field_exists($table, $field)) {
2873 /// Launch add field timecreated
2874 $dbman->add_field($table, $field);
2876 $DB->execute("UPDATE {user} SET timecreated = firstaccess");
2878 $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
2881 upgrade_main_savepoint($result, 2010020300);
2884 // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
2885 if ($result && $oldversion < 2010020301) {
2886 if ($preamble = $CFG->filter_tex_latexpreamble) {
2887 $preamble = preg_replace('/^ +/m', '', $preamble);
2888 set_config('filter_tex_latexpreamble', $preamble);
2890 upgrade_main_savepoint($result, 2010020301);
2893 if ($result && $oldversion < 2010021400) {
2894 /// Changes to modinfo mean we need to rebuild course cache
2895 require_once($CFG->dirroot . '/course/lib.php');
2896 rebuild_course_cache(0, true);
2897 upgrade_main_savepoint($result, 2010021400);
2900 if ($result && $oldversion < 2010021800) {
2901 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
2902 upgrade_main_savepoint($result, 2010021800);
2905 if ($result && $oldversion < 2010031900) {
2906 // regeneration of sessions is always enabled, no need for this setting any more
2907 unset_config('regenloginsession');
2908 upgrade_main_savepoint($result, 2010031900);
2912 if ($result && $oldversion < 2010032400) {
2913 // Upgrade all of those using the standardold theme to the use the standard
2915 if ($CFG->theme == 'standardold') {
2916 // The config setting that affects the whole site
2917 set_config('theme', 'standard');
2919 // Course Categories
2920 $DB->execute('UPDATE {course_categories} SET theme=? WHERE theme=?', array('standard', 'standardold'));
2922 $DB->execute('UPDATE {course} SET theme=? WHERE theme=?', array('standard', 'standardold'));
2924 $DB->execute('UPDATE {user} SET theme=? WHERE theme=?', array('standard', 'standardold'));
2925 upgrade_main_savepoint($result, 2010032400);
2928 if ($result && $oldversion < 2010033101.01) {
2930 /// Define field source to be added to files
2931 $table = new xmldb_table('files');
2933 $field = new xmldb_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'status');
2935 /// Conditionally launch add field source
2936 if (!$dbman->field_exists($table, $field)) {
2937 $dbman->add_field($table, $field);
2940 $field = new xmldb_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'source');
2942 /// Conditionally launch add field author
2943 if (!$dbman->field_exists($table, $field)) {
2944 $dbman->add_field($table, $field);
2947 $field = new xmldb_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'author');
2949 /// Conditionally launch add field license
2950 if (!$dbman->field_exists($table, $field)) {
2951 $dbman->add_field($table, $field);
2954 upgrade_main_savepoint($result, 2010033101.01);
2957 if ($result && $oldversion < 2010033101.02) {
2959 /// Define table license to be created
2960 $table = new xmldb_table('license');
2962 /// Adding fields to table license
2963 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2964 $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2965 $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2966 $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2967 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
2968 $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2970 /// Adding keys to table license
2971 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2973 /// Conditionally launch create table for license
2974 if (!$dbman->table_exists($table)) {
2975 $dbman->create_table($table);
2977 $active_licenses = array();
2979 $license = new stdclass;
2981 // add unknown license
2982 $license->shortname = 'unknown';
2983 $license->fullname = 'Unknown license';
2984 $license->source = '';
2985 $license->enabled = 1;
2986 $license->version = '2010033100';
2987 $active_licenses[] = $license->shortname;
2988 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2989 if ($record->version < $license->version) {
2990 // update license record
2991 $license->enabled = $record->enabled;
2992 $license->id = $record->id;
2993 $DB->update_record('license', $license);
2996 $DB->insert_record('license', $license);
2999 // add all rights reserved license
3000 $license->shortname = 'allrightsreserved';
3001 $license->fullname = 'All rights reserved';
3002 $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
3003 $license->enabled = 1;
3004 $license->version = '2010033100';
3005 $active_licenses[] = $license->shortname;
3006 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3007 if ($record->version < $license->version) {
3008 // update license record
3009 $license->id = $record->id;
3010 $license->enabled = $record->enabled;
3011 $DB->update_record('license', $license);
3014 $DB->insert_record('license', $license);
3017 // add public domain license
3018 $license->shortname = 'public';
3019 $license->fullname = 'Public Domain';
3020 $license->source = 'http://creativecommons.org/licenses/publicdomain/';
3021 $license->enabled = 1;
3022 $license->version = '2010033100';
3023 $active_licenses[] = $license->shortname;
3024 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3025 if ($record->version < $license->version) {
3026 // update license record
3027 $license->enabled = $record->enabled;
3028 $license->id = $record->id;
3029 $DB->update_record('license', $license);
3032 $DB->insert_record('license', $license);
3035 // add creative commons license
3036 $license->shortname = 'cc';
3037 $license->fullname = 'Creative Commons';
3038 $license->source = 'http://creativecommons.org/licenses/by/3.0/';
3039 $license->enabled = 1;
3040 $license->version = '2010033100';
3041 $active_licenses[] = $license->shortname;
3042 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3043 if ($record->version < $license->version) {
3044 // update license record
3045 $license->enabled = $record->enabled;
3046 $license->id = $record->id;
3047 $DB->update_record('license', $license);
3050 $DB->insert_record('license', $license);
3053 // add creative commons no derivs license
3054 $license->shortname = 'cc-nd';
3055 $license->fullname = 'Creative Commons - NoDerivs';
3056 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3057 $license->enabled = 1;
3058 $license->version = '2010033100';
3059 $active_licenses[] = $license->shortname;
3060 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3061 if ($record->version < $license->version) {
3062 // update license record
3063 $license->enabled = $record->enabled;
3064 $license->id = $record->id;
3065 $DB->update_record('license', $license);
3068 $DB->insert_record('license', $license);
3071 // add creative commons no commercial no derivs license
3072 $license->shortname = 'cc-nc-nd';
3073 $license->fullname = 'Creative Commons - No Commercial NoDerivs';
3074 $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
3075 $license->enabled = 1;
3076 $license->version = '2010033100';
3077 $active_licenses[] = $license->shortname;
3078 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3079 if ($record->version < $license->version) {
3080 // update license record
3081 $license->enabled = $record->enabled;
3082 $license->id = $record->id;
3083 $DB->update_record('license', $license);
3086 $DB->insert_record('license', $license);
3089 // add creative commons no commercial
3090 $license->shortname = 'cc-nc-nd';
3091 $license->shortname = 'cc-nc';
3092 $license->fullname = 'Creative Commons - No Commercial';
3093 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3094 $license->enabled = 1;
3095 $license->version = '2010033100';
3096 $active_licenses[] = $license->shortname;
3097 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3098 if ($record->version < $license->version) {
3099 // update license record
3100 $license->enabled = $record->enabled;
3101 $license->id = $record->id;
3102 $DB->update_record('license', $license);
3105 $DB->insert_record('license', $license);
3108 // add creative commons no commercial sharealike
3109 $license->shortname = 'cc-nc-sa';
3110 $license->fullname = 'Creative Commons - No Commercial ShareAlike';
3111 $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
3112 $license->enabled = 1;
3113 $license->version = '2010033100';
3114 $active_licenses[] = $license->shortname;
3115 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3116 if ($record->version < $license->version) {
3117 // update license record
3118 $license->enabled = $record->enabled;
3119 $license->id = $record->id;
3120 $DB->update_record('license', $license);
3123 $DB->insert_record('license', $license);
3126 // add creative commons sharealike
3127 $license->shortname = 'cc-sa';
3128 $license->fullname = 'Creative Commons - ShareAlike';
3129 $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
3130 $license->enabled = 1;
3131 $license->version = '2010033100';
3132 $active_licenses[] = $license->shortname;
3133 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3134 if ($record->version < $license->version) {
3135 // update license record
3136 $license->enabled = $record->enabled;
3137 $license->id = $record->id;
3138 $DB->update_record('license', $license);
3141 $DB->insert_record('license', $license);
3144 set_config('licenses', implode(',', $active_licenses));
3145 /// set site default license
3146 set_config('sitedefaultlicense', 'allrightsreserved');
3148 /// Main savepoint reached
3149 upgrade_main_savepoint($result, 2010033101.02);
3152 if ($result && $oldversion < 2010033102.00) {
3153 // rename course view capability to participate
3154 $params = array('view'=>'moodle/course:view', 'participate'=>'moodle/course:participate');
3155 $sql = "UPDATE {role_capabilities} SET capability = :participate WHERE capability = :view";
3156 $DB->execute($sql, $params);
3157 $sql = "UPDATE {capabilities} SET name = :participate WHERE name = :view";
3158 $DB->execute($sql, $params);
3159 // note: the view capability is readded again at the end of upgrade, but with different meaning
3160 upgrade_main_savepoint($result, 2010033102.00);
3163 if ($result && $oldversion < 2010033102.01) {
3164 // Define field archetype to be added to role table
3165 $table = new xmldb_table('role');
3166 $field = new xmldb_field('archetype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'sortorder');
3167 $dbman->add_field($table, $field);
3168 upgrade_main_savepoint($result, 2010033102.01);
3171 if ($result && $oldversion < 2010033102.02) {
3172 // Set archetype for existing roles and change admin role to manager role
3173 $sql = "SELECT r.*, rc.capability
3175 JOIN {role_capabilities} rc ON rc.roleid = r.id
3176 WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
3178 $params = array('syscontextid'=>SYSCONTEXTID, 'legacycaps'=>'moodle/legacy:%');
3179 $substart = strlen('moodle/legacy:');
3180 $roles = $DB->get_recordset_sql($sql, $params); // in theory could be multiple legacy flags in one role
3181 foreach ($roles as $role) {
3182 $role->archetype = substr($role->capability, $substart);
3183 unset($role->capability);
3184 if ($role->archetype === 'admin') {
3185 $role->archetype = 'manager';
3186 if ($role->shortname === 'admin') {
3187 $role->shortname = 'manager';
3188 $role->name = get_string('manager', 'role');
3189 $role->description = get_string('managerdescription', 'role');
3192 $DB->update_record('role', $role);
3196 upgrade_main_savepoint($result, 2010033102.02);
3199 if ($result && $oldversion < 2010033102.03) {
3200 // Now pick site admins (===have manager role assigned at the system context)
3201 // and store them in the new $CFG->siteadmins setting as comma separated list
3202 $sql = "SELECT ra.id, ra.userid
3203 FROM {role_assignments} ra
3204 JOIN {role} r ON r.id = ra.roleid
3205 JOIN {user} u ON u.id = ra.userid
3206 WHERE ra.contextid = :syscontext AND r.archetype = 'manager' AND u.deleted = 0
3208 $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
3210 foreach ($ras as $ra) {
3211 $admins[$ra->userid] = $ra->userid;
3212 set_config('siteadmins', implode(',', $admins)); // better to save it repeatedly, we do need at least one admin
3213 $DB->delete_records('role_assignments', array('id'=>$ra->id));
3216 upgrade_main_savepoint($result, 2010033102.03);
3219 if ($result && $oldversion < 2010033102.04) {
3220 // clean up the manager roles
3221 $managers = $DB->get_records('role', array('archetype'=>'manager'));
3222 foreach ($managers as $manager) {
3223 // now sanitize the capabilities and overrides
3224 $DB->delete_records('role_capabilities', array('capability'=>'moodle/site:config', 'roleid'=>$manager->id)); // only site admins may configure servers
3225 // note: doanything and legacy caps are deleted automatically, they get moodle/course:view later at the end of the upgrade
3227 // remove manager role assignments bellow the course context level - admin role was never intended for activities and blocks,
3228 // the problem is that those assignments would not be visible after upgrade and old style admins in activities make no sense anyway
3229 $DB->delete_records_select('role_assignments', "roleid = :manager AND contextid IN (SELECT id FROM {context} WHERE contextlevel > 50)", array('manager'=>$manager->id));
3231 // allow them to assign all roles except default user, guest and frontpage - users get these roles automatically on the fly when needed
3232 $DB->delete_records('role_allow_assign', array('roleid'=>$manager->id));
3233 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype <> 'user' AND archetype <> 'guest' AND archetype <> 'frontpage'");
3234 foreach ($roles as $role) {
3235 $record = (object)array('roleid'=>$manager->id, 'allowassign'=>$role->id);
3236 $DB->insert_record('role_allow_assign', $record);
3239 // allow them to override all roles
3240 $DB->delete_records('role_allow_override', array('roleid'=>$manager->id));
3241 $roles = $DB->get_records_sql("SELECT * FROM {role}");
3242 foreach ($roles as $role) {
3243 $record = (object)array('roleid'=>$manager->id, 'allowoverride'=>$role->id);
3244 $DB->insert_record('role_allow_override', $record);
3247 // allow them to switch to all following roles
3248 $DB->delete_records('role_allow_switch', array('roleid'=>$manager->id));
3249 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype IN ('student', 'teacher', 'editingteacher')");
3250 foreach ($roles as $role) {
3251 $record = (object)array('roleid'=>$manager->id, 'allowswitch'=>$role->id);
3252 $DB->insert_record('role_allow_switch', $record);
3256 upgrade_main_savepoint($result, 2010033102.04);
3259 if ($result && $oldversion < 2010033102.05) {
3260 // remove course:view from all roles that are not used for enrolment, it does NOT belong there because it really means user is enrolled!
3261 $noenrolroles = $DB->get_records_select('role', "archetype IN ('guest', 'user', 'manager', 'coursecreator', 'frontpage')");
3262 foreach ($noenrolroles as $role) {
3263 $DB->delete_records('role_capabilities', array('roleid'=>$role->id, 'capability'=>'moodle/course:participate'));
3265 upgrade_main_savepoint($result, 2010033102.05);
3268 if ($result && $oldversion < 2010033102.06) {
3269 // make sure there is nothing weird in default user role
3270 if (!empty($CFG->defaultuserroleid)) {
3271 if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
3272 if ($role->archetype !== '' and $role->archetype !== 'user') {
3273 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default authenticated user role (defaultuserroleid) value is invalid, setting cleared.');
3274 unset_config('defaultuserroleid');
3277 unset_config('defaultuserroleid');
3280 upgrade_main_savepoint($result, 2010033102.06);
3283 if ($result && $oldversion < 2010033102.07) {
3284 if (!empty($CFG->displayloginfailures) and $CFG->displayloginfailures === 'teacher') {
3285 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Displaying of login failuters to teachers is not supported any more.');
3286 unset_config('displayloginfailures');
3288 upgrade_main_savepoint($result, 2010033102.07);
3291 if ($result && $oldversion < 2010033102.08) {
3292 // make sure there are no problems in default guest role settings
3293 if (!empty($CFG->guestroleid)) {
3294 if ($role = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
3295 if ($role->archetype !== '' and $role->archetype !== 'guest') {
3296 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default guest role (guestroleid) value is invalid, setting cleared.');
3297 unset_config('guestroleid');
3300 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) does not exist, setting cleared.');
3301 unset_config('guestroleid');
3304 // remove all roles of the guest account - the only way to change it is to override the guest role, sorry
3305 // the guest account gets all the role assignments on the fly which works fine in has_capability(),
3306 $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE username = 'guest')");
3308 upgrade_main_savepoint($result, 2010033102.08);
3311 /// New table for storing which roles can be assigned in which contexts.
3312 if ($result && $oldversion < 2010033102.09) {
3314 /// Define table role_context_levels to be created
3315 $table = new xmldb_table('role_context_levels');
3317 /// Adding fields to table role_context_levels
3318 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3319 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3320 $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3322 /// Adding keys to table role_context_levels
3323 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3324 $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
3325 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
3327 /// Conditionally launch create table for role_context_levels
3328 if (!$dbman->table_exists($table)) {
3329 $dbman->create_table($table);
3332 /// Main savepoint reached
3333 upgrade_main_savepoint($result, 2010033102.09);
3336 if ($result && $oldversion < 2010033102.10) {
3337 // Now populate the role_context_levels table with the default values
3338 // NOTE: do not use accesslib methods here
3340 $rolecontextlevels = array();
3341 $defaults = array('manager' => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE),
3342 'coursecreator' => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT),
3343 'editingteacher' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3344 'teacher' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3345 'student' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3348 'frontpage' => array());
3350 $roles = $DB->get_records('role', array(), '', 'id, archetype');
3351 foreach ($roles as $role) {
3352 if (isset($defaults[$role->archetype])) {
3353 $rolecontextlevels[$role->id] = $defaults[$role->archetype];
3357 // add roles without archetypes, it may contain weird things, but we can not fix them
3358 $sql = "SELECT DISTINCT ra.roleid, con.contextlevel
3359 FROM {role_assignments} ra
3360 JOIN {context} con ON ra.contextid = con.id";
3361 $existingrolecontextlevels = $DB->get_recordset_sql($sql);
3362 foreach ($existingrolecontextlevels as $rcl) {
3363 if (isset($roleids[$rcl->roleid])) {
3366 if (!isset($rolecontextlevels[$rcl->roleid])) {
3367 $rolecontextlevels[$rcl->roleid] = array($rcl->contextlevel);
3368 } else if (!in_array($rcl->contextlevel, $rolecontextlevels[$rcl->roleid])) {
3369 $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
3372 $existingrolecontextlevels->close();
3374 // Put the data into the database.
3375 $rcl = new object();
3376 foreach ($rolecontextlevels as $roleid => $contextlevels) {
3377 $rcl->roleid = $roleid;
3378 foreach ($contextlevels as $level) {
3379 $rcl->contextlevel = $level;
3380 $DB->insert_record('role_context_levels', $rcl, false);
3388 unset($existingrolecontextlevels);
3389 unset($rolecontextlevels);
3391 // Main savepoint reached
3392 upgrade_main_savepoint($result, 2010033102.10);
3395 if ($result && $oldversion < 2010040700) {
3396 // migrate old groupings --> groupmembersonly setting
3397 if (isset($CFG->enablegroupings)) {
3398 set_config('enablegroupmembersonly', $CFG->enablegroupings);
3399 unset_config('enablegroupings');
3402 // Main savepoint reached
3403 upgrade_main_savepoint($result, 2010040700);
3406 if ($result && $oldversion < 2010040900) {
3408 // Changing the default of field lang on table user to good old "en"
3409 $table = new xmldb_table('user');
3410 $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en', 'country');
3412 // Launch change of default for field lang
3413 $dbman->change_field_default($table, $field);
3415 // update main site lang
3416 if (strpos($CFG->lang, '_utf8') !== false) {
3417 $lang = str_replace('_utf8', '', $CFG->lang);
3418 set_config('lang', $lang);
3422 if (!empty($CFG->langlist)) {
3423 $langs = explode(',', $CFG->langlist);