-
Notifications
You must be signed in to change notification settings - Fork 2
/
phd.js
457 lines (347 loc) · 13.9 KB
/
phd.js
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Setup the back button warning:
window.onbeforeunload = function() {
return 'Hitting the back button will break this experiment, and is disabled, hit cancel';
}
// how much time do they get total? I Think 15?
var TIME_LIMIT_IN_MINUTES = 10;
var INTERVAL_IN_SECONDS_OF_HOW_OFTEN_TO_SHOW_OTHER_STUDENT_ACTIONS = 60; // one minute
var TIMEOUT_FOR_OTHER_PARTICIPANTS = 15*1000;
window.participant_id = undefined;
window.group_id = undefined;
$(document).ready(function() {
// 1) Collect Group and Participant ID
$(".participant").modal({
close:false,
overlayId: 'simplemodal-overlay',
containerId: 'simplemodal-container',
onShow: function (dialog) {
var modal = this;
$(".participant").show();
//accept enter
$('.participant input').keyup(function(event) {
if (event.keyCode == '13') {
$('.participant .button').click();
}
});
// if the user clicks "yes"
$('.participant .button').click(function () {
// clear error message
$('.participant .error').html("");
// close the dialog
window.participant_id = $('.participant #participant_id').val();
window.group_id = $('.participant #group_id').val();
if(window.participant_id && window.group_id && window.group_id.match(/^\d\d*$/)
&& window.participant_id.match(/^\d\d*$/)) {
modal.close();
// move on to the next step..
// HACK: setTimeout because we can only have one modal dialog
// at a time
setTimeout(function(){
window.show_instructions();
},10);
}else {
var error = "You must enter a valid group id and participant id"
$('.participant .error').html(error);
if(window.console){
window.console.error(error)
}
}
});
}
});
// Add instructions
window.show_instructions= function() {
var timer_started = false;
$('.instructions').modal({
close:true,
overlayClose:true,
overlayId: 'instructions-overlay',
containerId: 'instructions-container',
onClose: function(){
if(!timer_started){
window.start_timer();
timer_started=true;
$(".content").show()
// click on first essay
$($(".essay_link")[0]).click();
}
this.close();
},
onShow:function(){
var modal = this;
$("#instructions-container").click(function(){
if(!timer_started){
window.start_timer();
timer_started=true;
$(".content").show()
// click on first essay
$($(".essay_link")[0]).click();
}
modal.close();
closed = true;
})
}
});
}
$(".show_instructions").click(function(event){
event.stopPropagation();
show_instructions();
return false;
});
$(".quit").click(function(event){
event.stopPropagation();
$('.confirm_quit').modal({
close:true,
overlayClose:true,
overlayId: 'quit-overlay',
containerId: 'quit-container',
onShow:function(){
var modal = this;
$(".confirm_quit .quit_button").click(function(){
modal.close();
// let it close the modal, and open a new one
setTimeout(function(){ window.quit()}, 10);
})
$(".confirm_quit .cancel_button").click(function(){
modal.close();
})
}
});
return false;
});
window.quit = function(kwargs){
// make sure whatever is open is now closed
$.modal.close();
// stop the timer
window.stop_timer();
var msg = "";
if(kwargs && kwargs.timeout){
msg = "You ran out of time, but at least it's over :-)";
}
$(".finished .msg").html(msg)
// remove unload handler so we can reset things easily
window.onbeforeunload = undefined;
// basically 'callLater' something about these dialogs
setTimeout(function(){
$(".finished").modal({
close:false,
overlayClose:false,
overlayId: 'quit-overlay',
containerId: 'quit-container',
onShow:function(){
// Hack to steal the time from the timer.
var time = $(".timer").html();
// TODO: k you may need to change the url to the score_card
// Do an ajax request to get the body we are looking for
$.get( window.path_to_controller + '/score_card?participant_id=' + window.participant_id, function(data) {
$('.finished .body').html(data);
});
//Send the time to the backend
$.ajax({
type: 'POST',
url: "users/complete",
data: {
"participant_id": window.participant_id,
"time_to_complete" : time
}
});
}
});
},10);
};
window.show_other_student_actions = function(){
// stops the timer and shows what other students are up to
// basically overlays a model dialog and we work from there
window.stop_timer();
// make sure whatever is open is now closed
$.modal.close();
var closed = false;
$(".other_participants").modal({
close:true,
overlayClose:true,
overlayId: 'quit-overlay',
containerId: 'quit-container',
onShow:function(){
var modal = this;
// TODO: Russell you may need to change the url to the stats
// Do an ajax request to get the body we are looking for
$.get( '/users/stats?group=' + window.group_id, function(data) {
$('.other_participants .body').html(data);
});
$(".other_participants .button").click(function(){
modal.close();
closed = true;
window.start_timer();
})
setTimeout(function(){
if(!closed) {
modal.close();
}
}, TIMEOUT_FOR_OTHER_PARTICIPANTS);
},
onClose:function(){
this.close();
window.start_timer();
}
});
};
// TODO: don't start the timer until they have finished the practice test
//TODO: have a way to do the handler
// add click handlers for all the individual essays
var cached_essays = {}
$('.essay_link').click(function(event){
event.stopPropagation();
var essay_number = $(this).attr('rel');
var essay_id = "#essay_" + essay_number;
$('.essay_link').each(function(){
this.className ="essay_link"
});
// when clicked give it selected_link
this.className = "essay_link selected_link";
// hide all content
$('.content').hide()
$(".essay_content").hide();
if(cached_essays[essay_id]){
$(essay_id).show();
$('.content').show()
window.essay_id=essay_id;
}else {
$.get('/essay_base/show/' + essay_number, function(data) {
$('.content').append(data);
cached_essays[essay_id] = true;
// process the div
attach_event_handlers_to_essay(essay_id);
window.essay_id=essay_id;
$(essay_id).show();
$('.content').show()
});
}
});
function generate_box(span) {
var is_quota = (span.className.match(/quota/i) != null) ? true : false;
if(is_quota && window.__show_quota_items){
span.className = "correctme unmodified_quota";
}
else{
span.className = "correctme unmodified";
}
var original_text = $(span).html();
//clear it
$(span).html("");
var input = document.createElement("input")
input.type = "text";
input.value = original_text;
input.size = input.value.length*1.2;
var correct_answer = span.getAttribute('rel');
input.state = function() {
/*
Given a span like:
<span class='correctme' rel='correct string'>original string</span>
Tells us whether the string modified by the user is
- correct
- modified, but reverted to the original
- no change (un touched at all by the user)
- modifies, and returns difference
*/
var current_value = input.value;
if(input.modified) {
if(current_value == correct_answer) {
return { status: "correct", msg: correct_answer, style_class: "state_correct" }
}
else if(current_value == original_text) {
return {status: "incorrect - reverted to original", msg: "modified but returned to normal", style_class: "state_reverted"}
}
else {
// find the differnce between original and difference
// using the diffString Algorithm
var difference = diffString(original_text, current_value);
// modified, return difference
return {status: "incorrect - modified", msg: difference, style_class: "state_modified"}
}
}else {
return { status: "incorrect - no change", msg: current_value, style_class: "state_no_change"};
}
}
var change_function = function() {
// remember that the field was modified
input.modified = true;
// timeout 10ms so we can read the input value for changes
setTimeout(function() {
if(is_quota && window.__show_quota_items){
span.className = "correctme modified_quota";
}else{
span.className = "correctme modified";
}
if(input.value == original_text) {
if(is_quota && window.__show_quota_items){
span.className = "correctme unmodified_quota";
}else{
span.className = "correctme unmodified";
}
}
},10);
}
// handle change events
input.onkeyup = change_function;
input.onchange = change_function;
// Save changes with window.send_correction
$(input).change(function(){
var field_id = span.id;
var current_value = input.value;
// correct_answer
var original_value = original_text;
var essay = window.essay_id;
//is_quota
window.send_correction(
window.group_id,
window.participant_id,
window.essay_id,
field_id,
original_value,
current_value,
correct_answer,
is_quota
);
});
span.appendChild(input);
}
// given an essay ID attach all the correct event handlers
function attach_event_handlers_to_essay(essay_id){
// generate boxes
$(essay_id + " span.correctme").each(function(){
generate_box(this);
});
}
// timer related functions and variables
var timer = null;
var seconds = 0;
window.start_timer = function(){
window.stop_timer();
timer = setInterval(function(){
seconds +=1;
var minutes_to_render = TIME_LIMIT_IN_MINUTES - parseInt(seconds / 60) - 1
var seconds_to_render = 60 - parseInt(seconds % 60)
var spacer = '0';
if(seconds_to_render>9){
spacer = ''
}
// render the seconds
$(".timer").html(minutes_to_render + ":" + spacer + seconds_to_render);
// show other student scores
if(seconds % INTERVAL_IN_SECONDS_OF_HOW_OFTEN_TO_SHOW_OTHER_STUDENT_ACTIONS == 0 && window.__show_other_student_actions){
window.stop_timer();
window.show_other_student_actions();
}
if(seconds == TIME_LIMIT_IN_MINUTES * 60){
window.stop_timer();
window.quit({'timeout' : true});
}
},1000)
};
window.stop_timer = function(){
if(timer){
clearInterval(timer)
}
}
// Handler for .ready() called.
});