MDL-26182 add detection of misconfigured nginx server
[moodle.git] / mod / scorm / datamodels / scorm_13lib.php
1 <?php
3 function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false, $tocheader=false) {
4     global $CFG, $DB, $PAGE, $OUTPUT;
6     $strexpand = get_string('expcoll','scorm');
7     $modestr = '';
8     if ($mode == 'browse') {
9         $modestr = '&amp;mode='.$mode;
10     }
12     $result = new stdClass();
13     if ($tocheader) {
14         $result->toc = '<div id="scorm_layout">';
15         $result->toc .= '<div id="scorm_toc">';
16         $result->toc .= '<div id="scorm_tree">';
17     }
18     $result->toc .= '<ul>';
19     $tocmenus = array();
20     $result->prerequisites = true;
21     $incomplete = false;
23     //
24     // Get the current organization infos
25     //
26     if (!empty($currentorg)) {
27         if (($organizationtitle = $DB->get_field('scorm_scoes', 'title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
28             if ($play) {
29             $result->toctitle = "$organizationtitle";
30             }
31             else {
32                 $result->toc .= "\t<li>$organizationtitle</li>\n";
33             }
34             $tocmenus[] = $organizationtitle;
35         }
36     }
37     //
38     // If not specified retrieve the last attempt number
39     //
40     if (empty($attempt)) {
41         $attempt = scorm_get_last_attempt($scorm->id, $user->id);
42     }
43     $result->attemptleft = $scorm->maxattempt - $attempt;
44     if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
45         //
46         // Retrieve user tracking data for each learning object
47         //
49         $usertracks = array();
50         foreach ($scoes as $sco) {
51             if (!empty($sco->launch)) {
52                 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
53                     if ($usertrack->status == '') {
54                         $usertrack->status = 'notattempted';
55                     }
56                     $usertracks[$sco->identifier] = $usertrack;
57                 }
58             }
59         }
61         $level=0;
62         $sublist=1;
63         $previd = 0;
64         $nextid = 0;
65         $findnext = false;
66         $parents[$level]='/';
67         foreach ($scoes as $pos => $sco) {
68             $isvisible = false;
69             $sco->title = $sco->title;
70             if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
71                 $isvisible = true;
72             }
73             if ($parents[$level]!=$sco->parent) {
74                 if ($newlevel = array_search($sco->parent,$parents)) {
75                     for ($i=0; $i<($level-$newlevel); $i++) {
76                         $result->toc .= "\t\t</li></ul></li>\n";
77                     }
78                     $level = $newlevel;
79                 } else {
80                     $i = $level;
81                     $closelist = '';
82                     while (($i > 0) && ($parents[$level] != $sco->parent)) {
83                         $closelist .= "\t\t</li></ul></li>\n";
84                         $i--;
85                     }
86                     if (($i == 0) && ($sco->parent != $currentorg)) {
87                         $style = '';
88                         if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
89                             $style = ' style="display: none;"';
90                         }
91                         //$result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
92                         $result->toc .= "\t\t><ul>\n";
93                         $level++;
94                     } else {
95                         $result->toc .= $closelist;
96                         $level = $i;
97                     }
98                     $parents[$level]=$sco->parent;
99                 }
100             }
101             if ($isvisible) {
102                 $result->toc .= "<li>";
103             }
104             if (isset($scoes[$pos+1])) {
105                 $nextsco = $scoes[$pos+1];
106             } else {
107                 $nextsco = false;
108             }
109             $nextisvisible = false;
110             if (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true'))) {
111                 $nextisvisible = true;
112             }
113             if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) &&
114                (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
115                 $sublist++;
116                 $icon = 'minus';
117                 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
118                     $icon = 'plus';
119                 }
120 //                $result->toc .= "\t\t".'<li><a href="javascript:expandCollide(\'img'.$sublist.'\',\'s'.$sublist.'\','.$nextsco->id.');">'.
121 //                                '<img id="img'.$sublist.'" src="'.$OUTPUT->pix_url($icon, 'scorm').'" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
122             } else if ($isvisible) {
123 //                $result->toc .= "\t\t".'<li><img src="'.$OUTPUT->pix_url('spacer', 'scorm').'" alt="" />';
124             }
125             if (empty($sco->title)) {
126                 $sco->title = $sco->identifier;
127             }
128             if (!empty($sco->launch)) {
129                 if ($isvisible) {
130                     $startbold = '';
131                     $endbold = '';
132                     $score = '';
133                     if (empty($scoid) && ($mode != 'normal')) {
134                         $scoid = $sco->id;
135                     }
136                     if (isset($usertracks[$sco->identifier])) {
137                         $usertrack = $usertracks[$sco->identifier];
138                         $strstatus = get_string($usertrack->status,'scorm');
139                         if ($sco->scormtype == 'sco') {
140                             $statusicon = '<img src="'.$OUTPUT->pix_url($usertrack->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" />';
141                         } else {
142                             $statusicon = '<img src="'.$OUTPUT->pix_url('assetc', 'scorm').'" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
143                         }
145                         if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
146                             $incomplete = true;
147                             if ($play && empty($scoid)) {
148                                 $scoid = $sco->id;
149                             }
150                         }
151                         if ($usertrack->score_raw != '') {
152                             $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
153                         }
154                         $strsuspended = get_string('suspended','scorm');
155                         if ($incomplete && isset($usertrack->{'cmi.exit'}) && ($usertrack->{'cmi.exit'} == 'suspend')) {
156                             $statusicon = '<img src="'.$OUTPUT->pix_url('suspend', 'scorm').'" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
157                         }
158                     } else {
159                         if ($play && empty($scoid)) {
160                             $scoid = $sco->id;
161                         }
162                         if ($sco->scormtype == 'sco') {
163                             $statusicon = '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
164                             $incomplete = true;
165                         } else {
166                             $statusicon = '<img src="'.$OUTPUT->pix_url('asset', 'scorm').'" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
167                         }
168                     }
170                     if ($sco->id == $scoid) {
171                         $startbold = '<b>';
172                         $endbold = '</b>';
173                         $findnext = true;
174                         $shownext = isset($sco->next) ? $sco->next : 0;
175                         $showprev = isset($sco->prev) ? $sco->prev : 0;
176                     }
178                     if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
179                         if (!empty($sco->launch)) {
180                             $previd = $sco->id;
181                         }
182                     }
183                     require_once($CFG->dirroot.'/mod/scorm/datamodels/sequencinglib.php');
184                     if (scorm_seq_evaluate($sco->id,$usertracks)) {
185                         if ($sco->id == $scoid) {
186                             $result->prerequisites = true;
187                         }
188                             $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
189                             $thisscoidstr = '&scoid='.$sco->id;
190                             //$link = $CFG->wwwroot.'/mod/scorm/loadSCO.php?a='.$scorm->id.$thisscoidstr.$modestr;
191                             $link = 'a='.$scorm->id.$thisscoidstr.'&currentorg='.$currentorg.$modestr.'&attempt='.$attempt;
192                             //$result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
193                             //$result->toc .= '<li><a title="'.$link.'">'.$statusicon.'&nbsp;'.format_string($sco->title).'&nbsp;'.$score.'</a>'."</li>\n";
194                             if ($sco->launch) {
195                                 $result->toc .= '<a title="'.$link.'">'.$statusicon.'&nbsp;'.format_string($sco->title).'&nbsp;'.$score.'</a>';
196                             }
197                             else {
198                                 $result->toc .= '<span>'.$statusicon.'&nbsp;'.format_string($sco->title).'</span>';
199                             }
200                             $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
201                     } else {
202                         if ($sco->id == $scoid) {
203                             $result->prerequisites = false;
204                         }
205                         //$result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."\n";
206                         if ($play) {
207                             // should be disabled
208                             $result->toc .= '<span>'.$statusicon.'&nbsp;'.format_string($sco->title).'</span>';
209                         }
210                         else {
211                             $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."\n";
212                         }
213                     }
214                     if (($nextsco === false) || $nextsco->parent == $sco->parent) {
215                         $result->toc .= '</li>';
216                     }
217                 }
218             } else {
219                 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
220             }
221             if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
222                 if (!empty($nextsco->launch)) {
223                     $nextid = $nextsco->id;
224                 }
225             }
226         }
227         for ($i=0;$i<$level;$i++) {
228             $result->toc .= "\t\t</ul></li>\n";
229         }
231         if ($play) {
232             $sco = $DB->get_record('scorm_scoes', array('id'=>$scoid));
233             $sco->previd = $previd;
234             $sco->nextid = $nextid;
235             $result->sco = $sco;
236             $result->incomplete = $incomplete;
237         } else {
238             $result->incomplete = $incomplete;
239         }
240     }
241     $result->toc .= '</ul>';
244     // NEW IMS TOC
245     if ($tocheader) {
246         $result->toc .= '</div></div></div>';
247         $result->toc .= '<div id="scorm_navpanel"></div>';
248     }
251     if ($scorm->hidetoc == 0) {
252         $PAGE->requires->data_for_js('scormdata', array(
253                 'plusicon' => $OUTPUT->pix_url('plus', 'scorm'),
254                 'minusicon' => $OUTPUT->pix_url('minus', 'scorm')));
255         $PAGE->requires->js('/lib/cookies.js');
256         $PAGE->requires->js('/mod/scorm/datamodels/scorm_datamodels.js');
257     }
259     $url = new moodle_url('/mod/scorm/player.php?a='.$scorm->id.'&currentorg='.$currentorg.$modestr);
260     $result->tocmenu = $OUTPUT->single_select($url, 'scoid', $tocmenus, $sco->id, null, "tocmenu");
262     return $result;