4 * This is really a little language parser for AICC_SCRIPT
5 * evaluates the expression and returns a boolean answer
6 * see 2.3.2.5.1. Sequencing/Navigation Today - from the SCORM 1.2 spec (CAM).
8 * @param string $prerequisites the aicc_script prerequisites expression
9 * @param array $usertracks the tracked user data of each SCO visited
12 function scorm_eval_prerequisites($prerequisites, $usertracks) {
14 // this is really a little language parser - AICC_SCRIPT is the reference
15 // see 2.3.2.5.1. Sequencing/Navigation Today - from the SCORM 1.2 spec
20 'completed' => 'completed',
22 'incomplete' => 'incomplete',
23 'browsed' => 'browsed',
24 'not attempted' => 'notattempted',
34 // expand the amp entities
35 $prerequisites = preg_replace('/&/', '&', $prerequisites);
36 // find all my parsable tokens
37 $prerequisites = preg_replace('/(&|\||\(|\)|\~)/', '\t$1\t', $prerequisites);
39 $prerequisites = preg_replace('/&/', '&&', $prerequisites);
40 $prerequisites = preg_replace('/\|/', '||', $prerequisites);
41 // now - grab all the tokens
42 $elements = explode('\t', trim($prerequisites));
44 // process each token to build an expression to be evaluated
46 foreach ($elements as $element) {
47 $element = trim($element);
48 if (empty($element)) {
51 if (!preg_match('/^(&&|\|\||\(|\))$/', $element)) {
52 // create each individual expression
53 // search for ~ = <> X*{}
55 // sets like 3*{S34, S36, S37, S39}
56 if (preg_match('/^(\d+)\*\{(.+)\}$/', $element, $matches)) {
57 $repeat = $matches[1];
58 $set = explode(',', $matches[2]);
60 foreach ($set as $setelement) {
61 if (isset($usertracks[$setelement]) &&
62 ($usertracks[$setelement]->status
== 'completed' ||
$usertracks[$element]->status
== 'passed')) {
66 if ($count >= $repeat) {
73 } else if ($element == '~') {
77 } else if (preg_match('/^(.+)(\=|\<\>)(.+)$/', $element, $matches)) {
78 $element = trim($matches[1]);
79 if (isset($usertracks[$element])) {
80 $value = trim(preg_replace('/(\'|\")/', '', $matches[3]));
81 if (isset($statuses[$value])) {
82 $value = $statuses[$value];
84 if ($matches[2] == '<>') {
89 $element = '(\''.$usertracks[$element]->status
.'\' '.$oper.' \''.$value.'\')';
94 // everything else must be an element defined like S45 ...
96 if (isset($usertracks[$element]) &&
97 ($usertracks[$element]->status
== 'completed' ||
$usertracks[$element]->status
== 'passed')) {
105 $stack []= ' '.$element.' ';
107 return eval('return '.implode($stack).';');
110 function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
113 $strexpand = get_string('expcoll','scorm');
115 if ($mode == 'browse') {
116 $modestr = '&mode='.$mode;
118 $scormpixdir = $CFG->modpixpath
.'/scorm/pix';
120 $result = new stdClass();
121 $result->toc
= "<ul id='s0' class='$liststyle'>\n";
123 $result->prerequisites
= true;
127 // Get the current organization infos
129 if (!empty($currentorg)) {
130 if (($organizationtitle = get_field('scorm_scoes','title','scorm',$scorm->id
,'identifier',$currentorg)) != '') {
131 $result->toc
.= "\t<li>$organizationtitle</li>\n";
132 $tocmenus[] = $organizationtitle;
136 // If not specified retrieve the last attempt number
138 if (empty($attempt)) {
139 $attempt = scorm_get_last_attempt($scorm->id
, $user->id
);
141 $result->attemptleft
= $scorm->maxattempt
- $attempt;
142 if ($scoes = scorm_get_scoes($scorm->id
, $currentorg)){
144 // Retrieve user tracking data for each learning object
146 $usertracks = array();
147 foreach ($scoes as $sco) {
148 if (!empty($sco->launch
)) {
149 if ($usertrack = scorm_get_tracks($sco->id
,$user->id
,$attempt)) {
150 if ($usertrack->status
== '') {
151 $usertrack->status
= 'notattempted';
153 $usertracks[$sco->identifier
] = $usertrack;
163 $parents[$level]='/';
165 foreach ($scoes as $pos => $sco) {
167 $sco->title
= stripslashes($sco->title
);
168 if (!isset($sco->isvisible
) ||
(isset($sco->isvisible
) && ($sco->isvisible
== 'true'))) {
171 if ($parents[$level] != $sco->parent
) {
172 if ($newlevel = array_search($sco->parent
,$parents)) {
173 for ($i=0; $i<($level-$newlevel); $i++
) {
174 $result->toc
.= "\t\t</ul></li>\n";
180 while (($i > 0) && ($parents[$level] != $sco->parent
)) {
181 $closelist .= "\t\t</ul></li>\n";
184 if (($i == 0) && ($sco->parent
!= $currentorg)) {
186 if (isset($_COOKIE['hide:SCORMitem'.$sco->id
])) {
187 $style = ' style="display: none;"';
189 $result->toc
.= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
192 $result->toc
.= $closelist;
195 $parents[$level] = $sco->parent
;
199 $result->toc
.= "\t\t<li>";
201 if (isset($scoes[$pos+
1])) {
202 $nextsco = $scoes[$pos+
1];
206 $nextisvisible = false;
207 if (($nextsco !== false) && (!isset($nextsco->isvisible
) ||
(isset($nextsco->isvisible
) && ($nextsco->isvisible
== 'true')))) {
208 $nextisvisible = true;
210 if ($nextisvisible && ($nextsco !== false) && ($sco->parent
!= $nextsco->parent
) && (($level==0) ||
(($level>0) && ($nextsco->parent
== $sco->identifier
)))) {
213 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id
])) {
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>';
217 } else if ($isvisible) {
218 $result->toc
.= '<img src="'.$scormpixdir.'/spacer.gif" />';
220 if (empty($sco->title
)) {
221 $sco->title
= $sco->identifier
;
223 if (!empty($sco->launch
)) {
228 if (empty($scoid) && ($mode != 'normal')) {
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.'" />';
237 $statusicon = '<img src="'.$scormpixdir.'/assetc.gif" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
240 if (($usertrack->status
== 'notattempted') ||
($usertrack->status
== 'incomplete') ||
($usertrack->status
== 'browsed')) {
242 if ($play && empty($scoid)) {
246 if ($usertrack->score_raw
!= '') {
247 $score = '('.get_string('score','scorm').': '.$usertrack->score_raw
.')';
249 $strsuspended = get_string('suspended','scorm');
250 if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
251 if($usertrack->status
!='completed') {
252 $statusicon = '<img src="'.$scormpixdir.'/suspend.gif" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
256 if ($play && empty($scoid)) {
260 if ($sco->scormtype
== 'sco') {
261 $statusicon = '<img src="'.$scormpixdir.'/notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
263 $statusicon = '<img src="'.$scormpixdir.'/asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
266 if ($sco->id
== $scoid) {
270 $shownext = isset($sco->next
) ?
$sco->next
: 0;
271 $showprev = isset($sco->previous
) ?
$sco->previous
: 0;
274 if (($nextid == 0) && (scorm_count_launchable($scorm->id
,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
275 if (!empty($sco->launch
)) {
279 if (empty($sco->prerequisites
) ||
scorm_eval_prerequisites($sco->prerequisites
,$usertracks)) {
280 if ($sco->id
== $scoid) {
281 $result->prerequisites
= true;
283 $url = $CFG->wwwroot
.'/mod/scorm/player.php?a='.$scorm->id
.'&currentorg='.$currentorg.$modestr.'&scoid='.$sco->id
;
284 $result->toc
.= $statusicon.' '.$startbold.'<a href="'.$url.'">'.format_string($sco->title
).'</a>'.$score.$endbold."</li>\n";
285 $tocmenus[$sco->id
] = scorm_repeater('−',$level) . '>' . format_string($sco->title
);
287 if ($sco->id
== $scoid) {
288 $result->prerequisites
= false;
290 $result->toc
.= $statusicon.' '.format_string($sco->title
)."</li>\n";
294 $result->toc
.= ' '.format_string($sco->title
)."</li>\n";
296 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
297 if (!empty($nextsco->launch
)) {
298 $nextid = $nextsco->id
;
302 for ($i=0;$i<$level;$i++
) {
303 $result->toc
.= "\t\t</ul></li>\n";
307 // it is possible that scoid is still not set, in this case we dont want an empty object
309 $sco = scorm_get_sco($scoid);
311 $sco->previd
= $previd;
312 $sco->nextid
= $nextid;
314 $result->incomplete
= $incomplete;
316 $result->incomplete
= $incomplete;
319 $result->toc
.= "\t</ul>\n";
320 if ($scorm->hidetoc
== 0) {
322 <script type="text/javascript">
324 function expandCollide(which,list,item) {
325 var el = document.ids ? document.ids[list] : document.getElementById ? document.getElementById(list) : document.all[list];
326 which = which.substring(0,(which.length));
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\';
331 new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
333 el2.src = "'.$scormpixdir.'/minus.gif";
334 el.style.display=\'block\';
335 new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
342 $url = $CFG->wwwroot
.'/mod/scorm/player.php?a='.$scorm->id
.'&currentorg='.$currentorg.$modestr.'&scoid=';
343 $result->tocmenu
= popup_form($url,$tocmenus, "tocmenu", $sco->id
, '', '', '', true);