<?phpnamespace App\Entity;use App\Repository\ScoringRepository;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\ORM\Mapping as ORM;/** * @ORM\Entity(repositoryClass=ScoringRepository::class) */class Scoring{ /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private $id; /** * @ORM\ManyToOne(targetEntity=ScoringQuiz::class, inversedBy="scorings") * @ORM\JoinColumn(nullable=false) */ private $scoringQuiz; /** * @ORM\ManyToOne(targetEntity=User::class, inversedBy="scorings") * @ORM\JoinColumn(nullable=false) */ private $user; /** * @ORM\OneToMany(targetEntity=ScoringAnswer::class, mappedBy="scoring", orphanRemoval=true) */ private $scoringAnswers; /** * @ORM\Column(type="datetime_immutable", nullable=true) */ private $downloadedAt; public function __construct() { $this->scoringAnswers = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getScoringQuiz(): ?scoringQuiz { return $this->scoringQuiz; } public function setScoringQuiz(?scoringQuiz $scoringQuiz): self { $this->scoringQuiz = $scoringQuiz; return $this; } public function getUser(): ?User { return $this->user; } public function setUser(?User $user): self { $this->user = $user; return $this; } /** * @return Collection<int, ScoringAnswer> */ public function getScoringAnswers(): Collection { return $this->scoringAnswers; } public function addScoringAnswer(ScoringAnswer $scoringAnswer): self { if (!$this->scoringAnswers->contains($scoringAnswer)) { $this->scoringAnswers[] = $scoringAnswer; $scoringAnswer->setScoring($this); } return $this; } public function removeScoringAnswer(ScoringAnswer $scoringAnswer): self { if ($this->scoringAnswers->removeElement($scoringAnswer)) { // set the owning side to null (unless already changed) if ($scoringAnswer->getScoring() === $this) { $scoringAnswer->setScoring(null); } } return $this; } public function getDownloadedAt(): ?\DateTimeImmutable { return $this->downloadedAt; } public function setDownloadedAt(?\DateTimeImmutable $downloadedAt): self { $this->downloadedAt = $downloadedAt; return $this; } public function calculateTotalWeightedScore($weighted = true): float { $total = 0; foreach ($this->getScoringQuiz()->getScoringSections() as $section) { $total += $section->calculateSectionScore($weighted); } return $total; } public function getGlobalResultDisplay(): ?ScoringQuizResultDisplay { $score = $this->calculateTotalWeightedScore(); $roundedScore = round($score); foreach ($this->getScoringQuiz()->getResultDisplayConfigs() as $config) { if ($roundedScore >= $config->getMinScore() && $roundedScore <= $config->getMaxScore()) { return $config; } } return null; } public function getSectionCompletionRatio(ScoringSection $section): string { $totalQuestions = $section->getScoringQuestions()->count(); if ($totalQuestions === 0) { return 0; } $answeredQuestions = $this->scoringAnswers->filter( fn(ScoringAnswer $answer) => $answer->getScoringQuestion()->getScoringSection() === $section && $answer->getPropositionIndex() !== null )->count(); return $answeredQuestions . ' / ' .$totalQuestions; } public function getGlobalCompletionRatio(): string { $totalQuestions = 0; $answeredQuestions = 0; foreach ($this->getScoringQuiz()->getScoringSections() as $section) { $sectionQuestions = $section->getScoringQuestions()->count(); $totalQuestions += $sectionQuestions; $answeredQuestions += $this->scoringAnswers->filter( fn(ScoringAnswer $answer) => $answer->getScoringQuestion()->getScoringSection() === $section && $answer->getPropositionIndex() !== null )->count(); } if ($totalQuestions === 0) { return 0; } return $answeredQuestions . ' / ' .$totalQuestions; }}