Spaces:
Sleeping
Sleeping
| """Quiz and assessment models.""" | |
| from datetime import datetime | |
| from enum import Enum as PyEnum | |
| from sqlalchemy import Column, String, Boolean, DateTime, Text, Integer, ForeignKey, Float, JSON | |
| from sqlalchemy.orm import relationship | |
| from uuid import uuid4 | |
| from app.database import Base | |
| class QuestionType(str, PyEnum): | |
| MULTIPLE_CHOICE = "multiple_choice" | |
| TRUE_FALSE = "true_false" | |
| SHORT_ANSWER = "short_answer" | |
| class Quiz(Base): | |
| __tablename__ = "quizzes" | |
| id = Column(String(36), primary_key=True, default=lambda: str(uuid4())) | |
| lesson_id = Column(String(36), ForeignKey("lessons.id"), nullable=False) | |
| title = Column(String(255), nullable=False) | |
| description = Column(Text, nullable=True) | |
| passing_score = Column(Float, default=70.0) | |
| time_limit_minutes = Column(Integer, nullable=True) | |
| max_attempts = Column(Integer, default=3) | |
| is_ai_generated = Column(Boolean, default=False) | |
| is_published = Column(Boolean, default=False) | |
| # Timestamps | |
| created_at = Column(DateTime, default=datetime.utcnow) | |
| updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) | |
| # Relationships | |
| lesson = relationship("Lesson", back_populates="quizzes") | |
| questions = relationship("QuizQuestion", back_populates="quiz", cascade="all, delete-orphan") | |
| attempts = relationship("QuizAttempt", back_populates="quiz", cascade="all, delete-orphan") | |
| def __repr__(self): | |
| return f"<Quiz {self.title}>" | |
| class QuizQuestion(Base): | |
| __tablename__ = "quiz_questions" | |
| id = Column(String(36), primary_key=True, default=lambda: str(uuid4())) | |
| quiz_id = Column(String(36), ForeignKey("quizzes.id"), nullable=False) | |
| question_type = Column(String(50), default=QuestionType.MULTIPLE_CHOICE) | |
| question_text = Column(Text, nullable=False) | |
| options = Column(JSON, nullable=True) # List of options for MCQ | |
| correct_answer = Column(Text, nullable=False) | |
| explanation = Column(Text, nullable=True) | |
| points = Column(Integer, default=1) | |
| order_index = Column(Integer, default=0) | |
| # Relationships | |
| quiz = relationship("Quiz", back_populates="questions") | |
| def __repr__(self): | |
| return f"<QuizQuestion {self.id}>" | |
| class QuizAttempt(Base): | |
| __tablename__ = "quiz_attempts" | |
| id = Column(String(36), primary_key=True, default=lambda: str(uuid4())) | |
| quiz_id = Column(String(36), ForeignKey("quizzes.id"), nullable=False) | |
| student_id = Column(String(36), ForeignKey("users.id"), nullable=False) | |
| score = Column(Float, default=0.0) | |
| passed = Column(Boolean, default=False) | |
| answers = Column(JSON, nullable=True) # Student's answers | |
| time_taken_seconds = Column(Integer, nullable=True) | |
| # Timestamps | |
| started_at = Column(DateTime, default=datetime.utcnow) | |
| completed_at = Column(DateTime, nullable=True) | |
| # Relationships | |
| quiz = relationship("Quiz", back_populates="attempts") | |
| student = relationship("User", back_populates="quiz_attempts") | |
| def __repr__(self): | |
| return f"<QuizAttempt {self.student_id} -> {self.quiz_id}>" | |