<?php
declare(strict_types=1);
namespace CoreBundle\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use UserBundle\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Table("quiz")
* @ORM\Entity(repositoryClass="CoreBundle\Entity\QuizRepository")
*/
class Quiz implements HasMediaInterface
{
use HasMediaTraits;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(type="integer", nullable=false, options={"default":0})
*/
private $duration;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=500, nullable=true)
*/
private $description;
/**
* @var bool
*
* @ORM\Column(name="enabled", type="boolean", options={"default":1})
*/
private $enabled;
/**
* @var Publication
*
* @ORM\ManyToOne(targetEntity="Publication", inversedBy="quizzes")
* @ORM\JoinColumn(name="publication_id", referencedColumnName="id")
*/
private $publication;
/**
* @ORM\OneToMany(targetEntity="RelationQuizMathproblem", mappedBy="quiz", cascade={"persist"})
*/
private Collection $mathproblems;
/**
* @var array
*
* @ORM\Column(name="problem_order", type="simple_array", nullable=true)
*/
private $problemOrder;
/**
* @var array
*
* @ORM\Column(name="mathproblems_for_templates", type="simple_array", nullable=true)
*/
private $mathproblemsForTemplates;
/**
* @var bool
*
* @ORM\Column(name="reusable", type="boolean", nullable=false)
*/
private $reusable;
/**
* @var bool
*
* @ORM\Column(name="abacus", type="boolean", nullable=true, options={"default":0})
*/
private $abacus;
/**
* @var bool
*
* @ORM\Column(name="target_student", type="boolean", nullable=true, options={"default":0})
*/
private $targetStudent;
/**
* @var Topic
*
* @ORM\ManyToOne(targetEntity="CoreBundle\Entity\Topic")
* @ORM\JoinColumn(name="topic", referencedColumnName="id")
*/
private $topic;
/**
* @var \DateTime
*
* @ORM\Column(name="createdAt", type="datetime", nullable=false)
*/
private $createdAt;
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="UserBundle\Entity\User")
* @ORM\JoinColumn(name="created_by", referencedColumnName="id")
*/
private $createdBy;
/**
* @var User[]
*
* @ORM\ManyToMany(targetEntity="UserBundle\Entity\User")
* @ORM\JoinTable(name="quiz_teacher",
* joinColumns={@ORM\JoinColumn(name="quiz_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="teacher_id", referencedColumnName="id")})
*/
private $teachers;
/**
* @var QuizSession[]
*
* @ORM\OneToMany(targetEntity="CoreBundle\Entity\QuizSession", mappedBy="quiz", cascade={"persist"})
*/
private $quizSessions;
/**
* @var ClassroomType
*
* @ORM\ManyToOne(targetEntity="CoreBundle\Entity\ClassroomType")
* @ORM\JoinColumn(name="level", referencedColumnName="id", nullable=true)
*/
private $level;
/**
* @var QuizType
*
* @ORM\ManyToOne(targetEntity="CoreBundle\Entity\QuizType")
* @ORM\JoinColumn(name="type", referencedColumnName="id", nullable=true)
*/
private $type;
/**
* @var array
*
* @ORM\Column(name="color_wrong_count_threshold", type="simple_array", nullable=true)
*/
private $colorWrongCountThreshold;
/**
* @var array
*
* @ORM\Column(name="color_percentage_threshold", type="simple_array", nullable=true)
*/
private $colorPercentageThreshold;
/**
* @var string
*
* @ORM\Column(name="ibook_id", type="string", length=255, nullable=true)
*/
private ?string $iBookId;
/**
* @ORM\OneToOne(targetEntity="VideoReference", inversedBy="quiz")
* @ORM\JoinColumn(name="video_reference", referencedColumnName="id", nullable=true)
*/
private ?VideoReference $videoReference;
public function __construct()
{
$this->enabled = true;
$this->mathproblems = new ArrayCollection();
$this->teachers = new ArrayCollection();
$this->quizSessions = new ArrayCollection();
$this->createdAt = new \DateTime("now");
$this->imageReference = null;
$this->videoReference = null;
$this->audioReference = null;
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Set duration.
*
* @param int $duration
*
* @return $this
*/
public function setDuration($duration)
{
$this->duration = $duration;
return $this;
}
/**
* Get duration.
*
* @return int
*/
public function getDuration()
{
return $this->duration;
}
/**
* @return ArrayCollection|Mathproblem[]
*/
public function getMathproblems(): Collection
{
$mathproblems = new ArrayCollection();
foreach ($this->mathproblems as $mathproblem) {
$mathproblems->add($mathproblem->getMathproblem());
}
return $mathproblems;
}
/**
* @return ArrayCollection|Mathproblem[]
*/
public function getEnabledMathproblems(): Collection
{
$mathproblems = new ArrayCollection();
foreach ($this->mathproblems as $mathproblem) {
if ($mathproblem->getEnabled()) {
$mathproblems->add($mathproblem->getMathproblem());
}
}
return $mathproblems;
}
/**
* @return ArrayCollection|Mathproblem[]
*/
public function getEnabledAndPublishedMathproblems(): Collection
{
$mathproblems = new ArrayCollection();
foreach ($this->mathproblems as $mathproblem) {
if ($mathproblem->getEnabled() && $mathproblem->getMathproblem()->isPublished()) {
$mathproblems->add($mathproblem->getMathproblem());
}
}
return $mathproblems;
}
/**
* @return array
*/
public function getSortedMathproblems()
{
$problemOrder = $this->problemOrder;
$mathproblems = $this->getEnabledMathproblems();
if ($problemOrder && count($problemOrder) == count($mathproblems)) {
$newmathproblems = [];
foreach ($mathproblems as $mp) {
$newkey = array_search($mp->getID(), $problemOrder);
$newmathproblems[$newkey] = $mp;
}
ksort($newmathproblems);
return $newmathproblems;
} else {
return $this->getEnabledMathproblems()->toArray();
}
}
/**
* @param Mathproblem[]
*/
public function setMathproblems(array $mathproblems)
{
// don't allow setting problems to a quiz that already has problems
if ($this->mathproblems->count() > 0) {
throw new BadRequestException('Cannot set mathproblems on a quiz that already has mathproblems');
}
foreach ($mathproblems as $mathproblem) {
$this->addMathproblem($mathproblem);
}
}
/**
* This collection can be used to edit the relations directly,
* remember to also remove the entity, when removing a relation.
*
* @return ArrayCollection|Collection
*/
public function getMathproblemRelations(): ArrayCollection|Collection
{
return $this->mathproblems;
}
/**
* @param Mathproblem
*/
public function addMathproblem($mathproblem)
{
$relation = new RelationQuizMathproblem();
$relation->setQuiz($this);
$relation->setMathproblem($mathproblem);
$relation->setEnabled(true);
$this->mathproblems->add($relation);
}
/**
* @return bool
*/
public function getReusable()
{
return $this->reusable;
}
/**
* @param bool $reusable
*
* @return $this
*/
public function setReusable($reusable)
{
$this->reusable = $reusable;
return $this;
}
/**
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* @return User
*/
public function getCreatedBy()
{
return $this->createdBy;
}
/**
* @param User $createdBy
*
* @return $this
*/
public function setCreatedBy($createdBy)
{
$this->createdBy = $createdBy;
return $this;
}
/**
* @return Collection
*/
public function getTeachers(): Collection
{
return $this->teachers;
}
/**
* @param User $teacher
*
* @return $this
*/
public function addTeacher(User $teacher)
{
$this->teachers->add($teacher);
return $this;
}
/**
* @return Collection
*/
public function getQuizSessions(): Collection
{
return $this->quizSessions;
}
/**
* @param QuizSession[] $quizSessions
*
* @return $this
*/
public function setQuizSessions($quizSessions)
{
$this->quizSessions = $quizSessions;
return $this;
}
/**
* @param QuizSession $quizSession
*
* @return $this
*/
public function addQuizSession(QuizSession $quizSession)
{
$this->quizSessions->add($quizSession);
$quizSession->setQuiz($this);
return $this;
}
/**
* @return bool
*/
public function isAbacus()
{
return $this->abacus;
}
/**
* @param bool $abacus
*/
public function setAbacus($abacus)
{
$this->abacus = $abacus;
}
/**
* @return bool | null
*/
public function isTargetStudent()
{
return $this->targetStudent;
}
/**
* @param bool $targetStudent
*/
public function setTargetStudent(bool $targetStudent)
{
$this->targetStudent = $targetStudent;
}
/**
* @return Topic | null
*/
public function getTopic()
{
return $this->topic;
}
/**
* @param Topic $topic
*/
public function setTopic(Topic $topic)
{
$this->topic = $topic;
}
/**
* @return ClassroomType|null
*/
public function getLevel(): ?ClassroomType
{
return $this->level;
}
/**
* @param ClassroomType|null $level
*/
public function setLevel(?ClassroomType $level): void
{
$this->level = $level;
}
/**
* @return array|null
*/
public function getProblemOrder(): ?array
{
return $this->problemOrder;
}
/**
* @param array $problemOrder
*/
public function setProblemOrder(array $problemOrder)
{
$this->problemOrder = $problemOrder;
}
/**
* @return QuizType | null
*/
public function getType(): QuizType | null
{
return $this->type;
}
/**
* @param QuizType $type
*/
public function setType(QuizType $type): void
{
$this->type = $type;
}
/**
* @return array|null
*/
public function getMathproblemsForTemplates()
{
return $this->mathproblemsForTemplates;
}
/**
* @param array $mathproblemsForTemplates
*/
public function setMathproblemsForTemplates(array $mathproblemsForTemplates): void
{
$this->mathproblemsForTemplates = $mathproblemsForTemplates;
}
/**
* @return string | null
*/
public function getDescription(): string | null
{
return $this->description;
}
/**
* @param string $description
*/
public function setDescription(string $description): void
{
$this->description = $description;
}
/**
* @return array|null
*/
public function getColorWrongCountThreshold(): ?array
{
return $this->colorWrongCountThreshold;
}
public function getColorPercentageThreshold(): ?array
{
return $this->colorPercentageThreshold;
}
public function getPublication(): ?Publication
{
return $this->publication;
}
public function setPublication(Publication $publication): void
{
$this->publication = $publication;
}
public function getEnabled(): bool
{
return $this->enabled;
}
public function setEnabled(string $enabled): void
{
$this->enabled = $enabled;
}
public function __toString()
{
return (string) $this->id;
}
public function getCmsImagePath(): string
{
if (!$this->imageReference) {
return 'Mangler';
}
if (!$this->imageReference->getFilePath()) {
return 'Mangler fil';
}
return $this->getImagePath();
}
public function getVideoReference(): ?VideoReference
{
return $this->videoReference;
}
public function setVideoReference(?VideoReference $videoReference): void
{
$this->videoReference = $videoReference;
}
public function getIBookId(): ?string
{
return $this->iBookId;
}
public function setIBookId(): void
{
$part1 = bin2hex(random_bytes(4));
$part2 = $this->id;
$this->iBookId = $part1 . '_' . $part2;
}
public function isABaCusQuiz(): bool
{
// abacus/kvik quizzes have abacus=1 and targetStudent=0
return $this->abacus && !$this->targetStudent;
}
public function setIsABaCusQuiz($isABaCusQuiz): void
{
if ($isABaCusQuiz) {
$this->abacus = true;
$this->targetStudent = false;
} else {
$this->abacus = null;
$this->targetStudent = null;
}
}
public function getProblemCount(): int
{
return count($this->getEnabledMathproblems());
}
}