From 07633dcaf9bb598f5af992bf1d80782236840ee6 Mon Sep 17 00:00:00 2001 From: ben8 Date: Mon, 21 Jul 2025 18:56:17 +0200 Subject: [PATCH] =?UTF-8?q?Sch=C3=A4tzfunktion(=20(mit=20Schieberegler)=20?= =?UTF-8?q?#148?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- django/library/views.py | 56 ++++++- django/play/consumers/game.py | 34 +++- django/templates/library/detail_quiz.html | 25 ++- .../library/question/question_guess.html | 150 ++++++++++++++++++ django/templates/play/game/question_host.html | 49 +++++- .../play/game/question_participant.html | 42 ++++- .../templates/play/game/score_overview.html | 21 ++- 7 files changed, 357 insertions(+), 20 deletions(-) create mode 100644 django/templates/library/question/question_guess.html diff --git a/django/library/views.py b/django/library/views.py index 11fbfd0..5da7064 100644 --- a/django/library/views.py +++ b/django/library/views.py @@ -849,6 +849,7 @@ def question_list(request): def new_question(request): question_type = request.GET.get('type') quiz_id = request.GET.get('quiz_id') + question_data = None if not quiz_id: messages.error(request, 'Quiz ID muss angegeben werden.') @@ -860,7 +861,7 @@ def new_question(request): messages.error(request, 'Quiz nicht gefunden oder keine Berechtigung.') return redirect('library:overview_quiz') - if question_type not in ['multiple_choice', 'true_false','input','order']: + if question_type not in ['multiple_choice', 'true_false','input','order','guess']: base_url = reverse('library:new_question') return redirect(f'{base_url}?type=multiple_choice&quiz_id={quiz_id}') @@ -937,6 +938,24 @@ def new_question(request): 'question': question_text, 'options': options } + + + elif question_type == 'guess': + correct_answer = request.POST.get('correct_answer') + min_value = request.POST.get('min') + max_value = request.POST.get('max') + tolerance = request.POST.get('tolerance') + json_data = { + 'type': question_type, + 'question': question_text, + 'correct_answer': correct_answer, + 'min': min_value, + 'max': max_value, + 'tolerance': tolerance, + 'options': [ + {'value': correct_answer, 'is_correct': True} + ] + } @@ -1005,6 +1024,24 @@ def new_question(request): ] } standard_time_per_question = 30 + elif question_type == 'guess': + correct_answer = request.POST.get('correct_answer') + min_value = request.POST.get('min') + max_value = request.POST.get('max') + tolerance = request.POST.get('tolerance') + json_data = { + 'type': question_type, + 'question': '', + 'correct_answer': correct_answer, + 'min': min_value, + 'max': max_value, + 'tolerance': tolerance, + 'options': [ + {'value': correct_answer, 'is_correct': True} + ] + } + standard_time_per_question = 30 + template_name = f'library/question/question_{question_type}.html' context = { @@ -1157,6 +1194,23 @@ def edit_question(request, pk): 'question': question_text, 'options': options } + elif question_type == 'guess': + correct_answer = request.POST.get('correct_answer') + min_value = request.POST.get('min') + max_value = request.POST.get('max') + tolerance = request.POST.get('tolerance') + json_data = { + 'type': question_type, + 'question': question_text, + 'correct_answer': correct_answer, + 'min': min_value, + 'max': max_value, + 'tolerance': tolerance, + 'options': [ + {'value': correct_answer, 'is_correct': True} + ] + } + question.data = json.dumps(json_data) question.time_per_question = request.POST.get('time_per_question', '30') try: diff --git a/django/play/consumers/game.py b/django/play/consumers/game.py index 01b3abc..5500af7 100644 --- a/django/play/consumers/game.py +++ b/django/play/consumers/game.py @@ -210,15 +210,43 @@ class GameConsumer(AsyncWebsocketConsumer): correct_sequence = sorted(question_data["options"], key=lambda x: x["order"]) correct_values = [opt["value"] for opt in correct_sequence] is_correct = (answers_order == correct_values) - + + elif question_type == "guess": + try: + user_val = float(answer) + correct_val = float(question_data['options'][0].get('value', 0)) + tolerance = float(question_data.get('tolerance', 0)) + percentage = min(user_val, correct_val) / (max(user_val, correct_val) + 1e-5) + + + score_percentage= min(round(percentage, 3), 1.0) + if score_percentage>= abs(tolerance/100): + is_correct=True + else: + is_correct=False + + print(score_percentage) + + + except ValueError: + is_correct = False # falls keine Zahl eingegeben wurde + answer_index = 0 if is_correct else 1 + # Score berechnen + try: + if score_percentage==None: + score_percentage=1 + except: + score_percentage=1 + + score = 0 if is_correct: base_score = 1000 time_factor = time_remaining / int(current_question.time_per_question) / 1000 - score = int(base_score * (0.5 + 0.5 * time_factor)) - + score = int(base_score * (0.75 + 0.25 * time_factor))*score_percentage + # Antwort speichern oder aktualisieren answer_obj, created = QuizAnswer.objects.get_or_create( participant=participant, diff --git a/django/templates/library/detail_quiz.html b/django/templates/library/detail_quiz.html index 46312f7..111b946 100644 --- a/django/templates/library/detail_quiz.html +++ b/django/templates/library/detail_quiz.html @@ -115,6 +115,11 @@ class=" mt-1 bg-yellow-100 text-yellow-800 px-4 py-2 rounded-md text-xs lg:text-lg hover:border-blue-600 border-2"> Reihenfolge + + + Schätzen + {% endif %} @@ -135,8 +140,8 @@

{{ question.data.question }}

- - {% if question.data.type == 'multiple_choice' %}Multiple Choice{% elif question.data.type == 'input' %}Eingabe{% elif question.data.type == 'true_false' %}Wahr/Falsch{% else %}Reihenfolge{% endif %} + + {% if question.data.type == 'multiple_choice' %}Multiple Choice{% elif question.data.type == 'input' %}Eingabe{% elif question.data.type == 'true_false' %}Wahr/Falsch{% elif question.data.type == 'order' %}Reihenfolge{% else %}Schätzen{% endif %}
@@ -199,6 +204,22 @@ {% if question.image.image.url %} Bild der Frage {% endif %} +
+ {% elif question.data.type == 'guess' %} +
+
    + {% for option in question.data.options %} +
  • + + + + {{ option.value }} +
  • + {% endfor %} +
+ {% if question.image.image.url %} + Bild der Frage + {% endif %}
{% else %}
diff --git a/django/templates/library/question/question_guess.html b/django/templates/library/question/question_guess.html new file mode 100644 index 0000000..5f1fe2d --- /dev/null +++ b/django/templates/library/question/question_guess.html @@ -0,0 +1,150 @@ +{% extends 'base.html' %} + +{% block content %} +
+
+

{% if question %}Frage bearbeiten{% else %}Neue Schätzfrage{% endif %}

+ + Zurück zum Quiz + +
+ +
+
+ {% csrf_token %} + + +
+ +
+ +
+
+ + {% if image %} + Bild der Frage + + {% endif %} +
+ + +
+ + + {% if question and question.data.options %} + {% for option in question.data.options %} +
+ +
+ + +
+
+ +
+ + +
+ +
+ +
+ + +
+
+ +
+ + +
+
+ {% endfor %} + {% else %} +
+ +
+ + +
+
+ +
+ + +
+ +
+ +
+ + +
+
+ +
+ + +
+ + + +
+ {% endif %} + + +
+ + +
+ +
+ +
+ +
+ + Abbrechen + + +
+ +
+
+{% endblock %} diff --git a/django/templates/play/game/question_host.html b/django/templates/play/game/question_host.html index 02bd1d6..71fdf17 100644 --- a/django/templates/play/game/question_host.html +++ b/django/templates/play/game/question_host.html @@ -141,27 +141,60 @@ {% if request.session.mode == "learn" %} {% endif %} - {% else %} + {% elif question_data.type == "input" %} {% if request.session.mode == "learn" %}
-
+ {% endif %} + {% elif question_data.type == "guess" %} + {% if request.session.mode == "learn" %} +
+ +
+ +

Wert: {{ question_data.min }}

+
+ + + + + +
+{% endif %} - {% endif %} + {% endif %} diff --git a/django/templates/play/game/question_participant.html b/django/templates/play/game/question_participant.html index fdfe94b..b2d8e5d 100644 --- a/django/templates/play/game/question_participant.html +++ b/django/templates/play/game/question_participant.html @@ -115,10 +115,41 @@
+ {% elif question_data.type == "guess" %} +
+ +
+ +

Wert: {{ question_data.min }}

+
+ + + +
+ + + @@ -126,14 +157,15 @@ - {% else %} + {% elif question_data.type == "input" %}
{% endif %} diff --git a/django/templates/play/game/score_overview.html b/django/templates/play/game/score_overview.html index f93d05c..8a6fa75 100644 --- a/django/templates/play/game/score_overview.html +++ b/django/templates/play/game/score_overview.html @@ -9,7 +9,7 @@

Aktuelle Frage

{{ question_data.question }}

- {% if question_data.type != "order" %} + {% if question_data.type != "order" and question_data.type != "guess" %}
{% for option in question_data.options %} @@ -25,6 +25,25 @@
{% endfor %}
+ {% elif question_data.type == "guess" %} +
+
+
+

Richtige Anwort: {% for option in question_data.options%} {{ option.value }}{% endfor %}

+

(prozentuale Punktevergabe ab Genauigkeit von {{ question_data.tolerance }}%)

+ +

0 Antworten

+
+
+
+
+
+

Schätzung zu ungenau

+

0 Antworten

+
+
+ + {% else %}
-- 2.49.1