MDL-61899 tool_dataprivacy: Create name-description exporter
[moodle.git] / mod / scorm / aicc.php
blob8902fd26577056f76bb5473736534ff4e8d7191c
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.
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;
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);
42 $PAGE->set_url($url);
44 if (empty($cfgscorm->allowaicchacp)) {
45 require_login();
46 if (!confirm_sesskey($sessionid)) {
47 print_error('invalidsesskey');
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');
56 $aiccuser = $DB->get_record('user', array('id' => $scormsession->userid), 'id,username,lastname,firstname', MUST_EXIST);
59 if (!empty($command)) {
60 $command = strtolower($command);
62 if (isset($scormsession->scoid)) {
63 $scoid = $scormsession->scoid;
64 } else {
65 print_error('cannotcallscript');
67 $mode = 'normal';
68 if (isset($scormsession->scormmode)) {
69 $mode = $scormsession->scormmode;
71 $status = 'Not Initialized';
72 if (isset($scormsession->scormstatus)) {
73 $status = $scormsession->scormstatus;
75 if (isset($scormsession->attempt)) {
76 $attempt = $scormsession->attempt;
77 } else {
78 $attempt = 1;
81 if ($sco = scorm_get_sco($scoid, SCO_ONLY)) {
82 if (!$scorm = $DB->get_record('scorm', array('id' => $sco->scorm))) {
83 print_error('cannotcallscript');
85 } else {
86 print_error('cannotcallscript');
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';
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 = '';
114 $aiccuserid = get_config('scorm', 'aiccuserid');
115 if (!empty($aiccuserid)) {
116 $userdata->student_id = $aiccuser->id;
117 } else {
118 $userdata->student_id = $aiccuser->username;
120 $userdata->student_name = $aiccuser->lastname .', '. $aiccuser->firstname;
121 $userdata->mode = $mode;
122 if ($userdata->mode == 'normal') {
123 $userdata->credit = 'credit';
124 } else {
125 $userdata->credit = 'no-credit';
128 if ($sco = scorm_get_sco($scoid)) {
129 $userdata->course_id = $sco->identifier;
130 $userdata->datafromlms = isset($sco->datafromlms) ? $sco->datafromlms : '';
131 $userdata->mastery_score = isset($sco->mastery_score) && is_numeric($sco->mastery_score) ?
132 trim($sco->mastery_score) : '';
133 $userdata->max_time_allowed = isset($sco->max_time_allowed) ? $sco->max_time_allowed : '';
134 $userdata->time_limit_action = isset($sco->time_limit_action) ? $sco->time_limit_action : '';
136 echo "error=0\r\nerror_text=Successful\r\naicc_data=";
137 echo "[Core]\r\n";
138 echo 'Student_ID='.$userdata->student_id."\r\n";
139 echo 'Student_Name='.$userdata->student_name."\r\n";
140 if (isset($userdata->{'cmi.core.lesson_location'})) {
141 echo 'Lesson_Location='.$userdata->{'cmi.core.lesson_location'}."\r\n";
142 } else {
143 echo 'Lesson_Location='."\r\n";
145 echo 'Credit='.$userdata->credit."\r\n";
146 if (isset($userdata->status)) {
147 if ($userdata->status == '') {
148 $userdata->entry = ', ab-initio';
149 } else {
150 if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
151 $userdata->entry = ', resume';
152 } else {
153 $userdata->entry = '';
157 if (isset($userdata->{'cmi.core.lesson_status'})) {
158 echo 'Lesson_Status='.$userdata->{'cmi.core.lesson_status'}.$userdata->entry."\r\n";
159 $scormsession->scorm_lessonstatus = $userdata->{'cmi.core.lesson_status'};
160 } else {
161 echo 'Lesson_Status=not attempted'.$userdata->entry."\r\n";
162 $scormsession->scorm_lessonstatus = 'not attempted';
164 if (isset($userdata->{'cmi.core.score.raw'})) {
165 $max = '';
166 $min = '';
167 if (isset($userdata->{'cmi.core.score.max'}) && !empty($userdata->{'cmi.core.score.max'})) {
168 $max = ', '.$userdata->{'cmi.core.score.max'};
169 if (isset($userdata->{'cmi.core.score.min'}) && !empty($userdata->{'cmi.core.score.min'})) {
170 $min = ', '.$userdata->{'cmi.core.score.min'};
173 echo 'Score='.$userdata->{'cmi.core.score.raw'}.$max.$min."\r\n";
174 } else {
175 echo 'Score='."\r\n";
177 if (isset($userdata->{'cmi.core.total_time'})) {
178 echo 'Time='.$userdata->{'cmi.core.total_time'}."\r\n";
179 } else {
180 echo 'Time='.'00:00:00'."\r\n";
182 echo 'Lesson_Mode='.$userdata->mode."\r\n";
183 if (isset($userdata->{'cmi.suspend_data'})) {
184 echo "[Core_Lesson]\r\n".rawurldecode($userdata->{'cmi.suspend_data'})."\r\n";
185 } else {
186 echo "[Core_Lesson]\r\n";
188 echo "[Core_Vendor]\r\n".$userdata->datafromlms."\r\n";
189 echo "[Evaluation]\r\nCourse_ID = {".$userdata->course_id."}\r\n";
190 echo "[Student_Data]\r\n";
191 echo 'Mastery_Score='.$userdata->mastery_score."\r\n";
192 echo 'Max_Time_Allowed='.$userdata->max_time_allowed."\r\n";
193 echo 'Time_Limit_Action='.$userdata->time_limit_action."\r\n";
194 } else {
195 print_error('cannotfindsco', 'scorm');
198 break;
199 case 'putparam':
200 if ($status == 'Running') {
201 if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $scorm->course)) {
202 echo "error=1\r\nerror_text=Unknown\r\n"; // No one must see this error message if not hacked.
204 $savetrack = has_capability('mod/scorm:savetrack', context_module::instance($cm->id), $aiccuser->id);
205 if (!empty($aiccdata) && $savetrack) {
206 $initlessonstatus = 'not attempted';
207 $lessonstatus = 'not attempted';
208 if (isset($scormsession->scorm_lessonstatus)) {
209 $initlessonstatus = $scormsession->scorm_lessonstatus;
211 $score = '';
212 $datamodel['lesson_location'] = 'cmi.core.lesson_location';
213 $datamodel['lesson_status'] = 'cmi.core.lesson_status';
214 $datamodel['score'] = 'cmi.core.score.raw';
215 $datamodel['time'] = 'cmi.core.session_time';
216 $datamodel['[core_lesson]'] = 'cmi.suspend_data';
217 $datamodel['[comments]'] = 'cmi.comments';
218 $datarows = explode("\r\n", $aiccdata);
219 reset($datarows);
220 foreach ($datarows as $datarow) {
221 if (($equal = strpos($datarow, '=')) !== false) {
222 $element = strtolower(trim(substr($datarow, 0, $equal)));
223 $value = trim(substr($datarow, $equal + 1));
224 if (isset($datamodel[$element])) {
225 $element = $datamodel[$element];
226 switch ($element) {
227 case 'cmi.core.lesson_location':
228 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
229 $attempt, $element, $value);
230 break;
231 case 'cmi.core.lesson_status':
232 $statuses = array(
233 'passed' => 'passed',
234 'completed' => 'completed',
235 'failed' => 'failed',
236 'incomplete' => 'incomplete',
237 'browsed' => 'browsed',
238 'not attempted' => 'not attempted',
239 'p' => 'passed',
240 'c' => 'completed',
241 'f' => 'failed',
242 'i' => 'incomplete',
243 'b' => 'browsed',
244 'n' => 'not attempted'
246 $exites = array(
247 'logout' => 'logout',
248 'time-out' => 'time-out',
249 'suspend' => 'suspend',
250 'l' => 'logout',
251 't' => 'time-out',
252 's' => 'suspend',
254 $values = explode(',', $value);
255 $value = '';
256 if (count($values) > 1) {
257 $value = trim(strtolower($values[1]));
258 $value = $value[0];
259 if (isset($exites[$value])) {
260 $value = $exites[$value];
263 if (empty($value) || isset($exites[$value])) {
264 $subelement = 'cmi.core.exit';
265 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
266 $attempt, $subelement, $value);
268 $value = trim(strtolower($values[0]));
269 $value = $value[0];
270 if (isset($statuses[$value]) && ($mode == 'normal')) {
271 $value = $statuses[$value];
272 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
273 $attempt, $element, $value);
275 $lessonstatus = $value;
276 break;
277 case 'cmi.core.score.raw':
278 $values = explode(',', $value);
279 if ((count($values) > 1) && ($values[1] >= $values[0]) && is_numeric($values[1])) {
280 $subelement = 'cmi.core.score.max';
281 $value = trim($values[1]);
282 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
283 $attempt, $subelement, $value);
284 if ((count($values) == 3) && ($values[2] <= $values[0]) && is_numeric($values[2])) {
285 $subelement = 'cmi.core.score.min';
286 $value = trim($values[2]);
287 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
288 $attempt, $subelement, $value);
292 $value = '';
293 if (is_numeric($values[0])) {
294 $value = trim($values[0]);
295 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
296 $attempt, $element, $value);
298 $score = $value;
299 break;
300 case 'cmi.core.session_time':
301 $scormsession->sessiontime = $value;
302 break;
305 } else {
306 if (isset($datamodel[strtolower(trim($datarow))])) {
307 $element = $datamodel[strtolower(trim($datarow))];
308 $value = '';
309 while ((($datarow = current($datarows)) !== false) && (substr($datarow, 0, 1) != '[')) {
310 $value .= $datarow."\r\n";
311 next($datarows);
313 $value = rawurlencode($value);
314 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id, $attempt, $element, $value);
318 if (($mode == 'browse') && ($initlessonstatus == 'not attempted')) {
319 $lessonstatus = 'browsed';
320 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
321 $attempt, 'cmi.core.lesson_status', 'browsed');
323 if ($mode == 'normal') {
324 if ($sco = scorm_get_sco($scoid)) {
325 if (isset($sco->mastery_score) && is_numeric($sco->mastery_score)) {
326 if ($score != '') { // Score is correctly initialized w/ an empty string, see above.
327 if ($score >= trim($sco->mastery_score)) {
328 $lessonstatus = 'passed';
329 } else {
330 $lessonstatus = 'failed';
334 $id = scorm_insert_track($aiccuser->id, $scorm->id, $sco->id,
335 $attempt, 'cmi.core.lesson_status', $lessonstatus);
339 echo "error=0\r\nerror_text=Successful\r\n";
340 } else if ($status == 'Terminated') {
341 echo "error=1\r\nerror_text=Terminated\r\n";
342 } else {
343 echo "error=1\r\nerror_text=Not Initialized\r\n";
345 break;
346 case 'putcomments':
347 if ($status == 'Running') {
348 echo "error=0\r\nerror_text=Successful\r\n";
349 } else if ($status == 'Terminated') {
350 echo "error=1\r\nerror_text=Terminated\r\n";
351 } else {
352 echo "error=1\r\nerror_text=Not Initialized\r\n";
354 break;
355 case 'putinteractions':
356 if ($status == 'Running') {
357 echo "error=0\r\nerror_text=Successful\r\n";
358 } else if ($status == 'Terminated') {
359 echo "error=1\r\nerror_text=Terminated\r\n";
360 } else {
361 echo "error=1\r\nerror_text=Not Initialized\r\n";
363 break;
364 case 'putobjectives':
365 if ($status == 'Running') {
366 echo "error=0\r\nerror_text=Successful\r\n";
367 } else if ($status == 'Terminated') {
368 echo "error=1\r\nerror_text=Terminated\r\n";
369 } else {
370 echo "error=1\r\nerror_text=Not Initialized\r\n";
372 break;
373 case 'putpath':
374 if ($status == 'Running') {
375 echo "error=0\r\nerror_text=Successful\r\n";
376 } else if ($status == 'Terminated') {
377 echo "error=1\r\nerror_text=Terminated\r\n";
378 } else {
379 echo "error=1\r\nerror_text=Not Initialized\r\n";
381 break;
382 case 'putperformance':
383 if ($status == 'Running') {
384 echo "error=0\r\nerror_text=Successful\r\n";
385 } else if ($status == 'Terminated') {
386 echo "error=1\r\nerror_text=Terminated\r\n";
387 } else {
388 echo "error=1\r\nerror_text=Not Initialized\r\n";
390 break;
391 case 'exitau':
392 if ($status == 'Running') {
393 if (isset($scormsession->sessiontime) && ($scormsession->sessiontime != '')) {
394 if ($track = $DB->get_record('scorm_scoes_track', array("userid" => $aiccuser->id,
395 "scormid" => $scorm->id,
396 "scoid" => $sco->id,
397 "attempt" => $attempt,
398 "element" => 'cmi.core.total_time'))) {
399 // Add session_time to total_time.
400 $value = scorm_add_time($track->value, $scormsession->sessiontime);
401 $track->value = $value;
402 $track->timemodified = time();
403 $DB->update_record('scorm_scoes_track', $track);
404 } else {
405 $track = new stdClass();
406 $track->userid = $aiccuser->id;
407 $track->scormid = $scorm->id;
408 $track->scoid = $sco->id;
409 $track->element = 'cmi.core.total_time';
410 $track->value = $scormsession->sessiontime;
411 $track->attempt = $attempt;
412 $track->timemodified = time();
413 $id = $DB->insert_record('scorm_scoes_track', $track);
415 scorm_update_grades($scorm, $aiccuser->id);
417 $scormsession->scormstatus = 'Terminated';
418 $scormsession->session_time = '';
419 echo "error=0\r\nerror_text=Successful\r\n";
420 } else if ($status == 'Terminated') {
421 echo "error=1\r\nerror_text=Terminated\r\n";
422 } else {
423 echo "error=1\r\nerror_text=Not Initialized\r\n";
425 break;
426 default:
427 echo "error=1\r\nerror_text=Invalid Command\r\n";
428 break;
431 } else {
432 if (empty($command)) {
433 echo "error=1\r\nerror_text=Invalid Command\r\n";
434 } else {
435 echo "error=3\r\nerror_text=Invalid Session ID\r\n";
438 if (empty($cfgscorm->allowaicchacp)) {
439 $SESSION->scorm = $scormsession;
440 } else {
441 $scormsession->timemodified = time();
442 $DB->update_record('scorm_aicc_session', $scormsession);
445 $aiccresponse = ob_get_contents();
446 scorm_debug_log_write("aicc", "HACP Response:\r\n$aiccresponse", $scoid);
447 ob_end_flush();