Updated the HEAD build version to 20090131
[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
a1f27717 33 if ($result && $oldversion < 2008030700) {
775f811a 34 upgrade_set_timeout(60*20); // this may take a while
a1f27717 35
36 /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
a8cb94f6 37 $table = new xmldb_table('grade_letters');
69b80cc2 38 $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
a1f27717 39
40 /// Launch drop index contextid-lowerboundary
eee5d9bb 41 $dbman->drop_index($table, $index);
a1f27717 42
a1f27717 43 /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
a8cb94f6 44 $table = new xmldb_table('grade_letters');
69b80cc2 45 $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
a1f27717 46
47 /// Launch add index contextid-lowerboundary-letter
eee5d9bb 48 $dbman->add_index($table, $index);
a1f27717 49
50 /// Main savepoint reached
0f77b186 51 upgrade_main_savepoint($result, 2008030700);
52 }
a1f27717 53
1ea260d8 54 if ($result && $oldversion < 2008050100) {
55 // Update courses that used weekscss to weeks
a8cb94f6 56 $result = $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
1ea260d8 57 upgrade_main_savepoint($result, 2008050100);
58 }
59
0b51c247 60 if ($result && $oldversion < 2008050200) {
61 // remove unused config options
62 unset_config('statsrolesupgraded');
63 upgrade_main_savepoint($result, 2008050200);
64 }
65
994fbaab 66 if ($result && $oldversion < 2008050700) {
775f811a 67 upgrade_set_timeout(60*20); // this may take a while
68
994fbaab 69 /// Fix minor problem caused by MDL-5482.
70 require_once($CFG->dirroot . '/question/upgrade.php');
71 $result = $result && question_fix_random_question_parents();
72 upgrade_main_savepoint($result, 2008050700);
73 }
74
ab37dc60 75 if ($result && $oldversion < 2008051200) {
76 // if guest role used as default user role unset it and force admin to choose new setting
77 if (!empty($CFG->defaultuserroleid)) {
910dd4dd 78 if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
ab37dc60 79 if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
80 if (isset($guestroles[$role->id])) {
81 set_config('defaultuserroleid', null);
82 notify('Guest role removed from "Default role for all users" setting, please select another role.', 'notifysuccess');
83 }
84 }
85 } else {
86 set_config('defaultuserroleid', null);
87 }
88 }
89 }
90
8b9cfac4 91 if ($result && $oldversion < 2008051201) {
92 notify('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
775f811a 93 upgrade_set_timeout(60*20); // this may take a while
8b9cfac4 94
469dcbcc 95 /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
cbc08f3b 96 $dbfamily = $DB->get_dbfamily();
97 if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
eee5d9bb 98 $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
469dcbcc 99 }
100
8b9cfac4 101 /// Define index idnumber (not unique) to be dropped form user
a8cb94f6 102 $table = new xmldb_table('user');
69b80cc2 103 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
8b9cfac4 104
105 /// Launch drop index idnumber
f33e1ed4 106 if ($dbman->index_exists($table, $index)) {
eee5d9bb 107 $dbman->drop_index($table, $index);
8b9cfac4 108 }
109
110 /// Changing precision of field idnumber on table user to (255)
a8cb94f6 111 $table = new xmldb_table('user');
69b80cc2 112 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'password');
8b9cfac4 113
114 /// Launch change of precision for field idnumber
eee5d9bb 115 $dbman->change_field_precision($table, $field);
8b9cfac4 116
117 /// Launch add index idnumber again
69b80cc2 118 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
eee5d9bb 119 $dbman->add_index($table, $index);
8b9cfac4 120
121 /// Main savepoint reached
122 upgrade_main_savepoint($result, 2008051201);
123 }
124
7ade55ad 125 if ($result && $oldversion < 2008051202) {
910dd4dd 126 $log_action = new object();
7ade55ad 127 $log_action->module = 'course';
128 $log_action->action = 'unenrol';
129 $log_action->mtable = 'course';
130 $log_action->field = 'fullname';
910dd4dd 131 if (!$DB->record_exists('log_display', array('action'=>'unenrol', 'module'=>'course'))) {
132 $result = $result && $DB->insert_record('log_display', $log_action);
7ade55ad 133 }
134 upgrade_main_savepoint($result, 2008051202);
135 }
136
fa162144 137 if ($result && $oldversion < 2008051203) {
138 $table = new xmldb_table('mnet_enrol_course');
139 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', true, true, null, false, false, 0);
eee5d9bb 140 $dbman->change_field_precision($table, $field);
fa162144 141 upgrade_main_savepoint($result, 2008051203);
142 }
143
38fb8190 144 if ($result && $oldversion < 2008063001) {
775f811a 145 upgrade_set_timeout(60*20); // this may take a while
146
38fb8190 147 // table to be modified
148 $table = new xmldb_table('tag_instance');
149 // add field
150 $field = new xmldb_field('tiuserid');
151 if (!$dbman->field_exists($table, $field)) {
152 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0, 'itemid');
153 $dbman->add_field($table, $field);
154 }
155 // modify index
156 $index = new xmldb_index('itemtype-itemid-tagid');
157 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
b91de8a5 158 if ($dbman->index_exists($table, $index)) {
159 $dbman->drop_index($table, $index);
160 }
38fb8190 161 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
162 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
b91de8a5 163 if (!$dbman->index_exists($table, $index)) {
164 $dbman->add_index($table, $index);
165 }
38fb8190 166
167 /// Main savepoint reached
168 upgrade_main_savepoint($result, 2008063001);
169 }
38fb8190 170
a036d7f3 171 if ($result && $oldversion < 2008070300) {
172 $result = $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
173 upgrade_main_savepoint($result, 2008070300);
174 }
4e781c7b 175
fe074586 176 if ($result && $oldversion < 2008070700) {
177 if (isset($CFG->defaultuserroleid) and isset($CFG->guestroleid) and $CFG->defaultuserroleid == $CFG->guestroleid) {
178 // guest can not be selected in defaultuserroleid!
179 unset_config('defaultuserroleid');
180 }
181 upgrade_main_savepoint($result, 2008070700);
182 }
183
ac5efef6 184 if ($result && $oldversion < 2008070701) {
185
186 /// Define table portfolio_instance to be created
187 $table = new xmldb_table('portfolio_instance');
188
189 /// Adding fields to table portfolio_instance
190 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
191 $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, null, null);
192 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
193 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1');
194
195 /// Adding keys to table portfolio_instance
196 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
197
198 /// Conditionally launch create table for portfolio_instance
199 if (!$dbman->table_exists($table)) {
200 $dbman->create_table($table);
201 }
202 /// Define table portfolio_instance_config to be created
203 $table = new xmldb_table('portfolio_instance_config');
204
205 /// Adding fields to table portfolio_instance_config
206 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
207 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
208 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
209 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
210
211 /// Adding keys to table portfolio_instance_config
212 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
213 $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
214
215 /// Adding indexes to table portfolio_instance_config
216 $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
217
218 /// Conditionally launch create table for portfolio_instance_config
219 if (!$dbman->table_exists($table)) {
220 $dbman->create_table($table);
221 }
222
223 /// Define table portfolio_instance_user to be created
224 $table = new xmldb_table('portfolio_instance_user');
225
226 /// Adding fields to table portfolio_instance_user
227 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
228 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
229 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
230 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
231 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
232
233 /// Adding keys to table portfolio_instance_user
234 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
235 $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
236 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
237
238 /// Conditionally launch create table for portfolio_instance_user
239 if (!$dbman->table_exists($table)) {
240 $dbman->create_table($table);
241 }
242
243 /// Main savepoint reached
244 upgrade_main_savepoint($result, 2008070701);
245 }
172dd12c 246
775f811a 247 if ($result && $oldversion < 2008072400) {
248 /// Create the database tables for message_processors
f65b2dae 249 $table = new xmldb_table('message_processors');
250 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
251 $table->add_field('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null, null, null);
252 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3b120e46 253 $dbman->create_table($table);
254
3b120e46 255 /// delete old and create new fields
f65b2dae 256 $table = new xmldb_table('message');
257 $field = new xmldb_field('messagetype');
3b120e46 258 $dbman->drop_field($table, $field);
259
260 /// fields to rename
f65b2dae 261 $field = new xmldb_field('message');
262 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, null);
3b120e46 263 $dbman->rename_field($table, $field, 'fullmessage');
f65b2dae 264 $field = new xmldb_field('format');
265 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, null, null, '0', null);
3b120e46 266 $dbman->rename_field($table, $field, 'fullmessageformat');
267
268 /// new message fields
f65b2dae 269 $field = new xmldb_field('subject');
270 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, null);
3b120e46 271 $dbman->add_field($table, $field);
f65b2dae 272 $field = new xmldb_field('fullmessagehtml');
273 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null, null);
3b120e46 274 $dbman->add_field($table, $field);
f65b2dae 275 $field = new xmldb_field('smallmessage');
276 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, null);
3b120e46 277 $dbman->add_field($table, $field);
278
279
f65b2dae 280 $table = new xmldb_table('message_read');
281 $field = new xmldb_field('messagetype');
3b120e46 282 $dbman->drop_field($table, $field);
f65b2dae 283 $field = new xmldb_field('mailed');
3b120e46 284 $dbman->drop_field($table, $field);
285
286 /// fields to rename
f65b2dae 287 $field = new xmldb_field('message');
288 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, null);
3b120e46 289 $dbman->rename_field($table, $field, 'fullmessage');
f65b2dae 290 $field = new xmldb_field('format');
291 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, null, null, '0', null);
3b120e46 292 $dbman->rename_field($table, $field, 'fullmessageformat');
293
294
295 /// new message fields
f65b2dae 296 $field = new xmldb_field('subject');
297 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, null);
3b120e46 298 $dbman->add_field($table, $field);
f65b2dae 299 $field = new xmldb_field('fullmessagehtml');
300 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null, null, null);
3b120e46 301 $dbman->add_field($table, $field);
f65b2dae 302 $field = new xmldb_field('smallmessage');
303 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, null);
3b120e46 304 $dbman->add_field($table, $field);
305
306 /// new table
f65b2dae 307 $table = new xmldb_table('message_working');
308 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
309 $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
310 $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
311 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3b120e46 312 $dbman->create_table($table);
313
314
315 upgrade_main_savepoint($result, 2008072400);
316 }
317
4e781c7b 318 if ($result && $oldversion < 2008072800) {
319
320 /// Define field enablecompletion to be added to course
321 $table = new xmldb_table('course');
322 $field = new xmldb_field('enablecompletion');
323 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'defaultrole');
324
325 /// Launch add field enablecompletion
775f811a 326 if (!$dbman->field_exists($table,$field)) {
4e781c7b 327 $dbman->add_field($table, $field);
328 }
775f811a 329
4e781c7b 330 /// Define field completion to be added to course_modules
331 $table = new xmldb_table('course_modules');
332 $field = new xmldb_field('completion');
333 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'groupmembersonly');
334
335 /// Launch add field completion
775f811a 336 if (!$dbman->field_exists($table,$field)) {
4e781c7b 337 $dbman->add_field($table, $field);
338 }
339
340 /// Define field completiongradeitemnumber to be added to course_modules
341 $field = new xmldb_field('completiongradeitemnumber');
342 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null, 'completion');
343
344 /// Launch add field completiongradeitemnumber
775f811a 345 if (!$dbman->field_exists($table,$field)) {
4e781c7b 346 $dbman->add_field($table, $field);
347 }
348
349 /// Define field completionview to be added to course_modules
350 $field = new xmldb_field('completionview');
351 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'completiongradeitemnumber');
352
353 /// Launch add field completionview
775f811a 354 if (!$dbman->field_exists($table,$field)) {
4e781c7b 355 $dbman->add_field($table, $field);
356 }
357
358 /// Define field completionexpected to be added to course_modules
359 $field = new xmldb_field('completionexpected');
360 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'completionview');
361
362 /// Launch add field completionexpected
775f811a 363 if (!$dbman->field_exists($table,$field)) {
4e781c7b 364 $dbman->add_field($table, $field);
365 }
366
367 /// Define table course_modules_completion to be created
368 $table = new xmldb_table('course_modules_completion');
775f811a 369 if (!$dbman->table_exists($table)) {
4e781c7b 370
371 /// Adding fields to table course_modules_completion
1e0c56ea 372 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
373 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
374 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
375 $table->add_field('completionstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
376 $table->add_field('viewed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null);
377 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
775f811a 378
4e781c7b 379 /// Adding keys to table course_modules_completion
1e0c56ea 380 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
775f811a 381
4e781c7b 382 /// Adding indexes to table course_modules_completion
1e0c56ea 383 $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
384 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
775f811a 385
4e781c7b 386 /// Launch create table for course_modules_completion
387 $dbman->create_table($table);
388 }
389
390 /// Main savepoint reached
391 upgrade_main_savepoint($result, 2008072800);
392 }
775f811a 393
4ab16a09 394 if ($result && $oldversion < 2008073000) {
ac5efef6 395
4ab16a09 396 /// Define table portfolio_log to be created
397 $table = new xmldb_table('portfolio_log');
398
399 /// Adding fields to table portfolio_log
400 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
401 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
402 $table->add_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
403 $table->add_field('portfolio', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
404 $table->add_field('caller_class', XMLDB_TYPE_CHAR, '150', null, XMLDB_NOTNULL, null, null, null, null);
405 $table->add_field('caller_file', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
406 $table->add_field('caller_sha1', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
407
408 /// Adding keys to table portfolio_log
409 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
410 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
411 $table->add_key('portfoliofk', XMLDB_KEY_FOREIGN, array('portfolio'), 'portfolio_instance', array('id'));
412
413 /// Conditionally launch create table for portfolio_log
414 if (!$dbman->table_exists($table)) {
415 $dbman->create_table($table);
416 }
417
418 /// Main savepoint reached
419 upgrade_main_savepoint($result, 2008073000);
420 }
120b3758 421
422 if ($result && $oldversion < 2008073104) {
423 /// Drop old table that might exist for some people
424 $table = new xmldb_table('message_providers');
425 if ($dbman->table_exists($table)) {
426 $dbman->drop_table($table);
427 }
428
429 /// Define table message_providers to be created
430 $table = new xmldb_table('message_providers');
431
432 /// Adding fields to table message_providers
433 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
434 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, null);
435 $table->add_field('component', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null, null, null);
436 $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
437
438 /// Adding keys to table message_providers
439 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
440
441 /// Adding indexes to table message_providers
442 $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
443
444 /// Create table for message_providers
445 $dbman->create_table($table);
446
447 upgrade_main_savepoint($result, 2008073104);
448 }
172dd12c 449
450 if ($result && $oldversion < 2008073111) {
451 /// Define table files to be created
452 $table = new xmldb_table('files');
453
454 /// Adding fields to table files
455 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
456 $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, null, null);
457 $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, null, null);
458 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
459 $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, null, null);
460 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
461 $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
462 $table->add_field('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
463 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
464 $table->add_field('filesize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
465 $table->add_field('mimetype', XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null);
466 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
467 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
468 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
469
470 /// Adding keys to table files
471 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
472 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
473 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
474
475 /// Adding indexes to table files
476 $table->add_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid'));
477 $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
478 $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
479
480 /// Conditionally launch create table for files
481 $dbman->create_table($table);
482
483 /// Main savepoint reached
484 upgrade_main_savepoint($result, 2008073111);
485 }
486
487 if ($result && $oldversion < 2008073112) {
488 /// Define table files_cleanup to be created
489 $table = new xmldb_table('files_cleanup');
490
491 /// Adding fields to table files_cleanup
492 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
493 $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, null, null);
494
495 /// Adding keys to table files_cleanup
496 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
497
498 /// Adding indexes to table files_cleanup
499 $table->add_index('contenthash', XMLDB_INDEX_UNIQUE, array('contenthash'));
500
501 /// Conditionally launch create table for files_cleanup
502 $dbman->create_table($table);
503
504 /// Main savepoint reached
505 upgrade_main_savepoint($result, 2008073112);
506 }
507
508 if ($result && $oldversion < 2008073113) {
509 /// move all course, backup and other files to new filepool based storage
510 upgrade_migrate_files_courses();
511 /// Main savepoint reached
512 upgrade_main_savepoint($result, 2008073113);
513 }
514
515 if ($result && $oldversion < 2008073114) {
516 /// move all course, backup and other files to new filepool based storage
517 upgrade_migrate_files_blog();
518 /// Main savepoint reached
519 upgrade_main_savepoint($result, 2008073114);
520 }
f33e1ed4 521
1ce2da58 522 if ($result && $oldversion < 2008080400) {
523 // Add field ssl_jump_url to mnet application, and populate existing default applications
524 $table = new xmldb_table('mnet_application');
525 $field = new xmldb_field('sso_jump_url');
526 if (!$dbman->field_exists($table, $field)) {
527 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
c5a04d12 528 $dbman->add_field($table, $field);
529 $result = $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
1ce2da58 530 $result = $result && $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
531 }
532
533 /// Main savepoint reached
534 upgrade_main_savepoint($result, 2008080400);
535 }
04f35360 536
537 if ($result && $oldversion < 2008080500) {
538
539 /// Define table portfolio_tempdata to be created
540 $table = new xmldb_table('portfolio_tempdata');
541
542 /// Adding fields to table portfolio_tempdata
543 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
544 $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
545
546 /// Adding keys to table portfolio_tempdata
547 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
548
549 /// Conditionally launch create table for portfolio_tempdata
550 if (!$dbman->table_exists($table)) {
551 $dbman->create_table($table);
552 }
553
554 /// Main savepoint reached
555 upgrade_main_savepoint($result, 2008080500);
556 }
557
84a44985 558 if ($result && $oldversion < 2008080600) {
559
560 $DB->delete_records('portfolio_tempdata'); // there shouldnt' be any, and it will cause problems with this upgrade.
561 /// Define field expirytime to be added to portfolio_tempdata
562 $table = new xmldb_table('portfolio_tempdata');
563 $field = new xmldb_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'data');
564
565 /// Conditionally launch add field expirytime
566 if (!$dbman->field_exists($table, $field)) {
567 $dbman->add_field($table, $field);
568 }
569
570 /// Main savepoint reached
571 upgrade_main_savepoint($result, 2008080600);
572 }
775f811a 573
f9a2cf86 574/// Changing the type of all the columns that the question bank uses to store grades to be NUMBER(12, 7).
575 if ($result && $oldversion < 2008081500) {
576 $table = new xmldb_table('question');
577 $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, null, null, 'generalfeedback');
578 $dbman->change_field_type($table, $field);
579 upgrade_main_savepoint($result, 2008081500);
580 }
581
582 if ($result && $oldversion < 2008081501) {
583 $table = new xmldb_table('question');
584 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, null, null, 'defaultgrade');
585 $dbman->change_field_type($table, $field);
586 upgrade_main_savepoint($result, 2008081501);
587 }
588
589 if ($result && $oldversion < 2008081502) {
590 $table = new xmldb_table('question_answers');
591 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, null, null, 'answer');
592 $dbman->change_field_type($table, $field);
593 upgrade_main_savepoint($result, 2008081502);
594 }
595
596 if ($result && $oldversion < 2008081503) {
597 $table = new xmldb_table('question_sessions');
598 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, null, null, 'newgraded');
599 $dbman->change_field_type($table, $field);
600 upgrade_main_savepoint($result, 2008081503);
601 }
602
603 if ($result && $oldversion < 2008081504) {
604 $table = new xmldb_table('question_states');
605 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, null, null, 'event');
606 $dbman->change_field_type($table, $field);
607 upgrade_main_savepoint($result, 2008081504);
608 }
609
610 if ($result && $oldversion < 2008081505) {
611 $table = new xmldb_table('question_states');
612 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, null, null, 'grade');
613 $dbman->change_field_type($table, $field);
614 upgrade_main_savepoint($result, 2008081505);
615 }
616
617 if ($result && $oldversion < 2008081506) {
618 $table = new xmldb_table('question_states');
619 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, null, null, 'raw_grade');
620 $dbman->change_field_type($table, $field);
621 upgrade_main_savepoint($result, 2008081506);
622 }
623
775f811a 624 if ($result && $oldversion < 2008081600) {
625
626 /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
627 unset_config('unicodedb');
628
629 /// Main savepoint reached
630 upgrade_main_savepoint($result, 2008081600);
631 }
632
ff2f69bc 633 if ($result && $oldversion < 2008081900) {
634 /// Define field userid to be added to portfolio_tempdata
635 $table = new xmldb_table('portfolio_tempdata');
636 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null, 'expirytime');
637
638 /// Conditionally launch add field userid
639 if (!$dbman->field_exists($table, $field)) {
640 $dbman->add_field($table, $field);
641 }
642 $DB->set_field('portfolio_tempdata', 'userid', 0);
643 /// now change it to be notnull
644
645 /// Changing nullability of field userid on table portfolio_tempdata to not null
646 $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null, 'expirytime');
647
648 /// Launch change of nullability for field userid
649 $dbman->change_field_notnull($table, $field);
650
651 /// Define key userfk (foreign) to be added to portfolio_tempdata
652 $table = new xmldb_table('portfolio_tempdata');
653 $key = new xmldb_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
654
655 /// Launch add key userfk
656 $dbman->add_key($table, $key);
657
658 upgrade_main_savepoint($result, 2008081900);
659 }
8f3f6cc3 660 if ($result && $oldversion < 2008082602) {
27051e43 661
662 /// Define table repository to be dropped
663 $table = new xmldb_table('repository');
664
665 /// Conditionally launch drop table for repository
666 if ($dbman->table_exists($table)) {
667 $dbman->drop_table($table);
668 }
669
670 /// Define table repository to be created
671 $table = new xmldb_table('repository');
672
673 /// Adding fields to table repository
674 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
675 $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
676 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, '1');
677 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
678
679 /// Adding keys to table repository
680 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
681
682 /// Conditionally launch create table for repository
683 if (!$dbman->table_exists($table)) {
684 $dbman->create_table($table);
685 }
686 /// Define table repository_instances to be created
687 $table = new xmldb_table('repository_instances');
688
689 /// Adding fields to table repository_instances
690 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
691 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
692 $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
693 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
694 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
695 $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
696 $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null);
697 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
698 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
699
700 /// Adding keys to table repository_instances
701 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
702
703 /// Conditionally launch create table for repository_instances
704 if (!$dbman->table_exists($table)) {
705 $dbman->create_table($table);
706 }
707
708 /// Define table repository_instance_config to be created
709 $table = new xmldb_table('repository_instance_config');
710
711 /// Adding fields to table repository_instance_config
712 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
713 $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
714 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
715 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
716
717 /// Adding keys to table repository_instance_config
718 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
719
720 /// Conditionally launch create table for repository_instance_config
721 if (!$dbman->table_exists($table)) {
722 $dbman->create_table($table);
723 }
724
725 /// Main savepoint reached
8f3f6cc3 726 upgrade_main_savepoint($result, 2008082602);
27051e43 727 }
728
62e76c67 729 if ($result && $oldversion < 2008082700) {
730 /// Add a new column to the question sessions table to record whether a
731 /// question has been flagged.
732
733 /// Define field flagged to be added to question_sessions
734 $table = new xmldb_table('question_sessions');
735 $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, null, null, '0', 'manualcomment');
736
737 /// Conditionally launch add field flagged
738 if (!$dbman->field_exists($table, $field)) {
739 $dbman->add_field($table, $field);
740 }
741
742 /// Main savepoint reached
743 upgrade_main_savepoint($result, 2008082700);
744 }
ff2f69bc 745
2c48aeab 746 if ($result && $oldversion < 2008082900) {
747
748 /// Changing precision of field parent_type on table mnet_rpc to (20)
749 $table = new xmldb_table('mnet_rpc');
750 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, null, 'xmlrpc_path');
751
752 /// Launch change of precision for field parent_type
753 $dbman->change_field_precision($table, $field);
754
755 /// Main savepoint reached
756 upgrade_main_savepoint($result, 2008082900);
757 }
758
ff7e7f92 759 if ($result && $oldversion < 2008090108) {
760 $repo = new object();
761 $repo->type = 'upload';
762 $repo->visible = 1;
763 $repo->sortorder = 1;
764 if (!$DB->record_exists('repository', array('type'=>'upload'))) {
765 $typeid = $DB->insert_record('repository', $repo);
766 }else{
767 $record = $DB->get_record('repository', array('type'=>'upload'));
768 $typeid = $record->id;
769 }
770 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
771 $instance = new object();
fb7cae3b 772 $instance->name = get_string('repositoryname', 'repository_upload');
ff7e7f92 773 $instance->typeid = $typeid;
774 $instance->userid = 0;
775 $instance->contextid = SITEID;
776 $instance->timecreated = time();
777 $instance->timemodified = time();
778 $result = $result && $DB->insert_record('repository_instances', $instance);
779 }
780 $repo->type = 'local';
781 $repo->visible = 1;
782 $repo->sortorder = 1;
783 if (!$DB->record_exists('repository', array('type'=>'local'))) {
784 $typeid = $DB->insert_record('repository', $repo);
785 }else{
786 $record = $DB->get_record('repository', array('type'=>'local'));
787 $typeid = $record->id;
788 }
789 if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
790 $instance = new object();
fb7cae3b 791 $instance->name = get_string('repositoryname', 'repository_local');
ff7e7f92 792 $instance->typeid = $typeid;
793 $instance->userid = 0;
794 $instance->contextid = SITEID;
795 $instance->timecreated = time();
796 $instance->timemodified = time();
797 $result = $result && $DB->insert_record('repository_instances', $instance);
798 }
799
800 upgrade_main_savepoint($result, 2008090108);
801 }
802
d849880e 803 // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
804 if ($result && $oldversion < 2008091000) {
805 foreach (get_object_vars($CFG) as $name => $value) {
806 if (substr($name, strlen($name) - 8) !== '_version') {
807 continue;
808 }
809 $pluginname = substr($name, 0, strlen($name) - 8);
810 if (!strpos($pluginname, '_')) {
811 // Skip things like backup_version that don't contain an extra _
812 continue;
813 }
814 if ($pluginname == 'enrol_ldap_version') {
815 // Special case - this is something different from a plugin version number.
816 continue;
817 }
6f49dd0f 818 if (!preg_match('/^\d{10}$/', $value)) {
d849880e 819 // Extra safety check, skip anything that does not look like a Moodle
820 // version number (10 digits).
821 continue;
822 }
823 $result = $result && set_config('version', $value, $pluginname);
824 $result = $result && unset_config($name);
825 }
826 upgrade_main_savepoint($result, 2008091000);
827 }
828
948c2860 829 //Add a readonly field to the repository_instances table
830 //in order to support instance created automatically by a repository plugin
831 if ($result && $oldversion < 2008091611) {
832
833 /// Define field readonly to be added to repository_instances
834 $table = new xmldb_table('repository_instances');
835 $field = new xmldb_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'timemodified');
836
837 /// Conditionally launch add field readonly
838 if (!$dbman->field_exists($table, $field)) {
839 $dbman->add_field($table, $field);
840 }
841
842 /// Main savepoint reached
843 upgrade_main_savepoint($result, 2008091611);
844 }
845
53b20fe3 846 if ($result && $oldversion < 2008092300) {
847 unset_config('editorspelling');
848 unset_config('editordictionary');
849 /// Main savepoint reached
850 upgrade_main_savepoint($result, 2008092300);
851 }
852
a7df430b 853 if ($result && $oldversion < 2008101000) {
854
855 /// Changing the default of field lang on table user to en_utf8
856 $table = new xmldb_table('user');
857 $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'en_utf8', 'country');
858
859 /// Launch change of default for field lang
860 $dbman->change_field_default($table, $field);
861
862 /// Main savepoint reached
863 upgrade_main_savepoint($result, 2008101000);
864 }
865
5e7206a8 866 if ($result && $oldversion < 2008101300) {
867
d9e673c1 868 if (!get_config(NULL, 'statsruntimedays')) {
5e7206a8 869 set_config('statsruntimedays', '31');
870 }
871
872 /// Main savepoint reached
873 upgrade_main_savepoint($result, 2008101300);
874 }
53b20fe3 875
01a80f51 876 /// New table for storing which roles can be assigned in which contexts.
877 if ($result && $oldversion < 2008110601) {
878
879 /// Define table role_context_levels to be created
880 $table = new xmldb_table('role_context_levels');
881
882 /// Adding fields to table role_context_levels
883 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
884 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
885 $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
886
887 /// Adding keys to table role_context_levels
888 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
889 $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
890 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
891
892 /// Conditionally launch create table for role_context_levels
893 if (!$dbman->table_exists($table)) {
894 $dbman->create_table($table);
895 }
896
897 /// Main savepoint reached
898 upgrade_main_savepoint($result, 2008110601);
899 }
900
901 /// Now populate the role_context_levels table with the defaults that match
902 /// moodle_install_roles, and any other combinations that exist in this system.
903 if ($result && $oldversion < 2008110602) {
904 $roleids = $DB->get_records_menu('role', array(), '', 'shortname,id');
905
906 /// Defaults, should match moodle_install_roles.
907 $rolecontextlevels = array();
908 if (isset($roleids['admin'])) {
909 $rolecontextlevels[$roleids['admin']] = get_default_contextlevels('admin');
910 }
911 if (isset($roleids['coursecreator'])) {
912 $rolecontextlevels[$roleids['coursecreator']] = get_default_contextlevels('coursecreator');
913 }
914 if (isset($roleids['editingteacher'])) {
915 $rolecontextlevels[$roleids['editingteacher']] = get_default_contextlevels('editingteacher');
916 }
917 if (isset($roleids['teacher'])) {
918 $rolecontextlevels[$roleids['teacher']] = get_default_contextlevels('teacher');
919 }
920 if (isset($roleids['student'])) {
921 $rolecontextlevels[$roleids['student']] = get_default_contextlevels('student');
922 }
923 if (isset($roleids['guest'])) {
924 $rolecontextlevels[$roleids['guest']] = get_default_contextlevels('guest');
925 }
926 if (isset($roleids['user'])) {
927 $rolecontextlevels[$roleids['user']] = get_default_contextlevels('user');
928 }
656be893 929
01a80f51 930 /// See what other role assignments are in this database, extend the allowed
931 /// lists to allow them too.
932 $existingrolecontextlevels = $DB->get_recordset_sql('SELECT DISTINCT ra.roleid, con.contextlevel FROM
933 {role_assignments} ra JOIN {context} con ON ra.contextid = con.id');
934 foreach ($existingrolecontextlevels as $rcl) {
935 if (!isset($rolecontextlevels[$rcl->roleid])) {
936 $rolecontextlevels[$rcl->roleid] = array($rcl->contextlevel);
937 } else if (!in_array($rcl->contextlevel, $rolecontextlevels[$rcl->roleid])) {
938 $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
939 }
940 }
941
942 /// Put the data into the database.
943 foreach ($rolecontextlevels as $roleid => $contextlevels) {
944 set_role_contextlevels($roleid, $contextlevels);
945 }
946
947 /// Main savepoint reached
948 upgrade_main_savepoint($result, 2008110602);
949 }
950
06de498b 951 /// Remove any role overrides for moodle/site:doanything, or any permissions
952 /// for it in a role without legacy:admin.
953 if ($result && $oldversion < 2008110603) {
954 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
955
956 // Remove all overrides.
957 $DB->delete_records_select('role_capabilities', 'capability = ? AND contextid <> ?', array('moodle/site:doanything', $systemcontext->id));
958
959 // Get the ids of all the roles that are moodle/legacy:admin.
960 $adminroleids = $DB->get_records_menu('role_capabilities',
961 array('capability' => 'moodle/legacy:admin', 'permission' => 1, 'contextid' => $systemcontext->id),
962 '', 'id, roleid');
963
964 // Remove moodle/site:doanything from all other roles.
965 list($notroletest, $params) = $DB->get_in_or_equal($adminroleids, SQL_PARAMS_QM, '', false);
966 $DB->delete_records_select('role_capabilities', "roleid $notroletest AND capability = ? AND contextid = ?",
967 array_merge($params, array('moodle/site:doanything', $systemcontext->id)));
968
969 // Ensure that for all admin-y roles, the permission for moodle/site:doanything is 1
970 list($isroletest, $params) = $DB->get_in_or_equal($adminroleids);
971 $DB->set_field_select('role_capabilities', 'permission', 1,
972 "roleid $isroletest AND capability = ? AND contextid = ?",
973 array_merge($params, array('moodle/site:doanything', $systemcontext->id)));
974
975 // And for any admin-y roles where moodle/site:doanything is not set, set it.
976 $doanythingroleids = $DB->get_records_menu('role_capabilities',
977 array('capability' => 'moodle/site:doanything', 'permission' => 1, 'contextid' => $systemcontext->id),
978 '', 'id, roleid');
979 foreach ($adminroleids as $roleid) {
980 if (!in_array($roleid, $doanythingroleids)) {
981 $rc = new stdClass;
982 $rc->contextid = $systemcontext->id;
983 $rc->roleid = $roleid;
984 $rc->capability = 'moodle/site:doanything';
985 $rc->permission = 1;
986 $rc->timemodified = time();
987 $DB->insert_record('role_capabilities', $rc);
988 }
989 }
990
991 /// Main savepoint reached
992 upgrade_main_savepoint($result, 2008110603);
993 }
994
9101efd3 995 /// Drop the deprecated teacher, teachers, student and students columns from the course table.
996 if ($result && $oldversion < 2008111200) {
997 $table = new xmldb_table('course');
998
999 /// Conditionally launch drop field teacher
1000 $field = new xmldb_field('teacher');
1001 if ($dbman->field_exists($table, $field)) {
1002 $dbman->drop_field($table, $field);
1003 }
1004
1005 /// Conditionally launch drop field teacher
1006 $field = new xmldb_field('teachers');
1007 if ($dbman->field_exists($table, $field)) {
1008 $dbman->drop_field($table, $field);
1009 }
1010
1011 /// Conditionally launch drop field teacher
1012 $field = new xmldb_field('student');
1013 if ($dbman->field_exists($table, $field)) {
1014 $dbman->drop_field($table, $field);
1015 }
1016
1017 /// Conditionally launch drop field teacher
1018 $field = new xmldb_field('students');
1019 if ($dbman->field_exists($table, $field)) {
1020 $dbman->drop_field($table, $field);
1021 }
1022
1023 /// Main savepoint reached
1024 upgrade_main_savepoint($result, 2008111200);
1025 }
1026
40c792c3 1027/// Add a unique index to the role.name column.
1028 if ($result && $oldversion < 2008111800) {
1029
1030 /// Define index name (unique) to be added to role
1031 $table = new xmldb_table('role');
1032 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
1033
1034 /// Conditionally launch add index name
1035 if (!$dbman->index_exists($table, $index)) {
1036 $dbman->add_index($table, $index);
1037 }
1038
1039 /// Main savepoint reached
1040 upgrade_main_savepoint($result, 2008111800);
1041 }
1042
1043/// Add a unique index to the role.shortname column.
1044 if ($result && $oldversion < 2008111801) {
1045
1046 /// Define index shortname (unique) to be added to role
1047 $table = new xmldb_table('role');
1048 $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
1049
1050 /// Conditionally launch add index shortname
1051 if (!$dbman->index_exists($table, $index)) {
1052 $dbman->add_index($table, $index);
1053 }
1054
1055 /// Main savepoint reached
1056 upgrade_main_savepoint($result, 2008111801);
1057 }
1058
82bd6a5e 1059 if ($result && $oldversion < 2008120700) {
1060
1061 /// Changing precision of field shortname on table course_request to (100)
1062 $table = new xmldb_table('course_request');
1063 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, null, 'fullname');
1064
1065 /// Launch change of precision for field shortname
1066 $dbman->change_field_precision($table, $field);
1067
1068 /// Main savepoint reached
1069 upgrade_main_savepoint($result, 2008120700);
1070 }
1071
1072 /// For MDL-17501. Ensure that any role that has moodle/course:update also
1073 /// has moodle/course:visibility.
1074 if ($result && $oldversion < 2008120800) {
1075 /// Get the roles with 'moodle/course:update'.
1076 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
1077 $roles = get_roles_with_capability('moodle/course:update', CAP_ALLOW, $systemcontext);
1078
1079 /// Give those roles 'moodle/course:visibility'.
1080 foreach ($roles as $role) {
1081 assign_capability('moodle/course:visibility', CAP_ALLOW, $role->id, $systemcontext->id);
1082 }
1083
1084 /// Force all sessions to refresh access data.
1085 mark_context_dirty($systemcontext->path);
1086
1087 /// Main savepoint reached
1088 upgrade_main_savepoint($result, 2008120800);
1089 }
1090
1091 if ($result && $oldversion < 2008120801) {
1092
1093 /// Changing precision of field shortname on table mnet_enrol_course to (100)
1094 $table = new xmldb_table('mnet_enrol_course');
1095 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, null, 'fullname');
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, 2008120801);
1102 }
1103
1104 if ($result && $oldversion < 2008121701) {
0953a4e7 1105
1106 /// Define field availablefrom to be added to course_modules
1107 $table = new xmldb_table('course_modules');
1108 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'completionexpected');
1109
1110 /// Conditionally launch add field availablefrom
1111 if (!$dbman->field_exists($table, $field)) {
1112 $dbman->add_field($table, $field);
1113 }
1114
1115 /// Define field availableuntil to be added to course_modules
1116 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'availablefrom');
1117
1118 /// Conditionally launch add field availableuntil
1119 if (!$dbman->field_exists($table, $field)) {
1120 $dbman->add_field($table, $field);
1121 }
82bd6a5e 1122
0953a4e7 1123 /// Define field showavailability to be added to course_modules
1124 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'availableuntil');
1125
1126 /// Conditionally launch add field showavailability
1127 if (!$dbman->field_exists($table, $field)) {
1128 $dbman->add_field($table, $field);
1129 }
82bd6a5e 1130
0953a4e7 1131 /// Define table course_modules_availability to be created
1132 $table = new xmldb_table('course_modules_availability');
1133
1134 /// Adding fields to table course_modules_availability
1135 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1136 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1137 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1138 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, null, null);
1139 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null);
1140 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
1141 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, null, null);
1142
1143 /// Adding keys to table course_modules_availability
1144 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
cbb17d54 1145 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
1146 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
1147 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
0953a4e7 1148
1149 /// Conditionally launch create table for course_modules_availability
1150 if (!$dbman->table_exists($table)) {
1151 $dbman->create_table($table);
1152 }
1153
82bd6a5e 1154 /// Changes to modinfo mean we need to rebuild course cache
1155 rebuild_course_cache(0,true);
9136a60c 1156
82bd6a5e 1157 /// For developer upgrades, turn on the conditional activities and completion
1158 /// features automatically (to gain more testing)
1159 if(debugging('',DEBUG_DEVELOPER)) {
1160 set_config('enableavailability',1);
1161 set_config('enablecompletion',1);
9136a60c 1162 }
1163
9d510a2e 1164 /// Main savepoint reached
82bd6a5e 1165 upgrade_main_savepoint($result, 2008121701);
9d510a2e 1166 }
1167
656be893 1168 if ($result && $oldversion < 2009010500) {
1169 /// clean up config table a bit
1170 unset_config('session_error_counter');
1171
1172 /// Main savepoint reached
1173 upgrade_main_savepoint($result, 2009010500);
1174 }
1175
1176 if ($result && $oldversion < 2009010600) {
1177
1178 /// Define field originalquestion to be dropped from question_states
1179 $table = new xmldb_table('question_states');
1180 $field = new xmldb_field('originalquestion');
1181
1182 /// Conditionally launch drop field originalquestion
1183 if ($dbman->field_exists($table, $field)) {
1184 $dbman->drop_field($table, $field);
1185 }
1186
1187 /// Main savepoint reached
1188 upgrade_main_savepoint($result, 2009010600);
1189 }
1190
1191 if ($result && $oldversion < 2009010601) {
1192
1193 /// Changing precision of field ip on table log to (45)
1194 $table = new xmldb_table('log');
1195 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, null, null, 'userid');
1196
1197 /// Launch change of precision for field ip
1198 $dbman->change_field_precision($table, $field);
1199
1200 /// Main savepoint reached
1201 upgrade_main_savepoint($result, 2009010601);
1202 }
1203
1204 if ($result && $oldversion < 2009010602) {
1205
1206 /// Changing precision of field lastip on table user to (45)
1207 $table = new xmldb_table('user');
1208 $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, null, null, 'currentlogin');
1209
1210 /// Launch change of precision for field lastip
1211 $dbman->change_field_precision($table, $field);
1212
1213 /// Main savepoint reached
1214 upgrade_main_savepoint($result, 2009010602);
1215 }
1216
1217 if ($result && $oldversion < 2009010603) {
1218
1219 /// Changing precision of field ip_address on table mnet_host to (45)
1220 $table = new xmldb_table('mnet_host');
1221 $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, null, null, 'wwwroot');
1222
1223 /// Launch change of precision for field ip_address
1224 $dbman->change_field_precision($table, $field);
1225
1226 /// Main savepoint reached
1227 upgrade_main_savepoint($result, 2009010603);
1228 }
1229
1230 if ($result && $oldversion < 2009010604) {
1231
1232 /// Changing precision of field ip on table mnet_log to (45)
1233 $table = new xmldb_table('mnet_log');
1234 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, null, null, 'userid');
1235
1236 /// Launch change of precision for field ip
1237 $dbman->change_field_precision($table, $field);
1238
1239 /// Main savepoint reached
1240 upgrade_main_savepoint($result, 2009010604);
1241 }
1242
4413941f 1243 if ($result && $oldversion < 2009010800) {
1244 /// Update the notifyloginfailures setting.
1245 if ($CFG->notifyloginfailures == 'mainadmin') {
1246 set_config('notifyloginfailures', get_admin()->username);
1247 } else if ($CFG->notifyloginfailures == 'alladmins') {
1248 set_config('notifyloginfailures', '$@ALL@$');
1249 } else {
1250 set_config('notifyloginfailures', '');
1251 }
1252
1253 /// Main savepoint reached
1254 upgrade_main_savepoint($result, 2009010800);
1255 }
1256
ab2eb65c 1257 if ($result && $oldversion < 2009011000) {
1258
1259 /// Changing nullability of field configdata on table block_instance to null
1260 $table = new xmldb_table('block_instance');
1261 $field = new xmldb_field('configdata');
1262 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null, 'visible');
1263
1264 /// Launch change of nullability for field configdata
1265 $dbman->change_field_notnull($table, $field);
1266
1267 /// Main savepoint reached
1268 upgrade_main_savepoint($result, 2009011000);
1269 }
1270
1caea91e 1271 if ($result && $oldversion < 2009011100) {
1272 /// Remove unused settings
1273 unset_config('zip');
1274 unset_config('unzip');
1275 unset_config('adminblocks_initialised');
1276
1277 /// Main savepoint reached
1278 upgrade_main_savepoint($result, 2009011100);
1279 }
1280
eb6a973c 1281 if ($result && $oldversion < 2009011101) {
1282 /// Migrate backup settings to core plugin config table
1283 $configs = $DB->get_records('backup_config');
1284 foreach ($configs as $config) {
1285 set_config($config->name, $config->value, 'backup');
1286 }
1287
1288 /// Define table to be dropped
1289 $table = new xmldb_table('backup_config');
1290
1291 /// Launch drop table for old backup config
1292 $dbman->drop_table($table);
1293
1294 /// Main savepoint reached
1295 upgrade_main_savepoint($result, 2009011101);
1296 }
1297
301bf0b2 1298 if ($result && $oldversion < 2009011303) {
1299
1300 /// Define table config_log to be created
1301 $table = new xmldb_table('config_log');
1302
1303 /// Adding fields to table config_log
1304 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1305 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1306 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1307 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null);
1308 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null, null);
1309 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
1310 $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
1311
1312 /// Adding keys to table config_log
1313 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1314 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1315
1316 /// Adding indexes to table config_log
1317 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1318
1319 /// Launch create table for config_log
1320 $dbman->create_table($table);
1321
1322 /// Main savepoint reached
1323 upgrade_main_savepoint($result, 2009011303);
1324 }
1325
b9fb7103 1326 if ($result && $oldversion < 2009011900) {
3f57bd45 1327
1328 /// Define table sessions2 to be dropped
1329 $table = new xmldb_table('sessions2');
1330
1331 /// Conditionally launch drop table for sessions
1332 if ($dbman->table_exists($table)) {
1333 $dbman->drop_table($table);
1334 }
1335
1336 /// Define table sessions to be dropped
1337 $table = new xmldb_table('sessions');
1338
1339 /// Conditionally launch drop table for sessions
1340 if ($dbman->table_exists($table)) {
1341 $dbman->drop_table($table);
1342 }
1343
1344 /// Define table sessions to be created
1345 $table = new xmldb_table('sessions');
1346
1347 /// Adding fields to table sessions
1348 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1349 $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
1350 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null, null, null);
1351 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1352 $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null);
3f57bd45 1353 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1354 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1355 $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null, null, null);
1356 $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null, null, null);
1357
1358 /// Adding keys to table sessions
1359 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1360 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1361
1362 /// Adding indexes to table sessions
1363 $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1364 $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1365 $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1366 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1367
1368 /// Launch create table for sessions
1369 $dbman->create_table($table);
1370
1371 /// Main savepoint reached
b9fb7103 1372 upgrade_main_savepoint($result, 2009011900);
3f57bd45 1373 }
1374
dc6f76b9 1375 if ($result && $oldversion < 2009012901) {
1f20942c 1376
1377 /// Define table upgrade_log to be created
1378 $table = new xmldb_table('upgrade_log');
1379
1380 /// Adding fields to table upgrade_log
1381 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
1382 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null);
1383 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null);
dc6f76b9 1384 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null);
1f20942c 1385 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
1386 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
1387 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
1388 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1389 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
1390
1391 /// Adding keys to table upgrade_log
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 upgrade_log
1396 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1397 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
1398
1399 /// Conditionally launch create table for upgrade_log
1400 if (!$dbman->table_exists($table)) {
1401 $dbman->create_table($table);
1402 }
1403
1404 /// Main savepoint reached
dc6f76b9 1405 upgrade_main_savepoint($result, 2009012901);
1f20942c 1406 }
1caea91e 1407
ac9b0805 1408 return $result;
4e423cbf 1409}
271e6dec 1410
1411
4e423cbf 1412?>