aadf2241d9a1798f8ee8761479cecba3e693bcf2
[moodle.git] / mod / scorm / aicc.php
1 <?php
3     //Content Type Header
4     header("Content-type: text/plain");
5     //Prevent Caching Headers
6     header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
7     header("Cache-Control: no-cache");
8     header("Pragma: no-cache");
10     require_once('../../config.php');
11     require_once('locallib.php');
12     require_once('datamodels/aicclib.php');
14     $command = required_param('command', PARAM_ALPHA);
15     $sessionid = required_param('session_id', PARAM_ALPHANUM);
16     $aiccdata = optional_param('aicc_data', '', PARAM_RAW);
18     require_login();
20     if (!empty($command) && confirm_sesskey($sessionid)) {
21         $command = strtolower($command);
23         if (isset($SESSION->scorm_scoid)) {
24             $scoid = $SESSION->scorm_scoid;
25         } else {
26             print_error('cannotcallscript');
27         }
28         $mode = 'normal';
29         if (isset($SESSION->scorm_mode)) {
30             $mode = $SESSION->scorm_mode;
31         }
32         $status = 'Not Initialized';
33         if (isset($SESSION->scorm_status)) {
34             $status = $SESSION->scorm_status;
35         }
36         if (isset($SESSION->attempt)) {
37             $attempt = $SESSION->attempt;
38         } else {
39             $attempt = 1;
40         }
42         if ($sco = scorm_get_sco($scoid, SCO_ONLY)) {
43             if (!$scorm = $DB->get_record('scorm','id',$sco->scorm)) {
44                 print_error('cannotcallscript');
45             }
46         } else {
47             print_error('cannotcallscript');
48         }
50         if ($scorm = $DB->get_record('scorm','id',$sco->scorm)) {
51             switch ($command) {
52                 case 'getparam':
53                     if ($status == 'Not Initialized') {
54                         $SESSION->scorm_status = 'Running';
55                         $status = 'Running';
56                     }
57                     if ($status != 'Running') {
58                         echo "error=101\nerror_text=Terminated\n";
59                     } else {
60                         if ($usertrack=scorm_get_tracks($scoid,$USER->id,$attempt)) {
61                             $userdata = $usertrack;
62                         } else {
63                             $userdata->status = '';
64                             $userdata->score_raw = '';
65                         }
66                         $userdata->student_id = $USER->username;
67                         $userdata->student_name = $USER->lastname .', '. $USER->firstname;
68                         $userdata->mode = $mode;
69                         if ($userdata->mode == 'normal') {
70                             $userdata->credit = 'credit';
71                         } else {
72                             $userdata->credit = 'no-credit';
73                         }
75                         if ($sco = scorm_get_sco($scoid)) {
76                             $userdata->course_id = $sco->identifier;
77                             $userdata->datafromlms = isset($sco->datafromlms)?$sco->datafromlms:'';
78                             $userdata->mastery_score = isset($sco->mastery_score)?$sco->mastery_score:'';
79                             $userdata->max_time_allowed = isset($sco->max_time_allowed)?$sco->max_time_allowed:'';
80                             $userdata->time_limit_action = isset($sco->time_limit_action)?$sco->time_limit_action:'';
82                             echo "error=0\nerror_text=Successful\naicc_data=";
83                             echo "[Core]\n";
84                             echo 'Student_ID = '.$userdata->student_id."\n";
85                             echo 'Student_Name = '.$userdata->student_name."\n";
86                             if (isset($userdata->{'cmi.core.lesson_location'})) {
87                                 echo 'Lesson_Location = '.$userdata->{'cmi.core.lesson_location'}."\n";
88                             } else {
89                                 echo 'Lesson_Location = '."\n";
90                             }
91                             echo 'Credit = '.$userdata->credit."\n";
92                             if (isset($userdata->status)) {
93                                 if ($userdata->status == '') {
94                                     $userdata->entry = ', ab-initio';
95                                 } else {
96                                     if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
97                                         $userdata->entry = ', resume';
98                                     } else {
99                                         $userdata->entry = '';
100                                     }
101                                 }
102                             }
103                             if (isset($userdata->{'cmi.core.lesson_status'})) {
104                                 echo 'Lesson_Status = '.$userdata->{'cmi.core.lesson_status'}.$userdata->entry."\n";
105                                 $SESSION->scorm_lessonstatus = $userdata->{'cmi.core.lesson_status'};
106                             } else {
107                                 echo 'Lesson_Status = not attempted'.$userdata->entry."\n";
108                                 $SESSION->scorm_lessonstatus = 'not attempted';
109                             }
110                             if (isset($userdata->{'cmi.core.score.raw'})) {
111                                 $max = '';
112                                 $min = '';
113                                 if (isset($userdata->{'cmi.core.score.max'}) && !empty($userdata->{'cmi.core.score.max'})) {
114                                     $max = ', '.$userdata->{'cmi.core.score.max'};
115                                     if (isset($userdata->{'cmi.core.score.min'}) && !empty($userdata->{'cmi.core.score.min'})) {
116                                         $min = ', '.$userdata->{'cmi.core.score.min'};
117                                     }
118                                 }
119                                 echo 'Score = '.$userdata->{'cmi.core.score.raw'}.$max.$min."\n";
120                             } else {
121                                 echo 'Score = '."\n";
122                             }
123                             if (isset($userdata->{'cmi.core.total_time'})) {
124                                 echo 'Time = '.$userdata->{'cmi.core.total_time'}."\n";
125                             } else {
126                                 echo 'Time = '.'00:00:00'."\n";
127                             }
128                             echo 'Lesson_Mode = '.$userdata->mode."\n";
129                             if (isset($userdata->{'cmi.suspend_data'})) {
130                                 echo "[Core_Lesson]\n".rawurldecode($userdata->{'cmi.suspend_data'})."\n";
131                             } else {
132                                 echo "[Core_Lesson]\n"."\n";
133                             }
134                             echo "[Core_Vendor]\n".$userdata->datafromlms."\n";
135                             echo "[Evaluation]\nCourse_ID = {".$userdata->course_id."}\n";
136                             echo "[Student_Data]\n";
137                             echo 'Mastery_Score = '.$userdata->mastery_score."\n";
138                             echo 'Max_Time_Allowed = '.$userdata->max_time_allowed."\n";
139                             echo 'Time_Limit_Action = '.$userdata->time_limit_action."\n";
140                         } else {
141                             print_error('cannotfindsco', 'scorm');
142                         }
143                     }
144                 break;
145                 case 'putparam':
146                     if ($status == 'Running') {
147                         if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $scorm->course)) {
148                             echo "error=1\nerror_text=Unknown\n"; // No one must see this error message if not hacked
149                         }
150                         if (!empty($aiccdata) && has_capability('mod/scorm:savetrack', get_context_instance(CONTEXT_MODULE, $cm->id))) {
151                             $initlessonstatus = 'not attempted';
152                             $lessonstatus = 'not attempted';
153                             if (isset($SESSION->scorm_lessonstatus)) {
154                                 $initlessonstatus = $SESSION->scorm_lessonstatus;
155                             }
156                             $score = '';
157                             $datamodel['lesson_location'] = 'cmi.core.lesson_location';
158                             $datamodel['lesson_status'] = 'cmi.core.lesson_status';
159                             $datamodel['score'] = 'cmi.core.score.raw';
160                             $datamodel['time'] = 'cmi.core.session_time';
161                             $datamodel['[core_lesson]'] = 'cmi.suspend_data';
162                             $datamodel['[comments]'] = 'cmi.comments';
163                             $datarows = explode("\n",$aiccdata);
164                             reset($datarows);
165                             while ((list(,$datarow) = each($datarows)) !== false) {
166                                 if (($equal = strpos($datarow, '=')) !== false) {
167                                     $element = strtolower(trim(substr($datarow,0,$equal)));
168                                     $value = trim(substr($datarow,$equal+1));
169                                     if (isset($datamodel[$element])) {
170                                         $element = $datamodel[$element];
171                                         switch ($element) {
172                                             case 'cmi.core.lesson_location':
173                                                 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
174                                             break;
175                                             case 'cmi.core.lesson_status':
176                                                 $statuses = array(
177                                                            'passed' => 'passed',
178                                                            'completed' => 'completed',
179                                                            'failed' => 'failed',
180                                                            'incomplete' => 'incomplete',
181                                                            'browsed' => 'browsed',
182                                                            'not attempted' => 'not attempted',
183                                                            'p' => 'passed',
184                                                            'c' => 'completed',
185                                                            'f' => 'failed',
186                                                            'i' => 'incomplete',
187                                                            'b' => 'browsed',
188                                                            'n' => 'not attempted'
189                                                            );
190                                                 $exites = array(
191                                                            'logout' => 'logout',
192                                                            'time-out' => 'time-out',
193                                                            'suspend' => 'suspend',
194                                                            'l' => 'logout',
195                                                            't' => 'time-out',
196                                                            's' => 'suspend',
197                                                            );
198                                                 $values = explode(',',$value);
199                                                 $value = '';
200                                                 if (count($values) > 1) {
201                                                     $value = trim(strtolower($values[1]));
202                                                     if (isset($exites[$value])) {
203                                                         $value = $exites[$value];
204                                                     }
205                                                 }
206                                                 if (empty($value) || isset($exites[$value])) {
207                                                     $subelement = 'cmi.core.exit';
208                                                     $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $subelement, $value);
209                                                 }
210                                                 $value = trim(strtolower($values[0]));
211                                                 if (isset($statuses[$value]) && ($mode == 'normal')) {
212                                                     $value = $statuses[$value];
213                                                     $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
214                                                 }
215                                                 $lessonstatus = $value;
216                                             break;
217                                             case 'cmi.core.score.raw':
218                                                  $values = explode(',',$value);
219                                                  if ((count($values) > 1) && ($values[1] >= $values[0]) && is_numeric($values[1])) {
220                                                      $subelement = 'cmi.core.score.max';
221                                                      $value = trim($values[1]);
222                                                      $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $subelement, $value);
223                                                      if ((count($values) == 3) && ($values[2] <= $values[0]) && is_numeric($values[2])) {
224                                                          $subelement = 'cmi.core.score.min';
225                                                          $value = trim($values[2]);
226                                                          $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $subelement, $value);
227                                                      }
228                                                  }
230                                                  $value = '';
231                                                  if (is_numeric($values[0])) {
232                                                      $value = trim($values[0]);
233                                                      $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
234                                                  }
235                                                  $score = $value;
236                                             break;
237                                             case 'cmi.core.session_time':
238                                                  $SESSION->scorm_session_time = $value;
239                                             break;
240                                         }
241                                     }
242                                 } else {
243                                     if (isset($datamodel[strtolower(trim($datarow))])) {
244                                         $element = $datamodel[strtolower(trim($datarow))];
245                                         $value = '';
246                                         while ((($datarow = current($datarows)) !== false) && (substr($datarow,0,1) != '[')) {
247                                             $value .= $datarow;
248                                             next($datarows);
249                                         }
250                                         $value = rawurlencode($value);
251                                         $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
252                                     }
253                                 }
254                             }
255                             if (($mode == 'browse') && ($initlessonstatus == 'not attempted')){
256                                 $lessonstatus = 'browsed';
257                                 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, 'cmi.core.lesson_status', 'browsed');
258                             }
259                             if ($mode == 'normal') {
260                                 if ($lessonstatus == 'completed') {
261                                     if ($sco = scorm_get_sco($scoid)) {
262                                         if (!empty($sco->mastery_score) && !empty($score) && ($score >= $sco->mastery_score)) {
263                                             $lessonstatus = 'passed';
264                                         } else {
265                                             $lessonstatus = 'failed';
266                                         }
267                                         $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, 'cmi.core.lesson_status', $lessonstatus);
268                                     }
269                                 }
270                             }
271                         }
272                         echo "error=0\nerror_text=Successful\n";
273                     } else if ($status == 'Terminated') {
274                         echo "error=1\nerror_text=Terminated\n";
275                     } else {
276                         echo "error=1\nerror_text=Not Initialized\n";
277                     }
278                 break;
279                 case 'putcomments':
280                     if ($status == 'Running') {
281                         echo "error=0\nerror_text=Successful\n";
282                     } else if ($status == 'Terminated') {
283                         echo "error=1\nerror_text=Terminated\n";
284                     } else {
285                         echo "error=1\nerror_text=Not Initialized\n";
286                     }
287                 break;
288                 case 'putinteractions':
289                     if ($status == 'Running') {
290                         echo "error=0\nerror_text=Successful\n";
291                     } else if ($status == 'Terminated') {
292                         echo "error=1\nerror_text=Terminated\n";
293                     } else {
294                         echo "error=1\nerror_text=Not Initialized\n";
295                     }
296                 break;
297                 case 'putobjectives':
298                     if ($status == 'Running') {
299                         echo "error=0\nerror_text=Successful\n";
300                     } else if ($status == 'Terminated') {
301                         echo "error=1\nerror_text=Terminated\n";
302                     } else {
303                         echo "error=1\nerror_text=Not Initialized\n";
304                     }
305                 break;
306                 case 'putpath':
307                     if ($status == 'Running') {
308                         echo "error=0\nerror_text=Successful\n";
309                     } else if ($status == 'Terminated') {
310                         echo "error=1\nerror_text=Terminated\n";
311                     } else {
312                         echo "error=1\nerror_text=Not Initialized\n";
313                     }
314                 break;
315                 case 'putperformance':
316                     if ($status == 'Running') {
317                         echo "error=0\nerror_text=Successful\n";
318                     } else if ($status == 'Terminated') {
319                         echo "error=1\nerror_text=Terminated\n";
320                     } else {
321                         echo "error=1\nerror_text=Not Initialized\n";
322                     }
323                 break;
324                 case 'exitau':
325                     if ($status == 'Running') {
326                         if (isset($SESSION->scorm_session_time) && ($SESSION->scorm_session_time != '')) {
327                             if ($track = $DB->get_record_select('scorm_scoes_track',"userid='$USER->id' AND scormid='$scorm->id' AND scoid='$sco->id' AND element='cmi.core.total_time'")) {
328                                // Add session_time to total_time
329                                 $value = scorm_add_time($track->value, $SESSION->scorm_session_time);
330                                 $track->value = $value;
331                                 $track->timemodified = time();
332                                 $id = $DB->update_record('scorm_scoes_track',$track);
333                             } else {
334                                 $track->userid = $USER->id;
335                                 $track->scormid = $scorm->id;
336                                 $track->scoid = $sco->id;
337                                 $track->element = 'cmi.core.total_time';
338                                 $track->value = $SESSION->scorm_session_time;
339                                 $track->timemodified = time();
340                                 $id = $DB->insert_record('scorm_scoes_track',$track);
341                             }
342                         }
343                         $SESSION->scorm_status = 'Terminated';
344                         $SESSION->scorm_session_time = '';
345                         echo "error=0\nerror_text=Successful\n";
346                     } else if ($status == 'Terminated') {
347                         echo "error=1\nerror_text=Terminated\n";
348                     } else {
349                         echo "error=1\nerror_text=Not Initialized\n";
350                     }
351                 break;
352                 default:
353                     echo "error=1\nerror_text=Invalid Command\n";
354                 break;
355             }
356         }
357     } else {
358         if (empty($command)) {
359             echo "error=1\nerror_text=Invalid Command\n";
360         } else {
361             echo "error=3\nerror_text=Invalid Session ID\n";
362         }
363     }
364 ?>