MDL-26176 - cleanup garbage/pointless code
[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[$setelement]->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,$toclink=TOCJSLINK,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false, $tocheader=false) {
110     global $CFG, $DB, $PAGE, $OUTPUT;
112     $modestr = '';
113     if ($mode == 'browse') {
114         $modestr = '&amp;mode='.$mode;
115     }
117     $result = new stdClass();
118     if ($tocheader) {
119         $result->toc = '<div id="scorm_layout">';
120         $result->toc .= '<div id="scorm_toc">';
121         $result->toc .= '<div id="scorm_tree">';
122     }
123     $result->toc .= '<ul>';
124     $tocmenus = array();
125     $result->prerequisites = true;
126     $incomplete = false;
128     //
129     // Get the current organization infos
130     //
131     if (!empty($currentorg)) {
132         if (($organizationtitle = $DB->get_field('scorm_scoes','title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
133             if ($play) {
134                 $result->toctitle = "$organizationtitle";
135             }
136             else {
137                 $result->toc .= "\t<li>$organizationtitle</li>\n";
138             }
139             $tocmenus[] = $organizationtitle;
140         }
141     }
143     //
144     // If not specified retrieve the last attempt number
145     //
146     if (empty($attempt)) {
147         $attempt = scorm_get_attempt_count($user->id, $scorm);
148     }
149     $result->attemptleft = $scorm->maxattempt == 0 ? 1 : $scorm->maxattempt - $attempt;
150     $conditions['scorm'] = $scorm->id;
151     if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
152         //
153         // Retrieve user tracking data for each learning object
154         //
155         $usertracks = array();
156         foreach ($scoes as $sco) {
157             if (!empty($sco->launch)) {
158                 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
159                     if ($usertrack->status == '') {
160                         $usertrack->status = 'notattempted';
161                     }
162                     $usertracks[$sco->identifier] = $usertrack;
163                 }
164             }
165         }
167         $level=0;
168         $sublist=1;
169         $previd = 0;
170         $nextid = 0;
171         $findnext = false;
172         $parents[$level]='/';
174         foreach ($scoes as $pos => $sco) {
175             $isvisible = false;
176             $sco->title = $sco->title;
177             if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
178                 $isvisible = true;
179             }
180             if ($parents[$level] != $sco->parent) {
181                 if ($newlevel = array_search($sco->parent,$parents)) {
182                     for ($i=0; $i<($level-$newlevel); $i++) {
183                         $result->toc .= "\t\t</li></ul></li>\n";
184                     }
185                     $level = $newlevel;
186                 } else {
187                     $i = $level;
188                     $closelist = '';
189                     while (($i > 0) && ($parents[$level] != $sco->parent)) {
190                         $closelist .= "\t\t</li></ul></li>\n";
191                         $i--;
192                     }
193                     if (($i == 0) && ($sco->parent != $currentorg)) {
194                         $result->toc .= "\t\t<ul>\n";
195                         $level++;
196                     } else {
197                         $result->toc .= $closelist;
198                         $level = $i;
199                     }
200                     $parents[$level] = $sco->parent;
201                 }
202             }
203             if ($isvisible) {
204                 $result->toc .= "<li>";
205             }
206             if (isset($scoes[$pos+1])) {
207                 $nextsco = $scoes[$pos+1];
208             } else {
209                 $nextsco = false;
210             }
211             $nextisvisible = false;
212             if (($nextsco !== false) && (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true')))) {
213                 $nextisvisible = true;
214             }
215             if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
216                 $sublist++;
217             }
218             if (empty($sco->title)) {
219                 $sco->title = $sco->identifier;
220             }
221             if (!empty($sco->launch)) {
222                 if ($isvisible) {
223                     $score = '';
224                     if (empty($scoid) && ($mode != 'normal')) {
225                         $scoid = $sco->id;
226                     }
227                     if (isset($usertracks[$sco->identifier])) {
228                         $usertrack = $usertracks[$sco->identifier];
229                         $strstatus = get_string($usertrack->status,'scorm');
230                         if ($sco->scormtype == 'sco') {
231                             $statusicon = '<img src="'.$OUTPUT->pix_url($usertrack->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" />';
232                         } else {
233                             $statusicon = '<img src="'.$OUTPUT->pix_url('assetc', 'scorm').'" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
234                         }
236                         if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
237                             $incomplete = true;
238                             if ($play && empty($scoid)) {
239                                 $scoid = $sco->id;
240                             }
241                         }
242                         if ($usertrack->score_raw != '') {
243                             $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
244                         }
245                         $strsuspended = get_string('suspended','scorm');
246                         if ($incomplete && isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
247                             $statusicon = '<img src="'.$OUTPUT->pix_url('suspend', 'scorm').'" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
248                         }
249                     } else {
250                         if ($play && empty($scoid)) {
251                             $scoid = $sco->id;
252                         }
253                         $incomplete = true;
254                         if ($sco->scormtype == 'sco') {
255                             $statusicon = '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
256                         } else {
257                             $statusicon = '<img src="'.$OUTPUT->pix_url('asset', 'scorm').'" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
258                         }
259                     }
260                     if ($sco->id == $scoid) {
261                         $findnext = true;
262                     }
264                     if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
265                         if (!empty($sco->launch)) {
266                             $previd = $sco->id;
267                         }
268                     }
269                     if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
270                         if ($sco->id == $scoid) {
271                             $result->prerequisites = true;
272                         }
273                         $thisscoidstr = '&scoid='.$sco->id;
274                         $link = 'a='.$scorm->id.$thisscoidstr.'&currentorg='.$currentorg.$modestr.'&attempt='.$attempt;
276                         if ($toclink == TOCFULLURL) { //display toc with urls for structure page
277                             $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
278                             $result->toc .= $statusicon.'&nbsp;<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score."\n";
279                         } else { //display toc for inside scorm player
280                             if ($sco->launch) {
281                                 $result->toc .= '<a title="'.$link.'">'.$statusicon.'&nbsp;'.format_string($sco->title).'&nbsp;'.$score.'</a>';
282                             } else {
283                                 $result->toc .= '<span>'.$statusicon.'&nbsp;'.format_string($sco->title).'</span>';
284                             }
285                         }
286                         $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
287                     } else {
288                         if ($sco->id == $scoid) {
289                             $result->prerequisites = false;
290                         }
291                         if ($play) {
292                             // should be disabled
293                             $result->toc .= '<span>'.$statusicon.'&nbsp;'.format_string($sco->title).'</span>';
294                         } else {
295                             $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."\n";
296                         }
297                     }
298                     if (($nextsco === false) || $nextsco->parent == $sco->parent) {
299                         $result->toc .= '</li>';
300                     }
301                 }
302             } else {
303                 $result->toc .= '&nbsp;'.format_string($sco->title)."\n";
304             }
305             if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
306                 if (!empty($nextsco->launch)) {
307                     $nextid = $nextsco->id;
308                 }
309             }
310         }
311         for ($i=0;$i<$level;$i++) {
312             $result->toc .= "\t\t</ul></li>\n";
313         }
315         if ($play) {
316             // it is possible that scoid is still not set, in this case we dont want an empty object
317             if ($scoid) {
318                 $sco = scorm_get_sco($scoid);
319             }
320             $sco->previd = $previd;
321             $sco->nextid = $nextid;
322             $result->sco = $sco;
323             $result->incomplete = $incomplete;
324         } else {
325             $result->incomplete = $incomplete;
326         }
327     }
328     $result->toc .= '</ul>';
330     // NEW IMS TOC
331     if ($tocheader) {
332         $result->toc .= '</div></div></div>';
333         $result->toc .= '<div id="scorm_navpanel"></div>';
334     }
337     if ($scorm->hidetoc == 0) {
338         $PAGE->requires->data_for_js('scormdata', array(
339                 'plusicon' => $OUTPUT->pix_url('plus', 'scorm'),
340                 'minusicon' => $OUTPUT->pix_url('minus', 'scorm')));
341         $PAGE->requires->js('/lib/cookies.js');
342         $PAGE->requires->js('/mod/scorm/datamodels/scorm_datamodels.js');
343     }
345     $url = new moodle_url('/mod/scorm/player.php?a='.$scorm->id.'&currentorg='.$currentorg.$modestr);
346     $result->tocmenu = $OUTPUT->single_select($url, 'scoid', $tocmenus, $sco->id, null, "tocmenu");
348     return $result;