🚀 close #1 add multiselect questions

master
gtitov 3 years ago
parent 46f0171d36
commit 26eb17acd4

@ -33,7 +33,23 @@ document.addEventListener("DOMContentLoaded", function () {
var questions_html = "<form id='form' onkeydown='return event.keyCode != 13;'>"
questions.forEach(q => {
// console.log(q)
if (q.options) {
if (q.is_multiple) {
let options_div = ""
q.options.forEach(o => {
options_div += `<label for="${o}${q.id}"><input type="checkbox" id="${o}${q.id}" name="${q.id}" value="${o}">${o}</label>`
})
const question_div =
`<fieldset>
<legend>Выберите ответ:</legend>
${options_div}
</fieldset>`
questions_html +=
`<article>
<h3>${q.question}</h3>
${q.picture ? `<img src='${q.picture}'>` : ""}
${question_div}
</article>`
} else if (q.options) {
let options_div = ""
q.options.forEach(o => {
options_div += `<label for="${o}${q.id}"><input type="radio" id="${o}${q.id}" name="${q.id}" value="${o}">${o}</label>`
@ -71,7 +87,7 @@ document.addEventListener("DOMContentLoaded", function () {
// TODO: move this logic to the backend check_answers function
// Populate quiz with empty answers (if no answer presented in select there'll be no property "answer" what could not be resolved in API)
for (const question of quiz.questions) {
question.student_answer = ""
question.is_multiple ? question.student_answer = [] : question.student_answer = ""
}
// Replace the empty answers with real answers
@ -79,10 +95,12 @@ document.addEventListener("DOMContentLoaded", function () {
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)
console.log(`${key}: ${value}\n`) // assume questions are in the same order - can it make code simplier?
const question = quiz.questions.find(q => q.id == key)
question.is_multiple ? question.student_answer.push(value) : question.student_answer = value
// 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',

@ -30,8 +30,10 @@ with open(QUESTIONS_FILE, "r", encoding="UTF-8") as f:
# if you want to use topics 1) filter all questions with topics 2) use topic questions to construct quiz_questions (when removing service keys from all_questions dict)
# topics = ["теодолит"] # move topics to VARIABLES
# topics_questions = [q for q in all_questions if q.get("topic") in topics]
typed_questions = [dict(q, **{"is_multiple": True}) if type(q["answer"]) is list else dict(q, **{"is_multiple": False}) for q in all_questions]
print(typed_questions)
remove_keys = ["author", "answer", "topic"]
quiz_questions = [{key: value for key, value in q.items() if key not in remove_keys} for q in all_questions] # all_questions can be replaced with topic_questions
quiz_questions = [{key: value for key, value in q.items() if key not in remove_keys} for q in typed_questions] # all_questions can be replaced with topic_questions
with open(STUDENTS_FILE, "r", encoding="UTF-8") as f:
content = json.load(f)
@ -46,7 +48,13 @@ def check_answers(student_answers: dict):
for a in checked_answers["questions"]:
question_id = a["id"]
a["correct_answer"] = next(question["answer"] for question in all_questions if question["id"] == question_id) # all_questions can be replaced with topic_questions
a["is_correct"] = a["student_answer"].casefold() == a["correct_answer"].casefold() # make sure answer is str
if type(a["student_answer"]) is str and type(a["correct_answer"]) is str:
a["is_correct"] = a["student_answer"].casefold() == a["correct_answer"].casefold()
elif type(a["student_answer"]) is list and type(a["correct_answer"]) is list:
a["is_correct"] = set(a["student_answer"]) == set(a["correct_answer"])
else:
print("Unmatched types! Can't compare.")
a["is_correct"] = False
checked_answers["correct"] = sum([a["is_correct"] for a in checked_answers["questions"]])
checked_answers["correct_percent"] = round(checked_answers["correct"] * 100 / len(checked_answers["questions"]))

@ -48,6 +48,22 @@
"Прибор для измерения высот"
],
"answer": "Прибор для измерения углов"
},
{
"id": 6,
"topic": "нивелир",
"author": "GT",
"question": "Что такое нивелир 2.0?",
"options": [
"Прибор для измерения земли",
"Прибор для измерения углов",
"Прибор для измерения расстояний",
"Прибор для измерения высот"
],
"answer": [
"Прибор для измерения расстояний",
"Прибор для измерения углов"
]
}
]
}
Loading…
Cancel
Save