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