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