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 @@
{{ question.data.question }}
Frage {{ question_index|add:1 }} von {{ total_questions }}
-0/0
Verbleibende Zeit
30