-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebFunctions.js
134 lines (109 loc) · 3.81 KB
/
WebFunctions.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
var __dirname;
var process;
function OnInput() {
console.log("things input");
this.style.height = 'auto';
this.style.height = (this.scrollHeight) + 'px';
}
async function Setup(){
__dirname = document.getElementById("main-section").getAttribute("data-");
await $.get(__dirname + '/.env', function (data) {
let parts = data.split(/\r?\n|\r|\n/g)
var keys = [];
var x = 0;
parts.forEach(part =>{
keys[x] = part.split("=")[1].replace(/"/g,'');
x++;
})
process = {
"masterKey" : keys[0],
"apiAccessKey" : keys[1],
"responseBin" : keys[3]
}
})
console.log(JSON.stringify(process))
console.log("setting up")
var tx = document.getElementsByClassName('question-textBox');
for (var i = 0; i < tx.length; i++) {
tx[i].setAttribute('style', 'height:' + (tx[i].scrollHeight) + 'px;overflow-y:hidden;');
tx[i].addEventListener("input", OnInput, false);
}
document.getElementById("logo").addEventListener("click", LogoClick,false)
}
function LogoClick(){
console.log("clicked")
window.location.assign("https://www.woodcroft.sa.edu.au/")
//shell.openExternal("https://www.woodcroft.sa.edu.au/")
//require('shell').openExternal("https://www.woodcroft.sa.edu.au/");
}
async function Submit(){
let responses = []
QuestionsDiv = document.getElementById("questions");
var textBoxs = QuestionsDiv.getElementsByClassName("question-textBox");
var Questions;
await $.getJSON(__dirname + '/Questions.json', function (data) {
Questions = data.TextAnswerQuestionList
})
var num = Questions.length
var name = document.getElementById("Student-name").textContent;
if (name === ""){
OpenPopup();
return;
}
//Get responses to questions and check that manditory one are answered
for (var i = 0; i < num; i++){
//check manditory
var manditory = Questions[i].manditory
if (textBoxs[i].textContent === "" && manditory){
OpenPopup();
return;
}
//get responses
responses.push({
"Question": QuestionsDiv.getElementsByClassName("question-text")[i].textContent,
"Response": textBoxs[i].textContent
})
}
//colate into an object to send to teacher
const date = new Date();
let response = {
"StudentName" : name,
"TimeSubmited" : `${date.getDate()}:${date.getMonth() + 1}:${date.getFullYear()}`,
"QuestionsAndAnswers" : responses
}
console.log(JSON.stringify(response));
//get list of responses
let req = new XMLHttpRequest();
//collect and resend the responses
req.onreadystatechange = () => {
console.log(`state changed ${req.readyState}`);
if (req.readyState === 4) {
//add the new response to list
console.log(req.responseText)
var retrieved = JSON.parse(req.responseText);
console.log(retrieved)
console.log(response)
retrieved.record.responses.push(response)
let req2 = new XMLHttpRequest();
//send back updated list
req2.open("PUT", "https://api.jsonbin.io/v3/b/63009faba1610e638606eccf", true);
req2.setRequestHeader("Content-Type", "application/json");
req2.setRequestHeader(process.masterKey, process.apiAccessKey);
req2.send(JSON.stringify(retrieved.record));
//after submission
window.location.href= __dirname + "/submitted.html"
}
};
req.open("GET", "https://api.jsonbin.io/v3/b/63009faba1610e638606eccf/latest", true);
req.setRequestHeader(process.masterKey, process.apiAccessKey);
req.send();
}
function OpenPopup(){
window.scrollTo({ top: 0, behavior: 'smooth' });
document.getElementById("popup-wrapper").style.visibility = "visible";
document.getElementById("popup-wrapper").style.opacity = "100%";
}
function ClosePopup(){
document.getElementById("popup-wrapper").style.visibility = "hidden";
document.getElementById("popup-wrapper").style.opacity = "0%";
}