SCORM MDL-22951 New player for SCORM fixes major regression that prevents some SCORM...
[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     //
8     // Checkin' script parameters
9     //
10     $id = optional_param('cm', '', PARAM_INT);       // Course Module ID, or
11     $a = optional_param('a', '', PARAM_INT);         // scorm ID
12     $scoid = required_param('scoid', PARAM_INT);  // sco ID
13     $mode = optional_param('mode', 'normal', PARAM_ALPHA); // navigation mode
14     $currentorg = optional_param('currentorg', '', PARAM_RAW); // selected organization
15     $newattempt = optional_param('newattempt', 'off', PARAM_ALPHA); // the user request to start a new attempt
17     //IE 6 Bug workaround
18     if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false && ini_get('zlib.output_compression') == 'On') {
19         ini_set('zlib.output_compression', 'Off');
20     }
22     if (!empty($id)) {
23         if (! $cm = get_coursemodule_from_id('scorm', $id)) {
24             print_error('invalidcoursemodule');
25         }
26         if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
27             print_error('coursemisconf');
28         }
29         if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
30             print_error('invalidcoursemodule');
31         }
32     } else if (!empty($a)) {
33         if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
34             print_error('invalidcoursemodule');
35         }
36         if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
37             print_error('coursemisconf');
38         }
39         if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
40             print_error('invalidcoursemodule');
41         }
42     } else {
43         print_error('missingparameter');
44     }
46     $url = new moodle_url('/mod/scorm/player.php', array('scoid'=>$scoid, 'cm'=>$cm->id));
47     if ($mode !== 'normal') {
48         $url->param('mode', $mode);
49     }
50     if ($currentorg !== '') {
51         $url->param('currentorg', $currentorg);
52     }
53     if ($newattempt !== 'off') {
54         $url->param('newattempt', $newattempt);
55     }
56     $PAGE->set_url($url);
58     require_login($course->id, false, $cm);
60     $strscorms = get_string('modulenameplural', 'scorm');
61     $strscorm  = get_string('modulename', 'scorm');
62     $strpopup = get_string('popup','scorm');
63     $strexit = get_string('exitactivity','scorm');
65     $pagetitle = strip_tags("$course->shortname: ".format_string($scorm->name));
66     $PAGE->set_title($pagetitle);
67     $PAGE->set_heading($course->fullname);
69     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE,$course->id))) {
70         echo $OUTPUT->header();
71         notice(get_string("activityiscurrentlyhidden"));
72         echo $OUTPUT->footer();
73         die;
74     }
76     //check if scorm closed
77     $timenow = time();
78     if ($scorm->timeclose !=0) {
79         if ($scorm->timeopen > $timenow) {
80             echo $OUTPUT->header();
81             echo $OUTPUT->box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "generalbox boxaligncenter");
82             echo $OUTPUT->footer();
83             die;
84         } elseif ($timenow > $scorm->timeclose) {
85             echo $OUTPUT->header();
86             echo $OUTPUT->box(get_string("expired", "scorm", userdate($scorm->timeclose)), "generalbox boxaligncenter");
87             echo $OUTPUT->footer();
88             die;
89         }
90     }
92     //
93     // TOC processing
94     //
95     $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR));   // Just to be safe
96     if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
97         $scorm->version = 'scorm_12';
98     }
99     require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
100     $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
101     if (($newattempt=='on') && (($attempt < $scorm->maxattempt) || ($scorm->maxattempt == 0))) {
102         $attempt++;
103         $mode = 'normal';
104     }
105     $attemptstr = '&amp;attempt=' . $attempt;
107     $result = scorm_get_toc($USER,$scorm,'structurelist',$currentorg,$scoid,$mode,$attempt,true);
108     $sco = $result->sco;
110     if (($mode == 'browse') && ($scorm->hidebrowse == 1)) {
111        $mode = 'normal';
112     }
113     if ($mode != 'browse') {
114         if ($trackdata = scorm_get_tracks($sco->id,$USER->id,$attempt)) {
115             if (($trackdata->status == 'completed') || ($trackdata->status == 'passed') || ($trackdata->status == 'failed')) {
116                 $mode = 'review';
117             } else {
118                 $mode = 'normal';
119             }
120         } else {
121             $mode = 'normal';
122         }
123     }
125     add_to_log($course->id, 'scorm', 'view', "player.php?cm=$cm->id&scoid=$sco->id", "$scorm->id", $cm->id);
128     $scoidstr = '&amp;scoid='.$sco->id;
129     $scoidpop = '&scoid='.$sco->id;
130     $modestr = '&amp;mode='.$mode;
131     if ($mode == 'browse') {
132         $modepop = '&mode='.$mode;
133     } else {
134         $modepop = '';
135     }
136     $orgstr = '&currentorg='.$currentorg;
138     $SESSION->scorm_scoid = $sco->id;
139     $SESSION->scorm_status = 'Not Initialized';
140     $SESSION->scorm_mode = $mode;
141     $SESSION->scorm_attempt = $attempt;
143     //
144     // Print the page header
145     //
146     $bodyscript = '';
147     if ($scorm->popup == 1) {
148         $bodyscript = 'onunload="main.close();"';
149     }
151     $exitlink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'" title="'.$strexit.'">'.$strexit.'</a> ';
153     $PAGE->set_button($exitlink);
155     $PAGE->requires->data_for_js('scormplayerdata', Array('cwidth'=>$scorm->width,'cheight'=>$scorm->height), true);
156     $PAGE->requires->js('/mod/scorm/request.js', true);
157     $PAGE->requires->js('/lib/cookies.js', true);
158     //$PAGE->requires->js('/mod/scorm/loaddatamodel.php?id='.$cm->id.$scoidstr.$modestr.$attemptstr, true);
159     $PAGE->requires->css('/mod/scorm/styles.css');
161     echo $OUTPUT->header();
163     // NEW IMS TOC
164     $PAGE->requires->string_for_js('navigation', 'scorm');
165     $PAGE->requires->string_for_js('toc', 'scorm');
166     $PAGE->requires->string_for_js('hide', 'moodle');
167     $PAGE->requires->string_for_js('show', 'moodle');
169     $name = false;
171 ?>
172     <div id="scormpage">
173     
174       <div id="tocbox">
175         <div id='scormapi-parent'>
176             <script id="external-scormapi" type="text/JavaScript"></script>
177         </div>
178         <div id="scormtop">
179 <?php
180     if ($result->prerequisites) {
181         if ($scorm->popup != 0) {
182             $linkcourse = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'">' . get_string('finishscormlinkname','scorm') . '</a>';
183             echo $OUTPUT->box(get_string('finishscorm','scorm',$linkcourse), 'generalbox', 'altfinishlink');
184         }
185     }
186 ?>
187         <?php echo $mode == 'browse' ? '<div id="scormmode" class="scorm-left">'.get_string('browsemode','scorm')."</div>\n" : ''; ?>
188         <?php echo $mode == 'review' ? '<div id="scormmode" class="scorm-left">'.get_string('reviewmode','scorm')."</div>\n" : ''; ?>
189             <div id="scormnav" class="scorm-right">
190 <?php
191         if ($scorm->hidetoc == 2) {
192              echo $result->tocmenu;
193         }
194 ?>
195             </div> <!-- Scormnav -->
196         </div> <!-- Scormtop -->
197             <div id="toctree" class="generalbox">
198             <?php echo $result->toc; ?>
199             </div> <!-- toctree -->
200         </div> <!--  tocbox -->
201                 <noscript>
202                     <div id="noscript">
203                         <?php print_string('noscriptnoscorm','scorm'); // No Martin(i), No Party ;-) ?>
205                     </div>
206                 </noscript>
207 <?php
208     if ($result->prerequisites) {
209         if ($scorm->popup == 0) {
210             if (scorm_debugging($scorm)) {
211 ?>
212                 <script>
213                 // Add in a JS controlled link for toggling the Debug logging
214                 var logButton = document.createElement('a');
215                 logButton.id = 'mod-scorm-log-toggle';
216                 logButton.name = 'logToggle';
217                 logButton.href = 'javascript:toggleLog();';
218                 if (getLoggingActive() == "A") {
219                     logButton.innerHTML = '<?php print_string('scormloggingon','scorm') ?>';
220                 } else {
221                     logButton.innerHTML = '<?php print_string('scormloggingoff','scorm') ?>';
222                 }
223                 var content = safeGetElement(document, 'scormpage');
224                 content.insertBefore(logButton, content.firstChild);
225                 </script>
226 <?php
227             }
228         } else {
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;
236             echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
237             echo html_writer::script(js_writer::function_call('scorm_openpopup', Array("loadSCO.php?id=".$cm->id.$scoidpop, $name, $scorm->options, $scorm->width, $scorm->height)));
238             ?>
239             <noscript>
240             <!--[if IE]>
241                 <iframe id="main" class="scoframe" name="main" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></iframe>
242             <![endif]-->
243             <!--[if !IE]>
244                 <object id="main" class="scoframe" type="text/html" data="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></object>
245             <![endif]-->
246             </noscript>
247 <?php
248             //Added incase javascript popups are blocked
249             $link = '<a href="'.$CFG->wwwroot.'/mod/scorm/loadSCO.php?id='.$cm->id.$scoidstr.$modestr.'" target="_blank">'.get_string('popupblockedlinkname','scorm').'</a>';
250             echo $OUTPUT->box(get_string('popupblocked','scorm',$link), 'generalbox', 'altpopuplink');
251             //$linkcourse = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'">' . get_string('finishscormlinkname','scorm') . '</a>';
252             //echo $OUTPUT->box(get_string('finishscorm','scorm',$linkcourse), 'generalbox', 'altfinishlink');
253         }
254     } else {
255         echo $OUTPUT->box(get_string('noprerequisites','scorm'));
256     }
257 ?>
258     </div> <!-- SCORM page -->
259 <?php 
260 // NEW IMS TOC
261 if (!isset($result->toctitle)) {
262     $result->toctitle = get_string('toc', 'scorm');
265 $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->hidenav, $scorm->hidetoc, $result->toctitle, $name, $scorm->options, $scorm->width, $scorm->height, $sco->id));
268 $completion=new completion_info($course);
269 $completion->set_module_viewed($cm);
271 echo $OUTPUT->footer();