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