made sure that ungraded modules would not show up in gradebook
[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();
26 cmi.core.score._children = "raw";
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;
101 switch (param) {
102 case "cmi.core.session_time":
103 cmi.core.total_time = AddTime(cmi.core.total_time, value);
104 //top.status="SET cmi.core.total_time = "+cmi.core.total_time;
105 eval(param+'="'+value+'";');
106 errorCode = 0;
107 return "true";
108 break;
109 case "cmi.core.lesson_status":
110 if ((value!="passed")&&(value!="completed")&&(value!="failed")&&(value!="incomplete")&&(value!="browsed")) {
111 errorCode = 405;
112 return "false";
113 }
114 eval(param+'="'+value+'";');
115 errorCode = 0;
116 return "true";
117 break;
118 case "cmi.core.score.raw":
119 if ((parseFloat(value,10)).toString() != value) {
120 errorCode = 405;
121 return "false";
c5163bfd 122 } else {
123 rawvalue = parseFloat(value,10);
124 if ((rawvalue<0) || (rawvalue>100)) {
125 errorCode = 405;
126 return "false";
127 }
98ca59f6 128 }
129 eval(param+'="'+value+'";');
130 errorCode = 0;
131 return "true";
132 break;
133 case "cmi.core.exit":
134 if ((value!="time-out")&&(value!="suspend")&&(value!="logout")&&(value!="")) {
135 errorCode = 405;
136 return "false";
137 }
138 eval(param+'="'+value+'";');
139 errorCode = 0;
140 return "true";
141 break;
142 case "cmi.core.lesson_location":
143 case "cmi.suspend_data":
144 eval(param+'="'+value+'";');
145 errorCode = 0;
146 return "true";
147 break;
148 case "cmi.core._children":
149 case "cmi.core.score._children":
150 errorCode = 402;
151 return "false";
152 break;
153 case "cmi.core.student_id":
154 case "cmi.core.student_name":
155 case "cmi.core.credit":
156 case "cmi.core.entry":
157 case "cmi.core.total_time":
158 case "cmi.core.lesson_mode":
159 case "cmi.launch_data":
160 errorCode = 403;
161 return "false";
162 break;
163 default:
164 //errorCode = 401; This is more correct but may have problem with some SCOes
165 errorCode = 0; // With this disable any possible SCO errors alert
166 return "false";
167 break;
168 }
169 } else {
170 errorCode = 301;
171 return "false";
172 }
173 }
174
175 function LMSCommit (param) {
176 if (param != "") {
177 errorCode = 201;
178 return "false";
179 }
180 if (Initialized) {
181 if (top.nav.cmi.document.theform) {
182 cmiform = top.nav.cmi.document.forms[0];
fc1b2663 183 cmiform.scoid.value = "<?php echo $sco->id; ?>";
98ca59f6 184 cmiform.cmi_core_lesson_location.value = cmi.core.lesson_location;
185 cmiform.cmi_core_lesson_status.value = cmi.core.lesson_status;
186 cmiform.cmi_core_exit.value = cmi.core.exit;
187 cmiform.cmi_core_session_time.value = cmi.core.session_time;
188 cmiform.cmi_core_total_time.value = cmi.core.total_time;
189 cmiform.cmi_core_score_raw.value = cmi.core.score.raw;
190 cmiform.cmi_suspend_data.value = cmi.suspend_data;
191 cmiform.submit();
192 }
193 errorCode = 0;
194 return "true";
195 } else {
196 errorCode = 301;
197 return "false";
198 }
199 }
200
201 function LMSFinish (param) {
202 if (param != "") {
203 errorCode = 201;
204 return "false";
205 }
206 if (!Initialized) {
207 errorCode = 301;
208 return "false";
209 } else {
210 Initialized = false;
211 errorCode = 0;
212 <?php
213 if ($scorm->auto) {
214 if ($sco != $last) {
215 print "setTimeout('top.nav.document.navform.next.click();',500);\n";
216 } else {
217 print "exitloc = '".$CFG->wwwroot."/mod/scorm/view.php?id=$cm->id';\n";
218 print "setTimeout('top.location = exitloc;',500);\n";
219 }
220 }
221 ?>
222 return "true";
223 }
224 }
225
226 function LMSGetLastError () {
227 return errorCode;
228 }
229
230 function LMSGetErrorString (param) {
231 var errorString = new Array();
232 errorString["0"] = "No error";
233 errorString["101"] = "General exception";
234 errorString["201"] = "Invalid argument error";
235 errorString["202"] = "Element cannot have children";
236 errorString["203"] = "Element not an array - cannot have count";
237 errorString["301"] = "Not initializated";
238 errorString["401"] = "Not implemented error";
239 errorString["402"] = "Invalid set value, element is a keyword";
240 errorString["403"] = "Element is read only";
241 errorString["404"] = "Element is write only";
242 errorString["405"] = "Incorrect data type";
243 return errorString[param];
244 }
245
246 function LMSGetDiagnostic (param) {
247 return param;
248 }
249
250 function AddTime (first, second) {
251 var sFirst = first.split(":");
252 var sSecond = second.split(":");
253 var change = 0;
254
255 var secs = (Math.round((parseFloat(sFirst[2],10)+parseFloat(sSecond[2],10))*100))/100; //Seconds
256 if (secs > 60) {
257 secs = secs - 60;
258 change = 1;
259 } else {
260 change = 0;
261 }
262 if (Math.floor(secs) < 10) secs = "0" + secs.toString();
263
264 mins = parseInt(sFirst[1],10)+parseInt(sSecond[1],10)+change; //Minutes
265 if (mins > 60)
266 change = 1;
267 else
268 change = 0;
269 if (mins < 10) mins = "0" + mins.toString();
270
271 hours = parseInt(sFirst[0],10)+parseInt(sSecond[0],10)+change; //Hours
272 if (hours < 10) hours = "0" + hours.toString();
273
274 return hours + ":" + mins + ":" + secs;
275 }
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();