af2ed90c6a99294a9c68bbd1e38b74e0a7242745
[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 // IE 9 workaround for Flash bug: MDL-29213
32 // Note that it's not clear if appending the meta tag via $CFG->additionalhtmlhead
33 // is correct at all, both because of the mechanism itself and because MS says
34 // the tag must be used *before* including other stuff. See the issue for more info.
35 // TODO: Once we implement some way to inject meta tags, change this to use it. MDL-30039
36 if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 9') !== false) {
37     if (!isset($CFG->additionalhtmlhead)) { //check to make sure set first - that way we can use .=
38         $CFG->additionalhtmlhead = '';
39     }
40     $CFG->additionalhtmlhead .= '<meta http-equiv="X-UA-Compatible" content="IE=8" />';
41 }
43 if (!empty($id)) {
44     if (! $cm = get_coursemodule_from_id('scorm', $id)) {
45         print_error('invalidcoursemodule');
46     }
47     if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
48         print_error('coursemisconf');
49     }
50     if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
51         print_error('invalidcoursemodule');
52     }
53 } else if (!empty($a)) {
54     if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
55         print_error('invalidcoursemodule');
56     }
57     if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
58         print_error('coursemisconf');
59     }
60     if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
61         print_error('invalidcoursemodule');
62     }
63 } else {
64     print_error('missingparameter');
65 }
66 // If new attempt is being triggered set normal mode and increment attempt number.
67 $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
69 // Check mode is correct and set mode/attempt (uses pass by reference).
70 scorm_check_mode($scorm, $newattempt, $attempt, $USER->id, $mode);
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('popup');
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', $coursecontext)) {
113     echo $OUTPUT->header();
114     notice(get_string("activityiscurrentlyhidden"));
115     echo $OUTPUT->footer();
116     die;
119 // Check if scorm closed.
120 $timenow = time();
121 if ($scorm->timeclose !=0) {
122     if ($scorm->timeopen > $timenow) {
123         echo $OUTPUT->header();
124         echo $OUTPUT->box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "generalbox boxaligncenter");
125         echo $OUTPUT->footer();
126         die;
127     } else if ($timenow > $scorm->timeclose) {
128         echo $OUTPUT->header();
129         echo $OUTPUT->box(get_string("expired", "scorm", userdate($scorm->timeclose)), "generalbox boxaligncenter");
130         echo $OUTPUT->footer();
132         die;
133     }
135 // TOC processing
136 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR));   // Just to be safe.
137 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
138     $scorm->version = 'scorm_12';
140 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
142 $result = scorm_get_toc($USER, $scorm, $cm->id, TOCJSLINK, $currentorg, $scoid, $mode, $attempt, true, true);
143 $sco = $result->sco;
144 if ($scorm->lastattemptlock == 1 && $result->attemptleft == 0) {
145     echo $OUTPUT->header();
146     echo $OUTPUT->notification(get_string('exceededmaxattempts', 'scorm'));
147     echo $OUTPUT->footer();
148     exit;
151 $scoidstr = '&amp;scoid='.$sco->id;
152 $modestr = '&amp;mode='.$mode;
154 $SESSION->scorm = new stdClass();
155 $SESSION->scorm->scoid = $sco->id;
156 $SESSION->scorm->scormstatus = 'Not Initialized';
157 $SESSION->scorm->scormmode = $mode;
158 $SESSION->scorm->attempt = $attempt;
160 // Mark module viewed.
161 $completion = new completion_info($course);
162 $completion->set_module_viewed($cm);
164 // Print the page header.
165 if (empty($scorm->popup) || $displaymode=='popup') {
166     $exitlink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'" title="'.$strexit.'">'.$strexit.'</a> ';
167     $PAGE->set_button($exitlink);
170 $PAGE->requires->data_for_js('scormplayerdata', Array('launch' => false,
171                                                        'currentorg' => '',
172                                                        'sco' => 0,
173                                                        'scorm' => 0,
174                                                        'courseid' => $scorm->course,
175                                                        'cwidth' => $scorm->width,
176                                                        'cheight' => $scorm->height,
177                                                        'popupoptions' => $scorm->options), true);
178 $PAGE->requires->js('/mod/scorm/request.js', true);
179 $PAGE->requires->js('/lib/cookies.js', true);
180 echo $OUTPUT->header();
181 echo $OUTPUT->heading(format_string($scorm->name));
183 $PAGE->requires->string_for_js('navigation', 'scorm');
184 $PAGE->requires->string_for_js('toc', 'scorm');
185 $PAGE->requires->string_for_js('hide', 'moodle');
186 $PAGE->requires->string_for_js('show', 'moodle');
187 $PAGE->requires->string_for_js('popupsblocked', 'scorm');
189 $name = false;
191 ?>
192     <div id="scormpage">
194       <div id="tocbox">
195         <div id='scormapi-parent'>
196             <script id="external-scormapi" type="text/JavaScript"></script>
197         </div>
198 <?php
199 if ($scorm->hidetoc == SCORM_TOC_POPUP or $mode=='browse' or $mode=='review') {
200     echo '<div id="scormtop">';
201     echo $mode == 'browse' ? '<div id="scormmode" class="scorm-left">'.get_string('browsemode', 'scorm')."</div>\n" : '';
202     echo $mode == 'review' ? '<div id="scormmode" class="scorm-left">'.get_string('reviewmode', 'scorm')."</div>\n" : '';
203     if ($scorm->hidetoc == SCORM_TOC_POPUP) {
204         echo '<div id="scormnav" class="scorm-right">'.$result->tocmenu.'</div>';
205     }
206     echo '</div>';
208 ?>
209             <div id="toctree">
210                 <?php
211                 if (empty($scorm->popup) || $displaymode == 'popup') {
212                     echo $result->toc;
213                 } else {
214                     //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.
215                     $linkcourse = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'">' . get_string('finishscormlinkname', 'scorm') . '</a>';
216                     echo $OUTPUT->box(get_string('finishscorm', 'scorm', $linkcourse), 'generalbox', 'altfinishlink');
217                 }?>
218             </div> <!-- toctree -->
219         </div> <!--  tocbox -->
220                 <noscript>
221                     <div id="noscript">
222                         <?php print_string('noscriptnoscorm', 'scorm'); // No Martin(i), No Party ;-) ?>
224                     </div>
225                 </noscript>
226 <?php
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         ?>
242             <noscript>
243                 <iframe id="main" class="scoframe" name="main" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></iframe>
244             </noscript>
245         <?php
246     }
247 } else {
248     echo $OUTPUT->box(get_string('noprerequisites', 'scorm'));
250 ?>
251     </div> <!-- SCORM page -->
252 <?php
253 $scoes = scorm_get_toc_object($USER, $scorm, $currentorg, $sco->id, $mode, $attempt);
254 $adlnav = scorm_get_adlnav_json($scoes['scoes']);
256 if (empty($scorm->popup) || $displaymode == 'popup') {
257     if (!isset($result->toctitle)) {
258         $result->toctitle = get_string('toc', 'scorm');
259     }
260     $jsmodule = array(
261         'name' => 'mod_scorm',
262         'fullpath' => '/mod/scorm/module.js',
263         'requires' => array('json'),
264     );
265     $scorm->nav = intval($scorm->nav);
266     $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->nav, $scorm->navpositionleft, $scorm->navpositiontop, $scorm->hidetoc,
267                                                             $collapsetocwinsize, $result->toctitle, $name, $sco->id, $adlnav), false, $jsmodule);
269 if (!empty($forcejs)) {
270     echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
272 echo $OUTPUT->footer();