moodle_database conversion error.
[moodle.git] / mod / scorm / datamodels / aicclib.php
CommitLineData
f4ba7e1a 1<?php // $Id$
dc383b6f 2function scorm_add_time($a, $b) {
3 $aes = explode(':',$a);
4 $bes = explode(':',$b);
5 $aseconds = explode('.',$aes[2]);
6 $bseconds = explode('.',$bes[2]);
7 $change = 0;
8
9 $acents = 0; //Cents
10 if (count($aseconds) > 1) {
11 $acents = $aseconds[1];
12 }
13 $bcents = 0;
14 if (count($bseconds) > 1) {
15 $bcents = $bseconds[1];
16 }
17 $cents = $acents + $bcents;
18 $change = floor($cents / 100);
19 $cents = $cents - ($change * 100);
20 if (floor($cents) < 10) {
21 $cents = '0'. $cents;
22 }
23
24 $secs = $aseconds[0] + $bseconds[0] + $change; //Seconds
25 $change = floor($secs / 60);
26 $secs = $secs - ($change * 60);
27 if (floor($secs) < 10) {
28 $secs = '0'. $secs;
29 }
30
31 $mins = $aes[1] + $bes[1] + $change; //Minutes
32 $change = floor($mins / 60);
33 $mins = $mins - ($change * 60);
34 if ($mins < 10) {
35 $mins = '0' . $mins;
36 }
37
38 $hours = $aes[0] + $bes[0] + $change; //Hours
39 if ($hours < 10) {
40 $hours = '0' . $hours;
41 }
42
43 if ($cents != '0') {
44 return $hours . ":" . $mins . ":" . $secs . '.' . $cents;
45 } else {
46 return $hours . ":" . $mins . ":" . $secs;
47 }
48}
49
50/**
51* Take the header row of an AICC definition file
52* and returns sequence of columns and a pointer to
53* the sco identifier column.
54*
55* @param string $row AICC header row
56* @param string $mastername AICC sco identifier column
57* @return mixed
58*/
59function scorm_get_aicc_columns($row,$mastername='system_id') {
60 $tok = strtok(strtolower($row),"\",\n\r");
61 $result->columns = array();
62 $i=0;
63 while ($tok) {
64 if ($tok !='') {
65 $result->columns[] = $tok;
66 if ($tok == $mastername) {
67 $result->mastercol = $i;
68 }
69 $i++;
70 }
71 $tok = strtok("\",\n\r");
72 }
73 return $result;
74}
75
76/**
77* Given a colums array return a string containing the regular
78* expression to match the columns in a text row.
79*
80* @param array $column The header columns
81* @param string $remodule The regular expression module for a single column
82* @return string
83*/
84function scorm_forge_cols_regexp($columns,$remodule='(".*")?,') {
85 $regexp = '/^';
86 foreach ($columns as $column) {
87 $regexp .= $remodule;
88 }
89 $regexp = substr($regexp,0,-1) . '/';
90 return $regexp;
91}
92
93function scorm_parse_aicc($pkgdir,$scormid) {
bf347041 94 global $DB;
95
dc383b6f 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);
f6520280 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 }
dc383b6f 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);
203
bf347041 204 $oldscoes = $DB->get_records('scorm_scoes', array('scorm'=>$scormid));
dc383b6f 205
206 $launch = 0;
207 if (isset($courses)) {
208 foreach ($courses as $course) {
531fa830 209 $sco = new object();
dc383b6f 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 = '';
217
218 //print_r($sco);
bf347041 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);
dc383b6f 222 unset($oldscoes[$id]);
223 } else {
bf347041 224 $id = $DB->insert_record('scorm_scoes',$sco);
dc383b6f 225 }
226
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;
f6520280 237
238 if (!isset($element->parent) || strtolower($element->parent) == 'root') {
dc383b6f 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';
f6520280 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;
bf347041 251 $id = $DB->update_record('scorm_scoes',$sco);
252 $DB->delete_records('scorm_scoes_data', array('scoid'=>$oldscoid));
f6520280 253 unset($oldscoes[$oldscoid]);
254 } else {
bf347041 255 $id = $DB->insert_record('scorm_scoes',$sco);
f6520280 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;
bf347041 263 $dataid = $DB->insert_record('scorm_scoes_data',$scodata);
f6520280 264 }
265 if (isset($element->prerequisites)) {
266 $scodata->name = 'prerequisites';
267 $scodata->value = $element->prerequisites;
bf347041 268 $dataid = $DB->insert_record('scorm_scoes_data',$scodata);
f6520280 269 }
270 if (isset($element->max_time_allowed)) {
271 $scodata->name = 'max_time_allowed';
272 $scodata->value = $element->max_time_allowed;
bf347041 273 $dataid = $DB->insert_record('scorm_scoes_data',$scodata);
f6520280 274 }
275 if (isset($element->time_limit_action)) {
276 $scodata->name = 'time_limit_action';
277 $scodata->value = $element->time_limit_action;
bf347041 278 $dataid = $DB->insert_record('scorm_scoes_data',$scodata);
f6520280 279 }
280 if (isset($element->mastery_score)) {
281 $scodata->name = 'mastery_score';
282 $scodata->value = $element->mastery_score;
bf347041 283 $dataid = $DB->insert_record('scorm_scoes_data',$scodata);
f6520280 284 }
52005f34 285 if (isset($element->core_vendor)) {
286 $scodata->name = 'datafromlms';
287 $scodata->value = eregi_replace('<cr>', "\r\n", $element->core_vendor);
c6089d93 288 $dataid = $DB->insert_record('scorm_scoes_data',$scodata);
52005f34 289 }
f6520280 290 }
291 if ($launch==0) {
292 $launch = $id;
293 }
dc383b6f 294 }
295 }
296 }
297 }
298 }
299 if (!empty($oldscoes)) {
300 foreach($oldscoes as $oldsco) {
bf347041 301 $DB->delete_records('scorm_scoes', array('id'=>$oldsco->id));
302 $DB->delete_records('scorm_scoes_track', array('scoid'=>$oldsco->id));
dc383b6f 303 }
304 }
bf347041 305 $DB->set_field('scorm','version','AICC', array('id'=>$scormid));
dc383b6f 306 return $launch;
307}
308
309function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
bf347041 310 global $CFG, $DB;
dc383b6f 311
dc383b6f 312 $strexpand = get_string('expcoll','scorm');
313 $modestr = '';
314 if ($mode == 'browse') {
315 $modestr = '&amp;mode='.$mode;
316 }
317 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
318
319 $result = new stdClass();
f6520280 320 $result->toc = "<ul id='s0' class='$liststyle'>\n";
dc383b6f 321 $tocmenus = array();
322 $result->prerequisites = true;
323 $incomplete = false;
324
325 //
326 // Get the current organization infos
327 //
dc383b6f 328 if (!empty($currentorg)) {
bf347041 329 if (($organizationtitle = $DB->get_field('scorm_scoes','title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
dc383b6f 330 $result->toc .= "\t<li>$organizationtitle</li>\n";
331 $tocmenus[] = $organizationtitle;
332 }
dc383b6f 333 }
334 //
335 // If not specified retrieve the last attempt number
336 //
337 if (empty($attempt)) {
338 $attempt = scorm_get_last_attempt($scorm->id, $user->id);
339 }
340 $result->attemptleft = $scorm->maxattempt - $attempt;
82605bea 341 if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
dc383b6f 342 //
343 // Retrieve user tracking data for each learning object
344 //
345 $usertracks = array();
346 foreach ($scoes as $sco) {
347 if (!empty($sco->launch)) {
82605bea 348 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
dc383b6f 349 if ($usertrack->status == '') {
350 $usertrack->status = 'notattempted';
351 }
352 $usertracks[$sco->identifier] = $usertrack;
353 }
354 }
355 }
356
357 $level=0;
358 $sublist=1;
359 $previd = 0;
360 $nextid = 0;
361 $findnext = false;
362 $parents[$level]='/';
363
82605bea 364 foreach ($scoes as $pos => $sco) {
f6520280 365 $isvisible = false;
bf347041 366 $sco->title = $sco->title;
82605bea 367 if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
368 $isvisible = true;
f6520280 369 }
dc383b6f 370 if ($parents[$level]!=$sco->parent) {
371 if ($newlevel = array_search($sco->parent,$parents)) {
372 for ($i=0; $i<($level-$newlevel); $i++) {
373 $result->toc .= "\t\t</ul></li>\n";
374 }
375 $level = $newlevel;
376 } else {
377 $i = $level;
378 $closelist = '';
379 while (($i > 0) && ($parents[$level] != $sco->parent)) {
380 $closelist .= "\t\t</ul></li>\n";
381 $i--;
382 }
383 if (($i == 0) && ($sco->parent != $currentorg)) {
384 $style = '';
385 if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
386 $style = ' style="display: none;"';
387 }
f6520280 388 $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
dc383b6f 389 $level++;
390 } else {
391 $result->toc .= $closelist;
392 $level = $i;
393 }
394 $parents[$level]=$sco->parent;
395 }
396 }
f6520280 397 if ($isvisible) {
398 $result->toc .= "\t\t<li>";
399 }
9fb2de4e 400 if (isset($scoes[$pos+1])) {
401 $nextsco = $scoes[$pos+1];
402 } else {
403 $nextsco = false;
404 }
f6520280 405 $nextisvisible = false;
82605bea 406 if (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true'))) {
407 $nextisvisible = true;
f6520280 408 }
409 if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
dc383b6f 410 $sublist++;
411 $icon = 'minus';
412 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
413 $icon = 'plus';
414 }
859e38fe 415 $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>';
f6520280 416 } else if ($isvisible) {
dc383b6f 417 $result->toc .= '<img src="'.$scormpixdir.'/spacer.gif" />';
418 }
419 if (empty($sco->title)) {
420 $sco->title = $sco->identifier;
421 }
422 if (!empty($sco->launch)) {
f6520280 423 if ($isvisible) {
424 $startbold = '';
425 $endbold = '';
426 $score = '';
427 if (empty($scoid) && ($mode != 'normal')) {
428 $scoid = $sco->id;
530a61d5 429 }
f6520280 430 if (isset($usertracks[$sco->identifier])) {
431 $usertrack = $usertracks[$sco->identifier];
432 $strstatus = get_string($usertrack->status,'scorm');
433 if ($sco->scormtype == 'sco') {
434 $statusicon = '<img src="'.$scormpixdir.'/'.$usertrack->status.'.gif" alt="'.$strstatus.'" title="'.$strstatus.'" />';
435 } else {
436 $statusicon = '<img src="'.$scormpixdir.'/assetc.gif" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
437 }
530a61d5 438
f6520280 439 if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
440 $incomplete = true;
441 if ($play && empty($scoid)) {
442 $scoid = $sco->id;
443 }
444 }
445 if ($usertrack->score_raw != '') {
446 $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
447 }
448 $strsuspended = get_string('suspended','scorm');
449 if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
450 $statusicon = '<img src="'.$scormpixdir.'/suspend.gif" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
451 }
452 } else {
dc383b6f 453 if ($play && empty($scoid)) {
454 $scoid = $sco->id;
455 }
f6520280 456 $incomplete = true;
457 if ($sco->scormtype == 'sco') {
458 $statusicon = '<img src="'.$scormpixdir.'/notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
459 } else {
460 $statusicon = '<img src="'.$scormpixdir.'/asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
461 }
530a61d5 462 }
f6520280 463 if ($sco->id == $scoid) {
f6520280 464 $startbold = '<b>';
465 $endbold = '</b>';
466 $findnext = true;
82605bea 467 $shownext = isset($sco->next) ? $sco->next : 0;
468 $showprev = isset($sco->previous) ? $sco->previous : 0;
dc383b6f 469 }
dc383b6f 470
f6520280 471 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
472 if (!empty($sco->launch)) {
473 $previd = $sco->id;
474 }
dc383b6f 475 }
f6520280 476 if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
477 if ($sco->id == $scoid) {
478 $result->prerequisites = true;
479 }
480 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
481 $result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
482 $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
483 } else {
484 if ($sco->id == $scoid) {
485 $result->prerequisites = false;
486 }
c852a628 487 $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."</li>\n";
dc383b6f 488 }
dc383b6f 489 }
490 } else {
c852a628 491 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
dc383b6f 492 }
493 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
494 if (!empty($nextsco->launch)) {
495 $nextid = $nextsco->id;
496 }
497 }
498 }
499 for ($i=0;$i<$level;$i++) {
500 $result->toc .= "\t\t</ul></li>\n";
501 }
502
503 if ($play) {
f6520280 504 $sco = scorm_get_sco($scoid);
dc383b6f 505 $sco->previd = $previd;
506 $sco->nextid = $nextid;
507 $result->sco = $sco;
508 $result->incomplete = $incomplete;
509 } else {
510 $result->incomplete = $incomplete;
511 }
512 }
513 $result->toc .= "\t</ul>\n";
514 if ($scorm->hidetoc == 0) {
515 $result->toc .= '
f4ba7e1a 516 <script type="text/javascript">
517 //<![CDATA[
dc383b6f 518 function expandCollide(which,list,item) {
859e38fe 519 var el = document.ids ? document.ids[list] : document.getElementById ? document.getElementById(list) : document.all[list];
f6520280 520 which = which.substring(0,(which.length));
859e38fe 521 var el2 = document.ids ? document.ids[which] : document.getElementById ? document.getElementById(which) : document.all[which];
522 if (el.style.display != "none") {
523 el2.src = "'.$scormpixdir.'/plus.gif";
524 el.style.display=\'none\';
dc383b6f 525 new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
526 } else {
859e38fe 527 el2.src = "'.$scormpixdir.'/minus.gif";
528 el.style.display=\'block\';
dc383b6f 529 new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
530 }
531 }
f4ba7e1a 532 //]]>
dc383b6f 533 </script>'."\n";
534 }
535
536 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid=';
537 $result->tocmenu = popup_form($url,$tocmenus, "tocmenu", $sco->id, '', '', '', true);
538
539 return $result;
540}
541
542?>