Fixed a problem with AICC tracking
authorbobopinna <bobopinna>
Tue, 26 Jul 2005 11:01:27 +0000 (11:01 +0000)
committerbobopinna <bobopinna>
Tue, 26 Jul 2005 11:01:27 +0000 (11:01 +0000)
mod/scorm/aicc.php
mod/scorm/datamodel.php
mod/scorm/lib.php
mod/scorm/loadSCO.php
mod/scorm/playscorm.php

index 5caa631..415b941 100755 (executable)
@@ -3,9 +3,10 @@
     require_once('lib.php');
 
     $command = required_param('command', '', PARAM_ALPHA);
-    $sessionid = required_param('session_id', '', PARAM_ALPHA);
+    $sessionid = required_param('session_id', '', PARAM_ALPHANUM);
+    $aiccdata = optional_param('aicc_data', '', PARAM_RAW);
 
-    require_login($course->id, false, $cm);
+    require_login();
     
     if (!empty($command) && confirm_sesskey($sessionid)) {
         $command = strtolower($command);
                         $userdata->student_id = $USER->username;
                         $userdata->student_name = $USER->lastname .', '. $USER->firstname;
                         $userdata->mode = $mode;
+                        if ($userdata->mode == 'normal') {
+                            $userdata->credit = 'credit';
+                        } else {
+                            $userdata->credit = 'no-credit';
+                        } 
                 
                         if ($sco = get_record('scorm_scoes','id',$scoid)) {
                             $userdata->course_id = $sco->identifier;
                             $userdata->masteryscore = $sco->masteryscore;
                             $userdata->maxtimeallowed = $sco->maxtimeallowed;
                             $userdata->timelimitaction = $sco->timelimitaction;
-                            if (!empty($sco->masteryscore)) {
-                                $userdata->credit = 'credit';
-                            } else {
-                                $userdata->credit = 'no-credit';
-                            }    
+                               
                             echo "error = 0\nerror_text = Successful\naicc_data=\n";
                             echo "[Core]\n";
                             echo 'Student_ID = '.$userdata->student_id."\n";
                                 echo 'Lesson_Location = '."\n";
                             }
                             echo 'Credit = '.$userdata->credit."\n";
+                            if (isset($userdata->status)) {
+                                if ($userdata->status == '') {
+                                    $userdata->entry = ', ab-initio';
+                                } else {
+                                    if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
+                                        $userdata->entry = ', resume';
+                                    } else {
+                                        $userdata->entry = '';
+                                    }
+                                }
+                            }
                             if (isset($userdata->{'cmi.core.lesson_status'})) {
-                                echo 'Lesson_Status = '.$userdata->{'cmi.core.lesson_status'}."\n";
+                                echo 'Lesson_Status = '.$userdata->{'cmi.core.lesson_status'}.$userdata->entry."\n";
+                                $SESSION->scorm_lessonstatus = $userdata->{'cmi.core.lesson_status'};
                             } else {
-                                echo 'Lesson_Status = '."\n";
+                                echo 'Lesson_Status = not attempted'.$userdata->entry."\n";
+                                $SESSION->scorm_lessonstatus = 'not attempted';
                             }
                             if (isset($userdata->{'cmi.core.score.raw'})) {
-                                echo 'Score = '.$userdata->{'cmi.core.score.raw'}."\n";
+                                $max = '';
+                                $min = '';
+                                if (isset($userdata->{'cmi.core.score.max'}) && !empty($userdata->{'cmi.core.score.max'})) {
+                                    $max = ', '.$userdata->{'cmi.core.score.max'};
+                                    if (isset($userdata->{'cmi.core.score.min'}) && !empty($userdata->{'cmi.core.score.min'})) {
+                                        $min = ', '.$userdata->{'cmi.core.score.min'};
+                                    }
+                                }
+                                echo 'Score = '.$userdata->{'cmi.core.score.raw'}.$max.$min."\n";
                             } else {
                                 echo 'Score = '."\n";
                             }
                             } else {
                                 echo 'Time = '.'00:00:00'."\n";
                             }
+                            echo 'Lesson_Mode = '.$userdata->mode."\n";
                             if (isset($userdata->{'cmi.suspend_data'})) {
                                 echo "[Core_Lesson]\n".$userdata->{'cmi.suspend_data'}."\n";
                             } else {
                             }
                             echo "[Core_Vendor]\n".$userdata->datafromlms."\n";
                             echo "[Evaluation]\nCourse_ID = {".$userdata->course_id."}\n";
+                            echo "[Student_Data]\n";
+                            echo 'Mastery_Score = '.$userdata->masteryscore."\n";
+                            echo 'Max_Time_Allowed = '.$userdata->maxtimeallowed."\n";
+                            echo 'Time_Limit_Action = '.$userdata->timelimitaction."\n";
                         } else {
                             error('Sco not found');
                         }
                 break;
                 case 'putparam':
                     if ($status == 'Running') {
+                        if (!empty($aiccdata)) {
+                            $initlessonstatus = 'not attempted';
+                            $lessonstatus = 'not attempted';
+                            if (isset($SESSION->scorm_lessonstatus)) {
+                                $initlessonstatus = $SESSION->scorm_lessonstatus;
+                            }
+                            $score = '';
+                            $datamodel['lesson_location'] = 'cmi.core.lesson_location';
+                            $datamodel['lesson_status'] = 'cmi.core.lesson_status';
+                            $datamodel['score'] = 'cmi.core.score.raw';
+                            $datamodel['time'] = 'cmi.core.session_time';
+                            $datamodel['[core_lesson]'] = 'cmi.suspend_data';
+                            $datamodel['[comments]'] = 'cmi.comments';
+                            $datarows = explode("\n",$aiccdata);
+                            reset($datarows);
+                            while ((list(,$datarow) = each($datarows)) !== false) {
+                                if (($equal = strpos($datarow, '=')) !== false) {
+                                    $element = strtolower(trim(substr($datarow,0,$equal)));
+                                    $value = trim(substr($datarow,$equal+1));
+                                    if (isset($datamodel[$element])) {
+                                        $element = $datamodel[$element];
+                                        switch ($element) {
+                                            case 'cmi.core.lesson_location':
+                                                $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $element, $value);
+                                            break;
+                                            case 'cmi.core.lesson_status':
+                                                $statuses = array(
+                                                           'passed' => 'passed',
+                                                           'completed' => 'completed',
+                                                           'failed' => 'failed',
+                                                           'incomplete' => 'incomplete',
+                                                           'browsed' => 'browsed',
+                                                           'not attempted' => 'not attempted',
+                                                           'p' => 'passed',
+                                                           'c' => 'completed',
+                                                           'f' => 'failed',
+                                                           'i' => 'incomplete',
+                                                           'b' => 'browsed',
+                                                           'n' => 'not attempted'
+                                                           );
+                                                $exites = array(
+                                                           'logout' => 'logout',
+                                                           'time-out' => 'time-out',
+                                                           'suspend' => 'suspend',
+                                                           'l' => 'logout',
+                                                           't' => 'time-out',
+                                                           's' => 'suspend',
+                                                           );
+                                                $values = explode(',',$value);
+                                                $value = '';
+                                                if (count($values) > 1) {
+                                                    $value = trim(strtolower($values[1]));
+                                                    if (isset($exites[$value])) {
+                                                        $value = $exites[$value];
+                                                    }
+                                                }
+                                                if (empty($value) || isset($exites[$value])) {
+                                                    $subelement = 'cmi.core.exit';
+                                                    $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $subelement, $value);
+                                                }
+                                                $value = trim(strtolower($values[0]));
+                                                if (isset($statuses[$value]) && ($mode == 'normal')) {
+                                                    $value = $statuses[$value];
+                                                    $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $element, $value);
+                                                }
+                                                $lessonstatus = $value;
+                                            break;
+                                            case 'cmi.core.score.raw':
+                                                 $values = explode(',',$value);
+                                                 if ((count($values) > 1) && ($values[1] >= $values[0]) && is_numeric($values[1])) {
+                                                     $subelement = 'cmi.core.score.max';
+                                                     $value = trim($values[1]);
+                                                     $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $subelement, $value);
+                                                     if ((count($values) == 3) && ($values[2] <= $values[0]) && is_numeric($values[2])) {
+                                                         $subelement = 'cmi.core.score.min';
+                                                         $value = trim($values[2]);
+                                                         $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $subelement, $value);
+                                                     }
+                                                 }
+                                              
+                                                 $value = '';
+                                                 if (is_numeric($values[0])) {
+                                                     $value = trim($values[0]);
+                                                     $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $element, $value);
+                                                 }
+                                                 $score = $value;
+                                            break;
+                                            case 'cmi.core.session_time':
+                                                 $SESSION->scorm_session_time = $value;
+                                            break;
+                                        }
+                                    }
+                                } else {
+                                    if (isset($datamodel[strtolower(trim($datarow))])) {
+                                        $element = $datamodel[strtolower(trim($datarow))];
+                                        $value = '';
+                                        while ((($datarow = current($datarows)) !== false) && (substr($datarow,0,1) != '[')) {
+                                            $value .= $datarow;
+                                            next($datarows);
+                                        }
+                                        $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $element, $value);
+                                    }
+                                }                               
+                            }
+                            if (($mode == 'browse') && ($initlessonstatus == 'not attempted')){
+                                $lessonstatus = 'browsed';
+                                $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, 'cmi.core.lesson_status', 'browsed');
+                            }
+                            if ($mode == 'normal') {
+                                if ($lessonstatus == 'completed') {
+                                    if (!empty($sco->masteryscore) && !empty($score) && ($score >= $sco->masteryscore)) {
+                                        $lessonstatus = 'passed';
+                                    } else {
+                                        $lessonstatus = 'failed';
+                                    }
+                                    $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, 'cmi.core.lesson_status', $lessonstatus);
+                                }
+                            }                  
+                        }
                         echo "error = 0\nerror_text = Successful\n";
                     } else if ($status == 'Terminated') {
                         echo "error = 1\nerror_text = Terminated\n";
                 break;
                 case 'exitau':
                     if ($status == 'Running') {
-                        $SESSION->scorm_status = 'Terminated';                  
+                        if (isset($SESSION->scorm_session_time) && ($SESSION->scorm_session_time != '')) {
+                            if ($track = get_record_select('scorm_scoes_track',"userid='$USER->id' AND scormid='$scorm->id' AND scoid='$sco->id' AND element='cmi.core.total_time'")) {
+                                // Add session_time to total_time
+                                $value = scorm_add_time($track->value, $SESSION->scorm_session_time);
+                                $track->value = $value;
+                                $track->timemodified = time();
+                                $id = update_record('scorm_scoes_track',$track);
+                            } else {
+                                $track->userid = $USER->id;
+                                $track->scormid = $scorm->id;
+                                $track->scoid = $sco->id;
+                                $track->element = 'cmi.core.total_time';
+                                $track->value = $SESSION->scorm_session_time;
+                                $track->timemodified = time();
+                                $id = insert_record('scorm_scoes_track',$track);
+                            }
+                        }
+                        
+                        $SESSION->scorm_status = 'Terminated';
+                        $SESSION->scorm_session_time = '';
                         echo "error = 0\nerror_text = Successful\n";
                     } else if ($status == 'Terminated') {
                         echo "error = 1\nerror_text = Terminated\n";
index f7bbe1b..ea09741 100755 (executable)
             if (substr($element,0,3) == 'cmi') {
                 $element = str_replace('__','.',$element);
                 $element = preg_replace('/_(\d+)/',".\$1",$element);
-                if ($track = get_record_select('scorm_scoes_track',"userid='$USER->id' AND scormid='$scorm->id' AND scoid='$scoid' AND element='$element'")) {
-                    $track->value = $value;
-                   $track->timemodified = time();
-                    $result = update_record('scorm_scoes_track',$track) && $result;
-                } else {
-                    $track->userid = $USER->id;
-                    $track->scormid = $scorm->id;
-                    $track->scoid = $scoid;
-                    $track->element = $element;
-                    $track->value = $value;
-                   $track->timemodified = time();
-                    $result = insert_record('scorm_scoes_track',$track) && $result;
-                }
+                $result = scorm_insert_track($USER->id, $scorm->id, $scoid, $element, $value) && $result;
             }
         }
         if ($result) {
index c07a2e9..4d2448c 100755 (executable)
@@ -960,17 +960,6 @@ function scorm_get_user_data($userid) {
     return get_record('user','id',$userid,'','','','','firstname, lastname, picture');
 }
 
-/*function scorm_remove_spaces($sourcestr) {
-// Remove blank space from a string
-    $newstr='';
-    for( $i=0; $i<strlen($sourcestr); $i++) {
-        if ($sourcestr[$i]!=' ') {
-            $newstr .=$sourcestr[$i];
-        }
-    }
-    return $newstr;
-} */
-
 function scorm_string_round($stringa, $len=11) {
 // Crop a string to $len character and set an anchor title to the full string
     if (strlen($stringa)>$len) {
@@ -979,15 +968,6 @@ function scorm_string_round($stringa, $len=11) {
         return $stringa;
     }
 }
-
-/*function scorm_id_search($id, $usertracks) {
-    foreach ($usertracks as $key => $usertrack) {
-        if ($usertrack->identifier == $id) {
-            return $key;
-        } 
-    }
-    return false;
-} */
     
 function scorm_eval_prerequisites($prerequisites,$usertracks) {
     $element = '';
@@ -1146,6 +1126,72 @@ function scorm_eval_prerequisites($prerequisites,$usertracks) {
     return eval('return '.implode($stack).';');
 }
 
+function scorm_insert_track($userid,$scormid,$scoid,$element,$value) {
+    $id = null;
+    if ($track = get_record_select('scorm_scoes_track',"userid='$userid' AND scormid='$scormid' AND scoid='$scoid' AND element='$element'")) {
+        $track->value = $value;
+        $track->timemodified = time();
+        $id = update_record('scorm_scoes_track',$track);
+    } else {
+        $track->userid = $userid;
+        $track->scormid = $scormid;
+        $track->scoid = $scoid;
+        $track->element = $element;
+        $track->value = $value;
+        $track->timemodified = time();
+        $id = insert_record('scorm_scoes_track',$track);
+    }
+    return $id;
+}
+
+function scorm_add_time($a, $b) {
+    $aes = explode(':',$a);
+    $bes = explode(':',$b);
+    $aseconds = explode('.',$aes[2]);
+    $bseconds = explode('.',$bes[2]);
+    $change = 0;
+
+    $acents = 0;  //Cents
+    if (count($aseconds) > 1) {
+        $acents = $aseconds[1];
+    }
+    $bcents = 0;
+    if (count($bseconds) > 1) {
+        $bcents = $bseconds[1];
+    }
+    $cents = $acents + $bcents;
+    $change = floor($cents / 100);
+    $cents = $cents - ($change * 100);
+    if (floor($cents) < 10) {
+        $cents = '0'. $cents;
+    }
+
+    $secs = $aseconds[0] + $bseconds[0] + $change;  //Seconds
+    $change = floor($secs / 60);
+    $secs = $secs - ($change * 60);
+    if (floor($secs) < 10) {
+        $secs = '0'. $secs;
+    }
+
+    $mins = $aes[1] + $bes[1] + $change;   //Minutes
+    $change = floor($mins / 60);
+    $mins = $mins - ($change * 60);
+    if ($mins < 10) {
+        $mins = '0' .  $mins;
+    }
+
+    $hours = $aes[0] + $bes[0] + $change;  //Hours
+    if ($hours < 10) {
+        $hours = '0' . $hours;
+    }
+
+    if ($cents != '0') {
+        return $hours . ":" . $mins . ":" . $secs . '.' . $cents;
+    } else {
+        return $hours . ":" . $mins . ":" . $secs;
+    }
+}
+
 function scorm_external_link($link) {
 // check if a link is external
     $result = false;
index 89afbc0..2e750b3 100755 (executable)
             $connector = '?';
         }
     }
-    if (scorm_external_link($sco->launch)) {
-        if ($version == 'AICC') {
-            if (!empty($sco->parameters)) {
-                $sco->parameters = '&'. $sco->parameters;
-            }
-            $result = $sco->launch.$connector.'aicc_sid='.sesskey().'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$sco->parameters;
-        } else {
-            $result = $sco->launch.$connector.$sco->parameters;
+    
+    if ($version == 'AICC') {
+        if (!empty($sco->parameters)) {
+            $sco->parameters = '&'. $sco->parameters;
         }
+        $launcher = $sco->launch.$connector.'aicc_sid='.sesskey().'&aicc_url='.$CFG->wwwroot.'/mod/scorm/aicc.php'.$sco->parameters;
+    } else {
+        $launcher = $sco->launch.$connector.$sco->parameters;
+    }
+    
+    if (scorm_external_link($sco->launch)) {
+        $result = $launcher;
     } else {
         if ($CFG->slasharguments) {
-            $result = $CFG->wwwroot.'/file.php/'.$scorm->course.'/moddata/scorm/'.$scorm->id.'/'.$sco->launch.$connector.$sco->parameters;
+            $result = $CFG->wwwroot.'/file.php/'.$scorm->course.'/moddata/scorm/'.$scorm->id.'/'.$launcher;
         } else {
-            $result = $CFG->wwwroot.'/file.php?file=/'.$scorm->course.'/moddata/scorm/'.$scorm->id.'/'.$sco->launch.$connector.$sco->parameters;
+            $result = $CFG->wwwroot.'/file.php?file=/'.$scorm->course.'/moddata/scorm/'.$scorm->id.'/'.$launcher;
         }
     }
 ?>
index 53dc7d0..8ab7d1c 100755 (executable)
     $modestring = '&mode='.$mode;
 
     $SESSION->scorm_scoid = $sco->id;
+    $SESSION->scorm_status = 'Not Initialized';
+    $SESSION->scorm_mode = $mode;
 
     //
     // Print the page header
     //
     $scripts = '';
-    if ($scorm->popup == 1) {
+    if (($scorm->popup == 1) && ($result->prerequisites)) {
         $scripts = 'onunload="top.main.close();"';
     }