// REPLACE localhost WITH ACTUAL HOST IP document.addEventListener("DOMContentLoaded", function () { // console.log("ok") var students_selector = document.getElementById("students-selector") var get_quiz_button = document.getElementById("get-quiz") fetch("http://localhost:8000/students") .then(r => r.json()) .then(students => { students.forEach(student => { students_selector.innerHTML += `` }) students_selector.addEventListener("change", function (e) { get_quiz_button.disabled = false }, { once: true } ) }) get_quiz_button.addEventListener("click", function () { // console.log(students_selector.value) // console.log(students_selector.options[students_selector.selectedIndex].text) fetch('http://localhost:8000/get_quiz?' + new URLSearchParams({ student_id: students_selector.value, student: students_selector.options[students_selector.selectedIndex].text })) .then(r => r.json()) .then(quiz => { // console.log(quiz) // console.log(quiz.questions) var questions = quiz.questions var questions_html = "
" document.getElementById("header").innerHTML = `${students_selector.options[students_selector.selectedIndex].text}
` document.getElementById("main").innerHTML = questions_html var button = document.createElement('button'); button.innerHTML = 'Сдать'; button.onclick = function () { const form = document.getElementById('form'); const formData = new FormData(form); for (const [key, value] of formData) { // console.log(quiz) // console.log(`${key}: ${value}\n`) // assume questions are in the same order - can it make code simplier? quiz.questions.find(q => q.id == key).student_answer = value // console.log(quiz) } fetch('http://localhost:8000/save_student_answers', { method: 'POST', // mode: 'no-cors', // headers: { // 'Accept': 'text/plain', // 'Content-Type': 'text/plain' // }, body: JSON.stringify(quiz) }) document.getElementById("main").innerHTML = "Тестирование окончено
" }; // where do we want to have the button to appear? // you can append it to another element just by doing something like // document.getElementById('foobutton').appendChild(button); document.getElementById("main").appendChild(button) }) }) document.getElementById("end-quiz").addEventListener("click", function() { let pass = window.prompt("Уважаемый преподаватель, введите пароль, чтобы завершить тестирование для всех", "Я здесь случайно") // console.log(pass) fetch('http://localhost:8000/end_quiz?' + new URLSearchParams({ password: pass })) .then(r => r.text()) .then(text => window.alert(text)) }) })