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('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
478 $table->add_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null);
479 $table->add_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null);
480 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
481 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
483 /// Adding keys to table files
484 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
485 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
486 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
488 /// Adding indexes to table files
489 $table->add_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid'));
490 $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
491 $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
493 /// Conditionally launch create table for files
494 $dbman->create_table($table);
496 /// Main savepoint reached
497 upgrade_main_savepoint($result, 2008073111);
500 if ($result && $oldversion < 2008073113) {
501 /// move all course, backup and other files to new filepool based storage
502 upgrade_migrate_files_courses();
503 /// Main savepoint reached
504 upgrade_main_savepoint($result, 2008073113);
507 if ($result && $oldversion < 2008073114) {
508 /// move all course, backup and other files to new filepool based storage
509 upgrade_migrate_files_blog();
510 /// Main savepoint reached
511 upgrade_main_savepoint($result, 2008073114);
514 if ($result && $oldversion < 2008080400) {
515 // Add field ssl_jump_url to mnet application, and populate existing default applications
516 $table = new xmldb_table('mnet_application');
517 $field = new xmldb_field('sso_jump_url');
518 if (!$dbman->field_exists($table, $field)) {
519 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
520 $dbman->add_field($table, $field);
521 $result = $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
522 $result = $result && $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
525 /// Main savepoint reached
526 upgrade_main_savepoint($result, 2008080400);
529 if ($result && $oldversion < 2008080500) {
531 /// Define table portfolio_tempdata to be created
532 $table = new xmldb_table('portfolio_tempdata');
534 /// Adding fields to table portfolio_tempdata
535 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
536 $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
538 /// Adding keys to table portfolio_tempdata
539 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
541 /// Conditionally launch create table for portfolio_tempdata
542 if (!$dbman->table_exists($table)) {
543 $dbman->create_table($table);
546 /// Main savepoint reached
547 upgrade_main_savepoint($result, 2008080500);
550 if ($result && $oldversion < 2008080600) {
552 $DB->delete_records('portfolio_tempdata'); // there shouldnt' be any, and it will cause problems with this upgrade.
553 /// Define field expirytime to be added to portfolio_tempdata
554 $table = new xmldb_table('portfolio_tempdata');
555 $field = new xmldb_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'data');
557 /// Conditionally launch add field expirytime
558 if (!$dbman->field_exists($table, $field)) {
559 $dbman->add_field($table, $field);
562 /// Main savepoint reached
563 upgrade_main_savepoint($result, 2008080600);
566 if ($result && $oldversion < 2008081500) {
567 /// Changing the type of all the columns that the question bank uses to store grades to be NUMBER(12, 7).
568 $table = new xmldb_table('question');
569 $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'generalfeedback');
570 $dbman->change_field_type($table, $field);
571 upgrade_main_savepoint($result, 2008081500);
574 if ($result && $oldversion < 2008081501) {
575 $table = new xmldb_table('question');
576 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'defaultgrade');
577 $dbman->change_field_type($table, $field);
578 upgrade_main_savepoint($result, 2008081501);
581 if ($result && $oldversion < 2008081502) {
582 $table = new xmldb_table('question_answers');
583 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'answer');
584 $dbman->change_field_type($table, $field);
585 upgrade_main_savepoint($result, 2008081502);
588 if ($result && $oldversion < 2008081503) {
589 $table = new xmldb_table('question_sessions');
590 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'newgraded');
591 $dbman->change_field_type($table, $field);
592 upgrade_main_savepoint($result, 2008081503);
595 if ($result && $oldversion < 2008081504) {
596 $table = new xmldb_table('question_states');
597 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'event');
598 $dbman->change_field_type($table, $field);
599 upgrade_main_savepoint($result, 2008081504);
602 if ($result && $oldversion < 2008081505) {
603 $table = new xmldb_table('question_states');
604 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'grade');
605 $dbman->change_field_type($table, $field);
606 upgrade_main_savepoint($result, 2008081505);
609 if ($result && $oldversion < 2008081506) {
610 $table = new xmldb_table('question_states');
611 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'raw_grade');
612 $dbman->change_field_type($table, $field);
613 upgrade_main_savepoint($result, 2008081506);
616 if ($result && $oldversion < 2008081600) {
618 /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
619 unset_config('unicodedb');
621 /// Main savepoint reached
622 upgrade_main_savepoint($result, 2008081600);
625 if ($result && $oldversion < 2008081900) {
626 /// Define field userid to be added to portfolio_tempdata
627 $table = new xmldb_table('portfolio_tempdata');
628 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'expirytime');
630 /// Conditionally launch add field userid
631 if (!$dbman->field_exists($table, $field)) {
632 $dbman->add_field($table, $field);
634 $DB->set_field('portfolio_tempdata', 'userid', 0);
635 /// now change it to be notnull
637 /// Changing nullability of field userid on table portfolio_tempdata to not null
638 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'expirytime');
640 /// Launch change of nullability for field userid
641 $dbman->change_field_notnull($table, $field);
643 /// Define key userfk (foreign) to be added to portfolio_tempdata
644 $table = new xmldb_table('portfolio_tempdata');
645 $key = new xmldb_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
647 /// Launch add key userfk
648 $dbman->add_key($table, $key);
650 upgrade_main_savepoint($result, 2008081900);
652 if ($result && $oldversion < 2008082602) {
654 /// Define table repository to be dropped
655 $table = new xmldb_table('repository');
657 /// Conditionally launch drop table for repository
658 if ($dbman->table_exists($table)) {
659 $dbman->drop_table($table);
662 /// Define table repository to be created
663 $table = new xmldb_table('repository');
665 /// Adding fields to table repository
666 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
667 $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
668 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1');
669 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
671 /// Adding keys to table repository
672 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
674 /// Conditionally launch create table for repository
675 if (!$dbman->table_exists($table)) {
676 $dbman->create_table($table);
678 /// Define table repository_instances to be created
679 $table = new xmldb_table('repository_instances');
681 /// Adding fields to table repository_instances
682 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
683 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
684 $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
685 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
686 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
687 $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null);
688 $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
689 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
690 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
692 /// Adding keys to table repository_instances
693 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
695 /// Conditionally launch create table for repository_instances
696 if (!$dbman->table_exists($table)) {
697 $dbman->create_table($table);
700 /// Define table repository_instance_config to be created
701 $table = new xmldb_table('repository_instance_config');
703 /// Adding fields to table repository_instance_config
704 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
705 $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
706 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
707 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
709 /// Adding keys to table repository_instance_config
710 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
712 /// Conditionally launch create table for repository_instance_config
713 if (!$dbman->table_exists($table)) {
714 $dbman->create_table($table);
717 /// Main savepoint reached
718 upgrade_main_savepoint($result, 2008082602);
721 if ($result && $oldversion < 2008082700) {
722 /// Add a new column to the question sessions table to record whether a
723 /// question has been flagged.
725 /// Define field flagged to be added to question_sessions
726 $table = new xmldb_table('question_sessions');
727 $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
729 /// Conditionally launch add field flagged
730 if (!$dbman->field_exists($table, $field)) {
731 $dbman->add_field($table, $field);
734 /// Main savepoint reached
735 upgrade_main_savepoint($result, 2008082700);
738 if ($result && $oldversion < 2008082900) {
740 /// Changing precision of field parent_type on table mnet_rpc to (20)
741 $table = new xmldb_table('mnet_rpc');
742 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
744 /// Launch change of precision for field parent_type
745 $dbman->change_field_precision($table, $field);
747 /// Main savepoint reached
748 upgrade_main_savepoint($result, 2008082900);
751 if ($result && $oldversion < 2008090108) {
752 $repo = new object();
753 $repo->type = 'upload';
755 $repo->sortorder = 1;
756 if (!$DB->record_exists('repository', array('type'=>'upload'))) {
757 $typeid = $DB->insert_record('repository', $repo);
759 $record = $DB->get_record('repository', array('type'=>'upload'));
760 $typeid = $record->id;
762 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
763 $instance = new object();
764 $instance->name = get_string('repositoryname', 'repository_upload');
765 $instance->typeid = $typeid;
766 $instance->userid = 0;
767 $instance->contextid = SITEID;
768 $instance->timecreated = time();
769 $instance->timemodified = time();
770 $result = $result && $DB->insert_record('repository_instances', $instance);
772 $repo->type = 'local';
774 $repo->sortorder = 1;
775 if (!$DB->record_exists('repository', array('type'=>'local'))) {
776 $typeid = $DB->insert_record('repository', $repo);
778 $record = $DB->get_record('repository', array('type'=>'local'));
779 $typeid = $record->id;
781 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
782 $instance = new object();
783 $instance->name = get_string('repositoryname', 'repository_local');
784 $instance->typeid = $typeid;
785 $instance->userid = 0;
786 $instance->contextid = SITEID;
787 $instance->timecreated = time();
788 $instance->timemodified = time();
789 $result = $result && $DB->insert_record('repository_instances', $instance);
792 upgrade_main_savepoint($result, 2008090108);
795 // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
796 if ($result && $oldversion < 2008091000) {
797 foreach (get_object_vars($CFG) as $name => $value) {
798 if (substr($name, strlen($name) - 8) !== '_version') {
801 $pluginname = substr($name, 0, strlen($name) - 8);
802 if (!strpos($pluginname, '_')) {
803 // Skip things like backup_version that don't contain an extra _
806 if ($pluginname == 'enrol_ldap_version') {
807 // Special case - this is something different from a plugin version number.
810 if (!preg_match('/^\d{10}$/', $value)) {
811 // Extra safety check, skip anything that does not look like a Moodle
812 // version number (10 digits).
815 $result = $result && set_config('version', $value, $pluginname);
816 $result = $result && unset_config($name);
818 upgrade_main_savepoint($result, 2008091000);
821 //Add a readonly field to the repository_instances table
822 //in order to support instance created automatically by a repository plugin
823 if ($result && $oldversion < 2008091611) {
825 /// Define field readonly to be added to repository_instances
826 $table = new xmldb_table('repository_instances');
827 $field = new xmldb_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
829 /// Conditionally launch add field readonly
830 if (!$dbman->field_exists($table, $field)) {
831 $dbman->add_field($table, $field);
834 /// Main savepoint reached
835 upgrade_main_savepoint($result, 2008091611);
838 if ($result && $oldversion < 2008092300) {
839 unset_config('editorspelling');
840 unset_config('editordictionary');
841 /// Main savepoint reached
842 upgrade_main_savepoint($result, 2008092300);
845 if ($result && $oldversion < 2008101000) {
847 /// Changing the default of field lang on table user to en_utf8
848 $table = new xmldb_table('user');
849 $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en_utf8', 'country');
851 /// Launch change of default for field lang
852 $dbman->change_field_default($table, $field);
854 /// Main savepoint reached
855 upgrade_main_savepoint($result, 2008101000);
858 if ($result && $oldversion < 2008101300) {
860 if (!get_config(NULL, 'statsruntimedays')) {
861 set_config('statsruntimedays', '31');
864 /// Main savepoint reached
865 upgrade_main_savepoint($result, 2008101300);
868 /// New table for storing which roles can be assigned in which contexts.
869 if ($result && $oldversion < 2008110601) {
871 /// Define table role_context_levels to be created
872 $table = new xmldb_table('role_context_levels');
874 /// Adding fields to table role_context_levels
875 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
876 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
877 $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
879 /// Adding keys to table role_context_levels
880 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
881 $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
882 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
884 /// Conditionally launch create table for role_context_levels
885 if (!$dbman->table_exists($table)) {
886 $dbman->create_table($table);
889 /// Main savepoint reached
890 upgrade_main_savepoint($result, 2008110601);
893 /// Now populate the role_context_levels table with the defaults that match
894 /// moodle_install_roles, and any other combinations that exist in this system.
895 if ($result && $oldversion < 2008110602) {
896 $roleids = $DB->get_records_menu('role', array(), '', 'shortname,id');
898 /// Defaults, should match moodle_install_roles.
899 $rolecontextlevels = array();
900 if (isset($roleids['coursecreator'])) {
901 $rolecontextlevels[$roleids['coursecreator']] = get_default_contextlevels('coursecreator');
903 if (isset($roleids['editingteacher'])) {
904 $rolecontextlevels[$roleids['editingteacher']] = get_default_contextlevels('editingteacher');
906 if (isset($roleids['teacher'])) {
907 $rolecontextlevels[$roleids['teacher']] = get_default_contextlevels('teacher');
909 if (isset($roleids['student'])) {
910 $rolecontextlevels[$roleids['student']] = get_default_contextlevels('student');
912 if (isset($roleids['guest'])) {
913 $rolecontextlevels[$roleids['guest']] = get_default_contextlevels('guest');
915 if (isset($roleids['user'])) {
916 $rolecontextlevels[$roleids['user']] = get_default_contextlevels('user');
919 /// See what other role assignments are in this database, extend the allowed
920 /// lists to allow them too.
921 $existingrolecontextlevels = $DB->get_recordset_sql('SELECT DISTINCT ra.roleid, con.contextlevel FROM
922 {role_assignments} ra JOIN {context} con ON ra.contextid = con.id');
923 foreach ($existingrolecontextlevels as $rcl) {
924 if (!isset($rolecontextlevels[$rcl->roleid])) {
925 $rolecontextlevels[$rcl->roleid] = array($rcl->contextlevel);
926 } else if (!in_array($rcl->contextlevel, $rolecontextlevels[$rcl->roleid])) {
927 $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
931 /// Put the data into the database.
932 foreach ($rolecontextlevels as $roleid => $contextlevels) {
933 set_role_contextlevels($roleid, $contextlevels);
936 /// Main savepoint reached
937 upgrade_main_savepoint($result, 2008110602);
940 /// Drop the deprecated teacher, teachers, student and students columns from the course table.
941 if ($result && $oldversion < 2008111200) {
942 $table = new xmldb_table('course');
944 /// Conditionally launch drop field teacher
945 $field = new xmldb_field('teacher');
946 if ($dbman->field_exists($table, $field)) {
947 $dbman->drop_field($table, $field);
950 /// Conditionally launch drop field teacher
951 $field = new xmldb_field('teachers');
952 if ($dbman->field_exists($table, $field)) {
953 $dbman->drop_field($table, $field);
956 /// Conditionally launch drop field teacher
957 $field = new xmldb_field('student');
958 if ($dbman->field_exists($table, $field)) {
959 $dbman->drop_field($table, $field);
962 /// Conditionally launch drop field teacher
963 $field = new xmldb_field('students');
964 if ($dbman->field_exists($table, $field)) {
965 $dbman->drop_field($table, $field);
968 /// Main savepoint reached
969 upgrade_main_savepoint($result, 2008111200);
972 /// Add a unique index to the role.name column.
973 if ($result && $oldversion < 2008111800) {
975 /// Define index name (unique) to be added to role
976 $table = new xmldb_table('role');
977 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
979 /// Conditionally launch add index name
980 if (!$dbman->index_exists($table, $index)) {
981 $dbman->add_index($table, $index);
984 /// Main savepoint reached
985 upgrade_main_savepoint($result, 2008111800);
988 /// Add a unique index to the role.shortname column.
989 if ($result && $oldversion < 2008111801) {
991 /// Define index shortname (unique) to be added to role
992 $table = new xmldb_table('role');
993 $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
995 /// Conditionally launch add index shortname
996 if (!$dbman->index_exists($table, $index)) {
997 $dbman->add_index($table, $index);
1000 /// Main savepoint reached
1001 upgrade_main_savepoint($result, 2008111801);
1004 if ($result && $oldversion < 2008120700) {
1006 /// Changing precision of field shortname on table course_request to (100)
1007 $table = new xmldb_table('course_request');
1008 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
1010 /// Before changing the field, drop dependent indexes
1011 /// Define index shortname (not unique) to be dropped form course_request
1012 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
1013 /// Conditionally launch drop index shortname
1014 if ($dbman->index_exists($table, $index)) {
1015 $dbman->drop_index($table, $index);
1018 /// Launch change of precision for field shortname
1019 $dbman->change_field_precision($table, $field);
1021 /// After changing the field, recreate dependent indexes
1022 /// Define index shortname (not unique) to be added to course_request
1023 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
1024 /// Conditionally launch add index shortname
1025 if (!$dbman->index_exists($table, $index)) {
1026 $dbman->add_index($table, $index);
1029 /// Main savepoint reached
1030 upgrade_main_savepoint($result, 2008120700);
1033 if ($result && $oldversion < 2008120801) {
1035 /// Changing precision of field shortname on table mnet_enrol_course to (100)
1036 $table = new xmldb_table('mnet_enrol_course');
1037 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
1039 /// Launch change of precision for field shortname
1040 $dbman->change_field_precision($table, $field);
1042 /// Main savepoint reached
1043 upgrade_main_savepoint($result, 2008120801);
1046 if ($result && $oldversion < 2008121701) {
1048 /// Define field availablefrom to be added to course_modules
1049 $table = new xmldb_table('course_modules');
1050 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
1052 /// Conditionally launch add field availablefrom
1053 if (!$dbman->field_exists($table, $field)) {
1054 $dbman->add_field($table, $field);
1057 /// Define field availableuntil to be added to course_modules
1058 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
1060 /// Conditionally launch add field availableuntil
1061 if (!$dbman->field_exists($table, $field)) {
1062 $dbman->add_field($table, $field);
1065 /// Define field showavailability to be added to course_modules
1066 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
1068 /// Conditionally launch add field showavailability
1069 if (!$dbman->field_exists($table, $field)) {
1070 $dbman->add_field($table, $field);
1073 /// Define table course_modules_availability to be created
1074 $table = new xmldb_table('course_modules_availability');
1076 /// Adding fields to table course_modules_availability
1077 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1078 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1079 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1080 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
1081 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1082 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1083 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1085 /// Adding keys to table course_modules_availability
1086 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1087 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
1088 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
1089 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
1091 /// Conditionally launch create table for course_modules_availability
1092 if (!$dbman->table_exists($table)) {
1093 $dbman->create_table($table);
1096 /// Changes to modinfo mean we need to rebuild course cache
1097 require_once($CFG->dirroot . '/course/lib.php');
1098 rebuild_course_cache(0, true);
1100 /// For developer upgrades, turn on the conditional activities and completion
1101 /// features automatically (to gain more testing)
1102 //TODO: remove before 2.0 final!
1103 if (debugging('', DEBUG_DEVELOPER)) {
1104 set_config('enableavailability', 1);
1105 set_config('enablecompletion', 1);
1108 /// Main savepoint reached
1109 upgrade_main_savepoint($result, 2008121701);
1112 if ($result && $oldversion < 2009010500) {
1113 /// clean up config table a bit
1114 unset_config('session_error_counter');
1116 /// Main savepoint reached
1117 upgrade_main_savepoint($result, 2009010500);
1120 if ($result && $oldversion < 2009010600) {
1122 /// Define field originalquestion to be dropped from question_states
1123 $table = new xmldb_table('question_states');
1124 $field = new xmldb_field('originalquestion');
1126 /// Conditionally launch drop field originalquestion
1127 if ($dbman->field_exists($table, $field)) {
1128 $dbman->drop_field($table, $field);
1131 /// Main savepoint reached
1132 upgrade_main_savepoint($result, 2009010600);
1135 if ($result && $oldversion < 2009010601) {
1137 /// Changing precision of field ip on table log to (45)
1138 $table = new xmldb_table('log');
1139 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1141 /// Launch change of precision for field ip
1142 $dbman->change_field_precision($table, $field);
1144 /// Main savepoint reached
1145 upgrade_main_savepoint($result, 2009010601);
1148 if ($result && $oldversion < 2009010602) {
1150 /// Changing precision of field lastip on table user to (45)
1151 $table = new xmldb_table('user');
1152 $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
1154 /// Launch change of precision for field lastip
1155 $dbman->change_field_precision($table, $field);
1157 /// Main savepoint reached
1158 upgrade_main_savepoint($result, 2009010602);
1161 if ($result && $oldversion < 2009010603) {
1163 /// Changing precision of field ip_address on table mnet_host to (45)
1164 $table = new xmldb_table('mnet_host');
1165 $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
1167 /// Launch change of precision for field ip_address
1168 $dbman->change_field_precision($table, $field);
1170 /// Main savepoint reached
1171 upgrade_main_savepoint($result, 2009010603);
1174 if ($result && $oldversion < 2009010604) {
1176 /// Changing precision of field ip on table mnet_log to (45)
1177 $table = new xmldb_table('mnet_log');
1178 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1180 /// Launch change of precision for field ip
1181 $dbman->change_field_precision($table, $field);
1183 /// Main savepoint reached
1184 upgrade_main_savepoint($result, 2009010604);
1187 if ($result && $oldversion < 2009010800) {
1188 /// Update the notifyloginfailures setting.
1189 if ($CFG->notifyloginfailures == 'mainadmin') {
1190 set_config('notifyloginfailures', get_admin()->username);
1191 } else if ($CFG->notifyloginfailures == 'alladmins') {
1192 set_config('notifyloginfailures', '$@ALL@$');
1194 set_config('notifyloginfailures', '');
1197 /// Main savepoint reached
1198 upgrade_main_savepoint($result, 2009010800);
1201 if ($result && $oldversion < 2009011000) {
1203 /// Changing nullability of field configdata on table block_instance to null
1204 $table = new xmldb_table('block_instance');
1205 $field = new xmldb_field('configdata');
1206 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
1208 /// Launch change of nullability for field configdata
1209 $dbman->change_field_notnull($table, $field);
1211 /// Main savepoint reached
1212 upgrade_main_savepoint($result, 2009011000);
1215 if ($result && $oldversion < 2009011100) {
1216 /// Remove unused settings
1217 unset_config('zip');
1218 unset_config('unzip');
1219 unset_config('adminblocks_initialised');
1221 /// Main savepoint reached
1222 upgrade_main_savepoint($result, 2009011100);
1225 if ($result && $oldversion < 2009011101) {
1226 /// Migrate backup settings to core plugin config table
1227 $configs = $DB->get_records('backup_config');
1228 foreach ($configs as $config) {
1229 set_config($config->name, $config->value, 'backup');
1232 /// Define table to be dropped
1233 $table = new xmldb_table('backup_config');
1235 /// Launch drop table for old backup config
1236 $dbman->drop_table($table);
1238 /// Main savepoint reached
1239 upgrade_main_savepoint($result, 2009011101);
1242 if ($result && $oldversion < 2009011303) {
1244 /// Define table config_log to be created
1245 $table = new xmldb_table('config_log');
1247 /// Adding fields to table config_log
1248 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1249 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1250 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1251 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1252 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1253 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1254 $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1256 /// Adding keys to table config_log
1257 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1258 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1260 /// Adding indexes to table config_log
1261 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1263 /// Launch create table for config_log
1264 $dbman->create_table($table);
1266 /// Main savepoint reached
1267 upgrade_main_savepoint($result, 2009011303);
1270 if ($result && $oldversion < 2009011900) {
1272 /// Define table sessions2 to be dropped
1273 $table = new xmldb_table('sessions2');
1275 /// Conditionally launch drop table for sessions
1276 if ($dbman->table_exists($table)) {
1277 $dbman->drop_table($table);
1280 /// Define table sessions to be dropped
1281 $table = new xmldb_table('sessions');
1283 /// Conditionally launch drop table for sessions
1284 if ($dbman->table_exists($table)) {
1285 $dbman->drop_table($table);
1288 /// Define table sessions to be created
1289 $table = new xmldb_table('sessions');
1291 /// Adding fields to table sessions
1292 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1293 $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1294 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
1295 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1296 $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
1297 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1298 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1299 $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1300 $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1302 /// Adding keys to table sessions
1303 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1304 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1306 /// Adding indexes to table sessions
1307 $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1308 $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1309 $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1310 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1312 /// Launch create table for sessions
1313 $dbman->create_table($table);
1315 /// Main savepoint reached
1316 upgrade_main_savepoint($result, 2009011900);
1319 if ($result && $oldversion < 2009012901) {
1320 // NOTE: this table may already exist, see beginning of this file ;-)
1322 /// Define table upgrade_log to be created
1323 $table = new xmldb_table('upgrade_log');
1325 /// Adding fields to table upgrade_log
1326 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1327 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1328 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1329 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1330 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1331 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1332 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1333 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1334 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1336 /// Adding keys to table upgrade_log
1337 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1338 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1340 /// Adding indexes to table upgrade_log
1341 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1342 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
1344 /// Conditionally launch create table for upgrade_log
1345 if (!$dbman->table_exists($table)) {
1346 $dbman->create_table($table);
1349 /// Main savepoint reached
1350 upgrade_main_savepoint($result, 2009012901);
1353 if ($result && $oldversion < 2009021800) {
1354 // Converting format of grade conditions, if any exist, to percentages.
1356 UPDATE {course_modules_availability} SET grademin=(
1357 SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
1358 /(gi.grademax-gi.grademin)
1359 FROM {grade_items} gi
1360 WHERE gi.id={course_modules_availability}.gradeitemid)
1361 WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1363 UPDATE {course_modules_availability} SET grademax=(
1364 SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
1365 /(gi.grademax-gi.grademin)
1366 FROM {grade_items} gi
1367 WHERE gi.id={course_modules_availability}.gradeitemid)
1368 WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1370 /// Main savepoint reached
1371 upgrade_main_savepoint($result, 2009021800);
1373 if ($result && $oldversion < 2009021801) {
1374 /// Define field backuptype to be added to backup_log
1375 $table = new xmldb_table('backup_log');
1376 $field = new xmldb_field('backuptype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'info');
1377 /// Conditionally Launch add field backuptype and set all old records as 'scheduledbackup' records.
1378 if (!$dbman->field_exists($table, $field)) {
1379 $dbman->add_field($table, $field);
1380 $DB->execute("UPDATE {backup_log} SET backuptype='scheduledbackup'");
1383 /// Main savepoint reached
1384 upgrade_main_savepoint($result, 2009021801);
1386 /// Add default sort order for question types.
1387 if ($result && $oldversion < 2009030300) {
1388 set_config('multichoice_sortorder', 1, 'question');
1389 set_config('truefalse_sortorder', 2, 'question');
1390 set_config('shortanswer_sortorder', 3, 'question');
1391 set_config('numerical_sortorder', 4, 'question');
1392 set_config('calculated_sortorder', 5, 'question');
1393 set_config('essay_sortorder', 6, 'question');
1394 set_config('match_sortorder', 7, 'question');
1395 set_config('randomsamatch_sortorder', 8, 'question');
1396 set_config('multianswer_sortorder', 9, 'question');
1397 set_config('description_sortorder', 10, 'question');
1398 set_config('random_sortorder', 11, 'question');
1399 set_config('missingtype_sortorder', 12, 'question');
1401 upgrade_main_savepoint($result, 2009030300);
1403 if ($result && $oldversion < 2009030501) {
1404 /// setup default repository plugins
1405 require_once($CFG->dirroot . '/repository/lib.php');
1406 repository_setup_default_plugins();
1407 /// Main savepoint reached
1408 upgrade_main_savepoint($result, 2009030501);
1411 /// MDL-18132 replace the use a new Role allow switch settings page, instead of
1412 /// $CFG->allowuserswitchrolestheycantassign
1413 if ($result && $oldversion < 2009032000) {
1414 /// First create the new table.
1415 $table = new xmldb_table('role_allow_switch');
1417 /// Adding fields to table role_allow_switch
1418 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1419 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1420 $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1422 /// Adding keys to table role_allow_switch
1423 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1424 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
1425 $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
1427 /// Adding indexes to table role_allow_switch
1428 $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1430 /// Conditionally launch create table for role_allow_switch
1431 if (!$dbman->table_exists($table)) {
1432 $dbman->create_table($table);
1435 /// Main savepoint reached
1436 upgrade_main_savepoint($result, 2009032000);
1439 if ($result && $oldversion < 2009032001) {
1440 /// Copy from role_allow_assign into the new table.
1441 $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1442 SELECT roleid, allowassign FROM {role_allow_assign}');
1444 /// Unset the config variable used in 1.9.
1445 unset_config('allowuserswitchrolestheycantassign');
1447 /// Main savepoint reached
1448 upgrade_main_savepoint($result, 2009032001);
1451 if ($result && $oldversion < 2009033100) {
1452 require_once("$CFG->dirroot/filter/tex/lib.php");
1453 filter_tex_updatedcallback(null);
1454 /// Main savepoint reached
1455 upgrade_main_savepoint($result, 2009033100);
1458 if ($result && $oldversion < 2009040300) {
1460 /// Define table filter_active to be created
1461 $table = new xmldb_table('filter_active');
1463 /// Adding fields to table filter_active
1464 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1465 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1466 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1467 $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1468 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1470 /// Adding keys to table filter_active
1471 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1472 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1474 /// Adding indexes to table filter_active
1475 $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1477 /// Conditionally launch create table for filter_active
1478 if (!$dbman->table_exists($table)) {
1479 $dbman->create_table($table);
1482 /// Main savepoint reached
1483 upgrade_main_savepoint($result, 2009040300);
1486 if ($result && $oldversion < 2009040301) {
1488 /// Define table filter_config to be created
1489 $table = new xmldb_table('filter_config');
1491 /// Adding fields to table filter_config
1492 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1493 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1494 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1495 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1496 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1498 /// Adding keys to table filter_config
1499 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1500 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1502 /// Adding indexes to table filter_config
1503 $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1505 /// Conditionally launch create table for filter_config
1506 if (!$dbman->table_exists($table)) {
1507 $dbman->create_table($table);
1510 /// Main savepoint reached
1511 upgrade_main_savepoint($result, 2009040301);
1514 if ($result && $oldversion < 2009040302) {
1515 /// Transfer current settings from $CFG->textfilters
1516 $disabledfilters = filter_get_all_installed();
1517 if (empty($CFG->textfilters)) {
1518 $activefilters = array();
1520 $activefilters = explode(',', $CFG->textfilters);
1522 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1524 foreach ($activefilters as $filter) {
1525 filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1527 unset($disabledfilters[$filter]);
1529 foreach ($disabledfilters as $filter => $notused) {
1530 filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1534 /// Main savepoint reached
1535 upgrade_main_savepoint($result, 2009040302);
1538 if ($result && $oldversion < 2009040600) {
1539 /// Ensure that $CFG->stringfilters is set.
1540 if (empty($CFG->stringfilters)) {
1541 if (!empty($CFG->filterall)) {
1542 set_config('stringfilters', $CFG->textfilters);
1544 set_config('stringfilters', '');
1548 set_config('filterall', !empty($CFG->stringfilters));
1549 unset_config('textfilters');
1551 /// Main savepoint reached
1552 upgrade_main_savepoint($result, 2009040600);
1555 if ($result && $oldversion < 2009041700) {
1556 /// To ensure the UI remains consistent with no behaviour change, any
1557 /// 'until' date in an activity condition should have 1 second subtracted
1558 /// (to go from 0:00 on the following day to 23:59 on the previous one).
1559 $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
1560 require_once($CFG->dirroot . '/course/lib.php');
1561 rebuild_course_cache(0, true);
1563 /// Main savepoint reached
1564 upgrade_main_savepoint($result, 2009041700);
1567 if ($result && $oldversion < 2009042600) {
1568 /// Deleting orphaned messages from deleted users.
1569 require_once($CFG->dirroot.'/message/lib.php');
1570 /// Detect deleted users with messages sent(useridfrom) and not read
1571 if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
1573 JOIN {message} m ON m.useridfrom = u.id
1574 WHERE u.deleted = ?', array(1))) {
1575 foreach ($deletedusers as $deleteduser) {
1576 message_move_userfrom_unread2read($deleteduser->id); // move messages
1579 /// Main savepoint reached
1580 upgrade_main_savepoint($result, 2009042600);
1583 /// Dropping all enums/check contraints from core. MDL-18577
1584 if ($result && $oldversion < 2009042700) {
1586 /// Changing list of values (enum) of field stattype on table stats_daily to none
1587 $table = new xmldb_table('stats_daily');
1588 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1590 /// Launch change of list of values for field stattype
1591 $dbman->drop_enum_from_field($table, $field);
1593 /// Changing list of values (enum) of field stattype on table stats_weekly to none
1594 $table = new xmldb_table('stats_weekly');
1595 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1597 /// Launch change of list of values for field stattype
1598 $dbman->drop_enum_from_field($table, $field);
1600 /// Changing list of values (enum) of field stattype on table stats_monthly to none
1601 $table = new xmldb_table('stats_monthly');
1602 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1604 /// Launch change of list of values for field stattype
1605 $dbman->drop_enum_from_field($table, $field);
1607 /// Changing list of values (enum) of field publishstate on table post to none
1608 $table = new xmldb_table('post');
1609 $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
1611 /// Launch change of list of values for field publishstate
1612 $dbman->drop_enum_from_field($table, $field);
1614 /// Main savepoint reached
1615 upgrade_main_savepoint($result, 2009042700);
1618 if ($result && $oldversion < 2009043000) {
1619 unset_config('grade_report_showgroups');
1620 upgrade_main_savepoint($result, 2009043000);
1623 if ($result && $oldversion < 2009050600) {
1624 /// Site front page blocks need to be moved due to page name change.
1625 $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
1627 /// Main savepoint reached
1628 upgrade_main_savepoint($result, 2009050600);
1631 if ($result && $oldversion < 2009050601) {
1633 /// Define table block_instance to be renamed to block_instances
1634 $table = new xmldb_table('block_instance');
1636 /// Launch rename table for block_instance
1637 $dbman->rename_table($table, 'block_instances');
1639 /// Main savepoint reached
1640 upgrade_main_savepoint($result, 2009050601);
1643 if ($result && $oldversion < 2009050602) {
1645 /// Define table block_instance to be renamed to block_instance_old
1646 $table = new xmldb_table('block_pinned');
1648 /// Launch rename table for block_instance
1649 $dbman->rename_table($table, 'block_pinned_old');
1651 /// Main savepoint reached
1652 upgrade_main_savepoint($result, 2009050602);
1655 if ($result && $oldversion < 2009050603) {
1657 /// Define table block_instance_old to be created
1658 $table = new xmldb_table('block_instance_old');
1660 /// Adding fields to table block_instance_old
1661 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1662 $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1663 $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1664 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1665 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
1666 $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
1667 $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
1668 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1669 $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1671 /// Adding keys to table block_instance_old
1672 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1673 $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
1675 /// Adding indexes to table block_instance_old
1676 $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1677 $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
1679 /// Conditionally launch create table for block_instance_old
1680 if (!$dbman->table_exists($table)) {
1681 $dbman->create_table($table);
1684 /// Main savepoint reached
1685 upgrade_main_savepoint($result, 2009050603);
1688 if ($result && $oldversion < 2009050604) {
1689 /// Copy current blocks data from block_instances to block_instance_old
1690 $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
1691 SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
1693 upgrade_main_savepoint($result, 2009050604);
1696 if ($result && $oldversion < 2009050605) {
1698 /// Define field multiple to be dropped from block
1699 $table = new xmldb_table('block');
1700 $field = new xmldb_field('multiple');
1702 /// Conditionally launch drop field multiple
1703 if ($dbman->field_exists($table, $field)) {
1704 $dbman->drop_field($table, $field);
1707 /// Main savepoint reached
1708 upgrade_main_savepoint($result, 2009050605);
1711 if ($result && $oldversion < 2009050606) {
1712 $table = new xmldb_table('block_instances');
1714 /// Rename field weight on table block_instances to defaultweight
1715 $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'position');
1716 $dbman->rename_field($table, $field, 'defaultweight');
1718 /// Rename field position on table block_instances to defaultregion
1719 $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
1720 $dbman->rename_field($table, $field, 'defaultregion');
1722 /// Main savepoint reached
1723 upgrade_main_savepoint($result, 2009050606);
1726 if ($result && $oldversion < 2009050607) {
1727 /// Changing precision of field defaultregion on table block_instances to (16)
1728 $table = new xmldb_table('block_instances');
1729 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
1731 /// Launch change of precision for field defaultregion
1732 $dbman->change_field_precision($table, $field);
1734 /// Main savepoint reached
1735 upgrade_main_savepoint($result, 2009050607);
1738 if ($result && $oldversion < 2009050608) {
1739 /// Change regions to the new notation
1740 $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1741 $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1742 $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1743 // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1744 // flex page course format. Hopefully this new value is an adequate alternative.
1746 /// Main savepoint reached
1747 upgrade_main_savepoint($result, 2009050608);
1750 if ($result && $oldversion < 2009050609) {
1752 /// Define key blockname (unique) to be added to block
1753 $table = new xmldb_table('block');
1754 $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1756 /// Launch add key blockname
1757 $dbman->add_key($table, $key);
1759 /// Main savepoint reached
1760 upgrade_main_savepoint($result, 2009050609);
1763 if ($result && $oldversion < 2009050610) {
1764 $table = new xmldb_table('block_instances');
1766 /// Define field blockname to be added to block_instances
1767 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
1768 if (!$dbman->field_exists($table, $field)) {
1769 $dbman->add_field($table, $field);
1772 /// Define field contextid to be added to block_instances
1773 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
1774 if (!$dbman->field_exists($table, $field)) {
1775 $dbman->add_field($table, $field);
1778 /// Define field showinsubcontexts to be added to block_instances
1779 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
1780 if (!$dbman->field_exists($table, $field)) {
1781 $dbman->add_field($table, $field);
1784 /// Define field subpagepattern to be added to block_instances
1785 $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
1786 if (!$dbman->field_exists($table, $field)) {
1787 $dbman->add_field($table, $field);
1790 /// Main savepoint reached
1791 upgrade_main_savepoint($result, 2009050610);
1794 if ($result && $oldversion < 2009050611) {
1795 $table = new xmldb_table('block_instances');
1797 /// Fill in blockname from blockid
1798 $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1800 /// Set showinsubcontexts = 0 for all rows.
1801 $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1803 /// Main savepoint reached
1804 upgrade_main_savepoint($result, 2009050611);
1807 if ($result && $oldversion < 2009050612) {
1809 /// Rename field pagetype on table block_instances to pagetypepattern
1810 $table = new xmldb_table('block_instances');
1811 $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
1813 /// Launch rename field pagetype
1814 $dbman->rename_field($table, $field, 'pagetypepattern');
1816 /// Main savepoint reached
1817 upgrade_main_savepoint($result, 2009050612);
1820 if ($result && $oldversion < 2009050613) {
1821 /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1824 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
1825 $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1826 pagetypepattern = 'site-index',
1827 subpagepattern = NULL
1828 WHERE pagetypepattern = 'site-index'");
1831 $DB->execute("UPDATE {block_instances} SET
1835 JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1836 WHERE {course}.id = pageid
1838 pagetypepattern = 'course-view-*',
1839 subpagepattern = NULL
1840 WHERE pagetypepattern = 'course-view'");
1843 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1844 $DB->execute("UPDATE {block_instances} SET
1845 contextid = " . $syscontext->id . ",
1846 pagetypepattern = 'admin-*',
1847 subpagepattern = NULL
1848 WHERE pagetypepattern = 'admin'");
1851 $DB->execute("UPDATE {block_instances} SET
1855 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1856 WHERE {user}.id = pageid
1858 pagetypepattern = 'my-index',
1859 subpagepattern = NULL
1860 WHERE pagetypepattern = 'my-index'");
1863 $DB->execute("UPDATE {block_instances} SET
1864 contextid = " . $syscontext->id . ",
1865 pagetypepattern = 'tag-index',
1866 subpagepattern = pageid
1867 WHERE pagetypepattern = 'tag-index'");
1870 $DB->execute("UPDATE {block_instances} SET
1874 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1875 WHERE {user}.id = pageid
1877 pagetypepattern = 'blog-index',
1878 subpagepattern = NULL
1879 WHERE pagetypepattern = 'blog-view'");
1882 $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1883 foreach ($moduleswithblocks as $modname) {
1884 if (!$dbman->table_exists($modname)) {
1887 $DB->execute("UPDATE {block_instances} SET
1891 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1892 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1893 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1894 WHERE {{$modname}}.id = pageid
1896 pagetypepattern = 'blog-index',
1897 subpagepattern = NULL
1898 WHERE pagetypepattern = 'blog-view'");
1901 /// Main savepoint reached
1902 upgrade_main_savepoint($result, 2009050613);
1905 if ($result && $oldversion < 2009050614) {
1906 /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
1907 $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
1909 /// Main savepoint reached
1910 upgrade_main_savepoint($result, 2009050614);
1913 if ($result && $oldversion < 2009050615) {
1914 $table = new xmldb_table('block_instances');
1916 /// Changing nullability of field blockname on table block_instances to not null
1917 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
1918 $dbman->change_field_notnull($table, $field);
1920 /// Changing nullability of field contextid on table block_instances to not null
1921 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
1922 $dbman->change_field_notnull($table, $field);
1924 /// Changing nullability of field showinsubcontexts on table block_instances to not null
1925 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
1926 $dbman->change_field_notnull($table, $field);
1928 /// Main savepoint reached
1929 upgrade_main_savepoint($result, 2009050615);
1932 if ($result && $oldversion < 2009050616) {
1933 /// Add exiting sticky blocks.
1934 $blocks = $DB->get_records('block');
1935 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1936 $newregions = array(
1939 'c' => 'course-view-top',
1941 $stickyblocks = $DB->get_recordset('block_pinned_old');
1942 foreach ($stickyblocks as $stickyblock) {
1943 $newblock = new object();
1944 $newblock->blockname = $blocks[$stickyblock->blockid]->name;
1945 $newblock->contextid = $syscontext->id;
1946 $newblock->showinsubcontexts = 1;
1947 switch ($stickyblock->pagetype) {
1949 $newblock->pagetypepattern = 'course-view-*';
1952 $newblock->pagetypepattern = $stickyblock->pagetype;
1954 $newblock->defaultregion = $newregions[$stickyblock->position];
1955 $newblock->defaultweight = $stickyblock->weight;
1956 $newblock->configdata = $stickyblock->configdata;
1957 $newblock->visible = 1;
1958 $DB->insert_record('block_instances', $newblock);
1961 /// Main savepoint reached
1962 upgrade_main_savepoint($result, 2009050616);
1965 if ($result && $oldversion < 2009050617) {
1967 /// Define table block_positions to be created
1968 $table = new xmldb_table('block_positions');
1970 /// Adding fields to table block_positions
1971 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1972 $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1973 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1974 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1975 $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1976 $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1');
1977 $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1978 $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1980 /// Adding keys to table block_positions
1981 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1982 $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
1983 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1985 /// Adding indexes to table block_positions
1986 $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
1988 /// Conditionally launch create table for block_positions
1989 if (!$dbman->table_exists($table)) {
1990 $dbman->create_table($table);
1993 /// Main savepoint reached
1994 upgrade_main_savepoint($result, 2009050617);
1997 if ($result && $oldversion < 2009050618) {
1998 /// And block instances with visible = 0, copy that information to block_positions
1999 $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
2000 SELECT id, contextid,
2001 CASE WHEN pagetypepattern = 'course-view-*' THEN
2002 (SELECT " . $DB->sql_concat("'course-view-'", 'format') . "
2004 JOIN {context} ON {course}.id = {context}.instanceid
2005 WHERE {context}.id = contextid)
2006 ELSE pagetypepattern END,
2007 CASE WHEN subpagepattern IS NULL THEN ''
2008 ELSE subpagepattern END,
2009 0, defaultregion, defaultweight
2010 FROM {block_instances} WHERE visible = 0 AND pagetypepattern <> 'admin-*'");
2012 /// Main savepoint reached
2013 upgrade_main_savepoint($result, 2009050618);
2016 if ($result && $oldversion < 2009050619) {
2017 $table = new xmldb_table('block_instances');
2019 /// Define field blockid to be dropped from block_instances
2020 $field = new xmldb_field('blockid');
2021 if ($dbman->field_exists($table, $field)) {
2022 /// Before dropping the field, drop dependent indexes
2023 $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
2024 if ($dbman->index_exists($table, $index)) {
2025 /// Launch drop index blockid
2026 $dbman->drop_index($table, $index);
2028 $dbman->drop_field($table, $field);
2031 /// Define field pageid to be dropped from block_instances
2032 $field = new xmldb_field('pageid');
2033 if ($dbman->field_exists($table, $field)) {
2034 /// Before dropping the field, drop dependent indexes
2035 $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
2036 if ($dbman->index_exists($table, $index)) {
2037 /// Launch drop index pageid
2038 $dbman->drop_index($table, $index);
2040 $dbman->drop_field($table, $field);
2043 /// Define field visible to be dropped from block_instances
2044 $field = new xmldb_field('visible');
2045 if ($dbman->field_exists($table, $field)) {
2046 $dbman->drop_field($table, $field);
2049 /// Main savepoint reached
2050 upgrade_main_savepoint($result, 2009050619);
2053 if ($result && $oldversion < 2009051200) {
2054 /// Let's check the status of mandatory mnet_host records, fixing them
2055 /// and moving "orphan" users to default localhost record. MDL-16879
2056 echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
2057 upgrade_fix_incorrect_mnethostids();
2059 /// Main savepoint reached
2060 upgrade_main_savepoint($result, 2009051200);
2064 if ($result && $oldversion < 2009051700) {
2065 /// migrate editor settings
2066 if (empty($CFG->htmleditor)) {
2067 set_config('texteditors', 'textarea');
2069 set_config('texteditors', 'tinymce,textarea');
2072 unset_config('htmleditor');
2073 unset_config('defaulthtmleditor');
2075 /// Main savepoint reached
2076 upgrade_main_savepoint($result, 2009051700);
2079 if ($result && $oldversion < 2009060200) {
2080 /// Define table files_cleanup to be dropped - not needed
2081 $table = new xmldb_table('files_cleanup');
2083 /// Conditionally launch drop table for files_cleanup
2084 if ($dbman->table_exists($table)) {
2085 $dbman->drop_table($table);
2088 /// Main savepoint reached
2089 upgrade_main_savepoint($result, 2009060200);
2092 if ($result && $oldversion < 2009061300) {
2093 //TODO: copy this to the very beginning of this upgrade script so that we may log upgrade queries
2095 /// Define table log_queries to be created
2096 $table = new xmldb_table('log_queries');
2098 /// Adding fields to table log_queries
2099 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2100 $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2101 $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
2102 $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
2103 $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2104 $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2105 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2106 $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
2107 $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2109 /// Adding keys to table log_queries
2110 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2112 /// Conditionally launch create table for log_queries
2113 if (!$dbman->table_exists($table)) {
2114 $dbman->create_table($table);
2117 /// Main savepoint reached
2118 upgrade_main_savepoint($result, 2009061300);
2121 /// Repeat 2009050607 upgrade step, which Petr commented out becuase of XMLDB
2122 /// stupidity, so lots of peopel will have missed.
2123 if ($result && $oldversion < 2009061600) {
2124 /// Changing precision of field defaultregion on table block_instances to (16)
2125 $table = new xmldb_table('block_instances');
2126 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
2128 /// Launch change of precision for field defaultregion
2129 $dbman->change_field_precision($table, $field);
2131 /// Main savepoint reached
2132 upgrade_main_savepoint($result, 2009061600);
2135 if ($result && $oldversion < 2009061702) {
2136 // standardizing plugin names
2137 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
2138 foreach ($configs as $config) {
2139 $result = $result && unset_config($config->name, $config->plugin); /// unset old config
2140 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
2141 $result = $result && set_config($config->name, $config->value, $config->plugin); /// set new config
2145 upgrade_main_savepoint($result, 2009061702);
2148 if ($result && $oldversion < 2009061703) {
2149 // standardizing plugin names
2150 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
2151 foreach ($configs as $config) {
2152 $result = $result && unset_config($config->name, $config->plugin); /// unset old config
2153 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
2154 $result = $result && set_config($config->name, $config->value, $config->plugin); /// set new config
2158 upgrade_main_savepoint($result, 2009061703);
2161 if ($result && $oldversion < 2009061704) {
2162 // change component string in capability records to new "_" format
2163 if ($caps = $DB->get_records('capabilities')) {
2164 foreach ($caps as $cap) {
2165 $cap->component = str_replace('/', '_', $cap->component);
2166 $DB->update_record('capabilities', $cap);
2170 upgrade_main_savepoint($result, 2009061704);
2173 if ($result && $oldversion < 2009061705) {
2174 // change component string in events_handlers records to new "_" format
2175 if ($handlers = $DB->get_records('events_handlers')) {
2176 foreach ($handlers as $handler) {
2177 $handler->handlermodule = str_replace('/', '_', $handler->handlermodule);
2178 $DB->update_record('events_handlers', $handler);
2182 upgrade_main_savepoint($result, 2009061705);
2185 if ($result && $oldversion < 2009061706) {
2186 // change component string in message_providers records to new "_" format
2187 if ($mps = $DB->get_records('message_providers')) {
2188 foreach ($mps as $mp) {
2189 $mp->component = str_replace('/', '_', $mp->component);
2190 $DB->update_record('message_providers', $cap);
2194 upgrade_main_savepoint($result, 2009061706);
2197 if ($result && $oldversion < 2009063000) {
2198 // upgrade format of _with_advanced settings - quiz only
2199 // note: this can be removed later, not needed for upgrades from 1.9.x
2200 if ($quiz = get_config('quiz')) {
2201 foreach ($quiz as $name=>$value) {
2202 if (strpos($name, 'fix_') !== 0) {
2205 $newname = substr($name,4).'_adv';
2206 set_config($newname, $value, 'quiz');
2207 unset_config($name, 'quiz');
2210 upgrade_main_savepoint($result, 2009063000);
2213 if ($result && $oldversion < 2009071000) {
2215 /// Rename field contextid on table block_instances to parentcontextid
2216 $table = new xmldb_table('block_instances');
2217 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
2219 /// Launch rename field parentcontextid
2220 $dbman->rename_field($table, $field, 'parentcontextid');
2222 /// Main savepoint reached
2223 upgrade_main_savepoint($result, 2009071000);
2226 if ($result && $oldversion < 2009071300) {
2228 /// Create contexts for every block. In the past, only non-sticky course block had contexts.
2229 /// This is a copy of the code in create_contexts.
2230 $sql = "INSERT INTO {context} (contextlevel, instanceid)
2231 SELECT " . CONTEXT_BLOCK . ", bi.id
2232 FROM {block_instances} bi
2233 WHERE NOT EXISTS (SELECT 'x'
2235 WHERE bi.id = ctx.instanceid AND ctx.contextlevel=" . CONTEXT_BLOCK . ")";
2238 /// TODO MDL-19776 We should not really use API funcitons in upgrade.
2239 /// If MDL-19776 is done, we can remove this whole upgrade block.
2240 build_context_path();
2242 /// Main savepoint reached
2243 upgrade_main_savepoint($result, 2009071300);
2246 if ($result && $oldversion < 2009071600) {
2248 /// Define field summaryformat to be added to post
2249 $table = new xmldb_table('post');
2250 $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
2252 /// Conditionally launch add field summaryformat
2253 if (!$dbman->field_exists($table, $field)) {
2254 $dbman->add_field($table, $field);
2257 /// Main savepoint reached
2258 upgrade_main_savepoint($result, 2009071600);
2261 if ($result && $oldversion < 2009072400) {
2263 /// Define table comments to be created
2264 $table = new xmldb_table('comments');
2266 /// Adding fields to table comments
2267 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2268 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2269 $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2270 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2271 $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2272 $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2273 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2274 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2276 /// Adding keys to table comments
2277 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2279 /// Conditionally launch create table for comments
2280 if (!$dbman->table_exists($table)) {
2281 $dbman->create_table($table);
2284 /// Main savepoint reached
2285 upgrade_main_savepoint($result, 2009072400);
2289 * This upgrade is to set up the new navigation blocks that have been developed
2290 * as part of Moodle 2.0
2291 * Now I [Sam Hemelryk] hit a conundrum while exploring how to go about this
2292 * as not only do we want to install the new blocks but we also want to set up
2293 * default instances of them, and at the same time remove instances of the blocks
2294 * that were/will-be outmoded by the two new navigation blocks.
2295 * After talking it through with Tim Hunt {@link http://moodle.org/mod/cvsadmin/view.php?conversationid=3112}
2296 * we decided that the best way to go about this was to put the bulk of the
2297 * upgrade operation into core upgrade `here` but to let the plugins block
2298 * still install the blocks.
2299 * This leaves one hairy end in that we will create block_instances within the
2300 * DB before the blocks themselves are created within the DB
2302 if ($result && $oldversion < 2009082800) {
2304 echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2306 // Get the system context so we can set the block instances to it
2307 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2309 // An array to contain the new block instances we will create
2310 $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
2311 // The new global navigation block instance as a stdClass
2312 $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
2313 $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
2314 $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutly everywhere
2315 $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
2316 $newblockinstances['globalnavigation']->subpagetypepattern = null;
2317 $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
2318 $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
2319 $newblockinstances['globalnavigation']->configdata = '';
2320 // The new settings navigation block instance as a stdClass
2321 $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
2322 $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
2323 $newblockinstances['settingsnavigation']->showinsubcontexts = true;
2324 $newblockinstances['settingsnavigation']->pagetypepattern = '*';
2325 $newblockinstances['settingsnavigation']->subpagetypepattern = null;
2326 $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
2327 $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
2328 $newblockinstances['settingsnavigation']->configdata = '';
2330 // Blocks that are outmoded and for whom the bells will toll... by which I
2331 // mean we will delete all instances of
2332 $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
2333 $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
2334 unset($outmodedblocks);
2335 // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
2336 // cut down the number of delete queries we will need to run
2337 $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
2339 $contextids = array();
2340 $instanceids = array();
2341 // Iterate through all block instances
2342 foreach ($allblockinstances as $blockinstance) {
2343 if (!in_array($blockinstance->parentcontextid, $contextids)) {
2344 $contextids[] = $blockinstance->parentcontextid;
2346 // If we have over 1000 contexts clean them up and reset the array
2347 // this ensures we don't hit any nasty memory limits or such
2348 if (count($contextids) > 1000) {
2349 $result = $result && upgrade_cleanup_unwanted_block_contexts($contextids);
2350 $contextids = array();
2353 if (!in_array($blockinstance->id, $instanceids)) {
2354 $instanceids[] = $blockinstance->id;
2355 // If we have more than 1000 block instances now remove all block positions
2356 // and empty the array
2357 if (count($contextids) > 1000) {
2358 $instanceidstring = join(',',$instanceids);
2359 $result = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2360 $instanceids = array();
2365 $result = $result && upgrade_cleanup_unwanted_block_contexts($contextids);
2367 $instanceidstring = join(',',$instanceids);
2368 $outcome1 = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2370 unset($allblockinstances);
2372 unset($instanceids);
2373 unset($instanceidstring);
2375 // Now remove the actual block instance
2376 $result = $result && $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
2377 unset($outmodedblocksstring);
2379 // Insert the new block instances. Remember they have not been installed yet
2380 // however this should not be a problem
2381 foreach ($newblockinstances as $blockinstance) {
2382 $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
2383 // Ensure the block context is created.
2384 get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
2386 unset($newblockinstances);
2388 upgrade_main_savepoint($result, 2009082800);
2389 // The end of the navigation upgrade
2392 if ($result && $oldversion < 2009090800){
2393 //insert new record for log_display table
2394 //used to record tag update.
2395 if (!$DB->record_exists('log_display', array('action'=>'update', 'module'=>'tag'))) {
2396 $log_action = new object();
2397 $log_action->module = 'tag';
2398 $log_action->action = 'update';
2399 $log_action->mtable = 'tag';
2400 $log_action->field = 'name';
2402 $result = $result && $DB->insert_record('log_display', $log_action);
2404 upgrade_main_savepoint($result, 2009090800);
2407 if ($result && $oldversion < 2009100601) {
2408 // drop all previous tables defined during the dev phase
2409 $dropold = array('external_services_users', 'external_services_functions', 'external_services', 'external_functions');
2410 foreach ($dropold as $tablename) {
2411 $table = new xmldb_table($tablename);
2412 if ($dbman->table_exists($table)) {
2413 $dbman->drop_table($table);
2416 upgrade_main_savepoint($result, 2009100601);
2419 if ($result && $oldversion < 2009100602) {
2420 /// Define table external_functions to be created
2421 $table = new xmldb_table('external_functions');
2423 /// Adding fields to table external_functions
2424 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2425 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2426 $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2427 $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2428 $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2429 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2431 /// Adding keys to table external_functions
2432 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2434 /// Adding indexes to table external_functions
2435 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2437 /// Launch create table for external_functions
2438 $dbman->create_table($table);
2440 /// Main savepoint reached
2441 upgrade_main_savepoint($result, 2009100602);
2444 if ($result && $oldversion < 2009100603) {
2445 /// Define table external_services to be created
2446 $table = new xmldb_table('external_services');
2448 /// Adding fields to table external_services
2449 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2450 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2451 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2452 $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
2453 $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2454 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
2456 /// Adding keys to table external_services
2457 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2459 /// Adding indexes to table external_services
2460 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2462 /// Launch create table for external_services
2463 $dbman->create_table($table);
2465 /// Main savepoint reached
2466 upgrade_main_savepoint($result, 2009100603);
2469 if ($result && $oldversion < 2009100604) {
2470 /// Define table external_services_functions to be created
2471 $table = new xmldb_table('external_services_functions');
2473 /// Adding fields to table external_services_functions
2474 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2475 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2476 $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2478 /// Adding keys to table external_services_functions
2479 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2480 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2482 /// Launch create table for external_services_functions
2483 $dbman->create_table($table);
2485 /// Main savepoint reached
2486 upgrade_main_savepoint($result, 2009100604);
2489 if ($result && $oldversion < 2009100605) {
2490 /// Define table external_services_users to be created
2491 $table = new xmldb_table('external_services_users');
2493 /// Adding fields to table external_services_users
2494 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2495 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2496 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2497 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2498 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2499 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2501 /// Adding keys to table external_services_users
2502 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2503 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2504 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2506 /// Launch create table for external_services_users
2507 $dbman->create_table($table);
2509 /// Main savepoint reached
2510 upgrade_main_savepoint($result, 2009100605);
2513 if ($result && $oldversion < 2009102600) {
2515 /// Define table external_tokens to be created
2516 $table = new xmldb_table('external_tokens');
2518 /// Adding fields to table external_tokens
2519 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2520 $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
2521 $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2522 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2523 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2524 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
2525 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2526 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2527 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2528 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2529 $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2531 /// Adding keys to table external_tokens
2532 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2533 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2534 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2535 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2537 /// Launch create table for external_tokens
2538 $dbman->create_table($table);
2540 /// Main savepoint reached
2541 upgrade_main_savepoint($result, 2009102600);
2544 if ($result && $oldversion < 2009103000) {
2546 /// Define table blog_association to be created
2547 $table = new xmldb_table('blog_association');
2549 /// Adding fields to table blog_association
2550 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2551 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2552 $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2554 /// Adding keys to table blog_association
2555 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2556 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2557 $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
2559 /// Conditionally launch create table for blog_association
2560 if (!$dbman->table_exists($table)) {
2561 $dbman->create_table($table);
2564 /// Define table blog_external to be created
2565 $table = new xmldb_table('blog_external');
2567 /// Adding fields to table blog_external
2568 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2569 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2570 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2571 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2572 $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2573 $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2574 $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2575 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2576 $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2578 /// Adding keys to table blog_external
2579 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2580 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2582 /// Conditionally launch create table for blog_external
2583 if ($dbman->table_exists($table)) {
2584 // Delete the existing one first (comes from early dev version)
2585 $dbman->drop_table($table);
2587 $dbman->create_table($table);
2589 // Print notice about need to upgrade bloglevel
2590 if (($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete)) {
2591 echo $OUTPUT->notification(get_string('bloglevelupgradenotice', 'admin'));
2593 // email admins about the need to upgrade their system using the admin/bloglevelupgrade.php script
2594 $admins = get_admins();
2598 $a->sitename = $site->fullname;
2599 $a->fixurl = "$CFG->wwwroot/$CFG->admin/bloglevelupgrade.php";
2601 $subject = get_string('bloglevelupgrade', 'admin');
2602 $description = get_string('bloglevelupgradedescription', 'admin', $a);
2604 // can not use messaging here because it is not configured yet!
2605 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2607 /// Main savepoint reached
2608 upgrade_main_savepoint($result, 2009103000);
2611 if ($result && $oldversion < 2009110400) {
2613 // An array used to store the table name and keys of summary and trust fields
2615 $extendtables = array();
2616 $extendtables['course'] = array('summaryformat');
2617 $extendtables['course_categories'] = array('descriptionformat');
2618 $extendtables['course_request'] = array('summaryformat');
2619 $extendtables['grade_outcomes'] = array('descriptionformat');
2620 $extendtables['groups'] = array('descriptionformat');
2621 $extendtables['groupings'] = array('descriptionformat');
2622 $extendtables['scale'] = array('descriptionformat');
2623 $extendtables['user'] = array('descriptionformat');
2624 $extendtables['user_info_field'] = array('descriptionformat', 'defaultdataformat');
2625 $extendtables['user_info_data'] = array('dataformat');
2627 foreach ($extendtables as $tablestr=>$newfields) {
2628 $table = new xmldb_table($tablestr);
2629 foreach ($newfields as $fieldstr) {
2630 $field = new xmldb_field($fieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2631 // Check that the field doesn't already exists
2632 if (!$dbman->field_exists($table, $field)) {
2633 // Add the new field
2634 $dbman->add_field($table, $field);
2635 // Update the field if the text contains the default FORMAT_MOODLE to FORMAT_HTML
2636 if (($pos = strpos($fieldstr, 'format'))>0) {
2637 upgrade_set_timeout(60*20); // this may take a little while
2638 $params = array(FORMAT_HTML, '<p%', '%<br />%', FORMAT_MOODLE);
2639 $textfield = substr($fieldstr, 0, $pos);
2640 $DB->execute('UPDATE {'.$tablestr.'} SET '.$fieldstr.'=? WHERE ('.$textfield.' LIKE ? OR '.$textfield.' LIKE ?) AND '.$fieldstr.'=?', $params);
2646 unset($extendtables);
2648 upgrade_main_savepoint($result, 2009110400);
2651 if ($result && $oldversion < 2009110605) {
2653 /// Define field timecreated to be added to external_services
2654 $table = new xmldb_table('external_services');
2655 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'component');
2657 /// Conditionally launch add field timecreated
2658 if (!$dbman->field_exists($table, $field)) {
2659 $dbman->add_field($table, $field);
2662 /// Define field timemodified to be added to external_services
2663 $table = new xmldb_table('external_services');
2664 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'timecreated');
2666 /// Conditionally launch add field timemodified
2667 if (!$dbman->field_exists($table, $field)) {
2668 $dbman->add_field($table, $field);
2671 /// Main savepoint reached
2672 upgrade_main_savepoint($result, 2009110605);
2675 if ($result && $oldversion < 2009111600) {
2677 /// Define field instance to be added to portfolio_tempdata
2678 $table = new xmldb_table('portfolio_tempdata');
2679 $field = new xmldb_field('instance', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'userid');
2681 /// Conditionally launch add field instance
2682 if (!$dbman->field_exists($table, $field)) {
2683 $dbman->add_field($table, $field);
2686 $key = new xmldb_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
2688 /// Launch add key instancefk
2689 $dbman->add_key($table, $key);
2691 /// Main savepoint reached
2692 upgrade_main_savepoint($result, 2009111600);
2695 if ($result && $oldversion < 2009111700) {
2697 /// Define field tempdataid to be added to portfolio_log
2698 $table = new xmldb_table('portfolio_log');
2699 $field = new xmldb_field('tempdataid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'caller_sha1');
2701 /// Conditionally launch add field tempdataid
2702 if (!$dbman->field_exists($table, $field)) {
2703 $dbman->add_field($table, $field);
2706 /// Main savepoint reached
2707 upgrade_main_savepoint($result, 2009111700);
2710 if ($result && $oldversion < 2009111701) {
2712 /// Define field returnurl to be added to portfolio_log
2713 $table = new xmldb_table('portfolio_log');
2714 $field = new xmldb_field('returnurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'tempdataid');
2716 /// Conditionally launch add field returnurl
2717 if (!$dbman->field_exists($table, $field)) {
2718 $dbman->add_field($table, $field);
2721 /// Main savepoint reached
2722 upgrade_main_savepoint($result, 2009111701);
2725 if ($result && $oldversion < 2009111702) {
2727 /// Define field continueurl to be added to portfolio_log
2728 $table = new xmldb_table('portfolio_log');
2729 $field = new xmldb_field('continueurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'returnurl');
2731 /// Conditionally launch add field continueurl
2732 if (!$dbman->field_exists($table, $field)) {
2733 $dbman->add_field($table, $field);
2736 /// Main savepoint reached
2737 upgrade_main_savepoint($result, 2009111702);
2740 if ($result && $oldversion < 2009112400) {
2741 if (empty($CFG->passwordsaltmain)) {
2742 $subject = get_string('check_passwordsaltmain_name', 'report_security');
2743 $description = get_string('check_passwordsaltmain_warning', 'report_security');;
2744 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2746 upgrade_main_savepoint($result, 2009112400);
2749 if ($result && $oldversion < 2010010601) {
2751 /// Define field creatorid to be added to external_tokens
2752 $table = new xmldb_table('external_tokens');
2753 $field = new xmldb_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'contextid');
2755 /// Conditionally launch add field creatorid
2756 if (!$dbman->field_exists($table, $field)) {
2757 $dbman->add_field($table, $field);
2760 /// Define key creatorid (foreign) to be added to external_tokens
2761 $table = new xmldb_table('external_tokens');
2762 $key = new xmldb_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
2764 /// Launch add key creatorid
2765 $dbman->add_key($table, $key);
2767 /// Main savepoint reached
2768 upgrade_main_savepoint($result, 2010010601);
2771 if ($result && $oldversion < 2010011200) {
2772 $table = new xmldb_table('grade_categories');
2773 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
2775 if (!$dbman->field_exists($table, $field)) {
2776 $dbman->add_field($table, $field);
2779 upgrade_main_savepoint($result, 2010011200);
2783 if ($result && $oldversion < 2010012500) {
2784 upgrade_fix_incorrect_mnethostids();
2785 upgrade_main_savepoint($result, 2010012500);
2788 if ($result && $oldversion < 2010012600) {
2789 // do stuff to the mnet table
2790 $table = new xmldb_table('mnet_rpc');
2792 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2793 $dbman->rename_field($table, $field, 'plugintype');
2795 $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2796 $dbman->rename_field($table, $field, 'pluginname');
2798 $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
2799 if (!$dbman->field_exists($table, $field)) {
2800 $dbman->add_field($table, $field);
2803 $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
2804 if (!$dbman->field_exists($table, $field)) {
2805 $dbman->add_field($table, $field);
2808 $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
2809 if (!$dbman->field_exists($table, $field)) {
2810 $dbman->add_field($table, $field);
2813 /// Main savepoint reached
2814 upgrade_main_savepoint($result, 2010012600);
2817 if ($result && $oldversion < 2010012900) {
2819 /// Define table mnet_remote_rpc to be created
2820 $table = new xmldb_table('mnet_remote_rpc');
2822 /// Adding fields to table mnet_remote_rpc
2823 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2824 $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2825 $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
2827 /// Adding keys to table mnet_remote_rpc
2828 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2830 /// Conditionally launch create table for mnet_remote_rpc
2831 if (!$dbman->table_exists($table)) {
2832 $dbman->create_table($table);
2836 /// Define table mnet_remote_service2rpc to be created
2837 $table = new xmldb_table('mnet_remote_service2rpc');
2839 /// Adding fields to table mnet_remote_service2rpc
2840 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2841 $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2842 $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2844 /// Adding keys to table mnet_remote_service2rpc
2845 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2847 /// Adding indexes to table mnet_remote_service2rpc
2848 $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2850 /// Conditionally launch create table for mnet_remote_service2rpc
2851 if (!$dbman->table_exists($table)) {
2852 $dbman->create_table($table);
2856 /// Rename field function_name on table mnet_rpc to functionname
2857 $table = new xmldb_table('mnet_rpc');
2858 $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
2860 /// Launch rename field function_name
2861 $dbman->rename_field($table, $field, 'functionname');
2864 /// Rename field xmlrpc_path on table mnet_rpc to xmlrpcpath
2865 $table = new xmldb_table('mnet_rpc');
2866 $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
2868 /// Launch rename field xmlrpc_path
2869 $dbman->rename_field($table, $field, 'xmlrpcpath');
2872 /// Main savepoint reached
2873 upgrade_main_savepoint($result, 2010012900);
2876 if ($result && $oldversion < 2010012901) {
2878 /// Define field plugintype to be added to mnet_remote_rpc
2879 $table = new xmldb_table('mnet_remote_rpc');
2880 $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
2882 /// Conditionally launch add field plugintype
2883 if (!$dbman->field_exists($table, $field)) {
2884 $dbman->add_field($table, $field);
2887 /// Define field pluginname to be added to mnet_remote_rpc
2888 $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
2890 /// Conditionally launch add field pluginname
2891 if (!$dbman->field_exists($table, $field)) {
2892 $dbman->add_field($table, $field);
2895 /// Main savepoint reached
2896 upgrade_main_savepoint($result, 2010012901);
2899 if ($result && $oldversion < 2010012902) {
2901 /// Define field enabled to be added to mnet_remote_rpc
2902 $table = new xmldb_table('mnet_remote_rpc');
2903 $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
2905 /// Conditionally launch add field enabled
2906 if (!$dbman->field_exists($table, $field)) {
2907 $dbman->add_field($table, $field);
2910 /// Main savepoint reached
2911 upgrade_main_savepoint($result, 2010012902);
2914 /// MDL-17863. Increase the portno column length on mnet_host to handle any port number
2915 if ($result && $oldversion < 2010020100) {
2916 /// Changing precision of field portno on table mnet_host to (5)
2917 $table = new xmldb_table('mnet_host');
2918 $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
2920 /// Launch change of precision for field portno
2921 $dbman->change_field_precision($table, $field);
2923 upgrade_main_savepoint($result, 2010020100);
2926 if ($result && $oldversion < 2010020300) {
2928 /// Define field timecreated to be added to user
2929 $table = new xmldb_table('user');
2930 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
2932 if (!$dbman->field_exists($table, $field)) {
2933 /// Launch add field timecreated
2934 $dbman->add_field($table, $field);
2936 $DB->execute("UPDATE {user} SET timecreated = firstaccess");
2938 $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
2941 upgrade_main_savepoint($result, 2010020300);
2944 // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
2945 if ($result && $oldversion < 2010020301) {
2946 if ($preamble = $CFG->filter_tex_latexpreamble) {
2947 $preamble = preg_replace('/^ +/m', '', $preamble);
2948 set_config('filter_tex_latexpreamble', $preamble);
2950 upgrade_main_savepoint($result, 2010020301);
2953 if ($result && $oldversion < 2010021400) {
2954 /// Changes to modinfo mean we need to rebuild course cache
2955 require_once($CFG->dirroot . '/course/lib.php');
2956 rebuild_course_cache(0, true);
2957 upgrade_main_savepoint($result, 2010021400);
2960 if ($result && $oldversion < 2010021800) {
2961 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
2962 upgrade_main_savepoint($result, 2010021800);
2965 if ($result && $oldversion < 2010031800) {
2966 //drop the erroneously created ratings table
2967 $table = new xmldb_table('ratings');
2968 if ($dbman->table_exists($table)) {
2969 $dbman->drop_table($table);
2972 //create the rating table (replaces module specific rating implementations)
2973 $table = new xmldb_table('rating');
2974 if ($dbman->table_exists($table)) {
2975 $dbman->drop_table($table);
2978 /// Adding fields to table rating
2979 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2980 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2982 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2983 $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2984 $table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2985 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2987 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2988 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2990 /// Adding keys to table rating
2991 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2992 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2993 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2995 /// Adding indexes to table rating
2996 $table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
2998 /// Create table for ratings
2999 if (!$dbman->table_exists($table)) {
3000 $dbman->create_table($table);
3003 upgrade_main_savepoint($result, 2010031800);
3006 if ($result && $oldversion < 2010031900) {
3007 // regeneration of sessions is always enabled, no need for this setting any more
3008 unset_config('regenloginsession');
3009 upgrade_main_savepoint($result, 2010031900);
3013 if ($result && $oldversion < 2010032400) {
3014 // Upgrade all of those using the standardold theme to the use the standard
3016 if ($CFG->theme == 'standardold') {
3017 // The config setting that affects the whole site
3018 set_config('theme', 'standard');
3020 // Course Categories
3021 $DB->execute('UPDATE {course_categories} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3023 $DB->execute('UPDATE {course} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3025 $DB->execute('UPDATE {user} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3026 upgrade_main_savepoint($result, 2010032400);
3029 if ($result && $oldversion < 2010033101.01) {
3031 /// Define field source to be added to files
3032 $table = new xmldb_table('files');
3034 $field = new xmldb_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'status');
3036 /// Conditionally launch add field source
3037 if (!$dbman->field_exists($table, $field)) {
3038 $dbman->add_field($table, $field);
3041 $field = new xmldb_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'source');
3043 /// Conditionally launch add field author
3044 if (!$dbman->field_exists($table, $field)) {
3045 $dbman->add_field($table, $field);
3048 $field = new xmldb_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'author');
3050 /// Conditionally launch add field license
3051 if (!$dbman->field_exists($table, $field)) {
3052 $dbman->add_field($table, $field);
3055 upgrade_main_savepoint($result, 2010033101.01);
3058 if ($result && $oldversion < 2010033101.02) {
3060 /// Define table license to be created
3061 $table = new xmldb_table('license');
3063 /// Adding fields to table license
3064 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3065 $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3066 $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
3067 $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3068 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
3069 $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
3071 /// Adding keys to table license
3072 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3074 /// Conditionally launch create table for license
3075 if (!$dbman->table_exists($table)) {
3076 $dbman->create_table($table);
3078 $active_licenses = array();
3080 $license = new stdclass;
3082 // add unknown license
3083 $license->shortname = 'unknown';
3084 $license->fullname = 'Unknown license';
3085 $license->source = '';
3086 $license->enabled = 1;
3087 $license->version = '2010033100';
3088 $active_licenses[] = $license->shortname;
3089 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3090 if ($record->version < $license->version) {
3091 // update license record
3092 $license->enabled = $record->enabled;
3093 $license->id = $record->id;
3094 $DB->update_record('license', $license);
3097 $DB->insert_record('license', $license);
3100 // add all rights reserved license
3101 $license->shortname = 'allrightsreserved';
3102 $license->fullname = 'All rights reserved';
3103 $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
3104 $license->enabled = 1;
3105 $license->version = '2010033100';
3106 $active_licenses[] = $license->shortname;
3107 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3108 if ($record->version < $license->version) {
3109 // update license record
3110 $license->id = $record->id;
3111 $license->enabled = $record->enabled;
3112 $DB->update_record('license', $license);
3115 $DB->insert_record('license', $license);
3118 // add public domain license
3119 $license->shortname = 'public';
3120 $license->fullname = 'Public Domain';
3121 $license->source = 'http://creativecommons.org/licenses/publicdomain/';
3122 $license->enabled = 1;
3123 $license->version = '2010033100';
3124 $active_licenses[] = $license->shortname;
3125 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3126 if ($record->version < $license->version) {
3127 // update license record
3128 $license->enabled = $record->enabled;
3129 $license->id = $record->id;
3130 $DB->update_record('license', $license);
3133 $DB->insert_record('license', $license);
3136 // add creative commons license
3137 $license->shortname = 'cc';
3138 $license->fullname = 'Creative Commons';
3139 $license->source = 'http://creativecommons.org/licenses/by/3.0/';
3140 $license->enabled = 1;
3141 $license->version = '2010033100';
3142 $active_licenses[] = $license->shortname;
3143 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3144 if ($record->version < $license->version) {
3145 // update license record
3146 $license->enabled = $record->enabled;
3147 $license->id = $record->id;
3148 $DB->update_record('license', $license);
3151 $DB->insert_record('license', $license);
3154 // add creative commons no derivs license
3155 $license->shortname = 'cc-nd';
3156 $license->fullname = 'Creative Commons - NoDerivs';
3157 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3158 $license->enabled = 1;
3159 $license->version = '2010033100';
3160 $active_licenses[] = $license->shortname;
3161 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3162 if ($record->version < $license->version) {
3163 // update license record
3164 $license->enabled = $record->enabled;
3165 $license->id = $record->id;
3166 $DB->update_record('license', $license);
3169 $DB->insert_record('license', $license);
3172 // add creative commons no commercial no derivs license
3173 $license->shortname = 'cc-nc-nd';
3174 $license->fullname = 'Creative Commons - No Commercial NoDerivs';
3175 $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
3176 $license->enabled = 1;
3177 $license->version = '2010033100';
3178 $active_licenses[] = $license->shortname;
3179 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3180 if ($record->version < $license->version) {
3181 // update license record
3182 $license->enabled = $record->enabled;
3183 $license->id = $record->id;
3184 $DB->update_record('license', $license);
3187 $DB->insert_record('license', $license);
3190 // add creative commons no commercial
3191 $license->shortname = 'cc-nc-nd';
3192 $license->shortname = 'cc-nc';
3193 $license->fullname = 'Creative Commons - No Commercial';
3194 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3195 $license->enabled = 1;
3196 $license->version = '2010033100';
3197 $active_licenses[] = $license->shortname;
3198 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3199 if ($record->version < $license->version) {
3200 // update license record
3201 $license->enabled = $record->enabled;
3202 $license->id = $record->id;
3203 $DB->update_record('license', $license);
3206 $DB->insert_record('license', $license);
3209 // add creative commons no commercial sharealike
3210 $license->shortname = 'cc-nc-sa';
3211 $license->fullname = 'Creative Commons - No Commercial ShareAlike';
3212 $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
3213 $license->enabled = 1;
3214 $license->version = '2010033100';
3215 $active_licenses[] = $license->shortname;
3216 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3217 if ($record->version < $license->version) {
3218 // update license record
3219 $license->enabled = $record->enabled;
3220 $license->id = $record->id;
3221 $DB->update_record('license', $license);
3224 $DB->insert_record('license', $license);
3227 // add creative commons sharealike
3228 $license->shortname = 'cc-sa';
3229 $license->fullname = 'Creative Commons - ShareAlike';
3230 $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
3231 $license->enabled = 1;
3232 $license->version = '2010033100';
3233 $active_licenses[] = $license->shortname;
3234 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3235 if ($record->version < $license->version) {
3236 // update license record
3237 $license->enabled = $record->enabled;
3238 $license->id = $record->id;
3239 $DB->update_record('license', $license);
3242 $DB->insert_record('license', $license);
3245 set_config('licenses', implode(',', $active_licenses));
3246 /// set site default license
3247 set_config('sitedefaultlicense', 'allrightsreserved');
3249 /// Main savepoint reached
3250 upgrade_main_savepoint($result, 2010033101.02);
3253 if ($result && $oldversion < 2010033102.00) {
3254 // rename course view capability to participate
3255 $params = array('view'=>'moodle/course:view', 'participate'=>'moodle/course:participate');
3256 $sql = "UPDATE {role_capabilities} SET capability = :participate WHERE capability = :view";
3257 $DB->execute($sql, $params);
3258 $sql = "UPDATE {capabilities} SET name = :participate WHERE name = :view";
3259 $DB->execute($sql, $params);
3260 // note: the view capability is readded again at the end of upgrade, but with different meaning
3261 upgrade_main_savepoint($result, 2010033102.00);
3264 if ($result && $oldversion < 2010033102.01) {
3265 // Define field archetype to be added to role table
3266 $table = new xmldb_table('role');
3267 $field = new xmldb_field('archetype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'sortorder');
3268 $dbman->add_field($table, $field);
3269 upgrade_main_savepoint($result, 2010033102.01);
3272 if ($result && $oldversion < 2010033102.02) {
3273 // Set archetype for existing roles and change admin role to manager role
3274 $sql = "SELECT r.*, rc.capability
3276 JOIN {role_capabilities} rc ON rc.roleid = r.id
3277 WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
3279 $params = array('syscontextid'=>SYSCONTEXTID, 'legacycaps'=>'moodle/legacy:%');
3280 $substart = strlen('moodle/legacy:');
3281 $roles = $DB->get_recordset_sql($sql, $params); // in theory could be multiple legacy flags in one role
3282 foreach ($roles as $role) {
3283 $role->archetype = substr($role->capability, $substart);
3284 unset($role->capability);
3285 if ($role->archetype === 'admin') {
3286 $role->archetype = 'manager';
3287 if ($role->shortname === 'admin') {
3288 $role->shortname = 'manager';
3289 $role->name = get_string('manager', 'role');
3290 $role->description = get_string('managerdescription', 'role');
3293 $DB->update_record('role', $role);
3297 upgrade_main_savepoint($result, 2010033102.02);
3300 if ($result && $oldversion < 2010033102.03) {
3301 // Now pick site admins (===have manager role assigned at the system context)
3302 // and store them in the new $CFG->siteadmins setting as comma separated list
3303 $sql = "SELECT ra.id, ra.userid
3304 FROM {role_assignments} ra
3305 JOIN {role} r ON r.id = ra.roleid
3306 JOIN {user} u ON u.id = ra.userid
3307 WHERE ra.contextid = :syscontext AND r.archetype = 'manager' AND u.deleted = 0
3309 $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
3311 foreach ($ras as $ra) {
3312 $admins[$ra->userid] = $ra->userid;
3313 set_config('siteadmins', implode(',', $admins)); // better to save it repeatedly, we do need at least one admin
3314 $DB->delete_records('role_assignments', array('id'=>$ra->id));
3317 upgrade_main_savepoint($result, 2010033102.03);
3320 if ($result && $oldversion < 2010033102.04) {
3321 // clean up the manager roles
3322 $managers = $DB->get_records('role', array('archetype'=>'manager'));
3323 foreach ($managers as $manager) {
3324 // now sanitize the capabilities and overrides
3325 $DB->delete_records('role_capabilities', array('capability'=>'moodle/site:config', 'roleid'=>$manager->id)); // only site admins may configure servers
3326 // note: doanything and legacy caps are deleted automatically, they get moodle/course:view later at the end of the upgrade
3328 // set usable contexts
3329 $DB->delete_records('role_context_levels', array('roleid'=>$manager->id));
3330 $assignlevels = array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE);
3331 foreach ($assignlevels as $assignlevel) {
3332 $record = (object)array('roleid'=>$manager->id, 'contextlevel'=>$assignlevel);
3333 $DB->insert_record('role_context_levels', $record);
3336 // remove manager role assignments bellow the course context level - admin role was never intended for activities and blocks,
3337 // the problem is that those assignments would not be visible after upgrade and old style admins in activities make no sense anyway
3338 $DB->delete_records_select('role_assignments', "roleid = :manager AND contextid IN (SELECT id FROM {context} WHERE contextlevel > 50)", array('manager'=>$manager->id));
3340 // allow them to assign all roles except default user, guest and frontpage - users get these roles automatically on the fly when needed
3341 $DB->delete_records('role_allow_assign', array('roleid'=>$manager->id));
3342 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype <> 'user' AND archetype <> 'guest' AND archetype <> 'frontpage'");
3343 foreach ($roles as $role) {
3344 $record = (object)array('roleid'=>$manager->id, 'allowassign'=>$role->id);
3345 $DB->insert_record('role_allow_assign', $record);
3348 // allow them to override all roles
3349 $DB->delete_records('role_allow_override', array('roleid'=>$manager->id));
3350 $roles = $DB->get_records_sql("SELECT * FROM {role}");
3351 foreach ($roles as $role) {
3352 $record = (object)array('roleid'=>$manager->id, 'allowoverride'=>$role->id);
3353 $DB->insert_record('role_allow_override', $record);
3356 // allow them to switch to all following roles
3357 $DB->delete_records('role_allow_switch', array('roleid'=>$manager->id));
3358 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype IN ('student', 'teacher', 'editingteacher')");
3359 foreach ($roles as $role) {
3360 $record = (object)array('roleid'=>$manager->id, 'allowswitch'=>$role->id);
3361 $DB->insert_record('role_allow_switch', $record);
3365 upgrade_main_savepoint($result, 2010033102.04);
3368 if ($result && $oldversion < 2010033102.05) {
3369 // remove course:view from all roles that are not used for enrolment, it does NOT belong there because it really means user is enrolled!
3370 $noenrolroles = $DB->get_records_select('role', "archetype IN ('guest', 'user', 'manager', 'coursecreator', 'frontpage')");
3371 foreach ($noenrolroles as $role) {
3372 $DB->delete_records('role_capabilities', array('roleid'=>$role->id, 'capability'=>'moodle/course:participate'));
3374 upgrade_main_savepoint($result, 2010033102.05);
3377 if ($result && $oldversion < 2010033102.06) {
3378 // make sure there is nothing weird in default user role
3379 if (!empty($CFG->defaultuserroleid)) {
3380 if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
3381 if ($role->archetype !== '' and $role->archetype !== 'user') {
3382 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default authenticated user role (defaultuserroleid) value is invalid, setting cleared.');
3383 unset_config('defaultuserroleid');
3386 unset_config('defaultuserroleid');
3389 upgrade_main_savepoint($result, 2010033102.06);
3392 if ($result && $oldversion < 2010033102.07) {
3393 if (!empty($CFG->displayloginfailures) and $CFG->displayloginfailures === 'teacher') {
3394 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Displaying of login failuters to teachers is not supported any more.');
3395 unset_config('displayloginfailures');
3397 upgrade_main_savepoint($result, 2010033102.07);
3400 if ($result && $oldversion < 2010033102.08) {
3401 // make sure there are no problems in default guest role settings
3402 if (!empty($CFG->guestroleid)) {
3403 if ($role = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
3404 if ($role->archetype !== '' and $role->archetype !== 'guest') {
3405 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default guest role (guestroleid) value is invalid, setting cleared.');
3406 unset_config('guestroleid');
3409 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) doeas not exist, setting cleared.');
3410 unset_config('guestroleid');
3413 // remove all roles of the guest account - the only way to change it is to override the guest role, sorry
3414 // the guest account gets all the role assignemnts on the fly whcih works fine in has_capability(),
3415 $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE username = 'guest')");
3417 upgrade_main_savepoint($result, 2010033102.08);
3420 if ($result && $oldversion < 2010033102.09) {
3421 // For MDL-17501. Ensure that any role that has moodle/course:update also has moodle/course:visibility.
3422 // Get the roles with 'moodle/course:update'.
3423 $systemcontext = get_context_instance(CONTEXT_SYSTEM);