2d1096aa44d8e64396c3eb55e1f6249ba0fb3943
[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->js('/mod/scorm/rd.js', true);
161     echo $OUTPUT->header();
163     $PAGE->requires->js_function_call('attach_resize_event');
164     if (($sco->previd != 0) && ((!isset($sco->previous)) || ($sco->previous == 0))) {
165         $scostr = '&scoid='.$sco->previd;
166         $PAGE->requires->js_function_call('scorm_set_prev', Array($CFG->wwwroot.'/mod/scorm/player.php?cm='.$cm->id.$orgstr.$modepop.$scostr));
167     } else {
168         $PAGE->requires->js_function_call('scorm_set_prev', Array($CFG->wwwroot.'/mod/scorm/view.php?id='.$cm->id));
169     }
170     if (($sco->nextid != 0) && ((!isset($sco->next)) || ($sco->next == 0))) {
171         $scostr = '&scoid='.$sco->nextid;
172         $PAGE->requires->js_function_call('scorm_set_next', Array($CFG->wwwroot.'/mod/scorm/player.php?cm='.$cm->id.$orgstr.$modepop.$scostr));
173     } else {
174         $PAGE->requires->js_function_call('scorm_set_next', Array($CFG->wwwroot.'/mod/scorm/view.php?id='.$cm->id));
175     }
176 ?>
177     <div id="scormpage">
178 <?php
179     if ($scorm->hidetoc == 0) {
180 ?>
181         <div id="tocbox">
182 <?php
183         if ($scorm->hidenav ==0){
184 ?>
185             <!-- Bottons nav at left-->
186             <div id="tochead">
187                 <form name="tochead" method="post" action="player.php?cm=<?php echo $cm->id ?>" target="_top">
188 <?php
189             $orgstr = '&amp;currentorg='.$currentorg;
190             if (($scorm->hidenav == 0) && ($sco->previd != 0) && (!isset($sco->previous) || $sco->previous == 0)) {
191                 // Print the prev LO button
192                 $scostr = '&amp;scoid='.$sco->previd;
193                 $url = $CFG->wwwroot.'/mod/scorm/player.php?cm='.$cm->id.$orgstr.$modestr.$scostr;
194 ?>
195                     <input name="prev" type="button" value="<?php print_string('prev','scorm') ?>" onClick="document.location.href=' <?php echo $url; ?> '"/>
196 <?php
197             }
198             if (($scorm->hidenav == 0) && ($sco->nextid != 0) && (!isset($sco->next) || $sco->next == 0)) {
199                 // Print the next LO button
200                 $scostr = '&amp;scoid='.$sco->nextid;
201                 $url = $CFG->wwwroot.'/mod/scorm/player.php?cm='.$cm->id.$orgstr.$modestr.$scostr;
202 ?>
203                     <input name="next" type="button" value="<?php print_string('next','scorm') ?>" onClick="document.location.href=' <?php echo $url; ?> '"/>
204 <?php
205             }
206 ?>
207                 </form>
208             </div> <!-- tochead -->
209 <?php
210         }
211 ?>
212             <div id="toctree" class="generalbox">
213             <?php echo $result->toc; ?>
214             </div> <!-- toctree -->
215         </div> <!--  tocbox -->
216 <?php
217         $class = ' class="toc"';
218     } else {
219         $class = ' class="no-toc"';
220     }
221 ?>
222         <div id="scormbox"<?php echo $class; if(($scorm->hidetoc == 2) || ($scorm->hidetoc == 1)){echo 'style="width:100%"';}?>>
223 <?php
224     // This very big test check if is necessary the "scormtop" div
225     if (
226            ($mode != 'normal') ||  // We are not in normal mode so review or browse text will displayed
227            (
228                ($scorm->hidenav == 0) &&  // Teacher want to display navigation links
229                ($scorm->hidetoc != 0) &&  // The buttons has not been displayed
230                (
231                    (
232                        ($sco->previd != 0) &&  // This is not the first learning object of the package
233                        ((!isset($sco->previous)) || ($sco->previous == 0))   // Moodle must manage the previous link
234                    ) ||
235                    (
236                        ($sco->nextid != 0) &&  // This is not the last learning object of the package
237                        ((!isset($sco->next)) || ($sco->next == 0))       // Moodle must manage the next link
238                    )
239                )
240            ) || ($scorm->hidetoc == 2)      // Teacher want to display toc in a small dropdown menu
241        ) {
242 ?>
243             <div id="scormtop">
244         <?php echo $mode == 'browse' ? '<div id="scormmode" class="scorm-left">'.get_string('browsemode','scorm')."</div>\n" : ''; ?>
245         <?php echo $mode == 'review' ? '<div id="scormmode" class="scorm-left">'.get_string('reviewmode','scorm')."</div>\n" : ''; ?>
246 <?php
247         if (($scorm->hidenav == 0) || ($scorm->hidetoc == 2) || ($scorm->hidetoc == 1)) {
248 ?>
249                 <div id="scormnav" class="scorm-right">
250         <?php
251             $orgstr = '&amp;currentorg='.$currentorg;
252             if (($scorm->hidenav == 0) && ($sco->previd != 0) && (!isset($sco->previous) || $sco->previous == 0) && (($scorm->hidetoc == 2) || ($scorm->hidetoc == 1)) ) {
253                 // Print the prev LO button
254                 $scostr = '&amp;scoid='.$sco->previd;
255                 $url = $CFG->wwwroot.'/mod/scorm/player.php?cm='.$cm->id.$orgstr.$modestr.$scostr;
256 ?>
257                     <form name="scormnavprev" method="post" action="player.php?cm=<?php echo $cm->id ?>" target="_top" style= "display:inline">
258                         <input name="prev" type="button" value="<?php print_string('prev','scorm') ?>" onClick="document.location.href=' <?php echo $url; ?> '"/>
259                     </form>
260 <?php
261             }
262             if ($scorm->hidetoc == 2) {
263                 echo $result->tocmenu;
264             }
265             if (($scorm->hidenav == 0) && ($sco->nextid != 0) && (!isset($sco->next) || $sco->next == 0) && (($scorm->hidetoc == 2) || ($scorm->hidetoc == 1))) {
266                 // Print the next LO button
267                 $scostr = '&amp;scoid='.$sco->nextid;
268                 $url = $CFG->wwwroot.'/mod/scorm/player.php?cm='.$cm->id.$orgstr.$modestr.$scostr;
269 ?>
270                     <form name="scormnavnext" method="post" action="player.php?cm=<?php echo $cm->id ?>" target="_top" style= "display:inline">
271                         <input name="next" type="button" value="<?php print_string('next','scorm') ?>" onClick="document.location.href=' <?php echo $url; ?> '"/>
272                     </form>
273 <?php
274             }
275         ?>
276                 </div>
277 <?php
278         }
279 ?>
280             </div> <!-- Scormtop -->
281 <?php
282     } // The end of the very big test
283 ?>
284             <div id="scormobject" class="scorm-right">
285                 <noscript>
286                     <div id="noscript">
287                         <?php print_string('noscriptnoscorm','scorm'); // No Martin(i), No Party ;-) ?>
289                     </div>
290                 </noscript>
291 <?php
292     if ($result->prerequisites) {
293         if ($scorm->popup == 0) {
294             $fullurl="loadSCO.php?id=".$cm->id.$scoidstr.$modestr;
295             ?>
296             <!--[if IE]>
297                 <iframe id="scoframe1" class="scoframe" name="scoframe1" src="<?php echo $fullurl; ?>"></iframe>
298             <![endif]-->
299             <![if !IE]>
300                 <object id="scoframe1" class="scoframe" type="text/html" data="<?php echo $fullurl; ?>"></object>
301             <![endif]>
303 <?php
304             if (scorm_debugging($scorm)) {
305 ?>
306                 <script>
307                 // Add in a JS controlled link for toggling the Debug logging
308                 var logButton = document.createElement('a');
309                 logButton.id = 'mod-scorm-log-toggle';
310                 logButton.name = 'logToggle';
311                 logButton.href = 'javascript:toggleLog();';
312                 if (getLoggingActive() == "A") {
313                     logButton.innerHTML = '<?php print_string('scormloggingon','scorm') ?>';
314                 } else {
315                     logButton.innerHTML = '<?php print_string('scormloggingoff','scorm') ?>';
316                 }
317                 var content = safeGetElement(document, 'scormpage');
318                 content.insertBefore(logButton, content.firstChild);
319                 </script>
320 <?php
321             }
322             $PAGE->requires->js_function_call('scorm_resize');
323         } else {
324             // Clean the name for the window as IE is fussy
325             $name = preg_replace("/[^A-Za-z0-9]/", "", $scorm->name);
326             if (!$name) {
327                 $name = 'DefaultPlayerWindow';
328             }
329             $name = 'scorm_'.$name;
331             echo html_writer::script(js_writer::function_call('scorm_resize'));
332             echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
333             echo html_writer::script(js_writer::function_call('scorm_openpopup', Array("loadSCO.php?id=".$cm->id.$scoidpop, $name, $scorm->options, $scorm->width, $scorm->height)));
334             ?>
335             <noscript>
336             <!--[if IE]>
337                 <iframe id="main" class="scoframe" name="main" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></iframe>
338             <![endif]-->
339             <!--[if !IE]>
340                 <object id="main" class="scoframe" type="text/html" data="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></object>
341             <![endif]-->
342             </noscript>
343 <?php
344             //Added incase javascript popups are blocked
345             $link = '<a href="'.$CFG->wwwroot.'/mod/scorm/loadSCO.php?id='.$cm->id.$scoidstr.$modestr.'" target="_blank">'.get_string('popupblockedlinkname','scorm').'</a>';
346             echo $OUTPUT->box(get_string('popupblocked','scorm',$link), 'generalbox', 'altpopuplink');
347             $linkcourse = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'">' . get_string('finishscormlinkname','scorm') . '</a>';
348             echo $OUTPUT->box(get_string('finishscorm','scorm',$linkcourse), 'generalbox', 'altfinishlink');
349         }
350     } else {
351         echo $OUTPUT->box(get_string('noprerequisites','scorm'));
352     }
353 ?>
354             </div> <!-- SCORM object -->
355         </div> <!-- SCORM box  -->
356     </div> <!-- SCORM page -->
357 <?php 
358 $completion=new completion_info($course);
359 $completion->set_module_viewed($cm);
361 echo $OUTPUT->footer();