MDL-11892 Propogate Fixes for 'calls to expand javascript' to SCORM 1.2 code. Minor...
[moodle.git] / mod / scorm / player.php
1 <?PHP  // $Id$
3 /// This page prints a particular instance of aicc/scorm package
5     require_once('../../config.php');
6     require_once('locallib.php');
7     //
8     // Checkin' script parameters
9     //
10     $id = optional_param('id', '', 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     if (!empty($id)) {
18         if (! $cm = get_coursemodule_from_id('scorm', $id)) {
19             print_error('invalidcoursemodule');
20         }
21         if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
22             print_error('coursemisconf');
23         }
24         if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
25             print_error('invalidcoursemodule');
26         }
27     } else if (!empty($a)) {
28         if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
29             print_error('invalidcoursemodule');
30         }
31         if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
32             print_error('coursemisconf');
33         }
34         if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
35             print_error('invalidcoursemodule');
36         }
37     } else {
38         print_error('missingparameter');
39     }
41     require_login($course->id, false, $cm);
43     $strscorms = get_string('modulenameplural', 'scorm');
44     $strscorm  = get_string('modulename', 'scorm');
45     $strpopup = get_string('popup','scorm');
46     $strexit = get_string('exitactivity','scorm');
48     $navlinks = array();
49     
50     if ($course->id != SITEID) {
51         if ($scorms = get_all_instances_in_course('scorm', $course)) {
52             // The module SCORM/AICC activity with the first id is the course  
53             $firstscorm = current($scorms);
54             if (!(($course->format == 'scorm') && ($firstscorm->id == $scorm->id))) {
55                 $navlinks[] = array('name' => $strscorms, 'link' => "index.php?id=$course->id", 'type' => 'activity');
56             }
57         }
58     }
60     $pagetitle = strip_tags("$course->shortname: ".format_string($scorm->name));
61     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE,$course->id))) {
62         $navlinks[] = array('name' => format_string($scorm->name,true), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
63         $navigation = build_navigation($navlinks);
64         
65         print_header($pagetitle, $course->fullname, $navigation,
66                  '', '', true, update_module_button($cm->id, $course->id, $strscorm), '', false);
67         notice(get_string("activityiscurrentlyhidden"));
68     }
69     //
70     // TOC processing
71     //
72     $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR));   // Just to be safe
73     if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
74         $scorm->version = 'scorm_12';
75     }
76     require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
77     $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
78     if (($newattempt=='on') && (($attempt < $scorm->maxattempt) || ($scorm->maxattempt == 0))) {
79         $attempt++;
80         $mode = 'normal';
81     }
82     $attemptstr = '&amp;attempt=' . $attempt;
84     $result = scorm_get_toc($USER,$scorm,'structurelist',$currentorg,$scoid,$mode,$attempt,true);
85     $sco = $result->sco;
87     if (($mode == 'browse') && ($scorm->hidebrowse == 1)) {
88        $mode = 'normal';
89     }
90     if ($mode != 'browse') {
91         if ($trackdata = scorm_get_tracks($sco->id,$USER->id,$attempt)) {
92             if (($trackdata->status == 'completed') || ($trackdata->status == 'passed') || ($trackdata->status == 'failed')) {
93                 $mode = 'review';
94             } else {
95                 $mode = 'normal';
96             }
97         } else {
98             $mode = 'normal';
99         }
100     }
102     add_to_log($course->id, 'scorm', 'view', "player.php?id=$cm->id&scoid=$sco->id", "$scorm->id");
104     $scoidstr = '&amp;scoid='.$sco->id;
105     $scoidpop = '&scoid='.$sco->id;
106     $modestr = '&amp;mode='.$mode;
107     if ($mode == 'browse') {
108         $modepop = '&mode='.$mode;
109     } else {
110         $modepop = '';
111     }
112     $orgstr = '&currentorg='.$currentorg;
114     $SESSION->scorm_scoid = $sco->id;
115     $SESSION->scorm_status = 'Not Initialized';
116     $SESSION->scorm_mode = $mode;
117     $SESSION->scorm_attempt = $attempt;
119     //
120     // Print the page header
121     //
122     $bodyscript = '';
123     if ($scorm->popup == 1) {
124         $bodyscript = 'onunload="main.close();"';
125     }
127     $navlinks[] = array('name' => format_string($scorm->name,true), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
128     $navigation = build_navigation($navlinks);
129     $exitlink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'" title="'.$strexit.'">'.$strexit.'</a> ';
130     
131     print_header($pagetitle, $course->fullname,
132                  $navigation,
133                  '', '', true, $exitlink.update_module_button($cm->id, $course->id, $strscorm), '', false, $bodyscript);
134     //if ($sco->scormtype == 'sco') {
135 ?>
136     <script type="text/javascript" src="request.js"></script>
137     <script type="text/javascript" src="api.php?id=<?php echo $cm->id.$scoidstr.$modestr.$attemptstr ?>"></script>
138     <script type="text/javascript" src="<?=$CFG->wwwroot?>/mod/scorm/rd.js"></script>
139     <script type="text/javascript">
140     <!--    
141         window.onresize = function() {
142             scorm_resize();
143         };
144     -->  
145     </script>
146 <?php
147     //}
148     if (($sco->previd != 0) && ((!isset($sco->previous)) || ($sco->previous == 0))) {
149         $scostr = '&scoid='.$sco->previd;
150         echo '    <script type="text/javascript">'."\n//<![CDATA[\n".'var prev="'.$CFG->wwwroot.'/mod/scorm/player.php?id='.$cm->id.$orgstr.$modepop.$scostr."\";\n//]]>\n</script>\n";
151     } else {
152         echo '    <script type="text/javascript">'."\n//<![CDATA[\n".'var prev="'.$CFG->wwwroot.'/mod/scorm/view.php?id='.$cm->id."\";\n//]]>\n</script>\n";
153     }
154     if (($sco->nextid != 0) && ((!isset($sco->next)) || ($sco->next == 0))) {
155         $scostr = '&scoid='.$sco->nextid;
156         echo '    <script type="text/javascript">'."\n//<![CDATA[\n".'var next="'.$CFG->wwwroot.'/mod/scorm/player.php?id='.$cm->id.$orgstr.$modepop.$scostr."\";\n//]]>\n</script>\n";
157     } else {
158         echo '    <script type="text/javascript">'."\n//<![CDATA[\n".'var next="'.$CFG->wwwroot.'/mod/scorm/view.php?id='.$cm->id."\";\n//]]>\n</script>\n";
159     }
160 ?>
161     <div id="scormpage">
162 <?php  
163     if ($scorm->hidetoc == 0) {
164 ?>
166         <div id="tocbox">
168 <?php
170         if ($scorm->hidenav ==0){
172 ?>
174             <!-- Bottons nav at left-->
176             <div id="tochead">
178                 <form name="tochead" method="post" action="player.php?id=<?php echo $cm->id ?>" target="_top">
180 <?php
182             $orgstr = '&amp;currentorg='.$currentorg;
184             if (($scorm->hidenav == 0) && ($sco->previd != 0) && ($sco->previous == 0)) {
186                 // Print the prev LO button
188                 $scostr = '&amp;scoid='.$sco->previd;
190                 $url = $CFG->wwwroot.'/mod/scorm/player.php?id='.$cm->id.$orgstr.$modestr.$scostr;
192 ?>
194                     <input name="prev" type="button" value="<?php print_string('prev','scorm') ?>" onClick="document.location.href=' <?php echo $url; ?> '"/>
196 <?php
198             }
200             
202             if (($scorm->hidenav == 0) && ($sco->nextid != 0) && ($sco->next == 0)) {
204                 // Print the next LO button
206                 $scostr = '&amp;scoid='.$sco->nextid;
208                 $url = $CFG->wwwroot.'/mod/scorm/player.php?id='.$cm->id.$orgstr.$modestr.$scostr;
210 ?>
212                     <input name="next" type="button" value="<?php print_string('next','scorm') ?>" onClick="document.location.href=' <?php echo $url; ?> '"/>
214 <?php
216             }
218 ?>
220                 </form>
222             </div> <!-- tochead -->
224 <?php
226         }
227 ?>
228             <div id="toctree" class="generalbox">
229             <?php echo $result->toc; ?>
230             </div> <!-- toctree -->
232         </div> <!--  tocbox -->
233 <?php
234         $class = ' class="toc"';
235     } else {
236         $class = ' class="no-toc"';
237     }
238 ?>
239         <div id="scormbox"<?php echo $class; if(($scorm->hidetoc == 2) || ($scorm->hidetoc == 1)){echo 'style="width:100%"';}?>>
240 <?php
241     // This very big test check if is necessary the "scormtop" div
242     if (
243            ($mode != 'normal') ||  // We are not in normal mode so review or browse text will displayed
244            (
245                ($scorm->hidenav == 0) &&  // Teacher want to display navigation links
246                ($scorm->hidetoc != 0) &&  // The buttons has not been displayed
247                (
248                    (
249                        ($sco->previd != 0) &&  // This is not the first learning object of the package
250                        ((!isset($sco->previous)) || ($sco->previous == 0))   // Moodle must manage the previous link
251                    ) || 
252                    (
253                        ($sco->nextid != 0) &&  // This is not the last learning object of the package
254                        ((!isset($sco->next)) || ($sco->next == 0))       // Moodle must manage the next link
255                    ) 
256                )
257            ) || ($scorm->hidetoc == 2)      // Teacher want to display toc in a small dropdown menu 
258        ) {
259 ?>
260             <div id="scormtop">
261         <?php echo $mode == 'browse' ? '<div id="scormmode" class="left">'.get_string('browsemode','scorm')."</div>\n" : ''; ?>
262         <?php echo $mode == 'review' ? '<div id="scormmode" class="left">'.get_string('reviewmode','scorm')."</div>\n" : ''; ?>
263 <?php
264         if (($scorm->hidenav == 0) || ($scorm->hidetoc == 2) || ($scorm->hidetoc == 1)) {
265 ?>
266                 <div id="scormnav" class="right">
267         <?php
268             $orgstr = '&amp;currentorg='.$currentorg;
269             if (($scorm->hidenav == 0) && ($sco->previd != 0) && ($sco->previous == 0) && (($scorm->hidetoc == 2) || ($scorm->hidetoc == 1)) ) {
271                 // Print the prev LO button
272                 $scostr = '&amp;scoid='.$sco->previd;
273                 $url = $CFG->wwwroot.'/mod/scorm/player.php?id='.$cm->id.$orgstr.$modestr.$scostr;
274 ?>
276                     <form name="scormnavprev" method="post" action="player.php?id=<?php echo $cm->id ?>" target="_top" style= "display:inline">
278                         <input name="prev" type="button" value="<?php print_string('prev','scorm') ?>" onClick="document.location.href=' <?php echo $url; ?> '"/>
280                     </form>
282 <?php
283             }
284             if ($scorm->hidetoc == 2) {
285                 echo $result->tocmenu;
286             }
287             if (($scorm->hidenav == 0) && ($sco->nextid != 0) && ($sco->next == 0) && (($scorm->hidetoc == 2) || ($scorm->hidetoc == 1))) {
289                 // Print the next LO button
290                 $scostr = '&amp;scoid='.$sco->nextid;
291                 $url = $CFG->wwwroot.'/mod/scorm/player.php?id='.$cm->id.$orgstr.$modestr.$scostr;
292 ?>
294                     <form name="scormnavnext" method="post" action="player.php?id=<?php echo $cm->id ?>" target="_top" style= "display:inline">
296                         <input name="next" type="button" value="<?php print_string('next','scorm') ?>" onClick="document.location.href=' <?php echo $url; ?> '"/>
298                     </form>
300 <?php
301             }
302         ?>
304                 </div>
305 <?php
306         } 
307 ?>
308             </div> <!-- Scormtop -->
309 <?php
310     } // The end of the very big test
311 ?>
312             <div id="scormobject" class="right">
313                 <noscript>
314                     <div id="noscript">
315                         <?php print_string('noscriptnoscorm','scorm'); // No Martin(i), No Party ;-) ?>
317                     </div>
318                 </noscript>
319 <?php
320     if ($result->prerequisites) {
321         if ($scorm->popup == 0) {
322             echo "                <script type=\"text/javascript\">scorm_resize();</script>\n";
324             $fullurl="loadSCO.php?id=".$cm->id.$scoidstr.$modestr;
326             echo "                <iframe id=\"scoframe1\" class=\"scoframe\" name=\"scoframe1\" src=\"{$fullurl}\"></iframe>\n";   
327         } else {
328 ?>
329                     <script type="text/javascript">
330                     //<![CDATA[
331                                         scorm_resize();
332                         function openpopup(url,name,options,width,height) {
333                             fullurl = "<?php echo $CFG->wwwroot.'/mod/scorm/' ?>" + url;
334                             windowobj = window.open(fullurl,name,options);
335                             if ((width==100) && (height==100)) {
336                                 // Fullscreen
337                                 windowobj.moveTo(0,0);
338                             } 
339                             if (width<=100) {
340                                 width = Math.round(screen.availWidth * width / 100);
341                             }
342                             if (height<=100) {
343                                 height = Math.round(screen.availHeight * height / 100);
344                             }
345                             windowobj.resizeTo(width,height);
346                             windowobj.focus();
347                             return windowobj;
348                         }
350                         url = "loadSCO.php?id=<?php echo $cm->id.$scoidpop ?>";
351                         width = <?php p($scorm->width) ?>;
352                         height = <?php p($scorm->height) ?>;
353                         var main = openpopup(url, "<?php p($scorm->name) ?>", "<?php p($scorm->options) ?>", width, height);
354                    //]]>
355                     </script>
356                     <noscript>
357                     <iframe id="main" class="scoframe" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr ?>">
358                     </iframe>
359                     </noscript>
360 <?php            
361         }
362     } else {
363         print_simple_box(get_string('noprerequisites','scorm'),'center');
364     }
365 ?>
366             </div> <!-- SCORM object -->
367         </div> <!-- SCORM box  -->
368     </div> <!-- SCORM content -->
369     <?php if (!empty($THEME->customcorners)) {
370         print_container_end();
371     } ?>
372     </div> <!-- Content -->
373     </div> <!-- Page -->
374 </body>
375 </html>