MDL-57649 mod_lesson: Delete answer files correctly.
[moodle.git] / mod / lesson / db / upgrade.php
blob6c9baf79a146517537684bcb77f2b26b0bd59ac7
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
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/>.
17 /**
18 * This file keeps track of upgrades to
19 * the lesson module
21 * Sometimes, changes between versions involve
22 * alterations to database structures and other
23 * major things that may break installations.
25 * The upgrade function in this file will attempt
26 * to perform all the necessary actions to upgrade
27 * your older installation to the current version.
29 * If there's something it cannot do itself, it
30 * will tell you what you need to do.
32 * The commands in here will all be database-neutral,
33 * using the methods of database_manager class
35 * Please do not forget to use upgrade_set_timeout()
36 * before any action that may take longer time to finish.
38 * @package mod_lesson
39 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 o
43 defined('MOODLE_INTERNAL') || die();
45 /**
47 * @global stdClass $CFG
48 * @global moodle_database $DB
49 * @param int $oldversion
50 * @return bool
52 function xmldb_lesson_upgrade($oldversion) {
53 global $CFG, $DB;
55 $dbman = $DB->get_manager();
57 if ($oldversion < 2016012800) {
58 // Convert lesson settings to use config_plugins instead of $CFG.
59 // Lesson_maxanswers => mod_lesson/maxanswers.
60 if (isset($CFG->lesson_maxanswers)) {
61 set_config('maxanswers', $CFG->lesson_maxanswers, 'mod_lesson');
62 set_config('maxanswers_adv', '1', 'mod_lesson');
63 unset_config('lesson_maxanswers');
66 // Lesson_slideshowwidth => mod_lesson/slideshowwidth.
67 if (isset($CFG->lesson_slideshowwidth)) {
68 set_config('slideshowwidth', $CFG->lesson_slideshowwidth, 'mod_lesson');
69 unset_config('lesson_slideshowwidth');
72 // Lesson_slideshowheight => mod_lesson/slideshowheight.
73 if (isset($CFG->lesson_slideshowheight)) {
74 set_config('slideshowheight', $CFG->lesson_slideshowheight, 'mod_lesson');
75 unset_config('lesson_slideshowheight');
78 // Lesson_slideshowbgcolor => mod_lesson/slideshowbgcolor.
79 if (isset($CFG->lesson_slideshowbgcolor)) {
80 set_config('slideshowbgcolor', $CFG->lesson_slideshowbgcolor, 'mod_lesson');
81 unset_config('lesson_slideshowbgcolor');
84 // Lesson_defaultnextpage => mod_lesson/defaultnextpage.
85 if (isset($CFG->lesson_defaultnextpage)) {
86 set_config('defaultnextpage', $CFG->lesson_defaultnextpage, 'mod_lesson');
87 set_config('defaultnextpage_adv', '1', 'mod_lesson');
88 unset_config('lesson_defaultnextpage');
91 // Lesson_mediawidth => mod_lesson/mediawidth.
92 if (isset($CFG->lesson_mediawidth)) {
93 set_config('mediawidth', $CFG->lesson_mediawidth, 'mod_lesson');
94 unset_config('lesson_mediawidth');
97 // Lesson_mediaheight => mod_lesson/mediaheight.
98 if (isset($CFG->lesson_mediaheight)) {
99 set_config('mediaheight', $CFG->lesson_mediaheight, 'mod_lesson');
100 unset_config('lesson_mediaheight');
103 // Lesson_mediaclose => mod_lesson/mediaclose.
104 if (isset($CFG->lesson_mediaclose)) {
105 set_config('mediaclose', $CFG->lesson_mediaclose, 'mod_lesson');
106 unset_config('lesson_mediaclose');
109 // Lesson savepoint reached.
110 upgrade_mod_savepoint(true, 2016012800, 'lesson');
112 // Moodle v3.1.0 release upgrade line.
113 // Put any upgrade step following this.
115 // Automatically generated Moodle v3.2.0 release upgrade line.
116 // Put any upgrade step following this.
118 if ($oldversion < 2016120515) {
119 // Define new fields to be added to lesson.
120 $table = new xmldb_table('lesson');
121 $field = new xmldb_field('allowofflineattempts', XMLDB_TYPE_INTEGER, '1', null, null, null, 0, 'completiontimespent');
122 // Conditionally launch add field allowofflineattempts.
123 if (!$dbman->field_exists($table, $field)) {
124 $dbman->add_field($table, $field);
126 // Lesson savepoint reached.
127 upgrade_mod_savepoint(true, 2016120515, 'lesson');
129 if ($oldversion < 2016120516) {
130 // New field for lesson_timer.
131 $table = new xmldb_table('lesson_timer');
132 $field = new xmldb_field('timemodifiedoffline', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, 'completed');
133 // Conditionally launch add field timemodifiedoffline.
134 if (!$dbman->field_exists($table, $field)) {
135 $dbman->add_field($table, $field);
137 // Lesson savepoint reached.
138 upgrade_mod_savepoint(true, 2016120516, 'lesson');
141 // Automatically generated Moodle v3.3.0 release upgrade line.
142 // Put any upgrade step following this.
144 if ($oldversion < 2017051501) {
146 // Delete orphaned lesson answer and response files.
147 $sql = "SELECT DISTINCT f.*
148 FROM {files} f
149 LEFT JOIN {lesson_answers} la ON f.itemid = la.id
150 WHERE component = :component
151 AND la.id IS NULL";
153 $orphanedfiles = $DB->get_recordset_sql($sql, array('component' => 'mod_lesson'));
154 $fs = get_file_storage();
155 foreach ($orphanedfiles as $file) {
156 $fs->delete_area_files($file->contextid, $file->component, $file->filearea, $file->itemid);
158 $orphanedfiles->close();
160 upgrade_mod_savepoint(true, 2017051501, 'lesson');
163 return true;