90dc15992f42251d826d723c5a6e38fc765509ba
[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 }
67 $url = new moodle_url('/mod/scorm/player.php', array('scoid'=>$scoid, 'cm'=>$cm->id));
68 if ($mode !== 'normal') {
69     $url->param('mode', $mode);
70 }
71 if ($currentorg !== '') {
72     $url->param('currentorg', $currentorg);
73 }
74 if ($newattempt !== 'off') {
75     $url->param('newattempt', $newattempt);
76 }
77 $PAGE->set_url($url);
78 $forcejs = get_config('scorm', 'forcejavascript');
79 if (!empty($forcejs)) {
80     $PAGE->add_body_class('forcejavascript');
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');
129 $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
130 if (($newattempt=='on') && (($attempt < $scorm->maxattempt) || ($scorm->maxattempt == 0))) {
131     $attempt++;
132     $mode = 'normal';
134 $attemptstr = '&amp;attempt=' . $attempt;
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 if (($mode == 'browse') && ($scorm->hidebrowse == 1)) {
146     $mode = 'normal';
148 if ($mode != 'browse') {
149     if ($trackdata = scorm_get_tracks($sco->id, $USER->id, $attempt)) {
150         if (($trackdata->status == 'completed') || ($trackdata->status == 'passed') || ($trackdata->status == 'failed')) {
151             $mode = 'review';
152         } else {
153             $mode = 'normal';
154         }
155     } else {
156         $mode = 'normal';
157     }
160 add_to_log($course->id, 'scorm', 'view', "player.php?cm=$cm->id&scoid=$sco->id", "$scorm->id", $cm->id);
163 $scoidstr = '&amp;scoid='.$sco->id;
164 $scoidpop = '&scoid='.$sco->id;
165 $modestr = '&amp;mode='.$mode;
166 if ($mode == 'browse') {
167     $modepop = '&mode='.$mode;
168 } else {
169     $modepop = '';
171 $orgstr = '&currentorg='.$currentorg;
173 $SESSION->scorm = new stdClass();
174 $SESSION->scorm->scoid = $sco->id;
175 $SESSION->scorm->scormstatus = 'Not Initialized';
176 $SESSION->scorm->scormmode = $mode;
177 $SESSION->scorm->attempt = $attempt;
179 // Mark module viewed
180 $completion = new completion_info($course);
181 $completion->set_module_viewed($cm);
183 // Print the page header
184 if (empty($scorm->popup) || $displaymode=='popup') {
185     $exitlink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'" title="'.$strexit.'">'.$strexit.'</a> ';
186     $PAGE->set_button($exitlink);
189 $PAGE->requires->data_for_js('scormplayerdata', Array('launch' => false,
190                                                        'currentorg' => '',
191                                                        'sco' => 0,
192                                                        'scorm' => 0,
193                                                        'courseid' => $scorm->course,
194                                                        'cwidth' => $scorm->width,
195                                                        'cheight' => $scorm->height,
196                                                        'popupoptions' => $scorm->options), true);
197 $PAGE->requires->js('/mod/scorm/request.js', true);
198 $PAGE->requires->js('/lib/cookies.js', true);
199 $PAGE->requires->css('/mod/scorm/styles.css');
200 echo $OUTPUT->header();
202 // NEW IMS TOC
203 $PAGE->requires->string_for_js('navigation', 'scorm');
204 $PAGE->requires->string_for_js('toc', 'scorm');
205 $PAGE->requires->string_for_js('hide', 'moodle');
206 $PAGE->requires->string_for_js('show', 'moodle');
207 $PAGE->requires->string_for_js('popupsblocked', 'scorm');
209 $name = false;
211 ?>
212     <div id="scormpage">
214       <div id="tocbox">
215         <div id='scormapi-parent'>
216             <script id="external-scormapi" type="text/JavaScript"></script>
217         </div>
218 <?php
219 if ($scorm->hidetoc == SCORM_TOC_POPUP or $mode=='browse' or $mode=='review') {
220     echo '<div id="scormtop">';
221     echo $mode == 'browse' ? '<div id="scormmode" class="scorm-left">'.get_string('browsemode', 'scorm')."</div>\n" : '';
222     echo $mode == 'review' ? '<div id="scormmode" class="scorm-left">'.get_string('reviewmode', 'scorm')."</div>\n" : '';
223     if ($scorm->hidetoc == SCORM_TOC_POPUP) {
224         echo '<div id="scormnav" class="scorm-right">'.$result->tocmenu.'</div>';
225     }
226     echo '</div>';
228 ?>
229             <div id="toctree">
230                 <?php
231                 if (empty($scorm->popup) || $displaymode == 'popup') {
232                     echo $result->toc;
233                 } else {
234                     //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.
235                     $linkcourse = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'">' . get_string('finishscormlinkname', 'scorm') . '</a>';
236                     echo $OUTPUT->box(get_string('finishscorm', 'scorm', $linkcourse), 'generalbox', 'altfinishlink');
237                 }?>
238             </div> <!-- toctree -->
239         </div> <!--  tocbox -->
240                 <noscript>
241                     <div id="noscript">
242                         <?php print_string('noscriptnoscorm', 'scorm'); // No Martin(i), No Party ;-) ?>
244                     </div>
245                 </noscript>
246 <?php
247 if ($result->prerequisites) {
248     if ($scorm->popup != 0 && $displaymode !=='popup') {
249         // Clean the name for the window as IE is fussy
250         $name = preg_replace("/[^A-Za-z0-9]/", "", $scorm->name);
251         if (!$name) {
252             $name = 'DefaultPlayerWindow';
253         }
254         $name = 'scorm_'.$name;
255         echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
256         $url = new moodle_url($PAGE->url, array('scoid' => $sco->id, 'display' => 'popup'));
257         echo html_writer::script(
258             js_writer::function_call('scorm_openpopup', Array($url->out(false),
259                                                        $name, $scorm->options,
260                                                        $scorm->width, $scorm->height)));
261         ?>
262             <noscript>
263             <!--[if IE]>
264                 <iframe id="main" class="scoframe" name="main" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></iframe>
265             <![endif]-->
266             <!--[if !IE]>
267                 <object id="main" class="scoframe" type="text/html" data="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></object>
268             <![endif]-->
269             </noscript>
270         <?php
271     }
272 } else {
273     echo $OUTPUT->box(get_string('noprerequisites', 'scorm'));
275 ?>
276     </div> <!-- SCORM page -->
277 <?php
278 $scoes = scorm_get_toc_object($USER, $scorm, "", $sco->id, $mode, $attempt);
279 $adlnav = scorm_get_adlnav_json($scoes['scoes']);
280 // NEW IMS TOC
281 if (empty($scorm->popup) || $displaymode == 'popup') {
282     if (!isset($result->toctitle)) {
283         $result->toctitle = get_string('toc', 'scorm');
284     }
285     $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->hidenav, $scorm->hidetoc, $result->toctitle, $name, $sco->id, $adlnav));
287 if (!empty($forcejs)) {
288     echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
290 echo $OUTPUT->footer();