MDL-64506 templates: Move BS2 btns' to BS4 btns'
[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
15838edb 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.
39355b46 29$displaymode = optional_param('display', '', PARAM_ALPHA);
e73457e4 30
ca4eda13 31if (!empty($id)) {
4df12b93 32 if (! $cm = get_coursemodule_from_id('scorm', $id, 0, true)) {
ca4eda13 33 print_error('invalidcoursemodule');
f9d5371b 34 }
27b8872c 35 if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
ca4eda13
DM
36 print_error('coursemisconf');
37 }
27b8872c 38 if (! $scorm = $DB->get_record("scorm", array("id" => $cm->instance))) {
ca4eda13 39 print_error('invalidcoursemodule');
5c508e3f 40 }
ca4eda13 41} else if (!empty($a)) {
27b8872c 42 if (! $scorm = $DB->get_record("scorm", array("id" => $a))) {
ca4eda13 43 print_error('invalidcoursemodule');
5c508e3f 44 }
27b8872c 45 if (! $course = $DB->get_record("course", array("id" => $scorm->course))) {
ca4eda13 46 print_error('coursemisconf');
5c508e3f 47 }
4df12b93 48 if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id, true)) {
ca4eda13 49 print_error('invalidcoursemodule');
30bce58e 50 }
ca4eda13
DM
51} else {
52 print_error('missingparameter');
53}
476e97f2
DM
54
55// PARAM_RAW is used for $currentorg, validate it against records stored in the table.
56if (!empty($currentorg)) {
57 if (!$DB->record_exists('scorm_scoes', array('scorm' => $scorm->id, 'identifier' => $currentorg))) {
58 $currentorg = '';
59 }
60}
61
39355b46
DM
62// If new attempt is being triggered set normal mode and increment attempt number.
63$attempt = scorm_get_last_attempt($scorm->id, $USER->id);
64
7f9759c2 65// Check mode is correct and set/validate mode/attempt/newattempt (uses pass by reference).
39355b46 66scorm_check_mode($scorm, $newattempt, $attempt, $USER->id, $mode);
ca4eda13 67
f47a8a6f
DM
68if (!empty($scoid)) {
69 $scoid = scorm_check_launchable_sco($scorm, $scoid);
70}
71
27b8872c 72$url = new moodle_url('/mod/scorm/player.php', array('scoid' => $scoid, 'cm' => $cm->id));
ca4eda13
DM
73if ($mode !== 'normal') {
74 $url->param('mode', $mode);
75}
76if ($currentorg !== '') {
77 $url->param('currentorg', $currentorg);
78}
79if ($newattempt !== 'off') {
80 $url->param('newattempt', $newattempt);
81}
b0c36204
BT
82if ($displaymode !== '') {
83 $url->param('display', $displaymode);
84}
ca4eda13
DM
85$PAGE->set_url($url);
86$forcejs = get_config('scorm', 'forcejavascript');
87if (!empty($forcejs)) {
88 $PAGE->add_body_class('forcejavascript');
89}
df5c7086
MG
90$collapsetocwinsize = get_config('scorm', 'collapsetocwinsize');
91if (empty($collapsetocwinsize)) {
92 // Set as default window size to collapse TOC.
93 $collapsetocwinsize = 767;
94} else {
95 $collapsetocwinsize = intval($collapsetocwinsize);
96}
5c508e3f 97
cdbea7ee 98require_login($course, false, $cm);
f9d5371b 99
ca4eda13
DM
100$strscorms = get_string('modulenameplural', 'scorm');
101$strscorm = get_string('modulename', 'scorm');
102$strpopup = get_string('popup', 'scorm');
103$strexit = get_string('exitactivity', 'scorm');
f9d5371b 104
a3fc4b3a 105$coursecontext = context_course::instance($course->id);
ca4eda13 106
6d954dd3 107if ($displaymode == 'popup') {
3babdf83 108 $PAGE->set_pagelayout('embedded');
6d954dd3 109} else {
8ebbb06a
SH
110 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
111 $pagetitle = strip_tags("$shortname: ".format_string($scorm->name));
6d954dd3
DM
112 $PAGE->set_title($pagetitle);
113 $PAGE->set_heading($course->fullname);
114}
f67b5c99 115if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', context_module::instance($cm->id))) {
ca4eda13
DM
116 echo $OUTPUT->header();
117 notice(get_string("activityiscurrentlyhidden"));
118 echo $OUTPUT->footer();
119 die;
120}
43f4e520 121
02c256c9
JL
122// Check if SCORM available.
123list($available, $warnings) = scorm_get_availability_status($scorm);
124if (!$available) {
125 $reason = current(array_keys($warnings));
126 echo $OUTPUT->header();
127 echo $OUTPUT->box(get_string($reason, "scorm", $warnings[$reason]), "generalbox boxaligncenter");
128 echo $OUTPUT->footer();
129 die;
ca4eda13 130}
02c256c9 131
ca4eda13 132// TOC processing
39355b46 133$scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe.
ca4eda13
DM
134if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
135 $scorm->version = 'scorm_12';
136}
137require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
f9d5371b 138
ca4eda13
DM
139$result = scorm_get_toc($USER, $scorm, $cm->id, TOCJSLINK, $currentorg, $scoid, $mode, $attempt, true, true);
140$sco = $result->sco;
cce0da74
DM
141if ($scorm->lastattemptlock == 1 && $result->attemptleft == 0) {
142 echo $OUTPUT->header();
143 echo $OUTPUT->notification(get_string('exceededmaxattempts', 'scorm'));
144 echo $OUTPUT->footer();
145 exit;
146}
f9d5371b 147
ca4eda13 148$scoidstr = '&amp;scoid='.$sco->id;
ca4eda13 149$modestr = '&amp;mode='.$mode;
f9d5371b 150
37ca1722 151$SESSION->scorm = new stdClass();
ba0e91a2
DM
152$SESSION->scorm->scoid = $sco->id;
153$SESSION->scorm->scormstatus = 'Not Initialized';
154$SESSION->scorm->scormmode = $mode;
155$SESSION->scorm->attempt = $attempt;
f9d5371b 156
39355b46 157// Mark module viewed.
ca4eda13
DM
158$completion = new completion_info($course);
159$completion->set_module_viewed($cm);
6553cda7 160
39355b46 161// Print the page header.
27b8872c 162if (empty($scorm->popup) || $displaymode == 'popup') {
81d7b77d
AN
163 if ($course->format == 'singleactivity' && $scorm->skipview == SCORM_SKIPVIEW_ALWAYS
164 && !has_capability('mod/scorm:viewreport', context_module::instance($cm->id))) {
165 // Redirect students back to site home to avoid redirect loop.
166 $exiturl = $CFG->wwwroot;
167 } else {
168 // Redirect back to the correct section if one section per page is being used.
169 $exiturl = course_get_url($course, $cm->sectionnum);
170 }
7a0d8e72 171
29551c4b 172 $exitlink = html_writer::link($exiturl, $strexit, array('title' => $strexit, 'class' => 'btn btn-secondary'));
6d954dd3 173 $PAGE->set_button($exitlink);
ca4eda13 174}
4e204f89 175
81f460b1 176$PAGE->requires->data_for_js('scormplayerdata', Array('launch' => false,
35cfa9e6
PH
177 'currentorg' => '',
178 'sco' => 0,
179 'scorm' => 0,
180 'courseid' => $scorm->course,
181 'cwidth' => $scorm->width,
182 'cheight' => $scorm->height,
183 'popupoptions' => $scorm->options), true);
ca4eda13
DM
184$PAGE->requires->js('/mod/scorm/request.js', true);
185$PAGE->requires->js('/lib/cookies.js', true);
93ca0d54
N
186
187if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.js')) {
188 $PAGE->requires->js('/mod/scorm/datamodels/'.$scorm->version.'.js', true);
189} else {
190 $PAGE->requires->js('/mod/scorm/datamodels/scorm_12.js', true);
191}
192
ca4eda13 193echo $OUTPUT->header();
74d2a9df 194if (!empty($scorm->displayactivityname)) {
74ecabb8
JC
195 echo $OUTPUT->heading(format_string($scorm->name));
196}
bd257031 197
ca4eda13
DM
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
27b8872c
N
206echo html_writer::start_div('', array('id' => 'scormpage'));
207echo html_writer::start_div('', array('id' => 'tocbox'));
4cb25b36 208echo html_writer::div(html_writer::tag('script', '', array('id' => 'external-scormapi', 'type' => 'text/JavaScript')), '',
209 array('id' => 'scormapi-parent'));
b5b99e0b 210
27b8872c
N
211if ($scorm->hidetoc == SCORM_TOC_POPUP or $mode == 'browse' or $mode == 'review') {
212 echo html_writer::start_div('', array('id' => 'scormtop'));
213 echo $mode == 'browse' ? html_writer::div(get_string('browsemode', 'scorm'), 'scorm-left', array('id' => 'scormmode')) : '';
214 echo $mode == 'review' ? html_writer::div(get_string('reviewmode', 'scorm'), 'scorm-left', array('id' => 'scormmode')) : '';
61c347cc 215 if ($scorm->hidetoc == SCORM_TOC_POPUP) {
27b8872c 216 echo html_writer::div($result->tocmenu, 'scorm-right', array('id' => 'scormnav'));
61c347cc 217 }
27b8872c
N
218 echo html_writer::end_div();
219}
220
221echo html_writer::start_div('', array('id' => 'toctree'));
222
223if (empty($scorm->popup) || $displaymode == 'popup') {
224 echo $result->toc;
225} else {
4cb25b36 226 // Added incase javascript popups are blocked we don't provide a direct link
227 // to the pop-up as JS communication can fail - the user must disable their pop-up blocker.
228 $linkcourse = html_writer::link($CFG->wwwroot.'/course/view.php?id='.
229 $scorm->course, get_string('finishscormlinkname', 'scorm'));
27b8872c
N
230 echo $OUTPUT->box(get_string('finishscorm', 'scorm', $linkcourse), 'generalbox', 'altfinishlink');
231}
15838edb 232echo html_writer::end_div(); // Toc tree ends.
233echo html_writer::end_div(); // Toc box ends.
27b8872c
N
234echo html_writer::tag('noscript', html_writer::div(get_string('noscriptnoscorm', 'scorm'), '', array('id' => 'noscript')));
235
ca4eda13 236if ($result->prerequisites) {
27b8872c 237 if ($scorm->popup != 0 && $displaymode !== 'popup') {
4cb25b36 238 // Clean the name for the window as IE is fussy.
ca4eda13
DM
239 $name = preg_replace("/[^A-Za-z0-9]/", "", $scorm->name);
240 if (!$name) {
241 $name = 'DefaultPlayerWindow';
242 }
243 $name = 'scorm_'.$name;
244 echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
39355b46 245 $url = new moodle_url($PAGE->url, array('scoid' => $sco->id, 'display' => 'popup', 'mode' => $mode));
ca4eda13 246 echo html_writer::script(
6d954dd3 247 js_writer::function_call('scorm_openpopup', Array($url->out(false),
ca4eda13
DM
248 $name, $scorm->options,
249 $scorm->width, $scorm->height)));
4cb25b36 250 echo html_writer::tag('noscript', html_writer::tag('iframe', '', array('id' => 'main',
251 'class' => 'scoframe', 'name' => 'main', 'src' => 'loadSCO.php?id='.$cm->id.$scoidstr.$modestr)));
f9d5371b 252 }
ca4eda13
DM
253} else {
254 echo $OUTPUT->box(get_string('noprerequisites', 'scorm'));
255}
15838edb 256echo html_writer::end_div(); // Scorm page ends.
27b8872c 257
df5c7086 258$scoes = scorm_get_toc_object($USER, $scorm, $currentorg, $sco->id, $mode, $attempt);
41e205cd 259$adlnav = scorm_get_adlnav_json($scoes['scoes']);
39355b46 260
6d954dd3
DM
261if (empty($scorm->popup) || $displaymode == 'popup') {
262 if (!isset($result->toctitle)) {
263 $result->toctitle = get_string('toc', 'scorm');
264 }
c23ff8e3
DS
265 $jsmodule = array(
266 'name' => 'mod_scorm',
267 'fullpath' => '/mod/scorm/module.js',
268 'requires' => array('json'),
269 );
df5c7086 270 $scorm->nav = intval($scorm->nav);
4cb25b36 271 $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->nav, $scorm->navpositionleft, $scorm->navpositiontop,
272 $scorm->hidetoc, $collapsetocwinsize, $result->toctitle, $name, $sco->id, $adlnav), false, $jsmodule);
5c2aa157 273}
30bce58e 274if (!empty($forcejs)) {
e63d7244
DM
275 $message = $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
276 echo html_writer::tag('noscript', $message);
30bce58e 277}
6bf26777 278
3ad5eeed
N
279if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.php')) {
280 include_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.php');
7b9036be 281} else {
3ad5eeed 282 include_once($CFG->dirroot.'/mod/scorm/datamodels/scorm_12.php');
7b9036be
N
283}
284
6bf26777
AN
285// Add the checknet system to keep checking for a connection.
286$PAGE->requires->string_for_js('networkdropped', 'mod_scorm');
ecdfab51
AG
287// Build arguments to send to checknet JS.
288$args = array(
6bf26777 289 'message' => array('networkdropped', 'mod_scorm'),
ecdfab51
AG
290 'frequency' => 30000, // Frequency of network check.
291 'timeout' => 10000, // Timeout of network check.
292 'maxalerts' => 1 // Max number of alerts to be thrown.
293);
294$PAGE->requires->yui_module('moodle-core-checknet', 'M.core.checknet.init', array($args));
a5ba44f9 295echo $OUTPUT->footer();
3ad5eeed
N
296
297// Set the start time of this SCO.
298scorm_insert_track($USER->id, $scorm->id, $scoid, $attempt, 'x.start.time', time());