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