Accessibility: MDL-17705: Thanks Tim
[moodle.git] / mod / scorm / datamodels / scorm_13lib.php
CommitLineData
f4ba7e1a 1<?php // $Id$
dc383b6f 2
3function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
bf347041 4 global $CFG, $DB;
dc383b6f 5
6 $strexpand = get_string('expcoll','scorm');
7 $modestr = '';
8 if ($mode == 'browse') {
9 $modestr = '&amp;mode='.$mode;
d9e6517a 10 }
dc383b6f 11 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
d9e6517a 12
dc383b6f 13 $result = new stdClass();
9b60b28a 14 $result->toc = "<ul id='s0' class='$liststyle'>\n";
dc383b6f 15 $tocmenus = array();
16 $result->prerequisites = true;
17 $incomplete = false;
d9e6517a 18
dc383b6f 19 //
20 // Get the current organization infos
21 //
dc383b6f 22 if (!empty($currentorg)) {
bf347041 23 if (($organizationtitle = $DB->get_field('scorm_scoes', 'title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
dc383b6f 24 $result->toc .= "\t<li>$organizationtitle</li>\n";
25 $tocmenus[] = $organizationtitle;
26 }
dc383b6f 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;
82605bea 35 if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
dc383b6f 36 //
37 // Retrieve user tracking data for each learning object
d9e6517a 38 //
39
dc383b6f 40 $usertracks = array();
41 foreach ($scoes as $sco) {
42 if (!empty($sco->launch)) {
82605bea 43 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
dc383b6f 44 if ($usertrack->status == '') {
45 $usertrack->status = 'notattempted';
46 }
47 $usertracks[$sco->identifier] = $usertrack;
48 }
49 }
50 }
51
52 $level=0;
53 $sublist=1;
54 $previd = 0;
55 $nextid = 0;
56 $findnext = false;
57 $parents[$level]='/';
82605bea 58 foreach ($scoes as $pos => $sco) {
b3659259 59 $isvisible = false;
294ce987 60 $sco->title = $sco->title;
82605bea 61 if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
62 $isvisible = true;
b3659259 63 }
dc383b6f 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 }
9b60b28a 82 $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
dc383b6f 83 $level++;
84 } else {
85 $result->toc .= $closelist;
86 $level = $i;
87 }
88 $parents[$level]=$sco->parent;
89 }
90 }
9fb2de4e 91 if (isset($scoes[$pos+1])) {
92 $nextsco = $scoes[$pos+1];
93 } else {
94 $nextsco = false;
95 }
b3659259 96 $nextisvisible = false;
82605bea 97 if (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true'))) {
98 $nextisvisible = true;
b3659259 99 }
d9e6517a 100 if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) &&
10fd330f 101 (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
dc383b6f 102 $sublist++;
103 $icon = 'minus';
104 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
105 $icon = 'plus';
106 }
d28548d5 107 $result->toc .= "\t\t".'<li><a href="javascript:expandCollide(\'img'.$sublist.'\',\'s'.$sublist.'\','.$nextsco->id.');">'.
10fd330f 108 '<img id="img'.$sublist.'" src="'.$scormpixdir.'/'.$icon.'.gif" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
b3659259 109 } else if ($isvisible) {
d9e6517a 110 $result->toc .= "\t\t".'<li><img src="'.$scormpixdir.'/spacer.gif" alt="" />';
dc383b6f 111 }
112 if (empty($sco->title)) {
113 $sco->title = $sco->identifier;
114 }
115 if (!empty($sco->launch)) {
b3659259 116 if ($isvisible) {
117 $startbold = '';
118 $endbold = '';
119 $score = '';
120 if (empty($scoid) && ($mode != 'normal')) {
121 $scoid = $sco->id;
530a61d5 122 }
b3659259 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 }
d9e6517a 131
b3659259 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')) {
7ef16bf9 143 if($usertrack->status !='completed') {
144 $statusicon = '<img src="'.$scormpixdir.'/suspend.gif" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
145 }
b3659259 146 }
147 } else {
dc383b6f 148 if ($play && empty($scoid)) {
149 $scoid = $sco->id;
150 }
b3659259 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 }
530a61d5 157 }
530a61d5 158
dc383b6f 159 if ($sco->id == $scoid) {
b3659259 160 $startbold = '<b>';
161 $endbold = '</b>';
162 $findnext = true;
82605bea 163 $shownext = isset($sco->next) ? $sco->next : 0;
164 $showprev = isset($sco->prev) ? $sco->prev : 0;
dc383b6f 165 }
d9e6517a 166
b3659259 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 }
10fd330f 172 require_once('sequencinglib.php');
173 if (scorm_seq_evaluate($sco->id,$usertracks)) {
b3659259 174 if ($sco->id == $scoid) {
175 $result->prerequisites = true;
176 }
b3659259 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);
b3659259 180 } else {
181 if ($sco->id == $scoid) {
182 $result->prerequisites = false;
183 }
184 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
dc383b6f 185 }
dc383b6f 186 }
187 } else {
b3659259 188 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
dc383b6f 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 }
d9e6517a 199
dc383b6f 200 if ($play) {
bf347041 201 $sco = $DB->get_record('scorm_scoes', array('id'=>$scoid));
dc383b6f 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 .= '
f4ba7e1a 213 <script type="text/javascript">
214 //<![CDATA[
dc383b6f 215 function expandCollide(which,list,item) {
770f1bc8 216 var el = document.ids ? document.ids[list] : document.getElementById ? document.getElementById(list) : document.all[list];
d28548d5 217 which = which.substring(0,(which.length));
770f1bc8 218 var el2 = document.ids ? document.ids[which] : document.getElementById ? document.getElementById(which) : document.all[which];
97852875 219 if (el.style.display != "none") {
220 el2.src = "'.$scormpixdir.'/plus.gif";
221 el.style.display=\'none\';
dc383b6f 222 new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
223 } else {
97852875 224 el2.src = "'.$scormpixdir.'/minus.gif";
225 el.style.display=\'block\';
dc383b6f 226 new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
227 }
228 }
f4ba7e1a 229 //]]>
dc383b6f 230 </script>'."\n";
231 }
d9e6517a 232
dc383b6f 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);
235
236 return $result;
237}
238
dc383b6f 239?>