<?php
namespace CoreBundle\Entity;
use AdminBundle\Controller\EasyAdmin\ProblemType;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use UserBundle\Entity\User;
use JsonSerializable;
use DateTime;
/**
* @ORM\Entity
* @ORM\Table("template_mathproblem",indexes={
* @ORM\Index(name="difficulty_idx", columns={"difficulty"}),
* @ORM\Index(name="type_idx", columns={"type"})
* })
* @ORM\Entity(repositoryClass="TemplateMathproblemRepository")
*
*/
class TemplateMathproblem implements JsonSerializable
{
// types used in database
public const TYPE_RADIO_BUTTON = 0;
public const TYPE_NUMBER = 1;
public const TYPE_DRAG_AND_DROP = 4;
public const TYPE_FORMULA = 5;
public const TYPE_DROPDOWN = 6;
public const TYPE_WORD_OPTIONS = 7;
public const TYPE_TEXT = 8;
public const TYPE_PARAGRAPH = 9;
public const TYPE_MARK = 10;
public const TEMPLATE_MATH_PROBLEM_TABLE = 11;
public const TEMPLATE_MATH_PROBLEM_GRID = 12;
public const TEMPLATE_MATH_PROBLEM_TYPES = [
self::TYPE_RADIO_BUTTON,
self::TYPE_NUMBER,
self::TYPE_DRAG_AND_DROP,
self::TYPE_FORMULA,
self::TYPE_DROPDOWN,
self::TYPE_WORD_OPTIONS,
self::TYPE_TEXT,
self::TYPE_PARAGRAPH,
self::TYPE_MARK
];
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="UserBundle\Entity\User")
*/
private $creator;
/**
* @ORM\Column(name="is_help_allowed", type="boolean")
*/
private bool $helpAllowed;
/**
* @var int
*
* @ORM\Column(name="difficulty", type="smallint")
*/
private $difficulty;
/**
* Specifies the type of problem:<br>
* 0 = single choice, multiple available answers<br>
* 1 = single choice, text answer<br>
* 4 = Drag and Drop
*
* @var int
*
* @ORM\Column(name="type", type="smallint")
*/
private $type;
/**
* @var bool $isUnique
*
* @ORM\Column(name="is_unique", type="boolean", unique=false)
*/
private $unique;
/**
* @ORM\Column(name="has_priority", type="boolean", unique=false)
*/
private bool $priority;
/**
* @var bool
*
* @ORM\Column(name="is_valid", type="boolean", unique=false, options={"default" : true})
*/
private $valid;
/**
* The mathproblems that are created using this template
*
* @var Collection|ArrayCollection|Mathproblem[]
*
* @ORM\OneToMany(targetEntity="Mathproblem", mappedBy="template", cascade={"persist", "remove"})
*/
private Collection $mathproblems;
/**
* @var Topic
*
* @ORM\ManyToOne(targetEntity="Topic", inversedBy="templates")
* @ORM\JoinColumn(name="topic_id", referencedColumnName="id")
*/
private $topic;
/**
* Target can be all - 0, session - 1, homework - 2, single session - 3, screening - 4
*
* @var string
* @ORM\Column(name="target", type="smallint", nullable=false)
*/
private $target;
/**
* @var ArrayCollection|TagMathproblem[]
*
* @ORM\ManyToMany(targetEntity="TagMathproblem", inversedBy="templates")
* @ORM\JoinTable(name="relation_template_tag",
* joinColumns={@ORM\JoinColumn(name="mpt_tag_id", referencedColumnName="id")})
* inverseJoinColumns={@ORM\JoinColumn(name="mpt_problem_id", referencedColumnName="id")},
*/
private $tags;
/**
* @var string
* @ORM\Column(name="mptopic", type="string", nullable=true)
*/
private $mptopic;
/**
* Mathproblem solving time in minutes
*
* @var int
* @ORM\Column(name="duration", type="smallint")
*/
private $duration;
/**
* @var ArrayCollection|TemplateSet[]
* @ORM\ManyToMany(targetEntity="TemplateSet", mappedBy="templates")
*
*/
private $templateSets;
/**
* @var bool $useForWorksheet
*
* @ORM\Column(name="use_for_worksheet", type="boolean", options={"default" : true})
*/
private $useForWorksheet = true;
/**
* @var bool $useCompactVersion
*
* @ORM\Column(name="use_compact_version", type="boolean", options={"default" : false})
*/
private $useCompactVersion = false;
/**
* @var bool $shouldShuffle
*
* @ORM\Column(name="should_shuffle", type="boolean", options={"default" : true})
*/
private $shouldShuffle = true;
/**
* @var bool $ignoreCase
*
* @ORM\Column(name="ignore_case", type="boolean", options={"default" : false})
*/
private $ignoreCase = false;
/**
* Flag used to count number of valid template mathproblems
* @var bool
*/
private $mathproblemsCount;
/**
* @var bool $answerPositionVertical
*
* @ORM\Column(name="answer_position_vertical", type="boolean", options={"default" : false})
*/
private $answerPositionVertical;
/**
* For the moment type is 4 for both match, block and sort problems. This field is used to differentiate.
* @var ?string $dragAndDropType
* @ORM\Column(name="dnd_type", type="string", nullable=true)
*/
private ?string $dndType;
/**
* @var ?DateTime
*
* @ORM\Column(name="deleted_at", type="datetime", nullable=true)
*/
private ?DateTime $deletedAt;
/**
* TemplateMathproblem constructor.
*/
public function __construct()
{
$this->mathproblems = new ArrayCollection();
$this->tags = new ArrayCollection();
$this->templateSets = new ArrayCollection();
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param Mathproblem[] $mathproblems
*
* @return TemplateMathproblem
*/
public function setAnswer($mathproblems)
{
$this->mathproblems = $mathproblems;
return $this;
}
/**
* @return Collection|ArrayCollection|Mathproblem[]
*/
public function getMathproblems(): Collection
{
return $this->mathproblems;
}
/**
* @param Mathproblem $mathproblem
*
* @return TemplateMathproblem
*/
public function addMathproblem(Mathproblem $mathproblem)
{
$this->mathproblems->add($mathproblem);
return $this;
}
/**
* @param Mathproblem $mathproblem
*
* @return bool
*/
public function removeMathproblem(Mathproblem $mathproblem)
{
return $this->mathproblems->removeElement($mathproblem);
}
/**
* @return Topic
*/
public function getTopic()
{
return $this->topic;
}
/**
* @param Topic $topic
*/
public function setTopic($topic)
{
$this->topic = $topic;
}
/**
* @return int
*/
public function getDifficulty()
{
return $this->difficulty;
}
public function setDifficulty(int $difficulty)
{
$this->difficulty = $difficulty;
}
/**
* @return int
*/
public function getType()
{
return $this->type;
}
public function setType(int $type)
{
$this->type = $type;
}
/**
* @return Collection|ArrayCollection|TagMathproblem[]
*/
public function getTags(): Collection
{
return $this->tags;
}
public function addTag(TagMathproblem $tag)
{
return $this->tags->add($tag);
}
public function getValidTagArray(): array {
$tagList = [];
foreach ($this->tags as $tag) {
if ($tag->isEnabled()) {
$tagList[] = $tag->getName();
}
}
return $tagList;
}
public function getTagList(): string {
$tagArray = $this->getValidTagArray();
if (empty($tagArray)) {
return '-';
}
return implode(', ', $tagArray);
}
/**
* @return ?string
*/
public function getMpTopic()
{
return $this->mptopic;
}
public function setMpTopic(?string $mpTopic): void
{
$this->mptopic = $mpTopic;
}
public function isHelpAllowed(): bool
{
return $this->helpAllowed;
}
public function setHelpAllowed(bool $isAllowed)
{
$this->helpAllowed = $isAllowed;
}
/**
* @return mixed
*/
public function getTarget()
{
return $this->target;
}
/**
* @param mixed $target
*/
public function setTarget($target)
{
$this->target = $target;
}
/**
* @return boolean
*/
public function isUnique()
{
return $this->unique;
}
public function setUnique(bool $isUnique)
{
$this->unique = $isUnique;
}
/**
* @return boolean
*/
public function isValid()
{
return $this->valid;
}
/**
* @param boolean $valid
*/
public function setValid($valid)
{
$this->valid = $valid;
}
/**
* @return int
*/
public function getDuration()
{
return $this->duration;
}
/**
* @param int $duration
*
* @return $this
*/
public function setDuration($duration)
{
$this->duration = $duration;
return $this;
}
public function getCreator(): ?User
{
return $this->creator;
}
public function setCreator(?User $creator)
{
$this->creator = $creator;
}
public function isPriority(): bool
{
return $this->priority;
}
/**
* @param bool $priority
*/
public function setPriority(bool $priority): void
{
$this->priority = $priority;
}
/**
* @return TemplateSet[]|ArrayCollection
*/
public function getTemplateSets()
{
return $this->templateSets;
}
public function addTemplateSet(TemplateSet $templateSet): void
{
if (!$this->templateSets->contains($templateSet)) {
$this->templateSets->add($templateSet);
$templateSet->addTemplate($this);
}
}
/**
* @param TemplateSet[]|ArrayCollection $templateSets
*/
public function setTemplateSets($templateSets): void
{
$this->templateSets = $templateSets;
}
/**
* @return bool
*/
public function isUseForWorksheet(): bool
{
return $this->useForWorksheet;
}
/**
* @param bool $useForWorksheet
*/
public function setUseForWorksheet(bool $useForWorksheet): void
{
$this->useForWorksheet = $useForWorksheet;
}
/**
* @return bool
*/
public function getUseCompactVersion(): bool
{
return $this->useCompactVersion;
}
/**
* @param bool $useCompactVersion
*/
public function setUseCompactVersion(bool $useCompactVersion): void
{
$this->useCompactVersion = $useCompactVersion;
}
/**
* @return bool
*/
public function getShouldShuffle(): bool
{
return $this->shouldShuffle;
}
/**
* @param bool $shouldShuffle
*/
public function setShouldShuffle(bool $shouldShuffle): void
{
$this->shouldShuffle = $shouldShuffle;
}
/**
* @return bool
*/
public function getIgnoreCase(): bool
{
return $this->ignoreCase;
}
/**
* @param bool $ignoreCase
*/
public function setIgnoreCase(bool $ignoreCase): void
{
$this->ignoreCase = $ignoreCase;
}
/**
*
*/
public function setMathproblemsCount()
{
$count = 0;
/** @var Mathproblem $mathproblem */
foreach ($this->mathproblems as $mathproblem) {
if ($mathproblem->isPublished()) {
$count++;
}
}
$this->mathproblemsCount = $count;
}
public function areAllProblemsPublished(): bool
{
foreach ($this->mathproblems as $mathproblem) {
if (!$mathproblem->isPublished() && !$mathproblem->isDeleted()) {
return false;
}
}
return true;
}
/**
* @return bool
*/
public function getMathproblemsCount()
{
return $this->mathproblemsCount;
}
public function isAnswerPositionVertical(): bool
{
return $this->answerPositionVertical;
}
public function setAnswerPositionVertical(bool $answerPositionVertical): void
{
$this->answerPositionVertical = $answerPositionVertical;
}
public function __toString(): string
{
return $this->id;
}
public function getDndType(): ?string
{
return $this->dndType ?? null;
}
public function setDndType(?string $dndType): void
{
$this->dndType = $dndType;
}
public function getProblemTypeAsString(): string {
return ProblemType::getProblemTypeFromTemplate($this)->name();
}
public function getProblemType(): ProblemType
{
return ProblemType::getProblemTypeFromTemplate($this);
}
public function getDeletedAt(): ?DateTime
{
return $this->deletedAt;
}
public function setDeletedAt(?DateTime $deletedAt): void
{
$this->deletedAt = $deletedAt;
}
function jsonSerialize(): mixed
{
return [
"id" => $this->getId(),
"type" => $this->getType(),
"topic" => $this->getTopic(),
"mpTopic" => $this->getMpTopic(),
"difficulty" => $this->getDifficulty(),
"helpAllowed" => $this->isHelpAllowed(),
"tags" => $this->getTags()->toArray(),
"duration" => $this->getDuration(),
"shouldShuffle" => $this->getShouldShuffle(),
"useCompactVersion" => $this->getUseCompactVersion(),
"answerPositionVertical" => $this->isAnswerPositionVertical(),
"templateSets" => $this->getTemplateSets()->toArray(),
"mathproblemCount" => $this->getMathproblemsCount(),
"creator" => $this->getCreator(),
"ignoreCase" => $this->getIgnoreCase(),
"target" => $this->getTarget(),
];
}
}