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