2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Upgrade script for the scorm module.
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 * @global moodle_database $DB
28 * @param int $oldversion
31 function xmldb_scorm_upgrade($oldversion) {
34 $dbman = $DB->get_manager();
37 // Moodle v2.2.0 release upgrade line
38 // Put any upgrade step following this
40 if ($oldversion < 2012032100) {
41 unset_config('updatetime', 'scorm');
42 upgrade_mod_savepoint(true, 2012032100, 'scorm');
45 // Adding completion fields to scorm table
46 if ($oldversion < 2012032101) {
47 $table = new xmldb_table('scorm');
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);
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);
59 upgrade_mod_savepoint(true, 2012032101, 'scorm');
62 // Moodle v2.3.0 release upgrade line
63 // Put any upgrade step following this
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');
72 unset_config('maxattempts', 'scorm'); //remove old setting.
73 unset_config('maxattempts_adv', 'scorm'); //remove old setting.
74 upgrade_mod_savepoint(true, 2012061701, 'scorm');
78 // Moodle v2.4.0 release upgrade line
79 // Put any upgrade step following this.
81 // Moodle v2.5.0 release upgrade line.
82 // Put any upgrade step following this.
84 // Remove old imsrepository type - convert any existing records to external type to help prevent major errors.
85 if ($oldversion < 2013081301) {
86 require_once($CFG->dirroot . '/mod/scorm/lib.php');
87 $scorms = $DB->get_recordset('scorm', array('scormtype' => 'imsrepository'));
88 foreach ($scorms as $scorm) {
89 $scorm->scormtype = SCORM_TYPE_EXTERNAL;
90 if (!empty($CFG->repository)) { // Fix path to imsmanifest if $CFG->repository is set.
91 $scorm->reference = $CFG->repository.substr($scorm->reference, 1).'/imsmanifest.xml';
92 $scorm->sha1hash = sha1($scorm->reference);
95 $DB->update_record('scorm', $scorm);
97 upgrade_mod_savepoint(true, 2013081301, 'scorm');
100 // Fix AICC parent/child relationships (MDL-37394).
101 if ($oldversion < 2013081302) {
102 // Get all AICC packages.
103 $aiccpackages = $DB->get_recordset('scorm', array('version' => 'AICC'), '', 'id');
104 foreach ($aiccpackages as $aicc) {
105 $sql = "UPDATE {scorm_scoes}
106 SET parent = organization
108 AND " . $DB->sql_isempty('scorm_scoes', 'manifest', false, false) . "
109 AND " . $DB->sql_isnotempty('scorm_scoes', 'organization', false, false) . "
111 $DB->execute($sql, array($aicc->id));
113 $aiccpackages->close();
114 upgrade_mod_savepoint(true, 2013081302, 'scorm');
117 if ($oldversion < 2013081303) {
119 // Define field sortorder to be added to scorm_scoes.
120 $table = new xmldb_table('scorm_scoes');
121 $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'title');
123 // Conditionally launch add field sortorder.
124 if (!$dbman->field_exists($table, $field)) {
125 $dbman->add_field($table, $field);
128 // Scorm savepoint reached.
129 upgrade_mod_savepoint(true, 2013081303, 'scorm');
132 if ($oldversion < 2013090100) {
134 $table = new xmldb_table('scorm');
136 $field = new xmldb_field('nav', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, true, null, 1, 'hidetoc');
137 if (!$dbman->field_exists($table, $field)) {
138 $dbman->add_field($table, $field);
141 $field = new xmldb_field('navpositionleft', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, -100, 'nav');
142 if (!$dbman->field_exists($table, $field)) {
143 $dbman->add_field($table, $field);
146 $field = new xmldb_field('navpositiontop', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, -100, 'navpositionleft');
147 if (!$dbman->field_exists($table, $field)) {
148 $dbman->add_field($table, $field);
151 $field = new xmldb_field('hidenav');
152 if ($dbman->field_exists($table, $field)) {
153 // Update nav setting to show floating navigation buttons under TOC.
154 $DB->set_field('scorm', 'nav', 2, array('hidenav' => 0));
155 $DB->set_field('scorm', 'navpositionleft', 215, array('hidenav' => 2));
156 $DB->set_field('scorm', 'navpositiontop', 300, array('hidenav' => 2));
158 // Update nav setting to disable navigation buttons.
159 $DB->set_field('scorm', 'nav', 0, array('hidenav' => 1));
160 // Drop hidenav field.
161 $dbman->drop_field($table, $field);
164 $hide = get_config('scorm', 'hidenav');
165 unset_config('hidenav', 'scorm');
167 require_once($CFG->dirroot . '/mod/scorm/lib.php');
168 set_config('nav', SCORM_NAV_DISABLED, 'scorm');
171 $hideadv = get_config('scorm', 'hidenav_adv');
172 unset_config('hidenav_adv', 'scorm');
173 set_config('nav_adv', $hideadv, 'scorm');
175 upgrade_mod_savepoint(true, 2013090100, 'scorm');
178 // Moodle v2.6.0 release upgrade line.
179 // Put any upgrade step following this.
181 if ($oldversion < 2013110501) {
182 // Fix invalid $scorm->launch records.
183 // Get all scorms that have a launch value that references a sco from a different scorm.
186 LEFT JOIN {scorm_scoes} c ON s.launch = c.id
187 WHERE c.id IS null OR s.id <> c.scorm";
188 $scorms = $DB->get_recordset_sql($sql);
189 foreach ($scorms as $scorm) {
190 // Find the first launchable sco for this SCORM.
191 // This scorm has an invalid launch param - we need to calculate it and get the first launchable sco.
192 $sqlselect = 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
193 // We use get_records here as we need to pass a limit in the query that works cross db.
194 $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scorm->id), 'sortorder', 'id', 0, 1);
195 if (!empty($scoes)) {
196 $sco = reset($scoes); // We only care about the first record - the above query only returns one.
197 $scorm->launch = $sco->id;
198 $DB->update_record('scorm', $scorm);
203 upgrade_mod_savepoint(true, 2013110501, 'scorm');
206 if ($oldversion < 2014031700) {
207 // Define field displayactivityname to be added to scorm.
208 $table = new xmldb_table('scorm');
209 $field = new xmldb_field(
210 'displayactivityname',
217 'completionscorerequired'
220 // Conditionally launch add field displayactivityname.
221 if (!$dbman->field_exists($table, $field)) {
222 $dbman->add_field($table, $field);
225 // Scorm savepoint reached.
226 upgrade_mod_savepoint(true, 2014031700, 'scorm');
229 if ($oldversion < 2014040200) {
230 // Fix invalid $scorm->launch records that launch an org sco instead of a real sco.
231 $sql = "SELECT s.*, c.identifier
233 LEFT JOIN {scorm_scoes} c ON s.launch = c.id
234 WHERE ".$DB->sql_isempty('scorm_scoes', 'c.launch', false, true);
235 $scorms = $DB->get_recordset_sql($sql);
236 foreach ($scorms as $scorm) {
237 upgrade_set_timeout(60); // Increase execution time just in case. (60 sec is minimum but prob excessive here).
238 $originallaunch = $scorm->launch;
239 // Find the first sco using the current identifier as it's parent
240 // we use get records here as we need to pass a limit in the query that works cross db.
241 $firstsco = $DB->get_records('scorm_scoes',
242 array('scorm' => $scorm->id, 'parent' => $scorm->identifier), 'sortorder', '*', 0, 1);
243 if (!empty($firstsco)) {
244 $firstsco = reset($firstsco);
246 if (!empty($firstsco->launch)) {
247 // Usual behavior - this is a valid sco with a launch param so use it.
248 $scorm->launch = $firstsco->id;
250 // The firstsco found is not launchable - find the first launchable sco after this sco.
251 $sqlselect = 'scorm = ? AND sortorder > ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
252 // We use get_records here as we need to pass a limit in the query that works cross db.
253 $scoes = $DB->get_records_select('scorm_scoes', $sqlselect,
254 array($scorm->id, $firstsco->sortorder), 'sortorder', 'id', 0, 1);
255 if (!empty($scoes)) {
256 $sco = reset($scoes); // We only care about the first record - the above query only returns one.
257 $scorm->launch = $sco->id;
259 // This is an invalid package - it has a default org that doesn't contain a launchable sco.
260 // Check for any valid sco with a launch param.
261 $sqlselect = 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
262 // We use get_records here as we need to pass a limit in the query that works cross db.
263 $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scorm->id), 'sortorder', 'id', 0, 1);
264 if (!empty($scoes)) {
265 $sco = reset($scoes); // We only care about the first record - the above query only returns one.
266 $scorm->launch = $sco->id;
270 if ($originallaunch != $scorm->launch) {
271 $DB->update_record('scorm', $scorm);
276 upgrade_mod_savepoint(true, 2014040200, 'scorm');
279 // Moodle v2.7.0 release upgrade line.
280 // Put any upgrade step following this.
282 if ($oldversion < 2014072500) {
284 // Define field autocommit to be added to scorm.
285 $table = new xmldb_table('scorm');
286 $field = new xmldb_field('autocommit', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'displayactivityname');
288 // Conditionally launch add field autocommit.
289 if (!$dbman->field_exists($table, $field)) {
290 $dbman->add_field($table, $field);
293 // Scorm savepoint reached.
294 upgrade_mod_savepoint(true, 2014072500, 'scorm');
297 // Moodle v2.8.0 release upgrade line.
298 // Put any upgrade step following this.
300 if ($oldversion < 2015031800) {
302 // Check to see if this site has any AICC packages - if so set the aiccuserid to pass the username
303 // so that the data remains consistent with existing packages.
304 $alreadyset = $DB->record_exists('config_plugins', array('plugin' => 'scorm', 'name' => 'aiccuserid'));
306 $hasaicc = $DB->record_exists('scorm', array('version' => 'AICC'));
308 set_config('aiccuserid', 0, 'scorm');
310 // We set the config value to hide this from upgrades as most users will not know what AICC is anyway.
311 set_config('aiccuserid', 1, 'scorm');
314 // Scorm savepoint reached.
315 upgrade_mod_savepoint(true, 2015031800, 'scorm');
318 // Moodle v2.9.0 release upgrade line.
319 // Put any upgrade step following this.
321 if ($oldversion < 2015091400) {
322 $table = new xmldb_table('scorm');
324 // Changing the default of field forcecompleted on table scorm to 0.
325 $field = new xmldb_field('forcecompleted', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'maxattempt');
326 // Launch change of default for field forcecompleted.
327 $dbman->change_field_default($table, $field);
329 // Changing the default of field displaycoursestructure on table scorm to 0.
330 $field = new xmldb_field('displaycoursestructure', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'displayattemptstatus');
331 // Launch change of default for field displaycoursestructure.
332 $dbman->change_field_default($table, $field);
334 // Scorm savepoint reached.
335 upgrade_mod_savepoint(true, 2015091400, 'scorm');
338 // Moodle v3.0.0 release upgrade line.
339 // Put any upgrade step following this.
341 // MDL-50620 Add mastery override option.
342 if ($oldversion < 2016021000) {
343 $table = new xmldb_table('scorm');
345 $field = new xmldb_field('masteryoverride', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'lastattemptlock');
346 if (!$dbman->field_exists($table, $field)) {
347 $dbman->add_field($table, $field);
350 upgrade_mod_savepoint(true, 2016021000, 'scorm');