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