pdf-quiz-generator/backend/app/models/attempt.py
Daniel 5398342e3d Suspend pauses timer; hide timer-expired attempts from history
Suspend now pauses the timer instead of letting it run out:
- 'Suspend & Leave' sends suspended=true with time_left to backend
- On resume, backend re-anchors started_at to now with held time_left
- Closing tab without suspending continues to run the timer (unchanged)

Timer-expired auto-submits are marked with expired=1 and excluded from:
- Attempt history (GET /attempts/history)
- Dashboard stats (quiz count, total attempts, average score)
- Attempt list (GET /attempts)
- DDL: ALTER TABLE quiz_attempts ADD COLUMN expired INTEGER DEFAULT 0

Course-quiz decoupling is preserved — these changes only touch
non-course quizzes (Quiz.course_id IS NULL).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-19 19:27:46 +02:00

37 lines
1.6 KiB
Python

from datetime import datetime
from sqlalchemy import Column, Integer, Boolean, String, DateTime, ForeignKey, JSON
from sqlalchemy.orm import relationship
from app.database import Base
class QuizAttempt(Base):
__tablename__ = "quiz_attempts"
id = Column(Integer, primary_key=True, index=True)
quiz_id = Column(Integer, ForeignKey("quizzes.id", ondelete="CASCADE"), nullable=False)
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
score = Column(Integer, default=0)
total_questions = Column(Integer, default=0)
started_at = Column(DateTime, default=datetime.utcnow)
completed_at = Column(DateTime, nullable=True)
selected_question_ids = Column(JSON, nullable=True) # for question pool: which questions this attempt uses
expired = Column(Integer, default=0) # 1 = auto-submitted due to timer expiry (abandoned), excluded from history
quiz = relationship("Quiz", back_populates="attempts")
user = relationship("User", back_populates="attempts")
answers = relationship("AttemptAnswer", back_populates="attempt", cascade="all, delete-orphan")
class AttemptAnswer(Base):
__tablename__ = "attempt_answers"
id = Column(Integer, primary_key=True, index=True)
attempt_id = Column(Integer, ForeignKey("quiz_attempts.id", ondelete="CASCADE"), nullable=False)
question_id = Column(Integer, ForeignKey("questions.id", ondelete="CASCADE"), nullable=False)
user_answer = Column(String, nullable=False)
is_correct = Column(Boolean, default=False)
attempt = relationship("QuizAttempt", back_populates="answers")
question = relationship("Question")