enrolment plugins: MDL-21337 Missing global $OUTPUT variable in some enrolment plugin...
[moodle.git] / mod / scorm / loadSCO.php
CommitLineData
5c508e3f 1<?php
b3659259 2 require_once('../../config.php');
03f5a0f8 3 require_once('locallib.php');
4
9528568b 5 $id = optional_param('id', '', PARAM_INT); // Course Module ID, or
6 $a = optional_param('a', '', PARAM_INT); // scorm ID
03f5a0f8 7 $scoid = required_param('scoid', PARAM_INT); // sco ID
8
8949f8df 9 $delayseconds = 2; // Delay time before sco launch, used to give time to browser to define API
10
03f5a0f8 11 if (!empty($id)) {
12 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
08b56f93 13 print_error('invalidcoursemodule');
03f5a0f8 14 }
bf347041 15 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
08b56f93 16 print_error('coursemisconf');
03f5a0f8 17 }
bf347041 18 if (! $scorm = $DB->get_record('scorm', array('id'=>$cm->instance))) {
08b56f93 19 print_error('invalidcoursemodule');
03f5a0f8 20 }
21 } else if (!empty($a)) {
bf347041 22 if (! $scorm = $DB->get_record('scorm', array('id'=>$a))) {
08b56f93 23 print_error('coursemisconf');
03f5a0f8 24 }
bf347041 25 if (! $course = $DB->get_record('course', array('id'=>$scorm->course))) {
08b56f93 26 print_error('coursemisconf');
03f5a0f8 27 }
b3659259 28 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
08b56f93 29 print_error('invalidcoursemodule');
03f5a0f8 30 }
31 } else {
08b56f93 32 print_error('missingparameter');
03f5a0f8 33 }
34
5c508e3f 35 $PAGE->set_url(new moodle_url($CFG->wwwroot.'/mod/scorm/loadSCO.php', array('scoid'=>$scoid, 'id'=>$cm->id)));
36
03f5a0f8 37 require_login($course->id, false, $cm);
f2c86f94 38
d54e2145 39 //check if scorm closed
40 $timenow = time();
41 if ($scorm->timeclose !=0) {
42 if ($scorm->timeopen > $timenow) {
60e40dda 43 print_error('notopenyet', 'scorm', null, userdate($scorm->timeopen));
d54e2145 44 } else if ($timenow > $scorm->timeclose) {
60e40dda 45 print_error('expired', 'scorm', null, userdate($scorm->timeclose));
d54e2145 46 }
47 }
f2c86f94 48
9528568b 49 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
50
03f5a0f8 51 if (!empty($scoid)) {
52 //
53 // Direct SCO request
54 //
b3659259 55 if ($sco = scorm_get_sco($scoid)) {
03f5a0f8 56 if ($sco->launch == '') {
57 // Search for the next launchable sco
5b4b959b 58 if ($scoes = $DB->get_records_select('scorm_scoes',"scorm = ? AND launch <> ? AND id > ?",array($scorm->id, $DB->sql_empty(), $sco->id), 'id ASC')) {
03f5a0f8 59 $sco = current($scoes);
60 }
61 }
62 }
63 }
64 //
65 // If no sco was found get the first of SCORM package
66 //
67 if (!isset($sco)) {
5b4b959b 68 $scoes = $DB->get_records_select('scorm_scoes',"scorm = ? AND launch <> ?", array($scorm->id, $DB->sql_empty()),'id ASC');
03f5a0f8 69 $sco = current($scoes);
70 }
71
3c6c4eaf 72 if ($sco->scormtype == 'asset') {
73 $attempt = scorm_get_last_attempt($scorm->id,$USER->id);
f2c86f94 74 $element = ($scorm->version == 'scorm_13' || $scorm->version == 'SCORM_1.3') ? 'cmi.completion_status':'cmi.core.lesson_status';
3c6c4eaf 75 $value = 'completed';
76 $result = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
77 }
9528568b 78
03f5a0f8 79 //
80 // Forge SCO URL
81 //
82 $connector = '';
83 $version = substr($scorm->version,0,4);
b3659259 84 if ((isset($sco->parameters) && (!empty($sco->parameters))) || ($version == 'AICC')) {
03f5a0f8 85 if (stripos($sco->launch,'?') !== false) {
86 $connector = '&';
87 } else {
88 $connector = '?';
89 }
b3659259 90 if ((isset($sco->parameters) && (!empty($sco->parameters))) && ($sco->parameters[0] == '?')) {
03f5a0f8 91 $sco->parameters = substr($sco->parameters,1);
92 }
93 }
9528568b 94
03f5a0f8 95 if ($version == 'AICC') {
b3659259 96 if (isset($sco->parameters) && (!empty($sco->parameters))) {
03f5a0f8 97 $sco->parameters = '&'. $sco->parameters;
98 }
3c6c4eaf 99 $launcher = $sco->launch.$connector.'aicc_sid='.sesskey().'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$sco->parameters;
03f5a0f8 100 } else {
b3659259 101 if (isset($sco->parameters) && (!empty($sco->parameters))) {
102 $launcher = $sco->launch.$connector.$sco->parameters;
103 } else {
104 $launcher = $sco->launch;
105 }
03f5a0f8 106 }
9528568b 107
03f5a0f8 108 if (scorm_external_link($sco->launch)) {
9528568b 109 //TODO: does this happen?
03f5a0f8 110 $result = $launcher;
9528568b 111
112 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
113 // Remote learning activity
114 $result = dirname($scorm->reference).'/'.$launcher;
115
116 } else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY) {
8949f8df 117 // Repository
9528568b 118 $result = $CFG->repositorywebroot.substr($scorm->reference, 1).'/'.$sco->launch;
119
120 } else if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
121 //note: do not convert this to use get_file_url()!
122 // SCORM does not work without slasharguments anyway and there might be some extra ?xx=yy params
123 // see MDL-16060
124 $result = "$CFG->wwwroot/pluginfile.php/$context->id/scorm_content/$scorm->revision/$launcher";
03f5a0f8 125 }
9528568b 126
f2c86f94 127 // which API are we looking for
128 $LMS_api = ($scorm->version == 'scorm_12' || $scorm->version == 'SCORM_1.2' || empty($scorm->version)) ? 'API' : 'API_1484_11';
03f5a0f8 129?>
130<html>
131 <head>
132 <title>LoadSCO</title>
f4ba7e1a 133 <script type="text/javascript">
134 //<![CDATA[
f2c86f94 135 var apiHandle = null;
136 var findAPITries = 0;
137
138 function getAPIHandle() {
139 if (apiHandle == null) {
140 apiHandle = getAPI();
141 }
142 return apiHandle;
143 }
144
145 function findAPI(win) {
46e58e80 146 while ((win.<?php echo $LMS_api; ?> == null) && (win.parent != null) && (win.parent != win)) {
f2c86f94 147 findAPITries++;
148 // Note: 7 is an arbitrary number, but should be more than sufficient
149 if (findAPITries > 7) {
150 return null;
151 }
152 win = win.parent;
153 }
46e58e80 154 return win.<?php echo $LMS_api; ?>;
f2c86f94 155 }
156
157 // hun for the API - needs to be loaded before we can launch the package
158 function getAPI() {
159 var theAPI = findAPI(window);
160 if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
161 theAPI = findAPI(window.opener);
162 }
163 if (theAPI == null) {
164 return null;
165 }
166 return theAPI;
167 }
168
169 function doredirect() {
170 if (getAPI() != null) {
171 location = "<?php echo $result ?>";
172 }
173 else {
b5d0cafc 174 document.body.innerHTML = "<p><?php echo get_string('activityloading', 'scorm');?> <span id='countdown'><?php echo $delayseconds ?></span> <?php echo get_string('numseconds');?>. &nbsp; <img src='<?php echo $OUTPUT->pix_url('pix/wait', 'scorm') ?>'><p>";
f2c86f94 175 var e = document.getElementById("countdown");
176 var cSeconds = parseInt(e.innerHTML);
177 var timer = setInterval(function() {
178 if( cSeconds && getAPI() == null ) {
179 e.innerHTML = --cSeconds;
180 } else {
181 clearInterval(timer);
182 document.body.innerHTML = "<p><?php echo get_string('activitypleasewait', 'scorm');?></p>";
183 location = "<?php echo $result ?>";
184 }
185 }, 1000);
186 }
e4f781c9 187 }
9528568b 188 //]]>
03f5a0f8 189 </script>
190 <noscript>
86ed86b8 191 <meta http-equiv="refresh" content="0;url=<?php echo $result ?>" />
9528568b 192 </noscript>
03f5a0f8 193 </head>
e4f781c9 194 <body onload="doredirect();">
f2c86f94 195 <p><?php echo get_string('activitypleasewait', 'scorm');?></p>
196 <?php if (debugging('',DEBUG_DEVELOPER)) {
1eb6604e 197 add_to_log($course->id, 'scorm', 'launch', 'view.php?id='.$cm->id, $result, $cm->id);
198 }
199 ?>
9528568b 200 </body>
60e40dda 201</html>