topic.js
5.48 KB
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
$.fn.extend({
animateCSS: function (animation, run_on_end) {
var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
this.addClass('animated ' + animation).one(animationEnd, function() {
$(this).removeClass('animated ' + animation);
if (run_on_end !== undefined) {
run_on_end();
}
});
}
});
function showTriesLeft(tries) {
var msg;
if (tries > 1)
msg = "(" + tries + " tentativas)";
else if (tries == 1)
msg = "(1 tentativa)";
else
msg = "";
$("#tries").html(msg);
}
// Get current question
function getQuestion() {
$("#comments").html("").hide();
$("#submit").addClass("disabled");
$.ajax({
type: "GET",
url: "/question",
dataType: "json", // expected from server
success: updateQuestion,
error: function() {alert("O servidor não responde.");}
});
}
// updates question in the page according to the response given by the server
function updateQuestion(response) {
var method = response["method"];
var params = response["params"];
$("#right, #wrong").hide();
switch (method) {
case "new_question":
new_question(params["type"], params["question"], params["tries"], params["progress"]);
break;
case "finished_topic":
$('#submit, #comments, #solution').remove();
$("#content").html(response["params"]["question"]).animateCSS('tada');
$('#topic_progress').css('width', '100%').attr('aria-valuenow', 100);
setTimeout(function(){window.location.replace('/course/');}, 2000);
break;
}
}
function new_question(type, question, tries, progress) {
window.scrollTo(0, 0);
$("#submit").hide();
$("#question_div").animateCSS('bounceOutUp', function() {
$("#question_div").html(question);
MathJax.typeset();
$("#question_div").animateCSS('bounceInDown', function() {
showTriesLeft(tries);
$("#submit").removeClass("disabled").show();
});
});
var btntext = (type == "information") ? "Continuar" : "Responder";
$("#submit").html(btntext).off().click(postAnswer);
$('#topic_progress').css('width', (100*progress)+'%').attr('aria-valuenow', 100*progress);
if (type == "radio") {
$(".list-group-item").click(function (e) {
var index = $(this).index();
$("div.list-group input:radio").eq(index).prop("checked", true);
});
}
// disable enter in some question types (prevent submission on enter)
$("input:text, input:radio, input:checkbox").keydown(function (e) {
if (e.keyCode == 13 || e.keyCode == 169) {
e.preventDefault();
return false;
}
});
}
// ===========================================================================
// Send answer and receive a response with the result of the correction.
// The response can be right, try_again or wrong.
// A new question is not loaded. To load questions a GET is required.
function postAnswer() {
if (typeof editor === 'object')
editor.save();
$.ajax({
type: "POST",
url: "/question",
data: $("#question_form").serialize(), // {'a':10,'b':20},
dataType: "json", // expected from server
success: getFeedback,
error: function() {alert("O servidor não responde.");}
});
$("#submit").addClass("disabled").html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>');
$("fieldset").prop("disabled", true);
}
function getFeedback(response) {
var method = response["method"];
var params = response["params"];
if (params['type'] == "information") {
getQuestion();
return;
}
switch (method) {
case "right":
$('#wrong').hide();
$('#comments').html(params['comments']).show();
$('#solution_right').html(params['solution']);
MathJax.typeset();
$('#right').show().animateCSS('zoomIn', function(){
$("#submit").html("Continuar").removeClass("disabled").off().click(getQuestion);
});
break;
case "try_again":
$('#comments').html(params['comments']).show();
MathJax.typeset();
$('#topic_progress').css('width', (100*params["progress"])+'%').attr('aria-valuenow', 100*params["progress"]);
$('#question_div').animateCSS('shake', function() {
showTriesLeft(params["tries"]);
$("fieldset").prop("disabled", false);
$("#submit").html("Responder").removeClass("disabled");
});
break;
case "wrong":
$("fieldset").prop("disabled", true);
$('#right').hide();
$('#comments').html(params['comments']).show();
$('#solution_wrong').html(params['solution']);
MathJax.typeset();
$('#question_div').animateCSS('shake', function() {
showTriesLeft(params["tries"]);
$('#wrong').show().animateCSS('zoomIn', function() {
$("#submit").html("Continuar").removeClass("disabled").off().click(getQuestion);
});
});
$('#topic_progress').css('width', (100*params["progress"])+'%').attr('aria-valuenow', 100*params["progress"]);
break;
case "invalid":
alert(params["msg"]);
break;
}
}
// ===========================================================================
$(document).ready(function() {
getQuestion();
$("#submit").click(postAnswer);
});