diff --git a/mod/scorm/backup/moodle2/restore_scorm_stepslib.php b/mod/scorm/backup/moodle2/restore_scorm_stepslib.php index b34a9149f55..a58786cb048 100644 --- a/mod/scorm/backup/moodle2/restore_scorm_stepslib.php +++ b/mod/scorm/backup/moodle2/restore_scorm_stepslib.php @@ -184,9 +184,27 @@ class restore_scorm_activity_structure_step extends restore_activity_structure_s } protected function after_execute() { + global $DB; + // Add scorm related files, no need to match by itemname (just internally handled context) $this->add_related_files('mod_scorm', 'intro', null); $this->add_related_files('mod_scorm', 'content', null); $this->add_related_files('mod_scorm', 'package', null); + + // Fix launch param in scorm table to use new sco id. + $scormid = $this->get_new_parentid('scorm'); + $scorm = $DB->get_record('scorm', array('id' => $scormid)); + $scorm->launch = $this->get_mappingid('scorm_sco', $scorm->launch, ''); + if (empty($scorm->launch)) { + // This scorm has an invalid launch param - we need to calculate it and get the first launchable sco. + $sqlselect = 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true). ' ORDER BY sortorder LIMIT 1'; + $sco = $DB->get_record_select('scorm_scoes', $sqlselect, array($scorm->id)); + if (!empty($sco)) { + $scorm->launch = $sco->id; + } + } + if (!empty($scorm->launch)) { + $DB->update_record('scorm', $scorm); + } } } diff --git a/mod/scorm/db/upgrade.php b/mod/scorm/db/upgrade.php index f2171b85fb2..e3ba1494b2f 100644 --- a/mod/scorm/db/upgrade.php +++ b/mod/scorm/db/upgrade.php @@ -175,6 +175,28 @@ function xmldb_scorm_upgrade($oldversion) { upgrade_mod_savepoint(true, 2013090100, 'scorm'); } + if ($oldversion < 2013110501) { + // Fix invalid $scorm->launch records. + // Get all scorms that have a launch value that references a sco from a different scorm. + $sql = "SELECT s.* + FROM {scorm} s + LEFT JOIN {scorm_scoes} c ON s.launch = c.id + WHERE c.id IS null OR s.id <> c.scorm"; + $scorms = $DB->get_recordset_sql($sql); + foreach ($scorms as $scorm) { + // Find the first launchable sco for this SCORM. + $sqlselect = 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true). ' ORDER BY sortorder LIMIT 1'; + $sco = $DB->get_record_select('scorm_scoes', $sqlselect, array($scorm->id)); + if (!empty($sco)) { + $scorm->launch = $sco->id; + $DB->update_record('scorm', $scorm); + } + } + $scorms->close(); + + upgrade_mod_savepoint(true, 2013110501, 'scorm'); + } + return true; } diff --git a/mod/scorm/version.php b/mod/scorm/version.php index 911a4a38677..ac3e1b8cd9b 100644 --- a/mod/scorm/version.php +++ b/mod/scorm/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$module->version = 2013110500; // The current module version (Date: YYYYMMDDXX) +$module->version = 2013110501; // The current module version (Date: YYYYMMDDXX) $module->requires = 2013110500; // Requires this Moodle version $module->component = 'mod_scorm'; // Full name of the plugin (used for diagnostics) $module->cron = 300;