MDL-18910 normalised module intro and introformat
[moodle.git] / mod / scorm / db / upgrade.php
CommitLineData
b8a342d7 1<?php //$Id$
2
9528568b 3// This file keeps track of upgrades to
b8a342d7 4// the scorm module
5//
6// Sometimes, changes between versions involve
7// alterations to database structures and other
8// major things that may break installations.
9//
10// The upgrade function in this file will attempt
11// to perform all the necessary actions to upgrade
12// your older installtion to the current version.
13//
14// If there's something it cannot do itself, it
15// will tell you what you need to do.
16//
17// The commands in here will all be database-neutral,
b1f93b15 18// using the methods of database_manager class
775f811a 19//
20// Please do not forget to use upgrade_set_timeout()
21// before any action that may take longer time to finish.
b8a342d7 22
775f811a 23function xmldb_scorm_upgrade($oldversion) {
24 global $CFG, $DB;
b8a342d7 25
c57ce9f2 26 $dbman = $DB->get_manager();
b8a342d7 27 $result = true;
28
219f652b 29//===== 1.9.0 upgrade line ======//
9858605e 30
c57ce9f2 31 // Adding missing 'whatgrade' field to table scorm
32 if ($result && $oldversion < 2008073000) {
33 $table = new xmldb_table('scorm');
34 $field = new xmldb_field('whatgrade');
35 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'grademethod');
9528568b 36
c57ce9f2 37 /// Launch add field whatgrade
775f811a 38 if (!$dbman->field_exists($table,$field)) {
c57ce9f2 39 $dbman->add_field($table, $field);
40 }
9528568b 41
c57ce9f2 42 upgrade_mod_savepoint($result, 2008073000, 'scorm');
43 }
9528568b 44
45 if ($result && $oldversion < 2008082500) {
46
47 /// Define field scormtype to be added to scorm
48 $table = new xmldb_table('scorm');
49 $field = new xmldb_field('scormtype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, null, 'local', 'name');
50
51 /// Launch add field scormtype
52 $dbman->add_field($table, $field);
53
54 /// scorm savepoint reached
55 upgrade_mod_savepoint($result, 2008082500, 'scorm');
56 }
57
58 if ($result && $oldversion < 2008090300) {
59
60 /// Define field sha1hash to be added to scorm
61 $table = new xmldb_table('scorm');
62 $field = new xmldb_field('sha1hash', XMLDB_TYPE_CHAR, '40', null, null, null, null, null, null, 'updatefreq');
63
64 /// Launch add field sha1hash
65 $dbman->add_field($table, $field);
66
67 /// scorm savepoint reached
68 upgrade_mod_savepoint($result, 2008090300, 'scorm');
69 }
70
71 if ($result && $oldversion < 2008090301) {
72
73 /// Define field revision to be added to scorm
74 $table = new xmldb_table('scorm');
75 $field = new xmldb_field('revision', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'md5hash');
76
77 /// Launch add field revision
78 $dbman->add_field($table, $field);
79
80 /// scorm savepoint reached
81 upgrade_mod_savepoint($result, 2008090301, 'scorm');
82 }
83
84 if ($result && $oldversion < 2008090302) {
85 $sql = "UPDATE {scorm}
86 SET scormtype = 'external'
87 WHERE reference LIKE ? OR reference LIKE ? OR reference LIKE ?";
88 $DB->execute($sql, array('http://%imsmanifest.xml', 'https://%imsmanifest.xml', 'www.%imsmanifest.xml'));
89
90 $sql = "UPDATE {scorm}
91 SET scormtype = 'localsync'
92 WHERE reference LIKE ? OR reference LIKE ? OR reference LIKE ?
93 OR reference LIKE ? OR reference LIKE ? OR reference LIKE ?";
94 $DB->execute($sql, array('http://%.zip', 'https://%.zip', 'www.%.zip', 'http://%.pif', 'https://%.pif', 'www.%.pif'));
95
96 $sql = "UPDATE {scorm} SET scormtype = 'imsrepository' WHERE reference LIKE ?";
97 $DB->execute($sql, array('#%'));
98
99 /// scorm savepoint reached
100 upgrade_mod_savepoint($result, 2008090302, 'scorm');
101 }
102
103 if ($result && $oldversion < 2008090303) {
104 //remove obsoleted config settings
105 unset_config('scorm_advancedsettings');
106 unset_config('scorm_windowsettings');
107
108 /// scorm savepoint reached
109 upgrade_mod_savepoint($result, 2008090303, 'scorm');
110 }
111
112 if ($result && $oldversion < 2008090304) {
113
114 /////////////////////////////////////
115 /// new file storage upgrade code ///
116 /////////////////////////////////////
117
118 function scorm_migrate_content_files($context, $base, $path) {
119 global $CFG;
120
121 $fullpathname = $base.$path;
122 $fs = get_file_storage();
123 $filearea = 'scorm_content';
124 $items = new DirectoryIterator($fullpathname);
125
126 foreach ($items as $item) {
127 if ($item->isDot()) {
128 unset($item); // release file handle
129 continue;
130 }
131
132 if ($item->isLink()) {
133 // do not follow symlinks - they were never supported in moddata, sorry
134 unset($item); // release file handle
135 continue;
136 }
137
138 if ($item->isFile()) {
139 if (!$item->isReadable()) {
140 notify(" File not readable, skipping: ".$fullpathname.$item->getFilename());
141 unset($item); // release file handle
142 continue;
143 }
144
145 $filepath = clean_param($path, PARAM_PATH);
146 $filename = clean_param($item->getFilename(), PARAM_FILE);
147 $oldpathname = $fullpathname.$item->getFilename();
148
149 if ($filename === '') {
150 continue;
151 unset($item); // release file handle
152 }
153
154 if (!$fs->file_exists($context->id, $filearea, '0', $filepath, $filename)) {
155 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>0, 'filepath'=>$filepath, 'filename'=>$filename,
156 'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
157 unset($item); // release file handle
158 if ($fs->create_file_from_pathname($file_record, $oldpathname)) {
159 @unlink($oldpathname);
160 }
161 } else {
162 unset($item); // release file handle
163 }
164
165 } else {
166 //migrate recursively all subdirectories
167 $oldpathname = $fullpathname.$item->getFilename().'/';
168 $subpath = $path.$item->getFilename().'/';
169 unset($item); // release file handle
170 scorm_migrate_content_files($context, $base, $subpath);
171 @rmdir($oldpathname); // deletes dir if empty
172 }
173 }
174 unset($items); //release file handles
175 }
176
177 $fs = get_file_storage();
178
179 $sqlfrom = "FROM {scorm} s
180 JOIN {modules} m ON m.name = 'scorm'
181 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = s.id)";
182
183 $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
184
185 if ($rs = $DB->get_recordset_sql("SELECT s.id, s.scormtype, s.reference, s.course, cm.id AS cmid $sqlfrom ORDER BY s.course, s.id")) {
186
187 $pbar = new progress_bar('migratescormfiles', 500, true);
188
9528568b 189 $i = 0;
190 foreach ($rs as $scorm) {
191 $i++;
192 upgrade_set_timeout(180); // set up timeout, may also abort execution
193 $pbar->update($i, $count, "Migrating scorm files - $i/$count.");
194
195 $context = get_context_instance(CONTEXT_MODULE, $scorm->cmid);
196 $coursecontext = get_context_instance(CONTEXT_COURSE, $scorm->course);
197
198 // first copy local packages if found - do not delete in case they are shared ;-)
199 if ($scorm->scormtype === 'local' and preg_match('/.*(\.zip|\.pif)$/i', $scorm->reference)) {
200 $packagefile = '/'.clean_param($scorm->reference, PARAM_PATH);
201 $pathnamehash = sha1($coursecontext->id.'course_content0'.$packagefile);
202 if ($file = $fs->get_file_by_hash($pathnamehash)) {
203 $file_record = array('scontextid'=>$context->id, 'filearea'=>'scorm_pacakge',
204 'itemid'=>0, 'filepath'=>'/');
205 $packagefile = $fs->create_file_from_storedfile($file_record, $file);
206 $scorm->reference = $packagefile->get_filename();
207 } else {
208 $scorm->reference = '';
209 }
210 $DB->update_record('scorm', $scorm);
211 }
212
213 // now migrate the extracted package
214 $basepath = "$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/$scorm->id";
215 if (!is_dir($basepath)) {
216 //no files?
217 continue;
218 }
219
220 scorm_migrate_content_files($context, $basepath, '/');
221
222 // remove dirs if empty
223 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/$scorm->id/");
224 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/");
225 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/");
226 }
9528568b 227 $rs->close();
228 }
229
230 /// scorm savepoint reached
231 upgrade_mod_savepoint($result, 2008090304, 'scorm');
232 }
233
6381fa56 234
235 if ($result && $oldversion < 2008090305) {
236
237 /// Define new fields forcecompleted, forcenewattempt, displayattemptstatus, and displaycoursestructure to be added to scorm
238 $table = new xmldb_table('scorm');
239 $field = new xmldb_field('forcecompleted', XMLDB_TYPE_INTEGER, '1', null, null, null, null, null, '0', 'maxattempt');
240 if (!$dbman->field_exists($table,$field)) {
241 $dbman->add_field($table, $field);
242 }
243 $field = new xmldb_field('forcenewattempt', XMLDB_TYPE_INTEGER, '1', null, null, null, null, null, '0', 'forcecompleted');
244 if (!$dbman->field_exists($table,$field)) {
245 $dbman->add_field($table, $field);
246 }
247 $field = new xmldb_field('lastattemptlock', XMLDB_TYPE_INTEGER, '1', null, null, null, null, null, '0', 'forcenewattempt');
248 if (!$dbman->field_exists($table,$field)) {
249 $dbman->add_field($table, $field);
250 }
251 $field = new xmldb_field('displayattemptstatus', XMLDB_TYPE_INTEGER, '1', null, null, null, null, null, '1', 'lastattemptlock');
252 if (!$dbman->field_exists($table,$field)) {
253 $dbman->add_field($table, $field);
254 }
255 $field = new xmldb_field('displaycoursestructure', XMLDB_TYPE_INTEGER, '1', null, null, null, null, null, '1', 'displayattemptstatus');
256 if (!$dbman->field_exists($table,$field)) {
257 $dbman->add_field($table, $field);
258 }
65478eea 259
6381fa56 260 /// scorm savepoint reached
261 upgrade_mod_savepoint($result, 2008090305, 'scorm');
262 }
65478eea 263
30fc6e2d 264
265 // remove redundant config values
266 if ($result && $oldversion < 2008090306) {
65478eea 267 /*
268 * comment this out as it is handled by the update mark 2008090310 below
269 * left for historical documentation as some early adopters may have done
270 * this already.
30fc6e2d 271 $redundant_config = array(
272 'scorm_allowapidebug',
273 'scorm_allowtypeexternal',
274 'scorm_allowtypeimsrepository',
65478eea 275 'scorm_allowtypelocalsync',
276 'scorm_apidebugmask',
277 'scorm_frameheight',
30fc6e2d 278 'scorm_framewidth',
279 'scorm_maxattempts',
280 'scorm_updatetime');
281 foreach ($redundant_config as $rcfg) {
282 if (isset($CFG->$rcfg)) {
283 unset_config($rcfg);
284 }
285 }
65478eea 286 */
1adc77e6 287 /// scorm savepoint reached
288 upgrade_mod_savepoint($result, 2008090306, 'scorm');
289 }
65478eea 290
291
1adc77e6 292
293 // remove redundant config values
294 if ($result && $oldversion < 2008090307) {
65478eea 295 /*
296 * comment this out as it is handled by the update mark 2008090310 below
297 * left for historical documentation as some early adopters may have done
298 * this already.
1adc77e6 299 $redundant_config = array(
300 'scorm_allowapidebug',
301 'scorm_allowtypeexternal',
302 'scorm_allowtypeimsrepository',
65478eea 303 'scorm_allowtypelocalsync',
304 'scorm_apidebugmask',
305 'scorm_frameheight',
1adc77e6 306 'scorm_framewidth',
307 'scorm_maxattempts',
308 'scorm_updatetime',
65478eea 309 'scorm_resizable',
310 'scorm_scrollbars',
311 'scorm_directories',
1adc77e6 312 'scorm_location',
65478eea 313 'scorm_menubar',
314 'scorm_toolbar',
1adc77e6 315 'scorm_status',
316 'scorm_grademethod',
317 'scorm_maxgrade',
318 'scorm_whatgrade',
319 'scorm_popup',
320 'scorm_skipview',
321 'scorm_hidebrowse',
322 'scorm_hidetoc',
323 'scorm_hidenav',
324 'scorm_auto',
325 'scorm_updatefreq'
326 );
327 foreach ($redundant_config as $rcfg) {
328 if (isset($CFG->$rcfg)) {
329 unset_config($rcfg);
30fc6e2d 330 }
331 }
65478eea 332 */
333
30fc6e2d 334 /// scorm savepoint reached
1adc77e6 335 upgrade_mod_savepoint($result, 2008090307, 'scorm');
30fc6e2d 336 }
65478eea 337
d54e2145 338 if ($result && $oldversion < 2008090308) {
339 $table = new xmldb_table('scorm');
340 $field = new xmldb_field('timeopen', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'height');
341 if (!$dbman->field_exists($table,$field)) {
342 $dbman->add_field($table, $field);
343 }
344 $field = new xmldb_field('timeclose', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'timeopen');
345 if (!$dbman->field_exists($table,$field)) {
346 $dbman->add_field($table, $field);
347 }
65478eea 348
d54e2145 349 /// scorm savepoint reached
350 upgrade_mod_savepoint($result, 2008090308, 'scorm');
351 }
65478eea 352
353
354 if ($result && $oldversion < 2008090310) {
355 // take above blocks that delete config and move the values in to config_plugins
356
357 $redundant_config = array(
358 'scorm_allowapidebug',
359 'scorm_allowtypeexternal',
360 'scorm_allowtypeimsrepository',
361 'scorm_allowtypelocalsync',
362 'scorm_apidebugmask',
363 'scorm_frameheight',
364 'scorm_framewidth',
365 'scorm_maxattempts',
366 'scorm_updatetime',
367 'scorm_resizable',
368 'scorm_scrollbars',
369 'scorm_directories',
370 'scorm_location',
371 'scorm_menubar',
372 'scorm_toolbar',
373 'scorm_status',
374 'scorm_grademethod',
375 'scorm_maxgrade',
376 'scorm_whatgrade',
377 'scorm_popup',
378 'scorm_skipview',
379 'scorm_hidebrowse',
380 'scorm_hidetoc',
381 'scorm_hidenav',
382 'scorm_auto',
383 'scorm_updatefreq',
384 'scorm_displayattemptstatus',
385 'scorm_displaycoursestructure',
386 'scorm_forcecompleted',
387 'scorm_forcenewattempt',
388 'scorm_lastattemptlock'
389 );
390
391 foreach ($redundant_config as $rcfg) {
392 if (isset($CFG->$rcfg)) {
393 $shortname = substr($rcfg, 6);
394 $result = $result && set_config($shortname, $CFG->$rcfg, 'scorm');
395 $result = $result && unset_config($rcfg);
396 }
397 }
398
399 /// scorm savepoint reached
400 upgrade_mod_savepoint($result, 2008090310, 'scorm');
401 }
402
b8a342d7 403 return $result;
404}
405
c57ce9f2 406?>