towards db layer 2.0
[moodle.git] / lib / db / upgrade.php
CommitLineData
4e423cbf 1<?PHP //$Id$
2
3// This file keeps track of upgrades to Moodle.
4//
5// Sometimes, changes between versions involve
6// alterations to database structures and other
7// major things that may break installations.
8//
9// The upgrade function in this file will attempt
10// to perform all the necessary actions to upgrade
11// your older installtion to the current version.
12//
13// If there's something it cannot do itself, it
14// will tell you what you need to do.
15//
16// The commands in here will all be database-neutral,
b1f93b15 17// using the methods of database_manager class
775f811a 18//
19// Please do not forget to use upgrade_set_timeout()
20// before any action that may take longer time to finish.
4e423cbf 21
775f811a 22function xmldb_main_upgrade($oldversion) {
f33e1ed4 23 global $CFG, $THEME, $USER, $DB;
4e423cbf 24
25 $result = true;
26
46d318bc 27 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
f33e1ed4 28
1ea260d8 29 ////////////////////////////////////////
30 ///upgrade supported only from 1.9.x ///
31 ////////////////////////////////////////
da4aa3e4 32
795a08ad 33 if ($result && $oldversion < 2008030600) {
34 //NOTE: this table was added much later, that is why this step is repeated later in this file
35
36 /// Define table upgrade_log to be created
37 $table = new xmldb_table('upgrade_log');
38
39 /// Adding fields to table upgrade_log
2a88f626 40 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
41 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
42 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
43 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
44 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
45 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
46 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
47 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
48 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
795a08ad 49
50 /// Adding keys to table upgrade_log
51 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
52 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
53
54 /// Adding indexes to table upgrade_log
55 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
56 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
57
58 /// Create table for upgrade_log
59 $dbman->create_table($table);
60
61 /// Main savepoint reached
62 upgrade_main_savepoint($result, 2008030600);
63 }
64
a1f27717 65 if ($result && $oldversion < 2008030700) {
775f811a 66 upgrade_set_timeout(60*20); // this may take a while
a1f27717 67
68 /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
a8cb94f6 69 $table = new xmldb_table('grade_letters');
69b80cc2 70 $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
a1f27717 71
72 /// Launch drop index contextid-lowerboundary
eee5d9bb 73 $dbman->drop_index($table, $index);
a1f27717 74
a1f27717 75 /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
a8cb94f6 76 $table = new xmldb_table('grade_letters');
69b80cc2 77 $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
a1f27717 78
79 /// Launch add index contextid-lowerboundary-letter
eee5d9bb 80 $dbman->add_index($table, $index);
a1f27717 81
82 /// Main savepoint reached
0f77b186 83 upgrade_main_savepoint($result, 2008030700);
84 }
a1f27717 85
1ea260d8 86 if ($result && $oldversion < 2008050100) {
87 // Update courses that used weekscss to weeks
a8cb94f6 88 $result = $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
1ea260d8 89 upgrade_main_savepoint($result, 2008050100);
90 }
91
0b51c247 92 if ($result && $oldversion < 2008050200) {
93 // remove unused config options
94 unset_config('statsrolesupgraded');
95 upgrade_main_savepoint($result, 2008050200);
96 }
97
994fbaab 98 if ($result && $oldversion < 2008050700) {
775f811a 99 upgrade_set_timeout(60*20); // this may take a while
100
994fbaab 101 /// Fix minor problem caused by MDL-5482.
102 require_once($CFG->dirroot . '/question/upgrade.php');
103 $result = $result && question_fix_random_question_parents();
104 upgrade_main_savepoint($result, 2008050700);
105 }
106
ab37dc60 107 if ($result && $oldversion < 2008051200) {
108 // if guest role used as default user role unset it and force admin to choose new setting
109 if (!empty($CFG->defaultuserroleid)) {
910dd4dd 110 if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
ab37dc60 111 if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
112 if (isset($guestroles[$role->id])) {
113 set_config('defaultuserroleid', null);
114 notify('Guest role removed from "Default role for all users" setting, please select another role.', 'notifysuccess');
115 }
116 }
117 } else {
118 set_config('defaultuserroleid', null);
119 }
120 }
121 }
122
8b9cfac4 123 if ($result && $oldversion < 2008051201) {
124 notify('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
775f811a 125 upgrade_set_timeout(60*20); // this may take a while
8b9cfac4 126
469dcbcc 127 /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
cbc08f3b 128 $dbfamily = $DB->get_dbfamily();
129 if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
eee5d9bb 130 $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
469dcbcc 131 }
132
8b9cfac4 133 /// Define index idnumber (not unique) to be dropped form user
a8cb94f6 134 $table = new xmldb_table('user');
69b80cc2 135 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
8b9cfac4 136
137 /// Launch drop index idnumber
f33e1ed4 138 if ($dbman->index_exists($table, $index)) {
eee5d9bb 139 $dbman->drop_index($table, $index);
8b9cfac4 140 }
141
142 /// Changing precision of field idnumber on table user to (255)
a8cb94f6 143 $table = new xmldb_table('user');
2a88f626 144 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
8b9cfac4 145
146 /// Launch change of precision for field idnumber
eee5d9bb 147 $dbman->change_field_precision($table, $field);
8b9cfac4 148
149 /// Launch add index idnumber again
69b80cc2 150 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
eee5d9bb 151 $dbman->add_index($table, $index);
8b9cfac4 152
153 /// Main savepoint reached
154 upgrade_main_savepoint($result, 2008051201);
155 }
156
7ade55ad 157 if ($result && $oldversion < 2008051202) {
910dd4dd 158 $log_action = new object();
7ade55ad 159 $log_action->module = 'course';
160 $log_action->action = 'unenrol';
161 $log_action->mtable = 'course';
162 $log_action->field = 'fullname';
910dd4dd 163 if (!$DB->record_exists('log_display', array('action'=>'unenrol', 'module'=>'course'))) {
164 $result = $result && $DB->insert_record('log_display', $log_action);
7ade55ad 165 }
166 upgrade_main_savepoint($result, 2008051202);
167 }
168
fa162144 169 if ($result && $oldversion < 2008051203) {
170 $table = new xmldb_table('mnet_enrol_course');
2a88f626 171 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
eee5d9bb 172 $dbman->change_field_precision($table, $field);
fa162144 173 upgrade_main_savepoint($result, 2008051203);
174 }
175
38fb8190 176 if ($result && $oldversion < 2008063001) {
775f811a 177 upgrade_set_timeout(60*20); // this may take a while
178
38fb8190 179 // table to be modified
180 $table = new xmldb_table('tag_instance');
181 // add field
182 $field = new xmldb_field('tiuserid');
183 if (!$dbman->field_exists($table, $field)) {
2a88f626 184 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
38fb8190 185 $dbman->add_field($table, $field);
186 }
187 // modify index
188 $index = new xmldb_index('itemtype-itemid-tagid');
189 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
b91de8a5 190 if ($dbman->index_exists($table, $index)) {
191 $dbman->drop_index($table, $index);
192 }
38fb8190 193 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
194 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
b91de8a5 195 if (!$dbman->index_exists($table, $index)) {
196 $dbman->add_index($table, $index);
197 }
38fb8190 198
199 /// Main savepoint reached
200 upgrade_main_savepoint($result, 2008063001);
201 }
38fb8190 202
a036d7f3 203 if ($result && $oldversion < 2008070300) {
204 $result = $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
205 upgrade_main_savepoint($result, 2008070300);
206 }
4e781c7b 207
fe074586 208 if ($result && $oldversion < 2008070700) {
209 if (isset($CFG->defaultuserroleid) and isset($CFG->guestroleid) and $CFG->defaultuserroleid == $CFG->guestroleid) {
210 // guest can not be selected in defaultuserroleid!
211 unset_config('defaultuserroleid');
212 }
213 upgrade_main_savepoint($result, 2008070700);
214 }
215
ac5efef6 216 if ($result && $oldversion < 2008070701) {
217
218 /// Define table portfolio_instance to be created
219 $table = new xmldb_table('portfolio_instance');
220
221 /// Adding fields to table portfolio_instance
2a88f626 222 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
223 $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
224 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
225 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
ac5efef6 226
227 /// Adding keys to table portfolio_instance
228 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
229
230 /// Conditionally launch create table for portfolio_instance
231 if (!$dbman->table_exists($table)) {
232 $dbman->create_table($table);
233 }
234 /// Define table portfolio_instance_config to be created
235 $table = new xmldb_table('portfolio_instance_config');
236
237 /// Adding fields to table portfolio_instance_config
2a88f626 238 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
239 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
240 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
241 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
ac5efef6 242
243 /// Adding keys to table portfolio_instance_config
244 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
245 $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
246
247 /// Adding indexes to table portfolio_instance_config
248 $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
249
250 /// Conditionally launch create table for portfolio_instance_config
251 if (!$dbman->table_exists($table)) {
252 $dbman->create_table($table);
253 }
254
255 /// Define table portfolio_instance_user to be created
256 $table = new xmldb_table('portfolio_instance_user');
257
258 /// Adding fields to table portfolio_instance_user
2a88f626 259 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
260 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
261 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
262 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
263 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
ac5efef6 264
265 /// Adding keys to table portfolio_instance_user
266 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
267 $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
268 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
269
270 /// Conditionally launch create table for portfolio_instance_user
271 if (!$dbman->table_exists($table)) {
272 $dbman->create_table($table);
273 }
274
275 /// Main savepoint reached
276 upgrade_main_savepoint($result, 2008070701);
277 }
172dd12c 278
775f811a 279 if ($result && $oldversion < 2008072400) {
280 /// Create the database tables for message_processors
f65b2dae 281 $table = new xmldb_table('message_processors');
2a88f626 282 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
283 $table->add_field('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null);
f65b2dae 284 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3b120e46 285 $dbman->create_table($table);
286
3b120e46 287 /// delete old and create new fields
f65b2dae 288 $table = new xmldb_table('message');
289 $field = new xmldb_field('messagetype');
3b120e46 290 $dbman->drop_field($table, $field);
291
292 /// fields to rename
f65b2dae 293 $field = new xmldb_field('message');
2a88f626 294 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 295 $dbman->rename_field($table, $field, 'fullmessage');
f65b2dae 296 $field = new xmldb_field('format');
2a88f626 297 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
3b120e46 298 $dbman->rename_field($table, $field, 'fullmessageformat');
299
300 /// new message fields
f65b2dae 301 $field = new xmldb_field('subject');
2a88f626 302 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 303 $dbman->add_field($table, $field);
f65b2dae 304 $field = new xmldb_field('fullmessagehtml');
2a88f626 305 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
3b120e46 306 $dbman->add_field($table, $field);
f65b2dae 307 $field = new xmldb_field('smallmessage');
2a88f626 308 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 309 $dbman->add_field($table, $field);
310
311
f65b2dae 312 $table = new xmldb_table('message_read');
313 $field = new xmldb_field('messagetype');
3b120e46 314 $dbman->drop_field($table, $field);
f65b2dae 315 $field = new xmldb_field('mailed');
3b120e46 316 $dbman->drop_field($table, $field);
317
318 /// fields to rename
f65b2dae 319 $field = new xmldb_field('message');
2a88f626 320 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 321 $dbman->rename_field($table, $field, 'fullmessage');
f65b2dae 322 $field = new xmldb_field('format');
2a88f626 323 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
3b120e46 324 $dbman->rename_field($table, $field, 'fullmessageformat');
325
326
327 /// new message fields
f65b2dae 328 $field = new xmldb_field('subject');
2a88f626 329 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 330 $dbman->add_field($table, $field);
f65b2dae 331 $field = new xmldb_field('fullmessagehtml');
2a88f626 332 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
3b120e46 333 $dbman->add_field($table, $field);
f65b2dae 334 $field = new xmldb_field('smallmessage');
2a88f626 335 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
3b120e46 336 $dbman->add_field($table, $field);
337
338 /// new table
f65b2dae 339 $table = new xmldb_table('message_working');
2a88f626 340 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
341 $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
342 $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
f65b2dae 343 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3b120e46 344 $dbman->create_table($table);
345
346
347 upgrade_main_savepoint($result, 2008072400);
348 }
349
4e781c7b 350 if ($result && $oldversion < 2008072800) {
351
352 /// Define field enablecompletion to be added to course
353 $table = new xmldb_table('course');
354 $field = new xmldb_field('enablecompletion');
2a88f626 355 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
4e781c7b 356
357 /// Launch add field enablecompletion
775f811a 358 if (!$dbman->field_exists($table,$field)) {
4e781c7b 359 $dbman->add_field($table, $field);
360 }
775f811a 361
4e781c7b 362 /// Define field completion to be added to course_modules
363 $table = new xmldb_table('course_modules');
364 $field = new xmldb_field('completion');
2a88f626 365 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'groupmembersonly');
4e781c7b 366
367 /// Launch add field completion
775f811a 368 if (!$dbman->field_exists($table,$field)) {
4e781c7b 369 $dbman->add_field($table, $field);
370 }
371
372 /// Define field completiongradeitemnumber to be added to course_modules
373 $field = new xmldb_field('completiongradeitemnumber');
2a88f626 374 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'completion');
4e781c7b 375
376 /// Launch add field completiongradeitemnumber
775f811a 377 if (!$dbman->field_exists($table,$field)) {
4e781c7b 378 $dbman->add_field($table, $field);
379 }
380
381 /// Define field completionview to be added to course_modules
382 $field = new xmldb_field('completionview');
2a88f626 383 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiongradeitemnumber');
4e781c7b 384
385 /// Launch add field completionview
775f811a 386 if (!$dbman->field_exists($table,$field)) {
4e781c7b 387 $dbman->add_field($table, $field);
388 }
389
390 /// Define field completionexpected to be added to course_modules
391 $field = new xmldb_field('completionexpected');
2a88f626 392 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionview');
4e781c7b 393
394 /// Launch add field completionexpected
775f811a 395 if (!$dbman->field_exists($table,$field)) {
4e781c7b 396 $dbman->add_field($table, $field);
397 }
398
399 /// Define table course_modules_completion to be created
400 $table = new xmldb_table('course_modules_completion');
775f811a 401 if (!$dbman->table_exists($table)) {
4e781c7b 402
403 /// Adding fields to table course_modules_completion
2a88f626 404 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
405 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
406 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
407 $table->add_field('completionstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
408 $table->add_field('viewed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
409 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
775f811a 410
4e781c7b 411 /// Adding keys to table course_modules_completion
1e0c56ea 412 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
775f811a 413
4e781c7b 414 /// Adding indexes to table course_modules_completion
1e0c56ea 415 $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
416 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
775f811a 417
4e781c7b 418 /// Launch create table for course_modules_completion
419 $dbman->create_table($table);
420 }
421
422 /// Main savepoint reached
423 upgrade_main_savepoint($result, 2008072800);
424 }
775f811a 425
4ab16a09 426 if ($result && $oldversion < 2008073000) {
ac5efef6 427
4ab16a09 428 /// Define table portfolio_log to be created
429 $table = new xmldb_table('portfolio_log');
430
431 /// Adding fields to table portfolio_log
2a88f626 432 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
433 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
434 $table->add_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
435 $table->add_field('portfolio', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
436 $table->add_field('caller_class', XMLDB_TYPE_CHAR, '150', null, XMLDB_NOTNULL, null, null);
437 $table->add_field('caller_file', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
438 $table->add_field('caller_sha1', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
4ab16a09 439
440 /// Adding keys to table portfolio_log
441 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
442 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
443 $table->add_key('portfoliofk', XMLDB_KEY_FOREIGN, array('portfolio'), 'portfolio_instance', array('id'));
444
445 /// Conditionally launch create table for portfolio_log
446 if (!$dbman->table_exists($table)) {
447 $dbman->create_table($table);
448 }
449
450 /// Main savepoint reached
451 upgrade_main_savepoint($result, 2008073000);
452 }
120b3758 453
454 if ($result && $oldversion < 2008073104) {
455 /// Drop old table that might exist for some people
456 $table = new xmldb_table('message_providers');
457 if ($dbman->table_exists($table)) {
458 $dbman->drop_table($table);
459 }
460
461 /// Define table message_providers to be created
462 $table = new xmldb_table('message_providers');
463
464 /// Adding fields to table message_providers
2a88f626 465 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
466 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
467 $table->add_field('component', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
468 $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
120b3758 469
470 /// Adding keys to table message_providers
471 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
472
473 /// Adding indexes to table message_providers
474 $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
475
476 /// Create table for message_providers
477 $dbman->create_table($table);
478
479 upgrade_main_savepoint($result, 2008073104);
480 }
172dd12c 481
482 if ($result && $oldversion < 2008073111) {
483 /// Define table files to be created
484 $table = new xmldb_table('files');
485
486 /// Adding fields to table files
2a88f626 487 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
488 $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
489 $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
490 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
491 $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
492 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
493 $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
494 $table->add_field('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
495 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
496 $table->add_field('filesize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
497 $table->add_field('mimetype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
498 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
499 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
500 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
172dd12c 501
502 /// Adding keys to table files
503 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
504 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
505 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
506
507 /// Adding indexes to table files
508 $table->add_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid'));
509 $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
510 $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
511
512 /// Conditionally launch create table for files
513 $dbman->create_table($table);
514
515 /// Main savepoint reached
516 upgrade_main_savepoint($result, 2008073111);
517 }
518
519 if ($result && $oldversion < 2008073112) {
520 /// Define table files_cleanup to be created
521 $table = new xmldb_table('files_cleanup');
522
523 /// Adding fields to table files_cleanup
2a88f626 524 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
525 $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
172dd12c 526
527 /// Adding keys to table files_cleanup
528 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
529
530 /// Adding indexes to table files_cleanup
531 $table->add_index('contenthash', XMLDB_INDEX_UNIQUE, array('contenthash'));
532
533 /// Conditionally launch create table for files_cleanup
534 $dbman->create_table($table);
535
536 /// Main savepoint reached
537 upgrade_main_savepoint($result, 2008073112);
538 }
539
540 if ($result && $oldversion < 2008073113) {
541 /// move all course, backup and other files to new filepool based storage
542 upgrade_migrate_files_courses();
543 /// Main savepoint reached
544 upgrade_main_savepoint($result, 2008073113);
545 }
546
547 if ($result && $oldversion < 2008073114) {
548 /// move all course, backup and other files to new filepool based storage
549 upgrade_migrate_files_blog();
550 /// Main savepoint reached
551 upgrade_main_savepoint($result, 2008073114);
552 }
f33e1ed4 553
1ce2da58 554 if ($result && $oldversion < 2008080400) {
555 // Add field ssl_jump_url to mnet application, and populate existing default applications
556 $table = new xmldb_table('mnet_application');
557 $field = new xmldb_field('sso_jump_url');
558 if (!$dbman->field_exists($table, $field)) {
2a88f626 559 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
c5a04d12 560 $dbman->add_field($table, $field);
561 $result = $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
1ce2da58 562 $result = $result && $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
563 }
564
565 /// Main savepoint reached
566 upgrade_main_savepoint($result, 2008080400);
567 }
04f35360 568
569 if ($result && $oldversion < 2008080500) {
570
571 /// Define table portfolio_tempdata to be created
572 $table = new xmldb_table('portfolio_tempdata');
573
574 /// Adding fields to table portfolio_tempdata
2a88f626 575 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
576 $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
04f35360 577
578 /// Adding keys to table portfolio_tempdata
579 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
580
581 /// Conditionally launch create table for portfolio_tempdata
582 if (!$dbman->table_exists($table)) {
583 $dbman->create_table($table);
584 }
585
586 /// Main savepoint reached
587 upgrade_main_savepoint($result, 2008080500);
588 }
589
84a44985 590 if ($result && $oldversion < 2008080600) {
591
592 $DB->delete_records('portfolio_tempdata'); // there shouldnt' be any, and it will cause problems with this upgrade.
593 /// Define field expirytime to be added to portfolio_tempdata
594 $table = new xmldb_table('portfolio_tempdata');
2a88f626 595 $field = new xmldb_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'data');
84a44985 596
597 /// Conditionally launch add field expirytime
598 if (!$dbman->field_exists($table, $field)) {
599 $dbman->add_field($table, $field);
600 }
601
602 /// Main savepoint reached
603 upgrade_main_savepoint($result, 2008080600);
604 }
775f811a 605
f9a2cf86 606/// Changing the type of all the columns that the question bank uses to store grades to be NUMBER(12, 7).
607 if ($result && $oldversion < 2008081500) {
608 $table = new xmldb_table('question');
2a88f626 609 $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'generalfeedback');
f9a2cf86 610 $dbman->change_field_type($table, $field);
611 upgrade_main_savepoint($result, 2008081500);
612 }
613
614 if ($result && $oldversion < 2008081501) {
615 $table = new xmldb_table('question');
2a88f626 616 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'defaultgrade');
f9a2cf86 617 $dbman->change_field_type($table, $field);
618 upgrade_main_savepoint($result, 2008081501);
619 }
620
621 if ($result && $oldversion < 2008081502) {
622 $table = new xmldb_table('question_answers');
2a88f626 623 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'answer');
f9a2cf86 624 $dbman->change_field_type($table, $field);
625 upgrade_main_savepoint($result, 2008081502);
626 }
627
628 if ($result && $oldversion < 2008081503) {
629 $table = new xmldb_table('question_sessions');
2a88f626 630 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'newgraded');
f9a2cf86 631 $dbman->change_field_type($table, $field);
632 upgrade_main_savepoint($result, 2008081503);
633 }
634
635 if ($result && $oldversion < 2008081504) {
636 $table = new xmldb_table('question_states');
2a88f626 637 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'event');
f9a2cf86 638 $dbman->change_field_type($table, $field);
639 upgrade_main_savepoint($result, 2008081504);
640 }
641
642 if ($result && $oldversion < 2008081505) {
643 $table = new xmldb_table('question_states');
2a88f626 644 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'grade');
f9a2cf86 645 $dbman->change_field_type($table, $field);
646 upgrade_main_savepoint($result, 2008081505);
647 }
648
649 if ($result && $oldversion < 2008081506) {
650 $table = new xmldb_table('question_states');
2a88f626 651 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'raw_grade');
f9a2cf86 652 $dbman->change_field_type($table, $field);
653 upgrade_main_savepoint($result, 2008081506);
654 }
655
775f811a 656 if ($result && $oldversion < 2008081600) {
657
658 /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
659 unset_config('unicodedb');
660
661 /// Main savepoint reached
662 upgrade_main_savepoint($result, 2008081600);
663 }
664
ff2f69bc 665 if ($result && $oldversion < 2008081900) {
666 /// Define field userid to be added to portfolio_tempdata
667 $table = new xmldb_table('portfolio_tempdata');
2a88f626 668 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'expirytime');
ff2f69bc 669
670 /// Conditionally launch add field userid
671 if (!$dbman->field_exists($table, $field)) {
672 $dbman->add_field($table, $field);
673 }
674 $DB->set_field('portfolio_tempdata', 'userid', 0);
675 /// now change it to be notnull
676
677 /// Changing nullability of field userid on table portfolio_tempdata to not null
2a88f626 678 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'expirytime');
ff2f69bc 679
680 /// Launch change of nullability for field userid
681 $dbman->change_field_notnull($table, $field);
682
683 /// Define key userfk (foreign) to be added to portfolio_tempdata
684 $table = new xmldb_table('portfolio_tempdata');
685 $key = new xmldb_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
686
687 /// Launch add key userfk
688 $dbman->add_key($table, $key);
689
690 upgrade_main_savepoint($result, 2008081900);
691 }
8f3f6cc3 692 if ($result && $oldversion < 2008082602) {
27051e43 693
694 /// Define table repository to be dropped
695 $table = new xmldb_table('repository');
696
697 /// Conditionally launch drop table for repository
698 if ($dbman->table_exists($table)) {
699 $dbman->drop_table($table);
700 }
701
702 /// Define table repository to be created
703 $table = new xmldb_table('repository');
704
705 /// Adding fields to table repository
2a88f626 706 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
707 $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
708 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1');
709 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
27051e43 710
711 /// Adding keys to table repository
712 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
713
714 /// Conditionally launch create table for repository
715 if (!$dbman->table_exists($table)) {
716 $dbman->create_table($table);
717 }
718 /// Define table repository_instances to be created
719 $table = new xmldb_table('repository_instances');
720
721 /// Adding fields to table repository_instances
2a88f626 722 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
723 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
724 $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
725 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
726 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
727 $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null);
728 $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
729 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
730 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
27051e43 731
732 /// Adding keys to table repository_instances
733 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
734
735 /// Conditionally launch create table for repository_instances
736 if (!$dbman->table_exists($table)) {
737 $dbman->create_table($table);
738 }
739
740 /// Define table repository_instance_config to be created
741 $table = new xmldb_table('repository_instance_config');
742
743 /// Adding fields to table repository_instance_config
2a88f626 744 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
745 $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
746 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
747 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
27051e43 748
749 /// Adding keys to table repository_instance_config
750 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
751
752 /// Conditionally launch create table for repository_instance_config
753 if (!$dbman->table_exists($table)) {
754 $dbman->create_table($table);
755 }
756
757 /// Main savepoint reached
8f3f6cc3 758 upgrade_main_savepoint($result, 2008082602);
27051e43 759 }
760
62e76c67 761 if ($result && $oldversion < 2008082700) {
762 /// Add a new column to the question sessions table to record whether a
763 /// question has been flagged.
764
765 /// Define field flagged to be added to question_sessions
766 $table = new xmldb_table('question_sessions');
2a88f626 767 $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
62e76c67 768
769 /// Conditionally launch add field flagged
770 if (!$dbman->field_exists($table, $field)) {
771 $dbman->add_field($table, $field);
772 }
773
774 /// Main savepoint reached
775 upgrade_main_savepoint($result, 2008082700);
776 }
ff2f69bc 777
2c48aeab 778 if ($result && $oldversion < 2008082900) {
779
780 /// Changing precision of field parent_type on table mnet_rpc to (20)
781 $table = new xmldb_table('mnet_rpc');
2a88f626 782 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2c48aeab 783
784 /// Launch change of precision for field parent_type
785 $dbman->change_field_precision($table, $field);
786
787 /// Main savepoint reached
788 upgrade_main_savepoint($result, 2008082900);
789 }
790
ff7e7f92 791 if ($result && $oldversion < 2008090108) {
792 $repo = new object();
793 $repo->type = 'upload';
794 $repo->visible = 1;
795 $repo->sortorder = 1;
796 if (!$DB->record_exists('repository', array('type'=>'upload'))) {
797 $typeid = $DB->insert_record('repository', $repo);
798 }else{
799 $record = $DB->get_record('repository', array('type'=>'upload'));
800 $typeid = $record->id;
801 }
802 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
803 $instance = new object();
fb7cae3b 804 $instance->name = get_string('repositoryname', 'repository_upload');
ff7e7f92 805 $instance->typeid = $typeid;
806 $instance->userid = 0;
807 $instance->contextid = SITEID;
808 $instance->timecreated = time();
809 $instance->timemodified = time();
810 $result = $result && $DB->insert_record('repository_instances', $instance);
811 }
812 $repo->type = 'local';
813 $repo->visible = 1;
814 $repo->sortorder = 1;
815 if (!$DB->record_exists('repository', array('type'=>'local'))) {
816 $typeid = $DB->insert_record('repository', $repo);
817 }else{
818 $record = $DB->get_record('repository', array('type'=>'local'));
819 $typeid = $record->id;
820 }
821 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
822 $instance = new object();
fb7cae3b 823 $instance->name = get_string('repositoryname', 'repository_local');
ff7e7f92 824 $instance->typeid = $typeid;
825 $instance->userid = 0;
826 $instance->contextid = SITEID;
827 $instance->timecreated = time();
828 $instance->timemodified = time();
829 $result = $result && $DB->insert_record('repository_instances', $instance);
830 }
831
832 upgrade_main_savepoint($result, 2008090108);
833 }
834
d849880e 835 // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
836 if ($result && $oldversion < 2008091000) {
837 foreach (get_object_vars($CFG) as $name => $value) {
838 if (substr($name, strlen($name) - 8) !== '_version') {
839 continue;
840 }
841 $pluginname = substr($name, 0, strlen($name) - 8);
842 if (!strpos($pluginname, '_')) {
843 // Skip things like backup_version that don't contain an extra _
844 continue;
845 }
846 if ($pluginname == 'enrol_ldap_version') {
847 // Special case - this is something different from a plugin version number.
848 continue;
849 }
6f49dd0f 850 if (!preg_match('/^\d{10}$/', $value)) {
d849880e 851 // Extra safety check, skip anything that does not look like a Moodle
852 // version number (10 digits).
853 continue;
854 }
855 $result = $result && set_config('version', $value, $pluginname);
856 $result = $result && unset_config($name);
857 }
858 upgrade_main_savepoint($result, 2008091000);
859 }
860
948c2860 861 //Add a readonly field to the repository_instances table
862 //in order to support instance created automatically by a repository plugin
863 if ($result && $oldversion < 2008091611) {
864
865 /// Define field readonly to be added to repository_instances
866 $table = new xmldb_table('repository_instances');
2a88f626 867 $field = new xmldb_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
948c2860 868
869 /// Conditionally launch add field readonly
870 if (!$dbman->field_exists($table, $field)) {
871 $dbman->add_field($table, $field);
872 }
873
874 /// Main savepoint reached
875 upgrade_main_savepoint($result, 2008091611);
876 }
877
53b20fe3 878 if ($result && $oldversion < 2008092300) {
879 unset_config('editorspelling');
880 unset_config('editordictionary');
881 /// Main savepoint reached
882 upgrade_main_savepoint($result, 2008092300);
883 }
884
a7df430b 885 if ($result && $oldversion < 2008101000) {
886
887 /// Changing the default of field lang on table user to en_utf8
888 $table = new xmldb_table('user');
2a88f626 889 $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en_utf8', 'country');
a7df430b 890
891 /// Launch change of default for field lang
892 $dbman->change_field_default($table, $field);
893
894 /// Main savepoint reached
895 upgrade_main_savepoint($result, 2008101000);
896 }
897
5e7206a8 898 if ($result && $oldversion < 2008101300) {
899
d9e673c1 900 if (!get_config(NULL, 'statsruntimedays')) {
5e7206a8 901 set_config('statsruntimedays', '31');
902 }
903
904 /// Main savepoint reached
905 upgrade_main_savepoint($result, 2008101300);
906 }
53b20fe3 907
01a80f51 908 /// New table for storing which roles can be assigned in which contexts.
909 if ($result && $oldversion < 2008110601) {
910
911 /// Define table role_context_levels to be created
912 $table = new xmldb_table('role_context_levels');
913
914 /// Adding fields to table role_context_levels
2a88f626 915 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
916 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
917 $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01a80f51 918
919 /// Adding keys to table role_context_levels
920 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
921 $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
922 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
923
924 /// Conditionally launch create table for role_context_levels
925 if (!$dbman->table_exists($table)) {
926 $dbman->create_table($table);
927 }
928
929 /// Main savepoint reached
930 upgrade_main_savepoint($result, 2008110601);
931 }
932
933 /// Now populate the role_context_levels table with the defaults that match
934 /// moodle_install_roles, and any other combinations that exist in this system.
935 if ($result && $oldversion < 2008110602) {
936 $roleids = $DB->get_records_menu('role', array(), '', 'shortname,id');
937
938 /// Defaults, should match moodle_install_roles.
939 $rolecontextlevels = array();
940 if (isset($roleids['admin'])) {
941 $rolecontextlevels[$roleids['admin']] = get_default_contextlevels('admin');
942 }
943 if (isset($roleids['coursecreator'])) {
944 $rolecontextlevels[$roleids['coursecreator']] = get_default_contextlevels('coursecreator');
945 }
946 if (isset($roleids['editingteacher'])) {
947 $rolecontextlevels[$roleids['editingteacher']] = get_default_contextlevels('editingteacher');
948 }
949 if (isset($roleids['teacher'])) {
950 $rolecontextlevels[$roleids['teacher']] = get_default_contextlevels('teacher');
951 }
952 if (isset($roleids['student'])) {
953 $rolecontextlevels[$roleids['student']] = get_default_contextlevels('student');
954 }
955 if (isset($roleids['guest'])) {
956 $rolecontextlevels[$roleids['guest']] = get_default_contextlevels('guest');
957 }
958 if (isset($roleids['user'])) {
959 $rolecontextlevels[$roleids['user']] = get_default_contextlevels('user');
960 }
656be893 961
01a80f51 962 /// See what other role assignments are in this database, extend the allowed
963 /// lists to allow them too.
964 $existingrolecontextlevels = $DB->get_recordset_sql('SELECT DISTINCT ra.roleid, con.contextlevel FROM
965 {role_assignments} ra JOIN {context} con ON ra.contextid = con.id');
966 foreach ($existingrolecontextlevels as $rcl) {
967 if (!isset($rolecontextlevels[$rcl->roleid])) {
968 $rolecontextlevels[$rcl->roleid] = array($rcl->contextlevel);
969 } else if (!in_array($rcl->contextlevel, $rolecontextlevels[$rcl->roleid])) {
970 $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
971 }
972 }
973
974 /// Put the data into the database.
975 foreach ($rolecontextlevels as $roleid => $contextlevels) {
976 set_role_contextlevels($roleid, $contextlevels);
977 }
978
979 /// Main savepoint reached
980 upgrade_main_savepoint($result, 2008110602);
981 }
982
06de498b 983 /// Remove any role overrides for moodle/site:doanything, or any permissions
984 /// for it in a role without legacy:admin.
985 if ($result && $oldversion < 2008110603) {
986 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
987
988 // Remove all overrides.
989 $DB->delete_records_select('role_capabilities', 'capability = ? AND contextid <> ?', array('moodle/site:doanything', $systemcontext->id));
990
991 // Get the ids of all the roles that are moodle/legacy:admin.
992 $adminroleids = $DB->get_records_menu('role_capabilities',
993 array('capability' => 'moodle/legacy:admin', 'permission' => 1, 'contextid' => $systemcontext->id),
994 '', 'id, roleid');
995
996 // Remove moodle/site:doanything from all other roles.
997 list($notroletest, $params) = $DB->get_in_or_equal($adminroleids, SQL_PARAMS_QM, '', false);
998 $DB->delete_records_select('role_capabilities', "roleid $notroletest AND capability = ? AND contextid = ?",
999 array_merge($params, array('moodle/site:doanything', $systemcontext->id)));
1000
1001 // Ensure that for all admin-y roles, the permission for moodle/site:doanything is 1
1002 list($isroletest, $params) = $DB->get_in_or_equal($adminroleids);
1003 $DB->set_field_select('role_capabilities', 'permission', 1,
1004 "roleid $isroletest AND capability = ? AND contextid = ?",
1005 array_merge($params, array('moodle/site:doanything', $systemcontext->id)));
1006
1007 // And for any admin-y roles where moodle/site:doanything is not set, set it.
1008 $doanythingroleids = $DB->get_records_menu('role_capabilities',
1009 array('capability' => 'moodle/site:doanything', 'permission' => 1, 'contextid' => $systemcontext->id),
1010 '', 'id, roleid');
1011 foreach ($adminroleids as $roleid) {
1012 if (!in_array($roleid, $doanythingroleids)) {
1013 $rc = new stdClass;
1014 $rc->contextid = $systemcontext->id;
1015 $rc->roleid = $roleid;
1016 $rc->capability = 'moodle/site:doanything';
1017 $rc->permission = 1;
1018 $rc->timemodified = time();
1019 $DB->insert_record('role_capabilities', $rc);
1020 }
1021 }
1022
1023 /// Main savepoint reached
1024 upgrade_main_savepoint($result, 2008110603);
1025 }
1026
9101efd3 1027 /// Drop the deprecated teacher, teachers, student and students columns from the course table.
1028 if ($result && $oldversion < 2008111200) {
1029 $table = new xmldb_table('course');
1030
1031 /// Conditionally launch drop field teacher
1032 $field = new xmldb_field('teacher');
1033 if ($dbman->field_exists($table, $field)) {
1034 $dbman->drop_field($table, $field);
1035 }
1036
1037 /// Conditionally launch drop field teacher
1038 $field = new xmldb_field('teachers');
1039 if ($dbman->field_exists($table, $field)) {
1040 $dbman->drop_field($table, $field);
1041 }
1042
1043 /// Conditionally launch drop field teacher
1044 $field = new xmldb_field('student');
1045 if ($dbman->field_exists($table, $field)) {
1046 $dbman->drop_field($table, $field);
1047 }
1048
1049 /// Conditionally launch drop field teacher
1050 $field = new xmldb_field('students');
1051 if ($dbman->field_exists($table, $field)) {
1052 $dbman->drop_field($table, $field);
1053 }
1054
1055 /// Main savepoint reached
1056 upgrade_main_savepoint($result, 2008111200);
1057 }
1058
40c792c3 1059/// Add a unique index to the role.name column.
1060 if ($result && $oldversion < 2008111800) {
1061
1062 /// Define index name (unique) to be added to role
1063 $table = new xmldb_table('role');
1064 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
1065
1066 /// Conditionally launch add index name
1067 if (!$dbman->index_exists($table, $index)) {
1068 $dbman->add_index($table, $index);
1069 }
1070
1071 /// Main savepoint reached
1072 upgrade_main_savepoint($result, 2008111800);
1073 }
1074
1075/// Add a unique index to the role.shortname column.
1076 if ($result && $oldversion < 2008111801) {
1077
1078 /// Define index shortname (unique) to be added to role
1079 $table = new xmldb_table('role');
1080 $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
1081
1082 /// Conditionally launch add index shortname
1083 if (!$dbman->index_exists($table, $index)) {
1084 $dbman->add_index($table, $index);
1085 }
1086
1087 /// Main savepoint reached
1088 upgrade_main_savepoint($result, 2008111801);
1089 }
1090
82bd6a5e 1091 if ($result && $oldversion < 2008120700) {
1092
1093 /// Changing precision of field shortname on table course_request to (100)
1094 $table = new xmldb_table('course_request');
2a88f626 1095 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
82bd6a5e 1096
1097 /// Launch change of precision for field shortname
1098 $dbman->change_field_precision($table, $field);
1099
1100 /// Main savepoint reached
1101 upgrade_main_savepoint($result, 2008120700);
1102 }
1103
1104 /// For MDL-17501. Ensure that any role that has moodle/course:update also
1105 /// has moodle/course:visibility.
1106 if ($result && $oldversion < 2008120800) {
1107 /// Get the roles with 'moodle/course:update'.
1108 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
1109 $roles = get_roles_with_capability('moodle/course:update', CAP_ALLOW, $systemcontext);
1110
1111 /// Give those roles 'moodle/course:visibility'.
1112 foreach ($roles as $role) {
1113 assign_capability('moodle/course:visibility', CAP_ALLOW, $role->id, $systemcontext->id);
1114 }
1115
1116 /// Force all sessions to refresh access data.
1117 mark_context_dirty($systemcontext->path);
1118
1119 /// Main savepoint reached
1120 upgrade_main_savepoint($result, 2008120800);
1121 }
1122
1123 if ($result && $oldversion < 2008120801) {
1124
1125 /// Changing precision of field shortname on table mnet_enrol_course to (100)
1126 $table = new xmldb_table('mnet_enrol_course');
2a88f626 1127 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
82bd6a5e 1128
1129 /// Launch change of precision for field shortname
1130 $dbman->change_field_precision($table, $field);
1131
1132 /// Main savepoint reached
1133 upgrade_main_savepoint($result, 2008120801);
1134 }
1135
1136 if ($result && $oldversion < 2008121701) {
0953a4e7 1137
1138 /// Define field availablefrom to be added to course_modules
1139 $table = new xmldb_table('course_modules');
2a88f626 1140 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
0953a4e7 1141
1142 /// Conditionally launch add field availablefrom
1143 if (!$dbman->field_exists($table, $field)) {
1144 $dbman->add_field($table, $field);
1145 }
1146
1147 /// Define field availableuntil to be added to course_modules
2a88f626 1148 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
0953a4e7 1149
1150 /// Conditionally launch add field availableuntil
1151 if (!$dbman->field_exists($table, $field)) {
1152 $dbman->add_field($table, $field);
1153 }
82bd6a5e 1154
0953a4e7 1155 /// Define field showavailability to be added to course_modules
2a88f626 1156 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
0953a4e7 1157
1158 /// Conditionally launch add field showavailability
1159 if (!$dbman->field_exists($table, $field)) {
1160 $dbman->add_field($table, $field);
1161 }
82bd6a5e 1162
0953a4e7 1163 /// Define table course_modules_availability to be created
1164 $table = new xmldb_table('course_modules_availability');
1165
1166 /// Adding fields to table course_modules_availability
2a88f626 1167 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1168 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1169 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1170 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
1171 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1172 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1173 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
0953a4e7 1174
1175 /// Adding keys to table course_modules_availability
1176 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
cbb17d54 1177 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
1178 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
1179 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
0953a4e7 1180
1181 /// Conditionally launch create table for course_modules_availability
1182 if (!$dbman->table_exists($table)) {
1183 $dbman->create_table($table);
1184 }
1185
82bd6a5e 1186 /// Changes to modinfo mean we need to rebuild course cache
a9a0b93d 1187 require_once($CFG->dirroot . '/course/lib.php');
82bd6a5e 1188 rebuild_course_cache(0,true);
9136a60c 1189
82bd6a5e 1190 /// For developer upgrades, turn on the conditional activities and completion
1191 /// features automatically (to gain more testing)
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
1750 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1751 $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1752 $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1753 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1754 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, null);
35550fd6 1755 $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, null);
66b10689 1756 $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, null, null, '0');
1757 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, null, '0');
1758 $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
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
1804 $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, null, null, '0', 'position');
1805 $dbman->rename_field($table, $field, 'defaultweight');
1806
1807 /// Rename field position on table block_instances to defaultregion
1808 $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, null, null, 'pagetype');
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
1817 /// Changing precision of field defaultregion on table block_instances to (16)
1818 $table = new xmldb_table('block_instances');
1819 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, null, null, 'subpagepattern');
1820
1821 /// Launch change of precision for field defaultregion
1822 $dbman->change_field_precision($table, $field);
1823
1824 /// Main savepoint reached
c045e45a 1825 upgrade_main_savepoint($result, 2009050607);
66b10689 1826 }
1827
c045e45a 1828 if ($result && $oldversion < 2009050608) {
66b10689 1829 /// Change regions to the new notation
1830 $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1831 $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1832 $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1833 // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1834 // flex page course format. Hopefully this new value is an adequate alternative.
1835
1836 /// Main savepoint reached
c045e45a 1837 upgrade_main_savepoint($result, 2009050608);
66b10689 1838 }
1839
c045e45a 1840 if ($result && $oldversion < 2009050609) {
66b10689 1841
1842 /// Define key blockname (unique) to be added to block
1843 $table = new xmldb_table('block');
1844 $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1845
1846 /// Launch add key blockname
1847 $dbman->add_key($table, $key);
1848
1849 /// Main savepoint reached
c045e45a 1850 upgrade_main_savepoint($result, 2009050609);
66b10689 1851 }
1852
c045e45a 1853 if ($result && $oldversion < 2009050610) {
66b10689 1854 $table = new xmldb_table('block_instances');
1855
1856 /// Define field blockname to be added to block_instances
1857 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, null, null, 'blockid');
1858 if (!$dbman->field_exists($table, $field)) {
1859 $dbman->add_field($table, $field);
1860 }
1861
1862 /// Define field contextid to be added to block_instances
1863 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null, 'blockname');
1864 if (!$dbman->field_exists($table, $field)) {
1865 $dbman->add_field($table, $field);
1866 }
1867
1868 /// Define field showinsubcontexts to be added to block_instances
1869 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, null, null, 'contextid');
1870 if (!$dbman->field_exists($table, $field)) {
1871 $dbman->add_field($table, $field);
1872 }
1873
1874 /// Define field subpagepattern to be added to block_instances
1875 $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, null, null, 'pagetype');
1876 if (!$dbman->field_exists($table, $field)) {
1877 $dbman->add_field($table, $field);
1878 }
1879
1880 /// Main savepoint reached
c045e45a 1881 upgrade_main_savepoint($result, 2009050610);
66b10689 1882 }
1883
c045e45a 1884 if ($result && $oldversion < 2009050611) {
66b10689 1885 $table = new xmldb_table('block_instances');
1886
1887 /// Fill in blockname from blockid
1888 $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1889
1890 /// Set showinsubcontexts = 0 for all rows.
1891 $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1892
1893 /// Main savepoint reached
c045e45a 1894 upgrade_main_savepoint($result, 2009050611);
66b10689 1895 }
1896
c045e45a 1897 if ($result && $oldversion < 2009050612) {
66b10689 1898
1899 /// Rename field pagetype on table block_instances to pagetypepattern
1900 $table = new xmldb_table('block_instances');
1901 $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, null, 'pageid');
1902
1903 /// Launch rename field pagetype
1904 $dbman->rename_field($table, $field, 'pagetypepattern');
1905
1906 /// Main savepoint reached
c045e45a 1907 upgrade_main_savepoint($result, 2009050612);
66b10689 1908 }
1909
c045e45a 1910 if ($result && $oldversion < 2009050613) {
66b10689 1911 /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1912
1913 /// site-index
1914 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
eb2761f8 1915 $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1916 pagetypepattern = 'site-index',
1917 subpagepattern = NULL
1918 WHERE pagetypepattern = 'site-index'");
66b10689 1919
1920 /// course-view
eb2761f8 1921 $DB->execute("UPDATE {block_instances} SET
1922 contextid = (
1923 SELECT {context}.id
1924 FROM {context}
1925 JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1926 WHERE {course}.id = pageid
1927 ),
1928 pagetypepattern = 'course-view-*',
1929 subpagepattern = NULL
1930 WHERE pagetypepattern = 'course-view'");
66b10689 1931
1932 /// admin
1933 $syscontext = get_context_instance(CONTEXT_SYSTEM);
eb2761f8 1934 $DB->execute("UPDATE {block_instances} SET
1935 contextid = " . $syscontext->id . ",
1936 pagetypepattern = 'admin-*',
1937 subpagepattern = NULL
1938 WHERE pagetypepattern = 'admin'");
66b10689 1939
1940 /// my-index
eb2761f8 1941 $DB->execute("UPDATE {block_instances} SET
1942 contextid = (
1943 SELECT {context}.id
1944 FROM {context}
1945 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1946 WHERE {user}.id = pageid
1947 ),
1948 pagetypepattern = 'my-index',
1949 subpagepattern = NULL
1950 WHERE pagetypepattern = 'my-index'");
66b10689 1951
1952 /// tag-index
eb2761f8 1953 $DB->execute("UPDATE {block_instances} SET
1954 contextid = " . $syscontext->id . ",
1955 pagetypepattern = 'tag-index',
1956 subpagepattern = pageid
1957 WHERE pagetypepattern = 'tag-index'");
66b10689 1958
1959 /// blog-view
eb2761f8 1960 $DB->execute("UPDATE {block_instances} SET
1961 contextid = (
1962 SELECT {context}.id
1963 FROM {context}
1964 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1965 WHERE {user}.id = pageid
1966 ),
1967 pagetypepattern = 'blog-index',
1968 subpagepattern = NULL
1969 WHERE pagetypepattern = 'blog-view'");
66b10689 1970
1971 /// mod-xxx-view
1972 $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1973 foreach ($moduleswithblocks as $modname) {
1974 if (!$dbman->table_exists($modname)) {
1975 continue;
1976 }
eb2761f8 1977 $DB->execute("UPDATE {block_instances} SET
1978 contextid = (
1979 SELECT {context}.id
1980 FROM {context}
1981 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1982 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1983 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1984 WHERE {{$modname}}.id = pageid
1985 ),
1986 pagetypepattern = 'blog-index',
1987 subpagepattern = NULL
1988 WHERE pagetypepattern = 'blog-view'");
66b10689 1989 }
1990
1991 /// Main savepoint reached
c045e45a 1992 upgrade_main_savepoint($result, 2009050613);
66b10689 1993 }
1994
c045e45a 1995 if ($result && $oldversion < 2009050614) {
66b10689 1996 /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
1997 $DB->execute("UPDATE {block_instances} SET contextid = -1 WHERE contextid IS NULL");
1998
1999 /// Main savepoint reached
c045e45a 2000 upgrade_main_savepoint($result, 2009050614);
66b10689 2001 }
2002
c045e45a 2003 if ($result && $oldversion < 2009050615) {
66b10689 2004 $table = new xmldb_table('block_instances');
2005
2006 /// Changing nullability of field blockname on table block_instances to not null
2007 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, null, null, 'id');
2008 $dbman->change_field_notnull($table, $field);
2009
2010 /// Changing nullability of field contextid on table block_instances to not null
2011 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'blockname');
2012 $dbman->change_field_notnull($table, $field);
2013
2014 /// Changing nullability of field showinsubcontexts on table block_instances to not null
2015 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, null, 'contextid');
2016 $dbman->change_field_notnull($table, $field);
2017
2018 /// Main savepoint reached
c045e45a 2019 upgrade_main_savepoint($result, 2009050615);
66b10689 2020 }
2021
c045e45a 2022 if ($result && $oldversion < 2009050616) {
66b10689 2023 /// Add exiting sticky blocks.
2024 $blocks = $DB->get_records('block');
2025 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2026 $newregions = array(
2027 'l' => 'side-pre',
2028 'r' => 'side-post',
2029 'c' => 'course-view-top',
2030 );
2031 $stickyblocks = $DB->get_recordset('block_pinned_old');
2032 foreach ($stickyblocks as $stickyblock) {
2033 $newblock = stdClass;
2034 $newblock->blockname = $blocks[$stickyblock]->name;
2035 $newblock->contextid = $syscontext->id;
2036 $newblock->showinsubcontexts = 1;
2037 switch ($stickyblock->pagetype) {
2038 case 'course-view':
2039 $newblock->pagetypepattern = 'course-view-*';
2040 break;
2041 default:
2042 $newblock->pagetypepattern = $stickyblock->pagetype;
2043 }
2044 $newblock->defaultregion = $newregions[$stickyblock->position];
2045 $newblock->defaultweight = $stickyblock->weight;
2046 $newblock->configdata = $stickyblock->configdata;
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
2060 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
2061 $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null);
2062 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
2063 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null, null, null);
2064 $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, null, null);
2065 $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1');
2066 $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, null, null);
2067 $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null);
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)) {
2111 $dbman->drop_field($table, $field);
2112 }
2113
2114 /// Define field pageid to be dropped from block_instances
2115 $field = new xmldb_field('pageid');
2116 if ($dbman->field_exists($table, $field)) {
2117 $dbman->drop_field($table, $field);
2118 }
2119
2120 /// Define field visible to be dropped from block_instances
2121 $field = new xmldb_field('visible');
2122 if ($dbman->field_exists($table, $field)) {
2123 $dbman->drop_field($table, $field);
2124 }
2125
2126 /// Main savepoint reached
c045e45a 2127 upgrade_main_savepoint($result, 2009050619);
35079f53 2128 }
2129
ecdf5d17 2130 if ($result && $oldversion < 2009051200) {
2131 /// Let's check the status of mandatory mnet_host records, fixing them
2132 /// and moving "orphan" users to default localhost record. MDL-16879
2133 notify('Fixing mnet records, this may take a while...', 'notifysuccess');
2134 upgrade_fix_incorrect_mnethostids();
2135
2136 /// Main savepoint reached
2137 upgrade_main_savepoint($result, 2009051200);
2138 }
2139
a5747cf8 2140
2141 if ($result && $oldversion < 2009051700) {
2142 /// migrate editor settings
2143 if (empty($CFG->htmleditor)) {
2144 set_config('texteditors', 'textarea');
2145 } else {
2146 set_config('texteditors', 'tinymce,textarea');
2147 }
2148
2149 unset_config('htmleditor');
2150 unset_config('defaulthtmleditor');
2151
2152 /// Main savepoint reached
2153 upgrade_main_savepoint($result, 2009051700);
2154 }
2155
ac9b0805 2156 return $result;
4e423cbf 2157}
271e6dec 2158