Merge branch 'MDL-59527-master-fix1' of http://github.com/damyon/moodle
[moodle.git] / mod / lesson / db / upgrade.php
CommitLineData
86342d63 1<?php
0a4abb73 2// This file is part of Moodle - http://moodle.org/
b8a342d7 3//
0a4abb73
SH
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.
b8a342d7 8//
0a4abb73
SH
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.
b8a342d7 13//
0a4abb73
SH
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
19 * the lesson module
20 *
21 * Sometimes, changes between versions involve
22 * alterations to database structures and other
23 * major things that may break installations.
24 *
25 * The upgrade function in this file will attempt
26 * to perform all the necessary actions to upgrade
2e0406a5 27 * your older installation to the current version.
0a4abb73
SH
28 *
29 * If there's something it cannot do itself, it
30 * will tell you what you need to do.
31 *
32 * The commands in here will all be database-neutral,
33 * using the methods of database_manager class
34 *
35 * Please do not forget to use upgrade_set_timeout()
36 * before any action that may take longer time to finish.
2f67a9b3 37 *
9b24f68b 38 * @package mod_lesson
cc3dbaaa
PS
39 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 o
0a4abb73 41 */
b8a342d7 42
1b08cef7
PS
43defined('MOODLE_INTERNAL') || die();
44
36a60f0f
SH
45/**
46 *
47 * @global stdClass $CFG
48 * @global moodle_database $DB
36a60f0f 49 * @param int $oldversion
40e4cd43 50 * @return bool
36a60f0f 51 */
775f811a 52function xmldb_lesson_upgrade($oldversion) {
e8c82aac 53 global $CFG, $DB;
b8a342d7 54
f33e1ed4 55 $dbman = $DB->get_manager();
ff99f91a 56
a36eba45
SB
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');
64 }
65
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');
70 }
71
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');
76 }
77
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');
82 }
83
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');
89 }
90
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');
95 }
96
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');
101 }
102
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');
107 }
108
109 // Lesson savepoint reached.
110 upgrade_mod_savepoint(true, 2016012800, 'lesson');
111 }
4da854a6
EL
112 // Moodle v3.1.0 release upgrade line.
113 // Put any upgrade step following this.
114
75c57a08
EL
115 // Automatically generated Moodle v3.2.0 release upgrade line.
116 // Put any upgrade step following this.
117
c6f9f061
JL
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);
125 }
126 // Lesson savepoint reached.
127 upgrade_mod_savepoint(true, 2016120515, 'lesson');
128 }
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);
136 }
137 // Lesson savepoint reached.
138 upgrade_mod_savepoint(true, 2016120516, 'lesson');
139 }
140
5e272283
EL
141 // Automatically generated Moodle v3.3.0 release upgrade line.
142 // Put any upgrade step following this.
143
63cf0326
AG
144 if ($oldversion < 2017051501) {
145
146 // Delete orphaned lesson answer and response files.
7b78154e 147 $sql = "SELECT DISTINCT f.contextid, f.component, f.filearea, f.itemid
63cf0326
AG
148 FROM {files} f
149 LEFT JOIN {lesson_answers} la ON f.itemid = la.id
150 WHERE component = :component
23e2a9cb 151 AND (filearea = :fileareaanswer OR filearea = :filearearesponse)
63cf0326
AG
152 AND la.id IS NULL";
153
23e2a9cb
AG
154 $orphanedfiles = $DB->get_recordset_sql($sql, array('component' => 'mod_lesson', 'fileareaanswer' => 'page_answers',
155 'filearearesponse' => 'page_responses'));
63cf0326
AG
156 $fs = get_file_storage();
157 foreach ($orphanedfiles as $file) {
158 $fs->delete_area_files($file->contextid, $file->component, $file->filearea, $file->itemid);
159 }
160 $orphanedfiles->close();
161
162 upgrade_mod_savepoint(true, 2017051501, 'lesson');
163 }
164
a4cdd6d2 165 return true;
b8a342d7 166}