-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz.html
352 lines (308 loc) · 7.89 KB
/
quiz.html
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<html>
<head>
<link rel="stylesheet" href="/css/global.css" type="text/css" media="all" />
<script type="text/javascript" src="javascript/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="javascript/jquery.cookie.js"></script>
<script>
$(document).ready(function() {
console.log('test');
});
var base_url = "http://vocabgrab.com";
//var base_url = "http://localhost:3000";
function success_list(data) {
$('#div_list_details').html(data.list_name);
}
function error_list() {
}
function request_current_list() {
var params = {
type: 'GET',
url: base_url + '/api/current_list.json',
success: success_list,
error: error_list
}
xhr = $.ajax(params);
}
// updates total points earned in display
function update_points(total_points) {
$('#div_points').html(total_points);
}
function success_points(data) {
update_points(data.total_points);
}
function error_points() {
}
// sets current word in cookie
function get_current_question_id() {
return $.cookie("current_question_id");
}
function persist_current_question_id(question_id) {
$.cookie("current_question_id", question_id, { expires: 7 });
}
// clears current word in cookie
function clear_current_question_id(){
$.cookie("current_question_id", null);
}
function answer_question(evt) {
var selected = $('#quiz_form input:checked');
var question_field = $('#quiz_form input:hidden');
var response_id = selected.val();
var question_id = question_field.val();
send_response(question_id, response_id);
}
function success_response(data) {
clear_current_question_id();
$("#prompt").empty();
$("#choices").empty();
var html = "<br/>";
if(data.is_correct) {
html += "You got it right,";
html += " " + data.points_earned + " points earned.<br/>";
update_points(data.total_points);
} else {
html += "You messed up, the correct definition for <b>" + data.word + "</b> is: " + data.answer + "<br/>";
}
html += "<br/><input type='button' id='next_question' value='gimme more'/>";
$("#prompt").html(html);
$("#next_question").click(function() {request_quiz();});
}
function error_response() {
}
function send_response(question_id, response_id) {
var params = {
type: 'GET',
url: base_url + '/api/response.json',
data: {question_id: question_id, response_id: response_id},
success: success_response,
error: error_response
}
xhr = $.ajax(params);
}
function success_quiz(data) {
$('#prompt').empty();
$('#choices').empty();
var question_id = data[0]['question_id'];
var word = data[0]['word'];
persist_current_question_id(question_id);
if(data.length > 0) {
$('#prompt').append('What is the definition of <b>' + word + '</b>?<br/>');
$('#choices').append("<form id='quiz_form'>");
} else {
$('#content').append("Nothing to quiz you, this should never happen!<br/>");
}
$('#choices #quiz_form').append("<ul>");
for(var i=0; i < data.length; i++) {
var row = data[i];
var word = row['value'];
var choice_id = row['choice_id'];
var input_txt = "<li><input type='radio' name='response' value='" + choice_id + "'/> " + word + "</li>";
var input = $(input_txt);
$('#choices #quiz_form').append(input);
}
$('#choices #quiz_form').append("</ul>");
// append question_id to hidden field
var question_id = data[0]['question_id'];
var question_id_field = "<input type='hidden' name='question_id' value='" + question_id + "'/>";
var hidden_input = $(question_id_field);
$('#choices #quiz_form').append(hidden_input);
// bind user click to radio button
$('input:radio').click(answer_question);
toggle_question_visible();
}
function toggle_question_visible() {
hide_loading('toggle question visible');
$('#content').show();
}
function show_loading(requestor) {
$('#loading').show();
}
function hide_loading(requestor) {
$('#loading').hide();
}
function error_words(xhr, status, error) {
}
function request_quiz() {
show_loading('request quiz');
var params = {
type: 'GET',
url: base_url + '/api/question.json',
success: success_quiz,
error: error_words
}
var current_question_id = get_current_question_id();
if(current_question_id != null) {
params['data'] = {question_id: current_question_id};
}
xhr = $.ajax(params);
}
function request_points() {
var params = {
type: 'GET',
url: base_url + '/api/points.json',
success: success_points,
error: error_points
}
xhr = $.ajax(params);
}
function error_auth_check(xhr, status) {
console.log("auth fail", xhr, status);
}
function success_auth_check(data) {
if(data.logged_in === true) {
valid_session();
} else {
$('#login').show();
$('#login_email').focus();
}
}
function auth_check() {
var params = {
type: 'GET',
url: base_url + '/api/auth_check',
success: success_auth_check,
error: error_auth_check
}
xhr = $.ajax(params);
}
function success_auth(data) {
$('#user_alerts').empty();
if(data.logged_in === true){
valid_session();
} else {
$('#user_alerts').html("Login didn't go through, try again.<br/>");
}
}
function error_auth() {
}
function success_signup(data) {
$('#user_alerts').empty();
$('#signup').hide();
if(data.logged_in === true){
valid_session();
} else {
$('#user_alerts').html("Signup didn't go through, try again.<br/>");
}
}
function error_signup() {
}
function auth(email, password) {
var params = {
type: 'GET',
url: base_url + '/api/auth',
success: success_auth,
error: error_auth,
data: {'email': email, 'password': password}
}
xhr = $.ajax(params);
}
function signup(email, password) {
var params = {
type: 'GET',
url: base_url + '/api/signup',
success: success_signup,
error: error_signup,
data: {'email': email, 'password': password}
}
xhr = $.ajax(params);
}
/**
* User is logged in, show words or question
*/
function valid_session() {
$('#login').hide();
$('#div_nav').show();
request_points();
request_current_list();
request_quiz();
}
function no_session() {
// let user login from the extension
$('#login').show();
}
$(document).ready(
function() {
$('#login_form').submit(function(evt) {
var email = $('#login_email').val();
var password = $('#login_password').val();
auth(email, password);
return false;
});
$('#signup_form').submit(function() {
var email = $('#signup_email').val();
var password = $('#signup_password').val();
signup(email, password);
return false;
});
$('#link_signup').click(function() {
$('#login').hide();
$('#signup').show();
$('#signup_email').focus();
});
$('#home').click(function() {
var props = {url: base_url};
chrome.tabs.create(props, function() {});
});
// check if user is logged in, branch logic from their
auth_check();
}
);
</script>
</head>
<body>
<div id='wrapper'>
<div id='user_alerts'></div>
<div id='content'>
<div id='prompt'>
</div>
<div id='choices'>
</div>
</div>
<div id='div_nav'>
<div id='home'></div>
<div id='div_list_details'></div>
<div id='div_points'></div>
</div>
<div id='login'>
<form action='/api/auth' method='post' id='login_form'>
<table>
<tr>
<td>Email:</td>
<td><input type='text', name='email' id='login_email'/></td>
</tr>
<tr>
<td>Password:</td>
<td> <input type='password', name='password' id='login_password'/> </td>
</tr>
<tr>
<td></td>
<td> <input type="submit" value="login" id="button_login" /></td>
</tr>
</table>
</form>
New to VocabGrab? <a href="#" id='link_signup'> Signup </a> in 5 seconds.
</div>
<div id='signup'>
Almost there! <br/><br/>
<form action='/api/signup' method='post' id='signup_form'>
<table>
<tr>
<td>Email:</td>
<td><input type='text', name='email' id='signup_email'/></td>
</tr>
<tr>
<td>Password:</td>
<td> <input type='password', name='password' id='signup_password'/> </td>
</tr>
<tr>
<td></td>
<td> <input type="submit" value="Signup" id="button_signup" /> </td>
</tr>
</table>
</form>
</div>
<div id='loading'>
loading question...
</div>
</div>
</body>
</html>