src/CoreBundle/Entity/Topic.php line 20

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