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