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