1adde9c0752e974a4d2236bd64dbd932fe6794df
[moodle.git] / mod / scorm / datamodels / scorm_12lib.php
1 <?php
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 (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;
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='0' class='$liststyle'>\n";
169     $tocmenus = array();
170     $result->prerequisites = true;
171     $incomplete = false;
172     
173     //
174     // Get the current organization infos
175     //
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;
181         }
182         $organizationsql = "AND 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     if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
192         //
193         // Retrieve user tracking data for each learning object
194         // 
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';
201                     }
202                     $usertracks[$sco->identifier] = $usertrack;
203                 }
204             }
205         }
207         $level=0;
208         $sublist=1;
209         $previd = 0;
210         $nextid = 0;
211         $findnext = false;
212         $parents[$level]='/';
213         
214         foreach ($scoes as $sco) {
215             if ($parents[$level]!=$sco->parent) {
216                 if ($newlevel = array_search($sco->parent,$parents)) {
217                     for ($i=0; $i<($level-$newlevel); $i++) {
218                         $result->toc .= "\t\t</ul></li>\n";
219                     }
220                     $level = $newlevel;
221                 } else {
222                     $i = $level;
223                     $closelist = '';
224                     while (($i > 0) && ($parents[$level] != $sco->parent)) {
225                         $closelist .= "\t\t</ul></li>\n";
226                         $i--;
227                     }
228                     if (($i == 0) && ($sco->parent != $currentorg)) {
229                         $style = '';
230                         if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
231                             $style = ' style="display: none;"';
232                         }
233                         $result->toc .= "\t\t<li><ul id='$sublist' class='$liststyle'$style>\n";
234                         $level++;
235                     } else {
236                         $result->toc .= $closelist;
237                         $level = $i;
238                     }
239                     $parents[$level]=$sco->parent;
240                 }
241             }
242             $result->toc .= "\t\t<li>";
243             $nextsco = next($scoes);
244             if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
245                 $sublist++;
246                 $icon = 'minus';
247                 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
248                     $icon = 'plus';
249                 }
250                 $result->toc .= '<a href="javascript:expandCollide(img'.$sublist.','.$sublist.','.$nextsco->id.');"><img id="img'.$sublist.'" src="'.$scormpixdir.'/'.$icon.'.gif" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
251             } else {
252                 $result->toc .= '<img src="'.$scormpixdir.'/spacer.gif" />';
253             }
254             if (empty($sco->title)) {
255                 $sco->title = $sco->identifier;
256             }
257             if (!empty($sco->launch)) {
258                 $startbold = '';
259                 $endbold = '';
260                 $score = '';
261                 if (empty($scoid) && ($mode != 'normal')) {
262                     $scoid = $sco->id;
263                 }
264                 if (isset($usertracks[$sco->identifier])) {
265                     $usertrack = $usertracks[$sco->identifier];
266                     $strstatus = get_string($usertrack->status,'scorm');
267                     $result->toc .= '<img src="'.$scormpixdir.'/'.$usertrack->status.'.gif" alt="'.$strstatus.'" title="'.$strstatus.'" />';
268                     
269                     if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
270                         $incomplete = true;
271                         if ($play && empty($scoid)) {
272                             $scoid = $sco->id;
273                         }
274                     }
275                     if ($usertrack->score_raw != '') {
276                         $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
277                     }
278                 } else {
279                     if ($play && empty($scoid)) {
280                         $scoid = $sco->id;
281                     }
282                     if ($sco->scormtype == 'sco') {
283                         $result->toc .= '<img src="'.$scormpixdir.'/notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
284                         $incomplete = true;
285                     } else {
286                         $result->toc .= '<img src="'.$scormpixdir.'/asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
287                     }
288                 }
289                 if ($sco->id == $scoid) {
290                     $startbold = '<b>';
291                     $endbold = '</b>';
292                     $findnext = true;
293                     $shownext = $sco->next;
294                     $showprev = $sco->previous;
295                 }
296                 
297                 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
298                     if (!empty($sco->launch)) {
299                         $previd = $sco->id;
300                     }
301                 }
302                 if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
303                     if ($sco->id == $scoid) {
304                         $result->prerequisites = true;
305                     }
306                     $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
307                     $result->toc .= '&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
308                     $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
309                 } else {
310                     if ($sco->id == $scoid) {
311                         $result->prerequisites = false;
312                     }
313                     $result->toc .= '&nbsp;'.$sco->title."</li>\n";
314                 }
315             } else {
316                 $result->toc .= '&nbsp;'.$sco->title."</li>\n";
317             }
318             if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
319                 if (!empty($nextsco->launch)) {
320                     $nextid = $nextsco->id;
321                 }
322             }
323         }
324         for ($i=0;$i<$level;$i++) {
325             $result->toc .= "\t\t</ul></li>\n";
326         }
327         
328         if ($play) {
329             $sco = get_record('scorm_scoes','id',$scoid);
330             $sco->previd = $previd;
331             $sco->nextid = $nextid;
332             $result->sco = $sco;
333             $result->incomplete = $incomplete;
334         } else {
335             $result->incomplete = $incomplete;
336         }
337     }
338     $result->toc .= "\t</ul>\n";
339     if ($scorm->hidetoc == 0) {
340         $result->toc .= '
341           <script language="javascript" type="text/javascript">
342           <!--
343               function expandCollide(which,list,item) {
344                   var nn=document.ids?true:false
345                   var w3c=document.getElementById?true:false
346                   var beg=nn?"document.ids.":w3c?"document.getElementById(":"document.all.";
347                   var mid=w3c?").style":".style";
349                   if (eval(beg+list+mid+".display") != "none") {
350                       which.src = "'.$scormpixdir.'/plus.gif";
351                       eval(beg+list+mid+".display=\'none\';");
352                       new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
353                   } else {
354                       which.src = "'.$scormpixdir.'/minus.gif";
355                       eval(beg+list+mid+".display=\'block\';");
356                       new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
357                   }
358               }
359           -->
360           </script>'."\n";
361     }
362     
363     $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid=';
364     $result->tocmenu = popup_form($url,$tocmenus, "tocmenu", $sco->id, '', '', '', true);
366     return $result;
369 ?>