-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
341 lines (225 loc) · 9.52 KB
/
code.js
File metadata and controls
341 lines (225 loc) · 9.52 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
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
//components loading
var flag = 0; //flag for location validation
var inputDataFlag = 1; //flag for validating input data( email/name)
/* Issue with global local scope of these variables, presently they are not used in methods.
Instead we extract ele. again*/ //(NOT RESOLVED)
function main() {
// variables from HTML elements
spinner_overlay = document.getElementById('spinner_overlay');
spinner_element = document.getElementById('spinner_element');
button = document.getElementById('button_id');
bypass_button = document.getElementById('report_button_id');
message = document.getElementById('message_label_id');
email_label = document.getElementById('email_label_id');
email_input = document.getElementById('email_input_id');
name_input = document.getElementById('name_input_id');
name_label = document.getElementById('name_label_id');
combobox_input = document.getElementById('combobox_id');
// *** ``` backsticks must be used for ${} notation
console.log(`Variables loaded with flag = ${flag}, inputDataFlag = 0`);
//All listeners after DOM
button.addEventListener('click', submit);
bypass_button.addEventListener('click', bypassSubmit);
email_input.addEventListener('change', emailValidation);
}
//global variables
const timeStamper = new Date();
var date = `${timeStamper.getDate()}/${timeStamper.getMonth()}/${timeStamper.getFullYear()}`;
var time = `${timeStamper.getTime()}`;
var latitude = 0.0;
var longitude = 0.0;
var message, name_input, bypass_button ,email_input, spinner_element, spinner_overlay, button;
// Functions......................................................................................................
function emailValidation(event){
console.log(`Validating email string in emailValidation() >>`);
console.log(`String: ${email_input.value}`);
var testText = email_input.value;
if ((testText.indexOf('@')=== -1) || (testText.indexOf('.')=== -1)){
email_label.innerHTML = `ENTER EMAIL<br><p style='color : 'red'; fontsize : 2px;'>-email invalid</p>`;
//inputDataFlag = 0
inputDataFlag = 0;
console.log('inputDataFlag 0');
} else {
email_label.innerHTML = "ENTER EMAIL";
inputDataFlag = 1;
console.log('inputDataFlag 1');
}
}
function submit(event) {
//prevent reload as Geolocation API takes time
event.preventDefault();
console.log("working on submit function >>");
// NOTE: This API needs HTTPS outside of development env.
// Geolocation API START.........................................................................................................
if (navigator.geolocation){
console.log("framework supportable >> ");
if (inputDataFlag == 1) {
console.log('inputDataFlag 1');
// navigator.geolocation.getCurrentPosition launches showPosition the moment it gets any real value API
navigator.geolocation.getCurrentPosition(showPosition, processIT);
} else {
alert("Invalid input data!");
console.log('inputDataFlag 0');
}
function processIT(something){
alert(`Location validation failed ${JSON.stringify(something)}`);
message.innerHTML = "Location validation failed! (Error code 2)" ;
console.log(`Location validation failed, something = ${something}>>`);
showPosition(something);
}
function showPosition(position){
console.log("Collecting position >>");
latitude = position.coords.latitude;
longitude = position.coords.longitude;
//console.log('Got location >> \nLatitude :'+ latitude.toString() +'\n Longitude :'+ longitude.toString());
console.log(`Got location >> \nLatitude :${latitude}\nLongitude :${longitude}`);
validatePosition();
}
} else {
alert("Your browser does not support the framework(Error code 1)");
}
}
//Geolocation API END...........................................................................................................
/*
LOCATION TEST [CHANGE LOCATION HERE]
FORMAT polygon = [[x1,y1], [x2,y2], [x3,y3], ...] -coordinates of edges of the definite room
*/
function validatePosition(){
console.log('Working in validatePosition() >>');
//Coordinates of the room(SET THIS)
var x = latitude, y = longitude;
var polygon = [[23.2027695, 72.6439792], [23.2027661, 72.6439791], [23.2095744, 72.630272], [23.2027559, 72.6439962]];
var n = polygon.length;
var i = 0, count = 0 ; //counter
for (i = 0; i < n; i++) {
var x1 = polygon[i] [0], y1 = polygon[i] [1];
var x2 = polygon[(i + 1) % n][0], y2 = polygon[(i + 1) % n][1]; //modulo ensures that the points are taken in a circle
if ((((y1 <= y) && (y <= y2)) || ((y2 <= y) && (y <= y1))) &&
(x <= (x2 -x1) * (y - y1)/(y2 - y1) + x2)) {
count+= 1;
}
}
if ((count % 2) == 0) {
flag = 0;
} else {
// Set flag to 1(TRUE)
flag = 1
console.log(`Location validation FLAG 1, count value : ${count}>>`);
writeSheetInJS(flag);
}
console.log(``);
if (flag == 0){
console.log(`Location validation FLAG 0, count value : ${count}>>`);
alert("Location out of bound! Go to the defined classroom location, and try again(ERROR CODE 3)\n\
Contact support or the developer if you think this is a mistake");
}
// var coordinatesOfRoom = []
// var room_top_latitude = 0;
// var room_top_longitude = 0;
// var room_bottom_latitude = 1000;
// var room_bottom_longitude = 1000;
// if ((latitude >= room_top_latitude) && (latitude <= room_bottom_latitude)){
// if ((longitude >= room_top_longitude) && (longitude <= room_bottom_longitude)){
// //Set flag to 1(TRUE)
// flag = 1
// console.log("Location validation FLAG 1 >>");
// writeSheetInJS(flag);
// }
// }
// If both/one condition fail, alert location validation fail as still FLAG = 0
}
function bypassSubmit(event){
//Prevent reload
event.preventDefault()
console.log("working in bypassSubmit() >>");
var bypassWarningText = `If instructor is absent in class after 15min of class commencement then bypassing location validation is expected to be used. Bypassing enables you to record attendance without location validation— by being outside the class. Be cautious that giving false bypass request may invite
disciplinary action.
P.S: Individual responses are being recorded. For more info, check T&C.
DO YOU WANT TO REPORT NO INSTRUCTOR?`
var bypass_alert_ans = confirm(bypassWarningText);
if (bypass_alert_ans == true){
console.info("By bypass_alert_ans TRUE>>");
} else {
console.info("By bypass_alert_ans FALSE>>");
}
}
function writeSheetInJS(flag_boolean) {
console.log("Working in writeSheetInJS function >>");
//Update date & time
date = `${timeStamper.getDate()}/${timeStamper.getMonth()}/${timeStamper.getFullYear()}`;
time = `${timeStamper.getTime()}`;
console.log(`Date and Time updated: date >> ${date}, time >> ${time}`);
if (flag_boolean == 1){
console.log("Flag 1 in writeSheetInJS function >>");
//Declaring the object--ya mapping :) -- to be sent as POST request
const dataObject = { name: name_input.value,
email: email_input.value,
subject: combobox_input.value,
date : date,
time : time
};
console.log(dataObject);
//options to fetch as object --> JSON
var options = {
method : 'POST',
headers : {
'Content-Type': 'application/json'
},
body : JSON.stringify(dataObject)// <-- Obj to JSON formatted string
//P.S: something.json() is used to JSON --> Obj
};
//FETCH method
async function fetchMethod(){
console.log("Working in async fetchMethod() >>");
//add animation
document.getElementById('spinner_overlay').style.display = 'block';
document.getElementById('spinner_element').style.display = 'block';
document.getElementById('button_id').style.visibility = 'hidden';
document.getElementById('report_button_id').style.visibility = 'hidden';
var postResponse = await fetch('/api', options); // NOTE: await always returns a Promise
console.log("Sent request --> Got response!");
var data = await postResponse.json(); //Obj --> JSON(stringify) -->Server --> JSON --> Obj(.json())
//Print the received response of POST request
console.log(data);
//Change message label (HTML in innerHTML)
message.innerHTML = `${data.response}<br>Ended with CODE ${data.code}<br>Reload for doubtful attempts`;// <-- HTML code here, innerHTML
console.log(data.code);
//sleep in JS in flexing way :)
await new Promise( (resolve, reject) => {
setTimeout(resolve, 5 * 1000); // wait for the resolve() for 5 * 1000 s
});
//Remove animation
document.getElementById('spinner_overlay').style.display = 'none';
document.getElementById('spinner_element').style.display = 'none';
document.getElementById('message_label_id').style.fontSize = '20';
//NOTE: (!data.code == 200) tests NULL and (data.code !== 200) tests value
if (data.code !== 200) {
console.log('Redirecting as received STATUS CODE != 200...');
window.location.replace('violation.html');
}
}
fetchMethod();
} else {
alert("Flag not 1");
console.log("Flag not 1 in writeSheetInJS function");
}
}
// if (flag_boolean == 1){
// var fs = require("fs");
// console.log("fs created for reading writing files >>");
// data_set = [name_input, email]
// fs.writeFile("records.csv", data_set, err => {
// if (err) {
// console.log(err);
// }
// console.log("Writing done >>");
// })
// } else {
// alert("Flag not 1.(ERROR CODE 3)");
// }
// }
function getDeviceInfo(){
//later....................... get unique identifier of a device
}
//All Listeners before DOM.................................................................................................
document.addEventListener('DOMContentLoaded', main);