Added ability to reset page to defaults. bug #4123
[moodle-linuxchix.git] / mod / scorm / aicc.php
blob6c61e6b525a169b0abb27da42a921948a2b5c743
1 <?php
2 require_once('../../config.php');
3 require_once('locallib.php');
5 $command = required_param('command', '', PARAM_ALPHA);
6 $sessionid = required_param('session_id', '', PARAM_ALPHANUM);
7 $aiccdata = optional_param('aicc_data', '', PARAM_RAW);
9 require_login();
11 if (!empty($command) && confirm_sesskey($sessionid)) {
12 $command = strtolower($command);
14 if (isset($SESSION->scorm_scoid)) {
15 $scoid = $SESSION->scorm_scoid;
16 } else {
17 error('Invalid script call');
19 $mode = 'normal';
20 if (isset($SESSION->scorm_mode)) {
21 $mode = $SESSION->scorm_mode;
23 $status = 'Not Initialized';
24 if (isset($SESSION->scorm_status)) {
25 $status = $SESSION->scorm_status;
27 if (isset($SESSION->attempt)) {
28 $attempt = $SESSION->attempt;
29 } else {
30 $attempt = 1;
33 if ($sco = get_record('scorm_scoes','id',$scoid)) {
34 if (!$scorm = get_record('scorm','id',$sco->scorm)) {
35 error('Invalid script call');
37 } else {
38 error('Invalid script call');
41 if ($scorm = get_record('scorm','id',$sco->scorm)) {
42 switch ($command) {
43 case 'getparam':
44 if ($status == 'Not Initialized') {
45 $SESSION->scorm_status = 'Running';
46 $status = 'Running';
48 if ($status != 'Running') {
49 echo "error = 101\nerror_text = Terminated\n";
50 } else {
51 if ($usertrack=scorm_get_tracks($scoid,$USER->id,$attempt)) {
52 $userdata = $usertrack;
53 } else {
54 $userdata->status = '';
55 $userdata->score_raw = '';
57 $userdata->student_id = $USER->username;
58 $userdata->student_name = $USER->lastname .', '. $USER->firstname;
59 $userdata->mode = $mode;
60 if ($userdata->mode == 'normal') {
61 $userdata->credit = 'credit';
62 } else {
63 $userdata->credit = 'no-credit';
66 if ($sco = get_record('scorm_scoes','id',$scoid)) {
67 $userdata->course_id = $sco->identifier;
68 $userdata->datafromlms = $sco->datafromlms;
69 $userdata->masteryscore = $sco->masteryscore;
70 $userdata->maxtimeallowed = $sco->maxtimeallowed;
71 $userdata->timelimitaction = $sco->timelimitaction;
73 echo "error = 0\nerror_text = Successful\naicc_data=\n";
74 echo "[Core]\n";
75 echo 'Student_ID = '.$userdata->student_id."\n";
76 echo 'Student_Name = '.$userdata->student_name."\n";
77 if (isset($userdata->{'cmi.core.lesson_location'})) {
78 echo 'Lesson_Location = '.$userdata->{'cmi.core.lesson_location'}."\n";
79 } else {
80 echo 'Lesson_Location = '."\n";
82 echo 'Credit = '.$userdata->credit."\n";
83 if (isset($userdata->status)) {
84 if ($userdata->status == '') {
85 $userdata->entry = ', ab-initio';
86 } else {
87 if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
88 $userdata->entry = ', resume';
89 } else {
90 $userdata->entry = '';
94 if (isset($userdata->{'cmi.core.lesson_status'})) {
95 echo 'Lesson_Status = '.$userdata->{'cmi.core.lesson_status'}.$userdata->entry."\n";
96 $SESSION->scorm_lessonstatus = $userdata->{'cmi.core.lesson_status'};
97 } else {
98 echo 'Lesson_Status = not attempted'.$userdata->entry."\n";
99 $SESSION->scorm_lessonstatus = 'not attempted';
101 if (isset($userdata->{'cmi.core.score.raw'})) {
102 $max = '';
103 $min = '';
104 if (isset($userdata->{'cmi.core.score.max'}) && !empty($userdata->{'cmi.core.score.max'})) {
105 $max = ', '.$userdata->{'cmi.core.score.max'};
106 if (isset($userdata->{'cmi.core.score.min'}) && !empty($userdata->{'cmi.core.score.min'})) {
107 $min = ', '.$userdata->{'cmi.core.score.min'};
110 echo 'Score = '.$userdata->{'cmi.core.score.raw'}.$max.$min."\n";
111 } else {
112 echo 'Score = '."\n";
114 if (isset($userdata->{'cmi.core.total_time'})) {
115 echo 'Time = '.$userdata->{'cmi.core.total_time'}."\n";
116 } else {
117 echo 'Time = '.'00:00:00'."\n";
119 echo 'Lesson_Mode = '.$userdata->mode."\n";
120 if (isset($userdata->{'cmi.suspend_data'})) {
121 echo "[Core_Lesson]\n".$userdata->{'cmi.suspend_data'}."\n";
122 } else {
123 echo "[Core_Lesson]\n"."\n";
125 echo "[Core_Vendor]\n".$userdata->datafromlms."\n";
126 echo "[Evaluation]\nCourse_ID = {".$userdata->course_id."}\n";
127 echo "[Student_Data]\n";
128 echo 'Mastery_Score = '.$userdata->masteryscore."\n";
129 echo 'Max_Time_Allowed = '.$userdata->maxtimeallowed."\n";
130 echo 'Time_Limit_Action = '.$userdata->timelimitaction."\n";
131 } else {
132 error('Sco not found');
135 break;
136 case 'putparam':
137 if ($status == 'Running') {
138 if (!empty($aiccdata) && isstudent($scorm->course)) {
139 $initlessonstatus = 'not attempted';
140 $lessonstatus = 'not attempted';
141 if (isset($SESSION->scorm_lessonstatus)) {
142 $initlessonstatus = $SESSION->scorm_lessonstatus;
144 $score = '';
145 $datamodel['lesson_location'] = 'cmi.core.lesson_location';
146 $datamodel['lesson_status'] = 'cmi.core.lesson_status';
147 $datamodel['score'] = 'cmi.core.score.raw';
148 $datamodel['time'] = 'cmi.core.session_time';
149 $datamodel['[core_lesson]'] = 'cmi.suspend_data';
150 $datamodel['[comments]'] = 'cmi.comments';
151 $datarows = explode("\n",$aiccdata);
152 reset($datarows);
153 while ((list(,$datarow) = each($datarows)) !== false) {
154 if (($equal = strpos($datarow, '=')) !== false) {
155 $element = strtolower(trim(substr($datarow,0,$equal)));
156 $value = trim(substr($datarow,$equal+1));
157 if (isset($datamodel[$element])) {
158 $element = $datamodel[$element];
159 switch ($element) {
160 case 'cmi.core.lesson_location':
161 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
162 break;
163 case 'cmi.core.lesson_status':
164 $statuses = array(
165 'passed' => 'passed',
166 'completed' => 'completed',
167 'failed' => 'failed',
168 'incomplete' => 'incomplete',
169 'browsed' => 'browsed',
170 'not attempted' => 'not attempted',
171 'p' => 'passed',
172 'c' => 'completed',
173 'f' => 'failed',
174 'i' => 'incomplete',
175 'b' => 'browsed',
176 'n' => 'not attempted'
178 $exites = array(
179 'logout' => 'logout',
180 'time-out' => 'time-out',
181 'suspend' => 'suspend',
182 'l' => 'logout',
183 't' => 'time-out',
184 's' => 'suspend',
186 $values = explode(',',$value);
187 $value = '';
188 if (count($values) > 1) {
189 $value = trim(strtolower($values[1]));
190 if (isset($exites[$value])) {
191 $value = $exites[$value];
194 if (empty($value) || isset($exites[$value])) {
195 $subelement = 'cmi.core.exit';
196 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $subelement, $value);
198 $value = trim(strtolower($values[0]));
199 if (isset($statuses[$value]) && ($mode == 'normal')) {
200 $value = $statuses[$value];
201 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
203 $lessonstatus = $value;
204 break;
205 case 'cmi.core.score.raw':
206 $values = explode(',',$value);
207 if ((count($values) > 1) && ($values[1] >= $values[0]) && is_numeric($values[1])) {
208 $subelement = 'cmi.core.score.max';
209 $value = trim($values[1]);
210 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $subelement, $value);
211 if ((count($values) == 3) && ($values[2] <= $values[0]) && is_numeric($values[2])) {
212 $subelement = 'cmi.core.score.min';
213 $value = trim($values[2]);
214 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $subelement, $value);
218 $value = '';
219 if (is_numeric($values[0])) {
220 $value = trim($values[0]);
221 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
223 $score = $value;
224 break;
225 case 'cmi.core.session_time':
226 $SESSION->scorm_session_time = $value;
227 break;
230 } else {
231 if (isset($datamodel[strtolower(trim($datarow))])) {
232 $element = $datamodel[strtolower(trim($datarow))];
233 $value = '';
234 while ((($datarow = current($datarows)) !== false) && (substr($datarow,0,1) != '[')) {
235 $value .= $datarow;
236 next($datarows);
238 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
242 if (($mode == 'browse') && ($initlessonstatus == 'not attempted')){
243 $lessonstatus = 'browsed';
244 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, 'cmi.core.lesson_status', 'browsed');
246 if ($mode == 'normal') {
247 if ($lessonstatus == 'completed') {
248 if (!empty($sco->masteryscore) && !empty($score) && ($score >= $sco->masteryscore)) {
249 $lessonstatus = 'passed';
250 } else {
251 $lessonstatus = 'failed';
253 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, 'cmi.core.lesson_status', $lessonstatus);
257 echo "error = 0\nerror_text = Successful\n";
258 } else if ($status == 'Terminated') {
259 echo "error = 1\nerror_text = Terminated\n";
260 } else {
261 echo "error = 1\nerror_text = Not Initialized\n";
263 break;
264 case 'putcomments':
265 if ($status == 'Running') {
266 echo "error = 0\nerror_text = Successful\n";
267 } else if ($status == 'Terminated') {
268 echo "error = 1\nerror_text = Terminated\n";
269 } else {
270 echo "error = 1\nerror_text = Not Initialized\n";
272 break;
273 case 'putinteractions':
274 if ($status == 'Running') {
275 echo "error = 0\nerror_text = Successful\n";
276 } else if ($status == 'Terminated') {
277 echo "error = 1\nerror_text = Terminated\n";
278 } else {
279 echo "error = 1\nerror_text = Not Initialized\n";
281 break;
282 case 'putobjectives':
283 if ($status == 'Running') {
284 echo "error = 0\nerror_text = Successful\n";
285 } else if ($status == 'Terminated') {
286 echo "error = 1\nerror_text = Terminated\n";
287 } else {
288 echo "error = 1\nerror_text = Not Initialized\n";
290 break;
291 case 'putpath':
292 if ($status == 'Running') {
293 echo "error = 0\nerror_text = Successful\n";
294 } else if ($status == 'Terminated') {
295 echo "error = 1\nerror_text = Terminated\n";
296 } else {
297 echo "error = 1\nerror_text = Not Initialized\n";
299 break;
300 case 'putperformance':
301 if ($status == 'Running') {
302 echo "error = 0\nerror_text = Successful\n";
303 } else if ($status == 'Terminated') {
304 echo "error = 1\nerror_text = Terminated\n";
305 } else {
306 echo "error = 1\nerror_text = Not Initialized\n";
308 break;
309 case 'exitau':
310 if ($status == 'Running') {
311 if (isset($SESSION->scorm_session_time) && ($SESSION->scorm_session_time != '')) {
312 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'")) {
313 // Add session_time to total_time
314 $value = scorm_add_time($track->value, $SESSION->scorm_session_time);
315 $track->value = $value;
316 $track->timemodified = time();
317 $id = update_record('scorm_scoes_track',$track);
318 } else {
319 $track->userid = $USER->id;
320 $track->scormid = $scorm->id;
321 $track->scoid = $sco->id;
322 $track->element = 'cmi.core.total_time';
323 $track->value = $SESSION->scorm_session_time;
324 $track->timemodified = time();
325 $id = insert_record('scorm_scoes_track',$track);
329 $SESSION->scorm_status = 'Terminated';
330 $SESSION->scorm_session_time = '';
331 echo "error = 0\nerror_text = Successful\n";
332 } else if ($status == 'Terminated') {
333 echo "error = 1\nerror_text = Terminated\n";
334 } else {
335 echo "error = 1\nerror_text = Not Initialized\n";
337 break;
338 default:
339 echo "error = 1\nerror_text = Invalid Command\n";
340 break;
343 } else {
344 if (empty($command)) {
345 echo "error = 1\nerror_text = Invalid Command\n";
346 } else {
347 echo "error = 3\nerror_text = Invalid Session ID\n";