Changes for XHTML and accessibility compliance
[moodle.git] / mod / scorm / api1_2.php
CommitLineData
98ca59f6 1//
2// SCORM API 1.2 Implementation
3//
434983ca 4
98ca59f6 5function SCORMapi() {
76d35423 6 var cmi= new Object();
fc274a5f 7 var nav = new Object();
76d35423 8
434983ca 9 var errorCode = "0";
76d35423 10
98ca59f6 11 var Initialized = false;
12
13 function LMSInitialize (param) {
14 if (param != "") {
434983ca 15 errorCode = "201";
98ca59f6 16 return "false";
17 }
18 if (!Initialized) {
19 Initialized = true;
434983ca 20 errorCode = "0";
76d35423 21
22 //
23 // CMI Initialization SCORM 1.2
24 //
25 cmi.core = new Object();
26 cmi.core._children = "student_id,student_name,lesson_location,credit,lesson_status,exit,entry,session_time,total_time,lesson_mode,score,suspend_data,launch_data";
27 cmi.core.student_id = "<?php echo $USER->username; ?>";
434983ca 28 cmi.core.student_name = "<?php echo $USER->lastname.", ".$USER->firstname; ?>";
76d35423 29 cmi.core.lesson_location = "<?php echo $sco_user->cmi_core_lesson_location; ?>";
6b4c13b7 30 cmi.core.credit = "<?php if ($mode != 'normal') {
31 echo "no-credit";
32 } else {
33 echo "credit";
34 }?>";
76d35423 35 cmi.core.lesson_status = "<?php echo $sco_user->cmi_core_lesson_status; ?>";
36 cmi.core.exit = "<?php echo $sco_user->cmi_core_exit ?>";
6b4c13b7 37 cmi.core.entry = "<?php if ($sco_user->cmi_core_lesson_status == 'not attempted') {
76d35423 38 echo 'ab-initio';
6b4c13b7 39 } else {
40 if ($sco_user->cmi_core_lesson_status != 'completed') {
76d35423 41 echo 'resume';
6b4c13b7 42 } else {
43 echo '';
44 }
45 }?>";
46 cmi.core.session_time = "00:00:00";
76d35423 47 cmi.core.total_time = "<?php echo $sco_user->cmi_core_total_time; ?>";
48 cmi.core.lesson_mode = "<?php echo $mode; ?>";
49 cmi.core.score = new Object();
50 cmi.core.score._children = "raw,min,max";
51 cmi.core.score.raw = "<?php echo $sco_user->cmi_core_score_raw; ?>";
434983ca 52 cmi.core.score.min = "";
53 cmi.core.score.max = "";
76d35423 54 cmi.suspend_data = "<?php echo $sco_user->cmi_suspend_data; ?>";
434983ca 55 cmi.launch_data = "<?php echo $sco->datafromlms; ?>";
56 cmi.comments = "";
57 cmi.comments_from_lms = "";
76d35423 58 //
59 // end CMI Initialization
60 //
6d9c91c7 61
fc274a5f 62 // Navigation Object
6d9c91c7 63 <?php
64 if ($scorm->auto) {
65 echo 'nav.event = "continue";'."\n";
66 } else {
67 echo 'nav.event = "";'."\n";
68 }
69 ?>
76d35423 70
98ca59f6 71 return "true";
72 } else {
434983ca 73 errorCode = "101";
98ca59f6 74 return "false";
75 }
76 }
77
78 function LMSGetValue (param) {
79 if (Initialized) {
6b4c13b7 80 //top.alert("GET "+param);
98ca59f6 81 switch (param) {
82 case "cmi.core._children":
83 case "cmi.core.student_id":
84 case "cmi.core.student_name":
85 case "cmi.core.lesson_location":
86 case "cmi.core.credit":
87 case "cmi.core.lesson_status":
88 case "cmi.core.entry":
89 case "cmi.core.total_time":
90 case "cmi.core.lesson_mode":
91 case "cmi.core.score._children":
92 case "cmi.core.score.raw":
434983ca 93 case "cmi.core.score.min":
94 case "cmi.core.score.max":
98ca59f6 95 case "cmi.launch_data":
96 case "cmi.suspend_data":
434983ca 97 case "cmi.comments":
98 case "cmi.comments_from_lms":
99 errorCode = "0";
98ca59f6 100 return eval(param);
101 break;
102 case "cmi.core.exit":
103 case "cmi.core.session_time":
434983ca 104 errorCode = "404";
98ca59f6 105 return "";
106 break;
107 default:
434983ca 108 errorCode = "401";
109 param = param.replace(/.(\d+)./g,"[$1].");
110
111 children = param.match(/._children$/);
112 if (children != null) {
113 objType = typeof eval(children[1]);
114 //alert (param+" :"+objType);
115 if (objType != "undefined") {
116
117 errorCode = "202";
118 }
119 }
120
121 counted = param.match(/._count$/);
122 if (counted != null) {
123 objType = typeof eval(counted[1]);
124 //alert (param+" :"+objType);
125 if (objType != "undefined") {
126 errorCode = "203";
127 }
128 }
129 //alert(param+": "+errorCode);
130 return "";
98ca59f6 131 break;
132 }
133 } else {
434983ca 134 errorCode = "301";
98ca59f6 135 return "";
136 }
137 }
138
139 function LMSSetValue (param,value) {
140 if (Initialized) {
5f4aacd3 141 //top.alert("SET "+param+" = "+value);
98ca59f6 142 switch (param) {
143 case "cmi.core.session_time":
6d9c91c7 144 if (typeof(value) == "string") {
fc274a5f 145 var parsedtime = value.match(/^([0-9]{2,4}):([0-9]{2}):([0-9]{2})(\.[0-9]{1,2})?$/);
146 if (parsedtime != null) {
147 //top.alert(parsedtime);
1a12b1f1 148 if (((parsedtime.length == 4) || (parsedtime.length == 5)) && (parsedtime[3]>=0) && (parsedtime[3]<=59)) {
6b4c13b7 149 eval(param+'="'+value+'";');
434983ca 150 errorCode = "0";
6b4c13b7 151 return "true";
152 } else {
434983ca 153 errorCode = "405";
6b4c13b7 154 return "false";
6d9c91c7 155 }
6b4c13b7 156 } else {
434983ca 157 errorCode = "405";
6d9c91c7 158 return "false";
159 }
160 } else {
434983ca 161 errorCode = "405";
6d9c91c7 162 return "false";
163 }
98ca59f6 164 break;
165 case "cmi.core.lesson_status":
166 if ((value!="passed")&&(value!="completed")&&(value!="failed")&&(value!="incomplete")&&(value!="browsed")) {
434983ca 167 errorCode = "405";
98ca59f6 168 return "false";
169 }
170 eval(param+'="'+value+'";');
434983ca 171 errorCode = "0";
98ca59f6 172 return "true";
173 break;
174 case "cmi.core.score.raw":
415807c0 175 case "cmi.core.score.min":
176 case "cmi.core.score.max":
434983ca 177 if (value != "") {
178 if ((parseFloat(value,10)).toString() != value) {
179 errorCode = "405";
180 return "false";
181 } else {
182 rawvalue = parseFloat(value,10);
183 if ((rawvalue<0) || (rawvalue>100)) {
184 errorCode = "405";
185 return "false";
186 }
187 }
98ca59f6 188 }
189 eval(param+'="'+value+'";');
434983ca 190 errorCode = "0";
98ca59f6 191 return "true";
192 break;
193 case "cmi.core.exit":
194 if ((value!="time-out")&&(value!="suspend")&&(value!="logout")&&(value!="")) {
434983ca 195 errorCode = "405";
98ca59f6 196 return "false";
197 }
198 eval(param+'="'+value+'";');
434983ca 199 errorCode = "0";
98ca59f6 200 return "true";
201 break;
202 case "cmi.core.lesson_location":
434983ca 203 if (value.length > 255) {
204 errorCode = "405";
205 return "false";
206 }
207 eval(param+'="'+value+'";');
208 errorCode = "0";
209 return "true";
210 break;
434983ca 211 case "cmi.comments":
2c508ab9 212 if ((value.length + cmi.comments.length) > 4096) {
213 errorCode = "405";
214 return "false";
215 }
216 eval(param+'=cmi.comments + "'+value+'";');
217 errorCode = "0";
218 return "true";
219 break;
220 case "cmi.suspend_data":
434983ca 221 if (value.length > 4096) {
222 errorCode = "405";
223 return "false";
224 }
98ca59f6 225 eval(param+'="'+value+'";');
434983ca 226 errorCode = "0";
98ca59f6 227 return "true";
228 break;
229 case "cmi.core._children":
230 case "cmi.core.score._children":
434983ca 231 errorCode = "402";
98ca59f6 232 return "false";
233 break;
234 case "cmi.core.student_id":
235 case "cmi.core.student_name":
236 case "cmi.core.credit":
237 case "cmi.core.entry":
238 case "cmi.core.total_time":
239 case "cmi.core.lesson_mode":
240 case "cmi.launch_data":
434983ca 241 case "cmi.comments_from_lms":
242 errorCode = "403";
98ca59f6 243 return "false";
244 break;
6d9c91c7 245 case "nav.event":
246 if ((value == "previous") || (value == "continue")) {
247 eval(param+'="'+value+'";');
434983ca 248 errorCode = "0";
6d9c91c7 249 return "true";
250 } else {
434983ca 251 erroCode = "405";
6d9c91c7 252 return "false";
253 }
254 break;
98ca59f6 255 default:
434983ca 256 errorCode = "401"; //This is more correct but may have problem with some SCOes
257 //errorCode = "0"; // With this disable any possible SCO errors alert
98ca59f6 258 return "false";
259 break;
260 }
261 } else {
434983ca 262 errorCode = "301";
98ca59f6 263 return "false";
264 }
265 }
266
267 function LMSCommit (param) {
268 if (param != "") {
434983ca 269 errorCode = "201";
98ca59f6 270 return "false";
271 }
272 if (Initialized) {
b98eebbf 273 if (<?php echo $navObj ?>cmi.document.theform) {
274 cmiform = <?php echo $navObj ?>cmi.document.forms[0];
fc1b2663 275 cmiform.scoid.value = "<?php echo $sco->id; ?>";
98ca59f6 276 cmiform.cmi_core_lesson_location.value = cmi.core.lesson_location;
277 cmiform.cmi_core_lesson_status.value = cmi.core.lesson_status;
278 cmiform.cmi_core_exit.value = cmi.core.exit;
98ca59f6 279 cmiform.cmi_core_score_raw.value = cmi.core.score.raw;
280 cmiform.cmi_suspend_data.value = cmi.suspend_data;
281 cmiform.submit();
282 }
434983ca 283 errorCode = "0";
98ca59f6 284 return "true";
285 } else {
434983ca 286 errorCode = "301";
98ca59f6 287 return "false";
288 }
289 }
290
291 function LMSFinish (param) {
292 if (param != "") {
434983ca 293 errorCode = "201";
98ca59f6 294 return "false";
295 }
296 if (!Initialized) {
434983ca 297 errorCode = "301";
98ca59f6 298 return "false";
299 } else {
300 Initialized = false;
434983ca 301 errorCode = "0";
1a12b1f1 302
6d9c91c7 303 if (nav.event != "") {
98ca59f6 304 <?php
6d9c91c7 305 if ($sco != $last) {
306 echo "setTimeout('top.changeSco(nav.event);',500);\n";
307 } else {
308 echo "exitloc = '".$CFG->wwwroot."/mod/scorm/view.php?id=".$cm->id."';\n";
309 echo "setTimeout('top.location = exitloc;',500);\n";
98ca59f6 310 }
311 ?>
6d9c91c7 312 }
98ca59f6 313 return "true";
314 }
315 }
316
317 function LMSGetLastError () {
318 return errorCode;
319 }
320
321 function LMSGetErrorString (param) {
322 var errorString = new Array();
323 errorString["0"] = "No error";
324 errorString["101"] = "General exception";
325 errorString["201"] = "Invalid argument error";
326 errorString["202"] = "Element cannot have children";
327 errorString["203"] = "Element not an array - cannot have count";
434983ca 328 errorString["301"] = "Not initialized";
98ca59f6 329 errorString["401"] = "Not implemented error";
330 errorString["402"] = "Invalid set value, element is a keyword";
331 errorString["403"] = "Element is read only";
332 errorString["404"] = "Element is write only";
333 errorString["405"] = "Incorrect data type";
334 return errorString[param];
335 }
336
337 function LMSGetDiagnostic (param) {
338 return param;
339 }
340
341 function AddTime (first, second) {
342 var sFirst = first.split(":");
343 var sSecond = second.split(":");
344 var change = 0;
345
346 var secs = (Math.round((parseFloat(sFirst[2],10)+parseFloat(sSecond[2],10))*100))/100; //Seconds
1a12b1f1 347 change = Math.floor(secs / 60);
348 secs = secs - (change * 60);
98ca59f6 349 if (Math.floor(secs) < 10) secs = "0" + secs.toString();
350
351 mins = parseInt(sFirst[1],10)+parseInt(sSecond[1],10)+change; //Minutes
1a12b1f1 352 change = Math.floor(mins / 60);
353 mins = mins - (change * 60);
98ca59f6 354 if (mins < 10) mins = "0" + mins.toString();
355
356 hours = parseInt(sFirst[0],10)+parseInt(sSecond[0],10)+change; //Hours
357 if (hours < 10) hours = "0" + hours.toString();
358
359 return hours + ":" + mins + ":" + secs;
360 }
76d35423 361
1a12b1f1 362 function SaveTotalTime() {
363 cmi.core.total_time = AddTime(cmi.core.total_time, cmi.core.session_time);
364 //top.alert(cmi.core.total_time);
365 if (<?php echo $navObj ?>cmi.document.forms[0]) {
366 cmiform = <?php echo $navObj ?>cmi.document.forms[0];
367 cmiform.reset();
368 cmiform.scoid.value = "<?php echo $sco->id; ?>";
369 cmiform.cmi_core_total_time.value = cmi.core.total_time;
370 cmiform.submit();
371 //top.alert(cmi.core.total_time);
372 }
373 }
374
375 this.SaveTotalTime = SaveTotalTime;
376
98ca59f6 377 this.LMSInitialize = LMSInitialize;
378 this.LMSGetValue = LMSGetValue;
379 this.LMSSetValue = LMSSetValue;
380 this.LMSCommit = LMSCommit;
381 this.LMSFinish = LMSFinish;
382 this.LMSGetLastError = LMSGetLastError;
383 this.LMSGetErrorString = LMSGetErrorString;
384 this.LMSGetDiagnostic = LMSGetDiagnostic;
385}
386
434983ca 387var API = new SCORMapi();