From 26eb17acd48a882f9f9d87adda7df1b77ca24eaa Mon Sep 17 00:00:00 2001 From: gtitov Date: Thu, 10 Nov 2022 22:44:19 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20close=20#1=20add=20multiselect?= =?UTF-8?q?=20questions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui/main.js | 28 +++++++++++++++++++++++----- main.py | 12 ++++++++++-- questions.json | 16 ++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/gui/main.js b/gui/main.js index 79e990e..ba3a432 100644 --- a/gui/main.js +++ b/gui/main.js @@ -33,7 +33,23 @@ document.addEventListener("DOMContentLoaded", function () { var questions_html = "
" questions.forEach(q => { // console.log(q) - if (q.options) { + if (q.is_multiple) { + let options_div = "" + q.options.forEach(o => { + options_div += `` + }) + const question_div = + `
+ Выберите ответ: + ${options_div} +
` + questions_html += + `
+

${q.question}

+ ${q.picture ? `` : ""} + ${question_div} +
` + } else if (q.options) { let options_div = "" q.options.forEach(o => { options_div += `` @@ -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', diff --git a/main.py b/main.py index 7f021c4..5c725a7 100644 --- a/main.py +++ b/main.py @@ -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"])) diff --git a/questions.json b/questions.json index c3a1f4c..61d3acd 100644 --- a/questions.json +++ b/questions.json @@ -48,6 +48,22 @@ "Прибор для измерения высот" ], "answer": "Прибор для измерения углов" + }, + { + "id": 6, + "topic": "нивелир", + "author": "GT", + "question": "Что такое нивелир 2.0?", + "options": [ + "Прибор для измерения земли", + "Прибор для измерения углов", + "Прибор для измерения расстояний", + "Прибор для измерения высот" + ], + "answer": [ + "Прибор для измерения расстояний", + "Прибор для измерения углов" + ] } ] } \ No newline at end of file