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