Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / scorm / datamodels / aicclib.php
blob2a3839305c9f9cf214420caace923b9006db850c
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);
282 if (isset($element->core_vendor)) {
283 $scodata->name = 'datafromlms';
284 $scodata->value = eregi_replace('<cr>', "\r\n", $element->core_vendor);
285 $dataid = insert_record('scorm_scoes_data',$scodata);
288 if ($launch==0) {
289 $launch = $id;
296 if (!empty($oldscoes)) {
297 foreach($oldscoes as $oldsco) {
298 delete_records('scorm_scoes','id',$oldsco->id);
299 delete_records('scorm_scoes_track','scoid',$oldsco->id);
302 set_field('scorm','version','AICC','id',$scormid);
303 return $launch;
306 function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
307 global $CFG;
309 $strexpand = get_string('expcoll','scorm');
310 $modestr = '';
311 if ($mode == 'browse') {
312 $modestr = '&amp;mode='.$mode;
314 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
316 $result = new stdClass();
317 $result->toc = "<ul id='s0' class='$liststyle'>\n";
318 $tocmenus = array();
319 $result->prerequisites = true;
320 $incomplete = false;
323 // Get the current organization infos
325 if (!empty($currentorg)) {
326 if (($organizationtitle = get_field('scorm_scoes','title','scorm',$scorm->id,'identifier',$currentorg)) != '') {
327 $result->toc .= "\t<li>$organizationtitle</li>\n";
328 $tocmenus[] = $organizationtitle;
332 // If not specified retrieve the last attempt number
334 if (empty($attempt)) {
335 $attempt = scorm_get_last_attempt($scorm->id, $user->id);
337 $result->attemptleft = $scorm->maxattempt - $attempt;
338 if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
340 // Retrieve user tracking data for each learning object
342 $usertracks = array();
343 foreach ($scoes as $sco) {
344 if (!empty($sco->launch)) {
345 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
346 if ($usertrack->status == '') {
347 $usertrack->status = 'notattempted';
349 $usertracks[$sco->identifier] = $usertrack;
354 $level=0;
355 $sublist=1;
356 $previd = 0;
357 $nextid = 0;
358 $findnext = false;
359 $parents[$level]='/';
361 foreach ($scoes as $pos => $sco) {
362 $isvisible = false;
363 $sco->title = stripslashes($sco->title);
364 if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
365 $isvisible = true;
367 if ($parents[$level]!=$sco->parent) {
368 if ($newlevel = array_search($sco->parent,$parents)) {
369 for ($i=0; $i<($level-$newlevel); $i++) {
370 $result->toc .= "\t\t</ul></li>\n";
372 $level = $newlevel;
373 } else {
374 $i = $level;
375 $closelist = '';
376 while (($i > 0) && ($parents[$level] != $sco->parent)) {
377 $closelist .= "\t\t</ul></li>\n";
378 $i--;
380 if (($i == 0) && ($sco->parent != $currentorg)) {
381 $style = '';
382 if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
383 $style = ' style="display: none;"';
385 $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
386 $level++;
387 } else {
388 $result->toc .= $closelist;
389 $level = $i;
391 $parents[$level]=$sco->parent;
394 if ($isvisible) {
395 $result->toc .= "\t\t<li>";
397 if (isset($scoes[$pos+1])) {
398 $nextsco = $scoes[$pos+1];
399 } else {
400 $nextsco = false;
402 $nextisvisible = false;
403 if (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true'))) {
404 $nextisvisible = true;
406 if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
407 $sublist++;
408 $icon = 'minus';
409 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
410 $icon = 'plus';
412 $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>';
413 } else if ($isvisible) {
414 $result->toc .= '<img src="'.$scormpixdir.'/spacer.gif" />';
416 if (empty($sco->title)) {
417 $sco->title = $sco->identifier;
419 if (!empty($sco->launch)) {
420 if ($isvisible) {
421 $startbold = '';
422 $endbold = '';
423 $score = '';
424 if (empty($scoid) && ($mode != 'normal')) {
425 $scoid = $sco->id;
427 if (isset($usertracks[$sco->identifier])) {
428 $usertrack = $usertracks[$sco->identifier];
429 $strstatus = get_string($usertrack->status,'scorm');
430 if ($sco->scormtype == 'sco') {
431 $statusicon = '<img src="'.$scormpixdir.'/'.$usertrack->status.'.gif" alt="'.$strstatus.'" title="'.$strstatus.'" />';
432 } else {
433 $statusicon = '<img src="'.$scormpixdir.'/assetc.gif" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
436 if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
437 $incomplete = true;
438 if ($play && empty($scoid)) {
439 $scoid = $sco->id;
442 if ($usertrack->score_raw != '') {
443 $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
445 $strsuspended = get_string('suspended','scorm');
446 if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
447 $statusicon = '<img src="'.$scormpixdir.'/suspend.gif" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
449 } else {
450 if ($play && empty($scoid)) {
451 $scoid = $sco->id;
453 $incomplete = true;
454 if ($sco->scormtype == 'sco') {
455 $statusicon = '<img src="'.$scormpixdir.'/notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
456 } else {
457 $statusicon = '<img src="'.$scormpixdir.'/asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
460 if ($sco->id == $scoid) {
461 $startbold = '<b>';
462 $endbold = '</b>';
463 $findnext = true;
464 $shownext = isset($sco->next) ? $sco->next : 0;
465 $showprev = isset($sco->previous) ? $sco->previous : 0;
468 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
469 if (!empty($sco->launch)) {
470 $previd = $sco->id;
473 if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
474 if ($sco->id == $scoid) {
475 $result->prerequisites = true;
477 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
478 $result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
479 $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
480 } else {
481 if ($sco->id == $scoid) {
482 $result->prerequisites = false;
484 $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."</li>\n";
487 } else {
488 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
490 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
491 if (!empty($nextsco->launch)) {
492 $nextid = $nextsco->id;
496 for ($i=0;$i<$level;$i++) {
497 $result->toc .= "\t\t</ul></li>\n";
500 if ($play) {
501 $sco = scorm_get_sco($scoid);
502 $sco->previd = $previd;
503 $sco->nextid = $nextid;
504 $result->sco = $sco;
505 $result->incomplete = $incomplete;
506 } else {
507 $result->incomplete = $incomplete;
510 $result->toc .= "\t</ul>\n";
511 if ($scorm->hidetoc == 0) {
512 $result->toc .= '
513 <script type="text/javascript">
514 //<![CDATA[
515 function expandCollide(which,list,item) {
516 var el = document.ids ? document.ids[list] : document.getElementById ? document.getElementById(list) : document.all[list];
517 which = which.substring(0,(which.length));
518 var el2 = document.ids ? document.ids[which] : document.getElementById ? document.getElementById(which) : document.all[which];
519 if (el.style.display != "none") {
520 el2.src = "'.$scormpixdir.'/plus.gif";
521 el.style.display=\'none\';
522 new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
523 } else {
524 el2.src = "'.$scormpixdir.'/minus.gif";
525 el.style.display=\'block\';
526 new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
529 //]]>
530 </script>'."\n";
533 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid=';
534 $result->tocmenu = popup_form($url,$tocmenus, "tocmenu", $sco->id, '', '', '', true);
536 return $result;