forked from jcputney/scorm-again
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScorm2004API.js
573 lines (524 loc) · 17.2 KB
/
Scorm2004API.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// @flow
import BaseAPI from './BaseAPI';
import {
ADL,
CMI,
CMICommentsObject,
CMIInteractionsCorrectResponsesObject,
CMIInteractionsObject,
CMIInteractionsObjectivesObject,
CMIObjectivesObject,
} from './cmi/scorm2004_cmi';
import * as Utilities from './utilities';
import APIConstants from './constants/api_constants';
import ErrorCodes from './constants/error_codes';
import Responses from './constants/response_constants';
import ValidLanguages from './constants/language_constants';
import Regex from './constants/regex';
const scorm2004_constants = APIConstants.scorm2004;
const global_constants = APIConstants.global;
const scorm2004_error_codes = ErrorCodes.scorm2004;
const correct_responses = Responses.correct;
const scorm2004_regex = Regex.scorm2004;
/**
* API class for SCORM 2004
*/
export default class Scorm2004API extends BaseAPI {
#version: '1.0';
/**
* Constructor for SCORM 2004 API
* @param {object} settings
*/
constructor(settings: {}) {
const finalSettings = {
...{
mastery_override: false,
}, ...settings,
};
super(scorm2004_error_codes, finalSettings);
this.cmi = new CMI();
this.adl = new ADL();
// Rename functions to match 2004 Spec and expose to modules
this.Initialize = this.lmsInitialize;
this.Terminate = this.lmsTerminate;
this.GetValue = this.lmsGetValue;
this.SetValue = this.lmsSetValue;
this.Commit = this.lmsCommit;
this.GetLastError = this.lmsGetLastError;
this.GetErrorString = this.lmsGetErrorString;
this.GetDiagnostic = this.lmsGetDiagnostic;
}
/**
* Getter for #version
* @return {string}
*/
get version() {
return this.#version;
}
/**
* @return {string} bool
*/
lmsInitialize() {
this.cmi.initialize();
return this.initialize('Initialize');
}
/**
* @return {string} bool
*/
lmsTerminate() {
const result = this.terminate('Terminate', true);
if (result === global_constants.SCORM_TRUE) {
if (this.adl.nav.request !== '_none_') {
switch (this.adl.nav.request) {
case 'continue':
this.processListeners('SequenceNext');
break;
case 'previous':
this.processListeners('SequencePrevious');
break;
case 'choice':
this.processListeners('SequenceChoice');
break;
case 'exit':
this.processListeners('SequenceExit');
break;
case 'exitAll':
this.processListeners('SequenceExitAll');
break;
case 'abandon':
this.processListeners('SequenceAbandon');
break;
case 'abandonAll':
this.processListeners('SequenceAbandonAll');
break;
}
} else if (this.settings.autoProgress) {
this.processListeners('SequenceNext');
}
}
return result;
}
/**
* @param {string} CMIElement
* @return {string}
*/
lmsGetValue(CMIElement) {
return this.getValue('GetValue', true, CMIElement);
}
/**
* @param {string} CMIElement
* @param {any} value
* @return {string}
*/
lmsSetValue(CMIElement, value) {
return this.setValue('SetValue', true, CMIElement, value);
}
/**
* Orders LMS to store all content parameters
*
* @return {string} bool
*/
lmsCommit() {
return this.commit('Commit');
}
/**
* Returns last error code
*
* @return {string}
*/
lmsGetLastError() {
return this.getLastError('GetLastError');
}
/**
* Returns the errorNumber error description
*
* @param {(string|number)} CMIErrorCode
* @return {string}
*/
lmsGetErrorString(CMIErrorCode) {
return this.getErrorString('GetErrorString', CMIErrorCode);
}
/**
* Returns a comprehensive description of the errorNumber error.
*
* @param {(string|number)} CMIErrorCode
* @return {string}
*/
lmsGetDiagnostic(CMIErrorCode) {
return this.getDiagnostic('GetDiagnostic', CMIErrorCode);
}
/**
* Sets a value on the CMI Object
*
* @param {string} CMIElement
* @param {any} value
* @return {string}
*/
setCMIValue(CMIElement, value) {
return this._commonSetCMIValue('SetValue', true, CMIElement, value);
}
/**
* Gets or builds a new child element to add to the array.
*
* @param {string} CMIElement
* @param {any} value
* @param {boolean} foundFirstIndex
* @return {any}
*/
getChildElement(CMIElement, value, foundFirstIndex) {
let newChild;
if (this.stringMatches(CMIElement, 'cmi\\.objectives\\.\\d')) {
newChild = new CMIObjectivesObject();
} else if (foundFirstIndex && this.stringMatches(CMIElement,
'cmi\\.interactions\\.\\d\\.correct_responses\\.\\d')) {
const parts = CMIElement.split('.');
const index = Number(parts[2]);
const interaction = this.cmi.interactions.childArray[index];
if (!interaction.type) {
this.throwSCORMError(scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
} else {
const interaction_type = interaction.type;
const interaction_count = interaction.correct_responses._count;
if (interaction_type === 'choice') {
for (let i = 0; i < interaction_count && this.lastErrorCode ===
0; i++) {
const response = interaction.correct_responses.childArray[i];
if (response.pattern === value) {
this.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE);
}
}
}
const response_type = correct_responses[interaction_type];
if (response_type) {
let nodes = [];
if (response_type?.delimiter) {
nodes = String(value).split(response_type.delimiter);
} else {
nodes[0] = value;
}
if (nodes.length > 0 && nodes.length <= response_type.max) {
this.checkCorrectResponseValue(interaction_type, nodes, value);
} else if (nodes.length > response_type.max) {
this.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE,
'Data Model Element Pattern Too Long');
}
} else {
this.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE,
'Incorrect Response Type: ' + interaction_type);
}
}
if (this.lastErrorCode === 0) {
newChild = new CMIInteractionsCorrectResponsesObject();
}
} else if (foundFirstIndex && this.stringMatches(CMIElement,
'cmi\\.interactions\\.\\d\\.objectives\\.\\d')) {
newChild = new CMIInteractionsObjectivesObject();
} else if (!foundFirstIndex &&
this.stringMatches(CMIElement, 'cmi\\.interactions\\.\\d')) {
newChild = new CMIInteractionsObject();
} else if (this.stringMatches(CMIElement,
'cmi\\.comments_from_learner\\.\\d')) {
newChild = new CMICommentsObject();
} else if (this.stringMatches(CMIElement,
'cmi\\.comments_from_lms\\.\\d')) {
newChild = new CMICommentsObject(true);
}
return newChild;
}
/**
* Validate correct response.
* @param {string} CMIElement
* @param {*} value
*/
validateCorrectResponse(CMIElement, value) {
const parts = CMIElement.split('.');
const index = Number(parts[2]);
const pattern_index = Number(parts[4]);
const interaction = this.cmi.interactions.childArray[index];
const interaction_type = interaction.type;
const interaction_count = interaction.correct_responses._count;
if (interaction_type === 'choice') {
for (let i = 0; i < interaction_count && this.lastErrorCode === 0; i++) {
const response = interaction.correct_responses.childArray[i];
if (response.pattern === value) {
this.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE);
}
}
}
const response_type = correct_responses[interaction_type];
if (typeof response_type.limit === 'undefined' || interaction_count <=
response_type.limit) {
let nodes = [];
if (response_type?.delimiter) {
nodes = String(value).split(response_type.delimiter);
} else {
nodes[0] = value;
}
if (nodes.length > 0 && nodes.length <= response_type.max) {
this.checkCorrectResponseValue(interaction_type, nodes, value);
} else if (nodes.length > response_type.max) {
this.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE,
'Data Model Element Pattern Too Long');
}
if (this.lastErrorCode === 0 &&
(!response_type.duplicate ||
!this.checkDuplicatedPattern(interaction.correct_responses,
pattern_index, value)) ||
(this.lastErrorCode === 0 && value === '')) {
// do nothing, we want the inverse
} else {
if (this.lastErrorCode === 0) {
this.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE,
'Data Model Element Pattern Already Exists');
}
}
} else {
this.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE,
'Data Model Element Collection Limit Reached');
}
}
/**
* Gets a value from the CMI Object
*
* @param {string} CMIElement
* @return {*}
*/
getCMIValue(CMIElement) {
return this._commonGetCMIValue('GetValue', true, CMIElement);
}
/**
* Returns the message that corresponds to errorNumber.
*
* @param {(string|number)} errorNumber
* @param {boolean} detail
* @return {string}
*/
getLmsErrorMessageDetails(errorNumber, detail) {
let basicMessage = '';
let detailMessage = '';
// Set error number to string since inconsistent from modules if string or number
errorNumber = String(errorNumber);
if (scorm2004_constants.error_descriptions[errorNumber]) {
basicMessage = scorm2004_constants.error_descriptions[errorNumber].basicMessage;
detailMessage = scorm2004_constants.error_descriptions[errorNumber].detailMessage;
}
return detail ? detailMessage : basicMessage;
}
/**
* Check to see if a correct_response value has been duplicated
* @param {CMIArray} correct_response
* @param {number} current_index
* @param {*} value
* @return {boolean}
*/
checkDuplicatedPattern = (correct_response, current_index, value) => {
let found = false;
const count = correct_response._count;
for (let i = 0; i < count && !found; i++) {
if (i !== current_index && correct_response.childArray[i] === value) {
found = true;
}
}
return found;
};
/**
* Checks for a valid correct_response value
* @param {string} interaction_type
* @param {Array} nodes
* @param {*} value
*/
checkCorrectResponseValue(interaction_type, nodes, value) {
const response = correct_responses[interaction_type];
const formatRegex = new RegExp(response.format);
for (let i = 0; i < nodes.length && this.lastErrorCode === 0; i++) {
if (interaction_type.match(
'^(fill-in|long-fill-in|matching|performance|sequencing)$')) {
nodes[i] = this.removeCorrectResponsePrefixes(nodes[i]);
}
if (response?.delimiter2) {
const values = nodes[i].split(response.delimiter2);
if (values.length === 2) {
const matches = values[0].match(formatRegex);
if (!matches) {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
} else {
if (!values[1].match(new RegExp(response.format2))) {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
}
} else {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
} else {
const matches = nodes[i].match(formatRegex);
if ((!matches && value !== '') ||
(!matches && interaction_type === 'true-false')) {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
} else {
if (interaction_type === 'numeric' && nodes.length > 1) {
if (Number(nodes[0]) > Number(nodes[1])) {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
} else {
if (nodes[i] !== '' && response.unique) {
for (let j = 0; j < i && this.lastErrorCode === 0; j++) {
if (nodes[i] === nodes[j]) {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
}
}
}
}
}
}
}
/**
* Remove prefixes from correct_response
* @param {string} node
* @return {*}
*/
removeCorrectResponsePrefixes(node) {
let seenOrder = false;
let seenCase = false;
let seenLang = false;
const prefixRegex = new RegExp(
'^({(lang|case_matters|order_matters)=([^}]+)})');
let matches = node.match(prefixRegex);
let langMatches = null;
while (matches) {
switch (matches[2]) {
case 'lang':
langMatches = node.match(scorm2004_regex.CMILangcr);
if (langMatches) {
const lang = langMatches[3];
if (lang !== undefined && lang.length > 0) {
if (ValidLanguages[lang.toLowerCase()] === undefined) {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
}
}
seenLang = true;
break;
case 'case_matters':
if (!seenLang && !seenOrder && !seenCase) {
if (matches[3] !== 'true' && matches[3] !== 'false') {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
}
seenCase = true;
break;
case 'order_matters':
if (!seenCase && !seenLang && !seenOrder) {
if (matches[3] !== 'true' && matches[3] !== 'false') {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
}
seenOrder = true;
break;
default:
break;
}
node = node.substr(matches[1].length);
matches = node.match(prefixRegex);
}
return node;
}
/**
* Replace the whole API with another
* @param {Scorm2004API} newAPI
*/
replaceWithAnotherScormAPI(newAPI) {
// Data Model
this.cmi = newAPI.cmi;
this.adl = newAPI.adl;
}
/**
* Render the cmi object to the proper format for LMS commit
*
* @param {boolean} terminateCommit
* @return {object|Array}
*/
renderCommitCMI(terminateCommit: boolean) {
const cmiExport = this.renderCMIToJSONObject();
if (terminateCommit) {
cmiExport.cmi.total_time = this.cmi.getCurrentTotalTime();
}
const result = [];
const flattened = Utilities.flatten(cmiExport);
switch (this.settings.dataCommitFormat) {
case 'flattened':
return Utilities.flatten(cmiExport);
case 'params':
for (const item in flattened) {
if ({}.hasOwnProperty.call(flattened, item)) {
result.push(`${item}=${flattened[item]}`);
}
}
return result;
case 'json':
default:
return cmiExport;
}
}
/**
* Attempts to store the data to the LMS
*
* @param {boolean} terminateCommit
* @return {string}
*/
storeData(terminateCommit: boolean) {
if (terminateCommit) {
if (this.cmi.mode === 'normal') {
if (this.cmi.credit === 'credit') {
if (this.cmi.completion_threshold && this.cmi.progress_measure) {
if (this.cmi.progress_measure >= this.cmi.completion_threshold) {
console.debug('Setting Completion Status: Completed');
this.cmi.completion_status = 'completed';
} else {
console.debug('Setting Completion Status: Incomplete');
this.cmi.completion_status = 'incomplete';
}
}
if (this.cmi.scaled_passing_score && this.cmi.score.scaled) {
if (this.cmi.score.scaled >= this.cmi.scaled_passing_score) {
console.debug('Setting Success Status: Passed');
this.cmi.success_status = 'passed';
} else {
console.debug('Setting Success Status: Failed');
this.cmi.success_status = 'failed';
}
}
}
}
}
let navRequest = false;
if (this.adl.nav.request !== (this.startingData?.adl?.nav?.request) &&
this.adl.nav.request !== '_none_') {
this.adl.nav.request = encodeURIComponent(this.adl.nav.request);
navRequest = true;
}
const commitObject = this.renderCommitCMI(terminateCommit);
if (this.settings.lmsCommitUrl) {
if (this.apiLogLevel === global_constants.LOG_LEVEL_DEBUG) {
console.debug('Commit (terminated: ' +
(terminateCommit ? 'yes' : 'no') + '): ');
console.debug(commitObject);
}
const result = this.processHttpRequest(this.settings.lmsCommitUrl,
commitObject);
// check if this is a sequencing call, and then call the necessary JS
{
if (navRequest && result.navRequest !== undefined &&
result.navRequest !== '') {
Function(`"use strict";(() => { ${result.navRequest} })()`)();
}
}
return result;
} else {
console.log('Commit (terminated: ' +
(terminateCommit ? 'yes' : 'no') + '): ');
console.log(commitObject);
return global_constants.SCORM_TRUE;
}
}
}