from datetime import datetime from sqlalchemy import JSON, Column, DateTime, ForeignKey, Integer, String, Text, UniqueConstraint from sqlalchemy.orm import relationship from app.database import Base class StudyPlan(Base): """An ordered set of question blocks a learner works through.""" __tablename__ = "study_plans" id = Column(Integer, primary_key=True, index=True) slug = Column(String(120), unique=True, nullable=False, index=True) name = Column(String(200), nullable=False) description = Column(Text, nullable=True) exam_id = Column(Integer, ForeignKey("exams.id", ondelete="SET NULL"), nullable=True) kind = Column(String(20), default="set") # set | mixed sort_order = Column(Integer, default=100) is_published = Column(Integer, default=1) created_at = Column(DateTime, default=datetime.utcnow) blocks = relationship("StudyPlanBlock", back_populates="plan", cascade="all, delete-orphan", order_by="StudyPlanBlock.position") class StudyPlanBlock(Base): """One numbered block. Its question ids are fixed, not a live filter. A plan you are part-way through must not reshuffle between visits, so the membership is snapshotted when the plan is built. """ __tablename__ = "study_plan_blocks" __table_args__ = (UniqueConstraint("plan_id", "position", name="uq_plan_block"),) id = Column(Integer, primary_key=True, index=True) plan_id = Column(Integer, ForeignKey("study_plans.id", ondelete="CASCADE"), nullable=False, index=True) position = Column(Integer, nullable=False) title = Column(String(200), nullable=False) question_ids = Column(JSON, nullable=False, default=list) plan = relationship("StudyPlan", back_populates="blocks") class StudyPlanBlockArticle(Base): """Reading attached to a block, in the order it should be read. A block was questions only, which put the reading that prepares you for them somewhere else entirely. This is the "read this, then sit this" pairing. """ __tablename__ = "study_plan_block_articles" __table_args__ = (UniqueConstraint("block_id", "article_id", name="uq_block_article"),) id = Column(Integer, primary_key=True, index=True) block_id = Column(Integer, ForeignKey("study_plan_blocks.id", ondelete="CASCADE"), nullable=False, index=True) article_id = Column(Integer, ForeignKey("articles.id", ondelete="CASCADE"), nullable=False, index=True) position = Column(Integer, default=0) class StudyPlanArticleRead(Base): """One learner marking one of a block's articles as read. Deliberately separate from `article_views`: opening an article is not the same claim as having finished with it, and the plan's progress is the second. """ __tablename__ = "study_plan_article_reads" __table_args__ = (UniqueConstraint("block_article_id", "user_id", name="uq_block_article_read"),) id = Column(Integer, primary_key=True, index=True) block_article_id = Column(Integer, ForeignKey("study_plan_block_articles.id", ondelete="CASCADE"), nullable=False, index=True) user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True) read_at = Column(DateTime, default=datetime.utcnow) class StudyPlanBlockProgress(Base): """Which block a learner has started, and the quiz it produced.""" __tablename__ = "study_plan_block_progress" __table_args__ = (UniqueConstraint("block_id", "user_id", name="uq_block_progress"),) id = Column(Integer, primary_key=True, index=True) block_id = Column(Integer, ForeignKey("study_plan_blocks.id", ondelete="CASCADE"), nullable=False, index=True) user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True) quiz_id = Column(Integer, ForeignKey("quizzes.id", ondelete="SET NULL"), nullable=True) completed_at = Column(DateTime, nullable=True)