h5p-question-set/js/questionset.js

836 lines
26 KiB
JavaScript
Raw Normal View History

2013-04-04 15:44:06 +02:00
var H5P = H5P || {};
2013-04-26 17:27:35 +02:00
/**
* Will render a Question with multiple choices for answers.
*
* Events provided:
* - h5pQuestionSetFinished: Triggered when a question is finished. (User presses Finish-button)
*
* @param {Array} options
* @param {int} contentId
* @param {Object} contentData
2013-04-26 17:27:35 +02:00
* @returns {H5P.QuestionSet} Instance
*/
H5P.QuestionSet = function (options, contentId, contentData) {
2013-04-26 17:27:35 +02:00
if (!(this instanceof H5P.QuestionSet)) {
return new H5P.QuestionSet(options, contentId, contentData);
2013-04-26 17:27:35 +02:00
}
2015-02-04 16:40:29 +01:00
H5P.EventDispatcher.call(this);
2013-04-04 15:44:06 +02:00
var $ = H5P.jQuery;
2014-10-13 21:51:36 +02:00
var self = this;
2015-02-04 16:40:29 +01:00
this.contentId = contentId;
2013-04-04 15:44:06 +02:00
2013-04-26 17:27:35 +02:00
var texttemplate =
'<% if (introPage.showIntroPage) { %>' +
'<div class="intro-page">' +
' <% if (introPage.title) { %>' +
' <div class="title"><span><%= introPage.title %></span></div>' +
' <% } %>' +
' <% if (introPage.introduction) { %>' +
' <div class="introduction"><%= introPage.introduction %></div>' +
' <% } %>' +
' <div class="buttons"><a class="qs-startbutton h5p-joubelui-button h5p-button"><%= introPage.startButtonText %></a></div>' +
2013-04-26 17:27:35 +02:00
'</div>' +
2013-05-03 10:05:49 +02:00
'<% } %>' +
'<div tabindex="-1" class="qs-progress-announcer"></div>' +
2013-05-03 10:05:49 +02:00
'<div class="questionset<% if (introPage.showIntroPage) { %> hidden<% } %>">' +
2013-04-26 17:27:35 +02:00
' <% for (var i=0; i<questions.length; i++) { %>' +
' <div class="question-container"></div>' +
2013-04-26 17:27:35 +02:00
' <% } %>' +
' <div class="qs-footer">' +
' <div class="qs-progress">' +
' <% if (progressType == "dots" && !disableBackwardsNavigation) { %>' +
' <ul class="dots-container" role="navigation">' +
2013-04-26 17:27:35 +02:00
' <% for (var i=0; i<questions.length; i++) { %>' +
' <li class="progress-item"><a href="#" class="progress-dot unanswered" ' +
' aria-label="<%=' +
' texts.jumpToQuestion.replace("%d", i + 1).replace("%total", questions.length)' +
' + ", " + texts.unansweredText' +
' %>" tabindex="-1"></a></li>' +
' <% } %>' +
2013-04-26 17:27:35 +02:00
' </div>' +
' <% } else if (progressType == "textual") { %>' +
' <span class="progress-text"></span>' +
' <% } %>' +
' </div>' +
' </div>' +
'</div>';
var resulttemplate =
'<div class="questionset-results">' +
' <div class="greeting"><%= message %></div>' +
2015-08-18 13:58:33 +02:00
' <div class="feedback-section">' +
' <div class="feedback-scorebar"></div>' +
' <div class="feedback-text"></div>' +
' </div>' +
' <% if (comment) { %>' +
' <div class="result-header"><%= comment %></div>' +
' <% } %>' +
' <% if (resulttext) { %>' +
' <div class="result-text"><%= resulttext %></div>' +
' <% } %>' +
' <div class="buttons">' +
' <button type="button" class="h5p-joubelui-button h5p-button qs-finishbutton"><%= finishButtonText %></button>' +
' <button type="button" class="h5p-joubelui-button h5p-button qs-solutionbutton"><%= solutionButtonText %></button>' +
' <button type="button" class="h5p-joubelui-button h5p-button qs-retrybutton"><%= retryButtonText %></button>' +
' </div>' +
2013-04-26 17:27:35 +02:00
'</div>';
2013-04-04 15:44:06 +02:00
var defaults = {
randomOrder: false,
initialQuestion: 0,
progressType: 'dots',
passPercentage: 50,
questions: [],
introPage: {
showIntroPage: false,
title: '',
introduction: '',
2013-04-26 17:27:35 +02:00
startButtonText: 'Start'
2013-04-04 15:44:06 +02:00
},
texts: {
2016-04-08 16:07:24 +02:00
prevButton: 'Previous question',
nextButton: 'Next question',
2013-04-26 17:27:35 +02:00
finishButton: 'Finish',
2016-03-17 14:09:13 +01:00
textualProgress: 'Question: @current of @total questions',
jumpToQuestion: 'Question %d of %total',
questionLabel: 'Question',
readSpeakerProgress: 'Question @current of @total',
unansweredText: 'Unanswered',
answeredText: 'Answered',
currentQuestionText: 'Current question'
2013-04-04 15:44:06 +02:00
},
endGame: {
showResultPage: true,
noResultMessage: 'Finished',
message: 'Your result:',
successGreeting: 'Congratulations!',
successComment: 'You have enough correct answers to pass the test.',
failGreeting: 'Sorry!',
failComment: "You don't have enough correct answers to pass this test.",
scoreString: 'You got @score of @total points',
finishButtonText: 'Finish',
solutionButtonText: 'Show solution',
2014-11-27 11:01:47 +01:00
retryButtonText: 'Retry',
2016-03-17 14:09:13 +01:00
showAnimations: false,
skipButtonText: 'Skip video'
},
disableBackwardsNavigation: false
2013-04-04 15:44:06 +02:00
};
var template = new EJS({text: texttemplate});
var endTemplate = new EJS({text: resulttemplate});
2013-04-26 17:27:35 +02:00
var params = $.extend(true, {}, defaults, options);
2013-04-04 15:44:06 +02:00
var currentQuestion = 0;
var questionInstances = [];
2013-04-04 15:44:06 +02:00
var $myDom;
2015-08-18 13:58:33 +02:00
var scoreBar;
var up;
var renderSolutions = false;
contentData = contentData || {};
if (contentData.previousState) {
currentQuestion = contentData.previousState.progress;
}
2013-04-04 15:44:06 +02:00
var $template = $(template.render(params));
2016-04-04 15:50:41 +02:00
// Set overrides for questions
var override;
if (params.override.showSolutionButton || params.override.retryButton) {
override = {};
if (params.override.showSolutionButton) {
// Force "Show solution" button to be on or off for all interactions
override.enableSolutionsButton =
(params.override.showSolutionButton === 'on' ? true : false);
}
if (params.override.retryButton) {
// Force "Retry" button to be on or off for all interactions
override.enableRetry =
(params.override.retryButton === 'on' ? true : false);
}
}
2013-04-04 15:44:06 +02:00
// Instantiate question instances
2013-04-26 17:27:35 +02:00
for (var i = 0; i < params.questions.length; i++) {
2013-04-04 15:44:06 +02:00
var question = params.questions[i];
2016-04-04 15:50:41 +02:00
if (override) {
2014-11-27 11:01:47 +01:00
// Extend subcontent with the overrided settings.
2016-04-04 15:50:41 +02:00
$.extend(question.params.behaviour, override);
2014-11-27 11:01:47 +01:00
}
question.params = question.params || {};
question.params.overrideSettings = question.params.overrideSettings || {};
question.params.overrideSettings.$confirmationDialogParent = $template.last();
question.params.overrideSettings.instance = this;
var questionInstance = H5P.newRunnable(question, contentId, undefined, undefined,
{
previousState: contentData.previousState ? contentData.previousState.answers[i] : undefined,
parent: self
});
questionInstance.on('resize', function () {
up = true;
self.trigger('resize');
});
2015-02-04 16:40:29 +01:00
questionInstances.push(questionInstance);
2013-04-04 15:44:06 +02:00
}
// Resize all interactions on resize
self.on('resize', function () {
if (up) {
// Prevent resizing the question again.
up = false;
return;
}
for (var i = 0; i < questionInstances.length; i++) {
questionInstances[i].trigger('resize');
}
});
2013-04-04 15:44:06 +02:00
// Update button state.
var _updateButtons = function () {
var answered = true;
for (var i = questionInstances.length - 1; i >= 0; i--) {
answered = answered && (questionInstances[i]).getAnswerGiven();
}
if (currentQuestion === (params.questions.length - 1) &&
2015-09-25 10:57:16 +02:00
questionInstances[currentQuestion]) {
if (answered) {
questionInstances[currentQuestion].showButton('finish');
}
else {
questionInstances[currentQuestion].hideButton('finish');
}
2013-04-04 15:44:06 +02:00
}
};
var _stopQuestion = function (questionNumber) {
if (questionInstances[questionNumber]) {
2015-11-18 09:59:45 +01:00
pauseMedia(questionInstances[questionNumber]);
}
};
var _showQuestion = function (questionNumber, preventAnnouncement) {
2013-04-04 15:44:06 +02:00
// Sanitize input.
2013-04-26 17:27:35 +02:00
if (questionNumber < 0) {
questionNumber = 0;
}
if (questionNumber >= params.questions.length) {
questionNumber = params.questions.length - 1;
}
2013-04-04 15:44:06 +02:00
currentQuestion = questionNumber;
2015-09-25 10:57:16 +02:00
// Hide all questions
$('.question-container', $myDom).hide().eq(questionNumber).show();
2013-04-04 15:44:06 +02:00
2015-09-25 10:57:16 +02:00
if (questionInstances[questionNumber]) {
// Trigger resize on question in case the size of the QS has changed.
var instance = questionInstances[questionNumber];
instance.setActivityStarted();
2015-09-25 10:57:16 +02:00
if (instance.$ !== undefined) {
instance.trigger('resize');
}
}
2013-04-04 15:44:06 +02:00
// Update progress indicator
// Test if current has been answered.
2013-04-26 17:27:35 +02:00
if (params.progressType === 'textual') {
2013-04-04 15:44:06 +02:00
$('.progress-text', $myDom).text(params.texts.textualProgress.replace("@current", questionNumber+1).replace("@total", params.questions.length));
2013-04-26 17:27:35 +02:00
}
else {
2013-04-04 15:44:06 +02:00
// Set currentNess
var previousQuestion = $('.progress-dot.current', $myDom).parent().index();
if (previousQuestion >= 0) {
toggleCurrentDot(previousQuestion, false);
toggleAnsweredDot(previousQuestion, questionInstances[previousQuestion].getAnswerGiven());
}
toggleCurrentDot(questionNumber, true);
2013-04-04 15:44:06 +02:00
}
if (!preventAnnouncement) {
// Announce question number of total, must use timeout because of buttons logic
setTimeout(function () {
var humanizedProgress = params.texts.readSpeakerProgress
.replace('@current', (currentQuestion + 1).toString())
.replace('@total', questionInstances.length.toString());
$('.qs-progress-announcer', $myDom)
.html(humanizedProgress)
.show().focus();
if (instance && instance.readFeedback) {
instance.readFeedback();
}
}, 0);
}
2013-04-04 15:44:06 +02:00
// Remember where we are
_updateButtons();
2015-02-04 16:40:29 +01:00
self.trigger('resize');
2013-04-04 15:44:06 +02:00
return currentQuestion;
};
2014-11-27 11:01:47 +01:00
/**
* Show solutions for subcontent, and hide subcontent buttons.
* Used for contracts with integrated content.
* @public
*/
2013-05-03 10:05:49 +02:00
var showSolutions = function () {
for (var i = 0; i < questionInstances.length; i++) {
2014-11-27 11:01:47 +01:00
try {
// Do not read answers
questionInstances[i].toggleReadSpeaker(true);
2014-11-27 11:01:47 +01:00
questionInstances[i].showSolutions();
questionInstances[i].toggleReadSpeaker(false);
2014-11-27 11:01:47 +01:00
}
catch(error) {
H5P.error("subcontent does not contain a valid showSolutions function");
H5P.error(error);
2014-11-27 11:01:47 +01:00
}
2013-05-03 10:05:49 +02:00
}
};
2014-11-27 11:01:47 +01:00
/**
* Resets the task and every subcontent task.
* Used for contracts with integrated content.
* @public
*/
var resetTask = function () {
for (var i = 0; i < questionInstances.length; i++) {
try {
questionInstances[i].resetTask();
}
catch(error) {
H5P.error("subcontent does not contain a valid resetTask function");
H5P.error(error);
2014-11-27 11:01:47 +01:00
}
2013-05-03 10:05:49 +02:00
}
// Hide finish button
questionInstances[questionInstances.length - 1].hideButton('finish');
// Mark all tasks as unanswered:
$('.progress-dot').each(function (idx) {
toggleAnsweredDot(idx, false);
});
2014-11-27 11:01:47 +01:00
//Force the last page to be reRendered
rendered = false;
2013-05-03 10:05:49 +02:00
};
var rendered = false;
2015-02-04 16:40:29 +01:00
this.reRender = function () {
2013-05-03 10:05:49 +02:00
rendered = false;
};
2016-03-17 14:09:13 +01:00
var moveQuestion = function (direction) {
_stopQuestion(currentQuestion);
if (currentQuestion + direction >= questionInstances.length) {
_displayEndGame();
}
else if (!params.disableBackwardsNavigation || questionInstances[currentQuestion].getAnswerGiven()) {
// Allow movement if backward navigation enabled or answer given
2016-03-17 14:09:13 +01:00
_showQuestion(currentQuestion + direction);
}
else {
//TODO: Give an error message ? or disable/grey out previous button when not allowed
}
2016-03-17 14:09:13 +01:00
};
/**
* Toggle answered state of dot at given index
* @param {number} dotIndex Index of dot
* @param {boolean} isAnswered True if is answered, False if not answered
*/
var toggleAnsweredDot = function(dotIndex, isAnswered) {
var $el = $('.progress-dot:eq(' + dotIndex +')', $myDom);
// Skip current button
if ($el.hasClass('current')) {
return;
}
// Ensure boolean
isAnswered = !!isAnswered;
var label = params.texts.jumpToQuestion
.replace('%d', (dotIndex + 1).toString())
.replace('%total', $('.progress-dot', $myDom).length) +
', ' +
(isAnswered ? params.texts.answeredText : params.texts.unansweredText);
$el.toggleClass('unanswered', !isAnswered)
.toggleClass('answered', isAnswered)
.attr('aria-label', label);
};
/**
* Toggle current state of dot at given index
* @param dotIndex
* @param isCurrent
*/
var toggleCurrentDot = function (dotIndex, isCurrent) {
var $el = $('.progress-dot:eq(' + dotIndex +')', $myDom);
var texts = params.texts;
var label = texts.jumpToQuestion
.replace('%d', (dotIndex + 1).toString())
.replace('%total', $('.progress-dot', $myDom).length);
if (!isCurrent) {
var isAnswered = $el.hasClass('answered');
label += ', ' + (isAnswered ? texts.answeredText : texts.unansweredText);
}
else {
label += ', ' + texts.currentQuestionText;
}
$el.toggleClass('current', isCurrent)
.attr('aria-label', label)
.attr('tabindex', isCurrent ? 0 : -1);
};
2013-04-04 15:44:06 +02:00
var _displayEndGame = function () {
$('.progress-dot.current', $myDom).removeClass('current');
2013-05-03 10:05:49 +02:00
if (rendered) {
$myDom.children().hide().filter('.questionset-results').show();
self.trigger('resize');
2013-05-03 10:05:49 +02:00
return;
}
2014-11-27 11:01:47 +01:00
//Remove old score screen.
$myDom.children().hide().filter('.questionset-results').remove();
2013-05-03 10:05:49 +02:00
rendered = true;
2013-04-04 15:44:06 +02:00
// Get total score.
2015-02-04 16:40:29 +01:00
var finals = self.getScore();
var totals = self.totalScore();
var scoreString = params.endGame.scoreString.replace("@score", finals).replace("@total", totals);
2013-04-04 15:44:06 +02:00
var success = ((100 * finals / totals) >= params.passPercentage);
var eventData = {
score: scoreString,
passed: success
};
/**
* Makes our buttons behave like other buttons.
*
* @private
* @param {string} classSelector
* @param {function} handler
*/
var hookUpButton = function (classSelector, handler) {
$(classSelector, $myDom).click(handler).keypress(function (e) {
if (e.which === 32) {
handler();
e.preventDefault();
}
});
};
2013-04-04 15:44:06 +02:00
var displayResults = function () {
self.triggerXAPICompleted(self.getScore(), self.totalScore(), success);
2013-04-04 15:44:06 +02:00
var eparams = {
message: params.endGame.showResultPage ? params.endGame.message : params.endGame.noResultMessage,
comment: params.endGame.showResultPage ? (success ? params.endGame.successGreeting : params.endGame.failGreeting) : undefined,
resulttext: params.endGame.showResultPage ? (success ? params.endGame.successComment : params.endGame.failComment) : undefined,
2013-05-03 10:05:49 +02:00
finishButtonText: params.endGame.finishButtonText,
solutionButtonText: params.endGame.solutionButtonText,
retryButtonText: params.endGame.retryButtonText
2013-04-04 15:44:06 +02:00
};
// Show result page.
$myDom.children().hide();
$myDom.append(endTemplate.render(eparams));
if (params.endGame.showResultPage) {
// Add event handlers to summary buttons
hookUpButton('.qs-finishbutton', function () {
self.trigger('h5pQuestionSetFinished', eventData);
});
hookUpButton('.qs-solutionbutton', function () {
showSolutions();
$myDom.children().hide().filter('.questionset').show();
2016-04-15 09:49:50 +02:00
_showQuestion(params.initialQuestion);
});
hookUpButton('.qs-retrybutton', function () {
resetTask();
$myDom.children().hide();
var $intro = $('.intro-page', $myDom);
if ($intro.length) {
// Show intro
$('.intro-page', $myDom).show();
}
else {
// Show first question
$('.questionset', $myDom).show();
_showQuestion(params.initialQuestion);
}
});
2015-08-18 13:58:33 +02:00
if (scoreBar === undefined) {
scoreBar = H5P.JoubelUI.createScoreBar(totals);
}
scoreBar.appendTo($('.feedback-scorebar', $myDom));
scoreBar.setScore(finals);
$('.feedback-text', $myDom).html(scoreString);
// Announce that the question set is complete
setTimeout(function () {
$('.qs-progress-announcer', $myDom)
.html(eparams.message + '.' +
scoreString + '.' +
eparams.comment + '.' +
eparams.resulttext)
.show().focus();
}, 0);
2015-08-18 13:58:33 +02:00
}
else {
// Remove buttons and feedback section
$('.qs-finishbutton, .qs-solutionbutton, .qs-retrybutton, .feedback-section', $myDom).remove();
}
self.trigger('resize');
2013-04-04 15:44:06 +02:00
};
if (params.endGame.showAnimations) {
var videoData = success ? params.endGame.successVideo : params.endGame.failVideo;
2013-04-04 15:44:06 +02:00
if (videoData) {
$myDom.children().hide();
2013-04-26 17:27:35 +02:00
var $videoContainer = $('<div class="video-container"></div>').appendTo($myDom);
var video = new H5P.Video({
2015-02-17 11:14:49 +01:00
sources: videoData,
2013-04-26 17:27:35 +02:00
fitToWrapper: true,
controls: false,
autoplay: false
}, contentId);
2015-02-17 11:14:49 +01:00
video.on('stateChange', function (event) {
if (event.data === H5P.Video.ENDED) {
displayResults();
$videoContainer.hide();
}
});
2013-04-26 17:27:35 +02:00
video.attach($videoContainer);
// Resize on video loaded
video.on('loaded', function () {
self.trigger('resize');
});
video.play();
2013-04-26 17:27:35 +02:00
if (params.endGame.skippable) {
$('<a class="h5p-joubelui-button h5p-button skip">' + params.endGame.skipButtonText + '</a>').click(function () {
2015-02-17 11:14:49 +01:00
video.pause();
2013-04-26 17:27:35 +02:00
$videoContainer.hide();
displayResults();
}).appendTo($videoContainer);
}
2013-04-04 15:44:06 +02:00
return;
}
}
// Trigger finished event.
displayResults();
2015-02-18 18:08:57 +01:00
self.trigger('resize');
2013-04-04 15:44:06 +02:00
};
// Function for attaching the multichoice to a DOM element.
2015-02-04 16:40:29 +01:00
this.attach = function (target) {
if (this.isRoot()) {
this.setActivityStarted();
}
2013-04-26 17:27:35 +02:00
if (typeof(target) === "string") {
$myDom = $('#' + target);
}
else {
2013-04-04 15:44:06 +02:00
$myDom = $(target);
}
// Render own DOM into target.
$myDom.children().remove();
$myDom.append($template);
if (params.backgroundImage !== undefined) {
$myDom.css({
overflow: 'hidden',
background: '#fff url("' + H5P.getPath(params.backgroundImage.path, contentId) + '") no-repeat 50% 50%',
backgroundSize: '100% auto'
});
}
if (params.introPage.backgroundImage !== undefined) {
var $intro = $myDom.find('.intro-page');
if ($intro.length) {
var bgImg = params.introPage.backgroundImage;
var bgImgRatio = (bgImg.height / bgImg.width);
$intro.css({
background: '#fff url("' + H5P.getPath(bgImg.path, contentId) + '") no-repeat 50% 50%',
backgroundSize: 'auto 100%',
minHeight: bgImgRatio * +window.getComputedStyle($intro[0]).width.replace('px','')
});
}
}
var registerImageLoadedListener = function (question) {
H5P.on(question, 'imageLoaded', function () {
self.trigger('resize');
});
};
2013-04-04 15:44:06 +02:00
// Attach questions
2013-04-26 17:27:35 +02:00
for (var i = 0; i < questionInstances.length; i++) {
2013-04-04 15:44:06 +02:00
var question = questionInstances[i];
question.attach($('.question-container:eq(' + i + ')', $myDom));
// Listen for image resize
registerImageLoadedListener(question);
// Add next/finish button
if (questionInstances[questionInstances.length -1] === question) {
// Add finish question set button
2016-03-17 14:09:13 +01:00
question.addButton('finish', params.texts.finishButton,
moveQuestion.bind(this, 1), false);
} else {
// Add next question button
question.addButton('next', '', moveQuestion.bind(this, 1), true, {
href: '#', // Use href since this is a navigation button
'aria-label': params.texts.nextButton
});
}
// Add previous question button
if (questionInstances[0] !== question && !params.disableBackwardsNavigation) {
question.addButton('prev', '', moveQuestion.bind(this, -1), true, {
href: '#', // Use href since this is a navigation button
'aria-label': params.texts.prevButton
});
}
2015-02-04 16:40:29 +01:00
question.on('xAPI', function (event) {
var shortVerb = event.getVerb();
if (shortVerb === 'interacted' ||
shortVerb === 'answered' ||
shortVerb === 'attempted') {
toggleAnsweredDot(currentQuestion,
questionInstances[currentQuestion].getAnswerGiven());
2014-10-13 21:51:36 +02:00
_updateButtons();
}
if (shortVerb === 'completed') {
// An activity within this activity is not allowed to send completed events
event.setVerb('answered');
}
if (event.data.statement.context.extensions === undefined) {
event.data.statement.context.extensions = {};
}
event.data.statement.context.extensions['http://id.tincanapi.com/extension/ending-point'] = currentQuestion + 1;
2013-04-04 15:44:06 +02:00
});
// Mark question if answered
toggleAnsweredDot(i, question.getAnswerGiven());
2013-04-04 15:44:06 +02:00
}
// Allow other libraries to add transitions after the questions have been inited
$('.questionset', $myDom).addClass('started');
2013-04-04 15:44:06 +02:00
2013-05-03 10:05:49 +02:00
$('.qs-startbutton', $myDom).click(function () {
2013-04-04 15:44:06 +02:00
$(this).parents('.intro-page').hide();
2016-04-15 09:49:50 +02:00
$('.questionset', $myDom).show();
_showQuestion(params.initialQuestion);
2013-04-04 15:44:06 +02:00
});
/**
* Triggers changing the current question.
*
* @private
* @param {Object} [event]
*/
var handleProgressDotClick = function (event) {
_stopQuestion(currentQuestion);
_showQuestion($(this).parent().index());
event.preventDefault();
};
// Set event listeners.
$('.progress-dot', $myDom).click(handleProgressDotClick).keydown(function (event) {
var $this = $(this);
switch (event.which) {
case 13: // Enter
case 32: // Space
handleProgressDotClick.call(this, event);
break;
case 37: // Left Arrow
case 38: // Up Arrow
// Go to previous dot
var $prev = $this.parent().prev();
if ($prev.length) {
$prev.children('a').attr('tabindex', '0').focus();
$this.attr('tabindex', '-1');
}
break;
case 39: // Right Arrow
case 40: // Down Arrow
// Go to next dot
var $next = $this.parent().next();
if ($next.length) {
$next.children('a').attr('tabindex', '0').focus();
$this.attr('tabindex', '-1');
}
break;
}
2013-04-04 15:44:06 +02:00
});
// Hide all but current question
_showQuestion(currentQuestion, true);
2013-05-03 10:05:49 +02:00
if (renderSolutions) {
showSolutions();
}
// Update buttons in case they have changed (restored user state)
_updateButtons();
2014-10-26 22:14:26 +01:00
this.trigger('resize');
2013-04-04 15:44:06 +02:00
return this;
};
// Get current score for questionset.
2015-02-04 16:40:29 +01:00
this.getScore = function () {
2013-04-04 15:44:06 +02:00
var score = 0;
for (var i = questionInstances.length - 1; i >= 0; i--) {
score += questionInstances[i].getScore();
}
return score;
};
// Get total score possible for questionset.
2015-02-04 16:40:29 +01:00
this.totalScore = function () {
2013-04-04 15:44:06 +02:00
var score = 0;
for (var i = questionInstances.length - 1; i >= 0; i--) {
2013-04-26 17:27:35 +02:00
score += questionInstances[i].getMaxScore();
2013-04-04 15:44:06 +02:00
}
return score;
};
/**
* Gather copyright information for the current content.
*
* @returns {H5P.ContentCopyrights}
*/
2015-02-04 16:40:29 +01:00
this.getCopyrights = function () {
var info = new H5P.ContentCopyrights();
// Background
if (params.backgroundImage !== undefined && params.backgroundImage.copyright !== undefined) {
var background = new H5P.MediaCopyright(params.backgroundImage.copyright);
background.setThumbnail(new H5P.Thumbnail(H5P.getPath(params.backgroundImage.path, contentId), params.backgroundImage.width, params.backgroundImage.height));
info.addMedia(background);
}
// Questions
var questionCopyrights;
for (var i = 0; i < questionInstances.length; i++) {
var instance = questionInstances[i];
var qParams = params.questions[i].params;
questionCopyrights = undefined;
if (instance.getCopyrights !== undefined) {
// Use the instance's own copyright generator
questionCopyrights = instance.getCopyrights();
}
if (questionCopyrights === undefined) {
// Create a generic flat copyright list
questionCopyrights = new H5P.ContentCopyrights();
H5P.findCopyrights(questionCopyrights, qParams, contentId);
}
// Determine label
2016-03-17 14:09:13 +01:00
var label = (params.texts.questionLabel + ' ' + (i + 1));
if (qParams.contentName !== undefined) {
label += ': ' + qParams.contentName;
}
else if (instance.getTitle !== undefined) {
label += ': ' + instance.getTitle();
}
questionCopyrights.setLabel(label);
// Add info
info.addContent(questionCopyrights);
}
// Success video
var video;
if (params.endGame.successVideo !== undefined && params.endGame.successVideo.length > 0) {
video = params.endGame.successVideo[0];
if (video.copyright !== undefined) {
info.addMedia(new H5P.MediaCopyright(video.copyright));
}
}
// Fail video
if (params.endGame.failVideo !== undefined && params.endGame.failVideo.length > 0) {
video = params.endGame.failVideo[0];
if (video.copyright !== undefined) {
info.addMedia(new H5P.MediaCopyright(video.copyright));
}
}
return info;
2014-06-26 16:23:12 +02:00
};
2015-02-04 16:40:29 +01:00
this.getQuestions = function() {
return questionInstances;
2013-04-04 15:44:06 +02:00
};
2015-02-04 16:40:29 +01:00
this.showSolutions = function() {
renderSolutions = true;
};
/**
* Stop the given element's playback if any.
*
* @param {object} instance
*/
var pauseMedia = function (instance) {
try {
if (instance.pause !== undefined &&
(instance.pause instanceof Function ||
typeof instance.pause === 'function')) {
instance.pause();
}
}
catch (err) {
// Prevent crashing, log error.
H5P.error(err);
}
};
/**
* Returns the complete state of question set and sub-content
*
* @returns {Object}
*/
this.getCurrentState = function () {
var state = {
progress: currentQuestion,
answers: questionInstances.map(function (qi) {
return qi.getCurrentState();
})
};
return state;
};
};
2015-02-04 16:40:29 +01:00
H5P.QuestionSet.prototype = Object.create(H5P.EventDispatcher.prototype);
H5P.QuestionSet.prototype.constructor = H5P.QuestionSet;