MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / scorm / datamodels / aicclib.php
blobb39a054d4a101a0cbb74cd341a0bfa330c4f2223
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);
175 if (isset($id->pre)) {
176 $rows = file($pkgdir.'/'.$id->pre);
177 $columns = scorm_get_aicc_columns($rows[0],'structure_element');
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 $courses[$courseid]->elements[$columns->mastercol+1]->prerequisites = substr(trim($matches[1-$columns->mastercol+1]),1,-1);
185 if (isset($id->cmp)) {
186 $rows = file($pkgdir.'/'.$id->cmp);
189 //print_r($courses);
191 $oldscoes = get_records('scorm_scoes','scorm',$scormid);
193 $launch = 0;
194 if (isset($courses)) {
195 foreach ($courses as $course) {
196 $sco = new object();
197 $sco->identifier = $course->id;
198 $sco->scorm = $scormid;
199 $sco->organization = '';
200 $sco->title = $course->title;
201 $sco->parent = '/';
202 $sco->launch = '';
203 $sco->scormtype = '';
205 //print_r($sco);
206 if (get_record('scorm_scoes','scorm',$scormid,'identifier',$sco->identifier)) {
207 $id = update_record('scorm_scoes',$sco);
208 unset($oldscoes[$id]);
209 } else {
210 $id = insert_record('scorm_scoes',$sco);
213 if ($launch == 0) {
214 $launch = $id;
216 if (isset($course->elements)) {
217 foreach($course->elements as $element) {
218 unset($sco);
219 $sco->identifier = $element->system_id;
220 $sco->scorm = $scormid;
221 $sco->organization = $course->id;
222 $sco->title = $element->title;
223 if (strtolower($element->parent) == 'root') {
224 $sco->parent = '/';
225 } else {
226 $sco->parent = $element->parent;
228 if (isset($element->file_name)) {
229 $sco->launch = $element->file_name;
230 $sco->scormtype = 'sco';
231 } else {
232 $element->file_name = '';
233 $sco->scormtype = '';
235 if (!isset($element->prerequisites)) {
236 $element->prerequisites = '';
238 $sco->prerequisites = $element->prerequisites;
239 if (!isset($element->max_time_allowed)) {
240 $element->max_time_allowed = '';
242 $sco->maxtimeallowed = $element->max_time_allowed;
243 if (!isset($element->time_limit_action)) {
244 $element->time_limit_action = '';
246 $sco->timelimitaction = $element->time_limit_action;
247 if (!isset($element->mastery_score)) {
248 $element->mastery_score = '';
250 $sco->masteryscore = $element->mastery_score;
251 $sco->previous = 0;
252 $sco->next = 0;
253 if ($oldscoid = scorm_array_search('identifier',$sco->identifier,$oldscoes)) {
254 $sco->id = $oldscoid;
255 $id = update_record('scorm_scoes',$sco);
256 unset($oldscoes[$oldscoid]);
257 } else {
258 $id = insert_record('scorm_scoes',$sco);
260 if ($launch==0) {
261 $launch = $id;
267 if (!empty($oldscoes)) {
268 foreach($oldscoes as $oldsco) {
269 delete_records('scorm_scoes','id',$oldsco->id);
270 delete_records('scorm_scoes_track','scoid',$oldsco->id);
273 set_field('scorm','version','AICC','id',$scormid);
274 return $launch;
277 function scorm_get_toc($user,$scorm,$liststyle,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false) {
278 global $CFG;
280 $strexpand = get_string('expcoll','scorm');
281 $modestr = '';
282 if ($mode == 'browse') {
283 $modestr = '&amp;mode='.$mode;
285 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
287 $result = new stdClass();
288 $result->toc = "<ul id='0' class='$liststyle'>\n";
289 $tocmenus = array();
290 $result->prerequisites = true;
291 $incomplete = false;
294 // Get the current organization infos
296 $organizationsql = '';
297 if (!empty($currentorg)) {
298 if (($organizationtitle = get_field('scorm_scoes','title','scorm',$scorm->id,'identifier',$currentorg)) != '') {
299 $result->toc .= "\t<li>$organizationtitle</li>\n";
300 $tocmenus[] = $organizationtitle;
302 $organizationsql = "AND organization='$currentorg'";
305 // If not specified retrieve the last attempt number
307 if (empty($attempt)) {
308 $attempt = scorm_get_last_attempt($scorm->id, $user->id);
311 $result->attemptleft = $scorm->maxattempt - $attempt;
312 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
314 // Retrieve user tracking data for each learning object
316 $usertracks = array();
317 foreach ($scoes as $sco) {
318 if (!empty($sco->launch)) {
319 if ($usertrack=scorm_get_tracks($sco->id,$user->id,$attempt)) {
320 if ($usertrack->status == '') {
321 $usertrack->status = 'notattempted';
323 $usertracks[$sco->identifier] = $usertrack;
328 $level=0;
329 $sublist=1;
330 $previd = 0;
331 $nextid = 0;
332 $findnext = false;
333 $parents[$level]='/';
335 foreach ($scoes as $sco) {
336 if ($parents[$level]!=$sco->parent) {
337 if ($newlevel = array_search($sco->parent,$parents)) {
338 for ($i=0; $i<($level-$newlevel); $i++) {
339 $result->toc .= "\t\t</ul></li>\n";
341 $level = $newlevel;
342 } else {
343 $i = $level;
344 $closelist = '';
345 while (($i > 0) && ($parents[$level] != $sco->parent)) {
346 $closelist .= "\t\t</ul></li>\n";
347 $i--;
349 if (($i == 0) && ($sco->parent != $currentorg)) {
350 $style = '';
351 if (isset($_COOKIE['hide:SCORMitem'.$sco->id])) {
352 $style = ' style="display: none;"';
354 $result->toc .= "\t\t<li><ul id='$sublist' class='$liststyle'$style>\n";
355 $level++;
356 } else {
357 $result->toc .= $closelist;
358 $level = $i;
360 $parents[$level]=$sco->parent;
363 $result->toc .= "\t\t<li>";
364 $nextsco = next($scoes);
365 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
366 $sublist++;
367 $icon = 'minus';
368 if (isset($_COOKIE['hide:SCORMitem'.$nextsco->id])) {
369 $icon = 'plus';
371 $result->toc .= '<a href="javascript:expandCollide(img'.$sublist.','.$sublist.','.$nextsco->id.');"><img id="img'.$sublist.'" src="'.$scormpixdir.'/'.$icon.'.gif" alt="'.$strexpand.'" title="'.$strexpand.'"/></a>';
372 } else {
373 $result->toc .= '<img src="'.$scormpixdir.'/spacer.gif" />';
375 if (empty($sco->title)) {
376 $sco->title = $sco->identifier;
378 if (!empty($sco->launch)) {
379 $startbold = '';
380 $endbold = '';
381 $score = '';
382 if (empty($scoid) && ($mode != 'normal')) {
383 $scoid = $sco->id;
385 if (isset($usertracks[$sco->identifier])) {
386 $usertrack = $usertracks[$sco->identifier];
387 $strstatus = get_string($usertrack->status,'scorm');
388 if ($sco->scormtype == 'sco') {
389 $statusicon .= '<img src="'.$scormpixdir.'/'.$usertrack->status.'.gif" alt="'.$strstatus.'" title="'.$strstatus.'" />';
390 } else {
391 $statusicon = '<img src="'.$scormpixdir.'/assetc.gif" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
394 if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
395 $incomplete = true;
396 if ($play && empty($scoid)) {
397 $scoid = $sco->id;
400 if ($usertrack->score_raw != '') {
401 $score = '('.get_string('score','scorm').':&nbsp;'.$usertrack->score_raw.')';
403 $strsuspended = get_string('suspended','scorm');
404 if ($usertrack->{'cmi.core.exit'} == 'suspend') {
405 $statusicon = '<img src="'.$scormpixdir.'/suspend.gif" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
407 } else {
408 if ($play && empty($scoid)) {
409 $scoid = $sco->id;
411 $incomplete = true;
412 if ($sco->scormtype == 'sco') {
413 $statusicon = '<img src="'.$scormpixdir.'/notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
414 } else {
415 $statusicon = '<img src="'.$scormpixdir.'/asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
418 if ($sco->id == $scoid) {
419 $startbold = '<b>';
420 $endbold = '</b>';
421 $findnext = true;
422 $shownext = $sco->next;
423 $showprev = $sco->previous;
426 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
427 if (!empty($sco->launch)) {
428 $previd = $sco->id;
431 if (empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks)) {
432 if ($sco->id == $scoid) {
433 $result->prerequisites = true;
435 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid='.$sco->id;
436 $result->toc .= $statusicon.'&nbsp;'.$startbold.'<a href="'.$url.'">'.format_string($sco->title).'</a>'.$score.$endbold."</li>\n";
437 $tocmenus[$sco->id] = scorm_repeater('&minus;',$level) . '&gt;' . format_string($sco->title);
438 } else {
439 if ($sco->id == $scoid) {
440 $result->prerequisites = false;
442 $result->toc .= '&nbsp;'.$sco->title."</li>\n";
444 } else {
445 $result->toc .= '&nbsp;'.$sco->title."</li>\n";
447 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
448 if (!empty($nextsco->launch)) {
449 $nextid = $nextsco->id;
453 for ($i=0;$i<$level;$i++) {
454 $result->toc .= "\t\t</ul></li>\n";
457 if ($play) {
458 $sco = get_record('scorm_scoes','id',$scoid);
459 $sco->previd = $previd;
460 $sco->nextid = $nextid;
461 $result->sco = $sco;
462 $result->incomplete = $incomplete;
463 } else {
464 $result->incomplete = $incomplete;
467 $result->toc .= "\t</ul>\n";
468 if ($scorm->hidetoc == 0) {
469 $result->toc .= '
470 <script type="text/javascript">
471 //<![CDATA[
472 function expandCollide(which,list,item) {
473 var nn=document.ids?true:false
474 var w3c=document.getElementById?true:false
475 var beg=nn?"document.ids.":w3c?"document.getElementById(":"document.all.";
476 var mid=w3c?").style":".style";
478 if (eval(beg+list+mid+".display") != "none") {
479 which.src = "'.$scormpixdir.'/plus.gif";
480 eval(beg+list+mid+".display=\'none\';");
481 new cookie("hide:SCORMitem" + item, 1, 356, "/").set();
482 } else {
483 which.src = "'.$scormpixdir.'/minus.gif";
484 eval(beg+list+mid+".display=\'block\';");
485 new cookie("hide:SCORMitem" + item, 1, -1, "/").set();
488 //]]>
489 </script>'."\n";
492 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&amp;currentorg='.$currentorg.$modestr.'&amp;scoid=';
493 $result->tocmenu = popup_form($url,$tocmenus, "tocmenu", $sco->id, '', '', '', true);
495 return $result;