src/Entity/QuizReponse.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\QuizReponseRepository;
  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(repositoryClassQuizReponseRepository::class)]
  9. class QuizReponse
  10. {
  11.     #[ORM\Id]
  12.     #[ORM\GeneratedValue]
  13.     #[ORM\Column]
  14.     private ?int $id null;
  15.     #[ORM\ManyToOne(inversedBy'reponses')]
  16.     private ?Quiz $quiz null;
  17.     #[ORM\ManyToOne(inversedBy'reponses')]
  18.     private ?User $stagiaire null;
  19.     #[ORM\ManyToOne(inversedBy'reponses')]
  20.     private ?QuizAssignment $assignment null;
  21.     #[ORM\Column(nullabletrue)]
  22.     private ?float $scorePourcentage null;
  23.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  24.     private ?\DateTimeInterface $startedAt null;
  25.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  26.     private ?\DateTimeInterface $submittedAt null;
  27.     // Score obtenu (points)
  28.     #[ORM\Column(nullabletrue)]
  29.     private ?float $score null;
  30.     #[ORM\Column]
  31.     private int $tentativeNumero 1;
  32.     #[ORM\OneToMany(mappedBy'reponse'targetEntityQuizReponseDetail::class , cascade: ['persist''remove'], orphanRemovaltrue)]
  33.     private Collection $details;
  34.     #[ORM\Column(length255nullabletrue)]
  35.     private ?string $statut  'en_cours';
  36.     public function __construct()
  37.     {
  38.         $this->details = new ArrayCollection();
  39.     }
  40.     public function getId(): ?int
  41.     {
  42.         return $this->id;
  43.     }
  44.     public function getQuiz(): ?Quiz
  45.     {
  46.         return $this->quiz;
  47.     }
  48.     public function setQuiz(?Quiz $quiz): self
  49.     {
  50.         $this->quiz $quiz;
  51.         return $this;
  52.     }
  53.     public function getStagiaire(): ?User
  54.     {
  55.         return $this->stagiaire;
  56.     }
  57.     public function setStagiaire(?User $stagiaire): self
  58.     {
  59.         $this->stagiaire $stagiaire;
  60.         return $this;
  61.     }
  62.     public function getAssignment(): ?QuizAssignment
  63.     {
  64.         return $this->assignment;
  65.     }
  66.     public function setAssignment(?QuizAssignment $assignment): self
  67.     {
  68.         $this->assignment $assignment;
  69.         return $this;
  70.     }
  71.     public function getStartedAt(): ?\DateTimeInterface { return $this->startedAt; }
  72.     public function setStartedAt(\DateTimeInterface $d): self $this->startedAt $d; return $this; }
  73.     public function getSubmittedAt(): ?\DateTimeInterface { return $this->submittedAt; }
  74.     public function setSubmittedAt(?\DateTimeInterface $d): self $this->submittedAt $d; return $this; }
  75.     public function getScore(): ?float { return $this->score; }
  76.     public function setScore(?float $s): self $this->score $s; return $this; }
  77.     public function getScorePourcentage(): ?float { return $this->scorePourcentage; }
  78.     public function setScorePourcentage(?float $s): self $this->scorePourcentage $s; return $this; }
  79.     public function getStatut(): string { return $this->statut; }
  80.     public function setStatut(string $s): self $this->statut $s; return $this; }
  81.     public function getTentativeNumero(): int { return $this->tentativeNumero; }
  82.     public function setTentativeNumero(int $n): self $this->tentativeNumero $n; return $this; }
  83.     public function isReussi(): bool {
  84.         $min $this->quiz?->getScoreMinimum();
  85.         if ($min === null) return true;
  86.         return ($this->scorePourcentage ?? 0) >= $min;
  87.     }
  88.     // Durée en secondes
  89.     /**
  90.      * Durée en secondes
  91.      * CORRECTION : Vérifie que les deux dates existent
  92.      */
  93.     public function getDuree(): ?int
  94.     {
  95.         // Vérifier que submittedAt ET startedAt existent tous les deux
  96.         if (!$this->submittedAt || !$this->startedAt) {
  97.             return null;
  98.         }
  99.         try {
  100.             return $this->submittedAt->getTimestamp() - $this->startedAt->getTimestamp();
  101.         } catch (\Exception $e) {
  102.             return null;
  103.         }
  104.     }
  105.     /**
  106.      * Durée formatée (minutes:secondes)
  107.      */
  108.     public function getDureeFormatee(): string
  109.     {
  110.         $duree $this->getDuree();
  111.         if ($duree === null) {
  112.             return '—';
  113.         }
  114.         $minutes floor($duree 60);
  115.         $secondes $duree 60;
  116.         return sprintf('%d min %02d s'$minutes$secondes);
  117.     }
  118.     /**
  119.      * Durée en minutes (arrondie)
  120.      */
  121.     public function getDureeMinutes(): ?int
  122.     {
  123.         $duree $this->getDuree();
  124.         if ($duree === null) {
  125.             return null;
  126.         }
  127.         return (int) round($duree 60);
  128.     }
  129.     /**
  130.      * @return Collection<int, QuizReponseDetail>
  131.      */
  132.     public function getDetails(): Collection
  133.     {
  134.         return $this->details;
  135.     }
  136.     public function addDetail(QuizReponseDetail $detail): self
  137.     {
  138.         if (!$this->details->contains($detail)) {
  139.             $this->details->add($detail);
  140.             $detail->setReponse($this);
  141.         }
  142.         return $this;
  143.     }
  144.     public function removeDetail(QuizReponseDetail $detail): self
  145.     {
  146.         if ($this->details->removeElement($detail)) {
  147.             // set the owning side to null (unless already changed)
  148.             if ($detail->getReponse() === $this) {
  149.                 $detail->setReponse(null);
  150.             }
  151.         }
  152.         return $this;
  153.     }
  154. }