This repository has been archived by the owner on Mar 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
251 lines (215 loc) · 6.78 KB
/
api.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
/** A SCORM API.
* It provides the `window.API_1484_11` object, which SCORM packages use to interact with the data model.
*
* @param {object} data - A dictionary of the SCORM data model
* @param {number} attempt_pk - the id of the associated attempt in the database
* @param {string} fallback_url - URL of the AJAX fallback endpoint
*/
function SCORM_API(options) {
var data = options.scorm_cmi;
var sc = this;
this.callbacks = new CallbackHandler();
this.attempt_pk = options.attempt_pk;
this.fallback_url = options.fallback_url;
this.show_attempts_url = options.show_attempts_url;
this.allow_review_from = load_date(options.allow_review_from);
this.available_until = load_date(options.available_until);
/** A dictionary of batches of elements which have been sent, but we haven't received confirmation that the server has saved them.
* Maps batch ids to lists of SCORMElements.
*/
this.sent = {};
this.element_acc = 0;
/** An accumulator for the batch IDs
*/
this.sent_acc = (new Date()).getTime();
this.initialise_data(data);
this.initialise_api();
}
SCORM_API.prototype = {
/** Has the API been initialised?
*/
initialized: false,
/** Has the API been terminated?
*/
terminated: false,
/** The code of the last error that was raised
*/
last_error: 0,
/** Setup the SCORM data model.
* Merge in elements loaded from the page with elements saved to localStorage, taking the most recent value when there's a clash.
*/
initialise_data: function(data) {
// create the data model
this.data = {};
for(var key in data) {
this.data[key] = data[key].value;
}
/** SCORM display mode - 'normal' or 'review'
*/
this.mode = this.data['cmi.mode'];
/** Is the client allowed to change data model elements?
* Not allowed in review mode.
*/
this.allow_set = this.mode=='normal';
// Force review mode from now on if activity is completed - could be out of sync if resuming a session which wasn't saved properly.
if(this.data['cmi.completion_status'] == 'completed') {
this.data['cmi.mode'] = this.mode = 'review';
}
this.callbacks.trigger('initialise_data');
},
/** Initialise the SCORM API and expose it to the SCORM activity
*/
initialise_api: function() {
var sc = this;
/** The API object to expose to the SCORM activity
*/
this.API_1484_11 = {};
['Initialize','Terminate','GetLastError','GetErrorString','GetDiagnostic','GetValue','SetValue','Commit'].forEach(function(fn) {
sc.API_1484_11[fn] = function() {
return sc[fn].apply(sc,arguments);
};
});
/** Counts for the various lists in the data model
*/
this.counts = {
'comments_from_learner': 0,
'comments_from_lms': 0,
'interactions': 0,
'objectives': 0,
}
this.interaction_counts = [];
/** Set the counts based on the existing data model
*/
for(var key in this.data) {
this.check_key_counts_something(key);
}
this.callbacks.trigger('initialise_api');
},
/** Force the exam to end.
*/
end: function() {
this.Terminate('');
this.ended = true;
},
/** For a given data model key, if it belongs to a list, update the counter for that list
*/
check_key_counts_something: function(key) {
var m;
if(m=key.match(/^cmi.(\w+).(\d+)/)) {
var ckey = m[1];
var n = parseInt(m[2]);
this.counts[ckey] = Math.max(n+1, this.counts[ckey]);
this.data['cmi.'+ckey+'._count'] = this.counts[ckey];
if(ckey=='interactions' && this.interaction_counts[n]===undefined) {
this.interaction_counts[n] = {
'objectives': 0,
'correct_responses': 0
}
}
}
if(m=key.match(/^cmi.interactions.(\d+).(objectives|correct_responses).(\d+)/)) {
var n1 = parseInt(m[1]);
var skey = m[2];
var n2 = parseInt(m[3]);
this.interaction_counts[n1][skey] = Math.max(n2+1, this.interaction_counts[n1][skey]);
this.data['cmi.interactions.'+n1+'.'+skey+'._count'] = this.interaction_counts[n1][skey];
}
},
Initialize: function(b) {
this.callbacks.trigger('Initialize',b);
if(b!='' || this.initialized || this.terminated) {
return false;
}
this.initialized = true;
return true;
},
Terminate: function(b) {
this.callbacks.trigger('Terminate',b);
if(b!='' || !this.initialized || this.terminated) {
return false;
}
this.terminated = true;
return true;
},
GetLastError: function() {
return this.last_error;
},
GetErrorString: function(code) {
return "I haven't written any error strings yet.";
},
GetDiagnostic: function(code) {
return "I haven't written any error handling yet.";
},
GetValue: function(key) {
var v = this.data[key];
if(v===undefined) {
return '';
} else {
return v;
}
},
SetValue: function(key,value) {
if(!this.allow_set) {
return;
}
value = (value+'');
var changed = value!=this.data[key];
if(changed) {
this.data[key] = value;
this.check_key_counts_something(key);
}
this.callbacks.trigger('SetValue',key,value,changed);
return true;
},
Commit: function(s) {
this.callbacks.trigger('Commit');
return true;
}
}
function CallbackHandler() {
this.callbacks = {};
}
CallbackHandler.prototype = {
on: function(key,fn) {
if(this.callbacks[key] === undefined) {
this.callbacks[key] = [];
}
this.callbacks[key].push(fn);
},
trigger: function(key) {
if(!this.callbacks[key]) {
return;
}
var args = Array.prototype.slice.call(arguments,1);
this.callbacks[key].forEach(function(fn) {
fn.apply(this,args);
});
}
}
/** A single SCORM data model element, with the time it was set.
*/
function SCORMData(key,value,time,counter) {
this.key = key;
this.value = value;
this.time = time;
this.counter = counter;
}
SCORMData.prototype = {
as_json: function() {
return {
key: this.key,
value: this.value,
time: this.timestamp(),
counter: this.counter
}
},
timestamp: function() {
return this.time.getTime()/1000
}
}
function load_date(date) {
if(date!==null) {
return new Date(date);
}
}
module.exports = SCORM_API;