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