weekly release 2.7dev
[moodle.git] / mod / scorm / db / upgrade.php
CommitLineData
e5dd8e3b 1<?php
f7b5c6aa 2// This file is part of Moodle - http://moodle.org/
b8a342d7 3//
f7b5c6aa
DM
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//
f7b5c6aa
DM
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//
f7b5c6aa
DM
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 * Upgrade script for the scorm module.
19 *
02bff1ad
DM
20 * @package mod_scorm
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
f7b5c6aa
DM
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
b8a342d7 25
b964171c
SH
26/**
27 * @global moodle_database $DB
28 * @param int $oldversion
29 * @return bool
30 */
775f811a 31function xmldb_scorm_upgrade($oldversion) {
32 global $CFG, $DB;
b8a342d7 33
c57ce9f2 34 $dbman = $DB->get_manager();
b8a342d7 35
ba0e91a2 36
46f2a936
AB
37 // Moodle v2.2.0 release upgrade line
38 // Put any upgrade step following this
39
0a466048 40 if ($oldversion < 2012032100) {
94db2749 41 unset_config('updatetime', 'scorm');
0a466048 42 upgrade_mod_savepoint(true, 2012032100, 'scorm');
94db2749
AB
43 }
44
45 // Adding completion fields to scorm table
0a466048 46 if ($oldversion < 2012032101) {
94db2749
AB
47 $table = new xmldb_table('scorm');
48
49 $field = new xmldb_field('completionstatusrequired', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, 'timemodified');
50 if (!$dbman->field_exists($table, $field)) {
51 $dbman->add_field($table, $field);
52 }
53
54 $field = new xmldb_field('completionscorerequired', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null, 'completionstatusrequired');
55 if (!$dbman->field_exists($table, $field)) {
56 $dbman->add_field($table, $field);
57 }
58
0a466048 59 upgrade_mod_savepoint(true, 2012032101, 'scorm');
94db2749
AB
60 }
61
99c3c503
AB
62 // Moodle v2.3.0 release upgrade line
63 // Put any upgrade step following this
64
e0d90c54
DM
65 //rename config var from maxattempts to maxattempt
66 if ($oldversion < 2012061701) {
67 $maxattempts = get_config('scorm', 'maxattempts');
68 $maxattempts_adv = get_config('scorm', 'maxattempts_adv');
69 set_config('maxattempt', $maxattempts, 'scorm');
70 set_config('maxattempt_adv', $maxattempts_adv, 'scorm');
71
72 unset_config('maxattempts', 'scorm'); //remove old setting.
73 unset_config('maxattempts_adv', 'scorm'); //remove old setting.
74 upgrade_mod_savepoint(true, 2012061701, 'scorm');
75 }
76
99c3c503 77
6b9dfe73 78 // Moodle v2.4.0 release upgrade line
d4978e04
DM
79 // Put any upgrade step following this.
80
81 // Moodle v2.5.0 release upgrade line.
82 // Put any upgrade step following this.
6b9dfe73 83
93c984a8 84 // Remove old imsrepository type - convert any existing records to external type to help prevent major errors.
d4978e04 85 if ($oldversion < 2013081301) {
93c984a8 86 $scorms = $DB->get_recordset('scorm', array('scormtype' => 'imsrepository'));
d4978e04 87 foreach ($scorms as $scorm) {
93c984a8
DM
88 $scorm->scormtype = SCORM_TYPE_EXTERNAL;
89 if (!empty($CFG->repository)) { // Fix path to imsmanifest if $CFG->repository is set.
90 $scorm->reference = $CFG->repository.substr($scorm->reference, 1).'/imsmanifest.xml';
91 $scorm->sha1hash = sha1($scorm->reference);
92 }
93 $scorm->revision++;
94 $DB->update_record('scorm', $scorm);
95 }
d4978e04 96 upgrade_mod_savepoint(true, 2013081301, 'scorm');
93c984a8
DM
97 }
98
d4978e04
DM
99 // Fix AICC parent/child relationships (MDL-37394).
100 if ($oldversion < 2013081302) {
101 // Get all AICC packages.
102 $aiccpackages = $DB->get_recordset('scorm', array('version' => 'AICC'), '', 'id');
103 foreach ($aiccpackages as $aicc) {
104 $sql = "UPDATE {scorm_scoes}
105 SET parent = organization
106 WHERE scorm = ?
107 AND " . $DB->sql_isempty('scorm_scoes', 'manifest', false, false) . "
108 AND " . $DB->sql_isnotempty('scorm_scoes', 'organization', false, false) . "
109 AND parent = '/'";
110 $DB->execute($sql, array($aicc->id));
111 }
112 $aiccpackages->close();
113 upgrade_mod_savepoint(true, 2013081302, 'scorm');
114 }
c9e54743 115
b33e4665
DM
116 if ($oldversion < 2013081303) {
117
118 // Define field sortorder to be added to scorm_scoes.
119 $table = new xmldb_table('scorm_scoes');
120 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'title');
121
122 // Conditionally launch add field sortorder.
123 if (!$dbman->field_exists($table, $field)) {
124 $dbman->add_field($table, $field);
125 }
126
127 // Scorm savepoint reached.
128 upgrade_mod_savepoint(true, 2013081303, 'scorm');
129 }
130
df5c7086 131 if ($oldversion < 2013090100) {
8d29a8c3 132 global $CFG;
df5c7086
MG
133 $table = new xmldb_table('scorm');
134
135 $field = new xmldb_field('nav', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, true, null, 1, 'hidetoc');
136 if (!$dbman->field_exists($table, $field)) {
137 $dbman->add_field($table, $field);
138 }
139
140 $field = new xmldb_field('navpositionleft', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, -100, 'nav');
141 if (!$dbman->field_exists($table, $field)) {
142 $dbman->add_field($table, $field);
143 }
144
145 $field = new xmldb_field('navpositiontop', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, -100, 'navpositionleft');
146 if (!$dbman->field_exists($table, $field)) {
147 $dbman->add_field($table, $field);
148 }
149
150 $field = new xmldb_field('hidenav');
151 if ($dbman->field_exists($table, $field)) {
df5c7086
MG
152 // Update nav setting to show floating navigation buttons under TOC.
153 $DB->set_field('scorm', 'nav', 2, array('hidenav' => 0));
6db676bf
DM
154 $DB->set_field('scorm', 'navpositionleft', 215, array('hidenav' => 2));
155 $DB->set_field('scorm', 'navpositiontop', 300, array('hidenav' => 2));
156
157 // Update nav setting to disable navigation buttons.
158 $DB->set_field('scorm', 'nav', 0, array('hidenav' => 1));
df5c7086
MG
159 // Drop hidenav field.
160 $dbman->drop_field($table, $field);
161 }
162
8d29a8c3
DM
163 $hide = get_config('scorm', 'hidenav');
164 unset_config('hidenav', 'scorm');
165 if (!empty($hide)) {
166 require_once($CFG->dirroot . '/mod/scorm/lib.php');
167 set_config('nav', SCORM_NAV_DISABLED, 'scorm');
df5c7086 168 }
8d29a8c3
DM
169
170 $hideadv = get_config('scorm', 'hidenav_adv');
171 unset_config('hidenav_adv', 'scorm');
172 set_config('nav_adv', $hideadv, 'scorm');
173
df5c7086
MG
174 upgrade_mod_savepoint(true, 2013090100, 'scorm');
175 }
176
47578dbf
DW
177 // Moodle v2.6.0 release upgrade line.
178 // Put any upgrade step following this.
179
27c58e55
DM
180 if ($oldversion < 2013110501) {
181 // Fix invalid $scorm->launch records.
182 // Get all scorms that have a launch value that references a sco from a different scorm.
183 $sql = "SELECT s.*
184 FROM {scorm} s
185 LEFT JOIN {scorm_scoes} c ON s.launch = c.id
186 WHERE c.id IS null OR s.id <> c.scorm";
187 $scorms = $DB->get_recordset_sql($sql);
188 foreach ($scorms as $scorm) {
189 // Find the first launchable sco for this SCORM.
eef4c4c1
DM
190 // This scorm has an invalid launch param - we need to calculate it and get the first launchable sco.
191 $sqlselect = 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
192 // We use get_records here as we need to pass a limit in the query that works cross db.
193 $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scorm->id), 'sortorder', 'id', 0, 1);
194 if (!empty($scoes)) {
195 $sco = reset($scoes); // We only care about the first record - the above query only returns one.
27c58e55
DM
196 $scorm->launch = $sco->id;
197 $DB->update_record('scorm', $scorm);
198 }
199 }
200 $scorms->close();
201
202 upgrade_mod_savepoint(true, 2013110501, 'scorm');
203 }
204
02bff1ad
DM
205 if ($oldversion < 2014021700) {
206 // Fix invalid $scorm->launch records that launch an org sco instead of a real sco.
207 $sql = "SELECT s.*, c.identifier
208 FROM {scorm} s
209 LEFT JOIN {scorm_scoes} c ON s.launch = c.id
210 WHERE ".$DB->sql_isempty('scorm_scoes', 'c.launch', false, true);
211 $scorms = $DB->get_recordset_sql($sql);
212 foreach ($scorms as $scorm) {
213 upgrade_set_timeout(60); // Increase execution time just in case. (60 sec is minimum but prob excessive here).
214 $originallaunch = $scorm->launch;
215 // Find the first sco using the current identifier as it's parent
216 // we use get records here as we need to pass a limit in the query that works cross db.
217 $firstsco = $DB->get_records('scorm_scoes',
218 array('scorm' => $scorm->id, 'parent' => $scorm->identifier), 'sortorder', '*', 0, 1);
219 if (!empty($firstsco)) {
220 $firstsco = reset($firstsco);
221 }
222 if (!empty($firstsco->launch)) {
223 // Usual behavior - this is a valid sco with a launch param so use it.
224 $scorm->launch = $firstsco->id;
225 } else {
226 // The firstsco found is not launchable - find the first launchable sco after this sco.
227 $sqlselect = 'scorm = ? AND sortorder > ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
228 // We use get_records here as we need to pass a limit in the query that works cross db.
229 $scoes = $DB->get_records_select('scorm_scoes', $sqlselect,
230 array($scorm->id, $firstsco->sortorder), 'sortorder', 'id', 0, 1);
231 if (!empty($scoes)) {
232 $sco = reset($scoes); // We only care about the first record - the above query only returns one.
233 $scorm->launch = $sco->id;
234 } else {
235 // This is an invalid package - it has a default org that doesn't contain a launchable sco.
236 // Check for any valid sco with a launch param.
237 $sqlselect = 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
238 // We use get_records here as we need to pass a limit in the query that works cross db.
239 $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scorm->id), 'sortorder', 'id', 0, 1);
240 if (!empty($scoes)) {
241 $sco = reset($scoes); // We only care about the first record - the above query only returns one.
242 $scorm->launch = $sco->id;
243 }
244 }
245 }
246 if ($originallaunch != $scorm->launch) {
247 $DB->update_record('scorm', $scorm);
248 }
249 }
250 $scorms->close();
251
252 upgrade_mod_savepoint(true, 2014021700, 'scorm');
253 }
197407ca
JC
254
255 if ($oldversion < 2014031700) {
256 // Define field displayactivityname to be added to scorm.
257 $table = new xmldb_table('scorm');
258 $field = new xmldb_field(
259 'displayactivityname',
260 XMLDB_TYPE_INTEGER,
261 '4',
262 null,
263 XMLDB_NOTNULL,
264 null,
265 '1',
266 'completionscorerequired'
267 );
268
269 // Conditionally launch add field displayactivityname.
270 if (!$dbman->field_exists($table, $field)) {
271 $dbman->add_field($table, $field);
272 }
273
274 // Scorm savepoint reached.
275 upgrade_mod_savepoint(true, 2014031700, 'scorm');
276 }
277
a4cdd6d2 278 return true;
b8a342d7 279}
280
e5dd8e3b 281