Browse Source

quiz participant score updated

master
Mohsen Taba 3 weeks ago
parent
commit
d6f4d3f55e
  1. 19
      apps/quiz/serializers/participant.py

19
apps/quiz/serializers/participant.py

@ -35,6 +35,25 @@ class QuizParticipantSerializer(serializers.ModelSerializer):
def create(self, validated_data): def create(self, validated_data):
answers = validated_data.pop('answers', []) 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) obj = super().create(validated_data)
answers_objs = [] answers_objs = []
for ans in answers: for ans in answers:

Loading…
Cancel
Save