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.
22 function xmldb_main_upgrade($oldversion) {
23 global $CFG, $USER, $DB, $OUTPUT;
25 require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
29 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
31 ////////////////////////////////////////
32 ///upgrade supported only from 1.9.x ///
33 ////////////////////////////////////////
35 if ($result && $oldversion < 2008030600) {
36 //NOTE: this table was added much later, that is why this step is repeated later in this file
38 /// Define table upgrade_log to be created
39 $table = new xmldb_table('upgrade_log');
41 /// Adding fields to table upgrade_log
42 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
43 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
44 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
45 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
46 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
47 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
48 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
49 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
50 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
52 /// Adding keys to table upgrade_log
53 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
54 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
56 /// Adding indexes to table upgrade_log
57 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
58 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
60 /// Create table for upgrade_log
61 $dbman->create_table($table);
63 /// Main savepoint reached
64 upgrade_main_savepoint($result, 2008030600);
67 if ($result && $oldversion < 2008030700) {
68 upgrade_set_timeout(60*20); // this may take a while
70 /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
71 $table = new xmldb_table('grade_letters');
72 $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
74 /// Launch drop index contextid-lowerboundary
75 $dbman->drop_index($table, $index);
77 /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
78 $table = new xmldb_table('grade_letters');
79 $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
81 /// Launch add index contextid-lowerboundary-letter
82 $dbman->add_index($table, $index);
84 /// Main savepoint reached
85 upgrade_main_savepoint($result, 2008030700);
88 if ($result && $oldversion < 2008050100) {
89 // Update courses that used weekscss to weeks
90 $result = $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
91 upgrade_main_savepoint($result, 2008050100);
94 if ($result && $oldversion < 2008050200) {
95 // remove unused config options
96 unset_config('statsrolesupgraded');
97 upgrade_main_savepoint($result, 2008050200);
100 if ($result && $oldversion < 2008050700) {
101 upgrade_set_timeout(60*20); // this may take a while
103 /// Fix minor problem caused by MDL-5482.
104 require_once($CFG->dirroot . '/question/upgrade.php');
105 $result = $result && question_fix_random_question_parents();
106 upgrade_main_savepoint($result, 2008050700);
109 if ($result && $oldversion < 2008051201) {
110 echo $OUTPUT->notification('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
111 upgrade_set_timeout(60*20); // this may take a while
113 /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
114 $dbfamily = $DB->get_dbfamily();
115 if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
116 $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
119 /// Define index idnumber (not unique) to be dropped form user
120 $table = new xmldb_table('user');
121 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
123 /// Launch drop index idnumber
124 if ($dbman->index_exists($table, $index)) {
125 $dbman->drop_index($table, $index);
128 /// Changing precision of field idnumber on table user to (255)
129 $table = new xmldb_table('user');
130 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
132 /// Launch change of precision for field idnumber
133 $dbman->change_field_precision($table, $field);
135 /// Launch add index idnumber again
136 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
137 $dbman->add_index($table, $index);
139 /// Main savepoint reached
140 upgrade_main_savepoint($result, 2008051201);
143 if ($result && $oldversion < 2008051202) {
144 $log_action = new object();
145 $log_action->module = 'course';
146 $log_action->action = 'unenrol';
147 $log_action->mtable = 'course';
148 $log_action->field = 'fullname';
149 if (!$DB->record_exists('log_display', array('action'=>'unenrol', 'module'=>'course'))) {
150 $result = $result && $DB->insert_record('log_display', $log_action);
152 upgrade_main_savepoint($result, 2008051202);
155 if ($result && $oldversion < 2008051203) {
156 $table = new xmldb_table('mnet_enrol_course');
157 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
158 $dbman->change_field_precision($table, $field);
159 upgrade_main_savepoint($result, 2008051203);
162 if ($result && $oldversion < 2008063001) {
163 upgrade_set_timeout(60*20); // this may take a while
165 // table to be modified
166 $table = new xmldb_table('tag_instance');
168 $field = new xmldb_field('tiuserid');
169 if (!$dbman->field_exists($table, $field)) {
170 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
171 $dbman->add_field($table, $field);
174 $index = new xmldb_index('itemtype-itemid-tagid');
175 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
176 if ($dbman->index_exists($table, $index)) {
177 $dbman->drop_index($table, $index);
179 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
180 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
181 if (!$dbman->index_exists($table, $index)) {
182 $dbman->add_index($table, $index);
185 /// Main savepoint reached
186 upgrade_main_savepoint($result, 2008063001);
189 if ($result && $oldversion < 2008070300) {
190 $result = $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
191 upgrade_main_savepoint($result, 2008070300);
194 if ($result && $oldversion < 2008070701) {
196 /// Define table portfolio_instance to be created
197 $table = new xmldb_table('portfolio_instance');
199 /// Adding fields to table portfolio_instance
200 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
201 $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
202 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
203 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
205 /// Adding keys to table portfolio_instance
206 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
208 /// Conditionally launch create table for portfolio_instance
209 if (!$dbman->table_exists($table)) {
210 $dbman->create_table($table);
212 /// Define table portfolio_instance_config to be created
213 $table = new xmldb_table('portfolio_instance_config');
215 /// Adding fields to table portfolio_instance_config
216 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
217 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
218 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
219 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
221 /// Adding keys to table portfolio_instance_config
222 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
223 $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
225 /// Adding indexes to table portfolio_instance_config
226 $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
228 /// Conditionally launch create table for portfolio_instance_config
229 if (!$dbman->table_exists($table)) {
230 $dbman->create_table($table);
233 /// Define table portfolio_instance_user to be created
234 $table = new xmldb_table('portfolio_instance_user');
236 /// Adding fields to table portfolio_instance_user
237 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
238 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
239 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
240 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
241 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
243 /// Adding keys to table portfolio_instance_user
244 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
245 $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
246 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
248 /// Conditionally launch create table for portfolio_instance_user
249 if (!$dbman->table_exists($table)) {
250 $dbman->create_table($table);
253 /// Main savepoint reached
254 upgrade_main_savepoint($result, 2008070701);
257 if ($result && $oldversion < 2008072400) {
258 /// Create the database tables for message_processors
259 $table = new xmldb_table('message_processors');
260 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
261 $table->add_field('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null);
262 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
263 $dbman->create_table($table);
265 /// delete old and create new fields
266 $table = new xmldb_table('message');
267 $field = new xmldb_field('messagetype');
268 $dbman->drop_field($table, $field);
271 $field = new xmldb_field('message');
272 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
273 $dbman->rename_field($table, $field, 'fullmessage');
274 $field = new xmldb_field('format');
275 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
276 $dbman->rename_field($table, $field, 'fullmessageformat');
278 /// new message fields
279 $field = new xmldb_field('subject');
280 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
281 $dbman->add_field($table, $field);
282 $field = new xmldb_field('fullmessagehtml');
283 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
284 $dbman->add_field($table, $field);
285 $field = new xmldb_field('smallmessage');
286 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
287 $dbman->add_field($table, $field);
290 $table = new xmldb_table('message_read');
291 $field = new xmldb_field('messagetype');
292 $dbman->drop_field($table, $field);
293 $field = new xmldb_field('mailed');
294 $dbman->drop_field($table, $field);
297 $field = new xmldb_field('message');
298 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
299 $dbman->rename_field($table, $field, 'fullmessage');
300 $field = new xmldb_field('format');
301 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
302 $dbman->rename_field($table, $field, 'fullmessageformat');
305 /// new message fields
306 $field = new xmldb_field('subject');
307 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
308 $dbman->add_field($table, $field);
309 $field = new xmldb_field('fullmessagehtml');
310 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
311 $dbman->add_field($table, $field);
312 $field = new xmldb_field('smallmessage');
313 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
314 $dbman->add_field($table, $field);
317 $table = new xmldb_table('message_working');
318 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
319 $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
320 $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
321 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
322 $dbman->create_table($table);
325 upgrade_main_savepoint($result, 2008072400);
328 if ($result && $oldversion < 2008072800) {
330 /// Define field enablecompletion to be added to course
331 $table = new xmldb_table('course');
332 $field = new xmldb_field('enablecompletion');
333 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
335 /// Launch add field enablecompletion
336 if (!$dbman->field_exists($table,$field)) {
337 $dbman->add_field($table, $field);
340 /// Define field completion to be added to course_modules
341 $table = new xmldb_table('course_modules');
342 $field = new xmldb_field('completion');
343 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'groupmembersonly');
345 /// Launch add field completion
346 if (!$dbman->field_exists($table,$field)) {
347 $dbman->add_field($table, $field);
350 /// Define field completiongradeitemnumber to be added to course_modules
351 $field = new xmldb_field('completiongradeitemnumber');
352 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'completion');
354 /// Launch add field completiongradeitemnumber
355 if (!$dbman->field_exists($table,$field)) {
356 $dbman->add_field($table, $field);
359 /// Define field completionview to be added to course_modules
360 $field = new xmldb_field('completionview');
361 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiongradeitemnumber');
363 /// Launch add field completionview
364 if (!$dbman->field_exists($table,$field)) {
365 $dbman->add_field($table, $field);
368 /// Define field completionexpected to be added to course_modules
369 $field = new xmldb_field('completionexpected');
370 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionview');
372 /// Launch add field completionexpected
373 if (!$dbman->field_exists($table,$field)) {
374 $dbman->add_field($table, $field);
377 /// Define table course_modules_completion to be created
378 $table = new xmldb_table('course_modules_completion');
379 if (!$dbman->table_exists($table)) {
381 /// Adding fields to table course_modules_completion
382 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
383 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
384 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
385 $table->add_field('completionstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
386 $table->add_field('viewed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
387 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
389 /// Adding keys to table course_modules_completion
390 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
392 /// Adding indexes to table course_modules_completion
393 $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
394 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
396 /// Launch create table for course_modules_completion
397 $dbman->create_table($table);
400 /// Main savepoint reached
401 upgrade_main_savepoint($result, 2008072800);
404 if ($result && $oldversion < 2008073000) {
406 /// Define table portfolio_log to be created
407 $table = new xmldb_table('portfolio_log');
409 /// Adding fields to table portfolio_log
410 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
411 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
412 $table->add_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
413 $table->add_field('portfolio', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
414 $table->add_field('caller_class', XMLDB_TYPE_CHAR, '150', null, XMLDB_NOTNULL, null, null);
415 $table->add_field('caller_file', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
416 $table->add_field('caller_sha1', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
418 /// Adding keys to table portfolio_log
419 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
420 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
421 $table->add_key('portfoliofk', XMLDB_KEY_FOREIGN, array('portfolio'), 'portfolio_instance', array('id'));
423 /// Conditionally launch create table for portfolio_log
424 if (!$dbman->table_exists($table)) {
425 $dbman->create_table($table);
428 /// Main savepoint reached
429 upgrade_main_savepoint($result, 2008073000);
432 if ($result && $oldversion < 2008073104) {
433 /// Drop old table that might exist for some people
434 $table = new xmldb_table('message_providers');
435 if ($dbman->table_exists($table)) {
436 $dbman->drop_table($table);
439 /// Define table message_providers to be created
440 $table = new xmldb_table('message_providers');
442 /// Adding fields to table message_providers
443 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
444 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
445 $table->add_field('component', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
446 $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
448 /// Adding keys to table message_providers
449 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
451 /// Adding indexes to table message_providers
452 $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
454 /// Create table for message_providers
455 $dbman->create_table($table);
457 upgrade_main_savepoint($result, 2008073104);
460 if ($result && $oldversion < 2008073111) {
461 /// Define table files to be created
462 $table = new xmldb_table('files');
464 /// Adding fields to table files
465 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
466 $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
467 $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
468 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
469 $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
470 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
471 $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
472 $table->add_field('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
473 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
474 $table->add_field('filesize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
475 $table->add_field('mimetype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
476 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
477 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
478 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
480 /// Adding keys to table files
481 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
482 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
483 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
485 /// Adding indexes to table files
486 $table->add_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid'));
487 $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
488 $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
490 /// Conditionally launch create table for files
491 $dbman->create_table($table);
493 /// Main savepoint reached
494 upgrade_main_savepoint($result, 2008073111);
497 if ($result && $oldversion < 2008073113) {
498 /// move all course, backup and other files to new filepool based storage
499 upgrade_migrate_files_courses();
500 /// Main savepoint reached
501 upgrade_main_savepoint($result, 2008073113);
504 if ($result && $oldversion < 2008073114) {
505 /// move all course, backup and other files to new filepool based storage
506 upgrade_migrate_files_blog();
507 /// Main savepoint reached
508 upgrade_main_savepoint($result, 2008073114);
511 if ($result && $oldversion < 2008080400) {
512 // Add field ssl_jump_url to mnet application, and populate existing default applications
513 $table = new xmldb_table('mnet_application');
514 $field = new xmldb_field('sso_jump_url');
515 if (!$dbman->field_exists($table, $field)) {
516 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
517 $dbman->add_field($table, $field);
518 $result = $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
519 $result = $result && $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
522 /// Main savepoint reached
523 upgrade_main_savepoint($result, 2008080400);
526 if ($result && $oldversion < 2008080500) {
528 /// Define table portfolio_tempdata to be created
529 $table = new xmldb_table('portfolio_tempdata');
531 /// Adding fields to table portfolio_tempdata
532 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
533 $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
535 /// Adding keys to table portfolio_tempdata
536 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
538 /// Conditionally launch create table for portfolio_tempdata
539 if (!$dbman->table_exists($table)) {
540 $dbman->create_table($table);
543 /// Main savepoint reached
544 upgrade_main_savepoint($result, 2008080500);
547 if ($result && $oldversion < 2008080600) {
549 $DB->delete_records('portfolio_tempdata'); // there shouldnt' be any, and it will cause problems with this upgrade.
550 /// Define field expirytime to be added to portfolio_tempdata
551 $table = new xmldb_table('portfolio_tempdata');
552 $field = new xmldb_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'data');
554 /// Conditionally launch add field expirytime
555 if (!$dbman->field_exists($table, $field)) {
556 $dbman->add_field($table, $field);
559 /// Main savepoint reached
560 upgrade_main_savepoint($result, 2008080600);
563 if ($result && $oldversion < 2008081500) {
564 /// Changing the type of all the columns that the question bank uses to store grades to be NUMBER(12, 7).
565 $table = new xmldb_table('question');
566 $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'generalfeedback');
567 $dbman->change_field_type($table, $field);
568 upgrade_main_savepoint($result, 2008081500);
571 if ($result && $oldversion < 2008081501) {
572 $table = new xmldb_table('question');
573 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'defaultgrade');
574 $dbman->change_field_type($table, $field);
575 upgrade_main_savepoint($result, 2008081501);
578 if ($result && $oldversion < 2008081502) {
579 $table = new xmldb_table('question_answers');
580 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'answer');
581 $dbman->change_field_type($table, $field);
582 upgrade_main_savepoint($result, 2008081502);
585 if ($result && $oldversion < 2008081503) {
586 $table = new xmldb_table('question_sessions');
587 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'newgraded');
588 $dbman->change_field_type($table, $field);
589 upgrade_main_savepoint($result, 2008081503);
592 if ($result && $oldversion < 2008081504) {
593 $table = new xmldb_table('question_states');
594 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'event');
595 $dbman->change_field_type($table, $field);
596 upgrade_main_savepoint($result, 2008081504);
599 if ($result && $oldversion < 2008081505) {
600 $table = new xmldb_table('question_states');
601 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'grade');
602 $dbman->change_field_type($table, $field);
603 upgrade_main_savepoint($result, 2008081505);
606 if ($result && $oldversion < 2008081506) {
607 $table = new xmldb_table('question_states');
608 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'raw_grade');
609 $dbman->change_field_type($table, $field);
610 upgrade_main_savepoint($result, 2008081506);
613 if ($result && $oldversion < 2008081600) {
615 /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
616 unset_config('unicodedb');
618 /// Main savepoint reached
619 upgrade_main_savepoint($result, 2008081600);
622 if ($result && $oldversion < 2008081900) {
623 /// Define field userid to be added to portfolio_tempdata
624 $table = new xmldb_table('portfolio_tempdata');
625 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'expirytime');
627 /// Conditionally launch add field userid
628 if (!$dbman->field_exists($table, $field)) {
629 $dbman->add_field($table, $field);
631 $DB->set_field('portfolio_tempdata', 'userid', 0);
632 /// now change it to be notnull
634 /// Changing nullability of field userid on table portfolio_tempdata to not null
635 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'expirytime');
637 /// Launch change of nullability for field userid
638 $dbman->change_field_notnull($table, $field);
640 /// Define key userfk (foreign) to be added to portfolio_tempdata
641 $table = new xmldb_table('portfolio_tempdata');
642 $key = new xmldb_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
644 /// Launch add key userfk
645 $dbman->add_key($table, $key);
647 upgrade_main_savepoint($result, 2008081900);
649 if ($result && $oldversion < 2008082602) {
651 /// Define table repository to be dropped
652 $table = new xmldb_table('repository');
654 /// Conditionally launch drop table for repository
655 if ($dbman->table_exists($table)) {
656 $dbman->drop_table($table);
659 /// Define table repository to be created
660 $table = new xmldb_table('repository');
662 /// Adding fields to table repository
663 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
664 $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
665 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1');
666 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
668 /// Adding keys to table repository
669 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
671 /// Conditionally launch create table for repository
672 if (!$dbman->table_exists($table)) {
673 $dbman->create_table($table);
675 /// Define table repository_instances to be created
676 $table = new xmldb_table('repository_instances');
678 /// Adding fields to table repository_instances
679 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
680 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
681 $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
682 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
683 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
684 $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null);
685 $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
686 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
687 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
689 /// Adding keys to table repository_instances
690 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
692 /// Conditionally launch create table for repository_instances
693 if (!$dbman->table_exists($table)) {
694 $dbman->create_table($table);
697 /// Define table repository_instance_config to be created
698 $table = new xmldb_table('repository_instance_config');
700 /// Adding fields to table repository_instance_config
701 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
702 $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
703 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
704 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
706 /// Adding keys to table repository_instance_config
707 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
709 /// Conditionally launch create table for repository_instance_config
710 if (!$dbman->table_exists($table)) {
711 $dbman->create_table($table);
714 /// Main savepoint reached
715 upgrade_main_savepoint($result, 2008082602);
718 if ($result && $oldversion < 2008082700) {
719 /// Add a new column to the question sessions table to record whether a
720 /// question has been flagged.
722 /// Define field flagged to be added to question_sessions
723 $table = new xmldb_table('question_sessions');
724 $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
726 /// Conditionally launch add field flagged
727 if (!$dbman->field_exists($table, $field)) {
728 $dbman->add_field($table, $field);
731 /// Main savepoint reached
732 upgrade_main_savepoint($result, 2008082700);
735 if ($result && $oldversion < 2008082900) {
737 /// Changing precision of field parent_type on table mnet_rpc to (20)
738 $table = new xmldb_table('mnet_rpc');
739 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
741 /// Launch change of precision for field parent_type
742 $dbman->change_field_precision($table, $field);
744 /// Main savepoint reached
745 upgrade_main_savepoint($result, 2008082900);
748 if ($result && $oldversion < 2008090108) {
749 $repo = new object();
750 $repo->type = 'upload';
752 $repo->sortorder = 1;
753 if (!$DB->record_exists('repository', array('type'=>'upload'))) {
754 $typeid = $DB->insert_record('repository', $repo);
756 $record = $DB->get_record('repository', array('type'=>'upload'));
757 $typeid = $record->id;
759 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
760 $instance = new object();
761 $instance->name = get_string('repositoryname', 'repository_upload');
762 $instance->typeid = $typeid;
763 $instance->userid = 0;
764 $instance->contextid = SITEID;
765 $instance->timecreated = time();
766 $instance->timemodified = time();
767 $result = $result && $DB->insert_record('repository_instances', $instance);
769 $repo->type = 'local';
771 $repo->sortorder = 1;
772 if (!$DB->record_exists('repository', array('type'=>'local'))) {
773 $typeid = $DB->insert_record('repository', $repo);
775 $record = $DB->get_record('repository', array('type'=>'local'));
776 $typeid = $record->id;
778 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
779 $instance = new object();
780 $instance->name = get_string('repositoryname', 'repository_local');
781 $instance->typeid = $typeid;
782 $instance->userid = 0;
783 $instance->contextid = SITEID;
784 $instance->timecreated = time();
785 $instance->timemodified = time();
786 $result = $result && $DB->insert_record('repository_instances', $instance);
789 upgrade_main_savepoint($result, 2008090108);
792 // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
793 if ($result && $oldversion < 2008091000) {
794 foreach (get_object_vars($CFG) as $name => $value) {
795 if (substr($name, strlen($name) - 8) !== '_version') {
798 $pluginname = substr($name, 0, strlen($name) - 8);
799 if (!strpos($pluginname, '_')) {
800 // Skip things like backup_version that don't contain an extra _
803 if ($pluginname == 'enrol_ldap_version') {
804 // Special case - this is something different from a plugin version number.
807 if (!preg_match('/^\d{10}$/', $value)) {
808 // Extra safety check, skip anything that does not look like a Moodle
809 // version number (10 digits).
812 $result = $result && set_config('version', $value, $pluginname);
813 $result = $result && unset_config($name);
815 upgrade_main_savepoint($result, 2008091000);
818 //Add a readonly field to the repository_instances table
819 //in order to support instance created automatically by a repository plugin
820 if ($result && $oldversion < 2008091611) {
822 /// Define field readonly to be added to repository_instances
823 $table = new xmldb_table('repository_instances');
824 $field = new xmldb_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
826 /// Conditionally launch add field readonly
827 if (!$dbman->field_exists($table, $field)) {
828 $dbman->add_field($table, $field);
831 /// Main savepoint reached
832 upgrade_main_savepoint($result, 2008091611);
835 if ($result && $oldversion < 2008092300) {
836 unset_config('editorspelling');
837 unset_config('editordictionary');
838 /// Main savepoint reached
839 upgrade_main_savepoint($result, 2008092300);
842 if ($result && $oldversion < 2008101000) {
844 /// Changing the default of field lang on table user to en_utf8
845 $table = new xmldb_table('user');
846 $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en_utf8', 'country');
848 /// Launch change of default for field lang
849 $dbman->change_field_default($table, $field);
851 /// Main savepoint reached
852 upgrade_main_savepoint($result, 2008101000);
855 if ($result && $oldversion < 2008101300) {
857 if (!get_config(NULL, 'statsruntimedays')) {
858 set_config('statsruntimedays', '31');
861 /// Main savepoint reached
862 upgrade_main_savepoint($result, 2008101300);
865 /// New table for storing which roles can be assigned in which contexts.
866 if ($result && $oldversion < 2008110601) {
868 /// Define table role_context_levels to be created
869 $table = new xmldb_table('role_context_levels');
871 /// Adding fields to table role_context_levels
872 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
873 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
874 $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
876 /// Adding keys to table role_context_levels
877 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
878 $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
879 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
881 /// Conditionally launch create table for role_context_levels
882 if (!$dbman->table_exists($table)) {
883 $dbman->create_table($table);
886 /// Main savepoint reached
887 upgrade_main_savepoint($result, 2008110601);
890 /// Now populate the role_context_levels table with the defaults that match
891 /// moodle_install_roles, and any other combinations that exist in this system.
892 if ($result && $oldversion < 2008110602) {
893 $roleids = $DB->get_records_menu('role', array(), '', 'shortname,id');
895 /// Defaults, should match moodle_install_roles.
896 $rolecontextlevels = array();
897 if (isset($roleids['coursecreator'])) {
898 $rolecontextlevels[$roleids['coursecreator']] = get_default_contextlevels('coursecreator');
900 if (isset($roleids['editingteacher'])) {
901 $rolecontextlevels[$roleids['editingteacher']] = get_default_contextlevels('editingteacher');
903 if (isset($roleids['teacher'])) {
904 $rolecontextlevels[$roleids['teacher']] = get_default_contextlevels('teacher');
906 if (isset($roleids['student'])) {
907 $rolecontextlevels[$roleids['student']] = get_default_contextlevels('student');
909 if (isset($roleids['guest'])) {
910 $rolecontextlevels[$roleids['guest']] = get_default_contextlevels('guest');
912 if (isset($roleids['user'])) {
913 $rolecontextlevels[$roleids['user']] = get_default_contextlevels('user');
916 /// See what other role assignments are in this database, extend the allowed
917 /// lists to allow them too.
918 $existingrolecontextlevels = $DB->get_recordset_sql('SELECT DISTINCT ra.roleid, con.contextlevel FROM
919 {role_assignments} ra JOIN {context} con ON ra.contextid = con.id');
920 foreach ($existingrolecontextlevels as $rcl) {
921 if (!isset($rolecontextlevels[$rcl->roleid])) {
922 $rolecontextlevels[$rcl->roleid] = array($rcl->contextlevel);
923 } else if (!in_array($rcl->contextlevel, $rolecontextlevels[$rcl->roleid])) {
924 $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
928 /// Put the data into the database.
929 foreach ($rolecontextlevels as $roleid => $contextlevels) {
930 set_role_contextlevels($roleid, $contextlevels);
933 /// Main savepoint reached
934 upgrade_main_savepoint($result, 2008110602);
937 /// Drop the deprecated teacher, teachers, student and students columns from the course table.
938 if ($result && $oldversion < 2008111200) {
939 $table = new xmldb_table('course');
941 /// Conditionally launch drop field teacher
942 $field = new xmldb_field('teacher');
943 if ($dbman->field_exists($table, $field)) {
944 $dbman->drop_field($table, $field);
947 /// Conditionally launch drop field teacher
948 $field = new xmldb_field('teachers');
949 if ($dbman->field_exists($table, $field)) {
950 $dbman->drop_field($table, $field);
953 /// Conditionally launch drop field teacher
954 $field = new xmldb_field('student');
955 if ($dbman->field_exists($table, $field)) {
956 $dbman->drop_field($table, $field);
959 /// Conditionally launch drop field teacher
960 $field = new xmldb_field('students');
961 if ($dbman->field_exists($table, $field)) {
962 $dbman->drop_field($table, $field);
965 /// Main savepoint reached
966 upgrade_main_savepoint($result, 2008111200);
969 /// Add a unique index to the role.name column.
970 if ($result && $oldversion < 2008111800) {
972 /// Define index name (unique) to be added to role
973 $table = new xmldb_table('role');
974 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
976 /// Conditionally launch add index name
977 if (!$dbman->index_exists($table, $index)) {
978 $dbman->add_index($table, $index);
981 /// Main savepoint reached
982 upgrade_main_savepoint($result, 2008111800);
985 /// Add a unique index to the role.shortname column.
986 if ($result && $oldversion < 2008111801) {
988 /// Define index shortname (unique) to be added to role
989 $table = new xmldb_table('role');
990 $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
992 /// Conditionally launch add index shortname
993 if (!$dbman->index_exists($table, $index)) {
994 $dbman->add_index($table, $index);
997 /// Main savepoint reached
998 upgrade_main_savepoint($result, 2008111801);
1001 if ($result && $oldversion < 2008120700) {
1003 /// Changing precision of field shortname on table course_request to (100)
1004 $table = new xmldb_table('course_request');
1005 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
1007 /// Before changing the field, drop dependent indexes
1008 /// Define index shortname (not unique) to be dropped form course_request
1009 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
1010 /// Conditionally launch drop index shortname
1011 if ($dbman->index_exists($table, $index)) {
1012 $dbman->drop_index($table, $index);
1015 /// Launch change of precision for field shortname
1016 $dbman->change_field_precision($table, $field);
1018 /// After changing the field, recreate dependent indexes
1019 /// Define index shortname (not unique) to be added to course_request
1020 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
1021 /// Conditionally launch add index shortname
1022 if (!$dbman->index_exists($table, $index)) {
1023 $dbman->add_index($table, $index);
1026 /// Main savepoint reached
1027 upgrade_main_savepoint($result, 2008120700);
1030 if ($result && $oldversion < 2008120801) {
1032 /// Changing precision of field shortname on table mnet_enrol_course to (100)
1033 $table = new xmldb_table('mnet_enrol_course');
1034 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
1036 /// Launch change of precision for field shortname
1037 $dbman->change_field_precision($table, $field);
1039 /// Main savepoint reached
1040 upgrade_main_savepoint($result, 2008120801);
1043 if ($result && $oldversion < 2008121701) {
1045 /// Define field availablefrom to be added to course_modules
1046 $table = new xmldb_table('course_modules');
1047 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
1049 /// Conditionally launch add field availablefrom
1050 if (!$dbman->field_exists($table, $field)) {
1051 $dbman->add_field($table, $field);
1054 /// Define field availableuntil to be added to course_modules
1055 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
1057 /// Conditionally launch add field availableuntil
1058 if (!$dbman->field_exists($table, $field)) {
1059 $dbman->add_field($table, $field);
1062 /// Define field showavailability to be added to course_modules
1063 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
1065 /// Conditionally launch add field showavailability
1066 if (!$dbman->field_exists($table, $field)) {
1067 $dbman->add_field($table, $field);
1070 /// Define table course_modules_availability to be created
1071 $table = new xmldb_table('course_modules_availability');
1073 /// Adding fields to table course_modules_availability
1074 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1075 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1076 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1077 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
1078 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1079 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1080 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1082 /// Adding keys to table course_modules_availability
1083 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1084 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
1085 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
1086 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
1088 /// Conditionally launch create table for course_modules_availability
1089 if (!$dbman->table_exists($table)) {
1090 $dbman->create_table($table);
1093 /// Changes to modinfo mean we need to rebuild course cache
1094 require_once($CFG->dirroot . '/course/lib.php');
1095 rebuild_course_cache(0, true);
1097 /// For developer upgrades, turn on the conditional activities and completion
1098 /// features automatically (to gain more testing)
1099 //TODO: remove before 2.0 final!
1100 if (debugging('', DEBUG_DEVELOPER)) {
1101 set_config('enableavailability', 1);
1102 set_config('enablecompletion', 1);
1105 /// Main savepoint reached
1106 upgrade_main_savepoint($result, 2008121701);
1109 if ($result && $oldversion < 2009010500) {
1110 /// clean up config table a bit
1111 unset_config('session_error_counter');
1113 /// Main savepoint reached
1114 upgrade_main_savepoint($result, 2009010500);
1117 if ($result && $oldversion < 2009010600) {
1119 /// Define field originalquestion to be dropped from question_states
1120 $table = new xmldb_table('question_states');
1121 $field = new xmldb_field('originalquestion');
1123 /// Conditionally launch drop field originalquestion
1124 if ($dbman->field_exists($table, $field)) {
1125 $dbman->drop_field($table, $field);
1128 /// Main savepoint reached
1129 upgrade_main_savepoint($result, 2009010600);
1132 if ($result && $oldversion < 2009010601) {
1134 /// Changing precision of field ip on table log to (45)
1135 $table = new xmldb_table('log');
1136 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1138 /// Launch change of precision for field ip
1139 $dbman->change_field_precision($table, $field);
1141 /// Main savepoint reached
1142 upgrade_main_savepoint($result, 2009010601);
1145 if ($result && $oldversion < 2009010602) {
1147 /// Changing precision of field lastip on table user to (45)
1148 $table = new xmldb_table('user');
1149 $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
1151 /// Launch change of precision for field lastip
1152 $dbman->change_field_precision($table, $field);
1154 /// Main savepoint reached
1155 upgrade_main_savepoint($result, 2009010602);
1158 if ($result && $oldversion < 2009010603) {
1160 /// Changing precision of field ip_address on table mnet_host to (45)
1161 $table = new xmldb_table('mnet_host');
1162 $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
1164 /// Launch change of precision for field ip_address
1165 $dbman->change_field_precision($table, $field);
1167 /// Main savepoint reached
1168 upgrade_main_savepoint($result, 2009010603);
1171 if ($result && $oldversion < 2009010604) {
1173 /// Changing precision of field ip on table mnet_log to (45)
1174 $table = new xmldb_table('mnet_log');
1175 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1177 /// Launch change of precision for field ip
1178 $dbman->change_field_precision($table, $field);
1180 /// Main savepoint reached
1181 upgrade_main_savepoint($result, 2009010604);
1184 if ($result && $oldversion < 2009010800) {
1185 /// Update the notifyloginfailures setting.
1186 if ($CFG->notifyloginfailures == 'mainadmin') {
1187 set_config('notifyloginfailures', get_admin()->username);
1188 } else if ($CFG->notifyloginfailures == 'alladmins') {
1189 set_config('notifyloginfailures', '$@ALL@$');
1191 set_config('notifyloginfailures', '');
1194 /// Main savepoint reached
1195 upgrade_main_savepoint($result, 2009010800);
1198 if ($result && $oldversion < 2009011000) {
1200 /// Changing nullability of field configdata on table block_instance to null
1201 $table = new xmldb_table('block_instance');
1202 $field = new xmldb_field('configdata');
1203 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
1205 /// Launch change of nullability for field configdata
1206 $dbman->change_field_notnull($table, $field);
1208 /// Main savepoint reached
1209 upgrade_main_savepoint($result, 2009011000);
1212 if ($result && $oldversion < 2009011100) {
1213 /// Remove unused settings
1214 unset_config('zip');
1215 unset_config('unzip');
1216 unset_config('adminblocks_initialised');
1218 /// Main savepoint reached
1219 upgrade_main_savepoint($result, 2009011100);
1222 if ($result && $oldversion < 2009011101) {
1223 /// Migrate backup settings to core plugin config table
1224 $configs = $DB->get_records('backup_config');
1225 foreach ($configs as $config) {
1226 set_config($config->name, $config->value, 'backup');
1229 /// Define table to be dropped
1230 $table = new xmldb_table('backup_config');
1232 /// Launch drop table for old backup config
1233 $dbman->drop_table($table);
1235 /// Main savepoint reached
1236 upgrade_main_savepoint($result, 2009011101);
1239 if ($result && $oldversion < 2009011303) {
1241 /// Define table config_log to be created
1242 $table = new xmldb_table('config_log');
1244 /// Adding fields to table config_log
1245 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1246 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1247 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1248 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1249 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1250 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1251 $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1253 /// Adding keys to table config_log
1254 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1255 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1257 /// Adding indexes to table config_log
1258 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1260 /// Launch create table for config_log
1261 $dbman->create_table($table);
1263 /// Main savepoint reached
1264 upgrade_main_savepoint($result, 2009011303);
1267 if ($result && $oldversion < 2009011900) {
1269 /// Define table sessions2 to be dropped
1270 $table = new xmldb_table('sessions2');
1272 /// Conditionally launch drop table for sessions
1273 if ($dbman->table_exists($table)) {
1274 $dbman->drop_table($table);
1277 /// Define table sessions to be dropped
1278 $table = new xmldb_table('sessions');
1280 /// Conditionally launch drop table for sessions
1281 if ($dbman->table_exists($table)) {
1282 $dbman->drop_table($table);
1285 /// Define table sessions to be created
1286 $table = new xmldb_table('sessions');
1288 /// Adding fields to table sessions
1289 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1290 $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1291 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
1292 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1293 $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
1294 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1295 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1296 $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1297 $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1299 /// Adding keys to table sessions
1300 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1301 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1303 /// Adding indexes to table sessions
1304 $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1305 $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1306 $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1307 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1309 /// Launch create table for sessions
1310 $dbman->create_table($table);
1312 /// Main savepoint reached
1313 upgrade_main_savepoint($result, 2009011900);
1316 if ($result && $oldversion < 2009012901) {
1317 // NOTE: this table may already exist, see beginning of this file ;-)
1319 /// Define table upgrade_log to be created
1320 $table = new xmldb_table('upgrade_log');
1322 /// Adding fields to table upgrade_log
1323 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1324 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1325 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1326 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1327 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1328 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1329 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1330 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1331 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1333 /// Adding keys to table upgrade_log
1334 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1335 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1337 /// Adding indexes to table upgrade_log
1338 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1339 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
1341 /// Conditionally launch create table for upgrade_log
1342 if (!$dbman->table_exists($table)) {
1343 $dbman->create_table($table);
1346 /// Main savepoint reached
1347 upgrade_main_savepoint($result, 2009012901);
1350 if ($result && $oldversion < 2009021800) {
1351 // Converting format of grade conditions, if any exist, to percentages.
1353 UPDATE {course_modules_availability} SET grademin=(
1354 SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
1355 /(gi.grademax-gi.grademin)
1356 FROM {grade_items} gi
1357 WHERE gi.id={course_modules_availability}.gradeitemid)
1358 WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1360 UPDATE {course_modules_availability} SET grademax=(
1361 SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
1362 /(gi.grademax-gi.grademin)
1363 FROM {grade_items} gi
1364 WHERE gi.id={course_modules_availability}.gradeitemid)
1365 WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1367 /// Main savepoint reached
1368 upgrade_main_savepoint($result, 2009021800);
1370 if ($result && $oldversion < 2009021801) {
1371 /// Define field backuptype to be added to backup_log
1372 $table = new xmldb_table('backup_log');
1373 $field = new xmldb_field('backuptype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'info');
1374 /// Conditionally Launch add field backuptype and set all old records as 'scheduledbackup' records.
1375 if (!$dbman->field_exists($table, $field)) {
1376 $dbman->add_field($table, $field);
1377 $DB->execute("UPDATE {backup_log} SET backuptype='scheduledbackup'");
1380 /// Main savepoint reached
1381 upgrade_main_savepoint($result, 2009021801);
1383 /// Add default sort order for question types.
1384 if ($result && $oldversion < 2009030300) {
1385 set_config('multichoice_sortorder', 1, 'question');
1386 set_config('truefalse_sortorder', 2, 'question');
1387 set_config('shortanswer_sortorder', 3, 'question');
1388 set_config('numerical_sortorder', 4, 'question');
1389 set_config('calculated_sortorder', 5, 'question');
1390 set_config('essay_sortorder', 6, 'question');
1391 set_config('match_sortorder', 7, 'question');
1392 set_config('randomsamatch_sortorder', 8, 'question');
1393 set_config('multianswer_sortorder', 9, 'question');
1394 set_config('description_sortorder', 10, 'question');
1395 set_config('random_sortorder', 11, 'question');
1396 set_config('missingtype_sortorder', 12, 'question');
1398 upgrade_main_savepoint($result, 2009030300);
1400 if ($result && $oldversion < 2009030501) {
1401 /// setup default repository plugins
1402 require_once($CFG->dirroot . '/repository/lib.php');
1403 repository_setup_default_plugins();
1404 /// Main savepoint reached
1405 upgrade_main_savepoint($result, 2009030501);
1408 /// MDL-18132 replace the use a new Role allow switch settings page, instead of
1409 /// $CFG->allowuserswitchrolestheycantassign
1410 if ($result && $oldversion < 2009032000) {
1411 /// First create the new table.
1412 $table = new xmldb_table('role_allow_switch');
1414 /// Adding fields to table role_allow_switch
1415 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1416 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1417 $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1419 /// Adding keys to table role_allow_switch
1420 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1421 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
1422 $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
1424 /// Adding indexes to table role_allow_switch
1425 $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1427 /// Conditionally launch create table for role_allow_switch
1428 if (!$dbman->table_exists($table)) {
1429 $dbman->create_table($table);
1432 /// Main savepoint reached
1433 upgrade_main_savepoint($result, 2009032000);
1436 if ($result && $oldversion < 2009032001) {
1437 /// Copy from role_allow_assign into the new table.
1438 $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1439 SELECT roleid, allowassign FROM {role_allow_assign}');
1441 /// Unset the config variable used in 1.9.
1442 unset_config('allowuserswitchrolestheycantassign');
1444 /// Main savepoint reached
1445 upgrade_main_savepoint($result, 2009032001);
1448 if ($result && $oldversion < 2009033100) {
1449 require_once("$CFG->dirroot/filter/tex/lib.php");
1450 filter_tex_updatedcallback(null);
1451 /// Main savepoint reached
1452 upgrade_main_savepoint($result, 2009033100);
1455 if ($result && $oldversion < 2009040300) {
1457 /// Define table filter_active to be created
1458 $table = new xmldb_table('filter_active');
1460 /// Adding fields to table filter_active
1461 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1462 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1463 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1464 $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1465 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1467 /// Adding keys to table filter_active
1468 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1469 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1471 /// Adding indexes to table filter_active
1472 $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1474 /// Conditionally launch create table for filter_active
1475 if (!$dbman->table_exists($table)) {
1476 $dbman->create_table($table);
1479 /// Main savepoint reached
1480 upgrade_main_savepoint($result, 2009040300);
1483 if ($result && $oldversion < 2009040301) {
1485 /// Define table filter_config to be created
1486 $table = new xmldb_table('filter_config');
1488 /// Adding fields to table filter_config
1489 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1490 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1491 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1492 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1493 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1495 /// Adding keys to table filter_config
1496 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1497 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1499 /// Adding indexes to table filter_config
1500 $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1502 /// Conditionally launch create table for filter_config
1503 if (!$dbman->table_exists($table)) {
1504 $dbman->create_table($table);
1507 /// Main savepoint reached
1508 upgrade_main_savepoint($result, 2009040301);
1511 if ($result && $oldversion < 2009040302) {
1512 /// Transfer current settings from $CFG->textfilters
1513 $disabledfilters = filter_get_all_installed();
1514 if (empty($CFG->textfilters)) {
1515 $activefilters = array();
1517 $activefilters = explode(',', $CFG->textfilters);
1519 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1521 foreach ($activefilters as $filter) {
1522 filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1524 unset($disabledfilters[$filter]);
1526 foreach ($disabledfilters as $filter => $notused) {
1527 filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1531 /// Main savepoint reached
1532 upgrade_main_savepoint($result, 2009040302);
1535 if ($result && $oldversion < 2009040600) {
1536 /// Ensure that $CFG->stringfilters is set.
1537 if (empty($CFG->stringfilters)) {
1538 if (!empty($CFG->filterall)) {
1539 set_config('stringfilters', $CFG->textfilters);
1541 set_config('stringfilters', '');
1545 set_config('filterall', !empty($CFG->stringfilters));
1546 unset_config('textfilters');
1548 /// Main savepoint reached
1549 upgrade_main_savepoint($result, 2009040600);
1552 if ($result && $oldversion < 2009041700) {
1553 /// To ensure the UI remains consistent with no behaviour change, any
1554 /// 'until' date in an activity condition should have 1 second subtracted
1555 /// (to go from 0:00 on the following day to 23:59 on the previous one).
1556 $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
1557 require_once($CFG->dirroot . '/course/lib.php');
1558 rebuild_course_cache(0, true);
1560 /// Main savepoint reached
1561 upgrade_main_savepoint($result, 2009041700);
1564 if ($result && $oldversion < 2009042600) {
1565 /// Deleting orphaned messages from deleted users.
1566 require_once($CFG->dirroot.'/message/lib.php');
1567 /// Detect deleted users with messages sent(useridfrom) and not read
1568 if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
1570 JOIN {message} m ON m.useridfrom = u.id
1571 WHERE u.deleted = ?', array(1))) {
1572 foreach ($deletedusers as $deleteduser) {
1573 message_move_userfrom_unread2read($deleteduser->id); // move messages
1576 /// Main savepoint reached
1577 upgrade_main_savepoint($result, 2009042600);
1580 /// Dropping all enums/check contraints from core. MDL-18577
1581 if ($result && $oldversion < 2009042700) {
1583 /// Changing list of values (enum) of field stattype on table stats_daily to none
1584 $table = new xmldb_table('stats_daily');
1585 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1587 /// Launch change of list of values for field stattype
1588 $dbman->drop_enum_from_field($table, $field);
1590 /// Changing list of values (enum) of field stattype on table stats_weekly to none
1591 $table = new xmldb_table('stats_weekly');
1592 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1594 /// Launch change of list of values for field stattype
1595 $dbman->drop_enum_from_field($table, $field);
1597 /// Changing list of values (enum) of field stattype on table stats_monthly to none
1598 $table = new xmldb_table('stats_monthly');
1599 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1601 /// Launch change of list of values for field stattype
1602 $dbman->drop_enum_from_field($table, $field);
1604 /// Changing list of values (enum) of field publishstate on table post to none
1605 $table = new xmldb_table('post');
1606 $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
1608 /// Launch change of list of values for field publishstate
1609 $dbman->drop_enum_from_field($table, $field);
1611 /// Main savepoint reached
1612 upgrade_main_savepoint($result, 2009042700);
1615 if ($result && $oldversion < 2009043000) {
1616 unset_config('grade_report_showgroups');
1617 upgrade_main_savepoint($result, 2009043000);
1620 if ($result && $oldversion < 2009050600) {
1621 /// Site front page blocks need to be moved due to page name change.
1622 $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
1624 /// Main savepoint reached
1625 upgrade_main_savepoint($result, 2009050600);
1628 if ($result && $oldversion < 2009050601) {
1630 /// Define table block_instance to be renamed to block_instances
1631 $table = new xmldb_table('block_instance');
1633 /// Launch rename table for block_instance
1634 $dbman->rename_table($table, 'block_instances');
1636 /// Main savepoint reached
1637 upgrade_main_savepoint($result, 2009050601);
1640 if ($result && $oldversion < 2009050602) {
1642 /// Define table block_instance to be renamed to block_instance_old
1643 $table = new xmldb_table('block_pinned');
1645 /// Launch rename table for block_instance
1646 $dbman->rename_table($table, 'block_pinned_old');
1648 /// Main savepoint reached
1649 upgrade_main_savepoint($result, 2009050602);
1652 if ($result && $oldversion < 2009050603) {
1654 /// Define table block_instance_old to be created
1655 $table = new xmldb_table('block_instance_old');
1657 /// Adding fields to table block_instance_old
1658 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1659 $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1660 $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1661 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1662 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
1663 $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
1664 $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
1665 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1666 $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1668 /// Adding keys to table block_instance_old
1669 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1670 $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
1672 /// Adding indexes to table block_instance_old
1673 $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1674 $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
1676 /// Conditionally launch create table for block_instance_old
1677 if (!$dbman->table_exists($table)) {
1678 $dbman->create_table($table);
1681 /// Main savepoint reached
1682 upgrade_main_savepoint($result, 2009050603);
1685 if ($result && $oldversion < 2009050604) {
1686 /// Copy current blocks data from block_instances to block_instance_old
1687 $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
1688 SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
1690 upgrade_main_savepoint($result, 2009050604);
1693 if ($result && $oldversion < 2009050605) {
1695 /// Define field multiple to be dropped from block
1696 $table = new xmldb_table('block');
1697 $field = new xmldb_field('multiple');
1699 /// Conditionally launch drop field multiple
1700 if ($dbman->field_exists($table, $field)) {
1701 $dbman->drop_field($table, $field);
1704 /// Main savepoint reached
1705 upgrade_main_savepoint($result, 2009050605);
1708 if ($result && $oldversion < 2009050606) {
1709 $table = new xmldb_table('block_instances');
1711 /// Rename field weight on table block_instances to defaultweight
1712 $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'position');
1713 $dbman->rename_field($table, $field, 'defaultweight');
1715 /// Rename field position on table block_instances to defaultregion
1716 $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
1717 $dbman->rename_field($table, $field, 'defaultregion');
1719 /// Main savepoint reached
1720 upgrade_main_savepoint($result, 2009050606);
1723 if ($result && $oldversion < 2009050607) {
1724 /// Changing precision of field defaultregion on table block_instances to (16)
1725 $table = new xmldb_table('block_instances');
1726 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
1728 /// Launch change of precision for field defaultregion
1729 $dbman->change_field_precision($table, $field);
1731 /// Main savepoint reached
1732 upgrade_main_savepoint($result, 2009050607);
1735 if ($result && $oldversion < 2009050608) {
1736 /// Change regions to the new notation
1737 $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1738 $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1739 $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1740 // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1741 // flex page course format. Hopefully this new value is an adequate alternative.
1743 /// Main savepoint reached
1744 upgrade_main_savepoint($result, 2009050608);
1747 if ($result && $oldversion < 2009050609) {
1749 /// Define key blockname (unique) to be added to block
1750 $table = new xmldb_table('block');
1751 $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1753 /// Launch add key blockname
1754 $dbman->add_key($table, $key);
1756 /// Main savepoint reached
1757 upgrade_main_savepoint($result, 2009050609);
1760 if ($result && $oldversion < 2009050610) {
1761 $table = new xmldb_table('block_instances');
1763 /// Define field blockname to be added to block_instances
1764 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
1765 if (!$dbman->field_exists($table, $field)) {
1766 $dbman->add_field($table, $field);
1769 /// Define field contextid to be added to block_instances
1770 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
1771 if (!$dbman->field_exists($table, $field)) {
1772 $dbman->add_field($table, $field);
1775 /// Define field showinsubcontexts to be added to block_instances
1776 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
1777 if (!$dbman->field_exists($table, $field)) {
1778 $dbman->add_field($table, $field);
1781 /// Define field subpagepattern to be added to block_instances
1782 $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
1783 if (!$dbman->field_exists($table, $field)) {
1784 $dbman->add_field($table, $field);
1787 /// Main savepoint reached
1788 upgrade_main_savepoint($result, 2009050610);
1791 if ($result && $oldversion < 2009050611) {
1792 $table = new xmldb_table('block_instances');
1794 /// Fill in blockname from blockid
1795 $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1797 /// Set showinsubcontexts = 0 for all rows.
1798 $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1800 /// Main savepoint reached
1801 upgrade_main_savepoint($result, 2009050611);
1804 if ($result && $oldversion < 2009050612) {
1806 /// Rename field pagetype on table block_instances to pagetypepattern
1807 $table = new xmldb_table('block_instances');
1808 $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
1810 /// Launch rename field pagetype
1811 $dbman->rename_field($table, $field, 'pagetypepattern');
1813 /// Main savepoint reached
1814 upgrade_main_savepoint($result, 2009050612);
1817 if ($result && $oldversion < 2009050613) {
1818 /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1821 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
1822 $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1823 pagetypepattern = 'site-index',
1824 subpagepattern = NULL
1825 WHERE pagetypepattern = 'site-index'");
1828 $DB->execute("UPDATE {block_instances} SET
1832 JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1833 WHERE {course}.id = pageid
1835 pagetypepattern = 'course-view-*',
1836 subpagepattern = NULL
1837 WHERE pagetypepattern = 'course-view'");
1840 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1841 $DB->execute("UPDATE {block_instances} SET
1842 contextid = " . $syscontext->id . ",
1843 pagetypepattern = 'admin-*',
1844 subpagepattern = NULL
1845 WHERE pagetypepattern = 'admin'");
1848 $DB->execute("UPDATE {block_instances} SET
1852 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1853 WHERE {user}.id = pageid
1855 pagetypepattern = 'my-index',
1856 subpagepattern = NULL
1857 WHERE pagetypepattern = 'my-index'");
1860 $DB->execute("UPDATE {block_instances} SET
1861 contextid = " . $syscontext->id . ",
1862 pagetypepattern = 'tag-index',
1863 subpagepattern = pageid
1864 WHERE pagetypepattern = 'tag-index'");
1867 $DB->execute("UPDATE {block_instances} SET
1871 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1872 WHERE {user}.id = pageid
1874 pagetypepattern = 'blog-index',
1875 subpagepattern = NULL
1876 WHERE pagetypepattern = 'blog-view'");
1879 $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1880 foreach ($moduleswithblocks as $modname) {
1881 if (!$dbman->table_exists($modname)) {
1884 $DB->execute("UPDATE {block_instances} SET
1888 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1889 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1890 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1891 WHERE {{$modname}}.id = pageid
1893 pagetypepattern = 'blog-index',
1894 subpagepattern = NULL
1895 WHERE pagetypepattern = 'blog-view'");
1898 /// Main savepoint reached
1899 upgrade_main_savepoint($result, 2009050613);
1902 if ($result && $oldversion < 2009050614) {
1903 /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
1904 $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
1906 /// Main savepoint reached
1907 upgrade_main_savepoint($result, 2009050614);
1910 if ($result && $oldversion < 2009050615) {
1911 $table = new xmldb_table('block_instances');
1913 /// Changing nullability of field blockname on table block_instances to not null
1914 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
1915 $dbman->change_field_notnull($table, $field);
1917 /// Changing nullability of field contextid on table block_instances to not null
1918 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
1919 $dbman->change_field_notnull($table, $field);
1921 /// Changing nullability of field showinsubcontexts on table block_instances to not null
1922 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
1923 $dbman->change_field_notnull($table, $field);
1925 /// Main savepoint reached
1926 upgrade_main_savepoint($result, 2009050615);
1929 if ($result && $oldversion < 2009050616) {
1930 /// Add exiting sticky blocks.
1931 $blocks = $DB->get_records('block');
1932 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1933 $newregions = array(
1936 'c' => 'course-view-top',
1938 $stickyblocks = $DB->get_recordset('block_pinned_old');
1939 foreach ($stickyblocks as $stickyblock) {
1940 $newblock = new object();
1941 $newblock->blockname = $blocks[$stickyblock->blockid]->name;
1942 $newblock->contextid = $syscontext->id;
1943 $newblock->showinsubcontexts = 1;
1944 switch ($stickyblock->pagetype) {
1946 $newblock->pagetypepattern = 'course-view-*';
1949 $newblock->pagetypepattern = $stickyblock->pagetype;
1951 $newblock->defaultregion = $newregions[$stickyblock->position];
1952 $newblock->defaultweight = $stickyblock->weight;
1953 $newblock->configdata = $stickyblock->configdata;
1954 $newblock->visible = 1;
1955 $DB->insert_record('block_instances', $newblock);
1958 /// Main savepoint reached
1959 upgrade_main_savepoint($result, 2009050616);
1962 if ($result && $oldversion < 2009050617) {
1964 /// Define table block_positions to be created
1965 $table = new xmldb_table('block_positions');
1967 /// Adding fields to table block_positions
1968 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1969 $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1970 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1971 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1972 $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1973 $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1');
1974 $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1975 $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1977 /// Adding keys to table block_positions
1978 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1979 $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
1980 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1982 /// Adding indexes to table block_positions
1983 $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
1985 /// Conditionally launch create table for block_positions
1986 if (!$dbman->table_exists($table)) {
1987 $dbman->create_table($table);
1990 /// Main savepoint reached
1991 upgrade_main_savepoint($result, 2009050617);
1994 if ($result && $oldversion < 2009050618) {
1995 /// And block instances with visible = 0, copy that information to block_positions
1996 $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
1997 SELECT id, contextid,
1998 CASE WHEN pagetypepattern = 'course-view-*' THEN
1999 (SELECT " . $DB->sql_concat("'course-view-'", 'format') . "
2001 JOIN {context} ON {course}.id = {context}.instanceid
2002 WHERE {context}.id = contextid)
2003 ELSE pagetypepattern END,
2004 CASE WHEN subpagepattern IS NULL THEN ''
2005 ELSE subpagepattern END,
2006 0, defaultregion, defaultweight
2007 FROM {block_instances} WHERE visible = 0 AND pagetypepattern <> 'admin-*'");
2009 /// Main savepoint reached
2010 upgrade_main_savepoint($result, 2009050618);
2013 if ($result && $oldversion < 2009050619) {
2014 $table = new xmldb_table('block_instances');
2016 /// Define field blockid to be dropped from block_instances
2017 $field = new xmldb_field('blockid');
2018 if ($dbman->field_exists($table, $field)) {
2019 /// Before dropping the field, drop dependent indexes
2020 $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
2021 if ($dbman->index_exists($table, $index)) {
2022 /// Launch drop index blockid
2023 $dbman->drop_index($table, $index);
2025 $dbman->drop_field($table, $field);
2028 /// Define field pageid to be dropped from block_instances
2029 $field = new xmldb_field('pageid');
2030 if ($dbman->field_exists($table, $field)) {
2031 /// Before dropping the field, drop dependent indexes
2032 $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
2033 if ($dbman->index_exists($table, $index)) {
2034 /// Launch drop index pageid
2035 $dbman->drop_index($table, $index);
2037 $dbman->drop_field($table, $field);
2040 /// Define field visible to be dropped from block_instances
2041 $field = new xmldb_field('visible');
2042 if ($dbman->field_exists($table, $field)) {
2043 $dbman->drop_field($table, $field);
2046 /// Main savepoint reached
2047 upgrade_main_savepoint($result, 2009050619);
2050 if ($result && $oldversion < 2009051200) {
2051 /// Let's check the status of mandatory mnet_host records, fixing them
2052 /// and moving "orphan" users to default localhost record. MDL-16879
2053 echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
2054 upgrade_fix_incorrect_mnethostids();
2056 /// Main savepoint reached
2057 upgrade_main_savepoint($result, 2009051200);
2061 if ($result && $oldversion < 2009051700) {
2062 /// migrate editor settings
2063 if (empty($CFG->htmleditor)) {
2064 set_config('texteditors', 'textarea');
2066 set_config('texteditors', 'tinymce,textarea');
2069 unset_config('htmleditor');
2070 unset_config('defaulthtmleditor');
2072 /// Main savepoint reached
2073 upgrade_main_savepoint($result, 2009051700);
2076 if ($result && $oldversion < 2009060200) {
2077 /// Define table files_cleanup to be dropped - not needed
2078 $table = new xmldb_table('files_cleanup');
2080 /// Conditionally launch drop table for files_cleanup
2081 if ($dbman->table_exists($table)) {
2082 $dbman->drop_table($table);
2085 /// Main savepoint reached
2086 upgrade_main_savepoint($result, 2009060200);
2089 if ($result && $oldversion < 2009061300) {
2090 //TODO: copy this to the very beginning of this upgrade script so that we may log upgrade queries
2092 /// Define table log_queries to be created
2093 $table = new xmldb_table('log_queries');
2095 /// Adding fields to table log_queries
2096 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2097 $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2098 $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
2099 $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
2100 $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2101 $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2102 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2103 $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
2104 $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2106 /// Adding keys to table log_queries
2107 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2109 /// Conditionally launch create table for log_queries
2110 if (!$dbman->table_exists($table)) {
2111 $dbman->create_table($table);
2114 /// Main savepoint reached
2115 upgrade_main_savepoint($result, 2009061300);
2118 /// Repeat 2009050607 upgrade step, which Petr commented out becuase of XMLDB
2119 /// stupidity, so lots of peopel will have missed.
2120 if ($result && $oldversion < 2009061600) {
2121 /// Changing precision of field defaultregion on table block_instances to (16)
2122 $table = new xmldb_table('block_instances');
2123 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
2125 /// Launch change of precision for field defaultregion
2126 $dbman->change_field_precision($table, $field);
2128 /// Main savepoint reached
2129 upgrade_main_savepoint($result, 2009061600);
2132 if ($result && $oldversion < 2009061702) {
2133 // standardizing plugin names
2134 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
2135 foreach ($configs as $config) {
2136 $result = $result && unset_config($config->name, $config->plugin); /// unset old config
2137 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
2138 $result = $result && set_config($config->name, $config->value, $config->plugin); /// set new config
2142 upgrade_main_savepoint($result, 2009061702);
2145 if ($result && $oldversion < 2009061703) {
2146 // standardizing plugin names
2147 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
2148 foreach ($configs as $config) {
2149 $result = $result && unset_config($config->name, $config->plugin); /// unset old config
2150 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
2151 $result = $result && set_config($config->name, $config->value, $config->plugin); /// set new config
2155 upgrade_main_savepoint($result, 2009061703);
2158 if ($result && $oldversion < 2009061704) {
2159 // change component string in capability records to new "_" format
2160 if ($caps = $DB->get_records('capabilities')) {
2161 foreach ($caps as $cap) {
2162 $cap->component = str_replace('/', '_', $cap->component);
2163 $DB->update_record('capabilities', $cap);
2167 upgrade_main_savepoint($result, 2009061704);
2170 if ($result && $oldversion < 2009061705) {
2171 // change component string in events_handlers records to new "_" format
2172 if ($handlers = $DB->get_records('events_handlers')) {
2173 foreach ($handlers as $handler) {
2174 $handler->handlermodule = str_replace('/', '_', $handler->handlermodule);
2175 $DB->update_record('events_handlers', $handler);
2179 upgrade_main_savepoint($result, 2009061705);
2182 if ($result && $oldversion < 2009061706) {
2183 // change component string in message_providers records to new "_" format
2184 if ($mps = $DB->get_records('message_providers')) {
2185 foreach ($mps as $mp) {
2186 $mp->component = str_replace('/', '_', $mp->component);
2187 $DB->update_record('message_providers', $cap);
2191 upgrade_main_savepoint($result, 2009061706);
2194 if ($result && $oldversion < 2009063000) {
2195 // upgrade format of _with_advanced settings - quiz only
2196 // note: this can be removed later, not needed for upgrades from 1.9.x
2197 if ($quiz = get_config('quiz')) {
2198 foreach ($quiz as $name=>$value) {
2199 if (strpos($name, 'fix_') !== 0) {
2202 $newname = substr($name,4).'_adv';
2203 set_config($newname, $value, 'quiz');
2204 unset_config($name, 'quiz');
2207 upgrade_main_savepoint($result, 2009063000);
2210 if ($result && $oldversion < 2009071000) {
2212 /// Rename field contextid on table block_instances to parentcontextid
2213 $table = new xmldb_table('block_instances');
2214 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
2216 /// Launch rename field parentcontextid
2217 $dbman->rename_field($table, $field, 'parentcontextid');
2219 /// Main savepoint reached
2220 upgrade_main_savepoint($result, 2009071000);
2223 if ($result && $oldversion < 2009071300) {
2225 /// Create contexts for every block. In the past, only non-sticky course block had contexts.
2226 /// This is a copy of the code in create_contexts.
2227 $sql = "INSERT INTO {context} (contextlevel, instanceid)
2228 SELECT " . CONTEXT_BLOCK . ", bi.id
2229 FROM {block_instances} bi
2230 WHERE NOT EXISTS (SELECT 'x'
2232 WHERE bi.id = ctx.instanceid AND ctx.contextlevel=" . CONTEXT_BLOCK . ")";
2235 /// TODO MDL-19776 We should not really use API funcitons in upgrade.
2236 /// If MDL-19776 is done, we can remove this whole upgrade block.
2237 build_context_path();
2239 /// Main savepoint reached
2240 upgrade_main_savepoint($result, 2009071300);
2243 if ($result && $oldversion < 2009071600) {
2245 /// Define field summaryformat to be added to post
2246 $table = new xmldb_table('post');
2247 $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
2249 /// Conditionally launch add field summaryformat
2250 if (!$dbman->field_exists($table, $field)) {
2251 $dbman->add_field($table, $field);
2254 /// Main savepoint reached
2255 upgrade_main_savepoint($result, 2009071600);
2258 if ($result && $oldversion < 2009072400) {
2260 /// Define table comments to be created
2261 $table = new xmldb_table('comments');
2263 /// Adding fields to table comments
2264 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2265 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2266 $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2267 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2268 $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2269 $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2270 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2271 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2273 /// Adding keys to table comments
2274 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2276 /// Conditionally launch create table for comments
2277 if (!$dbman->table_exists($table)) {
2278 $dbman->create_table($table);
2281 /// Main savepoint reached
2282 upgrade_main_savepoint($result, 2009072400);
2286 * This upgrade is to set up the new navigation blocks that have been developed
2287 * as part of Moodle 2.0
2288 * Now I [Sam Hemelryk] hit a conundrum while exploring how to go about this
2289 * as not only do we want to install the new blocks but we also want to set up
2290 * default instances of them, and at the same time remove instances of the blocks
2291 * that were/will-be outmoded by the two new navigation blocks.
2292 * After talking it through with Tim Hunt {@link http://moodle.org/mod/cvsadmin/view.php?conversationid=3112}
2293 * we decided that the best way to go about this was to put the bulk of the
2294 * upgrade operation into core upgrade `here` but to let the plugins block
2295 * still install the blocks.
2296 * This leaves one hairy end in that we will create block_instances within the
2297 * DB before the blocks themselves are created within the DB
2299 if ($result && $oldversion < 2009082800) {
2301 echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2303 // Get the system context so we can set the block instances to it
2304 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2306 // An array to contain the new block instances we will create
2307 $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
2308 // The new global navigation block instance as a stdClass
2309 $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
2310 $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
2311 $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutly everywhere
2312 $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
2313 $newblockinstances['globalnavigation']->subpagetypepattern = null;
2314 $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
2315 $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
2316 $newblockinstances['globalnavigation']->configdata = '';
2317 // The new settings navigation block instance as a stdClass
2318 $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
2319 $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
2320 $newblockinstances['settingsnavigation']->showinsubcontexts = true;
2321 $newblockinstances['settingsnavigation']->pagetypepattern = '*';
2322 $newblockinstances['settingsnavigation']->subpagetypepattern = null;
2323 $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
2324 $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
2325 $newblockinstances['settingsnavigation']->configdata = '';
2327 // Blocks that are outmoded and for whom the bells will toll... by which I
2328 // mean we will delete all instances of
2329 $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
2330 $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
2331 unset($outmodedblocks);
2332 // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
2333 // cut down the number of delete queries we will need to run
2334 $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
2336 $contextids = array();
2337 $instanceids = array();
2338 // Iterate through all block instances
2339 foreach ($allblockinstances as $blockinstance) {
2340 if (!in_array($blockinstance->parentcontextid, $contextids)) {
2341 $contextids[] = $blockinstance->parentcontextid;
2343 // If we have over 1000 contexts clean them up and reset the array
2344 // this ensures we don't hit any nasty memory limits or such
2345 if (count($contextids) > 1000) {
2346 $result = $result && upgrade_cleanup_unwanted_block_contexts($contextids);
2347 $contextids = array();
2350 if (!in_array($blockinstance->id, $instanceids)) {
2351 $instanceids[] = $blockinstance->id;
2352 // If we have more than 1000 block instances now remove all block positions
2353 // and empty the array
2354 if (count($contextids) > 1000) {
2355 $instanceidstring = join(',',$instanceids);
2356 $result = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2357 $instanceids = array();
2362 $result = $result && upgrade_cleanup_unwanted_block_contexts($contextids);
2364 $instanceidstring = join(',',$instanceids);
2365 $outcome1 = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2367 unset($allblockinstances);
2369 unset($instanceids);
2370 unset($instanceidstring);
2372 // Now remove the actual block instance
2373 $result = $result && $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
2374 unset($outmodedblocksstring);
2376 // Insert the new block instances. Remember they have not been installed yet
2377 // however this should not be a problem
2378 foreach ($newblockinstances as $blockinstance) {
2379 $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
2380 // Ensure the block context is created.
2381 get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
2383 unset($newblockinstances);
2385 upgrade_main_savepoint($result, 2009082800);
2386 // The end of the navigation upgrade
2389 if ($result && $oldversion < 2009090800){
2390 //insert new record for log_display table
2391 //used to record tag update.
2392 if (!$DB->record_exists('log_display', array('action'=>'update', 'module'=>'tag'))) {
2393 $log_action = new object();
2394 $log_action->module = 'tag';
2395 $log_action->action = 'update';
2396 $log_action->mtable = 'tag';
2397 $log_action->field = 'name';
2399 $result = $result && $DB->insert_record('log_display', $log_action);
2401 upgrade_main_savepoint($result, 2009090800);
2404 if ($result && $oldversion < 2009100601) {
2405 // drop all previous tables defined during the dev phase
2406 $dropold = array('external_services_users', 'external_services_functions', 'external_services', 'external_functions');
2407 foreach ($dropold as $tablename) {
2408 $table = new xmldb_table($tablename);
2409 if ($dbman->table_exists($table)) {
2410 $dbman->drop_table($table);
2413 upgrade_main_savepoint($result, 2009100601);
2416 if ($result && $oldversion < 2009100602) {
2417 /// Define table external_functions to be created
2418 $table = new xmldb_table('external_functions');
2420 /// Adding fields to table external_functions
2421 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2422 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2423 $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2424 $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2425 $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2426 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2428 /// Adding keys to table external_functions
2429 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2431 /// Adding indexes to table external_functions
2432 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2434 /// Launch create table for external_functions
2435 $dbman->create_table($table);
2437 /// Main savepoint reached
2438 upgrade_main_savepoint($result, 2009100602);
2441 if ($result && $oldversion < 2009100603) {
2442 /// Define table external_services to be created
2443 $table = new xmldb_table('external_services');
2445 /// Adding fields to table external_services
2446 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2447 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2448 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2449 $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
2450 $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2451 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
2453 /// Adding keys to table external_services
2454 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2456 /// Adding indexes to table external_services
2457 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2459 /// Launch create table for external_services
2460 $dbman->create_table($table);
2462 /// Main savepoint reached
2463 upgrade_main_savepoint($result, 2009100603);
2466 if ($result && $oldversion < 2009100604) {
2467 /// Define table external_services_functions to be created
2468 $table = new xmldb_table('external_services_functions');
2470 /// Adding fields to table external_services_functions
2471 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2472 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2473 $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2475 /// Adding keys to table external_services_functions
2476 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2477 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2479 /// Launch create table for external_services_functions
2480 $dbman->create_table($table);
2482 /// Main savepoint reached
2483 upgrade_main_savepoint($result, 2009100604);
2486 if ($result && $oldversion < 2009100605) {
2487 /// Define table external_services_users to be created
2488 $table = new xmldb_table('external_services_users');
2490 /// Adding fields to table external_services_users
2491 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2492 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2493 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2494 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2495 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2496 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2498 /// Adding keys to table external_services_users
2499 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2500 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2501 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2503 /// Launch create table for external_services_users
2504 $dbman->create_table($table);
2506 /// Main savepoint reached
2507 upgrade_main_savepoint($result, 2009100605);
2510 if ($result && $oldversion < 2009102600) {
2512 /// Define table external_tokens to be created
2513 $table = new xmldb_table('external_tokens');
2515 /// Adding fields to table external_tokens
2516 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2517 $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
2518 $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2519 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2520 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2521 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
2522 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2523 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2524 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2525 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2526 $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2528 /// Adding keys to table external_tokens
2529 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2530 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2531 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2532 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2534 /// Launch create table for external_tokens
2535 $dbman->create_table($table);
2537 /// Main savepoint reached
2538 upgrade_main_savepoint($result, 2009102600);
2541 if ($result && $oldversion < 2009103000) {
2543 /// Define table blog_association to be created
2544 $table = new xmldb_table('blog_association');
2546 /// Adding fields to table blog_association
2547 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2548 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2549 $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2551 /// Adding keys to table blog_association
2552 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2553 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2554 $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
2556 /// Conditionally launch create table for blog_association
2557 if (!$dbman->table_exists($table)) {
2558 $dbman->create_table($table);
2561 /// Define table blog_external to be created
2562 $table = new xmldb_table('blog_external');
2564 /// Adding fields to table blog_external
2565 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2566 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2567 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2568 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2569 $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2570 $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2571 $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2572 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2573 $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2575 /// Adding keys to table blog_external
2576 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2577 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2579 /// Conditionally launch create table for blog_external
2580 if ($dbman->table_exists($table)) {
2581 // Delete the existing one first (comes from early dev version)
2582 $dbman->drop_table($table);
2584 $dbman->create_table($table);
2586 // Print notice about need to upgrade bloglevel
2587 if (($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete)) {
2588 echo $OUTPUT->notification(get_string('bloglevelupgradenotice', 'admin'));
2590 // email admins about the need to upgrade their system using the admin/bloglevelupgrade.php script
2591 $admins = get_admins();
2595 $a->sitename = $site->fullname;
2596 $a->fixurl = "$CFG->wwwroot/$CFG->admin/bloglevelupgrade.php";
2598 $subject = get_string('bloglevelupgrade', 'admin');
2599 $description = get_string('bloglevelupgradedescription', 'admin', $a);
2601 // can not use messaging here because it is not configured yet!
2602 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2604 /// Main savepoint reached
2605 upgrade_main_savepoint($result, 2009103000);
2608 if ($result && $oldversion < 2009110400) {
2610 // An array used to store the table name and keys of summary and trust fields
2612 $extendtables = array();
2613 $extendtables['course'] = array('summaryformat');
2614 $extendtables['course_categories'] = array('descriptionformat');
2615 $extendtables['course_request'] = array('summaryformat');
2616 $extendtables['grade_outcomes'] = array('descriptionformat');
2617 $extendtables['groups'] = array('descriptionformat');
2618 $extendtables['groupings'] = array('descriptionformat');
2619 $extendtables['scale'] = array('descriptionformat');
2620 $extendtables['user'] = array('descriptionformat');
2621 $extendtables['user_info_field'] = array('descriptionformat', 'defaultdataformat');
2622 $extendtables['user_info_data'] = array('dataformat');
2624 foreach ($extendtables as $tablestr=>$newfields) {
2625 $table = new xmldb_table($tablestr);
2626 foreach ($newfields as $fieldstr) {
2627 $field = new xmldb_field($fieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2628 // Check that the field doesn't already exists
2629 if (!$dbman->field_exists($table, $field)) {
2630 // Add the new field
2631 $dbman->add_field($table, $field);
2632 // Update the field if the text contains the default FORMAT_MOODLE to FORMAT_HTML
2633 if (($pos = strpos($fieldstr, 'format'))>0) {
2634 upgrade_set_timeout(60*20); // this may take a little while
2635 $params = array(FORMAT_HTML, '<p%', '%<br />%', FORMAT_MOODLE);
2636 $textfield = substr($fieldstr, 0, $pos);
2637 $DB->execute('UPDATE {'.$tablestr.'} SET '.$fieldstr.'=? WHERE ('.$textfield.' LIKE ? OR '.$textfield.' LIKE ?) AND '.$fieldstr.'=?', $params);
2643 unset($extendtables);
2645 upgrade_main_savepoint($result, 2009110400);
2648 if ($result && $oldversion < 2009110605) {
2650 /// Define field timecreated to be added to external_services
2651 $table = new xmldb_table('external_services');
2652 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'component');
2654 /// Conditionally launch add field timecreated
2655 if (!$dbman->field_exists($table, $field)) {
2656 $dbman->add_field($table, $field);
2659 /// Define field timemodified to be added to external_services
2660 $table = new xmldb_table('external_services');
2661 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'timecreated');
2663 /// Conditionally launch add field timemodified
2664 if (!$dbman->field_exists($table, $field)) {
2665 $dbman->add_field($table, $field);
2668 /// Main savepoint reached
2669 upgrade_main_savepoint($result, 2009110605);
2672 if ($result && $oldversion < 2009111600) {
2674 /// Define field instance to be added to portfolio_tempdata
2675 $table = new xmldb_table('portfolio_tempdata');
2676 $field = new xmldb_field('instance', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'userid');
2678 /// Conditionally launch add field instance
2679 if (!$dbman->field_exists($table, $field)) {
2680 $dbman->add_field($table, $field);
2683 $key = new xmldb_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
2685 /// Launch add key instancefk
2686 $dbman->add_key($table, $key);
2688 /// Main savepoint reached
2689 upgrade_main_savepoint($result, 2009111600);
2692 if ($result && $oldversion < 2009111700) {
2694 /// Define field tempdataid to be added to portfolio_log
2695 $table = new xmldb_table('portfolio_log');
2696 $field = new xmldb_field('tempdataid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'caller_sha1');
2698 /// Conditionally launch add field tempdataid
2699 if (!$dbman->field_exists($table, $field)) {
2700 $dbman->add_field($table, $field);
2703 /// Main savepoint reached
2704 upgrade_main_savepoint($result, 2009111700);
2707 if ($result && $oldversion < 2009111701) {
2709 /// Define field returnurl to be added to portfolio_log
2710 $table = new xmldb_table('portfolio_log');
2711 $field = new xmldb_field('returnurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'tempdataid');
2713 /// Conditionally launch add field returnurl
2714 if (!$dbman->field_exists($table, $field)) {
2715 $dbman->add_field($table, $field);
2718 /// Main savepoint reached
2719 upgrade_main_savepoint($result, 2009111701);
2722 if ($result && $oldversion < 2009111702) {
2724 /// Define field continueurl to be added to portfolio_log
2725 $table = new xmldb_table('portfolio_log');
2726 $field = new xmldb_field('continueurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'returnurl');
2728 /// Conditionally launch add field continueurl
2729 if (!$dbman->field_exists($table, $field)) {
2730 $dbman->add_field($table, $field);
2733 /// Main savepoint reached
2734 upgrade_main_savepoint($result, 2009111702);
2737 if ($result && $oldversion < 2009112400) {
2738 if (empty($CFG->passwordsaltmain)) {
2739 $subject = get_string('check_passwordsaltmain_name', 'report_security');
2740 $description = get_string('check_passwordsaltmain_warning', 'report_security');;
2741 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2743 upgrade_main_savepoint($result, 2009112400);
2746 if ($result && $oldversion < 2010010601) {
2748 /// Define field creatorid to be added to external_tokens
2749 $table = new xmldb_table('external_tokens');
2750 $field = new xmldb_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'contextid');
2752 /// Conditionally launch add field creatorid
2753 if (!$dbman->field_exists($table, $field)) {
2754 $dbman->add_field($table, $field);
2757 /// Define key creatorid (foreign) to be added to external_tokens
2758 $table = new xmldb_table('external_tokens');
2759 $key = new xmldb_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
2761 /// Launch add key creatorid
2762 $dbman->add_key($table, $key);
2764 /// Main savepoint reached
2765 upgrade_main_savepoint($result, 2010010601);
2768 if ($result && $oldversion < 2010011200) {
2769 $table = new xmldb_table('grade_categories');
2770 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
2772 if (!$dbman->field_exists($table, $field)) {
2773 $dbman->add_field($table, $field);
2776 upgrade_main_savepoint($result, 2010011200);
2780 if ($result && $oldversion < 2010012500) {
2781 upgrade_fix_incorrect_mnethostids();
2782 upgrade_main_savepoint($result, 2010012500);
2785 if ($result && $oldversion < 2010012600) {
2786 // do stuff to the mnet table
2787 $table = new xmldb_table('mnet_rpc');
2789 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2790 $dbman->rename_field($table, $field, 'plugintype');
2792 $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2793 $dbman->rename_field($table, $field, 'pluginname');
2795 $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
2796 if (!$dbman->field_exists($table, $field)) {
2797 $dbman->add_field($table, $field);
2800 $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
2801 if (!$dbman->field_exists($table, $field)) {
2802 $dbman->add_field($table, $field);
2805 $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
2806 if (!$dbman->field_exists($table, $field)) {
2807 $dbman->add_field($table, $field);
2810 /// Main savepoint reached
2811 upgrade_main_savepoint($result, 2010012600);
2814 if ($result && $oldversion < 2010012900) {
2816 /// Define table mnet_remote_rpc to be created
2817 $table = new xmldb_table('mnet_remote_rpc');
2819 /// Adding fields to table mnet_remote_rpc
2820 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2821 $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2822 $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
2824 /// Adding keys to table mnet_remote_rpc
2825 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2827 /// Conditionally launch create table for mnet_remote_rpc
2828 if (!$dbman->table_exists($table)) {
2829 $dbman->create_table($table);
2833 /// Define table mnet_remote_service2rpc to be created
2834 $table = new xmldb_table('mnet_remote_service2rpc');
2836 /// Adding fields to table mnet_remote_service2rpc
2837 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2838 $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2839 $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2841 /// Adding keys to table mnet_remote_service2rpc
2842 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2844 /// Adding indexes to table mnet_remote_service2rpc
2845 $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2847 /// Conditionally launch create table for mnet_remote_service2rpc
2848 if (!$dbman->table_exists($table)) {
2849 $dbman->create_table($table);
2853 /// Rename field function_name on table mnet_rpc to functionname
2854 $table = new xmldb_table('mnet_rpc');
2855 $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
2857 /// Launch rename field function_name
2858 $dbman->rename_field($table, $field, 'functionname');
2861 /// Rename field xmlrpc_path on table mnet_rpc to xmlrpcpath
2862 $table = new xmldb_table('mnet_rpc');
2863 $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
2865 /// Launch rename field xmlrpc_path
2866 $dbman->rename_field($table, $field, 'xmlrpcpath');
2869 /// Main savepoint reached
2870 upgrade_main_savepoint($result, 2010012900);
2873 if ($result && $oldversion < 2010012901) {
2875 /// Define field plugintype to be added to mnet_remote_rpc
2876 $table = new xmldb_table('mnet_remote_rpc');
2877 $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
2879 /// Conditionally launch add field plugintype
2880 if (!$dbman->field_exists($table, $field)) {
2881 $dbman->add_field($table, $field);
2884 /// Define field pluginname to be added to mnet_remote_rpc
2885 $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
2887 /// Conditionally launch add field pluginname
2888 if (!$dbman->field_exists($table, $field)) {
2889 $dbman->add_field($table, $field);
2892 /// Main savepoint reached
2893 upgrade_main_savepoint($result, 2010012901);
2896 if ($result && $oldversion < 2010012902) {
2898 /// Define field enabled to be added to mnet_remote_rpc
2899 $table = new xmldb_table('mnet_remote_rpc');
2900 $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
2902 /// Conditionally launch add field enabled
2903 if (!$dbman->field_exists($table, $field)) {
2904 $dbman->add_field($table, $field);
2907 /// Main savepoint reached
2908 upgrade_main_savepoint($result, 2010012902);
2911 /// MDL-17863. Increase the portno column length on mnet_host to handle any port number
2912 if ($result && $oldversion < 2010020100) {
2913 /// Changing precision of field portno on table mnet_host to (5)
2914 $table = new xmldb_table('mnet_host');
2915 $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
2917 /// Launch change of precision for field portno
2918 $dbman->change_field_precision($table, $field);
2920 upgrade_main_savepoint($result, 2010020100);
2923 if ($result && $oldversion < 2010020300) {
2925 /// Define field timecreated to be added to user
2926 $table = new xmldb_table('user');
2927 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
2929 if (!$dbman->field_exists($table, $field)) {
2930 /// Launch add field timecreated
2931 $dbman->add_field($table, $field);
2933 $DB->execute("UPDATE {user} SET timecreated = firstaccess");
2935 $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
2938 upgrade_main_savepoint($result, 2010020300);
2941 // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
2942 if ($result && $oldversion < 2010020301) {
2943 if ($preamble = $CFG->filter_tex_latexpreamble) {
2944 $preamble = preg_replace('/^ +/m', '', $preamble);
2945 set_config('filter_tex_latexpreamble', $preamble);
2947 upgrade_main_savepoint($result, 2010020301);
2950 if ($result && $oldversion < 2010021400) {
2951 /// Changes to modinfo mean we need to rebuild course cache
2952 require_once($CFG->dirroot . '/course/lib.php');
2953 rebuild_course_cache(0, true);
2954 upgrade_main_savepoint($result, 2010021400);
2957 if ($result && $oldversion < 2010021800) {
2958 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
2959 upgrade_main_savepoint($result, 2010021800);
2962 if ($result && $oldversion < 2010031800) {
2963 //drop the erroneously created ratings table
2964 $table = new xmldb_table('ratings');
2965 if ($dbman->table_exists($table)) {
2966 $dbman->drop_table($table);
2969 //create the rating table (replaces module specific rating implementations)
2970 $table = new xmldb_table('rating');
2971 if ($dbman->table_exists($table)) {
2972 $dbman->drop_table($table);
2975 /// Adding fields to table rating
2976 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2977 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2979 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2980 $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2981 $table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2982 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2984 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2985 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2987 /// Adding keys to table rating
2988 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2989 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2990 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2992 /// Adding indexes to table rating
2993 $table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
2995 /// Create table for ratings
2996 if (!$dbman->table_exists($table)) {
2997 $dbman->create_table($table);
3000 upgrade_main_savepoint($result, 2010031800);
3003 if ($result && $oldversion < 2010031900) {
3004 // regeneration of sessions is always enabled, no need for this setting any more
3005 unset_config('regenloginsession');
3006 upgrade_main_savepoint($result, 2010031900);
3010 if ($result && $oldversion < 2010032400) {
3011 // Upgrade all of those using the standardold theme to the use the standard
3013 if ($CFG->theme == 'standardold') {
3014 // The config setting that affects the whole site
3015 set_config('theme', 'standard');
3017 // Course Categories
3018 $DB->execute('UPDATE {course_categories} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3020 $DB->execute('UPDATE {course} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3022 $DB->execute('UPDATE {user} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3023 upgrade_main_savepoint($result, 2010032400);
3026 if ($result && $oldversion < 2010033101) {
3028 /// Define field source to be added to files
3029 $table = new xmldb_table('files');
3031 $field = new xmldb_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'timemodified');
3033 /// Conditionally launch add field source
3034 if (!$dbman->field_exists($table, $field)) {
3035 $dbman->add_field($table, $field);
3038 $field = new xmldb_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'source');
3040 /// Conditionally launch add field author
3041 if (!$dbman->field_exists($table, $field)) {
3042 $dbman->add_field($table, $field);
3045 $field = new xmldb_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'author');
3047 /// Conditionally launch add field license
3048 if (!$dbman->field_exists($table, $field)) {
3049 $dbman->add_field($table, $field);
3052 /// Define table license to be created
3053 $table = new xmldb_table('license');
3055 /// Adding fields to table license
3056 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3057 $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3058 $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
3059 $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3060 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
3061 $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
3063 /// Adding keys to table license
3064 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3066 /// Conditionally launch create table for license
3067 if (!$dbman->table_exists($table)) {
3068 $dbman->create_table($table);
3070 $active_licenses = array();
3072 $license = new stdclass;
3074 // add unknown license
3075 $license->shortname = 'unknown';
3076 $license->fullname = 'Unknown license';
3077 $license->source = '';
3078 $license->enabled = 1;
3079 $license->version = '2010033100';
3080 $active_licenses[] = $license->shortname;
3081 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3082 if ($record->version < $license->version) {
3083 // update license record
3084 $license->enabled = $record->enabled;
3085 $license->id = $record->id;
3086 $DB->update_record('license', $license);
3089 $DB->insert_record('license', $license);
3092 // add all rights reserved license
3093 $license->shortname = 'allrightsreserved';
3094 $license->fullname = 'All rights reserved';
3095 $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
3096 $license->enabled = 1;
3097 $license->version = '2010033100';
3098 $active_licenses[] = $license->shortname;
3099 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3100 if ($record->version < $license->version) {
3101 // update license record
3102 $license->id = $record->id;
3103 $license->enabled = $record->enabled;
3104 $DB->update_record('license', $license);
3107 $DB->insert_record('license', $license);
3110 // add public domain license
3111 $license->shortname = 'public';
3112 $license->fullname = 'Public Domain';
3113 $license->source = 'http://creativecommons.org/licenses/publicdomain/';
3114 $license->enabled = 1;
3115 $license->version = '2010033100';
3116 $active_licenses[] = $license->shortname;
3117 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3118 if ($record->version < $license->version) {
3119 // update license record
3120 $license->enabled = $record->enabled;
3121 $license->id = $record->id;
3122 $DB->update_record('license', $license);
3125 $DB->insert_record('license', $license);
3128 // add creative commons license
3129 $license->shortname = 'cc';
3130 $license->fullname = 'Creative Commons';
3131 $license->source = 'http://creativecommons.org/licenses/by/3.0/';
3132 $license->enabled = 1;
3133 $license->version = '2010033100';
3134 $active_licenses[] = $license->shortname;
3135 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3136 if ($record->version < $license->version) {
3137 // update license record
3138 $license->enabled = $record->enabled;
3139 $license->id = $record->id;
3140 $DB->update_record('license', $license);
3143 $DB->insert_record('license', $license);
3146 // add creative commons no derivs license
3147 $license->shortname = 'cc-nd';
3148 $license->fullname = 'Creative Commons - NoDerivs';
3149 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3150 $license->enabled = 1;
3151 $license->version = '2010033100';
3152 $active_licenses[] = $license->shortname;
3153 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3154 if ($record->version < $license->version) {
3155 // update license record
3156 $license->enabled = $record->enabled;
3157 $license->id = $record->id;
3158 $DB->update_record('license', $license);
3161 $DB->insert_record('license', $license);
3164 // add creative commons no commercial no derivs license
3165 $license->shortname = 'cc-nc-nd';
3166 $license->fullname = 'Creative Commons - No Commercial NoDerivs';
3167 $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
3168 $license->enabled = 1;
3169 $license->version = '2010033100';
3170 $active_licenses[] = $license->shortname;
3171 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3172 if ($record->version < $license->version) {
3173 // update license record
3174 $license->enabled = $record->enabled;
3175 $license->id = $record->id;
3176 $DB->update_record('license', $license);
3179 $DB->insert_record('license', $license);
3182 // add creative commons no commercial
3183 $license->shortname = 'cc-nc-nd';
3184 $license->shortname = 'cc-nc';
3185 $license->fullname = 'Creative Commons - No Commercial';
3186 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3187 $license->enabled = 1;
3188 $license->version = '2010033100';
3189 $active_licenses[] = $license->shortname;
3190 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3191 if ($record->version < $license->version) {
3192 // update license record
3193 $license->enabled = $record->enabled;
3194 $license->id = $record->id;
3195 $DB->update_record('license', $license);
3198 $DB->insert_record('license', $license);
3201 // add creative commons no commercial sharealike
3202 $license->shortname = 'cc-nc-sa';
3203 $license->fullname = 'Creative Commons - No Commercial ShareAlike';
3204 $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
3205 $license->enabled = 1;
3206 $license->version = '2010033100';
3207 $active_licenses[] = $license->shortname;
3208 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3209 if ($record->version < $license->version) {
3210 // update license record
3211 $license->enabled = $record->enabled;
3212 $license->id = $record->id;
3213 $DB->update_record('license', $license);
3216 $DB->insert_record('license', $license);
3219 // add creative commons sharealike
3220 $license->shortname = 'cc-sa';
3221 $license->fullname = 'Creative Commons - ShareAlike';
3222 $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
3223 $license->enabled = 1;
3224 $license->version = '2010033100';
3225 $active_licenses[] = $license->shortname;
3226 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3227 if ($record->version < $license->version) {
3228 // update license record
3229 $license->enabled = $record->enabled;
3230 $license->id = $record->id;
3231 $DB->update_record('license', $license);
3234 $DB->insert_record('license', $license);
3237 set_config('licenses', implode(',', $active_licenses));
3238 /// set site default license
3239 set_config('sitedefaultlicense', 'allrightsreserved');
3241 /// Main savepoint reached
3242 upgrade_main_savepoint($result, 2010033101);
3245 if ($result && $oldversion < 2010033102.00) {
3246 // rename course view capability to participate
3247 $params = array('view'=>'moodle/course:view', 'participate'=>'moodle/course:participate');
3248 $sql = "UPDATE {role_capabilities} SET capability = :participate WHERE capability = :view";
3249 $DB->execute($sql, $params);
3250 $sql = "UPDATE {capabilities} SET name = :participate WHERE name = :view";
3251 $DB->execute($sql, $params);
3252 // note: the view capability is readded again at the end of upgrade, but with different meaning
3253 upgrade_main_savepoint($result, 2010033102.00);
3256 if ($result && $oldversion < 2010033102.01) {
3257 // Define field archetype to be added to role table
3258 $table = new xmldb_table('role');
3259 $field = new xmldb_field('archetype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'sortorder');
3260 $dbman->add_field($table, $field);
3261 upgrade_main_savepoint($result, 2010033102.01);
3264 if ($result && $oldversion < 2010033102.02) {
3265 // Set archetype for existing roles and change admin role to manager role
3266 $sql = "SELECT r.*, rc.capability
3268 JOIN {role_capabilities} rc ON rc.roleid = r.id
3269 WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
3271 $params = array('syscontextid'=>SYSCONTEXTID, 'legacycaps'=>'moodle/legacy:%');
3272 $substart = strlen('moodle/legacy:');
3273 $roles = $DB->get_recordset_sql($sql, $params); // in theory could be multiple legacy flags in one role
3274 foreach ($roles as $role) {
3275 $role->archetype = substr($role->capability, $substart);
3276 unset($role->capability);
3277 if ($role->archetype === 'admin') {
3278 $role->archetype = 'manager';
3279 if ($role->shortname === 'admin') {
3280 $role->shortname = 'manager';
3281 $role->name = get_string('manager', 'role');
3282 $role->description = get_string('managerdescription', 'role');
3285 $DB->update_record('role', $role);
3289 upgrade_main_savepoint($result, 2010033102.02);
3292 if ($result && $oldversion < 2010033102.03) {
3293 // Now pick site admins (===have manager role assigned at the system context)
3294 // and store them in the new $CFG->siteadmins setting as comma separated list
3295 $sql = "SELECT ra.id, ra.userid
3296 FROM {role_assignments} ra
3297 JOIN {role} r ON r.id = ra.roleid
3298 JOIN {user} u ON u.id = ra.userid
3299 WHERE ra.contextid = :syscontext AND r.archetype = 'manager' AND u.deleted = 0
3301 $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
3303 foreach ($ras as $ra) {
3304 $admins[$ra->userid] = $ra->userid;
3305 set_config('siteadmins', implode(',', $admins)); // better to save it repeatedly, we do need at least one admin
3306 $DB->delete_records('role_assignments', array('id'=>$ra->id));
3309 upgrade_main_savepoint($result, 2010033102.03);
3312 if ($result && $oldversion < 2010033102.04) {
3313 // clean up the manager roles
3314 $managers = $DB->get_records('role', array('archetype'=>'manager'));
3315 foreach ($managers as $manager) {
3316 // now sanitize the capabilities and overrides
3317 $DB->delete_records('role_capabilities', array('capability'=>'moodle/site:config', 'roleid'=>$manager->id)); // only site admins may configure servers
3318 // note: doanything and legacy caps are deleted automatically, they get moodle/course:view later at the end of the upgrade
3320 // set usable contexts
3321 $DB->delete_records('role_context_levels', array('roleid'=>$manager->id));
3322 $assignlevels = array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE);
3323 foreach ($assignlevels as $assignlevel) {
3324 $record = (object)array('roleid'=>$manager->id, 'contextlevel'=>$assignlevel);
3325 $DB->insert_record('role_context_levels', $record);
3328 // remove manager role assignments bellow the course context level - admin role was never intended for activities and blocks,
3329 // the problem is that those assignments would not be visible after upgrade and old style admins in activities make no sense anyway
3330 $DB->delete_records_select('role_assignments', "roleid = :manager AND contextid IN (SELECT id FROM {context} WHERE contextlevel > 50)", array('manager'=>$manager->id));
3332 // allow them to assign all roles except default user, guest and frontpage - users get these roles automatically on the fly when needed
3333 $DB->delete_records('role_allow_assign', array('roleid'=>$manager->id));
3334 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype <> 'user' AND archetype <> 'guest' AND archetype <> 'frontpage'");
3335 foreach ($roles as $role) {
3336 $record = (object)array('roleid'=>$manager->id, 'allowassign'=>$role->id);
3337 $DB->insert_record('role_allow_assign', $record);
3340 // allow them to override all roles
3341 $DB->delete_records('role_allow_override', array('roleid'=>$manager->id));
3342 $roles = $DB->get_records_sql("SELECT * FROM {role}");
3343 foreach ($roles as $role) {
3344 $record = (object)array('roleid'=>$manager->id, 'allowoverride'=>$role->id);
3345 $DB->insert_record('role_allow_override', $record);
3348 // allow them to switch to all following roles
3349 $DB->delete_records('role_allow_switch', array('roleid'=>$manager->id));
3350 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype IN ('student', 'teacher', 'editingteacher')");
3351 foreach ($roles as $role) {
3352 $record = (object)array('roleid'=>$manager->id, 'allowswitch'=>$role->id);
3353 $DB->insert_record('role_allow_switch', $record);
3357 upgrade_main_savepoint($result, 2010033102.04);
3360 if ($result && $oldversion < 2010033102.05) {
3361 // remove course:view from all roles that are not used for enrolment, it does NOT belong there because it really means user is enrolled!
3362 $noenrolroles = $DB->get_records_select('role', "archetype IN ('guest', 'user', 'manager', 'coursecreator', 'frontpage')");
3363 foreach ($noenrolroles as $role) {
3364 $DB->delete_records('role_capabilities', array('roleid'=>$role->id, 'capability'=>'moodle/course:participate'));
3366 upgrade_main_savepoint($result, 2010033102.05);
3369 if ($result && $oldversion < 2010033102.06) {
3370 // make sure there is nothing weird in default user role
3371 if (!empty($CFG->defaultuserroleid)) {
3372 if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
3373 if ($role->archetype !== '' and $role->archetype !== 'user') {
3374 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default authenticated user role (defaultuserroleid) value is invalid, setting cleared.');
3375 unset_config('defaultuserroleid');
3378 unset_config('defaultuserroleid');
3381 upgrade_main_savepoint($result, 2010033102.06);
3384 if ($result && $oldversion < 2010033102.07) {
3385 if (!empty($CFG->displayloginfailures) and $CFG->displayloginfailures === 'teacher') {
3386 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Displaying of login failuters to teachers is not supported any more.');
3387 unset_config('displayloginfailures');
3389 upgrade_main_savepoint($result, 2010033102.07);
3392 if ($result && $oldversion < 2010033102.08) {
3393 // make sure there are no problems in default guest role settings
3394 if (!empty($CFG->guestroleid)) {
3395 if ($role = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
3396 if ($role->archetype !== '' and $role->archetype !== 'guest') {
3397 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default guest role (guestroleid) value is invalid, setting cleared.');
3398 unset_config('guestroleid');
3401 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) doeas not exist, setting cleared.');
3402 unset_config('guestroleid');
3405 // remove all roles of the guest account - the only way to change it is to override the guest role, sorry
3406 // the guest account gets all the role assignemnts on the fly whcih works fine in has_capability(),
3407 $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE username = 'guest')");
3409 upgrade_main_savepoint($result, 2010033102.08);
3412 if ($result && $oldversion < 2010033102.09) {
3413 // For MDL-17501. Ensure that any role that has moodle/course:update also has moodle/course:visibility.
3414 // Get the roles with 'moodle/course:update'.
3415 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
3416 $roles = get_roles_with_capability('moodle/course:update', CAP_ALLOW, $systemcontext);
3418 // Give those roles 'moodle/course:visibility'.
3419 foreach ($roles as $role) {
3420 assign_capability('moodle/course:visibility', CAP_ALLOW, $role->id, $systemcontext->id);
3423 // Force all sessions to refresh access data.
3424 mark_context_dirty($systemcontext->path);
3426 // Main savepoint reached
3427 upgrade_main_savepoint($result, 2010033102.09);
3433 //TODO: Before 2.0 release
3434 // 1/ remove the automatic enabling of completion lib if debug enabled ( in 2008121701 block)
3435 // 2/ move 2009061300 block to the top of the file so that we may log upgrade queries
3436 // 3/ force admin password change if salt not set, to be done after planned role changes