MDL-17549 auth/radius: add CHAP and MSCHAP auth support, detect PHP extension
[moodle.git] / mod / scorm / loadSCO.php
CommitLineData
f4ba7e1a 1<?php // $Id$
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
35 require_login($course->id, false, $cm);
f2c86f94 36
d54e2145 37 //check if scorm closed
38 $timenow = time();
39 if ($scorm->timeclose !=0) {
40 if ($scorm->timeopen > $timenow) {
41 error(get_string("notopenyet", "scorm", userdate($scorm->timeopen)));
42 } else if ($timenow > $scorm->timeclose) {
43 error(get_string("expired", "scorm", userdate($scorm->timeclose)));
44 }
45 }
f2c86f94 46
9528568b 47 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
48
03f5a0f8 49 if (!empty($scoid)) {
50 //
51 // Direct SCO request
52 //
b3659259 53 if ($sco = scorm_get_sco($scoid)) {
03f5a0f8 54 if ($sco->launch == '') {
55 // Search for the next launchable sco
5b4b959b 56 if ($scoes = $DB->get_records_select('scorm_scoes',"scorm = ? AND launch <> ? AND id > ?",array($scorm->id, $DB->sql_empty(), $sco->id), 'id ASC')) {
03f5a0f8 57 $sco = current($scoes);
58 }
59 }
60 }
61 }
62 //
63 // If no sco was found get the first of SCORM package
64 //
65 if (!isset($sco)) {
5b4b959b 66 $scoes = $DB->get_records_select('scorm_scoes',"scorm = ? AND launch <> ?", array($scorm->id, $DB->sql_empty()),'id ASC');
03f5a0f8 67 $sco = current($scoes);
68 }
69
3c6c4eaf 70 if ($sco->scormtype == 'asset') {
71 $attempt = scorm_get_last_attempt($scorm->id,$USER->id);
f2c86f94 72 $element = ($scorm->version == 'scorm_13' || $scorm->version == 'SCORM_1.3') ? 'cmi.completion_status':'cmi.core.lesson_status';
3c6c4eaf 73 $value = 'completed';
74 $result = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
75 }
9528568b 76
03f5a0f8 77 //
78 // Forge SCO URL
79 //
80 $connector = '';
81 $version = substr($scorm->version,0,4);
b3659259 82 if ((isset($sco->parameters) && (!empty($sco->parameters))) || ($version == 'AICC')) {
03f5a0f8 83 if (stripos($sco->launch,'?') !== false) {
84 $connector = '&';
85 } else {
86 $connector = '?';
87 }
b3659259 88 if ((isset($sco->parameters) && (!empty($sco->parameters))) && ($sco->parameters[0] == '?')) {
03f5a0f8 89 $sco->parameters = substr($sco->parameters,1);
90 }
91 }
9528568b 92
03f5a0f8 93 if ($version == 'AICC') {
b3659259 94 if (isset($sco->parameters) && (!empty($sco->parameters))) {
03f5a0f8 95 $sco->parameters = '&'. $sco->parameters;
96 }
3c6c4eaf 97 $launcher = $sco->launch.$connector.'aicc_sid='.sesskey().'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$sco->parameters;
03f5a0f8 98 } else {
b3659259 99 if (isset($sco->parameters) && (!empty($sco->parameters))) {
100 $launcher = $sco->launch.$connector.$sco->parameters;
101 } else {
102 $launcher = $sco->launch;
103 }
03f5a0f8 104 }
9528568b 105
03f5a0f8 106 if (scorm_external_link($sco->launch)) {
9528568b 107 //TODO: does this happen?
03f5a0f8 108 $result = $launcher;
9528568b 109
110 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
111 // Remote learning activity
112 $result = dirname($scorm->reference).'/'.$launcher;
113
114 } else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY) {
8949f8df 115 // Repository
9528568b 116 $result = $CFG->repositorywebroot.substr($scorm->reference, 1).'/'.$sco->launch;
117
118 } else if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
119 //note: do not convert this to use get_file_url()!
120 // SCORM does not work without slasharguments anyway and there might be some extra ?xx=yy params
121 // see MDL-16060
122 $result = "$CFG->wwwroot/pluginfile.php/$context->id/scorm_content/$scorm->revision/$launcher";
03f5a0f8 123 }
9528568b 124
e4f781c9 125 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
f2c86f94 126
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 {
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 $scormpixdir;?>/wait.gif'><p>";
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>
f2c86f94 201</html>