<?php
namespace App\Entity;
use App\Repository\QuizQuestionRepository;
use App\Traits\DateTrait;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=QuizQuestionRepository::class)
*
* @Vich\Uploadable
*/
class QuizQuestion
{
const TYPE_RADIO = 'radio';
const TYPE_CHECKBOX = 'checkbox';
const TYPE = [
'Choix unique' => self::TYPE_RADIO,
'Choix multiple' => self::TYPE_CHECKBOX,
];
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private ?string $text;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $image = NULL;
/**
* @Vich\UploadableField(mapping="custom_product_images", fileNameProperty="image")
*/
private ?File $imageFile = NULL;
/**
* @ORM\Column(type="string", length=255, options={"default":self::TYPE_RADIO})
*/
private string $type = self::TYPE_RADIO;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private ?int $timeLimit;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private ?int $orderIndex;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected ?string $explanation;
/**
* @ORM\OneToMany(targetEntity=QuizAnswer::class, mappedBy="quizQuestion", cascade={"remove", "persist"})
*/
private $quizAnswers;
/**
* @ORM\ManyToOne(targetEntity=Quiz::class, inversedBy="quizQuestions")
*/
private ?Quiz $quiz;
use DateTrait;
public function __construct()
{
$this->quizAnswers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getText(): ?string
{
return $this->text;
}
public function setText( string $text ): QuizQuestion
{
$this->text = $text;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage( ?string $image ): QuizQuestion
{
$this->image = $image;
return $this;
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageFile(File $imageFile): QuizQuestion
{
$this->imageFile = $imageFile;
$this->updatedAt = new DateTime();
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType( string $type ): QuizQuestion
{
$this->type = $type;
return $this;
}
public function getTimeLimit(): ?int
{
return $this->timeLimit;
}
public function setTimeLimit( ?int $timeLimit ): QuizQuestion
{
$this->timeLimit = $timeLimit;
return $this;
}
public function getOrderIndex(): ?int
{
return $this->orderIndex;
}
public function setOrderIndex( ?int $orderIndex ): QuizQuestion
{
$this->orderIndex = $orderIndex;
return $this;
}
public function getExplanation(): ?string
{
return $this->explanation;
}
public function setExplanation( ?string $explanation ): QuizQuestion
{
$this->explanation = $explanation;
return $this;
}
public function getQuizAnswers(): Collection
{
return $this->quizAnswers;
}
public function addQuizAnswer( QuizAnswer $quizAnswer ): QuizQuestion
{
if ( !$this->quizAnswers->contains( $quizAnswer ) ) {
$this->quizAnswers[] = $quizAnswer;
$quizAnswer->setQuizQuestion( $this );
}
return $this;
}
public function removeQuizAnswer( QuizAnswer $quizAnswer ): QuizQuestion
{
if ( $this->quizAnswers->removeElement( $quizAnswer ) ) {
// set the owning side to null (unless already changed)
if ( $quizAnswer->getQuizQuestion() === $this ) {
$quizAnswer->setQuizQuestion( NULL );
}
}
return $this;
}
public function getQuiz(): ?Quiz
{
return $this->quiz;
}
public function setQuiz( ?Quiz $quiz ): QuizQuestion
{
$this->quiz = $quiz;
return $this;
}
public function getCorrectAnswer()
{
return $this->quizAnswers->filter(static function($answer){
return $answer->getIsRight();
})->toArray();
}
public function getWrongAnswer()
{
return $this->quizAnswers->filter(static function($answer){
return !$answer->getIsRight();
})->toArray();
}
public function isTimeLimited(): bool
{
return $this->timeLimit !== NULL && $this->timeLimit > 0;
}
}