src/Entity/Question.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Traits\Timestampable;
  4. use App\Repository\QuestionRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. // Question d'une Activity. `type` reprend en général celui de l'Activity
  9. // parente mais reste porté ici pour permettre, à terme, des activités mixtes.
  10. // `matchMode` ne s'applique qu'aux questions de type short_answer (stratégie
  11. // de correction du texte libre : exact|ci [insensible à la casse]|contains).
  12. #[ORM\HasLifecycleCallbacks]
  13. #[ORM\Entity(repositoryClassQuestionRepository::class)]
  14. class Question
  15. {
  16.     use Timestampable;
  17.     public const MATCH_EXACT 'exact';
  18.     public const MATCH_CASE_INSENSITIVE 'ci';
  19.     public const MATCH_CONTAINS 'contains';
  20.     #[ORM\Id]
  21.     #[ORM\GeneratedValue]
  22.     #[ORM\Column]
  23.     private ?int $id null;
  24.     #[ORM\ManyToOne(inversedBy'questions')]
  25.     #[ORM\JoinColumn(nullablefalseonDelete'CASCADE')]
  26.     private ?Activity $activity null;
  27.     #[ORM\Column(length20)]
  28.     private ?string $type null;
  29.     #[ORM\Column(type'text')]
  30.     private ?string $text null;
  31.     #[ORM\Column]
  32.     private int $position 0;
  33.     #[ORM\Column(length20nullabletrue)]
  34.     private ?string $matchMode null;
  35.     // Compétence évaluée par CETTE question précise (module Apprentissage) —
  36.     // niveau le plus spécifique de la chaîne de résolution, voir
  37.     // getEffectiveLearningCompetency(). Utile quand un même exercice mélange
  38.     // des questions qui ne testent pas toutes le même savoir-faire.
  39.     #[ORM\ManyToOne]
  40.     #[ORM\JoinColumn(nullabletrueonDelete'SET NULL')]
  41.     private ?LearningCompetency $learningCompetency null;
  42.     #[ORM\OneToMany(mappedBy'question'targetEntityQuestionOption::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  43.     #[ORM\OrderBy(['position' => 'ASC'])]
  44.     private Collection $options;
  45.     public function __construct()
  46.     {
  47.         $this->options = new ArrayCollection();
  48.     }
  49.     public function getId(): ?int
  50.     {
  51.         return $this->id;
  52.     }
  53.     public function getActivity(): ?Activity
  54.     {
  55.         return $this->activity;
  56.     }
  57.     public function setActivity(?Activity $activity): static
  58.     {
  59.         $this->activity $activity;
  60.         return $this;
  61.     }
  62.     public function getType(): ?string
  63.     {
  64.         return $this->type;
  65.     }
  66.     public function setType(string $type): static
  67.     {
  68.         $this->type $type;
  69.         return $this;
  70.     }
  71.     public function getText(): ?string
  72.     {
  73.         return $this->text;
  74.     }
  75.     public function setText(string $text): static
  76.     {
  77.         $this->text $text;
  78.         return $this;
  79.     }
  80.     public function getPosition(): int
  81.     {
  82.         return $this->position;
  83.     }
  84.     public function setPosition(int $position): static
  85.     {
  86.         $this->position $position;
  87.         return $this;
  88.     }
  89.     public function getMatchMode(): ?string
  90.     {
  91.         return $this->matchMode;
  92.     }
  93.     public function setMatchMode(?string $matchMode): static
  94.     {
  95.         $this->matchMode $matchMode;
  96.         return $this;
  97.     }
  98.     public function getLearningCompetency(): ?LearningCompetency
  99.     {
  100.         return $this->learningCompetency;
  101.     }
  102.     public function setLearningCompetency(?LearningCompetency $learningCompetency): static
  103.     {
  104.         $this->learningCompetency $learningCompetency;
  105.         return $this;
  106.     }
  107.     /**
  108.      * Compétence réellement mesurée par cette question, en remontant la
  109.      * chaîne question → activité → capsule jusqu'à trouver un niveau qui en
  110.      * précise une (le plus spécifique gagne) — voir demande utilisateur :
  111.      * "associer une capsule ou un quiz à cette compétence, ou des questions
  112.      * des exercices à cette compétence".
  113.      */
  114.     public function getEffectiveLearningCompetency(): ?LearningCompetency
  115.     {
  116.         return $this->learningCompetency
  117.             ?? $this->activity?->getLearningCompetency()
  118.             ?? $this->activity?->getBlock()?->getCapsule()?->getLearningCompetency();
  119.     }
  120.     /** @return Collection<int, QuestionOption> */
  121.     public function getOptions(): Collection
  122.     {
  123.         return $this->options;
  124.     }
  125.     public function addOption(QuestionOption $option): static
  126.     {
  127.         if (!$this->options->contains($option)) {
  128.             $this->options->add($option);
  129.             $option->setQuestion($this);
  130.         }
  131.         return $this;
  132.     }
  133.     public function removeOption(QuestionOption $option): static
  134.     {
  135.         if ($this->options->removeElement($option)) {
  136.             if ($option->getQuestion() === $this) {
  137.                 $option->setQuestion(null);
  138.             }
  139.         }
  140.         return $this;
  141.     }
  142. }