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 *
20 * @package mod
21 * @subpackage scorm
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
b8a342d7 26
b964171c
SH
27/**
28 * @global moodle_database $DB
29 * @param int $oldversion
30 * @return bool
31 */
775f811a 32function xmldb_scorm_upgrade($oldversion) {
33 global $CFG, $DB;
b8a342d7 34
c57ce9f2 35 $dbman = $DB->get_manager();
b8a342d7 36
ba0e91a2 37
46f2a936
AB
38 // Moodle v2.2.0 release upgrade line
39 // Put any upgrade step following this
40
0a466048 41 if ($oldversion < 2012032100) {
94db2749 42 unset_config('updatetime', 'scorm');
0a466048 43 upgrade_mod_savepoint(true, 2012032100, 'scorm');
94db2749
AB
44 }
45
46 // Adding completion fields to scorm table
0a466048 47 if ($oldversion < 2012032101) {
94db2749
AB
48 $table = new xmldb_table('scorm');
49
50 $field = new xmldb_field('completionstatusrequired', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, 'timemodified');
51 if (!$dbman->field_exists($table, $field)) {
52 $dbman->add_field($table, $field);
53 }
54
55 $field = new xmldb_field('completionscorerequired', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null, 'completionstatusrequired');
56 if (!$dbman->field_exists($table, $field)) {
57 $dbman->add_field($table, $field);
58 }
59
0a466048 60 upgrade_mod_savepoint(true, 2012032101, 'scorm');
94db2749
AB
61 }
62
99c3c503
AB
63 // Moodle v2.3.0 release upgrade line
64 // Put any upgrade step following this
65
e0d90c54
DM
66 //rename config var from maxattempts to maxattempt
67 if ($oldversion < 2012061701) {
68 $maxattempts = get_config('scorm', 'maxattempts');
69 $maxattempts_adv = get_config('scorm', 'maxattempts_adv');
70 set_config('maxattempt', $maxattempts, 'scorm');
71 set_config('maxattempt_adv', $maxattempts_adv, 'scorm');
72
73 unset_config('maxattempts', 'scorm'); //remove old setting.
74 unset_config('maxattempts_adv', 'scorm'); //remove old setting.
75 upgrade_mod_savepoint(true, 2012061701, 'scorm');
76 }
77
99c3c503 78
6b9dfe73 79 // Moodle v2.4.0 release upgrade line
d4978e04
DM
80 // Put any upgrade step following this.
81
82 // Moodle v2.5.0 release upgrade line.
83 // Put any upgrade step following this.
6b9dfe73 84
93c984a8 85 // Remove old imsrepository type - convert any existing records to external type to help prevent major errors.
d4978e04 86 if ($oldversion < 2013081301) {
93c984a8 87 $scorms = $DB->get_recordset('scorm', array('scormtype' => 'imsrepository'));
d4978e04 88 foreach ($scorms as $scorm) {
93c984a8
DM
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);
93 }
94 $scorm->revision++;
95 $DB->update_record('scorm', $scorm);
96 }
d4978e04 97 upgrade_mod_savepoint(true, 2013081301, 'scorm');
93c984a8
DM
98 }
99
d4978e04
DM
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
107 WHERE scorm = ?
108 AND " . $DB->sql_isempty('scorm_scoes', 'manifest', false, false) . "
109 AND " . $DB->sql_isnotempty('scorm_scoes', 'organization', false, false) . "
110 AND parent = '/'";
111 $DB->execute($sql, array($aicc->id));
112 }
113 $aiccpackages->close();
114 upgrade_mod_savepoint(true, 2013081302, 'scorm');
115 }
c9e54743 116
b33e4665
DM
117 if ($oldversion < 2013081303) {
118
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');
122
123 // Conditionally launch add field sortorder.
124 if (!$dbman->field_exists($table, $field)) {
125 $dbman->add_field($table, $field);
126 }
127
128 // Scorm savepoint reached.
129 upgrade_mod_savepoint(true, 2013081303, 'scorm');
130 }
131
df5c7086 132 if ($oldversion < 2013090100) {
8d29a8c3 133 global $CFG;
df5c7086
MG
134 $table = new xmldb_table('scorm');
135
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);
139 }
140
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);
144 }
145
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);
149 }
150
151 $field = new xmldb_field('hidenav');
152 if ($dbman->field_exists($table, $field)) {
df5c7086
MG
153 // Update nav setting to show floating navigation buttons under TOC.
154 $DB->set_field('scorm', 'nav', 2, array('hidenav' => 0));
6db676bf
DM
155 $DB->set_field('scorm', 'navpositionleft', 215, array('hidenav' => 2));
156 $DB->set_field('scorm', 'navpositiontop', 300, array('hidenav' => 2));
157
158 // Update nav setting to disable navigation buttons.
159 $DB->set_field('scorm', 'nav', 0, array('hidenav' => 1));
df5c7086
MG
160 // Drop hidenav field.
161 $dbman->drop_field($table, $field);
162 }
163
8d29a8c3
DM
164 $hide = get_config('scorm', 'hidenav');
165 unset_config('hidenav', 'scorm');
166 if (!empty($hide)) {
167 require_once($CFG->dirroot . '/mod/scorm/lib.php');
168 set_config('nav', SCORM_NAV_DISABLED, 'scorm');
df5c7086 169 }
8d29a8c3
DM
170
171 $hideadv = get_config('scorm', 'hidenav_adv');
172 unset_config('hidenav_adv', 'scorm');
173 set_config('nav_adv', $hideadv, 'scorm');
174
df5c7086
MG
175 upgrade_mod_savepoint(true, 2013090100, 'scorm');
176 }
177
47578dbf
DW
178 // Moodle v2.6.0 release upgrade line.
179 // Put any upgrade step following this.
180
27c58e55
DM
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.
184 $sql = "SELECT s.*
185 FROM {scorm} s
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.
eef4c4c1
DM
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.
27c58e55
DM
197 $scorm->launch = $sco->id;
198 $DB->update_record('scorm', $scorm);
199 }
200 }
201 $scorms->close();
202
203 upgrade_mod_savepoint(true, 2013110501, 'scorm');
204 }
205
a4cdd6d2 206 return true;
b8a342d7 207}
208
e5dd8e3b 209