-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathmultichoice.js
1107 lines (981 loc) · 31.5 KB
/
multichoice.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Will render a Question with multiple choices for answers.
// Options format:
// {
// title: "Optional title for question box",
// question: "Question text",
// answers: [{text: "Answer text", correct: false}, ...],
// singleAnswer: true, // or false, will change rendered output slightly.
// singlePoint: true, // True if question give a single point score only
// // if all are correct, false to give 1 point per
// // correct answer. (Only for singleAnswer=false)
// randomAnswers: false // Whether to randomize the order of answers.
// }
//
// Events provided:
// - h5pQuestionAnswered: Triggered when a question has been answered.
var H5P = H5P || {};
/**
* @typedef {Object} Options
* Options for multiple choice
*
* @property {Object} behaviour
* @property {boolean} behaviour.confirmCheckDialog
* @property {boolean} behaviour.confirmRetryDialog
*
* @property {Object} UI
* @property {string} UI.tipsLabel
*
* @property {Object} [confirmRetry]
* @property {string} [confirmRetry.header]
* @property {string} [confirmRetry.body]
* @property {string} [confirmRetry.cancelLabel]
* @property {string} [confirmRetry.confirmLabel]
*
* @property {Object} [confirmCheck]
* @property {string} [confirmCheck.header]
* @property {string} [confirmCheck.body]
* @property {string} [confirmCheck.cancelLabel]
* @property {string} [confirmCheck.confirmLabel]
*/
/**
* Module for creating a multiple choice question
*
* @param {Options} options
* @param {number} contentId
* @param {Object} contentData
* @returns {H5P.MultiChoice}
* @constructor
*/
H5P.MultiChoice = function (options, contentId, contentData) {
if (!(this instanceof H5P.MultiChoice))
return new H5P.MultiChoice(options, contentId, contentData);
var self = this;
this.contentId = contentId;
this.contentData = contentData;
H5P.Question.call(self, 'multichoice');
var $ = H5P.jQuery;
var defaults = {
image: null,
question: "No question text provided",
answers: [
{
tipsAndFeedback: {
tip: '',
chosenFeedback: '',
notChosenFeedback: ''
},
text: "Answer 1",
correct: true
}
],
overallFeedback: [],
weight: 1,
userAnswers: [],
UI: {
checkAnswerButton: 'Check',
submitAnswerButton: 'Submit',
showSolutionButton: 'Show solution',
tryAgainButton: 'Try again',
scoreBarLabel: 'You got :num out of :total points',
tipAvailable: "Tip available",
feedbackAvailable: "Feedback available",
readFeedback: 'Read feedback',
shouldCheck: "Should have been checked",
shouldNotCheck: "Should not have been checked",
noInput: 'Input is required before viewing the solution',
a11yCheck: 'Check the answers. The responses will be marked as correct, incorrect, or unanswered.',
a11yShowSolution: 'Show the solution. The task will be marked with its correct solution.',
a11yRetry: 'Retry the task. Reset all responses and start the task over again.',
},
behaviour: {
enableRetry: true,
enableSolutionsButton: true,
enableCheckButton: true,
type: 'auto',
singlePoint: true,
randomAnswers: false,
showSolutionsRequiresInput: true,
autoCheck: false,
passPercentage: 100,
showScorePoints: true
}
};
var params = $.extend(true, defaults, options);
// Keep track of number of correct choices
var numCorrect = 0;
// Loop through choices
for (var i = 0; i < params.answers.length; i++) {
var answer = params.answers[i];
// Make sure tips and feedback exists
answer.tipsAndFeedback = answer.tipsAndFeedback || {};
if (params.answers[i].correct) {
// Update number of correct choices
numCorrect++;
}
}
// Determine if no choices is the correct
var blankIsCorrect = (numCorrect === 0);
// Determine task type
if (params.behaviour.type === 'auto') {
// Use single choice if only one choice is correct
params.behaviour.singleAnswer = (numCorrect === 1);
}
else {
params.behaviour.singleAnswer = (params.behaviour.type === 'single');
}
var getCheckboxOrRadioIcon = function (radio, selected) {
var icon;
if (radio) {
icon = selected ? '' : '';
}
else {
icon = selected ? '' : '';
}
return icon;
};
// Initialize buttons and elements.
var $myDom;
var $feedbackDialog;
/**
* Remove all feedback dialogs
*/
var removeFeedbackDialog = function () {
// Remove the open feedback dialogs.
$myDom.unbind('click', removeFeedbackDialog);
$myDom.find('.h5p-feedback-button, .h5p-feedback-dialog').remove();
$myDom.find('.h5p-has-feedback').removeClass('h5p-has-feedback');
if ($feedbackDialog) {
$feedbackDialog.remove();
}
};
var score = 0;
var solutionsVisible = false;
/**
* Add feedback to element
* @param {jQuery} $element Element that feedback will be added to
* @param {string} feedback Feedback string
*/
var addFeedback = function ($element, feedback) {
$feedbackDialog = $('' +
'<div class="h5p-feedback-dialog">' +
'<div class="h5p-feedback-inner">' +
'<div class="h5p-feedback-text">' + feedback + '</div>' +
'</div>' +
'</div>');
//make sure feedback is only added once
if (!$element.find($('.h5p-feedback-dialog')).length) {
$feedbackDialog.appendTo($element.addClass('h5p-has-feedback'));
}
};
/**
* Register the different parts of the task with the H5P.Question structure.
*/
self.registerDomElements = function () {
var media = params.media;
if (media && media.type && media.type.library) {
media = media.type;
var type = media.library.split(' ')[0];
if (type === 'H5P.Image') {
if (media.params.file) {
// Register task image
self.setImage(media.params.file.path, {
disableImageZooming: params.media.disableImageZooming || false,
alt: media.params.alt,
title: media.params.title
});
}
}
else if (type === 'H5P.Video') {
if (media.params.sources) {
// Register task video
self.setVideo(media);
}
}
else if (type === 'H5P.Audio') {
if (media.params.files) {
// Register task audio
self.setAudio(media);
}
}
}
// Determine if we're using checkboxes or radio buttons
for (var i = 0; i < params.answers.length; i++) {
params.answers[i].checkboxOrRadioIcon = getCheckboxOrRadioIcon(params.behaviour.singleAnswer, params.userAnswers.indexOf(i) > -1);
}
// Register Introduction
self.setIntroduction('<div id="' + params.labelId + '">' + params.question + '</div>');
// Register task content area
$myDom = $('<ul>', {
'class': 'h5p-answers',
role: params.role,
'aria-labelledby': params.labelId
});
for (let i = 0; i < params.answers.length; i++) {
const answer = params.answers[i];
answer.text = answer.text ?? '<div></div>';
$('<li>', {
'class': 'h5p-answer',
role: answer.role,
tabindex: answer.tabindex,
'aria-checked': answer.checked,
'data-id': i,
html: '<div class="h5p-alternative-container"><span class="h5p-alternative-inner">' + answer.text + '</span></div>',
appendTo: $myDom
});
}
self.setContent($myDom, {
'class': params.behaviour.singleAnswer ? 'h5p-radio' : 'h5p-check'
});
// Create tips:
var $answers = $('.h5p-answer', $myDom).each(function (i) {
var tip = params.answers[i].tipsAndFeedback.tip;
if (tip === undefined) {
return; // No tip
}
tip = tip.trim();
var tipContent = tip
.replace(/ /g, '')
.replace(/<p>/g, '')
.replace(/<\/p>/g, '')
.trim();
if (!tipContent.length) {
return; // Empty tip
}
else {
$(this).addClass('h5p-has-tip');
}
// Add tip
var $wrap = $('<div/>', {
'class': 'h5p-multichoice-tipwrap',
'aria-label': params.UI.tipAvailable + '.'
});
var $multichoiceTip = $('<div>', {
'role': 'button',
'tabindex': 0,
'title': params.UI.tipsLabel,
'aria-label': params.UI.tipsLabel,
'aria-expanded': false,
'class': 'multichoice-tip',
appendTo: $wrap
});
var tipIconHtml = '<span class="joubel-icon-tip-normal">' +
'<span class="h5p-icon-shadow"></span>' +
'<span class="h5p-icon-speech-bubble"></span>' +
'<span class="h5p-icon-info"></span>' +
'</span>';
$multichoiceTip.append(tipIconHtml);
$multichoiceTip.click(function () {
var $tipContainer = $multichoiceTip.parents('.h5p-answer');
var openFeedback = !$tipContainer.children('.h5p-feedback-dialog').is($feedbackDialog);
removeFeedbackDialog();
// Do not open feedback if it was open
if (openFeedback) {
$multichoiceTip.attr('aria-expanded', true);
// Add tip dialog
addFeedback($tipContainer, tip);
$feedbackDialog.addClass('h5p-has-tip');
// Tip for readspeaker
self.read(tip);
}
else {
$multichoiceTip.attr('aria-expanded', false);
}
self.trigger('resize');
// Remove tip dialog on dom click
setTimeout(function () {
$myDom.click(removeFeedbackDialog);
}, 100);
// Do not propagate
return false;
}).keydown(function (e) {
if (e.which === 32) {
$(this).click();
return false;
}
});
$('.h5p-alternative-container', this).append($wrap);
});
// Set event listeners.
var toggleCheck = function ($ans) {
if ($ans.attr('aria-disabled') === 'true') {
return;
}
self.answered = true;
var num = parseInt($ans.data('id'));
if (params.behaviour.singleAnswer) {
// Store answer
params.userAnswers = [num];
// Calculate score
score = (params.answers[num].correct ? 1 : 0);
// De-select previous answer
$answers.not($ans).removeClass('h5p-selected').attr('tabindex', '-1').attr('aria-checked', 'false');
// Select new answer
$ans.addClass('h5p-selected').attr('tabindex', '0').attr('aria-checked', 'true');
}
else {
if ($ans.attr('aria-checked') === 'true') {
const pos = params.userAnswers.indexOf(num);
if (pos !== -1) {
params.userAnswers.splice(pos, 1);
}
// Do not allow un-checking when retry disabled and auto check
if (params.behaviour.autoCheck && !params.behaviour.enableRetry) {
return;
}
// Remove check
$ans.removeClass('h5p-selected').attr('aria-checked', 'false');
}
else {
params.userAnswers.push(num);
$ans.addClass('h5p-selected').attr('aria-checked', 'true');
}
// Calculate score
calcScore();
}
self.triggerXAPI('interacted');
hideSolution($ans);
if (params.userAnswers.length) {
self.showButton('check-answer');
self.hideButton('try-again');
self.hideButton('show-solution');
if (params.behaviour.autoCheck) {
if (params.behaviour.singleAnswer) {
// Only a single answer allowed
checkAnswer();
}
else {
// Show feedback for selected alternatives
self.showCheckSolution(true);
// Always finish task if it was completed successfully
if (score === self.getMaxScore()) {
checkAnswer();
}
}
}
}
};
$answers.click(function () {
toggleCheck($(this));
}).keydown(function (e) {
if (e.keyCode === 32) { // Space bar
// Select current item
toggleCheck($(this));
return false;
}
if (params.behaviour.singleAnswer) {
switch (e.keyCode) {
case 38: // Up
case 37: { // Left
// Try to select previous item
var $prev = $(this).prev();
if ($prev.length) {
toggleCheck($prev.focus());
}
return false;
}
case 40: // Down
case 39: { // Right
// Try to select next item
var $next = $(this).next();
if ($next.length) {
toggleCheck($next.focus());
}
return false;
}
}
}
});
if (params.behaviour.singleAnswer) {
// Special focus handler for radio buttons
$answers.focus(function () {
if ($(this).attr('aria-disabled') !== 'true') {
$answers.not(this).attr('tabindex', '-1');
}
}).blur(function () {
if (!$answers.filter('.h5p-selected').length) {
$answers.first().add($answers.last()).attr('tabindex', '0');
}
});
}
// Adds check and retry button
addButtons();
if (!params.behaviour.singleAnswer) {
calcScore();
}
else {
if (params.userAnswers.length && params.answers[params.userAnswers[0]].correct) {
score = 1;
}
else {
score = 0;
}
}
// Has answered through auto-check in a previous session
if (hasCheckedAnswer && params.behaviour.autoCheck) {
// Check answers if answer has been given or max score reached
if (params.behaviour.singleAnswer || score === self.getMaxScore()) {
checkAnswer();
}
else {
// Show feedback for checked checkboxes
self.showCheckSolution(true);
}
}
};
this.showAllSolutions = function () {
if (solutionsVisible) {
return;
}
solutionsVisible = true;
$myDom.find('.h5p-answer').each(function (i, e) {
var $e = $(e);
var a = params.answers[i];
const className = 'h5p-solution-icon-' + (params.behaviour.singleAnswer ? 'radio' : 'checkbox');
if (a.correct) {
$e.addClass('h5p-should').append($('<span/>', {
'class': className,
html: params.UI.shouldCheck + '.'
}));
}
else {
$e.addClass('h5p-should-not').append($('<span/>', {
'class': className,
html: params.UI.shouldNotCheck + '.'
}));
}
}).find('.h5p-question-plus-one, .h5p-question-minus-one').remove();
// Make sure input is disabled in solution mode
disableInput();
// Move focus back to the first alternative so that the user becomes
// aware that the solution is being shown.
$myDom.find('.h5p-answer:first-child').focus();
//Hide buttons and retry depending on settings.
self.hideButton('check-answer');
self.hideButton('show-solution');
if (params.behaviour.enableRetry) {
self.showButton('try-again');
}
self.trigger('resize');
};
/**
* Used in contracts.
* Shows the solution for the task and hides all buttons.
*/
this.showSolutions = function () {
removeFeedbackDialog();
self.showCheckSolution();
self.showAllSolutions();
disableInput();
self.hideButton('try-again');
};
/**
* Hide solution for the given answer(s)
*
* @private
* @param {H5P.jQuery} $answer
*/
var hideSolution = function ($answer) {
$answer
.removeClass('h5p-correct')
.removeClass('h5p-wrong')
.removeClass('h5p-should')
.removeClass('h5p-should-not')
.removeClass('h5p-has-feedback')
.find('.h5p-question-plus-one, ' +
'.h5p-question-minus-one, ' +
'.h5p-answer-icon, ' +
'.h5p-solution-icon-radio, ' +
'.h5p-solution-icon-checkbox, ' +
'.h5p-feedback-dialog')
.remove();
};
/**
*
*/
this.hideSolutions = function () {
solutionsVisible = false;
hideSolution($('.h5p-answer', $myDom));
this.removeFeedback(); // Reset feedback
self.trigger('resize');
};
/**
* Resets the whole task.
* Used in contracts with integrated content.
* @private
*/
this.resetTask = function () {
self.answered = false;
self.hideSolutions();
params.userAnswers = [];
removeSelections();
self.showButton('check-answer');
self.hideButton('try-again');
self.hideButton('show-solution');
enableInput();
$myDom.find('.h5p-feedback-available').remove();
};
var calculateMaxScore = function () {
if (blankIsCorrect) {
return params.weight;
}
var maxScore = 0;
for (var i = 0; i < params.answers.length; i++) {
var choice = params.answers[i];
if (choice.correct) {
maxScore += (choice.weight !== undefined ? choice.weight : 1);
}
}
return maxScore;
};
this.getMaxScore = function () {
return (!params.behaviour.singleAnswer && !params.behaviour.singlePoint ? calculateMaxScore() : params.weight);
};
/**
* Check answer
*/
var checkAnswer = function () {
// Unbind removal of feedback dialogs on click
$myDom.unbind('click', removeFeedbackDialog);
// Remove all tip dialogs
removeFeedbackDialog();
if (params.behaviour.enableSolutionsButton) {
self.showButton('show-solution');
}
if (params.behaviour.enableRetry) {
self.showButton('try-again');
}
self.hideButton('check-answer');
self.showCheckSolution();
disableInput();
var xAPIEvent = self.createXAPIEventTemplate('answered');
addQuestionToXAPI(xAPIEvent);
addResponseToXAPI(xAPIEvent);
self.trigger(xAPIEvent);
};
/**
* Adds the ui buttons.
* @private
*/
var addButtons = function () {
var $content = $('[data-content-id="' + self.contentId + '"].h5p-content');
var $containerParents = $content.parents('.h5p-container');
// select find container to attach dialogs to
var $container;
if($containerParents.length !== 0) {
// use parent highest up if any
$container = $containerParents.last();
}
else if($content.length !== 0){
$container = $content;
}
else {
$container = $(document.body);
}
// Show solution button
self.addButton('show-solution', params.UI.showSolutionButton, function () {
if (params.behaviour.showSolutionsRequiresInput && !self.getAnswerGiven(true)) {
// Require answer before solution can be viewed
self.updateFeedbackContent(params.UI.noInput);
self.read(params.UI.noInput);
}
else {
calcScore();
self.showAllSolutions();
}
}, false, {
'aria-label': params.UI.a11yShowSolution,
});
// Check button
if (params.behaviour.enableCheckButton && (!params.behaviour.autoCheck || !params.behaviour.singleAnswer)) {
self.addButton('check-answer', params.UI.checkAnswerButton,
function () {
self.answered = true;
checkAnswer();
$myDom.find('.h5p-answer:first-child').focus();
},
true,
{
'aria-label': params.UI.a11yCheck,
},
{
confirmationDialog: {
enable: params.behaviour.confirmCheckDialog,
l10n: params.confirmCheck,
instance: self,
$parentElement: $container
},
contentData: self.contentData,
textIfSubmitting: params.UI.submitAnswerButton,
}
);
}
// Try Again button
self.addButton('try-again', params.UI.tryAgainButton, function () {
self.resetTask();
if (params.behaviour.randomAnswers) {
// reshuffle answers
var oldIdMap = idMap;
idMap = getShuffleMap();
var answersDisplayed = $myDom.find('.h5p-answer');
// remember tips
var tip = [];
for (i = 0; i < answersDisplayed.length; i++) {
tip[i] = $(answersDisplayed[i]).find('.h5p-multichoice-tipwrap');
}
// Those two loops cannot be merged or you'll screw up your tips
for (i = 0; i < answersDisplayed.length; i++) {
// move tips and answers on display
$(answersDisplayed[i]).find('.h5p-alternative-inner').html(params.answers[i].text);
$(tip[i]).detach().appendTo($(answersDisplayed[idMap.indexOf(oldIdMap[i])]).find('.h5p-alternative-container'));
}
}
}, false, {
'aria-label': params.UI.a11yRetry,
}, {
confirmationDialog: {
enable: params.behaviour.confirmRetryDialog,
l10n: params.confirmRetry,
instance: self,
$parentElement: $container
}
});
};
/**
* Determine which feedback text to display
*
* @param {number} score
* @param {number} max
* @return {string}
*/
var getFeedbackText = function (score, max) {
var ratio = (score / max);
var feedback = H5P.Question.determineOverallFeedback(params.overallFeedback, ratio);
return feedback.replace('@score', score).replace('@total', max);
};
/**
* Shows feedback on the selected fields.
* @public
* @param {boolean} [skipFeedback] Skip showing feedback if true
*/
this.showCheckSolution = function (skipFeedback) {
var scorePoints;
if (!(params.behaviour.singleAnswer || params.behaviour.singlePoint || !params.behaviour.showScorePoints)) {
scorePoints = new H5P.Question.ScorePoints();
}
$myDom.find('.h5p-answer').each(function (i, e) {
var $e = $(e);
var a = params.answers[i];
var chosen = ($e.attr('aria-checked') === 'true');
if (chosen) {
if (a.correct) {
// May already have been applied by instant feedback
if (!$e.hasClass('h5p-correct')) {
$e.addClass('h5p-correct').append($('<span/>', {
'class': 'h5p-answer-icon',
html: params.UI.correctAnswer + '.'
}));
}
}
else {
if (!$e.hasClass('h5p-wrong')) {
$e.addClass('h5p-wrong').append($('<span/>', {
'class': 'h5p-answer-icon',
html: params.UI.wrongAnswer + '.'
}));
}
}
if (scorePoints) {
var alternativeContainer = $e[0].querySelector('.h5p-alternative-container');
if (!params.behaviour.autoCheck || alternativeContainer.querySelector('.h5p-question-plus-one, .h5p-question-minus-one') === null) {
alternativeContainer.appendChild(scorePoints.getElement(a.correct));
}
}
}
if (!skipFeedback) {
if (chosen && a.tipsAndFeedback.chosenFeedback !== undefined && a.tipsAndFeedback.chosenFeedback !== '') {
addFeedback($e, a.tipsAndFeedback.chosenFeedback);
}
else if (!chosen && a.tipsAndFeedback.notChosenFeedback !== undefined && a.tipsAndFeedback.notChosenFeedback !== '') {
addFeedback($e, a.tipsAndFeedback.notChosenFeedback);
}
}
});
// Determine feedback
var max = self.getMaxScore();
// Disable task if maxscore is achieved
var fullScore = (score === max);
if (fullScore) {
self.hideButton('check-answer');
self.hideButton('try-again');
self.hideButton('show-solution');
}
// Show feedback
if (!skipFeedback) {
this.setFeedback(getFeedbackText(score, max), score, max, params.UI.scoreBarLabel);
}
self.trigger('resize');
};
/**
* Disables choosing new input.
*/
var disableInput = function () {
$('.h5p-answer', $myDom).attr({
'aria-disabled': 'true',
'tabindex': '-1'
}).removeAttr('role')
.removeAttr('aria-checked');
$('.h5p-answers').removeAttr('role');
};
/**
* Enables new input.
*/
var enableInput = function () {
$('.h5p-answer', $myDom)
.attr({
'aria-disabled': 'false',
'role': params.behaviour.singleAnswer ? 'radio' : 'checkbox',
});
$('.h5p-answers').attr('role', params.role);
};
var calcScore = function () {
score = 0;
for (const answer of params.userAnswers) {
const choice = params.answers[answer];
const weight = (choice.weight !== undefined ? choice.weight : 1);
if (choice.correct) {
score += weight;
}
else {
score -= weight;
}
}
if (score < 0) {
score = 0;
}
if (!params.userAnswers.length && blankIsCorrect) {
score = params.weight;
}
if (params.behaviour.singlePoint) {
score = (100 * score / calculateMaxScore()) >= params.behaviour.passPercentage ? params.weight : 0;
}
};
/**
* Removes selections from task.
*/
var removeSelections = function () {
var $answers = $('.h5p-answer', $myDom)
.removeClass('h5p-selected')
.attr('aria-checked', 'false');
if (!params.behaviour.singleAnswer) {
$answers.attr('tabindex', '0');
}
else {
$answers.first().attr('tabindex', '0');
}
// Set focus to first option
$answers.first().focus();
calcScore();
};
/**
* Get xAPI data.
* Contract used by report rendering engine.
*
* @see contract at {@link https://h5p.org/documentation/developers/contracts#guides-header-6}
*/
this.getXAPIData = function(){
var xAPIEvent = this.createXAPIEventTemplate('answered');
addQuestionToXAPI(xAPIEvent);
addResponseToXAPI(xAPIEvent);
return {
statement: xAPIEvent.data.statement
};
};
/**
* Add the question itself to the definition part of an xAPIEvent
*/
var addQuestionToXAPI = function (xAPIEvent) {
var definition = xAPIEvent.getVerifiedStatementValue(['object', 'definition']);
definition.description = {
// Remove tags, must wrap in div tag because jQuery 1.9 will crash if the string isn't wrapped in a tag.
'en-US': $('<div>' + params.question + '</div>').text()
};
definition.type = 'http://adlnet.gov/expapi/activities/cmi.interaction';
definition.interactionType = 'choice';
definition.correctResponsesPattern = [];
definition.choices = [];
for (var i = 0; i < params.answers.length; i++) {
definition.choices[i] = {
'id': params.answers[i].originalOrder + '',
'description': {
// Remove tags, must wrap in div tag because jQuery 1.9 will crash if the string isn't wrapped in a tag.
'en-US': $('<div>' + params.answers[i].text + '</div>').text()
}
};
if (params.answers[i].correct) {
if (!params.singleAnswer) {
if (definition.correctResponsesPattern.length) {
definition.correctResponsesPattern[0] += '[,]';
// This looks insane, but it's how you separate multiple answers
// that must all be chosen to achieve perfect score...
}
else {
definition.correctResponsesPattern.push('');
}
definition.correctResponsesPattern[0] += params.answers[i].originalOrder;
}
else {
definition.correctResponsesPattern.push('' + params.answers[i].originalOrder);
}
}
}
};
/**
* Add the response part to an xAPI event
*
* @param {H5P.XAPIEvent} xAPIEvent
* The xAPI event we will add a response to
*/
var addResponseToXAPI = function (xAPIEvent) {
var maxScore = self.getMaxScore();
var success = (100 * score / maxScore) >= params.behaviour.passPercentage;
xAPIEvent.setScoredResult(score, maxScore, self, true, success);
if (params.userAnswers === undefined) {
calcScore();
}
// Add the response
var response = '';
for (var i = 0; i < params.userAnswers.length; i++) {
if (response !== '') {
response += '[,]';
}
response += idMap === undefined ? params.userAnswers[i] : idMap[params.userAnswers[i]];
}
xAPIEvent.data.statement.result.response = response;
};
/**
* Create a map pointing from original answers to shuffled answers
*
* @return {number[]} map pointing from original answers to shuffled answers
*/
var getShuffleMap = function() {
params.answers = H5P.shuffleArray(params.answers);
// Create a map from the new id to the old one
var idMap = [];
for (i = 0; i < params.answers.length; i++) {
idMap[i] = params.answers[i].originalOrder;
}
return idMap;
};
// Initialization code
// Randomize order, if requested
var idMap;
// Store original order in answers
for (i = 0; i < params.answers.length; i++) {
params.answers[i].originalOrder = i;
}
if (params.behaviour.randomAnswers) {
idMap = getShuffleMap();
}
// Start with an empty set of user answers.
params.userAnswers = [];
// Restore previous state
if (contentData && contentData.previousState !== undefined) {
// Restore answers
if (contentData.previousState.answers) {
if (!idMap) {