src/Entity/Core/Topic/Topic.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Core\Topic;
  3. use App\Entity\Core\Category;
  4. use App\Entity\Core\Classroom\ClassroomType;
  5. use App\Entity\Core\Session\Session;
  6. use App\Entity\Core\TemplateMathproblem;
  7. use DateTime;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use JsonSerializable;
  12. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity as UniqueEntity;
  13. use Symfony\Component\Yaml\Yaml;
  14. #[ORM\Table('topic')]
  15. #[UniqueEntity('name')]
  16. #[ORM\Entity(repositoryClass: TopicRepository::class)]
  17. class Topic implements JsonSerializable
  18. {
  19. /**
  20. * @var int
  21. */
  22. #[ORM\Column(name: 'id', type: 'integer')]
  23. #[ORM\Id]
  24. #[ORM\GeneratedValue(strategy: 'AUTO')]
  25. private $id;
  26. #[ORM\Column(type: 'string', length: 255, unique: true)]
  27. private string $name;
  28. #[ORM\Column(type: 'string', length: 255, nullable: true)]
  29. private ?string $displayName = null;
  30. #[ORM\Column(name: 'selfstudy_name', type: 'string', length: 255, nullable: true)]
  31. private ?string $selfstudyName;
  32. /**
  33. * @var Category
  34. */
  35. #[ORM\JoinColumn(name: 'category_id', referencedColumnName: 'id')]
  36. #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'topics')]
  37. private $category;
  38. /**
  39. * @var ArrayCollection|TemplateMathproblem[]
  40. */
  41. #[ORM\OneToMany(targetEntity: TemplateMathproblem::class, mappedBy: 'topic', cascade: ['persist'])]
  42. private $templates;
  43. /**
  44. * @var ArrayCollection|Session[]
  45. *
  46. */
  47. #[ORM\ManyToMany(targetEntity: Session::class, mappedBy: 'topics')]
  48. private $sessions;
  49. /**
  50. * @var bool $enabled
  51. */
  52. #[ORM\Column(name: 'enabled', type: 'boolean', unique: false, options: ['default' => true])]
  53. private $enabled;
  54. /**
  55. * The type of math student 1=A, 2=A+B, 3=A+B+C, null = A+B+C
  56. * @var int
  57. */
  58. #[ORM\Column(type: 'smallint', nullable: true)]
  59. private $curriculum;
  60. /**
  61. * 1=important, 0=not important
  62. * @var int
  63. */
  64. #[ORM\Column(name: 'exam_priority', type: 'smallint', nullable: true)]
  65. private $examPriority;
  66. /**
  67. * @var int
  68. */
  69. #[ORM\Column(name: 'sort_order', type: 'smallint', nullable: true)]
  70. private $sortOrder;
  71. /**
  72. * @var bool
  73. */
  74. #[ORM\Column(name: 'visible_in_training', type: 'boolean', options: ['default' => true], nullable: false)]
  75. private bool $visibleInTraining;
  76. /**
  77. * @var bool
  78. */
  79. #[ORM\Column(name: 'visible_in_quiz', type: 'boolean', options: ['default' => true], nullable: false)]
  80. private bool $visibleInQuiz;
  81. /**
  82. * @var ArrayCollection
  83. */
  84. #[ORM\JoinTable(name: 'topics_classroomtypes')]
  85. #[ORM\JoinColumn(name: 'topic_id', referencedColumnName: 'id')]
  86. #[ORM\InverseJoinColumn(name: 'classroomtype_id', referencedColumnName: 'id')]
  87. #[ORM\ManyToMany(targetEntity: ClassroomType::class)]
  88. private $classroomTypes;
  89. private static $translation = [];
  90. /**
  91. * @var ?DateTime
  92. */
  93. #[ORM\Column(name: 'deleted_at', type: 'datetime', nullable: true)]
  94. private ?DateTime $deletedAt;
  95. public function __construct()
  96. {
  97. $this->templates = new ArrayCollection();
  98. $this->sessions = new ArrayCollection();
  99. $this->classroomTypes = new ArrayCollection();
  100. }
  101. public function getId(): ?int
  102. {
  103. return $this->id;
  104. }
  105. public function setName(string $name): self
  106. {
  107. $this->name = $name;
  108. return $this;
  109. }
  110. public function getName(): string
  111. {
  112. return $this->name;
  113. }
  114. public function setSelfStudyName(?string $name): self
  115. {
  116. $this->selfstudyName = $name;
  117. return $this;
  118. }
  119. public function getSelfStudyName(): ?string
  120. {
  121. return $this->selfstudyName;
  122. }
  123. /**
  124. * Set category.
  125. *
  126. * @param Category $category
  127. *
  128. * @return Topic
  129. */
  130. public function setCategory($category)
  131. {
  132. $this->category = $category;
  133. return $this;
  134. }
  135. /**
  136. * Get category.
  137. *
  138. * @return Category
  139. */
  140. public function getCategory()
  141. {
  142. return $this->category;
  143. }
  144. /**
  145. * @return TemplateMathproblem[]
  146. */
  147. public function getTemplates()
  148. {
  149. return $this->templates;
  150. }
  151. /**
  152. * @return Topic
  153. */
  154. public function addTemplate(TemplateMathproblem $template)
  155. {
  156. $template->setTopic($this);
  157. $this->templates->add($template);
  158. return $this;
  159. }
  160. /**
  161. * @return bool
  162. */
  163. public function removeTemplate(TemplateMathproblem $template)
  164. {
  165. $template->setTopic(null);
  166. return $this->templates->removeElement($template);
  167. }
  168. /**
  169. * @return Session[]
  170. */
  171. public function getSessions()
  172. {
  173. return $this->sessions;
  174. }
  175. /**
  176. * @return Topic
  177. */
  178. public function addSession(Session $session)
  179. {
  180. $this->sessions->add($session);
  181. return $this;
  182. }
  183. /**
  184. * @return bool
  185. */
  186. public function removeSession(Session $session)
  187. {
  188. return $this->sessions->removeElement($session);
  189. }
  190. /**
  191. * @return boolean
  192. */
  193. public function isEnabled()
  194. {
  195. return $this->enabled;
  196. }
  197. /**
  198. * @param boolean $enabled
  199. */
  200. public function setEnabled($enabled)
  201. {
  202. $this->enabled = $enabled;
  203. }
  204. /**
  205. * @return int
  206. */
  207. public function getCurriculum()
  208. {
  209. return $this->curriculum;
  210. }
  211. /**
  212. * @param int $curriculum
  213. */
  214. public function setCurriculum($curriculum)
  215. {
  216. $this->curriculum = $curriculum;
  217. }
  218. /**
  219. * @return int
  220. */
  221. public function getExamPriority()
  222. {
  223. return $this->examPriority;
  224. }
  225. /**
  226. * @param int $examPriority
  227. */
  228. public function setExamPriority($examPriority)
  229. {
  230. $this->examPriority = $examPriority;
  231. }
  232. /**
  233. * @return int
  234. */
  235. public function getSortOrder()
  236. {
  237. return $this->sortOrder;
  238. }
  239. /**
  240. * @param int $sortOrder
  241. */
  242. public function setSortOrder($sortOrder)
  243. {
  244. $this->sortOrder = $sortOrder;
  245. }
  246. public function getClassroomTypes(): Collection
  247. {
  248. return $this->classroomTypes;
  249. }
  250. public function getClassroomTypesList(bool $shouldTruncate=false): string
  251. {
  252. if ($this->classroomTypes->isEmpty()) {
  253. return '-';
  254. }
  255. $classroomTypes = new ArrayCollection();
  256. foreach ($this->classroomTypes as $classroomType) {
  257. $classroomTypes->add($classroomType->getName());
  258. if ($shouldTruncate) {
  259. if ($classroomTypes->count() > 3) {
  260. break;
  261. }
  262. }
  263. }
  264. $classroomTypes = implode(', ', $classroomTypes->toArray());
  265. if ($shouldTruncate) {
  266. $classroomTypes .= ', ...';
  267. }
  268. return $classroomTypes;
  269. }
  270. public function jsonSerialize(): mixed
  271. {
  272. $name = explode('_', $this->name)[0];
  273. $display = null;
  274. if(count(self::$translation) == 0) {
  275. $translation_path_da = dirname($_SERVER['DOCUMENT_ROOT']) . '/translations/messages.da.yml';
  276. $translation_path_en = dirname($_SERVER['DOCUMENT_ROOT']) . '/translations/messages.en.yml';
  277. $translation_en = Yaml::parse(file_get_contents($translation_path_en))['abacus']['problem_types'];
  278. $translation_da = Yaml::parse(file_get_contents($translation_path_da))['abacus']['problem_types'];
  279. self::$translation = [];
  280. foreach ($translation_da as $key => $value) {
  281. self::$translation[$translation_en[$key]] = $value;
  282. }
  283. }
  284. if(isset(self::$translation[$name])){
  285. $display = self::$translation[$name];
  286. }
  287. return [
  288. "id" => $this->id,
  289. "name" => $name,
  290. "displayName" => $this->getDisplayName(),
  291. "category" => $this->getCategory(),
  292. "display" => $display
  293. ];
  294. }
  295. public function isVisibleInTraining(): bool
  296. {
  297. return $this->visibleInTraining;
  298. }
  299. public function setVisibleInTraining(bool $visibleInTraining): void
  300. {
  301. $this->visibleInTraining = $visibleInTraining;
  302. }
  303. public function isVisibleInQuiz(): bool
  304. {
  305. return $this->visibleInQuiz;
  306. }
  307. public function setVisibleInQuiz(bool $visibleInQuiz): void
  308. {
  309. $this->visibleInQuiz = $visibleInQuiz;
  310. }
  311. public function getDisplayName(): string
  312. {
  313. if ($this->displayName) {
  314. return $this->displayName;
  315. }
  316. return $this->name;
  317. }
  318. public function setDisplayName(?string $displayName): void
  319. {
  320. $this->displayName = $displayName;
  321. }
  322. public function __toString(): string
  323. {
  324. return $this->getDisplayName();
  325. }
  326. public function hasDisplayName(): bool
  327. {
  328. if (mb_strlen($this->displayName) > 0) {
  329. return true;
  330. }
  331. return false;
  332. }
  333. public function getDisplayNameOrSanitizedName(): string
  334. {
  335. return $this->hasDisplayName() ? $this->getDisplayName() : explode("_", $this->getName())[0];
  336. }
  337. public function getSelfStudyNameOrSanitizedName(): string
  338. {
  339. return $this->selfstudyName ?: explode("_", $this->getName())[0];
  340. }
  341. public function getDeletedAt(): ?DateTime
  342. {
  343. return $this->deletedAt;
  344. }
  345. public function setDeletedAt(?DateTime $deletedAt): void
  346. {
  347. $this->deletedAt = $deletedAt;
  348. }
  349. public function areAllProblemsPublished(): bool
  350. {
  351. foreach ($this->templates as $template) {
  352. foreach ($template->getMathproblems() as $mathproblem) {
  353. if (!$mathproblem->isPublished()) {
  354. return false;
  355. }
  356. }
  357. }
  358. return true;
  359. }
  360. }