From 039e3ea0cb99028148683506f7943d2fbf939ee3 Mon Sep 17 00:00:00 2001 From: juhnsa Date: Wed, 12 Mar 2025 19:08:08 +0100 Subject: [PATCH] =?UTF-8?q?Vergr=C3=B6=C3=9Fere=20Textfelder=20in=20Quiz-F?= =?UTF-8?q?ragen-Formularen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Ändere Eingabefelder zu Textareas für bessere Benutzerfreundlichkeit - Fragen-Textfeld auf 4 Zeilen vergrößert - Antwort-Optionen auf 2 Zeilen vergrößert - Behebe Probleme mit der Anzeige bestehender Fragen --- core/library/views.py | 102 ++++++++++++------ .../question/question_multiple_choice.html | 22 ++-- .../library/question/question_true_false.html | 8 +- 3 files changed, 85 insertions(+), 47 deletions(-) diff --git a/core/library/views.py b/core/library/views.py index 4cc3344..4851d6c 100644 --- a/core/library/views.py +++ b/core/library/views.py @@ -100,7 +100,7 @@ def new_question(request): return redirect(f'{base_url}?type=multiple_choice&quiz_id={quiz_id}') if request.method == 'POST': - question_text = request.POST.get('question') + question_text = request.POST.get('question', '') # Handle different question types if question_type == 'true_false': @@ -140,27 +140,28 @@ def new_question(request): question = QivipQuestion() question.data = json.dumps(json_data) question.quiz_id = quiz - question.type = question_type - question.question = question_text question.save() return redirect('library:detail_quiz', pk=quiz.pk) else: # Initialize empty question data for new questions - question_data = { - 'type': question_type, - 'question': '', - 'options': [] - } if question_type == 'true_false': - question_data['options'] = [ - {'value': 'Wahr', 'is_correct': True}, - {'value': 'Falsch', 'is_correct': False} - ] + question_data = { + 'type': question_type, + 'question': '', + 'options': [ + {'value': 'Wahr', 'is_correct': True}, + {'value': 'Falsch', 'is_correct': False} + ] + } elif question_type == 'multiple_choice': - question_data['options'] = [ - {'value': '', 'is_correct': False}, - {'value': '', 'is_correct': False} - ] + question_data = { + 'type': question_type, + 'question': '', + 'options': [ + {'value': '', 'is_correct': False}, + {'value': '', 'is_correct': False} + ] + } template_name = f'library/question/question_{question_type}.html' context = { @@ -179,11 +180,53 @@ def edit_question(request, pk): return redirect('library:question_list') # Parse the existing JSON data - question_data = json.loads(question.data) if question.data else {} - question_type = question_data.get('type', 'multiple_choice') + try: + question_data = json.loads(question.data) if question.data else {} + question_type = question_data.get('type', 'multiple_choice') + + # For true/false questions, get the correct answer from the options + if question_type == 'true_false': + # Default to true if no options exist + correct_answer = True + if question_data.get('options'): + correct_answer = question_data['options'][0].get('is_correct', True) + question_data = { + 'type': question_type, + 'question': question_data.get('question', ''), + 'correct_answer': correct_answer, # Add this for template compatibility + 'options': [ + {'value': 'Wahr', 'is_correct': correct_answer}, + {'value': 'Falsch', 'is_correct': not correct_answer} + ] + } + else: + # For multiple choice questions + question_data.setdefault('type', question_type) + question_data.setdefault('question', '') + question_data.setdefault('options', []) + + except (json.JSONDecodeError, AttributeError): + # Handle invalid JSON or None data + question_type = question_data.get('type', 'multiple_choice') + if question_type == 'true_false': + question_data = { + 'type': question_type, + 'question': '', + 'correct_answer': True, # Default to true for new questions + 'options': [ + {'value': 'Wahr', 'is_correct': True}, + {'value': 'Falsch', 'is_correct': False} + ] + } + else: + question_data = { + 'type': question_type, + 'question': '', + 'options': [] + } if request.method == 'POST': - question_text = request.POST.get('question') + question_text = request.POST.get('question', '') # Handle different question types if question_type == 'true_false': @@ -191,7 +234,6 @@ def edit_question(request, pk): json_data = { 'type': question_type, 'question': question_text, - 'correct_answer': correct_answer, 'options': [ {'value': 'Wahr', 'is_correct': correct_answer}, {'value': 'Falsch', 'is_correct': not correct_answer} @@ -205,7 +247,7 @@ def edit_question(request, pk): options.append({ 'order': i, 'value': request.POST.get(f'option_{i}'), - 'is_correct': int(request.POST.get(f'correct_{i}', 0)) + 'is_correct': request.POST.get(f'correct_{i}') == '1' }) i += 1 @@ -225,22 +267,18 @@ def edit_question(request, pk): 'options': options } - question.question = question_text - question.type = question_type question.data = json.dumps(json_data) question.save() return redirect('library:detail_quiz', pk=question.quiz_id.pk) else: - question_data = json.loads(question.data) if question.data else {} + template_name = f'library/question/question_{question_type}.html' + context = { + 'question': {'data': question_data}, # Wrap in data structure expected by template + 'quiz_id': question.quiz_id.pk, + 'quiz': question.quiz_id + } - template_name = f'library/question/question_{question_type}.html' - context = { - 'question': question_data, - 'quiz_id': question.quiz_id.pk, - 'quiz': question.quiz_id - } - - return render(request, template_name, context) + return render(request, template_name, context) # Frage löschen @login_required diff --git a/core/templates/library/question/question_multiple_choice.html b/core/templates/library/question/question_multiple_choice.html index 3aa6e13..205f333 100644 --- a/core/templates/library/question/question_multiple_choice.html +++ b/core/templates/library/question/question_multiple_choice.html @@ -18,9 +18,9 @@
- + required>{{ question.data.question|default:'' }}
@@ -31,9 +31,9 @@ {% if question and question.data.options %} {% for option in question.data.options %}
- + required>{{ option.value }}
- + required>