blob: 37445d220b84d12294d3a72e588492a91cce35bf [file] [log] [blame]
avm999634f7c03c2020-09-29 00:08:12 +02001<!DOCTYPE html>
2
3<html lang="ca">
4 <head>
5 <meta charset="utf-8">
6 <meta name="viewport" content="width=device-width, initial-scale=1">
7 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
8 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.0/css/bulma.min.css">
Andreu Huguet898768a2020-09-29 00:30:23 +02009 <link rel="stylesheet" href="./css/bulma-adjustments.css">
avm999634f7c03c2020-09-29 00:08:12 +020010 </head>
11 <body>
avm999634f7c03c2020-09-29 00:08:12 +020012 <script>
13 var elements = document.getElementsByClassName("button");
14 Array.from(elements).forEach(function(element) {
15 element.addEventListener('click', clickButton);
16 element.parent = element.parentNode.id;
17 });
18 var elements = document.getElementsByClassName("complex-button");
19 Array.from(elements).forEach(function(element) {
20 element.addEventListener('click', clickButton);
21 element.parent = element.parentNode.id;
22 });
23
24 var final_JSON = {
25 "class": null,
26 "number": "",
27 "letter": ""
28 };
29
30 function fillInSummary() {
31 var begins = new Date(parseInt(final_JSON.class.begins)*1000);
32 var ends = new Date(parseInt(final_JSON.class.ends)*1000);
33
34 document.getElementById('subject-final').textContent = final_JSON.class.friendly_name;
35 document.getElementById('classroom-final').textContent = final_JSON.class.room;
36 document.getElementById('date-final').textContent = begins.toLocaleDateString();
37 document.getElementById('time-final').textContent = formatDate(begins) + ' - ' + formatDate(ends);
38 document.getElementById('letter-final').textContent = final_JSON.letter;
39 document.getElementById('number-final').textContent = final_JSON.number;
40 }
41
42 function clickButton(element) {
43 var btn = element.currentTarget;
44 var parent = btn.parent;
45
46 if (parent == "subject-container") {
47 // Canvi de background del button
48 var selectedClass = JSON.parse(btn.getAttribute('data-class'));
49 $("#subject-container .complex-button").removeClass("is-link")
50 btn.classList.add("is-link");
51 // Canvi JSON
52 final_JSON["class"] = selectedClass;
53 // Anchor següent pregunta
54 switchSection("section-2");
55 } else if (parent == "number-container") {
56 // Canvi de background del button
57 $("#number-container .button").removeClass("is-link is-light is-active")
58 btn.classList.add("is-link", "is-light", "is-active");
59 // Canvi JSON
60 final_JSON["number"] = btn.getAttribute('data-number');
61 // Introducció de totes les dades al resum final
62 fillInSummary();
63 // Anchor següent pregunta
64 switchSection("section-send");
65 } else if (parent == "letter-container") {
66 // Canvi de background del button
67 $("#letter-container .button").removeClass("is-link is-light is-active")
68 btn.classList.add("is-link", "is-light", "is-active");
69 // Canvi JSON
70 final_JSON["letter"] = btn.getAttribute('data-letter');
71 // Anchor següent pregunta
72 switchSection("section-3");
73 }
74 }
75
76 function formatDate(d) {
77 var str = "";
78 if (d.getHours() < 10) str += "0";
79 str += d.getHours();
80 str += ":";
81 if (d.getMinutes() < 10) str += "0";
82 str += d.getMinutes();
83 return str;
84 }
85
86 var api_url;
87
88 window.addEventListener('load', _ => {
89 // Check if user is signed in
90 if (localStorage.getItem('devMode') == 'true') {
91 var banner = document.getElementById('dev-mode');
92 banner.addEventListener('click', _ => {
93 localStorage.devMode = 'false';
94 location.reload();
95 });
96 banner.classList.remove('hidden');
97 api_url = localStorage.getItem('apiUrl') || 'https://covid-tracability-backend-dev.sandbox.avm99963.com/api/v1/'
98 } else {
99 api_url = "https://covid-tracability-backend-prod.sandbox.avm99963.com/api/v1/";
100 }
101 fetch(api_url + "isSignedIn", {
102 "mode": "cors",
103 "credentials": "include"
104 })
105 .then(response => response.json())
106 .then(data => {
107 if (!data.payload.signedIn) {
108 console.log("Not signed in!");
109 fetch(api_url + "getAuthUrl", {
110 "mode": "cors",
111 "credentials": "include"
112 })
113 .then(response => response.json())
114 .then(data => {
115 // TODO: redirect here
116 // location.href = data.payload.url;
117 console.warn('Log in here: ', data.payload.url);
118 });
119 }
120 });
121
122 fetch(api_url + "getCurrentClasses", {
123 "mode": "cors",
124 "credentials": "include"
125 })
126 .then(response => response.json())
127 .then(data => {
128 for (var classe of data.payload.classes) {
129 console.log(classe);
130
131 var hora_inici = formatDate(new Date(parseInt(classe.begins)*1000));
132 var hora_final = formatDate(new Date(parseInt(classe.ends)*1000));
133
134 var classeDiv = document.createElement('div');
135 classeDiv.classList.add('message', 'complex-button');
136 classeDiv.id = 'subject-' + classe.subject_id + '-' + classe.room;
137 classeDiv.setAttribute('data-class', JSON.stringify(classe));
138
139 var header = document.createElement('div');
140 header.classList.add('message-header');
141 header.textContent = classe.friendly_name;
142
143 var body = document.createElement('div');
144 body.classList.add('message-body');
145
146 var div1 = document.createElement('div');
147 var span = document.createElement('span');
148 span.textContent = classe.room;
149
150 div1.textContent = 'Aula ';
151 div1.appendChild(span);
152
153 var div2 = document.createElement('div');
154 div2.textContent = hora_inici + ' - ' + hora_final;
155
156 body.appendChild(div1);
157 body.appendChild(div2);
158
159 classeDiv.appendChild(header);
160 classeDiv.appendChild(body);
161
162 document.getElementById("subject-container").appendChild(classeDiv);
163 }
164
165 var elements = document.getElementsByClassName("button");
166 Array.from(elements).forEach(function(element) {
167 element.addEventListener('click', clickButton);
168 element.parent = element.parentNode.id;
169 });
170 var elements = document.getElementsByClassName("complex-button");
171 Array.from(elements).forEach(function(element) {
172 element.addEventListener('click', clickButton);
173 element.parent = element.parentNode.id;
174 });
175 });
176 });
177 </script>
178
179 <span id="dev-mode" class="hidden">Dev mode</span>
180
181 <nav class="navbar is-link">
182 <div class="navbar-brand">
183 <div class="navbar-item has-text-weight-bold">Traçabilitat DAFME</div>
184 </div>
185 </nav>
186
187 <section id="section-1" class="section">
188 <!--
189 <div class="field has-addons">
190 <p class="control">
191 <button class="button">&lt;</button>
192 </p>
193 <p class="control">
194 <input type="date" class="button">
195 </p>
196 <p class="control">
197 <button class="button">&gt;</button>
198 </p>
199 </div>
200 <div class="field has-addons">
201 <p class="control">
202 <button class="button">&lt;</button>
203 </p>
204 <p class="control">
205 <input type="button" class="button" value="08:00 - 08:30">
206 </p>
207 <p class="control">
208 <button class="button">&gt;</button>
209 </p>
210 </div><br>
211 -->
212 <div id="subject-container" class="grid buttons"></div><br>
213
214 </section>
215
216 <section id="section-2" class="section" style="display: none;">
217 <button class="button is-link is-light" onclick="switchSection('section-1')">Torna enrere (Classe)</button>
218
219 <div id="letter-container" class="buttons grid">
220 <button class="button" id="letter-A" data-letter="A">A</button>
221 <button class="button" id="letter-B" data-letter="B">B</button>
222 <button class="button" id="letter-C" data-letter="C">C</button>
223 <button class="button" id="letter-D" data-letter="D">D</button>
224 <button class="button" id="letter-E" data-letter="E">E</button>
225 <button class="button" id="letter-F" data-letter="F">F</button>
226 <button class="button" id="letter-G" data-letter="G">G</button>
227 <button class="button" id="letter-H" data-letter="H">H</button>
228 <button class="button" id="letter-I" data-letter="I">I</button>
229 <button class="button" id="letter-J" data-letter="J">J</button>
230 <button class="button" id="letter-K" data-letter="K">K</button>
231 <button class="button" id="letter-L" data-letter="L">L</button>
232 </div>
233 </section>
234
235 <section id="section-3" class="section" style="display: none;">
236 <button class="button is-link is-light" onclick="switchSection('section-2')">Torna enrere (Lletra)</button>
237
238 <div id="number-container" class="buttons grid">
239 <button class="button" id="number-1" data-number="1">1</button>
240 <button class="button" id="number-2" data-number="2">2</button>
241 <button class="button" id="number-3" data-number="3">3</button>
242 <button class="button" id="number-4" data-number="4">4</button>
243 <button class="button" id="number-5" data-number="5">5</button>
244 <button class="button" id="number-6" data-number="6">6</button>
245 <button class="button" id="number-7" data-number="7">7</button>
246 <button class="button" id="number-8" data-number="8">8</button>
247 <button class="button" id="number-9" data-number="9">9</button>
248 <button class="button" id="number-10" data-number="10">10</button>
249 </div>
250 </section>
251 <section id="section-send" class="section" style="display: none;">
252 <button class="button is-link is-light" onclick="switchSection('section-3')">Torna enrere (Número)</button>
253 <br/><br/>
254 <span class="has-text-weight-bold">Assignatura:</span>
Andreu Huguet898768a2020-09-29 00:30:23 +0200255 <span id="subject-final">ASSIGNATURA ESCOLLIDA</span><br>
avm999634f7c03c2020-09-29 00:08:12 +0200256 <span class="has-text-weight-bold">Aula:</span>
Andreu Huguet898768a2020-09-29 00:30:23 +0200257 <span id="classroom-final">AULA ESCOLLIDA</span><br>
avm999634f7c03c2020-09-29 00:08:12 +0200258 <span class="has-text-weight-bold">Data:</span>
Andreu Huguet898768a2020-09-29 00:30:23 +0200259 <span id="date-final">DD/MM/YYYY</span><br>
avm999634f7c03c2020-09-29 00:08:12 +0200260 <span class="has-text-weight-bold">Hora:</span>
Andreu Huguet898768a2020-09-29 00:30:23 +0200261 <span id="time-final">HH:MM - HH:MM</span><br>
avm999634f7c03c2020-09-29 00:08:12 +0200262 <span class="has-text-weight-bold">Posició:</span>
Andreu Huguet898768a2020-09-29 00:30:23 +0200263 <span id="letter-final">A</span><span id="number-final">1</span><br><br>
avm999634f7c03c2020-09-29 00:08:12 +0200264
265 <button id="send-button" class="button is-link is-large">Omple el formulari</button><br><br>
266
267 Hi ha errors? <br /><a class="button is-link is-light" href="https://docs.google.com/forms/d/e/1FAIpQLSfT9o287VqLyhwR8LPdloAQWhuqCgA3NfdhgP5vb9_sVQHL-g/viewform">Completa el formulari manualment</a>
268 </section>
269
270 <script>
271 var current_section = "section-1";
272
273 function toggleVisibility(id) {
274 var x = document.getElementById(id);
275 if (x.style.display === "none") {
276 x.style.display = "block";
277 } else {
278 x.style.display = "none";
279 }
280 }
281
282 function switchSection(s) {
283 setTimeout(function(){
284 toggleVisibility(current_section);
285 toggleVisibility(s);
286 current_section = s;
287 }, 75);
288 }
289
290 const idsFormulari = {
291 room: "1063142948",
292 day: "2115504093",
293 begins: "1749141911",
294 ends: "1827359679",
295 rows: {
296 A: "208184485",
297 B: "1077148310",
298 C: "642851281",
299 D: "1686039024",
300 E: "697835787",
301 F: "1511799646",
302 G: "809853432",
303 H: "182597499",
304 I: "1890539481",
305 J: "529159478",
306 K: "1615241874",
307 L: "1334263875"
308 },
309 notes: "1600275159"
310 };
311 const formBaseUrl = "https://docs.google.com/forms/d/e/1FAIpQLSfT9o287VqLyhwR8LPdloAQWhuqCgA3NfdhgP5vb9_sVQHL-g/viewform";
312
313 function sendForm() {
314 // Add subject to user
315 fetch(api_url + "addUserSubject", {
316 "method": "POST",
317 "body": JSON.stringify({
318 subject: final_JSON.class.subject_id
319 }),
320 "mode": "cors",
321 "credentials": "include"
322 })
323 .then(res => res.json())
324 .then(json => {
325 console.log("Subject added to user: ", json);
326
327 var begins = new Date(parseInt(final_JSON.class.begins)*1000);
328 var ends = new Date(parseInt(final_JSON.class.ends)*1000);
329
330 var params = new URLSearchParams();
331 params.append("entry." + idsFormulari.room, final_JSON.class.room); // class, number, letter
332 params.append("entry." + idsFormulari.day, begins.getFullYear().toString() + '-' + (begins.getMonth() + 1).toString().padStart(2, "0") + '-' + begins.getDate().toString().padStart(2, "0"));
333 params.append("entry." + idsFormulari.begins, formatDate(begins));
334 params.append("entry." + idsFormulari.ends, formatDate(ends));
335 params.append("entry." + idsFormulari.rows[final_JSON.letter], 'Columna ' + final_JSON.number);
336 params.append("entry." + idsFormulari.notes, '[Autogenerat per delefme/covid-tracability -- Assignatura seleccionada: ' + final_JSON.class.friendly_name + ']');
337
338 var formulari_link = formBaseUrl + '?' + params.toString();
339 window.location.href = formulari_link;
340 });
341 }
342
343 document.getElementById("send-button").addEventListener('click', function (el) {
344 document.getElementById('send-button').classList.add('is-loading');
345
346 sendForm();
347 });
348 </script>
349 </body>
350</html>