<?php
namespace App\Entity;
use App\Repository\SubjectChapterRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Traits\Timestampable;
/**
* Chapitre LOCAL d'une matière dans un établissement.
*
* Créé automatiquement par copie du programme national (Chapter) lors
* de l'affectation d'un Subject à un SubjectReference, ou manuellement
* par l'admin/éditeur de l'école.
*
* Chaque école possède sa propre copie, modifiable indépendamment du
* programme national. `sourceChapter` garde la traçabilité de l'origine
* nationale (nullable : null si chapitre 100 % personnalisé).
*
* ChapterProgress pointe désormais ici (et non plus sur Chapter).
*/
#[ORM\HasLifecycleCallbacks]
#[ORM\Entity(repositoryClass: SubjectChapterRepository::class)]
class SubjectChapter
{
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 la progression locale (1, 2, 3...).
* L'admin peut réordonner librement via drag & drop.
*/
#[ORM\Column]
private ?int $position = null;
/**
* La matière réelle (école + classe + année) à laquelle ce chapitre appartient.
*/
#[ORM\ManyToOne(inversedBy: 'subjectChapters')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
private ?Subject $subject = null;
/**
* Chapitre national d'origine.
* NULL si le chapitre a été créé manuellement par l'admin (hors programme national).
* Permet au superadmin de tracer la couverture nationale via cette FK.
*/
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
private ?Chapter $sourceChapter = null;
/**
* Séquence avant la fin de laquelle ce chapitre doit être terminé.
*/
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
private ?Quarter $deadlineQuarter = null;
/**
* Date calendaire optionnelle de deadline (alternative à deadlineQuarter).
*/
#[ORM\Column(type: 'date', nullable: true)]
private ?\DateTimeInterface $deadlineDate = null;
/**
* 1 SubjectChapter → au plus 1 ChapterProgress (même Subject fixé).
*/
#[ORM\OneToMany(
mappedBy: 'subjectChapter',
targetEntity: ChapterProgress::class,
cascade: ['persist', 'remove'],
orphanRemoval: true
)]
private Collection $progresses;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
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 getSubject(): ?Subject
{
return $this->subject;
}
public function setSubject(?Subject $subject): static
{
$this->subject = $subject;
return $this;
}
public function getSourceChapter(): ?Chapter
{
return $this->sourceChapter;
}
public function setSourceChapter(?Chapter $sourceChapter): static
{
$this->sourceChapter = $sourceChapter;
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->setSubjectChapter($this);
}
return $this;
}
public function removeProgress(ChapterProgress $progress): static
{
if ($this->progresses->removeElement($progress)) {
if ($progress->getSubjectChapter() === $this) {
$progress->setSubjectChapter(null);
}
}
return $this;
}
public function getAuthor(): ?User
{
return $this->author;
}
public function setAuthor(?User $author): static
{
$this->author = $author;
return $this;
}
/**
* true = chapitre 100 % custom (hors programme national).
* false = chapitre copié depuis le programme national.
*/
public function isCustom(): bool
{
return $this->sourceChapter === null;
}
}