"MDL-12304, fix double text"
[moodle-linuxchix.git] / mod / scorm / datamodels / aicclib.php
blob1753e1d78c91a4bc815f19d387e7c60330c76e2a
1 <?php // $Id$
2 function 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;
9 $acents = 0; //Cents
10 if (count($aseconds) > 1) {
11 $acents = $aseconds[1];
13 $bcents = 0;
14 if (count($bseconds) > 1) {
15 $bcents = $bseconds[1];
17 $cents = $acents + $bcents;
18 $change = floor($cents / 100);
19 $cents = $cents - ($change * 100);
20 if (floor($cents) < 10) {
21 $cents = '0'. $cents;
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;
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;
38 $hours = $aes[0] + $bes[0] + $change; //Hours
39 if ($hours < 10) {
40 $hours = '0' . $hours;
43 if ($cents != '0') {
44 return $hours . ":" . $mins . ":" . $secs . '.' . $cents;
45 } else {
46 return $hours . ":" . $mins . ":" . $secs;
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.
55 * @param string $row AICC header row
56 * @param string $mastername AICC sco identifier column
57 * @return mixed
59 function 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;
69 $i++;
71 $tok = strtok("\",\n\r");
73 return $result;
76 /**
77 * Given a colums array return a string containing the regular
78 * expression to match the columns in a text row.
80 * @param array $column The header columns
81 * @param string $remodule The regular expression module for a single column
82 * @return string
84 function scorm_forge_cols_regexp($columns,$remodule='(".*")?,') {
85 $regexp = '/^';
86 foreach ($columns as $column) {
87 $regexp .= $remodule;
89 $regexp = substr($regexp,0,-1) . '/';
90 return $regexp;
93 function 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;
109 closedir($handle);
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;
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);
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);
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);
172 if (isset($id->ort)) {
173 $rows = file($pkgdir.'/'.$id->ort);
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);
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);
196 if (isset($id->cmp)) {
197 $rows = file($pkgdir.'/'.$id->cmp);
200 //print_r($courses);
202 $oldscoes = get_records('scorm_scoes','scorm',$scormid);
204 $launch = 0;
205 if (isset($courses)) {
206 foreach ($courses as $course) {
207 $sco = new object();
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 = '';
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);
224 if ($launch == 0) {
225 $launch = $id;
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;
235 if (!isset($element->parent) || strtolower($element->parent) == 'root') {
236 $sco->parent = '/';
237 } else {
238 $sco->parent = $element->parent;
240 if (isset($element->file_name)) {
241 $sco->launch = $element->file_name;
242 $sco->scormtype = 'sco';
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);
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);
262 if (isset($element->prerequisites)) {
263 $scodata->name = 'prerequisites';
264 $scodata->value = $element->prerequisites;
265 $dataid = insert_record('scorm_scoes_data',$scodata);
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);
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);
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);
283 if ($launch==0) {
284 $launch = $id;
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);
297 set_field('scorm','version','AICC','id',$scormid);
298 return $launch;
301 function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
302 global $CFG;
304 $strexpand = get_string('expcoll','scorm');
305 $modestr = '';
306 if ($mode == 'browse') {
307 $modestr = '&amp;mode='.$mode;
309 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
311 $result = new stdClass();
312 $result->toc = "<ul id='s0' class='$liststyle'>\n";
313 $tocmenus = array();
314 $result->prerequisites = true;
315 $incomplete = false;
318 // Get the current organization infos
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;
326 $organizationsql = "AND organization='$currentorg'";
329 // If not specified retrieve the last attempt number
331 if (empty($attempt)) {
332 $attempt = scorm_get_last_attempt($scorm->id, $user->id);
334 $result->attemptleft = $scorm->maxattempt - $attempt;
335 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
336 // drop keys so that we can access array sequentially
337 $scoes = array_values($scoes);
339 // Retrieve user tracking data for each learning object
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';
348 $usertracks[$sco->identifier] = $usertrack;
353 $level=0;
354 $sublist=1;
355 $previd = 0;
356 $nextid = 0;
357 $findnext = false;
358 $parents[$level]='/';
360 foreach ($scoes as $pos=>$sco) {
361 $isvisible = false;
362 $sco->title = stripslashes($sco->title);
363 if ($optionaldatas = scorm_get_sco($sco->id, SCO_DATA)) {
364 if (!isset($optionaldatas->isvisible) || (isset($optionaldatas->isvisible) && ($optionaldatas->isvisible == 'true'))) {
365 $isvisible = true;
368 else
369 $isvisible = true;
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";
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--;
383 if (($i == 0) && ($sco->parent != $currentorg)) {
384 $style = '';
385 if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
386 $style = ' style="display: none;"';
388 $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
389 $level++;
390 } else {
391 $result->toc .= $closelist;
392 $level = $i;
394 $parents[$level]=$sco->parent;
397 if ($isvisible) {
398 $result->toc .= "\t\t<li>";
400 if (isset($scoes[$pos+1])) {
401 $nextsco = $scoes[$pos+1];
402 } else {
403 $nextsco = false;
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;
411 if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
412 $sublist++;
413 $icon = 'minus';
414 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
415 $icon = 'plus';
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) {
419 $result->toc .= '<img src="'.$scormpixdir.'/spacer.gif" />';
421 if (empty($sco->title)) {
422 $sco->title = $sco->identifier;
424 if (!empty($sco->launch)) {
425 if ($isvisible) {
426 $startbold = '';
427 $endbold = '';
428 $score = '';
429 if (empty($scoid) && ($mode != 'normal')) {
430 $scoid = $sco->id;
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').'" />';
441 if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
442 $incomplete = true;
443 if ($play && empty($scoid)) {
444 $scoid = $sco->id;
447 if ($usertrack->score_raw != '') {
448 $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
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.'" />';
454 } else {
455 if ($play && empty($scoid)) {
456 $scoid = $sco->id;
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').'" />';
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;
474 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
475 if (!empty($sco->launch)) {
476 $previd = $sco->id;
479 if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
480 if ($sco->id == $scoid) {
481 $result->prerequisites = true;
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;
490 $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."</li>\n";
493 } else {
494 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
496 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
497 if (!empty($nextsco->launch)) {
498 $nextid = $nextsco->id;
502 for ($i=0;$i<$level;$i++) {
503 $result->toc .= "\t\t</ul></li>\n";
506 if ($play) {
507 $sco = scorm_get_sco($scoid);
508 $sco->previd = $previd;
509 $sco->nextid = $nextid;
510 $result->sco = $sco;
511 $result->incomplete = $incomplete;
512 } else {
513 $result->incomplete = $incomplete;
516 $result->toc .= "\t</ul>\n";
517 if ($scorm->hidetoc == 0) {
518 $result->toc .= '
519 <script type="text/javascript">
520 //<![CDATA[
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";
526 which = which.substring(0,(which.length));
527 if (eval(beg+list+mid+".display") != "none") {
528 document.getElementById(which).src = "'.$scormpixdir.'/plus.gif";
529 eval(beg+list+mid+".display=\'none\';");
530 new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
531 } else {
532 document.getElementById(which).src = "'.$scormpixdir.'/minus.gif";
533 eval(beg+list+mid+".display=\'block\';");
534 new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
537 //]]>
538 </script>'."\n";
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);
544 return $result;