<?php
namespace App\Entity;
use App\Repository\QuizQuestionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: QuizQuestionRepository::class)]
class QuizQuestion
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'questions')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
private ?Quiz $quiz = null;
#[ORM\Column(type: Types::TEXT)]
private ?string $enonce = null;
// 'qcu' = choix unique, 'qcm' = choix multiples,
// 'texte' = réponse libre, 'vrai_faux'
#[ORM\Column(length: 20)]
private string $typeQuestion = 'qcu';
#[ORM\Column(nullable: true)]
private ?int $points = 1;
#[ORM\Column]
private int $position = 0;
// Image optionnelle pour la question
#[ORM\Column(length: 255, nullable: true)]
private ?string $image = null;
// Explication affichée après réponse
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $explication = null;
#[ORM\Column]
private bool $obligatoire = true;
#[ORM\OneToMany(mappedBy: 'question', targetEntity: QuizOption::class , cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $options;
#[ORM\OneToMany(mappedBy: 'question', targetEntity: QuizReponseDetail::class)]
private Collection $details;
public function __construct()
{
$this->options = new ArrayCollection();
$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 getEnonce(): ?string { return $this->enonce; }
public function setEnonce(string $e): self { $this->enonce = $e; return $this; }
public function getTypeQuestion(): string { return $this->typeQuestion; }
public function setTypeQuestion(string $t): self { $this->typeQuestion = $t; return $this; }
public function getPoints(): ?int { return $this->points; }
public function setPoints(?int $p): self { $this->points = $p; return $this; }
public function getPosition(): int { return $this->position; }
public function setPosition(int $p): self { $this->position = $p; return $this; }
public function getImage(): ?string { return $this->image; }
public function setImage(?string $i): self { $this->image = $i; return $this; }
public function getExplication(): ?string { return $this->explication; }
public function setExplication(?string $e): self { $this->explication = $e; return $this; }
public function isObligatoire(): bool { return $this->obligatoire; }
public function setObligatoire(bool $o): self { $this->obligatoire = $o; return $this; }
/**
* @return Collection<int, QuizOption>
*/
public function getOptions(): Collection
{
return $this->options;
}
public function addOption(QuizOption $option): self
{
if (!$this->options->contains($option)) {
$this->options->add($option);
$option->setQuestion($this);
}
return $this;
}
public function removeOption(QuizOption $option): self
{
if ($this->options->removeElement($option)) {
// set the owning side to null (unless already changed)
if ($option->getQuestion() === $this) {
$option->setQuestion(null);
}
}
return $this;
}
/**
* @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->setQuestion($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->getQuestion() === $this) {
$detail->setQuestion(null);
}
}
return $this;
}
// Retourne les bonnes réponses
public function getBonnesReponses(): Collection {
return $this->options->filter(fn($o) => $o->isCorrect());
}
}