MDL-37733 SCORM: Correct check for force new attempt
[moodle.git] / mod / scorm / player.php
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/>.
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 if (!empty($id)) {
32     if (! $cm = get_coursemodule_from_id('scorm', $id)) {
33         print_error('invalidcoursemodule');
34     }
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');
40     }
41 } else if (!empty($a)) {
42     if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
43         print_error('invalidcoursemodule');
44     }
45     if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
46         print_error('coursemisconf');
47     }
48     if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
49         print_error('invalidcoursemodule');
50     }
51 } else {
52     print_error('missingparameter');
53 }
54 // If new attempt is being triggered set normal mode and increment attempt number.
55 $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
57 // Check mode is correct and set/validate mode/attempt/newattempt (uses pass by reference).
58 scorm_check_mode($scorm, $newattempt, $attempt, $USER->id, $mode);
60 $url = new moodle_url('/mod/scorm/player.php', array('scoid'=>$scoid, 'cm'=>$cm->id));
61 if ($mode !== 'normal') {
62     $url->param('mode', $mode);
63 }
64 if ($currentorg !== '') {
65     $url->param('currentorg', $currentorg);
66 }
67 if ($newattempt !== 'off') {
68     $url->param('newattempt', $newattempt);
69 }
70 $PAGE->set_url($url);
71 $forcejs = get_config('scorm', 'forcejavascript');
72 if (!empty($forcejs)) {
73     $PAGE->add_body_class('forcejavascript');
74 }
75 $collapsetocwinsize = get_config('scorm', 'collapsetocwinsize');
76 if (empty($collapsetocwinsize)) {
77     // Set as default window size to collapse TOC.
78     $collapsetocwinsize = 767;
79 } else {
80     $collapsetocwinsize = intval($collapsetocwinsize);
81 }
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);
99 }
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;
121     }
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');
130 $result = scorm_get_toc($USER, $scorm, $cm->id, TOCJSLINK, $currentorg, $scoid, $mode, $attempt, true, true);
131 $sco = $result->sco;
132 if ($scorm->lastattemptlock == 1 && $result->attemptleft == 0) {
133     echo $OUTPUT->header();
134     echo $OUTPUT->notification(get_string('exceededmaxattempts', 'scorm'));
135     echo $OUTPUT->footer();
136     exit;
139 $scoidstr = '&amp;scoid='.$sco->id;
140 $modestr = '&amp;mode='.$mode;
142 $SESSION->scorm = new stdClass();
143 $SESSION->scorm->scoid = $sco->id;
144 $SESSION->scorm->scormstatus = 'Not Initialized';
145 $SESSION->scorm->scormmode = $mode;
146 $SESSION->scorm->attempt = $attempt;
148 // Mark module viewed.
149 $completion = new completion_info($course);
150 $completion->set_module_viewed($cm);
152 // Print the page header.
153 if (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);
158 $PAGE->requires->data_for_js('scormplayerdata', Array('launch' => false,
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);
166 $PAGE->requires->js('/mod/scorm/request.js', true);
167 $PAGE->requires->js('/lib/cookies.js', true);
168 echo $OUTPUT->header();
169 echo $OUTPUT->heading(format_string($scorm->name));
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');
177 $name = false;
179 ?>
180     <div id="scormpage">
182       <div id="tocbox">
183         <div id='scormapi-parent'>
184             <script id="external-scormapi" type="text/JavaScript"></script>
185         </div>
186 <?php
187 if ($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>';
196 ?>
197             <div id="toctree">
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                 }?>
206             </div> <!-- toctree -->
207         </div> <!--  tocbox -->
208                 <noscript>
209                     <div id="noscript">
210                         <?php print_string('noscriptnoscorm', 'scorm'); // No Martin(i), No Party ;-) ?>
212                     </div>
213                 </noscript>
214 <?php
215 if ($result->prerequisites) {
216     if ($scorm->popup != 0 && $displaymode !=='popup') {
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');
224         $url = new moodle_url($PAGE->url, array('scoid' => $sco->id, 'display' => 'popup', 'mode' => $mode));
225         echo html_writer::script(
226             js_writer::function_call('scorm_openpopup', Array($url->out(false),
227                                                        $name, $scorm->options,
228                                                        $scorm->width, $scorm->height)));
229         ?>
230             <noscript>
231                 <iframe id="main" class="scoframe" name="main" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></iframe>
232             </noscript>
233         <?php
234     }
235 } else {
236     echo $OUTPUT->box(get_string('noprerequisites', 'scorm'));
238 ?>
239     </div> <!-- SCORM page -->
240 <?php
241 $scoes = scorm_get_toc_object($USER, $scorm, $currentorg, $sco->id, $mode, $attempt);
242 $adlnav = scorm_get_adlnav_json($scoes['scoes']);
244 if (empty($scorm->popup) || $displaymode == 'popup') {
245     if (!isset($result->toctitle)) {
246         $result->toctitle = get_string('toc', 'scorm');
247     }
248     $jsmodule = array(
249         'name' => 'mod_scorm',
250         'fullpath' => '/mod/scorm/module.js',
251         'requires' => array('json'),
252     );
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);
257 if (!empty($forcejs)) {
258     echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
260 echo $OUTPUT->footer();