From a6cac52ba67dd54aaa6d4e27ab3da56d0cedb2fb Mon Sep 17 00:00:00 2001 From: ben8 Date: Fri, 2 May 2025 17:06:29 +0200 Subject: [PATCH] Eingabe_Funktion --- django/library/views.py | 36 ++++++- django/play/consumers/game.py | 26 +++++- django/templates/library/detail_quiz.html | 24 ++++- .../library/question/question_input.html | 93 +++++++++++++++++++ django/templates/play/game/question_host.html | 48 ++++++++-- .../play/game/question_participant.html | 32 ++++++- .../templates/play/game/score_overview.html | 4 +- 7 files changed, 245 insertions(+), 18 deletions(-) create mode 100644 django/templates/library/question/question_input.html diff --git a/django/library/views.py b/django/library/views.py index 867a4c2..cb44cc1 100644 --- a/django/library/views.py +++ b/django/library/views.py @@ -359,7 +359,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']: + if question_type not in ['multiple_choice', 'true_false','input']: base_url = reverse('library:new_question') return redirect(f'{base_url}?type=multiple_choice&quiz_id={quiz_id}') @@ -377,6 +377,17 @@ def new_question(request): {'value': 'Falsch', 'is_correct': not correct_answer} ] } + + elif question_type == 'input': + correct_answer = request.POST.get('correct_answer') + json_data = { + 'type': question_type, + 'question': question_text, + 'options': [ + {'value': correct_answer, 'is_correct': True} + + ] + } elif question_type == 'multiple_choice': options = [] i = 1 @@ -428,6 +439,18 @@ def new_question(request): ] } standard_time_per_question = 30 + + elif question_type == 'input': + question_data = { + 'type': question_type, + 'question': '', + 'options': [ + + + ] + } + standard_time_per_question = 30 + elif question_type == 'multiple_choice': question_data = { 'type': question_type, @@ -525,6 +548,17 @@ def edit_question(request, pk): {'value': 'Falsch', 'is_correct': not correct_answer} ] } + elif question_type == 'input': + correct_answer = request.POST.get('correct_answer') + json_data = { + 'type': question_type, + 'question': question_text, + 'options': [ + {'value': correct_answer,'is_correct':True} + + ] + } + elif question_type == 'multiple_choice': options = [] i = 1 diff --git a/django/play/consumers/game.py b/django/play/consumers/game.py index f46cad3..07292d3 100644 --- a/django/play/consumers/game.py +++ b/django/play/consumers/game.py @@ -84,7 +84,10 @@ class GameConsumer(AsyncWebsocketConsumer): elif message_type == 'submit_answer': participant_id = text_data_json['participant_id'] - answer = text_data_json['answer'] + try: + answer = text_data_json['answer'] + except: + answer=-1 time_remaining = text_data_json.get('time_remaining', 0) # Save answer and update participant score @@ -182,15 +185,30 @@ class GameConsumer(AsyncWebsocketConsumer): # Check if answer is correct is_correct = False - if answer_index >= 0: # -1 means timeout - question_data = json.loads(current_question.data) + question_data = json.loads(current_question.data) + print(answer_index) + try: + #if answer_index >= 0 : # -1 means timeout is_correct = question_data['options'][answer_index]['is_correct'] + print(is_correct) + except: + user_input = str(answer_index).strip().lower() + correct_value = str(question_data['options'][0].get('value', '')).strip().lower() + print("Richtig",correct_value) + if user_input == correct_value: + is_correct=True + answer_index=0 + print(is_correct) + + + + # Calculate score based on correctness and time score = 0 if is_correct: base_score = 1000 - time_factor = time_remaining / 30000 # 30 seconds max + time_factor = time_remaining / int(current_question.time_per_question)/int(1000) score = int(base_score * (0.5 + 0.5 * time_factor)) # Update or create answer in QuizAnswer table diff --git a/django/templates/library/detail_quiz.html b/django/templates/library/detail_quiz.html index 1d01e31..992fb7e 100644 --- a/django/templates/library/detail_quiz.html +++ b/django/templates/library/detail_quiz.html @@ -90,6 +90,14 @@

Fragen

+ {% if quiz.user_id == request.user %} + + Eingabe + + {% endif %} + + {% if quiz.user_id == request.user %} @@ -118,8 +126,8 @@

{{ question.data.question }}

- - {% if question.data.type == 'multiple_choice' %}Multiple Choice{% else %}Wahr/Falsch{% endif %} + + {% if question.data.type == 'multiple_choice' %}Multiple Choice{% elif question.data.type == 'input' %}Eingabe{% else %}Wahr/Falsch{% endif %}
@@ -143,6 +151,18 @@ {% endfor %} + {% elif question.data.type == 'input' %} +
    + {% for option in question.data.options %} +
  • + + + + {{ option.value }} +
  • + {% endfor %} +
+ {% else %} {% for option in question.data.options %} {% if option.is_correct %} diff --git a/django/templates/library/question/question_input.html b/django/templates/library/question/question_input.html new file mode 100644 index 0000000..cd29d18 --- /dev/null +++ b/django/templates/library/question/question_input.html @@ -0,0 +1,93 @@ +{% extends 'base.html' %} + +{% block content %} +
+ + +
+
+ {% 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 480e116..1ad132c 100644 --- a/django/templates/play/game/question_host.html +++ b/django/templates/play/game/question_host.html @@ -11,7 +11,7 @@
{% endif %}

Frage {{ question_index|add:1 }} von {{ total_questions }}

-

{{ question_data.question }}

+

{{ question_data.question }}

{% if question_image %}
@@ -36,7 +36,7 @@

0/0

- + {% if question_data.type != "input" %}
{% for option in question_data.options %}
+ {% else %} + {% if request.session.mode == "learn" %} + + + + + {% endif %} + {% endif %}
@@ -149,13 +162,15 @@ {% endif %} - + // Request initial participants list gameSocket.onopen = function(e) { gameSocket.send(JSON.stringify({ 'type': 'update_participants' })); + {% if request.session.mode != "learn" %} updateTimer(); + {% endif %} }; @@ -177,10 +192,20 @@ function submitAnswer(optionIndex) { if (hasAnswered) return; - + let input = ""; // Declare input here + + try { + input = document.getElementById("textanswer").value; + } catch (e) { + input = ""; + } hasAnswered = true; const now = Date.now(); - const timeRemaining = Math.max(0, questionDuration - (now - questionStartTime)); + let timeRemaining = Math.max(0, questionDuration - (now - questionStartTime)); + {% if request.session.mode == "learn" %} + timeRemaining = questionDuration; + {% endif %} + // Disable all options and highlight selected const options = document.getElementsByClassName('answer-option'); @@ -194,14 +219,23 @@ option.classList.add('opacity-50'); } } - + if(input!=""){ // Send answer to server gameSocket.send(JSON.stringify({ type: 'submit_answer', participant_id: participantId, - answer: optionIndex, + answer:input, time_remaining: timeRemaining })); } + else{ + gameSocket.send(JSON.stringify({ + type: 'submit_answer', + participant_id: participantId, + answer:optionIndex, + time_remaining: timeRemaining + })); + } + } {% endblock %} \ No newline at end of file diff --git a/django/templates/play/game/question_participant.html b/django/templates/play/game/question_participant.html index 2800f35..a99e222 100644 --- a/django/templates/play/game/question_participant.html +++ b/django/templates/play/game/question_participant.html @@ -15,7 +15,7 @@

Verbleibende Zeit

30

- + {% if question_data.type != "input" %}
{% for option in question_data.options %}
+ {% else %} + + + {% endif %}
{% endblock %} @@ -57,6 +66,13 @@ function submitAnswer(optionIndex) { if (hasAnswered) return; + let input = ""; // Declare input here + + try { + input = document.getElementById("textanswer").value; + } catch (e) { + input = ""; + } hasAnswered = true; const now = Date.now(); @@ -75,14 +91,26 @@ } } + if(input!=""){ // Send answer to server gameSocket.send(JSON.stringify({ type: 'submit_answer', participant_id: participantId, - answer: optionIndex, + answer:input, time_remaining: timeRemaining })); } + else{ + gameSocket.send(JSON.stringify({ + type: 'submit_answer', + participant_id: participantId, + answer:optionIndex, + time_remaining: timeRemaining + })); + } + } + + // Timer function updateTimer() { diff --git a/django/templates/play/game/score_overview.html b/django/templates/play/game/score_overview.html index b33a2f5..b75eed5 100644 --- a/django/templates/play/game/score_overview.html +++ b/django/templates/play/game/score_overview.html @@ -18,7 +18,7 @@ - {% if request.session.mode != "learn" %} +

Punktestand

@@ -48,7 +48,7 @@ {% endfor %}
- {% endif %} + {% if is_host %} {% if is_last_question %}