You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.1 KiB
100 lines
3.1 KiB
from rest_framework import serializers
|
|
|
|
from apps.quiz.models import Question, Quiz, QuizParticipant
|
|
from apps.course.models import Participant
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QuizListSerializer(serializers.ModelSerializer):
|
|
is_complated = serializers.SerializerMethodField()
|
|
permission = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Quiz
|
|
fields = ['id', 'title', 'description', 'permission', 'each_question_timing', 'is_complated']
|
|
|
|
def get_permission(self, obj):
|
|
request = self.context.get('request')
|
|
if not request or not request.user.is_authenticated:
|
|
return False
|
|
# Check if the user has participated in this quiz
|
|
user = request.user
|
|
|
|
# obj.lesson is now CourseLesson directly
|
|
course_lesson = obj.lesson
|
|
if not course_lesson:
|
|
return False
|
|
|
|
course = course_lesson.course
|
|
|
|
if not self._is_participant(user, course):
|
|
return False
|
|
|
|
participated = QuizParticipant.objects.filter(user=user, quiz=obj).exists()
|
|
return not participated
|
|
|
|
|
|
def _is_participant(self, student, course):
|
|
"""Helper method to check if a student is a participant in the given course."""
|
|
return Participant.objects.filter(student=student, course=course).exists()
|
|
|
|
def get_is_complated(self, obj):
|
|
request = self.context.get('request')
|
|
if not request or not request.user.is_authenticated:
|
|
return False
|
|
user = request.user
|
|
return QuizParticipant.objects.filter(user=user, quiz=obj).exists()
|
|
|
|
|
|
|
|
|
|
|
|
class QuestionSerializer(serializers.ModelSerializer):
|
|
options = serializers.SerializerMethodField()
|
|
|
|
def get_options(self, obj) -> list:
|
|
return [
|
|
{
|
|
'id': i,
|
|
'title': getattr(obj, f"option{i}")
|
|
} for i in range(1, 5)
|
|
]
|
|
|
|
class Meta:
|
|
model = Question
|
|
fields = ['id', 'question', 'options', 'correct_answer']
|
|
|
|
|
|
class QuizSerializer(serializers.ModelSerializer):
|
|
lesson = serializers.PrimaryKeyRelatedField(read_only=True)
|
|
questions = QuestionSerializer(many=True)
|
|
permission = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Quiz
|
|
fields = ['id', 'permission', 'lesson', 'title', 'description', 'each_question_timing', 'questions']
|
|
|
|
def get_permission(self, obj):
|
|
request = self.context.get('request')
|
|
if not request or not request.user.is_authenticated:
|
|
return False
|
|
# Check if the user has participated in this quiz
|
|
user = request.user
|
|
|
|
# obj.lesson is now CourseLesson directly
|
|
course_lesson = obj.lesson
|
|
if not course_lesson:
|
|
return False
|
|
|
|
course = course_lesson.course
|
|
|
|
# Check if user is a participant in the course
|
|
if not Participant.objects.filter(student=user, course=course).exists():
|
|
return False
|
|
|
|
participated = QuizParticipant.objects.filter(user=user, quiz=obj).exists()
|
|
return participated
|