2013-01-08 13:04:15 +01:00
|
|
|
// 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.
|
|
|
|
// }
|
2013-01-13 09:43:55 +01:00
|
|
|
//
|
|
|
|
// Events provided:
|
|
|
|
// - h5pQuestionSetFinished: Triggered when a question is finished. (User presses Finish-button)
|
2013-01-08 13:04:15 +01:00
|
|
|
window.H5P = window.H5P || {};
|
|
|
|
|
2013-01-22 21:59:14 +01:00
|
|
|
H5P.QuestionSet = function (options, contentId) {
|
2013-01-08 13:04:15 +01:00
|
|
|
if ( !(this instanceof H5P.QuestionSet) )
|
2013-01-22 21:59:14 +01:00
|
|
|
return new H5P.QuestionSet(options, contentId);
|
2013-01-08 13:04:15 +01:00
|
|
|
|
2013-01-13 09:43:55 +01:00
|
|
|
var $ = H5P.jQuery;
|
2013-01-22 21:59:14 +01:00
|
|
|
var cp = H5P.getContentPath(contentId);
|
2013-01-13 09:43:55 +01:00
|
|
|
|
2013-01-08 13:04:15 +01:00
|
|
|
var texttemplate = '' +
|
2013-01-25 10:22:47 +01:00
|
|
|
'<% if (introPage.showIntroPage) { %>' +
|
|
|
|
'<div class="intro-page">' +
|
|
|
|
' <div class="title"><%= introPage.title %></div>' +
|
|
|
|
' <div class="introduction"><%= introPage.introduction %></div>' +
|
2013-01-30 19:55:33 +01:00
|
|
|
' <div class="buttons"><a id="qs-startbutton" class="button"><%= introPage.startButtonText %></a></div>' +
|
2013-01-25 10:22:47 +01:00
|
|
|
'</div>' +
|
|
|
|
'<%} %>' +
|
|
|
|
'<div class="questionset hidden">' +
|
2013-01-08 16:16:19 +01:00
|
|
|
' <div class="title"><%= title %></div>' +
|
2013-01-08 13:04:15 +01:00
|
|
|
' <% for (var i=0; i<questions.length; i++) { %>' +
|
2013-01-09 16:26:52 +01:00
|
|
|
' <div class="question-container" id="q-<%= i %>">' +
|
2013-02-07 13:11:42 +01:00
|
|
|
' <div><%= questions[i].library %></div>' +
|
2013-01-08 13:04:15 +01:00
|
|
|
' </div>' +
|
|
|
|
' <% } %>' +
|
2013-01-25 10:22:47 +01:00
|
|
|
' <div class="qs-footer">' +
|
|
|
|
' <div class="qs-progress">' +
|
2013-01-08 13:04:15 +01:00
|
|
|
' <% if (progressType == "dots") { %>' +
|
2013-01-09 16:26:52 +01:00
|
|
|
' <div class="dots-container">' +
|
|
|
|
' <% for (var i=0; i<questions.length; i++) { %>' +
|
|
|
|
' <span class="progress-dot unanswered" id="qdot-<%= i %>"></span>' +
|
|
|
|
' <%} %>' +
|
|
|
|
' </div>' +
|
2013-01-08 13:04:15 +01:00
|
|
|
' <% } else if (progressType == "textual") { %>' +
|
2013-01-08 16:16:19 +01:00
|
|
|
' <span class="progress-text"></span>' +
|
2013-01-08 13:04:15 +01:00
|
|
|
' <% } %>' +
|
|
|
|
' </div>' +
|
2013-01-30 19:55:33 +01:00
|
|
|
' <a class="prev button"><%= texts.prevButton %></a>' +
|
|
|
|
' <a class="next button"><%= texts.nextButton %></a>' +
|
|
|
|
' <a class="finish button"><%= texts.finishButton %></a>' +
|
2013-01-08 13:04:15 +01:00
|
|
|
' </div>' +
|
|
|
|
'</div>' +
|
|
|
|
'';
|
2013-01-13 09:43:55 +01:00
|
|
|
var resulttemplate = '' +
|
|
|
|
'<div class="questionset-results">' +
|
|
|
|
' <div class="greeting"><%= greeting %></div>' +
|
|
|
|
' <div class="score <%= scoreclass %>"><%= score %></div>' +
|
2013-01-25 10:22:47 +01:00
|
|
|
' <div class="resulttext <%= scoreclass %>"><%= resulttext %></div>' +
|
2013-02-07 14:08:44 +01:00
|
|
|
' <div class="buttons"><a class="button qs-finishbutton"><%= finishButtonText %></a></div>' +
|
2013-01-13 09:43:55 +01:00
|
|
|
'</div>' +
|
2013-02-07 14:08:44 +01:00
|
|
|
'';
|
2013-01-13 09:43:55 +01:00
|
|
|
|
|
|
|
var that = this;
|
2013-01-08 13:04:15 +01:00
|
|
|
var defaults = {
|
|
|
|
title: "",
|
|
|
|
randomOrder: false,
|
|
|
|
initialQuestion: 0,
|
2013-01-13 09:43:55 +01:00
|
|
|
backgroundImage: "",
|
2013-01-14 18:36:07 +01:00
|
|
|
progressType: 'dots',
|
2013-01-13 09:43:55 +01:00
|
|
|
passPercentage: 50,
|
2013-01-08 16:16:19 +01:00
|
|
|
questions: [],
|
2013-01-13 09:43:55 +01:00
|
|
|
introPage: {
|
|
|
|
showIntroPage: true,
|
|
|
|
title: "Welcome",
|
|
|
|
introduction: "Click start to start.",
|
|
|
|
startButtonText: "Start"
|
|
|
|
},
|
2013-01-08 16:16:19 +01:00
|
|
|
texts: {
|
|
|
|
prevButton: "Previous",
|
|
|
|
nextButton: "Next",
|
|
|
|
finishButton: "Finish",
|
|
|
|
textualProgress: "Question: @current of @total questions"
|
2013-01-13 09:43:55 +01:00
|
|
|
},
|
|
|
|
endGame: {
|
|
|
|
showResultPage: true,
|
|
|
|
resultPage: {
|
2013-01-23 23:05:22 +01:00
|
|
|
successGreeting: "Congratulations!",
|
2013-01-13 09:43:55 +01:00
|
|
|
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: "@score/@total",
|
|
|
|
finishButtonText: "Finish"
|
|
|
|
},
|
|
|
|
animations: {
|
|
|
|
showAnimations: false,
|
2013-01-23 23:05:22 +01:00
|
|
|
successVideo: undefined,
|
2013-02-06 15:49:15 +01:00
|
|
|
failVideo: undefined
|
2013-01-13 09:43:55 +01:00
|
|
|
}
|
2013-01-08 16:16:19 +01:00
|
|
|
}
|
2013-01-08 13:04:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var template = new EJS({text: texttemplate});
|
2013-01-13 09:43:55 +01:00
|
|
|
var endTemplate = new EJS({text: resulttemplate});
|
|
|
|
var params = $.extend({}, defaults, options);
|
2013-01-08 13:04:15 +01:00
|
|
|
|
2013-01-09 16:26:52 +01:00
|
|
|
var currentQuestion = 0;
|
2013-01-08 13:04:15 +01:00
|
|
|
var questionInstances = new Array();
|
|
|
|
var allQuestionsAnswered = false;
|
2013-01-14 19:04:07 +01:00
|
|
|
var $myDom;
|
2013-01-08 13:04:15 +01:00
|
|
|
|
|
|
|
if (params.randomOrder) {
|
|
|
|
// TODO: Randomize order of questions
|
|
|
|
console.log("TODO: Randomize order of questions");
|
|
|
|
}
|
|
|
|
|
2013-01-13 09:43:55 +01:00
|
|
|
// Instantiate question instances
|
|
|
|
for (var i=0; i<params.questions.length; i++) {
|
2013-02-07 18:02:34 +01:00
|
|
|
var question = params.questions[i];
|
2013-01-13 09:43:55 +01:00
|
|
|
// TODO: Render on init, inject in template.
|
2013-02-07 18:02:34 +01:00
|
|
|
var tmp = new (H5P.classFromName(question.library.library))(question.library.params, contentId);
|
2013-01-13 09:43:55 +01:00
|
|
|
questionInstances.push(tmp);
|
|
|
|
}
|
|
|
|
|
2013-02-06 20:25:06 +01:00
|
|
|
// Update button state.
|
|
|
|
var _updateButtons = function () {
|
2013-01-09 16:26:52 +01:00
|
|
|
var answered = true;
|
|
|
|
for (var i = questionInstances.length - 1; i >= 0; i--) {
|
|
|
|
answered = answered && (questionInstances[i]).getAnswerGiven();
|
|
|
|
}
|
2013-02-06 20:25:06 +01:00
|
|
|
|
|
|
|
if (currentQuestion === 0) {
|
|
|
|
$('.prev.button', $myDom).hide();
|
|
|
|
} else {
|
|
|
|
$('.prev.button', $myDom).show();
|
|
|
|
}
|
|
|
|
if (currentQuestion == (params.questions.length - 1)) {
|
|
|
|
$('.next.button', $myDom).hide();
|
|
|
|
if (answered) {
|
|
|
|
$('.finish.button', $myDom).show();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$('.next.button', $myDom).show();
|
|
|
|
$('.finish.button', $myDom).hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2013-01-09 16:26:52 +01:00
|
|
|
|
2013-01-08 13:04:15 +01:00
|
|
|
var _showQuestion = function (questionNumber) {
|
|
|
|
// Sanitize input.
|
|
|
|
if (questionNumber < 0) { questionNumber = 0; }
|
|
|
|
if (questionNumber >= params.questions.length) { questionNumber = params.questions.length - 1; }
|
|
|
|
|
2013-02-06 20:25:06 +01:00
|
|
|
// Hide all questions
|
2013-01-14 19:04:07 +01:00
|
|
|
$('.question-container', $myDom).hide();
|
2013-01-08 13:04:15 +01:00
|
|
|
|
|
|
|
// Reshow the requested question
|
2013-01-14 19:04:07 +01:00
|
|
|
$('#q-' + questionNumber, $myDom).show();
|
2013-01-08 13:04:15 +01:00
|
|
|
|
|
|
|
// Update progress indicator
|
|
|
|
// Test if current has been answered.
|
|
|
|
if (params.progressType == 'textual') {
|
2013-01-14 19:04:07 +01:00
|
|
|
$('.progress-text', $myDom).text(params.texts.textualProgress.replace("@current", questionNumber+1).replace("@total", params.questions.length));
|
2013-01-08 13:04:15 +01:00
|
|
|
} else {
|
2013-01-13 09:43:55 +01:00
|
|
|
// Set currentNess
|
2013-01-14 19:04:07 +01:00
|
|
|
$('.progress-dot.current', $myDom).removeClass('current');
|
|
|
|
$('#qdot-' + questionNumber, $myDom).addClass('current');
|
2013-01-08 13:04:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remember where we are
|
|
|
|
currentQuestion = questionNumber;
|
2013-02-06 20:25:06 +01:00
|
|
|
_updateButtons();
|
2013-01-08 13:04:15 +01:00
|
|
|
return currentQuestion;
|
|
|
|
};
|
|
|
|
|
2013-02-07 14:08:44 +01:00
|
|
|
var _displayEndGame = function () {
|
|
|
|
// Get total score.
|
|
|
|
var finals = getScore();
|
|
|
|
var totals = totalScore();
|
|
|
|
var scoreString = params.endGame.resultPage.scoreString.replace("@score", finals).replace("@total", totals);
|
|
|
|
var success = ((100 * finals / totals) >= params.passPercentage);
|
|
|
|
var eventData = {
|
|
|
|
score: scoreString,
|
|
|
|
passed: success
|
|
|
|
};
|
|
|
|
var displayResults = function () {
|
|
|
|
if (!params.endGame.showResultPage) {
|
|
|
|
$(returnObject).trigger('h5pQuestionSetFinished', eventData);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var eparams = {
|
|
|
|
greeting: (success ? params.endGame.resultPage.succesGreeting : params.endGame.resultPage.failGreeting),
|
|
|
|
score: scoreString,
|
|
|
|
scoreclass: (success ? 'success' : 'fail'),
|
|
|
|
resulttext: (success ? params.endGame.resultPage.successComment : params.endGame.resultPage.failComment),
|
|
|
|
finishButtonText: params.endGame.resultPage.finishButtonText
|
|
|
|
};
|
|
|
|
|
|
|
|
// Show result page.
|
|
|
|
$myDom.children().hide();
|
|
|
|
$myDom.append(endTemplate.render(eparams));
|
|
|
|
$('.qs-finishbutton').click(function (ev) {
|
|
|
|
$(returnObject).trigger('h5pQuestionSetFinished', eventData);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (params.endGame.animations.showAnimations) {
|
|
|
|
var videoData = success ? params.endGame.animations.successVideo : params.endGame.animations.failVideo;
|
|
|
|
if (videoData) {
|
2013-02-07 18:01:08 +01:00
|
|
|
H5P.playVideo($myDom, videoData, cp, function () {
|
2013-02-07 14:08:44 +01:00
|
|
|
displayResults();
|
2013-02-07 18:01:08 +01:00
|
|
|
});
|
|
|
|
return;
|
2013-02-07 14:08:44 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-07 18:01:08 +01:00
|
|
|
// Trigger finished event.
|
|
|
|
displayResults();
|
2013-02-07 14:08:44 +01:00
|
|
|
};
|
|
|
|
|
2013-01-08 13:04:15 +01:00
|
|
|
// Function for attaching the multichoice to a DOM element.
|
2013-01-22 21:59:14 +01:00
|
|
|
var attach = function (target) {
|
|
|
|
if (typeof(target) == "string") {
|
2013-02-07 14:08:44 +01:00
|
|
|
$myDom = $("#" + target);
|
2013-01-22 21:59:14 +01:00
|
|
|
} else {
|
2013-02-07 14:08:44 +01:00
|
|
|
$myDom = $(target);
|
2013-01-22 21:59:14 +01:00
|
|
|
}
|
|
|
|
|
2013-01-08 13:04:15 +01:00
|
|
|
// Render own DOM into target.
|
2013-01-22 21:59:14 +01:00
|
|
|
$myDom.html(template.render(params)).css({
|
2013-02-06 15:49:15 +01:00
|
|
|
backgroundImage: 'url(' + cp + params.backgroundImage.path + ')'
|
2013-01-22 21:59:14 +01:00
|
|
|
});
|
2013-01-08 13:04:15 +01:00
|
|
|
|
|
|
|
// Attach questions
|
2013-01-13 09:43:55 +01:00
|
|
|
for (var i=0; i<questionInstances.length; i++) {
|
2013-02-07 18:02:34 +01:00
|
|
|
var question = questionInstances[i];
|
2013-01-08 13:04:15 +01:00
|
|
|
// TODO: Render on init, inject in template.
|
2013-02-07 18:02:34 +01:00
|
|
|
question.attach('q-' + i);
|
|
|
|
$(question).on('h5pQuestionAnswered', function (ev) {
|
2013-01-14 19:04:07 +01:00
|
|
|
$('#qdot-' + currentQuestion, $myDom).removeClass('unanswered').addClass('answered');
|
2013-02-06 20:25:06 +01:00
|
|
|
_updateButtons();
|
2013-01-13 09:43:55 +01:00
|
|
|
});
|
2013-02-07 18:02:34 +01:00
|
|
|
if (question.getAnswerGiven()) {
|
2013-01-15 08:17:59 +01:00
|
|
|
$('#qdot-'+i, $myDom).removeClass('unanswered').addClass('answered');
|
|
|
|
}
|
2013-01-08 13:04:15 +01:00
|
|
|
}
|
|
|
|
|
2013-01-16 13:42:51 +01:00
|
|
|
$('#qs-startbutton').click(function (ev) {
|
|
|
|
$(this).parents('.intro-page').hide();
|
2013-01-25 10:22:47 +01:00
|
|
|
$('.questionset', $myDom).removeClass('hidden');
|
2013-01-16 13:42:51 +01:00
|
|
|
});
|
|
|
|
|
2013-01-08 13:04:15 +01:00
|
|
|
// Set event listeners.
|
2013-01-15 08:17:59 +01:00
|
|
|
$('.progress-dot', $myDom).click(function (ev) {
|
2013-02-07 14:08:44 +01:00
|
|
|
var idx = parseInt($(this).attr('id').split('-')[1], 10);
|
2013-01-15 08:17:59 +01:00
|
|
|
_showQuestion(idx);
|
|
|
|
});
|
2013-01-14 19:04:07 +01:00
|
|
|
$('.next.button', $myDom).click(function (ev) {
|
2013-01-08 13:04:15 +01:00
|
|
|
_showQuestion(currentQuestion + 1);
|
|
|
|
});
|
2013-01-14 19:04:07 +01:00
|
|
|
$('.prev.button', $myDom).click(function (ev) {
|
2013-01-08 13:04:15 +01:00
|
|
|
_showQuestion(currentQuestion - 1);
|
|
|
|
});
|
2013-01-14 19:04:07 +01:00
|
|
|
$('.finish.button', $myDom).click(function (ev) {
|
2013-02-07 14:08:44 +01:00
|
|
|
_displayEndGame();
|
2013-01-09 16:26:52 +01:00
|
|
|
});
|
2013-01-08 13:04:15 +01:00
|
|
|
|
|
|
|
// Hide all but initial Question.
|
|
|
|
_showQuestion(params.initialQuestion);
|
2013-02-06 20:25:06 +01:00
|
|
|
_updateButtons();
|
2013-01-08 13:04:15 +01:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2013-01-13 09:43:55 +01:00
|
|
|
// Get current score for questionset.
|
2013-01-08 13:04:15 +01:00
|
|
|
var getScore = function () {
|
|
|
|
var score = 0;
|
|
|
|
for (var i = questionInstances.length - 1; i >= 0; i--) {
|
|
|
|
score += questionInstances[i].getScore();
|
|
|
|
}
|
|
|
|
return score;
|
|
|
|
};
|
|
|
|
|
2013-01-13 09:43:55 +01:00
|
|
|
// Get total score possible for questionset.
|
|
|
|
var totalScore = function () {
|
|
|
|
var score = 0;
|
|
|
|
for (var i = questionInstances.length - 1; i >= 0; i--) {
|
|
|
|
score += questionInstances[i].totalScore();
|
|
|
|
}
|
|
|
|
return score;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Masquerade the main object to hide inner properties and functions.
|
|
|
|
var returnObject = {
|
2013-01-08 13:04:15 +01:00
|
|
|
attach: attach, // Attach to DOM object
|
|
|
|
getQuestions: function () {return questionInstances;},
|
|
|
|
getScore: getScore,
|
2013-01-13 09:43:55 +01:00
|
|
|
totalScore: totalScore,
|
2013-01-08 13:04:15 +01:00
|
|
|
defaults: defaults // Provide defaults for inspection
|
|
|
|
};
|
2013-01-13 09:43:55 +01:00
|
|
|
return returnObject;
|
2013-01-08 13:04:15 +01:00
|
|
|
};
|