MDL-64506 templates: Move BS2 btns' to BS4 btns'
[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, 0, true)) {
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, true)) {
49         print_error('invalidcoursemodule');
50     }
51 } else {
52     print_error('missingparameter');
53 }
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 = '';
59     }
60 }
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);
70 }
72 $url = new moodle_url('/mod/scorm/player.php', array('scoid' => $scoid, 'cm' => $cm->id));
73 if ($mode !== 'normal') {
74     $url->param('mode', $mode);
75 }
76 if ($currentorg !== '') {
77     $url->param('currentorg', $currentorg);
78 }
79 if ($newattempt !== 'off') {
80     $url->param('newattempt', $newattempt);
81 }
82 if ($displaymode !== '') {
83     $url->param('display', $displaymode);
84 }
85 $PAGE->set_url($url);
86 $forcejs = get_config('scorm', 'forcejavascript');
87 if (!empty($forcejs)) {
88     $PAGE->add_body_class('forcejavascript');
89 }
90 $collapsetocwinsize = get_config('scorm', 'collapsetocwinsize');
91 if (empty($collapsetocwinsize)) {
92     // Set as default window size to collapse TOC.
93     $collapsetocwinsize = 767;
94 } else {
95     $collapsetocwinsize = intval($collapsetocwinsize);
96 }
98 require_login($course, false, $cm);
100 $strscorms = get_string('modulenameplural', 'scorm');
101 $strscorm  = get_string('modulename', 'scorm');
102 $strpopup = get_string('popup', 'scorm');
103 $strexit = get_string('exitactivity', 'scorm');
105 $coursecontext = context_course::instance($course->id);
107 if ($displaymode == 'popup') {
108     $PAGE->set_pagelayout('embedded');
109 } else {
110     $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
111     $pagetitle = strip_tags("$shortname: ".format_string($scorm->name));
112     $PAGE->set_title($pagetitle);
113     $PAGE->set_heading($course->fullname);
115 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', context_module::instance($cm->id))) {
116     echo $OUTPUT->header();
117     notice(get_string("activityiscurrentlyhidden"));
118     echo $OUTPUT->footer();
119     die;
122 // Check if SCORM available.
123 list($available, $warnings) = scorm_get_availability_status($scorm);
124 if (!$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;
132 // TOC processing
133 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR));   // Just to be safe.
134 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
135     $scorm->version = 'scorm_12';
137 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
139 $result = scorm_get_toc($USER, $scorm, $cm->id, TOCJSLINK, $currentorg, $scoid, $mode, $attempt, true, true);
140 $sco = $result->sco;
141 if ($scorm->lastattemptlock == 1 && $result->attemptleft == 0) {
142     echo $OUTPUT->header();
143     echo $OUTPUT->notification(get_string('exceededmaxattempts', 'scorm'));
144     echo $OUTPUT->footer();
145     exit;
148 $scoidstr = '&amp;scoid='.$sco->id;
149 $modestr = '&amp;mode='.$mode;
151 $SESSION->scorm = new stdClass();
152 $SESSION->scorm->scoid = $sco->id;
153 $SESSION->scorm->scormstatus = 'Not Initialized';
154 $SESSION->scorm->scormmode = $mode;
155 $SESSION->scorm->attempt = $attempt;
157 // Mark module viewed.
158 $completion = new completion_info($course);
159 $completion->set_module_viewed($cm);
161 // Print the page header.
162 if (empty($scorm->popup) || $displaymode == 'popup') {
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     }
172     $exitlink = html_writer::link($exiturl, $strexit, array('title' => $strexit, 'class' => 'btn btn-secondary'));
173     $PAGE->set_button($exitlink);
176 $PAGE->requires->data_for_js('scormplayerdata', Array('launch' => false,
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);
184 $PAGE->requires->js('/mod/scorm/request.js', true);
185 $PAGE->requires->js('/lib/cookies.js', true);
187 if (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);
193 echo $OUTPUT->header();
194 if (!empty($scorm->displayactivityname)) {
195     echo $OUTPUT->heading(format_string($scorm->name));
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');
204 $name = false;
206 echo html_writer::start_div('', array('id' => 'scormpage'));
207 echo html_writer::start_div('', array('id' => 'tocbox'));
208 echo html_writer::div(html_writer::tag('script', '', array('id' => 'external-scormapi', 'type' => 'text/JavaScript')), '',
209                         array('id' => 'scormapi-parent'));
211 if ($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')) : '';
215     if ($scorm->hidetoc == SCORM_TOC_POPUP) {
216         echo html_writer::div($result->tocmenu, 'scorm-right', array('id' => 'scormnav'));
217     }
218     echo html_writer::end_div();
221 echo html_writer::start_div('', array('id' => 'toctree'));
223 if (empty($scorm->popup) || $displaymode == 'popup') {
224     echo $result->toc;
225 } else {
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'));
230     echo $OUTPUT->box(get_string('finishscorm', 'scorm', $linkcourse), 'generalbox', 'altfinishlink');
232 echo html_writer::end_div(); // Toc tree ends.
233 echo html_writer::end_div(); // Toc box ends.
234 echo html_writer::tag('noscript', html_writer::div(get_string('noscriptnoscorm', 'scorm'), '', array('id' => 'noscript')));
236 if ($result->prerequisites) {
237     if ($scorm->popup != 0 && $displaymode !== 'popup') {
238         // Clean the name for the window as IE is fussy.
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');
245         $url = new moodle_url($PAGE->url, array('scoid' => $sco->id, 'display' => 'popup', 'mode' => $mode));
246         echo html_writer::script(
247             js_writer::function_call('scorm_openpopup', Array($url->out(false),
248                                                        $name, $scorm->options,
249                                                        $scorm->width, $scorm->height)));
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)));
252     }
253 } else {
254     echo $OUTPUT->box(get_string('noprerequisites', 'scorm'));
256 echo html_writer::end_div(); // Scorm page ends.
258 $scoes = scorm_get_toc_object($USER, $scorm, $currentorg, $sco->id, $mode, $attempt);
259 $adlnav = scorm_get_adlnav_json($scoes['scoes']);
261 if (empty($scorm->popup) || $displaymode == 'popup') {
262     if (!isset($result->toctitle)) {
263         $result->toctitle = get_string('toc', 'scorm');
264     }
265     $jsmodule = array(
266         'name' => 'mod_scorm',
267         'fullpath' => '/mod/scorm/module.js',
268         'requires' => array('json'),
269     );
270     $scorm->nav = intval($scorm->nav);
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);
274 if (!empty($forcejs)) {
275     $message = $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
276     echo html_writer::tag('noscript', $message);
279 if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.php')) {
280     include_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.php');
281 } else {
282     include_once($CFG->dirroot.'/mod/scorm/datamodels/scorm_12.php');
285 // Add the checknet system to keep checking for a connection.
286 $PAGE->requires->string_for_js('networkdropped', 'mod_scorm');
287 // Build arguments to send to checknet JS.
288 $args = array(
289     'message' => array('networkdropped', 'mod_scorm'),
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));
295 echo $OUTPUT->footer();
297 // Set the start time of this SCO.
298 scorm_insert_track($USER->id, $scorm->id, $scoid, $attempt, 'x.start.time', time());