Merge branch 'MDL-44822' of git://github.com/aolley/moodle
[moodle.git] / mod / scorm / loadSCO.php
blob41466a147b0c0ac368dcbf21e901fbca0516ac6f
1 <?php
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.
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/>.
17 require_once('../../config.php');
18 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
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
24 $delayseconds = 2; // Delay time before sco launch, used to give time to browser to define API
26 if (!empty($id)) {
27 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
28 print_error('invalidcoursemodule');
30 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
31 print_error('coursemisconf');
33 if (! $scorm = $DB->get_record('scorm', array('id'=>$cm->instance))) {
34 print_error('invalidcoursemodule');
36 } else if (!empty($a)) {
37 if (! $scorm = $DB->get_record('scorm', array('id'=>$a))) {
38 print_error('coursemisconf');
40 if (! $course = $DB->get_record('course', array('id'=>$scorm->course))) {
41 print_error('coursemisconf');
43 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
44 print_error('invalidcoursemodule');
46 } else {
47 print_error('missingparameter');
50 $PAGE->set_url('/mod/scorm/loadSCO.php', array('scoid'=>$scoid, 'id'=>$cm->id));
52 if (!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;
61 require_login($course, false, $cm, false); // Call require_login anyway to set up globals correctly.
63 //check if scorm closed
64 $timenow = time();
65 if ($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));
73 $context = context_module::instance($cm->id);
75 if (!empty($scoid)) {
77 // Direct SCO request
79 if ($sco = scorm_get_sco($scoid)) {
80 if ($sco->launch == '') {
81 // Search for the next launchable sco
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),
86 'sortorder, id')) {
87 $sco = current($scoes);
93 // If no sco was found get the first of SCORM package
95 if (!isset($sco)) {
96 $scoes = $DB->get_records_select(
97 'scorm_scoes',
98 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true),
99 array($scorm->id),
100 'sortorder, id'
102 $sco = current($scoes);
105 if ($sco->scormtype == 'asset') {
106 $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
107 $element = (scorm_version_check($scorm->version, SCORM_13)) ? 'cmi.completion_status':'cmi.core.lesson_status';
108 $value = 'completed';
109 $result = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
113 // Forge SCO URL
115 $connector = '';
116 $version = substr($scorm->version, 0, 4);
117 if ((isset($sco->parameters) && (!empty($sco->parameters))) || ($version == 'AICC')) {
118 if (stripos($sco->launch, '?') !== false) {
119 $connector = '&';
120 } else {
121 $connector = '?';
123 if ((isset($sco->parameters) && (!empty($sco->parameters))) && ($sco->parameters[0] == '?')) {
124 $sco->parameters = substr($sco->parameters, 1);
128 if ($version == 'AICC') {
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();
134 $sco_params = '';
135 if (isset($sco->parameters) && (!empty($sco->parameters))) {
136 $sco_params = '&'. $sco->parameters;
138 $launcher = $sco->launch.$connector.'aicc_sid='.$aicc_sid.'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$sco_params;
139 } else {
140 if (isset($sco->parameters) && (!empty($sco->parameters))) {
141 $launcher = $sco->launch.$connector.$sco->parameters;
142 } else {
143 $launcher = $sco->launch;
147 if (scorm_external_link($sco->launch)) {
148 //TODO: does this happen?
149 $result = $launcher;
150 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
151 // Remote learning activity.
152 $result = dirname($scorm->reference).'/'.$launcher;
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";
156 } else if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
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.
159 $result = "$CFG->wwwroot/pluginfile.php/$context->id/mod_scorm/content/$scorm->revision/$launcher";
162 // Trigger a Sco launched event.
163 $event = \mod_scorm\event\sco_launched::create(array(
164 'objectid' => $sco->id,
165 'context' => $context,
166 'other' => array('instanceid' => $scorm->id, 'loadedcontent' => $result)
168 $event->add_record_snapshot('course_modules', $cm);
169 $event->add_record_snapshot('scorm', $scorm);
170 $event->add_record_snapshot('scorm_scoes', $sco);
171 $event->trigger();
173 header('Content-Type: text/html; charset=UTF-8');
175 if ($sco->scormtype == 'asset') {
176 // HTTP 302 Found => Moved Temporarily.
177 header('Location: ' . $result);
178 // Provide a short feedback in case of slow network connection.
179 echo '<html><body><p>' . get_string('activitypleasewait', 'scorm'). '</p></body></html>';
180 exit;
183 // We expect a SCO: select which API are we looking for.
184 $LMS_api = (scorm_version_check($scorm->version, SCORM_12) || empty($scorm->version)) ? 'API' : 'API_1484_11';
187 <html>
188 <head>
189 <title>LoadSCO</title>
190 <script type="text/javascript">
191 //<![CDATA[
192 var myApiHandle = null;
193 var myFindAPITries = 0;
195 function myGetAPIHandle() {
196 myFindAPITries = 0;
197 if (myApiHandle == null) {
198 myApiHandle = myGetAPI();
200 return myApiHandle;
203 function myFindAPI(win) {
204 while ((win.<?php echo $LMS_api; ?> == null) && (win.parent != null) && (win.parent != win)) {
205 myFindAPITries++;
206 // Note: 7 is an arbitrary number, but should be more than sufficient
207 if (myFindAPITries > 7) {
208 return null;
210 win = win.parent;
212 return win.<?php echo $LMS_api; ?>;
215 // hun for the API - needs to be loaded before we can launch the package
216 function myGetAPI() {
217 var theAPI = myFindAPI(window);
218 if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
219 theAPI = myFindAPI(window.opener);
221 if (theAPI == null) {
222 return null;
224 return theAPI;
227 function doredirect() {
228 if (myGetAPIHandle() != null) {
229 location = "<?php echo $result ?>";
231 else {
232 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>";
233 var e = document.getElementById("countdown");
234 var cSeconds = parseInt(e.innerHTML);
235 var timer = setInterval(function() {
236 if( cSeconds && myGetAPIHandle() == null ) {
237 e.innerHTML = --cSeconds;
238 } else {
239 clearInterval(timer);
240 document.body.innerHTML = "<p><?php echo get_string('activitypleasewait', 'scorm');?></p>";
241 location = "<?php echo $result ?>";
243 }, 1000);
246 //]]>
247 </script>
248 <noscript>
249 <meta http-equiv="refresh" content="0;url=<?php echo $result ?>" />
250 </noscript>
251 </head>
252 <body onload="doredirect();">
253 <p><?php echo get_string('activitypleasewait', 'scorm');?></p>
254 </body>
255 </html>