MDL-22943 fixed enrol upgrade problem, credit goes to Matt Campbell
[moodle.git] / lib / db / upgrade.php
1 <?PHP
3 // This file keeps track of upgrades to Moodle.
4 //
5 // Sometimes, changes between versions involve
6 // alterations to database structures and other
7 // major things that may break installations.
8 //
9 // The upgrade function in this file will attempt
10 // to perform all the necessary actions to upgrade
11 // your older installtion to the current version.
12 //
13 // If there's something it cannot do itself, it
14 // will tell you what you need to do.
15 //
16 // The commands in here will all be database-neutral,
17 // using the methods of database_manager class
18 //
19 // Please do not forget to use upgrade_set_timeout()
20 // before any action that may take longer time to finish.
22 /**
23  *
24  * @global stdClass $CFG
25  * @global stdClass $USER
26  * @global moodle_database $DB
27  * @global core_renderer $OUTPUT
28  * @param int $oldversion
29  * @return bool
30  */
31 function xmldb_main_upgrade($oldversion) {
32     global $CFG, $USER, $DB, $OUTPUT;
34     require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
36     $result = true;
38     $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
40     ////////////////////////////////////////
41     ///upgrade supported only from 1.9.x ///
42     ////////////////////////////////////////
44     if ($result && $oldversion < 2008030600) {
45         //NOTE: this table was added much later, that is why this step is repeated later in this file
47     /// Define table upgrade_log to be created
48         $table = new xmldb_table('upgrade_log');
50     /// Adding fields to table upgrade_log
51         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
52         $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
53         $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
54         $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
55         $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
56         $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
57         $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
58         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
59         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
61     /// Adding keys to table upgrade_log
62         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
63         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
65     /// Adding indexes to table upgrade_log
66         $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
67         $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
69     /// Create table for upgrade_log
70         $dbman->create_table($table);
72     /// Main savepoint reached
73         upgrade_main_savepoint($result, 2008030600);
74     }
76     if ($result && $oldversion < 2008030601) {
77         @unlink($CFG->dataroot.'/cache/languages');
79         // rename old lang directory so that the new and old langs do not mix
80         if (rename("$CFG->dataroot/lang", "$CFG->dataroot/oldlang")) {
81             $oldlang = "$CFG->dataroot/oldlang";
82         } else {
83             $oldlang = "$CFG->dataroot/lang";
84         }
85         // TODO: fetch previously installed languages ("*_utf8") found in $oldlang from moodle.org
86         upgrade_set_timeout(60*20); // this may take a while
89         // TODO: add some info file to $oldlang describing what to do with "$oldlang/*_utf8_local" dirs
92         // Main savepoint reached
93         upgrade_main_savepoint($result, 2008030601);
94     }
96     if ($result && $oldversion < 2008030700) {
97         upgrade_set_timeout(60*20); // this may take a while
99     /// Define index contextid-lowerboundary (not unique) to be dropped form grade_letters
100         $table = new xmldb_table('grade_letters');
101         $index = new xmldb_index('contextid-lowerboundary', XMLDB_INDEX_NOTUNIQUE, array('contextid', 'lowerboundary'));
103     /// Launch drop index contextid-lowerboundary
104         if ($dbman->index_exists($table, $index)) {
105             $dbman->drop_index($table, $index);
106         }
108     /// Define index contextid-lowerboundary-letter (unique) to be added to grade_letters
109         $table = new xmldb_table('grade_letters');
110         $index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
112     /// Launch add index contextid-lowerboundary-letter
113         $dbman->add_index($table, $index);
115     /// Main savepoint reached
116         upgrade_main_savepoint($result, 2008030700);
117     }
119     if ($result && $oldversion < 2008050100) {
120         // Update courses that used weekscss to weeks
121         $DB->set_field('course', 'format', 'weeks', array('format' => 'weekscss'));
122         upgrade_main_savepoint($result, 2008050100);
123     }
125     if ($result && $oldversion < 2008050200) {
126         // remove unused config options
127         unset_config('statsrolesupgraded');
128         upgrade_main_savepoint($result, 2008050200);
129     }
131     if ($result && $oldversion < 2008050700) {
132         upgrade_set_timeout(60*20); // this may take a while
134     /// Fix minor problem caused by MDL-5482.
135         require_once($CFG->dirroot . '/question/upgrade.php');
136         question_fix_random_question_parents();
137         upgrade_main_savepoint($result, 2008050700);
138     }
140     if ($result && $oldversion < 2008051201) {
141         echo $OUTPUT->notification('Increasing size of user idnumber field, this may take a while...', 'notifysuccess');
142         upgrade_set_timeout(60*20); // this may take a while
144     /// Under MySQL and Postgres... detect old NULL contents and change them by correct empty string. MDL-14859
145         $dbfamily = $DB->get_dbfamily();
146         if ($dbfamily === 'mysql' || $dbfamily === 'postgres') {
147             $DB->execute("UPDATE {user} SET idnumber = '' WHERE idnumber IS NULL");
148         }
150     /// Define index idnumber (not unique) to be dropped form user
151         $table = new xmldb_table('user');
152         $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
154     /// Launch drop index idnumber
155         if ($dbman->index_exists($table, $index)) {
156             $dbman->drop_index($table, $index);
157         }
159     /// Changing precision of field idnumber on table user to (255)
160         $table = new xmldb_table('user');
161         $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'password');
163     /// Launch change of precision for field idnumber
164         $dbman->change_field_precision($table, $field);
166     /// Launch add index idnumber again
167         $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
168         $dbman->add_index($table, $index);
170     /// Main savepoint reached
171         upgrade_main_savepoint($result, 2008051201);
172     }
174     if ($result && $oldversion < 2008051202) {
175         $log_action = new object();
176         $log_action->module = 'course';
177         $log_action->action = 'unenrol';
178         $log_action->mtable = 'course';
179         $log_action->field  = 'fullname';
180         if (!$DB->record_exists('log_display', array('action'=>'unenrol', 'module'=>'course'))) {
181             $DB->insert_record('log_display', $log_action);
182         }
183         upgrade_main_savepoint($result, 2008051202);
184     }
186     if ($result && $oldversion < 2008051203) {
187         $table = new xmldb_table('mnet_enrol_course');
188         $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
189         $dbman->change_field_precision($table, $field);
190         upgrade_main_savepoint($result, 2008051203);
191     }
193     if ($result && $oldversion < 2008063001) {
194         upgrade_set_timeout(60*20); // this may take a while
196         // table to be modified
197         $table = new xmldb_table('tag_instance');
198         // add field
199         $field = new xmldb_field('tiuserid');
200         if (!$dbman->field_exists($table, $field)) {
201             $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'itemid');
202             $dbman->add_field($table, $field);
203         }
204         // modify index
205         $index = new xmldb_index('itemtype-itemid-tagid');
206         $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid'));
207         if ($dbman->index_exists($table, $index)) {
208             $dbman->drop_index($table, $index);
209         }
210         $index = new xmldb_index('itemtype-itemid-tagid-tiuserid');
211         $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid'));
212         if (!$dbman->index_exists($table, $index)) {
213             $dbman->add_index($table, $index);
214         }
216         /// Main savepoint reached
217         upgrade_main_savepoint($result, 2008063001);
218     }
220     if ($result && $oldversion < 2008070300) {
221         $DB->delete_records_select('role_names', $DB->sql_isempty('role_names', 'name', false, false));
222         upgrade_main_savepoint($result, 2008070300);
223     }
225     if ($result && $oldversion < 2008070701) {
227     /// Define table portfolio_instance to be created
228         $table = new xmldb_table('portfolio_instance');
230     /// Adding fields to table portfolio_instance
231         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
232         $table->add_field('plugin', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
233         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
234         $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
236     /// Adding keys to table portfolio_instance
237         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
239     /// Conditionally launch create table for portfolio_instance
240         if (!$dbman->table_exists($table)) {
241             $dbman->create_table($table);
242         }
243   /// Define table portfolio_instance_config to be created
244         $table = new xmldb_table('portfolio_instance_config');
246     /// Adding fields to table portfolio_instance_config
247         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
248         $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
249         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
250         $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
252     /// Adding keys to table portfolio_instance_config
253         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
254         $table->add_key('instance', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
256     /// Adding indexes to table portfolio_instance_config
257         $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name'));
259     /// Conditionally launch create table for portfolio_instance_config
260         if (!$dbman->table_exists($table)) {
261             $dbman->create_table($table);
262         }
264    /// Define table portfolio_instance_user to be created
265         $table = new xmldb_table('portfolio_instance_user');
267     /// Adding fields to table portfolio_instance_user
268         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
269         $table->add_field('instance', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
270         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
271         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
272         $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
274     /// Adding keys to table portfolio_instance_user
275         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
276         $table->add_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
277         $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
279     /// Conditionally launch create table for portfolio_instance_user
280         if (!$dbman->table_exists($table)) {
281             $dbman->create_table($table);
282         }
284     /// Main savepoint reached
285         upgrade_main_savepoint($result, 2008070701);
286     }
288     if ($result && $oldversion < 2008072400) {
289     /// Create the database tables for message_processors
290         $table = new xmldb_table('message_processors');
291         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
292         $table->add_field('name', XMLDB_TYPE_CHAR, '166', null, XMLDB_NOTNULL, null, null);
293         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
294         $dbman->create_table($table);
296     /// delete old and create new fields
297         $table = new xmldb_table('message');
298         $field = new xmldb_field('messagetype');
299         $dbman->drop_field($table, $field);
301     /// fields to rename
302         $field = new xmldb_field('message');
303         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
304         $dbman->rename_field($table, $field, 'fullmessage');
305         $field = new xmldb_field('format');
306         $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
307         $dbman->rename_field($table, $field, 'fullmessageformat');
309     /// new message fields
310         $field = new xmldb_field('subject');
311         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
312         $dbman->add_field($table, $field);
313         $field = new xmldb_field('fullmessagehtml');
314         $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
315         $dbman->add_field($table, $field);
316         $field = new xmldb_field('smallmessage');
317         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
318         $dbman->add_field($table, $field);
321         $table = new xmldb_table('message_read');
322         $field = new xmldb_field('messagetype');
323         $dbman->drop_field($table, $field);
324         $field = new xmldb_field('mailed');
325         $dbman->drop_field($table, $field);
327     /// fields to rename
328         $field = new xmldb_field('message');
329         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
330         $dbman->rename_field($table, $field, 'fullmessage');
331         $field = new xmldb_field('format');
332         $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, null, null, '0', null);
333         $dbman->rename_field($table, $field, 'fullmessageformat');
336     /// new message fields
337         $field = new xmldb_field('subject');
338         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
339         $dbman->add_field($table, $field);
340         $field = new xmldb_field('fullmessagehtml');
341         $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, null);
342         $dbman->add_field($table, $field);
343         $field = new xmldb_field('smallmessage');
344         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, null);
345         $dbman->add_field($table, $field);
347     /// new table
348         $table = new xmldb_table('message_working');
349         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
350         $table->add_field('unreadmessageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
351         $table->add_field('processorid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
352         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
353         $dbman->create_table($table);
356         upgrade_main_savepoint($result, 2008072400);
357     }
359     if ($result && $oldversion < 2008072800) {
361     /// Define field enablecompletion to be added to course
362         $table = new xmldb_table('course');
363         $field = new xmldb_field('enablecompletion');
364         $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'defaultrole');
366     /// Launch add field enablecompletion
367         if (!$dbman->field_exists($table,$field)) {
368             $dbman->add_field($table, $field);
369         }
371     /// Define field completion to be added to course_modules
372         $table = new xmldb_table('course_modules');
373         $field = new xmldb_field('completion');
374         $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'groupmembersonly');
376     /// Launch add field completion
377         if (!$dbman->field_exists($table,$field)) {
378             $dbman->add_field($table, $field);
379         }
381     /// Define field completiongradeitemnumber to be added to course_modules
382         $field = new xmldb_field('completiongradeitemnumber');
383         $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'completion');
385     /// Launch add field completiongradeitemnumber
386         if (!$dbman->field_exists($table,$field)) {
387             $dbman->add_field($table, $field);
388         }
390     /// Define field completionview to be added to course_modules
391         $field = new xmldb_field('completionview');
392         $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completiongradeitemnumber');
394     /// Launch add field completionview
395         if (!$dbman->field_exists($table,$field)) {
396             $dbman->add_field($table, $field);
397         }
399     /// Define field completionexpected to be added to course_modules
400         $field = new xmldb_field('completionexpected');
401         $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionview');
403     /// Launch add field completionexpected
404         if (!$dbman->field_exists($table,$field)) {
405             $dbman->add_field($table, $field);
406         }
408    /// Define table course_modules_completion to be created
409         $table = new xmldb_table('course_modules_completion');
410         if (!$dbman->table_exists($table)) {
412         /// Adding fields to table course_modules_completion
413             $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
414             $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
415             $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
416             $table->add_field('completionstate', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
417             $table->add_field('viewed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
418             $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
420         /// Adding keys to table course_modules_completion
421             $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
423         /// Adding indexes to table course_modules_completion
424             $table->add_index('coursemoduleid', XMLDB_INDEX_NOTUNIQUE, array('coursemoduleid'));
425             $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
427         /// Launch create table for course_modules_completion
428             $dbman->create_table($table);
429         }
431         /// Main savepoint reached
432         upgrade_main_savepoint($result, 2008072800);
433     }
435     if ($result && $oldversion < 2008073000) {
437     /// Define table portfolio_log to be created
438         $table = new xmldb_table('portfolio_log');
440     /// Adding fields to table portfolio_log
441         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
442         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
443         $table->add_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
444         $table->add_field('portfolio', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
445         $table->add_field('caller_class', XMLDB_TYPE_CHAR, '150', null, XMLDB_NOTNULL, null, null);
446         $table->add_field('caller_file', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
447         $table->add_field('caller_sha1', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
449     /// Adding keys to table portfolio_log
450         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
451         $table->add_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
452         $table->add_key('portfoliofk', XMLDB_KEY_FOREIGN, array('portfolio'), 'portfolio_instance', array('id'));
454     /// Conditionally launch create table for portfolio_log
455         if (!$dbman->table_exists($table)) {
456             $dbman->create_table($table);
457         }
459     /// Main savepoint reached
460         upgrade_main_savepoint($result, 2008073000);
461     }
463     if ($result && $oldversion < 2008073104) {
464     /// Drop old table that might exist for some people
465         $table = new xmldb_table('message_providers');
466         if ($dbman->table_exists($table)) {
467             $dbman->drop_table($table);
468         }
470     /// Define table message_providers to be created
471         $table = new xmldb_table('message_providers');
473     /// Adding fields to table message_providers
474         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
475         $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
476         $table->add_field('component', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
477         $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
479     /// Adding keys to table message_providers
480         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
482     /// Adding indexes to table message_providers
483         $table->add_index('componentname', XMLDB_INDEX_UNIQUE, array('component', 'name'));
485     /// Create table for message_providers
486         $dbman->create_table($table);
488         upgrade_main_savepoint($result, 2008073104);
489     }
491     if ($result && $oldversion < 2008073111) {
492     /// Define table files to be created
493         $table = new xmldb_table('files');
495     /// Adding fields to table files
496         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
497         $table->add_field('contenthash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
498         $table->add_field('pathnamehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
499         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
500         $table->add_field('filearea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
501         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
502         $table->add_field('filepath', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
503         $table->add_field('filename', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
504         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
505         $table->add_field('filesize', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
506         $table->add_field('mimetype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
507         $table->add_field('status', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
508         $table->add_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
509         $table->add_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null);
510         $table->add_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null);
511         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
512         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
514     /// Adding keys to table files
515         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
516         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
517         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
519     /// Adding indexes to table files
520         $table->add_index('filearea-contextid-itemid', XMLDB_INDEX_NOTUNIQUE, array('filearea', 'contextid', 'itemid'));
521         $table->add_index('contenthash', XMLDB_INDEX_NOTUNIQUE, array('contenthash'));
522         $table->add_index('pathnamehash', XMLDB_INDEX_UNIQUE, array('pathnamehash'));
524     /// Conditionally launch create table for files
525         $dbman->create_table($table);
527     /// Main savepoint reached
528         upgrade_main_savepoint($result, 2008073111);
529     }
531     if ($result && $oldversion < 2008073112) {
532         // Define field legacyfiles to be added to course
533         $table = new xmldb_table('course');
534         $field = new xmldb_field('legacyfiles', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'maxbytes');
536         // Launch add field legacyfiles
537         $dbman->add_field($table, $field);
538         // enable legacy files in all courses
539         $DB->execute("UPDATE {course} SET legacyfiles = 2");
541         // Main savepoint reached
542         upgrade_main_savepoint($result, 2008073112);
543     }
545     if ($result && $oldversion < 2008073113) {
546     /// move all course, backup and other files to new filepool based storage
547         upgrade_migrate_files_courses();
548     /// Main savepoint reached
549         upgrade_main_savepoint($result, 2008073113);
550     }
552     if ($result && $oldversion < 2008073114) {
553     /// move all course, backup and other files to new filepool based storage
554         upgrade_migrate_files_blog();
555     /// Main savepoint reached
556         upgrade_main_savepoint($result, 2008073114);
557     }
559     if ($result && $oldversion < 2008080400) {
560         // Add field ssl_jump_url to mnet application, and populate existing default applications
561         $table = new xmldb_table('mnet_application');
562         $field = new xmldb_field('sso_jump_url');
563         if (!$dbman->field_exists($table, $field)) {
564             $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
565             $dbman->add_field($table, $field);
566             $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
567             $DB->set_field('mnet_application', 'sso_jump_url', '/auth/xmlrpc/jump.php', array('name' => 'mahara'));
568         }
570         /// Main savepoint reached
571         upgrade_main_savepoint($result, 2008080400);
572     }
574     if ($result && $oldversion < 2008080500) {
576    /// Define table portfolio_tempdata to be created
577         $table = new xmldb_table('portfolio_tempdata');
579     /// Adding fields to table portfolio_tempdata
580         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
581         $table->add_field('data', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
583     /// Adding keys to table portfolio_tempdata
584         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
586     /// Conditionally launch create table for portfolio_tempdata
587         if (!$dbman->table_exists($table)) {
588             $dbman->create_table($table);
589         }
591     /// Main savepoint reached
592         upgrade_main_savepoint($result, 2008080500);
593     }
595     if ($result && $oldversion < 2008080600) {
597         $DB->delete_records('portfolio_tempdata'); // there shouldnt' be any, and it will cause problems with this upgrade.
598     /// Define field expirytime to be added to portfolio_tempdata
599         $table = new xmldb_table('portfolio_tempdata');
600         $field = new xmldb_field('expirytime', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'data');
602     /// Conditionally launch add field expirytime
603         if (!$dbman->field_exists($table, $field)) {
604             $dbman->add_field($table, $field);
605         }
607     /// Main savepoint reached
608         upgrade_main_savepoint($result, 2008080600);
609     }
611     if ($result && $oldversion < 2008081500) {
612     /// Changing the type of all the columns that the question bank uses to store grades to be NUMBER(12, 7).
613         $table = new xmldb_table('question');
614         $field = new xmldb_field('defaultgrade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'generalfeedback');
615         $dbman->change_field_type($table, $field);
616         upgrade_main_savepoint($result, 2008081500);
617     }
619     if ($result && $oldversion < 2008081501) {
620         $table = new xmldb_table('question');
621         $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'defaultgrade');
622         $dbman->change_field_type($table, $field);
623         upgrade_main_savepoint($result, 2008081501);
624     }
626     if ($result && $oldversion < 2008081502) {
627         $table = new xmldb_table('question_answers');
628         $field = new xmldb_field('fraction', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'answer');
629         $dbman->change_field_type($table, $field);
630         upgrade_main_savepoint($result, 2008081502);
631     }
633     if ($result && $oldversion < 2008081503) {
634         $table = new xmldb_table('question_sessions');
635         $field = new xmldb_field('sumpenalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'newgraded');
636         $dbman->change_field_type($table, $field);
637         upgrade_main_savepoint($result, 2008081503);
638     }
640     if ($result && $oldversion < 2008081504) {
641         $table = new xmldb_table('question_states');
642         $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'event');
643         $dbman->change_field_type($table, $field);
644         upgrade_main_savepoint($result, 2008081504);
645     }
647     if ($result && $oldversion < 2008081505) {
648         $table = new xmldb_table('question_states');
649         $field = new xmldb_field('raw_grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'grade');
650         $dbman->change_field_type($table, $field);
651         upgrade_main_savepoint($result, 2008081505);
652     }
654     if ($result && $oldversion < 2008081506) {
655         $table = new xmldb_table('question_states');
656         $field = new xmldb_field('penalty', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'raw_grade');
657         $dbman->change_field_type($table, $field);
658         upgrade_main_savepoint($result, 2008081506);
659     }
661     if ($result && $oldversion < 2008081600) {
663     /// all 1.9 sites and fresh installs must already be unicode, not needed anymore
664         unset_config('unicodedb');
666     /// Main savepoint reached
667         upgrade_main_savepoint($result, 2008081600);
668     }
670     if ($result && $oldversion < 2008081900) {
671     /// Define field userid to be added to portfolio_tempdata
672         $table = new xmldb_table('portfolio_tempdata');
673         $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'expirytime');
675     /// Conditionally launch add field userid
676         if (!$dbman->field_exists($table, $field)) {
677             $dbman->add_field($table, $field);
678         }
679         $DB->set_field('portfolio_tempdata', 'userid', 0);
680     /// now change it to be notnull
682     /// Changing nullability of field userid on table portfolio_tempdata to not null
683         $field = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'expirytime');
685     /// Launch change of nullability for field userid
686         $dbman->change_field_notnull($table, $field);
688     /// Define key userfk (foreign) to be added to portfolio_tempdata
689         $table = new xmldb_table('portfolio_tempdata');
690         $key = new xmldb_key('userfk', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
692     /// Launch add key userfk
693         $dbman->add_key($table, $key);
695         upgrade_main_savepoint($result, 2008081900);
696     }
697     if ($result && $oldversion < 2008082602) {
699     /// Define table repository to be dropped
700         $table = new xmldb_table('repository');
702     /// Conditionally launch drop table for repository
703         if ($dbman->table_exists($table)) {
704             $dbman->drop_table($table);
705         }
707     /// Define table repository to be created
708         $table = new xmldb_table('repository');
710     /// Adding fields to table repository
711         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
712         $table->add_field('type', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
713         $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '1');
714         $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
716     /// Adding keys to table repository
717         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
719     /// Conditionally launch create table for repository
720         if (!$dbman->table_exists($table)) {
721             $dbman->create_table($table);
722         }
723     /// Define table repository_instances to be created
724         $table = new xmldb_table('repository_instances');
726     /// Adding fields to table repository_instances
727         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
728         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
729         $table->add_field('typeid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
730         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
731         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
732         $table->add_field('username', XMLDB_TYPE_CHAR, '255', null, null, null, null);
733         $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
734         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
735         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
737     /// Adding keys to table repository_instances
738         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
740     /// Conditionally launch create table for repository_instances
741         if (!$dbman->table_exists($table)) {
742             $dbman->create_table($table);
743         }
745     /// Define table repository_instance_config to be created
746         $table = new xmldb_table('repository_instance_config');
748     /// Adding fields to table repository_instance_config
749         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
750         $table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
751         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
752         $table->add_field('value', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
754     /// Adding keys to table repository_instance_config
755         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
757     /// Conditionally launch create table for repository_instance_config
758         if (!$dbman->table_exists($table)) {
759             $dbman->create_table($table);
760         }
762     /// Main savepoint reached
763         upgrade_main_savepoint($result, 2008082602);
764     }
766     if ($result && $oldversion < 2008082700) {
767     /// Add a new column to the question sessions table to record whether a
768     /// question has been flagged.
770     /// Define field flagged to be added to question_sessions
771         $table = new xmldb_table('question_sessions');
772         $field = new xmldb_field('flagged', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'manualcomment');
774     /// Conditionally launch add field flagged
775         if (!$dbman->field_exists($table, $field)) {
776             $dbman->add_field($table, $field);
777         }
779     /// Main savepoint reached
780         upgrade_main_savepoint($result, 2008082700);
781     }
783     if ($result && $oldversion < 2008082900) {
785     /// Changing precision of field parent_type on table mnet_rpc to (20)
786         $table = new xmldb_table('mnet_rpc');
787         $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
789     /// Launch change of precision for field parent_type
790         $dbman->change_field_precision($table, $field);
792     /// Main savepoint reached
793         upgrade_main_savepoint($result, 2008082900);
794     }
796     if ($result && $oldversion < 2008090108) {
797         $repo = new object();
798         $repo->type      = 'upload';
799         $repo->visible   = 1;
800         $repo->sortorder = 1;
801         if (!$DB->record_exists('repository', array('type'=>'upload'))) {
802             $typeid = $DB->insert_record('repository', $repo);
803         }else{
804             $record = $DB->get_record('repository', array('type'=>'upload'));
805             $typeid = $record->id;
806         }
807         if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
808             $instance = new object();
809             $instance->name      = get_string('repositoryname', 'repository_upload');
810             $instance->typeid    = $typeid;
811             $instance->userid    = 0;
812             $instance->contextid = SITEID;
813             $instance->timecreated  = time();
814             $instance->timemodified = time();
815             $DB->insert_record('repository_instances', $instance);
816         }
817         $repo->type      = 'local';
818         $repo->visible   = 1;
819         $repo->sortorder = 1;
820         if (!$DB->record_exists('repository', array('type'=>'local'))) {
821             $typeid = $DB->insert_record('repository', $repo);
822         }else{
823             $record = $DB->get_record('repository', array('type'=>'local'));
824             $typeid = $record->id;
825         }
826         if (!$DB->record_exists('repository_instances', array('typeid'=>$typeid))) {
827             $instance = new object();
828             $instance->name      = get_string('repositoryname', 'repository_local');
829             $instance->typeid    = $typeid;
830             $instance->userid    = 0;
831             $instance->contextid = SITEID;
832             $instance->timecreated  = time();
833             $instance->timemodified = time();
834             $DB->insert_record('repository_instances', $instance);
835         }
837         upgrade_main_savepoint($result, 2008090108);
838     }
840     // MDL-16411 Move all plugintype_pluginname_version values from config to config_plugins.
841     if ($result && $oldversion < 2008091000) {
842         foreach (get_object_vars($CFG) as $name => $value) {
843             if (substr($name, strlen($name) - 8) !== '_version') {
844                 continue;
845             }
846             $pluginname = substr($name, 0, strlen($name) - 8);
847             if (!strpos($pluginname, '_')) {
848                 // Skip things like backup_version that don't contain an extra _
849                 continue;
850             }
851             if ($pluginname == 'enrol_ldap_version') {
852                 // Special case - this is something different from a plugin version number.
853                 continue;
854             }
855             if (!preg_match('/^\d{10}$/', $value)) {
856                 // Extra safety check, skip anything that does not look like a Moodle
857                 // version number (10 digits).
858                 continue;
859             }
860             set_config('version', $value, $pluginname);
861             unset_config($name);
862         }
863         upgrade_main_savepoint($result, 2008091000);
864     }
866     //Add a readonly field to the repository_instances table
867     //in order to support instance created automatically by a repository plugin
868      if ($result && $oldversion < 2008091611) {
870     /// Define field readonly to be added to repository_instances
871         $table = new xmldb_table('repository_instances');
872         $field = new xmldb_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
874     /// Conditionally launch add field readonly
875         if (!$dbman->field_exists($table, $field)) {
876             $dbman->add_field($table, $field);
877         }
879     /// Main savepoint reached
880         upgrade_main_savepoint($result, 2008091611);
881     }
883     if ($result && $oldversion < 2008092300) {
884         unset_config('editorspelling');
885         unset_config('editordictionary');
886     /// Main savepoint reached
887         upgrade_main_savepoint($result, 2008092300);
888     }
890     if ($result && $oldversion < 2008101300) {
892         if (!get_config(NULL, 'statsruntimedays')) {
893             set_config('statsruntimedays', '31');
894         }
896     /// Main savepoint reached
897         upgrade_main_savepoint($result, 2008101300);
898     }
900     /// Drop the deprecated teacher, teachers, student and students columns from the course table.
901     if ($result && $oldversion < 2008111200) {
902         $table = new xmldb_table('course');
904     /// Conditionally launch drop field teacher
905         $field = new xmldb_field('teacher');
906         if ($dbman->field_exists($table, $field)) {
907             $dbman->drop_field($table, $field);
908         }
910     /// Conditionally launch drop field teacher
911         $field = new xmldb_field('teachers');
912         if ($dbman->field_exists($table, $field)) {
913             $dbman->drop_field($table, $field);
914         }
916     /// Conditionally launch drop field teacher
917         $field = new xmldb_field('student');
918         if ($dbman->field_exists($table, $field)) {
919             $dbman->drop_field($table, $field);
920         }
922     /// Conditionally launch drop field teacher
923         $field = new xmldb_field('students');
924         if ($dbman->field_exists($table, $field)) {
925             $dbman->drop_field($table, $field);
926         }
928     /// Main savepoint reached
929         upgrade_main_savepoint($result, 2008111200);
930     }
932 /// Add a unique index to the role.name column.
933     if ($result && $oldversion < 2008111800) {
935     /// Define index name (unique) to be added to role
936         $table = new xmldb_table('role');
937         $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
939     /// Conditionally launch add index name
940         if (!$dbman->index_exists($table, $index)) {
941             $dbman->add_index($table, $index);
942         }
944     /// Main savepoint reached
945         upgrade_main_savepoint($result, 2008111800);
946     }
948 /// Add a unique index to the role.shortname column.
949     if ($result && $oldversion < 2008111801) {
951     /// Define index shortname (unique) to be added to role
952         $table = new xmldb_table('role');
953         $index = new xmldb_index('shortname', XMLDB_INDEX_UNIQUE, array('shortname'));
955     /// Conditionally launch add index shortname
956         if (!$dbman->index_exists($table, $index)) {
957             $dbman->add_index($table, $index);
958         }
960     /// Main savepoint reached
961         upgrade_main_savepoint($result, 2008111801);
962     }
964     if ($result && $oldversion < 2008120700) {
966     /// Changing precision of field shortname on table course_request to (100)
967         $table = new xmldb_table('course_request');
968         $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
970     /// Before changing the field, drop dependent indexes
971     /// Define index shortname (not unique) to be dropped form course_request
972         $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
973     /// Conditionally launch drop index shortname
974         if ($dbman->index_exists($table, $index)) {
975             $dbman->drop_index($table, $index);
976         }
978     /// Launch change of precision for field shortname
979         $dbman->change_field_precision($table, $field);
981     /// After changing the field, recreate dependent indexes
982     /// Define index shortname (not unique) to be added to course_request
983         $index = new xmldb_index('shortname', XMLDB_INDEX_NOTUNIQUE, array('shortname'));
984     /// Conditionally launch add index shortname
985         if (!$dbman->index_exists($table, $index)) {
986             $dbman->add_index($table, $index);
987         }
989     /// Main savepoint reached
990         upgrade_main_savepoint($result, 2008120700);
991     }
993     if ($result && $oldversion < 2008120801) {
995     /// Changing precision of field shortname on table mnet_enrol_course to (100)
996         $table = new xmldb_table('mnet_enrol_course');
997         $field = new xmldb_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'fullname');
999     /// Launch change of precision for field shortname
1000         $dbman->change_field_precision($table, $field);
1002     /// Main savepoint reached
1003         upgrade_main_savepoint($result, 2008120801);
1004     }
1006     if ($result && $oldversion < 2008121701) {
1008     /// Define field availablefrom to be added to course_modules
1009         $table = new xmldb_table('course_modules');
1010         $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'completionexpected');
1012     /// Conditionally launch add field availablefrom
1013         if (!$dbman->field_exists($table, $field)) {
1014             $dbman->add_field($table, $field);
1015         }
1017     /// Define field availableuntil to be added to course_modules
1018         $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availablefrom');
1020     /// Conditionally launch add field availableuntil
1021         if (!$dbman->field_exists($table, $field)) {
1022             $dbman->add_field($table, $field);
1023         }
1025     /// Define field showavailability to be added to course_modules
1026         $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'availableuntil');
1028     /// Conditionally launch add field showavailability
1029         if (!$dbman->field_exists($table, $field)) {
1030             $dbman->add_field($table, $field);
1031         }
1033     /// Define table course_modules_availability to be created
1034         $table = new xmldb_table('course_modules_availability');
1036     /// Adding fields to table course_modules_availability
1037         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1038         $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1039         $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1040         $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null);
1041         $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
1042         $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1043         $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
1045     /// Adding keys to table course_modules_availability
1046         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1047         $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
1048         $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
1049         $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
1051     /// Conditionally launch create table for course_modules_availability
1052         if (!$dbman->table_exists($table)) {
1053             $dbman->create_table($table);
1054         }
1056     /// Changes to modinfo mean we need to rebuild course cache
1057         require_once($CFG->dirroot . '/course/lib.php');
1058         rebuild_course_cache(0, true);
1060     /// Main savepoint reached
1061         upgrade_main_savepoint($result, 2008121701);
1062     }
1064     if ($result && $oldversion < 2009010500) {
1065     /// clean up config table a bit
1066         unset_config('session_error_counter');
1068     /// Main savepoint reached
1069         upgrade_main_savepoint($result, 2009010500);
1070     }
1072     if ($result && $oldversion < 2009010600) {
1074     /// Define field originalquestion to be dropped from question_states
1075         $table = new xmldb_table('question_states');
1076         $field = new xmldb_field('originalquestion');
1078     /// Conditionally launch drop field originalquestion
1079         if ($dbman->field_exists($table, $field)) {
1080             $dbman->drop_field($table, $field);
1081         }
1083     /// Main savepoint reached
1084         upgrade_main_savepoint($result, 2009010600);
1085     }
1087     if ($result && $oldversion < 2009010601) {
1089     /// Changing precision of field ip on table log to (45)
1090         $table = new xmldb_table('log');
1091         $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1093     /// Launch change of precision for field ip
1094         $dbman->change_field_precision($table, $field);
1096     /// Main savepoint reached
1097         upgrade_main_savepoint($result, 2009010601);
1098     }
1100     if ($result && $oldversion < 2009010602) {
1102     /// Changing precision of field lastip on table user to (45)
1103         $table = new xmldb_table('user');
1104         $field = new xmldb_field('lastip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'currentlogin');
1106     /// Launch change of precision for field lastip
1107         $dbman->change_field_precision($table, $field);
1109     /// Main savepoint reached
1110         upgrade_main_savepoint($result, 2009010602);
1111     }
1113     if ($result && $oldversion < 2009010603) {
1115     /// Changing precision of field ip_address on table mnet_host to (45)
1116         $table = new xmldb_table('mnet_host');
1117         $field = new xmldb_field('ip_address', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'wwwroot');
1119     /// Launch change of precision for field ip_address
1120         $dbman->change_field_precision($table, $field);
1122     /// Main savepoint reached
1123         upgrade_main_savepoint($result, 2009010603);
1124     }
1126     if ($result && $oldversion < 2009010604) {
1128     /// Changing precision of field ip on table mnet_log to (45)
1129         $table = new xmldb_table('mnet_log');
1130         $field = new xmldb_field('ip', XMLDB_TYPE_CHAR, '45', null, XMLDB_NOTNULL, null, null, 'userid');
1132     /// Launch change of precision for field ip
1133         $dbman->change_field_precision($table, $field);
1135     /// Main savepoint reached
1136         upgrade_main_savepoint($result, 2009010604);
1137     }
1139     if ($result && $oldversion < 2009011000) {
1141     /// Changing nullability of field configdata on table block_instance to null
1142         $table = new xmldb_table('block_instance');
1143         $field = new xmldb_field('configdata');
1144         $field->set_attributes(XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'visible');
1146     /// Launch change of nullability for field configdata
1147         $dbman->change_field_notnull($table, $field);
1149     /// Main savepoint reached
1150         upgrade_main_savepoint($result, 2009011000);
1151     }
1153     if ($result && $oldversion < 2009011100) {
1154     /// Remove unused settings
1155         unset_config('zip');
1156         unset_config('unzip');
1157         unset_config('adminblocks_initialised');
1159     /// Main savepoint reached
1160         upgrade_main_savepoint($result, 2009011100);
1161     }
1163     if ($result && $oldversion < 2009011101) {
1164     /// Migrate backup settings to core plugin config table
1165         $configs = $DB->get_records('backup_config');
1166         foreach ($configs as $config) {
1167             set_config($config->name, $config->value, 'backup');
1168         }
1170     /// Define table to be dropped
1171         $table = new xmldb_table('backup_config');
1173     /// Launch drop table for old backup config
1174         $dbman->drop_table($table);
1176     /// Main savepoint reached
1177         upgrade_main_savepoint($result, 2009011101);
1178     }
1180     if ($result && $oldversion < 2009011303) {
1182     /// Define table config_log to be created
1183         $table = new xmldb_table('config_log');
1185     /// Adding fields to table config_log
1186         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1187         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1188         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1189         $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1190         $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1191         $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1192         $table->add_field('oldvalue', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1194     /// Adding keys to table config_log
1195         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1196         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1198     /// Adding indexes to table config_log
1199         $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1201     /// Launch create table for config_log
1202         $dbman->create_table($table);
1204     /// Main savepoint reached
1205         upgrade_main_savepoint($result, 2009011303);
1206     }
1208     if ($result && $oldversion < 2009011900) {
1210     /// Define table sessions2 to be dropped
1211         $table = new xmldb_table('sessions2');
1213     /// Conditionally launch drop table for sessions
1214         if ($dbman->table_exists($table)) {
1215             $dbman->drop_table($table);
1216         }
1218     /// Define table sessions to be dropped
1219         $table = new xmldb_table('sessions');
1221     /// Conditionally launch drop table for sessions
1222         if ($dbman->table_exists($table)) {
1223             $dbman->drop_table($table);
1224         }
1226     /// Define table sessions to be created
1227         $table = new xmldb_table('sessions');
1229     /// Adding fields to table sessions
1230         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1231         $table->add_field('state', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1232         $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
1233         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1234         $table->add_field('sessdata', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
1235         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1236         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1237         $table->add_field('firstip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1238         $table->add_field('lastip', XMLDB_TYPE_CHAR, '45', null, null, null, null);
1240     /// Adding keys to table sessions
1241         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1242         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1244     /// Adding indexes to table sessions
1245         $table->add_index('state', XMLDB_INDEX_NOTUNIQUE, array('state'));
1246         $table->add_index('sid', XMLDB_INDEX_UNIQUE, array('sid'));
1247         $table->add_index('timecreated', XMLDB_INDEX_NOTUNIQUE, array('timecreated'));
1248         $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1250     /// Launch create table for sessions
1251         $dbman->create_table($table);
1253     /// Main savepoint reached
1254         upgrade_main_savepoint($result, 2009011900);
1255     }
1257     if ($result && $oldversion < 2009012901) {
1258         // NOTE: this table may already exist, see beginning of this file ;-)
1260     /// Define table upgrade_log to be created
1261         $table = new xmldb_table('upgrade_log');
1263     /// Adding fields to table upgrade_log
1264         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1265         $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1266         $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1267         $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
1268         $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1269         $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1270         $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1271         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1272         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1274     /// Adding keys to table upgrade_log
1275         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1276         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1278     /// Adding indexes to table upgrade_log
1279         $table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1280         $table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
1282     /// Conditionally launch create table for upgrade_log
1283         if (!$dbman->table_exists($table)) {
1284             $dbman->create_table($table);
1285         }
1287     /// Main savepoint reached
1288         upgrade_main_savepoint($result, 2009012901);
1289     }
1291     if ($result && $oldversion < 2009021800) {
1292         // Converting format of grade conditions, if any exist, to percentages.
1293         $DB->execute("
1294 UPDATE {course_modules_availability} SET grademin=(
1295     SELECT 100.0*({course_modules_availability}.grademin-gi.grademin)
1296         /(gi.grademax-gi.grademin)
1297     FROM {grade_items} gi
1298     WHERE gi.id={course_modules_availability}.gradeitemid)
1299 WHERE gradeitemid IS NOT NULL AND grademin IS NOT NULL");
1300         $DB->execute("
1301 UPDATE {course_modules_availability} SET grademax=(
1302     SELECT 100.0*({course_modules_availability}.grademax-gi.grademin)
1303         /(gi.grademax-gi.grademin)
1304     FROM {grade_items} gi
1305     WHERE gi.id={course_modules_availability}.gradeitemid)
1306 WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
1308     /// Main savepoint reached
1309         upgrade_main_savepoint($result, 2009021800);
1310     }
1311     if ($result && $oldversion < 2009021801) {
1312     /// Define field backuptype to be added to backup_log
1313         $table = new xmldb_table('backup_log');
1314         $field = new xmldb_field('backuptype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'info');
1315     /// Conditionally Launch add field backuptype and set all old records as 'scheduledbackup' records.
1316         if (!$dbman->field_exists($table, $field)) {
1317             $dbman->add_field($table, $field);
1318             $DB->execute("UPDATE {backup_log} SET backuptype='scheduledbackup'");
1319         }
1321     /// Main savepoint reached
1322         upgrade_main_savepoint($result, 2009021801);
1323     }
1324     /// Add default sort order for question types.
1325     if ($result && $oldversion < 2009030300) {
1326         set_config('multichoice_sortorder', 1, 'question');
1327         set_config('truefalse_sortorder', 2, 'question');
1328         set_config('shortanswer_sortorder', 3, 'question');
1329         set_config('numerical_sortorder', 4, 'question');
1330         set_config('calculated_sortorder', 5, 'question');
1331         set_config('essay_sortorder', 6, 'question');
1332         set_config('match_sortorder', 7, 'question');
1333         set_config('randomsamatch_sortorder', 8, 'question');
1334         set_config('multianswer_sortorder', 9, 'question');
1335         set_config('description_sortorder', 10, 'question');
1336         set_config('random_sortorder', 11, 'question');
1337         set_config('missingtype_sortorder', 12, 'question');
1339         upgrade_main_savepoint($result, 2009030300);
1340     }
1341     if ($result && $oldversion < 2009030501) {
1342     /// setup default repository plugins
1343         require_once($CFG->dirroot . '/repository/lib.php');
1344         repository_setup_default_plugins();
1345     /// Main savepoint reached
1346         upgrade_main_savepoint($result, 2009030501);
1347     }
1349     /// MDL-18132 replace the use a new Role allow switch settings page, instead of
1350     /// $CFG->allowuserswitchrolestheycantassign
1351     if ($result && $oldversion < 2009032000) {
1352     /// First create the new table.
1353             $table = new xmldb_table('role_allow_switch');
1355     /// Adding fields to table role_allow_switch
1356         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1357         $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1358         $table->add_field('allowswitch', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1360     /// Adding keys to table role_allow_switch
1361         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1362         $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
1363         $table->add_key('allowswitch', XMLDB_KEY_FOREIGN, array('allowswitch'), 'role', array('id'));
1365     /// Adding indexes to table role_allow_switch
1366         $table->add_index('roleid-allowoverride', XMLDB_INDEX_UNIQUE, array('roleid', 'allowswitch'));
1368     /// Conditionally launch create table for role_allow_switch
1369         if (!$dbman->table_exists($table)) {
1370             $dbman->create_table($table);
1371         }
1373     /// Main savepoint reached
1374         upgrade_main_savepoint($result, 2009032000);
1375     }
1377     if ($result && $oldversion < 2009032001) {
1378     /// Copy from role_allow_assign into the new table.
1379         $DB->execute('INSERT INTO {role_allow_switch} (roleid, allowswitch)
1380                 SELECT roleid, allowassign FROM {role_allow_assign}');
1382     /// Unset the config variable used in 1.9.
1383         unset_config('allowuserswitchrolestheycantassign');
1385     /// Main savepoint reached
1386         upgrade_main_savepoint($result, 2009032001);
1387     }
1389     if ($result && $oldversion < 2009033100) {
1390         require_once("$CFG->dirroot/filter/tex/lib.php");
1391         filter_tex_updatedcallback(null);
1392     /// Main savepoint reached
1393         upgrade_main_savepoint($result, 2009033100);
1394     }
1396     if ($result && $oldversion < 2009040300) {
1398     /// Define table filter_active to be created
1399         $table = new xmldb_table('filter_active');
1401     /// Adding fields to table filter_active
1402         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1403         $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1404         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1405         $table->add_field('active', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
1406         $table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1408     /// Adding keys to table filter_active
1409         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1410         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1412     /// Adding indexes to table filter_active
1413         $table->add_index('contextid-filter', XMLDB_INDEX_UNIQUE, array('contextid', 'filter'));
1415     /// Conditionally launch create table for filter_active
1416         if (!$dbman->table_exists($table)) {
1417             $dbman->create_table($table);
1418         }
1420     /// Main savepoint reached
1421         upgrade_main_savepoint($result, 2009040300);
1422     }
1424     if ($result && $oldversion < 2009040301) {
1426     /// Define table filter_config to be created
1427         $table = new xmldb_table('filter_config');
1429     /// Adding fields to table filter_config
1430         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1431         $table->add_field('filter', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
1432         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1433         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1434         $table->add_field('value', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1436     /// Adding keys to table filter_config
1437         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1438         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1440     /// Adding indexes to table filter_config
1441         $table->add_index('contextid-filter-name', XMLDB_INDEX_UNIQUE, array('contextid', 'filter', 'name'));
1443     /// Conditionally launch create table for filter_config
1444         if (!$dbman->table_exists($table)) {
1445             $dbman->create_table($table);
1446         }
1448     /// Main savepoint reached
1449         upgrade_main_savepoint($result, 2009040301);
1450     }
1452     if ($result && $oldversion < 2009040302) {
1453     /// Transfer current settings from $CFG->textfilters
1454         $disabledfilters = filter_get_all_installed();
1455         if (empty($CFG->textfilters)) {
1456             $activefilters = array();
1457         } else {
1458             $activefilters = explode(',', $CFG->textfilters);
1459         }
1460         $syscontext = get_context_instance(CONTEXT_SYSTEM);
1461         $sortorder = 1;
1462         foreach ($activefilters as $filter) {
1463             filter_set_global_state($filter, TEXTFILTER_ON, $sortorder);
1464             $sortorder += 1;
1465             unset($disabledfilters[$filter]);
1466         }
1467         foreach ($disabledfilters as $filter => $notused) {
1468             filter_set_global_state($filter, TEXTFILTER_DISABLED, $sortorder);
1469             $sortorder += 1;
1470         }
1472     /// Main savepoint reached
1473         upgrade_main_savepoint($result, 2009040302);
1474     }
1476     if ($result && $oldversion < 2009040600) {
1477     /// Ensure that $CFG->stringfilters is set.
1478         if (empty($CFG->stringfilters)) {
1479             if (!empty($CFG->filterall)) {
1480                 set_config('stringfilters', $CFG->textfilters);
1481             } else {
1482                 set_config('stringfilters', '');
1483             }
1484         }
1486         set_config('filterall', !empty($CFG->stringfilters));
1487         unset_config('textfilters');
1489     /// Main savepoint reached
1490         upgrade_main_savepoint($result, 2009040600);
1491     }
1493     if ($result && $oldversion < 2009041700) {
1494     /// To ensure the UI remains consistent with no behaviour change, any
1495     /// 'until' date in an activity condition should have 1 second subtracted
1496     /// (to go from 0:00 on the following day to 23:59 on the previous one).
1497         $DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil - 1 WHERE availableuntil <> 0');
1498         require_once($CFG->dirroot . '/course/lib.php');
1499         rebuild_course_cache(0, true);
1501     /// Main savepoint reached
1502         upgrade_main_savepoint($result, 2009041700);
1503     }
1505     if ($result && $oldversion < 2009042600) {
1506     /// Deleting orphaned messages from deleted users.
1507         require_once($CFG->dirroot.'/message/lib.php');
1508     /// Detect deleted users with messages sent(useridfrom) and not read
1509         if ($deletedusers = $DB->get_records_sql('SELECT DISTINCT u.id
1510                                                     FROM {user} u
1511                                                     JOIN {message} m ON m.useridfrom = u.id
1512                                                    WHERE u.deleted = ?', array(1))) {
1513             foreach ($deletedusers as $deleteduser) {
1514                 message_move_userfrom_unread2read($deleteduser->id); // move messages
1515             }
1516         }
1517     /// Main savepoint reached
1518         upgrade_main_savepoint($result, 2009042600);
1519     }
1521     /// Dropping all enums/check contraints from core. MDL-18577
1522     if ($result && $oldversion < 2009042700) {
1524     /// Changing list of values (enum) of field stattype on table stats_daily to none
1525         $table = new xmldb_table('stats_daily');
1526         $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1528     /// Launch change of list of values for field stattype
1529         $dbman->drop_enum_from_field($table, $field);
1531     /// Changing list of values (enum) of field stattype on table stats_weekly to none
1532         $table = new xmldb_table('stats_weekly');
1533         $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1535     /// Launch change of list of values for field stattype
1536         $dbman->drop_enum_from_field($table, $field);
1538     /// Changing list of values (enum) of field stattype on table stats_monthly to none
1539         $table = new xmldb_table('stats_monthly');
1540         $field = new xmldb_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity', 'roleid');
1542     /// Launch change of list of values for field stattype
1543         $dbman->drop_enum_from_field($table, $field);
1545     /// Changing list of values (enum) of field publishstate on table post to none
1546         $table = new xmldb_table('post');
1547         $field = new xmldb_field('publishstate', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'draft', 'attachment');
1549     /// Launch change of list of values for field publishstate
1550         $dbman->drop_enum_from_field($table, $field);
1552     /// Main savepoint reached
1553         upgrade_main_savepoint($result, 2009042700);
1554     }
1556     if ($result && $oldversion < 2009043000) {
1557         unset_config('grade_report_showgroups');
1558         upgrade_main_savepoint($result, 2009043000);
1559     }
1561     if ($result && $oldversion < 2009050600) {
1562     /// Site front page blocks need to be moved due to page name change.
1563         $DB->set_field('block_instance', 'pagetype', 'site-index', array('pagetype' => 'course-view', 'pageid' => SITEID));
1565     /// Main savepoint reached
1566         upgrade_main_savepoint($result, 2009050600);
1567     }
1569     if ($result && $oldversion < 2009050601) {
1571     /// Define table block_instance to be renamed to block_instances
1572         $table = new xmldb_table('block_instance');
1574     /// Launch rename table for block_instance
1575         $dbman->rename_table($table, 'block_instances');
1577     /// Main savepoint reached
1578         upgrade_main_savepoint($result, 2009050601);
1579     }
1581     if ($result && $oldversion < 2009050602) {
1583     /// Define table block_instance to be renamed to block_instance_old
1584         $table = new xmldb_table('block_pinned');
1586     /// Launch rename table for block_instance
1587         $dbman->rename_table($table, 'block_pinned_old');
1589     /// Main savepoint reached
1590         upgrade_main_savepoint($result, 2009050602);
1591     }
1593     if ($result && $oldversion < 2009050603) {
1595     /// Define table block_instance_old to be created
1596         $table = new xmldb_table('block_instance_old');
1598     /// Adding fields to table block_instance_old
1599         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1600         $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1601         $table->add_field('blockid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1602         $table->add_field('pageid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
1603         $table->add_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
1604         $table->add_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null);
1605         $table->add_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0');
1606         $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
1607         $table->add_field('configdata', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
1609     /// Adding keys to table block_instance_old
1610         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1611         $table->add_key('blockid', XMLDB_KEY_FOREIGN, array('blockid'), 'block', array('id'));
1613     /// Adding indexes to table block_instance_old
1614         $table->add_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1615         $table->add_index('pagetype', XMLDB_INDEX_NOTUNIQUE, array('pagetype'));
1617     /// Conditionally launch create table for block_instance_old
1618         if (!$dbman->table_exists($table)) {
1619             $dbman->create_table($table);
1620         }
1622     /// Main savepoint reached
1623         upgrade_main_savepoint($result, 2009050603);
1624     }
1626     if ($result && $oldversion < 2009050604) {
1627     /// Copy current blocks data from block_instances to block_instance_old
1628         $DB->execute('INSERT INTO {block_instance_old} (oldid, blockid, pageid, pagetype, position, weight, visible, configdata)
1629             SELECT id, blockid, pageid, pagetype, position, weight, visible, configdata FROM {block_instances} ORDER BY id');
1631         upgrade_main_savepoint($result, 2009050604);
1632     }
1634     if ($result && $oldversion < 2009050605) {
1636     /// Define field multiple to be dropped from block
1637         $table = new xmldb_table('block');
1638         $field = new xmldb_field('multiple');
1640     /// Conditionally launch drop field multiple
1641         if ($dbman->field_exists($table, $field)) {
1642             $dbman->drop_field($table, $field);
1643         }
1645     /// Main savepoint reached
1646         upgrade_main_savepoint($result, 2009050605);
1647     }
1649     if ($result && $oldversion < 2009050606) {
1650         $table = new xmldb_table('block_instances');
1652     /// Rename field weight on table block_instances to defaultweight
1653         $field = new xmldb_field('weight', XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'position');
1654         $dbman->rename_field($table, $field, 'defaultweight');
1656     /// Rename field position on table block_instances to defaultregion
1657         $field = new xmldb_field('position', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null, 'pagetype');
1658         $dbman->rename_field($table, $field, 'defaultregion');
1660         /// Main savepoint reached
1661         upgrade_main_savepoint($result, 2009050606);
1662     }
1664     if ($result && $oldversion < 2009050607) {
1665     /// Changing precision of field defaultregion on table block_instances to (16)
1666         $table = new xmldb_table('block_instances');
1667         $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'pagetype');
1669     /// Launch change of precision for field defaultregion
1670         $dbman->change_field_precision($table, $field);
1672     /// Main savepoint reached
1673         upgrade_main_savepoint($result, 2009050607);
1674     }
1676     if ($result && $oldversion < 2009050608) {
1677     /// Change regions to the new notation
1678         $DB->set_field('block_instances', 'defaultregion', 'side-pre', array('defaultregion' => 'l'));
1679         $DB->set_field('block_instances', 'defaultregion', 'side-post', array('defaultregion' => 'r'));
1680         $DB->set_field('block_instances', 'defaultregion', 'course-view-top', array('defaultregion' => 'c'));
1681         // This third one is a custom value from contrib/patches/center_blocks_position_patch and the
1682         // flex page course format. Hopefully this new value is an adequate alternative.
1684     /// Main savepoint reached
1685         upgrade_main_savepoint($result, 2009050608);
1686     }
1688     if ($result && $oldversion < 2009050609) {
1690     /// Define key blockname (unique) to be added to block
1691         $table = new xmldb_table('block');
1692         $key = new xmldb_key('blockname', XMLDB_KEY_UNIQUE, array('name'));
1694     /// Launch add key blockname
1695         $dbman->add_key($table, $key);
1697     /// Main savepoint reached
1698         upgrade_main_savepoint($result, 2009050609);
1699     }
1701     if ($result && $oldversion < 2009050610) {
1702         $table = new xmldb_table('block_instances');
1704     /// Define field blockname to be added to block_instances
1705         $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'blockid');
1706         if (!$dbman->field_exists($table, $field)) {
1707             $dbman->add_field($table, $field);
1708         }
1710     /// Define field contextid to be added to block_instances
1711         $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'blockname');
1712         if (!$dbman->field_exists($table, $field)) {
1713             $dbman->add_field($table, $field);
1714         }
1716     /// Define field showinsubcontexts to be added to block_instances
1717         $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, null, null, null, 'contextid');
1718         if (!$dbman->field_exists($table, $field)) {
1719             $dbman->add_field($table, $field);
1720         }
1722     /// Define field subpagepattern to be added to block_instances
1723         $field = new xmldb_field('subpagepattern', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'pagetype');
1724         if (!$dbman->field_exists($table, $field)) {
1725             $dbman->add_field($table, $field);
1726         }
1728     /// Main savepoint reached
1729         upgrade_main_savepoint($result, 2009050610);
1730     }
1732     if ($result && $oldversion < 2009050611) {
1733         $table = new xmldb_table('block_instances');
1735     /// Fill in blockname from blockid
1736         $DB->execute("UPDATE {block_instances} SET blockname = (SELECT name FROM {block} WHERE id = blockid)");
1738     /// Set showinsubcontexts = 0 for all rows.
1739         $DB->execute("UPDATE {block_instances} SET showinsubcontexts = 0");
1741     /// Main savepoint reached
1742         upgrade_main_savepoint($result, 2009050611);
1743     }
1745     if ($result && $oldversion < 2009050612) {
1747     /// Rename field pagetype on table block_instances to pagetypepattern
1748         $table = new xmldb_table('block_instances');
1749         $field = new xmldb_field('pagetype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'pageid');
1751     /// Launch rename field pagetype
1752         $dbman->rename_field($table, $field, 'pagetypepattern');
1754     /// Main savepoint reached
1755         upgrade_main_savepoint($result, 2009050612);
1756     }
1758     if ($result && $oldversion < 2009050613) {
1759     /// fill in contextid and subpage, and update pagetypepattern from pagetype and pageid
1761     /// site-index
1762         $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
1763         $DB->execute("UPDATE {block_instances} SET contextid = " . $frontpagecontext->id . ",
1764                                                    pagetypepattern = 'site-index',
1765                                                    subpagepattern = NULL
1766                       WHERE pagetypepattern = 'site-index'");
1768     /// course-view
1769         $DB->execute("UPDATE {block_instances} SET
1770                         contextid = (
1771                             SELECT {context}.id
1772                             FROM {context}
1773                             JOIN {course} ON instanceid = {course}.id AND contextlevel = " . CONTEXT_COURSE . "
1774                             WHERE {course}.id = pageid
1775                         ),
1776                        pagetypepattern = 'course-view-*',
1777                        subpagepattern = NULL
1778                       WHERE pagetypepattern = 'course-view'");
1780     /// admin
1781         $syscontext = get_context_instance(CONTEXT_SYSTEM);
1782         $DB->execute("UPDATE {block_instances} SET
1783                         contextid = " . $syscontext->id . ",
1784                         pagetypepattern = 'admin-*',
1785                         subpagepattern = NULL
1786                       WHERE pagetypepattern = 'admin'");
1788     /// my-index
1789         $DB->execute("UPDATE {block_instances} SET
1790                         contextid = (
1791                             SELECT {context}.id
1792                             FROM {context}
1793                             JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1794                             WHERE {user}.id = pageid
1795                         ),
1796                         pagetypepattern = 'my-index',
1797                         subpagepattern = NULL
1798                       WHERE pagetypepattern = 'my-index'");
1800     /// tag-index
1801         $DB->execute("UPDATE {block_instances} SET
1802                         contextid = " . $syscontext->id . ",
1803                         pagetypepattern = 'tag-index',
1804                         subpagepattern = pageid
1805                       WHERE pagetypepattern = 'tag-index'");
1807     /// blog-view
1808         $DB->execute("UPDATE {block_instances} SET
1809                         contextid = (
1810                             SELECT {context}.id
1811                             FROM {context}
1812                             JOIN {user} ON instanceid = {user}.id AND contextlevel = " . CONTEXT_USER . "
1813                             WHERE {user}.id = pageid
1814                         ),
1815                         pagetypepattern = 'blog-index',
1816                         subpagepattern = NULL
1817                       WHERE pagetypepattern = 'blog-view'");
1819     /// mod-xxx-view
1820         $moduleswithblocks = array('chat', 'data', 'lesson', 'quiz', 'dimdim', 'game', 'wiki', 'oublog');
1821         foreach ($moduleswithblocks as $modname) {
1822             if (!$dbman->table_exists($modname)) {
1823                 continue;
1824             }
1825             $DB->execute("UPDATE {block_instances} SET
1826                             contextid = (
1827                                 SELECT {context}.id
1828                                 FROM {context}
1829                                 JOIN {course_modules} ON instanceid = {course_modules}.id AND contextlevel = " . CONTEXT_MODULE . "
1830                                 JOIN {modules} ON {modules}.id = {course_modules}.module AND {modules}.name = '$modname'
1831                                 JOIN {{$modname}} ON {course_modules}.instance = {{$modname}}.id
1832                                 WHERE {{$modname}}.id = pageid
1833                             ),
1834                             pagetypepattern = 'blog-index',
1835                             subpagepattern = NULL
1836                           WHERE pagetypepattern = 'blog-view'");
1837         }
1839     /// Main savepoint reached
1840         upgrade_main_savepoint($result, 2009050613);
1841     }
1843     if ($result && $oldversion < 2009050614) {
1844     /// fill in any missing contextids with a dummy value, so we can add the not-null constraint.
1845         $DB->execute("UPDATE {block_instances} SET contextid = 0 WHERE contextid IS NULL");
1847     /// Main savepoint reached
1848         upgrade_main_savepoint($result, 2009050614);
1849     }
1851     if ($result && $oldversion < 2009050615) {
1852         $table = new xmldb_table('block_instances');
1854     /// Arrived here, any block_instances record without blockname is one
1855     /// orphan block coming from 1.9. Just delete them. MDL-22503
1856         $DB->delete_records_select('block_instances', 'blockname IS NULL');
1858     /// Changing nullability of field blockname on table block_instances to not null
1859         $field = new xmldb_field('blockname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
1860         $dbman->change_field_notnull($table, $field);
1862     /// Changing nullability of field contextid on table block_instances to not null
1863         $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
1864         $dbman->change_field_notnull($table, $field);
1866     /// Changing nullability of field showinsubcontexts on table block_instances to not null
1867         $field = new xmldb_field('showinsubcontexts', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, 'contextid');
1868         $dbman->change_field_notnull($table, $field);
1870     /// Main savepoint reached
1871         upgrade_main_savepoint($result, 2009050615);
1872     }
1874     if ($result && $oldversion < 2009050616) {
1875     /// Add exiting sticky blocks.
1876         $blocks = $DB->get_records('block');
1877         $syscontext = get_context_instance(CONTEXT_SYSTEM);
1878         $newregions = array(
1879             'l' => 'side-pre',
1880             'r' => 'side-post',
1881             'c' => 'course-view-top',
1882         );
1883         $stickyblocks = $DB->get_recordset('block_pinned_old');
1884         foreach ($stickyblocks as $stickyblock) {
1885             // Only if the block exists (avoid orphaned sticky blocks)
1886             if (!isset($blocks[$stickyblock->blockid]) || empty($blocks[$stickyblock->blockid]->name)) {
1887                 continue;
1888             }
1889             $newblock = new object();
1890             $newblock->blockname = $blocks[$stickyblock->blockid]->name;
1891             $newblock->contextid = $syscontext->id;
1892             $newblock->showinsubcontexts = 1;
1893             switch ($stickyblock->pagetype) {
1894                 case 'course-view':
1895                     $newblock->pagetypepattern = 'course-view-*';
1896                     break;
1897                 default:
1898                     $newblock->pagetypepattern = $stickyblock->pagetype;
1899             }
1900             $newblock->defaultregion = $newregions[$stickyblock->position];
1901             $newblock->defaultweight = $stickyblock->weight;
1902             $newblock->configdata = $stickyblock->configdata;
1903             $newblock->visible = 1;
1904             $DB->insert_record('block_instances', $newblock);
1905         }
1907     /// Main savepoint reached
1908         upgrade_main_savepoint($result, 2009050616);
1909     }
1911     if ($result && $oldversion < 2009050617) {
1913     /// Define table block_positions to be created
1914         $table = new xmldb_table('block_positions');
1916     /// Adding fields to table block_positions
1917         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1918         $table->add_field('blockinstanceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1919         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
1920         $table->add_field('pagetype', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
1921         $table->add_field('subpage', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1922         $table->add_field('visible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1');
1923         $table->add_field('region', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null);
1924         $table->add_field('weight', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1926     /// Adding keys to table block_positions
1927         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1928         $table->add_key('blockinstanceid', XMLDB_KEY_FOREIGN, array('blockinstanceid'), 'block_instances', array('id'));
1929         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
1931     /// Adding indexes to table block_positions
1932         $table->add_index('blockinstanceid-contextid-pagetype-subpage', XMLDB_INDEX_UNIQUE, array('blockinstanceid', 'contextid', 'pagetype', 'subpage'));
1934     /// Conditionally launch create table for block_positions
1935         if (!$dbman->table_exists($table)) {
1936             $dbman->create_table($table);
1937         }
1939     /// Main savepoint reached
1940         upgrade_main_savepoint($result, 2009050617);
1941     }
1943     if ($result && $oldversion < 2009050618) {
1944     /// And block instances with visible = 0, copy that information to block_positions
1945         $DB->execute("INSERT INTO {block_positions} (blockinstanceid, contextid, pagetype, subpage, visible, region, weight)
1946                 SELECT id, contextid,
1947                 CASE WHEN pagetypepattern = 'course-view-*' THEN
1948                         (SELECT " . $DB->sql_concat("'course-view-'", 'format') . "
1949                         FROM {course}
1950                         JOIN {context} ON {course}.id = {context}.instanceid
1951                         WHERE {context}.id = contextid)
1952                     ELSE pagetypepattern END,
1953                 CASE WHEN subpagepattern IS NULL THEN ''
1954                     ELSE subpagepattern END,
1955                 0, defaultregion, defaultweight
1956                 FROM {block_instances} WHERE visible = 0 AND pagetypepattern <> 'admin-*'");
1958     /// Main savepoint reached
1959         upgrade_main_savepoint($result, 2009050618);
1960     }
1962     if ($result && $oldversion < 2009050619) {
1963         $table = new xmldb_table('block_instances');
1965     /// Define field blockid to be dropped from block_instances
1966         $field = new xmldb_field('blockid');
1967         if ($dbman->field_exists($table, $field)) {
1968         /// Before dropping the field, drop dependent indexes
1969             $index = new xmldb_index('blockid', XMLDB_INDEX_NOTUNIQUE, array('blockid'));
1970             if ($dbman->index_exists($table, $index)) {
1971             /// Launch drop index blockid
1972                 $dbman->drop_index($table, $index);
1973             }
1974             $dbman->drop_field($table, $field);
1975         }
1977     /// Define field pageid to be dropped from block_instances
1978         $field = new xmldb_field('pageid');
1979         if ($dbman->field_exists($table, $field)) {
1980         /// Before dropping the field, drop dependent indexes
1981             $index = new xmldb_index('pageid', XMLDB_INDEX_NOTUNIQUE, array('pageid'));
1982             if ($dbman->index_exists($table, $index)) {
1983             /// Launch drop index pageid
1984                 $dbman->drop_index($table, $index);
1985             }
1986             $dbman->drop_field($table, $field);
1987         }
1989     /// Define field visible to be dropped from block_instances
1990         $field = new xmldb_field('visible');
1991         if ($dbman->field_exists($table, $field)) {
1992             $dbman->drop_field($table, $field);
1993         }
1995     /// Main savepoint reached
1996         upgrade_main_savepoint($result, 2009050619);
1997     }
1999     if ($result && $oldversion < 2009051200) {
2000     /// Let's check the status of mandatory mnet_host records, fixing them
2001     /// and moving "orphan" users to default localhost record. MDL-16879
2002         echo $OUTPUT->notification('Fixing mnet records, this may take a while...', 'notifysuccess');
2003         upgrade_fix_incorrect_mnethostids();
2005     /// Main savepoint reached
2006         upgrade_main_savepoint($result, 2009051200);
2007     }
2010     if ($result && $oldversion < 2009051700) {
2011     /// migrate editor settings
2012         if (empty($CFG->htmleditor)) {
2013             set_config('texteditors', 'textarea');
2014         } else {
2015             set_config('texteditors', 'tinymce,textarea');
2016         }
2018         unset_config('htmleditor');
2019         unset_config('defaulthtmleditor');
2021     /// Main savepoint reached
2022         upgrade_main_savepoint($result, 2009051700);
2023     }
2025     if ($result && $oldversion < 2009060200) {
2026     /// Define table files_cleanup to be dropped - not needed
2027         $table = new xmldb_table('files_cleanup');
2029     /// Conditionally launch drop table for files_cleanup
2030         if ($dbman->table_exists($table)) {
2031             $dbman->drop_table($table);
2032         }
2034     /// Main savepoint reached
2035         upgrade_main_savepoint($result, 2009060200);
2036     }
2038     if ($result && $oldversion < 2009061300) {
2039         //TODO: copy this to the very beginning of this upgrade script so that we may log upgrade queries
2041     /// Define table log_queries to be created
2042         $table = new xmldb_table('log_queries');
2044     /// Adding fields to table log_queries
2045         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2046         $table->add_field('qtype', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2047         $table->add_field('sqltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
2048         $table->add_field('sqlparams', XMLDB_TYPE_TEXT, 'big', null, null, null, null);
2049         $table->add_field('error', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2050         $table->add_field('info', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2051         $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2052         $table->add_field('exectime', XMLDB_TYPE_NUMBER, '10, 5', null, XMLDB_NOTNULL, null, null);
2053         $table->add_field('timelogged', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2055     /// Adding keys to table log_queries
2056         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2058     /// Conditionally launch create table for log_queries
2059         if (!$dbman->table_exists($table)) {
2060             $dbman->create_table($table);
2061         }
2063     /// Main savepoint reached
2064         upgrade_main_savepoint($result, 2009061300);
2065     }
2067     /// Repeat 2009050607 upgrade step, which Petr commented out becuase of XMLDB
2068     /// stupidity, so lots of peopel will have missed.
2069     if ($result && $oldversion < 2009061600) {
2070     /// Changing precision of field defaultregion on table block_instances to (16)
2071         $table = new xmldb_table('block_instances');
2072         $field = new xmldb_field('defaultregion', XMLDB_TYPE_CHAR, '16', null, XMLDB_NOTNULL, null, null, 'configdata');
2074     /// Launch change of precision for field defaultregion
2075         $dbman->change_field_precision($table, $field);
2077     /// Main savepoint reached
2078         upgrade_main_savepoint($result, 2009061600);
2079     }
2081     if ($result && $oldversion < 2009061702) {
2082         // standardizing plugin names
2083         if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'quizreport_%'")) {
2084             foreach ($configs as $config) {
2085                 unset_config($config->name, $config->plugin); /// unset old config
2086                 $config->plugin = str_replace('quizreport_', 'quiz_', $config->plugin);
2087                 set_config($config->name, $config->value, $config->plugin); /// set new config
2088             }
2089         }
2090         unset($configs);
2091         upgrade_main_savepoint($result, 2009061702);
2092     }
2094     if ($result && $oldversion < 2009061703) {
2095         // standardizing plugin names
2096         if ($configs = $DB->get_records_select('config_plugins', "plugin LIKE 'assignment_type_%'")) {
2097             foreach ($configs as $config) {
2098                 unset_config($config->name, $config->plugin); /// unset old config
2099                 $config->plugin = str_replace('assignment_type_', 'assignment_', $config->plugin);
2100                 set_config($config->name, $config->value, $config->plugin); /// set new config
2101             }
2102         }
2103         unset($configs);
2104         upgrade_main_savepoint($result, 2009061703);
2105     }
2107     if ($result && $oldversion < 2009061704) {
2108         // change component string in capability records to new "_" format
2109         if ($caps = $DB->get_records('capabilities')) {
2110             foreach ($caps as $cap) {
2111                 $cap->component = str_replace('/', '_', $cap->component);
2112                 $DB->update_record('capabilities', $cap);
2113             }
2114         }
2115         unset($caps);
2116         upgrade_main_savepoint($result, 2009061704);
2117     }
2119     if ($result && $oldversion < 2009061705) {
2120         // change component string in events_handlers records to new "_" format
2121         if ($handlers = $DB->get_records('events_handlers')) {
2122             foreach ($handlers as $handler) {
2123                 $handler->handlermodule = str_replace('/', '_', $handler->handlermodule);
2124                 $DB->update_record('events_handlers', $handler);
2125             }
2126         }
2127         unset($handlers);
2128         upgrade_main_savepoint($result, 2009061705);
2129     }
2131     if ($result && $oldversion < 2009061706) {
2132         // change component string in message_providers records to new "_" format
2133         if ($mps = $DB->get_records('message_providers')) {
2134             foreach ($mps as $mp) {
2135                 $mp->component = str_replace('/', '_', $mp->component);
2136                 $DB->update_record('message_providers', $cap);
2137             }
2138         }
2139         unset($caps);
2140         upgrade_main_savepoint($result, 2009061706);
2141     }
2143     if ($result && $oldversion < 2009063000) {
2144         // upgrade format of _with_advanced settings - quiz only
2145         // note: this can be removed later, not needed for upgrades from 1.9.x
2146         if ($quiz = get_config('quiz')) {
2147             foreach ($quiz as $name=>$value) {
2148                 if (strpos($name, 'fix_') !== 0) {
2149                     continue;
2150                 }
2151                 $newname = substr($name,4).'_adv';
2152                 set_config($newname, $value, 'quiz');
2153                 unset_config($name, 'quiz');
2154             }
2155         }
2156         upgrade_main_savepoint($result, 2009063000);
2157     }
2159     if ($result && $oldversion < 2009071000) {
2161     /// Rename field contextid on table block_instances to parentcontextid
2162         $table = new xmldb_table('block_instances');
2163         $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, 'blockname');
2165     /// Launch rename field parentcontextid
2166         $dbman->rename_field($table, $field, 'parentcontextid');
2168     /// Main savepoint reached
2169         upgrade_main_savepoint($result, 2009071000);
2170     }
2172     if ($result && $oldversion < 2009071300) {
2174     /// Create contexts for every block. In the past, only non-sticky course block had contexts.
2175     /// This is a copy of the code in create_contexts.
2176         $sql = "INSERT INTO {context} (contextlevel, instanceid)
2177                 SELECT " . CONTEXT_BLOCK . ", bi.id
2178                   FROM {block_instances} bi
2179                  WHERE NOT EXISTS (SELECT 'x'
2180                                      FROM {context} ctx
2181                                     WHERE bi.id = ctx.instanceid AND ctx.contextlevel=" . CONTEXT_BLOCK . ")";
2182         $DB->execute($sql);
2184     /// TODO MDL-19776 We should not really use API funcitons in upgrade.
2185     /// If MDL-19776 is done, we can remove this whole upgrade block.
2186         build_context_path();
2188     /// Main savepoint reached
2189         upgrade_main_savepoint($result, 2009071300);
2190     }
2192     if ($result && $oldversion < 2009071600) {
2194     /// Define field summaryformat to be added to post
2195         $table = new xmldb_table('post');
2196         $field = new xmldb_field('summaryformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
2198     /// Conditionally launch add field summaryformat
2199         if (!$dbman->field_exists($table, $field)) {
2200             $dbman->add_field($table, $field);
2201         }
2203     /// Main savepoint reached
2204         upgrade_main_savepoint($result, 2009071600);
2205     }
2207     if ($result && $oldversion < 2009072400) {
2209     /// Define table comments to be created
2210         $table = new xmldb_table('comments');
2212     /// Adding fields to table comments
2213         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2214         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2215         $table->add_field('commentarea', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2216         $table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2217         $table->add_field('content', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2218         $table->add_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2219         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2220         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2222     /// Adding keys to table comments
2223         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2225     /// Conditionally launch create table for comments
2226         if (!$dbman->table_exists($table)) {
2227             $dbman->create_table($table);
2228         }
2230     /// Main savepoint reached
2231         upgrade_main_savepoint($result, 2009072400);
2232     }
2234     /**
2235      * This upgrade is to set up the new navigation blocks that have been developed
2236      * as part of Moodle 2.0
2237      * Now I [Sam Hemelryk] hit a conundrum while exploring how to go about this
2238      * as not only do we want to install the new blocks but we also want to set up
2239      * default instances of them, and at the same time remove instances of the blocks
2240      * that were/will-be outmoded by the two new navigation blocks.
2241      * After talking it through with Tim Hunt {@link http://moodle.org/mod/cvsadmin/view.php?conversationid=3112}
2242      * we decided that the best way to go about this was to put the bulk of the
2243      * upgrade operation into core upgrade `here` but to let the plugins block
2244      * still install the blocks.
2245      * This leaves one hairy end in that we will create block_instances within the
2246      * DB before the blocks themselves are created within the DB
2247      */
2248     if ($result && $oldversion < 2009082800) {
2250         echo $OUTPUT->notification(get_string('navigationupgrade', 'admin'));
2252         // Get the system context so we can set the block instances to it
2253         $syscontext = get_context_instance(CONTEXT_SYSTEM);
2255         // An array to contain the new block instances we will create
2256         $newblockinstances = array('globalnavigation'=>new stdClass,'settingsnavigation'=>new stdClass);
2257         // The new global navigation block instance as a stdClass
2258         $newblockinstances['globalnavigation']->blockname = 'global_navigation_tree';
2259         $newblockinstances['globalnavigation']->parentcontextid = $syscontext->id; // System context
2260         $newblockinstances['globalnavigation']->showinsubcontexts = true; // Show absolutly everywhere
2261         $newblockinstances['globalnavigation']->pagetypepattern = '*'; // Thats right everywhere
2262         $newblockinstances['globalnavigation']->subpagetypepattern = null;
2263         $newblockinstances['globalnavigation']->defaultregion = BLOCK_POS_LEFT;
2264         $newblockinstances['globalnavigation']->defaultweight = -10; // Try make this first
2265         $newblockinstances['globalnavigation']->configdata = '';
2266         // The new settings navigation block instance as a stdClass
2267         $newblockinstances['settingsnavigation']->blockname = 'settings_navigation_tree';
2268         $newblockinstances['settingsnavigation']->parentcontextid = $syscontext->id;
2269         $newblockinstances['settingsnavigation']->showinsubcontexts = true;
2270         $newblockinstances['settingsnavigation']->pagetypepattern = '*';
2271         $newblockinstances['settingsnavigation']->subpagetypepattern = null;
2272         $newblockinstances['settingsnavigation']->defaultregion = BLOCK_POS_LEFT;
2273         $newblockinstances['settingsnavigation']->defaultweight = -9; // Try make this second
2274         $newblockinstances['settingsnavigation']->configdata = '';
2276         // Blocks that are outmoded and for whom the bells will toll... by which I
2277         // mean we will delete all instances of
2278         $outmodedblocks = array('participants','admin_tree','activity_modules','admin','course_list');
2279         $outmodedblocksstring = '\''.join('\',\'',$outmodedblocks).'\'';
2280         unset($outmodedblocks);
2281         // Retrieve the block instance id's and parent contexts, so we can join them an GREATLY
2282         // cut down the number of delete queries we will need to run
2283         $allblockinstances = $DB->get_recordset_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')', array(), '', 'id, parentcontextid');
2285         $contextids = array();
2286         $instanceids = array();
2287         // Iterate through all block instances
2288         foreach ($allblockinstances as $blockinstance) {
2289             if (!in_array($blockinstance->parentcontextid, $contextids)) {
2290                 $contextids[] = $blockinstance->parentcontextid;
2292                 // If we have over 1000 contexts clean them up and reset the array
2293                 // this ensures we don't hit any nasty memory limits or such
2294                 if (count($contextids) > 1000) {
2295                     upgrade_cleanup_unwanted_block_contexts($contextids);
2296                     $contextids = array();
2297                 }
2298             }
2299             if (!in_array($blockinstance->id, $instanceids)) {
2300                 $instanceids[] = $blockinstance->id;
2301                 // If we have more than 1000 block instances now remove all block positions
2302                 // and empty the array
2303                 if (count($contextids) > 1000) {
2304                     $instanceidstring = join(',',$instanceids);
2305                     $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2306                     $instanceids = array();
2307                 }
2308             }
2309         }
2311         upgrade_cleanup_unwanted_block_contexts($contextids);
2313         $instanceidstring = join(',',$instanceids);
2314         $outcome1 = $result && $DB->delete_records_select('block_positions', 'blockinstanceid IN ('.$instanceidstring.')');
2316         unset($allblockinstances);
2317         unset($contextids);
2318         unset($instanceids);
2319         unset($instanceidstring);
2321         // Now remove the actual block instance
2322         $DB->delete_records_select('block_instances', 'blockname IN ('.$outmodedblocksstring.')');
2323         unset($outmodedblocksstring);
2325         // Insert the new block instances. Remember they have not been installed yet
2326         // however this should not be a problem
2327         foreach ($newblockinstances as $blockinstance) {
2328             $blockinstance->id= $DB->insert_record('block_instances', $blockinstance);
2329             // Ensure the block context is created.
2330             get_context_instance(CONTEXT_BLOCK, $blockinstance->id);
2331         }
2332         unset($newblockinstances);
2334         upgrade_main_savepoint($result, 2009082800);
2335         // The end of the navigation upgrade
2336     }
2338     if ($result && $oldversion < 2009090800){
2339         //insert new record for log_display table
2340         //used to record tag update.
2341         if (!$DB->record_exists('log_display', array('action'=>'update', 'module'=>'tag'))) {
2342             $log_action = new object();
2343             $log_action->module = 'tag';
2344             $log_action->action = 'update';
2345             $log_action->mtable = 'tag';
2346             $log_action->field  = 'name';
2348             $result  = $result && $DB->insert_record('log_display', $log_action);
2349         }
2350         upgrade_main_savepoint($result, 2009090800);
2351     }
2353     if ($result && $oldversion < 2009100601) {
2354         // drop all previous tables defined during the dev phase
2355         $dropold = array('external_services_users', 'external_services_functions', 'external_services', 'external_functions');
2356         foreach ($dropold as $tablename) {
2357             $table = new xmldb_table($tablename);
2358             if ($dbman->table_exists($table)) {
2359                 $dbman->drop_table($table);
2360             }
2361         }
2362         upgrade_main_savepoint($result, 2009100601);
2363     }
2365     if ($result && $oldversion < 2009100602) {
2366     /// Define table external_functions to be created
2367         $table = new xmldb_table('external_functions');
2369     /// Adding fields to table external_functions
2370         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2371         $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2372         $table->add_field('classname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2373         $table->add_field('methodname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2374         $table->add_field('classpath', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2375         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2377     /// Adding keys to table external_functions
2378         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2380     /// Adding indexes to table external_functions
2381         $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2383     /// Launch create table for external_functions
2384         $dbman->create_table($table);
2386     /// Main savepoint reached
2387         upgrade_main_savepoint($result, 2009100602);
2388     }
2390     if ($result && $oldversion < 2009100603) {
2391     /// Define table external_services to be created
2392         $table = new xmldb_table('external_services');
2394     /// Adding fields to table external_services
2395         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2396         $table->add_field('name', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2397         $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2398         $table->add_field('requiredcapability', XMLDB_TYPE_CHAR, '150', null, null, null, null);
2399         $table->add_field('restrictedusers', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2400         $table->add_field('component', XMLDB_TYPE_CHAR, '100', null, null, null, null);
2402     /// Adding keys to table external_services
2403         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2405     /// Adding indexes to table external_services
2406         $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name'));
2408     /// Launch create table for external_services
2409         $dbman->create_table($table);
2411     /// Main savepoint reached
2412         upgrade_main_savepoint($result, 2009100603);
2413     }
2415     if ($result && $oldversion < 2009100604) {
2416     /// Define table external_services_functions to be created
2417         $table = new xmldb_table('external_services_functions');
2419     /// Adding fields to table external_services_functions
2420         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2421         $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2422         $table->add_field('functionname', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null);
2424     /// Adding keys to table external_services_functions
2425         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2426         $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2428     /// Launch create table for external_services_functions
2429         $dbman->create_table($table);
2431     /// Main savepoint reached
2432         upgrade_main_savepoint($result, 2009100604);
2433     }
2435     if ($result && $oldversion < 2009100605) {
2436     /// Define table external_services_users to be created
2437         $table = new xmldb_table('external_services_users');
2439     /// Adding fields to table external_services_users
2440         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2441         $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2442         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2443         $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2444         $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2445         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2447     /// Adding keys to table external_services_users
2448         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2449         $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2450         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2452     /// Launch create table for external_services_users
2453         $dbman->create_table($table);
2455     /// Main savepoint reached
2456         upgrade_main_savepoint($result, 2009100605);
2457     }
2459     if ($result && $oldversion < 2009102600) {
2461     /// Define table external_tokens to be created
2462         $table = new xmldb_table('external_tokens');
2464     /// Adding fields to table external_tokens
2465         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2466         $table->add_field('token', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null);
2467         $table->add_field('tokentype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2468         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2469         $table->add_field('externalserviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2470         $table->add_field('sid', XMLDB_TYPE_CHAR, '128', null, null, null, null);
2471         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2472         $table->add_field('iprestriction', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2473         $table->add_field('validuntil', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2474         $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2475         $table->add_field('lastaccess', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2477     /// Adding keys to table external_tokens
2478         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2479         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2480         $table->add_key('externalserviceid', XMLDB_KEY_FOREIGN, array('externalserviceid'), 'external_services', array('id'));
2481         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2483     /// Launch create table for external_tokens
2484         $dbman->create_table($table);
2486     /// Main savepoint reached
2487         upgrade_main_savepoint($result, 2009102600);
2488     }
2490    if ($result && $oldversion < 2009103000) {
2492     /// Define table blog_association to be created
2493         $table = new xmldb_table('blog_association');
2495     /// Adding fields to table blog_association
2496         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2497         $table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2498         $table->add_field('blogid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2500     /// Adding keys to table blog_association
2501         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2502         $table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
2503         $table->add_key('blogid', XMLDB_KEY_FOREIGN, array('blogid'), 'post', array('id'));
2505     /// Conditionally launch create table for blog_association
2506         if (!$dbman->table_exists($table)) {
2507             $dbman->create_table($table);
2508         }
2510 /// Define table blog_external to be created
2511         $table = new xmldb_table('blog_external');
2513     /// Adding fields to table blog_external
2514         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2515         $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
2516         $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2517         $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2518         $table->add_field('url', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
2519         $table->add_field('filtertags', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2520         $table->add_field('failedlastsync', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2521         $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
2522         $table->add_field('timefetched', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2524     /// Adding keys to table blog_external
2525         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2526         $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2528     /// Conditionally launch create table for blog_external
2529         if ($dbman->table_exists($table)) {
2530             // Delete the existing one first (comes from early dev version)
2531             $dbman->drop_table($table);
2532         }
2533         $dbman->create_table($table);
2535         // now inform admins that some settings require attention after upgrade
2536         if (($CFG->bloglevel == BLOG_COURSE_LEVEL || $CFG->bloglevel == BLOG_GROUP_LEVEL) && empty($CFG->bloglevel_upgrade_complete)) {
2537             echo $OUTPUT->notification(get_string('bloglevelupgradenotice', 'admin'));
2539             $site = get_site();
2541             $a = new StdClass;
2542             $a->sitename = $site->fullname;
2543             $a->fixurl   = "$CFG->wwwroot/$CFG->admin/bloglevelupgrade.php";
2545             $subject = get_string('bloglevelupgrade', 'admin');
2546             $description = get_string('bloglevelupgradedescription', 'admin', $a);
2548             // can not use messaging here because it is not configured yet!
2549             upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2550         }
2551     /// Main savepoint reached
2552         upgrade_main_savepoint($result, 2009103000);
2553     }
2555     if ($result && $oldversion < 2009110400) {
2557         // An array used to store the table name and keys of summary and trust fields
2558         // to be added
2559         $extendtables = array();
2560         $extendtables['course'] = array('summaryformat');
2561         $extendtables['course_categories'] = array('descriptionformat');
2562         $extendtables['course_request'] = array('summaryformat');
2563         $extendtables['grade_outcomes'] = array('descriptionformat');
2564         $extendtables['groups'] = array('descriptionformat');
2565         $extendtables['groupings'] = array('descriptionformat');
2566         $extendtables['scale'] = array('descriptionformat');
2567         $extendtables['user'] = array('descriptionformat');
2568         $extendtables['user_info_field'] = array('descriptionformat', 'defaultdataformat');
2569         $extendtables['user_info_data'] = array('dataformat');
2571         foreach ($extendtables as $tablestr=>$newfields) {
2572             $table = new xmldb_table($tablestr);
2573             foreach ($newfields as $fieldstr) {
2574                 $field = new xmldb_field($fieldstr, XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2575                 // Check that the field doesn't already exists
2576                 if (!$dbman->field_exists($table, $field)) {
2577                     // Add the new field
2578                     $dbman->add_field($table, $field);
2579                     // Update the field if the text contains the default FORMAT_MOODLE to FORMAT_HTML
2580                     if (($pos = strpos($fieldstr, 'format'))>0) {
2581                         upgrade_set_timeout(60*20); // this may take a little while
2582                         $params = array(FORMAT_HTML, '<p%', '%<br />%', FORMAT_MOODLE);
2583                         $textfield = substr($fieldstr, 0, $pos);
2584                         $DB->execute('UPDATE {'.$tablestr.'} SET '.$fieldstr.'=? WHERE ('.$textfield.' LIKE ? OR '.$textfield.' LIKE ?) AND '.$fieldstr.'=?', $params);
2585                     }
2586                 }
2587             }
2588         }
2590         unset($extendtables);
2592         upgrade_main_savepoint($result, 2009110400);
2593     }
2595     if ($result && $oldversion < 2009110605) {
2597     /// Define field timecreated to be added to external_services
2598         $table = new xmldb_table('external_services');
2599         $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'component');
2601     /// Conditionally launch add field timecreated
2602         if (!$dbman->field_exists($table, $field)) {
2603             $dbman->add_field($table, $field);
2604         }
2606     /// Define field timemodified to be added to external_services
2607         $table = new xmldb_table('external_services');
2608         $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, 'timecreated');
2610     /// Conditionally launch add field timemodified
2611         if (!$dbman->field_exists($table, $field)) {
2612             $dbman->add_field($table, $field);
2613         }
2615     /// Main savepoint reached
2616         upgrade_main_savepoint($result, 2009110605);
2617     }
2619     if ($result && $oldversion < 2009111600) {
2621     /// Define field instance to be added to portfolio_tempdata
2622         $table = new xmldb_table('portfolio_tempdata');
2623         $field = new xmldb_field('instance', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'userid');
2625     /// Conditionally launch add field instance
2626         if (!$dbman->field_exists($table, $field)) {
2627             $dbman->add_field($table, $field);
2628         }
2630        $key = new xmldb_key('instancefk', XMLDB_KEY_FOREIGN, array('instance'), 'portfolio_instance', array('id'));
2632     /// Launch add key instancefk
2633         $dbman->add_key($table, $key);
2635     /// Main savepoint reached
2636         upgrade_main_savepoint($result, 2009111600);
2637     }
2639     if ($result && $oldversion < 2009111700) {
2641     /// Define field tempdataid to be added to portfolio_log
2642         $table = new xmldb_table('portfolio_log');
2643         $field = new xmldb_field('tempdataid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'caller_sha1');
2645     /// Conditionally launch add field tempdataid
2646         if (!$dbman->field_exists($table, $field)) {
2647             $dbman->add_field($table, $field);
2648         }
2650     /// Main savepoint reached
2651         upgrade_main_savepoint($result, 2009111700);
2652     }
2654     if ($result && $oldversion < 2009111701) {
2656     /// Define field returnurl to be added to portfolio_log
2657         $table = new xmldb_table('portfolio_log');
2658         $field = new xmldb_field('returnurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'tempdataid');
2660     /// Conditionally launch add field returnurl
2661         if (!$dbman->field_exists($table, $field)) {
2662             $dbman->add_field($table, $field);
2663         }
2665     /// Main savepoint reached
2666         upgrade_main_savepoint($result, 2009111701);
2667     }
2669     if ($result && $oldversion < 2009111702) {
2671     /// Define field continueurl to be added to portfolio_log
2672         $table = new xmldb_table('portfolio_log');
2673         $field = new xmldb_field('continueurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'returnurl');
2675     /// Conditionally launch add field continueurl
2676         if (!$dbman->field_exists($table, $field)) {
2677             $dbman->add_field($table, $field);
2678         }
2680     /// Main savepoint reached
2681         upgrade_main_savepoint($result, 2009111702);
2682     }
2684     if ($result && $oldversion < 2009112400) {
2685         if (empty($CFG->passwordsaltmain)) {
2686             $subject = get_string('check_passwordsaltmain_name', 'report_security');
2687             $description = get_string('check_passwordsaltmain_warning', 'report_security');;
2688             upgrade_log(UPGRADE_LOG_NOTICE, null, $subject, $description);
2689         }
2690         upgrade_main_savepoint($result, 2009112400);
2691     }
2693     if ($result && $oldversion < 2010010601) {
2695     /// Define field creatorid to be added to external_tokens
2696         $table = new xmldb_table('external_tokens');
2697         $field = new xmldb_field('creatorid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', 'contextid');
2699     /// Conditionally launch add field creatorid
2700         if (!$dbman->field_exists($table, $field)) {
2701             $dbman->add_field($table, $field);
2702         }
2704     /// Define key creatorid (foreign) to be added to external_tokens
2705         $table = new xmldb_table('external_tokens');
2706         $key = new xmldb_key('creatorid', XMLDB_KEY_FOREIGN, array('creatorid'), 'user', array('id'));
2708     /// Launch add key creatorid
2709         $dbman->add_key($table, $key);
2711     /// Main savepoint reached
2712         upgrade_main_savepoint($result, 2010010601);
2713     }
2715     if ($result && $oldversion < 2010011200) {
2716         $table = new xmldb_table('grade_categories');
2717         $field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
2719         if (!$dbman->field_exists($table, $field)) {
2720             $dbman->add_field($table, $field);
2721         }
2723         upgrade_main_savepoint($result, 2010011200);
2724     }
2727     if ($result && $oldversion < 2010012500) {
2728         upgrade_fix_incorrect_mnethostids();
2729         upgrade_main_savepoint($result, 2010012500);
2730     }
2732     if ($result && $oldversion < 2010012600) {
2733         // do stuff to the mnet table
2734         $table = new xmldb_table('mnet_rpc');
2736         $field = new xmldb_field('parent_type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2737         $dbman->rename_field($table, $field, 'plugintype');
2739         $field = new xmldb_field('parent', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpc_path');
2740         $dbman->rename_field($table, $field, 'pluginname');
2742         $field = new xmldb_field('filename', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'profile');
2743         if (!$dbman->field_exists($table, $field)) {
2744             $dbman->add_field($table, $field);
2745         }
2747         $field = new xmldb_field('classname', XMLDB_TYPE_CHAR, '150', null, null, null, null, 'filename');
2748         if (!$dbman->field_exists($table, $field)) {
2749             $dbman->add_field($table, $field);
2750         }
2752         $field = new xmldb_field('static', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'classname');
2753         if (!$dbman->field_exists($table, $field)) {
2754             $dbman->add_field($table, $field);
2755         }
2757     /// Main savepoint reached
2758         upgrade_main_savepoint($result, 2010012600);
2759     }
2761     if ($result && $oldversion < 2010012900) {
2763     /// Define table mnet_remote_rpc to be created
2764         $table = new xmldb_table('mnet_remote_rpc');
2766     /// Adding fields to table mnet_remote_rpc
2767         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2768         $table->add_field('functionname', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2769         $table->add_field('xmlrpcpath', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null);
2771     /// Adding keys to table mnet_remote_rpc
2772         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2774     /// Conditionally launch create table for mnet_remote_rpc
2775         if (!$dbman->table_exists($table)) {
2776             $dbman->create_table($table);
2777         }
2780     /// Define table mnet_remote_service2rpc to be created
2781         $table = new xmldb_table('mnet_remote_service2rpc');
2783     /// Adding fields to table mnet_remote_service2rpc
2784         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2785         $table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2786         $table->add_field('rpcid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
2788     /// Adding keys to table mnet_remote_service2rpc
2789         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2791     /// Adding indexes to table mnet_remote_service2rpc
2792         $table->add_index('rpcid_serviceid', XMLDB_INDEX_UNIQUE, array('rpcid', 'serviceid'));
2794     /// Conditionally launch create table for mnet_remote_service2rpc
2795         if (!$dbman->table_exists($table)) {
2796             $dbman->create_table($table);
2797         }
2800     /// Rename field function_name on table mnet_rpc to functionname
2801         $table = new xmldb_table('mnet_rpc');
2802         $field = new xmldb_field('function_name', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'id');
2804     /// Launch rename field function_name
2805         $dbman->rename_field($table, $field, 'functionname');
2808     /// Rename field xmlrpc_path on table mnet_rpc to xmlrpcpath
2809         $table = new xmldb_table('mnet_rpc');
2810         $field = new xmldb_field('xmlrpc_path', XMLDB_TYPE_CHAR, '80', null, XMLDB_NOTNULL, null, null, 'function_name');
2812     /// Launch rename field xmlrpc_path
2813         $dbman->rename_field($table, $field, 'xmlrpcpath');
2816     /// Main savepoint reached
2817         upgrade_main_savepoint($result, 2010012900);
2818     }
2820     if ($result && $oldversion < 2010012901) {
2822         /// Define field plugintype to be added to mnet_remote_rpc
2823         $table = new xmldb_table('mnet_remote_rpc');
2824         $field = new xmldb_field('plugintype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'xmlrpcpath');
2826         /// Conditionally launch add field plugintype
2827         if (!$dbman->field_exists($table, $field)) {
2828             $dbman->add_field($table, $field);
2829         }
2831     /// Define field pluginname to be added to mnet_remote_rpc
2832         $field = new xmldb_field('pluginname', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'plugintype');
2834     /// Conditionally launch add field pluginname
2835         if (!$dbman->field_exists($table, $field)) {
2836             $dbman->add_field($table, $field);
2837         }
2839         /// Main savepoint reached
2840         upgrade_main_savepoint($result, 2010012901);
2841     }
2843     if ($result && $oldversion < 2010012902) {
2845     /// Define field enabled to be added to mnet_remote_rpc
2846         $table = new xmldb_table('mnet_remote_rpc');
2847         $field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null, 'pluginname');
2849     /// Conditionally launch add field enabled
2850         if (!$dbman->field_exists($table, $field)) {
2851             $dbman->add_field($table, $field);
2852         }
2854         /// Main savepoint reached
2855         upgrade_main_savepoint($result, 2010012902);
2856     }
2858     /// MDL-17863. Increase the portno column length on mnet_host to handle any port number
2859     if ($result && $oldversion < 2010020100) {
2860     /// Changing precision of field portno on table mnet_host to (5)
2861         $table = new xmldb_table('mnet_host');
2862         $field = new xmldb_field('portno', XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'transport');
2864     /// Launch change of precision for field portno
2865         $dbman->change_field_precision($table, $field);
2867         upgrade_main_savepoint($result, 2010020100);
2868     }
2870     if ($result && $oldversion < 2010020300) {
2872     /// Define field timecreated to be added to user
2873         $table = new xmldb_table('user');
2874         $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'trackforums');
2876         if (!$dbman->field_exists($table, $field)) {
2877         /// Launch add field timecreated
2878             $dbman->add_field($table, $field);
2880             $DB->execute("UPDATE {user} SET timecreated = firstaccess");
2882             $sql = "UPDATE {user} SET timecreated = " . time() ." where timecreated = 0";
2883             $DB->execute($sql);
2884         }
2885         upgrade_main_savepoint($result, 2010020300);
2886     }
2888     // MDL-21407. Trim leading spaces from default tex latexpreamble causing problems under some confs
2889     if ($result && $oldversion < 2010020301) {
2890         if ($preamble = $CFG->filter_tex_latexpreamble) {
2891             $preamble = preg_replace('/^ +/m', '', $preamble);
2892             set_config('filter_tex_latexpreamble', $preamble);
2893         }
2894         upgrade_main_savepoint($result, 2010020301);
2895     }
2897     if ($result && $oldversion < 2010021400) {
2898     /// Changes to modinfo mean we need to rebuild course cache
2899         require_once($CFG->dirroot . '/course/lib.php');
2900         rebuild_course_cache(0, true);
2901         upgrade_main_savepoint($result, 2010021400);
2902     }
2904     if ($result && $oldversion < 2010021800) {
2905         $DB->set_field('mnet_application', 'sso_jump_url', '/auth/mnet/jump.php', array('name' => 'moodle'));
2906         upgrade_main_savepoint($result, 2010021800);
2907     }
2909     if ($result && $oldversion < 2010031900) {
2910         // regeneration of sessions is always enabled, no need for this setting any more
2911         unset_config('regenloginsession');
2912         upgrade_main_savepoint($result, 2010031900);
2913     }
2916     if ($result && $oldversion < 2010032400) {
2917         // Upgrade all of those using the standardold theme to the use the standard
2918         // theme instead
2919         if ($CFG->theme == 'standardold') {
2920             // The config setting that affects the whole site
2921             set_config('theme', 'standard');
2922         }
2923         // Course Categories
2924         $DB->execute('UPDATE {course_categories} SET theme=? WHERE theme=?', array('standard', 'standardold'));
2925         // Course
2926         $DB->execute('UPDATE {course} SET theme=? WHERE theme=?', array('standard', 'standardold'));
2927         // User
2928         $DB->execute('UPDATE {user} SET theme=? WHERE theme=?', array('standard', 'standardold'));
2929         upgrade_main_savepoint($result, 2010032400);
2930     }
2932     if ($result && $oldversion < 2010033101.01) {
2934     /// Define field source to be added to files
2935         $table = new xmldb_table('files');
2937         $field = new xmldb_field('source', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'status');
2939     /// Conditionally launch add field source
2940         if (!$dbman->field_exists($table, $field)) {
2941             $dbman->add_field($table, $field);
2942         }
2944         $field = new xmldb_field('author', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'source');
2946     /// Conditionally launch add field author
2947         if (!$dbman->field_exists($table, $field)) {
2948             $dbman->add_field($table, $field);
2949         }
2951         $field = new xmldb_field('license', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'author');
2953     /// Conditionally launch add field license
2954         if (!$dbman->field_exists($table, $field)) {
2955             $dbman->add_field($table, $field);
2956         }
2958         upgrade_main_savepoint($result, 2010033101.01);
2959     }
2961     if ($result && $oldversion < 2010033101.02) {
2963     /// Define table license to be created
2964         $table = new xmldb_table('license');
2966     /// Adding fields to table license
2967         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2968         $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2969         $table->add_field('fullname', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
2970         $table->add_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null);
2971         $table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1');
2972         $table->add_field('version', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2974     /// Adding keys to table license
2975         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2977     /// Conditionally launch create table for license
2978         if (!$dbman->table_exists($table)) {
2979             $dbman->create_table($table);
2980         }
2981         $active_licenses = array();
2983         $license = new stdclass;
2985         // add unknown license
2986         $license->shortname = 'unknown';
2987         $license->fullname = 'Unknown license';
2988         $license->source = '';
2989         $license->enabled = 1;
2990         $license->version = '2010033100';
2991         $active_licenses[] = $license->shortname;
2992         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
2993             if ($record->version < $license->version) {
2994                 // update license record
2995                 $license->enabled = $record->enabled;
2996                 $license->id = $record->id;
2997                 $DB->update_record('license', $license);
2998             }
2999         } else {
3000             $DB->insert_record('license', $license);
3001         }
3003         // add all rights reserved license
3004         $license->shortname = 'allrightsreserved';
3005         $license->fullname = 'All rights reserved';
3006         $license->source = 'http://en.wikipedia.org/wiki/All_rights_reserved';
3007         $license->enabled = 1;
3008         $license->version = '2010033100';
3009         $active_licenses[] = $license->shortname;
3010         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3011             if ($record->version < $license->version) {
3012                 // update license record
3013                 $license->id = $record->id;
3014                 $license->enabled = $record->enabled;
3015                 $DB->update_record('license', $license);
3016             }
3017         } else {
3018             $DB->insert_record('license', $license);
3019         }
3021         // add public domain license
3022         $license->shortname = 'public';
3023         $license->fullname = 'Public Domain';
3024         $license->source = 'http://creativecommons.org/licenses/publicdomain/';
3025         $license->enabled = 1;
3026         $license->version = '2010033100';
3027         $active_licenses[] = $license->shortname;
3028         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3029             if ($record->version < $license->version) {
3030                 // update license record
3031                 $license->enabled = $record->enabled;
3032                 $license->id = $record->id;
3033                 $DB->update_record('license', $license);
3034             }
3035         } else {
3036             $DB->insert_record('license', $license);
3037         }
3039         // add creative commons license
3040         $license->shortname = 'cc';
3041         $license->fullname = 'Creative Commons';
3042         $license->source = 'http://creativecommons.org/licenses/by/3.0/';
3043         $license->enabled = 1;
3044         $license->version = '2010033100';
3045         $active_licenses[] = $license->shortname;
3046         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3047             if ($record->version < $license->version) {
3048                 // update license record
3049                 $license->enabled = $record->enabled;
3050                 $license->id = $record->id;
3051                 $DB->update_record('license', $license);
3052             }
3053         } else {
3054             $DB->insert_record('license', $license);
3055         }
3057         // add creative commons no derivs license
3058         $license->shortname = 'cc-nd';
3059         $license->fullname = 'Creative Commons - NoDerivs';
3060         $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3061         $license->enabled = 1;
3062         $license->version = '2010033100';
3063         $active_licenses[] = $license->shortname;
3064         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3065             if ($record->version < $license->version) {
3066                 // update license record
3067                 $license->enabled = $record->enabled;
3068                 $license->id = $record->id;
3069                 $DB->update_record('license', $license);
3070             }
3071         } else {
3072             $DB->insert_record('license', $license);
3073         }
3075         // add creative commons no commercial no derivs license
3076         $license->shortname = 'cc-nc-nd';
3077         $license->fullname = 'Creative Commons - No Commercial NoDerivs';
3078         $license->source = 'http://creativecommons.org/licenses/by-nc-nd/3.0/';
3079         $license->enabled = 1;
3080         $license->version = '2010033100';
3081         $active_licenses[] = $license->shortname;
3082         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3083             if ($record->version < $license->version) {
3084                 // update license record
3085                 $license->enabled = $record->enabled;
3086                 $license->id = $record->id;
3087                 $DB->update_record('license', $license);
3088             }
3089         } else {
3090             $DB->insert_record('license', $license);
3091         }
3093         // add creative commons no commercial
3094         $license->shortname = 'cc-nc-nd';
3095         $license->shortname = 'cc-nc';
3096         $license->fullname = 'Creative Commons - No Commercial';
3097         $license->source = 'http://creativecommons.org/licenses/by-nd/3.0/';
3098         $license->enabled = 1;
3099         $license->version = '2010033100';
3100         $active_licenses[] = $license->shortname;
3101         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3102             if ($record->version < $license->version) {
3103                 // update license record
3104                 $license->enabled = $record->enabled;
3105                 $license->id = $record->id;
3106                 $DB->update_record('license', $license);
3107             }
3108         } else {
3109             $DB->insert_record('license', $license);
3110         }
3112         // add creative commons no commercial sharealike
3113         $license->shortname = 'cc-nc-sa';
3114         $license->fullname = 'Creative Commons - No Commercial ShareAlike';
3115         $license->source = 'http://creativecommons.org/licenses/by-nc-sa/3.0/';
3116         $license->enabled = 1;
3117         $license->version = '2010033100';
3118         $active_licenses[] = $license->shortname;
3119         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3120             if ($record->version < $license->version) {
3121                 // update license record
3122                 $license->enabled = $record->enabled;
3123                 $license->id = $record->id;
3124                 $DB->update_record('license', $license);
3125             }
3126         } else {
3127             $DB->insert_record('license', $license);
3128         }
3130         // add creative commons sharealike
3131         $license->shortname = 'cc-sa';
3132         $license->fullname = 'Creative Commons - ShareAlike';
3133         $license->source = 'http://creativecommons.org/licenses/by-sa/3.0/';
3134         $license->enabled = 1;
3135         $license->version = '2010033100';
3136         $active_licenses[] = $license->shortname;
3137         if ($record = $DB->get_record('license', array('shortname'=>$license->shortname))) {
3138             if ($record->version < $license->version) {
3139                 // update license record
3140                 $license->enabled = $record->enabled;
3141                 $license->id = $record->id;
3142                 $DB->update_record('license', $license);
3143             }
3144         } else {
3145             $DB->insert_record('license', $license);
3146         }
3148         set_config('licenses', implode(',', $active_licenses));
3149     /// set site default license
3150         set_config('sitedefaultlicense', 'allrightsreserved');
3152     /// Main savepoint reached
3153         upgrade_main_savepoint($result, 2010033101.02);
3154     }
3156     if ($result && $oldversion < 2010033102.00) {
3157         // rename course view capability to participate
3158         $params = array('view'=>'moodle/course:view', 'participate'=>'moodle/course:participate');
3159         $sql = "UPDATE {role_capabilities} SET capability = :participate WHERE capability = :view";
3160         $DB->execute($sql, $params);
3161         $sql = "UPDATE {capabilities} SET name = :participate WHERE name = :view";
3162         $DB->execute($sql, $params);
3163         // note: the view capability is readded again at the end of upgrade, but with different meaning
3164         upgrade_main_savepoint($result, 2010033102.00);
3165     }
3167     if ($result && $oldversion < 2010033102.01) {
3168         // Define field archetype to be added to role table
3169         $table = new xmldb_table('role');
3170         $field = new xmldb_field('archetype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'sortorder');
3171         $dbman->add_field($table, $field);
3172         upgrade_main_savepoint($result, 2010033102.01);
3173     }
3175     if ($result && $oldversion < 2010033102.02) {
3176         // Set archetype for existing roles and change admin role to manager role
3177         $sql = "SELECT r.*, rc.capability
3178                   FROM {role} r
3179                   JOIN {role_capabilities} rc ON rc.roleid = r.id
3180                  WHERE rc.contextid = :syscontextid AND rc.capability LIKE :legacycaps
3181               ORDER BY r.id";
3182         $params = array('syscontextid'=>SYSCONTEXTID, 'legacycaps'=>'moodle/legacy:%');
3183         $substart = strlen('moodle/legacy:');
3184         $roles = $DB->get_recordset_sql($sql, $params); // in theory could be multiple legacy flags in one role
3185         foreach ($roles as $role) {
3186             $role->archetype = substr($role->capability, $substart);
3187             unset($role->capability);
3188             if ($role->archetype === 'admin') {
3189                 $role->archetype = 'manager';
3190                 if ($role->shortname === 'admin') {
3191                     $role->shortname   = 'manager';
3192                     $role->name        = get_string('manager', 'role');
3193                     $role->description = get_string('managerdescription', 'role');
3194                 }
3195             }
3196             $DB->update_record('role', $role);
3197         }
3198         $roles->close();
3200         upgrade_main_savepoint($result, 2010033102.02);
3201     }
3203     if ($result && $oldversion < 2010033102.03) {
3204         // Now pick site admins (===have manager role assigned at the system context)
3205         // and store them in the new $CFG->siteadmins setting as comma separated list
3206         $sql = "SELECT ra.id, ra.userid
3207                   FROM {role_assignments} ra
3208                   JOIN {role} r ON r.id = ra.roleid
3209                   JOIN {user} u ON u.id = ra.userid
3210                  WHERE ra.contextid = :syscontext AND r.archetype = 'manager' AND u.deleted = 0
3211               ORDER BY ra.id";
3212         $ras = $DB->get_records_sql($sql, array('syscontext'=>SYSCONTEXTID));
3213         $admins = array();
3214         foreach ($ras as $ra) {
3215             $admins[$ra->userid] = $ra->userid;
3216             set_config('siteadmins', implode(',', $admins)); // better to save it repeatedly, we do need at least one admin
3217             $DB->delete_records('role_assignments', array('id'=>$ra->id));
3218         }
3220         upgrade_main_savepoint($result, 2010033102.03);
3221     }
3223     if ($result && $oldversion < 2010033102.04) {
3224         // clean up the manager roles
3225         $managers = $DB->get_records('role', array('archetype'=>'manager'));
3226         foreach ($managers as $manager) {
3227             // now sanitize the capabilities and overrides
3228             $DB->delete_records('role_capabilities', array('capability'=>'moodle/site:config', 'roleid'=>$manager->id)); // only site admins may configure servers
3229             // note: doanything and legacy caps are deleted automatically, they get moodle/course:view later at the end of the upgrade
3231             // remove manager role assignments bellow the course context level - admin role was never intended for activities and blocks,
3232             // the problem is that those assignments would not be visible after upgrade and old style admins in activities make no sense anyway
3233             $DB->delete_records_select('role_assignments', "roleid = :manager AND contextid IN (SELECT id FROM {context} WHERE contextlevel > 50)", array('manager'=>$manager->id));
3235             // allow them to assign all roles except default user, guest and frontpage - users get these roles automatically on the fly when needed
3236             $DB->delete_records('role_allow_assign', array('roleid'=>$manager->id));
3237             $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype <> 'user' AND archetype <> 'guest' AND archetype <> 'frontpage'");
3238             foreach ($roles as $role) {
3239                 $record = (object)array('roleid'=>$manager->id, 'allowassign'=>$role->id);
3240                 $DB->insert_record('role_allow_assign', $record);
3241             }
3243             // allow them to override all roles
3244             $DB->delete_records('role_allow_override', array('roleid'=>$manager->id));
3245             $roles = $DB->get_records_sql("SELECT * FROM {role}");
3246             foreach ($roles as $role) {
3247                 $record = (object)array('roleid'=>$manager->id, 'allowoverride'=>$role->id);
3248                 $DB->insert_record('role_allow_override', $record);
3249             }
3251             // allow them to switch to all following roles
3252             $DB->delete_records('role_allow_switch', array('roleid'=>$manager->id));
3253             $roles = $DB->get_records_sql("SELECT * FROM {role} WHERE archetype IN ('student', 'teacher', 'editingteacher')");
3254             foreach ($roles as $role) {
3255                 $record = (object)array('roleid'=>$manager->id, 'allowswitch'=>$role->id);
3256                 $DB->insert_record('role_allow_switch', $record);
3257             }
3258         }
3260         upgrade_main_savepoint($result, 2010033102.04);
3261     }
3263     if ($result && $oldversion < 2010033102.05) {
3264         // remove course:view from all roles that are not used for enrolment, it does NOT belong there because it really means user is enrolled!
3265         $noenrolroles = $DB->get_records_select('role', "archetype IN ('guest', 'user', 'manager', 'coursecreator', 'frontpage')");
3266         foreach ($noenrolroles as $role) {
3267             $DB->delete_records('role_capabilities', array('roleid'=>$role->id, 'capability'=>'moodle/course:participate'));
3268         }
3269         upgrade_main_savepoint($result, 2010033102.05);
3270     }
3272     if ($result && $oldversion < 2010033102.06) {
3273         // make sure there is nothing weird in default user role
3274         if (!empty($CFG->defaultuserroleid)) {
3275             if ($role = $DB->get_record('role', array('id'=>$CFG->defaultuserroleid))) {
3276                 if ($role->archetype !== '' and $role->archetype !== 'user') {
3277                     upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default authenticated user role (defaultuserroleid) value is invalid, setting cleared.');
3278                     unset_config('defaultuserroleid');
3279                 }
3280             } else {
3281                 unset_config('defaultuserroleid');
3282             }
3283         }
3284         upgrade_main_savepoint($result, 2010033102.06);
3285     }
3287     if ($result && $oldversion < 2010033102.07) {
3288         if (!empty($CFG->displayloginfailures) and $CFG->displayloginfailures === 'teacher') {
3289             upgrade_log(UPGRADE_LOG_NOTICE, null, 'Displaying of login failuters to teachers is not supported any more.');
3290             unset_config('displayloginfailures');
3291         }
3292         upgrade_main_savepoint($result, 2010033102.07);
3293     }
3295     if ($result && $oldversion < 2010033102.08) {
3296         // make sure there are no problems in default guest role settings
3297         if (!empty($CFG->guestroleid)) {
3298             if ($role = $DB->get_record('role', array('id'=>$CFG->guestroleid))) {
3299                 if ($role->archetype !== '' and $role->archetype !== 'guest') {
3300                     upgrade_log(UPGRADE_LOG_NOTICE, null, 'Default guest role (guestroleid) value is invalid, setting cleared.');
3301                     unset_config('guestroleid');
3302                 }
3303             } else {
3304                 upgrade_log(UPGRADE_LOG_NOTICE, null, 'Role specified in Default guest role (guestroleid) does not exist, setting cleared.');
3305                 unset_config('guestroleid');
3306             }
3307         }
3308         // remove all roles of the guest account - the only way to change it is to override the guest role, sorry
3309         // the guest account gets all the role assignments on the fly which works fine in has_capability(),
3310         $DB->delete_records_select('role_assignments', "userid IN (SELECT id FROM {user} WHERE username = 'guest')");
3312         upgrade_main_savepoint($result, 2010033102.08);
3313     }
3315     /// New table for storing which roles can be assigned in which contexts.
3316     if ($result && $oldversion < 2010033102.09) {
3318     /// Define table role_context_levels to be created
3319         $table = new xmldb_table('role_context_levels');
3321     /// Adding fields to table role_context_levels
3322         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3323         $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3324         $table->add_field('contextlevel', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
3326     /// Adding keys to table role_context_levels
3327         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3328         $table->add_key('contextlevel-roleid', XMLDB_KEY_UNIQUE, array('contextlevel', 'roleid'));
3329         $table->add_key('roleid', XMLDB_KEY_FOREIGN, array('roleid'), 'role', array('id'));
3331     /// Conditionally launch create table for role_context_levels
3332         if (!$dbman->table_exists($table)) {
3333             $dbman->create_table($table);
3334         }
3336     /// Main savepoint reached
3337         upgrade_main_savepoint($result, 2010033102.09);
3338     }
3340     if ($result && $oldversion < 2010033102.10) {
3341         // Now populate the role_context_levels table with the default values
3342         // NOTE: do not use accesslib methods here
3344         $rolecontextlevels = array();
3345         $defaults = array('manager'        => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT, CONTEXT_COURSE),
3346                           'coursecreator'  => array(CONTEXT_SYSTEM, CONTEXT_COURSECAT),
3347                           'editingteacher' => array(CONTEXT_COURSE, CONTEXT_MODULE),
3348                           'teacher'        => array(CONTEXT_COURSE, CONTEXT_MODULE),
3349                           'student'        => array(CONTEXT_COURSE, CONTEXT_MODULE),
3350                           'guest'          => array(),
3351                           'user'           => array(),
3352                           'frontpage'      => array());
3354         $roles = $DB->get_records('role', array(), '', 'id, archetype');
3355         foreach ($roles as $role) {
3356             if (isset($defaults[$role->archetype])) {
3357                 $rolecontextlevels[$role->id] = $defaults[$role->archetype];
3358             }
3359         }
3361         // add roles without archetypes, it may contain weird things, but we can not fix them
3362         $sql = "SELECT DISTINCT ra.roleid, con.contextlevel
3363                   FROM {role_assignments} ra
3364                   JOIN {context} con ON ra.contextid = con.id";
3365         $existingrolecontextlevels = $DB->get_recordset_sql($sql);
3366         foreach ($existingrolecontextlevels as $rcl) {
3367             if (isset($roleids[$rcl->roleid])) {
3368                 continue;
3369             }
3370             if (!isset($rolecontextlevels[$rcl->roleid])) {
3371                 $rolecontextlevels[$rcl->roleid] = array($rcl->contextlevel);
3372             } else if (!in_array($rcl->contextlevel, $rolecontextlevels[$rcl->roleid])) {
3373                 $rolecontextlevels[$rcl->roleid][] = $rcl->contextlevel;
3374             }
3375         }
3376         $existingrolecontextlevels->close();
3378         // Put the data into the database.
3379         $rcl = new object();
3380         foreach ($rolecontextlevels as $roleid => $contextlevels) {
3381             $rcl->roleid = $roleid;
3382             foreach ($contextlevels as $level) {
3383                 $rcl->contextlevel = $level;
3384                 $DB->insert_record('role_context_levels', $rcl, false);
3385             }
3386         }
3388         // release memory!!
3389         unset($roles);
3390         unset($defaults);
3391         unset($rcl);
3392         unset($existingrolecontextlevels);
3393         unset($rolecontextlevels);
3395         // Main savepoint reached
3396         upgrade_main_savepoint($result, 2010033102.10);
3397     }
3399     if ($result && $oldversion < 2010040700) {
3400         // migrate old groupings --> groupmembersonly setting
3401         if (isset($CFG->enablegroupings)) {
3402             set_config('enablegroupmembersonly', $CFG->enablegroupings);
3403             unset_config('enablegroupings');
3404         }
3406         // Main savepoint reached
3407         upgrade_main_savepoint($result, 2010040700);
3408     }
3410     if ($result && $oldversion < 2010040900) {
3412         // Changing the default of field lang on table user to good old "en"
3413         $table = new xmldb_table('user');
3414         $field = new xmldb_field('lang', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'en', 'country');
3416         // Launch change of default for field lang
3417         $dbman->change_field_default($table, $field);
3419         // update main site lang
3420         if (strpos($CFG->lang, '_utf8') !== false) {
3421             $lang = str_replace('_utf8', '', $CFG->lang);
3422             set_config('lang', $lang);
3423         }