SCORM MDL-29159 - override getContentHtml function in treeview so that it doesn't...
[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 $url = new moodle_url('/mod/scorm/aicc.php', array('command'=>$command, 'session_id'=>$sessionid));
37 if ($aiccdata !== 0) {
38     $url->param('aicc_data', $aiccdata);
39 }
40 $PAGE->set_url($url);
42 require_login();
44 if (!empty($command) && confirm_sesskey($sessionid)) {
45     $command = strtolower($command);
47     if (isset($SESSION->scorm_scoid)) {
48         $scoid = $SESSION->scorm_scoid;
49     } else {
50         print_error('cannotcallscript');
51     }
52     $mode = 'normal';
53     if (isset($SESSION->scorm_mode)) {
54         $mode = $SESSION->scorm_mode;
55     }
56     $status = 'Not Initialized';
57     if (isset($SESSION->scorm_status)) {
58         $status = $SESSION->scorm_status;
59     }
60     if (isset($SESSION->scorm_attempt)) {
61         $attempt = $SESSION->scorm_attempt;
62     } else {
63         $attempt = 1;
64     }
66     if ($sco = scorm_get_sco($scoid, SCO_ONLY)) {
67         if (!$scorm = $DB->get_record('scorm', array('id'=>$sco->scorm))) {
68             print_error('cannotcallscript');
69         }
70     } else {
71         print_error('cannotcallscript');
72     }
73     $aiccrequest = "MOODLE scoid: $scoid"
74                  . "\r\nMOODLE mode: $mode"
75                  . "\r\nMOODLE status: $status"
76                  . "\r\nMOODLE attempt: $attempt"
77                  . "\r\nAICC sessionid: $sessionid"
78                  . "\r\nAICC command: $command"
79                  . "\r\nAICC aiccdata:\r\n$aiccdata";
80     scorm_debug_log_write("aicc", "HACP Request:\r\n$aiccrequest", $scoid);
81     ob_start();
83     if ($scorm = $DB->get_record('scorm', array('id'=>$sco->scorm))) {
84         switch ($command) {
85             case 'getparam':
86                 if ($status == 'Not Initialized') {
87                     $SESSION->scorm_status = 'Running';
88                     $status = 'Running';
89                 }
90                 if ($status != 'Running') {
91                     echo "error=101\r\nerror_text=Terminated\r\n";
92                 } else {
93                     if ($usertrack=scorm_get_tracks($scoid, $USER->id, $attempt)) {
94                         $userdata = $usertrack;
95                     } else {
96                         $userdata->status = '';
97                         $userdata->score_raw = '';
98                     }
99                     $userdata->student_id = $USER->username;
100                     $userdata->student_name = $USER->lastname .', '. $USER->firstname;
101                     $userdata->mode = $mode;
102                     if ($userdata->mode == 'normal') {
103                         $userdata->credit = 'credit';
104                     } else {
105                         $userdata->credit = 'no-credit';
106                     }
108                     if ($sco = scorm_get_sco($scoid)) {
109                         $userdata->course_id = $sco->identifier;
110                         $userdata->datafromlms = isset($sco->datafromlms)?$sco->datafromlms:'';
111                         $userdata->mastery_score = isset($sco->mastery_score) && is_numeric($sco->mastery_score)?trim($sco->mastery_score):'';
112                         $userdata->max_time_allowed = isset($sco->max_time_allowed)?$sco->max_time_allowed:'';
113                         $userdata->time_limit_action = isset($sco->time_limit_action)?$sco->time_limit_action:'';
115                         echo "error=0\r\nerror_text=Successful\r\naicc_data=";
116                         echo "[Core]\r\n";
117                         echo 'Student_ID='.$userdata->student_id."\r\n";
118                         echo 'Student_Name='.$userdata->student_name."\r\n";
119                         if (isset($userdata->{'cmi.core.lesson_location'})) {
120                             echo 'Lesson_Location='.$userdata->{'cmi.core.lesson_location'}."\r\n";
121                         } else {
122                             echo 'Lesson_Location='."\r\n";
123                         }
124                         echo 'Credit='.$userdata->credit."\r\n";
125                         if (isset($userdata->status)) {
126                             if ($userdata->status == '') {
127                                 $userdata->entry = ', ab-initio';
128                             } else {
129                                 if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
130                                     $userdata->entry = ', resume';
131                                 } else {
132                                     $userdata->entry = '';
133                                 }
134                             }
135                         }
136                         if (isset($userdata->{'cmi.core.lesson_status'})) {
137                             echo 'Lesson_Status='.$userdata->{'cmi.core.lesson_status'}.$userdata->entry."\r\n";
138                             $SESSION->scorm_lessonstatus = $userdata->{'cmi.core.lesson_status'};
139                         } else {
140                             echo 'Lesson_Status=not attempted'.$userdata->entry."\r\n";
141                             $SESSION->scorm_lessonstatus = 'not attempted';
142                         }
143                         if (isset($userdata->{'cmi.core.score.raw'})) {
144                             $max = '';
145                             $min = '';
146                             if (isset($userdata->{'cmi.core.score.max'}) && !empty($userdata->{'cmi.core.score.max'})) {
147                                 $max = ', '.$userdata->{'cmi.core.score.max'};
148                                 if (isset($userdata->{'cmi.core.score.min'}) && !empty($userdata->{'cmi.core.score.min'})) {
149                                     $min = ', '.$userdata->{'cmi.core.score.min'};
150                                 }
151                             }
152                             echo 'Score='.$userdata->{'cmi.core.score.raw'}.$max.$min."\r\n";
153                         } else {
154                             echo 'Score='."\r\n";
155                         }
156                         if (isset($userdata->{'cmi.core.total_time'})) {
157                             echo 'Time='.$userdata->{'cmi.core.total_time'}."\r\n";
158                         } else {
159                             echo 'Time='.'00:00:00'."\r\n";
160                         }
161                         echo 'Lesson_Mode='.$userdata->mode."\r\n";
162                         if (isset($userdata->{'cmi.suspend_data'})) {
163                             echo "[Core_Lesson]\r\n".rawurldecode($userdata->{'cmi.suspend_data'})."\r\n";
164                         } else {
165                             echo "[Core_Lesson]\r\n";
166                         }
167                         echo "[Core_Vendor]\r\n".$userdata->datafromlms."\r\n";
168                         echo "[Evaluation]\r\nCourse_ID = {".$userdata->course_id."}\r\n";
169                         echo "[Student_Data]\r\n";
170                         echo 'Mastery_Score='.$userdata->mastery_score."\r\n";
171                         echo 'Max_Time_Allowed='.$userdata->max_time_allowed."\r\n";
172                         echo 'Time_Limit_Action='.$userdata->time_limit_action."\r\n";
173                     } else {
174                         print_error('cannotfindsco', 'scorm');
175                     }
176                 }
177             break;
178             case 'putparam':
179                 if ($status == 'Running') {
180                     if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $scorm->course)) {
181                         echo "error=1\r\nerror_text=Unknown\r\n"; // No one must see this error message if not hacked
182                     }
183                     if (!empty($aiccdata) && has_capability('mod/scorm:savetrack', get_context_instance(CONTEXT_MODULE, $cm->id))) {
184                         $initlessonstatus = 'not attempted';
185                         $lessonstatus = 'not attempted';
186                         if (isset($SESSION->scorm_lessonstatus)) {
187                             $initlessonstatus = $SESSION->scorm_lessonstatus;
188                         }
189                         $score = '';
190                         $datamodel['lesson_location'] = 'cmi.core.lesson_location';
191                         $datamodel['lesson_status'] = 'cmi.core.lesson_status';
192                         $datamodel['score'] = 'cmi.core.score.raw';
193                         $datamodel['time'] = 'cmi.core.session_time';
194                         $datamodel['[core_lesson]'] = 'cmi.suspend_data';
195                         $datamodel['[comments]'] = 'cmi.comments';
196                         $datarows = explode("\r\n", $aiccdata);
197                         reset($datarows);
198                         while ((list(, $datarow) = each($datarows)) !== false) {
199                             if (($equal = strpos($datarow, '=')) !== false) {
200                                 $element = strtolower(trim(substr($datarow, 0, $equal)));
201                                 $value = trim(substr($datarow, $equal+1));
202                                 if (isset($datamodel[$element])) {
203                                     $element = $datamodel[$element];
204                                     switch ($element) {
205                                         case 'cmi.core.lesson_location':
206                                             $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
207                                         break;
208                                         case 'cmi.core.lesson_status':
209                                             $statuses = array(
210                                                        'passed' => 'passed',
211                                                        'completed' => 'completed',
212                                                        'failed' => 'failed',
213                                                        'incomplete' => 'incomplete',
214                                                        'browsed' => 'browsed',
215                                                        'not attempted' => 'not attempted',
216                                                        'p' => 'passed',
217                                                        'c' => 'completed',
218                                                        'f' => 'failed',
219                                                        'i' => 'incomplete',
220                                                        'b' => 'browsed',
221                                                        'n' => 'not attempted'
222                                                        );
223                                             $exites = array(
224                                                        'logout' => 'logout',
225                                                        'time-out' => 'time-out',
226                                                        'suspend' => 'suspend',
227                                                        'l' => 'logout',
228                                                        't' => 'time-out',
229                                                        's' => 'suspend',
230                                                        );
231                                             $values = explode(',', $value);
232                                             $value = '';
233                                             if (count($values) > 1) {
234                                                 $value = trim(strtolower($values[1]));
235                                                 $value = $value[0];
236                                                 if (isset($exites[$value])) {
237                                                     $value = $exites[$value];
238                                                 }
239                                             }
240                                             if (empty($value) || isset($exites[$value])) {
241                                                 $subelement = 'cmi.core.exit';
242                                                 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $subelement, $value);
243                                             }
244                                             $value = trim(strtolower($values[0]));
245                                             $value = $value[0];
246                                             if (isset($statuses[$value]) && ($mode == 'normal')) {
247                                                 $value = $statuses[$value];
248                                                 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
249                                             }
250                                             $lessonstatus = $value;
251                                         break;
252                                         case 'cmi.core.score.raw':
253                                             $values = explode(',', $value);
254                                             if ((count($values) > 1) && ($values[1] >= $values[0]) && is_numeric($values[1])) {
255                                                 $subelement = 'cmi.core.score.max';
256                                                 $value = trim($values[1]);
257                                                 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $subelement, $value);
258                                                 if ((count($values) == 3) && ($values[2] <= $values[0]) && is_numeric($values[2])) {
259                                                     $subelement = 'cmi.core.score.min';
260                                                     $value = trim($values[2]);
261                                                     $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $subelement, $value);
262                                                 }
263                                             }
265                                             $value = '';
266                                             if (is_numeric($values[0])) {
267                                                 $value = trim($values[0]);
268                                                 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
269                                             }
270                                             $score = $value;
271                                         break;
272                                         case 'cmi.core.session_time':
273                                              $SESSION->scorm_session_time = $value;
274                                         break;
275                                     }
276                                 }
277                             } else {
278                                 if (isset($datamodel[strtolower(trim($datarow))])) {
279                                     $element = $datamodel[strtolower(trim($datarow))];
280                                     $value = '';
281                                     while ((($datarow = current($datarows)) !== false) && (substr($datarow, 0, 1) != '[')) {
282                                         $value .= $datarow."\r\n";
283                                         next($datarows);
284                                     }
285                                     $value = rawurlencode($value);
286                                     $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
287                                 }
288                             }
289                         }
290                         if (($mode == 'browse') && ($initlessonstatus == 'not attempted')) {
291                             $lessonstatus = 'browsed';
292                             $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, 'cmi.core.lesson_status', 'browsed');
293                         }
294                         if ($mode == 'normal') {
295                             if ($sco = scorm_get_sco($scoid)) {
296                                 if (isset($sco->mastery_score) && is_numeric($sco->mastery_score)) {
297                                     if ($score != '') { // $score is correctly initialized w/ an empty string, see above
298                                         if ($score >= trim($sco->mastery_score)) {
299                                             $lessonstatus = 'passed';
300                                         } else {
301                                             $lessonstatus = 'failed';
302                                         }
303                                     }
304                                 }
305                                 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, 'cmi.core.lesson_status', $lessonstatus);
306                             }
307                         }
308                     }
309                     echo "error=0\r\nerror_text=Successful\r\n";
310                 } else if ($status == 'Terminated') {
311                     echo "error=1\r\nerror_text=Terminated\r\n";
312                 } else {
313                     echo "error=1\r\nerror_text=Not Initialized\r\n";
314                 }
315             break;
316             case 'putcomments':
317                 if ($status == 'Running') {
318                     echo "error=0\r\nerror_text=Successful\r\n";
319                 } else if ($status == 'Terminated') {
320                     echo "error=1\r\nerror_text=Terminated\r\n";
321                 } else {
322                     echo "error=1\r\nerror_text=Not Initialized\r\n";
323                 }
324             break;
325             case 'putinteractions':
326                 if ($status == 'Running') {
327                     echo "error=0\r\nerror_text=Successful\r\n";
328                 } else if ($status == 'Terminated') {
329                     echo "error=1\r\nerror_text=Terminated\r\n";
330                 } else {
331                     echo "error=1\r\nerror_text=Not Initialized\r\n";
332                 }
333             break;
334             case 'putobjectives':
335                 if ($status == 'Running') {
336                     echo "error=0\r\nerror_text=Successful\r\n";
337                 } else if ($status == 'Terminated') {
338                     echo "error=1\r\nerror_text=Terminated\r\n";
339                 } else {
340                     echo "error=1\r\nerror_text=Not Initialized\r\n";
341                 }
342             break;
343             case 'putpath':
344                 if ($status == 'Running') {
345                     echo "error=0\r\nerror_text=Successful\r\n";
346                 } else if ($status == 'Terminated') {
347                     echo "error=1\r\nerror_text=Terminated\r\n";
348                 } else {
349                     echo "error=1\r\nerror_text=Not Initialized\r\n";
350                 }
351             break;
352             case 'putperformance':
353                 if ($status == 'Running') {
354                     echo "error=0\r\nerror_text=Successful\r\n";
355                 } else if ($status == 'Terminated') {
356                     echo "error=1\r\nerror_text=Terminated\r\n";
357                 } else {
358                     echo "error=1\r\nerror_text=Not Initialized\r\n";
359                 }
360             break;
361             case 'exitau':
362                 if ($status == 'Running') {
363                     if (isset($SESSION->scorm_session_time) && ($SESSION->scorm_session_time != '')) {
364                         if ($track = $DB->get_record('scorm_scoes_track', array("userid"=>$USER->id,
365                                                                                 "scormid"=>$scorm->id,
366                                                                                 "scoid"=>$sco->id,
367                                                                                 "attempt"=>$attempt,
368                                                                                 "element"=>'cmi.core.total_time'))) {
369                             // Add session_time to total_time
370                             $value = scorm_add_time($track->value, $SESSION->scorm_session_time);
371                             $track->value = $value;
372                             $track->timemodified = time();
373                             $DB->update_record('scorm_scoes_track', $track);
374                         } else {
375                             $track = new stdClass();
376                             $track->userid = $USER->id;
377                             $track->scormid = $scorm->id;
378                             $track->scoid = $sco->id;
379                             $track->element = 'cmi.core.total_time';
380                             $track->value = $SESSION->scorm_session_time;
381                             $track->attempt = $attempt;
382                             $track->timemodified = time();
383                             $id = $DB->insert_record('scorm_scoes_track', $track);
384                         }
385                         scorm_update_grades($scorm, $USER->id);
386                     }
387                     $SESSION->scorm_status = 'Terminated';
388                     $SESSION->scorm_session_time = '';
389                     echo "error=0\r\nerror_text=Successful\r\n";
390                 } else if ($status == 'Terminated') {
391                     echo "error=1\r\nerror_text=Terminated\r\n";
392                 } else {
393                     echo "error=1\r\nerror_text=Not Initialized\r\n";
394                 }
395             break;
396             default:
397                 echo "error=1\r\nerror_text=Invalid Command\r\n";
398             break;
399         }
400     }
401 } else {
402     if (empty($command)) {
403         echo "error=1\r\nerror_text=Invalid Command\r\n";
404     } else {
405         echo "error=3\r\nerror_text=Invalid Session ID\r\n";
406     }
408 $aiccresponse = ob_get_contents();
409 scorm_debug_log_write("aicc", "HACP Response:\r\n$aiccresponse", $scoid);
410 ob_end_flush();