Merge branch 'MDL-40604-master' of git://github.com/danpoltawski/moodle
[moodle.git] / mod / scorm / player.php
blob36dc9bc7fd74fcc4ac753ab8f514a0adca4f39c2
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 /// This page prints a particular instance of aicc/scorm package
19 require_once('../../config.php');
20 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
21 require_once($CFG->libdir . '/completionlib.php');
23 $id = optional_param('cm', '', PARAM_INT); // Course Module ID, or
24 $a = optional_param('a', '', PARAM_INT); // scorm ID
25 $scoid = required_param('scoid', PARAM_INT); // sco ID
26 $mode = optional_param('mode', 'normal', PARAM_ALPHA); // navigation mode
27 $currentorg = optional_param('currentorg', '', PARAM_RAW); // selected organization
28 $newattempt = optional_param('newattempt', 'off', PARAM_ALPHA); // the user request to start a new attempt
29 $displaymode = optional_param('display','',PARAM_ALPHA);
31 // IE 9 workaround for Flash bug: MDL-29213
32 // Note that it's not clear if appending the meta tag via $CFG->additionalhtmlhead
33 // is correct at all, both because of the mechanism itself and because MS says
34 // the tag must be used *before* including other stuff. See the issue for more info.
35 // TODO: Once we implement some way to inject meta tags, change this to use it. MDL-30039
36 if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 9') !== false) {
37 if (!isset($CFG->additionalhtmlhead)) { //check to make sure set first - that way we can use .=
38 $CFG->additionalhtmlhead = '';
40 $CFG->additionalhtmlhead .= '<meta http-equiv="X-UA-Compatible" content="IE=8" />';
43 if (!empty($id)) {
44 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
45 print_error('invalidcoursemodule');
47 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
48 print_error('coursemisconf');
50 if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
51 print_error('invalidcoursemodule');
53 } else if (!empty($a)) {
54 if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
55 print_error('invalidcoursemodule');
57 if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
58 print_error('coursemisconf');
60 if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
61 print_error('invalidcoursemodule');
63 } else {
64 print_error('missingparameter');
67 $url = new moodle_url('/mod/scorm/player.php', array('scoid'=>$scoid, 'cm'=>$cm->id));
68 if ($mode !== 'normal') {
69 $url->param('mode', $mode);
71 if ($currentorg !== '') {
72 $url->param('currentorg', $currentorg);
74 if ($newattempt !== 'off') {
75 $url->param('newattempt', $newattempt);
77 $PAGE->set_url($url);
78 $forcejs = get_config('scorm', 'forcejavascript');
79 if (!empty($forcejs)) {
80 $PAGE->add_body_class('forcejavascript');
83 require_login($course, false, $cm);
85 $strscorms = get_string('modulenameplural', 'scorm');
86 $strscorm = get_string('modulename', 'scorm');
87 $strpopup = get_string('popup', 'scorm');
88 $strexit = get_string('exitactivity', 'scorm');
90 $coursecontext = context_course::instance($course->id);
92 if ($displaymode == 'popup') {
93 $PAGE->set_pagelayout('popup');
94 } else {
95 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
96 $pagetitle = strip_tags("$shortname: ".format_string($scorm->name));
97 $PAGE->set_title($pagetitle);
98 $PAGE->set_heading($course->fullname);
100 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
101 echo $OUTPUT->header();
102 notice(get_string("activityiscurrentlyhidden"));
103 echo $OUTPUT->footer();
104 die;
107 //check if scorm closed
108 $timenow = time();
109 if ($scorm->timeclose !=0) {
110 if ($scorm->timeopen > $timenow) {
111 echo $OUTPUT->header();
112 echo $OUTPUT->box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "generalbox boxaligncenter");
113 echo $OUTPUT->footer();
114 die;
115 } else if ($timenow > $scorm->timeclose) {
116 echo $OUTPUT->header();
117 echo $OUTPUT->box(get_string("expired", "scorm", userdate($scorm->timeclose)), "generalbox boxaligncenter");
118 echo $OUTPUT->footer();
120 die;
123 // TOC processing
124 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
125 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
126 $scorm->version = 'scorm_12';
128 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
129 $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
130 if (($newattempt=='on') && (($attempt < $scorm->maxattempt) || ($scorm->maxattempt == 0))) {
131 $attempt++;
132 $mode = 'normal';
134 $attemptstr = '&amp;attempt=' . $attempt;
136 $result = scorm_get_toc($USER, $scorm, $cm->id, TOCJSLINK, $currentorg, $scoid, $mode, $attempt, true, true);
137 $sco = $result->sco;
138 if ($scorm->lastattemptlock == 1 && $result->attemptleft == 0) {
139 echo $OUTPUT->header();
140 echo $OUTPUT->notification(get_string('exceededmaxattempts', 'scorm'));
141 echo $OUTPUT->footer();
142 exit;
145 if (($mode == 'browse') && ($scorm->hidebrowse == 1)) {
146 $mode = 'normal';
148 if ($mode != 'browse') {
149 if ($trackdata = scorm_get_tracks($sco->id, $USER->id, $attempt)) {
150 if (($trackdata->status == 'completed') || ($trackdata->status == 'passed') || ($trackdata->status == 'failed')) {
151 $mode = 'review';
152 } else {
153 $mode = 'normal';
155 } else {
156 $mode = 'normal';
160 add_to_log($course->id, 'scorm', 'view', "player.php?cm=$cm->id&scoid=$sco->id", "$scorm->id", $cm->id);
163 $scoidstr = '&amp;scoid='.$sco->id;
164 $scoidpop = '&scoid='.$sco->id;
165 $modestr = '&amp;mode='.$mode;
166 if ($mode == 'browse') {
167 $modepop = '&mode='.$mode;
168 } else {
169 $modepop = '';
171 $orgstr = '&currentorg='.$currentorg;
173 $SESSION->scorm = new stdClass();
174 $SESSION->scorm->scoid = $sco->id;
175 $SESSION->scorm->scormstatus = 'Not Initialized';
176 $SESSION->scorm->scormmode = $mode;
177 $SESSION->scorm->attempt = $attempt;
179 // Mark module viewed
180 $completion = new completion_info($course);
181 $completion->set_module_viewed($cm);
183 // Print the page header
184 if (empty($scorm->popup) || $displaymode=='popup') {
185 $exitlink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'" title="'.$strexit.'">'.$strexit.'</a> ';
186 $PAGE->set_button($exitlink);
189 $PAGE->requires->data_for_js('scormplayerdata', Array('launch' => false,
190 'currentorg' => '',
191 'sco' => 0,
192 'scorm' => 0,
193 'courseid' => $scorm->course,
194 'cwidth' => $scorm->width,
195 'cheight' => $scorm->height,
196 'popupoptions' => $scorm->options), true);
197 $PAGE->requires->js('/mod/scorm/request.js', true);
198 $PAGE->requires->js('/lib/cookies.js', true);
199 echo $OUTPUT->header();
201 // NEW IMS TOC
202 $PAGE->requires->string_for_js('navigation', 'scorm');
203 $PAGE->requires->string_for_js('toc', 'scorm');
204 $PAGE->requires->string_for_js('hide', 'moodle');
205 $PAGE->requires->string_for_js('show', 'moodle');
206 $PAGE->requires->string_for_js('popupsblocked', 'scorm');
208 $name = false;
211 <div id="scormpage">
213 <div id="tocbox">
214 <div id='scormapi-parent'>
215 <script id="external-scormapi" type="text/JavaScript"></script>
216 </div>
217 <?php
218 if ($scorm->hidetoc == SCORM_TOC_POPUP or $mode=='browse' or $mode=='review') {
219 echo '<div id="scormtop">';
220 echo $mode == 'browse' ? '<div id="scormmode" class="scorm-left">'.get_string('browsemode', 'scorm')."</div>\n" : '';
221 echo $mode == 'review' ? '<div id="scormmode" class="scorm-left">'.get_string('reviewmode', 'scorm')."</div>\n" : '';
222 if ($scorm->hidetoc == SCORM_TOC_POPUP) {
223 echo '<div id="scormnav" class="scorm-right">'.$result->tocmenu.'</div>';
225 echo '</div>';
228 <div id="toctree">
229 <?php
230 if (empty($scorm->popup) || $displaymode == 'popup') {
231 echo $result->toc;
232 } else {
233 //Added incase javascript popups are blocked we don't provide a direct link to the pop-up as JS communication can fail - the user must disable their pop-up blocker.
234 $linkcourse = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'">' . get_string('finishscormlinkname', 'scorm') . '</a>';
235 echo $OUTPUT->box(get_string('finishscorm', 'scorm', $linkcourse), 'generalbox', 'altfinishlink');
237 </div> <!-- toctree -->
238 </div> <!-- tocbox -->
239 <noscript>
240 <div id="noscript">
241 <?php print_string('noscriptnoscorm', 'scorm'); // No Martin(i), No Party ;-) ?>
243 </div>
244 </noscript>
245 <?php
246 if ($result->prerequisites) {
247 if ($scorm->popup != 0 && $displaymode !=='popup') {
248 // Clean the name for the window as IE is fussy
249 $name = preg_replace("/[^A-Za-z0-9]/", "", $scorm->name);
250 if (!$name) {
251 $name = 'DefaultPlayerWindow';
253 $name = 'scorm_'.$name;
254 echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
255 $url = new moodle_url($PAGE->url, array('scoid' => $sco->id, 'display' => 'popup'));
256 echo html_writer::script(
257 js_writer::function_call('scorm_openpopup', Array($url->out(false),
258 $name, $scorm->options,
259 $scorm->width, $scorm->height)));
261 <noscript>
262 <!--[if IE]>
263 <iframe id="main" class="scoframe" name="main" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></iframe>
264 <![endif]-->
265 <!--[if !IE]>
266 <object id="main" class="scoframe" type="text/html" data="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></object>
267 <![endif]-->
268 </noscript>
269 <?php
271 } else {
272 echo $OUTPUT->box(get_string('noprerequisites', 'scorm'));
275 </div> <!-- SCORM page -->
276 <?php
277 $scoes = scorm_get_toc_object($USER, $scorm, "", $sco->id, $mode, $attempt);
278 $adlnav = scorm_get_adlnav_json($scoes['scoes']);
279 // NEW IMS TOC
280 if (empty($scorm->popup) || $displaymode == 'popup') {
281 if (!isset($result->toctitle)) {
282 $result->toctitle = get_string('toc', 'scorm');
284 $jsmodule = array(
285 'name' => 'mod_scorm',
286 'fullpath' => '/mod/scorm/module.js',
287 'requires' => array('json'),
289 $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->hidenav, $scorm->hidetoc, $result->toctitle, $name, $sco->id, $adlnav), false, $jsmodule);
291 if (!empty($forcejs)) {
292 echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
294 echo $OUTPUT->footer();