weekly release 2.4dev
[moodle.git] / mod / scorm / loadSCO.php
CommitLineData
5c508e3f 1<?php
f7b5c6aa
DM
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17require_once('../../config.php');
18require_once($CFG->dirroot.'/mod/scorm/locallib.php');
19
20$id = optional_param('id', '', PARAM_INT); // Course Module ID, or
21$a = optional_param('a', '', PARAM_INT); // scorm ID
22$scoid = required_param('scoid', PARAM_INT); // sco ID
23
24$delayseconds = 2; // Delay time before sco launch, used to give time to browser to define API
25
26if (!empty($id)) {
27 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
28 print_error('invalidcoursemodule');
29 }
30 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
31 print_error('coursemisconf');
32 }
33 if (! $scorm = $DB->get_record('scorm', array('id'=>$cm->instance))) {
34 print_error('invalidcoursemodule');
35 }
36} else if (!empty($a)) {
37 if (! $scorm = $DB->get_record('scorm', array('id'=>$a))) {
38 print_error('coursemisconf');
03f5a0f8 39 }
f7b5c6aa
DM
40 if (! $course = $DB->get_record('course', array('id'=>$scorm->course))) {
41 print_error('coursemisconf');
42 }
43 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
44 print_error('invalidcoursemodule');
45 }
46} else {
47 print_error('missingparameter');
48}
03f5a0f8 49
f7b5c6aa 50$PAGE->set_url('/mod/scorm/loadSCO.php', array('scoid'=>$scoid, 'id'=>$cm->id));
5c508e3f 51
cdbea7ee 52require_login($course, false, $cm);
f2c86f94 53
f7b5c6aa
DM
54//check if scorm closed
55$timenow = time();
56if ($scorm->timeclose !=0) {
57 if ($scorm->timeopen > $timenow) {
58 print_error('notopenyet', 'scorm', null, userdate($scorm->timeopen));
59 } else if ($timenow > $scorm->timeclose) {
60 print_error('expired', 'scorm', null, userdate($scorm->timeclose));
d54e2145 61 }
f7b5c6aa 62}
f2c86f94 63
a3fc4b3a 64$context = context_module::instance($cm->id);
9528568b 65
f7b5c6aa 66if (!empty($scoid)) {
03f5a0f8 67 //
68 // Direct SCO request
69 //
f7b5c6aa
DM
70 if ($sco = scorm_get_sco($scoid)) {
71 if ($sco->launch == '') {
72 // Search for the next launchable sco
73 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), 'id ASC')) {
74 $sco = current($scoes);
03f5a0f8 75 }
76 }
77 }
f7b5c6aa
DM
78}
79//
80// If no sco was found get the first of SCORM package
81//
82if (!isset($sco)) {
83 $scoes = $DB->get_records_select('scorm_scoes', "scorm = ? AND ".$DB->sql_isnotempty('scorm_scoes', 'launch', false, true), array($scorm->id), 'id ASC');
84 $sco = current($scoes);
85}
86
87if ($sco->scormtype == 'asset') {
88 $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
e6402b54 89 $element = (scorm_version_check($scorm->version, SCORM_13)) ? 'cmi.completion_status':'cmi.core.lesson_status';
f7b5c6aa
DM
90 $value = 'completed';
91 $result = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
92}
93
94//
95// Forge SCO URL
96//
97$connector = '';
98$version = substr($scorm->version, 0, 4);
99if ((isset($sco->parameters) && (!empty($sco->parameters))) || ($version == 'AICC')) {
100 if (stripos($sco->launch, '?') !== false) {
101 $connector = '&';
102 } else {
103 $connector = '?';
03f5a0f8 104 }
f7b5c6aa
DM
105 if ((isset($sco->parameters) && (!empty($sco->parameters))) && ($sco->parameters[0] == '?')) {
106 $sco->parameters = substr($sco->parameters, 1);
3c6c4eaf 107 }
f7b5c6aa 108}
9528568b 109
f7b5c6aa 110if ($version == 'AICC') {
ba0e91a2
DM
111 require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
112 $aicc_sid = scorm_aicc_get_hacp_session($scorm->id);
113 if (empty($aicc_sid)) {
114 $aicc_sid = sesskey();
115 }
f7b5c6aa
DM
116 $sco_params = '';
117 if (isset($sco->parameters) && (!empty($sco->parameters))) {
118 $sco_params = '&'. $sco->parameters;
03f5a0f8 119 }
ba0e91a2 120 $launcher = $sco->launch.$connector.'aicc_sid='.$aicc_sid.'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$sco_params;
f7b5c6aa
DM
121} else {
122 if (isset($sco->parameters) && (!empty($sco->parameters))) {
123 $launcher = $sco->launch.$connector.$sco->parameters;
03f5a0f8 124 } else {
f7b5c6aa 125 $launcher = $sco->launch;
03f5a0f8 126 }
f7b5c6aa
DM
127}
128
129if (scorm_external_link($sco->launch)) {
130 //TODO: does this happen?
131 $result = $launcher;
132} else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
133 // Remote learning activity
134 $result = dirname($scorm->reference).'/'.$launcher;
135} else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY) {
136 // Repository
137 $result = $CFG->repositorywebroot.substr($scorm->reference, 1).'/'.$sco->launch;
138} else if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
139 //note: do not convert this to use get_file_url() or moodle_url()
140 //SCORM does not work without slasharguments and moodle_url() encodes querystring vars
141 $result = "$CFG->wwwroot/pluginfile.php/$context->id/mod_scorm/content/$scorm->revision/$launcher";
142}
143
62169a67
DM
144add_to_log($course->id, 'scorm', 'launch', 'view.php?id='.$cm->id, $result, $cm->id);
145
f7b5c6aa 146// which API are we looking for
e6402b54 147$LMS_api = (scorm_version_check($scorm->version, SCORM_12) || empty($scorm->version)) ? 'API' : 'API_1484_11';
99302c5e
PS
148
149header('Content-Type: text/html; charset=UTF-8');
150
03f5a0f8 151?>
152<html>
153 <head>
154 <title>LoadSCO</title>
f4ba7e1a 155 <script type="text/javascript">
156 //<![CDATA[
485f4ce6
PH
157 var myApiHandle = null;
158 var myFindAPITries = 0;
f2c86f94 159
485f4ce6
PH
160 function myGetAPIHandle() {
161 myFindAPITries = 0;
162 if (myApiHandle == null) {
163 myApiHandle = myGetAPI();
f2c86f94 164 }
485f4ce6 165 return myApiHandle;
f2c86f94 166 }
167
485f4ce6 168 function myFindAPI(win) {
46e58e80 169 while ((win.<?php echo $LMS_api; ?> == null) && (win.parent != null) && (win.parent != win)) {
485f4ce6 170 myFindAPITries++;
f2c86f94 171 // Note: 7 is an arbitrary number, but should be more than sufficient
485f4ce6 172 if (myFindAPITries > 7) {
f2c86f94 173 return null;
174 }
175 win = win.parent;
176 }
46e58e80 177 return win.<?php echo $LMS_api; ?>;
f2c86f94 178 }
179
180 // hun for the API - needs to be loaded before we can launch the package
485f4ce6
PH
181 function myGetAPI() {
182 var theAPI = myFindAPI(window);
f2c86f94 183 if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
485f4ce6 184 theAPI = myFindAPI(window.opener);
f2c86f94 185 }
186 if (theAPI == null) {
187 return null;
188 }
189 return theAPI;
190 }
191
192 function doredirect() {
485f4ce6 193 if (myGetAPIHandle() != null) {
f2c86f94 194 location = "<?php echo $result ?>";
195 }
196 else {
485f4ce6 197 document.body.innerHTML = "<p><?php echo get_string('activityloading', 'scorm');?> <span id='countdown'><?php echo $delayseconds ?></span> <?php echo get_string('numseconds', 'moodle', '');?>. &nbsp; <img src='<?php echo $OUTPUT->pix_url('wait', 'scorm') ?>'><p>";
f2c86f94 198 var e = document.getElementById("countdown");
199 var cSeconds = parseInt(e.innerHTML);
200 var timer = setInterval(function() {
485f4ce6 201 if( cSeconds && myGetAPIHandle() == null ) {
f2c86f94 202 e.innerHTML = --cSeconds;
203 } else {
204 clearInterval(timer);
205 document.body.innerHTML = "<p><?php echo get_string('activitypleasewait', 'scorm');?></p>";
206 location = "<?php echo $result ?>";
207 }
208 }, 1000);
209 }
e4f781c9 210 }
9528568b 211 //]]>
03f5a0f8 212 </script>
213 <noscript>
86ed86b8 214 <meta http-equiv="refresh" content="0;url=<?php echo $result ?>" />
9528568b 215 </noscript>
03f5a0f8 216 </head>
e4f781c9 217 <body onload="doredirect();">
f2c86f94 218 <p><?php echo get_string('activitypleasewait', 'scorm');?></p>
9528568b 219 </body>
60e40dda 220</html>