forked from h5p/h5p-goals-assessment-page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goals-assessment-page.js
433 lines (378 loc) · 12.4 KB
/
goals-assessment-page.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
/*global Mustache*/
var H5P = H5P || {};
/**
* Goals Assessment Page module
* @external {jQuery} $ H5P.jQuery
*/
H5P.GoalsAssessmentPage = (function ($, EventDispatcher) {
"use strict";
// CSS Classes:
var MAIN_CONTAINER = 'h5p-goals-assessment-page';
/**
* Helper for enabling tabbing
* @param {H5P.jQuery} $element
*/
var enableTab = function ($element) {
$element.attr('tabindex', 0);
};
/**
* Helper for disabling tabbing
* @param {H5P.jQuery} $element
*/
var disableTab = function ($element) {
$element.attr('tabindex', -1);
};
/**
* Helper for checking a radio
* @param {H5P.jQuery} $element
*/
var check = function ($element) {
$element.attr('aria-checked', true);
};
/**
* Helper for unchecking a radio
* @param {H5P.jQuery} $element
*/
var uncheck = function ($element) {
$element.attr('aria-checked', false);
};
/**
* Helper for making elements with role radio behave as excpected
* @param {H5P.jQuery} $element
*/
var makeRadiosAccessible = function ($alternatives) {
// Initially, first radio is tabable
enableTab($alternatives.first());
// Handle arrow-keys
$alternatives.on('keydown', function (event) {
var $current = $(this);
var $focusOn;
switch (event.which) {
case 35: // End button
// Go to previous Option
$focusOn = $alternatives.last();
break;
case 36: // Home button
// Go to previous Option
$focusOn = $alternatives.first();
break;
case 37: // Left Arrow
case 38: // Up Arrow
// Go to previous Option
$focusOn = $current.prev();
if ($focusOn.length === 0) {
// Wrap around
$focusOn = $alternatives.last();
}
break;
case 39: // Right Arrow
case 40: // Down Arrow
// Go to next Option
$focusOn = $current.next();
if ($focusOn.length === 0) {
// Wrap around
$focusOn = $alternatives.first();
}
break;
}
if ($focusOn && $focusOn.length === 1) {
disableTab($alternatives);
enableTab($focusOn);
$focusOn.focus();
event.preventDefault();
}
});
};
var goalsAssessmentTemplate =
'<div class="page-header" role="heading" tabindex="-1">' +
' <div class="page-title">{{{title}}}</div>' +
' <button class="page-help-text">{{{helpTextLabel}}}</button>' +
'</div>' +
'<div class="goals-assessment-description">{{{description}}}</div>' +
'<div class="legend" aria-hidden="true">' +
' <span class="legend-header">{{{legendHeader}}}</span>' +
' <ul class="ratings">' +
' <li class="rating low">{{{lowRating}}}</li> ' +
' <li class="rating mid">{{{midRating}}}</li> ' +
' <li class="rating high">{{{highRating}}}</li> ' +
' </ul>' +
'</div>' +
'<div class="goals-assessment-view">' +
' <div class="goals-header">' +
' <span class="goal-name-header">{{{goalHeader}}}</span>' +
' <span class="rating-header">{{{ratingHeader}}}</span>' +
' </div>' +
' <div>' +
' <ul class="goals">' +
' </ul>' +
'</div>';
var goalTemplate =
'<li class="goal">' +
' <span class="goal-name">{{{goalName}}}</span>' +
' <ul role="radiogroup" class="rating-container">' +
' <li role="radio" class="rating low" aria-label="{{{lowRating}}}"></li>' +
' <li role="radio" class="rating mid" aria-label="{{{midRating}}}"></li>' +
' <li role="radio" class="rating high" aria-label="{{{highRating}}}"></li>' +
' </ul>' +
'</li>';
/**
* Initialize module.
* @param {Object} params Behavior settings
* @param {Number} id Content identification
* @returns {Object} GoalsAssessmentPage GoalsAssessmentPage instance
*/
function GoalsAssessmentPage(params, id, extras) {
EventDispatcher.call(this);
this.id = id;
this.extras = extras;
// Set default behavior.
this.params = $.extend({
title: this.getTitle(),
description: '',
lowRating: 'Learned little',
midRating: 'Learned something',
highRating: 'Learned a lot',
noGoalsText: 'You have not chosen any goals yet.',
helpTextLabel: 'Read more',
helpText: 'Help text',
legendHeader: 'Possible ratings:',
goalHeader: 'Goals',
ratingHeader: 'Rating'
}, params);
// Array containing assessment categories,
// makes it easier to extend categories at a later point.
this.assessmentCategories = [
this.params.lowRating,
this.params.midRating,
this.params.highRating
];
this.currentGoals = [];
this.state = {};
this.currentSelection;
}
GoalsAssessmentPage.prototype = Object.create(EventDispatcher.prototype);
GoalsAssessmentPage.prototype.constructor = GoalsAssessmentPage;
/**
* Attach function called by H5P framework to insert H5P content into page.
*
* @param {jQuery} $container The container which will be appended to.
*/
GoalsAssessmentPage.prototype.attach = function ($container) {
this.$inner = $('<div>', {
'class': MAIN_CONTAINER
}).appendTo($container);
this.$inner.append(Mustache.render(goalsAssessmentTemplate, this.params));
this.$goals = $('.goals', this.$inner);
this.$pageTitle = $('.page-header', this.$inner);
this.$helpButton = $('.page-help-text', this.$inner);
this.initHelpTextButton();
};
/**
* Create help text functionality for reading more about the task
*/
GoalsAssessmentPage.prototype.initHelpTextButton = function () {
var self = this;
if (this.params.helpText !== undefined && this.params.helpText.length) {
self.$helpButton.on('click', function () {
self.trigger('open-help-dialog', {
title: self.params.title,
helpText: self.params.helpText
});
});
}
else {
self.$helpButton.remove();
}
};
/**
* Get page title
* @returns {String} page title
*/
GoalsAssessmentPage.prototype.getTitle = function () {
return H5P.createTitle((this.extras && this.extras.metadata && this.extras.metadata.title) ? this.extras.metadata.title : 'Goals Assessment');
};
/**
* Updates internal list of assessment goals
*
* @param {Array} newGoals Array of goals
*/
GoalsAssessmentPage.prototype.updateAssessmentGoals = function (newGoals) {
var self = this;
this.currentGoals = [];
this.$goals.empty();
// Create and place all goals
newGoals.forEach(function (goalsPage) {
goalsPage.forEach(function (goalInstance) {
self.createGoalAssessmentElement(goalInstance);
});
});
self.trigger('resize');
};
/**
* Create goal assessment element from goal instance
* @param {H5P.GoalsPage.GoalInstance} goalInstance Goal instance
*/
GoalsAssessmentPage.prototype.createGoalAssessmentElement = function (goalInstance) {
var self = this;
var goalText = goalInstance.goalText();
// Check if goal is defined
if (!goalText) {
return;
}
self.currentGoals.push(goalInstance);
var $goal = $(Mustache.render(goalTemplate, {
goalName: goalText,
lowRating: self.params.lowRating,
midRating: self.params.midRating,
highRating: self.params.highRating
})).appendTo(self.$goals);
// Setup buttons
var $ratingButtons = $goal.find('[role="radio"]');
makeRadiosAccessible($ratingButtons);
H5P.DocumentationTool.handleButtonClick($ratingButtons, function () {
var $currentElement = $(this);
uncheck($ratingButtons);
check($currentElement);
var selectedCategoryIndex = $currentElement.index();
// Save answer
goalInstance.goalAnswer(selectedCategoryIndex);
goalInstance.setTextualAnswer(self.assessmentCategories[selectedCategoryIndex]);
var xAPIEvent = self.createXAPIEventTemplate('interacted');
self.addQuestionToGoalXAPI(xAPIEvent, goalText);
self.addResponseToGoalXAPI(xAPIEvent, $currentElement.index());
self.trigger(xAPIEvent);
});
// If already checked - update UI
if (goalInstance.goalAnswer() !== -1) {
check($ratingButtons.eq(goalInstance.goalAnswer()));
}
};
/**
* Gets current updated goals
*
* @returns {Object} current goals and assessment categories
*/
GoalsAssessmentPage.prototype.getAssessedGoals = function () {
return {
goals: this.currentGoals,
categories: this.assessmentCategories
};
};
/**
* Sets focus on page
*/
GoalsAssessmentPage.prototype.focus = function () {
this.$pageTitle.focus();
};
/**
* Triggers an 'answered' xAPI event for all inputs
*/
GoalsAssessmentPage.prototype.triggerAnsweredEvents = function () {
var self = this;
this.getAssessedGoals().goals.forEach(function(goal) {
var xAPIEvent = self.createXAPIEventTemplate('answered');
self.addQuestionToGoalXAPI(xAPIEvent, goal.text);
self.addResponseToGoalXAPI(xAPIEvent, goal.answer);
self.trigger(xAPIEvent);
});
};
/**
* Helper function to return all xAPI data
* @returns {Array}
*/
GoalsAssessmentPage.prototype.getXAPIDataFromChildren = function () {
var children = [];
var self = this;
this.getAssessedGoals().goals.forEach(function(goal) {
var xAPIEvent = self.createXAPIEventTemplate('answered');
self.addQuestionToGoalXAPI(xAPIEvent, goal.text);
self.addResponseToGoalXAPI(xAPIEvent, goal.answer);
children.push({
statement: xAPIEvent.data.statement
});
});
return children;
};
/**
* Generate xAPI object definition used in xAPI statements for the entire goals assessment page
* @return {Object}
*/
GoalsAssessmentPage.prototype.getxAPIDefinition = function () {
var definition = {};
var self = this;
definition.interactionType = 'compound';
definition.type = 'http://adlnet.gov/expapi/activities/cmi.interaction';
definition.description = {
'en-US': self.params.title
};
definition.extensions = {
'https://h5p.org/x-api/h5p-machine-name': 'H5P.GoalsAssessmentPage'
};
return definition;
};
/**
* Generate xAPI object definition used in xAPI statements for each goal
* @param {string} goalText Title of the goal
* @return {Object}
*/
GoalsAssessmentPage.prototype.getGoalXAPIDefinition = function (goalText) {
var definition = {};
var self = this;
var choices = self.assessmentCategories.map(function(alt, i) {
return {
id: '' + i,
description: {
'en-US': alt // We don't actually know the language at runtime
}
};
});
definition.interactionType = 'choice';
definition.type = 'http://adlnet.gov/expapi/activities/cmi.interaction';
definition.description = {
'en-US': goalText
};
definition.choices = choices;
return definition;
};
/**
* Add the question itself to the definition part of an xAPIEvent
*/
GoalsAssessmentPage.prototype.addQuestionToXAPI = function (xAPIEvent) {
var definition = xAPIEvent.getVerifiedStatementValue(['object', 'definition']);
$.extend(definition, this.getxAPIDefinition());
};
/**
* Add the question itself to the definition part of an xAPIEvent for a goal
* @param {string} goal The goal title
*/
GoalsAssessmentPage.prototype.addQuestionToGoalXAPI = function (xAPIEvent, goalText) {
var definition = xAPIEvent.getVerifiedStatementValue(['object', 'definition']);
$.extend(definition, this.getGoalXAPIDefinition(goalText));
};
/**
* Add the response part to an xAPI event for each goal
*
* @param {H5P.XAPIEvent} xAPIEvent
* The xAPI event we will add a response to
* @param {number} answer The response
*/
GoalsAssessmentPage.prototype.addResponseToGoalXAPI = function (xAPIEvent, answer) {
xAPIEvent.data.statement.result = {}; // Convert to a string
xAPIEvent.data.statement.result.response = answer + ''; // Convert to a string
};
/**
* Get xAPI data.
* Contract used by report rendering engine.
*
* @see contract at {@link https://h5p.org/documentation/developers/contracts#guides-header-6}
*/
GoalsAssessmentPage.prototype.getXAPIData = function () {
var xAPIEvent = this.createXAPIEventTemplate('answered');
this.addQuestionToXAPI(xAPIEvent);
return {
statement: xAPIEvent.data.statement,
children: this.getXAPIDataFromChildren()
};
};
return GoalsAssessmentPage;
}(H5P.jQuery, H5P.EventDispatcher));