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