SCORM MDL-25781 fix links in TOC on view.php
[moodle.git] / mod / scorm / datamodels / scorm_12lib.php
CommitLineData
e5dd8e3b 1<?php
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]) &&
79ef247a 61 ($usertracks[$setelement]->status == 'completed' || $usertracks[$setelement]->status == 'passed')) {
82605bea 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
08868f58 109function scorm_get_toc($user,$scorm,$toclink=TOCJSLINK,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false, $tocheader=false) {
d436d197 110 global $CFG, $DB, $PAGE, $OUTPUT;
d9e6517a 111
dc383b6f 112 $strexpand = get_string('expcoll','scorm');
113 $modestr = '';
114 if ($mode == 'browse') {
115 $modestr = '&amp;mode='.$mode;
d9e6517a 116 }
d9e6517a 117
dc383b6f 118 $result = new stdClass();
5c2aa157
DM
119 //$result->toc = "<ul id='s0' class='$liststyle'>\n";
120 if ($tocheader) {
121 $result->toc = '<div id="scorm_layout">';
122 $result->toc .= '<div id="scorm_toc">';
123 $result->toc .= '<div id="scorm_tree">';
124 }
125 $result->toc .= '<ul>';
dc383b6f 126 $tocmenus = array();
127 $result->prerequisites = true;
128 $incomplete = false;
d9e6517a 129
dc383b6f 130 //
131 // Get the current organization infos
132 //
dc383b6f 133 if (!empty($currentorg)) {
bf347041 134 if (($organizationtitle = $DB->get_field('scorm_scoes','title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
5c2aa157
DM
135 if ($play) {
136 $result->toctitle = "$organizationtitle";
137 }
138 else {
139 $result->toc .= "\t<li>$organizationtitle</li>\n";
140 }
dc383b6f 141 $tocmenus[] = $organizationtitle;
142 }
dc383b6f 143 }
5c2aa157 144
dc383b6f 145 //
146 // If not specified retrieve the last attempt number
147 //
148 if (empty($attempt)) {
b1ca5d31 149 $attempt = scorm_get_attempt_count($user->id, $scorm);
dc383b6f 150 }
983befaf 151 $result->attemptleft = $scorm->maxattempt == 0 ? 1 : $scorm->maxattempt - $attempt;
bf347041 152 $conditions['scorm'] = $scorm->id;
82605bea 153 if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
dc383b6f 154 //
155 // Retrieve user tracking data for each learning object
9fb2de4e 156 //
dc383b6f 157 $usertracks = array();
158 foreach ($scoes as $sco) {
159 if (!empty($sco->launch)) {
82605bea 160 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
dc383b6f 161 if ($usertrack->status == '') {
162 $usertrack->status = 'notattempted';
163 }
164 $usertracks[$sco->identifier] = $usertrack;
165 }
166 }
167 }
168
169 $level=0;
170 $sublist=1;
171 $previd = 0;
172 $nextid = 0;
173 $findnext = false;
174 $parents[$level]='/';
d9e6517a 175
82605bea 176 foreach ($scoes as $pos => $sco) {
b3659259 177 $isvisible = false;
bf347041 178 $sco->title = $sco->title;
82605bea 179 if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
e73457e4 180 $isvisible = true;
eb0e9d9d 181 }
82605bea 182 if ($parents[$level] != $sco->parent) {
dc383b6f 183 if ($newlevel = array_search($sco->parent,$parents)) {
184 for ($i=0; $i<($level-$newlevel); $i++) {
5c2aa157 185 $result->toc .= "\t\t</li></ul></li>\n";
dc383b6f 186 }
187 $level = $newlevel;
188 } else {
189 $i = $level;
190 $closelist = '';
191 while (($i > 0) && ($parents[$level] != $sco->parent)) {
5c2aa157 192 $closelist .= "\t\t</li></ul></li>\n";
dc383b6f 193 $i--;
194 }
195 if (($i == 0) && ($sco->parent != $currentorg)) {
196 $style = '';
197 if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
198 $style = ' style="display: none;"';
199 }
5c2aa157
DM
200 //$result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
201 $result->toc .= "\t\t<ul>\n";
202
203
204
dc383b6f 205 $level++;
206 } else {
207 $result->toc .= $closelist;
208 $level = $i;
209 }
82605bea 210 $parents[$level] = $sco->parent;
dc383b6f 211 }
212 }
b3659259 213 if ($isvisible) {
5c2aa157 214 $result->toc .= "<li>";
b3659259 215 }
9fb2de4e 216 if (isset($scoes[$pos+1])) {
217 $nextsco = $scoes[$pos+1];
218 } else {
219 $nextsco = false;
220 }
b3659259 221 $nextisvisible = false;
82605bea 222 if (($nextsco !== false) && (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true')))) {
223 $nextisvisible = true;
b3659259 224 }
225 if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
dc383b6f 226 $sublist++;
227 $icon = 'minus';
228 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
229 $icon = 'plus';
230 }
5c2aa157 231 //$result->toc .= '<a href="javascript:expandCollide(\'img'.$sublist.'\',\'s'.$sublist.'\','.$nextsco->id.');"><img id="img'.$sublist.'" src="'.$OUTPUT->pix_url($icon, 'scorm').'" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
b3659259 232 } else if ($isvisible) {
5c2aa157 233 //$result->toc .= '<img src="'.$OUTPUT->pix_url('spacer', 'scorm').'" alt="" />';
dc383b6f 234 }
235 if (empty($sco->title)) {
236 $sco->title = $sco->identifier;
237 }
238 if (!empty($sco->launch)) {
b3659259 239 if ($isvisible) {
240 $startbold = '';
241 $endbold = '';
242 $score = '';
243 if (empty($scoid) && ($mode != 'normal')) {
244 $scoid = $sco->id;
530a61d5 245 }
b3659259 246 if (isset($usertracks[$sco->identifier])) {
247 $usertrack = $usertracks[$sco->identifier];
248 $strstatus = get_string($usertrack->status,'scorm');
249 if ($sco->scormtype == 'sco') {
485f4ce6 250 $statusicon = '<img src="'.$OUTPUT->pix_url($usertrack->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" />';
b3659259 251 } else {
485f4ce6 252 $statusicon = '<img src="'.$OUTPUT->pix_url('assetc', 'scorm').'" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
b3659259 253 }
d9e6517a 254
b3659259 255 if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
256 $incomplete = true;
257 if ($play && empty($scoid)) {
258 $scoid = $sco->id;
259 }
260 }
261 if ($usertrack->score_raw != '') {
262 $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
263 }
264 $strsuspended = get_string('suspended','scorm');
946b6f0d
DM
265 if ($incomplete && isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
266 $statusicon = '<img src="'.$OUTPUT->pix_url('suspend', 'scorm').'" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
b3659259 267 }
268 } else {
dc383b6f 269 if ($play && empty($scoid)) {
270 $scoid = $sco->id;
271 }
409375ee 272 $incomplete = true;
b3659259 273 if ($sco->scormtype == 'sco') {
485f4ce6 274 $statusicon = '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
b3659259 275 } else {
485f4ce6 276 $statusicon = '<img src="'.$OUTPUT->pix_url('asset', 'scorm').'" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
b3659259 277 }
dc383b6f 278 }
b3659259 279 if ($sco->id == $scoid) {
b3659259 280 $startbold = '<b>';
281 $endbold = '</b>';
282 $findnext = true;
82605bea 283 $shownext = isset($sco->next) ? $sco->next : 0;
284 $showprev = isset($sco->previous) ? $sco->previous : 0;
dc383b6f 285 }
d9e6517a 286
b3659259 287 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
288 if (!empty($sco->launch)) {
289 $previd = $sco->id;
290 }
dc383b6f 291 }
b3659259 292 if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
293 if ($sco->id == $scoid) {
294 $result->prerequisites = true;
295 }
5c2aa157 296 $thisscoidstr = '&scoid='.$sco->id;
5c2aa157
DM
297 $link = 'a='.$scorm->id.$thisscoidstr.'&currentorg='.$currentorg.$modestr.'&attempt='.$attempt;
298
08868f58
DM
299 if ($toclink == TOCFULLURL) { //display toc with urls for structure page
300 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
301 $result->toc .= $statusicon.'&nbsp;<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score."\n";
302 } else { //display toc for inside scorm player
303 if ($sco->launch) {
304 $result->toc .= '<a title="'.$link.'">'.$statusicon.'&nbsp;'.format_string($sco->title).'&nbsp;'.$score.'</a>';
305 } else {
306 $result->toc .= '<span>'.$statusicon.'&nbsp;'.format_string($sco->title).'</span>';
307 }
5c2aa157 308 }
b3659259 309 $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
310 } else {
311 if ($sco->id == $scoid) {
312 $result->prerequisites = false;
313 }
5c2aa157
DM
314 if ($play) {
315 // should be disabled
316 $result->toc .= '<span>'.$statusicon.'&nbsp;'.format_string($sco->title).'</span>';
317 }
318 else {
319 $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."\n";
320 }
321 }
322 if (($nextsco === false) || $nextsco->parent == $sco->parent) {
eea0c8ea 323 $result->toc .= '</li>';
dc383b6f 324 }
dc383b6f 325 }
326 } else {
5c2aa157 327 $result->toc .= '&nbsp;'.format_string($sco->title)."\n";
dc383b6f 328 }
329 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
330 if (!empty($nextsco->launch)) {
331 $nextid = $nextsco->id;
332 }
333 }
334 }
335 for ($i=0;$i<$level;$i++) {
336 $result->toc .= "\t\t</ul></li>\n";
337 }
d9e6517a 338
dc383b6f 339 if ($play) {
c6af52a9 340 // it is possible that scoid is still not set, in this case we dont want an empty object
341 if ($scoid) {
342 $sco = scorm_get_sco($scoid);
343 }
dc383b6f 344 $sco->previd = $previd;
345 $sco->nextid = $nextid;
346 $result->sco = $sco;
347 $result->incomplete = $incomplete;
348 } else {
349 $result->incomplete = $incomplete;
350 }
351 }
5c2aa157
DM
352 $result->toc .= '</ul>';
353
354 // NEW IMS TOC
355 if ($tocheader) {
356 $result->toc .= '</div></div></div>';
357 $result->toc .= '<div id="scorm_navpanel"></div>';
358 }
359
360
361
dc383b6f 362 if ($scorm->hidetoc == 0) {
d436d197 363 $PAGE->requires->data_for_js('scormdata', array(
485f4ce6
PH
364 'plusicon' => $OUTPUT->pix_url('plus', 'scorm'),
365 'minusicon' => $OUTPUT->pix_url('minus', 'scorm')));
9dec75db
PS
366 $PAGE->requires->js('/lib/cookies.js');
367 $PAGE->requires->js('/mod/scorm/datamodels/scorm_datamodels.js');
dc383b6f 368 }
d9e6517a 369
f8dab966
PS
370 $url = new moodle_url('/mod/scorm/player.php?a='.$scorm->id.'&currentorg='.$currentorg.$modestr);
371 $result->tocmenu = $OUTPUT->single_select($url, 'scoid', $tocmenus, $sco->id, null, "tocmenu");
dc383b6f 372
373 return $result;
374}
375
e5dd8e3b 376