MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / scorm / datamodels / scorm_12lib.php
blob797041998fcc31f8d174fe92b265a23704c45826
1 <?php // $Id$
3 function scorm_eval_prerequisites($prerequisites,$usertracks) {
4 $element = '';
5 $stack = array();
6 $statuses = array(
7 'passed' => 'passed',
8 'completed' => 'completed',
9 'failed' => 'failed',
10 'incomplete' => 'incomplete',
11 'browsed' => 'browsed',
12 'not attempted' => 'notattempted',
13 'p' => 'passed',
14 'c' => 'completed',
15 'f' => 'failed',
16 'i' => 'incomplete',
17 'b' => 'browsed',
18 'n' => 'notattempted'
20 $i=0;
21 while ($i<strlen($prerequisites)) {
22 $symbol = $prerequisites[$i];
23 switch ($symbol) {
24 case '&':
25 case '|':
26 $symbol .= $symbol;
27 case '~':
28 case '(':
29 case ')':
30 case '*':
31 $element = trim($element);
33 if (!empty($element)) {
34 $element = trim($element);
35 if (isset($usertracks[$element])) {
36 $element = '((\''.$usertracks[$element]->status.'\' == \'completed\') || '.
37 '(\''.$usertracks[$element]->status.'\' == \'passed\'))';
38 } else if (($operator = strpos($element,'=')) !== false) {
39 $item = trim(substr($element,0,$operator));
40 if (!isset($usertracks[$item])) {
41 return false;
44 $value = trim(trim(substr($element,$operator+1)),'"');
45 if (isset($statuses[$value])) {
46 $status = $statuses[$value];
47 } else {
48 return false;
51 $element = '(\''.$usertracks[$item]->status.'\' == \''.$status.'\')';
52 } else if (($operator = strpos($element,'<>')) !== false) {
53 $item = trim(substr($element,0,$operator));
54 if (!isset($usertracks[$item])) {
55 return false;
58 $value = trim(trim(substr($element,$operator+2)),'"');
59 if (isset($statuses[$value])) {
60 $status = $statuses[$value];
61 } else {
62 return false;
65 $element = '(\''.$usertracks[$item]->status.'\' != \''.$status.'\')';
66 } else if (is_numeric($element)) {
67 if ($symbol == '*') {
68 $symbol = '';
69 $open = strpos($prerequisites,'{',$i);
70 $opened = 1;
71 $closed = 0;
72 for ($close=$open+1; (($opened > $closed) && ($close<strlen($prerequisites))); $close++) {
73 if ($prerequisites[$close] == '}') {
74 $closed++;
75 } else if ($prerequisites[$close] == '{') {
76 $opened++;
79 $i = $close;
81 $setelements = explode(',', substr($prerequisites, $open+1, $close-($open+1)-1));
82 $settrue = 0;
83 foreach ($setelements as $setelement) {
84 if (scorm_eval_prerequisites($setelement,$usertracks)) {
85 $settrue++;
89 if ($settrue >= $element) {
90 $element = 'true';
91 } else {
92 $element = 'false';
95 } else {
96 return false;
99 array_push($stack,$element);
100 $element = '';
102 if ($symbol == '~') {
103 $symbol = '!';
105 if (!empty($symbol)) {
106 array_push($stack,$symbol);
108 break;
109 default:
110 $element .= $symbol;
111 break;
113 $i++;
115 if (!empty($element)) {
116 $element = trim($element);
117 if (isset($usertracks[$element])) {
118 $element = '((\''.$usertracks[$element]->status.'\' == \'completed\') || '.
119 '(\''.$usertracks[$element]->status.'\' == \'passed\'))';
120 } else if (($operator = strpos($element,'=')) !== false) {
121 $item = trim(substr($element,0,$operator));
122 if (!isset($usertracks[$item])) {
123 return false;
126 $value = trim(trim(substr($element,$operator+1)),'"');
127 if (isset($statuses[$value])) {
128 $status = $statuses[$value];
129 } else {
130 return false;
133 $element = '(\''.$usertracks[$item]->status.'\' == \''.$status.'\')';
134 } else if (($operator = strpos($element,'<>')) !== false) {
135 $item = trim(substr($element,0,$operator));
136 if (!isset($usertracks[$item])) {
137 return false;
140 $value = trim(trim(substr($element,$operator+1)),'"');
141 if (isset($statuses[$value])) {
142 $status = $statuses[$value];
143 } else {
144 return false;
147 $element = '(\''.$usertracks[$item]->status.'\' != \''.trim($status).'\')';
148 } else {
149 return false;
152 array_push($stack,$element);
154 return eval('return '.implode($stack).';');
157 function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
158 global $CFG;
160 $strexpand = get_string('expcoll','scorm');
161 $modestr = '';
162 if ($mode == 'browse') {
163 $modestr = '&amp;mode='.$mode;
165 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
167 $result = new stdClass();
168 $result->toc = "<ul id='s0' class='$liststyle'>\n";
169 $tocmenus = array();
170 $result->prerequisites = true;
171 $incomplete = false;
174 // Get the current organization infos
176 $organizationsql = '';
177 if (!empty($currentorg)) {
178 if (($organizationtitle = get_field('scorm_scoes','title','scorm',$scorm->id,'identifier',$currentorg)) != '') {
179 $result->toc .= "\t<li>$organizationtitle</li>\n";
180 $tocmenus[] = $organizationtitle;
182 $organizationsql = "AND organization='$currentorg'";
185 // If not specified retrieve the last attempt number
187 if (empty($attempt)) {
188 $attempt = scorm_get_last_attempt($scorm->id, $user->id);
190 $result->attemptleft = $scorm->maxattempt - $attempt;
191 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
193 // Retrieve user tracking data for each learning object
195 $usertracks = array();
196 foreach ($scoes as $sco) {
197 if (!empty($sco->launch)) {
198 if ($usertrack=scorm_get_tracks($sco->id,$user->id,$attempt)) {
199 if ($usertrack->status == '') {
200 $usertrack->status = 'notattempted';
202 $usertracks[$sco->identifier] = $usertrack;
207 $level=0;
208 $sublist=1;
209 $previd = 0;
210 $nextid = 0;
211 $findnext = false;
212 $parents[$level]='/';
214 foreach ($scoes as $sco) {
215 $isvisible = false;
216 if ($optionaldatas = scorm_get_sco($sco->id, SCO_DATA)) {
217 if (!isset($optionaldatas->isvisible) || (isset($optionaldatas->isvisible) && ($optionaldatas->isvisible == 'true'))) {
218 $isvisible = true;
221 else{
222 $isvisible = true;
224 if ($parents[$level]!=$sco->parent) {
225 if ($newlevel = array_search($sco->parent,$parents)) {
226 for ($i=0; $i<($level-$newlevel); $i++) {
227 $result->toc .= "\t\t</ul></li>\n";
229 $level = $newlevel;
230 } else {
231 $i = $level;
232 $closelist = '';
233 while (($i > 0) && ($parents[$level] != $sco->parent)) {
234 $closelist .= "\t\t</ul></li>\n";
235 $i--;
237 if (($i == 0) && ($sco->parent != $currentorg)) {
238 $style = '';
239 if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
240 $style = ' style="display: none;"';
242 $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
243 $level++;
244 } else {
245 $result->toc .= $closelist;
246 $level = $i;
248 $parents[$level]=$sco->parent;
251 if ($isvisible) {
252 $result->toc .= "\t\t<li>";
254 $nextsco = next($scoes);
255 $nextisvisible = false;
256 if (($nextsco !== false) && ($optionaldatas = scorm_get_sco($nextsco->id, SCO_DATA))) {
257 if (!isset($optionaldatas->isvisible) || (isset($optionaldatas->isvisible) && ($optionaldatas->isvisible == 'true'))) {
258 $nextisvisible = true;
261 if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
262 $sublist++;
263 $icon = 'minus';
264 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
265 $icon = 'plus';
267 $result->toc .= '<a href="javascript:expandCollide(\'img'.$sublist.'\','.$sublist.','.$nextsco->id.');"><img id="img'.$sublist.'" src="'.$scormpixdir.'/'.$icon.'.gif" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
268 // $result->toc .= '<a href="#" onclick="elementToggleHide(\''.$sublist.'\',true);"><img id="img'.$sublist.'" src="'.$scormpixdir.'/'.$icon.'.gif" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
269 } else if ($isvisible) {
270 $result->toc .= '<img src="'.$scormpixdir.'/spacer.gif" />';
272 if (empty($sco->title)) {
273 $sco->title = $sco->identifier;
275 if (!empty($sco->launch)) {
276 if ($isvisible) {
277 $startbold = '';
278 $endbold = '';
279 $score = '';
280 if (empty($scoid) && ($mode != 'normal')) {
281 $scoid = $sco->id;
283 if (isset($usertracks[$sco->identifier])) {
284 $usertrack = $usertracks[$sco->identifier];
285 $strstatus = get_string($usertrack->status,'scorm');
286 if ($sco->scormtype == 'sco') {
287 $statusicon = '<img src="'.$scormpixdir.'/'.$usertrack->status.'.gif" alt="'.$strstatus.'" title="'.$strstatus.'" />';
288 } else {
289 $statusicon = '<img src="'.$scormpixdir.'/assetc.gif" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
292 if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
293 $incomplete = true;
294 if ($play && empty($scoid)) {
295 $scoid = $sco->id;
298 if ($usertrack->score_raw != '') {
299 $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
301 $strsuspended = get_string('suspended','scorm');
302 if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
303 $statusicon = '<img src="'.$scormpixdir.'/suspend.gif" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
305 } else {
306 if ($play && empty($scoid)) {
307 $scoid = $sco->id;
309 $incomplete = true;
310 if ($sco->scormtype == 'sco') {
311 $statusicon = '<img src="'.$scormpixdir.'/notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
312 } else {
313 $statusicon = '<img src="'.$scormpixdir.'/asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
316 if ($sco->id == $scoid) {
317 $scodata = scorm_get_sco($sco->id, SCO_DATA);
318 $startbold = '<b>';
319 $endbold = '</b>';
320 $findnext = true;
321 $shownext = isset($scodata->next) ? $scodata->next : 0;
322 $showprev = isset($scodata->previous) ? $scodata->previous : 0;
325 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
326 if (!empty($sco->launch)) {
327 $previd = $sco->id;
330 if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
331 if ($sco->id == $scoid) {
332 $result->prerequisites = true;
334 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
335 $result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
336 $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
337 } else {
338 if ($sco->id == $scoid) {
339 $result->prerequisites = false;
341 $result->toc .= $statusicon.'&nbsp;'.$sco->title."</li>\n";
344 } else {
345 $result->toc .= '&nbsp;'.$sco->title."</li>\n";
347 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
348 if (!empty($nextsco->launch)) {
349 $nextid = $nextsco->id;
353 for ($i=0;$i<$level;$i++) {
354 $result->toc .= "\t\t</ul></li>\n";
357 if ($play) {
358 $sco = scorm_get_sco($scoid);
359 $sco->previd = $previd;
360 $sco->nextid = $nextid;
361 $result->sco = $sco;
362 $result->incomplete = $incomplete;
363 } else {
364 $result->incomplete = $incomplete;
367 $result->toc .= "\t</ul>\n";
368 if ($scorm->hidetoc == 0) {
369 $result->toc .= '
370 <script type="text/javascript">
371 //<![CDATA[
372 function expandCollide(which,list,item) {
373 var nn=document.ids?true:false
374 var w3c=document.getElementById?true:false
375 var beg=nn?"document.ids.":w3c?"document.getElementById(":"document.all.";
377 var mid=w3c?").style":".style";
378 which = which.substring(0,(which.length));
380 if (eval(beg+list+mid+".display") != "none") {
381 document.getElementById(which).src = "'.$scormpixdir.'/plus.gif";
382 eval(beg+list+mid+".display=\'none\';");
383 new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
384 } else {
385 document.getElementById(which).src = "'.$scormpixdir.'/minus.gif";
386 eval(beg+list+mid+".display=\'block\';");
387 new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
390 //]]>
391 </script>'."\n";
394 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid=';
395 $result->tocmenu = popup_form($url,$tocmenus, "tocmenu", $sco->id, '', '', '', true);
397 return $result;