<?php
namespace App\Entity;
use App\Repository\ChapterRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Traits\Timestampable;
/**
* Chapitre du programme national (créé par le superadmin).
*
* Rattaché à un SubjectReference (= matière + ClassOption).
* `position` définit l'ordre d'apparition dans le programme.
* `deadlineQuarter` indique la séquence avant la fin de laquelle le
* chapitre doit être terminé (ex : "avant la fin de la Séquence 2").
*
* La date réelle de la deadline se calcule via deadlineQuarter->getEndDate()
* si ce champ existe sur Quarter, sinon on affiche juste le nom de la séquence.
*/
#[ORM\HasLifecycleCallbacks]
#[ORM\Entity(repositoryClass: ChapterRepository::class)]
class Chapter
{
use Timestampable;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $title = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $titleEn = null;
#[ORM\Column(type: 'text', nullable: true)]
private ?string $description = null;
/**
* Ordre du chapitre dans le programme (1, 2, 3...).
*/
#[ORM\Column]
private ?int $position = null;
#[ORM\ManyToOne(inversedBy: 'chapters')]
#[ORM\JoinColumn(nullable: false)]
private ?SubjectReference $subjectReference = null;
/**
* Séquence avant la fin de laquelle ce chapitre doit être terminé.
* Nullable : un chapitre peut ne pas avoir de deadline imposée.
*/
#[ORM\ManyToOne]
private ?Quarter $deadlineQuarter = null;
/**
* Date calendaire optionnelle, en complément ou alternative à deadlineQuarter.
* Permet une deadline fixe (ex: 15/11/2025) si le superadmin préfère.
*/
#[ORM\Column(type: 'date', nullable: true)]
private ?\DateTimeInterface $deadlineDate = null;
#[ORM\OneToMany(mappedBy: 'chapter', targetEntity: ChapterProgress::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $progresses;
#[ORM\ManyToOne]
private ?User $author = null;
public function __construct()
{
$this->progresses = new ArrayCollection();
}
public function __toString(): string
{
return $this->title;
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): static
{
$this->title = $title;
return $this;
}
public function getTitleEn(): ?string
{
return $this->titleEn;
}
public function setTitleEn(?string $titleEn): static
{
$this->titleEn = $titleEn;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
$this->description = $description;
return $this;
}
public function getPosition(): ?int
{
return $this->position;
}
public function setPosition(int $position): static
{
$this->position = $position;
return $this;
}
public function getSubjectReference(): ?SubjectReference
{
return $this->subjectReference;
}
public function setSubjectReference(?SubjectReference $subjectReference): static
{
$this->subjectReference = $subjectReference;
return $this;
}
public function getDeadlineQuarter(): ?Quarter
{
return $this->deadlineQuarter;
}
public function setDeadlineQuarter(?Quarter $deadlineQuarter): static
{
$this->deadlineQuarter = $deadlineQuarter;
return $this;
}
public function getDeadlineDate(): ?\DateTimeInterface
{
return $this->deadlineDate;
}
public function setDeadlineDate(?\DateTimeInterface $deadlineDate): static
{
$this->deadlineDate = $deadlineDate;
return $this;
}
/**
* @return Collection<int, ChapterProgress>
*/
public function getProgresses(): Collection
{
return $this->progresses;
}
public function addProgress(ChapterProgress $progress): static
{
if (!$this->progresses->contains($progress)) {
$this->progresses->add($progress);
$progress->setChapter($this);
}
return $this;
}
public function removeProgress(ChapterProgress $progress): static
{
if ($this->progresses->removeElement($progress)) {
if ($progress->getChapter() === $this) {
$progress->setChapter(null);
}
}
return $this;
}
public function getAuthor(): ?User
{
return $this->author;
}
public function setAuthor(?User $author): static
{
$this->author = $author;
return $this;
}
}