Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/db/upgrade.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00044 defined('MOODLE_INTERNAL') || die();
00045 
00055 function xmldb_main_upgrade($oldversion) {
00056     global $CFG, $USER, $DB, $OUTPUT;
00057 
00058     require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
00059 
00060     $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
00061 
00065 
00066     if ($oldversion < 2008030600) {
00067         //NOTE: this table was added much later later in dev cycle, but we need it here, upgrades from pre PR1 not supported
00068 
00070         $table = new xmldb_table('upgrade_log');
00071 
00073         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00074         $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
00075         $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
00076         $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
00077         $table->add_field('targetversion', XMLDB_TYPE_CHAR, '100', null, null, null, null);
00078         $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00079         $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
00080         $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
00081         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00082         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00083 
00085         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00086         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
00087 
00089         $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
00090         $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
00091 
00093         $dbman->create_table($table);
00094 
00096         upgrade_main_savepoint(true, 2008030600);
00097     }
00098 
00099     if ($oldversion < 2008030601) {
00100         //NOTE: this table was added much later later in dev cycle, but we need it here, upgrades from pre PR1 not supported
00101 
00103         $table = new xmldb_table('log_queries');
00104 
00106         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00107         $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00108         $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
00109         $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
00110         $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
00111         $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
00112         $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
00113         $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
00114         $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00115 
00117         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00118 
00120         $dbman->create_table($table);
00121 
00123         upgrade_main_savepoint(true, 2008030601);
00124     }
00125 
00126     if ($oldversion < 2008030602) {
00127         @unlink($CFG->cachedir.'/languages');
00128 
00129         if (file_exists("$CFG->dataroot/lang")) {
00130             // rename old lang directory so that the new and old langs do not mix
00131             if (rename("$CFG->dataroot/lang", "$CFG->dataroot/oldlang")) {
00132                 $oldlang = "$CFG->dataroot/oldlang";
00133             } else {
00134                 $oldlang = "$CFG->dataroot/lang";
00135             }
00136         } else {
00137             $oldlang = '';
00138         }
00139         // TODO: fetch previously installed languages ("*_utf8") found in $oldlang from moodle.org
00140         upgrade_set_timeout(60*20); // this may take a while
00141 
00142 
00143         // TODO: add some info file to $oldlang describing what to do with "$oldlang/*_utf8_local" dirs
00144 
00145 
00146         // Main savepoint reached
00147         upgrade_main_savepoint(true, 2008030602);
00148     }
00149 
00150     if ($oldversion < 2008030700) {
00151         upgrade_set_timeout(60*20); // this may take a while
00152 
00154         $table = new xmldb_table('grade_letters');
00155         $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
00156 
00158         if ($dbman->index_exists($table, $index)) {
00159             $dbman->drop_index($table, $index);
00160         }
00161 
00163         $table = new xmldb_table('grade_letters');
00164         $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
00165 
00167         $dbman->add_index($table, $index);
00168 
00170         upgrade_main_savepoint(true, 2008030700);
00171     }
00172 
00173     if ($oldversion < 2008050100) {
00174         // Update courses that used weekscss to weeks
00175         $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
00176         upgrade_main_savepoint(true, 2008050100);
00177     }
00178 
00179     if ($oldversion < 2008050200) {
00180         // remove unused config options
00181         unset_config('statsrolesupgraded');
00182         upgrade_main_savepoint(true, 2008050200);
00183     }
00184 
00185     if ($oldversion < 2008050700) {
00186         upgrade_set_timeout(60*20); // this may take a while
00187 
00189         require_once($CFG->dirroot . '/question/upgrade.php');
00190         question_fix_random_question_parents();
00191         upgrade_main_savepoint(true, 2008050700);
00192     }
00193 
00194     if ($oldversion < 2008051201) {
00195         echo $OUTPUT->notification('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
00196         upgrade_set_timeout(60*20); // this may take a while
00197 
00199         $dbfamily = $DB->get_dbfamily();
00200         if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
00201             $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
00202         }
00203 
00205         $table = new xmldb_table('user');
00206         $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
00207 
00209         if ($dbman->index_exists($table, $index)) {
00210             $dbman->drop_index($table, $index);
00211         }
00212 
00214         $table = new xmldb_table('user');
00215         $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
00216 
00218         $dbman->change_field_precision($table, $field);
00219 
00221         $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
00222         $dbman->add_index($table, $index);
00223 
00225         upgrade_main_savepoint(true, 2008051201);
00226     }
00227 
00228     if ($oldversion < 2008051203) {
00229         $table = new xmldb_table('mnet_enrol_course');
00230         $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
00231         $dbman->change_field_precision($table, $field);
00232         upgrade_main_savepoint(true, 2008051203);
00233     }
00234 
00235     if ($oldversion < 2008063001) {
00236         upgrade_set_timeout(60*20); // this may take a while
00237 
00238         // table to be modified
00239         $table = new xmldb_table('tag_instance');
00240         // add field
00241         $field = new xmldb_field('tiuserid');
00242         if (!$dbman->field_exists($table, $field)) {
00243             $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
00244             $dbman->add_field($table, $field);
00245         }
00246         // modify index
00247         $index = new xmldb_index('itemtype-itemid-tagid');
00248         $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
00249         if ($dbman->index_exists($table, $index)) {
00250             $dbman->drop_index($table, $index);
00251         }
00252         $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
00253         $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
00254         if (!$dbman->index_exists($table, $index)) {
00255             $dbman->add_index($table, $index);
00256         }
00257 
00259         upgrade_main_savepoint(true, 2008063001);
00260     }
00261 
00262     if ($oldversion < 2008070300) {
00263         $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
00264         upgrade_main_savepoint(true, 2008070300);
00265     }
00266 
00267     if ($oldversion < 2008070701) {
00268 
00270         $table = new xmldb_table('portfolio_instance');
00271 
00273         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00274         $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
00275         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00276         $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
00277 
00279         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00280 
00282         if (!$dbman->table_exists($table)) {
00283             $dbman->create_table($table);
00284         }
00286         $table = new xmldb_table('portfolio_instance_config');
00287 
00289         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00290         $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00291         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00292         $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
00293 
00295         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00296         $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
00297 
00299         $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
00300 
00302         if (!$dbman->table_exists($table)) {
00303             $dbman->create_table($table);
00304         }
00305 
00307         $table = new xmldb_table('portfolio_instance_user');
00308 
00310         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
00311         $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00312         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00313         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00314         $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
00315 
00317         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00318         $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
00319         $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
00320 
00322         if (!$dbman->table_exists($table)) {
00323             $dbman->create_table($table);
00324         }
00325 
00327         upgrade_main_savepoint(true, 2008070701);
00328     }
00329 
00330     if ($oldversion < 2008072400) {
00332         $table = new xmldb_table('message_processors');
00333         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00334         $table->add_field('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null);
00335         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00336         $dbman->create_table($table);
00337 
00339         $table = new xmldb_table('message');
00340         $field = new xmldb_field('messagetype');
00341         $dbman->drop_field($table, $field);
00342 
00344         $field = new xmldb_field('message');
00345         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
00346         $dbman->rename_field($table, $field, 'fullmessage');
00347         $field = new xmldb_field('format');
00348         $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
00349         $dbman->rename_field($table, $field, 'fullmessageformat');
00350 
00352         $field = new xmldb_field('subject');
00353         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
00354         $dbman->add_field($table, $field);
00355         $field = new xmldb_field('fullmessagehtml');
00356         $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
00357         $dbman->add_field($table, $field);
00358         $field = new xmldb_field('smallmessage');
00359         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
00360         $dbman->add_field($table, $field);
00361 
00362 
00363         $table = new xmldb_table('message_read');
00364         $field = new xmldb_field('messagetype');
00365         $dbman->drop_field($table, $field);
00366         $field = new xmldb_field('mailed');
00367         $dbman->drop_field($table, $field);
00368 
00370         $field = new xmldb_field('message');
00371         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
00372         $dbman->rename_field($table, $field, 'fullmessage');
00373         $field = new xmldb_field('format');
00374         $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
00375         $dbman->rename_field($table, $field, 'fullmessageformat');
00376 
00377 
00379         $field = new xmldb_field('subject');
00380         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
00381         $dbman->add_field($table, $field);
00382         $field = new xmldb_field('fullmessagehtml');
00383         $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
00384         $dbman->add_field($table, $field);
00385         $field = new xmldb_field('smallmessage');
00386         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
00387         $dbman->add_field($table, $field);
00388 
00390         $table = new xmldb_table('message_working');
00391         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00392         $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00393         $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00394         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00395         $dbman->create_table($table);
00396 
00397 
00398         upgrade_main_savepoint(true, 2008072400);
00399     }
00400 
00401     if ($oldversion < 2008072800) {
00402 
00404         $table = new xmldb_table('course');
00405         $field = new xmldb_field('enablecompletion');
00406         $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
00407 
00409         if (!$dbman->field_exists($table,$field)) {
00410             $dbman->add_field($table, $field);
00411         }
00412 
00414         $table = new xmldb_table('course_modules');
00415         $field = new xmldb_field('completion');
00416         $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'groupmembersonly');
00417 
00419         if (!$dbman->field_exists($table,$field)) {
00420             $dbman->add_field($table, $field);
00421         }
00422 
00424         $field = new xmldb_field('completiongradeitemnumber');
00425         $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'completion');
00426 
00428         if (!$dbman->field_exists($table,$field)) {
00429             $dbman->add_field($table, $field);
00430         }
00431 
00433         $field = new xmldb_field('completionview');
00434         $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiongradeitemnumber');
00435 
00437         if (!$dbman->field_exists($table,$field)) {
00438             $dbman->add_field($table, $field);
00439         }
00440 
00442         $field = new xmldb_field('completionexpected');
00443         $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionview');
00444 
00446         if (!$dbman->field_exists($table,$field)) {
00447             $dbman->add_field($table, $field);
00448         }
00449 
00451         $table = new xmldb_table('course_modules_completion');
00452         if (!$dbman->table_exists($table)) {
00453 
00455             $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00456             $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00457             $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00458             $table->add_field('completionstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00459             $table->add_field('viewed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
00460             $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00461 
00463             $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00464 
00466             $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
00467             $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
00468 
00470             $dbman->create_table($table);
00471         }
00472 
00474         upgrade_main_savepoint(true, 2008072800);
00475     }
00476 
00477     if ($oldversion < 2008073000) {
00478 
00480         $table = new xmldb_table('portfolio_log');
00481 
00483         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00484         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00485         $table->add_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00486         $table->add_field('portfolio', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00487         $table->add_field('caller_class', XMLDB_TYPE_CHAR, '150', null, XMLDB_NOTNULL, null, null);
00488         $table->add_field('caller_file', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00489         $table->add_field('caller_sha1', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00490         $table->add_field('tempdataid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
00491         $table->add_field('returnurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00492         $table->add_field('continueurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00493 
00495         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00496         $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
00497         $table->add_key('portfoliofk', XMLDB_KEY_FOREIGN, array('portfolio'), 'portfolio_instance', array('id'));
00498 
00500         if (!$dbman->table_exists($table)) {
00501             $dbman->create_table($table);
00502         }
00503 
00505         upgrade_main_savepoint(true, 2008073000);
00506     }
00507 
00508     if ($oldversion < 2008073104) {
00510         $table = new xmldb_table('message_providers');
00511         if ($dbman->table_exists($table)) {
00512             $dbman->drop_table($table);
00513         }
00514 
00516         $table = new xmldb_table('message_providers');
00517 
00519         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00520         $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
00521         $table->add_field('component', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
00522         $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
00523 
00525         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00526 
00528         $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
00529 
00531         $dbman->create_table($table);
00532 
00533         upgrade_main_savepoint(true, 2008073104);
00534     }
00535 
00536     if ($oldversion < 2008073111) {
00538         $table = new xmldb_table('files');
00539 
00541         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00542         $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
00543         $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
00544         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00545         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
00546         $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
00547         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00548         $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00549         $table->add_field('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00550         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
00551         $table->add_field('filesize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00552         $table->add_field('mimetype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
00553         $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
00554         $table->add_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
00555         $table->add_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null);
00556         $table->add_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null);
00557         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00558         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00559 
00561         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00562         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
00563         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
00564 
00566         $table->add_index('component-filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('component', 'filearea', 'contextid', 'itemid'));
00567         $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
00568         $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
00569 
00571         $dbman->create_table($table);
00572 
00574         upgrade_main_savepoint(true, 2008073111);
00575     }
00576 
00577     if ($oldversion < 2008073112) {
00578         // Define field legacyfiles to be added to course
00579         $table = new xmldb_table('course');
00580         $field = new xmldb_field('legacyfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'maxbytes');
00581 
00582         // Launch add field legacyfiles
00583         $dbman->add_field($table, $field);
00584         // enable legacy files in all courses
00585         $DB->execute("UPDATE {course} SET legacyfiles = 2");
00586 
00587         // Main savepoint reached
00588         upgrade_main_savepoint(true, 2008073112);
00589     }
00590 
00591     if ($oldversion < 2008073113) {
00593         upgrade_migrate_files_courses();
00595         upgrade_main_savepoint(true, 2008073113);
00596     }
00597 
00598     if ($oldversion < 2008073114) {
00600         upgrade_migrate_files_blog();
00602         upgrade_main_savepoint(true, 2008073114);
00603     }
00604 
00605     if ($oldversion < 2008080400) {
00606         // Add field ssl_jump_url to mnet application, and populate existing default applications
00607         $table = new xmldb_table('mnet_application');
00608         $field = new xmldb_field('sso_jump_url');
00609         if (!$dbman->field_exists($table, $field)) {
00610             $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00611             $dbman->add_field($table, $field);
00612             $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
00613             $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
00614         }
00615 
00617         upgrade_main_savepoint(true, 2008080400);
00618     }
00619 
00620     if ($oldversion < 2008080500) {
00621 
00623         $table = new xmldb_table('portfolio_tempdata');
00624 
00626         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00627         $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
00628         $table->add_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00629         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00630         $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', null, null, null, '0');
00631 
00633         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00634         $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
00635         $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
00636 
00638         if (!$dbman->table_exists($table)) {
00639             $dbman->create_table($table);
00640         }
00641 
00643         upgrade_main_savepoint(true, 2008080500);
00644     }
00645 
00646     if ($oldversion < 2008081500) {
00648         $table = new xmldb_table('question');
00649         $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1.0000000', 'generalfeedback');
00650         $dbman->change_field_type($table, $field);
00651         upgrade_main_savepoint(true, 2008081500);
00652     }
00653 
00654     if ($oldversion < 2008081501) {
00655         $table = new xmldb_table('question');
00656         $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0.1000000', 'defaultgrade');
00657         $dbman->change_field_type($table, $field);
00658         upgrade_main_savepoint(true, 2008081501);
00659     }
00660 
00661     if ($oldversion < 2008081502) {
00662         $table = new xmldb_table('question_answers');
00663         $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'answer');
00664         $dbman->change_field_type($table, $field);
00665         upgrade_main_savepoint(true, 2008081502);
00666     }
00667 
00668     if ($oldversion < 2008081503) {
00669         $table = new xmldb_table('question_sessions');
00670         $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'newgraded');
00671         $dbman->change_field_type($table, $field);
00672         upgrade_main_savepoint(true, 2008081503);
00673     }
00674 
00675     if ($oldversion < 2008081504) {
00676         $table = new xmldb_table('question_states');
00677         $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'event');
00678         $dbman->change_field_type($table, $field);
00679         upgrade_main_savepoint(true, 2008081504);
00680     }
00681 
00682     if ($oldversion < 2008081505) {
00683         $table = new xmldb_table('question_states');
00684         $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'grade');
00685         $dbman->change_field_type($table, $field);
00686         upgrade_main_savepoint(true, 2008081505);
00687     }
00688 
00689     if ($oldversion < 2008081506) {
00690         $table = new xmldb_table('question_states');
00691         $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'raw_grade');
00692         $dbman->change_field_type($table, $field);
00693         upgrade_main_savepoint(true, 2008081506);
00694     }
00695 
00696     if ($oldversion < 2008081600) {
00697 
00699         unset_config('unicodedb');
00700 
00702         upgrade_main_savepoint(true, 2008081600);
00703     }
00704 
00705     if ($oldversion < 2008082602) {
00706 
00708         $table = new xmldb_table('repository');
00709 
00711         if ($dbman->table_exists($table)) {
00712             $dbman->drop_table($table);
00713         }
00714 
00716         $table = new xmldb_table('repository');
00717 
00719         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00720         $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00721         $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1');
00722         $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
00723 
00725         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00726 
00728         if (!$dbman->table_exists($table)) {
00729             $dbman->create_table($table);
00730         }
00731 
00733         $table = new xmldb_table('repository_instances');
00734 
00736         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00737         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00738         $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00739         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
00740         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00741         $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null);
00742         $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
00743         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
00744         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
00745         $table->add_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
00746 
00748         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00749 
00751         if (!$dbman->table_exists($table)) {
00752             $dbman->create_table($table);
00753         }
00754 
00756         $table = new xmldb_table('repository_instance_config');
00757 
00759         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00760         $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00761         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00762         $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
00763 
00765         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00766 
00768         if (!$dbman->table_exists($table)) {
00769             $dbman->create_table($table);
00770         }
00771 
00773         upgrade_main_savepoint(true, 2008082602);
00774     }
00775 
00776     if ($oldversion < 2008082700) {
00779 
00781         $table = new xmldb_table('question_sessions');
00782         $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
00783 
00785         if (!$dbman->field_exists($table, $field)) {
00786             $dbman->add_field($table, $field);
00787         }
00788 
00790         upgrade_main_savepoint(true, 2008082700);
00791     }
00792 
00793     if ($oldversion < 2008082900) {
00794 
00796         $table = new xmldb_table('mnet_rpc');
00797         $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
00798 
00800         $dbman->change_field_precision($table, $field);
00801 
00803         upgrade_main_savepoint(true, 2008082900);
00804     }
00805 
00806     // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
00807     if ($oldversion < 2008091000) {
00808         foreach (get_object_vars($CFG) as $name => $value) {
00809             if (substr($name, strlen($name) - 8) !== '_version') {
00810                 continue;
00811             }
00812             $pluginname = substr($name, 0, strlen($name) - 8);
00813             if (!strpos($pluginname, '_')) {
00814                 // Skip things like backup_version that don't contain an extra _
00815                 continue;
00816             }
00817             if ($pluginname == 'enrol_ldap_version') {
00818                 // Special case - this is something different from a plugin version number.
00819                 continue;
00820             }
00821             if (!preg_match('/^\d{10}$/', $value)) {
00822                 // Extra safety check, skip anything that does not look like a Moodle
00823                 // version number (10 digits).
00824                 continue;
00825             }
00826             set_config('version', $value, $pluginname);
00827             unset_config($name);
00828         }
00829         upgrade_main_savepoint(true, 2008091000);
00830     }
00831 
00832     if ($oldversion < 2008092300) {
00833         unset_config('editorspelling');
00834         unset_config('editordictionary');
00836         upgrade_main_savepoint(true, 2008092300);
00837     }
00838 
00839     if ($oldversion < 2008101300) {
00840 
00841         if (!get_config(NULL, 'statsruntimedays')) {
00842             set_config('statsruntimedays', '31');
00843         }
00844 
00846         upgrade_main_savepoint(true, 2008101300);
00847     }
00848 
00850     if ($oldversion < 2008111200) {
00851         $table = new xmldb_table('course');
00852 
00854         $field = new xmldb_field('teacher');
00855         if ($dbman->field_exists($table, $field)) {
00856             $dbman->drop_field($table, $field);
00857         }
00858 
00860         $field = new xmldb_field('teachers');
00861         if ($dbman->field_exists($table, $field)) {
00862             $dbman->drop_field($table, $field);
00863         }
00864 
00866         $field = new xmldb_field('student');
00867         if ($dbman->field_exists($table, $field)) {
00868             $dbman->drop_field($table, $field);
00869         }
00870 
00872         $field = new xmldb_field('students');
00873         if ($dbman->field_exists($table, $field)) {
00874             $dbman->drop_field($table, $field);
00875         }
00876 
00878         upgrade_main_savepoint(true, 2008111200);
00879     }
00880 
00882     if ($oldversion < 2008111800) {
00883 
00885         $table = new xmldb_table('role');
00886         $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
00887 
00889         if (!$dbman->index_exists($table, $index)) {
00890             $dbman->add_index($table, $index);
00891         }
00892 
00894         upgrade_main_savepoint(true, 2008111800);
00895     }
00896 
00898     if ($oldversion < 2008111801) {
00899 
00901         $table = new xmldb_table('role');
00902         $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
00903 
00905         if (!$dbman->index_exists($table, $index)) {
00906             $dbman->add_index($table, $index);
00907         }
00908 
00910         upgrade_main_savepoint(true, 2008111801);
00911     }
00912 
00913     if ($oldversion < 2008120700) {
00914 
00916         $table = new xmldb_table('course_request');
00917         $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
00918 
00921         $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
00923         if ($dbman->index_exists($table, $index)) {
00924             $dbman->drop_index($table, $index);
00925         }
00926 
00928         $dbman->change_field_precision($table, $field);
00929 
00932         $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
00934         if (!$dbman->index_exists($table, $index)) {
00935             $dbman->add_index($table, $index);
00936         }
00937 
00939         upgrade_main_savepoint(true, 2008120700);
00940     }
00941 
00942     if ($oldversion < 2008120801) {
00943 
00945         $table = new xmldb_table('mnet_enrol_course');
00946         $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
00947 
00949         $dbman->change_field_precision($table, $field);
00950 
00952         upgrade_main_savepoint(true, 2008120801);
00953     }
00954 
00955     if ($oldversion < 2008121701) {
00956 
00958         $table = new xmldb_table('course_modules');
00959         $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
00960 
00962         if (!$dbman->field_exists($table, $field)) {
00963             $dbman->add_field($table, $field);
00964         }
00965 
00967         $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
00968 
00970         if (!$dbman->field_exists($table, $field)) {
00971             $dbman->add_field($table, $field);
00972         }
00973 
00975         $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
00976 
00978         if (!$dbman->field_exists($table, $field)) {
00979             $dbman->add_field($table, $field);
00980         }
00981 
00983         $table = new xmldb_table('course_modules_availability');
00984 
00986         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00987         $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00988         $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
00989         $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
00990         $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
00991         $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
00992         $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
00993 
00995         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00996         $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
00997         $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
00998         $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
00999 
01001         if (!$dbman->table_exists($table)) {
01002             $dbman->create_table($table);
01003         }
01004 
01006         require_once($CFG->dirroot . '/course/lib.php');
01007         rebuild_course_cache(0, true);
01008 
01010         upgrade_main_savepoint(true, 2008121701);
01011     }
01012 
01013     if ($oldversion < 2009010500) {
01015         unset_config('session_error_counter');
01016 
01018         upgrade_main_savepoint(true, 2009010500);
01019     }
01020 
01021     if ($oldversion < 2009010600) {
01022 
01024         $table = new xmldb_table('question_states');
01025         $field = new xmldb_field('originalquestion');
01026 
01028         if ($dbman->field_exists($table, $field)) {
01029             $dbman->drop_field($table, $field);
01030         }
01031 
01033         upgrade_main_savepoint(true, 2009010600);
01034     }
01035 
01036     if ($oldversion < 2009010601) {
01037 
01039         $table = new xmldb_table('log');
01040         $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
01041 
01043         $dbman->change_field_precision($table, $field);
01044 
01046         upgrade_main_savepoint(true, 2009010601);
01047     }
01048 
01049     if ($oldversion < 2009010602) {
01050 
01052         $table = new xmldb_table('user');
01053         $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
01054 
01056         $dbman->change_field_precision($table, $field);
01057 
01059         upgrade_main_savepoint(true, 2009010602);
01060     }
01061 
01062     if ($oldversion < 2009010603) {
01063 
01065         $table = new xmldb_table('mnet_host');
01066         $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
01067 
01069         $dbman->change_field_precision($table, $field);
01070 
01072         upgrade_main_savepoint(true, 2009010603);
01073     }
01074 
01075     if ($oldversion < 2009010604) {
01076 
01078         $table = new xmldb_table('mnet_log');
01079         $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
01080 
01082         $dbman->change_field_precision($table, $field);
01083 
01085         upgrade_main_savepoint(true, 2009010604);
01086     }
01087 
01088     if ($oldversion < 2009011000) {
01089 
01091         $table = new xmldb_table('block_instance');
01092         $field = new xmldb_field('configdata');
01093         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
01094 
01096         $dbman->change_field_notnull($table, $field);
01097 
01099         upgrade_main_savepoint(true, 2009011000);
01100     }
01101 
01102     if ($oldversion < 2009011100) {
01104         unset_config('zip');
01105         unset_config('unzip');
01106         unset_config('adminblocks_initialised');
01107 
01109         upgrade_main_savepoint(true, 2009011100);
01110     }
01111 
01112     if ($oldversion < 2009011101) {
01114         $configs = $DB->get_records('backup_config');
01115         foreach ($configs as $config) {
01116             set_config($config->name, $config->value, 'backup');
01117         }
01118 
01120         $table = new xmldb_table('backup_config');
01121 
01123         $dbman->drop_table($table);
01124 
01126         upgrade_main_savepoint(true, 2009011101);
01127     }
01128 
01129     if ($oldversion < 2009011303) {
01130 
01132         $table = new xmldb_table('config_log');
01133 
01135         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
01136         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01137         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01138         $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
01139         $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
01140         $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
01141         $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
01142 
01144         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
01145         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
01146 
01148         $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
01149 
01151         $dbman->create_table($table);
01152 
01154         upgrade_main_savepoint(true, 2009011303);
01155     }
01156 
01157     if ($oldversion < 2009011900) {
01158 
01160         $table = new xmldb_table('sessions2');
01161 
01163         if ($dbman->table_exists($table)) {
01164             $dbman->drop_table($table);
01165         }
01166 
01168         $table = new xmldb_table('sessions');
01169 
01171         if ($dbman->table_exists($table)) {
01172             $dbman->drop_table($table);
01173         }
01174 
01176         $table = new xmldb_table('sessions');
01177 
01179         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
01180         $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
01181         $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
01182         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01183         $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
01184         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01185         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01186         $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
01187         $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
01188 
01190         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
01191         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
01192 
01194         $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
01195         $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
01196         $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
01197         $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
01198 
01200         $dbman->create_table($table);
01201 
01203         upgrade_main_savepoint(true, 2009011900);
01204     }
01205 
01206     if ($oldversion < 2009021800) {
01207         // Converting format of grade conditions, if any exist, to percentages.
01208         $DB->execute("
01209 UPDATE {course_modules_availability} SET grademin=(
01210     SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
01211         /(gi.grademax-gi.grademin)
01212     FROM {grade_items} gi
01213     WHERE gi.id={course_modules_availability}.gradeitemid)
01214 WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
01215         $DB->execute("
01216 UPDATE {course_modules_availability} SET grademax=(
01217     SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
01218         /(gi.grademax-gi.grademin)
01219     FROM {grade_items} gi
01220     WHERE gi.id={course_modules_availability}.gradeitemid)
01221 WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
01222 
01224         upgrade_main_savepoint(true, 2009021800);
01225     }
01226 
01228     if ($oldversion < 2009030300) {
01229         set_config('multichoice_sortorder', 1, 'question');
01230         set_config('truefalse_sortorder', 2, 'question');
01231         set_config('shortanswer_sortorder', 3, 'question');
01232         set_config('numerical_sortorder', 4, 'question');
01233         set_config('calculated_sortorder', 5, 'question');
01234         set_config('essay_sortorder', 6, 'question');
01235         set_config('match_sortorder', 7, 'question');
01236         set_config('randomsamatch_sortorder', 8, 'question');
01237         set_config('multianswer_sortorder', 9, 'question');
01238         set_config('description_sortorder', 10, 'question');
01239         set_config('random_sortorder', 11, 'question');
01240         set_config('missingtype_sortorder', 12, 'question');
01241 
01242         upgrade_main_savepoint(true, 2009030300);
01243     }
01244 
01247     if ($oldversion < 2009032000) {
01249             $table = new xmldb_table('role_allow_switch');
01250 
01252         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
01253         $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01254         $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01255 
01257         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
01258         $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
01259         $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
01260 
01262         $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
01263 
01265         if (!$dbman->table_exists($table)) {
01266             $dbman->create_table($table);
01267         }
01268 
01270         upgrade_main_savepoint(true, 2009032000);
01271     }
01272 
01273     if ($oldversion < 2009032001) {
01275         $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
01276                 SELECT roleid, allowassign FROM {role_allow_assign}');
01277 
01279         unset_config('allowuserswitchrolestheycantassign');
01280 
01282         upgrade_main_savepoint(true, 2009032001);
01283     }
01284 
01285     if ($oldversion < 2009040300) {
01286 
01288         $table = new xmldb_table('filter_active');
01289 
01291         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
01292         $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
01293         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01294         $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
01295         $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
01296 
01298         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
01299         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
01300 
01302         $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
01303 
01305         if (!$dbman->table_exists($table)) {
01306             $dbman->create_table($table);
01307         }
01308 
01310         upgrade_main_savepoint(true, 2009040300);
01311     }
01312 
01313     if ($oldversion < 2009040301) {
01314 
01316         $table = new xmldb_table('filter_config');
01317 
01319         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
01320         $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
01321         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01322         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
01323         $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
01324 
01326         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
01327         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
01328 
01330         $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
01331 
01333         if (!$dbman->table_exists($table)) {
01334             $dbman->create_table($table);
01335         }
01336 
01338         upgrade_main_savepoint(true, 2009040301);
01339     }
01340 
01341     if ($oldversion < 2009040302) {
01343         $disabledfilters = filter_get_all_installed();
01344         if (empty($CFG->textfilters)) {
01345             $activefilters = array();
01346         } else {
01347             $activefilters = explode(',', $CFG->textfilters);
01348         }
01349         $syscontext = get_context_instance(CONTEXT_SYSTEM);
01350         $sortorder = 1;
01351         foreach ($activefilters as $filter) {
01352             filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
01353             $sortorder += 1;
01354             unset($disabledfilters[$filter]);
01355         }
01356         foreach ($disabledfilters as $filter => $notused) {
01357             filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
01358             $sortorder += 1;
01359         }
01360 
01362         upgrade_main_savepoint(true, 2009040302);
01363     }
01364 
01365     if ($oldversion < 2009040600) {
01367         if (empty($CFG->stringfilters)) {
01368             if (!empty($CFG->filterall)) {
01369                 set_config('stringfilters', $CFG->textfilters);
01370             } else {
01371                 set_config('stringfilters', '');
01372             }
01373         }
01374 
01375         set_config('filterall', !empty($CFG->stringfilters));
01376         unset_config('textfilters');
01377 
01379         upgrade_main_savepoint(true, 2009040600);
01380     }
01381 
01382     if ($oldversion < 2009041700) {
01386         $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
01387         require_once($CFG->dirroot . '/course/lib.php');
01388         rebuild_course_cache(0, true);
01389 
01391         upgrade_main_savepoint(true, 2009041700);
01392     }
01393 
01394     if ($oldversion < 2009042600) {
01396         require_once($CFG->dirroot.'/message/lib.php');
01398         if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
01399                                                     FROM {user} u
01400                                                     JOIN {message} m ON m.useridfrom = u.id
01401                                                    WHERE u.deleted = ?', array(1))) {
01402             foreach ($deletedusers as $deleteduser) {
01403                 message_move_userfrom_unread2read($deleteduser->id); // move messages
01404             }
01405         }
01407         upgrade_main_savepoint(true, 2009042600);
01408     }
01409 
01411     if ($oldversion < 2009042700) {
01412 
01414         $table = new xmldb_table('stats_daily');
01415         $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
01416 
01418         $dbman->drop_enum_from_field($table, $field);
01419 
01421         $table = new xmldb_table('stats_weekly');
01422         $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
01423 
01425         $dbman->drop_enum_from_field($table, $field);
01426 
01428         $table = new xmldb_table('stats_monthly');
01429         $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
01430 
01432         $dbman->drop_enum_from_field($table, $field);
01433 
01435         $table = new xmldb_table('post');
01436         $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
01437 
01439         $dbman->drop_enum_from_field($table, $field);
01440 
01442         upgrade_main_savepoint(true, 2009042700);
01443     }
01444 
01445     if ($oldversion < 2009043000) {
01446         unset_config('grade_report_showgroups');
01447         upgrade_main_savepoint(true, 2009043000);
01448     }
01449 
01450     if ($oldversion < 2009050600) {
01452         $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
01453 
01455         upgrade_main_savepoint(true, 2009050600);
01456     }
01457 
01458     if ($oldversion < 2009050601) {
01459 
01461         $table = new xmldb_table('block_instance');
01462 
01464         $dbman->rename_table($table, 'block_instances');
01465 
01467         upgrade_main_savepoint(true, 2009050601);
01468     }
01469 
01470     if ($oldversion < 2009050602) {
01471 
01473         $table = new xmldb_table('block_pinned');
01474 
01476         $dbman->rename_table($table, 'block_pinned_old');
01477 
01479         upgrade_main_savepoint(true, 2009050602);
01480     }
01481 
01482     if ($oldversion < 2009050603) {
01483 
01485         $table = new xmldb_table('block_instance_old');
01486 
01488         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
01489         $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01490         $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
01491         $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
01492         $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
01493         $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
01494         $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
01495         $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
01496         $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
01497 
01499         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
01500         $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
01501 
01503         $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
01504         $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
01505 
01507         if (!$dbman->table_exists($table)) {
01508             $dbman->create_table($table);
01509         }
01510 
01512         upgrade_main_savepoint(true, 2009050603);
01513     }
01514 
01515     if ($oldversion < 2009050604) {
01517         $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
01518             SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
01519 
01520         upgrade_main_savepoint(true, 2009050604);
01521     }
01522 
01523     if ($oldversion < 2009050605) {
01524 
01526         $table = new xmldb_table('block');
01527         $field = new xmldb_field('multiple');
01528 
01530         if ($dbman->field_exists($table, $field)) {
01531             $dbman->drop_field($table, $field);
01532         }
01533 
01535         upgrade_main_savepoint(true, 2009050605);
01536     }
01537 
01538     if ($oldversion < 2009050606) {
01539         $table = new xmldb_table('block_instances');
01540 
01542         $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, 'position');
01543         $dbman->rename_field($table, $field, 'defaultweight');
01544 
01546         $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
01547         $dbman->rename_field($table, $field, 'defaultregion');
01548 
01550         upgrade_main_savepoint(true, 2009050606);
01551     }
01552 
01553     if ($oldversion < 2009050607) {
01555         $table = new xmldb_table('block_instances');
01556         $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
01557 
01559         $dbman->change_field_precision($table, $field);
01560 
01562         upgrade_main_savepoint(true, 2009050607);
01563     }
01564 
01565     if ($oldversion < 2009050608) {
01567         $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
01568         $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
01569         $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
01570         // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
01571         // flex page course format. Hopefully this new value is an adequate alternative.
01572 
01574         upgrade_main_savepoint(true, 2009050608);
01575     }
01576 
01577     if ($oldversion < 2009050609) {
01578 
01580         $table = new xmldb_table('block');
01581         $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
01582 
01584         $dbman->add_key($table, $key);
01585 
01587         upgrade_main_savepoint(true, 2009050609);
01588     }
01589 
01590     if ($oldversion < 2009050610) {
01591         $table = new xmldb_table('block_instances');
01592 
01594         $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
01595         if (!$dbman->field_exists($table, $field)) {
01596             $dbman->add_field($table, $field);
01597         }
01598 
01600         $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
01601         if (!$dbman->field_exists($table, $field)) {
01602             $dbman->add_field($table, $field);
01603         }
01604 
01606         $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
01607         if (!$dbman->field_exists($table, $field)) {
01608             $dbman->add_field($table, $field);
01609         }
01610 
01612         $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
01613         if (!$dbman->field_exists($table, $field)) {
01614             $dbman->add_field($table, $field);
01615         }
01616 
01618         upgrade_main_savepoint(true, 2009050610);
01619     }
01620 
01621     if ($oldversion < 2009050611) {
01622         $table = new xmldb_table('block_instances');
01623 
01625         $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
01626 
01628         $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
01629 
01631         upgrade_main_savepoint(true, 2009050611);
01632     }
01633 
01634     if ($oldversion < 2009050612) {
01635 
01637         $table = new xmldb_table('block_instances');
01638         $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
01639 
01641         $dbman->rename_field($table, $field, 'pagetypepattern');
01642 
01644         upgrade_main_savepoint(true, 2009050612);
01645     }
01646 
01647     if ($oldversion < 2009050613) {
01649 
01651         $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
01652         $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
01653                                                    pagetypepattern = 'site-index',
01654                                                    subpagepattern = NULL
01655                       WHERE pagetypepattern = 'site-index'");
01656 
01658         $DB->execute("UPDATE {block_instances} SET
01659                         contextid = (
01660                             SELECT {context}.id
01661                             FROM {context}
01662                             JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
01663                             WHERE {course}.id = pageid
01664                         ),
01665                        pagetypepattern = 'course-view-*',
01666                        subpagepattern = NULL
01667                       WHERE pagetypepattern = 'course-view'");
01668 
01670         $syscontext = get_context_instance(CONTEXT_SYSTEM);
01671         $DB->execute("UPDATE {block_instances} SET
01672                         contextid = " . $syscontext->id . ",
01673                         pagetypepattern = 'admin-*',
01674                         subpagepattern = NULL
01675                       WHERE pagetypepattern = 'admin'");
01676 
01678         $DB->execute("UPDATE {block_instances} SET
01679                         contextid = (
01680                             SELECT {context}.id
01681                             FROM {context}
01682                             JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
01683                             WHERE {user}.id = pageid
01684                         ),
01685                         pagetypepattern = 'my-index',
01686                         subpagepattern = NULL
01687                       WHERE pagetypepattern = 'my-index'");
01688 
01690         $DB->execute("UPDATE {block_instances} SET
01691                         contextid = " . $syscontext->id . ",
01692                         pagetypepattern = 'tag-index',
01693                         subpagepattern = pageid
01694                       WHERE pagetypepattern = 'tag-index'");
01695 
01697         $DB->execute("UPDATE {block_instances} SET
01698                         contextid = (
01699                             SELECT {context}.id
01700                             FROM {context}
01701                             JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
01702                             WHERE {user}.id = pageid
01703                         ),
01704                         pagetypepattern = 'blog-index',
01705                         subpagepattern = NULL
01706                       WHERE pagetypepattern = 'blog-view'");
01707 
01709         $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
01710         foreach ($moduleswithblocks as $modname) {
01711             if (!$dbman->table_exists($modname)) {
01712                 continue;
01713             }
01714             $DB->execute("UPDATE {block_instances} SET
01715                             contextid = (
01716                                 SELECT {context}.id
01717                                 FROM {context}
01718                                 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
01719                                 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
01720                                 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
01721                                 WHERE {{$modname}}.id = pageid
01722                             ),
01723                             pagetypepattern = 'blog-index',
01724                             subpagepattern = NULL
01725                           WHERE pagetypepattern = 'blog-view'");
01726         }
01727 
01729         upgrade_main_savepoint(true, 2009050613);
01730     }
01731 
01732     if ($oldversion < 2009050614) {
01734         $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
01735 
01737         upgrade_main_savepoint(true, 2009050614);
01738     }
01739 
01740     if ($oldversion < 2009050615) {
01741         $table = new xmldb_table('block_instances');
01742 
01745         $DB->delete_records_select('block_instances', 'blockname IS NULL');
01746 
01748         $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
01749         $dbman->change_field_notnull($table, $field);
01750 
01752         $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
01753         $dbman->change_field_notnull($table, $field);
01754 
01756         $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
01757         $dbman->change_field_notnull($table, $field);
01758 
01760         upgrade_main_savepoint(true, 2009050615);
01761     }
01762 
01763     if ($oldversion < 2009050616) {
01765         $blocks = $DB->get_records('block');
01766         $syscontext = get_context_instance(CONTEXT_SYSTEM);
01767         $newregions = array(
01768             'l' => 'side-pre',
01769             'r' => 'side-post',
01770             'c' => 'course-view-top',
01771         );
01772         $stickyblocks = $DB->get_recordset('block_pinned_old');
01773         foreach ($stickyblocks as $stickyblock) {
01774             // Only if the block exists (avoid orphaned sticky blocks)
01775             if (!isset($blocks[$stickyblock->blockid]) || empty($blocks[$stickyblock->blockid]->name)) {
01776                 continue;
01777             }
01778             $newblock = new stdClass();
01779             $newblock->blockname = $blocks[$stickyblock->blockid]->name;
01780             $newblock->contextid = $syscontext->id;
01781             $newblock->showinsubcontexts = 1;
01782             switch ($stickyblock->pagetype) {
01783                 case 'course-view':
01784                     $newblock->pagetypepattern = 'course-view-*';
01785                     break;
01786                 default:
01787                     $newblock->pagetypepattern = $stickyblock->pagetype;
01788             }
01789             $newblock->defaultregion = $newregions[$stickyblock->position];
01790             $newblock->defaultweight = $stickyblock->weight;
01791             $newblock->configdata = $stickyblock->configdata;
01792             $newblock->visible = 1;
01793             $DB->insert_record('block_instances', $newblock);
01794         }
01795 
01797         upgrade_main_savepoint(true, 2009050616);
01798     }
01799 
01800     if ($oldversion < 2009050617) {
01801 
01803         $table = new xmldb_table('block_positions');
01804 
01806         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
01807         $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01808         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
01809         $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
01810         $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
01811         $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
01812         $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
01813         $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
01814 
01816         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
01817         $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
01818         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
01819 
01821         $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
01822 
01824         if (!$dbman->table_exists($table)) {
01825             $dbman->create_table($table);
01826         }
01827 
01829         upgrade_main_savepoint(true, 2009050617);
01830     }
01831 
01832     if ($oldversion < 2009050618) {
01834         $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
01835                 SELECT bi.id, bi.contextid,
01836                        CASE WHEN bi.pagetypepattern = 'course-view-*'
01837                            THEN (SELECT " . $DB->sql_concat("'course-view-'", 'c.format') . "
01838                                    FROM {course} c
01839                                    JOIN {context} ctx ON c.id = ctx.instanceid
01840                                   WHERE ctx.id = bi.contextid)
01841                            ELSE bi.pagetypepattern END,
01842                        CASE WHEN bi.subpagepattern IS NULL
01843                            THEN '" . $DB->sql_empty() . "'
01844                            ELSE bi.subpagepattern END,
01845                        0, bi.defaultregion, bi.defaultweight
01846                   FROM {block_instances} bi
01847                  WHERE bi.visible = 0 AND bi.pagetypepattern <> 'admin-*' AND bi.pagetypepattern IS NOT NULL");
01848         // note: MDL-25031 all block instances should have a pagetype pattern, NULL is not allowed,
01849         //       if we manage to find out how NULLs get there we should fix them before this step
01850 
01852         upgrade_main_savepoint(true, 2009050618);
01853     }
01854 
01855     if ($oldversion < 2009050619) {
01856         $table = new xmldb_table('block_instances');
01857 
01859         $field = new xmldb_field('blockid');
01860         if ($dbman->field_exists($table, $field)) {
01862             $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
01863             if ($dbman->index_exists($table, $index)) {
01865                 $dbman->drop_index($table, $index);
01866             }
01867             $dbman->drop_field($table, $field);
01868         }
01869 
01871         $field = new xmldb_field('pageid');
01872         if ($dbman->field_exists($table, $field)) {
01874             $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
01875             if ($dbman->index_exists($table, $index)) {
01877                 $dbman->drop_index($table, $index);
01878             }
01879             $dbman->drop_field($table, $field);
01880         }
01881 
01883         $field = new xmldb_field('visible');
01884         if ($dbman->field_exists($table, $field)) {
01885             $dbman->drop_field($table, $field);
01886         }
01887 
01889         upgrade_main_savepoint(true, 2009050619);
01890     }
01891 
01892     if ($oldversion < 2009051200) {
01895         echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
01896         upgrade_fix_incorrect_mnethostids();
01897 
01899         upgrade_main_savepoint(true, 2009051200);
01900     }
01901 
01902 
01903     if ($oldversion < 2009051700) {
01905         if (empty($CFG->htmleditor)) {
01906             set_config('texteditors', 'textarea');
01907         } else {
01908             set_config('texteditors', 'tinymce,textarea');
01909         }
01910 
01911         unset_config('htmleditor');
01912         unset_config('defaulthtmleditor');
01913 
01915         upgrade_main_savepoint(true, 2009051700);
01916     }
01917 
01920     if ($oldversion < 2009061600) {
01922         $table = new xmldb_table('block_instances');
01923         $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
01924 
01926         $dbman->change_field_precision($table, $field);
01927 
01929         upgrade_main_savepoint(true, 2009061600);
01930     }
01931 
01932     if ($oldversion < 2009061702) {
01933         // standardizing plugin names
01934         if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
01935             foreach ($configs as $config) {
01936                 unset_config($config->name, $config->plugin); 
01937                 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
01938                 set_config($config->name, $config->value, $config->plugin); 
01939             }
01940         }
01941         unset($configs);
01942         upgrade_main_savepoint(true, 2009061702);
01943     }
01944 
01945     if ($oldversion < 2009061703) {
01946         // standardizing plugin names
01947         if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
01948             foreach ($configs as $config) {
01949                 unset_config($config->name, $config->plugin); 
01950                 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
01951                 set_config($config->name, $config->value, $config->plugin); 
01952             }
01953         }
01954         unset($configs);
01955         upgrade_main_savepoint(true, 2009061703);
01956     }
01957 
01958     if ($oldversion < 2009061704) {
01959         // change component string in capability records to new "_" format
01960         if ($caps = $DB->get_records('capabilities')) {
01961             foreach ($caps as $cap) {
01962                 $cap->component = str_replace('/', '_', $cap->component);
01963                 $DB->update_record('capabilities', $cap);
01964             }
01965         }
01966         unset($caps);
01967         upgrade_main_savepoint(true, 2009061704);
01968     }
01969 
01970     if ($oldversion < 2009063000) {
01971         // upgrade format of _with_advanced settings - quiz only
01972         // note: this can be removed later, not needed for upgrades from 1.9.x
01973         if ($quiz = get_config('quiz')) {
01974             foreach ($quiz as $name=>$value) {
01975                 if (strpos($name, 'fix_') !== 0) {
01976                     continue;
01977                 }
01978                 $newname = substr($name,4).'_adv';
01979                 set_config($newname, $value, 'quiz');
01980                 unset_config($name, 'quiz');
01981             }
01982         }
01983         upgrade_main_savepoint(true, 2009063000);
01984     }
01985 
01986     if ($oldversion < 2009071000) {
01987 
01989         $table = new xmldb_table('block_instances');
01990         $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
01991 
01993         $dbman->rename_field($table, $field, 'parentcontextid');
01994 
01996         upgrade_main_savepoint(true, 2009071000);
01997     }
01998 
01999     if ($oldversion < 2009071600) {
02000 
02002         $table = new xmldb_table('post');
02003         $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
02004 
02006         if (!$dbman->field_exists($table, $field)) {
02007             $dbman->add_field($table, $field);
02008         }
02009 
02011         upgrade_main_savepoint(true, 2009071600);
02012     }
02013 
02014     if ($oldversion < 2009072400) {
02015 
02017         $table = new xmldb_table('comments');
02018 
02020         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
02021         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02022         $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
02023         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02024         $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
02025         $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
02026         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02027         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02028 
02030         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
02031 
02033         if (!$dbman->table_exists($table)) {
02034             $dbman->create_table($table);
02035         }
02036 
02038         upgrade_main_savepoint(true, 2009072400);
02039     }
02040 
02055     if ($oldversion < 2009082800) {
02056 
02057         echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
02058 
02059         // Get the system context so we can set the block instances to it
02060         $syscontext = get_context_instance(CONTEXT_SYSTEM);
02061 
02062         // An array to contain the new block instances we will create
02063         $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
02064         // The new global navigation block instance as a stdClass
02065         $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
02066         $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
02067         $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutely everywhere
02068         $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
02069         $newblockinstances['globalnavigation']->subpagetypepattern = null;
02070         $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
02071         $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
02072         $newblockinstances['globalnavigation']->configdata = '';
02073         // The new settings navigation block instance as a stdClass
02074         $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
02075         $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
02076         $newblockinstances['settingsnavigation']->showinsubcontexts = true;
02077         $newblockinstances['settingsnavigation']->pagetypepattern = '*';
02078         $newblockinstances['settingsnavigation']->subpagetypepattern = null;
02079         $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
02080         $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
02081         $newblockinstances['settingsnavigation']->configdata = '';
02082 
02083         // Blocks that are outmoded and for whom the bells will toll... by which I
02084         // mean we will delete all instances of
02085         $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
02086         $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
02087         unset($outmodedblocks);
02088         // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
02089         // cut down the number of delete queries we will need to run
02090         $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
02091 
02092         $contextids = array();
02093         $instanceids = array();
02094         // Iterate through all block instances
02095         foreach ($allblockinstances as $blockinstance) {
02096             if (!in_array($blockinstance->parentcontextid, $contextids)) {
02097                 $contextids[] = $blockinstance->parentcontextid;
02098 
02099                 // If we have over 1000 contexts clean them up and reset the array
02100                 // this ensures we don't hit any nasty memory limits or such
02101                 if (count($contextids) > 1000) {
02102                     upgrade_cleanup_unwanted_block_contexts($contextids);
02103                     $contextids = array();
02104                 }
02105             }
02106             if (!in_array($blockinstance->id, $instanceids)) {
02107                 $instanceids[] = $blockinstance->id;
02108                 // If we have more than 1000 block instances now remove all block positions
02109                 // and empty the array
02110                 if (count($instanceids) > 1000) {
02111                     $instanceidstring = join(',',$instanceids);
02112                     $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
02113                     $instanceids = array();
02114                 }
02115             }
02116         }
02117 
02118         upgrade_cleanup_unwanted_block_contexts($contextids);
02119 
02120         if ($instanceids) {
02121             $instanceidstring = join(',',$instanceids);
02122             $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
02123         }
02124 
02125         unset($allblockinstances);
02126         unset($contextids);
02127         unset($instanceids);
02128         unset($instanceidstring);
02129 
02130         // Now remove the actual block instance
02131         $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
02132         unset($outmodedblocksstring);
02133 
02134         // Insert the new block instances. Remember they have not been installed yet
02135         // however this should not be a problem
02136         foreach ($newblockinstances as $blockinstance) {
02137             $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
02138             // Ensure the block context is created.
02139             get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
02140         }
02141         unset($newblockinstances);
02142 
02143         upgrade_main_savepoint(true, 2009082800);
02144         // The end of the navigation upgrade
02145     }
02146 
02147     if ($oldversion < 2009100602) {
02149         $table = new xmldb_table('external_functions');
02150 
02152         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
02153         $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
02154         $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
02155         $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
02156         $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
02157         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
02158 
02160         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
02161 
02163         $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
02164 
02166         $dbman->create_table($table);
02167 
02169         upgrade_main_savepoint(true, 2009100602);
02170     }
02171 
02172     if ($oldversion < 2009100603) {
02174         $table = new xmldb_table('external_services');
02175 
02177         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
02178         $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
02179         $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02180         $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
02181         $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02182         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
02183         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02184         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
02185 
02187         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
02188 
02190         $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
02191 
02193         $dbman->create_table($table);
02194 
02196         upgrade_main_savepoint(true, 2009100603);
02197     }
02198 
02199     if ($oldversion < 2009100604) {
02201         $table = new xmldb_table('external_services_functions');
02202 
02204         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
02205         $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02206         $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
02207 
02209         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
02210         $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
02211 
02213         $dbman->create_table($table);
02214 
02216         upgrade_main_savepoint(true, 2009100604);
02217     }
02218 
02219     if ($oldversion < 2009100605) {
02221         $table = new xmldb_table('external_services_users');
02222 
02224         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
02225         $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02226         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02227         $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
02228         $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
02229         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
02230 
02232         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
02233         $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
02234         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
02235 
02237         $dbman->create_table($table);
02238 
02240         upgrade_main_savepoint(true, 2009100605);
02241     }
02242 
02243     if ($oldversion < 2009102600) {
02244 
02246         $table = new xmldb_table('external_tokens');
02247 
02249         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
02250         $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
02251         $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02252         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02253         $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02254         $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
02255         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02256         $table->add_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
02257         $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
02258         $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
02259         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02260         $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
02261 
02263         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
02264         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
02265         $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
02266         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
02267         $table->add_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
02268 
02270         $dbman->create_table($table);
02271 
02273         upgrade_main_savepoint(true, 2009102600);
02274     }
02275 
02276    if ($oldversion < 2009103000) {
02277 
02279         $table = new xmldb_table('blog_association');
02280 
02282         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
02283         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02284         $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02285 
02287         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
02288         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
02289         $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
02290 
02292         if (!$dbman->table_exists($table)) {
02293             $dbman->create_table($table);
02294         }
02295 
02297         $table = new xmldb_table('blog_external');
02298 
02300         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
02301         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02302         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
02303         $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
02304         $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
02305         $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
02306         $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
02307         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
02308         $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
02309 
02311         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
02312         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
02313 
02315         if ($dbman->table_exists($table)) {
02316             // Delete the existing one first (comes from early dev version)
02317             $dbman->drop_table($table);
02318         }
02319         $dbman->create_table($table);
02320 
02321        // upgrade notice is now in admin/tool/bloglevelupgrade/
02322 
02324         upgrade_main_savepoint(true, 2009103000);
02325     }
02326 
02327     if ($oldversion < 2009110400) {
02328         // list of tables where we need to add new format field and convert texts
02329         $extendtables = array('course'              => 'summary',
02330                               'course_categories'   => 'description',
02331                               'course_categories'   => 'description',
02332                               'course_request'      => 'summary',
02333                               'grade_outcomes'      => 'description',
02334                               'groups'              => 'description',
02335                               'groupings'           => 'description',
02336                               'scale'               => 'description',
02337                               'user_info_field'     => 'description',
02338                               'user_info_field'     => 'defaultdata',
02339                               'user_info_data'      => 'data');
02340 
02341         foreach ($extendtables as $tablestr => $fieldstr) {
02342             $formatfieldstr = $fieldstr.'format';
02343 
02344             $table = new xmldb_table($tablestr);
02345             $field = new xmldb_field($formatfieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', $fieldstr);
02346             // Check that the field doesn't already exists
02347             if (!$dbman->field_exists($table, $field)) {
02348                 // Add the new field
02349                 $dbman->add_field($table, $field);
02350             }
02351             if ($CFG->texteditors !== 'textarea') {
02352                 $rs = $DB->get_recordset($tablestr, array($formatfieldstr => FORMAT_MOODLE), '', "id,$fieldstr,$formatfieldstr");
02353                 foreach ($rs as $rec) {
02354                     $rec->$fieldstr       = text_to_html($rec->$fieldstr, false, false, true);
02355                     $rec->$formatfieldstr = FORMAT_HTML;
02356                     $DB->update_record($tablestr, $rec);
02357                     upgrade_set_timeout();
02358                 }
02359                 $rs->close();
02360                 unset($rs);
02361             }
02362         }
02363 
02364         unset($rec);
02365         unset($extendtables);
02366 
02367         upgrade_main_savepoint(true, 2009110400);
02368     }
02369 
02370     if ($oldversion < 2009110401) {
02371         $table = new xmldb_table('user');
02372 
02373         // Change the precision of the description field first up.
02374         // This may grow!
02375         $field = new xmldb_field('description', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'url');
02376         $dbman->change_field_precision($table, $field);
02377 
02378         $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'description');
02379         // Check that the field doesn't already exists
02380         if (!$dbman->field_exists($table, $field)) {
02381             // Add the new field
02382             $dbman->add_field($table, $field);
02383         }
02384         if ($CFG->texteditors !== 'textarea') {
02385             $rs = $DB->get_recordset('user', array('descriptionformat'=>FORMAT_MOODLE, 'deleted'=>0, 'htmleditor'=>1), '', "id,description,descriptionformat");
02386             foreach ($rs as $rec) {
02387                 $rec->description       = text_to_html($rec->description, false, false, true);
02388                 $rec->descriptionformat = FORMAT_HTML;
02389                 $DB->update_record('user', $rec);
02390                 upgrade_set_timeout();
02391             }
02392             $rs->close();
02393         }
02394 
02395         upgrade_main_savepoint(true, 2009110401);
02396     }
02397 
02398     if ($oldversion < 2009112400) {
02399         if (empty($CFG->passwordsaltmain)) {
02400             $subject = get_string('check_passwordsaltmain_name', 'report_security');
02401             $description = get_string('check_passwordsaltmain_warning', 'report_security');;
02402             upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
02403         }
02404         upgrade_main_savepoint(true, 2009112400);
02405     }
02406 
02407     if ($oldversion < 2010011200) {
02408         $table = new xmldb_table('grade_categories');
02409         $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timemodified');
02410 
02411         if (!$dbman->field_exists($table, $field)) {
02412             $dbman->add_field($table, $field);
02413         }
02414 
02415         upgrade_main_savepoint(true, 2010011200);
02416     }
02417 
02418     if ($oldversion < 2010012500) {
02419         upgrade_fix_incorrect_mnethostids();
02420         upgrade_main_savepoint(true, 2010012500);
02421     }
02422 
02423     if ($oldversion < 2010012600) {
02424         // do stuff to the mnet table
02425         $table = new xmldb_table('mnet_rpc');
02426 
02427         $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
02428         $dbman->rename_field($table, $field, 'plugintype');
02429 
02430         $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
02431         $dbman->rename_field($table, $field, 'pluginname');
02432 
02433         $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
02434         if (!$dbman->field_exists($table, $field)) {
02435             $dbman->add_field($table, $field);
02436         }
02437 
02438         $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
02439         if (!$dbman->field_exists($table, $field)) {
02440             $dbman->add_field($table, $field);
02441         }
02442 
02443         $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
02444         if (!$dbman->field_exists($table, $field)) {
02445             $dbman->add_field($table, $field);
02446         }
02447 
02449         upgrade_main_savepoint(true, 2010012600);
02450     }
02451 
02452     if ($oldversion < 2010012900) {
02453 
02455         $table = new xmldb_table('mnet_remote_rpc');
02456 
02458         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
02459         $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
02460         $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
02461 
02463         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
02464 
02466         if (!$dbman->table_exists($table)) {
02467             $dbman->create_table($table);
02468         }
02469 
02470 
02472         $table = new xmldb_table('mnet_remote_service2rpc');
02473 
02475         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
02476         $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
02477         $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
02478 
02480         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
02481 
02483         $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
02484 
02486         if (!$dbman->table_exists($table)) {
02487             $dbman->create_table($table);
02488         }
02489 
02490 
02492         $table = new xmldb_table('mnet_rpc');
02493         $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
02494 
02496         $dbman->rename_field($table, $field, 'functionname');
02497 
02498 
02500         $table = new xmldb_table('mnet_rpc');
02501         $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
02502 
02504         $dbman->rename_field($table, $field, 'xmlrpcpath');
02505 
02506 
02508         upgrade_main_savepoint(true, 2010012900);
02509     }
02510 
02511     if ($oldversion < 2010012901) {
02512 
02514         $table = new xmldb_table('mnet_remote_rpc');
02515         $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
02516 
02518         if (!$dbman->field_exists($table, $field)) {
02519             $dbman->add_field($table, $field);
02520         }
02521 
02523         $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
02524 
02526         if (!$dbman->field_exists($table, $field)) {
02527             $dbman->add_field($table, $field);
02528         }
02529 
02531         upgrade_main_savepoint(true, 2010012901);
02532     }
02533 
02534     if ($oldversion < 2010012902) {
02535 
02537         $table = new xmldb_table('mnet_remote_rpc');
02538         $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
02539 
02541         if (!$dbman->field_exists($table, $field)) {
02542             $dbman->add_field($table, $field);
02543         }
02544 
02546         upgrade_main_savepoint(true, 2010012902);
02547     }
02548 
02550     if ($oldversion < 2010020100) {
02552         $table = new xmldb_table('mnet_host');
02553         $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
02554 
02556         $dbman->change_field_precision($table, $field);
02557 
02558         upgrade_main_savepoint(true, 2010020100);
02559     }
02560 
02561     if ($oldversion < 2010020300) {
02562 
02564         $table = new xmldb_table('user');
02565         $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
02566 
02567         if (!$dbman->field_exists($table, $field)) {
02569             $dbman->add_field($table, $field);
02570 
02571             $DB->execute("UPDATE {user} SET timecreated = firstaccess");
02572 
02573             $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
02574             $DB->execute($sql);
02575         }
02576         upgrade_main_savepoint(true, 2010020300);
02577     }
02578 
02579     // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
02580     if ($oldversion < 2010020301) {
02581         if ($preamble = $CFG->filter_tex_latexpreamble) {
02582             $preamble = preg_replace('/^ +/m', '', $preamble);
02583             set_config('filter_tex_latexpreamble', $preamble);
02584         }
02585         upgrade_main_savepoint(true, 2010020301);
02586     }
02587 
02588     if ($oldversion < 2010021400) {
02590         require_once($CFG->dirroot . '/course/lib.php');
02591         rebuild_course_cache(0, true);
02592         upgrade_main_savepoint(true, 2010021400);
02593     }
02594 
02595     if ($oldversion < 2010021800) {
02596         $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
02597         upgrade_main_savepoint(true, 2010021800);
02598     }
02599 
02600     if ($oldversion < 2010031900) {
02601         // regeneration of sessions is always enabled, no need for this setting any more
02602         unset_config('regenloginsession');
02603         upgrade_main_savepoint(true, 2010031900);
02604     }
02605 
02606     if ($oldversion < 2010033101.02) {
02607 
02609         $table = new xmldb_table('license');
02610 
02612         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
02613         $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
02614         $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
02615         $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
02616         $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
02617         $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
02618 
02620         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
02621 
02623         if (!$dbman->table_exists($table)) {
02624             $dbman->create_table($table);
02625         }
02626         $active_licenses = array();
02627 
02628         $license = new stdClass();
02629 
02630         // add unknown license
02631         $license->shortname = 'unknown';
02632         $license->fullname = 'Unknown license';
02633         $license->source = '';
02634         $license->enabled = 1;
02635         $license->version = '2010033100';
02636         $active_licenses[] = $license->shortname;
02637         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
02638             if ($record->version < $license->version) {
02639                 // update license record
02640                 $license->enabled = $record->enabled;
02641                 $license->id = $record->id;
02642                 $DB->update_record('license', $license);
02643             }
02644         } else {
02645             $DB->insert_record('license', $license);
02646         }
02647 
02648         // add all rights reserved license
02649         $license->shortname = 'allrightsreserved';
02650         $license->fullname = 'All rights reserved';
02651         $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
02652         $license->enabled = 1;
02653         $license->version = '2010033100';
02654         $active_licenses[] = $license->shortname;
02655         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
02656             if ($record->version < $license->version) {
02657                 // update license record
02658                 $license->id = $record->id;
02659                 $license->enabled = $record->enabled;
02660                 $DB->update_record('license', $license);
02661             }
02662         } else {
02663             $DB->insert_record('license', $license);
02664         }
02665 
02666         // add public domain license
02667         $license->shortname = 'public';
02668         $license->fullname = 'Public Domain';
02669         $license->source = 'http://creativecommons.org/licenses/publicdomain/';
02670         $license->enabled = 1;
02671         $license->version = '2010033100';
02672         $active_licenses[] = $license->shortname;
02673         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
02674             if ($record->version < $license->version) {
02675                 // update license record
02676                 $license->enabled = $record->enabled;
02677                 $license->id = $record->id;
02678                 $DB->update_record('license', $license);
02679             }
02680         } else {
02681             $DB->insert_record('license', $license);
02682         }
02683 
02684         // add creative commons license
02685         $license->shortname = 'cc';
02686         $license->fullname = 'Creative Commons';
02687         $license->source = 'http://creativecommons.org/licenses/by/3.0/';
02688         $license->enabled = 1;
02689         $license->version = '2010033100';
02690         $active_licenses[] = $license->shortname;
02691         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
02692             if ($record->version < $license->version) {
02693                 // update license record
02694                 $license->enabled = $record->enabled;
02695                 $license->id = $record->id;
02696                 $DB->update_record('license', $license);
02697             }
02698         } else {
02699             $DB->insert_record('license', $license);
02700         }
02701 
02702         // add creative commons no derivs license
02703         $license->shortname = 'cc-nd';
02704         $license->fullname = 'Creative Commons - NoDerivs';
02705         $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
02706         $license->enabled = 1;
02707         $license->version = '2010033100';
02708         $active_licenses[] = $license->shortname;
02709         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
02710             if ($record->version < $license->version) {
02711                 // update license record
02712                 $license->enabled = $record->enabled;
02713                 $license->id = $record->id;
02714                 $DB->update_record('license', $license);
02715             }
02716         } else {
02717             $DB->insert_record('license', $license);
02718         }
02719 
02720         // add creative commons no commercial no derivs license
02721         $license->shortname = 'cc-nc-nd';
02722         $license->fullname = 'Creative Commons - No Commercial NoDerivs';
02723         $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
02724         $license->enabled = 1;
02725         $license->version = '2010033100';
02726         $active_licenses[] = $license->shortname;
02727         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
02728             if ($record->version < $license->version) {
02729                 // update license record
02730                 $license->enabled = $record->enabled;
02731                 $license->id = $record->id;
02732                 $DB->update_record('license', $license);
02733             }
02734         } else {
02735             $DB->insert_record('license', $license);
02736         }
02737 
02738         // add creative commons no commercial
02739         $license->shortname = 'cc-nc-nd';
02740         $license->shortname = 'cc-nc';
02741         $license->fullname = 'Creative Commons - No Commercial';
02742         $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
02743         $license->enabled = 1;
02744         $license->version = '2010033100';
02745         $active_licenses[] = $license->shortname;
02746         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
02747             if ($record->version < $license->version) {
02748                 // update license record
02749                 $license->enabled = $record->enabled;
02750                 $license->id = $record->id;
02751                 $DB->update_record('license', $license);
02752             }
02753         } else {
02754             $DB->insert_record('license', $license);
02755         }
02756 
02757         // add creative commons no commercial sharealike
02758         $license->shortname = 'cc-nc-sa';
02759         $license->fullname = 'Creative Commons - No Commercial ShareAlike';
02760         $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
02761         $license->enabled = 1;
02762         $license->version = '2010033100';
02763         $active_licenses[] = $license->shortname;
02764         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
02765             if ($record->version < $license->version) {
02766                 // update license record
02767                 $license->enabled = $record->enabled;
02768                 $license->id = $record->id;
02769                 $DB->update_record('license', $license);
02770             }
02771         } else {
02772             $DB->insert_record('license', $license);
02773         }
02774 
02775         // add creative commons sharealike
02776         $license->shortname = 'cc-sa';
02777         $license->fullname = 'Creative Commons - ShareAlike';
02778         $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
02779         $license->enabled = 1;
02780         $license->version = '2010033100';
02781         $active_licenses[] = $license->shortname;
02782         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
02783             if ($record->version < $license->version) {
02784                 // update license record
02785                 $license->enabled = $record->enabled;
02786                 $license->id = $record->id;
02787                 $DB->update_record('license', $license);
02788             }
02789         } else {
02790             $DB->insert_record('license', $license);
02791         }
02792 
02793         set_config('licenses', implode(',', $active_licenses));
02795         set_config('sitedefaultlicense', 'allrightsreserved');
02796 
02798         upgrade_main_savepoint(true, 2010033101.02);
02799     }
02800 
02801     if ($oldversion < 2010033102.00) {
02802         // rename course view capability to participate
02803         $params = array('viewcap'=>'moodle/course:view', 'participatecap'=>'moodle/course:participate');
02804         $sql = "UPDATE {role_capabilities} SET capability = :participatecap WHERE capability = :viewcap";
02805         $DB->execute($sql, $params);
02806         $sql = "UPDATE {capabilities} SET name = :participatecap WHERE name = :viewcap";
02807         $DB->execute($sql, $params);
02808         // note: the view capability is readded again at the end of upgrade, but with different meaning
02809         upgrade_main_savepoint(true, 2010033102.00);
02810     }
02811 
02812     if ($oldversion < 2010033102.01) {
02813         // Define field archetype to be added to role table
02814         $table = new xmldb_table('role');
02815         $field = new xmldb_field('archetype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'sortorder');
02816         $dbman->add_field($table, $field);
02817         upgrade_main_savepoint(true, 2010033102.01);
02818     }
02819 
02820     if ($oldversion < 2010033102.02) {
02821         // Set archetype for existing roles and change admin role to manager role
02822         $sql = "SELECT r.*, rc.capability
02823                   FROM {role} r
02824                   JOIN {role_capabilities} rc ON rc.roleid = r.id
02825                  WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
02826               ORDER BY r.id";
02827         $params = array('syscontextid'=>SYSCONTEXTID, 'legacycaps'=>'moodle/legacy:%');
02828         $substart = strlen('moodle/legacy:');
02829         $roles = $DB->get_recordset_sql($sql, $params); // in theory could be multiple legacy flags in one role
02830         foreach ($roles as $role) {
02831             $role->archetype = substr($role->capability, $substart);
02832             unset($role->capability);
02833             if ($role->archetype === 'admin') {
02834                 $i = '';
02835                 if ($DB->record_exists('role', array('shortname'=>'manager')) or $DB->record_exists('role', array('name'=>get_string('manager', 'role')))) {
02836                     $i = 2;
02837                     while($DB->record_exists('role', array('shortname'=>'manager'.$i)) or $DB->record_exists('role', array('name'=>get_string('manager', 'role').$i))) {
02838                         $i++;
02839                     }
02840                 }
02841                 $role->archetype = 'manager';
02842                 if ($role->shortname === 'admin') {
02843                     $role->shortname   = 'manager'.$i;
02844                     $role->name        = get_string('manager', 'role').$i;
02845                     $role->description = get_string('managerdescription', 'role');
02846                 }
02847             }
02848             $DB->update_record('role', $role);
02849         }
02850         $roles->close();
02851 
02852         upgrade_main_savepoint(true, 2010033102.02);
02853     }
02854 
02855     if ($oldversion < 2010033102.03) {
02856         // Now pick site admins (===have manager role assigned at the system context)
02857         // and store them in the new $CFG->siteadmins setting as comma separated list
02858         $sql = "SELECT ra.id, ra.userid
02859                   FROM {role_assignments} ra
02860                   JOIN {role} r ON r.id = ra.roleid
02861                   JOIN {user} u ON u.id = ra.userid
02862                  WHERE ra.contextid = :syscontext AND r.archetype = 'manager' AND u.deleted = 0
02863               ORDER BY ra.id";
02864         $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
02865         $admins = array();
02866         foreach ($ras as $ra) {
02867             $admins[$ra->userid] = $ra->userid;
02868             set_config('siteadmins', implode(',', $admins)); // better to save it repeatedly, we do need at least one admin
02869             $DB->delete_records('role_assignments', array('id'=>$ra->id));
02870         }
02871 
02872         upgrade_main_savepoint(true, 2010033102.03);
02873     }
02874 
02875     if ($oldversion < 2010033102.04) {
02876         // clean up the manager roles
02877         $managers = $DB->get_records('role', array('archetype'=>'manager'));
02878         foreach ($managers as $manager) {
02879             // now sanitize the capabilities and overrides
02880             $DB->delete_records('role_capabilities', array('capability'=>'moodle/site:config', 'roleid'=>$manager->id)); // only site admins may configure servers
02881             // note: doanything and legacy caps are deleted automatically, they get moodle/course:view later at the end of the upgrade
02882 
02883             // remove manager role assignments below the course context level - admin role was never intended for activities and blocks,
02884             // the problem is that those assignments would not be visible after upgrade and old style admins in activities make no sense anyway
02885             $DB->delete_records_select('role_assignments', "roleid = :manager AND contextid IN (SELECT id FROM {context} WHERE contextlevel > 50)", array('manager'=>$manager->id));
02886 
02887             // allow them to assign all roles except default user, guest and frontpage - users get these roles automatically on the fly when needed
02888             $DB->delete_records('role_allow_assign', array('roleid'=>$manager->id));
02889             $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype <> 'user' AND archetype <> 'guest' AND archetype <> 'frontpage'");
02890             foreach ($roles as $role) {
02891                 $record = (object)array('roleid'=>$manager->id, 'allowassign'=>$role->id);
02892                 $DB->insert_record('role_allow_assign', $record);
02893             }
02894 
02895             // allow them to override all roles
02896             $DB->delete_records('role_allow_override', array('roleid'=>$manager->id));
02897             $roles = $DB->get_records_sql("SELECT * FROM {role}");
02898             foreach ($roles as $role) {
02899                 $record = (object)array('roleid'=>$manager->id, 'allowoverride'=>$role->id);
02900                 $DB->insert_record('role_allow_override', $record);
02901             }
02902 
02903             // allow them to switch to all following roles
02904             $DB->delete_records('role_allow_switch', array('roleid'=>$manager->id));
02905             $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype IN ('student', 'teacher', 'editingteacher')");
02906             foreach ($roles as $role) {
02907                 $record = (object)array('roleid'=>$manager->id, 'allowswitch'=>$role->id);
02908                 $DB->insert_record('role_allow_switch', $record);
02909             }
02910         }
02911 
02912         upgrade_main_savepoint(true, 2010033102.04);
02913     }
02914 
02915     if ($oldversion < 2010033102.05) {
02916         // remove course:view from all roles that are not used for enrolment, it does NOT belong there because it really means user is enrolled!
02917         $noenrolroles = $DB->get_records_select('role', "archetype IN ('guest', 'user', 'manager', 'coursecreator', 'frontpage')");
02918         foreach ($noenrolroles as $role) {
02919             $DB->delete_records('role_capabilities', array('roleid'=>$role->id, 'capability'=>'moodle/course:participate'));
02920         }
02921         upgrade_main_savepoint(true, 2010033102.05);
02922     }
02923 
02924     if ($oldversion < 2010033102.06) {
02925         // make sure there is nothing weird in default user role
02926         if (!empty($CFG->defaultuserroleid)) {
02927             if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
02928                 if ($role->archetype !== '' and $role->archetype !== 'user') {
02929                     upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default authenticated user role (defaultuserroleid) value is invalid, setting cleared.');
02930                     unset_config('defaultuserroleid');
02931                 }
02932             } else {
02933                 unset_config('defaultuserroleid');
02934             }
02935         }
02936         upgrade_main_savepoint(true, 2010033102.06);
02937     }
02938 
02939     if ($oldversion < 2010033102.07) {
02940         if (!empty($CFG->displayloginfailures) and $CFG->displayloginfailures === 'teacher') {
02941             upgrade_log(UPGRADE_LOG_NOTICE, null, 'Displaying of login failuters to teachers is not supported any more.');
02942             unset_config('displayloginfailures');
02943         }
02944         upgrade_main_savepoint(true, 2010033102.07);
02945     }
02946 
02947     if ($oldversion < 2010033102.08) {
02948         // make sure there are no problems in default guest role settings
02949         if (!empty($CFG->guestroleid)) {
02950             if ($role = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
02951                 if ($role->archetype !== '' and $role->archetype !== 'guest') {
02952                     upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default guest role (guestroleid) value is invalid, setting cleared.');
02953                     unset_config('guestroleid');
02954                 }
02955             } else {
02956                 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) does not exist, setting cleared.');
02957                 unset_config('guestroleid');
02958             }
02959         }
02960         // remove all roles of the guest account - the only way to change it is to override the guest role, sorry
02961         // the guest account gets all the role assignments on the fly which works fine in has_capability(),
02962         $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE username = 'guest')");
02963 
02964         upgrade_main_savepoint(true, 2010033102.08);
02965     }
02966 
02968     if ($oldversion < 2010033102.09) {
02969 
02971         $table = new xmldb_table('role_context_levels');
02972 
02974         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
02975         $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02976         $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
02977 
02979         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
02980         $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
02981         $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
02982 
02984         if (!$dbman->table_exists($table)) {
02985             $dbman->create_table($table);
02986         }
02987 
02989         upgrade_main_savepoint(true, 2010033102.09);
02990     }
02991 
02992     if ($oldversion < 2010033102.10) {
02993         // Now populate the role_context_levels table with the default values
02994         // NOTE: do not use accesslib methods here
02995 
02996         $rolecontextlevels = array();
02997         $defaults = array('manager'        => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE),
02998                           'coursecreator'  => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT),
02999                           'editingteacher' => array(CONTEXT_COURSE, CONTEXT_MODULE),
03000                           'teacher'        => array(CONTEXT_COURSE, CONTEXT_MODULE),
03001                           'student'        => array(CONTEXT_COURSE, CONTEXT_MODULE),
03002                           'guest'          => array(),
03003                           'user'           => array(),
03004                           'frontpage'      => array());
03005 
03006         $roles = $DB->get_records('role', array(), '', 'id, archetype');
03007         foreach ($roles as $role) {
03008             if (isset($defaults[$role->archetype])) {
03009                 $rolecontextlevels[$role->id] = $defaults[$role->archetype];
03010             }
03011         }
03012 
03013         // add roles without archetypes, it may contain weird things, but we can not fix them
03014         list($narsql, $params) = $DB->get_in_or_equal(array_keys($defaults), SQL_PARAMS_NAMED, 'ar', false);
03015         $sql = "SELECT DISTINCT ra.roleid, con.contextlevel
03016                   FROM {role_assignments} ra
03017                   JOIN {context} con ON ra.contextid = con.id
03018                   JOIN {role} r ON r.id = ra.roleid
03019                  WHERE r.archetype $narsql";
03020         $existingrolecontextlevels = $DB->get_recordset_sql($sql, $params);
03021         foreach ($existingrolecontextlevels as $rcl) {
03022             if (!isset($rolecontextlevels[$rcl->roleid])) {
03023                 $rolecontextlevels[$rcl->roleid] = array();
03024             }
03025             $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
03026         }
03027         $existingrolecontextlevels->close();
03028 
03029         // Put the data into the database.
03030         $rcl = new stdClass();
03031         foreach ($rolecontextlevels as $roleid => $contextlevels) {
03032             $rcl->roleid = $roleid;
03033             foreach ($contextlevels as $level) {
03034                 $rcl->contextlevel = $level;
03035                 $DB->insert_record('role_context_levels', $rcl, false);
03036             }
03037         }
03038 
03039         // release memory!!
03040         unset($roles);
03041         unset($defaults);
03042         unset($rcl);
03043         unset($existingrolecontextlevels);
03044         unset($rolecontextlevels);
03045 
03046         // Main savepoint reached
03047         upgrade_main_savepoint(true, 2010033102.10);
03048     }
03049 
03050     if ($oldversion < 2010040700) {
03051         // migrate old groupings --> groupmembersonly setting
03052         if (isset($CFG->enablegroupings)) {
03053             set_config('enablegroupmembersonly', $CFG->enablegroupings);
03054             unset_config('enablegroupings');
03055         }
03056 
03057         // Main savepoint reached
03058         upgrade_main_savepoint(true, 2010040700);
03059     }
03060 
03061     if ($oldversion < 2010040900) {
03062 
03063         // Changing the default of field lang on table user to good old "en"
03064         $table = new xmldb_table('user');
03065         $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en', 'country');
03066 
03067         // Launch change of default for field lang
03068         $dbman->change_field_default($table, $field);
03069 
03070         // update main site lang
03071         if (strpos($CFG->lang, '_utf8') !== false) {
03072             $lang = str_replace('_utf8', '', $CFG->lang);
03073             set_config('lang', $lang);
03074         }
03075 
03076         // tweak langlist
03077         if (!empty($CFG->langlist)) {
03078             $langs = explode(',', $CFG->langlist);
03079             foreach ($langs as $key=>$lang) {
03080                 $lang = str_replace('_utf8', '', $lang);
03081                 $langs[$key] = $lang;
03082             }
03083             set_config('langlist', implode(',', $langs));
03084         }
03085 
03086         // Main savepoint reached
03087         upgrade_main_savepoint(true, 2010040900);
03088     }
03089 
03090     if ($oldversion < 2010040901) {
03091 
03092         // Remove "_utf8" suffix from all langs in user table
03093         $langs = $DB->get_records_sql("SELECT DISTINCT lang FROM {user} WHERE lang LIKE ?", array('%_utf8'));
03094 
03095         foreach ($langs as $lang=>$unused) {
03096             $newlang = str_replace('_utf8', '', $lang);
03097             $sql = "UPDATE {user} SET lang = :newlang WHERE lang = :lang";
03098             $DB->execute($sql, array('newlang'=>$newlang, 'lang'=>$lang));
03099         }
03100 
03101         // Main savepoint reached
03102         upgrade_main_savepoint(true, 2010040901);
03103     }
03104 
03105     if ($oldversion < 2010041301) {
03106         $sql = "UPDATE {block} SET name=? WHERE name=?";
03107         $DB->execute($sql, array('navigation', 'global_navigation_tree'));
03108         $DB->execute($sql, array('settings', 'settings_navigation_tree'));
03109 
03110         $sql = "UPDATE {block_instances} SET blockname=? WHERE blockname=?";
03111         $DB->execute($sql, array('navigation', 'global_navigation_tree'));
03112         $DB->execute($sql, array('settings', 'settings_navigation_tree'));
03113         upgrade_main_savepoint(true, 2010041301);
03114     }
03115 
03116     if ($oldversion < 2010042100) {
03117 
03119         $table = new xmldb_table('backup_controllers');
03120 
03122         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03123         $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
03124         $table->add_field('type', XMLDB_TYPE_CHAR, '6', null, XMLDB_NOTNULL, null, null);
03125         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03126         $table->add_field('format', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
03127         $table->add_field('interactive', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03128         $table->add_field('purpose', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03129         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03130         $table->add_field('status', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03131         $table->add_field('execution', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03132         $table->add_field('executiontime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03133         $table->add_field('checksum', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
03134         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03135         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03136         $table->add_field('controller', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
03137 
03139         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03140         $table->add_key('backupid_uk', XMLDB_KEY_UNIQUE, array('backupid'));
03141         $table->add_key('userid_fk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
03142 
03144         $table->add_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
03145 
03147         if (!$dbman->table_exists($table)) {
03148             $dbman->create_table($table);
03149         }
03150 
03152         $table = new xmldb_table('backup_ids_template');
03153 
03155         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03156         $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
03157         $table->add_field('itemname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
03158         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03159         $table->add_field('parentitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
03160 
03162         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03163         $table->add_key('backupid_itemname_itemid_uk', XMLDB_KEY_UNIQUE, array('backupid', 'itemname', 'itemid'));
03164 
03166         $table->add_index('backupid_parentitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'parentitemid'));
03167 
03169         if (!$dbman->table_exists($table)) {
03170             $dbman->create_table($table);
03171         }
03172 
03174         upgrade_main_savepoint(true, 2010042100);
03175     }
03176 
03177     if ($oldversion < 2010042301) {
03178 
03179         $table = new xmldb_table('course_sections');
03180         $field = new xmldb_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'section');
03181 
03182         if (!$dbman->field_exists($table, $field)) {
03183             $dbman->add_field($table, $field);
03184         }
03185 
03186         upgrade_main_savepoint(true, 2010042301);
03187     }
03188 
03189     if ($oldversion < 2010042302) {
03190         // Define table cohort to be created
03191         $table = new xmldb_table('cohort');
03192 
03193         // Adding fields to table cohort
03194         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03195         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03196         $table->add_field('name', XMLDB_TYPE_CHAR, '254', null, XMLDB_NOTNULL, null, null);
03197         $table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
03198         $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
03199         $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03200         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
03201         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03202         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03203 
03204         // Adding keys to table cohort
03205         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03206         $table->add_key('context', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
03207 
03208         // Conditionally launch create table for cohort
03209         if (!$dbman->table_exists($table)) {
03210             $dbman->create_table($table);
03211         }
03212 
03213         upgrade_main_savepoint(true, 2010042302);
03214     }
03215 
03216     if ($oldversion < 2010042303) {
03217         // Define table cohort_members to be created
03218         $table = new xmldb_table('cohort_members');
03219 
03220         // Adding fields to table cohort_members
03221         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03222         $table->add_field('cohortid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03223         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03224         $table->add_field('timeadded', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03225 
03226         // Adding keys to table cohort_members
03227         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03228         $table->add_key('cohortid', XMLDB_KEY_FOREIGN, array('cohortid'), 'cohort', array('id'));
03229         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
03230 
03231         // Adding indexes to table cohort_members
03232         $table->add_index('cohortid-userid', XMLDB_INDEX_UNIQUE, array('cohortid', 'userid'));
03233 
03234         // Conditionally launch create table for cohort_members
03235         if (!$dbman->table_exists($table)) {
03236             $dbman->create_table($table);
03237         }
03238 
03239         // Main savepoint reached
03240         upgrade_main_savepoint(true, 2010042303);
03241     }
03242 
03243     if ($oldversion < 2010042800) {
03244         //drop the previously created ratings table
03245         $table = new xmldb_table('ratings');
03246         if ($dbman->table_exists($table)) {
03247             $dbman->drop_table($table);
03248         }
03249 
03250         //create the rating table (replaces module specific rating implementations)
03251         $table = new xmldb_table('rating');
03252         if ($dbman->table_exists($table)) {
03253             $dbman->drop_table($table);
03254         }
03255 
03257         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03258         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03259 
03260         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03261         $table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
03262         $table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03263         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03264 
03265         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03266         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03267 
03269         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03270         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
03271         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
03272 
03274         $table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
03275 
03277         if (!$dbman->table_exists($table)) {
03278             $dbman->create_table($table);
03279         }
03280 
03281         upgrade_main_savepoint(true, 2010042800);
03282     }
03283 
03284     if ($oldversion < 2010042801) {
03285         // migrating old comments block content
03286         $DB->execute("UPDATE {comments}
03287                          SET contextid = (SELECT parentcontextid
03288                                             FROM {block_instances}
03289                                            WHERE id = {comments}.itemid AND blockname = 'comments'),
03290                              commentarea = 'page_comments',
03291                              itemid = 0
03292                        WHERE commentarea = 'block_comments'
03293                              AND itemid != 0
03294                              AND EXISTS (SELECT 'x'
03295                                            FROM {block_instances}
03296                                           WHERE id = {comments}.itemid
03297                                                 AND blockname = 'comments')");
03298 
03299         // remove all orphaned record
03300         $DB->delete_records('comments', array('commentarea'=>'block_comments'));
03301         upgrade_main_savepoint(true, 2010042801);
03302     }
03303 
03304     if ($oldversion < 2010042802) { // Change backup_controllers->type to varchar10 (recreate dep. index)
03305 
03307         $table = new xmldb_table('backup_controllers');
03308         $index = new xmldb_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
03309 
03311         if ($dbman->index_exists($table, $index)) {
03312             $dbman->drop_index($table, $index);
03313         }
03314 
03316         $table = new xmldb_table('backup_controllers');
03317         $field = new xmldb_field('type', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'backupid');
03318 
03320         $dbman->change_field_precision($table, $field);
03321 
03323         $table = new xmldb_table('backup_controllers');
03324         $index = new xmldb_index('typeitem_ix', XMLDB_INDEX_NOTUNIQUE, array('type', 'itemid'));
03325 
03327         if (!$dbman->index_exists($table, $index)) {
03328             $dbman->add_index($table, $index);
03329         }
03330 
03332         upgrade_main_savepoint(true, 2010042802);
03333     }
03334 
03335     if ($oldversion < 2010043000) {  // Adding new course completion feature
03336 
03339         $table = new xmldb_table('course_completion_aggr_methd');
03340 
03342         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03343         $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03344         $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, null);
03345         $table->add_field('method', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03346         $table->add_field('value', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
03347 
03349         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03350 
03352         $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
03353         $table->add_index('criteriatype', XMLDB_INDEX_NOTUNIQUE, array('criteriatype'));
03354 
03356         if (!$dbman->table_exists($table)) {
03357             $dbman->create_table($table);
03358         }
03359 
03360 
03362         $table = new xmldb_table('course_completion_criteria');
03363 
03365         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03366         $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03367         $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03368         $table->add_field('module', XMLDB_TYPE_CHAR, '100', null, null, null, null);
03369         $table->add_field('moduleinstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
03370         $table->add_field('courseinstance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
03371         $table->add_field('enrolperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
03372         $table->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
03373         $table->add_field('gradepass', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
03374         $table->add_field('role', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
03375 
03377         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03378 
03380         $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
03381 
03383         if (!$dbman->table_exists($table)) {
03384             $dbman->create_table($table);
03385         }
03386 
03387 
03389         $table = new xmldb_table('course_completion_crit_compl');
03390 
03392         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03393         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03394         $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03395         $table->add_field('criteriaid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03396         $table->add_field('gradefinal', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
03397         $table->add_field('unenroled', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
03398         $table->add_field('deleted', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
03399         $table->add_field('timecompleted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
03400 
03402         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03403 
03405         $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
03406         $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
03407         $table->add_index('criteriaid', XMLDB_INDEX_NOTUNIQUE, array('criteriaid'));
03408         $table->add_index('timecompleted', XMLDB_INDEX_NOTUNIQUE, array('timecompleted'));
03409 
03411         if (!$dbman->table_exists($table)) {
03412             $dbman->create_table($table);
03413         }
03414 
03415 
03417         $table = new xmldb_table('course_completion_notify');
03418 
03420         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03421         $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03422         $table->add_field('role', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03423         $table->add_field('message', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
03424         $table->add_field('timesent', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03425 
03427         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03428 
03430         $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
03431 
03433         if (!$dbman->table_exists($table)) {
03434             $dbman->create_table($table);
03435         }
03436 
03438         $table = new xmldb_table('course_completions');
03439 
03441         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03442         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03443         $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03444         $table->add_field('deleted', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
03445         $table->add_field('timenotified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
03446         $table->add_field('timeenrolled', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03447         $table->add_field('timestarted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03448         $table->add_field('timecompleted', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
03449         $table->add_field('reaggregate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03450 
03452         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03453 
03455         $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
03456         $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
03457         $table->add_index('timecompleted', XMLDB_INDEX_NOTUNIQUE, array('timecompleted'));
03458 
03460         if (!$dbman->table_exists($table)) {
03461             $dbman->create_table($table);
03462         }
03463 
03464 
03467         $table = new xmldb_table('course');
03468         $field = new xmldb_field('enablecompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
03469 
03471         if (!$dbman->field_exists($table, $field)) {
03472             $dbman->add_field($table, $field);
03473         }
03474 
03476         $field = new xmldb_field('completionstartonenrol', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'enablecompletion');
03477 
03479         if (!$dbman->field_exists($table, $field)) {
03480             $dbman->add_field($table, $field);
03481         }
03482 
03484         $field = new xmldb_field('completionnotify', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'enablecompletion');
03485 
03487         if (!$dbman->field_exists($table, $field)) {
03488             $dbman->add_field($table, $field);
03489         }
03490 
03491         upgrade_main_savepoint(true, 2010043000);
03492     }
03493 
03494     if ($oldversion < 2010043001) {
03495 
03497         $table = new xmldb_table('registration_hubs');
03498 
03500         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03501         $table->add_field('token', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
03502         $table->add_field('hubname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
03503         $table->add_field('huburl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
03504         $table->add_field('confirmed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03505 
03507         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03508 
03510         if (!$dbman->table_exists($table)) {
03511             $dbman->create_table($table);
03512         }
03513 
03515         upgrade_main_savepoint(true, 2010043001);
03516     }
03517 
03518     if ($oldversion < 2010050200) {
03519 
03521         $table = new xmldb_table('backup_logs');
03522 
03524         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03525         $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
03526         $table->add_field('loglevel', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03527         $table->add_field('message', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
03528         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03529 
03531         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03532         $table->add_key('backupid', XMLDB_KEY_FOREIGN, array('backupid'), 'backup_controllers', array('backupid'));
03533 
03535         $table->add_index('backupid-id', XMLDB_INDEX_UNIQUE, array('backupid', 'id'));
03536 
03538         if (!$dbman->table_exists($table)) {
03539             $dbman->create_table($table);
03540         }
03541 
03543 
03545         $table = new xmldb_table('backup_files');
03546 
03548         if ($dbman->table_exists($table)) {
03549             $dbman->drop_table($table);
03550         }
03551 
03553         $table = new xmldb_table('backup_ids');
03554 
03556         if ($dbman->table_exists($table)) {
03557             $dbman->drop_table($table);
03558         }
03559 
03561         upgrade_main_savepoint(true, 2010050200);
03562     }
03563 
03564     if ($oldversion < 2010050403) {  // my_pages for My Moodle and Public Profile pages
03565 
03567         $table = new xmldb_table('my_pages');
03568 
03570         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03571         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 0);
03572         $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
03573         $table->add_field('private', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
03574         $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '6', null, XMLDB_NOTNULL, null, '0');
03575 
03576 
03578         $table->add_key('id', XMLDB_KEY_PRIMARY, array('id'));
03579 
03581         $table->add_index('useridprivate', XMLDB_INDEX_NOTUNIQUE, array('userid', 'private'));
03582 
03584         if (!$dbman->table_exists($table)) {
03585             $dbman->create_table($table);
03586         }
03587 
03589         $mypage = new stdClass();
03590         $mypage->userid = NULL;
03591         $mypage->name = '__default';
03592         $mypage->private = 0;
03593         $mypage->sortorder  = 0;
03594         if (!$DB->record_exists('my_pages', array('userid'=>NULL, 'private'=>0))) {
03595             $DB->insert_record('my_pages', $mypage);
03596         }
03597         $mypage->private = 1;
03598         if (!$DB->record_exists('my_pages', array('userid'=>NULL, 'private'=>1))) {
03599             $DB->insert_record('my_pages', $mypage);
03600         }
03601 
03605 
03606         if ($mypage = $DB->get_record('my_pages', array('userid'=>NULL, 'private'=>1))) {
03607             if (!$DB->record_exists('block_instances', array('pagetypepattern'=>'my-index', 'parentcontextid'=>SITEID, 'subpagepattern'=>$mypage->id))) {
03608 
03609                 // No default exist there yet, let's put a few into My Moodle so it's useful.
03610 
03611                 $blockinstance = new stdClass;
03612                 $blockinstance->parentcontextid = SYSCONTEXTID;
03613                 $blockinstance->showinsubcontexts = 0;
03614                 $blockinstance->pagetypepattern = 'my-index';
03615                 $blockinstance->subpagepattern = $mypage->id;
03616                 $blockinstance->configdata = '';
03617 
03618                 $blockinstance->blockname = 'private_files';
03619                 $blockinstance->defaultregion = 'side-post';
03620                 $blockinstance->defaultweight = 0;
03621                 $blockinstanceid = $DB->insert_record('block_instances', $blockinstance);
03622                 get_context_instance(CONTEXT_BLOCK, $blockinstanceid);
03623 
03624                 $blockinstance->blockname = 'online_users';
03625                 $blockinstance->defaultregion = 'side-post';
03626                 $blockinstance->defaultweight = 1;
03627                 $blockinstanceid = $DB->insert_record('block_instances', $blockinstance);
03628                 get_context_instance(CONTEXT_BLOCK, $blockinstanceid);
03629 
03630                 $blockinstance->blockname = 'course_overview';
03631                 $blockinstance->defaultregion = 'content';
03632                 $blockinstance->defaultweight = 0;
03633                 $blockinstanceid = $DB->insert_record('block_instances', $blockinstance);
03634                 get_context_instance(CONTEXT_BLOCK, $blockinstanceid);
03635             }
03636         }
03637 
03639         upgrade_main_savepoint(true, 2010050403);
03640     }
03641 
03642     if ($oldversion < 2010051500) {
03643 
03645         $table = new xmldb_table('published_courses');
03646         if ($dbman->table_exists($table)) {
03647             $dbman->drop_table($table);
03648         }
03649 
03651         $table = new xmldb_table('course_published');
03652 
03654         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03655         $table->add_field('hubid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03656         $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03657         $table->add_field('timepublished', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03658         $table->add_field('enrollable', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
03659         $table->add_field('hubcourseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03660 
03662         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03663 
03665         if (!$dbman->table_exists($table)) {
03666             $dbman->create_table($table);
03667         }
03668 
03670         upgrade_main_savepoint(true, 2010051500);
03671     }
03672 
03673     if ($oldversion < 2010051600) {
03674 
03676         $DB->delete_records('block', array('name'=>'admin'));
03677         $DB->delete_records('block', array('name'=>'admin_tree'));
03678 
03680         upgrade_main_savepoint(true, 2010051600);
03681     }
03682 
03683     if ($oldversion < 2010051800) {
03684         // switching to userid in config settings because user names are not unique and reliable enough
03685         if (!empty($CFG->courserequestnotify) and $CFG->courserequestnotify !== '$@NONE@$' and $CFG->courserequestnotify !== '$@ALL@$') {
03686             list($where, $params) = $DB->get_in_or_equal(explode(',', $CFG->courserequestnotify));
03687             $params[] = $CFG->mnet_localhost_id;
03688             $users = $DB->get_fieldset_select('user', 'id', "username $where AND mnethostid = ?", $params);
03689             if ($users) {
03690                 set_config('courserequestnotify', implode(',', $users));
03691             } else {
03692                 set_config('courserequestnotify', '$@NONE@$');
03693             }
03694         }
03695         upgrade_main_savepoint(true, 2010051800);
03696     }
03697 
03698     if ($oldversion < 2010051801) {
03699         // Update the notifyloginfailures setting.
03700         if ($CFG->notifyloginfailures == 'mainadmin') {
03701             if ($admins = explode(',', $CFG->siteadmins)) {
03702                 $adminid = reset($admins);
03703                 set_config('notifyloginfailures', $adminid);
03704             } else {
03705                 unset_config('notifyloginfailures'); // let them choose
03706             }
03707             unset($admins);
03708 
03709         } else if ($CFG->notifyloginfailures == 'alladmins') {
03710             set_config('notifyloginfailures', '$@ALL@$');
03711 
03712         } else {
03713             set_config('notifyloginfailures', '$@NONE@$');
03714         }
03715 
03716         upgrade_main_savepoint(true, 2010051801);
03717     }
03718 
03719     if ($oldversion < 2010052200) {
03720         // Define field legacyfiles to be added to course - just in case we are upgrading from PR1
03721         $table = new xmldb_table('course');
03722         $field = new xmldb_field('legacyfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'maxbytes');
03723 
03724         // Conditionally launch add field legacyfiles
03725         if (!$dbman->field_exists($table, $field)) {
03726             $dbman->add_field($table, $field);
03727             // enable legacy files in all courses
03728             $DB->execute("UPDATE {course} SET legacyfiles = 2");
03729         }
03730 
03731         // Main savepoint reached
03732         upgrade_main_savepoint(true, 2010052200);
03733     }
03734 
03735     if ($oldversion < 2010052401) {
03736 
03738         $table = new xmldb_table('course_published');
03739         $field = new xmldb_field('status', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0', 'hubcourseid');
03740 
03742         if (!$dbman->field_exists($table, $field)) {
03743             $dbman->add_field($table, $field);
03744         }
03745 
03747         $table = new xmldb_table('course_published');
03748         $field = new xmldb_field('timechecked', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'status');
03749 
03751         if (!$dbman->field_exists($table, $field)) {
03752             $dbman->add_field($table, $field);
03753         }
03754 
03756         upgrade_main_savepoint(true, 2010052401);
03757     }
03758 
03759     if ($oldversion < 2010052700) {
03760 
03762         $table = new xmldb_table('course_sections');
03763         $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'summary');
03764 
03766         if (!$dbman->field_exists($table, $field)) {
03767             $dbman->add_field($table, $field);
03768         }
03769 
03770         $DB->set_field('course_sections', 'summaryformat', 1, array()); // originally treated as HTML
03771 
03773         upgrade_main_savepoint(true, 2010052700);
03774     }
03775 
03776     if ($oldversion < 2010052800) {
03778         require_once($CFG->dirroot . '/course/lib.php');
03779         rebuild_course_cache(0, true);
03780         upgrade_main_savepoint(true, 2010052800);
03781     }
03782 
03783     if ($oldversion < 2010052801) {
03784 
03786         $table = new xmldb_table('files');
03787         $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
03788 
03790         if (!$dbman->field_exists($table, $field)) {
03791             $dbman->add_field($table, $field);
03792         }
03793 
03795         upgrade_main_savepoint(true, 2010052801);
03796     }
03797 
03798     if ($oldversion < 2010061900.01) {
03799         // Define table enrol to be created
03800         $table = new xmldb_table('enrol');
03801 
03802         // Adding fields to table enrol
03803         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03804         $table->add_field('enrol', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
03805         $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03806         $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03807         $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03808         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null);
03809         $table->add_field('enrolperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
03810         $table->add_field('enrolstartdate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
03811         $table->add_field('enrolenddate', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
03812         $table->add_field('expirynotify', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0');
03813         $table->add_field('expirythreshold', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
03814         $table->add_field('notifyall', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0');
03815         $table->add_field('password', XMLDB_TYPE_CHAR, '50', null, null, null, null);
03816         $table->add_field('cost', XMLDB_TYPE_CHAR, '20', null, null, null, null);
03817         $table->add_field('currency', XMLDB_TYPE_CHAR, '3', null, null, null, null);
03818         $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
03819         $table->add_field('customint1', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
03820         $table->add_field('customint2', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
03821         $table->add_field('customint3', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
03822         $table->add_field('customint4', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
03823         $table->add_field('customchar1', XMLDB_TYPE_CHAR, '255', null, null, null, null);
03824         $table->add_field('customchar2', XMLDB_TYPE_CHAR, '255', null, null, null, null);
03825         $table->add_field('customdec1', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null);
03826         $table->add_field('customdec2', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null);
03827         $table->add_field('customtext1', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
03828         $table->add_field('customtext2', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
03829         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03830         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03831 
03832         // Adding keys to table enrol
03833         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03834         $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
03835 
03836         // Adding indexes to table enrol
03837         $table->add_index('enrol', XMLDB_INDEX_NOTUNIQUE, array('enrol'));
03838 
03839         // launch create table for enrol
03840         $dbman->create_table($table);
03841 
03842         // Main savepoint reached
03843         upgrade_main_savepoint(true, 2010061900.01);
03844     }
03845 
03846     if ($oldversion < 2010061900.02) {
03847         // Define table course_participant to be created
03848         $table = new xmldb_table('user_enrolments');
03849 
03850         // Adding fields to table course_participant
03851         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
03852         $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03853         $table->add_field('enrolid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03854         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
03855         $table->add_field('timestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03856         $table->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2147483647');
03857         $table->add_field('modifierid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03858         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03859         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
03860 
03861         // Adding keys to table course_participant
03862         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
03863         $table->add_key('enrolid', XMLDB_KEY_FOREIGN, array('enrolid'), 'enrol', array('id'));
03864         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
03865         $table->add_key('modifierid', XMLDB_KEY_FOREIGN, array('modifierid'), 'user', array('id'));
03866 
03867 
03868         // Adding indexes to table user_enrolments
03869         $table->add_index('enrolid-userid', XMLDB_INDEX_UNIQUE, array('enrolid', 'userid'));
03870 
03871         // Launch create table for course_participant
03872         $dbman->create_table($table);
03873 
03874         // Main savepoint reached
03875         upgrade_main_savepoint(true, 2010061900.02);
03876     }
03877 
03878     if ($oldversion < 2010061900.03) {
03879         // Define field itemid to be added to role_assignments
03880         $table = new xmldb_table('role_assignments');
03881         $field = new xmldb_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'enrol');
03882 
03883         // Launch add field itemid
03884         $dbman->add_field($table, $field);
03885 
03886         // The new enrol plugins may assign one role several times in one context,
03887         // if we did not allow it we would have big problems with roles when unenrolling
03888         $table = new xmldb_table('role_assignments');
03889         $index = new xmldb_index('contextid-roleid-userid', XMLDB_INDEX_UNIQUE, array('contextid', 'roleid', 'userid'));
03890 
03891         // Conditionally launch drop index contextid-roleid-userid
03892         if ($dbman->index_exists($table, $index)) {
03893             $dbman->drop_index($table, $index);
03894         }
03895 
03896         // Main savepoint reached
03897         upgrade_main_savepoint(true, 2010061900.03);
03898     }
03899 
03900     if ($oldversion < 2010061900.04) {
03901         // there is no default course role any more, each enrol plugin has to handle it separately
03902         if (!empty($CFG->defaultcourseroleid)) {
03903             $sql = "UPDATE {course} SET defaultrole = :defaultrole WHERE defaultrole = 0";
03904             $params = array('defaultrole' => $CFG->defaultcourseroleid);
03905             $DB->execute($sql, $params);
03906         }
03907         unset_config('defaultcourseroleid');
03908 
03909         // Main savepoint reached
03910         upgrade_main_savepoint(true, 2010061900.04);
03911     }
03912 
03913     if ($oldversion < 2010061900.05) {
03914         // make sure enrol settings make actually sense and tweak defaults a bit
03915 
03916         $sqlempty = $DB->sql_empty();
03917 
03918         // set course->enrol to default value so that other upgrade code is simpler
03919         $defaultenrol = empty($CFG->enrol) ? 'manual' : $CFG->enrol;
03920         $sql = "UPDATE {course} SET enrol = ? WHERE enrol = '$sqlempty'";
03921         $DB->execute($sql, array($defaultenrol));
03922         unset_config('enrol');
03923 
03924         if (!isset($CFG->enrol_plugins_enabled) or empty($CFG->enrol_plugins_enabled)) {
03925             set_config('enrol_plugins_enabled', 'manual');
03926         } else {
03927             $enabledplugins = explode(',', $CFG->enrol_plugins_enabled);
03928             $enabledplugins = array_unique($enabledplugins);
03929             set_config('enrol_plugins_enabled', implode(',', $enabledplugins));
03930         }
03931 
03932         // Main savepoint reached
03933         upgrade_main_savepoint(true, 2010061900.05);
03934     }
03935 
03936     if ($oldversion < 2010061900.06) {
03937         $sqlempty = $DB->sql_empty();
03938         $params = array('siteid'=>SITEID);
03939 
03940         // enable manual in all courses
03941         $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, expirynotify, expirythreshold, notifyall, roleid, timecreated, timemodified)
03942                 SELECT 'manual', 0, id, 0, enrolperiod, expirynotify, expirythreshold, notifystudents, defaultrole, timecreated, timemodified
03943                   FROM {course}
03944                  WHERE id <> :siteid";
03945         $DB->execute($sql, $params);
03946 
03947         // enable self enrol only when course enrollable
03948         $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
03949                                      notifyall, password, roleid, timecreated, timemodified)
03950                 SELECT 'self', 0, id, 1, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
03951                        notifystudents, password, defaultrole, timecreated, timemodified
03952                   FROM {course}
03953                  WHERE enrollable = 1 AND id <> :siteid";
03954         $DB->execute($sql, $params);
03955 
03956         // enable guest access if previously allowed - separately with or without password
03957         $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, timecreated, timemodified)
03958                 SELECT 'guest', 0, id, 2, timecreated, timemodified
03959                   FROM {course}
03960                  WHERE guest = 1 AND id <> :siteid";
03961         $DB->execute($sql, $params);
03962         $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, password, timecreated, timemodified)
03963                 SELECT 'guest', 0, id, 2, password, timecreated, timemodified
03964                   FROM {course}
03965                  WHERE guest = 2 and password <> '$sqlempty' AND id <> :siteid";
03966         $DB->execute($sql, $params);
03967 
03968         upgrade_main_savepoint(true, 2010061900.06);
03969     }
03970 
03971     if ($oldversion < 2010061900.07) {
03972         // now migrate old style "interactive" enrol plugins - we know them by looking into course.enrol
03973         $params = array('siteid'=>SITEID);
03974         $enabledplugins = explode(',', $CFG->enrol_plugins_enabled);
03975         $usedplugins = $DB->get_fieldset_sql("SELECT DISTINCT enrol FROM {course}");
03976         foreach ($usedplugins as $plugin) {
03977             if ($plugin === 'manual') {
03978                 continue;
03979             }
03980             $enabled = in_array($plugin, $enabledplugins) ? 0 : 1; // 0 means active, 1 disabled
03981             $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
03982                                          notifyall, password, cost, currency, roleid, timecreated, timemodified)
03983                     SELECT enrol, $enabled, id, 4, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
03984                            notifystudents, password, cost, currency, defaultrole, timecreated, timemodified
03985                       FROM {course}
03986                      WHERE enrol = :plugin AND id <> :siteid";
03987             $params['plugin'] = $plugin;
03988             $DB->execute($sql, $params);
03989         }
03990         upgrade_main_savepoint(true, 2010061900.07);
03991     }
03992 
03993     if ($oldversion < 2010061900.08) {
03994         // now migrate the rest - these plugins are not in course.enrol, instead we just look for suspicious role assignments,
03995         // unfortunately old enrol plugins were doing sometimes weird role assignments :-(
03996 
03997         // enabled
03998             $enabledplugins = explode(',', $CFG->enrol_plugins_enabled);
03999         list($sqlenabled, $params) = $DB->get_in_or_equal($enabledplugins, SQL_PARAMS_NAMED, 'ena');
04000         $params['siteid'] = SITEID;
04001         $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
04002                                      notifyall, password, cost, currency, roleid, timecreated, timemodified)
04003                 SELECT DISTINCT ra.enrol, 0, c.id, 5, c.enrolperiod, c.enrolstartdate, c.enrolenddate, c.expirynotify, c.expirythreshold,
04004                        c.notifystudents, c.password, c.cost, c.currency, c.defaultrole, c.timecreated, c.timemodified
04005                   FROM {course} c
04006                   JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = 50)
04007                   JOIN {role_assignments} ra ON (ra.contextid = ctx.id)
04008                  WHERE c.id <> :siteid AND ra.enrol $sqlenabled";
04009         $processed = $DB->get_fieldset_sql("SELECT DISTINCT enrol FROM {enrol}");
04010         if ($processed) {
04011             list($sqlnotprocessed, $params2) = $DB->get_in_or_equal($processed, SQL_PARAMS_NAMED, 'np', false);
04012             $params = array_merge($params, $params2);
04013             $sql = "$sql AND ra.enrol $sqlnotprocessed";
04014         }
04015         $DB->execute($sql, $params);
04016 
04017         // disabled
04018         $params = array('siteid' => SITEID);
04019         $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, enrolperiod, enrolstartdate, enrolenddate, expirynotify, expirythreshold,
04020                                      notifyall, password, cost, currency, roleid, timecreated, timemodified)
04021                 SELECT DISTINCT ra.enrol, 1, c.id, 5, c.enrolperiod, c.enrolstartdate, c.enrolenddate, c.expirynotify, c.expirythreshold,
04022                        c.notifystudents, c.password, c.cost, c.currency, c.defaultrole, c.timecreated, c.timemodified
04023                   FROM {course} c
04024                   JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = 50)
04025                   JOIN {role_assignments} ra ON (ra.contextid = ctx.id)
04026                  WHERE c.id <> :siteid";
04027         $processed = $DB->get_fieldset_sql("SELECT DISTINCT enrol FROM {enrol}");
04028         if ($processed) {
04029             list($sqlnotprocessed, $params2) = $DB->get_in_or_equal($processed, SQL_PARAMS_NAMED, 'np', false);
04030             $params = array_merge($params, $params2);
04031             $sql = "$sql AND ra.enrol $sqlnotprocessed";
04032         }
04033         $DB->execute($sql, $params);
04034 
04035         upgrade_main_savepoint(true, 2010061900.08);
04036     }
04037 
04038     if ($oldversion < 2010061900.09) {
04039         // unfortunately there may be still some leftovers
04040         // after reconfigured, uninstalled or borked enrol plugins,
04041         // unfortunately this may be a bit slow - but there should not be many of these
04042         upgrade_set_timeout();
04043         $sqlempty = $DB->sql_empty();
04044         $sql = "SELECT DISTINCT c.id AS courseid, ra.enrol, c.timecreated, c.timemodified
04045                   FROM {course} c
04046                   JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = 50)
04047                   JOIN {role_assignments} ra ON (ra.contextid = ctx.id AND ra.enrol <> '$sqlempty')
04048              LEFT JOIN {enrol} e ON (e.courseid = c.id AND e.enrol = ra.enrol)
04049                  WHERE c.id <> :siteid AND e.id IS NULL";
04050         $params = array('siteid'=>SITEID);
04051         $rs = $DB->get_recordset_sql($sql, $params);
04052         foreach ($rs as $enrol) {
04053             upgrade_set_timeout();
04054             $enrol->status = 1; // better disable them
04055             $DB->insert_record('enrol', $enrol);
04056         }
04057         $rs->close();
04058         upgrade_main_savepoint(true, 2010061900.09);
04059     }
04060 
04061     if ($oldversion < 2010061900.10) {
04062         // migrate existing setup of meta courses, ignore records referencing invalid courses
04063         $sql = "INSERT INTO {enrol} (enrol, status, courseid, sortorder, customint1)
04064                 SELECT 'meta', 0, cm.parent_course, 5, cm.child_course
04065                   FROM {course_meta} cm
04066                   JOIN {course} p ON p.id = cm.parent_course
04067                   JOIN {course} c ON c.id = cm.child_course";
04068         $DB->execute($sql);
04069 
04070         upgrade_main_savepoint(true, 2010061900.10);
04071     }
04072 
04073     if ($oldversion < 2010061900.11) {
04074         // nuke any old role assignments+enrolments in previous meta courses, we have to start from scratch
04075         $select = "SELECT ctx.id
04076                      FROM {context} ctx
04077                      JOIN {course} c ON (c.id = ctx.instanceid AND ctx.contextlevel = 50 AND c.metacourse = 1)";
04078         $DB->delete_records_select('role_assignments', "contextid IN ($select) AND enrol = 'manual'");
04079 
04080         // course_meta to be dropped - we use enrol_meta plugin instead now
04081         $table = new xmldb_table('course_meta');
04082 
04083         // Launch drop table for course_meta
04084         $dbman->drop_table($table);
04085 
04086         // Main savepoint reached
04087         upgrade_main_savepoint(true, 2010061900.11);
04088     }
04089 
04090     if ($oldversion < 2010061900.12) {
04091         // finally remove all obsolete fields from the course table - yay!
04092         // all the information was migrated to the enrol table
04093 
04094         // Define field guest to be dropped from course
04095         $table = new xmldb_table('course');
04096         $field = new xmldb_field('guest');
04097 
04098         // Conditionally launch drop field guest
04099         if ($dbman->field_exists($table, $field)) {
04100             $dbman->drop_field($table, $field);
04101         }
04102 
04103         // Define field password to be dropped from course
04104         $table = new xmldb_table('course');
04105         $field = new xmldb_field('password');
04106 
04107         // Conditionally launch drop field password
04108         if ($dbman->field_exists($table, $field)) {
04109             $dbman->drop_field($table, $field);
04110         }
04111 
04112         // Define field enrolperiod to be dropped from course
04113         $table = new xmldb_table('course');
04114         $field = new xmldb_field('enrolperiod');
04115 
04116         // Conditionally launch drop field enrolperiod
04117         if ($dbman->field_exists($table, $field)) {
04118             $dbman->drop_field($table, $field);
04119         }
04120 
04121         // Define field cost to be dropped from course
04122         $table = new xmldb_table('course');
04123         $field = new xmldb_field('cost');
04124 
04125         // Conditionally launch drop field cost
04126         if ($dbman->field_exists($table, $field)) {
04127             $dbman->drop_field($table, $field);
04128         }
04129 
04130         // Define field currency to be dropped from course
04131         $table = new xmldb_table('course');
04132         $field = new xmldb_field('currency');
04133 
04134         // Conditionally launch drop field currency
04135         if ($dbman->field_exists($table, $field)) {
04136             $dbman->drop_field($table, $field);
04137         }
04138 
04139         // Define field metacourse to be dropped from course
04140         $table = new xmldb_table('course');
04141         $field = new xmldb_field('metacourse');
04142 
04143         // Conditionally launch drop field metacourse
04144         if ($dbman->field_exists($table, $field)) {
04145             $dbman->drop_field($table, $field);
04146         }
04147 
04148         // Define field expirynotify to be dropped from course
04149         $table = new xmldb_table('course');
04150         $field = new xmldb_field('expirynotify');
04151 
04152         // Conditionally launch drop field expirynotify
04153         if ($dbman->field_exists($table, $field)) {
04154             $dbman->drop_field($table, $field);
04155         }
04156 
04157         // Define field expirythreshold to be dropped from course
04158         $table = new xmldb_table('course');
04159         $field = new xmldb_field('expirythreshold');
04160 
04161         // Conditionally launch drop field expirythreshold
04162         if ($dbman->field_exists($table, $field)) {
04163             $dbman->drop_field($table, $field);
04164         }
04165 
04166         // Define field notifystudents to be dropped from course
04167         $table = new xmldb_table('course');
04168         $field = new xmldb_field('notifystudents');
04169 
04170         // Conditionally launch drop field notifystudents
04171         if ($dbman->field_exists($table, $field)) {
04172             $dbman->drop_field($table, $field);
04173         }
04174 
04175         // Define field enrollable to be dropped from course
04176         $table = new xmldb_table('course');
04177         $field = new xmldb_field('enrollable');
04178 
04179         // Conditionally launch drop field enrollable
04180         if ($dbman->field_exists($table, $field)) {
04181             $dbman->drop_field($table, $field);
04182         }
04183 
04184         // Define field enrolstartdate to be dropped from course
04185         $table = new xmldb_table('course');
04186         $field = new xmldb_field('enrolstartdate');
04187 
04188         // Conditionally launch drop field enrolstartdate
04189         if ($dbman->field_exists($table, $field)) {
04190             $dbman->drop_field($table, $field);
04191         }
04192 
04193         // Define field enrolenddate to be dropped from course
04194         $table = new xmldb_table('course');
04195         $field = new xmldb_field('enrolenddate');
04196 
04197         // Conditionally launch drop field enrolenddate
04198         if ($dbman->field_exists($table, $field)) {
04199             $dbman->drop_field($table, $field);
04200         }
04201 
04202         // Define field enrol to be dropped from course
04203         $table = new xmldb_table('course');
04204         $field = new xmldb_field('enrol');
04205 
04206         // Conditionally launch drop field enrol
04207         if ($dbman->field_exists($table, $field)) {
04208             $dbman->drop_field($table, $field);
04209         }
04210 
04211         // Define field defaultrole to be dropped from course
04212         $table = new xmldb_table('course');
04213         $field = new xmldb_field('defaultrole');
04214 
04215         // Conditionally launch drop field defaultrole
04216         if ($dbman->field_exists($table, $field)) {
04217             $dbman->drop_field($table, $field);
04218         }
04219 
04220         upgrade_main_savepoint(true, 2010061900.12);
04221     }
04222 
04223     if ($oldversion < 2010061900.13) {
04224         // Define field visibleold to be added to course_categories
04225         $table = new xmldb_table('course_categories');
04226         $field = new xmldb_field('visibleold', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'visible');
04227 
04228         // Launch add field visibleold
04229         $dbman->add_field($table, $field);
04230 
04231         // Main savepoint reached
04232         upgrade_main_savepoint(true, 2010061900.13);
04233     }
04234 
04235     if ($oldversion < 2010061900.14) {
04236         // keep previous visible state
04237         $DB->execute("UPDATE {course_categories} SET visibleold = visible");
04238 
04239         // make sure all subcategories of hidden categories are hidden too, do not rely on category path yet
04240         $sql = "SELECT c.id
04241                   FROM {course_categories} c
04242                   JOIN {course_categories} pc ON (pc.id = c.parent AND pc.visible = 0)
04243                  WHERE c.visible = 1";
04244         while ($categories = $DB->get_records_sql($sql)) {
04245             foreach ($categories as $cat) {
04246                 upgrade_set_timeout();
04247                 $DB->set_field('course_categories', 'visible', 0, array('id'=>$cat->id));
04248             }
04249         }
04250         upgrade_main_savepoint(true, 2010061900.14);
04251     }
04252 
04253     if ($oldversion < 2010061900.15) {
04254         // Define field visibleold to be added to course
04255         $table = new xmldb_table('course');
04256         $field = new xmldb_field('visibleold', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'visible');
04257 
04258         // Launch add field visibleold
04259         $dbman->add_field($table, $field);
04260 
04261         // Main savepoint reached
04262         upgrade_main_savepoint(true, 2010061900.15);
04263     }
04264 
04265     if ($oldversion < 2010061900.16) {
04266         // keep previous visible state
04267         $DB->execute("UPDATE {course} SET visibleold = visible");
04268 
04269         // make sure all courses in hidden categories are hidden
04270         $DB->execute("UPDATE {course} SET visible = 0 WHERE category IN (SELECT id FROM {course_categories} WHERE visible = 0)");
04271 
04272         upgrade_main_savepoint(true, 2010061900.16);
04273     }
04274 
04275     if ($oldversion < 2010061900.20) {
04276         // 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
04277 
04278         $syscontext = get_context_instance(CONTEXT_SYSTEM);
04279         $params = array('syscontext'=>$syscontext->id, 'participate'=>'moodle/course:participate');
04280         $roles = $DB->get_fieldset_sql("SELECT DISTINCT roleid FROM {role_capabilities} WHERE contextid = :syscontext AND capability = :participate AND permission = 1", $params);
04281         if ($roles) {
04282             list($sqlroles, $params) = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED, 'r');
04283 
04284             $sql = "INSERT INTO {user_enrolments} (status, enrolid, userid, timestart, timeend, modifierid, timecreated, timemodified)
04285 
04286                     SELECT 0, e.id, ra.userid, MIN(ra.timestart), MIN(ra.timeend), 0, MIN(ra.timemodified), MAX(ra.timemodified)
04287                       FROM {role_assignments} ra
04288                       JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = 50)
04289                       JOIN {enrol} e ON (e.enrol = ra.enrol AND e.courseid = c.instanceid)
04290                       JOIN {user} u ON u.id = ra.userid
04291                      WHERE u.deleted = 0 AND ra.roleid $sqlroles
04292                   GROUP BY e.id, ra.userid";
04293             $DB->execute($sql, $params);
04294         }
04295 
04296         upgrade_main_savepoint(true, 2010061900.20);
04297     }
04298 
04299     if ($oldversion < 2010061900.21) {
04300         // hidden is completely removed, timestart+timeend are now in the user_enrolments table
04301 
04302         // Define field hidden to be dropped from role_assignments
04303         $table = new xmldb_table('role_assignments');
04304         $field = new xmldb_field('hidden');
04305 
04306         // Conditionally launch drop field hidden
04307         if ($dbman->field_exists($table, $field)) {
04308             $dbman->drop_field($table, $field);
04309         }
04310 
04311         // Define field timestart to be dropped from role_assignments
04312         $table = new xmldb_table('role_assignments');
04313         $field = new xmldb_field('timestart');
04314 
04315         // Conditionally launch drop field timestart
04316         if ($dbman->field_exists($table, $field)) {
04317             $dbman->drop_field($table, $field);
04318         }
04319 
04320         // Define field timeend to be dropped from role_assignments
04321         $table = new xmldb_table('role_assignments');
04322         $field = new xmldb_field('timeend');
04323 
04324         // Conditionally launch drop field timeend
04325         if ($dbman->field_exists($table, $field)) {
04326             $dbman->drop_field($table, $field);
04327         }
04328 
04329         // Main savepoint reached
04330         upgrade_main_savepoint(true, 2010061900.21);
04331     }
04332 
04333     if ($oldversion < 2010061900.22) {
04334         // Rename field enrol on table role_assignments to component and update content
04335 
04336         $table = new xmldb_table('role_assignments');
04337         $field = new xmldb_field('enrol', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'modifierid');
04338 
04339         // Launch rename field enrol
04340         $dbman->rename_field($table, $field, 'component');
04341 
04342         // Changing precision of field component on table role_assignments to (100)
04343         $table = new xmldb_table('role_assignments');
04344         $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'modifierid');
04345 
04346         // Launch change of precision for field component
04347         $dbman->change_field_precision($table, $field);
04348 
04349         // Manual is a special case - we use empty string instead now
04350         $params = array('empty'=>$DB->sql_empty(), 'manual'=>'manual');
04351         $sql = "UPDATE {role_assignments}
04352                    SET component = :empty
04353                  WHERE component = :manual";
04354         $DB->execute($sql, $params);
04355 
04356         // Now migrate to real enrol component names
04357         $params = array('empty'=>$DB->sql_empty());
04358         $concat = $DB->sql_concat("'enrol_'", 'component');
04359         $sql = "UPDATE {role_assignments}
04360                    SET component = $concat
04361                  WHERE component <> :empty
04362                        AND contextid IN (
04363                            SELECT id
04364                              FROM {context}
04365                             WHERE contextlevel >= 50)";
04366         $DB->execute($sql, $params);
04367 
04368         // Now migrate to real auth component names
04369         $params = array('empty'=>$DB->sql_empty());
04370         $concat = $DB->sql_concat("'auth_'", 'component');
04371         $sql = "UPDATE {role_assignments}
04372                    SET component = $concat
04373                  WHERE component <> :empty
04374                        AND contextid IN (
04375                            SELECT id
04376                              FROM {context}
04377                             WHERE contextlevel < 50)";
04378         $DB->execute($sql, $params);
04379 
04380         // Main savepoint reached
04381         upgrade_main_savepoint(true, 2010061900.22);
04382     }
04383 
04384     if ($oldversion < 2010061900.23) {
04385         // add proper itemid to role assignments that were added by enrolment plugins
04386         $sql = "UPDATE {role_assignments}
04387                    SET itemid = (SELECT MIN({enrol}.id)
04388                                     FROM {enrol}
04389                                     JOIN {context} ON ({context}.contextlevel = 50 AND {context}.instanceid = {enrol}.courseid)
04390                                    WHERE {role_assignments}.component = ".$DB->sql_concat("'enrol_'", "{enrol}.enrol")." AND {context}.id = {role_assignments}.contextid)
04391                  WHERE component <> 'enrol_manual' AND component LIKE 'enrol_%'";
04392         $DB->execute($sql);
04393         // Main savepoint reached
04394         upgrade_main_savepoint(true, 2010061900.23);
04395     }
04396 
04397     if ($oldversion < 2010061900.30) {
04398         // make new list of active enrol plugins - order is important, meta should be always last, manual first
04399         $enabledplugins = explode(',', $CFG->enrol_plugins_enabled);
04400         $enabledplugins = array_merge(array('manual', 'guest', 'self', 'cohort'), $enabledplugins);
04401         if ($DB->record_exists('enrol', array('enrol'=>'meta'))) {
04402             $enabledplugins[] = 'meta';
04403         }
04404         $enabledplugins = array_unique($enabledplugins);
04405         set_config('enrol_plugins_enabled', implode(',', $enabledplugins));
04406 
04407         // Main savepoint reached
04408         upgrade_main_savepoint(true, 2010061900.30);
04409     }
04410 
04411     if ($oldversion < 2010061900.31) {
04412         // finalise all new enrol settings and cleanup old settings
04413 
04414         // legacy allowunenrol was deprecated in 1.9 already
04415         unset_config('allwunenroll');
04416 
04417         // obsolete course presets
04418         unset_config('metacourse', 'moodlecourse');
04419         unset_config('enrol', 'moodlecourse');
04420         unset_config('enrollable', 'moodlecourse');
04421         unset_config('enrolperiod', 'moodlecourse');
04422         unset_config('expirynotify', 'moodlecourse');
04423         unset_config('notifystudents', 'moodlecourse');
04424         unset_config('expirythreshold', 'moodlecourse');
04425         unset_config('enrolpassword', 'moodlecourse');
04426         unset_config('guest', 'moodlecourse');
04427 
04428         unset_config('backup_sche_metacourse', 'backup');
04429 
04430         unset_config('lastexpirynotify');
04431 
04432         // hidden course categories now prevent only browsing, courses are accessible if you know the URL and course is visible
04433         unset_config('allowvisiblecoursesinhiddencategories');
04434 
04435         if (isset($CFG->coursemanager)) {
04436             set_config('coursecontact', $CFG->coursemanager);
04437             unset_config('coursemanager');
04438         }
04439 
04440         // migrate plugin settings - the problem here is we are splitting manual into three different plugins
04441         if (isset($CFG->enrol_manual_usepasswordpolicy)) {
04442             set_config('usepasswordpolicy', $CFG->enrol_manual_usepasswordpolicy, 'enrol_guest');
04443             set_config('usepasswordpolicy', $CFG->enrol_manual_usepasswordpolicy, 'enrol_self');
04444             set_config('groupenrolmentkeypolicy', $CFG->enrol_manual_usepasswordpolicy);
04445             unset_config('enrol_manual_usepasswordpolicy');
04446         }
04447         if (isset($CFG->enrol_manual_requirekey)) {
04448             set_config('requirepassword', $CFG->enrol_manual_requirekey, 'enrol_guest');
04449             set_config('requirepassword', $CFG->enrol_manual_requirekey, 'enrol_self');
04450             unset_config('enrol_manual_requirekey');
04451         }
04452         if (isset($CFG->enrol_manual_showhint)) {
04453             set_config('showhint', $CFG->enrol_manual_showhint, 'enrol_guest');
04454             set_config('showhint', $CFG->enrol_manual_showhint, 'enrol_self');
04455             unset_config('enrol_manual_showhint');
04456         }
04457 
04458         upgrade_main_savepoint(true, 2010061900.31);
04459     }
04460 
04461     if ($oldversion < 2010061900.32) {
04462         // MDL-22797 course completion has to be updated to use new enrol framework, it will not be enabled in final 2.0
04463         set_config('enableavailability', 0);
04464         set_config('enablecompletion', 0);
04465         upgrade_main_savepoint(true, 2010061900.32);
04466     }
04467 
04468     if ($oldversion < 2010062101) {
04469 
04471         $table = new xmldb_table('course_published');
04472         $field = new xmldb_field('hubid');
04473 
04475         if ($dbman->field_exists($table, $field)) {
04476             $dbman->drop_field($table, $field);
04477         }
04478 
04480         $table = new xmldb_table('course_published');
04481         $field = new xmldb_field('huburl', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'id');
04482 
04484         if (!$dbman->field_exists($table, $field)) {
04485             $dbman->add_field($table, $field);
04486         }
04487 
04489         upgrade_main_savepoint(true, 2010062101);
04490     }
04491 
04492     if ($oldversion < 2010070300) {
04493         //TODO: this is a temporary hack for upgrade from PR3, to be removed later
04494 
04495         // Define field component to be added to files
04496         $table = new xmldb_table('files');
04497         $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'contextid');
04498 
04499         // Conditionally upgrade from PR3
04500         if (!$dbman->field_exists($table, $field)) {
04501             $dbman->add_field($table, $field);
04502             $index = new xmldb_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid'));
04503             $dbman->drop_index($table, $index);
04504             $index = new xmldb_index('component-filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('component', 'filearea', 'contextid', 'itemid'));
04505             $dbman->add_index($table, $index);
04506 
04507             // Rename areas as add proper component
04508             $areas = $DB->get_fieldset_sql("SELECT DISTINCT filearea FROM {files}");
04509             if ($areas) {
04510                 // fix incorrect itemids
04511                 $DB->execute("UPDATE {files} SET itemid = 0 WHERE filearea = 'category_description'"); // context identifies instances
04512                 $DB->execute("UPDATE {files} SET itemid = 0 WHERE filearea = 'user_profile'"); // context identifies instances
04513                 $DB->execute("UPDATE {files} SET itemid = 0 WHERE filearea = 'block_html'"); // context identifies instances
04514                 foreach ($areas as $area) {
04515                     // rename areas
04516                     if ($area === 'course_backup') {
04517                         $area = 'backup_course';
04518                     } else if ($area === 'section_backup') {
04519                         $area = 'backup_section';
04520                     } else if ($area === 'activity_backup') {
04521                         $area = 'backup_activity';
04522                     } else if ($area === 'category_description') {
04523                         $area = 'coursecat_description';
04524                     }
04525                     if ($area === 'block_html') {
04526                         $component = 'block_html';
04527                         $filearea = 'content';
04528                     } else {
04529                         list($component, $filearea) = explode('_', $area, 2);
04530                         // 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
04531                         if (file_exists("$CFG->dirroot/mod/$component/lib.php")) {
04532                             $component = 'mod_'.$component;
04533                         }
04534                     }
04535                     $DB->execute("UPDATE {files} SET component = :component, filearea = :filearea WHERE filearea = :area", array('component'=>$component, 'filearea'=>$filearea, 'area'=>$area));
04536                 }
04537                 // Update all hashes
04538                 $rs = $DB->get_recordset('files', array());
04539                 foreach ($rs as $file) {
04540                     upgrade_set_timeout();
04541                     $pathnamehash = sha1("/$file->contextid/$file->component/$file->filearea/$file->itemid".$file->filepath.$file->filename);
04542                     $DB->set_field('files', 'pathnamehash', $pathnamehash, array('id'=>$file->id));
04543                 }
04544                 $rs->close();
04545             }
04546         }
04547 
04548         // Main savepoint reached
04549         upgrade_main_savepoint(true, 2010070300);
04550     }
04551 
04552     if ($oldversion < 2010070500) {
04553 
04555         $table = new xmldb_table('backup_controllers');
04556         $field = new xmldb_field('operation', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'backup', 'backupid');
04557 
04559         if (!$dbman->field_exists($table, $field)) {
04560             $dbman->add_field($table, $field);
04561         }
04562 
04564         upgrade_main_savepoint(true, 2010070500);
04565     }
04566 
04567     if ($oldversion < 2010070501) {
04568 
04570         $table = new xmldb_table('user');
04571         $field = new xmldb_field('suspended', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'deleted');
04572 
04574         if (!$dbman->field_exists($table, $field)) {
04575             $dbman->add_field($table, $field);
04576         }
04577 
04579         upgrade_main_savepoint(true, 2010070501);
04580     }
04581 
04582     if ($oldversion < 2010070502) {
04583 
04585         $table = new xmldb_table('backup_ids_template');
04586         $field = new xmldb_field('newitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'itemid');
04588         if (!$dbman->field_exists($table, $field)) {
04589             $dbman->add_field($table, $field);
04590         }
04591 
04593         $table = new xmldb_table('backup_ids_template');
04594         $field = new xmldb_field('info', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'parentitemid');
04596         if (!$dbman->field_exists($table, $field)) {
04597             $dbman->add_field($table, $field);
04598         }
04599 
04601         upgrade_main_savepoint(true, 2010070502);
04602     }
04603 
04604     if ($oldversion < 2010070601) {
04605         // delete loan calc if not used - it was moved to contrib
04606         if (!file_exists("$CFG->dirroot/blocks/loancalc/version.php")) {
04607             if (!$DB->record_exists('block_instances', array('blockname'=>'loancalc'))) {
04608                 $DB->delete_records('block', array('name'=>'loancalc'));
04609             }
04610         }
04611         upgrade_main_savepoint(true, 2010070601);
04612     }
04613 
04614     if ($oldversion < 2010070602) {
04615         // delete exercise if not used and not installed - now in contrib (do not use adminlib uninstall functions, they may change)
04616         if (!file_exists("$CFG->dirroot/mod/exercise/version.php")) {
04617             if ($module = $DB->get_record('modules', array('name'=>'exercise'))) {
04618                 if (!$DB->record_exists('course_modules', array('module'=>$module->id))) {
04619                     //purge capabilities
04620                     $DB->delete_records_select('capabilities', "name LIKE ?", array('mod/exercise:%'));
04621                     $DB->delete_records_select('role_capabilities', "capability LIKE ?", array('mod/exercise:%'));
04622                     $tables = array('exercise', 'exercise_submissions', 'exercise_assessments', 'exercise_elements', 'exercise_rubrics', 'exercise_grades');
04623                     foreach ($tables as $tname) {
04624                         $table = new xmldb_table($tname);
04625                         if ($dbman->table_exists($table)) {
04626                             $dbman->drop_table($table);
04627                         }
04628                     }
04629                     $DB->delete_records('event', array('modulename' => 'exercise'));
04630                     $DB->delete_records('log', array('module' => 'exercise'));
04631                     $DB->delete_records('modules', array('name'=>'exercise'));
04632                 }
04633             }
04634         }
04635         upgrade_main_savepoint(true, 2010070602);
04636     }
04637 
04638     if ($oldversion < 2010070603) {
04639         // delete journal if not used and not installed - now in contrib (do not use adminlib uninstall functions, they may change)
04640         if (!file_exists("$CFG->dirroot/mod/journal/version.php")) {
04641             if ($module = $DB->get_record('modules', array('name'=>'journal'))) {
04642                 if (!$DB->record_exists('course_modules', array('module'=>$module->id))) {
04643                     //purge capabilities
04644                     $DB->delete_records_select('capabilities', "name LIKE ?", array('mod/journal:%'));
04645                     $DB->delete_records_select('role_capabilities', "capability LIKE ?", array('mod/journal:%'));
04646                     $tables = array('journal', 'journal_entries');
04647                     foreach ($tables as $tname) {
04648                         $table = new xmldb_table($tname);
04649                         if ($dbman->table_exists($table)) {
04650                             $dbman->drop_table($table);
04651                         }
04652                     }
04653                     $DB->delete_records('event', array('modulename' => 'journal'));
04654                     $DB->delete_records('log', array('module' => 'journal'));
04655                     $DB->delete_records('modules', array('name'=>'journal'));
04656                     unset_config('journal_initialdisable');
04657                 }
04658             }
04659         }
04660         upgrade_main_savepoint(true, 2010070603);
04661     }
04662 
04663     if ($oldversion < 2010070604) {
04664         // delete lams if not used and not installed - now in contrib (do not use adminlib uninstall functions, they may change)
04665         if (!file_exists("$CFG->dirroot/mod/lams/version.php")) {
04666             if ($module = $DB->get_record('modules', array('name'=>'lams'))) {
04667                 if (!$DB->record_exists('course_modules', array('module'=>$module->id))) {
04668                     //purge capabilities
04669                     $DB->delete_records_select('capabilities', "name LIKE ?", array('mod/lams:%'));
04670                     $DB->delete_records_select('role_capabilities', "capability LIKE ?", array('mod/lams:%'));
04671                     $tables = array('lams', '');
04672                     foreach ($tables as $tname) {
04673                         $table = new xmldb_table($tname);
04674                         if ($dbman->table_exists($table)) {
04675                             $dbman->drop_table($table);
04676                         }
04677                     }
04678                     $DB->delete_records('event', array('modulename' => 'lams'));
04679                     $DB->delete_records('log', array('module' => 'lams'));
04680                     $DB->delete_records('modules', array('name'=>'lams'));
04681                     unset_config('lams_initialdisable');
04682                 }
04683             }
04684         }
04685         upgrade_main_savepoint(true, 2010070604);
04686     }
04687 
04688     if ($oldversion < 2010070801) {
04691         $table = new xmldb_table('user');
04692         $index = new xmldb_index('city', XMLDB_INDEX_NOTUNIQUE, array('city'));
04694         if ($dbman->index_exists($table, $index)) {
04695             $dbman->drop_index($table, $index);
04696         }
04698         $field = new xmldb_field('city', XMLDB_TYPE_CHAR, '120', null, XMLDB_NOTNULL, null, null, 'address');
04699 
04701         $dbman->change_field_precision($table, $field);
04702 
04704         if (!$dbman->index_exists($table, $index)) {
04705             $dbman->add_index($table, $index);
04706         }
04708         upgrade_main_savepoint(true, 2010070801);
04709     }
04710 
04711     if ($oldversion < 2010071000) {
04712         //purge unused editor settings
04713         unset_config('editorbackgroundcolor');
04714         unset_config('editorfontfamily');
04715         unset_config('editorfontsize');
04716         unset_config('editorkillword');
04717         unset_config('editorhidebuttons');
04718         unset_config('editorfontlist');
04719 
04720         upgrade_main_savepoint(true, 2010071000);
04721     }
04722 
04723     if ($oldversion < 2010071001) {
04724         // purge obsolete stats settings
04725         unset_config('statscatdepth');
04726         upgrade_main_savepoint(true, 2010071001);
04727     }
04728 
04729     if ($oldversion < 2010071100) {
04730         // move user icons to file storage pool
04731         upgrade_migrate_user_icons();
04732         upgrade_main_savepoint(true, 2010071100);
04733     }
04734 
04735     if ($oldversion < 2010071101) {
04736         // move user icons to file storage pool
04737         upgrade_migrate_group_icons();
04738         upgrade_main_savepoint(true, 2010071101);
04739     }
04740 
04741     if ($oldversion < 2010071300) {
04742         // Define field timecreated to be added to user_enrolments
04743         $table = new xmldb_table('user_enrolments');
04744         $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'modifierid');
04745 
04746         // Launch add field timecreated
04747         if (!$dbman->field_exists($table, $field)) {
04748             $dbman->add_field($table, $field);
04749         }
04750 
04751         // now try to guess the time created
04752         $sql = "UPDATE {user_enrolments} SET timecreated = timemodified WHERE timecreated = 0";
04753         $DB->execute($sql);
04754         $sql = "UPDATE {user_enrolments} SET timecreated = timestart WHERE timestart <> 0 AND timestart < timemodified";
04755         $DB->execute($sql);
04756 
04757         upgrade_main_savepoint(true, 2010071300);
04758     }
04759 
04760     if ($oldversion < 2010071700) { // Make itemname bigger (160cc) to store component+filearea
04761 
04762         $table = new xmldb_table('backup_ids_template');
04763         // Define key backupid_itemname_itemid_uk (unique) to be dropped form backup_ids_template
04764         $key = new xmldb_key('backupid_itemname_itemid_uk', XMLDB_KEY_UNIQUE, array('backupid', 'itemname', 'itemid'));
04765         // Define index backupid_parentitemid_ix (not unique) to be dropped form backup_ids_template
04766         $index = new xmldb_index('backupid_parentitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'parentitemid'));
04767         // Define field itemname to be 160cc
04768         $field = new xmldb_field('itemname', XMLDB_TYPE_CHAR, '160', null, XMLDB_NOTNULL, null, null, 'backupid');
04769 
04770         // Launch drop key backupid_itemname_itemid_uk
04771         $dbman->drop_key($table, $key);
04772         // Conditionally launch drop index backupid_parentitemid_ix
04773         if ($dbman->index_exists($table, $index)) {
04774             $dbman->drop_index($table, $index);
04775         }
04776 
04777         // Changing precision of field itemname on table backup_ids_template to (160)
04778         $dbman->change_field_precision($table, $field);
04779 
04780         // Launch add key backupid_itemname_itemid_uk
04781         $dbman->add_key($table, $key);
04782         // Conditionally launch add index backupid_parentitemid_ix
04783         if (!$dbman->index_exists($table, $index)) {
04784             $dbman->add_index($table, $index);
04785         }
04786 
04787         // Main savepoint reached
04788         upgrade_main_savepoint(true, 2010071700);
04789     }
04790 
04791     if ($oldversion < 2010071701) {
04792         // Drop legacy core tables that now belongs to mnetservice_enrol plugin
04793         // Upgrade procedure not needed as the tables are used for caching purposes only
04794         $tables = array('mnet_enrol_course', 'mnet_enrol_assignments');
04795         foreach ($tables as $tname) {
04796             $table = new xmldb_table($tname);
04797             if ($dbman->table_exists($table)) {
04798                 $dbman->drop_table($table);
04799             }
04800         }
04801 
04802         upgrade_main_savepoint(true, 2010071701);
04803     }
04804 
04805     if ($oldversion < 2010071800) {
04806 
04807         // Define table backup_files_template to be created
04808         $table = new xmldb_table('backup_files_template');
04809 
04810         // Adding fields to table backup_files_template
04811         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
04812         $table->add_field('backupid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
04813         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
04814         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
04815         $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
04816         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
04817         $table->add_field('info', XMLDB_TYPE_TEXT, 'medium', null, null, null, null);
04818 
04819         // Adding keys to table backup_files_template
04820         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
04821 
04822         // Adding indexes to table backup_files_template
04823         $table->add_index('backupid_contextid_component_filearea_itemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'contextid', 'component', 'filearea', 'itemid'));
04824 
04825         // Conditionally launch create table for backup_files_template
04826         if (!$dbman->table_exists($table)) {
04827             $dbman->create_table($table);
04828         }
04829 
04830         // Main savepoint reached
04831         upgrade_main_savepoint(true, 2010071800);
04832     }
04833 
04834     if ($oldversion < 2010072300) {
04835 
04836         // Define field capabilities to be added to external_functions
04837         $table = new xmldb_table('external_functions');
04838         $field = new xmldb_field('capabilities', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'component');
04839 
04840         // Conditionally launch add field capabilities
04841         if (!$dbman->field_exists($table, $field)) {
04842             $dbman->add_field($table, $field);
04843         }
04844 
04845         // Main savepoint reached
04846         upgrade_main_savepoint(true, 2010072300);
04847     }
04848 
04849     if ($oldversion < 2010072700) {
04850 
04851         // Define index backupid_itemname_newitemid_ix (not unique) to be added to backup_ids_template
04852         $table = new xmldb_table('backup_ids_template');
04853         $index = new xmldb_index('backupid_itemname_newitemid_ix', XMLDB_INDEX_NOTUNIQUE, array('backupid', 'itemname', 'newitemid'));
04854 
04855         // Conditionally launch add index backupid_itemname_newitemid_ix
04856         if (!$dbman->index_exists($table, $index)) {
04857             $dbman->add_index($table, $index);
04858         }
04859 
04860         // Main savepoint reached
04861         upgrade_main_savepoint(true, 2010072700);
04862     }
04863 
04864     if ($oldversion < 2010080303) {
04865         $rs = $DB->get_recordset_sql('SELECT i.id, i.name, r.type FROM {repository_instances} i, {repository} r WHERE i.typeid = r.id');
04866         foreach ($rs as $record) {
04867             upgrade_set_timeout();
04868             if ($record->name == $record->type) {
04869                 // repository_instances was saving type name as in name field
04870                 // which should be empty, the repository api will try to find
04871                 // instance name from language files
04872                 $DB->set_field('repository_instances', 'name', '');
04873             }
04874         }
04875         $rs->close();
04876         upgrade_main_savepoint(true, 2010080303);
04877     }
04878 
04879     if ($oldversion < 2010080305) {
04880         // first drop all log display actions, we will recreate them automatically later
04881         $DB->delete_records('log_display', array());
04882 
04883         // Define field component to be added to log_display
04884         $table = new xmldb_table('log_display');
04885         $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'field');
04886 
04887         // Launch add field component
04888         if (!$dbman->field_exists($table, $field)) {
04889             $dbman->add_field($table, $field);
04890         }
04891 
04892         // Main savepoint reached
04893         upgrade_main_savepoint(true, 2010080305);
04894     }
04895 
04896     if ($oldversion < 2010080900) {
04897 
04899         $table = new xmldb_table('question');
04900         $field = new xmldb_field('generalfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'generalfeedback');
04901 
04903         if (!$dbman->field_exists($table, $field)) {
04904             $dbman->add_field($table, $field);
04905         }
04906 
04913         $field = new xmldb_field('oldquestiontextformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'generalfeedback');
04914 
04916         if (!$dbman->field_exists($table, $field)) {
04917             $dbman->add_field($table, $field);
04918         }
04919 
04921         $table = new xmldb_table('question_categories');
04922         $field = new xmldb_field('infoformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'info');
04923 
04925         if (!$dbman->field_exists($table, $field)) {
04926             $dbman->add_field($table, $field);
04927         }
04928 
04930         $table = new xmldb_table('question_answers');
04931         $field = new xmldb_field('answerformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'answer');
04932 
04934         if (!$dbman->field_exists($table, $field)) {
04935             $dbman->add_field($table, $field);
04936         }
04937 
04939         $field = new xmldb_field('feedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'feedback');
04940 
04942         if (!$dbman->field_exists($table, $field)) {
04943             $dbman->add_field($table, $field);
04944         }
04945 
04947         $table = new xmldb_table('question_sessions');
04948         $field = new xmldb_field('manualcommentformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
04949 
04951         if (!$dbman->field_exists($table, $field)) {
04952             $dbman->add_field($table, $field);
04953         }
04954 
04956         upgrade_main_savepoint(true, 2010080900);
04957     }
04958 
04960     if ($oldversion < 2010080901) {
04961         $fs = get_file_storage();
04962 
04963         // Define field image to be dropped from question
04964         $table = new xmldb_table('question');
04965         $field = new xmldb_field('image');
04966 
04967         // Conditionally launch drop field image
04968         if ($dbman->field_exists($table, $field)) {
04969 
04970             $rs = $DB->get_recordset('question');
04971             $textlib = textlib_get_instance();
04972 
04973             foreach ($rs as $question) {
04974                 // may take awhile
04975                 upgrade_set_timeout();
04976                 if (empty($question->image)) {
04977                     continue;
04978                 }
04979                 if (!$category = $DB->get_record('question_categories', array('id'=>$question->category))) {
04980                     continue;
04981                 }
04982                 $categorycontext = get_context_instance_by_id($category->contextid);
04983                 // question files are stored in course level
04984                 // so we have to find course context
04985                 switch ($categorycontext->contextlevel){
04986                     case CONTEXT_COURSE :
04987                         $context = $categorycontext;
04988                         break;
04989                     case CONTEXT_MODULE :
04990                         $courseid = $DB->get_field('course_modules', 'course', array('id'=>$categorycontext->instanceid));
04991                         $context = get_context_instance(CONTEXT_COURSE, $courseid);
04992                         break;
04993                     case CONTEXT_COURSECAT :
04994                     case CONTEXT_SYSTEM :
04995                         $context = get_system_context();
04996                         break;
04997                     default :
04998                         continue;
04999                 }
05000                 if ($textlib->substr($textlib->strtolower($question->image), 0, 7) == 'http://') {
05001                     // it is a link, appending to existing question text
05002                     $question->questiontext .= ' <img src="' . $question->image . '" />';
05003                     $question->image = '';
05004                     // update question record
05005                     $DB->update_record('question', $question);
05006                 } else {
05007                     $filename = basename($question->image);
05008                     $filepath = dirname($question->image);
05009                     if (empty($filepath) or $filepath == '.' or $filepath == '/') {
05010                         $filepath = '/';
05011                     } else {
05012                         // append /
05013                         $filepath = '/'.trim($filepath, './@#$ ').'/';
05014                     }
05015 
05016                     // course files already moved to file pool by previous upgrade block
05017                     // so we just create copy from course_legacy area
05018                     if ($image = $fs->get_file($context->id, 'course', 'legacy', 0, $filepath, $filename)) {
05019                         // move files to file pool
05020                         $file_record = array(
05021                             'contextid'=>$category->contextid,
05022                             'component'=>'question',
05023                             'filearea'=>'questiontext',
05024                             'itemid'=>$question->id
05025                         );
05026                         $fs->create_file_from_storedfile($file_record, $image);
05027                         $question->questiontext .= ' <img src="@@PLUGINFILE@@' . $filepath . $filename . '" />';
05028                         $question->image = '';
05029                         // update question record
05030                         $DB->update_record('question', $question);
05031                     }
05032                 }
05033             }
05034             $rs->close();
05035 
05036             $dbman->drop_field($table, $field);
05037         }
05038 
05039         // Update question_answers.
05040         // In question_answers.feedback was previously always treated as
05041         // FORMAT_HTML in calculated, multianswer, multichoice, numerical,
05042         // shortanswer and truefalse; and
05043         // FORMAT_MOODLE in essay (despite being edited using the HTML editor)
05044         // So essay feedback needs to be converted to HTML unless $CFG->texteditors == 'textarea'.
05045         // For all question types except multichoice,
05046         // question_answers.answer is FORMAT_PLAIN and does not need to be changed.
05047         // For multichoice, question_answers.answer is FORMAT_MOODLE, and should
05048         // stay that way, at least for now.
05049         $rs = $DB->get_recordset_sql('
05050                 SELECT qa.*, q.qtype
05051                 FROM {question_answers} qa
05052                 JOIN {question} q ON qa.question = q.id');
05053         foreach ($rs as $record) {
05054             // may take awhile
05055             upgrade_set_timeout();
05056             // Convert question_answers.answer
05057             if ($record->qtype !== 'multichoice') {
05058                 $record->answerformat = FORMAT_PLAIN;
05059             } else {
05060                 $record->answerformat = FORMAT_MOODLE;
05061             }
05062 
05063             // Convert question_answers.feedback
05064             if ($CFG->texteditors !== 'textarea') {
05065                 if ($record->qtype == 'essay') {
05066                     $record->feedback = text_to_html($record->feedback, false, false, true);
05067                 }
05068                 $record->feedbackformat = FORMAT_HTML;
05069             } else {
05070                 $record->feedbackformat = FORMAT_MOODLE;
05071             }
05072 
05073             $DB->update_record('question_answers', $record);
05074         }
05075         $rs->close();
05076 
05077         // In the question table, the code previously used questiontextformat
05078         // for both question text and general feedback. We need to copy the
05079         // values into the new column.
05080         // Then we need to convert FORMAT_MOODLE to FORMAT_HTML (depending on
05081         // $CFG->texteditors).
05082         $DB->execute('
05083                 UPDATE {question}
05084                 SET generalfeedbackformat = questiontextformat');
05085         // Also save the old questiontextformat, so that plugins that need it
05086         // can access it.
05087         $DB->execute('
05088                 UPDATE {question}
05089                 SET oldquestiontextformat = questiontextformat');
05090         // Now covert FORMAT_MOODLE content, if necssary.
05091         if ($CFG->texteditors !== 'textarea') {
05092             $rs = $DB->get_recordset('question', array('questiontextformat'=>FORMAT_MOODLE));
05093             foreach ($rs as $record) {
05094                 // may take awhile
05095                 upgrade_set_timeout();
05096                 $record->questiontext = text_to_html($record->questiontext, false, false, true);
05097                 $record->questiontextformat = FORMAT_HTML;
05098                 $record->generalfeedback = text_to_html($record->generalfeedback, false, false, true);
05099                 $record->generalfeedbackformat = FORMAT_HTML;
05100                 $DB->update_record('question', $record);
05101             }
05102             $rs->close();
05103         }
05104 
05105         // In the past, question_sessions.manualcommentformat was always treated
05106         // as FORMAT_HTML.
05107         $DB->set_field('question_sessions', 'manualcommentformat', FORMAT_HTML);
05108 
05109         // Main savepoint reached
05110         upgrade_main_savepoint(true, 2010080901);
05111     }
05112 
05113     if ($oldversion < 2010082502) {
05114         // migrate file pool xx/xx/xx directory structure to xx/xx in older 2.0dev installs
05115         upgrade_simplify_overkill_pool_structure();
05116         upgrade_main_savepoint(true, 2010082502);
05117     }
05118 
05119     if ($oldversion < 2010091303) {
05120         // drop all test tables from old xmldb test suite
05121         $table = new xmldb_table('testtable');
05122         if ($dbman->table_exists($table)) {
05123             $dbman->drop_table($table);
05124         }
05125         $table = new xmldb_table('anothertest');
05126         if ($dbman->table_exists($table)) {
05127             $dbman->drop_table($table);
05128         }
05129         $table = new xmldb_table('newnameforthetable');
05130         if ($dbman->table_exists($table)) {
05131             $dbman->drop_table($table);
05132         }
05133         upgrade_main_savepoint(true, 2010091303);
05134     }
05135 
05136     if ($oldversion < 2010091500) {
05137 
05138         // Changing precision of field token on table registration_hubs to (255)
05139         $table = new xmldb_table('registration_hubs');
05140         $field = new xmldb_field('token', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'id');
05141 
05142         // Launch change of precision for field token
05143         $dbman->change_field_precision($table, $field);
05144 
05145         // Main savepoint reached
05146         upgrade_main_savepoint(true, 2010091500);
05147     }
05148 
05149     if ($oldversion < 2010091501) {
05150         // This index used to exist in Moodle 1.9 and was never dropped in the upgrade above.
05151         // Drop it now, or it breaks the following alter column.
05152 
05153         // Define index pagetypepattern (not unique) to be dropped form block_instances
05154         $table = new xmldb_table('block_instances');
05155         $index = new xmldb_index('pagetypepattern', XMLDB_INDEX_NOTUNIQUE, array('pagetypepattern'));
05156 
05157         // Conditionally launch drop index pagetypepattern
05158         if ($dbman->index_exists($table, $index)) {
05159             $dbman->drop_index($table, $index);
05160         }
05161 
05162         // Main savepoint reached
05163         upgrade_main_savepoint(true, 2010091501);
05164     }
05165 
05166     if ($oldversion < 2010091502) {
05167         // Need to drop the index before we can alter the column precision in the next step.
05168 
05169         // Define index parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern (not unique) to be dropped form block_instances
05170         $table = new xmldb_table('block_instances');
05171         $index = new xmldb_index('parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern', XMLDB_INDEX_NOTUNIQUE, array('parentcontextid', 'showinsubcontexts', 'pagetypepattern', 'subpagepattern'));
05172 
05173         // Conditionally launch drop index parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern
05174         if ($dbman->index_exists($table, $index)) {
05175             $dbman->drop_index($table, $index);
05176         }
05177 
05178         // Main savepoint reached
05179         upgrade_main_savepoint(true, 2010091502);
05180     }
05181 
05182     if ($oldversion < 2010091503) {
05183 
05184         // Changing precision of field pagetypepattern on table block_instances to (64)
05185         $table = new xmldb_table('block_instances');
05186         $field = new xmldb_field('pagetypepattern', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null, 'showinsubcontexts');
05187 
05188         // Launch change of precision for field pagetypepattern
05189         $dbman->change_field_precision($table, $field);
05190 
05191         // Main savepoint reached
05192         upgrade_main_savepoint(true, 2010091503);
05193     }
05194 
05195     if ($oldversion < 2010091504) {
05196         // Now add the index back.
05197 
05198         // Define index parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern (not unique) to be added to block_instances
05199         $table = new xmldb_table('block_instances');
05200         $index = new xmldb_index('parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern', XMLDB_INDEX_NOTUNIQUE, array('parentcontextid', 'showinsubcontexts', 'pagetypepattern', 'subpagepattern'));
05201 
05202         // Conditionally launch add index parentcontextid-showinsubcontexts-pagetypepattern-subpagepattern
05203         if (!$dbman->index_exists($table, $index)) {
05204             $dbman->add_index($table, $index);
05205         }
05206 
05207         // Main savepoint reached
05208         upgrade_main_savepoint(true, 2010091504);
05209     }
05210 
05211     if ($oldversion < 2010091505) {
05212         // drop all events queued from 1.9, unfortunately we can not process them because the serialisation of data changed
05213         // also the events format was changed....
05214         $DB->delete_records('events_queue_handlers', array());
05215         $DB->delete_records('events_queue', array());
05216 
05217         //reset all status fields too
05218         $DB->set_field('events_handlers', 'status', 0, array());
05219 
05220         upgrade_main_savepoint(true, 2010091505);
05221     }
05222 
05223     if ($oldversion < 2010091506) {
05224         // change component string in events_handlers records to new "_" format
05225         if ($handlers = $DB->get_records('events_handlers')) {
05226             foreach ($handlers as $handler) {
05227                 $handler->handlermodule = str_replace('/', '_', $handler->handlermodule);
05228                 $DB->update_record('events_handlers', $handler);
05229             }
05230         }
05231         unset($handlers);
05232         upgrade_main_savepoint(true, 2010091506);
05233     }
05234 
05235     if ($oldversion < 2010091507) {
05236 
05237         // Define index eventname-handlermodule (unique) to be dropped form events_handlers
05238         $table = new xmldb_table('events_handlers');
05239         $index = new xmldb_index('eventname-handlermodule', XMLDB_INDEX_UNIQUE, array('eventname', 'handlermodule'));
05240 
05241         // Conditionally launch drop index eventname-handlermodule
05242         if ($dbman->index_exists($table, $index)) {
05243             $dbman->drop_index($table, $index);
05244         }
05245 
05246         // Main savepoint reached
05247         upgrade_main_savepoint(true, 2010091507);
05248     }
05249 
05250     if ($oldversion < 2010091508) {
05251 
05252         // Rename field handlermodule on table events_handlers to component
05253         $table = new xmldb_table('events_handlers');
05254         $field = new xmldb_field('handlermodule', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null, 'eventname');
05255 
05256         // Launch rename field handlermodule
05257         $dbman->rename_field($table, $field, 'component');
05258 
05259         // Main savepoint reached
05260         upgrade_main_savepoint(true, 2010091508);
05261     }
05262 
05263     if ($oldversion < 2010091509) {
05264 
05265         // Define index eventname-component (unique) to be added to events_handlers
05266         $table = new xmldb_table('events_handlers');
05267         $index = new xmldb_index('eventname-component', XMLDB_INDEX_UNIQUE, array('eventname', 'component'));
05268 
05269         // Conditionally launch add index eventname-component
05270         if (!$dbman->index_exists($table, $index)) {
05271             $dbman->add_index($table, $index);
05272         }
05273 
05274         // Main savepoint reached
05275         upgrade_main_savepoint(true, 2010091509);
05276     }
05277 
05278     if ($oldversion < 2010091510) {
05279 
05280         // Define field internal to be added to events_handlers
05281         $table = new xmldb_table('events_handlers');
05282         $field = new xmldb_field('internal', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'status');
05283 
05284         // Conditionally launch add field internal
05285         if (!$dbman->field_exists($table, $field)) {
05286             $dbman->add_field($table, $field);
05287         }
05288 
05289         // Main savepoint reached
05290         upgrade_main_savepoint(true, 2010091510);
05291     }
05292 
05293     if ($oldversion < 2010091700) {
05294         // Fix MNet sso_jump_url for Moodle application
05295         $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php',
05296                        array('name' => 'moodle', 'sso_jump_url' => '/auth/mnet/land.php'));
05297         upgrade_main_savepoint(true, 2010091700);
05298     }
05299 
05300     if ($oldversion < 2010092000) {
05301         // drop multiple field again because it was still in install.xml in 2.0dev
05302 
05303         // Define field multiple to be dropped from block
05304         $table = new xmldb_table('block');
05305         $field = new xmldb_field('multiple');
05306 
05307         // Conditionally launch drop field multiple
05308         if ($dbman->field_exists($table, $field)) {
05309             $dbman->drop_field($table, $field);
05310         }
05311 
05312         // Main savepoint reached
05313         upgrade_main_savepoint(true, 2010092000);
05314     }
05315 
05316     if ($oldversion < 2010101300) {
05317         // Fix MDL-24641 : the registered language should not be empty otherwise cron will fail
05318         $registeredhubs = $DB->get_records('registration_hubs', array('confirmed' => 1));
05319         if (!empty($registeredhubs)) {
05320             foreach ($registeredhubs as $hub) {
05321                 $cleanhuburl = clean_param($hub->huburl, PARAM_ALPHANUMEXT);
05322                 $sitelanguage = get_config('hub', 'site_language_' . $cleanhuburl);
05323                 if (empty($sitelanguage)) {
05324                     set_config('site_language_' . $cleanhuburl, current_language(), 'hub');
05325                 }
05326             }
05327         }
05328         upgrade_main_savepoint(true, 2010101300);
05329     }
05330 
05331     //MDL-24721 -add hidden column to grade_categories. This was done previously but it wasn't included in
05332     //install.xml so there are 2.0 sites that are missing it.
05333     if ($oldversion < 2010101900) {
05334         $table = new xmldb_table('grade_categories');
05335         $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timemodified');
05336 
05337         if (!$dbman->field_exists($table, $field)) {
05338             $dbman->add_field($table, $field);
05339         }
05340 
05341         upgrade_main_savepoint(true, 2010101900);
05342     }
05343 
05344     // new format of the emoticons setting
05345     if ($oldversion < 2010102300) {
05346         unset($CFG->emoticons);
05347         $DB->delete_records('config', array('name' => 'emoticons'));
05348         $DB->delete_records('cache_text'); // changed md5 hash calculation
05349         upgrade_main_savepoint(true, 2010102300);
05350     }
05351 
05352     //MDL-24771
05353     if ($oldversion < 2010102601) {
05354 
05355         $fieldnotification = new xmldb_field('notification', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, 0, 'smallmessage');
05356         $fieldcontexturl = new xmldb_field('contexturl', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'notification');
05357         $fieldcontexturlname = new xmldb_field('contexturlname', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'contexturl');
05358         $fieldstoadd = array($fieldnotification, $fieldcontexturl, $fieldcontexturlname);
05359 
05360         $tablestomodify = array(new xmldb_table('message'), new xmldb_table('message_read'));
05361 
05362         foreach($tablestomodify as $table) {
05363             foreach($fieldstoadd as $field) {
05364                 if (!$dbman->field_exists($table, $field)) {
05365                     $dbman->add_field($table, $field);
05366                 }
05367             }
05368         }
05369 
05370         upgrade_main_savepoint(true, 2010102601);
05371     }
05372 
05373     // MDL-24694 needs increasing size of user_preferences.name(varchar[50]) field due to
05374     // long preferences names for messaging which need components parts within the name
05375     // eg: 'message_provider_mod_assignment_assignments_loggedin'
05376     if ($oldversion < 2010102602) {
05377 
05378         // Define index userid-name (unique) to be dropped form user_preferences
05379         $table = new xmldb_table('user_preferences');
05380         $index = new xmldb_index('userid-name', XMLDB_INDEX_UNIQUE, array('userid', 'name'));
05381 
05382         // Conditionally launch drop index userid-name
05383         if ($dbman->index_exists($table, $index)) {
05384             $dbman->drop_index($table, $index);
05385         }
05386 
05387         // Changing precision of field name on table user_preferences to (255)
05388         $field = new xmldb_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'userid');
05389 
05390         // Launch change of precision for field name
05391         $dbman->change_field_precision($table, $field);
05392 
05393         // Conditionally launch add index userid-name
05394         if (!$dbman->index_exists($table, $index)) {
05395             $dbman->add_index($table, $index);
05396         }
05397 
05398         // Main savepoint reached
05399         upgrade_main_savepoint(true, 2010102602);
05400     }
05401 
05402     if ($oldversion < 2010102700) {
05403 
05404         $table = new xmldb_table('post');
05405         $field = new xmldb_field('uniquehash', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'content');
05406         // Launch change of precision for field name
05407         $dbman->change_field_precision($table, $field);
05408 
05409         // Main savepoint reached
05410         upgrade_main_savepoint(true, 2010102700);
05411     }
05412 
05413     if ($oldversion < 2010110200) {
05414 
05415         // fix tags itemtype for wiki
05416         $sql = "UPDATE {tag_instance}
05417                 SET itemtype = 'wiki_pages'
05418                 WHERE itemtype = 'wiki_page'";
05419         $DB->execute($sql);
05420 
05421         echo $OUTPUT->notification('Updating tags itemtype', 'notifysuccess');
05422 
05423         // Main savepoint reached
05424         upgrade_main_savepoint(true, 2010110200);
05425     }
05426 
05427     //remove forum_logblocked from config. No longer required after user->emailstop was removed
05428     if ($oldversion < 2010110500) {
05429         unset_config('forum_logblocked');
05430         upgrade_main_savepoint(true, 2010110500);
05431     }
05432 
05433     if ($oldversion < 2010110800) {
05434         // convert $CFG->disablecourseajax to $CFG->enablecourseajax
05435         $disabledcourseajax = get_config('disablecourseajax', 0);
05436         if ($disabledcourseajax) {
05437             set_config('enablecourseajax', 0);
05438         } else {
05439             set_config('enablecourseajax', 1);
05440         }
05441         unset_config('disablecourseajax');
05442 
05443         upgrade_main_savepoint(true, 2010110800);
05444     }
05445 
05446     if ($oldversion < 2010111000) {
05447 
05448         // Clean up the old scheduled backup settings that are no longer relevant
05449         update_fix_automated_backup_config();
05450         upgrade_main_savepoint(true, 2010111000);
05451     }
05452 
05453     if ($oldversion < 2010111702) {
05454 
05455         // Clean up the old experimental split restore no loger used
05456         unset_config('experimentalsplitrestore');
05457 
05458         upgrade_main_savepoint(true, 2010111702);
05459     }
05460 
05461     if ($oldversion < 2010121401) {
05462 
05463         // Define table profiling to be created
05464         $table = new xmldb_table('profiling');
05465 
05466         // Adding fields to table profiling
05467         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
05468         $table->add_field('runid', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
05469         $table->add_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
05470         $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null);
05471         $table->add_field('totalexecutiontime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
05472         $table->add_field('totalcputime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
05473         $table->add_field('totalcalls', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
05474         $table->add_field('totalmemory', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
05475         $table->add_field('runreference', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
05476         $table->add_field('runcomment', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
05477         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
05478 
05479         // Adding keys to table profiling
05480         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
05481         $table->add_key('runid_uk', XMLDB_KEY_UNIQUE, array('runid'));
05482 
05483         // Adding indexes to table profiling
05484         $table->add_index('url_runreference_ix', XMLDB_INDEX_NOTUNIQUE, array('url', 'runreference'));
05485         $table->add_index('timecreated_runreference_ix', XMLDB_INDEX_NOTUNIQUE, array('timecreated', 'runreference'));
05486 
05487         // Conditionally launch create table for profiling
05488         if (!$dbman->table_exists($table)) {
05489             $dbman->create_table($table);
05490         }
05491 
05492         // Main savepoint reached
05493         upgrade_main_savepoint(true, 2010121401);
05494     }
05495 
05496     if ($oldversion < 2011011401) {
05497         $columns = $DB->get_columns('block_instances');
05498 
05499         // Check if we need to fix the default weight column
05500         if (array_key_exists('defaultweight', $columns) && $columns['defaultweight']->max_length != 10) {
05501             // Fix discrepancies in the block_instances table after upgrade from 1.9
05502             $table = new xmldb_table('block_instances');
05503 
05504             // defaultweight is smallint(3) after upgrade should be bigint 10
05505             // Also fixed in earlier upgrade code
05506             $field = new xmldb_field('defaultweight', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null, 'defaultregion');
05507             if ($dbman->field_exists($table, $field)) {
05508                 $dbman->change_field_type($table, $field);
05509             }
05510 
05511             // add missing key `blocinst_par_ix` (`parentcontextid`)
05512             $index = new xmldb_index('parentcontextid', XMLDB_INDEX_NOTUNIQUE, array('parentcontextid'));
05513             if (!$dbman->index_exists($table, $index)) {
05514                 $dbman->add_index($table, $index);
05515             }
05516         }
05517 
05518         // Main savepoint reached
05519         upgrade_main_savepoint(true, 2011011401);
05520     }
05521 
05522     if ($oldversion < 2011011402) {
05523         // Fix discrepancies in the block_positions table after upgrade from 1.9
05524         $table = new xmldb_table('block_positions');
05525         $columns = $DB->get_columns('block_positions');
05526 
05527         // Check if we need to fix the blockinstanceid field
05528         if (array_key_exists('blockinstanceid', $columns) && empty($columns['blockinstanceid']->unsigned)) {
05529             // Fix blockinstanceid
05530             // First remove the indexs on the field
05531             $indexone = new xmldb_index('blockinstanceid', XMLDB_INDEX_NOTUNIQUE, array('blockinstanceid'));
05532             $indexall = new xmldb_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid','contextid','pagetype','subpage'));
05533             if ($dbman->index_exists($table, $indexone)) {
05534                 $dbman->drop_index($table, $indexone);
05535             }
05536             if ($dbman->index_exists($table, $indexall)) {
05537                 $dbman->drop_index($table, $indexall);
05538             }
05539             // blockinstanceid should be unsigned
05540             // Also fixed in earlier upgrade code
05541             $field = new xmldb_field('blockinstanceid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'id');
05542             if ($dbman->field_exists($table, $field)) {
05543                 $dbman->change_field_unsigned($table, $field);
05544             }
05545 
05546             // Add the indexs back in
05547             $dbman->add_index($table, $indexone);
05548             $dbman->add_index($table, $indexall);
05549         }
05550 
05551         // Check if the visible field needs fixing.
05552         if (array_key_exists('visible', $columns) && !empty($columns['visible']->has_default)) {
05553             // visible shouldn't have a default
05554             // Also fixed in earlier upgrade code
05555             $field = new xmldb_field('visible', XMLDB_TYPE_INTEGER, 4, null, XMLDB_NOTNULL, null, null, 'subpage');
05556             if ($dbman->field_exists($table, $field)) {
05557                 $dbman->change_field_default($table, $field);
05558             }
05559         }
05560 
05561         // Main savepoint reached
05562         upgrade_main_savepoint(true, 2011011402);
05563     }
05564 
05565     if ($oldversion < 2011011403) {
05566         $columns = $DB->get_columns('grade_categories');
05567         // Check if we need to fix the hidden field
05568         if (array_key_exists('hidden', $columns) && $columns['hidden']->max_length != 1) {
05569             // Fix discrepancies in the grade_categories table after upgrade from 1.9
05570             $table = new xmldb_table('grade_categories');
05571 
05572             // hidden should be tinyint(1)
05573             // Also fixed in earlier upgrade code
05574             $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timemodified');
05575             if ($dbman->field_exists($table, $field)) {
05576                 $dbman->change_field_precision($table, $field);
05577             }
05578         }
05579 
05580         // Main savepoint reached
05581         upgrade_main_savepoint(true, 2011011403);
05582     }
05583 
05584     if ($oldversion < 2011011404) {
05585         // Fix discrepancies in the message table after upgrade from 1.9
05586         $columns = $DB->get_columns('message');
05587         $table = new xmldb_table('message');
05588 
05589         // Check if we need to fix the useridfrom field
05590         if (array_key_exists('useridfrom', $columns) && empty($columns['useridfrom']->unsigned)) {
05591             // useridfrom should be unsigned
05592             $field = new xmldb_field('useridfrom', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'id');
05593             $index = new xmldb_index('useridfrom', XMLDB_INDEX_NOTUNIQUE, array('useridfrom'));
05594             if ($dbman->index_exists($table, $index)) {
05595                 $dbman->drop_index($table, $index);
05596             }
05597             if ($dbman->field_exists($table, $field)) {
05598                 $dbman->change_field_unsigned($table, $field);
05599             }
05600             $dbman->add_index($table, $index);
05601         }
05602 
05603         // Check if we need to fix the useridto field
05604         if (array_key_exists('useridto', $columns) && empty($columns['useridto']->unsigned)) {
05605             // useridto should be unsigned
05606             $field = new xmldb_field('useridto', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'useridfrom');
05607             $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
05608             if ($dbman->index_exists($table, $index)) {
05609                 $dbman->drop_index($table, $index);
05610             }
05611             if ($dbman->field_exists($table, $field)) {
05612                 $dbman->change_field_unsigned($table, $field);
05613             }
05614             $dbman->add_index($table, $index);
05615         }
05616 
05617         // Check if we need to fix the notification field
05618         if (array_key_exists('notification', $columns) && !empty($columns['notification']->not_null)) {
05619             // notification should allow null
05620             // Fixed in earlier upgrade code
05621             $field = new xmldb_field('notification', XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, null, null, 0, 'smallmessage');
05622             if ($dbman->field_exists($table, $field)) {
05623                 $dbman->change_field_notnull($table, $field);
05624             }
05625         }
05626 
05627         // Check if we need to fix the contexturl field
05628         if (array_key_exists('contexturl', $columns) && strpos($columns['contexturl']->type, 'text') === false) {
05629             // contexturl should be text
05630             // Fixed in earlier upgrade code
05631             $field = new xmldb_field('contexturl', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'notification');
05632             if ($dbman->field_exists($table, $field)) {
05633                 $dbman->change_field_type($table, $field);
05634             }
05635         }
05636 
05637         // Check if we need to fix the contexturl field
05638         if (array_key_exists('contexturlname', $columns) && strpos($columns['contexturlname']->type, 'text') === false) {
05639             // contexturlname should be text
05640             // Fixed in earlier upgrade code
05641             $field = new xmldb_field('contexturlname', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'contexturl');
05642             if ($dbman->field_exists($table, $field)) {
05643                 $dbman->change_field_type($table, $field);
05644             }
05645         }
05646 
05647         // Main savepoint reached
05648         upgrade_main_savepoint(true, 2011011404);
05649     }
05650 
05651     if ($oldversion < 2011011405) {
05652         // Fix discrepancies in the message_read table after upgrade from 1.9
05653         $columns = $DB->get_columns('message_read');
05654         $table = new xmldb_table('message_read');
05655 
05656         // Check if we need to fix the useridfrom field
05657         if (array_key_exists('useridfrom', $columns) && empty($columns['useridfrom']->unsigned)) {
05658             // useridfrom should be unsigned
05659             $field = new xmldb_field('useridfrom', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'id');
05660             $index = new xmldb_index('useridfrom', XMLDB_INDEX_NOTUNIQUE, array('useridfrom'));
05661             if ($dbman->index_exists($table, $index)) {
05662                 $dbman->drop_index($table, $index);
05663             }
05664             if ($dbman->field_exists($table, $field)) {
05665                 $dbman->change_field_unsigned($table, $field);
05666             }
05667             $dbman->add_index($table, $index);
05668         }
05669 
05670         // Check if we need to fix the useridto field
05671         if (array_key_exists('useridto', $columns) && empty($columns['useridto']->unsigned)) {
05672             // useridto should be unsigned
05673             $field = new xmldb_field('useridto', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'useridfrom');
05674             $index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));
05675             if ($dbman->index_exists($table, $index)) {
05676                 $dbman->drop_index($table, $index);
05677             }
05678             if ($dbman->field_exists($table, $field)) {
05679                 $dbman->change_field_unsigned($table, $field);
05680             }
05681             $dbman->add_index($table, $index);
05682         }
05683 
05684         // Check if we need to fix the notification field
05685         if (array_key_exists('notification', $columns) && !empty($columns['notification']->not_null)) {
05686             // notification should allow null
05687             // Fixed in earlier upgrade code
05688             $field = new xmldb_field('notification', XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, null, null, 0, 'smallmessage');
05689             if ($dbman->field_exists($table, $field)) {
05690                 $dbman->change_field_notnull($table, $field);
05691             }
05692         }
05693 
05694         // Check if we need to fix the contexturl field
05695         if (array_key_exists('contexturl', $columns) && strpos($columns['contexturl']->type, 'text') === false) {
05696             // contexturl should be text
05697             // Fixed in earlier upgrade code
05698             $field = new xmldb_field('contexturl', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'notification');
05699             if ($dbman->field_exists($table, $field)) {
05700                 $dbman->change_field_type($table, $field);
05701             }
05702         }
05703 
05704         // Check if we need to fix the contexturl field
05705         if (array_key_exists('contexturlname', $columns) && strpos($columns['contexturlname']->type, 'text') === false) {
05706             // contexturlname should be text
05707             // Fixed in earlier upgrade code
05708             $field = new xmldb_field('contexturlname', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'contexturl');
05709             if ($dbman->field_exists($table, $field)) {
05710                 $dbman->change_field_type($table, $field);
05711             }
05712         }
05713 
05714         // Main savepoint reached
05715         upgrade_main_savepoint(true, 2011011405);
05716     }
05717 
05718     if ($oldversion < 2011011406) {
05719         // Fix discrepancies in the my_pages table after upgrade from 1.9
05720         $columns = $DB->get_columns('my_pages');
05721         $table = new xmldb_table('my_pages');
05722 
05723         // Check if we need to fix the private column
05724         if (array_key_exists('private', $columns) && $columns['private']->default_value != '1') {
05725             // private should be default 1
05726             // Fixed in earlier upgrade code
05727             $field = new xmldb_field('private', XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 1, 'name');
05728             $index = new xmldb_index('user_idx', XMLDB_INDEX_NOTUNIQUE, array('userid','private'));
05729             if ($dbman->index_exists($table, $index)) {
05730                 $dbman->drop_index($table, $index);
05731             }
05732             if ($dbman->field_exists($table, $field)) {
05733                 $dbman->change_field_default($table, $field);
05734             }
05735             $dbman->add_index($table, $index);
05736         }
05737 
05738         // Check if we need to fix the sortorder field
05739         if (array_key_exists('sortorder', $columns) && !empty($columns['sortorder']->unsigned)) {
05740             // Sortorder should not be unsigned
05741             // Fixed in earlier upgrade code
05742             $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, 6, null, XMLDB_NOTNULL, null, 0, 'private');
05743             if ($dbman->field_exists($table, $field)) {
05744                 $dbman->change_field_notnull($table, $field);
05745             }
05746         }
05747 
05748         upgrade_main_savepoint(true, 2011011406);
05749     }
05750 
05751     if ($oldversion < 2011011408) {
05752         // Fix question in the post table after upgrade from 1.9
05753         $columns = $DB->get_columns('question');
05754         $table = new xmldb_table('question');
05755 
05756         // Check if we need to fix default grade
05757         if (array_key_exists('defaultgrade', $columns) && (
05758                 empty($columns['defaultgrade']->unsigned) ||
05759                 empty($columns['defaultgrade']->not_null) ||
05760                 $columns['defaultgrade']->default_value !== '1.0000000')) {
05761             // defaultgrade should be unsigned NOT NULL DEFAULT '1.0000000'
05762             // Fixed in earlier upgrade code
05763             $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1.0000000', 'generalfeedbackformat');
05764             if ($dbman->field_exists($table, $field)) {
05765                 $dbman->change_field_default($table, $field);
05766             }
05767         }
05768 
05769         // Check if we need to fix penalty
05770         if (array_key_exists('penalty', $columns) && (empty($columns['penalty']->not_null) || $columns['penalty']->default_value !== '0.1000000')) {
05771             // penalty should be NOT NULL DEFAULT '0.1000000'
05772             // Fixed in earlier upgrade code
05773             $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0.1000000', 'defaultgrade');
05774             if ($dbman->field_exists($table, $field)) {
05775                 $dbman->change_field_default($table, $field);
05776             }
05777         }
05778 
05779         upgrade_main_savepoint(true, 2011011408);
05780     }
05781 
05782     if ($oldversion < 2011011409) {
05783         // Fix question_answers in the post table after upgrade from 1.9
05784         $columns = $DB->get_columns('question_answers');
05785         $table = new xmldb_table('question_answers');
05786 
05787         if (array_key_exists('fraction', $columns) && empty($columns['fraction']->not_null)) {
05788             // fraction should be NOT NULL DEFAULT '0.0000000',
05789             // Fixed in earlier upgrade code
05790             $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'feedback');
05791             if ($dbman->field_exists($table, $field)) {
05792                 $dbman->change_field_default($table, $field);
05793             }
05794         }
05795 
05796         upgrade_main_savepoint(true, 2011011409);
05797     }
05798 
05799     if ($oldversion < 2011011410) {
05800         // Fix question_sessions in the post table after upgrade from 1.9
05801         $columns = $DB->get_columns('question_sessions');
05802         $table = new xmldb_table('question_sessions');
05803 
05804         // Check if we need to fix sumpenalty
05805         if (array_key_exists('sumpenalty', $columns) && empty($columns['sumpenalty']->not_null)) {
05806             // sumpenalty should be NOT NULL DEFAULT '0.0000000',
05807             // Fixed in earlier upgrade code
05808             $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'newgraded');
05809             if ($dbman->field_exists($table, $field)) {
05810                 $dbman->change_field_default($table, $field);
05811             }
05812         }
05813 
05814         upgrade_main_savepoint(true, 2011011410);
05815     }
05816 
05817     if ($oldversion < 2011011411) {
05818         // Fix question_states in the post table after upgrade from 1.9
05819         $columns = $DB->get_columns('question_states');
05820         $table = new xmldb_table('question_states');
05821 
05822         // Check if we need to fix grade
05823         if (array_key_exists('grade', $columns) && empty($columns['grade']->not_null)) {
05824             // grade should be NOT NULL DEFAULT '0.0000000',
05825             // Fixed in earlier upgrade code
05826             $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'event');
05827             if ($dbman->field_exists($table, $field)) {
05828                 $dbman->change_field_default($table, $field);
05829             }
05830         }
05831 
05832         // Check if we need to fix raw_grade
05833         if (array_key_exists('raw_grade', $columns) && empty($columns['raw_grade']->not_null)) {
05834             // raw_grade should be NOT NULL DEFAULT '0.0000000',
05835             // Fixed in earlier upgrade code
05836             $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'grade');
05837             if ($dbman->field_exists($table, $field)) {
05838                 $dbman->change_field_default($table, $field);
05839             }
05840         }
05841 
05842         // Check if we need to fix raw_grade
05843         if (array_key_exists('penalty', $columns) && empty($columns['penalty']->not_null)) {
05844             // penalty should be NOT NULL DEFAULT '0.0000000',
05845             // Fixed in earlier upgrade code
05846             $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '0', 'raw_grade');
05847             if ($dbman->field_exists($table, $field)) {
05848                 $dbman->change_field_default($table, $field);
05849             }
05850         }
05851 
05852         upgrade_main_savepoint(true, 2011011411);
05853     }
05854 
05855     if ($oldversion < 2011011412) {
05856         // Fix tag_instance in the post table after upgrade from 1.9
05857         $columns = $DB->get_columns('tag_instance');
05858         $table = new xmldb_table('tag_instance');
05859 
05860         // Check if we need to fix tiuserid
05861         if (array_key_exists('tiuserid', $columns) && !empty($columns['tiuserid']->has_default)) {
05862             // tiuserid should have no default
05863             // Fixed in earlier upgrade code
05864             $field = new xmldb_field('tiuserid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
05865             $index = new xmldb_index('itemtype-itemid-tagid-tiuserid', XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
05866             if ($dbman->index_exists($table, $index)) {
05867                 $dbman->drop_index($table, $index);
05868             }
05869             if ($dbman->field_exists($table, $field)) {
05870                 $dbman->change_field_default($table, $field);
05871             }
05872             $dbman->add_index($table, $index);
05873         }
05874 
05875         upgrade_main_savepoint(true, 2011011412);
05876     }
05877 
05878     if ($oldversion < 2011011413) {
05879         // Fix user_info_field in the post table after upgrade from 1.9
05880         $table = new xmldb_table('user_info_field');
05881 
05882         // Missing field descriptionformat
05883         // Fixed in earlier upgrade code
05884         $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, 2, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'description');
05885         if (!$dbman->field_exists($table, $field)) {
05886             $dbman->add_field($table, $field);
05887         }
05888 
05889         upgrade_main_savepoint(true, 2011011413);
05890     }
05891 
05892     if ($oldversion < 2011011414) {
05893         // Drop the adodb_logsql table if it exists... it was never actually used anyway.
05894         $table = new xmldb_table('adodb_logsql');
05895 
05896         if ($dbman->table_exists($table)) {
05897             $dbman->drop_table($table);
05898         }
05899 
05900         upgrade_main_savepoint(true, 2011011414);
05901     }
05902 
05903     if ($oldversion < 2011011415) {
05904         //create the rating table indexes if required
05905         $table = new xmldb_table('rating');
05906 
05907         $index = new xmldb_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
05908         if (!$dbman->index_exists($table, $index)) {
05909             $dbman->add_index($table, $index);
05910 
05911             $key = new xmldb_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
05912             $dbman->add_key($table, $key);
05913 
05914             $key = new xmldb_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
05915             $dbman->add_key($table, $key);
05916         }
05917 
05918         upgrade_main_savepoint(true, 2011011415);
05919     }
05920 
05921     if ($oldversion < 2011012400) {
05922         // Clean up the old progress tracked roles setting, no longer used (replaced by enrolment)
05923         unset_config('progresstrackedroles');
05924         upgrade_main_savepoint(true, 2011012400);
05925     }
05926 
05927     if ($oldversion < 2011012500) {
05928         $columns = $DB->get_columns('tag_instance');
05929         $table = new xmldb_table('tag_instance');
05930 
05931         // Drop and recreate index if tiuserid doesn't have default value
05932         if (array_key_exists('tiuserid', $columns) && empty($columns['tiuserid']->has_default)) {
05933             // Define index itemtype-itemid-tagid-tiuserid (unique) to be dropped form tag_instance
05934             $index = new xmldb_index('itemtype-itemid-tagid-tiuserid', XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
05935             // Conditionally launch drop index itemtype-itemid-tagid-tiuserid
05936             if ($dbman->index_exists($table, $index)) {
05937                 $dbman->drop_index($table, $index);
05938             }
05939 
05940             // Changing the default of field tiuserid on table tag_instance to 0
05941             $field = new xmldb_field('tiuserid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'itemid');
05942 
05943             // Launch change of default for field tiuserid
05944             $dbman->change_field_default($table, $field);
05945 
05946             $index = new xmldb_index('itemtype-itemid-tagid-tiuserid', XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
05947 
05948             // Conditionally launch add index itemtype-itemid-tagid-tiuserid
05949             if (!$dbman->index_exists($table, $index)) {
05950                 $dbman->add_index($table, $index);
05951             }
05952         }
05953 
05954         // Main savepoint reached
05955         upgrade_main_savepoint(true, 2011012500);
05956     }
05957 
05958     if ($oldversion < 2011012501) {
05959         //add the index userfieldidx (not unique) to user_info_data
05960         $table = new xmldb_table('user_info_data');
05961         $index = new xmldb_index('userfieldidx', XMLDB_INDEX_NOTUNIQUE, array('userid', 'fieldid'));
05962 
05963         if (!$dbman->index_exists($table, $index)) {
05964             $dbman->add_index($table, $index);
05965         }
05966 
05967         upgrade_main_savepoint(true, 2011012501);
05968     }
05969 
05970     if ($oldversion < 2011020200.01) {
05971 
05972         // Define field targetversion to be added to upgrade_log
05973         $table = new xmldb_table('upgrade_log');
05974         $field = new xmldb_field('targetversion', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'version');
05975 
05976         // Conditionally launch add field targetversion
05977         if (!$dbman->field_exists($table, $field)) {
05978             $dbman->add_field($table, $field);
05979         }
05980 
05981         // Main savepoint reached
05982         upgrade_main_savepoint(true, 2011020200.01);
05983     }
05984 
05985     if ($oldversion < 2011020900.07) {
05986         $DB->delete_records('course_display', array('display' => 0));
05987         upgrade_main_savepoint(true, 2011020900.07);
05988     }
05989 
05990     if ($oldversion < 2011020900.08) {
05991          // Define field secret to be added to registration_hubs
05992         $table = new xmldb_table('registration_hubs');
05993         $field = new xmldb_field('secret', XMLDB_TYPE_CHAR, '255', null, null, null,
05994                 null, 'confirmed');
05995 
05996         // Conditionally launch add field secret and set its value
05997         if (!$dbman->field_exists($table, $field)) {
05998             $dbman->add_field($table, $field);
05999             $DB->set_field('registration_hubs', 'secret', $CFG->siteidentifier);
06000         }
06001 
06002         // Main savepoint reached
06003         upgrade_main_savepoint(true, 2011020900.08);
06004     }
06005 
06006     if ($oldversion < 2011022100.01) {
06007         // hack alert: inject missing version of manual auth_plugin,
06008         //             we need to do it so that we may use upgrade.php there
06009 
06010         set_config('version', 2011022100, 'auth_manual');
06011         upgrade_main_savepoint(true, 2011022100.01);
06012     }
06013 
06014     if ($oldversion < 2011052300.00) {
06015         $table = new xmldb_table('rating');
06016 
06017         // Add the component field to the ratings table
06018         upgrade_set_timeout(60 * 20);
06019         $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, 'unknown', 'contextid');
06020         if (!$dbman->field_exists($table, $field)) {
06021             $dbman->add_field($table, $field);
06022         }
06023 
06024         // Add the ratingarea field to the ratings table
06025         upgrade_set_timeout(60 * 20);
06026         $field = new xmldb_field('ratingarea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, 'unknown', 'component');
06027         if (!$dbman->field_exists($table, $field)) {
06028             $dbman->add_field($table, $field);
06029         }
06030 
06031         upgrade_main_savepoint(true, 2011052300.00);
06032     }
06033 
06034     if ($oldversion < 2011052300.01) {
06035 
06036         // Define index uniqueuserrating (unique) to be added to rating
06037         $table = new xmldb_table('rating');
06038         $index = new xmldb_index('uniqueuserrating', XMLDB_INDEX_NOTUNIQUE, array('component', 'ratingarea', 'contextid', 'itemid'));
06039 
06040         // Conditionally launch add index uniqueuserrating
06041         if (!$dbman->index_exists($table, $index)) {
06042             $dbman->add_index($table, $index);
06043         }
06044 
06045         // Main savepoint reached
06046         upgrade_main_savepoint(true, 2011052300.01);
06047     }
06048 
06049     if ($oldversion < 2011052300.02) {
06050 
06051         // Define index itemid (not unique) to be dropped form rating
06052         $table = new xmldb_table('rating');
06053         $index = new xmldb_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
06054 
06055         // Conditionally launch drop index itemid
06056         if ($dbman->index_exists($table, $index)) {
06057             $dbman->drop_index($table, $index);
06058         }
06059 
06060         // Main savepoint reached
06061         upgrade_main_savepoint(true, 2011052300.02);
06062     }
06063 
06064     // Question engine 2 changes (14) start here
06065     if ($oldversion < 2011060300) {
06066         // Changing the default of field penalty on table question to 0.3333333
06067         $table = new xmldb_table('question');
06068         $field = new xmldb_field('penalty');
06069         $field->set_attributes(XMLDB_TYPE_NUMBER, '12, 7', null,
06070                 XMLDB_NOTNULL, null, '0.3333333');
06071 
06072         // Launch change of default for field penalty
06073         $dbman->change_field_default($table, $field);
06074 
06075         // quiz savepoint reached
06076         upgrade_main_savepoint(true, 2011060300);
06077     }
06078 
06079     if ($oldversion < 2011060301) {
06080 
06081         // Rename field defaultgrade on table question to defaultmark
06082         $table = new xmldb_table('question');
06083         $field = new xmldb_field('defaultgrade');
06084         $field->set_attributes(XMLDB_TYPE_NUMBER, '12, 7', null,
06085                 XMLDB_NOTNULL, null, '1');
06086 
06087         // Launch rename field defaultmark
06088         if ($dbman->field_exists($table, $field)) {
06089             $dbman->rename_field($table, $field, 'defaultmark');
06090         }
06091 
06092         // quiz savepoint reached
06093         upgrade_main_savepoint(true, 2011060301);
06094     }
06095 
06096     if ($oldversion < 2011060302) {
06097 
06098         // Rename the question_attempts table to question_usages.
06099         $table = new xmldb_table('question_attempts');
06100         if (!$dbman->table_exists('question_usages')) {
06101             $dbman->rename_table($table, 'question_usages');
06102         }
06103 
06104         // quiz savepoint reached
06105         upgrade_main_savepoint(true, 2011060302);
06106     }
06107 
06108     if ($oldversion < 2011060303) {
06109 
06110         // Rename the modulename field to component ...
06111         $table = new xmldb_table('question_usages');
06112         $field = new xmldb_field('modulename');
06113         $field->set_attributes(XMLDB_TYPE_CHAR, '255', null,
06114                 XMLDB_NOTNULL, null, null, 'contextid');
06115 
06116         if ($dbman->field_exists($table, $field)) {
06117             $dbman->rename_field($table, $field, 'component');
06118         }
06119 
06120         // ... and update its contents.
06121         $DB->set_field('question_usages', 'component', 'mod_quiz', array('component' => 'quiz'));
06122 
06123         // Add the contextid field.
06124         $field = new xmldb_field('contextid');
06125         $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06126                 null, null, null, 'id');
06127         if (!$dbman->field_exists($table, $field)) {
06128             $dbman->add_field($table, $field);
06129 
06130             // And populate it.
06131             $quizmoduleid = $DB->get_field('modules', 'id', array('name' => 'quiz'));
06132             $DB->execute("
06133                 UPDATE {question_usages} SET contextid = (
06134                     SELECT ctx.id
06135                     FROM {context} ctx
06136                     JOIN {course_modules} cm ON cm.id = ctx.instanceid AND cm.module = $quizmoduleid
06137                     JOIN {quiz_attempts} quiza ON quiza.quiz = cm.instance
06138                     WHERE ctx.contextlevel = " . CONTEXT_MODULE . "
06139                     AND quiza.uniqueid = {question_usages}.id
06140                 )
06141             ");
06142 
06143             // It seems that it is possible, in old versions of Moodle, for a
06144             // quiz_attempt to be deleted while the question_attempt remains.
06145             // In that situation we still get NULLs left in the table, which
06146             // causes the upgrade to break at the next step. To avoid breakage,
06147             // without risking dataloss, we just replace all NULLs with 0 here.
06148             $DB->set_field_select('question_usages', 'contextid', 0, 'contextid IS NULL');
06149 
06150             // Then make it NOT NULL.
06151             $field = new xmldb_field('contextid');
06152             $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06153                     XMLDB_NOTNULL, null, null, 'id');
06154             $dbman->change_field_notnull($table, $field);
06155         }
06156 
06157         // Add the preferredbehaviour column. Populate it with a dummy value
06158         // for now. We will fill in the appropriate behaviour name when
06159         // updating all the rest of the attempt data.
06160         $field = new xmldb_field('preferredbehaviour');
06161         if (!$dbman->field_exists($table, $field)) {
06162             $field->set_attributes(XMLDB_TYPE_CHAR, '32', null,
06163                     XMLDB_NOTNULL, null, 'to_be_set_later', 'component');
06164             $dbman->add_field($table, $field);
06165 
06166             // Then remove the default value, now the column is populated.
06167             $field = new xmldb_field('preferredbehaviour');
06168             $field->set_attributes(XMLDB_TYPE_CHAR, '32', null,
06169                     XMLDB_NOTNULL, null, null, 'component');
06170             $dbman->change_field_default($table, $field);
06171         }
06172 
06173         // quiz savepoint reached
06174         upgrade_main_savepoint(true, 2011060303);
06175     }
06176 
06177     if ($oldversion < 2011060304) {
06178 
06179         // Define key contextid (foreign) to be added to question_usages
06180         $table = new xmldb_table('question_usages');
06181         $key = new XMLDBKey('contextid');
06182         $key->set_attributes(XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
06183 
06184         // Launch add key contextid
06185         $dbman->add_key($table, $key);
06186 
06187         // quiz savepoint reached
06188         upgrade_main_savepoint(true, 2011060304);
06189     }
06190 
06191     if ($oldversion < 2011060305) {
06192 
06193         // Changing precision of field component on table question_usages to (255)
06194         // This was missed during the upgrade from old versions.
06195         $table = new xmldb_table('question_usages');
06196         $field = new xmldb_field('component');
06197         $field->set_attributes(XMLDB_TYPE_CHAR, '255', null,
06198                 XMLDB_NOTNULL, null, null, 'contextid');
06199 
06200         // Launch change of precision for field component
06201         $dbman->change_field_precision($table, $field);
06202 
06203         // quiz savepoint reached
06204         upgrade_main_savepoint(true, 2011060305);
06205     }
06206 
06207     if ($oldversion < 2011060306) {
06208 
06209         // Define table question_attempts to be created
06210         $table = new xmldb_table('question_attempts');
06211         if (!$dbman->table_exists($table)) {
06212 
06213             // Adding fields to table question_attempts
06214             $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06215                     XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
06216             $table->add_field('questionusageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06217                     XMLDB_NOTNULL, null, null);
06218             $table->add_field('slot', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06219                     XMLDB_NOTNULL, null, null);
06220             $table->add_field('behaviour', XMLDB_TYPE_CHAR, '32', null,
06221                     XMLDB_NOTNULL, null, null);
06222             $table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06223                     XMLDB_NOTNULL, null, null);
06224             $table->add_field('maxmark', XMLDB_TYPE_NUMBER, '12, 7', null,
06225                     XMLDB_NOTNULL, null, null);
06226             $table->add_field('minfraction', XMLDB_TYPE_NUMBER, '12, 7', null,
06227                     XMLDB_NOTNULL, null, null);
06228             $table->add_field('flagged', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
06229                     XMLDB_NOTNULL, null, '0');
06230             $table->add_field('questionsummary', XMLDB_TYPE_TEXT, 'small', null,
06231                     null, null, null);
06232             $table->add_field('rightanswer', XMLDB_TYPE_TEXT, 'small', null,
06233                     null, null, null);
06234             $table->add_field('responsesummary', XMLDB_TYPE_TEXT, 'small', null,
06235                     null, null, null);
06236             $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06237                     XMLDB_NOTNULL, null, null);
06238 
06239             // Adding keys to table question_attempts
06240             $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
06241             $table->add_key('questionid', XMLDB_KEY_FOREIGN, array('questionid'),
06242                     'question', array('id'));
06243             $table->add_key('questionusageid', XMLDB_KEY_FOREIGN, array('questionusageid'),
06244                     'question_usages', array('id'));
06245 
06246             // Adding indexes to table question_attempts
06247             $table->add_index('questionusageid-slot', XMLDB_INDEX_UNIQUE,
06248                     array('questionusageid', 'slot'));
06249 
06250             // Launch create table for question_attempts
06251             $dbman->create_table($table);
06252         }
06253 
06254         // quiz savepoint reached
06255         upgrade_main_savepoint(true, 2011060306);
06256     }
06257 
06258     if ($oldversion < 2011060307) {
06259 
06260         // Define table question_attempt_steps to be created
06261         $table = new xmldb_table('question_attempt_steps');
06262         if (!$dbman->table_exists($table)) {
06263 
06264             // Adding fields to table question_attempt_steps
06265             $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06266                     XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
06267             $table->add_field('questionattemptid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06268                     XMLDB_NOTNULL, null, null);
06269             $table->add_field('sequencenumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06270                     XMLDB_NOTNULL, null, null);
06271             $table->add_field('state', XMLDB_TYPE_CHAR, '13', null,
06272                     XMLDB_NOTNULL, null, null);
06273             $table->add_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null,
06274                     null, null, null);
06275             $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06276                     XMLDB_NOTNULL, null, null);
06277             $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06278                     null, null, null);
06279 
06280             // Adding keys to table question_attempt_steps
06281             $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
06282             $table->add_key('questionattemptid', XMLDB_KEY_FOREIGN,
06283                     array('questionattemptid'), 'question_attempts_new', array('id'));
06284             $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'),
06285                     'user', array('id'));
06286 
06287             // Adding indexes to table question_attempt_steps
06288             $table->add_index('questionattemptid-sequencenumber', XMLDB_INDEX_UNIQUE,
06289                     array('questionattemptid', 'sequencenumber'));
06290 
06291             // Launch create table for question_attempt_steps
06292             $dbman->create_table($table);
06293         }
06294 
06295         // quiz savepoint reached
06296         upgrade_main_savepoint(true, 2011060307);
06297     }
06298 
06299     if ($oldversion < 2011060308) {
06300 
06301         // Define table question_attempt_step_data to be created
06302         $table = new xmldb_table('question_attempt_step_data');
06303         if (!$dbman->table_exists($table)) {
06304 
06305             // Adding fields to table question_attempt_step_data
06306             $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06307                     XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
06308             $table->add_field('attemptstepid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06309                     XMLDB_NOTNULL, null, null);
06310             $table->add_field('name', XMLDB_TYPE_CHAR, '32', null,
06311                     XMLDB_NOTNULL, null, null);
06312             $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null,
06313                     null, null, null);
06314 
06315             // Adding keys to table question_attempt_step_data
06316             $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
06317             $table->add_key('attemptstepid', XMLDB_KEY_FOREIGN, array('attemptstepid'),
06318                     'question_attempt_steps', array('id'));
06319 
06320             // Adding indexes to table question_attempt_step_data
06321             $table->add_index('attemptstepid-name', XMLDB_INDEX_UNIQUE,
06322                     array('attemptstepid', 'name'));
06323 
06324             // Launch create table for question_attempt_step_data
06325             $dbman->create_table($table);
06326         }
06327 
06328         // quiz savepoint reached
06329         upgrade_main_savepoint(true, 2011060308);
06330     }
06331 
06332     if ($oldversion < 2011060309) {
06333 
06334         // Define table question_hints to be created
06335         $table = new xmldb_table('question_hints');
06336 
06337         // Adding fields to table question_hints
06338         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06339                 XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
06340         $table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06341                 XMLDB_NOTNULL, null, null);
06342         $table->add_field('hint', XMLDB_TYPE_TEXT, 'small', null,
06343                 XMLDB_NOTNULL, null, null);
06344         $table->add_field('hintformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED,
06345                 XMLDB_NOTNULL, null, '0');
06346         $table->add_field('shownumcorrect', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
06347                 null, null, null);
06348         $table->add_field('clearwrong', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED,
06349                 null, null, null);
06350         $table->add_field('options', XMLDB_TYPE_CHAR, '255', null,
06351                 null, null, null);
06352 
06353         // Adding keys to table question_hints
06354         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
06355         $table->add_key('questionid', XMLDB_KEY_FOREIGN, array('questionid'),
06356                 'question', array('id'));
06357 
06358         // Conditionally launch create table for question_hints
06359         if (!$dbman->table_exists($table)) {
06360             $dbman->create_table($table);
06361         }
06362 
06363         // quiz savepoint reached
06364         upgrade_main_savepoint(true, 2011060309);
06365     }
06366 
06367     if ($oldversion < 2011060310) {
06368 
06369         // In the past, question_answer fractions were stored with rather
06370         // sloppy rounding. Now update them to the new standard of 7 d.p.
06371         $changes = array(
06372             '-0.66666'  => '-0.6666667',
06373             '-0.33333'  => '-0.3333333',
06374             '-0.16666'  => '-0.1666667',
06375             '-0.142857' => '-0.1428571',
06376              '0.11111'  =>  '0.1111111',
06377              '0.142857' =>  '0.1428571',
06378              '0.16666'  =>  '0.1666667',
06379              '0.33333'  =>  '0.3333333',
06380              '0.333333' =>  '0.3333333',
06381              '0.66666'  =>  '0.6666667',
06382         );
06383         foreach ($changes as $from => $to) {
06384             $DB->set_field('question_answers',
06385                     'fraction', $to, array('fraction' => $from));
06386         }
06387 
06388         // quiz savepoint reached
06389         upgrade_main_savepoint(true, 2011060310);
06390     }
06391 
06392     if ($oldversion < 2011060311) {
06393 
06394         // In the past, question penalties were stored with rather
06395         // sloppy rounding. Now update them to the new standard of 7 d.p.
06396         $DB->set_field('question',
06397                 'penalty', 0.3333333, array('penalty' => 33.3));
06398         $DB->set_field_select('question',
06399                 'penalty', 0.3333333, 'penalty >= 0.33 AND penalty <= 0.34');
06400         $DB->set_field_select('question',
06401                 'penalty', 0.6666667, 'penalty >= 0.66 AND penalty <= 0.67');
06402         $DB->set_field_select('question',
06403                 'penalty', 1, 'penalty > 1');
06404 
06405         // quiz savepoint reached
06406         upgrade_main_savepoint(true, 2011060311);
06407     }
06408 
06409     if ($oldversion < 2011060312) {
06410 
06411         // Define field hintformat to be added to question_hints table.
06412         $table = new xmldb_table('question_hints');
06413         $field = new xmldb_field('hintformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED,
06414                 XMLDB_NOTNULL, null, '0');
06415 
06416         // Conditionally launch add field partiallycorrectfeedbackformat
06417         if (!$dbman->field_exists($table, $field)) {
06418             $dbman->add_field($table, $field);
06419         }
06420 
06421         upgrade_main_savepoint(true, 2011060312);
06422     }
06423 
06424     if ($oldversion < 2011060313) {
06425         // Define field variant to be added to question_attempts
06426         $table = new xmldb_table('question_attempts');
06427         $field = new xmldb_field('variant', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
06428                 XMLDB_NOTNULL, null, 1, 'questionid');
06429 
06430         // Launch add field component
06431         if (!$dbman->field_exists($table, $field)) {
06432             $dbman->add_field($table, $field);
06433         }
06434 
06435         // Main savepoint reached
06436         upgrade_main_savepoint(true, 2011060313);
06437     }
06438     // Question engine 2 changes (14) end here
06439 
06440     if ($oldversion < 2011060500) {
06441 
06442          // Define index uniqueuserrating (not unique) to be dropped from rating
06443         $table = new xmldb_table('rating');
06444         $index = new xmldb_index('uniqueuserrating', XMLDB_INDEX_NOTUNIQUE,
06445                          array('component', 'ratingarea', 'contextid', 'itemid'));
06446 
06447         // Drop dependent index before changing fields specs
06448         if ($dbman->index_exists($table, $index)) {
06449             $dbman->drop_index($table, $index);
06450         }
06451 
06452         // Changing the default of field component on table rating to drop it
06453         $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'contextid');
06454 
06455         // Launch change of default for field component
06456         $dbman->change_field_default($table, $field);
06457 
06458         // Changing the default of field ratingarea on table rating to drop it
06459         $field = new xmldb_field('ratingarea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'component');
06460 
06461         // Launch change of default for field ratingarea
06462         $dbman->change_field_default($table, $field);
06463 
06464         // Add dependent index back
06465         if (!$dbman->index_exists($table, $index)) {
06466             $dbman->add_index($table, $index);
06467         }
06468 
06469         // Main savepoint reached
06470         upgrade_main_savepoint(true, 2011060500);
06471     }
06472 
06473     if ($oldversion < 2011060800) {
06474         // Add enabled field to message_processors
06475         $table = new xmldb_table('message_processors');
06476         $field = new xmldb_field('enabled');
06477         $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'name');
06478 
06479         // Launch add field addition
06480         if (!$dbman->field_exists($table,$field)) {
06481             $dbman->add_field($table, $field);
06482         }
06483 
06484         // Populate default messaging settings
06485         upgrade_populate_default_messaging_prefs();
06486 
06487         upgrade_main_savepoint(true, 2011060800);
06488     }
06489 
06490     if ($oldversion < 2011060800.01) { //TODO: put the right latest version
06491         // Define field shortname to be added to external_services
06492         $table = new xmldb_table('external_services');
06493         $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'timemodified');
06494 
06495         // Conditionally launch add field shortname
06496         if (!$dbman->field_exists($table, $field)) {
06497             $dbman->add_field($table, $field);
06498         }
06499 
06500         // Main savepoint reached
06501         upgrade_main_savepoint(true, 2011060800.01);
06502     }
06503 
06504     if ($oldversion < 2011062000.01) {
06505         // Changing sign of field minfraction on table question_attempts to signed
06506         $table = new xmldb_table('question_attempts');
06507         $field = new xmldb_field('minfraction', XMLDB_TYPE_NUMBER, '12, 7', null,
06508                 XMLDB_NOTNULL, null, null, 'maxmark');
06509 
06510         // Launch change of sign for field minfraction
06511         $dbman->change_field_unsigned($table, $field);
06512 
06513         // Main savepoint reached
06514         upgrade_main_savepoint(true, 2011062000.01);
06515     }
06516 
06517     // Signed fixes - MDL-28032
06518     if ($oldversion < 2011062400.02) {
06519 
06520         // Changing sign of field defaultmark on table question to unsigned
06521         $table = new xmldb_table('question');
06522         $field = new xmldb_field('defaultmark', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '1', 'generalfeedbackformat');
06523 
06524         // Launch change of sign for field defaultmark
06525         $dbman->change_field_unsigned($table, $field);
06526 
06527         // Main savepoint reached
06528         upgrade_main_savepoint(true, 2011062400.02);
06529     }
06530 
06531     if ($oldversion < 2011062400.03) {
06532         // Completion system has issue in which possible duplicate rows are
06533         // added to the course_modules_completion table. This change deletes
06534         // the older version of duplicate rows and replaces an index with a
06535         // unique one so it won't happen again.
06536 
06537         // This would have been a single query but because MySQL is a PoS
06538         // and can't do subqueries in DELETE, I have made it into two. The
06539         // system is unlikely to run out of memory as only IDs are stored in
06540         // the array.
06541 
06542         // Find all rows cmc1 where there is another row cmc2 with the
06543         // same user id and the same coursemoduleid, but a higher id (=> newer,
06544         // meaning that cmc1 is an older row).
06545         $rs = $DB->get_recordset_sql("
06546 SELECT DISTINCT
06547     cmc1.id
06548 FROM
06549     {course_modules_completion} cmc1
06550     JOIN {course_modules_completion} cmc2
06551         ON cmc2.userid = cmc1.userid
06552         AND cmc2.coursemoduleid = cmc1.coursemoduleid
06553         AND cmc2.id > cmc1.id");
06554         $deleteids = array();
06555         foreach ($rs as $row) {
06556             $deleteids[] = $row->id;
06557         }
06558         $rs->close();
06559         // Note: SELECT part performance tested on table with ~7m
06560         // rows of which ~15k match, only took 30 seconds so probably okay.
06561 
06562         // Delete all those rows
06563         $DB->delete_records_list('course_modules_completion', 'id', $deleteids);
06564 
06565         // Define index userid (not unique) to be dropped form course_modules_completion
06566         $table = new xmldb_table('course_modules_completion');
06567         $index = new xmldb_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
06568 
06569         // Conditionally launch drop index userid
06570         if ($dbman->index_exists($table, $index)) {
06571             $dbman->drop_index($table, $index);
06572         }
06573 
06574         // Define index userid-coursemoduleid (unique) to be added to course_modules_completion
06575         $index = new xmldb_index('userid-coursemoduleid', XMLDB_INDEX_UNIQUE,
06576                 array('userid', 'coursemoduleid'));
06577 
06578         // Conditionally launch add index userid-coursemoduleid
06579         if (!$dbman->index_exists($table, $index)) {
06580             $dbman->add_index($table, $index);
06581         }
06582 
06583         upgrade_main_savepoint(true, 2011062400.03);
06584     }
06585 
06586     // Moodle v2.1.0 release upgrade line
06587     // Put any upgrade step following this
06588 
06589     if ($oldversion < 2011070800.01) {
06590         // remove unused config options
06591         unset_config('excludeoldflashclients');
06592         upgrade_main_savepoint(true, 2011070800.01);
06593     }
06594 
06595     if ($oldversion < 2011071300.01) {
06596         // remove unused config option
06597         unset_config('framename');
06598         upgrade_main_savepoint(true, 2011071300.01);
06599     }
06600 
06601     if ($oldversion < 2011081700.01) {
06602         // Remove category_sortorder index that was supposed to be removed long time ago
06603         $table = new xmldb_table('course');
06604         $index = new xmldb_index('category_sortorder', XMLDB_INDEX_UNIQUE, array('category', 'sortorder'));
06605 
06606         if ($dbman->index_exists($table, $index)) {
06607             $dbman->drop_index($table, $index);
06608         }
06609         upgrade_main_savepoint(true, 2011081700.01);
06610     }
06611 
06612     if ($oldversion < 2011081700.02) {
06613         // remove safety block backup from 2.0 upgrade
06614         $table = new xmldb_table('block_pinned_old');
06615         if ($dbman->table_exists($table)) {
06616             $dbman->drop_table($table);
06617         }
06618         $table = new xmldb_table('block_instance_old');
06619         if ($dbman->table_exists($table)) {
06620             $dbman->drop_table($table);
06621         }
06622         upgrade_main_savepoint(true, 2011081700.02);
06623     }
06624 
06625     if ($oldversion < 2011083100.02) {
06626         // Define field showdescription to be added to course_modules
06627         $table = new xmldb_table('course_modules');
06628         $field = new xmldb_field('showdescription', XMLDB_TYPE_INTEGER, '1',
06629                 XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'showavailability');
06630 
06631         // Conditionally launch add field showdescription
06632         if (!$dbman->field_exists($table, $field)) {
06633             $dbman->add_field($table, $field);
06634         }
06635 
06636         // Main savepoint reached
06637         upgrade_main_savepoint(true, 2011083100.02);
06638     }
06639 
06640     if ($oldversion < 2011090700.01) {
06641         // Changing the default of field secret on table registration_hubs to NULL
06642         $table = new xmldb_table('registration_hubs');
06643         $field = new xmldb_field('secret', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'confirmed');
06644 
06645         // Launch change of default for field secret
06646         $dbman->change_field_default($table, $field);
06647 
06648         // Main savepoint reached
06649         upgrade_main_savepoint(true, 2011090700.01);
06650     }
06651 
06652     if ($oldversion < 2011091200.00) {
06653         //preference not required since 2.0
06654         $DB->delete_records('user_preferences', array('name'=>'message_showmessagewindow'));
06655 
06656         //re-introducing emailstop. check that its turned off so people dont suddenly stop getting notifications
06657         $DB->set_field('user', 'emailstop', 0, array('emailstop' => 1));
06658 
06659         upgrade_main_savepoint(true, 2011091200.00);
06660     }
06661 
06662     if ($oldversion < 2011091300.00) {
06663         // Increase the length of the of the course shortname field as it is now going
06664         // to be consistently filtered and 100 characters is practically useless for
06665         // things like the multilang filter.
06666 
06667         $table = new xmldb_table('course');
06668         $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'fullname');
06669         $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
06670 
06671         // First check the shortname field exists... pretty heavy mod if it doesnt!
06672         if ($dbman->field_exists($table, $field)) {
06673             // Conditionally launch drop index shortname, this is required to happen
06674             // before we can edit the field.
06675             if ($dbman->index_exists($table, $index)) {
06676                 $dbman->drop_index($table, $index);
06677             }
06678 
06679             // Launch change of precision for field shortname
06680             $dbman->change_field_precision($table, $field);
06681             // Add the index back to the table now that we're finished our mods
06682             $dbman->add_index($table, $index);
06683         }
06684 
06685         // Main savepoint reached
06686         upgrade_main_savepoint(true, 2011091300.00);
06687     }
06688 
06689     if ($oldversion < 2011091600.01) {
06690         // It has been decided that it is now safe to drop the backup_log table
06691         // as it hasn't been used within 2+.
06692 
06693         // Define table backup_log to be dropped
06694         $table = new xmldb_table('backup_log');
06695 
06696         // Conditionally launch drop table for backup_log
06697         if ($dbman->table_exists($table)) {
06698             $dbman->drop_table($table);
06699         }
06700 
06701         // Main savepoint reached
06702         upgrade_main_savepoint(true, 2011091600.01);
06703     }
06704 
06705     if ($oldversion < 2011092800.01) {
06706         // Check for potential missing columns in the grade_items_history
06707 
06708         $table = new xmldb_table('grade_items_history');
06709         $field = new xmldb_field('display', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, 'sortorder');
06710         if (!$dbman->field_exists($table, $field)) {
06711             $dbman->add_field($table, $field);
06712         }
06713         $field = new xmldb_field('decimals', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, 'display');
06714         if (!$dbman->field_exists($table, $field)) {
06715             $dbman->add_field($table, $field);
06716         } else {
06717             //check that the grade_items_history.decimals allows nulls
06718             //Somehow some Moodle databases have this column marked as "not null"
06719             $columns = $DB->get_columns('grade_items_history');
06720             if (array_key_exists('display', $columns) && !empty($columns['display']->not_null)) {
06721                 $dbman->change_field_notnull($table, $field);
06722             }
06723         }
06724 
06725         // Main savepoint reached
06726         upgrade_main_savepoint(true, 2011092800.01);
06727     }
06728 
06729     if ($oldversion < 2011092800.02) {
06730         // Check for potential missing columns in the grade_categories_history
06731 
06732         $table = new xmldb_table('grade_categories_history');
06733         $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timemodified');
06734         if (!$dbman->field_exists($table, $field)) {
06735             $dbman->add_field($table, $field);
06736         }
06737 
06738         // Main savepoint reached
06739         upgrade_main_savepoint(true, 2011092800.02);
06740     }
06741 
06742     if ($oldversion < 2011092800.03) {
06743         // Check for potential missing columns in the grade_outcomes_history
06744 
06745         $table = new xmldb_table('grade_outcomes_history');
06746         $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'description');
06747         if (!$dbman->field_exists($table, $field)) {
06748             $dbman->add_field($table, $field);
06749         }
06750 
06751         // Main savepoint reached
06752         upgrade_main_savepoint(true, 2011092800.03);
06753     }
06754 
06755     if ($oldversion < 2011100700.02) {
06756         // Define field idnumber to be added to course_categories
06757         $table = new xmldb_table('course_categories');
06758         $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'name');
06759 
06760         // Conditionally launch add field idnumber
06761         if (!$dbman->field_exists($table,$field)) {
06762             $dbman->add_field($table, $field);
06763         }
06764 
06765         // Main savepoint reached
06766         upgrade_main_savepoint(true, 2011100700.02);
06767     }
06768 
06769     if ($oldversion < 2011101200.01) {
06770         // The conditional availability date system used to rely on dates being
06771         // set to 23:59:59 for the end date, but now that exact times are
06772         // supported, it uses midnight on the following day.
06773 
06774         // The query is restricted on 'time mod 10 = 9' in order that
06775         // it is safe to run this upgrade twice if something goes wrong.
06776         $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil + 1 ' .
06777                 'WHERE availableuntil > 0 AND ' . $DB->sql_modulo('availableuntil', 10) . ' = 9');
06778 
06779         // Because availableuntil is stored in modinfo, we need to clear modinfo
06780         // for all courses.
06781         rebuild_course_cache(0, true);
06782 
06783         // Main savepoint reached
06784         upgrade_main_savepoint(true, 2011101200.01);
06785     }
06786 
06787     if ($oldversion < 2011101900.02) {
06788         // remove unused setting
06789         unset_config('enablehtmlpurifier');
06790         upgrade_main_savepoint(true, 2011101900.02);
06791     }
06792 
06793     if ($oldversion < 2011102700.01) {
06794         // purge everything related to abandoned experimental global search
06795 
06796         // unset setting - this disables it in case user does not delete the dirs
06797         unset_config('enableglobalsearch');
06798 
06799         // Delete block, instances and db table
06800         $table = new xmldb_table('block_search_documents');
06801         if ($dbman->table_exists($table)) {
06802             $instances = $DB->get_records('block_instances', array('blockname'=>'search'));
06803             foreach($instances as $instance) {
06804                 $context = context_block::instance($instance->id);
06805                 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
06806                 $DB->delete_records('block_positions', array('blockinstanceid' => $instance->id));
06807                 $DB->delete_records('block_instances', array('id' => $instance->id));
06808             }
06809             $DB->delete_records('block', array('name'=>'search'));
06810 
06811             $dbman->drop_table($table);
06812         }
06813 
06814         // purge all settings used by the search block
06815         $like = $DB->sql_like('name', '?', true, true, false, '|');
06816         $params = array($DB->sql_like_escape('block_search_', '|') . '%', $DB->sql_like_escape('search_in_', '|') . '%');
06817         $settings = $DB->get_records_select('config', "$like OR $like", $params);
06818         foreach ($settings as $setting) {
06819             unset_config($setting->name);
06820         }
06821 
06822         upgrade_main_savepoint(true, 2011102700.01);
06823     }
06824 
06825     if ($oldversion < 2011110200.01) {
06826         // Rename 'extrauserselectorfields' to 'showuseridentity' as it is
06827         // being used more widely
06828         if (isset($CFG->extrauserselectorfields)) {
06829             set_config('showuseridentity', $CFG->extrauserselectorfields);
06830             unset_config('extrauserselectorfields');
06831         }
06832         unset_config('grade_report_showuseridnumber');
06833         upgrade_main_savepoint(true, 2011110200.01);
06834     }
06835 
06836     if ($oldversion < 2011110200.02) {
06837         // create new core tables for the advanced grading methods framework
06838 
06839         // grading_areas table
06840         $table = new xmldb_table('grading_areas');
06841         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
06842         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
06843         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
06844         $table->add_field('areaname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
06845         $table->add_field('activemethod', XMLDB_TYPE_CHAR, '100', null, null, null, null);
06846         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
06847         $table->add_key('uq_gradable_area', XMLDB_KEY_UNIQUE, array('contextid', 'component', 'areaname'));
06848         $table->add_key('fk_context', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
06849 
06850         if (!$dbman->table_exists($table)) {
06851             $dbman->create_table($table);
06852         }
06853 
06854         // grading_definitions table
06855         $table = new xmldb_table('grading_definitions');
06856         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
06857         $table->add_field('areaid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
06858         $table->add_field('method', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
06859         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
06860         $table->add_field('description', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
06861         $table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null);
06862         $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
06863         $table->add_field('copiedfromid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
06864         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
06865         $table->add_field('usercreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
06866         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
06867         $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
06868         $table->add_field('timecopied', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, '0');
06869         $table->add_field('options', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
06870         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
06871         $table->add_key('fk_areaid', XMLDB_KEY_FOREIGN, array('areaid'), 'grading_areas', array('id'));
06872         $table->add_key('fk_usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
06873         $table->add_key('uq_area_method', XMLDB_KEY_UNIQUE, array('areaid', 'method'));
06874         $table->add_key('fk_usercreated', XMLDB_KEY_FOREIGN, array('usercreated'), 'user', array('id'));
06875 
06876         if (!$dbman->table_exists($table)) {
06877             $dbman->create_table($table);
06878         }
06879 
06880         // grading_instances table
06881         $table = new xmldb_table('grading_instances');
06882         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
06883         $table->add_field('definitionid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
06884         $table->add_field('raterid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
06885         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
06886         $table->add_field('rawgrade', XMLDB_TYPE_NUMBER, '10, 5', XMLDB_UNSIGNED, null, null, null);
06887         $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
06888         $table->add_field('feedback', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
06889         $table->add_field('feedbackformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null);
06890         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
06891         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
06892         $table->add_key('fk_definitionid', XMLDB_KEY_FOREIGN, array('definitionid'), 'grading_definitions', array('id'));
06893         $table->add_key('fk_raterid', XMLDB_KEY_FOREIGN, array('raterid'), 'user', array('id'));
06894 
06895         if (!$dbman->table_exists($table)) {
06896             $dbman->create_table($table);
06897         }
06898 
06899         upgrade_main_savepoint(true, 2011110200.02);
06900     }
06901 
06902     if ($oldversion < 2011111500.01) {
06903         upgrade_set_timeout(60*20); // this may take a while
06904         // Remove duplicate entries from groupings_groups table
06905         $sql = 'SELECT MIN(id) AS firstid, groupingid, groupid FROM {groupings_groups} '.
06906                'GROUP BY groupingid, groupid HAVING COUNT(id)>1';
06907         $badrs = $DB->get_recordset_sql($sql);
06908         foreach ($badrs as $badrec) {
06909             $where = 'groupingid = ? and groupid = ? and id > ?';
06910             $params = array($badrec->groupingid, $badrec->groupid, $badrec->firstid);
06911             $DB->delete_records_select('groupings_groups', $where, $params);
06912         }
06913         $badrs->close();
06914 
06915         // Main savepoint reached
06916         upgrade_main_savepoint(true, 2011111500.01);
06917     }
06918 
06919     if ($oldversion < 2011111800.01) {
06920         // Define field downloadfiles to be added to external_services
06921         $table = new xmldb_table('external_services');
06922         $field = new xmldb_field('downloadfiles', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'shortname');
06923 
06924         // Conditionally launch add field downloadfiles
06925         if (!$dbman->field_exists($table, $field)) {
06926             $dbman->add_field($table, $field);
06927         }
06928 
06929         // Main savepoint reached
06930         upgrade_main_savepoint(true, 2011111800.01);
06931     }
06932 
06933     // Moodle v2.2.0 release upgrade line
06934     // Put any upgrade step following this
06935 
06936     if ($oldversion < 2011120500.02) {
06937 
06938         upgrade_set_timeout(60*20); // This may take a while
06939         // MDL-28180. Some missing restrictions in certain backup & restore operations
06940         // were causing incorrect duplicates in the course_completion_aggr_methd table.
06941         // This upgrade step takes rid of them.
06942         $sql = 'SELECT course, criteriatype, MIN(id) AS minid
06943                   FROM {course_completion_aggr_methd}
06944               GROUP BY course, criteriatype
06945                 HAVING COUNT(*) > 1';
06946         $duprs = $DB->get_recordset_sql($sql);
06947         foreach ($duprs as $duprec) {
06948             // We need to handle NULLs in criteriatype diferently
06949             if (is_null($duprec->criteriatype)) {
06950                 $where = 'course = ? AND criteriatype IS NULL AND id > ?';
06951                 $params = array($duprec->course, $duprec->minid);
06952             } else {
06953                 $where = 'course = ? AND criteriatype = ? AND id > ?';
06954                 $params = array($duprec->course, $duprec->criteriatype, $duprec->minid);
06955             }
06956             $DB->delete_records_select('course_completion_aggr_methd', $where, $params);
06957         }
06958         $duprs->close();
06959 
06960         // Main savepoint reached
06961         upgrade_main_savepoint(true, 2011120500.02);
06962     }
06963 
06964     if ($oldversion < 2011120500.03) {
06965 
06966         // Changing precision of field value on table user_preferences to (1333)
06967         $table = new xmldb_table('user_preferences');
06968         $field = new xmldb_field('value', XMLDB_TYPE_CHAR, '1333', null, XMLDB_NOTNULL, null, null, 'name');
06969 
06970         // Launch change of precision for field value
06971         $dbman->change_field_precision($table, $field);
06972 
06973         // Main savepoint reached
06974         upgrade_main_savepoint(true, 2011120500.03);
06975     }
06976 
06977     if ($oldversion < 2011120501.08) {
06978         // Check if we need to fix post.uniquehash
06979         $columns = $DB->get_columns('post');
06980         if (array_key_exists('uniquehash', $columns) && $columns['uniquehash']->max_length != 255) {
06981             // Fix discrepancies in the post table after upgrade from 1.9
06982             $table = new xmldb_table('post');
06983 
06984             // Uniquehash should be 255 chars, fixed in earlier upgrade code
06985             $field = new xmldb_field('uniquehash', XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL, null, null, 'content');
06986             if ($dbman->field_exists($table, $field)) {
06987                 $dbman->change_field_precision($table, $field);
06988             }
06989         }
06990 
06991         upgrade_main_savepoint(true, 2011120501.08);
06992     }
06993 
06994     if ($oldversion < 2011120501.09) {
06995         // Somewhere before 1.9 summary and content column in post table were not null. In 1.9+
06996         // not null became false.
06997         $columns = $DB->get_columns('post');
06998 
06999         // Fix discrepancies in summary field after upgrade from 1.9
07000         if (array_key_exists('summary', $columns) && $columns['summary']->not_null != false) {
07001             $table = new xmldb_table('post');
07002             $summaryfield = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'subject');
07003 
07004             if ($dbman->field_exists($table, $summaryfield)) {
07005                 $dbman->change_field_notnull($table, $summaryfield);
07006             }
07007 
07008         }
07009 
07010         // Fix discrepancies in content field after upgrade from 1.9
07011         if (array_key_exists('content', $columns) && $columns['content']->not_null != false) {
07012             $table = new xmldb_table('post');
07013             $contentfield = new xmldb_field('content', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'summary');
07014 
07015             if ($dbman->field_exists($table, $contentfield)) {
07016                 $dbman->change_field_notnull($table, $contentfield);
07017             }
07018 
07019         }
07020 
07021         upgrade_main_savepoint(true, 2011120501.09);
07022     }
07023 
07024     return true;
07025 }
07026 
 All Data Structures Namespaces Files Functions Variables Enumerations