forked from greenpeace/gpes-html5-scorm-presentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscormfunctions.js
183 lines (138 loc) · 5.28 KB
/
scormfunctions.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
/*
Source code created by Rustici Software, LLC is licensed under a
Creative Commons Attribution 3.0 United States License
(http://creativecommons.org/licenses/by/3.0/us/)
Want to make SCORM easy? See our solutions at http://www.scorm.com.
This example provides for the bare minimum SCORM run-time calls.
It will demonstrate using the API discovery algorithm to find the
SCORM API and then calling Initialize and Terminate when the page
loads and unloads respectively. This example also demonstrates
some basic SCORM error handling.
*/
//Include the standard ADL-supplied API discovery algorithm
///////////////////////////////////////////
//Begin ADL-provided API discovery algorithm
///////////////////////////////////////////
var findAPITries = 0;
function findAPI(win)
{
// Check to see if the window (win) contains the API
// if the window (win) does not contain the API and
// the window (win) has a parent window and the parent window
// is not the same as the window (win)
while ( (win.API == null) &&
(win.parent != null) &&
(win.parent != win) )
{
// increment the number of findAPITries
findAPITries++;
// Note: 7 is an arbitrary number, but should be more than sufficient
if (findAPITries > 7)
{
alert("Error finding API -- too deeply nested.");
return null;
}
// set the variable that represents the window being
// being searched to be the parent of the current window
// then search for the API again
win = win.parent;
}
return win.API;
}
function getAPI()
{
// start by looking for the API in the current window
var theAPI = findAPI(window);
// if the API is null (could not be found in the current window)
// and the current window has an opener window
if ( (theAPI == null) &&
(window.opener != null) &&
(typeof(window.opener) != "undefined") )
{
// try to find the API in the current window’s opener
theAPI = findAPI(window.opener);
}
// if the API has not been found
if (theAPI == null)
{
// Alert the user that the API Adapter could not be found
alert("Unable to find an API adapter");
}
return theAPI;
}
function setValue(n, v)
{
var s = API.LMSSetValue(n, v);
return s;
}
function getValue(n)
{
var s = API.LMSGetValue(n);
return s;
}
///////////////////////////////////////////
//End ADL-provided API discovery algorithm
///////////////////////////////////////////
//Create function handlers for the loading and unloading of the page
//Constants
var SCORM_TRUE = "true";
var SCORM_FALSE = "false";
//Since the Unload handler will be called twice, from both the onunload
//and onbeforeunload events, ensure that we only call LMSFinish once.
var finishCalled = false;
//Track whether or not we successfully initialized.
var initialized = false;
var API = null;
var initialTime = new Date();
function ScormProcessInitialize(){
var result;
API = getAPI();
if (API == null){
alert("ERROR - Could not establish a connection with the LMS.\n\nYour results may not be recorded.");
return;
}
result = API.LMSInitialize("");
if (result == SCORM_FALSE){
var errorNumber = API.LMSGetLastError();
var errorString = API.LMSGetErrorString(errorNumber);
var diagnostic = API.LMSGetDiagnostic(errorNumber);
var errorDescription = "Number: " + errorNumber + "\nDescription: " + errorString + "\nDiagnostic: " + diagnostic;
alert("Error - Could not initialize communication with the LMS.\n\nYour results may not be recorded.\n\n" + errorDescription);
return;
}
initialized = true;
}
function ScormProcessFinish(){
var result;
var finalTime = new Date();
var completedTimeSecs = ( finalTime.getTime() - initialTime.getTime() ) / 1000 ;
//Don't terminate if we haven't initialized or if we've already terminated
if (initialized == false || finishCalled == true){return;}
if (typeof(pageScore) === "number") {
setValue("cmi.core.score.raw", pageScore);
}
if ( typeof(minCompletedTime === "number") && completedTimeSecs < minCompletedTime && getValue("cmi.core.lesson_status") !== "completed" ) {
setValue("cmi.core.lesson_status", "incomplete");
} else {
setValue("cmi.core.lesson_status", "completed");
}
result = API.LMSFinish("");
finishCalled = true;
if (result == SCORM_FALSE){
var errorNumber = API.LMSGetLastError();
var errorString = API.LMSGetErrorString(errorNumber);
var diagnostic = API.LMSGetDiagnostic(errorNumber);
var errorDescription = "Number: " + errorNumber + "\nDescription: " + errorString + "\nDiagnostic: " + diagnostic;
alert("Error - Could not terminate communication with the LMS.\n\nYour results may not be recorded.\n\n" + errorDescription);
return;
}
}
/*
Assign the processing functions to the page's load and unload
events. The onbeforeunload event is included because it can be
more reliable than the onunload event and we want to make sure
that Terminate is ALWAYS called.
*/
window.onload = ScormProcessInitialize;
window.onunload = ScormProcessFinish;
window.onbeforeunload = ScormProcessFinish;