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