MDL-24520 use different name if manager role already exists
[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)) {
2a88f626 242 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, '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');
648 $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'generalfeedback');
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');
2a88f626 655 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, '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');
2a88f626 662 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, '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');
2a88f626 669 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, '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');
2a88f626 676 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, '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');
2a88f626 683 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, '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');
2a88f626 690 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, '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
2b212777 1555 $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', '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);
1820 $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
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);
1824 $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1');
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)
1848 SELECT id, contextid,
1849 CASE WHEN pagetypepattern = 'course-view-*' THEN
1850 (SELECT " . $DB->sql_concat("'course-view-'", 'format') . "
1851 FROM {course}
1852 JOIN {context} ON {course}.id = {context}.instanceid
1853 WHERE {context}.id = contextid)
1854 ELSE pagetypepattern END,
1855 CASE WHEN subpagepattern IS NULL THEN ''
1856 ELSE subpagepattern END,
1857 0, defaultregion, defaultweight
1858 FROM {block_instances} WHERE visible = 0 AND pagetypepattern <> 'admin-*'");
1859
1860 /// Main savepoint reached
a4cdd6d2 1861 upgrade_main_savepoint(true, 2009050618);
66b10689 1862 }
1863
a4cdd6d2 1864 if ($oldversion < 2009050619) {
66b10689 1865 $table = new xmldb_table('block_instances');
1866
1867 /// Define field blockid to be dropped from block_instances
1868 $field = new xmldb_field('blockid');
1869 if ($dbman->field_exists($table, $field)) {
4276a19a 1870 /// Before dropping the field, drop dependent indexes
1871 $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
1872 if ($dbman->index_exists($table, $index)) {
1873 /// Launch drop index blockid
1874 $dbman->drop_index($table, $index);
1875 }
66b10689 1876 $dbman->drop_field($table, $field);
1877 }
1878
1879 /// Define field pageid to be dropped from block_instances
1880 $field = new xmldb_field('pageid');
1881 if ($dbman->field_exists($table, $field)) {
4276a19a 1882 /// Before dropping the field, drop dependent indexes
1883 $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1884 if ($dbman->index_exists($table, $index)) {
1885 /// Launch drop index pageid
1886 $dbman->drop_index($table, $index);
1887 }
66b10689 1888 $dbman->drop_field($table, $field);
1889 }
1890
1891 /// Define field visible to be dropped from block_instances
1892 $field = new xmldb_field('visible');
1893 if ($dbman->field_exists($table, $field)) {
1894 $dbman->drop_field($table, $field);
1895 }
1896
1897 /// Main savepoint reached
a4cdd6d2 1898 upgrade_main_savepoint(true, 2009050619);
35079f53 1899 }
1900
a4cdd6d2 1901 if ($oldversion < 2009051200) {
ecdf5d17 1902 /// Let's check the status of mandatory mnet_host records, fixing them
1903 /// and moving "orphan" users to default localhost record. MDL-16879
aa9a6867 1904 echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
ecdf5d17 1905 upgrade_fix_incorrect_mnethostids();
1906
1907 /// Main savepoint reached
a4cdd6d2 1908 upgrade_main_savepoint(true, 2009051200);
ecdf5d17 1909 }
1910
a5747cf8 1911
a4cdd6d2 1912 if ($oldversion < 2009051700) {
a5747cf8 1913 /// migrate editor settings
1914 if (empty($CFG->htmleditor)) {
1915 set_config('texteditors', 'textarea');
1916 } else {
1917 set_config('texteditors', 'tinymce,textarea');
1918 }
1919
1920 unset_config('htmleditor');
1921 unset_config('defaulthtmleditor');
1922
1923 /// Main savepoint reached
a4cdd6d2 1924 upgrade_main_savepoint(true, 2009051700);
a5747cf8 1925 }
1926
6389e70a
PS
1927 /// Repeat 2009050607 upgrade step, which Petr commented out because of XMLDB
1928 /// stupidity, so lots of people will have missed.
a4cdd6d2 1929 if ($oldversion < 2009061600) {
52902cea 1930 /// Changing precision of field defaultregion on table block_instances to (16)
1931 $table = new xmldb_table('block_instances');
1932 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
1933
1934 /// Launch change of precision for field defaultregion
1935 $dbman->change_field_precision($table, $field);
1936
1937 /// Main savepoint reached
a4cdd6d2 1938 upgrade_main_savepoint(true, 2009061600);
52902cea 1939 }
1940
a4cdd6d2 1941 if ($oldversion < 2009061702) {
17da2e6f 1942 // standardizing plugin names
1943 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
1944 foreach ($configs as $config) {
61676608 1945 unset_config($config->name, $config->plugin); /// unset old config
17da2e6f 1946 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
61676608 1947 set_config($config->name, $config->value, $config->plugin); /// set new config
17da2e6f 1948 }
1949 }
1950 unset($configs);
a4cdd6d2 1951 upgrade_main_savepoint(true, 2009061702);
17da2e6f 1952 }
1953
a4cdd6d2 1954 if ($oldversion < 2009061703) {
17da2e6f 1955 // standardizing plugin names
1956 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
1957 foreach ($configs as $config) {
61676608 1958 unset_config($config->name, $config->plugin); /// unset old config
17da2e6f 1959 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
61676608 1960 set_config($config->name, $config->value, $config->plugin); /// set new config
17da2e6f 1961 }
1962 }
1963 unset($configs);
a4cdd6d2 1964 upgrade_main_savepoint(true, 2009061703);
17da2e6f 1965 }
1966
a4cdd6d2 1967 if ($oldversion < 2009061704) {
13a0d3d3 1968 // change component string in capability records to new "_" format
17da2e6f 1969 if ($caps = $DB->get_records('capabilities')) {
1970 foreach ($caps as $cap) {
1971 $cap->component = str_replace('/', '_', $cap->component);
1972 $DB->update_record('capabilities', $cap);
1973 }
1974 }
1975 unset($caps);
a4cdd6d2 1976 upgrade_main_savepoint(true, 2009061704);
17da2e6f 1977 }
1978
a4cdd6d2 1979 if ($oldversion < 2009063000) {
adf8f3f9 1980 // upgrade format of _with_advanced settings - quiz only
1981 // note: this can be removed later, not needed for upgrades from 1.9.x
c1f58efe 1982 if ($quiz = get_config('quiz')) {
1983 foreach ($quiz as $name=>$value) {
1984 if (strpos($name, 'fix_') !== 0) {
1985 continue;
1986 }
1987 $newname = substr($name,4).'_adv';
1988 set_config($newname, $value, 'quiz');
1989 unset_config($name, 'quiz');
adf8f3f9 1990 }
13a0d3d3 1991 }
a4cdd6d2 1992 upgrade_main_savepoint(true, 2009063000);
adf8f3f9 1993 }
13a0d3d3 1994
a4cdd6d2 1995 if ($oldversion < 2009071000) {
13a0d3d3 1996
1997 /// Rename field contextid on table block_instances to parentcontextid
1998 $table = new xmldb_table('block_instances');
1999 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
2000
2001 /// Launch rename field parentcontextid
2002 $dbman->rename_field($table, $field, 'parentcontextid');
2003
2004 /// Main savepoint reached
a4cdd6d2 2005 upgrade_main_savepoint(true, 2009071000);
13a0d3d3 2006 }
2007
a4cdd6d2 2008 if ($oldversion < 2009071600) {
cae83708 2009
2010 /// Define field summaryformat to be added to post
2011 $table = new xmldb_table('post');
2012 $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
2013
2014 /// Conditionally launch add field summaryformat
2015 if (!$dbman->field_exists($table, $field)) {
2016 $dbman->add_field($table, $field);
2017 }
2018
2019 /// Main savepoint reached
a4cdd6d2 2020 upgrade_main_savepoint(true, 2009071600);
cae83708 2021 }
e92c286c 2022
a4cdd6d2 2023 if ($oldversion < 2009072400) {
1bcb7eb5 2024
2025 /// Define table comments to be created
2026 $table = new xmldb_table('comments');
2027
2028 /// Adding fields to table comments
2029 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2030 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2031 $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2032 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2033 $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2034 $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2035 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2036 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2037
2038 /// Adding keys to table comments
2039 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2040
2041 /// Conditionally launch create table for comments
2042 if (!$dbman->table_exists($table)) {
2043 $dbman->create_table($table);
2044 }
0a127169 2045
1bcb7eb5 2046 /// Main savepoint reached
a4cdd6d2 2047 upgrade_main_savepoint(true, 2009072400);
1bcb7eb5 2048 }
2049
7d2a0492 2050 /**
2051 * This upgrade is to set up the new navigation blocks that have been developed
2052 * as part of Moodle 2.0
2053 * Now I [Sam Hemelryk] hit a conundrum while exploring how to go about this
2054 * as not only do we want to install the new blocks but we also want to set up
2055 * default instances of them, and at the same time remove instances of the blocks
2056 * that were/will-be outmoded by the two new navigation blocks.
2057 * After talking it through with Tim Hunt {@link http://moodle.org/mod/cvsadmin/view.php?conversationid=3112}
2058 * we decided that the best way to go about this was to put the bulk of the
2059 * upgrade operation into core upgrade `here` but to let the plugins block
2060 * still install the blocks.
2061 * This leaves one hairy end in that we will create block_instances within the
2062 * DB before the blocks themselves are created within the DB
2063 */
a4cdd6d2 2064 if ($oldversion < 2009082800) {
7d2a0492 2065
2066 echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2067
2068 // Get the system context so we can set the block instances to it
2069 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2070
2071 // An array to contain the new block instances we will create
2072 $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
2073 // The new global navigation block instance as a stdClass
2074 $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
2075 $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
2e5eba85 2076 $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutely everywhere
7d2a0492 2077 $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
2078 $newblockinstances['globalnavigation']->subpagetypepattern = null;
2079 $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
2080 $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
2081 $newblockinstances['globalnavigation']->configdata = '';
2082 // The new settings navigation block instance as a stdClass
2083 $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
2084 $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
2085 $newblockinstances['settingsnavigation']->showinsubcontexts = true;
2086 $newblockinstances['settingsnavigation']->pagetypepattern = '*';
2087 $newblockinstances['settingsnavigation']->subpagetypepattern = null;
2088 $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
2089 $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
2090 $newblockinstances['settingsnavigation']->configdata = '';
2091
2092 // Blocks that are outmoded and for whom the bells will toll... by which I
2093 // mean we will delete all instances of
2094 $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
2095 $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
2096 unset($outmodedblocks);
2097 // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
2098 // cut down the number of delete queries we will need to run
2099 $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
1918f7e0 2100
7d2a0492 2101 $contextids = array();
2102 $instanceids = array();
2103 // Iterate through all block instances
2104 foreach ($allblockinstances as $blockinstance) {
2105 if (!in_array($blockinstance->parentcontextid, $contextids)) {
2106 $contextids[] = $blockinstance->parentcontextid;
2107
2108 // If we have over 1000 contexts clean them up and reset the array
2109 // this ensures we don't hit any nasty memory limits or such
2110 if (count($contextids) > 1000) {
61676608 2111 upgrade_cleanup_unwanted_block_contexts($contextids);
7d2a0492 2112 $contextids = array();
2113 }
2114 }
2115 if (!in_array($blockinstance->id, $instanceids)) {
2116 $instanceids[] = $blockinstance->id;
2117 // If we have more than 1000 block instances now remove all block positions
2118 // and empty the array
2119 if (count($contextids) > 1000) {
2120 $instanceidstring = join(',',$instanceids);
61676608 2121 $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
7d2a0492 2122 $instanceids = array();
2123 }
2124 }
2125 }
2126
61676608 2127 upgrade_cleanup_unwanted_block_contexts($contextids);
7d2a0492 2128
2129 $instanceidstring = join(',',$instanceids);
a4cdd6d2 2130 $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
1918f7e0 2131
7d2a0492 2132 unset($allblockinstances);
2133 unset($contextids);
2134 unset($instanceids);
2135 unset($instanceidstring);
1918f7e0 2136
7d2a0492 2137 // Now remove the actual block instance
7874b33d 2138 $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
7d2a0492 2139 unset($outmodedblocksstring);
2140
2141 // Insert the new block instances. Remember they have not been installed yet
2142 // however this should not be a problem
2143 foreach ($newblockinstances as $blockinstance) {
2144 $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
2145 // Ensure the block context is created.
2146 get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
2147 }
2148 unset($newblockinstances);
1918f7e0 2149
a4cdd6d2 2150 upgrade_main_savepoint(true, 2009082800);
7d2a0492 2151 // The end of the navigation upgrade
2152 }
2153
a4cdd6d2 2154 if ($oldversion < 2009100602) {
4313fa89 2155 /// Define table external_functions to be created
2156 $table = new xmldb_table('external_functions');
2157
2158 /// Adding fields to table external_functions
2159 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
5d820ca2 2160 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2161 $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2162 $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2163 $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
4313fa89 2164 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
4313fa89 2165
2166 /// Adding keys to table external_functions
2167 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2168
5d820ca2 2169 /// Adding indexes to table external_functions
2170 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2171
2172 /// Launch create table for external_functions
2173 $dbman->create_table($table);
4313fa89 2174
5d820ca2 2175 /// Main savepoint reached
a4cdd6d2 2176 upgrade_main_savepoint(true, 2009100602);
5d820ca2 2177 }
4313fa89 2178
a4cdd6d2 2179 if ($oldversion < 2009100603) {
f6882162 2180 /// Define table external_services to be created
4313fa89 2181 $table = new xmldb_table('external_services');
2182
2183 /// Adding fields to table external_services
2184 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
5d820ca2 2185 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2186 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2187 $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
2188 $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2189 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
f6882162
PS
2190 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2191 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
4313fa89 2192
2193 /// Adding keys to table external_services
2194 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2195
5b9a70a1 2196 /// Adding indexes to table external_services
2197 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2198
5d820ca2 2199 /// Launch create table for external_services
2200 $dbman->create_table($table);
4313fa89 2201
5d820ca2 2202 /// Main savepoint reached
a4cdd6d2 2203 upgrade_main_savepoint(true, 2009100603);
5d820ca2 2204 }
4313fa89 2205
a4cdd6d2 2206 if ($oldversion < 2009100604) {
4313fa89 2207 /// Define table external_services_functions to be created
2208 $table = new xmldb_table('external_services_functions');
2209
2210 /// Adding fields to table external_services_functions
2211 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2212 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
5d820ca2 2213 $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
4313fa89 2214
2215 /// Adding keys to table external_services_functions
2216 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
5d820ca2 2217 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
4313fa89 2218
5d820ca2 2219 /// Launch create table for external_services_functions
2220 $dbman->create_table($table);
2221
2222 /// Main savepoint reached
a4cdd6d2 2223 upgrade_main_savepoint(true, 2009100604);
5d820ca2 2224 }
2225
a4cdd6d2 2226 if ($oldversion < 2009100605) {
5d820ca2 2227 /// Define table external_services_users to be created
2228 $table = new xmldb_table('external_services_users');
2229
2230 /// Adding fields to table external_services_users
2231 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2232 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2233 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
88af54fa 2234 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2235 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2236 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
5d820ca2 2237
2238 /// Adding keys to table external_services_users
2239 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2240 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2241 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2242
2243 /// Launch create table for external_services_users
2244 $dbman->create_table($table);
4313fa89 2245
2246 /// Main savepoint reached
a4cdd6d2 2247 upgrade_main_savepoint(true, 2009100605);
4313fa89 2248 }
2249
a4cdd6d2 2250 if ($oldversion < 2009102600) {
1942103f
PS
2251
2252 /// Define table external_tokens to be created
2253 $table = new xmldb_table('external_tokens');
2254
2255 /// Adding fields to table external_tokens
2256 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2257 $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
2258 $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2259 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2260 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2261 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
2262 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
0b3681a4 2263 $table->add_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
1942103f
PS
2264 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2265 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2266 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2267 $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2268
2269 /// Adding keys to table external_tokens
2270 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2271 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2272 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2273 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
0b3681a4 2274 $table->add_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
1942103f
PS
2275
2276 /// Launch create table for external_tokens
2277 $dbman->create_table($table);
2278
2279 /// Main savepoint reached
a4cdd6d2 2280 upgrade_main_savepoint(true, 2009102600);
1942103f
PS
2281 }
2282
a4cdd6d2 2283 if ($oldversion < 2009103000) {
cc82aef6
NC
2284
2285 /// Define table blog_association to be created
2286 $table = new xmldb_table('blog_association');
2287
2288 /// Adding fields to table blog_association
2289 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2290 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2291 $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2292
2293 /// Adding keys to table blog_association
2294 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2295 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2296 $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
2297
2298 /// Conditionally launch create table for blog_association
2299 if (!$dbman->table_exists($table)) {
2300 $dbman->create_table($table);
2301 }
2302
2303/// Define table blog_external to be created
2304 $table = new xmldb_table('blog_external');
2305
2306 /// Adding fields to table blog_external
2307 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2308 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2309 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2310 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2311 $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2312 $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2313 $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2314 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2315 $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2316
2317 /// Adding keys to table blog_external
2318 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2319 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2320
2321 /// Conditionally launch create table for blog_external
2322 if ($dbman->table_exists($table)) {
2323 // Delete the existing one first (comes from early dev version)
2324 $dbman->drop_table($table);
2325 }
2326 $dbman->create_table($table);
2327
adf176d7 2328 // now inform admins that some settings require attention after upgrade
cc82aef6
NC
2329 if (($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete)) {
2330 echo $OUTPUT->notification(get_string('bloglevelupgradenotice', 'admin'));
2331
cc82aef6
NC
2332 $site = get_site();
2333
2334 $a = new StdClass;
2335 $a->sitename = $site->fullname;
2336 $a->fixurl = "$CFG->wwwroot/$CFG->admin/bloglevelupgrade.php";
2337
c01678b1
PS
2338 $subject = get_string('bloglevelupgrade', 'admin');
2339 $description = get_string('bloglevelupgradedescription', 'admin', $a);
2340
2341 // can not use messaging here because it is not configured yet!
2342 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
cc82aef6
NC
2343 }
2344 /// Main savepoint reached
a4cdd6d2 2345 upgrade_main_savepoint(true, 2009103000);
cc82aef6 2346 }
117bd748 2347
a4cdd6d2 2348 if ($oldversion < 2009110400) {
985acc93
PS
2349 // list of tables where we need to add new format field and convert texts
2350 $extendtables = array('course' => 'summary',
2351 'course_categories' => 'description',
2352 'course_categories' => 'description',
2353 'course_request' => 'summary',
2354 'grade_outcomes' => 'description',
2355 'groups' => 'description',
2356 'groupings' => 'description',
2357 'scale' => 'description',
2358 'user_info_field' => 'description',
2359 'user_info_field' => 'defaultdata',
2360 'user_info_data' => 'data');
2361
2362 foreach ($extendtables as $tablestr=>$fieldstr) {
2363 $formatfieldstr = $fieldstr.'format';
8bdc9cac 2364
8bdc9cac 2365 $table = new xmldb_table($tablestr);
985acc93
PS
2366 $field = new xmldb_field($formatfieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', $fieldstr);
2367 // Check that the field doesn't already exists
2368 if (!$dbman->field_exists($table, $field)) {
2369 // Add the new field
2370 $dbman->add_field($table, $field);
2371 }
2372 if ($CFG->texteditors !== 'textarea') {
2373 $rs = $DB->get_recordset($tablestr, array($formatfieldstr=>FORMAT_MOODLE), '', "id,$fieldstr,$formatfieldstr");
2374 foreach ($rs as $rec) {
2375 $rec->$fieldstr = text_to_html($rec->$fieldstr, false, false, true);
2376 $rec->$formatfieldstr = FORMAT_HTML;
2377 $DB->update_record($tablestr, $rec);
2378 upgrade_set_timeout();
8bdc9cac 2379 }
985acc93 2380 $rs->close();
8bdc9cac
SH
2381 }
2382 }
2383
985acc93 2384 unset($rec);
8bdc9cac
SH
2385 unset($extendtables);
2386
a4cdd6d2 2387 upgrade_main_savepoint(true, 2009110400);
8bdc9cac
SH
2388 }
2389
985acc93
PS
2390 if ($oldversion < 2009110401) {
2391 $table = new xmldb_table('user');
7f38a28d
SH
2392
2393 // Change the precision of the description field first up.
2394 // This may grow!
2395 $field = new xmldb_field('description', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'url');
2396 $dbman->change_field_precision($table, $field);
2397
985acc93
PS
2398 $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'description');
2399 // Check that the field doesn't already exists
2400 if (!$dbman->field_exists($table, $field)) {
2401 // Add the new field
2402 $dbman->add_field($table, $field);
2403 }
2404 if ($CFG->texteditors !== 'textarea') {
2405 $rs = $DB->get_recordset('user', array('descriptionformat'=>FORMAT_MOODLE, 'deleted'=>0, 'htmleditor'=>1), '', "id,description,descriptionformat");
2406 foreach ($rs as $rec) {
2407 $rec->description = text_to_html($rec->description, false, false, true);
2408 $rec->descriptionformat = FORMAT_HTML;
2409 $DB->update_record('user', $rec);
2410 upgrade_set_timeout();
2411 }
2412 $rs->close();
2413 }
2414
2415 upgrade_main_savepoint(true, 2009110401);
2416 }
2417
a4cdd6d2 2418 if ($oldversion < 2009112400) {
57e6ce5e
PS
2419 if (empty($CFG->passwordsaltmain)) {
2420 $subject = get_string('check_passwordsaltmain_name', 'report_security');
2421 $description = get_string('check_passwordsaltmain_warning', 'report_security');;
2422 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2423 }
a4cdd6d2 2424 upgrade_main_savepoint(true, 2009112400);
57e6ce5e 2425 }
e1b9ac3e 2426
a4cdd6d2 2427 if ($oldversion < 2010011200) {
a25bb902
AD
2428 $table = new xmldb_table('grade_categories');
2429 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
2430
2431 if (!$dbman->field_exists($table, $field)) {
2432 $dbman->add_field($table, $field);
2433 }
2434
a4cdd6d2 2435 upgrade_main_savepoint(true, 2010011200);
a25bb902
AD
2436 }
2437
a4cdd6d2 2438 if ($oldversion < 2010012500) {
7062a798 2439 upgrade_fix_incorrect_mnethostids();
a4cdd6d2 2440 upgrade_main_savepoint(true, 2010012500);
7062a798 2441 }
de260e0f 2442
a4cdd6d2 2443 if ($oldversion < 2010012600) {
de260e0f
PL
2444 // do stuff to the mnet table
2445 $table = new xmldb_table('mnet_rpc');
2446
2447 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2448 $dbman->rename_field($table, $field, 'plugintype');
2449
2450 $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2451 $dbman->rename_field($table, $field, 'pluginname');
2452
2453 $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
2454 if (!$dbman->field_exists($table, $field)) {
2455 $dbman->add_field($table, $field);
2456 }
2457
2458 $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
2459 if (!$dbman->field_exists($table, $field)) {
2460 $dbman->add_field($table, $field);
2461 }
2462
2463 $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
2464 if (!$dbman->field_exists($table, $field)) {
2465 $dbman->add_field($table, $field);
2466 }
2467
2468 /// Main savepoint reached
a4cdd6d2 2469 upgrade_main_savepoint(true, 2010012600);
de260e0f
PL
2470 }
2471
a4cdd6d2 2472 if ($oldversion < 2010012900) {
8586dbe2
PL
2473
2474 /// Define table mnet_remote_rpc to be created
2475 $table = new xmldb_table('mnet_remote_rpc');
2476
2477 /// Adding fields to table mnet_remote_rpc
2478 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2479 $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2480 $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
2481
2482 /// Adding keys to table mnet_remote_rpc
2483 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2484
2485 /// Conditionally launch create table for mnet_remote_rpc
2486 if (!$dbman->table_exists($table)) {
2487 $dbman->create_table($table);
2488 }
2489
2490
2491 /// Define table mnet_remote_service2rpc to be created
2492 $table = new xmldb_table('mnet_remote_service2rpc');
2493
2494 /// Adding fields to table mnet_remote_service2rpc
2495 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2496 $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2497 $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2498
2499 /// Adding keys to table mnet_remote_service2rpc
2500 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2501
2502 /// Adding indexes to table mnet_remote_service2rpc
2503 $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2504
2505 /// Conditionally launch create table for mnet_remote_service2rpc
2506 if (!$dbman->table_exists($table)) {
2507 $dbman->create_table($table);
2508 }
2509
2510
2511 /// Rename field function_name on table mnet_rpc to functionname
2512 $table = new xmldb_table('mnet_rpc');
2513 $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
2514
2515 /// Launch rename field function_name
2516 $dbman->rename_field($table, $field, 'functionname');
2517
2518
2519 /// Rename field xmlrpc_path on table mnet_rpc to xmlrpcpath
2520 $table = new xmldb_table('mnet_rpc');
2521 $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
2522
2523 /// Launch rename field xmlrpc_path
2524 $dbman->rename_field($table, $field, 'xmlrpcpath');
2525
2526
2527 /// Main savepoint reached
a4cdd6d2 2528 upgrade_main_savepoint(true, 2010012900);
8586dbe2 2529 }
a6e9dafe 2530
a4cdd6d2 2531 if ($oldversion < 2010012901) {
a6e9dafe
PL
2532
2533 /// Define field plugintype to be added to mnet_remote_rpc
2534 $table = new xmldb_table('mnet_remote_rpc');
2535 $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
2536
2537 /// Conditionally launch add field plugintype
2538 if (!$dbman->field_exists($table, $field)) {
2539 $dbman->add_field($table, $field);
2540 }
2541
a6e9dafe
PL
2542 /// Define field pluginname to be added to mnet_remote_rpc
2543 $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
2544
2545 /// Conditionally launch add field pluginname
2546 if (!$dbman->field_exists($table, $field)) {
2547 $dbman->add_field($table, $field);
2548 }
5b7984a5
PL
2549
2550 /// Main savepoint reached
a4cdd6d2 2551 upgrade_main_savepoint(true, 2010012901);
5b7984a5
PL
2552 }
2553
a4cdd6d2 2554 if ($oldversion < 2010012902) {
5b7984a5
PL
2555
2556 /// Define field enabled to be added to mnet_remote_rpc
2557 $table = new xmldb_table('mnet_remote_rpc');
2558 $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
2559
2560 /// Conditionally launch add field enabled
2561 if (!$dbman->field_exists($table, $field)) {
2562 $dbman->add_field($table, $field);
2563 }
2564
2565 /// Main savepoint reached
a4cdd6d2 2566 upgrade_main_savepoint(true, 2010012902);
a6e9dafe
PL
2567 }
2568
f6852f02 2569 /// MDL-17863. Increase the portno column length on mnet_host to handle any port number
a4cdd6d2 2570 if ($oldversion < 2010020100) {
f6852f02
PL
2571 /// Changing precision of field portno on table mnet_host to (5)
2572 $table = new xmldb_table('mnet_host');
2573 $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
2574
2575 /// Launch change of precision for field portno
2576 $dbman->change_field_precision($table, $field);
2577
a4cdd6d2 2578 upgrade_main_savepoint(true, 2010020100);
f6852f02
PL
2579 }
2580
a4cdd6d2 2581 if ($oldversion < 2010020300) {
d3d393ab
RW
2582
2583 /// Define field timecreated to be added to user
2584 $table = new xmldb_table('user');
2585 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
2586
2587 if (!$dbman->field_exists($table, $field)) {
2588 /// Launch add field timecreated
2589 $dbman->add_field($table, $field);
2590
2591 $DB->execute("UPDATE {user} SET timecreated = firstaccess");
2592
2593 $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
2594 $DB->execute($sql);
2595 }
a4cdd6d2 2596 upgrade_main_savepoint(true, 2010020300);
d3d393ab 2597 }
073d3804
EL
2598
2599 // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
a4cdd6d2 2600 if ($oldversion < 2010020301) {
073d3804
EL
2601 if ($preamble = $CFG->filter_tex_latexpreamble) {
2602 $preamble = preg_replace('/^ +/m', '', $preamble);
2603 set_config('filter_tex_latexpreamble', $preamble);
2604 }
a4cdd6d2 2605 upgrade_main_savepoint(true, 2010020301);
073d3804
EL
2606 }
2607
a4cdd6d2 2608 if ($oldversion < 2010021400) {
9a9012dc
PS
2609 /// Changes to modinfo mean we need to rebuild course cache
2610 require_once($CFG->dirroot . '/course/lib.php');
2611 rebuild_course_cache(0, true);
a4cdd6d2 2612 upgrade_main_savepoint(true, 2010021400);
9a9012dc
PS
2613 }
2614
a4cdd6d2 2615 if ($oldversion < 2010021800) {
c9606565 2616 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
a4cdd6d2 2617 upgrade_main_savepoint(true, 2010021800);
c9606565
PL
2618 }
2619
a4cdd6d2 2620 if ($oldversion < 2010031900) {
cd1bca89
PS
2621 // regeneration of sessions is always enabled, no need for this setting any more
2622 unset_config('regenloginsession');
a4cdd6d2 2623 upgrade_main_savepoint(true, 2010031900);
cd1bca89 2624 }
90723839 2625
a4cdd6d2 2626 if ($oldversion < 2010033101.02) {
98da6021 2627
1dce6261
DC
2628 /// Define table license to be created
2629 $table = new xmldb_table('license');
2630
2631 /// Adding fields to table license
2632 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2633 $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2634 $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2635 $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2636 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
2637 $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2638
2639 /// Adding keys to table license
2640 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2641
2642 /// Conditionally launch create table for license
2643 if (!$dbman->table_exists($table)) {
2644 $dbman->create_table($table);
2645 }
d1d4813f
DC
2646 $active_licenses = array();
2647
6bdfef5d 2648 $license = new stdClass();
d1d4813f
DC
2649
2650 // add unknown license
2651 $license->shortname = 'unknown';
2652 $license->fullname = 'Unknown license';
2653 $license->source = '';
2654 $license->enabled = 1;
2655 $license->version = '2010033100';
2656 $active_licenses[] = $license->shortname;
2657 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2658 if ($record->version < $license->version) {
2659 // update license record
2660 $license->enabled = $record->enabled;
2661 $license->id = $record->id;
2662 $DB->update_record('license', $license);
2663 }
2664 } else {
2665 $DB->insert_record('license', $license);
2666 }
2667
2668 // add all rights reserved license
2669 $license->shortname = 'allrightsreserved';
2670 $license->fullname = 'All rights reserved';
2671 $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
2672 $license->enabled = 1;
2673 $license->version = '2010033100';
2674 $active_licenses[] = $license->shortname;
2675 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2676 if ($record->version < $license->version) {
2677 // update license record
2678 $license->id = $record->id;
2679 $license->enabled = $record->enabled;
2680 $DB->update_record('license', $license);
2681 }
2682 } else {
2683 $DB->insert_record('license', $license);
2684 }
2685
2686 // add public domain license
2687 $license->shortname = 'public';
2688 $license->fullname = 'Public Domain';
2689 $license->source = 'http://creativecommons.org/licenses/publicdomain/';
2690 $license->enabled = 1;
2691 $license->version = '2010033100';
2692 $active_licenses[] = $license->shortname;
2693 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2694 if ($record->version < $license->version) {
2695 // update license record
2696 $license->enabled = $record->enabled;
2697 $license->id = $record->id;
2698 $DB->update_record('license', $license);
2699 }
2700 } else {
2701 $DB->insert_record('license', $license);
2702 }
2703
2704 // add creative commons license
2705 $license->shortname = 'cc';
2706 $license->fullname = 'Creative Commons';
2707 $license->source = 'http://creativecommons.org/licenses/by/3.0/';
2708 $license->enabled = 1;
2709 $license->version = '2010033100';
2710 $active_licenses[] = $license->shortname;
2711 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2712 if ($record->version < $license->version) {
2713 // update license record
2714 $license->enabled = $record->enabled;
2715 $license->id = $record->id;
2716 $DB->update_record('license', $license);
2717 }
2718 } else {
2719 $DB->insert_record('license', $license);
2720 }
2721
2722 // add creative commons no derivs license
2723 $license->shortname = 'cc-nd';
2724 $license->fullname = 'Creative Commons - NoDerivs';
2725 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
2726 $license->enabled = 1;
2727 $license->version = '2010033100';
2728 $active_licenses[] = $license->shortname;
2729 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2730 if ($record->version < $license->version) {
2731 // update license record
2732 $license->enabled = $record->enabled;
2733 $license->id = $record->id;
2734 $DB->update_record('license', $license);
2735 }
2736 } else {
2737 $DB->insert_record('license', $license);
2738 }
2739
2740 // add creative commons no commercial no derivs license
2741 $license->shortname = 'cc-nc-nd';
2742 $license->fullname = 'Creative Commons - No Commercial NoDerivs';
2743 $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
2744 $license->enabled = 1;
2745 $license->version = '2010033100';
2746 $active_licenses[] = $license->shortname;
2747 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2748 if ($record->version < $license->version) {
2749 // update license record
2750 $license->enabled = $record->enabled;
2751 $license->id = $record->id;
2752 $DB->update_record('license', $license);
2753 }
2754 } else {
2755 $DB->insert_record('license', $license);
2756 }
2757
2758 // add creative commons no commercial
2759 $license->shortname = 'cc-nc-nd';
2760 $license->shortname = 'cc-nc';
2761 $license->fullname = 'Creative Commons - No Commercial';
2762 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
2763 $license->enabled = 1;
2764 $license->version = '2010033100';
2765 $active_licenses[] = $license->shortname;
2766 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2767 if ($record->version < $license->version) {
2768 // update license record
2769 $license->enabled = $record->enabled;
2770 $license->id = $record->id;
2771 $DB->update_record('license', $license);
2772 }
2773 } else {
2774 $DB->insert_record('license', $license);
2775 }
2776
2777 // add creative commons no commercial sharealike
2778 $license->shortname = 'cc-nc-sa';
2779 $license->fullname = 'Creative Commons - No Commercial ShareAlike';
2780 $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
2781 $license->enabled = 1;
2782 $license->version = '2010033100';
2783 $active_licenses[] = $license->shortname;
2784 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2785 if ($record->version < $license->version) {
2786 // update license record
2787 $license->enabled = $record->enabled;
2788 $license->id = $record->id;
2789 $DB->update_record('license', $license);
2790 }
2791 } else {
2792 $DB->insert_record('license', $license);
2793 }
2794
2795 // add creative commons sharealike
2796 $license->shortname = 'cc-sa';
2797 $license->fullname = 'Creative Commons - ShareAlike';
2798 $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
2799 $license->enabled = 1;
2800 $license->version = '2010033100';
2801 $active_licenses[] = $license->shortname;
2802 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2803 if ($record->version < $license->version) {
2804 // update license record
2805 $license->enabled = $record->enabled;
2806 $license->id = $record->id;
2807 $DB->update_record('license', $license);
2808 }
2809 } else {
2810 $DB->insert_record('license', $license);
2811 }
2812
2813 set_config('licenses', implode(',', $active_licenses));
1dce6261
DC
2814 /// set site default license
2815 set_config('sitedefaultlicense', 'allrightsreserved');
2816
2817 /// Main savepoint reached
a4cdd6d2 2818 upgrade_main_savepoint(true, 2010033101.02);
1dce6261 2819 }
4f0c2d00 2820
a4cdd6d2 2821 if ($oldversion < 2010033102.00) {
4f0c2d00
PS
2822 // rename course view capability to participate
2823 $params = array('view'=>'moodle/course:view', 'participate'=>'moodle/course:participate');
2824 $sql = "UPDATE {role_capabilities} SET capability = :participate WHERE capability = :view";
2825 $DB->execute($sql, $params);
2826 $sql = "UPDATE {capabilities} SET name = :participate WHERE name = :view";
2827 $DB->execute($sql, $params);
2828 // note: the view capability is readded again at the end of upgrade, but with different meaning
a4cdd6d2 2829 upgrade_main_savepoint(true, 2010033102.00);
4f0c2d00
PS
2830 }
2831
a4cdd6d2 2832 if ($oldversion < 2010033102.01) {
4f0c2d00
PS
2833 // Define field archetype to be added to role table
2834 $table = new xmldb_table('role');
2835 $field = new xmldb_field('archetype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'sortorder');
2836 $dbman->add_field($table, $field);
a4cdd6d2 2837 upgrade_main_savepoint(true, 2010033102.01);
4f0c2d00
PS
2838 }
2839
a4cdd6d2 2840 if ($oldversion < 2010033102.02) {
4f0c2d00
PS
2841 // Set archetype for existing roles and change admin role to manager role
2842 $sql = "SELECT r.*, rc.capability
2843 FROM {role} r
2844 JOIN {role_capabilities} rc ON rc.roleid = r.id
2845 WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
2846 ORDER BY r.id";
2847 $params = array('syscontextid'=>SYSCONTEXTID, 'legacycaps'=>'moodle/legacy:%');
2848 $substart = strlen('moodle/legacy:');
2849 $roles = $DB->get_recordset_sql($sql, $params); // in theory could be multiple legacy flags in one role
2850 foreach ($roles as $role) {
2851 $role->archetype = substr($role->capability, $substart);
2852 unset($role->capability);
2853 if ($role->archetype === 'admin') {
96e890bb
PS
2854 $i = '';
2855 if ($DB->record_exists('role', array('shortname'=>'manager'))) {
2856 $i = 2;
2857 while($DB->record_exists('role', array('shortname'=>'manager'.$i))) {
2858 $i++;
2859 }
2860 }
4f0c2d00
PS
2861 $role->archetype = 'manager';
2862 if ($role->shortname === 'admin') {
96e890bb 2863 $role->shortname = 'manager'.$i;
4f0c2d00
PS
2864 $role->name = get_string('manager', 'role');
2865 $role->description = get_string('managerdescription', 'role');
2866 }
2867 }
2868 $DB->update_record('role', $role);
2869 }
2870 $roles->close();
2871
a4cdd6d2 2872 upgrade_main_savepoint(true, 2010033102.02);
4f0c2d00
PS
2873 }
2874
a4cdd6d2 2875 if ($oldversion < 2010033102.03) {
4f0c2d00
PS
2876 // Now pick site admins (===have manager role assigned at the system context)
2877 // and store them in the new $CFG->siteadmins setting as comma separated list
2878 $sql = "SELECT ra.id, ra.userid
2879 FROM {role_assignments} ra
2880 JOIN {role} r ON r.id = ra.roleid
2881 JOIN {user} u ON u.id = ra.userid
2882 WHERE ra.contextid = :syscontext AND r.archetype = 'manager' AND u.deleted = 0
2883 ORDER BY ra.id";
2884 $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
2885 $admins = array();
2886 foreach ($ras as $ra) {
2887 $admins[$ra->userid] = $ra->userid;
2888 set_config('siteadmins', implode(',', $admins)); // better to save it repeatedly, we do need at least one admin
2889 $DB->delete_records('role_assignments', array('id'=>$ra->id));
2890 }
2891
a4cdd6d2 2892 upgrade_main_savepoint(true, 2010033102.03);
4f0c2d00
PS
2893 }
2894
a4cdd6d2 2895 if ($oldversion < 2010033102.04) {
4f0c2d00
PS
2896 // clean up the manager roles
2897 $managers = $DB->get_records('role', array('archetype'=>'manager'));
2898 foreach ($managers as $manager) {
2899 // now sanitize the capabilities and overrides
2900 $DB->delete_records('role_capabilities', array('capability'=>'moodle/site:config', 'roleid'=>$manager->id)); // only site admins may configure servers
2901 // note: doanything and legacy caps are deleted automatically, they get moodle/course:view later at the end of the upgrade
2902
4f0c2d00
PS
2903 // remove manager role assignments bellow the course context level - admin role was never intended for activities and blocks,
2904 // the problem is that those assignments would not be visible after upgrade and old style admins in activities make no sense anyway
2905 $DB->delete_records_select('role_assignments', "roleid = :manager AND contextid IN (SELECT id FROM {context} WHERE contextlevel > 50)", array('manager'=>$manager->id));
2906
2907 // allow them to assign all roles except default user, guest and frontpage - users get these roles automatically on the fly when needed
2908 $DB->delete_records('role_allow_assign', array('roleid'=>$manager->id));
2909 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype <> 'user' AND archetype <> 'guest' AND archetype <> 'frontpage'");
2910 foreach ($roles as $role) {
2911 $record = (object)array('roleid'=>$manager->id, 'allowassign'=>$role->id);
2912 $DB->insert_record('role_allow_assign', $record);
2913 }
2914
2915 // allow them to override all roles
2916 $DB->delete_records('role_allow_override', array('roleid'=>$manager->id));
2917 $roles = $DB->get_records_sql("SELECT * FROM {role}");
2918 foreach ($roles as $role) {
2919 $record = (object)array('roleid'=>$manager->id, 'allowoverride'=>$role->id);
2920 $DB->insert_record('role_allow_override', $record);
2921 }
2922
2923 // allow them to switch to all following roles
2924 $DB->delete_records('role_allow_switch', array('roleid'=>$manager->id));
2925 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype IN ('student', 'teacher', 'editingteacher')");
2926 foreach ($roles as $role) {
2927 $record = (object)array('roleid'=>$manager->id, 'allowswitch'=>$role->id);
2928 $DB->insert_record('role_allow_switch', $record);
2929 }
2930 }
2931
a4cdd6d2 2932 upgrade_main_savepoint(true, 2010033102.04);
4f0c2d00
PS
2933 }
2934
a4cdd6d2 2935 if ($oldversion < 2010033102.05) {
4f0c2d00
PS
2936 // remove course:view from all roles that are not used for enrolment, it does NOT belong there because it really means user is enrolled!
2937 $noenrolroles = $DB->get_records_select('role', "archetype IN ('guest', 'user', 'manager', 'coursecreator', 'frontpage')");
2938 foreach ($noenrolroles as $role) {
2939 $DB->delete_records('role_capabilities', array('roleid'=>$role->id, 'capability'=>'moodle/course:participate'));
2940 }
a4cdd6d2 2941 upgrade_main_savepoint(true, 2010033102.05);
4f0c2d00
PS
2942 }
2943
a4cdd6d2 2944 if ($oldversion < 2010033102.06) {
4f0c2d00
PS
2945 // make sure there is nothing weird in default user role
2946 if (!empty($CFG->defaultuserroleid)) {
2947 if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
2948 if ($role->archetype !== '' and $role->archetype !== 'user') {
2949 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default authenticated user role (defaultuserroleid) value is invalid, setting cleared.');
2950 unset_config('defaultuserroleid');
2951 }
2952 } else {
2953 unset_config('defaultuserroleid');
2954 }
2955 }
a4cdd6d2 2956 upgrade_main_savepoint(true, 2010033102.06);
4f0c2d00
PS
2957 }
2958
a4cdd6d2 2959 if ($oldversion < 2010033102.07) {
4f0c2d00
PS
2960 if (!empty($CFG->displayloginfailures) and $CFG->displayloginfailures === 'teacher') {
2961 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Displaying of login failuters to teachers is not supported any more.');
2962 unset_config('displayloginfailures');
2963 }
a4cdd6d2 2964 upgrade_main_savepoint(true, 2010033102.07);
4f0c2d00
PS
2965 }
2966
a4cdd6d2 2967 if ($oldversion < 2010033102.08) {
4f0c2d00
PS
2968 // make sure there are no problems in default guest role settings
2969 if (!empty($CFG->guestroleid)) {
2970 if ($role = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
2971 if ($role->archetype !== '' and $role->archetype !== 'guest') {
2972 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default guest role (guestroleid) value is invalid, setting cleared.');
2973 unset_config('guestroleid');
2974 }
2975 } else {
df997f84 2976 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) does not exist, setting cleared.');
4f0c2d00
PS
2977 unset_config('guestroleid');
2978 }
2979 }
2980 // remove all roles of the guest account - the only way to change it is to override the guest role, sorry
df997f84 2981 // the guest account gets all the role assignments on the fly which works fine in has_capability(),
4f0c2d00
PS
2982 $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE username = 'guest')");
2983
a4cdd6d2 2984 upgrade_main_savepoint(true, 2010033102.08);
4f0c2d00
PS
2985 }
2986
df997f84 2987 /// New table for storing which roles can be assigned in which contexts.
a4cdd6d2 2988 if ($oldversion < 2010033102.09) {
4f0c2d00 2989
df997f84
PS
2990 /// Define table role_context_levels to be created
2991 $table = new xmldb_table('role_context_levels');
2992
2993 /// Adding fields to table role_context_levels
2994 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2995 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2996 $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2997
2998 /// Adding keys to table role_context_levels
2999 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3000 $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
3001 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
3002
3003 /// Conditionally launch create table for role_context_levels
3004 if (!$dbman->table_exists($table)) {
3005 $dbman->create_table($table);
3006 }
3007
3008 /// Main savepoint reached
a4cdd6d2 3009 upgrade_main_savepoint(true, 2010033102.09);
df997f84
PS
3010 }
3011
a4cdd6d2 3012 if ($oldversion < 2010033102.10) {
df997f84
PS
3013 // Now populate the role_context_levels table with the default values
3014 // NOTE: do not use accesslib methods here
3015
3016 $rolecontextlevels = array();
3017 $defaults = array('manager' => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE),
3018 'coursecreator' => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT),
3019 'editingteacher' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3020 'teacher' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3021 'student' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3022 'guest' => array(),
3023 'user' => array(),
3024 'frontpage' => array());
3025
3026 $roles = $DB->get_records('role', array(), '', 'id, archetype');
4f0c2d00 3027 foreach ($roles as $role) {
df997f84
PS
3028 if (isset($defaults[$role->archetype])) {
3029 $rolecontextlevels[$role->id] = $defaults[$role->archetype];
3030 }
3031 }
3032
3033 // add roles without archetypes, it may contain weird things, but we can not fix them
3db2ddbd 3034 list($narsql, $params) = $DB->get_in_or_equal(array_keys($defaults), SQL_PARAMS_NAMED, 'ar000', false);
df997f84
PS
3035 $sql = "SELECT DISTINCT ra.roleid, con.contextlevel
3036 FROM {role_assignments} ra
3db2ddbd
PS
3037 JOIN {context} con ON ra.contextid = con.id
3038 JOIN {role} r ON r.id = ra.roleid
3039 WHERE r.archetype $narsql";
3040 $existingrolecontextlevels = $DB->get_recordset_sql($sql, $params);
df997f84 3041 foreach ($existingrolecontextlevels as $rcl) {
df997f84 3042 if (!isset($rolecontextlevels[$rcl->roleid])) {
3db2ddbd 3043 $rolecontextlevels[$rcl->roleid] = array();
df997f84 3044 }
3db2ddbd 3045 $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
df997f84
PS
3046 }
3047 $existingrolecontextlevels->close();
3048
3049 // Put the data into the database.
365a5941 3050 $rcl = new stdClass();
df997f84
PS
3051 foreach ($rolecontextlevels as $roleid => $contextlevels) {
3052 $rcl->roleid = $roleid;
3053 foreach ($contextlevels as $level) {
3054 $rcl->contextlevel = $level;
3055 $DB->insert_record('role_context_levels', $rcl, false);
3056 }
4f0c2d00
PS
3057 }
3058
df997f84
PS
3059 // release memory!!
3060 unset($roles);
3061 unset($defaults);
3062 unset($rcl);
3063 unset($existingrolecontextlevels);
3064 unset($rolecontextlevels);
4f0c2d00
PS
3065
3066 // Main savepoint reached
a4cdd6d2 3067 upgrade_main_savepoint(true, 2010033102.10);
4f0c2d00
PS
3068 }
3069
a4cdd6d2 3070 if ($oldversion < 2010040700) {
98da6021
PS
3071 // migrate old groupings --> groupmembersonly setting
3072 if (isset($CFG->enablegroupings)) {
3073 set_config('enablegroupmembersonly', $CFG->enablegroupings);
3074 unset_config('enablegroupings');
3075 }
3076
3077 // Main savepoint reached
a4cdd6d2 3078 upgrade_main_savepoint(true, 2010040700);
98da6021
PS
3079 }
3080
a4cdd6d2 3081 if ($oldversion < 2010040900) {
3a915b06
PS
3082
3083 // Changing the default of field lang on table user to good old "en"
3084 $table = new xmldb_table('user');
3085 $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en', 'country');
3086
3087 // Launch change of default for field lang
3088 $dbman->change_field_default($table, $field);
3089
3090 // update main site lang
3091 if (strpos($CFG->lang, '_utf8') !== false) {
3092 $lang = str_replace('_utf8', '', $CFG->lang);
3093 set_config('lang', $lang);
3094 }
3095
2357ab49
PS
3096 // tweak langlist
3097 if (!empty($CFG->langlist)) {
3098 $langs = explode(',', $CFG->langlist);
3099 foreach ($langs as $key=>$lang) {
3100 $lang = str_replace('_utf8', '', $lang);
3101 $langs[$key] = $lang;
3102 }
3103 set_config('langlist', implode(',', $langs));
3104 }
3105
3a915b06 3106 // Main savepoint reached
a4cdd6d2 3107 upgrade_main_savepoint(true, 2010040900);
3a915b06
PS
3108 }
3109
a4cdd6d2 3110 if ($oldversion < 2010040901) {
3a915b06
PS
3111
3112 // Remove "_utf8" suffix from all langs in user table
3113 $langs = $DB->get_records_sql("SELECT DISTINCT lang FROM {user} WHERE lang LIKE ?", array('%_utf8'));
3114
3115 foreach ($langs as $lang=>$unused) {
3116 $newlang = str_replace('_utf8', '', $lang);
3117 $sql = "UPDATE {user} SET lang = :newlang WHERE lang = :lang";
3118 $DB->execute($sql, array('newlang'=>$newlang, 'lang'=>$lang));
3119 }
3120
3121 // Main savepoint reached
a4cdd6d2 3122 upgrade_main_savepoint(true, 2010040901);
3a915b06
PS
3123 }
3124
a4cdd6d2 3125 if ($oldversion < 2010041301) {
3406acde
SH
3126 $sql = "UPDATE {block} SET name=? WHERE name=?";
3127 $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3128 $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3129
3130 $sql = "UPDATE {block_instances} SET blockname=? WHERE blockname=?";
3131 $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3132 $DB->execute($sql, array('settings', 'settings_navigation_tree'));
a4cdd6d2 3133 upgrade_main_savepoint(true, 2010041301);
3406acde
SH
3134 }
3135
a4cdd6d2 3136 if ($oldversion < 2010042100) {
ee4d3dee
EL
3137
3138 /// Define table backup_controllers to be created
3139 $table = new xmldb_table('backup_controllers');
3140
3141 /// Adding fields to table backup_controllers
3142 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3143 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3144 $table->add_field('type', XMLDB_TYPE_CHAR, '6', null, XMLDB_NOTNULL, null, null);
3145 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3146 $table->add_field('format', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
3147 $table->add_field('interactive', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3148 $table->add_field('purpose', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3149 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3150 $table->add_field('status', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3151 $table->add_field('execution', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3152 $table->add_field('executiontime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3153 $table->add_field('checksum', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3154 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3155 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3156 $table->add_field('controller', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
3157
3158 /// Adding keys to table backup_controllers
3159 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3160 $table->add_key('backupid_uk', XMLDB_KEY_UNIQUE, array('backupid'));
3161 $table->add_key('userid_fk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3162
3163 /// Adding indexes to table backup_controllers
3164 $table->add_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3165
3166 /// Conditionally launch create table for backup_controllers
3167 if (!$dbman->table_exists($table)) {
3168 $dbman->create_table($table);
3169 }
3170
3171 /// Define table backup_ids_template to be created
3172 $table = new xmldb_table('backup_ids_template');
3173
3174 /// Adding fields to table backup_ids_template
3175 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3176 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3177 $table->add_field('itemname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
3178 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3179 $table->add_field('parentitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3180
3181 /// Adding keys to table backup_ids_template
3182 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3183 $table->add_key('backupid_itemname_itemid_uk', XMLDB_KEY_UNIQUE, array('backupid', 'itemname', 'itemid'));
3184
3185 /// Adding indexes to table backup_ids_template
3186 $table->add_index('backupid_parentitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'parentitemid'));
3187
3188 /// Conditionally launch create table for backup_controllers
3189 if (!$dbman->table_exists($table)) {
3190 $dbman->create_table($table);
3191 }
3192
3193 /// Main savepoint reached
a4cdd6d2 3194 upgrade_main_savepoint(true, 2010042100);
ee4d3dee
EL
3195 }
3196
a4cdd6d2 3197 if ($oldversion < 2010042301) {
0f4ab67d
SH
3198
3199 $table = new xmldb_table('course_sections');
3200 $field = new xmldb_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'section');
3201
3202 if (!$dbman->field_exists($table, $field)) {
3203 $dbman->add_field($table, $field);
3204 }
3205
a4cdd6d2 3206 upgrade_main_savepoint(true, 2010042301);
0f4ab67d
SH
3207 }
3208
a4cdd6d2 3209 if ($oldversion < 2010042302) {
74180df2
PS
3210 // Define table cohort to be created
3211 $table = new xmldb_table('cohort');
3212
3213 // Adding fields to table cohort
3214 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3215 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3216 $table->add_field('name', XMLDB_TYPE_CHAR, '254', null, XMLDB_NOTNULL, null, null);
3217 $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3218 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
3219 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
df997f84 3220 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
74180df2
PS
3221 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3222 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3223
3224 // Adding keys to table cohort
3225 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3226 $table->add_key('context', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3227
3228 // Conditionally launch create table for cohort
3229 if (!$dbman->table_exists($table)) {
3230 $dbman->create_table($table);
3231 }
3232
a4cdd6d2 3233 upgrade_main_savepoint(true, 2010042302);
74180df2
PS
3234 }
3235
a4cdd6d2 3236 if ($oldversion < 2010042303) {
74180df2
PS
3237 // Define table cohort_members to be created
3238 $table = new xmldb_table('cohort_members');
3239
3240 // Adding fields to table cohort_members
3241 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3242 $table->add_field('cohortid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3243 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3244 $table->add_field('timeadded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3245
3246 // Adding keys to table cohort_members
3247 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3248 $table->add_key('cohortid', XMLDB_KEY_FOREIGN, array('cohortid'), 'cohort', array('id'));
3249 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3250
3251 // Adding indexes to table cohort_members
3252 $table->add_index('cohortid-userid', XMLDB_INDEX_UNIQUE, array('cohortid', 'userid'));
3253
3254 // Conditionally launch create table for cohort_members
3255 if (!$dbman->table_exists($table)) {
3256 $dbman->create_table($table);
3257 }
3258
3259 // Main savepoint reached
a4cdd6d2 3260 upgrade_main_savepoint(true, 2010042303);
cc3b3fae 3261 }
74180df2 3262
a4cdd6d2 3263 if ($oldversion < 2010042800) {
c506e354
AD
3264 //drop the previ