-
Notifications
You must be signed in to change notification settings - Fork 6
/
survey.js
221 lines (192 loc) · 4.67 KB
/
survey.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
// number of milliseconds used to fade things in and out
var fade_ms = 200;
var cur_question_id = -1;
function begin_questionnaire()
{
$("#header").hide();
$("#example_container").hide();
$("#show_example").hide();
$("#hide_example").hide();
$("#begin").hide();
$("#demographic_container").fadeIn(fade_ms);
$("#begin_survey").show();
}
function begin_survey()
{
if(!check_demographic_questionnaire())
{
return;
}
$("#header").hide();
hide_examples();
//generate_questions();
$("#demographic_container").hide();
$("#begin_survey").hide();
$("#next").show();
next_question();
}
function check_response()
{
return typeof $('input:radio[name=question'+cur_question_id+']:checked').val() !== 'undefined';
}
function next_question()
{
if(cur_question_id>=0 && !check_response())
{
alert("Please answer the question.");
return;
}
$("#question"+cur_question_id).hide();
cur_question_id++;
$("#question"+cur_question_id).fadeIn(fade_ms);
if($("#question"+(cur_question_id+1)).length === 0)
{
$("#next").hide();
$("#finish").show();
}
}
function finish()
{
$("#question"+cur_question_id).hide();
cur_question_id++;
// submit the survey
document.survey.submit();
$("#thanks").fadeIn(fade_ms);
}
// Hide the examples container
function hide_examples()
{
$("#example_container").hide();
$("#show_example").show();
$("#hide_example").hide();
}
// Hide the examples container
function show_examples()
{
$("#example_container").fadeIn(fade_ms);
$("#hide_example").show();
$("#show_example").hide();
}
// Checks that each question in the demographics questionnaire has been
// answered. Highlights those that are not answered.
//
// Returns true only if each question is answered.
function check_demographic_questionnaire()
{
var valid = true;
// get form
$.each($("#demographic_container :input"), function(index,select)
{
valid &= check_question(select);
}
);
return valid;
}
// Helper for check_demographic_questionnaire
//
// Inputs:
// select DOM element for select of question
//
// Returns true only if question is answered.
function check_question(select)
{
if(select.value == "")
{
select.className = "missing";
select.parentNode.parentNode.className = "missing";
return false;
}else
{
select.className = "";
select.parentNode.parentNode.className = "";
return true;
}
}
function generate_questions()
{
var drawings = Array("lion","rings","snake");
var num_drawings = drawings.length;
var lightings = Array("s","d");
var num_lightings = lightings.length;
var num_views = 2;
var num_repititions = 3;
var methods = Array("real","ink","lumo","inf");
var num_methods = methods.length;
// create all possible combos: 3*2*2*3
// Each entry looks like:
// ['drawing_name','lighting_char',view_num,['cand0',...,'cand3']]
var combos = Array();
for(var r = 0;r<num_repititions;r++)
{
for(var d = 0;d<num_drawings;d++)
{
for(var l = 0;l<num_lightings;l++)
{
for(var v = 0;v<num_views;v++)
{
combos.push([drawings[d],lightings[l],v+1,shuffle(methods.slice(0))]);
}
}
}
}
// randomly order
shuffle(combos);
var num_combos = combos.length;
for(var c = 0;c<num_combos;c++)
{
}
}
// http://stackoverflow.com/a/2450976/148668
function shuffle(array) {
var currentIndex = array.length
, temporaryValue
, randomIndex
;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// A countdown timer in jQuery:
// http://stackoverflow.com/questions/3089475/how-can-i-create-a-5-second-countdown-timer-with-jquery-that-ends-with-a-login-p
var kStudySeconds = 10;
function update_primer_button( seconds )
{
if( seconds == kStudySeconds )
{
$('#begin').html( 'Begin (study for ' + seconds + ' seconds)' );
}
else if( seconds > 1 )
{
$('#begin').html( 'Begin (study for ' + seconds + ' more seconds)' );
}
else if( seconds == 1 )
{
$('#begin').html( 'Begin (study for 1 more second)' );
}
else
{
$('#begin').html( 'Begin' );
$('#begin').removeAttr( 'disabled' );
}
}
var counter = kStudySeconds;
var countdown_timer = setInterval(
function() {
counter--;
update_primer_button( counter );
if( counter <= 0 )
{
clearInterval( countdown_timer );
}
},
1000
);
update_primer_button( counter );