src/Entity/Scoring.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ScoringRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use App\Entity\Instance;
  8. /**
  9.  * @ORM\Entity(repositoryClass=ScoringRepository::class)
  10.  */
  11. class Scoring
  12. {
  13.     /**
  14.      * @ORM\Id
  15.      * @ORM\GeneratedValue
  16.      * @ORM\Column(type="integer")
  17.      */
  18.     private $id;
  19.     /**
  20.      * @ORM\ManyToOne(targetEntity=ScoringQuiz::class, inversedBy="scorings")
  21.      * @ORM\JoinColumn(nullable=false)
  22.      */
  23.     private $scoringQuiz;
  24.     /**
  25.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="scorings")
  26.      * @ORM\JoinColumn(nullable=false)
  27.      */
  28.     private $user;
  29.     /**
  30.      * @ORM\OneToMany(targetEntity=ScoringAnswer::class, mappedBy="scoring", orphanRemoval=true)
  31.      */
  32.     private $scoringAnswers;
  33.     /**
  34.      * @ORM\Column(type="datetime_immutable", nullable=true)
  35.      */
  36.     private $downloadedAt;
  37.     public function __construct()
  38.     {
  39.         $this->scoringAnswers = new ArrayCollection();
  40.     }
  41.     public function getId(): ?int
  42.     {
  43.         return $this->id;
  44.     }
  45.     public function getScoringQuiz(): ?scoringQuiz
  46.     {
  47.         return $this->scoringQuiz;
  48.     }
  49.     public function setScoringQuiz(?scoringQuiz $scoringQuiz): self
  50.     {
  51.         $this->scoringQuiz $scoringQuiz;
  52.         return $this;
  53.     }
  54.     public function getUser(): ?User
  55.     {
  56.         return $this->user;
  57.     }
  58.     public function setUser(?User $user): self
  59.     {
  60.         $this->user $user;
  61.         return $this;
  62.     }
  63.     /**
  64.      * @return Collection<int, ScoringAnswer>
  65.      */
  66.     public function getScoringAnswers(): Collection
  67.     {
  68.         return $this->scoringAnswers;
  69.     }
  70.     public function addScoringAnswer(ScoringAnswer $scoringAnswer): self
  71.     {
  72.         if (!$this->scoringAnswers->contains($scoringAnswer)) {
  73.             $this->scoringAnswers[] = $scoringAnswer;
  74.             $scoringAnswer->setScoring($this);
  75.         }
  76.         return $this;
  77.     }
  78.     public function removeScoringAnswer(ScoringAnswer $scoringAnswer): self
  79.     {
  80.         if ($this->scoringAnswers->removeElement($scoringAnswer)) {
  81.             // set the owning side to null (unless already changed)
  82.             if ($scoringAnswer->getScoring() === $this) {
  83.                 $scoringAnswer->setScoring(null);
  84.             }
  85.         }
  86.         return $this;
  87.     }
  88.     public function getDownloadedAt(): ?\DateTimeImmutable
  89.     {
  90.         return $this->downloadedAt;
  91.     }
  92.     public function setDownloadedAt(?\DateTimeImmutable $downloadedAt): self
  93.     {
  94.         $this->downloadedAt $downloadedAt;
  95.         return $this;
  96.     }
  97.     /*public function calculateTotalWeightedScore($weighted = true): float
  98.     {
  99.         $total = 0;
  100.         foreach ($this->getScoringQuiz()->getScoringSections() as $section) {
  101.             $total += $section->calculateSectionScore($weighted);
  102.         }
  103.         return $total;
  104.     }*/
  105.     public function calculateTotalWeightedScore($weighted true): float
  106.     {
  107.         $total 0;
  108.         foreach ($this->getScoringQuiz()->getScoringSections() as $section) {
  109.             // on lui passe "moi" (le scoring courant)
  110.             $total += $section->calculateSectionScore$weighted,$this);
  111.         }
  112.         return $total;
  113.     }
  114.     public function getGlobalResultDisplay(): ?ScoringQuizResultDisplay
  115.     {
  116.         $score $this->calculateTotalWeightedScore();
  117.         
  118.         foreach ($this->getScoringQuiz()->getResultDisplayConfigs() as $config) {
  119.             if ($score >= $config->getMinScore() && $score <= $config->getMaxScore()) {
  120.                 return $config;
  121.             }
  122.         }
  123.         
  124.         return null;
  125.     }
  126.     public function getSectionCompletionRatio(ScoringSection $section): string
  127.     {
  128.         $totalQuestions $section->getScoringQuestions()->count();
  129.         if ($totalQuestions === 0) {
  130.             return 0;
  131.         }
  132.         $answeredQuestions $this->scoringAnswers->filter(
  133.             fn(ScoringAnswer $answer) => $answer->getScoringQuestion()->getScoringSection() === $section
  134.                                     && $answer->getPropositionIndex() !== null
  135.         )->count();
  136.         return $answeredQuestions ' / ' .$totalQuestions;
  137.     }
  138.     public function getGlobalCompletionRatio(): string
  139.     {
  140.         $totalQuestions 0;
  141.         $answeredQuestions 0;
  142.         foreach ($this->getScoringQuiz()->getScoringSections() as $section) {
  143.             $sectionQuestions $section->getScoringQuestions()->count();
  144.             $totalQuestions += $sectionQuestions;
  145.             $answeredQuestions += $this->scoringAnswers->filter(
  146.                 fn(ScoringAnswer $answer) => $answer->getScoringQuestion()->getScoringSection() === $section
  147.                                         && $answer->getPropositionIndex() !== null
  148.             )->count();
  149.         }
  150.         if ($totalQuestions === 0) {
  151.             return 0;
  152.         }
  153.         return $answeredQuestions ' / ' .$totalQuestions;
  154.     }
  155.     /**
  156.      * @ORM\Column(type="boolean", options={"default": false})
  157.      */
  158.     private bool $isFinished false;
  159.     /**
  160.      * @ORM\Column(type="datetime", nullable=true)
  161.      */
  162.     private ?\DateTimeInterface $endDate null;
  163.     public function isFinished(): bool
  164.     {
  165.         return $this->isFinished;
  166.     }
  167.     public function setIsFinished(bool $isFinished): self
  168.     {
  169.         $this->isFinished $isFinished;
  170.         return $this;
  171.     }
  172.     public function getEndDate(): ?\DateTimeInterface
  173.     {
  174.         return $this->endDate;
  175.     }
  176.     public function setEndDate(?\DateTimeInterface $endDate): self
  177.     {
  178.         $this->endDate $endDate;
  179.         return $this;
  180.     }
  181.     /**
  182.      * Instance associée au scoring (nullable pour ne pas casser l’existant)
  183.      * @ORM\ManyToOne(targetEntity=Instance::class)
  184.      * @ORM\JoinColumn(name="instance_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
  185.      */
  186.     private ?Instance $instance null;
  187.     public function getInstance(): ?Instance
  188.     {
  189.         return $this->instance;
  190.     }
  191.     public function setInstance(?Instance $instance): self
  192.     {
  193.         $this->instance $instance;
  194.         return $this;
  195.     }
  196. }