MDL-80880 quiz: change display of previous attempts summary
[moodle.git] / mod / scorm / player.php
blob5962d31080722251b069b02a4107ab4ae6b00e18
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 if (!empty($id)) {
32 if (! $cm = get_coursemodule_from_id('scorm', $id, 0, true)) {
33 throw new \moodle_exception('invalidcoursemodule');
35 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
36 throw new \moodle_exception('coursemisconf');
38 if (! $scorm = $DB->get_record("scorm", array("id" => $cm->instance))) {
39 throw new \moodle_exception('invalidcoursemodule');
41 } else if (!empty($a)) {
42 if (! $scorm = $DB->get_record("scorm", array("id" => $a))) {
43 throw new \moodle_exception('invalidcoursemodule');
45 if (! $course = $DB->get_record("course", array("id" => $scorm->course))) {
46 throw new \moodle_exception('coursemisconf');
48 if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id, true)) {
49 throw new \moodle_exception('invalidcoursemodule');
51 } else {
52 throw new \moodle_exception('missingparameter');
55 // PARAM_RAW is used for $currentorg, validate it against records stored in the table.
56 if (!empty($currentorg)) {
57 if (!$DB->record_exists('scorm_scoes', array('scorm' => $scorm->id, 'identifier' => $currentorg))) {
58 $currentorg = '';
62 // If new attempt is being triggered set normal mode and increment attempt number.
63 $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
65 // Check mode is correct and set/validate mode/attempt/newattempt (uses pass by reference).
66 scorm_check_mode($scorm, $newattempt, $attempt, $USER->id, $mode);
68 if (!empty($scoid)) {
69 $scoid = scorm_check_launchable_sco($scorm, $scoid);
72 $url = new moodle_url('/mod/scorm/player.php', array('scoid' => $scoid, 'cm' => $cm->id));
73 if ($mode !== 'normal') {
74 $url->param('mode', $mode);
76 if ($currentorg !== '') {
77 $url->param('currentorg', $currentorg);
79 if ($newattempt !== 'off') {
80 $url->param('newattempt', $newattempt);
82 if ($displaymode !== '') {
83 $url->param('display', $displaymode);
85 $PAGE->set_url($url);
86 $PAGE->set_secondary_active_tab("modulepage");
88 $forcejs = get_config('scorm', 'forcejavascript');
89 if (!empty($forcejs)) {
90 $PAGE->add_body_class('forcejavascript');
92 $collapsetocwinsize = get_config('scorm', 'collapsetocwinsize');
93 if (empty($collapsetocwinsize)) {
94 // Set as default window size to collapse TOC.
95 $collapsetocwinsize = 767;
96 } else {
97 $collapsetocwinsize = intval($collapsetocwinsize);
100 require_login($course, false, $cm);
102 $strscorms = get_string('modulenameplural', 'scorm');
103 $strscorm = get_string('modulename', 'scorm');
104 $strpopup = get_string('popup', 'scorm');
105 $strexit = get_string('exitactivity', 'scorm');
107 $coursecontext = context_course::instance($course->id);
109 if ($displaymode == 'popup') {
110 $PAGE->set_pagelayout('embedded');
111 } else {
112 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
113 $pagetitle = strip_tags("$shortname: ".format_string($scorm->name));
114 $PAGE->set_title($pagetitle);
115 $PAGE->set_heading($course->fullname);
117 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', context_module::instance($cm->id))) {
118 echo $OUTPUT->header();
119 notice(get_string("activityiscurrentlyhidden"));
120 echo $OUTPUT->footer();
121 die;
124 // Check if SCORM available.
125 list($available, $warnings) = scorm_get_availability_status($scorm);
126 if (!$available) {
127 $reason = current(array_keys($warnings));
128 echo $OUTPUT->header();
129 echo $OUTPUT->box(get_string($reason, "scorm", $warnings[$reason]), "generalbox boxaligncenter");
130 echo $OUTPUT->footer();
131 die;
134 // TOC processing
135 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe.
136 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
137 $scorm->version = 'scorm_12';
139 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
141 $result = scorm_get_toc($USER, $scorm, $cm->id, TOCJSLINK, $currentorg, $scoid, $mode, $attempt, true, true);
142 $sco = $result->sco;
143 if ($scorm->lastattemptlock == 1 && $result->attemptleft == 0) {
144 echo $OUTPUT->header();
145 echo $OUTPUT->notification(get_string('exceededmaxattempts', 'scorm'));
146 echo $OUTPUT->footer();
147 exit;
150 $scoidstr = '&amp;scoid='.$sco->id;
151 $modestr = '&amp;mode='.$mode;
153 $SESSION->scorm = new stdClass();
154 $SESSION->scorm->scoid = $sco->id;
155 $SESSION->scorm->scormstatus = 'Not Initialized';
156 $SESSION->scorm->scormmode = $mode;
157 $SESSION->scorm->attempt = $attempt;
159 // Mark module viewed.
160 $completion = new completion_info($course);
161 $completion->set_module_viewed($cm);
163 // Generate the exit button.
164 $exiturl = "";
165 if (empty($scorm->popup) || $displaymode == 'popup') {
166 if ($course->format == 'singleactivity' && $scorm->skipview == SCORM_SKIPVIEW_ALWAYS
167 && !has_capability('mod/scorm:viewreport', context_module::instance($cm->id))) {
168 // Redirect students back to site home to avoid redirect loop.
169 $exiturl = $CFG->wwwroot;
170 } else {
171 // Redirect back to the correct section if one section per page is being used.
172 $exiturl = course_get_url($course, $cm->sectionnum)->out();
176 // Print the page header.
177 $PAGE->requires->data_for_js('scormplayerdata', Array('launch' => false,
178 'currentorg' => '',
179 'sco' => 0,
180 'scorm' => 0,
181 'courseid' => $scorm->course,
182 'cwidth' => $scorm->width,
183 'cheight' => $scorm->height,
184 'popupoptions' => $scorm->options), true);
185 $PAGE->requires->js('/mod/scorm/request.js', true);
186 $PAGE->requires->js('/lib/cookies.js', true);
188 if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.js')) {
189 $PAGE->requires->js('/mod/scorm/datamodels/'.$scorm->version.'.js', true);
190 } else {
191 $PAGE->requires->js('/mod/scorm/datamodels/scorm_12.js', true);
193 $activityheader = $PAGE->activityheader;
194 $headerconfig = [
195 'description' => '',
196 'hidecompletion' => true
199 $activityheader->set_attrs($headerconfig);
200 echo $OUTPUT->header();
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;
210 // Exit button should ONLY be displayed when in the current window.
211 if ($displaymode !== 'popup') {
212 $renderer = $PAGE->get_renderer('mod_scorm');
213 echo $renderer->generate_exitbar($exiturl);
216 echo html_writer::start_div('', array('id' => 'scormpage'));
217 echo html_writer::start_div('', array('id' => 'tocbox'));
218 echo html_writer::div(html_writer::tag('script', '', array('id' => 'external-scormapi', 'type' => 'text/JavaScript')), '',
219 array('id' => 'scormapi-parent'));
221 if ($scorm->hidetoc == SCORM_TOC_POPUP or $mode == 'browse' or $mode == 'review') {
222 echo html_writer::start_div('mb-3', array('id' => 'scormtop'));
223 if ($mode == 'browse' || $mode == 'review') {
224 echo html_writer::div(get_string("{$mode}mode", 'scorm'), 'scorm-left h3', ['id' => 'scormmode']);
226 if ($scorm->hidetoc == SCORM_TOC_POPUP) {
227 echo html_writer::div($result->tocmenu, 'scorm-right', array('id' => 'scormnav'));
229 echo html_writer::end_div();
232 echo html_writer::start_div('', array('id' => 'toctree'));
234 if (empty($scorm->popup) || $displaymode == 'popup') {
235 echo $result->toc;
236 } else {
237 // Added incase javascript popups are blocked we don't provide a direct link
238 // to the pop-up as JS communication can fail - the user must disable their pop-up blocker.
239 $linkcourse = html_writer::link($CFG->wwwroot.'/course/view.php?id='.
240 $scorm->course, get_string('finishscormlinkname', 'scorm'));
241 echo $OUTPUT->box(get_string('finishscorm', 'scorm', $linkcourse), 'generalbox', 'altfinishlink');
243 echo html_writer::end_div(); // Toc tree ends.
244 echo html_writer::end_div(); // Toc box ends.
245 echo html_writer::tag('noscript', html_writer::div(get_string('noscriptnoscorm', 'scorm'), '', array('id' => 'noscript')));
247 if ($result->prerequisites) {
248 if ($scorm->popup != 0 && $displaymode !== 'popup') {
249 // Clean the name for the window as IE is fussy.
250 $name = preg_replace("/[^A-Za-z0-9]/", "", $scorm->name);
251 if (!$name) {
252 $name = 'DefaultPlayerWindow';
254 $name = 'scorm_'.$name;
255 echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
256 $url = new moodle_url($PAGE->url, array('scoid' => $sco->id, 'display' => 'popup', 'mode' => $mode));
257 echo html_writer::script(
258 js_writer::function_call('scorm_openpopup', Array($url->out(false),
259 $name, $scorm->options,
260 $scorm->width, $scorm->height)));
261 echo html_writer::tag('noscript', html_writer::tag('iframe', '', array('id' => 'main',
262 'class' => 'scoframe', 'name' => 'main', 'src' => 'loadSCO.php?id='.$cm->id.$scoidstr.$modestr)));
264 } else {
265 echo $OUTPUT->box(get_string('noprerequisites', 'scorm'));
267 echo html_writer::end_div(); // Scorm page ends.
269 $scoes = scorm_get_toc_object($USER, $scorm, $currentorg, $sco->id, $mode, $attempt);
270 $adlnav = scorm_get_adlnav_json($scoes['scoes']);
272 if (empty($scorm->popup) || $displaymode == 'popup') {
273 if (!isset($result->toctitle)) {
274 $result->toctitle = get_string('toc', 'scorm');
276 $jsmodule = array(
277 'name' => 'mod_scorm',
278 'fullpath' => '/mod/scorm/module.js',
279 'requires' => array('json'),
281 $scorm->nav = intval($scorm->nav);
282 $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->nav, $scorm->navpositionleft, $scorm->navpositiontop,
283 $scorm->hidetoc, $collapsetocwinsize, $result->toctitle, $name, $sco->id, $adlnav), false, $jsmodule);
285 if (!empty($forcejs)) {
286 $message = $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
287 echo html_writer::tag('noscript', $message);
290 if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.php')) {
291 include_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.php');
292 } else {
293 include_once($CFG->dirroot.'/mod/scorm/datamodels/scorm_12.php');
296 // Add the keepalive system to keep checking for a connection.
297 \core\session\manager::keepalive('networkdropped', 'mod_scorm', 30, 10);
299 echo $OUTPUT->footer();
301 // Set the start time of this SCO.
302 scorm_insert_track($USER->id, $scorm->id, $scoid, $attempt, 'x.start.time', time());