MDL-21432 backup 2.0 - initial commit. backup DB structures
[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
3406acde
SH
22/**
23 *
24 * @global stdClass $CFG
25 * @global stdClass $USER
26 * @global moodle_database $DB
27 * @global core_renderer $OUTPUT
28 * @param int $oldversion
29 * @return bool
30 */
775f811a 31function xmldb_main_upgrade($oldversion) {
78946b9b 32 global $CFG, $USER, $DB, $OUTPUT;
4e423cbf 33
4f12838e 34 require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
13a0d3d3 35
4e423cbf 36 $result = true;
37
46d318bc 38 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
f33e1ed4 39
1ea260d8 40 ////////////////////////////////////////
41 ///upgrade supported only from 1.9.x ///
42 ////////////////////////////////////////
da4aa3e4 43
795a08ad 44 if ($result && $oldversion < 2008030600) {
45 //NOTE: this table was added much later, that is why this step is repeated later in this file
46
47 /// Define table upgrade_log to be created
48 $table = new xmldb_table('upgrade_log');
49
50 /// Adding fields to table upgrade_log
2a88f626 51 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
52 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
53 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
54 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
55 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
56 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
57 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
58 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
59 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
795a08ad 60
61 /// Adding keys to table upgrade_log
62 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
63 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
64
65 /// Adding indexes to table upgrade_log
66 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
67 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
68
69 /// Create table for upgrade_log
70 $dbman->create_table($table);
71
72 /// Main savepoint reached
73 upgrade_main_savepoint($result, 2008030600);
74 }
75
3a915b06
PS
76 if ($result && $oldversion < 2008030601) {
77 @unlink($CFG->dataroot.'/cache/languages');
78
79 // rename old lang directory so that the new and old langs do not mix
80 if (rename("$CFG->dataroot/lang", "$CFG->dataroot/oldlang")) {
81 $oldlang = "$CFG->dataroot/oldlang";
82 } else {
83 $oldlang = "$CFG->dataroot/lang";
84 }
85 // TODO: fetch previously installed languages ("*_utf8") found in $oldlang from moodle.org
86 upgrade_set_timeout(60*20); // this may take a while
87
88
89 // TODO: add some info file to $oldlang describing what to do with "$oldlang/*_utf8_local" dirs
90
91
92 // Main savepoint reached
93 upgrade_main_savepoint($result, 2008030601);
94 }
95
a1f27717 96 if ($result && $oldversion < 2008030700) {
775f811a 97 upgrade_set_timeout(60*20); // this may take a while
a1f27717 98
99 /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
a8cb94f6 100 $table = new xmldb_table('grade_letters');
69b80cc2 101 $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
a1f27717 102
103 /// Launch drop index contextid-lowerboundary
eee5d9bb 104 $dbman->drop_index($table, $index);
a1f27717 105
a1f27717 106 /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
a8cb94f6 107 $table = new xmldb_table('grade_letters');
69b80cc2 108 $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
a1f27717 109
110 /// Launch add index contextid-lowerboundary-letter
eee5d9bb 111 $dbman->add_index($table, $index);
a1f27717 112
113 /// Main savepoint reached
0f77b186 114 upgrade_main_savepoint($result, 2008030700);
115 }
a1f27717 116
1ea260d8 117 if ($result && $oldversion < 2008050100) {
118 // Update courses that used weekscss to weeks
a8cb94f6 119 $result = $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
1ea260d8 120 upgrade_main_savepoint($result, 2008050100);
121 }
122
0b51c247 123 if ($result && $oldversion < 2008050200) {
124 // remove unused config options
125 unset_config('statsrolesupgraded');
126 upgrade_main_savepoint($result, 2008050200);
127 }
128
994fbaab 129 if ($result && $oldversion < 2008050700) {
775f811a 130 upgrade_set_timeout(60*20); // this may take a while
131
994fbaab 132 /// Fix minor problem caused by MDL-5482.
133 require_once($CFG->dirroot . '/question/upgrade.php');
134 $result = $result && question_fix_random_question_parents();
135 upgrade_main_savepoint($result, 2008050700);
136 }
137
8b9cfac4 138 if ($result && $oldversion < 2008051201) {
aa9a6867 139 echo $OUTPUT->notification('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
775f811a 140 upgrade_set_timeout(60*20); // this may take a while
8b9cfac4 141
469dcbcc 142 /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
cbc08f3b 143 $dbfamily = $DB->get_dbfamily();
144 if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
eee5d9bb 145 $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
469dcbcc 146 }
147
8b9cfac4 148 /// Define index idnumber (not unique) to be dropped form user
a8cb94f6 149 $table = new xmldb_table('user');
69b80cc2 150 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
8b9cfac4 151
152 /// Launch drop index idnumber
f33e1ed4 153 if ($dbman->index_exists($table, $index)) {
eee5d9bb 154 $dbman->drop_index($table, $index);
8b9cfac4 155 }
156
157 /// Changing precision of field idnumber on table user to (255)
a8cb94f6 158 $table = new xmldb_table('user');
2a88f626 159 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
8b9cfac4 160
161 /// Launch change of precision for field idnumber
eee5d9bb 162 $dbman->change_field_precision($table, $field);
8b9cfac4 163
164 /// Launch add index idnumber again
69b80cc2 165 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
eee5d9bb 166 $dbman->add_index($table, $index);
8b9cfac4 167
168 /// Main savepoint reached
169 upgrade_main_savepoint($result, 2008051201);
170 }
171
7ade55ad 172 if ($result && $oldversion < 2008051202) {
910dd4dd 173 $log_action = new object();
7ade55ad 174 $log_action->module = 'course';
175 $log_action->action = 'unenrol';
176 $log_action->mtable = 'course';
177 $log_action->field = 'fullname';
910dd4dd 178 if (!$DB->record_exists('log_display', array('action'=>'unenrol', 'module'=>'course'))) {
179 $result = $result && $DB->insert_record('log_display', $log_action);
7ade55ad 180 }
181 upgrade_main_savepoint($result, 2008051202);
182 }
183
fa162144 184 if ($result && $oldversion < 2008051203) {
185 $table = new xmldb_table('mnet_enrol_course');
2a88f626 186 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
eee5d9bb 187 $dbman->change_field_precision($table, $field);
fa162144 188 upgrade_main_savepoint($result, 2008051203);
189 }
190
38fb8190 191 if ($result && $oldversion < 2008063001) {
775f811a 192 upgrade_set_timeout(60*20); // this may take a while
193
38fb8190 194 // table to be modified
195 $table = new xmldb_table('tag_instance');
196 // add field
197 $field = new xmldb_field('tiuserid');
198 if (!$dbman->field_exists($table, $field)) {
2a88f626 199 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
38fb8190 200 $dbman->add_field($table, $field);
201 }
202 // modify index
203 $index = new xmldb_index('itemtype-itemid-tagid');
204 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
b91de8a5 205 if ($dbman->index_exists($table, $index)) {
206 $dbman->drop_index($table, $index);
207 }
38fb8190 208 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
209 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
b91de8a5 210 if (!$dbman->index_exists($table, $index)) {
211 $dbman->add_index($table, $index);
212 }
38fb8190 213
214 /// Main savepoint reached
215 upgrade_main_savepoint($result, 2008063001);
216 }
38fb8190 217
a036d7f3 218 if ($result && $oldversion < 2008070300) {
219 $result = $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
220 upgrade_main_savepoint($result, 2008070300);
221 }
4e781c7b 222
ac5efef6 223 if ($result && $oldversion < 2008070701) {
224
225 /// Define table portfolio_instance to be created
226 $table = new xmldb_table('portfolio_instance');
227
228 /// Adding fields to table portfolio_instance
2a88f626 229 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
230 $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
231 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
232 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
ac5efef6 233
234 /// Adding keys to table portfolio_instance
235 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
236
237 /// Conditionally launch create table for portfolio_instance
238 if (!$dbman->table_exists($table)) {
239 $dbman->create_table($table);
240 }
241 /// Define table portfolio_instance_config to be created
242 $table = new xmldb_table('portfolio_instance_config');
243
244 /// Adding fields to table portfolio_instance_config
2a88f626 245 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
246 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
247 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
248 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
ac5efef6 249
250 /// Adding keys to table portfolio_instance_config
251 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
252 $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
253
254 /// Adding indexes to table portfolio_instance_config
255 $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
256
257 /// Conditionally launch create table for portfolio_instance_config
258 if (!$dbman->table_exists($table)) {
259 $dbman->create_table($table);
260 }
261
262 /// Define table portfolio_instance_user to be created
263 $table = new xmldb_table('portfolio_instance_user');
264
265 /// Adding fields to table portfolio_instance_user
2a88f626 266 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
267 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
268 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
269 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
270 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
ac5efef6 271
272 /// Adding keys to table portfolio_instance_user
273 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
274 $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
275 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
276
277 /// Conditionally launch create table for portfolio_instance_user
278 if (!$dbman->table_exists($table)) {
279 $dbman->create_table($table);
280 }
281
282 /// Main savepoint reached
283 upgrade_main_savepoint($result, 2008070701);
284 }
172dd12c 285
775f811a 286 if ($result && $oldversion < 2008072400) {
287 /// Create the database tables for message_processors
f65b2dae 288 $table = new xmldb_table('message_processors');
2a88f626 289 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
290 $table->add_field('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null);
f65b2dae 291 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3b120e46 292 $dbman->create_table($table);
293
3b120e46 294 /// delete old and create new fields
f65b2dae 295 $table = new xmldb_table('message');
296 $field = new xmldb_field('messagetype');
3b120e46 297 $dbman->drop_field($table, $field);
298
299 /// fields to rename
f65b2dae 300 $field = new xmldb_field('message');
2a88f626 301 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 302 $dbman->rename_field($table, $field, 'fullmessage');
f65b2dae 303 $field = new xmldb_field('format');
2a88f626 304 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
3b120e46 305 $dbman->rename_field($table, $field, 'fullmessageformat');
306
307 /// new message fields
f65b2dae 308 $field = new xmldb_field('subject');
2a88f626 309 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 310 $dbman->add_field($table, $field);
f65b2dae 311 $field = new xmldb_field('fullmessagehtml');
2a88f626 312 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
3b120e46 313 $dbman->add_field($table, $field);
f65b2dae 314 $field = new xmldb_field('smallmessage');
2a88f626 315 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 316 $dbman->add_field($table, $field);
317
318
f65b2dae 319 $table = new xmldb_table('message_read');
320 $field = new xmldb_field('messagetype');
3b120e46 321 $dbman->drop_field($table, $field);
f65b2dae 322 $field = new xmldb_field('mailed');
3b120e46 323 $dbman->drop_field($table, $field);
324
325 /// fields to rename
f65b2dae 326 $field = new xmldb_field('message');
2a88f626 327 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 328 $dbman->rename_field($table, $field, 'fullmessage');
f65b2dae 329 $field = new xmldb_field('format');
2a88f626 330 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
3b120e46 331 $dbman->rename_field($table, $field, 'fullmessageformat');
332
333
334 /// new message fields
f65b2dae 335 $field = new xmldb_field('subject');
2a88f626 336 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 337 $dbman->add_field($table, $field);
f65b2dae 338 $field = new xmldb_field('fullmessagehtml');
2a88f626 339 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
3b120e46 340 $dbman->add_field($table, $field);
f65b2dae 341 $field = new xmldb_field('smallmessage');
2a88f626 342 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 343 $dbman->add_field($table, $field);
344
345 /// new table
f65b2dae 346 $table = new xmldb_table('message_working');
2a88f626 347 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
348 $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
349 $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
f65b2dae 350 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3b120e46 351 $dbman->create_table($table);
352
353
354 upgrade_main_savepoint($result, 2008072400);
355 }
356
4e781c7b 357 if ($result && $oldversion < 2008072800) {
358
359 /// Define field enablecompletion to be added to course
360 $table = new xmldb_table('course');
361 $field = new xmldb_field('enablecompletion');
2a88f626 362 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
4e781c7b 363
364 /// Launch add field enablecompletion
775f811a 365 if (!$dbman->field_exists($table,$field)) {
4e781c7b 366 $dbman->add_field($table, $field);
367 }
775f811a 368
4e781c7b 369 /// Define field completion to be added to course_modules
370 $table = new xmldb_table('course_modules');
371 $field = new xmldb_field('completion');
2a88f626 372 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'groupmembersonly');
4e781c7b 373
374 /// Launch add field completion
775f811a 375 if (!$dbman->field_exists($table,$field)) {
4e781c7b 376 $dbman->add_field($table, $field);
377 }
378
379 /// Define field completiongradeitemnumber to be added to course_modules
380 $field = new xmldb_field('completiongradeitemnumber');
2a88f626 381 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'completion');
4e781c7b 382
383 /// Launch add field completiongradeitemnumber
775f811a 384 if (!$dbman->field_exists($table,$field)) {
4e781c7b 385 $dbman->add_field($table, $field);
386 }
387
388 /// Define field completionview to be added to course_modules
389 $field = new xmldb_field('completionview');
2a88f626 390 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiongradeitemnumber');
4e781c7b 391
392 /// Launch add field completionview
775f811a 393 if (!$dbman->field_exists($table,$field)) {
4e781c7b 394 $dbman->add_field($table, $field);
395 }
396
397 /// Define field completionexpected to be added to course_modules
398 $field = new xmldb_field('completionexpected');
2a88f626 399 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionview');
4e781c7b 400
401 /// Launch add field completionexpected
775f811a 402 if (!$dbman->field_exists($table,$field)) {
4e781c7b 403 $dbman->add_field($table, $field);
404 }
405
406 /// Define table course_modules_completion to be created
407 $table = new xmldb_table('course_modules_completion');
775f811a 408 if (!$dbman->table_exists($table)) {
4e781c7b 409
410 /// Adding fields to table course_modules_completion
2a88f626 411 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
412 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
413 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
414 $table->add_field('completionstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
415 $table->add_field('viewed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
416 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
775f811a 417
4e781c7b 418 /// Adding keys to table course_modules_completion
1e0c56ea 419 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
775f811a 420
4e781c7b 421 /// Adding indexes to table course_modules_completion
1e0c56ea 422 $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
423 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
775f811a 424
4e781c7b 425 /// Launch create table for course_modules_completion
426 $dbman->create_table($table);
427 }
428
429 /// Main savepoint reached
430 upgrade_main_savepoint($result, 2008072800);
431 }
775f811a 432
4ab16a09 433 if ($result && $oldversion < 2008073000) {
ac5efef6 434
4ab16a09 435 /// Define table portfolio_log to be created
436 $table = new xmldb_table('portfolio_log');
437
438 /// Adding fields to table portfolio_log
2a88f626 439 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
440 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
441 $table->add_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
442 $table->add_field('portfolio', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
443 $table->add_field('caller_class', XMLDB_TYPE_CHAR, '150', null, XMLDB_NOTNULL, null, null);
444 $table->add_field('caller_file', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
445 $table->add_field('caller_sha1', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
4ab16a09 446
447 /// Adding keys to table portfolio_log
448 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
449 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
450 $table->add_key('portfoliofk', XMLDB_KEY_FOREIGN, array('portfolio'), 'portfolio_instance', array('id'));
451
452 /// Conditionally launch create table for portfolio_log
453 if (!$dbman->table_exists($table)) {
454 $dbman->create_table($table);
455 }
456
457 /// Main savepoint reached
458 upgrade_main_savepoint($result, 2008073000);
459 }
120b3758 460
461 if ($result && $oldversion < 2008073104) {
462 /// Drop old table that might exist for some people
463 $table = new xmldb_table('message_providers');
464 if ($dbman->table_exists($table)) {
465 $dbman->drop_table($table);
466 }
467
468 /// Define table message_providers to be created
469 $table = new xmldb_table('message_providers');
470
471 /// Adding fields to table message_providers
2a88f626 472 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
473 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
474 $table->add_field('component', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
475 $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
120b3758 476
477 /// Adding keys to table message_providers
478 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
479
480 /// Adding indexes to table message_providers
481 $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
482
483 /// Create table for message_providers
484 $dbman->create_table($table);
485
486 upgrade_main_savepoint($result, 2008073104);
487 }
172dd12c 488
489 if ($result && $oldversion < 2008073111) {
490 /// Define table files to be created
491 $table = new xmldb_table('files');
492
493 /// Adding fields to table files
2a88f626 494 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
495 $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
496 $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
497 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
498 $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
499 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
500 $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
501 $table->add_field('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
502 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
503 $table->add_field('filesize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
504 $table->add_field('mimetype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
505 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
f0b9ee18
PS
506 $table->add_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
507 $table->add_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null);
508 $table->add_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2a88f626 509 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
510 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
172dd12c 511
512 /// Adding keys to table files
513 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
514 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
515 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
516
517 /// Adding indexes to table files
518 $table->add_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid'));
519 $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
520 $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
521
522 /// Conditionally launch create table for files
523 $dbman->create_table($table);
524
525 /// Main savepoint reached
526 upgrade_main_savepoint($result, 2008073111);
527 }
528
172dd12c 529 if ($result && $oldversion < 2008073113) {
530 /// move all course, backup and other files to new filepool based storage
531 upgrade_migrate_files_courses();
532 /// Main savepoint reached
533 upgrade_main_savepoint($result, 2008073113);
534 }
535
536 if ($result && $oldversion < 2008073114) {
537 /// move all course, backup and other files to new filepool based storage
538 upgrade_migrate_files_blog();
539 /// Main savepoint reached
540 upgrade_main_savepoint($result, 2008073114);
541 }
f33e1ed4 542
1ce2da58 543 if ($result && $oldversion < 2008080400) {
544 // Add field ssl_jump_url to mnet application, and populate existing default applications
545 $table = new xmldb_table('mnet_application');
546 $field = new xmldb_field('sso_jump_url');
547 if (!$dbman->field_exists($table, $field)) {
2a88f626 548 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
c5a04d12 549 $dbman->add_field($table, $field);
550 $result = $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
1ce2da58 551 $result = $result && $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
552 }
553
554 /// Main savepoint reached
555 upgrade_main_savepoint($result, 2008080400);
556 }
04f35360 557
558 if ($result && $oldversion < 2008080500) {
559
560 /// Define table portfolio_tempdata to be created
561 $table = new xmldb_table('portfolio_tempdata');
562
563 /// Adding fields to table portfolio_tempdata
2a88f626 564 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
565 $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
04f35360 566
567 /// Adding keys to table portfolio_tempdata
568 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
569
570 /// Conditionally launch create table for portfolio_tempdata
571 if (!$dbman->table_exists($table)) {
572 $dbman->create_table($table);
573 }
574
575 /// Main savepoint reached
576 upgrade_main_savepoint($result, 2008080500);
577 }
578
84a44985 579 if ($result && $oldversion < 2008080600) {
580
581 $DB->delete_records('portfolio_tempdata'); // there shouldnt' be any, and it will cause problems with this upgrade.
582 /// Define field expirytime to be added to portfolio_tempdata
583 $table = new xmldb_table('portfolio_tempdata');
2a88f626 584 $field = new xmldb_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'data');
84a44985 585
586 /// Conditionally launch add field expirytime
587 if (!$dbman->field_exists($table, $field)) {
588 $dbman->add_field($table, $field);
589 }
590
591 /// Main savepoint reached
592 upgrade_main_savepoint($result, 2008080600);
593 }
775f811a 594
c1f58efe 595 if ($result && $oldversion < 2008081500) {
596 /// Changing the type of all the columns that the question bank uses to store grades to be NUMBER(12, 7).
597 $table = new xmldb_table('question');
598 $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'generalfeedback');
599 $dbman->change_field_type($table, $field);
600 upgrade_main_savepoint($result, 2008081500);
601 }
602
f9a2cf86 603 if ($result && $oldversion < 2008081501) {
604 $table = new xmldb_table('question');
2a88f626 605 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'defaultgrade');
f9a2cf86 606 $dbman->change_field_type($table, $field);
607 upgrade_main_savepoint($result, 2008081501);
608 }
609
610 if ($result && $oldversion < 2008081502) {
611 $table = new xmldb_table('question_answers');
2a88f626 612 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'answer');
f9a2cf86 613 $dbman->change_field_type($table, $field);
614 upgrade_main_savepoint($result, 2008081502);
615 }
616
617 if ($result && $oldversion < 2008081503) {
618 $table = new xmldb_table('question_sessions');
2a88f626 619 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'newgraded');
f9a2cf86 620 $dbman->change_field_type($table, $field);
621 upgrade_main_savepoint($result, 2008081503);
622 }
623
624 if ($result && $oldversion < 2008081504) {
625 $table = new xmldb_table('question_states');
2a88f626 626 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'event');
f9a2cf86 627 $dbman->change_field_type($table, $field);
628 upgrade_main_savepoint($result, 2008081504);
629 }
630
631 if ($result && $oldversion < 2008081505) {
632 $table = new xmldb_table('question_states');
2a88f626 633 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'grade');
f9a2cf86 634 $dbman->change_field_type($table, $field);
635 upgrade_main_savepoint($result, 2008081505);
636 }
637
638 if ($result && $oldversion < 2008081506) {
639 $table = new xmldb_table('question_states');
2a88f626 640 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'raw_grade');
f9a2cf86 641 $dbman->change_field_type($table, $field);
642 upgrade_main_savepoint($result, 2008081506);
643 }
644
775f811a 645 if ($result && $oldversion < 2008081600) {
646
647 /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
648 unset_config('unicodedb');
649
650 /// Main savepoint reached
651 upgrade_main_savepoint($result, 2008081600);
652 }
653
ff2f69bc 654 if ($result && $oldversion < 2008081900) {
655 /// Define field userid to be added to portfolio_tempdata
656 $table = new xmldb_table('portfolio_tempdata');
2a88f626 657 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'expirytime');
ff2f69bc 658
659 /// Conditionally launch add field userid
660 if (!$dbman->field_exists($table, $field)) {
661 $dbman->add_field($table, $field);
662 }
663 $DB->set_field('portfolio_tempdata', 'userid', 0);
664 /// now change it to be notnull
665
666 /// Changing nullability of field userid on table portfolio_tempdata to not null
2a88f626 667 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'expirytime');
ff2f69bc 668
669 /// Launch change of nullability for field userid
670 $dbman->change_field_notnull($table, $field);
671
672 /// Define key userfk (foreign) to be added to portfolio_tempdata
673 $table = new xmldb_table('portfolio_tempdata');
674 $key = new xmldb_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
675
676 /// Launch add key userfk
677 $dbman->add_key($table, $key);
678
679 upgrade_main_savepoint($result, 2008081900);
680 }
8f3f6cc3 681 if ($result && $oldversion < 2008082602) {
27051e43 682
683 /// Define table repository to be dropped
684 $table = new xmldb_table('repository');
685
686 /// Conditionally launch drop table for repository
687 if ($dbman->table_exists($table)) {
688 $dbman->drop_table($table);
689 }
690
691 /// Define table repository to be created
692 $table = new xmldb_table('repository');
693
694 /// Adding fields to table repository
2a88f626 695 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
696 $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
697 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1');
698 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
27051e43 699
700 /// Adding keys to table repository
701 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
702
703 /// Conditionally launch create table for repository
704 if (!$dbman->table_exists($table)) {
705 $dbman->create_table($table);
706 }
707 /// Define table repository_instances to be created
708 $table = new xmldb_table('repository_instances');
709
710 /// Adding fields to table repository_instances
2a88f626 711 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
712 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
713 $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
714 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
715 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
716 $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null);
717 $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
718 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
719 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
27051e43 720
721 /// Adding keys to table repository_instances
722 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
723
724 /// Conditionally launch create table for repository_instances
725 if (!$dbman->table_exists($table)) {
726 $dbman->create_table($table);
727 }
728
729 /// Define table repository_instance_config to be created
730 $table = new xmldb_table('repository_instance_config');
731
732 /// Adding fields to table repository_instance_config
2a88f626 733 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
734 $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
735 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
736 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
27051e43 737
738 /// Adding keys to table repository_instance_config
739 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
740
741 /// Conditionally launch create table for repository_instance_config
742 if (!$dbman->table_exists($table)) {
743 $dbman->create_table($table);
744 }
745
746 /// Main savepoint reached
8f3f6cc3 747 upgrade_main_savepoint($result, 2008082602);
27051e43 748 }
749
62e76c67 750 if ($result && $oldversion < 2008082700) {
751 /// Add a new column to the question sessions table to record whether a
752 /// question has been flagged.
753
754 /// Define field flagged to be added to question_sessions
755 $table = new xmldb_table('question_sessions');
2a88f626 756 $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
62e76c67 757
758 /// Conditionally launch add field flagged
759 if (!$dbman->field_exists($table, $field)) {
760 $dbman->add_field($table, $field);
761 }
762
763 /// Main savepoint reached
764 upgrade_main_savepoint($result, 2008082700);
765 }
ff2f69bc 766
2c48aeab 767 if ($result && $oldversion < 2008082900) {
768
769 /// Changing precision of field parent_type on table mnet_rpc to (20)
770 $table = new xmldb_table('mnet_rpc');
2a88f626 771 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2c48aeab 772
773 /// Launch change of precision for field parent_type
774 $dbman->change_field_precision($table, $field);
775
776 /// Main savepoint reached
777 upgrade_main_savepoint($result, 2008082900);
778 }
779
ff7e7f92 780 if ($result && $oldversion < 2008090108) {
781 $repo = new object();
782 $repo->type = 'upload';
783 $repo->visible = 1;
784 $repo->sortorder = 1;
785 if (!$DB->record_exists('repository', array('type'=>'upload'))) {
786 $typeid = $DB->insert_record('repository', $repo);
787 }else{
788 $record = $DB->get_record('repository', array('type'=>'upload'));
789 $typeid = $record->id;
790 }
791 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
792 $instance = new object();
fb7cae3b 793 $instance->name = get_string('repositoryname', 'repository_upload');
ff7e7f92 794 $instance->typeid = $typeid;
795 $instance->userid = 0;
796 $instance->contextid = SITEID;
797 $instance->timecreated = time();
798 $instance->timemodified = time();
799 $result = $result && $DB->insert_record('repository_instances', $instance);
800 }
801 $repo->type = 'local';
802 $repo->visible = 1;
803 $repo->sortorder = 1;
804 if (!$DB->record_exists('repository', array('type'=>'local'))) {
805 $typeid = $DB->insert_record('repository', $repo);
806 }else{
807 $record = $DB->get_record('repository', array('type'=>'local'));
808 $typeid = $record->id;
809 }
810 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
811 $instance = new object();
fb7cae3b 812 $instance->name = get_string('repositoryname', 'repository_local');
ff7e7f92 813 $instance->typeid = $typeid;
814 $instance->userid = 0;
815 $instance->contextid = SITEID;
816 $instance->timecreated = time();
817 $instance->timemodified = time();
818 $result = $result && $DB->insert_record('repository_instances', $instance);
819 }
820
821 upgrade_main_savepoint($result, 2008090108);
822 }
823
d849880e 824 // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
825 if ($result && $oldversion < 2008091000) {
826 foreach (get_object_vars($CFG) as $name => $value) {
827 if (substr($name, strlen($name) - 8) !== '_version') {
828 continue;
829 }
830 $pluginname = substr($name, 0, strlen($name) - 8);
831 if (!strpos($pluginname, '_')) {
832 // Skip things like backup_version that don't contain an extra _
833 continue;
834 }
835 if ($pluginname == 'enrol_ldap_version') {
836 // Special case - this is something different from a plugin version number.
837 continue;
838 }
6f49dd0f 839 if (!preg_match('/^\d{10}$/', $value)) {
d849880e 840 // Extra safety check, skip anything that does not look like a Moodle
841 // version number (10 digits).
842 continue;
843 }
844 $result = $result && set_config('version', $value, $pluginname);
845 $result = $result && unset_config($name);
846 }
847 upgrade_main_savepoint($result, 2008091000);
848 }
849
948c2860 850 //Add a readonly field to the repository_instances table
851 //in order to support instance created automatically by a repository plugin
852 if ($result && $oldversion < 2008091611) {
853
854 /// Define field readonly to be added to repository_instances
855 $table = new xmldb_table('repository_instances');
2a88f626 856 $field = new xmldb_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
948c2860 857
858 /// Conditionally launch add field readonly
859 if (!$dbman->field_exists($table, $field)) {
860 $dbman->add_field($table, $field);
861 }
862
863 /// Main savepoint reached
864 upgrade_main_savepoint($result, 2008091611);
865 }
866
53b20fe3 867 if ($result && $oldversion < 2008092300) {
868 unset_config('editorspelling');
869 unset_config('editordictionary');
870 /// Main savepoint reached
871 upgrade_main_savepoint($result, 2008092300);
872 }
873
5e7206a8 874 if ($result && $oldversion < 2008101300) {
875
d9e673c1 876 if (!get_config(NULL, 'statsruntimedays')) {
5e7206a8 877 set_config('statsruntimedays', '31');
878 }
879
880 /// Main savepoint reached
881 upgrade_main_savepoint($result, 2008101300);
882 }
53b20fe3 883
01a80f51 884 /// New table for storing which roles can be assigned in which contexts.
885 if ($result && $oldversion < 2008110601) {
886
887 /// Define table role_context_levels to be created
888 $table = new xmldb_table('role_context_levels');
889
890 /// Adding fields to table role_context_levels
2a88f626 891 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
892 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
893 $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01a80f51 894
895 /// Adding keys to table role_context_levels
896 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
897 $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
898 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
899
900 /// Conditionally launch create table for role_context_levels
901 if (!$dbman->table_exists($table)) {
902 $dbman->create_table($table);
903 }
904
905 /// Main savepoint reached
906 upgrade_main_savepoint($result, 2008110601);
907 }
908
909 /// Now populate the role_context_levels table with the defaults that match
910 /// moodle_install_roles, and any other combinations that exist in this system.
911 if ($result && $oldversion < 2008110602) {
912 $roleids = $DB->get_records_menu('role', array(), '', 'shortname,id');
913
914 /// Defaults, should match moodle_install_roles.
915 $rolecontextlevels = array();
01a80f51 916 if (isset($roleids['coursecreator'])) {
917 $rolecontextlevels[$roleids['coursecreator']] = get_default_contextlevels('coursecreator');
918 }
919 if (isset($roleids['editingteacher'])) {
920 $rolecontextlevels[$roleids['editingteacher']] = get_default_contextlevels('editingteacher');
921 }
922 if (isset($roleids['teacher'])) {
923 $rolecontextlevels[$roleids['teacher']] = get_default_contextlevels('teacher');
924 }
925 if (isset($roleids['student'])) {
926 $rolecontextlevels[$roleids['student']] = get_default_contextlevels('student');
927 }
928 if (isset($roleids['guest'])) {
929 $rolecontextlevels[$roleids['guest']] = get_default_contextlevels('guest');
930 }
931 if (isset($roleids['user'])) {
932 $rolecontextlevels[$roleids['user']] = get_default_contextlevels('user');
933 }
656be893 934
01a80f51 935 /// See what other role assignments are in this database, extend the allowed
936 /// lists to allow them too.
937 $existingrolecontextlevels = $DB->get_recordset_sql('SELECT DISTINCT ra.roleid, con.contextlevel FROM
938 {role_assignments} ra JOIN {context} con ON ra.contextid = con.id');
939 foreach ($existingrolecontextlevels as $rcl) {
940 if (!isset($rolecontextlevels[$rcl->roleid])) {
941 $rolecontextlevels[$rcl->roleid] = array($rcl->contextlevel);
942 } else if (!in_array($rcl->contextlevel, $rolecontextlevels[$rcl->roleid])) {
943 $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
944 }
945 }
946
947 /// Put the data into the database.
948 foreach ($rolecontextlevels as $roleid => $contextlevels) {
949 set_role_contextlevels($roleid, $contextlevels);
950 }
951
952 /// Main savepoint reached
953 upgrade_main_savepoint($result, 2008110602);
954 }
955
9101efd3 956 /// Drop the deprecated teacher, teachers, student and students columns from the course table.
957 if ($result && $oldversion < 2008111200) {
958 $table = new xmldb_table('course');
959
960 /// Conditionally launch drop field teacher
961 $field = new xmldb_field('teacher');
962 if ($dbman->field_exists($table, $field)) {
963 $dbman->drop_field($table, $field);
964 }
965
966 /// Conditionally launch drop field teacher
967 $field = new xmldb_field('teachers');
968 if ($dbman->field_exists($table, $field)) {
969 $dbman->drop_field($table, $field);
970 }
971
972 /// Conditionally launch drop field teacher
973 $field = new xmldb_field('student');
974 if ($dbman->field_exists($table, $field)) {
975 $dbman->drop_field($table, $field);
976 }
977
978 /// Conditionally launch drop field teacher
979 $field = new xmldb_field('students');
980 if ($dbman->field_exists($table, $field)) {
981 $dbman->drop_field($table, $field);
982 }
983
984 /// Main savepoint reached
985 upgrade_main_savepoint($result, 2008111200);
986 }
987
40c792c3 988/// Add a unique index to the role.name column.
989 if ($result && $oldversion < 2008111800) {
990
991 /// Define index name (unique) to be added to role
992 $table = new xmldb_table('role');
993 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
994
995 /// Conditionally launch add index name
996 if (!$dbman->index_exists($table, $index)) {
997 $dbman->add_index($table, $index);
998 }
999
1000 /// Main savepoint reached
1001 upgrade_main_savepoint($result, 2008111800);
1002 }
1003
1004/// Add a unique index to the role.shortname column.
1005 if ($result && $oldversion < 2008111801) {
1006
1007 /// Define index shortname (unique) to be added to role
1008 $table = new xmldb_table('role');
1009 $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
1010
1011 /// Conditionally launch add index shortname
1012 if (!$dbman->index_exists($table, $index)) {
1013 $dbman->add_index($table, $index);
1014 }
1015
1016 /// Main savepoint reached
1017 upgrade_main_savepoint($result, 2008111801);
1018 }
1019
82bd6a5e 1020 if ($result && $oldversion < 2008120700) {
1021
1022 /// Changing precision of field shortname on table course_request to (100)
1023 $table = new xmldb_table('course_request');
2a88f626 1024 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
82bd6a5e 1025
da750a51 1026 /// Before changing the field, drop dependent indexes
1027 /// Define index shortname (not unique) to be dropped form course_request
1028 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
1029 /// Conditionally launch drop index shortname
1030 if ($dbman->index_exists($table, $index)) {
1031 $dbman->drop_index($table, $index);
1032 }
1033
82bd6a5e 1034 /// Launch change of precision for field shortname
1035 $dbman->change_field_precision($table, $field);
1036
da750a51 1037 /// After changing the field, recreate dependent indexes
1038 /// Define index shortname (not unique) to be added to course_request
1039 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
1040 /// Conditionally launch add index shortname
1041 if (!$dbman->index_exists($table, $index)) {
1042 $dbman->add_index($table, $index);
1043 }
1044
82bd6a5e 1045 /// Main savepoint reached
1046 upgrade_main_savepoint($result, 2008120700);
1047 }
1048
82bd6a5e 1049 if ($result && $oldversion < 2008120801) {
1050
1051 /// Changing precision of field shortname on table mnet_enrol_course to (100)
1052 $table = new xmldb_table('mnet_enrol_course');
2a88f626 1053 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
82bd6a5e 1054
1055 /// Launch change of precision for field shortname
1056 $dbman->change_field_precision($table, $field);
1057
1058 /// Main savepoint reached
1059 upgrade_main_savepoint($result, 2008120801);
1060 }
1061
1062 if ($result && $oldversion < 2008121701) {
0953a4e7 1063
1064 /// Define field availablefrom to be added to course_modules
1065 $table = new xmldb_table('course_modules');
2a88f626 1066 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
0953a4e7 1067
1068 /// Conditionally launch add field availablefrom
1069 if (!$dbman->field_exists($table, $field)) {
1070 $dbman->add_field($table, $field);
1071 }
1072
1073 /// Define field availableuntil to be added to course_modules
2a88f626 1074 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
0953a4e7 1075
1076 /// Conditionally launch add field availableuntil
1077 if (!$dbman->field_exists($table, $field)) {
1078 $dbman->add_field($table, $field);
1079 }
82bd6a5e 1080
0953a4e7 1081 /// Define field showavailability to be added to course_modules
2a88f626 1082 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
0953a4e7 1083
1084 /// Conditionally launch add field showavailability
1085 if (!$dbman->field_exists($table, $field)) {
1086 $dbman->add_field($table, $field);
1087 }
82bd6a5e 1088
0953a4e7 1089 /// Define table course_modules_availability to be created
1090 $table = new xmldb_table('course_modules_availability');
1091
1092 /// Adding fields to table course_modules_availability
2a88f626 1093 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1094 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1095 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1096 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
1097 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1098 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1099 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
0953a4e7 1100
1101 /// Adding keys to table course_modules_availability
1102 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
cbb17d54 1103 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
1104 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
1105 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
0953a4e7 1106
1107 /// Conditionally launch create table for course_modules_availability
1108 if (!$dbman->table_exists($table)) {
1109 $dbman->create_table($table);
1110 }
1111
82bd6a5e 1112 /// Changes to modinfo mean we need to rebuild course cache
a9a0b93d 1113 require_once($CFG->dirroot . '/course/lib.php');
8b6a0968 1114 rebuild_course_cache(0, true);
9136a60c 1115
82bd6a5e 1116 /// For developer upgrades, turn on the conditional activities and completion
1117 /// features automatically (to gain more testing)
8b6a0968 1118//TODO: remove before 2.0 final!
1119 if (debugging('', DEBUG_DEVELOPER)) {
1120 set_config('enableavailability', 1);
1121 set_config('enablecompletion', 1);
9136a60c 1122 }
1123
9d510a2e 1124 /// Main savepoint reached
82bd6a5e 1125 upgrade_main_savepoint($result, 2008121701);
9d510a2e 1126 }
1127
656be893 1128 if ($result && $oldversion < 2009010500) {
1129 /// clean up config table a bit
1130 unset_config('session_error_counter');
1131
1132 /// Main savepoint reached
1133 upgrade_main_savepoint($result, 2009010500);
1134 }
1135
1136 if ($result && $oldversion < 2009010600) {
1137
1138 /// Define field originalquestion to be dropped from question_states
1139 $table = new xmldb_table('question_states');
1140 $field = new xmldb_field('originalquestion');
1141
1142 /// Conditionally launch drop field originalquestion
1143 if ($dbman->field_exists($table, $field)) {
1144 $dbman->drop_field($table, $field);
1145 }
1146
1147 /// Main savepoint reached
1148 upgrade_main_savepoint($result, 2009010600);
1149 }
1150
1151 if ($result && $oldversion < 2009010601) {
1152
1153 /// Changing precision of field ip on table log to (45)
1154 $table = new xmldb_table('log');
2a88f626 1155 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
656be893 1156
1157 /// Launch change of precision for field ip
1158 $dbman->change_field_precision($table, $field);
1159
1160 /// Main savepoint reached
1161 upgrade_main_savepoint($result, 2009010601);
1162 }
1163
1164 if ($result && $oldversion < 2009010602) {
1165
1166 /// Changing precision of field lastip on table user to (45)
1167 $table = new xmldb_table('user');
2a88f626 1168 $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
656be893 1169
1170 /// Launch change of precision for field lastip
1171 $dbman->change_field_precision($table, $field);
1172
1173 /// Main savepoint reached
1174 upgrade_main_savepoint($result, 2009010602);
1175 }
1176
1177 if ($result && $oldversion < 2009010603) {
1178
1179 /// Changing precision of field ip_address on table mnet_host to (45)
1180 $table = new xmldb_table('mnet_host');
2a88f626 1181 $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
656be893 1182
1183 /// Launch change of precision for field ip_address
1184 $dbman->change_field_precision($table, $field);
1185
1186 /// Main savepoint reached
1187 upgrade_main_savepoint($result, 2009010603);
1188 }
1189
1190 if ($result && $oldversion < 2009010604) {
1191
1192 /// Changing precision of field ip on table mnet_log to (45)
1193 $table = new xmldb_table('mnet_log');
2a88f626 1194 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
656be893 1195
1196 /// Launch change of precision for field ip
1197 $dbman->change_field_precision($table, $field);
1198
1199 /// Main savepoint reached
1200 upgrade_main_savepoint($result, 2009010604);
1201 }
1202
4413941f 1203 if ($result && $oldversion < 2009010800) {
1204 /// Update the notifyloginfailures setting.
1205 if ($CFG->notifyloginfailures == 'mainadmin') {
1206 set_config('notifyloginfailures', get_admin()->username);
1207 } else if ($CFG->notifyloginfailures == 'alladmins') {
1208 set_config('notifyloginfailures', '$@ALL@$');
1209 } else {
1210 set_config('notifyloginfailures', '');
1211 }
1212
1213 /// Main savepoint reached
1214 upgrade_main_savepoint($result, 2009010800);
1215 }
1216
ab2eb65c 1217 if ($result && $oldversion < 2009011000) {
1218
1219 /// Changing nullability of field configdata on table block_instance to null
1220 $table = new xmldb_table('block_instance');
1221 $field = new xmldb_field('configdata');
2a88f626 1222 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
ab2eb65c 1223
1224 /// Launch change of nullability for field configdata
1225 $dbman->change_field_notnull($table, $field);
1226
1227 /// Main savepoint reached
1228 upgrade_main_savepoint($result, 2009011000);
1229 }
1230
1caea91e 1231 if ($result && $oldversion < 2009011100) {
1232 /// Remove unused settings
1233 unset_config('zip');
1234 unset_config('unzip');
1235 unset_config('adminblocks_initialised');
1236
1237 /// Main savepoint reached
1238 upgrade_main_savepoint($result, 2009011100);
1239 }
1240
eb6a973c 1241 if ($result && $oldversion < 2009011101) {
1242 /// Migrate backup settings to core plugin config table
1243 $configs = $DB->get_records('backup_config');
1244 foreach ($configs as $config) {
1245 set_config($config->name, $config->value, 'backup');
1246 }
1247
1248 /// Define table to be dropped
1249 $table = new xmldb_table('backup_config');
1250
1251 /// Launch drop table for old backup config
1252 $dbman->drop_table($table);
1253
1254 /// Main savepoint reached
1255 upgrade_main_savepoint($result, 2009011101);
1256 }
1257
301bf0b2 1258 if ($result && $oldversion < 2009011303) {
1259
1260 /// Define table config_log to be created
1261 $table = new xmldb_table('config_log');
1262
1263 /// Adding fields to table config_log
2a88f626 1264 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1265 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1266 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1267 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1268 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1269 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1270 $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
301bf0b2 1271
1272 /// Adding keys to table config_log
1273 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1274 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1275
1276 /// Adding indexes to table config_log
1277 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1278
1279 /// Launch create table for config_log
1280 $dbman->create_table($table);
1281
1282 /// Main savepoint reached
1283 upgrade_main_savepoint($result, 2009011303);
1284 }
1285
b9fb7103 1286 if ($result && $oldversion < 2009011900) {
3f57bd45 1287
1288 /// Define table sessions2 to be dropped
1289 $table = new xmldb_table('sessions2');
1290
1291 /// Conditionally launch drop table for sessions
1292 if ($dbman->table_exists($table)) {
1293 $dbman->drop_table($table);
1294 }
1295
1296 /// Define table sessions to be dropped
1297 $table = new xmldb_table('sessions');
1298
1299 /// Conditionally launch drop table for sessions
1300 if ($dbman->table_exists($table)) {
1301 $dbman->drop_table($table);
1302 }
1303
1304 /// Define table sessions to be created
1305 $table = new xmldb_table('sessions');
1306
1307 /// Adding fields to table sessions
2a88f626 1308 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1309 $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1310 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
1311 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1312 $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
1313 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1314 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1315 $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1316 $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
3f57bd45 1317
1318 /// Adding keys to table sessions
1319 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1320 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1321
1322 /// Adding indexes to table sessions
1323 $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1324 $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1325 $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1326 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1327
1328 /// Launch create table for sessions
1329 $dbman->create_table($table);
1330
1331 /// Main savepoint reached
b9fb7103 1332 upgrade_main_savepoint($result, 2009011900);
3f57bd45 1333 }
1334
dc6f76b9 1335 if ($result && $oldversion < 2009012901) {
795a08ad 1336 // NOTE: this table may already exist, see beginning of this file ;-)
1f20942c 1337
1338 /// Define table upgrade_log to be created
1339 $table = new xmldb_table('upgrade_log');
1340
1341 /// Adding fields to table upgrade_log
2a88f626 1342 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1343 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1344 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1345 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1346 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1347 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1348 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1349 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1350 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1f20942c 1351
1352 /// Adding keys to table upgrade_log
1353 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1354 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1355
1356 /// Adding indexes to table upgrade_log
1357 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1358 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
1359
1360 /// Conditionally launch create table for upgrade_log
1361 if (!$dbman->table_exists($table)) {
1362 $dbman->create_table($table);
1363 }
1364
1365 /// Main savepoint reached
dc6f76b9 1366 upgrade_main_savepoint($result, 2009012901);
1f20942c 1367 }
1caea91e 1368
75564228 1369 if ($result && $oldversion < 2009021800) {
eb2761f8 1370 // Converting format of grade conditions, if any exist, to percentages.
75564228 1371 $DB->execute("
1372UPDATE {course_modules_availability} SET grademin=(
1373 SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
eb2761f8 1374 /(gi.grademax-gi.grademin)
1375 FROM {grade_items} gi
75564228 1376 WHERE gi.id={course_modules_availability}.gradeitemid)
1377WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1378 $DB->execute("
1379UPDATE {course_modules_availability} SET grademax=(
1380 SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
eb2761f8 1381 /(gi.grademax-gi.grademin)
1382 FROM {grade_items} gi
75564228 1383 WHERE gi.id={course_modules_availability}.gradeitemid)
1384WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1385
1386 /// Main savepoint reached
1387 upgrade_main_savepoint($result, 2009021800);
1388 }
bc7ec91a 1389 if ($result && $oldversion < 2009021801) {
1390 /// Define field backuptype to be added to backup_log
382123a0 1391 $table = new xmldb_table('backup_log');
2a88f626 1392 $field = new xmldb_field('backuptype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'info');
bc7ec91a 1393 /// Conditionally Launch add field backuptype and set all old records as 'scheduledbackup' records.
1394 if (!$dbman->field_exists($table, $field)) {
1395 $dbman->add_field($table, $field);
1396 $DB->execute("UPDATE {backup_log} SET backuptype='scheduledbackup'");
1397 }
75564228 1398
bc7ec91a 1399 /// Main savepoint reached
1400 upgrade_main_savepoint($result, 2009021801);
1401 }
af52ecee 1402 /// Add default sort order for question types.
1403 if ($result && $oldversion < 2009030300) {
1404 set_config('multichoice_sortorder', 1, 'question');
1405 set_config('truefalse_sortorder', 2, 'question');
1406 set_config('shortanswer_sortorder', 3, 'question');
1407 set_config('numerical_sortorder', 4, 'question');
1408 set_config('calculated_sortorder', 5, 'question');
1409 set_config('essay_sortorder', 6, 'question');
1410 set_config('match_sortorder', 7, 'question');
1411 set_config('randomsamatch_sortorder', 8, 'question');
1412 set_config('multianswer_sortorder', 9, 'question');
1413 set_config('description_sortorder', 10, 'question');
1414 set_config('random_sortorder', 11, 'question');
1415 set_config('missingtype_sortorder', 12, 'question');
1416
1417 upgrade_main_savepoint($result, 2009030300);
1418 }
a75c78d3 1419 if ($result && $oldversion < 2009030501) {
1420 /// setup default repository plugins
1421 require_once($CFG->dirroot . '/repository/lib.php');
1422 repository_setup_default_plugins();
1423 /// Main savepoint reached
1424 upgrade_main_savepoint($result, 2009030501);
1425 }
af52ecee 1426
82701e24 1427 /// MDL-18132 replace the use a new Role allow switch settings page, instead of
1428 /// $CFG->allowuserswitchrolestheycantassign
1429 if ($result && $oldversion < 2009032000) {
1430 /// First create the new table.
1431 $table = new xmldb_table('role_allow_switch');
1432
1433 /// Adding fields to table role_allow_switch
2a88f626 1434 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1435 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1436 $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
82701e24 1437
1438 /// Adding keys to table role_allow_switch
1439 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1440 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
1441 $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
1442
1443 /// Adding indexes to table role_allow_switch
1444 $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1445
1446 /// Conditionally launch create table for role_allow_switch
1447 if (!$dbman->table_exists($table)) {
1448 $dbman->create_table($table);
1449 }
1450
1451 /// Main savepoint reached
1452 upgrade_main_savepoint($result, 2009032000);
1453 }
0fc9c009 1454
82701e24 1455 if ($result && $oldversion < 2009032001) {
1456 /// Copy from role_allow_assign into the new table.
dc740f97 1457 $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1458 SELECT roleid, allowassign FROM {role_allow_assign}');
82701e24 1459
1460 /// Unset the config variable used in 1.9.
1461 unset_config('allowuserswitchrolestheycantassign');
1462
1463 /// Main savepoint reached
1464 upgrade_main_savepoint($result, 2009032001);
1465 }
c94985ef 1466
1467 if ($result && $oldversion < 2009033100) {
1468 require_once("$CFG->dirroot/filter/tex/lib.php");
1469 filter_tex_updatedcallback(null);
1470 /// Main savepoint reached
1471 upgrade_main_savepoint($result, 2009033100);
1472 }
1473
0fc9c009 1474 if ($result && $oldversion < 2009040300) {
1475
1476 /// Define table filter_active to be created
1477 $table = new xmldb_table('filter_active');
1478
1479 /// Adding fields to table filter_active
2a88f626 1480 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1481 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1482 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1483 $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1484 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
0fc9c009 1485
1486 /// Adding keys to table filter_active
1487 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1488 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1489
1490 /// Adding indexes to table filter_active
1491 $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1492
1493 /// Conditionally launch create table for filter_active
1494 if (!$dbman->table_exists($table)) {
1495 $dbman->create_table($table);
1496 }
1497
1498 /// Main savepoint reached
1499 upgrade_main_savepoint($result, 2009040300);
1500 }
1501
1502 if ($result && $oldversion < 2009040301) {
1503
1504 /// Define table filter_config to be created
1505 $table = new xmldb_table('filter_config');
1506
1507 /// Adding fields to table filter_config
2a88f626 1508 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1509 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1510 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1511 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1512 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
0fc9c009 1513
1514 /// Adding keys to table filter_config
1515 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1516 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1517
1518 /// Adding indexes to table filter_config
1519 $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1520
1521 /// Conditionally launch create table for filter_config
1522 if (!$dbman->table_exists($table)) {
1523 $dbman->create_table($table);
1524 }
1525
1526 /// Main savepoint reached
1527 upgrade_main_savepoint($result, 2009040301);
1528 }
1529
c07e6d8d 1530 if ($result && $oldversion < 2009040302) {
1531 /// Transfer current settings from $CFG->textfilters
1532 $disabledfilters = filter_get_all_installed();
1533 if (empty($CFG->textfilters)) {
1534 $activefilters = array();
1535 } else {
1536 $activefilters = explode(',', $CFG->textfilters);
1537 }
1538 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1539 $sortorder = 1;
1540 foreach ($activefilters as $filter) {
1541 filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1542 $sortorder += 1;
1543 unset($disabledfilters[$filter]);
1544 }
1545 foreach ($disabledfilters as $filter => $notused) {
1546 filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1547 $sortorder += 1;
1548 }
1549
1550 /// Main savepoint reached
1551 upgrade_main_savepoint($result, 2009040302);
1552 }
1553
bbb45905 1554 if ($result && $oldversion < 2009040600) {
1555 /// Ensure that $CFG->stringfilters is set.
1556 if (empty($CFG->stringfilters)) {
1557 if (!empty($CFG->filterall)) {
1558 set_config('stringfilters', $CFG->textfilters);
1559 } else {
1560 set_config('stringfilters', '');
1561 }
1562 }
1563
1564 set_config('filterall', !empty($CFG->stringfilters));
1565 unset_config('textfilters');
1566
1567 /// Main savepoint reached
1568 upgrade_main_savepoint($result, 2009040600);
1569 }
1570
e7c6bf0e 1571 if ($result && $oldversion < 2009041700) {
1572 /// To ensure the UI remains consistent with no behaviour change, any
1573 /// 'until' date in an activity condition should have 1 second subtracted
1574 /// (to go from 0:00 on the following day to 23:59 on the previous one).
1575 $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
6861b6fe 1576 require_once($CFG->dirroot . '/course/lib.php');
0f9534c1 1577 rebuild_course_cache(0, true);
e7c6bf0e 1578
1579 /// Main savepoint reached
1580 upgrade_main_savepoint($result, 2009041700);
1581 }
1582
6bdf4c99 1583 if ($result && $oldversion < 2009042600) {
1584 /// Deleting orphaned messages from deleted users.
1585 require_once($CFG->dirroot.'/message/lib.php');
1586 /// Detect deleted users with messages sent(useridfrom) and not read
1587 if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
1588 FROM {user} u
1589 JOIN {message} m ON m.useridfrom = u.id
1590 WHERE u.deleted = ?', array(1))) {
1591 foreach ($deletedusers as $deleteduser) {
1592 message_move_userfrom_unread2read($deleteduser->id); // move messages
1593 }
1594 }
1595 /// Main savepoint reached
1596 upgrade_main_savepoint($result, 2009042600);
1597 }
1598
e37cd84a 1599 /// Dropping all enums/check contraints from core. MDL-18577
1600 if ($result && $oldversion < 2009042700) {
1601
1602 /// Changing list of values (enum) of field stattype on table stats_daily to none
1603 $table = new xmldb_table('stats_daily');
2a88f626 1604 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
e37cd84a 1605
1606 /// Launch change of list of values for field stattype
2a88f626 1607 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1608
1609 /// Changing list of values (enum) of field stattype on table stats_weekly to none
1610 $table = new xmldb_table('stats_weekly');
2a88f626 1611 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
e37cd84a 1612
1613 /// Launch change of list of values for field stattype
2a88f626 1614 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1615
1616 /// Changing list of values (enum) of field stattype on table stats_monthly to none
1617 $table = new xmldb_table('stats_monthly');
2a88f626 1618 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
e37cd84a 1619
1620 /// Launch change of list of values for field stattype
2a88f626 1621 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1622
1623 /// Changing list of values (enum) of field publishstate on table post to none
1624 $table = new xmldb_table('post');
2a88f626 1625 $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
e37cd84a 1626
1627 /// Launch change of list of values for field publishstate
2a88f626 1628 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1629
1630 /// Main savepoint reached
1631 upgrade_main_savepoint($result, 2009042700);
1632 }
1633
c045e45a 1634 if ($result && $oldversion < 2009043000) {
1635 unset_config('grade_report_showgroups');
1636 upgrade_main_savepoint($result, 2009043000);
1637 }
1638
1639 if ($result && $oldversion < 2009050600) {
e88462a0 1640 /// Site front page blocks need to be moved due to page name change.
1641 $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
1642
1643 /// Main savepoint reached
c045e45a 1644 upgrade_main_savepoint($result, 2009050600);
e88462a0 1645 }
1646
c045e45a 1647 if ($result && $oldversion < 2009050601) {
66b10689 1648
3aceb097 1649 /// Define table block_instance to be renamed to block_instances
66b10689 1650 $table = new xmldb_table('block_instance');
1651
1652 /// Launch rename table for block_instance
3aceb097 1653 $dbman->rename_table($table, 'block_instances');
66b10689 1654
1655 /// Main savepoint reached
c045e45a 1656 upgrade_main_savepoint($result, 2009050601);
66b10689 1657 }
1658
c045e45a 1659 if ($result && $oldversion < 2009050602) {
66b10689 1660
1661 /// Define table block_instance to be renamed to block_instance_old
1662 $table = new xmldb_table('block_pinned');
1663
1664 /// Launch rename table for block_instance
1665 $dbman->rename_table($table, 'block_pinned_old');
1666
1667 /// Main savepoint reached
c045e45a 1668 upgrade_main_savepoint($result, 2009050602);
66b10689 1669 }
1670
c045e45a 1671 if ($result && $oldversion < 2009050603) {
66b10689 1672
1673 /// Define table block_instance_old to be created
1674 $table = new xmldb_table('block_instance_old');
1675
1676 /// Adding fields to table block_instance_old
2b212777 1677 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1678 $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1679 $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1680 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1681 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
1682 $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
1683 $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
1684 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1685 $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
66b10689 1686
1687 /// Adding keys to table block_instance_old
1688 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1689 $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
1690
1691 /// Adding indexes to table block_instance_old
1692 $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1693 $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
1694
1695 /// Conditionally launch create table for block_instance_old
1696 if (!$dbman->table_exists($table)) {
1697 $dbman->create_table($table);
1698 }
1699
1700 /// Main savepoint reached
c045e45a 1701 upgrade_main_savepoint($result, 2009050603);
66b10689 1702 }
1703
c045e45a 1704 if ($result && $oldversion < 2009050604) {
66b10689 1705 /// Copy current blocks data from block_instances to block_instance_old
35550fd6 1706 $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
1707 SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
66b10689 1708
c045e45a 1709 upgrade_main_savepoint($result, 2009050604);
66b10689 1710 }
1711
c045e45a 1712 if ($result && $oldversion < 2009050605) {
66b10689 1713
1714 /// Define field multiple to be dropped from block
1715 $table = new xmldb_table('block');
1716 $field = new xmldb_field('multiple');
1717
1718 /// Conditionally launch drop field multiple
1719 if ($dbman->field_exists($table, $field)) {
1720 $dbman->drop_field($table, $field);
1721 }
1722
1723 /// Main savepoint reached
c045e45a 1724 upgrade_main_savepoint($result, 2009050605);
66b10689 1725 }
1726
c045e45a 1727 if ($result && $oldversion < 2009050606) {
66b10689 1728 $table = new xmldb_table('block_instances');
1729
1730 /// Rename field weight on table block_instances to defaultweight
2b212777 1731 $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'position');
66b10689 1732 $dbman->rename_field($table, $field, 'defaultweight');
1733
1734 /// Rename field position on table block_instances to defaultregion
2b212777 1735 $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
66b10689 1736 $dbman->rename_field($table, $field, 'defaultregion');
1737
1738 /// Main savepoint reached
c045e45a 1739 upgrade_main_savepoint($result, 2009050606);
66b10689 1740 }
1741
c045e45a 1742 if ($result && $oldversion < 2009050607) {
66b10689 1743 /// Changing precision of field defaultregion on table block_instances to (16)
a26146d5 1744 $table = new xmldb_table('block_instances');
1745 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
66b10689 1746
1747 /// Launch change of precision for field defaultregion
1748 $dbman->change_field_precision($table, $field);
1749
1750 /// Main savepoint reached
a26146d5 1751 upgrade_main_savepoint($result, 2009050607);
66b10689 1752 }
1753
c045e45a 1754 if ($result && $oldversion < 2009050608) {
66b10689 1755 /// Change regions to the new notation
1756 $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1757 $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1758 $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1759 // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1760 // flex page course format. Hopefully this new value is an adequate alternative.
1761
1762 /// Main savepoint reached
c045e45a 1763 upgrade_main_savepoint($result, 2009050608);
66b10689 1764 }
1765
c045e45a 1766 if ($result && $oldversion < 2009050609) {
66b10689 1767
1768 /// Define key blockname (unique) to be added to block
1769 $table = new xmldb_table('block');
1770 $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1771
1772 /// Launch add key blockname
1773 $dbman->add_key($table, $key);
1774
1775 /// Main savepoint reached
c045e45a 1776 upgrade_main_savepoint($result, 2009050609);
66b10689 1777 }
1778
c045e45a 1779 if ($result && $oldversion < 2009050610) {
66b10689 1780 $table = new xmldb_table('block_instances');
1781
1782 /// Define field blockname to be added to block_instances
2b212777 1783 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
66b10689 1784 if (!$dbman->field_exists($table, $field)) {
1785 $dbman->add_field($table, $field);
1786 }
1787
1788 /// Define field contextid to be added to block_instances
2b212777 1789 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
66b10689 1790 if (!$dbman->field_exists($table, $field)) {
1791 $dbman->add_field($table, $field);
1792 }
1793
1794 /// Define field showinsubcontexts to be added to block_instances
2b212777 1795 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
66b10689 1796 if (!$dbman->field_exists($table, $field)) {
1797 $dbman->add_field($table, $field);
1798 }
1799
1800 /// Define field subpagepattern to be added to block_instances
2b212777 1801 $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
66b10689 1802 if (!$dbman->field_exists($table, $field)) {
1803 $dbman->add_field($table, $field);
1804 }
1805
1806 /// Main savepoint reached
c045e45a 1807 upgrade_main_savepoint($result, 2009050610);
66b10689 1808 }
1809
c045e45a 1810 if ($result && $oldversion < 2009050611) {
66b10689 1811 $table = new xmldb_table('block_instances');
1812
1813 /// Fill in blockname from blockid
1814 $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1815
1816 /// Set showinsubcontexts = 0 for all rows.
1817 $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1818
1819 /// Main savepoint reached
c045e45a 1820 upgrade_main_savepoint($result, 2009050611);
66b10689 1821 }
1822
c045e45a 1823 if ($result && $oldversion < 2009050612) {
66b10689 1824
1825 /// Rename field pagetype on table block_instances to pagetypepattern
1826 $table = new xmldb_table('block_instances');
2b212777 1827 $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
66b10689 1828
1829 /// Launch rename field pagetype
1830 $dbman->rename_field($table, $field, 'pagetypepattern');
1831
1832 /// Main savepoint reached
c045e45a 1833 upgrade_main_savepoint($result, 2009050612);
66b10689 1834 }
1835
c045e45a 1836 if ($result && $oldversion < 2009050613) {
66b10689 1837 /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1838
1839 /// site-index
1840 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
eb2761f8 1841 $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1842 pagetypepattern = 'site-index',
1843 subpagepattern = NULL
1844 WHERE pagetypepattern = 'site-index'");
66b10689 1845
1846 /// course-view
eb2761f8 1847 $DB->execute("UPDATE {block_instances} SET
1848 contextid = (
1849 SELECT {context}.id
1850 FROM {context}
1851 JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1852 WHERE {course}.id = pageid
1853 ),
1854 pagetypepattern = 'course-view-*',
1855 subpagepattern = NULL
1856 WHERE pagetypepattern = 'course-view'");
66b10689 1857
1858 /// admin
1859 $syscontext = get_context_instance(CONTEXT_SYSTEM);
eb2761f8 1860 $DB->execute("UPDATE {block_instances} SET
1861 contextid = " . $syscontext->id . ",
1862 pagetypepattern = 'admin-*',
1863 subpagepattern = NULL
1864 WHERE pagetypepattern = 'admin'");
66b10689 1865
1866 /// my-index
eb2761f8 1867 $DB->execute("UPDATE {block_instances} SET
1868 contextid = (
1869 SELECT {context}.id
1870 FROM {context}
1871 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1872 WHERE {user}.id = pageid
1873 ),
1874 pagetypepattern = 'my-index',
1875 subpagepattern = NULL
1876 WHERE pagetypepattern = 'my-index'");
66b10689 1877
1878 /// tag-index
eb2761f8 1879 $DB->execute("UPDATE {block_instances} SET
1880 contextid = " . $syscontext->id . ",
1881 pagetypepattern = 'tag-index',
1882 subpagepattern = pageid
1883 WHERE pagetypepattern = 'tag-index'");
66b10689 1884
1885 /// blog-view
eb2761f8 1886 $DB->execute("UPDATE {block_instances} SET
1887 contextid = (
1888 SELECT {context}.id
1889 FROM {context}
1890 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1891 WHERE {user}.id = pageid
1892 ),
1893 pagetypepattern = 'blog-index',
1894 subpagepattern = NULL
1895 WHERE pagetypepattern = 'blog-view'");
66b10689 1896
1897 /// mod-xxx-view
1898 $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1899 foreach ($moduleswithblocks as $modname) {
1900 if (!$dbman->table_exists($modname)) {
1901 continue;
1902 }
eb2761f8 1903 $DB->execute("UPDATE {block_instances} SET
1904 contextid = (
1905 SELECT {context}.id
1906 FROM {context}
1907 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1908 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1909 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1910 WHERE {{$modname}}.id = pageid
1911 ),
1912 pagetypepattern = 'blog-index',
1913 subpagepattern = NULL
1914 WHERE pagetypepattern = 'blog-view'");
66b10689 1915 }
1916
1917 /// Main savepoint reached
c045e45a 1918 upgrade_main_savepoint($result, 2009050613);
66b10689 1919 }
1920
c045e45a 1921 if ($result && $oldversion < 2009050614) {
66b10689 1922 /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
bf659cf0 1923 $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
66b10689 1924
1925 /// Main savepoint reached
c045e45a 1926 upgrade_main_savepoint($result, 2009050614);
66b10689 1927 }
1928
c045e45a 1929 if ($result && $oldversion < 2009050615) {
66b10689 1930 $table = new xmldb_table('block_instances');
1931
1932 /// Changing nullability of field blockname on table block_instances to not null
2b212777 1933 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
66b10689 1934 $dbman->change_field_notnull($table, $field);
1935
1936 /// Changing nullability of field contextid on table block_instances to not null
2b212777 1937 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
66b10689 1938 $dbman->change_field_notnull($table, $field);
1939
1940 /// Changing nullability of field showinsubcontexts on table block_instances to not null
2b212777 1941 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
66b10689 1942 $dbman->change_field_notnull($table, $field);
1943
1944 /// Main savepoint reached
c045e45a 1945 upgrade_main_savepoint($result, 2009050615);
66b10689 1946 }
1947
c045e45a 1948 if ($result && $oldversion < 2009050616) {
66b10689 1949 /// Add exiting sticky blocks.
1950 $blocks = $DB->get_records('block');
1951 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1952 $newregions = array(
1953 'l' => 'side-pre',
1954 'r' => 'side-post',
1955 'c' => 'course-view-top',
1956 );
1957 $stickyblocks = $DB->get_recordset('block_pinned_old');
1958 foreach ($stickyblocks as $stickyblock) {
93d3a250 1959 $newblock = new object();
35fb85a7 1960 $newblock->blockname = $blocks[$stickyblock->blockid]->name;
66b10689 1961 $newblock->contextid = $syscontext->id;
1962 $newblock->showinsubcontexts = 1;
1963 switch ($stickyblock->pagetype) {
1964 case 'course-view':
1965 $newblock->pagetypepattern = 'course-view-*';
1966 break;
1967 default:
1968 $newblock->pagetypepattern = $stickyblock->pagetype;
1969 }
1970 $newblock->defaultregion = $newregions[$stickyblock->position];
1971 $newblock->defaultweight = $stickyblock->weight;
1972 $newblock->configdata = $stickyblock->configdata;
01de485d 1973 $newblock->visible = 1;
66b10689 1974 $DB->insert_record('block_instances', $newblock);
1975 }
1976
1977 /// Main savepoint reached
c045e45a 1978 upgrade_main_savepoint($result, 2009050616);
66b10689 1979 }
1980
c045e45a 1981 if ($result && $oldversion < 2009050617) {
66b10689 1982
1983 /// Define table block_positions to be created
1984 $table = new xmldb_table('block_positions');
1985
1986 /// Adding fields to table block_positions
2b212777 1987 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1988 $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1989 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1990 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1991 $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1992 $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1');
1993 $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1994 $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
66b10689 1995
1996 /// Adding keys to table block_positions
1997 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1998 $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
1999 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2000
2001 /// Adding indexes to table block_positions
2002 $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
2003
2004 /// Conditionally launch create table for block_positions
2005 if (!$dbman->table_exists($table)) {
2006 $dbman->create_table($table);
2007 }
2008
2009 /// Main savepoint reached
c045e45a 2010 upgrade_main_savepoint($result, 2009050617);
66b10689 2011 }
2012
c045e45a 2013 if ($result && $oldversion < 2009050618) {
66b10689 2014 /// And block instances with visible = 0, copy that information to block_positions
2015 $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
2016 SELECT id, contextid,
2017 CASE WHEN pagetypepattern = 'course-view-*' THEN
2018 (SELECT " . $DB->sql_concat("'course-view-'", 'format') . "
2019 FROM {course}
2020 JOIN {context} ON {course}.id = {context}.instanceid
2021 WHERE {context}.id = contextid)
2022 ELSE pagetypepattern END,
2023 CASE WHEN subpagepattern IS NULL THEN ''
2024 ELSE subpagepattern END,
2025 0, defaultregion, defaultweight
2026 FROM {block_instances} WHERE visible = 0 AND pagetypepattern <> 'admin-*'");
2027
2028 /// Main savepoint reached
c045e45a 2029 upgrade_main_savepoint($result, 2009050618);
66b10689 2030 }
2031
c045e45a 2032 if ($result && $oldversion < 2009050619) {
66b10689 2033 $table = new xmldb_table('block_instances');
2034
2035 /// Define field blockid to be dropped from block_instances
2036 $field = new xmldb_field('blockid');
2037 if ($dbman->field_exists($table, $field)) {
4276a19a 2038 /// Before dropping the field, drop dependent indexes
2039 $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
2040 if ($dbman->index_exists($table, $index)) {
2041 /// Launch drop index blockid
2042 $dbman->drop_index($table, $index);
2043 }
66b10689 2044 $dbman->drop_field($table, $field);
2045 }
2046
2047 /// Define field pageid to be dropped from block_instances
2048 $field = new xmldb_field('pageid');
2049 if ($dbman->field_exists($table, $field)) {
4276a19a 2050 /// Before dropping the field, drop dependent indexes
2051 $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
2052 if ($dbman->index_exists($table, $index)) {
2053 /// Launch drop index pageid
2054 $dbman->drop_index($table, $index);
2055 }
66b10689 2056 $dbman->drop_field($table, $field);
2057 }
2058
2059 /// Define field visible to be dropped from block_instances
2060 $field = new xmldb_field('visible');
2061 if ($dbman->field_exists($table, $field)) {
2062 $dbman->drop_field($table, $field);
2063 }
2064
2065 /// Main savepoint reached
c045e45a 2066 upgrade_main_savepoint($result, 2009050619);
35079f53 2067 }
2068
ecdf5d17 2069 if ($result && $oldversion < 2009051200) {
2070 /// Let's check the status of mandatory mnet_host records, fixing them
2071 /// and moving "orphan" users to default localhost record. MDL-16879
aa9a6867 2072 echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
ecdf5d17 2073 upgrade_fix_incorrect_mnethostids();
2074
2075 /// Main savepoint reached
2076 upgrade_main_savepoint($result, 2009051200);
2077 }
2078
a5747cf8 2079
2080 if ($result && $oldversion < 2009051700) {
2081 /// migrate editor settings
2082 if (empty($CFG->htmleditor)) {
2083 set_config('texteditors', 'textarea');
2084 } else {
2085 set_config('texteditors', 'tinymce,textarea');
2086 }
2087
2088 unset_config('htmleditor');
2089 unset_config('defaulthtmleditor');
2090
2091 /// Main savepoint reached
2092 upgrade_main_savepoint($result, 2009051700);
2093 }
2094
1aa01caf 2095 if ($result && $oldversion < 2009060200) {
2096 /// Define table files_cleanup to be dropped - not needed
2097 $table = new xmldb_table('files_cleanup');
2098
2099 /// Conditionally launch drop table for files_cleanup
2100 if ($dbman->table_exists($table)) {
2101 $dbman->drop_table($table);
2102 }
2103
2104 /// Main savepoint reached
2105 upgrade_main_savepoint($result, 2009060200);
2106 }
2107
b4154c2d 2108 if ($result && $oldversion < 2009061300) {
2109 //TODO: copy this to the very beginning of this upgrade script so that we may log upgrade queries
2110
2111 /// Define table log_queries to be created
2112 $table = new xmldb_table('log_queries');
2113
2114 /// Adding fields to table log_queries
2115 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2116 $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2117 $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
2118 $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
2119 $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2120 $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2121 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2122 $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
2123 $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2124
2125 /// Adding keys to table log_queries
2126 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2127
2128 /// Conditionally launch create table for log_queries
2129 if (!$dbman->table_exists($table)) {
2130 $dbman->create_table($table);
2131 }
2132
2133 /// Main savepoint reached
2134 upgrade_main_savepoint($result, 2009061300);
2135 }
2136
52902cea 2137 /// Repeat 2009050607 upgrade step, which Petr commented out becuase of XMLDB
2138 /// stupidity, so lots of peopel will have missed.
2139 if ($result && $oldversion < 2009061600) {
2140 /// Changing precision of field defaultregion on table block_instances to (16)
2141 $table = new xmldb_table('block_instances');
2142 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
2143
2144 /// Launch change of precision for field defaultregion
2145 $dbman->change_field_precision($table, $field);
2146
2147 /// Main savepoint reached
2148 upgrade_main_savepoint($result, 2009061600);
2149 }
2150
17da2e6f 2151 if ($result && $oldversion < 2009061702) {
2152 // standardizing plugin names
2153 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
2154 foreach ($configs as $config) {
ce11763a 2155 $result = $result && unset_config($config->name, $config->plugin); /// unset old config
17da2e6f 2156 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
ce11763a 2157 $result = $result && set_config($config->name, $config->value, $config->plugin); /// set new config
17da2e6f 2158 }
2159 }
2160 unset($configs);
2161 upgrade_main_savepoint($result, 2009061702);
2162 }
2163
2164 if ($result && $oldversion < 2009061703) {
2165 // standardizing plugin names
2166 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
2167 foreach ($configs as $config) {
ce11763a 2168 $result = $result && unset_config($config->name, $config->plugin); /// unset old config
17da2e6f 2169 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
ce11763a 2170 $result = $result && set_config($config->name, $config->value, $config->plugin); /// set new config
17da2e6f 2171 }
2172 }
2173 unset($configs);
2174 upgrade_main_savepoint($result, 2009061703);
2175 }
2176
2177 if ($result && $oldversion < 2009061704) {
13a0d3d3 2178 // change component string in capability records to new "_" format
17da2e6f 2179 if ($caps = $DB->get_records('capabilities')) {
2180 foreach ($caps as $cap) {
2181 $cap->component = str_replace('/', '_', $cap->component);
2182 $DB->update_record('capabilities', $cap);
2183 }
2184 }
2185 unset($caps);
2186 upgrade_main_savepoint($result, 2009061704);
2187 }
2188
2189 if ($result && $oldversion < 2009061705) {
13a0d3d3 2190 // change component string in events_handlers records to new "_" format
17da2e6f 2191 if ($handlers = $DB->get_records('events_handlers')) {
2192 foreach ($handlers as $handler) {
2193 $handler->handlermodule = str_replace('/', '_', $handler->handlermodule);
2194 $DB->update_record('events_handlers', $handler);
2195 }
2196 }
2197 unset($handlers);
2198 upgrade_main_savepoint($result, 2009061705);
2199 }
2200
2201 if ($result && $oldversion < 2009061706) {
13a0d3d3 2202 // change component string in message_providers records to new "_" format
17da2e6f 2203 if ($mps = $DB->get_records('message_providers')) {
2204 foreach ($mps as $mp) {
2205 $mp->component = str_replace('/', '_', $mp->component);
2206 $DB->update_record('message_providers', $cap);
2207 }
2208 }
2209 unset($caps);
2210 upgrade_main_savepoint($result, 2009061706);
2211 }
13a0d3d3 2212
adf8f3f9 2213 if ($result && $oldversion < 2009063000) {
2214 // upgrade format of _with_advanced settings - quiz only
2215 // note: this can be removed later, not needed for upgrades from 1.9.x
c1f58efe 2216 if ($quiz = get_config('quiz')) {
2217 foreach ($quiz as $name=>$value) {
2218 if (strpos($name, 'fix_') !== 0) {
2219 continue;
2220 }
2221 $newname = substr($name,4).'_adv';
2222 set_config($newname, $value, 'quiz');
2223 unset_config($name, 'quiz');
adf8f3f9 2224 }
13a0d3d3 2225 }
adf8f3f9 2226 upgrade_main_savepoint($result, 2009063000);
2227 }
13a0d3d3 2228
13a0d3d3 2229 if ($result && $oldversion < 2009071000) {
2230
2231 /// Rename field contextid on table block_instances to parentcontextid
2232 $table = new xmldb_table('block_instances');
2233 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
2234
2235 /// Launch rename field parentcontextid
2236 $dbman->rename_field($table, $field, 'parentcontextid');
2237
2238 /// Main savepoint reached
2239 upgrade_main_savepoint($result, 2009071000);
2240 }
2241
e92c286c 2242 if ($result && $oldversion < 2009071300) {
2243
2244 /// Create contexts for every block. In the past, only non-sticky course block had contexts.
2245 /// This is a copy of the code in create_contexts.
2246 $sql = "INSERT INTO {context} (contextlevel, instanceid)
2247 SELECT " . CONTEXT_BLOCK . ", bi.id
2248 FROM {block_instances} bi
2249 WHERE NOT EXISTS (SELECT 'x'
2250 FROM {context} ctx
2251 WHERE bi.id = ctx.instanceid AND ctx.contextlevel=" . CONTEXT_BLOCK . ")";
2252 $DB->execute($sql);
2253
2254 /// TODO MDL-19776 We should not really use API funcitons in upgrade.
2255 /// If MDL-19776 is done, we can remove this whole upgrade block.
2256 build_context_path();
2257
2258 /// Main savepoint reached
2259 upgrade_main_savepoint($result, 2009071300);
2260 }
1918f7e0 2261
cae83708 2262 if ($result && $oldversion < 2009071600) {
2263
2264 /// Define field summaryformat to be added to post
2265 $table = new xmldb_table('post');
2266 $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
2267
2268 /// Conditionally launch add field summaryformat
2269 if (!$dbman->field_exists($table, $field)) {
2270 $dbman->add_field($table, $field);
2271 }
2272
2273 /// Main savepoint reached
2274 upgrade_main_savepoint($result, 2009071600);
2275 }
e92c286c 2276
1bcb7eb5 2277 if ($result && $oldversion < 2009072400) {
2278
2279 /// Define table comments to be created
2280 $table = new xmldb_table('comments');
2281
2282 /// Adding fields to table comments
2283 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2284 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2285 $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2286 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2287 $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2288 $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2289 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2290 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2291
2292 /// Adding keys to table comments
2293 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2294
2295 /// Conditionally launch create table for comments
2296 if (!$dbman->table_exists($table)) {
2297 $dbman->create_table($table);
2298 }
0a127169 2299
1bcb7eb5 2300 /// Main savepoint reached
2301 upgrade_main_savepoint($result, 2009072400);
2302 }
2303
7d2a0492 2304 /**
2305 * This upgrade is to set up the new navigation blocks that have been developed
2306 * as part of Moodle 2.0
2307 * Now I [Sam Hemelryk] hit a conundrum while exploring how to go about this
2308 * as not only do we want to install the new blocks but we also want to set up
2309 * default instances of them, and at the same time remove instances of the blocks
2310 * that were/will-be outmoded by the two new navigation blocks.
2311 * After talking it through with Tim Hunt {@link http://moodle.org/mod/cvsadmin/view.php?conversationid=3112}
2312 * we decided that the best way to go about this was to put the bulk of the
2313 * upgrade operation into core upgrade `here` but to let the plugins block
2314 * still install the blocks.
2315 * This leaves one hairy end in that we will create block_instances within the
2316 * DB before the blocks themselves are created within the DB
2317 */
2318 if ($result && $oldversion < 2009082800) {
2319
2320 echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2321
2322 // Get the system context so we can set the block instances to it
2323 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2324
2325 // An array to contain the new block instances we will create
2326 $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
2327 // The new global navigation block instance as a stdClass
2328 $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
2329 $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
2330 $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutly everywhere
2331 $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
2332 $newblockinstances['globalnavigation']->subpagetypepattern = null;
2333 $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
2334 $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
2335 $newblockinstances['globalnavigation']->configdata = '';
2336 // The new settings navigation block instance as a stdClass
2337 $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
2338 $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
2339 $newblockinstances['settingsnavigation']->showinsubcontexts = true;
2340 $newblockinstances['settingsnavigation']->pagetypepattern = '*';
2341 $newblockinstances['settingsnavigation']->subpagetypepattern = null;
2342 $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
2343 $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
2344 $newblockinstances['settingsnavigation']->configdata = '';
2345
2346 // Blocks that are outmoded and for whom the bells will toll... by which I
2347 // mean we will delete all instances of
2348 $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
2349 $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
2350 unset($outmodedblocks);
2351 // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
2352 // cut down the number of delete queries we will need to run
2353 $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
1918f7e0 2354
7d2a0492 2355 $contextids = array();
2356 $instanceids = array();
2357 // Iterate through all block instances
2358 foreach ($allblockinstances as $blockinstance) {
2359 if (!in_array($blockinstance->parentcontextid, $contextids)) {
2360 $contextids[] = $blockinstance->parentcontextid;
2361
2362 // If we have over 1000 contexts clean them up and reset the array
2363 // this ensures we don't hit any nasty memory limits or such
2364 if (count($contextids) > 1000) {
2365 $result = $result && upgrade_cleanup_unwanted_block_contexts($contextids);
2366 $contextids = array();
2367 }
2368 }
2369 if (!in_array($blockinstance->id, $instanceids)) {
2370 $instanceids[] = $blockinstance->id;
2371 // If we have more than 1000 block instances now remove all block positions
2372 // and empty the array
2373 if (count($contextids) > 1000) {
2374 $instanceidstring = join(',',$instanceids);
2375 $result = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2376 $instanceids = array();
2377 }
2378 }
2379 }
2380
2381 $result = $result && upgrade_cleanup_unwanted_block_contexts($contextids);
2382
2383 $instanceidstring = join(',',$instanceids);
2384 $outcome1 = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
1918f7e0 2385
7d2a0492 2386 unset($allblockinstances);
2387 unset($contextids);
2388 unset($instanceids);
2389 unset($instanceidstring);
1918f7e0 2390
7d2a0492 2391 // Now remove the actual block instance
2392 $result = $result && $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
2393 unset($outmodedblocksstring);
2394
2395 // Insert the new block instances. Remember they have not been installed yet
2396 // however this should not be a problem
2397 foreach ($newblockinstances as $blockinstance) {
2398 $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
2399 // Ensure the block context is created.
2400 get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
2401 }
2402 unset($newblockinstances);
1918f7e0 2403
7d2a0492 2404 upgrade_main_savepoint($result, 2009082800);
2405 // The end of the navigation upgrade
2406 }
2407
b89e4ad8 2408 if ($result && $oldversion < 2009090800){
2409 //insert new record for log_display table
2410 //used to record tag update.
2411 if (!$DB->record_exists('log_display', array('action'=>'update', 'module'=>'tag'))) {
2412 $log_action = new object();
2413 $log_action->module = 'tag';
2414 $log_action->action = 'update';
2415 $log_action->mtable = 'tag';
2416 $log_action->field = 'name';
2417
2418 $result = $result && $DB->insert_record('log_display', $log_action);
2419 }
2420 upgrade_main_savepoint($result, 2009090800);
2421 }
4313fa89 2422
88af54fa 2423 if ($result && $oldversion < 2009100601) {
5d820ca2 2424 // drop all previous tables defined during the dev phase
5b9a70a1 2425 $dropold = array('external_services_users', 'external_services_functions', 'external_services', 'external_functions');
5d820ca2 2426 foreach ($dropold as $tablename) {
2427 $table = new xmldb_table($tablename);
2428 if ($dbman->table_exists($table)) {
2429 $dbman->drop_table($table);
2430 }
4313fa89 2431 }
88af54fa 2432 upgrade_main_savepoint($result, 2009100601);
5d820ca2 2433 }
4313fa89 2434
88af54fa 2435 if ($result && $oldversion < 2009100602) {
4313fa89 2436 /// Define table external_functions to be created
2437 $table = new xmldb_table('external_functions');
2438
2439 /// Adding fields to table external_functions
2440 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
5d820ca2 2441 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2442 $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2443 $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2444 $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
4313fa89 2445 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
4313fa89 2446
2447 /// Adding keys to table external_functions
2448 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2449
5d820ca2 2450 /// Adding indexes to table external_functions
2451 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2452
2453 /// Launch create table for external_functions
2454 $dbman->create_table($table);
4313fa89 2455
5d820ca2 2456 /// Main savepoint reached
88af54fa 2457 upgrade_main_savepoint($result, 2009100602);
5d820ca2 2458 }
4313fa89 2459
88af54fa 2460 if ($result && $oldversion < 2009100603) {
4313fa89 2461 /// Define table external_services to be created
2462 $table = new xmldb_table('external_services');
2463
2464 /// Adding fields to table external_services
2465 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
5d820ca2 2466 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2467 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2468 $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
2469 $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2470 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
4313fa89 2471
2472 /// Adding keys to table external_services
2473 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2474
5b9a70a1 2475 /// Adding indexes to table external_services
2476 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2477
5d820ca2 2478 /// Launch create table for external_services
2479 $dbman->create_table($table);
4313fa89 2480
5d820ca2 2481 /// Main savepoint reached
88af54fa 2482 upgrade_main_savepoint($result, 2009100603);
5d820ca2 2483 }
4313fa89 2484
88af54fa 2485 if ($result && $oldversion < 2009100604) {
4313fa89 2486 /// Define table external_services_functions to be created
2487 $table = new xmldb_table('external_services_functions');
2488
2489 /// Adding fields to table external_services_functions
2490 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2491 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
5d820ca2 2492 $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
4313fa89 2493
2494 /// Adding keys to table external_services_functions
2495 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
5d820ca2 2496 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
4313fa89 2497
5d820ca2 2498 /// Launch create table for external_services_functions
2499 $dbman->create_table($table);
2500
2501 /// Main savepoint reached
88af54fa 2502 upgrade_main_savepoint($result, 2009100604);
5d820ca2 2503 }
2504
88af54fa 2505 if ($result && $oldversion < 2009100605) {
5d820ca2 2506 /// Define table external_services_users to be created
2507 $table = new xmldb_table('external_services_users');
2508
2509 /// Adding fields to table external_services_users
2510 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2511 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2512 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
88af54fa 2513 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2514 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2515 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
5d820ca2 2516
2517 /// Adding keys to table external_services_users
2518 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2519 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2520 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2521
2522 /// Launch create table for external_services_users
2523 $dbman->create_table($table);
4313fa89 2524
2525 /// Main savepoint reached
88af54fa 2526 upgrade_main_savepoint($result, 2009100605);
4313fa89 2527 }
2528
1942103f
PS
2529 if ($result && $oldversion < 2009102600) {
2530
2531 /// Define table external_tokens to be created
2532 $table = new xmldb_table('external_tokens');
2533
2534 /// Adding fields to table external_tokens
2535 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2536 $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
2537 $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2538 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2539 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2540 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
2541 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2542 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2543 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2544 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2545 $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2546
2547 /// Adding keys to table external_tokens
2548 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2549 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2550 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2551 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2552
2553 /// Launch create table for external_tokens
2554 $dbman->create_table($table);
2555
2556 /// Main savepoint reached
2557 upgrade_main_savepoint($result, 2009102600);
2558 }
2559
cc82aef6
NC
2560 if ($result && $oldversion < 2009103000) {
2561
2562 /// Define table blog_association to be created
2563 $table = new xmldb_table('blog_association');
2564
2565 /// Adding fields to table blog_association
2566 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2567 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2568 $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2569
2570 /// Adding keys to table blog_association
2571 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2572 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2573 $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
2574
2575 /// Conditionally launch create table for blog_association
2576 if (!$dbman->table_exists($table)) {
2577 $dbman->create_table($table);
2578 }
2579
2580/// Define table blog_external to be created
2581 $table = new xmldb_table('blog_external');
2582
2583 /// Adding fields to table blog_external
2584 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2585 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2586 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2587 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2588 $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2589 $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2590 $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2591 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2592 $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2593
2594 /// Adding keys to table blog_external
2595 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2596 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2597
2598 /// Conditionally launch create table for blog_external
2599 if ($dbman->table_exists($table)) {
2600 // Delete the existing one first (comes from early dev version)
2601 $dbman->drop_table($table);
2602 }
2603 $dbman->create_table($table);
2604
2605 // Print notice about need to upgrade bloglevel
2606 if (($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete)) {
2607 echo $OUTPUT->notification(get_string('bloglevelupgradenotice', 'admin'));
2608
2609 // email admins about the need to upgrade their system using the admin/bloglevelupgrade.php script
2610 $admins = get_admins();
2611 $site = get_site();
2612
2613 $a = new StdClass;
2614 $a->sitename = $site->fullname;
2615 $a->fixurl = "$CFG->wwwroot/$CFG->admin/bloglevelupgrade.php";
2616
c01678b1
PS
2617 $subject = get_string('bloglevelupgrade', 'admin');
2618 $description = get_string('bloglevelupgradedescription', 'admin', $a);
2619
2620 // can not use messaging here because it is not configured yet!
2621 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
cc82aef6
NC
2622 }
2623 /// Main savepoint reached
2624 upgrade_main_savepoint($result, 2009103000);
2625 }
117bd748 2626
8bdc9cac
SH
2627 if ($result && $oldversion < 2009110400) {
2628
2629 // An array used to store the table name and keys of summary and trust fields
2630 // to be added
2631 $extendtables = array();
2632 $extendtables['course'] = array('summaryformat');
2633 $extendtables['course_categories'] = array('descriptionformat');
2634 $extendtables['course_request'] = array('summaryformat');
2635 $extendtables['grade_outcomes'] = array('descriptionformat');
2636 $extendtables['groups'] = array('descriptionformat');
2637 $extendtables['groupings'] = array('descriptionformat');
2638 $extendtables['scale'] = array('descriptionformat');
2639 $extendtables['user'] = array('descriptionformat');
2640 $extendtables['user_info_field'] = array('descriptionformat', 'defaultdataformat');
2641 $extendtables['user_info_data'] = array('dataformat');
2642
2643 foreach ($extendtables as $tablestr=>$newfields) {
2644 $table = new xmldb_table($tablestr);
2645 foreach ($newfields as $fieldstr) {
2646 $field = new xmldb_field($fieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2647 // Check that the field doesn't already exists
2648 if (!$dbman->field_exists($table, $field)) {
2649 // Add the new field
2650 $dbman->add_field($table, $field);
2651 // Update the field if the text contains the default FORMAT_MOODLE to FORMAT_HTML
2652 if (($pos = strpos($fieldstr, 'format'))>0) {
2653 upgrade_set_timeout(60*20); // this may take a little while
2654 $params = array(FORMAT_HTML, '<p%', '%<br />%', FORMAT_MOODLE);
2655 $textfield = substr($fieldstr, 0, $pos);
cb8a83bb 2656 $DB->execute('UPDATE {'.$tablestr.'} SET '.$fieldstr.'=? WHERE ('.$textfield.' LIKE ? OR '.$textfield.' LIKE ?) AND '.$fieldstr.'=?', $params);
8bdc9cac
SH
2657 }
2658 }
2659 }
2660 }
2661
2662 unset($extendtables);
2663
2664 upgrade_main_savepoint($result, 2009110400);
2665 }
2666
975341d1 2667 if ($result && $oldversion < 2009110605) {
2668
2669 /// Define field timecreated to be added to external_services
2670 $table = new xmldb_table('external_services');
2671 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'component');
2672
2673 /// Conditionally launch add field timecreated
2674 if (!$dbman->field_exists($table, $field)) {
2675 $dbman->add_field($table, $field);
2676 }
2677
2678 /// Define field timemodified to be added to external_services
2679 $table = new xmldb_table('external_services');
2680 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'timecreated');
2681
2682 /// Conditionally launch add field timemodified
2683 if (!$dbman->field_exists($table, $field)) {
2684 $dbman->add_field($table, $field);
2685 }
2686
2687 /// Main savepoint reached
2688 upgrade_main_savepoint($result, 2009110605);
2689 }
2690
e1b9ac3e
PL
2691 if ($result && $oldversion < 2009111600) {
2692
2693 /// Define field instance to be added to portfolio_tempdata
2694 $table = new xmldb_table('portfolio_tempdata');
2695 $field = new xmldb_field('instance', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'userid');
2696
2697 /// Conditionally launch add field instance
2698 if (!$dbman->field_exists($table, $field)) {
2699 $dbman->add_field($table, $field);
2700 }
2701
2702 $key = new xmldb_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
2703
2704 /// Launch add key instancefk
2705 $dbman->add_key($table, $key);
2706
2707 /// Main savepoint reached
2708 upgrade_main_savepoint($result, 2009111600);
2709 }
2710
45645bce
PL
2711 if ($result && $oldversion < 2009111700) {
2712
2713 /// Define field tempdataid to be added to portfolio_log
2714 $table = new xmldb_table('portfolio_log');
2715 $field = new xmldb_field('tempdataid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'caller_sha1');
2716
2717 /// Conditionally launch add field tempdataid
2718 if (!$dbman->field_exists($table, $field)) {
2719 $dbman->add_field($table, $field);
2720 }
2721
2722 /// Main savepoint reached
2723 upgrade_main_savepoint($result, 2009111700);
2724 }
2725
eeb7ccf0
PL
2726 if ($result && $oldversion < 2009111701) {
2727
2728 /// Define field returnurl to be added to portfolio_log
2729 $table = new xmldb_table('portfolio_log');
2730 $field = new xmldb_field('returnurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'tempdataid');
2731
2732 /// Conditionally launch add field returnurl
2733 if (!$dbman->field_exists($table, $field)) {
2734 $dbman->add_field($table, $field);
2735 }
2736
2737 /// Main savepoint reached
2738 upgrade_main_savepoint($result, 2009111701);
2739 }
2740
2741 if ($result && $oldversion < 2009111702) {
2742
2743 /// Define field continueurl to be added to portfolio_log
2744 $table = new xmldb_table('portfolio_log');
2745 $field = new xmldb_field('continueurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'returnurl');
2746
2747 /// Conditionally launch add field continueurl
2748 if (!$dbman->field_exists($table, $field)) {
2749 $dbman->add_field($table, $field);
2750 }
2751
2752 /// Main savepoint reached
2753 upgrade_main_savepoint($result, 2009111702);
2754 }
2755
57e6ce5e
PS
2756 if ($result && $oldversion < 2009112400) {
2757 if (empty($CFG->passwordsaltmain)) {
2758 $subject = get_string('check_passwordsaltmain_name', 'report_security');
2759 $description = get_string('check_passwordsaltmain_warning', 'report_security');;
2760 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2761 }
2762 upgrade_main_savepoint($result, 2009112400);
2763 }
e1b9ac3e 2764
5ae9f539 2765 if ($result && $oldversion < 2010010601) {
2766
2767 /// Define field creatorid to be added to external_tokens
2768 $table = new xmldb_table('external_tokens');
2769 $field = new xmldb_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'contextid');
2770
2771 /// Conditionally launch add field creatorid
2772 if (!$dbman->field_exists($table, $field)) {
2773 $dbman->add_field($table, $field);
2774 }
2775
2776 /// Define key creatorid (foreign) to be added to external_tokens
2777 $table = new xmldb_table('external_tokens');
2778 $key = new xmldb_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
2779
2780 /// Launch add key creatorid
2781 $dbman->add_key($table, $key);
2782
2783 /// Main savepoint reached
2784 upgrade_main_savepoint($result, 2010010601);
2785 }
2786
c95002a2 2787 if ($result && $oldversion < 2010011200) {
a25bb902
AD
2788 $table = new xmldb_table('grade_categories');
2789 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
2790
2791 if (!$dbman->field_exists($table, $field)) {
2792 $dbman->add_field($table, $field);
2793 }
2794
c95002a2 2795 upgrade_main_savepoint($result, 2010011200);
a25bb902
AD
2796 }
2797
de260e0f 2798
7062a798
PL
2799 if ($result && $oldversion < 2010012500) {
2800 upgrade_fix_incorrect_mnethostids();
2801 upgrade_main_savepoint($result, 2010012500);
2802 }
de260e0f
PL
2803
2804 if ($result && $oldversion < 2010012600) {
2805 // do stuff to the mnet table
2806 $table = new xmldb_table('mnet_rpc');
2807
2808 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2809 $dbman->rename_field($table, $field, 'plugintype');
2810
2811 $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2812 $dbman->rename_field($table, $field, 'pluginname');
2813
2814 $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
2815 if (!$dbman->field_exists($table, $field)) {
2816 $dbman->add_field($table, $field);
2817 }
2818
2819 $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
2820 if (!$dbman->field_exists($table, $field)) {
2821 $dbman->add_field($table, $field);
2822 }
2823
2824 $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
2825 if (!$dbman->field_exists($table, $field)) {
2826 $dbman->add_field($table, $field);
2827 }
2828
2829 /// Main savepoint reached
2830 upgrade_main_savepoint($result, 2010012600);
2831 }
2832
8586dbe2
PL
2833 if ($result && $oldversion < 2010012900) {
2834
2835 /// Define table mnet_remote_rpc to be created
2836 $table = new xmldb_table('mnet_remote_rpc');
2837
2838 /// Adding fields to table mnet_remote_rpc
2839 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2840 $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2841 $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
2842
2843 /// Adding keys to table mnet_remote_rpc
2844 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2845
2846 /// Conditionally launch create table for mnet_remote_rpc
2847 if (!$dbman->table_exists($table)) {
2848 $dbman->create_table($table);
2849 }
2850
2851
2852 /// Define table mnet_remote_service2rpc to be created
2853 $table = new xmldb_table('mnet_remote_service2rpc');
2854
2855 /// Adding fields to table mnet_remote_service2rpc
2856 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2857 $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2858 $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2859
2860 /// Adding keys to table mnet_remote_service2rpc
2861 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2862
2863 /// Adding indexes to table mnet_remote_service2rpc
2864 $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2865
2866 /// Conditionally launch create table for mnet_remote_service2rpc
2867 if (!$dbman->table_exists($table)) {
2868 $dbman->create_table($table);
2869 }
2870
2871
2872 /// Rename field function_name on table mnet_rpc to functionname
2873 $table = new xmldb_table('mnet_rpc');
2874 $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
2875
2876 /// Launch rename field function_name
2877 $dbman->rename_field($table, $field, 'functionname');
2878
2879
2880 /// Rename field xmlrpc_path on table mnet_rpc to xmlrpcpath
2881 $table = new xmldb_table('mnet_rpc');
2882 $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
2883
2884 /// Launch rename field xmlrpc_path
2885 $dbman->rename_field($table, $field, 'xmlrpcpath');
2886
2887
2888 /// Main savepoint reached
2889 upgrade_main_savepoint($result, 2010012900);
2890 }
a6e9dafe
PL
2891
2892 if ($result && $oldversion < 2010012901) {
2893
2894 /// Define field plugintype to be added to mnet_remote_rpc
2895 $table = new xmldb_table('mnet_remote_rpc');
2896 $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
2897
2898 /// Conditionally launch add field plugintype
2899 if (!$dbman->field_exists($table, $field)) {
2900 $dbman->add_field($table, $field);
2901 }
2902
a6e9dafe
PL
2903 /// Define field pluginname to be added to mnet_remote_rpc
2904 $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
2905
2906 /// Conditionally launch add field pluginname
2907 if (!$dbman->field_exists($table, $field)) {
2908 $dbman->add_field($table, $field);
2909 }
5b7984a5
PL
2910
2911 /// Main savepoint reached
2912 upgrade_main_savepoint($result, 2010012901);
2913 }
2914
2915 if ($result && $oldversion < 2010012902) {
2916
2917 /// Define field enabled to be added to mnet_remote_rpc
2918 $table = new xmldb_table('mnet_remote_rpc');
2919 $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
2920
2921 /// Conditionally launch add field enabled
2922 if (!$dbman->field_exists($table, $field)) {
2923 $dbman->add_field($table, $field);
2924 }
2925
2926 /// Main savepoint reached
2927 upgrade_main_savepoint($result, 2010012902);
a6e9dafe
PL
2928 }
2929
f6852f02
PL
2930 /// MDL-17863. Increase the portno column length on mnet_host to handle any port number
2931 if ($result && $oldversion < 2010020100) {
2932 /// Changing precision of field portno on table mnet_host to (5)
2933 $table = new xmldb_table('mnet_host');
2934 $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
2935
2936 /// Launch change of precision for field portno
2937 $dbman->change_field_precision($table, $field);
2938
2939 upgrade_main_savepoint($result, 2010020100);
2940 }
2941
d3d393ab
RW
2942 if ($result && $oldversion < 2010020300) {
2943
2944 /// Define field timecreated to be added to user
2945 $table = new xmldb_table('user');
2946 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
2947
2948 if (!$dbman->field_exists($table, $field)) {
2949 /// Launch add field timecreated
2950 $dbman->add_field($table, $field);
2951
2952 $DB->execute("UPDATE {user} SET timecreated = firstaccess");
2953
2954 $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
2955 $DB->execute($sql);
2956 }
2957 upgrade_main_savepoint($result, 2010020300);
2958 }
073d3804
EL
2959
2960 // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
2961 if ($result && $oldversion < 2010020301) {
2962 if ($preamble = $CFG->filter_tex_latexpreamble) {
2963 $preamble = preg_replace('/^ +/m', '', $preamble);
2964 set_config('filter_tex_latexpreamble', $preamble);
2965 }
2966 upgrade_main_savepoint($result, 2010020301);
2967 }
2968
9a9012dc
PS
2969 if ($result && $oldversion < 2010021400) {
2970 /// Changes to modinfo mean we need to rebuild course cache
2971 require_once($CFG->dirroot . '/course/lib.php');
2972 rebuild_course_cache(0, true);
2973 upgrade_main_savepoint($result, 2010021400);
2974 }
2975
c9606565
PL
2976 if ($result && $oldversion < 2010021800) {
2977 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
2978 upgrade_main_savepoint($result, 2010021800);
2979 }
2980
cd1bca89
PS
2981 if ($result && $oldversion < 2010031900) {
2982 // regeneration of sessions is always enabled, no need for this setting any more
2983 unset_config('regenloginsession');
2984 upgrade_main_savepoint($result, 2010031900);
2985 }
90723839 2986
1dce6261 2987
90723839
SH
2988 if ($result && $oldversion < 2010032400) {
2989 // Upgrade all of those using the standardold theme to the use the standard
2990 // theme instead
2991 if ($CFG->theme == 'standardold') {
2992 // The config setting that affects the whole site
2993 set_config('theme', 'standard');
2994 }
2995 // Course Categories
2996 $DB->execute('UPDATE {course_categories} SET theme=? WHERE theme=?', array('standard', 'standardold'));
2997 // Course
2998 $DB->execute('UPDATE {course} SET theme=? WHERE theme=?', array('standard', 'standardold'));
2999 // User
3000 $DB->execute('UPDATE {user} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3001 upgrade_main_savepoint($result, 2010032400);
3002 }
1dce6261 3003
f0b9ee18 3004 if ($result && $oldversion < 2010033101.01) {
1dce6261
DC
3005
3006 /// Define field source to be added to files
3007 $table = new xmldb_table('files');
3008
f0b9ee18 3009 $field = new xmldb_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'status');
1dce6261
DC
3010
3011 /// Conditionally launch add field source
3012 if (!$dbman->field_exists($table, $field)) {
3013 $dbman->add_field($table, $field);
3014 }
3015
3016 $field = new xmldb_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'source');
3017
3018 /// Conditionally launch add field author
3019 if (!$dbman->field_exists($table, $field)) {
3020 $dbman->add_field($table, $field);
3021 }
3022
3023 $field = new xmldb_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'author');
3024
3025 /// Conditionally launch add field license
3026 if (!$dbman->field_exists($table, $field)) {
3027 $dbman->add_field($table, $field);
3028 }
3029
f0b9ee18
PS
3030 upgrade_main_savepoint($result, 2010033101.01);
3031 }
3032
3033 if ($result && $oldversion < 2010033101.02) {
98da6021 3034
1dce6261
DC
3035 /// Define table license to be created
3036 $table = new xmldb_table('license');
3037
3038 /// Adding fields to table license
3039 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3040 $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3041 $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
3042 $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3043 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
3044 $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
3045
3046 /// Adding keys to table license
3047 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3048
3049 /// Conditionally launch create table for license
3050 if (!$dbman->table_exists($table)) {
3051 $dbman->create_table($table);
3052 }
d1d4813f
DC
3053 $active_licenses = array();
3054
3055 $license = new stdclass;
3056
3057 // add unknown license
3058 $license->shortname = 'unknown';
3059 $license->fullname = 'Unknown license';
3060 $license->source = '';
3061 $license->enabled = 1;
3062 $license->version = '2010033100';
3063 $active_licenses[] = $license->shortname;
3064 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3065 if ($record->version < $license->version) {
3066 // update license record
3067 $license->enabled = $record->enabled;
3068 $license->id = $record->id;
3069 $DB->update_record('license', $license);
3070 }
3071 } else {
3072 $DB->insert_record('license', $license);
3073 }
3074
3075 // add all rights reserved license
3076 $license->shortname = 'allrightsreserved';
3077 $license->fullname = 'All rights reserved';
3078 $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
3079 $license->enabled = 1;
3080 $license->version = '2010033100';
3081 $active_licenses[] = $license->shortname;
3082 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3083 if ($record->version < $license->version) {
3084 // update license record
3085 $license->id = $record->id;
3086 $license->enabled = $record->enabled;
3087 $DB->update_record('license', $license);
3088 }
3089 } else {
3090 $DB->insert_record('license', $license);
3091 }
3092
3093 // add public domain license
3094 $license->shortname = 'public';
3095 $license->fullname = 'Public Domain';
3096 $license->source = 'http://creativecommons.org/licenses/publicdomain/';
3097 $license->enabled = 1;
3098 $license->version = '2010033100';
3099 $active_licenses[] = $license->shortname;
3100 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3101 if ($record->version < $license->version) {
3102 // update license record
3103 $license->enabled = $record->enabled;
3104 $license->id = $record->id;
3105 $DB->update_record('license', $license);
3106 }
3107 } else {
3108 $DB->insert_record('license', $license);
3109 }
3110
3111 // add creative commons license
3112 $license->shortname = 'cc';
3113 $license->fullname = 'Creative Commons';
3114 $license->source = 'http://creativecommons.org/licenses/by/3.0/';
3115 $license->enabled = 1;
3116 $license->version = '2010033100';
3117 $active_licenses[] = $license->shortname;
3118 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3119 if ($record->version < $license->version) {
3120 // update license record
3121 $license->enabled = $record->enabled;
3122 $license->id = $record->id;
3123 $DB->update_record('license', $license);
3124 }
3125 } else {
3126 $DB->insert_record('license', $license);
3127 }
3128
3129 // add creative commons no derivs license
3130 $license->shortname = 'cc-nd';
3131 $license->fullname = 'Creative Commons - NoDerivs';
3132 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3133 $license->enabled = 1;
3134 $license->version = '2010033100';
3135 $active_licenses[] = $license->shortname;
3136 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3137 if ($record->version < $license->version) {
3138 // update license record
3139 $license->enabled = $record->enabled;
3140 $license->id = $record->id;
3141 $DB->update_record('license', $license);
3142 }
3143 } else {
3144 $DB->insert_record('license', $license);
3145 }
3146
3147 // add creative commons no commercial no derivs license
3148 $license->shortname = 'cc-nc-nd';
3149 $license->fullname = 'Creative Commons - No Commercial NoDerivs';
3150 $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
3151 $license->enabled = 1;
3152 $license->version = '2010033100';
3153 $active_licenses[] = $license->shortname;
3154 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3155 if ($record->version < $license->version) {
3156 // update license record
3157 $license->enabled = $record->enabled;
3158 $license->id = $record->id;
3159 $DB->update_record('license', $license);
3160 }
3161 } else {
3162 $DB->insert_record('license', $license);
3163 }
3164
3165 // add creative commons no commercial
3166 $license->shortname = 'cc-nc-nd';
3167 $license->shortname = 'cc-nc';
3168 $license->fullname = 'Creative Commons - No Commercial';
3169 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3170 $license->enabled = 1;
3171 $license->version = '2010033100';
3172 $active_licenses[] = $license->shortname;
3173 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3174 if ($record->version < $license->version) {
3175 // update license record
3176 $license->enabled = $record->enabled;
3177 $license->id = $record->id;
3178 $DB->update_record('license', $license);
3179 }
3180 } else {
3181 $DB->insert_record('license', $license);
3182 }
3183
3184 // add creative commons no commercial sharealike
3185 $license->shortname = 'cc-nc-sa';
3186 $license->fullname = 'Creative Commons - No Commercial ShareAlike';
3187 $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
3188 $license->enabled = 1;
3189 $license->version = '2010033100';
3190 $active_licenses[] = $license->shortname;
3191 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3192 if ($record->version < $license->version) {
3193 // update license record
3194 $license->enabled = $record->enabled;
3195 $license->id = $record->id;
3196 $DB->update_record('license', $license);
3197 }
3198 } else {
3199 $DB->insert_record('license', $license);
3200 }
3201
3202 // add creative commons sharealike
3203 $license->shortname = 'cc-sa';
3204 $license->fullname = 'Creative Commons - ShareAlike';
3205 $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
3206 $license->enabled = 1;
3207 $license->version = '2010033100';
3208 $active_licenses[] = $license->shortname;
3209 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3210 if ($record->version < $license->version) {
3211 // update license record
3212 $license->enabled = $record->enabled;
3213 $license->id = $record->id;
3214 $DB->update_record('license', $license);
3215 }
3216 } else {
3217 $DB->insert_record('license', $license);
3218 }
3219
3220 set_config('licenses', implode(',', $active_licenses));
1dce6261
DC
3221 /// set site default license
3222 set_config('sitedefaultlicense', 'allrightsreserved');
3223
3224 /// Main savepoint reached
f0b9ee18 3225 upgrade_main_savepoint($result, 2010033101.02);
1dce6261 3226 }
4f0c2d00
PS
3227
3228 if ($result && $oldversion < 2010033102.00) {
3229 // rename course view capability to participate
3230 $params = array('view'=>'moodle/course:view', 'participate'=>'moodle/course:participate');
3231 $sql = "UPDATE {role_capabilities} SET capability = :participate WHERE capability = :view";
3232 $DB->execute($sql, $params);
3233 $sql = "UPDATE {capabilities} SET name = :participate WHERE name = :view";
3234 $DB->execute($sql, $params);
3235 // note: the view capability is readded again at the end of upgrade, but with different meaning
3236 upgrade_main_savepoint($result, 2010033102.00);
3237 }
3238
3239 if ($result && $oldversion < 2010033102.01) {
3240 // Define field archetype to be added to role table
3241 $table = new xmldb_table('role');
3242 $field = new xmldb_field('archetype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'sortorder');
3243 $dbman->add_field($table, $field);
3244 upgrade_main_savepoint($result, 2010033102.01);
3245 }
3246
3247 if ($result && $oldversion < 2010033102.02) {
3248 // Set archetype for existing roles and change admin role to manager role
3249 $sql = "SELECT r.*, rc.capability
3250 FROM {role} r
3251 JOIN {role_capabilities} rc ON rc.roleid = r.id
3252 WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
3253 ORDER BY r.id";
3254 $params = array('syscontextid'=>SYSCONTEXTID, 'legacycaps'=>'moodle/legacy:%');
3255 $substart = strlen('moodle/legacy:');
3256 $roles = $DB->get_recordset_sql($sql, $params); // in theory could be multiple legacy flags in one role
3257 foreach ($roles as $role) {
3258 $role->archetype = substr($role->capability, $substart);
3259 unset($role->capability);
3260 if ($role->archetype === 'admin') {
3261 $role->archetype = 'manager';
3262 if ($role->shortname === 'admin') {
3263 $role->shortname = 'manager';
3264 $role->name = get_string('manager', 'role');
3265 $role->description = get_string('managerdescription', 'role');
3266 }
3267 }
3268 $DB->update_record('role', $role);
3269 }
3270 $roles->close();
3271
3272 upgrade_main_savepoint($result, 2010033102.02);
3273 }
3274
3275 if ($result && $oldversion < 2010033102.03) {
3276 // Now pick site admins (===have manager role assigned at the system context)
3277 // and store them in the new $CFG->siteadmins setting as comma separated list
3278 $sql = "SELECT ra.id, ra.userid
3279 FROM {role_assignments} ra
3280 JOIN {role} r ON r.id = ra.roleid
3281 JOIN {user} u ON u.id = ra.userid
3282 WHERE ra.contextid = :syscontext AND r.archetype = 'manager' AND u.deleted = 0
3283 ORDER BY ra.id";
3284 $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
3285 $admins = array();
3286 foreach ($ras as $ra) {
3287 $admins[$ra->userid] = $ra->userid;
3288 set_config('siteadmins', implode(',', $admins)); // better to save it repeatedly, we do need at least one admin
3289 $DB->delete_records('role_assignments', array('id'=>$ra->id));
3290 }
3291
3292 upgrade_main_savepoint($result, 2010033102.03);
3293 }
3294
3295 if ($result && $oldversion < 2010033102.04) {
3296 // clean up the manager roles
3297 $managers = $DB->get_records('role', array('archetype'=>'manager'));
3298 foreach ($managers as $manager) {
3299 // now sanitize the capabilities and overrides
3300 $DB->delete_records('role_capabilities', array('capability'=>'moodle/site:config', 'roleid'=>$manager->id)); // only site admins may configure servers
3301 // note: doanything and legacy caps are deleted automatically, they get moodle/course:view later at the end of the upgrade
3302
3303 // set usable contexts
3304 $DB->delete_records('role_context_levels', array('roleid'=>$manager->id));
3305 $assignlevels = array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE);
3306 foreach ($assignlevels as $assignlevel) {
3307 $record = (object)array('roleid'=>$manager->id, 'contextlevel'=>$assignlevel);
3308 $DB->insert_record('role_context_levels', $record);
3309 }
3310
3311 // remove manager role assignments bellow the course context level - admin role was never intended for activities and blocks,
3312 // the problem is that those assignments would not be visible after upgrade and old style admins in activities make no sense anyway
3313 $DB->delete_records_select('role_assignments', "roleid = :manager AND contextid IN (SELECT id FROM {context} WHERE contextlevel > 50)", array('manager'=>$manager->id));
3314
3315 // allow them to assign all roles except default user, guest and frontpage - users get these roles automatically on the fly when needed
3316 $DB->delete_records('role_allow_assign', array('roleid'=>$manager->id));
3317 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype <> 'user' AND archetype <> 'guest' AND archetype <> 'frontpage'");
3318 foreach ($roles as $role) {
3319 $record = (object)array('roleid'=>$manager->id, 'allowassign'=>$role->id);
3320 $DB->insert_record('role_allow_assign', $record);
3321 }
3322
3323 // allow them to override all roles
3324 $DB->delete_records('role_allow_override', array('roleid'=>$manager->id));
3325 $roles = $DB->get_records_sql("SELECT * FROM {role}");
3326 foreach ($roles as $role) {
3327 $record = (object)array('roleid'=>$manager->id, 'allowoverride'=>$role->id);
3328 $DB->insert_record('role_allow_override', $record);
3329 }
3330
3331 // allow them to switch to all following roles
3332 $DB->delete_records('role_allow_switch', array('roleid'=>$manager->id));
3333 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype IN ('student', 'teacher', 'editingteacher')");
3334 foreach ($roles as $role) {
3335 $record = (object)array('roleid'=>$manager->id, 'allowswitch'=>$role->id);
3336 $DB->insert_record('role_allow_switch', $record);
3337 }
3338 }
3339
3340 upgrade_main_savepoint($result, 2010033102.04);
3341 }
3342
3343 if ($result && $oldversion < 2010033102.05) {
3344 // remove course:view from all roles that are not used for enrolment, it does NOT belong there because it really means user is enrolled!
3345 $noenrolroles = $DB->get_records_select('role', "archetype IN ('guest', 'user', 'manager', 'coursecreator', 'frontpage')");
3346 foreach ($noenrolroles as $role) {
3347 $DB->delete_records('role_capabilities', array('roleid'=>$role->id, 'capability'=>'moodle/course:participate'));
3348 }
3349 upgrade_main_savepoint($result, 2010033102.05);
3350 }
3351
3352 if ($result && $oldversion < 2010033102.06) {
3353 // make sure there is nothing weird in default user role
3354 if (!empty($CFG->defaultuserroleid)) {
3355 if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
3356 if ($role->archetype !== '' and $role->archetype !== 'user') {
3357 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default authenticated user role (defaultuserroleid) value is invalid, setting cleared.');
3358 unset_config('defaultuserroleid');
3359 }
3360 } else {
3361 unset_config('defaultuserroleid');
3362 }
3363 }
3364 upgrade_main_savepoint($result, 2010033102.06);
3365 }
3366
3367 if ($result && $oldversion < 2010033102.07) {
3368 if (!empty($CFG->displayloginfailures) and $CFG->displayloginfailures === 'teacher') {
3369 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Displaying of login failuters to teachers is not supported any more.');
3370 unset_config('displayloginfailures');
3371 }
3372 upgrade_main_savepoint($result, 2010033102.07);
3373 }
3374
3375 if ($result && $oldversion < 2010033102.08) {
3376 // make sure there are no problems in default guest role settings
3377 if (!empty($CFG->guestroleid)) {
3378 if ($role = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
3379 if ($role->archetype !== '' and $role->archetype !== 'guest') {
3380 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default guest role (guestroleid) value is invalid, setting cleared.');
3381 unset_config('guestroleid');
3382 }
3383 } else {
3384 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) doeas not exist, setting cleared.');
3385 unset_config('guestroleid');
3386 }
3387 }
3388 // remove all roles of the guest account - the only way to change it is to override the guest role, sorry
3389 // the guest account gets all the role assignemnts on the fly whcih works fine in has_capability(),
3390 $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE username = 'guest')");
3391
3392 upgrade_main_savepoint($result, 2010033102.08);
3393 }
3394
3395 if ($result && $oldversion < 2010033102.09) {
3396 // For MDL-17501. Ensure that any role that has moodle/course:update also has moodle/course:visibility.
3397 // Get the roles with 'moodle/course:update'.
3398 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
3399 $roles = get_roles_with_capability('moodle/course:update', CAP_ALLOW, $systemcontext);
3400
3401 // Give those roles 'moodle/course:visibility'.
3402 foreach ($roles as $role) {
3403 assign_capability('moodle/course:visibility', CAP_ALLOW, $role->id, $systemcontext->id);
3404 }
3405
3406 // Force all sessions to refresh access data.
3407 mark_context_dirty($systemcontext->path);
3408
3409 // Main savepoint reached
3410 upgrade_main_savepoint($result, 2010033102.09);
3411 }
3412
98da6021
PS
3413 if ($result && $oldversion < 2010040700) {
3414 // migrate old groupings --> groupmembersonly setting
3415 if (isset($CFG->enablegroupings)) {
3416 set_config('enablegroupmembersonly', $CFG->enablegroupings);
3417 unset_config('enablegroupings');
3418 }
3419
3420 // Main savepoint reached
3421 upgrade_main_savepoint($result, 2010040700);
3422 }
3423
3a915b06
PS
3424 if ($result && $oldversion < 2010040900) {
3425
3426 // Changing the default of field lang on table user to good old "en"
3427 $table = new xmldb_table('user');
3428 $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en', 'country');
3429
3430 // Launch change of default for field lang
3431 $dbman->change_field_default($table, $field);
3432
3433 // update main site lang
3434 if (strpos($CFG->lang, '_utf8') !== false) {
3435 $lang = str_replace('_utf8', '', $CFG->lang);
3436 set_config('lang', $lang);
3437 }
3438
2357ab49
PS
3439 // tweak langlist
3440 if (!empty($CFG->langlist)) {
3441 $langs = explode(',', $CFG->langlist);
3442 foreach ($langs as $key=>$lang) {
3443 $lang = str_replace('_utf8', '', $lang);
3444 $langs[$key] = $lang;
3445 }
3446 set_config('langlist', implode(',', $langs));
3447 }
3448
3a915b06
PS
3449 // Main savepoint reached
3450 upgrade_main_savepoint($result, 2010040900);
3451 }
3452
3453 if ($result && $oldversion < 2010040901) {
3454
3455 // Remove "_utf8" suffix from all langs in user table
3456 $langs = $DB->get_records_sql("SELECT DISTINCT lang FROM {user} WHERE lang LIKE ?", array('%_utf8'));
3457
3458 foreach ($langs as $lang=>$unused) {
3459 $newlang = str_replace('_utf8', '', $lang);
3460 $sql = "UPDATE {user} SET lang = :newlang WHERE lang = :lang";
3461 $DB->execute($sql, array('newlang'=>$newlang, 'lang'=>$lang));
3462 }
3463
3464 // Main savepoint reached
3465 upgrade_main_savepoint($result, 2010040901);
3466 }
3467
80350ef6
AD
3468 if ($result && $oldversion < 2010041300) {
3469 //drop the erroneously created ratings table
3470 $table = new xmldb_table('ratings');
3471 if ($dbman->table_exists($table)) {
3472 $dbman->drop_table($table);
3473 }
3474
3475 //create the rating table (replaces module specific rating implementations)
3476 $table = new xmldb_table('rating');
3477 if ($dbman->table_exists($table)) {
3478 $dbman->drop_table($table);
3479 }
3480
3481 /// Adding fields to table rating
3482 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3483 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3484
3485 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3486 $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
3487 $table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3488 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3489
3490 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3491 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3492
3493 /// Adding keys to table rating
3494 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3495 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3496 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3497
3498 /// Adding indexes to table rating
3499 $table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
3500
3501 /// Create table for ratings
3502 if (!$dbman->table_exists($table)) {
3503 $dbman->create_table($table);
3504 }
3505
3506 upgrade_main_savepoint($result, 2010041300);
3507 }
3508
3406acde
SH
3509 if ($result && $oldversion < 2010041301) {
3510 $sql = "UPDATE {block} SET name=? WHERE name=?";
3511 $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3512 $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3513
3514 $sql = "UPDATE {block_instances} SET blockname=? WHERE blockname=?";
3515 $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3516 $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3517 upgrade_main_savepoint($result, 2010041301);
3518 }
3519
ee4d3dee
EL
3520 if ($result && $oldversion < 2010042100) {
3521
3522 /// Define table backup_controllers to be created
3523 $table = new xmldb_table('backup_controllers');
3524
3525 /// Adding fields to table backup_controllers
3526 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3527 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3528 $table->add_field('type', XMLDB_TYPE_CHAR, '6', null, XMLDB_NOTNULL, null, null);
3529 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3530 $table->add_field('format', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
3531 $table->add_field('interactive', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3532 $table->add_field('purpose', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3533 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3534 $table->add_field('status', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3535 $table->add_field('execution', XMLDB