-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathadapt-offlineStorage-scorm.js
248 lines (215 loc) · 7.57 KB
/
adapt-offlineStorage-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
import Adapt from 'core/js/adapt';
import ScormWrapper from './scorm/wrapper';
import SCORMSuspendData from './serializers/SCORMSuspendData';
import offlineStorage from 'core/js/offlineStorage';
/**
* SCORM handler for offlineStorage interface.
*/
export default class OfflineStorageScorm extends Backbone.Controller {
initialize(statefulSession) {
this.offlineStorage = offlineStorage;
this.scorm = ScormWrapper.getInstance();
this.statefulSession = statefulSession;
this.temporaryStore = {};
this.suspendDataStore = {};
this.suspendDataRestored = false;
offlineStorage.initialize(this);
}
save() {
this.statefulSession.saveSessionState();
this.scorm.commit();
}
serialize(...args) {
return SCORMSuspendData.serialize(...args);
}
deserialize(...args) {
return SCORMSuspendData.deserialize(...args);
}
get(name) {
if (name === undefined) {
// If not connected return just temporary store.
if (this.useTemporaryStore()) return this.temporaryStore;
// Get all values as a combined object
this.suspendDataStore = this.getCustomStates();
const data = Object.assign(_.clone(this.suspendDataStore), {
location: this.scorm.getLessonLocation(),
score: this.scorm.getScore(),
status: this.scorm.getStatus(),
student: this.scorm.getStudentName(),
learnerInfo: this.getLearnerInfo()
});
this.suspendDataRestored = true;
return data;
}
// If not connected return just temporary store value.
if (this.useTemporaryStore()) return this.temporaryStore[name];
// Get by name
let courseState;
switch (name.toLowerCase()) {
case 'location':
return this.scorm.getLessonLocation();
case 'score':
return this.scorm.getScore();
case 'status':
return this.scorm.getStatus();
case 'student':
// for backwards-compatibility. learnerInfo is preferred now and will
// give you more information
return this.scorm.getStudentName();
case 'learnerinfo':
return this.getLearnerInfo();
case 'coursestate': {
courseState = this.getCustomState('c');
const stateArray = (courseState && SCORMSuspendData.deserialize(courseState)) || [];
return {
_isCourseComplete: Boolean(stateArray.slice(0, 1).map(Number)[0]),
_isAssessmentPassed: Boolean(stateArray.slice(1, 2).map(Number)[0]),
completion: stateArray.slice(2).map(Number).map(String).join('') || ''
};
}
case 'completion':
courseState = this.getCustomState('c');
return (courseState && SCORMSuspendData
.deserialize(courseState)
.slice(2)
.map(Number)
.map(String)
.join('')) || '';
case '_iscoursecomplete':
courseState = this.getCustomState('c');
return Boolean(courseState && SCORMSuspendData
.deserialize(courseState)
.slice(0, 1)
.map(Number)[0]);
case '_isassessmentpassed':
courseState = this.getCustomState('c');
return Boolean(courseState && SCORMSuspendData
.deserialize(courseState)
.slice(1, 2)
.map(Number)[0]);
case 'questions': {
const questionsState = this.getCustomState('q');
return questionsState || '';
}
default:
return this.getCustomState(name);
}
}
set(name, value) {
// Convert arguments to array and drop the 'name' parameter
const args = [...arguments].slice(1);
const isObject = typeof name === 'object';
if (isObject) {
value = name;
name = 'suspendData';
}
if (this.useTemporaryStore()) {
if (isObject) {
Object.assign(this.temporaryStore, value);
} else {
this.temporaryStore[name] = value;
}
return true;
}
switch (name.toLowerCase()) {
case 'interaction':
return this.scorm.recordInteraction(...args);
case 'objectivedescription':
return this.scorm.recordObjectiveDescription(...args);
case 'objectivestatus':
return this.scorm.recordObjectiveStatus(...args);
case 'objectivescore':
return this.scorm.recordObjectiveScore(...args);
case 'location':
return this.scorm.setLessonLocation(...args);
case 'score':
return this.scorm.setScore(...args);
case 'status':
return this.scorm.setStatus(...args);
case 'student':
case 'learnerinfo':
return false;// these properties are read-only
case 'lang':
this.scorm.setLanguage(value);
// fall-through so that lang gets stored in suspend_data as well:
// because in SCORM 1.2 cmi.student_preference.language is an optional
// data element so we can't rely on the LMS having support for it.
// If it does support it we may as well save the user's choice there
// purely for reporting purposes
break;
case 'suspenddata':
break;
}
if (isObject) {
Object.assign(this.suspendDataStore, value);
} else {
this.suspendDataStore[name] = value;
}
const dataAsString = JSON.stringify(this.suspendDataStore);
return (this.suspendDataRestored) ? this.scorm.setSuspendData(dataAsString) : false;
}
clear() {
this.temporaryStore = {};
this.suspendDataStore = {};
const dataAsString = JSON.stringify(this.suspendDataStore);
this.scorm.setSuspendData(dataAsString);
}
getCustomStates() {
const isSuspendDataStoreEmpty = _.isEmpty(this.suspendDataStore);
if (!isSuspendDataStoreEmpty && this.suspendDataRestored) {
return _.clone(this.suspendDataStore);
}
const dataAsString = this.scorm.getSuspendData();
if (dataAsString === '' || dataAsString === ' ' || dataAsString === undefined) {
return {};
}
const dataAsJSON = JSON.parse(dataAsString);
if (!isSuspendDataStoreEmpty && !this.suspendDataRestored) {
Object.assign(dataAsJSON, this.suspendDataStore);
}
return dataAsJSON;
}
getCustomState(name) {
const dataAsJSON = this.getCustomStates();
return dataAsJSON[name];
}
useTemporaryStore() {
const cfg = Adapt.config.get('_spoor');
if (!this.scorm.lmsConnected || (cfg?._isEnabled === false)) return true;
return false;
}
/**
* Returns an object with the properties:
* - id (cmi.core.student_id)
* - name (cmi.core.student_name - which is usually in the format
* 'Lastname, Firstname' or 'Firstname Lastname' - but it sometimes doesn't
* have the space after the comma
* )
* - firstname
* - lastname
*/
getLearnerInfo() {
const id = this.scorm.getStudentId();
let name = this.scorm.getStudentName();
let firstname = '';
let lastname = '';
let hasName = (name && name !== 'undefined');
const isNameCommaSeparated = hasName && name.includes(',');
const isNameSpaceSeparated = hasName && name.includes(' ');
// Name must have either a comma or a space
hasName = hasName && (isNameCommaSeparated || isNameSpaceSeparated);
if (!hasName) {
console.log('SPOOR: LMS learner_name not in \'lastname, firstname\' or \'firstname lastname\' format');
return { id, name, firstname, lastname };
}
const separator = isNameCommaSeparated ? ',' : ' ';
const nameParts = name.split(separator);
if (isNameCommaSeparated) {
// Assume lastname appears before the comma
nameParts.reverse();
}
[ firstname, lastname ] = nameParts.map(part => part.trim());
name = `${firstname} ${lastname}`;
return { id, name, firstname, lastname };
}
}