summaryrefslogtreecommitdiffstats
path: root/theme/javascript/core/quiz.js
blob: d34af48f9b715d9b44b5f10b20114efe89abe2b4 (plain)
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
define([
    "jQuery",
    "utils/execute",
    "core/events",
    "core/state"
], function($, execute, events, state){
    // Bind an exercise
    var prepareQuiz = function($quiz) {

        $quiz.find(".quiz-answers input").click(function(e) {
            e.preventDefault();
        });

        // Submit: test code
        $quiz.find(".action-submit").click(function(e) {
            e.preventDefault();
            events.trigger("exercise.submit", {type: "quiz"});
            $quiz.find("tr.alert-danger,li.alert-danger").removeClass("alert-danger");
            $quiz.find(".alert-success,.alert-danger").addClass("hidden");

            $quiz.find(".question").each(function(q) {
                var result = true;

                var $questions = $(this).find(".question-content").find("input[type=radio], input[type=checkbox]");
                var $answers = $(this).find(".question-answers").find("input[type=radio], input[type=checkbox]");

                $questions.each(function(i) {
                    var correct = $(this).is(":checked") === $answers.slice(i).first().is(":checked");
                    result = result && correct;
                    if (!correct) {
                        $(this).closest("tr, li").addClass("alert-danger");
                    }
                });
                $(this).find(result ? "div.alert-success" : "div.alert-danger").toggleClass("hidden");
            });

        });

        $quiz.find(".action-solution").click(function(e) {
            e.preventDefault();
            $quiz.find(".question-content, .question-answers").toggleClass("hidden");
        });
    };

    // Prepare all exercise
    var init = function() {
        state.$book.find("section.quiz").each(function() {
            prepareQuiz($(this));
        });
    };

    return {
        init: init,
        prepare: prepareQuiz
    };
});