Merge branch 'm20_MDL-26201_forcejs' of git://github.com/danmarsden/moodle
[moodle.git] / mod / scorm / player.php
1 <?PHP
3 /// This page prints a particular instance of aicc/scorm package
5     require_once('../../config.php');
6     require_once($CFG->dirroot.'/mod/scorm/locallib.php');
7     require_once($CFG->libdir . '/completionlib.php');
9     //
10     // Checkin' script parameters
11     //
12     $id = optional_param('cm', '', PARAM_INT);       // Course Module ID, or
13     $a = optional_param('a', '', PARAM_INT);         // scorm ID
14     $scoid = required_param('scoid', PARAM_INT);  // sco ID
15     $mode = optional_param('mode', 'normal', PARAM_ALPHA); // navigation mode
16     $currentorg = optional_param('currentorg', '', PARAM_RAW); // selected organization
17     $newattempt = optional_param('newattempt', 'off', PARAM_ALPHA); // the user request to start a new attempt
19     //IE 6 Bug workaround
20     if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false && ini_get('zlib.output_compression') == 'On') {
21         ini_set('zlib.output_compression', 'Off');
22     }
24     if (!empty($id)) {
25         if (! $cm = get_coursemodule_from_id('scorm', $id)) {
26             print_error('invalidcoursemodule');
27         }
28         if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
29             print_error('coursemisconf');
30         }
31         if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
32             print_error('invalidcoursemodule');
33         }
34     } else if (!empty($a)) {
35         if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
36             print_error('invalidcoursemodule');
37         }
38         if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
39             print_error('coursemisconf');
40         }
41         if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
42             print_error('invalidcoursemodule');
43         }
44     } else {
45         print_error('missingparameter');
46     }
48     $url = new moodle_url('/mod/scorm/player.php', array('scoid'=>$scoid, 'cm'=>$cm->id));
49     if ($mode !== 'normal') {
50         $url->param('mode', $mode);
51     }
52     if ($currentorg !== '') {
53         $url->param('currentorg', $currentorg);
54     }
55     if ($newattempt !== 'off') {
56         $url->param('newattempt', $newattempt);
57     }
58     $PAGE->set_url($url);
59     $forcejs = get_config('scorm','forcejavascript');
60     if (!empty($forcejs)) {
61         $PAGE->add_body_class('forcejavascript');
62     }
64     require_login($course->id, false, $cm);
66     $strscorms = get_string('modulenameplural', 'scorm');
67     $strscorm  = get_string('modulename', 'scorm');
68     $strpopup = get_string('popup','scorm');
69     $strexit = get_string('exitactivity','scorm');
71     $pagetitle = strip_tags("$course->shortname: ".format_string($scorm->name));
72     $PAGE->set_title($pagetitle);
73     $PAGE->set_heading($course->fullname);
75     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE,$course->id))) {
76         echo $OUTPUT->header();
77         notice(get_string("activityiscurrentlyhidden"));
78         echo $OUTPUT->footer();
79         die;
80     }
82     //check if scorm closed
83     $timenow = time();
84     if ($scorm->timeclose !=0) {
85         if ($scorm->timeopen > $timenow) {
86             echo $OUTPUT->header();
87             echo $OUTPUT->box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "generalbox boxaligncenter");
88             echo $OUTPUT->footer();
89             die;
90         } elseif ($timenow > $scorm->timeclose) {
91             echo $OUTPUT->header();
92             echo $OUTPUT->box(get_string("expired", "scorm", userdate($scorm->timeclose)), "generalbox boxaligncenter");
93             echo $OUTPUT->footer();
94             die;
95         }
96     }
98     //
99     // TOC processing
100     //
101     $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR));   // Just to be safe
102     if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
103         $scorm->version = 'scorm_12';
104     }
105     require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
106     $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
107     if (($newattempt=='on') && (($attempt < $scorm->maxattempt) || ($scorm->maxattempt == 0))) {
108         $attempt++;
109         $mode = 'normal';
110     }
111     $attemptstr = '&amp;attempt=' . $attempt;
113     $result = scorm_get_toc($USER, $scorm, $cm->id, TOCJSLINK, $currentorg, $scoid, $mode, $attempt, true, true);
114     $sco = $result->sco;
116     if (($mode == 'browse') && ($scorm->hidebrowse == 1)) {
117        $mode = 'normal';
118     }
119     if ($mode != 'browse') {
120         if ($trackdata = scorm_get_tracks($sco->id,$USER->id,$attempt)) {
121             if (($trackdata->status == 'completed') || ($trackdata->status == 'passed') || ($trackdata->status == 'failed')) {
122                 $mode = 'review';
123             } else {
124                 $mode = 'normal';
125             }
126         } else {
127             $mode = 'normal';
128         }
129     }
131     add_to_log($course->id, 'scorm', 'view', "player.php?cm=$cm->id&scoid=$sco->id", "$scorm->id", $cm->id);
134     $scoidstr = '&amp;scoid='.$sco->id;
135     $scoidpop = '&scoid='.$sco->id;
136     $modestr = '&amp;mode='.$mode;
137     if ($mode == 'browse') {
138         $modepop = '&mode='.$mode;
139     } else {
140         $modepop = '';
141     }
142     $orgstr = '&currentorg='.$currentorg;
144     $SESSION->scorm_scoid = $sco->id;
145     $SESSION->scorm_status = 'Not Initialized';
146     $SESSION->scorm_mode = $mode;
147     $SESSION->scorm_attempt = $attempt;
149     //
150     // Print the page header
151     //
152     $bodyscript = '';
153     if ($scorm->popup == 1) {
154         $bodyscript = 'onunload="main.close();"';
155     }
157     $exitlink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'" title="'.$strexit.'">'.$strexit.'</a> ';
159     $PAGE->set_button($exitlink);
161     $PAGE->requires->data_for_js('scormplayerdata', Array('cwidth'=>$scorm->width,'cheight'=>$scorm->height,
162                                                           'popupoptions' => $scorm->options), true);
163     $PAGE->requires->js('/mod/scorm/request.js', true);
164     $PAGE->requires->js('/lib/cookies.js', true);
165     //$PAGE->requires->js('/mod/scorm/loaddatamodel.php?id='.$cm->id.$scoidstr.$modestr.$attemptstr, true);
166     $PAGE->requires->css('/mod/scorm/styles.css');
168     echo $OUTPUT->header();
170     // NEW IMS TOC
171     $PAGE->requires->string_for_js('navigation', 'scorm');
172     $PAGE->requires->string_for_js('toc', 'scorm');
173     $PAGE->requires->string_for_js('hide', 'moodle');
174     $PAGE->requires->string_for_js('show', 'moodle');
175     $PAGE->requires->string_for_js('popupsblocked', 'scorm');
177     $name = false;
179 ?>
180     <div id="scormpage">
181     
182       <div id="tocbox">
183         <div id='scormapi-parent'>
184             <script id="external-scormapi" type="text/JavaScript"></script>
185         </div>
186         <div id="scormtop">
187 <?php
188     if ($result->prerequisites) {
189         if ($scorm->popup != 0) {
190             //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.
191             $linkcourse = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'">' . get_string('finishscormlinkname','scorm') . '</a>';
192             echo $OUTPUT->box(get_string('finishscorm','scorm',$linkcourse), 'generalbox', 'altfinishlink');
193         }
194     }
195 ?>
196         <?php echo $mode == 'browse' ? '<div id="scormmode" class="scorm-left">'.get_string('browsemode','scorm')."</div>\n" : ''; ?>
197         <?php echo $mode == 'review' ? '<div id="scormmode" class="scorm-left">'.get_string('reviewmode','scorm')."</div>\n" : ''; ?>
198             <div id="scormnav" class="scorm-right">
199 <?php
200         if ($scorm->hidetoc == 2) {
201              echo $result->tocmenu;
202         }
203 ?>
204             </div> <!-- Scormnav -->
205         </div> <!-- Scormtop -->
206             <div id="toctree" class="generalbox">
207             <?php echo $result->toc; ?>
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) {
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;
226             echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
227             echo html_writer::script(js_writer::function_call('scorm_openpopup', Array("loadSCO.php?id=".$cm->id.$scoidpop, $name, $scorm->options, $scorm->width, $scorm->height)));
228             ?>
229             <noscript>
230             <!--[if IE]>
231                 <iframe id="main" class="scoframe" name="main" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></iframe>
232             <![endif]-->
233             <!--[if !IE]>
234                 <object id="main" class="scoframe" type="text/html" data="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></object>
235             <![endif]-->
236             </noscript>
237 <?php
238         }
239     } else {
240         echo $OUTPUT->box(get_string('noprerequisites','scorm'));
241     }
242 ?>
243     </div> <!-- SCORM page -->
244 <?php 
245 // NEW IMS TOC
246 if (!isset($result->toctitle)) {
247     $result->toctitle = get_string('toc', 'scorm');
250 $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->hidenav, $scorm->hidetoc, $result->toctitle, $name, $sco->id));
253 $completion=new completion_info($course);
254 $completion->set_module_viewed($cm);
256 if (!empty($forcejs)) {
257     echo $OUTPUT->box(get_string("forcejavascriptmessage", "scorm"), "generalbox boxaligncenter forcejavascriptmessage");
259 echo $OUTPUT->footer();