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