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