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