MDL-41580 SCORM: allow imsmanifest.xml to be used in file system repos
[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
299aedfa
DM
52if (!isloggedin()) { // Prevent login page from being shown in iframe.
53 // Using simple html instead of exceptions here as shown inside iframe/object.
54 echo html_writer::start_tag('html');
55 echo html_writer::tag('head', '');
56 echo html_writer::tag('body', get_string('loggedinnot'));
57 echo html_writer::end_tag('html');
58 exit;
59}
60
61require_login($course, false, $cm, false); // Call require_login anyway to set up globals correctly.
f2c86f94 62
f7b5c6aa
DM
63//check if scorm closed
64$timenow = time();
65if ($scorm->timeclose !=0) {
66 if ($scorm->timeopen > $timenow) {
67 print_error('notopenyet', 'scorm', null, userdate($scorm->timeopen));
68 } else if ($timenow > $scorm->timeclose) {
69 print_error('expired', 'scorm', null, userdate($scorm->timeclose));
d54e2145 70 }
f7b5c6aa 71}
f2c86f94 72
a3fc4b3a 73$context = context_module::instance($cm->id);
9528568b 74
f7b5c6aa 75if (!empty($scoid)) {
03f5a0f8 76 //
77 // Direct SCO request
78 //
f7b5c6aa
DM
79 if ($sco = scorm_get_sco($scoid)) {
80 if ($sco->launch == '') {
81 // Search for the next launchable sco
4704e6f8
MS
82 if ($scoes = $DB->get_records_select(
83 'scorm_scoes',
84 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true).' AND id > ?',
85 array($scorm->id, $sco->id),
b33e4665 86 'sortorder, id')) {
f7b5c6aa 87 $sco = current($scoes);
03f5a0f8 88 }
89 }
90 }
f7b5c6aa
DM
91}
92//
93// If no sco was found get the first of SCORM package
94//
95if (!isset($sco)) {
4704e6f8
MS
96 $scoes = $DB->get_records_select(
97 'scorm_scoes',
98 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true),
99 array($scorm->id),
b33e4665 100 'sortorder, id'
4704e6f8 101 );
f7b5c6aa
DM
102 $sco = current($scoes);
103}
104
105if ($sco->scormtype == 'asset') {
106 $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
e6402b54 107 $element = (scorm_version_check($scorm->version, SCORM_13)) ? 'cmi.completion_status':'cmi.core.lesson_status';
f7b5c6aa
DM
108 $value = 'completed';
109 $result = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
110}
111
112//
113// Forge SCO URL
114//
115$connector = '';
116$version = substr($scorm->version, 0, 4);
117if ((isset($sco->parameters) && (!empty($sco->parameters))) || ($version == 'AICC')) {
118 if (stripos($sco->launch, '?') !== false) {
119 $connector = '&';
120 } else {
121 $connector = '?';
03f5a0f8 122 }
f7b5c6aa
DM
123 if ((isset($sco->parameters) && (!empty($sco->parameters))) && ($sco->parameters[0] == '?')) {
124 $sco->parameters = substr($sco->parameters, 1);
3c6c4eaf 125 }
f7b5c6aa 126}
9528568b 127
f7b5c6aa 128if ($version == 'AICC') {
ba0e91a2
DM
129 require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
130 $aicc_sid = scorm_aicc_get_hacp_session($scorm->id);
131 if (empty($aicc_sid)) {
132 $aicc_sid = sesskey();
133 }
f7b5c6aa
DM
134 $sco_params = '';
135 if (isset($sco->parameters) && (!empty($sco->parameters))) {
136 $sco_params = '&'. $sco->parameters;
03f5a0f8 137 }
ba0e91a2 138 $launcher = $sco->launch.$connector.'aicc_sid='.$aicc_sid.'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$sco_params;
f7b5c6aa
DM
139} else {
140 if (isset($sco->parameters) && (!empty($sco->parameters))) {
141 $launcher = $sco->launch.$connector.$sco->parameters;
03f5a0f8 142 } else {
f7b5c6aa 143 $launcher = $sco->launch;
03f5a0f8 144 }
f7b5c6aa
DM
145}
146
147if (scorm_external_link($sco->launch)) {
148 //TODO: does this happen?
149 $result = $launcher;
150} else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
361a47d4 151 // Remote learning activity.
f7b5c6aa 152 $result = dirname($scorm->reference).'/'.$launcher;
361a47d4
DM
153} else if ($scorm->scormtype === SCORM_TYPE_LOCAL && strtolower($scorm->reference) == 'imsmanifest.xml') {
154 // This SCORM content sits in a repository that allows relative links.
155 $result = "$CFG->wwwroot/pluginfile.php/$context->id/mod_scorm/imsmanifest/$scorm->revision/$launcher";
f7b5c6aa 156} else if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
361a47d4
DM
157 // Note: do not convert this to use get_file_url() or moodle_url()
158 // SCORM does not work without slasharguments and moodle_url() encodes querystring vars.
f7b5c6aa
DM
159 $result = "$CFG->wwwroot/pluginfile.php/$context->id/mod_scorm/content/$scorm->revision/$launcher";
160}
161
62169a67
DM
162add_to_log($course->id, 'scorm', 'launch', 'view.php?id='.$cm->id, $result, $cm->id);
163
c39896e8
DP
164header('Content-Type: text/html; charset=UTF-8');
165
5ecd13bc
MS
166if ($sco->scormtype == 'asset') {
167 // HTTP 302 Found => Moved Temporarily.
168 header('Location: ' . $result);
169 // Provide a short feedback in case of slow network connection.
170 echo '<html><body><p>' . get_string('activitypleasewait', 'scorm'). '</p></body></html>';
171 exit;
172}
173
174// We expect a SCO: select which API are we looking for.
175$LMS_api = (scorm_version_check($scorm->version, SCORM_12) || empty($scorm->version)) ? 'API' : 'API_1484_11';
176
03f5a0f8 177?>
178<html>
179 <head>
180 <title>LoadSCO</title>
f4ba7e1a 181 <script type="text/javascript">
182 //<![CDATA[
485f4ce6
PH
183 var myApiHandle = null;
184 var myFindAPITries = 0;
f2c86f94 185
485f4ce6
PH
186 function myGetAPIHandle() {
187 myFindAPITries = 0;
188 if (myApiHandle == null) {
189 myApiHandle = myGetAPI();
f2c86f94 190 }
485f4ce6 191 return myApiHandle;
f2c86f94 192 }
193
485f4ce6 194 function myFindAPI(win) {
46e58e80 195 while ((win.<?php echo $LMS_api; ?> == null) && (win.parent != null) && (win.parent != win)) {
485f4ce6 196 myFindAPITries++;
f2c86f94 197 // Note: 7 is an arbitrary number, but should be more than sufficient
485f4ce6 198 if (myFindAPITries > 7) {
f2c86f94 199 return null;
200 }
201 win = win.parent;
202 }
46e58e80 203 return win.<?php echo $LMS_api; ?>;
f2c86f94 204 }
205
206 // hun for the API - needs to be loaded before we can launch the package
485f4ce6
PH
207 function myGetAPI() {
208 var theAPI = myFindAPI(window);
f2c86f94 209 if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
485f4ce6 210 theAPI = myFindAPI(window.opener);
f2c86f94 211 }
212 if (theAPI == null) {
213 return null;
214 }
215 return theAPI;
216 }
217
218 function doredirect() {
485f4ce6 219 if (myGetAPIHandle() != null) {
f2c86f94 220 location = "<?php echo $result ?>";
221 }
222 else {
485f4ce6 223 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 224 var e = document.getElementById("countdown");
225 var cSeconds = parseInt(e.innerHTML);
226 var timer = setInterval(function() {
485f4ce6 227 if( cSeconds && myGetAPIHandle() == null ) {
f2c86f94 228 e.innerHTML = --cSeconds;
229 } else {
230 clearInterval(timer);
231 document.body.innerHTML = "<p><?php echo get_string('activitypleasewait', 'scorm');?></p>";
232 location = "<?php echo $result ?>";
233 }
234 }, 1000);
235 }
e4f781c9 236 }
9528568b 237 //]]>
03f5a0f8 238 </script>
239 <noscript>
86ed86b8 240 <meta http-equiv="refresh" content="0;url=<?php echo $result ?>" />
9528568b 241 </noscript>
03f5a0f8 242 </head>
e4f781c9 243 <body onload="doredirect();">
f2c86f94 244 <p><?php echo get_string('activitypleasewait', 'scorm');?></p>
9528568b 245 </body>
60e40dda 246</html>