3 // This file keeps track of upgrades to
6 // Sometimes, changes between versions involve
7 // alterations to database structures and other
8 // major things that may break installations.
10 // The upgrade function in this file will attempt
11 // to perform all the necessary actions to upgrade
12 // your older installation to the current version.
14 // If there's something it cannot do itself, it
15 // will tell you what you need to do.
17 // The commands in here will all be database-neutral,
18 // using the methods of database_manager class
20 // Please do not forget to use upgrade_set_timeout()
21 // before any action that may take longer time to finish.
23 function xmldb_scorm_upgrade($oldversion) {
26 $dbman = $DB->get_manager();
28 //===== 1.9.0 upgrade line ======//
30 // Adding missing 'whatgrade' field to table scorm
31 if ($oldversion < 2008073000) {
32 $table = new xmldb_table('scorm');
33 $field = new xmldb_field('whatgrade');
34 $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'grademethod');
36 /// Launch add field whatgrade
37 if (!$dbman->field_exists($table,$field)) {
38 $dbman->add_field($table, $field);
39 $whatgradefixed = get_config('scorm', 'whatgradefixed');
40 if (empty($whatgradefixed)) {
41 /// fix bad usage of whatgrade/grading method.
42 $scorms = $DB->get_records('scorm');
43 foreach ($scorms as $scorm) {
44 $scorm->whatgrade = $scorm->grademethod/10;
45 $DB->update_record('scorm', $scorm);
49 //dump this config var as it isn't needed anymore.
50 unset_config('whatgradefixed', 'scorm');
53 upgrade_mod_savepoint(true, 2008073000, 'scorm');
56 if ($oldversion < 2008082500) {
58 /// Define field scormtype to be added to scorm
59 $table = new xmldb_table('scorm');
60 $field = new xmldb_field('scormtype', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, 'local', 'name');
62 /// Launch add field scormtype
63 $dbman->add_field($table, $field);
65 /// scorm savepoint reached
66 upgrade_mod_savepoint(true, 2008082500, 'scorm');
69 if ($oldversion < 2008090300) {
71 /// Define field sha1hash to be added to scorm
72 $table = new xmldb_table('scorm');
73 $field = new xmldb_field('sha1hash', XMLDB_TYPE_CHAR, '40', null, null, null, null, 'updatefreq');
75 /// Launch add field sha1hash
76 $dbman->add_field($table, $field);
78 /// scorm savepoint reached
79 upgrade_mod_savepoint(true, 2008090300, 'scorm');
82 if ($oldversion < 2008090301) {
84 /// Define field revision to be added to scorm
85 $table = new xmldb_table('scorm');
86 $field = new xmldb_field('revision', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'md5hash');
88 /// Launch add field revision
89 $dbman->add_field($table, $field);
91 /// scorm savepoint reached
92 upgrade_mod_savepoint(true, 2008090301, 'scorm');
95 if ($oldversion < 2008090302) {
96 $sql = "UPDATE {scorm}
97 SET scormtype = 'external'
98 WHERE reference LIKE ? OR reference LIKE ? OR reference LIKE ?";
99 $DB->execute($sql, array('http://%imsmanifest.xml', 'https://%imsmanifest.xml', 'www.%imsmanifest.xml'));
101 $sql = "UPDATE {scorm}
102 SET scormtype = 'localsync'
103 WHERE reference LIKE ? OR reference LIKE ? OR reference LIKE ?
104 OR reference LIKE ? OR reference LIKE ? OR reference LIKE ?";
105 $DB->execute($sql, array('http://%.zip', 'https://%.zip', 'www.%.zip', 'http://%.pif', 'https://%.pif', 'www.%.pif'));
107 $sql = "UPDATE {scorm} SET scormtype = 'imsrepository' WHERE reference LIKE ?";
108 $DB->execute($sql, array('#%'));
110 /// scorm savepoint reached
111 upgrade_mod_savepoint(true, 2008090302, 'scorm');
114 if ($oldversion < 2008090303) {
115 //remove obsoleted config settings
116 unset_config('scorm_advancedsettings');
117 unset_config('scorm_windowsettings');
119 /// scorm savepoint reached
120 upgrade_mod_savepoint(true, 2008090303, 'scorm');
123 if ($oldversion < 2008090304) {
125 /////////////////////////////////////
126 /// new file storage upgrade code ///
127 /////////////////////////////////////
129 function scorm_migrate_content_files($context, $base, $path) {
130 global $CFG, $OUTPUT;
132 $fullpathname = $base.$path;
133 $fs = get_file_storage();
134 $filearea = 'content';
135 $items = new DirectoryIterator($fullpathname);
137 foreach ($items as $item) {
138 if ($item->isDot()) {
139 unset($item); // release file handle
143 if ($item->isLink()) {
144 // do not follow symlinks - they were never supported in moddata, sorry
145 unset($item); // release file handle
149 if ($item->isFile()) {
150 if (!$item->isReadable()) {
151 echo $OUTPUT->notification(" File not readable, skipping: ".$fullpathname.$item->getFilename());
152 unset($item); // release file handle
156 $filepath = clean_param($path, PARAM_PATH);
157 $filename = clean_param($item->getFilename(), PARAM_FILE);
158 $oldpathname = $fullpathname.$item->getFilename();
160 if ($filename === '') {
162 unset($item); // release file handle
165 if (!$fs->file_exists($context->id, 'mod_scorm', $filearea, '0', $filepath, $filename)) {
166 $file_record = array('contextid'=>$context->id, 'component'=>'mod_scorm', 'filearea'=>$filearea, 'itemid'=>0, 'filepath'=>$filepath, 'filename'=>$filename,
167 'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
168 unset($item); // release file handle
169 if ($fs->create_file_from_pathname($file_record, $oldpathname)) {
170 @unlink($oldpathname);
173 unset($item); // release file handle
177 //migrate recursively all subdirectories
178 $oldpathname = $fullpathname.$item->getFilename().'/';
179 $subpath = $path.$item->getFilename().'/';
180 unset($item); // release file handle
181 scorm_migrate_content_files($context, $base, $subpath);
182 @rmdir($oldpathname); // deletes dir if empty
185 unset($items); //release file handles
188 $fs = get_file_storage();
190 $sqlfrom = "FROM {scorm} s
191 JOIN {modules} m ON m.name = 'scorm'
192 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = s.id)";
194 $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
196 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")) {
198 $pbar = new progress_bar('migratescormfiles', 500, true);
201 foreach ($rs as $scorm) {
203 upgrade_set_timeout(180); // set up timeout, may also abort execution
204 $pbar->update($i, $count, "Migrating scorm files - $i/$count.");
206 $context = get_context_instance(CONTEXT_MODULE, $scorm->cmid);
207 $coursecontext = get_context_instance(CONTEXT_COURSE, $scorm->course);
209 // first copy local packages if found - do not delete in case they are shared ;-)
210 if ($scorm->scormtype === 'local' and preg_match('/.*(\.zip|\.pif)$/i', $scorm->reference)) {
211 $packagefile = clean_param($scorm->reference, PARAM_PATH);
212 $pathnamehash = sha1("/$coursecontext->id/course/legacy/0/$packagefile");
213 if ($file = $fs->get_file_by_hash($pathnamehash)) {
214 $file_record = array('scontextid'=>$context->id, 'component'=>'mod_scorm', 'filearea'=>'package',
215 'itemid'=>0, 'filepath'=>'/');
217 $fs->create_file_from_storedfile($file_record, $file);
218 } catch (Exception $x) {
220 $scorm->reference = $file->get_filepath().$file->get_filename();
223 $scorm->reference = '';
225 $DB->update_record('scorm', $scorm);
228 // now migrate the extracted package
229 $basepath = "$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/$scorm->id";
230 if (!is_dir($basepath)) {
235 scorm_migrate_content_files($context, $basepath, '/');
237 // remove dirs if empty
238 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/$scorm->id/");
239 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/scorm/");
240 @rmdir("$CFG->dataroot/$scorm->course/$CFG->moddata/");
245 /// scorm savepoint reached
246 upgrade_mod_savepoint(true, 2008090304, 'scorm');
250 if ($oldversion < 2008090305) {
252 /// Define new fields forcecompleted, forcenewattempt, displayattemptstatus, and displaycoursestructure to be added to scorm
253 $table = new xmldb_table('scorm');
254 $field = new xmldb_field('forcecompleted', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'maxattempt');
255 if (!$dbman->field_exists($table,$field)) {
256 $dbman->add_field($table, $field);
258 $field = new xmldb_field('forcenewattempt', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'forcecompleted');
259 if (!$dbman->field_exists($table,$field)) {
260 $dbman->add_field($table, $field);
262 $field = new xmldb_field('lastattemptlock', XMLDB_TYPE_INTEGER, '1', null, null, null, '0', 'forcenewattempt');
263 if (!$dbman->field_exists($table,$field)) {
264 $dbman->add_field($table, $field);
266 $field = new xmldb_field('displayattemptstatus', XMLDB_TYPE_INTEGER, '1', null, null, null, '1', 'lastattemptlock');
267 if (!$dbman->field_exists($table,$field)) {
268 $dbman->add_field($table, $field);
270 $field = new xmldb_field('displaycoursestructure', XMLDB_TYPE_INTEGER, '1', null, null, null, '1', 'displayattemptstatus');
271 if (!$dbman->field_exists($table,$field)) {
272 $dbman->add_field($table, $field);
275 /// scorm savepoint reached
276 upgrade_mod_savepoint(true, 2008090305, 'scorm');
280 // remove redundant config values
281 if ($oldversion < 2008090306) {
283 * comment this out as it is handled by the update mark 2008090310 below
284 * left for historical documentation as some early adopters may have done
286 $redundant_config = array(
287 'scorm_allowapidebug',
288 'scorm_allowtypeexternal',
289 'scorm_allowtypeimsrepository',
290 'scorm_allowtypelocalsync',
291 'scorm_apidebugmask',
296 foreach ($redundant_config as $rcfg) {
297 if (isset($CFG->$rcfg)) {
302 /// scorm savepoint reached
303 upgrade_mod_savepoint(true, 2008090306, 'scorm');
308 // remove redundant config values
309 if ($oldversion < 2008090307) {
311 * comment this out as it is handled by the update mark 2008090310 below
312 * left for historical documentation as some early adopters may have done
314 $redundant_config = array(
315 'scorm_allowapidebug',
316 'scorm_allowtypeexternal',
317 'scorm_allowtypeimsrepository',
318 'scorm_allowtypelocalsync',
319 'scorm_apidebugmask',
342 foreach ($redundant_config as $rcfg) {
343 if (isset($CFG->$rcfg)) {
349 /// scorm savepoint reached
350 upgrade_mod_savepoint(true, 2008090307, 'scorm');
353 if ($oldversion < 2008090308) {
354 $table = new xmldb_table('scorm');
355 $field = new xmldb_field('timeopen', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'height');
356 if (!$dbman->field_exists($table,$field)) {
357 $dbman->add_field($table, $field);
359 $field = new xmldb_field('timeclose', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timeopen');
360 if (!$dbman->field_exists($table,$field)) {
361 $dbman->add_field($table, $field);
364 /// scorm savepoint reached
365 upgrade_mod_savepoint(true, 2008090308, 'scorm');
369 if ($oldversion < 2008090310) {
370 // take above blocks that delete config and move the values in to config_plugins
372 $redundant_config = array(
373 'scorm_allowapidebug',
374 'scorm_allowtypeexternal',
375 'scorm_allowtypeimsrepository',
376 'scorm_allowtypelocalsync',
377 'scorm_apidebugmask',
399 'scorm_displayattemptstatus',
400 'scorm_displaycoursestructure',
401 'scorm_forcecompleted',
402 'scorm_forcenewattempt',
403 'scorm_lastattemptlock'
406 foreach ($redundant_config as $rcfg) {
407 if (isset($CFG->$rcfg)) {
408 $shortname = substr($rcfg, 6);
409 set_config($shortname, $CFG->$rcfg, 'scorm');
414 /// scorm savepoint reached
415 upgrade_mod_savepoint(true, 2008090310, 'scorm');
418 if ($oldversion < 2009042000) {
420 /// Rename field summary on table scorm to intro
421 $table = new xmldb_table('scorm');
422 $field = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, 'reference');
424 /// Launch rename field summary
425 $dbman->rename_field($table, $field, 'intro');
427 /// scorm savepoint reached
428 upgrade_mod_savepoint(true, 2009042000, 'scorm');
431 if ($oldversion < 2009042001) {
433 /// Define field introformat to be added to scorm
434 $table = new xmldb_table('scorm');
435 $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
437 /// Launch add field introformat
438 if (!$dbman->field_exists($table, $field)) {
439 $dbman->add_field($table, $field);
442 // conditionally migrate to html format in intro
443 if ($CFG->texteditors !== 'textarea') {
444 $rs = $DB->get_recordset('scorm', array('introformat'=>FORMAT_MOODLE), '', 'id,intro,introformat');
445 foreach ($rs as $s) {
446 $s->intro = text_to_html($s->intro, false, false, true);
447 $s->introformat = FORMAT_HTML;
448 $DB->update_record('scorm', $s);
449 upgrade_set_timeout();
454 /// scorm savepoint reached
455 upgrade_mod_savepoint(true, 2009042001, 'scorm');
458 if ($oldversion < 2009042002) {
460 /// Define field introformat to be added to scorm
461 $table = new xmldb_table('scorm_scoes');
462 $field = new xmldb_field('launch', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
464 /// Launch add field introformat
465 $dbman->change_field_type($table, $field);
467 /// scorm savepoint reached
468 upgrade_mod_savepoint(true, 2009042002, 'scorm');
470 if ($oldversion < 2010070800) {
471 //check to see if this has already been tidied up by a 1.9 upgrade.
472 $grademethodfixed = get_config('scorm', 'grademethodfixed');
473 if (empty($grademethodfixed)) {
474 /// fix bad usage of whatgrade/grading method.
475 $scorms = $DB->get_records('scorm');
476 foreach ($scorms as $scorm) {
477 $scorm->grademethod = $scorm->grademethod%10;
478 $DB->update_record('scorm', $scorm);
481 //dump this config var as it isn't needed anymore.
482 unset_config('grademethodfixed', 'scorm');
485 /// scorm savepoint reached
486 upgrade_mod_savepoint(true, 2010070800, 'scorm');