c8a8ba04aa6f7d4d70eb9d180c84273819c87ed9
[moodle.git] / mod / scorm / datamodels / aicclib.php
1 <?php
3 function scorm_add_time($a, $b) {
4     $aes = explode(':',$a);
5     $bes = explode(':',$b);
6     $aseconds = explode('.',$aes[2]);
7     $bseconds = explode('.',$bes[2]);
8     $change = 0;
10     $acents = 0;  //Cents
11     if (count($aseconds) > 1) {
12         $acents = $aseconds[1];
13     }
14     $bcents = 0;
15     if (count($bseconds) > 1) {
16         $bcents = $bseconds[1];
17     }
18     $cents = $acents + $bcents;
19     $change = floor($cents / 100);
20     $cents = $cents - ($change * 100);
21     if (floor($cents) < 10) {
22         $cents = '0'. $cents;
23     }
25     $secs = $aseconds[0] + $bseconds[0] + $change;  //Seconds
26     $change = floor($secs / 60);
27     $secs = $secs - ($change * 60);
28     if (floor($secs) < 10) {
29         $secs = '0'. $secs;
30     }
32     $mins = $aes[1] + $bes[1] + $change;   //Minutes
33     $change = floor($mins / 60);
34     $mins = $mins - ($change * 60);
35     if ($mins < 10) {
36         $mins = '0' .  $mins;
37     }
39     $hours = $aes[0] + $bes[0] + $change;  //Hours
40     if ($hours < 10) {
41         $hours = '0' . $hours;
42     }
44     if ($cents != '0') {
45         return $hours . ":" . $mins . ":" . $secs . '.' . $cents;
46     } else {
47         return $hours . ":" . $mins . ":" . $secs;
48     }
49 }
51 /**
52 * Take the header row of an AICC definition file
53 * and returns sequence of columns and a pointer to
54 * the sco identifier column.
55 *
56 * @param string $row AICC header row
57 * @param string $mastername AICC sco identifier column
58 * @return mixed
59 */
60 function scorm_get_aicc_columns($row,$mastername='system_id') {
61     $tok = strtok(strtolower($row),"\",\n\r");
62     $result->columns = array();
63     $i=0;
64     while ($tok) {
65         if ($tok !='') {
66             $result->columns[] = $tok;
67             if ($tok == $mastername) {
68                 $result->mastercol = $i;
69             }
70             $i++;
71         }
72         $tok = strtok("\",\n\r");
73     }
74     return $result;
75 }
77 /**
78 * Given a colums array return a string containing the regular
79 * expression to match the columns in a text row.
80 *
81 * @param array $column The header columns
82 * @param string $remodule The regular expression module for a single column
83 * @return string
84 */
85 function scorm_forge_cols_regexp($columns,$remodule='(".*")?,') {
86     $regexp = '/^';
87     foreach ($columns as $column) {
88         $regexp .= $remodule;
89     }
90     $regexp = substr($regexp,0,-1) . '/';
91     return $regexp;
92 }
95 function scorm_parse_aicc($scorm) {
96     global $DB;
98     if (!isset($scorm->cmid)) {
99         $cm = get_coursemodule_from_instance('scorm', $scorm->id);
100         $scorm->cmid = $cm->id;
101     }
102     $context = get_context_instance(CONTEXT_MODULE, $scorm->cmid);
104     $fs = get_file_storage();
106     $files = $fs->get_area_files($context->id, 'mod_scorm', 'content', 0, '', false);
109     $version = 'AICC';
110     $ids = array();
111     $courses = array();
112     $extaiccfiles = array('crs','des','au','cst','ort','pre','cmp');
114     foreach ($files as $file) {
115         $filename = $file->get_filename();
116         $ext = substr($filename,strrpos($filename,'.'));
117         $extension = strtolower(substr($ext,1));
118         if (in_array($extension,$extaiccfiles)) {
119             $id = strtolower(basename($filename,$ext));
120             $ids[$id]->$extension = $file;
121         }
122     }
124     foreach ($ids as $courseid => $id) {
125         if (isset($id->crs)) {
126             $contents = $id->crs->get_content();
127             $rows = explode("\r\n", $contents);
128             if (is_array($rows)) {
129                 foreach ($rows as $row) {
130                     if (preg_match("/^(.+)=(.+)$/",$row,$matches)) {
131                         switch (strtolower(trim($matches[1]))) {
132                             case 'course_id':
133                                 $courses[$courseid]->id = trim($matches[2]);
134                             break;
135                             case 'course_title':
136                                 $courses[$courseid]->title = trim($matches[2]);
137                             break;
138                             case 'version':
139                                 $courses[$courseid]->version = 'AICC_'.trim($matches[2]);
140                             break;
141                         }
142                     }
143                 }
144             }
145         }
146         if (isset($id->des)) {
147             $contents = $id->des->get_content();
148             $rows = explode("\r\n", $contents);
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->au)) {
161             $contents = $id->au->get_content();
162             $rows = explode("\r\n", $contents);
163             $columns = scorm_get_aicc_columns($rows[0]);
164             $regexp = scorm_forge_cols_regexp($columns->columns);
165             for ($i=1;$i<count($rows);$i++) {
166                 if (preg_match($regexp,$rows[$i],$matches)) {
167                     for ($j=0;$j<count($columns->columns);$j++) {
168                         $column = $columns->columns[$j];
169                         $courses[$courseid]->elements[substr(trim($matches[$columns->mastercol+1]),1,-1)]->$column = substr(trim($matches[$j+1]),1,-1);
170                     }
171                 }
172             }
173         }
174         if (isset($id->cst)) {
175             $contents = $id->cst->get_content();
176             $rows = explode("\r\n", $contents);
177             $columns = scorm_get_aicc_columns($rows[0],'block');
178             $regexp = scorm_forge_cols_regexp($columns->columns,'(.+)?,');
179             for ($i=1;$i<count($rows);$i++) {
180                 if (preg_match($regexp,$rows[$i],$matches)) {
181                     for ($j=0;$j<count($columns->columns);$j++) {
182                         if ($j != $columns->mastercol) {
183                             $courses[$courseid]->elements[substr(trim($matches[$j+1]),1,-1)]->parent = substr(trim($matches[$columns->mastercol+1]),1,-1);
184                         }
185                     }
186                 }
187             }
188         }
189         if (isset($id->ort)) {
190             $contents = $id->ort->get_content();
191             $rows = explode("\r\n", $contents);
192             $columns = scorm_get_aicc_columns($rows[0],'course_element');
193             $regexp = scorm_forge_cols_regexp($columns->columns,'(.+)?,');
194             for ($i=1;$i<count($rows);$i++) {
195                 if (preg_match($regexp,$rows[$i],$matches)) {
196                     for ($j=0;$j<count($matches)-1;$j++) {
197                         if ($j != $columns->mastercol) {
198                             $courses[$courseid]->elements[substr(trim($matches[$j+1]),1,-1)]->parent = substr(trim($matches[$columns->mastercol+1]),1,-1);
199                         }
200                     }
201                 }
202             }
203         }
204         if (isset($id->pre)) {
205             $contents = $id->pre->get_content();
206             $rows = explode("\r\n", $contents);
207             $columns = scorm_get_aicc_columns($rows[0],'structure_element');
208             $regexp = scorm_forge_cols_regexp($columns->columns,'(.+),');
209             for ($i=1;$i<count($rows);$i++) {
210                 if (preg_match($regexp,$rows[$i],$matches)) {
211                     $courses[$courseid]->elements[$columns->mastercol+1]->prerequisites = substr(trim($matches[1-$columns->mastercol+1]),1,-1);
212                 }
213             }
214         }
215         if (isset($id->cmp)) {
216             $contents = $id->cmp->get_content();
217             $rows = explode("\r\n", $contents);
218         }
219     }
220     //print_r($courses);
222     $oldscoes = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id));
224     $launch = 0;
225     if (isset($courses)) {
226         foreach ($courses as $course) {
227             $sco = new object();
228             $sco->identifier = $course->id;
229             $sco->scorm = $scorm->id;
230             $sco->organization = '';
231             $sco->title = $course->title;
232             $sco->parent = '/';
233             $sco->launch = '';
234             $sco->scormtype = '';
236             //print_r($sco);
237             if ($ss = $DB->get_record('scorm_scoes', array('scorm'=>$scorm->id,'identifier'=>$sco->identifier))) {
238                 $id = $ss->id;
239                 $DB->update_record('scorm_scoes',$sco);
240                 unset($oldscoes[$id]);
241             } else {
242                 $id = $DB->insert_record('scorm_scoes',$sco);
243             }
245             if ($launch == 0) {
246                 $launch = $id;
247             }
248             if (isset($course->elements)) {
249                 foreach($course->elements as $element) {
250                     unset($sco);
251                     $sco->identifier = $element->system_id;
252                     $sco->scorm = $scorm->id;
253                     $sco->organization = $course->id;
254                     $sco->title = $element->title;
256                     if (!isset($element->parent) || strtolower($element->parent) == 'root') {
257                         $sco->parent = '/';
258                     } else {
259                         $sco->parent = $element->parent;
260                     }
261                     if (isset($element->file_name)) {
262                         $sco->launch = $element->file_name;
263                         $sco->scormtype = 'sco';
264                         $sco->previous = 0;
265                         $sco->next = 0;
266                         $id = null;
267                         if ($oldscoid = scorm_array_search('identifier',$sco->identifier,$oldscoes)) {
268                             $sco->id = $oldscoid;
269                             if ($DB->update_record('scorm_scoes',$sco)) {
270                                 $id = $oldscoid;
271                             }
272                             $DB->delete_records('scorm_scoes_data', array('scoid'=>$oldscoid));
273                             unset($oldscoes[$oldscoid]);
274                         } else {
275                             $id = $DB->insert_record('scorm_scoes',$sco);
276                         }
277                         if (!empty($id)) {
278                             unset($scodata);
279                             $scodata->scoid = $id;
280                             if (isset($element->web_launch)) {
281                                 $scodata->name = 'parameters';
282                                 $scodata->value = $element->web_launch;
283                                 $dataid = $DB->insert_record('scorm_scoes_data',$scodata);
284                             }
285                             if (isset($element->prerequisites)) {
286                                 $scodata->name = 'prerequisites';
287                                 $scodata->value = $element->prerequisites;
288                                 $dataid = $DB->insert_record('scorm_scoes_data',$scodata);
289                             }
290                             if (isset($element->max_time_allowed)) {
291                                 $scodata->name = 'max_time_allowed';
292                                 $scodata->value = $element->max_time_allowed;
293                                 $dataid = $DB->insert_record('scorm_scoes_data',$scodata);
294                             }
295                             if (isset($element->time_limit_action)) {
296                                 $scodata->name = 'time_limit_action';
297                                 $scodata->value = $element->time_limit_action;
298                                 $dataid = $DB->insert_record('scorm_scoes_data',$scodata);
299                             }
300                             if (isset($element->mastery_score)) {
301                                 $scodata->name = 'mastery_score';
302                                 $scodata->value = $element->mastery_score;
303                                 $dataid = $DB->insert_record('scorm_scoes_data',$scodata);
304                             }
305                             if (isset($element->core_vendor)) {
306                                 $scodata->name = 'datafromlms';
307                                 $scodata->value = preg_replace('/<cr>/i', "\r\n", $element->core_vendor);
308                                 $dataid = $DB->insert_record('scorm_scoes_data',$scodata);
309                             }
310                         }
311                         if ($launch==0) {
312                             $launch = $id;
313                         }
314                     }
315                 }
316             }
317         }
318     }
319     if (!empty($oldscoes)) {
320         foreach($oldscoes as $oldsco) {
321             $DB->delete_records('scorm_scoes', array('id'=>$oldsco->id));
322             $DB->delete_records('scorm_scoes_track', array('scoid'=>$oldsco->id));
323         }
324     }
326     $scorm->version = 'AICC';
328     $scorm->launch = $launch;
330     return true;
333 function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
334     global $CFG, $DB, $PAGE, $OUTPUT;
336     $strexpand = get_string('expcoll','scorm');
337     $modestr = '';
338     if ($mode == 'browse') {
339         $modestr = '&amp;mode='.$mode;
340     }
342     $result = new stdClass();
343     $result->toc = "<ul id='s0' class='$liststyle'>\n";
344     $tocmenus = array();
345     $result->prerequisites = true;
346     $incomplete = false;
348     //
349     // Get the current organization infos
350     //
351     if (!empty($currentorg)) {
352         if (($organizationtitle = $DB->get_field('scorm_scoes','title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
353             $result->toc .= "\t<li>$organizationtitle</li>\n";
354             $tocmenus[] = $organizationtitle;
355         }
356     }
357     //
358     // If not specified retrieve the last attempt number
359     //
360     if (empty($attempt)) {
361         $attempt = scorm_get_last_attempt($scorm->id, $user->id);
362     }
363     $result->attemptleft = $scorm->maxattempt - $attempt;
364     if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
365         //
366         // Retrieve user tracking data for each learning object
367         //
368         $usertracks = array();
369         foreach ($scoes as $sco) {
370             if (!empty($sco->launch)) {
371                 if (empty($scoid)) {
372                     $scoid = $sco->id;
373                 }
374                 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
375                     if ($usertrack->status == '') {
376                         $usertrack->status = 'notattempted';
377                     }
378                     $usertracks[$sco->identifier] = $usertrack;
379                 }
380             }
381         }
383         $level=0;
384         $sublist=1;
385         $previd = 0;
386         $nextid = 0;
387         $findnext = false;
388         $parents[$level]='/';
390         foreach ($scoes as $pos => $sco) {
391             $isvisible = false;
392             $sco->title = $sco->title;
393             if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
394                 $isvisible = true;
395             }
396             if ($parents[$level]!=$sco->parent) {
397                 if ($newlevel = array_search($sco->parent,$parents)) {
398                     for ($i=0; $i<($level-$newlevel); $i++) {
399                         $result->toc .= "\t\t</ul></li>\n";
400                     }
401                     $level = $newlevel;
402                 } else {
403                     $i = $level;
404                     $closelist = '';
405                     while (($i > 0) && ($parents[$level] != $sco->parent)) {
406                         $closelist .= "\t\t</ul></li>\n";
407                         $i--;
408                     }
409                     if (($i == 0) && ($sco->parent != $currentorg)) {
410                         $style = '';
411                         if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
412                             $style = ' style="display: none;"';
413                         }
414                         $result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
415                         $level++;
416                     } else {
417                         $result->toc .= $closelist;
418                         $level = $i;
419                     }
420                     $parents[$level]=$sco->parent;
421                 }
422             }
423             if ($isvisible) {
424                 $result->toc .= "\t\t<li>";
425             }
426             if (isset($scoes[$pos+1])) {
427                 $nextsco = $scoes[$pos+1];
428             } else {
429                 $nextsco = false;
430             }
431             $nextisvisible = false;
432             if (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true'))) {
433                 $nextisvisible = true;
434             }
435             if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
436                 $sublist++;
437                 $icon = 'minus';
438                 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
439                     $icon = 'plus';
440                 }
441                 $result->toc .= '<a href="javascript:expandCollide(\'img'.$sublist.'\',\'s'.$sublist.'\','.$nextsco->id.');"><img id="img'.$sublist.'" src="'.$OUTPUT->pix_url($icon, 'scorm').'" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
442             } else if ($isvisible) {
443                 $result->toc .= '<img src="'.$OUTPUT->pix_url('spacer', 'scorm').'" alt="" />';
444             }
445             if (empty($sco->title)) {
446                 $sco->title = $sco->identifier;
447             }
448             if (!empty($sco->launch)) {
449                 if ($isvisible) {
450                     $startbold = '';
451                     $endbold = '';
452                     $score = '';
453                     if (empty($scoid) && ($mode != 'normal')) {
454                         $scoid = $sco->id;
455                     }
456                     if (isset($usertracks[$sco->identifier])) {
457                         $usertrack = $usertracks[$sco->identifier];
458                         $strstatus = get_string($usertrack->status,'scorm');
459                         if ($sco->scormtype == 'sco') {
460                             $statusicon = '<img src="'.$OUTPUT->pix_url($usertrack->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" />';
461                         } else {
462                             $statusicon = '<img src="'.$OUTPUT->pix_url('assetc', 'scorm').'" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
463                         }
465                         if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
466                             $incomplete = true;
467                             if ($play && empty($scoid)) {
468                                 $scoid = $sco->id;
469                             }
470                         }
471                         if ($usertrack->score_raw != '') {
472                             $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
473                         }
474                         $strsuspended = get_string('suspended','scorm');
475                         if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
476                             $statusicon = '<img src="'.$OUTPUT->pix_url('suspend', 'scorm').'" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
477                         }
478                     } else {
479                         if ($play && empty($scoid)) {
480                             $scoid = $sco->id;
481                         }
482                         $incomplete = true;
483                         if ($sco->scormtype == 'sco') {
484                             $statusicon = '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
485                         } else {
486                             $statusicon = '<img src="'.$OUTPUT->pix_url('asset', 'scorm').'" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
487                         }
488                     }
489                     if ($sco->id == $scoid) {
490                         $startbold = '<b>';
491                         $endbold = '</b>';
492                         $findnext = true;
493                         $shownext = isset($sco->next) ? $sco->next : 0;
494                         $showprev = isset($sco->previous) ? $sco->previous : 0;
495                     }
497                     if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
498                         if (!empty($sco->launch)) {
499                             $previd = $sco->id;
500                         }
501                     }
502                     if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
503                         if ($sco->id == $scoid) {
504                             $result->prerequisites = true;
505                         }
506                         $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
507                         $result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
508                         $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
509                     } else {
510                         if ($sco->id == $scoid) {
511                             $result->prerequisites = false;
512                         }
513                         $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."</li>\n";
514                     }
515                 }
516             } else {
517                 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
518             }
519             if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
520                 if (!empty($nextsco->launch)) {
521                     $nextid = $nextsco->id;
522                 }
523             }
524         }
525         for ($i=0;$i<$level;$i++) {
526             $result->toc .= "\t\t</ul></li>\n";
527         }
529         if ($play) {
530             $sco = scorm_get_sco($scoid);
531             $sco->previd = $previd;
532             $sco->nextid = $nextid;
533             $result->sco = $sco;
534             $result->incomplete = $incomplete;
535         } else {
536             $result->incomplete = $incomplete;
537         }
538     }
539     $result->toc .= "\t</ul>\n";
540     if ($scorm->hidetoc == 0) {
541         $result->toc .= html_writer::script(js_writer::set_variable('scormdata', array(
542                 'plusicon' => $OUTPUT->pix_url('plus', 'scorm'),
543                 'minusicon' => $OUTPUT->pix_url('minus', 'scorm'))));
544         $result->toc .= html_writer::script('', $CFG->wwwroot.'/lib/cookies.js');
545         $result->toc .= html_writer::script('', $CFG->wwwroot.'/mod/scorm/datamodels/scorm_datamodels.js');
546     }
548     $url = new moodle_url('/mod/scorm/player.php?a='.$scorm->id.'&currentorg='.$currentorg.$modestr);
549     $result->tocmenu = $OUTPUT->single_select($url, 'scoid', $tocmenus, $sco->id, null, "tocmenu");
551     return $result;