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