MDL-37733 SCORM: Correct check for force new attempt
[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
39355b46 17// This page prints a particular instance of aicc/scorm package.
f9d5371b 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
39355b46
DM
28$newattempt = optional_param('newattempt', 'off', PARAM_ALPHA); // the user request to start a new attempt.
29$displaymode = optional_param('display', '', PARAM_ALPHA);
e73457e4 30
ca4eda13
DM
31if (!empty($id)) {
32 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
33 print_error('invalidcoursemodule');
f9d5371b 34 }
ca4eda13
DM
35 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
36 print_error('coursemisconf');
37 }
38 if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
39 print_error('invalidcoursemodule');
5c508e3f 40 }
ca4eda13
DM
41} else if (!empty($a)) {
42 if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
43 print_error('invalidcoursemodule');
5c508e3f 44 }
ca4eda13
DM
45 if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
46 print_error('coursemisconf');
5c508e3f 47 }
ca4eda13
DM
48 if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
49 print_error('invalidcoursemodule');
30bce58e 50 }
ca4eda13
DM
51} else {
52 print_error('missingparameter');
53}
39355b46
DM
54// If new attempt is being triggered set normal mode and increment attempt number.
55$attempt = scorm_get_last_attempt($scorm->id, $USER->id);
56
7f9759c2 57// Check mode is correct and set/validate mode/attempt/newattempt (uses pass by reference).
39355b46 58scorm_check_mode($scorm, $newattempt, $attempt, $USER->id, $mode);
ca4eda13
DM
59
60$url = new moodle_url('/mod/scorm/player.php', array('scoid'=>$scoid, 'cm'=>$cm->id));
61if ($mode !== 'normal') {
62 $url->param('mode', $mode);
63}
64if ($currentorg !== '') {
65 $url->param('currentorg', $currentorg);
66}
67if ($newattempt !== 'off') {
68 $url->param('newattempt', $newattempt);
69}
70$PAGE->set_url($url);
71$forcejs = get_config('scorm', 'forcejavascript');
72if (!empty($forcejs)) {
73 $PAGE->add_body_class('forcejavascript');
74}
df5c7086
MG
75$collapsetocwinsize = get_config('scorm', 'collapsetocwinsize');
76if (empty($collapsetocwinsize)) {
77 // Set as default window size to collapse TOC.
78 $collapsetocwinsize = 767;
79} else {
80 $collapsetocwinsize = intval($collapsetocwinsize);
81}
5c508e3f 82
cdbea7ee 83require_login($course, false, $cm);
f9d5371b 84
ca4eda13
DM
85$strscorms = get_string('modulenameplural', 'scorm');
86$strscorm = get_string('modulename', 'scorm');
87$strpopup = get_string('popup', 'scorm');
88$strexit = get_string('exitactivity', 'scorm');
f9d5371b 89
a3fc4b3a 90$coursecontext = context_course::instance($course->id);
ca4eda13 91
6d954dd3
DM
92if ($displaymode == 'popup') {
93 $PAGE->set_pagelayout('popup');
94} else {
8ebbb06a
SH
95 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
96 $pagetitle = strip_tags("$shortname: ".format_string($scorm->name));
6d954dd3
DM
97 $PAGE->set_title($pagetitle);
98 $PAGE->set_heading($course->fullname);
99}
8ebbb06a 100if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
ca4eda13
DM
101 echo $OUTPUT->header();
102 notice(get_string("activityiscurrentlyhidden"));
103 echo $OUTPUT->footer();
104 die;
105}
43f4e520 106
39355b46 107// Check if scorm closed.
ca4eda13
DM
108$timenow = time();
109if ($scorm->timeclose !=0) {
110 if ($scorm->timeopen > $timenow) {
52869011 111 echo $OUTPUT->header();
ca4eda13 112 echo $OUTPUT->box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "generalbox boxaligncenter");
670bc3e7 113 echo $OUTPUT->footer();
d54e2145 114 die;
ca4eda13
DM
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();
d54e2145 119
ca4eda13 120 die;
f9d5371b 121 }
ca4eda13
DM
122}
123// TOC processing
39355b46 124$scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe.
ca4eda13
DM
125if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
126 $scorm->version = 'scorm_12';
127}
128require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
f9d5371b 129
ca4eda13
DM
130$result = scorm_get_toc($USER, $scorm, $cm->id, TOCJSLINK, $currentorg, $scoid, $mode, $attempt, true, true);
131$sco = $result->sco;
cce0da74
DM
132if ($scorm->lastattemptlock == 1 && $result->attemptleft == 0) {
133 echo $OUTPUT->header();
134 echo $OUTPUT->notification(get_string('exceededmaxattempts', 'scorm'));
135 echo $OUTPUT->footer();
136 exit;
137}
f9d5371b 138
ca4eda13 139$scoidstr = '&amp;scoid='.$sco->id;
ca4eda13 140$modestr = '&amp;mode='.$mode;
f9d5371b 141
37ca1722 142$SESSION->scorm = new stdClass();
ba0e91a2
DM
143$SESSION->scorm->scoid = $sco->id;
144$SESSION->scorm->scormstatus = 'Not Initialized';
145$SESSION->scorm->scormmode = $mode;
146$SESSION->scorm->attempt = $attempt;
f9d5371b 147
39355b46 148// Mark module viewed.
ca4eda13
DM
149$completion = new completion_info($course);
150$completion->set_module_viewed($cm);
6553cda7 151
39355b46 152// Print the page header.
6d954dd3
DM
153if (empty($scorm->popup) || $displaymode=='popup') {
154 $exitlink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'" title="'.$strexit.'">'.$strexit.'</a> ';
155 $PAGE->set_button($exitlink);
ca4eda13 156}
4e204f89 157
81f460b1 158$PAGE->requires->data_for_js('scormplayerdata', Array('launch' => false,
35cfa9e6
PH
159 'currentorg' => '',
160 'sco' => 0,
161 'scorm' => 0,
162 'courseid' => $scorm->course,
163 'cwidth' => $scorm->width,
164 'cheight' => $scorm->height,
165 'popupoptions' => $scorm->options), true);
ca4eda13
DM
166$PAGE->requires->js('/mod/scorm/request.js', true);
167$PAGE->requires->js('/lib/cookies.js', true);
ca4eda13 168echo $OUTPUT->header();
b2970f96 169echo $OUTPUT->heading(format_string($scorm->name));
bd257031 170
ca4eda13
DM
171$PAGE->requires->string_for_js('navigation', 'scorm');
172$PAGE->requires->string_for_js('toc', 'scorm');
173$PAGE->requires->string_for_js('hide', 'moodle');
174$PAGE->requires->string_for_js('show', 'moodle');
175$PAGE->requires->string_for_js('popupsblocked', 'scorm');
5c2aa157 176
ca4eda13 177$name = false;
5c2aa157 178
f9d5371b 179?>
180 <div id="scormpage">
b5b99e0b 181
5c2aa157
DM
182 <div id="tocbox">
183 <div id='scormapi-parent'>
184 <script id="external-scormapi" type="text/JavaScript"></script>
185 </div>
f9d5371b 186<?php
61c347cc
DM
187if ($scorm->hidetoc == SCORM_TOC_POPUP or $mode=='browse' or $mode=='review') {
188 echo '<div id="scormtop">';
189 echo $mode == 'browse' ? '<div id="scormmode" class="scorm-left">'.get_string('browsemode', 'scorm')."</div>\n" : '';
190 echo $mode == 'review' ? '<div id="scormmode" class="scorm-left">'.get_string('reviewmode', 'scorm')."</div>\n" : '';
191 if ($scorm->hidetoc == SCORM_TOC_POPUP) {
192 echo '<div id="scormnav" class="scorm-right">'.$result->tocmenu.'</div>';
193 }
194 echo '</div>';
ca4eda13 195}
f9d5371b 196?>
61c347cc 197 <div id="toctree">
6d954dd3
DM
198 <?php
199 if (empty($scorm->popup) || $displaymode == 'popup') {
200 echo $result->toc;
201 } else {
202 //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.
203 $linkcourse = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'">' . get_string('finishscormlinkname', 'scorm') . '</a>';
204 echo $OUTPUT->box(get_string('finishscorm', 'scorm', $linkcourse), 'generalbox', 'altfinishlink');
205 }?>
5c2aa157
DM
206 </div> <!-- toctree -->
207 </div> <!-- tocbox -->
f9d5371b 208 <noscript>
209 <div id="noscript">
ca4eda13 210 <?php print_string('noscriptnoscorm', 'scorm'); // No Martin(i), No Party ;-) ?>
f9d5371b 211
212 </div>
213 </noscript>
214<?php
ca4eda13 215if ($result->prerequisites) {
6d954dd3 216 if ($scorm->popup != 0 && $displaymode !=='popup') {
ca4eda13
DM
217 // Clean the name for the window as IE is fussy
218 $name = preg_replace("/[^A-Za-z0-9]/", "", $scorm->name);
219 if (!$name) {
220 $name = 'DefaultPlayerWindow';
221 }
222 $name = 'scorm_'.$name;
223 echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
39355b46 224 $url = new moodle_url($PAGE->url, array('scoid' => $sco->id, 'display' => 'popup', 'mode' => $mode));
ca4eda13 225 echo html_writer::script(
6d954dd3 226 js_writer::function_call('scorm_openpopup', Array($url->out(false),
ca4eda13
DM
227 $name, $scorm->options,
228 $scorm->width, $scorm->height)));
229 ?>
485f4ce6 230 <noscript>
ec678a9a 231 <iframe id="main" class="scoframe" name="main" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></iframe>
485f4ce6 232 </noscript>
ca4eda13 233 <?php
f9d5371b 234 }
ca4eda13
DM
235} else {
236 echo $OUTPUT->box(get_string('noprerequisites', 'scorm'));
237}
f9d5371b 238?>
5b89dfbb 239 </div> <!-- SCORM page -->
b5b99e0b 240<?php
df5c7086 241$scoes = scorm_get_toc_object($USER, $scorm, $currentorg, $sco->id, $mode, $attempt);
41e205cd 242$adlnav = scorm_get_adlnav_json($scoes['scoes']);
39355b46 243
6d954dd3
DM
244if (empty($scorm->popup) || $displaymode == 'popup') {
245 if (!isset($result->toctitle)) {
246 $result->toctitle = get_string('toc', 'scorm');
247 }
c23ff8e3
DS
248 $jsmodule = array(
249 'name' => 'mod_scorm',
250 'fullpath' => '/mod/scorm/module.js',
251 'requires' => array('json'),
252 );
df5c7086
MG
253 $scorm->nav = intval($scorm->nav);
254 $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->nav, $scorm->navpositionleft, $scorm->navpositiontop, $scorm->hidetoc,
255 $collapsetocwinsize, $result->toctitle, $name, $sco->id, $adlnav), false, $jsmodule);
5c2aa157 256}
30bce58e
DM
257if (!empty($forcejs)) {
258 echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
259}
a5ba44f9 260echo $OUTPUT->footer();