SCORM MDL-22951 New player for SCORM fixes major regression that prevents some SCORM...
[moodle.git] / mod / scorm / datamodels / scorm_12lib.php
1 <?php
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, $tocheader=true) {
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     if ($tocheader) {
121         $result->toc = '<div id="scorm_layout">';
122         $result->toc .= '<div id="scorm_toc">';
123         $result->toc .= '<div id="scorm_tree">';
124     }
125     $result->toc .= '<ul>';
126     $tocmenus = array();
127     $result->prerequisites = true;
128     $incomplete = false;
130     //
131     // Get the current organization infos
132     //
133     if (!empty($currentorg)) {
134         if (($organizationtitle = $DB->get_field('scorm_scoes','title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
135             if ($play) {
136                 $result->toctitle = "$organizationtitle";
137             }
138             else {
139                 $result->toc .= "\t<li>$organizationtitle</li>\n";
140             }
141             $tocmenus[] = $organizationtitle;
142         }
143     }
145     //
146     // If not specified retrieve the last attempt number
147     //
148     if (empty($attempt)) {
149         $attempt = scorm_get_attempt_count($user, $scorm);
150     }
151     $result->attemptleft = $scorm->maxattempt == 0 ? 1 : $scorm->maxattempt - $attempt;
152     $conditions['scorm'] = $scorm->id;
153     if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
154         //
155         // Retrieve user tracking data for each learning object
156         //
157         $usertracks = array();
158         foreach ($scoes as $sco) {
159             if (!empty($sco->launch)) {
160                 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
161                     if ($usertrack->status == '') {
162                         $usertrack->status = 'notattempted';
163                     }
164                     $usertracks[$sco->identifier] = $usertrack;
165                 }
166             }
167         }
169         $level=0;
170         $sublist=1;
171         $previd = 0;
172         $nextid = 0;
173         $findnext = false;
174         $parents[$level]='/';
176         foreach ($scoes as $pos => $sco) {
177             $isvisible = false;
178             $sco->title = $sco->title;
179             if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
180                 $isvisible = true;
181             }
182             if ($parents[$level] != $sco->parent) {
183                 if ($newlevel = array_search($sco->parent,$parents)) {
184                     for ($i=0; $i<($level-$newlevel); $i++) {
185                         $result->toc .= "\t\t</li></ul></li>\n";
186                     }
187                     $level = $newlevel;
188                 } else {
189                     $i = $level;
190                     $closelist = '';
191                     while (($i > 0) && ($parents[$level] != $sco->parent)) {
192                         $closelist .= "\t\t</li></ul></li>\n";
193                         $i--;
194                     }
195                     if (($i == 0) && ($sco->parent != $currentorg)) {
196                         $style = '';
197                         if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
198                             $style = ' style="display: none;"';
199                         }
200                         //$result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
201                         $result->toc .= "\t\t<ul>\n";
205                         $level++;
206                     } else {
207                         $result->toc .= $closelist;
208                         $level = $i;
209                     }
210                     $parents[$level] = $sco->parent;
211                 }
212             }
213             if ($isvisible) {
214                 $result->toc .= "<li>";
215             }
216             if (isset($scoes[$pos+1])) {
217                 $nextsco = $scoes[$pos+1];
218             } else {
219                 $nextsco = false;
220             }
221             $nextisvisible = false;
222             if (($nextsco !== false) && (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true')))) {
223                 $nextisvisible = true;
224             }
225             if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
226                 $sublist++;
227                 $icon = 'minus';
228                 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
229                     $icon = 'plus';
230                 }
231                 //$result->toc .= '<a href="javascript:expandCollide(\'img'.$sublist.'\',\'s'.$sublist.'\','.$nextsco->id.');"><img id="img'.$sublist.'" src="'.$OUTPUT->pix_url($icon, 'scorm').'" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
232             } else if ($isvisible) {
233                 //$result->toc .= '<img src="'.$OUTPUT->pix_url('spacer', 'scorm').'" alt="" />';
234             }
235             if (empty($sco->title)) {
236                 $sco->title = $sco->identifier;
237             }
238             if (!empty($sco->launch)) {
239                 if ($isvisible) {
240                     $startbold = '';
241                     $endbold = '';
242                     $score = '';
243                     if (empty($scoid) && ($mode != 'normal')) {
244                         $scoid = $sco->id;
245                     }
246                     if (isset($usertracks[$sco->identifier])) {
247                         $usertrack = $usertracks[$sco->identifier];
248                         $strstatus = get_string($usertrack->status,'scorm');
249                         if ($sco->scormtype == 'sco') {
250                             $statusicon = '<img src="'.$OUTPUT->pix_url($usertrack->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" />';
251                         } else {
252                             $statusicon = '<img src="'.$OUTPUT->pix_url('assetc', 'scorm').'" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
253                         }
255                         if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
256                             $incomplete = true;
257                             if ($play && empty($scoid)) {
258                                 $scoid = $sco->id;
259                             }
260                         }
261                         if ($usertrack->score_raw != '') {
262                             $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
263                         }
264                         $strsuspended = get_string('suspended','scorm');
265                         if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
266                             if($usertrack->status !='completed') {
267                                 $statusicon = '<img src="'.$OUTPUT->pix_url('suspend', 'scorm').'" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
268                             }
269                         }
270                     } else {
271                         if ($play && empty($scoid)) {
272                             $scoid = $sco->id;
273                         }
274                         $incomplete = true;
275                         if ($sco->scormtype == 'sco') {
276                             $statusicon = '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
277                         } else {
278                             $statusicon = '<img src="'.$OUTPUT->pix_url('asset', 'scorm').'" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
279                         }
280                     }
281                     if ($sco->id == $scoid) {
282                         $startbold = '<b>';
283                         $endbold = '</b>';
284                         $findnext = true;
285                         $shownext = isset($sco->next) ? $sco->next : 0;
286                         $showprev = isset($sco->previous) ? $sco->previous : 0;
287                     }
289                     if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
290                         if (!empty($sco->launch)) {
291                             $previd = $sco->id;
292                         }
293                     }
294                     if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
295                         if ($sco->id == $scoid) {
296                             $result->prerequisites = true;
297                         }
298                        // $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
299                         $thisscoidstr = '&scoid='.$sco->id;
300                         //$link = $CFG->wwwroot.'/mod/scorm/loadSCO.php?a='.$scorm->id.$thisscoidstr.$modestr;
301                         $link = 'a='.$scorm->id.$thisscoidstr.'&currentorg='.$currentorg.$modestr.'&attempt='.$attempt;
303                         //$result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."\n";
304                         //$result->toc .= $startbold.'<a title="'.$link.'">'.$statusicon.'&nbsp;'.format_string($sco->title).'&nbsp;'.$score.'</a>'.$endbold."\n";
305                         if ($sco->launch) {
306                             $result->toc .= '<a title="'.$link.'">'.$statusicon.'&nbsp;'.format_string($sco->title).'&nbsp;'.$score.'</a>';
307                         }
308                         else {
309                             $result->toc .= '<span>'.$statusicon.'&nbsp;'.format_string($sco->title).'</span>';
310                         }
311                         $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
312                     } else {
313                         if ($sco->id == $scoid) {
314                             $result->prerequisites = false;
315                         }
316                         if ($play) {
317                             // should be disabled
318                             $result->toc .= '<span>'.$statusicon.'&nbsp;'.format_string($sco->title).'</span>';
319                         }
320                         else {
321                             $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."\n";
322                         }
323                     }
324                     if (($nextsco === false) || $nextsco->parent == $sco->parent) {
325                     $result->toc .= '</li>';
326                     }
327                 }
328             } else {
329                 $result->toc .= '&nbsp;'.format_string($sco->title)."\n";
330             }
331             if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
332                 if (!empty($nextsco->launch)) {
333                     $nextid = $nextsco->id;
334                 }
335             }
336         }
337         for ($i=0;$i<$level;$i++) {
338             $result->toc .= "\t\t</ul></li>\n";
339         }
341         if ($play) {
342             // it is possible that scoid is still not set, in this case we dont want an empty object
343             if ($scoid) {
344                 $sco = scorm_get_sco($scoid);
345             }
346             $sco->previd = $previd;
347             $sco->nextid = $nextid;
348             $result->sco = $sco;
349             $result->incomplete = $incomplete;
350         } else {
351             $result->incomplete = $incomplete;
352         }
353     }
354     $result->toc .= '</ul>';
356     // NEW IMS TOC
357     if ($tocheader) {
358         $result->toc .= '</div></div></div>';
359         $result->toc .= '<div id="scorm_navpanel"></div>';
360     }
364     if ($scorm->hidetoc == 0) {
365         $PAGE->requires->data_for_js('scormdata', array(
366                 'plusicon' => $OUTPUT->pix_url('plus', 'scorm'),
367                 'minusicon' => $OUTPUT->pix_url('minus', 'scorm')));
368         $PAGE->requires->js('/lib/cookies.js');
369         $PAGE->requires->js('/mod/scorm/datamodels/scorm_datamodels.js');
370     }
372     $url = new moodle_url('/mod/scorm/player.php?a='.$scorm->id.'&currentorg='.$currentorg.$modestr);
373     $result->tocmenu = $OUTPUT->single_select($url, 'scoid', $tocmenus, $sco->id, null, "tocmenu");
375     return $result;