Putting SCORM into maintree CVS, to make it easier to test.
[moodle.git] / mod / scorm / api1_2.php
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";
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 ?>";
11         cmi.core.credit = "credit";
12         cmi.core.lesson_status = "<?php echo $sco_user->cmi_core_lesson_status ?>";
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 '';?>";
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 $_GET["mode"] ?>";
24         
25         cmi.core.score = new Object();
26         cmi.core.score._children = "raw";
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 ?>";
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;
36 //
37 // end CMI Initialization
38 //
41 // 
42 // SCORM API 1.2 Implementation
43 //
44 function SCORMapi() {
45     var Initialized = false;
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";
122                     }
123                     eval(param+'="'+value+'";');
124                     errorCode = 0;
125                     return "true";
126                 break;
127                 case "cmi.core.exit":
128                     if ((value!="time-out")&&(value!="suspend")&&(value!="logout")&&(value!="")) {
129                         errorCode = 405;
130                         return "false";
131                     }
132                     eval(param+'="'+value+'";');
133                     errorCode = 0;
134                     return "true";
135                 break;
136                 case "cmi.core.lesson_location":
137                 case "cmi.suspend_data":
138                     eval(param+'="'+value+'";');
139                     errorCode = 0;
140                     return "true";
141                 break;
142                 case "cmi.core._children":
143                 case "cmi.core.score._children":
144                     errorCode = 402;
145                     return "false";
146                 break;
147                 case "cmi.core.student_id":
148                 case "cmi.core.student_name":
149                 case "cmi.core.credit":
150                 case "cmi.core.entry":
151                 case "cmi.core.total_time":
152                 case "cmi.core.lesson_mode":
153                 case "cmi.launch_data":
154                     errorCode = 403;
155                     return "false";
156                 break;
157                 default:
158                     //errorCode = 401;  This is more correct but may have problem with some SCOes
159                     errorCode = 0; // With this disable any possible SCO errors alert
160                     return "false";
161                 break;
162             }
163         } else {
164             errorCode = 301;
165             return "false";
166         }
167     }
169     function LMSCommit (param) {
170         if (param != "") {
171             errorCode = 201;
172             return "false";
173         }
174         if (Initialized) {
175             if (top.nav.cmi.document.theform) {
176                 cmiform = top.nav.cmi.document.forms[0];
177                 cmiform.scoid.value = "<?php echo  $sco->id ?>";
178                 cmiform.cmi_core_lesson_location.value = cmi.core.lesson_location;
179                 cmiform.cmi_core_lesson_status.value = cmi.core.lesson_status;
180                 cmiform.cmi_core_exit.value = cmi.core.exit;
181                 cmiform.cmi_core_session_time.value = cmi.core.session_time;
182                 cmiform.cmi_core_total_time.value = cmi.core.total_time;
183                 cmiform.cmi_core_score_raw.value = cmi.core.score.raw;
184                 cmiform.cmi_suspend_data.value = cmi.suspend_data;
185                 cmiform.submit();
186             }
187             errorCode = 0;
188             return "true";
189         } else {
190             errorCode = 301;
191             return "false";
192         }
193     }
194     
195     function LMSFinish (param) {
196         if (param != "") {
197             errorCode = 201;
198             return "false";
199         }
200         if (!Initialized) {
201             errorCode = 301;
202             return "false";
203         } else {
204             Initialized = false;
205             errorCode = 0;
206             <?php
207                  if ($scorm->auto) {
208                      if ($sco != $last) {
209                          print "setTimeout('top.nav.document.navform.next.click();',500);\n";
210                      } else {
211                          print "exitloc = '".$CFG->wwwroot."/mod/scorm/view.php?id=$cm->id';\n";
212                          print "setTimeout('top.location = exitloc;',500);\n";
213                      }
214                 } 
215             ?>
216             return "true";
217         }    
218     }
219     
220     function LMSGetLastError () {
221         return errorCode;
222     }
223     
224     function LMSGetErrorString (param) {
225         var errorString = new Array();
226         errorString["0"] = "No error";
227         errorString["101"] = "General exception";
228         errorString["201"] = "Invalid argument error";
229         errorString["202"] = "Element cannot have children";
230         errorString["203"] = "Element not an array - cannot have count";
231         errorString["301"] = "Not initializated";
232         errorString["401"] = "Not implemented error";
233         errorString["402"] = "Invalid set value, element is a keyword";
234         errorString["403"] = "Element is read only";
235         errorString["404"] = "Element is write only";
236         errorString["405"] = "Incorrect data type";
237         return errorString[param];
238     }
239     
240     function LMSGetDiagnostic (param) {
241         return param;
242     }
243         
244     function AddTime (first, second) {
245         var sFirst = first.split(":");
246         var sSecond = second.split(":");
247         var change = 0;
248         
249         var secs = (Math.round((parseFloat(sFirst[2],10)+parseFloat(sSecond[2],10))*100))/100;  //Seconds
250         if (secs > 60) {
251             secs = secs - 60;
252             change = 1;
253         } else {
254             change = 0;
255         }
256         if (Math.floor(secs) < 10) secs = "0" + secs.toString();
257         
258         mins = parseInt(sFirst[1],10)+parseInt(sSecond[1],10)+change;   //Minutes
259         if (mins > 60) 
260             change = 1;
261         else
262             change = 0;
263         if (mins < 10) mins = "0" + mins.toString();
264             
265         hours = parseInt(sFirst[0],10)+parseInt(sSecond[0],10)+change;  //Hours
266         if (hours < 10) hours = "0" + hours.toString();
267         
268         return hours + ":" + mins + ":" + secs;
269     }
270     this.LMSInitialize = LMSInitialize;
271     this.LMSGetValue = LMSGetValue;
272     this.LMSSetValue = LMSSetValue;
273     this.LMSCommit = LMSCommit;
274     this.LMSFinish = LMSFinish;
275     this.LMSGetLastError = LMSGetLastError;
276     this.LMSGetErrorString = LMSGetErrorString;
277     this.LMSGetDiagnostic = LMSGetDiagnostic;
280 var API = new SCORMapi();
282 function SCOInitialize() { 
283 <?php
284     if ( $sco->previous || $first) {
285         print "\ttop.nav.document.navform.prev.disabled = true;\n";
286         print "\ttop.nav.document.navform.prev.style.display = 'none';\n";
287     }
288     if ( $sco->next || $last) {
289         print "\ttop.nav.document.navform.next.disabled = true;\n";
290         print "\ttop.nav.document.navform.next.style.display = 'none';\n";
291     }
292 ?>
293         top.main.location="<?php echo $result ?>";
294
296 function changeSco(direction) {
297         if (direction == "prev")
298             top.nav.document.navform.scoid.value="<?php echo $prevsco ?>";
299         else
300             top.nav.document.navform.scoid.value="<?php echo $nextsco ?>";
301             
302         //alert ("Prev: <?php echo $prevsco ?>\nNext: <?php echo $nextsco ?>\nNew SCO: "+top.nav.document.navform.scoid.value);
303         top.nav.document.navform.submit();