weekly release 2.2dev
[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
f7b5c6aa 52require_login($course->id, 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
f7b5c6aa 64$context = get_context_instance(CONTEXT_MODULE, $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
DM
110if ($version == 'AICC') {
111 $sco_params = '';
112 if (isset($sco->parameters) && (!empty($sco->parameters))) {
113 $sco_params = '&'. $sco->parameters;
03f5a0f8 114 }
f7b5c6aa
DM
115 $launcher = $sco->launch.$connector.'aicc_sid='.sesskey().'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$sco_params;
116} else {
117 if (isset($sco->parameters) && (!empty($sco->parameters))) {
118 $launcher = $sco->launch.$connector.$sco->parameters;
03f5a0f8 119 } else {
f7b5c6aa 120 $launcher = $sco->launch;
03f5a0f8 121 }
f7b5c6aa
DM
122}
123
124if (scorm_external_link($sco->launch)) {
125 //TODO: does this happen?
126 $result = $launcher;
127} else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
128 // Remote learning activity
129 $result = dirname($scorm->reference).'/'.$launcher;
130} else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY) {
131 // Repository
132 $result = $CFG->repositorywebroot.substr($scorm->reference, 1).'/'.$sco->launch;
133} else if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
134 //note: do not convert this to use get_file_url() or moodle_url()
135 //SCORM does not work without slasharguments and moodle_url() encodes querystring vars
136 $result = "$CFG->wwwroot/pluginfile.php/$context->id/mod_scorm/content/$scorm->revision/$launcher";
137}
138
139// which API are we looking for
e6402b54 140$LMS_api = (scorm_version_check($scorm->version, SCORM_12) || empty($scorm->version)) ? 'API' : 'API_1484_11';
03f5a0f8 141?>
142<html>
143 <head>
144 <title>LoadSCO</title>
f4ba7e1a 145 <script type="text/javascript">
146 //<![CDATA[
485f4ce6
PH
147 var myApiHandle = null;
148 var myFindAPITries = 0;
f2c86f94 149
485f4ce6
PH
150 function myGetAPIHandle() {
151 myFindAPITries = 0;
152 if (myApiHandle == null) {
153 myApiHandle = myGetAPI();
f2c86f94 154 }
485f4ce6 155 return myApiHandle;
f2c86f94 156 }
157
485f4ce6 158 function myFindAPI(win) {
46e58e80 159 while ((win.<?php echo $LMS_api; ?> == null) && (win.parent != null) && (win.parent != win)) {
485f4ce6 160 myFindAPITries++;
f2c86f94 161 // Note: 7 is an arbitrary number, but should be more than sufficient
485f4ce6 162 if (myFindAPITries > 7) {
f2c86f94 163 return null;
164 }
165 win = win.parent;
166 }
46e58e80 167 return win.<?php echo $LMS_api; ?>;
f2c86f94 168 }
169
170 // hun for the API - needs to be loaded before we can launch the package
485f4ce6
PH
171 function myGetAPI() {
172 var theAPI = myFindAPI(window);
f2c86f94 173 if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
485f4ce6 174 theAPI = myFindAPI(window.opener);
f2c86f94 175 }
176 if (theAPI == null) {
177 return null;
178 }
179 return theAPI;
180 }
181
182 function doredirect() {
485f4ce6 183 if (myGetAPIHandle() != null) {
f2c86f94 184 location = "<?php echo $result ?>";
185 }
186 else {
485f4ce6 187 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 188 var e = document.getElementById("countdown");
189 var cSeconds = parseInt(e.innerHTML);
190 var timer = setInterval(function() {
485f4ce6 191 if( cSeconds && myGetAPIHandle() == null ) {
f2c86f94 192 e.innerHTML = --cSeconds;
193 } else {
194 clearInterval(timer);
195 document.body.innerHTML = "<p><?php echo get_string('activitypleasewait', 'scorm');?></p>";
196 location = "<?php echo $result ?>";
197 }
198 }, 1000);
199 }
e4f781c9 200 }
9528568b 201 //]]>
03f5a0f8 202 </script>
203 <noscript>
86ed86b8 204 <meta http-equiv="refresh" content="0;url=<?php echo $result ?>" />
9528568b 205 </noscript>
03f5a0f8 206 </head>
e4f781c9 207 <body onload="doredirect();">
f2c86f94 208 <p><?php echo get_string('activitypleasewait', 'scorm');?></p>
f7b5c6aa 209 <?php if (debugging('', DEBUG_DEVELOPER)) {
1eb6604e 210 add_to_log($course->id, 'scorm', 'launch', 'view.php?id='.$cm->id, $result, $cm->id);
211 }
212 ?>
9528568b 213 </body>
60e40dda 214</html>