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