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