src/Entity/Scoring.php line 13

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. /**
  8.  * @ORM\Entity(repositoryClass=ScoringRepository::class)
  9.  */
  10. class Scoring
  11. {
  12.     /**
  13.      * @ORM\Id
  14.      * @ORM\GeneratedValue
  15.      * @ORM\Column(type="integer")
  16.      */
  17.     private $id;
  18.     /**
  19.      * @ORM\ManyToOne(targetEntity=ScoringQuiz::class, inversedBy="scorings")
  20.      * @ORM\JoinColumn(nullable=false)
  21.      */
  22.     private $scoringQuiz;
  23.     /**
  24.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="scorings")
  25.      * @ORM\JoinColumn(nullable=false)
  26.      */
  27.     private $user;
  28.     /**
  29.      * @ORM\OneToMany(targetEntity=ScoringAnswer::class, mappedBy="scoring", orphanRemoval=true)
  30.      */
  31.     private $scoringAnswers;
  32.     /**
  33.      * @ORM\Column(type="datetime_immutable", nullable=true)
  34.      */
  35.     private $downloadedAt;
  36.     public function __construct()
  37.     {
  38.         $this->scoringAnswers = new ArrayCollection();
  39.     }
  40.     public function getId(): ?int
  41.     {
  42.         return $this->id;
  43.     }
  44.     public function getScoringQuiz(): ?scoringQuiz
  45.     {
  46.         return $this->scoringQuiz;
  47.     }
  48.     public function setScoringQuiz(?scoringQuiz $scoringQuiz): self
  49.     {
  50.         $this->scoringQuiz $scoringQuiz;
  51.         return $this;
  52.     }
  53.     public function getUser(): ?User
  54.     {
  55.         return $this->user;
  56.     }
  57.     public function setUser(?User $user): self
  58.     {
  59.         $this->user $user;
  60.         return $this;
  61.     }
  62.     /**
  63.      * @return Collection<int, ScoringAnswer>
  64.      */
  65.     public function getScoringAnswers(): Collection
  66.     {
  67.         return $this->scoringAnswers;
  68.     }
  69.     public function addScoringAnswer(ScoringAnswer $scoringAnswer): self
  70.     {
  71.         if (!$this->scoringAnswers->contains($scoringAnswer)) {
  72.             $this->scoringAnswers[] = $scoringAnswer;
  73.             $scoringAnswer->setScoring($this);
  74.         }
  75.         return $this;
  76.     }
  77.     public function removeScoringAnswer(ScoringAnswer $scoringAnswer): self
  78.     {
  79.         if ($this->scoringAnswers->removeElement($scoringAnswer)) {
  80.             // set the owning side to null (unless already changed)
  81.             if ($scoringAnswer->getScoring() === $this) {
  82.                 $scoringAnswer->setScoring(null);
  83.             }
  84.         }
  85.         return $this;
  86.     }
  87.     public function getDownloadedAt(): ?\DateTimeImmutable
  88.     {
  89.         return $this->downloadedAt;
  90.     }
  91.     public function setDownloadedAt(?\DateTimeImmutable $downloadedAt): self
  92.     {
  93.         $this->downloadedAt $downloadedAt;
  94.         return $this;
  95.     }
  96.     public function calculateTotalWeightedScore($weighted true): float
  97.     {
  98.         $total 0;
  99.         foreach ($this->getScoringQuiz()->getScoringSections() as $section) {
  100.             $total += $section->calculateSectionScore($weighted);
  101.         }
  102.         return $total;
  103.     }
  104.     public function getGlobalResultDisplay(): ?ScoringQuizResultDisplay
  105.     {
  106.         $score $this->calculateTotalWeightedScore();
  107.         $roundedScore round($score);
  108.         
  109.         foreach ($this->getScoringQuiz()->getResultDisplayConfigs() as $config) {
  110.             if ($roundedScore >= $config->getMinScore() && $roundedScore <= $config->getMaxScore()) {
  111.                 return $config;
  112.             }
  113.         }
  114.         
  115.         return null;
  116.     }
  117.     public function getSectionCompletionRatio(ScoringSection $section): string
  118.     {
  119.         $totalQuestions $section->getScoringQuestions()->count();
  120.         if ($totalQuestions === 0) {
  121.             return 0;
  122.         }
  123.         $answeredQuestions $this->scoringAnswers->filter(
  124.             fn(ScoringAnswer $answer) => $answer->getScoringQuestion()->getScoringSection() === $section
  125.                                     && $answer->getPropositionIndex() !== null
  126.         )->count();
  127.         return $answeredQuestions ' / ' .$totalQuestions;
  128.     }
  129.     public function getGlobalCompletionRatio(): string
  130.     {
  131.         $totalQuestions 0;
  132.         $answeredQuestions 0;
  133.         foreach ($this->getScoringQuiz()->getScoringSections() as $section) {
  134.             $sectionQuestions $section->getScoringQuestions()->count();
  135.             $totalQuestions += $sectionQuestions;
  136.             $answeredQuestions += $this->scoringAnswers->filter(
  137.                 fn(ScoringAnswer $answer) => $answer->getScoringQuestion()->getScoringSection() === $section
  138.                                         && $answer->getPropositionIndex() !== null
  139.             )->count();
  140.         }
  141.         if ($totalQuestions === 0) {
  142.             return 0;
  143.         }
  144.         return $answeredQuestions ' / ' .$totalQuestions;
  145.     }
  146. }