MDL-24258 (updated): eliminate student capability to delete their post when it exceed...
[moodle.git] / mod / scorm / loadSCO.php
CommitLineData
5c508e3f 1<?php
b3659259 2 require_once('../../config.php');
86996ffe 3 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
03f5a0f8 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
a6855934 35 $PAGE->set_url('/mod/scorm/loadSCO.php', array('scoid'=>$scoid, 'id'=>$cm->id));
5c508e3f 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') {
5eaf56b8 96 $sco_params = '';
b3659259 97 if (isset($sco->parameters) && (!empty($sco->parameters))) {
5eaf56b8 98 $sco_params = '&'. $sco->parameters;
03f5a0f8 99 }
5eaf56b8 100 $launcher = $sco->launch.$connector.'aicc_sid='.sesskey().'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$sco_params;
03f5a0f8 101 } else {
b3659259 102 if (isset($sco->parameters) && (!empty($sco->parameters))) {
103 $launcher = $sco->launch.$connector.$sco->parameters;
104 } else {
105 $launcher = $sco->launch;
106 }
03f5a0f8 107 }
9528568b 108
03f5a0f8 109 if (scorm_external_link($sco->launch)) {
9528568b 110 //TODO: does this happen?
03f5a0f8 111 $result = $launcher;
9528568b 112
113 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
114 // Remote learning activity
115 $result = dirname($scorm->reference).'/'.$launcher;
116
117 } else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY) {
8949f8df 118 // Repository
9528568b 119 $result = $CFG->repositorywebroot.substr($scorm->reference, 1).'/'.$sco->launch;
120
121 } else if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
5cf85097
DM
122 //note: do not convert this to use get_file_url() or moodle_url()
123 //SCORM does not work without slasharguments and moodle_url() encodes querystring vars
38961293 124 $result = "$CFG->wwwroot/pluginfile.php/$context->id/mod_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[
485f4ce6
PH
135 var myApiHandle = null;
136 var myFindAPITries = 0;
f2c86f94 137
485f4ce6
PH
138 function myGetAPIHandle() {
139 myFindAPITries = 0;
140 if (myApiHandle == null) {
141 myApiHandle = myGetAPI();
f2c86f94 142 }
485f4ce6 143 return myApiHandle;
f2c86f94 144 }
145
485f4ce6 146 function myFindAPI(win) {
46e58e80 147 while ((win.<?php echo $LMS_api; ?> == null) && (win.parent != null) && (win.parent != win)) {
485f4ce6 148 myFindAPITries++;
f2c86f94 149 // Note: 7 is an arbitrary number, but should be more than sufficient
485f4ce6 150 if (myFindAPITries > 7) {
f2c86f94 151 return null;
152 }
153 win = win.parent;
154 }
46e58e80 155 return win.<?php echo $LMS_api; ?>;
f2c86f94 156 }
157
158 // hun for the API - needs to be loaded before we can launch the package
485f4ce6
PH
159 function myGetAPI() {
160 var theAPI = myFindAPI(window);
f2c86f94 161 if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
485f4ce6 162 theAPI = myFindAPI(window.opener);
f2c86f94 163 }
164 if (theAPI == null) {
165 return null;
166 }
167 return theAPI;
168 }
169
170 function doredirect() {
485f4ce6 171 if (myGetAPIHandle() != null) {
f2c86f94 172 location = "<?php echo $result ?>";
173 }
174 else {
485f4ce6 175 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 176 var e = document.getElementById("countdown");
177 var cSeconds = parseInt(e.innerHTML);
178 var timer = setInterval(function() {
485f4ce6 179 if( cSeconds && myGetAPIHandle() == null ) {
f2c86f94 180 e.innerHTML = --cSeconds;
181 } else {
182 clearInterval(timer);
183 document.body.innerHTML = "<p><?php echo get_string('activitypleasewait', 'scorm');?></p>";
184 location = "<?php echo $result ?>";
185 }
186 }, 1000);
187 }
e4f781c9 188 }
9528568b 189 //]]>
03f5a0f8 190 </script>
191 <noscript>
86ed86b8 192 <meta http-equiv="refresh" content="0;url=<?php echo $result ?>" />
9528568b 193 </noscript>
03f5a0f8 194 </head>
e4f781c9 195 <body onload="doredirect();">
f2c86f94 196 <p><?php echo get_string('activitypleasewait', 'scorm');?></p>
197 <?php if (debugging('',DEBUG_DEVELOPER)) {
1eb6604e 198 add_to_log($course->id, 'scorm', 'launch', 'view.php?id='.$cm->id, $result, $cm->id);
199 }
200 ?>
9528568b 201 </body>
60e40dda 202</html>