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