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