Merged from STABLE
[moodle.git] / mod / scorm / locallib.php
CommitLineData
e4aa175a 1<?php // $Id$
f69db63e 2
e4aa175a 3define("VALUESCOES",0);
4define("VALUEHIGHEST",1);
5define("VALUEAVERAGE",2);
6define("VALUESUM",3);
7
f69db63e 8// Functions added by Pham Minh Duc
9function scorm_get_score_from_parent($sco,$userid,$grademethod=VALUESCOES) {
10 $scores = NULL;
11 $scores->scoes = 0;
12 $scores->values = 0;
13 $scores->scaled = 0;
14 $scores->max = 0;
15 $scores->sum = 0;
16
17 $scoes_total = 0;
18 $scoes_count = 0;
19 $attempt = scorm_get_last_attempt($sco->scorm, $userid);
20 $scoes = get_records('scorm_scoes', 'parent', $sco->identifier);
21 foreach ($scoes as $sco)
22 {
23 $scoes_total++;
24 if ($userdata=scorm_get_tracks($sco->id, $userid,$attempt)) {
25 if (($userdata->status == 'completed') || ($userdata->success_status == 'passed')) {
26 $scoes_count++;
27 }
28
29 $scoreraw = $userdata->score_raw;
30
31 if (!empty($userdata->score_raw)) {
32 $scores->values++;
33 $scores->sum += $userdata->score_raw;
34 $scores->max = ($userdata->score_raw > $scores->max)?$userdata->score_raw:$scores->max;
35 }
36 if (!empty($userdata->score_scaled)) {
37 $scores->scaled = $scores->scaled + $userdata->score_scaled;
38 }
39 }
40 }
41 if ($scoes_count > 0) {
42 $scores->scaled = ($scores->scaled)/($scoes_count);
43 }
44 switch ($grademethod) {
45 case VALUEHIGHEST:
46 return $scores->max;
47 break;
48 case VALUEAVERAGE:
49 if ($scores->values > 0) {
50 return $scores->sum/$scores->values;
51 } else {
52 return 0;
53 }
54 break;
55 case VALUESUM:
56 return $scores->sum;
57 break;
58 case VALUESCOES:
59 return $scores->scaled;
60 break;
61 }
62}
63
64function scorm_get_user_sco_count($scormid, $userid) {
65 $scoes_count = 0;
66 $attempt = scorm_get_last_attempt($current->scorm, $userid);
67 $scoes = get_records('scorm_scoes', 'scorm', $scormid);
68
69 foreach ($scoes as $sco) {
70 if ($userdata=scorm_get_tracks($sco->id, $userid,$attempt)) {
71 if (($userdata->status == 'completed') || ($userdata->success_status == 'passed')) {
72 $scoes_count++;
73 }
74 }
75 }
76 return $scoes_count;
77}
78
79function scorm_grade_user_new($scoes, $userid, $grademethod=VALUESCOES) {
80 $scores = NULL;
81 $scores->scoes = 0;
82 $scores->values = 0;
83 $scores->scaled = 0;
84 $scores->max = 0;
85 $scores->sum = 0;
86
87 if (!$scoes) {
88 return '';
89 }
90 $current = current($scoes);
91 $attempt = scorm_get_last_attempt($current->scorm, $userid);
92 foreach ($scoes as $sco) {
93 if ($userdata=scorm_get_tracks($sco->id, $userid,$attempt)) {
94 if (($userdata->status == 'completed') || ($userdata->success_status == 'passed')) {
95 $scores->scoes++;
96 }
97 $scaled = $userdata->score_scaled;
98 $scoreraw = $userdata->score_raw;
99 if ($scaled ==0){
100 $scores->scaled = $scores->scaled / $scores->scoes;
101 }
102 if (!empty($userdata->score_raw)) {
103 $scores->values++;
104 $scores->sum += $userdata->score_raw;
105 $scores->max = ($userdata->score_raw > $scores->max)?$userdata->score_raw:$scores->max;
106 }
107 if (!empty($scaled)) {
108 $scores->scaled = (($scores->scaled) * ($scores->scoes-1) + $scaled)/($scores->scoes);
109 }
110 }
111 }
112 switch ($grademethod) {
113 case VALUEHIGHEST:
114 return $scores->max;
115 break;
116 case VALUEAVERAGE:
117 if ($scores->values > 0) {
118 return $scores->sum/$scores->values;
119 } else {
120 return 0;
121 }
122 break;
123 case VALUESUM:
124 return $scores->sum;
125 break;
126 case VALUESCOES:
127 return $scores->scaled;
128 break;
129 }
130}
131
132function scorm_insert_statistic($statisticInput){
133 $id = null;
134 if ($statistic = get_record_select('scorm_statistic',"userid='$statisticInput->userid' AND scormid='$statisticInput->scormid'")) {
135
136 $statistic->durationtime = $statisticInput->duration;
137 $statistic->accesstime = $statisticInput->accesstime;
138 $statistic->status = $statisticInput->status;
139 $statistic->attemptnumber = $statisticInput->attemptnumber;
140 $id = update_record('scorm_statistic',$statistic);
141 } else {
142 $id = insert_record('scorm_statistic',$statisticInput);
143 }
144 return $id;
145}
146
147function scorm_insert_trackmodel($userid,$scormid,$scoid,$attempt) {
148 $id = null;
149 if ($suspendtrack = get_record_select('scorm_suspendtrack',"userid='$userid' AND scormid='$scormid'")) {
150 $suspendtrack->suspendscoid = $scoid;
151 $suspendtrack->attempt = $attempt;
152 $id = update_record('scorm_suspendtrack',$suspendtrack);
153 } else {
154 $suspendtrack->scormid = $scormid;
155 $suspendtrack->suspendscoid = $scoid;
156 $suspendtrack->userid = $userid;
157 $suspendtrack->attempt = $attempt;
158 $id = insert_record('scorm_suspendtrack',$suspendtrack);
159 }
160 return $id;
161}
162
163function scorm_get_suspendscoid($scormid,$userid) {
164 if ($sco = get_record("scorm_suspendtrack","scormid",$scormid,"userid",$userid)) {
165 $suspendscoid = $sco->suspendscoid;
166 return $suspendscoid;
167 } else {
168 return 0;
169 }
170}
171
172function scorm_set_attempt($scoid,$userid) {
173 if ($scormid = get_field('scorm_scoes','scorm','id',$scoid)) {
174 $attempt = scorm_get_last_attempt($scormid,$userid);
175 } else {
176 $attempt = 1;
177 }
178 $scormtype = get_field('scorm_scoes','scormtype','id',$scoid) ;
179 if ($scormtype == 'sco'){
180 $element = 'cmi.attempt_status';
181 $value = 'attempted';
182 scorm_insert_track($userid,$scormid,$scoid,$attempt,$element,$value);
183 }
184}
185
186function scorm_get_AbsoluteTimeLimit($scoid){
187 $sco = get_record("scorm_scoes","id",$scoid);
188 if (!empty($sco)){
189 return $sco->attemptAbsoluteDurationLimit;
190 }
191 return 0;
192}
193
194function scorm_update_status($scormid,$scoid)
195{
196
197}
198
199function scorm_get_nextsco($scormid,$scoid)
200{
201
202}
203
204function scorm_get_presco($scormid,$scoid)
205{
206
207}
208
209function scorm_isChoice($scormid,$scoid)
210{
211 $sco = get_record("scorm_sequencing_controlmode","scormid",$scormid,"scoid",$scoid);
212 $scoparent = get_record("scorm_sequencing_controlmode","scormid",$scormid,"identifier",$sco->parent);
213
214 return $scoparent->choice;
215}
216
217function scorm_isChoiceexit($scormid,$scoid)
218{
219 $sco = get_record("scorm_sequencing_controlmode","scormid",$scormid,"scoid",$scoid);
220 $scoparent = get_record("scorm_sequencing_controlmode","scormid",$scormid,"identifier",$sco->parent);
221
222 return $scoparent->choiceexit;
223}
224// End add
225
e4aa175a 226/// Local Library of functions and constants for module scorm
227
228/**
229* Create a new temporary subdirectory with a random name in the given path
230*
231* @param string $strpath The scorm data directory
232* @return string/boolean
233*/
234function scorm_datadir($strPath)
235{
236 global $CFG;
237
238 if (is_dir($strPath)) {
239 do {
240 // Create a random string of 8 chars
241 $randstring = NULL;
242 $lchar = '';
243 $len = 8;
244 for ($i=0; $i<$len; $i++) {
245 $char = chr(rand(48,122));
246 while (!ereg('[a-zA-Z0-9]', $char)){
247 if ($char == $lchar) continue;
248 $char = chr(rand(48,90));
249 }
250 $randstring .= $char;
251 $lchar = $char;
252 }
253 $datadir='/'.$randstring;
254 } while (file_exists($strPath.$datadir));
255 mkdir($strPath.$datadir, $CFG->directorypermissions);
256 @chmod($strPath.$datadir, $CFG->directorypermissions); // Just in case mkdir didn't do it
257 return $strPath.$datadir;
258 } else {
259 return false;
260 }
261}
262
263/**
264* Given a package directory, this function will check if the package is valid
265*
266* @param string $packagedir The package directory
267* @return mixed
268*/
269function scorm_validate($packagedir) {
270
e4aa175a 271 $validation = new stdClass();
272 if (is_file($packagedir.'/imsmanifest.xml')) {
273 $validation->result = 'found';
274 $validation->pkgtype = 'SCORM';
275 } else {
276 if ($handle = opendir($packagedir)) {
277 while (($file = readdir($handle)) !== false) {
278 $ext = substr($file,strrpos($file,'.'));
279 if (strtolower($ext) == '.cst') {
280 $validation->result = 'found';
281 $validation->pkgtype = 'AICC';
282 break;
283 }
284 }
285 closedir($handle);
286 }
287 if (!isset($validation->result)) {
288 $validation->result = 'nomanifest';
289 $validation->pkgtype = 'SCORM';
290 }
291 }
292 return $validation;
293}
294
295function scorm_get_user_data($userid) {
296/// Gets user info required to display the table of scorm results
297/// for report.php
298
299 return get_record('user','id',$userid,'','','','','firstname, lastname, picture');
300}
301
302function scorm_string_wrap($stringa, $len=15) {
303// Crop the given string into max $len characters lines
304 $textlib = textlib_get_instance();
305 if ($textlib->strlen($stringa, current_charset()) > $len) {
306 $words = explode(' ', $stringa);
307 $newstring = '';
308 $substring = '';
309 foreach ($words as $word) {
310 if (($textlib->strlen($substring, current_charset())+$textlib->strlen($word, current_charset())+1) < $len) {
311 $substring .= ' '.$word;
312 } else {
313 $newstring .= ' '.$substring.'<br />';
314 $substring = $word;
315 }
316 }
317 if (!empty($substring)) {
318 $newstring .= ' '.$substring;
319 }
320 return $newstring;
321 } else {
322 return $stringa;
323 }
324}
325
326function scorm_eval_prerequisites($prerequisites,$usertracks) {
e4aa175a 327 $element = '';
328 $stack = array();
329 $statuses = array(
330 'passed' => 'passed',
331 'completed' => 'completed',
332 'failed' => 'failed',
333 'incomplete' => 'incomplete',
334 'browsed' => 'browsed',
335 'not attempted' => 'notattempted',
336 'p' => 'passed',
337 'c' => 'completed',
338 'f' => 'failed',
339 'i' => 'incomplete',
340 'b' => 'browsed',
341 'n' => 'notattempted'
342 );
343 $i=0;
344 while ($i<strlen($prerequisites)) {
345 $symbol = $prerequisites[$i];
346 switch ($symbol) {
347 case '&':
348 case '|':
349 $symbol .= $symbol;
350 case '~':
351 case '(':
352 case ')':
353 case '*':
e4aa175a 354 $element = trim($element);
355
356 if (!empty($element)) {
357 $element = trim($element);
358 if (isset($usertracks[$element])) {
359 $element = '((\''.$usertracks[$element]->status.'\' == \'completed\') || '.
360 '(\''.$usertracks[$element]->status.'\' == \'passed\'))';
361 } else if (($operator = strpos($element,'=')) !== false) {
362 $item = trim(substr($element,0,$operator));
363 if (!isset($usertracks[$item])) {
364 return false;
365 }
366
367 $value = trim(trim(substr($element,$operator+1)),'"');
368 if (isset($statuses[$value])) {
369 $status = $statuses[$value];
370 } else {
371 return false;
372 }
373
374 $element = '(\''.$usertracks[$item]->status.'\' == \''.$status.'\')';
375 } else if (($operator = strpos($element,'<>')) !== false) {
376 $item = trim(substr($element,0,$operator));
377 if (!isset($usertracks[$item])) {
378 return false;
379 }
380
381 $value = trim(trim(substr($element,$operator+2)),'"');
382 if (isset($statuses[$value])) {
383 $status = $statuses[$value];
384 } else {
385 return false;
386 }
387
388 $element = '(\''.$usertracks[$item]->status.'\' != \''.$status.'\')';
389 } else if (is_numeric($element)) {
390 if ($symbol == '*') {
391 $symbol = '';
392 $open = strpos($prerequisites,'{',$i);
393 $opened = 1;
394 $closed = 0;
395 for ($close=$open+1; (($opened > $closed) && ($close<strlen($prerequisites))); $close++) {
396 if ($prerequisites[$close] == '}') {
397 $closed++;
398 } else if ($prerequisites[$close] == '{') {
399 $opened++;
400 }
401 }
402 $i = $close;
403
404 $setelements = explode(',', substr($prerequisites, $open+1, $close-($open+1)-1));
405 $settrue = 0;
406 foreach ($setelements as $setelement) {
407 if (scorm_eval_prerequisites($setelement,$usertracks)) {
408 $settrue++;
409 }
410 }
411
412 if ($settrue >= $element) {
413 $element = 'true';
414 } else {
415 $element = 'false';
416 }
417 }
418 } else {
419 return false;
420 }
421
422 array_push($stack,$element);
423 $element = '';
424 }
425 if ($symbol == '~') {
426 $symbol = '!';
427 }
428 if (!empty($symbol)) {
429 array_push($stack,$symbol);
430 }
431 break;
432 default:
433 $element .= $symbol;
434 break;
435 }
436 $i++;
437 }
438 if (!empty($element)) {
439 $element = trim($element);
440 if (isset($usertracks[$element])) {
441 $element = '((\''.$usertracks[$element]->status.'\' == \'completed\') || '.
442 '(\''.$usertracks[$element]->status.'\' == \'passed\'))';
443 } else if (($operator = strpos($element,'=')) !== false) {
444 $item = trim(substr($element,0,$operator));
445 if (!isset($usertracks[$item])) {
446 return false;
447 }
448
449 $value = trim(trim(substr($element,$operator+1)),'"');
450 if (isset($statuses[$value])) {
451 $status = $statuses[$value];
452 } else {
453 return false;
454 }
455
456 $element = '(\''.$usertracks[$item]->status.'\' == \''.$status.'\')';
457 } else if (($operator = strpos($element,'<>')) !== false) {
458 $item = trim(substr($element,0,$operator));
459 if (!isset($usertracks[$item])) {
460 return false;
461 }
462
463 $value = trim(trim(substr($element,$operator+1)),'"');
464 if (isset($statuses[$value])) {
465 $status = $statuses[$value];
466 } else {
467 return false;
468 }
469
470 $element = '(\''.$usertracks[$item]->status.'\' != \''.trim($status).'\')';
471 } else {
472 return false;
473 }
474
475 array_push($stack,$element);
476 }
477 return eval('return '.implode($stack).';');
478}
479
e4aa175a 480function scorm_insert_track($userid,$scormid,$scoid,$attempt,$element,$value) {
e4aa175a 481 $id = null;
482 if ($track = get_record_select('scorm_scoes_track',"userid='$userid' AND scormid='$scormid' AND scoid='$scoid' AND attempt='$attempt' AND element='$element'")) {
483 $track->value = $value;
484 $track->timemodified = time();
e4aa175a 485 $id = update_record('scorm_scoes_track',$track);
486 } else {
487 $track->userid = $userid;
488 $track->scormid = $scormid;
489 $track->scoid = $scoid;
490 $track->attempt = $attempt;
491 $track->element = $element;
492 $track->value = addslashes($value);
493 $track->timemodified = time();
e4aa175a 494 $id = insert_record('scorm_scoes_track',$track);
495 }
496 return $id;
497}
498
e4aa175a 499function scorm_add_time($a, $b) {
500 $aes = explode(':',$a);
501 $bes = explode(':',$b);
502 $aseconds = explode('.',$aes[2]);
503 $bseconds = explode('.',$bes[2]);
504 $change = 0;
505
506 $acents = 0; //Cents
507 if (count($aseconds) > 1) {
508 $acents = $aseconds[1];
509 }
510 $bcents = 0;
511 if (count($bseconds) > 1) {
512 $bcents = $bseconds[1];
513 }
514 $cents = $acents + $bcents;
515 $change = floor($cents / 100);
516 $cents = $cents - ($change * 100);
517 if (floor($cents) < 10) {
518 $cents = '0'. $cents;
f69db63e 519 }
e4aa175a 520
f69db63e 521 $secs = $aseconds[0] + $bseconds[0] + $change; //Seconds
522 $change = floor($secs / 60);
523 $secs = $secs - ($change * 60);
524 if (floor($secs) < 10) {
525 $secs = '0'. $secs;
526 }
e4aa175a 527
f69db63e 528 $mins = $aes[1] + $bes[1] + $change; //Minutes
529 $change = floor($mins / 60);
530 $mins = $mins - ($change * 60);
531 if ($mins < 10) {
532 $mins = '0' . $mins;
533 }
e4aa175a 534
f69db63e 535 $hours = $aes[0] + $bes[0] + $change; //Hours
536 if ($hours < 10) {
537 $hours = '0' . $hours;
538 }
e4aa175a 539
f69db63e 540 if ($cents != '0') {
541 return $hours . ":" . $mins . ":" . $secs . '.' . $cents;
542 } else {
543 return $hours . ":" . $mins . ":" . $secs;
e4aa175a 544 }
e4aa175a 545}
546
f69db63e 547function scorm_external_link($link) {
548// check if a link is external
549 $result = false;
550 $link = strtolower($link);
551 if (substr($link,0,7) == 'http://') {
552 $result = true;
553 } else if (substr($link,0,8) == 'https://') {
554 $result = true;
555 } else if (substr($link,0,4) == 'www.') {
556 $result = true;
557 }
558 return $result;
559}
e4aa175a 560
f69db63e 561function scorm_grade_user($scoes, $userid, $grademethod=VALUESCOES) {
e4aa175a 562 $scores = NULL;
563 $scores->scoes = 0;
564 $scores->values = 0;
e4aa175a 565 $scores->max = 0;
566 $scores->sum = 0;
567
568 if (!$scoes) {
e4aa175a 569 return '';
570 }
571
572 $current = current($scoes);
573 $attempt = scorm_get_last_attempt($current->scorm, $userid);
e4aa175a 574 foreach ($scoes as $sco) {
575 if ($userdata=scorm_get_tracks($sco->id, $userid,$attempt)) {
f69db63e 576 if (($userdata->status == 'completed') || ($userdata->status == 'passed')) {
e4aa175a 577 $scores->scoes++;
f69db63e 578 }
e4aa175a 579 if (!empty($userdata->score_raw)) {
580 $scores->values++;
581 $scores->sum += $userdata->score_raw;
582 $scores->max = ($userdata->score_raw > $scores->max)?$userdata->score_raw:$scores->max;
e4aa175a 583 }
e4aa175a 584 }
585 }
e4aa175a 586 switch ($grademethod) {
587 case VALUEHIGHEST:
e4aa175a 588 return $scores->max;
589 break;
590 case VALUEAVERAGE:
e4aa175a 591 if ($scores->values > 0) {
592 return $scores->sum/$scores->values;
593 } else {
594 return 0;
595 }
596 break;
597 case VALUESUM:
e4aa175a 598 return $scores->sum;
599 break;
600 case VALUESCOES:
f69db63e 601 return $scores->scoes;
e4aa175a 602 break;
603 }
604}
605
606function scorm_count_launchable($scormid,$organization) {
607 return count_records_select('scorm_scoes',"scorm=$scormid AND organization='$organization' AND launch<>''");
608}
609
610function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
611 global $CFG;
f69db63e 612
613 // Added by Pham Minh Duc
e4aa175a 614 $suspendscoid = scorm_get_suspendscoid($scorm->id,$user->id);
f69db63e 615 // End add
e4aa175a 616
617 $strexpand = get_string('expcoll','scorm');
618 $modestr = '';
619 if ($mode == 'browse') {
620 $modestr = '&amp;mode='.$mode;
621 }
622 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
623
624 $result = new stdClass();
625 $result->toc = "<ul id='0' class='$liststyle'>\n";
626 $tocmenus = array();
627 $result->prerequisites = true;
628 $incomplete = false;
629
630 //
631 // Get the current organization infos
632 //
633 $organizationsql = '';
634 if (!empty($currentorg)) {
635 if (($organizationtitle = get_field('scorm_scoes','title','scorm',$scorm->id,'identifier',$currentorg)) != '') {
636 $result->toc .= "\t<li>$organizationtitle</li>\n";
637 $tocmenus[] = $organizationtitle;
638 }
639 $organizationsql = "AND organization='$currentorg'";
640 }
641 //
642 // If not specified retrieve the last attempt number
643 //
644 if (empty($attempt)) {
645 $attempt = scorm_get_last_attempt($scorm->id, $user->id);
646 }
647 $result->attemptleft = $scorm->maxattempt - $attempt;
e4aa175a 648
649 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
e4aa175a 650 $usertracks = array();
651 foreach ($scoes as $sco) {
e4aa175a 652 if (!empty($sco->launch)) {
653 if ($usertrack=scorm_get_tracks($sco->id,$user->id,$attempt)) {
654 if ($usertrack->status == '') {
655 $usertrack->status = 'notattempted';
656 }
e4aa175a 657 $usertracks[$sco->identifier] = $usertrack;
658 }
659 }
660 }
661
662 $level=0;
663 $sublist=1;
664 $previd = 0;
665 $nextid = 0;
666 $findnext = false;
667 $parents[$level]='/';
668
669 foreach ($scoes as $sco) {
670 if ($parents[$level]!=$sco->parent) {
671 if ($newlevel = array_search($sco->parent,$parents)) {
672 for ($i=0; $i<($level-$newlevel); $i++) {
673 $result->toc .= "\t\t</ul></li>\n";
674 }
675 $level = $newlevel;
676 } else {
677 $i = $level;
678 $closelist = '';
679 while (($i > 0) && ($parents[$level] != $sco->parent)) {
680 $closelist .= "\t\t</ul></li>\n";
681 $i--;
682 }
683 if (($i == 0) && ($sco->parent != $currentorg)) {
684 $style = '';
685 if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
686 $style = ' style="display: none;"';
687 }
688 $result->toc .= "\t\t<li><ul id='$sublist' class='$liststyle'$style>\n";
689 $level++;
690 } else {
691 $result->toc .= $closelist;
692 $level = $i;
693 }
694 $parents[$level]=$sco->parent;
695 }
696 }
697 $result->toc .= "\t\t<li>";
698 $nextsco = next($scoes);
699 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
700 $sublist++;
701 $icon = 'minus';
702 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
703 $icon = 'plus';
704 }
705 $result->toc .= '<a href="javascript:expandCollide(img'.$sublist.','.$sublist.','.$nextsco->id.');"><img id="img'.$sublist.'" src="'.$scormpixdir.'/'.$icon.'.gif" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
706 } else {
707 $result->toc .= '<img src="'.$scormpixdir.'/spacer.gif" />';
708 }
709 if (empty($sco->title)) {
710 $sco->title = $sco->identifier;
711 }
712 if (!empty($sco->launch)) {
713 $startbold = '';
714 $endbold = '';
715 $score = '';
716 if (empty($scoid) && ($mode != 'normal')) {
717 $scoid = $sco->id;
718 }
e4aa175a 719 if ($suspendscoid == $sco->id){
720 $result->toc .= '<img src="'.$scormpixdir.'/suspend.gif" alt="Dang tam dung o day" title="Dang dung o day" />';
721 } else {
e4aa175a 722 if (isset($usertracks[$sco->identifier])) {
723 $usertrack = $usertracks[$sco->identifier];
724 $strstatus = get_string($usertrack->status,'scorm');
725 $result->toc .= '<img src="'.$scormpixdir.'/'.$usertrack->status.'.gif" alt="'.$strstatus.'" title="'.$strstatus.'" />';
726
727 if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
e4aa175a 728 $incomplete = true;
729 if ($play && empty($scoid)) {
730 $scoid = $sco->id;
731 }
732 }
733 if ($usertrack->score_raw != '') {
734 $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
735 }
736 } else {
737 if ($play && empty($scoid)) {
738 $scoid = $sco->id;
739 }
740 if ($sco->scormtype == 'sco') {
741 $result->toc .= '<img src="'.$scormpixdir.'/notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
742 $incomplete = true;
743 } else {
744 $result->toc .= '<img src="'.$scormpixdir.'/asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
745 }
746 }
747 }
748 if ($sco->id == $scoid) {
749 $startbold = '<b>';
750 $endbold = '</b>';
751 $findnext = true;
752 $shownext = $sco->next;
753 $showprev = $sco->previous;
754 }
755
756 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
757 if (!empty($sco->launch)) {
758 $previd = $sco->id;
759 }
760 }
761 if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
762 if ($sco->id == $scoid) {
763 $result->prerequisites = true;
764 }
f69db63e 765 // Modified by Pham Minh Duc
766 if (scorm_isChoice($scorm->id,$sco->id) == 1) {
767 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
768 $result->toc .= '&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
769 $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
770 } else {
771 $result->toc .= '&nbsp;'.$startbold.format_string($sco->title).$score.$endbold."</li>\n";
772 $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
e4aa175a 773 }
f69db63e 774 // End modify
e4aa175a 775 } else {
776 if ($sco->id == $scoid) {
777 $result->prerequisites = false;
778 }
779 $result->toc .= '&nbsp;'.$sco->title."</li>\n";
780 }
781 } else {
782 $result->toc .= '&nbsp;'.$sco->title."</li>\n";
783 }
784 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
785 if (!empty($nextsco->launch)) {
786 $nextid = $nextsco->id;
787 }
788 }
789 }
790 for ($i=0;$i<$level;$i++) {
791 $result->toc .= "\t\t</ul></li>\n";
792 }
793
794 if ($play) {
795 $sco = get_record('scorm_scoes','id',$scoid);
796 $sco->previd = $previd;
797 $sco->nextid = $nextid;
798 $result->sco = $sco;
799 $result->incomplete = $incomplete;
800 } else {
801 $result->incomplete = $incomplete;
802 }
803 }
804 $result->toc .= "\t</ul>\n";
805 if ($scorm->hidetoc == 0) {
806 $result->toc .= '
807 <script language="javascript" type="text/javascript">
808 <!--
809 function expandCollide(which,list,item) {
810 var nn=document.ids?true:false
811 var w3c=document.getElementById?true:false
812 var beg=nn?"document.ids.":w3c?"document.getElementById(":"document.all.";
813 var mid=w3c?").style":".style";
814
815 if (eval(beg+list+mid+".display") != "none") {
816 which.src = "'.$scormpixdir.'/plus.gif";
817 eval(beg+list+mid+".display=\'none\';");
818 new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
819 } else {
820 which.src = "'.$scormpixdir.'/minus.gif";
821 eval(beg+list+mid+".display=\'block\';");
822 new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
823 }
824 }
825 -->
826 </script>'."\n";
827 }
828
829 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid=';
830 $result->tocmenu = popup_form($url,$tocmenus, "tocmenu", $sco->id, '', '', '', true);
831
832 return $result;
833}
834
835function scorm_get_last_attempt($scormid, $userid) {
e4aa175a 836/// Find the last attempt number for the given user id and scorm id
837 if ($lastattempt = get_record('scorm_scoes_track', 'userid', $userid, 'scormid', $scormid, '', '', 'max(attempt) as a')) {
838 if (empty($lastattempt->a)) {
839 return '1';
840 } else {
841 return $lastattempt->a;
842 }
843 }
844}
845
e4aa175a 846function scorm_get_tracks($scoid,$userid,$attempt='') {
e4aa175a 847/// Gets all tracks of specified sco and user
848 global $CFG;
849
850 if (empty($attempt)) {
851 if ($scormid = get_field('scorm_scoes','scorm','id',$scoid)) {
852 $attempt = scorm_get_last_attempt($scormid,$userid);
853 } else {
854 $attempt = 1;
855 }
856 }
857 $attemptsql = ' AND attempt=' . $attempt;
858 if ($tracks = get_records_select('scorm_scoes_track',"userid=$userid AND scoid=$scoid".$attemptsql,'element ASC')) {
859 $usertrack->userid = $userid;
860 $usertrack->scoid = $scoid;
861 $usertrack->score_raw = '';
e4aa175a 862 $usertrack->status = '';
e4aa175a 863 $usertrack->total_time = '00:00:00';
864 $usertrack->session_time = '00:00:00';
865 $usertrack->timemodified = 0;
f69db63e 866 // Added by Pham Minh Duc
867 $usertrack->score_scaled = '';
868 $usertrack->success_status = '';
869 $usertrack->attempt_status = '';
870 $usertrack->satisfied_status = '';
871 // End Add
e4aa175a 872 foreach ($tracks as $track) {
873 $element = $track->element;
874 $usertrack->{$element} = $track->value;
875 switch ($element) {
f69db63e 876 // Added by Pham Minh Duc
e4aa175a 877 case 'cmi.attempt_status':
878 $usertrack->status = $track->value;
879 $usertrack->attempt_status = $track->value;
880 break;
e4aa175a 881 case 'cmi.success_status':
882 $usertrack->success_status = $track->value;
883 if ($track->value=='passed'){
884 $usertrack->satisfied_status = 'satisfied';
885 }
886 if ($track->value=='failed'){
887 $usertrack->satisfied_status = 'notSatisfied';
888 }
889 break;
f69db63e 890 case 'cmi.score.scaled':
891 $usertrack->score_scaled = $track->value;
892 break;
893 // End Add
894 case 'cmi.core.lesson_status':
895 case 'cmi.completion_status':
896 if ($track->value == 'not attempted') {
897 $track->value = 'notattempted';
898 }
899 $usertrack->status = $track->value;
900 break;
e4aa175a 901 case 'cmi.core.score.raw':
902 case 'cmi.score.raw':
903 $usertrack->score_raw = $track->value;
904 break;
e4aa175a 905 case 'cmi.core.session_time':
906 case 'cmi.session_time':
907 $usertrack->session_time = $track->value;
908 break;
909 case 'cmi.core.total_time':
910 case 'cmi.total_time':
911 $usertrack->total_time = $track->value;
912 break;
913 }
914 if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
915 $usertrack->timemodified = $track->timemodified;
916 }
917 }
918 return $usertrack;
919 } else {
920 return false;
921 }
922}
923
e4aa175a 924//-----------------------------------------------------
925/// Library of functions and constants for parsing packages
926
927function scorm_parse($scorm) {
928 global $CFG;
929
e4aa175a 930 // Parse scorm manifest
931 if ($scorm->pkgtype == 'AICC') {
932 $scorm->launch = scorm_parse_aicc($scorm->dir.'/'.$scorm->id,$scorm->id);
933 } else {
934 if (basename($scorm->reference) != 'imsmanifest.xml') {
935 $scorm->launch = scorm_parse_scorm($scorm->dir.'/'.$scorm->id,$scorm->id);
936 } else {
937 $scorm->launch = scorm_parse_scorm($CFG->dataroot.'/'.$scorm->course.'/'.dirname($scorm->reference),$scorm->id);
938 }
939 }
940
941 return $scorm->launch;
942}
943
944/**
945* Take the header row of an AICC definition file
946* and returns sequence of columns and a pointer to
947* the sco identifier column.
948*
949* @param string $row AICC header row
950* @param string $mastername AICC sco identifier column
951* @return mixed
952*/
953function scorm_get_aicc_columns($row,$mastername='system_id') {
954 $tok = strtok(strtolower($row),"\",\n\r");
955 $result->columns = array();
956 $i=0;
957 while ($tok) {
958 if ($tok !='') {
959 $result->columns[] = $tok;
960 if ($tok == $mastername) {
961 $result->mastercol = $i;
962 }
963 $i++;
964 }
965 $tok = strtok("\",\n\r");
966 }
967 return $result;
968}
969
970/**
971* Given a colums array return a string containing the regular
972* expression to match the columns in a text row.
973*
974* @param array $column The header columns
975* @param string $remodule The regular expression module for a single column
976* @return string
977*/
978function scorm_forge_cols_regexp($columns,$remodule='(".*")?,') {
979 $regexp = '/^';
980 foreach ($columns as $column) {
981 $regexp .= $remodule;
982 }
983 $regexp = substr($regexp,0,-1) . '/';
984 return $regexp;
985}
986
987function scorm_parse_aicc($pkgdir,$scormid){
e4aa175a 988 $version = 'AICC';
989 $ids = array();
990 $courses = array();
f69db63e 991 $extaiccfiles = array('crs','des','au','cst','ort','pre','cmp');
e4aa175a 992 if ($handle = opendir($pkgdir)) {
993 while (($file = readdir($handle)) !== false) {
f69db63e 994 if ($file[0] != '.') {
995 $ext = substr($file,strrpos($file,'.'));
996 $extension = strtolower(substr($ext,1));
997 if (in_array($extension,$extaiccfiles)) {
998 $id = strtolower(basename($file,$ext));
999 $ids[$id]->$extension = $file;
1000 }
1001 }
e4aa175a 1002 }
1003 closedir($handle);
1004 }
1005 foreach ($ids as $courseid => $id) {
1006 if (isset($id->crs)) {
1007 if (is_file($pkgdir.'/'.$id->crs)) {
1008 $rows = file($pkgdir.'/'.$id->crs);
1009 foreach ($rows as $row) {
1010 if (preg_match("/^(.+)=(.+)$/",$row,$matches)) {
1011 switch (strtolower(trim($matches[1]))) {
1012 case 'course_id':
1013 $courses[$courseid]->id = trim($matches[2]);
1014 break;
1015 case 'course_title':
1016 $courses[$courseid]->title = trim($matches[2]);
1017 break;
1018 case 'version':
1019 $courses[$courseid]->version = 'AICC_'.trim($matches[2]);
1020 break;
1021 }
1022 }
1023 }
1024 }
1025 }
1026 if (isset($id->des)) {
1027 $rows = file($pkgdir.'/'.$id->des);
1028 $columns = scorm_get_aicc_columns($rows[0]);
1029 $regexp = scorm_forge_cols_regexp($columns->columns);
1030 for ($i=1;$i<count($rows);$i++) {
1031 if (preg_match($regexp,$rows[$i],$matches)) {
1032 for ($j=0;$j<count($columns->columns);$j++) {
1033 $column = $columns->columns[$j];
1034 $courses[$courseid]->elements[substr(trim($matches[$columns->mastercol+1]),1,-1)]->$column = substr(trim($matches[$j+1]),1,-1);
1035 }
1036 }
1037 }
1038 }
1039 if (isset($id->au)) {
1040 $rows = file($pkgdir.'/'.$id->au);
1041 $columns = scorm_get_aicc_columns($rows[0]);
1042 $regexp = scorm_forge_cols_regexp($columns->columns);
1043 for ($i=1;$i<count($rows);$i++) {
1044 if (preg_match($regexp,$rows[$i],$matches)) {
1045 for ($j=0;$j<count($columns->columns);$j++) {
1046 $column = $columns->columns[$j];
1047 $courses[$courseid]->elements[substr(trim($matches[$columns->mastercol+1]),1,-1)]->$column = substr(trim($matches[$j+1]),1,-1);
1048 }
1049 }
1050 }
1051 }
1052 if (isset($id->cst)) {
1053 $rows = file($pkgdir.'/'.$id->cst);
1054 $columns = scorm_get_aicc_columns($rows[0],'block');
1055 $regexp = scorm_forge_cols_regexp($columns->columns,'(.+)?,');
1056 for ($i=1;$i<count($rows);$i++) {
1057 if (preg_match($regexp,$rows[$i],$matches)) {
1058 for ($j=0;$j<count($columns->columns);$j++) {
1059 if ($j != $columns->mastercol) {
1060 $courses[$courseid]->elements[substr(trim($matches[$j+1]),1,-1)]->parent = substr(trim($matches[$columns->mastercol+1]),1,-1);
1061 }
1062 }
1063 }
1064 }
1065 }
1066 if (isset($id->ort)) {
1067 $rows = file($pkgdir.'/'.$id->ort);
1068 }
1069 if (isset($id->pre)) {
1070 $rows = file($pkgdir.'/'.$id->pre);
1071 $columns = scorm_get_aicc_columns($rows[0],'structure_element');
1072 $regexp = scorm_forge_cols_regexp($columns->columns,'(.+),');
1073 for ($i=1;$i<count($rows);$i++) {
1074 if (preg_match($regexp,$rows[$i],$matches)) {
1075 $courses[$courseid]->elements[$columns->mastercol+1]->prerequisites = substr(trim($matches[1-$columns->mastercol+1]),1,-1);
1076 }
1077 }
1078 }
1079 if (isset($id->cmp)) {
1080 $rows = file($pkgdir.'/'.$id->cmp);
1081 }
1082 }
1083 //print_r($courses);
1084 $launch = 0;
1085 if (isset($courses)) {
1086 foreach ($courses as $course) {
1087 unset($sco);
1088 $sco->identifier = $course->id;
1089 $sco->scorm = $scormid;
1090 $sco->organization = '';
1091 $sco->title = $course->title;
1092 $sco->parent = '/';
1093 $sco->launch = '';
1094 $sco->scormtype = '';
1095 //print_r($sco);
1096 $id = insert_record('scorm_scoes',$sco);
1097 if ($launch == 0) {
1098 $launch = $id;
1099 }
1100 if (isset($course->elements)) {
1101 foreach($course->elements as $element) {
1102 unset($sco);
1103 $sco->identifier = $element->system_id;
1104 $sco->scorm = $scormid;
1105 $sco->organization = $course->id;
1106 $sco->title = $element->title;
1107 if (strtolower($element->parent) == 'root') {
1108 $sco->parent = '/';
1109 } else {
1110 $sco->parent = $element->parent;
1111 }
1112 if (isset($element->file_name)) {
1113 $sco->launch = $element->file_name;
1114 $sco->scormtype = 'sco';
1115 } else {
1116 $element->file_name = '';
1117 $sco->scormtype = '';
1118 }
1119 if (!isset($element->prerequisites)) {
1120 $element->prerequisites = '';
1121 }
1122 $sco->prerequisites = $element->prerequisites;
1123 if (!isset($element->max_time_allowed)) {
1124 $element->max_time_allowed = '';
1125 }
1126 $sco->maxtimeallowed = $element->max_time_allowed;
1127 if (!isset($element->time_limit_action)) {
1128 $element->time_limit_action = '';
1129 }
1130 $sco->timelimitaction = $element->time_limit_action;
1131 if (!isset($element->mastery_score)) {
1132 $element->mastery_score = '';
1133 }
1134 $sco->masteryscore = $element->mastery_score;
1135 $sco->previous = 0;
1136 $sco->next = 0;
1137 $id = insert_record('scorm_scoes',$sco);
1138 if ($launch==0) {
1139 $launch = $id;
1140 }
1141 }
1142 }
1143 }
1144 }
1145 set_field('scorm','version','AICC','id',$scormid);
1146 return $launch;
1147}
1148
1149function scorm_get_resources($blocks) {
e4aa175a 1150 foreach ($blocks as $block) {
1151 if ($block['name'] == 'RESOURCES') {
1152 foreach ($block['children'] as $resource) {
1153 if ($resource['name'] == 'RESOURCE') {
1154 $resources[addslashes($resource['attrs']['IDENTIFIER'])] = $resource['attrs'];
1155 }
1156 }
1157 }
1158 }
1159 return $resources;
1160}
1161
1162function scorm_get_manifest($blocks,$scoes) {
e4aa175a 1163 static $parents = array();
1164 static $resources;
1165
1166 static $manifest;
1167 static $organization;
1168
1169 if (count($blocks) > 0) {
1170 foreach ($blocks as $block) {
1171 switch ($block['name']) {
1172 case 'METADATA':
1173 if (isset($block['children'])) {
1174 foreach ($block['children'] as $metadata) {
1175 if ($metadata['name'] == 'SCHEMAVERSION') {
1176 if (empty($scoes->version)) {
1177 if (isset($metadata['tagData']) && (preg_match("/^(1\.2)$|^(CAM )?(1\.3)$/",$metadata['tagData'],$matches))) {
1178 $scoes->version = 'SCORM_'.$matches[count($matches)-1];
1179 } else {
1180 $scoes->version = 'SCORM_1.2';
1181 }
1182 }
1183 }
1184 }
1185 }
1186 break;
1187 case 'MANIFEST':
f69db63e 1188 $manifest = addslashes($block['attrs']['IDENTIFIER']);
e4aa175a 1189 $organization = '';
1190 $resources = array();
1191 $resources = scorm_get_resources($block['children']);
1192 $scoes = scorm_get_manifest($block['children'],$scoes);
1193 if (count($scoes->elements) <= 0) {
1194 foreach ($resources as $item => $resource) {
1195 if (!empty($resource['HREF'])) {
1196 $sco = new stdClass();
1197 $sco->identifier = $item;
1198 $sco->title = $item;
1199 $sco->parent = '/';
1200 $sco->launch = addslashes($resource['HREF']);
1201 $sco->scormtype = addslashes($resource['ADLCP:SCORMTYPE']);
1202 $scoes->elements[$manifest][$organization][$item] = $sco;
1203 }
1204 }
1205 }
1206 break;
1207 case 'ORGANIZATIONS':
1208 if (!isset($scoes->defaultorg)) {
1209 $scoes->defaultorg = addslashes($block['attrs']['DEFAULT']);
1210 }
1211 $scoes = scorm_get_manifest($block['children'],$scoes);
1212 break;
1213 case 'ORGANIZATION':
1214 $identifier = addslashes($block['attrs']['IDENTIFIER']);
1215 $organization = '';
1216 $scoes->elements[$manifest][$organization][$identifier]->identifier = $identifier;
1217 $scoes->elements[$manifest][$organization][$identifier]->parent = '/';
1218 $scoes->elements[$manifest][$organization][$identifier]->launch = '';
1219 $scoes->elements[$manifest][$organization][$identifier]->scormtype = '';
1220
1221 $parents = array();
1222 $parent = new stdClass();
1223 $parent->identifier = $identifier;
1224 $parent->organization = $organization;
1225 array_push($parents, $parent);
1226 $organization = $identifier;
1227
1228 $scoes = scorm_get_manifest($block['children'],$scoes);
1229
1230 array_pop($parents);
1231 break;
1232 case 'ITEM':
1233 $parent = array_pop($parents);
1234 array_push($parents, $parent);
1235
1236 $identifier = addslashes($block['attrs']['IDENTIFIER']);
1237 $scoes->elements[$manifest][$organization][$identifier]->identifier = $identifier;
1238 $scoes->elements[$manifest][$organization][$identifier]->parent = $parent->identifier;
1239 if (!isset($block['attrs']['ISVISIBLE'])) {
1240 $block['attrs']['ISVISIBLE'] = 'true';
1241 }
1242 $scoes->elements[$manifest][$organization][$identifier]->isvisible = addslashes($block['attrs']['ISVISIBLE']);
1243 if (!isset($block['attrs']['PARAMETERS'])) {
1244 $block['attrs']['PARAMETERS'] = '';
1245 }
1246 $scoes->elements[$manifest][$organization][$identifier]->parameters = addslashes($block['attrs']['PARAMETERS']);
1247 if (!isset($block['attrs']['IDENTIFIERREF'])) {
1248 $scoes->elements[$manifest][$organization][$identifier]->launch = '';
1249 $scoes->elements[$manifest][$organization][$identifier]->scormtype = 'asset';
1250 } else {
1251 $idref = addslashes($block['attrs']['IDENTIFIERREF']);
1252 $base = '';
1253 if (isset($resources[$idref]['XML:BASE'])) {
1254 $base = $resources[$idref]['XML:BASE'];
1255 }
1256 $scoes->elements[$manifest][$organization][$identifier]->launch = addslashes($base.$resources[$idref]['HREF']);
1257 if (empty($resources[$idref]['ADLCP:SCORMTYPE'])) {
1258 $resources[$idref]['ADLCP:SCORMTYPE'] = 'asset';
1259 }
1260 $scoes->elements[$manifest][$organization][$identifier]->scormtype = addslashes($resources[$idref]['ADLCP:SCORMTYPE']);
1261 }
1262
e4aa175a 1263 $parent = new stdClass();
1264 $parent->identifier = $identifier;
1265 $parent->organization = $organization;
1266 array_push($parents, $parent);
1267
1268 $scoes = scorm_get_manifest($block['children'],$scoes);
1269
1270 array_pop($parents);
1271 break;
1272 case 'TITLE':
1273 $parent = array_pop($parents);
1274 array_push($parents, $parent);
f69db63e 1275 if (!isset($block['tagData'])) {
1276 $block['tagData'] = '';
1277 }
e4aa175a 1278 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->title = addslashes($block['tagData']);
1279 break;
1280 case 'ADLCP:PREREQUISITES':
1281 if ($block['attrs']['TYPE'] == 'aicc_script') {
1282 $parent = array_pop($parents);
1283 array_push($parents, $parent);
f69db63e 1284 if (!isset($block['tagData'])) {
1285 $block['tagData'] = '';
1286 }
e4aa175a 1287 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->prerequisites = addslashes($block['tagData']);
1288 }
1289 break;
1290 case 'ADLCP:MAXTIMEALLOWED':
1291 $parent = array_pop($parents);
1292 array_push($parents, $parent);
f69db63e 1293 if (!isset($block['tagData'])) {
1294 $block['tagData'] = '';
1295 }
e4aa175a 1296 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->maxtimeallowed = addslashes($block['tagData']);
1297 break;
1298 case 'ADLCP:TIMELIMITACTION':
1299 $parent = array_pop($parents);
1300 array_push($parents, $parent);
f69db63e 1301 if (!isset($block['tagData'])) {
1302 $block['tagData'] = '';
1303 }
e4aa175a 1304 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->timelimitaction = addslashes($block['tagData']);
1305 break;
1306 case 'ADLCP:DATAFROMLMS':
1307 $parent = array_pop($parents);
1308 array_push($parents, $parent);
f69db63e 1309 if (!isset($block['tagData'])) {
1310 $block['tagData'] = '';
1311 }
e4aa175a 1312 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->datafromlms = addslashes($block['tagData']);
1313 break;
1314 case 'ADLCP:MASTERYSCORE':
1315 $parent = array_pop($parents);
1316 array_push($parents, $parent);
f69db63e 1317 if (!isset($block['tagData'])) {
1318 $block['tagData'] = '';
1319 }
e4aa175a 1320 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->masteryscore = addslashes($block['tagData']);
1321 break;
f69db63e 1322 // Added by Pham Minh Duc
e4aa175a 1323 case 'ADLNAV:PRESENTATION':
1324 $parent = array_pop($parents);
1325 array_push($parents, $parent);
1326 foreach ($block['children'] as $adlnav) {
f69db63e 1327 if ($adlnav['name'] == 'ADLNAV:NAVIGATIONINTERFACE') {
1328 foreach ($adlnav['children'] as $adlnavInterface) {
1329 if ($adlnavInterface['name'] == 'ADLNAV:HIDELMSUI') {
1330 if ($adlnavInterface['tagData'] == 'continue') {
1331 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->next = 1;
e4aa175a 1332 }
f69db63e 1333 if ($adlnavInterface['tagData'] == 'previous') {
1334 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->previous = 1;
e4aa175a 1335 }
1336 }
1337
1338 }
e4aa175a 1339 }
1340 }
1341 break;
e4aa175a 1342 case 'IMSSS:SEQUENCING':
1343 $parent = array_pop($parents);
1344 array_push($parents, $parent);
1345 foreach ($block['children'] as $sequencing) {
f69db63e 1346 if ($sequencing['name']=='IMSSS:CONTROLMODE') {
1347 if ($sequencing['attrs']['CHOICE'] == 'false') {
1348 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->choice = 0;
e4aa175a 1349 }
f69db63e 1350 if ($sequencing['attrs']['CHOICEEXIT'] == 'false') {
1351 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->choiceexit = 0;
e4aa175a 1352 }
f69db63e 1353 if ($sequencing['attrs']['FLOW'] == 'true') {
1354 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->flow = 1;
e4aa175a 1355 }
f69db63e 1356 if ($sequencing['attrs']['FORWARDONLY'] == 'true') {
1357 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->forwardonly = 1;
e4aa175a 1358 }
f69db63e 1359 if ($sequencing['attrs']['USECURRENTATTEMPTOBJECTINFO'] == 'true') {
1360 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->usecurrentattemptobjectinfo = 1;
e4aa175a 1361 }
f69db63e 1362 if ($sequencing['attrs']['USECURRENTATTEMPTPROGRESSINFO'] == 'true') {
1363 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->usecurrentattemptprogressinfo = 1;
e4aa175a 1364 }
1365 }
f69db63e 1366 if ($sequencing['name']=='ADLSEQ:CONSTRAINEDCHOICECONSIDERATIONS') {
1367 if ($sequencing['attrs']['CONSTRAINCHOICE'] == 'true') {
1368 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->constrainChoice = 1;
e4aa175a 1369 }
f69db63e 1370 if ($sequencing['attrs']['PREVENTACTIVATION'] == 'true') {
1371 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->preventactivation = 1;
e4aa175a 1372 }
1373
1374 }
f69db63e 1375 if ($sequencing['name']=='IMSSS:OBJECTIVES') {
1376 foreach ($sequencing['children'] as $objective) {
1377 if ($objective['name']=='IMSSS:PRIMARYOBJECTIVE') {
1378 foreach ($objective['children'] as $primaryobjective) {
1379 if ($primaryobjective['name']=='IMSSS:MINNORMALIZEDMEASURE') {
1380 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->minnormalizedmeasure = $primaryobjective['tagData'];
e4aa175a 1381 }
1382 }
1383 }
1384 }
1385 }
f69db63e 1386 if ($sequencing['name']=='IMSSS:LIMITCONDITIONS') {
1387 if (!empty($sequencing['attrs']['ATTEMPTLIMIT'])) {
1388 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->attemptLimit = $sequencing['attrs']['ATTEMPTLIMIT'];
e4aa175a 1389 }
f69db63e 1390 if (!empty($sequencing['attrs']['ATTEMPTABSOLUTEDURATIONLIMIT'])) {
1391 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->attemptAbsoluteDurationLimit = $sequencing['attrs']['ATTEMPTABSOLUTEDURATIONLIMIT'];
e4aa175a 1392 }
1393 }
f69db63e 1394 if ($sequencing['name']=='IMSSS:ROLLUPRULES') {
e4aa175a 1395 $rolluprules = array();
f69db63e 1396 if (!empty($sequencing['attrs']['ROLLUPOBJECTIVESATISFIED'])) {
1397 if ($sequencing['attrs']['ROLLUPOBJECTIVESATISFIED']== 'false') {
1398 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rollupobjectivesatisfied = 0;
e4aa175a 1399 }
1400 }
f69db63e 1401 if (!empty($sequencing['attrs']['ROLLUPPROGRESSCOMPLETION'])) {
1402 if ($sequencing['attrs']['ROLLUPPROGRESSCOMPLETION']== 'false') {
1403 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rollupprogresscompletion = 0;
e4aa175a 1404 }
1405 }
f69db63e 1406 if (!empty($sequencing['attrs']['OBJECTIVEMEASUREWEIGHT'])) {
e4aa175a 1407 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->objectivemeasureweight = $sequencing['attrs']['OBJECTIVEMEASUREWEIGHT'];
1408 }
1409
1410 if (!empty($sequencing['children'])){
f69db63e 1411 foreach ($sequencing['children'] as $sequencingrolluprule) {
1412 if ($sequencingrolluprule['name']=='IMSSS:ROLLUPRULE' ) {
e4aa175a 1413 $rolluprule = new stdClass();
f69db63e 1414 if ($sequencingrolluprule['attrs']['CHILDACTIVITYSET'] !=' ') {
e4aa175a 1415 $rolluprule->childactivityset = $sequencingrolluprule['attrs']['CHILDACTIVITYSET'];
f69db63e 1416 if (!empty($sequencingrolluprule['children'])) {
1417 foreach ($sequencingrolluprule['children'] as $rolluproleconditions) {
1418 if ($rolluproleconditions['name']=='IMSSS:ROLLUPCONDITIONS') {
e4aa175a 1419 $conditions = array();
f69db63e 1420 if (!empty($rolluproleconditions['attrs']['conditionCombination'])) {
1421 $rolluprule->conditionCombination = $rolluproleconditions['attrs']['conditionCombination'];
e4aa175a 1422 }
f69db63e 1423 foreach ($rolluproleconditions['children'] as $rolluprulecondition) {
1424 if ($rolluprulecondition['name']=='IMSSS:ROLLUPCONDITION') {
e4aa175a 1425 $condition = new stdClass();
f69db63e 1426 if (!empty($rolluprulecondition['attrs']['OPERATOR'])) {
e4aa175a 1427 $condition->operator = $rolluprulecondition['attrs']['OPERATOR'];
1428 }
f69db63e 1429 if (!empty($rolluprulecondition['attrs']['CONDITION'])) {
e4aa175a 1430 $condition->condition = $rolluprulecondition['attrs']['CONDITION'];
1431 }
f69db63e 1432 array_push($conditions,$condition);
e4aa175a 1433 }
e4aa175a 1434 }
f69db63e 1435 $rolluprule->conditions = $conditions;
e4aa175a 1436 }
f69db63e 1437 if ($rolluproleconditions['name']=='IMSSS:ROLLUPACTION') {
1438 $rolluprule->rollupruleaction = $rolluproleconditions['attrs']['ACTION'];
e4aa175a 1439 }
1440 }
1441 }
e4aa175a 1442 }
e4aa175a 1443 array_push($rolluprules, $rolluprule);
e4aa175a 1444 }
e4aa175a 1445 }
1446 }
1447 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->rolluprules = $rolluprules;
e4aa175a 1448 }
e4aa175a 1449
f69db63e 1450 if ($sequencing['name']=='IMSSS:SEQUENCINGRULES') {
e4aa175a 1451 $sequencingrules = array();
f69db63e 1452 foreach ($sequencing['children'] as $conditionrules) {
1453 if ($conditionrules['name']=='IMSSS:EXITCONDITIONRULE') {
1454 $sequencingrule = new stdClass();
1455 if (!empty($conditionrules['children'])) {
1456 foreach ($conditionrules['children'] as $conditionrule) {
1457 if ($conditionrule['name']=='IMSSS:RULECONDITIONS') {
1458 $ruleconditions = array();
1459 if (!empty($conditionrule['attrs']['conditionCombination'])) {
e4aa175a 1460 $sequencingrule->conditionCombination = $conditionrule['attrs']['conditionCombination'];
f69db63e 1461 }
1462 foreach ($conditionrule['children'] as $rulecondition) {
1463 if ($rulecondition['name']=='IMSSS:RULECONDITION') {
1464 $condition = new stdClass();
1465 if (!empty($rulecondition['attrs']['OPERATOR'])) {
1466 $condition->operator = $rulecondition['attrs']['OPERATOR'];
e4aa175a 1467 }
f69db63e 1468 if (!empty($rulecondition['attrs']['CONDITION'])) {
1469 $condition->condition = $rulecondition['attrs']['CONDITION'];
1470 }
1471 if (!empty($rulecondition['attrs']['MEASURETHRESHOLD'])) {
1472 $condition->measurethreshold = $rulecondition['attrs']['MEASURETHRESHOLD'];
1473 }
1474 if (!empty($rulecondition['attrs']['REFERENCEDOBJECTIVE'])) {
1475 $condition->referencedobjective = $rulecondition['attrs']['REFERENCEDOBJECTIVE'];
1476 }
1477 array_push($ruleconditions,$condition);
e4aa175a 1478 }
e4aa175a 1479 }
f69db63e 1480 $sequencingrule->ruleconditions = $ruleconditions;
1481 }
1482 if ($conditionrule['name']=='IMSSS:RULEACTION') {
e4aa175a 1483 $sequencingrule->exitconditionruleaction = $conditionrule['attrs']['ACTION'];
f69db63e 1484 }
1485 }
1486 }
1487 array_push($sequencingrules,$sequencingrule);
1488 }
1489 if ($conditionrules['name']=='IMSSS:PRECONDITIONRULE') {
1490 $sequencingrule = new stdClass();
1491 if (!empty($conditionrules['children'])) {
1492 foreach ($conditionrules['children'] as $conditionrule) {
1493 if ($conditionrule['name']=='IMSSS:RULECONDITIONS') {
1494 $ruleconditions = array();
1495 if (!empty($conditionrule['attrs']['conditionCombination'])) {
1496 $sequencingrule->conditionCombination = $conditionrule['attrs']['conditionCombination'];
1497 }
1498 foreach ($conditionrule['children'] as $rulecondition) {
1499 if ($rulecondition['name']=='IMSSS:RULECONDITION') {
1500 $condition = new stdClass();
1501 if (!empty($rulecondition['attrs']['OPERATOR'])) {
1502 $condition->operator = $rulecondition['attrs']['OPERATOR'];
1503 }
1504 if (!empty($rulecondition['attrs']['CONDITION'])) {
1505 $condition->condition = $rulecondition['attrs']['CONDITION'];
1506 }
1507 if (!empty($rulecondition['attrs']['MEASURETHRESHOLD'])) {
1508 $condition->measurethreshold = $rulecondition['attrs']['MEASURETHRESHOLD'];
1509 }
1510 if (!empty($rulecondition['attrs']['REFERENCEDOBJECTIVE'])) {
1511 $condition->referencedobjective = $rulecondition['attrs']['REFERENCEDOBJECTIVE'];
1512 }
1513 array_push($ruleconditions,$condition);
1514 }
e4aa175a 1515 }
f69db63e 1516 $sequencingrule->ruleconditions = $ruleconditions;
1517 }
1518 if ($conditionrule['name']=='IMSSS:RULEACTION') {
1519 $sequencingrule->preconditionruleaction = $conditionrule['attrs']['ACTION'];
e4aa175a 1520 }
1521 }
f69db63e 1522 }
1523 array_push($sequencingrules,$sequencingrule);
e4aa175a 1524 }
f69db63e 1525 if ($conditionrules['name']=='IMSSS:POSTCONDITIONRULE') {
e4aa175a 1526 $sequencingrule = new stdClass();
f69db63e 1527 if (!empty($conditionrules['children'])) {
1528 foreach ($conditionrules['children'] as $conditionrule) {
e4aa175a 1529 if ($conditionrule['name']=='IMSSS:RULECONDITIONS'){
1530 $ruleconditions = array();
1531 if (!empty($conditionrule['attrs']['conditionCombination'])){
1532 $sequencingrule->conditionCombination = $conditionrule['attrs']['conditionCombination'];
1533 }
1534 foreach ($conditionrule['children'] as $rulecondition){
1535 if ($rulecondition['name']=='IMSSS:RULECONDITION'){
1536 $condition = new stdClass();
1537 if (!empty($rulecondition['attrs']['OPERATOR'])){
1538 $condition->operator = $rulecondition['attrs']['OPERATOR'];
1539 }
1540 if (!empty($rulecondition['attrs']['CONDITION'])){
1541 $condition->condition = $rulecondition['attrs']['CONDITION'];
1542 }
1543 if (!empty($rulecondition['attrs']['MEASURETHRESHOLD'])){
1544 $condition->measurethreshold = $rulecondition['attrs']['MEASURETHRESHOLD'];
1545 }
1546 if (!empty($rulecondition['attrs']['REFERENCEDOBJECTIVE'])){
1547 $condition->referencedobjective = $rulecondition['attrs']['REFERENCEDOBJECTIVE'];
f69db63e 1548 }
1549 array_push($ruleconditions,$condition);
e4aa175a 1550 }
e4aa175a 1551 }
f69db63e 1552 $sequencingrule->ruleconditions = $ruleconditions;
e4aa175a 1553 }
1554 if ($conditionrule['name']=='IMSSS:RULEACTION'){
e4aa175a 1555 $sequencingrule->postconditionruleaction = $conditionrule['attrs']['ACTION'];
e4aa175a 1556 }
1557 }
f69db63e 1558 }
1559 array_push($sequencingrules,$sequencingrule);
e4aa175a 1560 }
f69db63e 1561 $scoes->elements[$manifest][$parent->organization][$parent->identifier]->sequencingrules = $sequencingrules;
e4aa175a 1562 }
1563 }
1564 }
e4aa175a 1565 break;
f69db63e 1566 // End Add
e4aa175a 1567 }
1568 }
1569 }
e4aa175a 1570 return $scoes;
1571}
1572
1573function scorm_parse_scorm($pkgdir,$scormid) {
1574 global $CFG;
e4aa175a 1575
1576 $launch = 0;
1577 $manifestfile = $pkgdir.'/imsmanifest.xml';
1578
1579 if (is_file($manifestfile)) {
1580
1581 $xmlstring = file_get_contents($manifestfile);
1582 $objXML = new xml2Array();
1583 $manifests = $objXML->parse($xmlstring);
1584
1585 $scoes = new stdClass();
1586 $scoes->version = '';
1587 $scoes = scorm_get_manifest($manifests,$scoes);
1588
1589 if (count($scoes->elements) > 0) {
1590 foreach ($scoes->elements as $manifest => $organizations) {
1591 foreach ($organizations as $organization => $items) {
1592 foreach ($items as $identifier => $item) {
1593 $item->scorm = $scormid;
1594 $item->manifest = $manifest;
1595 $item->organization = $organization;
e4aa175a 1596 $id = insert_record('scorm_scoes',$item);
f69db63e 1597 // Added by Pham Minh Duc
e4aa175a 1598 $item->scormid = $scormid;
1599 $item->scoid = $id;
1600 $idControlMode = insert_record('scorm_sequencing_controlmode',$item);
1601
f69db63e 1602 if (!empty($item->sequencingrules)) {
1603 foreach($item->sequencingrules as $sequencingrule) {
e4aa175a 1604 $sequencingrule->scormid = $scormid;
1605 $sequencingrule->scoid = $item->scoid;
e4aa175a 1606 $idruleconditions = insert_record('scorm_sequencing_ruleconditions',$sequencingrule);
f69db63e 1607 foreach($sequencingrule->ruleconditions as $rulecondition) {
e4aa175a 1608 $rulecondition->scormid = $sequencingrule->scormid;
1609 $rulecondition->scoid = $sequencingrule->scoid;
1610 $rulecondition->ruleconditionsid = $idruleconditions;
1611 $idrulecondition = insert_record('scorm_sequencing_rulecondition',$rulecondition);
e4aa175a 1612 }
e4aa175a 1613 }
1614 }
1615
f69db63e 1616 if (!empty($item->rolluprules)) {
e4aa175a 1617 $idControlMode = insert_record('scorm_sequencing_rolluprules',$item);
f69db63e 1618 foreach($item->rolluprules as $rollup) {
e4aa175a 1619 $rollup->rolluprulesid =$idControlMode;
1620 $rollup->scormid = $scormid;
1621 $rollup->scoid = $item->scoid;
1622
e4aa175a 1623 $idRollupRule = insert_record('scorm_sequencing_rolluprule',$rollup);
e4aa175a 1624 $rollup->rollupruleid = $idRollupRule;
1625 $idconditions = insert_record('scorm_sequencing_rollupruleconditions',$rollup);
e4aa175a 1626 foreach($rollup->conditions as $condition){
1627 $condition->ruleconditionsid = $idconditions;
1628 $condition->scormid = $rollup->scormid;
1629 $condition->scoid = $rollup->scoid;
1630 $idcondition = insert_record('scorm_sequencing_rolluprulecondition',$condition);
e4aa175a 1631 }
e4aa175a 1632 }
1633 }
f69db63e 1634 // End Add
e4aa175a 1635 if (($launch == 0) && ((empty($scoes->defaultorg)) || ($scoes->defaultorg == $identifier))) {
1636 $launch = $id;
1637 }
1638 }
1639 }
1640 }
1641 set_field('scorm','version',$scoes->version,'id',$scormid);
1642 }
1643 }
1644
1645 return $launch;
1646}
1647
1648function scorm_course_format_display($user,$course) {
1649 global $CFG;
1650
1651 $strupdate = get_string('update');
1652 $strmodule = get_string('modulename','scorm');
1653
1654 echo '<div class="mod-scorm">';
1655 if ($scorms = get_all_instances_in_course('scorm', $course)) {
1656 // The module SCORM activity with the least id is the course
1657 $scorm = current($scorms);
1658 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
1659 error("Course Module ID was incorrect");
1660 }
1661 $colspan = '';
1662 $headertext = '<table width="100%"><tr><td class="title">'.get_string('name').': <b>'.format_string($scorm->name).'</b>';
1663 if (isteacher($course->id, $user->id, true)) {
1664 if (isediting($course->id)) {
1665 // Display update icon
1666 $path = $CFG->wwwroot.'/course';
1667 $headertext .= '<span class="commands">'.
1668 '<a title="'.$strupdate.'" href="'.$path.'/mod.php?update='.$cm->id.'&amp;sesskey='.sesskey().'">'.
1669 '<img src="'.$CFG->pixpath.'/t/edit.gif" hspace="2" height="11" width="11" border="0" alt="'.$strupdate.'" /></a></span>';
1670 }
1671 $headertext .= '</td>';
1672 // Display report link
1673 $trackedusers = get_record('scorm_scoes_track', 'scormid', $scorm->id, '', '', '', '', 'count(distinct(userid)) as c');
1674 if ($trackedusers->c > 0) {
1675 $headertext .= '<td class="reportlink">'.
1676 '<a target="'.$CFG->framename.'" href="'.$CFG->wwwroot.'/mod/scorm/report.php?id='.$cm->id.'">'.
1677 get_string('viewallreports','scorm',$trackedusers->c).'</a>';
1678 } else {
1679 $headertext .= '<td class="reportlink">'.get_string('noreports','scorm');
1680 }
1681 $colspan = ' colspan="2"';
1682 }
1683 $headertext .= '</td></tr><tr><td'.$colspan.'>'.format_text(get_string('summary').':<br />'.$scorm->summary).'</td></tr></table>';
1684 print_simple_box($headertext,'','100%');
1685 scorm_view_display($user, $scorm, 'view.php?id='.$course->id, $cm, '100%');
1686 } else {
1687 if (isteacheredit($course->id, $user->id)) {
1688 // Create a new activity
1689 redirect('mod.php?id='.$course->id.'&amp;section=0&sesskey='.sesskey().'&amp;add=scorm');
1690 } else {
1691 notify('Could not find a scorm course here');
1692 }
1693 }
1694 echo '</div>';
1695}
1696
1697function scorm_view_display ($user, $scorm, $action, $cm, $blockwidth='') {
1698 global $CFG;
1699 $organization = optional_param('organization', '', PARAM_INT);
1700
1701 print_simple_box_start('center',$blockwidth);
1702?>
1703 <div class="structurehead"><?php print_string('coursestruct','scorm') ?></div>
1704<?php
1705 if (empty($organization)) {
1706 $organization = $scorm->launch;
1707 }
1708 if ($orgs = get_records_select_menu('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,title')) {
1709 if (count($orgs) > 1) {
1710 ?>
1711 <div class='center'>
1712 <?php print_string('organizations','scorm') ?>
1713 <form name='changeorg' method='post' action='<?php echo $action ?>'>
1714 <?php choose_from_menu($orgs, 'organization', "$organization", '','submit()') ?>
1715 </form>
1716 </div>
1717<?php
1718 }
1719 }
1720 $orgidentifier = '';
1721 if ($org = get_record('scorm_scoes','id',$organization)) {
1722 if (($org->organization == '') && ($org->launch == '')) {
1723 $orgidentifier = $org->identifier;
1724 } else {
1725 $orgidentifier = $org->organization;
1726 }
1727 }
1728 $result = scorm_get_toc($user,$scorm,'structlist',$orgidentifier);
1729 $incomplete = $result->incomplete;
e4aa175a 1730 echo $result->toc;
e4aa175a 1731 print_simple_box_end();
1732?>
1733 <div class="center">
1734 <form name="theform" method="post" action="<?php echo $CFG->wwwroot ?>/mod/scorm/player.php?id=<?php echo $cm->id ?>"<?php echo $scorm->popup == 1?' target="newwin"':'' ?>>
1735 <?php
f69db63e 1736 // Added by Pham Minh Duc
1737 $suspend = get_record("scorm_suspendtrack","scormid",$scorm->id,"userid",$user->id);
1738 // End Add
e4aa175a 1739
e4aa175a 1740 if ($scorm->hidebrowse == 0) {
1741 print_string("mode","scorm");
1742 echo ': <input type="radio" id="b" name="mode" value="browse" /><label for="b">'.get_string('browse','scorm').'</label>'."\n";
1743 if ($incomplete === true) {
1744 echo '<input type="radio" id="n" name="mode" value="normal" checked="checked" /><label for="n">'.get_string('normal','scorm')."</label>\n";
f69db63e 1745 // Added by Pham Minh Duc
1746 if (!empty($suspend)) {
e4aa175a 1747 echo '<input type="radio" id="n" name="mode" value="continue" checked="checked" /><label for="n">'.get_string('continue','scorm')."</label>\n";
1748 }
f69db63e 1749 // End Add
e4aa175a 1750 } else {
1751 echo '<input type="radio" id="r" name="mode" value="review" checked="checked" /><label for="r">'.get_string('review','scorm')."</label>\n";
1752 }
1753 } else {
1754 if ($incomplete === true) {
1755 echo '<input type="hidden" name="mode" value="normal" />'."\n";
1756 } else {
1757 echo '<input type="hidden" name="mode" value="review" />'."\n";
1758 }
1759 }
1760 if (($incomplete === false) && (($result->attemptleft > 0)||($scorm->maxattempt == 0))) {
1761?>
1762 <br />
1763 <input type="checkbox" id="a" name="newattempt" />
1764 <label for="a"><?php print_string('newattempt','scorm') ?></label>
1765<?php
1766 }
1767 ?>
1768 <br />
1769 <input type="hidden" name="scoid" />
1770 <input type="hidden" name="currentorg" value="<?php echo $orgidentifier ?>" />
1771 <input type="submit" value="<?php print_string('entercourse','scorm') ?>" />
1772 </form>
1773 </div>
1774<?php
1775}
1776
e4aa175a 1777function scorm_repeater($what, $times) {
1778 if ($times <= 0) {
1779 return null;
1780 }
1781 $return = '';
1782 for ($i=0; $i<$times;$i++) {
1783 $return .= $what;
1784 }
1785 return $return;
1786}
1787
e4aa175a 1788/* Usage
1789 Grab some XML data, either from a file, URL, etc. however you want. Assume storage in $strYourXML;
1790
1791 $objXML = new xml2Array();
1792 $arrOutput = $objXML->parse($strYourXML);
1793 print_r($arrOutput); //print it out, or do whatever!
1794
1795*/
1796class xml2Array {
1797
1798 var $arrOutput = array();
1799 var $resParser;
1800 var $strXmlData;
1801
1802 /**
1803 * Convert a utf-8 string to html entities
1804 *
1805 * @param string $str The UTF-8 string
1806 * @return string
1807 */
1808 function utf8_to_entities($str) {
f69db63e 1809 global $CFG;
1810
e4aa175a 1811 $entities = '';
1812 $values = array();
1813 $lookingfor = 1;
1814
f69db63e 1815 if (empty($CFG->unicodedb)) { // If Unicode DB support enable does not convert string
1816 $textlib = textlib_get_instance();
1817 for ($i = 0; $i < $textlib->strlen($str,'utf-8'); $i++) {
1818 $thisvalue = ord($str[$i]);
1819 if ($thisvalue < 128) {
1820 $entities .= $str[$i]; // Leave ASCII chars unchanged
1821 } else {
1822 if (count($values) == 0) {
1823 $lookingfor = ($thisvalue < 224) ? 2 : 3;
1824 }
1825 $values[] = $thisvalue;
1826 if (count($values) == $lookingfor) {
1827 $number = ($lookingfor == 3) ?
1828 (($values[0] % 16) * 4096) + (($values[1] % 64) * 64) + ($values[2] % 64):
1829 (($values[0] % 32) * 64) + ($values[1] % 64);
1830 $entities .= '&#' . $number . ';';
1831 $values = array();
1832 $lookingfor = 1;
1833 }
e4aa175a 1834 }
1835 }
f69db63e 1836 return $entities;
1837 } else {
1838 return $str;
e4aa175a 1839 }
e4aa175a 1840 }
1841
1842 /**
1843 * Parse an XML text string and create an array tree that rapresent the XML structure
1844 *
1845 * @param string $strInputXML The XML string
1846 * @return array
1847 */
1848 function parse($strInputXML) {
1849 $this->resParser = xml_parser_create ('UTF-8');
1850 xml_set_object($this->resParser,$this);
1851 xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
1852
1853 xml_set_character_data_handler($this->resParser, "tagData");
1854
1855 $this->strXmlData = xml_parse($this->resParser,$strInputXML );
1856 if(!$this->strXmlData) {
1857 die(sprintf("XML error: %s at line %d",
1858 xml_error_string(xml_get_error_code($this->resParser)),
1859 xml_get_current_line_number($this->resParser)));
1860 }
1861
1862 xml_parser_free($this->resParser);
1863
1864 return $this->arrOutput;
1865 }
1866
1867 function tagOpen($parser, $name, $attrs) {
1868 $tag=array("name"=>$name,"attrs"=>$attrs);
1869 array_push($this->arrOutput,$tag);
1870 }
1871
1872 function tagData($parser, $tagData) {
1873 if(trim($tagData)) {
1874 if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
1875 $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $this->utf8_to_entities($tagData);
1876 } else {
1877 $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $this->utf8_to_entities($tagData);
1878 }
1879 }
1880 }
1881
1882 function tagClosed($parser, $name) {
1883 $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
1884 array_pop($this->arrOutput);
1885 }
1886
1887}
1888?>