<?php
namespace App\Entity;
use App\Repository\QuizReponseRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: QuizReponseRepository::class)]
class QuizReponse
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'reponses')]
private ?Quiz $quiz = null;
#[ORM\ManyToOne(inversedBy: 'reponses')]
private ?User $stagiaire = null;
#[ORM\ManyToOne(inversedBy: 'reponses')]
private ?QuizAssignment $assignment = null;
#[ORM\Column(nullable: true)]
private ?float $scorePourcentage = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $startedAt = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $submittedAt = null;
// Score obtenu (points)
#[ORM\Column(nullable: true)]
private ?float $score = null;
#[ORM\Column]
private int $tentativeNumero = 1;
#[ORM\OneToMany(mappedBy: 'reponse', targetEntity: QuizReponseDetail::class , cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $details;
#[ORM\Column(length: 255, nullable: true)]
private ?string $statut = 'en_cours';
public function __construct()
{
$this->details = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getQuiz(): ?Quiz
{
return $this->quiz;
}
public function setQuiz(?Quiz $quiz): self
{
$this->quiz = $quiz;
return $this;
}
public function getStagiaire(): ?User
{
return $this->stagiaire;
}
public function setStagiaire(?User $stagiaire): self
{
$this->stagiaire = $stagiaire;
return $this;
}
public function getAssignment(): ?QuizAssignment
{
return $this->assignment;
}
public function setAssignment(?QuizAssignment $assignment): self
{
$this->assignment = $assignment;
return $this;
}
public function getStartedAt(): ?\DateTimeInterface { return $this->startedAt; }
public function setStartedAt(\DateTimeInterface $d): self { $this->startedAt = $d; return $this; }
public function getSubmittedAt(): ?\DateTimeInterface { return $this->submittedAt; }
public function setSubmittedAt(?\DateTimeInterface $d): self { $this->submittedAt = $d; return $this; }
public function getScore(): ?float { return $this->score; }
public function setScore(?float $s): self { $this->score = $s; return $this; }
public function getScorePourcentage(): ?float { return $this->scorePourcentage; }
public function setScorePourcentage(?float $s): self { $this->scorePourcentage = $s; return $this; }
public function getStatut(): string { return $this->statut; }
public function setStatut(string $s): self { $this->statut = $s; return $this; }
public function getTentativeNumero(): int { return $this->tentativeNumero; }
public function setTentativeNumero(int $n): self { $this->tentativeNumero = $n; return $this; }
public function isReussi(): bool {
$min = $this->quiz?->getScoreMinimum();
if ($min === null) return true;
return ($this->scorePourcentage ?? 0) >= $min;
}
// Durée en secondes
/**
* Durée en secondes
* CORRECTION : Vérifie que les deux dates existent
*/
public function getDuree(): ?int
{
// Vérifier que submittedAt ET startedAt existent tous les deux
if (!$this->submittedAt || !$this->startedAt) {
return null;
}
try {
return $this->submittedAt->getTimestamp() - $this->startedAt->getTimestamp();
} catch (\Exception $e) {
return null;
}
}
/**
* Durée formatée (minutes:secondes)
*/
public function getDureeFormatee(): string
{
$duree = $this->getDuree();
if ($duree === null) {
return '—';
}
$minutes = floor($duree / 60);
$secondes = $duree % 60;
return sprintf('%d min %02d s', $minutes, $secondes);
}
/**
* Durée en minutes (arrondie)
*/
public function getDureeMinutes(): ?int
{
$duree = $this->getDuree();
if ($duree === null) {
return null;
}
return (int) round($duree / 60);
}
/**
* @return Collection<int, QuizReponseDetail>
*/
public function getDetails(): Collection
{
return $this->details;
}
public function addDetail(QuizReponseDetail $detail): self
{
if (!$this->details->contains($detail)) {
$this->details->add($detail);
$detail->setReponse($this);
}
return $this;
}
public function removeDetail(QuizReponseDetail $detail): self
{
if ($this->details->removeElement($detail)) {
// set the owning side to null (unless already changed)
if ($detail->getReponse() === $this) {
$detail->setReponse(null);
}
}
return $this;
}
}