Translated and revised by Claudio Maggi
[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 ?>";
31 cmi.core.entry = "<?php if ($sco_user->cmi_core_lesson_status=="not attempted")
32 echo 'ab-initio';
33 else
34 if ($sco_user->cmi_core_lesson_status!="completed")
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 //
49
98ca59f6 50 return "true";
51 } else {
52 errorCode = 101;
53 return "false";
54 }
55 }
56
57 function LMSGetValue (param) {
58 if (Initialized) {
59 //top.status="GET "+param;
60 switch (param) {
61 case "cmi.core._children":
62 case "cmi.core.student_id":
63 case "cmi.core.student_name":
64 case "cmi.core.lesson_location":
65 case "cmi.core.credit":
66 case "cmi.core.lesson_status":
67 case "cmi.core.entry":
68 case "cmi.core.total_time":
69 case "cmi.core.lesson_mode":
70 case "cmi.core.score._children":
71 case "cmi.core.score.raw":
72 case "cmi.launch_data":
73 case "cmi.suspend_data":
74 errorCode = 0;
75 return eval(param);
76 break;
77 case "cmi.core.exit":
78 case "cmi.core.session_time":
79 errorCode = 404;
80 return "";
81 break;
82 default:
83 errorCode = 401;
84 return "";
85 break;
86 }
87 } else {
88 errorCode = 301;
89 return "";
90 }
91 }
92
93 function LMSSetValue (param,value) {
94 if (Initialized) {
95 //top.status="SET "+param+" = "+value;
415807c0 96 //top.alert("SET "+param+" = "+value);
98ca59f6 97 switch (param) {
98 case "cmi.core.session_time":
99 cmi.core.total_time = AddTime(cmi.core.total_time, value);
100 //top.status="SET cmi.core.total_time = "+cmi.core.total_time;
101 eval(param+'="'+value+'";');
102 errorCode = 0;
103 return "true";
104 break;
105 case "cmi.core.lesson_status":
106 if ((value!="passed")&&(value!="completed")&&(value!="failed")&&(value!="incomplete")&&(value!="browsed")) {
107 errorCode = 405;
108 return "false";
109 }
110 eval(param+'="'+value+'";');
111 errorCode = 0;
112 return "true";
113 break;
114 case "cmi.core.score.raw":
415807c0 115 case "cmi.core.score.min":
116 case "cmi.core.score.max":
117 //top.alert("SET "+param+" = "+value);
98ca59f6 118 if ((parseFloat(value,10)).toString() != value) {
119 errorCode = 405;
120 return "false";
c5163bfd 121 } else {
122 rawvalue = parseFloat(value,10);
123 if ((rawvalue<0) || (rawvalue>100)) {
124 errorCode = 405;
125 return "false";
126 }
98ca59f6 127 }
128 eval(param+'="'+value+'";');
129 errorCode = 0;
130 return "true";
131 break;
132 case "cmi.core.exit":
133 if ((value!="time-out")&&(value!="suspend")&&(value!="logout")&&(value!="")) {
134 errorCode = 405;
135 return "false";
136 }
137 eval(param+'="'+value+'";');
138 errorCode = 0;
139 return "true";
140 break;
141 case "cmi.core.lesson_location":
142 case "cmi.suspend_data":
143 eval(param+'="'+value+'";');
144 errorCode = 0;
145 return "true";
146 break;
147 case "cmi.core._children":
148 case "cmi.core.score._children":
149 errorCode = 402;
150 return "false";
151 break;
152 case "cmi.core.student_id":
153 case "cmi.core.student_name":
154 case "cmi.core.credit":
155 case "cmi.core.entry":
156 case "cmi.core.total_time":
157 case "cmi.core.lesson_mode":
158 case "cmi.launch_data":
159 errorCode = 403;
160 return "false";
161 break;
162 default:
163 //errorCode = 401; This is more correct but may have problem with some SCOes
164 errorCode = 0; // With this disable any possible SCO errors alert
165 return "false";
166 break;
167 }
168 } else {
169 errorCode = 301;
170 return "false";
171 }
172 }
173
174 function LMSCommit (param) {
175 if (param != "") {
176 errorCode = 201;
177 return "false";
178 }
179 if (Initialized) {
180 if (top.nav.cmi.document.theform) {
181 cmiform = top.nav.cmi.document.forms[0];
fc1b2663 182 cmiform.scoid.value = "<?php echo $sco->id; ?>";
98ca59f6 183 cmiform.cmi_core_lesson_location.value = cmi.core.lesson_location;
184 cmiform.cmi_core_lesson_status.value = cmi.core.lesson_status;
185 cmiform.cmi_core_exit.value = cmi.core.exit;
186 cmiform.cmi_core_session_time.value = cmi.core.session_time;
187 cmiform.cmi_core_total_time.value = cmi.core.total_time;
188 cmiform.cmi_core_score_raw.value = cmi.core.score.raw;
189 cmiform.cmi_suspend_data.value = cmi.suspend_data;
190 cmiform.submit();
191 }
192 errorCode = 0;
193 return "true";
194 } else {
195 errorCode = 301;
196 return "false";
197 }
198 }
199
200 function LMSFinish (param) {
201 if (param != "") {
202 errorCode = 201;
203 return "false";
204 }
205 if (!Initialized) {
206 errorCode = 301;
207 return "false";
208 } else {
209 Initialized = false;
210 errorCode = 0;
211 <?php
212 if ($scorm->auto) {
213 if ($sco != $last) {
214 print "setTimeout('top.nav.document.navform.next.click();',500);\n";
215 } else {
216 print "exitloc = '".$CFG->wwwroot."/mod/scorm/view.php?id=$cm->id';\n";
217 print "setTimeout('top.location = exitloc;',500);\n";
218 }
219 }
220 ?>
221 return "true";
222 }
223 }
224
225 function LMSGetLastError () {
226 return errorCode;
227 }
228
229 function LMSGetErrorString (param) {
230 var errorString = new Array();
231 errorString["0"] = "No error";
232 errorString["101"] = "General exception";
233 errorString["201"] = "Invalid argument error";
234 errorString["202"] = "Element cannot have children";
235 errorString["203"] = "Element not an array - cannot have count";
236 errorString["301"] = "Not initializated";
237 errorString["401"] = "Not implemented error";
238 errorString["402"] = "Invalid set value, element is a keyword";
239 errorString["403"] = "Element is read only";
240 errorString["404"] = "Element is write only";
241 errorString["405"] = "Incorrect data type";
242 return errorString[param];
243 }
244
245 function LMSGetDiagnostic (param) {
246 return param;
247 }
248
249 function AddTime (first, second) {
250 var sFirst = first.split(":");
251 var sSecond = second.split(":");
252 var change = 0;
253
254 var secs = (Math.round((parseFloat(sFirst[2],10)+parseFloat(sSecond[2],10))*100))/100; //Seconds
255 if (secs > 60) {
256 secs = secs - 60;
257 change = 1;
258 } else {
259 change = 0;
260 }
261 if (Math.floor(secs) < 10) secs = "0" + secs.toString();
262
263 mins = parseInt(sFirst[1],10)+parseInt(sSecond[1],10)+change; //Minutes
264 if (mins > 60)
265 change = 1;
266 else
267 change = 0;
268 if (mins < 10) mins = "0" + mins.toString();
269
270 hours = parseInt(sFirst[0],10)+parseInt(sSecond[0],10)+change; //Hours
271 if (hours < 10) hours = "0" + hours.toString();
272
273 return hours + ":" + mins + ":" + secs;
274 }
76d35423 275
98ca59f6 276 this.LMSInitialize = LMSInitialize;
277 this.LMSGetValue = LMSGetValue;
278 this.LMSSetValue = LMSSetValue;
279 this.LMSCommit = LMSCommit;
280 this.LMSFinish = LMSFinish;
281 this.LMSGetLastError = LMSGetLastError;
282 this.LMSGetErrorString = LMSGetErrorString;
283 this.LMSGetDiagnostic = LMSGetDiagnostic;
284}
285
286var API = new SCORMapi();