NOBUG: Whitespace fixes
[moodle.git] / mod / scorm / player.php
CommitLineData
f7b5c6aa
DM
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.
13//
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/>.
f9d5371b 16
17/// This page prints a particular instance of aicc/scorm package
18
ca4eda13
DM
19require_once('../../config.php');
20require_once($CFG->dirroot.'/mod/scorm/locallib.php');
21require_once($CFG->libdir . '/completionlib.php');
134c8644 22
ca4eda13
DM
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
6d954dd3 29$displaymode = optional_param('display','',PARAM_ALPHA);
e73457e4 30
ca4eda13
DM
31//IE 6 Bug workaround
32if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false) {
33 @ini_set('zlib.output_compression', 'Off');
34 @apache_setenv('no-gzip', 1);
35}
15d0fae4 36
799be368
EL
37// IE 9 workaround for Flash bug: MDL-29213
38// Note that it's not clear if appending the meta tag via $CFG->additionalhtmlhead
39// is correct at all, both because of the mechanism itself and because MS says
40// the tag must be used *before* including other stuff. See the issue for more info.
41// TODO: Once we implement some way to inject meta tags, change this to use it. MDL-30039
a5ba44f9
DM
42if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 9') !== false) {
43 if (!isset($CFG->additionalhtmlhead)) { //check to make sure set first - that way we can use .=
44 $CFG->additionalhtmlhead = '';
45 }
46 $CFG->additionalhtmlhead .= '<meta http-equiv="X-UA-Compatible" content="IE=8" />';
47}
48
ca4eda13
DM
49if (!empty($id)) {
50 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
51 print_error('invalidcoursemodule');
f9d5371b 52 }
ca4eda13
DM
53 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
54 print_error('coursemisconf');
55 }
56 if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
57 print_error('invalidcoursemodule');
5c508e3f 58 }
ca4eda13
DM
59} else if (!empty($a)) {
60 if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
61 print_error('invalidcoursemodule');
5c508e3f 62 }
ca4eda13
DM
63 if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
64 print_error('coursemisconf');
5c508e3f 65 }
ca4eda13
DM
66 if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
67 print_error('invalidcoursemodule');
30bce58e 68 }
ca4eda13
DM
69} else {
70 print_error('missingparameter');
71}
72
73$url = new moodle_url('/mod/scorm/player.php', array('scoid'=>$scoid, 'cm'=>$cm->id));
74if ($mode !== 'normal') {
75 $url->param('mode', $mode);
76}
77if ($currentorg !== '') {
78 $url->param('currentorg', $currentorg);
79}
80if ($newattempt !== 'off') {
81 $url->param('newattempt', $newattempt);
82}
83$PAGE->set_url($url);
84$forcejs = get_config('scorm', 'forcejavascript');
85if (!empty($forcejs)) {
86 $PAGE->add_body_class('forcejavascript');
87}
5c508e3f 88
ca4eda13 89require_login($course->id, false, $cm);
f9d5371b 90
ca4eda13
DM
91$strscorms = get_string('modulenameplural', 'scorm');
92$strscorm = get_string('modulename', 'scorm');
93$strpopup = get_string('popup', 'scorm');
94$strexit = get_string('exitactivity', 'scorm');
f9d5371b 95
8ebbb06a 96$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
ca4eda13 97
6d954dd3
DM
98if ($displaymode == 'popup') {
99 $PAGE->set_pagelayout('popup');
100} else {
8ebbb06a
SH
101 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
102 $pagetitle = strip_tags("$shortname: ".format_string($scorm->name));
6d954dd3
DM
103 $PAGE->set_title($pagetitle);
104 $PAGE->set_heading($course->fullname);
105}
8ebbb06a 106if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
ca4eda13
DM
107 echo $OUTPUT->header();
108 notice(get_string("activityiscurrentlyhidden"));
109 echo $OUTPUT->footer();
110 die;
111}
43f4e520 112
ca4eda13
DM
113//check if scorm closed
114$timenow = time();
115if ($scorm->timeclose !=0) {
116 if ($scorm->timeopen > $timenow) {
52869011 117 echo $OUTPUT->header();
ca4eda13 118 echo $OUTPUT->box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "generalbox boxaligncenter");
670bc3e7 119 echo $OUTPUT->footer();
d54e2145 120 die;
ca4eda13
DM
121 } else if ($timenow > $scorm->timeclose) {
122 echo $OUTPUT->header();
123 echo $OUTPUT->box(get_string("expired", "scorm", userdate($scorm->timeclose)), "generalbox boxaligncenter");
124 echo $OUTPUT->footer();
d54e2145 125
ca4eda13 126 die;
f9d5371b 127 }
ca4eda13
DM
128}
129// TOC processing
130$scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
131if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
132 $scorm->version = 'scorm_12';
133}
134require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
135$attempt = scorm_get_last_attempt($scorm->id, $USER->id);
136if (($newattempt=='on') && (($attempt < $scorm->maxattempt) || ($scorm->maxattempt == 0))) {
137 $attempt++;
138 $mode = 'normal';
139}
140$attemptstr = '&amp;attempt=' . $attempt;
f9d5371b 141
ca4eda13
DM
142$result = scorm_get_toc($USER, $scorm, $cm->id, TOCJSLINK, $currentorg, $scoid, $mode, $attempt, true, true);
143$sco = $result->sco;
f9d5371b 144
ca4eda13
DM
145if (($mode == 'browse') && ($scorm->hidebrowse == 1)) {
146 $mode = 'normal';
147}
148if ($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';
57d52eeb 152 } else {
153 $mode = 'normal';
f9d5371b 154 }
ca4eda13
DM
155 } else {
156 $mode = 'normal';
f9d5371b 157 }
ca4eda13 158}
f9d5371b 159
ca4eda13 160add_to_log($course->id, 'scorm', 'view', "player.php?cm=$cm->id&scoid=$sco->id", "$scorm->id", $cm->id);
43f4e520 161
f9d5371b 162
ca4eda13
DM
163$scoidstr = '&amp;scoid='.$sco->id;
164$scoidpop = '&scoid='.$sco->id;
165$modestr = '&amp;mode='.$mode;
166if ($mode == 'browse') {
167 $modepop = '&mode='.$mode;
168} else {
169 $modepop = '';
170}
171$orgstr = '&currentorg='.$currentorg;
f9d5371b 172
ba0e91a2
DM
173$SESSION->scorm->scoid = $sco->id;
174$SESSION->scorm->scormstatus = 'Not Initialized';
175$SESSION->scorm->scormmode = $mode;
176$SESSION->scorm->attempt = $attempt;
f9d5371b 177
ca4eda13
DM
178// Mark module viewed
179$completion = new completion_info($course);
180$completion->set_module_viewed($cm);
6553cda7 181
ca4eda13 182// Print the page header
6d954dd3
DM
183if (empty($scorm->popup) || $displaymode=='popup') {
184 $exitlink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'" title="'.$strexit.'">'.$strexit.'</a> ';
185 $PAGE->set_button($exitlink);
ca4eda13 186}
4e204f89 187
3b71ce6a 188$PAGE->requires->yui2_lib('connection');
ca4eda13
DM
189$PAGE->requires->data_for_js('scormplayerdata', Array('cwidth'=>$scorm->width,
190 'cheight'=>$scorm->height,
191 'popupoptions' => $scorm->options), true);
192$PAGE->requires->js('/mod/scorm/request.js', true);
193$PAGE->requires->js('/lib/cookies.js', true);
194$PAGE->requires->css('/mod/scorm/styles.css');
ca4eda13 195echo $OUTPUT->header();
bd257031 196
ca4eda13
DM
197// NEW IMS TOC
198$PAGE->requires->string_for_js('navigation', 'scorm');
199$PAGE->requires->string_for_js('toc', 'scorm');
200$PAGE->requires->string_for_js('hide', 'moodle');
201$PAGE->requires->string_for_js('show', 'moodle');
202$PAGE->requires->string_for_js('popupsblocked', 'scorm');
5c2aa157 203
ca4eda13 204$name = false;
5c2aa157 205
f9d5371b 206?>
207 <div id="scormpage">
b5b99e0b 208
5c2aa157
DM
209 <div id="tocbox">
210 <div id='scormapi-parent'>
211 <script id="external-scormapi" type="text/JavaScript"></script>
212 </div>
f9d5371b 213<?php
61c347cc
DM
214if ($scorm->hidetoc == SCORM_TOC_POPUP or $mode=='browse' or $mode=='review') {
215 echo '<div id="scormtop">';
216 echo $mode == 'browse' ? '<div id="scormmode" class="scorm-left">'.get_string('browsemode', 'scorm')."</div>\n" : '';
217 echo $mode == 'review' ? '<div id="scormmode" class="scorm-left">'.get_string('reviewmode', 'scorm')."</div>\n" : '';
218 if ($scorm->hidetoc == SCORM_TOC_POPUP) {
219 echo '<div id="scormnav" class="scorm-right">'.$result->tocmenu.'</div>';
220 }
221 echo '</div>';
ca4eda13 222}
f9d5371b 223?>
61c347cc 224 <div id="toctree">
6d954dd3
DM
225 <?php
226 if (empty($scorm->popup) || $displaymode == 'popup') {
227 echo $result->toc;
228 } else {
229 //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.
230 $linkcourse = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'">' . get_string('finishscormlinkname', 'scorm') . '</a>';
231 echo $OUTPUT->box(get_string('finishscorm', 'scorm', $linkcourse), 'generalbox', 'altfinishlink');
232 }?>
5c2aa157
DM
233 </div> <!-- toctree -->
234 </div> <!-- tocbox -->
f9d5371b 235 <noscript>
236 <div id="noscript">
ca4eda13 237 <?php print_string('noscriptnoscorm', 'scorm'); // No Martin(i), No Party ;-) ?>
f9d5371b 238
239 </div>
240 </noscript>
241<?php
ca4eda13 242if ($result->prerequisites) {
6d954dd3 243 if ($scorm->popup != 0 && $displaymode !=='popup') {
ca4eda13
DM
244 // Clean the name for the window as IE is fussy
245 $name = preg_replace("/[^A-Za-z0-9]/", "", $scorm->name);
246 if (!$name) {
247 $name = 'DefaultPlayerWindow';
248 }
249 $name = 'scorm_'.$name;
250 echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
6d954dd3 251 $url = new moodle_url($PAGE->url, array('scoid' => $sco->id, 'display' => 'popup'));
ca4eda13 252 echo html_writer::script(
6d954dd3 253 js_writer::function_call('scorm_openpopup', Array($url->out(false),
ca4eda13
DM
254 $name, $scorm->options,
255 $scorm->width, $scorm->height)));
256 ?>
485f4ce6 257 <noscript>
ac2032dc 258 <!--[if IE]>
ec678a9a 259 <iframe id="main" class="scoframe" name="main" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></iframe>
ac2032dc
DM
260 <![endif]-->
261 <!--[if !IE]>
ec678a9a 262 <object id="main" class="scoframe" type="text/html" data="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></object>
ac2032dc 263 <![endif]-->
485f4ce6 264 </noscript>
ca4eda13 265 <?php
f9d5371b 266 }
ca4eda13
DM
267} else {
268 echo $OUTPUT->box(get_string('noprerequisites', 'scorm'));
269}
f9d5371b 270?>
5b89dfbb 271 </div> <!-- SCORM page -->
b5b99e0b 272<?php
5c2aa157 273// NEW IMS TOC
6d954dd3
DM
274if (empty($scorm->popup) || $displaymode == 'popup') {
275 if (!isset($result->toctitle)) {
276 $result->toctitle = get_string('toc', 'scorm');
277 }
278 $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->hidenav, $scorm->hidetoc, $result->toctitle, $name, $sco->id));
5c2aa157 279}
30bce58e
DM
280if (!empty($forcejs)) {
281 echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
282}
a5ba44f9 283echo $OUTPUT->footer();