src/Entity/Quiz.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\QuizRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. #[ORM\Entity(repositoryClassQuizRepository::class)]
  9. class Quiz
  10. {
  11.     #[ORM\Id]
  12.     #[ORM\GeneratedValue]
  13.     #[ORM\Column]
  14.     private ?int $id null;
  15.     #[ORM\Column(length255)]
  16.     private ?string $titre null;
  17.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  18.     private ?string $description null;
  19.     // 'qcm' | 'qcu' | 'mixte'
  20.     #[ORM\Column(length50nullabletrue)]
  21.     private ?string $type 'mixte';
  22.     // Durée en minutes (null = pas de limite)
  23.     #[ORM\Column(nullabletrue)]
  24.     private ?int $dureeMinutes null;
  25.     // Nombre de tentatives autorisées (null = illimité)
  26.     #[ORM\Column(nullabletrue)]
  27.     private ?int $maxTentatives null;
  28.     // Afficher résultats après soumission ?
  29.     #[ORM\Column]
  30.     private bool $afficherResultat true;
  31.     // Mélanger les questions ?
  32.     #[ORM\Column]
  33.     private bool $melangerQuestions false;
  34.     // Mélanger les options ?
  35.     #[ORM\Column]
  36.     private bool $melangerOptions false;
  37.     // Score minimum pour réussir (%)
  38.     #[ORM\Column(nullabletrue)]
  39.     private ?int $scoreMinimum null;
  40.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  41.     private ?\DateTimeInterface $createdAt null;
  42.     #[ORM\Column]
  43.     private bool $isPublished false;
  44.     #[ORM\OneToMany(mappedBy'quiz'targetEntityQuizQuestion::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  45.     private Collection $questions;
  46.     #[ORM\OneToMany(mappedBy'quiz'targetEntityQuizAssignment::class ,
  47.         cascade: ['remove'])]
  48.     private Collection $assignments;
  49.     #[ORM\OneToMany(mappedBy'quiz'targetEntityQuizReponse::class ,
  50.         cascade: ['remove'])]
  51.     private Collection $reponses;
  52.     #[ORM\ManyToOne(inversedBy'quizzes')]
  53.     private ?User $createdBy null;
  54.     #[ORM\Column(nullabletrue)]
  55.     private ?int $nbQuestionsAffichees null;
  56.     public function __construct()
  57.     {
  58.         $this->questions = new ArrayCollection();
  59.         $this->assignments = new ArrayCollection();
  60.         $this->reponses = new ArrayCollection();
  61.     }
  62.     public function getId(): ?int
  63.     {
  64.         return $this->id;
  65.     }
  66.     public function getTitre(): ?string { return $this->titre; }
  67.     public function setTitre(string $titre): self $this->titre $titre; return $this; }
  68.     public function getDescription(): ?string { return $this->description; }
  69.     public function setDescription(?string $d): self $this->description $d; return $this; }
  70.     public function getType(): ?string { return $this->type; }
  71.     public function setType(?string $t): self $this->type $t; return $this; }
  72.     public function getDureeMinutes(): ?int { return $this->dureeMinutes; }
  73.     public function setDureeMinutes(?int $d): self $this->dureeMinutes $d; return $this; }
  74.     public function getMaxTentatives(): ?int { return $this->maxTentatives; }
  75.     public function setMaxTentatives(?int $m): self $this->maxTentatives $m; return $this; }
  76.     public function isAfficherResultat(): bool { return $this->afficherResultat; }
  77.     public function setAfficherResultat(bool $a): self $this->afficherResultat $a; return $this; }
  78.     public function isMelangerQuestions(): bool { return $this->melangerQuestions; }
  79.     public function setMelangerQuestions(bool $m): self $this->melangerQuestions $m; return $this; }
  80.     public function isMelangerOptions(): bool { return $this->melangerOptions; }
  81.     public function setMelangerOptions(bool $m): self $this->melangerOptions $m; return $this; }
  82.     public function getScoreMinimum(): ?int { return $this->scoreMinimum; }
  83.     public function setScoreMinimum(?int $s): self $this->scoreMinimum $s; return $this; }
  84.     public function getCreatedAt(): ?\DateTimeInterface { return $this->createdAt; }
  85.     public function setCreatedAt(\DateTimeInterface $d): self $this->createdAt $d; return $this; }
  86.     public function isPublished(): bool { return $this->isPublished; }
  87.     public function setIsPublished(bool $p): self $this->isPublished $p; return $this; }
  88.     /**
  89.      * @return Collection<int, QuizQuestion>
  90.      */
  91.     public function getQuestions(): Collection
  92.     {
  93.         return $this->questions;
  94.     }
  95.     public function addQuestion(QuizQuestion $question): self
  96.     {
  97.         if (!$this->questions->contains($question)) {
  98.             $this->questions->add($question);
  99.             $question->setQuiz($this);
  100.         }
  101.         return $this;
  102.     }
  103.     public function removeQuestion(QuizQuestion $question): self
  104.     {
  105.         if ($this->questions->removeElement($question)) {
  106.             // set the owning side to null (unless already changed)
  107.             if ($question->getQuiz() === $this) {
  108.                 $question->setQuiz(null);
  109.             }
  110.         }
  111.         return $this;
  112.     }
  113.     /**
  114.      * @return Collection<int, QuizAssignment>
  115.      */
  116.     public function getAssignments(): Collection
  117.     {
  118.         return $this->assignments;
  119.     }
  120.     public function addAssignment(QuizAssignment $assignment): self
  121.     {
  122.         if (!$this->assignments->contains($assignment)) {
  123.             $this->assignments->add($assignment);
  124.             $assignment->setQuiz($this);
  125.         }
  126.         return $this;
  127.     }
  128.     public function removeAssignment(QuizAssignment $assignment): self
  129.     {
  130.         if ($this->assignments->removeElement($assignment)) {
  131.             // set the owning side to null (unless already changed)
  132.             if ($assignment->getQuiz() === $this) {
  133.                 $assignment->setQuiz(null);
  134.             }
  135.         }
  136.         return $this;
  137.     }
  138.     /**
  139.      * @return Collection<int, QuizReponse>
  140.      */
  141.     public function getReponses(): Collection
  142.     {
  143.         return $this->reponses;
  144.     }
  145.     public function addReponse(QuizReponse $reponse): self
  146.     {
  147.         if (!$this->reponses->contains($reponse)) {
  148.             $this->reponses->add($reponse);
  149.             $reponse->setQuiz($this);
  150.         }
  151.         return $this;
  152.     }
  153.     public function removeReponse(QuizReponse $reponse): self
  154.     {
  155.         if ($this->reponses->removeElement($reponse)) {
  156.             // set the owning side to null (unless already changed)
  157.             if ($reponse->getQuiz() === $this) {
  158.                 $reponse->setQuiz(null);
  159.             }
  160.         }
  161.         return $this;
  162.     }
  163.     // Calcul du total de points
  164.     public function getTotalPoints(): int {
  165.         $total 0;
  166.         foreach ($this->questions as $q) {
  167.             $total += $q->getPoints() ?? 1;
  168.         }
  169.         return $total;
  170.     }
  171.     public function getCreatedBy(): ?User
  172.     {
  173.         return $this->createdBy;
  174.     }
  175.     public function setCreatedBy(?User $createdBy): self
  176.     {
  177.         $this->createdBy $createdBy;
  178.         return $this;
  179.     }
  180.     public function getNbQuestionsAffichees(): ?int
  181.     {
  182.         return $this->nbQuestionsAffichees;
  183.     }
  184.     public function setNbQuestionsAffichees(?int $nbQuestionsAffichees): self
  185.     {
  186.         $this->nbQuestionsAffichees $nbQuestionsAffichees;
  187.         return $this;
  188.     }
  189.     // Dans src/Entity/Quiz.php, ajoutez cette méthode :
  190. // Calcul du total de points pour les questions affichées (utile pour les examens)
  191.     public function getTotalPointsAffiches(): int {
  192.         $total 0;
  193.         // Si c'est un examen avec un nombre limité, on calcule sur toutes les questions
  194.         // car le total des points doit correspondre au max possible
  195.         foreach ($this->questions as $q) {
  196.             $total += $q->getPoints() ?? 1;
  197.         }
  198.         return $total;
  199.     }
  200. // Pour l'affichage, on utilise getTotalPoints() qui donne le total général
  201. }