MDL-11892 Propogate Fixes for 'calls to expand javascript' to SCORM 1.2 code. Minor...
[moodle.git] / mod / scorm / datamodels / scorm_12lib.php
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'
19                 );
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);
32                 
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;
42                         }
43                         
44                         $value = trim(trim(substr($element,$operator+1)),'"');
45                         if (isset($statuses[$value])) {
46                             $status = $statuses[$value];
47                         } else {
48                             return false;
49                         }
50                                               
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;
56                         }
57                         
58                         $value = trim(trim(substr($element,$operator+2)),'"');
59                         if (isset($statuses[$value])) {
60                             $status = $statuses[$value];
61                         } else {
62                             return false;
63                         }
64                         
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++;
77                                  }
78                             } 
79                             $i = $close;
80                             
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++;
86                                 }
87                             }
88                             
89                             if ($settrue >= $element) {
90                                 $element = 'true'; 
91                             } else {
92                                 $element = 'false';
93                             }
94                         }
95                     } else {
96                         return false;
97                     }
98                     
99                     array_push($stack,$element);
100                     $element = '';
101                 }
102                 if ($symbol == '~') {
103                     $symbol = '!';
104                 }
105                 if (!empty($symbol)) {
106                     array_push($stack,$symbol);
107                 }
108             break;
109             default:
110                 $element .= $symbol;
111             break;
112         }
113         $i++;
114     }
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;
124             }
125             
126             $value = trim(trim(substr($element,$operator+1)),'"');
127             if (isset($statuses[$value])) {
128                 $status = $statuses[$value];
129             } else {
130                 return false;
131             }
132             
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;
138             }
139             
140             $value = trim(trim(substr($element,$operator+1)),'"');
141             if (isset($statuses[$value])) {
142                 $status = $statuses[$value];
143             } else {
144                 return false;
145             }
146             
147             $element = '(\''.$usertracks[$item]->status.'\' != \''.trim($status).'\')';
148         } else {
149             return false;
150         }
151         
152         array_push($stack,$element);
153     }
154     return eval('return '.implode($stack).';');
157 function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
158     global $CFG, $DB;
159     
160     $strexpand = get_string('expcoll','scorm');
161     $modestr = '';
162     if ($mode == 'browse') {
163         $modestr = '&amp;mode='.$mode;
164     } 
165     $scormpixdir = $CFG->modpixpath.'/scorm/pix';
166     
167     $result = new stdClass();
168     $result->toc = "<ul id='s0' class='$liststyle'>\n";
169     $tocmenus = array();
170     $result->prerequisites = true;
171     $incomplete = false;
172     
173     //
174     // Get the current organization infos
175     //
176     $conditions = array();;
177     if (!empty($currentorg)) {
178         if (($organizationtitle = $DB->get_field('scorm_scoes','title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
179             $result->toc .= "\t<li>$organizationtitle</li>\n";
180             $tocmenus[] = $organizationtitle;
181         }
182         $conditions['organization'] = $currentorg;
183     }
184     //
185     // If not specified retrieve the last attempt number
186     //
187     if (empty($attempt)) {
188         $attempt = scorm_get_last_attempt($scorm->id, $user->id);
189     }
190     $result->attemptleft = $scorm->maxattempt - $attempt;
191     $conditions['scorm'] = $scorm->id;
192     if ($scoes = $DB->get_records('scorm_scoes', $conditions, "id ASC")){
193         // drop keys so that we can access array sequentially
194         $scoes = array_values($scoes); 
195         //
196         // Retrieve user tracking data for each learning object
197         //
198         $usertracks = array();
199         foreach ($scoes as $sco) {
200             if (!empty($sco->launch)) {
201                 if ($usertrack=scorm_get_tracks($sco->id,$user->id,$attempt)) {
202                     if ($usertrack->status == '') {
203                         $usertrack->status = 'notattempted';
204                     }
205                     $usertracks[$sco->identifier] = $usertrack;
206                 }
207             }
208         }
210         $level=0;
211         $sublist=1;
212         $previd = 0;
213         $nextid = 0;
214         $findnext = false;
215         $parents[$level]='/';
216         
217         foreach ($scoes as $pos=>$sco) {
218             $isvisible = false;
219             $sco->title = $sco->title;
220             if ($optionaldatas = scorm_get_sco($sco->id, SCO_DATA)) {
221                 if (!isset($optionaldatas->isvisible) || (isset($optionaldatas->isvisible) && ($optionaldatas->isvisible == 'true'))) {
222                     $isvisible = true;
223                 }
224             }
225             else{
226                 $isvisible = true;
227             }
228             if ($parents[$level]!=$sco->parent) {
229                 if ($newlevel = array_search($sco->parent,$parents)) {
230                     for ($i=0; $i<($level-$newlevel); $i++) {
231                         $result->toc .= "\t\t</ul></li>\n";
232                     }
233                     $level = $newlevel;
234                 } else {
235                     $i = $level;
236                     $closelist = '';
237                     while (($i > 0) && ($parents[$level] != $sco->parent)) {
238                         $closelist .= "\t\t</ul></li>\n";
239                         $i--;
240                     }
241                     if (($i == 0) && ($sco->parent != $currentorg)) {
242                         $style = '';
243                         if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
244                             $style = ' style="display: none;"';
245                         }
246                         $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
247                         $level++;
248                     } else {
249                         $result->toc .= $closelist;
250                         $level = $i;
251                     }
252                     $parents[$level]=$sco->parent;
253                 }
254             }
255             if ($isvisible) {
256                 $result->toc .= "\t\t<li>";
257             }
258             if (isset($scoes[$pos+1])) {
259                 $nextsco = $scoes[$pos+1];
260             } else {
261                 $nextsco = false;
262             }
263             $nextisvisible = false;
264             if (($nextsco !== false) && ($optionaldatas = scorm_get_sco($nextsco->id, SCO_DATA))) {
265                 if (!isset($optionaldatas->isvisible) || (isset($optionaldatas->isvisible) && ($optionaldatas->isvisible == 'true'))) {
266                     $nextisvisible = true;
267                 }
268             }
269             if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
270                 $sublist++;
271                 $icon = 'minus';
272                 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
273                     $icon = 'plus';
274                 }
275                 $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>';
276             } else if ($isvisible) {
277                 $result->toc .= '<img src="'.$scormpixdir.'/spacer.gif" />';
278             }
279             if (empty($sco->title)) {
280                 $sco->title = $sco->identifier;
281             }
282             if (!empty($sco->launch)) {
283                 if ($isvisible) {
284                     $startbold = '';
285                     $endbold = '';
286                     $score = '';
287                     if (empty($scoid) && ($mode != 'normal')) {
288                         $scoid = $sco->id;
289                     }
290                     if (isset($usertracks[$sco->identifier])) {
291                         $usertrack = $usertracks[$sco->identifier];
292                         $strstatus = get_string($usertrack->status,'scorm');
293                         if ($sco->scormtype == 'sco') {
294                             $statusicon = '<img src="'.$scormpixdir.'/'.$usertrack->status.'.gif" alt="'.$strstatus.'" title="'.$strstatus.'" />';
295                         } else {
296                             $statusicon = '<img src="'.$scormpixdir.'/assetc.gif" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
297                         }
298                     
299                         if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
300                             $incomplete = true;
301                             if ($play && empty($scoid)) {
302                                 $scoid = $sco->id;
303                             }
304                         }
305                         if ($usertrack->score_raw != '') {
306                             $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
307                         }
308                         $strsuspended = get_string('suspended','scorm');
309                         if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
310                             $statusicon = '<img src="'.$scormpixdir.'/suspend.gif" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
311                         }
312                     } else {
313                         if ($play && empty($scoid)) {
314                             $scoid = $sco->id;
315                         }
316                         $incomplete = true;
317                         if ($sco->scormtype == 'sco') {
318                             $statusicon = '<img src="'.$scormpixdir.'/notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
319                         } else {
320                             $statusicon = '<img src="'.$scormpixdir.'/asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
321                         }
322                     }
323                     if ($sco->id == $scoid) {
324                         $scodata = scorm_get_sco($sco->id, SCO_DATA);
325                         $startbold = '<b>';
326                         $endbold = '</b>';
327                         $findnext = true;
328                         $shownext = isset($scodata->next) ? $scodata->next : 0;
329                         $showprev = isset($scodata->previous) ? $scodata->previous : 0;
330                     }
331                 
332                     if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
333                         if (!empty($sco->launch)) {
334                             $previd = $sco->id;
335                         }
336                     }
337                     if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
338                         if ($sco->id == $scoid) {
339                             $result->prerequisites = true;
340                         }
341                         $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
342                         $result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
343                         $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
344                     } else {
345                         if ($sco->id == $scoid) {
346                             $result->prerequisites = false;
347                         }
348                         $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."</li>\n";
349                     }
350                 }
351             } else {
352                 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
353             }
354             if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
355                 if (!empty($nextsco->launch)) {
356                     $nextid = $nextsco->id;
357                 }
358             }
359         }
360         for ($i=0;$i<$level;$i++) {
361             $result->toc .= "\t\t</ul></li>\n";
362         }
363         
364         if ($play) {
365             // it is possible that scoid is still not set, in this case we dont want an empty object
366             if ($scoid) {
367                 $sco = scorm_get_sco($scoid);
368             }
369             $sco->previd = $previd;
370             $sco->nextid = $nextid;
371             $result->sco = $sco;
372             $result->incomplete = $incomplete;
373         } else {
374             $result->incomplete = $incomplete;
375         }
376     }
377     $result->toc .= "\t</ul>\n";
378     if ($scorm->hidetoc == 0) {
379         $result->toc .= '
380           <script type="text/javascript">
381           //<![CDATA[
382               function expandCollide(which,list,item) {
383                                   var el = document.ids ? document.ids[list] : document.getElementById ? document.getElementById(list) : document.all[list];
384                   which = which.substring(0,(which.length));
385                                   var el2 = document.ids ? document.ids[which] : document.getElementById ? document.getElementById(which) : document.all[which];
386                   if (el.style.display != "none") {
387                       el2.src = "'.$scormpixdir.'/plus.gif";
388                       el.style.display=\'none\';
389                       new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
390                   } else {
391                       el2.src = "'.$scormpixdir.'/minus.gif";
392                       el.style.display=\'block\';
393                       new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
394                   }
395               }
396           //]]>
397           </script>'."\n";
398     }
399     
400     $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid=';
401     $result->tocmenu = popup_form($url,$tocmenus, "tocmenu", $sco->id, '', '', '', true);
403     return $result;
406 ?>