lib-cookies MDL-19737 Removed need to include cookies.js on every page
[moodle.git] / mod / scorm / datamodels / scorm_13lib.php
1 <?php // $Id$
3 function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=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     $result->toc = "<ul id='s0' class='$liststyle'>\n";
14     $tocmenus = array();
15     $result->prerequisites = true;
16     $incomplete = false;
18     //
19     // Get the current organization infos
20     //
21     if (!empty($currentorg)) {
22         if (($organizationtitle = $DB->get_field('scorm_scoes', 'title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
23             $result->toc .= "\t<li>$organizationtitle</li>\n";
24             $tocmenus[] = $organizationtitle;
25         }
26     }
27     //
28     // If not specified retrieve the last attempt number
29     //
30     if (empty($attempt)) {
31         $attempt = scorm_get_last_attempt($scorm->id, $user->id);
32     }
33     $result->attemptleft = $scorm->maxattempt - $attempt;
34     if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
35         //
36         // Retrieve user tracking data for each learning object
37         //
39         $usertracks = array();
40         foreach ($scoes as $sco) {
41             if (!empty($sco->launch)) {
42                 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
43                     if ($usertrack->status == '') {
44                         $usertrack->status = 'notattempted';
45                     }
46                     $usertracks[$sco->identifier] = $usertrack;
47                 }
48             }
49         }
51         $level=0;
52         $sublist=1;
53         $previd = 0;
54         $nextid = 0;
55         $findnext = false;
56         $parents[$level]='/';
57         foreach ($scoes as $pos => $sco) {
58             $isvisible = false;
59             $sco->title = $sco->title;
60             if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
61                 $isvisible = true;
62             }
63             if ($parents[$level]!=$sco->parent) {
64                 if ($newlevel = array_search($sco->parent,$parents)) {
65                     for ($i=0; $i<($level-$newlevel); $i++) {
66                         $result->toc .= "\t\t</ul></li>\n";
67                     }
68                     $level = $newlevel;
69                 } else {
70                     $i = $level;
71                     $closelist = '';
72                     while (($i > 0) && ($parents[$level] != $sco->parent)) {
73                         $closelist .= "\t\t</ul></li>\n";
74                         $i--;
75                     }
76                     if (($i == 0) && ($sco->parent != $currentorg)) {
77                         $style = '';
78                         if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
79                             $style = ' style="display: none;"';
80                         }
81                         $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
82                         $level++;
83                     } else {
84                         $result->toc .= $closelist;
85                         $level = $i;
86                     }
87                     $parents[$level]=$sco->parent;
88                 }
89             }
90             if (isset($scoes[$pos+1])) {
91                 $nextsco = $scoes[$pos+1];
92             } else {
93                 $nextsco = false;
94             }
95             $nextisvisible = false;
96             if (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true'))) {
97                 $nextisvisible = true;
98             }
99             if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) &&
100                (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
101                 $sublist++;
102                 $icon = 'minus';
103                 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
104                     $icon = 'plus';
105                 }
106                 $result->toc .= "\t\t".'<li><a href="javascript:expandCollide(\'img'.$sublist.'\',\'s'.$sublist.'\','.$nextsco->id.');">'.
107                                 '<img id="img'.$sublist.'" src="'.$OUTPUT->mod_icon_url('pix/'.$icon, 'scorm').'" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
108             } else if ($isvisible) {
109                 $result->toc .= "\t\t".'<li><img src="'.$OUTPUT->mod_icon_url('pix/spacer', 'scorm').'" alt="" />';
110             }
111             if (empty($sco->title)) {
112                 $sco->title = $sco->identifier;
113             }
114             if (!empty($sco->launch)) {
115                 if ($isvisible) {
116                     $startbold = '';
117                     $endbold = '';
118                     $score = '';
119                     if (empty($scoid) && ($mode != 'normal')) {
120                         $scoid = $sco->id;
121                     }
122                     if (isset($usertracks[$sco->identifier])) {
123                         $usertrack = $usertracks[$sco->identifier];
124                         $strstatus = get_string($usertrack->status,'scorm');
125                         if ($sco->scormtype == 'sco') {
126                             $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/'.$usertrack->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" />';
127                         } else {
128                             $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/assetc', 'scorm').'" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
129                         }
131                         if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
132                             $incomplete = true;
133                             if ($play && empty($scoid)) {
134                                 $scoid = $sco->id;
135                             }
136                         }
137                         if ($usertrack->score_raw != '') {
138                             $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
139                         }
140                         $strsuspended = get_string('suspended','scorm');
141                         if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
142                             if($usertrack->status !='completed') {
143                                 $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/suspend', 'scorm').'" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
144                             }
145                         }
146                     } else {
147                         if ($play && empty($scoid)) {
148                             $scoid = $sco->id;
149                         }
150                         if ($sco->scormtype == 'sco') {
151                             $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/notattempted', 'scorm').'" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
152                             $incomplete = true;
153                         } else {
154                             $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/asset', 'scorm').'" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
155                         }
156                     }
158                     if ($sco->id == $scoid) {
159                         $startbold = '<b>';
160                         $endbold = '</b>';
161                         $findnext = true;
162                         $shownext = isset($sco->next) ? $sco->next : 0;
163                         $showprev = isset($sco->prev) ? $sco->prev : 0;
164                     }
166                     if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
167                         if (!empty($sco->launch)) {
168                             $previd = $sco->id;
169                         }
170                     }
171                     require_once('sequencinglib.php');
172                     if (scorm_seq_evaluate($sco->id,$usertracks)) {
173                         if ($sco->id == $scoid) {
174                             $result->prerequisites = true;
175                         }
176                             $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
177                             $result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
178                             $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
179                     } else {
180                         if ($sco->id == $scoid) {
181                             $result->prerequisites = false;
182                         }
183                         $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
184                     }
185                 }
186             } else {
187                 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
188             }
189             if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
190                 if (!empty($nextsco->launch)) {
191                     $nextid = $nextsco->id;
192                 }
193             }
194         }
195         for ($i=0;$i<$level;$i++) {
196             $result->toc .= "\t\t</ul></li>\n";
197         }
199         if ($play) {
200             $sco = $DB->get_record('scorm_scoes', array('id'=>$scoid));
201             $sco->previd = $previd;
202             $sco->nextid = $nextid;
203             $result->sco = $sco;
204             $result->incomplete = $incomplete;
205         } else {
206             $result->incomplete = $incomplete;
207         }
208     }
209     $result->toc .= "\t</ul>\n";
210     if ($scorm->hidetoc == 0) {
211         $PAGE->requires->data_for_js('scormdata', array(
212                 'plusicon' => $OUTPUT->mod_icon_url('pix/plus', 'scorm'),
213                 'minusicon' => $OUTPUT->mod_icon_url('pix/minus', 'scorm')));
214         $PAGE->requires->js('lib/cookies.js');
215         $PAGE->requires->js('mod/scorm/datamodels/scorm_datamodels.js');
216     }
218     $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&currentorg='.$currentorg.$modestr;
219     $select = html_select::make_popup_form($url, 'scoid', $tocmenus, "tocmenu", $sco->id);
220     $select->nothinglabel = false;
221     $result->tocmenu = $OUTPUT->select($select);
223     return $result;
226 ?>