MDL-41577 SCORM: remove old IE9 hack that didn't really work.
[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 mode/attempt (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 add_to_log($course->id, 'scorm', 'view', "player.php?cm=$cm->id&scoid=$sco->id", "$scorm->id", $cm->id);
142 $scoidstr = '&amp;scoid='.$sco->id;
143 $modestr = '&amp;mode='.$mode;
145 $SESSION->scorm = new stdClass();
146 $SESSION->scorm->scoid = $sco->id;
147 $SESSION->scorm->scormstatus = 'Not Initialized';
148 $SESSION->scorm->scormmode = $mode;
149 $SESSION->scorm->attempt = $attempt;
151 // Mark module viewed.
152 $completion = new completion_info($course);
153 $completion->set_module_viewed($cm);
155 // Print the page header.
156 if (empty($scorm->popup) || $displaymode=='popup') {
157     $exitlink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'" title="'.$strexit.'">'.$strexit.'</a> ';
158     $PAGE->set_button($exitlink);
161 $PAGE->requires->data_for_js('scormplayerdata', Array('launch' => false,
162                                                        'currentorg' => '',
163                                                        'sco' => 0,
164                                                        'scorm' => 0,
165                                                        'courseid' => $scorm->course,
166                                                        'cwidth' => $scorm->width,
167                                                        'cheight' => $scorm->height,
168                                                        'popupoptions' => $scorm->options), true);
169 $PAGE->requires->js('/mod/scorm/request.js', true);
170 $PAGE->requires->js('/lib/cookies.js', true);
171 echo $OUTPUT->header();
172 echo $OUTPUT->heading(format_string($scorm->name));
174 $PAGE->requires->string_for_js('navigation', 'scorm');
175 $PAGE->requires->string_for_js('toc', 'scorm');
176 $PAGE->requires->string_for_js('hide', 'moodle');
177 $PAGE->requires->string_for_js('show', 'moodle');
178 $PAGE->requires->string_for_js('popupsblocked', 'scorm');
180 $name = false;
182 ?>
183     <div id="scormpage">
185       <div id="tocbox">
186         <div id='scormapi-parent'>
187             <script id="external-scormapi" type="text/JavaScript"></script>
188         </div>
189 <?php
190 if ($scorm->hidetoc == SCORM_TOC_POPUP or $mode=='browse' or $mode=='review') {
191     echo '<div id="scormtop">';
192     echo $mode == 'browse' ? '<div id="scormmode" class="scorm-left">'.get_string('browsemode', 'scorm')."</div>\n" : '';
193     echo $mode == 'review' ? '<div id="scormmode" class="scorm-left">'.get_string('reviewmode', 'scorm')."</div>\n" : '';
194     if ($scorm->hidetoc == SCORM_TOC_POPUP) {
195         echo '<div id="scormnav" class="scorm-right">'.$result->tocmenu.'</div>';
196     }
197     echo '</div>';
199 ?>
200             <div id="toctree">
201                 <?php
202                 if (empty($scorm->popup) || $displaymode == 'popup') {
203                     echo $result->toc;
204                 } else {
205                     //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.
206                     $linkcourse = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'">' . get_string('finishscormlinkname', 'scorm') . '</a>';
207                     echo $OUTPUT->box(get_string('finishscorm', 'scorm', $linkcourse), 'generalbox', 'altfinishlink');
208                 }?>
209             </div> <!-- toctree -->
210         </div> <!--  tocbox -->
211                 <noscript>
212                     <div id="noscript">
213                         <?php print_string('noscriptnoscorm', 'scorm'); // No Martin(i), No Party ;-) ?>
215                     </div>
216                 </noscript>
217 <?php
218 if ($result->prerequisites) {
219     if ($scorm->popup != 0 && $displaymode !=='popup') {
220         // Clean the name for the window as IE is fussy
221         $name = preg_replace("/[^A-Za-z0-9]/", "", $scorm->name);
222         if (!$name) {
223             $name = 'DefaultPlayerWindow';
224         }
225         $name = 'scorm_'.$name;
226         echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
227         $url = new moodle_url($PAGE->url, array('scoid' => $sco->id, 'display' => 'popup', 'mode' => $mode));
228         echo html_writer::script(
229             js_writer::function_call('scorm_openpopup', Array($url->out(false),
230                                                        $name, $scorm->options,
231                                                        $scorm->width, $scorm->height)));
232         ?>
233             <noscript>
234                 <iframe id="main" class="scoframe" name="main" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></iframe>
235             </noscript>
236         <?php
237     }
238 } else {
239     echo $OUTPUT->box(get_string('noprerequisites', 'scorm'));
241 ?>
242     </div> <!-- SCORM page -->
243 <?php
244 $scoes = scorm_get_toc_object($USER, $scorm, $currentorg, $sco->id, $mode, $attempt);
245 $adlnav = scorm_get_adlnav_json($scoes['scoes']);
247 if (empty($scorm->popup) || $displaymode == 'popup') {
248     if (!isset($result->toctitle)) {
249         $result->toctitle = get_string('toc', 'scorm');
250     }
251     $jsmodule = array(
252         'name' => 'mod_scorm',
253         'fullpath' => '/mod/scorm/module.js',
254         'requires' => array('json'),
255     );
256     $scorm->nav = intval($scorm->nav);
257     $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->nav, $scorm->navpositionleft, $scorm->navpositiontop, $scorm->hidetoc,
258                                                             $collapsetocwinsize, $result->toctitle, $name, $sco->id, $adlnav), false, $jsmodule);
260 if (!empty($forcejs)) {
261     echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
263 echo $OUTPUT->footer();