SCORM MDL-25781 fix links in TOC on view.php
[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);
60     require_login($course->id, false, $cm);
62     $strscorms = get_string('modulenameplural', 'scorm');
63     $strscorm  = get_string('modulename', 'scorm');
64     $strpopup = get_string('popup','scorm');
65     $strexit = get_string('exitactivity','scorm');
67     $pagetitle = strip_tags("$course->shortname: ".format_string($scorm->name));
68     $PAGE->set_title($pagetitle);
69     $PAGE->set_heading($course->fullname);
71     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE,$course->id))) {
72         echo $OUTPUT->header();
73         notice(get_string("activityiscurrentlyhidden"));
74         echo $OUTPUT->footer();
75         die;
76     }
78     //check if scorm closed
79     $timenow = time();
80     if ($scorm->timeclose !=0) {
81         if ($scorm->timeopen > $timenow) {
82             echo $OUTPUT->header();
83             echo $OUTPUT->box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "generalbox boxaligncenter");
84             echo $OUTPUT->footer();
85             die;
86         } elseif ($timenow > $scorm->timeclose) {
87             echo $OUTPUT->header();
88             echo $OUTPUT->box(get_string("expired", "scorm", userdate($scorm->timeclose)), "generalbox boxaligncenter");
89             echo $OUTPUT->footer();
90             die;
91         }
92     }
94     //
95     // TOC processing
96     //
97     $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR));   // Just to be safe
98     if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
99         $scorm->version = 'scorm_12';
100     }
101     require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
102     $attempt = scorm_get_last_attempt($scorm->id, $USER->id);
103     if (($newattempt=='on') && (($attempt < $scorm->maxattempt) || ($scorm->maxattempt == 0))) {
104         $attempt++;
105         $mode = 'normal';
106     }
107     $attemptstr = '&amp;attempt=' . $attempt;
109     $result = scorm_get_toc($USER, $scorm, TOCJSLINK, $currentorg, $scoid, $mode, $attempt, true, true);
110     $sco = $result->sco;
112     if (($mode == 'browse') && ($scorm->hidebrowse == 1)) {
113        $mode = 'normal';
114     }
115     if ($mode != 'browse') {
116         if ($trackdata = scorm_get_tracks($sco->id,$USER->id,$attempt)) {
117             if (($trackdata->status == 'completed') || ($trackdata->status == 'passed') || ($trackdata->status == 'failed')) {
118                 $mode = 'review';
119             } else {
120                 $mode = 'normal';
121             }
122         } else {
123             $mode = 'normal';
124         }
125     }
127     add_to_log($course->id, 'scorm', 'view', "player.php?cm=$cm->id&scoid=$sco->id", "$scorm->id", $cm->id);
130     $scoidstr = '&amp;scoid='.$sco->id;
131     $scoidpop = '&scoid='.$sco->id;
132     $modestr = '&amp;mode='.$mode;
133     if ($mode == 'browse') {
134         $modepop = '&mode='.$mode;
135     } else {
136         $modepop = '';
137     }
138     $orgstr = '&currentorg='.$currentorg;
140     $SESSION->scorm_scoid = $sco->id;
141     $SESSION->scorm_status = 'Not Initialized';
142     $SESSION->scorm_mode = $mode;
143     $SESSION->scorm_attempt = $attempt;
145     //
146     // Print the page header
147     //
148     $bodyscript = '';
149     if ($scorm->popup == 1) {
150         $bodyscript = 'onunload="main.close();"';
151     }
153     $exitlink = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'" title="'.$strexit.'">'.$strexit.'</a> ';
155     $PAGE->set_button($exitlink);
157     $PAGE->requires->data_for_js('scormplayerdata', Array('cwidth'=>$scorm->width,'cheight'=>$scorm->height,
158                                                           'popupoptions' => $scorm->options), true);
159     $PAGE->requires->js('/mod/scorm/request.js', true);
160     $PAGE->requires->js('/lib/cookies.js', true);
161     //$PAGE->requires->js('/mod/scorm/loaddatamodel.php?id='.$cm->id.$scoidstr.$modestr.$attemptstr, true);
162     $PAGE->requires->css('/mod/scorm/styles.css');
164     echo $OUTPUT->header();
166     // NEW IMS TOC
167     $PAGE->requires->string_for_js('navigation', 'scorm');
168     $PAGE->requires->string_for_js('toc', 'scorm');
169     $PAGE->requires->string_for_js('hide', 'moodle');
170     $PAGE->requires->string_for_js('show', 'moodle');
171     $PAGE->requires->string_for_js('popupsblocked', 'scorm');
173     $name = false;
175 ?>
176     <div id="scormpage">
177     
178       <div id="tocbox">
179         <div id='scormapi-parent'>
180             <script id="external-scormapi" type="text/JavaScript"></script>
181         </div>
182         <div id="scormtop">
183 <?php
184     if ($result->prerequisites) {
185         if ($scorm->popup != 0) {
186             //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.
187             $linkcourse = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$scorm->course.'">' . get_string('finishscormlinkname','scorm') . '</a>';
188             echo $OUTPUT->box(get_string('finishscorm','scorm',$linkcourse), 'generalbox', 'altfinishlink');
189         }
190     }
191 ?>
192         <?php echo $mode == 'browse' ? '<div id="scormmode" class="scorm-left">'.get_string('browsemode','scorm')."</div>\n" : ''; ?>
193         <?php echo $mode == 'review' ? '<div id="scormmode" class="scorm-left">'.get_string('reviewmode','scorm')."</div>\n" : ''; ?>
194             <div id="scormnav" class="scorm-right">
195 <?php
196         if ($scorm->hidetoc == 2) {
197              echo $result->tocmenu;
198         }
199 ?>
200             </div> <!-- Scormnav -->
201         </div> <!-- Scormtop -->
202             <div id="toctree" class="generalbox">
203             <?php echo $result->toc; ?>
204             </div> <!-- toctree -->
205         </div> <!--  tocbox -->
206                 <noscript>
207                     <div id="noscript">
208                         <?php print_string('noscriptnoscorm','scorm'); // No Martin(i), No Party ;-) ?>
210                     </div>
211                 </noscript>
212 <?php
213     if ($result->prerequisites) {
214         if ($scorm->popup != 0) {
215             // Clean the name for the window as IE is fussy
216             $name = preg_replace("/[^A-Za-z0-9]/", "", $scorm->name);
217             if (!$name) {
218                 $name = 'DefaultPlayerWindow';
219             }
220             $name = 'scorm_'.$name;
222             echo html_writer::script('', $CFG->wwwroot.'/mod/scorm/player.js');
223             echo html_writer::script(js_writer::function_call('scorm_openpopup', Array("loadSCO.php?id=".$cm->id.$scoidpop, $name, $scorm->options, $scorm->width, $scorm->height)));
224             ?>
225             <noscript>
226             <!--[if IE]>
227                 <iframe id="main" class="scoframe" name="main" src="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></iframe>
228             <![endif]-->
229             <!--[if !IE]>
230                 <object id="main" class="scoframe" type="text/html" data="loadSCO.php?id=<?php echo $cm->id.$scoidstr.$modestr; ?>"></object>
231             <![endif]-->
232             </noscript>
233 <?php
234         }
235     } else {
236         echo $OUTPUT->box(get_string('noprerequisites','scorm'));
237     }
238 ?>
239     </div> <!-- SCORM page -->
240 <?php 
241 // NEW IMS TOC
242 if (!isset($result->toctitle)) {
243     $result->toctitle = get_string('toc', 'scorm');
246 $PAGE->requires->js_init_call('M.mod_scorm.init', array($scorm->hidenav, $scorm->hidetoc, $result->toctitle, $name, $sco->id));
249 $completion=new completion_info($course);
250 $completion->set_module_viewed($cm);
252 echo $OUTPUT->footer();