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