MDL-50428 mod_scorm: Move SCO load code to new API

This commit is contained in:
Juan Leyva 2015-11-17 13:23:18 +01:00
parent 0dfcc2541a
commit e9b1e1ccfe
2 changed files with 120 additions and 89 deletions

View File

@ -65,104 +65,24 @@ scorm_require_available($scorm);
$context = context_module::instance($cm->id);
if (!empty($scoid)) {
// Direct SCO request.
if ($sco = scorm_get_sco($scoid)) {
if ($sco->launch == '') {
// Search for the next launchable sco.
if ($scoes = $DB->get_records_select(
'scorm_scoes',
'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true).' AND id > ?',
array($scorm->id, $sco->id),
'sortorder, id')) {
$sco = current($scoes);
}
}
}
}
// If no sco was found get the first of SCORM package.
if (!isset($sco)) {
$scoes = $DB->get_records_select(
'scorm_scoes',
'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true),
array($scorm->id),
'sortorder, id'
);
$sco = current($scoes);
}
// Forge SCO URL.
list($sco, $scolaunchurl) = scorm_get_sco_and_launch_url($scorm, $scoid, $context);
if ($sco->scormtype == 'asset') {
$attempt = scorm_get_last_attempt($scorm->id, $USER->id);
$element = (scorm_version_check($scorm->version, SCORM_13)) ? 'cmi.completion_status' : 'cmi.core.lesson_status';
$value = 'completed';
$result = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
}
// Forge SCO URL.
$connector = '';
$version = substr($scorm->version, 0, 4);
if ((isset($sco->parameters) && (!empty($sco->parameters))) || ($version == 'AICC')) {
if (stripos($sco->launch, '?') !== false) {
$connector = '&';
} else {
$connector = '?';
}
if ((isset($sco->parameters) && (!empty($sco->parameters))) && ($sco->parameters[0] == '?')) {
$sco->parameters = substr($sco->parameters, 1);
}
}
if ($version == 'AICC') {
require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
$aiccsid = scorm_aicc_get_hacp_session($scorm->id);
if (empty($aiccsid)) {
$aiccsid = sesskey();
}
$scoparams = '';
if (isset($sco->parameters) && (!empty($sco->parameters))) {
$scoparams = '&'. $sco->parameters;
}
$launcher = $sco->launch.$connector.'aicc_sid='.$aiccsid.'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$scoparams;
} else {
if (isset($sco->parameters) && (!empty($sco->parameters))) {
$launcher = $sco->launch.$connector.$sco->parameters;
} else {
$launcher = $sco->launch;
}
}
if (scorm_external_link($sco->launch)) {
// TODO: does this happen?
$result = $launcher;
} else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
// Remote learning activity.
$result = dirname($scorm->reference).'/'.$launcher;
} else if ($scorm->scormtype === SCORM_TYPE_LOCAL && strtolower($scorm->reference) == 'imsmanifest.xml') {
// This SCORM content sits in a repository that allows relative links.
$result = "$CFG->wwwroot/pluginfile.php/$context->id/mod_scorm/imsmanifest/$scorm->revision/$launcher";
} else if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
// Note: do not convert this to use get_file_url() or moodle_url()
// SCORM does not work without slasharguments and moodle_url() encodes querystring vars.
$result = "$CFG->wwwroot/pluginfile.php/$context->id/mod_scorm/content/$scorm->revision/$launcher";
}
// Trigger a Sco launched event.
$event = \mod_scorm\event\sco_launched::create(array(
'objectid' => $sco->id,
'context' => $context,
'other' => array('instanceid' => $scorm->id, 'loadedcontent' => $result)
));
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('scorm', $scorm);
$event->add_record_snapshot('scorm_scoes', $sco);
$event->trigger();
// Trigger the SCO launched event.
scorm_launch_sco($scorm, $sco, $cm, $context, $scolaunchurl);
header('Content-Type: text/html; charset=UTF-8');
if ($sco->scormtype == 'asset') {
// HTTP 302 Found => Moved Temporarily.
header('Location: ' . $result);
header('Location: ' . $scolaunchurl);
// Provide a short feedback in case of slow network connection.
echo html_writer::start_tag('html');
echo html_writer::tag('body', html_writer::tag('p', get_string('activitypleasewait', 'scorm')));
@ -216,7 +136,7 @@ echo html_writer::tag('title', 'LoadSCO');
function doredirect() {
if (myGetAPIHandle() != null) {
location = "<?php echo $result ?>";
location = "<?php echo $scolaunchurl ?>";
}
else {
document.body.innerHTML = "<p><?php echo get_string('activityloading', 'scorm');?>" +
@ -231,7 +151,7 @@ echo html_writer::tag('title', 'LoadSCO');
} else {
clearInterval(timer);
document.body.innerHTML = "<p><?php echo get_string('activitypleasewait', 'scorm');?></p>";
location = "<?php echo $result ?>";
location = "<?php echo $scolaunchurl ?>";
}
}, 1000);
}
@ -239,7 +159,7 @@ echo html_writer::tag('title', 'LoadSCO');
//]]>
</script>
<noscript>
<meta http-equiv="refresh" content="0;url=<?php echo $result ?>" />
<meta http-equiv="refresh" content="0;url=<?php echo $scolaunchurl ?>" />
</noscript>
<?php
echo html_writer::end_tag('head');

View File

@ -2084,3 +2084,114 @@ function scorm_require_available($scorm, $checkviewreportcap = false, $context =
}
}
/**
* Return a SCO object and the SCO launch URL
*
* @param stdClass $scorm SCORM object
* @param int $scoid The SCO id in database
* @param stdClass $context context object
* @return array the SCO object and URL
* @since Moodle 3.1
*/
function scorm_get_sco_and_launch_url($scorm, $scoid, $context) {
global $CFG, $DB;
if (!empty($scoid)) {
// Direct SCO request.
if ($sco = scorm_get_sco($scoid)) {
if ($sco->launch == '') {
// Search for the next launchable sco.
if ($scoes = $DB->get_records_select(
'scorm_scoes',
'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true).' AND id > ?',
array($scorm->id, $sco->id),
'sortorder, id')) {
$sco = current($scoes);
}
}
}
}
// If no sco was found get the first of SCORM package.
if (!isset($sco)) {
$scoes = $DB->get_records_select(
'scorm_scoes',
'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true),
array($scorm->id),
'sortorder, id'
);
$sco = current($scoes);
}
$connector = '';
$version = substr($scorm->version, 0, 4);
if ((isset($sco->parameters) && (!empty($sco->parameters))) || ($version == 'AICC')) {
if (stripos($sco->launch, '?') !== false) {
$connector = '&';
} else {
$connector = '?';
}
if ((isset($sco->parameters) && (!empty($sco->parameters))) && ($sco->parameters[0] == '?')) {
$sco->parameters = substr($sco->parameters, 1);
}
}
if ($version == 'AICC') {
require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
$aiccsid = scorm_aicc_get_hacp_session($scorm->id);
if (empty($aiccsid)) {
$aiccsid = sesskey();
}
$scoparams = '';
if (isset($sco->parameters) && (!empty($sco->parameters))) {
$scoparams = '&'. $sco->parameters;
}
$launcher = $sco->launch.$connector.'aicc_sid='.$aiccsid.'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$scoparams;
} else {
if (isset($sco->parameters) && (!empty($sco->parameters))) {
$launcher = $sco->launch.$connector.$sco->parameters;
} else {
$launcher = $sco->launch;
}
}
if (scorm_external_link($sco->launch)) {
// TODO: does this happen?
$scolaunchurl = $launcher;
} else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
// Remote learning activity.
$scolaunchurl = dirname($scorm->reference).'/'.$launcher;
} else if ($scorm->scormtype === SCORM_TYPE_LOCAL && strtolower($scorm->reference) == 'imsmanifest.xml') {
// This SCORM content sits in a repository that allows relative links.
$scolaunchurl = "$CFG->wwwroot/pluginfile.php/$context->id/mod_scorm/imsmanifest/$scorm->revision/$launcher";
} else if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
// Note: do not convert this to use get_file_url() or moodle_url()
// SCORM does not work without slasharguments and moodle_url() encodes querystring vars.
$scolaunchurl = "$CFG->wwwroot/pluginfile.php/$context->id/mod_scorm/content/$scorm->revision/$launcher";
}
return array($sco, $scolaunchurl);
}
/**
* Trigger the scorm_launched event.
*
* @param stdClass $scorm scorm object
* @param stdClass $sco sco object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @param string $scourl SCO URL
* @since Moodle 3.1
*/
function scorm_launch_sco($scorm, $sco, $cm, $context, $scourl) {
$event = \mod_scorm\event\sco_launched::create(array(
'objectid' => $sco->id,
'context' => $context,
'other' => array('instanceid' => $scorm->id, 'loadedcontent' => $scourl)
));
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('scorm', $scorm);
$event->add_record_snapshot('scorm_scoes', $sco);
$event->trigger();
}