we usually use include instead of require for plugin files
[moodle.git] / mod / scorm / db / upgrade.php
CommitLineData
e5dd8e3b 1<?php
b8a342d7 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');
2a88f626 35 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, 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');
2a88f626 49 $field = new xmldb_field('scormtype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, 'local', 'name');
9528568b 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');
2a88f626 62 $field = new xmldb_field('sha1hash', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'updatefreq');
9528568b 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');
2a88f626 75 $field = new xmldb_field('revision', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'md5hash');
9528568b 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) {
6aff538a 119 global $CFG, $OUTPUT;
9528568b 120
121 $fullpathname = $base.$path;
122 $fs = get_file_storage();
64f93798 123 $filearea = 'content';
9528568b 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()) {
6aff538a 140 echo $OUTPUT->notification(" File not readable, skipping: ".$fullpathname.$item->getFilename());
9528568b 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
64f93798
PS
154 if (!$fs->file_exists($context->id, 'mod_scorm', $filearea, '0', $filepath, $filename)) {
155 $file_record = array('contextid'=>$context->id, 'component'=>'mod_scorm', 'filearea'=>$filearea, 'itemid'=>0, 'filepath'=>$filepath, 'filename'=>$filename,
9528568b 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)) {
64f93798
PS
200 $packagefile = clean_param($scorm->reference, PARAM_PATH);
201 $pathnamehash = sha1("/$coursecontext->id/course/legacy/0/$packagefile");
9528568b 202 if ($file = $fs->get_file_by_hash($pathnamehash)) {
64f93798 203 $file_record = array('scontextid'=>$context->id, 'component'=>'mod_scorm', 'filearea'=>'package',
9528568b 204 'itemid'=>0, 'filepath'=>'/');
84683aba
DM
205 try {
206 $fs->create_file_from_storedfile($file_record, $file);
207 } catch (Exception $x) {
208 }
209 $scorm->reference = $file->get_filepath().$file->get_filename();
210
9528568b 211 } else {
212 $scorm->reference = '';
213 }
214 $DB->update_record('scorm', $scorm);
215 }
216
217 // now migrate the extracted package
218 $basepath = "$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/$scorm->id";
219 if (!is_dir($basepath)) {
220 //no files?
221 continue;
222 }
223
224 scorm_migrate_content_files($context, $basepath, '/');
225
226 // remove dirs if empty
227 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/$scorm->id/");
228 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/");
229 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/");
230 }
9528568b 231 $rs->close();
232 }
233
234 /// scorm savepoint reached
235 upgrade_mod_savepoint($result, 2008090304, 'scorm');
236 }
237
6381fa56 238
239 if ($result && $oldversion < 2008090305) {
240
241 /// Define new fields forcecompleted, forcenewattempt, displayattemptstatus, and displaycoursestructure to be added to scorm
242 $table = new xmldb_table('scorm');
2a88f626 243 $field = new xmldb_field('forcecompleted', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'maxattempt');
6381fa56 244 if (!$dbman->field_exists($table,$field)) {
245 $dbman->add_field($table, $field);
246 }
2a88f626 247 $field = new xmldb_field('forcenewattempt', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'forcecompleted');
6381fa56 248 if (!$dbman->field_exists($table,$field)) {
249 $dbman->add_field($table, $field);
250 }
2a88f626 251 $field = new xmldb_field('lastattemptlock', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'forcenewattempt');
6381fa56 252 if (!$dbman->field_exists($table,$field)) {
253 $dbman->add_field($table, $field);
254 }
2a88f626 255 $field = new xmldb_field('displayattemptstatus', XMLDB_TYPE_INTEGER, '1', null, null, null, '1', 'lastattemptlock');
6381fa56 256 if (!$dbman->field_exists($table,$field)) {
257 $dbman->add_field($table, $field);
258 }
2a88f626 259 $field = new xmldb_field('displaycoursestructure', XMLDB_TYPE_INTEGER, '1', null, null, null, '1', 'displayattemptstatus');
6381fa56 260 if (!$dbman->field_exists($table,$field)) {
261 $dbman->add_field($table, $field);
262 }
65478eea 263
6381fa56 264 /// scorm savepoint reached
265 upgrade_mod_savepoint($result, 2008090305, 'scorm');
266 }
65478eea 267
30fc6e2d 268
269 // remove redundant config values
270 if ($result && $oldversion < 2008090306) {
65478eea 271 /*
272 * comment this out as it is handled by the update mark 2008090310 below
273 * left for historical documentation as some early adopters may have done
274 * this already.
30fc6e2d 275 $redundant_config = array(
276 'scorm_allowapidebug',
277 'scorm_allowtypeexternal',
278 'scorm_allowtypeimsrepository',
65478eea 279 'scorm_allowtypelocalsync',
280 'scorm_apidebugmask',
281 'scorm_frameheight',
30fc6e2d 282 'scorm_framewidth',
283 'scorm_maxattempts',
284 'scorm_updatetime');
285 foreach ($redundant_config as $rcfg) {
286 if (isset($CFG->$rcfg)) {
287 unset_config($rcfg);
288 }
289 }
65478eea 290 */
1adc77e6 291 /// scorm savepoint reached
292 upgrade_mod_savepoint($result, 2008090306, 'scorm');
293 }
65478eea 294
295
1adc77e6 296
297 // remove redundant config values
298 if ($result && $oldversion < 2008090307) {
65478eea 299 /*
300 * comment this out as it is handled by the update mark 2008090310 below
301 * left for historical documentation as some early adopters may have done
302 * this already.
1adc77e6 303 $redundant_config = array(
304 'scorm_allowapidebug',
305 'scorm_allowtypeexternal',
306 'scorm_allowtypeimsrepository',
65478eea 307 'scorm_allowtypelocalsync',
308 'scorm_apidebugmask',
309 'scorm_frameheight',
1adc77e6 310 'scorm_framewidth',
311 'scorm_maxattempts',
312 'scorm_updatetime',
65478eea 313 'scorm_resizable',
314 'scorm_scrollbars',
315 'scorm_directories',
1adc77e6 316 'scorm_location',
65478eea 317 'scorm_menubar',
318 'scorm_toolbar',
1adc77e6 319 'scorm_status',
320 'scorm_grademethod',
321 'scorm_maxgrade',
322 'scorm_whatgrade',
323 'scorm_popup',
324 'scorm_skipview',
325 'scorm_hidebrowse',
326 'scorm_hidetoc',
327 'scorm_hidenav',
328 'scorm_auto',
329 'scorm_updatefreq'
330 );
331 foreach ($redundant_config as $rcfg) {
332 if (isset($CFG->$rcfg)) {
333 unset_config($rcfg);
30fc6e2d 334 }
335 }
65478eea 336 */
337
30fc6e2d 338 /// scorm savepoint reached
1adc77e6 339 upgrade_mod_savepoint($result, 2008090307, 'scorm');
30fc6e2d 340 }
65478eea 341
d54e2145 342 if ($result && $oldversion < 2008090308) {
343 $table = new xmldb_table('scorm');
2a88f626 344 $field = new xmldb_field('timeopen', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'height');
d54e2145 345 if (!$dbman->field_exists($table,$field)) {
346 $dbman->add_field($table, $field);
347 }
2a88f626 348 $field = new xmldb_field('timeclose', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timeopen');
d54e2145 349 if (!$dbman->field_exists($table,$field)) {
350 $dbman->add_field($table, $field);
351 }
65478eea 352
d54e2145 353 /// scorm savepoint reached
354 upgrade_mod_savepoint($result, 2008090308, 'scorm');
355 }
65478eea 356
357
358 if ($result && $oldversion < 2008090310) {
359 // take above blocks that delete config and move the values in to config_plugins
360
361 $redundant_config = array(
362 'scorm_allowapidebug',
363 'scorm_allowtypeexternal',
364 'scorm_allowtypeimsrepository',
365 'scorm_allowtypelocalsync',
366 'scorm_apidebugmask',
367 'scorm_frameheight',
368 'scorm_framewidth',
369 'scorm_maxattempts',
370 'scorm_updatetime',
371 'scorm_resizable',
372 'scorm_scrollbars',
373 'scorm_directories',
374 'scorm_location',
375 'scorm_menubar',
376 'scorm_toolbar',
377 'scorm_status',
378 'scorm_grademethod',
379 'scorm_maxgrade',
380 'scorm_whatgrade',
381 'scorm_popup',
382 'scorm_skipview',
383 'scorm_hidebrowse',
384 'scorm_hidetoc',
385 'scorm_hidenav',
386 'scorm_auto',
387 'scorm_updatefreq',
388 'scorm_displayattemptstatus',
389 'scorm_displaycoursestructure',
390 'scorm_forcecompleted',
391 'scorm_forcenewattempt',
392 'scorm_lastattemptlock'
393 );
394
395 foreach ($redundant_config as $rcfg) {
396 if (isset($CFG->$rcfg)) {
397 $shortname = substr($rcfg, 6);
398 $result = $result && set_config($shortname, $CFG->$rcfg, 'scorm');
399 $result = $result && unset_config($rcfg);
400 }
401 }
402
403 /// scorm savepoint reached
404 upgrade_mod_savepoint($result, 2008090310, 'scorm');
405 }
406
d0bcf735 407 if ($result && $oldversion < 2009042000) {
408
409 /// Rename field summary on table scorm to intro
410 $table = new xmldb_table('scorm');
2a88f626 411 $field = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, 'reference');
d0bcf735 412
413 /// Launch rename field summary
414 $dbman->rename_field($table, $field, 'intro');
415
416 /// scorm savepoint reached
417 upgrade_mod_savepoint($result, 2009042000, 'scorm');
418 }
419
420 if ($result && $oldversion < 2009042001) {
421
422 /// Define field introformat to be added to scorm
423 $table = new xmldb_table('scorm');
2a88f626 424 $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
d0bcf735 425
426 /// Launch add field introformat
427 $dbman->add_field($table, $field);
428
429 /// scorm savepoint reached
430 upgrade_mod_savepoint($result, 2009042001, 'scorm');
431 }
432
1bd51a6c
DM
433 if ($result && $oldversion < 2009042002) {
434
435 /// Define field introformat to be added to scorm
436 $table = new xmldb_table('scorm_scoes');
437 $field = new xmldb_field('launch', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
438
439 /// Launch add field introformat
440 $dbman->change_field_type($table, $field);
441
442 /// scorm savepoint reached
443 upgrade_mod_savepoint($result, 2009042002, 'scorm');
444 }
445
b8a342d7 446 return $result;
447}
448
e5dd8e3b 449