153beb5929c47541c83b4dd84d62cf0d05de5505
[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     }
188     this.connectPrereqCallback = {
190         success: function(o) {
191             scorm_tree_node = YAHOO.widget.TreeView.getTree('scorm_tree');
192             if (o.responseText !== undefined) {
193                 if (scorm_tree_node && o.responseText) {
194                     var hnode = scorm_tree_node.getHighlightedNode();
195                     var hidx = null;
196                     if (hnode) {
197                         hidx = hnode.index + scorm_tree_node.getNodeCount();
198                     }
199                     // all gone
200                     var root_node = scorm_tree_node.getRoot();
201                         scorm_tree_node.removeNode(root_node.children[0]);
202                     }
203                     // make sure the temporary tree element is not there
204                     var el_old_tree = document.getElementById('scormtree123');
205                     if (el_old_tree) {
206                         el_old_tree.parentNode.removeChild(el_old_tree);
207                     }
208                     var el_new_tree = document.createElement('div');
209                     var pagecontent = document.getElementById("page-content");
210                     el_new_tree.setAttribute('id','scormtree123');
211                     el_new_tree.innerHTML = o.responseText;
212                     // make sure it doesnt show
213                     el_new_tree.style.display = 'none';
214                     pagecontent.appendChild(el_new_tree)
215                     // ignore the first level element as this is the title
216                     scorm_tree_node.buildTreeFromMarkup(el_new_tree.firstChild.firstChild);
217                     var el = document.getElementById('scormtree123');
218                     el.parentNode.removeChild(el);
219                     scorm_tree_node.expandAll();
220                     scorm_tree_node.render();
221                     if (hidx != null) {
222                         hnode = scorm_tree_node.getNodeByIndex(hidx);
223                         if (hnode) {
224                             hnode.highlight();
225                             scorm_layout_widget = YAHOO.widget.Layout.getLayoutById('scorm_layout');
226                             var left = scorm_layout_widget.getUnitByPosition('left');
227                             if (left.expanded) {
228                                 hnode.focus();
229                             }
230                         }
231                     }
232                 }
233         },
235         failure: function(o) {
236             // do some sort of error handling
237             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(); ?>";
238             //TODO: Enable this error handing correctly - avoiding issues when closing player MDL-23470 
239             //alert('Prerequisites update failed - must restart SCORM player');
240             //window.location.href = sURL;
241         }
243     };
246     function LMSFinish (param) {
247         errorCode = "0";
248         if (param == "") {
249             if (Initialized) {
250                 Initialized = false;
251                 result = StoreData(cmi,true);
252                 if (nav.event != '') {
253                     if (nav.event == 'continue') {
254                         setTimeout('top.document.location=top.next;',500);
255                     } else {
256                         setTimeout('top.document.location=top.prev;',500);
257                     }
258                 } else {
259                     if (<?php echo $scorm->auto ?> == 1) {
260                         setTimeout('top.document.location=top.next;',500);
261                     }
262                 }
263                 <?php
264                     if (scorm_debugging($scorm)) {
265                         echo 'LogAPICall("LMSFinish", "AJAXResult", result, 0);';
266                     }
267                 ?>
268                 result = ('true' == result) ? 'true' : 'false';
269                 errorCode = (result == 'true')? '0' : '101';
270                 <?php
271                     if (scorm_debugging($scorm)) {
272                         //echo 'alert("Finished SCORM 1.2");';
273                         echo 'LogAPICall("LMSFinish", "result", result, 0);';
274                         echo 'LogAPICall("LMSFinish", param, "", 0);';
275                     }
276                 ?>
277                 // trigger TOC update
278                 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(); ?>";
279                 YAHOO.util.Connect.asyncRequest('GET', sURL, this.connectPrereqCallback, null);
280                 return result;
281             } else {
282                 errorCode = "301";
283             }
284         } else {
285             errorCode = "201";
286         }
287         <?php
288             if (scorm_debugging($scorm)) {
289                 echo 'LogAPICall("LMSFinish", param, "", errorCode);';
290             }
291         ?>
292         return "false";
293     }
295     function LMSGetValue (element) {
296         errorCode = "0";
297         if (Initialized) {
298             if (element !="") {
299                 expression = new RegExp(CMIIndex,'g');
300                 elementmodel = String(element).replace(expression,'.n.');
301                 if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {
302                     if (eval('datamodel["'+elementmodel+'"].mod') != 'w') {
303                         element = String(element).replace(expression, "_$1.");
304                         elementIndexes = element.split('.');
305                         subelement = 'cmi';
306                         i = 1;
307                         while ((i < elementIndexes.length) && (typeof eval(subelement) != "undefined")) {
308                             subelement += '.'+elementIndexes[i++];
309                         }
310                             if (subelement == element) {
311                             errorCode = "0";
312                             <?php
313                                 if (scorm_debugging($scorm)) {
314                                    //echo 'alert(element+": "+eval(element));';
315                                     echo 'LogAPICall("LMSGetValue", element, eval(element), 0);';
316                                 }
317                             ?>
318                             return eval(element);
319                         } else {
320                             errorCode = "0"; // Need to check if it is the right errorCode
321                         }
322                     } else {
323                         errorCode = eval('datamodel["'+elementmodel+'"].readerror');
324                     }
325                 } else {
326                     childrenstr = '._children';
327                     countstr = '._count';
328                     if (elementmodel.substr(elementmodel.length-childrenstr.length,elementmodel.length) == childrenstr) {
329                         parentmodel = elementmodel.substr(0,elementmodel.length-childrenstr.length);
330                         if ((typeof eval('datamodel["'+parentmodel+'"]')) != "undefined") {
331                             errorCode = "202";
332                         } else {
333                             errorCode = "201";
334                         }
335                     } else if (elementmodel.substr(elementmodel.length-countstr.length,elementmodel.length) == countstr) {
336                         parentmodel = elementmodel.substr(0,elementmodel.length-countstr.length);
337                         if ((typeof eval('datamodel["'+parentmodel+'"]')) != "undefined") {
338                             errorCode = "203";
339                         } else {
340                             errorCode = "201";
341                         }
342                     } else {
343                         errorCode = "201";
344                     }
345                 }
346             } else {
347                 errorCode = "201";
348             }
349         } else {
350             errorCode = "301";
351         }
352         <?php
353             if (scorm_debugging($scorm)) {
354                 echo 'LogAPICall("LMSGetValue", element, "", errorCode);';
355             }
356         ?>
357         return "";
358     }
360     function LMSSetValue (element,value) {
361         errorCode = "0";
362         if (Initialized) {
363             if (element != "") {
364                 expression = new RegExp(CMIIndex,'g');
365                 elementmodel = String(element).replace(expression,'.n.');
366                 if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {
367                     if (eval('datamodel["'+elementmodel+'"].mod') != 'r') {
368                         expression = new RegExp(eval('datamodel["'+elementmodel+'"].format'));
369                         value = value+'';
370                         matches = value.match(expression);
371                         if (matches != null) {
372                             //Create dynamic data model element
373                             if (element != elementmodel) {
374                                 elementIndexes = element.split('.');
375                                 subelement = 'cmi';
376                                 for (i=1;i < elementIndexes.length-1;i++) {
377                                     elementIndex = elementIndexes[i];
378                                     if (elementIndexes[i+1].match(/^\d+$/)) {
379                                         if ((typeof eval(subelement+'.'+elementIndex)) == "undefined") {
380                                             eval(subelement+'.'+elementIndex+' = new Object();');
381                                             eval(subelement+'.'+elementIndex+'._count = 0;');
382                                         }
383                                         if (elementIndexes[i+1] == eval(subelement+'.'+elementIndex+'._count')) {
384                                             eval(subelement+'.'+elementIndex+'._count++;');
385                                         }
386                                         if (elementIndexes[i+1] > eval(subelement+'.'+elementIndex+'._count')) {
387                                             errorCode = "201";
388                                         }
389                                         subelement = subelement.concat('.'+elementIndex+'_'+elementIndexes[i+1]);
390                                         i++;
391                                     } else {
392                                         subelement = subelement.concat('.'+elementIndex);
393                                     }
394                                     if ((typeof eval(subelement)) == "undefined") {
395                                         eval(subelement+' = new Object();');
396                                         if (subelement.substr(0,14) == 'cmi.objectives') {
397                                             eval(subelement+'.score = new Object();');
398                                             eval(subelement+'.score._children = score_children;');
399                                             eval(subelement+'.score.raw = "";');
400                                             eval(subelement+'.score.min = "";');
401                                             eval(subelement+'.score.max = "";');
402                                         }
403                                         if (subelement.substr(0,16) == 'cmi.interactions') {
404                                             eval(subelement+'.objectives = new Object();');
405                                             eval(subelement+'.objectives._count = 0;');
406                                             eval(subelement+'.correct_responses = new Object();');
407                                             eval(subelement+'.correct_responses._count = 0;');
408                                         }
409                                     }
410                                 }
411                                 element = subelement.concat('.'+elementIndexes[elementIndexes.length-1]);
412                             }
413                             //Store data
414                             if (errorCode == "0") {
415                                 if ((typeof eval('datamodel["'+elementmodel+'"].range')) != "undefined") {
416                                     range = eval('datamodel["'+elementmodel+'"].range');
417                                     ranges = range.split('#');
418                                     value = value*1.0;
419                                     if ((value >= ranges[0]) && (value <= ranges[1])) {
420                                         eval(element+'=value;');
421                                         errorCode = "0";
422                                         <?php
423                                             if (scorm_debugging($scorm)) {
424                                                 echo 'LogAPICall("LMSSetValue", element, value, errorCode);';
425                                                 //echo 'alert(element+":= "+value);';
426                                             }
427                                         ?>
428                                         return "true";
429                                     } else {
430                                         errorCode = eval('datamodel["'+elementmodel+'"].writeerror');
431                                     }
432                                 } else {
433                                     if (element == 'cmi.comments') {
434                                         cmi.comments = cmi.comments + value;
435                                     } else {
436                                         eval(element+'=value;');
437                                     }
438                                     errorCode = "0";
439                                     <?php
440                                         if (scorm_debugging($scorm)) {
441                                             echo 'LogAPICall("LMSSetValue", element, value, errorCode);';
442                                             //echo 'alert(element+":= "+value);';
443                                         }
444                                     ?>
445                                     return "true";
446                                 }
447                             }
448                         } else {
449                             errorCode = eval('datamodel["'+elementmodel+'"].writeerror');
450                         }
451                     } else {
452                         errorCode = eval('datamodel["'+elementmodel+'"].writeerror');
453                     }
454                 } else {
455                     errorCode = "201"
456                 }
457             } else {
458                 errorCode = "201";
459             }
460         } else {
461             errorCode = "301";
462         }
463        <?php
464         if (scorm_debugging($scorm)) {
465             echo 'LogAPICall("LMSSetValue", element, value, errorCode);';
466         }
467         ?>
468         return "false";
469     }
471     function LMSCommit (param) {
472         errorCode = "0";
473         if (param == "") {
474             if (Initialized) {
475                 result = StoreData(cmi,false);
476                 <?php
477                     if (scorm_debugging($scorm)) {
478                         echo 'LogAPICall("Commit", param, "", 0);';
479                         //echo 'alert("Data Commited");';
480                     }
481                 ?>
482                 <?php
483                     if (scorm_debugging($scorm)) {
484                         echo 'LogAPICall("LMSCommit", "AJAXResult", result, 0);';
485                     }
486                 ?>
487                 result = ('true' == result) ? 'true' : 'false';
488                 errorCode = (result ==' true')? '0' : '101';
489                 <?php 
490                     if (scorm_debugging($scorm)) {
491                         //echo 'alert("Finished SCORM 1.2");';
492                         echo 'LogAPICall("LMSCommit", "result", result, 0);';
493                         echo 'LogAPICall("LMSCommit", param, "", 0);';
494                     }
495                 ?>
496                 return result;
497             } else {
498                 errorCode = "301";
499             }
500         } else {
501             errorCode = "201";
502         }
503         <?php
504             if (scorm_debugging($scorm)) {
505                 echo 'LogAPICall("LMSCommit", param, "", 0);';
506             }
507         ?>
508         return "false";
509     }
511     function LMSGetLastError () {
512      <?php
513         if (scorm_debugging($scorm)) {
514             echo 'LogAPICall("LMSGetLastError", "", "", errorCode);';
515         }
516     ?>
517         return errorCode;
518     }
520     function LMSGetErrorString (param) {
521         if (param != "") {
522             var errorString = new Array();
523             errorString["0"] = "No error";
524             errorString["101"] = "General exception";
525             errorString["201"] = "Invalid argument error";
526             errorString["202"] = "Element cannot have children";
527             errorString["203"] = "Element not an array - cannot have count";
528             errorString["301"] = "Not initialized";
529             errorString["401"] = "Not implemented error";
530             errorString["402"] = "Invalid set value, element is a keyword";
531             errorString["403"] = "Element is read only";
532             errorString["404"] = "Element is write only";
533             errorString["405"] = "Incorrect data type";
534             <?php
535             if (scorm_debugging($scorm)) {
536                 echo 'LogAPICall("LMSGetErrorString", param,  errorString[param], 0);';
537             }
538              ?>
539             return errorString[param];
540         } else {
541            <?php
542             if (scorm_debugging($scorm)) {
543                 echo 'LogAPICall("LMSGetErrorString", param,  "No error string found!", 0);';
544             }
545              ?>
546            return "";
547         }
548     }
550     function LMSGetDiagnostic (param) {
551         if (param == "") {
552             param = errorCode;
553         }
554         <?php
555             if (scorm_debugging($scorm)) {
556                 echo 'LogAPICall("LMSGetDiagnostic", param, param, 0);';
557             }
558         ?>
559         return param;
560     }
562     function AddTime (first, second) {
563         var sFirst = first.split(":");
564         var sSecond = second.split(":");
565         var cFirst = sFirst[2].split(".");
566         var cSecond = sSecond[2].split(".");
567         var change = 0;
569         FirstCents = 0;  //Cents
570         if (cFirst.length > 1) {
571             FirstCents = parseInt(cFirst[1],10);
572         }
573         SecondCents = 0;
574         if (cSecond.length > 1) {
575             SecondCents = parseInt(cSecond[1],10);
576         }
577         var cents = FirstCents + SecondCents;
578         change = Math.floor(cents / 100);
579         cents = cents - (change * 100);
580         if (Math.floor(cents) < 10) {
581             cents = "0" + cents.toString();
582         }
584         var secs = parseInt(cFirst[0],10)+parseInt(cSecond[0],10)+change;  //Seconds
585         change = Math.floor(secs / 60);
586         secs = secs - (change * 60);
587         if (Math.floor(secs) < 10) {
588             secs = "0" + secs.toString();
589         }
591         mins = parseInt(sFirst[1],10)+parseInt(sSecond[1],10)+change;   //Minutes
592         change = Math.floor(mins / 60);
593         mins = mins - (change * 60);
594         if (mins < 10) {
595             mins = "0" + mins.toString();
596         }
598         hours = parseInt(sFirst[0],10)+parseInt(sSecond[0],10)+change;  //Hours
599         if (hours < 10) {
600             hours = "0" + hours.toString();
601         }
603         if (cents != '0') {
604             return hours + ":" + mins + ":" + secs + '.' + cents;
605         } else {
606             return hours + ":" + mins + ":" + secs;
607         }
608     }
610     function TotalTime() {
611         total_time = AddTime(cmi.core.total_time, cmi.core.session_time);
612         return '&'+underscore('cmi.core.total_time')+'='+encodeURIComponent(total_time);
613     }
615     function CollectData(data,parent) {
616         var datastring = '';
617         for (property in data) {
618             if (typeof data[property] == 'object') {
619                 datastring += CollectData(data[property],parent+'.'+property);
620             } else {
621                 element = parent+'.'+property;
622                 expression = new RegExp(CMIIndex,'g');
623                 elementmodel = String(element).replace(expression,'.n.');
624                 if (elementmodel != "cmi.core.session_time") {
625                     if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {
626                         if (eval('datamodel["'+elementmodel+'"].mod') != 'r') {
627                             elementstring = '&'+underscore(element)+'='+encodeURIComponent(data[property]);
628                             if ((typeof eval('datamodel["'+elementmodel+'"].defaultvalue')) != "undefined") {
629                                 if (eval('datamodel["'+elementmodel+'"].defaultvalue') != data[property] || eval('typeof(datamodel["'+elementmodel+'"].defaultvalue)') != typeof(data[property])) {
630                                     datastring += elementstring;
631                                     eval('datamodel["'+elementmodel+'"].defaultvalue=data[property];');
632                                 }
633                             } else {
634                                 datastring += elementstring;
635                                 eval('datamodel["'+elementmodel+'"].defaultvalue=data[property];');
636                             }
637                         }
638                     }
639                 }
640             }
641         }
642         return datastring;
643     }
645     function StoreData(data,storetotaltime) {
646         if (storetotaltime) {
647             if (cmi.core.lesson_status == 'not attempted') {
648                 cmi.core.lesson_status = 'completed';
649             }
650             if (cmi.core.lesson_mode == 'normal') {
651                 if (cmi.core.credit == 'credit') {
652                     if (cmi.student_data.mastery_score != '' && cmi.core.score.raw != '') {
653                         if (parseFloat(cmi.core.score.raw) >= parseFloat(cmi.student_data.mastery_score)) {
654                             cmi.core.lesson_status = 'passed';
655                         } else {
656                             cmi.core.lesson_status = 'failed';
657                         }
658                     }
659                 }
660             }
661             if (cmi.core.lesson_mode == 'browse') {
662                 if (datamodel['cmi.core.lesson_status'].defaultvalue == '' && cmi.core.lesson_status == 'not attempted') {
663                     cmi.core.lesson_status = 'browsed';
664                 }
665             }
666             datastring = CollectData(data,'cmi');
667             datastring += TotalTime();
668         } else {
669             datastring = CollectData(data,'cmi');
670         }
671         datastring += '&attempt=<?php echo $attempt ?>';
672         datastring += '&scoid=<?php echo $scoid ?>';
674         var myRequest = NewHttpReq();
675         //alert('going to:' + "<?php p($CFG->wwwroot) ?>/mod/scorm/datamodel.php" + "id=<?php p($id) ?>&a=<?php p($a) ?>&sesskey=<?php echo sesskey() ?>"+datastring);
676         result = DoRequest(myRequest,"<?php p($CFG->wwwroot) ?>/mod/scorm/datamodel.php","id=<?php p($id) ?>&a=<?php p($a) ?>&sesskey=<?php echo sesskey() ?>"+datastring);
677         results = String(result).split('\n');
678         errorCode = results[1];
679         return results[0];
680     }
682     this.LMSInitialize = LMSInitialize;
683     this.LMSFinish = LMSFinish;
684     this.LMSGetValue = LMSGetValue;
685     this.LMSSetValue = LMSSetValue;
686     this.LMSCommit = LMSCommit;
687     this.LMSGetLastError = LMSGetLastError;
688     this.LMSGetErrorString = LMSGetErrorString;
689     this.LMSGetDiagnostic = LMSGetDiagnostic;
692 var API = new SCORMapi1_2();
694 <?php
695 // pull in the debugging utilities
696 if (scorm_debugging($scorm)) {
697     include_once($CFG->dirroot.'/mod/scorm/datamodels/debug.js.php');
698     echo 'AppendToLog("Moodle SCORM 1.2 API Loaded, Activity: '.$scorm->name.', SCO: '.$sco->identifier.'", 0);';
700  ?>