weekly release 2.8dev
[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) {
e2049782 88 global $CFG, $USER, $DB, $OUTPUT, $SITE, $COURSE;
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
e2049782
MG
319 // Since structure of 'course' table has changed we need to re-read $SITE from DB.
320 $SITE = $DB->get_record('course', array('id' => $SITE->id));
321 $COURSE = clone($SITE);
322
9665ecd2
TH
323 upgrade_main_savepoint(true, 2012031500.02);
324 }
325
326 if ($oldversion < 2012031500.03) {
327
328 // Define table course_allowed_modules to be dropped
329 $table = new xmldb_table('course_allowed_modules');
330
331 // Conditionally launch drop table for course_allowed_modules
332 if ($dbman->table_exists($table)) {
333 $dbman->drop_table($table);
334 }
335
336 upgrade_main_savepoint(true, 2012031500.03);
337 }
338
339 if ($oldversion < 2012031500.04) {
340 // Clean up the old admin settings.
341 unset_config('restrictmodulesfor');
342 unset_config('restrictbydefault');
343 unset_config('defaultallowedmodules');
344
345 upgrade_main_savepoint(true, 2012031500.04);
346 }
e9e4a4a6 347
e63f95d7
PS
348 if ($oldversion < 2012032300.02) {
349 // Migrate the old admin debug setting.
350 if ($CFG->debug == 38911) {
351 set_config('debug', DEBUG_DEVELOPER);
352 } else if ($CFG->debug == 6143) {
353 set_config('debug', DEBUG_ALL);
354 }
355 upgrade_main_savepoint(true, 2012032300.02);
356 }
357
e7e0f8d2
DP
358 if ($oldversion < 2012042300.00) {
359 // This change makes the course_section index unique.
360
cf76b335 361 // xmldb does not allow changing index uniqueness - instead we must drop
e7e0f8d2
DP
362 // index then add it again
363 $table = new xmldb_table('course_sections');
cf76b335 364 $index = new xmldb_index('course_section', XMLDB_INDEX_NOTUNIQUE, array('course', 'section'));
e7e0f8d2
DP
365
366 // Conditionally launch drop index course_section
367 if ($dbman->index_exists($table, $index)) {
368 $dbman->drop_index($table, $index);
369 }
cf76b335 370
371 // Look for any duplicate course_sections entries. There should not be
372 // any but on some busy systems we found a few, maybe due to previous
373 // bugs.
374 $transaction = $DB->start_delegated_transaction();
375 $rs = $DB->get_recordset_sql('
376 SELECT DISTINCT
377 cs.id, cs.course
378 FROM
379 {course_sections} cs
380 INNER JOIN {course_sections} older
381 ON cs.course = older.course AND cs.section = older.section
382 AND older.id < cs.id');
383 foreach ($rs as $rec) {
384 $DB->delete_records('course_sections', array('id' => $rec->id));
026082b4
DP
385 // We can't use rebuild_course_cache() here because introducing sectioncache later
386 // so reset modinfo manually.
387 $DB->set_field('course', 'modinfo', null, array('id' => $rec->course));
cf76b335 388 }
389 $rs->close();
390 $transaction->allow_commit();
391
392 // Define index course_section (unique) to be added to course_sections
393 $index = new xmldb_index('course_section', XMLDB_INDEX_UNIQUE, array('course', 'section'));
394
395 // Conditionally launch add index course_section
396 if (!$dbman->index_exists($table, $index)) {
397 $dbman->add_index($table, $index);
398 }
399
400 // Main savepoint reached
e7e0f8d2 401 upgrade_main_savepoint(true, 2012042300.00);
cf76b335 402 }
296c0247 403
ede323e2 404 if ($oldversion < 2012042300.02) {
60829d80 405 require_once($CFG->dirroot.'/completion/criteria/completion_criteria.php');
ede323e2
ARN
406 // Delete orphaned criteria which were left when modules were removed
407 if ($DB->get_dbfamily() === 'mysql') {
408 $sql = "DELETE cc FROM {course_completion_criteria} cc
409 LEFT JOIN {course_modules} cm ON cm.id = cc.moduleinstance
410 WHERE cm.id IS NULL AND cc.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
411 } else {
412 $sql = "DELETE FROM {course_completion_criteria}
413 WHERE NOT EXISTS (
414 SELECT 'x' FROM {course_modules}
415 WHERE {course_modules}.id = {course_completion_criteria}.moduleinstance)
2ea2549e 416 AND {course_completion_criteria}.criteriatype = ".COMPLETION_CRITERIA_TYPE_ACTIVITY;
ede323e2
ARN
417 }
418 $DB->execute($sql);
419
420 // Main savepoint reached
421 upgrade_main_savepoint(true, 2012042300.02);
422 }
423
0dda790c 424 if ($oldversion < 2012050300.01) {
fa530687
PS
425 // Make sure deleted users do not have picture flag.
426 $DB->set_field('user', 'picture', 0, array('deleted'=>1, 'picture'=>1));
0dda790c 427 upgrade_main_savepoint(true, 2012050300.01);
fa530687
PS
428 }
429
0dda790c 430 if ($oldversion < 2012050300.02) {
4d254790
PS
431
432 // Changing precision of field picture on table user to (10)
433 $table = new xmldb_table('user');
434 $field = new xmldb_field('picture', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'secret');
435
436 // Launch change of precision for field picture
437 $dbman->change_field_precision($table, $field);
438
439 // Main savepoint reached
0dda790c 440 upgrade_main_savepoint(true, 2012050300.02);
4d254790
PS
441 }
442
1156d38d
DP
443 if ($oldversion < 2012050300.03) {
444
445 // Define field coursedisplay to be added to course
446 $table = new xmldb_table('course');
447 $field = new xmldb_field('coursedisplay', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'completionnotify');
448
449 // Conditionally launch add field coursedisplay
450 if (!$dbman->field_exists($table, $field)) {
451 $dbman->add_field($table, $field);
452 }
453
e2049782
MG
454 // Since structure of 'course' table has changed we need to re-read $SITE from DB.
455 $SITE = $DB->get_record('course', array('id' => $SITE->id));
456 $COURSE = clone($SITE);
457
1156d38d
DP
458 // Main savepoint reached
459 upgrade_main_savepoint(true, 2012050300.03);
460 }
461
b9bcdb54
DP
462 if ($oldversion < 2012050300.04) {
463
464 // Define table course_display to be dropped
465 $table = new xmldb_table('course_display');
466
467 // Conditionally launch drop table for course_display
468 if ($dbman->table_exists($table)) {
469 $dbman->drop_table($table);
470 }
471
472 // Main savepoint reached
473 upgrade_main_savepoint(true, 2012050300.04);
474 }
475
476 if ($oldversion < 2012050300.05) {
477
478 // Clean up removed admin setting.
479 unset_config('navlinkcoursesections');
480
481 upgrade_main_savepoint(true, 2012050300.05);
482 }
483
c119a2ee
PS
484 if ($oldversion < 2012050400.01) {
485
486 // Define index sortorder (not unique) to be added to course
487 $table = new xmldb_table('course');
488 $index = new xmldb_index('sortorder', XMLDB_INDEX_NOTUNIQUE, array('sortorder'));
489
490 // Conditionally launch add index sortorder
491 if (!$dbman->index_exists($table, $index)) {
492 $dbman->add_index($table, $index);
493 }
494
495 // Main savepoint reached
496 upgrade_main_savepoint(true, 2012050400.01);
497 }
498
bbb0a6eb
SH
499 if ($oldversion < 2012050400.02) {
500
9fa74379
DP
501 // Clean up removed admin setting.
502 unset_config('enablecourseajax');
503
bbb0a6eb 504 upgrade_main_savepoint(true, 2012050400.02);
9fa74379
DP
505 }
506
74b714df
ARN
507 if ($oldversion < 2012051100.01) {
508
509 // Define field idnumber to be added to groups
510 $table = new xmldb_table('groups');
511 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'courseid');
512 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
513
514 // Conditionally launch add field idnumber
515 if (!$dbman->field_exists($table, $field)) {
516 $dbman->add_field($table, $field);
517 }
518
519 // Conditionally launch add index idnumber
520 if (!$dbman->index_exists($table, $index)) {
521 $dbman->add_index($table, $index);
522 }
523
524 // Define field idnumber to be added to groupings
525 $table = new xmldb_table('groupings');
526 $field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'name');
527 $index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
528
529 // Conditionally launch add field idnumber
530 if (!$dbman->field_exists($table, $field)) {
531 $dbman->add_field($table, $field);
532 }
533
534 // Conditionally launch add index idnumber
535 if (!$dbman->index_exists($table, $index)) {
536 $dbman->add_index($table, $index);
537 }
538
539 // Main savepoint reached
540 upgrade_main_savepoint(true, 2012051100.01);
541 }
542
dd420aba
EL
543 if ($oldversion < 2012051100.03) {
544
ce4dfd27 545 // Amend course table to add sectioncache cache
546 $table = new xmldb_table('course');
547 $field = new xmldb_field('sectioncache', XMLDB_TYPE_TEXT, null, null, null, null, null, 'showgrades');
548 if (!$dbman->field_exists($table, $field)) {
549 $dbman->add_field($table, $field);
550 }
551
552 // Amend course_sections to add date, time and groupingid availability
553 // conditions and a setting about whether to show them
554 $table = new xmldb_table('course_sections');
dd420aba 555 $field = new xmldb_field('availablefrom', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'visible');
ce4dfd27 556 if (!$dbman->field_exists($table, $field)) {
557 $dbman->add_field($table, $field);
558 }
dd420aba 559 $field = new xmldb_field('availableuntil', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'availablefrom');
ce4dfd27 560 if (!$dbman->field_exists($table, $field)) {
561 $dbman->add_field($table, $field);
562 }
dd420aba 563 $field = new xmldb_field('showavailability', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'availableuntil');
ce4dfd27 564 // Conditionally launch add field showavailability
565 if (!$dbman->field_exists($table, $field)) {
566 $dbman->add_field($table, $field);
567 }
dd420aba 568 $field = new xmldb_field('groupingid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'showavailability');
ce4dfd27 569 // Conditionally launch add field groupingid
570 if (!$dbman->field_exists($table, $field)) {
571 $dbman->add_field($table, $field);
572 }
573
e2049782
MG
574 // Since structure of 'course' table has changed we need to re-read $SITE from DB.
575 $SITE = $DB->get_record('course', array('id' => $SITE->id));
576 $COURSE = clone($SITE);
577
ce4dfd27 578 // Add course_sections_availability to add completion & grade availability conditions
579 $table = new xmldb_table('course_sections_availability');
580
581 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
582 $table->add_field('coursesectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
583 $table->add_field('sourcecmid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
584 $table->add_field('requiredcompletion', XMLDB_TYPE_INTEGER, '1', null, null, null, null);
585 $table->add_field('gradeitemid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
586 $table->add_field('grademin', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
587 $table->add_field('grademax', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
588
589 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
dd420aba
EL
590 $table->add_key('coursesectionid', XMLDB_KEY_FOREIGN, array('coursesectionid'), 'course_sections', array('id'));
591 $table->add_key('sourcecmid', XMLDB_KEY_FOREIGN, array('sourcecmid'), 'course_modules', array('id'));
592 $table->add_key('gradeitemid', XMLDB_KEY_FOREIGN, array('gradeitemid'), 'grade_items', array('id'));
ce4dfd27 593
594 if (!$dbman->table_exists($table)) {
595 $dbman->create_table($table);
596 }
597
94dc3c7d 598 // Main savepoint reached
dd420aba 599 upgrade_main_savepoint(true, 2012051100.03);
ce4dfd27 600 }
601
67233725
DC
602 if ($oldversion < 2012052100.00) {
603
7051415c 604 // Define field referencefileid to be added to files.
67233725
DC
605 $table = new xmldb_table('files');
606
7051415c 607 // Define field referencefileid to be added to files.
67233725 608 $field = new xmldb_field('referencefileid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'sortorder');
7051415c
DC
609
610 // Conditionally launch add field referencefileid.
67233725
DC
611 if (!$dbman->field_exists($table, $field)) {
612 $dbman->add_field($table, $field);
613 }
614
7051415c 615 // Define field referencelastsync to be added to files.
67233725 616 $field = new xmldb_field('referencelastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencefileid');
7051415c
DC
617
618 // Conditionally launch add field referencelastsync.
67233725
DC
619 if (!$dbman->field_exists($table, $field)) {
620 $dbman->add_field($table, $field);
621 }
622
7051415c
DC
623 // Define field referencelifetime to be added to files.
624 $field = new xmldb_field('referencelifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'referencelastsync');
625
626 // Conditionally launch add field referencelifetime.
67233725
DC
627 if (!$dbman->field_exists($table, $field)) {
628 $dbman->add_field($table, $field);
629 }
630
631 $key = new xmldb_key('referencefileid', XMLDB_KEY_FOREIGN, array('referencefileid'), 'files_reference', array('id'));
632 // Launch add key referencefileid
633 $dbman->add_key($table, $key);
634
635 // Define table files_reference to be created.
636 $table = new xmldb_table('files_reference');
637
638 // Adding fields to table files_reference.
639 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
67233725
DC
640 $table->add_field('repositoryid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
641 $table->add_field('lastsync', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
642 $table->add_field('lifetime', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
643 $table->add_field('reference', XMLDB_TYPE_TEXT, null, null, null, null, null);
644
645 // Adding keys to table files_reference.
646 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
647 $table->add_key('repositoryid', XMLDB_KEY_FOREIGN, array('repositoryid'), 'repository_instances', array('id'));
648
649 // Conditionally launch create table for files_reference
650 if (!$dbman->table_exists($table)) {
651 $dbman->create_table($table);
652 }
653
654 // Main savepoint reached
655 upgrade_main_savepoint(true, 2012052100.00);
656 }
657
3f1bca9d 658 if ($oldversion < 2012052500.03) { // fix invalid course_completion_records MDL-27368
d2e3a1b4
DM
659 //first get all instances of duplicate records
660 $sql = 'SELECT userid, course FROM {course_completions} WHERE (deleted IS NULL OR deleted <> 1) GROUP BY userid, course HAVING (count(id) > 1)';
661 $duplicates = $DB->get_recordset_sql($sql, array());
662
663 foreach ($duplicates as $duplicate) {
664 $pointer = 0;
665 //now get all the records for this user/course
666 $sql = 'userid = ? AND course = ? AND (deleted IS NULL OR deleted <> 1)';
667 $completions = $DB->get_records_select('course_completions', $sql,
668 array($duplicate->userid, $duplicate->course), 'timecompleted DESC, timestarted DESC');
669 $needsupdate = false;
670 $origcompletion = null;
671 foreach ($completions as $completion) {
672 $pointer++;
673 if ($pointer === 1) { //keep 1st record but delete all others.
674 $origcompletion = $completion;
675 } else {
676 //we need to keep the "oldest" of all these fields as the valid completion record.
677 $fieldstocheck = array('timecompleted', 'timestarted', 'timeenrolled');
678 foreach ($fieldstocheck as $f) {
679 if ($origcompletion->$f > $completion->$f) {
680 $origcompletion->$f = $completion->$f;
681 $needsupdate = true;
682 }
683 }
684 $DB->delete_records('course_completions', array('id'=>$completion->id));
685 }
686 }
687 if ($needsupdate) {
688 $DB->update_record('course_completions', $origcompletion);
689 }
690 }
691
692 // Main savepoint reached
3f1bca9d 693 upgrade_main_savepoint(true, 2012052500.03);
d2e3a1b4 694 }
424a19b1 695
bd633dda 696 if ($oldversion < 2012052900.00) {
e6f55285
SH
697 // Clean up all duplicate records in the course_completions table in preparation
698 // for adding a new index there.
424a19b1
AB
699 upgrade_course_completion_remove_duplicates(
700 'course_completions',
701 array('userid', 'course'),
702 array('timecompleted', 'timestarted', 'timeenrolled')
703 );
704
e6f55285
SH
705 // Main savepoint reached
706 upgrade_main_savepoint(true, 2012052900.00);
707 }
708
709 if ($oldversion < 2012052900.01) {
710 // Add indexes to prevent new duplicates in the course_completions table.
424a19b1
AB
711 // Define index useridcourse (unique) to be added to course_completions
712 $table = new xmldb_table('course_completions');
713 $index = new xmldb_index('useridcourse', XMLDB_INDEX_UNIQUE, array('userid', 'course'));
714
424a19b1
AB
715 // Conditionally launch add index useridcourse
716 if (!$dbman->index_exists($table, $index)) {
717 $dbman->add_index($table, $index);
718 }
719
720 // Main savepoint reached
e6f55285 721 upgrade_main_savepoint(true, 2012052900.01);
424a19b1
AB
722 }
723
e6f55285
SH
724 if ($oldversion < 2012052900.02) {
725 // Clean up all duplicate records in the course_completion_crit_compl table in preparation
726 // for adding a new index there.
424a19b1
AB
727 upgrade_course_completion_remove_duplicates(
728 'course_completion_crit_compl',
729 array('userid', 'course', 'criteriaid'),
730 array('timecompleted')
731 );
732
e6f55285
SH
733 // Main savepoint reached
734 upgrade_main_savepoint(true, 2012052900.02);
735 }
424a19b1 736
e6f55285
SH
737 if ($oldversion < 2012052900.03) {
738 // Add indexes to prevent new duplicates in the course_completion_crit_compl table.
424a19b1
AB
739 // Define index useridcoursecriteraid (unique) to be added to course_completion_crit_compl
740 $table = new xmldb_table('course_completion_crit_compl');
741 $index = new xmldb_index('useridcoursecriteraid', XMLDB_INDEX_UNIQUE, array('userid', 'course', 'criteriaid'));
742
743 // Conditionally launch add index useridcoursecriteraid
744 if (!$dbman->index_exists($table, $index)) {
745 $dbman->add_index($table, $index);
746 }
747
748 // Main savepoint reached
e6f55285 749 upgrade_main_savepoint(true, 2012052900.03);
424a19b1
AB
750 }
751
e6f55285
SH
752 if ($oldversion < 2012052900.04) {
753 // Clean up all duplicate records in the course_completion_aggr_methd table in preparation
754 // for adding a new index there.
424a19b1
AB
755 upgrade_course_completion_remove_duplicates(
756 'course_completion_aggr_methd',
757 array('course', 'criteriatype')
758 );
759
e6f55285
SH
760 // Main savepoint reached
761 upgrade_main_savepoint(true, 2012052900.04);
762 }
763
764 if ($oldversion < 2012052900.05) {
765 // Add indexes to prevent new duplicates in the course_completion_aggr_methd table.
424a19b1
AB
766 // Define index coursecriteratype (unique) to be added to course_completion_aggr_methd
767 $table = new xmldb_table('course_completion_aggr_methd');
768 $index = new xmldb_index('coursecriteriatype', XMLDB_INDEX_UNIQUE, array('course', 'criteriatype'));
769
770 // Conditionally launch add index coursecriteratype
771 if (!$dbman->index_exists($table, $index)) {
772 $dbman->add_index($table, $index);
773 }
774
775 // Main savepoint reached
e6f55285 776 upgrade_main_savepoint(true, 2012052900.05);
424a19b1
AB
777 }
778
79c2b509 779 if ($oldversion < 2012060600.01) {
51003653
DM
780 // Add field referencehash to files_reference
781 $table = new xmldb_table('files_reference');
782 $field = new xmldb_field('referencehash', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null, 'reference');
783 if (!$dbman->field_exists($table, $field)) {
784 $dbman->add_field($table, $field);
785 }
79c2b509 786 upgrade_main_savepoint(true, 2012060600.01);
51003653
DM
787 }
788
79c2b509 789 if ($oldversion < 2012060600.02) {
dccba8bc
DM
790 // Populate referencehash field with SHA1 hash of the reference - this shoudl affect only 2.3dev sites
791 // that were using the feature for testing. Production sites have the table empty.
792 $rs = $DB->get_recordset('files_reference', null, '', 'id, reference');
793 foreach ($rs as $record) {
794 $hash = sha1($record->reference);
795 $DB->set_field('files_reference', 'referencehash', $hash, array('id' => $record->id));
796 }
797 $rs->close();
798
79c2b509 799 upgrade_main_savepoint(true, 2012060600.02);
dccba8bc
DM
800 }
801
79c2b509 802 if ($oldversion < 2012060600.03) {
8f47a60e
DM
803 // Merge duplicate records in files_reference that were created during the development
804 // phase at 2.3dev sites. This is needed so we can create the unique index over
805 // (repositoryid, referencehash) fields.
806 $sql = "SELECT repositoryid, referencehash, MIN(id) AS minid
807 FROM {files_reference}
808 GROUP BY repositoryid, referencehash
4a5be0e9 809 HAVING COUNT(*) > 1";
8f47a60e
DM
810 $duprs = $DB->get_recordset_sql($sql);
811 foreach ($duprs as $duprec) {
812 // get the list of all ids in {files_reference} that need to be remapped
813 $dupids = $DB->get_records_select('files_reference', "repositoryid = ? AND referencehash = ? AND id > ?",
814 array($duprec->repositoryid, $duprec->referencehash, $duprec->minid), '', 'id');
815 $dupids = array_keys($dupids);
816 // relink records in {files} that are now referring to a duplicate record
817 // in {files_reference} to refer to the first one
818 list($subsql, $subparams) = $DB->get_in_or_equal($dupids);
819 $DB->set_field_select('files', 'referencefileid', $duprec->minid, "referencefileid $subsql", $subparams);
820 // and finally remove all orphaned records from {files_reference}
821 $DB->delete_records_list('files_reference', 'id', $dupids);
822 }
823 $duprs->close();
824
79c2b509 825 upgrade_main_savepoint(true, 2012060600.03);
8f47a60e
DM
826 }
827
79c2b509 828 if ($oldversion < 2012060600.04) {
6bbf31c3
DM
829 // Add a unique index over repositoryid and referencehash fields in files_reference table
830 $table = new xmldb_table('files_reference');
831 $index = new xmldb_index('uq_external_file', XMLDB_INDEX_UNIQUE, array('repositoryid', 'referencehash'));
832
833 if (!$dbman->index_exists($table, $index)) {
834 $dbman->add_index($table, $index);
835 }
836
79c2b509 837 upgrade_main_savepoint(true, 2012060600.04);
6bbf31c3
DM
838 }
839
a80b5a0c
DP
840 if ($oldversion < 2012061800.01) {
841
842 // Define field screenreader to be dropped from user
843 $table = new xmldb_table('user');
844 $field = new xmldb_field('ajax');
845
846 // Conditionally launch drop field screenreader
847 if ($dbman->field_exists($table, $field)) {
848 $dbman->drop_field($table, $field);
849 }
850
851 // Main savepoint reached
852 upgrade_main_savepoint(true, 2012061800.01);
853 }
854
088c374a 855 if ($oldversion < 2012062000.00) {
38ca2f1a
DM
856 // Add field newcontextid to backup_files_template
857 $table = new xmldb_table('backup_files_template');
858 $field = new xmldb_field('newcontextid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'info');
859
860 if (!$dbman->field_exists($table, $field)) {
861 $dbman->add_field($table, $field);
862 }
863
088c374a 864 upgrade_main_savepoint(true, 2012062000.00);
38ca2f1a
DM
865 }
866
088c374a 867 if ($oldversion < 2012062000.01) {
38ca2f1a
DM
868 // Add field newitemid to backup_files_template
869 $table = new xmldb_table('backup_files_template');
870 $field = new xmldb_field('newitemid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'newcontextid');
871
872 if (!$dbman->field_exists($table, $field)) {
873 $dbman->add_field($table, $field);
874 }
875
088c374a 876 upgrade_main_savepoint(true, 2012062000.01);
38ca2f1a
DM
877 }
878
44cb29a0
EL
879 // Moodle v2.3.0 release upgrade line
880 // Put any upgrade step following this
881
44cb29a0 882 if ($oldversion < 2012062500.02) {
3f4a2999
883 // Drop some old backup tables, not used anymore
884
885 // Define table backup_files to be dropped
886 $table = new xmldb_table('backup_files');
887
888 // Conditionally launch drop table for backup_files
889 if ($dbman->table_exists($table)) {
890 $dbman->drop_table($table);
891 }
892
893 // Define table backup_ids to be dropped
894 $table = new xmldb_table('backup_ids');
895
896 // Conditionally launch drop table for backup_ids
66dd3e5d
SH
897 if ($dbman->table_exists($table)) {
898 $dbman->drop_table($table);
3f4a2999
899 }
900
901 // Main savepoint reached
44cb29a0 902 upgrade_main_savepoint(true, 2012062500.02);
3f4a2999
903 }
904
00142ce5 905 if ($oldversion < 2012070600.04) {
141d3c86
SH
906 // Define table course_modules_avail_fields to be created
907 $table = new xmldb_table('course_modules_avail_fields');
908
909 // Adding fields to table course_modules_avail_fields
910 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
911 $table->add_field('coursemoduleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
912 $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
913 $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
914 $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
915 $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
916
917 // Adding keys to table course_modules_avail_fields
918 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
919 $table->add_key('coursemoduleid', XMLDB_KEY_FOREIGN, array('coursemoduleid'), 'course_modules', array('id'));
920
921 // Conditionally launch create table for course_modules_avail_fields
922 if (!$dbman->table_exists($table)) {
923 $dbman->create_table($table);
924 }
925
926 // Main savepoint reached
00142ce5 927 upgrade_main_savepoint(true, 2012070600.04);
141d3c86
SH
928 }
929
00142ce5 930 if ($oldversion < 2012070600.05) {
141d3c86
SH
931 // Define table course_sections_avail_fields to be created
932 $table = new xmldb_table('course_sections_avail_fields');
933
934 // Adding fields to table course_sections_avail_fields
935 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
936 $table->add_field('coursesectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
937 $table->add_field('userfield', XMLDB_TYPE_CHAR, '50', null, null, null, null);
938 $table->add_field('customfieldid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
939 $table->add_field('operator', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
940 $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
941
942 // Adding keys to table course_sections_avail_fields
943 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
944 $table->add_key('coursesectionid', XMLDB_KEY_FOREIGN, array('coursesectionid'), 'course_sections', array('id'));
945
946 // Conditionally launch create table for course_sections_avail_fields
947 if (!$dbman->table_exists($table)) {
948 $dbman->create_table($table);
949 }
950
951 // Main savepoint reached
00142ce5 952 upgrade_main_savepoint(true, 2012070600.05);
141d3c86 953 }
a80b5a0c 954
00142ce5 955 if ($oldversion < 2012070600.06) {
a2a2e7fb
SH
956
957 // Drop "deleted" fields
46eca1f7
AB
958 $table = new xmldb_table('course_completions');
959 $field = new xmldb_field('timenotified');
a2a2e7fb
SH
960 $field = new xmldb_field('deleted');
961
962 // Conditionally launch drop field deleted from course_completions
963 if ($dbman->field_exists($table, $field)) {
66dd3e5d 964 $dbman->drop_field($table, $field);
a2a2e7fb 965 }
46eca1f7 966
a2a2e7fb 967 $field = new xmldb_field('timenotified');
46eca1f7
AB
968 // Conditionally launch drop field timenotified from course_completions
969 if ($dbman->field_exists($table, $field)) {
970 $dbman->drop_field($table, $field);
971 }
972
973 // Main savepoint reached
00142ce5 974 upgrade_main_savepoint(true, 2012070600.06);
46eca1f7
AB
975 }
976
00142ce5 977 if ($oldversion < 2012070600.07) {
a2a2e7fb
SH
978 $table = new xmldb_table('course_completion_crit_compl');
979 $field = new xmldb_field('deleted');
980
981 // Conditionally launch drop field deleted from course_completion_crit_compl
982 if ($dbman->field_exists($table, $field)) {
983 $dbman->drop_field($table, $field);
984 }
985 // Main savepoint reached
00142ce5 986 upgrade_main_savepoint(true, 2012070600.07);
a2a2e7fb
SH
987 }
988
00142ce5 989 if ($oldversion < 2012070600.08) {
a2a2e7fb
SH
990
991 // Drop unused table "course_completion_notify"
992 $table = new xmldb_table('course_completion_notify');
993
994 // Conditionally launch drop table course_completion_notify
995 if ($dbman->table_exists($table)) {
996 $dbman->drop_table($table);
997 }
998
999 // Main savepoint reached
00142ce5 1000 upgrade_main_savepoint(true, 2012070600.08);
a2a2e7fb
SH
1001 }
1002
00142ce5 1003 if ($oldversion < 2012070600.09) {
bd991d03
PS
1004
1005 // Define index path (not unique) to be added to context
1006 $table = new xmldb_table('context');
1007 $index = new xmldb_index('path', XMLDB_INDEX_NOTUNIQUE, array('path'), array('varchar_pattern_ops'));
1008
1009 // Recreate index with new pattern hint
1010 if ($DB->get_dbfamily() === 'postgres') {
1011 if ($dbman->index_exists($table, $index)) {
1012 $dbman->drop_index($table, $index);
1013 }
1014 $dbman->add_index($table, $index);
1015 }
1016
1017 // Main savepoint reached
00142ce5 1018 upgrade_main_savepoint(true, 2012070600.09);
bd991d03
PS
1019 }
1020
00142ce5 1021 if ($oldversion < 2012070600.10) {
c52551dc
PS
1022
1023 // Define index name (unique) to be dropped form role
1024 $table = new xmldb_table('role');
1025 $index = new xmldb_index('name', XMLDB_INDEX_UNIQUE, array('name'));
1026
1027 // Conditionally launch drop index name
1028 if ($dbman->index_exists($table, $index)) {
1029 $dbman->drop_index($table, $index);
1030 }
1031
1032 // Main savepoint reached
00142ce5 1033 upgrade_main_savepoint(true, 2012070600.10);
c52551dc
PS
1034 }
1035
f0d6e53c 1036 if ($oldversion < 2012070600.11) {
79a47143
1037
1038 // Define index component-itemid-userid (not unique) to be added to role_assignments
1039 $table = new xmldb_table('role_assignments');
1040 $index = new xmldb_index('component-itemid-userid', XMLDB_INDEX_NOTUNIQUE, array('component', 'itemid', 'userid'));
1041
1042 // Conditionally launch add index component-itemid-userid
1043 if (!$dbman->index_exists($table, $index)) {
1044 $dbman->add_index($table, $index);
1045 }
1046
1047 // Main savepoint reached
f0d6e53c 1048 upgrade_main_savepoint(true, 2012070600.11);
79a47143
1049 }
1050
c8b3346c
PS
1051 if ($oldversion < 2012071900.01) {
1052 // Cleanup after simpeltests tool
1053 capabilities_cleanup('tool_unittest');
1054 unset_all_config_for_plugin('tool_unittest');
1055
1056 upgrade_main_savepoint(true, 2012071900.01);
1057 }
1058
b7db7803 1059 if ($oldversion < 2012072400.00) {
13725b37
PS
1060 // Remove obsolete xhtml strict setting - use THEME->doctype in theme config if necessary,
1061 // see theme_config->doctype in lib/outputlib.php for more details.
1062 unset_config('xmlstrictheaders');
b7db7803 1063 upgrade_main_savepoint(true, 2012072400.00);
13725b37 1064 }
a80b5a0c 1065
61e93a4c 1066 if ($oldversion < 2012072401.00) {
67e3dd2c
FM
1067
1068 // Saves orphaned questions from the Dark Side
1069 upgrade_save_orphaned_questions();
1070
1071 // Main savepoint reached
61e93a4c 1072 upgrade_main_savepoint(true, 2012072401.00);
67e3dd2c 1073 }
a80b5a0c 1074
3713514b 1075 if ($oldversion < 2012072600.01) {
f14f9842
AA
1076 // Handle events with empty eventtype //MDL-32827
1077
1078 $DB->set_field('event', 'eventtype', 'site', array('eventtype' => '', 'courseid' => $SITE->id));
1079 $DB->set_field_select('event', 'eventtype', 'due', "eventtype = '' AND courseid != 0 AND groupid = 0 AND (modulename = 'assignment' OR modulename = 'assign')");
1080 $DB->set_field_select('event', 'eventtype', 'course', "eventtype = '' AND courseid != 0 AND groupid = 0");
1081 $DB->set_field_select('event', 'eventtype', 'group', "eventtype = '' AND groupid != 0");
1082 $DB->set_field_select('event', 'eventtype', 'user', "eventtype = '' AND userid != 0");
1083
3713514b
AA
1084 // Main savepoint reached
1085 upgrade_main_savepoint(true, 2012072600.01);
1086 }
237f05cf
PS
1087
1088 if ($oldversion < 2012080200.02) {
1089 // Drop obsolete question upgrade field that should have been added to the install.xml.
1090 $table = new xmldb_table('question');
1091 $field = new xmldb_field('oldquestiontextformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
1092
1093 if ($dbman->field_exists($table, $field)) {
1094 $dbman->drop_field($table, $field);
1095 }
1096
1097 upgrade_main_savepoint(true, 2012080200.02);
1098 }
237f05cf 1099
76b97631 1100 if ($oldversion < 2012081400.01) {
850d2db8
DP
1101 // Move the ability to disable blogs to its own setting MDL-25012.
1102
1103 if (isset($CFG->bloglevel)) {
1104 // Only change settings if existing setting was set.
1105 if (empty($CFG->bloglevel)) {
1106 set_config('enableblogs', 0);
1107 // Now set the bloglevel to a valid setting as the disabled setting has been removed.
1108 // This prevents confusing results when users enable the blog system in future.
1109 set_config('bloglevel', BLOG_USER_LEVEL);
1110 } else {
1111 set_config('enableblogs', 1);
1112 }
1113 }
1114
1115 // Main savepoint reached
76b97631 1116 upgrade_main_savepoint(true, 2012081400.01);
850d2db8
DP
1117 }
1118
704533fc
PS
1119 if ($oldversion < 2012081600.01) {
1120 // Delete removed setting - Google Maps API V2 will not work in 2013.
1121 unset_config('googlemapkey');
1122 upgrade_main_savepoint(true, 2012081600.01);
1123 }
1124
882fb835
PS
1125 if ($oldversion < 2012082300.01) {
1126 // Add more custom enrol fields.
1127 $table = new xmldb_table('enrol');
882fb835 1128 $field = new xmldb_field('customint5', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint4');
59b9a140 1129
882fb835
PS
1130 if (!$dbman->field_exists($table, $field)) {
1131 $dbman->add_field($table, $field);
1132 }
1133
1134 $field = new xmldb_field('customint6', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint5');
1135 if (!$dbman->field_exists($table, $field)) {
1136 $dbman->add_field($table, $field);
1137 }
1138
1139 $field = new xmldb_field('customint7', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint6');
1140 if (!$dbman->field_exists($table, $field)) {
1141 $dbman->add_field($table, $field);
1142 }
1143
1144 $field = new xmldb_field('customint8', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'customint7');
1145 if (!$dbman->field_exists($table, $field)) {
1146 $dbman->add_field($table, $field);
1147 }
1148
1149 $field = new xmldb_field('customchar3', XMLDB_TYPE_CHAR, '1333', null, null, null, null, 'customchar2');
1150 if (!$dbman->field_exists($table, $field)) {
1151 $dbman->add_field($table, $field);
1152 }
1153
1154 $field = new xmldb_field('customtext3', XMLDB_TYPE_TEXT, null, null, null, null, null, 'customtext2');
1155 if (!$dbman->field_exists($table, $field)) {
1156 $dbman->add_field($table, $field);
1157 }
1158
1159 $field = new xmldb_field('customtext4', XMLDB_TYPE_TEXT, null, null, null, null, null, 'customtext3');
1160 if (!$dbman->field_exists($table, $field)) {
1161 $dbman->add_field($table, $field);
1162 }
1163
1164 // Main savepoint reached.
1165 upgrade_main_savepoint(true, 2012082300.01);
1166 }
1167
238f7761 1168 if ($oldversion < 2012082300.02) {
1d1917ae 1169 // Define field component to be added to groups_members
1170 $table = new xmldb_table('groups_members');
1171 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'timeadded');
1172
1173 // Conditionally launch add field component
1174 if (!$dbman->field_exists($table, $field)) {
1175 $dbman->add_field($table, $field);
1176 }
1177
1178 // Define field itemid to be added to groups_members
1179 $field = new xmldb_field('itemid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'component');
1180
1181 // Conditionally launch add field itemid
1182 if (!$dbman->field_exists($table, $field)) {
1183 $dbman->add_field($table, $field);
1184 }
1185
1186 // Main savepoint reached
238f7761 1187 upgrade_main_savepoint(true, 2012082300.02);
1d1917ae 1188 }
704533fc 1189
937b0acb
AB
1190 if ($oldversion < 2012090500.00) {
1191 $subquery = 'SELECT b.id FROM {blog_external} b where b.id = ' . $DB->sql_cast_char2int('{post}.content', true);
f3616783
AD
1192 $sql = 'DELETE FROM {post}
1193 WHERE {post}.module = \'blog_external\'
1194 AND NOT EXISTS (' . $subquery . ')
1195 AND ' . $DB->sql_isnotempty('post', 'uniquehash', false, false);
1196 $DB->execute($sql);
937b0acb 1197 upgrade_main_savepoint(true, 2012090500.00);
f3616783 1198 }
704533fc 1199
59b9a140
FM
1200 if ($oldversion < 2012090700.01) {
1201 // Add a category field in the course_request table
1202 $table = new xmldb_table('course_request');
1203 $field = new xmldb_field('category', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, 'summaryformat');
1204 if (!$dbman->field_exists($table, $field)) {
1205 $dbman->add_field($table, $field);
1206 }
1207
1208 // Main savepoint reached.
1209 upgrade_main_savepoint(true, 2012090700.01);
1210 }
1211
d11f12f6
DM
1212 if ($oldversion < 2012091700.00) {
1213
1214 // Dropping screenreader field from user.
1215 $table = new xmldb_table('user');
1216 $field = new xmldb_field('screenreader');
1217
1218 if ($dbman->field_exists($table, $field)) {
1219 $dbman->drop_field($table, $field);
1220 }
1221
1222 // Main savepoint reached.
1223 upgrade_main_savepoint(true, 2012091700.00);
1224 }
1225
52ebfade 1226 if ($oldversion < 2012092100.01) {
0f764c02
FM
1227 // Some folders still have a sortorder set, which is used for main files but is not
1228 // supported by the folder resource. We reset the value here.
1229 $sql = 'UPDATE {files} SET sortorder = ? WHERE component = ? AND filearea = ? AND sortorder <> ?';
1230 $DB->execute($sql, array(0, 'mod_folder', 'content', 0));
1231
1232 // Main savepoint reached.
52ebfade 1233 upgrade_main_savepoint(true, 2012092100.01);
0f764c02
FM
1234 }
1235
bb14cc2d 1236 if ($oldversion < 2012092600.00) {
7dd764b8
JG
1237 // Define index idname (unique) to be added to tag
1238 $table = new xmldb_table('tag');
1239 $index = new xmldb_index('idname', XMLDB_INDEX_UNIQUE, array('id', 'name'));
1240
1241 // Conditionally launch add index idname
1242 if (!$dbman->index_exists($table, $index)) {
1243 $dbman->add_index($table, $index);
1244 }
1245
1246 // Main savepoint reached
bb14cc2d 1247 upgrade_main_savepoint(true, 2012092600.00);
7dd764b8
JG
1248 }
1249
cb6b5fb6 1250 if ($oldversion < 2012101500.01) {
c0a05926
AG
1251 // Find all orphaned blog associations that might exist.
1252 $sql = "SELECT ba.id
1253 FROM {blog_association} ba
1254 LEFT JOIN {post} p
1255 ON p.id = ba.blogid
1256 WHERE p.id IS NULL";
1257 $orphanedrecordids = $DB->get_records_sql($sql);
1258 // Now delete these associations.
1259 foreach ($orphanedrecordids as $orphanedrecord) {
1260 $DB->delete_records('blog_association', array('id' => $orphanedrecord->id));
1261 }
1262
cb6b5fb6 1263 upgrade_main_savepoint(true, 2012101500.01);
c0a05926
AG
1264 }
1265
c28717d9 1266 if ($oldversion < 2012101800.02) {
d8271e39
FM
1267 // Renaming backups using previous file naming convention.
1268 upgrade_rename_old_backup_files_using_shortname();
1269
1270 // Main savepoint reached.
c28717d9 1271 upgrade_main_savepoint(true, 2012101800.02);
d8271e39
FM
1272 }
1273
0c918a1f 1274 if ($oldversion < 2012103001.00) {
b5a52acd
JH
1275 // create new event_subscriptions table
1276 $table = new xmldb_table('event_subscriptions');
e30390a0 1277 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
b5a52acd
JH
1278 $table->add_field('url', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
1279 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1280 $table->add_field('groupid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1281 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1282 $table->add_field('pollinterval', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1283 $table->add_field('lastupdated', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
e30390a0 1284 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
b5a52acd
JH
1285 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1286 if (!$dbman->table_exists($table)) {
1287 $dbman->create_table($table);
1288 }
1289 // Main savepoint reached
0c918a1f 1290 upgrade_main_savepoint(true, 2012103001.00);
b5a52acd
JH
1291 }
1292
0c918a1f 1293 if ($oldversion < 2012103002.00) {
b5a52acd
JH
1294 // Add subscription field to the event table
1295 $table = new xmldb_table('event');
e30390a0
SH
1296 $field = new xmldb_field('subscriptionid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'timemodified');
1297
1298 // Conditionally launch add field subscriptionid
b5a52acd
JH
1299 if (!$dbman->field_exists($table, $field)) {
1300 $dbman->add_field($table, $field);
1301 }
d618dcf3
DP
1302 upgrade_main_savepoint(true, 2012103002.00);
1303 }
b5a52acd 1304
d618dcf3 1305 if ($oldversion < 2012103003.00) {
e696fe6a 1306 // Fix uuid field in event table to match RFC-2445 UID property.
d618dcf3 1307 $table = new xmldb_table('event');
e30390a0 1308 $field = new xmldb_field('uuid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'visible');
e696fe6a
PS
1309 // The column already exists, so make sure there are no nulls (crazy mysql).
1310 $DB->set_field_select('event', 'uuid', '', "uuid IS NULL");
1311 // Changing precision of field uuid on table event to (255).
1312 $dbman->change_field_precision($table, $field);
b5a52acd 1313 // Main savepoint reached
d618dcf3 1314 upgrade_main_savepoint(true, 2012103003.00);
b5a52acd
JH
1315 }
1316
29ac9fc1
MG
1317 if ($oldversion < 2012110200.00) {
1318
1319 // Define table course_format_options to be created
1320 $table = new xmldb_table('course_format_options');
1321
1322 // Adding fields to table course_format_options
1323 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1324 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1325 $table->add_field('format', XMLDB_TYPE_CHAR, '21', null, XMLDB_NOTNULL, null, null);
1326 $table->add_field('sectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'format');
1327 $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
1328 $table->add_field('value', XMLDB_TYPE_TEXT, null, null, null, null, null);
1329
1330 // Adding keys to table course_format_options
1331 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1332 $table->add_key('courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1333
1334 // Adding indexes to table course_format_options
1335 $table->add_index('formatoption', XMLDB_INDEX_UNIQUE, array('courseid', 'format', 'sectionid', 'name'));
1336
1337 // Conditionally launch create table for course_format_options
1338 if (!$dbman->table_exists($table)) {
1339 $dbman->create_table($table);
1340 }
1341
1342 // Changing type of field format on table course to char with length 21
1343 $table = new xmldb_table('course');
1344 $field = new xmldb_field('format', XMLDB_TYPE_CHAR, '21', null, XMLDB_NOTNULL, null, 'topics', 'summaryformat');
1345
1346 // Launch change of type for field format
1347 $dbman->change_field_type($table, $field);
1348
e2049782
MG
1349 // Since structure of 'course' table has changed we need to re-read $SITE from DB.
1350 $SITE = $DB->get_record('course', array('id' => $SITE->id));
1351 $COURSE = clone($SITE);
1352
29ac9fc1
MG
1353 // Main savepoint reached
1354 upgrade_main_savepoint(true, 2012110200.00);
1355 }
1356
b5cf83f0
MG
1357 if ($oldversion < 2012110201.00) {
1358
1359 // Copy fields 'coursedisplay', 'numsections', 'hiddensections' from table {course}
1360 // to table {course_format_options} as the additional format options
1361 $fields = array();
1362 $table = new xmldb_table('course');
1363 foreach (array('coursedisplay', 'numsections', 'hiddensections') as $fieldname) {
1364 // first check that fields still exist
1365 $field = new xmldb_field($fieldname);
1366 if ($dbman->field_exists($table, $field)) {
1367 $fields[] = $fieldname;
1368 }
1369 }
1370
1371 if (!empty($fields)) {
1372 $transaction = $DB->start_delegated_transaction();
1373 $rs = $DB->get_recordset_sql('SELECT id, format, '. join(',', $fields).'
1374 FROM {course}
1375 WHERE format <> ? AND format <> ?',
1376 array('scorm', 'social'));
1377 // (do not copy fields from scrom and social formats, we already know that they are not used)
1378 foreach ($rs as $rec) {
1379 foreach ($fields as $field) {
1380 try {
1381 $DB->insert_record('course_format_options',
1382 array(
1383 'courseid' => $rec->id,
1384 'format' => $rec->format,
1385 'sectionid' => 0,
1386 'name' => $field,
1387 'value' => $rec->$field
1388 ));
1389 } catch (dml_exception $e) {
1390 // index 'courseid,format,sectionid,name' violation
1391 // continue; the entry in course_format_options already exists, use it
1392 }
1393 }
1394 }
1395 $rs->close();
1396 $transaction->allow_commit();
1397
1398 // Drop fields from table course
1399 foreach ($fields as $fieldname) {
1400 $field = new xmldb_field($fieldname);
1401 $dbman->drop_field($table, $field);
1402 }
1403 }
1404
e2049782
MG
1405 // Since structure of 'course' table has changed we need to re-read $SITE from DB.
1406 $SITE = $DB->get_record('course', array('id' => $SITE->id));
1407 $COURSE = clone($SITE);
1408
b5cf83f0
MG
1409 // Main savepoint reached
1410 upgrade_main_savepoint(true, 2012110201.00);
1411 }
1412
37743241
MN
1413 if ($oldversion < 2012110700.01) {
1414
1415 // Define field caller_component to be added to portfolio_log.
1416 $table = new xmldb_table('portfolio_log');
1417 $field = new xmldb_field('caller_component', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'caller_file');
1418
1419 // Conditionally launch add field caller_component.
1420 if (!$dbman->field_exists($table, $field)) {
1421 $dbman->add_field($table, $field);
1422 }
1423
1424 // Main savepoint reached.
1425 upgrade_main_savepoint(true, 2012110700.01);
1426 }
1427
7d270556
AB
1428 if ($oldversion < 2012111200.00) {
1429
1430 // Define table temp_enroled_template to be created
1431 $table = new xmldb_table('temp_enroled_template');
1432
1433 // Adding fields to table temp_enroled_template
1434 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1435 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1436 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1437 $table->add_field('roleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
1438
1439 // Adding keys to table temp_enroled_template
1440 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1441
1442 // Adding indexes to table temp_enroled_template
1443 $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid'));
1444 $table->add_index('courseid', XMLDB_INDEX_NOTUNIQUE, array('courseid'));
1445 $table->add_index('roleid', XMLDB_INDEX_NOTUNIQUE, array('roleid'));
1446
1447 // Conditionally launch create table for temp_enroled_template
1448 if (!$dbman->table_exists($table)) {
1449 $dbman->create_table($table);
1450 }
1451
1452 // Define table temp_log_template to be created
1453 $table = new xmldb_table('temp_log_template');
1454
1455 // Adding fields to table temp_log_template
1456 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
1457 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1458 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
1459 $table->add_field('action', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
1460
1461 // Adding keys to table temp_log_template
1462 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1463
1464 // Adding indexes to table temp_log_template
1465 $table->add_index('action', XMLDB_INDEX_NOTUNIQUE, array('action'));
1466 $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
1467 $table->add_index('user', XMLDB_INDEX_NOTUNIQUE, array('userid'));
1468 $table->add_index('usercourseaction', XMLDB_INDEX_NOTUNIQUE, array('userid', 'course', 'action'));
1469
1470 // Conditionally launch create table for temp_log_template
1471 if (!$dbman->table_exists($table)) {
1472 $dbman->create_table($table);
1473 }
1474
1475 // Main savepoint reached
1476 upgrade_main_savepoint(true, 2012111200.00);
1477 }
1478
9b835f70
FM
1479 if ($oldversion < 2012111200.01) {
1480 // Force the rebuild of the cache of every courses, some cached information could contain wrong icon references.
4202471a 1481 $DB->execute('UPDATE {course} set modinfo = ?, sectioncache = ?', array(null, null));
9b835f70
FM
1482
1483 // Main savepoint reached.
1484 upgrade_main_savepoint(true, 2012111200.01);
1485 }
1486
0b37fe46
PS
1487 if ($oldversion < 2012111601.01) {
1488 // Clea up after old shared memory caching support.
1489 unset_config('cachetype');
1490 unset_config('rcache');
1491 unset_config('rcachettl');
1492 unset_config('intcachemax');
1493 unset_config('memcachedhosts');
1494 unset_config('memcachedpconn');
1495
1496 // Main savepoint reached.
1497 upgrade_main_savepoint(true, 2012111601.01);
1498 }
1499
9cae08da
AB
1500 if ($oldversion < 2012112100.00) {
1501
1502 // Define field eventtype to be added to event_subscriptions.
1503 $table = new xmldb_table('event_subscriptions');
1504 $field = new xmldb_field('eventtype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, 'userid');
1505
1506 // Conditionally launch add field eventtype.
1507 if (!$dbman->field_exists($table, $field)) {
1508 $dbman->add_field($table, $field);
1509 }
1510
1511 // Main savepoint reached.
1512 upgrade_main_savepoint(true, 2012112100.00);
1513 }
7d270556 1514
6b9dfe73
EL
1515 // Moodle v2.4.0 release upgrade line
1516 // Put any upgrade step following this
1517
e28d5db2
MG
1518 if ($oldversion < 2012120300.01) {
1519 // Make sure site-course has format='site' //MDL-36840
1520
1521 if ($SITE->format !== 'site') {
1522 $DB->set_field('course', 'format', 'site', array('id' => $SITE->id));
1523 $SITE->format = 'site';
e2049782 1524 $COURSE->format = 'site';
e28d5db2
MG
1525 }
1526
1527 // Main savepoint reached
1528 upgrade_main_savepoint(true, 2012120300.01);
1529 }
1530
dbfe4150
PS
1531 if ($oldversion < 2012120300.04) {
1532 // Remove "_utf8" suffix from all langs in course table.
1533 $langs = $DB->get_records_sql("SELECT DISTINCT lang FROM {course} WHERE lang LIKE ?", array('%_utf8'));
1534
1535 foreach ($langs as $lang=>$unused) {
1536 $newlang = str_replace('_utf8', '', $lang);
1537 $sql = "UPDATE {course} SET lang = :newlang WHERE lang = :lang";
1538 $DB->execute($sql, array('newlang'=>$newlang, 'lang'=>$lang));
1539 }
1540
1541 // Main savepoint reached.
1542 upgrade_main_savepoint(true, 2012120300.04);
1543 }
1544
21df0539 1545 if ($oldversion < 2012123000.00) {
0662bd67
PS
1546 // Purge removed module filters and all their settings.
1547
1548 $tables = array('filter_active', 'filter_config');
1549 foreach ($tables as $table) {
1550 $DB->delete_records_select($table, "filter LIKE 'mod/%'");
1551 $filters = $DB->get_records_sql("SELECT DISTINCT filter FROM {{$table}} WHERE filter LIKE 'filter/%'");
1552 foreach ($filters as $filter) {
1553 $DB->set_field($table, 'filter', substr($filter->filter, 7), array('filter'=>$filter->filter));
1554 }
1555 }
1556
1557 $configs = array('stringfilters', 'filterall');
1558 foreach ($configs as $config) {
1559 if ($filters = get_config(null, $config)) {
1560 $filters = explode(',', $filters);
1561 $newfilters = array();
1562 foreach($filters as $filter) {
1563 if (strpos($filter, '/') === false) {
1564 $newfilters[] = $filter;
1565 } else if (strpos($filter, 'filter/') === 0) {
1566 $newfilters[] = substr($filter, 7);
1567 }
1568 }
1569 $filters = implode(',', $newfilters);
1570 set_config($config, $filters);
1571 }
1572 }
1573
1574 unset($tables);
1575 unset($table);
1576 unset($configs);
1577 unset($newfilters);
1578 unset($filters);
1579 unset($filter);
1580
1581 // Main savepoint reached.
21df0539 1582 upgrade_main_savepoint(true, 2012123000.00);
0662bd67
PS
1583 }
1584
1e7db9fe 1585 if ($oldversion < 2013021100.01) {
ffda0ca3
PS
1586 // Make sure there are no bogus nulls in old MySQL tables.
1587 $DB->set_field_select('user', 'password', '', "password IS NULL");
ec2d8ceb
SC
1588
1589 // Changing precision of field password on table user to (255).
1590 $table = new xmldb_table('user');
1591 $field = new xmldb_field('password', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'username');
1592
1593 // Launch change of precision for field password.
1594 $dbman->change_field_precision($table, $field);
1595
1596 // Main savepoint reached.
1e7db9fe 1597 upgrade_main_savepoint(true, 2013021100.01);
ec2d8ceb 1598 }
0662bd67 1599
e0d9b7c0
SH
1600 if ($oldversion < 2013021800.00) {
1601 // Add the site identifier to the cache config's file.
1602 $siteidentifier = $DB->get_field('config', 'value', array('name' => 'siteidentifier'));
1603 cache_helper::update_site_identifier($siteidentifier);
1604
1605 // Main savepoint reached.
1606 upgrade_main_savepoint(true, 2013021800.00);
1607 }
1608
28d72ad9 1609 if ($oldversion < 2013021801.00) {
58b1904f
FM
1610 // Fixing possible wrong MIME types for SMART Notebook files.
1611 $extensions = array('%.gallery', '%.galleryitem', '%.gallerycollection', '%.nbk', '%.notebook', '%.xbk');
1612 $select = $DB->sql_like('filename', '?', false);
1613 foreach ($extensions as $extension) {
1614 $DB->set_field_select(
1615 'files',
1616 'mimetype',
1617 'application/x-smarttech-notebook',
1618 $select,
1619 array($extension)
1620 );
1621 }
219faacc 1622 upgrade_main_savepoint(true, 2013021801.00);
58b1904f 1623 }
e0d9b7c0 1624
82a63328 1625 if ($oldversion < 2013021801.01) {
47644f7d
MG
1626 // This upgrade step is re-written under MDL-38228 (see below).
1627 /*
59a0ba3b
ARN
1628 // Retrieve the list of course_sections as a recordset to save memory
1629 $coursesections = $DB->get_recordset('course_sections', null, 'course, id', 'id, course, sequence');
1630 foreach ($coursesections as $coursesection) {
1631 // Retrieve all of the actual modules in this course and section combination to reduce DB calls
1632 $actualsectionmodules = $DB->get_records('course_modules',
1633 array('course' => $coursesection->course, 'section' => $coursesection->id), '', 'id, section');
1634
1635 // Break out the current sequence so that we can compare it
1636 $currentsequence = explode(',', $coursesection->sequence);
1637 $newsequence = array();
1638
1639 // Check each of the modules in the current sequence
1640 foreach ($currentsequence as $module) {
1641 if (isset($actualsectionmodules[$module])) {
1642 $newsequence[] = $module;
1643 // We unset the actualsectionmodules so that we don't get duplicates and that we can add orphaned
1644 // modules later
1645 unset($actualsectionmodules[$module]);
1646 }
1647 }
1648
1649 // Append any modules which have somehow been orphaned
1650 foreach ($actualsectionmodules as $module) {
1651 $newsequence[] = $module->id;
1652 }
1653
1654 // Piece it all back together
1655 $sequence = implode(',', $newsequence);
1656
1657 // Only update if there have been changes
1658 if ($sequence !== $coursesection->sequence) {
1659 $coursesection->sequence = $sequence;
1660 $DB->update_record('course_sections', $coursesection);
1661
1662 // And clear the sectioncache and modinfo cache - they'll be regenerated on next use
1663 $course = new stdClass();
1664 $course->id = $coursesection->course;
1665 $course->sectioncache = null;
1666 $course->modinfo = null;
1667 $DB->update_record('course', $course);
1668 }
1669 }
1670 $coursesections->close();
47644f7d 1671 */
59a0ba3b 1672 // Main savepoint reached.
82a63328 1673 upgrade_main_savepoint(true, 2013021801.01);
59a0ba3b
ARN
1674 }
1675
345768b9 1676 if ($oldversion < 2013021902.00) {
47570cc6
DP
1677 // ISO country change: Netherlands Antilles is split into BQ, CW & SX
1678 // http://www.iso.org/iso/iso_3166-1_newsletter_vi-8_split_of_the_dutch_antilles_final-en.pdf
1679 $sql = "UPDATE {user} SET country = '' WHERE country = ?";
1680 $DB->execute($sql, array('AN'));
1681
345768b9 1682 upgrade_main_savepoint(true, 2013021902.00);
47570cc6
DP
1683 }
1684
be1ef2f3
DW
1685 if ($oldversion < 2013022600.00) {
1686 // Delete entries regarding invalid 'interests' option which breaks course.
1687 $DB->delete_records('course_sections_avail_fields', array('userfield' => 'interests'));
1688 $DB->delete_records('course_modules_avail_fields', array('userfield' => 'interests'));
6fea7a5f 1689 // Clear course cache (will be rebuilt on first visit) in case of changes to these.
4202471a 1690 $DB->execute('UPDATE {course} set modinfo = ?, sectioncache = ?', array(null, null));
6fea7a5f 1691
be1ef2f3 1692 upgrade_main_savepoint(true, 2013022600.00);
6fea7a5f 1693 }
1694
e1580085 1695 // Add index to field "timemodified" for grade_grades_history table.
4ce76171 1696 if ($oldversion < 2013030400.00) {
e1580085
TG
1697 $table = new xmldb_table('grade_grades_history');
1698 $field = new xmldb_field('timemodified');
1699
1700 if ($dbman->field_exists($table, $field)) {
1701 $index = new xmldb_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
1702 if (!$dbman->index_exists($table, $index)) {
1703 $dbman->add_index($table, $index);
1704 }
1705 }
1706
1707 // Main savepoint reached.
4ce76171 1708 upgrade_main_savepoint(true, 2013030400.00);
e1580085
TG
1709 }
1710
adc4aca4 1711 if ($oldversion < 2013030400.02) {
4f315786
JMV
1712 // Cleanup qformat blackboard settings.
1713 unset_all_config_for_plugin('qformat_blackboard');
1714
adc4aca4 1715 upgrade_main_savepoint(true, 2013030400.02);
4f315786
JMV
1716 }
1717
005c6640
DW
1718 // This is checking to see if the site has been running a specific version with a bug in it
1719 // because this upgrade step is slow and is only needed if the site has been running with the affected versions.
8673a98d 1720 if ($oldversion >= 2012062504.08 && $oldversion < 2012062504.13) {
47644f7d
MG
1721 // This upgrade step is re-written under MDL-38228 (see below).
1722
1723 /*
005c6640
DW
1724 // Retrieve the list of course_sections as a recordset to save memory.
1725 // This is to fix a regression caused by MDL-37939.
1726 // In this case the upgrade step is fixing records where:
1727 // The data in course_sections.sequence contains the correct module id
1728 // The section field for on the course modules table may have been updated to point to the incorrect id.
1729
1730 // This query is looking for sections where the sequence is not in sync with the course_modules table.
1731 // The syntax for the like query is looking for a value in a comma separated list.
1732 // It adds a comma to either site of the list and then searches for LIKE '%,id,%'.
1733 $sequenceconcat = $DB->sql_concat("','", 's.sequence', "','");
1734 $moduleconcat = $DB->sql_concat("'%,'", 'cm.id', "',%'");
1735 $sql = 'SELECT s2.id, s2.course, s2.sequence
8673a98d 1736 FROM {course_sections} s2
005c6640
DW
1737 JOIN(
1738 SELECT DISTINCT s.id
1739 FROM
1740 {course_modules} cm
1741 JOIN {course_sections} s
1742 ON
1743 cm.course = s.course
1744 WHERE cm.section != s.id AND ' . $sequenceconcat . ' LIKE ' . $moduleconcat . '
1745 ) d
1746 ON s2.id = d.id';
1747 $coursesections = $DB->get_recordset_sql($sql);
1748
1749 foreach ($coursesections as $coursesection) {
1750 // Retrieve all of the actual modules in this course and section combination to reduce DB calls.
1751 $actualsectionmodules = $DB->get_records('course_modules',
1752 array('course' => $coursesection->course, 'section' => $coursesection->id), '', 'id, section');
1753
1754 // Break out the current sequence so that we can compare it.
1755 $currentsequence = explode(',', $coursesection->sequence);
1756 $orphanlist = array();
1757
1758 // Check each of the modules in the current sequence.
1759 foreach ($currentsequence as $cmid) {
1760 if (!empty($cmid) && !isset($actualsectionmodules[$cmid])) {
1761 $orphanlist[] = $cmid;
1762 }
1763 }
1764
1765 if (!empty($orphanlist)) {
1766 list($sql, $params) = $DB->get_in_or_equal($orphanlist, SQL_PARAMS_NAMED);
1767 $sql = "id $sql";
1768
1769 $DB->set_field_select('course_modules', 'section', $coursesection->id, $sql, $params);
1770
1771 // And clear the sectioncache and modinfo cache - they'll be regenerated on next use.
1772 $course = new stdClass();
1773 $course->id = $coursesection->course;
1774 $course->sectioncache = null;
1775 $course->modinfo = null;
1776 $DB->update_record('course', $course);
1777 }
1778 }
1779 $coursesections->close();
1780
1781 // No savepoint needed for this change.
47644f7d 1782 */
005c6640
DW
1783 }
1784
689096bc
PS
1785 if ($oldversion < 2013032200.01) {
1786 // GD is now always available
c7289da7 1787 set_config('gdversion', 2);
689096bc
PS
1788
1789 upgrade_main_savepoint(true, 2013032200.01);
1790 }
1791
2013daeb 1792 if ($oldversion < 2013032600.03) {
2aecb84d
MS
1793 // Fixing possible wrong MIME type for MIME HTML (MHTML) files.
1794 $extensions = array('%.mht', '%.mhtml');
1795 $select = $DB->sql_like('filename', '?', false);
1796 foreach ($extensions as $extension) {
1797 $DB->set_field_select(
1798 'files',
1799 'mimetype',
1800 'message/rfc822',
1801 $select,
1802 array($extension)
1803 );
1804 }
2013daeb 1805 upgrade_main_savepoint(true, 2013032600.03);
2aecb84d
MS
1806 }
1807
cf140de9 1808 if ($oldversion < 2013032600.04) {
0559a72a
TH
1809 // MDL-31983 broke the quiz version number. Fix it.
1810 $DB->set_field('modules', 'version', '2013021500',
1811 array('name' => 'quiz', 'version' => '2013310100'));
cf140de9 1812 upgrade_main_savepoint(true, 2013032600.04);
0559a72a
TH
1813 }
1814
27806552
YB
1815 if ($oldversion < 2013040200.00) {
1816 // Add openbadges tables.
1817
1818 // Define table 'badge' to be created.
1819 $table = new xmldb_table('badge');
1820
1821 // Adding fields to table 'badge'.
1822 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1823 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'id');
1824 $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null, 'name');
1825 $table->add_field('image', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'description');
1826 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'image');
1827 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'timecreated');
1828 $table->add_field('usercreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'timemodified');
1829 $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'usercreated');
1830 $table->add_field('issuername', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'usermodified');
1831 $table->add_field('issuerurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'issuername');
1832 $table->add_field('issuercontact', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'issuerurl');
1833 $table->add_field('expiredate', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'issuercontact');
1834 $table->add_field('expireperiod', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'expiredate');
1835 $table->add_field('type', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'expireperiod');
1836 $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'type');
1837 $table->add_field('message', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'courseid');
1838 $table->add_field('messagesubject', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'message');
1839 $table->add_field('attachment', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'messagesubject');
1840 $table->add_field('notification', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'attachment');
1841 $table->add_field('status', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'notification');
1842 $table->add_field('nextcron', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'status');
1843
1844 // Adding keys to table 'badge'.
1845 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1846 $table->add_key('fk_courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id'));
1847 $table->add_key('fk_usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id'));
1848 $table->add_key('fk_usercreated', XMLDB_KEY_FOREIGN, array('usercreated'), 'user', array('id'));
1849
1850 // Adding indexes to table 'badge'.
1851 $table->add_index('type', XMLDB_INDEX_NOTUNIQUE, array('type'));
1852
1853 // Conditionally launch create table for 'badge'.
1854 if (!$dbman->table_exists($table)) {
1855 $dbman->create_table($table);
1856 }
1857
1858 // Define table 'badge_criteria' to be created.
1859 $table = new xmldb_table('badge_criteria');
1860
1861 // Adding fields to table 'badge_criteria'.
1862 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1863 $table->add_field('badgeid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
1864 $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'badgeid');
1865 $table->add_field('method', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'criteriatype');
1866
1867 // Adding keys to table 'badge_criteria'.
1868 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1869 $table->add_key('fk_badgeid', XMLDB_KEY_FOREIGN, array('badgeid'), 'badge', array('id'));
1870
1871 // Adding indexes to table 'badge_criteria'.
27806552
YB
1872 $table->add_index('criteriatype', XMLDB_INDEX_NOTUNIQUE, array('criteriatype'));
1873 $table->add_index('badgecriteriatype', XMLDB_INDEX_UNIQUE, array('badgeid', 'criteriatype'));
1874
1875 // Conditionally launch create table for 'badge_criteria'.
1876 if (!$dbman->table_exists($table)) {
1877 $dbman->create_table($table);
1878 }
1879
1880 // Define table 'badge_criteria_param' to be created.
1881 $table = new xmldb_table('badge_criteria_param');
1882
1883 // Adding fields to table 'badge_criteria_param'.
1884 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1885 $table->add_field('critid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id');
1886 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'critid');
1887 $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'name');
1888
1889 // Adding keys to table 'badge_criteria_param'.
1890 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1891 $table->add_key('fk_critid', XMLDB_KEY_FOREIGN, array('critid'), 'badge_criteria', array('id'));
1892
27806552
YB
1893 // Conditionally launch create table for 'badge_criteria_param'.
1894 if (!$dbman->table_exists($table)) {
1895 $dbman->create_table($table);
1896 }
1897
1898 // Define table 'badge_issued' to be created.
1899 $table = new xmldb_table('badge_issued');
1900
1901 // Adding fields to table 'badge_issued'.
1902 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1903 $table->add_field('badgeid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
1904 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'badgeid');
1905 $table->add_field('uniquehash', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'userid');
1906 $table->add_field('dateissued', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'uniquehash');
1907 $table->add_field('dateexpire', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'dateissued');
1908 $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'dateexpire');
1909 $table->add_field('issuernotified', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'visible');
1910
1911 // Adding keys to table 'badge_issued'.
1912 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1913 $table->add_key('fk_badgeid', XMLDB_KEY_FOREIGN, array('badgeid'), 'badge', array('id'));
1914 $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1915
27806552
YB
1916 $table->add_index('badgeuser', XMLDB_INDEX_UNIQUE, array('badgeid', 'userid'));
1917
1918 // Conditionally launch create table for 'badge_issued'.
1919 if (!$dbman->table_exists($table)) {
1920 $dbman->create_table($table);
1921 }
1922
1923 // Define table 'badge_criteria_met' to be created.
1924 $table = new xmldb_table('badge_criteria_met');
1925
1926 // Adding fields to table 'badge_criteria_met'.
1927 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1928 $table->add_field('issuedid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'id');
1929 $table->add_field('critid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'issuedid');
1930 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'critid');
1931 $table->add_field('datemet', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'userid');
1932
1933 // Adding keys to table 'badge_criteria_met'
1934 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1935 $table->add_key('fk_critid', XMLDB_KEY_FOREIGN, array('critid'), 'badge_criteria', array('id'));
1936 $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1937 $table->add_key('fk_issuedid', XMLDB_KEY_FOREIGN, array('issuedid'), 'badge_issued', array('id'));
1938
1939 // Conditionally launch create table for 'badge_criteria_met'.
1940 if (!$dbman->table_exists($table)) {
1941 $dbman->create_table($table);
1942 }
1943
1944 // Define table 'badge_manual_award' to be created.
1945 $table = new xmldb_table('badge_manual_award');
1946
1947 // Adding fields to table 'badge_manual_award'.
1948 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1949 $table->add_field('badgeid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id');
1950 $table->add_field('recipientid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'badgeid');
1951 $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'recipientid');
1952 $table->add_field('issuerrole', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'issuerid');
1953 $table->add_field('datemet', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'issuerrole');
1954
1955 // Adding keys to table 'badge_manual_award'.
1956 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2916ca61 1957 $table->add_key('fk_badgeid', XMLDB_KEY_FOREIGN, array('badgeid'), 'badge', array('id'));
27806552
YB
1958 $table->add_key('fk_recipientid', XMLDB_KEY_FOREIGN, array('recipientid'), 'user', array('id'));
1959 $table->add_key('fk_issuerid', XMLDB_KEY_FOREIGN, array('issuerid'), 'user', array('id'));
1960 $table->add_key('fk_issuerrole', XMLDB_KEY_FOREIGN, array('issuerrole'), 'role', array('id'));
1961
1962 // Conditionally launch create table for 'badge_manual_award'.
1963 if (!$dbman->table_exists($table)) {
1964 $dbman->create_table($table);
1965 }
1966
1967 // Define table 'badge_backpack' to be created.
1968 $table = new xmldb_table('badge_backpack');
1969
1970 // Adding fields to table 'badge_backpack'.
1971 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
1972 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
1973 $table->add_field('email', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'userid');
1974 $table->add_field('backpackurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'email');
1975 $table->add_field('backpackuid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'backpackurl');
1976 $table->add_field('backpackgid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'backpackuid');
1977 $table->add_field('autosync', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'backpackgid');
1978 $table->add_field('password', XMLDB_TYPE_CHAR, '50', null, null, null, null, 'autosync');
1979
1980 // Adding keys to table 'badge_backpack'.
1981 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
1982 $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
1983
27806552
YB
1984 // Conditionally launch create table for 'badge_backpack'.
1985 if (!$dbman->table_exists($table)) {
1986 $dbman->create_table($table);
1987 }
1988
1989 // Main savepoint reached.
1990 upgrade_main_savepoint(true, 2013040200.00);
1991 }
1992
eee20b12 1993 if ($oldversion < 2013040201.00) {
e2e9ff65
RT
1994 // Convert name field in event table to text type as RFC-2445 doesn't have any limitation on it.
1995 $table = new xmldb_table('event');
1996 $field = new xmldb_field('name', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
1997 if ($dbman->field_exists($table, $field)) {
1998 $dbman->change_field_type($table, $field);
1999 }
2000 // Main savepoint reached.
eee20b12 2001 upgrade_main_savepoint(true, 2013040201.00);
e2e9ff65
RT
2002 }
2003
dcf63185 2004 if ($oldversion < 2013040300.01) {
8819a836
FM
2005
2006 // Define field completionstartonenrol to be dropped from course.
2007 $table = new xmldb_table('course');
2008 $field = new xmldb_field('completionstartonenrol');
2009
2010 // Conditionally launch drop field completionstartonenrol.
2011 if ($dbman->field_exists($table, $field)) {
2012 $dbman->drop_field($table, $field);
2013 }
2014
e2049782
MG
2015 // Since structure of 'course' table has changed we need to re-read $SITE from DB.
2016 $SITE = $DB->get_record('course', array('id' => $SITE->id));
2017 $COURSE = clone($SITE);
2018
8819a836 2019 // Main savepoint reached.
dcf63185 2020 upgrade_main_savepoint(true, 2013040300.01);
8819a836
FM
2021 }
2022
42f563f6 2023 if ($oldversion < 2013041200.00) {
63354ab5 2024 // MDL-29877 Some bad restores created grade items with no category information.
c7ee04bd 2025 $sql = "UPDATE {grade_items}
63354ab5
AD
2026 SET categoryid = courseid
2027 WHERE itemtype <> 'course' and itemtype <> 'category'
2028 AND categoryid IS NULL";
2029 $DB->execute($sql);
42f563f6 2030 upgrade_main_savepoint(true, 2013041200.00);
63354ab5
AD
2031 }
2032
0fd26350
MG
2033 if ($oldversion < 2013041600.00) {
2034 // Copy constants from /course/lib.php instead of including the whole library:
2035 $c = array( 'FRONTPAGENEWS' => 0,
2036 'FRONTPAGECOURSELIST' => 1,
2037 'FRONTPAGECATEGORYNAMES' => 2,
2038 'FRONTPAGETOPICONLY' => 3,
2039 'FRONTPAGECATEGORYCOMBO' => 4,
2040 'FRONTPAGEENROLLEDCOURSELIST' => 5,
2041 'FRONTPAGEALLCOURSELIST' => 6,
2042 'FRONTPAGECOURSESEARCH' => 7);
2043 // Update frontpage settings $CFG->frontpage and $CFG->frontpageloggedin. In 2.4 there was too much of hidden logic about them.
2044 // 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.
2045 $ncourses = $DB->count_records('course');
2046 foreach (array('frontpage', 'frontpageloggedin') as $configkey) {
2047 if ($frontpage = explode(',', $CFG->{$configkey})) {
2048 $newfrontpage = array();
2049 foreach ($frontpage as $v) {
2050 switch ($v) {
2051 case $c['FRONTPAGENEWS']:
2052 // Not related to course listings, leave as it is.
2053 $newfrontpage[] = $c['FRONTPAGENEWS'];
2054 break;
2055 case $c['FRONTPAGECOURSELIST']:
2056 if ($configkey === 'frontpageloggedin' && empty($CFG->disablemycourses)) {
2057 // In 2.4 unless prohibited in config, the "list of courses" was considered "list of enrolled courses" plus course search box.
2058 $newfrontpage[] = $c['FRONTPAGEENROLLEDCOURSELIST'];
2059 } else if ($ncourses <= 200) {
2060 // Still list of courses was only displayed in there were less than 200 courses in system. Otherwise - search box only.
2061 $newfrontpage[] = $c['FRONTPAGEALLCOURSELIST'];
2062 break; // skip adding search box
2063 }
2064 if (!in_array($c['FRONTPAGECOURSESEARCH'], $newfrontpage)) {
2065 $newfrontpage[] = $c['FRONTPAGECOURSESEARCH'];
2066 }
2067 break;
2068 case $c['FRONTPAGECATEGORYNAMES']:
2069 // In 2.4 search box was displayed automatically after categories list. In 2.5 it is displayed as a separate setting.
2070 $newfrontpage[] = $c['FRONTPAGECATEGORYNAMES'];
2071 if (!in_array($c['FRONTPAGECOURSESEARCH'], $newfrontpage)) {
2072 $newfrontpage[] = $c['FRONTPAGECOURSESEARCH'];
2073 }
2074 break;
2075 case $c['FRONTPAGECATEGORYCOMBO']:
2076 $maxcourses = empty($CFG->numcoursesincombo) ? 500 : $CFG->numcoursesincombo;
2077 // In 2.4 combo list was not displayed if there are more than $CFG->numcoursesincombo courses in the system.
2078 if ($ncourses < $maxcourses) {
2079 $newfrontpage[] = $c['FRONTPAGECATEGORYCOMBO'];
2080 }
2081 if (!in_array($c['FRONTPAGECOURSESEARCH'], $newfrontpage)) {
2082 $newfrontpage[] = $c['FRONTPAGECOURSESEARCH'];
2083 }
2084 break;
2085 }
2086 }
2087 set_config($configkey, join(',', $newfrontpage));
2088 }
2089 }
2090 // $CFG->numcoursesincombo no longer affects whether the combo list is displayed. Setting is deprecated.
2091 unset_config('numcoursesincombo');
2092
2093 upgrade_main_savepoint(true, 2013041600.00);
2094 }
2095
19226a30 2096 if ($oldversion < 2013041601.00) {
e2805314
YB
2097 // Create a new 'badge_external' table first.
2098 // Define table 'badge_external' to be created.
2099 $table = new xmldb_table('badge_external');
7e43a15f 2100
e2805314
YB
2101 // Adding fields to table 'badge_external'.
2102 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
2103 $table->add_field('backpackid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id');
2104 $table->add_field('collectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'backpackid');
7e43a15f 2105
e2805314
YB
2106 // Adding keys to table 'badge_external'.
2107 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2108 $table->add_key('fk_backpackid', XMLDB_KEY_FOREIGN, array('backpackid'), 'badge_backpack', array('id'));
7e43a15f 2109
e2805314
YB
2110 // Conditionally launch create table for 'badge_external'.
2111 if (!$dbman->table_exists($table)) {
2112 $dbman->create_table($table);
2113 }
7e43a15f 2114
e2805314
YB
2115 // Perform user data migration.
2116 $usercollections = $DB->get_records('badge_backpack');
2117 foreach ($usercollections as $usercollection) {
2118 $collection = new stdClass();
2119 $collection->backpackid = $usercollection->id;
2120 $collection->collectionid = $usercollection->backpackgid;
2121 $DB->insert_record('badge_external', $collection);
2122 }
7e43a15f 2123
e2805314
YB
2124 // Finally, drop the column.
2125 // Define field backpackgid to be dropped from 'badge_backpack'.
2126 $table = new xmldb_table('badge_backpack');
2127 $field = new xmldb_field('backpackgid');
7e43a15f 2128
e2805314
YB
2129 // Conditionally launch drop field backpackgid.
2130 if ($dbman->field_exists($table, $field)) {
2131 $dbman->drop_field($table, $field);
2132 }
7e43a15f 2133
e2805314 2134 // Main savepoint reached.
19226a30 2135 upgrade_main_savepoint(true, 2013041601.00);
e2805314
YB
2136 }
2137
359c1d47 2138 if ($oldversion < 2013041601.01) {
0c4c981f
PS
2139 // Changing the default of field descriptionformat on table user to 1.
2140 $table = new xmldb_table('user');
2141 $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '1', 'description');
2142
2143 // Launch change of default for field descriptionformat.
2144 $dbman->change_field_default($table, $field);
2145
2146 // Main savepoint reached.
359c1d47 2147 upgrade_main_savepoint(true, 2013041601.01);
0c4c981f
PS
2148 }
2149
fbf76dcd 2150 if ($oldversion < 2013041900.00) {
5cba0c4b 2151 require_once($CFG->dirroot . '/cache/locallib.php');
fbf76dcd
SH
2152 // The features bin needs updating.
2153 cache_config_writer::update_default_config_stores();
2154 // Main savepoint reached.
2155 upgrade_main_savepoint(true, 2013041900.00);
2156 }
2157
b0c4e474 2158 if ($oldversion < 2013042300.00) {
d750d41e
MC
2159 // Adding index to unreadmessageid field of message_working table (MDL-34933)
2160 $table = new xmldb_table('message_working');
2161 $index = new xmldb_index('unreadmessageid_idx', XMLDB_INDEX_NOTUNIQUE, array('unreadmessageid'));
2162
2163 // Conditionally launch add index unreadmessageid
2164 if (!$dbman->index_exists($table, $index)) {
2165 $dbman->add_index($table, $index);
2166 }
2167
2168 // Main savepoint reached.
b0c4e474 2169 upgrade_main_savepoint(true, 2013042300.00);
d750d41e
MC
2170 }
2171
4f5bcf2e
DP
2172 // Moodle v2.5.0 release upgrade line.
2173 // Put any upgrade step following this.
2174
82198dec
DP
2175 if ($oldversion < 2013051400.01) {
2176 // Fix incorrect cc-nc url. Unfortunately the license 'plugins' do
2177 // not give a mechanism to do this.
2178
2179 $sql = "UPDATE {license}
2180 SET source = :url, version = :newversion
2181 WHERE shortname = :shortname AND version = :oldversion";
2182
2183 $params = array(
2184 'url' => 'http://creativecommons.org/licenses/by-nc/3.0/',
2185 'shortname' => 'cc-nc',
2186 'newversion' => '2013051500',
2187 'oldversion' => '2010033100'
2188 );
2189
2190 $DB->execute($sql, $params);
2191
2192 // Main savepoint reached.
2193 upgrade_main_savepoint(true, 2013051400.01);
2194 }
2195
a48dad1f 2196 if ($oldversion < 2013061400.01) {
9feda85d
DP
2197 // Clean up old tokens which haven't been deleted.
2198 $DB->execute("DELETE FROM {user_private_key} WHERE NOT EXISTS
2199 (SELECT 'x' FROM {user} WHERE deleted = 0 AND id = userid)");
2200
2201 // Main savepoint reached.
a48dad1f 2202 upgrade_main_savepoint(true, 2013061400.01);
9feda85d
DP
2203 }
2204
96c90fab 2205 if ($oldversion < 2013061700.00) {
283e448a 2206 // MDL-40103: Remove unused template tables from the database.
672bee2d 2207 // These are now created inline with xmldb_table.
283e448a 2208
62d6f183
RS
2209 $tablestocleanup = array('temp_enroled_template','temp_log_template','backup_files_template','backup_ids_template');
2210 $dbman = $DB->get_manager();
283e448a 2211
62d6f183 2212 foreach ($tablestocleanup as $table) {
283e448a 2213 $xmltable = new xmldb_table($table);
672bee2d 2214 if ($dbman->table_exists($xmltable)) {
62d6f183
RS
2215 $dbman->drop_table($xmltable);
2216 }
283e448a
RS
2217 }
2218
2219 // Main savepoint reached.
96c90fab 2220 upgrade_main_savepoint(true, 2013061700.00);
283e448a 2221 }
96c90fab 2222
64830bf6 2223 if ($oldversion < 2013070800.00) {
4aa4d88d
FM
2224
2225 // Remove orphan repository instances.
0193dd1c
FM
2226 if ($DB->get_dbfamily() === 'mysql') {
2227 $sql = "DELETE {repository_instances} FROM {repository_instances}
2228 LEFT JOIN {context} ON {context}.id = {repository_instances}.contextid
2229 WHERE {context}.id IS NULL";
2230 } else {
2231 $sql = "DELETE FROM {repository_instances}
2232 WHERE NOT EXISTS (
2233 SELECT 'x' FROM {context}
2234 WHERE {context}.id = {repository_instances}.contextid)";
2235 }
2236 $DB->execute($sql);
4aa4d88d
FM
2237
2238 // Main savepoint reached.
64830bf6 2239 upgrade_main_savepoint(true, 2013070800.00);
4aa4d88d
FM
2240 }
2241
a327f25e
AG
2242 if ($oldversion < 2013070800.01) {
2243
2244 // Define field lastnamephonetic to be added to user.
2245 $table = new xmldb_table('user');
2246 $field = new xmldb_field('lastnamephonetic', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'imagealt');
2247 $index = new xmldb_index('lastnamephonetic', XMLDB_INDEX_NOTUNIQUE, array('lastnamephonetic'));
2248
2249 // Conditionally launch add field lastnamephonetic.
2250 if (!$dbman->field_exists($table, $field)) {
2251 $dbman->add_field($table, $field);
2252 $dbman->add_index($table, $index);
2253 }
2254
2255 // Define field firstnamephonetic to be added to user.
2256 $table = new xmldb_table('user');
2257 $field = new xmldb_field('firstnamephonetic', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'lastnamephonetic');
2258 $index = new xmldb_index('firstnamephonetic', XMLDB_INDEX_NOTUNIQUE, array('firstnamephonetic'));
2259
2260 // Conditionally launch add field firstnamephonetic.
2261 if (!$dbman->field_exists($table, $field)) {
2262 $dbman->add_field($table, $field);
2263 $dbman->add_index($table, $index);
2264 }
2265
2266 // Define field alternatename to be added to user.
2267 $table = new xmldb_table('user');
2268 $field = new xmldb_field('middlename', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'firstnamephonetic');
2269 $index = new xmldb_index('middlename', XMLDB_INDEX_NOTUNIQUE, array('middlename'));
2270
2271 // Conditionally launch add field firstnamephonetic.
2272 if (!$dbman->field_exists($table, $field)) {
2273 $dbman->add_field($table, $field);
2274 $dbman->add_index($table, $index);
2275 }
2276
2277 // Define field alternatename to be added to user.
2278 $table = new xmldb_table('user');
2279 $field = new xmldb_field('alternatename', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'middlename');
2280 $index = new xmldb_index('alternatename', XMLDB_INDEX_NOTUNIQUE, array('alternatename'));
2281
2282 // Conditionally launch add field alternatename.
2283 if (!$dbman->field_exists($table, $field)) {
2284 $dbman->add_field($table, $field);
2285 $dbman->add_index($table, $index);
2286 }
2287
2288 // Main savepoint reached.
2289 upgrade_main_savepoint(true, 2013070800.01);
2290 }
c2ca2d8e
DP
2291 if ($oldversion < 2013071500.01) {
2292 // The enrol_authorize plugin has been removed, if there are no records
2293 // and no plugin files then remove the plugin data.
2294 $enrolauthorize = new xmldb_table('enrol_authorize');
2295 $enrolauthorizerefunds = new xmldb_table('enrol_authorize_refunds');
2296
2297 if (!file_exists($CFG->dirroot.'/enrol/authorize/version.php') &&
2298 $dbman->table_exists($enrolauthorize) &&
2299 $dbman->table_exists($enrolauthorizerefunds)) {
2300
2301 $enrolauthorizecount = $DB->count_records('enrol_authorize');
2302 $enrolauthorizerefundcount = $DB->count_records('enrol_authorize_refunds');
2303
2304 if (empty($enrolauthorizecount) && empty($enrolauthorizerefundcount)) {
2305
2306 // Drop the database tables.
2307 $dbman->drop_table($enrolauthorize);
2308 $dbman->drop_table($enrolauthorizerefunds);
2309
2310 // Drop the message provider and associated data manually.
2311 $DB->delete_records('message_providers', array('component' => 'enrol_authorize'));
2312 $DB->delete_records_select('config_plugins', "plugin = 'message' AND ".$DB->sql_like('name', '?', false), array("%_provider_enrol_authorize_%"));
2313 $DB->delete_records_select('user_preferences', $DB->sql_like('name', '?', false), array("message_provider_enrol_authorize_%"));
2314
2315 // Remove capabilities.
2316 capabilities_cleanup('enrol_authorize');
2317
2318 // Remove all other associated config.
2319 unset_all_config_for_plugin('enrol_authorize');
2320 }
2321 }
2322 upgrade_main_savepoint(true, 2013071500.01);
2323 }
2324
e780e5cb 2325 if ($oldversion < 2013071500.02) {
e50220f8
YB
2326 // Define field attachment to be dropped from badge.
2327 $table = new xmldb_table('badge');
2328 $field = new xmldb_field('image');
2329
2330 // Conditionally launch drop field eventtype.
2331 if ($dbman->field_exists($table, $field)) {
2332 $dbman->drop_field($table, $field);
2333 }
2334
e780e5cb 2335 upgrade_main_savepoint(true, 2013071500.02);
e50220f8
YB
2336 }
2337
656250de
PS
2338 if ($oldversion < 2013072600.01) {
2339 upgrade_mssql_nvarcharmax();
2340 upgrade_mssql_varbinarymax();
2341
2342 upgrade_main_savepoint(true, 2013072600.01);
2343 }
2344
8b7378cf 2345 if ($oldversion < 2013081200.00) {
106c55fb
DW
2346 // Define field uploadfiles to be added to external_services.
2347 $table = new xmldb_table('external_services');
2348 $field = new xmldb_field('uploadfiles', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'downloadfiles');
2349
2350 // Conditionally launch add field uploadfiles.
2351 if (!$dbman->field_exists($table, $field)) {
2352 $dbman->add_field($table, $field);
2353 }
2354
2355 // Main savepoint reached.
8b7378cf 2356 upgrade_main_savepoint(true, 2013081200.00);
106c55fb
DW
2357 }
2358
6d8627cb
MN
2359 if ($oldversion < 2013082300.01) {
2360 // Define the table 'backup_logs' and the field 'message' which we will be changing from a char to a text field.
2361 $table = new xmldb_table('backup_logs');
2362 $field = new xmldb_field('message', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'loglevel');
2363
2364 // Perform the change.
2365 $dbman->change_field_type($table, $field);
2366
2367 // Main savepoint reached.
2368 upgrade_main_savepoint(true, 2013082300.01);
2369 }
2370
3d1808c6 2371 // Convert SCORM course format courses to singleactivity.
f6c2af86 2372 if ($oldversion < 2013082700.00) {
3d1808c6
DM
2373 // First set relevant singleactivity settings.
2374 $formatoptions = new stdClass();
2375 $formatoptions->format = 'singleactivity';
2376 $formatoptions->sectionid = 0;
2377 $formatoptions->name = 'activitytype';
2378 $formatoptions->value = 'scorm';
2379
2380 $courses = $DB->get_recordset('course', array('format' => 'scorm'), 'id');
2381 foreach ($courses as $course) {
2382 $formatoptions->courseid = $course->id;
2383 $DB->insert_record('course_format_options', $formatoptions);
2384 }
2385 $courses->close();
2386
2387 // Now update course format for these courses.
2388 $sql = "UPDATE {course}
2389 SET format = 'singleactivity', modinfo = '', sectioncache = ''
2390 WHERE format = 'scorm'";
2391 $DB->execute($sql);
f6c2af86 2392 upgrade_main_savepoint(true, 2013082700.00);
3d1808c6
DM
2393 }
2394
62321a7b
MN
2395 if ($oldversion < 2013090500.01) {
2396 // Define field calendartype to be added to course.
2397 $table = new xmldb_table('course');
2398 $field = new xmldb_field('calendartype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
2399
2400 // Conditionally launch add field calendartype.
2401 if (!$dbman->field_exists($table, $field)) {
2402 $dbman->add_field($table, $field);
2403 }
2404
e2049782
MG
2405 // Since structure of 'course' table has changed we need to re-read $SITE from DB.
2406 $SITE = $DB->get_record('course', array('id' => $SITE->id));
2407 $COURSE = clone($SITE);
2408
62321a7b
MN
2409 // Define field calendartype to be added to user.
2410 $table = new xmldb_table('user');
2411 $field = new xmldb_field('calendartype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'gregorian');
2412
2413 // Conditionally launch add field calendartype.
2414 if (!$dbman->field_exists($table, $field)) {
2415 $dbman->add_field($table, $field);
2416 }
2417
2418 // Main savepoint reached.
2419 upgrade_main_savepoint(true, 2013090500.01);
2420 }
2421
299cfee5
MG
2422 if ($oldversion < 2013091000.02) {
2423
2424 // Define field cacherev to be added to course.
2425 $table = new xmldb_table('course');
2426 $field = new xmldb_field('cacherev', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'completionnotify');
2427
2428 // Conditionally launch add field cacherev.
2429 if (!$dbman->field_exists($table, $field)) {
2430 $dbman->add_field($table, $field);
2431 }
2432
e2049782
MG
2433 // Since structure of 'course' table has changed we need to re-read $SITE from DB.
2434 $SITE = $DB->get_record('course', array('id' => $SITE->id));
2435 $COURSE = clone($SITE);
2436
299cfee5
MG
2437 // Main savepoint reached.
2438 upgrade_main_savepoint(true, 2013091000.02);
2439 }
2440
64501eee
MG
2441 if ($oldversion < 2013091000.03) {
2442
2443 // Define field modinfo to be dropped from course.
2444 $table = new xmldb_table('course');
2445 $field = new xmldb_field('modinfo');
2446
2447 // Conditionally launch drop field modinfo.
2448 if ($dbman->field_exists($table, $field)) {
2449 $dbman->drop_field($table, $field);
2450 }
2451
2452 // Define field sectioncache to be dropped from course.
2453 $field = new xmldb_field('sectioncache');
2454
2455 // Conditionally launch drop field sectioncache.
2456 if ($dbman->field_exists($table, $field)) {
2457 $dbman->drop_field($table, $field);
2458 }
2459
e2049782
MG
2460 // Since structure of 'course' table has changed we need to re-read $SITE from DB.
2461 $SITE = $DB->get_record('course', array('id' => $SITE->id));
2462 $COURSE = clone($SITE);
2463
64501eee
MG
2464 // Main savepoint reached.
2465 upgrade_main_savepoint(true, 2013091000.03);
2466 }
2467
a8fd33b0
MG
2468 if ($oldversion < 2013091300.01) {
2469
2470 $table = new xmldb_table('user');
2471
2472 // Changing precision of field institution on table user to (255).
2473 $field = new xmldb_field('institution', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'phone2');
2474
2475 // Launch change of precision for field institution.
2476 $dbman->change_field_precision($table, $field);
2477
2478 // Changing precision of field department on table user to (255).
2479 $field = new xmldb_field('department', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'institution');
2480
2481 // Launch change of precision for field department.
2482 $dbman->change_field_precision($table, $field);
2483
2484 // Changing precision of field address on table user to (255).
2485 $field = new xmldb_field('address', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'department');
2486
2487 // Launch change of precision for field address.
2488 $dbman->change_field_precision($table, $field);
2489
2490 // Main savepoint reached.
2491 upgrade_main_savepoint(true, 2013091300.01);
2492 }
2493
e68e4ccf
JP
2494 if ($oldversion < 2013092000.01) {
2495
2496 // Define table question_statistics to be created.
2497 $table = new xmldb_table('question_statistics');
2498
2499 // Adding fields to table question_statistics.
2500 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2501 $table->add_field('hashcode', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2502 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2503 $table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2504 $table->add_field('slot', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
2505 $table->add_field('subquestion', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
2506 $table->add_field('s', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
2507 $table->add_field('effectiveweight', XMLDB_TYPE_NUMBER, '15, 5', null, null, null, null);
2508 $table->add_field('negcovar', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
2509 $table->add_field('discriminationindex', XMLDB_TYPE_NUMBER, '15, 5', null, null, null, null);
2510 $table->add_field('discriminativeefficiency', XMLDB_TYPE_NUMBER, '15, 5', null, null, null, null);
2511 $table->add_field('sd', XMLDB_TYPE_NUMBER, '15, 10', null, null, null, null);
2512 $table->add_field('facility', XMLDB_TYPE_NUMBER, '15, 10', null, null, null, null);
2513 $table->add_field('subquestions', XMLDB_TYPE_TEXT, null, null, null, null, null);
2514 $table->add_field('maxmark', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null);
2515 $table->add_field('positions', XMLDB_TYPE_TEXT, null, null, null, null, null);
2516 $table->add_field('randomguessscore', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null);
2517
2518 // Adding keys to table question_statistics.
2519 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2520
2521 // Conditionally launch create table for question_statistics.
2522 if (!$dbman->table_exists($table)) {
2523 $dbman->create_table($table);
2524 }
2525
2526 // Define table question_response_analysis to be created.
2527 $table = new xmldb_table('question_response_analysis');
2528
2529 // Adding fields to table question_response_analysis.
2530 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2531 $table->add_field('hashcode', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
2532 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2533 $table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
2534 $table->add_field('subqid', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
2535 $table->add_field('aid', XMLDB_TYPE_CHAR, '100', null, null, null, null);
2536 $table->add_field('response', XMLDB_TYPE_TEXT, null, null, null, null, null);
2537 $table->add_field('rcount', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
2538 $table->add_field('credit', XMLDB_TYPE_NUMBER, '15, 5', null, XMLDB_NOTNULL, null, null);
2539
2540 // Adding keys to table question_response_analysis.
2541 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2542
2543 // Conditionally launch create table for question_response_analysis.
2544 if (!$dbman->table_exists($table)) {
2545 $dbman->create_table($table);
2546 }
2547
2548 // Main savepoint reached.
2549 upgrade_main_savepoint(true, 2013092000.01);
2550 }
2551
bde002b8
PS
2552 if ($oldversion < 2013092001.01) {
2553 // Force uninstall of deleted tool.
2554 if (!file_exists("$CFG->dirroot/$CFG->admin/tool/bloglevelupgrade")) {
2555 // Remove capabilities.
2556 capabilities_cleanup('tool_bloglevelupgrade');
2557 // Remove all other associated config.
2558 unset_all_config_for_plugin('tool_bloglevelupgrade');
2559 }
2560 upgrade_main_savepoint(true, 2013092001.01);
2561 }
2562
2563 if ($oldversion < 2013092001.02) {
2564 // Define field version to be dropped from modules.
2565 $table = new xmldb_table('modules');
2566 $field = new xmldb_field('version');
2567
2568 // Conditionally launch drop field version.
2569 if ($dbman->field_exists($table, $field)) {
2570 // Migrate all plugin version info to config_plugins table.
2571 $modules = $DB->get_records('modules');
2572 foreach ($modules as $module) {
2573 set_config('version', $module->version, 'mod_'.$module->name);
2574 }
2575 unset($modules);
2576
2577 $dbman->drop_field($table, $field);
2578 }
2579
2580 // Define field version to be dropped from block.
2581 $table = new xmldb_table('block');
2582 $field = new xmldb_field('version');
2583
2584 // Conditionally launch drop field version.
2585 if ($dbman->field_exists($table, $field)) {
2586 $blocks = $DB->get_records('block');
2587 foreach ($blocks as $block) {
2588 set_config('version', $block->version, 'block_'.$block->name);
2589 }
2590 unset($blocks);
2591
2592 $dbman->drop_field($table, $field);
2593 }
2594
2595 // Main savepoint reached.
2596 upgrade_main_savepoint(true, 2013092001.02);
2597 }
2598
e2a61ee3
MG
2599 if ($oldversion < 2013092700.01) {
2600
2601 $table = new xmldb_table('files');
2602
2603 // Define field referencelastsync to be dropped from files.
2604 $field = new xmldb_field('referencelastsync');
2605
2606 // Conditionally launch drop field referencelastsync.
2607 if ($dbman->field_exists($table, $field)) {
2608 $dbman->drop_field($table, $field);
2609 }
2610
2611 // Define field referencelifetime to be dropped from files.
2612 $field = new xmldb_field('referencelifetime');
2613
2614 // Conditionally launch drop field referencelifetime.
2615 if ($dbman->field_exists($table, $field)) {
2616 $dbman->drop_field($table, $field);
2617 }
2618
2619 // Main savepoint reached.
2620 upgrade_main_savepoint(true, 2013092700.01);
2621 }
5e8331c8 2622
e10df76f
JL
2623 if ($oldversion < 2013100400.01) {
2624 // Add user_devices core table.
2625
2626 // Define field id to be added to user_devices.
2627 $table = new xmldb_table('user_devices');
2628
2629 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
2630 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
2631 $table->add_field('appid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null, 'userid');
2632 $table->add_field('name', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'appid');
2633 $table->add_field('model', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'name');
2634 $table->add_field('platform', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'model');
2635 $table->add_field('version', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'platform');
2636 $table->add_field('pushid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'version');
2637 $table->add_field('uuid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'pushid');
2638 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'uuid');
2639 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'timecreated');
2640
2641 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2642 $table->add_key('pushid-userid', XMLDB_KEY_UNIQUE, array('pushid', 'userid'));
2643 $table->add_key('pushid-platform', XMLDB_KEY_UNIQUE, array('pushid', 'platform'));
2644 $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2645
2646 if (!$dbman->table_exists($table)) {
2647 $dbman->create_table($table);
2648 }
2649
2650 // Main savepoint reached.
2651 upgrade_main_savepoint(true, 2013100400.01);
cbf34f34
SH
2652 }
2653
2654 if ($oldversion < 2013100800.00) {
4e3d8293
TH
2655
2656 // Define field maxfraction to be added to question_attempts.
2657 $table = new xmldb_table('question_attempts');
2658 $field = new xmldb_field('maxfraction', XMLDB_TYPE_NUMBER, '12, 7', null, XMLDB_NOTNULL, null, '1', 'minfraction');
2659
2660 // Conditionally launch add field maxfraction.
2661 if (!$dbman->field_exists($table, $field)) {
2662 $dbman->add_field($table, $field);
2663 }
e10df76f 2664
4e3d8293 2665 // Main savepoint reached.
cbf34f34 2666 upgrade_main_savepoint(true, 2013100800.00);
e10df76f
JL
2667 }
2668
18ef6201 2669 if ($oldversion < 2013100800.01) {
92de749f
PB
2670 // Create a new 'user_password_resets' table.
2671 $table = new xmldb_table('user_password_resets');
2672 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
2673 $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null);
2674 $table->add_field('timerequested', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null);
2675 $table->add_field('timererequested', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, null);
2676 $table->add_field('token', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, null);
2677
2678 // Adding keys to table.
2679 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2680 $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
2681
2682 // Conditionally launch create table.
2683 if (!$dbman->table_exists($table)) {
2684 $dbman->create_table($table);
2685 }
18ef6201 2686 upgrade_main_savepoint(true, 2013100800.01);
92de749f
PB
2687 }
2688
4f0eaf18 2689 if ($oldversion < 2013100800.02) {
3d27180e
DW
2690 $sql = "INSERT INTO {user_preferences}(userid, name, value)
2691 SELECT id, 'htmleditor', 'textarea' FROM {user} u where u.htmleditor = 0";
2692 $DB->execute($sql);
2693
2694 // Define field htmleditor to be dropped from user
2695 $table = new xmldb_table('user');
2696 $field = new xmldb_field('htmleditor');
2697
2698 // Conditionally launch drop field requested
2699 if ($dbman->field_exists($table, $field)) {
2700 $dbman->drop_field($table, $field);
2701 }
3d27180e 2702 // Main savepoint reached.
4f0eaf18 2703 upgrade_main_savepoint(true, 2013100800.02);
3d27180e
DW
2704 }
2705
f18f96f7 2706 if ($oldversion < 2013100900.00) {
87355560
MG
2707
2708 // Define field lifetime to be dropped from files_reference.
2709 $table = new xmldb_table('files_reference');
2710 $field = new xmldb_field('lifetime');
2711
2712 // Conditionally launch drop field lifetime.
2713 if ($dbman->field_exists($table, $field)) {
2714 $dbman->drop_field($table, $field);
2715 }
2716
2717 // Main savepoint reached.
f18f96f7 2718 upgrade_main_savepoint(true, 2013100900.00);
87355560
MG
2719 }
2720
07b02145 2721 if ($oldversion < 2013100901.00) {
fd43a49b
MS
2722 // Fixing possible wrong MIME type for Java Network Launch Protocol (JNLP) files.
2723 $select = $DB->sql_like('filename', '?', false);
2724 $DB->set_field_select(
2725 'files',
2726 'mimetype',
2727 'application/x-java-jnlp-file',
2728 $select,
2729 array('%.jnlp')
2730 );
2731
2732 // Main savepoint reached.
07b02145 2733 upgrade_main_savepoint(true, 2013100901.00);
fd43a49b 2734 }
3481222e
DP
2735
2736 if ($oldversion < 2013102100.00) {
2737 // Changing default value for the status of a course backup.
2738 $table = new xmldb_table('backup_courses');
2739 $field = new xmldb_field('laststatus', XMLDB_TYPE_CHAR, '1', null, XMLDB_NOTNULL, null, '5', 'lastendtime');
2740
2741 // Launch change of precision for field value
2742 $dbman->change_field_precision($table, $field);
2743
2744 // Main savepoint reached.
2745 upgrade_main_savepoint(true, 2013102100.00);
2746 }
2747
2748 if ($oldversion < 2013102201.00) {
775a32c9
DW
2749 $params = array('plugin' => 'editor_atto', 'name' => 'version');
2750 $attoversion = $DB->get_record('config_plugins',
2751 $params,
2752 'value',
2753 IGNORE_MISSING);
2754
2755 if ($attoversion) {
2756 $attoversion = floatval($attoversion->value);
2757 }
2758 // Only these versions that were part of 2.6 beta should be removed.
2759 // Manually installed versions of 2.5 - or later releases for 2.6 installed
2760 // via the plugins DB should not be uninstalled.
3481222e 2761 if ($attoversion && $attoversion > 2013051500.00 && $attoversion < 2013102201.00) {
775a32c9
DW
2762 // Remove all other associated config.
2763 unset_all_config_for_plugin('editor_atto');
2764 unset_all_config_for_plugin('atto_bold');
2765 unset_all_config_for_plugin('atto_clear');
2766 unset_all_config_for_plugin('atto_html');
2767 unset_all_config_for_plugin('atto_image');
2768 unset_all_config_for_plugin('atto_indent');
2769 unset_all_config_for_plugin('atto_italic');
2770 unset_all_config_for_plugin('atto_link');
2771 unset_all_config_for_plugin('atto_media');
2772 unset_all_config_for_plugin('atto_orderedlist');
2773 unset_all_config_for_plugin('atto_outdent');
2774 unset_all_config_for_plugin('atto_strike');
2775 unset_all_config_for_plugin('atto_title');
2776 unset_all_config_for_plugin('atto_underline');
2777 unset_all_config_for_plugin('atto_unlink');
2778 unset_all_config_for_plugin('atto_unorderedlist');
2779
2780 }
2781
2782 // Main savepoint reached.
3481222e 2783 upgrade_main_savepoint(true, 2013102201.00);
70f210c0
MN
2784 }
2785
d90c8115
MG
2786 if ($oldversion < 2013102500.01) {
2787 // Find all fileareas that have missing root folder entry and add the root folder entry.
2788 if (empty($CFG->filesrootrecordsfixed)) {
119cf17e 2789 upgrade_fix_missing_root_folders();
d90c8115
MG
2790 // To skip running the same script on the upgrade to the next major release.
2791 set_config('filesrootrecordsfixed', 1);
2792 }
2793
2794 // Main savepoint reached.
2795 upgrade_main_savepoint(true, 2013102500.01);
2796 }
2797
e0caccb4
PS
2798 if ($oldversion < 2013110500.01) {
2799 // MDL-38228. Corrected course_modules upgrade script instead of 2013021801.01.
2800
2801 // This upgrade script fixes the mismatches between DB fields course_modules.section
2802 // and course_sections.sequence. It makes sure that each module is included
2803 // in the sequence of at least one section.
2804 // There is also a separate script for admins: admin/cli/fix_course_sortorder.php
2805
2806 // This script in included in each major version upgrade process so make sure we don't run it twice.
2807 if (empty($CFG->movingmoduleupgradescriptwasrun)) {
2808 upgrade_course_modules_sequences();
2809
2810 // To skip running the same script on the upgrade to the next major release.
2811 set_config('movingmoduleupgradescriptwasrun', 1);
2812 }
2813
2814 // Main savepoint reached.
2815 upgrade_main_savepoint(true, 2013110500.01);
2816 }
7c2efc11 2817
e0caccb4 2818 if ($oldversion < 2013110600.01) {
7c2efc11 2819
e0caccb4 2820 if (!file_exists($CFG->dirroot . '/theme/mymobile')) {
7c2efc11
JM
2821 // Replace the mymobile settings.
2822 $DB->set_field('course', 'theme', 'clean', array('theme' => 'mymobile'));
2823 $DB->set_field('course_categories', 'theme', 'clean', array('theme' => 'mymobile'));
2824 $DB->set_field('user', 'theme', 'clean', array('theme' => 'mymobile'));
2825 $DB->set_field('mnet_host', 'theme', 'clean', array('theme' => 'mymobile'));
2826
2827 // Replace the theme configs.
e0caccb4 2828 if (get_config('core', 'theme') === 'mymobile') {
7c2efc11
JM
2829 set_config('theme', 'clean');
2830 }
e0caccb4 2831 if (get_config('core', 'thememobile') === 'mymobile') {
7c2efc11
JM
2832 set_config('thememobile', 'clean');
2833 }
e0caccb4 2834 if (get_config('core', 'themelegacy') === 'mymobile') {
7c2efc11
JM
2835 set_config('themelegacy', 'clean');
2836 }
e0caccb4 2837 if (get_config('core', 'themetablet') === 'mymobile') {
7c2efc11
JM
2838 set_config('themetablet', 'clean');
2839 }
47644f7d 2840
e0caccb4
PS
2841 // Hacky emulation of plugin uninstallation.
2842 unset_all_config_for_plugin('theme_mymobile');
47644f7d
MG
2843 }
2844
2845 // Main savepoint reached.
e0caccb4 2846 upgrade_main_savepoint(true, 2013110600.01);
47644f7d
MG
2847 }
2848
4bceea11
FM
2849 if ($oldversion < 2013110600.02) {
2850
2851 // If the user is logged in, we ensure that the alternate name fields are present
2852 // in the session. It will not be the case when upgrading from 2.5 downwards.
2853 if (!empty($USER->id)) {
2854 $refreshuser = $DB->get_record('user', array('id' => $USER->id));
2855 $fields = array('firstnamephonetic', 'lastnamephonetic', 'middlename', 'alternatename', 'firstname', 'lastname');
2856 foreach ($fields as $field) {
2857 $USER->{$field} = $refreshuser->{$field};
2858 }
2859 }
2860
2861 // Main savepoint reached.
2862 upgrade_main_savepoint(true, 2013110600.02);
2863 }
2864
47578dbf
DW
2865 // Moodle v2.6.0 release upgrade line.
2866 // Put any upgrade step following this.
cc49a6f7
AA
2867 if ($oldversion < 2013111800.01) {
2868
2869 // Delete notes of deleted courses.
2870 $sql = "DELETE FROM {post}
2871 WHERE NOT EXISTS (SELECT {course}.id FROM {course}
2872 WHERE {course}.id = {post}.courseid)
2873 AND {post}.module = ?";
2874 $DB->execute($sql, array('notes'));
2875
2876 // Main savepoint reached.
2877 upgrade_main_savepoint(true, 2013111800.01);
2878 }
47578dbf 2879
914499a3
PS
2880 if ($oldversion < 2013122400.01) {
2881 // Purge stored passwords from config_log table, ideally this should be in each plugin
2882 // but that would complicate backporting...
2883 $items = array(
2884 'core/cronremotepassword', 'core/proxypassword', 'core/smtppass', 'core/jabberpassword',
2885 'enrol_database/dbpass', 'enrol_ldap/bind_pw', 'url/secretphrase');
2886 foreach ($items as $item) {
2887 list($plugin, $name) = explode('/', $item);
b47b3c58
EL
2888 $sqlcomparevalue = $DB->sql_compare_text('value');
2889 $sqlcompareoldvalue = $DB->sql_compare_text('oldvalue');
914499a3
PS
2890 if ($plugin === 'core') {
2891 $sql = "UPDATE {config_log}
2892 SET value = :value
b47b3c58
EL
2893 WHERE name = :name AND plugin IS NULL AND $sqlcomparevalue <> :empty";
2894 $params = array('value' => '********', 'name' => $name, 'empty' => '');
914499a3
PS
2895 $DB->execute($sql, $params);
2896
2897 $sql = "UPDATE {config_log}
2898 SET oldvalue = :value
b47b3c58
EL
2899 WHERE name = :name AND plugin IS NULL AND $sqlcompareoldvalue <> :empty";
2900 $params = array('value' => '********', 'name' => $name, 'empty' => '');
914499a3
PS
2901 $DB->execute($sql, $params);
2902
2903 } else {
2904 $sql = "UPDATE {config_log}
2905 SET value = :value
b47b3c58
EL
2906 WHERE name = :name AND plugin = :plugin AND $sqlcomparevalue <> :empty";
2907 $params = array('value' => '********', 'name' => $name, 'plugin' => $plugin, 'empty' => '');
914499a3
PS
2908 $DB->execute($sql, $params);
2909
2910 $sql = "UPDATE {config_log}
2911 SET oldvalue = :value
b47b3c58
EL
2912 WHERE name = :name AND plugin = :plugin AND $sqlcompareoldvalue <> :empty";
2913 $params = array('value' => '********', 'name' => $name, 'plugin' => $plugin, 'empty' => '');
914499a3
PS
2914 $DB->execute($sql, $params);
2915 }
2916 }
2917 // Main savepoint reached.
2918 upgrade_main_savepoint(true, 2013122400.01);
2919 }
2920
62c80325
2921 if ($oldversion < 2014011000.01) {
2922
2923 // Define table cache_text to be dropped.
2924 $table = new xmldb_table('cache_text');
2925
2926 // Conditionally launch drop table for cache_text.
2927 if ($dbman->table_exists($table)) {
2928 $dbman->drop_table($table);
2929 }
2930
2931 unset_config('cachetext');
2932
2933 // Main savepoint reached.
2934 upgrade_main_savepoint(true, 2014011000.01);
2935 }
2936
2a9d7a42
DP
2937 if ($oldversion < 2014011701.00) {
2938 // Fix gradebook sortorder duplicates.
2939 upgrade_grade_item_fix_sortorder();
2940
2941 // Main savepoint reached.
2942 upgrade_main_savepoint(true, 2014011701.00);
2943 }
2944
a5ab83ac
DC
2945 if ($oldversion < 2014012300.01) {
2946 // Remove deleted users home pages.
c8ade8cb
DC
2947 $sql = "DELETE FROM {my_pages}
2948 WHERE EXISTS (SELECT {user}.id
2949 FROM {user}
2950 WHERE {user}.id = {my_pages}.userid
2951 AND {user}.deleted = 1)
2952 AND {my_pages}.private = 1";
2953 $DB->execute($sql);
a5ab83ac
DC
2954
2955 // Reached main savepoint.
2956 upgrade_main_savepoint(true, 2014012300.01);
2957 }
2958
9843e5ec
DW
2959 if ($oldversion < 2014012400.00) {
2960 // Define table lock_db to be created.
2961 $table = new xmldb_table('lock_db');
2962
2963 // Adding fields to table lock_db.
2964 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
2965 $table->add_field('resourcekey', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
2966 $table->add_field('expires', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
2967 $table->add_field('owner', XMLDB_TYPE_CHAR, '36', null, null, null, null);
2968
2969 // Adding keys to table lock_db.
2970 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
2971
2972 // Adding indexes to table lock_db.
2973 $table->add_index('resourcekey_uniq', XMLDB_INDEX_UNIQUE, array('resourcekey'));
2974 $table->add_index('expires_idx', XMLDB_INDEX_NOTUNIQUE, array('expires'));
2975 $table->add_index('owner_idx', XMLDB_INDEX_NOTUNIQUE, array('owner'));
2976
2977 // Conditionally launch create table for lock_db.
2978 if (!$dbman->table_exists($table)) {
2979 $dbman->create_table($table);
2980 }
2981
2982 // Main savepoint reached.
2983 upgrade_main_savepoint(true, 2014012400.00);
2984 }
2985
4922e79f
JP
2986 if ($oldversion < 2014021300.01) {
2987 // Delete any cached stats to force recalculation later, then we can be sure that cached records will have the correct
2988 // field.
2989 $DB->delete_records('question_response_analysis');
2990 $DB->delete_records('question_statistics');
2991 $DB->delete_records('quiz_statistics');
2992
1239d287
JP
2993 // Define field variant to be added to question_statistics.
2994 $table = new xmldb_table('question_statistics');
2995 $field = new xmldb_field('variant', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'subquestion');
2996
2997 // Conditionally launch add field variant.
2998 if (!$dbman->field_exists($table, $field)) {
2999 $dbman->add_field($table, $field);
3000 }
3001
3002 // Main savepoint reached.
4922e79f
JP
3003 upgrade_main_savepoint(true, 2014021300.01);
3004 }
3005
3006 if ($oldversion < 2014021300.02) {
3007
3008 // Define field variant to be added to question_response_analysis.
3009 $table = new xmldb_table('question_response_analysis');
9d3d859c 3010 $field = new xmldb_field('variant', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'questionid');
4922e79f
JP
3011
3012 // Conditionally launch add field variant.
3013 if (!$dbman->field_exists($table, $field)) {
3014 $dbman->add_field($table, $field);
3015 }
3016
3017 // Main savepoint reached.
3018 upgrade_main_savepoint(true, 2014021300.02);
1239d287
JP
3019 }
3020
0e84ecef 3021 if ($oldversion < 2014021800.00) {
fd0e0192
3022
3023 // Define field queued to be added to portfolio_tempdata.
3024 $table = new xmldb_table('portfolio_tempdata');
3025 $field = new xmldb_field('queued', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'instance');
3026
3027 // Conditionally launch add field queued.
3028 if (!$dbman->field_exists($table, $field)) {
3029 $dbman->add_field($table, $field);
3030 }
3031
3032 // Main savepoint reached.
0e84ecef 3033 upgrade_main_savepoint(true, 2014021800.00);
fd0e0192
3034 }
3035
0d7e83c4 3036 if ($oldversion < 2014021900.01) {
4e8a07d2 3037 // Force uninstall of deleted tool.
3a528d4e
TH
3038
3039 // Normally, in this sort of situation, we would do a file_exists check,
3040 // in case the plugin had been added back as an add-on. However, this
3041 // plugin is completely useless after Moodle 2.6, so we check that the
3042 // files have been removed in upgrade_stale_php_files_present, and we
3043 // uninstall it unconditionally here.
3044
3045 // Remove all associated config.
3046 unset_all_config_for_plugin('tool_qeupgradehelper');
3047
0d7e83c4 3048 upgrade_main_savepoint(true, 2014021900.01);
4e8a07d2
TH
3049 }
3050
0d7e83c4 3051 if ($oldversion < 2014021900.02) {
30de48d2
TH
3052
3053 // Define table question_states to be dropped.
3054 $table = new xmldb_table('question_states');
3055
3056 // Conditionally launch drop table for question_states.
3057 if ($dbman->table_exists($table)) {
3058 $dbman->drop_table($table);
3059 }
3060
3061 // Main savepoint reached.
0d7e83c4 3062 upgrade_main_savepoint(true, 2014021900.02);
30de48d2
TH
3063 }
3064
d4e57579 3065 if ($oldversion < 2014021900.03) {
30de48d2
TH
3066
3067 // Define table question_sessions to be dropped.
3068 $table = new xmldb_table('question_sessions');
3069
3070 // Conditionally launch drop table for question_sessions.
3071 if ($dbman->table_exists($table)) {
3072 $dbman->drop_table($table);
3073 }
3074
3075 // Main savepoint reached.
d4e57579 3076 upgrade_main_savepoint(true, 2014021900.03);
30de48d2
TH
3077 }
3078
309ae892
DW
3079 if ($oldversion < 2014022600.00) {
3080 $table = new xmldb_table('task_scheduled');
3081
3082 // Adding fields to table task_scheduled.
3083 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3084 $table->add_field('component', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
3085 $table->add_field('classname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
3086 $table->add_field('lastruntime', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
3087 $table->add_field('nextruntime', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
3088 $table->add_field('blocking', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
3089 $table->add_field('minute', XMLDB_TYPE_CHAR, '25', null, XMLDB_NOTNULL, null, null);
3090 $table->add_field('hour', XMLDB_TYPE_CHAR, '25', null, XMLDB_NOTNULL, null, null);
3091 $table->add_field('day', XMLDB_TYPE_CHAR, '25', null, XMLDB_NOTNULL, null, null);
3092 $table->add_field('month', XMLDB_TYPE_CHAR, '25', null, XMLDB_NOTNULL, null, null);
3093 $table->add_field('dayofweek', XMLDB_TYPE_CHAR, '25', null, XMLDB_NOTNULL, null, null);
3094 $table->add_field('faildelay', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
3095 $table->add_field('customised', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
0a5aa65b 3096 $table->add_field('disabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
309ae892
DW
3097
3098 // Adding keys to table task_scheduled.
3099 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3100
3101 // Adding indexes to table task_scheduled.
3102 $table->add_index('classname_uniq', XMLDB_INDEX_UNIQUE, array('classname'));
3103
3104 // Conditionally launch create table for task_scheduled.
3105 if (!$dbman->table_exists($table)) {
3106 $dbman->create_table($table);
3107 }
3108
3109 // Define table task_adhoc to be created.
3110 $table = new xmldb_table('task_adhoc');
3111
3112 // Adding fields to table task_adhoc.
3113 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3114 $table->add_field('component', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
3115 $table->add_field('classname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
3116 $table->add_field('nextruntime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
3117 $table->add_field('faildelay', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
3118 $table->add_field('customdata', XMLDB_TYPE_TEXT, null, null, null, null, null);
3119 $table->add_field('blocking', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
3120
3121 // Adding keys to table task_adhoc.
3122 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3123
3124 // Adding indexes to table task_adhoc.
3125 $table->add_index('nextruntime_idx', XMLDB_INDEX_NOTUNIQUE, array('nextruntime'));
3126
3127 // Conditionally launch create table for task_adhoc.
3128 if (!$dbman->table_exists($table)) {
3129 $dbman->create_table($table);
3130 }
3131
3132 // Main savepoint reached.
3133 upgrade_main_savepoint(true, 2014022600.00);
3134 }
3135
2c36f834 3136 if ($oldversion < 2014031400.02) {
038014c4
JP
3137 // Delete any cached stats to force recalculation later, then we can be sure that cached records will have the correct
3138 // field.
3139 $DB->delete_records('question_response_analysis');
3140 $DB->delete_records('question_statistics');
3141 $DB->delete_records('quiz_statistics');
3142
3143 // Define field response to be dropped from question_response_analysis.
3144 $table = new xmldb_table('question_response_analysis');
3145 $field = new xmldb_field('rcount');
3146
3147 // Conditionally launch drop field response.
3148 if ($dbman->field_exists($table, $field)) {
3149 $dbman->drop_field($table, $field);
3150 }
3151
3152 // Main savepoint reached.
2c36f834 3153 upgrade_main_savepoint(true, 2014031400.02);
038014c4
JP
3154 }
3155
2c36f834 3156 if ($oldversion < 2014031400.03) {
038014c4
JP
3157
3158 // Define table question_response_count to be created.
3159 $table = new xmldb_table('question_response_count');
3160
3161 // Adding fields to table question_response_count.
3162 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
3163 $table->add_field('analysisid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
3164 $table->add_field('try', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
3165 $table->add_field('rcount', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
3166
3167 // Adding keys to table question_response_count.
3168 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
3169 $table->add_key('analysisid', XMLDB_KEY_FOREIGN, array('analysisid'), 'question_response_analysis', array('id'));
3170
3171 // Conditionally launch create table for question_response_count.
3172 if (!$dbman->table_exists($table)) {
3173 $dbman->create_table($table);
3174 }
3175
3176 // Main savepoint reached.
2c36f834 3177 upgrade_main_savepoint(true, 2014031400.03);
038014c4
JP
3178 }
3179
2c36f834 3180 if ($oldversion < 2014031400.04) {
038014c4
JP
3181
3182 // Define field whichtries to be added to question_response_analysis.
3183 $table = new xmldb_table('question_response_analysis');
3184 $field = new xmldb_field('whichtries', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'hashcode');
3185
3186 // Conditionally launch add field whichtries.
3187 if (!$dbman->field_exists($table, $field)) {
3188 $dbman->add_field($table, $field);
3189 }
3190
3191 // Main savepoint reached.
2c36f834 3192 upgrade_main_savepoint(true, 2014031400.04);
038014c4
JP
3193 }
3194
9c093a7b
PS
3195 if ($oldversion < 2014032600.00) {
3196 // Removing the themes from core.
3197 $themes = array('afterburner', 'anomaly', 'arialist', 'binarius', 'boxxie', 'brick', 'formal_white', 'formfactor',
3198 'fusion', 'leatherbound', 'magazine', 'nimble', 'nonzero', 'overlay', 'serenity', 'sky_high', 'splash',
3199 'standard', 'standardold');
3200
3201 foreach ($themes as $key => $theme) {
3202 if (check_dir_exists($CFG->dirroot . '/theme/' . $theme, false)) {
3203 // Ignore the themes that have been re-downloaded.
3204 unset($themes[$key]);
3205 }
3206 }
3207
9bebe2bb
SH
3208 // Check we actually have themes to remove.
3209 if (count($themes) > 0) {
9c093a7b 3210
9bebe2bb
SH
3211 // Replace the theme configs.
3212 if (in_array(get_config('core', 'theme'), $themes)) {
3213 set_config('theme', 'clean');
3214 }
3215 if (in_array(get_config('core', 'thememobile'), $themes)) {
3216 set_config('thememobile', 'clean');
3217 }
3218 if (in_array(get_config('core', 'themelegacy'), $themes)) {
3219 set_config('themelegacy', 'clean');
3220 }
3221 if (in_array(get_config('core', 'themetablet'), $themes)) {
3222 set_config('themetablet', 'clean');
3223 }
9c093a7b
PS
3224 }
3225
3226 // Main savepoint reached.
3227 upgrade_main_savepoint(true, 2014032600.00);
3228 }
3229
3230 if ($oldversion < 2014032600.02) {
8dfc4d9c
MN
3231 // Add new fields to the 'tag_instance' table.
3232 $table = new xmldb_table('tag_instance');
3233 $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, false, null, null, 'tagid');
3234 if (!$dbman->field_exists($table, $field)) {
3235 $dbman->add_field($table, $field);
3236 }
3237
9c093a7b
PS
3238 $field = new xmldb_field('contextid', XMLDB_TYPE_INTEGER, '10', null, false, null, null, 'itemid');
3239 // Define the 'contextid' foreign key to be added to the tag_instance table.
3240 $key = new xmldb_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
3241 if ($dbman->field_exists($table, $field)) {
3242 $dbman->drop_key($table, $key);
3243 $DB->set_field('tag_instance', 'contextid', null, array('contextid' => 0));
3244 $dbman->change_field_default($table, $field);
3245 } else {
8dfc4d9c
MN
3246 $dbman->add_field($table, $field);
3247 }
9c093a7b 3248 $dbman->add_key($table, $key);
8dfc4d9c
MN
3249
3250 $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'ordering');
3251 if (!$dbman->field_exists($table, $field)) {
3252 $dbman->add_field($table, $field);
3253 }
3254
3255 $sql = "UPDATE {tag_instance}
3256 SET timecreated = timemodified";
3257 $DB->execute($sql);
3258
3259 // Update all the course tags.
3260 $sql = "UPDATE {tag_instance}
3261 SET component = 'core',
3262 contextid = (SELECT ctx.id
3263 FROM {context} ctx
3264 WHERE ctx.contextlevel = :contextlevel
3265 AND ctx.instanceid = {tag_instance}.itemid)
9c093a7b 3266 WHERE itemtype = 'course' AND contextid IS NULL";
8dfc4d9c
MN
3267 $DB->execute($sql, array('contextlevel' => CONTEXT_COURSE));
3268
3269 // Update all the user tags.
3270 $sql = "UPDATE {tag_instance}
3271 SET component = 'core',
3272 contextid = (SELECT ctx.id
3273 FROM {context} ctx
3274 WHERE ctx.contextlevel = :contextlevel
3275 AND ctx.instanceid = {tag_instance}.itemid)
9c093a7b 3276 WHERE itemtype = 'user' AND contextid IS NULL";
8dfc4d9c
MN
3277 $DB->execute($sql, array('contextlevel' => CONTEXT_USER));
3278
3279 // Update all the blog post tags.
3280 $sql = "UPDATE {tag_instance}
3281 SET component = 'core',
3282 contextid = (SELECT ctx.id
3283 FROM {context} ctx
3284 JOIN {post} p
3285 ON p.userid = ctx.instanceid
3286 WHERE ctx.contextlevel = :contextlevel
3287 AND p.id = {tag_instance}.itemid)
9c093a7b 3288 WHERE itemtype = 'post' AND contextid IS NULL";
8dfc4d9c
MN
3289 $DB->execute($sql, array('contextlevel' => CONTEXT_USER));
3290
3291 // Update all the wiki page tags.
3292 $sql = "UPDATE {tag_instance}
3293 SET component = 'mod_wiki',
3294 contextid = (SELECT ctx.id
3295 FROM {context} ctx
3296 JOIN {course_modules} cm
3297 ON cm.id = ctx.instanceid
3298 JOIN {modules} m
3299 ON m.id = cm.module
3300 JOIN {wiki} w
3301 ON w.id = cm.instance
3302 JOIN {wiki_subwikis} sw
3303 ON sw.wikiid = w.id
3304 JOIN {wiki_pages} wp
3305 ON wp.subwikiid = sw.id
3306 WHERE m.name = 'wiki'
3307 AND ctx.contextlevel = :contextlevel
3308 AND wp.id = {tag_instance}.itemid)
9c093a7b 3309 WHERE itemtype = 'wiki_pages' AND contextid IS NULL";
8dfc4d9c
MN
3310 $DB->execute($sql, array('contextlevel' => CONTEXT_MODULE));
3311
3312 // Update all the question tags.
3313 $sql = "UPDATE {tag_instance}
3314 SET component = 'core_question',
3315 contextid = (SELECT qc.contextid
3316 FROM {question} q
3317 JOIN {question_categories} qc
3318 ON q.category = qc.id
3319 WHERE q.id = {tag_instance}.itemid)
9c093a7b 3320 WHERE itemtype = 'question' AND contextid IS NULL";
8dfc4d9c
MN
3321 $DB->execute($sql);
3322
3323 // Update all the tag tags.
3324 $sql = "UPDATE {tag_instance}
3325 SET component = 'core',
3326 contextid = :systemcontext
9c093a7b 3327 WHERE itemtype = 'tag' AND contextid IS NULL";
8dfc4d9c
MN
3328 $DB->execute($sql, array('systemcontext' => context_system::instance()->id));
3329
3330 // Main savepoint reached.
9c093a7b 3331 upgrade_main_savepoint(true, 2014032600.02);
0a341617
MN
3332 }
3333
0a5aa65b
3334 if ($oldversion < 2014032700.01) {
3335
3336 // Define field disabled to be added to task_scheduled.
3337 $table = new xmldb_table('task_scheduled');
3338 $field = new xmldb_field('disabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'customised');
3339
3340 // Conditionally launch add field disabled.
3341 if (!$dbman->field_exists($table, $field)) {
3342 $dbman->add_field($table, $field);
3343 }
3344
3345 // Main savepoint reached.
3346 upgrade_main_savepoint(true, 2014032700.01);
3347 }
3348
52dc1de7
AA
3349 if ($oldversion < 2014032700.02) {
3350
3351 // Update displayloginfailures setting.
3352 if (empty($CFG->displayloginfailures)) {
3353 set_config('displayloginfailures', 0);
3354 } else {
3355 set_config('displayloginfailures', 1);
3356 }
3357
3358 // Main savepoint reached.
3359 upgrade_main_savepoint(true, 2014032700.02);
3360 }
3361
515fe565 3362 if ($oldversion < 2014040800.00) {
8e97006a 3363
3364 // Define field availability to be added to course_modules.
3365 $table = new xmldb_table('course_modules');
3366 $field = new xmldb_field('availability', XMLDB_TYPE_TEXT, null, null, null, null, null, 'showdescription');
3367
3368 // Conditionally launch add field availability.
3369 if (!$dbman->field_exists($table, $field)) {
3370 $dbman->add_field($table, $field);
3371 }
3372
3373 // Define field availability to be added to course_sections.
3374 $table = new xmldb_table('course_sections');
3375 $field = new xmldb_field('availability', XMLDB_TYPE_TEXT, null, null, null, null, null, 'groupingid');
3376
3377 // Conditionally launch add field availability.
3378 if (!$dbman->field_exists($table, $field)) {
3379 $dbman->add_field($table, $field);
3380 }
3381
3382 // Update existing conditions to new format. This could be a slow
3383 // process, so begin by counting the number of affected modules/sections.
3384 // (Performance: On the OU system, these took ~0.3 seconds, with about
3385 // 20,000 results out of about 400,000 total rows in those tables.)
3386 $cmcount = $DB->count_records_sql("
3387 SELECT COUNT(1)
3388 FROM {course_modules} cm
3389 WHERE cm.availablefrom != 0 OR
3390 cm.availableuntil != 0 OR
3391 EXISTS (SELECT 1 FROM {course_modules_availability} WHERE coursemoduleid = cm.id) OR
3392 EXISTS (SELECT 1 FROM {course_modules_avail_fields} WHERE coursemoduleid = cm.id)");
3393 $sectcount = $DB->count_records_sql("
3394 SELECT COUNT(1)
3395 FROM {course_sections} cs
3396 WHERE cs.groupingid != 0 OR
3397 cs.availablefrom != 0 OR
3398 cs.availableuntil != 0 OR
3399 EXISTS (SELECT 1 FROM {course_sections_availability} WHERE coursesectionid = cs.id) OR
3400 EXISTS (SELECT 1 FROM {course_sections_avail_fields} WHERE coursesectionid = cs.id)");
3401
3402 if ($cmcount + $sectcount > 0) {
3403 // Show progress bar and start db transaction.
3404 $transaction = $DB->start_delegated_transaction();
3405 $pbar = new progress_bar('availupdate', 500, true);
3406
3407 // Loop through all course-modules.
3408 // (Performance: On the OU system, the query took <1 second for ~20k
3409 // results; updating all those entries took ~3 minutes.)
3410 $done = 0;
3411 $lastupdate = 0;
3412 $rs = $DB->get_recordset_sql("
3413 SELECT cm.id, cm.availablefrom, cm.availableuntil, cm.showavailability,
039d204a
MG
3414 COUNT(DISTINCT cma.id) AS availcount,
3415 COUNT(DISTINCT cmf.id) AS fieldcount
8e97006a 3416 FROM {course_modules} cm
3417 LEFT JOIN {course_modules_availability} cma ON cma.coursemoduleid = cm.id
3418 LEFT JOIN {course_modules_avail_fields} cmf ON cmf.coursemoduleid = cm.id
3419 WHERE cm.availablefrom != 0 OR
3420 cm.availableuntil != 0 OR
3421 cma.id IS NOT NULL OR
3422 cmf.id IS NOT NULL
3423 GROUP BY cm.id, cm.availablefrom, cm.availableuntil, cm.showavailability");
3424 foreach ($rs as $rec) {
3425 // Update progress initially and then once per second.
3426 if (time() != $lastupdate) {
3427 $lastupdate = time();
3428 $pbar->update($done, $cmcount + $sectcount,
3429 "Updating activity availability settings ($done/$cmcount)");
3430 }
3431
3432 // Get supporting records - only if there are any (to reduce the
3433 // number of queries where just date/group is used).
3434 if ($rec->availcount) {
3435 $availrecs = $DB->get_records('course_modules_availability',
3436 array('coursemoduleid' => $rec->id));
3437 } else {
3438 $availrecs = array();
3439 }
3440 if ($rec->fieldcount) {
3441 $fieldrecs = $DB->get_records_sql("
3442 SELECT cmaf.userfield, cmaf.operator, cmaf.value, uif.shortname
3443 FROM {course_modules_avail_fields} cmaf
3444 LEFT JOIN {user_info_field} uif ON uif.id = cmaf.customfieldid
3445 WHERE cmaf.coursemoduleid = ?", array($rec->id));
3446 } else {
3447 $fieldrecs = array();
3448 }
3449
3450 // Update item.
3451 $availability = upgrade_availability_item(0, 0,
3452 $rec->availablefrom, $rec->availableuntil,
3453 $rec->showavailability, $availrecs, $fieldrecs);
3454 if ($availability) {
3455 $DB->set_field('course_modules', 'availability', $availability, array('id' => $rec->id));
3456 }
3457
3458 // Update progress.
3459 $done++;
3460 }
3461 $rs->close();
3462
3463 // Loop through all course-sections.
3464 // (Performance: On the OU system, this took <1 second for, er, 150 results.)
3465 $done = 0;
3466 $rs = $DB->get_recordset_sql("
3467 SELECT cs.id, cs.groupingid, cs.availablefrom,
3468 cs.availableuntil, cs.showavailability,
039d204a
MG
3469 COUNT(DISTINCT csa.id) AS availcount,
3470 COUNT(DISTINCT csf.id) AS fieldcount
8e97006a 3471 FROM {course_sections} cs
3472 LEFT JOIN {course_sections_availability} csa ON csa.coursesectionid = cs.id
3473 LEFT JOIN {course_sections_avail_fields} csf ON csf.coursesectionid = cs.id
3474 WHERE cs.groupingid != 0 OR
3475 cs.availablefrom != 0 OR
3476 cs.availableuntil != 0 OR
3477 csa.id IS NOT NULL OR
3478 csf.id IS NOT NULL
3479 GROUP BY cs.id, cs.groupingid, cs.availablefrom,
3480 cs.availableuntil, cs.showavailability");
3481 foreach ($rs as $rec) {
3482 // Update progress once per second.
3483 if (time() != $lastupdate) {
3484 $lastupdate = time();
3485 $pbar->update($done + $cmcount, $cmcount + $sectcount,
3486 "Updating section availability settings ($done/$sectcount)");
3487 }
3488
3489 // Get supporting records - only if there are any (to reduce the
3490 // number of queries where just date/group is used).
3491 if ($rec->availcount) {
3492 $availrecs = $DB->get_records('course_sections_availability',
3493 array('coursesectionid' => $rec->id));
3494 } else {
3495 $availrecs = array();
3496 }
3497 if ($rec->fieldcount) {
3498 $fieldrecs = $DB->get_records_sql("
3499 SELECT csaf.userfield, csaf.operator, csaf.value, uif.shortname
3500 FROM {course_sections_avail_fields} csaf
3501 LEFT JOIN {user_info_field} uif ON uif.id = csaf.customfieldid
3502 WHERE csaf.coursesectionid = ?", array($rec->id));
3503 } else {
3504 $fieldrecs = array();
3505 }
3506
3507 // Update item.
3508 $availability = upgrade_availability_item($rec->groupingid ? 1 : 0,
3509 $rec->groupingid, $rec->availablefrom, $rec->availableuntil,
3510 $rec->showavailability, $availrecs, $fieldrecs);
3511 if ($availability) {
3512 $DB->set_field('course_sections', 'availability', $availability, array('id' => $rec->id));
3513 }
3514
3515 // Update progress.
3516 $done++;
3517 }
3518 $rs->close();
3519
3520 // Final progress update for 100%.
3521 $pbar->update($done + $cmcount, $cmcount + $sectcount,
3522 'Availability settings updated for ' . ($cmcount + $sectcount) .
3523 ' activities and sections');
3524
3525 $transaction->allow_commit();
3526 }
3527
3528 // Drop tables which are not necessary because they are covered by the
3529 // new availability fields.
3530 $table = new xmldb_table('course_modules_availability');
3531 if ($dbman->table_exists($table)) {
3532 $dbman->drop_table($table);
3533 }
3534 $table = new xmldb_table('course_modules_avail_fields');
3535 if ($dbman->table_exists($table)) {
3536 $dbman->drop_table($table);
3537 }
3538 $table = new xmldb_table('course_sections_availability');
3539 if ($dbman->table_exists($table)) {
3540 $dbman->drop_table($table);
3541 }
3542 $table = new xmldb_table('course_sections_avail_fields');
3543 if ($dbman->table_exists($table)) {
3544 $dbman->drop_table($table);
3545 }
3546
3547 // Drop unnnecessary fields from course_modules.
3548 $table = new xmldb_table('course_modules');
3549 $field = new xmldb_field('availablefrom');
3550 if ($dbman->field_exists($table, $field)) {
3551 $dbman->drop_field($table, $field);
3552 }
3553 $field = new xmldb_field('availableuntil');
3554 if ($dbman->field_exists($table, $field)) {
3555 $dbman->drop_field($table, $field);
3556 }
3557 $field = new xmldb_field('showavailability');
3558 if ($dbman->field_exists($table, $field)) {
3559</