MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / scorm / aicc.php
blob64b2de3a26c72b23ced28cc0ceb316b1d3dd982c
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 = scorm_get_sco($scoid, SCO_ONLY)) {
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 = scorm_get_sco($scoid)) {
67 $userdata->course_id = $sco->identifier;
68 $userdata->datafromlms = isset($sco->datafromlms)?$sco->datafromlms:'';
69 $userdata->masteryscore = isset($sco->masteryscore)?$sco->masteryscore:'';
70 $userdata->maxtimeallowed = isset($sco->maxtimeallowed)?$sco->maxtimeallowed:'';
71 $userdata->timelimitaction = isset($sco->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 (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $scorm->course)) {
139 echo "error = 1\nerror_text = Unknown\n"; // No one must see this error message if not hacked
141 if (!empty($aiccdata) && has_capability('mod/scorm:savetrack', get_context_instance(CONTEXT_MODULE, $cm->id))) {
142 $initlessonstatus = 'not attempted';
143 $lessonstatus = 'not attempted';
144 if (isset($SESSION->scorm_lessonstatus)) {
145 $initlessonstatus = $SESSION->scorm_lessonstatus;
147 $score = '';
148 $datamodel['lesson_location'] = 'cmi.core.lesson_location';
149 $datamodel['lesson_status'] = 'cmi.core.lesson_status';
150 $datamodel['score'] = 'cmi.core.score.raw';
151 $datamodel['time'] = 'cmi.core.session_time';
152 $datamodel['[core_lesson]'] = 'cmi.suspend_data';
153 $datamodel['[comments]'] = 'cmi.comments';
154 $datarows = explode("\n",$aiccdata);
155 reset($datarows);
156 while ((list(,$datarow) = each($datarows)) !== false) {
157 if (($equal = strpos($datarow, '=')) !== false) {
158 $element = strtolower(trim(substr($datarow,0,$equal)));
159 $value = trim(substr($datarow,$equal+1));
160 if (isset($datamodel[$element])) {
161 $element = $datamodel[$element];
162 switch ($element) {
163 case 'cmi.core.lesson_location':
164 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
165 break;
166 case 'cmi.core.lesson_status':
167 $statuses = array(
168 'passed' => 'passed',
169 'completed' => 'completed',
170 'failed' => 'failed',
171 'incomplete' => 'incomplete',
172 'browsed' => 'browsed',
173 'not attempted' => 'not attempted',
174 'p' => 'passed',
175 'c' => 'completed',
176 'f' => 'failed',
177 'i' => 'incomplete',
178 'b' => 'browsed',
179 'n' => 'not attempted'
181 $exites = array(
182 'logout' => 'logout',
183 'time-out' => 'time-out',
184 'suspend' => 'suspend',
185 'l' => 'logout',
186 't' => 'time-out',
187 's' => 'suspend',
189 $values = explode(',',$value);
190 $value = '';
191 if (count($values) > 1) {
192 $value = trim(strtolower($values[1]));
193 if (isset($exites[$value])) {
194 $value = $exites[$value];
197 if (empty($value) || isset($exites[$value])) {
198 $subelement = 'cmi.core.exit';
199 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $subelement, $value);
201 $value = trim(strtolower($values[0]));
202 if (isset($statuses[$value]) && ($mode == 'normal')) {
203 $value = $statuses[$value];
204 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
206 $lessonstatus = $value;
207 break;
208 case 'cmi.core.score.raw':
209 $values = explode(',',$value);
210 if ((count($values) > 1) && ($values[1] >= $values[0]) && is_numeric($values[1])) {
211 $subelement = 'cmi.core.score.max';
212 $value = trim($values[1]);
213 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $subelement, $value);
214 if ((count($values) == 3) && ($values[2] <= $values[0]) && is_numeric($values[2])) {
215 $subelement = 'cmi.core.score.min';
216 $value = trim($values[2]);
217 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $subelement, $value);
221 $value = '';
222 if (is_numeric($values[0])) {
223 $value = trim($values[0]);
224 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
226 $score = $value;
227 break;
228 case 'cmi.core.session_time':
229 $SESSION->scorm_session_time = $value;
230 break;
233 } else {
234 if (isset($datamodel[strtolower(trim($datarow))])) {
235 $element = $datamodel[strtolower(trim($datarow))];
236 $value = '';
237 while ((($datarow = current($datarows)) !== false) && (substr($datarow,0,1) != '[')) {
238 $value .= $datarow;
239 next($datarows);
241 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, $element, $value);
245 if (($mode == 'browse') && ($initlessonstatus == 'not attempted')){
246 $lessonstatus = 'browsed';
247 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, 'cmi.core.lesson_status', 'browsed');
249 if ($mode == 'normal') {
250 if ($lessonstatus == 'completed') {
251 if (!empty($sco->masteryscore) && !empty($score) && ($score >= $sco->masteryscore)) {
252 $lessonstatus = 'passed';
253 } else {
254 $lessonstatus = 'failed';
256 $id = scorm_insert_track($USER->id, $scorm->id, $sco->id, $attempt, 'cmi.core.lesson_status', $lessonstatus);
260 echo "error = 0\nerror_text = Successful\n";
261 } else if ($status == 'Terminated') {
262 echo "error = 1\nerror_text = Terminated\n";
263 } else {
264 echo "error = 1\nerror_text = Not Initialized\n";
266 break;
267 case 'putcomments':
268 if ($status == 'Running') {
269 echo "error = 0\nerror_text = Successful\n";
270 } else if ($status == 'Terminated') {
271 echo "error = 1\nerror_text = Terminated\n";
272 } else {
273 echo "error = 1\nerror_text = Not Initialized\n";
275 break;
276 case 'putinteractions':
277 if ($status == 'Running') {
278 echo "error = 0\nerror_text = Successful\n";
279 } else if ($status == 'Terminated') {
280 echo "error = 1\nerror_text = Terminated\n";
281 } else {
282 echo "error = 1\nerror_text = Not Initialized\n";
284 break;
285 case 'putobjectives':
286 if ($status == 'Running') {
287 echo "error = 0\nerror_text = Successful\n";
288 } else if ($status == 'Terminated') {
289 echo "error = 1\nerror_text = Terminated\n";
290 } else {
291 echo "error = 1\nerror_text = Not Initialized\n";
293 break;
294 case 'putpath':
295 if ($status == 'Running') {
296 echo "error = 0\nerror_text = Successful\n";
297 } else if ($status == 'Terminated') {
298 echo "error = 1\nerror_text = Terminated\n";
299 } else {
300 echo "error = 1\nerror_text = Not Initialized\n";
302 break;
303 case 'putperformance':
304 if ($status == 'Running') {
305 echo "error = 0\nerror_text = Successful\n";
306 } else if ($status == 'Terminated') {
307 echo "error = 1\nerror_text = Terminated\n";
308 } else {
309 echo "error = 1\nerror_text = Not Initialized\n";
311 break;
312 case 'exitau':
313 if ($status == 'Running') {
314 if (isset($SESSION->scorm_session_time) && ($SESSION->scorm_session_time != '')) {
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'")) {
316 // Add session_time to total_time
317 $value = scorm_add_time($track->value, $SESSION->scorm_session_time);
318 $track->value = $value;
319 $track->timemodified = time();
320 update_record('scorm_scoes_track',$track);
321 $id = $track->id;
322 } else {
323 $track = new object();
324 $track->userid = $USER->id;
325 $track->scormid = $scorm->id;
326 $track->scoid = $sco->id;
327 $track->element = 'cmi.core.total_time';
328 $track->value = $SESSION->scorm_session_time;
329 $track->timemodified = time();
330 $id = insert_record('scorm_scoes_track',$track);
332 scorm_update_grades($scorm, $USER->id);
335 $SESSION->scorm_status = 'Terminated';
336 $SESSION->scorm_session_time = '';
337 echo "error = 0\nerror_text = Successful\n";
338 } else if ($status == 'Terminated') {
339 echo "error = 1\nerror_text = Terminated\n";
340 } else {
341 echo "error = 1\nerror_text = Not Initialized\n";
343 break;
344 default:
345 echo "error = 1\nerror_text = Invalid Command\n";
346 break;
349 } else {
350 if (empty($command)) {
351 echo "error = 1\nerror_text = Invalid Command\n";
352 } else {
353 echo "error = 3\nerror_text = Invalid Session ID\n";