From d6f4d3f55e978b644daa4166f22b15c5e71d65ca Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Tue, 30 Jun 2026 13:08:05 +0330 Subject: [PATCH] quiz participant score updated --- apps/quiz/serializers/participant.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/apps/quiz/serializers/participant.py b/apps/quiz/serializers/participant.py index ba931f7..4635a12 100644 --- a/apps/quiz/serializers/participant.py +++ b/apps/quiz/serializers/participant.py @@ -35,6 +35,25 @@ class QuizParticipantSerializer(serializers.ModelSerializer): def create(self, validated_data): answers = validated_data.pop('answers', []) + + quiz = validated_data.get('quiz') + total_questions = quiz.questions.count() + correct_count = 0 + questions_map = {q.id: q.correct_answer for q in quiz.questions.all()} + + for ans in answers: + # Depending on if ans['question'] is an instance or ID + q_id = ans.get('question').id if hasattr(ans.get('question'), 'id') else ans.get('question') + option_num = ans.get('option_num') + if q_id in questions_map and questions_map[q_id] == option_num: + correct_count += 1 + + question_score = round((correct_count / total_questions) * 100) if total_questions > 0 else 0 + + validated_data['question_score'] = question_score + validated_data['timing_score'] = 0 + validated_data['total_score'] = question_score + obj = super().create(validated_data) answers_objs = [] for ans in answers: