MDL-20245 grade: increased the size of the user_preferences value column and added...
[moodle.git] / lib / db / upgrade.php
blob733bac4a002dd128e117fe30a4d16679a9325467
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This file keeps track of upgrades to Moodle.
21 * Sometimes, changes between versions involve
22 * alterations to database structures and other
23 * major things that may break installations.
25 * The upgrade function in this file will attempt
26 * to perform all the necessary actions to upgrade
27 * your older installation to the current version.
29 * If there's something it cannot do itself, it
30 * will tell you what you need to do.
32 * The commands in here will all be database-neutral,
33 * using the methods of database_manager class
35 * Please do not forget to use upgrade_set_timeout()
36 * before any action that may take longer time to finish.
38 * @package core
39 * @subpackage admin
40 * @copyright 2006 onwards Martin Dougiamas http://dougiamas.com
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 defined('MOODLE_INTERNAL') || die();
46 /**
48 * @global stdClass $CFG
49 * @global stdClass $USER
50 * @global moodle_database $DB
51 * @global core_renderer $OUTPUT
52 * @param int $oldversion
53 * @return bool always true
55 function xmldb_main_upgrade($oldversion) {
56 global $CFG, $USER, $DB, $OUTPUT;
58 require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
60 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
62 ////////////////////////////////////////
63 ///upgrade supported only from 1.9.x ///
64 ////////////////////////////////////////
66 if ($oldversion < 2008030600) {
67 //NOTE: this table was added much later later in dev cycle, but we need it here, upgrades from pre PR1 not supported
69 /// Define table upgrade_log to be created
70 $table = new xmldb_table('upgrade_log');
72 /// Adding fields to table upgrade_log
73 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
74 $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
75 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
76 $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
77 $table->add_field('targetversion', XMLDB_TYPE_CHAR, '100', null, null, null, null);
78 $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
79 $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
80 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
81 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
82 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
84 /// Adding keys to table upgrade_log
85 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
86 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
88 /// Adding indexes to table upgrade_log
89 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
90 $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
92 /// Create table for upgrade_log
93 $dbman->create_table($table);
95 /// Main savepoint reached
96 upgrade_main_savepoint(true, 2008030600);
99 if ($oldversion < 2008030601) {
100 //NOTE: this table was added much later later in dev cycle, but we need it here, upgrades from pre PR1 not supported
102 /// Define table log_queries to be created
103 $table = new xmldb_table('log_queries');
105 /// Adding fields to table log_queries
106 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
107 $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
108 $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
109 $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
110 $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
111 $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
112 $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
113 $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
114 $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
116 /// Adding keys to table log_queries
117 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
119 /// Conditionally launch create table for log_queries
120 $dbman->create_table($table);
122 /// Main savepoint reached
123 upgrade_main_savepoint(true, 2008030601);
126 if ($oldversion < 2008030602) {
127 @unlink($CFG->cachedir.'/languages');
129 if (file_exists("$CFG->dataroot/lang")) {
130 // rename old lang directory so that the new and old langs do not mix
131 if (rename("$CFG->dataroot/lang", "$CFG->dataroot/oldlang")) {
132 $oldlang = "$CFG->dataroot/oldlang";
133 } else {
134 $oldlang = "$CFG->dataroot/lang";
136 } else {
137 $oldlang = '';
139 // TODO: fetch previously installed languages ("*_utf8") found in $oldlang from moodle.org
140 upgrade_set_timeout(60*20); // this may take a while
143 // TODO: add some info file to $oldlang describing what to do with "$oldlang/*_utf8_local" dirs
146 // Main savepoint reached
147 upgrade_main_savepoint(true, 2008030602);
150 if ($oldversion < 2008030700) {
151 upgrade_set_timeout(60*20); // this may take a while
153 /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
154 $table = new xmldb_table('grade_letters');
155 $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
157 /// Launch drop index contextid-lowerboundary
158 if ($dbman->index_exists($table, $index)) {
159 $dbman->drop_index($table, $index);
162 /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
163 $table = new xmldb_table('grade_letters');
164 $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
166 /// Launch add index contextid-lowerboundary-letter
167 $dbman->add_index($table, $index);
169 /// Main savepoint reached
170 upgrade_main_savepoint(true, 2008030700);
173 if ($oldversion < 2008050100) {
174 // Update courses that used weekscss to weeks
175 $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
176 upgrade_main_savepoint(true, 2008050100);
179 if ($oldversion < 2008050200) {
180 // remove unused config options
181 unset_config('statsrolesupgraded');
182 upgrade_main_savepoint(true, 2008050200);
185 if ($oldversion < 2008050700) {
186 upgrade_set_timeout(60*20); // this may take a while
188 /// Fix minor problem caused by MDL-5482.
189 require_once($CFG->dirroot . '/question/upgrade.php');
190 question_fix_random_question_parents();
191 upgrade_main_savepoint(true, 2008050700);
194 if ($oldversion < 2008051201) {
195 echo $OUTPUT->notification('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
196 upgrade_set_timeout(60*20); // this may take a while
198 /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
199 $dbfamily = $DB->get_dbfamily();
200 if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
201 $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
204 /// Define index idnumber (not unique) to be dropped form user
205 $table = new xmldb_table('user');
206 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
208 /// Launch drop index idnumber
209 if ($dbman->index_exists($table, $index)) {
210 $dbman->drop_index($table, $index);
213 /// Changing precision of field idnumber on table user to (255)
214 $table = new xmldb_table('user');
215 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
217 /// Launch change of precision for field idnumber
218 $dbman->change_field_precision($table, $field);
220 /// Launch add index idnumber again
221 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
222 $dbman->add_index($table, $index);
224 /// Main savepoint reached
225 upgrade_main_savepoint(true, 2008051201);
228 if ($oldversion < 2008051203) {
229 $table = new xmldb_table('mnet_enrol_course');
230 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
231 $dbman->change_field_precision($table, $field);
232 upgrade_main_savepoint(true, 2008051203);
235 if ($oldversion < 2008063001) {
236 upgrade_set_timeout(60*20); // this may take a while
238 // table to be modified
239 $table = new xmldb_table('tag_instance');
240 // add field
241 $field = new xmldb_field('tiuserid');
242 if (!$dbman->field_exists($table, $field)) {
243 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
244 $dbman->add_field($table, $field);
246 // modify index
247 $index = new xmldb_index('itemtype-itemid-tagid');
248 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
249 if ($dbman->index_exists($table, $index)) {
250 $dbman->drop_index($table, $index);
252 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
253 $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
254 if (!$dbman->index_exists($table, $index)) {
255 $dbman->add_index($table, $index);
258 /// Main savepoint reached
259 upgrade_main_savepoint(true, 2008063001);
262 if ($oldversion < 2008070300) {
263 $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
264 upgrade_main_savepoint(true, 2008070300);
267 if ($oldversion < 2008070701) {
269 /// Define table portfolio_instance to be created
270 $table = new xmldb_table('portfolio_instance');
272 /// Adding fields to table portfolio_instance
273 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
274 $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
275 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
276 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
278 /// Adding keys to table portfolio_instance
279 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
281 /// Conditionally launch create table for portfolio_instance
282 if (!$dbman->table_exists($table)) {
283 $dbman->create_table($table);
285 /// Define table portfolio_instance_config to be created
286 $table = new xmldb_table('portfolio_instance_config');
288 /// Adding fields to table portfolio_instance_config
289 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
290 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
291 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
292 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
294 /// Adding keys to table portfolio_instance_config
295 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
296 $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
298 /// Adding indexes to table portfolio_instance_config
299 $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
301 /// Conditionally launch create table for portfolio_instance_config
302 if (!$dbman->table_exists($table)) {
303 $dbman->create_table($table);
306 /// Define table portfolio_instance_user to be created
307 $table = new xmldb_table('portfolio_instance_user');
309 /// Adding fields to table portfolio_instance_user
310 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
311 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
312 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
313 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
314 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
316 /// Adding keys to table portfolio_instance_user
317 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
318 $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
319 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
321 /// Conditionally launch create table for portfolio_instance_user
322 if (!$dbman->table_exists($table)) {
323 $dbman->create_table($table);
326 /// Main savepoint reached
327 upgrade_main_savepoint(true, 2008070701);
330 if ($oldversion < 2008072400) {
331 /// Create the database tables for message_processors
332 $table = new xmldb_table('message_processors');
333 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
334 $table->add_field('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null);
335 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
336 $dbman->create_table($table);
338 /// delete old and create new fields
339 $table = new xmldb_table('message');
340 $field = new xmldb_field('messagetype');
341 $dbman->drop_field($table, $field);
343 /// fields to rename
344 $field = new xmldb_field('message');
345 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
346 $dbman->rename_field($table, $field, 'fullmessage');
347 $field = new xmldb_field('format');
348 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
349 $dbman->rename_field($table, $field, 'fullmessageformat');
351 /// new message fields
352 $field = new xmldb_field('subject');
353 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
354 $dbman->add_field($table, $field);
355 $field = new xmldb_field('fullmessagehtml');
356 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
357 $dbman->add_field($table, $field);
358 $field = new xmldb_field('smallmessage');
359 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
360 $dbman->add_field($table, $field);
363 $table = new xmldb_table('message_read');
364 $field = new xmldb_field('messagetype');
365 $dbman->drop_field($table, $field);
366 $field = new xmldb_field('mailed');
367 $dbman->drop_field($table, $field);
369 /// fields to rename
370 $field = new xmldb_field('message');
371 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
372 $dbman->rename_field($table, $field, 'fullmessage');
373 $field = new xmldb_field('format');
374 $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
375 $dbman->rename_field($table, $field, 'fullmessageformat');
378 /// new message fields
379 $field = new xmldb_field('subject');
380 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
381 $dbman->add_field($table, $field);
382 $field = new xmldb_field('fullmessagehtml');
383 $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
384 $dbman->add_field($table, $field);
385 $field = new xmldb_field('smallmessage');
386 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
387 $dbman->add_field($table, $field);
389 /// new table
390 $table = new xmldb_table('message_working');
391 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
392 $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
393 $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
394 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
395 $dbman->create_table($table);
398 upgrade_main_savepoint(true, 2008072400);
401 if ($oldversion < 2008072800) {
403 /// Define field enablecompletion to be added to course
404 $table = new xmldb_table('course');
405 $field = new xmldb_field('enablecompletion');
406 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
408 /// Launch add field enablecompletion
409 if (!$dbman->field_exists($table,$field)) {
410 $dbman->add_field($table, $field);
413 /// Define field completion to be added to course_modules
414 $table = new xmldb_table('course_modules');
415 $field = new xmldb_field('completion');
416 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'groupmembersonly');
418 /// Launch add field completion
419 if (!$dbman->field_exists($table,$field)) {
420 $dbman->add_field($table, $field);
423 /// Define field completiongradeitemnumber to be added to course_modules
424 $field = new xmldb_field('completiongradeitemnumber');
425 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'completion');
427 /// Launch add field completiongradeitemnumber
428 if (!$dbman->field_exists($table,$field)) {
429 $dbman->add_field($table, $field);
432 /// Define field completionview to be added to course_modules
433 $field = new xmldb_field('completionview');
434 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiongradeitemnumber');
436 /// Launch add field completionview
437 if (!$dbman->field_exists($table,$field)) {
438 $dbman->add_field($table, $field);
441 /// Define field completionexpected to be added to course_modules
442 $field = new xmldb_field('completionexpected');
443 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionview');
445 /// Launch add field completionexpected
446 if (!$dbman->field_exists($table,$field)) {
447 $dbman->add_field($table, $field);
450 /// Define table course_modules_completion to be created
451 $table = new xmldb_table('course_modules_completion');
452 if (!$dbman->table_exists($table)) {
454 /// Adding fields to table course_modules_completion
455 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
456 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
457 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
458 $table->add_field('completionstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
459 $table->add_field('viewed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
460 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
462 /// Adding keys to table course_modules_completion
463 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
465 /// Adding indexes to table course_modules_completion
466 $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
467 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
469 /// Launch create table for course_modules_completion
470 $dbman->create_table($table);
473 /// Main savepoint reached
474 upgrade_main_savepoint(true, 2008072800);
477 if ($oldversion < 2008073000) {
479 /// Define table portfolio_log to be created
480 $table = new xmldb_table('portfolio_log');
482 /// Adding fields to table portfolio_log
483 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
484 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
485 $table->add_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
486 $table->add_field('portfolio', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
487 $table->add_field('caller_class', XMLDB_TYPE_CHAR, '150', null, XMLDB_NOTNULL, null, null);
488 $table->add_field('caller_file', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
489 $table->add_field('caller_sha1', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
490 $table->add_field('tempdataid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
491 $table->add_field('returnurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
492 $table->add_field('continueurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
494 /// Adding keys to table portfolio_log
495 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
496 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
497 $table->add_key('portfoliofk', XMLDB_KEY_FOREIGN, array('portfolio'), 'portfolio_instance', array('id'));
499 /// Conditionally launch create table for portfolio_log
500 if (!$dbman->table_exists($table)) {
501 $dbman->create_table($table);
504 /// Main savepoint reached
505 upgrade_main_savepoint(true, 2008073000);
508 if ($oldversion < 2008073104) {
509 /// Drop old table that might exist for some people
510 $table = new xmldb_table('message_providers');
511 if ($dbman->table_exists($table)) {
512 $dbman->drop_table($table);
515 /// Define table message_providers to be created
516 $table = new xmldb_table('message_providers');
518 /// Adding fields to table message_providers
519 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
520 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
521 $table->add_field('component', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
522 $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
524 /// Adding keys to table message_providers
525 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
527 /// Adding indexes to table message_providers
528 $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
530 /// Create table for message_providers
531 $dbman->create_table($table);
533 upgrade_main_savepoint(true, 2008073104);
536 if ($oldversion < 2008073111) {
537 /// Define table files to be created
538 $table = new xmldb_table('files');
540 /// Adding fields to table files
541 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
542 $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
543 $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
544 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
545 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
546 $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
547 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
548 $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
549 $table->add_field('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
550 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
551 $table->add_field('filesize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
552 $table->add_field('mimetype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
553 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
554 $table->add_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
555 $table->add_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null);
556 $table->add_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null);
557 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
558 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
560 /// Adding keys to table files
561 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
562 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
563 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
565 /// Adding indexes to table files
566 $table->add_index('component-filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('component', 'filearea', 'contextid', 'itemid'));
567 $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
568 $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
570 /// Conditionally launch create table for files
571 $dbman->create_table($table);
573 /// Main savepoint reached
574 upgrade_main_savepoint(true, 2008073111);
577 if ($oldversion < 2008073112) {
578 // Define field legacyfiles to be added to course
579 $table = new xmldb_table('course');
580 $field = new xmldb_field('legacyfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'maxbytes');
582 // Launch add field legacyfiles
583 $dbman->add_field($table, $field);
584 // enable legacy files in all courses
585 $DB->execute("UPDATE {course} SET legacyfiles = 2");
587 // Main savepoint reached
588 upgrade_main_savepoint(true, 2008073112);
591 if ($oldversion < 2008073113) {
592 /// move all course, backup and other files to new filepool based storage
593 upgrade_migrate_files_courses();
594 /// Main savepoint reached
595 upgrade_main_savepoint(true, 2008073113);
598 if ($oldversion < 2008073114) {
599 /// move all course, backup and other files to new filepool based storage
600 upgrade_migrate_files_blog();
601 /// Main savepoint reached
602 upgrade_main_savepoint(true, 2008073114);
605 if ($oldversion < 2008080400) {
606 // Add field ssl_jump_url to mnet application, and populate existing default applications
607 $table = new xmldb_table('mnet_application');
608 $field = new xmldb_field('sso_jump_url');
609 if (!$dbman->field_exists($table, $field)) {
610 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
611 $dbman->add_field($table, $field);
612 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
613 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
616 /// Main savepoint reached
617 upgrade_main_savepoint(true, 2008080400);
620 if ($oldversion < 2008080500) {
622 /// Define table portfolio_tempdata to be created
623 $table = new xmldb_table('portfolio_tempdata');
625 /// Adding fields to table portfolio_tempdata
626 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
627 $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
628 $table->add_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
629 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
630 $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
632 /// Adding keys to table portfolio_tempdata
633 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
634 $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
635 $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
637 /// Conditionally launch create table for portfolio_tempdata
638 if (!$dbman->table_exists($table)) {
639 $dbman->create_table($table);
642 /// Main savepoint reached
643 upgrade_main_savepoint(true, 2008080500);
646 if ($oldversion < 2008081500) {
647 /// Changing the type of all the columns that the question bank uses to store grades to be NUMBER(12, 7).
648 $table = new xmldb_table('question');
649 $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1.0000000', 'generalfeedback');
650 $dbman->change_field_type($table, $field);
651 upgrade_main_savepoint(true, 2008081500);
654 if ($oldversion < 2008081501) {
655 $table = new xmldb_table('question');
656 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0.1000000', 'defaultgrade');
657 $dbman->change_field_type($table, $field);
658 upgrade_main_savepoint(true, 2008081501);
661 if ($oldversion < 2008081502) {
662 $table = new xmldb_table('question_answers');
663 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'answer');
664 $dbman->change_field_type($table, $field);
665 upgrade_main_savepoint(true, 2008081502);
668 if ($oldversion < 2008081503) {
669 $table = new xmldb_table('question_sessions');
670 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'newgraded');
671 $dbman->change_field_type($table, $field);
672 upgrade_main_savepoint(true, 2008081503);
675 if ($oldversion < 2008081504) {
676 $table = new xmldb_table('question_states');
677 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'event');
678 $dbman->change_field_type($table, $field);
679 upgrade_main_savepoint(true, 2008081504);
682 if ($oldversion < 2008081505) {
683 $table = new xmldb_table('question_states');
684 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'grade');
685 $dbman->change_field_type($table, $field);
686 upgrade_main_savepoint(true, 2008081505);
689 if ($oldversion < 2008081506) {
690 $table = new xmldb_table('question_states');
691 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'raw_grade');
692 $dbman->change_field_type($table, $field);
693 upgrade_main_savepoint(true, 2008081506);
696 if ($oldversion < 2008081600) {
698 /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
699 unset_config('unicodedb');
701 /// Main savepoint reached
702 upgrade_main_savepoint(true, 2008081600);
705 if ($oldversion < 2008082602) {
707 /// Define table repository to be dropped
708 $table = new xmldb_table('repository');
710 /// Conditionally launch drop table for repository
711 if ($dbman->table_exists($table)) {
712 $dbman->drop_table($table);
715 /// Define table repository to be created
716 $table = new xmldb_table('repository');
718 /// Adding fields to table repository
719 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
720 $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
721 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1');
722 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
724 /// Adding keys to table repository
725 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
727 /// Conditionally launch create table for repository
728 if (!$dbman->table_exists($table)) {
729 $dbman->create_table($table);
732 /// Define table repository_instances to be created
733 $table = new xmldb_table('repository_instances');
735 /// Adding fields to table repository_instances
736 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
737 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
738 $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
739 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
740 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
741 $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null);
742 $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
743 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
744 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
745 $table->add_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
747 /// Adding keys to table repository_instances
748 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
750 /// Conditionally launch create table for repository_instances
751 if (!$dbman->table_exists($table)) {
752 $dbman->create_table($table);
755 /// Define table repository_instance_config to be created
756 $table = new xmldb_table('repository_instance_config');
758 /// Adding fields to table repository_instance_config
759 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
760 $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
761 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
762 $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
764 /// Adding keys to table repository_instance_config
765 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
767 /// Conditionally launch create table for repository_instance_config
768 if (!$dbman->table_exists($table)) {
769 $dbman->create_table($table);
772 /// Main savepoint reached
773 upgrade_main_savepoint(true, 2008082602);
776 if ($oldversion < 2008082700) {
777 /// Add a new column to the question sessions table to record whether a
778 /// question has been flagged.
780 /// Define field flagged to be added to question_sessions
781 $table = new xmldb_table('question_sessions');
782 $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
784 /// Conditionally launch add field flagged
785 if (!$dbman->field_exists($table, $field)) {
786 $dbman->add_field($table, $field);
789 /// Main savepoint reached
790 upgrade_main_savepoint(true, 2008082700);
793 if ($oldversion < 2008082900) {
795 /// Changing precision of field parent_type on table mnet_rpc to (20)
796 $table = new xmldb_table('mnet_rpc');
797 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
799 /// Launch change of precision for field parent_type
800 $dbman->change_field_precision($table, $field);
802 /// Main savepoint reached
803 upgrade_main_savepoint(true, 2008082900);
806 // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
807 if ($oldversion < 2008091000) {
808 foreach (get_object_vars($CFG) as $name => $value) {
809 if (substr($name, strlen($name) - 8) !== '_version') {
810 continue;
812 $pluginname = substr($name, 0, strlen($name) - 8);
813 if (!strpos($pluginname, '_')) {
814 // Skip things like backup_version that don't contain an extra _
815 continue;
817 if ($pluginname == 'enrol_ldap_version') {
818 // Special case - this is something different from a plugin version number.
819 continue;
821 if (!preg_match('/^\d{10}$/', $value)) {
822 // Extra safety check, skip anything that does not look like a Moodle
823 // version number (10 digits).
824 continue;
826 set_config('version', $value, $pluginname);
827 unset_config($name);
829 upgrade_main_savepoint(true, 2008091000);
832 if ($oldversion < 2008092300) {
833 unset_config('editorspelling');
834 unset_config('editordictionary');
835 /// Main savepoint reached
836 upgrade_main_savepoint(true, 2008092300);
839 if ($oldversion < 2008101300) {
841 if (!get_config(NULL, 'statsruntimedays')) {
842 set_config('statsruntimedays', '31');
845 /// Main savepoint reached
846 upgrade_main_savepoint(true, 2008101300);
849 /// Drop the deprecated teacher, teachers, student and students columns from the course table.
850 if ($oldversion < 2008111200) {
851 $table = new xmldb_table('course');
853 /// Conditionally launch drop field teacher
854 $field = new xmldb_field('teacher');
855 if ($dbman->field_exists($table, $field)) {
856 $dbman->drop_field($table, $field);
859 /// Conditionally launch drop field teacher
860 $field = new xmldb_field('teachers');
861 if ($dbman->field_exists($table, $field)) {
862 $dbman->drop_field($table, $field);
865 /// Conditionally launch drop field teacher
866 $field = new xmldb_field('student');
867 if ($dbman->field_exists($table, $field)) {
868 $dbman->drop_field($table, $field);
871 /// Conditionally launch drop field teacher
872 $field = new xmldb_field('students');
873 if ($dbman->field_exists($table, $field)) {
874 $dbman->drop_field($table, $field);
877 /// Main savepoint reached
878 upgrade_main_savepoint(true, 2008111200);
881 /// Add a unique index to the role.name column.
882 if ($oldversion < 2008111800) {
884 /// Define index name (unique) to be added to role
885 $table = new xmldb_table('role');
886 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
888 /// Conditionally launch add index name
889 if (!$dbman->index_exists($table, $index)) {
890 $dbman->add_index($table, $index);
893 /// Main savepoint reached
894 upgrade_main_savepoint(true, 2008111800);
897 /// Add a unique index to the role.shortname column.
898 if ($oldversion < 2008111801) {
900 /// Define index shortname (unique) to be added to role
901 $table = new xmldb_table('role');
902 $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
904 /// Conditionally launch add index shortname
905 if (!$dbman->index_exists($table, $index)) {
906 $dbman->add_index($table, $index);
909 /// Main savepoint reached
910 upgrade_main_savepoint(true, 2008111801);
913 if ($oldversion < 2008120700) {
915 /// Changing precision of field shortname on table course_request to (100)
916 $table = new xmldb_table('course_request');
917 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
919 /// Before changing the field, drop dependent indexes
920 /// Define index shortname (not unique) to be dropped form course_request
921 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
922 /// Conditionally launch drop index shortname
923 if ($dbman->index_exists($table, $index)) {
924 $dbman->drop_index($table, $index);
927 /// Launch change of precision for field shortname
928 $dbman->change_field_precision($table, $field);
930 /// After changing the field, recreate dependent indexes
931 /// Define index shortname (not unique) to be added to course_request
932 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
933 /// Conditionally launch add index shortname
934 if (!$dbman->index_exists($table, $index)) {
935 $dbman->add_index($table, $index);
938 /// Main savepoint reached
939 upgrade_main_savepoint(true, 2008120700);
942 if ($oldversion < 2008120801) {
944 /// Changing precision of field shortname on table mnet_enrol_course to (100)
945 $table = new xmldb_table('mnet_enrol_course');
946 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
948 /// Launch change of precision for field shortname
949 $dbman->change_field_precision($table, $field);
951 /// Main savepoint reached
952 upgrade_main_savepoint(true, 2008120801);
955 if ($oldversion < 2008121701) {
957 /// Define field availablefrom to be added to course_modules
958 $table = new xmldb_table('course_modules');
959 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
961 /// Conditionally launch add field availablefrom
962 if (!$dbman->field_exists($table, $field)) {
963 $dbman->add_field($table, $field);
966 /// Define field availableuntil to be added to course_modules
967 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
969 /// Conditionally launch add field availableuntil
970 if (!$dbman->field_exists($table, $field)) {
971 $dbman->add_field($table, $field);
974 /// Define field showavailability to be added to course_modules
975 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
977 /// Conditionally launch add field showavailability
978 if (!$dbman->field_exists($table, $field)) {
979 $dbman->add_field($table, $field);
982 /// Define table course_modules_availability to be created
983 $table = new xmldb_table('course_modules_availability');
985 /// Adding fields to table course_modules_availability
986 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
987 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
988 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
989 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
990 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
991 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
992 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
994 /// Adding keys to table course_modules_availability
995 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
996 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
997 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
998 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
1000 /// Conditionally launch create table for course_modules_availability
1001 if (!$dbman->table_exists($table)) {
1002 $dbman->create_table($table);
1005 /// Changes to modinfo mean we need to rebuild course cache
1006 require_once($CFG->dirroot . '/course/lib.php');
1007 rebuild_course_cache(0, true);
1009 /// Main savepoint reached
1010 upgrade_main_savepoint(true, 2008121701);
1013 if ($oldversion < 2009010500) {
1014 /// clean up config table a bit
1015 unset_config('session_error_counter');
1017 /// Main savepoint reached
1018 upgrade_main_savepoint(true, 2009010500);
1021 if ($oldversion < 2009010600) {
1023 /// Define field originalquestion to be dropped from question_states
1024 $table = new xmldb_table('question_states');
1025 $field = new xmldb_field('originalquestion');
1027 /// Conditionally launch drop field originalquestion
1028 if ($dbman->field_exists($table, $field)) {
1029 $dbman->drop_field($table, $field);
1032 /// Main savepoint reached
1033 upgrade_main_savepoint(true, 2009010600);
1036 if ($oldversion < 2009010601) {
1038 /// Changing precision of field ip on table log to (45)
1039 $table = new xmldb_table('log');
1040 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1042 /// Launch change of precision for field ip
1043 $dbman->change_field_precision($table, $field);
1045 /// Main savepoint reached
1046 upgrade_main_savepoint(true, 2009010601);
1049 if ($oldversion < 2009010602) {
1051 /// Changing precision of field lastip on table user to (45)
1052 $table = new xmldb_table('user');
1053 $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
1055 /// Launch change of precision for field lastip
1056 $dbman->change_field_precision($table, $field);
1058 /// Main savepoint reached
1059 upgrade_main_savepoint(true, 2009010602);
1062 if ($oldversion < 2009010603) {
1064 /// Changing precision of field ip_address on table mnet_host to (45)
1065 $table = new xmldb_table('mnet_host');
1066 $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
1068 /// Launch change of precision for field ip_address
1069 $dbman->change_field_precision($table, $field);
1071 /// Main savepoint reached
1072 upgrade_main_savepoint(true, 2009010603);
1075 if ($oldversion < 2009010604) {
1077 /// Changing precision of field ip on table mnet_log to (45)
1078 $table = new xmldb_table('mnet_log');
1079 $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1081 /// Launch change of precision for field ip
1082 $dbman->change_field_precision($table, $field);
1084 /// Main savepoint reached
1085 upgrade_main_savepoint(true, 2009010604);
1088 if ($oldversion < 2009011000) {
1090 /// Changing nullability of field configdata on table block_instance to null
1091 $table = new xmldb_table('block_instance');
1092 $field = new xmldb_field('configdata');
1093 $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
1095 /// Launch change of nullability for field configdata
1096 $dbman->change_field_notnull($table, $field);
1098 /// Main savepoint reached
1099 upgrade_main_savepoint(true, 2009011000);
1102 if ($oldversion < 2009011100) {
1103 /// Remove unused settings
1104 unset_config('zip');
1105 unset_config('unzip');
1106 unset_config('adminblocks_initialised');
1108 /// Main savepoint reached
1109 upgrade_main_savepoint(true, 2009011100);
1112 if ($oldversion < 2009011101) {
1113 /// Migrate backup settings to core plugin config table
1114 $configs = $DB->get_records('backup_config');
1115 foreach ($configs as $config) {
1116 set_config($config->name, $config->value, 'backup');
1119 /// Define table to be dropped
1120 $table = new xmldb_table('backup_config');
1122 /// Launch drop table for old backup config
1123 $dbman->drop_table($table);
1125 /// Main savepoint reached
1126 upgrade_main_savepoint(true, 2009011101);
1129 if ($oldversion < 2009011303) {
1131 /// Define table config_log to be created
1132 $table = new xmldb_table('config_log');
1134 /// Adding fields to table config_log
1135 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1136 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1137 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1138 $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1139 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1140 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1141 $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1143 /// Adding keys to table config_log
1144 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1145 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1147 /// Adding indexes to table config_log
1148 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1150 /// Launch create table for config_log
1151 $dbman->create_table($table);
1153 /// Main savepoint reached
1154 upgrade_main_savepoint(true, 2009011303);
1157 if ($oldversion < 2009011900) {
1159 /// Define table sessions2 to be dropped
1160 $table = new xmldb_table('sessions2');
1162 /// Conditionally launch drop table for sessions
1163 if ($dbman->table_exists($table)) {
1164 $dbman->drop_table($table);
1167 /// Define table sessions to be dropped
1168 $table = new xmldb_table('sessions');
1170 /// Conditionally launch drop table for sessions
1171 if ($dbman->table_exists($table)) {
1172 $dbman->drop_table($table);
1175 /// Define table sessions to be created
1176 $table = new xmldb_table('sessions');
1178 /// Adding fields to table sessions
1179 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1180 $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1181 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
1182 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1183 $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
1184 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1185 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1186 $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1187 $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1189 /// Adding keys to table sessions
1190 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1191 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1193 /// Adding indexes to table sessions
1194 $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1195 $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1196 $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1197 $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1199 /// Launch create table for sessions
1200 $dbman->create_table($table);
1202 /// Main savepoint reached
1203 upgrade_main_savepoint(true, 2009011900);
1206 if ($oldversion < 2009021800) {
1207 // Converting format of grade conditions, if any exist, to percentages.
1208 $DB->execute("
1209 UPDATE {course_modules_availability} SET grademin=(
1210 SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
1211 /(gi.grademax-gi.grademin)
1212 FROM {grade_items} gi
1213 WHERE gi.id={course_modules_availability}.gradeitemid)
1214 WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1215 $DB->execute("
1216 UPDATE {course_modules_availability} SET grademax=(
1217 SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
1218 /(gi.grademax-gi.grademin)
1219 FROM {grade_items} gi
1220 WHERE gi.id={course_modules_availability}.gradeitemid)
1221 WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1223 /// Main savepoint reached
1224 upgrade_main_savepoint(true, 2009021800);
1227 /// Add default sort order for question types.
1228 if ($oldversion < 2009030300) {
1229 set_config('multichoice_sortorder', 1, 'question');
1230 set_config('truefalse_sortorder', 2, 'question');
1231 set_config('shortanswer_sortorder', 3, 'question');
1232 set_config('numerical_sortorder', 4, 'question');
1233 set_config('calculated_sortorder', 5, 'question');
1234 set_config('essay_sortorder', 6, 'question');
1235 set_config('match_sortorder', 7, 'question');
1236 set_config('randomsamatch_sortorder', 8, 'question');
1237 set_config('multianswer_sortorder', 9, 'question');
1238 set_config('description_sortorder', 10, 'question');
1239 set_config('random_sortorder', 11, 'question');
1240 set_config('missingtype_sortorder', 12, 'question');
1242 upgrade_main_savepoint(true, 2009030300);
1245 /// MDL-18132 replace the use a new Role allow switch settings page, instead of
1246 /// $CFG->allowuserswitchrolestheycantassign
1247 if ($oldversion < 2009032000) {
1248 /// First create the new table.
1249 $table = new xmldb_table('role_allow_switch');
1251 /// Adding fields to table role_allow_switch
1252 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1253 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1254 $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1256 /// Adding keys to table role_allow_switch
1257 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1258 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
1259 $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
1261 /// Adding indexes to table role_allow_switch
1262 $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1264 /// Conditionally launch create table for role_allow_switch
1265 if (!$dbman->table_exists($table)) {
1266 $dbman->create_table($table);
1269 /// Main savepoint reached
1270 upgrade_main_savepoint(true, 2009032000);
1273 if ($oldversion < 2009032001) {
1274 /// Copy from role_allow_assign into the new table.
1275 $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1276 SELECT roleid, allowassign FROM {role_allow_assign}');
1278 /// Unset the config variable used in 1.9.
1279 unset_config('allowuserswitchrolestheycantassign');
1281 /// Main savepoint reached
1282 upgrade_main_savepoint(true, 2009032001);
1285 if ($oldversion < 2009040300) {
1287 /// Define table filter_active to be created
1288 $table = new xmldb_table('filter_active');
1290 /// Adding fields to table filter_active
1291 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1292 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1293 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1294 $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1295 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1297 /// Adding keys to table filter_active
1298 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1299 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1301 /// Adding indexes to table filter_active
1302 $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1304 /// Conditionally launch create table for filter_active
1305 if (!$dbman->table_exists($table)) {
1306 $dbman->create_table($table);
1309 /// Main savepoint reached
1310 upgrade_main_savepoint(true, 2009040300);
1313 if ($oldversion < 2009040301) {
1315 /// Define table filter_config to be created
1316 $table = new xmldb_table('filter_config');
1318 /// Adding fields to table filter_config
1319 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1320 $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1321 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1322 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1323 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1325 /// Adding keys to table filter_config
1326 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1327 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1329 /// Adding indexes to table filter_config
1330 $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1332 /// Conditionally launch create table for filter_config
1333 if (!$dbman->table_exists($table)) {
1334 $dbman->create_table($table);
1337 /// Main savepoint reached
1338 upgrade_main_savepoint(true, 2009040301);
1341 if ($oldversion < 2009040302) {
1342 /// Transfer current settings from $CFG->textfilters
1343 $disabledfilters = filter_get_all_installed();
1344 if (empty($CFG->textfilters)) {
1345 $activefilters = array();
1346 } else {
1347 $activefilters = explode(',', $CFG->textfilters);
1349 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1350 $sortorder = 1;
1351 foreach ($activefilters as $filter) {
1352 filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1353 $sortorder += 1;
1354 unset($disabledfilters[$filter]);
1356 foreach ($disabledfilters as $filter => $notused) {
1357 filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1358 $sortorder += 1;
1361 /// Main savepoint reached
1362 upgrade_main_savepoint(true, 2009040302);
1365 if ($oldversion < 2009040600) {
1366 /// Ensure that $CFG->stringfilters is set.
1367 if (empty($CFG->stringfilters)) {
1368 if (!empty($CFG->filterall)) {
1369 set_config('stringfilters', $CFG->textfilters);
1370 } else {
1371 set_config('stringfilters', '');
1375 set_config('filterall', !empty($CFG->stringfilters));
1376 unset_config('textfilters');
1378 /// Main savepoint reached
1379 upgrade_main_savepoint(true, 2009040600);
1382 if ($oldversion < 2009041700) {
1383 /// To ensure the UI remains consistent with no behaviour change, any
1384 /// 'until' date in an activity condition should have 1 second subtracted
1385 /// (to go from 0:00 on the following day to 23:59 on the previous one).
1386 $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
1387 require_once($CFG->dirroot . '/course/lib.php');
1388 rebuild_course_cache(0, true);
1390 /// Main savepoint reached
1391 upgrade_main_savepoint(true, 2009041700);
1394 if ($oldversion < 2009042600) {
1395 /// Deleting orphaned messages from deleted users.
1396 require_once($CFG->dirroot.'/message/lib.php');
1397 /// Detect deleted users with messages sent(useridfrom) and not read
1398 if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
1399 FROM {user} u
1400 JOIN {message} m ON m.useridfrom = u.id
1401 WHERE u.deleted = ?', array(1))) {
1402 foreach ($deletedusers as $deleteduser) {
1403 message_move_userfrom_unread2read($deleteduser->id); // move messages
1406 /// Main savepoint reached
1407 upgrade_main_savepoint(true, 2009042600);
1410 /// Dropping all enums/check contraints from core. MDL-18577
1411 if ($oldversion < 2009042700) {
1413 /// Changing list of values (enum) of field stattype on table stats_daily to none
1414 $table = new xmldb_table('stats_daily');
1415 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1417 /// Launch change of list of values for field stattype
1418 $dbman->drop_enum_from_field($table, $field);
1420 /// Changing list of values (enum) of field stattype on table stats_weekly to none
1421 $table = new xmldb_table('stats_weekly');
1422 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1424 /// Launch change of list of values for field stattype
1425 $dbman->drop_enum_from_field($table, $field);
1427 /// Changing list of values (enum) of field stattype on table stats_monthly to none
1428 $table = new xmldb_table('stats_monthly');
1429 $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1431 /// Launch change of list of values for field stattype
1432 $dbman->drop_enum_from_field($table, $field);
1434 /// Changing list of values (enum) of field publishstate on table post to none
1435 $table = new xmldb_table('post');
1436 $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
1438 /// Launch change of list of values for field publishstate
1439 $dbman->drop_enum_from_field($table, $field);
1441 /// Main savepoint reached
1442 upgrade_main_savepoint(true, 2009042700);
1445 if ($oldversion < 2009043000) {
1446 unset_config('grade_report_showgroups');
1447 upgrade_main_savepoint(true, 2009043000);
1450 if ($oldversion < 2009050600) {
1451 /// Site front page blocks need to be moved due to page name change.
1452 $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
1454 /// Main savepoint reached
1455 upgrade_main_savepoint(true, 2009050600);
1458 if ($oldversion < 2009050601) {
1460 /// Define table block_instance to be renamed to block_instances
1461 $table = new xmldb_table('block_instance');
1463 /// Launch rename table for block_instance
1464 $dbman->rename_table($table, 'block_instances');
1466 /// Main savepoint reached
1467 upgrade_main_savepoint(true, 2009050601);
1470 if ($oldversion < 2009050602) {
1472 /// Define table block_instance to be renamed to block_instance_old
1473 $table = new xmldb_table('block_pinned');
1475 /// Launch rename table for block_instance
1476 $dbman->rename_table($table, 'block_pinned_old');
1478 /// Main savepoint reached
1479 upgrade_main_savepoint(true, 2009050602);
1482 if ($oldversion < 2009050603) {
1484 /// Define table block_instance_old to be created
1485 $table = new xmldb_table('block_instance_old');
1487 /// Adding fields to table block_instance_old
1488 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1489 $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1490 $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1491 $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1492 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
1493 $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
1494 $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
1495 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1496 $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1498 /// Adding keys to table block_instance_old
1499 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1500 $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
1502 /// Adding indexes to table block_instance_old
1503 $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1504 $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
1506 /// Conditionally launch create table for block_instance_old
1507 if (!$dbman->table_exists($table)) {
1508 $dbman->create_table($table);
1511 /// Main savepoint reached
1512 upgrade_main_savepoint(true, 2009050603);
1515 if ($oldversion < 2009050604) {
1516 /// Copy current blocks data from block_instances to block_instance_old
1517 $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
1518 SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
1520 upgrade_main_savepoint(true, 2009050604);
1523 if ($oldversion < 2009050605) {
1525 /// Define field multiple to be dropped from block
1526 $table = new xmldb_table('block');
1527 $field = new xmldb_field('multiple');
1529 /// Conditionally launch drop field multiple
1530 if ($dbman->field_exists($table, $field)) {
1531 $dbman->drop_field($table, $field);
1534 /// Main savepoint reached
1535 upgrade_main_savepoint(true, 2009050605);
1538 if ($oldversion < 2009050606) {
1539 $table = new xmldb_table('block_instances');
1541 /// Rename field weight on table block_instances to defaultweight
1542 $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, 'position');
1543 $dbman->rename_field($table, $field, 'defaultweight');
1545 /// Rename field position on table block_instances to defaultregion
1546 $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
1547 $dbman->rename_field($table, $field, 'defaultregion');
1549 /// Main savepoint reached
1550 upgrade_main_savepoint(true, 2009050606);
1553 if ($oldversion < 2009050607) {
1554 /// Changing precision of field defaultregion on table block_instances to (16)
1555 $table = new xmldb_table('block_instances');
1556 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
1558 /// Launch change of precision for field defaultregion
1559 $dbman->change_field_precision($table, $field);
1561 /// Main savepoint reached
1562 upgrade_main_savepoint(true, 2009050607);
1565 if ($oldversion < 2009050608) {
1566 /// Change regions to the new notation
1567 $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1568 $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1569 $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1570 // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1571 // flex page course format. Hopefully this new value is an adequate alternative.
1573 /// Main savepoint reached
1574 upgrade_main_savepoint(true, 2009050608);
1577 if ($oldversion < 2009050609) {
1579 /// Define key blockname (unique) to be added to block
1580 $table = new xmldb_table('block');
1581 $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1583 /// Launch add key blockname
1584 $dbman->add_key($table, $key);
1586 /// Main savepoint reached
1587 upgrade_main_savepoint(true, 2009050609);
1590 if ($oldversion < 2009050610) {
1591 $table = new xmldb_table('block_instances');
1593 /// Define field blockname to be added to block_instances
1594 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
1595 if (!$dbman->field_exists($table, $field)) {
1596 $dbman->add_field($table, $field);
1599 /// Define field contextid to be added to block_instances
1600 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
1601 if (!$dbman->field_exists($table, $field)) {
1602 $dbman->add_field($table, $field);
1605 /// Define field showinsubcontexts to be added to block_instances
1606 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
1607 if (!$dbman->field_exists($table, $field)) {
1608 $dbman->add_field($table, $field);
1611 /// Define field subpagepattern to be added to block_instances
1612 $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
1613 if (!$dbman->field_exists($table, $field)) {
1614 $dbman->add_field($table, $field);
1617 /// Main savepoint reached
1618 upgrade_main_savepoint(true, 2009050610);
1621 if ($oldversion < 2009050611) {
1622 $table = new xmldb_table('block_instances');
1624 /// Fill in blockname from blockid
1625 $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1627 /// Set showinsubcontexts = 0 for all rows.
1628 $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1630 /// Main savepoint reached
1631 upgrade_main_savepoint(true, 2009050611);
1634 if ($oldversion < 2009050612) {
1636 /// Rename field pagetype on table block_instances to pagetypepattern
1637 $table = new xmldb_table('block_instances');
1638 $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
1640 /// Launch rename field pagetype
1641 $dbman->rename_field($table, $field, 'pagetypepattern');
1643 /// Main savepoint reached
1644 upgrade_main_savepoint(true, 2009050612);
1647 if ($oldversion < 2009050613) {
1648 /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1650 /// site-index
1651 $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
1652 $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1653 pagetypepattern = 'site-index',
1654 subpagepattern = NULL
1655 WHERE pagetypepattern = 'site-index'");
1657 /// course-view
1658 $DB->execute("UPDATE {block_instances} SET
1659 contextid = (
1660 SELECT {context}.id
1661 FROM {context}
1662 JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1663 WHERE {course}.id = pageid
1665 pagetypepattern = 'course-view-*',
1666 subpagepattern = NULL
1667 WHERE pagetypepattern = 'course-view'");
1669 /// admin
1670 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1671 $DB->execute("UPDATE {block_instances} SET
1672 contextid = " . $syscontext->id . ",
1673 pagetypepattern = 'admin-*',
1674 subpagepattern = NULL
1675 WHERE pagetypepattern = 'admin'");
1677 /// my-index
1678 $DB->execute("UPDATE {block_instances} SET
1679 contextid = (
1680 SELECT {context}.id
1681 FROM {context}
1682 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1683 WHERE {user}.id = pageid
1685 pagetypepattern = 'my-index',
1686 subpagepattern = NULL
1687 WHERE pagetypepattern = 'my-index'");
1689 /// tag-index
1690 $DB->execute("UPDATE {block_instances} SET
1691 contextid = " . $syscontext->id . ",
1692 pagetypepattern = 'tag-index',
1693 subpagepattern = pageid
1694 WHERE pagetypepattern = 'tag-index'");
1696 /// blog-view
1697 $DB->execute("UPDATE {block_instances} SET
1698 contextid = (
1699 SELECT {context}.id
1700 FROM {context}
1701 JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1702 WHERE {user}.id = pageid
1704 pagetypepattern = 'blog-index',
1705 subpagepattern = NULL
1706 WHERE pagetypepattern = 'blog-view'");
1708 /// mod-xxx-view
1709 $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1710 foreach ($moduleswithblocks as $modname) {
1711 if (!$dbman->table_exists($modname)) {
1712 continue;
1714 $DB->execute("UPDATE {block_instances} SET
1715 contextid = (
1716 SELECT {context}.id
1717 FROM {context}
1718 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1719 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1720 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1721 WHERE {{$modname}}.id = pageid
1723 pagetypepattern = 'blog-index',
1724 subpagepattern = NULL
1725 WHERE pagetypepattern = 'blog-view'");
1728 /// Main savepoint reached
1729 upgrade_main_savepoint(true, 2009050613);
1732 if ($oldversion < 2009050614) {
1733 /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
1734 $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
1736 /// Main savepoint reached
1737 upgrade_main_savepoint(true, 2009050614);
1740 if ($oldversion < 2009050615) {
1741 $table = new xmldb_table('block_instances');
1743 /// Arrived here, any block_instances record without blockname is one
1744 /// orphan block coming from 1.9. Just delete them. MDL-22503
1745 $DB->delete_records_select('block_instances', 'blockname IS NULL');
1747 /// Changing nullability of field blockname on table block_instances to not null
1748 $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
1749 $dbman->change_field_notnull($table, $field);
1751 /// Changing nullability of field contextid on table block_instances to not null
1752 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
1753 $dbman->change_field_notnull($table, $field);
1755 /// Changing nullability of field showinsubcontexts on table block_instances to not null
1756 $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
1757 $dbman->change_field_notnull($table, $field);
1759 /// Main savepoint reached
1760 upgrade_main_savepoint(true, 2009050615);
1763 if ($oldversion < 2009050616) {
1764 /// Add exiting sticky blocks.
1765 $blocks = $DB->get_records('block');
1766 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1767 $newregions = array(
1768 'l' => 'side-pre',
1769 'r' => 'side-post',
1770 'c' => 'course-view-top',
1772 $stickyblocks = $DB->get_recordset('block_pinned_old');
1773 foreach ($stickyblocks as $stickyblock) {
1774 // Only if the block exists (avoid orphaned sticky blocks)
1775 if (!isset($blocks[$stickyblock->blockid]) || empty($blocks[$stickyblock->blockid]->name)) {
1776 continue;
1778 $newblock = new stdClass();
1779 $newblock->blockname = $blocks[$stickyblock->blockid]->name;
1780 $newblock->contextid = $syscontext->id;
1781 $newblock->showinsubcontexts = 1;
1782 switch ($stickyblock->pagetype) {
1783 case 'course-view':
1784 $newblock->pagetypepattern = 'course-view-*';
1785 break;
1786 default:
1787 $newblock->pagetypepattern = $stickyblock->pagetype;
1789 $newblock->defaultregion = $newregions[$stickyblock->position];
1790 $newblock->defaultweight = $stickyblock->weight;
1791 $newblock->configdata = $stickyblock->configdata;
1792 $newblock->visible = 1;
1793 $DB->insert_record('block_instances', $newblock);
1796 /// Main savepoint reached
1797 upgrade_main_savepoint(true, 2009050616);
1800 if ($oldversion < 2009050617) {
1802 /// Define table block_positions to be created
1803 $table = new xmldb_table('block_positions');
1805 /// Adding fields to table block_positions
1806 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1807 $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1808 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1809 $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1810 $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1811 $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1812 $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1813 $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1815 /// Adding keys to table block_positions
1816 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1817 $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
1818 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1820 /// Adding indexes to table block_positions
1821 $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
1823 /// Conditionally launch create table for block_positions
1824 if (!$dbman->table_exists($table)) {
1825 $dbman->create_table($table);
1828 /// Main savepoint reached
1829 upgrade_main_savepoint(true, 2009050617);
1832 if ($oldversion < 2009050618) {
1833 /// And block instances with visible = 0, copy that information to block_positions
1834 $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
1835 SELECT bi.id, bi.contextid,
1836 CASE WHEN bi.pagetypepattern = 'course-view-*'
1837 THEN (SELECT " . $DB->sql_concat("'course-view-'", 'c.format') . "
1838 FROM {course} c
1839 JOIN {context} ctx ON c.id = ctx.instanceid
1840 WHERE ctx.id = bi.contextid)
1841 ELSE bi.pagetypepattern END,
1842 CASE WHEN bi.subpagepattern IS NULL
1843 THEN '" . $DB->sql_empty() . "'
1844 ELSE bi.subpagepattern END,
1845 0, bi.defaultregion, bi.defaultweight
1846 FROM {block_instances} bi
1847 WHERE bi.visible = 0 AND bi.pagetypepattern <> 'admin-*' AND bi.pagetypepattern IS NOT NULL");
1848 // note: MDL-25031 all block instances should have a pagetype pattern, NULL is not allowed,
1849 // if we manage to find out how NULLs get there we should fix them before this step
1851 /// Main savepoint reached
1852 upgrade_main_savepoint(true, 2009050618);
1855 if ($oldversion < 2009050619) {
1856 $table = new xmldb_table('block_instances');
1858 /// Define field blockid to be dropped from block_instances
1859 $field = new xmldb_field('blockid');
1860 if ($dbman->field_exists($table, $field)) {
1861 /// Before dropping the field, drop dependent indexes
1862 $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
1863 if ($dbman->index_exists($table, $index)) {
1864 /// Launch drop index blockid
1865 $dbman->drop_index($table, $index);
1867 $dbman->drop_field($table, $field);
1870 /// Define field pageid to be dropped from block_instances
1871 $field = new xmldb_field('pageid');
1872 if ($dbman->field_exists($table, $field)) {
1873 /// Before dropping the field, drop dependent indexes
1874 $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1875 if ($dbman->index_exists($table, $index)) {
1876 /// Launch drop index pageid
1877 $dbman->drop_index($table, $index);
1879 $dbman->drop_field($table, $field);
1882 /// Define field visible to be dropped from block_instances
1883 $field = new xmldb_field('visible');
1884 if ($dbman->field_exists($table, $field)) {
1885 $dbman->drop_field($table, $field);
1888 /// Main savepoint reached
1889 upgrade_main_savepoint(true, 2009050619);
1892 if ($oldversion < 2009051200) {
1893 /// Let's check the status of mandatory mnet_host records, fixing them
1894 /// and moving "orphan" users to default localhost record. MDL-16879
1895 echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
1896 upgrade_fix_incorrect_mnethostids();
1898 /// Main savepoint reached
1899 upgrade_main_savepoint(true, 2009051200);
1903 if ($oldversion < 2009051700) {
1904 /// migrate editor settings
1905 if (empty($CFG->htmleditor)) {
1906 set_config('texteditors', 'textarea');
1907 } else {
1908 set_config('texteditors', 'tinymce,textarea');
1911 unset_config('htmleditor');
1912 unset_config('defaulthtmleditor');
1914 /// Main savepoint reached
1915 upgrade_main_savepoint(true, 2009051700);
1918 /// Repeat 2009050607 upgrade step, which Petr commented out because of XMLDB
1919 /// stupidity, so lots of people will have missed.
1920 if ($oldversion < 2009061600) {
1921 /// Changing precision of field defaultregion on table block_instances to (16)
1922 $table = new xmldb_table('block_instances');
1923 $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
1925 /// Launch change of precision for field defaultregion
1926 $dbman->change_field_precision($table, $field);
1928 /// Main savepoint reached
1929 upgrade_main_savepoint(true, 2009061600);
1932 if ($oldversion < 2009061702) {
1933 // standardizing plugin names
1934 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
1935 foreach ($configs as $config) {
1936 unset_config($config->name, $config->plugin); /// unset old config
1937 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
1938 set_config($config->name, $config->value, $config->plugin); /// set new config
1941 unset($configs);
1942 upgrade_main_savepoint(true, 2009061702);
1945 if ($oldversion < 2009061703) {
1946 // standardizing plugin names
1947 if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
1948 foreach ($configs as $config) {
1949 unset_config($config->name, $config->plugin); /// unset old config
1950 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
1951 set_config($config->name, $config->value, $config->plugin); /// set new config
1954 unset($configs);
1955 upgrade_main_savepoint(true, 2009061703);
1958 if ($oldversion < 2009061704) {
1959 // change component string in capability records to new "_" format
1960 if ($caps = $DB->get_records('capabilities')) {
1961 foreach ($caps as $cap) {
1962 $cap->component = str_replace('/', '_', $cap->component);
1963 $DB->update_record('capabilities', $cap);
1966 unset($caps);
1967 upgrade_main_savepoint(true, 2009061704);
1970 if ($oldversion < 2009063000) {
1971 // upgrade format of _with_advanced settings - quiz only
1972 // note: this can be removed later, not needed for upgrades from 1.9.x
1973 if ($quiz = get_config('quiz')) {
1974 foreach ($quiz as $name=>$value) {
1975 if (strpos($name, 'fix_') !== 0) {
1976 continue;
1978 $newname = substr($name,4).'_adv';
1979 set_config($newname, $value, 'quiz');
1980 unset_config($name, 'quiz');
1983 upgrade_main_savepoint(true, 2009063000);
1986 if ($oldversion < 2009071000) {
1988 /// Rename field contextid on table block_instances to parentcontextid
1989 $table = new xmldb_table('block_instances');
1990 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
1992 /// Launch rename field parentcontextid
1993 $dbman->rename_field($table, $field, 'parentcontextid');
1995 /// Main savepoint reached
1996 upgrade_main_savepoint(true, 2009071000);
1999 if ($oldversion < 2009071600) {
2001 /// Define field summaryformat to be added to post
2002 $table = new xmldb_table('post');
2003 $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
2005 /// Conditionally launch add field summaryformat
2006 if (!$dbman->field_exists($table, $field)) {
2007 $dbman->add_field($table, $field);
2010 /// Main savepoint reached
2011 upgrade_main_savepoint(true, 2009071600);
2014 if ($oldversion < 2009072400) {
2016 /// Define table comments to be created
2017 $table = new xmldb_table('comments');
2019 /// Adding fields to table comments
2020 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2021 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2022 $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2023 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2024 $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2025 $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2026 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2027 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2029 /// Adding keys to table comments
2030 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2032 /// Conditionally launch create table for comments
2033 if (!$dbman->table_exists($table)) {
2034 $dbman->create_table($table);
2037 /// Main savepoint reached
2038 upgrade_main_savepoint(true, 2009072400);
2042 * This upgrade is to set up the new navigation blocks that have been developed
2043 * as part of Moodle 2.0
2044 * Now I [Sam Hemelryk] hit a conundrum while exploring how to go about this
2045 * as not only do we want to install the new blocks but we also want to set up
2046 * default instances of them, and at the same time remove instances of the blocks
2047 * that were/will-be outmoded by the two new navigation blocks.
2048 * After talking it through with Tim Hunt {@link http://moodle.org/mod/cvsadmin/view.php?conversationid=3112}
2049 * we decided that the best way to go about this was to put the bulk of the
2050 * upgrade operation into core upgrade `here` but to let the plugins block
2051 * still install the blocks.
2052 * This leaves one hairy end in that we will create block_instances within the
2053 * DB before the blocks themselves are created within the DB
2055 if ($oldversion < 2009082800) {
2057 echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2059 // Get the system context so we can set the block instances to it
2060 $syscontext = get_context_instance(CONTEXT_SYSTEM);
2062 // An array to contain the new block instances we will create
2063 $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
2064 // The new global navigation block instance as a stdClass
2065 $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
2066 $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
2067 $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutely everywhere
2068 $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
2069 $newblockinstances['globalnavigation']->subpagetypepattern = null;
2070 $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
2071 $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
2072 $newblockinstances['globalnavigation']->configdata = '';
2073 // The new settings navigation block instance as a stdClass
2074 $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
2075 $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
2076 $newblockinstances['settingsnavigation']->showinsubcontexts = true;
2077 $newblockinstances['settingsnavigation']->pagetypepattern = '*';
2078 $newblockinstances['settingsnavigation']->subpagetypepattern = null;
2079 $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
2080 $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
2081 $newblockinstances['settingsnavigation']->configdata = '';
2083 // Blocks that are outmoded and for whom the bells will toll... by which I
2084 // mean we will delete all instances of
2085 $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
2086 $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
2087 unset($outmodedblocks);
2088 // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
2089 // cut down the number of delete queries we will need to run
2090 $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
2092 $contextids = array();
2093 $instanceids = array();
2094 // Iterate through all block instances
2095 foreach ($allblockinstances as $blockinstance) {
2096 if (!in_array($blockinstance->parentcontextid, $contextids)) {
2097 $contextids[] = $blockinstance->parentcontextid;
2099 // If we have over 1000 contexts clean them up and reset the array
2100 // this ensures we don't hit any nasty memory limits or such
2101 if (count($contextids) > 1000) {
2102 upgrade_cleanup_unwanted_block_contexts($contextids);
2103 $contextids = array();
2106 if (!in_array($blockinstance->id, $instanceids)) {
2107 $instanceids[] = $blockinstance->id;
2108 // If we have more than 1000 block instances now remove all block positions
2109 // and empty the array
2110 if (count($instanceids) > 1000) {
2111 $instanceidstring = join(',',$instanceids);
2112 $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2113 $instanceids = array();
2118 upgrade_cleanup_unwanted_block_contexts($contextids);
2120 if ($instanceids) {
2121 $instanceidstring = join(',',$instanceids);
2122 $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2125 unset($allblockinstances);
2126 unset($contextids);
2127 unset($instanceids);
2128 unset($instanceidstring);
2130 // Now remove the actual block instance
2131 $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
2132 unset($outmodedblocksstring);
2134 // Insert the new block instances. Remember they have not been installed yet
2135 // however this should not be a problem
2136 foreach ($newblockinstances as $blockinstance) {
2137 $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
2138 // Ensure the block context is created.
2139 get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
2141 unset($newblockinstances);
2143 upgrade_main_savepoint(true, 2009082800);
2144 // The end of the navigation upgrade
2147 if ($oldversion < 2009100602) {
2148 /// Define table external_functions to be created
2149 $table = new xmldb_table('external_functions');
2151 /// Adding fields to table external_functions
2152 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2153 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2154 $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2155 $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2156 $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2157 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2159 /// Adding keys to table external_functions
2160 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2162 /// Adding indexes to table external_functions
2163 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2165 /// Launch create table for external_functions
2166 $dbman->create_table($table);
2168 /// Main savepoint reached
2169 upgrade_main_savepoint(true, 2009100602);
2172 if ($oldversion < 2009100603) {
2173 /// Define table external_services to be created
2174 $table = new xmldb_table('external_services');
2176 /// Adding fields to table external_services
2177 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2178 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2179 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2180 $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
2181 $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2182 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
2183 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2184 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2186 /// Adding keys to table external_services
2187 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2189 /// Adding indexes to table external_services
2190 $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2192 /// Launch create table for external_services
2193 $dbman->create_table($table);
2195 /// Main savepoint reached
2196 upgrade_main_savepoint(true, 2009100603);
2199 if ($oldversion < 2009100604) {
2200 /// Define table external_services_functions to be created
2201 $table = new xmldb_table('external_services_functions');
2203 /// Adding fields to table external_services_functions
2204 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2205 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2206 $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2208 /// Adding keys to table external_services_functions
2209 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2210 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2212 /// Launch create table for external_services_functions
2213 $dbman->create_table($table);
2215 /// Main savepoint reached
2216 upgrade_main_savepoint(true, 2009100604);
2219 if ($oldversion < 2009100605) {
2220 /// Define table external_services_users to be created
2221 $table = new xmldb_table('external_services_users');
2223 /// Adding fields to table external_services_users
2224 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2225 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2226 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2227 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2228 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2229 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2231 /// Adding keys to table external_services_users
2232 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2233 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2234 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2236 /// Launch create table for external_services_users
2237 $dbman->create_table($table);
2239 /// Main savepoint reached
2240 upgrade_main_savepoint(true, 2009100605);
2243 if ($oldversion < 2009102600) {
2245 /// Define table external_tokens to be created
2246 $table = new xmldb_table('external_tokens');
2248 /// Adding fields to table external_tokens
2249 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2250 $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
2251 $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2252 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2253 $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2254 $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
2255 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2256 $table->add_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
2257 $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2258 $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2259 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2260 $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2262 /// Adding keys to table external_tokens
2263 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2264 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2265 $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2266 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2267 $table->add_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
2269 /// Launch create table for external_tokens
2270 $dbman->create_table($table);
2272 /// Main savepoint reached
2273 upgrade_main_savepoint(true, 2009102600);
2276 if ($oldversion < 2009103000) {
2278 /// Define table blog_association to be created
2279 $table = new xmldb_table('blog_association');
2281 /// Adding fields to table blog_association
2282 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2283 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2284 $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2286 /// Adding keys to table blog_association
2287 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2288 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2289 $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
2291 /// Conditionally launch create table for blog_association
2292 if (!$dbman->table_exists($table)) {
2293 $dbman->create_table($table);
2296 /// Define table blog_external to be created
2297 $table = new xmldb_table('blog_external');
2299 /// Adding fields to table blog_external
2300 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2301 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2302 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2303 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2304 $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2305 $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2306 $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2307 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2308 $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2310 /// Adding keys to table blog_external
2311 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2312 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2314 /// Conditionally launch create table for blog_external
2315 if ($dbman->table_exists($table)) {
2316 // Delete the existing one first (comes from early dev version)
2317 $dbman->drop_table($table);
2319 $dbman->create_table($table);
2321 // upgrade notice is now in admin/tool/bloglevelupgrade/
2323 /// Main savepoint reached
2324 upgrade_main_savepoint(true, 2009103000);
2327 if ($oldversion < 2009110400) {
2328 // list of tables where we need to add new format field and convert texts
2329 $extendtables = array('course' => 'summary',
2330 'course_categories' => 'description',
2331 'course_categories' => 'description',
2332 'course_request' => 'summary',
2333 'grade_outcomes' => 'description',
2334 'groups' => 'description',
2335 'groupings' => 'description',
2336 'scale' => 'description',
2337 'user_info_field' => 'description',
2338 'user_info_field' => 'defaultdata',
2339 'user_info_data' => 'data');
2341 foreach ($extendtables as $tablestr => $fieldstr) {
2342 $formatfieldstr = $fieldstr.'format';
2344 $table = new xmldb_table($tablestr);
2345 $field = new xmldb_field($formatfieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', $fieldstr);
2346 // Check that the field doesn't already exists
2347 if (!$dbman->field_exists($table, $field)) {
2348 // Add the new field
2349 $dbman->add_field($table, $field);
2351 if ($CFG->texteditors !== 'textarea') {
2352 $rs = $DB->get_recordset($tablestr, array($formatfieldstr => FORMAT_MOODLE), '', "id,$fieldstr,$formatfieldstr");
2353 foreach ($rs as $rec) {
2354 $rec->$fieldstr = text_to_html($rec->$fieldstr, false, false, true);
2355 $rec->$formatfieldstr = FORMAT_HTML;
2356 $DB->update_record($tablestr, $rec);
2357 upgrade_set_timeout();
2359 $rs->close();
2360 unset($rs);
2364 unset($rec);
2365 unset($extendtables);
2367 upgrade_main_savepoint(true, 2009110400);
2370 if ($oldversion < 2009110401) {
2371 $table = new xmldb_table('user');
2373 // Change the precision of the description field first up.
2374 // This may grow!
2375 $field = new xmldb_field('description', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'url');
2376 $dbman->change_field_precision($table, $field);
2378 $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'description');
2379 // Check that the field doesn't already exists
2380 if (!$dbman->field_exists($table, $field)) {
2381 // Add the new field
2382 $dbman->add_field($table, $field);
2384 if ($CFG->texteditors !== 'textarea') {
2385 $rs = $DB->get_recordset('user', array('descriptionformat'=>FORMAT_MOODLE, 'deleted'=>0, 'htmleditor'=>1), '', "id,description,descriptionformat");
2386 foreach ($rs as $rec) {
2387 $rec->description = text_to_html($rec->description, false, false, true);
2388 $rec->descriptionformat = FORMAT_HTML;
2389 $DB->update_record('user', $rec);
2390 upgrade_set_timeout();
2392 $rs->close();
2395 upgrade_main_savepoint(true, 2009110401);
2398 if ($oldversion < 2009112400) {
2399 if (empty($CFG->passwordsaltmain)) {
2400 $subject = get_string('check_passwordsaltmain_name', 'report_security');
2401 $description = get_string('check_passwordsaltmain_warning', 'report_security');;
2402 upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2404 upgrade_main_savepoint(true, 2009112400);
2407 if ($oldversion < 2010011200) {
2408 $table = new xmldb_table('grade_categories');
2409 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timemodified');
2411 if (!$dbman->field_exists($table, $field)) {
2412 $dbman->add_field($table, $field);
2415 upgrade_main_savepoint(true, 2010011200);
2418 if ($oldversion < 2010012500) {
2419 upgrade_fix_incorrect_mnethostids();
2420 upgrade_main_savepoint(true, 2010012500);
2423 if ($oldversion < 2010012600) {
2424 // do stuff to the mnet table
2425 $table = new xmldb_table('mnet_rpc');
2427 $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2428 $dbman->rename_field($table, $field, 'plugintype');
2430 $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2431 $dbman->rename_field($table, $field, 'pluginname');
2433 $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
2434 if (!$dbman->field_exists($table, $field)) {
2435 $dbman->add_field($table, $field);
2438 $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
2439 if (!$dbman->field_exists($table, $field)) {
2440 $dbman->add_field($table, $field);
2443 $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
2444 if (!$dbman->field_exists($table, $field)) {
2445 $dbman->add_field($table, $field);
2448 /// Main savepoint reached
2449 upgrade_main_savepoint(true, 2010012600);
2452 if ($oldversion < 2010012900) {
2454 /// Define table mnet_remote_rpc to be created
2455 $table = new xmldb_table('mnet_remote_rpc');
2457 /// Adding fields to table mnet_remote_rpc
2458 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2459 $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2460 $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
2462 /// Adding keys to table mnet_remote_rpc
2463 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2465 /// Conditionally launch create table for mnet_remote_rpc
2466 if (!$dbman->table_exists($table)) {
2467 $dbman->create_table($table);
2471 /// Define table mnet_remote_service2rpc to be created
2472 $table = new xmldb_table('mnet_remote_service2rpc');
2474 /// Adding fields to table mnet_remote_service2rpc
2475 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2476 $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2477 $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2479 /// Adding keys to table mnet_remote_service2rpc
2480 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2482 /// Adding indexes to table mnet_remote_service2rpc
2483 $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2485 /// Conditionally launch create table for mnet_remote_service2rpc
2486 if (!$dbman->table_exists($table)) {
2487 $dbman->create_table($table);
2491 /// Rename field function_name on table mnet_rpc to functionname
2492 $table = new xmldb_table('mnet_rpc');
2493 $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
2495 /// Launch rename field function_name
2496 $dbman->rename_field($table, $field, 'functionname');
2499 /// Rename field xmlrpc_path on table mnet_rpc to xmlrpcpath
2500 $table = new xmldb_table('mnet_rpc');
2501 $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
2503 /// Launch rename field xmlrpc_path
2504 $dbman->rename_field($table, $field, 'xmlrpcpath');
2507 /// Main savepoint reached
2508 upgrade_main_savepoint(true, 2010012900);
2511 if ($oldversion < 2010012901) {
2513 /// Define field plugintype to be added to mnet_remote_rpc
2514 $table = new xmldb_table('mnet_remote_rpc');
2515 $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
2517 /// Conditionally launch add field plugintype
2518 if (!$dbman->field_exists($table, $field)) {
2519 $dbman->add_field($table, $field);
2522 /// Define field pluginname to be added to mnet_remote_rpc
2523 $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
2525 /// Conditionally launch add field pluginname
2526 if (!$dbman->field_exists($table, $field)) {
2527 $dbman->add_field($table, $field);
2530 /// Main savepoint reached
2531 upgrade_main_savepoint(true, 2010012901);
2534 if ($oldversion < 2010012902) {
2536 /// Define field enabled to be added to mnet_remote_rpc
2537 $table = new xmldb_table('mnet_remote_rpc');
2538 $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
2540 /// Conditionally launch add field enabled
2541 if (!$dbman->field_exists($table, $field)) {
2542 $dbman->add_field($table, $field);
2545 /// Main savepoint reached
2546 upgrade_main_savepoint(true, 2010012902);
2549 /// MDL-17863. Increase the portno column length on mnet_host to handle any port number
2550 if ($oldversion < 2010020100) {
2551 /// Changing precision of field portno on table mnet_host to (5)
2552 $table = new xmldb_table('mnet_host');
2553 $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
2555 /// Launch change of precision for field portno
2556 $dbman->change_field_precision($table, $field);
2558 upgrade_main_savepoint(true, 2010020100);
2561 if ($oldversion < 2010020300) {
2563 /// Define field timecreated to be added to user
2564 $table = new xmldb_table('user');
2565 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
2567 if (!$dbman->field_exists($table, $field)) {
2568 /// Launch add field timecreated
2569 $dbman->add_field($table, $field);
2571 $DB->execute("UPDATE {user} SET timecreated = firstaccess");
2573 $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
2574 $DB->execute($sql);
2576 upgrade_main_savepoint(true, 2010020300);
2579 // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
2580 if ($oldversion < 2010020301) {
2581 if ($preamble = $CFG->filter_tex_latexpreamble) {
2582 $preamble = preg_replace('/^ +/m', '', $preamble);
2583 set_config('filter_tex_latexpreamble', $preamble);
2585 upgrade_main_savepoint(true, 2010020301);
2588 if ($oldversion < 2010021400) {
2589 /// Changes to modinfo mean we need to rebuild course cache
2590 require_once($CFG->dirroot . '/course/lib.php');
2591 rebuild_course_cache(0, true);
2592 upgrade_main_savepoint(true, 2010021400);
2595 if ($oldversion < 2010021800) {
2596 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
2597 upgrade_main_savepoint(true, 2010021800);
2600 if ($oldversion < 2010031900) {
2601 // regeneration of sessions is always enabled, no need for this setting any more
2602 unset_config('regenloginsession');
2603 upgrade_main_savepoint(true, 2010031900);
2606 if ($oldversion < 2010033101.02) {
2608 /// Define table license to be created
2609 $table = new xmldb_table('license');
2611 /// Adding fields to table license
2612 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2613 $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2614 $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2615 $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2616 $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
2617 $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2619 /// Adding keys to table license
2620 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2622 /// Conditionally launch create table for license
2623 if (!$dbman->table_exists($table)) {
2624 $dbman->create_table($table);
2626 $active_licenses = array();
2628 $license = new stdClass();
2630 // add unknown license
2631 $license->shortname = 'unknown';
2632 $license->fullname = 'Unknown license';
2633 $license->source = '';
2634 $license->enabled = 1;
2635 $license->version = '2010033100';
2636 $active_licenses[] = $license->shortname;
2637 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2638 if ($record->version < $license->version) {
2639 // update license record
2640 $license->enabled = $record->enabled;
2641 $license->id = $record->id;
2642 $DB->update_record('license', $license);
2644 } else {
2645 $DB->insert_record('license', $license);
2648 // add all rights reserved license
2649 $license->shortname = 'allrightsreserved';
2650 $license->fullname = 'All rights reserved';
2651 $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
2652 $license->enabled = 1;
2653 $license->version = '2010033100';
2654 $active_licenses[] = $license->shortname;
2655 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2656 if ($record->version < $license->version) {
2657 // update license record
2658 $license->id = $record->id;
2659 $license->enabled = $record->enabled;
2660 $DB->update_record('license', $license);
2662 } else {
2663 $DB->insert_record('license', $license);
2666 // add public domain license
2667 $license->shortname = 'public';
2668 $license->fullname = 'Public Domain';
2669 $license->source = 'http://creativecommons.org/licenses/publicdomain/';
2670 $license->enabled = 1;
2671 $license->version = '2010033100';
2672 $active_licenses[] = $license->shortname;
2673 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2674 if ($record->version < $license->version) {
2675 // update license record
2676 $license->enabled = $record->enabled;
2677 $license->id = $record->id;
2678 $DB->update_record('license', $license);
2680 } else {
2681 $DB->insert_record('license', $license);
2684 // add creative commons license
2685 $license->shortname = 'cc';
2686 $license->fullname = 'Creative Commons';
2687 $license->source = 'http://creativecommons.org/licenses/by/3.0/';
2688 $license->enabled = 1;
2689 $license->version = '2010033100';
2690 $active_licenses[] = $license->shortname;
2691 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2692 if ($record->version < $license->version) {
2693 // update license record
2694 $license->enabled = $record->enabled;
2695 $license->id = $record->id;
2696 $DB->update_record('license', $license);
2698 } else {
2699 $DB->insert_record('license', $license);
2702 // add creative commons no derivs license
2703 $license->shortname = 'cc-nd';
2704 $license->fullname = 'Creative Commons - NoDerivs';
2705 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
2706 $license->enabled = 1;
2707 $license->version = '2010033100';
2708 $active_licenses[] = $license->shortname;
2709 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2710 if ($record->version < $license->version) {
2711 // update license record
2712 $license->enabled = $record->enabled;
2713 $license->id = $record->id;
2714 $DB->update_record('license', $license);
2716 } else {
2717 $DB->insert_record('license', $license);
2720 // add creative commons no commercial no derivs license
2721 $license->shortname = 'cc-nc-nd';
2722 $license->fullname = 'Creative Commons - No Commercial NoDerivs';
2723 $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
2724 $license->enabled = 1;
2725 $license->version = '2010033100';
2726 $active_licenses[] = $license->shortname;
2727 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2728 if ($record->version < $license->version) {
2729 // update license record
2730 $license->enabled = $record->enabled;
2731 $license->id = $record->id;
2732 $DB->update_record('license', $license);
2734 } else {
2735 $DB->insert_record('license', $license);
2738 // add creative commons no commercial
2739 $license->shortname = 'cc-nc-nd';
2740 $license->shortname = 'cc-nc';
2741 $license->fullname = 'Creative Commons - No Commercial';
2742 $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
2743 $license->enabled = 1;
2744 $license->version = '2010033100';
2745 $active_licenses[] = $license->shortname;
2746 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2747 if ($record->version < $license->version) {
2748 // update license record
2749 $license->enabled = $record->enabled;
2750 $license->id = $record->id;
2751 $DB->update_record('license', $license);
2753 } else {
2754 $DB->insert_record('license', $license);
2757 // add creative commons no commercial sharealike
2758 $license->shortname = 'cc-nc-sa';
2759 $license->fullname = 'Creative Commons - No Commercial ShareAlike';
2760 $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
2761 $license->enabled = 1;
2762 $license->version = '2010033100';
2763 $active_licenses[] = $license->shortname;
2764 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2765 if ($record->version < $license->version) {
2766 // update license record
2767 $license->enabled = $record->enabled;
2768 $license->id = $record->id;
2769 $DB->update_record('license', $license);
2771 } else {
2772 $DB->insert_record('license', $license);
2775 // add creative commons sharealike
2776 $license->shortname = 'cc-sa';
2777 $license->fullname = 'Creative Commons - ShareAlike';
2778 $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
2779 $license->enabled = 1;
2780 $license->version = '2010033100';
2781 $active_licenses[] = $license->shortname;
2782 if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2783 if ($record->version < $license->version) {
2784 // update license record
2785 $license->enabled = $record->enabled;
2786 $license->id = $record->id;
2787 $DB->update_record('license', $license);
2789 } else {
2790 $DB->insert_record('license', $license);
2793 set_config('licenses', implode(',', $active_licenses));
2794 /// set site default license
2795 set_config('sitedefaultlicense', 'allrightsreserved');
2797 /// Main savepoint reached
2798 upgrade_main_savepoint(true, 2010033101.02);
2801 if ($oldversion < 2010033102.00) {
2802 // rename course view capability to participate
2803 $params = array('viewcap'=>'moodle/course:view', 'participatecap'=>'moodle/course:participate');
2804 $sql = "UPDATE {role_capabilities} SET capability = :participatecap WHERE capability = :viewcap";
2805 $DB->execute($sql, $params);
2806 $sql = "UPDATE {capabilities} SET name = :participatecap WHERE name = :viewcap";
2807 $DB->execute($sql, $params);
2808 // note: the view capability is readded again at the end of upgrade, but with different meaning
2809 upgrade_main_savepoint(true, 2010033102.00);
2812 if ($oldversion < 2010033102.01) {
2813 // Define field archetype to be added to role table
2814 $table = new xmldb_table('role');
2815 $field = new xmldb_field('archetype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'sortorder');
2816 $dbman->add_field($table, $field);
2817 upgrade_main_savepoint(true, 2010033102.01);
2820 if ($oldversion < 2010033102.02) {
2821 // Set archetype for existing roles and change admin role to manager role
2822 $sql = "SELECT r.*, rc.capability
2823 FROM {role} r
2824 JOIN {role_capabilities} rc ON rc.roleid = r.id
2825 WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
2826 ORDER BY r.id";
2827 $params = array('syscontextid'=>SYSCONTEXTID, 'legacycaps'=>'moodle/legacy:%');
2828 $substart = strlen('moodle/legacy:');
2829 $roles = $DB->get_recordset_sql($sql, $params); // in theory could be multiple legacy flags in one role
2830 foreach ($roles as $role) {
2831 $role->archetype = substr($role->capability, $substart);
2832 unset($role->capability);
2833 if ($role->archetype === 'admin') {
2834 $i = '';
2835 if ($DB->record_exists('role', array('shortname'=>'manager')) or $DB->record_exists('role', array('name'=>get_string('manager', 'role')))) {
2836 $i = 2;
2837 while($DB->record_exists('role', array('shortname'=>'manager'.$i)) or $DB->record_exists('role', array('name'=>get_string('manager', 'role').$i))) {
2838 $i++;
2841 $role->archetype = 'manager';
2842 if ($role->shortname === 'admin') {
2843 $role->shortname = 'manager'.$i;
2844 $role->name = get_string('manager', 'role').$i;
2845 $role->description = get_string('managerdescription', 'role');
2848 $DB->update_record('role', $role);
2850 $roles->close();
2852 upgrade_main_savepoint(true, 2010033102.02);
2855 if ($oldversion < 2010033102.03) {
2856 // Now pick site admins (===have manager role assigned at the system context)
2857 // and store them in the new $CFG->siteadmins setting as comma separated list
2858 $sql = "SELECT ra.id, ra.userid
2859 FROM {role_assignments} ra
2860 JOIN {role} r ON r.id = ra.roleid
2861 JOIN {user} u ON u.id = ra.userid
2862 WHERE ra.contextid = :syscontext AND r.archetype = 'manager' AND u.deleted = 0
2863 ORDER BY ra.id";
2864 $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
2865 $admins = array();
2866 foreach ($ras as $ra) {
2867 $admins[$ra->userid] = $ra->userid;
2868 set_config('siteadmins', implode(',', $admins)); // better to save it repeatedly, we do need at least one admin
2869 $DB->delete_records('role_assignments', array('id'=>$ra->id));
2872 upgrade_main_savepoint(true, 2010033102.03);
2875 if ($oldversion < 2010033102.04) {
2876 // clean up the manager roles
2877 $managers = $DB->get_records('role', array('archetype'=>'manager'));
2878 foreach ($managers as $manager) {
2879 // now sanitize the capabilities and overrides
2880 $DB->delete_records('role_capabilities', array('capability'=>'moodle/site:config', 'roleid'=>$manager->id)); // only site admins may configure servers
2881 // note: doanything and legacy caps are deleted automatically, they get moodle/course:view later at the end of the upgrade
2883 // remove manager role assignments bellow the course context level - admin role was never intended for activities and blocks,
2884 // the problem is that those assignments would not be visible after upgrade and old style admins in activities make no sense anyway
2885 $DB->delete_records_select('role_assignments', "roleid = :manager AND contextid IN (SELECT id FROM {context} WHERE contextlevel > 50)", array('manager'=>$manager->id));
2887 // allow them to assign all roles except default user, guest and frontpage - users get these roles automatically on the fly when needed
2888 $DB->delete_records('role_allow_assign', array('roleid'=>$manager->id));
2889 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype <> 'user' AND archetype <> 'guest' AND archetype <> 'frontpage'");
2890 foreach ($roles as $role) {
2891 $record = (object)array('roleid'=>$manager->id, 'allowassign'=>$role->id);
2892 $DB->insert_record('role_allow_assign', $record);
2895 // allow them to override all roles
2896 $DB->delete_records('role_allow_override', array('roleid'=>$manager->id));
2897 $roles = $DB->get_records_sql("SELECT * FROM {role}");
2898 foreach ($roles as $role) {
2899 $record = (object)array('roleid'=>$manager->id, 'allowoverride'=>$role->id);
2900 $DB->insert_record('role_allow_override', $record);
2903 // allow them to switch to all following roles
2904 $DB->delete_records('role_allow_switch', array('roleid'=>$manager->id));
2905 $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype IN ('student', 'teacher', 'editingteacher')");
2906 foreach ($roles as $role) {
2907 $record = (object)array('roleid'=>$manager->id, 'allowswitch'=>$role->id);
2908 $DB->insert_record('role_allow_switch', $record);
2912 upgrade_main_savepoint(true, 2010033102.04);
2915 if ($oldversion < 2010033102.05) {
2916 // remove course:view from all roles that are not used for enrolment, it does NOT belong there because it really means user is enrolled!
2917 $noenrolroles = $DB->get_records_select('role', "archetype IN ('guest', 'user', 'manager', 'coursecreator', 'frontpage')");
2918 foreach ($noenrolroles as $role) {
2919 $DB->delete_records('role_capabilities', array('roleid'=>$role->id, 'capability'=>'moodle/course:participate'));
2921 upgrade_main_savepoint(true, 2010033102.05);
2924 if ($oldversion < 2010033102.06) {
2925 // make sure there is nothing weird in default user role
2926 if (!empty($CFG->defaultuserroleid)) {
2927 if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
2928 if ($role->archetype !== '' and $role->archetype !== 'user') {
2929 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default authenticated user role (defaultuserroleid) value is invalid, setting cleared.');
2930 unset_config('defaultuserroleid');
2932 } else {
2933 unset_config('defaultuserroleid');
2936 upgrade_main_savepoint(true, 2010033102.06);
2939 if ($oldversion < 2010033102.07) {
2940 if (!empty($CFG->displayloginfailures) and $CFG->displayloginfailures === 'teacher') {
2941 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Displaying of login failuters to teachers is not supported any more.');
2942 unset_config('displayloginfailures');
2944 upgrade_main_savepoint(true, 2010033102.07);
2947 if ($oldversion < 2010033102.08) {
2948 // make sure there are no problems in default guest role settings
2949 if (!empty($CFG->guestroleid)) {
2950 if ($role = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
2951 if ($role->archetype !== '' and $role->archetype !== 'guest') {
2952 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default guest role (guestroleid) value is invalid, setting cleared.');
2953 unset_config('guestroleid');
2955 } else {
2956 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) does not exist, setting cleared.');
2957 unset_config('guestroleid');
2960 // remove all roles of the guest account - the only way to change it is to override the guest role, sorry
2961 // the guest account gets all the role assignments on the fly which works fine in has_capability(),
2962 $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE username = 'guest')");
2964 upgrade_main_savepoint(true, 2010033102.08);
2967 /// New table for storing which roles can be assigned in which contexts.
2968 if ($oldversion < 2010033102.09) {
2970 /// Define table role_context_levels to be created
2971 $table = new xmldb_table('role_context_levels');
2973 /// Adding fields to table role_context_levels
2974 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2975 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2976 $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2978 /// Adding keys to table role_context_levels
2979 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2980 $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
2981 $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
2983 /// Conditionally launch create table for role_context_levels
2984 if (!$dbman->table_exists($table)) {
2985 $dbman->create_table($table);
2988 /// Main savepoint reached
2989 upgrade_main_savepoint(true, 2010033102.09);
2992 if ($oldversion < 2010033102.10) {
2993 // Now populate the role_context_levels table with the default values
2994 // NOTE: do not use accesslib methods here
2996 $rolecontextlevels = array();
2997 $defaults = array('manager' => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE),
2998 'coursecreator' => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT),
2999 'editingteacher' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3000 'teacher' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3001 'student' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3002 'guest' => array(),
3003 'user' => array(),
3004 'frontpage' => array());
3006 $roles = $DB->get_records('role', array(), '', 'id, archetype');
3007 foreach ($roles as $role) {
3008 if (isset($defaults[$role->archetype])) {
3009 $rolecontextlevels[$role->id] = $defaults[$role->archetype];
3013 // add roles without archetypes, it may contain weird things, but we can not fix them
3014 list($narsql, $params) = $DB->get_in_or_equal(array_keys($defaults), SQL_PARAMS_NAMED, 'ar', false);
3015 $sql = "SELECT DISTINCT ra.roleid, con.contextlevel
3016 FROM {role_assignments} ra
3017 JOIN {context} con ON ra.contextid = con.id
3018 JOIN {role} r ON r.id = ra.roleid
3019 WHERE r.archetype $narsql";
3020 $existingrolecontextlevels = $DB->get_recordset_sql($sql, $params);
3021 foreach ($existingrolecontextlevels as $rcl) {
3022 if (!isset($rolecontextlevels[$rcl->roleid])) {
3023 $rolecontextlevels[$rcl->roleid] = array();
3025 $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
3027 $existingrolecontextlevels->close();
3029 // Put the data into the database.
3030 $rcl = new stdClass();
3031 foreach ($rolecontextlevels as $roleid => $contextlevels) {
3032 $rcl->roleid = $roleid;
3033 foreach ($contextlevels as $level) {
3034 $rcl->contextlevel = $level;
3035 $DB->insert_record('role_context_levels', $rcl, false);
3039 // release memory!!
3040 unset($roles);
3041 unset($defaults);
3042 unset($rcl);
3043 unset($existingrolecontextlevels);
3044 unset($rolecontextlevels);
3046 // Main savepoint reached
3047 upgrade_main_savepoint(true, 2010033102.10);
3050 if ($oldversion < 2010040700) {
3051 // migrate old groupings --> groupmembersonly setting
3052 if (isset($CFG->enablegroupings)) {
3053 set_config('enablegroupmembersonly', $CFG->enablegroupings);
3054 unset_config('enablegroupings');
3057 // Main savepoint reached
3058 upgrade_main_savepoint(true, 2010040700);
3061 if ($oldversion < 2010040900) {
3063 // Changing the default of field lang on table user to good old "en"
3064 $table = new xmldb_table('user');
3065 $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en', 'country');
3067 // Launch change of default for field lang
3068 $dbman->change_field_default($table, $field);
3070 // update main site lang
3071 if (strpos($CFG->lang, '_utf8') !== false) {
3072 $lang = str_replace('_utf8', '', $CFG->lang);
3073 set_config('lang', $lang);
3076 // tweak langlist
3077 if (!empty($CFG->langlist)) {
3078 $langs = explode(',', $CFG->langlist);
3079 foreach ($langs as $key=>$lang) {
3080 $lang = str_replace('_utf8', '', $lang);
3081 $langs[$key] = $lang;
3083 set_config('langlist', implode(',', $langs));
3086 // Main savepoint reached
3087 upgrade_main_savepoint(true, 2010040900);
3090 if ($oldversion < 2010040901) {
3092 // Remove "_utf8" suffix from all langs in user table
3093 $langs = $DB->get_records_sql("SELECT DISTINCT lang FROM {user} WHERE lang LIKE ?", array('%_utf8'));
3095 foreach ($langs as $lang=>$unused) {
3096 $newlang = str_replace('_utf8', '', $lang);
3097 $sql = "UPDATE {user} SET lang = :newlang WHERE lang = :lang";
3098 $DB->execute($sql, array('newlang'=>$newlang, 'lang'=>$lang));
3101 // Main savepoint reached
3102 upgrade_main_savepoint(true, 2010040901);
3105 if ($oldversion < 2010041301) {
3106 $sql = "UPDATE {block} SET name=? WHERE name=?";
3107 $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3108 $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3110 $sql = "UPDATE {block_instances} SET blockname=? WHERE blockname=?";
3111 $DB->execute($sql, array('navigation', 'global_navigation_tree'));
3112 $DB->execute($sql, array('settings', 'settings_navigation_tree'));
3113 upgrade_main_savepoint(true, 2010041301);
3116 if ($oldversion < 2010042100) {
3118 /// Define table backup_controllers to be created
3119 $table = new xmldb_table('backup_controllers');
3121 /// Adding fields to table backup_controllers
3122 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3123 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3124 $table->add_field('type', XMLDB_TYPE_CHAR, '6', null, XMLDB_NOTNULL, null, null);
3125 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3126 $table->add_field('format', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
3127 $table->add_field('interactive', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3128 $table->add_field('purpose', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3129 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3130 $table->add_field('status', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3131 $table->add_field('execution', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3132 $table->add_field('executiontime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3133 $table->add_field('checksum', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3134 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3135 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3136 $table->add_field('controller', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
3138 /// Adding keys to table backup_controllers
3139 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3140 $table->add_key('backupid_uk', XMLDB_KEY_UNIQUE, array('backupid'));
3141 $table->add_key('userid_fk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3143 /// Adding indexes to table backup_controllers
3144 $table->add_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3146 /// Conditionally launch create table for backup_controllers
3147 if (!$dbman->table_exists($table)) {
3148 $dbman->create_table($table);
3151 /// Define table backup_ids_template to be created
3152 $table = new xmldb_table('backup_ids_template');
3154 /// Adding fields to table backup_ids_template
3155 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3156 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3157 $table->add_field('itemname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
3158 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3159 $table->add_field('parentitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3161 /// Adding keys to table backup_ids_template
3162 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3163 $table->add_key('backupid_itemname_itemid_uk', XMLDB_KEY_UNIQUE, array('backupid', 'itemname', 'itemid'));
3165 /// Adding indexes to table backup_ids_template
3166 $table->add_index('backupid_parentitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'parentitemid'));
3168 /// Conditionally launch create table for backup_controllers
3169 if (!$dbman->table_exists($table)) {
3170 $dbman->create_table($table);
3173 /// Main savepoint reached
3174 upgrade_main_savepoint(true, 2010042100);
3177 if ($oldversion < 2010042301) {
3179 $table = new xmldb_table('course_sections');
3180 $field = new xmldb_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'section');
3182 if (!$dbman->field_exists($table, $field)) {
3183 $dbman->add_field($table, $field);
3186 upgrade_main_savepoint(true, 2010042301);
3189 if ($oldversion < 2010042302) {
3190 // Define table cohort to be created
3191 $table = new xmldb_table('cohort');
3193 // Adding fields to table cohort
3194 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3195 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3196 $table->add_field('name', XMLDB_TYPE_CHAR, '254', null, XMLDB_NOTNULL, null, null);
3197 $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3198 $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
3199 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3200 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
3201 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3202 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3204 // Adding keys to table cohort
3205 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3206 $table->add_key('context', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3208 // Conditionally launch create table for cohort
3209 if (!$dbman->table_exists($table)) {
3210 $dbman->create_table($table);
3213 upgrade_main_savepoint(true, 2010042302);
3216 if ($oldversion < 2010042303) {
3217 // Define table cohort_members to be created
3218 $table = new xmldb_table('cohort_members');
3220 // Adding fields to table cohort_members
3221 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3222 $table->add_field('cohortid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3223 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3224 $table->add_field('timeadded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3226 // Adding keys to table cohort_members
3227 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3228 $table->add_key('cohortid', XMLDB_KEY_FOREIGN, array('cohortid'), 'cohort', array('id'));
3229 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3231 // Adding indexes to table cohort_members
3232 $table->add_index('cohortid-userid', XMLDB_INDEX_UNIQUE, array('cohortid', 'userid'));
3234 // Conditionally launch create table for cohort_members
3235 if (!$dbman->table_exists($table)) {
3236 $dbman->create_table($table);
3239 // Main savepoint reached
3240 upgrade_main_savepoint(true, 2010042303);
3243 if ($oldversion < 2010042800) {
3244 //drop the previously created ratings table
3245 $table = new xmldb_table('ratings');
3246 if ($dbman->table_exists($table)) {
3247 $dbman->drop_table($table);
3250 //create the rating table (replaces module specific rating implementations)
3251 $table = new xmldb_table('rating');
3252 if ($dbman->table_exists($table)) {
3253 $dbman->drop_table($table);
3256 /// Adding fields to table rating
3257 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3258 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3260 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3261 $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
3262 $table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3263 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3265 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3266 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3268 /// Adding keys to table rating
3269 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3270 $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3271 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3273 /// Adding indexes to table rating
3274 $table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
3276 /// Create table for ratings
3277 if (!$dbman->table_exists($table)) {
3278 $dbman->create_table($table);
3281 upgrade_main_savepoint(true, 2010042800);
3284 if ($oldversion < 2010042801) {
3285 // migrating old comments block content
3286 $DB->execute("UPDATE {comments}
3287 SET contextid = (SELECT parentcontextid
3288 FROM {block_instances}
3289 WHERE id = {comments}.itemid AND blockname = 'comments'),
3290 commentarea = 'page_comments',
3291 itemid = 0
3292 WHERE commentarea = 'block_comments'
3293 AND itemid != 0
3294 AND EXISTS (SELECT 'x'
3295 FROM {block_instances}
3296 WHERE id = {comments}.itemid
3297 AND blockname = 'comments')");
3299 // remove all orphaned record
3300 $DB->delete_records('comments', array('commentarea'=>'block_comments'));
3301 upgrade_main_savepoint(true, 2010042801);
3304 if ($oldversion < 2010042802) { // Change backup_controllers->type to varchar10 (recreate dep. index)
3306 /// Define index typeitem_ix (not unique) to be dropped form backup_controllers
3307 $table = new xmldb_table('backup_controllers');
3308 $index = new xmldb_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3310 /// Conditionally launch drop index typeitem_ix
3311 if ($dbman->index_exists($table, $index)) {
3312 $dbman->drop_index($table, $index);
3315 /// Changing precision of field type on table backup_controllers to (10)
3316 $table = new xmldb_table('backup_controllers');
3317 $field = new xmldb_field('type', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'backupid');
3319 /// Launch change of precision for field type
3320 $dbman->change_field_precision($table, $field);
3322 /// Define index typeitem_ix (not unique) to be added to backup_controllers
3323 $table = new xmldb_table('backup_controllers');
3324 $index = new xmldb_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
3326 /// Conditionally launch add index typeitem_ix
3327 if (!$dbman->index_exists($table, $index)) {
3328 $dbman->add_index($table, $index);
3331 /// Main savepoint reached
3332 upgrade_main_savepoint(true, 2010042802);
3335 if ($oldversion < 2010043000) { // Adding new course completion feature
3337 /// Add course completion tables
3338 /// Define table course_completion_aggr_methd to be created
3339 $table = new xmldb_table('course_completion_aggr_methd');
3341 /// Adding fields to table course_completion_aggr_methd
3342 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3343 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3344 $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, null);
3345 $table->add_field('method', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3346 $table->add_field('value', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3348 /// Adding keys to table course_completion_aggr_methd
3349 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3351 /// Adding indexes to table course_completion_aggr_methd
3352 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3353 $table->add_index('criteriatype', XMLDB_INDEX_NOTUNIQUE, array('criteriatype'));
3355 /// Conditionally launch create table for course_completion_aggr_methd
3356 if (!$dbman->table_exists($table)) {
3357 $dbman->create_table($table);
3361 /// Define table course_completion_criteria to be created
3362 $table = new xmldb_table('course_completion_criteria');
3364 /// Adding fields to table course_completion_criteria
3365 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3366 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3367 $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3368 $table->add_field('module', XMLDB_TYPE_CHAR, '100', null, null, null, null);
3369 $table->add_field('moduleinstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3370 $table->add_field('courseinstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3371 $table->add_field('enrolperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3372 $table->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3373 $table->add_field('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3374 $table->add_field('role', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3376 /// Adding keys to table course_completion_criteria
3377 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3379 /// Adding indexes to table course_completion_criteria
3380 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3382 /// Conditionally launch create table for course_completion_criteria
3383 if (!$dbman->table_exists($table)) {
3384 $dbman->create_table($table);
3388 /// Define table course_completion_crit_compl to be created
3389 $table = new xmldb_table('course_completion_crit_compl');
3391 /// Adding fields to table course_completion_crit_compl
3392 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3393 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3394 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3395 $table->add_field('criteriaid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3396 $table->add_field('gradefinal', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
3397 $table->add_field('unenroled', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3398 $table->add_field('deleted', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
3399 $table->add_field('timecompleted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3401 /// Adding keys to table course_completion_crit_compl
3402 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3404 /// Adding indexes to table course_completion_crit_compl
3405 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
3406 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3407 $table->add_index('criteriaid', XMLDB_INDEX_NOTUNIQUE, array('criteriaid'));
3408 $table->add_index('timecompleted', XMLDB_INDEX_NOTUNIQUE, array('timecompleted'));
3410 /// Conditionally launch create table for course_completion_crit_compl
3411 if (!$dbman->table_exists($table)) {
3412 $dbman->create_table($table);
3416 /// Define table course_completion_notify to be created
3417 $table = new xmldb_table('course_completion_notify');
3419 /// Adding fields to table course_completion_notify
3420 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3421 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3422 $table->add_field('role', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3423 $table->add_field('message', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
3424 $table->add_field('timesent', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3426 /// Adding keys to table course_completion_notify
3427 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3429 /// Adding indexes to table course_completion_notify
3430 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3432 /// Conditionally launch create table for course_completion_notify
3433 if (!$dbman->table_exists($table)) {
3434 $dbman->create_table($table);
3437 /// Define table course_completions to be created
3438 $table = new xmldb_table('course_completions');
3440 /// Adding fields to table course_completions
3441 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3442 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3443 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3444 $table->add_field('deleted', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
3445 $table->add_field('timenotified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3446 $table->add_field('timeenrolled', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3447 $table->add_field('timestarted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3448 $table->add_field('timecompleted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
3449 $table->add_field('reaggregate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3451 /// Adding keys to table course_completions
3452 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3454 /// Adding indexes to table course_completions
3455 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
3456 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
3457 $table->add_index('timecompleted', XMLDB_INDEX_NOTUNIQUE, array('timecompleted'));
3459 /// Conditionally launch create table for course_completions
3460 if (!$dbman->table_exists($table)) {
3461 $dbman->create_table($table);
3465 /// Add cols to course table
3466 /// Define field enablecompletion to be added to course
3467 $table = new xmldb_table('course');
3468 $field = new xmldb_field('enablecompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
3470 /// Conditionally launch add field enablecompletion
3471 if (!$dbman->field_exists($table, $field)) {
3472 $dbman->add_field($table, $field);
3475 /// Define field completionstartonenrol to be added to course
3476 $field = new xmldb_field('completionstartonenrol', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'enablecompletion');
3478 /// Conditionally launch add field completionstartonenrol
3479 if (!$dbman->field_exists($table, $field)) {
3480 $dbman->add_field($table, $field);
3483 /// Define field completionnotify to be added to course
3484 $field = new xmldb_field('completionnotify', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'enablecompletion');
3486 /// Conditionally launch add field completionnotify
3487 if (!$dbman->field_exists($table, $field)) {
3488 $dbman->add_field($table, $field);
3491 upgrade_main_savepoint(true, 2010043000);
3494 if ($oldversion < 2010043001) {
3496 /// Define table registration_hubs to be created
3497 $table = new xmldb_table('registration_hubs');
3499 /// Adding fields to table registration_hubs
3500 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3501 $table->add_field('token', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
3502 $table->add_field('hubname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
3503 $table->add_field('huburl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
3504 $table->add_field('confirmed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3506 /// Adding keys to table registration_hubs
3507 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3509 /// Conditionally launch create table for registration_hubs
3510 if (!$dbman->table_exists($table)) {
3511 $dbman->create_table($table);
3514 /// Main savepoint reached
3515 upgrade_main_savepoint(true, 2010043001);
3518 if ($oldversion < 2010050200) {
3520 /// Define table backup_logs to be created
3521 $table = new xmldb_table('backup_logs');
3523 /// Adding fields to table backup_logs
3524 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3525 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
3526 $table->add_field('loglevel', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3527 $table->add_field('message', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
3528 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3530 /// Adding keys to table backup_logs
3531 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3532 $table->add_key('backupid', XMLDB_KEY_FOREIGN, array('backupid'), 'backup_controllers', array('backupid'));
3534 /// Adding indexes to table backup_logs
3535 $table->add_index('backupid-id', XMLDB_INDEX_UNIQUE, array('backupid', 'id'));
3537 /// Conditionally launch create table for backup_logs
3538 if (!$dbman->table_exists($table)) {
3539 $dbman->create_table($table);
3542 /// Drop some old backup tables, not used anymore
3544 /// Define table backup_files to be dropped
3545 $table = new xmldb_table('backup_files');
3547 /// Conditionally launch drop table for backup_files
3548 if ($dbman->table_exists($table)) {
3549 $dbman->drop_table($table);
3552 /// Define table backup_ids to be dropped
3553 $table = new xmldb_table('backup_ids');
3555 /// Conditionally launch drop table for backup_ids
3556 if ($dbman->table_exists($table)) {
3557 $dbman->drop_table($table);
3560 /// Main savepoint reached
3561 upgrade_main_savepoint(true, 2010050200);
3564 if ($oldversion < 2010050403) { // my_pages for My Moodle and Public Profile pages
3566 /// Define table my_pages to be created
3567 $table = new xmldb_table('my_pages');
3569 /// Adding fields to table my_pages
3570 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3571 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 0);
3572 $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
3573 $table->add_field('private', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
3574 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, '0');
3577 /// Adding keys to table my_pages
3578 $table->add_key('id', XMLDB_KEY_PRIMARY, array('id'));
3580 /// Adding indexes to table my_pages
3581 $table->add_index('useridprivate', XMLDB_INDEX_NOTUNIQUE, array('userid', 'private'));
3583 /// Conditionally launch create table for my_pages
3584 if (!$dbman->table_exists($table)) {
3585 $dbman->create_table($table);
3588 /// Add two lines of data into this new table. These are the default pages.
3589 $mypage = new stdClass();
3590 $mypage->userid = NULL;
3591 $mypage->name = '__default';
3592 $mypage->private = 0;
3593 $mypage->sortorder = 0;
3594 if (!$DB->record_exists('my_pages', array('userid'=>NULL, 'private'=>0))) {
3595 $DB->insert_record('my_pages', $mypage);
3597 $mypage->private = 1;
3598 if (!$DB->record_exists('my_pages', array('userid'=>NULL, 'private'=>1))) {
3599 $DB->insert_record('my_pages', $mypage);
3602 /// This bit is a "illegal" hack, unfortunately, but there is not a better way to install default
3603 /// blocks right now, since the upgrade function need to be called after core AND plugins upgrade,
3604 /// and there is no such hook yet. Sigh.
3606 if ($mypage = $DB->get_record('my_pages', array('userid'=>NULL, 'private'=>1))) {
3607 if (!$DB->record_exists('block_instances', array('pagetypepattern'=>'my-index', 'parentcontextid'=>SITEID, 'subpagepattern'=>$mypage->id))) {
3609 // No default exist there yet, let's put a few into My Moodle so it's useful.
3611 $blockinstance = new stdClass;
3612 $blockinstance->parentcontextid = SYSCONTEXTID;
3613 $blockinstance->showinsubcontexts = 0;
3614 $blockinstance->pagetypepattern = 'my-index';
3615 $blockinstance->subpagepattern = $mypage->id;
3616 $blockinstance->configdata = '';
3618 $blockinstance->blockname = 'private_files';
3619 $blockinstance->defaultregion = 'side-post';
3620 $blockinstance->defaultweight = 0;
3621 $blockinstanceid = $DB->insert_record('block_instances', $blockinstance);
3622 get_context_instance(CONTEXT_BLOCK, $blockinstanceid);
3624 $blockinstance->blockname = 'online_users';
3625 $blockinstance->defaultregion = 'side-post';
3626 $blockinstance->defaultweight = 1;
3627 $blockinstanceid = $DB->insert_record('block_instances', $blockinstance);
3628 get_context_instance(CONTEXT_BLOCK, $blockinstanceid);
3630 $blockinstance->blockname = 'course_overview';
3631 $blockinstance->defaultregion = 'content';
3632 $blockinstance->defaultweight = 0;
3633 $blockinstanceid = $DB->insert_record('block_instances', $blockinstance);
3634 get_context_instance(CONTEXT_BLOCK, $blockinstanceid);
3638 /// Main savepoint reached
3639 upgrade_main_savepoint(true, 2010050403);
3642 if ($oldversion < 2010051500) {
3644 /// Fix a bad table name that existed for a few days in HEAD
3645 $table = new xmldb_table('published_courses');
3646 if ($dbman->table_exists($table)) {
3647 $dbman->drop_table($table);
3650 /// Define table course_published to be created
3651 $table = new xmldb_table('course_published');
3653 /// Adding fields to table course_published
3654 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3655 $table->add_field('hubid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3656 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3657 $table->add_field('timepublished', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3658 $table->add_field('enrollable', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
3659 $table->add_field('hubcourseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3661 /// Adding keys to table course_published
3662 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3664 /// Conditionally launch create table for course_published
3665 if (!$dbman->table_exists($table)) {
3666 $dbman->create_table($table);
3669 /// Main savepoint reached
3670 upgrade_main_savepoint(true, 2010051500);
3673 if ($oldversion < 2010051600) {
3675 /// Delete the blocks completely. All the contexts, instances etc were cleaned up above in 2009082800
3676 $DB->delete_records('block', array('name'=>'admin'));
3677 $DB->delete_records('block', array('name'=>'admin_tree'));
3679 /// Main savepoint reached
3680 upgrade_main_savepoint(true, 2010051600);
3683 if ($oldversion < 2010051800) {
3684 // switching to userid in config settings because user names are not unique and reliable enough
3685 if (!empty($CFG->courserequestnotify) and $CFG->courserequestnotify !== '$@NONE@$' and $CFG->courserequestnotify !== '$@ALL@$') {
3686 list($where, $params) = $DB->get_in_or_equal(explode(',', $CFG->courserequestnotify));
3687 $params[] = $CFG->mnet_localhost_id;
3688 $users = $DB->get_fieldset_select('user', 'id', "username $where AND mnethostid = ?", $params);
3689 if ($users) {
3690 set_config('courserequestnotify', implode(',', $users));
3691 } else {
3692 set_config('courserequestnotify', '$@NONE@$');
3695 upgrade_main_savepoint(true, 2010051800);
3698 if ($oldversion < 2010051801) {
3699 // Update the notifyloginfailures setting.
3700 if ($CFG->notifyloginfailures == 'mainadmin') {
3701 if ($admins = explode(',', $CFG->siteadmins)) {
3702 $adminid = reset($admins);
3703 set_config('notifyloginfailures', $adminid);
3704 } else {
3705 unset_config('notifyloginfailures'); // let them choose
3707 unset($admins);
3709 } else if ($CFG->notifyloginfailures == 'alladmins') {
3710 set_config('notifyloginfailures', '$@ALL@$');
3712 } else {
3713 set_config('notifyloginfailures', '$@NONE@$');
3716 upgrade_main_savepoint(true, 2010051801);
3719 if ($oldversion < 2010052200) {
3720 // Define field legacyfiles to be added to course - just in case we are upgrading from PR1
3721 $table = new xmldb_table('course');
3722 $field = new xmldb_field('legacyfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'maxbytes');
3724 // Conditionally launch add field legacyfiles
3725 if (!$dbman->field_exists($table, $field)) {
3726 $dbman->add_field($table, $field);
3727 // enable legacy files in all courses
3728 $DB->execute("UPDATE {course} SET legacyfiles = 2");
3731 // Main savepoint reached
3732 upgrade_main_savepoint(true, 2010052200);
3735 if ($oldversion < 2010052401) {
3737 /// Define field status to be added to course_published
3738 $table = new xmldb_table('course_published');
3739 $field = new xmldb_field('status', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0', 'hubcourseid');
3741 /// Conditionally launch add field status
3742 if (!$dbman->field_exists($table, $field)) {
3743 $dbman->add_field($table, $field);
3746 /// Define field timechecked to be added to course_published
3747 $table = new xmldb_table('course_published');
3748 $field = new xmldb_field('timechecked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'status');
3750 /// Conditionally launch add field timechecked
3751 if (!$dbman->field_exists($table, $field)) {
3752 $dbman->add_field($table, $field);
3755 /// Main savepoint reached
3756 upgrade_main_savepoint(true, 2010052401);
3759 if ($oldversion < 2010052700) {
3761 /// Define field summaryformat to be added to course sections table
3762 $table = new xmldb_table('course_sections');
3763 $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'summary');
3765 /// Conditionally launch add field summaryformat
3766 if (!$dbman->field_exists($table, $field)) {
3767 $dbman->add_field($table, $field);
3770 $DB->set_field('course_sections', 'summaryformat', 1, array()); // originally treated as HTML
3772 /// Main savepoint reached
3773 upgrade_main_savepoint(true, 2010052700);
3776 if ($oldversion < 2010052800) {
3777 /// Changes to modinfo mean we need to rebuild course cache
3778 require_once($CFG->dirroot . '/course/lib.php');
3779 rebuild_course_cache(0, true);
3780 upgrade_main_savepoint(true, 2010052800);
3783 if ($oldversion < 2010052801) {
3785 /// Define field sortorder to be added to files
3786 $table = new xmldb_table('files');
3787 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
3789 /// Conditionally launch add field sortorder
3790 if (!$dbman->field_exists($table, $field)) {
3791 $dbman->add_field($table, $field);
3794 /// Main savepoint reached
3795 upgrade_main_savepoint(true, 2010052801);
3798 if ($oldversion < 2010061900.01) {
3799 // Define table enrol to be created
3800 $table = new xmldb_table('enrol');
3802 // Adding fields to table enrol
3803 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3804 $table->add_field('enrol', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
3805 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3806 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3807 $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3808 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3809 $table->add_field('enrolperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
3810 $table->add_field('enrolstartdate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
3811 $table->add_field('enrolenddate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
3812 $table->add_field('expirynotify', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0');
3813 $table->add_field('expirythreshold', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
3814 $table->add_field('notifyall', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0');
3815 $table->add_field('password', XMLDB_TYPE_CHAR, '50', null, null, null, null);
3816 $table->add_field('cost', XMLDB_TYPE_CHAR, '20', null, null, null, null);
3817 $table->add_field('currency', XMLDB_TYPE_CHAR, '3', null, null, null, null);
3818 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
3819 $table->add_field('customint1', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
3820 $table->add_field('customint2', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
3821 $table->add_field('customint3', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
3822 $table->add_field('customint4', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
3823 $table->add_field('customchar1', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3824 $table->add_field('customchar2', XMLDB_TYPE_CHAR, '255', null, null, null, null);
3825 $table->add_field('customdec1', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null);
3826 $table->add_field('customdec2', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null);
3827 $table->add_field('customtext1', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
3828 $table->add_field('customtext2', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
3829 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3830 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3832 // Adding keys to table enrol
3833 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3834 $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
3836 // Adding indexes to table enrol
3837 $table->add_index('enrol', XMLDB_INDEX_NOTUNIQUE, array('enrol'));
3839 // launch create table for enrol
3840 $dbman->create_table($table);
3842 // Main savepoint reached
3843 upgrade_main_savepoint(true, 2010061900.01);
3846 if ($oldversion < 2010061900.02) {
3847 // Define table course_participant to be created
3848 $table = new xmldb_table('user_enrolments');
3850 // Adding fields to table course_participant
3851 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3852 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3853 $table->add_field('enrolid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3854 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3855 $table->add_field('timestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3856 $table->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2147483647');
3857 $table->add_field('modifierid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3858 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3859 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
3861 // Adding keys to table course_participant
3862 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3863 $table->add_key('enrolid', XMLDB_KEY_FOREIGN, array('enrolid'), 'enrol', array('id'));
3864 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
3865 $table->add_key('modifierid', XMLDB_KEY_FOREIGN, array('modifierid'), 'user', array('id'));
3868 // Adding indexes to table user_enrolments
3869 $table->add_index('enrolid-userid', XMLDB_INDEX_UNIQUE, array('enrolid', 'userid'));
3871 // Launch create table for course_participant
3872 $dbman->create_table($table);
3874 // Main savepoint reached
3875 upgrade_main_savepoint(true, 2010061900.02);
3878 if ($oldversion < 2010061900.03) {
3879 // Define field itemid to be added to role_assignments
3880 $table = new xmldb_table('role_assignments');
3881 $field = new xmldb_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'enrol');
3883 // Launch add field itemid
3884 $dbman->add_field($table, $field);
3886 // The new enrol plugins may assign one role several times in one context,
3887 // if we did not allow it we would have big problems with roles when unenrolling
3888 $table = new xmldb_table('role_assignments');
3889 $index = new xmldb_index('contextid-roleid-userid', XMLDB_INDEX_UNIQUE, array('contextid', 'roleid', 'userid'));
3891 // Conditionally launch drop index contextid-roleid-userid
3892 if ($dbman->index_exists($table, $index)) {
3893 $dbman->drop_index($table, $index);
3896 // Main savepoint reached
3897 upgrade_main_savepoint(true, 2010061900.03);
3900 if ($oldversion < 2010061900.04) {
3901 // there is no default course role any more, each enrol plugin has to handle it separately
3902 if (!empty($CFG->defaultcourseroleid)) {
3903 $sql = "UPDATE {course} SET defaultrole = :defaultrole WHERE defaultrole = 0";
3904 $params = array('defaultrole' => $CFG->defaultcourseroleid);
3905 $DB->execute($sql, $params);
3907 unset_config('defaultcourseroleid');
3909 // Main savepoint reached
3910 upgrade_main_savepoint(true, 2010061900.04);
3913 if ($oldversion < 2010061900.05) {
3914 // make sure enrol settings make actually sense and tweak defaults a bit
3916 $sqlempty = $DB->sql_empty();
3918 // set course->enrol to default value so that other upgrade code is simpler
3919 $defaultenrol = empty($CFG->enrol) ? 'manual' : $CFG->enrol;
3920 $sql = "UPDATE {course} SET enrol = ? WHERE enrol = '$sqlempty'";
3921 $DB->execute($sql, array($defaultenrol));
3922 unset_config('enrol');
3924 if (!isset($CFG->enrol_plugins_enabled) or empty($CFG->enrol_plugins_enabled)) {
3925 set_config('enrol_plugins_enabled', 'manual');
3926 } else {
3927 $enabledplugins = explode(',', $CFG->enrol_plugins_enabled);
3928 $enabledplugins = array_unique($enabledplugins);
3929 set_config('enrol_plugins_enabled', implode(',', $enabledplugins));
3932 // Main savepoint reached
3933 upgrade_main_savepoint(true, 2010061900.05);
3936 if ($oldversion < 2010061900.06) {
3937 $sqlempty = $DB->sql_empty();
3938 $params = array('siteid'=>SITEID);
3940 // enable manual in all courses
3941 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, expirynotify, expirythreshold, notifyall, roleid, timecreated, timemodified)
3942 SELECT 'manual', 0, id, 0, enrolperiod, expirynotify, expirythreshold, notifystudents, defaultrole, timecreated, timemodified
3943 FROM {course}
3944 WHERE id <> :siteid";
3945 $DB->execute($sql, $params);
3947 // enable self enrol only when course enrollable
3948 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
3949 notifyall, password, roleid, timecreated, timemodified)
3950 SELECT 'self', 0, id, 1, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
3951 notifystudents, password, defaultrole, timecreated, timemodified
3952 FROM {course}
3953 WHERE enrollable = 1 AND id <> :siteid";
3954 $DB->execute($sql, $params);
3956 // enable guest access if previously allowed - separately with or without password
3957 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, timecreated, timemodified)
3958 SELECT 'guest', 0, id, 2, timecreated, timemodified
3959 FROM {course}
3960 WHERE guest = 1 AND id <> :siteid";
3961 $DB->execute($sql, $params);
3962 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, password, timecreated, timemodified)
3963 SELECT 'guest', 0, id, 2, password, timecreated, timemodified
3964 FROM {course}
3965 WHERE guest = 2 and password <> '$sqlempty' AND id <> :siteid";
3966 $DB->execute($sql, $params);
3968 upgrade_main_savepoint(true, 2010061900.06);
3971 if ($oldversion < 2010061900.07) {
3972 // now migrate old style "interactive" enrol plugins - we know them by looking into course.enrol
3973 $params = array('siteid'=>SITEID);
3974 $enabledplugins = explode(',', $CFG->enrol_plugins_enabled);
3975 $usedplugins = $DB->get_fieldset_sql("SELECT DISTINCT enrol FROM {course}");
3976 foreach ($usedplugins as $plugin) {
3977 if ($plugin === 'manual') {
3978 continue;
3980 $enabled = in_array($plugin, $enabledplugins) ? 0 : 1; // 0 means active, 1 disabled
3981 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
3982 notifyall, password, cost, currency, roleid, timecreated, timemodified)
3983 SELECT enrol, $enabled, id, 4, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
3984 notifystudents, password, cost, currency, defaultrole, timecreated, timemodified
3985 FROM {course}
3986 WHERE enrol = :plugin AND id <> :siteid";
3987 $params['plugin'] = $plugin;
3988 $DB->execute($sql, $params);
3990 upgrade_main_savepoint(true, 2010061900.07);
3993 if ($oldversion < 2010061900.08) {
3994 // now migrate the rest - these plugins are not in course.enrol, instead we just look for suspicious role assignments,
3995 // unfortunately old enrol plugins were doing sometimes weird role assignments :-(
3997 // enabled
3998 $enabledplugins = explode(',', $CFG->enrol_plugins_enabled);
3999 list($sqlenabled, $params) = $DB->get_in_or_equal($enabledplugins, SQL_PARAMS_NAMED, 'ena');
4000 $params['siteid'] = SITEID;
4001 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
4002 notifyall, password, cost, currency, roleid, timecreated, timemodified)
4003 SELECT DISTINCT ra.enrol, 0, c.id, 5, c.enrolperiod, c.enrolstartdate, c.enrolenddate, c.expirynotify, c.expirythreshold,
4004 c.notifystudents, c.password, c.cost, c.currency, c.defaultrole, c.timecreated, c.timemodified
4005 FROM {course} c
4006 JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = 50)
4007 JOIN {role_assignments} ra ON (ra.contextid = ctx.id)
4008 WHERE c.id <> :siteid AND ra.enrol $sqlenabled";
4009 $processed = $DB->get_fieldset_sql("SELECT DISTINCT enrol FROM {enrol}");
4010 if ($processed) {
4011 list($sqlnotprocessed, $params2) = $DB->get_in_or_equal($processed, SQL_PARAMS_NAMED, 'np', false);
4012 $params = array_merge($params, $params2);
4013 $sql = "$sql AND ra.enrol $sqlnotprocessed";
4015 $DB->execute($sql, $params);
4017 // disabled
4018 $params = array('siteid' => SITEID);
4019 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
4020 notifyall, password, cost, currency, roleid, timecreated, timemodified)
4021 SELECT DISTINCT ra.enrol, 1, c.id, 5, c.enrolperiod, c.enrolstartdate, c.enrolenddate, c.expirynotify, c.expirythreshold,
4022 c.notifystudents, c.password, c.cost, c.currency, c.defaultrole, c.timecreated, c.timemodified
4023 FROM {course} c
4024 JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = 50)
4025 JOIN {role_assignments} ra ON (ra.contextid = ctx.id)
4026 WHERE c.id <> :siteid";
4027 $processed = $DB->get_fieldset_sql("SELECT DISTINCT enrol FROM {enrol}");
4028 if ($processed) {
4029 list($sqlnotprocessed, $params2) = $DB->get_in_or_equal($processed, SQL_PARAMS_NAMED, 'np', false);
4030 $params = array_merge($params, $params2);
4031 $sql = "$sql AND ra.enrol $sqlnotprocessed";
4033 $DB->execute($sql, $params);
4035 upgrade_main_savepoint(true, 2010061900.08);
4038 if ($oldversion < 2010061900.09) {
4039 // unfortunately there may be still some leftovers
4040 // after reconfigured, uninstalled or borked enrol plugins,
4041 // unfortunately this may be a bit slow - but there should not be many of these
4042 upgrade_set_timeout();
4043 $sqlempty = $DB->sql_empty();
4044 $sql = "SELECT DISTINCT c.id AS courseid, ra.enrol, c.timecreated, c.timemodified
4045 FROM {course} c
4046 JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = 50)
4047 JOIN {role_assignments} ra ON (ra.contextid = ctx.id AND ra.enrol <> '$sqlempty')
4048 LEFT JOIN {enrol} e ON (e.courseid = c.id AND e.enrol = ra.enrol)
4049 WHERE c.id <> :siteid AND e.id IS NULL";
4050 $params = array('siteid'=>SITEID);
4051 $rs = $DB->get_recordset_sql($sql, $params);
4052 foreach ($rs as $enrol) {
4053 upgrade_set_timeout();
4054 $enrol->status = 1; // better disable them
4055 $DB->insert_record('enrol', $enrol);
4057 $rs->close();
4058 upgrade_main_savepoint(true, 2010061900.09);
4061 if ($oldversion < 2010061900.10) {
4062 // migrate existing setup of meta courses, ignore records referencing invalid courses
4063 $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, customint1)
4064 SELECT 'meta', 0, cm.parent_course, 5, cm.child_course
4065 FROM {course_meta} cm
4066 JOIN {course} p ON p.id = cm.parent_course
4067 JOIN {course} c ON c.id = cm.child_course";
4068 $DB->execute($sql);
4070 upgrade_main_savepoint(true, 2010061900.10);
4073 if ($oldversion < 2010061900.11) {
4074 // nuke any old role assignments+enrolments in previous meta courses, we have to start from scratch
4075 $select = "SELECT ctx.id
4076 FROM {context} ctx
4077 JOIN {course} c ON (c.id = ctx.instanceid AND ctx.contextlevel = 50 AND c.metacourse = 1)";
4078 $DB->delete_records_select('role_assignments', "contextid IN ($select) AND enrol = 'manual'");
4080 // course_meta to be dropped - we use enrol_meta plugin instead now
4081 $table = new xmldb_table('course_meta');
4083 // Launch drop table for course_meta
4084 $dbman->drop_table($table);
4086 // Main savepoint reached
4087 upgrade_main_savepoint(true, 2010061900.11);
4090 if ($oldversion < 2010061900.12) {
4091 // finally remove all obsolete fields from the course table - yay!
4092 // all the information was migrated to the enrol table
4094 // Define field guest to be dropped from course
4095 $table = new xmldb_table('course');
4096 $field = new xmldb_field('guest');
4098 // Conditionally launch drop field guest
4099 if ($dbman->field_exists($table, $field)) {
4100 $dbman->drop_field($table, $field);
4103 // Define field password to be dropped from course
4104 $table = new xmldb_table('course');
4105 $field = new xmldb_field('password');
4107 // Conditionally launch drop field password
4108 if ($dbman->field_exists($table, $field)) {
4109 $dbman->drop_field($table, $field);
4112 // Define field enrolperiod to be dropped from course
4113 $table = new xmldb_table('course');
4114 $field = new xmldb_field('enrolperiod');
4116 // Conditionally launch drop field enrolperiod
4117 if ($dbman->field_exists($table, $field)) {
4118 $dbman->drop_field($table, $field);
4121 // Define field cost to be dropped from course
4122 $table = new xmldb_table('course');
4123 $field = new xmldb_field('cost');
4125 // Conditionally launch drop field cost
4126 if ($dbman->field_exists($table, $field)) {
4127 $dbman->drop_field($table, $field);
4130 // Define field currency to be dropped from course
4131 $table = new xmldb_table('course');
4132 $field = new xmldb_field('currency');
4134 // Conditionally launch drop field currency
4135 if ($dbman->field_exists($table, $field)) {
4136 $dbman->drop_field($table, $field);
4139 // Define field metacourse to be dropped from course
4140 $table = new xmldb_table('course');
4141 $field = new xmldb_field('metacourse');
4143 // Conditionally launch drop field metacourse
4144 if ($dbman->field_exists($table, $field)) {
4145 $dbman->drop_field($table, $field);
4148 // Define field expirynotify to be dropped from course
4149 $table = new xmldb_table('course');
4150 $field = new xmldb_field('expirynotify');
4152 // Conditionally launch drop field expirynotify
4153 if ($dbman->field_exists($table, $field)) {
4154 $dbman->drop_field($table, $field);
4157 // Define field expirythreshold to be dropped from course
4158 $table = new xmldb_table('course');
4159 $field = new xmldb_field('expirythreshold');
4161 // Conditionally launch drop field expirythreshold
4162 if ($dbman->field_exists($table, $field)) {
4163 $dbman->drop_field($table, $field);
4166 // Define field notifystudents to be dropped from course
4167 $table = new xmldb_table('course');
4168 $field = new xmldb_field('notifystudents');
4170 // Conditionally launch drop field notifystudents
4171 if ($dbman->field_exists($table, $field)) {
4172 $dbman->drop_field($table, $field);
4175 // Define field enrollable to be dropped from course
4176 $table = new xmldb_table('course');
4177 $field = new xmldb_field('enrollable');
4179 // Conditionally launch drop field enrollable
4180 if ($dbman->field_exists($table, $field)) {
4181 $dbman->drop_field($table, $field);
4184 // Define field enrolstartdate to be dropped from course
4185 $table = new xmldb_table('course');
4186 $field = new xmldb_field('enrolstartdate');
4188 // Conditionally launch drop field enrolstartdate
4189 if ($dbman->field_exists($table, $field)) {
4190 $dbman->drop_field($table, $field);
4193 // Define field enrolenddate to be dropped from course
4194 $table = new xmldb_table('course');
4195 $field = new xmldb_field('enrolenddate');
4197 // Conditionally launch drop field enrolenddate
4198 if ($dbman->field_exists($table, $field)) {
4199 $dbman->drop_field($table, $field);
4202 // Define field enrol to be dropped from course
4203 $table = new xmldb_table('course');
4204 $field = new xmldb_field('enrol');
4206 // Conditionally launch drop field enrol
4207 if ($dbman->field_exists($table, $field)) {
4208 $dbman->drop_field($table, $field);
4211 // Define field defaultrole to be dropped from course
4212 $table = new xmldb_table('course');
4213 $field = new xmldb_field('defaultrole');
4215 // Conditionally launch drop field defaultrole
4216 if ($dbman->field_exists($table, $field)) {
4217 $dbman->drop_field($table, $field);
4220 upgrade_main_savepoint(true, 2010061900.12);
4223 if ($oldversion < 2010061900.13) {
4224 // Define field visibleold to be added to course_categories
4225 $table = new xmldb_table('course_categories');
4226 $field = new xmldb_field('visibleold', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'visible');
4228 // Launch add field visibleold
4229 $dbman->add_field($table, $field);
4231 // Main savepoint reached
4232 upgrade_main_savepoint(true, 2010061900.13);
4235 if ($oldversion < 2010061900.14) {
4236 // keep previous visible state
4237 $DB->execute("UPDATE {course_categories} SET visibleold = visible");
4239 // make sure all subcategories of hidden categories are hidden too, do not rely on category path yet
4240 $sql = "SELECT c.id
4241 FROM {course_categories} c
4242 JOIN {course_categories} pc ON (pc.id = c.parent AND pc.visible = 0)
4243 WHERE c.visible = 1";
4244 while ($categories = $DB->get_records_sql($sql)) {
4245 foreach ($categories as $cat) {
4246 upgrade_set_timeout();
4247 $DB->set_field('course_categories', 'visible', 0, array('id'=>$cat->id));
4250 upgrade_main_savepoint(true, 2010061900.14);
4253 if ($oldversion < 2010061900.15) {
4254 // Define field visibleold to be added to course
4255 $table = new xmldb_table('course');
4256 $field = new xmldb_field('visibleold', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'visible');
4258 // Launch add field visibleold
4259 $dbman->add_field($table, $field);
4261 // Main savepoint reached
4262 upgrade_main_savepoint(true, 2010061900.15);
4265 if ($oldversion < 2010061900.16) {
4266 // keep previous visible state
4267 $DB->execute("UPDATE {course} SET visibleold = visible");
4269 // make sure all courses in hidden categories are hidden
4270 $DB->execute("UPDATE {course} SET visible = 0 WHERE category IN (SELECT id FROM {course_categories} WHERE visible = 0)");
4272 upgrade_main_savepoint(true, 2010061900.16);
4275 if ($oldversion < 2010061900.20) {
4276 // now set up the enrolments - look for roles with course:participate only at course context - the category enrolments are synchronised later by archetype and new capability
4278 $syscontext = get_context_instance(CONTEXT_SYSTEM);
4279 $params = array('syscontext'=>$syscontext->id, 'participate'=>'moodle/course:participate');
4280 $roles = $DB->get_fieldset_sql("SELECT DISTINCT roleid FROM {role_capabilities} WHERE contextid = :syscontext AND capability = :participate AND permission = 1", $params);
4281 if ($roles) {
4282 list($sqlroles, $params) = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED, 'r');
4284 $sql = "INSERT INTO {user_enrolments} (status, enrolid, userid, timestart, timeend, modifierid, timecreated, timemodified)
4286 SELECT 0, e.id, ra.userid, MIN(ra.timestart), MIN(ra.timeend), 0, MIN(ra.timemodified), MAX(ra.timemodified)
4287 FROM {role_assignments} ra
4288 JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = 50)
4289 JOIN {enrol} e ON (e.enrol = ra.enrol AND e.courseid = c.instanceid)
4290 JOIN {user} u ON u.id = ra.userid
4291 WHERE u.deleted = 0 AND ra.roleid $sqlroles
4292 GROUP BY e.id, ra.userid";
4293 $DB->execute($sql, $params);
4296 upgrade_main_savepoint(true, 2010061900.20);
4299 if ($oldversion < 2010061900.21) {
4300 // hidden is completely removed, timestart+timeend are now in the user_enrolments table
4302 // Define field hidden to be dropped from role_assignments
4303 $table = new xmldb_table('role_assignments');
4304 $field = new xmldb_field('hidden');
4306 // Conditionally launch drop field hidden
4307 if ($dbman->field_exists($table, $field)) {
4308 $dbman->drop_field($table, $field);
4311 // Define field timestart to be dropped from role_assignments
4312 $table = new xmldb_table('role_assignments');
4313 $field = new xmldb_field('timestart');
4315 // Conditionally launch drop field timestart
4316 if ($dbman->field_exists($table, $field)) {
4317 $dbman->drop_field($table, $field);
4320 // Define field timeend to be dropped from role_assignments
4321 $table = new xmldb_table('role_assignments');
4322 $field = new xmldb_field('timeend');
4324 // Conditionally launch drop field timeend
4325 if ($dbman->field_exists($table, $field)) {
4326 $dbman->drop_field($table, $field);
4329 // Main savepoint reached
4330 upgrade_main_savepoint(true, 2010061900.21);
4333 if ($oldversion < 2010061900.22) {
4334 // Rename field enrol on table role_assignments to component and update content
4336 $table = new xmldb_table('role_assignments');
4337 $field = new xmldb_field('enrol', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'modifierid');
4339 // Launch rename field enrol
4340 $dbman->rename_field($table, $field, 'component');
4342 // Changing precision of field component on table role_assignments to (100)
4343 $table = new xmldb_table('role_assignments');
4344 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'modifierid');
4346 // Launch change of precision for field component
4347 $dbman->change_field_precision($table, $field);
4349 // Manual is a special case - we use empty string instead now
4350 $params = array('empty'=>$DB->sql_empty(), 'manual'=>'manual');
4351 $sql = "UPDATE {role_assignments}
4352 SET component = :empty
4353 WHERE component = :manual";
4354 $DB->execute($sql, $params);
4356 // Now migrate to real enrol component names
4357 $params = array('empty'=>$DB->sql_empty());
4358 $concat = $DB->sql_concat("'enrol_'", 'component');
4359 $sql = "UPDATE {role_assignments}
4360 SET component = $concat
4361 WHERE component <> :empty
4362 AND contextid IN (
4363 SELECT id
4364 FROM {context}
4365 WHERE contextlevel >= 50)";
4366 $DB->execute($sql, $params);
4368 // Now migrate to real auth component names
4369 $params = array('empty'=>$DB->sql_empty());
4370 $concat = $DB->sql_concat("'auth_'", 'component');
4371 $sql = "UPDATE {role_assignments}
4372 SET component = $concat
4373 WHERE component <> :empty
4374 AND contextid IN (
4375 SELECT id
4376 FROM {context}
4377 WHERE contextlevel < 50)";
4378 $DB->execute($sql, $params);
4380 // Main savepoint reached
4381 upgrade_main_savepoint(true, 2010061900.22);
4384 if ($oldversion < 2010061900.23) {
4385 // add proper itemid to role assignments that were added by enrolment plugins
4386 $sql = "UPDATE {role_assignments}
4387 SET itemid = (SELECT MIN({enrol}.id)
4388 FROM {enrol}
4389 JOIN {context} ON ({context}.contextlevel = 50 AND {context}.instanceid = {enrol}.courseid)
4390 WHERE {role_assignments}.component = ".$DB->sql_concat("'enrol_'", "{enrol}.enrol")." AND {context}.id = {role_assignments}.contextid)
4391 WHERE component <> 'enrol_manual' AND component LIKE 'enrol_%'";
4392 $DB->execute($sql);
4393 // Main savepoint reached
4394 upgrade_main_savepoint(true, 2010061900.23);
4397 if ($oldversion < 2010061900.30) {
4398 // make new list of active enrol plugins - order is important, meta should be always last, manual first
4399 $enabledplugins = explode(',', $CFG->enrol_plugins_enabled);
4400 $enabledplugins = array_merge(array('manual', 'guest', 'self', 'cohort'), $enabledplugins);
4401 if ($DB->record_exists('enrol', array('enrol'=>'meta'))) {
4402 $enabledplugins[] = 'meta';
4404 $enabledplugins = array_unique($enabledplugins);
4405 set_config('enrol_plugins_enabled', implode(',', $enabledplugins));
4407 // Main savepoint reached
4408 upgrade_main_savepoint(true, 2010061900.30);
4411 if ($oldversion < 2010061900.31) {
4412 // finalise all new enrol settings and cleanup old settings
4414 // legacy allowunenrol was deprecated in 1.9 already
4415 unset_config('allwunenroll');
4417 // obsolete course presets
4418 unset_config('metacourse', 'moodlecourse');
4419 unset_config('enrol', 'moodlecourse');
4420 unset_config('enrollable', 'moodlecourse');
4421 unset_config('enrolperiod', 'moodlecourse');
4422 unset_config('expirynotify', 'moodlecourse');
4423 unset_config('notifystudents', 'moodlecourse');
4424 unset_config('expirythreshold', 'moodlecourse');
4425 unset_config('enrolpassword', 'moodlecourse');
4426 unset_config('guest', 'moodlecourse');
4428 unset_config('backup_sche_metacourse', 'backup');
4430 unset_config('lastexpirynotify');
4432 // hidden course categories now prevent only browsing, courses are accessible if you know the URL and course is visible
4433 unset_config('allowvisiblecoursesinhiddencategories');
4435 if (isset($CFG->coursemanager)) {
4436 set_config('coursecontact', $CFG->coursemanager);
4437 unset_config('coursemanager');
4440 // migrate plugin settings - the problem here is we are splitting manual into three different plugins
4441 if (isset($CFG->enrol_manual_usepasswordpolicy)) {
4442 set_config('usepasswordpolicy', $CFG->enrol_manual_usepasswordpolicy, 'enrol_guest');
4443 set_config('usepasswordpolicy', $CFG->enrol_manual_usepasswordpolicy, 'enrol_self');
4444 set_config('groupenrolmentkeypolicy', $CFG->enrol_manual_usepasswordpolicy);
4445 unset_config('enrol_manual_usepasswordpolicy');
4447 if (isset($CFG->enrol_manual_requirekey)) {
4448 set_config('requirepassword', $CFG->enrol_manual_requirekey, 'enrol_guest');
4449 set_config('requirepassword', $CFG->enrol_manual_requirekey, 'enrol_self');
4450 unset_config('enrol_manual_requirekey');
4452 if (isset($CFG->enrol_manual_showhint)) {
4453 set_config('showhint', $CFG->enrol_manual_showhint, 'enrol_guest');
4454 set_config('showhint', $CFG->enrol_manual_showhint, 'enrol_self');
4455 unset_config('enrol_manual_showhint');
4458 upgrade_main_savepoint(true, 2010061900.31);
4461 if ($oldversion < 2010061900.32) {
4462 // MDL-22797 course completion has to be updated to use new enrol framework, it will not be enabled in final 2.0
4463 set_config('enableavailability', 0);
4464 set_config('enablecompletion', 0);
4465 upgrade_main_savepoint(true, 2010061900.32);
4468 if ($oldversion < 2010062101) {
4470 /// Define field huburl to be dropped from course_published
4471 $table = new xmldb_table('course_published');
4472 $field = new xmldb_field('hubid');
4474 /// Conditionally launch drop field huburl
4475 if ($dbman->field_exists($table, $field)) {
4476 $dbman->drop_field($table, $field);
4479 /// Define field huburl to be added to course_published
4480 $table = new xmldb_table('course_published');
4481 $field = new xmldb_field('huburl', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'id');
4483 /// Conditionally launch add field huburl
4484 if (!$dbman->field_exists($table, $field)) {
4485 $dbman->add_field($table, $field);
4488 /// Main savepoint reached
4489 upgrade_main_savepoint(true, 2010062101);
4492 if ($oldversion < 2010070300) {
4493 //TODO: this is a temporary hack for upgrade from PR3, to be removed later
4495 // Define field component to be added to files
4496 $table = new xmldb_table('files');
4497 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'contextid');
4499 // Conditionally upgrade from PR3
4500 if (!$dbman->field_exists($table, $field)) {
4501 $dbman->add_field($table, $field);
4502 $index = new xmldb_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid'));
4503 $dbman->drop_index($table, $index);
4504 $index = new xmldb_index('component-filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('component', 'filearea', 'contextid', 'itemid'));
4505 $dbman->add_index($table, $index);
4507 // Rename areas as add proper component
4508 $areas = $DB->get_fieldset_sql("SELECT DISTINCT filearea FROM {files}");
4509 if ($areas) {
4510 // fix incorrect itemids
4511 $DB->execute("UPDATE {files} SET itemid = 0 WHERE filearea = 'category_description'"); // context identifies instances
4512 $DB->execute("UPDATE {files} SET itemid = 0 WHERE filearea = 'user_profile'"); // context identifies instances
4513 $DB->execute("UPDATE {files} SET itemid = 0 WHERE filearea = 'block_html'"); // context identifies instances
4514 foreach ($areas as $area) {
4515 // rename areas
4516 if ($area === 'course_backup') {
4517 $area = 'backup_course';
4518 } else if ($area === 'section_backup') {
4519 $area = 'backup_section';
4520 } else if ($area === 'activity_backup') {
4521 $area = 'backup_activity';
4522 } else if ($area === 'category_description') {
4523 $area = 'coursecat_description';
4525 if ($area === 'block_html') {
4526 $component = 'block_html';
4527 $filearea = 'content';
4528 } else {
4529 list($component, $filearea) = explode('_', $area, 2);
4530 // note this is just a hack which guesses plugin from old PRE3 files code, the whole point of adding component is to get rid of this guessing
4531 if (file_exists("$CFG->dirroot/mod/$component/lib.php")) {
4532 $component = 'mod_'.$component;
4535 $DB->execute("UPDATE {files} SET component = :component, filearea = :filearea WHERE filearea = :area", array('component'=>$component, 'filearea'=>$filearea, 'area'=>$area));
4537 // Update all hashes
4538 $rs = $DB->get_recordset('files', array());
4539 foreach ($rs as $file) {
4540 upgrade_set_timeout();
4541 $pathnamehash = sha1("/$file->contextid/$file->component/$file->filearea/$file->itemid".$file->filepath.$file->filename);
4542 $DB->set_field('files', 'pathnamehash', $pathnamehash, array('id'=>$file->id));
4544 $rs->close();
4548 // Main savepoint reached
4549 upgrade_main_savepoint(true, 2010070300);
4552 if ($oldversion < 2010070500) {
4554 /// Define field operation to be added to backup_controllers
4555 $table = new xmldb_table('backup_controllers');
4556 $field = new xmldb_field('operation', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'backup', 'backupid');
4558 /// Conditionally launch add field operation
4559 if (!$dbman->field_exists($table, $field)) {
4560 $dbman->add_field($table, $field);
4563 /// Main savepoint reached
4564 upgrade_main_savepoint(true, 2010070500);
4567 if ($oldversion < 2010070501) {
4569 /// Define field suspended to be added to user
4570 $table = new xmldb_table('user');
4571 $field = new xmldb_field('suspended', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'deleted');
4573 /// Conditionally launch add field suspended
4574 if (!$dbman->field_exists($table, $field)) {
4575 $dbman->add_field($table, $field);
4578 /// Main savepoint reached
4579 upgrade_main_savepoint(true, 2010070501);
4582 if ($oldversion < 2010070502) {
4584 /// Define field newitemid to be added to backup_ids_template
4585 $table = new xmldb_table('backup_ids_template');
4586 $field = new xmldb_field('newitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'itemid');
4587 /// Conditionally launch add field newitemid
4588 if (!$dbman->field_exists($table, $field)) {
4589 $dbman->add_field($table, $field);
4592 /// Define field info to be added to backup_ids_template
4593 $table = new xmldb_table('backup_ids_template');
4594 $field = new xmldb_field('info', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'parentitemid');
4595 /// Conditionally launch add field info
4596 if (!$dbman->field_exists($table, $field)) {
4597 $dbman->add_field($table, $field);
4600 /// Main savepoint reached
4601 upgrade_main_savepoint(true, 2010070502);
4604 if ($oldversion < 2010070601) {
4605 // delete loan calc if not used - it was moved to contrib
4606 if (!file_exists("$CFG->dirroot/blocks/loancalc/version.php")) {
4607 if (!$DB->record_exists('block_instances', array('blockname'=>'loancalc'))) {
4608 $DB->delete_records('block', array('name'=>'loancalc'));
4611 upgrade_main_savepoint(true, 2010070601);
4614 if ($oldversion < 2010070602) {
4615 // delete exercise if not used and not installed - now in contrib (do not use adminlib uninstall functions, they may change)
4616 if (!file_exists("$CFG->dirroot/mod/exercise/version.php")) {
4617 if ($module = $DB->get_record('modules', array('name'=>'exercise'))) {
4618 if (!$DB->record_exists('course_modules', array('module'=>$module->id))) {
4619 //purge capabilities
4620 $DB->delete_records_select('capabilities', "name LIKE ?", array('mod/exercise:%'));
4621 $DB->delete_records_select('role_capabilities', "capability LIKE ?", array('mod/exercise:%'));
4622 $tables = array('exercise', 'exercise_submissions', 'exercise_assessments', 'exercise_elements', 'exercise_rubrics', 'exercise_grades');
4623 foreach ($tables as $tname) {
4624 $table = new xmldb_table($tname);
4625 if ($dbman->table_exists($table)) {
4626 $dbman->drop_table($table);
4629 $DB->delete_records('event', array('modulename' => 'exercise'));
4630 $DB->delete_records('log', array('module' => 'exercise'));
4631 $DB->delete_records('modules', array('name'=>'exercise'));
4635 upgrade_main_savepoint(true, 2010070602);
4638 if ($oldversion < 2010070603) {
4639 // delete journal if not used and not installed - now in contrib (do not use adminlib uninstall functions, they may change)
4640 if (!file_exists("$CFG->dirroot/mod/journal/version.php")) {
4641 if ($module = $DB->get_record('modules', array('name'=>'journal'))) {
4642 if (!$DB->record_exists('course_modules', array('module'=>$module->id))) {
4643 //purge capabilities
4644 $DB->delete_records_select('capabilities', "name LIKE ?", array('mod/journal:%'));
4645 $DB->delete_records_select('role_capabilities', "capability LIKE ?", array('mod/journal:%'));
4646 $tables = array('journal', 'journal_entries');
4647 foreach ($tables as $tname) {
4648 $table = new xmldb_table($tname);
4649 if ($dbman->table_exists($table)) {
4650 $dbman->drop_table($table);
4653 $DB->delete_records('event', array('modulename' => 'journal'));
4654 $DB->delete_records('log', array('module' => 'journal'));
4655 $DB->delete_records('modules', array('name'=>'journal'));
4656 unset_config('journal_initialdisable');
4660 upgrade_main_savepoint(true, 2010070603);
4663 if ($oldversion < 2010070604) {
4664 // delete lams if not used and not installed - now in contrib (do not use adminlib uninstall functions, they may change)
4665 if (!file_exists("$CFG->dirroot/mod/lams/version.php")) {
4666 if ($module = $DB->get_record('modules', array('name'=>'lams'))) {
4667 if (!$DB->record_exists('course_modules', array('module'=>$module->id))) {
4668 //purge capabilities
4669 $DB->delete_records_select('capabilities', "name LIKE ?", array('mod/lams:%'));
4670 $DB->delete_records_select('role_capabilities', "capability LIKE ?", array('mod/lams:%'));
4671 $tables = array('lams', '');
4672 foreach ($tables as $tname) {
4673 $table = new xmldb_table($tname);
4674 if ($dbman->table_exists($table)) {
4675 $dbman->drop_table($table);
4678 $DB->delete_records('event', array('modulename' => 'lams'));
4679 $DB->delete_records('log', array('module' => 'lams'));
4680 $DB->delete_records('modules', array('name'=>'lams'));
4681 unset_config('lams_initialdisable');
4685 upgrade_main_savepoint(true, 2010070604);
4688 if ($oldversion < 2010070801) {
4689 /// Before changing the field, drop dependent indexes
4690 /// Define index shortname (not unique) to be dropped form course_request
4691 $table = new xmldb_table('user');
4692 $index = new xmldb_index('city', XMLDB_INDEX_NOTUNIQUE, array('city'));
4693 /// Conditionally launch drop index shortname
4694 if ($dbman->index_exists($table, $index)) {
4695 $dbman->drop_index($table, $index);
4697 /// Changing precision of field city on table user to (120)
4698 $field = new xmldb_field('city', XMLDB_TYPE_CHAR, '120', null, XMLDB_NOTNULL, null, null, 'address');
4700 /// Launch change of precision for field city
4701 $dbman->change_field_precision($table, $field);
4703 /// Conditionally launch add index typeitem_ix
4704 if (!$dbman->index_exists($table, $index)) {
4705 $dbman->add_index($table, $index);
4707 /// Main savepoint reached
4708 upgrade_main_savepoint(true, 2010070801);
4711 if ($oldversion < 2010071000) {
4712 //purge unused editor settings
4713 unset_config('editorbackgroundcolor');
4714 unset_config('editorfontfamily');
4715 unset_config('editorfontsize');
4716 unset_config('editorkillword');
4717 unset_config('editorhidebuttons');
4718 unset_config('editorfontlist');
4720 upgrade_main_savepoint(true, 2010071000);
4723 if ($oldversion < 2010071001) {
4724 // purge obsolete stats settings
4725 unset_config('statscatdepth');
4726 upgrade_main_savepoint(true, 2010071001);
4729 if ($oldversion < 2010071100) {
4730 // move user icons to file storage pool
4731 upgrade_migrate_user_icons();
4732 upgrade_main_savepoint(true, 2010071100);
4735 if ($oldversion < 2010071101) {
4736 // move user icons to file storage pool
4737 upgrade_migrate_group_icons();
4738 upgrade_main_savepoint(true, 2010071101);
4741 if ($oldversion < 2010071300) {
4742 // Define field timecreated to be added to user_enrolments
4743 $table = new xmldb_table('user_enrolments');
4744 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'modifierid');
4746 // Launch add field timecreated
4747 if (!$dbman->field_exists($table, $field)) {
4748 $dbman->add_field($table, $field);
4751 // now try to guess the time created
4752 $sql = "UPDATE {user_enrolments} SET timecreated = timemodified WHERE timecreated = 0";
4753 $DB->execute($sql);
4754 $sql = "UPDATE {user_enrolments} SET timecreated = timestart WHERE timestart <> 0 AND timestart < timemodified";
4755 $DB->execute($sql);
4757 upgrade_main_savepoint(true, 2010071300);
4760 if ($oldversion < 2010071700) { // Make itemname bigger (160cc) to store component+filearea
4762 $table = new xmldb_table('backup_ids_template');
4763 // Define key backupid_itemname_itemid_uk (unique) to be dropped form backup_ids_template
4764 $key = new xmldb_key('backupid_itemname_itemid_uk', XMLDB_KEY_UNIQUE, array('backupid', 'itemname', 'itemid'));
4765 // Define index backupid_parentitemid_ix (not unique) to be dropped form backup_ids_template
4766 $index = new xmldb_index('backupid_parentitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'parentitemid'));
4767 // Define field itemname to be 160cc
4768 $field = new xmldb_field('itemname', XMLDB_TYPE_CHAR, '160', null, XMLDB_NOTNULL, null, null, 'backupid');
4770 // Launch drop key backupid_itemname_itemid_uk
4771 $dbman->drop_key($table, $key);
4772 // Conditionally launch drop index backupid_parentitemid_ix
4773 if ($dbman->index_exists($table, $index)) {
4774 $dbman->drop_index($table, $index);
4777 // Changing precision of field itemname on table backup_ids_template to (160)
4778 $dbman->change_field_precision($table, $field);
4780 // Launch add key backupid_itemname_itemid_uk
4781 $dbman->add_key($table, $key);
4782 // Conditionally launch add index backupid_parentitemid_ix
4783 if (!$dbman->index_exists($table, $index)) {
4784 $dbman->add_index($table, $index);
4787 // Main savepoint reached
4788 upgrade_main_savepoint(true, 2010071700);
4791 if ($oldversion < 2010071701) {
4792 // Drop legacy core tables that now belongs to mnetservice_enrol plugin
4793 // Upgrade procedure not needed as the tables are used for caching purposes only
4794 $tables = array('mnet_enrol_course', 'mnet_enrol_assignments');
4795 foreach ($tables as $tname) {
4796 $table = new xmldb_table($tname);
4797 if ($dbman->table_exists($table)) {
4798 $dbman->drop_table($table);
4802 upgrade_main_savepoint(true, 2010071701);
4805 if ($oldversion < 2010071800) {
4807 // Define table backup_files_template to be created
4808 $table = new xmldb_table('backup_files_template');
4810 // Adding fields to table backup_files_template
4811 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
4812 $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
4813 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
4814 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
4815 $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
4816 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
4817 $table->add_field('info', XMLDB_TYPE_TEXT, 'medium', null, null, null, null);
4819 // Adding keys to table backup_files_template
4820 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
4822 // Adding indexes to table backup_files_template
4823 $table->add_index('backupid_contextid_component_filearea_itemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'contextid', 'component', 'filearea', 'itemid'));
4825 // Conditionally launch create table for backup_files_template
4826 if (!$dbman->table_exists($table)) {
4827 $dbman->create_table($table);
4830 // Main savepoint reached
4831 upgrade_main_savepoint(true, 2010071800);
4834 if ($oldversion < 2010072300) {
4836 // Define field capabilities to be added to external_functions
4837 $table = new xmldb_table('external_functions');
4838 $field = new xmldb_field('capabilities', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'component');
4840 // Conditionally launch add field capabilities
4841 if (!$dbman->field_exists($table, $field)) {
4842 $dbman->add_field($table, $field);
4845 // Main savepoint reached
4846 upgrade_main_savepoint(true, 2010072300);
4849 if ($oldversion < 2010072700) {
4851 // Define index backupid_itemname_newitemid_ix (not unique) to be added to backup_ids_template
4852 $table = new xmldb_table('backup_ids_template');
4853 $index = new xmldb_index('backupid_itemname_newitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'newitemid'));
4855 // Conditionally launch add index backupid_itemname_newitemid_ix
4856 if (!$dbman->index_exists($table, $index)) {
4857 $dbman->add_index($table, $index);
4860 // Main savepoint reached
4861 upgrade_main_savepoint(true, 2010072700);
4864 if ($oldversion < 2010080303) {
4865 $rs = $DB->get_recordset_sql('SELECT i.id, i.name, r.type FROM {repository_instances} i, {repository} r WHERE i.typeid = r.id');
4866 foreach ($rs as $record) {
4867 upgrade_set_timeout();
4868 if ($record->name == $record->type) {
4869 // repository_instances was saving type name as in name field
4870 // which should be empty, the repository api will try to find
4871 // instance name from language files
4872 $DB->set_field('repository_instances', 'name', '');
4875 $rs->close();
4876 upgrade_main_savepoint(true, 2010080303);
4879 if ($oldversion < 2010080305) {
4880 // first drop all log display actions, we will recreate them automatically later
4881 $DB->delete_records('log_display', array());
4883 // Define field component to be added to log_display
4884 $table = new xmldb_table('log_display');
4885 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'field');
4887 // Launch add field component
4888 if (!$dbman->field_exists($table, $field)) {
4889 $dbman->add_field($table, $field);
4892 // Main savepoint reached
4893 upgrade_main_savepoint(true, 2010080305);
4896 if ($oldversion < 2010080900) {
4898 /// Define field generalfeedbackformat to be added to question
4899 $table = new xmldb_table('question');
4900 $field = new xmldb_field('generalfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'generalfeedback');
4902 /// Conditionally launch add field generalfeedbackformat
4903 if (!$dbman->field_exists($table, $field)) {
4904 $dbman->add_field($table, $field);
4907 /// Upgrading the text formats in some question types depends on the
4908 /// questiontextformat field, but the question type upgrade only runs
4909 /// after the code below has messed around with the questiontextformat
4910 /// value. Therefore, we need to create a new column to store the old value.
4911 /// The column should be dropped in Moodle 2.1.
4912 /// Define field oldquestiontextformat to be added to question
4913 $field = new xmldb_field('oldquestiontextformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'generalfeedback');
4915 /// Conditionally launch add field oldquestiontextformat
4916 if (!$dbman->field_exists($table, $field)) {
4917 $dbman->add_field($table, $field);
4920 /// Define field infoformat to be added to question_categories
4921 $table = new xmldb_table('question_categories');
4922 $field = new xmldb_field('infoformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'info');
4924 /// Conditionally launch add field infoformat
4925 if (!$dbman->field_exists($table, $field)) {
4926 $dbman->add_field($table, $field);
4929 /// Define field answerformat to be added to question_answers
4930 $table = new xmldb_table('question_answers');
4931 $field = new xmldb_field('answerformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'answer');
4933 /// Conditionally launch add field answerformat
4934 if (!$dbman->field_exists($table, $field)) {
4935 $dbman->add_field($table, $field);
4938 /// Define field feedbackformat to be added to question_answers
4939 $field = new xmldb_field('feedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'feedback');
4941 /// Conditionally launch add field feedbackformat
4942 if (!$dbman->field_exists($table, $field)) {
4943 $dbman->add_field($table, $field);
4946 /// Define field manualcommentformat to be added to question_sessions
4947 $table = new xmldb_table('question_sessions');
4948 $field = new xmldb_field('manualcommentformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
4950 /// Conditionally launch add field manualcommentformat
4951 if (!$dbman->field_exists($table, $field)) {
4952 $dbman->add_field($table, $field);
4955 /// Main savepoint reached
4956 upgrade_main_savepoint(true, 2010080900);
4959 /// updating question image
4960 if ($oldversion < 2010080901) {
4961 $fs = get_file_storage();
4963 // Define field image to be dropped from question
4964 $table = new xmldb_table('question');
4965 $field = new xmldb_field('image');
4967 // Conditionally launch drop field image
4968 if ($dbman->field_exists($table, $field)) {
4970 $rs = $DB->get_recordset('question');
4971 $textlib = textlib_get_instance();
4973 foreach ($rs as $question) {
4974 // may take awhile
4975 upgrade_set_timeout();
4976 if (empty($question->image)) {
4977 continue;
4979 if (!$category = $DB->get_record('question_categories', array('id'=>$question->category))) {
4980 continue;
4982 $categorycontext = get_context_instance_by_id($category->contextid);
4983 // question files are stored in course level
4984 // so we have to find course context
4985 switch ($categorycontext->contextlevel){
4986 case CONTEXT_COURSE :
4987 $context = $categorycontext;
4988 break;
4989 case CONTEXT_MODULE :
4990 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$categorycontext->instanceid));
4991 $context = get_context_instance(CONTEXT_COURSE, $courseid);
4992 break;
4993 case CONTEXT_COURSECAT :
4994 case CONTEXT_SYSTEM :
4995 $context = get_system_context();
4996 break;
4997 default :
4998 continue;
5000 if ($textlib->substr($textlib->strtolower($question->image), 0, 7) == 'http://') {
5001 // it is a link, appending to existing question text
5002 $question->questiontext .= ' <img src="' . $question->image . '" />';
5003 $question->image = '';
5004 // update question record
5005 $DB->update_record('question', $question);
5006 } else {
5007 $filename = basename($question->image);
5008 $filepath = dirname($question->image);
5009 if (empty($filepath) or $filepath == '.' or $filepath == '/') {
5010 $filepath = '/';
5011 } else {
5012 // append /
5013 $filepath = '/'.trim($filepath, './@#$ ').'/';
5016 // course files already moved to file pool by previous upgrade block
5017 // so we just create copy from course_legacy area
5018 if ($image = $fs->get_file($context->id, 'course', 'legacy', 0, $filepath, $filename)) {
5019 // move files to file pool
5020 $file_record = array(
5021 'contextid'=>$category->contextid,
5022 'component'=>'question',
5023 'filearea'=>'questiontext',
5024 'itemid'=>$question->id
5026 $fs->create_file_from_storedfile($file_record, $image);
5027 $question->questiontext .= ' <img src="@@PLUGINFILE@@' . $filepath . $filename . '" />';
5028 $question->image = '';
5029 // update question record
5030 $DB->update_record('question', $question);
5034 $rs->close();
5036 $dbman->drop_field($table, $field);
5039 // Update question_answers.
5040 // In question_answers.feedback was previously always treated as
5041 // FORMAT_HTML in calculated, multianswer, multichoice, numerical,
5042 // shortanswer and truefalse; and
5043 // FORMAT_MOODLE in essay (despite being edited using the HTML editor)
5044 // So essay feedback needs to be converted to HTML unless $CFG->texteditors == 'textarea'.
5045 // For all question types except multichoice,
5046 // question_answers.answer is FORMAT_PLAIN and does not need to be changed.
5047 // For multichoice, question_answers.answer is FORMAT_MOODLE, and should
5048 // stay that way, at least for now.
5049 $rs = $DB->get_recordset_sql('
5050 SELECT qa.*, q.qtype
5051 FROM {question_answers} qa
5052 JOIN {question} q ON qa.question = q.id');
5053 foreach ($rs as $record) {
5054 // may take awhile
5055 upgrade_set_timeout();
5056 // Convert question_answers.answer
5057 if ($record->qtype !== 'multichoice') {
5058 $record->answerformat = FORMAT_PLAIN;
5059 } else {
5060 $record->answerformat = FORMAT_MOODLE;
5063 // Convert question_answers.feedback
5064 if ($CFG->texteditors !== 'textarea') {
5065 if ($record->qtype == 'essay') {
5066 $record->feedback = text_to_html($record->feedback, false, false, true);
5068 $record->feedbackformat = FORMAT_HTML;
5069 } else {
5070 $record->feedbackformat = FORMAT_MOODLE;
5073 $DB->update_record('question_answers', $record);
5075 $rs->close();
5077 // In the question table, the code previously used questiontextformat
5078 // for both question text and general feedback. We need to copy the
5079 // values into the new column.
5080 // Then we need to convert FORMAT_MOODLE to FORMAT_HTML (depending on
5081 // $CFG->texteditors).
5082 $DB->execute('
5083 UPDATE {question}
5084 SET generalfeedbackformat = questiontextformat');
5085 // Also save the old questiontextformat, so that plugins that need it
5086 // can access it.
5087 $DB->execute('
5088 UPDATE {question}
5089 SET oldquestiontextformat = questiontextformat');
5090 // Now covert FORMAT_MOODLE content, if necssary.
5091 if ($CFG->texteditors !== 'textarea') {
5092 $rs = $DB->get_recordset('question', array('questiontextformat'=>FORMAT_MOODLE));
5093 foreach ($rs as $record) {
5094 // may take awhile
5095 upgrade_set_timeout();
5096 $record->questiontext = text_to_html($record->questiontext, false, false, true);
5097 $record->questiontextformat = FORMAT_HTML;
5098 $record->generalfeedback = text_to_html($record->generalfeedback, false, false, true);
5099 $record->generalfeedbackformat = FORMAT_HTML;
5100 $DB->update_record('question', $record);
5102 $rs->close();
5105 // In the past, question_sessions.manualcommentformat was always treated
5106 // as FORMAT_HTML.
5107 $DB->set_field('question_sessions', 'manualcommentformat', FORMAT_HTML);
5109 // Main savepoint reached
5110 upgrade_main_savepoint(true, 2010080901);
5113 if ($oldversion < 2010082502) {
5114 // migrate file pool xx/xx/xx directory structure to xx/xx in older 2.0dev installs
5115 upgrade_simplify_overkill_pool_structure();
5116 upgrade_main_savepoint(true, 2010082502);
5119 if ($oldversion < 2010091303) {
5120 // drop all test tables from old xmldb test suite
5121 $table = new xmldb_table('testtable');
5122 if ($dbman->table_exists($table)) {
5123 $dbman->drop_table($table);
5125 $table = new xmldb_table('anothertest');
5126 if ($dbman->table_exists($table)) {
5127 $dbman->drop_table($table);
5129 $table = new xmldb_table('newnameforthetable');
5130 if ($dbman->table_exists($table)) {
5131 $dbman->drop_table($table);
5133 upgrade_main_savepoint(true, 2010091303);
5136 if ($oldversion < 2010091500) {
5138 // Changing precision of field token on table registration_hubs to (255)
5139 $table = new xmldb_table('registration_hubs');
5140 $field = new xmldb_field('token', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'id');
5142 // Launch change of precision for field token
5143 $dbman->change_field_precision($table, $field);
5145 // Main savepoint reached
5146 upgrade_main_savepoint(true, 2010091500);
5149 if ($oldversion < 2010091501) {
5150 // This index used to exist in Moodle 1.9 and was never dropped in the upgrade above.
5151 // Drop it now, or it breaks the following alter column.
5153 // Define index pagetypepattern (not unique) to be dropped form block_instances
5154 $table = new xmldb_table('block_instances');
5155 $index = new xmldb_index('pagetypepattern', XMLDB_INDEX_NOTUNIQUE, array('pagetypepattern'));
5157 // Conditionally launch drop index pagetypepattern
5158 if ($dbman->index_exists($table, $index)) {
5159 $dbman->drop_index($table, $index);
5162 // Main savepoint reached
5163 upgrade_main_savepoint(true, 2010091501);
5166 if ($oldversion < 2010091502) {
5167 // Need to drop the index before we can alter the column precision in the next step.
5169 // Define index parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern (not unique) to be dropped form block_instances
5170 $table = new xmldb_table('block_instances');
5171 $index = new xmldb_index('parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern', XMLDB_INDEX_NOTUNIQUE, array('parentcontextid', 'showinsubcontexts', 'pagetypepattern', 'subpagepattern'));
5173 // Conditionally launch drop index parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern
5174 if ($dbman->index_exists($table, $index)) {
5175 $dbman->drop_index($table, $index);
5178 // Main savepoint reached
5179 upgrade_main_savepoint(true, 2010091502);
5182 if ($oldversion < 2010091503) {
5184 // Changing precision of field pagetypepattern on table block_instances to (64)
5185 $table = new xmldb_table('block_instances');
5186 $field = new xmldb_field('pagetypepattern', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null, 'showinsubcontexts');
5188 // Launch change of precision for field pagetypepattern
5189 $dbman->change_field_precision($table, $field);
5191 // Main savepoint reached
5192 upgrade_main_savepoint(true, 2010091503);
5195 if ($oldversion < 2010091504) {
5196 // Now add the index back.
5198 // Define index parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern (not unique) to be added to block_instances
5199 $table = new xmldb_table('block_instances');
5200 $index = new xmldb_index('parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern', XMLDB_INDEX_NOTUNIQUE, array('parentcontextid', 'showinsubcontexts', 'pagetypepattern', 'subpagepattern'));
5202 // Conditionally launch add index parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern
5203 if (!$dbman->index_exists($table, $index)) {
5204 $dbman->add_index($table, $index);
5207 // Main savepoint reached
5208 upgrade_main_savepoint(true, 2010091504);
5211 if ($oldversion < 2010091505) {
5212 // drop all events queued from 1.9, unfortunately we can not process them because the serialisation of data changed
5213 // also the events format was changed....
5214 $DB->delete_records('events_queue_handlers', array());
5215 $DB->delete_records('events_queue', array());
5217 //reset all status fields too
5218 $DB->set_field('events_handlers', 'status', 0, array());
5220 upgrade_main_savepoint(true, 2010091505);
5223 if ($oldversion < 2010091506) {
5224 // change component string in events_handlers records to new "_" format
5225 if ($handlers = $DB->get_records('events_handlers')) {
5226 foreach ($handlers as $handler) {
5227 $handler->handlermodule = str_replace('/', '_', $handler->handlermodule);
5228 $DB->update_record('events_handlers', $handler);
5231 unset($handlers);
5232 upgrade_main_savepoint(true, 2010091506);
5235 if ($oldversion < 2010091507) {
5237 // Define index eventname-handlermodule (unique) to be dropped form events_handlers
5238 $table = new xmldb_table('events_handlers');
5239 $index = new xmldb_index('eventname-handlermodule', XMLDB_INDEX_UNIQUE, array('eventname', 'handlermodule'));
5241 // Conditionally launch drop index eventname-handlermodule
5242 if ($dbman->index_exists($table, $index)) {
5243 $dbman->drop_index($table, $index);
5246 // Main savepoint reached
5247 upgrade_main_savepoint(true, 2010091507);
5250 if ($oldversion < 2010091508) {
5252 // Rename field handlermodule on table events_handlers to component
5253 $table = new xmldb_table('events_handlers');
5254 $field = new xmldb_field('handlermodule', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null, 'eventname');
5256 // Launch rename field handlermodule
5257 $dbman->rename_field($table, $field, 'component');
5259 // Main savepoint reached
5260 upgrade_main_savepoint(true, 2010091508);
5263 if ($oldversion < 2010091509) {
5265 // Define index eventname-component (unique) to be added to events_handlers
5266 $table = new xmldb_table('events_handlers');
5267 $index = new xmldb_index('eventname-component', XMLDB_INDEX_UNIQUE, array('eventname', 'component'));
5269 // Conditionally launch add index eventname-component
5270 if (!$dbman->index_exists($table, $index)) {
5271 $dbman->add_index($table, $index);
5274 // Main savepoint reached
5275 upgrade_main_savepoint(true, 2010091509);
5278 if ($oldversion < 2010091510) {
5280 // Define field internal to be added to events_handlers
5281 $table = new xmldb_table('events_handlers');
5282 $field = new xmldb_field('internal', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'status');
5284 // Conditionally launch add field internal
5285 if (!$dbman->field_exists($table, $field)) {
5286 $dbman->add_field($table, $field);
5289 // Main savepoint reached
5290 upgrade_main_savepoint(true, 2010091510);
5293 if ($oldversion < 2010091700) {
5294 // Fix MNet sso_jump_url for Moodle application
5295 $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php',
5296 array('name' => 'moodle', 'sso_jump_url' => '/auth/mnet/land.php'));
5297 upgrade_main_savepoint(true, 2010091700);
5300 if ($oldversion < 2010092000) {
5301 // drop multiple field again because it was still in install.xml in 2.0dev
5303 // Define field multiple to be dropped from block
5304 $table = new xmldb_table('block');
5305 $field = new xmldb_field('multiple');
5307 // Conditionally launch drop field multiple
5308 if ($dbman->field_exists($table, $field)) {
5309 $dbman->drop_field($table, $field);
5312 // Main savepoint reached
5313 upgrade_main_savepoint(true, 2010092000);
5316 if ($oldversion < 2010101300) {
5317 // Fix MDL-24641 : the registered language should not be empty otherwise cron will fail
5318 $registeredhubs = $DB->get_records('registration_hubs', array('confirmed' => 1));
5319 if (!empty($registeredhubs)) {
5320 foreach ($registeredhubs as $hub) {
5321 $cleanhuburl = clean_param($hub->huburl, PARAM_ALPHANUMEXT);
5322 $sitelanguage = get_config('hub', 'site_language_' . $cleanhuburl);
5323 if (empty($sitelanguage)) {
5324 set_config('site_language_' . $cleanhuburl, current_language(), 'hub');
5328 upgrade_main_savepoint(true, 2010101300);
5331 //MDL-24721 -add hidden column to grade_categories. This was done previously but it wasn't included in
5332 //install.xml so there are 2.0 sites that are missing it.
5333 if ($oldversion < 2010101900) {
5334 $table = new xmldb_table('grade_categories');
5335 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timemodified');
5337 if (!$dbman->field_exists($table, $field)) {
5338 $dbman->add_field($table, $field);
5341 upgrade_main_savepoint(true, 2010101900);
5344 // new format of the emoticons setting
5345 if ($oldversion < 2010102300) {
5346 unset($CFG->emoticons);
5347 $DB->delete_records('config', array('name' => 'emoticons'));
5348 $DB->delete_records('cache_text'); // changed md5 hash calculation
5349 upgrade_main_savepoint(true, 2010102300);
5352 //MDL-24771
5353 if ($oldversion < 2010102601) {
5355 $fieldnotification = new xmldb_field('notification', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, 0, 'smallmessage');
5356 $fieldcontexturl = new xmldb_field('contexturl', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'notification');
5357 $fieldcontexturlname = new xmldb_field('contexturlname', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'contexturl');
5358 $fieldstoadd = array($fieldnotification, $fieldcontexturl, $fieldcontexturlname);
5360 $tablestomodify = array(new xmldb_table('message'), new xmldb_table('message_read'));
5362 foreach($tablestomodify as $table) {
5363 foreach($fieldstoadd as $field) {
5364 if (!$dbman->field_exists($table, $field)) {
5365 $dbman->add_field($table, $field);
5370 upgrade_main_savepoint(true, 2010102601);
5373 // MDL-24694 needs increasing size of user_preferences.name(varchar[50]) field due to
5374 // long preferences names for messaging which need components parts within the name
5375 // eg: 'message_provider_mod_assignment_assignments_loggedin'
5376 if ($oldversion < 2010102602) {
5378 // Define index userid-name (unique) to be dropped form user_preferences
5379 $table = new xmldb_table('user_preferences');
5380 $index = new xmldb_index('userid-name', XMLDB_INDEX_UNIQUE, array('userid', 'name'));
5382 // Conditionally launch drop index userid-name
5383 if ($dbman->index_exists($table, $index)) {
5384 $dbman->drop_index($table, $index);
5387 // Changing precision of field name on table user_preferences to (255)
5388 $field = new xmldb_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'userid');
5390 // Launch change of precision for field name
5391 $dbman->change_field_precision($table, $field);
5393 // Conditionally launch add index userid-name
5394 if (!$dbman->index_exists($table, $index)) {
5395 $dbman->add_index($table, $index);
5398 // Main savepoint reached
5399 upgrade_main_savepoint(true, 2010102602);
5402 if ($oldversion < 2010102700) {
5404 $table = new xmldb_table('post');
5405 $field = new xmldb_field('uniquehash', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null, 'content');
5406 // Launch change of precision for field name
5407 $dbman->change_field_precision($table, $field);
5409 // Main savepoint reached
5410 upgrade_main_savepoint(true, 2010102700);
5413 if ($oldversion < 2010110200) {
5415 // fix tags itemtype for wiki
5416 $sql = "UPDATE {tag_instance}
5417 SET itemtype = 'wiki_pages'
5418 WHERE itemtype = 'wiki_page'";
5419 $DB->execute($sql);
5421 echo $OUTPUT->notification('Updating tags itemtype', 'notifysuccess');
5423 // Main savepoint reached
5424 upgrade_main_savepoint(true, 2010110200);
5427 //remove forum_logblocked from config. No longer required after user->emailstop was removed
5428 if ($oldversion < 2010110500) {
5429 unset_config('forum_logblocked');
5430 upgrade_main_savepoint(true, 2010110500);
5433 if ($oldversion < 2010110800) {
5434 // convert $CFG->disablecourseajax to $CFG->enablecourseajax
5435 $disabledcourseajax = get_config('disablecourseajax', 0);
5436 if ($disabledcourseajax) {
5437 set_config('enablecourseajax', 0);
5438 } else {
5439 set_config('enablecourseajax', 1);
5441 unset_config('disablecourseajax');
5443 upgrade_main_savepoint(true, 2010110800);
5446 if ($oldversion < 2010111000) {
5448 // Clean up the old scheduled backup settings that are no longer relevant
5449 update_fix_automated_backup_config();
5450 upgrade_main_savepoint(true, 2010111000);
5453 if ($oldversion < 2010111702) {
5455 // Clean up the old experimental split restore no loger used
5456 unset_config('experimentalsplitrestore');
5458 upgrade_main_savepoint(true, 2010111702);
5461 if ($oldversion < 2010121401) {
5463 // Define table profiling to be created
5464 $table = new xmldb_table('profiling');
5466 // Adding fields to table profiling
5467 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
5468 $table->add_field('runid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
5469 $table->add_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
5470 $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
5471 $table->add_field('totalexecutiontime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
5472 $table->add_field('totalcputime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
5473 $table->add_field('totalcalls', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
5474 $table->add_field('totalmemory', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
5475 $table->add_field('runreference', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
5476 $table->add_field('runcomment', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
5477 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
5479 // Adding keys to table profiling
5480 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
5481 $table->add_key('runid_uk', XMLDB_KEY_UNIQUE, array('runid'));
5483 // Adding indexes to table profiling
5484 $table->add_index('url_runreference_ix', XMLDB_INDEX_NOTUNIQUE, array('url', 'runreference'));
5485 $table->add_index('timecreated_runreference_ix', XMLDB_INDEX_NOTUNIQUE, array('timecreated', 'runreference'));
5487 // Conditionally launch create table for profiling
5488 if (!$dbman->table_exists($table)) {
5489 $dbman->create_table($table);
5492 // Main savepoint reached
5493 upgrade_main_savepoint(true, 2010121401);
5496 if ($oldversion < 2011011401) {
5497 $columns = $DB->get_columns('block_instances');
5499 // Check if we need to fix the default weight column
5500 if (array_key_exists('defaultweight', $columns) && $columns['defaultweight']->max_length != 10) {
5501 // Fix discrepancies in the block_instances table after upgrade from 1.9
5502 $table = new xmldb_table('block_instances');
5504 // defaultweight is smallint(3) after upgrade should be bigint 10
5505 // Also fixed in earlier upgrade code
5506 $field = new xmldb_field('defaultweight', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, 'defaultregion');
5507 if ($dbman->field_exists($table, $field)) {
5508 $dbman->change_field_type($table, $field);
5511 // add missing key `blocinst_par_ix` (`parentcontextid`)
5512 $index = new xmldb_index('parentcontextid', XMLDB_INDEX_NOTUNIQUE, array('parentcontextid'));
5513 if (!$dbman->index_exists($table, $index)) {
5514 $dbman->add_index($table, $index);
5518 // Main savepoint reached
5519 upgrade_main_savepoint(true, 2011011401);
5522 if ($oldversion < 2011011402) {
5523 // Fix discrepancies in the block_positions table after upgrade from 1.9
5524 $table = new xmldb_table('block_positions');
5525 $columns = $DB->get_columns('block_positions');
5527 // Check if we need to fix the blockinstanceid field
5528 if (array_key_exists('blockinstanceid', $columns) && empty($columns['blockinstanceid']->unsigned)) {
5529 // Fix blockinstanceid
5530 // First remove the indexs on the field
5531 $indexone = new xmldb_index('blockinstanceid', XMLDB_INDEX_NOTUNIQUE, array('blockinstanceid'));
5532 $indexall = new xmldb_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid','contextid','pagetype','subpage'));
5533 if ($dbman->index_exists($table, $indexone)) {
5534 $dbman->drop_index($table, $indexone);
5536 if ($dbman->index_exists($table, $indexall)) {
5537 $dbman->drop_index($table, $indexall);
5539 // blockinstanceid should be unsigned
5540 // Also fixed in earlier upgrade code
5541 $field = new xmldb_field('blockinstanceid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'id');
5542 if ($dbman->field_exists($table, $field)) {
5543 $dbman->change_field_unsigned($table, $field);
5546 // Add the indexs back in
5547 $dbman->add_index($table, $indexone);
5548 $dbman->add_index($table, $indexall);
5551 // Check if the visible field needs fixing.
5552 if (array_key_exists('visible', $columns) && !empty($columns['visible']->has_default)) {
5553 // visible shouldn't have a default
5554 // Also fixed in earlier upgrade code
5555 $field = new xmldb_field('visible', XMLDB_TYPE_INTEGER, 4, null, XMLDB_NOTNULL, null, null, 'subpage');
5556 if ($dbman->field_exists($table, $field)) {
5557 $dbman->change_field_default($table, $field);
5561 // Main savepoint reached
5562 upgrade_main_savepoint(true, 2011011402);
5565 if ($oldversion < 2011011403) {
5566 $columns = $DB->get_columns('grade_categories');
5567 // Check if we need to fix the hidden field
5568 if (array_key_exists('hidden', $columns) && $columns['hidden']->max_length != 1) {
5569 // Fix discrepancies in the grade_categories table after upgrade from 1.9
5570 $table = new xmldb_table('grade_categories');
5572 // hidden should be tinyint(1)
5573 // Also fixed in earlier upgrade code
5574 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timemodified');
5575 if ($dbman->field_exists($table, $field)) {
5576 $dbman->change_field_precision($table, $field);
5580 // Main savepoint reached
5581 upgrade_main_savepoint(true, 2011011403);
5584 if ($oldversion < 2011011404) {
5585 // Fix discrepancies in the message table after upgrade from 1.9
5586 $columns = $DB->get_columns('message');
5587 $table = new xmldb_table('message');
5589 // Check if we need to fix the useridfrom field
5590 if (array_key_exists('useridfrom', $columns) && empty($columns['useridfrom']->unsigned)) {
5591 // useridfrom should be unsigned
5592 $field = new xmldb_field('useridfrom', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'id');
5593 $index = new xmldb_index('useridfrom', XMLDB_INDEX_NOTUNIQUE, array('useridfrom'));
5594 if ($dbman->index_exists($table, $index)) {
5595 $dbman->drop_index($table, $index);
5597 if ($dbman->field_exists($table, $field)) {
5598 $dbman->change_field_unsigned($table, $field);
5600 $dbman->add_index($table, $index);
5603 // Check if we need to fix the useridto field
5604 if (array_key_exists('useridto', $columns) && empty($columns['useridto']->unsigned)) {
5605 // useridto should be unsigned
5606 $field = new xmldb_field('useridto', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'useridfrom');
5607 $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
5608 if ($dbman->index_exists($table, $index)) {
5609 $dbman->drop_index($table, $index);
5611 if ($dbman->field_exists($table, $field)) {
5612 $dbman->change_field_unsigned($table, $field);
5614 $dbman->add_index($table, $index);
5617 // Check if we need to fix the notification field
5618 if (array_key_exists('notification', $columns) && !empty($columns['notification']->not_null)) {
5619 // notification should allow null
5620 // Fixed in earlier upgrade code
5621 $field = new xmldb_field('notification', XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, null, null, 0, 'smallmessage');
5622 if ($dbman->field_exists($table, $field)) {
5623 $dbman->change_field_notnull($table, $field);
5627 // Check if we need to fix the contexturl field
5628 if (array_key_exists('contexturl', $columns) && strpos($columns['contexturl']->type, 'text') === false) {
5629 // contexturl should be text
5630 // Fixed in earlier upgrade code
5631 $field = new xmldb_field('contexturl', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'notification');
5632 if ($dbman->field_exists($table, $field)) {
5633 $dbman->change_field_type($table, $field);
5637 // Check if we need to fix the contexturl field
5638 if (array_key_exists('contexturlname', $columns) && strpos($columns['contexturlname']->type, 'text') === false) {
5639 // contexturlname should be text
5640 // Fixed in earlier upgrade code
5641 $field = new xmldb_field('contexturlname', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'contexturl');
5642 if ($dbman->field_exists($table, $field)) {
5643 $dbman->change_field_type($table, $field);
5647 // Main savepoint reached
5648 upgrade_main_savepoint(true, 2011011404);
5651 if ($oldversion < 2011011405) {
5652 // Fix discrepancies in the message_read table after upgrade from 1.9
5653 $columns = $DB->get_columns('message_read');
5654 $table = new xmldb_table('message_read');
5656 // Check if we need to fix the useridfrom field
5657 if (array_key_exists('useridfrom', $columns) && empty($columns['useridfrom']->unsigned)) {
5658 // useridfrom should be unsigned
5659 $field = new xmldb_field('useridfrom', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'id');
5660 $index = new xmldb_index('useridfrom', XMLDB_INDEX_NOTUNIQUE, array('useridfrom'));
5661 if ($dbman->index_exists($table, $index)) {
5662 $dbman->drop_index($table, $index);
5664 if ($dbman->field_exists($table, $field)) {
5665 $dbman->change_field_unsigned($table, $field);
5667 $dbman->add_index($table, $index);
5670 // Check if we need to fix the useridto field
5671 if (array_key_exists('useridto', $columns) && empty($columns['useridto']->unsigned)) {
5672 // useridto should be unsigned
5673 $field = new xmldb_field('useridto', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'useridfrom');
5674 $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
5675 if ($dbman->index_exists($table, $index)) {
5676 $dbman->drop_index($table, $index);
5678 if ($dbman->field_exists($table, $field)) {
5679 $dbman->change_field_unsigned($table, $field);
5681 $dbman->add_index($table, $index);
5684 // Check if we need to fix the notification field
5685 if (array_key_exists('notification', $columns) && !empty($columns['notification']->not_null)) {
5686 // notification should allow null
5687 // Fixed in earlier upgrade code
5688 $field = new xmldb_field('notification', XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, null, null, 0, 'smallmessage');
5689 if ($dbman->field_exists($table, $field)) {
5690 $dbman->change_field_notnull($table, $field);
5694 // Check if we need to fix the contexturl field
5695 if (array_key_exists('contexturl', $columns) && strpos($columns['contexturl']->type, 'text') === false) {
5696 // contexturl should be text
5697 // Fixed in earlier upgrade code
5698 $field = new xmldb_field('contexturl', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'notification');
5699 if ($dbman->field_exists($table, $field)) {
5700 $dbman->change_field_type($table, $field);
5704 // Check if we need to fix the contexturl field
5705 if (array_key_exists('contexturlname', $columns) && strpos($columns['contexturlname']->type, 'text') === false) {
5706 // contexturlname should be text
5707 // Fixed in earlier upgrade code
5708 $field = new xmldb_field('contexturlname', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'contexturl');
5709 if ($dbman->field_exists($table, $field)) {
5710 $dbman->change_field_type($table, $field);
5714 // Main savepoint reached
5715 upgrade_main_savepoint(true, 2011011405);
5718 if ($oldversion < 2011011406) {
5719 // Fix discrepancies in the my_pages table after upgrade from 1.9
5720 $columns = $DB->get_columns('my_pages');
5721 $table = new xmldb_table('my_pages');
5723 // Check if we need to fix the private column
5724 if (array_key_exists('private', $columns) && $columns['private']->default_value != '1') {
5725 // private should be default 1
5726 // Fixed in earlier upgrade code
5727 $field = new xmldb_field('private', XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 1, 'name');
5728 $index = new xmldb_index('user_idx', XMLDB_INDEX_NOTUNIQUE, array('userid','private'));
5729 if ($dbman->index_exists($table, $index)) {
5730 $dbman->drop_index($table, $index);
5732 if ($dbman->field_exists($table, $field)) {
5733 $dbman->change_field_default($table, $field);
5735 $dbman->add_index($table, $index);
5738 // Check if we need to fix the sortorder field
5739 if (array_key_exists('sortorder', $columns) && !empty($columns['sortorder']->unsigned)) {
5740 // Sortorder should not be unsigned
5741 // Fixed in earlier upgrade code
5742 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, 6, null, XMLDB_NOTNULL, null, 0, 'private');
5743 if ($dbman->field_exists($table, $field)) {
5744 $dbman->change_field_notnull($table, $field);
5748 upgrade_main_savepoint(true, 2011011406);
5751 if ($oldversion < 2011011407) {
5752 // Check if we need to fix post.uniquehash
5753 $columns = $DB->get_columns('my_pages');
5754 if (array_key_exists('uniquehash', $columns) && $columns['uniquehash']->max_length != 128) {
5755 // Fix discrepancies in the post table after upgrade from 1.9
5756 $table = new xmldb_table('post');
5758 // Uniquehash should be 128 chars
5759 // Fixed in earlier upgrade code
5760 $field = new xmldb_field('uniquehash', XMLDB_TYPE_CHAR, 128, null, XMLDB_NOTNULL, null, null, 'content');
5761 if ($dbman->field_exists($table, $field)) {
5762 $dbman->change_field_precision($table, $field);
5766 upgrade_main_savepoint(true, 2011011407);
5769 if ($oldversion < 2011011408) {
5770 // Fix question in the post table after upgrade from 1.9
5771 $columns = $DB->get_columns('question');
5772 $table = new xmldb_table('question');
5774 // Check if we need to fix default grade
5775 if (array_key_exists('defaultgrade', $columns) && (
5776 empty($columns['defaultgrade']->unsigned) ||
5777 empty($columns['defaultgrade']->not_null) ||
5778 $columns['defaultgrade']->default_value !== '1.0000000')) {
5779 // defaultgrade should be unsigned NOT NULL DEFAULT '1.0000000'
5780 // Fixed in earlier upgrade code
5781 $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1.0000000', 'generalfeedbackformat');
5782 if ($dbman->field_exists($table, $field)) {
5783 $dbman->change_field_default($table, $field);
5787 // Check if we need to fix penalty
5788 if (array_key_exists('penalty', $columns) && (empty($columns['penalty']->not_null) || $columns['penalty']->default_value !== '0.1000000')) {
5789 // penalty should be NOT NULL DEFAULT '0.1000000'
5790 // Fixed in earlier upgrade code
5791 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0.1000000', 'defaultgrade');
5792 if ($dbman->field_exists($table, $field)) {
5793 $dbman->change_field_default($table, $field);
5797 upgrade_main_savepoint(true, 2011011408);
5800 if ($oldversion < 2011011409) {
5801 // Fix question_answers in the post table after upgrade from 1.9
5802 $columns = $DB->get_columns('question_answers');
5803 $table = new xmldb_table('question_answers');
5805 if (array_key_exists('fraction', $columns) && empty($columns['fraction']->not_null)) {
5806 // fraction should be NOT NULL DEFAULT '0.0000000',
5807 // Fixed in earlier upgrade code
5808 $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'feedback');
5809 if ($dbman->field_exists($table, $field)) {
5810 $dbman->change_field_default($table, $field);
5814 upgrade_main_savepoint(true, 2011011409);
5817 if ($oldversion < 2011011410) {
5818 // Fix question_sessions in the post table after upgrade from 1.9
5819 $columns = $DB->get_columns('question_sessions');
5820 $table = new xmldb_table('question_sessions');
5822 // Check if we need to fix sumpenalty
5823 if (array_key_exists('sumpenalty', $columns) && empty($columns['sumpenalty']->not_null)) {
5824 // sumpenalty should be NOT NULL DEFAULT '0.0000000',
5825 // Fixed in earlier upgrade code
5826 $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'newgraded');
5827 if ($dbman->field_exists($table, $field)) {
5828 $dbman->change_field_default($table, $field);
5832 upgrade_main_savepoint(true, 2011011410);
5835 if ($oldversion < 2011011411) {
5836 // Fix question_states in the post table after upgrade from 1.9
5837 $columns = $DB->get_columns('question_states');
5838 $table = new xmldb_table('question_states');
5840 // Check if we need to fix grade
5841 if (array_key_exists('grade', $columns) && empty($columns['grade']->not_null)) {
5842 // grade should be NOT NULL DEFAULT '0.0000000',
5843 // Fixed in earlier upgrade code
5844 $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'event');
5845 if ($dbman->field_exists($table, $field)) {
5846 $dbman->change_field_default($table, $field);
5850 // Check if we need to fix raw_grade
5851 if (array_key_exists('raw_grade', $columns) && empty($columns['raw_grade']->not_null)) {
5852 // raw_grade should be NOT NULL DEFAULT '0.0000000',
5853 // Fixed in earlier upgrade code
5854 $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'grade');
5855 if ($dbman->field_exists($table, $field)) {
5856 $dbman->change_field_default($table, $field);
5860 // Check if we need to fix raw_grade
5861 if (array_key_exists('penalty', $columns) && empty($columns['penalty']->not_null)) {
5862 // penalty should be NOT NULL DEFAULT '0.0000000',
5863 // Fixed in earlier upgrade code
5864 $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'raw_grade');
5865 if ($dbman->field_exists($table, $field)) {
5866 $dbman->change_field_default($table, $field);
5870 upgrade_main_savepoint(true, 2011011411);
5873 if ($oldversion < 2011011412) {
5874 // Fix tag_instance in the post table after upgrade from 1.9
5875 $columns = $DB->get_columns('tag_instance');
5876 $table = new xmldb_table('tag_instance');
5878 // Check if we need to fix tiuserid
5879 if (array_key_exists('tiuserid', $columns) && !empty($columns['tiuserid']->has_default)) {
5880 // tiuserid should have no default
5881 // Fixed in earlier upgrade code
5882 $field = new xmldb_field('tiuserid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
5883 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid', XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
5884 if ($dbman->index_exists($table, $index)) {
5885 $dbman->drop_index($table, $index);
5887 if ($dbman->field_exists($table, $field)) {
5888 $dbman->change_field_default($table, $field);
5890 $dbman->add_index($table, $index);
5893 upgrade_main_savepoint(true, 2011011412);
5896 if ($oldversion < 2011011413) {
5897 // Fix user_info_field in the post table after upgrade from 1.9
5898 $table = new xmldb_table('user_info_field');
5900 // Missing field descriptionformat
5901 // Fixed in earlier upgrade code
5902 $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, 2, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'description');
5903 if (!$dbman->field_exists($table, $field)) {
5904 $dbman->add_field($table, $field);
5907 upgrade_main_savepoint(true, 2011011413);
5910 if ($oldversion < 2011011414) {
5911 // Drop the adodb_logsql table if it exists... it was never actually used anyway.
5912 $table = new xmldb_table('adodb_logsql');
5914 if ($dbman->table_exists($table)) {
5915 $dbman->drop_table($table);
5918 upgrade_main_savepoint(true, 2011011414);
5921 if ($oldversion < 2011011415) {
5922 //create the rating table indexes if required
5923 $table = new xmldb_table('rating');
5925 $index = new xmldb_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
5926 if (!$dbman->index_exists($table, $index)) {
5927 $dbman->add_index($table, $index);
5929 $key = new xmldb_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
5930 $dbman->add_key($table, $key);
5932 $key = new xmldb_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
5933 $dbman->add_key($table, $key);
5936 upgrade_main_savepoint(true, 2011011415);
5939 if ($oldversion < 2011012400) {
5940 // Clean up the old progress tracked roles setting, no longer used (replaced by enrolment)
5941 unset_config('progresstrackedroles');
5942 upgrade_main_savepoint(true, 2011012400);
5945 if ($oldversion < 2011012500) {
5946 $columns = $DB->get_columns('tag_instance');
5947 $table = new xmldb_table('tag_instance');
5949 // Drop and recreate index if tiuserid doesn't have default value
5950 if (array_key_exists('tiuserid', $columns) && empty($columns['tiuserid']->has_default)) {
5951 // Define index itemtype-itemid-tagid-tiuserid (unique) to be dropped form tag_instance
5952 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid', XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
5953 // Conditionally launch drop index itemtype-itemid-tagid-tiuserid
5954 if ($dbman->index_exists($table, $index)) {
5955 $dbman->drop_index($table, $index);
5958 // Changing the default of field tiuserid on table tag_instance to 0
5959 $field = new xmldb_field('tiuserid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'itemid');
5961 // Launch change of default for field tiuserid
5962 $dbman->change_field_default($table, $field);
5964 $index = new xmldb_index('itemtype-itemid-tagid-tiuserid', XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
5966 // Conditionally launch add index itemtype-itemid-tagid-tiuserid
5967 if (!$dbman->index_exists($table, $index)) {
5968 $dbman->add_index($table, $index);
5972 // Main savepoint reached
5973 upgrade_main_savepoint(true, 2011012500);
5976 if ($oldversion < 2011012501) {
5977 //add the index userfieldidx (not unique) to user_info_data
5978 $table = new xmldb_table('user_info_data');
5979 $index = new xmldb_index('userfieldidx', XMLDB_INDEX_NOTUNIQUE, array('userid', 'fieldid'));
5981 if (!$dbman->index_exists($table, $index)) {
5982 $dbman->add_index($table, $index);
5985 upgrade_main_savepoint(true, 2011012501);
5988 if ($oldversion < 2011020200.01) {
5990 // Define field targetversion to be added to upgrade_log
5991 $table = new xmldb_table('upgrade_log');
5992 $field = new xmldb_field('targetversion', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'version');
5994 // Conditionally launch add field targetversion
5995 if (!$dbman->field_exists($table, $field)) {
5996 $dbman->add_field($table, $field);
5999 // Main savepoint reached
6000 upgrade_main_savepoint(true, 2011020200.01);
6003 if ($oldversion < 2011020900.07) {
6004 $DB->delete_records('course_display', array('display' => 0));
6005 upgrade_main_savepoint(true, 2011020900.07);
6008 if ($oldversion < 2011020900.08) {
6009 // Define field secret to be added to registration_hubs
6010 $table = new xmldb_table('registration_hubs');
6011 $field = new xmldb_field('secret', XMLDB_TYPE_CHAR, '255', null, null, null,
6012 null, 'confirmed');
6014 // Conditionally launch add field secret and set its value
6015 if (!$dbman->field_exists($table, $field)) {
6016 $dbman->add_field($table, $field);
6017 $DB->set_field('registration_hubs', 'secret', $CFG->siteidentifier);
6020 // Main savepoint reached
6021 upgrade_main_savepoint(true, 2011020900.08);
6024 if ($oldversion < 2011022100.01) {
6025 // hack alert: inject missing version of manual auth_plugin,
6026 // we need to do it so that we may use upgrade.php there
6028 set_config('version', 2011022100, 'auth_manual');
6029 upgrade_main_savepoint(true, 2011022100.01);
6032 if ($oldversion < 2011052300.00) {
6033 $table = new xmldb_table('rating');
6035 // Add the component field to the ratings table
6036 upgrade_set_timeout(60 * 20);
6037 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, 'unknown', 'contextid');
6038 if (!$dbman->field_exists($table, $field)) {
6039 $dbman->add_field($table, $field);
6042 // Add the ratingarea field to the ratings table
6043 upgrade_set_timeout(60 * 20);
6044 $field = new xmldb_field('ratingarea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, 'unknown', 'component');
6045 if (!$dbman->field_exists($table, $field)) {
6046 $dbman->add_field($table, $field);
6049 upgrade_main_savepoint(true, 2011052300.00);
6052 if ($oldversion < 2011052300.01) {
6054 // Define index uniqueuserrating (unique) to be added to rating
6055 $table = new xmldb_table('rating');
6056 $index = new xmldb_index('uniqueuserrating', XMLDB_INDEX_NOTUNIQUE, array('component', 'ratingarea', 'contextid', 'itemid'));
6058 // Conditionally launch add index uniqueuserrating
6059 if (!$dbman->index_exists($table, $index)) {
6060 $dbman->add_index($table, $index);
6063 // Main savepoint reached
6064 upgrade_main_savepoint(true, 2011052300.01);
6067 if ($oldversion < 2011052300.02) {
6069 // Define index itemid (not unique) to be dropped form rating
6070 $table = new xmldb_table('rating');
6071 $index = new xmldb_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
6073 // Conditionally launch drop index itemid
6074 if ($dbman->index_exists($table, $index)) {
6075 $dbman->drop_index($table, $index);
6078 // Main savepoint reached
6079 upgrade_main_savepoint(true, 2011052300.02);
6082 // Question engine 2 changes (14) start here
6083 if ($oldversion < 2011060300) {
6084 // Changing the default of field penalty on table question to 0.3333333
6085 $table = new xmldb_table('question');
6086 $field = new xmldb_field('penalty');
6087 $field->set_attributes(XMLDB_TYPE_NUMBER, '12, 7', null,
6088 XMLDB_NOTNULL, null, '0.3333333');
6090 // Launch change of default for field penalty
6091 $dbman->change_field_default($table, $field);
6093 // quiz savepoint reached
6094 upgrade_main_savepoint(true, 2011060300);
6097 if ($oldversion < 2011060301) {
6099 // Rename field defaultgrade on table question to defaultmark
6100 $table = new xmldb_table('question');
6101 $field = new xmldb_field('defaultgrade');
6102 $field->set_attributes(XMLDB_TYPE_NUMBER, '12, 7', null,
6103 XMLDB_NOTNULL, null, '1');
6105 // Launch rename field defaultmark
6106 if ($dbman->field_exists($table, $field)) {
6107 $dbman->rename_field($table, $field, 'defaultmark');
6110 // quiz savepoint reached
6111 upgrade_main_savepoint(true, 2011060301);
6114 if ($oldversion < 2011060302) {
6116 // Rename the question_attempts table to question_usages.
6117 $table = new xmldb_table('question_attempts');
6118 if (!$dbman->table_exists('question_usages')) {
6119 $dbman->rename_table($table, 'question_usages');
6122 // quiz savepoint reached
6123 upgrade_main_savepoint(true, 2011060302);
6126 if ($oldversion < 2011060303) {
6128 // Rename the modulename field to component ...
6129 $table = new xmldb_table('question_usages');
6130 $field = new xmldb_field('modulename');
6131 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null,
6132 XMLDB_NOTNULL, null, null, 'contextid');
6134 if ($dbman->field_exists($table, $field)) {
6135 $dbman->rename_field($table, $field, 'component');
6138 // ... and update its contents.
6139 $DB->set_field('question_usages', 'component', 'mod_quiz', array('component' => 'quiz'));
6141 // Add the contextid field.
6142 $field = new xmldb_field('contextid');
6143 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6144 null, null, null, 'id');
6145 if (!$dbman->field_exists($table, $field)) {
6146 $dbman->add_field($table, $field);
6148 // And populate it.
6149 $quizmoduleid = $DB->get_field('modules', 'id', array('name' => 'quiz'));
6150 $DB->execute("
6151 UPDATE {question_usages} SET contextid = (
6152 SELECT ctx.id
6153 FROM {context} ctx
6154 JOIN {course_modules} cm ON cm.id = ctx.instanceid AND cm.module = $quizmoduleid
6155 JOIN {quiz_attempts} quiza ON quiza.quiz = cm.instance
6156 WHERE ctx.contextlevel = " . CONTEXT_MODULE . "
6157 AND quiza.uniqueid = {question_usages}.id
6161 // It seems that it is possible, in old versions of Moodle, for a
6162 // quiz_attempt to be deleted while the question_attempt remains.
6163 // In that situation we still get NULLs left in the table, which
6164 // causes the upgrade to break at the next step. To avoid breakage,
6165 // without risking dataloss, we just replace all NULLs with 0 here.
6166 $DB->set_field_select('question_usages', 'contextid', 0, 'contextid IS NULL');
6168 // Then make it NOT NULL.
6169 $field = new xmldb_field('contextid');
6170 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6171 XMLDB_NOTNULL, null, null, 'id');
6172 $dbman->change_field_notnull($table, $field);
6175 // Add the preferredbehaviour column. Populate it with a dummy value
6176 // for now. We will fill in the appropriate behaviour name when
6177 // updating all the rest of the attempt data.
6178 $field = new xmldb_field('preferredbehaviour');
6179 if (!$dbman->field_exists($table, $field)) {
6180 $field->set_attributes(XMLDB_TYPE_CHAR, '32', null,
6181 XMLDB_NOTNULL, null, 'to_be_set_later', 'component');
6182 $dbman->add_field($table, $field);
6184 // Then remove the default value, now the column is populated.
6185 $field = new xmldb_field('preferredbehaviour');
6186 $field->set_attributes(XMLDB_TYPE_CHAR, '32', null,
6187 XMLDB_NOTNULL, null, null, 'component');
6188 $dbman->change_field_default($table, $field);
6191 // quiz savepoint reached
6192 upgrade_main_savepoint(true, 2011060303);
6195 if ($oldversion < 2011060304) {
6197 // Define key contextid (foreign) to be added to question_usages
6198 $table = new xmldb_table('question_usages');
6199 $key = new XMLDBKey('contextid');
6200 $key->set_attributes(XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
6202 // Launch add key contextid
6203 $dbman->add_key($table, $key);
6205 // quiz savepoint reached
6206 upgrade_main_savepoint(true, 2011060304);
6209 if ($oldversion < 2011060305) {
6211 // Changing precision of field component on table question_usages to (255)
6212 // This was missed during the upgrade from old versions.
6213 $table = new xmldb_table('question_usages');
6214 $field = new xmldb_field('component');
6215 $field->set_attributes(XMLDB_TYPE_CHAR, '255', null,
6216 XMLDB_NOTNULL, null, null, 'contextid');
6218 // Launch change of precision for field component
6219 $dbman->change_field_precision($table, $field);
6221 // quiz savepoint reached
6222 upgrade_main_savepoint(true, 2011060305);
6225 if ($oldversion < 2011060306) {
6227 // Define table question_attempts to be created
6228 $table = new xmldb_table('question_attempts');
6229 if (!$dbman->table_exists($table)) {
6231 // Adding fields to table question_attempts
6232 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6233 XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
6234 $table->add_field('questionusageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6235 XMLDB_NOTNULL, null, null);
6236 $table->add_field('slot', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6237 XMLDB_NOTNULL, null, null);
6238 $table->add_field('behaviour', XMLDB_TYPE_CHAR, '32', null,
6239 XMLDB_NOTNULL, null, null);
6240 $table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6241 XMLDB_NOTNULL, null, null);
6242 $table->add_field('maxmark', XMLDB_TYPE_NUMBER, '12, 7', null,
6243 XMLDB_NOTNULL, null, null);
6244 $table->add_field('minfraction', XMLDB_TYPE_NUMBER, '12, 7', null,
6245 XMLDB_NOTNULL, null, null);
6246 $table->add_field('flagged', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
6247 XMLDB_NOTNULL, null, '0');
6248 $table->add_field('questionsummary', XMLDB_TYPE_TEXT, 'small', null,
6249 null, null, null);
6250 $table->add_field('rightanswer', XMLDB_TYPE_TEXT, 'small', null,
6251 null, null, null);
6252 $table->add_field('responsesummary', XMLDB_TYPE_TEXT, 'small', null,
6253 null, null, null);
6254 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6255 XMLDB_NOTNULL, null, null);
6257 // Adding keys to table question_attempts
6258 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
6259 $table->add_key('questionid', XMLDB_KEY_FOREIGN, array('questionid'),
6260 'question', array('id'));
6261 $table->add_key('questionusageid', XMLDB_KEY_FOREIGN, array('questionusageid'),
6262 'question_usages', array('id'));
6264 // Adding indexes to table question_attempts
6265 $table->add_index('questionusageid-slot', XMLDB_INDEX_UNIQUE,
6266 array('questionusageid', 'slot'));
6268 // Launch create table for question_attempts
6269 $dbman->create_table($table);
6272 // quiz savepoint reached
6273 upgrade_main_savepoint(true, 2011060306);
6276 if ($oldversion < 2011060307) {
6278 // Define table question_attempt_steps to be created
6279 $table = new xmldb_table('question_attempt_steps');
6280 if (!$dbman->table_exists($table)) {
6282 // Adding fields to table question_attempt_steps
6283 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6284 XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
6285 $table->add_field('questionattemptid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6286 XMLDB_NOTNULL, null, null);
6287 $table->add_field('sequencenumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6288 XMLDB_NOTNULL, null, null);
6289 $table->add_field('state', XMLDB_TYPE_CHAR, '13', null,
6290 XMLDB_NOTNULL, null, null);
6291 $table->add_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null,
6292 null, null, null);
6293 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6294 XMLDB_NOTNULL, null, null);
6295 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6296 null, null, null);
6298 // Adding keys to table question_attempt_steps
6299 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
6300 $table->add_key('questionattemptid', XMLDB_KEY_FOREIGN,
6301 array('questionattemptid'), 'question_attempts_new', array('id'));
6302 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'),
6303 'user', array('id'));
6305 // Adding indexes to table question_attempt_steps
6306 $table->add_index('questionattemptid-sequencenumber', XMLDB_INDEX_UNIQUE,
6307 array('questionattemptid', 'sequencenumber'));
6309 // Launch create table for question_attempt_steps
6310 $dbman->create_table($table);
6313 // quiz savepoint reached
6314 upgrade_main_savepoint(true, 2011060307);
6317 if ($oldversion < 2011060308) {
6319 // Define table question_attempt_step_data to be created
6320 $table = new xmldb_table('question_attempt_step_data');
6321 if (!$dbman->table_exists($table)) {
6323 // Adding fields to table question_attempt_step_data
6324 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6325 XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
6326 $table->add_field('attemptstepid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6327 XMLDB_NOTNULL, null, null);
6328 $table->add_field('name', XMLDB_TYPE_CHAR, '32', null,
6329 XMLDB_NOTNULL, null, null);
6330 $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null,
6331 null, null, null);
6333 // Adding keys to table question_attempt_step_data
6334 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
6335 $table->add_key('attemptstepid', XMLDB_KEY_FOREIGN, array('attemptstepid'),
6336 'question_attempt_steps', array('id'));
6338 // Adding indexes to table question_attempt_step_data
6339 $table->add_index('attemptstepid-name', XMLDB_INDEX_UNIQUE,
6340 array('attemptstepid', 'name'));
6342 // Launch create table for question_attempt_step_data
6343 $dbman->create_table($table);
6346 // quiz savepoint reached
6347 upgrade_main_savepoint(true, 2011060308);
6350 if ($oldversion < 2011060309) {
6352 // Define table question_hints to be created
6353 $table = new xmldb_table('question_hints');
6355 // Adding fields to table question_hints
6356 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6357 XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
6358 $table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6359 XMLDB_NOTNULL, null, null);
6360 $table->add_field('hint', XMLDB_TYPE_TEXT, 'small', null,
6361 XMLDB_NOTNULL, null, null);
6362 $table->add_field('hintformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED,
6363 XMLDB_NOTNULL, null, '0');
6364 $table->add_field('shownumcorrect', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
6365 null, null, null);
6366 $table->add_field('clearwrong', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
6367 null, null, null);
6368 $table->add_field('options', XMLDB_TYPE_CHAR, '255', null,
6369 null, null, null);
6371 // Adding keys to table question_hints
6372 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
6373 $table->add_key('questionid', XMLDB_KEY_FOREIGN, array('questionid'),
6374 'question', array('id'));
6376 // Conditionally launch create table for question_hints
6377 if (!$dbman->table_exists($table)) {
6378 $dbman->create_table($table);
6381 // quiz savepoint reached
6382 upgrade_main_savepoint(true, 2011060309);
6385 if ($oldversion < 2011060310) {
6387 // In the past, question_answer fractions were stored with rather
6388 // sloppy rounding. Now update them to the new standard of 7 d.p.
6389 $changes = array(
6390 '-0.66666' => '-0.6666667',
6391 '-0.33333' => '-0.3333333',
6392 '-0.16666' => '-0.1666667',
6393 '-0.142857' => '-0.1428571',
6394 '0.11111' => '0.1111111',
6395 '0.142857' => '0.1428571',
6396 '0.16666' => '0.1666667',
6397 '0.33333' => '0.3333333',
6398 '0.333333' => '0.3333333',
6399 '0.66666' => '0.6666667',
6401 foreach ($changes as $from => $to) {
6402 $DB->set_field('question_answers',
6403 'fraction', $to, array('fraction' => $from));
6406 // quiz savepoint reached
6407 upgrade_main_savepoint(true, 2011060310);
6410 if ($oldversion < 2011060311) {
6412 // In the past, question penalties were stored with rather
6413 // sloppy rounding. Now update them to the new standard of 7 d.p.
6414 $DB->set_field('question',
6415 'penalty', 0.3333333, array('penalty' => 33.3));
6416 $DB->set_field_select('question',
6417 'penalty', 0.3333333, 'penalty >= 0.33 AND penalty <= 0.34');
6418 $DB->set_field_select('question',
6419 'penalty', 0.6666667, 'penalty >= 0.66 AND penalty <= 0.67');
6420 $DB->set_field_select('question',
6421 'penalty', 1, 'penalty > 1');
6423 // quiz savepoint reached
6424 upgrade_main_savepoint(true, 2011060311);
6427 if ($oldversion < 2011060312) {
6429 // Define field hintformat to be added to question_hints table.
6430 $table = new xmldb_table('question_hints');
6431 $field = new xmldb_field('hintformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED,
6432 XMLDB_NOTNULL, null, '0');
6434 // Conditionally launch add field partiallycorrectfeedbackformat
6435 if (!$dbman->field_exists($table, $field)) {
6436 $dbman->add_field($table, $field);
6439 upgrade_main_savepoint(true, 2011060312);
6442 if ($oldversion < 2011060313) {
6443 // Define field variant to be added to question_attempts
6444 $table = new xmldb_table('question_attempts');
6445 $field = new xmldb_field('variant', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
6446 XMLDB_NOTNULL, null, 1, 'questionid');
6448 // Launch add field component
6449 if (!$dbman->field_exists($table, $field)) {
6450 $dbman->add_field($table, $field);
6453 // Main savepoint reached
6454 upgrade_main_savepoint(true, 2011060313);
6456 // Question engine 2 changes (14) end here
6458 if ($oldversion < 2011060500) {
6460 // Define index uniqueuserrating (not unique) to be dropped from rating
6461 $table = new xmldb_table('rating');
6462 $index = new xmldb_index('uniqueuserrating', XMLDB_INDEX_NOTUNIQUE,
6463 array('component', 'ratingarea', 'contextid', 'itemid'));
6465 // Drop dependent index before changing fields specs
6466 if ($dbman->index_exists($table, $index)) {
6467 $dbman->drop_index($table, $index);
6470 // Changing the default of field component on table rating to drop it
6471 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'contextid');
6473 // Launch change of default for field component
6474 $dbman->change_field_default($table, $field);
6476 // Changing the default of field ratingarea on table rating to drop it
6477 $field = new xmldb_field('ratingarea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'component');
6479 // Launch change of default for field ratingarea
6480 $dbman->change_field_default($table, $field);
6482 // Add dependent index back
6483 if (!$dbman->index_exists($table, $index)) {
6484 $dbman->add_index($table, $index);
6487 // Main savepoint reached
6488 upgrade_main_savepoint(true, 2011060500);
6491 if ($oldversion < 2011060800) {
6492 // Add enabled field to message_processors
6493 $table = new xmldb_table('message_processors');
6494 $field = new xmldb_field('enabled');
6495 $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'name');
6497 // Launch add field addition
6498 if (!$dbman->field_exists($table,$field)) {
6499 $dbman->add_field($table, $field);
6502 // Populate default messaging settings
6503 upgrade_populate_default_messaging_prefs();
6505 upgrade_main_savepoint(true, 2011060800);
6508 if ($oldversion < 2011060800.01) { //TODO: put the right latest version
6509 // Define field shortname to be added to external_services
6510 $table = new xmldb_table('external_services');
6511 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'timemodified');
6513 // Conditionally launch add field shortname
6514 if (!$dbman->field_exists($table, $field)) {
6515 $dbman->add_field($table, $field);
6518 // Main savepoint reached
6519 upgrade_main_savepoint(true, 2011060800.01);
6522 if ($oldversion < 2011062000.01) {
6523 // Changing sign of field minfraction on table question_attempts to signed
6524 $table = new xmldb_table('question_attempts');
6525 $field = new xmldb_field('minfraction', XMLDB_TYPE_NUMBER, '12, 7', null,
6526 XMLDB_NOTNULL, null, null, 'maxmark');
6528 // Launch change of sign for field minfraction
6529 $dbman->change_field_unsigned($table, $field);
6531 // Main savepoint reached
6532 upgrade_main_savepoint(true, 2011062000.01);
6535 // Signed fixes - MDL-28032
6536 if ($oldversion < 2011062400.02) {
6538 // Changing sign of field defaultmark on table question to unsigned
6539 $table = new xmldb_table('question');
6540 $field = new xmldb_field('defaultmark', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '1', 'generalfeedbackformat');
6542 // Launch change of sign for field defaultmark
6543 $dbman->change_field_unsigned($table, $field);
6545 // Main savepoint reached
6546 upgrade_main_savepoint(true, 2011062400.02);
6549 if ($oldversion < 2011062400.03) {
6550 // Completion system has issue in which possible duplicate rows are
6551 // added to the course_modules_completion table. This change deletes
6552 // the older version of duplicate rows and replaces an index with a
6553 // unique one so it won't happen again.
6555 // This would have been a single query but because MySQL is a PoS
6556 // and can't do subqueries in DELETE, I have made it into two. The
6557 // system is unlikely to run out of memory as only IDs are stored in
6558 // the array.
6560 // Find all rows cmc1 where there is another row cmc2 with the
6561 // same user id and the same coursemoduleid, but a higher id (=> newer,
6562 // meaning that cmc1 is an older row).
6563 $rs = $DB->get_recordset_sql("
6564 SELECT DISTINCT
6565 cmc1.id
6566 FROM
6567 {course_modules_completion} cmc1
6568 JOIN {course_modules_completion} cmc2
6569 ON cmc2.userid = cmc1.userid
6570 AND cmc2.coursemoduleid = cmc1.coursemoduleid
6571 AND cmc2.id > cmc1.id");
6572 $deleteids = array();
6573 foreach ($rs as $row) {
6574 $deleteids[] = $row->id;
6576 $rs->close();
6577 // Note: SELECT part performance tested on table with ~7m
6578 // rows of which ~15k match, only took 30 seconds so probably okay.
6580 // Delete all those rows
6581 $DB->delete_records_list('course_modules_completion', 'id', $deleteids);
6583 // Define index userid (not unique) to be dropped form course_modules_completion
6584 $table = new xmldb_table('course_modules_completion');
6585 $index = new xmldb_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
6587 // Conditionally launch drop index userid
6588 if ($dbman->index_exists($table, $index)) {
6589 $dbman->drop_index($table, $index);
6592 // Define index userid-coursemoduleid (unique) to be added to course_modules_completion
6593 $index = new xmldb_index('userid-coursemoduleid', XMLDB_INDEX_UNIQUE,
6594 array('userid', 'coursemoduleid'));
6596 // Conditionally launch add index userid-coursemoduleid
6597 if (!$dbman->index_exists($table, $index)) {
6598 $dbman->add_index($table, $index);
6601 upgrade_main_savepoint(true, 2011062400.03);
6604 // Moodle v2.1.0 release upgrade line
6605 // Put any upgrade step following this
6607 if ($oldversion < 2011070800.01) {
6608 // remove unused config options
6609 unset_config('excludeoldflashclients');
6610 upgrade_main_savepoint(true, 2011070800.01);
6613 if ($oldversion < 2011071300.01) {
6614 // remove unused config option
6615 unset_config('framename');
6616 upgrade_main_savepoint(true, 2011071300.01);
6619 if ($oldversion < 2011081700.01) {
6620 // Remove category_sortorder index that was supposed to be removed long time ago
6621 $table = new xmldb_table('course');
6622 $index = new xmldb_index('category_sortorder', XMLDB_INDEX_UNIQUE, array('category', 'sortorder'));
6624 if ($dbman->index_exists($table, $index)) {
6625 $dbman->drop_index($table, $index);
6627 upgrade_main_savepoint(true, 2011081700.01);
6630 if ($oldversion < 2011081700.02) {
6631 // remove safety block backup from 2.0 upgrade
6632 $table = new xmldb_table('block_pinned_old');
6633 if ($dbman->table_exists($table)) {
6634 $dbman->drop_table($table);
6636 $table = new xmldb_table('block_instance_old');
6637 if ($dbman->table_exists($table)) {
6638 $dbman->drop_table($table);
6640 upgrade_main_savepoint(true, 2011081700.02);
6643 if ($oldversion < 2011083100.02) {
6644 // Define field showdescription to be added to course_modules
6645 $table = new xmldb_table('course_modules');
6646 $field = new xmldb_field('showdescription', XMLDB_TYPE_INTEGER, '1',
6647 XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'showavailability');
6649 // Conditionally launch add field showdescription
6650 if (!$dbman->field_exists($table, $field)) {
6651 $dbman->add_field($table, $field);
6654 // Main savepoint reached
6655 upgrade_main_savepoint(true, 2011083100.02);
6658 if ($oldversion < 2011090700.01) {
6659 // Changing the default of field secret on table registration_hubs to NULL
6660 $table = new xmldb_table('registration_hubs');
6661 $field = new xmldb_field('secret', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'confirmed');
6663 // Launch change of default for field secret
6664 $dbman->change_field_default($table, $field);
6666 // Main savepoint reached
6667 upgrade_main_savepoint(true, 2011090700.01);
6670 if ($oldversion < 2011091200.00) {
6671 //preference not required since 2.0
6672 $DB->delete_records('user_preferences', array('name'=>'message_showmessagewindow'));
6674 //re-introducing emailstop. check that its turned off so people dont suddenly stop getting notifications
6675 $DB->set_field('user', 'emailstop', 0, array('emailstop' => 1));
6677 upgrade_main_savepoint(true, 2011091200.00);
6680 if ($oldversion < 2011091300.00) {
6681 // Increase the length of the of the course shortname field as it is now going
6682 // to be consistently filtered and 100 characters is practically useless for
6683 // things like the multilang filter.
6685 $table = new xmldb_table('course');
6686 $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'fullname');
6687 $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
6689 // First check the shortname field exists... pretty heavy mod if it doesnt!
6690 if ($dbman->field_exists($table, $field)) {
6691 // Conditionally launch drop index shortname, this is required to happen
6692 // before we can edit the field.
6693 if ($dbman->index_exists($table, $index)) {
6694 $dbman->drop_index($table, $index);
6697 // Launch change of precision for field shortname
6698 $dbman->change_field_precision($table, $field);
6699 // Add the index back to the table now that we're finished our mods
6700 $dbman->add_index($table, $index);
6703 // Main savepoint reached
6704 upgrade_main_savepoint(true, 2011091300.00);
6707 if ($oldversion < 2011091600.01) {
6708 // It has been decided that it is now safe to drop the backup_log table
6709 // as it hasn't been used within 2+.
6711 // Define table backup_log to be dropped
6712 $table = new xmldb_table('backup_log');
6714 // Conditionally launch drop table for backup_log
6715 if ($dbman->table_exists($table)) {
6716 $dbman->drop_table($table);
6719 // Main savepoint reached
6720 upgrade_main_savepoint(true, 2011091600.01);
6723 if ($oldversion < 2011092800.01) {
6724 // Check for potential missing columns in the grade_items_history
6726 $table = new xmldb_table('grade_items_history');
6727 $field = new xmldb_field('display', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, 'sortorder');
6728 if (!$dbman->field_exists($table, $field)) {
6729 $dbman->add_field($table, $field);
6731 $field = new xmldb_field('decimals', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, 'display');
6732 if (!$dbman->field_exists($table, $field)) {
6733 $dbman->add_field($table, $field);
6734 } else {
6735 //check that the grade_items_history.decimals allows nulls
6736 //Somehow some Moodle databases have this column marked as "not null"
6737 $columns = $DB->get_columns('grade_items_history');
6738 if (array_key_exists('display', $columns) && !empty($columns['display']->not_null)) {
6739 $dbman->change_field_notnull($table, $field);
6743 // Main savepoint reached
6744 upgrade_main_savepoint(true, 2011092800.01);
6747 if ($oldversion < 2011092800.02) {
6748 // Check for potential missing columns in the grade_categories_history
6750 $table = new xmldb_table('grade_categories_history');
6751 $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timemodified');
6752 if (!$dbman->field_exists($table, $field)) {
6753 $dbman->add_field($table, $field);
6756 // Main savepoint reached
6757 upgrade_main_savepoint(true, 2011092800.02);
6760 if ($oldversion < 2011092800.03) {
6761 // Check for potential missing columns in the grade_outcomes_history
6763 $table = new xmldb_table('grade_outcomes_history');
6764 $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'description');
6765 if (!$dbman->field_exists($table, $field)) {
6766 $dbman->add_field($table, $field);
6769 // Main savepoint reached
6770 upgrade_main_savepoint(true, 2011092800.03);
6773 if ($oldversion < 2011100700.02) {
6774 // Define field idnumber to be added to course_categories
6775 $table = new xmldb_table('course_categories');
6776 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'name');
6778 // Conditionally launch add field idnumber
6779 if (!$dbman->field_exists($table,$field)) {
6780 $dbman->add_field($table, $field);
6783 // Main savepoint reached
6784 upgrade_main_savepoint(true, 2011100700.02);
6787 if ($oldversion < 2011101200.01) {
6788 // The conditional availability date system used to rely on dates being
6789 // set to 23:59:59 for the end date, but now that exact times are
6790 // supported, it uses midnight on the following day.
6792 // The query is restricted on 'time mod 10 = 9' in order that
6793 // it is safe to run this upgrade twice if something goes wrong.
6794 $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil + 1 ' .
6795 'WHERE availableuntil > 0 AND ' . $DB->sql_modulo('availableuntil', 10) . ' = 9');
6797 // Because availableuntil is stored in modinfo, we need to clear modinfo
6798 // for all courses.
6799 rebuild_course_cache(0, true);
6801 // Main savepoint reached
6802 upgrade_main_savepoint(true, 2011101200.01);
6805 if ($oldversion < 2011101900.02) {
6806 // remove unused setting
6807 unset_config('enablehtmlpurifier');
6808 upgrade_main_savepoint(true, 2011101900.02);
6811 if ($oldversion < 2011102700.01) {
6812 // purge everything related to abandoned experimental global search
6814 // unset setting - this disables it in case user does not delete the dirs
6815 unset_config('enableglobalsearch');
6817 // Delete block, instances and db table
6818 $table = new xmldb_table('block_search_documents');
6819 if ($dbman->table_exists($table)) {
6820 $instances = $DB->get_records('block_instances', array('blockname'=>'search'));
6821 foreach($instances as $instance) {
6822 $context = context_block::instance($instance->id);
6823 upgrade_cleanup_unwanted_block_contexts(array($context->id)); // do not use standard block deleting MDL-30517, there is no extra stuff used in block, so this should be safe
6824 $DB->delete_records('block_positions', array('blockinstanceid' => $instance->id));
6825 $DB->delete_records('block_instances', array('id' => $instance->id));
6827 $DB->delete_records('block', array('name'=>'search'));
6829 $dbman->drop_table($table);
6832 // purge all settings used by the search block
6833 $like = $DB->sql_like('name', '?', true, true, false, '|');
6834 $params = array($DB->sql_like_escape('block_search_', '|') . '%', $DB->sql_like_escape('search_in_', '|') . '%');
6835 $settings = $DB->get_records_select('config', "$like OR $like", $params);
6836 foreach ($settings as $setting) {
6837 unset_config($setting->name);
6840 upgrade_main_savepoint(true, 2011102700.01);
6843 if ($oldversion < 2011110200.01) {
6844 // Rename 'extrauserselectorfields' to 'showuseridentity' as it is
6845 // being used more widely
6846 if (isset($CFG->extrauserselectorfields)) {
6847 set_config('showuseridentity', $CFG->extrauserselectorfields);
6848 unset_config('extrauserselectorfields');
6850 unset_config('grade_report_showuseridnumber');
6851 upgrade_main_savepoint(true, 2011110200.01);
6854 if ($oldversion < 2011110200.02) {
6855 // create new core tables for the advanced grading methods framework
6857 // grading_areas table
6858 $table = new xmldb_table('grading_areas');
6859 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
6860 $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
6861 $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
6862 $table->add_field('areaname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
6863 $table->add_field('activemethod', XMLDB_TYPE_CHAR, '100', null, null, null, null);
6864 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
6865 $table->add_key('uq_gradable_area', XMLDB_KEY_UNIQUE, array('contextid', 'component', 'areaname'));
6866 $table->add_key('fk_context', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
6868 if (!$dbman->table_exists($table)) {
6869 $dbman->create_table($table);
6872 // grading_definitions table
6873 $table = new xmldb_table('grading_definitions');
6874 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
6875 $table->add_field('areaid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
6876 $table->add_field('method', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
6877 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
6878 $table->add_field('description', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
6879 $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null);
6880 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
6881 $table->add_field('copiedfromid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
6882 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
6883 $table->add_field('usercreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
6884 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
6885 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
6886 $table->add_field('timecopied', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
6887 $table->add_field('options', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
6888 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
6889 $table->add_key('fk_areaid', XMLDB_KEY_FOREIGN, array('areaid'), 'grading_areas', array('id'));
6890 $table->add_key('fk_usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
6891 $table->add_key('uq_area_method', XMLDB_KEY_UNIQUE, array('areaid', 'method'));
6892 $table->add_key('fk_usercreated', XMLDB_KEY_FOREIGN, array('usercreated'), 'user', array('id'));
6894 if (!$dbman->table_exists($table)) {
6895 $dbman->create_table($table);
6898 // grading_instances table
6899 $table = new xmldb_table('grading_instances');
6900 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
6901 $table->add_field('definitionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
6902 $table->add_field('raterid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
6903 $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
6904 $table->add_field('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null);
6905 $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
6906 $table->add_field('feedback', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
6907 $table->add_field('feedbackformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null);
6908 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
6909 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
6910 $table->add_key('fk_definitionid', XMLDB_KEY_FOREIGN, array('definitionid'), 'grading_definitions', array('id'));
6911 $table->add_key('fk_raterid', XMLDB_KEY_FOREIGN, array('raterid'), 'user', array('id'));
6913 if (!$dbman->table_exists($table)) {
6914 $dbman->create_table($table);
6917 upgrade_main_savepoint(true, 2011110200.02);
6920 if ($oldversion < 2011111500.01) {
6921 upgrade_set_timeout(60*20); // this may take a while
6922 // Remove duplicate entries from groupings_groups table
6923 $sql = 'SELECT MIN(id) AS firstid, groupingid, groupid FROM {groupings_groups} '.
6924 'GROUP BY groupingid, groupid HAVING COUNT(id)>1';
6925 $badrs = $DB->get_recordset_sql($sql);
6926 foreach ($badrs as $badrec) {
6927 $where = 'groupingid = ? and groupid = ? and id > ?';
6928 $params = array($badrec->groupingid, $badrec->groupid, $badrec->firstid);
6929 $DB->delete_records_select('groupings_groups', $where, $params);
6931 $badrs->close();
6933 // Main savepoint reached
6934 upgrade_main_savepoint(true, 2011111500.01);
6937 if ($oldversion < 2011111800.01) {
6938 // Define field downloadfiles to be added to external_services
6939 $table = new xmldb_table('external_services');
6940 $field = new xmldb_field('downloadfiles', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'shortname');
6942 // Conditionally launch add field downloadfiles
6943 if (!$dbman->field_exists($table, $field)) {
6944 $dbman->add_field($table, $field);
6947 // Main savepoint reached
6948 upgrade_main_savepoint(true, 2011111800.01);
6951 // Moodle v2.2.0 release upgrade line
6952 // Put any upgrade step following this
6954 if ($oldversion < 2011120500.02) {
6956 upgrade_set_timeout(60*20); // This may take a while
6957 // MDL-28180. Some missing restrictions in certain backup & restore operations
6958 // were causing incorrect duplicates in the course_completion_aggr_methd table.
6959 // This upgrade step takes rid of them.
6960 $sql = 'SELECT course, criteriatype, MIN(id) AS minid
6961 FROM {course_completion_aggr_methd}
6962 GROUP BY course, criteriatype
6963 HAVING COUNT(*) > 1';
6964 $duprs = $DB->get_recordset_sql($sql);
6965 foreach ($duprs as $duprec) {
6966 // We need to handle NULLs in criteriatype diferently
6967 if (is_null($duprec->criteriatype)) {
6968 $where = 'course = ? AND criteriatype IS NULL AND id > ?';
6969 $params = array($duprec->course, $duprec->minid);
6970 } else {
6971 $where = 'course = ? AND criteriatype = ? AND id > ?';
6972 $params = array($duprec->course, $duprec->criteriatype, $duprec->minid);
6974 $DB->delete_records_select('course_completion_aggr_methd', $where, $params);
6976 $duprs->close();
6978 // Main savepoint reached
6979 upgrade_main_savepoint(true, 2011120500.02);
6982 if ($oldversion < 2011120500.01) {
6984 // Changing precision of field value on table user_preferences to (1333)
6985 $table = new xmldb_table('user_preferences');
6986 $field = new xmldb_field('value', XMLDB_TYPE_CHAR, '1333', null, XMLDB_NOTNULL, null, null, 'name');
6988 // Launch change of precision for field value
6989 $dbman->change_field_precision($table, $field);
6991 // Main savepoint reached
6992 upgrade_main_savepoint(true, 2011120500.01);
6995 return true;