-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathSCORM_API_wrapper.js
924 lines (613 loc) · 27.4 KB
/
SCORM_API_wrapper.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
/*global define, module */
/* ===========================================================
pipwerks SCORM Wrapper for JavaScript
v1.1.20180906
Created by Philip Hutchison, January 2008-2018
https://github.com/pipwerks/scorm-api-wrapper
Copyright (c) Philip Hutchison
MIT-style license: http://pipwerks.mit-license.org/
This wrapper works with both SCORM 1.2 and SCORM 2004.
Inspired by APIWrapper.js, created by the ADL and
Concurrent Technologies Corporation, distributed by
the ADL (http://www.adlnet.gov/scorm).
SCORM.API.find() and SCORM.API.get() functions based
on ADL code, modified by Mike Rustici
(http://www.scorm.com/resources/apifinder/SCORMAPIFinder.htm),
further modified by Philip Hutchison
=============================================================== */
(function(root, factory) {
"use strict";
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.pipwerks = factory();
}
}(this, function() {
"use strict";
var pipwerks = {}; //pipwerks 'namespace' helps ensure no conflicts with possible other "SCORM" variables
pipwerks.UTILS = {}; //For holding UTILS functions
pipwerks.debug = { isActive: true }; //Enable (true) or disable (false) for debug mode
pipwerks.SCORM = { //Define the SCORM object
version: null, //Store SCORM version.
handleCompletionStatus: true, //Whether or not the wrapper should automatically handle the initial completion status
handleExitMode: true, //Whether or not the wrapper should automatically handle the exit mode
API: {
handle: null,
isFound: false
}, //Create API child object
connection: { isActive: false }, //Create connection child object
data: {
completionStatus: null,
exitStatus: null
}, //Create data child object
debug: {} //Create debug child object
};
/* --------------------------------------------------------------------------------
pipwerks.SCORM.isAvailable
A simple function to allow Flash ExternalInterface to confirm
presence of JS wrapper before attempting any LMS communication.
Parameters: none
Returns: Boolean (true)
----------------------------------------------------------------------------------- */
pipwerks.SCORM.isAvailable = function() {
return true;
};
// ------------------------------------------------------------------------- //
// --- SCORM.API functions ------------------------------------------------- //
// ------------------------------------------------------------------------- //
/* -------------------------------------------------------------------------
pipwerks.SCORM.API.find(window)
Looks for an object named API in parent and opener windows
Parameters: window (the browser window object).
Returns: Object if API is found, null if no API found
---------------------------------------------------------------------------- */
pipwerks.SCORM.API.find = function(win) {
var API = null,
findAttempts = 0,
findAttemptLimit = 500,
traceMsgPrefix = "SCORM.API.find",
trace = pipwerks.UTILS.trace,
scorm = pipwerks.SCORM;
while ((!win.API && !win.API_1484_11) &&
(win.parent) &&
(win.parent != win) &&
(findAttempts <= findAttemptLimit)) {
findAttempts++;
win = win.parent;
}
//If SCORM version is specified by user, look for specific API
if (scorm.version) {
switch (scorm.version) {
case "2004":
if (win.API_1484_11) {
API = win.API_1484_11;
} else {
trace(traceMsgPrefix + ": SCORM version 2004 was specified by user, but API_1484_11 cannot be found.");
}
break;
case "1.2":
if (win.API) {
API = win.API;
} else {
trace(traceMsgPrefix + ": SCORM version 1.2 was specified by user, but API cannot be found.");
}
break;
}
} else { //If SCORM version not specified by user, look for APIs
if (win.API_1484_11) { //SCORM 2004-specific API.
scorm.version = "2004"; //Set version
API = win.API_1484_11;
} else if (win.API) { //SCORM 1.2-specific API
scorm.version = "1.2"; //Set version
API = win.API;
}
}
if (API) {
trace(traceMsgPrefix + ": API found. Version: " + scorm.version);
trace("API: " + API);
} else {
trace(traceMsgPrefix + ": Error finding API. \nFind attempts: " + findAttempts + ". \nFind attempt limit: " + findAttemptLimit);
}
return API;
};
/* -------------------------------------------------------------------------
pipwerks.SCORM.API.get()
Looks for an object named API, first in the current window's frame
hierarchy and then, if necessary, in the current window's opener window
hierarchy (if there is an opener window).
Parameters: None.
Returns: Object if API found, null if no API found
---------------------------------------------------------------------------- */
pipwerks.SCORM.API.get = function() {
var API = null,
win = window,
scorm = pipwerks.SCORM,
find = scorm.API.find,
trace = pipwerks.UTILS.trace;
API = find(win);
if (!API && win.parent && win.parent != win) {
API = find(win.parent);
}
if (!API && win.top && win.top.opener) {
API = find(win.top.opener);
}
//Special handling for Plateau
//Thanks to Joseph Venditti for the patch
if (!API && win.top && win.top.opener && win.top.opener.document) {
API = find(win.top.opener.document);
}
if (API) {
scorm.API.isFound = true;
} else {
trace("API.get failed: Can't find the API!");
}
return API;
};
/* -------------------------------------------------------------------------
pipwerks.SCORM.API.getHandle()
Returns the handle to API object if it was previously set
Parameters: None.
Returns: Object (the pipwerks.SCORM.API.handle variable).
---------------------------------------------------------------------------- */
pipwerks.SCORM.API.getHandle = function() {
var API = pipwerks.SCORM.API;
if (!API.handle && !API.isFound) {
API.handle = API.get();
}
return API.handle;
};
// ------------------------------------------------------------------------- //
// --- pipwerks.SCORM.connection functions --------------------------------- //
// ------------------------------------------------------------------------- //
/* -------------------------------------------------------------------------
pipwerks.SCORM.connection.initialize()
Tells the LMS to initiate the communication session.
Parameters: None
Returns: Boolean
---------------------------------------------------------------------------- */
pipwerks.SCORM.connection.initialize = function() {
var success = false,
scorm = pipwerks.SCORM,
completionStatus = scorm.data.completionStatus,
trace = pipwerks.UTILS.trace,
makeBoolean = pipwerks.UTILS.StringToBoolean,
debug = scorm.debug,
traceMsgPrefix = "SCORM.connection.initialize ";
trace("connection.initialize called.");
if (!scorm.connection.isActive) {
var API = scorm.API.getHandle(),
errorCode = 0;
if (API) {
switch (scorm.version) {
case "1.2":
success = makeBoolean(API.LMSInitialize(""));
break;
case "2004":
success = makeBoolean(API.Initialize(""));
break;
}
if (success) {
//Double-check that connection is active and working before returning 'true' boolean
errorCode = debug.getCode();
if (errorCode !== null && errorCode === 0) {
scorm.connection.isActive = true;
if (scorm.handleCompletionStatus) {
//Automatically set new launches to incomplete
completionStatus = scorm.status("get");
if (completionStatus) {
switch (completionStatus) {
//Both SCORM 1.2 and 2004
case "not attempted":
scorm.status("set", "incomplete");
break;
//SCORM 2004 only
case "unknown":
scorm.status("set", "incomplete");
break;
//Additional options, presented here in case you'd like to use them
//case "completed" : break;
//case "incomplete" : break;
//case "passed" : break; //SCORM 1.2 only
//case "failed" : break; //SCORM 1.2 only
//case "browsed" : break; //SCORM 1.2 only
}
//Commit changes
scorm.save();
}
}
} else {
success = false;
trace(traceMsgPrefix + "failed. \nError code: " + errorCode + " \nError info: " + debug.getInfo(errorCode));
}
} else {
errorCode = debug.getCode();
if (errorCode !== null && errorCode !== 0) {
trace(traceMsgPrefix + "failed. \nError code: " + errorCode + " \nError info: " + debug.getInfo(errorCode));
} else {
trace(traceMsgPrefix + "failed: No response from server.");
}
}
} else {
trace(traceMsgPrefix + "failed: API is null.");
}
} else {
trace(traceMsgPrefix + "aborted: Connection already active.");
}
return success;
};
/* -------------------------------------------------------------------------
pipwerks.SCORM.connection.terminate()
Tells the LMS to terminate the communication session
Parameters: None
Returns: Boolean
---------------------------------------------------------------------------- */
pipwerks.SCORM.connection.terminate = function() {
var success = false,
scorm = pipwerks.SCORM,
exitStatus = scorm.data.exitStatus,
completionStatus = scorm.data.completionStatus,
trace = pipwerks.UTILS.trace,
makeBoolean = pipwerks.UTILS.StringToBoolean,
debug = scorm.debug,
traceMsgPrefix = "SCORM.connection.terminate ";
if (scorm.connection.isActive) {
var API = scorm.API.getHandle(),
errorCode = 0;
if (API) {
if (scorm.handleExitMode && !exitStatus) {
if (completionStatus !== "completed" && completionStatus !== "passed") {
switch (scorm.version) {
case "1.2":
success = scorm.set("cmi.core.exit", "suspend");
break;
case "2004":
success = scorm.set("cmi.exit", "suspend");
break;
}
} else {
switch (scorm.version) {
case "1.2":
success = scorm.set("cmi.core.exit", "logout");
break;
case "2004":
success = scorm.set("cmi.exit", "normal");
break;
}
}
}
//Ensure we persist the data for 1.2 - not required for 2004 where an implicit commit is applied during the Terminate
success = (scorm.version === "1.2") ? scorm.save() : true;
if (success) {
switch (scorm.version) {
case "1.2":
success = makeBoolean(API.LMSFinish(""));
break;
case "2004":
success = makeBoolean(API.Terminate(""));
break;
}
if (success) {
scorm.connection.isActive = false;
} else {
errorCode = debug.getCode();
trace(traceMsgPrefix + "failed. \nError code: " + errorCode + " \nError info: " + debug.getInfo(errorCode));
}
}
} else {
trace(traceMsgPrefix + "failed: API is null.");
}
} else {
trace(traceMsgPrefix + "aborted: Connection already terminated.");
}
return success;
};
// ------------------------------------------------------------------------- //
// --- pipwerks.SCORM.data functions --------------------------------------- //
// ------------------------------------------------------------------------- //
/* -------------------------------------------------------------------------
pipwerks.SCORM.data.get(parameter)
Requests information from the LMS.
Parameter: parameter (string, name of the SCORM data model element)
Returns: string (the value of the specified data model element)
---------------------------------------------------------------------------- */
pipwerks.SCORM.data.get = function(parameter) {
var value = null,
scorm = pipwerks.SCORM,
trace = pipwerks.UTILS.trace,
debug = scorm.debug,
traceMsgPrefix = "SCORM.data.get('" + parameter + "') ";
if (scorm.connection.isActive) {
var API = scorm.API.getHandle(),
errorCode = 0;
if (API) {
switch (scorm.version) {
case "1.2":
value = API.LMSGetValue(parameter);
break;
case "2004":
value = API.GetValue(parameter);
break;
}
errorCode = debug.getCode();
//GetValue returns an empty string on errors
//If value is an empty string, check errorCode to make sure there are no errors
if (value !== "" || errorCode === 0) {
//GetValue is successful.
//If parameter is lesson_status/completion_status or exit status, let's
//grab the value and cache it so we can check it during connection.terminate()
switch (parameter) {
case "cmi.core.lesson_status":
case "cmi.completion_status":
scorm.data.completionStatus = value;
break;
case "cmi.core.exit":
case "cmi.exit":
scorm.data.exitStatus = value;
break;
}
} else {
trace(traceMsgPrefix + "failed. \nError code: " + errorCode + "\nError info: " + debug.getInfo(errorCode));
}
} else {
trace(traceMsgPrefix + "failed: API is null.");
}
} else {
trace(traceMsgPrefix + "failed: API connection is inactive.");
}
trace(traceMsgPrefix + " value: " + value);
return String(value);
};
/* -------------------------------------------------------------------------
pipwerks.SCORM.data.set()
Tells the LMS to assign the value to the named data model element.
Also stores the SCO's completion status in a variable named
pipwerks.SCORM.data.completionStatus. This variable is checked whenever
pipwerks.SCORM.connection.terminate() is invoked.
Parameters: parameter (string). The data model element
value (string). The value for the data model element
Returns: Boolean
---------------------------------------------------------------------------- */
pipwerks.SCORM.data.set = function(parameter, value) {
var success = false,
scorm = pipwerks.SCORM,
trace = pipwerks.UTILS.trace,
makeBoolean = pipwerks.UTILS.StringToBoolean,
debug = scorm.debug,
traceMsgPrefix = "SCORM.data.set('" + parameter + "') ";
if (scorm.connection.isActive) {
var API = scorm.API.getHandle(),
errorCode = 0;
if (API) {
switch (scorm.version) {
case "1.2":
success = makeBoolean(API.LMSSetValue(parameter, value));
break;
case "2004":
success = makeBoolean(API.SetValue(parameter, value));
break;
}
if (success) {
if (parameter === "cmi.core.lesson_status" || parameter === "cmi.completion_status") {
scorm.data.completionStatus = value;
}
} else {
errorCode = debug.getCode();
trace(traceMsgPrefix + "failed. \nError code: " + errorCode + ". \nError info: " + debug.getInfo(errorCode));
}
} else {
trace(traceMsgPrefix + "failed: API is null.");
}
} else {
trace(traceMsgPrefix + "failed: API connection is inactive.");
}
trace(traceMsgPrefix + " value: " + value);
return success;
};
/* -------------------------------------------------------------------------
pipwerks.SCORM.data.save()
Instructs the LMS to persist all data to this point in the session
Parameters: None
Returns: Boolean
---------------------------------------------------------------------------- */
pipwerks.SCORM.data.save = function() {
var success = false,
scorm = pipwerks.SCORM,
trace = pipwerks.UTILS.trace,
makeBoolean = pipwerks.UTILS.StringToBoolean,
traceMsgPrefix = "SCORM.data.save failed";
if (scorm.connection.isActive) {
var API = scorm.API.getHandle();
if (API) {
switch (scorm.version) {
case "1.2":
success = makeBoolean(API.LMSCommit(""));
break;
case "2004":
success = makeBoolean(API.Commit(""));
break;
}
} else {
trace(traceMsgPrefix + ": API is null.");
}
} else {
trace(traceMsgPrefix + ": API connection is inactive.");
}
return success;
};
pipwerks.SCORM.status = function(action, status) {
var success = false,
scorm = pipwerks.SCORM,
trace = pipwerks.UTILS.trace,
traceMsgPrefix = "SCORM.getStatus failed",
cmi = "";
if (action !== null) {
switch (scorm.version) {
case "1.2":
cmi = "cmi.core.lesson_status";
break;
case "2004":
cmi = "cmi.completion_status";
break;
}
switch (action) {
case "get":
success = scorm.data.get(cmi);
break;
case "set":
if (status !== null) {
success = scorm.data.set(cmi, status);
} else {
success = false;
trace(traceMsgPrefix + ": status was not specified.");
}
break;
default:
success = false;
trace(traceMsgPrefix + ": no valid action was specified.");
}
} else {
trace(traceMsgPrefix + ": action was not specified.");
}
return success;
};
// ------------------------------------------------------------------------- //
// --- pipwerks.SCORM.debug functions -------------------------------------- //
// ------------------------------------------------------------------------- //
/* -------------------------------------------------------------------------
pipwerks.SCORM.debug.getCode
Requests the error code for the current error state from the LMS
Parameters: None
Returns: Integer (the last error code).
---------------------------------------------------------------------------- */
pipwerks.SCORM.debug.getCode = function() {
var scorm = pipwerks.SCORM,
API = scorm.API.getHandle(),
trace = pipwerks.UTILS.trace,
code = 0;
if (API) {
switch (scorm.version) {
case "1.2":
code = parseInt(API.LMSGetLastError(), 10);
break;
case "2004":
code = parseInt(API.GetLastError(), 10);
break;
}
} else {
trace("SCORM.debug.getCode failed: API is null.");
}
return code;
};
/* -------------------------------------------------------------------------
pipwerks.SCORM.debug.getInfo()
"Used by a SCO to request the textual description for the error code
specified by the value of [errorCode]."
Parameters: errorCode (integer).
Returns: String.
----------------------------------------------------------------------------- */
pipwerks.SCORM.debug.getInfo = function(errorCode) {
var scorm = pipwerks.SCORM,
API = scorm.API.getHandle(),
trace = pipwerks.UTILS.trace,
result = "";
if (API) {
switch (scorm.version) {
case "1.2":
result = API.LMSGetErrorString(errorCode.toString());
break;
case "2004":
result = API.GetErrorString(errorCode.toString());
break;
}
} else {
trace("SCORM.debug.getInfo failed: API is null.");
}
return String(result);
};
/* -------------------------------------------------------------------------
pipwerks.SCORM.debug.getDiagnosticInfo
"Exists for LMS specific use. It allows the LMS to define additional
diagnostic information through the API Instance."
Parameters: errorCode (integer).
Returns: String (Additional diagnostic information about the given error code).
---------------------------------------------------------------------------- */
pipwerks.SCORM.debug.getDiagnosticInfo = function(errorCode) {
var scorm = pipwerks.SCORM,
API = scorm.API.getHandle(),
trace = pipwerks.UTILS.trace,
result = "";
if (API) {
switch (scorm.version) {
case "1.2":
result = API.LMSGetDiagnostic(errorCode);
break;
case "2004":
result = API.GetDiagnostic(errorCode);
break;
}
} else {
trace("SCORM.debug.getDiagnosticInfo failed: API is null.");
}
return String(result);
};
// ------------------------------------------------------------------------- //
// --- Shortcuts! ---------------------------------------------------------- //
// ------------------------------------------------------------------------- //
// Because nobody likes typing verbose code.
pipwerks.SCORM.init = pipwerks.SCORM.connection.initialize;
pipwerks.SCORM.get = pipwerks.SCORM.data.get;
pipwerks.SCORM.set = pipwerks.SCORM.data.set;
pipwerks.SCORM.save = pipwerks.SCORM.data.save;
pipwerks.SCORM.quit = pipwerks.SCORM.connection.terminate;
// ------------------------------------------------------------------------- //
// --- pipwerks.UTILS functions -------------------------------------------- //
// ------------------------------------------------------------------------- //
/* -------------------------------------------------------------------------
pipwerks.UTILS.StringToBoolean()
Converts 'boolean strings' into actual valid booleans.
(Most values returned from the API are the strings "true" and "false".)
Parameters: String
Returns: Boolean
---------------------------------------------------------------------------- */
pipwerks.UTILS.StringToBoolean = function(value) {
var t = typeof value;
switch (t) {
//typeof new String("true") === "object", so handle objects as string via fall-through.
//See https://github.com/pipwerks/scorm-api-wrapper/issues/3
case "object":
case "string":
return (/(true|1)/i).test(value);
case "number":
return !!value;
case "boolean":
return value;
case "undefined":
return null;
default:
return false;
}
};
/* -------------------------------------------------------------------------
pipwerks.UTILS.trace()
Displays error messages when in debug mode.
Parameters: msg (string)
Return: None
---------------------------------------------------------------------------- */
pipwerks.UTILS.trace = function(msg) {
if (pipwerks.debug.isActive) {
if (window.console && window.console.log) {
window.console.log(msg);
} else {
//alert(msg);
}
}
};
return pipwerks;
}));