SCORM MDL-28111 tidy up formatting of code
[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
19 require_once('../../config.php');
86996ffe 20 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
134c8644
DM
21 require_once($CFG->libdir . '/completionlib.php');
22
f9d5371b 23 //
24 // Checkin' script parameters
25 //
3d00d6ab 26 $id = optional_param('cm', '', PARAM_INT); // Course Module ID, or
f9d5371b 27 $a = optional_param('a', '', PARAM_INT); // scorm ID
28 $scoid = required_param('scoid', PARAM_INT); // sco ID
29 $mode = optional_param('mode', 'normal', PARAM_ALPHA); // navigation mode
30 $currentorg = optional_param('currentorg', '', PARAM_RAW); // selected organization
31 $newattempt = optional_param('newattempt', 'off', PARAM_ALPHA); // the user request to start a new attempt
e73457e4 32
15d0fae4 33 //IE 6 Bug workaround
d1e3b5c2
DM
34 if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false) {
35 @ini_set('zlib.output_compression', 'Off');
36 @apache_setenv('no-gzip', 1);
15d0fae4
DM
37 }
38
f9d5371b 39 if (!empty($id)) {
40 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
08b56f93 41 print_error('invalidcoursemodule');
f9d5371b 42 }
bf347041 43 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
08b56f93 44 print_error('coursemisconf');
f9d5371b 45 }
bf347041 46 if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
08b56f93 47 print_error('invalidcoursemodule');
f9d5371b 48 }
49 } else if (!empty($a)) {
bf347041 50 if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
08b56f93 51 print_error('invalidcoursemodule');
f9d5371b 52 }
bf347041 53 if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
08b56f93 54 print_error('coursemisconf');
f9d5371b 55 }
56 if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
08b56f93 57 print_error('invalidcoursemodule');
f9d5371b 58 }
59 } else {
08b56f93 60 print_error('missingparameter');
f9d5371b 61 }
62
3d00d6ab 63 $url = new moodle_url('/mod/scorm/player.php', array('scoid'=>$scoid, 'cm'=>$cm->id));
5c508e3f 64 if ($mode !== 'normal') {
65 $url->param('mode', $mode);
66 }
67 if ($currentorg !== '') {
68 $url->param('currentorg', $currentorg);
69 }
70 if ($newattempt !== 'off') {
71 $url->param('newattempt', $newattempt);
72 }
73 $PAGE->set_url($url);
30bce58e
DM
74 $forcejs = get_config('scorm','forcejavascript');
75 if (!empty($forcejs)) {
76 $PAGE->add_body_class('forcejavascript');
77 }
5c508e3f 78
f9d5371b 79 require_login($course->id, false, $cm);
80
f9d5371b 81 $strscorms = get_string('modulenameplural', 'scorm');
82 $strscorm = get_string('modulename', 'scorm');
83 $strpopup = get_string('popup','scorm');
bb8f1433 84 $strexit = get_string('exitactivity','scorm');
f9d5371b 85
f9d5371b 86 $pagetitle = strip_tags("$course->shortname: ".format_string($scorm->name));
52869011 87 $PAGE->set_title($pagetitle);
88 $PAGE->set_heading($course->fullname);
43f4e520 89
52869011 90 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE,$course->id))) {
91 echo $OUTPUT->header();
f9d5371b 92 notice(get_string("activityiscurrentlyhidden"));
670bc3e7 93 echo $OUTPUT->footer();
d54e2145 94 die;
f9d5371b 95 }
d54e2145 96
97 //check if scorm closed
98 $timenow = time();
99 if ($scorm->timeclose !=0) {
100 if ($scorm->timeopen > $timenow) {
52869011 101 echo $OUTPUT->header();
6aff538a 102 echo $OUTPUT->box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "generalbox boxaligncenter");
670bc3e7 103 echo $OUTPUT->footer();
d54e2145 104 die;
105 } elseif ($timenow > $scorm->timeclose) {
52869011 106 echo $OUTPUT->header();
6aff538a 107 echo $OUTPUT->box(get_string("expired", "scorm", userdate($scorm->timeclose)), "generalbox boxaligncenter");
670bc3e7 108 echo $OUTPUT->footer();
d54e2145 109 die;
110 }
111 }
112
f9d5371b 113 //
114 // TOC processing
115 //
2b3447c3 116 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
dbe7e6f6 117 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
faf01ee4 118 $scorm->version = 'scorm_12';
119 }
2b3447c3 120 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
121 $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
f9d5371b 122 if (($newattempt=='on') && (($attempt < $scorm->maxattempt) || ($scorm->maxattempt == 0))) {
123 $attempt++;
a30b6819 124 $mode = 'normal';
f9d5371b 125 }
126 $attemptstr = '&amp;attempt=' . $attempt;
127
334d8b94 128 $result = scorm_get_toc($USER, $scorm, $cm->id, TOCJSLINK, $currentorg, $scoid, $mode, $attempt, true, true);
f9d5371b 129 $sco = $result->sco;
130
131 if (($mode == 'browse') && ($scorm->hidebrowse == 1)) {
132 $mode = 'normal';
133 }
134 if ($mode != 'browse') {
f9d5371b 135 if ($trackdata = scorm_get_tracks($sco->id,$USER->id,$attempt)) {
136 if (($trackdata->status == 'completed') || ($trackdata->status == 'passed') || ($trackdata->status == 'failed')) {
137 $mode = 'review';
f9d5371b 138 } else {
139 $mode = 'normal';
f9d5371b 140 }
57d52eeb 141 } else {
142 $mode = 'normal';
f9d5371b 143 }
144 }
145
3d00d6ab 146 add_to_log($course->id, 'scorm', 'view', "player.php?cm=$cm->id&scoid=$sco->id", "$scorm->id", $cm->id);
43f4e520 147
f9d5371b 148
149 $scoidstr = '&amp;scoid='.$sco->id;
150 $scoidpop = '&scoid='.$sco->id;
151 $modestr = '&amp;mode='.$mode;
152 if ($mode == 'browse') {
153 $modepop = '&mode='.$mode;
154 } else {
155 $modepop = '';
156 }
157 $orgstr = '&currentorg='.$currentorg;
158
159 $SESSION->scorm_scoid = $sco->id;
160 $SESSION->scorm_status = 'Not Initialized';
161 $SESSION->scorm_mode = $mode;
2b3447c3 162 $SESSION->scorm_attempt = $attempt;
f9d5371b 163
6553cda7 164 // Mark module viewed
165 $completion = new completion_info($course);
166 $completion->set_module_viewed($cm);
167
f9d5371b 168 //
169 // Print the page header
170 //
171 $bodyscript = '';
172 if ($scorm->popup == 1) {
173 $bodyscript = 'onunload="main.close();"';
174 }
4e204f89 175
bb8f1433 176 $exitlink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'" title="'.$strexit.'">'.$strexit.'</a> ';
43f4e520 177
0a1f8f8f 178 $PAGE->set_button($exitlink);
bd257031 179
d1cf56e7
PH
180 $PAGE->requires->data_for_js('scormplayerdata', Array('cwidth'=>$scorm->width,'cheight'=>$scorm->height,
181 'popupoptions' => $scorm->options), true);
60409fe1
PS
182 $PAGE->requires->js('/mod/scorm/request.js', true);
183 $PAGE->requires->js('/lib/cookies.js', true);
5c2aa157
DM
184 //$PAGE->requires->js('/mod/scorm/loaddatamodel.php?id='.$cm->id.$scoidstr.$modestr.$attemptstr, true);
185 $PAGE->requires->css('/mod/scorm/styles.css');
52869011 186
187 echo $OUTPUT->header();
bd257031 188
5c2aa157
DM
189 // NEW IMS TOC
190 $PAGE->requires->string_for_js('navigation', 'scorm');
191 $PAGE->requires->string_for_js('toc', 'scorm');
192 $PAGE->requires->string_for_js('hide', 'moodle');
193 $PAGE->requires->string_for_js('show', 'moodle');
fe3b9da3 194 $PAGE->requires->string_for_js('popupsblocked', 'scorm');
5c2aa157
DM
195
196 $name = false;
197
f9d5371b 198?>
199 <div id="scormpage">
5c2aa157
DM
200
201 <div id="tocbox">
202 <div id='scormapi-parent'>
203 <script id="external-scormapi" type="text/JavaScript"></script>
204 </div>
205 <div id="scormtop">
43f4e520 206<?php
5c2aa157
DM
207 if ($result->prerequisites) {
208 if ($scorm->popup != 0) {
5d21c260 209 //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.
5c2aa157 210 $linkcourse = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'">' . get_string('finishscormlinkname','scorm') . '</a>';
31845fcd 211 echo $OUTPUT->box(get_string('finishscorm','scorm',$linkcourse), 'generalbox', 'altfinishlink');
9438ede6 212 }
f9d5371b 213 }
214?>
52a9a9b5 215 <?php echo $mode == 'browse' ? '<div id="scormmode" class="scorm-left">'.get_string('browsemode','scorm')."</div>\n" : ''; ?>
216 <?php echo $mode == 'review' ? '<div id="scormmode" class="scorm-left">'.get_string('reviewmode','scorm')."</div>\n" : ''; ?>
5c2aa157 217 <div id="scormnav" class="scorm-right">
f9d5371b 218<?php
5c2aa157
DM
219 if ($scorm->hidetoc == 2) {
220 echo $result->tocmenu;
43f4e520 221 }
f9d5371b 222?>
5c2aa157
DM
223 </div> <!-- Scormnav -->
224 </div> <!-- Scormtop -->
225 <div id="toctree" class="generalbox">
226 <?php echo $result->toc; ?>
227 </div> <!-- toctree -->
228 </div> <!-- tocbox -->
f9d5371b 229 <noscript>
230 <div id="noscript">
231 <?php print_string('noscriptnoscorm','scorm'); // No Martin(i), No Party ;-) ?>
232
233 </div>
234 </noscript>
235<?php
236 if ($result->prerequisites) {
22a79674 237 if ($scorm->popup != 0) {
61fa8a09 238 // Clean the name for the window as IE is fussy
6dbcacee 239 $name = preg_replace("/[^A-Za-z0-9]/", "", $scorm->name);
61fa8a09 240 if (!$name) {
241 $name = 'DefaultPlayerWindow';
242 }
243 $name = 'scorm_'.$name;
f9d5371b 244
6c772bcb
PS
245 echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
246 echo html_writer::script(js_writer::function_call('scorm_openpopup', Array("loadSCO.php?id=".$cm->id.$scoidpop, $name, $scorm->options, $scorm->width, $scorm->height)));
f3665549 247 ?>
485f4ce6 248 <noscript>
ac2032dc 249 <!--[if IE]>
ec678a9a 250 <iframe id="main" class="scoframe" name="main" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></iframe>
ac2032dc
DM
251 <![endif]-->
252 <!--[if !IE]>
ec678a9a 253 <object id="main" class="scoframe" type="text/html" data="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></object>
ac2032dc 254 <![endif]-->
485f4ce6 255 </noscript>
43f4e520 256<?php
f9d5371b 257 }
258 } else {
5c508e3f 259 echo $OUTPUT->box(get_string('noprerequisites','scorm'));
f9d5371b 260 }
261?>
5b89dfbb 262 </div> <!-- SCORM page -->
b3611e0d 263<?php
5c2aa157
DM
264// NEW IMS TOC
265if (!isset($result->toctitle)) {
266 $result->toctitle = get_string('toc', 'scorm');
267}
268
fe3b9da3 269$PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->hidenav, $scorm->hidetoc, $result->toctitle, $name, $sco->id));
5c2aa157 270
30bce58e
DM
271if (!empty($forcejs)) {
272 echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
273}
b3611e0d 274echo $OUTPUT->footer();