<?php
namespace App\Entity\Core\Topic;
use App\Entity\Core\Category;
use App\Entity\Core\Classroom\ClassroomType;
use App\Entity\Core\Session\Session;
use App\Entity\Core\TemplateMathproblem;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity as UniqueEntity;
use Symfony\Component\Yaml\Yaml;
#[ORM\Table('topic')]
#[UniqueEntity('name')]
#[ORM\Entity(repositoryClass: TopicRepository::class)]
class Topic implements JsonSerializable
{
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
#[ORM\Column(type: 'string', length: 255, unique: true)]
private string $name;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $displayName = null;
#[ORM\Column(name: 'selfstudy_name', type: 'string', length: 255, nullable: true)]
private ?string $selfstudyName;
/**
* @var Category
*/
#[ORM\JoinColumn(name: 'category_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'topics')]
private $category;
/**
* @var ArrayCollection|TemplateMathproblem[]
*/
#[ORM\OneToMany(targetEntity: TemplateMathproblem::class, mappedBy: 'topic', cascade: ['persist'])]
private $templates;
/**
* @var ArrayCollection|Session[]
*
*/
#[ORM\ManyToMany(targetEntity: Session::class, mappedBy: 'topics')]
private $sessions;
/**
* @var bool $enabled
*/
#[ORM\Column(name: 'enabled', type: 'boolean', unique: false, options: ['default' => true])]
private $enabled;
/**
* The type of math student 1=A, 2=A+B, 3=A+B+C, null = A+B+C
* @var int
*/
#[ORM\Column(type: 'smallint', nullable: true)]
private $curriculum;
/**
* 1=important, 0=not important
* @var int
*/
#[ORM\Column(name: 'exam_priority', type: 'smallint', nullable: true)]
private $examPriority;
/**
* @var int
*/
#[ORM\Column(name: 'sort_order', type: 'smallint', nullable: true)]
private $sortOrder;
/**
* @var bool
*/
#[ORM\Column(name: 'visible_in_training', type: 'boolean', options: ['default' => true], nullable: false)]
private bool $visibleInTraining;
/**
* @var bool
*/
#[ORM\Column(name: 'visible_in_quiz', type: 'boolean', options: ['default' => true], nullable: false)]
private bool $visibleInQuiz;
/**
* @var ArrayCollection
*/
#[ORM\JoinTable(name: 'topics_classroomtypes')]
#[ORM\JoinColumn(name: 'topic_id', referencedColumnName: 'id')]
#[ORM\InverseJoinColumn(name: 'classroomtype_id', referencedColumnName: 'id')]
#[ORM\ManyToMany(targetEntity: ClassroomType::class)]
private $classroomTypes;
private static $translation = [];
/**
* @var ?DateTime
*/
#[ORM\Column(name: 'deleted_at', type: 'datetime', nullable: true)]
private ?DateTime $deletedAt;
public function __construct()
{
$this->templates = new ArrayCollection();
$this->sessions = new ArrayCollection();
$this->classroomTypes = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getName(): string
{
return $this->name;
}
public function setSelfStudyName(?string $name): self
{
$this->selfstudyName = $name;
return $this;
}
public function getSelfStudyName(): ?string
{
return $this->selfstudyName;
}
/**
* Set category.
*
* @param Category $category
*
* @return Topic
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
* Get category.
*
* @return Category
*/
public function getCategory()
{
return $this->category;
}
/**
* @return TemplateMathproblem[]
*/
public function getTemplates()
{
return $this->templates;
}
/**
* @return Topic
*/
public function addTemplate(TemplateMathproblem $template)
{
$template->setTopic($this);
$this->templates->add($template);
return $this;
}
/**
* @return bool
*/
public function removeTemplate(TemplateMathproblem $template)
{
$template->setTopic(null);
return $this->templates->removeElement($template);
}
/**
* @return Session[]
*/
public function getSessions()
{
return $this->sessions;
}
/**
* @return Topic
*/
public function addSession(Session $session)
{
$this->sessions->add($session);
return $this;
}
/**
* @return bool
*/
public function removeSession(Session $session)
{
return $this->sessions->removeElement($session);
}
/**
* @return boolean
*/
public function isEnabled()
{
return $this->enabled;
}
/**
* @param boolean $enabled
*/
public function setEnabled($enabled)
{
$this->enabled = $enabled;
}
/**
* @return int
*/
public function getCurriculum()
{
return $this->curriculum;
}
/**
* @param int $curriculum
*/
public function setCurriculum($curriculum)
{
$this->curriculum = $curriculum;
}
/**
* @return int
*/
public function getExamPriority()
{
return $this->examPriority;
}
/**
* @param int $examPriority
*/
public function setExamPriority($examPriority)
{
$this->examPriority = $examPriority;
}
/**
* @return int
*/
public function getSortOrder()
{
return $this->sortOrder;
}
/**
* @param int $sortOrder
*/
public function setSortOrder($sortOrder)
{
$this->sortOrder = $sortOrder;
}
public function getClassroomTypes(): Collection
{
return $this->classroomTypes;
}
public function getClassroomTypesList(bool $shouldTruncate=false): string
{
if ($this->classroomTypes->isEmpty()) {
return '-';
}
$classroomTypes = new ArrayCollection();
foreach ($this->classroomTypes as $classroomType) {
$classroomTypes->add($classroomType->getName());
if ($shouldTruncate) {
if ($classroomTypes->count() > 3) {
break;
}
}
}
$classroomTypes = implode(', ', $classroomTypes->toArray());
if ($shouldTruncate) {
$classroomTypes .= ', ...';
}
return $classroomTypes;
}
public function jsonSerialize(): mixed
{
$name = explode('_', $this->name)[0];
$display = null;
if(count(self::$translation) == 0) {
$translation_path_da = dirname($_SERVER['DOCUMENT_ROOT']) . '/translations/messages.da.yml';
$translation_path_en = dirname($_SERVER['DOCUMENT_ROOT']) . '/translations/messages.en.yml';
$translation_en = Yaml::parse(file_get_contents($translation_path_en))['abacus']['problem_types'];
$translation_da = Yaml::parse(file_get_contents($translation_path_da))['abacus']['problem_types'];
self::$translation = [];
foreach ($translation_da as $key => $value) {
self::$translation[$translation_en[$key]] = $value;
}
}
if(isset(self::$translation[$name])){
$display = self::$translation[$name];
}
return [
"id" => $this->id,
"name" => $name,
"displayName" => $this->getDisplayName(),
"category" => $this->getCategory(),
"display" => $display
];
}
public function isVisibleInTraining(): bool
{
return $this->visibleInTraining;
}
public function setVisibleInTraining(bool $visibleInTraining): void
{
$this->visibleInTraining = $visibleInTraining;
}
public function isVisibleInQuiz(): bool
{
return $this->visibleInQuiz;
}
public function setVisibleInQuiz(bool $visibleInQuiz): void
{
$this->visibleInQuiz = $visibleInQuiz;
}
public function getDisplayName(): string
{
if ($this->displayName) {
return $this->displayName;
}
return $this->name;
}
public function setDisplayName(?string $displayName): void
{
$this->displayName = $displayName;
}
public function __toString(): string
{
return $this->getDisplayName();
}
public function hasDisplayName(): bool
{
if (mb_strlen($this->displayName) > 0) {
return true;
}
return false;
}
public function getDisplayNameOrSanitizedName(): string
{
return $this->hasDisplayName() ? $this->getDisplayName() : explode("_", $this->getName())[0];
}
public function getSelfStudyNameOrSanitizedName(): string
{
return $this->selfstudyName ?: explode("_", $this->getName())[0];
}
public function getDeletedAt(): ?DateTime
{
return $this->deletedAt;
}
public function setDeletedAt(?DateTime $deletedAt): void
{
$this->deletedAt = $deletedAt;
}
public function areAllProblemsPublished(): bool
{
foreach ($this->templates as $template) {
foreach ($template->getMathproblems() as $mathproblem) {
if (!$mathproblem->isPublished()) {
return false;
}
}
}
return true;
}
}