MDL-50837 mod_scorm: Fix availability checks
[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 $PAGE->set_url($url);
83 $forcejs = get_config('scorm', 'forcejavascript');
84 if (!empty($forcejs)) {
85     $PAGE->add_body_class('forcejavascript');
86 }
87 $collapsetocwinsize = get_config('scorm', 'collapsetocwinsize');
88 if (empty($collapsetocwinsize)) {
89     // Set as default window size to collapse TOC.
90     $collapsetocwinsize = 767;
91 } else {
92     $collapsetocwinsize = intval($collapsetocwinsize);
93 }
95 require_login($course, false, $cm);
97 $strscorms = get_string('modulenameplural', 'scorm');
98 $strscorm  = get_string('modulename', 'scorm');
99 $strpopup = get_string('popup', 'scorm');
100 $strexit = get_string('exitactivity', 'scorm');
102 $coursecontext = context_course::instance($course->id);
104 if ($displaymode == 'popup') {
105     $PAGE->set_pagelayout('embedded');
106 } else {
107     $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
108     $pagetitle = strip_tags("$shortname: ".format_string($scorm->name));
109     $PAGE->set_title($pagetitle);
110     $PAGE->set_heading($course->fullname);
112 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', context_module::instance($cm->id))) {
113     echo $OUTPUT->header();
114     notice(get_string("activityiscurrentlyhidden"));
115     echo $OUTPUT->footer();
116     die;
119 // Check if SCORM available.
120 list($available, $warnings) = scorm_get_availability_status($scorm);
121 if (!$available) {
122     $reason = current(array_keys($warnings));
123     echo $OUTPUT->header();
124     echo $OUTPUT->box(get_string($reason, "scorm", $warnings[$reason]), "generalbox boxaligncenter");
125     echo $OUTPUT->footer();
126     die;
129 // TOC processing
130 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR));   // Just to be safe.
131 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
132     $scorm->version = 'scorm_12';
134 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
136 $result = scorm_get_toc($USER, $scorm, $cm->id, TOCJSLINK, $currentorg, $scoid, $mode, $attempt, true, true);
137 $sco = $result->sco;
138 if ($scorm->lastattemptlock == 1 && $result->attemptleft == 0) {
139     echo $OUTPUT->header();
140     echo $OUTPUT->notification(get_string('exceededmaxattempts', 'scorm'));
141     echo $OUTPUT->footer();
142     exit;
145 $scoidstr = '&amp;scoid='.$sco->id;
146 $modestr = '&amp;mode='.$mode;
148 $SESSION->scorm = new stdClass();
149 $SESSION->scorm->scoid = $sco->id;
150 $SESSION->scorm->scormstatus = 'Not Initialized';
151 $SESSION->scorm->scormmode = $mode;
152 $SESSION->scorm->attempt = $attempt;
154 // Mark module viewed.
155 $completion = new completion_info($course);
156 $completion->set_module_viewed($cm);
158 // Print the page header.
159 if (empty($scorm->popup) || $displaymode == 'popup') {
160     // Redirect back to the correct section if one section per page is being used.
161     $exiturl = course_get_url($course, $cm->sectionnum);
163     $exitlink = html_writer::link($exiturl, $strexit, array('title' => $strexit));
164     $PAGE->set_button($exitlink);
167 $PAGE->requires->data_for_js('scormplayerdata', Array('launch' => false,
168                                                        'currentorg' => '',
169                                                        'sco' => 0,
170                                                        'scorm' => 0,
171                                                        'courseid' => $scorm->course,
172                                                        'cwidth' => $scorm->width,
173                                                        'cheight' => $scorm->height,
174                                                        'popupoptions' => $scorm->options), true);
175 $PAGE->requires->js('/mod/scorm/request.js', true);
176 $PAGE->requires->js('/lib/cookies.js', true);
178 if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.js')) {
179     $PAGE->requires->js('/mod/scorm/datamodels/'.$scorm->version.'.js', true);
180 } else {
181     $PAGE->requires->js('/mod/scorm/datamodels/scorm_12.js', true);
184 echo $OUTPUT->header();
185 if (!empty($scorm->displayactivityname)) {
186     echo $OUTPUT->heading(format_string($scorm->name));
189 $PAGE->requires->string_for_js('navigation', 'scorm');
190 $PAGE->requires->string_for_js('toc', 'scorm');
191 $PAGE->requires->string_for_js('hide', 'moodle');
192 $PAGE->requires->string_for_js('show', 'moodle');
193 $PAGE->requires->string_for_js('popupsblocked', 'scorm');
195 $name = false;
197 echo html_writer::start_div('', array('id' => 'scormpage'));
198 echo html_writer::start_div('', array('id' => 'tocbox'));
199 echo html_writer::div(html_writer::tag('script', '', array('id' => 'external-scormapi', 'type' => 'text/JavaScript')), '',
200                         array('id' => 'scormapi-parent'));
202 if ($scorm->hidetoc == SCORM_TOC_POPUP or $mode == 'browse' or $mode == 'review') {
203     echo html_writer::start_div('', array('id' => 'scormtop'));
204     echo $mode == 'browse' ? html_writer::div(get_string('browsemode', 'scorm'), 'scorm-left', array('id' => 'scormmode')) : '';
205     echo $mode == 'review' ? html_writer::div(get_string('reviewmode', 'scorm'), 'scorm-left', array('id' => 'scormmode')) : '';
206     if ($scorm->hidetoc == SCORM_TOC_POPUP) {
207         echo html_writer::div($result->tocmenu, 'scorm-right', array('id' => 'scormnav'));
208     }
209     echo html_writer::end_div();
212 echo html_writer::start_div('', array('id' => 'toctree'));
214 if (empty($scorm->popup) || $displaymode == 'popup') {
215     echo $result->toc;
216 } else {
217     // Added incase javascript popups are blocked we don't provide a direct link
218     // to the pop-up as JS communication can fail - the user must disable their pop-up blocker.
219     $linkcourse = html_writer::link($CFG->wwwroot.'/course/view.php?id='.
220                     $scorm->course, get_string('finishscormlinkname', 'scorm'));
221     echo $OUTPUT->box(get_string('finishscorm', 'scorm', $linkcourse), 'generalbox', 'altfinishlink');
223 echo html_writer::end_div(); // Toc tree ends.
224 echo html_writer::end_div(); // Toc box ends.
225 echo html_writer::tag('noscript', html_writer::div(get_string('noscriptnoscorm', 'scorm'), '', array('id' => 'noscript')));
227 if ($result->prerequisites) {
228     if ($scorm->popup != 0 && $displaymode !== 'popup') {
229         // Clean the name for the window as IE is fussy.
230         $name = preg_replace("/[^A-Za-z0-9]/", "", $scorm->name);
231         if (!$name) {
232             $name = 'DefaultPlayerWindow';
233         }
234         $name = 'scorm_'.$name;
235         echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
236         $url = new moodle_url($PAGE->url, array('scoid' => $sco->id, 'display' => 'popup', 'mode' => $mode));
237         echo html_writer::script(
238             js_writer::function_call('scorm_openpopup', Array($url->out(false),
239                                                        $name, $scorm->options,
240                                                        $scorm->width, $scorm->height)));
241         echo html_writer::tag('noscript', html_writer::tag('iframe', '', array('id' => 'main',
242                                 'class' => 'scoframe', 'name' => 'main', 'src' => 'loadSCO.php?id='.$cm->id.$scoidstr.$modestr)));
243     }
244 } else {
245     echo $OUTPUT->box(get_string('noprerequisites', 'scorm'));
247 echo html_writer::end_div(); // Scorm page ends.
249 $scoes = scorm_get_toc_object($USER, $scorm, $currentorg, $sco->id, $mode, $attempt);
250 $adlnav = scorm_get_adlnav_json($scoes['scoes']);
252 if (empty($scorm->popup) || $displaymode == 'popup') {
253     if (!isset($result->toctitle)) {
254         $result->toctitle = get_string('toc', 'scorm');
255     }
256     $jsmodule = array(
257         'name' => 'mod_scorm',
258         'fullpath' => '/mod/scorm/module.js',
259         'requires' => array('json'),
260     );
261     $scorm->nav = intval($scorm->nav);
262     $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->nav, $scorm->navpositionleft, $scorm->navpositiontop,
263                             $scorm->hidetoc, $collapsetocwinsize, $result->toctitle, $name, $sco->id, $adlnav), false, $jsmodule);
265 if (!empty($forcejs)) {
266     echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
269 if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.php')) {
270     include_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.php');
271 } else {
272     include_once($CFG->dirroot.'/mod/scorm/datamodels/scorm_12.php');
275 // Add the checknet system to keep checking for a connection.
276 $PAGE->requires->string_for_js('networkdropped', 'mod_scorm');
277 $PAGE->requires->yui_module('moodle-core-checknet', 'M.core.checknet.init', array(array(
278     'message' => array('networkdropped', 'mod_scorm'),
279 )));
280 echo $OUTPUT->footer();
282 // Set the start time of this SCO.
283 scorm_insert_track($USER->id, $scorm->id, $scoid, $attempt, 'x.start.time', time());