First translation into Portuguese
[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);
148 if (((parsedtime.length == 4) || (parsedtime.length == 5)) && (parsedtime[2]>=0) && (parsedtime[2]<=59) && (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;
98ca59f6 211 case "cmi.suspend_data":
434983ca 212 case "cmi.comments":
213 if (value.length > 4096) {
214 errorCode = "405";
215 return "false";
216 }
98ca59f6 217 eval(param+'="'+value+'";');
434983ca 218 errorCode = "0";
98ca59f6 219 return "true";
220 break;
221 case "cmi.core._children":
222 case "cmi.core.score._children":
434983ca 223 errorCode = "402";
98ca59f6 224 return "false";
225 break;
226 case "cmi.core.student_id":
227 case "cmi.core.student_name":
228 case "cmi.core.credit":
229 case "cmi.core.entry":
230 case "cmi.core.total_time":
231 case "cmi.core.lesson_mode":
232 case "cmi.launch_data":
434983ca 233 case "cmi.comments_from_lms":
234 errorCode = "403";
98ca59f6 235 return "false";
236 break;
6d9c91c7 237 case "nav.event":
238 if ((value == "previous") || (value == "continue")) {
239 eval(param+'="'+value+'";');
434983ca 240 errorCode = "0";
6d9c91c7 241 return "true";
242 } else {
434983ca 243 erroCode = "405";
6d9c91c7 244 return "false";
245 }
246 break;
98ca59f6 247 default:
434983ca 248 errorCode = "401"; //This is more correct but may have problem with some SCOes
249 //errorCode = "0"; // With this disable any possible SCO errors alert
98ca59f6 250 return "false";
251 break;
252 }
253 } else {
434983ca 254 errorCode = "301";
98ca59f6 255 return "false";
256 }
257 }
258
259 function LMSCommit (param) {
260 if (param != "") {
434983ca 261 errorCode = "201";
98ca59f6 262 return "false";
263 }
264 if (Initialized) {
b98eebbf 265 if (<?php echo $navObj ?>cmi.document.theform) {
266 cmiform = <?php echo $navObj ?>cmi.document.forms[0];
fc1b2663 267 cmiform.scoid.value = "<?php echo $sco->id; ?>";
98ca59f6 268 cmiform.cmi_core_lesson_location.value = cmi.core.lesson_location;
269 cmiform.cmi_core_lesson_status.value = cmi.core.lesson_status;
270 cmiform.cmi_core_exit.value = cmi.core.exit;
98ca59f6 271 cmiform.cmi_core_score_raw.value = cmi.core.score.raw;
272 cmiform.cmi_suspend_data.value = cmi.suspend_data;
273 cmiform.submit();
274 }
434983ca 275 errorCode = "0";
98ca59f6 276 return "true";
277 } else {
434983ca 278 errorCode = "301";
98ca59f6 279 return "false";
280 }
281 }
282
283 function LMSFinish (param) {
284 if (param != "") {
434983ca 285 errorCode = "201";
98ca59f6 286 return "false";
287 }
288 if (!Initialized) {
434983ca 289 errorCode = "301";
98ca59f6 290 return "false";
291 } else {
292 Initialized = false;
434983ca 293 errorCode = "0";
6d9c91c7 294 cmi.core.total_time = AddTime(cmi.core.total_time, cmi.core.session_time);
fc274a5f 295 //top.alert(cmi.core.total_time);
6d9c91c7 296 if (<?php echo $navObj ?>cmi.document.theform) {
297 cmiform = <?php echo $navObj ?>cmi.document.forms[0];
298 cmiform.scoid.value = "<?php echo $sco->id; ?>";
299 cmiform.cmi_core_total_time.value = cmi.core.total_time;
300 cmiform.submit();
fc274a5f 301
6d9c91c7 302 }
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
347 if (secs > 60) {
348 secs = secs - 60;
349 change = 1;
350 } else {
351 change = 0;
352 }
353 if (Math.floor(secs) < 10) secs = "0" + secs.toString();
354
355 mins = parseInt(sFirst[1],10)+parseInt(sSecond[1],10)+change; //Minutes
356 if (mins > 60)
357 change = 1;
358 else
359 change = 0;
360 if (mins < 10) mins = "0" + mins.toString();
361
362 hours = parseInt(sFirst[0],10)+parseInt(sSecond[0],10)+change; //Hours
363 if (hours < 10) hours = "0" + hours.toString();
364
365 return hours + ":" + mins + ":" + secs;
366 }
76d35423 367
98ca59f6 368 this.LMSInitialize = LMSInitialize;
369 this.LMSGetValue = LMSGetValue;
370 this.LMSSetValue = LMSSetValue;
371 this.LMSCommit = LMSCommit;
372 this.LMSFinish = LMSFinish;
373 this.LMSGetLastError = LMSGetLastError;
374 this.LMSGetErrorString = LMSGetErrorString;
375 this.LMSGetDiagnostic = LMSGetDiagnostic;
376}
377
434983ca 378var API = new SCORMapi();