MDL-15967 - must use full php tags, not shortags - thanks Urs
[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;
10 }
11 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
12
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;
18
19 //
20 // Get the current organization infos
21 //
bf347041 22 $conditions = array();
dc383b6f 23 if (!empty($currentorg)) {
bf347041 24 if (($organizationtitle = $DB->get_field('scorm_scoes', 'title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
dc383b6f 25 $result->toc .= "\t<li>$organizationtitle</li>\n";
26 $tocmenus[] = $organizationtitle;
27 }
bf347041 28 $conditions['organization'] = $currentorg;
dc383b6f 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;
bf347041 37 $conditions['scorm'] = $scorm->id;
38 if ($scoes = $DB->get_records('scorm_scoes', $conditions, "id ASC")){
9fb2de4e 39 // drop keys so that we can access array sequentially
40 $scoes = array_values($scoes);
dc383b6f 41 //
42 // Retrieve user tracking data for each learning object
43 //
530a61d5 44
dc383b6f 45 $usertracks = array();
10fd330f 46 $optionaldatas = array();
dc383b6f 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 }
10fd330f 55 if ($optionaldata = scorm_get_sco($sco->id, SCO_DATA)) {
56 $optionaldatas[$sco->identifier] = $optionaldata;
57 }
dc383b6f 58 }
59 }
60
61 $level=0;
62 $sublist=1;
63 $previd = 0;
64 $nextid = 0;
65 $findnext = false;
66 $parents[$level]='/';
9fb2de4e 67 foreach ($scoes as $pos=>$sco) {
b3659259 68 $isvisible = false;
294ce987 69 $sco->title = $sco->title;
10fd330f 70 if (isset($optionaldatas[$sco->identifier])) {
71 if (!isset($optionaldatas[$sco->identifier]->isvisible) ||
72 (isset($optionaldatas[$sco->identifier]->isvisible) && ($optionaldatas[$sco->identifier]->isvisible == 'true'))) {
b3659259 73 $isvisible = true;
74 }
75 }
dc383b6f 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 }
9b60b28a 94 $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
dc383b6f 95 $level++;
96 } else {
97 $result->toc .= $closelist;
98 $level = $i;
99 }
100 $parents[$level]=$sco->parent;
101 }
102 }
9fb2de4e 103 if (isset($scoes[$pos+1])) {
104 $nextsco = $scoes[$pos+1];
105 } else {
106 $nextsco = false;
107 }
b3659259 108 $nextisvisible = false;
10fd330f 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'))) {
b3659259 112 $nextisvisible = true;
113 }
114 }
10fd330f 115 if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) &&
116 (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
dc383b6f 117 $sublist++;
118 $icon = 'minus';
119 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
120 $icon = 'plus';
121 }
d28548d5 122 $result->toc .= "\t\t".'<li><a href="javascript:expandCollide(\'img'.$sublist.'\',\'s'.$sublist.'\','.$nextsco->id.');">'.
10fd330f 123 '<img id="img'.$sublist.'" src="'.$scormpixdir.'/'.$icon.'.gif" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
b3659259 124 } else if ($isvisible) {
9b60b28a 125 $result->toc .= "\t\t".'<li><img src="'.$scormpixdir.'/spacer.gif" />';
dc383b6f 126 }
127 if (empty($sco->title)) {
128 $sco->title = $sco->identifier;
129 }
130 if (!empty($sco->launch)) {
b3659259 131 if ($isvisible) {
132 $startbold = '';
133 $endbold = '';
134 $score = '';
135 if (empty($scoid) && ($mode != 'normal')) {
136 $scoid = $sco->id;
530a61d5 137 }
b3659259 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 {
dc383b6f 161 if ($play && empty($scoid)) {
162 $scoid = $sco->id;
163 }
b3659259 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 }
530a61d5 170 }
530a61d5 171
dc383b6f 172 if ($sco->id == $scoid) {
b3659259 173 $startbold = '<b>';
174 $endbold = '</b>';
175 $findnext = true;
10fd330f 176 $shownext = isset($optionaldatas[$sco->identifier]->next) ? $optionaldatas[$sco->identifier]->next : 0;
177 $showprev = isset($optionaldatas[$sco->identifier]->prev) ? $optionaldatas[$sco->identifier]->prev : 0;
dc383b6f 178 }
b3659259 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 }
10fd330f 185 require_once('sequencinglib.php');
186 if (scorm_seq_evaluate($sco->id,$usertracks)) {
b3659259 187 if ($sco->id == $scoid) {
188 $result->prerequisites = true;
189 }
b3659259 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);
b3659259 193 } else {
194 if ($sco->id == $scoid) {
195 $result->prerequisites = false;
196 }
197 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
dc383b6f 198 }
dc383b6f 199 }
200 } else {
b3659259 201 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
dc383b6f 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) {
bf347041 214 $sco = $DB->get_record('scorm_scoes', array('id'=>$scoid));
dc383b6f 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 .= '
f4ba7e1a 226 <script type="text/javascript">
227 //<![CDATA[
dc383b6f 228 function expandCollide(which,list,item) {
770f1bc8 229 var el = document.ids ? document.ids[list] : document.getElementById ? document.getElementById(list) : document.all[list];
d28548d5 230 which = which.substring(0,(which.length));
770f1bc8 231 var el2 = document.ids ? document.ids[which] : document.getElementById ? document.getElementById(which) : document.all[which];
97852875 232 if (el.style.display != "none") {
233 el2.src = "'.$scormpixdir.'/plus.gif";
234 el.style.display=\'none\';
dc383b6f 235 new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
236 } else {
97852875 237 el2.src = "'.$scormpixdir.'/minus.gif";
238 el.style.display=\'block\';
dc383b6f 239 new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
240 }
241 }
f4ba7e1a 242 //]]>
dc383b6f 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);
248
249 return $result;
250}
251
dc383b6f 252?>