-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretest.html
205 lines (173 loc) · 7.65 KB
/
pretest.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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title> Swell Pre-test </title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link href='https://fonts.googleapis.com/css?family=Roboto+Mono' rel='stylesheet'>
<link href='style.css' rel='stylesheet' type='text/css'>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</head>
<body>
<div class="jumbotron">
<h5> Swell Pre-test Survey </h5>
<p> Before the fun begins, please answer the following 2 questions! </p>
<div id="question">
1. How excited are you about programming?
</div>
<form id="answer"> </form>
<button id="back" class="btn btn-background" > Back </button>
<button id="nextBtn" class="btn btn-background" > Next </button>
<p id="uid"> UID: </p>
</div>
<script>
let next = document.getElementById("nextBtn");
let back = document.getElementById("back");
let question = document.getElementById("question");
let answer_radio = document.getElementById("answer");
back.style.background = "#D8D8D8";
back.disabled = true;
next.style.background = "#D8D8D8";
next.disabled = true;
//display uid for debugging
let uid = localStorage.getItem("swell_uid") || "someUID";
let uid_tag = document.getElementById("uid");
uid_tag.innerHTML += uid;
let doNotLog = localStorage.getItem("data_collection") != "1";
let questions = [
["How excited are you about programming?", "pre-excitement"],
["Have you programmed before?", "experience"]
]
let answers = [
[["Very excited.", 2], ["Excited.", 1], ["Not excited.", 0]],
[["Yes.", 1], ["No.", 0]]
]
let q_order = shuffle([0, 1]);
let i = 0;
let q_tag = 0;
updateQuestion();
//set up the next button
next.onclick = function() {
storeAnswer();
if (i < q_order.length - 1) {
i++;
updateQuestion();
if (i > 0) {
back.style.background = "#673AB7";
back.disabled = false;
}
} else if (i == q_order.length - 1) {
//remove radio inputs saved to localStorage, if any
for (var q of questions) {
localStorage.removeItem(q[1]);
}
location.href = "index.html";
}
}
//set up the back button
back.onclick = function() {
storeAnswer();
if (i > 0) {
i--;
updateQuestion();
if (i == 0) {
back.style.background = "#D8D8D8";
back.disabled = true;
}
}
}
//enable next button if 1 radio button is checked
function checkRadio() {
var radios = document.getElementsByName(q_tag);
for (let radio of radios) {
if (radio.checked) {
next.style.background = "#673AB7";
next.disabled = false;
return;
}
}
next.style.background = "#D8D8D8";
next.disabled = true;
}
//update the current question
function updateQuestion() {
question.innerHTML = (i + 1) + ". " + questions[q_order[i]][0];
q_tag = questions[q_order[i]][1];
let a_order = shuffle([0, 1, 2]);
if (q_order[i] == 1) {
a_order = shuffle([0, 1]);
}
let checked = localStorage.getItem(q_tag);
if (checked != null) {
checked = parseInt(checked);
}
answer_radio.innerHTML = '';
for (let j of a_order) {
var div = document.createElement('div');
div.className = 'radio';
if (checked != null && checked == answers[q_order[i]][j][1]) {
div.innerHTML = `<label><input type="radio" name=${q_tag} value=${answers[q_order[i]][j][1]} onclick="checkRadio()" checked> ${answers[q_order[i]][j][0]} </label>`;
} else {
div.innerHTML = `<label><input type="radio" name=${q_tag} value=${answers[q_order[i]][j][1]} onclick="checkRadio()"> ${answers[q_order[i]][j][0]} </label>`;
}
answer_radio.appendChild(div);
}
next.style.background = "#D8D8D8";
next.disabled = true;
}
function storeAnswer() {
var radios = document.getElementsByName(q_tag);
for (let radio of radios) {
if (radio.checked) {
logSurvey(q_tag, radio.value, doNotLog);
return;
}
}
}
//function to shuffle questions and answers
function shuffle(arr) {
for (var i = arr.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}
async function logSurvey(eventtype, data, doNotLog) {
let promise = new Promise((resolve, reject) => {
if (doNotLog) {
return;
}
function pad(n, width) {
let padWith = '0';
let nstr = n.toString();
return nstr.length >= width ? nstr : new Array(width - nstr.length + 1).join(padWith) + nstr;
}
let date = new Date();
let year = date.getFullYear().toString();
let month = pad(date.getMonth() + 1, 2);
let day = pad(date.getDate(), 2);
let hour = pad(date.getHours(), 2);
let minutes = pad(date.getMinutes(), 2);
let seconds = pad(date.getSeconds(), 2);
let payload = new FormData();
payload.append('uid', uid);
payload.append('data', data);
payload.append('eventtype', eventtype);
payload.append('time', year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds);
payload.append('checkpoint_id', 'l0c0');
payload.append('parses', 'false');
let xhr = new XMLHttpRequest();
xhr.open("POST", "http://camembert.cs.williams.edu:8080/events", true);
xhr.send(payload);
});
let result = await promise;
}
</script>
</body>
</html>