ce176d9cb97f98628c36a76ae2cffc2c13722a2a
[moodle.git] / mod / scorm / aicc.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 // Prevent Caching Headers.
18 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
19 header("Cache-Control: no-cache");
20 header("Pragma: no-cache");
22 require_once('../../config.php');
23 require_once($CFG->dirroot.'/mod/scorm/lib.php');
24 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
25 require_once($CFG->dirroot.'/mod/scorm/datamodels/aicclib.php');
27 foreach ($_POST as $key => $value) {
28     $tempkey = strtolower($key);
29     $_POST[$tempkey] = $value;
30 }
32 $command = required_param('command', PARAM_ALPHA);
33 $sessionid = required_param('session_id', PARAM_ALPHANUM);
34 $aiccdata = optional_param('aicc_data', '', PARAM_RAW);
36 $cfgscorm = get_config('scorm');
38 $url = new moodle_url('/mod/scorm/aicc.php', array('command' => $command, 'session_id' => $sessionid));
39 if ($aiccdata !== 0) {
40     $url->param('aicc_data', $aiccdata);
41 }
42 $PAGE->set_url($url);
44 if (empty($cfgscorm->allowaicchacp)) {
45     require_login();
46     if (!confirm_sesskey($sessionid)) {
47         print_error('invalidsesskey');
48     }
49     $aiccuser = $USER;
50     $scormsession = $SESSION->scorm;
51 } else {
52     $scormsession = scorm_aicc_confirm_hacp_session($sessionid);
53     if (empty($scormsession)) {
54         print_error('invalidhacpsession', 'scorm');
55     }
56     $aiccuser = $DB->get_record('user', array('id' => $scormsession->userid), 'id,username,lastname,firstname', MUST_EXIST);
57 }
59 if (!empty($command)) {
60     $command = strtolower($command);
62     if (isset($scormsession->scoid)) {
63         $scoid = $scormsession->scoid;
64     } else {
65         print_error('cannotcallscript');
66     }
67     $mode = 'normal';
68     if (isset($scormsession->scormmode)) {
69         $mode = $scormsession->scormmode;
70     }
71     $status = 'Not Initialized';
72     if (isset($scormsession->scormstatus)) {
73         $status = $scormsession->scormstatus;
74     }
75     if (isset($scormsession->attempt)) {
76         $attempt = $scormsession->attempt;
77     } else {
78         $attempt = 1;
79     }
81     if ($sco = scorm_get_sco($scoid, SCO_ONLY)) {
82         if (!$scorm = $DB->get_record('scorm', array('id' => $sco->scorm))) {
83             print_error('cannotcallscript');
84         }
85     } else {
86         print_error('cannotcallscript');
87     }
88     $aiccrequest = "MOODLE scoid: $scoid"
89                  . "\r\nMOODLE mode: $mode"
90                  . "\r\nMOODLE status: $status"
91                  . "\r\nMOODLE attempt: $attempt"
92                  . "\r\nAICC sessionid: $sessionid"
93                  . "\r\nAICC command: $command"
94                  . "\r\nAICC aiccdata:\r\n$aiccdata";
95     scorm_debug_log_write("aicc", "HACP Request:\r\n$aiccrequest", $scoid);
96     ob_start();
98     if ($scorm = $DB->get_record('scorm', array('id' => $sco->scorm))) {
99         switch ($command) {
100             case 'getparam':
101                 if ($status == 'Not Initialized') {
102                     $scormsession->scormstatus = 'Running';
103                     $status = 'Running';
104                 }
105                 if ($status != 'Running') {
106                     echo "error=101\r\nerror_text=Terminated\r\n";
107                 } else {
108                     if ($usertrack = scorm_get_tracks($scoid, $aiccuser->id, $attempt)) {
109                         $userdata = $usertrack;
110                     } else {
111                         $userdata->status = '';
112                         $userdata->score_raw = '';
113                     }
114                     if (!empty($cfg_scorm->aiccuserid)) {
115                         $userdata->student_id = $aiccuser->id;
116                     } else {
117                         $userdata->student_id = $aiccuser->username;
118                     }
119                     $userdata->student_name = $aiccuser->lastname .', '. $aiccuser->firstname;
120                     $userdata->mode = $mode;
121                     if ($userdata->mode == 'normal') {
122                         $userdata->credit = 'credit';
123                     } else {
124                         $userdata->credit = 'no-credit';
125                     }
127                     if ($sco = scorm_get_sco($scoid)) {
128                         $userdata->course_id = $sco->identifier;
129                         $userdata->datafromlms = isset($sco->datafromlms) ? $sco->datafromlms : '';
130                         $userdata->mastery_score = isset($sco->mastery_score) && is_numeric($sco->mastery_score) ?
131                                                         trim($sco->mastery_score) : '';
132                         $userdata->max_time_allowed = isset($sco->max_time_allowed) ? $sco->max_time_allowed : '';
133                         $userdata->time_limit_action = isset($sco->time_limit_action) ? $sco->time_limit_action : '';
135                         echo "error=0\r\nerror_text=Successful\r\naicc_data=";
136                         echo "[Core]\r\n";
137                         echo 'Student_ID='.$userdata->student_id."\r\n";
138                         echo 'Student_Name='.$userdata->student_name."\r\n";
139                         if (isset($userdata->{'cmi.core.lesson_location'})) {
140                             echo 'Lesson_Location='.$userdata->{'cmi.core.lesson_location'}."\r\n";
141                         } else {
142                             echo 'Lesson_Location='."\r\n";
143                         }
144                         echo 'Credit='.$userdata->credit."\r\n";
145                         if (isset($userdata->status)) {
146                             if ($userdata->status == '') {
147                                 $userdata->entry = ', ab-initio';
148                             } else {
149                                 if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
150                                     $userdata->entry = ', resume';
151                                 } else {
152                                     $userdata->entry = '';
153                                 }
154                             }
155                         }
156                         if (isset($userdata->{'cmi.core.lesson_status'})) {
157                             echo 'Lesson_Status='.$userdata->{'cmi.core.lesson_status'}.$userdata->entry."\r\n";
158                             $scormsession->scorm_lessonstatus = $userdata->{'cmi.core.lesson_status'};
159                         } else {
160                             echo 'Lesson_Status=not attempted'.$userdata->entry."\r\n";
161                             $scormsession->scorm_lessonstatus = 'not attempted';
162                         }
163                         if (isset($userdata->{'cmi.core.score.raw'})) {
164                             $max = '';
165                             $min = '';
166                             if (isset($userdata->{'cmi.core.score.max'}) && !empty($userdata->{'cmi.core.score.max'})) {
167                                 $max = ', '.$userdata->{'cmi.core.score.max'};
168                                 if (isset($userdata->{'cmi.core.score.min'}) && !empty($userdata->{'cmi.core.score.min'})) {
169                                     $min = ', '.$userdata->{'cmi.core.score.min'};
170                                 }
171                             }
172                             echo 'Score='.$userdata->{'cmi.core.score.raw'}.$max.$min."\r\n";
173                         } else {
174                             echo 'Score='."\r\n";
175                         }
176                         if (isset($userdata->{'cmi.core.total_time'})) {
177                             echo 'Time='.$userdata->{'cmi.core.total_time'}."\r\n";
178                         } else {
179                             echo 'Time='.'00:00:00'."\r\n";
180                         }
181                         echo 'Lesson_Mode='.$userdata->mode."\r\n";
182                         if (isset($userdata->{'cmi.suspend_data'})) {
183                             echo "[Core_Lesson]\r\n".rawurldecode($userdata->{'cmi.suspend_data'})."\r\n";
184                         } else {
185                             echo "[Core_Lesson]\r\n";
186                         }
187                         echo "[Core_Vendor]\r\n".$userdata->datafromlms."\r\n";
188                         echo "[Evaluation]\r\nCourse_ID = {".$userdata->course_id."}\r\n";
189                         echo "[Student_Data]\r\n";
190                         echo 'Mastery_Score='.$userdata->mastery_score."\r\n";
191                         echo 'Max_Time_Allowed='.$userdata->max_time_allowed."\r\n";
192                         echo 'Time_Limit_Action='.$userdata->time_limit_action."\r\n";
193                     } else {
194                         print_error('cannotfindsco', 'scorm');
195                     }
196                 }
197             break;
198             case 'putparam':
199                 if ($status == 'Running') {
200                     if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $scorm->course)) {
201                         echo "error=1\r\nerror_text=Unknown\r\n"; // No one must see this error message if not hacked.
202                     }
203                     $savetrack = has_capability('mod/scorm:savetrack', context_module::instance($cm->id), $aiccuser->id);
204                     if (!empty($aiccdata) && $savetrack) {
205                         $initlessonstatus = 'not attempted';
206                         $lessonstatus = 'not attempted';
207                         if (isset($scormsession->scorm_lessonstatus)) {
208                             $initlessonstatus = $scormsession->scorm_lessonstatus;
209                         }
210                         $score = '';
211                         $datamodel['lesson_location'] = 'cmi.core.lesson_location';
212                         $datamodel['lesson_status'] = 'cmi.core.lesson_status';
213                         $datamodel['score'] = 'cmi.core.score.raw';
214                         $datamodel['time'] = 'cmi.core.session_time';
215                         $datamodel['[core_lesson]'] = 'cmi.suspend_data';
216                         $datamodel['[comments]'] = 'cmi.comments';
217                         $datarows = explode("\r\n", $aiccdata);
218                         reset($datarows);
219                         while ((list(, $datarow) = each($datarows)) !== false) {
220                             if (($equal = strpos($datarow, '=')) !== false) {
221                                 $element = strtolower(trim(substr($datarow, 0, $equal)));
222                                 $value = trim(substr($datarow, $equal + 1));
223                                 if (isset($datamodel[$element])) {
224                                     $element = $datamodel[$element];
225                                     switch ($element) {
226                                         case 'cmi.core.lesson_location':
227                                             $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
228                                                                         $attempt, $element, $value);
229                                         break;
230                                         case 'cmi.core.lesson_status':
231                                             $statuses = array(
232                                                        'passed' => 'passed',
233                                                        'completed' => 'completed',
234                                                        'failed' => 'failed',
235                                                        'incomplete' => 'incomplete',
236                                                        'browsed' => 'browsed',
237                                                        'not attempted' => 'not attempted',
238                                                        'p' => 'passed',
239                                                        'c' => 'completed',
240                                                        'f' => 'failed',
241                                                        'i' => 'incomplete',
242                                                        'b' => 'browsed',
243                                                        'n' => 'not attempted'
244                                                        );
245                                             $exites = array(
246                                                        'logout' => 'logout',
247                                                        'time-out' => 'time-out',
248                                                        'suspend' => 'suspend',
249                                                        'l' => 'logout',
250                                                        't' => 'time-out',
251                                                        's' => 'suspend',
252                                                        );
253                                             $values = explode(',', $value);
254                                             $value = '';
255                                             if (count($values) > 1) {
256                                                 $value = trim(strtolower($values[1]));
257                                                 $value = $value[0];
258                                                 if (isset($exites[$value])) {
259                                                     $value = $exites[$value];
260                                                 }
261                                             }
262                                             if (empty($value) || isset($exites[$value])) {
263                                                 $subelement = 'cmi.core.exit';
264                                                 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
265                                                                             $attempt, $subelement, $value);
266                                             }
267                                             $value = trim(strtolower($values[0]));
268                                             $value = $value[0];
269                                             if (isset($statuses[$value]) && ($mode == 'normal')) {
270                                                 $value = $statuses[$value];
271                                                 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
272                                                                             $attempt, $element, $value);
273                                             }
274                                             $lessonstatus = $value;
275                                         break;
276                                         case 'cmi.core.score.raw':
277                                             $values = explode(',', $value);
278                                             if ((count($values) > 1) && ($values[1] >= $values[0]) && is_numeric($values[1])) {
279                                                 $subelement = 'cmi.core.score.max';
280                                                 $value = trim($values[1]);
281                                                 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
282                                                                             $attempt, $subelement, $value);
283                                                 if ((count($values) == 3) && ($values[2] <= $values[0]) && is_numeric($values[2])) {
284                                                     $subelement = 'cmi.core.score.min';
285                                                     $value = trim($values[2]);
286                                                     $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
287                                                                                 $attempt, $subelement, $value);
288                                                 }
289                                             }
291                                             $value = '';
292                                             if (is_numeric($values[0])) {
293                                                 $value = trim($values[0]);
294                                                 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
295                                                                             $attempt, $element, $value);
296                                             }
297                                             $score = $value;
298                                         break;
299                                         case 'cmi.core.session_time':
300                                              $scormsession->sessiontime = $value;
301                                         break;
302                                     }
303                                 }
304                             } else {
305                                 if (isset($datamodel[strtolower(trim($datarow))])) {
306                                     $element = $datamodel[strtolower(trim($datarow))];
307                                     $value = '';
308                                     while ((($datarow = current($datarows)) !== false) && (substr($datarow, 0, 1) != '[')) {
309                                         $value .= $datarow."\r\n";
310                                         next($datarows);
311                                     }
312                                     $value = rawurlencode($value);
313                                     $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id, $attempt, $element, $value);
314                                 }
315                             }
316                         }
317                         if (($mode == 'browse') && ($initlessonstatus == 'not attempted')) {
318                             $lessonstatus = 'browsed';
319                             $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
320                                                         $attempt, 'cmi.core.lesson_status', 'browsed');
321                         }
322                         if ($mode == 'normal') {
323                             if ($sco = scorm_get_sco($scoid)) {
324                                 if (isset($sco->mastery_score) && is_numeric($sco->mastery_score)) {
325                                     if ($score != '') { // Score is correctly initialized w/ an empty string, see above.
326                                         if ($score >= trim($sco->mastery_score)) {
327                                             $lessonstatus = 'passed';
328                                         } else {
329                                             $lessonstatus = 'failed';
330                                         }
331                                     }
332                                 }
333                                 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
334                                                             $attempt, 'cmi.core.lesson_status', $lessonstatus);
335                             }
336                         }
337                     }
338                     echo "error=0\r\nerror_text=Successful\r\n";
339                 } else if ($status == 'Terminated') {
340                     echo "error=1\r\nerror_text=Terminated\r\n";
341                 } else {
342                     echo "error=1\r\nerror_text=Not Initialized\r\n";
343                 }
344             break;
345             case 'putcomments':
346                 if ($status == 'Running') {
347                     echo "error=0\r\nerror_text=Successful\r\n";
348                 } else if ($status == 'Terminated') {
349                     echo "error=1\r\nerror_text=Terminated\r\n";
350                 } else {
351                     echo "error=1\r\nerror_text=Not Initialized\r\n";
352                 }
353             break;
354             case 'putinteractions':
355                 if ($status == 'Running') {
356                     echo "error=0\r\nerror_text=Successful\r\n";
357                 } else if ($status == 'Terminated') {
358                     echo "error=1\r\nerror_text=Terminated\r\n";
359                 } else {
360                     echo "error=1\r\nerror_text=Not Initialized\r\n";
361                 }
362             break;
363             case 'putobjectives':
364                 if ($status == 'Running') {
365                     echo "error=0\r\nerror_text=Successful\r\n";
366                 } else if ($status == 'Terminated') {
367                     echo "error=1\r\nerror_text=Terminated\r\n";
368                 } else {
369                     echo "error=1\r\nerror_text=Not Initialized\r\n";
370                 }
371             break;
372             case 'putpath':
373                 if ($status == 'Running') {
374                     echo "error=0\r\nerror_text=Successful\r\n";
375                 } else if ($status == 'Terminated') {
376                     echo "error=1\r\nerror_text=Terminated\r\n";
377                 } else {
378                     echo "error=1\r\nerror_text=Not Initialized\r\n";
379                 }
380             break;
381             case 'putperformance':
382                 if ($status == 'Running') {
383                     echo "error=0\r\nerror_text=Successful\r\n";
384                 } else if ($status == 'Terminated') {
385                     echo "error=1\r\nerror_text=Terminated\r\n";
386                 } else {
387                     echo "error=1\r\nerror_text=Not Initialized\r\n";
388                 }
389             break;
390             case 'exitau':
391                 if ($status == 'Running') {
392                     if (isset($scormsession->sessiontime) && ($scormsession->sessiontime != '')) {
393                         if ($track = $DB->get_record('scorm_scoes_track', array("userid" => $aiccuser->id,
394                                                                                 "scormid" => $scorm->id,
395                                                                                 "scoid" => $sco->id,
396                                                                                 "attempt" => $attempt,
397                                                                                 "element" => 'cmi.core.total_time'))) {
398                             // Add session_time to total_time.
399                             $value = scorm_add_time($track->value, $scormsession->sessiontime);
400                             $track->value = $value;
401                             $track->timemodified = time();
402                             $DB->update_record('scorm_scoes_track', $track);
403                         } else {
404                             $track = new stdClass();
405                             $track->userid = $aiccuser->id;
406                             $track->scormid = $scorm->id;
407                             $track->scoid = $sco->id;
408                             $track->element = 'cmi.core.total_time';
409                             $track->value = $scormsession->sessiontime;
410                             $track->attempt = $attempt;
411                             $track->timemodified = time();
412                             $id = $DB->insert_record('scorm_scoes_track', $track);
413                         }
414                         scorm_update_grades($scorm, $aiccuser->id);
415                     }
416                     $scormsession->scormstatus = 'Terminated';
417                     $scormsession->session_time = '';
418                     echo "error=0\r\nerror_text=Successful\r\n";
419                 } else if ($status == 'Terminated') {
420                     echo "error=1\r\nerror_text=Terminated\r\n";
421                 } else {
422                     echo "error=1\r\nerror_text=Not Initialized\r\n";
423                 }
424             break;
425             default:
426                 echo "error=1\r\nerror_text=Invalid Command\r\n";
427             break;
428         }
429     }
430 } else {
431     if (empty($command)) {
432         echo "error=1\r\nerror_text=Invalid Command\r\n";
433     } else {
434         echo "error=3\r\nerror_text=Invalid Session ID\r\n";
435     }
437 if (empty($cfgscorm->allowaicchacp)) {
438     $SESSION->scorm = $scormsession;
439 } else {
440     $scormsession->timemodified = time();
441     $DB->update_record('scorm_aicc_session', $scormsession);
444 $aiccresponse = ob_get_contents();
445 scorm_debug_log_write("aicc", "HACP Response:\r\n$aiccresponse", $scoid);
446 ob_end_flush();