MDL-14741: use the default html editor, as chosen in the admin settings
[moodle.git] / mod / scorm / datamodels / aicclib.php
CommitLineData
f4ba7e1a 1<?php // $Id$
dc383b6f 2function scorm_add_time($a, $b) {
3 $aes = explode(':',$a);
4 $bes = explode(':',$b);
5 $aseconds = explode('.',$aes[2]);
6 $bseconds = explode('.',$bes[2]);
7 $change = 0;
8
9 $acents = 0; //Cents
10 if (count($aseconds) > 1) {
11 $acents = $aseconds[1];
12 }
13 $bcents = 0;
14 if (count($bseconds) > 1) {
15 $bcents = $bseconds[1];
16 }
17 $cents = $acents + $bcents;
18 $change = floor($cents / 100);
19 $cents = $cents - ($change * 100);
20 if (floor($cents) < 10) {
21 $cents = '0'. $cents;
22 }
23
24 $secs = $aseconds[0] + $bseconds[0] + $change; //Seconds
25 $change = floor($secs / 60);
26 $secs = $secs - ($change * 60);
27 if (floor($secs) < 10) {
28 $secs = '0'. $secs;
29 }
30
31 $mins = $aes[1] + $bes[1] + $change; //Minutes
32 $change = floor($mins / 60);
33 $mins = $mins - ($change * 60);
34 if ($mins < 10) {
35 $mins = '0' . $mins;
36 }
37
38 $hours = $aes[0] + $bes[0] + $change; //Hours
39 if ($hours < 10) {
40 $hours = '0' . $hours;
41 }
42
43 if ($cents != '0') {
44 return $hours . ":" . $mins . ":" . $secs . '.' . $cents;
45 } else {
46 return $hours . ":" . $mins . ":" . $secs;
47 }
48}
49
50/**
51* Take the header row of an AICC definition file
52* and returns sequence of columns and a pointer to
53* the sco identifier column.
54*
55* @param string $row AICC header row
56* @param string $mastername AICC sco identifier column
57* @return mixed
58*/
59function scorm_get_aicc_columns($row,$mastername='system_id') {
60 $tok = strtok(strtolower($row),"\",\n\r");
61 $result->columns = array();
62 $i=0;
63 while ($tok) {
64 if ($tok !='') {
65 $result->columns[] = $tok;
66 if ($tok == $mastername) {
67 $result->mastercol = $i;
68 }
69 $i++;
70 }
71 $tok = strtok("\",\n\r");
72 }
73 return $result;
74}
75
76/**
77* Given a colums array return a string containing the regular
78* expression to match the columns in a text row.
79*
80* @param array $column The header columns
81* @param string $remodule The regular expression module for a single column
82* @return string
83*/
84function scorm_forge_cols_regexp($columns,$remodule='(".*")?,') {
85 $regexp = '/^';
86 foreach ($columns as $column) {
87 $regexp .= $remodule;
88 }
89 $regexp = substr($regexp,0,-1) . '/';
90 return $regexp;
91}
92
93function scorm_parse_aicc($pkgdir,$scormid) {
94 $version = 'AICC';
95 $ids = array();
96 $courses = array();
97 $extaiccfiles = array('crs','des','au','cst','ort','pre','cmp');
98 if ($handle = opendir($pkgdir)) {
99 while (($file = readdir($handle)) !== false) {
100 if ($file[0] != '.') {
101 $ext = substr($file,strrpos($file,'.'));
102 $extension = strtolower(substr($ext,1));
103 if (in_array($extension,$extaiccfiles)) {
104 $id = strtolower(basename($file,$ext));
105 $ids[$id]->$extension = $file;
106 }
107 }
108 }
109 closedir($handle);
110 }
111 foreach ($ids as $courseid => $id) {
112 if (isset($id->crs)) {
113 if (is_file($pkgdir.'/'.$id->crs)) {
114 $rows = file($pkgdir.'/'.$id->crs);
115 foreach ($rows as $row) {
116 if (preg_match("/^(.+)=(.+)$/",$row,$matches)) {
117 switch (strtolower(trim($matches[1]))) {
118 case 'course_id':
119 $courses[$courseid]->id = trim($matches[2]);
120 break;
121 case 'course_title':
122 $courses[$courseid]->title = trim($matches[2]);
123 break;
124 case 'version':
125 $courses[$courseid]->version = 'AICC_'.trim($matches[2]);
126 break;
127 }
128 }
129 }
130 }
131 }
132 if (isset($id->des)) {
133 $rows = file($pkgdir.'/'.$id->des);
134 $columns = scorm_get_aicc_columns($rows[0]);
135 $regexp = scorm_forge_cols_regexp($columns->columns);
136 for ($i=1;$i<count($rows);$i++) {
137 if (preg_match($regexp,$rows[$i],$matches)) {
138 for ($j=0;$j<count($columns->columns);$j++) {
139 $column = $columns->columns[$j];
140 $courses[$courseid]->elements[substr(trim($matches[$columns->mastercol+1]),1,-1)]->$column = substr(trim($matches[$j+1]),1,-1);
141 }
142 }
143 }
144 }
145 if (isset($id->au)) {
146 $rows = file($pkgdir.'/'.$id->au);
147 $columns = scorm_get_aicc_columns($rows[0]);
148 $regexp = scorm_forge_cols_regexp($columns->columns);
149 for ($i=1;$i<count($rows);$i++) {
150 if (preg_match($regexp,$rows[$i],$matches)) {
151 for ($j=0;$j<count($columns->columns);$j++) {
152 $column = $columns->columns[$j];
153 $courses[$courseid]->elements[substr(trim($matches[$columns->mastercol+1]),1,-1)]->$column = substr(trim($matches[$j+1]),1,-1);
154 }
155 }
156 }
157 }
158 if (isset($id->cst)) {
159 $rows = file($pkgdir.'/'.$id->cst);
160 $columns = scorm_get_aicc_columns($rows[0],'block');
161 $regexp = scorm_forge_cols_regexp($columns->columns,'(.+)?,');
162 for ($i=1;$i<count($rows);$i++) {
163 if (preg_match($regexp,$rows[$i],$matches)) {
164 for ($j=0;$j<count($columns->columns);$j++) {
165 if ($j != $columns->mastercol) {
166 $courses[$courseid]->elements[substr(trim($matches[$j+1]),1,-1)]->parent = substr(trim($matches[$columns->mastercol+1]),1,-1);
167 }
168 }
169 }
170 }
171 }
172 if (isset($id->ort)) {
173 $rows = file($pkgdir.'/'.$id->ort);
f6520280 174 $columns = scorm_get_aicc_columns($rows[0],'course_element');
175 $regexp = scorm_forge_cols_regexp($columns->columns,'(.+)?,');
176 for ($i=1;$i<count($rows);$i++) {
177 if (preg_match($regexp,$rows[$i],$matches)) {
178 for ($j=0;$j<count($matches)-1;$j++) {
179 if ($j != $columns->mastercol) {
180 $courses[$courseid]->elements[substr(trim($matches[$j+1]),1,-1)]->parent = substr(trim($matches[$columns->mastercol+1]),1,-1);
181 }
182 }
183 }
184 }
dc383b6f 185 }
186 if (isset($id->pre)) {
187 $rows = file($pkgdir.'/'.$id->pre);
188 $columns = scorm_get_aicc_columns($rows[0],'structure_element');
189 $regexp = scorm_forge_cols_regexp($columns->columns,'(.+),');
190 for ($i=1;$i<count($rows);$i++) {
191 if (preg_match($regexp,$rows[$i],$matches)) {
192 $courses[$courseid]->elements[$columns->mastercol+1]->prerequisites = substr(trim($matches[1-$columns->mastercol+1]),1,-1);
193 }
194 }
195 }
196 if (isset($id->cmp)) {
197 $rows = file($pkgdir.'/'.$id->cmp);
198 }
199 }
200 //print_r($courses);
201
202 $oldscoes = get_records('scorm_scoes','scorm',$scormid);
203
204 $launch = 0;
205 if (isset($courses)) {
206 foreach ($courses as $course) {
531fa830 207 $sco = new object();
dc383b6f 208 $sco->identifier = $course->id;
209 $sco->scorm = $scormid;
210 $sco->organization = '';
211 $sco->title = $course->title;
212 $sco->parent = '/';
213 $sco->launch = '';
214 $sco->scormtype = '';
215
216 //print_r($sco);
217 if (get_record('scorm_scoes','scorm',$scormid,'identifier',$sco->identifier)) {
218 $id = update_record('scorm_scoes',$sco);
219 unset($oldscoes[$id]);
220 } else {
221 $id = insert_record('scorm_scoes',$sco);
222 }
223
224 if ($launch == 0) {
225 $launch = $id;
226 }
227 if (isset($course->elements)) {
228 foreach($course->elements as $element) {
229 unset($sco);
230 $sco->identifier = $element->system_id;
231 $sco->scorm = $scormid;
232 $sco->organization = $course->id;
233 $sco->title = $element->title;
f6520280 234
235 if (!isset($element->parent) || strtolower($element->parent) == 'root') {
dc383b6f 236 $sco->parent = '/';
237 } else {
238 $sco->parent = $element->parent;
239 }
240 if (isset($element->file_name)) {
241 $sco->launch = $element->file_name;
242 $sco->scormtype = 'sco';
f6520280 243 $sco->previous = 0;
244 $sco->next = 0;
245 $id = null;
246 if ($oldscoid = scorm_array_search('identifier',$sco->identifier,$oldscoes)) {
247 $sco->id = $oldscoid;
248 $id = update_record('scorm_scoes',$sco);
249 delete_records('scorm_scoes_data','scoid',$oldscoid);
250 unset($oldscoes[$oldscoid]);
251 } else {
252 $id = insert_record('scorm_scoes',$sco);
253 }
254 if (!empty($id)) {
255 unset($scodata);
256 $scodata->scoid = $id;
257 if (isset($element->web_launch)) {
258 $scodata->name = 'parameters';
259 $scodata->value = $element->web_launch;
260 $dataid = insert_record('scorm_scoes_data',$scodata);
261 }
262 if (isset($element->prerequisites)) {
263 $scodata->name = 'prerequisites';
264 $scodata->value = $element->prerequisites;
265 $dataid = insert_record('scorm_scoes_data',$scodata);
266 }
267 if (isset($element->max_time_allowed)) {
268 $scodata->name = 'max_time_allowed';
269 $scodata->value = $element->max_time_allowed;
270 $dataid = insert_record('scorm_scoes_data',$scodata);
271 }
272 if (isset($element->time_limit_action)) {
273 $scodata->name = 'time_limit_action';
274 $scodata->value = $element->time_limit_action;
275 $dataid = insert_record('scorm_scoes_data',$scodata);
276 }
277 if (isset($element->mastery_score)) {
278 $scodata->name = 'mastery_score';
279 $scodata->value = $element->mastery_score;
280 $dataid = insert_record('scorm_scoes_data',$scodata);
281 }
282 }
283 if ($launch==0) {
284 $launch = $id;
285 }
dc383b6f 286 }
287 }
288 }
289 }
290 }
291 if (!empty($oldscoes)) {
292 foreach($oldscoes as $oldsco) {
293 delete_records('scorm_scoes','id',$oldsco->id);
294 delete_records('scorm_scoes_track','scoid',$oldsco->id);
295 }
296 }
297 set_field('scorm','version','AICC','id',$scormid);
298 return $launch;
299}
300
301function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
302 global $CFG;
303
dc383b6f 304 $strexpand = get_string('expcoll','scorm');
305 $modestr = '';
306 if ($mode == 'browse') {
307 $modestr = '&amp;mode='.$mode;
308 }
309 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
310
311 $result = new stdClass();
f6520280 312 $result->toc = "<ul id='s0' class='$liststyle'>\n";
dc383b6f 313 $tocmenus = array();
314 $result->prerequisites = true;
315 $incomplete = false;
316
317 //
318 // Get the current organization infos
319 //
320 $organizationsql = '';
321 if (!empty($currentorg)) {
322 if (($organizationtitle = get_field('scorm_scoes','title','scorm',$scorm->id,'identifier',$currentorg)) != '') {
323 $result->toc .= "\t<li>$organizationtitle</li>\n";
324 $tocmenus[] = $organizationtitle;
325 }
326 $organizationsql = "AND organization='$currentorg'";
327 }
328 //
329 // If not specified retrieve the last attempt number
330 //
331 if (empty($attempt)) {
332 $attempt = scorm_get_last_attempt($scorm->id, $user->id);
333 }
334 $result->attemptleft = $scorm->maxattempt - $attempt;
335 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
9fb2de4e 336 // drop keys so that we can access array sequentially
337 $scoes = array_values($scoes);
dc383b6f 338 //
339 // Retrieve user tracking data for each learning object
340 //
341 $usertracks = array();
342 foreach ($scoes as $sco) {
343 if (!empty($sco->launch)) {
344 if ($usertrack=scorm_get_tracks($sco->id,$user->id,$attempt)) {
345 if ($usertrack->status == '') {
346 $usertrack->status = 'notattempted';
347 }
348 $usertracks[$sco->identifier] = $usertrack;
349 }
350 }
351 }
352
353 $level=0;
354 $sublist=1;
355 $previd = 0;
356 $nextid = 0;
357 $findnext = false;
358 $parents[$level]='/';
359
9fb2de4e 360 foreach ($scoes as $pos=>$sco) {
f6520280 361 $isvisible = false;
c852a628 362 $sco->title = stripslashes($sco->title);
f6520280 363 if ($optionaldatas = scorm_get_sco($sco->id, SCO_DATA)) {
364 if (!isset($optionaldatas->isvisible) || (isset($optionaldatas->isvisible) && ($optionaldatas->isvisible == 'true'))) {
365 $isvisible = true;
366 }
367 }
368 else
369 $isvisible = true;
dc383b6f 370 if ($parents[$level]!=$sco->parent) {
371 if ($newlevel = array_search($sco->parent,$parents)) {
372 for ($i=0; $i<($level-$newlevel); $i++) {
373 $result->toc .= "\t\t</ul></li>\n";
374 }
375 $level = $newlevel;
376 } else {
377 $i = $level;
378 $closelist = '';
379 while (($i > 0) && ($parents[$level] != $sco->parent)) {
380 $closelist .= "\t\t</ul></li>\n";
381 $i--;
382 }
383 if (($i == 0) && ($sco->parent != $currentorg)) {
384 $style = '';
385 if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
386 $style = ' style="display: none;"';
387 }
f6520280 388 $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
dc383b6f 389 $level++;
390 } else {
391 $result->toc .= $closelist;
392 $level = $i;
393 }
394 $parents[$level]=$sco->parent;
395 }
396 }
f6520280 397 if ($isvisible) {
398 $result->toc .= "\t\t<li>";
399 }
9fb2de4e 400 if (isset($scoes[$pos+1])) {
401 $nextsco = $scoes[$pos+1];
402 } else {
403 $nextsco = false;
404 }
f6520280 405 $nextisvisible = false;
406 if (($nextsco !== false) && ($optionaldatas = scorm_get_sco($nextsco->id, SCO_DATA))) {
407 if (!isset($optionaldatas->isvisible) || (isset($optionaldatas->isvisible) && ($optionaldatas->isvisible == 'true'))) {
408 $nextisvisible = true;
409 }
410 }
411 if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
dc383b6f 412 $sublist++;
413 $icon = 'minus';
414 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
415 $icon = 'plus';
416 }
f6520280 417 $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>';
418 } else if ($isvisible) {
dc383b6f 419 $result->toc .= '<img src="'.$scormpixdir.'/spacer.gif" />';
420 }
421 if (empty($sco->title)) {
422 $sco->title = $sco->identifier;
423 }
424 if (!empty($sco->launch)) {
f6520280 425 if ($isvisible) {
426 $startbold = '';
427 $endbold = '';
428 $score = '';
429 if (empty($scoid) && ($mode != 'normal')) {
430 $scoid = $sco->id;
530a61d5 431 }
f6520280 432 if (isset($usertracks[$sco->identifier])) {
433 $usertrack = $usertracks[$sco->identifier];
434 $strstatus = get_string($usertrack->status,'scorm');
435 if ($sco->scormtype == 'sco') {
436 $statusicon = '<img src="'.$scormpixdir.'/'.$usertrack->status.'.gif" alt="'.$strstatus.'" title="'.$strstatus.'" />';
437 } else {
438 $statusicon = '<img src="'.$scormpixdir.'/assetc.gif" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
439 }
530a61d5 440
f6520280 441 if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
442 $incomplete = true;
443 if ($play && empty($scoid)) {
444 $scoid = $sco->id;
445 }
446 }
447 if ($usertrack->score_raw != '') {
448 $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
449 }
450 $strsuspended = get_string('suspended','scorm');
451 if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
452 $statusicon = '<img src="'.$scormpixdir.'/suspend.gif" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
453 }
454 } else {
dc383b6f 455 if ($play && empty($scoid)) {
456 $scoid = $sco->id;
457 }
f6520280 458 $incomplete = true;
459 if ($sco->scormtype == 'sco') {
460 $statusicon = '<img src="'.$scormpixdir.'/notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
461 } else {
462 $statusicon = '<img src="'.$scormpixdir.'/asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
463 }
530a61d5 464 }
f6520280 465 if ($sco->id == $scoid) {
466 $scodata = scorm_get_sco($sco->id, SCO_DATA);
467 $startbold = '<b>';
468 $endbold = '</b>';
469 $findnext = true;
470 $shownext = isset($scodata->next) ? $scodata->next : 0;
471 $showprev = isset($scodata->previous) ? $scodata->previous : 0;
dc383b6f 472 }
dc383b6f 473
f6520280 474 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
475 if (!empty($sco->launch)) {
476 $previd = $sco->id;
477 }
dc383b6f 478 }
f6520280 479 if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
480 if ($sco->id == $scoid) {
481 $result->prerequisites = true;
482 }
483 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
484 $result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
485 $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
486 } else {
487 if ($sco->id == $scoid) {
488 $result->prerequisites = false;
489 }
c852a628 490 $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."</li>\n";
dc383b6f 491 }
dc383b6f 492 }
493 } else {
c852a628 494 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
dc383b6f 495 }
496 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
497 if (!empty($nextsco->launch)) {
498 $nextid = $nextsco->id;
499 }
500 }
501 }
502 for ($i=0;$i<$level;$i++) {
503 $result->toc .= "\t\t</ul></li>\n";
504 }
505
506 if ($play) {
f6520280 507 $sco = scorm_get_sco($scoid);
dc383b6f 508 $sco->previd = $previd;
509 $sco->nextid = $nextid;
510 $result->sco = $sco;
511 $result->incomplete = $incomplete;
512 } else {
513 $result->incomplete = $incomplete;
514 }
515 }
516 $result->toc .= "\t</ul>\n";
517 if ($scorm->hidetoc == 0) {
518 $result->toc .= '
f4ba7e1a 519 <script type="text/javascript">
520 //<![CDATA[
dc383b6f 521 function expandCollide(which,list,item) {
522 var nn=document.ids?true:false
523 var w3c=document.getElementById?true:false
524 var beg=nn?"document.ids.":w3c?"document.getElementById(":"document.all.";
525 var mid=w3c?").style":".style";
f6520280 526 which = which.substring(0,(which.length));
dc383b6f 527 if (eval(beg+list+mid+".display") != "none") {
f6520280 528 document.getElementById(which).src = "'.$scormpixdir.'/plus.gif";
dc383b6f 529 eval(beg+list+mid+".display=\'none\';");
530 new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
531 } else {
f6520280 532 document.getElementById(which).src = "'.$scormpixdir.'/minus.gif";
dc383b6f 533 eval(beg+list+mid+".display=\'block\';");
534 new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
535 }
536 }
f4ba7e1a 537 //]]>
dc383b6f 538 </script>'."\n";
539 }
540
541 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid=';
542 $result->tocmenu = popup_form($url,$tocmenus, "tocmenu", $sco->id, '', '', '', true);
543
544 return $result;
545}
546
547?>