Merge branch 'MDL-57660-master' of git://github.com/junpataleta/moodle
[moodle.git] / mod / scorm / datamodels / aicc.js
1 // This file is part of Moodle - http://moodle.org/
2 //
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16 //
17 // SCORM 1.2 API Implementation
18 //
19 function AICCapi(def, cmiobj, scormauto, cfgwwwroot, scormid, scoid, attempt, viewmode, currentorg, sesskey, cmid, autocommit) {
21     var prerequrl = cfgwwwroot + "/mod/scorm/prereqs.php?a=" + scormid + "&scoid=" + scoid + "&attempt=" + attempt + "&mode=" + viewmode + "&currentorg=" + currentorg + "&sesskey=" + sesskey;
22     var datamodelurl = cfgwwwroot + "/mod/scorm/datamodel.php";
23     var datamodelurlparams = "id=" + cmid + "&a=" + scormid + "&sesskey=" + sesskey + "&attempt=" + attempt + "&scoid=" + scoid;
25     // Standard Data Type Definition
26     CMIString256 = '^.{0,255}$';
27     CMIString4096 = '^.{0,4096}$';
28     CMITime = '^([0-2]{1}[0-9]{1}):([0-5]{1}[0-9]{1}):([0-5]{1}[0-9]{1})(\.[0-9]{1,2})?$';
29     CMITimespan = '^([0-9]{2,4}):([0-9]{2}):([0-9]{2})(\.[0-9]{1,2})?$';
30     CMIInteger = '^\\d+$';
31     CMISInteger = '^-?([0-9]+)$';
32     CMIDecimal = '^-?([0-9]{0,3})(\.[0-9]{1,2})?$';
33     CMIIdentifier = '^\\w{1,255}$';
34     CMIFeedback = CMIString256; // This must be redefined
35     CMIIndex = '[._](\\d+).';
37     // Vocabulary Data Type Definition
38     CMIStatus = '^passed$|^completed$|^failed$|^incomplete$|^browsed$';
39     CMIStatus2 = '^passed$|^completed$|^failed$|^incomplete$|^browsed$|^not attempted$';
40     CMIExit = '^time-out$|^suspend$|^logout$|^$';
41     CMIType = '^true-false$|^choice$|^fill-in$|^matching$|^performance$|^sequencing$|^likert$|^numeric$';
42     CMIResult = '^correct$|^wrong$|^unanticipated$|^neutral$|^([0-9]{0,3})?(\.[0-9]{1,2})?$';
43     NAVEvent = '^previous$|^continue$';
45     // Children lists
46     cmi_children = 'core, suspend_data, launch_data, comments, objectives, student_data, student_preference, interactions';
47     core_children = 'student_id, student_name, lesson_location, credit, lesson_status, entry, score, total_time, lesson_mode, exit, session_time';
48     score_children = 'raw, min, max';
49     comments_children = 'content, location, time';
50     objectives_children = 'id, score, status';
51     student_data_children = 'attempt_number, tries, mastery_score, max_time_allowed, time_limit_action';
52     student_preference_children = 'audio, language, speed, text';
53     interactions_children = 'id, objectives, time, type, correct_responses, weighting, student_response, result, latency';
55     // Data ranges
56     score_range = '0#100';
57     audio_range = '-1#100';
58     speed_range = '-100#100';
59     weighting_range = '-100#100';
60     text_range = '-1#1';
62     // The AICC data model
63     var datamodel = {};
64     for(scoid in def){
65         datamodel[scoid] = {
66             'cmi._children':{'defaultvalue':cmi_children, 'mod':'r', 'writeerror':'402'},
67             'cmi._version':{'defaultvalue':'3.4', 'mod':'r', 'writeerror':'402'},
68             'cmi.core._children':{'defaultvalue':core_children, 'mod':'r', 'writeerror':'402'},
69             'cmi.core.student_id':{'defaultvalue':def[scoid]['cmi.core.student_id'], 'mod':'r', 'writeerror':'403'},
70             'cmi.core.student_name':{'defaultvalue':def[scoid]['cmi.core.student_name'], 'mod':'r', 'writeerror':'403'},
71             'cmi.core.lesson_location':{'defaultvalue':def[scoid]['cmi.core.lesson_location'], 'format':CMIString256, 'mod':'rw', 'writeerror':'405'},
72             'cmi.core.credit':{'defaultvalue':def[scoid]['cmi.core.credit'], 'mod':'r', 'writeerror':'403'},
73             'cmi.core.lesson_status':{'defaultvalue':def[scoid]['cmi.core.lesson_status'], 'format':CMIStatus, 'mod':'rw', 'writeerror':'405'},
74             'cmi.core.exit':{'defaultvalue':def[scoid]['cmi.core.exit'], 'format':CMIExit, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
75             'cmi.core.entry':{'defaultvalue':def[scoid]['cmi.core.entry'], 'mod':'r', 'writeerror':'403'},
76             'cmi.core.score._children':{'defaultvalue':score_children, 'mod':'r', 'writeerror':'402'},
77             'cmi.core.score.raw':{'defaultvalue':def[scoid]['cmi.core.score.raw'], 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
78             'cmi.core.score.max':{'defaultvalue':def[scoid]['cmi.core.score.max'], 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
79             'cmi.core.score.min':{'defaultvalue':def[scoid]['cmi.core.score.min'], 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
80             'cmi.core.session_time':{'format':CMITimespan, 'mod':'w', 'defaultvalue':'00:00:00', 'readerror':'404', 'writeerror':'405'},
81             'cmi.core.total_time':{'defaultvalue':def[scoid]['cmi.core.total_time'], 'mod':'r', 'writeerror':'403'},
82             'cmi.core.lesson_mode':{'defaultvalue':def[scoid]['cmi.core.lesson_mode'], 'mod':'r', 'writeerror':'403'},
83             'cmi.suspend_data':{'defaultvalue':def[scoid]['cmi.suspend_data'], 'format':CMIString4096, 'mod':'rw', 'writeerror':'405'},
84             'cmi.launch_data':{'defaultvalue':def[scoid]['cmi.launch_data'], 'mod':'r', 'writeerror':'403'},
85             'cmi.comments':{'defaultvalue':def[scoid]['cmi.comments'], 'format':CMIString4096, 'mod':'rw', 'writeerror':'405'},
86             // deprecated evaluation attributes
87             'cmi.evaluation.comments._count':{'defaultvalue':'0', 'mod':'r', 'writeerror':'402'},
88             'cmi.evaluation.comments._children':{'defaultvalue':comments_children, 'mod':'r', 'writeerror':'402'},
89             'cmi.evaluation.comments.n.content':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIString256, 'mod':'rw', 'writeerror':'405'},
90             'cmi.evaluation.comments.n.location':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIString256, 'mod':'rw', 'writeerror':'405'},
91             'cmi.evaluation.comments.n.time':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMITime, 'mod':'rw', 'writeerror':'405'},
92             'cmi.comments_from_lms':{'mod':'r', 'writeerror':'403'},
93             'cmi.objectives._children':{'defaultvalue':objectives_children, 'mod':'r', 'writeerror':'402'},
94             'cmi.objectives._count':{'mod':'r', 'defaultvalue':'0', 'writeerror':'402'},
95             'cmi.objectives.n.id':{'pattern':CMIIndex, 'format':CMIIdentifier, 'mod':'rw', 'writeerror':'405'},
96             'cmi.objectives.n.score._children':{'pattern':CMIIndex, 'mod':'r', 'writeerror':'402'},
97             'cmi.objectives.n.score.raw':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
98             'cmi.objectives.n.score.min':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
99             'cmi.objectives.n.score.max':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
100             'cmi.objectives.n.status':{'pattern':CMIIndex, 'format':CMIStatus2, 'mod':'rw', 'writeerror':'405'},
101             'cmi.student_data._children':{'defaultvalue':student_data_children, 'mod':'r', 'writeerror':'402'},
102             'cmi.student_data.attempt_number':{'defaultvalue':def[scoid]['cmi.student_data.attempt_number'], 'mod':'r', 'writeerror':'402'},
103             'cmi.student_data.tries.n.score.raw':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
104             'cmi.student_data.tries.n.score.min':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
105             'cmi.student_data.tries.n.score.max':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
106             'cmi.student_data.tries.n.status':{'pattern':CMIIndex, 'format':CMIStatus2, 'mod':'rw', 'writeerror':'405'},
107             'cmi.student_data.tries.n.time':{'pattern':CMIIndex, 'format':CMITime, 'mod':'rw', 'writeerror':'405'},
108             'cmi.student_data.mastery_score':{'defaultvalue':def[scoid]['cmi.student_data.mastery_score'], 'mod':'r', 'writeerror':'403'},
109             'cmi.student_data.max_time_allowed':{'defaultvalue':def[scoid]['cmi.student_data.max_time_allowed'], 'mod':'r', 'writeerror':'403'},
110             'cmi.student_data.time_limit_action':{'defaultvalue':def[scoid]['cmi.student_data.time_limit_action'], 'mod':'r', 'writeerror':'403'},
111             'cmi.student_data.tries_during_lesson':{'defaultvalue':def[scoid]['cmi.student_data.tries_during_lesson'], 'mod':'r', 'writeerror':'402'},
112             'cmi.student_preference._children':{'defaultvalue':student_preference_children, 'mod':'r', 'writeerror':'402'},
113             'cmi.student_preference.audio':{'defaultvalue':'0', 'format':CMISInteger, 'range':audio_range, 'mod':'rw', 'writeerror':'405'},
114             'cmi.student_preference.language':{'defaultvalue':'', 'format':CMIString256, 'mod':'rw', 'writeerror':'405'},
115             'cmi.student_preference.speed':{'defaultvalue':'0', 'format':CMISInteger, 'range':speed_range, 'mod':'rw', 'writeerror':'405'},
116             'cmi.student_preference.text':{'defaultvalue':'0', 'format':CMISInteger, 'range':text_range, 'mod':'rw', 'writeerror':'405'},
117             'cmi.interactions._children':{'defaultvalue':interactions_children, 'mod':'r', 'writeerror':'402'},
118             'cmi.interactions._count':{'mod':'r', 'defaultvalue':'0', 'writeerror':'402'},
119             'cmi.interactions.n.id':{'pattern':CMIIndex, 'format':CMIIdentifier, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
120             'cmi.interactions.n.objectives._count':{'pattern':CMIIndex, 'mod':'r', 'defaultvalue':'0', 'writeerror':'402'},
121             'cmi.interactions.n.objectives.n.id':{'pattern':CMIIndex, 'format':CMIIdentifier, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
122             'cmi.interactions.n.time':{'pattern':CMIIndex, 'format':CMITime, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
123             'cmi.interactions.n.type':{'pattern':CMIIndex, 'format':CMIType, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
124             'cmi.interactions.n.correct_responses._count':{'pattern':CMIIndex, 'mod':'r', 'defaultvalue':'0', 'writeerror':'402'},
125             'cmi.interactions.n.correct_responses.n.pattern':{'pattern':CMIIndex, 'format':CMIFeedback, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
126             'cmi.interactions.n.weighting':{'pattern':CMIIndex, 'format':CMIDecimal, 'range':weighting_range, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
127             'cmi.interactions.n.student_response':{'pattern':CMIIndex, 'format':CMIFeedback, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
128             'cmi.interactions.n.result':{'pattern':CMIIndex, 'format':CMIResult, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
129             'cmi.interactions.n.latency':{'pattern':CMIIndex, 'format':CMITimespan, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
130             'nav.event':{'defaultvalue':'', 'format':NAVEvent, 'mod':'w', 'readerror':'404', 'writeerror':'405'}
131         };
132     }
134     var cmi, nav;
135     function initdatamodel(scoid){
136         prerequrl = cfgwwwroot + "/mod/scorm/prereqs.php?a=" + scormid + "&scoid=" + scoid + "&attempt=" + attempt + "&mode=" + viewmode + "&currentorg=" + currentorg + "&sesskey=" + sesskey;
137         datamodelurlparams = "id=" + cmid + "&a=" + scormid + "&sesskey=" + sesskey + "&attempt=" + attempt + "&scoid=" + scoid;
139         //
140         // Datamodel inizialization
141         //
142         cmi = new Object();
143             cmi.core = new Object();
144             cmi.core.score = new Object();
145             cmi.objectives = new Object();
146             cmi.student_data = new Object();
147             cmi.student_preference = new Object();
148             cmi.interactions = new Object();
149             // deprecated evaluation attributes
150             cmi.evaluation = new Object();
151             cmi.evaluation.comments = new Object();
153         // Navigation Object
154         nav = new Object();
156         for (element in datamodel[scoid]) {
157             if (element.match(/\.n\./) == null) {
158                 if (typeof datamodel[scoid][element].defaultvalue != 'undefined') {
159                     eval(element + ' = datamodel["' + scoid + '"]["' + element + '"].defaultvalue;');
160                 } else {
161                     eval(element + ' = "";');
162                 }
163             }
164         }
166         eval(cmiobj[scoid]);
168         if (cmi.core.lesson_status == '') {
169             cmi.core.lesson_status = 'not attempted';
170         }
171     }
173     //
174     // API Methods definition
175     //
176     var Initialized = false;
178     function LMSInitialize (param) {
179         scoid = scorm_current_node ? scorm_current_node.scoid : scoid;
180         initdatamodel(scoid);
182         errorCode = "0";
183         if (param == "") {
184             if (!Initialized) {
185                 Initialized = true;
186                 errorCode = "0";
187                 return "true";
188             } else {
189                 errorCode = "101";
190             }
191         } else {
192             errorCode = "201";
193         }
194         return "false";
195     }
197     function LMSFinish (param) {
198         errorCode = "0";
199         if (param == "") {
200             if (Initialized) {
201                 Initialized = false;
202                 result = StoreData(cmi,true);
203                 if (nav.event != '') {
204                     if (nav.event == 'continue') {
205                         setTimeout('mod_scorm_launch_next_sco();',500);
206                     } else {
207                         setTimeout('mod_scorm_launch_prev_sco();',500);
208                     }
209                 } else {
210                     if (scormauto == 1) {
211                         setTimeout('mod_scorm_launch_next_sco();',500);
212                     }
213                 }
214                 // trigger TOC update
215                 var callback = M.mod_scorm.connectPrereqCallback;
216                 YUI().use('io-base', function(Y) {
217                     Y.on('io:complete', callback.success, Y);
218                     Y.io(prerequrl);
219                 });
220                 return "true";
221             } else {
222                 errorCode = "301";
223             }
224         } else {
225             errorCode = "201";
226         }
227         return "false";
228     }
230     function LMSGetValue (element) {
231         errorCode = "0";
232         if (Initialized) {
233             if (element != "") {
234                 expression = new RegExp(CMIIndex,'g');
235                 elementmodel = String(element).replace(expression,'.n.');
236                 if (typeof datamodel[scoid][elementmodel] != "undefined") {
237                     if (datamodel[scoid][elementmodel].mod != 'w') {
238                             element = String(element).replace(expression, "_$1.");
239                             elementIndexes = element.split('.');
240                         subelement = 'cmi';
241                         i = 1;
242                         while ((i < elementIndexes.length) && (typeof eval(subelement) != "undefined")) {
243                             subelement += '.' + elementIndexes[i++];
244                         }
245                         if (subelement == element) {
246                             errorCode = "0";
247                             return eval(element);
248                         } else {
249                             errorCode = "0"; // Need to check if it is the right errorCode
250                         }
251                     } else {
252                         errorCode = datamodel[scoid][elementmodel].readerror;
253                     }
254                 } else {
255                     childrenstr = '._children';
256                     countstr = '._count';
257                     if (elementmodel.substr(elementmodel.length - childrenstr.length,elementmodel.length) == childrenstr) {
258                         parentmodel = elementmodel.substr(0,elementmodel.length - childrenstr.length);
259                         if (typeof datamodel[scoid][parentmodel] != "undefined") {
260                             errorCode = "202";
261                         } else {
262                             errorCode = "201";
263                         }
264                     } else if (elementmodel.substr(elementmodel.length - countstr.length,elementmodel.length) == countstr) {
265                         parentmodel = elementmodel.substr(0,elementmodel.length - countstr.length);
266                         if (typeof datamodel[scoid][parentmodel] != "undefined") {
267                             errorCode = "203";
268                         } else {
269                             errorCode = "201";
270                         }
271                     } else {
272                         errorCode = "201";
273                     }
274                 }
275             } else {
276                 errorCode = "201";
277             }
278         } else {
279             errorCode = "301";
280         }
281         return "";
282     }
284     function LMSSetValue (element,value) {
285         errorCode = "0";
286         if (Initialized) {
287             if (element != "") {
288                 expression = new RegExp(CMIIndex,'g');
289                 elementmodel = String(element).replace(expression,'.n.');
290                 if (typeof datamodel[scoid][elementmodel] != "undefined") {
291                     if (datamodel[scoid][elementmodel].mod != 'r') {
292                         expression = new RegExp(datamodel[scoid][elementmodel].format);
293                         value = value + '';
294                         matches = value.match(expression);
295                         if (matches != null) {
296                             //Create dynamic data model element
297                             if (element != elementmodel) {
298                                 elementIndexes = element.split('.');
299                                 subelement = 'cmi';
300                                 for (i = 1; i < elementIndexes.length - 1; i++) {
301                                     elementIndex = elementIndexes[i];
302                                     if (elementIndexes[i + 1].match(/^\d+$/)) {
303                                         if ((typeof eval(subelement + '.' + elementIndex)) == "undefined") {
304                                             eval(subelement + '.' + elementIndex + ' = new Object();');
305                                             eval(subelement + '.' + elementIndex + '._count = 0;');
306                                         }
307                                         if (elementIndexes[i + 1] == eval(subelement + '.' + elementIndex + '._count')) {
308                                             eval(subelement + '.' + elementIndex + '._count++;');
309                                         }
310                                         if (elementIndexes[i + 1] > eval(subelement + '.' + elementIndex + '._count')) {
311                                             errorCode = "201";
312                                         }
313                                         subelement = subelement.concat('.' + elementIndex + '_' + elementIndexes[i + 1]);
314                                         i++;
315                                     } else {
316                                         subelement = subelement.concat('.' + elementIndex);
317                                     }
318                                     if ((typeof eval(subelement)) == "undefined") {
319                                         eval(subelement + ' = new Object();');
320                                         if (subelement.substr(0,14) == 'cmi.objectives') {
321                                             eval(subelement + '.score = new Object();');
322                                             eval(subelement + '.score._children = score_children;');
323                                             eval(subelement + '.score.raw = "";');
324                                             eval(subelement + '.score.min = "";');
325                                             eval(subelement + '.score.max = "";');
326                                         }
327                                         if (subelement.substr(0,16) == 'cmi.interactions') {
328                                             eval(subelement + '.objectives = new Object();');
329                                             eval(subelement + '.objectives._count = 0;');
330                                             eval(subelement + '.correct_responses = new Object();');
331                                             eval(subelement + '.correct_responses._count = 0;');
332                                         }
333                                     }
334                                 }
335                                 element = subelement.concat('.' + elementIndexes[elementIndexes.length - 1]);
336                             }
337                             // Store data.
338                             if (errorCode == "0") {
339                                 if (autocommit && !(AICCapi.timeout)) {
340                                     AICCapi.timeout = Y.later(60000, API, 'LMSCommit', [""], false);
341                                 }
342                                 if (typeof datamodel[scoid][elementmodel].range != "undefined") {
343                                     range = datamodel[scoid][elementmodel].range;
344                                     ranges = range.split('#');
345                                     value = value * 1.0;
346                                     if ((value >= ranges[0]) && (value <= ranges[1])) {
347                                         eval(element + '="' + value + '";');
348                                         errorCode = "0";
349                                         return "true";
350                                     } else {
351                                         errorCode = datamodel[scoid][elementmodel].writeerror;
352                                     }
353                                 } else {
354                                     if (element == 'cmi.comments') {
355                                         eval(element + '+="' + value + '";');
356                                     } else {
357                                         eval(element + '="' + value + '";');
358                                     }
359                                     errorCode = "0";
360                                     return "true";
361                                 }
362                             }
363                         } else {
364                             errorCode = datamodel[scoid][elementmodel].writeerror;
365                         }
366                     } else {
367                         errorCode = datamodel[scoid][elementmodel].writeerror;
368                     }
369                 } else {
370                     errorCode = "201"
371                 }
372             } else {
373                 errorCode = "201";
374             }
375         } else {
376             errorCode = "301";
377         }
378         return "false";
379     }
381     function LMSCommit (param) {
382         if (AICCapi.timeout) {
383             AICCapi.timeout.cancel();
384             AICCapi.timeout = null;
385         }
386         errorCode = "0";
387         if (param == "") {
388             if (Initialized) {
389                 result = StoreData(cmi,false);
390                 return "true";
391             } else {
392                 errorCode = "301";
393             }
394         } else {
395             errorCode = "201";
396         }
397         return "false";
398     }
400     function LMSGetLastError () {
401         return errorCode;
402     }
404     function LMSGetErrorString (param) {
405         if (param != "") {
406             var errorString = new Array();
407             errorString["0"] = "No error";
408             errorString["101"] = "General exception";
409             errorString["201"] = "Invalid argument error";
410             errorString["202"] = "Element cannot have children";
411             errorString["203"] = "Element not an array - cannot have count";
412             errorString["301"] = "Not initialized";
413             errorString["401"] = "Not implemented error";
414             errorString["402"] = "Invalid set value, element is a keyword";
415             errorString["403"] = "Element is read only";
416             errorString["404"] = "Element is write only";
417             errorString["405"] = "Incorrect data type";
418             return errorString[param];
419         } else {
420            return "";
421         }
422     }
424     function LMSGetDiagnostic (param) {
425         if (param == "") {
426             param = errorCode;
427         }
428         return param;
429     }
431     function AddTime (first, second) {
432         var sFirst = first.split(":");
433         var sSecond = second.split(":");
434         var cFirst = sFirst[2].split(".");
435         var cSecond = sSecond[2].split(".");
436         var change = 0;
438         FirstCents = 0;  //Cents
439         if (cFirst.length > 1) {
440             FirstCents = parseInt(cFirst[1],10);
441         }
442         SecondCents = 0;
443         if (cSecond.length > 1) {
444             SecondCents = parseInt(cSecond[1],10);
445         }
446         var cents = FirstCents + SecondCents;
447         change = Math.floor(cents / 100);
448         cents = cents - (change * 100);
449         if (Math.floor(cents) < 10) {
450             cents = "0" + cents.toString();
451         }
453         var secs = parseInt(cFirst[0],10) + parseInt(cSecond[0],10) + change;  //Seconds
454         change = Math.floor(secs / 60);
455         secs = secs - (change * 60);
456         if (Math.floor(secs) < 10) {
457             secs = "0" + secs.toString();
458         }
460         mins = parseInt(sFirst[1],10) + parseInt(sSecond[1],10) + change;   //Minutes
461         change = Math.floor(mins / 60);
462         mins = mins - (change * 60);
463         if (mins < 10) {
464             mins = "0" + mins.toString();
465         }
467         hours = parseInt(sFirst[0],10) + parseInt(sSecond[0],10) + change;  //Hours
468         if (hours < 10) {
469             hours = "0" + hours.toString();
470         }
472         if (cents != '0') {
473             return hours + ":" + mins + ":" + secs + '.' + cents;
474         } else {
475             return hours + ":" + mins + ":" + secs;
476         }
477     }
479     function TotalTime() {
480         total_time = AddTime(cmi.core.total_time, cmi.core.session_time);
481         return '&' + underscore('cmi.core.total_time') + '=' + escape(total_time);
482     }
484     function CollectData(data,parent) {
485         var datastring = '';
486         for (property in data) {
487             if (typeof data[property] == 'object') {
488                 datastring += CollectData(data[property],parent + '.' + property);
489             } else {
490                 element = parent + '.' + property;
491                 expression = new RegExp(CMIIndex,'g');
492                 elementmodel = String(element).replace(expression,'.n.');
493                 if (typeof datamodel[scoid][elementmodel] != "undefined") {
494                     if (datamodel[scoid][elementmodel].mod != 'r') {
495                         elementstring = '&' + underscore(element) + '=' + escape(data[property]);
496                         if (typeof datamodel[scoid][elementmodel].defaultvalue != "undefined") {
497                             if (datamodel[scoid][elementmodel].defaultvalue != data[property]) {
498                                 datastring += elementstring;
499                             }
500                         } else {
501                             datastring += elementstring;
502                         }
503                     }
504                 }
505             }
506         }
507         return datastring;
508     }
510     function StoreData(data,storetotaltime) {
511         if (storetotaltime) {
512             if (cmi.core.lesson_mode == 'normal') {
513                 if (cmi.core.credit == 'credit') {
514                     if (cmi.student_data.mastery_score != '' && cmi.core.score.raw != '') {
515                         if (cmi.core.score.raw >= cmi.student_data.mastery_score) {
516                             cmi.core.lesson_status = 'passed';
517                         } else {
518                             cmi.core.lesson_status = 'failed';
519                         }
520                     }
521                 }
522             }
523             if (cmi.core.lesson_mode == 'browse') {
524                 if (datamodel[scoid]['cmi.core.lesson_status'].defaultvalue == '' && cmi.core.lesson_status == 'not attempted') {
525                     cmi.core.lesson_status = 'browsed';
526                 }
527             }
528             datastring = CollectData(data,'cmi');
529             datastring += TotalTime();
530         } else {
531             datastring = CollectData(data,'cmi');
532         }
534         //popupwin(datastring);
535         var myRequest = NewHttpReq();
536         result = DoRequest(myRequest,datamodelurl,datamodelurlparams + datastring);
537         results = String(result).split('\n');
538         errorCode = results[1];
539         return results[0];
540     }
542     this.LMSInitialize = LMSInitialize;
543     this.LMSFinish = LMSFinish;
544     this.LMSGetValue = LMSGetValue;
545     this.LMSSetValue = LMSSetValue;
546     this.LMSCommit = LMSCommit;
547     this.LMSGetLastError = LMSGetLastError;
548     this.LMSGetErrorString = LMSGetErrorString;
549     this.LMSGetDiagnostic = LMSGetDiagnostic;
552 M.scorm_api = {};
554 M.scorm_api.init = function(Y, def, cmiobj, scormauto, cfgwwwroot, scormid, scoid, attempt, viewmode, currentorg, sesskey, cmid, autocommit) {
555     window.API = new AICCapi(def, cmiobj, scormauto, cfgwwwroot, scormid, scoid, attempt, viewmode, currentorg, sesskey, cmid, autocommit);