weekly release 2.6dev
[moodle.git] / lib / db / upgrade.php
CommitLineData
5b4a78e2 1<?php
5b4a78e2 2// This file is part of Moodle - http://moodle.org/
4e423cbf 3//
5b4a78e2
PS
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
4e423cbf 8//
5b4a78e2
PS
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
4e423cbf 13//
5b4a78e2
PS
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This file keeps track of upgrades to Moodle.
19 *
20 * Sometimes, changes between versions involve
21 * alterations to database structures and other
22 * major things that may break installations.
23 *
24 * The upgrade function in this file will attempt
25 * to perform all the necessary actions to upgrade
26 * your older installation to the current version.
27 *
28 * If there's something it cannot do itself, it
29 * will tell you what you need to do.
30 *
31 * The commands in here will all be database-neutral,
32 * using the methods of database_manager class
33 *
34 * Please do not forget to use upgrade_set_timeout()
35 * before any action that may take longer time to finish.
36 *
39b90b51
EL
37 * @package core_install
38 * @category upgrade
39 * @copyright 2006 onwards Martin Dougiamas http://dougiamas.com
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5b4a78e2
PS
41 */
42
43defined('MOODLE_INTERNAL') || die();
4e423cbf 44
3406acde 45/**
39b90b51
EL
46 * Main upgrade tasks to be executed on Moodle version bump
47 *
48 * This function is automatically executed after one bump in the Moodle core
f9488a6f 49 * version is detected. It's in charge of performing the required tasks
39b90b51
EL
50 * to raise core from the previous version to the next one.
51 *
52 * It's a collection of ordered blocks of code, named "upgrade steps",
f9488a6f 53 * each one performing one isolated (from the rest of steps) task. Usually
39b90b51
EL
54 * tasks involve creating new DB objects or performing manipulation of the
55 * information for cleanup/fixup purposes.
56 *
57 * Each upgrade step has a fixed structure, that can be summarised as follows:
58 *
59 * if ($oldversion < XXXXXXXXXX.XX) {
f9488a6f 60 * // Explanation of the update step, linking to issue in the Tracker if necessary
39b90b51
EL
61 * upgrade_set_timeout(XX); // Optional for big tasks
62 * // Code to execute goes here, usually the XMLDB Editor will
63 * // help you here. See {@link http://docs.moodle.org/dev/XMLDB_editor}.
64 * upgrade_main_savepoint(true, XXXXXXXXXX.XX);
65 * }
66 *
67 * All plugins within Moodle (modules, blocks, reports...) support the existence of
68 * their own upgrade.php file, using the "Frankenstyle" component name as
69 * defined at {@link http://docs.moodle.org/dev/Frankenstyle}, for example:
70 * - {@link xmldb_page_upgrade($oldversion)}. (modules don't require the plugintype ("mod_") to be used.
71 * - {@link xmldb_auth_manual_upgrade($oldversion)}.
72 * - {@link xmldb_workshopform_accumulative_upgrade($oldversion)}.
73 * - ....
74 *
75 * In order to keep the contents of this file reduced, it's allowed to create some helper
76 * functions to be used here in the {@link upgradelib.php} file at the same directory. Note
f9488a6f 77 * that such a file must be manually included from upgrade.php, and there are some restrictions
39b90b51
EL
78 * about what can be used within it.
79 *
80 * For more information, take a look to the documentation available:
81 * - Data definition API: {@link http://docs.moodle.org/dev/Data_definition_API}
82 * - Upgrade API: {@link http://docs.moodle.org/dev/Upgrade_API}
3406acde 83 *
3406acde 84 * @param int $oldversion
5b4a78e2 85 * @return bool always true
3406acde 86 */
775f811a 87function xmldb_main_upgrade($oldversion) {
3713514b 88 global $CFG, $USER, $DB, $OUTPUT, $SITE;
4e423cbf 89
4f12838e 90 require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions
13a0d3d3 91
46d318bc 92 $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
f33e1ed4 93
5c79b8ed
PS
94 if ($oldversion < 2011120500) {
95 // just in case somebody hacks upgrade scripts or env, we really can not continue
96 echo("You need to upgrade to 2.2.x first!\n");
97 exit(1);
ae95489d
EL
98 // Note this savepoint is 100% unreachable, but needed to pass the upgrade checks
99 upgrade_main_savepoint(true, 2011120500);
5c79b8ed
PS
100 }
101
39b90b51
EL
102 // Moodle v2.2.0 release upgrade line
103 // Put any upgrade step following this
da4aa3e4 104
43cf3165
EL
105 if ($oldversion < 2011120500.02) {
106
107 upgrade_set_timeout(60*20); // This may take a while
108 // MDL-28180. Some missing restrictions in certain backup & restore operations
109 // were causing incorrect duplicates in the course_completion_aggr_methd table.
110 // This upgrade step takes rid of them.
111 $sql = 'SELECT course, criteriatype, MIN(id) AS minid
112 FROM {course_completion_aggr_methd}
113 GROUP BY course, criteriatype
114 HAVING COUNT(*) > 1';
115 $duprs = $DB->get_recordset_sql($sql);
116 foreach ($duprs as $duprec) {
117 // We need to handle NULLs in criteriatype diferently
118 if (is_null($duprec->criteriatype)) {
119 $where = 'course = ? AND criteriatype IS NULL AND id > ?';
120 $params = array($duprec->course, $duprec->minid);
121 } else {
122 $where = 'course = ? AND criteriatype = ? AND id > ?';
123 $params = array($duprec->course, $duprec->criteriatype, $duprec->minid);
124 }
125 $DB->delete_records_select('course_completion_aggr_methd', $where, $params);
126 }
127 $duprs->close();
128
129 // Main savepoint reached
130 upgrade_main_savepoint(true, 2011120500.02);
131 }
132
52a23063 133 if ($oldversion < 2011120500.03) {
8e54ce97
AD
134
135 // Changing precision of field value on table user_preferences to (1333)
136 $table = new xmldb_table('user_preferences');
137 $field = new xmldb_field('value', XMLDB_TYPE_CHAR, '1333', null, XMLDB_NOTNULL, null, null, 'name');
138
139 // Launch change of precision for field value
140 $dbman->change_field_precision($table, $field);
141
142 // Main savepoint reached
52a23063 143 upgrade_main_savepoint(true, 2011120500.03);
8e54ce97
AD
144 }
145
91331b6b
MG
146 if ($oldversion < 2012020200.03) {
147
148 // Define index rolecontext (not unique) to be added to role_assignments
149 $table = new xmldb_table('role_assignments');
150 $index = new xmldb_index('rolecontext', XMLDB_INDEX_NOTUNIQUE, array('roleid', 'contextid'));
151
152 // Conditionally launch add index rolecontext
153 if (!$dbman->index_exists($table, $index)) {
154 $dbman->add_index($table, $index);
155 }
156
157 // Define index usercontextrole (not unique) to be added to role_assignments
158 $index = new xmldb_index('usercontextrole', XMLDB_INDEX_NOTUNIQUE, array('userid', 'contextid', 'roleid'));
159
160 // Conditionally launch add index usercontextrole
161 if (!$dbman->index_exists($table, $index)) {
162 $dbman->add_index($table, $index);
163 }
164
165 // Main savepoint reached
166 upgrade_main_savepoint(true, 2012020200.03);
167 }
168
8900213b
AD
169 if ($oldversion < 2012020200.06) {
170 // Previously we always allowed users to override their email address via the messaging system
171 // We have now added a setting to allow admins to turn this this ability on and off
172 // While this setting defaults to 0 (off) we're setting it to 1 (on) to maintain the behaviour for upgrading sites
173 set_config('messagingallowemailoverride', 1);
174
175 // Main savepoint reached
176 upgrade_main_savepoint(true, 2012020200.06);
177 }
178
bc5fbbb3 179 if ($oldversion < 2012021700.01) {
669bdcab
RT
180 // Changing precision of field uniquehash on table post to 255
181 $table = new xmldb_table('post');
182 $field = new xmldb_field('uniquehash', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'content');
183
184 // Launch change of precision for field uniquehash
185 $dbman->change_field_precision($table, $field);
186
187 // Main savepoint reached
bc5fbbb3 188 upgrade_main_savepoint(true, 2012021700.01);
669bdcab
RT
189 }
190
aa803ceb
RT
191 if ($oldversion < 2012021700.02) {
192 // Somewhere before 1.9 summary and content column in post table were not null. In 1.9+
193 // not null became false.
194 $columns = $DB->get_columns('post');
195
196 // Fix discrepancies in summary field after upgrade from 1.9
197 if (array_key_exists('summary', $columns) && $columns['summary']->not_null != false) {
198 $table = new xmldb_table('post');
199 $summaryfield = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'subject');
5f5ee191 200
aa803ceb
RT
201 if ($dbman->field_exists($table, $summaryfield)) {
202 $dbman->change_field_notnull($table, $summaryfield);
203 }
204
205 }
206
207 // Fix discrepancies in content field after upgrade from 1.9
208 if (array_key_exists('content', $columns) && $columns['content']->not_null != false) {
209 $table = new xmldb_table('post');
210 $contentfield = new xmldb_field('content', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'summary');
211
212 if ($dbman->field_exists($table, $contentfield)) {
213 $dbman->change_field_notnull($table, $contentfield);
214 }
215
216 }
217
218 upgrade_main_savepoint(true, 2012021700.02);
219 }
220
f8822462
EL
221 // The ability to backup user (private) files is out completely - MDL-29248
222 if ($oldversion < 2012030100.01) {
223 unset_config('backup_general_user_files', 'backup');
224 unset_config('backup_general_user_files_locked', 'backup');
225 unset_config('backup_auto_user_files', 'backup');
226
227 upgrade_main_savepoint(true, 2012030100.01);
228 }
229
436650b0 230 if ($oldversion < 2012030900.01) {
801ae0f4
PS
231 // Migrate all numbers to signed & all texts and binaries to big size.
232 // It should be safe to interrupt this and continue later.
233 upgrade_mysql_fix_unsigned_and_lob_columns();
436650b0
PS
234
235 // Main savepoint reached
236 upgrade_main_savepoint(true, 2012030900.01);
237 }
238
9665ecd2
TH
239 if ($oldversion < 2012031500.01) {
240 // Upgrade old course_allowed_modules data to be permission overrides.
6f3570ff 241 if ($CFG->restrictmodulesfor === 'all') {
9665ecd2 242 $courses = $DB->get_records_menu('course', array(), 'id', 'id, 1');
6f3570ff 243 } else if ($CFG->restrictmodulesfor === 'requested') {
e08eaf7d 244 $courses = $DB->get_records_menu('course', array('restrictmodules' => 1), 'id', 'id, 1');
9665ecd2
TH
245 } else {
246 $courses = array();
247 }
248
249 if (!$dbman->table_exists('course_allowed_modules')) {
250 // Upgrade must already have been run on this server. This might happen,
251 // for example, during development of these changes.
252 $courses = array();
253 }
254
255 $modidtoname = $DB->get_records_menu('modules', array(), 'id', 'id, name');
256
257 $coursecount = count($courses);
258 if ($coursecount) {
259 $pbar = new progress_bar('allowedmods', 500, true);
260 $transaction = $DB->start_delegated_transaction();
261 }
262
263 $i = 0;
264 foreach ($courses as $courseid => $notused) {
265 $i += 1;
266 upgrade_set_timeout(60); // 1 minute per course should be fine.
267
268 $allowedmoduleids = $DB->get_records_menu('course_allowed_modules',
269 array('course' => $courseid), 'module', 'module, 1');
270 if (empty($allowedmoduleids)) {
271 // This seems to be the best match for backwards compatibility,
8149d624 272 // not necessarily with the old code in course_allowed_module function,
9665ecd2
TH
273 // but with the code that used to be in the coures settings form.
274 $allowedmoduleids = explode(',', $CFG->defaultallowedmodules);
275 $allowedmoduleids = array_combine($allowedmoduleids, $allowedmoduleids);
276 }
277
278 $context = context_course::instance($courseid);
279
280 list($roleids) = get_roles_with_cap_in_context($context, 'moodle/course:manageactivities');
281 list($managerroleids) = get_roles_with_cap_in_context($context, 'moodle/site:config');
282 foreach ($managerroleids as $roleid) {
283 unset($roleids[$roleid]);
284 }
285
286 foreach ($modidtoname as $modid => $modname) {
287 if (isset($allowedmoduleids[$modid])) {
288 // Module is allowed, no worries.
289 continue;
290 }
291
292 $capability = 'mod/' . $modname . ':addinstance';
293 foreach ($roleids as $roleid) {
294 assign_capability($capability, CAP_PREVENT, $roleid, $context);
295 }
296 }
297
298 $pbar->update($i, $coursecount, "Upgrading legacy course_allowed_modules data - $i/$coursecount.");
299 }
300
301 if ($coursecount) {
302 $transaction->allow_commit();
303 }
304
305 upgrade_main_savepoint(true, 2012031500.01);
306 }
307
308 if ($oldversion < 2012031500.02) {
309
e08eaf7d 310 // Define field restrictmodules to be dropped from course
9665ecd2 311 $table = new xmldb_table('course');
4354486d 312 $field = new xmldb_field('restrictmodules');
9665ecd2
TH
313
314 // Conditionally launch drop field requested
315 if ($dbman->field_exists($table, $field)) {
316 $dbman->drop_field($table, $field);
317 }
318
319 upgrade_main_savepoint(true, 2012031500.02);
320 }
321
322 if ($oldversion < 2012031500.03) {
323
324 // Define table course_allowed_modules to be dropped
325 $table = new xmldb_table('course_allowed_modules');
326
327 // Conditionally launch drop table for course_allowed_modules
328 if ($dbman->table_exists($table)) {
329 $dbman->drop_table($table);
330 }
331
332 upgrade_main_savepoint(true, 2012031500.03);
333 }
334
335 if ($oldversion < 2012031500.04) {
336 // Clean up the old admin settings.
337 unset_config('restrictmodulesfor');
338 unset_config('restrictbydefault');
339 unset_config('defaultallowedmodules');
340
341 upgrade_main_savepoint(true, 2012031500.04);
342 }
e9e4a4a6 343
e63f95d7
PS
344 if ($oldversion < 2012032300.02) {
345 // Migrate the old admin debug setting.
346 if ($CFG->debug == 38911) {
347 set_config('debug', DEBUG_DEVELOPER);
348 } else if ($CFG->debug == 6143) {
349 set_config('debug', DEBUG_ALL);
350 }
351 upgrade_main_savepoint(true, 2012032300.02);
352 }
353
e7e0f8d2
DP
354 if ($oldversion < 2012042300.00) {
355 // This change makes the course_section index unique.
356
cf76b335 357 // xmldb does not allow changing index uniqueness - instead we must drop
e7e0f8d2
DP
358 // index then add it again
359 $table = new xmldb_table('course_sections');
cf76b335 360 $index = new xmldb_index('course_section', XMLDB_INDEX_NOTUNIQUE, array('course', 'section'));
e7e0f8d2
DP
361
362 // Conditionally launch drop index course_section
363 if ($dbman->index_exists($table, $index)) {
364 $dbman->drop_index($table, $index);
365 }
cf76b335 366
367 // Look for any duplicate course_sections entries. There should not be
368 // any but on some busy systems we found a few, maybe due to previous
369 // bugs.
370 $transaction = $DB->start_delegated_transaction();
371 $rs = $DB->get_recordset_sql('
372 SELECT DISTINCT
373 cs.id, cs.course
374 FROM
375 {course_sections} cs
376 INNER JOIN {course_sections} older
377 ON cs.course = older.course AND cs.section = older.section
378 AND older.id < cs.id');
379 foreach ($rs as $rec) {
380 $DB->delete_records('course_sections', array('id' => $rec->id));
026082b4
DP
381 // We can't use rebuild_course_cache() here because introducing sectioncache later
382 // so reset modinfo manually.
383 $DB->set_field('course', 'modinfo', null, array('id' => $rec->course));
cf76b335 384 }
385 $rs->close();
386 $transaction->allow_commit();
387
388 // Define index course_section (unique) to be added to course_sections
389 $index = new xmldb_index('course_section', XMLDB_INDEX_UNIQUE, array('course', 'section'));
390
391 // Conditionally launch add index course_section
392 if (!$dbman->index_exists($table, $index)) {
393 $dbman->add_index($table, $index);
394 }
395
396 // Main savepoint reached
e7e0f8d2 397 upgrade_main_savepoint(true, 2012042300.00);
cf76b335 398 }
296c0247 399
ede323e2 400 if ($oldversion < 2012042300.02) {
60829d80 401 require_once($CFG->dirroot.'/completion/criteria/completion_criteria.php');
ede323e2
ARN
402 // Delete orphaned criteria which were left when modules were removed
403 if ($DB->get_dbfamily() === 'mysql') {
404 $sql = "DELETE cc FROM {course_completion_criteria} cc
405 LEFT JOIN {course_modules} cm ON cm.id = cc.moduleinstance
406 WHERE cm.id IS NULL AND cc.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
407 } else {
408 $sql = "DELETE FROM {course_completion_criteria}
409 WHERE NOT EXISTS (
410 SELECT 'x' FROM {course_modules}
411 WHERE {course_modules}.id = {course_completion_criteria}.moduleinstance)
2ea2549e 412 AND {course_completion_criteria}.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
ede323e2
ARN
413 }
414 $DB->execute($sql);
415
416 // Main savepoint reached
417 upgrade_main_savepoint(true, 2012042300.02);
418 }
419
0dda790c 420 if ($oldversion < 2012050300.01) {
fa530687
PS
421 // Make sure deleted users do not have picture flag.
422 $DB->set_field('user', 'picture', 0, array('deleted'=>1, 'picture'=>1));
0dda790c 423 upgrade_main_savepoint(true, 2012050300.01);
fa530687
PS
424 }
425
0dda790c 426 if ($oldversion < 2012050300.02) {
4d254790
PS
427
428 // Changing precision of field picture on table user to (10)
429 $table = new xmldb_table('user');
430 $field = new xmldb_field('picture', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'secret');
431
432 // Launch change of precision for field picture
433 $dbman->change_field_precision($table, $field);
434
435 // Main savepoint reached
0dda790c 436 upgrade_main_savepoint(true, 2012050300.02);
4d254790
PS
437 }
438
1156d38d
DP
439 if ($oldversion < 2012050300.03) {
440
441 // Define field coursedisplay to be added to course
442 $table = new xmldb_table('course');
443 $field = new xmldb_field('coursedisplay', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'completionnotify');
444
445 // Conditionally launch add field coursedisplay
446 if (!$dbman->field_exists($table, $field)) {
447 $dbman->add_field($table, $field);
448 }
449
450 // Main savepoint reached
451 upgrade_main_savepoint(true, 2012050300.03);
452 }
453
b9bcdb54
DP
454 if ($oldversion < 2012050300.04) {
455
456 // Define table course_display to be dropped
457 $table = new xmldb_table('course_display');
458
459 // Conditionally launch drop table for course_display
460 if ($dbman->table_exists($table)) {
461 $dbman->drop_table($table);
462 }
463
464 // Main savepoint reached
465 upgrade_main_savepoint(true, 2012050300.04);
466 }
467
468 if ($oldversion < 2012050300.05) {
469
470 // Clean up removed admin setting.
471 unset_config('navlinkcoursesections');
472
473 upgrade_main_savepoint(true, 2012050300.05);
474 }
475
c119a2ee
PS
476 if ($oldversion < 2012050400.01) {
477
478 // Define index sortorder (not unique) to be added to course
479 $table = new xmldb_table('course');
480 $index = new xmldb_index('sortorder', XMLDB_INDEX_NOTUNIQUE, array('sortorder'));
481
482 // Conditionally launch add index sortorder
483 if (!$dbman->index_exists($table, $index)) {
484 $dbman->add_index($table, $index);
485 }
486
487 // Main savepoint reached
488 upgrade_main_savepoint(true, 2012050400.01);
489 }
490
bbb0a6eb
SH
491 if ($oldversion < 2012050400.02) {
492
9fa74379
DP
493 // Clean up removed admin setting.
494 unset_config('enablecourseajax');
495
bbb0a6eb 496 upgrade_main_savepoint(true, 2012050400.02);
9fa74379
DP
497 }
498
74b714df
ARN
499 if ($oldversion < 2012051100.01) {
500
501 // Define field idnumber to be added to groups
502 $table = new xmldb_table('groups');
503 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'courseid');
504 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
505
506 // Conditionally launch add field idnumber
507 if (!$dbman->field_exists($table, $field)) {
508 $dbman->add_field($table, $field);
509 }
510
511 // Conditionally launch add index idnumber
512 if (!$dbman->index_exists($table, $index)) {
513 $dbman->add_index($table, $index);
514 }
515
516 // Define field idnumber to be added to groupings
517 $table = new xmldb_table('groupings');
518 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'name');
519 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
520
521 // Conditionally launch add field idnumber
522 if (!$dbman->field_exists($table, $field)) {
523 $dbman->add_field($table, $field);
524 }
525
526 // Conditionally launch add index idnumber
527 if (!$dbman->index_exists($table, $index)) {
528 $dbman->add_index($table, $index);
529 }
530
531 // Main savepoint reached
532 upgrade_main_savepoint(true, 2012051100.01);
533 }
534
dd420aba
EL
535 if ($oldversion < 2012051100.03) {
536
ce4dfd27 537 // Amend course table to add sectioncache cache
538 $table = new xmldb_table('course');
539 $field = new xmldb_field('sectioncache', XMLDB_TYPE_TEXT, null, null, null, null, null, 'showgrades');
540 if (!$dbman->field_exists($table, $field)) {
541 $dbman->add_field($table, $field);
542 }
543
544 // Amend course_sections to add date, time and groupingid availability
545 // conditions and a setting about whether to show them
546 $table = new xmldb_table('course_sections');
dd420aba 547 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'visible');
ce4dfd27 548 if (!$dbman->field_exists($table, $field)) {
549 $dbman->add_field($table, $field);
550 }
dd420aba 551 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'availablefrom');
ce4dfd27 552 if (!$dbman->field_exists($table, $field)) {
553 $dbman->add_field($table, $field);
554 }
dd420aba 555 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'availableuntil');
ce4dfd27 556 // Conditionally launch add field showavailability
557 if (!$dbman->field_exists($table, $field)) {
558 $dbman->add_field($table, $field);
559 }
dd420aba 560 $field = new xmldb_field('groupingid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'showavailability');
ce4dfd27 561 // Conditionally launch add field groupingid
562 if (!$dbman->field_exists($table, $field)) {
563 $dbman->add_field($table, $field);
564 }
565
566 // Add course_sections_availability to add completion & grade availability conditions
567 $table = new xmldb_table('course_sections_availability');
568
569 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
570 $table->add_field('coursesectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
571 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
572 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', null, null, null, null);
573 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
574 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
575 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
576
577 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
dd420aba
EL
578 $table->add_key('coursesectionid', XMLDB_KEY_FOREIGN, array('coursesectionid'), 'course_sections', array('id'));
579 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
580 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
ce4dfd27 581
582 if (!$dbman->table_exists($table)) {
583 $dbman->create_table($table);
584 }
585
94dc3c7d 586 // Main savepoint reached
dd420aba 587 upgrade_main_savepoint(true, 2012051100.03);
ce4dfd27 588 }
589
67233725
DC
590 if ($oldversion < 2012052100.00) {
591
7051415c 592 // Define field referencefileid to be added to files.
67233725
DC
593 $table = new xmldb_table('files');
594
7051415c 595 // Define field referencefileid to be added to files.
67233725 596 $field = new xmldb_field('referencefileid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'sortorder');
7051415c
DC
597
598 // Conditionally launch add field referencefileid.
67233725
DC
599 if (!$dbman->field_exists($table, $field)) {
600 $dbman->add_field($table, $field);
601 }
602
7051415c 603 // Define field referencelastsync to be added to files.
67233725 604 $field = new xmldb_field('referencelastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencefileid');
7051415c
DC
605
606 // Conditionally launch add field referencelastsync.
67233725
DC
607 if (!$dbman->field_exists($table, $field)) {
608 $dbman->add_field($table, $field);
609 }
610
7051415c
DC
611 // Define field referencelifetime to be added to files.
612 $field = new xmldb_field('referencelifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencelastsync');
613
614 // Conditionally launch add field referencelifetime.
67233725
DC
615 if (!$dbman->field_exists($table, $field)) {
616 $dbman->add_field($table, $field);
617 }
618
619 $key = new xmldb_key('referencefileid', XMLDB_KEY_FOREIGN, array('referencefileid'), 'files_reference', array('id'));
620 // Launch add key referencefileid
621 $dbman->add_key($table, $key);
622
623 // Define table files_reference to be created.
624 $table = new xmldb_table('files_reference');
625
626 // Adding fields to table files_reference.
627 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
67233725
DC
628 $table->add_field('repositoryid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
629 $table->add_field('lastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
630 $table->add_field('lifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
631 $table->add_field('reference', XMLDB_TYPE_TEXT, null, null, null, null, null);
632
633 // Adding keys to table files_reference.
634 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
635 $table->add_key('repositoryid', XMLDB_KEY_FOREIGN, array('repositoryid'), 'repository_instances', array('id'));
636
637 // Conditionally launch create table for files_reference
638 if (!$dbman->table_exists($table)) {
639 $dbman->create_table($table);
640 }
641
642 // Main savepoint reached
643 upgrade_main_savepoint(true, 2012052100.00);
644 }
645
3f1bca9d 646 if ($oldversion < 2012052500.03) { // fix invalid course_completion_records MDL-27368
d2e3a1b4
DM
647 //first get all instances of duplicate records
648 $sql = 'SELECT userid, course FROM {course_completions} WHERE (deleted IS NULL OR deleted <> 1) GROUP BY userid, course HAVING (count(id) > 1)';
649 $duplicates = $DB->get_recordset_sql($sql, array());
650
651 foreach ($duplicates as $duplicate) {
652 $pointer = 0;
653 //now get all the records for this user/course
654 $sql = 'userid = ? AND course = ? AND (deleted IS NULL OR deleted <> 1)';
655 $completions = $DB->get_records_select('course_completions', $sql,
656 array($duplicate->userid, $duplicate->course), 'timecompleted DESC, timestarted DESC');
657 $needsupdate = false;
658 $origcompletion = null;
659 foreach ($completions as $completion) {
660 $pointer++;
661 if ($pointer === 1) { //keep 1st record but delete all others.
662 $origcompletion = $completion;
663 } else {
664 //we need to keep the "oldest" of all these fields as the valid completion record.
665 $fieldstocheck = array('timecompleted', 'timestarted', 'timeenrolled');
666 foreach ($fieldstocheck as $f) {
667 if ($origcompletion->$f > $completion->$f) {
668 $origcompletion->$f = $completion->$f;
669 $needsupdate = true;
670 }
671 }
672 $DB->delete_records('course_completions', array('id'=>$completion->id));
673 }
674 }
675 if ($needsupdate) {
676 $DB->update_record('course_completions', $origcompletion);
677 }
678 }
679
680 // Main savepoint reached
3f1bca9d 681 upgrade_main_savepoint(true, 2012052500.03);
d2e3a1b4 682 }
424a19b1 683
bd633dda 684 if ($oldversion < 2012052900.00) {
e6f55285
SH
685 // Clean up all duplicate records in the course_completions table in preparation
686 // for adding a new index there.
424a19b1
AB
687 upgrade_course_completion_remove_duplicates(
688 'course_completions',
689 array('userid', 'course'),
690 array('timecompleted', 'timestarted', 'timeenrolled')
691 );
692
e6f55285
SH
693 // Main savepoint reached
694 upgrade_main_savepoint(true, 2012052900.00);
695 }
696
697 if ($oldversion < 2012052900.01) {
698 // Add indexes to prevent new duplicates in the course_completions table.
424a19b1
AB
699 // Define index useridcourse (unique) to be added to course_completions
700 $table = new xmldb_table('course_completions');
701 $index = new xmldb_index('useridcourse', XMLDB_INDEX_UNIQUE, array('userid', 'course'));
702
424a19b1
AB
703 // Conditionally launch add index useridcourse
704 if (!$dbman->index_exists($table, $index)) {
705 $dbman->add_index($table, $index);
706 }
707
708 // Main savepoint reached
e6f55285 709 upgrade_main_savepoint(true, 2012052900.01);
424a19b1
AB
710 }
711
e6f55285
SH
712 if ($oldversion < 2012052900.02) {
713 // Clean up all duplicate records in the course_completion_crit_compl table in preparation
714 // for adding a new index there.
424a19b1
AB
715 upgrade_course_completion_remove_duplicates(
716 'course_completion_crit_compl',
717 array('userid', 'course', 'criteriaid'),
718 array('timecompleted')
719 );
720
e6f55285
SH
721 // Main savepoint reached
722 upgrade_main_savepoint(true, 2012052900.02);
723 }
424a19b1 724
e6f55285
SH
725 if ($oldversion < 2012052900.03) {
726 // Add indexes to prevent new duplicates in the course_completion_crit_compl table.
424a19b1
AB
727 // Define index useridcoursecriteraid (unique) to be added to course_completion_crit_compl
728 $table = new xmldb_table('course_completion_crit_compl');
729 $index = new xmldb_index('useridcoursecriteraid', XMLDB_INDEX_UNIQUE, array('userid', 'course', 'criteriaid'));
730
731 // Conditionally launch add index useridcoursecriteraid
732 if (!$dbman->index_exists($table, $index)) {
733 $dbman->add_index($table, $index);
734 }
735
736 // Main savepoint reached
e6f55285 737 upgrade_main_savepoint(true, 2012052900.03);
424a19b1
AB
738 }
739
e6f55285
SH
740 if ($oldversion < 2012052900.04) {
741 // Clean up all duplicate records in the course_completion_aggr_methd table in preparation
742 // for adding a new index there.
424a19b1
AB
743 upgrade_course_completion_remove_duplicates(
744 'course_completion_aggr_methd',
745 array('course', 'criteriatype')
746 );
747
e6f55285
SH
748 // Main savepoint reached
749 upgrade_main_savepoint(true, 2012052900.04);
750 }
751
752 if ($oldversion < 2012052900.05) {
753 // Add indexes to prevent new duplicates in the course_completion_aggr_methd table.
424a19b1
AB
754 // Define index coursecriteratype (unique) to be added to course_completion_aggr_methd
755 $table = new xmldb_table('course_completion_aggr_methd');
756 $index = new xmldb_index('coursecriteriatype', XMLDB_INDEX_UNIQUE, array('course', 'criteriatype'));
757
758 // Conditionally launch add index coursecriteratype
759 if (!$dbman->index_exists($table, $index)) {
760 $dbman->add_index($table, $index);
761 }
762
763 // Main savepoint reached
e6f55285 764 upgrade_main_savepoint(true, 2012052900.05);
424a19b1
AB
765 }
766
79c2b509 767 if ($oldversion < 2012060600.01) {
51003653
DM
768 // Add field referencehash to files_reference
769 $table = new xmldb_table('files_reference');
770 $field = new xmldb_field('referencehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'reference');
771 if (!$dbman->field_exists($table, $field)) {
772 $dbman->add_field($table, $field);
773 }
79c2b509 774 upgrade_main_savepoint(true, 2012060600.01);
51003653
DM
775 }
776
79c2b509 777 if ($oldversion < 2012060600.02) {
dccba8bc
DM
778 // Populate referencehash field with SHA1 hash of the reference - this shoudl affect only 2.3dev sites
779 // that were using the feature for testing. Production sites have the table empty.
780 $rs = $DB->get_recordset('files_reference', null, '', 'id, reference');
781 foreach ($rs as $record) {
782 $hash = sha1($record->reference);
783 $DB->set_field('files_reference', 'referencehash', $hash, array('id' => $record->id));
784 }
785 $rs->close();
786
79c2b509 787 upgrade_main_savepoint(true, 2012060600.02);
dccba8bc
DM
788 }
789
79c2b509 790 if ($oldversion < 2012060600.03) {
8f47a60e
DM
791 // Merge duplicate records in files_reference that were created during the development
792 // phase at 2.3dev sites. This is needed so we can create the unique index over
793 // (repositoryid, referencehash) fields.
794 $sql = "SELECT repositoryid, referencehash, MIN(id) AS minid
795 FROM {files_reference}
796 GROUP BY repositoryid, referencehash
4a5be0e9 797 HAVING COUNT(*) > 1";
8f47a60e
DM
798 $duprs = $DB->get_recordset_sql($sql);
799 foreach ($duprs as $duprec) {
800 // get the list of all ids in {files_reference} that need to be remapped
801 $dupids = $DB->get_records_select('files_reference', "repositoryid = ? AND referencehash = ? AND id > ?",
802 array($duprec->repositoryid, $duprec->referencehash, $duprec->minid), '', 'id');
803 $dupids = array_keys($dupids);
804 // relink records in {files} that are now referring to a duplicate record
805 // in {files_reference} to refer to the first one
806 list($subsql, $subparams) = $DB->get_in_or_equal($dupids);
807 $DB->set_field_select('files', 'referencefileid', $duprec->minid, "referencefileid $subsql", $subparams);
808 // and finally remove all orphaned records from {files_reference}
809 $DB->delete_records_list('files_reference', 'id', $dupids);
810 }
811 $duprs->close();
812
79c2b509 813 upgrade_main_savepoint(true, 2012060600.03);
8f47a60e
DM
814 }
815
79c2b509 816 if ($oldversion < 2012060600.04) {
6bbf31c3
DM
817 // Add a unique index over repositoryid and referencehash fields in files_reference table
818 $table = new xmldb_table('files_reference');
819 $index = new xmldb_index('uq_external_file', XMLDB_INDEX_UNIQUE, array('repositoryid', 'referencehash'));
820
821 if (!$dbman->index_exists($table, $index)) {
822 $dbman->add_index($table, $index);
823 }
824
79c2b509 825 upgrade_main_savepoint(true, 2012060600.04);
6bbf31c3
DM
826 }
827
a80b5a0c
DP
828 if ($oldversion < 2012061800.01) {
829
830 // Define field screenreader to be dropped from user
831 $table = new xmldb_table('user');
832 $field = new xmldb_field('ajax');
833
834 // Conditionally launch drop field screenreader
835 if ($dbman->field_exists($table, $field)) {
836 $dbman->drop_field($table, $field);
837 }
838
839 // Main savepoint reached
840 upgrade_main_savepoint(true, 2012061800.01);
841 }
842
088c374a 843 if ($oldversion < 2012062000.00) {
38ca2f1a
DM
844 // Add field newcontextid to backup_files_template
845 $table = new xmldb_table('backup_files_template');
846 $field = new xmldb_field('newcontextid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'info');
847
848 if (!$dbman->field_exists($table, $field)) {
849 $dbman->add_field($table, $field);
850 }
851
088c374a 852 upgrade_main_savepoint(true, 2012062000.00);
38ca2f1a
DM
853 }
854
088c374a 855 if ($oldversion < 2012062000.01) {
38ca2f1a
DM
856 // Add field newitemid to backup_files_template
857 $table = new xmldb_table('backup_files_template');
858 $field = new xmldb_field('newitemid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'newcontextid');
859
860 if (!$dbman->field_exists($table, $field)) {
861 $dbman->add_field($table, $field);
862 }
863
088c374a 864 upgrade_main_savepoint(true, 2012062000.01);
38ca2f1a
DM
865 }
866
44cb29a0
EL
867 // Moodle v2.3.0 release upgrade line
868 // Put any upgrade step following this
869
44cb29a0 870 if ($oldversion < 2012062500.02) {
3f4a2999
871 // Drop some old backup tables, not used anymore
872
873 // Define table backup_files to be dropped
874 $table = new xmldb_table('backup_files');
875
876 // Conditionally launch drop table for backup_files
877 if ($dbman->table_exists($table)) {
878 $dbman->drop_table($table);
879 }
880
881 // Define table backup_ids to be dropped
882 $table = new xmldb_table('backup_ids');
883
884 // Conditionally launch drop table for backup_ids
66dd3e5d
SH
885 if ($dbman->table_exists($table)) {
886 $dbman->drop_table($table);
3f4a2999
887 }
888
889 // Main savepoint reached
44cb29a0 890 upgrade_main_savepoint(true, 2012062500.02);
3f4a2999
891 }
892
00142ce5 893 if ($oldversion < 2012070600.04) {
141d3c86
SH
894 // Define table course_modules_avail_fields to be created
895 $table = new xmldb_table('course_modules_avail_fields');
896
897 // Adding fields to table course_modules_avail_fields
898 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
899 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
900 $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
901 $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
902 $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
903 $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
904
905 // Adding keys to table course_modules_avail_fields
906 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
907 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
908
909 // Conditionally launch create table for course_modules_avail_fields
910 if (!$dbman->table_exists($table)) {
911 $dbman->create_table($table);
912 }
913
914 // Main savepoint reached
00142ce5 915 upgrade_main_savepoint(true, 2012070600.04);
141d3c86
SH
916 }
917
00142ce5 918 if ($oldversion < 2012070600.05) {
141d3c86
SH
919 // Define table course_sections_avail_fields to be created
920 $table = new xmldb_table('course_sections_avail_fields');
921
922 // Adding fields to table course_sections_avail_fields
923 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
924 $table->add_field('coursesectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
925 $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
926 $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
927 $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
928 $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
929
930 // Adding keys to table course_sections_avail_fields
931 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
932 $table->add_key('coursesectionid', XMLDB_KEY_FOREIGN, array('coursesectionid'), 'course_sections', array('id'));
933
934 // Conditionally launch create table for course_sections_avail_fields
935 if (!$dbman->table_exists($table)) {
936 $dbman->create_table($table);
937 }
938
939 // Main savepoint reached
00142ce5 940 upgrade_main_savepoint(true, 2012070600.05);
141d3c86 941 }
a80b5a0c 942
00142ce5 943 if ($oldversion < 2012070600.06) {
a2a2e7fb
SH
944
945 // Drop "deleted" fields
46eca1f7
AB
946 $table = new xmldb_table('course_completions');
947 $field = new xmldb_field('timenotified');
a2a2e7fb
SH
948 $field = new xmldb_field('deleted');
949
950 // Conditionally launch drop field deleted from course_completions
951 if ($dbman->field_exists($table, $field)) {
66dd3e5d 952 $dbman->drop_field($table, $field);
a2a2e7fb 953 }
46eca1f7 954
a2a2e7fb 955 $field = new xmldb_field('timenotified');
46eca1f7
AB
956 // Conditionally launch drop field timenotified from course_completions
957 if ($dbman->field_exists($table, $field)) {
958 $dbman->drop_field($table, $field);
959 }
960
961 // Main savepoint reached
00142ce5 962 upgrade_main_savepoint(true, 2012070600.06);
46eca1f7
AB
963 }
964
00142ce5 965 if ($oldversion < 2012070600.07) {
a2a2e7fb
SH
966 $table = new xmldb_table('course_completion_crit_compl');
967 $field = new xmldb_field('deleted');
968
969 // Conditionally launch drop field deleted from course_completion_crit_compl
970 if ($dbman->field_exists($table, $field)) {
971 $dbman->drop_field($table, $field);
972 }
973 // Main savepoint reached
00142ce5 974 upgrade_main_savepoint(true, 2012070600.07);
a2a2e7fb
SH
975 }
976
00142ce5 977 if ($oldversion < 2012070600.08) {
a2a2e7fb
SH
978
979 // Drop unused table "course_completion_notify"
980 $table = new xmldb_table('course_completion_notify');
981
982 // Conditionally launch drop table course_completion_notify
983 if ($dbman->table_exists($table)) {
984 $dbman->drop_table($table);
985 }
986
987 // Main savepoint reached
00142ce5 988 upgrade_main_savepoint(true, 2012070600.08);
a2a2e7fb
SH
989 }
990
00142ce5 991 if ($oldversion < 2012070600.09) {
bd991d03
PS
992
993 // Define index path (not unique) to be added to context
994 $table = new xmldb_table('context');
995 $index = new xmldb_index('path', XMLDB_INDEX_NOTUNIQUE, array('path'), array('varchar_pattern_ops'));
996
997 // Recreate index with new pattern hint
998 if ($DB->get_dbfamily() === 'postgres') {
999 if ($dbman->index_exists($table, $index)) {
1000 $dbman->drop_index($table, $index);
1001 }
1002 $dbman->add_index($table, $index);
1003 }
1004
1005 // Main savepoint reached
00142ce5 1006 upgrade_main_savepoint(true, 2012070600.09);
bd991d03
PS
1007 }
1008
00142ce5 1009 if ($oldversion < 2012070600.10) {
c52551dc
PS
1010
1011 // Define index name (unique) to be dropped form role
1012 $table = new xmldb_table('role');
1013 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
1014
1015 // Conditionally launch drop index name
1016 if ($dbman->index_exists($table, $index)) {
1017 $dbman->drop_index($table, $index);
1018 }
1019
1020 // Main savepoint reached
00142ce5 1021 upgrade_main_savepoint(true, 2012070600.10);
c52551dc
PS
1022 }
1023
f0d6e53c 1024 if ($oldversion < 2012070600.11) {
79a47143
1025
1026 // Define index component-itemid-userid (not unique) to be added to role_assignments
1027 $table = new xmldb_table('role_assignments');
1028 $index = new xmldb_index('component-itemid-userid', XMLDB_INDEX_NOTUNIQUE, array('component', 'itemid', 'userid'));
1029
1030 // Conditionally launch add index component-itemid-userid
1031 if (!$dbman->index_exists($table, $index)) {
1032 $dbman->add_index($table, $index);
1033 }
1034
1035 // Main savepoint reached
f0d6e53c 1036 upgrade_main_savepoint(true, 2012070600.11);
79a47143
1037 }
1038
c8b3346c
PS
1039 if ($oldversion < 2012071900.01) {
1040 // Cleanup after simpeltests tool
1041 capabilities_cleanup('tool_unittest');
1042 unset_all_config_for_plugin('tool_unittest');
1043
1044 upgrade_main_savepoint(true, 2012071900.01);
1045 }
1046
b7db7803 1047 if ($oldversion < 2012072400.00) {
13725b37
PS
1048 // Remove obsolete xhtml strict setting - use THEME->doctype in theme config if necessary,
1049 // see theme_config->doctype in lib/outputlib.php for more details.
1050 unset_config('xmlstrictheaders');
b7db7803 1051 upgrade_main_savepoint(true, 2012072400.00);
13725b37 1052 }
a80b5a0c 1053
61e93a4c 1054 if ($oldversion < 2012072401.00) {
67e3dd2c
FM
1055
1056 // Saves orphaned questions from the Dark Side
1057 upgrade_save_orphaned_questions();
1058
1059 // Main savepoint reached
61e93a4c 1060 upgrade_main_savepoint(true, 2012072401.00);
67e3dd2c 1061 }
a80b5a0c 1062
3713514b 1063 if ($oldversion < 2012072600.01) {
f14f9842
AA
1064 // Handle events with empty eventtype //MDL-32827
1065
1066 $DB->set_field('event', 'eventtype', 'site', array('eventtype' => '', 'courseid' => $SITE->id));
1067 $DB->set_field_select('event', 'eventtype', 'due', "eventtype = '' AND courseid != 0 AND groupid = 0 AND (modulename = 'assignment' OR modulename = 'assign')");
1068 $DB->set_field_select('event', 'eventtype', 'course', "eventtype = '' AND courseid != 0 AND groupid = 0");
1069 $DB->set_field_select('event', 'eventtype', 'group', "eventtype = '' AND groupid != 0");
1070 $DB->set_field_select('event', 'eventtype', 'user', "eventtype = '' AND userid != 0");
1071
3713514b
AA
1072 // Main savepoint reached
1073 upgrade_main_savepoint(true, 2012072600.01);
1074 }
237f05cf
PS
1075
1076 if ($oldversion < 2012080200.02) {
1077 // Drop obsolete question upgrade field that should have been added to the install.xml.
1078 $table = new xmldb_table('question');
1079 $field = new xmldb_field('oldquestiontextformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
1080
1081 if ($dbman->field_exists($table, $field)) {
1082 $dbman->drop_field($table, $field);
1083 }
1084
1085 upgrade_main_savepoint(true, 2012080200.02);
1086 }
237f05cf 1087
76b97631 1088 if ($oldversion < 2012081400.01) {
850d2db8
DP
1089 // Move the ability to disable blogs to its own setting MDL-25012.
1090
1091 if (isset($CFG->bloglevel)) {
1092 // Only change settings if existing setting was set.
1093 if (empty($CFG->bloglevel)) {
1094 set_config('enableblogs', 0);
1095 // Now set the bloglevel to a valid setting as the disabled setting has been removed.
1096 // This prevents confusing results when users enable the blog system in future.
1097 set_config('bloglevel', BLOG_USER_LEVEL);
1098 } else {
1099 set_config('enableblogs', 1);
1100 }
1101 }
1102
1103 // Main savepoint reached
76b97631 1104 upgrade_main_savepoint(true, 2012081400.01);
850d2db8
DP
1105 }
1106
704533fc
PS
1107 if ($oldversion < 2012081600.01) {
1108 // Delete removed setting - Google Maps API V2 will not work in 2013.
1109 unset_config('googlemapkey');
1110 upgrade_main_savepoint(true, 2012081600.01);
1111 }
1112
882fb835
PS
1113 if ($oldversion < 2012082300.01) {
1114 // Add more custom enrol fields.
1115 $table = new xmldb_table('enrol');
882fb835 1116 $field = new xmldb_field('customint5', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint4');
59b9a140 1117
882fb835
PS
1118 if (!$dbman->field_exists($table, $field)) {
1119 $dbman->add_field($table, $field);
1120 }
1121
1122 $field = new xmldb_field('customint6', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint5');
1123 if (!$dbman->field_exists($table, $field)) {
1124 $dbman->add_field($table, $field);
1125 }
1126
1127 $field = new xmldb_field('customint7', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint6');
1128 if (!$dbman->field_exists($table, $field)) {
1129 $dbman->add_field($table, $field);
1130 }
1131
1132 $field = new xmldb_field('customint8', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint7');
1133 if (!$dbman->field_exists($table, $field)) {
1134 $dbman->add_field($table, $field);
1135 }
1136
1137 $field = new xmldb_field('customchar3', XMLDB_TYPE_CHAR, '1333', null, null, null, null, 'customchar2');
1138 if (!$dbman->field_exists($table, $field)) {
1139 $dbman->add_field($table, $field);
1140 }
1141
1142 $field = new xmldb_field('customtext3', XMLDB_TYPE_TEXT, null, null, null, null, null, 'customtext2');
1143 if (!$dbman->field_exists($table, $field)) {
1144 $dbman->add_field($table, $field);
1145 }
1146
1147 $field = new xmldb_field('customtext4', XMLDB_TYPE_TEXT, null, null, null, null, null, 'customtext3');
1148 if (!$dbman->field_exists($table, $field)) {
1149 $dbman->add_field($table, $field);
1150 }
1151
1152 // Main savepoint reached.
1153 upgrade_main_savepoint(true, 2012082300.01);
1154 }
1155
238f7761 1156 if ($oldversion < 2012082300.02) {
1d1917ae 1157 // Define field component to be added to groups_members
1158 $table = new xmldb_table('groups_members');
1159 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'timeadded');
1160
1161 // Conditionally launch add field component
1162 if (!$dbman->field_exists($table, $field)) {
1163 $dbman->add_field($table, $field);
1164 }
1165
1166 // Define field itemid to be added to groups_members
1167 $field = new xmldb_field('itemid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'component');
1168
1169 // Conditionally launch add field itemid
1170 if (!$dbman->field_exists($table, $field)) {
1171 $dbman->add_field($table, $field);
1172 }
1173
1174 // Main savepoint reached
238f7761 1175 upgrade_main_savepoint(true, 2012082300.02);
1d1917ae 1176 }
704533fc 1177
937b0acb
AB
1178 if ($oldversion < 2012090500.00) {
1179 $subquery = 'SELECT b.id FROM {blog_external} b where b.id = ' . $DB->sql_cast_char2int('{post}.content', true);
f3616783
AD
1180 $sql = 'DELETE FROM {post}
1181 WHERE {post}.module = \'blog_external\'
1182 AND NOT EXISTS (' . $subquery . ')
1183 AND ' . $DB->sql_isnotempty('post', 'uniquehash', false, false);
1184 $DB->execute($sql);
937b0acb 1185 upgrade_main_savepoint(true, 2012090500.00);
f3616783 1186 }
704533fc 1187
59b9a140
FM
1188 if ($oldversion < 2012090700.01) {
1189 // Add a category field in the course_request table
1190 $table = new xmldb_table('course_request');
1191 $field = new xmldb_field('category', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, 'summaryformat');
1192 if (!$dbman->field_exists($table, $field)) {
1193 $dbman->add_field($table, $field);
1194 }
1195
1196 // Main savepoint reached.
1197 upgrade_main_savepoint(true, 2012090700.01);
1198 }
1199
d11f12f6
DM
1200 if ($oldversion < 2012091700.00) {
1201
1202 // Dropping screenreader field from user.
1203 $table = new xmldb_table('user');
1204 $field = new xmldb_field('screenreader');
1205
1206 if ($dbman->field_exists($table, $field)) {
1207 $dbman->drop_field($table, $field);
1208 }
1209
1210 // Main savepoint reached.
1211 upgrade_main_savepoint(true, 2012091700.00);
1212 }
1213
52ebfade 1214 if ($oldversion < 2012092100.01) {
0f764c02
FM
1215 // Some folders still have a sortorder set, which is used for main files but is not
1216 // supported by the folder resource. We reset the value here.
1217 $sql = 'UPDATE {files} SET sortorder = ? WHERE component = ? AND filearea = ? AND sortorder <> ?';
1218 $DB->execute($sql, array(0, 'mod_folder', 'content', 0));
1219
1220 // Main savepoint reached.
52ebfade 1221 upgrade_main_savepoint(true, 2012092100.01);
0f764c02
FM
1222 }
1223
bb14cc2d 1224 if ($oldversion < 2012092600.00) {
7dd764b8
JG
1225 // Define index idname (unique) to be added to tag
1226 $table = new xmldb_table('tag');
1227 $index = new xmldb_index('idname', XMLDB_INDEX_UNIQUE, array('id', 'name'));
1228
1229 // Conditionally launch add index idname
1230 if (!$dbman->index_exists($table, $index)) {
1231 $dbman->add_index($table, $index);
1232 }
1233
1234 // Main savepoint reached
bb14cc2d 1235 upgrade_main_savepoint(true, 2012092600.00);
7dd764b8
JG
1236 }
1237
cb6b5fb6 1238 if ($oldversion < 2012101500.01) {
c0a05926
AG
1239 // Find all orphaned blog associations that might exist.
1240 $sql = "SELECT ba.id
1241 FROM {blog_association} ba
1242 LEFT JOIN {post} p
1243 ON p.id = ba.blogid
1244 WHERE p.id IS NULL";
1245 $orphanedrecordids = $DB->get_records_sql($sql);
1246 // Now delete these associations.
1247 foreach ($orphanedrecordids as $orphanedrecord) {
1248 $DB->delete_records('blog_association', array('id' => $orphanedrecord->id));
1249 }
1250
cb6b5fb6 1251 upgrade_main_savepoint(true, 2012101500.01);
c0a05926
AG
1252 }
1253
c28717d9 1254 if ($oldversion < 2012101800.02) {
d8271e39
FM
1255 // Renaming backups using previous file naming convention.
1256 upgrade_rename_old_backup_files_using_shortname();
1257
1258 // Main savepoint reached.
c28717d9 1259 upgrade_main_savepoint(true, 2012101800.02);
d8271e39
FM
1260 }
1261
0c918a1f 1262 if ($oldversion < 2012103001.00) {
b5a52acd
JH
1263 // create new event_subscriptions table
1264 $table = new xmldb_table('event_subscriptions');
e30390a0 1265 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
b5a52acd
JH
1266 $table->add_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1267 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1268 $table->add_field('groupid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1269 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1270 $table->add_field('pollinterval', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1271 $table->add_field('lastupdated', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
e30390a0 1272 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
b5a52acd
JH
1273 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1274 if (!$dbman->table_exists($table)) {
1275 $dbman->create_table($table);
1276 }
1277 // Main savepoint reached
0c918a1f 1278 upgrade_main_savepoint(true, 2012103001.00);
b5a52acd
JH
1279 }
1280
0c918a1f 1281 if ($oldversion < 2012103002.00) {
b5a52acd
JH
1282 // Add subscription field to the event table
1283 $table = new xmldb_table('event');
e30390a0
SH
1284 $field = new xmldb_field('subscriptionid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'timemodified');
1285
1286 // Conditionally launch add field subscriptionid
b5a52acd
JH
1287 if (!$dbman->field_exists($table, $field)) {
1288 $dbman->add_field($table, $field);
1289 }
d618dcf3
DP
1290 upgrade_main_savepoint(true, 2012103002.00);
1291 }
b5a52acd 1292
d618dcf3 1293 if ($oldversion < 2012103003.00) {
b5a52acd 1294 // Fix uuid field in event table to match RFC-2445 UID property
d618dcf3 1295 $table = new xmldb_table('event');
e30390a0 1296 $field = new xmldb_field('uuid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'visible');
b5a52acd 1297 if ($dbman->field_exists($table, $field)) {
e30390a0 1298 // Changing precision of field uuid on table event to (255)
b5a52acd
JH
1299 $dbman->change_field_precision($table, $field);
1300 }
b5a52acd 1301 // Main savepoint reached
d618dcf3 1302 upgrade_main_savepoint(true, 2012103003.00);
b5a52acd
JH
1303 }
1304
29ac9fc1
MG
1305 if ($oldversion < 2012110200.00) {
1306
1307 // Define table course_format_options to be created
1308 $table = new xmldb_table('course_format_options');
1309
1310 // Adding fields to table course_format_options
1311 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1312 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1313 $table->add_field('format', XMLDB_TYPE_CHAR, '21', null, XMLDB_NOTNULL, null, null);
1314 $table->add_field('sectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'format');
1315 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1316 $table->add_field('value', XMLDB_TYPE_TEXT, null, null, null, null, null);
1317
1318 // Adding keys to table course_format_options
1319 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1320 $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1321
1322 // Adding indexes to table course_format_options
1323 $table->add_index('formatoption', XMLDB_INDEX_UNIQUE, array('courseid', 'format', 'sectionid', 'name'));
1324
1325 // Conditionally launch create table for course_format_options
1326 if (!$dbman->table_exists($table)) {
1327 $dbman->create_table($table);
1328 }
1329
1330 // Changing type of field format on table course to char with length 21
1331 $table = new xmldb_table('course');
1332 $field = new xmldb_field('format', XMLDB_TYPE_CHAR, '21', null, XMLDB_NOTNULL, null, 'topics', 'summaryformat');
1333
1334 // Launch change of type for field format
1335 $dbman->change_field_type($table, $field);
1336
1337 // Main savepoint reached
1338 upgrade_main_savepoint(true, 2012110200.00);
1339 }
1340
b5cf83f0
MG
1341 if ($oldversion < 2012110201.00) {
1342
1343 // Copy fields 'coursedisplay', 'numsections', 'hiddensections' from table {course}
1344 // to table {course_format_options} as the additional format options
1345 $fields = array();
1346 $table = new xmldb_table('course');
1347 foreach (array('coursedisplay', 'numsections', 'hiddensections') as $fieldname) {
1348 // first check that fields still exist
1349 $field = new xmldb_field($fieldname);
1350 if ($dbman->field_exists($table, $field)) {
1351 $fields[] = $fieldname;
1352 }
1353 }
1354
1355 if (!empty($fields)) {
1356 $transaction = $DB->start_delegated_transaction();
1357 $rs = $DB->get_recordset_sql('SELECT id, format, '. join(',', $fields).'
1358 FROM {course}
1359 WHERE format <> ? AND format <> ?',
1360 array('scorm', 'social'));
1361 // (do not copy fields from scrom and social formats, we already know that they are not used)
1362 foreach ($rs as $rec) {
1363 foreach ($fields as $field) {
1364 try {
1365 $DB->insert_record('course_format_options',
1366 array(
1367 'courseid' => $rec->id,
1368 'format' => $rec->format,
1369 'sectionid' => 0,
1370 'name' => $field,
1371 'value' => $rec->$field
1372 ));
1373 } catch (dml_exception $e) {
1374 // index 'courseid,format,sectionid,name' violation
1375 // continue; the entry in course_format_options already exists, use it
1376 }
1377 }
1378 }
1379 $rs->close();
1380 $transaction->allow_commit();
1381
1382 // Drop fields from table course
1383 foreach ($fields as $fieldname) {
1384 $field = new xmldb_field($fieldname);
1385 $dbman->drop_field($table, $field);
1386 }
1387 }
1388
1389 // Main savepoint reached
1390 upgrade_main_savepoint(true, 2012110201.00);
1391 }
1392
37743241
MN
1393 if ($oldversion < 2012110700.01) {
1394
1395 // Define field caller_component to be added to portfolio_log.
1396 $table = new xmldb_table('portfolio_log');
1397 $field = new xmldb_field('caller_component', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'caller_file');
1398
1399 // Conditionally launch add field caller_component.
1400 if (!$dbman->field_exists($table, $field)) {
1401 $dbman->add_field($table, $field);
1402 }
1403
1404 // Main savepoint reached.
1405 upgrade_main_savepoint(true, 2012110700.01);
1406 }
1407
7d270556
AB
1408 if ($oldversion < 2012111200.00) {
1409
1410 // Define table temp_enroled_template to be created
1411 $table = new xmldb_table('temp_enroled_template');
1412
1413 // Adding fields to table temp_enroled_template
1414 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1415 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1416 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1417 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1418
1419 // Adding keys to table temp_enroled_template
1420 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1421
1422 // Adding indexes to table temp_enroled_template
1423 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
1424 $table->add_index('courseid', XMLDB_INDEX_NOTUNIQUE, array('courseid'));
1425 $table->add_index('roleid', XMLDB_INDEX_NOTUNIQUE, array('roleid'));
1426
1427 // Conditionally launch create table for temp_enroled_template
1428 if (!$dbman->table_exists($table)) {
1429 $dbman->create_table($table);
1430 }
1431
1432 // Define table temp_log_template to be created
1433 $table = new xmldb_table('temp_log_template');
1434
1435 // Adding fields to table temp_log_template
1436 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1437 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1438 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1439 $table->add_field('action', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
1440
1441 // Adding keys to table temp_log_template
1442 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1443
1444 // Adding indexes to table temp_log_template
1445 $table->add_index('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
1446 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
1447 $table->add_index('user', XMLDB_INDEX_NOTUNIQUE, array('userid'));
1448 $table->add_index('usercourseaction', XMLDB_INDEX_NOTUNIQUE, array('userid', 'course', 'action'));
1449
1450 // Conditionally launch create table for temp_log_template
1451 if (!$dbman->table_exists($table)) {
1452 $dbman->create_table($table);
1453 }
1454
1455 // Main savepoint reached
1456 upgrade_main_savepoint(true, 2012111200.00);
1457 }
1458
9b835f70
FM
1459 if ($oldversion < 2012111200.01) {
1460 // Force the rebuild of the cache of every courses, some cached information could contain wrong icon references.
4202471a 1461 $DB->execute('UPDATE {course} set modinfo = ?, sectioncache = ?', array(null, null));
9b835f70
FM
1462
1463 // Main savepoint reached.
1464 upgrade_main_savepoint(true, 2012111200.01);
1465 }
1466
0b37fe46
PS
1467 if ($oldversion < 2012111601.01) {
1468 // Clea up after old shared memory caching support.
1469 unset_config('cachetype');
1470 unset_config('rcache');
1471 unset_config('rcachettl');
1472 unset_config('intcachemax');
1473 unset_config('memcachedhosts');
1474 unset_config('memcachedpconn');
1475
1476 // Main savepoint reached.
1477 upgrade_main_savepoint(true, 2012111601.01);
1478 }
1479
9cae08da
AB
1480 if ($oldversion < 2012112100.00) {
1481
1482 // Define field eventtype to be added to event_subscriptions.
1483 $table = new xmldb_table('event_subscriptions');
1484 $field = new xmldb_field('eventtype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'userid');
1485
1486 // Conditionally launch add field eventtype.
1487 if (!$dbman->field_exists($table, $field)) {
1488 $dbman->add_field($table, $field);
1489 }
1490
1491 // Main savepoint reached.
1492 upgrade_main_savepoint(true, 2012112100.00);
1493 }
7d270556 1494
6b9dfe73
EL
1495 // Moodle v2.4.0 release upgrade line
1496 // Put any upgrade step following this
1497
e28d5db2
MG
1498 if ($oldversion < 2012120300.01) {
1499 // Make sure site-course has format='site' //MDL-36840
1500
1501 if ($SITE->format !== 'site') {
1502 $DB->set_field('course', 'format', 'site', array('id' => $SITE->id));
1503 $SITE->format = 'site';
1504 }
1505
1506 // Main savepoint reached
1507 upgrade_main_savepoint(true, 2012120300.01);
1508 }
1509
dbfe4150
PS
1510 if ($oldversion < 2012120300.04) {
1511 // Remove "_utf8" suffix from all langs in course table.
1512 $langs = $DB->get_records_sql("SELECT DISTINCT lang FROM {course} WHERE lang LIKE ?", array('%_utf8'));
1513
1514 foreach ($langs as $lang=>$unused) {
1515 $newlang = str_replace('_utf8', '', $lang);
1516 $sql = "UPDATE {course} SET lang = :newlang WHERE lang = :lang";
1517 $DB->execute($sql, array('newlang'=>$newlang, 'lang'=>$lang));
1518 }
1519
1520 // Main savepoint reached.
1521 upgrade_main_savepoint(true, 2012120300.04);
1522 }
1523
21df0539 1524 if ($oldversion < 2012123000.00) {
0662bd67
PS
1525 // Purge removed module filters and all their settings.
1526
1527 $tables = array('filter_active', 'filter_config');
1528 foreach ($tables as $table) {
1529 $DB->delete_records_select($table, "filter LIKE 'mod/%'");
1530 $filters = $DB->get_records_sql("SELECT DISTINCT filter FROM {{$table}} WHERE filter LIKE 'filter/%'");
1531 foreach ($filters as $filter) {
1532 $DB->set_field($table, 'filter', substr($filter->filter, 7), array('filter'=>$filter->filter));
1533 }
1534 }
1535
1536 $configs = array('stringfilters', 'filterall');
1537 foreach ($configs as $config) {
1538 if ($filters = get_config(null, $config)) {
1539 $filters = explode(',', $filters);
1540 $newfilters = array();
1541 foreach($filters as $filter) {
1542 if (strpos($filter, '/') === false) {
1543 $newfilters[] = $filter;
1544 } else if (strpos($filter, 'filter/') === 0) {
1545 $newfilters[] = substr($filter, 7);
1546 }
1547 }
1548 $filters = implode(',', $newfilters);
1549 set_config($config, $filters);
1550 }
1551 }
1552
1553 unset($tables);
1554 unset($table);
1555 unset($configs);
1556 unset($newfilters);
1557 unset($filters);
1558 unset($filter);
1559
1560 // Main savepoint reached.
21df0539 1561 upgrade_main_savepoint(true, 2012123000.00);
0662bd67
PS
1562 }
1563
1e7db9fe 1564 if ($oldversion < 2013021100.01) {
ec2d8ceb
SC
1565
1566 // Changing precision of field password on table user to (255).
1567 $table = new xmldb_table('user');
1568 $field = new xmldb_field('password', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'username');
1569
1570 // Launch change of precision for field password.
1571 $dbman->change_field_precision($table, $field);
1572
1573 // Main savepoint reached.
1e7db9fe 1574 upgrade_main_savepoint(true, 2013021100.01);
ec2d8ceb 1575 }
0662bd67 1576
e0d9b7c0
SH
1577 if ($oldversion < 2013021800.00) {
1578 // Add the site identifier to the cache config's file.
1579 $siteidentifier = $DB->get_field('config', 'value', array('name' => 'siteidentifier'));
1580 cache_helper::update_site_identifier($siteidentifier);
1581
1582 // Main savepoint reached.
1583 upgrade_main_savepoint(true, 2013021800.00);
1584 }
1585
28d72ad9 1586 if ($oldversion < 2013021801.00) {
58b1904f
FM
1587 // Fixing possible wrong MIME types for SMART Notebook files.
1588 $extensions = array('%.gallery', '%.galleryitem', '%.gallerycollection', '%.nbk', '%.notebook', '%.xbk');
1589 $select = $DB->sql_like('filename', '?', false);
1590 foreach ($extensions as $extension) {
1591 $DB->set_field_select(
1592 'files',
1593 'mimetype',
1594 'application/x-smarttech-notebook',
1595 $select,
1596 array($extension)
1597 );
1598 }
219faacc 1599 upgrade_main_savepoint(true, 2013021801.00);
58b1904f 1600 }
e0d9b7c0 1601
82a63328 1602 if ($oldversion < 2013021801.01) {
59a0ba3b
ARN
1603 // Retrieve the list of course_sections as a recordset to save memory
1604 $coursesections = $DB->get_recordset('course_sections', null, 'course, id', 'id, course, sequence');
1605 foreach ($coursesections as $coursesection) {
1606 // Retrieve all of the actual modules in this course and section combination to reduce DB calls
1607 $actualsectionmodules = $DB->get_records('course_modules',
1608 array('course' => $coursesection->course, 'section' => $coursesection->id), '', 'id, section');
1609
1610 // Break out the current sequence so that we can compare it
1611 $currentsequence = explode(',', $coursesection->sequence);
1612 $newsequence = array();
1613
1614 // Check each of the modules in the current sequence
1615 foreach ($currentsequence as $module) {
1616 if (isset($actualsectionmodules[$module])) {
1617 $newsequence[] = $module;
1618 // We unset the actualsectionmodules so that we don't get duplicates and that we can add orphaned
1619 // modules later
1620 unset($actualsectionmodules[$module]);
1621 }
1622 }
1623
1624 // Append any modules which have somehow been orphaned
1625 foreach ($actualsectionmodules as $module) {
1626 $newsequence[] = $module->id;
1627 }
1628
1629 // Piece it all back together
1630 $sequence = implode(',', $newsequence);
1631
1632 // Only update if there have been changes
1633 if ($sequence !== $coursesection->sequence) {
1634 $coursesection->sequence = $sequence;
1635 $DB->update_record('course_sections', $coursesection);
1636
1637 // And clear the sectioncache and modinfo cache - they'll be regenerated on next use
1638 $course = new stdClass();
1639 $course->id = $coursesection->course;
1640 $course->sectioncache = null;
1641 $course->modinfo = null;
1642 $DB->update_record('course', $course);
1643 }
1644 }
1645 $coursesections->close();
1646
1647 // Main savepoint reached.
82a63328 1648 upgrade_main_savepoint(true, 2013021801.01);
59a0ba3b
ARN
1649 }
1650
345768b9 1651 if ($oldversion < 2013021902.00) {
47570cc6
DP
1652 // ISO country change: Netherlands Antilles is split into BQ, CW & SX
1653 // http://www.iso.org/iso/iso_3166-1_newsletter_vi-8_split_of_the_dutch_antilles_final-en.pdf
1654 $sql = "UPDATE {user} SET country = '' WHERE country = ?";
1655 $DB->execute($sql, array('AN'));
1656
345768b9 1657 upgrade_main_savepoint(true, 2013021902.00);
47570cc6
DP
1658 }
1659
be1ef2f3
DW
1660 if ($oldversion < 2013022600.00) {
1661 // Delete entries regarding invalid 'interests' option which breaks course.
1662 $DB->delete_records('course_sections_avail_fields', array('userfield' => 'interests'));
1663 $DB->delete_records('course_modules_avail_fields', array('userfield' => 'interests'));
6fea7a5f 1664 // Clear course cache (will be rebuilt on first visit) in case of changes to these.
4202471a 1665 $DB->execute('UPDATE {course} set modinfo = ?, sectioncache = ?', array(null, null));
6fea7a5f 1666
be1ef2f3 1667 upgrade_main_savepoint(true, 2013022600.00);
6fea7a5f 1668 }
1669
e1580085 1670 // Add index to field "timemodified" for grade_grades_history table.
4ce76171 1671 if ($oldversion < 2013030400.00) {
e1580085
TG
1672 $table = new xmldb_table('grade_grades_history');
1673 $field = new xmldb_field('timemodified');
1674
1675 if ($dbman->field_exists($table, $field)) {
1676 $index = new xmldb_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1677 if (!$dbman->index_exists($table, $index)) {
1678 $dbman->add_index($table, $index);
1679 }
1680 }
1681
1682 // Main savepoint reached.
4ce76171 1683 upgrade_main_savepoint(true, 2013030400.00);
e1580085
TG
1684 }
1685
adc4aca4 1686 if ($oldversion < 2013030400.02) {
4f315786
JMV
1687 // Cleanup qformat blackboard settings.
1688 unset_all_config_for_plugin('qformat_blackboard');
1689
adc4aca4 1690 upgrade_main_savepoint(true, 2013030400.02);
4f315786
JMV
1691 }
1692
005c6640
DW
1693 // This is checking to see if the site has been running a specific version with a bug in it
1694 // because this upgrade step is slow and is only needed if the site has been running with the affected versions.
8673a98d 1695 if ($oldversion >= 2012062504.08 && $oldversion < 2012062504.13) {
005c6640
DW
1696 // Retrieve the list of course_sections as a recordset to save memory.
1697 // This is to fix a regression caused by MDL-37939.
1698 // In this case the upgrade step is fixing records where:
1699 // The data in course_sections.sequence contains the correct module id
1700 // The section field for on the course modules table may have been updated to point to the incorrect id.
1701
1702 // This query is looking for sections where the sequence is not in sync with the course_modules table.
1703 // The syntax for the like query is looking for a value in a comma separated list.
1704 // It adds a comma to either site of the list and then searches for LIKE '%,id,%'.
1705 $sequenceconcat = $DB->sql_concat("','", 's.sequence', "','");
1706 $moduleconcat = $DB->sql_concat("'%,'", 'cm.id', "',%'");
1707 $sql = 'SELECT s2.id, s2.course, s2.sequence
8673a98d 1708 FROM {course_sections} s2
005c6640
DW
1709 JOIN(
1710 SELECT DISTINCT s.id
1711 FROM
1712 {course_modules} cm
1713 JOIN {course_sections} s
1714 ON
1715 cm.course = s.course
1716 WHERE cm.section != s.id AND ' . $sequenceconcat . ' LIKE ' . $moduleconcat . '
1717 ) d
1718 ON s2.id = d.id';
1719 $coursesections = $DB->get_recordset_sql($sql);
1720
1721 foreach ($coursesections as $coursesection) {
1722 // Retrieve all of the actual modules in this course and section combination to reduce DB calls.
1723 $actualsectionmodules = $DB->get_records('course_modules',
1724 array('course' => $coursesection->course, 'section' => $coursesection->id), '', 'id, section');
1725
1726 // Break out the current sequence so that we can compare it.
1727 $currentsequence = explode(',', $coursesection->sequence);
1728 $orphanlist = array();
1729
1730 // Check each of the modules in the current sequence.
1731 foreach ($currentsequence as $cmid) {
1732 if (!empty($cmid) && !isset($actualsectionmodules[$cmid])) {
1733 $orphanlist[] = $cmid;
1734 }
1735 }
1736
1737 if (!empty($orphanlist)) {
1738 list($sql, $params) = $DB->get_in_or_equal($orphanlist, SQL_PARAMS_NAMED);
1739 $sql = "id $sql";
1740
1741 $DB->set_field_select('course_modules', 'section', $coursesection->id, $sql, $params);
1742
1743 // And clear the sectioncache and modinfo cache - they'll be regenerated on next use.
1744 $course = new stdClass();
1745 $course->id = $coursesection->course;
1746 $course->sectioncache = null;
1747 $course->modinfo = null;
1748 $DB->update_record('course', $course);
1749 }
1750 }
1751 $coursesections->close();
1752
1753 // No savepoint needed for this change.
1754 }
1755
689096bc
PS
1756 if ($oldversion < 2013032200.01) {
1757 // GD is now always available
c7289da7 1758 set_config('gdversion', 2);
689096bc
PS
1759
1760 upgrade_main_savepoint(true, 2013032200.01);
1761 }
1762
2013daeb 1763 if ($oldversion < 2013032600.03) {
2aecb84d
MS
1764 // Fixing possible wrong MIME type for MIME HTML (MHTML) files.
1765 $extensions = array('%.mht', '%.mhtml');
1766 $select = $DB->sql_like('filename', '?', false);
1767 foreach ($extensions as $extension) {
1768 $DB->set_field_select(
1769 'files',
1770 'mimetype',
1771 'message/rfc822',
1772 $select,
1773 array($extension)
1774 );
1775 }
2013daeb 1776 upgrade_main_savepoint(true, 2013032600.03);
2aecb84d
MS
1777 }
1778
cf140de9 1779 if ($oldversion < 2013032600.04) {
0559a72a
TH
1780 // MDL-31983 broke the quiz version number. Fix it.
1781 $DB->set_field('modules', 'version', '2013021500',
1782 array('name' => 'quiz', 'version' => '2013310100'));
cf140de9 1783 upgrade_main_savepoint(true, 2013032600.04);
0559a72a
TH
1784 }
1785
27806552
YB
1786 if ($oldversion < 2013040200.00) {
1787 // Add openbadges tables.
1788
1789 // Define table 'badge' to be created.
1790 $table = new xmldb_table('badge');
1791
1792 // Adding fields to table 'badge'.
1793 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1794 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'id');
1795 $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null, 'name');
1796 $table->add_field('image', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'description');
1797 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'image');
1798 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'timecreated');
1799 $table->add_field('usercreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'timemodified');
1800 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'usercreated');
1801 $table->add_field('issuername', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'usermodified');
1802 $table->add_field('issuerurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'issuername');
1803 $table->add_field('issuercontact', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'issuerurl');
1804 $table->add_field('expiredate', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'issuercontact');
1805 $table->add_field('expireperiod', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'expiredate');
1806 $table->add_field('type', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'expireperiod');
1807 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'type');
1808 $table->add_field('message', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'courseid');
1809 $table->add_field('messagesubject', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'message');
1810 $table->add_field('attachment', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'messagesubject');
1811 $table->add_field('notification', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'attachment');
1812 $table->add_field('status', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'notification');
1813 $table->add_field('nextcron', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'status');
1814
1815 // Adding keys to table 'badge'.
1816 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1817 $table->add_key('fk_courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1818 $table->add_key('fk_usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
1819 $table->add_key('fk_usercreated', XMLDB_KEY_FOREIGN, array('usercreated'), 'user', array('id'));
1820
1821 // Adding indexes to table 'badge'.
1822 $table->add_index('type', XMLDB_INDEX_NOTUNIQUE, array('type'));
1823
1824 // Conditionally launch create table for 'badge'.
1825 if (!$dbman->table_exists($table)) {
1826 $dbman->create_table($table);
1827 }
1828
1829 // Define table 'badge_criteria' to be created.
1830 $table = new xmldb_table('badge_criteria');
1831
1832 // Adding fields to table 'badge_criteria'.
1833 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1834 $table->add_field('badgeid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
1835 $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'badgeid');
1836 $table->add_field('method', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'criteriatype');
1837
1838 // Adding keys to table 'badge_criteria'.
1839 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1840 $table->add_key('fk_badgeid', XMLDB_KEY_FOREIGN, array('badgeid'), 'badge', array('id'));
1841
1842 // Adding indexes to table 'badge_criteria'.
27806552
YB
1843 $table->add_index('criteriatype', XMLDB_INDEX_NOTUNIQUE, array('criteriatype'));
1844 $table->add_index('badgecriteriatype', XMLDB_INDEX_UNIQUE, array('badgeid', 'criteriatype'));
1845
1846 // Conditionally launch create table for 'badge_criteria'.
1847 if (!$dbman->table_exists($table)) {
1848 $dbman->create_table($table);
1849 }
1850
1851 // Define table 'badge_criteria_param' to be created.
1852 $table = new xmldb_table('badge_criteria_param');
1853
1854 // Adding fields to table 'badge_criteria_param'.
1855 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1856 $table->add_field('critid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id');
1857 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'critid');
1858 $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'name');
1859
1860 // Adding keys to table 'badge_criteria_param'.
1861 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1862 $table->add_key('fk_critid', XMLDB_KEY_FOREIGN, array('critid'), 'badge_criteria', array('id'));
1863
27806552
YB
1864 // Conditionally launch create table for 'badge_criteria_param'.
1865 if (!$dbman->table_exists($table)) {
1866 $dbman->create_table($table);
1867 }
1868
1869 // Define table 'badge_issued' to be created.
1870 $table = new xmldb_table('badge_issued');
1871
1872 // Adding fields to table 'badge_issued'.
1873 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1874 $table->add_field('badgeid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
1875 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'badgeid');
1876 $table->add_field('uniquehash', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'userid');
1877 $table->add_field('dateissued', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'uniquehash');
1878 $table->add_field('dateexpire', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'dateissued');
1879 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'dateexpire');
1880 $table->add_field('issuernotified', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'visible');
1881
1882 // Adding keys to table 'badge_issued'.
1883 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1884 $table->add_key('fk_badgeid', XMLDB_KEY_FOREIGN, array('badgeid'), 'badge', array('id'));
1885 $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1886
27806552
YB
1887 $table->add_index('badgeuser', XMLDB_INDEX_UNIQUE, array('badgeid', 'userid'));
1888
1889 // Conditionally launch create table for 'badge_issued'.
1890 if (!$dbman->table_exists($table)) {
1891 $dbman->create_table($table);
1892 }
1893
1894 // Define table 'badge_criteria_met' to be created.
1895 $table = new xmldb_table('badge_criteria_met');
1896
1897 // Adding fields to table 'badge_criteria_met'.
1898 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1899 $table->add_field('issuedid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'id');
1900 $table->add_field('critid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'issuedid');
1901 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'critid');
1902 $table->add_field('datemet', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'userid');
1903
1904 // Adding keys to table 'badge_criteria_met'
1905 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1906 $table->add_key('fk_critid', XMLDB_KEY_FOREIGN, array('critid'), 'badge_criteria', array('id'));
1907 $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1908 $table->add_key('fk_issuedid', XMLDB_KEY_FOREIGN, array('issuedid'), 'badge_issued', array('id'));
1909
1910 // Conditionally launch create table for 'badge_criteria_met'.
1911 if (!$dbman->table_exists($table)) {
1912 $dbman->create_table($table);
1913 }
1914
1915 // Define table 'badge_manual_award' to be created.
1916 $table = new xmldb_table('badge_manual_award');
1917
1918 // Adding fields to table 'badge_manual_award'.
1919 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1920 $table->add_field('badgeid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id');
1921 $table->add_field('recipientid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'badgeid');
1922 $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'recipientid');
1923 $table->add_field('issuerrole', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'issuerid');
1924 $table->add_field('datemet', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'issuerrole');
1925
1926 // Adding keys to table 'badge_manual_award'.
1927 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2916ca61 1928 $table->add_key('fk_badgeid', XMLDB_KEY_FOREIGN, array('badgeid'), 'badge', array('id'));
27806552
YB
1929 $table->add_key('fk_recipientid', XMLDB_KEY_FOREIGN, array('recipientid'), 'user', array('id'));
1930 $table->add_key('fk_issuerid', XMLDB_KEY_FOREIGN, array('issuerid'), 'user', array('id'));
1931 $table->add_key('fk_issuerrole', XMLDB_KEY_FOREIGN, array('issuerrole'), 'role', array('id'));
1932
1933 // Conditionally launch create table for 'badge_manual_award'.
1934 if (!$dbman->table_exists($table)) {
1935 $dbman->create_table($table);
1936 }
1937
1938 // Define table 'badge_backpack' to be created.
1939 $table = new xmldb_table('badge_backpack');
1940
1941 // Adding fields to table 'badge_backpack'.
1942 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1943 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
1944 $table->add_field('email', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'userid');
1945 $table->add_field('backpackurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'email');
1946 $table->add_field('backpackuid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'backpackurl');
1947 $table->add_field('backpackgid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'backpackuid');
1948 $table->add_field('autosync', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'backpackgid');
1949 $table->add_field('password', XMLDB_TYPE_CHAR, '50', null, null, null, null, 'autosync');
1950
1951 // Adding keys to table 'badge_backpack'.
1952 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1953 $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1954
27806552
YB
1955 // Conditionally launch create table for 'badge_backpack'.
1956 if (!$dbman->table_exists($table)) {
1957 $dbman->create_table($table);
1958 }
1959
1960 // Main savepoint reached.
1961 upgrade_main_savepoint(true, 2013040200.00);
1962 }
1963
eee20b12 1964 if ($oldversion < 2013040201.00) {
e2e9ff65
RT
1965 // Convert name field in event table to text type as RFC-2445 doesn't have any limitation on it.
1966 $table = new xmldb_table('event');
1967 $field = new xmldb_field('name', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1968 if ($dbman->field_exists($table, $field)) {
1969 $dbman->change_field_type($table, $field);
1970 }
1971 // Main savepoint reached.
eee20b12 1972 upgrade_main_savepoint(true, 2013040201.00);
e2e9ff65
RT
1973 }
1974
dcf63185 1975 if ($oldversion < 2013040300.01) {
8819a836
FM
1976
1977 // Define field completionstartonenrol to be dropped from course.
1978 $table = new xmldb_table('course');
1979 $field = new xmldb_field('completionstartonenrol');
1980
1981 // Conditionally launch drop field completionstartonenrol.
1982 if ($dbman->field_exists($table, $field)) {
1983 $dbman->drop_field($table, $field);
1984 }
1985
1986 // Main savepoint reached.
dcf63185 1987 upgrade_main_savepoint(true, 2013040300.01);
8819a836
FM
1988 }
1989
42f563f6 1990 if ($oldversion < 2013041200.00) {
63354ab5 1991 // MDL-29877 Some bad restores created grade items with no category information.
c7ee04bd 1992 $sql = "UPDATE {grade_items}
63354ab5
AD
1993 SET categoryid = courseid
1994 WHERE itemtype <> 'course' and itemtype <> 'category'
1995 AND categoryid IS NULL";
1996 $DB->execute($sql);
42f563f6 1997 upgrade_main_savepoint(true, 2013041200.00);
63354ab5
AD
1998 }
1999
0fd26350
MG
2000 if ($oldversion < 2013041600.00) {
2001 // Copy constants from /course/lib.php instead of including the whole library:
2002 $c = array( 'FRONTPAGENEWS' => 0,
2003 'FRONTPAGECOURSELIST' => 1,
2004 'FRONTPAGECATEGORYNAMES' => 2,
2005 'FRONTPAGETOPICONLY' => 3,
2006 'FRONTPAGECATEGORYCOMBO' => 4,
2007 'FRONTPAGEENROLLEDCOURSELIST' => 5,
2008 'FRONTPAGEALLCOURSELIST' => 6,
2009 'FRONTPAGECOURSESEARCH' => 7);
2010 // Update frontpage settings $CFG->frontpage and $CFG->frontpageloggedin. In 2.4 there was too much of hidden logic about them.
2011 // This script tries to make sure that with the new (more user-friendly) frontpage settings the frontpage looks as similar as possible to what it was before upgrade.
2012 $ncourses = $DB->count_records('course');
2013 foreach (array('frontpage', 'frontpageloggedin') as $configkey) {
2014 if ($frontpage = explode(',', $CFG->{$configkey})) {
2015 $newfrontpage = array();
2016 foreach ($frontpage as $v) {
2017 switch ($v) {
2018 case $c['FRONTPAGENEWS']:
2019 // Not related to course listings, leave as it is.
2020 $newfrontpage[] = $c['FRONTPAGENEWS'];
2021 break;
2022 case $c['FRONTPAGECOURSELIST']:
2023 if ($configkey === 'frontpageloggedin' && empty($CFG->disablemycourses)) {
2024 // In 2.4 unless prohibited in config, the "list of courses" was considered "list of enrolled courses" plus course search box.
2025 $newfrontpage[] = $c['FRONTPAGEENROLLEDCOURSELIST'];
2026 } else if ($ncourses <= 200) {
2027 // Still list of courses was only displayed in there were less than 200 courses in system. Otherwise - search box only.
2028 $newfrontpage[] = $c['FRONTPAGEALLCOURSELIST'];
2029 break; // skip adding search box
2030 }
2031 if (!in_array($c['FRONTPAGECOURSESEARCH'], $newfrontpage)) {
2032 $newfrontpage[] = $c['FRONTPAGECOURSESEARCH'];
2033 }
2034 break;
2035 case $c['FRONTPAGECATEGORYNAMES']:
2036 // In 2.4 search box was displayed automatically after categories list. In 2.5 it is displayed as a separate setting.
2037 $newfrontpage[] = $c['FRONTPAGECATEGORYNAMES'];
2038 if (!in_array($c['FRONTPAGECOURSESEARCH'], $newfrontpage)) {
2039 $newfrontpage[] = $c['FRONTPAGECOURSESEARCH'];
2040 }
2041 break;
2042 case $c['FRONTPAGECATEGORYCOMBO']:
2043 $maxcourses = empty($CFG->numcoursesincombo) ? 500 : $CFG->numcoursesincombo;
2044 // In 2.4 combo list was not displayed if there are more than $CFG->numcoursesincombo courses in the system.
2045 if ($ncourses < $maxcourses) {
2046 $newfrontpage[] = $c['FRONTPAGECATEGORYCOMBO'];
2047 }
2048 if (!in_array($c['FRONTPAGECOURSESEARCH'], $newfrontpage)) {
2049 $newfrontpage[] = $c['FRONTPAGECOURSESEARCH'];
2050 }
2051 break;
2052 }
2053 }
2054 set_config($configkey, join(',', $newfrontpage));
2055 }
2056 }
2057 // $CFG->numcoursesincombo no longer affects whether the combo list is displayed. Setting is deprecated.
2058 unset_config('numcoursesincombo');
2059
2060 upgrade_main_savepoint(true, 2013041600.00);
2061 }
2062
19226a30 2063 if ($oldversion < 2013041601.00) {
e2805314
YB
2064 // Create a new 'badge_external' table first.
2065 // Define table 'badge_external' to be created.
2066 $table = new xmldb_table('badge_external');
7e43a15f 2067
e2805314
YB
2068 // Adding fields to table 'badge_external'.
2069 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
2070 $table->add_field('backpackid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id');
2071 $table->add_field('collectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'backpackid');
7e43a15f 2072
e2805314
YB
2073 // Adding keys to table 'badge_external'.
2074 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2075 $table->add_key('fk_backpackid', XMLDB_KEY_FOREIGN, array('backpackid'), 'badge_backpack', array('id'));
7e43a15f 2076
e2805314
YB
2077 // Conditionally launch create table for 'badge_external'.
2078 if (!$dbman->table_exists($table)) {
2079 $dbman->create_table($table);
2080 }
7e43a15f 2081
e2805314
YB
2082 // Perform user data migration.
2083 $usercollections = $DB->get_records('badge_backpack');
2084 foreach ($usercollections as $usercollection) {
2085 $collection = new stdClass();
2086 $collection->backpackid = $usercollection->id;
2087 $collection->collectionid = $usercollection->backpackgid;
2088 $DB->insert_record('badge_external', $collection);
2089 }
7e43a15f 2090
e2805314
YB
2091 // Finally, drop the column.
2092 // Define field backpackgid to be dropped from 'badge_backpack'.
2093 $table = new xmldb_table('badge_backpack');
2094 $field = new xmldb_field('backpackgid');
7e43a15f 2095
e2805314
YB
2096 // Conditionally launch drop field backpackgid.
2097 if ($dbman->field_exists($table, $field)) {
2098 $dbman->drop_field($table, $field);
2099 }
7e43a15f 2100
e2805314 2101 // Main savepoint reached.
19226a30 2102 upgrade_main_savepoint(true, 2013041601.00);
e2805314
YB
2103 }
2104
359c1d47 2105 if ($oldversion < 2013041601.01) {
0c4c981f
PS
2106 // Changing the default of field descriptionformat on table user to 1.
2107 $table = new xmldb_table('user');
2108 $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1', 'description');
2109
2110 // Launch change of default for field descriptionformat.
2111 $dbman->change_field_default($table, $field);
2112
2113 // Main savepoint reached.
359c1d47 2114 upgrade_main_savepoint(true, 2013041601.01);
0c4c981f
PS
2115 }
2116
fbf76dcd 2117 if ($oldversion < 2013041900.00) {
5cba0c4b 2118 require_once($CFG->dirroot . '/cache/locallib.php');
fbf76dcd
SH
2119 // The features bin needs updating.
2120 cache_config_writer::update_default_config_stores();
2121 // Main savepoint reached.
2122 upgrade_main_savepoint(true, 2013041900.00);
2123 }
2124
b0c4e474 2125 if ($oldversion < 2013042300.00) {
d750d41e
MC
2126 // Adding index to unreadmessageid field of message_working table (MDL-34933)
2127 $table = new xmldb_table('message_working');
2128 $index = new xmldb_index('unreadmessageid_idx', XMLDB_INDEX_NOTUNIQUE, array('unreadmessageid'));
2129
2130 // Conditionally launch add index unreadmessageid
2131 if (!$dbman->index_exists($table, $index)) {
2132 $dbman->add_index($table, $index);
2133 }
2134
2135 // Main savepoint reached.
b0c4e474 2136 upgrade_main_savepoint(true, 2013042300.00);
d750d41e
MC
2137 }
2138
4f5bcf2e
DP
2139 // Moodle v2.5.0 release upgrade line.
2140 // Put any upgrade step following this.
2141
82198dec
DP
2142 if ($oldversion < 2013051400.01) {
2143 // Fix incorrect cc-nc url. Unfortunately the license 'plugins' do
2144 // not give a mechanism to do this.
2145
2146 $sql = "UPDATE {license}
2147 SET source = :url, version = :newversion
2148 WHERE shortname = :shortname AND version = :oldversion";
2149
2150 $params = array(
2151 'url' => 'http://creativecommons.org/licenses/by-nc/3.0/',
2152 'shortname' => 'cc-nc',
2153 'newversion' => '2013051500',
2154 'oldversion' => '2010033100'
2155 );
2156
2157 $DB->execute($sql, $params);
2158
2159 // Main savepoint reached.
2160 upgrade_main_savepoint(true, 2013051400.01);
2161 }
2162
a48dad1f 2163 if ($oldversion < 2013061400.01) {
9feda85d
DP
2164 // Clean up old tokens which haven't been deleted.
2165 $DB->execute("DELETE FROM {user_private_key} WHERE NOT EXISTS
2166 (SELECT 'x' FROM {user} WHERE deleted = 0 AND id = userid)");
2167
2168 // Main savepoint reached.
a48dad1f 2169 upgrade_main_savepoint(true, 2013061400.01);
9feda85d
DP
2170 }
2171
96c90fab 2172 if ($oldversion < 2013061700.00) {
283e448a 2173 // MDL-40103: Remove unused template tables from the database.
672bee2d 2174 // These are now created inline with xmldb_table.
283e448a 2175
62d6f183
RS
2176 $tablestocleanup = array('temp_enroled_template','temp_log_template','backup_files_template','backup_ids_template');
2177 $dbman = $DB->get_manager();
283e448a 2178
62d6f183 2179 foreach ($tablestocleanup as $table) {
283e448a 2180 $xmltable = new xmldb_table($table);
672bee2d 2181 if ($dbman->table_exists($xmltable)) {
62d6f183
RS
2182 $dbman->drop_table($xmltable);
2183 }
283e448a
RS
2184 }
2185
2186 // Main savepoint reached.
96c90fab 2187 upgrade_main_savepoint(true, 2013061700.00);
283e448a 2188 }
96c90fab 2189
64830bf6 2190 if ($oldversion < 2013070800.00) {
4aa4d88d
FM
2191
2192 // Remove orphan repository instances.
0193dd1c
FM
2193 if ($DB->get_dbfamily() === 'mysql') {
2194 $sql = "DELETE {repository_instances} FROM {repository_instances}
2195 LEFT JOIN {context} ON {context}.id = {repository_instances}.contextid
2196 WHERE {context}.id IS NULL";
2197 } else {
2198 $sql = "DELETE FROM {repository_instances}
2199 WHERE NOT EXISTS (
2200 SELECT 'x' FROM {context}
2201 WHERE {context}.id = {repository_instances}.contextid)";
2202 }
2203 $DB->execute($sql);
4aa4d88d
FM
2204
2205 // Main savepoint reached.
64830bf6 2206 upgrade_main_savepoint(true, 2013070800.00);
4aa4d88d
FM
2207 }
2208
a327f25e
AG
2209 if ($oldversion < 2013070800.01) {
2210
2211 // Define field lastnamephonetic to be added to user.
2212 $table = new xmldb_table('user');
2213 $field = new xmldb_field('lastnamephonetic', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'imagealt');
2214 $index = new xmldb_index('lastnamephonetic', XMLDB_INDEX_NOTUNIQUE, array('lastnamephonetic'));
2215
2216 // Conditionally launch add field lastnamephonetic.
2217 if (!$dbman->field_exists($table, $field)) {
2218 $dbman->add_field($table, $field);
2219 $dbman->add_index($table, $index);
2220 }
2221
2222 // Define field firstnamephonetic to be added to user.
2223 $table = new xmldb_table('user');
2224 $field = new xmldb_field('firstnamephonetic', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'lastnamephonetic');
2225 $index = new xmldb_index('firstnamephonetic', XMLDB_INDEX_NOTUNIQUE, array('firstnamephonetic'));
2226
2227 // Conditionally launch add field firstnamephonetic.
2228 if (!$dbman->field_exists($table, $field)) {
2229 $dbman->add_field($table, $field);
2230 $dbman->add_index($table, $index);
2231 }
2232
2233 // Define field alternatename to be added to user.
2234 $table = new xmldb_table('user');
2235 $field = new xmldb_field('middlename', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'firstnamephonetic');
2236 $index = new xmldb_index('middlename', XMLDB_INDEX_NOTUNIQUE, array('middlename'));
2237
2238 // Conditionally launch add field firstnamephonetic.
2239 if (!$dbman->field_exists($table, $field)) {
2240 $dbman->add_field($table, $field);
2241 $dbman->add_index($table, $index);
2242 }
2243
2244 // Define field alternatename to be added to user.
2245 $table = new xmldb_table('user');
2246 $field = new xmldb_field('alternatename', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'middlename');
2247 $index = new xmldb_index('alternatename', XMLDB_INDEX_NOTUNIQUE, array('alternatename'));
2248
2249 // Conditionally launch add field alternatename.
2250 if (!$dbman->field_exists($table, $field)) {
2251 $dbman->add_field($table, $field);
2252 $dbman->add_index($table, $index);
2253 }
2254
2255 // Main savepoint reached.
2256 upgrade_main_savepoint(true, 2013070800.01);
2257 }
2258
c2ca2d8e
DP
2259 if ($oldversion < 2013071500.01) {
2260 // The enrol_authorize plugin has been removed, if there are no records
2261 // and no plugin files then remove the plugin data.
2262 $enrolauthorize = new xmldb_table('enrol_authorize');
2263 $enrolauthorizerefunds = new xmldb_table('enrol_authorize_refunds');
2264
2265 if (!file_exists($CFG->dirroot.'/enrol/authorize/version.php') &&
2266 $dbman->table_exists($enrolauthorize) &&
2267 $dbman->table_exists($enrolauthorizerefunds)) {
2268
2269 $enrolauthorizecount = $DB->count_records('enrol_authorize');
2270 $enrolauthorizerefundcount = $DB->count_records('enrol_authorize_refunds');
2271
2272 if (empty($enrolauthorizecount) && empty($enrolauthorizerefundcount)) {
2273
2274 // Drop the database tables.
2275 $dbman->drop_table($enrolauthorize);
2276 $dbman->drop_table($enrolauthorizerefunds);
2277
2278 // Drop the message provider and associated data manually.
2279 $DB->delete_records('message_providers', array('component' => 'enrol_authorize'));
2280 $DB->delete_records_select('config_plugins', "plugin = 'message' AND ".$DB->sql_like('name', '?', false), array("%_provider_enrol_authorize_%"));
2281 $DB->delete_records_select('user_preferences', $DB->sql_like('name', '?', false), array("message_provider_enrol_authorize_%"));
2282
2283 // Remove capabilities.
2284 capabilities_cleanup('enrol_authorize');
2285
2286 // Remove all other associated config.
2287 unset_all_config_for_plugin('enrol_authorize');
2288 }
2289 }
2290 upgrade_main_savepoint(true, 2013071500.01);
2291 }
2292
e780e5cb 2293 if ($oldversion < 2013071500.02) {
e50220f8
YB
2294 // Define field attachment to be dropped from badge.
2295 $table = new xmldb_table('badge');
2296 $field = new xmldb_field('image');
2297
2298 // Conditionally launch drop field eventtype.
2299 if ($dbman->field_exists($table, $field)) {
2300 $dbman->drop_field($table, $field);
2301 }
2302
e780e5cb 2303 upgrade_main_savepoint(true, 2013071500.02);
e50220f8
YB
2304 }
2305
656250de
PS
2306 if ($oldversion < 2013072600.01) {
2307 upgrade_mssql_nvarcharmax();
2308 upgrade_mssql_varbinarymax();
2309
2310 upgrade_main_savepoint(true, 2013072600.01);
2311 }
2312
8b7378cf 2313 if ($oldversion < 2013081200.00) {
106c55fb
DW
2314 // Define field uploadfiles to be added to external_services.
2315 $table = new xmldb_table('external_services');
2316 $field = new xmldb_field('uploadfiles', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'downloadfiles');
2317
2318 // Conditionally launch add field uploadfiles.
2319 if (!$dbman->field_exists($table, $field)) {
2320 $dbman->add_field($table, $field);
2321 }
2322
2323 // Main savepoint reached.
8b7378cf 2324 upgrade_main_savepoint(true, 2013081200.00);
106c55fb
DW
2325 }
2326
6d8627cb
MN
2327 if ($oldversion < 2013082300.01) {
2328 // Define the table 'backup_logs' and the field 'message' which we will be changing from a char to a text field.
2329 $table = new xmldb_table('backup_logs');
2330 $field = new xmldb_field('message', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'loglevel');
2331
2332 // Perform the change.
2333 $dbman->change_field_type($table, $field);
2334
2335 // Main savepoint reached.
2336 upgrade_main_savepoint(true, 2013082300.01);
2337 }
2338
3d1808c6 2339 // Convert SCORM course format courses to singleactivity.
f6c2af86 2340 if ($oldversion < 2013082700.00) {
3d1808c6
DM
2341 // First set relevant singleactivity settings.
2342 $formatoptions = new stdClass();
2343 $formatoptions->format = 'singleactivity';
2344 $formatoptions->sectionid = 0;
2345 $formatoptions->name = 'activitytype';
2346 $formatoptions->value = 'scorm';
2347
2348 $courses = $DB->get_recordset('course', array('format' => 'scorm'), 'id');
2349 foreach ($courses as $course) {
2350 $formatoptions->courseid = $course->id;
2351 $DB->insert_record('course_format_options', $formatoptions);
2352 }
2353 $courses->close();
2354
2355 // Now update course format for these courses.
2356 $sql = "UPDATE {course}
2357 SET format = 'singleactivity', modinfo = '', sectioncache = ''
2358 WHERE format = 'scorm'";
2359 $DB->execute($sql);
f6c2af86 2360 upgrade_main_savepoint(true, 2013082700.00);
3d1808c6
DM
2361 }
2362
62321a7b
MN
2363 if ($oldversion < 2013090500.01) {
2364 // Define field calendartype to be added to course.
2365 $table = new xmldb_table('course');
2366 $field = new xmldb_field('calendartype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
2367
2368 // Conditionally launch add field calendartype.
2369 if (!$dbman->field_exists($table, $field)) {
2370 $dbman->add_field($table, $field);
2371 }
2372
2373 // Define field calendartype to be added to user.
2374 $table = new xmldb_table('user');
2375 $field = new xmldb_field('calendartype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'gregorian');
2376
2377 // Conditionally launch add field calendartype.
2378 if (!$dbman->field_exists($table, $field)) {
2379 $dbman->add_field($table, $field);
2380 }
2381
2382 // Main savepoint reached.
2383 upgrade_main_savepoint(true, 2013090500.01);
2384 }
2385
299cfee5
MG
2386 if ($oldversion < 2013091000.02) {
2387
2388 // Define field cacherev to be added to course.
2389 $table = new xmldb_table('course');
2390 $field = new xmldb_field('cacherev', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'completionnotify');
2391
2392 // Conditionally launch add field cacherev.
2393 if (!$dbman->field_exists($table, $field)) {
2394 $dbman->add_field($table, $field);
2395 }
2396
2397 // Main savepoint reached.
2398 upgrade_main_savepoint(true, 2013091000.02);
2399 }
2400
64501eee
MG
2401 if ($oldversion < 2013091000.03) {
2402
2403 // Define field modinfo to be dropped from course.
2404 $table = new xmldb_table('course');
2405 $field = new xmldb_field('modinfo');
2406
2407 // Conditionally launch drop field modinfo.
2408 if ($dbman->field_exists($table, $field)) {
2409 $dbman->drop_field($table, $field);
2410 }
2411
2412 // Define field sectioncache to be dropped from course.
2413 $field = new xmldb_field('sectioncache');
2414
2415 // Conditionally launch drop field sectioncache.
2416 if ($dbman->field_exists($table, $field)) {
2417 $dbman->drop_field($table, $field);
2418 }
2419
2420 // Main savepoint reached.
2421 upgrade_main_savepoint(true, 2013091000.03);
2422 }
2423
a8fd33b0
MG
2424 if ($oldversion < 2013091300.01) {
2425
2426 $table = new xmldb_table('user');
2427
2428 // Changing precision of field institution on table user to (255).
2429 $field = new xmldb_field('institution', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'phone2');
2430
2431 // Launch change of precision for field institution.
2432 $dbman->change_field_precision($table, $field);
2433
2434 // Changing precision of field department on table user to (255).
2435 $field = new xmldb_field('department', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'institution');
2436
2437 // Launch change of precision for field department.
2438 $dbman->change_field_precision($table, $field);
2439
2440 // Changing precision of field address on table user to (255).
2441 $field = new xmldb_field('address', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'department');
2442
2443 // Launch change of precision for field address.
2444 $dbman->change_field_precision($table, $field);
2445
2446 // Main savepoint reached.
2447 upgrade_main_savepoint(true, 2013091300.01);
2448 }
2449
e68e4ccf
JP
2450 if ($oldversion < 2013092000.01) {
2451
2452 // Define table question_statistics to be created.
2453 $table = new xmldb_table('question_statistics');
2454
2455 // Adding fields to table question_statistics.
2456 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2457 $table->add_field('hashcode', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2458 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2459 $table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2460 $table->add_field('slot', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
2461 $table->add_field('subquestion', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
2462 $table->add_field('s', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2463 $table->add_field('effectiveweight', XMLDB_TYPE_NUMBER, '15, 5', null, null, null, null);
2464 $table->add_field('negcovar', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
2465 $table->add_field('discriminationindex', XMLDB_TYPE_NUMBER, '15, 5', null, null, null, null);
2466 $table->add_field('discriminativeefficiency', XMLDB_TYPE_NUMBER, '15, 5', null, null, null, null);
2467 $table->add_field('sd', XMLDB_TYPE_NUMBER, '15, 10', null, null, null, null);
2468 $table->add_field('facility', XMLDB_TYPE_NUMBER, '15, 10', null, null, null, null);
2469 $table->add_field('subquestions', XMLDB_TYPE_TEXT, null, null, null, null, null);
2470 $table->add_field('maxmark', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null);
2471 $table->add_field('positions', XMLDB_TYPE_TEXT, null, null, null, null, null);
2472 $table->add_field('randomguessscore', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null);
2473
2474 // Adding keys to table question_statistics.
2475 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2476
2477 // Conditionally launch create table for question_statistics.
2478 if (!$dbman->table_exists($table)) {
2479 $dbman->create_table($table);
2480 }
2481
2482 // Define table question_response_analysis to be created.
2483 $table = new xmldb_table('question_response_analysis');
2484
2485 // Adding fields to table question_response_analysis.
2486 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2487 $table->add_field('hashcode', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2488 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2489 $table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2490 $table->add_field('subqid', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2491 $table->add_field('aid', XMLDB_TYPE_CHAR, '100', null, null, null, null);
2492 $table->add_field('response', XMLDB_TYPE_TEXT, null, null, null, null, null);
2493 $table->add_field('rcount', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
2494 $table->add_field('credit', XMLDB_TYPE_NUMBER, '15, 5', null, XMLDB_NOTNULL, null, null);
2495
2496 // Adding keys to table question_response_analysis.
2497 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2498
2499 // Conditionally launch create table for question_response_analysis.
2500 if (!$dbman->table_exists($table)) {
2501 $dbman->create_table($table);
2502 }
2503
2504 // Main savepoint reached.
2505 upgrade_main_savepoint(true, 2013092000.01);
2506 }
2507
bde002b8
PS
2508 if ($oldversion < 2013092001.01) {
2509 // Force uninstall of deleted tool.
2510 if (!file_exists("$CFG->dirroot/$CFG->admin/tool/bloglevelupgrade")) {
2511 // Remove capabilities.
2512 capabilities_cleanup('tool_bloglevelupgrade');
2513 // Remove all other associated config.
2514 unset_all_config_for_plugin('tool_bloglevelupgrade');
2515 }
2516 upgrade_main_savepoint(true, 2013092001.01);
2517 }
2518
2519 if ($oldversion < 2013092001.02) {
2520 // Define field version to be dropped from modules.
2521 $table = new xmldb_table('modules');
2522 $field = new xmldb_field('version');
2523
2524 // Conditionally launch drop field version.
2525 if ($dbman->field_exists($table, $field)) {
2526 // Migrate all plugin version info to config_plugins table.
2527 $modules = $DB->get_records('modules');
2528 foreach ($modules as $module) {
2529 set_config('version', $module->version, 'mod_'.$module->name);
2530 }
2531 unset($modules);
2532
2533 $dbman->drop_field($table, $field);
2534 }
2535
2536 // Define field version to be dropped from block.
2537 $table = new xmldb_table('block');
2538 $field = new xmldb_field('version');
2539
2540 // Conditionally launch drop field version.
2541 if ($dbman->field_exists($table, $field)) {
2542 $blocks = $DB->get_records('block');
2543 foreach ($blocks as $block) {
2544 set_config('version', $block->version, 'block_'.$block->name);
2545 }
2546 unset($blocks);
2547
2548 $dbman->drop_field($table, $field);
2549 }
2550
2551 // Main savepoint reached.
2552 upgrade_main_savepoint(true, 2013092001.02);
2553 }
2554
e2a61ee3
MG
2555 if ($oldversion < 2013092700.01) {
2556
2557 $table = new xmldb_table('files');
2558
2559 // Define field referencelastsync to be dropped from files.
2560 $field = new xmldb_field('referencelastsync');
2561
2562 // Conditionally launch drop field referencelastsync.
2563 if ($dbman->field_exists($table, $field)) {
2564 $dbman->drop_field($table, $field);
2565 }
2566
2567 // Define field referencelifetime to be dropped from files.
2568 $field = new xmldb_field('referencelifetime');
2569
2570 // Conditionally launch drop field referencelifetime.
2571 if ($dbman->field_exists($table, $field)) {
2572 $dbman->drop_field($table, $field);
2573 }
2574
2575 // Main savepoint reached.
2576 upgrade_main_savepoint(true, 2013092700.01);
2577 }
5e8331c8 2578
a4cdd6d2 2579 return true;
51003653 2580}