rename api.php file:MDL-17515: Moodle API will treat files called api.php in a specia...
[moodle.git] / mod / scorm / loadSCO.php
CommitLineData
f4ba7e1a 1<?php // $Id$
b3659259 2 require_once('../../config.php');
03f5a0f8 3 require_once('locallib.php');
4
9528568b 5 $id = optional_param('id', '', PARAM_INT); // Course Module ID, or
6 $a = optional_param('a', '', PARAM_INT); // scorm ID
03f5a0f8 7 $scoid = required_param('scoid', PARAM_INT); // sco ID
8
8949f8df 9 $delayseconds = 2; // Delay time before sco launch, used to give time to browser to define API
10
03f5a0f8 11 if (!empty($id)) {
12 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
08b56f93 13 print_error('invalidcoursemodule');
03f5a0f8 14 }
bf347041 15 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
08b56f93 16 print_error('coursemisconf');
03f5a0f8 17 }
bf347041 18 if (! $scorm = $DB->get_record('scorm', array('id'=>$cm->instance))) {
08b56f93 19 print_error('invalidcoursemodule');
03f5a0f8 20 }
21 } else if (!empty($a)) {
bf347041 22 if (! $scorm = $DB->get_record('scorm', array('id'=>$a))) {
08b56f93 23 print_error('coursemisconf');
03f5a0f8 24 }
bf347041 25 if (! $course = $DB->get_record('course', array('id'=>$scorm->course))) {
08b56f93 26 print_error('coursemisconf');
03f5a0f8 27 }
b3659259 28 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
08b56f93 29 print_error('invalidcoursemodule');
03f5a0f8 30 }
31 } else {
08b56f93 32 print_error('missingparameter');
03f5a0f8 33 }
34
35 require_login($course->id, false, $cm);
d54e2145 36
37 //check if scorm closed
38 $timenow = time();
39 if ($scorm->timeclose !=0) {
40 if ($scorm->timeopen > $timenow) {
41 error(get_string("notopenyet", "scorm", userdate($scorm->timeopen)));
42 } else if ($timenow > $scorm->timeclose) {
43 error(get_string("expired", "scorm", userdate($scorm->timeclose)));
44 }
45 }
46
9528568b 47 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
48
03f5a0f8 49 if (!empty($scoid)) {
50 //
51 // Direct SCO request
52 //
b3659259 53 if ($sco = scorm_get_sco($scoid)) {
03f5a0f8 54 if ($sco->launch == '') {
55 // Search for the next launchable sco
5b4b959b 56 if ($scoes = $DB->get_records_select('scorm_scoes',"scorm = ? AND launch <> ? AND id > ?",array($scorm->id, $DB->sql_empty(), $sco->id), 'id ASC')) {
03f5a0f8 57 $sco = current($scoes);
58 }
59 }
60 }
61 }
62 //
63 // If no sco was found get the first of SCORM package
64 //
65 if (!isset($sco)) {
5b4b959b 66 $scoes = $DB->get_records_select('scorm_scoes',"scorm = ? AND launch <> ?", array($scorm->id, $DB->sql_empty()),'id ASC');
03f5a0f8 67 $sco = current($scoes);
68 }
69
3c6c4eaf 70 if ($sco->scormtype == 'asset') {
71 $attempt = scorm_get_last_attempt($scorm->id,$USER->id);
72 $element = $scorm->version == 'scorm_13'?'cmi.completion_status':'cmi.core.lesson_status';
73 $value = 'completed';
74 $result = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
75 }
9528568b 76
03f5a0f8 77 //
78 // Forge SCO URL
79 //
80 $connector = '';
81 $version = substr($scorm->version,0,4);
b3659259 82 if ((isset($sco->parameters) && (!empty($sco->parameters))) || ($version == 'AICC')) {
03f5a0f8 83 if (stripos($sco->launch,'?') !== false) {
84 $connector = '&';
85 } else {
86 $connector = '?';
87 }
b3659259 88 if ((isset($sco->parameters) && (!empty($sco->parameters))) && ($sco->parameters[0] == '?')) {
03f5a0f8 89 $sco->parameters = substr($sco->parameters,1);
90 }
91 }
9528568b 92
03f5a0f8 93 if ($version == 'AICC') {
b3659259 94 if (isset($sco->parameters) && (!empty($sco->parameters))) {
03f5a0f8 95 $sco->parameters = '&'. $sco->parameters;
96 }
3c6c4eaf 97 $launcher = $sco->launch.$connector.'aicc_sid='.sesskey().'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$sco->parameters;
03f5a0f8 98 } else {
b3659259 99 if (isset($sco->parameters) && (!empty($sco->parameters))) {
100 $launcher = $sco->launch.$connector.$sco->parameters;
101 } else {
102 $launcher = $sco->launch;
103 }
03f5a0f8 104 }
9528568b 105
03f5a0f8 106 if (scorm_external_link($sco->launch)) {
9528568b 107 //TODO: does this happen?
03f5a0f8 108 $result = $launcher;
9528568b 109
110 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
111 // Remote learning activity
112 $result = dirname($scorm->reference).'/'.$launcher;
113
114 } else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY) {
8949f8df 115 // Repository
9528568b 116 $result = $CFG->repositorywebroot.substr($scorm->reference, 1).'/'.$sco->launch;
117
118 } else if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
119 //note: do not convert this to use get_file_url()!
120 // SCORM does not work without slasharguments anyway and there might be some extra ?xx=yy params
121 // see MDL-16060
122 $result = "$CFG->wwwroot/pluginfile.php/$context->id/scorm_content/$scorm->revision/$launcher";
03f5a0f8 123 }
9528568b 124
e4f781c9 125 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
03f5a0f8 126?>
127<html>
128 <head>
129 <title>LoadSCO</title>
f4ba7e1a 130 <script type="text/javascript">
131 //<![CDATA[
e4f781c9 132 function doredirect() {
133 var e = document.getElementById("countdown");
134 var cSeconds = parseInt(e.innerHTML);
135 var timer = setInterval(function() {
136 if( cSeconds ) {
137 e.innerHTML = --cSeconds;
138 } else {
139 clearInterval(timer);
320f3aab 140 document.body.innerHTML = "<?php echo get_string('activitypleasewait', 'scorm');?>";
e4f781c9 141 location = "<?php echo $result ?>";
142 }
143 }, 1000);
144 }
9528568b 145 //]]>
03f5a0f8 146 </script>
147 <noscript>
86ed86b8 148 <meta http-equiv="refresh" content="0;url=<?php echo $result ?>" />
9528568b 149 </noscript>
03f5a0f8 150 </head>
e4f781c9 151 <body onload="doredirect();">
476c00cb 152 <p><?php echo get_string('activityloading', 'scorm');?> <span id="countdown"><?php echo $delayseconds ?></span> <?php echo get_string('numseconds');?>. &nbsp; <img src='<?php echo $scormpixdir;?>/wait.gif'><p>
1881df27 153 <?php if (scorm_debugging($scorm)) {
1eb6604e 154 add_to_log($course->id, 'scorm', 'launch', 'view.php?id='.$cm->id, $result, $cm->id);
155 }
156 ?>
9528568b 157 </body>
03f5a0f8 158</html>