<?php
namespace App\Entity;
use App\Repository\ScoringRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Instance;
/**
* @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 calculateTotalWeightedScore($weighted = true): float
{
$total = 0;
foreach ($this->getScoringQuiz()->getScoringSections() as $section) {
// on lui passe "moi" (le scoring courant)
$total += $section->calculateSectionScore( $weighted,$this);
}
return $total;
}
public function getGlobalResultDisplay(): ?ScoringQuizResultDisplay
{
$score = $this->calculateTotalWeightedScore();
foreach ($this->getScoringQuiz()->getResultDisplayConfigs() as $config) {
if ($score >= $config->getMinScore() && $score <= $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;
}
/**
* @ORM\Column(type="boolean", options={"default": false})
*/
private bool $isFinished = false;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private ?\DateTimeInterface $endDate = null;
public function isFinished(): bool
{
return $this->isFinished;
}
public function setIsFinished(bool $isFinished): self
{
$this->isFinished = $isFinished;
return $this;
}
public function getEndDate(): ?\DateTimeInterface
{
return $this->endDate;
}
public function setEndDate(?\DateTimeInterface $endDate): self
{
$this->endDate = $endDate;
return $this;
}
/**
* Instance associée au scoring (nullable pour ne pas casser l’existant)
* @ORM\ManyToOne(targetEntity=Instance::class)
* @ORM\JoinColumn(name="instance_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private ?Instance $instance = null;
public function getInstance(): ?Instance
{
return $this->instance;
}
public function setInstance(?Instance $instance): self
{
$this->instance = $instance;
return $this;
}
}