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