MDL-45339 Libraries: Add a note to docblock of get_config, set_config, and unset_conf...
[moodle.git] / mod / scorm / datamodels / scorm_12.js.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
19 if (isset($userdata->status)) {
20     if ($userdata->status == '') {
21         $userdata->entry = 'ab-initio';
22     } else {
23         if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
24             $userdata->entry = 'resume';
25         } else {
26             $userdata->entry = '';
27         }
28     }
29 }
30 if (!isset($currentorg)) {
31     $currentorg = '';
32 }
34 // If SCORM 1.2 standard mode is disabled allow higher datamodel limits.
35 if (intval(get_config("scorm", "scorm12standard"))) {
36     $cmistring256 = '^[\\u0000-\\uFFFF]{0,255}$';
37     $cmistring4096 = '^[\\u0000-\\uFFFF]{0,4096}$';
38 } else {
39     $cmistring256 = '^[\\u0000-\\uFFFF]{0,64000}$';
40     $cmistring4096 = $cmistring256;
41 }
42 ?>
43 //
44 // SCORM 1.2 API Implementation
45 //
46 function SCORMapi1_2() {
47     // Standard Data Type Definition
48     CMIString256 = '<?php echo $cmistring256 ?>';
49     CMIString4096 = '<?php echo $cmistring4096 ?>';
50     CMITime = '^([0-2]{1}[0-9]{1}):([0-5]{1}[0-9]{1}):([0-5]{1}[0-9]{1})(\.[0-9]{1,2})?$';
51     CMITimespan = '^([0-9]{2,4}):([0-9]{2}):([0-9]{2})(\.[0-9]{1,2})?$';
52     CMIInteger = '^\\d+$';
53     CMISInteger = '^-?([0-9]+)$';
54     CMIDecimal = '^-?([0-9]{0,3})(\.[0-9]*)?$';
55     CMIIdentifier = '^[\\u0021-\\u007E]{0,255}$';
56     CMIFeedback = CMIString256; // This must be redefined
57     CMIIndex = '[._](\\d+).';
58     // Vocabulary Data Type Definition
59     CMIStatus = '^passed$|^completed$|^failed$|^incomplete$|^browsed$';
60     CMIStatus2 = '^passed$|^completed$|^failed$|^incomplete$|^browsed$|^not attempted$';
61     CMIExit = '^time-out$|^suspend$|^logout$|^$';
62     CMIType = '^true-false$|^choice$|^fill-in$|^matching$|^performance$|^sequencing$|^likert$|^numeric$';
63     CMIResult = '^correct$|^wrong$|^unanticipated$|^neutral$|^([0-9]{0,3})?(\.[0-9]*)?$';
64     NAVEvent = '^previous$|^continue$';
65     // Children lists
66     cmi_children = 'core,suspend_data,launch_data,comments,objectives,student_data,student_preference,interactions';
67     core_children = 'student_id,student_name,lesson_location,credit,lesson_status,entry,score,total_time,lesson_mode,exit,session_time';
68     score_children = 'raw,min,max';
69     comments_children = 'content,location,time';
70     objectives_children = 'id,score,status';
71     correct_responses_children = 'pattern';
72     student_data_children = 'mastery_score,max_time_allowed,time_limit_action';
73     student_preference_children = 'audio,language,speed,text';
74     interactions_children = 'id,objectives,time,type,correct_responses,weighting,student_response,result,latency';
75     // Data ranges
76     score_range = '0#100';
77     audio_range = '-1#100';
78     speed_range = '-100#100';
79     weighting_range = '-100#100';
80     text_range = '-1#1';
81     // The SCORM 1.2 data model
82     var datamodel =  {
83         'cmi._children':{'defaultvalue':cmi_children, 'mod':'r', 'writeerror':'402'},
84         'cmi._version':{'defaultvalue':'3.4', 'mod':'r', 'writeerror':'402'},
85         'cmi.core._children':{'defaultvalue':core_children, 'mod':'r', 'writeerror':'402'},
86         'cmi.core.student_id':{'defaultvalue':'<?php echo $userdata->student_id ?>', 'mod':'r', 'writeerror':'403'},
87         'cmi.core.student_name':{'defaultvalue':'<?php echo $userdata->student_name ?>', 'mod':'r', 'writeerror':'403'},
88         'cmi.core.lesson_location':{'defaultvalue':'<?php echo isset($userdata->{'cmi.core.lesson_location'})?$userdata->{'cmi.core.lesson_location'}:'' ?>', 'format':CMIString256, 'mod':'rw', 'writeerror':'405'},
89         'cmi.core.credit':{'defaultvalue':'<?php echo $userdata->credit ?>', 'mod':'r', 'writeerror':'403'},
90         'cmi.core.lesson_status':{'defaultvalue':'<?php echo isset($userdata->{'cmi.core.lesson_status'})?$userdata->{'cmi.core.lesson_status'}:'' ?>', 'format':CMIStatus, 'mod':'rw', 'writeerror':'405'},
91         'cmi.core.entry':{'defaultvalue':'<?php echo $userdata->entry ?>', 'mod':'r', 'writeerror':'403'},
92         'cmi.core.score._children':{'defaultvalue':score_children, 'mod':'r', 'writeerror':'402'},
93         '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'},
94         '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'},
95         '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'},
96         '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'},
97         'cmi.core.lesson_mode':{'defaultvalue':'<?php echo $userdata->mode ?>', 'mod':'r', 'writeerror':'403'},
98         'cmi.core.exit':{'defaultvalue':'<?php echo isset($userdata->{'cmi.core.exit'})?$userdata->{'cmi.core.exit'}:'' ?>', 'format':CMIExit, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
99         'cmi.core.session_time':{'format':CMITimespan, 'mod':'w', 'defaultvalue':'00:00:00', 'readerror':'404', 'writeerror':'405'},
100         'cmi.suspend_data':{'defaultvalue':'<?php echo isset($userdata->{'cmi.suspend_data'})?$userdata->{'cmi.suspend_data'}:'' ?>', 'format':CMIString4096, 'mod':'rw', 'writeerror':'405'},
101         'cmi.launch_data':{'defaultvalue':'<?php echo isset($userdata->datafromlms)?$userdata->datafromlms:'' ?>', 'mod':'r', 'writeerror':'403'},
102         'cmi.comments':{'defaultvalue':'<?php echo isset($userdata->{'cmi.comments'})?$userdata->{'cmi.comments'}:'' ?>', 'format':CMIString4096, 'mod':'rw', 'writeerror':'405'},
103         // deprecated evaluation attributes
104         'cmi.evaluation.comments._count':{'defaultvalue':'0', 'mod':'r', 'writeerror':'402'},
105         'cmi.evaluation.comments._children':{'defaultvalue':comments_children, 'mod':'r', 'writeerror':'402'},
106         'cmi.evaluation.comments.n.content':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIString256, 'mod':'rw', 'writeerror':'405'},
107         'cmi.evaluation.comments.n.location':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIString256, 'mod':'rw', 'writeerror':'405'},
108         'cmi.evaluation.comments.n.time':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMITime, 'mod':'rw', 'writeerror':'405'},
109         'cmi.comments_from_lms':{'mod':'r', 'writeerror':'403'},
110         'cmi.objectives._children':{'defaultvalue':objectives_children, 'mod':'r', 'writeerror':'402'},
111         'cmi.objectives._count':{'mod':'r', 'defaultvalue':'0', 'writeerror':'402'},
112         'cmi.objectives.n.id':{'pattern':CMIIndex, 'format':CMIIdentifier, 'mod':'rw', 'writeerror':'405'},
113         'cmi.objectives.n.score._children':{'pattern':CMIIndex, 'mod':'r', 'writeerror':'402'},
114         'cmi.objectives.n.score.raw':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
115         'cmi.objectives.n.score.min':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
116         'cmi.objectives.n.score.max':{'defaultvalue':'', 'pattern':CMIIndex, 'format':CMIDecimal, 'range':score_range, 'mod':'rw', 'writeerror':'405'},
117         'cmi.objectives.n.status':{'pattern':CMIIndex, 'format':CMIStatus2, 'mod':'rw', 'writeerror':'405'},
118         'cmi.student_data._children':{'defaultvalue':student_data_children, 'mod':'r', 'writeerror':'402'},
119         'cmi.student_data.mastery_score':{'defaultvalue':'<?php echo isset($userdata->masteryscore)?$userdata->masteryscore:'' ?>', 'mod':'r', 'writeerror':'403'},
120         'cmi.student_data.max_time_allowed':{'defaultvalue':'<?php echo isset($userdata->maxtimeallowed)?$userdata->maxtimeallowed:'' ?>', 'mod':'r', 'writeerror':'403'},
121         'cmi.student_data.time_limit_action':{'defaultvalue':'<?php echo isset($userdata->timelimitaction)?$userdata->timelimitaction:'' ?>', 'mod':'r', 'writeerror':'403'},
122         'cmi.student_preference._children':{'defaultvalue':student_preference_children, 'mod':'r', 'writeerror':'402'},
123         'cmi.student_preference.audio':{'defaultvalue':'0', 'format':CMISInteger, 'range':audio_range, 'mod':'rw', 'writeerror':'405'},
124         'cmi.student_preference.language':{'defaultvalue':'', 'format':CMIString256, 'mod':'rw', 'writeerror':'405'},
125         'cmi.student_preference.speed':{'defaultvalue':'0', 'format':CMISInteger, 'range':speed_range, 'mod':'rw', 'writeerror':'405'},
126         'cmi.student_preference.text':{'defaultvalue':'0', 'format':CMISInteger, 'range':text_range, 'mod':'rw', 'writeerror':'405'},
127         'cmi.interactions._children':{'defaultvalue':interactions_children, 'mod':'r', 'writeerror':'402'},
128         'cmi.interactions._count':{'mod':'r', 'defaultvalue':'0', 'writeerror':'402'},
129         'cmi.interactions.n.id':{'pattern':CMIIndex, 'format':CMIIdentifier, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
130         'cmi.interactions.n.objectives._count':{'pattern':CMIIndex, 'mod':'r', 'defaultvalue':'0', 'writeerror':'402'},
131         'cmi.interactions.n.objectives.n.id':{'pattern':CMIIndex, 'format':CMIIdentifier, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
132         'cmi.interactions.n.time':{'pattern':CMIIndex, 'format':CMITime, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
133         'cmi.interactions.n.type':{'pattern':CMIIndex, 'format':CMIType, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
134         'cmi.interactions.n.correct_responses._count':{'pattern':CMIIndex, 'mod':'r', 'defaultvalue':'0', 'writeerror':'402'},
135         'cmi.interactions.n.correct_responses.n.pattern':{'pattern':CMIIndex, 'format':CMIFeedback, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
136         'cmi.interactions.n.weighting':{'pattern':CMIIndex, 'format':CMIDecimal, 'range':weighting_range, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
137         'cmi.interactions.n.student_response':{'pattern':CMIIndex, 'format':CMIFeedback, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
138         'cmi.interactions.n.result':{'pattern':CMIIndex, 'format':CMIResult, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
139         'cmi.interactions.n.latency':{'pattern':CMIIndex, 'format':CMITimespan, 'mod':'w', 'readerror':'404', 'writeerror':'405'},
140         'nav.event':{'defaultvalue':'', 'format':NAVEvent, 'mod':'w', 'readerror':'404', 'writeerror':'405'}
141     };
142     //
143     // Datamodel inizialization
144     //
145     var cmi = new Object();
146         cmi.core = new Object();
147         cmi.core.score = new Object();
148         cmi.objectives = new Object();
149         cmi.student_data = new Object();
150         cmi.student_preference = new Object();
151         cmi.interactions = new Object();
152         // deprecated evaluation attributes
153         cmi.evaluation = new Object();
154         cmi.evaluation.comments = new Object();
156     // Navigation Object
157     var nav = new Object();
159     for (element in datamodel) {
160         if (element.match(/\.n\./) == null) {
161             if ((typeof eval('datamodel["'+element+'"].defaultvalue')) != 'undefined') {
162                 eval(element+' = datamodel["'+element+'"].defaultvalue;');
163             } else {
164                 eval(element+' = "";');
165             }
166         }
167     }
169 <?php
170      // reconstitute objectives
171     scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.objectives', array('score'));
172     scorm_reconstitute_array_element($scorm->version, $userdata, 'cmi.interactions', array('objectives', 'correct_responses'));
173     ?>
175     if (cmi.core.lesson_status == '') {
176         cmi.core.lesson_status = 'not attempted';
177     }
179     //
180     // API Methods definition
181     //
182     var Initialized = false;
184     function LMSInitialize (param) {
185         errorCode = "0";
186         if (param == "") {
187             if (!Initialized) {
188                 Initialized = true;
189                 errorCode = "0";
190                 <?php
191                     if (scorm_debugging($scorm)) {
192                         echo 'LogAPICall("LMSInitialize", param, "", errorCode);';
193                     }
194                 ?>
195                 return "true";
196             } else {
197                 errorCode = "101";
198             }
199         } else {
200             errorCode = "201";
201         }
202         <?php
203             if (scorm_debugging($scorm)) {
204                 echo 'LogAPICall("LMSInitialize", param, "", errorCode);';
205             }
206         ?>
207         return "false";
208     }
210     function LMSFinish (param) {
211         errorCode = "0";
212         if (param == "") {
213             if (Initialized) {
214                 Initialized = false;
215                 result = StoreData(cmi,true);
216                 if (nav.event != '') {
217                     if (nav.event == 'continue') {
218                         setTimeout('mod_scorm_launch_next_sco();',500);
219                     } else {
220                         setTimeout('mod_scorm_launch_prev_sco();',500);
221                     }
222                 } else {
223                     if (<?php echo $scorm->auto ?> == 1) {
224                         setTimeout('mod_scorm_launch_next_sco();',500);
225                     }
226                 }
227                 <?php
228                     if (scorm_debugging($scorm)) {
229                         echo 'LogAPICall("LMSFinish", "AJAXResult", result, 0);';
230                     }
231                 ?>
232                 result = ('true' == result) ? 'true' : 'false';
233                 errorCode = (result == 'true')? '0' : '101';
234                 <?php
235                     if (scorm_debugging($scorm)) {
236                         echo 'LogAPICall("LMSFinish", "result", result, 0);';
237                         echo 'LogAPICall("LMSFinish", param, "", 0);';
238                     }
239                 ?>
240                 // trigger TOC update
241                 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(); ?>";
242                 var callback = M.mod_scorm.connectPrereqCallback;
243                 YUI().use('io-base', function(Y) {
244                     Y.on('io:complete', callback.success, Y);
245                     Y.io(sURL);
246                 });
247                 return result;
248             } else {
249                 errorCode = "301";
250             }
251         } else {
252             errorCode = "201";
253         }
254         <?php
255             if (scorm_debugging($scorm)) {
256                 echo 'LogAPICall("LMSFinish", param, "", errorCode);';
257             }
258         ?>
259         return "false";
260     }
262     function LMSGetValue (element) {
263         errorCode = "0";
264         if (Initialized) {
265             if (element !="") {
266                 expression = new RegExp(CMIIndex,'g');
267                 elementmodel = String(element).replace(expression,'.n.');
268                 if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {
269                     if (eval('datamodel["'+elementmodel+'"].mod') != 'w') {
270                         element = String(element).replace(expression, "_$1.");
271                         elementIndexes = element.split('.');
272                         subelement = 'cmi';
273                         i = 1;
274                         while ((i < elementIndexes.length) && (typeof eval(subelement) != "undefined")) {
275                             subelement += '.'+elementIndexes[i++];
276                         }
277                             if (subelement == element) {
278                             errorCode = "0";
279                             <?php
280                                 if (scorm_debugging($scorm)) {
281                                     echo 'LogAPICall("LMSGetValue", element, eval(element), 0);';
282                                 }
283                             ?>
284                             return eval(element);
285                         } else {
286                             errorCode = "0"; // Need to check if it is the right errorCode
287                         }
288                     } else {
289                         errorCode = eval('datamodel["'+elementmodel+'"].readerror');
290                     }
291                 } else {
292                     childrenstr = '._children';
293                     countstr = '._count';
294                     if (elementmodel.substr(elementmodel.length-childrenstr.length,elementmodel.length) == childrenstr) {
295                         parentmodel = elementmodel.substr(0,elementmodel.length-childrenstr.length);
296                         if ((typeof eval('datamodel["'+parentmodel+'"]')) != "undefined") {
297                             errorCode = "202";
298                         } else {
299                             errorCode = "201";
300                         }
301                     } else if (elementmodel.substr(elementmodel.length-countstr.length,elementmodel.length) == countstr) {
302                         parentmodel = elementmodel.substr(0,elementmodel.length-countstr.length);
303                         if ((typeof eval('datamodel["'+parentmodel+'"]')) != "undefined") {
304                             errorCode = "203";
305                         } else {
306                             errorCode = "201";
307                         }
308                     } else {
309                         errorCode = "201";
310                     }
311                 }
312             } else {
313                 errorCode = "201";
314             }
315         } else {
316             errorCode = "301";
317         }
318         <?php
319             if (scorm_debugging($scorm)) {
320                 echo 'LogAPICall("LMSGetValue", element, "", errorCode);';
321             }
322         ?>
323         return "";
324     }
326     function LMSSetValue (element,value) {
327         errorCode = "0";
328         if (Initialized) {
329             if (element != "") {
330                 expression = new RegExp(CMIIndex,'g');
331                 elementmodel = String(element).replace(expression,'.n.');
332                 if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {
333                     if (eval('datamodel["'+elementmodel+'"].mod') != 'r') {
334                         expression = new RegExp(eval('datamodel["'+elementmodel+'"].format'));
335                         value = value+'';
336                         matches = value.match(expression);
337                         if (matches != null) {
338                             //Create dynamic data model element
339                             if (element != elementmodel) {
340                                 elementIndexes = element.split('.');
341                                 subelement = 'cmi';
342                                 for (i=1;i < elementIndexes.length-1;i++) {
343                                     elementIndex = elementIndexes[i];
344                                     if (elementIndexes[i+1].match(/^\d+$/)) {
345                                         if ((typeof eval(subelement+'.'+elementIndex)) == "undefined") {
346                                             eval(subelement+'.'+elementIndex+' = new Object();');
347                                             eval(subelement+'.'+elementIndex+'._count = 0;');
348                                         }
349                                         if (elementIndexes[i+1] == eval(subelement+'.'+elementIndex+'._count')) {
350                                             eval(subelement+'.'+elementIndex+'._count++;');
351                                         }
352                                         if (elementIndexes[i+1] > eval(subelement+'.'+elementIndex+'._count')) {
353                                             errorCode = "201";
354                                         }
355                                         subelement = subelement.concat('.'+elementIndex+'_'+elementIndexes[i+1]);
356                                         i++;
357                                     } else {
358                                         subelement = subelement.concat('.'+elementIndex);
359                                     }
360                                     if ((typeof eval(subelement)) == "undefined") {
361                                         eval(subelement+' = new Object();');
362                                         if (subelement.substr(0,14) == 'cmi.objectives') {
363                                             eval(subelement+'.score = new Object();');
364                                             eval(subelement+'.score._children = score_children;');
365                                             eval(subelement+'.score.raw = "";');
366                                             eval(subelement+'.score.min = "";');
367                                             eval(subelement+'.score.max = "";');
368                                         }
369                                         if (subelement.substr(0,16) == 'cmi.interactions') {
370                                             eval(subelement+'.objectives = new Object();');
371                                             eval(subelement+'.objectives._count = 0;');
372                                             eval(subelement+'.correct_responses = new Object();');
373                                             eval(subelement+'.correct_responses._count = 0;');
374                                         }
375                                     }
376                                 }
377                                 element = subelement.concat('.'+elementIndexes[elementIndexes.length-1]);
378                             }
379                             //Store data
380                             if (errorCode == "0") {
381                                 if ((typeof eval('datamodel["'+elementmodel+'"].range')) != "undefined") {
382                                     range = eval('datamodel["'+elementmodel+'"].range');
383                                     ranges = range.split('#');
384                                     value = value*1.0;
385                                     if ((value >= ranges[0]) && (value <= ranges[1])) {
386                                         eval(element+'=value;');
387                                         errorCode = "0";
388                                         <?php
389                                             if (scorm_debugging($scorm)) {
390                                                 echo 'LogAPICall("LMSSetValue", element, value, errorCode);';
391                                             }
392                                         ?>
393                                         return "true";
394                                     } else {
395                                         errorCode = eval('datamodel["'+elementmodel+'"].writeerror');
396                                     }
397                                 } else {
398                                     if (element == 'cmi.comments') {
399                                         cmi.comments = cmi.comments + value;
400                                     } else {
401                                         eval(element+'=value;');
402                                     }
403                                     errorCode = "0";
404                                     <?php
405                                         if (scorm_debugging($scorm)) {
406                                             echo 'LogAPICall("LMSSetValue", element, value, errorCode);';
407                                         }
408                                     ?>
409                                     return "true";
410                                 }
411                             }
412                         } else {
413                             errorCode = eval('datamodel["'+elementmodel+'"].writeerror');
414                         }
415                     } else {
416                         errorCode = eval('datamodel["'+elementmodel+'"].writeerror');
417                     }
418                 } else {
419                     errorCode = "201"
420                 }
421             } else {
422                 errorCode = "201";
423             }
424         } else {
425             errorCode = "301";
426         }
427        <?php
428         if (scorm_debugging($scorm)) {
429             echo 'LogAPICall("LMSSetValue", element, value, errorCode);';
430         }
431         ?>
432         return "false";
433     }
435     function LMSCommit (param) {
436         errorCode = "0";
437         if (param == "") {
438             if (Initialized) {
439                 result = StoreData(cmi,false);
440                 // trigger TOC update
441                 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(); ?>";
442                 var callback = M.mod_scorm.connectPrereqCallback;
443                 YUI().use('io-base', function(Y) {
444                     Y.on('io:complete', callback.success, Y);
445                     Y.io(sURL);
446                 });
447                 <?php
448                     if (scorm_debugging($scorm)) {
449                         echo 'LogAPICall("Commit", param, "", 0);';
450                     }
451                 ?>
452                 <?php
453                     if (scorm_debugging($scorm)) {
454                         echo 'LogAPICall("LMSCommit", "AJAXResult", result, 0);';
455                     }
456                 ?>
457                 result = ('true' == result) ? 'true' : 'false';
458                 errorCode = (result =='true')? '0' : '101';
459                 <?php
460                     if (scorm_debugging($scorm)) {
461                         echo 'LogAPICall("LMSCommit", "result", result, 0);';
462                         echo 'LogAPICall("LMSCommit", "errorCode", errorCode, 0);';
463                     }
464                 ?>
465                 return result;
466             } else {
467                 errorCode = "301";
468             }
469         } else {
470             errorCode = "201";
471         }
472         <?php
473             if (scorm_debugging($scorm)) {
474                 echo 'LogAPICall("LMSCommit", param, "", 0);';
475             }
476         ?>
477         return "false";
478     }
480     function LMSGetLastError () {
481      <?php
482         if (scorm_debugging($scorm)) {
483             echo 'LogAPICall("LMSGetLastError", "", "", errorCode);';
484         }
485     ?>
486         return errorCode;
487     }
489     function LMSGetErrorString (param) {
490         if (param != "") {
491             var errorString = new Array();
492             errorString["0"] = "No error";
493             errorString["101"] = "General exception";
494             errorString["201"] = "Invalid argument error";
495             errorString["202"] = "Element cannot have children";
496             errorString["203"] = "Element not an array - cannot have count";
497             errorString["301"] = "Not initialized";
498             errorString["401"] = "Not implemented error";
499             errorString["402"] = "Invalid set value, element is a keyword";
500             errorString["403"] = "Element is read only";
501             errorString["404"] = "Element is write only";
502             errorString["405"] = "Incorrect data type";
503             <?php
504             if (scorm_debugging($scorm)) {
505                 echo 'LogAPICall("LMSGetErrorString", param,  errorString[param], 0);';
506             }
507              ?>
508             return errorString[param];
509         } else {
510            <?php
511             if (scorm_debugging($scorm)) {
512                 echo 'LogAPICall("LMSGetErrorString", param,  "No error string found!", 0);';
513             }
514              ?>
515            return "";
516         }
517     }
519     function LMSGetDiagnostic (param) {
520         if (param == "") {
521             param = errorCode;
522         }
523         <?php
524             if (scorm_debugging($scorm)) {
525                 echo 'LogAPICall("LMSGetDiagnostic", param, param, 0);';
526             }
527         ?>
528         return param;
529     }
531     function AddTime (first, second) {
532         var sFirst = first.split(":");
533         var sSecond = second.split(":");
534         var cFirst = sFirst[2].split(".");
535         var cSecond = sSecond[2].split(".");
536         var change = 0;
538         FirstCents = 0;  //Cents
539         if (cFirst.length > 1) {
540             FirstCents = parseInt(cFirst[1],10);
541         }
542         SecondCents = 0;
543         if (cSecond.length > 1) {
544             SecondCents = parseInt(cSecond[1],10);
545         }
546         var cents = FirstCents + SecondCents;
547         change = Math.floor(cents / 100);
548         cents = cents - (change * 100);
549         if (Math.floor(cents) < 10) {
550             cents = "0" + cents.toString();
551         }
553         var secs = parseInt(cFirst[0],10)+parseInt(cSecond[0],10)+change;  //Seconds
554         change = Math.floor(secs / 60);
555         secs = secs - (change * 60);
556         if (Math.floor(secs) < 10) {
557             secs = "0" + secs.toString();
558         }
560         mins = parseInt(sFirst[1],10)+parseInt(sSecond[1],10)+change;   //Minutes
561         change = Math.floor(mins / 60);
562         mins = mins - (change * 60);
563         if (mins < 10) {
564             mins = "0" + mins.toString();
565         }
567         hours = parseInt(sFirst[0],10)+parseInt(sSecond[0],10)+change;  //Hours
568         if (hours < 10) {
569             hours = "0" + hours.toString();
570         }
572         if (cents != '0') {
573             return hours + ":" + mins + ":" + secs + '.' + cents;
574         } else {
575             return hours + ":" + mins + ":" + secs;
576         }
577     }
579     function TotalTime() {
580         total_time = AddTime(cmi.core.total_time, cmi.core.session_time);
581         return '&'+underscore('cmi.core.total_time')+'='+encodeURIComponent(total_time);
582     }
584     function CollectData(data,parent) {
585         var datastring = '';
586         for (property in data) {
587             if (typeof data[property] == 'object') {
588                 datastring += CollectData(data[property],parent+'.'+property);
589             } else {
590                 element = parent+'.'+property;
591                 expression = new RegExp(CMIIndex,'g');
593                 // get the generic name for this element (e.g. convert 'cmi.interactions.1.id' to 'cmi.interactions.n.id')
594                 elementmodel = String(element).replace(expression,'.n.');
596                 // ignore the session time element
597                 if (element != "cmi.core.session_time") {
599                     // check if this specific element is not defined in the datamodel,
600                     // but the generic element name is
601                     if ((eval('typeof datamodel["'+element+'"]')) == "undefined"
602                         && (eval('typeof datamodel["'+elementmodel+'"]')) != "undefined") {
604                         // add this specific element to the data model (by cloning
605                         // the generic element) so we can track changes to it
606                         eval('datamodel["'+element+'"]=CloneObj(datamodel["'+elementmodel+'"]);');
607                     }
609                     // check if the current element exists in the datamodel
610                     if ((typeof eval('datamodel["'+element+'"]')) != "undefined") {
612                         // make sure this is not a read only element
613                         if (eval('datamodel["'+element+'"].mod') != 'r') {
615                             elementstring = '&'+underscore(element)+'='+encodeURIComponent(data[property]);
617                             // check if the element has a default value
618                             if ((typeof eval('datamodel["'+element+'"].defaultvalue')) != "undefined") {
620                                 // check if the default value is different from the current value
621                                 if (eval('datamodel["'+element+'"].defaultvalue') != data[property]
622                                     || eval('typeof(datamodel["'+element+'"].defaultvalue)') != typeof(data[property])) {
624                                     // append the URI fragment to the string we plan to commit
625                                     datastring += elementstring;
627                                     // update the element default to reflect the current committed value
628                                     eval('datamodel["'+element+'"].defaultvalue=data[property];');
629                                 }
630                             } else {
631                                 // append the URI fragment to the string we plan to commit
632                                 datastring += elementstring;
633                                 // no default value for the element, so set it now
634                                 eval('datamodel["'+element+'"].defaultvalue=data[property];');
635                             }
636                         }
637                     }
638                 }
639             }
640         }
641         return datastring;
642     }
644     function CloneObj(obj){
645         if(obj == null || typeof(obj) != 'object') {
646             return obj;
647         }
649         var temp = new obj.constructor(); // changed (twice)
650         for(var key in obj) {
651             temp[key] = CloneObj(obj[key]);
652         }
654         return temp;
655     }
657     function StoreData(data,storetotaltime) {
658         if (storetotaltime) {
659             if (cmi.core.lesson_status == 'not attempted') {
660                 cmi.core.lesson_status = 'completed';
661             }
662             if (cmi.core.lesson_mode == 'normal') {
663                 if (cmi.core.credit == 'credit') {
664                     if (cmi.student_data.mastery_score !== '' && cmi.core.score.raw !== '') {
665                         if (parseFloat(cmi.core.score.raw) >= parseFloat(cmi.student_data.mastery_score)) {
666                             cmi.core.lesson_status = 'passed';
667                         } else {
668                             cmi.core.lesson_status = 'failed';
669                         }
670                     }
671                 }
672             }
673             if (cmi.core.lesson_mode == 'browse') {
674                 if (datamodel['cmi.core.lesson_status'].defaultvalue == '' && cmi.core.lesson_status == 'not attempted') {
675                     cmi.core.lesson_status = 'browsed';
676                 }
677             }
678             datastring = CollectData(data,'cmi');
679             datastring += TotalTime();
680         } else {
681             datastring = CollectData(data,'cmi');
682         }
683         datastring += '&attempt=<?php echo $attempt ?>';
684         datastring += '&scoid=<?php echo $scoid ?>';
686         var myRequest = NewHttpReq();
687         //alert('going to:' + "<?php p($CFG->wwwroot) ?>/mod/scorm/datamodel.php" + "id=<?php p($id) ?>&a=<?php p($a) ?>&sesskey=<?php echo sesskey() ?>"+datastring);
688         result = DoRequest(myRequest,"<?php p($CFG->wwwroot) ?>/mod/scorm/datamodel.php","id=<?php p($id) ?>&a=<?php p($a) ?>&sesskey=<?php echo sesskey() ?>"+datastring);
689         results = String(result).split('\n');
690         errorCode = results[1];
691         return results[0];
692     }
694     this.LMSInitialize = LMSInitialize;
695     this.LMSFinish = LMSFinish;
696     this.LMSGetValue = LMSGetValue;
697     this.LMSSetValue = LMSSetValue;
698     this.LMSCommit = LMSCommit;
699     this.LMSGetLastError = LMSGetLastError;
700     this.LMSGetErrorString = LMSGetErrorString;
701     this.LMSGetDiagnostic = LMSGetDiagnostic;
704 var API = new SCORMapi1_2();
706 <?php
707 // pull in the debugging utilities
708 if (scorm_debugging($scorm)) {
709     include_once($CFG->dirroot.'/mod/scorm/datamodels/debug.js.php');
710     echo 'AppendToLog("Moodle SCORM 1.2 API Loaded, Activity: '.$scorm->name.', SCO: '.$sco->identifier.'", 0);';