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 /// New table for storing which roles can be assigned in which contexts.
901 if ($result && $oldversion < 2008110601) {
903 /// Define table role_context_levels to be created
904 $table = new xmldb_table('role_context_levels');
906 /// Adding fields to table role_context_levels
907 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
908 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
909 $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
911 /// Adding keys to table role_context_levels
912 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
913 $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
914 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
916 /// Conditionally launch create table for role_context_levels
917 if (!$dbman->table_exists($table)) {
918 $dbman->create_table($table);
921 /// Main savepoint reached
922 upgrade_main_savepoint($result, 2008110601);
925 /// Now populate the role_context_levels table with the defaults that match
926 /// moodle_install_roles, and any other combinations that exist in this system.
927 if ($result && $oldversion < 2008110602) {
928 $roleids = $DB->get_records_menu('role', array(), '', 'shortname,id');
930 /// Defaults, should match moodle_install_roles.
931 $rolecontextlevels = array();
932 if (isset($roleids['coursecreator'])) {
933 $rolecontextlevels[$roleids['coursecreator']] = get_default_contextlevels('coursecreator');
935 if (isset($roleids['editingteacher'])) {
936 $rolecontextlevels[$roleids['editingteacher']] = get_default_contextlevels('editingteacher');
938 if (isset($roleids['teacher'])) {
939 $rolecontextlevels[$roleids['teacher']] = get_default_contextlevels('teacher');
941 if (isset($roleids['student'])) {
942 $rolecontextlevels[$roleids['student']] = get_default_contextlevels('student');
944 if (isset($roleids['guest'])) {
945 $rolecontextlevels[$roleids['guest']] = get_default_contextlevels('guest');
947 if (isset($roleids['user'])) {
948 $rolecontextlevels[$roleids['user']] = get_default_contextlevels('user');
951 /// See what other role assignments are in this database, extend the allowed
952 /// lists to allow them too.
953 $existingrolecontextlevels = $DB->get_recordset_sql('SELECT DISTINCT ra.roleid, con.contextlevel FROM
954 {role_assignments} ra JOIN {context} con ON ra.contextid = con.id');
955 foreach ($existingrolecontextlevels as $rcl) {
956 if (!isset($rolecontextlevels[$rcl->roleid])) {
957 $rolecontextlevels[$rcl->roleid] = array($rcl->contextlevel);
958 } else if (!in_array($rcl->contextlevel, $rolecontextlevels[$rcl->roleid])) {
959 $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
963 /// Put the data into the database.
964 foreach ($rolecontextlevels as $roleid => $contextlevels) {
965 set_role_contextlevels($roleid, $contextlevels);
968 /// Main savepoint reached
969 upgrade_main_savepoint($result, 2008110602);
972 /// Drop the deprecated teacher, teachers, student and students columns from the course table.
973 if ($result && $oldversion < 2008111200) {
974 $table = new xmldb_table('course');
976 /// Conditionally launch drop field teacher
977 $field = new xmldb_field('teacher');
978 if ($dbman->field_exists($table, $field)) {
979 $dbman->drop_field($table, $field);
982 /// Conditionally launch drop field teacher
983 $field = new xmldb_field('teachers');
984 if ($dbman->field_exists($table, $field)) {
985 $dbman->drop_field($table, $field);
988 /// Conditionally launch drop field teacher
989 $field = new xmldb_field('student');
990 if ($dbman->field_exists($table, $field)) {
991 $dbman->drop_field($table, $field);
994 /// Conditionally launch drop field teacher
995 $field = new xmldb_field('students');
996 if ($dbman->field_exists($table, $field)) {
997 $dbman->drop_field($table, $field);
1000 /// Main savepoint reached
1001 upgrade_main_savepoint($result, 2008111200);
1004 /// Add a unique index to the role.name column.
1005 if ($result && $oldversion < 2008111800) {
1007 /// Define index name (unique) to be added to role
1008 $table = new xmldb_table('role');
1009 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
1011 /// Conditionally launch add index name
1012 if (!$dbman->index_exists($table, $index)) {
1013 $dbman->add_index($table, $index);
1016 /// Main savepoint reached
1017 upgrade_main_savepoint($result, 2008111800);
1020 /// Add a unique index to the role.shortname column.
1021 if ($result && $oldversion < 2008111801) {
1023 /// Define index shortname (unique) to be added to role
1024 $table = new xmldb_table('role');
1025 $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
1027 /// Conditionally launch add index shortname
1028 if (!$dbman->index_exists($table, $index)) {
1029 $dbman->add_index($table, $index);
1032 /// Main savepoint reached
1033 upgrade_main_savepoint($result, 2008111801);
1036 if ($result && $oldversion < 2008120700) {
1038 /// Changing precision of field shortname on table course_request to (100)
1039 $table = new xmldb_table('course_request');
1040 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
1042 /// Before changing the field, drop dependent indexes
1043 /// Define index shortname (not unique) to be dropped form course_request
1044 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
1045 /// Conditionally launch drop index shortname
1046 if ($dbman->index_exists($table, $index)) {
1047 $dbman->drop_index($table, $index);
1050 /// Launch change of precision for field shortname
1051 $dbman->change_field_precision($table, $field);
1053 /// After changing the field, recreate dependent indexes
1054 /// Define index shortname (not unique) to be added to course_request
1055 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
1056 /// Conditionally launch add index shortname
1057 if (!$dbman->index_exists($table, $index)) {
1058 $dbman->add_index($table, $index);
1061 /// Main savepoint reached
1062 upgrade_main_savepoint($result, 2008120700);
1065 if ($result && $oldversion < 2008120801) {
1067 /// Changing precision of field shortname on table mnet_enrol_course to (100)
1068 $table = new xmldb_table('mnet_enrol_course');
1069 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
1071 /// Launch change of precision for field shortname
1072 $dbman->change_field_precision($table, $field);
1074 /// Main savepoint reached
1075 upgrade_main_savepoint($result, 2008120801);
1078 if ($result && $oldversion < 2008121701) {
1080 /// Define field availablefrom to be added to course_modules
1081 $table = new xmldb_table('course_modules');
1082 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
1084 /// Conditionally launch add field availablefrom
1085 if (!$dbman->field_exists($table, $field)) {
1086 $dbman->add_field($table, $field);
1089 /// Define field availableuntil to be added to course_modules
1090 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
1092 /// Conditionally launch add field availableuntil
1093 if (!$dbman->field_exists($table, $field)) {
1094 $dbman->add_field($table, $field);
1097 /// Define field showavailability to be added to course_modules
1098 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
1100 /// Conditionally launch add field showavailability
1101 if (!$dbman->field_exists($table, $field)) {
1102 $dbman->add_field($table, $field);
1105 /// Define table course_modules_availability to be created
1106 $table = new xmldb_table('course_modules_availability');
1108 /// Adding fields to table course_modules_availability
1109 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1110 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1111 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1112 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
1113 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1114 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1115 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1117 /// Adding keys to table course_modules_availability
1118 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1119 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
1120 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
1121 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
1123 /// Conditionally launch create table for course_modules_availability
1124 if (!$dbman->table_exists($table)) {
1125 $dbman->create_table($table);
1128 /// Changes to modinfo mean we need to rebuild course cache
1129 require_once($CFG->dirroot . '/course/lib.php');
1130 rebuild_course_cache(0, true);
1132 /// For developer upgrades, turn on the conditional activities and completion
1133 /// features automatically (to gain more testing)
1134 //TODO: remove before 2.0 final!
1135 if (debugging('', DEBUG_DEVELOPER)) {
1136 set_config('enableavailability', 1);
1137 set_config('enablecompletion', 1);
1140 /// Main savepoint reached
1141 upgrade_main_savepoint($result, 2008121701);
1144 if ($result && $oldversion < 2009010500) {
1145 /// clean up config table a bit
1146 unset_config('session_error_counter');
1148 /// Main savepoint reached
1149 upgrade_main_savepoint($result, 2009010500);
1152 if ($result && $oldversion < 2009010600) {
1154 /// Define field originalquestion to be dropped from question_states
1155 $table = new xmldb_table('question_states');
1156 $field = new xmldb_field('originalquestion');
1158 /// Conditionally launch drop field originalquestion
1159 if ($dbman->field_exists($table, $field)) {
1160 $dbman->drop_field($table, $field);
1163 /// Main savepoint reached
1164 upgrade_main_savepoint($result, 2009010600);
1167 if ($result && $oldversion < 2009010601) {
1169 /// Changing precision of field ip on table log to (45)
1170 $table = new xmldb_table('log');
1171 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1173 /// Launch change of precision for field ip
1174 $dbman->change_field_precision($table, $field);
1176 /// Main savepoint reached
1177 upgrade_main_savepoint($result, 2009010601);
1180 if ($result && $oldversion < 2009010602) {
1182 /// Changing precision of field lastip on table user to (45)
1183 $table = new xmldb_table('user');
1184 $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
1186 /// Launch change of precision for field lastip
1187 $dbman->change_field_precision($table, $field);
1189 /// Main savepoint reached
1190 upgrade_main_savepoint($result, 2009010602);
1193 if ($result && $oldversion < 2009010603) {
1195 /// Changing precision of field ip_address on table mnet_host to (45)
1196 $table = new xmldb_table('mnet_host');
1197 $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
1199 /// Launch change of precision for field ip_address
1200 $dbman->change_field_precision($table, $field);
1202 /// Main savepoint reached
1203 upgrade_main_savepoint($result, 2009010603);
1206 if ($result && $oldversion < 2009010604) {
1208 /// Changing precision of field ip on table mnet_log to (45)
1209 $table = new xmldb_table('mnet_log');
1210 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1212 /// Launch change of precision for field ip
1213 $dbman->change_field_precision($table, $field);
1215 /// Main savepoint reached
1216 upgrade_main_savepoint($result, 2009010604);
1219 if ($result && $oldversion < 2009011000) {
1221 /// Changing nullability of field configdata on table block_instance to null
1222 $table = new xmldb_table('block_instance');
1223 $field = new xmldb_field('configdata');
1224 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
1226 /// Launch change of nullability for field configdata
1227 $dbman->change_field_notnull($table, $field);
1229 /// Main savepoint reached
1230 upgrade_main_savepoint($result, 2009011000);
1233 if ($result && $oldversion < 2009011100) {
1234 /// Remove unused settings
1235 unset_config('zip');
1236 unset_config('unzip');
1237 unset_config('adminblocks_initialised');
1239 /// Main savepoint reached
1240 upgrade_main_savepoint($result, 2009011100);
1243 if ($result && $oldversion < 2009011101) {
1244 /// Migrate backup settings to core plugin config table
1245 $configs = $DB->get_records('backup_config');
1246 foreach ($configs as $config) {
1247 set_config($config->name, $config->value, 'backup');
1250 /// Define table to be dropped
1251 $table = new xmldb_table('backup_config');
1253 /// Launch drop table for old backup config
1254 $dbman->drop_table($table);
1256 /// Main savepoint reached
1257 upgrade_main_savepoint($result, 2009011101);
1260 if ($result && $oldversion < 2009011303) {
1262 /// Define table config_log to be created
1263 $table = new xmldb_table('config_log');
1265 /// Adding fields to table config_log
1266 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1267 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1268 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1269 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1270 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1271 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1272 $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1274 /// Adding keys to table config_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 config_log
1279 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1281 /// Launch create table for config_log
1282 $dbman->create_table($table);
1284 /// Main savepoint reached
1285 upgrade_main_savepoint($result, 2009011303);
1288 if ($result && $oldversion < 2009011900) {
1290 /// Define table sessions2 to be dropped
1291 $table = new xmldb_table('sessions2');
1293 /// Conditionally launch drop table for sessions
1294 if ($dbman->table_exists($table)) {
1295 $dbman->drop_table($table);
1298 /// Define table sessions to be dropped
1299 $table = new xmldb_table('sessions');
1301 /// Conditionally launch drop table for sessions
1302 if ($dbman->table_exists($table)) {
1303 $dbman->drop_table($table);
1306 /// Define table sessions to be created
1307 $table = new xmldb_table('sessions');
1309 /// Adding fields to table sessions
1310 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1311 $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1312 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
1313 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1314 $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
1315 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1316 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1317 $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1318 $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1320 /// Adding keys to table sessions
1321 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1322 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1324 /// Adding indexes to table sessions
1325 $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1326 $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1327 $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1328 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1330 /// Launch create table for sessions
1331 $dbman->create_table($table);
1333 /// Main savepoint reached
1334 upgrade_main_savepoint($result, 2009011900);
1337 if ($result && $oldversion < 2009012901) {
1338 // NOTE: this table may already exist, see beginning of this file ;-)
1340 /// Define table upgrade_log to be created
1341 $table = new xmldb_table('upgrade_log');
1343 /// Adding fields to table upgrade_log
1344 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1345 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1346 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1347 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1348 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1349 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1350 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1351 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1352 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1354 /// Adding keys to table upgrade_log
1355 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1356 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1358 /// Adding indexes to table upgrade_log
1359 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1360 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
1362 /// Conditionally launch create table for upgrade_log
1363 if (!$dbman->table_exists($table)) {
1364 $dbman->create_table($table);
1367 /// Main savepoint reached
1368 upgrade_main_savepoint($result, 2009012901);
1371 if ($result && $oldversion < 2009021800) {
1372 // Converting format of grade conditions, if any exist, to percentages.
1374 UPDATE {course_modules_availability} SET grademin=(
1375 SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
1376 /(gi.grademax-gi.grademin)
1377 FROM {grade_items} gi
1378 WHERE gi.id={course_modules_availability}.gradeitemid)
1379 WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1381 UPDATE {course_modules_availability} SET grademax=(
1382 SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
1383 /(gi.grademax-gi.grademin)
1384 FROM {grade_items} gi
1385 WHERE gi.id={course_modules_availability}.gradeitemid)
1386 WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1388 /// Main savepoint reached
1389 upgrade_main_savepoint($result, 2009021800);
1391 if ($result && $oldversion < 2009021801) {
1392 /// Define field backuptype to be added to backup_log
1393 $table = new xmldb_table('backup_log');
1394 $field = new xmldb_field('backuptype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'info');
1395 /// Conditionally Launch add field backuptype and set all old records as 'scheduledbackup' records.
1396 if (!$dbman->field_exists($table, $field)) {
1397 $dbman->add_field($table, $field);
1398 $DB->execute("UPDATE {backup_log} SET backuptype='scheduledbackup'");
1401 /// Main savepoint reached
1402 upgrade_main_savepoint($result, 2009021801);
1404 /// Add default sort order for question types.
1405 if ($result && $oldversion < 2009030300) {
1406 set_config('multichoice_sortorder', 1, 'question');
1407 set_config('truefalse_sortorder', 2, 'question');
1408 set_config('shortanswer_sortorder', 3, 'question');
1409 set_config('numerical_sortorder', 4, 'question');
1410 set_config('calculated_sortorder', 5, 'question');
1411 set_config('essay_sortorder', 6, 'question');
1412 set_config('match_sortorder', 7, 'question');
1413 set_config('randomsamatch_sortorder', 8, 'question');
1414 set_config('multianswer_sortorder', 9, 'question');
1415 set_config('description_sortorder', 10, 'question');
1416 set_config('random_sortorder', 11, 'question');
1417 set_config('missingtype_sortorder', 12, 'question');
1419 upgrade_main_savepoint($result, 2009030300);
1421 if ($result && $oldversion < 2009030501) {
1422 /// setup default repository plugins
1423 require_once($CFG->dirroot . '/repository/lib.php');
1424 repository_setup_default_plugins();
1425 /// Main savepoint reached
1426 upgrade_main_savepoint($result, 2009030501);
1429 /// MDL-18132 replace the use a new Role allow switch settings page, instead of
1430 /// $CFG->allowuserswitchrolestheycantassign
1431 if ($result && $oldversion < 2009032000) {
1432 /// First create the new table.
1433 $table = new xmldb_table('role_allow_switch');
1435 /// Adding fields to table role_allow_switch
1436 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1437 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1438 $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1440 /// Adding keys to table role_allow_switch
1441 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1442 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
1443 $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
1445 /// Adding indexes to table role_allow_switch
1446 $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1448 /// Conditionally launch create table for role_allow_switch
1449 if (!$dbman->table_exists($table)) {
1450 $dbman->create_table($table);
1453 /// Main savepoint reached
1454 upgrade_main_savepoint($result, 2009032000);
1457 if ($result && $oldversion < 2009032001) {
1458 /// Copy from role_allow_assign into the new table.
1459 $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1460 SELECT roleid, allowassign FROM {role_allow_assign}');
1462 /// Unset the config variable used in 1.9.
1463 unset_config('allowuserswitchrolestheycantassign');
1465 /// Main savepoint reached
1466 upgrade_main_savepoint($result, 2009032001);
1469 if ($result && $oldversion < 2009033100) {
1470 require_once("$CFG->dirroot/filter/tex/lib.php");
1471 filter_tex_updatedcallback(null);
1472 /// Main savepoint reached
1473 upgrade_main_savepoint($result, 2009033100);
1476 if ($result && $oldversion < 2009040300) {
1478 /// Define table filter_active to be created
1479 $table = new xmldb_table('filter_active');
1481 /// Adding fields to table filter_active
1482 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1483 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1484 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1485 $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1486 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1488 /// Adding keys to table filter_active
1489 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1490 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1492 /// Adding indexes to table filter_active
1493 $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1495 /// Conditionally launch create table for filter_active
1496 if (!$dbman->table_exists($table)) {
1497 $dbman->create_table($table);
1500 /// Main savepoint reached
1501 upgrade_main_savepoint($result, 2009040300);
1504 if ($result && $oldversion < 2009040301) {
1506 /// Define table filter_config to be created
1507 $table = new xmldb_table('filter_config');
1509 /// Adding fields to table filter_config
1510 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1511 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1512 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1513 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1514 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1516 /// Adding keys to table filter_config
1517 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1518 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1520 /// Adding indexes to table filter_config
1521 $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1523 /// Conditionally launch create table for filter_config
1524 if (!$dbman->table_exists($table)) {
1525 $dbman->create_table($table);
1528 /// Main savepoint reached
1529 upgrade_main_savepoint($result, 2009040301);
1532 if ($result && $oldversion < 2009040302) {
1533 /// Transfer current settings from $CFG->textfilters
1534 $disabledfilters = filter_get_all_installed();
1535 if (empty($CFG->textfilters)) {
1536 $activefilters = array();
1538 $activefilters = explode(',', $CFG->textfilters);
1540 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1542 foreach ($activefilters as $filter) {
1543 filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1545 unset($disabledfilters[$filter]);
1547 foreach ($disabledfilters as $filter => $notused) {
1548 filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1552 /// Main savepoint reached
1553 upgrade_main_savepoint($result, 2009040302);
1556 if ($result && $oldversion < 2009040600) {
1557 /// Ensure that $CFG->stringfilters is set.
1558 if (empty($CFG->stringfilters)) {
1559 if (!empty($CFG->filterall)) {
1560 set_config('stringfilters', $CFG->textfilters);
1562 set_config('stringfilters', '');
1566 set_config('filterall', !empty($CFG->stringfilters));
1567 unset_config('textfilters');
1569 /// Main savepoint reached
1570 upgrade_main_savepoint($result, 2009040600);
1573 if ($result && $oldversion < 2009041700) {
1574 /// To ensure the UI remains consistent with no behaviour change, any
1575 /// 'until' date in an activity condition should have 1 second subtracted
1576 /// (to go from 0:00 on the following day to 23:59 on the previous one).
1577 $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
1578 require_once($CFG->dirroot . '/course/lib.php');
1579 rebuild_course_cache(0, true);
1581 /// Main savepoint reached
1582 upgrade_main_savepoint($result, 2009041700);
1585 if ($result && $oldversion < 2009042600) {
1586 /// Deleting orphaned messages from deleted users.
1587 require_once($CFG->dirroot.'/message/lib.php');
1588 /// Detect deleted users with messages sent(useridfrom) and not read
1589 if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
1591 JOIN {message} m ON m.useridfrom = u.id
1592 WHERE u.deleted = ?', array(1))) {
1593 foreach ($deletedusers as $deleteduser) {
1594 message_move_userfrom_unread2read($deleteduser->id); // move messages
1597 /// Main savepoint reached
1598 upgrade_main_savepoint($result, 2009042600);
1601 /// Dropping all enums/check contraints from core. MDL-18577
1602 if ($result && $oldversion < 2009042700) {
1604 /// Changing list of values (enum) of field stattype on table stats_daily to none
1605 $table = new xmldb_table('stats_daily');
1606 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1608 /// Launch change of list of values for field stattype
1609 $dbman->drop_enum_from_field($table, $field);
1611 /// Changing list of values (enum) of field stattype on table stats_weekly to none
1612 $table = new xmldb_table('stats_weekly');
1613 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1615 /// Launch change of list of values for field stattype
1616 $dbman->drop_enum_from_field($table, $field);
1618 /// Changing list of values (enum) of field stattype on table stats_monthly to none
1619 $table = new xmldb_table('stats_monthly');
1620 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1622 /// Launch change of list of values for field stattype
1623 $dbman->drop_enum_from_field($table, $field);
1625 /// Changing list of values (enum) of field publishstate on table post to none
1626 $table = new xmldb_table('post');
1627 $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
1629 /// Launch change of list of values for field publishstate
1630 $dbman->drop_enum_from_field($table, $field);
1632 /// Main savepoint reached
1633 upgrade_main_savepoint($result, 2009042700);
1636 if ($result && $oldversion < 2009043000) {
1637 unset_config('grade_report_showgroups');
1638 upgrade_main_savepoint($result, 2009043000);
1641 if ($result && $oldversion < 2009050600) {
1642 /// Site front page blocks need to be moved due to page name change.
1643 $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
1645 /// Main savepoint reached
1646 upgrade_main_savepoint($result, 2009050600);
1649 if ($result && $oldversion < 2009050601) {
1651 /// Define table block_instance to be renamed to block_instances
1652 $table = new xmldb_table('block_instance');
1654 /// Launch rename table for block_instance
1655 $dbman->rename_table($table, 'block_instances');
1657 /// Main savepoint reached
1658 upgrade_main_savepoint($result, 2009050601);
1661 if ($result && $oldversion < 2009050602) {
1663 /// Define table block_instance to be renamed to block_instance_old
1664 $table = new xmldb_table('block_pinned');
1666 /// Launch rename table for block_instance
1667 $dbman->rename_table($table, 'block_pinned_old');
1669 /// Main savepoint reached
1670 upgrade_main_savepoint($result, 2009050602);
1673 if ($result && $oldversion < 2009050603) {
1675 /// Define table block_instance_old to be created
1676 $table = new xmldb_table('block_instance_old');
1678 /// Adding fields to table block_instance_old
1679 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1680 $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1681 $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1682 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1683 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
1684 $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
1685 $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
1686 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1687 $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1689 /// Adding keys to table block_instance_old
1690 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1691 $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
1693 /// Adding indexes to table block_instance_old
1694 $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1695 $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
1697 /// Conditionally launch create table for block_instance_old
1698 if (!$dbman->table_exists($table)) {
1699 $dbman->create_table($table);
1702 /// Main savepoint reached
1703 upgrade_main_savepoint($result, 2009050603);
1706 if ($result && $oldversion < 2009050604) {
1707 /// Copy current blocks data from block_instances to block_instance_old
1708 $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
1709 SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
1711 upgrade_main_savepoint($result, 2009050604);
1714 if ($result && $oldversion < 2009050605) {
1716 /// Define field multiple to be dropped from block
1717 $table = new xmldb_table('block');
1718 $field = new xmldb_field('multiple');
1720 /// Conditionally launch drop field multiple
1721 if ($dbman->field_exists($table, $field)) {
1722 $dbman->drop_field($table, $field);
1725 /// Main savepoint reached
1726 upgrade_main_savepoint($result, 2009050605);
1729 if ($result && $oldversion < 2009050606) {
1730 $table = new xmldb_table('block_instances');
1732 /// Rename field weight on table block_instances to defaultweight
1733 $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'position');
1734 $dbman->rename_field($table, $field, 'defaultweight');
1736 /// Rename field position on table block_instances to defaultregion
1737 $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
1738 $dbman->rename_field($table, $field, 'defaultregion');
1740 /// Main savepoint reached
1741 upgrade_main_savepoint($result, 2009050606);
1744 if ($result && $oldversion < 2009050607) {
1745 /// Changing precision of field defaultregion on table block_instances to (16)
1746 $table = new xmldb_table('block_instances');
1747 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
1749 /// Launch change of precision for field defaultregion
1750 $dbman->change_field_precision($table, $field);
1752 /// Main savepoint reached
1753 upgrade_main_savepoint($result, 2009050607);
1756 if ($result && $oldversion < 2009050608) {
1757 /// Change regions to the new notation
1758 $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1759 $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1760 $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1761 // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1762 // flex page course format. Hopefully this new value is an adequate alternative.
1764 /// Main savepoint reached
1765 upgrade_main_savepoint($result, 2009050608);
1768 if ($result && $oldversion < 2009050609) {
1770 /// Define key blockname (unique) to be added to block
1771 $table = new xmldb_table('block');
1772 $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1774 /// Launch add key blockname
1775 $dbman->add_key($table, $key);
1777 /// Main savepoint reached
1778 upgrade_main_savepoint($result, 2009050609);
1781 if ($result && $oldversion < 2009050610) {
1782 $table = new xmldb_table('block_instances');
1784 /// Define field blockname to be added to block_instances
1785 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
1786 if (!$dbman->field_exists($table, $field)) {
1787 $dbman->add_field($table, $field);
1790 /// Define field contextid to be added to block_instances
1791 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
1792 if (!$dbman->field_exists($table, $field)) {
1793 $dbman->add_field($table, $field);
1796 /// Define field showinsubcontexts to be added to block_instances
1797 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
1798 if (!$dbman->field_exists($table, $field)) {
1799 $dbman->add_field($table, $field);
1802 /// Define field subpagepattern to be added to block_instances
1803 $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
1804 if (!$dbman->field_exists($table, $field)) {
1805 $dbman->add_field($table, $field);
1808 /// Main savepoint reached
1809 upgrade_main_savepoint($result, 2009050610);
1812 if ($result && $oldversion < 2009050611) {
1813 $table = new xmldb_table('block_instances');
1815 /// Fill in blockname from blockid
1816 $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1818 /// Set showinsubcontexts = 0 for all rows.
1819 $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1821 /// Main savepoint reached
1822 upgrade_main_savepoint($result, 2009050611);
1825 if ($result && $oldversion < 2009050612) {
1827 /// Rename field pagetype on table block_instances to pagetypepattern
1828 $table = new xmldb_table('block_instances');
1829 $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
1831 /// Launch rename field pagetype
1832 $dbman->rename_field($table, $field, 'pagetypepattern');
1834 /// Main savepoint reached
1835 upgrade_main_savepoint($result, 2009050612);
1838 if ($result && $oldversion < 2009050613) {
1839 /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1842 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
1843 $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1844 pagetypepattern = 'site-index',
1845 subpagepattern = NULL
1846 WHERE pagetypepattern = 'site-index'");
1849 $DB->execute("UPDATE {block_instances} SET
1853 JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1854 WHERE {course}.id = pageid
1856 pagetypepattern = 'course-view-*',
1857 subpagepattern = NULL
1858 WHERE pagetypepattern = 'course-view'");
1861 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1862 $DB->execute("UPDATE {block_instances} SET
1863 contextid = " . $syscontext->id . ",
1864 pagetypepattern = 'admin-*',
1865 subpagepattern = NULL
1866 WHERE pagetypepattern = 'admin'");
1869 $DB->execute("UPDATE {block_instances} SET
1873 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1874 WHERE {user}.id = pageid
1876 pagetypepattern = 'my-index',
1877 subpagepattern = NULL
1878 WHERE pagetypepattern = 'my-index'");
1881 $DB->execute("UPDATE {block_instances} SET
1882 contextid = " . $syscontext->id . ",
1883 pagetypepattern = 'tag-index',
1884 subpagepattern = pageid
1885 WHERE pagetypepattern = 'tag-index'");
1888 $DB->execute("UPDATE {block_instances} SET
1892 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1893 WHERE {user}.id = pageid
1895 pagetypepattern = 'blog-index',
1896 subpagepattern = NULL
1897 WHERE pagetypepattern = 'blog-view'");
1900 $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1901 foreach ($moduleswithblocks as $modname) {
1902 if (!$dbman->table_exists($modname)) {
1905 $DB->execute("UPDATE {block_instances} SET
1909 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1910 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1911 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1912 WHERE {{$modname}}.id = pageid
1914 pagetypepattern = 'blog-index',
1915 subpagepattern = NULL
1916 WHERE pagetypepattern = 'blog-view'");
1919 /// Main savepoint reached
1920 upgrade_main_savepoint($result, 2009050613);
1923 if ($result && $oldversion < 2009050614) {
1924 /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
1925 $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
1927 /// Main savepoint reached
1928 upgrade_main_savepoint($result, 2009050614);
1931 if ($result && $oldversion < 2009050615) {
1932 $table = new xmldb_table('block_instances');
1934 /// Changing nullability of field blockname on table block_instances to not null
1935 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
1936 $dbman->change_field_notnull($table, $field);
1938 /// Changing nullability of field contextid on table block_instances to not null
1939 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
1940 $dbman->change_field_notnull($table, $field);
1942 /// Changing nullability of field showinsubcontexts on table block_instances to not null
1943 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
1944 $dbman->change_field_notnull($table, $field);
1946 /// Main savepoint reached
1947 upgrade_main_savepoint($result, 2009050615);
1950 if ($result && $oldversion < 2009050616) {
1951 /// Add exiting sticky blocks.
1952 $blocks = $DB->get_records('block');
1953 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1954 $newregions = array(
1957 'c' => 'course-view-top',
1959 $stickyblocks = $DB->get_recordset('block_pinned_old');
1960 foreach ($stickyblocks as $stickyblock) {
1961 $newblock = new object();
1962 $newblock->blockname = $blocks[$stickyblock->blockid]->name;
1963 $newblock->contextid = $syscontext->id;
1964 $newblock->showinsubcontexts = 1;
1965 switch ($stickyblock->pagetype) {
1967 $newblock->pagetypepattern = 'course-view-*';
1970 $newblock->pagetypepattern = $stickyblock->pagetype;
1972 $newblock->defaultregion = $newregions[$stickyblock->position];
1973 $newblock->defaultweight = $stickyblock->weight;
1974 $newblock->configdata = $stickyblock->configdata;
1975 $newblock->visible = 1;
1976 $DB->insert_record('block_instances', $newblock);
1979 /// Main savepoint reached
1980 upgrade_main_savepoint($result, 2009050616);
1983 if ($result && $oldversion < 2009050617) {
1985 /// Define table block_positions to be created
1986 $table = new xmldb_table('block_positions');
1988 /// Adding fields to table block_positions
1989 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1990 $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1991 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1992 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1993 $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1994 $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1');
1995 $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1996 $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1998 /// Adding keys to table block_positions
1999 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2000 $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
2001 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2003 /// Adding indexes to table block_positions
2004 $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
2006 /// Conditionally launch create table for block_positions
2007 if (!$dbman->table_exists($table)) {
2008 $dbman->create_table($table);
2011 /// Main savepoint reached
2012 upgrade_main_savepoint($result, 2009050617);
2015 if ($result && $oldversion < 2009050618) {
2016 /// And block instances with visible = 0, copy that information to block_positions
2017 $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
2018 SELECT id, contextid,
2019 CASE WHEN pagetypepattern = 'course-view-*' THEN
2020 (SELECT " . $DB->sql_concat("'course-view-'", 'format') . "
2022 JOIN {context} ON {course}.id = {context}.instanceid
2023 WHERE {context}.id = contextid)
2024 ELSE pagetypepattern END,
2025 CASE WHEN subpagepattern IS NULL THEN ''
2026 ELSE subpagepattern END,
2027 0, defaultregion, defaultweight
2028 FROM {block_instances} WHERE visible = 0 AND pagetypepattern <> 'admin-*'");
2030 /// Main savepoint reached
2031 upgrade_main_savepoint($result, 2009050618);
2034 if ($result && $oldversion < 2009050619) {
2035 $table = new xmldb_table('block_instances');
2037 /// Define field blockid to be dropped from block_instances
2038 $field = new xmldb_field('blockid');
2039 if ($dbman->field_exists($table, $field)) {
2040 /// Before dropping the field, drop dependent indexes
2041 $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
2042 if ($dbman->index_exists($table, $index)) {
2043 /// Launch drop index blockid
2044 $dbman->drop_index($table, $index);
2046 $dbman->drop_field($table, $field);
2049 /// Define field pageid to be dropped from block_instances
2050 $field = new xmldb_field('pageid');
2051 if ($dbman->field_exists($table, $field)) {
2052 /// Before dropping the field, drop dependent indexes
2053 $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
2054 if ($dbman->index_exists($table, $index)) {
2055 /// Launch drop index pageid
2056 $dbman->drop_index($table, $index);
2058 $dbman->drop_field($table, $field);
2061 /// Define field visible to be dropped from block_instances
2062 $field = new xmldb_field('visible');
2063 if ($dbman->field_exists($table, $field)) {
2064 $dbman->drop_field($table, $field);
2067 /// Main savepoint reached
2068 upgrade_main_savepoint($result, 2009050619);
2071 if ($result && $oldversion < 2009051200) {
2072 /// Let's check the status of mandatory mnet_host records, fixing them
2073 /// and moving "orphan" users to default localhost record. MDL-16879
2074 echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
2075 upgrade_fix_incorrect_mnethostids();
2077 /// Main savepoint reached
2078 upgrade_main_savepoint($result, 2009051200);
2082 if ($result && $oldversion < 2009051700) {
2083 /// migrate editor settings
2084 if (empty($CFG->htmleditor)) {
2085 set_config('texteditors', 'textarea');
2087 set_config('texteditors', 'tinymce,textarea');
2090 unset_config('htmleditor');
2091 unset_config('defaulthtmleditor');
2093 /// Main savepoint reached
2094 upgrade_main_savepoint($result, 2009051700);
2097 if ($result && $oldversion < 2009060200) {
2098 /// Define table files_cleanup to be dropped - not needed
2099 $table = new xmldb_table('files_cleanup');
2101 /// Conditionally launch drop table for files_cleanup
2102 if ($dbman->table_exists($table)) {
2103 $dbman->drop_table($table);
2106 /// Main savepoint reached
2107 upgrade_main_savepoint($result, 2009060200);
2110 if ($result && $oldversion < 2009061300) {
2111 //TODO: copy this to the very beginning of this upgrade script so that we may log upgrade queries
2113 /// Define table log_queries to be created
2114 $table = new xmldb_table('log_queries');
2116 /// Adding fields to table log_queries
2117 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2118 $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2119 $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
2120 $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
2121 $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2122 $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2123 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2124 $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
2125 $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2127 /// Adding keys to table log_queries
2128 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2130 /// Conditionally launch create table for log_queries
2131 if (!$dbman->table_exists($table)) {
2132 $dbman->create_table($table);
2135 /// Main savepoint reached
2136 upgrade_main_savepoint($result, 2009061300);
2139 /// Repeat 2009050607 upgrade step, which Petr commented out becuase of XMLDB
2140 /// stupidity, so lots of peopel will have missed.
2141 if ($result && $oldversion < 2009061600) {
2142 /// Changing precision of field defaultregion on table block_instances to (16)
2143 $table = new xmldb_table('block_instances');
2144 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
2146 /// Launch change of precision for field defaultregion
2147 $dbman->change_field_precision($table, $field);
2149 /// Main savepoint reached
2150 upgrade_main_savepoint($result, 2009061600);
2153 if ($result && $oldversion < 2009061702) {
2154 // standardizing plugin names
2155 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
2156 foreach ($configs as $config) {
2157 unset_config($config->name, $config->plugin); /// unset old config
2158 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
2159 set_config($config->name, $config->value, $config->plugin); /// set new config
2163 upgrade_main_savepoint($result, 2009061702);
2166 if ($result && $oldversion < 2009061703) {
2167 // standardizing plugin names
2168 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
2169 foreach ($configs as $config) {
2170 unset_config($config->name, $config->plugin); /// unset old config
2171 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
2172 set_config($config->name, $config->value, $config->plugin); /// set new config
2176 upgrade_main_savepoint($result, 2009061703);
2179 if ($result && $oldversion < 2009061704) {
2180 // change component string in capability records to new "_" format
2181 if ($caps = $DB->get_records('capabilities')) {
2182 foreach ($caps as $cap) {
2183 $cap->component = str_replace('/', '_', $cap->component);
2184 $DB->update_record('capabilities', $cap);
2188 upgrade_main_savepoint($result, 2009061704);
2191 if ($result && $oldversion < 2009061705) {
2192 // change component string in events_handlers records to new "_" format
2193 if ($handlers = $DB->get_records('events_handlers')) {
2194 foreach ($handlers as $handler) {
2195 $handler->handlermodule = str_replace('/', '_', $handler->handlermodule);
2196 $DB->update_record('events_handlers', $handler);
2200 upgrade_main_savepoint($result, 2009061705);
2203 if ($result && $oldversion < 2009061706) {
2204 // change component string in message_providers records to new "_" format
2205 if ($mps = $DB->get_records('message_providers')) {
2206 foreach ($mps as $mp) {
2207 $mp->component = str_replace('/', '_', $mp->component);
2208 $DB->update_record('message_providers', $cap);
2212 upgrade_main_savepoint($result, 2009061706);
2215 if ($result && $oldversion < 2009063000) {
2216 // upgrade format of _with_advanced settings - quiz only
2217 // note: this can be removed later, not needed for upgrades from 1.9.x
2218 if ($quiz = get_config('quiz')) {
2219 foreach ($quiz as $name=>$value) {
2220 if (strpos($name, 'fix_') !== 0) {
2223 $newname = substr($name,4).'_adv';
2224 set_config($newname, $value, 'quiz');
2225 unset_config($name, 'quiz');
2228 upgrade_main_savepoint($result, 2009063000);
2231 if ($result && $oldversion < 2009071000) {
2233 /// Rename field contextid on table block_instances to parentcontextid
2234 $table = new xmldb_table('block_instances');
2235 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
2237 /// Launch rename field parentcontextid
2238 $dbman->rename_field($table, $field, 'parentcontextid');
2240 /// Main savepoint reached
2241 upgrade_main_savepoint($result, 2009071000);
2244 if ($result && $oldversion < 2009071300) {
2246 /// Create contexts for every block. In the past, only non-sticky course block had contexts.
2247 /// This is a copy of the code in create_contexts.
2248 $sql = "INSERT INTO {context} (contextlevel, instanceid)
2249 SELECT " . CONTEXT_BLOCK . ", bi.id
2250 FROM {block_instances} bi
2251 WHERE NOT EXISTS (SELECT 'x'
2253 WHERE bi.id = ctx.instanceid AND ctx.contextlevel=" . CONTEXT_BLOCK . ")";
2256 /// TODO MDL-19776 We should not really use API funcitons in upgrade.
2257 /// If MDL-19776 is done, we can remove this whole upgrade block.
2258 build_context_path();
2260 /// Main savepoint reached
2261 upgrade_main_savepoint($result, 2009071300);
2264 if ($result && $oldversion < 2009071600) {
2266 /// Define field summaryformat to be added to post
2267 $table = new xmldb_table('post');
2268 $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
2270 /// Conditionally launch add field summaryformat
2271 if (!$dbman->field_exists($table, $field)) {
2272 $dbman->add_field($table, $field);
2275 /// Main savepoint reached
2276 upgrade_main_savepoint($result, 2009071600);
2279 if ($result && $oldversion < 2009072400) {
2281 /// Define table comments to be created
2282 $table = new xmldb_table('comments');
2284 /// Adding fields to table comments
2285 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2286 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2287 $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2288 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2289 $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2290 $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2291 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2292 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2294 /// Adding keys to table comments
2295 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2297 /// Conditionally launch create table for comments
2298 if (!$dbman->table_exists($table)) {
2299 $dbman->create_table($table);
2302 /// Main savepoint reached
2303 upgrade_main_savepoint($result, 2009072400);
2307 * This upgrade is to set up the new navigation blocks that have been developed
2308 * as part of Moodle 2.0
2309 * Now I [Sam Hemelryk] hit a conundrum while exploring how to go about this
2310 * as not only do we want to install the new blocks but we also want to set up
2311 * default instances of them, and at the same time remove instances of the blocks
2312 * that were/will-be outmoded by the two new navigation blocks.
2313 * After talking it through with Tim Hunt {@link http://moodle.org/mod/cvsadmin/view.php?conversationid=3112}
2314 * we decided that the best way to go about this was to put the bulk of the
2315 * upgrade operation into core upgrade `here` but to let the plugins block
2316 * still install the blocks.
2317 * This leaves one hairy end in that we will create block_instances within the
2318 * DB before the blocks themselves are created within the DB
2320 if ($result && $oldversion < 2009082800) {
2322 echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2324 // Get the system context so we can set the block instances to it
2325 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2327 // An array to contain the new block instances we will create
2328 $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
2329 // The new global navigation block instance as a stdClass
2330 $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
2331 $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
2332 $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutly everywhere
2333 $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
2334 $newblockinstances['globalnavigation']->subpagetypepattern = null;
2335 $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
2336 $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
2337 $newblockinstances['globalnavigation']->configdata = '';
2338 // The new settings navigation block instance as a stdClass
2339 $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
2340 $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
2341 $newblockinstances['settingsnavigation']->showinsubcontexts = true;
2342 $newblockinstances['settingsnavigation']->pagetypepattern = '*';
2343 $newblockinstances['settingsnavigation']->subpagetypepattern = null;
2344 $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
2345 $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
2346 $newblockinstances['settingsnavigation']->configdata = '';
2348 // Blocks that are outmoded and for whom the bells will toll... by which I
2349 // mean we will delete all instances of
2350 $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
2351 $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
2352 unset($outmodedblocks);
2353 // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
2354 // cut down the number of delete queries we will need to run
2355 $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
2357 $contextids = array();
2358 $instanceids = array();
2359 // Iterate through all block instances
2360 foreach ($allblockinstances as $blockinstance) {
2361 if (!in_array($blockinstance->parentcontextid, $contextids)) {
2362 $contextids[] = $blockinstance->parentcontextid;
2364 // If we have over 1000 contexts clean them up and reset the array
2365 // this ensures we don't hit any nasty memory limits or such
2366 if (count($contextids) > 1000) {
2367 upgrade_cleanup_unwanted_block_contexts($contextids);
2368 $contextids = array();
2371 if (!in_array($blockinstance->id, $instanceids)) {
2372 $instanceids[] = $blockinstance->id;
2373 // If we have more than 1000 block instances now remove all block positions
2374 // and empty the array
2375 if (count($contextids) > 1000) {
2376 $instanceidstring = join(',',$instanceids);
2377 $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2378 $instanceids = array();
2383 upgrade_cleanup_unwanted_block_contexts($contextids);
2385 $instanceidstring = join(',',$instanceids);
2386 $outcome1 = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2388 unset($allblockinstances);
2390 unset($instanceids);
2391 unset($instanceidstring);
2393 // Now remove the actual block instance
2394 $$DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
2395 unset($outmodedblocksstring);
2397 // Insert the new block instances. Remember they have not been installed yet
2398 // however this should not be a problem
2399 foreach ($newblockinstances as $blockinstance) {
2400 $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
2401 // Ensure the block context is created.
2402 get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
2404 unset($newblockinstances);
2406 upgrade_main_savepoint($result, 2009082800);
2407 // The end of the navigation upgrade
2410 if ($result && $oldversion < 2009090800){
2411 //insert new record for log_display table
2412 //used to record tag update.
2413 if (!$DB->record_exists('log_display', array('action'=>'update', 'module'=>'tag'))) {
2414 $log_action = new object();
2415 $log_action->module = 'tag';
2416 $log_action->action = 'update';
2417 $log_action->mtable = 'tag';
2418 $log_action->field = 'name';
2420 $result = $result && $DB->insert_record('log_display', $log_action);
2422 upgrade_main_savepoint($result, 2009090800);
2425 if ($result && $oldversion < 2009100601) {
2426 // drop all previous tables defined during the dev phase
2427 $dropold = array('external_services_users', 'external_services_functions', 'external_services', 'external_functions');
2428 foreach ($dropold as $tablename) {
2429 $table = new xmldb_table($tablename);
2430 if ($dbman->table_exists($table)) {
2431 $dbman->drop_table($table);
2434 upgrade_main_savepoint($result, 2009100601);
2437 if ($result && $oldversion < 2009100602) {
2438 /// Define table external_functions to be created
2439 $table = new xmldb_table('external_functions');
2441 /// Adding fields to table external_functions
2442 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2443 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2444 $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2445 $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2446 $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2447 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2449 /// Adding keys to table external_functions
2450 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2452 /// Adding indexes to table external_functions
2453 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2455 /// Launch create table for external_functions
2456 $dbman->create_table($table);
2458 /// Main savepoint reached
2459 upgrade_main_savepoint($result, 2009100602);
2462 if ($result && $oldversion < 2009100603) {
2463 /// Define table external_services to be created
2464 $table = new xmldb_table('external_services');
2466 /// Adding fields to table external_services
2467 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2468 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2469 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2470 $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
2471 $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2472 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
2474 /// Adding keys to table external_services
2475 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2477 /// Adding indexes to table external_services
2478 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2480 /// Launch create table for external_services
2481 $dbman->create_table($table);
2483 /// Main savepoint reached
2484 upgrade_main_savepoint($result, 2009100603);
2487 if ($result && $oldversion < 2009100604) {
2488 /// Define table external_services_functions to be created
2489 $table = new xmldb_table('external_services_functions');
2491 /// Adding fields to table external_services_functions
2492 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2493 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2494 $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2496 /// Adding keys to table external_services_functions
2497 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2498 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2500 /// Launch create table for external_services_functions
2501 $dbman->create_table($table);
2503 /// Main savepoint reached
2504 upgrade_main_savepoint($result, 2009100604);
2507 if ($result && $oldversion < 2009100605) {
2508 /// Define table external_services_users to be created
2509 $table = new xmldb_table('external_services_users');
2511 /// Adding fields to table external_services_users
2512 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2513 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2514 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2515 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2516 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2517 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2519 /// Adding keys to table external_services_users
2520 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2521 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2522 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2524 /// Launch create table for external_services_users
2525 $dbman->create_table($table);
2527 /// Main savepoint reached
2528 upgrade_main_savepoint($result, 2009100605);
2531 if ($result && $oldversion < 2009102600) {
2533 /// Define table external_tokens to be created
2534 $table = new xmldb_table('external_tokens');
2536 /// Adding fields to table external_tokens
2537 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2538 $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
2539 $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2540 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2541 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2542 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
2543 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2544 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2545 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2546 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2547 $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2549 /// Adding keys to table external_tokens
2550 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2551 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2552 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2553 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2555 /// Launch create table for external_tokens
2556 $dbman->create_table($table);
2558 /// Main savepoint reached
2559 upgrade_main_savepoint($result, 2009102600);
2562 if ($result && $oldversion < 2009103000) {
2564 /// Define table blog_association to be created
2565 $table = new xmldb_table('blog_association');
2567 /// Adding fields to table blog_association
2568 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2569 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2570 $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2572 /// Adding keys to table blog_association
2573 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2574 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2575 $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
2577 /// Conditionally launch create table for blog_association
2578 if (!$dbman->table_exists($table)) {
2579 $dbman->create_table($table);
2582 /// Define table blog_external to be created
2583 $table = new xmldb_table('blog_external');
2585 /// Adding fields to table blog_external
2586 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2587 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2588 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2589 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2590 $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2591 $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2592 $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2593 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2594 $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2596 /// Adding keys to table blog_external
2597 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2598 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2600 /// Conditionally launch create table for blog_external
2601 if ($dbman->table_exists($table)) {
2602 // Delete the existing one first (comes from early dev version)
2603 $dbman->drop_table($table);
2605 $dbman->create_table($table);
2607 // now inform admins that some settings require attention after upgrade
2608 if (($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete)) {
2609 echo $OUTPUT->notification(get_string('bloglevelupgradenotice', 'admin'));
2614 $a->sitename = $site->fullname;
2615 $a->fixurl = "$CFG->wwwroot/$CFG->admin/bloglevelupgrade.php";
2617 $subject = get_string('bloglevelupgrade', 'admin');
2618 $description = get_string('bloglevelupgradedescription', 'admin', $a);
2620 // can not use messaging here because it is not configured yet!
2621 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2623 /// Main savepoint reached
2624 upgrade_main_savepoint($result, 2009103000);
2627 if ($result && $oldversion < 2009110400) {
2629 // An array used to store the table name and keys of summary and trust fields
2631 $extendtables = array();
2632 $extendtables['course'] = array('summaryformat');
2633 $extendtables['course_categories'] = array('descriptionformat');
2634 $extendtables['course_request'] = array('summaryformat');
2635 $extendtables['grade_outcomes'] = array('descriptionformat');
2636 $extendtables['groups'] = array('descriptionformat');
2637 $extendtables['groupings'] = array('descriptionformat');
2638 $extendtables['scale'] = array('descriptionformat');
2639 $extendtables['user'] = array('descriptionformat');
2640 $extendtables['user_info_field'] = array('descriptionformat', 'defaultdataformat');
2641 $extendtables['user_info_data'] = array('dataformat');
2643 foreach ($extendtables as $tablestr=>$newfields) {
2644 $table = new xmldb_table($tablestr);
2645 foreach ($newfields as $fieldstr) {
2646 $field = new xmldb_field($fieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2647 // Check that the field doesn't already exists
2648 if (!$dbman->field_exists($table, $field)) {
2649 // Add the new field
2650 $dbman->add_field($table, $field);
2651 // Update the field if the text contains the default FORMAT_MOODLE to FORMAT_HTML
2652 if (($pos = strpos($fieldstr, 'format'))>0) {
2653 upgrade_set_timeout(60*20); // this may take a little while
2654 $params = array(FORMAT_HTML, '<p%', '%<br />%', FORMAT_MOODLE);
2655 $textfield = substr($fieldstr, 0, $pos);
2656 $DB->execute('UPDATE {'.$tablestr.'} SET '.$fieldstr.'=? WHERE ('.$textfield.' LIKE ? OR '.$textfield.' LIKE ?) AND '.$fieldstr.'=?', $params);
2662 unset($extendtables);
2664 upgrade_main_savepoint($result, 2009110400);
2667 if ($result && $oldversion < 2009110605) {
2669 /// Define field timecreated to be added to external_services
2670 $table = new xmldb_table('external_services');
2671 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'component');
2673 /// Conditionally launch add field timecreated
2674 if (!$dbman->field_exists($table, $field)) {
2675 $dbman->add_field($table, $field);
2678 /// Define field timemodified to be added to external_services
2679 $table = new xmldb_table('external_services');
2680 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'timecreated');
2682 /// Conditionally launch add field timemodified
2683 if (!$dbman->field_exists($table, $field)) {
2684 $dbman->add_field($table, $field);
2687 /// Main savepoint reached
2688 upgrade_main_savepoint($result, 2009110605);
2691 if ($result && $oldversion < 2009111600) {
2693 /// Define field instance to be added to portfolio_tempdata
2694 $table = new xmldb_table('portfolio_tempdata');
2695 $field = new xmldb_field('instance', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'userid');
2697 /// Conditionally launch add field instance
2698 if (!$dbman->field_exists($table, $field)) {
2699 $dbman->add_field($table, $field);
2702 $key = new xmldb_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
2704 /// Launch add key instancefk
2705 $dbman->add_key($table, $key);
2707 /// Main savepoint reached
2708 upgrade_main_savepoint($result, 2009111600);
2711 if ($result && $oldversion < 2009111700) {
2713 /// Define field tempdataid to be added to portfolio_log
2714 $table = new xmldb_table('portfolio_log');
2715 $field = new xmldb_field('tempdataid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'caller_sha1');
2717 /// Conditionally launch add field tempdataid
2718 if (!$dbman->field_exists($table, $field)) {
2719 $dbman->add_field($table, $field);
2722 /// Main savepoint reached
2723 upgrade_main_savepoint($result, 2009111700);
2726 if ($result && $oldversion < 2009111701) {
2728 /// Define field returnurl to be added to portfolio_log
2729 $table = new xmldb_table('portfolio_log');
2730 $field = new xmldb_field('returnurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'tempdataid');
2732 /// Conditionally launch add field returnurl
2733 if (!$dbman->field_exists($table, $field)) {
2734 $dbman->add_field($table, $field);
2737 /// Main savepoint reached
2738 upgrade_main_savepoint($result, 2009111701);
2741 if ($result && $oldversion < 2009111702) {
2743 /// Define field continueurl to be added to portfolio_log
2744 $table = new xmldb_table('portfolio_log');
2745 $field = new xmldb_field('continueurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'returnurl');
2747 /// Conditionally launch add field continueurl
2748 if (!$dbman->field_exists($table, $field)) {
2749 $dbman->add_field($table, $field);
2752 /// Main savepoint reached
2753 upgrade_main_savepoint($result, 2009111702);
2756 if ($result && $oldversion < 2009112400) {
2757 if (empty($CFG->passwordsaltmain)) {
2758 $subject = get_string('check_passwordsaltmain_name', 'report_security');
2759 $description = get_string('check_passwordsaltmain_warning', 'report_security');;
2760 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2762 upgrade_main_savepoint($result, 2009112400);
2765 if ($result && $oldversion < 2010010601) {
2767 /// Define field creatorid to be added to external_tokens
2768 $table = new xmldb_table('external_tokens');
2769 $field = new xmldb_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'contextid');
2771 /// Conditionally launch add field creatorid
2772 if (!$dbman->field_exists($table, $field)) {
2773 $dbman->add_field($table, $field);
2776 /// Define key creatorid (foreign) to be added to external_tokens
2777 $table = new xmldb_table('external_tokens');
2778 $key = new xmldb_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
2780 /// Launch add key creatorid
2781 $dbman->add_key($table, $key);
2783 /// Main savepoint reached
2784 upgrade_main_savepoint($result, 2010010601);
2787 if ($result && $oldversion < 2010011200) {
2788 $table = new xmldb_table('grade_categories');
2789 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
2791 if (!$dbman->field_exists($table, $field)) {
2792 $dbman->add_field($table, $field);
2795 upgrade_main_savepoint($result, 2010011200);
2799 if ($result && $oldversion < 2010012500) {
2800 upgrade_fix_incorrect_mnethostids();
2801 upgrade_main_savepoint($result, 2010012500);
2804 if ($result && $oldversion < 2010012600) {
2805 // do stuff to the mnet table
2806 $table = new xmldb_table('mnet_rpc');
2808 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2809 $dbman->rename_field($table, $field, 'plugintype');
2811 $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2812 $dbman->rename_field($table, $field, 'pluginname');
2814 $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
2815 if (!$dbman->field_exists($table, $field)) {
2816 $dbman->add_field($table, $field);
2819 $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
2820 if (!$dbman->field_exists($table, $field)) {
2821 $dbman->add_field($table, $field);
2824 $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
2825 if (!$dbman->field_exists($table, $field)) {
2826 $dbman->add_field($table, $field);
2829 /// Main savepoint reached
2830 upgrade_main_savepoint($result, 2010012600);
2833 if ($result && $oldversion < 2010012900) {
2835 /// Define table mnet_remote_rpc to be created
2836 $table = new xmldb_table('mnet_remote_rpc');
2838 /// Adding fields to table mnet_remote_rpc
2839 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2840 $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2841 $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
2843 /// Adding keys to table mnet_remote_rpc
2844 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2846 /// Conditionally launch create table for mnet_remote_rpc
2847 if (!$dbman->table_exists($table)) {
2848 $dbman->create_table($table);
2852 /// Define table mnet_remote_service2rpc to be created
2853 $table = new xmldb_table('mnet_remote_service2rpc');
2855 /// Adding fields to table mnet_remote_service2rpc
2856 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2857 $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2858 $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2860 /// Adding keys to table mnet_remote_service2rpc
2861 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2863 /// Adding indexes to table mnet_remote_service2rpc
2864 $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2866 /// Conditionally launch create table for mnet_remote_service2rpc
2867 if (!$dbman->table_exists($table)) {
2868 $dbman->create_table($table);
2872 /// Rename field function_name on table mnet_rpc to functionname
2873 $table = new xmldb_table('mnet_rpc');
2874 $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
2876 /// Launch rename field function_name
2877 $dbman->rename_field($table, $field, 'functionname');
2880 /// Rename field xmlrpc_path on table mnet_rpc to xmlrpcpath
2881 $table = new xmldb_table('mnet_rpc');
2882 $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
2884 /// Launch rename field xmlrpc_path
2885 $dbman->rename_field($table, $field, 'xmlrpcpath');
2888 /// Main savepoint reached
2889 upgrade_main_savepoint($result, 2010012900);
2892 if ($result && $oldversion < 2010012901) {
2894 /// Define field plugintype to be added to mnet_remote_rpc
2895 $table = new xmldb_table('mnet_remote_rpc');
2896 $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
2898 /// Conditionally launch add field plugintype
2899 if (!$dbman->field_exists($table, $field)) {
2900 $dbman->add_field($table, $field);
2903 /// Define field pluginname to be added to mnet_remote_rpc
2904 $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
2906 /// Conditionally launch add field pluginname
2907 if (!$dbman->field_exists($table, $field)) {
2908 $dbman->add_field($table, $field);
2911 /// Main savepoint reached
2912 upgrade_main_savepoint($result, 2010012901);
2915 if ($result && $oldversion < 2010012902) {
2917 /// Define field enabled to be added to mnet_remote_rpc
2918 $table = new xmldb_table('mnet_remote_rpc');
2919 $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
2921 /// Conditionally launch add field enabled
2922 if (!$dbman->field_exists($table, $field)) {
2923 $dbman->add_field($table, $field);
2926 /// Main savepoint reached
2927 upgrade_main_savepoint($result, 2010012902);
2930 /// MDL-17863. Increase the portno column length on mnet_host to handle any port number
2931 if ($result && $oldversion < 2010020100) {
2932 /// Changing precision of field portno on table mnet_host to (5)
2933 $table = new xmldb_table('mnet_host');
2934 $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
2936 /// Launch change of precision for field portno
2937 $dbman->change_field_precision($table, $field);
2939 upgrade_main_savepoint($result, 2010020100);
2942 if ($result && $oldversion < 2010020300) {
2944 /// Define field timecreated to be added to user
2945 $table = new xmldb_table('user');
2946 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
2948 if (!$dbman->field_exists($table, $field)) {
2949 /// Launch add field timecreated
2950 $dbman->add_field($table, $field);
2952 $DB->execute("UPDATE {user} SET timecreated = firstaccess");
2954 $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
2957 upgrade_main_savepoint($result, 2010020300);
2960 // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
2961 if ($result && $oldversion < 2010020301) {
2962 if ($preamble = $CFG->filter_tex_latexpreamble) {
2963 $preamble = preg_replace('/^ +/m', '', $preamble);
2964 set_config('filter_tex_latexpreamble', $preamble);
2966 upgrade_main_savepoint($result, 2010020301);
2969 if ($result && $oldversion < 2010021400) {
2970 /// Changes to modinfo mean we need to rebuild course cache
2971 require_once($CFG->dirroot . '/course/lib.php');
2972 rebuild_course_cache(0, true);
2973 upgrade_main_savepoint($result, 2010021400);
2976 if ($result && $oldversion < 2010021800) {
2977 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
2978 upgrade_main_savepoint($result, 2010021800);
2981 if ($result && $oldversion < 2010031900) {
2982 // regeneration of sessions is always enabled, no need for this setting any more
2983 unset_config('regenloginsession');
2984 upgrade_main_savepoint($result, 2010031900);
2988 if ($result && $oldversion < 2010032400) {
2989 // Upgrade all of those using the standardold theme to the use the standard
2991 if ($CFG->theme == 'standardold') {
2992 // The config setting that affects the whole site
2993 set_config('theme', 'standard');
2995 // Course Categories
2996 $DB->execute('UPDATE {course_categories} SET theme=? WHERE theme=?', array('standard', 'standardold'));
2998 $DB->execute('UPDATE {course} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3000 $DB->execute('UPDATE {user} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3001 upgrade_main_savepoint($result, 2010032400);
3004 if ($result && $oldversion < 2010033101.01) {
3006 /// Define field source to be added to files
3007 $table = new xmldb_table('files');
3009 $field = new xmldb_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'status');
3011 /// Conditionally launch add field source
3012 if (!$dbman->field_exists($table, $field)) {
3013 $dbman->add_field($table, $field);
3016 $field = new xmldb_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'source');
3018 /// Conditionally launch add field author
3019 if (!$dbman->field_exists($table, $field)) {
3020 $dbman->add_field($table, $field);
3023 $field = new xmldb_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'author');
3025 /// Conditionally launch add field license
3026 if (!$dbman->field_exists($table, $field)) {
3027 $dbman->add_field($table, $field);
3030 upgrade_main_savepoint($result, 2010033101.01);
3033 if ($result && $oldversion < 2010033101.02) {
3035 /// Define table license to be created
3036 $table = new xmldb_table('license');
3038 /// Adding fields to table license
3039 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3040 $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3041 $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
3042 $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3043 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
3044 $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
3046 /// Adding keys to table license
3047 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3049 /// Conditionally launch create table for license
3050 if (!$dbman->table_exists($table)) {
3051 $dbman->create_table($table);
3053 $active_licenses = array();
3055 $license = new stdclass;
3057 // add unknown license
3058 $license->shortname = 'unknown';
3059 $license->fullname = 'Unknown license';
3060 $license->source = '';
3061 $license->enabled = 1;
3062 $license->version = '2010033100';
3063 $active_licenses[] = $license->shortname;
3064 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3065 if ($record->version < $license->version) {
3066 // update license record
3067 $license->enabled = $record->enabled;
3068 $license->id = $record->id;
3069 $DB->update_record('license', $license);
3072 $DB->insert_record('license', $license);
3075 // add all rights reserved license
3076 $license->shortname = 'allrightsreserved';
3077 $license->fullname = 'All rights reserved';
3078 $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
3079 $license->enabled = 1;
3080 $license->version = '2010033100';
3081 $active_licenses[] = $license->shortname;
3082 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3083 if ($record->version < $license->version) {
3084 // update license record
3085 $license->id = $record->id;
3086 $license->enabled = $record->enabled;
3087 $DB->update_record('license', $license);
3090 $DB->insert_record('license', $license);
3093 // add public domain license
3094 $license->shortname = 'public';
3095 $license->fullname = 'Public Domain';
3096 $license->source = 'http://creativecommons.org/licenses/publicdomain/';
3097 $license->enabled = 1;
3098 $license->version = '2010033100';
3099 $active_licenses[] = $license->shortname;
3100 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3101 if ($record->version < $license->version) {
3102 // update license record
3103 $license->enabled = $record->enabled;
3104 $license->id = $record->id;
3105 $DB->update_record('license', $license);
3108 $DB->insert_record('license', $license);
3111 // add creative commons license
3112 $license->shortname = 'cc';
3113 $license->fullname = 'Creative Commons';
3114 $license->source = 'http://creativecommons.org/licenses/by/3.0/';
3115 $license->enabled = 1;
3116 $license->version = '2010033100';
3117 $active_licenses[] = $license->shortname;
3118 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3119 if ($record->version < $license->version) {
3120 // update license record
3121 $license->enabled = $record->enabled;
3122 $license->id = $record->id;
3123 $DB->update_record('license', $license);
3126 $DB->insert_record('license', $license);
3129 // add creative commons no derivs license
3130 $license->shortname = 'cc-nd';
3131 $license->fullname = 'Creative Commons - NoDerivs';
3132 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3133 $license->enabled = 1;
3134 $license->version = '2010033100';
3135 $active_licenses[] = $license->shortname;
3136 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3137 if ($record->version < $license->version) {
3138 // update license record
3139 $license->enabled = $record->enabled;
3140 $license->id = $record->id;
3141 $DB->update_record('license', $license);
3144 $DB->insert_record('license', $license);
3147 // add creative commons no commercial no derivs license
3148 $license->shortname = 'cc-nc-nd';
3149 $license->fullname = 'Creative Commons - No Commercial NoDerivs';
3150 $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
3151 $license->enabled = 1;
3152 $license->version = '2010033100';
3153 $active_licenses[] = $license->shortname;
3154 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3155 if ($record->version < $license->version) {
3156 // update license record
3157 $license->enabled = $record->enabled;
3158 $license->id = $record->id;
3159 $DB->update_record('license', $license);
3162 $DB->insert_record('license', $license);
3165 // add creative commons no commercial
3166 $license->shortname = 'cc-nc-nd';
3167 $license->shortname = 'cc-nc';
3168 $license->fullname = 'Creative Commons - No Commercial';
3169 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3170 $license->enabled = 1;
3171 $license->version = '2010033100';
3172 $active_licenses[] = $license->shortname;
3173 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3174 if ($record->version < $license->version) {
3175 // update license record
3176 $license->enabled = $record->enabled;
3177 $license->id = $record->id;
3178 $DB->update_record('license', $license);
3181 $DB->insert_record('license', $license);
3184 // add creative commons no commercial sharealike
3185 $license->shortname = 'cc-nc-sa';
3186 $license->fullname = 'Creative Commons - No Commercial ShareAlike';
3187 $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
3188 $license->enabled = 1;
3189 $license->version = '2010033100';
3190 $active_licenses[] = $license->shortname;
3191 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3192 if ($record->version < $license->version) {
3193 // update license record
3194 $license->enabled = $record->enabled;
3195 $license->id = $record->id;
3196 $DB->update_record('license', $license);
3199 $DB->insert_record('license', $license);
3202 // add creative commons sharealike
3203 $license->shortname = 'cc-sa';
3204 $license->fullname = 'Creative Commons - ShareAlike';
3205 $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
3206 $license->enabled = 1;
3207 $license->version = '2010033100';
3208 $active_licenses[] = $license->shortname;
3209 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3210 if ($record->version < $license->version) {
3211 // update license record
3212 $license->enabled = $record->enabled;
3213 $license->id = $record->id;
3214 $DB->update_record('license', $license);
3217 $DB->insert_record('license', $license);
3220 set_config('licenses', implode(',', $active_licenses));
3221 /// set site default license
3222 set_config('sitedefaultlicense', 'allrightsreserved');
3224 /// Main savepoint reached
3225 upgrade_main_savepoint($result, 2010033101.02);
3228 if ($result && $oldversion < 2010033102.00) {
3229 // rename course view capability to participate
3230 $params = array('view'=>'moodle/course:view', 'participate'=>'moodle/course:participate');
3231 $sql = "UPDATE {role_capabilities} SET capability = :participate WHERE capability = :view";
3232 $DB->execute($sql, $params);
3233 $sql = "UPDATE {capabilities} SET name = :participate WHERE name = :view";
3234 $DB->execute($sql, $params);
3235 // note: the view capability is readded again at the end of upgrade, but with different meaning
3236 upgrade_main_savepoint($result, 2010033102.00);
3239 if ($result && $oldversion < 2010033102.01) {
3240 // Define field archetype to be added to role table
3241 $table = new xmldb_table('role');
3242 $field = new xmldb_field('archetype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'sortorder');
3243 $dbman->add_field($table, $field);
3244 upgrade_main_savepoint($result, 2010033102.01);
3247 if ($result && $oldversion < 2010033102.02) {
3248 // Set archetype for existing roles and change admin role to manager role
3249 $sql = "SELECT r.*, rc.capability
3251 JOIN {role_capabilities} rc ON rc.roleid = r.id
3252 WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
3254 $params = array('syscontextid'=>SYSCONTEXTID, 'legacycaps'=>'moodle/legacy:%');
3255 $substart = strlen('moodle/legacy:');
3256 $roles = $DB->get_recordset_sql($sql, $params); // in theory could be multiple legacy flags in one role
3257 foreach ($roles as $role) {
3258 $role->archetype = substr($role->capability, $substart);
3259 unset($role->capability);
3260 if ($role->archetype === 'admin') {
3261 $role->archetype = 'manager';
3262 if ($role->shortname === 'admin') {
3263 $role->shortname = 'manager';
3264 $role->name = get_string('manager', 'role');
3265 $role->description = get_string('managerdescription', 'role');
3268 $DB->update_record('role', $role);
3272 upgrade_main_savepoint($result, 2010033102.02);
3275 if ($result && $oldversion < 2010033102.03) {
3276 // Now pick site admins (===have manager role assigned at the system context)
3277 // and store them in the new $CFG->siteadmins setting as comma separated list
3278 $sql = "SELECT ra.id, ra.userid
3279 FROM {role_assignments} ra
3280 JOIN {role} r ON r.id = ra.roleid
3281 JOIN {user} u ON u.id = ra.userid
3282 WHERE ra.contextid = :syscontext AND r.archetype = 'manager' AND u.deleted = 0
3284 $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
3286 foreach ($ras as $ra) {
3287 $admins[$ra->userid] = $ra->userid;
3288 set_config('siteadmins', implode(',', $admins)); // better to save it repeatedly, we do need at least one admin
3289 $DB->delete_records('role_assignments', array('id'=>$ra->id));
3292 upgrade_main_savepoint($result, 2010033102.03);
3295 if ($result && $oldversion < 2010033102.04) {
3296 // clean up the manager roles
3297 $managers = $DB->get_records('role', array('archetype'=>'manager'));
3298 foreach ($managers as $manager) {
3299 // now sanitize the capabilities and overrides
3300 $DB->delete_records('role_capabilities', array('capability'=>'moodle/site:config', 'roleid'=>$manager->id)); // only site admins may configure servers
3301 // note: doanything and legacy caps are deleted automatically, they get moodle/course:view later at the end of the upgrade
3303 // set usable contexts
3304 $DB->delete_records('role_context_levels', array('roleid'=>$manager->id));
3305 $assignlevels = array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE);
3306 foreach ($assignlevels as $assignlevel) {
3307 $record = (object)array('roleid'=>$manager->id, 'contextlevel'=>$assignlevel);
3308 $DB->insert_record('role_context_levels', $record);
3311 // remove manager role assignments bellow the course context level - admin role was never intended for activities and blocks,
3312 // the problem is that those assignments would not be visible after upgrade and old style admins in activities make no sense anyway
3313 $DB->delete_records_select('role_assignments', "roleid = :manager AND contextid IN (SELECT id FROM {context} WHERE contextlevel > 50)", array('manager'=>$manager->id));
3315 // allow them to assign all roles except default user, guest and frontpage - users get these roles automatically on the fly when needed
3316 $DB->delete_records('role_allow_assign', array('roleid'=>$manager->id));
3317 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype <> 'user' AND archetype <> 'guest' AND archetype <> 'frontpage'");
3318 foreach ($roles as $role) {
3319 $record = (object)array('roleid'=>$manager->id, 'allowassign'=>$role->id);
3320 $DB->insert_record('role_allow_assign', $record);
3323 // allow them to override all roles
3324 $DB->delete_records('role_allow_override', array('roleid'=>$manager->id));
3325 $roles = $DB->get_records_sql("SELECT * FROM {role}");
3326 foreach ($roles as $role) {
3327 $record = (object)array('roleid'=>$manager->id, 'allowoverride'=>$role->id);
3328 $DB->insert_record('role_allow_override', $record);
3331 // allow them to switch to all following roles
3332 $DB->delete_records('role_allow_switch', array('roleid'=>$manager->id));
3333 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype IN ('student', 'teacher', 'editingteacher')");
3334 foreach ($roles as $role) {
3335 $record = (object)array('roleid'=>$manager->id, 'allowswitch'=>$role->id);
3336 $DB->insert_record('role_allow_switch', $record);
3340 upgrade_main_savepoint($result, 2010033102.04);
3343 if ($result && $oldversion < 2010033102.05) {
3344 // remove course:view from all roles that are not used for enrolment, it does NOT belong there because it really means user is enrolled!
3345 $noenrolroles = $DB->get_records_select('role', "archetype IN ('guest', 'user', 'manager', 'coursecreator', 'frontpage')");
3346 foreach ($noenrolroles as $role) {
3347 $DB->delete_records('role_capabilities', array('roleid'=>$role->id, 'capability'=>'moodle/course:participate'));
3349 upgrade_main_savepoint($result, 2010033102.05);
3352 if ($result && $oldversion < 2010033102.06) {
3353 // make sure there is nothing weird in default user role
3354 if (!empty($CFG->defaultuserroleid)) {
3355 if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
3356 if ($role->archetype !== '' and $role->archetype !== 'user') {
3357 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default authenticated user role (defaultuserroleid) value is invalid, setting cleared.');
3358 unset_config('defaultuserroleid');
3361 unset_config('defaultuserroleid');
3364 upgrade_main_savepoint($result, 2010033102.06);
3367 if ($result && $oldversion < 2010033102.07) {
3368 if (!empty($CFG->displayloginfailures) and $CFG->displayloginfailures === 'teacher') {
3369 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Displaying of login failuters to teachers is not supported any more.');
3370 unset_config('displayloginfailures');
3372 upgrade_main_savepoint($result, 2010033102.07);
3375 if ($result && $oldversion < 2010033102.08) {
3376 // make sure there are no problems in default guest role settings
3377 if (!empty($CFG->guestroleid)) {
3378 if ($role = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
3379 if ($role->archetype !== '' and $role->archetype !== 'guest') {
3380 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default guest role (guestroleid) value is invalid, setting cleared.');
3381 unset_config('guestroleid');
3384 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) doeas not exist, setting cleared.');
3385 unset_config('guestroleid');
3388 // remove all roles of the guest account - the only way to change it is to override the guest role, sorry
3389 // the guest account gets all the role assignemnts on the fly whcih works fine in has_capability(),
3390 $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE username = 'guest')");
3392 upgrade_main_savepoint($result, 2010033102.08);
3395 if ($result && $oldversion < 2010033102.09) {
3396 // For MDL-17501. Ensure that any role that has moodle/course:update also has moodle/course:visibility.
3397 // Get the roles with 'moodle/course:update'.
3398 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
3399 $roles = get_roles_with_capability('moodle/course:update', CAP_ALLOW, $systemcontext);
3401 // Give those roles 'moodle/course:visibility'.
3402 foreach ($roles as $role) {
3403 assign_capability('moodle/course:visibility', CAP_ALLOW, $role->id, $systemcontext->id);
3406 // Force all sessions to refresh access data.
3407 mark_context_dirty($systemcontext->path);
3409 // Main savepoint reached
3410 upgrade_main_savepoint($result, 2010033102.09);
3413 if ($result && $oldversion < 2010040700) {
3414 // migrate old groupings --> groupmembersonly setting
3415 if (isset($CFG->enablegroupings)) {
3416 set_config('enablegroupmembersonly', $CFG->enablegroupings);
3417 unset_config('enablegroupings');
3420 // Main savepoint reached
3421 upgrade_main_savepoint($result, 2010040700);
3424 if ($result && $oldversion < 2010040900) {
3426 // Changing the default of field lang on table user to good old "en"
3427 $table = new xmldb_table('user');
3428 $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en', 'country');
3430 // Launch change of default for field lang
3431 $dbman->change_field_default($table, $field);
3433 // update main site lang
3434 if (strpos($CFG->lang, '_utf8') !== false) {
3435 $lang = str_replace('_utf8', '', $CFG->lang);
3436 set_config('lang', $lang);
3440 if (!empty($CFG->langlist)) {
3441 $langs = explode(',', $CFG->langlist);
3442 foreach ($langs as $key=>$lang) {
3443 $lang = str_replace('_utf8', '', $lang);
3444 $langs[$key] = $lang;
3446 set_config('langlist', implode(',', $langs));
3449 // Main savepoint reached
3450 upgrade_main_savepoint($result, 2010040900);
3453 if ($result && $oldversion < 2010040901) {
3455 // Remove "_utf8" suffix from all langs in user table
3456 $langs = $DB->get_records_sql("SELECT DISTINCT lang FROM {user} WHERE lang LIKE ?", array('%_utf8'));
3458 foreach ($langs as $lang=>$unused) {
3459 $newlang = str_replace('_utf8', '', $lang);
3460 $sql = "UPDATE {user} SET lang = :newlang WHERE lang = :lang";
3461 $DB->execute($sql, array('newlang'=>$newlang, 'lang'=>$lang));
3464 // Main savepoint reached
3465 upgrade_main_savepoint($result, 2010040901);
3468 if ($result && $oldversion < 2010041301) {
3469 $sql = "UPDATE {block} SET name=? WHERE name=?";
3470 $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3471 $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3473 $sql = "UPDATE {block_instances} SET blockname=? WHERE blockname=?";
3474 $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3475 $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3476 upgrade_main_savepoint($result, 2010041301);
3479 if ($result && $oldversion < 2010042100) {
3481 /// Define table backup_controllers to be created
3482 $table = new xmldb_table('backup_controllers');
3484 /// Adding fields to table backup_controllers
3485 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3486 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3487 $table->add_field('type', XMLDB_TYPE_CHAR, '6', null, XMLDB_NOTNULL, null, null);
3488 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3489 $table->add_field('format', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
3490 $table->add_field('interactive', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3491 $table->add_field('purpose', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3492 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3493 $table->add_field('status', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3494 $table->add_field('execution', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3495 $table->add_field('executiontime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3496 $table->add_field('checksum', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3497 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3498 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3499 $table->add_field('controller', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
3501 /// Adding keys to table backup_controllers
3502 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3503 $table->add_key('backupid_uk', XMLDB_KEY_UNIQUE, array('backupid'));
3504 $table->add_key('userid_fk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3506 /// Adding indexes to table backup_controllers
3507 $table->add_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3509 /// Conditionally launch create table for backup_controllers
3510 if (!$dbman->table_exists($table)) {
3511 $dbman->create_table($table);
3514 /// Define table backup_ids_template to be created
3515 $table = new xmldb_table('backup_ids_template');
3517 /// Adding fields to table backup_ids_template
3518 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3519 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3520 $table->add_field('itemname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
3521 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3522 $table->add_field('parentitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3524 /// Adding keys to table backup_ids_template
3525 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3526 $table->add_key('backupid_itemname_itemid_uk', XMLDB_KEY_UNIQUE, array('backupid', 'itemname', 'itemid'));
3528 /// Adding indexes to table backup_ids_template
3529 $table->add_index('backupid_parentitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'parentitemid'));
3531 /// Conditionally launch create table for backup_controllers
3532 if (!$dbman->table_exists($table)) {
3533 $dbman->create_table($table);
3536 /// Main savepoint reached
3537 upgrade_main_savepoint($result, 2010042100);
3540 if ($result && $oldversion < 2010042301) {
3542 $table = new xmldb_table('course_sections');
3543 $field = new xmldb_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'section');
3545 if (!$dbman->field_exists($table, $field)) {
3546 $dbman->add_field($table, $field);
3549 upgrade_main_savepoint($result, 2010042301);
3552 if ($result && $oldversion < 2010042302) {
3553 // Define table cohort to be created
3554 $table = new xmldb_table('cohort');
3556 // Adding fields to table cohort
3557 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3558 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3559 $table->add_field('name', XMLDB_TYPE_CHAR, '254', null, XMLDB_NOTNULL, null, null);
3560 $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3561 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
3562 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3563 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3564 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3565 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3567 // Adding keys to table cohort
3568 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3569 $table->add_key('context', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3571 // Conditionally launch create table for cohort
3572 if (!$dbman->table_exists($table)) {
3573 $dbman->create_table($table);
3576 upgrade_main_savepoint($result, 2010042302);
3579 if ($result && $oldversion < 2010042303) {
3580 // Define table cohort_members to be created
3581 $table = new xmldb_table('cohort_members');
3583 // Adding fields to table cohort_members
3584 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3585 $table->add_field('cohortid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3586 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3587 $table->add_field('timeadded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3589 // Adding keys to table cohort_members
3590 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3591 $table->add_key('cohortid', XMLDB_KEY_FOREIGN, array('cohortid'), 'cohort', array('id'));
3592 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3594 // Adding indexes to table cohort_members
3595 $table->add_index('cohortid-userid', XMLDB_INDEX_UNIQUE, array('cohortid', 'userid'));
3597 // Conditionally launch create table for cohort_members
3598 if (!$dbman->table_exists($table)) {
3599 $dbman->create_table($table);
3602 // Main savepoint reached
3603 upgrade_main_savepoint($result, 2010042303);
3606 if ($result && $oldversion < 2010042800) {
3607 //drop the previously created ratings table
3608 $table = new xmldb_table('ratings');
3609 if ($dbman->table_exists($table)) {
3610 $dbman->drop_table($table);
3613 //create the rating table (replaces module specific rating implementations)
3614 $table = new xmldb_table('rating');
3615 if ($dbman->table_exists($table)) {
3616 $dbman->drop_table($table);
3619 /// Adding fields to table rating
3620 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3621 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3623 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3624 $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
3625 $table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3626 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3628 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3629 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3631 /// Adding keys to table rating
3632 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3633 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3634 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3636 /// Adding indexes to table rating
3637 $table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
3639 /// Create table for ratings
3640 if (!$dbman->table_exists($table)) {
3641 $dbman->create_table($table);
3644 upgrade_main_savepoint($result, 2010042800);
3647 if ($result && $oldversion < 2010042801) {
3648 // migrating old comments block content
3649 $DB->execute("UPDATE {comments}
3650 SET contextid = (SELECT parentcontextid
3651 FROM {block_instances}
3652 WHERE id = {comments}.itemid AND blockname = 'comments'),
3653 commentarea = 'page_comments',
3655 WHERE commentarea = 'block_comments'
3657 AND EXISTS (SELECT 'x'
3658 FROM {block_instances}
3659 WHERE id = {comments}.itemid
3660 AND blockname = 'comments')");
3662 // remove all orphaned record
3663 $DB->delete_records('comments', array('commentarea'=>'block_comments'));
3664 upgrade_main_savepoint($result, 2010042801);
3667 if ($result && $oldversion < 2010042802) { // Change backup_controllers->type to varchar10 (recreate dep. index)
3669 /// Define index typeitem_ix (not unique) to be dropped form backup_controllers
3670 $table = new xmldb_table('backup_controllers');
3671 $index = new xmldb_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3673 /// Conditionally launch drop index typeitem_ix
3674 if ($dbman->index_exists($table, $index)) {
3675 $dbman->drop_index($table, $index);
3678 /// Changing precision of field type on table backup_controllers to (10)
3679 $table = new xmldb_table('backup_controllers');
3680 $field = new xmldb_field('type', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'backupid');
3682 /// Launch change of precision for field type
3683 $dbman->change_field_precision($table, $field);
3685 /// Define index typeitem_ix (not unique) to be added to backup_controllers
3686 $table = new xmldb_table('backup_controllers');
3687 $index = new xmldb_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3689 /// Conditionally launch add index typeitem_ix
3690 if (!$dbman->index_exists($table, $index)) {
3691 $dbman->add_index($table, $index);
3694 /// Main savepoint reached
3695 upgrade_main_savepoint($result, 2010042802);
3698 if ($result && $oldversion < 2010043000) { // Adding new course completion feature
3700 /// Add course completion tables
3701 /// Define table course_completion_aggr_methd to be created
3702 $table = new xmldb_table('course_completion_aggr_methd');
3704 /// Adding fields to table course_completion_aggr_methd
3705 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3706 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3707 $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, null);
3708 $table->add_field('method', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3709 $table->add_field('value', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3711 /// Adding keys to table course_completion_aggr_methd
3712 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3714 /// Adding indexes to table course_completion_aggr_methd
3715 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3716 $table->add_index('criteriatype', XMLDB_INDEX_NOTUNIQUE, array('criteriatype'));
3718 /// Conditionally launch create table for course_completion_aggr_methd
3719 if (!$dbman->table_exists($table)) {
3720 $dbman->create_table($table);
3724 /// Define table course_completion_criteria to be created
3725 $table = new xmldb_table('course_completion_criteria');
3727 /// Adding fields to table course_completion_criteria
3728 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3729 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3730 $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3731 $table->add_field('module', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3732 $table->add_field('moduleinstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3733 $table->add_field('courseinstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3734 $table->add_field('enrolperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3735 $table->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3736 $table->add_field('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3737 $table->add_field('role', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3739 /// Adding keys to table course_completion_criteria
3740 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3742 /// Adding indexes to table course_completion_criteria
3743 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3745 /// Conditionally launch create table for course_completion_criteria
3746 if (!$dbman->table_exists($table)) {
3747 $dbman->create_table($table);
3751 /// Define table course_completion_crit_compl to be created
3752 $table = new xmldb_table('course_completion_crit_compl');
3754 /// Adding fields to table course_completion_crit_compl
3755 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3756 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3757 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3758 $table->add_field('criteriaid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3759 $table->add_field('gradefinal', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3760 $table->add_field('unenroled', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3761 $table->add_field('deleted', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
3762 $table->add_field('timecompleted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3764 /// Adding keys to table course_completion_crit_compl
3765 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3767 /// Adding indexes to table course_completion_crit_compl
3768 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
3769 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3770 $table->add_index('criteriaid', XMLDB_INDEX_NOTUNIQUE, array('criteriaid'));
3771 $table->add_index('timecompleted', XMLDB_INDEX_NOTUNIQUE, array('timecompleted'));
3773 /// Conditionally launch create table for course_completion_crit_compl
3774 if (!$dbman->table_exists($table)) {
3775 $dbman->create_table($table);
3779 /// Define table course_completion_notify to be created
3780 $table = new xmldb_table('course_completion_notify');
3782 /// Adding fields to table course_completion_notify
3783 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3784 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3785 $table->add_field('role', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3786 $table->add_field('message', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
3787 $table->add_field('timesent', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3789 /// Adding keys to table course_completion_notify
3790 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3792 /// Adding indexes to table course_completion_notify
3793 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3795 /// Conditionally launch create table for course_completion_notify
3796 if (!$dbman->table_exists($table)) {
3797 $dbman->create_table($table);
3800 /// Define table course_completions to be created
3801 $table = new xmldb_table('course_completions');
3803 /// Adding fields to table course_completions
3804 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3805 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3806 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3807 $table->add_field('deleted', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
3808 $table->add_field('timenotified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3809 $table->add_field('timeenrolled', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3810 $table->add_field('timestarted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3811 $table->add_field('timecompleted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3812 $table->add_field('reaggregate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3814 /// Adding keys to table course_completions
3815 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3817 /// Adding indexes to table course_completions
3818 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
3819 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3820 $table->add_index('timecompleted', XMLDB_INDEX_NOTUNIQUE, array('timecompleted'));
3822 /// Conditionally launch create table for course_completions
3823 if (!$dbman->table_exists($table)) {
3824 $dbman->create_table($table);
3828 /// Add cols to course table
3829 /// Define field enablecompletion to be added to course
3830 $table = new xmldb_table('course');
3831 $field = new xmldb_field('enablecompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
3833 /// Conditionally launch add field enablecompletion
3834 if (!$dbman->field_exists($table, $field)) {
3835 $dbman->add_field($table, $field);
3838 /// Define field completionstartonenrol to be added to course
3839 $field = new xmldb_field('completionstartonenrol', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'enablecompletion');
3841 /// Conditionally launch add field completionstartonenrol
3842 if (!$dbman->field_exists($table, $field)) {
3843 $dbman->add_field($table, $field);
3846 /// Define field completionnotify to be added to course
3847 $field = new xmldb_field('completionnotify', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'enablecompletion');
3849 /// Conditionally launch add field completionnotify
3850 if (!$dbman->field_exists($table, $field)) {
3851 $dbman->add_field($table, $field);
3854 upgrade_main_savepoint($result, 2010043000);
3857 if ($result && $oldversion < 2010043001) {
3859 /// Define table registration_hubs to be created
3860 $table = new xmldb_table('registration_hubs');
3862 /// Adding fields to table registration_hubs
3863 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3864 $table->add_field('token', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
3865 $table->add_field('hubname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
3866 $table->add_field('huburl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
3867 $table->add_field('confirmed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3869 /// Adding keys to table registration_hubs
3870 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3872 /// Conditionally launch create table for registration_hubs
3873 if (!$dbman->table_exists($table)) {
3874 $dbman->create_table($table);
3877 /// Main savepoint reached
3878 upgrade_main_savepoint($result, 2010043001);
3881 if ($result && $oldversion < 2010050200) {
3883 /// Define table backup_logs to be created
3884 $table = new xmldb_table('backup_logs');
3886 /// Adding fields to table backup_logs
3887 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3888 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3889 $table->add_field('loglevel', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3890 $table->add_field('message', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
3891 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3893 /// Adding keys to table backup_logs
3894 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3895 $table->add_key('backupid', XMLDB_KEY_FOREIGN, array('backupid'), 'backup_controllers', array('backupid'));
3897 /// Adding indexes to table backup_logs
3898 $table->add_index('backupid-id', XMLDB_INDEX_UNIQUE, array('backupid', 'id'));
3900 /// Conditionally launch create table for backup_logs
3901 if (!$dbman->table_exists($table)) {
3902 $dbman->create_table($table);
3905 /// Drop some old backup tables, not used anymore
3907 /// Define table backup_files to be dropped
3908 $table = new xmldb_table('backup_files');
3910 /// Conditionally launch drop table for backup_files
3911 if ($dbman->table_exists($table)) {
3912 $dbman->drop_table($table);
3915 /// Define table backup_ids to be dropped
3916 $table = new xmldb_table('backup_ids');
3918 /// Conditionally launch drop table for backup_ids
3919 if ($dbman->table_exists($table)) {
3920 $dbman->drop_table($table);
3923 /// Main savepoint reached
3924 upgrade_main_savepoint($result, 2010050200);
3927 if ($result && $oldversion < 2010050403) { // my_pages for My Moodle and Public Profile pages
3929 /// Define table my_pages to be created
3930 $table = new xmldb_table('my_pages');
3932 /// Adding fields to table my_pages
3933 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3934 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 0);
3935 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
3936 $table->add_field('private', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
3937 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3940 /// Adding keys to table my_pages
3941 $table->add_key('id', XMLDB_KEY_PRIMARY, array('id'));
3943 /// Adding indexes to table my_pages
3944 $table->add_index('useridprivate', XMLDB_INDEX_NOTUNIQUE, array('userid', 'private'));
3946 /// Conditionally launch create table for my_pages
3947 if (!$dbman->table_exists($table)) {
3948 $dbman->create_table($table);
3951 /// Add two lines of data into this new table. These are the default pages.
3952 $mypage = new object();
3953 $mypage->userid = NULL;
3954 $mypage->name = '__default';
3955 $mypage->private = 0;
3956 $mypage->sortorder = 0;
3957 if (!$DB->record_exists('my_pages', array('userid'=>NULL, 'private'=>0))) {
3958 $DB->insert_record('my_pages', $mypage);
3960 $mypage->private = 1;
3961 if (!$DB->record_exists('my_pages', array('userid'=>NULL, 'private'=>1))) {
3962 $DB->insert_record('my_pages', $mypage);
3965 /// This bit is a "illegal" hack, unfortunately, but there is not a better way to install default
3966 /// blocks right now, since the upgrade function need to be called after core AND plugins upgrade,
3967 /// and there is no such hook yet. Sigh.
3969 if ($mypage = $DB->get_record('my_pages', array('userid'=>NULL, 'private'=>1))) {
3970 if (!$DB->record_exists('block_instances', array('pagetypepattern'=>'my-index', 'parentcontextid'=>SITEID, 'subpagepattern'=>$mypage->id))) {
3972 // No default exist there yet, let's put a few into My Moodle so it's useful.
3974 $blockinstance = new stdClass;
3975 $blockinstance->parentcontextid = SITEID;
3976 $blockinstance->showinsubcontexts = 0;
3977 $blockinstance->pagetypepattern = 'my-index';
3978 $blockinstance->subpagepattern = $mypage->id;
3979 $blockinstance->configdata = '';
3981 $blockinstance->blockname = 'private_files';
3982 $blockinstance->defaultregion = 'side-post';
3983 $blockinstance->defaultweight = 0;
3984 $blockinstanceid = $DB->insert_record('block_instances', $blockinstance);
3985 get_context_instance(CONTEXT_BLOCK, $blockinstanceid);
3987 $blockinstance->blockname = 'online_users';
3988 $blockinstance->defaultregion = 'side-post';
3989 $blockinstance->defaultweight = 1;
3990 $blockinstanceid = $DB->insert_record('block_instances', $blockinstance);
3991 get_context_instance(CONTEXT_BLOCK, $blockinstanceid);
3993 $blockinstance->blockname = 'course_overview';
3994 $blockinstance->defaultregion = 'content';