<?php
namespace App\Entity;
use App\Repository\QuizRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: QuizRepository::class)]
class Quiz
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $titre = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description = null;
// 'qcm' | 'qcu' | 'mixte'
#[ORM\Column(length: 50, nullable: true)]
private ?string $type = 'mixte';
// Durée en minutes (null = pas de limite)
#[ORM\Column(nullable: true)]
private ?int $dureeMinutes = null;
// Nombre de tentatives autorisées (null = illimité)
#[ORM\Column(nullable: true)]
private ?int $maxTentatives = null;
// Afficher résultats après soumission ?
#[ORM\Column]
private bool $afficherResultat = true;
// Mélanger les questions ?
#[ORM\Column]
private bool $melangerQuestions = false;
// Mélanger les options ?
#[ORM\Column]
private bool $melangerOptions = false;
// Score minimum pour réussir (%)
#[ORM\Column(nullable: true)]
private ?int $scoreMinimum = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $createdAt = null;
#[ORM\Column]
private bool $isPublished = false;
#[ORM\OneToMany(mappedBy: 'quiz', targetEntity: QuizQuestion::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $questions;
#[ORM\OneToMany(mappedBy: 'quiz', targetEntity: QuizAssignment::class ,
cascade: ['remove'])]
private Collection $assignments;
#[ORM\OneToMany(mappedBy: 'quiz', targetEntity: QuizReponse::class ,
cascade: ['remove'])]
private Collection $reponses;
#[ORM\ManyToOne(inversedBy: 'quizzes')]
private ?User $createdBy = null;
#[ORM\Column(nullable: true)]
private ?int $nbQuestionsAffichees = null;
public function __construct()
{
$this->questions = new ArrayCollection();
$this->assignments = new ArrayCollection();
$this->reponses = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitre(): ?string { return $this->titre; }
public function setTitre(string $titre): self { $this->titre = $titre; return $this; }
public function getDescription(): ?string { return $this->description; }
public function setDescription(?string $d): self { $this->description = $d; return $this; }
public function getType(): ?string { return $this->type; }
public function setType(?string $t): self { $this->type = $t; return $this; }
public function getDureeMinutes(): ?int { return $this->dureeMinutes; }
public function setDureeMinutes(?int $d): self { $this->dureeMinutes = $d; return $this; }
public function getMaxTentatives(): ?int { return $this->maxTentatives; }
public function setMaxTentatives(?int $m): self { $this->maxTentatives = $m; return $this; }
public function isAfficherResultat(): bool { return $this->afficherResultat; }
public function setAfficherResultat(bool $a): self { $this->afficherResultat = $a; return $this; }
public function isMelangerQuestions(): bool { return $this->melangerQuestions; }
public function setMelangerQuestions(bool $m): self { $this->melangerQuestions = $m; return $this; }
public function isMelangerOptions(): bool { return $this->melangerOptions; }
public function setMelangerOptions(bool $m): self { $this->melangerOptions = $m; return $this; }
public function getScoreMinimum(): ?int { return $this->scoreMinimum; }
public function setScoreMinimum(?int $s): self { $this->scoreMinimum = $s; return $this; }
public function getCreatedAt(): ?\DateTimeInterface { return $this->createdAt; }
public function setCreatedAt(\DateTimeInterface $d): self { $this->createdAt = $d; return $this; }
public function isPublished(): bool { return $this->isPublished; }
public function setIsPublished(bool $p): self { $this->isPublished = $p; return $this; }
/**
* @return Collection<int, QuizQuestion>
*/
public function getQuestions(): Collection
{
return $this->questions;
}
public function addQuestion(QuizQuestion $question): self
{
if (!$this->questions->contains($question)) {
$this->questions->add($question);
$question->setQuiz($this);
}
return $this;
}
public function removeQuestion(QuizQuestion $question): self
{
if ($this->questions->removeElement($question)) {
// set the owning side to null (unless already changed)
if ($question->getQuiz() === $this) {
$question->setQuiz(null);
}
}
return $this;
}
/**
* @return Collection<int, QuizAssignment>
*/
public function getAssignments(): Collection
{
return $this->assignments;
}
public function addAssignment(QuizAssignment $assignment): self
{
if (!$this->assignments->contains($assignment)) {
$this->assignments->add($assignment);
$assignment->setQuiz($this);
}
return $this;
}
public function removeAssignment(QuizAssignment $assignment): self
{
if ($this->assignments->removeElement($assignment)) {
// set the owning side to null (unless already changed)
if ($assignment->getQuiz() === $this) {
$assignment->setQuiz(null);
}
}
return $this;
}
/**
* @return Collection<int, QuizReponse>
*/
public function getReponses(): Collection
{
return $this->reponses;
}
public function addReponse(QuizReponse $reponse): self
{
if (!$this->reponses->contains($reponse)) {
$this->reponses->add($reponse);
$reponse->setQuiz($this);
}
return $this;
}
public function removeReponse(QuizReponse $reponse): self
{
if ($this->reponses->removeElement($reponse)) {
// set the owning side to null (unless already changed)
if ($reponse->getQuiz() === $this) {
$reponse->setQuiz(null);
}
}
return $this;
}
// Calcul du total de points
public function getTotalPoints(): int {
$total = 0;
foreach ($this->questions as $q) {
$total += $q->getPoints() ?? 1;
}
return $total;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
public function getNbQuestionsAffichees(): ?int
{
return $this->nbQuestionsAffichees;
}
public function setNbQuestionsAffichees(?int $nbQuestionsAffichees): self
{
$this->nbQuestionsAffichees = $nbQuestionsAffichees;
return $this;
}
// Dans src/Entity/Quiz.php, ajoutez cette méthode :
// Calcul du total de points pour les questions affichées (utile pour les examens)
public function getTotalPointsAffiches(): int {
$total = 0;
// Si c'est un examen avec un nombre limité, on calcule sur toutes les questions
// car le total des points doit correspondre au max possible
foreach ($this->questions as $q) {
$total += $q->getPoints() ?? 1;
}
return $total;
}
// Pour l'affichage, on utilise getTotalPoints() qui donne le total général
}