Updated the HEAD build version to 20100331
[moodle.git] / lib / db / upgrade.php
CommitLineData
117bd748 1<?PHP
4e423cbf 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) {
78946b9b 23 global $CFG, $USER, $DB, $OUTPUT;
4e423cbf 24
4f12838e 25 require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
13a0d3d3 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);
aa9a6867 116 echo $OUTPUT->notification('Guest role removed from "Default role for all users" setting, please select another role.', 'notifysuccess');
ab37dc60 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) {
aa9a6867 128 echo $OUTPUT->notification('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
da750a51 1080 /// Before changing the field, drop dependent indexes
1081 /// Define index shortname (not unique) to be dropped form course_request
1082 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
1083 /// Conditionally launch drop index shortname
1084 if ($dbman->index_exists($table, $index)) {
1085 $dbman->drop_index($table, $index);
1086 }
1087
82bd6a5e 1088 /// Launch change of precision for field shortname
1089 $dbman->change_field_precision($table, $field);
1090
da750a51 1091 /// After changing the field, recreate dependent indexes
1092 /// Define index shortname (not unique) to be added to course_request
1093 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
1094 /// Conditionally launch add index shortname
1095 if (!$dbman->index_exists($table, $index)) {
1096 $dbman->add_index($table, $index);
1097 }
1098
82bd6a5e 1099 /// Main savepoint reached
1100 upgrade_main_savepoint($result, 2008120700);
1101 }
1102
1103 /// For MDL-17501. Ensure that any role that has moodle/course:update also
1104 /// has moodle/course:visibility.
1105 if ($result && $oldversion < 2008120800) {
1106 /// Get the roles with 'moodle/course:update'.
1107 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
1108 $roles = get_roles_with_capability('moodle/course:update', CAP_ALLOW, $systemcontext);
1109
1110 /// Give those roles 'moodle/course:visibility'.
1111 foreach ($roles as $role) {
1112 assign_capability('moodle/course:visibility', CAP_ALLOW, $role->id, $systemcontext->id);
1113 }
1114
1115 /// Force all sessions to refresh access data.
1116 mark_context_dirty($systemcontext->path);
1117
1118 /// Main savepoint reached
1119 upgrade_main_savepoint($result, 2008120800);
1120 }
1121
1122 if ($result && $oldversion < 2008120801) {
1123
1124 /// Changing precision of field shortname on table mnet_enrol_course to (100)
1125 $table = new xmldb_table('mnet_enrol_course');
2a88f626 1126 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
82bd6a5e 1127
1128 /// Launch change of precision for field shortname
1129 $dbman->change_field_precision($table, $field);
1130
1131 /// Main savepoint reached
1132 upgrade_main_savepoint($result, 2008120801);
1133 }
1134
1135 if ($result && $oldversion < 2008121701) {
0953a4e7 1136
1137 /// Define field availablefrom to be added to course_modules
1138 $table = new xmldb_table('course_modules');
2a88f626 1139 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
0953a4e7 1140
1141 /// Conditionally launch add field availablefrom
1142 if (!$dbman->field_exists($table, $field)) {
1143 $dbman->add_field($table, $field);
1144 }
1145
1146 /// Define field availableuntil to be added to course_modules
2a88f626 1147 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
0953a4e7 1148
1149 /// Conditionally launch add field availableuntil
1150 if (!$dbman->field_exists($table, $field)) {
1151 $dbman->add_field($table, $field);
1152 }
82bd6a5e 1153
0953a4e7 1154 /// Define field showavailability to be added to course_modules
2a88f626 1155 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
0953a4e7 1156
1157 /// Conditionally launch add field showavailability
1158 if (!$dbman->field_exists($table, $field)) {
1159 $dbman->add_field($table, $field);
1160 }
82bd6a5e 1161
0953a4e7 1162 /// Define table course_modules_availability to be created
1163 $table = new xmldb_table('course_modules_availability');
1164
1165 /// Adding fields to table course_modules_availability
2a88f626 1166 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1167 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1168 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1169 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
1170 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1171 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1172 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
0953a4e7 1173
1174 /// Adding keys to table course_modules_availability
1175 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
cbb17d54 1176 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
1177 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
1178 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
0953a4e7 1179
1180 /// Conditionally launch create table for course_modules_availability
1181 if (!$dbman->table_exists($table)) {
1182 $dbman->create_table($table);
1183 }
1184
82bd6a5e 1185 /// Changes to modinfo mean we need to rebuild course cache
a9a0b93d 1186 require_once($CFG->dirroot . '/course/lib.php');
8b6a0968 1187 rebuild_course_cache(0, true);
9136a60c 1188
82bd6a5e 1189 /// For developer upgrades, turn on the conditional activities and completion
1190 /// features automatically (to gain more testing)
8b6a0968 1191//TODO: remove before 2.0 final!
1192 if (debugging('', DEBUG_DEVELOPER)) {
1193 set_config('enableavailability', 1);
1194 set_config('enablecompletion', 1);
9136a60c 1195 }
1196
9d510a2e 1197 /// Main savepoint reached
82bd6a5e 1198 upgrade_main_savepoint($result, 2008121701);
9d510a2e 1199 }
1200
656be893 1201 if ($result && $oldversion < 2009010500) {
1202 /// clean up config table a bit
1203 unset_config('session_error_counter');
1204
1205 /// Main savepoint reached
1206 upgrade_main_savepoint($result, 2009010500);
1207 }
1208
1209 if ($result && $oldversion < 2009010600) {
1210
1211 /// Define field originalquestion to be dropped from question_states
1212 $table = new xmldb_table('question_states');
1213 $field = new xmldb_field('originalquestion');
1214
1215 /// Conditionally launch drop field originalquestion
1216 if ($dbman->field_exists($table, $field)) {
1217 $dbman->drop_field($table, $field);
1218 }
1219
1220 /// Main savepoint reached
1221 upgrade_main_savepoint($result, 2009010600);
1222 }
1223
1224 if ($result && $oldversion < 2009010601) {
1225
1226 /// Changing precision of field ip on table log to (45)
1227 $table = new xmldb_table('log');
2a88f626 1228 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
656be893 1229
1230 /// Launch change of precision for field ip
1231 $dbman->change_field_precision($table, $field);
1232
1233 /// Main savepoint reached
1234 upgrade_main_savepoint($result, 2009010601);
1235 }
1236
1237 if ($result && $oldversion < 2009010602) {
1238
1239 /// Changing precision of field lastip on table user to (45)
1240 $table = new xmldb_table('user');
2a88f626 1241 $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
656be893 1242
1243 /// Launch change of precision for field lastip
1244 $dbman->change_field_precision($table, $field);
1245
1246 /// Main savepoint reached
1247 upgrade_main_savepoint($result, 2009010602);
1248 }
1249
1250 if ($result && $oldversion < 2009010603) {
1251
1252 /// Changing precision of field ip_address on table mnet_host to (45)
1253 $table = new xmldb_table('mnet_host');
2a88f626 1254 $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
656be893 1255
1256 /// Launch change of precision for field ip_address
1257 $dbman->change_field_precision($table, $field);
1258
1259 /// Main savepoint reached
1260 upgrade_main_savepoint($result, 2009010603);
1261 }
1262
1263 if ($result && $oldversion < 2009010604) {
1264
1265 /// Changing precision of field ip on table mnet_log to (45)
1266 $table = new xmldb_table('mnet_log');
2a88f626 1267 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
656be893 1268
1269 /// Launch change of precision for field ip
1270 $dbman->change_field_precision($table, $field);
1271
1272 /// Main savepoint reached
1273 upgrade_main_savepoint($result, 2009010604);
1274 }
1275
4413941f 1276 if ($result && $oldversion < 2009010800) {
1277 /// Update the notifyloginfailures setting.
1278 if ($CFG->notifyloginfailures == 'mainadmin') {
1279 set_config('notifyloginfailures', get_admin()->username);
1280 } else if ($CFG->notifyloginfailures == 'alladmins') {
1281 set_config('notifyloginfailures', '$@ALL@$');
1282 } else {
1283 set_config('notifyloginfailures', '');
1284 }
1285
1286 /// Main savepoint reached
1287 upgrade_main_savepoint($result, 2009010800);
1288 }
1289
ab2eb65c 1290 if ($result && $oldversion < 2009011000) {
1291
1292 /// Changing nullability of field configdata on table block_instance to null
1293 $table = new xmldb_table('block_instance');
1294 $field = new xmldb_field('configdata');
2a88f626 1295 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
ab2eb65c 1296
1297 /// Launch change of nullability for field configdata
1298 $dbman->change_field_notnull($table, $field);
1299
1300 /// Main savepoint reached
1301 upgrade_main_savepoint($result, 2009011000);
1302 }
1303
1caea91e 1304 if ($result && $oldversion < 2009011100) {
1305 /// Remove unused settings
1306 unset_config('zip');
1307 unset_config('unzip');
1308 unset_config('adminblocks_initialised');
1309
1310 /// Main savepoint reached
1311 upgrade_main_savepoint($result, 2009011100);
1312 }
1313
eb6a973c 1314 if ($result && $oldversion < 2009011101) {
1315 /// Migrate backup settings to core plugin config table
1316 $configs = $DB->get_records('backup_config');
1317 foreach ($configs as $config) {
1318 set_config($config->name, $config->value, 'backup');
1319 }
1320
1321 /// Define table to be dropped
1322 $table = new xmldb_table('backup_config');
1323
1324 /// Launch drop table for old backup config
1325 $dbman->drop_table($table);
1326
1327 /// Main savepoint reached
1328 upgrade_main_savepoint($result, 2009011101);
1329 }
1330
301bf0b2 1331 if ($result && $oldversion < 2009011303) {
1332
1333 /// Define table config_log to be created
1334 $table = new xmldb_table('config_log');
1335
1336 /// Adding fields to table config_log
2a88f626 1337 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1338 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1339 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1340 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1341 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1342 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1343 $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
301bf0b2 1344
1345 /// Adding keys to table config_log
1346 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1347 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1348
1349 /// Adding indexes to table config_log
1350 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1351
1352 /// Launch create table for config_log
1353 $dbman->create_table($table);
1354
1355 /// Main savepoint reached
1356 upgrade_main_savepoint($result, 2009011303);
1357 }
1358
b9fb7103 1359 if ($result && $oldversion < 2009011900) {
3f57bd45 1360
1361 /// Define table sessions2 to be dropped
1362 $table = new xmldb_table('sessions2');
1363
1364 /// Conditionally launch drop table for sessions
1365 if ($dbman->table_exists($table)) {
1366 $dbman->drop_table($table);
1367 }
1368
1369 /// Define table sessions to be dropped
1370 $table = new xmldb_table('sessions');
1371
1372 /// Conditionally launch drop table for sessions
1373 if ($dbman->table_exists($table)) {
1374 $dbman->drop_table($table);
1375 }
1376
1377 /// Define table sessions to be created
1378 $table = new xmldb_table('sessions');
1379
1380 /// Adding fields to table sessions
2a88f626 1381 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1382 $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1383 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
1384 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1385 $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
1386 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1387 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1388 $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1389 $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
3f57bd45 1390
1391 /// Adding keys to table sessions
1392 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1393 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1394
1395 /// Adding indexes to table sessions
1396 $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1397 $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1398 $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1399 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1400
1401 /// Launch create table for sessions
1402 $dbman->create_table($table);
1403
1404 /// Main savepoint reached
b9fb7103 1405 upgrade_main_savepoint($result, 2009011900);
3f57bd45 1406 }
1407
dc6f76b9 1408 if ($result && $oldversion < 2009012901) {
795a08ad 1409 // NOTE: this table may already exist, see beginning of this file ;-)
1f20942c 1410
1411 /// Define table upgrade_log to be created
1412 $table = new xmldb_table('upgrade_log');
1413
1414 /// Adding fields to table upgrade_log
2a88f626 1415 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1416 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1417 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1418 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1419 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1420 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1421 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1422 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1423 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1f20942c 1424
1425 /// Adding keys to table upgrade_log
1426 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1427 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1428
1429 /// Adding indexes to table upgrade_log
1430 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1431 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
1432
1433 /// Conditionally launch create table for upgrade_log
1434 if (!$dbman->table_exists($table)) {
1435 $dbman->create_table($table);
1436 }
1437
1438 /// Main savepoint reached
dc6f76b9 1439 upgrade_main_savepoint($result, 2009012901);
1f20942c 1440 }
1caea91e 1441
75564228 1442 if ($result && $oldversion < 2009021800) {
eb2761f8 1443 // Converting format of grade conditions, if any exist, to percentages.
75564228 1444 $DB->execute("
1445UPDATE {course_modules_availability} SET grademin=(
1446 SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
eb2761f8 1447 /(gi.grademax-gi.grademin)
1448 FROM {grade_items} gi
75564228 1449 WHERE gi.id={course_modules_availability}.gradeitemid)
1450WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1451 $DB->execute("
1452UPDATE {course_modules_availability} SET grademax=(
1453 SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
eb2761f8 1454 /(gi.grademax-gi.grademin)
1455 FROM {grade_items} gi
75564228 1456 WHERE gi.id={course_modules_availability}.gradeitemid)
1457WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1458
1459 /// Main savepoint reached
1460 upgrade_main_savepoint($result, 2009021800);
1461 }
bc7ec91a 1462 if ($result && $oldversion < 2009021801) {
1463 /// Define field backuptype to be added to backup_log
382123a0 1464 $table = new xmldb_table('backup_log');
2a88f626 1465 $field = new xmldb_field('backuptype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'info');
bc7ec91a 1466 /// Conditionally Launch add field backuptype and set all old records as 'scheduledbackup' records.
1467 if (!$dbman->field_exists($table, $field)) {
1468 $dbman->add_field($table, $field);
1469 $DB->execute("UPDATE {backup_log} SET backuptype='scheduledbackup'");
1470 }
75564228 1471
bc7ec91a 1472 /// Main savepoint reached
1473 upgrade_main_savepoint($result, 2009021801);
1474 }
af52ecee 1475 /// Add default sort order for question types.
1476 if ($result && $oldversion < 2009030300) {
1477 set_config('multichoice_sortorder', 1, 'question');
1478 set_config('truefalse_sortorder', 2, 'question');
1479 set_config('shortanswer_sortorder', 3, 'question');
1480 set_config('numerical_sortorder', 4, 'question');
1481 set_config('calculated_sortorder', 5, 'question');
1482 set_config('essay_sortorder', 6, 'question');
1483 set_config('match_sortorder', 7, 'question');
1484 set_config('randomsamatch_sortorder', 8, 'question');
1485 set_config('multianswer_sortorder', 9, 'question');
1486 set_config('description_sortorder', 10, 'question');
1487 set_config('random_sortorder', 11, 'question');
1488 set_config('missingtype_sortorder', 12, 'question');
1489
1490 upgrade_main_savepoint($result, 2009030300);
1491 }
a75c78d3 1492 if ($result && $oldversion < 2009030501) {
1493 /// setup default repository plugins
1494 require_once($CFG->dirroot . '/repository/lib.php');
1495 repository_setup_default_plugins();
1496 /// Main savepoint reached
1497 upgrade_main_savepoint($result, 2009030501);
1498 }
af52ecee 1499
82701e24 1500 /// MDL-18132 replace the use a new Role allow switch settings page, instead of
1501 /// $CFG->allowuserswitchrolestheycantassign
1502 if ($result && $oldversion < 2009032000) {
1503 /// First create the new table.
1504 $table = new xmldb_table('role_allow_switch');
1505
1506 /// Adding fields to table role_allow_switch
2a88f626 1507 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1508 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1509 $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
82701e24 1510
1511 /// Adding keys to table role_allow_switch
1512 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1513 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
1514 $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
1515
1516 /// Adding indexes to table role_allow_switch
1517 $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1518
1519 /// Conditionally launch create table for role_allow_switch
1520 if (!$dbman->table_exists($table)) {
1521 $dbman->create_table($table);
1522 }
1523
1524 /// Main savepoint reached
1525 upgrade_main_savepoint($result, 2009032000);
1526 }
0fc9c009 1527
82701e24 1528 if ($result && $oldversion < 2009032001) {
1529 /// Copy from role_allow_assign into the new table.
dc740f97 1530 $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1531 SELECT roleid, allowassign FROM {role_allow_assign}');
82701e24 1532
1533 /// Unset the config variable used in 1.9.
1534 unset_config('allowuserswitchrolestheycantassign');
1535
1536 /// Main savepoint reached
1537 upgrade_main_savepoint($result, 2009032001);
1538 }
c94985ef 1539
1540 if ($result && $oldversion < 2009033100) {
1541 require_once("$CFG->dirroot/filter/tex/lib.php");
1542 filter_tex_updatedcallback(null);
1543 /// Main savepoint reached
1544 upgrade_main_savepoint($result, 2009033100);
1545 }
1546
0fc9c009 1547 if ($result && $oldversion < 2009040300) {
1548
1549 /// Define table filter_active to be created
1550 $table = new xmldb_table('filter_active');
1551
1552 /// Adding fields to table filter_active
2a88f626 1553 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1554 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1555 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1556 $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1557 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
0fc9c009 1558
1559 /// Adding keys to table filter_active
1560 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1561 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1562
1563 /// Adding indexes to table filter_active
1564 $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1565
1566 /// Conditionally launch create table for filter_active
1567 if (!$dbman->table_exists($table)) {
1568 $dbman->create_table($table);
1569 }
1570
1571 /// Main savepoint reached
1572 upgrade_main_savepoint($result, 2009040300);
1573 }
1574
1575 if ($result && $oldversion < 2009040301) {
1576
1577 /// Define table filter_config to be created
1578 $table = new xmldb_table('filter_config');
1579
1580 /// Adding fields to table filter_config
2a88f626 1581 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1582 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1583 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1584 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1585 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
0fc9c009 1586
1587 /// Adding keys to table filter_config
1588 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1589 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1590
1591 /// Adding indexes to table filter_config
1592 $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1593
1594 /// Conditionally launch create table for filter_config
1595 if (!$dbman->table_exists($table)) {
1596 $dbman->create_table($table);
1597 }
1598
1599 /// Main savepoint reached
1600 upgrade_main_savepoint($result, 2009040301);
1601 }
1602
c07e6d8d 1603 if ($result && $oldversion < 2009040302) {
1604 /// Transfer current settings from $CFG->textfilters
1605 $disabledfilters = filter_get_all_installed();
1606 if (empty($CFG->textfilters)) {
1607 $activefilters = array();
1608 } else {
1609 $activefilters = explode(',', $CFG->textfilters);
1610 }
1611 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1612 $sortorder = 1;
1613 foreach ($activefilters as $filter) {
1614 filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1615 $sortorder += 1;
1616 unset($disabledfilters[$filter]);
1617 }
1618 foreach ($disabledfilters as $filter => $notused) {
1619 filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1620 $sortorder += 1;
1621 }
1622
1623 /// Main savepoint reached
1624 upgrade_main_savepoint($result, 2009040302);
1625 }
1626
bbb45905 1627 if ($result && $oldversion < 2009040600) {
1628 /// Ensure that $CFG->stringfilters is set.
1629 if (empty($CFG->stringfilters)) {
1630 if (!empty($CFG->filterall)) {
1631 set_config('stringfilters', $CFG->textfilters);
1632 } else {
1633 set_config('stringfilters', '');
1634 }
1635 }
1636
1637 set_config('filterall', !empty($CFG->stringfilters));
1638 unset_config('textfilters');
1639
1640 /// Main savepoint reached
1641 upgrade_main_savepoint($result, 2009040600);
1642 }
1643
e7c6bf0e 1644 if ($result && $oldversion < 2009041700) {
1645 /// To ensure the UI remains consistent with no behaviour change, any
1646 /// 'until' date in an activity condition should have 1 second subtracted
1647 /// (to go from 0:00 on the following day to 23:59 on the previous one).
1648 $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
6861b6fe 1649 require_once($CFG->dirroot . '/course/lib.php');
0f9534c1 1650 rebuild_course_cache(0, true);
e7c6bf0e 1651
1652 /// Main savepoint reached
1653 upgrade_main_savepoint($result, 2009041700);
1654 }
1655
6bdf4c99 1656 if ($result && $oldversion < 2009042600) {
1657 /// Deleting orphaned messages from deleted users.
1658 require_once($CFG->dirroot.'/message/lib.php');
1659 /// Detect deleted users with messages sent(useridfrom) and not read
1660 if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
1661 FROM {user} u
1662 JOIN {message} m ON m.useridfrom = u.id
1663 WHERE u.deleted = ?', array(1))) {
1664 foreach ($deletedusers as $deleteduser) {
1665 message_move_userfrom_unread2read($deleteduser->id); // move messages
1666 }
1667 }
1668 /// Main savepoint reached
1669 upgrade_main_savepoint($result, 2009042600);
1670 }
1671
e37cd84a 1672 /// Dropping all enums/check contraints from core. MDL-18577
1673 if ($result && $oldversion < 2009042700) {
1674
1675 /// Changing list of values (enum) of field stattype on table stats_daily to none
1676 $table = new xmldb_table('stats_daily');
2a88f626 1677 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
e37cd84a 1678
1679 /// Launch change of list of values for field stattype
2a88f626 1680 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1681
1682 /// Changing list of values (enum) of field stattype on table stats_weekly to none
1683 $table = new xmldb_table('stats_weekly');
2a88f626 1684 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
e37cd84a 1685
1686 /// Launch change of list of values for field stattype
2a88f626 1687 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1688
1689 /// Changing list of values (enum) of field stattype on table stats_monthly to none
1690 $table = new xmldb_table('stats_monthly');
2a88f626 1691 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
e37cd84a 1692
1693 /// Launch change of list of values for field stattype
2a88f626 1694 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1695
1696 /// Changing list of values (enum) of field publishstate on table post to none
1697 $table = new xmldb_table('post');
2a88f626 1698 $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
e37cd84a 1699
1700 /// Launch change of list of values for field publishstate
2a88f626 1701 $dbman->drop_enum_from_field($table, $field);
e37cd84a 1702
1703 /// Main savepoint reached
1704 upgrade_main_savepoint($result, 2009042700);
1705 }
1706
c045e45a 1707 if ($result && $oldversion < 2009043000) {
1708 unset_config('grade_report_showgroups');
1709 upgrade_main_savepoint($result, 2009043000);
1710 }
1711
1712 if ($result && $oldversion < 2009050600) {
e88462a0 1713 /// Site front page blocks need to be moved due to page name change.
1714 $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
1715
1716 /// Main savepoint reached
c045e45a 1717 upgrade_main_savepoint($result, 2009050600);
e88462a0 1718 }
1719
c045e45a 1720 if ($result && $oldversion < 2009050601) {
66b10689 1721
3aceb097 1722 /// Define table block_instance to be renamed to block_instances
66b10689 1723 $table = new xmldb_table('block_instance');
1724
1725 /// Launch rename table for block_instance
3aceb097 1726 $dbman->rename_table($table, 'block_instances');
66b10689 1727
1728 /// Main savepoint reached
c045e45a 1729 upgrade_main_savepoint($result, 2009050601);
66b10689 1730 }
1731
c045e45a 1732 if ($result && $oldversion < 2009050602) {
66b10689 1733
1734 /// Define table block_instance to be renamed to block_instance_old
1735 $table = new xmldb_table('block_pinned');
1736
1737 /// Launch rename table for block_instance
1738 $dbman->rename_table($table, 'block_pinned_old');
1739
1740 /// Main savepoint reached
c045e45a 1741 upgrade_main_savepoint($result, 2009050602);
66b10689 1742 }
1743
c045e45a 1744 if ($result && $oldversion < 2009050603) {
66b10689 1745
1746 /// Define table block_instance_old to be created
1747 $table = new xmldb_table('block_instance_old');
1748
1749 /// Adding fields to table block_instance_old
2b212777 1750 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1751 $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1752 $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1753 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1754 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
1755 $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
1756 $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
1757 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1758 $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
66b10689 1759
1760 /// Adding keys to table block_instance_old
1761 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1762 $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
1763
1764 /// Adding indexes to table block_instance_old
1765 $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1766 $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
1767
1768 /// Conditionally launch create table for block_instance_old
1769 if (!$dbman->table_exists($table)) {
1770 $dbman->create_table($table);
1771 }
1772
1773 /// Main savepoint reached
c045e45a 1774 upgrade_main_savepoint($result, 2009050603);
66b10689 1775 }
1776
c045e45a 1777 if ($result && $oldversion < 2009050604) {
66b10689 1778 /// Copy current blocks data from block_instances to block_instance_old
35550fd6 1779 $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
1780 SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
66b10689 1781
c045e45a 1782 upgrade_main_savepoint($result, 2009050604);
66b10689 1783 }
1784
c045e45a 1785 if ($result && $oldversion < 2009050605) {
66b10689 1786
1787 /// Define field multiple to be dropped from block
1788 $table = new xmldb_table('block');
1789 $field = new xmldb_field('multiple');
1790
1791 /// Conditionally launch drop field multiple
1792 if ($dbman->field_exists($table, $field)) {
1793 $dbman->drop_field($table, $field);
1794 }
1795
1796 /// Main savepoint reached
c045e45a 1797 upgrade_main_savepoint($result, 2009050605);
66b10689 1798 }
1799
c045e45a 1800 if ($result && $oldversion < 2009050606) {
66b10689 1801 $table = new xmldb_table('block_instances');
1802
1803 /// Rename field weight on table block_instances to defaultweight
2b212777 1804 $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'position');
66b10689 1805 $dbman->rename_field($table, $field, 'defaultweight');
1806
1807 /// Rename field position on table block_instances to defaultregion
2b212777 1808 $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
66b10689 1809 $dbman->rename_field($table, $field, 'defaultregion');
1810
1811 /// Main savepoint reached
c045e45a 1812 upgrade_main_savepoint($result, 2009050606);
66b10689 1813 }
1814
c045e45a 1815 if ($result && $oldversion < 2009050607) {
66b10689 1816 /// Changing precision of field defaultregion on table block_instances to (16)
a26146d5 1817 $table = new xmldb_table('block_instances');
1818 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
66b10689 1819
1820 /// Launch change of precision for field defaultregion
1821 $dbman->change_field_precision($table, $field);
1822
1823 /// Main savepoint reached
a26146d5 1824 upgrade_main_savepoint($result, 2009050607);
66b10689 1825 }
1826
c045e45a 1827 if ($result && $oldversion < 2009050608) {
66b10689 1828 /// Change regions to the new notation
1829 $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1830 $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1831 $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1832 // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1833 // flex page course format. Hopefully this new value is an adequate alternative.
1834
1835 /// Main savepoint reached
c045e45a 1836 upgrade_main_savepoint($result, 2009050608);
66b10689 1837 }
1838
c045e45a 1839 if ($result && $oldversion < 2009050609) {
66b10689 1840
1841 /// Define key blockname (unique) to be added to block
1842 $table = new xmldb_table('block');
1843 $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1844
1845 /// Launch add key blockname
1846 $dbman->add_key($table, $key);
1847
1848 /// Main savepoint reached
c045e45a 1849 upgrade_main_savepoint($result, 2009050609);
66b10689 1850 }
1851
c045e45a 1852 if ($result && $oldversion < 2009050610) {
66b10689 1853 $table = new xmldb_table('block_instances');
1854
1855 /// Define field blockname to be added to block_instances
2b212777 1856 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
66b10689 1857 if (!$dbman->field_exists($table, $field)) {
1858 $dbman->add_field($table, $field);
1859 }
1860
1861 /// Define field contextid to be added to block_instances
2b212777 1862 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
66b10689 1863 if (!$dbman->field_exists($table, $field)) {
1864 $dbman->add_field($table, $field);
1865 }
1866
1867 /// Define field showinsubcontexts to be added to block_instances
2b212777 1868 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
66b10689 1869 if (!$dbman->field_exists($table, $field)) {
1870 $dbman->add_field($table, $field);
1871 }
1872
1873 /// Define field subpagepattern to be added to block_instances
2b212777 1874 $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
66b10689 1875 if (!$dbman->field_exists($table, $field)) {
1876 $dbman->add_field($table, $field);
1877 }
1878
1879 /// Main savepoint reached
c045e45a 1880 upgrade_main_savepoint($result, 2009050610);
66b10689 1881 }
1882
c045e45a 1883 if ($result && $oldversion < 2009050611) {
66b10689 1884 $table = new xmldb_table('block_instances');
1885
1886 /// Fill in blockname from blockid
1887 $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1888
1889 /// Set showinsubcontexts = 0 for all rows.
1890 $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1891
1892 /// Main savepoint reached
c045e45a 1893 upgrade_main_savepoint($result, 2009050611);
66b10689 1894 }
1895
c045e45a 1896 if ($result && $oldversion < 2009050612) {
66b10689 1897
1898 /// Rename field pagetype on table block_instances to pagetypepattern
1899 $table = new xmldb_table('block_instances');
2b212777 1900 $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
66b10689 1901
1902 /// Launch rename field pagetype
1903 $dbman->rename_field($table, $field, 'pagetypepattern');
1904
1905 /// Main savepoint reached
c045e45a 1906 upgrade_main_savepoint($result, 2009050612);
66b10689 1907 }
1908
c045e45a 1909 if ($result && $oldversion < 2009050613) {
66b10689 1910 /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1911
1912 /// site-index
1913 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
eb2761f8 1914 $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1915 pagetypepattern = 'site-index',
1916 subpagepattern = NULL
1917 WHERE pagetypepattern = 'site-index'");
66b10689 1918
1919 /// course-view
eb2761f8 1920 $DB->execute("UPDATE {block_instances} SET
1921 contextid = (
1922 SELECT {context}.id
1923 FROM {context}
1924 JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1925 WHERE {course}.id = pageid
1926 ),
1927 pagetypepattern = 'course-view-*',
1928 subpagepattern = NULL
1929 WHERE pagetypepattern = 'course-view'");
66b10689 1930
1931 /// admin
1932 $syscontext = get_context_instance(CONTEXT_SYSTEM);
eb2761f8 1933 $DB->execute("UPDATE {block_instances} SET
1934 contextid = " . $syscontext->id . ",
1935 pagetypepattern = 'admin-*',
1936 subpagepattern = NULL
1937 WHERE pagetypepattern = 'admin'");
66b10689 1938
1939 /// my-index
eb2761f8 1940 $DB->execute("UPDATE {block_instances} SET
1941 contextid = (
1942 SELECT {context}.id
1943 FROM {context}
1944 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1945 WHERE {user}.id = pageid
1946 ),
1947 pagetypepattern = 'my-index',
1948 subpagepattern = NULL
1949 WHERE pagetypepattern = 'my-index'");
66b10689 1950
1951 /// tag-index
eb2761f8 1952 $DB->execute("UPDATE {block_instances} SET
1953 contextid = " . $syscontext->id . ",
1954 pagetypepattern = 'tag-index',
1955 subpagepattern = pageid
1956 WHERE pagetypepattern = 'tag-index'");
66b10689 1957
1958 /// blog-view
eb2761f8 1959 $DB->execute("UPDATE {block_instances} SET
1960 contextid = (
1961 SELECT {context}.id
1962 FROM {context}
1963 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1964 WHERE {user}.id = pageid
1965 ),
1966 pagetypepattern = 'blog-index',
1967 subpagepattern = NULL
1968 WHERE pagetypepattern = 'blog-view'");
66b10689 1969
1970 /// mod-xxx-view
1971 $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1972 foreach ($moduleswithblocks as $modname) {
1973 if (!$dbman->table_exists($modname)) {
1974 continue;
1975 }
eb2761f8 1976 $DB->execute("UPDATE {block_instances} SET
1977 contextid = (
1978 SELECT {context}.id
1979 FROM {context}
1980 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1981 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1982 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1983 WHERE {{$modname}}.id = pageid
1984 ),
1985 pagetypepattern = 'blog-index',
1986 subpagepattern = NULL
1987 WHERE pagetypepattern = 'blog-view'");
66b10689 1988 }
1989
1990 /// Main savepoint reached
c045e45a 1991 upgrade_main_savepoint($result, 2009050613);
66b10689 1992 }
1993
c045e45a 1994 if ($result && $oldversion < 2009050614) {
66b10689 1995 /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
bf659cf0 1996 $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
66b10689 1997
1998 /// Main savepoint reached
c045e45a 1999 upgrade_main_savepoint($result, 2009050614);
66b10689 2000 }
2001
c045e45a 2002 if ($result && $oldversion < 2009050615) {
66b10689 2003 $table = new xmldb_table('block_instances');
2004
2005 /// Changing nullability of field blockname on table block_instances to not null
2b212777 2006 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
66b10689 2007 $dbman->change_field_notnull($table, $field);
2008
2009 /// Changing nullability of field contextid on table block_instances to not null
2b212777 2010 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
66b10689 2011 $dbman->change_field_notnull($table, $field);
2012
2013 /// Changing nullability of field showinsubcontexts on table block_instances to not null
2b212777 2014 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
66b10689 2015 $dbman->change_field_notnull($table, $field);
2016
2017 /// Main savepoint reached
c045e45a 2018 upgrade_main_savepoint($result, 2009050615);
66b10689 2019 }
2020
c045e45a 2021 if ($result && $oldversion < 2009050616) {
66b10689 2022 /// Add exiting sticky blocks.
2023 $blocks = $DB->get_records('block');
2024 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2025 $newregions = array(
2026 'l' => 'side-pre',
2027 'r' => 'side-post',
2028 'c' => 'course-view-top',
2029 );
2030 $stickyblocks = $DB->get_recordset('block_pinned_old');
2031 foreach ($stickyblocks as $stickyblock) {
93d3a250 2032 $newblock = new object();
35fb85a7 2033 $newblock->blockname = $blocks[$stickyblock->blockid]->name;
66b10689 2034 $newblock->contextid = $syscontext->id;
2035 $newblock->showinsubcontexts = 1;
2036 switch ($stickyblock->pagetype) {
2037 case 'course-view':
2038 $newblock->pagetypepattern = 'course-view-*';
2039 break;
2040 default:
2041 $newblock->pagetypepattern = $stickyblock->pagetype;
2042 }
2043 $newblock->defaultregion = $newregions[$stickyblock->position];
2044 $newblock->defaultweight = $stickyblock->weight;
2045 $newblock->configdata = $stickyblock->configdata;
01de485d 2046 $newblock->visible = 1;
66b10689 2047 $DB->insert_record('block_instances', $newblock);
2048 }
2049
2050 /// Main savepoint reached
c045e45a 2051 upgrade_main_savepoint($result, 2009050616);
66b10689 2052 }
2053
c045e45a 2054 if ($result && $oldversion < 2009050617) {
66b10689 2055
2056 /// Define table block_positions to be created
2057 $table = new xmldb_table('block_positions');
2058
2059 /// Adding fields to table block_positions
2b212777 2060 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2061 $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2062 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2063 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
2064 $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
2065 $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1');
2066 $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
2067 $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
66b10689 2068
2069 /// Adding keys to table block_positions
2070 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2071 $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
2072 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2073
2074 /// Adding indexes to table block_positions
2075 $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
2076
2077 /// Conditionally launch create table for block_positions
2078 if (!$dbman->table_exists($table)) {
2079 $dbman->create_table($table);
2080 }
2081
2082 /// Main savepoint reached
c045e45a 2083 upgrade_main_savepoint($result, 2009050617);
66b10689 2084 }
2085
c045e45a 2086 if ($result && $oldversion < 2009050618) {
66b10689 2087 /// And block instances with visible = 0, copy that information to block_positions
2088 $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
2089 SELECT id, contextid,
2090 CASE WHEN pagetypepattern = 'course-view-*' THEN
2091 (SELECT " . $DB->sql_concat("'course-view-'", 'format') . "
2092 FROM {course}
2093 JOIN {context} ON {course}.id = {context}.instanceid
2094 WHERE {context}.id = contextid)
2095 ELSE pagetypepattern END,
2096 CASE WHEN subpagepattern IS NULL THEN ''
2097 ELSE subpagepattern END,
2098 0, defaultregion, defaultweight
2099 FROM {block_instances} WHERE visible = 0 AND pagetypepattern <> 'admin-*'");
2100
2101 /// Main savepoint reached
c045e45a 2102 upgrade_main_savepoint($result, 2009050618);
66b10689 2103 }
2104
c045e45a 2105 if ($result && $oldversion < 2009050619) {
66b10689 2106 $table = new xmldb_table('block_instances');
2107
2108 /// Define field blockid to be dropped from block_instances
2109 $field = new xmldb_field('blockid');
2110 if ($dbman->field_exists($table, $field)) {
4276a19a 2111 /// Before dropping the field, drop dependent indexes
2112 $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
2113 if ($dbman->index_exists($table, $index)) {
2114 /// Launch drop index blockid
2115 $dbman->drop_index($table, $index);
2116 }
66b10689 2117 $dbman->drop_field($table, $field);
2118 }
2119
2120 /// Define field pageid to be dropped from block_instances
2121 $field = new xmldb_field('pageid');
2122 if ($dbman->field_exists($table, $field)) {
4276a19a 2123 /// Before dropping the field, drop dependent indexes
2124 $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
2125 if ($dbman->index_exists($table, $index)) {
2126 /// Launch drop index pageid
2127 $dbman->drop_index($table, $index);
2128 }
66b10689 2129 $dbman->drop_field($table, $field);
2130 }
2131
2132 /// Define field visible to be dropped from block_instances
2133 $field = new xmldb_field('visible');
2134 if ($dbman->field_exists($table, $field)) {
2135 $dbman->drop_field($table, $field);
2136 }
2137
2138 /// Main savepoint reached
c045e45a 2139 upgrade_main_savepoint($result, 2009050619);
35079f53 2140 }
2141
ecdf5d17 2142 if ($result && $oldversion < 2009051200) {
2143 /// Let's check the status of mandatory mnet_host records, fixing them
2144 /// and moving "orphan" users to default localhost record. MDL-16879
aa9a6867 2145 echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
ecdf5d17 2146 upgrade_fix_incorrect_mnethostids();
2147
2148 /// Main savepoint reached
2149 upgrade_main_savepoint($result, 2009051200);
2150 }
2151
a5747cf8 2152
2153 if ($result && $oldversion < 2009051700) {
2154 /// migrate editor settings
2155 if (empty($CFG->htmleditor)) {
2156 set_config('texteditors', 'textarea');
2157 } else {
2158 set_config('texteditors', 'tinymce,textarea');
2159 }
2160
2161 unset_config('htmleditor');
2162 unset_config('defaulthtmleditor');
2163
2164 /// Main savepoint reached
2165 upgrade_main_savepoint($result, 2009051700);
2166 }
2167
1aa01caf 2168 if ($result && $oldversion < 2009060200) {
2169 /// Define table files_cleanup to be dropped - not needed
2170 $table = new xmldb_table('files_cleanup');
2171
2172 /// Conditionally launch drop table for files_cleanup
2173 if ($dbman->table_exists($table)) {
2174 $dbman->drop_table($table);
2175 }
2176
2177 /// Main savepoint reached
2178 upgrade_main_savepoint($result, 2009060200);
2179 }
2180
b4154c2d 2181 if ($result && $oldversion < 2009061300) {
2182 //TODO: copy this to the very beginning of this upgrade script so that we may log upgrade queries
2183
2184 /// Define table log_queries to be created
2185 $table = new xmldb_table('log_queries');
2186
2187 /// Adding fields to table log_queries
2188 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2189 $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2190 $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
2191 $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
2192 $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2193 $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2194 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2195 $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
2196 $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2197
2198 /// Adding keys to table log_queries
2199 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2200
2201 /// Conditionally launch create table for log_queries
2202 if (!$dbman->table_exists($table)) {
2203 $dbman->create_table($table);
2204 }
2205
2206 /// Main savepoint reached
2207 upgrade_main_savepoint($result, 2009061300);
2208 }
2209
52902cea 2210 /// Repeat 2009050607 upgrade step, which Petr commented out becuase of XMLDB
2211 /// stupidity, so lots of peopel will have missed.
2212 if ($result && $oldversion < 2009061600) {
2213 /// Changing precision of field defaultregion on table block_instances to (16)
2214 $table = new xmldb_table('block_instances');
2215 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
2216
2217 /// Launch change of precision for field defaultregion
2218 $dbman->change_field_precision($table, $field);
2219
2220 /// Main savepoint reached
2221 upgrade_main_savepoint($result, 2009061600);
2222 }
2223
17da2e6f 2224 if ($result && $oldversion < 2009061702) {
2225 // standardizing plugin names
2226 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
2227 foreach ($configs as $config) {
ce11763a 2228 $result = $result && unset_config($config->name, $config->plugin); /// unset old config
17da2e6f 2229 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
ce11763a 2230 $result = $result && set_config($config->name, $config->value, $config->plugin); /// set new config
17da2e6f 2231 }
2232 }
2233 unset($configs);
2234 upgrade_main_savepoint($result, 2009061702);
2235 }
2236
2237 if ($result && $oldversion < 2009061703) {
2238 // standardizing plugin names
2239 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
2240 foreach ($configs as $config) {
ce11763a 2241 $result = $result && unset_config($config->name, $config->plugin); /// unset old config
17da2e6f 2242 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
ce11763a 2243 $result = $result && set_config($config->name, $config->value, $config->plugin); /// set new config
17da2e6f 2244 }
2245 }
2246 unset($configs);
2247 upgrade_main_savepoint($result, 2009061703);
2248 }
2249
2250 if ($result && $oldversion < 2009061704) {
13a0d3d3 2251 // change component string in capability records to new "_" format
17da2e6f 2252 if ($caps = $DB->get_records('capabilities')) {
2253 foreach ($caps as $cap) {
2254 $cap->component = str_replace('/', '_', $cap->component);
2255 $DB->update_record('capabilities', $cap);
2256 }
2257 }
2258 unset($caps);
2259 upgrade_main_savepoint($result, 2009061704);
2260 }
2261
2262 if ($result && $oldversion < 2009061705) {
13a0d3d3 2263 // change component string in events_handlers records to new "_" format
17da2e6f 2264 if ($handlers = $DB->get_records('events_handlers')) {
2265 foreach ($handlers as $handler) {
2266 $handler->handlermodule = str_replace('/', '_', $handler->handlermodule);
2267 $DB->update_record('events_handlers', $handler);
2268 }
2269 }
2270 unset($handlers);
2271 upgrade_main_savepoint($result, 2009061705);
2272 }
2273
2274 if ($result && $oldversion < 2009061706) {
13a0d3d3 2275 // change component string in message_providers records to new "_" format
17da2e6f 2276 if ($mps = $DB->get_records('message_providers')) {
2277 foreach ($mps as $mp) {
2278 $mp->component = str_replace('/', '_', $mp->component);
2279 $DB->update_record('message_providers', $cap);
2280 }
2281 }
2282 unset($caps);
2283 upgrade_main_savepoint($result, 2009061706);
2284 }
13a0d3d3 2285
adf8f3f9 2286 if ($result && $oldversion < 2009063000) {
2287 // upgrade format of _with_advanced settings - quiz only
2288 // note: this can be removed later, not needed for upgrades from 1.9.x
c1f58efe 2289 if ($quiz = get_config('quiz')) {
2290 foreach ($quiz as $name=>$value) {
2291 if (strpos($name, 'fix_') !== 0) {
2292 continue;
2293 }
2294 $newname = substr($name,4).'_adv';
2295 set_config($newname, $value, 'quiz');
2296 unset_config($name, 'quiz');
adf8f3f9 2297 }
13a0d3d3 2298 }
adf8f3f9 2299 upgrade_main_savepoint($result, 2009063000);
2300 }
13a0d3d3 2301
13a0d3d3 2302 if ($result && $oldversion < 2009071000) {
2303
2304 /// Rename field contextid on table block_instances to parentcontextid
2305 $table = new xmldb_table('block_instances');
2306 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
2307
2308 /// Launch rename field parentcontextid
2309 $dbman->rename_field($table, $field, 'parentcontextid');
2310
2311 /// Main savepoint reached
2312 upgrade_main_savepoint($result, 2009071000);
2313 }
2314
e92c286c 2315 if ($result && $oldversion < 2009071300) {
2316
2317 /// Create contexts for every block. In the past, only non-sticky course block had contexts.
2318 /// This is a copy of the code in create_contexts.
2319 $sql = "INSERT INTO {context} (contextlevel, instanceid)
2320 SELECT " . CONTEXT_BLOCK . ", bi.id
2321 FROM {block_instances} bi
2322 WHERE NOT EXISTS (SELECT 'x'
2323 FROM {context} ctx
2324 WHERE bi.id = ctx.instanceid AND ctx.contextlevel=" . CONTEXT_BLOCK . ")";
2325 $DB->execute($sql);
2326
2327 /// TODO MDL-19776 We should not really use API funcitons in upgrade.
2328 /// If MDL-19776 is done, we can remove this whole upgrade block.
2329 build_context_path();
2330
2331 /// Main savepoint reached
2332 upgrade_main_savepoint($result, 2009071300);
2333 }
1918f7e0 2334
cae83708 2335 if ($result && $oldversion < 2009071600) {
2336
2337 /// Define field summaryformat to be added to post
2338 $table = new xmldb_table('post');
2339 $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
2340
2341 /// Conditionally launch add field summaryformat
2342 if (!$dbman->field_exists($table, $field)) {
2343 $dbman->add_field($table, $field);
2344 }
2345
2346 /// Main savepoint reached
2347 upgrade_main_savepoint($result, 2009071600);
2348 }
e92c286c 2349
1bcb7eb5 2350 if ($result && $oldversion < 2009072400) {
2351
2352 /// Define table comments to be created
2353 $table = new xmldb_table('comments');
2354
2355 /// Adding fields to table comments
2356 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2357 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2358 $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2359 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2360 $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2361 $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2362 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2363 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2364
2365 /// Adding keys to table comments
2366 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2367
2368 /// Conditionally launch create table for comments
2369 if (!$dbman->table_exists($table)) {
2370 $dbman->create_table($table);
2371 }
0a127169 2372
1bcb7eb5 2373 /// Main savepoint reached
2374 upgrade_main_savepoint($result, 2009072400);
2375 }
2376
7d2a0492 2377 /**
2378 * This upgrade is to set up the new navigation blocks that have been developed
2379 * as part of Moodle 2.0
2380 * Now I [Sam Hemelryk] hit a conundrum while exploring how to go about this
2381 * as not only do we want to install the new blocks but we also want to set up
2382 * default instances of them, and at the same time remove instances of the blocks
2383 * that were/will-be outmoded by the two new navigation blocks.
2384 * After talking it through with Tim Hunt {@link http://moodle.org/mod/cvsadmin/view.php?conversationid=3112}
2385 * we decided that the best way to go about this was to put the bulk of the
2386 * upgrade operation into core upgrade `here` but to let the plugins block
2387 * still install the blocks.
2388 * This leaves one hairy end in that we will create block_instances within the
2389 * DB before the blocks themselves are created within the DB
2390 */
2391 if ($result && $oldversion < 2009082800) {
2392
2393 echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2394
2395 // Get the system context so we can set the block instances to it
2396 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2397
2398 // An array to contain the new block instances we will create
2399 $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
2400 // The new global navigation block instance as a stdClass
2401 $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
2402 $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
2403 $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutly everywhere
2404 $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
2405 $newblockinstances['globalnavigation']->subpagetypepattern = null;
2406 $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
2407 $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
2408 $newblockinstances['globalnavigation']->configdata = '';
2409 // The new settings navigation block instance as a stdClass
2410 $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
2411 $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
2412 $newblockinstances['settingsnavigation']->showinsubcontexts = true;
2413 $newblockinstances['settingsnavigation']->pagetypepattern = '*';
2414 $newblockinstances['settingsnavigation']->subpagetypepattern = null;
2415 $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
2416 $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
2417 $newblockinstances['settingsnavigation']->configdata = '';
2418
2419 // Blocks that are outmoded and for whom the bells will toll... by which I
2420 // mean we will delete all instances of
2421 $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
2422 $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
2423 unset($outmodedblocks);
2424 // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
2425 // cut down the number of delete queries we will need to run
2426 $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
1918f7e0 2427
7d2a0492 2428 $contextids = array();
2429 $instanceids = array();
2430 // Iterate through all block instances
2431 foreach ($allblockinstances as $blockinstance) {
2432 if (!in_array($blockinstance->parentcontextid, $contextids)) {
2433 $contextids[] = $blockinstance->parentcontextid;
2434
2435 // If we have over 1000 contexts clean them up and reset the array
2436 // this ensures we don't hit any nasty memory limits or such
2437 if (count($contextids) > 1000) {
2438 $result = $result && upgrade_cleanup_unwanted_block_contexts($contextids);
2439 $contextids = array();
2440 }
2441 }
2442 if (!in_array($blockinstance->id, $instanceids)) {
2443 $instanceids[] = $blockinstance->id;
2444 // If we have more than 1000 block instances now remove all block positions
2445 // and empty the array
2446 if (count($contextids) > 1000) {
2447 $instanceidstring = join(',',$instanceids);
2448 $result = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2449 $instanceids = array();
2450 }
2451 }
2452 }
2453
2454 $result = $result && upgrade_cleanup_unwanted_block_contexts($contextids);
2455
2456 $instanceidstring = join(',',$instanceids);
2457 $outcome1 = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
1918f7e0 2458
7d2a0492 2459 unset($allblockinstances);
2460 unset($contextids);
2461 unset($instanceids);
2462 unset($instanceidstring);
1918f7e0 2463
7d2a0492 2464 // Now remove the actual block instance
2465 $result = $result && $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
2466 unset($outmodedblocksstring);
2467
2468 // Insert the new block instances. Remember they have not been installed yet
2469 // however this should not be a problem
2470 foreach ($newblockinstances as $blockinstance) {
2471 $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
2472 // Ensure the block context is created.
2473 get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
2474 }
2475 unset($newblockinstances);
1918f7e0 2476
7d2a0492 2477 upgrade_main_savepoint($result, 2009082800);
2478 // The end of the navigation upgrade
2479 }
2480
b89e4ad8 2481 if ($result && $oldversion < 2009090800){
2482 //insert new record for log_display table
2483 //used to record tag update.
2484 if (!$DB->record_exists('log_display', array('action'=>'update', 'module'=>'tag'))) {
2485 $log_action = new object();
2486 $log_action->module = 'tag';
2487 $log_action->action = 'update';
2488 $log_action->mtable = 'tag';
2489 $log_action->field = 'name';
2490
2491 $result = $result && $DB->insert_record('log_display', $log_action);
2492 }
2493 upgrade_main_savepoint($result, 2009090800);
2494 }
4313fa89 2495
88af54fa 2496 if ($result && $oldversion < 2009100601) {
5d820ca2 2497 // drop all previous tables defined during the dev phase
5b9a70a1 2498 $dropold = array('external_services_users', 'external_services_functions', 'external_services', 'external_functions');
5d820ca2 2499 foreach ($dropold as $tablename) {
2500 $table = new xmldb_table($tablename);
2501 if ($dbman->table_exists($table)) {
2502 $dbman->drop_table($table);
2503 }
4313fa89 2504 }
88af54fa 2505 upgrade_main_savepoint($result, 2009100601);
5d820ca2 2506 }
4313fa89 2507
88af54fa 2508 if ($result && $oldversion < 2009100602) {
4313fa89 2509 /// Define table external_functions to be created
2510 $table = new xmldb_table('external_functions');
2511
2512 /// Adding fields to table external_functions
2513 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
5d820ca2 2514 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2515 $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2516 $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2517 $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
4313fa89 2518 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
4313fa89 2519
2520 /// Adding keys to table external_functions
2521 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2522
5d820ca2 2523 /// Adding indexes to table external_functions
2524 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2525
2526 /// Launch create table for external_functions
2527 $dbman->create_table($table);
4313fa89 2528
5d820ca2 2529 /// Main savepoint reached
88af54fa 2530 upgrade_main_savepoint($result, 2009100602);
5d820ca2 2531 }
4313fa89 2532
88af54fa 2533 if ($result && $oldversion < 2009100603) {
4313fa89 2534 /// Define table external_services to be created
2535 $table = new xmldb_table('external_services');
2536
2537 /// Adding fields to table external_services
2538 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
5d820ca2 2539 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2540 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2541 $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
2542 $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2543 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
4313fa89 2544
2545 /// Adding keys to table external_services
2546 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2547
5b9a70a1 2548 /// Adding indexes to table external_services
2549 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2550
5d820ca2 2551 /// Launch create table for external_services
2552 $dbman->create_table($table);
4313fa89 2553
5d820ca2 2554 /// Main savepoint reached
88af54fa 2555 upgrade_main_savepoint($result, 2009100603);
5d820ca2 2556 }
4313fa89 2557
88af54fa 2558 if ($result && $oldversion < 2009100604) {
4313fa89 2559 /// Define table external_services_functions to be created
2560 $table = new xmldb_table('external_services_functions');
2561
2562 /// Adding fields to table external_services_functions
2563 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2564 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
5d820ca2 2565 $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
4313fa89 2566
2567 /// Adding keys to table external_services_functions
2568 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
5d820ca2 2569 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
4313fa89 2570
5d820ca2 2571 /// Launch create table for external_services_functions
2572 $dbman->create_table($table);
2573
2574 /// Main savepoint reached
88af54fa 2575 upgrade_main_savepoint($result, 2009100604);
5d820ca2 2576 }
2577
88af54fa 2578 if ($result && $oldversion < 2009100605) {
5d820ca2 2579 /// Define table external_services_users to be created
2580 $table = new xmldb_table('external_services_users');
2581
2582 /// Adding fields to table external_services_users
2583 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2584 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2585 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
88af54fa 2586 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2587 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2588 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
5d820ca2 2589
2590 /// Adding keys to table external_services_users
2591 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2592 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2593 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2594
2595 /// Launch create table for external_services_users
2596 $dbman->create_table($table);
4313fa89 2597
2598 /// Main savepoint reached
88af54fa 2599 upgrade_main_savepoint($result, 2009100605);
4313fa89 2600 }
2601
1942103f
PS
2602 if ($result && $oldversion < 2009102600) {
2603
2604 /// Define table external_tokens to be created
2605 $table = new xmldb_table('external_tokens');
2606
2607 /// Adding fields to table external_tokens
2608 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2609 $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
2610 $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2611 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2612 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2613 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
2614 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2615 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2616 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2617 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2618 $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2619
2620 /// Adding keys to table external_tokens
2621 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2622 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2623 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2624 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2625
2626 /// Launch create table for external_tokens
2627 $dbman->create_table($table);
2628
2629 /// Main savepoint reached
2630 upgrade_main_savepoint($result, 2009102600);
2631 }
2632
cc82aef6
NC
2633 if ($result && $oldversion < 2009103000) {
2634
2635 /// Define table blog_association to be created
2636 $table = new xmldb_table('blog_association');
2637
2638 /// Adding fields to table blog_association
2639 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2640 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2641 $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2642
2643 /// Adding keys to table blog_association
2644 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2645 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2646 $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
2647
2648 /// Conditionally launch create table for blog_association
2649 if (!$dbman->table_exists($table)) {
2650 $dbman->create_table($table);
2651 }
2652
2653/// Define table blog_external to be created
2654 $table = new xmldb_table('blog_external');
2655
2656 /// Adding fields to table blog_external
2657 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2658 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2659 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2660 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2661 $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2662 $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2663 $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2664 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2665 $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2666
2667 /// Adding keys to table blog_external
2668 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2669 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2670
2671 /// Conditionally launch create table for blog_external
2672 if ($dbman->table_exists($table)) {
2673 // Delete the existing one first (comes from early dev version)
2674 $dbman->drop_table($table);
2675 }
2676 $dbman->create_table($table);
2677
2678 // Print notice about need to upgrade bloglevel
2679 if (($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete)) {
2680 echo $OUTPUT->notification(get_string('bloglevelupgradenotice', 'admin'));
2681
2682 // email admins about the need to upgrade their system using the admin/bloglevelupgrade.php script
2683 $admins = get_admins();
2684 $site = get_site();
2685
2686 $a = new StdClass;
2687 $a->sitename = $site->fullname;
2688 $a->fixurl = "$CFG->wwwroot/$CFG->admin/bloglevelupgrade.php";
2689
c01678b1
PS
2690 $subject = get_string('bloglevelupgrade', 'admin');
2691 $description = get_string('bloglevelupgradedescription', 'admin', $a);
2692
2693 // can not use messaging here because it is not configured yet!
2694 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
cc82aef6
NC
2695 }
2696 /// Main savepoint reached
2697 upgrade_main_savepoint($result, 2009103000);
2698 }
117bd748 2699
8bdc9cac
SH
2700 if ($result && $oldversion < 2009110400) {
2701
2702 // An array used to store the table name and keys of summary and trust fields
2703 // to be added
2704 $extendtables = array();
2705 $extendtables['course'] = array('summaryformat');
2706 $extendtables['course_categories'] = array('descriptionformat');
2707 $extendtables['course_request'] = array('summaryformat');
2708 $extendtables['grade_outcomes'] = array('descriptionformat');
2709 $extendtables['groups'] = array('descriptionformat');
2710 $extendtables['groupings'] = array('descriptionformat');
2711 $extendtables['scale'] = array('descriptionformat');
2712 $extendtables['user'] = array('descriptionformat');
2713 $extendtables['user_info_field'] = array('descriptionformat', 'defaultdataformat');
2714 $extendtables['user_info_data'] = array('dataformat');
2715
2716 foreach ($extendtables as $tablestr=>$newfields) {
2717 $table = new xmldb_table($tablestr);
2718 foreach ($newfields as $fieldstr) {
2719 $field = new xmldb_field($fieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2720 // Check that the field doesn't already exists
2721 if (!$dbman->field_exists($table, $field)) {
2722 // Add the new field
2723 $dbman->add_field($table, $field);
2724 // Update the field if the text contains the default FORMAT_MOODLE to FORMAT_HTML
2725 if (($pos = strpos($fieldstr, 'format'))>0) {
2726 upgrade_set_timeout(60*20); // this may take a little while
2727 $params = array(FORMAT_HTML, '<p%', '%<br />%', FORMAT_MOODLE);
2728 $textfield = substr($fieldstr, 0, $pos);
cb8a83bb 2729 $DB->execute('UPDATE {'.$tablestr.'} SET '.$fieldstr.'=? WHERE ('.$textfield.' LIKE ? OR '.$textfield.' LIKE ?) AND '.$fieldstr.'=?', $params);
8bdc9cac
SH
2730 }
2731 }
2732 }
2733 }
2734
2735 unset($extendtables);
2736
2737 upgrade_main_savepoint($result, 2009110400);
2738 }
2739
975341d1 2740 if ($result && $oldversion < 2009110605) {
2741
2742 /// Define field timecreated to be added to external_services
2743 $table = new xmldb_table('external_services');
2744 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'component');
2745
2746 /// Conditionally launch add field timecreated
2747 if (!$dbman->field_exists($table, $field)) {
2748 $dbman->add_field($table, $field);
2749 }
2750
2751 /// Define field timemodified to be added to external_services
2752 $table = new xmldb_table('external_services');
2753 $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'timecreated');
2754
2755 /// Conditionally launch add field timemodified
2756 if (!$dbman->field_exists($table, $field)) {
2757 $dbman->add_field($table, $field);
2758 }
2759
2760 /// Main savepoint reached
2761 upgrade_main_savepoint($result, 2009110605);
2762 }
2763
e1b9ac3e
PL
2764 if ($result && $oldversion < 2009111600) {
2765
2766 /// Define field instance to be added to portfolio_tempdata
2767 $table = new xmldb_table('portfolio_tempdata');
2768 $field = new xmldb_field('instance', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'userid');
2769
2770 /// Conditionally launch add field instance
2771 if (!$dbman->field_exists($table, $field)) {
2772 $dbman->add_field($table, $field);
2773 }
2774
2775 $key = new xmldb_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
2776
2777 /// Launch add key instancefk
2778 $dbman->add_key($table, $key);
2779
2780 /// Main savepoint reached
2781 upgrade_main_savepoint($result, 2009111600);
2782 }
2783
45645bce
PL
2784 if ($result && $oldversion < 2009111700) {
2785
2786 /// Define field tempdataid to be added to portfolio_log
2787 $table = new xmldb_table('portfolio_log');
2788 $field = new xmldb_field('tempdataid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'caller_sha1');
2789
2790 /// Conditionally launch add field tempdataid
2791 if (!$dbman->field_exists($table, $field)) {
2792 $dbman->add_field($table, $field);
2793 }
2794
2795 /// Main savepoint reached
2796 upgrade_main_savepoint($result, 2009111700);
2797 }
2798
eeb7ccf0
PL
2799 if ($result && $oldversion < 2009111701) {
2800
2801 /// Define field returnurl to be added to portfolio_log
2802 $table = new xmldb_table('portfolio_log');
2803 $field = new xmldb_field('returnurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'tempdataid');
2804
2805 /// Conditionally launch add field returnurl
2806 if (!$dbman->field_exists($table, $field)) {
2807 $dbman->add_field($table, $field);
2808 }
2809
2810 /// Main savepoint reached
2811 upgrade_main_savepoint($result, 2009111701);
2812 }
2813
2814 if ($result && $oldversion < 2009111702) {
2815
2816 /// Define field continueurl to be added to portfolio_log
2817 $table = new xmldb_table('portfolio_log');
2818 $field = new xmldb_field('continueurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'returnurl');
2819
2820 /// Conditionally launch add field continueurl
2821 if (!$dbman->field_exists($table, $field)) {
2822 $dbman->add_field($table, $field);
2823 }
2824
2825 /// Main savepoint reached
2826 upgrade_main_savepoint($result, 2009111702);
2827 }
2828
57e6ce5e
PS
2829 if ($result && $oldversion < 2009112400) {
2830 if (empty($CFG->passwordsaltmain)) {
2831 $subject = get_string('check_passwordsaltmain_name', 'report_security');
2832 $description = get_string('check_passwordsaltmain_warning', 'report_security');;
2833 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2834 }
2835 upgrade_main_savepoint($result, 2009112400);
2836 }
e1b9ac3e 2837
5ae9f539 2838 if ($result && $oldversion < 2010010601) {
2839
2840 /// Define field creatorid to be added to external_tokens
2841 $table = new xmldb_table('external_tokens');
2842 $field = new xmldb_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'contextid');
2843
2844 /// Conditionally launch add field creatorid
2845 if (!$dbman->field_exists($table, $field)) {
2846 $dbman->add_field($table, $field);
2847 }
2848
2849 /// Define key creatorid (foreign) to be added to external_tokens
2850 $table = new xmldb_table('external_tokens');
2851 $key = new xmldb_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
2852
2853 /// Launch add key creatorid
2854 $dbman->add_key($table, $key);
2855
2856 /// Main savepoint reached
2857 upgrade_main_savepoint($result, 2010010601);
2858 }
2859
c95002a2 2860 if ($result && $oldversion < 2010011200) {
a25bb902
AD
2861 $table = new xmldb_table('grade_categories');
2862 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
2863
2864 if (!$dbman->field_exists($table, $field)) {
2865 $dbman->add_field($table, $field);
2866 }
2867
c95002a2 2868 upgrade_main_savepoint($result, 2010011200);
a25bb902
AD
2869 }
2870
de260e0f 2871
7062a798
PL
2872 if ($result && $oldversion < 2010012500) {
2873 upgrade_fix_incorrect_mnethostids();
2874 upgrade_main_savepoint($result, 2010012500);
2875 }
de260e0f
PL
2876
2877 if ($result && $oldversion < 2010012600) {
2878 // do stuff to the mnet table
2879 $table = new xmldb_table('mnet_rpc');
2880
2881 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2882 $dbman->rename_field($table, $field, 'plugintype');
2883
2884 $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2885 $dbman->rename_field($table, $field, 'pluginname');
2886
2887 $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
2888 if (!$dbman->field_exists($table, $field)) {
2889 $dbman->add_field($table, $field);
2890 }
2891
2892 $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
2893 if (!$dbman->field_exists($table, $field)) {
2894 $dbman->add_field($table, $field);
2895 }
2896
2897 $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
2898 if (!$dbman->field_exists($table, $field)) {
2899 $dbman->add_field($table, $field);
2900 }
2901
2902 /// Main savepoint reached
2903 upgrade_main_savepoint($result, 2010012600);
2904 }
2905
8586dbe2
PL
2906 if ($result && $oldversion < 2010012900) {
2907
2908 /// Define table mnet_remote_rpc to be created
2909 $table = new xmldb_table('mnet_remote_rpc');
2910
2911 /// Adding fields to table mnet_remote_rpc
2912 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2913 $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2914 $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
2915
2916 /// Adding keys to table mnet_remote_rpc
2917 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2918
2919 /// Conditionally launch create table for mnet_remote_rpc
2920 if (!$dbman->table_exists($table)) {
2921 $dbman->create_table($table);
2922 }
2923
2924
2925 /// Define table mnet_remote_service2rpc to be created
2926 $table = new xmldb_table('mnet_remote_service2rpc');
2927
2928 /// Adding fields to table mnet_remote_service2rpc
2929 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2930 $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2931 $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2932
2933 /// Adding keys to table mnet_remote_service2rpc
2934 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2935
2936 /// Adding indexes to table mnet_remote_service2rpc
2937 $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2938
2939 /// Conditionally launch create table for mnet_remote_service2rpc
2940 if (!$dbman->table_exists($table)) {
2941 $dbman->create_table($table);
2942 }
2943
2944
2945 /// Rename field function_name on table mnet_rpc to functionname
2946 $table = new xmldb_table('mnet_rpc');
2947 $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
2948
2949 /// Launch rename field function_name
2950 $dbman->rename_field($table, $field, 'functionname');
2951
2952
2953 /// Rename field xmlrpc_path on table mnet_rpc to xmlrpcpath
2954 $table = new xmldb_table('mnet_rpc');
2955 $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
2956
2957 /// Launch rename field xmlrpc_path
2958 $dbman->rename_field($table, $field, 'xmlrpcpath');
2959
2960
2961 /// Main savepoint reached
2962 upgrade_main_savepoint($result, 2010012900);
2963 }
a6e9dafe
PL
2964
2965 if ($result && $oldversion < 2010012901) {
2966
2967 /// Define field plugintype to be added to mnet_remote_rpc
2968 $table = new xmldb_table('mnet_remote_rpc');
2969 $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
2970
2971 /// Conditionally launch add field plugintype
2972 if (!$dbman->field_exists($table, $field)) {
2973 $dbman->add_field($table, $field);
2974 }
2975
a6e9dafe
PL
2976 /// Define field pluginname to be added to mnet_remote_rpc
2977 $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
2978
2979 /// Conditionally launch add field pluginname
2980 if (!$dbman->field_exists($table, $field)) {
2981 $dbman->add_field($table, $field);
2982 }
5b7984a5
PL
2983
2984 /// Main savepoint reached
2985 upgrade_main_savepoint($result, 2010012901);
2986 }
2987
2988 if ($result && $oldversion < 2010012902) {
2989
2990 /// Define field enabled to be added to mnet_remote_rpc
2991 $table = new xmldb_table('mnet_remote_rpc');
2992 $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
2993
2994 /// Conditionally launch add field enabled
2995 if (!$dbman->field_exists($table, $field)) {
2996 $dbman->add_field($table, $field);
2997 }
2998
2999 /// Main savepoint reached
3000 upgrade_main_savepoint($result, 2010012902);
a6e9dafe
PL
3001 }
3002
f6852f02
PL
3003 /// MDL-17863. Increase the portno column length on mnet_host to handle any port number
3004 if ($result && $oldversion < 2010020100) {
3005 /// Changing precision of field portno on table mnet_host to (5)
3006 $table = new xmldb_table('mnet_host');
3007 $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
3008
3009 /// Launch change of precision for field portno
3010 $dbman->change_field_precision($table, $field);
3011
3012 upgrade_main_savepoint($result, 2010020100);
3013 }
3014
d3d393ab
RW
3015 if ($result && $oldversion < 2010020300) {
3016
3017 /// Define field timecreated to be added to user
3018 $table = new xmldb_table('user');
3019 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
3020
3021 if (!$dbman->field_exists($table, $field)) {
3022 /// Launch add field timecreated
3023 $dbman->add_field($table, $field);
3024
3025 $DB->execute("UPDATE {user} SET timecreated = firstaccess");
3026
3027 $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
3028 $DB->execute($sql);
3029 }
3030 upgrade_main_savepoint($result, 2010020300);
3031 }
073d3804
EL
3032
3033 // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
3034 if ($result && $oldversion < 2010020301) {
3035 if ($preamble = $CFG->filter_tex_latexpreamble) {
3036 $preamble = preg_replace('/^ +/m', '', $preamble);
3037 set_config('filter_tex_latexpreamble', $preamble);
3038 }
3039 upgrade_main_savepoint($result, 2010020301);
3040 }
3041
9a9012dc
PS
3042 if ($result && $oldversion < 2010021400) {
3043 /// Changes to modinfo mean we need to rebuild course cache
3044 require_once($CFG->dirroot . '/course/lib.php');
3045 rebuild_course_cache(0, true);
3046 upgrade_main_savepoint($result, 2010021400);
3047 }
3048
c9606565
PL
3049 if ($result && $oldversion < 2010021800) {
3050 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
3051 upgrade_main_savepoint($result, 2010021800);
3052 }
3053
8b69c341 3054 if ($result && $oldversion < 2010031800) {
0f51ca79
AD
3055 //drop the erroneously created ratings table
3056 $table = new xmldb_table('ratings');
3057 if ($dbman->table_exists($table)) {
3058 $dbman->drop_table($table);
3059 }
3060
54e42e0b
AD
3061 //create the rating table (replaces module specific rating implementations)
3062 $table = new xmldb_table('rating');
8b69c341
AD
3063 if ($dbman->table_exists($table)) {
3064 $dbman->drop_table($table);
3065 }
a09aeee4 3066
54e42e0b 3067 /// Adding fields to table rating
a09aeee4
AD
3068 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3069 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3070
3071 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3072 $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3073 $table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3074 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3075
3076 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3077 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3078
54e42e0b 3079 /// Adding keys to table rating
a09aeee4
AD
3080 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3081 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3082 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3083
54e42e0b 3084 /// Adding indexes to table rating
a09aeee4
AD
3085 $table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
3086
3087 /// Create table for ratings
158cd5d9
AD
3088 if (!$dbman->table_exists($table)) {
3089 $dbman->create_table($table);
3090 }
a09aeee4 3091
8b69c341 3092 upgrade_main_savepoint($result, 2010031800);
a09aeee4
AD
3093 }
3094
cd1bca89
PS
3095 if ($result && $oldversion < 2010031900) {
3096 // regeneration of sessions is always enabled, no need for this setting any more
3097 unset_config('regenloginsession');
3098 upgrade_main_savepoint($result, 2010031900);
3099 }
90723839 3100
1dce6261 3101
90723839
SH
3102 if ($result && $oldversion < 2010032400) {
3103 // Upgrade all of those using the standardold theme to the use the standard
3104 // theme instead
3105 if ($CFG->theme == 'standardold') {
3106 // The config setting that affects the whole site
3107 set_config('theme', 'standard');
3108 }
3109 // Course Categories
3110 $DB->execute('UPDATE {course_categories} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3111 // Course
3112 $DB->execute('UPDATE {course} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3113 // User
3114 $DB->execute('UPDATE {user} SET theme=? WHERE theme=?', array('standard', 'standardold'));
3115 upgrade_main_savepoint($result, 2010032400);
3116 }
1dce6261
DC
3117
3118 if ($result && $oldversion < 2010032405) {
3119
3120 /// Define field source to be added to files
3121 $table = new xmldb_table('files');
3122
3123 $field = new xmldb_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'timemodified');
3124
3125 /// Conditionally launch add field source
3126 if (!$dbman->field_exists($table, $field)) {
3127 $dbman->add_field($table, $field);
3128 }
3129
3130 $field = new xmldb_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'source');
3131
3132 /// Conditionally launch add field author
3133 if (!$dbman->field_exists($table, $field)) {
3134 $dbman->add_field($table, $field);
3135 }
3136
3137 $field = new xmldb_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'author');
3138
3139 /// Conditionally launch add field license
3140 if (!$dbman->field_exists($table, $field)) {
3141 $dbman->add_field($table, $field);
3142 }
3143
3144 /// Define table license to be created
3145 $table = new xmldb_table('license');
3146
3147 /// Adding fields to table license
3148 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3149 $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3150 $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
3151 $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3152 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
3153 $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
3154
3155 /// Adding keys to table license
3156 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3157
3158 /// Conditionally launch create table for license
3159 if (!$dbman->table_exists($table)) {
3160 $dbman->create_table($table);
3161 }
3162 require_once($CFG->libdir . '/licenselib.php');
3163 license_manager::install_licenses();
3164 /// set site default license
3165 set_config('sitedefaultlicense', 'allrightsreserved');
3166
3167 /// Main savepoint reached
3168 upgrade_main_savepoint($result, 2010032405);
3169 }
cd1bca89 3170
ac9b0805 3171 return $result;
4e423cbf 3172}
271e6dec 3173
e92c286c 3174//TODO: Before 2.0 release
3175// 1/ remove the automatic enabling of completion lib if debug enabled ( in 2008121701 block)
3176// 2/ move 2009061300 block to the top of the file so that we may log upgrade queries
2f223338 3177// 3/ force admin password change if salt not set, to be done after planned role changes