blob: 044db2dae5f8c7383b1b5cdc8b99d9469921b88c [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 = "";
avm999634f7c03c2020-09-29 00:08:12 +020078 str += d.getHours();
79 str += ":";
80 if (d.getMinutes() < 10) str += "0";
81 str += d.getMinutes();
82 return str;
83 }
84
85 var api_url;
86
87 window.addEventListener('load', _ => {
88 // Check if user is signed in
89 if (localStorage.getItem('devMode') == 'true') {
90 var banner = document.getElementById('dev-mode');
91 banner.addEventListener('click', _ => {
92 localStorage.devMode = 'false';
93 location.reload();
94 });
95 banner.classList.remove('hidden');
96 api_url = localStorage.getItem('apiUrl') || 'https://covid-tracability-backend-dev.sandbox.avm99963.com/api/v1/'
97 } else {
98 api_url = "https://covid-tracability-backend-prod.sandbox.avm99963.com/api/v1/";
99 }
100 fetch(api_url + "isSignedIn", {
101 "mode": "cors",
102 "credentials": "include"
103 })
104 .then(response => response.json())
105 .then(data => {
106 if (!data.payload.signedIn) {
107 console.log("Not signed in!");
108 fetch(api_url + "getAuthUrl", {
109 "mode": "cors",
110 "credentials": "include"
111 })
112 .then(response => response.json())
113 .then(data => {
114 // TODO: redirect here
115 // location.href = data.payload.url;
116 console.warn('Log in here: ', data.payload.url);
117 });
118 }
119 });
120
121 fetch(api_url + "getCurrentClasses", {
122 "mode": "cors",
123 "credentials": "include"
124 })
125 .then(response => response.json())
126 .then(data => {
127 for (var classe of data.payload.classes) {
128 console.log(classe);
129
130 var hora_inici = formatDate(new Date(parseInt(classe.begins)*1000));
131 var hora_final = formatDate(new Date(parseInt(classe.ends)*1000));
132
133 var classeDiv = document.createElement('div');
134 classeDiv.classList.add('message', 'complex-button');
135 classeDiv.id = 'subject-' + classe.subject_id + '-' + classe.room;
136 classeDiv.setAttribute('data-class', JSON.stringify(classe));
137
138 var header = document.createElement('div');
139 header.classList.add('message-header');
140 header.textContent = classe.friendly_name;
141
142 var body = document.createElement('div');
143 body.classList.add('message-body');
144
145 var div1 = document.createElement('div');
146 var span = document.createElement('span');
147 span.textContent = classe.room;
148
149 div1.textContent = 'Aula ';
150 div1.appendChild(span);
151
152 var div2 = document.createElement('div');
153 div2.textContent = hora_inici + ' - ' + hora_final;
154
155 body.appendChild(div1);
156 body.appendChild(div2);
157
158 classeDiv.appendChild(header);
159 classeDiv.appendChild(body);
160
161 document.getElementById("subject-container").appendChild(classeDiv);
162 }
163
164 var elements = document.getElementsByClassName("button");
165 Array.from(elements).forEach(function(element) {
166 element.addEventListener('click', clickButton);
167 element.parent = element.parentNode.id;
168 });
169 var elements = document.getElementsByClassName("complex-button");
170 Array.from(elements).forEach(function(element) {
171 element.addEventListener('click', clickButton);
172 element.parent = element.parentNode.id;
173 });
174 });
175 });
176 </script>
177
178 <span id="dev-mode" class="hidden">Dev mode</span>
179
180 <nav class="navbar is-link">
181 <div class="navbar-brand">
182 <div class="navbar-item has-text-weight-bold">Traçabilitat DAFME</div>
183 </div>
184 </nav>
185
186 <section id="section-1" class="section">
187 <!--
188 <div class="field has-addons">
189 <p class="control">
190 <button class="button">&lt;</button>
191 </p>
192 <p class="control">
193 <input type="date" class="button">
194 </p>
195 <p class="control">
196 <button class="button">&gt;</button>
197 </p>
198 </div>
199 <div class="field has-addons">
200 <p class="control">
201 <button class="button">&lt;</button>
202 </p>
203 <p class="control">
204 <input type="button" class="button" value="08:00 - 08:30">
205 </p>
206 <p class="control">
207 <button class="button">&gt;</button>
208 </p>
209 </div><br>
210 -->
211 <div id="subject-container" class="grid buttons"></div><br>
212
213 </section>
214
215 <section id="section-2" class="section" style="display: none;">
216 <button class="button is-link is-light" onclick="switchSection('section-1')">Torna enrere (Classe)</button>
217
218 <div id="letter-container" class="buttons grid">
219 <button class="button" id="letter-A" data-letter="A">A</button>
220 <button class="button" id="letter-B" data-letter="B">B</button>
221 <button class="button" id="letter-C" data-letter="C">C</button>
222 <button class="button" id="letter-D" data-letter="D">D</button>
223 <button class="button" id="letter-E" data-letter="E">E</button>
224 <button class="button" id="letter-F" data-letter="F">F</button>
225 <button class="button" id="letter-G" data-letter="G">G</button>
226 <button class="button" id="letter-H" data-letter="H">H</button>
227 <button class="button" id="letter-I" data-letter="I">I</button>
228 <button class="button" id="letter-J" data-letter="J">J</button>
229 <button class="button" id="letter-K" data-letter="K">K</button>
230 <button class="button" id="letter-L" data-letter="L">L</button>
231 </div>
232 </section>
233
234 <section id="section-3" class="section" style="display: none;">
235 <button class="button is-link is-light" onclick="switchSection('section-2')">Torna enrere (Lletra)</button>
236
237 <div id="number-container" class="buttons grid">
238 <button class="button" id="number-1" data-number="1">1</button>
239 <button class="button" id="number-2" data-number="2">2</button>
240 <button class="button" id="number-3" data-number="3">3</button>
241 <button class="button" id="number-4" data-number="4">4</button>
242 <button class="button" id="number-5" data-number="5">5</button>
243 <button class="button" id="number-6" data-number="6">6</button>
244 <button class="button" id="number-7" data-number="7">7</button>
245 <button class="button" id="number-8" data-number="8">8</button>
246 <button class="button" id="number-9" data-number="9">9</button>
247 <button class="button" id="number-10" data-number="10">10</button>
248 </div>
249 </section>
250 <section id="section-send" class="section" style="display: none;">
251 <button class="button is-link is-light" onclick="switchSection('section-3')">Torna enrere (Número)</button>
252 <br/><br/>
253 <span class="has-text-weight-bold">Assignatura:</span>
Andreu Huguet898768a2020-09-29 00:30:23 +0200254 <span id="subject-final">ASSIGNATURA ESCOLLIDA</span><br>
avm999634f7c03c2020-09-29 00:08:12 +0200255 <span class="has-text-weight-bold">Aula:</span>
Andreu Huguet898768a2020-09-29 00:30:23 +0200256 <span id="classroom-final">AULA ESCOLLIDA</span><br>
avm999634f7c03c2020-09-29 00:08:12 +0200257 <span class="has-text-weight-bold">Data:</span>
Andreu Huguet898768a2020-09-29 00:30:23 +0200258 <span id="date-final">DD/MM/YYYY</span><br>
avm999634f7c03c2020-09-29 00:08:12 +0200259 <span class="has-text-weight-bold">Hora:</span>
Andreu Huguet898768a2020-09-29 00:30:23 +0200260 <span id="time-final">HH:MM - HH:MM</span><br>
avm999634f7c03c2020-09-29 00:08:12 +0200261 <span class="has-text-weight-bold">Posició:</span>
Andreu Huguet898768a2020-09-29 00:30:23 +0200262 <span id="letter-final">A</span><span id="number-final">1</span><br><br>
avm999634f7c03c2020-09-29 00:08:12 +0200263
264 <button id="send-button" class="button is-link is-large">Omple el formulari</button><br><br>
265
266 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>
267 </section>
268
269 <script>
270 var current_section = "section-1";
271
272 function toggleVisibility(id) {
273 var x = document.getElementById(id);
274 if (x.style.display === "none") {
275 x.style.display = "block";
276 } else {
277 x.style.display = "none";
278 }
279 }
280
281 function switchSection(s) {
282 setTimeout(function(){
283 toggleVisibility(current_section);
284 toggleVisibility(s);
285 current_section = s;
286 }, 75);
287 }
288
289 const idsFormulari = {
290 room: "1063142948",
291 day: "2115504093",
292 begins: "1749141911",
293 ends: "1827359679",
294 rows: {
295 A: "208184485",
296 B: "1077148310",
297 C: "642851281",
298 D: "1686039024",
299 E: "697835787",
300 F: "1511799646",
301 G: "809853432",
302 H: "182597499",
303 I: "1890539481",
304 J: "529159478",
305 K: "1615241874",
306 L: "1334263875"
307 },
308 notes: "1600275159"
309 };
310 const formBaseUrl = "https://docs.google.com/forms/d/e/1FAIpQLSfT9o287VqLyhwR8LPdloAQWhuqCgA3NfdhgP5vb9_sVQHL-g/viewform";
311
312 function sendForm() {
313 // Add subject to user
314 fetch(api_url + "addUserSubject", {
315 "method": "POST",
316 "body": JSON.stringify({
317 subject: final_JSON.class.subject_id
318 }),
319 "mode": "cors",
320 "credentials": "include"
321 })
322 .then(res => res.json())
323 .then(json => {
324 console.log("Subject added to user: ", json);
325
326 var begins = new Date(parseInt(final_JSON.class.begins)*1000);
327 var ends = new Date(parseInt(final_JSON.class.ends)*1000);
328
329 var params = new URLSearchParams();
330 params.append("entry." + idsFormulari.room, final_JSON.class.room); // class, number, letter
331 params.append("entry." + idsFormulari.day, begins.getFullYear().toString() + '-' + (begins.getMonth() + 1).toString().padStart(2, "0") + '-' + begins.getDate().toString().padStart(2, "0"));
332 params.append("entry." + idsFormulari.begins, formatDate(begins));
333 params.append("entry." + idsFormulari.ends, formatDate(ends));
334 params.append("entry." + idsFormulari.rows[final_JSON.letter], 'Columna ' + final_JSON.number);
avm9996356f28532020-09-29 01:06:17 +0200335 // params.append("entry." + idsFormulari.notes, '[Autogenerat per delefme/covid-tracability -- Assignatura seleccionada: ' + final_JSON.class.friendly_name + ']');
avm999634f7c03c2020-09-29 00:08:12 +0200336
avm99963dbdc9652020-09-29 01:09:25 +0200337 var formulari_link = formBaseUrl + '?' + params.toString() + '#i1';
avm999634f7c03c2020-09-29 00:08:12 +0200338 window.location.href = formulari_link;
339 });
340 }
341
342 document.getElementById("send-button").addEventListener('click', function (el) {
343 document.getElementById('send-button').classList.add('is-loading');
344
345 sendForm();
346 });
347 </script>
348 </body>
349</html>