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