-
Notifications
You must be signed in to change notification settings - Fork 1
/
scorm.js
513 lines (478 loc) · 18.5 KB
/
scorm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
Elucidat SCORM API wrapper - https://github.com/elucidat/eluciat-scorm-wrapper/
Licensed under the MIT license
Copyright (c) 2013 Elucidat Ltd
Version 1.0.1
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var Debug_API = function () {};
// 2004 functions
Debug_API.prototype.Initialize = function () { console.log(' └ Debug_api:Initialize'); return true; };
Debug_API.prototype.Terminate = function () { console.log(' └ Debug_api:Terminate'); return true; };
Debug_API.prototype.GetValue = function (nam) { return ''; };
Debug_API.prototype.SetValue = function (nam,val) { console.log(' ┌ Debug_api:SetValue: '+nam+'='+val); return ''; };
Debug_API.prototype.Commit = function () { console.log(' └ Debug_api:Commit'); return true; };
Debug_API.prototype.GetLastError = function () { return 0; };
Debug_API.prototype.GetErrorString = function (code) { return ""; };
Debug_API.prototype.GetDiagnostic = function (code) { return ""; };
// 1.2 functions
Debug_API.prototype.LMSInitialize = function () { console.log(' └ Debug_api:LMSInitialize'); return true; };
Debug_API.prototype.LMSFinish = function () { console.log(' └ Debug_api:LMSFinish'); return true; };
Debug_API.prototype.LMSGetValue = function (nam) { return ''; };
Debug_API.prototype.LMSSetValue = function (nam,val) { console.log(' ┌ Debug_api:LMSSetValue: '+nam+'='+val); return ''; };
Debug_API.prototype.LMSCommit = function () { console.log(' └ Debug_api:Commit'); return true; };
Debug_API.prototype.LMSGetLastError = function () { return 0; };
Debug_API.prototype.LMSGetErrorString = function (code) { return ""; };
Debug_API.prototype.LMSGetDiagnostic = function (code) { return ""; };
var Scorm = function (options) {
this.options = $.extend({}, {
debug_mode: '2004'
}, options);
this.scorm_interface = null;
// default used by debug interface
this.mode = null;
this.active = false;
this.objectives = [];
this.interactions = [];
this.start_time = new Date().getTime();
this.has_score = false;
this.is_debug = false;
// we need to search window, window.parent(s) and window.top.opener for either API or API_1484_11
this._search_for_api ( window );
if (this.scorm_interface == null) {
this.mode = this.options.debug_mode;
// if we have the scorm_mode variable then
/* -- assess impact on BMJ before adding
if (window['default_scorm_mode'] && default_scorm_mode !== undefined) {
// if it's a string
if(typeof(default_scorm_mode) === 'string') {
// set the mode to the scorm_mode value passed through
this.mode = default_scorm_mode;
}
}
*/
this.is_debug = true;
console.log('LMS not present - Created SCORM '+this.mode+' Debug interface.');
this.scorm_interface = new Debug_API();
} else {
// now test to see if the interface is 1.2 or 2004
if ("LMSCommit" in this.scorm_interface)
this.mode = "1.2";
else
this.mode = "2004";
console.log('Found SCORM '+this.mode+' interface.');
}
};
Scorm.prototype._format_time = function ( session_time ) {
function pad(num, size) {
var s = "0000" + new String(Math.round(num));
return s.substr(s.length-size);
}
// convert session time to seconds
session_time /= 1000;
// scorm 2004
//if (this.mode == '2004')
// return Math.round(session_time);
// split out the time parts
var hours = Math.floor(session_time / 3600);
session_time = session_time - (hours * 3600);
var minutes = Math.floor(session_time / 60);
var session_time = session_time - (minutes * 60);
if (this.mode == '2004') {
// scorm 2004 - this is kept for now in case - http://www.ostyn.com/standards/scorm/samples/ISOTimeForSCORM.htm
return 'PT'+(hours?hours+'H':'')+(minutes?minutes+'M':'')+Math.round(session_time)+'S';
} else {
// scorm 1.2
return pad(hours,2)+':'+pad(minutes,2)+':'+pad(session_time,2);
}
}
Scorm.prototype._search_for_api = function ( win ) {
try {
while (win != null && this.scorm_interface == null) {
// record the API if we've found it
// if we have win.API first - so if both are present we opt for win.API
if (win.API) {
this.scorm_interface = win.API;
} else if (win.API_1484_11) {
this.scorm_interface = win.API_1484_11;
}
// now branch off to look at the window opener of this window.
if (win.opener != null && !win.opener.closed)
this._search_for_api ( win.opener );
// if at the top - that's the end
if (win == win.parent)
win = null;
else
// up up up the tree
win = win.parent;
}
} catch(err) {
return null;
}
};
/* general API calls */
Scorm.prototype.Check = function () {
var error = 0, error_string = '';
if (this.mode == '2004') {
error = parseInt(this.scorm_interface.GetLastError());
if (error) error_string = 'Error ('+error+'): '+this.scorm_interface.GetErrorString(error);
} else if (this.mode == '1.2') {
error = parseInt(this.scorm_interface.LMSGetLastError());
if (error) error_string = 'Error ('+error+'): '+this.scorm_interface.LMSGetErrorString(error);
}
return { 'code': error, 'description': error_string };
};
Scorm.prototype.Initialize = function () {
console.log('Scorm:Initialize');
if (this.mode == '2004') {
this.scorm_interface.Initialize('');
} else if (this.mode == '1.2') {
this.scorm_interface.LMSInitialize('');
}
// check for errors
if (this.Check()['code']==0) this.active = true;
// mark course as incomplete if it has not been attempted
var status = this.GetCompletionStatus();
if (!status || status == 'not attempted' || status == 'unknown'){
this.SetIncomplete();
this.SetExit('suspend');
}
return this.active;
};
Scorm.prototype.Terminate = function () {
console.log('Scorm:Terminate');
if (this.mode == '2004') {
this.scorm_interface.Terminate('');
} else if (this.mode == '1.2') {
this.scorm_interface.LMSFinish('');
}
// check for errors
if (this.Check()['code']==0) this.active = false;
};
Scorm.prototype.Deactivate = function () {
console.log('Scorm:Deactivate');
this.active = false;
};
Scorm.prototype.SetValue = function ( varname, value, skip_checking ) {
if (this.active) {
var checkback;
if (this.mode == '2004') {
this.scorm_interface.SetValue( varname, value );
if (!skip_checking)
checkback = this.scorm_interface.GetValue( varname );
} else if (this.mode == '1.2') {
this.scorm_interface.LMSSetValue( varname, value );
if (!skip_checking)
checkback = this.scorm_interface.LMSGetValue( varname );
}
// we are also going to send as a postMessage to the top window
if (top['postMessage'])
top.postMessage( varname+'='+value, '*' );
// make sure that worked
var error = this.Check();
var feedback = 'Scorm:SetValue: '+varname+'='+value;
if (!skip_checking)
feedback += '? '+(error['code']?error['description']: 'Echo:'+checkback);
if(error['code'] && error['code'] !== 0){
try {
window.dispatchEvent(new CustomEvent('REPORT_ERROR', {
'detail': {
error: 'Scorm Error',
type: 'SCORM',
code: error['code'],
description: error['description'],
key: varname,
value: value
}
}));
} catch (e) {
console.warn("Error reporting error: " + e);
}
}
console.log(feedback);
return error['code'];
}
};
Scorm.prototype.GetValue = function ( varname ) {
if (this.active) {
var val;
if (this.mode == '2004') val = this.scorm_interface.GetValue( varname );
else if (this.mode == '1.2') val = this.scorm_interface.LMSGetValue( varname );
console.log('Scorm:GetValue: '+varname+'='+val);
return val;
}
};
Scorm.prototype.Commit = function () {
console.log('Scorm:Commit');
if (this.mode == '2004') {
return this.scorm_interface.Commit('');
} else if (this.mode == '1.2') {
return this.scorm_interface.LMSCommit('');
}
};
/* specific actions */
/* being friendly */
Scorm.prototype.GetLearnerName = function () {
if (this.mode == '2004')
return this.GetValue('cmi.learner_name');
else if (this.mode == '1.2')
return this.GetValue('cmi.core.student_name');
};
/* being friendly */
Scorm.prototype.GetLearnerID = function () {
if (this.mode == '2004')
return this.GetValue('cmi.learner_id');
else if (this.mode == '1.2')
return this.GetValue('cmi.core.student_id');
};
/* Browsing History */
Scorm.prototype.GetLocation = function () {
if (this.mode == '2004')
return this.GetValue('cmi.location');
else if (this.mode == '1.2')
return this.GetValue('cmi.core.lesson_location');
};
Scorm.prototype.SetLocation = function ( url ) {
if (this.mode == '2004')
return this.SetValue('cmi.location', url);
else if (this.mode == '1.2')
return this.SetValue('cmi.core.lesson_location', url);
};
/* Exit */
Scorm.prototype.GetExit = function () {
if (this.mode == '2004')
return this.GetValue('cmi.exit');
else if (this.mode == '1.2')
return this.GetValue('cmi.core.exit');
};
Scorm.prototype.SetExit = function ( value ) {
if (this.mode == '2004')
return this.SetValue('cmi.exit', value);
else if (this.mode == '1.2')
return this.SetValue('cmi.core.exit', value);
};
/* Suspend data */
Scorm.prototype.GetSuspendData = function () {
return this.GetValue('cmi.suspend_data');
};
Scorm.prototype.SetSuspendData = function ( data ) {
this.SetValue('cmi.suspend_data', data);
// this one is important so save now
this.Commit();
};
/* Progress */
Scorm.prototype.GetProgress = function () {
if (this.mode == '2004')
return this.GetValue('cmi.progress_measure');
};
Scorm.prototype.SetProgress = function ( progress ) {
if (this.mode == '2004')
return this.SetValue('cmi.progress_measure', progress);
};
/* Completion Threshold */
Scorm.prototype.GetCompletionThreshold = function () {
if (this.mode == '2004')
return this.GetValue('cmi.completion_threshold');
return false;
};
Scorm.prototype.SetCompletionThreshold = function ( threshold ) {
if (this.mode == '2004')
this.SetValue('cmi.completion_threshold', threshold);
};
/* Score Threshold */
Scorm.prototype.GetScoreThreshold = function () {
if (this.mode == '2004')
return this.GetValue('cmi.progress_measure');
return false;
};
Scorm.prototype.SetScoreThreshold = function ( threshold ) {
if (this.mode == '2004')
this.SetValue('cmi.scaled_passing_score', threshold);
};
Scorm.prototype.SetSessionTime = function ( session_time ) { // session_time in javascript format - ie - milliseconds
if (!session_time)
session_time = ( new Date().getTime() ) - this.start_time;
if (this.mode == '2004')
return this.SetValue('cmi.session_time', this._format_time( session_time ), true );
else if (this.mode == '1.2')
return this.SetValue('cmi.core.session_time', this._format_time( session_time ), true );
};
Scorm.prototype.GetTotalTime = function () {
if (this.mode == '2004')
return this.GetValue('cmi.total_time');
else if (this.mode == '1.2')
return this.GetValue('cmi.core.total_time');
return false;
};
Scorm.prototype.GetCompletionStatus = function () {
if (this.mode == '2004') {
// 2004 doesn't need any fixing
return this.GetValue('cmi.completion_status');
} else if (this.mode == '1.2') {
// passed and failed are not relevant here - they are just in this case synonyms for completed and incomplete
var status = this.GetValue('cmi.core.lesson_status');
if (status == 'passed' || status == 'failed') return 'completed';
return status;
}
};
Scorm.prototype.SetCompletionStatus = function ( v ) {
// and send
if (this.mode == '2004')
this.SetValue('cmi.completion_status', v);
else if (this.mode == '1.2')
this.SetValue('cmi.core.lesson_status', v);
// this one is important so save now
this.Commit();
};
Scorm.prototype.GetResult = function () {
if (this.mode == '2004') {
// 2004 doesn't need any fixing
return this.GetValue('cmi.success_status');
} else if (this.mode == '1.2') {
// passed and failed are not relevant here - they are just in this case synonyms for completed and incomplete
var status = this.GetValue('cmi.core.lesson_status');
if (status == 'passed' || status == 'failed') return status;
return 'unknown';
}
};
Scorm.prototype.SetResult = function ( outcome ) {
if (this.mode == '2004') {
if (outcome == 'passed') {
this.SetValue('cmi.success_status','passed');
} else if (outcome == 'failed') {
this.SetValue('cmi.success_status','failed');
}
} else if (this.mode == '1.2') {
// complete and outcome are stored in the same variable in 1.2, so we just use completion status
if (outcome == 'passed')
this.SetCompletionStatus('passed');
else if (outcome == 'failed')
this.SetCompletionStatus('failed');
}
// this one is important so save now
this.Commit();
};
/* Complete the course - maybe unnecessary */
Scorm.prototype.SetPassed = function () {
// set score
this.SetResult( 'passed' );
};
Scorm.prototype.SetFailed = function () {
// set score
this.SetResult( 'failed' );
};
Scorm.prototype.SetCompleted = function () {
// set score
this.SetCompletionStatus( 'completed' );
};
Scorm.prototype.SetIncomplete = function () {
// set score
this.SetCompletionStatus( 'incomplete' );
};
Scorm.prototype.SetScore = function (score, min, max) {
this.has_score = true;
if (this.mode == '2004') {
this.SetValue('cmi.score.raw',score);
this.SetValue('cmi.score.scaled',1/max*score);
this.SetValue('cmi.score.min',min);
this.SetValue('cmi.score.max',max);
} else if (this.mode == '1.2') {
this.SetValue('cmi.core.score.raw',score);
this.SetValue('cmi.core.score.min',min);
this.SetValue('cmi.core.score.max',max);
}
// this one is important so save now
this.Commit();
};
/* record an objective in the course */
Scorm.prototype.SetObjective = function ( objective_name, outcome, score, min, max, description ) {
var ob_id = this.objectives.indexOf( objective_name );
if (ob_id == -1) {
// init if not exists already
ob_id = this.objectives.length;
this.SetValue('cmi.objectives.'+ob_id+'.id', objective_name);
if (this.mode == '2004') this.SetValue('cmi.objectives.'+ob_id+'.description', description);
// increment
this.objectives.push(objective_name);
// init score
this.SetValue('cmi.objectives.'+ob_id+'.score.min', (min?min:0));
this.SetValue('cmi.objectives.'+ob_id+'.score.max', (max?max:100));
}
// always do scoring
this.SetValue('cmi.objectives.'+ob_id+'.score.raw', (score?score:0));
if (this.mode == '2004') {
this.SetValue('cmi.objectives.'+ob_id+'.completion_status','completed');
if (outcome=='passed'||outcome=='failed') this.SetValue('cmi.objectives.'+ob_id+'.success_status', outcome);
} else {
if (outcome=='passed'||outcome=='failed') this.SetValue('cmi.objectives.'+ob_id+'.status', outcome);
else this.SetValue('cmi.objectives.'+ob_id+'.status', 'completed');
}
};
/* record an objective in the course */
Scorm.prototype.SetInteraction = function ( interaction_name, objective_name, outcome, learner_response, description, interaction_type, correct_response_pattern, weighting ) {
var int_id = this.interactions.indexOf( interaction_name );
var skip_checking = (this.mode == '1.2' ? true : false);
if (int_id == -1) {
// init if not exists already
int_id = this.interactions.length;
this.SetValue('cmi.interactions.'+int_id+'.id', interaction_name, skip_checking);
this.SetValue('cmi.interactions.'+int_id+'.objectives.0.id', objective_name, skip_checking);
if (this.mode == '2004') {
this.SetValue('cmi.interactions.'+int_id+'.description', description);
this.SetValue('cmi.interactions.'+int_id+'.type', interaction_type);
}
// increment
this.interactions.push(interaction_name);
}
if (learner_response) {
if (this.mode == '2004') {
if( interaction_type && correct_response_pattern ) {
this.SetValue('cmi.interactions.'+int_id+'.learner_response', learner_response);
//did it really set the value?
if(this.GetValue('cmi.interactions.'+int_id+'.learner_response') != learner_response){
//No it hasn't set it to something guaranteed to be acceptable, then try setting it again!
this.SetValue('cmi.interactions.'+int_id+'.learner_response', "elucidaterror");
this.SetValue('cmi.interactions.'+int_id+'.learner_response', learner_response);
if(this.GetValue('cmi.interactions.'+int_id+'.learner_response') != learner_response){
//Oh dear it's still not setting, this probably means the [,] isnt allowed, lets replace it
var sanitised_response = learner_response.split("[,]").join("__");
this.SetValue('cmi.interactions.'+int_id+'.learner_response', sanitised_response);
}
}
// Send the correct answer id
this.SetValue('cmi.interactions.'+int_id+'.correct_responses.0.pattern', correct_response_pattern);
this.SetValue('cmi.interactions.'+int_id+'.weighting', weighting);
}
} else {
this.SetValue('cmi.interactions.'+int_id+'.student_response', learner_response, skip_checking);
}
}
if(outcome=='neutral'){
this.SetValue('cmi.interactions.'+int_id+'.result', 'neutral', skip_checking);
}else if (outcome=='passed' || outcome=='completed') {
this.SetValue('cmi.interactions.'+int_id+'.result', 'correct', skip_checking);
} else if (outcome=='failed') {
if (this.mode == '2004')
this.SetValue('cmi.interactions.'+int_id+'.result', 'incorrect');
else
this.SetValue('cmi.interactions.'+int_id+'.result', 'wrong', skip_checking);
}
};
Scorm.prototype.GetLanguage = function() {
var code = null;
if (this.mode == '2004')
code = this.GetValue('cmi.learner_preference.language');
else if (this.mode == '1.2')
code = this.GetValue('cmi.student_preference.language');
if ( code )
code = code.toLowerCase().trim();
return code;
}
Scorm.prototype.SetLanguage = function( value ) {
if (this.mode == '2004')
return this.SetValue('cmi.learner_preference.language', value );
else if (this.mode == '1.2')
return this.SetValue('cmi.student_preference.language', value );
}