forked from h5p/h5p-branching-question
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranchingQuestion.js
407 lines (344 loc) · 14.2 KB
/
branchingQuestion.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
H5P.BranchingQuestion = (function () {
function BranchingQuestion(parameters) {
var self = this;
H5P.EventDispatcher.call(self);
this.container = null;
let answered;
let timestamp;
/**
* Get closest ancestor of DOM element that matches selector.
*
* Mimics Element.closest(), workaround for IE11.
*
* @param {Element} element DOM element.
* @param {string} selector CSS selector.
* @return {Element|null} Element, if found. Else null.
*/
const getClosestParent = function (element, selector) {
if (!document.documentElement.contains(element)) {
return null;
}
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
}
do {
if (element.matches(selector)) {
return element;
}
element = element.parentElement || element.parentNode;
}
while (element !== null && element.nodeType === 1);
return null;
};
var createWrapper = function () {
var wrapper = document.createElement('div');
wrapper.classList.add('h5p-branching-question');
var icon = document.createElement('img');
icon.classList.add('h5p-branching-question-icon');
icon.src = self.getLibraryFilePath('branching-question-icon.svg');
wrapper.appendChild(icon);
return wrapper;
};
var appendMultiChoiceSection = function (parameters, wrapper) {
var questionWrapper = document.createElement('div');
questionWrapper.classList.add('h5p-multichoice-wrapper');
var title = document.createElement('div');
title.classList.add('h5p-branching-question-title');
if (parameters.branchingQuestion.question) {
title.innerHTML = parameters.branchingQuestion.question;
}
questionWrapper.appendChild(title);
const alternatives = parameters.branchingQuestion.alternatives || [] ;
alternatives.forEach(function (altParams, index, array) {
const alternative = createAlternativeContainer(altParams.text, index);
alternative.nextContentId = altParams.nextContentId;
// Create feedback screen if it exists
const hasFeedback = altParams.feedback && !!(
altParams.feedback.title && altParams.feedback.title.trim() ||
altParams.feedback.subtitle && altParams.feedback.subtitle.trim() ||
altParams.feedback.image
);
if (hasFeedback && altParams.nextContentId !== -1) {
alternative.feedbackScreen = createFeedbackScreen(
altParams.feedback,
alternative.nextContentId,
index
);
alternative.proceedButton = alternative.feedbackScreen.querySelectorAll('button')[0];
}
alternative.hasFeedback = altParams.feedback && !!(hasFeedback || altParams.feedback.endScreenScore !== undefined);
alternative.feedback = altParams.feedback;
alternative.addEventListener('keyup', function (event) {
if (event.which === 13 || event.which === 32) {
this.click();
}
});
alternative.onclick = function (e) {
if (this.feedbackScreen !== undefined) {
if (self.container) {
self.container.classList.add('h5p-branching-scenario-feedback-dialog');
}
wrapper.innerHTML = '';
wrapper.appendChild(this.feedbackScreen);
self.parent.trigger('resize');
answered = index;
timestamp = new Date().toISOString();
const container = document.querySelector('.h5p-branching-question-container');
if (container.hasAttribute('role')) {
container.removeAttribute('role');
container.removeAttribute('aria-labelledby');
}
this.feedbackScreen.setAttribute('role', 'dialog');
this.feedbackScreen.setAttribute('aria-labelledby', 'h5p-feedback-content-title');
this.feedbackScreen.setAttribute('aria-describedby', 'h5p-feedback-content-content');
this.proceedButton.focus();
self.triggerXAPI('interacted');
}
else {
var currentAlt = e.target.classList.contains('.h5p-branching-question-alternative') ?
e.target : getClosestParent(e.target, '.h5p-branching-question-alternative');
var alts = questionWrapper.querySelectorAll('.h5p-branching-question-alternative');
var index2;
for (var i = 0; i < alts.length; i++) {
if (alts[i] === currentAlt) {
index2 = +alts[i].getAttribute('data-id');
break;
}
}
answered = index2;
timestamp = new Date().toISOString();
var nextScreen = {
nextContentId: this.nextContentId,
chosenAlternative: index2,
};
const currentAltParams = parameters.branchingQuestion.alternatives[index2];
const currentAltHasFeedback = !!(currentAltParams.feedback.title
|| currentAltParams.feedback.subtitle
|| currentAltParams.feedback.image
|| currentAltParams.feedback.endScreenScore !== undefined
);
if (index2 >= 0 && currentAltHasFeedback) {
nextScreen.feedback = currentAltParams.feedback;
}
self.trigger('navigated', nextScreen);
}
};
questionWrapper.appendChild(alternative);
});
if (parameters.branchingQuestion.randomize && !questionWrapper.dataset.shuffled) {
const alternatives = questionWrapper.querySelectorAll('button.h5p-branching-question-alternative');
const shuffledAlternatives = H5P.shuffleArray(Array.from(alternatives));
// Reorder the alternatives according to shuffledAlternatives
shuffledAlternatives.forEach(function (alternative) {
questionWrapper.appendChild(alternative);
});
// Prevent shuffling more than once
questionWrapper.setAttribute('data-shuffled', true);
}
wrapper.appendChild(questionWrapper);
return wrapper;
};
var createAlternativeContainer = function (text, id) {
var wrapper = document.createElement('button');
wrapper.classList.add('h5p-branching-question-alternative');
wrapper.tabIndex = 0;
wrapper.setAttribute('data-id', id);
var alternativeText = document.createElement('p');
alternativeText.innerHTML = text;
wrapper.appendChild(alternativeText);
return wrapper;
};
var createFeedbackScreen = function (feedback, nextContentId, chosenAlternativeIndex) {
var wrapper = document.createElement('div');
wrapper.classList.add('h5p-branching-question');
wrapper.classList.add(feedback.image !== undefined ? 'h5p-feedback-has-image' : 'h5p-feedback-default');
if (feedback.image !== undefined && feedback.image.path !== undefined) {
var imageContainer = document.createElement('div');
imageContainer.classList.add('h5p-branching-question');
imageContainer.classList.add('h5p-feedback-image');
var image = document.createElement('img');
image.src = H5P.getPath(feedback.image.path, self.contentId);
imageContainer.appendChild(image);
wrapper.appendChild(imageContainer);
}
var feedbackContent = document.createElement('div');
feedbackContent.classList.add('h5p-branching-question');
feedbackContent.classList.add('h5p-feedback-content');
var feedbackText = document.createElement('div');
feedbackText.classList.add('h5p-feedback-content-content');
feedbackContent.appendChild(feedbackText);
var title = document.createElement('h1');
title.innerHTML = feedback.title || '';
title.id = 'h5p-feedback-content-title';
feedbackText.appendChild(title);
if (feedback.subtitle) {
var subtitle = document.createElement('div');
subtitle.id = 'h5p-feedback-content-content';
subtitle.innerHTML = feedback.subtitle || '';
feedbackText.appendChild(subtitle);
}
var navButton = document.createElement('button');
navButton.onclick = function () {
self.trigger('navigated', {
nextContentId: nextContentId,
chosenAlternative: chosenAlternativeIndex
});
};
var text = document.createTextNode(parameters.proceedButtonText);
navButton.appendChild(text);
feedbackContent.appendChild(navButton);
wrapper.appendChild(feedbackContent);
return wrapper;
};
/**
* Get xAPI data.
* Contract used by report rendering engine.
*
* @see contract at {@link https://h5p.org/documentation/developers/contracts#guides-header-6}
*/
self.getXAPIData = function () {
var xAPIEvent = this.createXAPIEventTemplate('answered');
addQuestionToXAPI(xAPIEvent);
xAPIEvent.setScoredResult(undefined, undefined, self, true);
xAPIEvent.data.statement.result.response = answered;
xAPIEvent.data.statement.timestamp = timestamp;
return {
statement: xAPIEvent.data.statement
};
};
/**
* If the chosen answer contains feedback data, adds an extension to the
* provided extensions object, so that it can be included in reports.
*
* @param {object} extensions Existing object to use
*/
var addFeedbackInfoExtension = function (extensions) {
const alternatives = parameters.branchingQuestion.alternatives;
const chosen = alternatives[answered];
const feedback = chosen.feedback;
if (!feedback.title && !feedback.subtitle && !feedback.image) {
return; // Nothing to add
}
const xapiFeedback = {};
const converter = document.createElement('div');
if (feedback.image) {
xapiFeedback.imageUrl = H5P.getPath(
feedback.image.path,
self.parent.contentId
);
}
if (feedback.title) {
converter.innerHTML = feedback.title;
xapiFeedback.title = converter.innerText.trim();
}
if (feedback.subtitle) {
converter.innerHTML = feedback.subtitle;
xapiFeedback.subtitle = converter.innerText.trim();
}
const key = 'https://h5p.org/x-api/branching-choice-feedback';
extensions[key] = xapiFeedback;
};
/**
* Determine whether the Branching Scenario is using dynamic score.
*
* @return {boolean}
*/
var contentIsUsingDynamicScore = function () {
return (
self.parent &&
self.parent.params &&
self.parent.params.scoringOptionGroup &&
self.parent.params.scoringOptionGroup.scoringOption === 'dynamic-score'
);
};
/**
* If applicable, adds scoring and correctness information to the xAPI
* statement for use in reports.
*
* @param {object} definition xAPI object definition
* @param {array} alternatives Available branching choices
*/
var addScoringAndCorrectness = function (definition, alternatives) {
// Only include scoring and correctness data for dynamic score option
if (!contentIsUsingDynamicScore()) {
return;
}
// Track each possible score and the alternatives that award it
const scoreMap = new Map();
for (let i = 0; i < alternatives.length; i++) {
const currentScore = alternatives[i].feedback.endScreenScore;
if (typeof currentScore === 'number' && currentScore > 0) {
if (scoreMap.has(currentScore)) {
scoreMap.get(currentScore).push(i);
}
else {
scoreMap.set(currentScore, [i]);
}
}
}
if (scoreMap.size > 0) {
// All alternatives that give the max score are considered correct
// See https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#correct-responses-pattern
const maxScore = Math.max(...scoreMap.keys());
definition.correctResponsesPattern = scoreMap.get(maxScore);
// Use an extension in order to provide the points awarded by each alternative
const extensionKey = 'https://h5p.org/x-api/alternatives-with-score';
definition.extensions[extensionKey] = {};
scoreMap.forEach((alternatives, score) => {
alternatives.forEach(alternative => {
definition.extensions[extensionKey][alternative] = score;
});
});
// Remove extension that indicates there is no correct answer
delete definition.extensions['https://h5p.org/x-api/no-correct-answer'];
}
};
/**
* Add the question to the given xAPIEvent
*
* @param {H5P.XAPIEvent} xAPIEvent
*/
var addQuestionToXAPI = function (xAPIEvent) {
const converter = document.createElement('div');
var definition = xAPIEvent.getVerifiedStatementValue(['object', 'definition']);
converter.innerHTML = parameters.branchingQuestion.question;
definition.description = {
'en-US': converter.innerText
};
definition.type = 'http://adlnet.gov/expapi/activities/cmi.interaction';
definition.interactionType = 'choice';
definition.correctResponsesPattern = [];
definition.choices = [];
definition.extensions = {
'https://h5p.org/x-api/no-correct-answer': 1
};
const alternatives = parameters.branchingQuestion.alternatives;
for (let i = 0; i < alternatives.length; i++) {
converter.innerHTML = alternatives[i].text;
definition.choices[i] = {
'id': i + '',
'description': {
'en-US': converter.innerText
}
};
}
addScoringAndCorrectness(definition, alternatives);
if (answered !== undefined) {
addFeedbackInfoExtension(definition.extensions);
}
};
/**
* TODO
*/
self.attach = function ($container) {
var questionContainer = document.createElement('div');
questionContainer.classList.add('h5p-branching-question-container');
var branchingQuestion = createWrapper(parameters);
branchingQuestion = appendMultiChoiceSection(parameters, branchingQuestion);
questionContainer.appendChild(branchingQuestion);
$container.append(questionContainer);
this.container = $container[0];
};
}
return BranchingQuestion;
})();