(function($){
$.fn.extend({
jQuizler: function(questions) {
// @todo избавиться от возможности двойного клика по кнопкам "cследующий", "предыдущий", "результат"
// @todo добавить возможность выбора нескольких вариантов ответа
// @todo добавить возможность внедрения названия теста на страницу результатов
// @todo избавиться от зависимости Bootsrap CSS
// @todo поработать над стилем элементов кнопок и прогресса
// @todo добавить кнопки "поделиться" или "like" для разных соц сетей ("ВКонтакте", "Однолкассники", ...)
// @todo разобраться с проблемой клика по элементу кнопки на странице результатов
// @todo переписать код, сделать его более изящным и чистым
// @todo сделать так, чтобы плагин можно было применять для 2х и более тестов на страницу
// @todo создать online инструмент для формирования и редактирования вопросов
if (questions == null)
throw 'No questions was provided.';
var reviewQuiz = false;
var percentage = 0;
var percentPiece = 100 / questions.length;
var rightAnswers = 0;
$(this).html("
" + $(this).html() + "
");
$(this).click(function(){
$(this).off('click');
$(".intro").hide();
$(this).css("text-align", "left");
$('.progress').css("display", "block");
var question = $("#question-1");
question.css({opacity : '0', height : '0px'});
question.animate({
opacity : '1',
height : '100%'
}, 500, function(e){});
question.css('display', 'block');
percentage += percentPiece;
$(".progress div").css("width", percentage + "%");
});
(function($){
$.shuffle = function(arr) {
for(
var j, x, i = arr.length; i;
j = parseInt(Math.random() * i),
x = arr[--i], arr[i] = arr[j], arr[j] = x
);
return arr;
}
})(jQuery);
String.prototype.replaceAll = function (find, replace) {
var str = this;
return str.replace(new RegExp(find, 'g'), replace);
};
return this.each(function(){
var html = "";
html += "";
$.each(questions, function(index, question){
html += "
";
html += "
Вопрос " + (index + 1) + ' из ' + questions.length + "
";
html += question.question;
var correctAnswers = [];
for (var i = 0; i < question.correct.length; i++)
correctAnswers.push(question.answers[question.correct[i] - 1]);
question.answers = $.shuffle(question.answers);
var correctAnswersNewIndexes = [];
for (var i = 0; i < question.correct.length; i++)
correctAnswersNewIndexes.push(question.answers.indexOf(correctAnswers[i]));
question.correct = correctAnswersNewIndexes;
// for (var i = 0; i < question.correct.length; i++)
// console.log(question.answers[correctAnswersNewIndexes[i]]);
html += "
";
for (var i = 0; i < question.answers.length; i++)
html += "
" + question.answers[i] + "
";
html += "
";
html += "
";
html += "
Выберите ответ
";
if (index != 0) {
html += "";
}
if (index != questions.length-1) {
html += "";
html += "";
} else {
html += "";
html += "";
}
html += "
";
html += "";
});
html += "
";
html += "";
html += "
";
// /html += "";
$(this).append(html);
$("div[id*='question-'] li").click(function(){
if (!reviewQuiz) {
/*$(this).siblings().removeClass("selected");
$(this).toggleClass("selected");*/
$(this).siblings().removeClass("btn-info");
$(this).toggleClass("btn-info");
}
});
$(".final").click(function(e){
var div = $(e.target).closest("div[id*='question-']");
var userAnswer = div.find("li.btn-info");
if (userAnswer.index() == -1 && !reviewQuiz) {
var notice = div.find(".notice");
notice.css('opacity', '0');
notice.animate({
opacity: 1
}, 500, function(){});
div.find(".notice").css('display', 'block');
} else if (!reviewQuiz) {
div.find(".notice").css('display', 'none');
percentage += percentPiece;
if (percentage > 100) percentage = 100;
$("#percent").css("width", percentage + "%");
var resultHTML = "