MDL-40258: Fixed invalid json string error in file-picker
[moodle.git] / mod / scorm / player.php
blobecca604dcca53e2dbe7bd1530f657a987537d13c
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.
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 = '';
40 $CFG->additionalhtmlhead .= '<meta http-equiv="X-UA-Compatible" content="IE=8" />';
43 if (!empty($id)) {
44 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
45 print_error('invalidcoursemodule');
47 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
48 print_error('coursemisconf');
50 if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
51 print_error('invalidcoursemodule');
53 } else if (!empty($a)) {
54 if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
55 print_error('invalidcoursemodule');
57 if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
58 print_error('coursemisconf');
60 if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
61 print_error('invalidcoursemodule');
63 } else {
64 print_error('missingparameter');
67 $url = new moodle_url('/mod/scorm/player.php', array('scoid'=>$scoid, 'cm'=>$cm->id));
68 if ($mode !== 'normal') {
69 $url->param('mode', $mode);
71 if ($currentorg !== '') {
72 $url->param('currentorg', $currentorg);
74 if ($newattempt !== 'off') {
75 $url->param('newattempt', $newattempt);
77 $PAGE->set_url($url);
78 $forcejs = get_config('scorm', 'forcejavascript');
79 if (!empty($forcejs)) {
80 $PAGE->add_body_class('forcejavascript');
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 = get_context_instance(CONTEXT_COURSE, $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);
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;
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';
155 } else {
156 $mode = 'normal';
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->yui2_lib('connection');
190 $PAGE->requires->data_for_js('scormplayerdata', Array('cwidth'=>$scorm->width,
191 'cheight'=>$scorm->height,
192 'popupoptions' => $scorm->options), true);
193 $PAGE->requires->js('/mod/scorm/request.js', true);
194 $PAGE->requires->js('/lib/cookies.js', true);
195 echo $OUTPUT->header();
197 // NEW IMS TOC
198 $PAGE->requires->string_for_js('navigation', 'scorm');
199 $PAGE->requires->string_for_js('toc', 'scorm');
200 $PAGE->requires->string_for_js('hide', 'moodle');
201 $PAGE->requires->string_for_js('show', 'moodle');
202 $PAGE->requires->string_for_js('popupsblocked', 'scorm');
204 $name = false;
207 <div id="scormpage">
209 <div id="tocbox">
210 <div id='scormapi-parent'>
211 <script id="external-scormapi" type="text/JavaScript"></script>
212 </div>
213 <?php
214 if ($scorm->hidetoc == SCORM_TOC_POPUP or $mode=='browse' or $mode=='review') {
215 echo '<div id="scormtop">';
216 echo $mode == 'browse' ? '<div id="scormmode" class="scorm-left">'.get_string('browsemode', 'scorm')."</div>\n" : '';
217 echo $mode == 'review' ? '<div id="scormmode" class="scorm-left">'.get_string('reviewmode', 'scorm')."</div>\n" : '';
218 if ($scorm->hidetoc == SCORM_TOC_POPUP) {
219 echo '<div id="scormnav" class="scorm-right">'.$result->tocmenu.'</div>';
221 echo '</div>';
224 <div id="toctree">
225 <?php
226 if (empty($scorm->popup) || $displaymode == 'popup') {
227 echo $result->toc;
228 } else {
229 //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.
230 $linkcourse = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'">' . get_string('finishscormlinkname', 'scorm') . '</a>';
231 echo $OUTPUT->box(get_string('finishscorm', 'scorm', $linkcourse), 'generalbox', 'altfinishlink');
233 </div> <!-- toctree -->
234 </div> <!-- tocbox -->
235 <noscript>
236 <div id="noscript">
237 <?php print_string('noscriptnoscorm', 'scorm'); // No Martin(i), No Party ;-) ?>
239 </div>
240 </noscript>
241 <?php
242 if ($result->prerequisites) {
243 if ($scorm->popup != 0 && $displaymode !=='popup') {
244 // Clean the name for the window as IE is fussy
245 $name = preg_replace("/[^A-Za-z0-9]/", "", $scorm->name);
246 if (!$name) {
247 $name = 'DefaultPlayerWindow';
249 $name = 'scorm_'.$name;
250 echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
251 $url = new moodle_url($PAGE->url, array('scoid' => $sco->id, 'display' => 'popup'));
252 echo html_writer::script(
253 js_writer::function_call('scorm_openpopup', Array($url->out(false),
254 $name, $scorm->options,
255 $scorm->width, $scorm->height)));
257 <noscript>
258 <!--[if IE]>
259 <iframe id="main" class="scoframe" name="main" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></iframe>
260 <![endif]-->
261 <!--[if !IE]>
262 <object id="main" class="scoframe" type="text/html" data="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></object>
263 <![endif]-->
264 </noscript>
265 <?php
267 } else {
268 echo $OUTPUT->box(get_string('noprerequisites', 'scorm'));
271 </div> <!-- SCORM page -->
272 <?php
273 // NEW IMS TOC
274 if (empty($scorm->popup) || $displaymode == 'popup') {
275 if (!isset($result->toctitle)) {
276 $result->toctitle = get_string('toc', 'scorm');
278 $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->hidenav, $scorm->hidetoc, $result->toctitle, $name, $sco->id));
280 if (!empty($forcejs)) {
281 echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
283 echo $OUTPUT->footer();