c07b547d6902b8fa9f6b5791d8e5862fef70a869
[moodle.git] / mod / scorm / datamodels / scorm_12lib.php
1 <?php // $Id$
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 */
11 function scorm_eval_prerequisites($prerequisites, $usertracks) {
13     // this is really a little language parser - AICC_SCRIPT is the reference
14     // see 2.3.2.5.1. Sequencing/Navigation Today  - from the SCORM 1.2 spec
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                 );
31     $i=0;
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));
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*{}
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]) &&
61                       ($usertracks[$setelement]->status == 'completed' || $usertracks[$element]->status == 'passed')) {
62                       $count++;
63                   }
64                 }
65                 if ($count >= $repeat) {
66                     $element = 'true';
67                 } else {
68                     $element = 'false';
69                 }
71             // ~ Not
72             } else if ($element == '~') {
73                 $element = '!';
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';
91                 }
93             // everything else must be an element defined like S45 ...
94             } else {
95                 if (isset($usertracks[$element]) &&
96                     ($usertracks[$element]->status == 'completed' || $usertracks[$element]->status == 'passed')) {
97                     $element = 'true';
98                 } else {
99                     $element = 'false';
100                 }
101             }
103         }
104         $stack []= ' '.$element.' ';
105     }
106     return eval('return '.implode($stack).';');
109 function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
110     global $CFG, $DB, $PAGE, $OUTPUT;
112     $strexpand = get_string('expcoll','scorm');
113     $modestr = '';
114     if ($mode == 'browse') {
115         $modestr = '&amp;mode='.$mode;
116     }
118     $result = new stdClass();
119     $result->toc = "<ul id='s0' class='$liststyle'>\n";
120     $tocmenus = array();
121     $result->prerequisites = true;
122     $incomplete = false;
124     //
125     // Get the current organization infos
126     //
127     if (!empty($currentorg)) {
128         if (($organizationtitle = $DB->get_field('scorm_scoes','title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
129             $result->toc .= "\t<li>$organizationtitle</li>\n";
130             $tocmenus[] = $organizationtitle;
131         }
132     }
133     //
134     // If not specified retrieve the last attempt number
135     //
136     if (empty($attempt)) {
137         $attempt = scorm_get_last_attempt($scorm->id, $user->id);
138     }
139     $result->attemptleft = $scorm->maxattempt - $attempt;
140     $conditions['scorm'] = $scorm->id;
141     if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
142         //
143         // Retrieve user tracking data for each learning object
144         //
145         $usertracks = array();
146         foreach ($scoes as $sco) {
147             if (!empty($sco->launch)) {
148                 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
149                     if ($usertrack->status == '') {
150                         $usertrack->status = 'notattempted';
151                     }
152                     $usertracks[$sco->identifier] = $usertrack;
153                 }
154             }
155         }
157         $level=0;
158         $sublist=1;
159         $previd = 0;
160         $nextid = 0;
161         $findnext = false;
162         $parents[$level]='/';
164         foreach ($scoes as $pos => $sco) {
165             $isvisible = false;
166             $sco->title = $sco->title;
167             if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
168                 $isvisible = true;
169             }
170             if ($parents[$level] != $sco->parent) {
171                 if ($newlevel = array_search($sco->parent,$parents)) {
172                     for ($i=0; $i<($level-$newlevel); $i++) {
173                         $result->toc .= "\t\t</ul></li>\n";
174                     }
175                     $level = $newlevel;
176                 } else {
177                     $i = $level;
178                     $closelist = '';
179                     while (($i > 0) && ($parents[$level] != $sco->parent)) {
180                         $closelist .= "\t\t</ul></li>\n";
181                         $i--;
182                     }
183                     if (($i == 0) && ($sco->parent != $currentorg)) {
184                         $style = '';
185                         if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
186                             $style = ' style="display: none;"';
187                         }
188                         $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
189                         $level++;
190                     } else {
191                         $result->toc .= $closelist;
192                         $level = $i;
193                     }
194                     $parents[$level] = $sco->parent;
195                 }
196             }
197             if ($isvisible) {
198                 $result->toc .= "\t\t<li>";
199             }
200             if (isset($scoes[$pos+1])) {
201                 $nextsco = $scoes[$pos+1];
202             } else {
203                 $nextsco = false;
204             }
205             $nextisvisible = false;
206             if (($nextsco !== false) && (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true')))) {
207                 $nextisvisible = true;
208             }
209             if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
210                 $sublist++;
211                 $icon = 'minus';
212                 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
213                     $icon = 'plus';
214                 }
215                 $result->toc .= '<a href="javascript:expandCollide(\'img'.$sublist.'\',\'s'.$sublist.'\','.$nextsco->id.');"><img id="img'.$sublist.'" src="'.$OUTPUT->mod_icon_url('pix/' . $icon, 'scorm').'" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
216             } else if ($isvisible) {
217                 $result->toc .= '<img src="'.$OUTPUT->mod_icon_url('pix/spacer', 'scorm').'" alt="" />';
218             }
219             if (empty($sco->title)) {
220                 $sco->title = $sco->identifier;
221             }
222             if (!empty($sco->launch)) {
223                 if ($isvisible) {
224                     $startbold = '';
225                     $endbold = '';
226                     $score = '';
227                     if (empty($scoid) && ($mode != 'normal')) {
228                         $scoid = $sco->id;
229                     }
230                     if (isset($usertracks[$sco->identifier])) {
231                         $usertrack = $usertracks[$sco->identifier];
232                         $strstatus = get_string($usertrack->status,'scorm');
233                         if ($sco->scormtype == 'sco') {
234                             $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/'.$usertrack->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" />';
235                         } else {
236                             $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/assetc/', 'scorm').'" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
237                         }
239                         if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
240                             $incomplete = true;
241                             if ($play && empty($scoid)) {
242                                 $scoid = $sco->id;
243                             }
244                         }
245                         if ($usertrack->score_raw != '') {
246                             $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
247                         }
248                         $strsuspended = get_string('suspended','scorm');
249                         if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
250                             if($usertrack->status !='completed') {
251                                 $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/suspend', 'scorm').'" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
252                             }
253                         }
254                     } else {
255                         if ($play && empty($scoid)) {
256                             $scoid = $sco->id;
257                         }
258                         $incomplete = true;
259                         if ($sco->scormtype == 'sco') {
260                             $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/notattempted', 'scorm').'" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
261                         } else {
262                             $statusicon = '<img src="'.$OUTPUT->mod_icon_url('pix/asset', 'scorm').'" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
263                         }
264                     }
265                     if ($sco->id == $scoid) {
266                         $startbold = '<b>';
267                         $endbold = '</b>';
268                         $findnext = true;
269                         $shownext = isset($sco->next) ? $sco->next : 0;
270                         $showprev = isset($sco->previous) ? $sco->previous : 0;
271                     }
273                     if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
274                         if (!empty($sco->launch)) {
275                             $previd = $sco->id;
276                         }
277                     }
278                     if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
279                         if ($sco->id == $scoid) {
280                             $result->prerequisites = true;
281                         }
282                         $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
283                         $result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
284                         $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
285                     } else {
286                         if ($sco->id == $scoid) {
287                             $result->prerequisites = false;
288                         }
289                         $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."</li>\n";
290                     }
291                 }
292             } else {
293                 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
294             }
295             if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
296                 if (!empty($nextsco->launch)) {
297                     $nextid = $nextsco->id;
298                 }
299             }
300         }
301         for ($i=0;$i<$level;$i++) {
302             $result->toc .= "\t\t</ul></li>\n";
303         }
305         if ($play) {
306             // it is possible that scoid is still not set, in this case we dont want an empty object
307             if ($scoid) {
308                 $sco = scorm_get_sco($scoid);
309             }
310             $sco->previd = $previd;
311             $sco->nextid = $nextid;
312             $result->sco = $sco;
313             $result->incomplete = $incomplete;
314         } else {
315             $result->incomplete = $incomplete;
316         }
317     }
318     $result->toc .= "\t</ul>\n";
319     if ($scorm->hidetoc == 0) {
320         $PAGE->requires->data_for_js('scormdata', array(
321                 'plusicon' => $OUTPUT->mod_icon_url('pix/plus', 'scorm'),
322                 'minusicon' => $OUTPUT->mod_icon_url('pix/minus', 'scorm')));
323         $PAGE->requires->js('mod/scorm/datamodels/scorm_datamodels.js');
324     }
326     $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&currentorg='.$currentorg.$modestr;
327     $select = html_select::make_popup_form($url, 'scoid', $tocmenus, "tocmenu", $sco->id);
328     $select->nothinglabel = false;
329     $result->tocmenu = $OUTPUT->select($select);
331     return $result;
334 ?>