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
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
82 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')) {
83 $sco = current($scoes);
03f5a0f8 84 }
85 }
86 }
f7b5c6aa
DM
87}
88//
89// If no sco was found get the first of SCORM package
90//
91if (!isset($sco)) {
92 $scoes = $DB->get_records_select('scorm_scoes', "scorm = ? AND ".$DB->sql_isnotempty('scorm_scoes', 'launch', false, true), array($scorm->id), 'id ASC');
93 $sco = current($scoes);
94}
95
96if ($sco->scormtype == 'asset') {
97 $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
e6402b54 98 $element = (scorm_version_check($scorm->version, SCORM_13)) ? 'cmi.completion_status':'cmi.core.lesson_status';
f7b5c6aa
DM
99 $value = 'completed';
100 $result = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
101}
102
103//
104// Forge SCO URL
105//
106$connector = '';
107$version = substr($scorm->version, 0, 4);
108if ((isset($sco->parameters) && (!empty($sco->parameters))) || ($version == 'AICC')) {
109 if (stripos($sco->launch, '?') !== false) {
110 $connector = '&';
111 } else {
112 $connector = '?';
03f5a0f8 113 }
f7b5c6aa
DM
114 if ((isset($sco->parameters) && (!empty($sco->parameters))) && ($sco->parameters[0] == '?')) {
115 $sco->parameters = substr($sco->parameters, 1);
3c6c4eaf 116 }
f7b5c6aa 117}
9528568b 118
f7b5c6aa 119if ($version == 'AICC') {
ba0e91a2
DM
120 require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
121 $aicc_sid = scorm_aicc_get_hacp_session($scorm->id);
122 if (empty($aicc_sid)) {
123 $aicc_sid = sesskey();
124 }
f7b5c6aa
DM
125 $sco_params = '';
126 if (isset($sco->parameters) && (!empty($sco->parameters))) {
127 $sco_params = '&'. $sco->parameters;
03f5a0f8 128 }
ba0e91a2 129 $launcher = $sco->launch.$connector.'aicc_sid='.$aicc_sid.'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$sco_params;
f7b5c6aa
DM
130} else {
131 if (isset($sco->parameters) && (!empty($sco->parameters))) {
132 $launcher = $sco->launch.$connector.$sco->parameters;
03f5a0f8 133 } else {
f7b5c6aa 134 $launcher = $sco->launch;
03f5a0f8 135 }
f7b5c6aa
DM
136}
137
138if (scorm_external_link($sco->launch)) {
139 //TODO: does this happen?
140 $result = $launcher;
141} else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
142 // Remote learning activity
143 $result = dirname($scorm->reference).'/'.$launcher;
144} else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY) {
145 // Repository
146 $result = $CFG->repositorywebroot.substr($scorm->reference, 1).'/'.$sco->launch;
147} else if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
148 //note: do not convert this to use get_file_url() or moodle_url()
149 //SCORM does not work without slasharguments and moodle_url() encodes querystring vars
150 $result = "$CFG->wwwroot/pluginfile.php/$context->id/mod_scorm/content/$scorm->revision/$launcher";
151}
152
62169a67
DM
153add_to_log($course->id, 'scorm', 'launch', 'view.php?id='.$cm->id, $result, $cm->id);
154
f7b5c6aa 155// which API are we looking for
e6402b54 156$LMS_api = (scorm_version_check($scorm->version, SCORM_12) || empty($scorm->version)) ? 'API' : 'API_1484_11';
99302c5e
PS
157
158header('Content-Type: text/html; charset=UTF-8');
159
03f5a0f8 160?>
161<html>
162 <head>
163 <title>LoadSCO</title>
f4ba7e1a 164 <script type="text/javascript">
165 //<![CDATA[
485f4ce6
PH
166 var myApiHandle = null;
167 var myFindAPITries = 0;
f2c86f94 168
485f4ce6
PH
169 function myGetAPIHandle() {
170 myFindAPITries = 0;
171 if (myApiHandle == null) {
172 myApiHandle = myGetAPI();
f2c86f94 173 }
485f4ce6 174 return myApiHandle;
f2c86f94 175 }
176
485f4ce6 177 function myFindAPI(win) {
46e58e80 178 while ((win.<?php echo $LMS_api; ?> == null) && (win.parent != null) && (win.parent != win)) {
485f4ce6 179 myFindAPITries++;
f2c86f94 180 // Note: 7 is an arbitrary number, but should be more than sufficient
485f4ce6 181 if (myFindAPITries > 7) {
f2c86f94 182 return null;
183 }
184 win = win.parent;
185 }
46e58e80 186 return win.<?php echo $LMS_api; ?>;
f2c86f94 187 }
188
189 // hun for the API - needs to be loaded before we can launch the package
485f4ce6
PH
190 function myGetAPI() {
191 var theAPI = myFindAPI(window);
f2c86f94 192 if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
485f4ce6 193 theAPI = myFindAPI(window.opener);
f2c86f94 194 }
195 if (theAPI == null) {
196 return null;
197 }
198 return theAPI;
199 }
200
201 function doredirect() {
485f4ce6 202 if (myGetAPIHandle() != null) {
f2c86f94 203 location = "<?php echo $result ?>";
204 }
205 else {
485f4ce6 206 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 207 var e = document.getElementById("countdown");
208 var cSeconds = parseInt(e.innerHTML);
209 var timer = setInterval(function() {
485f4ce6 210 if( cSeconds && myGetAPIHandle() == null ) {
f2c86f94 211 e.innerHTML = --cSeconds;
212 } else {
213 clearInterval(timer);
214 document.body.innerHTML = "<p><?php echo get_string('activitypleasewait', 'scorm');?></p>";
215 location = "<?php echo $result ?>";
216 }
217 }, 1000);
218 }
e4f781c9 219 }
9528568b 220 //]]>
03f5a0f8 221 </script>
222 <noscript>
86ed86b8 223 <meta http-equiv="refresh" content="0;url=<?php echo $result ?>" />
9528568b 224 </noscript>
03f5a0f8 225 </head>
e4f781c9 226 <body onload="doredirect();">
f2c86f94 227 <p><?php echo get_string('activitypleasewait', 'scorm');?></p>
9528568b 228 </body>
60e40dda 229</html>