SCORM MDL-22951 New player for SCORM fixes major regression that prevents some SCORM...
[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     $result->toc = '<div id="scorm_layout">';
345     $result->toc .= '<div id="scorm_toc">';
346     $result->toc .= '<div id="scorm_tree"><ul>';
347     $tocmenus = array();
348     $result->prerequisites = true;
349     $incomplete = false;
351     //
352     // Get the current organization infos
353     //
354     if (!empty($currentorg)) {
355         if (($organizationtitle = $DB->get_field('scorm_scoes','title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
356             if ($play) {
357                 $result->toctitle = "$organizationtitle";
358             }
359             else {
360                 $result->toc .= "\t<li>$organizationtitle</li>\n";
361             }
362             $tocmenus[] = $organizationtitle;
363         }
364     }
365     //
366     // If not specified retrieve the last attempt number
367     //
368     if (empty($attempt)) {
369         $attempt = scorm_get_last_attempt($scorm->id, $user->id);
370     }
371     $result->attemptleft = $scorm->maxattempt - $attempt;
372     if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
373         //
374         // Retrieve user tracking data for each learning object
375         //
376         $usertracks = array();
377         foreach ($scoes as $sco) {
378             if (!empty($sco->launch)) {
379                 if (empty($scoid)) {
380                     $scoid = $sco->id;
381                 }
382                 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
383                     if ($usertrack->status == '') {
384                         $usertrack->status = 'notattempted';
385                     }
386                     $usertracks[$sco->identifier] = $usertrack;
387                 }
388             }
389         }
391         $level=0;
392         $sublist=1;
393         $previd = 0;
394         $nextid = 0;
395         $findnext = false;
396         $parents[$level]='/';
398         foreach ($scoes as $pos => $sco) {
399             $isvisible = false;
400             $sco->title = $sco->title;
401             if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
402                 $isvisible = true;
403             }
404             if ($parents[$level]!=$sco->parent) {
405                 if ($newlevel = array_search($sco->parent,$parents)) {
406                     for ($i=0; $i<($level-$newlevel); $i++) {
407                         $result->toc .= "\t\t</ul></li>\n";
408                     }
409                     $level = $newlevel;
410                 } else {
411                     $i = $level;
412                     $closelist = '';
413                     while (($i > 0) && ($parents[$level] != $sco->parent)) {
414                         $closelist .= "\t\t</ul></li>\n";
415                         $i--;
416                     }
417                     if (($i == 0) && ($sco->parent != $currentorg)) {
418                         $style = '';
419                         if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
420                             $style = ' style="display: none;"';
421                         }
422                         //$result->toc .= "\t\t<li><ul id='s$sublist' class='$liststyle'$style>\n";
423                         $result->toc .= "\t\t<ul>\n";
424                         $level++;
425                     } else {
426                         $result->toc .= $closelist;
427                         $level = $i;
428                     }
429                     $parents[$level]=$sco->parent;
430                 }
431             }
432             if ($isvisible) {
433                 $result->toc .= "\t\t<li>";
434             }
435             if (isset($scoes[$pos+1])) {
436                 $nextsco = $scoes[$pos+1];
437             } else {
438                 $nextsco = false;
439             }
440             $nextisvisible = false;
441             if (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true'))) {
442                 $nextisvisible = true;
443             }
444             if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
445                 $sublist++;
446                 $icon = 'minus';
447                 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
448                     $icon = 'plus';
449                 }
450                 //$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>';
451             } else if ($isvisible) {
452                 //$result->toc .= '<img src="'.$OUTPUT->pix_url('spacer', 'scorm').'" alt="" />';
453             }
454             if (empty($sco->title)) {
455                 $sco->title = $sco->identifier;
456             }
457             if (!empty($sco->launch)) {
458                 if ($isvisible) {
459                     $startbold = '';
460                     $endbold = '';
461                     $score = '';
462                     if (empty($scoid) && ($mode != 'normal')) {
463                         $scoid = $sco->id;
464                     }
465                     if (isset($usertracks[$sco->identifier])) {
466                         $usertrack = $usertracks[$sco->identifier];
467                         $strstatus = get_string($usertrack->status,'scorm');
468                         if ($sco->scormtype == 'sco') {
469                             $statusicon = '<img src="'.$OUTPUT->pix_url($usertrack->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" />';
470                         } else {
471                             $statusicon = '<img src="'.$OUTPUT->pix_url('assetc', 'scorm').'" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
472                         }
474                         if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
475                             $incomplete = true;
476                             if ($play && empty($scoid)) {
477                                 $scoid = $sco->id;
478                             }
479                         }
480                         if ($usertrack->score_raw != '') {
481                             $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
482                         }
483                         $strsuspended = get_string('suspended','scorm');
484                         if (isset($usertrack->{'cmi.core.exit'}) && ($usertrack->{'cmi.core.exit'} == 'suspend')) {
485                             $statusicon = '<img src="'.$OUTPUT->pix_url('suspend', 'scorm').'" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
486                         }
487                     } else {
488                         if ($play && empty($scoid)) {
489                             $scoid = $sco->id;
490                         }
491                         $incomplete = true;
492                         if ($sco->scormtype == 'sco') {
493                             $statusicon = '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
494                         } else {
495                             $statusicon = '<img src="'.$OUTPUT->pix_url('asset', 'scorm').'" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
496                         }
497                     }
498                     if ($sco->id == $scoid) {
499                         $startbold = '<b>';
500                         $endbold = '</b>';
501                         $findnext = true;
502                         $shownext = isset($sco->next) ? $sco->next : 0;
503                         $showprev = isset($sco->previous) ? $sco->previous : 0;
504                     }
506                     if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
507                         if (!empty($sco->launch)) {
508                             $previd = $sco->id;
509                         }
510                     }
511                     if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
512                         if ($sco->id == $scoid) {
513                             $result->prerequisites = true;
514                         }
515                         $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
516                         $thisscoidstr = '&scoid='.$sco->id;
517                         $link = $CFG->wwwroot.'/mod/scorm/loadSCO.php?a='.$scorm->id.$thisscoidstr.$modestr;
518                         //$result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
519                         $result->toc .= '<a title="'.$link.'">'.$statusicon.'&nbsp;'.format_string($sco->title).'&nbsp;'.$score.'</a>';
520                         $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
521                     } else {
522                         if ($sco->id == $scoid) {
523                             $result->prerequisites = false;
524                         }
525                         $result->toc .= $statusicon.'&nbsp;'.format_string($sco->title)."</li>\n";
526                     }
527                 }
528             } else {
529                 $result->toc .= '&nbsp;'.format_string($sco->title)."</li>\n";
530             }
531             if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
532                 if (!empty($nextsco->launch)) {
533                     $nextid = $nextsco->id;
534                 }
535             }
536         }
537         for ($i=0;$i<$level;$i++) {
538             $result->toc .= "\t\t</ul></li>\n";
539         }
541         if ($play) {
542             $sco = scorm_get_sco($scoid);
543             $sco->previd = $previd;
544             $sco->nextid = $nextid;
545             $result->sco = $sco;
546             $result->incomplete = $incomplete;
547         } else {
548             $result->incomplete = $incomplete;
549         }
550     }
551     //$result->toc .= "\t</ul>\n";
553     // NEW IMS TOC
554     $result->toc .= '</ul></div></div>';
555     $result->toc .= '</div>';
556     $result->toc .= '<div id="scorm_navpanel"></div>';
559     if ($scorm->hidetoc == 0) {
560         $result->toc .= html_writer::script(js_writer::set_variable('scormdata', array(
561                 'plusicon' => $OUTPUT->pix_url('plus', 'scorm'),
562                 'minusicon' => $OUTPUT->pix_url('minus', 'scorm'))));
563         $result->toc .= html_writer::script('', $CFG->wwwroot.'/lib/cookies.js');
564         $result->toc .= html_writer::script('', $CFG->wwwroot.'/mod/scorm/datamodels/scorm_datamodels.js');
565     }
567     $url = new moodle_url('/mod/scorm/player.php?a='.$scorm->id.'&currentorg='.$currentorg.$modestr);
568     $result->tocmenu = $OUTPUT->single_select($url, 'scoid', $tocmenus, $sco->id, null, "tocmenu");
570     return $result;