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