MDL-29956: Implement export of Quiz activity (r16977, r17032)
[moodle.git] / mod / scorm / loadSCO.php
blobd7988f576e802f987e170a3e1cc2e2271279674a
1 <?php
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.
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/>.
17 require_once('../../config.php');
18 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
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
24 $delayseconds = 2; // Delay time before sco launch, used to give time to browser to define API
26 if (!empty($id)) {
27 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
28 print_error('invalidcoursemodule');
30 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
31 print_error('coursemisconf');
33 if (! $scorm = $DB->get_record('scorm', array('id'=>$cm->instance))) {
34 print_error('invalidcoursemodule');
36 } else if (!empty($a)) {
37 if (! $scorm = $DB->get_record('scorm', array('id'=>$a))) {
38 print_error('coursemisconf');
40 if (! $course = $DB->get_record('course', array('id'=>$scorm->course))) {
41 print_error('coursemisconf');
43 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
44 print_error('invalidcoursemodule');
46 } else {
47 print_error('missingparameter');
50 $PAGE->set_url('/mod/scorm/loadSCO.php', array('scoid'=>$scoid, 'id'=>$cm->id));
52 require_login($course->id, false, $cm);
54 //check if scorm closed
55 $timenow = time();
56 if ($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));
64 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
66 if (!empty($scoid)) {
68 // Direct SCO request
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);
80 // If no sco was found get the first of SCORM package
82 if (!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);
87 if ($sco->scormtype == 'asset') {
88 $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
89 $element = (scorm_version_check($scorm->version, SCORM_13)) ? 'cmi.completion_status':'cmi.core.lesson_status';
90 $value = 'completed';
91 $result = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
95 // Forge SCO URL
97 $connector = '';
98 $version = substr($scorm->version, 0, 4);
99 if ((isset($sco->parameters) && (!empty($sco->parameters))) || ($version == 'AICC')) {
100 if (stripos($sco->launch, '?') !== false) {
101 $connector = '&';
102 } else {
103 $connector = '?';
105 if ((isset($sco->parameters) && (!empty($sco->parameters))) && ($sco->parameters[0] == '?')) {
106 $sco->parameters = substr($sco->parameters, 1);
110 if ($version == 'AICC') {
111 require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
112 $aicc_sid = scorm_aicc_get_hacp_session($scorm->id);
113 if (empty($aicc_sid)) {
114 $aicc_sid = sesskey();
116 $sco_params = '';
117 if (isset($sco->parameters) && (!empty($sco->parameters))) {
118 $sco_params = '&'. $sco->parameters;
120 $launcher = $sco->launch.$connector.'aicc_sid='.$aicc_sid.'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$sco_params;
121 } else {
122 if (isset($sco->parameters) && (!empty($sco->parameters))) {
123 $launcher = $sco->launch.$connector.$sco->parameters;
124 } else {
125 $launcher = $sco->launch;
129 if (scorm_external_link($sco->launch)) {
130 //TODO: does this happen?
131 $result = $launcher;
132 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
133 // Remote learning activity
134 $result = dirname($scorm->reference).'/'.$launcher;
135 } else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY) {
136 // Repository
137 $result = $CFG->repositorywebroot.substr($scorm->reference, 1).'/'.$sco->launch;
138 } else if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
139 //note: do not convert this to use get_file_url() or moodle_url()
140 //SCORM does not work without slasharguments and moodle_url() encodes querystring vars
141 $result = "$CFG->wwwroot/pluginfile.php/$context->id/mod_scorm/content/$scorm->revision/$launcher";
144 // which API are we looking for
145 $LMS_api = (scorm_version_check($scorm->version, SCORM_12) || empty($scorm->version)) ? 'API' : 'API_1484_11';
147 <html>
148 <head>
149 <title>LoadSCO</title>
150 <script type="text/javascript">
151 //<![CDATA[
152 var myApiHandle = null;
153 var myFindAPITries = 0;
155 function myGetAPIHandle() {
156 myFindAPITries = 0;
157 if (myApiHandle == null) {
158 myApiHandle = myGetAPI();
160 return myApiHandle;
163 function myFindAPI(win) {
164 while ((win.<?php echo $LMS_api; ?> == null) && (win.parent != null) && (win.parent != win)) {
165 myFindAPITries++;
166 // Note: 7 is an arbitrary number, but should be more than sufficient
167 if (myFindAPITries > 7) {
168 return null;
170 win = win.parent;
172 return win.<?php echo $LMS_api; ?>;
175 // hun for the API - needs to be loaded before we can launch the package
176 function myGetAPI() {
177 var theAPI = myFindAPI(window);
178 if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined")) {
179 theAPI = myFindAPI(window.opener);
181 if (theAPI == null) {
182 return null;
184 return theAPI;
187 function doredirect() {
188 if (myGetAPIHandle() != null) {
189 location = "<?php echo $result ?>";
191 else {
192 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>";
193 var e = document.getElementById("countdown");
194 var cSeconds = parseInt(e.innerHTML);
195 var timer = setInterval(function() {
196 if( cSeconds && myGetAPIHandle() == null ) {
197 e.innerHTML = --cSeconds;
198 } else {
199 clearInterval(timer);
200 document.body.innerHTML = "<p><?php echo get_string('activitypleasewait', 'scorm');?></p>";
201 location = "<?php echo $result ?>";
203 }, 1000);
206 //]]>
207 </script>
208 <noscript>
209 <meta http-equiv="refresh" content="0;url=<?php echo $result ?>" />
210 </noscript>
211 </head>
212 <body onload="doredirect();">
213 <p><?php echo get_string('activitypleasewait', 'scorm');?></p>
214 <?php if (debugging('', DEBUG_DEVELOPER)) {
215 add_to_log($course->id, 'scorm', 'launch', 'view.php?id='.$cm->id, $result, $cm->id);
218 </body>
219 </html>