MDL-21146 fixed upgrade regression - we need to create all necessary "files" fields...
[moodle.git] / lib / db / upgrade.php
CommitLineData
117bd748 1<?PHP
4e423cbf 2
3// This file keeps track of upgrades to Moodle.
4//
5// Sometimes, changes between versions involve
6// alterations to database structures and other
7// major things that may break installations.
8//
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.
12//
13// If there's something it cannot do itself, it
14// will tell you what you need to do.
15//
16// The commands in here will all be database-neutral,
b1f93b15 17// using the methods of database_manager class
775f811a 18//
19// Please do not forget to use upgrade_set_timeout()
20// before any action that may take longer time to finish.
4e423cbf 21
775f811a 22function xmldb_main_upgrade($oldversion) {
78946b9b 23 global $CFG, $USER, $DB, $OUTPUT;
4e423cbf 24
4f12838e 25 require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
13a0d3d3 26
4e423cbf 27 $result = true;
28
46d318bc 29 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
f33e1ed4 30
1ea260d8 31 ////////////////////////////////////////
32 ///upgrade supported only from 1.9.x ///
33 ////////////////////////////////////////
da4aa3e4 34
795a08ad 35 if ($result && $oldversion < 2008030600) {
36 //NOTE: this table was added much later, that is why this step is repeated later in this file
37
38 /// Define table upgrade_log to be created
39 $table = new xmldb_table('upgrade_log');
40
41 /// Adding fields to table upgrade_log
2a88f626 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);
795a08ad 51
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'));
55
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'));
59
60 /// Create table for upgrade_log
61 $dbman->create_table($table);
62
63 /// Main savepoint reached
64 upgrade_main_savepoint($result, 2008030600);
65 }
66
a1f27717 67 if ($result && $oldversion < 2008030700) {
775f811a 68 upgrade_set_timeout(60*20); // this may take a while
a1f27717 69
70 /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
a8cb94f6 71 $table = new xmldb_table('grade_letters');
69b80cc2 72 $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
a1f27717 73
74 /// Launch drop index contextid-lowerboundary
eee5d9bb 75 $dbman->drop_index($table, $index);
a1f27717 76
a1f27717 77 /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
a8cb94f6 78 $table = new xmldb_table('grade_letters');
69b80cc2 79 $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
a1f27717 80
81 /// Launch add index contextid-lowerboundary-letter
eee5d9bb 82 $dbman->add_index($table, $index);
a1f27717 83
84 /// Main savepoint reached
0f77b186 85 upgrade_main_savepoint($result, 2008030700);
86 }
a1f27717 87
1ea260d8 88 if ($result && $oldversion < 2008050100) {
89 // Update courses that used weekscss to weeks
a8cb94f6 90 $result = $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
1ea260d8 91 upgrade_main_savepoint($result, 2008050100);
92 }
93
0b51c247 94 if ($result && $oldversion < 2008050200) {
95 // remove unused config options
96 unset_config('statsrolesupgraded');
97 upgrade_main_savepoint($result, 2008050200);
98 }
99
994fbaab 100 if ($result && $oldversion < 2008050700) {
775f811a 101 upgrade_set_timeout(60*20); // this may take a while
102
994fbaab 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);
107 }
108
8b9cfac4 109 if ($result && $oldversion < 2008051201) {
aa9a6867 110 echo $OUTPUT->notification('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
775f811a 111 upgrade_set_timeout(60*20); // this may take a while
8b9cfac4 112
469dcbcc 113 /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
cbc08f3b 114 $dbfamily = $DB->get_dbfamily();
115 if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
eee5d9bb 116 $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
469dcbcc 117 }
118
8b9cfac4 119 /// Define index idnumber (not unique) to be dropped form user
a8cb94f6 120 $table = new xmldb_table('user');
69b80cc2 121 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
8b9cfac4 122
123 /// Launch drop index idnumber
f33e1ed4 124 if ($dbman->index_exists($table, $index)) {
eee5d9bb 125 $dbman->drop_index($table, $index);
8b9cfac4 126 }
127
128 /// Changing precision of field idnumber on table user to (255)
a8cb94f6 129 $table = new xmldb_table('user');
2a88f626 130 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
8b9cfac4 131
132 /// Launch change of precision for field idnumber
eee5d9bb 133 $dbman->change_field_precision($table, $field);
8b9cfac4 134
135 /// Launch add index idnumber again
69b80cc2 136 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
eee5d9bb 137 $dbman->add_index($table, $index);
8b9cfac4 138
139 /// Main savepoint reached
140 upgrade_main_savepoint($result, 2008051201);
141 }
142
7ade55ad 143 if ($result && $oldversion < 2008051202) {
910dd4dd 144 $log_action = new object();
7ade55ad 145 $log_action->module = 'course';
146 $log_action->action = 'unenrol';
147 $log_action->mtable = 'course';
148 $log_action->field = 'fullname';
910dd4dd 149 if (!$DB->record_exists('log_display', array('action'=>'unenrol', 'module'=>'course'))) {
150 $result = $result && $DB->insert_record('log_display', $log_action);
7ade55ad 151 }
152 upgrade_main_savepoint($result, 2008051202);
153 }
154
fa162144 155 if ($result && $oldversion < 2008051203) {
156 $table = new xmldb_table('mnet_enrol_course');
2a88f626 157 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
eee5d9bb 158 $dbman->change_field_precision($table, $field);
fa162144 159 upgrade_main_savepoint($result, 2008051203);
160 }
161
38fb8190 162 if ($result && $oldversion < 2008063001) {
775f811a 163 upgrade_set_timeout(60*20); // this may take a while
164
38fb8190 165 // table to be modified
166 $table = new xmldb_table('tag_instance');
167 // add field
168 $field = new xmldb_field('tiuserid');
169 if (!$dbman->field_exists($table, $field)) {
2a88f626 170 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
38fb8190 171 $dbman->add_field($table, $field);
172 }
173 // modify index
174 $index = new xmldb_index('itemtype-itemid-tagid');
175 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
b91de8a5 176 if ($dbman->index_exists($table, $index)) {
177 $dbman->drop_index($table, $index);
178 }
38fb8190 179 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
180 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
b91de8a5 181 if (!$dbman->index_exists($table, $index)) {
182 $dbman->add_index($table, $index);
183 }
38fb8190 184
185 /// Main savepoint reached
186 upgrade_main_savepoint($result, 2008063001);
187 }
38fb8190 188
a036d7f3 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);
192 }
4e781c7b 193
ac5efef6 194 if ($result && $oldversion < 2008070701) {
195
196 /// Define table portfolio_instance to be created
197 $table = new xmldb_table('portfolio_instance');
198
199 /// Adding fields to table portfolio_instance
2a88f626 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');
ac5efef6 204
205 /// Adding keys to table portfolio_instance
206 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
207
208 /// Conditionally launch create table for portfolio_instance
209 if (!$dbman->table_exists($table)) {
210 $dbman->create_table($table);
211 }
212 /// Define table portfolio_instance_config to be created
213 $table = new xmldb_table('portfolio_instance_config');
214
215 /// Adding fields to table portfolio_instance_config
2a88f626 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);
ac5efef6 220
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'));
224
225 /// Adding indexes to table portfolio_instance_config
226 $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
227
228 /// Conditionally launch create table for portfolio_instance_config
229 if (!$dbman->table_exists($table)) {
230 $dbman->create_table($table);
231 }
232
233 /// Define table portfolio_instance_user to be created
234 $table = new xmldb_table('portfolio_instance_user');
235
236 /// Adding fields to table portfolio_instance_user
2a88f626 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);
ac5efef6 242
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'));
247
248 /// Conditionally launch create table for portfolio_instance_user
249 if (!$dbman->table_exists($table)) {
250 $dbman->create_table($table);
251 }
252
253 /// Main savepoint reached
254 upgrade_main_savepoint($result, 2008070701);
255 }
172dd12c 256
775f811a 257 if ($result && $oldversion < 2008072400) {
258 /// Create the database tables for message_processors
f65b2dae 259 $table = new xmldb_table('message_processors');
2a88f626 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);
f65b2dae 262 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3b120e46 263 $dbman->create_table($table);
264
3b120e46 265 /// delete old and create new fields
f65b2dae 266 $table = new xmldb_table('message');
267 $field = new xmldb_field('messagetype');
3b120e46 268 $dbman->drop_field($table, $field);
269
270 /// fields to rename
f65b2dae 271 $field = new xmldb_field('message');
2a88f626 272 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 273 $dbman->rename_field($table, $field, 'fullmessage');
f65b2dae 274 $field = new xmldb_field('format');
2a88f626 275 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
3b120e46 276 $dbman->rename_field($table, $field, 'fullmessageformat');
277
278 /// new message fields
f65b2dae 279 $field = new xmldb_field('subject');
2a88f626 280 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 281 $dbman->add_field($table, $field);
f65b2dae 282 $field = new xmldb_field('fullmessagehtml');
2a88f626 283 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
3b120e46 284 $dbman->add_field($table, $field);
f65b2dae 285 $field = new xmldb_field('smallmessage');
2a88f626 286 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 287 $dbman->add_field($table, $field);
288
289
f65b2dae 290 $table = new xmldb_table('message_read');
291 $field = new xmldb_field('messagetype');
3b120e46 292 $dbman->drop_field($table, $field);
f65b2dae 293 $field = new xmldb_field('mailed');
3b120e46 294 $dbman->drop_field($table, $field);
295
296 /// fields to rename
f65b2dae 297 $field = new xmldb_field('message');
2a88f626 298 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 299 $dbman->rename_field($table, $field, 'fullmessage');
f65b2dae 300 $field = new xmldb_field('format');
2a88f626 301 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
3b120e46 302 $dbman->rename_field($table, $field, 'fullmessageformat');
303
304
305 /// new message fields
f65b2dae 306 $field = new xmldb_field('subject');
2a88f626 307 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 308 $dbman->add_field($table, $field);
f65b2dae 309 $field = new xmldb_field('fullmessagehtml');
2a88f626 310 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
3b120e46 311 $dbman->add_field($table, $field);
f65b2dae 312 $field = new xmldb_field('smallmessage');
2a88f626 313 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 314 $dbman->add_field($table, $field);
315
316 /// new table
f65b2dae 317 $table = new xmldb_table('message_working');
2a88f626 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);
f65b2dae 321 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3b120e46 322 $dbman->create_table($table);
323
324
325 upgrade_main_savepoint($result, 2008072400);
326 }
327
4e781c7b 328 if ($result && $oldversion < 2008072800) {
329
330 /// Define field enablecompletion to be added to course
331 $table = new xmldb_table('course');
332 $field = new xmldb_field('enablecompletion');
2a88f626 333 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
4e781c7b 334
335 /// Launch add field enablecompletion
775f811a 336 if (!$dbman->field_exists($table,$field)) {
4e781c7b 337 $dbman->add_field($table, $field);
338 }
775f811a 339
4e781c7b 340 /// Define field completion to be added to course_modules
341 $table = new xmldb_table('course_modules');
342 $field = new xmldb_field('completion');
2a88f626 343 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'groupmembersonly');
4e781c7b 344
345 /// Launch add field completion
775f811a 346 if (!$dbman->field_exists($table,$field)) {
4e781c7b 347 $dbman->add_field($table, $field);
348 }
349
350 /// Define field completiongradeitemnumber to be added to course_modules
351 $field = new xmldb_field('completiongradeitemnumber');
2a88f626 352 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'completion');
4e781c7b 353
354 /// Launch add field completiongradeitemnumber
775f811a 355 if (!$dbman->field_exists($table,$field)) {
4e781c7b 356 $dbman->add_field($table, $field);
357 }
358
359 /// Define field completionview to be added to course_modules
360 $field = new xmldb_field('completionview');
2a88f626 361 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiongradeitemnumber');
4e781c7b 362
363 /// Launch add field completionview
775f811a 364 if (!$dbman->field_exists($table,$field)) {
4e781c7b 365 $dbman->add_field($table, $field);
366 }
367
368 /// Define field completionexpected to be added to course_modules
369 $field = new xmldb_field('completionexpected');
2a88f626 370 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionview');
4e781c7b 371
372 /// Launch add field completionexpected
775f811a 373 if (!$dbman->field_exists($table,$field)) {
4e781c7b 374 $dbman->add_field($table, $field);
375 }
376
377 /// Define table course_modules_completion to be created
378 $table = new xmldb_table('course_modules_completion');
775f811a 379 if (!$dbman->table_exists($table)) {
4e781c7b 380
381 /// Adding fields to table course_modules_completion
2a88f626 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);
775f811a 388
4e781c7b 389 /// Adding keys to table course_modules_completion
1e0c56ea 390 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
775f811a 391
4e781c7b 392 /// Adding indexes to table course_modules_completion
1e0c56ea 393 $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
394 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
775f811a 395
4e781c7b 396 /// Launch create table for course_modules_completion
397 $dbman->create_table($table);
398 }
399
400 /// Main savepoint reached
401 upgrade_main_savepoint($result, 2008072800);
402 }
775f811a 403
4ab16a09 404 if ($result && $oldversion < 2008073000) {
ac5efef6 405
4ab16a09 406 /// Define table portfolio_log to be created
407 $table = new xmldb_table('portfolio_log');
408
409 /// Adding fields to table portfolio_log
2a88f626 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);
4ab16a09 417
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'));
422
423 /// Conditionally launch create table for portfolio_log
424 if (!$dbman->table_exists($table)) {
425 $dbman->create_table($table);
426 }
427
428 /// Main savepoint reached
429 upgrade_main_savepoint($result, 2008073000);
430 }
120b3758 431
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);
437 }
438
439 /// Define table message_providers to be created
440 $table = new xmldb_table('message_providers');
441
442 /// Adding fields to table message_providers
2a88f626 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);
120b3758 447
448 /// Adding keys to table message_providers
449 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
450
451 /// Adding indexes to table message_providers
452 $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
453
454 /// Create table for message_providers
455 $dbman->create_table($table);
456
457 upgrade_main_savepoint($result, 2008073104);
458 }
172dd12c 459
460 if ($result && $oldversion < 2008073111) {
461 /// Define table files to be created
462 $table = new xmldb_table('files');
463
464 /// Adding fields to table files
2a88f626 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');
f0b9ee18
PS
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);
2a88f626 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);
172dd12c 482
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'));
487
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'));
492
493 /// Conditionally launch create table for files
494 $dbman->create_table($table);
495
496 /// Main savepoint reached
497 upgrade_main_savepoint($result, 2008073111);
498 }
499
172dd12c 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);
505 }
506
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);
512 }
f33e1ed4 513
1ce2da58 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)) {
2a88f626 519 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
c5a04d12 520 $dbman->add_field($table, $field);
521 $result = $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
1ce2da58 522 $result = $result && $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
523 }
524
525 /// Main savepoint reached
526 upgrade_main_savepoint($result, 2008080400);
527 }
04f35360 528
529 if ($result && $oldversion < 2008080500) {
530
531 /// Define table portfolio_tempdata to be created
532 $table = new xmldb_table('portfolio_tempdata');
533
534 /// Adding fields to table portfolio_tempdata
2a88f626 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);
04f35360 537
538 /// Adding keys to table portfolio_tempdata
539 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
540
541 /// Conditionally launch create table for portfolio_tempdata
542 if (!$dbman->table_exists($table)) {
543 $dbman->create_table($table);
544 }
545
546 /// Main savepoint reached
547 upgrade_main_savepoint($result, 2008080500);
548 }
549
84a44985 550 if ($result && $oldversion < 2008080600) {
551
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');
2a88f626 555 $field = new xmldb_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'data');
84a44985 556
557 /// Conditionally launch add field expirytime
558 if (!$dbman->field_exists($table, $field)) {
559 $dbman->add_field($table, $field);
560 }
561
562 /// Main savepoint reached
563 upgrade_main_savepoint($result, 2008080600);
564 }
775f811a 565
c1f58efe 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);
572 }
573
f9a2cf86 574 if ($result && $oldversion < 2008081501) {
575 $table = new xmldb_table('question');
2a88f626 576 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'defaultgrade');
f9a2cf86 577 $dbman->change_field_type($table, $field);
578 upgrade_main_savepoint($result, 2008081501);
579 }
580
581 if ($result && $oldversion < 2008081502) {
582 $table = new xmldb_table('question_answers');
2a88f626 583 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'answer');
f9a2cf86 584 $dbman->change_field_type($table, $field);
585 upgrade_main_savepoint($result, 2008081502);
586 }
587
588 if ($result && $oldversion < 2008081503) {
589 $table = new xmldb_table('question_sessions');
2a88f626 590 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'newgraded');
f9a2cf86 591 $dbman->change_field_type($table, $field);
592 upgrade_main_savepoint($result, 2008081503);
593 }
594
595 if ($result && $oldversion < 2008081504) {
596 $table = new xmldb_table('question_states');
2a88f626 597 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'event');
f9a2cf86 598 $dbman->change_field_type($table, $field);
599 upgrade_main_savepoint($result, 2008081504);
600 }
601
602 if ($result && $oldversion < 2008081505) {
603 $table = new xmldb_table('question_states');
2a88f626 604 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'grade');
f9a2cf86 605 $dbman->change_field_type($table, $field);
606 upgrade_main_savepoint($result, 2008081505);
607 }
608
609 if ($result && $oldversion < 2008081506) {
610 $table = new xmldb_table('question_states');
2a88f626 611 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'raw_grade');
f9a2cf86 612 $dbman->change_field_type($table, $field);
613 upgrade_main_savepoint($result, 2008081506);
614 }
615
775f811a 616 if ($result && $oldversion < 2008081600) {
617
618 /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
619 unset_config('unicodedb');
620
621 /// Main savepoint reached
622 upgrade_main_savepoint($result, 2008081600);
623 }
624
ff2f69bc 625 if ($result && $oldversion < 2008081900) {
626 /// Define field userid to be added to portfolio_tempdata
627 $table = new xmldb_table('portfolio_tempdata');
2a88f626 628 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'expirytime');
ff2f69bc 629
630 /// Conditionally launch add field userid
631 if (!$dbman->field_exists($table, $field)) {
632 $dbman->add_field($table, $field);
633 }
634 $DB->set_field('portfolio_tempdata', 'userid', 0);
635 /// now change it to be notnull
636
637 /// Changing nullability of field userid on table portfolio_tempdata to not null
2a88f626 638 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'expirytime');
ff2f69bc 639
640 /// Launch change of nullability for field userid
641 $dbman->change_field_notnull($table, $field);
642
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'));
646
647 /// Launch add key userfk
648 $dbman->add_key($table, $key);
649
650 upgrade_main_savepoint($result, 2008081900);
651 }
8f3f6cc3 652 if ($result && $oldversion < 2008082602) {
27051e43 653
654 /// Define table repository to be dropped
655 $table = new xmldb_table('repository');
656
657 /// Conditionally launch drop table for repository
658 if ($dbman->table_exists($table)) {
659 $dbman->drop_table($table);
660 }
661
662 /// Define table repository to be created
663 $table = new xmldb_table('repository');
664
665 /// Adding fields to table repository
2a88f626 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');
27051e43 670
671 /// Adding keys to table repository
672 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
673
674 /// Conditionally launch create table for repository
675 if (!$dbman->table_exists($table)) {
676 $dbman->create_table($table);
677 }
678 /// Define table repository_instances to be created
679 $table = new xmldb_table('repository_instances');
680
681 /// Adding fields to table repository_instances
2a88f626 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);
27051e43 691
692 /// Adding keys to table repository_instances
693 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
694
695 /// Conditionally launch create table for repository_instances
696 if (!$dbman->table_exists($table)) {
697 $dbman->create_table($table);
698 }
699
700 /// Define table repository_instance_config to be created
701 $table = new xmldb_table('repository_instance_config');
702
703 /// Adding fields to table repository_instance_config
2a88f626 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);
27051e43 708
709 /// Adding keys to table repository_instance_config
710 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
711
712 /// Conditionally launch create table for repository_instance_config
713 if (!$dbman->table_exists($table)) {
714 $dbman->create_table($table);
715 }
716
717 /// Main savepoint reached
8f3f6cc3 718 upgrade_main_savepoint($result, 2008082602);
27051e43 719 }
720
62e76c67 721 if ($result && $oldversion < 2008082700) {
722 /// Add a new column to the question sessions table to record whether a
723 /// question has been flagged.
724
725 /// Define field flagged to be added to question_sessions
726 $table = new xmldb_table('question_sessions');
2a88f626 727 $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
62e76c67 728
729 /// Conditionally launch add field flagged
730 if (!$dbman->field_exists($table, $field)) {
731 $dbman->add_field($table, $field);
732 }
733
734 /// Main savepoint reached
735 upgrade_main_savepoint($result, 2008082700);
736 }
ff2f69bc 737
2c48aeab 738 if ($result && $oldversion < 2008082900) {
739
740 /// Changing precision of field parent_type on table mnet_rpc to (20)
741 $table = new xmldb_table('mnet_rpc');
2a88f626 742 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2c48aeab 743
744 /// Launch change of precision for field parent_type
745 $dbman->change_field_precision($table, $field);
746
747 /// Main savepoint reached
748 upgrade_main_savepoint($result, 2008082900);
749 }
750
ff7e7f92 751 if ($result && $oldversion < 2008090108) {
752 $repo = new object();
753 $repo->type = 'upload';
754 $repo->visible = 1;
755 $repo->sortorder = 1;
756 if (!$DB->record_exists('repository', array('type'=>'upload'))) {
757 $typeid = $DB->insert_record('repository', $repo);
758 }else{
759 $record = $DB->get_record('repository', array('type'=>'upload'));
760 $typeid = $record->id;
761 }
762 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
763 $instance = new object();
fb7cae3b 764 $instance->name = get_string('repositoryname', 'repository_upload');
ff7e7f92 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);
771 }
772 $repo->type = 'local';
773 $repo->visible = 1;
774 $repo->sortorder = 1;
775 if (!$DB->record_exists('repository', array('type'=>'local'))) {
776 $typeid = $DB->insert_record('repository', $repo);
777 }else{
778 $record = $DB->get_record('repository', array('type'=>'local'));
779 $typeid = $record->id;
780 }
781 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
782 $instance = new object();
fb7cae3b 783 $instance->name = get_string('repositoryname', 'repository_local');
ff7e7f92 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);
790 }
791
792 upgrade_main_savepoint($result, 2008090108);
793 }
794
d849880e 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') {
799 continue;
800 }
801 $pluginname = substr($name, 0, strlen($name) - 8);
802 if (!strpos($pluginname, '_')) {
803 // Skip things like backup_version that don't contain an extra _
804 continue;
805 }
806 if ($pluginname == 'enrol_ldap_version') {
807 // Special case - this is something different from a plugin version number.
808 continue;
809 }
6f49dd0f 810 if (!preg_match('/^\d{10}$/', $value)) {
d849880e 811 // Extra safety check, skip anything that does not look like a Moodle
812 // version number (10 digits).
813 continue;
814 }
815 $result = $result && set_config('version', $value, $pluginname);
816 $result = $result && unset_config($name);
817 }
818 upgrade_main_savepoint($result, 2008091000);
819 }
820
948c2860 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) {
824
825 /// Define field readonly to be added to repository_instances
826 $table = new xmldb_table('repository_instances');
2a88f626 827 $field = new xmldb_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
948c2860 828
829 /// Conditionally launch add field readonly
830 if (!$dbman->field_exists($table, $field)) {
831 $dbman->add_field($table, $field);
832 }
833
834 /// Main savepoint reached
835 upgrade_main_savepoint($result, 2008091611);
836 }
837
53b20fe3 838 if ($result && $oldversion < 2008092300) {
839 unset_config('editorspelling');
840 unset_config('editordictionary');
841 /// Main savepoint reached
842 upgrade_main_savepoint($result, 2008092300);
843 }
844
a7df430b 845 if ($result && $oldversion < 2008101000) {
846
847 /// Changing the default of field lang on table user to en_utf8
848 $table = new xmldb_table('user');
2a88f626 849 $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en_utf8', 'country');
a7df430b 850
851 /// Launch change of default for field lang
852 $dbman->change_field_default($table, $field);
853
854 /// Main savepoint reached
855 upgrade_main_savepoint($result, 2008101000);
856 }
857
5e7206a8 858 if ($result && $oldversion < 2008101300) {
859
d9e673c1 860 if (!get_config(NULL, 'statsruntimedays')) {
5e7206a8 861 set_config('statsruntimedays', '31');
862 }
863
864 /// Main savepoint reached
865 upgrade_main_savepoint($result, 2008101300);
866 }
53b20fe3 867
01a80f51 868 /// New table for storing which roles can be assigned in which contexts.
869 if ($result && $oldversion < 2008110601) {
870
871 /// Define table role_context_levels to be created
872 $table = new xmldb_table('role_context_levels');
873
874 /// Adding fields to table role_context_levels
2a88f626 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);
01a80f51 878
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'));
883
884 /// Conditionally launch create table for role_context_levels
885 if (!$dbman->table_exists($table)) {
886 $dbman->create_table($table);
887 }
888
889 /// Main savepoint reached
890 upgrade_main_savepoint($result, 2008110601);
891 }
892
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');
897
898 /// Defaults, should match moodle_install_roles.
899 $rolecontextlevels = array();
01a80f51 900 if (isset($roleids['coursecreator'])) {
901 $rolecontextlevels[$roleids['coursecreator']] = get_default_contextlevels('coursecreator');
902 }
903 if (isset($roleids['editingteacher'])) {
904 $rolecontextlevels[$roleids['editingteacher']] = get_default_contextlevels('editingteacher');
905 }
906 if (isset($roleids['teacher'])) {
907 $rolecontextlevels[$roleids['teacher']] = get_default_contextlevels('teacher');
908 }
909 if (isset($roleids['student'])) {
910 $rolecontextlevels[$roleids['student']] = get_default_contextlevels('student');
911 }
912 if (isset($roleids['guest'])) {
913 $rolecontextlevels[$roleids['guest']] = get_default_contextlevels('guest');
914 }
915 if (isset($roleids['user'])) {
916 $rolecontextlevels[$roleids['user']] = get_default_contextlevels('user');
917 }
656be893 918
01a80f51 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;
928 }
929 }
930
931 /// Put the data into the database.
932 foreach ($rolecontextlevels as $roleid => $contextlevels) {
933 set_role_contextlevels($roleid, $contextlevels);
934 }
935
936 /// Main savepoint reached
937 upgrade_main_savepoint($result, 2008110602);
938 }
939
9101efd3 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');
943
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);
948 }
949
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);
954 }
955
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);
960 }
961
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);
966 }
967
968 /// Main savepoint reached
969 upgrade_main_savepoint($result, 2008111200);
970 }
971
40c792c3 972/// Add a unique index to the role.name column.
973 if ($result && $oldversion < 2008111800) {
974
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'));
978
979 /// Conditionally launch add index name
980 if (!$dbman->index_exists($table, $index)) {
981 $dbman->add_index($table, $index);
982 }
983
984 /// Main savepoint reached
985 upgrade_main_savepoint($result, 2008111800);
986 }
987
988/// Add a unique index to the role.shortname column.
989 if ($result && $oldversion < 2008111801) {
990
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'));
994
995 /// Conditionally launch add index shortname
996 if (!$dbman->index_exists($table, $index)) {
997 $dbman->add_index($table, $index);
998 }
999
1000 /// Main savepoint reached
1001 upgrade_main_savepoint($result, 2008111801);
1002 }
1003
82bd6a5e 1004 if ($result && $oldversion < 2008120700) {
1005
1006 /// Changing precision of field shortname on table course_request to (100)
1007 $table = new xmldb_table('course_request');
2a88f626 1008 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
82bd6a5e 1009
da750a51 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);
1016 }
1017
82bd6a5e 1018 /// Launch change of precision for field shortname
1019 $dbman->change_field_precision($table, $field);
1020
da750a51 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);
1027 }
1028
82bd6a5e 1029 /// Main savepoint reached
1030 upgrade_main_savepoint($result, 2008120700);
1031 }
1032
82bd6a5e 1033 if ($result && $oldversion < 2008120801) {
1034
1035 /// Changing precision of field shortname on table mnet_enrol_course to (100)
1036 $table = new xmldb_table('mnet_enrol_course');
2a88f626 1037 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
82bd6a5e 1038
1039 /// Launch change of precision for field shortname
1040 $dbman->change_field_precision($table, $field);
1041
1042 /// Main savepoint reached
1043 upgrade_main_savepoint($result, 2008120801);
1044 }
1045
1046 if ($result && $oldversion < 2008121701) {
0953a4e7 1047
1048 /// Define field availablefrom to be added to course_modules
1049 $table = new xmldb_table('course_modules');
2a88f626 1050 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
0953a4e7 1051
1052 /// Conditionally launch add field availablefrom
1053 if (!$dbman->field_exists($table, $field)) {
1054 $dbman->add_field($table, $field);
1055 }
1056
1057 /// Define field availableuntil to be added to course_modules
2a88f626 1058 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
0953a4e7 1059
1060 /// Conditionally launch add field availableuntil
1061 if (!$dbman->field_exists($table, $field)) {
1062 $dbman->add_field($table, $field);
1063 }
82bd6a5e 1064
0953a4e7 1065 /// Define field showavailability to be added to course_modules
2a88f626 1066 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
0953a4e7 1067
1068 /// Conditionally launch add field showavailability
1069 if (!$dbman->field_exists($table, $field)) {
1070 $dbman->add_field($table, $field);
1071 }
82bd6a5e 1072
0953a4e7 1073 /// Define table course_modules_availability to be created
1074 $table = new xmldb_table('course_modules_availability');
1075
1076 /// Adding fields to table course_modules_availability
2a88f626 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);
0953a4e7 1084
1085 /// Adding keys to table course_modules_availability
1086 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
cbb17d54 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'));
0953a4e7 1090
1091 /// Conditionally launch create table for course_modules_availability
1092 if (!$dbman->table_exists($table)) {
1093 $dbman->create_table($table);
1094 }
1095
82bd6a5e 1096 /// Changes to modinfo mean we need to rebuild course cache
a9a0b93d 1097 require_once($CFG->dirroot . '/course/lib.php');
8b6a0968 1098 rebuild_course_cache(0, true);
9136a60c 1099
82bd6a5e 1100 /// For developer upgrades, turn on the conditional activities and completion
1101 /// features automatically (to gain more testing)
8b6a0968 1102//TODO: remove before 2.0 final!
1103 if (debugging('', DEBUG_DEVELOPER)) {
1104 set_config('enableavailability', 1);
1105 set_config('enablecompletion', 1);
9136a60c 1106 }
1107
9d510a2e 1108 /// Main savepoint reached
82bd6a5e 1109 upgrade_main_savepoint($result, 2008121701);
9d510a2e 1110 }
1111
656be893 1112 if ($result && $oldversion < 2009010500) {
1113 /// clean up config table a bit
1114 unset_config('session_error_counter');
1115
1116 /// Main savepoint reached
1117 upgrade_main_savepoint($result, 2009010500);
1118 }
1119
1120 if ($result && $oldversion < 2009010600) {
1121
1122 /// Define field originalquestion to be dropped from question_states
1123 $table = new xmldb_table('question_states');
1124 $field = new xmldb_field('originalquestion');
1125
1126 /// Conditionally launch drop field originalquestion
1127 if ($dbman->field_exists($table, $field)) {
1128 $dbman->drop_field($table, $field);
1129 }
1130
1131 /// Main savepoint reached
1132 upgrade_main_savepoint($result, 2009010600);
1133 }
1134
1135 if ($result && $oldversion < 2009010601) {
1136
1137 /// Changing precision of field ip on table log to (45)
1138 $table = new xmldb_table('log');
2a88f626 1139 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
656be893 1140
1141 /// Launch change of precision for field ip
1142 $dbman->change_field_precision($table, $field);
1143
1144 /// Main savepoint reached
1145 upgrade_main_savepoint($result, 2009010601);
1146 }
1147
1148 if ($result && $oldversion < 2009010602) {
1149
1150 /// Changing precision of field lastip on table user to (45)
1151 $table = new xmldb_table('user');
2a88f626 1152 $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
656be893 1153
1154 /// Launch change of precision for field lastip
1155 $dbman->change_field_precision($table, $field);
1156
1157 /// Main savepoint reached
1158 upgrade_main_savepoint($result, 2009010602);
1159 }
1160
1161 if ($result && $oldversion < 2009010603) {
1162
1163 /// Changing precision of field ip_address on table mnet_host to (45)
1164 $table = new xmldb_table('mnet_host');
2a88f626 1165 $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
656be893 1166
1167 /// Launch change of precision for field ip_address
1168 $dbman->change_field_precision($table, $field);
1169
1170 /// Main savepoint reached
1171 upgrade_main_savepoint($result, 2009010603);
1172 }
1173
1174 if ($result && $oldversion < 2009010604) {
1175
1176 /// Changing precision of field ip on table mnet_log to (45)
1177 $table = new xmldb_table('mnet_log');
2a88f626 1178 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
656be893 1179
1180 /// Launch change of precision for field ip
1181 $dbman->change_field_precision($table, $field);
1182
1183 /// Main savepoint reached
1184 upgrade_main_savepoint($result, 2009010604);
1185 }
1186
4413941f 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@$');
1193 } else {
1194 set_config('notifyloginfailures', '');
1195 }
1196
1197 /// Main savepoint reached
1198 upgrade_main_savepoint($result, 2009010800);
1199 }
1200
ab2eb65c 1201 if ($result && $oldversion < 2009011000) {
1202
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');
2a88f626 1206 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
ab2eb65c 1207
1208 /// Launch change of nullability for field configdata
1209 $dbman->change_field_notnull($table, $field);
1210
1211 /// Main savepoint reached
1212 upgrade_main_savepoint($result, 2009011000);
1213 }
1214
1caea91e 1215 if ($result && $oldversion < 2009011100) {
1216 /// Remove unused settings
1217 unset_config('zip');
1218 unset_config('unzip');
1219 unset_config('adminblocks_initialised');
1220
1221 /// Main savepoint reached
1222 upgrade_main_savepoint($result, 2009011100);
1223 }
1224
eb6a973c 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');
1230 }
1231
1232 /// Define table to be dropped
1233 $table = new xmldb_table('backup_config');
1234
1235 /// Launch drop table for old backup config
1236 $dbman->drop_table($table);
1237
1238 /// Main savepoint reached
1239 upgrade_main_savepoint($result, 2009011101);
1240 }
1241
301bf0b2 1242 if ($result && $oldversion < 2009011303) {
1243
1244 /// Define table config_log to be created
1245 $table = new xmldb_table('config_log');
1246
1247 /// Adding fields to table config_log
2a88f626 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);
301bf0b2 1255
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'));
1259
1260 /// Adding indexes to table config_log
1261 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1262
1263 /// Launch create table for config_log
1264 $dbman->create_table($table);
1265
1266 /// Main savepoint reached
1267 upgrade_main_savepoint($result, 2009011303);
1268 }
1269
b9fb7103 1270 if ($result && $oldversion < 2009011900) {
3f57bd45 1271
1272 /// Define table sessions2 to be dropped
1273 $table = new xmldb_table('sessions2');
1274
1275 /// Conditionally launch drop table for sessions
1276 if ($dbman->table_exists($table)) {
1277 $dbman->drop_table($table);
1278 }
1279
1280 /// Define table sessions to be dropped
1281 $table = new xmldb_table('sessions');
1282
1283 /// Conditionally launch drop table for sessions
1284 if ($dbman->table_exists($table)) {
1285 $dbman->drop_table($table);
1286 }
1287
1288 /// Define table sessions to be created
1289 $table = new xmldb_table('sessions');
1290
1291 /// Adding fields to table sessions
2a88f626 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);
3f57bd45 1301
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'));
1305
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'));
1311
1312 /// Launch create table for sessions
1313 $dbman->create_table($table);
1314
1315 /// Main savepoint reached
b9fb7103 1316 upgrade_main_savepoint($result, 2009011900);
3f57bd45 1317 }
1318
dc6f76b9 1319 if ($result && $oldversion < 2009012901) {
795a08ad 1320 // NOTE: this table may already exist, see beginning of this file ;-)
1f20942c 1321
1322 /// Define table upgrade_log to be created
1323 $table = new xmldb_table('upgrade_log');
1324
1325 /// Adding fields to table upgrade_log
2a88f626 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);
1f20942c 1335
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'));
1339
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'));
1343
1344 /// Conditionally launch create table for upgrade_log
1345 if (!$dbman->table_exists($table)) {
1346 $dbman->create_table($table);
1347 }
1348
1349 /// Main savepoint reached
dc6f76b9 1350 upgrade_main_savepoint($result, 2009012901);
1f20942c 1351 }
1caea91e 1352
75564228 1353 if ($result && $oldversion < 2009021800) {
eb2761f8 1354 // Converting format of grade conditions, if any exist, to percentages.
75564228 1355 $DB->execute("
1356UPDATE {course_modules_availability} SET grademin=(
1357 SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
eb2761f8 1358 /(gi.grademax-gi.grademin)
1359 FROM {grade_items} gi
75564228 1360 WHERE gi.id={course_modules_availability}.gradeitemid)
1361WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1362 $DB->execute("
1363UPDATE {course_modules_availability} SET grademax=(
1364 SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
eb2761f8 1365 /(gi.grademax-gi.grademin)
1366 FROM {grade_items} gi
75564228 1367 WHERE gi.id={course_modules_availability}.gradeitemid)
1368WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1369
1370 /// Main savepoint reached
1371 upgrade_main_savepoint($result, 2009021800);
1372 }
bc7ec91a 1373 if ($result && $oldversion < 2009021801) {
1374 /// Define field backuptype to be added to backup_log
382123a0 1375 $table = new xmldb_table('backup_log');
2a88f626 1376 $field = new xmldb_field('backuptype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'info');
bc7ec91a 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'");
1381 }
75564228 1382
bc7ec91a 1383 /// Main savepoint reached
1384 upgrade_main_savepoint($result, 2009021801);
1385 }
af52ecee 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');
1400
1401 upgrade_main_savepoint($result, 2009030300);
1402 }
a75c78d3 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);
1409 }
af52ecee 1410
82701e24 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');
1416
1417 /// Adding fields to table role_allow_switch
2a88f626 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);
82701e24 1421
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'));
1426
1427 /// Adding indexes to table role_allow_switch
1428 $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1429
1430 /// Conditionally launch create table for role_allow_switch
1431 if (!$dbman->table_exists($table)) {
1432 $dbman->create_table($table);
1433 }
1434
1435 /// Main savepoint reached
1436 upgrade_main_savepoint($result, 2009032000);
1437 }
0fc9c009 1438
82701e24 1439 if ($result && $oldversion < 2009032001) {
1440 /// Copy from role_allow_assign into the new table.
dc740f97 1441 $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1442 SELECT roleid, allowassign FROM {role_allow_assign}');
82701e24 1443
1444 /// Unset the config variable used in 1.9.
1445 unset_config('allowuserswitchrolestheycantassign');
1446
1447 /// Main savepoint reached
1448 upgrade_main_savepoint($result, 2009032001);
1449 }
c94985ef 1450
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);
1456 }
1457
0fc9c009 1458 if ($result && $oldversion < 2009040300) {
1459
1460 /// Define table filter_active to be created
1461 $table = new xmldb_table('filter_active');
1462
1463 /// Adding fields to table filter_active
2a88f626 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');
0fc9c009 1469
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'));
1473
1474 /// Adding indexes to table filter_active
1475 $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1476
1477 /// Conditionally launch create table for filter_active
1478 if (!$dbman->table_exists($table)) {
1479 $dbman->create_table($table);
1480 }
1481
1482 /// Main savepoint reached
1483 upgrade_main_savepoint($result, 2009040300);
1484 }
1485
1486 if ($result && $oldversion < 2009040301) {
1487
1488 /// Define table filter_config to be created
1489 $table = new xmldb_table('filter_config');
1490
1491 /// Adding fields to table filter_config
2a88f626 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);
0fc9c009 1497
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'));
1501
1502 /// Adding indexes to table filter_config
1503 $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1504
1505 /// Conditionally launch create table for filter_config
1506 if (!$dbman->table_exists($table)) {
1507 $dbman->create_table($table);
1508 }
1509
1510 /// Main savepoint reached
1511 upgrade_main_savepoint($result, 2009040301);
1512 }
1513
c07e6d8d 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();
1519 } else {
1520 $activefilters = explode(',', $CFG->textfilters);
1521 }
1522 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1523 $sortorder = 1;
1524 foreach ($activefilters as $filter) {
1525 filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1526 $sortorder += 1;
1527 unset($disabledfilters[$filter]);
1528 }
1529 foreach ($disabledfilters as $filter => $notused) {
1530 filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1531 $sortorder += 1;
1532 }
1533
1534 /// Main savepoint reached
1535 upgrade_main_savepoint($result, 2009040302);
1536 }
1537
bbb45905 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);
1543 } else {
1544 set_config('stringfilters', '');
1545 }
1546 }
1547
1548 set_config('filterall', !empty($CFG->stringfilters));
1549 unset_config('textfilters');
1550
1551 /// Main savepoint reached
1552 upgrade_main_savepoint($result, 2009040600);
1553 }
1554
e7c6bf0e 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');
6861b6fe 1560 require_once($CFG->dirroot . '/course/lib.php');
0f9534c1 1561 rebuild_course_cache(0, true);
e7c6bf0e 1562
1563 /// Main savepoint reached
1564 upgrade_main_savepoint($result, 2009041700);
1565 }
1566
6bdf4c99 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
1572 FROM {user} u
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
1577 }
1578 }
1579 /// Main savepoint reached
1580 upgrade_main_savepoint($result, 2009042600);
1581 }
1582
e37cd84a 1583 /// Dropping all enums/check contraints from core. MDL-18577
1584 if ($result && $oldversion < 2009042700) {
1585
1586 /// Changing list of values (enum) of field stattype on table stats_daily to none
1587 $table = new xmldb_table('stats_daily');
2a88f626 1588 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
e37cd84a 1589
1590 /// Launch change of list of values for field stattype
2a88f626 1591 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1592
1593 /// Changing list of values (enum) of field stattype on table stats_weekly to none
1594 $table = new xmldb_table('stats_weekly');
2a88f626 1595 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
e37cd84a 1596
1597 /// Launch change of list of values for field stattype
2a88f626 1598 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1599
1600 /// Changing list of values (enum) of field stattype on table stats_monthly to none
1601 $table = new xmldb_table('stats_monthly');
2a88f626 1602 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
e37cd84a 1603
1604 /// Launch change of list of values for field stattype
2a88f626 1605 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1606
1607 /// Changing list of values (enum) of field publishstate on table post to none
1608 $table = new xmldb_table('post');
2a88f626 1609 $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
e37cd84a 1610
1611 /// Launch change of list of values for field publishstate
2a88f626 1612 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1613
1614 /// Main savepoint reached
1615 upgrade_main_savepoint($result, 2009042700);
1616 }
1617
c045e45a 1618 if ($result && $oldversion < 2009043000) {
1619 unset_config('grade_report_showgroups');
1620 upgrade_main_savepoint($result, 2009043000);
1621 }
1622
1623 if ($result && $oldversion < 2009050600) {
e88462a0 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));
1626
1627 /// Main savepoint reached
c045e45a 1628 upgrade_main_savepoint($result, 2009050600);
e88462a0 1629 }
1630
c045e45a 1631 if ($result && $oldversion < 2009050601) {
66b10689 1632
3aceb097 1633 /// Define table block_instance to be renamed to block_instances
66b10689 1634 $table = new xmldb_table('block_instance');
1635
1636 /// Launch rename table for block_instance
3aceb097 1637 $dbman->rename_table($table, 'block_instances');
66b10689 1638
1639 /// Main savepoint reached
c045e45a 1640 upgrade_main_savepoint($result, 2009050601);
66b10689 1641 }
1642
c045e45a 1643 if ($result && $oldversion < 2009050602) {
66b10689 1644
1645 /// Define table block_instance to be renamed to block_instance_old
1646 $table = new xmldb_table('block_pinned');
1647
1648 /// Launch rename table for block_instance
1649 $dbman->rename_table($table, 'block_pinned_old');
1650
1651 /// Main savepoint reached
c045e45a 1652 upgrade_main_savepoint($result, 2009050602);
66b10689 1653 }
1654
c045e45a 1655 if ($result && $oldversion < 2009050603) {
66b10689 1656
1657 /// Define table block_instance_old to be created
1658 $table = new xmldb_table('block_instance_old');
1659
1660 /// Adding fields to table block_instance_old
2b212777 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);
66b10689 1670
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'));
1674
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'));
1678
1679 /// Conditionally launch create table for block_instance_old
1680 if (!$dbman->table_exists($table)) {
1681 $dbman->create_table($table);
1682 }
1683
1684 /// Main savepoint reached
c045e45a 1685 upgrade_main_savepoint($result, 2009050603);
66b10689 1686 }
1687
c045e45a 1688 if ($result && $oldversion < 2009050604) {
66b10689 1689 /// Copy current blocks data from block_instances to block_instance_old
35550fd6 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');
66b10689 1692
c045e45a 1693 upgrade_main_savepoint($result, 2009050604);
66b10689 1694 }
1695
c045e45a 1696 if ($result && $oldversion < 2009050605) {
66b10689 1697
1698 /// Define field multiple to be dropped from block
1699 $table = new xmldb_table('block');
1700 $field = new xmldb_field('multiple');
1701
1702 /// Conditionally launch drop field multiple
1703 if ($dbman->field_exists($table, $field)) {
1704 $dbman->drop_field($table, $field);
1705 }
1706
1707 /// Main savepoint reached
c045e45a 1708 upgrade_main_savepoint($result, 2009050605);
66b10689 1709 }
1710
c045e45a 1711 if ($result && $oldversion < 2009050606) {
66b10689 1712 $table = new xmldb_table('block_instances');
1713
1714 /// Rename field weight on table block_instances to defaultweight
2b212777 1715 $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'position');
66b10689 1716 $dbman->rename_field($table, $field, 'defaultweight');
1717
1718 /// Rename field position on table block_instances to defaultregion
2b212777 1719 $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
66b10689 1720 $dbman->rename_field($table, $field, 'defaultregion');
1721
1722 /// Main savepoint reached
c045e45a 1723 upgrade_main_savepoint($result, 2009050606);
66b10689 1724 }
1725
c045e45a 1726 if ($result && $oldversion < 2009050607) {
66b10689 1727 /// Changing precision of field defaultregion on table block_instances to (16)
a26146d5 1728 $table = new xmldb_table('block_instances');
1729 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
66b10689 1730
1731 /// Launch change of precision for field defaultregion
1732 $dbman->change_field_precision($table, $field);
1733
1734 /// Main savepoint reached
a26146d5 1735 upgrade_main_savepoint($result, 2009050607);
66b10689 1736 }
1737
c045e45a 1738 if ($result && $oldversion < 2009050608) {
66b10689 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.
1745
1746 /// Main savepoint reached
c045e45a 1747 upgrade_main_savepoint($result, 2009050608);
66b10689 1748 }
1749
c045e45a 1750 if ($result && $oldversion < 2009050609) {
66b10689 1751
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'));
1755
1756 /// Launch add key blockname
1757 $dbman->add_key($table, $key);
1758
1759 /// Main savepoint reached
c045e45a 1760 upgrade_main_savepoint($result, 2009050609);
66b10689 1761 }
1762
c045e45a 1763 if ($result && $oldversion < 2009050610) {
66b10689 1764 $table = new xmldb_table('block_instances');
1765
1766 /// Define field blockname to be added to block_instances
2b212777 1767 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
66b10689 1768 if (!$dbman->field_exists($table, $field)) {
1769 $dbman->add_field($table, $field);
1770 }
1771
1772 /// Define field contextid to be added to block_instances
2b212777 1773 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
66b10689 1774 if (!$dbman->field_exists($table, $field)) {
1775 $dbman->add_field($table, $field);
1776 }
1777
1778 /// Define field showinsubcontexts to be added to block_instances
2b212777 1779 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
66b10689 1780 if (!$dbman->field_exists($table, $field)) {
1781 $dbman->add_field($table, $field);
1782 }
1783
1784 /// Define field subpagepattern to be added to block_instances
2b212777 1785 $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
66b10689 1786 if (!$dbman->field_exists($table, $field)) {
1787 $dbman->add_field($table, $field);
1788 }
1789
1790 /// Main savepoint reached
c045e45a 1791 upgrade_main_savepoint($result, 2009050610);
66b10689 1792 }
1793
c045e45a 1794 if ($result && $oldversion < 2009050611) {
66b10689 1795 $table = new xmldb_table('block_instances');
1796
1797 /// Fill in blockname from blockid
1798 $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1799
1800 /// Set showinsubcontexts = 0 for all rows.
1801 $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1802
1803 /// Main savepoint reached
c045e45a 1804 upgrade_main_savepoint($result, 2009050611);
66b10689 1805 }
1806
c045e45a 1807 if ($result && $oldversion < 2009050612) {
66b10689 1808
1809 /// Rename field pagetype on table block_instances to pagetypepattern
1810 $table = new xmldb_table('block_instances');
2b212777 1811 $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
66b10689 1812
1813 /// Launch rename field pagetype
1814 $dbman->rename_field($table, $field, 'pagetypepattern');
1815
1816 /// Main savepoint reached
c045e45a 1817 upgrade_main_savepoint($result, 2009050612);
66b10689 1818 }
1819
c045e45a 1820 if ($result && $oldversion < 2009050613) {
66b10689 1821 /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1822
1823 /// site-index
1824 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
eb2761f8 1825 $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1826 pagetypepattern = 'site-index',
1827 subpagepattern = NULL
1828 WHERE pagetypepattern = 'site-index'");
66b10689 1829
1830 /// course-view
eb2761f8 1831 $DB->execute("UPDATE {block_instances} SET
1832 contextid = (
1833 SELECT {context}.id
1834 FROM {context}
1835 JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1836 WHERE {course}.id = pageid
1837 ),
1838 pagetypepattern = 'course-view-*',
1839 subpagepattern = NULL
1840 WHERE pagetypepattern = 'course-view'");
66b10689 1841
1842 /// admin
1843 $syscontext = get_context_instance(CONTEXT_SYSTEM);
eb2761f8 1844 $DB->execute("UPDATE {block_instances} SET
1845 contextid = " . $syscontext->id . ",
1846 pagetypepattern = 'admin-*',
1847 subpagepattern = NULL
1848 WHERE pagetypepattern = 'admin'");
66b10689 1849
1850 /// my-index
eb2761f8 1851 $DB->execute("UPDATE {block_instances} SET
1852 contextid = (
1853 SELECT {context}.id
1854 FROM {context}
1855 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1856 WHERE {user}.id = pageid
1857 ),
1858 pagetypepattern = 'my-index',
1859 subpagepattern = NULL
1860 WHERE pagetypepattern = 'my-index'");
66b10689 1861
1862 /// tag-index
eb2761f8 1863 $DB->execute("UPDATE {block_instances} SET
1864 contextid = " . $syscontext->id . ",
1865 pagetypepattern = 'tag-index',
1866 subpagepattern = pageid
1867 WHERE pagetypepattern = 'tag-index'");
66b10689 1868
1869 /// blog-view
eb2761f8 1870 $DB->execute("UPDATE {block_instances} SET
1871 contextid = (
1872 SELECT {context}.id
1873 FROM {context}
1874 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1875 WHERE {user}.id = pageid
1876 ),
1877 pagetypepattern = 'blog-index',
1878 subpagepattern = NULL
1879 WHERE pagetypepattern = 'blog-view'");
66b10689 1880
1881 /// mod-xxx-view
1882 $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1883 foreach ($moduleswithblocks as $modname) {
1884 if (!$dbman->table_exists($modname)) {
1885 continue;
1886 }
eb2761f8 1887 $DB->execute("UPDATE {block_instances} SET
1888 contextid = (
1889 SELECT {context}.id
1890 FROM {context}
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
1895 ),
1896 pagetypepattern = 'blog-index',
1897 subpagepattern = NULL
1898 WHERE pagetypepattern = 'blog-view'");
66b10689 1899 }
1900
1901 /// Main savepoint reached
c045e45a 1902 upgrade_main_savepoint($result, 2009050613);
66b10689 1903 }
1904
c045e45a 1905 if ($result && $oldversion < 2009050614) {
66b10689 1906 /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
bf659cf0 1907 $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
66b10689 1908
1909 /// Main savepoint reached
c045e45a 1910 upgrade_main_savepoint($result, 2009050614);
66b10689 1911 }
1912
c045e45a 1913 if ($result && $oldversion < 2009050615) {
66b10689 1914 $table = new xmldb_table('block_instances');
1915
1916 /// Changing nullability of field blockname on table block_instances to not null
2b212777 1917 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
66b10689 1918 $dbman->change_field_notnull($table, $field);
1919
1920 /// Changing nullability of field contextid on table block_instances to not null
2b212777 1921 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
66b10689 1922 $dbman->change_field_notnull($table, $field);
1923
1924 /// Changing nullability of field showinsubcontexts on table block_instances to not null
2b212777 1925 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
66b10689 1926 $dbman->change_field_notnull($table, $field);
1927
1928 /// Main savepoint reached
c045e45a 1929 upgrade_main_savepoint($result, 2009050615);
66b10689 1930 }
1931
c045e45a 1932 if ($result && $oldversion < 2009050616) {
66b10689 1933 /// Add exiting sticky blocks.
1934 $blocks = $DB->get_records('block');
1935 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1936 $newregions = array(
1937 'l' => 'side-pre',
1938 'r' => 'side-post',
1939 'c' => 'course-view-top',
1940 );
1941 $stickyblocks = $DB->get_recordset('block_pinned_old');
1942 foreach ($stickyblocks as $stickyblock) {
93d3a250 1943 $newblock = new object();
35fb85a7 1944 $newblock->blockname = $blocks[$stickyblock->blockid]->name;
66b10689 1945 $newblock->contextid = $syscontext->id;
1946 $newblock->showinsubcontexts = 1;
1947 switch ($stickyblock->pagetype) {
1948 case 'course-view':
1949 $newblock->pagetypepattern = 'course-view-*';
1950 break;
1951 default:
1952 $newblock->pagetypepattern = $stickyblock->pagetype;
1953 }
1954 $newblock->defaultregion = $newregions[$stickyblock->position];
1955 $newblock->defaultweight = $stickyblock->weight;
1956 $newblock->configdata = $stickyblock->configdata;
01de485d 1957 $newblock->visible = 1;
66b10689 1958 $DB->insert_record('block_instances', $newblock);
1959 }
1960
1961 /// Main savepoint reached
c045e45a 1962 upgrade_main_savepoint($result, 2009050616);
66b10689 1963 }
1964
c045e45a 1965 if ($result && $oldversion < 2009050617) {
66b10689 1966
1967 /// Define table block_positions to be created
1968 $table = new xmldb_table('block_positions');
1969
1970 /// Adding fields to table block_positions
2b212777 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);
66b10689 1979
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'));
1984
1985 /// Adding indexes to table block_positions
1986 $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
1987
1988 /// Conditionally launch create table for block_positions
1989 if (!$dbman->table_exists($table)) {
1990 $dbman->create_table($table);
1991 }
1992
1993 /// Main savepoint reached
c045e45a 1994 upgrade_main_savepoint($result, 2009050617);
66b10689 1995 }
1996
c045e45a 1997 if ($result && $oldversion < 2009050618) {
66b10689 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') . "
2003 FROM {course}
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-*'");
2011
2012 /// Main savepoint reached
c045e45a 2013 upgrade_main_savepoint($result, 2009050618);
66b10689 2014 }
2015
c045e45a 2016 if ($result && $oldversion < 2009050619) {
66b10689 2017 $table = new xmldb_table('block_instances');
2018
2019 /// Define field blockid to be dropped from block_instances
2020 $field = new xmldb_field('blockid');
2021 if ($dbman->field_exists($table, $field)) {
4276a19a 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);
2027 }
66b10689 2028 $dbman->drop_field($table, $field);
2029 }
2030
2031 /// Define field pageid to be dropped from block_instances
2032 $field = new xmldb_field('pageid');
2033 if ($dbman->field_exists($table, $field)) {
4276a19a 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);
2039 }
66b10689 2040 $dbman->drop_field($table, $field);
2041 }
2042
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);
2047 }
2048
2049 /// Main savepoint reached
c045e45a 2050 upgrade_main_savepoint($result, 2009050619);
35079f53 2051 }
2052
ecdf5d17 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
aa9a6867 2056 echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
ecdf5d17 2057 upgrade_fix_incorrect_mnethostids();
2058
2059 /// Main savepoint reached
2060 upgrade_main_savepoint($result, 2009051200);
2061 }
2062
a5747cf8 2063
2064 if ($result && $oldversion < 2009051700) {
2065 /// migrate editor settings
2066 if (empty($CFG->htmleditor)) {
2067 set_config('texteditors', 'textarea');
2068 } else {
2069 set_config('texteditors', 'tinymce,textarea');
2070 }
2071
2072 unset_config('htmleditor');
2073 unset_config('defaulthtmleditor');
2074
2075 /// Main savepoint reached
2076 upgrade_main_savepoint($result, 2009051700);
2077 }
2078
1aa01caf 2079 if ($result && $oldversion < 2009060200) {
2080 /// Define table files_cleanup to be dropped - not needed
2081 $table = new xmldb_table('files_cleanup');
2082
2083 /// Conditionally launch drop table for files_cleanup
2084 if ($dbman->table_exists($table)) {
2085 $dbman->drop_table($table);
2086 }
2087
2088 /// Main savepoint reached
2089 upgrade_main_savepoint($result, 2009060200);
2090 }
2091
b4154c2d 2092 if ($result && $oldversion < 2009061300) {
2093 //TODO: copy this to the very beginning of this upgrade script so that we may log upgrade queries
2094
2095 /// Define table log_queries to be created
2096 $table = new xmldb_table('log_queries');
2097
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);
2108
2109 /// Adding keys to table log_queries
2110 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2111
2112 /// Conditionally launch create table for log_queries
2113 if (!$dbman->table_exists($table)) {
2114 $dbman->create_table($table);
2115 }
2116
2117 /// Main savepoint reached
2118 upgrade_main_savepoint($result, 2009061300);
2119 }
2120
52902cea 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');
2127
2128 /// Launch change of precision for field defaultregion
2129 $dbman->change_field_precision($table, $field);
2130
2131 /// Main savepoint reached
2132 upgrade_main_savepoint($result, 2009061600);
2133 }
2134
17da2e6f 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) {
ce11763a 2139 $result = $result && unset_config($config->name, $config->plugin); /// unset old config
17da2e6f 2140 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
ce11763a 2141 $result = $result && set_config($config->name, $config->value, $config->plugin); /// set new config
17da2e6f 2142 }
2143 }
2144 unset($configs);
2145 upgrade_main_savepoint($result, 2009061702);
2146 }
2147
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) {
ce11763a 2152 $result = $result && unset_config($config->name, $config->plugin); /// unset old config
17da2e6f 2153 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
ce11763a 2154 $result = $result && set_config($config->name, $config->value, $config->plugin); /// set new config
17da2e6f 2155 }
2156 }
2157 unset($configs);
2158 upgrade_main_savepoint($result, 2009061703);
2159 }
2160
2161 if ($result && $oldversion < 2009061704) {
13a0d3d3 2162 // change component string in capability records to new "_" format
17da2e6f 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);
2167 }
2168 }
2169 unset($caps);
2170 upgrade_main_savepoint($result, 2009061704);
2171 }
2172
2173 if ($result && $oldversion < 2009061705) {
13a0d3d3 2174 // change component string in events_handlers records to new "_" format
17da2e6f 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);
2179 }
2180 }
2181 unset($handlers);
2182 upgrade_main_savepoint($result, 2009061705);
2183 }
2184
2185 if ($result && $oldversion < 2009061706) {
13a0d3d3 2186 // change component string in message_providers records to new "_" format
17da2e6f 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);
2191 }
2192 }
2193 unset($caps);
2194 upgrade_main_savepoint($result, 2009061706);
2195 }
13a0d3d3 2196
adf8f3f9 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
c1f58efe 2200 if ($quiz = get_config('quiz')) {
2201 foreach ($quiz as $name=>$value) {
2202 if (strpos($name, 'fix_') !== 0) {
2203 continue;
2204 }
2205 $newname = substr($name,4).'_adv';
2206 set_config($newname, $value, 'quiz');
2207 unset_config($name, 'quiz');
adf8f3f9 2208 }
13a0d3d3 2209 }
adf8f3f9 2210 upgrade_main_savepoint($result, 2009063000);
2211 }
13a0d3d3 2212
13a0d3d3 2213 if ($result && $oldversion < 2009071000) {
2214
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');
2218
2219 /// Launch rename field parentcontextid
2220 $dbman->rename_field($table, $field, 'parentcontextid');
2221
2222 /// Main savepoint reached
2223 upgrade_main_savepoint($result, 2009071000);
2224 }
2225
e92c286c 2226 if ($result && $oldversion < 2009071300) {
2227
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'
2234 FROM {context} ctx
2235 WHERE bi.id = ctx.instanceid AND ctx.contextlevel=" . CONTEXT_BLOCK . ")";
2236 $DB->execute($sql);
2237
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();
2241
2242 /// Main savepoint reached
2243 upgrade_main_savepoint($result, 2009071300);
2244 }
1918f7e0 2245
cae83708 2246 if ($result && $oldversion < 2009071600) {
2247
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');
2251
2252 /// Conditionally launch add field summaryformat
2253 if (!$dbman->field_exists($table, $field)) {
2254 $dbman->add_field($table, $field);
2255 }
2256
2257 /// Main savepoint reached
2258 upgrade_main_savepoint($result, 2009071600);
2259 }
e92c286c 2260
1bcb7eb5 2261 if ($result && $oldversion < 2009072400) {
2262
2263 /// Define table comments to be created
2264 $table = new xmldb_table('comments');
2265
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);
2275
2276 /// Adding keys to table comments
2277 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2278
2279 /// Conditionally launch create table for comments
2280 if (!$dbman->table_exists($table)) {
2281 $dbman->create_table($table);
2282 }
0a127169 2283
1bcb7eb5 2284 /// Main savepoint reached
2285 upgrade_main_savepoint($result, 2009072400);
2286 }
2287
7d2a0492 2288 /**
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
2301 */
2302 if ($result && $oldversion < 2009082800) {
2303
2304 echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2305
2306 // Get the system context so we can set the block instances to it
2307 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2308
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 = '';
2329
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');
1918f7e0 2338
7d2a0492 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;
2345
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();
2351 }
2352 }
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();
2361 }
2362 }
2363 }
2364
2365 $result = $result && upgrade_cleanup_unwanted_block_contexts($contextids);
2366
2367 $instanceidstring = join(',',$instanceids);
2368 $outcome1 = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
1918f7e0 2369
7d2a0492 2370 unset($allblockinstances);
2371 unset($contextids);
2372 unset($instanceids);
2373 unset($instanceidstring);
1918f7e0 2374
7d2a0492 2375 // Now remove the actual block instance
2376 $result = $result && $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
2377 unset($outmodedblocksstring);
2378
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);
2385 }
2386 unset($newblockinstances);
1918f7e0 2387
7d2a0492 2388 upgrade_main_savepoint($result, 2009082800);
2389 // The end of the navigation upgrade
2390 }
2391
b89e4ad8 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';
2401
2402 $result = $result && $DB->insert_record('log_display', $log_action);
2403 }
2404 upgrade_main_savepoint($result, 2009090800);
2405 }
4313fa89 2406
88af54fa 2407 if ($result && $oldversion < 2009100601) {
5d820ca2 2408 // drop all previous tables defined during the dev phase
5b9a70a1 2409 $dropold = array('external_services_users', 'external_services_functions', 'external_services', 'external_functions');
5d820ca2 2410 foreach ($dropold as $tablename) {
2411 $table = new xmldb_table($tablename);
2412 if ($dbman->table_exists($table)) {
2413 $dbman->drop_table($table);
2414 }
4313fa89 2415 }
88af54fa 2416 upgrade_main_savepoint($result, 2009100601);
5d820ca2 2417 }
4313fa89 2418
88af54fa 2419 if ($result && $oldversion < 2009100602) {
4313fa89 2420 /// Define table external_functions to be created
2421 $table = new xmldb_table('external_functions');
2422
2423 /// Adding fields to table external_functions
2424 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
5d820ca2 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);
4313fa89 2429 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
4313fa89 2430
2431 /// Adding keys to table external_functions
2432 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2433
5d820ca2 2434 /// Adding indexes to table external_functions
2435 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2436
2437 /// Launch create table for external_functions
2438 $dbman->create_table($table);
4313fa89 2439
5d820ca2 2440 /// Main savepoint reached
88af54fa 2441 upgrade_main_savepoint($result, 2009100602);
5d820ca2 2442 }
4313fa89 2443
88af54fa 2444 if ($result && $oldversion < 2009100603) {
4313fa89 2445 /// Define table external_services to be created
2446 $table = new xmldb_table('external_services');
2447
2448 /// Adding fields to table external_services
2449 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
5d820ca2 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);
4313fa89 2455
2456 /// Adding keys to table external_services
2457 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2458
5b9a70a1 2459 /// Adding indexes to table external_services
2460 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2461
5d820ca2 2462 /// Launch create table for external_services
2463 $dbman->create_table($table);
4313fa89 2464
5d820ca2 2465 /// Main savepoint reached
88af54fa 2466 upgrade_main_savepoint($result, 2009100603);
5d820ca2 2467 }
4313fa89 2468
88af54fa 2469 if ($result && $oldversion < 2009100604) {
4313fa89 2470 /// Define table external_services_functions to be created
2471 $table = new xmldb_table('external_services_functions');
2472
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);
5d820ca2 2476 $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
4313fa89 2477
2478 /// Adding keys to table external_services_functions
2479 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
5d820ca2 2480 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
4313fa89 2481
5d820ca2 2482 /// Launch create table for external_services_functions
2483 $dbman->create_table($table);
2484
2485 /// Main savepoint reached
88af54fa 2486 upgrade_main_savepoint($result, 2009100604);
5d820ca2 2487 }
2488
88af54fa 2489 if ($result && $oldversion < 2009100605) {
5d820ca2 2490 /// Define table external_services_users to be created
2491 $table = new xmldb_table('external_services_users');
2492
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);
88af54fa 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);
5d820ca2 2500
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'));
2505
2506 /// Launch create table for external_services_users
2507 $dbman->create_table($table);
4313fa89 2508
2509 /// Main savepoint reached
88af54fa 2510 upgrade_main_savepoint($result, 2009100605);
4313fa89 2511 }
2512
1942103f
PS
2513 if ($result && $oldversion < 2009102600) {
2514
2515 /// Define table external_tokens to be created
2516 $table = new xmldb_table('external_tokens');
2517
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);
2530
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'));
2536
2537 /// Launch create table for external_tokens
2538 $dbman->create_table($table);
2539
2540 /// Main savepoint reached
2541 upgrade_main_savepoint($result, 2009102600);
2542 }
2543
cc82aef6
NC
2544 if ($result && $oldversion < 2009103000) {
2545
2546 /// Define table blog_association to be created
2547 $table = new xmldb_table('blog_association');
2548
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);
2553
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'));
2558
2559 /// Conditionally launch create table for blog_association
2560 if (!$dbman->table_exists($table)) {
2561 $dbman->create_table($table);
2562 }
2563
2564/// Define table blog_external to be created
2565 $table = new xmldb_table('blog_external');
2566
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');
2577
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'));
2581
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);
2586 }
2587 $dbman->create_table($table);
2588
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'));
2592
2593 // email admins about the need to upgrade their system using the admin/bloglevelupgrade.php script
2594 $admins = get_admins();
2595 $site = get_site();
2596
2597 $a = new StdClass;
2598 $a->sitename = $site->fullname;
2599 $a->fixurl = "$CFG->wwwroot/$CFG->admin/bloglevelupgrade.php";
2600
c01678b1
PS
2601 $subject = get_string('bloglevelupgrade', 'admin');
2602 $description = get_string('bloglevelupgradedescription', 'admin', $a);
2603
2604 // can not use messaging here because it is not configured yet!
2605 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
cc82aef6
NC
2606 }
2607 /// Main savepoint reached
2608 upgrade_main_savepoint($result, 2009103000);
2609 }
117bd748 2610
8bdc9cac
SH
2611 if ($result && $oldversion < 2009110400) {
2612
2613 // An array used to store the table name and keys of summary and trust fields
2614 // to be added
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');
2626
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);
cb8a83bb 2640 $DB->execute('UPDATE {'.$tablestr.'} SET '.$fieldstr.'=? WHERE ('.$textfield.' LIKE ? OR '.$textfield.' LIKE ?) AND '.$fieldstr.'=?', $params);
8bdc9cac
SH
2641 }
2642 }
2643 }
2644 }
2645
2646 unset($extendtables);
2647
2648 upgrade_main_savepoint($result, 2009110400);
2649 }
2650
975341d1 2651 if ($result && $oldversion < 2009110605) {
2652
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');
2656
2657 /// Conditionally launch add field timecreated
2658 if (!$dbman->field_exists($table, $field)) {
2659 $dbman->add_field($table, $field);
2660 }
2661
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');
2665
2666 /// Conditionally launch add field timemodified
2667 if (!$dbman->field_exists($table, $field)) {
2668 $dbman->add_field($table, $field);
2669 }
2670
2671 /// Main savepoint reached
2672 upgrade_main_savepoint($result, 2009110605);
2673 }
2674
e1b9ac3e
PL
2675 if ($result && $oldversion < 2009111600) {
2676
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');
2680
2681 /// Conditionally launch add field instance
2682 if (!$dbman->field_exists($table, $field)) {
2683 $dbman->add_field($table, $field);
2684 }
2685
2686 $key = new xmldb_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
2687
2688 /// Launch add key instancefk
2689 $dbman->add_key($table, $key);
2690
2691 /// Main savepoint reached
2692 upgrade_main_savepoint($result, 2009111600);
2693 }
2694
45645bce
PL
2695 if ($result && $oldversion < 2009111700) {
2696
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');
2700
2701 /// Conditionally launch add field tempdataid
2702 if (!$dbman->field_exists($table, $field)) {
2703 $dbman->add_field($table, $field);
2704 }
2705
2706 /// Main savepoint reached
2707 upgrade_main_savepoint($result, 2009111700);
2708 }
2709
eeb7ccf0
PL
2710 if ($result && $oldversion < 2009111701) {
2711
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');
2715
2716 /// Conditionally launch add field returnurl
2717 if (!$dbman->field_exists($table, $field)) {
2718 $dbman->add_field($table, $field);
2719 }
2720
2721 /// Main savepoint reached
2722 upgrade_main_savepoint($result, 2009111701);
2723 }
2724
2725 if ($result && $oldversion < 2009111702) {
2726
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');
2730
2731 /// Conditionally launch add field continueurl
2732 if (!$dbman->field_exists($table, $field)) {
2733 $dbman->add_field($table, $field);
2734 }
2735
2736 /// Main savepoint reached
2737 upgrade_main_savepoint($result, 2009111702);
2738 }
2739
57e6ce5e
PS
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);
2745 }
2746 upgrade_main_savepoint($result, 2009112400);
2747 }
e1b9ac3e 2748
5ae9f539 2749 if ($result && $oldversion < 2010010601) {
2750
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');
2754
2755 /// Conditionally launch add field creatorid
2756 if (!$dbman->field_exists($table, $field)) {
2757 $dbman->add_field($table, $field);
2758 }
2759
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'));
2763
2764 /// Launch add key creatorid
2765 $dbman->add_key($table, $key);
2766
2767 /// Main savepoint reached
2768 upgrade_main_savepoint($result, 2010010601);
2769 }
2770
c95002a2 2771 if ($result && $oldversion < 2010011200) {
a25bb902
AD
2772 $table = new xmldb_table('grade_categories');
2773 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
2774
2775 if (!$dbman->field_exists($table, $field)) {
2776 $dbman->add_field($table, $field);
2777 }
2778
c95002a2 2779 upgrade_main_savepoint($result, 2010011200);
a25bb902
AD
2780 }
2781
de260e0f 2782
7062a798
PL
2783 if ($result && $oldversion < 2010012500) {
2784 upgrade_fix_incorrect_mnethostids();
2785 upgrade_main_savepoint($result, 2010012500);
2786 }
de260e0f
PL
2787
2788 if ($result && $oldversion < 2010012600) {
2789 // do stuff to the mnet table
2790 $table = new xmldb_table('mnet_rpc');
2791
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');
2794
2795 $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2796 $dbman->rename_field($table, $field, 'pluginname');
2797
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);
2801 }
2802
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);
2806 }
2807
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);
2811 }
2812
2813 /// Main savepoint reached
2814 upgrade_main_savepoint($result, 2010012600);
2815 }
2816
8586dbe2
PL
2817 if ($result && $oldversion < 2010012900) {
2818
2819 /// Define table mnet_remote_rpc to be created
2820 $table = new xmldb_table('mnet_remote_rpc');
2821
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);
2826
2827 /// Adding keys to table mnet_remote_rpc
2828 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2829
2830 /// Conditionally launch create table for mnet_remote_rpc
2831 if (!$dbman->table_exists($table)) {
2832 $dbman->create_table($table);
2833 }
2834
2835
2836 /// Define table mnet_remote_service2rpc to be created
2837 $table = new xmldb_table('mnet_remote_service2rpc');
2838
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');
2843
2844 /// Adding keys to table mnet_remote_service2rpc
2845 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2846
2847 /// Adding indexes to table mnet_remote_service2rpc
2848 $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2849
2850 /// Conditionally launch create table for mnet_remote_service2rpc
2851 if (!$dbman->table_exists($table)) {
2852 $dbman->create_table($table);
2853 }
2854
2855
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');
2859
2860 /// Launch rename field function_name
2861 $dbman->rename_field($table, $field, 'functionname');
2862
2863
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');
2867
2868 /// Launch rename field xmlrpc_path
2869 $dbman->rename_field($table, $field, 'xmlrpcpath');
2870
2871
2872 /// Main savepoint reached
2873 upgrade_main_savepoint($result, 2010012900);
2874 }
a6e9dafe
PL
2875
2876 if ($result && $oldversion < 2010012901) {
2877
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');
2881
2882 /// Conditionally launch add field plugintype
2883 if (!$dbman->field_exists($table, $field)) {
2884 $dbman->add_field($table, $field);
2885 }
2886
a6e9dafe
PL
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');
2889
2890 /// Conditionally launch add field pluginname
2891 if (!$dbman->field_exists($table, $field)) {
2892 $dbman->add_field($table, $field);
2893 }
5b7984a5
PL
2894
2895 /// Main savepoint reached
2896 upgrade_main_savepoint($result, 2010012901);
2897 }
2898
2899 if ($result && $oldversion < 2010012902) {
2900
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');
2904
2905 /// Conditionally launch add field enabled
2906 if (!$dbman->field_exists($table, $field)) {
2907 $dbman->add_field($table, $field);
2908 }
2909
2910 /// Main savepoint reached
2911 upgrade_main_savepoint($result, 2010012902);
a6e9dafe
PL
2912 }
2913
f6852f02
PL
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');
2919
2920 /// Launch change of precision for field portno
2921 $dbman->change_field_precision($table, $field);
2922
2923 upgrade_main_savepoint($result, 2010020100);
2924 }
2925
d3d393ab
RW
2926 if ($result && $oldversion < 2010020300) {
2927
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');
2931
2932 if (!$dbman->field_exists($table, $field)) {
2933 /// Launch add field timecreated
2934 $dbman->add_field($table, $field);
2935
2936 $DB->execute("UPDATE {user} SET timecreated = firstaccess");
2937
2938 $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
2939 $DB->execute($sql);
2940 }
2941 upgrade_main_savepoint($result, 2010020300);
2942 }
073d3804
EL
2943
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);
2949 }
2950 upgrade_main_savepoint($result, 2010020301);
2951 }
2952
9a9012dc
PS
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);
2958 }
2959
c9606565
PL
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);
2963 }
2964
8b69c341 2965 if ($result && $oldversion < 2010031800) {
0f51ca79
AD
2966 //drop the erroneously created ratings table
2967 $table = new xmldb_table('ratings');
2968 if ($dbman->table_exists($table)) {
2969 $dbman->drop_table($table);
2970 }
2971
54e42e0b
AD
2972 //create the rating table (replaces module specific rating implementations)
2973 $table = new xmldb_table('rating');
8b69c341
AD
2974 if ($dbman->table_exists($table)) {
2975 $dbman->drop_table($table);
2976 }
a09aeee4 2977
54e42e0b 2978 /// Adding fields to table rating
a09aeee4
AD
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);
2981
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);
4f0c2d00 2986
a09aeee4
AD
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);
2989
54e42e0b 2990 /// Adding keys to table rating
a09aeee4
AD
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'));
2994
54e42e0b 2995 /// Adding indexes to table rating
a09aeee4
AD
2996 $table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
2997
2998 /// Create table for ratings
158cd5d9
AD
2999 if (!$dbman->table_exists($table)) {
3000 $dbman->create_table($table);
3001 }
a09aeee4 3002
8b69c341 3003 upgrade_main_savepoint($result, 2010031800);
a09aeee4
AD
3004 }
3005
cd1bca89
PS
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);
3010 }
90723839 3011
1dce6261 3012
90723839
SH
3013 if ($result && $oldversion < 2010032400) {
3014 // Upgrade all of those using the standardold theme to the use the standard
3015 // theme instead
3016 if ($CFG->theme == 'standardold') {
3017 // The config setting that affects the whole site
3018 set_config('theme', 'standard');
3019 }
3020 // Course Categories
3021 $DB->execute('UPDATE {course_categories} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3022 // Course
3023 $DB->execute('UPDATE {course} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3024 // User
3025 $DB->execute('UPDATE {user} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3026 upgrade_main_savepoint($result, 2010032400);
3027 }
1dce6261 3028
f0b9ee18 3029 if ($result && $oldversion < 2010033101.01) {
1dce6261
DC
3030
3031 /// Define field source to be added to files
3032 $table = new xmldb_table('files');
3033
f0b9ee18 3034 $field = new xmldb_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'status');
1dce6261
DC
3035
3036 /// Conditionally launch add field source
3037 if (!$dbman->field_exists($table, $field)) {
3038 $dbman->add_field($table, $field);
3039 }
3040
3041 $field = new xmldb_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'source');
3042
3043 /// Conditionally launch add field author
3044 if (!$dbman->field_exists($table, $field)) {
3045 $dbman->add_field($table, $field);
3046 }
3047
3048 $field = new xmldb_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'author');
3049
3050 /// Conditionally launch add field license
3051 if (!$dbman->field_exists($table, $field)) {
3052 $dbman->add_field($table, $field);
3053 }
3054
f0b9ee18
PS
3055 upgrade_main_savepoint($result, 2010033101.01);
3056 }
3057
3058 if ($result && $oldversion < 2010033101.02) {
3059
1dce6261
DC
3060 /// Define table license to be created
3061 $table = new xmldb_table('license');
3062
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');
3070
3071 /// Adding keys to table license
3072 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3073
3074 /// Conditionally launch create table for license
3075 if (!$dbman->table_exists($table)) {
3076 $dbman->create_table($table);
3077 }
d1d4813f
DC
3078 $active_licenses = array();
3079
3080 $license = new stdclass;
3081
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);
3095 }
3096 } else {
3097 $DB->insert_record('license', $license);
3098 }
3099
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);
3113 }
3114 } else {
3115 $DB->insert_record('license', $license);
3116 }
3117
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);
3131 }
3132 } else {
3133 $DB->insert_record('license', $license);
3134 }
3135
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);
3149 }
3150 } else {
3151 $DB->insert_record('license', $license);
3152 }
3153
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);
3167 }
3168 } else {
3169 $DB->insert_record('license', $license);
3170 }
3171
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);
3185 }
3186 } else {
3187 $DB->insert_record('license', $license);
3188 }
3189
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);
3204 }
3205 } else {
3206 $DB->insert_record('license', $license);
3207 }
3208
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);
3222 }
3223 } else {
3224 $DB->insert_record('license', $license);
3225 }
3226
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);
3240 }
3241 } else {
3242 $DB->insert_record('license', $license);
3243 }
3244
3245 set_config('licenses', implode(',', $active_licenses));
1dce6261
DC
3246 /// set site default license
3247 set_config('sitedefaultlicense', 'allrightsreserved');
3248
3249 /// Main savepoint reached
f0b9ee18 3250 upgrade_main_savepoint($result, 2010033101.02);
1dce6261 3251 }
4f0c2d00
PS
3252
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);
3262 }
3263
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);
3270 }
3271
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
3275 FROM {role} r
3276 JOIN {role_capabilities} rc ON rc.roleid = r.id
3277 WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
3278 ORDER BY r.id";
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');
3291 }
3292 }
3293 $DB->update_record('role', $role);
3294 }
3295 $roles->close();
3296
3297 upgrade_main_savepoint($result, 2010033102.02);
3298 }
3299
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
3308 ORDER BY ra.id";
3309 $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
3310 $admins = array();
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));
3315 }
3316
3317 upgrade_main_savepoint($result, 2010033102.03);
3318 }
3319
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
3327
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);
3334 }
3335
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));
3339
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);
3346 }
3347
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);
3354 }
3355
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);
3362 }
3363 }
3364
3365 upgrade_main_savepoint($result, 2010033102.04);
3366 }
3367
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'));
3373 }
3374 upgrade_main_savepoint($result, 2010033102.05);
3375 }
3376
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');
3384 }
3385 } else {
3386 unset_config('defaultuserroleid');
3387 }
3388 }
3389 upgrade_main_savepoint($result, 2010033102.06);
3390 }
3391
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');
3396 }
3397 upgrade_main_savepoint($result, 2010033102.07);
3398 }
3399
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');
3407 }
3408 } else {
3409 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) doeas not exist, setting cleared.');
3410 unset_config('guestroleid');
3411 }
3412 }
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')");
3416
3417 upgrade_main_savepoint($result, 2010033102.08);
3418 }
3419
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);
3424 $roles = get_roles_with_capability('moodle/course:update', CAP_ALLOW, $systemcontext);
3425
3426 // Give those roles 'moodle/course:visibility'.
3427 foreach ($roles as $role) {
3428 assign_capability('moodle/course:visibility', CAP_ALLOW, $role->id, $systemcontext->id);
3429 }
3430
3431 // Force all sessions to refresh access data.
3432 mark_context_dirty($systemcontext->path);
3433
3434 // Main savepoint reached
3435 upgrade_main_savepoint($result, 2010033102.09);
3436 }
3437
ac9b0805 3438 return $result;
4e423cbf 3439}
271e6dec 3440
f0b9ee18 3441//TODO: Cleanup before the 2.0 release - we do not want to drag along these dev machine fixes forever
e92c286c 3442// 1/ remove the automatic enabling of completion lib if debug enabled ( in 2008121701 block)
3443// 2/ move 2009061300 block to the top of the file so that we may log upgrade queries
f0b9ee18
PS
3444// 3/ remove 2010033101 block
3445// 4/ remove 2010032400 block