<?php
namespace App\Entity;
use App\Repository\SubjectLearningUnitRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Traits\Timestampable;
/**
* Unité d'Apprentissage (UA) LOCALE d'une matière dans un établissement.
*
* Pendant de LearningUnit (programme national) pour la partie du cahier de
* texte "hors programme national" — voir SubjectModule pour le même
* principe côté Thème. Scopée au Subject réel (matière + classe + année +
* école), comme SubjectLesson.
*
* `subjectModule` (local) est indépendant de `module` national :
* SubjectLesson peut être rattachée à une UA locale sans passer par un
* thème local (subjectModule null), exactement comme LearningUnit peut
* exister sans Module parent côté programme national.
*/
#[ORM\HasLifecycleCallbacks]
#[ORM\Entity(repositoryClass: SubjectLearningUnitRepository::class)]
class SubjectLearningUnit
{
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]
private ?int $position = null;
#[ORM\ManyToOne(inversedBy: 'subjectLearningUnits')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
private ?Subject $subject = null;
#[ORM\ManyToOne(inversedBy: 'subjectLearningUnits')]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
private ?SubjectModule $subjectModule = null;
/**
* Thème national (Module) rattaché DIRECTEMENT, sans passer par une
* copie locale — même principe que SubjectLesson::$module : permet à
* l'école de classer une UA locale sous un thème du programme national
* existant plutôt que de forcément créer un SubjectModule pour ça.
* Indépendant de $subjectModule (l'un OU l'autre, jamais les deux —
* voir normalizeTheme()).
*/
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
private ?Module $module = null;
#[ORM\OneToMany(mappedBy: 'subjectLearningUnit', targetEntity: SubjectLesson::class)]
private Collection $subjectLessons;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
private ?User $author = null;
public function __construct()
{
$this->subjectLessons = 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 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 getSubjectModule(): ?SubjectModule
{
return $this->subjectModule;
}
public function setSubjectModule(?SubjectModule $subjectModule): static
{
$this->subjectModule = $subjectModule;
return $this;
}
public function getModule(): ?Module
{
return $this->module;
}
public function setModule(?Module $module): static
{
$this->module = $module;
return $this;
}
/**
* Libellé du thème effectif (local OU national), pour affichage uniforme
* dans les templates — voir SubjectLesson::getEffectiveThemeLabel()
* pour le même principe.
*/
public function getEffectiveThemeLabel(): ?string
{
return $this->subjectModule?->getTitle() ?? $this->module?->getTitle();
}
/**
* Empêche l'état incohérent "thème local ET thème national tous les
* deux renseignés" — voir SubjectLesson::normalizeTheme() pour le même
* principe (le formulaire propose les deux comme deux champs
* indépendants, voir SubjectLearningUnitType).
*/
public function normalizeTheme(): void
{
if ($this->subjectModule !== null) {
$this->module = null;
}
}
/** @return Collection<int, SubjectLesson> */
public function getSubjectLessons(): Collection
{
return $this->subjectLessons;
}
public function getAuthor(): ?User
{
return $this->author;
}
public function setAuthor(?User $author): static
{
$this->author = $author;
return $this;
}
}