Accessibility: MDL-17705: Thanks Tim
[moodle.git] / mod / scorm / datamodels / scorm_12lib.php
CommitLineData
f4ba7e1a 1<?php // $Id$
82605bea 2/**
3* This is really a little language parser for AICC_SCRIPT
4* evaluates the expression and returns a boolean answer
5* see 2.3.2.5.1. Sequencing/Navigation Today - from the SCORM 1.2 spec (CAM).
6*
7* @param string $prerequisites the aicc_script prerequisites expression
8* @param array $usertracks the tracked user data of each SCO visited
9* @return boolean
10*/
11function scorm_eval_prerequisites($prerequisites, $usertracks) {
d9e6517a 12
13 // this is really a little language parser - AICC_SCRIPT is the reference
82605bea 14 // see 2.3.2.5.1. Sequencing/Navigation Today - from the SCORM 1.2 spec
dc383b6f 15 $element = '';
16 $stack = array();
17 $statuses = array(
18 'passed' => 'passed',
19 'completed' => 'completed',
20 'failed' => 'failed',
21 'incomplete' => 'incomplete',
22 'browsed' => 'browsed',
23 'not attempted' => 'notattempted',
24 'p' => 'passed',
25 'c' => 'completed',
26 'f' => 'failed',
27 'i' => 'incomplete',
28 'b' => 'browsed',
29 'n' => 'notattempted'
30 );
d9e6517a 31 $i=0;
82605bea 32
33 // expand the amp entities
34 $prerequisites = preg_replace('/&amp;/', '&', $prerequisites);
35 // find all my parsable tokens
36 $prerequisites = preg_replace('/(&|\||\(|\)|\~)/', '\t$1\t', $prerequisites);
37 // expand operators
38 $prerequisites = preg_replace('/&/', '&&', $prerequisites);
39 $prerequisites = preg_replace('/\|/', '||', $prerequisites);
40 // now - grab all the tokens
41 $elements = explode('\t', trim($prerequisites));
42
43 // process each token to build an expression to be evaluated
44 $stack = array();
45 foreach ($elements as $element) {
46 $element = trim($element);
47 if (empty($element)) {
48 continue;
49 }
50 if (!preg_match('/^(&&|\|\||\(|\))$/', $element)) {
51 // create each individual expression
52 // search for ~ = <> X*{}
d9e6517a 53
82605bea 54 // sets like 3*{S34, S36, S37, S39}
55 if (preg_match('/^(\d+)\*\{(.+)\}$/', $element, $matches)) {
56 $repeat = $matches[1];
57 $set = explode(',', $matches[2]);
58 $count = 0;
59 foreach ($set as $setelement) {
60 if (isset($usertracks[$setelement]) &&
61 ($usertracks[$setelement]->status == 'completed' || $usertracks[$element]->status == 'passed')) {
62 $count++;
63 }
dc383b6f 64 }
82605bea 65 if ($count >= $repeat) {
66 $element = 'true';
67 } else {
68 $element = 'false';
dc383b6f 69 }
d9e6517a 70
82605bea 71 // ~ Not
72 } else if ($element == '~') {
73 $element = '!';
d9e6517a 74
82605bea 75 // = | <>
76 } else if (preg_match('/^(.+)(\=|\<\>)(.+)$/', $element, $matches)) {
77 $element = trim($matches[1]);
78 if (isset($usertracks[$element])) {
79 $value = trim(preg_replace('/(\'|\")/', '', $matches[3]));
80 if (isset($statuses[$value])) {
81 $value = $statuses[$value];
82 }
83 if ($matches[2] == '<>') {
84 $oper = '!=';
85 } else {
86 $oper = '==';
87 }
88 $element = '(\''.$usertracks[$element]->status.'\' '.$oper.' \''.$value.'\')';
89 } else {
90 $element = 'false';
dc383b6f 91 }
d9e6517a 92
82605bea 93 // everything else must be an element defined like S45 ...
dc383b6f 94 } else {
82605bea 95 if (isset($usertracks[$element]) &&
96 ($usertracks[$element]->status == 'completed' || $usertracks[$element]->status == 'passed')) {
97 $element = 'true';
98 } else {
99 $element = 'false';
100 }
dc383b6f 101 }
d9e6517a 102
dc383b6f 103 }
82605bea 104 $stack []= ' '.$element.' ';
dc383b6f 105 }
106 return eval('return '.implode($stack).';');
107}
108
109function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
bf347041 110 global $CFG, $DB;
d9e6517a 111
dc383b6f 112 $strexpand = get_string('expcoll','scorm');
113 $modestr = '';
114 if ($mode == 'browse') {
115 $modestr = '&amp;mode='.$mode;
d9e6517a 116 }
dc383b6f 117 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
d9e6517a 118
dc383b6f 119 $result = new stdClass();
75bca47d 120 $result->toc = "<ul id='s0' class='$liststyle'>\n";
dc383b6f 121 $tocmenus = array();
122 $result->prerequisites = true;
123 $incomplete = false;
d9e6517a 124
dc383b6f 125 //
126 // Get the current organization infos
127 //
dc383b6f 128 if (!empty($currentorg)) {
bf347041 129 if (($organizationtitle = $DB->get_field('scorm_scoes','title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
dc383b6f 130 $result->toc .= "\t<li>$organizationtitle</li>\n";
131 $tocmenus[] = $organizationtitle;
132 }
dc383b6f 133 }
134 //
135 // If not specified retrieve the last attempt number
136 //
137 if (empty($attempt)) {
138 $attempt = scorm_get_last_attempt($scorm->id, $user->id);
139 }
140 $result->attemptleft = $scorm->maxattempt - $attempt;
bf347041 141 $conditions['scorm'] = $scorm->id;
82605bea 142 if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
dc383b6f 143 //
144 // Retrieve user tracking data for each learning object
9fb2de4e 145 //
dc383b6f 146 $usertracks = array();
147 foreach ($scoes as $sco) {
148 if (!empty($sco->launch)) {
82605bea 149 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
dc383b6f 150 if ($usertrack->status == '') {
151 $usertrack->status = 'notattempted';
152 }
153 $usertracks[$sco->identifier] = $usertrack;
154 }
155 }
156 }
157
158 $level=0;
159 $sublist=1;
160 $previd = 0;
161 $nextid = 0;
162 $findnext = false;
163 $parents[$level]='/';
d9e6517a 164
82605bea 165 foreach ($scoes as $pos => $sco) {
b3659259 166 $isvisible = false;
bf347041 167 $sco->title = $sco->title;
82605bea 168 if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
e73457e4 169 $isvisible = true;
eb0e9d9d 170 }
82605bea 171 if ($parents[$level] != $sco->parent) {
dc383b6f 172 if ($newlevel = array_search($sco->parent,$parents)) {
173 for ($i=0; $i<($level-$newlevel); $i++) {
174 $result->toc .= "\t\t</ul></li>\n";
175 }
176 $level = $newlevel;
177 } else {
178 $i = $level;
179 $closelist = '';
180 while (($i > 0) && ($parents[$level] != $sco->parent)) {
181 $closelist .= "\t\t</ul></li>\n";
182 $i--;
183 }
184 if (($i == 0) && ($sco->parent != $currentorg)) {
185 $style = '';
186 if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
187 $style = ' style="display: none;"';
188 }
75bca47d 189 $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
dc383b6f 190 $level++;
191 } else {
192 $result->toc .= $closelist;
193 $level = $i;
194 }
82605bea 195 $parents[$level] = $sco->parent;
dc383b6f 196 }
197 }
b3659259 198 if ($isvisible) {
199 $result->toc .= "\t\t<li>";
200 }
9fb2de4e 201 if (isset($scoes[$pos+1])) {
202 $nextsco = $scoes[$pos+1];
203 } else {
204 $nextsco = false;
205 }
b3659259 206 $nextisvisible = false;
82605bea 207 if (($nextsco !== false) && (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true')))) {
208 $nextisvisible = true;
b3659259 209 }
210 if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
dc383b6f 211 $sublist++;
212 $icon = 'minus';
213 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
214 $icon = 'plus';
215 }
97852875 216 $result->toc .= '<a href="javascript:expandCollide(\'img'.$sublist.'\',\'s'.$sublist.'\','.$nextsco->id.');"><img id="img'.$sublist.'" src="'.$scormpixdir.'/'.$icon.'.gif" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
b3659259 217 } else if ($isvisible) {
d9e6517a 218 $result->toc .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
dc383b6f 219 }
220 if (empty($sco->title)) {
221 $sco->title = $sco->identifier;
222 }
223 if (!empty($sco->launch)) {
b3659259 224 if ($isvisible) {
225 $startbold = '';
226 $endbold = '';
227 $score = '';
228 if (empty($scoid) && ($mode != 'normal')) {
229 $scoid = $sco->id;
530a61d5 230 }
b3659259 231 if (isset($usertracks[$sco->identifier])) {
232 $usertrack = $usertracks[$sco->identifier];
233 $strstatus = get_string($usertrack->status,'scorm');
234 if ($sco->scormtype == 'sco') {
235 $statusicon = '<img src="'.$scormpixdir.'/'.$usertrack->status.'.gif" alt="'.$strstatus.'" title="'.$strstatus.'" />';
236 } else {
409375ee 237 $statusicon = '<img src="'.$scormpixdir.'/assetc.gif" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
b3659259 238 }
d9e6517a 239
b3659259 240 if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
241 $incomplete = true;
242 if ($play && empty($scoid)) {
243 $scoid = $sco->id;
244 }
245 }
246 if ($usertrack->score_raw != '') {
247 $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
248 }
249 $strsuspended = get_string('suspended','scorm');
250 if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
7ef16bf9 251 if($usertrack->status !='completed') {
252 $statusicon = '<img src="'.$scormpixdir.'/suspend.gif" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
253 }
b3659259 254 }
255 } else {
dc383b6f 256 if ($play && empty($scoid)) {
257 $scoid = $sco->id;
258 }
409375ee 259 $incomplete = true;
b3659259 260 if ($sco->scormtype == 'sco') {
261 $statusicon = '<img src="'.$scormpixdir.'/notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
b3659259 262 } else {
263 $statusicon = '<img src="'.$scormpixdir.'/asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
264 }
dc383b6f 265 }
b3659259 266 if ($sco->id == $scoid) {
b3659259 267 $startbold = '<b>';
268 $endbold = '</b>';
269 $findnext = true;
82605bea 270 $shownext = isset($sco->next) ? $sco->next : 0;
271 $showprev = isset($sco->previous) ? $sco->previous : 0;
dc383b6f 272 }
d9e6517a 273
b3659259 274 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
275 if (!empty($sco->launch)) {
276 $previd = $sco->id;
277 }
dc383b6f 278 }
b3659259 279 if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
280 if ($sco->id == $scoid) {
281 $result->prerequisites = true;
282 }
283 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
284 $result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
285 $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
286 } else {
287 if ($sco->id == $scoid) {
288 $result->prerequisites = false;
289 }
c852a628 290 $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."</li>\n";
dc383b6f 291 }
dc383b6f 292 }
293 } else {
c852a628 294 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
dc383b6f 295 }
296 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
297 if (!empty($nextsco->launch)) {
298 $nextid = $nextsco->id;
299 }
300 }
301 }
302 for ($i=0;$i<$level;$i++) {
303 $result->toc .= "\t\t</ul></li>\n";
304 }
d9e6517a 305
dc383b6f 306 if ($play) {
c6af52a9 307 // it is possible that scoid is still not set, in this case we dont want an empty object
308 if ($scoid) {
309 $sco = scorm_get_sco($scoid);
310 }
dc383b6f 311 $sco->previd = $previd;
312 $sco->nextid = $nextid;
313 $result->sco = $sco;
314 $result->incomplete = $incomplete;
315 } else {
316 $result->incomplete = $incomplete;
317 }
318 }
319 $result->toc .= "\t</ul>\n";
320 if ($scorm->hidetoc == 0) {
321 $result->toc .= '
f4ba7e1a 322 <script type="text/javascript">
323 //<![CDATA[
97852875 324 function expandCollide(which,list,item) {
325 var el = document.ids ? document.ids[list] : document.getElementById ? document.getElementById(list) : document.all[list];
c31f631b 326 which = which.substring(0,(which.length));
97852875 327 var el2 = document.ids ? document.ids[which] : document.getElementById ? document.getElementById(which) : document.all[which];
328 if (el.style.display != "none") {
329 el2.src = "'.$scormpixdir.'/plus.gif";
330 el.style.display=\'none\';
dc383b6f 331 new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
332 } else {
97852875 333 el2.src = "'.$scormpixdir.'/minus.gif";
334 el.style.display=\'block\';
dc383b6f 335 new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
336 }
337 }
f4ba7e1a 338 //]]>
dc383b6f 339 </script>'."\n";
340 }
d9e6517a 341
dc383b6f 342 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid=';
343 $result->tocmenu = popup_form($url,$tocmenus, "tocmenu", $sco->id, '', '', '', true);
344
345 return $result;
346}
347
348?>