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/accounts/home.html b/core/templates/accounts/home.html index c73c060..f1a5421 100644 --- a/core/templates/accounts/home.html +++ b/core/templates/accounts/home.html @@ -3,7 +3,7 @@ {% block content %} -
+
diff --git a/core/templates/base.html b/core/templates/base.html index 9552c8d..9340c71 100644 --- a/core/templates/base.html +++ b/core/templates/base.html @@ -5,7 +5,7 @@ - + qivip diff --git a/core/templates/library/delete_confirmation.html b/core/templates/library/delete_confirmation.html index 3548e3d..ecb3254 100644 --- a/core/templates/library/delete_confirmation.html +++ b/core/templates/library/delete_confirmation.html @@ -3,7 +3,7 @@ {% block content %}
-
+
diff --git a/core/templates/library/detail_quiz.html b/core/templates/library/detail_quiz.html index d0f6a41..ca61e96 100644 --- a/core/templates/library/detail_quiz.html +++ b/core/templates/library/detail_quiz.html @@ -5,8 +5,8 @@ @@ -14,17 +14,17 @@

{{ quiz.description }}

{% endif %} -
+
{% if questions %} +
{% for question in questions %} -
+ + + {% else %} +
diff --git a/core/templates/library/form.html b/core/templates/library/form.html index fd85510..c5e32ae 100644 --- a/core/templates/library/form.html +++ b/core/templates/library/form.html @@ -3,7 +3,7 @@ {% block content %}

Formular

{{ form.name.value }}

-
+
{% csrf_token %} {{ form.as_p }} diff --git a/core/templates/library/overview_quiz.html b/core/templates/library/overview_quiz.html index 3fdfd00..8fabf6f 100644 --- a/core/templates/library/overview_quiz.html +++ b/core/templates/library/overview_quiz.html @@ -3,16 +3,25 @@ {% block content %}

Übersicht

-
+ +
{% for quiz in quizzes %} -
-

{{ quiz.name }}

-

{{ quiz.description }}

- Spiel starten - - Löschen +
+

{{ quiz.name }}

+

{{ quiz.description }}

+ +
+
+ Spiel starten + +
+ + 🗑 +
+
+
{% endfor %}
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>