MDL-7097 - API debug. Change the control of debugging over to SCORM admin settings.
[moodle.git] / mod / scorm / datamodels / scorm_12.js.php
1 <?php
2     require_once($CFG->dirroot.'/mod/scorm/locallib.php');
3     
4     if (isset($userdata->status)) {
5         if ($userdata->status == '') {
6             $userdata->entry = 'ab-initio';
7         } else {
8             if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
9                 $userdata->entry = 'resume';
10             } else {
11                 $userdata->entry = '';
12             }
13         }
14     }
15 ?>
16 //
17 // SCORM 1.2 API Implementation
18 //
19 function SCORMapi1_2() {
20     // Standard Data Type Definition
21     CMIString256 = '^[\\u0000-\\uffff]{0,255}$';
22     CMIString4096 = '^[\\u0000-\\uffff]{0,4096}$';
23     CMITime = '^([0-2]{1}[0-9]{1}):([0-5]{1}[0-9]{1}):([0-5]{1}[0-9]{1})(\.[0-9]{1,2})?$';
24     CMITimespan = '^([0-9]{2,4}):([0-9]{2}):([0-9]{2})(\.[0-9]{1,2})?$';
25     CMIInteger = '^\\d+$';
26     CMISInteger = '^-?([0-9]+)$';
27     CMIDecimal = '^-?([0-9]{0,3})(\.[0-9]{1,2})?$';
28     CMIIdentifier = '^\\w{1,255}$';
29     CMIFeedback = CMIString256; // This must be redefined
30     CMIIndex = '[._](\\d+).';
31     // Vocabulary Data Type Definition
32     CMIStatus = '^passed$|^completed$|^failed$|^incomplete$|^browsed$';
33     CMIStatus2 = '^passed$|^completed$|^failed$|^incomplete$|^browsed$|^not attempted$';
34     CMIExit = '^time-out$|^suspend$|^logout$|^$';
35     CMIType = '^true-false$|^choice$|^fill-in$|^matching$|^performance$|^sequencing$|^likert$|^numeric$';
36     CMIResult = '^correct$|^wrong$|^unanticipated$|^neutral$|^([0-9]{0,3})?(\.[0-9]{1,2})?$';
37     NAVEvent = '^previous$|^continue$';
38     // Children lists
39     cmi_children = 'core, suspend_data, launch_data, comments, objectives, student_data, student_preference, interactions';
40     core_children = 'student_id, student_name, lesson_location, credit, lesson_status, entry, score, total_time, lesson_mode, exit, session_time';
41     score_children = 'raw, min, max';
42     comments_children = 'content, location, time';
43     objectives_children = 'id, score, status';
44     student_data_children = 'mastery_score, max_time_allowed, time_limit_action';
45     student_preference_children = 'audio, language, speed, text';
46     interactions_children = 'id, objectives, time, type, correct_responses, weighting, student_response, result, latency';
47     // Data ranges
48     score_range = '0#100';
49     audio_range = '-1#100';
50     speed_range = '-100#100';
51     weighting_range = '-100#100';
52     text_range = '-1#1';
53     // The SCORM 1.2 data model
54     var datamodel =  {
55         'cmi._children':{'defaultvalue':cmi_children, 'mod':'r', 'writeerror':'402'},
56         'cmi._version':{'defaultvalue':'3.4', 'mod':'r', 'writeerror':'402'},
57         'cmi.core._children':{'defaultvalue':core_children, 'mod':'r', 'writeerror':'402'},
58         'cmi.core.student_id':{'defaultvalue':'<?php echo $userdata->student_id ?>', 'mod':'r', 'writeerror':'403'},
59         'cmi.core.student_name':{'defaultvalue':'<?php echo $userdata->student_name ?>', 'mod':'r', 'writeerror':'403'},
60         'cmi.core.lesson_location':{'defaultvalue':'<?php echo isset($userdata->{'cmi.core.lesson_location'})?$userdata->{'cmi.core.lesson_location'}:'' ?>', 'format':CMIString256, 'mod':'rw', 'writeerror':'405'},
61         'cmi.core.credit':{'defaultvalue':'<?php echo $userdata->credit ?>', 'mod':'r', 'writeerror':'403'},
62         'cmi.core.lesson_status':{'defaultvalue':'<?php echo isset($userdata->{'cmi.core.lesson_status'})?$userdata->{'cmi.core.lesson_status'}:'' ?>', 'format':CMIStatus, 'mod':'rw', 'writeerror':'405'},
63         'cmi.core.entry':{'defaultvalue':'<?php echo $userdata->entry ?>', 'mod':'r', 'writeerror':'403'},
64         'cmi.core.score._children':{'defaultvalue':score_children, 'mod':'r', 'writeerror':'402'},
65         'cmi.core.score.raw':{'defaultvalue':'<?php echo isset($userdata->{'cmi.core.score.raw'})?$userdata->{'cmi.core.score.raw'}:'' ?>', 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
66         'cmi.core.score.max':{'defaultvalue':'<?php echo isset($userdata->{'cmi.core.score.max'})?$userdata->{'cmi.core.score.max'}:'' ?>', 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
67         'cmi.core.score.min':{'defaultvalue':'<?php echo isset($userdata->{'cmi.core.score.min'})?$userdata->{'cmi.core.score.min'}:'' ?>', 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
68         'cmi.core.total_time':{'defaultvalue':'<?php echo isset($userdata->{'cmi.core.total_time'})?$userdata->{'cmi.core.total_time'}:'00:00:00' ?>', 'mod':'r', 'writeerror':'403'},
69         'cmi.core.lesson_mode':{'defaultvalue':'<?php echo $userdata->mode ?>', 'mod':'r', 'writeerror':'403'},
70         'cmi.core.exit':{'defaultvalue':'<?php echo isset($userdata->{'cmi.core.exit'})?$userdata->{'cmi.core.exit'}:'' ?>', 'format':CMIExit, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
71         'cmi.core.session_time':{'format':CMITimespan, 'mod':'w', 'defaultvalue':'00:00:00', 'readerror':'404', 'writeerror':'405'},
72         'cmi.suspend_data':{'defaultvalue':'<?php echo isset($userdata->{'cmi.suspend_data'})?$userdata->{'cmi.suspend_data'}:'' ?>', 'format':CMIString4096, 'mod':'rw', 'writeerror':'405'},
73         'cmi.launch_data':{'defaultvalue':'<?php echo isset($userdata->datafromlms)?$userdata->datafromlms:'' ?>', 'mod':'r', 'writeerror':'403'},
74         'cmi.comments':{'defaultvalue':'<?php echo isset($userdata->{'cmi.comments'})?$userdata->{'cmi.comments'}:'' ?>', 'format':CMIString4096, 'mod':'rw', 'writeerror':'405'},
75         // deprecated evaluation attributes
76         'cmi.evaluation.comments._count':{'defaultvalue':'0', 'mod':'r', 'writeerror':'402'},
77         'cmi.evaluation.comments._children':{'defaultvalue':comments_children, 'mod':'r', 'writeerror':'402'},
78         'cmi.evaluation.comments.n.content':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIString256, 'mod':'rw', 'writeerror':'405'},
79         'cmi.evaluation.comments.n.location':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIString256, 'mod':'rw', 'writeerror':'405'},
80         'cmi.evaluation.comments.n.time':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMITime, 'mod':'rw', 'writeerror':'405'},
81         'cmi.comments_from_lms':{'mod':'r', 'writeerror':'403'},
82         'cmi.objectives._children':{'defaultvalue':objectives_children, 'mod':'r', 'writeerror':'402'},
83         'cmi.objectives._count':{'mod':'r', 'defaultvalue':'0', 'writeerror':'402'},
84         'cmi.objectives.n.id':{'pattern':CMIIndex, 'format':CMIIdentifier, 'mod':'rw', 'writeerror':'405'},
85         'cmi.objectives.n.score._children':{'pattern':CMIIndex, 'mod':'r', 'writeerror':'402'},
86         'cmi.objectives.n.score.raw':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
87         'cmi.objectives.n.score.min':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
88         'cmi.objectives.n.score.max':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
89         'cmi.objectives.n.status':{'pattern':CMIIndex, 'format':CMIStatus2, 'mod':'rw', 'writeerror':'405'},
90         'cmi.student_data._children':{'defaultvalue':student_data_children, 'mod':'r', 'writeerror':'402'},
91         'cmi.student_data.mastery_score':{'defaultvalue':'<?php echo isset($userdata->masteryscore)?$userdata->masteryscore:'' ?>', 'mod':'r', 'writeerror':'403'},
92         'cmi.student_data.max_time_allowed':{'defaultvalue':'<?php echo isset($userdata->maxtimeallowed)?$userdata->maxtimeallowed:'' ?>', 'mod':'r', 'writeerror':'403'},
93         'cmi.student_data.time_limit_action':{'defaultvalue':'<?php echo isset($userdata->timelimitaction)?$userdata->timelimitaction:'' ?>', 'mod':'r', 'writeerror':'403'},
94         'cmi.student_preference._children':{'defaultvalue':student_preference_children, 'mod':'r', 'writeerror':'402'},
95         'cmi.student_preference.audio':{'defaultvalue':'0', 'format':CMISInteger, 'range':audio_range, 'mod':'rw', 'writeerror':'405'},
96         'cmi.student_preference.language':{'defaultvalue':'', 'format':CMIString256, 'mod':'rw', 'writeerror':'405'},
97         'cmi.student_preference.speed':{'defaultvalue':'0', 'format':CMISInteger, 'range':speed_range, 'mod':'rw', 'writeerror':'405'},
98         'cmi.student_preference.text':{'defaultvalue':'0', 'format':CMISInteger, 'range':text_range, 'mod':'rw', 'writeerror':'405'},
99         'cmi.interactions._children':{'defaultvalue':interactions_children, 'mod':'r', 'writeerror':'402'},
100         'cmi.interactions._count':{'mod':'r', 'defaultvalue':'0', 'writeerror':'402'},
101         'cmi.interactions.n.id':{'pattern':CMIIndex, 'format':CMIIdentifier, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
102         'cmi.interactions.n.objectives._count':{'pattern':CMIIndex, 'mod':'r', 'defaultvalue':'0', 'writeerror':'402'},
103         'cmi.interactions.n.objectives.n.id':{'pattern':CMIIndex, 'format':CMIIdentifier, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
104         'cmi.interactions.n.time':{'pattern':CMIIndex, 'format':CMITime, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
105         'cmi.interactions.n.type':{'pattern':CMIIndex, 'format':CMIType, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
106         'cmi.interactions.n.correct_responses._count':{'pattern':CMIIndex, 'mod':'r', 'defaultvalue':'0', 'writeerror':'402'},
107         'cmi.interactions.n.correct_responses.n.pattern':{'pattern':CMIIndex, 'format':CMIFeedback, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
108         'cmi.interactions.n.weighting':{'pattern':CMIIndex, 'format':CMIDecimal, 'range':weighting_range, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
109         'cmi.interactions.n.student_response':{'pattern':CMIIndex, 'format':CMIFeedback, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
110         'cmi.interactions.n.result':{'pattern':CMIIndex, 'format':CMIResult, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
111         'cmi.interactions.n.latency':{'pattern':CMIIndex, 'format':CMITimespan, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
112         'nav.event':{'defaultvalue':'', 'format':NAVEvent, 'mod':'w', 'readerror':'404', 'writeerror':'405'}
113     };
114     //
115     // Datamodel inizialization
116     //
117     var cmi = new Object();
118         cmi.core = new Object();
119         cmi.core.score = new Object();
120         cmi.objectives = new Object();
121         cmi.student_data = new Object();
122         cmi.student_preference = new Object();
123         cmi.interactions = new Object();
124         // deprecated evaluation attributes
125         cmi.evaluation = new Object();
126         cmi.evaluation.comments = new Object();
128     // Navigation Object
129     var nav = new Object();
131     for (element in datamodel) {
132         if (element.match(/\.n\./) == null) {
133             if ((typeof eval('datamodel["'+element+'"].defaultvalue')) != 'undefined') {
134                 eval(element+' = datamodel["'+element+'"].defaultvalue;');
135             } else {
136                 eval(element+' = "";');
137             }
138         }
139     }
141 <?php
142      // reconstitute objectives
143     scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.objectives', array('score'));
144 ?>
146     if (cmi.core.lesson_status == '') {
147         cmi.core.lesson_status = 'not attempted';
148     } 
149     
150     //
151     // API Methods definition
152     //
153     var Initialized = false;
155     function LMSInitialize (param) {
156         errorCode = "0";
157         if (param == "") {
158             if (!Initialized) {
159                 Initialized = true;
160                 errorCode = "0";
161                 <?php 
162                     if (scorm_debugging($scorm)) {
163                         //echo 'alert("Initialized SCORM 1.2");';
164                         echo 'LogAPICall("LMSInitialize", param, "", errorCode);';
165                     }
166                 ?>
167                 return "true";
168             } else {
169                 errorCode = "101";
170             }
171         } else {
172             errorCode = "201";
173         }
174         <?php 
175             if (scorm_debugging($scorm)) {
176                 echo 'LogAPICall("LMSInitialize", param, "", errorCode);';
177             }
178         ?>
179         return "false";
180     }
181     
182     function LMSFinish (param) {
183         errorCode = "0";
184         if (param == "") {
185             if (Initialized) {
186                 Initialized = false;
187                 result = StoreData(cmi,true);
188                 if (nav.event != '') {
189                     if (nav.event == 'continue') {
190                         setTimeout('top.document.location=top.next;',500);
191                     } else {
192                         setTimeout('top.document.location=top.prev;',500);
193                     }
194                 } else {
195                     if (<?php echo $scorm->auto ?> == 1) {
196                         setTimeout('top.document.location=top.next;',500);
197                     }
198                 }    
199                 <?php 
200                     if (scorm_debugging($scorm)) {
201                         //echo 'alert("Finished SCORM 1.2");';
202                         echo 'LogAPICall("LMSFinish", param, "", 0);';
203                     }
204                 ?>
205                 return "true";
206             } else {
207                 errorCode = "301";
208             }
209         } else {
210             errorCode = "201";
211         }
212         <?php 
213             if (scorm_debugging($scorm)) {
214                 echo 'LogAPICall("LMSFinish", param, "", errorCode);';
215             }
216         ?>
217         return "false";
218     }
219     
220     function LMSGetValue (element) {
221         errorCode = "0";
222         if (Initialized) {
223             if (element !="") {
224                 expression = new RegExp(CMIIndex,'g');
225                 elementmodel = String(element).replace(expression,'.n.');
226                 if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {
227                     if (eval('datamodel["'+elementmodel+'"].mod') != 'w') {
228                         element = String(element).replace(expression, "_$1.");
229                         elementIndexes = element.split('.');
230                         subelement = 'cmi';
231                         i = 1;
232                         while ((i < elementIndexes.length) && (typeof eval(subelement) != "undefined")) {
233                             subelement += '.'+elementIndexes[i++];
234                         }
235                             if (subelement == element) {
236                             errorCode = "0";
237                             <?php 
238                                 if (scorm_debugging($scorm)) {
239                                    //echo 'alert(element+": "+eval(element));';
240                                     echo 'LogAPICall("LMSGetValue", element, eval(element), 0);';
241                                 }
242                             ?>
243                             return eval(element);
244                         } else {
245                             errorCode = "0"; // Need to check if it is the right errorCode
246                         }
247                     } else {
248                         errorCode = eval('datamodel["'+elementmodel+'"].readerror');
249                     }
250                 } else {
251                     childrenstr = '._children';
252                     countstr = '._count';
253                     if (elementmodel.substr(elementmodel.length-childrenstr.length,elementmodel.length) == childrenstr) {
254                         parentmodel = elementmodel.substr(0,elementmodel.length-childrenstr.length);
255                         if ((typeof eval('datamodel["'+parentmodel+'"]')) != "undefined") {
256                             errorCode = "202";
257                         } else {
258                             errorCode = "201";
259                         }
260                     } else if (elementmodel.substr(elementmodel.length-countstr.length,elementmodel.length) == countstr) {
261                         parentmodel = elementmodel.substr(0,elementmodel.length-countstr.length);
262                         if ((typeof eval('datamodel["'+parentmodel+'"]')) != "undefined") {
263                             errorCode = "203";
264                         } else {
265                             errorCode = "201";
266                         }
267                     } else {
268                         errorCode = "201";
269                     }
270                 }
271             } else {
272                 errorCode = "201";
273             }
274         } else {
275             errorCode = "301";
276         }
277         <?php 
278             if (scorm_debugging($scorm)) {
279                 echo 'LogAPICall("LMSGetValue", element, "", errorCode);';
280             }
281         ?>
282         return "";
283     }
284     
285     function LMSSetValue (element,value) {
286         errorCode = "0";
287         if (Initialized) {
288             if (element != "") {
289                 expression = new RegExp(CMIIndex,'g');
290                 elementmodel = String(element).replace(expression,'.n.');
291                 if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {
292                     if (eval('datamodel["'+elementmodel+'"].mod') != 'r') {
293                         expression = new RegExp(eval('datamodel["'+elementmodel+'"].format'));
294                         value = value+'';
295                         matches = value.match(expression);
296                         if (matches != null) {
297                             //Create dynamic data model element
298                             if (element != elementmodel) {
299                                 elementIndexes = element.split('.');
300                                 subelement = 'cmi';
301                                 for (i=1;i < elementIndexes.length-1;i++) {
302                                     elementIndex = elementIndexes[i];
303                                     if (elementIndexes[i+1].match(/^\d+$/)) {
304                                         if ((typeof eval(subelement+'.'+elementIndex)) == "undefined") {
305                                             eval(subelement+'.'+elementIndex+' = new Object();');
306                                             eval(subelement+'.'+elementIndex+'._count = 0;');
307                                         }
308                                         if (elementIndexes[i+1] == eval(subelement+'.'+elementIndex+'._count')) {
309                                             eval(subelement+'.'+elementIndex+'._count++;');
310                                         } 
311                                         if (elementIndexes[i+1] > eval(subelement+'.'+elementIndex+'._count')) {
312                                             errorCode = "201";
313                                         }
314                                         subelement = subelement.concat('.'+elementIndex+'_'+elementIndexes[i+1]);
315                                         i++;
316                                     } else {
317                                         subelement = subelement.concat('.'+elementIndex);
318                                     }
319                                     if ((typeof eval(subelement)) == "undefined") {
320                                         eval(subelement+' = new Object();');
321                                         if (subelement.substr(0,14) == 'cmi.objectives') {
322                                             eval(subelement+'.score = new Object();');
323                                             eval(subelement+'.score._children = score_children;');
324                                             eval(subelement+'.score.raw = "";');
325                                             eval(subelement+'.score.min = "";');
326                                             eval(subelement+'.score.max = "";');
327                                         }
328                                         if (subelement.substr(0,16) == 'cmi.interactions') {
329                                             eval(subelement+'.objectives = new Object();');
330                                             eval(subelement+'.objectives._count = 0;');
331                                             eval(subelement+'.correct_responses = new Object();');
332                                             eval(subelement+'.correct_responses._count = 0;');
333                                         }
334                                     }
335                                 }
336                                 element = subelement.concat('.'+elementIndexes[elementIndexes.length-1]);
337                             }
338                             //Store data
339                             if (errorCode == "0") {
340                                 if ((typeof eval('datamodel["'+elementmodel+'"].range')) != "undefined") {
341                                     range = eval('datamodel["'+elementmodel+'"].range');
342                                     ranges = range.split('#');
343                                     value = value*1.0;
344                                     if ((value >= ranges[0]) && (value <= ranges[1])) {
345                                         eval(element+'=value;');
346                                         errorCode = "0";
347                                         <?php 
348                                             if (scorm_debugging($scorm)) {
349                                                 echo 'LogAPICall("LMSSetValue", element, value, errorCode);';
350                                                 //echo 'alert(element+":= "+value);';
351                                             }
352                                         ?>
353                                         return "true";
354                                     } else {
355                                         errorCode = eval('datamodel["'+elementmodel+'"].writeerror');
356                                     }
357                                 } else {
358                                     if (element == 'cmi.comments') {
359                                         cmi.comments = cmi.comments + value;
360                                     } else {
361                                         eval(element+'=value;');
362                                     }
363                                     errorCode = "0";
364                                     <?php 
365                                         if (scorm_debugging($scorm)) {
366                                             echo 'LogAPICall("LMSSetValue", element, value, errorCode);';
367                                             //echo 'alert(element+":= "+value);';
368                                         }
369                                     ?>
370                                     return "true";
371                                 }
372                             }
373                         } else {
374                             errorCode = eval('datamodel["'+elementmodel+'"].writeerror');
375                         }
376                     } else {
377                         errorCode = eval('datamodel["'+elementmodel+'"].writeerror');
378                     }
379                 } else {
380                     errorCode = "201"
381                 }
382             } else {
383                 errorCode = "201";
384             }
385         } else {
386             errorCode = "301";
387         }
388        <?php 
389         if (scorm_debugging($scorm)) {
390             echo 'LogAPICall("LMSSetValue", element, value, errorCode);';
391         }
392         ?>
393         return "false";
394     }
395     
396     function LMSCommit (param) {
397         errorCode = "0";
398         if (param == "") {
399             if (Initialized) {
400                 result = StoreData(cmi,false);
401                 <?php 
402                     if (scorm_debugging($scorm)) {
403                         echo 'LogAPICall("Commit", param, "", 0);';
404                         //echo 'alert("Data Commited");';
405                     }
406                 ?>
407                 return "true";
408             } else {
409                 errorCode = "301";
410             }
411         } else {
412             errorCode = "201";
413         }
414         <?php 
415             if (scorm_debugging($scorm)) {
416                 echo 'LogAPICall("LMSCommit", param, "", 0);';
417             }
418         ?>
419         return "false";
420     }
421     
422     function LMSGetLastError () {
423      <?php 
424         if (scorm_debugging($scorm)) {
425             echo 'LogAPICall("LMSGetLastError", "", "", errorCode);';
426         }
427     ?>
428         return errorCode;
429     }
430     
431     function LMSGetErrorString (param) {
432         if (param != "") {
433             var errorString = new Array();
434             errorString["0"] = "No error";
435             errorString["101"] = "General exception";
436             errorString["201"] = "Invalid argument error";
437             errorString["202"] = "Element cannot have children";
438             errorString["203"] = "Element not an array - cannot have count";
439             errorString["301"] = "Not initialized";
440             errorString["401"] = "Not implemented error";
441             errorString["402"] = "Invalid set value, element is a keyword";
442             errorString["403"] = "Element is read only";
443             errorString["404"] = "Element is write only";
444             errorString["405"] = "Incorrect data type";
445             <?php 
446             if (scorm_debugging($scorm)) {
447                 echo 'LogAPICall("LMSGetErrorString", param,  errorString[param], 0);';
448             }
449              ?>
450             return errorString[param];
451         } else {
452            <?php 
453             if (scorm_debugging($scorm)) {
454                 echo 'LogAPICall("LMSGetErrorString", param,  "No error string found!", 0);';
455             }
456              ?>
457            return "";
458         }
459     }
460     
461     function LMSGetDiagnostic (param) {
462         if (param == "") {
463             param = errorCode;
464         }
465         <?php 
466             if (scorm_debugging($scorm)) {
467                 echo 'LogAPICall("LMSGetDiagnostic", param, param, 0);';
468             }
469         ?>
470         return param;
471     }
473     function AddTime (first, second) {
474         var sFirst = first.split(":");
475         var sSecond = second.split(":");
476         var cFirst = sFirst[2].split(".");
477         var cSecond = sSecond[2].split(".");
478         var change = 0;
480         FirstCents = 0;  //Cents
481         if (cFirst.length > 1) {
482             FirstCents = parseInt(cFirst[1],10);
483         }
484         SecondCents = 0;
485         if (cSecond.length > 1) {
486             SecondCents = parseInt(cSecond[1],10);
487         }
488         var cents = FirstCents + SecondCents;
489         change = Math.floor(cents / 100);
490         cents = cents - (change * 100);
491         if (Math.floor(cents) < 10) {
492             cents = "0" + cents.toString();
493         }
495         var secs = parseInt(cFirst[0],10)+parseInt(cSecond[0],10)+change;  //Seconds
496         change = Math.floor(secs / 60);
497         secs = secs - (change * 60);
498         if (Math.floor(secs) < 10) {
499             secs = "0" + secs.toString();
500         }
502         mins = parseInt(sFirst[1],10)+parseInt(sSecond[1],10)+change;   //Minutes
503         change = Math.floor(mins / 60);
504         mins = mins - (change * 60);
505         if (mins < 10) {
506             mins = "0" + mins.toString();
507         }
509         hours = parseInt(sFirst[0],10)+parseInt(sSecond[0],10)+change;  //Hours
510         if (hours < 10) {
511             hours = "0" + hours.toString();
512         }
514         if (cents != '0') {
515             return hours + ":" + mins + ":" + secs + '.' + cents;
516         } else {
517             return hours + ":" + mins + ":" + secs;
518         }
519     }
521     function TotalTime() {
522         total_time = AddTime(cmi.core.total_time, cmi.core.session_time);
523         return '&'+underscore('cmi.core.total_time')+'='+encodeURIComponent(total_time);
524     }
526     function CollectData(data,parent) {
527         var datastring = '';
528         for (property in data) {
529             if (typeof data[property] == 'object') {
530                 datastring += CollectData(data[property],parent+'.'+property);
531             } else {
532                 element = parent+'.'+property;
533                 expression = new RegExp(CMIIndex,'g');
534                 elementmodel = String(element).replace(expression,'.n.');
535                 if (elementmodel != "cmi.core.session_time") {
536                     if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {
537                         if (eval('datamodel["'+elementmodel+'"].mod') != 'r') {
538                             elementstring = '&'+underscore(element)+'='+encodeURIComponent(data[property]);
539                             if ((typeof eval('datamodel["'+elementmodel+'"].defaultvalue')) != "undefined") {
540                                 if (eval('datamodel["'+elementmodel+'"].defaultvalue') != data[property]) {
541                                     datastring += elementstring;
542                                 }
543                             } else {
544                                 datastring += elementstring;
545                             }
546                         }
547                     }
548                 }
549             }
550         }
551         return datastring;
552     }
554     function StoreData(data,storetotaltime) {
555         if (storetotaltime) {
556             if (cmi.core.lesson_status == 'not attempted') {
557                 cmi.core.lesson_status = 'completed';
558             }
559             if (cmi.core.lesson_mode == 'normal') {
560                 if (cmi.core.credit == 'credit') {
561                     if (cmi.core.lesson_status == 'completed') {
562                         if (cmi.student_data.mastery_score != '' && cmi.core.score.raw != '') {
563                             if (parseFloat(cmi.core.score.raw) >= parseFloat(cmi.student_data.mastery_score)) {
564                                 cmi.core.lesson_status = 'passed';
565                             } else {
566                                 cmi.core.lesson_status = 'failed';
567                             }
568                         }
569                     }
570                 }
571             }
572             if (cmi.core.lesson_mode == 'browse') {
573                 if (datamodel['cmi.core.lesson_status'].defaultvalue == '' && cmi.core.lesson_status == 'not attempted') {
574                     cmi.core.lesson_status = 'browsed';
575                 }
576             }
577             datastring = CollectData(data,'cmi');
578             datastring += TotalTime();
579         } else {
580             datastring = CollectData(data,'cmi');
581         }
582         datastring += '&attempt=<?php echo $attempt ?>';
583         datastring += '&scoid=<?php echo $scoid ?>';
584         
585         var myRequest = NewHttpReq();
586         result = DoRequest(myRequest,"<?php p($CFG->wwwroot) ?>/mod/scorm/datamodel.php","id=<?php p($id) ?>&sesskey=<?php p($USER->sesskey) ?>"+datastring);
587         results = String(result).split('\n');
588         errorCode = results[1];
589         return results[0];
590     }
592     this.LMSInitialize = LMSInitialize;
593     this.LMSFinish = LMSFinish;
594     this.LMSGetValue = LMSGetValue;
595     this.LMSSetValue = LMSSetValue;
596     this.LMSCommit = LMSCommit;
597     this.LMSGetLastError = LMSGetLastError;
598     this.LMSGetErrorString = LMSGetErrorString;
599     this.LMSGetDiagnostic = LMSGetDiagnostic;
602 var API = new SCORMapi1_2();
604 <?php
605 // pull in the debugging utilities
606 if (scorm_debugging($scorm)) {
607     include_once($CFG->dirroot.'/mod/scorm/datamodels/debug.js.php');
608     echo 'AppendToLog("Moodle SCORM 1.2 API Loaded, Activity: '.$scorm->name.', SCO: '.$sco->identifier.'", 0);';
610  ?>