Clarify comment.
[moodle.git] / lib / db / upgrade.php
CommitLineData
4e423cbf 1<?PHP //$Id$
2
3// This file keeps track of upgrades to Moodle.
4//
5// Sometimes, changes between versions involve
6// alterations to database structures and other
7// major things that may break installations.
8//
9// The upgrade function in this file will attempt
10// to perform all the necessary actions to upgrade
11// your older installtion to the current version.
12//
13// If there's something it cannot do itself, it
14// will tell you what you need to do.
15//
16// The commands in here will all be database-neutral,
b1f93b15 17// using the methods of database_manager class
775f811a 18//
19// Please do not forget to use upgrade_set_timeout()
20// before any action that may take longer time to finish.
4e423cbf 21
775f811a 22function xmldb_main_upgrade($oldversion) {
f33e1ed4 23 global $CFG, $THEME, $USER, $DB;
4e423cbf 24
4f12838e 25 require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
edcb15b5 26
4e423cbf 27 $result = true;
28
46d318bc 29 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
f33e1ed4 30
1ea260d8 31 ////////////////////////////////////////
32 ///upgrade supported only from 1.9.x ///
33 ////////////////////////////////////////
da4aa3e4 34
795a08ad 35 if ($result && $oldversion < 2008030600) {
36 //NOTE: this table was added much later, that is why this step is repeated later in this file
37
38 /// Define table upgrade_log to be created
39 $table = new xmldb_table('upgrade_log');
40
41 /// Adding fields to table upgrade_log
2a88f626 42 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
43 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
44 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
45 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
46 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
47 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
48 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
49 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
50 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
795a08ad 51
52 /// Adding keys to table upgrade_log
53 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
54 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
55
56 /// Adding indexes to table upgrade_log
57 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
58 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
59
60 /// Create table for upgrade_log
61 $dbman->create_table($table);
62
63 /// Main savepoint reached
64 upgrade_main_savepoint($result, 2008030600);
65 }
66
a1f27717 67 if ($result && $oldversion < 2008030700) {
775f811a 68 upgrade_set_timeout(60*20); // this may take a while
a1f27717 69
70 /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
a8cb94f6 71 $table = new xmldb_table('grade_letters');
69b80cc2 72 $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
a1f27717 73
74 /// Launch drop index contextid-lowerboundary
eee5d9bb 75 $dbman->drop_index($table, $index);
a1f27717 76
a1f27717 77 /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
a8cb94f6 78 $table = new xmldb_table('grade_letters');
69b80cc2 79 $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
a1f27717 80
81 /// Launch add index contextid-lowerboundary-letter
eee5d9bb 82 $dbman->add_index($table, $index);
a1f27717 83
84 /// Main savepoint reached
0f77b186 85 upgrade_main_savepoint($result, 2008030700);
86 }
a1f27717 87
1ea260d8 88 if ($result && $oldversion < 2008050100) {
89 // Update courses that used weekscss to weeks
a8cb94f6 90 $result = $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
1ea260d8 91 upgrade_main_savepoint($result, 2008050100);
92 }
93
0b51c247 94 if ($result && $oldversion < 2008050200) {
95 // remove unused config options
96 unset_config('statsrolesupgraded');
97 upgrade_main_savepoint($result, 2008050200);
98 }
99
994fbaab 100 if ($result && $oldversion < 2008050700) {
775f811a 101 upgrade_set_timeout(60*20); // this may take a while
102
994fbaab 103 /// Fix minor problem caused by MDL-5482.
104 require_once($CFG->dirroot . '/question/upgrade.php');
105 $result = $result && question_fix_random_question_parents();
106 upgrade_main_savepoint($result, 2008050700);
107 }
108
ab37dc60 109 if ($result && $oldversion < 2008051200) {
110 // if guest role used as default user role unset it and force admin to choose new setting
111 if (!empty($CFG->defaultuserroleid)) {
910dd4dd 112 if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
ab37dc60 113 if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
114 if (isset($guestroles[$role->id])) {
115 set_config('defaultuserroleid', null);
116 notify('Guest role removed from "Default role for all users" setting, please select another role.', 'notifysuccess');
117 }
118 }
119 } else {
120 set_config('defaultuserroleid', null);
121 }
122 }
a54e1638 123 /// Main savepoint reached
124 upgrade_main_savepoint($result, 2008051200);
ab37dc60 125 }
126
8b9cfac4 127 if ($result && $oldversion < 2008051201) {
128 notify('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
775f811a 129 upgrade_set_timeout(60*20); // this may take a while
8b9cfac4 130
469dcbcc 131 /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
cbc08f3b 132 $dbfamily = $DB->get_dbfamily();
133 if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
eee5d9bb 134 $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
469dcbcc 135 }
136
8b9cfac4 137 /// Define index idnumber (not unique) to be dropped form user
a8cb94f6 138 $table = new xmldb_table('user');
69b80cc2 139 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
8b9cfac4 140
141 /// Launch drop index idnumber
f33e1ed4 142 if ($dbman->index_exists($table, $index)) {
eee5d9bb 143 $dbman->drop_index($table, $index);
8b9cfac4 144 }
145
146 /// Changing precision of field idnumber on table user to (255)
a8cb94f6 147 $table = new xmldb_table('user');
2a88f626 148 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
8b9cfac4 149
150 /// Launch change of precision for field idnumber
eee5d9bb 151 $dbman->change_field_precision($table, $field);
8b9cfac4 152
153 /// Launch add index idnumber again
69b80cc2 154 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
eee5d9bb 155 $dbman->add_index($table, $index);
8b9cfac4 156
157 /// Main savepoint reached
158 upgrade_main_savepoint($result, 2008051201);
159 }
160
7ade55ad 161 if ($result && $oldversion < 2008051202) {
910dd4dd 162 $log_action = new object();
7ade55ad 163 $log_action->module = 'course';
164 $log_action->action = 'unenrol';
165 $log_action->mtable = 'course';
166 $log_action->field = 'fullname';
910dd4dd 167 if (!$DB->record_exists('log_display', array('action'=>'unenrol', 'module'=>'course'))) {
168 $result = $result && $DB->insert_record('log_display', $log_action);
7ade55ad 169 }
170 upgrade_main_savepoint($result, 2008051202);
171 }
172
fa162144 173 if ($result && $oldversion < 2008051203) {
174 $table = new xmldb_table('mnet_enrol_course');
2a88f626 175 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
eee5d9bb 176 $dbman->change_field_precision($table, $field);
fa162144 177 upgrade_main_savepoint($result, 2008051203);
178 }
179
38fb8190 180 if ($result && $oldversion < 2008063001) {
775f811a 181 upgrade_set_timeout(60*20); // this may take a while
182
38fb8190 183 // table to be modified
184 $table = new xmldb_table('tag_instance');
185 // add field
186 $field = new xmldb_field('tiuserid');
187 if (!$dbman->field_exists($table, $field)) {
2a88f626 188 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
38fb8190 189 $dbman->add_field($table, $field);
190 }
191 // modify index
192 $index = new xmldb_index('itemtype-itemid-tagid');
193 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
b91de8a5 194 if ($dbman->index_exists($table, $index)) {
195 $dbman->drop_index($table, $index);
196 }
38fb8190 197 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
198 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
b91de8a5 199 if (!$dbman->index_exists($table, $index)) {
200 $dbman->add_index($table, $index);
201 }
38fb8190 202
203 /// Main savepoint reached
204 upgrade_main_savepoint($result, 2008063001);
205 }
38fb8190 206
a036d7f3 207 if ($result && $oldversion < 2008070300) {
208 $result = $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
209 upgrade_main_savepoint($result, 2008070300);
210 }
4e781c7b 211
fe074586 212 if ($result && $oldversion < 2008070700) {
213 if (isset($CFG->defaultuserroleid) and isset($CFG->guestroleid) and $CFG->defaultuserroleid == $CFG->guestroleid) {
214 // guest can not be selected in defaultuserroleid!
215 unset_config('defaultuserroleid');
216 }
217 upgrade_main_savepoint($result, 2008070700);
218 }
219
ac5efef6 220 if ($result && $oldversion < 2008070701) {
221
222 /// Define table portfolio_instance to be created
223 $table = new xmldb_table('portfolio_instance');
224
225 /// Adding fields to table portfolio_instance
2a88f626 226 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
227 $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
228 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
229 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
ac5efef6 230
231 /// Adding keys to table portfolio_instance
232 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
233
234 /// Conditionally launch create table for portfolio_instance
235 if (!$dbman->table_exists($table)) {
236 $dbman->create_table($table);
237 }
238 /// Define table portfolio_instance_config to be created
239 $table = new xmldb_table('portfolio_instance_config');
240
241 /// Adding fields to table portfolio_instance_config
2a88f626 242 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
243 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
244 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
245 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
ac5efef6 246
247 /// Adding keys to table portfolio_instance_config
248 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
249 $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
250
251 /// Adding indexes to table portfolio_instance_config
252 $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
253
254 /// Conditionally launch create table for portfolio_instance_config
255 if (!$dbman->table_exists($table)) {
256 $dbman->create_table($table);
257 }
258
259 /// Define table portfolio_instance_user to be created
260 $table = new xmldb_table('portfolio_instance_user');
261
262 /// Adding fields to table portfolio_instance_user
2a88f626 263 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
264 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
265 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
266 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
267 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
ac5efef6 268
269 /// Adding keys to table portfolio_instance_user
270 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
271 $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
272 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
273
274 /// Conditionally launch create table for portfolio_instance_user
275 if (!$dbman->table_exists($table)) {
276 $dbman->create_table($table);
277 }
278
279 /// Main savepoint reached
280 upgrade_main_savepoint($result, 2008070701);
281 }
172dd12c 282
775f811a 283 if ($result && $oldversion < 2008072400) {
284 /// Create the database tables for message_processors
f65b2dae 285 $table = new xmldb_table('message_processors');
2a88f626 286 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
287 $table->add_field('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null);
f65b2dae 288 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3b120e46 289 $dbman->create_table($table);
290
3b120e46 291 /// delete old and create new fields
f65b2dae 292 $table = new xmldb_table('message');
293 $field = new xmldb_field('messagetype');
3b120e46 294 $dbman->drop_field($table, $field);
295
296 /// fields to rename
f65b2dae 297 $field = new xmldb_field('message');
2a88f626 298 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 299 $dbman->rename_field($table, $field, 'fullmessage');
f65b2dae 300 $field = new xmldb_field('format');
2a88f626 301 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
3b120e46 302 $dbman->rename_field($table, $field, 'fullmessageformat');
303
304 /// new message fields
f65b2dae 305 $field = new xmldb_field('subject');
2a88f626 306 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 307 $dbman->add_field($table, $field);
f65b2dae 308 $field = new xmldb_field('fullmessagehtml');
2a88f626 309 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
3b120e46 310 $dbman->add_field($table, $field);
f65b2dae 311 $field = new xmldb_field('smallmessage');
2a88f626 312 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 313 $dbman->add_field($table, $field);
314
315
f65b2dae 316 $table = new xmldb_table('message_read');
317 $field = new xmldb_field('messagetype');
3b120e46 318 $dbman->drop_field($table, $field);
f65b2dae 319 $field = new xmldb_field('mailed');
3b120e46 320 $dbman->drop_field($table, $field);
321
322 /// fields to rename
f65b2dae 323 $field = new xmldb_field('message');
2a88f626 324 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 325 $dbman->rename_field($table, $field, 'fullmessage');
f65b2dae 326 $field = new xmldb_field('format');
2a88f626 327 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
3b120e46 328 $dbman->rename_field($table, $field, 'fullmessageformat');
329
330
331 /// new message fields
f65b2dae 332 $field = new xmldb_field('subject');
2a88f626 333 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 334 $dbman->add_field($table, $field);
f65b2dae 335 $field = new xmldb_field('fullmessagehtml');
2a88f626 336 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
3b120e46 337 $dbman->add_field($table, $field);
f65b2dae 338 $field = new xmldb_field('smallmessage');
2a88f626 339 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 340 $dbman->add_field($table, $field);
341
342 /// new table
f65b2dae 343 $table = new xmldb_table('message_working');
2a88f626 344 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
345 $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
346 $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
f65b2dae 347 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3b120e46 348 $dbman->create_table($table);
349
350
351 upgrade_main_savepoint($result, 2008072400);
352 }
353
4e781c7b 354 if ($result && $oldversion < 2008072800) {
355
356 /// Define field enablecompletion to be added to course
357 $table = new xmldb_table('course');
358 $field = new xmldb_field('enablecompletion');
2a88f626 359 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
4e781c7b 360
361 /// Launch add field enablecompletion
775f811a 362 if (!$dbman->field_exists($table,$field)) {
4e781c7b 363 $dbman->add_field($table, $field);
364 }
775f811a 365
4e781c7b 366 /// Define field completion to be added to course_modules
367 $table = new xmldb_table('course_modules');
368 $field = new xmldb_field('completion');
2a88f626 369 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'groupmembersonly');
4e781c7b 370
371 /// Launch add field completion
775f811a 372 if (!$dbman->field_exists($table,$field)) {
4e781c7b 373 $dbman->add_field($table, $field);
374 }
375
376 /// Define field completiongradeitemnumber to be added to course_modules
377 $field = new xmldb_field('completiongradeitemnumber');
2a88f626 378 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'completion');
4e781c7b 379
380 /// Launch add field completiongradeitemnumber
775f811a 381 if (!$dbman->field_exists($table,$field)) {
4e781c7b 382 $dbman->add_field($table, $field);
383 }
384
385 /// Define field completionview to be added to course_modules
386 $field = new xmldb_field('completionview');
2a88f626 387 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiongradeitemnumber');
4e781c7b 388
389 /// Launch add field completionview
775f811a 390 if (!$dbman->field_exists($table,$field)) {
4e781c7b 391 $dbman->add_field($table, $field);
392 }
393
394 /// Define field completionexpected to be added to course_modules
395 $field = new xmldb_field('completionexpected');
2a88f626 396 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionview');
4e781c7b 397
398 /// Launch add field completionexpected
775f811a 399 if (!$dbman->field_exists($table,$field)) {
4e781c7b 400 $dbman->add_field($table, $field);
401 }
402
403 /// Define table course_modules_completion to be created
404 $table = new xmldb_table('course_modules_completion');
775f811a 405 if (!$dbman->table_exists($table)) {
4e781c7b 406
407 /// Adding fields to table course_modules_completion
2a88f626 408 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
409 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
410 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
411 $table->add_field('completionstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
412 $table->add_field('viewed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
413 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
775f811a 414
4e781c7b 415 /// Adding keys to table course_modules_completion
1e0c56ea 416 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
775f811a 417
4e781c7b 418 /// Adding indexes to table course_modules_completion
1e0c56ea 419 $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
420 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
775f811a 421
4e781c7b 422 /// Launch create table for course_modules_completion
423 $dbman->create_table($table);
424 }
425
426 /// Main savepoint reached
427 upgrade_main_savepoint($result, 2008072800);
428 }
775f811a 429
4ab16a09 430 if ($result && $oldversion < 2008073000) {
ac5efef6 431
4ab16a09 432 /// Define table portfolio_log to be created
433 $table = new xmldb_table('portfolio_log');
434
435 /// Adding fields to table portfolio_log
2a88f626 436 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
437 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
438 $table->add_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
439 $table->add_field('portfolio', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
440 $table->add_field('caller_class', XMLDB_TYPE_CHAR, '150', null, XMLDB_NOTNULL, null, null);
441 $table->add_field('caller_file', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
442 $table->add_field('caller_sha1', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
4ab16a09 443
444 /// Adding keys to table portfolio_log
445 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
446 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
447 $table->add_key('portfoliofk', XMLDB_KEY_FOREIGN, array('portfolio'), 'portfolio_instance', array('id'));
448
449 /// Conditionally launch create table for portfolio_log
450 if (!$dbman->table_exists($table)) {
451 $dbman->create_table($table);
452 }
453
454 /// Main savepoint reached
455 upgrade_main_savepoint($result, 2008073000);
456 }
120b3758 457
458 if ($result && $oldversion < 2008073104) {
459 /// Drop old table that might exist for some people
460 $table = new xmldb_table('message_providers');
461 if ($dbman->table_exists($table)) {
462 $dbman->drop_table($table);
463 }
464
465 /// Define table message_providers to be created
466 $table = new xmldb_table('message_providers');
467
468 /// Adding fields to table message_providers
2a88f626 469 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
470 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
471 $table->add_field('component', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
472 $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
120b3758 473
474 /// Adding keys to table message_providers
475 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
476
477 /// Adding indexes to table message_providers
478 $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
479
480 /// Create table for message_providers
481 $dbman->create_table($table);
482
483 upgrade_main_savepoint($result, 2008073104);
484 }
172dd12c 485
486 if ($result && $oldversion < 2008073111) {
487 /// Define table files to be created
488 $table = new xmldb_table('files');
489
490 /// Adding fields to table files
2a88f626 491 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
492 $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
493 $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
494 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
495 $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
496 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
497 $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
498 $table->add_field('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
499 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
500 $table->add_field('filesize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
501 $table->add_field('mimetype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
502 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
503 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
504 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
172dd12c 505
506 /// Adding keys to table files
507 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
508 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
509 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
510
511 /// Adding indexes to table files
512 $table->add_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid'));
513 $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
514 $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
515
516 /// Conditionally launch create table for files
517 $dbman->create_table($table);
518
519 /// Main savepoint reached
520 upgrade_main_savepoint($result, 2008073111);
521 }
522
172dd12c 523 if ($result && $oldversion < 2008073113) {
524 /// move all course, backup and other files to new filepool based storage
525 upgrade_migrate_files_courses();
526 /// Main savepoint reached
527 upgrade_main_savepoint($result, 2008073113);
528 }
529
530 if ($result && $oldversion < 2008073114) {
531 /// move all course, backup and other files to new filepool based storage
532 upgrade_migrate_files_blog();
533 /// Main savepoint reached
534 upgrade_main_savepoint($result, 2008073114);
535 }
f33e1ed4 536
1ce2da58 537 if ($result && $oldversion < 2008080400) {
538 // Add field ssl_jump_url to mnet application, and populate existing default applications
539 $table = new xmldb_table('mnet_application');
540 $field = new xmldb_field('sso_jump_url');
541 if (!$dbman->field_exists($table, $field)) {
2a88f626 542 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
c5a04d12 543 $dbman->add_field($table, $field);
544 $result = $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
1ce2da58 545 $result = $result && $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
546 }
547
548 /// Main savepoint reached
549 upgrade_main_savepoint($result, 2008080400);
550 }
04f35360 551
552 if ($result && $oldversion < 2008080500) {
553
554 /// Define table portfolio_tempdata to be created
555 $table = new xmldb_table('portfolio_tempdata');
556
557 /// Adding fields to table portfolio_tempdata
2a88f626 558 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
559 $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
04f35360 560
561 /// Adding keys to table portfolio_tempdata
562 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
563
564 /// Conditionally launch create table for portfolio_tempdata
565 if (!$dbman->table_exists($table)) {
566 $dbman->create_table($table);
567 }
568
569 /// Main savepoint reached
570 upgrade_main_savepoint($result, 2008080500);
571 }
572
84a44985 573 if ($result && $oldversion < 2008080600) {
574
575 $DB->delete_records('portfolio_tempdata'); // there shouldnt' be any, and it will cause problems with this upgrade.
576 /// Define field expirytime to be added to portfolio_tempdata
577 $table = new xmldb_table('portfolio_tempdata');
2a88f626 578 $field = new xmldb_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'data');
84a44985 579
580 /// Conditionally launch add field expirytime
581 if (!$dbman->field_exists($table, $field)) {
582 $dbman->add_field($table, $field);
583 }
584
585 /// Main savepoint reached
586 upgrade_main_savepoint($result, 2008080600);
587 }
775f811a 588
c1f58efe 589 if ($result && $oldversion < 2008081500) {
590 /// Changing the type of all the columns that the question bank uses to store grades to be NUMBER(12, 7).
591 $table = new xmldb_table('question');
592 $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'generalfeedback');
593 $dbman->change_field_type($table, $field);
594 upgrade_main_savepoint($result, 2008081500);
595 }
596
f9a2cf86 597 if ($result && $oldversion < 2008081501) {
598 $table = new xmldb_table('question');
2a88f626 599 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'defaultgrade');
f9a2cf86 600 $dbman->change_field_type($table, $field);
601 upgrade_main_savepoint($result, 2008081501);
602 }
603
604 if ($result && $oldversion < 2008081502) {
605 $table = new xmldb_table('question_answers');
2a88f626 606 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'answer');
f9a2cf86 607 $dbman->change_field_type($table, $field);
608 upgrade_main_savepoint($result, 2008081502);
609 }
610
611 if ($result && $oldversion < 2008081503) {
612 $table = new xmldb_table('question_sessions');
2a88f626 613 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'newgraded');
f9a2cf86 614 $dbman->change_field_type($table, $field);
615 upgrade_main_savepoint($result, 2008081503);
616 }
617
618 if ($result && $oldversion < 2008081504) {
619 $table = new xmldb_table('question_states');
2a88f626 620 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'event');
f9a2cf86 621 $dbman->change_field_type($table, $field);
622 upgrade_main_savepoint($result, 2008081504);
623 }
624
625 if ($result && $oldversion < 2008081505) {
626 $table = new xmldb_table('question_states');
2a88f626 627 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'grade');
f9a2cf86 628 $dbman->change_field_type($table, $field);
629 upgrade_main_savepoint($result, 2008081505);
630 }
631
632 if ($result && $oldversion < 2008081506) {
633 $table = new xmldb_table('question_states');
2a88f626 634 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'raw_grade');
f9a2cf86 635 $dbman->change_field_type($table, $field);
636 upgrade_main_savepoint($result, 2008081506);
637 }
638
775f811a 639 if ($result && $oldversion < 2008081600) {
640
641 /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
642 unset_config('unicodedb');
643
644 /// Main savepoint reached
645 upgrade_main_savepoint($result, 2008081600);
646 }
647
ff2f69bc 648 if ($result && $oldversion < 2008081900) {
649 /// Define field userid to be added to portfolio_tempdata
650 $table = new xmldb_table('portfolio_tempdata');
2a88f626 651 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'expirytime');
ff2f69bc 652
653 /// Conditionally launch add field userid
654 if (!$dbman->field_exists($table, $field)) {
655 $dbman->add_field($table, $field);
656 }
657 $DB->set_field('portfolio_tempdata', 'userid', 0);
658 /// now change it to be notnull
659
660 /// Changing nullability of field userid on table portfolio_tempdata to not null
2a88f626 661 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'expirytime');
ff2f69bc 662
663 /// Launch change of nullability for field userid
664 $dbman->change_field_notnull($table, $field);
665
666 /// Define key userfk (foreign) to be added to portfolio_tempdata
667 $table = new xmldb_table('portfolio_tempdata');
668 $key = new xmldb_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
669
670 /// Launch add key userfk
671 $dbman->add_key($table, $key);
672
673 upgrade_main_savepoint($result, 2008081900);
674 }
8f3f6cc3 675 if ($result && $oldversion < 2008082602) {
27051e43 676
677 /// Define table repository to be dropped
678 $table = new xmldb_table('repository');
679
680 /// Conditionally launch drop table for repository
681 if ($dbman->table_exists($table)) {
682 $dbman->drop_table($table);
683 }
684
685 /// Define table repository to be created
686 $table = new xmldb_table('repository');
687
688 /// Adding fields to table repository
2a88f626 689 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
690 $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
691 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1');
692 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
27051e43 693
694 /// Adding keys to table repository
695 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
696
697 /// Conditionally launch create table for repository
698 if (!$dbman->table_exists($table)) {
699 $dbman->create_table($table);
700 }
701 /// Define table repository_instances to be created
702 $table = new xmldb_table('repository_instances');
703
704 /// Adding fields to table repository_instances
2a88f626 705 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
706 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
707 $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
708 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
709 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
710 $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null);
711 $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
712 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
713 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
27051e43 714
715 /// Adding keys to table repository_instances
716 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
717
718 /// Conditionally launch create table for repository_instances
719 if (!$dbman->table_exists($table)) {
720 $dbman->create_table($table);
721 }
722
723 /// Define table repository_instance_config to be created
724 $table = new xmldb_table('repository_instance_config');
725
726 /// Adding fields to table repository_instance_config
2a88f626 727 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
728 $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
729 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
730 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
27051e43 731
732 /// Adding keys to table repository_instance_config
733 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
734
735 /// Conditionally launch create table for repository_instance_config
736 if (!$dbman->table_exists($table)) {
737 $dbman->create_table($table);
738 }
739
740 /// Main savepoint reached
8f3f6cc3 741 upgrade_main_savepoint($result, 2008082602);
27051e43 742 }
743
62e76c67 744 if ($result && $oldversion < 2008082700) {
745 /// Add a new column to the question sessions table to record whether a
746 /// question has been flagged.
747
748 /// Define field flagged to be added to question_sessions
749 $table = new xmldb_table('question_sessions');
2a88f626 750 $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
62e76c67 751
752 /// Conditionally launch add field flagged
753 if (!$dbman->field_exists($table, $field)) {
754 $dbman->add_field($table, $field);
755 }
756
757 /// Main savepoint reached
758 upgrade_main_savepoint($result, 2008082700);
759 }
ff2f69bc 760
2c48aeab 761 if ($result && $oldversion < 2008082900) {
762
763 /// Changing precision of field parent_type on table mnet_rpc to (20)
764 $table = new xmldb_table('mnet_rpc');
2a88f626 765 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2c48aeab 766
767 /// Launch change of precision for field parent_type
768 $dbman->change_field_precision($table, $field);
769
770 /// Main savepoint reached
771 upgrade_main_savepoint($result, 2008082900);
772 }
773
ff7e7f92 774 if ($result && $oldversion < 2008090108) {
775 $repo = new object();
776 $repo->type = 'upload';
777 $repo->visible = 1;
778 $repo->sortorder = 1;
779 if (!$DB->record_exists('repository', array('type'=>'upload'))) {
780 $typeid = $DB->insert_record('repository', $repo);
781 }else{
782 $record = $DB->get_record('repository', array('type'=>'upload'));
783 $typeid = $record->id;
784 }
785 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
786 $instance = new object();
fb7cae3b 787 $instance->name = get_string('repositoryname', 'repository_upload');
ff7e7f92 788 $instance->typeid = $typeid;
789 $instance->userid = 0;
790 $instance->contextid = SITEID;
791 $instance->timecreated = time();
792 $instance->timemodified = time();
793 $result = $result && $DB->insert_record('repository_instances', $instance);
794 }
795 $repo->type = 'local';
796 $repo->visible = 1;
797 $repo->sortorder = 1;
798 if (!$DB->record_exists('repository', array('type'=>'local'))) {
799 $typeid = $DB->insert_record('repository', $repo);
800 }else{
801 $record = $DB->get_record('repository', array('type'=>'local'));
802 $typeid = $record->id;
803 }
804 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
805 $instance = new object();
fb7cae3b 806 $instance->name = get_string('repositoryname', 'repository_local');
ff7e7f92 807 $instance->typeid = $typeid;
808 $instance->userid = 0;
809 $instance->contextid = SITEID;
810 $instance->timecreated = time();
811 $instance->timemodified = time();
812 $result = $result && $DB->insert_record('repository_instances', $instance);
813 }
814
815 upgrade_main_savepoint($result, 2008090108);
816 }
817
d849880e 818 // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
819 if ($result && $oldversion < 2008091000) {
820 foreach (get_object_vars($CFG) as $name => $value) {
821 if (substr($name, strlen($name) - 8) !== '_version') {
822 continue;
823 }
824 $pluginname = substr($name, 0, strlen($name) - 8);
825 if (!strpos($pluginname, '_')) {
826 // Skip things like backup_version that don't contain an extra _
827 continue;
828 }
829 if ($pluginname == 'enrol_ldap_version') {
830 // Special case - this is something different from a plugin version number.
831 continue;
832 }
6f49dd0f 833 if (!preg_match('/^\d{10}$/', $value)) {
d849880e 834 // Extra safety check, skip anything that does not look like a Moodle
835 // version number (10 digits).
836 continue;
837 }
838 $result = $result && set_config('version', $value, $pluginname);
839 $result = $result && unset_config($name);
840 }
841 upgrade_main_savepoint($result, 2008091000);
842 }
843
948c2860 844 //Add a readonly field to the repository_instances table
845 //in order to support instance created automatically by a repository plugin
846 if ($result && $oldversion < 2008091611) {
847
848 /// Define field readonly to be added to repository_instances
849 $table = new xmldb_table('repository_instances');
2a88f626 850 $field = new xmldb_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
948c2860 851
852 /// Conditionally launch add field readonly
853 if (!$dbman->field_exists($table, $field)) {
854 $dbman->add_field($table, $field);
855 }
856
857 /// Main savepoint reached
858 upgrade_main_savepoint($result, 2008091611);
859 }
860
53b20fe3 861 if ($result && $oldversion < 2008092300) {
862 unset_config('editorspelling');
863 unset_config('editordictionary');
864 /// Main savepoint reached
865 upgrade_main_savepoint($result, 2008092300);
866 }
867
a7df430b 868 if ($result && $oldversion < 2008101000) {
869
870 /// Changing the default of field lang on table user to en_utf8
871 $table = new xmldb_table('user');
2a88f626 872 $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en_utf8', 'country');
a7df430b 873
874 /// Launch change of default for field lang
875 $dbman->change_field_default($table, $field);
876
877 /// Main savepoint reached
878 upgrade_main_savepoint($result, 2008101000);
879 }
880
5e7206a8 881 if ($result && $oldversion < 2008101300) {
882
d9e673c1 883 if (!get_config(NULL, 'statsruntimedays')) {
5e7206a8 884 set_config('statsruntimedays', '31');
885 }
886
887 /// Main savepoint reached
888 upgrade_main_savepoint($result, 2008101300);
889 }
53b20fe3 890
01a80f51 891 /// New table for storing which roles can be assigned in which contexts.
892 if ($result && $oldversion < 2008110601) {
893
894 /// Define table role_context_levels to be created
895 $table = new xmldb_table('role_context_levels');
896
897 /// Adding fields to table role_context_levels
2a88f626 898 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
899 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
900 $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01a80f51 901
902 /// Adding keys to table role_context_levels
903 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
904 $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
905 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
906
907 /// Conditionally launch create table for role_context_levels
908 if (!$dbman->table_exists($table)) {
909 $dbman->create_table($table);
910 }
911
912 /// Main savepoint reached
913 upgrade_main_savepoint($result, 2008110601);
914 }
915
916 /// Now populate the role_context_levels table with the defaults that match
917 /// moodle_install_roles, and any other combinations that exist in this system.
918 if ($result && $oldversion < 2008110602) {
919 $roleids = $DB->get_records_menu('role', array(), '', 'shortname,id');
920
921 /// Defaults, should match moodle_install_roles.
922 $rolecontextlevels = array();
923 if (isset($roleids['admin'])) {
924 $rolecontextlevels[$roleids['admin']] = get_default_contextlevels('admin');
925 }
926 if (isset($roleids['coursecreator'])) {
927 $rolecontextlevels[$roleids['coursecreator']] = get_default_contextlevels('coursecreator');
928 }
929 if (isset($roleids['editingteacher'])) {
930 $rolecontextlevels[$roleids['editingteacher']] = get_default_contextlevels('editingteacher');
931 }
932 if (isset($roleids['teacher'])) {
933 $rolecontextlevels[$roleids['teacher']] = get_default_contextlevels('teacher');
934 }
935 if (isset($roleids['student'])) {
936 $rolecontextlevels[$roleids['student']] = get_default_contextlevels('student');
937 }
938 if (isset($roleids['guest'])) {
939 $rolecontextlevels[$roleids['guest']] = get_default_contextlevels('guest');
940 }
941 if (isset($roleids['user'])) {
942 $rolecontextlevels[$roleids['user']] = get_default_contextlevels('user');
943 }
656be893 944
01a80f51 945 /// See what other role assignments are in this database, extend the allowed
946 /// lists to allow them too.
947 $existingrolecontextlevels = $DB->get_recordset_sql('SELECT DISTINCT ra.roleid, con.contextlevel FROM
948 {role_assignments} ra JOIN {context} con ON ra.contextid = con.id');
949 foreach ($existingrolecontextlevels as $rcl) {
950 if (!isset($rolecontextlevels[$rcl->roleid])) {
951 $rolecontextlevels[$rcl->roleid] = array($rcl->contextlevel);
952 } else if (!in_array($rcl->contextlevel, $rolecontextlevels[$rcl->roleid])) {
953 $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
954 }
955 }
956
957 /// Put the data into the database.
958 foreach ($rolecontextlevels as $roleid => $contextlevels) {
959 set_role_contextlevels($roleid, $contextlevels);
960 }
961
962 /// Main savepoint reached
963 upgrade_main_savepoint($result, 2008110602);
964 }
965
06de498b 966 /// Remove any role overrides for moodle/site:doanything, or any permissions
967 /// for it in a role without legacy:admin.
968 if ($result && $oldversion < 2008110603) {
969 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
970
971 // Remove all overrides.
972 $DB->delete_records_select('role_capabilities', 'capability = ? AND contextid <> ?', array('moodle/site:doanything', $systemcontext->id));
973
974 // Get the ids of all the roles that are moodle/legacy:admin.
975 $adminroleids = $DB->get_records_menu('role_capabilities',
976 array('capability' => 'moodle/legacy:admin', 'permission' => 1, 'contextid' => $systemcontext->id),
977 '', 'id, roleid');
978
979 // Remove moodle/site:doanything from all other roles.
980 list($notroletest, $params) = $DB->get_in_or_equal($adminroleids, SQL_PARAMS_QM, '', false);
981 $DB->delete_records_select('role_capabilities', "roleid $notroletest AND capability = ? AND contextid = ?",
982 array_merge($params, array('moodle/site:doanything', $systemcontext->id)));
983
984 // Ensure that for all admin-y roles, the permission for moodle/site:doanything is 1
985 list($isroletest, $params) = $DB->get_in_or_equal($adminroleids);
986 $DB->set_field_select('role_capabilities', 'permission', 1,
987 "roleid $isroletest AND capability = ? AND contextid = ?",
988 array_merge($params, array('moodle/site:doanything', $systemcontext->id)));
989
990 // And for any admin-y roles where moodle/site:doanything is not set, set it.
991 $doanythingroleids = $DB->get_records_menu('role_capabilities',
992 array('capability' => 'moodle/site:doanything', 'permission' => 1, 'contextid' => $systemcontext->id),
993 '', 'id, roleid');
994 foreach ($adminroleids as $roleid) {
995 if (!in_array($roleid, $doanythingroleids)) {
996 $rc = new stdClass;
997 $rc->contextid = $systemcontext->id;
998 $rc->roleid = $roleid;
999 $rc->capability = 'moodle/site:doanything';
1000 $rc->permission = 1;
1001 $rc->timemodified = time();
1002 $DB->insert_record('role_capabilities', $rc);
1003 }
1004 }
1005
1006 /// Main savepoint reached
1007 upgrade_main_savepoint($result, 2008110603);
1008 }
1009
9101efd3 1010 /// Drop the deprecated teacher, teachers, student and students columns from the course table.
1011 if ($result && $oldversion < 2008111200) {
1012 $table = new xmldb_table('course');
1013
1014 /// Conditionally launch drop field teacher
1015 $field = new xmldb_field('teacher');
1016 if ($dbman->field_exists($table, $field)) {
1017 $dbman->drop_field($table, $field);
1018 }
1019
1020 /// Conditionally launch drop field teacher
1021 $field = new xmldb_field('teachers');
1022 if ($dbman->field_exists($table, $field)) {
1023 $dbman->drop_field($table, $field);
1024 }
1025
1026 /// Conditionally launch drop field teacher
1027 $field = new xmldb_field('student');
1028 if ($dbman->field_exists($table, $field)) {
1029 $dbman->drop_field($table, $field);
1030 }
1031
1032 /// Conditionally launch drop field teacher
1033 $field = new xmldb_field('students');
1034 if ($dbman->field_exists($table, $field)) {
1035 $dbman->drop_field($table, $field);
1036 }
1037
1038 /// Main savepoint reached
1039 upgrade_main_savepoint($result, 2008111200);
1040 }
1041
40c792c3 1042/// Add a unique index to the role.name column.
1043 if ($result && $oldversion < 2008111800) {
1044
1045 /// Define index name (unique) to be added to role
1046 $table = new xmldb_table('role');
1047 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
1048
1049 /// Conditionally launch add index name
1050 if (!$dbman->index_exists($table, $index)) {
1051 $dbman->add_index($table, $index);
1052 }
1053
1054 /// Main savepoint reached
1055 upgrade_main_savepoint($result, 2008111800);
1056 }
1057
1058/// Add a unique index to the role.shortname column.
1059 if ($result && $oldversion < 2008111801) {
1060
1061 /// Define index shortname (unique) to be added to role
1062 $table = new xmldb_table('role');
1063 $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
1064
1065 /// Conditionally launch add index shortname
1066 if (!$dbman->index_exists($table, $index)) {
1067 $dbman->add_index($table, $index);
1068 }
1069
1070 /// Main savepoint reached
1071 upgrade_main_savepoint($result, 2008111801);
1072 }
1073
82bd6a5e 1074 if ($result && $oldversion < 2008120700) {
1075
1076 /// Changing precision of field shortname on table course_request to (100)
1077 $table = new xmldb_table('course_request');
2a88f626 1078 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
82bd6a5e 1079
1080 /// Launch change of precision for field shortname
1081 $dbman->change_field_precision($table, $field);
1082
1083 /// Main savepoint reached
1084 upgrade_main_savepoint($result, 2008120700);
1085 }
1086
1087 /// For MDL-17501. Ensure that any role that has moodle/course:update also
1088 /// has moodle/course:visibility.
1089 if ($result && $oldversion < 2008120800) {
1090 /// Get the roles with 'moodle/course:update'.
1091 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
1092 $roles = get_roles_with_capability('moodle/course:update', CAP_ALLOW, $systemcontext);
1093
1094 /// Give those roles 'moodle/course:visibility'.
1095 foreach ($roles as $role) {
1096 assign_capability('moodle/course:visibility', CAP_ALLOW, $role->id, $systemcontext->id);
1097 }
1098
1099 /// Force all sessions to refresh access data.
1100 mark_context_dirty($systemcontext->path);
1101
1102 /// Main savepoint reached
1103 upgrade_main_savepoint($result, 2008120800);
1104 }
1105
1106 if ($result && $oldversion < 2008120801) {
1107
1108 /// Changing precision of field shortname on table mnet_enrol_course to (100)
1109 $table = new xmldb_table('mnet_enrol_course');
2a88f626 1110 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
82bd6a5e 1111
1112 /// Launch change of precision for field shortname
1113 $dbman->change_field_precision($table, $field);
1114
1115 /// Main savepoint reached
1116 upgrade_main_savepoint($result, 2008120801);
1117 }
1118
1119 if ($result && $oldversion < 2008121701) {
0953a4e7 1120
1121 /// Define field availablefrom to be added to course_modules
1122 $table = new xmldb_table('course_modules');
2a88f626 1123 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
0953a4e7 1124
1125 /// Conditionally launch add field availablefrom
1126 if (!$dbman->field_exists($table, $field)) {
1127 $dbman->add_field($table, $field);
1128 }
1129
1130 /// Define field availableuntil to be added to course_modules
2a88f626 1131 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
0953a4e7 1132
1133 /// Conditionally launch add field availableuntil
1134 if (!$dbman->field_exists($table, $field)) {
1135 $dbman->add_field($table, $field);
1136 }
82bd6a5e 1137
0953a4e7 1138 /// Define field showavailability to be added to course_modules
2a88f626 1139 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
0953a4e7 1140
1141 /// Conditionally launch add field showavailability
1142 if (!$dbman->field_exists($table, $field)) {
1143 $dbman->add_field($table, $field);
1144 }
82bd6a5e 1145
0953a4e7 1146 /// Define table course_modules_availability to be created
1147 $table = new xmldb_table('course_modules_availability');
1148
1149 /// Adding fields to table course_modules_availability
2a88f626 1150 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1151 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1152 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1153 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
1154 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1155 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1156 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
0953a4e7 1157
1158 /// Adding keys to table course_modules_availability
1159 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
cbb17d54 1160 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
1161 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
1162 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
0953a4e7 1163
1164 /// Conditionally launch create table for course_modules_availability
1165 if (!$dbman->table_exists($table)) {
1166 $dbman->create_table($table);
1167 }
1168
82bd6a5e 1169 /// Changes to modinfo mean we need to rebuild course cache
a9a0b93d 1170 require_once($CFG->dirroot . '/course/lib.php');
8b6a0968 1171 rebuild_course_cache(0, true);
9136a60c 1172
82bd6a5e 1173 /// For developer upgrades, turn on the conditional activities and completion
1174 /// features automatically (to gain more testing)
8b6a0968 1175//TODO: remove before 2.0 final!
1176 if (debugging('', DEBUG_DEVELOPER)) {
1177 set_config('enableavailability', 1);
1178 set_config('enablecompletion', 1);
9136a60c 1179 }
1180
9d510a2e 1181 /// Main savepoint reached
82bd6a5e 1182 upgrade_main_savepoint($result, 2008121701);
9d510a2e 1183 }
1184
656be893 1185 if ($result && $oldversion < 2009010500) {
1186 /// clean up config table a bit
1187 unset_config('session_error_counter');
1188
1189 /// Main savepoint reached
1190 upgrade_main_savepoint($result, 2009010500);
1191 }
1192
1193 if ($result && $oldversion < 2009010600) {
1194
1195 /// Define field originalquestion to be dropped from question_states
1196 $table = new xmldb_table('question_states');
1197 $field = new xmldb_field('originalquestion');
1198
1199 /// Conditionally launch drop field originalquestion
1200 if ($dbman->field_exists($table, $field)) {
1201 $dbman->drop_field($table, $field);
1202 }
1203
1204 /// Main savepoint reached
1205 upgrade_main_savepoint($result, 2009010600);
1206 }
1207
1208 if ($result && $oldversion < 2009010601) {
1209
1210 /// Changing precision of field ip on table log to (45)
1211 $table = new xmldb_table('log');
2a88f626 1212 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
656be893 1213
1214 /// Launch change of precision for field ip
1215 $dbman->change_field_precision($table, $field);
1216
1217 /// Main savepoint reached
1218 upgrade_main_savepoint($result, 2009010601);
1219 }
1220
1221 if ($result && $oldversion < 2009010602) {
1222
1223 /// Changing precision of field lastip on table user to (45)
1224 $table = new xmldb_table('user');
2a88f626 1225 $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
656be893 1226
1227 /// Launch change of precision for field lastip
1228 $dbman->change_field_precision($table, $field);
1229
1230 /// Main savepoint reached
1231 upgrade_main_savepoint($result, 2009010602);
1232 }
1233
1234 if ($result && $oldversion < 2009010603) {
1235
1236 /// Changing precision of field ip_address on table mnet_host to (45)
1237 $table = new xmldb_table('mnet_host');
2a88f626 1238 $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
656be893 1239
1240 /// Launch change of precision for field ip_address
1241 $dbman->change_field_precision($table, $field);
1242
1243 /// Main savepoint reached
1244 upgrade_main_savepoint($result, 2009010603);
1245 }
1246
1247 if ($result && $oldversion < 2009010604) {
1248
1249 /// Changing precision of field ip on table mnet_log to (45)
1250 $table = new xmldb_table('mnet_log');
2a88f626 1251 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
656be893 1252
1253 /// Launch change of precision for field ip
1254 $dbman->change_field_precision($table, $field);
1255
1256 /// Main savepoint reached
1257 upgrade_main_savepoint($result, 2009010604);
1258 }
1259
4413941f 1260 if ($result && $oldversion < 2009010800) {
1261 /// Update the notifyloginfailures setting.
1262 if ($CFG->notifyloginfailures == 'mainadmin') {
1263 set_config('notifyloginfailures', get_admin()->username);
1264 } else if ($CFG->notifyloginfailures == 'alladmins') {
1265 set_config('notifyloginfailures', '$@ALL@$');
1266 } else {
1267 set_config('notifyloginfailures', '');
1268 }
1269
1270 /// Main savepoint reached
1271 upgrade_main_savepoint($result, 2009010800);
1272 }
1273
ab2eb65c 1274 if ($result && $oldversion < 2009011000) {
1275
1276 /// Changing nullability of field configdata on table block_instance to null
1277 $table = new xmldb_table('block_instance');
1278 $field = new xmldb_field('configdata');
2a88f626 1279 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
ab2eb65c 1280
1281 /// Launch change of nullability for field configdata
1282 $dbman->change_field_notnull($table, $field);
1283
1284 /// Main savepoint reached
1285 upgrade_main_savepoint($result, 2009011000);
1286 }
1287
1caea91e 1288 if ($result && $oldversion < 2009011100) {
1289 /// Remove unused settings
1290 unset_config('zip');
1291 unset_config('unzip');
1292 unset_config('adminblocks_initialised');
1293
1294 /// Main savepoint reached
1295 upgrade_main_savepoint($result, 2009011100);
1296 }
1297
eb6a973c 1298 if ($result && $oldversion < 2009011101) {
1299 /// Migrate backup settings to core plugin config table
1300 $configs = $DB->get_records('backup_config');
1301 foreach ($configs as $config) {
1302 set_config($config->name, $config->value, 'backup');
1303 }
1304
1305 /// Define table to be dropped
1306 $table = new xmldb_table('backup_config');
1307
1308 /// Launch drop table for old backup config
1309 $dbman->drop_table($table);
1310
1311 /// Main savepoint reached
1312 upgrade_main_savepoint($result, 2009011101);
1313 }
1314
301bf0b2 1315 if ($result && $oldversion < 2009011303) {
1316
1317 /// Define table config_log to be created
1318 $table = new xmldb_table('config_log');
1319
1320 /// Adding fields to table config_log
2a88f626 1321 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1322 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1323 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1324 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1325 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1326 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1327 $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
301bf0b2 1328
1329 /// Adding keys to table config_log
1330 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1331 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1332
1333 /// Adding indexes to table config_log
1334 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1335
1336 /// Launch create table for config_log
1337 $dbman->create_table($table);
1338
1339 /// Main savepoint reached
1340 upgrade_main_savepoint($result, 2009011303);
1341 }
1342
b9fb7103 1343 if ($result && $oldversion < 2009011900) {
3f57bd45 1344
1345 /// Define table sessions2 to be dropped
1346 $table = new xmldb_table('sessions2');
1347
1348 /// Conditionally launch drop table for sessions
1349 if ($dbman->table_exists($table)) {
1350 $dbman->drop_table($table);
1351 }
1352
1353 /// Define table sessions to be dropped
1354 $table = new xmldb_table('sessions');
1355
1356 /// Conditionally launch drop table for sessions
1357 if ($dbman->table_exists($table)) {
1358 $dbman->drop_table($table);
1359 }
1360
1361 /// Define table sessions to be created
1362 $table = new xmldb_table('sessions');
1363
1364 /// Adding fields to table sessions
2a88f626 1365 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1366 $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1367 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
1368 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1369 $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
1370 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1371 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1372 $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1373 $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
3f57bd45 1374
1375 /// Adding keys to table sessions
1376 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1377 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1378
1379 /// Adding indexes to table sessions
1380 $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1381 $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1382 $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1383 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1384
1385 /// Launch create table for sessions
1386 $dbman->create_table($table);
1387
1388 /// Main savepoint reached
b9fb7103 1389 upgrade_main_savepoint($result, 2009011900);
3f57bd45 1390 }
1391
dc6f76b9 1392 if ($result && $oldversion < 2009012901) {
795a08ad 1393 // NOTE: this table may already exist, see beginning of this file ;-)
1f20942c 1394
1395 /// Define table upgrade_log to be created
1396 $table = new xmldb_table('upgrade_log');
1397
1398 /// Adding fields to table upgrade_log
2a88f626 1399 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1400 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1401 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1402 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1403 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1404 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1405 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1406 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1407 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1f20942c 1408
1409 /// Adding keys to table upgrade_log
1410 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1411 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1412
1413 /// Adding indexes to table upgrade_log
1414 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1415 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
1416
1417 /// Conditionally launch create table for upgrade_log
1418 if (!$dbman->table_exists($table)) {
1419 $dbman->create_table($table);
1420 }
1421
1422 /// Main savepoint reached
dc6f76b9 1423 upgrade_main_savepoint($result, 2009012901);
1f20942c 1424 }
1caea91e 1425
75564228 1426 if ($result && $oldversion < 2009021800) {
eb2761f8 1427 // Converting format of grade conditions, if any exist, to percentages.
75564228 1428 $DB->execute("
1429UPDATE {course_modules_availability} SET grademin=(
1430 SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
eb2761f8 1431 /(gi.grademax-gi.grademin)
1432 FROM {grade_items} gi
75564228 1433 WHERE gi.id={course_modules_availability}.gradeitemid)
1434WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1435 $DB->execute("
1436UPDATE {course_modules_availability} SET grademax=(
1437 SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
eb2761f8 1438 /(gi.grademax-gi.grademin)
1439 FROM {grade_items} gi
75564228 1440 WHERE gi.id={course_modules_availability}.gradeitemid)
1441WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1442
1443 /// Main savepoint reached
1444 upgrade_main_savepoint($result, 2009021800);
1445 }
bc7ec91a 1446 if ($result && $oldversion < 2009021801) {
1447 /// Define field backuptype to be added to backup_log
382123a0 1448 $table = new xmldb_table('backup_log');
2a88f626 1449 $field = new xmldb_field('backuptype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'info');
bc7ec91a 1450 /// Conditionally Launch add field backuptype and set all old records as 'scheduledbackup' records.
1451 if (!$dbman->field_exists($table, $field)) {
1452 $dbman->add_field($table, $field);
1453 $DB->execute("UPDATE {backup_log} SET backuptype='scheduledbackup'");
1454 }
75564228 1455
bc7ec91a 1456 /// Main savepoint reached
1457 upgrade_main_savepoint($result, 2009021801);
1458 }
af52ecee 1459 /// Add default sort order for question types.
1460 if ($result && $oldversion < 2009030300) {
1461 set_config('multichoice_sortorder', 1, 'question');
1462 set_config('truefalse_sortorder', 2, 'question');
1463 set_config('shortanswer_sortorder', 3, 'question');
1464 set_config('numerical_sortorder', 4, 'question');
1465 set_config('calculated_sortorder', 5, 'question');
1466 set_config('essay_sortorder', 6, 'question');
1467 set_config('match_sortorder', 7, 'question');
1468 set_config('randomsamatch_sortorder', 8, 'question');
1469 set_config('multianswer_sortorder', 9, 'question');
1470 set_config('description_sortorder', 10, 'question');
1471 set_config('random_sortorder', 11, 'question');
1472 set_config('missingtype_sortorder', 12, 'question');
1473
1474 upgrade_main_savepoint($result, 2009030300);
1475 }
a75c78d3 1476 if ($result && $oldversion < 2009030501) {
1477 /// setup default repository plugins
1478 require_once($CFG->dirroot . '/repository/lib.php');
1479 repository_setup_default_plugins();
1480 /// Main savepoint reached
1481 upgrade_main_savepoint($result, 2009030501);
1482 }
af52ecee 1483
82701e24 1484 /// MDL-18132 replace the use a new Role allow switch settings page, instead of
1485 /// $CFG->allowuserswitchrolestheycantassign
1486 if ($result && $oldversion < 2009032000) {
1487 /// First create the new table.
1488 $table = new xmldb_table('role_allow_switch');
1489
1490 /// Adding fields to table role_allow_switch
2a88f626 1491 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1492 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1493 $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
82701e24 1494
1495 /// Adding keys to table role_allow_switch
1496 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1497 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
1498 $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
1499
1500 /// Adding indexes to table role_allow_switch
1501 $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1502
1503 /// Conditionally launch create table for role_allow_switch
1504 if (!$dbman->table_exists($table)) {
1505 $dbman->create_table($table);
1506 }
1507
1508 /// Main savepoint reached
1509 upgrade_main_savepoint($result, 2009032000);
1510 }
0fc9c009 1511
82701e24 1512 if ($result && $oldversion < 2009032001) {
1513 /// Copy from role_allow_assign into the new table.
dc740f97 1514 $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1515 SELECT roleid, allowassign FROM {role_allow_assign}');
82701e24 1516
1517 /// Unset the config variable used in 1.9.
1518 unset_config('allowuserswitchrolestheycantassign');
1519
1520 /// Main savepoint reached
1521 upgrade_main_savepoint($result, 2009032001);
1522 }
c94985ef 1523
1524 if ($result && $oldversion < 2009033100) {
1525 require_once("$CFG->dirroot/filter/tex/lib.php");
1526 filter_tex_updatedcallback(null);
1527 /// Main savepoint reached
1528 upgrade_main_savepoint($result, 2009033100);
1529 }
1530
0fc9c009 1531 if ($result && $oldversion < 2009040300) {
1532
1533 /// Define table filter_active to be created
1534 $table = new xmldb_table('filter_active');
1535
1536 /// Adding fields to table filter_active
2a88f626 1537 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1538 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1539 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1540 $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1541 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
0fc9c009 1542
1543 /// Adding keys to table filter_active
1544 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1545 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1546
1547 /// Adding indexes to table filter_active
1548 $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1549
1550 /// Conditionally launch create table for filter_active
1551 if (!$dbman->table_exists($table)) {
1552 $dbman->create_table($table);
1553 }
1554
1555 /// Main savepoint reached
1556 upgrade_main_savepoint($result, 2009040300);
1557 }
1558
1559 if ($result && $oldversion < 2009040301) {
1560
1561 /// Define table filter_config to be created
1562 $table = new xmldb_table('filter_config');
1563
1564 /// Adding fields to table filter_config
2a88f626 1565 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1566 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1567 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1568 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1569 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
0fc9c009 1570
1571 /// Adding keys to table filter_config
1572 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1573 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1574
1575 /// Adding indexes to table filter_config
1576 $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1577
1578 /// Conditionally launch create table for filter_config
1579 if (!$dbman->table_exists($table)) {
1580 $dbman->create_table($table);
1581 }
1582
1583 /// Main savepoint reached
1584 upgrade_main_savepoint($result, 2009040301);
1585 }
1586
c07e6d8d 1587 if ($result && $oldversion < 2009040302) {
1588 /// Transfer current settings from $CFG->textfilters
1589 $disabledfilters = filter_get_all_installed();
1590 if (empty($CFG->textfilters)) {
1591 $activefilters = array();
1592 } else {
1593 $activefilters = explode(',', $CFG->textfilters);
1594 }
1595 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1596 $sortorder = 1;
1597 foreach ($activefilters as $filter) {
1598 filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1599 $sortorder += 1;
1600 unset($disabledfilters[$filter]);
1601 }
1602 foreach ($disabledfilters as $filter => $notused) {
1603 filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1604 $sortorder += 1;
1605 }
1606
1607 /// Main savepoint reached
1608 upgrade_main_savepoint($result, 2009040302);
1609 }
1610
bbb45905 1611 if ($result && $oldversion < 2009040600) {
1612 /// Ensure that $CFG->stringfilters is set.
1613 if (empty($CFG->stringfilters)) {
1614 if (!empty($CFG->filterall)) {
1615 set_config('stringfilters', $CFG->textfilters);
1616 } else {
1617 set_config('stringfilters', '');
1618 }
1619 }
1620
1621 set_config('filterall', !empty($CFG->stringfilters));
1622 unset_config('textfilters');
1623
1624 /// Main savepoint reached
1625 upgrade_main_savepoint($result, 2009040600);
1626 }
1627
e7c6bf0e 1628 if ($result && $oldversion < 2009041700) {
1629 /// To ensure the UI remains consistent with no behaviour change, any
1630 /// 'until' date in an activity condition should have 1 second subtracted
1631 /// (to go from 0:00 on the following day to 23:59 on the previous one).
1632 $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
6861b6fe 1633 require_once($CFG->dirroot . '/course/lib.php');
0f9534c1 1634 rebuild_course_cache(0, true);
e7c6bf0e 1635
1636 /// Main savepoint reached
1637 upgrade_main_savepoint($result, 2009041700);
1638 }
1639
6bdf4c99 1640 if ($result && $oldversion < 2009042600) {
1641 /// Deleting orphaned messages from deleted users.
1642 require_once($CFG->dirroot.'/message/lib.php');
1643 /// Detect deleted users with messages sent(useridfrom) and not read
1644 if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
1645 FROM {user} u
1646 JOIN {message} m ON m.useridfrom = u.id
1647 WHERE u.deleted = ?', array(1))) {
1648 foreach ($deletedusers as $deleteduser) {
1649 message_move_userfrom_unread2read($deleteduser->id); // move messages
1650 }
1651 }
1652 /// Main savepoint reached
1653 upgrade_main_savepoint($result, 2009042600);
1654 }
1655
e37cd84a 1656 /// Dropping all enums/check contraints from core. MDL-18577
1657 if ($result && $oldversion < 2009042700) {
1658
1659 /// Changing list of values (enum) of field stattype on table stats_daily to none
1660 $table = new xmldb_table('stats_daily');
2a88f626 1661 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
e37cd84a 1662
1663 /// Launch change of list of values for field stattype
2a88f626 1664 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1665
1666 /// Changing list of values (enum) of field stattype on table stats_weekly to none
1667 $table = new xmldb_table('stats_weekly');
2a88f626 1668 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
e37cd84a 1669
1670 /// Launch change of list of values for field stattype
2a88f626 1671 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1672
1673 /// Changing list of values (enum) of field stattype on table stats_monthly to none
1674 $table = new xmldb_table('stats_monthly');
2a88f626 1675 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
e37cd84a 1676
1677 /// Launch change of list of values for field stattype
2a88f626 1678 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1679
1680 /// Changing list of values (enum) of field publishstate on table post to none
1681 $table = new xmldb_table('post');
2a88f626 1682 $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
e37cd84a 1683
1684 /// Launch change of list of values for field publishstate
2a88f626 1685 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1686
1687 /// Main savepoint reached
1688 upgrade_main_savepoint($result, 2009042700);
1689 }
1690
c045e45a 1691 if ($result && $oldversion < 2009043000) {
1692 unset_config('grade_report_showgroups');
1693 upgrade_main_savepoint($result, 2009043000);
1694 }
1695
1696 if ($result && $oldversion < 2009050600) {
e88462a0 1697 /// Site front page blocks need to be moved due to page name change.
1698 $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
1699
1700 /// Main savepoint reached
c045e45a 1701 upgrade_main_savepoint($result, 2009050600);
e88462a0 1702 }
1703
c045e45a 1704 if ($result && $oldversion < 2009050601) {
66b10689 1705
3aceb097 1706 /// Define table block_instance to be renamed to block_instances
66b10689 1707 $table = new xmldb_table('block_instance');
1708
1709 /// Launch rename table for block_instance
3aceb097 1710 $dbman->rename_table($table, 'block_instances');
66b10689 1711
1712 /// Main savepoint reached
c045e45a 1713 upgrade_main_savepoint($result, 2009050601);
66b10689 1714 }
1715
c045e45a 1716 if ($result && $oldversion < 2009050602) {
66b10689 1717
1718 /// Define table block_instance to be renamed to block_instance_old
1719 $table = new xmldb_table('block_pinned');
1720
1721 /// Launch rename table for block_instance
1722 $dbman->rename_table($table, 'block_pinned_old');
1723
1724 /// Main savepoint reached
c045e45a 1725 upgrade_main_savepoint($result, 2009050602);
66b10689 1726 }
1727
c045e45a 1728 if ($result && $oldversion < 2009050603) {
66b10689 1729
1730 /// Define table block_instance_old to be created
1731 $table = new xmldb_table('block_instance_old');
1732
1733 /// Adding fields to table block_instance_old
2b212777 1734 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1735 $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1736 $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1737 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1738 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
1739 $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
1740 $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
1741 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1742 $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
66b10689 1743
1744 /// Adding keys to table block_instance_old
1745 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1746 $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
1747
1748 /// Adding indexes to table block_instance_old
1749 $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1750 $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
1751
1752 /// Conditionally launch create table for block_instance_old
1753 if (!$dbman->table_exists($table)) {
1754 $dbman->create_table($table);
1755 }
1756
1757 /// Main savepoint reached
c045e45a 1758 upgrade_main_savepoint($result, 2009050603);
66b10689 1759 }
1760
c045e45a 1761 if ($result && $oldversion < 2009050604) {
66b10689 1762 /// Copy current blocks data from block_instances to block_instance_old
35550fd6 1763 $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
1764 SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
66b10689 1765
c045e45a 1766 upgrade_main_savepoint($result, 2009050604);
66b10689 1767 }
1768
c045e45a 1769 if ($result && $oldversion < 2009050605) {
66b10689 1770
1771 /// Define field multiple to be dropped from block
1772 $table = new xmldb_table('block');
1773 $field = new xmldb_field('multiple');
1774
1775 /// Conditionally launch drop field multiple
1776 if ($dbman->field_exists($table, $field)) {
1777 $dbman->drop_field($table, $field);
1778 }
1779
1780 /// Main savepoint reached
c045e45a 1781 upgrade_main_savepoint($result, 2009050605);
66b10689 1782 }
1783
c045e45a 1784 if ($result && $oldversion < 2009050606) {
66b10689 1785 $table = new xmldb_table('block_instances');
1786
1787 /// Rename field weight on table block_instances to defaultweight
2b212777 1788 $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'position');
66b10689 1789 $dbman->rename_field($table, $field, 'defaultweight');
1790
1791 /// Rename field position on table block_instances to defaultregion
2b212777 1792 $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
66b10689 1793 $dbman->rename_field($table, $field, 'defaultregion');
1794
1795 /// Main savepoint reached
c045e45a 1796 upgrade_main_savepoint($result, 2009050606);
66b10689 1797 }
1798
c045e45a 1799 if ($result && $oldversion < 2009050607) {
66b10689 1800 /// Changing precision of field defaultregion on table block_instances to (16)
a26146d5 1801 $table = new xmldb_table('block_instances');
1802 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
66b10689 1803
1804 /// Launch change of precision for field defaultregion
1805 $dbman->change_field_precision($table, $field);
1806
1807 /// Main savepoint reached
a26146d5 1808 upgrade_main_savepoint($result, 2009050607);
66b10689 1809 }
1810
c045e45a 1811 if ($result && $oldversion < 2009050608) {
66b10689 1812 /// Change regions to the new notation
1813 $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1814 $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1815 $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1816 // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1817 // flex page course format. Hopefully this new value is an adequate alternative.
1818
1819 /// Main savepoint reached
c045e45a 1820 upgrade_main_savepoint($result, 2009050608);
66b10689 1821 }
1822
c045e45a 1823 if ($result && $oldversion < 2009050609) {
66b10689 1824
1825 /// Define key blockname (unique) to be added to block
1826 $table = new xmldb_table('block');
1827 $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1828
1829 /// Launch add key blockname
1830 $dbman->add_key($table, $key);
1831
1832 /// Main savepoint reached
c045e45a 1833 upgrade_main_savepoint($result, 2009050609);
66b10689 1834 }
1835
c045e45a 1836 if ($result && $oldversion < 2009050610) {
66b10689 1837 $table = new xmldb_table('block_instances');
1838
1839 /// Define field blockname to be added to block_instances
2b212777 1840 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
66b10689 1841 if (!$dbman->field_exists($table, $field)) {
1842 $dbman->add_field($table, $field);
1843 }
1844
1845 /// Define field contextid to be added to block_instances
2b212777 1846 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
66b10689 1847 if (!$dbman->field_exists($table, $field)) {
1848 $dbman->add_field($table, $field);
1849 }
1850
1851 /// Define field showinsubcontexts to be added to block_instances
2b212777 1852 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
66b10689 1853 if (!$dbman->field_exists($table, $field)) {
1854 $dbman->add_field($table, $field);
1855 }
1856
1857 /// Define field subpagepattern to be added to block_instances
2b212777 1858 $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
66b10689 1859 if (!$dbman->field_exists($table, $field)) {
1860 $dbman->add_field($table, $field);
1861 }
1862
1863 /// Main savepoint reached
c045e45a 1864 upgrade_main_savepoint($result, 2009050610);
66b10689 1865 }
1866
c045e45a 1867 if ($result && $oldversion < 2009050611) {
66b10689 1868 $table = new xmldb_table('block_instances');
1869
1870 /// Fill in blockname from blockid
1871 $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1872
1873 /// Set showinsubcontexts = 0 for all rows.
1874 $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1875
1876 /// Main savepoint reached
c045e45a 1877 upgrade_main_savepoint($result, 2009050611);
66b10689 1878 }
1879
c045e45a 1880 if ($result && $oldversion < 2009050612) {
66b10689 1881
1882 /// Rename field pagetype on table block_instances to pagetypepattern
1883 $table = new xmldb_table('block_instances');
2b212777 1884 $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
66b10689 1885
1886 /// Launch rename field pagetype
1887 $dbman->rename_field($table, $field, 'pagetypepattern');
1888
1889 /// Main savepoint reached
c045e45a 1890 upgrade_main_savepoint($result, 2009050612);
66b10689 1891 }
1892
c045e45a 1893 if ($result && $oldversion < 2009050613) {
66b10689 1894 /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1895
1896 /// site-index
1897 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
eb2761f8 1898 $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1899 pagetypepattern = 'site-index',
1900 subpagepattern = NULL
1901 WHERE pagetypepattern = 'site-index'");
66b10689 1902
1903 /// course-view
eb2761f8 1904 $DB->execute("UPDATE {block_instances} SET
1905 contextid = (
1906 SELECT {context}.id
1907 FROM {context}
1908 JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1909 WHERE {course}.id = pageid
1910 ),
1911 pagetypepattern = 'course-view-*',
1912 subpagepattern = NULL
1913 WHERE pagetypepattern = 'course-view'");
66b10689 1914
1915 /// admin
1916 $syscontext = get_context_instance(CONTEXT_SYSTEM);
eb2761f8 1917 $DB->execute("UPDATE {block_instances} SET
1918 contextid = " . $syscontext->id . ",
1919 pagetypepattern = 'admin-*',
1920 subpagepattern = NULL
1921 WHERE pagetypepattern = 'admin'");
66b10689 1922
1923 /// my-index
eb2761f8 1924 $DB->execute("UPDATE {block_instances} SET
1925 contextid = (
1926 SELECT {context}.id
1927 FROM {context}
1928 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1929 WHERE {user}.id = pageid
1930 ),
1931 pagetypepattern = 'my-index',
1932 subpagepattern = NULL
1933 WHERE pagetypepattern = 'my-index'");
66b10689 1934
1935 /// tag-index
eb2761f8 1936 $DB->execute("UPDATE {block_instances} SET
1937 contextid = " . $syscontext->id . ",
1938 pagetypepattern = 'tag-index',
1939 subpagepattern = pageid
1940 WHERE pagetypepattern = 'tag-index'");
66b10689 1941
1942 /// blog-view
eb2761f8 1943 $DB->execute("UPDATE {block_instances} SET
1944 contextid = (
1945 SELECT {context}.id
1946 FROM {context}
1947 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1948 WHERE {user}.id = pageid
1949 ),
1950 pagetypepattern = 'blog-index',
1951 subpagepattern = NULL
1952 WHERE pagetypepattern = 'blog-view'");
66b10689 1953
1954 /// mod-xxx-view
1955 $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1956 foreach ($moduleswithblocks as $modname) {
1957 if (!$dbman->table_exists($modname)) {
1958 continue;
1959 }
eb2761f8 1960 $DB->execute("UPDATE {block_instances} SET
1961 contextid = (
1962 SELECT {context}.id
1963 FROM {context}
1964 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1965 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1966 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1967 WHERE {{$modname}}.id = pageid
1968 ),
1969 pagetypepattern = 'blog-index',
1970 subpagepattern = NULL
1971 WHERE pagetypepattern = 'blog-view'");
66b10689 1972 }
1973
1974 /// Main savepoint reached
c045e45a 1975 upgrade_main_savepoint($result, 2009050613);
66b10689 1976 }
1977
c045e45a 1978 if ($result && $oldversion < 2009050614) {
66b10689 1979 /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
1980 $DB->execute("UPDATE {block_instances} SET contextid = -1 WHERE contextid IS NULL");
1981
1982 /// Main savepoint reached
c045e45a 1983 upgrade_main_savepoint($result, 2009050614);
66b10689 1984 }
1985
c045e45a 1986 if ($result && $oldversion < 2009050615) {
66b10689 1987 $table = new xmldb_table('block_instances');
1988
1989 /// Changing nullability of field blockname on table block_instances to not null
2b212777 1990 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
66b10689 1991 $dbman->change_field_notnull($table, $field);
1992
1993 /// Changing nullability of field contextid on table block_instances to not null
2b212777 1994 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
66b10689 1995 $dbman->change_field_notnull($table, $field);
1996
1997 /// Changing nullability of field showinsubcontexts on table block_instances to not null
2b212777 1998 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
66b10689 1999 $dbman->change_field_notnull($table, $field);
2000
2001 /// Main savepoint reached
c045e45a 2002 upgrade_main_savepoint($result, 2009050615);
66b10689 2003 }
2004
c045e45a 2005 if ($result && $oldversion < 2009050616) {
66b10689 2006 /// Add exiting sticky blocks.
2007 $blocks = $DB->get_records('block');
2008 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2009 $newregions = array(
2010 'l' => 'side-pre',
2011 'r' => 'side-post',
2012 'c' => 'course-view-top',
2013 );
2014 $stickyblocks = $DB->get_recordset('block_pinned_old');
2015 foreach ($stickyblocks as $stickyblock) {
2016 $newblock = stdClass;
2017 $newblock->blockname = $blocks[$stickyblock]->name;
2018 $newblock->contextid = $syscontext->id;
2019 $newblock->showinsubcontexts = 1;
2020 switch ($stickyblock->pagetype) {
2021 case 'course-view':
2022 $newblock->pagetypepattern = 'course-view-*';
2023 break;
2024 default:
2025 $newblock->pagetypepattern = $stickyblock->pagetype;
2026 }
2027 $newblock->defaultregion = $newregions[$stickyblock->position];
2028 $newblock->defaultweight = $stickyblock->weight;
2029 $newblock->configdata = $stickyblock->configdata;
2030 $DB->insert_record('block_instances', $newblock);
2031 }
2032
2033 /// Main savepoint reached
c045e45a 2034 upgrade_main_savepoint($result, 2009050616);
66b10689 2035 }
2036
c045e45a 2037 if ($result && $oldversion < 2009050617) {
66b10689 2038
2039 /// Define table block_positions to be created
2040 $table = new xmldb_table('block_positions');
2041
2042 /// Adding fields to table block_positions
2b212777 2043 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2044 $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2045 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2046 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
2047 $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
2048 $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1');
2049 $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
2050 $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
66b10689 2051
2052 /// Adding keys to table block_positions
2053 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2054 $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
2055 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2056
2057 /// Adding indexes to table block_positions
2058 $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
2059
2060 /// Conditionally launch create table for block_positions
2061 if (!$dbman->table_exists($table)) {
2062 $dbman->create_table($table);
2063 }
2064
2065 /// Main savepoint reached
c045e45a 2066 upgrade_main_savepoint($result, 2009050617);
66b10689 2067 }
2068
c045e45a 2069 if ($result && $oldversion < 2009050618) {
66b10689 2070 /// And block instances with visible = 0, copy that information to block_positions
2071 $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
2072 SELECT id, contextid,
2073 CASE WHEN pagetypepattern = 'course-view-*' THEN
2074 (SELECT " . $DB->sql_concat("'course-view-'", 'format') . "
2075 FROM {course}
2076 JOIN {context} ON {course}.id = {context}.instanceid
2077 WHERE {context}.id = contextid)
2078 ELSE pagetypepattern END,
2079 CASE WHEN subpagepattern IS NULL THEN ''
2080 ELSE subpagepattern END,
2081 0, defaultregion, defaultweight
2082 FROM {block_instances} WHERE visible = 0 AND pagetypepattern <> 'admin-*'");
2083
2084 /// Main savepoint reached
c045e45a 2085 upgrade_main_savepoint($result, 2009050618);
66b10689 2086 }
2087
c045e45a 2088 if ($result && $oldversion < 2009050619) {
66b10689 2089 $table = new xmldb_table('block_instances');
2090
2091 /// Define field blockid to be dropped from block_instances
2092 $field = new xmldb_field('blockid');
2093 if ($dbman->field_exists($table, $field)) {
4276a19a 2094 /// Before dropping the field, drop dependent indexes
2095 $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
2096 if ($dbman->index_exists($table, $index)) {
2097 /// Launch drop index blockid
2098 $dbman->drop_index($table, $index);
2099 }
66b10689 2100 $dbman->drop_field($table, $field);
2101 }
2102
2103 /// Define field pageid to be dropped from block_instances
2104 $field = new xmldb_field('pageid');
2105 if ($dbman->field_exists($table, $field)) {
4276a19a 2106 /// Before dropping the field, drop dependent indexes
2107 $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
2108 if ($dbman->index_exists($table, $index)) {
2109 /// Launch drop index pageid
2110 $dbman->drop_index($table, $index);
2111 }
66b10689 2112 $dbman->drop_field($table, $field);
2113 }
2114
2115 /// Define field visible to be dropped from block_instances
2116 $field = new xmldb_field('visible');
2117 if ($dbman->field_exists($table, $field)) {
2118 $dbman->drop_field($table, $field);
2119 }
2120
2121 /// Main savepoint reached
c045e45a 2122 upgrade_main_savepoint($result, 2009050619);
35079f53 2123 }
2124
ecdf5d17 2125 if ($result && $oldversion < 2009051200) {
2126 /// Let's check the status of mandatory mnet_host records, fixing them
2127 /// and moving "orphan" users to default localhost record. MDL-16879
2128 notify('Fixing mnet records, this may take a while...', 'notifysuccess');
2129 upgrade_fix_incorrect_mnethostids();
2130
2131 /// Main savepoint reached
2132 upgrade_main_savepoint($result, 2009051200);
2133 }
2134
a5747cf8 2135
2136 if ($result && $oldversion < 2009051700) {
2137 /// migrate editor settings
2138 if (empty($CFG->htmleditor)) {
2139 set_config('texteditors', 'textarea');
2140 } else {
2141 set_config('texteditors', 'tinymce,textarea');
2142 }
2143
2144 unset_config('htmleditor');
2145 unset_config('defaulthtmleditor');
2146
2147 /// Main savepoint reached
2148 upgrade_main_savepoint($result, 2009051700);
2149 }
2150
1aa01caf 2151 if ($result && $oldversion < 2009060200) {
2152 /// Define table files_cleanup to be dropped - not needed
2153 $table = new xmldb_table('files_cleanup');
2154
2155 /// Conditionally launch drop table for files_cleanup
2156 if ($dbman->table_exists($table)) {
2157 $dbman->drop_table($table);
2158 }
2159
2160 /// Main savepoint reached
2161 upgrade_main_savepoint($result, 2009060200);
2162 }
2163
b4154c2d 2164 if ($result && $oldversion < 2009061300) {
2165 //TODO: copy this to the very beginning of this upgrade script so that we may log upgrade queries
2166
2167 /// Define table log_queries to be created
2168 $table = new xmldb_table('log_queries');
2169
2170 /// Adding fields to table log_queries
2171 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2172 $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2173 $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
2174 $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
2175 $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2176 $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2177 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2178 $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
2179 $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2180
2181 /// Adding keys to table log_queries
2182 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2183
2184 /// Conditionally launch create table for log_queries
2185 if (!$dbman->table_exists($table)) {
2186 $dbman->create_table($table);
2187 }
2188
2189 /// Main savepoint reached
2190 upgrade_main_savepoint($result, 2009061300);
2191 }
2192
52902cea 2193 /// Repeat 2009050607 upgrade step, which Petr commented out becuase of XMLDB
2194 /// stupidity, so lots of peopel will have missed.
2195 if ($result && $oldversion < 2009061600) {
2196 /// Changing precision of field defaultregion on table block_instances to (16)
2197 $table = new xmldb_table('block_instances');
2198 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
2199
2200 /// Launch change of precision for field defaultregion
2201 $dbman->change_field_precision($table, $field);
2202
2203 /// Main savepoint reached
2204 upgrade_main_savepoint($result, 2009061600);
2205 }
2206
17da2e6f 2207 if ($result && $oldversion < 2009061702) {
2208 // standardizing plugin names
2209 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
2210 foreach ($configs as $config) {
2211 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
2212 $DB->update_record('config_plugins', $config);
2213 }
2214 }
2215 unset($configs);
2216 upgrade_main_savepoint($result, 2009061702);
2217 }
2218
2219 if ($result && $oldversion < 2009061703) {
2220 // standardizing plugin names
2221 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
2222 foreach ($configs as $config) {
2223 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
2224 $DB->update_record('config_plugins', $config);
2225 }
2226 }
2227 unset($configs);
2228 upgrade_main_savepoint($result, 2009061703);
2229 }
2230
2231 if ($result && $oldversion < 2009061704) {
edcb15b5 2232 // change component string in capability records to new "_" format
17da2e6f 2233 if ($caps = $DB->get_records('capabilities')) {
2234 foreach ($caps as $cap) {
2235 $cap->component = str_replace('/', '_', $cap->component);
2236 $DB->update_record('capabilities', $cap);
2237 }
2238 }
2239 unset($caps);
2240 upgrade_main_savepoint($result, 2009061704);
2241 }
2242
2243 if ($result && $oldversion < 2009061705) {
edcb15b5 2244 // change component string in events_handlers records to new "_" format
17da2e6f 2245 if ($handlers = $DB->get_records('events_handlers')) {
2246 foreach ($handlers as $handler) {
2247 $handler->handlermodule = str_replace('/', '_', $handler->handlermodule);
2248 $DB->update_record('events_handlers', $handler);
2249 }
2250 }
2251 unset($handlers);
2252 upgrade_main_savepoint($result, 2009061705);
2253 }
2254
2255 if ($result && $oldversion < 2009061706) {
edcb15b5 2256 // change component string in message_providers records to new "_" format
17da2e6f 2257 if ($mps = $DB->get_records('message_providers')) {
2258 foreach ($mps as $mp) {
2259 $mp->component = str_replace('/', '_', $mp->component);
2260 $DB->update_record('message_providers', $cap);
2261 }
2262 }
2263 unset($caps);
2264 upgrade_main_savepoint($result, 2009061706);
2265 }
edcb15b5 2266
adf8f3f9 2267 if ($result && $oldversion < 2009063000) {
2268 // upgrade format of _with_advanced settings - quiz only
2269 // note: this can be removed later, not needed for upgrades from 1.9.x
c1f58efe 2270 if ($quiz = get_config('quiz')) {
2271 foreach ($quiz as $name=>$value) {
2272 if (strpos($name, 'fix_') !== 0) {
2273 continue;
2274 }
2275 $newname = substr($name,4).'_adv';
2276 set_config($newname, $value, 'quiz');
2277 unset_config($name, 'quiz');
adf8f3f9 2278 }
edcb15b5 2279 }
adf8f3f9 2280 upgrade_main_savepoint($result, 2009063000);
2281 }
edcb15b5 2282
856b6fe6 2283 if ($result && $oldversion < 2009070100) {
2284 // MDL-19677 Change $CFG->bloglevel to BLOG_SITE_LEVEL if BLOG_COURSE_LEVEL or BLOG_GROUP_LEVEL
2285 $current_bloglevel = get_config(null, 'bloglevel');
2286
2287 if ($current_bloglevel == BLOG_GROUP_LEVEL || $current_bloglevel == BLOG_COURSE_LEVEL) {
2288 set_config('bloglevel', BLOG_SITE_LEVEL);
2289 }
2290 }
adf8f3f9 2291
ac9b0805 2292 return $result;
4e423cbf 2293}
271e6dec 2294
8b6a0968 2295//TODO: before 2.0 release
2296// 1/ remove the automatic enabling of completion lib if debug enabled