src/CoreBundle/Entity/TemplateMathproblem.php line 22

Open in your IDE?
  1. <?php
  2. namespace CoreBundle\Entity;
  3. use AdminBundle\Controller\EasyAdmin\ProblemType;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use UserBundle\Entity\User;
  8. use JsonSerializable;
  9. use DateTime;
  10. /**
  11. * @ORM\Entity
  12. * @ORM\Table("template_mathproblem",indexes={
  13. * @ORM\Index(name="difficulty_idx", columns={"difficulty"}),
  14. * @ORM\Index(name="type_idx", columns={"type"})
  15. * })
  16. * @ORM\Entity(repositoryClass="TemplateMathproblemRepository")
  17. *
  18. */
  19. class TemplateMathproblem implements JsonSerializable
  20. {
  21. // types used in database
  22. public const TYPE_RADIO_BUTTON = 0;
  23. public const TYPE_NUMBER = 1;
  24. public const TYPE_DRAG_AND_DROP = 4;
  25. public const TYPE_FORMULA = 5;
  26. public const TYPE_DROPDOWN = 6;
  27. public const TYPE_WORD_OPTIONS = 7;
  28. public const TYPE_TEXT = 8;
  29. public const TYPE_PARAGRAPH = 9;
  30. public const TYPE_MARK = 10;
  31. public const TEMPLATE_MATH_PROBLEM_TABLE = 11;
  32. public const TEMPLATE_MATH_PROBLEM_GRID = 12;
  33. public const TEMPLATE_MATH_PROBLEM_TYPES = [
  34. self::TYPE_RADIO_BUTTON,
  35. self::TYPE_NUMBER,
  36. self::TYPE_DRAG_AND_DROP,
  37. self::TYPE_FORMULA,
  38. self::TYPE_DROPDOWN,
  39. self::TYPE_WORD_OPTIONS,
  40. self::TYPE_TEXT,
  41. self::TYPE_PARAGRAPH,
  42. self::TYPE_MARK
  43. ];
  44. /**
  45. * @var int
  46. *
  47. * @ORM\Column(name="id", type="integer")
  48. * @ORM\Id
  49. * @ORM\GeneratedValue(strategy="AUTO")
  50. */
  51. private $id;
  52. /**
  53. * @var User
  54. *
  55. * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User")
  56. */
  57. private $creator;
  58. /**
  59. * @ORM\Column(name="is_help_allowed", type="boolean")
  60. */
  61. private bool $helpAllowed;
  62. /**
  63. * @var int
  64. *
  65. * @ORM\Column(name="difficulty", type="smallint")
  66. */
  67. private $difficulty;
  68. /**
  69. * Specifies the type of problem:<br>
  70. * 0 = single choice, multiple available answers<br>
  71. * 1 = single choice, text answer<br>
  72. * 4 = Drag and Drop
  73. *
  74. * @var int
  75. *
  76. * @ORM\Column(name="type", type="smallint")
  77. */
  78. private $type;
  79. /**
  80. * @var bool $isUnique
  81. *
  82. * @ORM\Column(name="is_unique", type="boolean", unique=false)
  83. */
  84. private $unique;
  85. /**
  86. * @ORM\Column(name="has_priority", type="boolean", unique=false)
  87. */
  88. private bool $priority;
  89. /**
  90. * @var bool
  91. *
  92. * @ORM\Column(name="is_valid", type="boolean", unique=false, options={"default" : true})
  93. */
  94. private $valid;
  95. /**
  96. * The mathproblems that are created using this template
  97. *
  98. * @var Collection|ArrayCollection|Mathproblem[]
  99. *
  100. * @ORM\OneToMany(targetEntity="Mathproblem", mappedBy="template", cascade={"persist", "remove"})
  101. */
  102. private Collection $mathproblems;
  103. /**
  104. * @var Topic
  105. *
  106. * @ORM\ManyToOne(targetEntity="Topic", inversedBy="templates")
  107. * @ORM\JoinColumn(name="topic_id", referencedColumnName="id")
  108. */
  109. private $topic;
  110. /**
  111. * Target can be all - 0, session - 1, homework - 2, single session - 3, screening - 4
  112. *
  113. * @var string
  114. * @ORM\Column(name="target", type="smallint", nullable=false)
  115. */
  116. private $target;
  117. /**
  118. * @var ArrayCollection|TagMathproblem[]
  119. *
  120. * @ORM\ManyToMany(targetEntity="TagMathproblem", inversedBy="templates")
  121. * @ORM\JoinTable(name="relation_template_tag",
  122. * joinColumns={@ORM\JoinColumn(name="mpt_tag_id", referencedColumnName="id")})
  123. * inverseJoinColumns={@ORM\JoinColumn(name="mpt_problem_id", referencedColumnName="id")},
  124. */
  125. private $tags;
  126. /**
  127. * @var string
  128. * @ORM\Column(name="mptopic", type="string", nullable=true)
  129. */
  130. private $mptopic;
  131. /**
  132. * Mathproblem solving time in minutes
  133. *
  134. * @var int
  135. * @ORM\Column(name="duration", type="smallint")
  136. */
  137. private $duration;
  138. /**
  139. * @var ArrayCollection|TemplateSet[]
  140. * @ORM\ManyToMany(targetEntity="TemplateSet", mappedBy="templates")
  141. *
  142. */
  143. private $templateSets;
  144. /**
  145. * @var bool $useForWorksheet
  146. *
  147. * @ORM\Column(name="use_for_worksheet", type="boolean", options={"default" : true})
  148. */
  149. private $useForWorksheet = true;
  150. /**
  151. * @var bool $useCompactVersion
  152. *
  153. * @ORM\Column(name="use_compact_version", type="boolean", options={"default" : false})
  154. */
  155. private $useCompactVersion = false;
  156. /**
  157. * @var bool $shouldShuffle
  158. *
  159. * @ORM\Column(name="should_shuffle", type="boolean", options={"default" : true})
  160. */
  161. private $shouldShuffle = true;
  162. /**
  163. * @var bool $ignoreCase
  164. *
  165. * @ORM\Column(name="ignore_case", type="boolean", options={"default" : false})
  166. */
  167. private $ignoreCase = false;
  168. /**
  169. * Flag used to count number of valid template mathproblems
  170. * @var bool
  171. */
  172. private $mathproblemsCount;
  173. /**
  174. * @var bool $answerPositionVertical
  175. *
  176. * @ORM\Column(name="answer_position_vertical", type="boolean", options={"default" : false})
  177. */
  178. private $answerPositionVertical;
  179. /**
  180. * For the moment type is 4 for both match, block and sort problems. This field is used to differentiate.
  181. * @var ?string $dragAndDropType
  182. * @ORM\Column(name="dnd_type", type="string", nullable=true)
  183. */
  184. private ?string $dndType;
  185. /**
  186. * @var ?DateTime
  187. *
  188. * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
  189. */
  190. private ?DateTime $deletedAt;
  191. /**
  192. * TemplateMathproblem constructor.
  193. */
  194. public function __construct()
  195. {
  196. $this->mathproblems = new ArrayCollection();
  197. $this->tags = new ArrayCollection();
  198. $this->templateSets = new ArrayCollection();
  199. }
  200. /**
  201. * Get id.
  202. *
  203. * @return int
  204. */
  205. public function getId()
  206. {
  207. return $this->id;
  208. }
  209. /**
  210. * @param Mathproblem[] $mathproblems
  211. *
  212. * @return TemplateMathproblem
  213. */
  214. public function setAnswer($mathproblems)
  215. {
  216. $this->mathproblems = $mathproblems;
  217. return $this;
  218. }
  219. /**
  220. * @return Collection|ArrayCollection|Mathproblem[]
  221. */
  222. public function getMathproblems(): Collection
  223. {
  224. return $this->mathproblems;
  225. }
  226. /**
  227. * @param Mathproblem $mathproblem
  228. *
  229. * @return TemplateMathproblem
  230. */
  231. public function addMathproblem(Mathproblem $mathproblem)
  232. {
  233. $this->mathproblems->add($mathproblem);
  234. return $this;
  235. }
  236. /**
  237. * @param Mathproblem $mathproblem
  238. *
  239. * @return bool
  240. */
  241. public function removeMathproblem(Mathproblem $mathproblem)
  242. {
  243. return $this->mathproblems->removeElement($mathproblem);
  244. }
  245. /**
  246. * @return Topic
  247. */
  248. public function getTopic()
  249. {
  250. return $this->topic;
  251. }
  252. /**
  253. * @param Topic $topic
  254. */
  255. public function setTopic($topic)
  256. {
  257. $this->topic = $topic;
  258. }
  259. /**
  260. * @return int
  261. */
  262. public function getDifficulty()
  263. {
  264. return $this->difficulty;
  265. }
  266. public function setDifficulty(int $difficulty)
  267. {
  268. $this->difficulty = $difficulty;
  269. }
  270. /**
  271. * @return int
  272. */
  273. public function getType()
  274. {
  275. return $this->type;
  276. }
  277. public function setType(int $type)
  278. {
  279. $this->type = $type;
  280. }
  281. /**
  282. * @return Collection|ArrayCollection|TagMathproblem[]
  283. */
  284. public function getTags(): Collection
  285. {
  286. return $this->tags;
  287. }
  288. public function addTag(TagMathproblem $tag)
  289. {
  290. return $this->tags->add($tag);
  291. }
  292. public function getValidTagArray(): array {
  293. $tagList = [];
  294. foreach ($this->tags as $tag) {
  295. if ($tag->isEnabled()) {
  296. $tagList[] = $tag->getName();
  297. }
  298. }
  299. return $tagList;
  300. }
  301. public function getTagList(): string {
  302. $tagArray = $this->getValidTagArray();
  303. if (empty($tagArray)) {
  304. return '-';
  305. }
  306. return implode(', ', $tagArray);
  307. }
  308. /**
  309. * @return ?string
  310. */
  311. public function getMpTopic()
  312. {
  313. return $this->mptopic;
  314. }
  315. public function setMpTopic(?string $mpTopic): void
  316. {
  317. $this->mptopic = $mpTopic;
  318. }
  319. public function isHelpAllowed(): bool
  320. {
  321. return $this->helpAllowed;
  322. }
  323. public function setHelpAllowed(bool $isAllowed)
  324. {
  325. $this->helpAllowed = $isAllowed;
  326. }
  327. /**
  328. * @return mixed
  329. */
  330. public function getTarget()
  331. {
  332. return $this->target;
  333. }
  334. /**
  335. * @param mixed $target
  336. */
  337. public function setTarget($target)
  338. {
  339. $this->target = $target;
  340. }
  341. /**
  342. * @return boolean
  343. */
  344. public function isUnique()
  345. {
  346. return $this->unique;
  347. }
  348. public function setUnique(bool $isUnique)
  349. {
  350. $this->unique = $isUnique;
  351. }
  352. /**
  353. * @return boolean
  354. */
  355. public function isValid()
  356. {
  357. return $this->valid;
  358. }
  359. /**
  360. * @param boolean $valid
  361. */
  362. public function setValid($valid)
  363. {
  364. $this->valid = $valid;
  365. }
  366. /**
  367. * @return int
  368. */
  369. public function getDuration()
  370. {
  371. return $this->duration;
  372. }
  373. /**
  374. * @param int $duration
  375. *
  376. * @return $this
  377. */
  378. public function setDuration($duration)
  379. {
  380. $this->duration = $duration;
  381. return $this;
  382. }
  383. public function getCreator(): ?User
  384. {
  385. return $this->creator;
  386. }
  387. public function setCreator(?User $creator)
  388. {
  389. $this->creator = $creator;
  390. }
  391. public function isPriority(): bool
  392. {
  393. return $this->priority;
  394. }
  395. /**
  396. * @param bool $priority
  397. */
  398. public function setPriority(bool $priority): void
  399. {
  400. $this->priority = $priority;
  401. }
  402. /**
  403. * @return TemplateSet[]|ArrayCollection
  404. */
  405. public function getTemplateSets()
  406. {
  407. return $this->templateSets;
  408. }
  409. public function addTemplateSet(TemplateSet $templateSet): void
  410. {
  411. if (!$this->templateSets->contains($templateSet)) {
  412. $this->templateSets->add($templateSet);
  413. $templateSet->addTemplate($this);
  414. }
  415. }
  416. /**
  417. * @param TemplateSet[]|ArrayCollection $templateSets
  418. */
  419. public function setTemplateSets($templateSets): void
  420. {
  421. $this->templateSets = $templateSets;
  422. }
  423. /**
  424. * @return bool
  425. */
  426. public function isUseForWorksheet(): bool
  427. {
  428. return $this->useForWorksheet;
  429. }
  430. /**
  431. * @param bool $useForWorksheet
  432. */
  433. public function setUseForWorksheet(bool $useForWorksheet): void
  434. {
  435. $this->useForWorksheet = $useForWorksheet;
  436. }
  437. /**
  438. * @return bool
  439. */
  440. public function getUseCompactVersion(): bool
  441. {
  442. return $this->useCompactVersion;
  443. }
  444. /**
  445. * @param bool $useCompactVersion
  446. */
  447. public function setUseCompactVersion(bool $useCompactVersion): void
  448. {
  449. $this->useCompactVersion = $useCompactVersion;
  450. }
  451. /**
  452. * @return bool
  453. */
  454. public function getShouldShuffle(): bool
  455. {
  456. return $this->shouldShuffle;
  457. }
  458. /**
  459. * @param bool $shouldShuffle
  460. */
  461. public function setShouldShuffle(bool $shouldShuffle): void
  462. {
  463. $this->shouldShuffle = $shouldShuffle;
  464. }
  465. /**
  466. * @return bool
  467. */
  468. public function getIgnoreCase(): bool
  469. {
  470. return $this->ignoreCase;
  471. }
  472. /**
  473. * @param bool $ignoreCase
  474. */
  475. public function setIgnoreCase(bool $ignoreCase): void
  476. {
  477. $this->ignoreCase = $ignoreCase;
  478. }
  479. /**
  480. *
  481. */
  482. public function setMathproblemsCount()
  483. {
  484. $count = 0;
  485. /** @var Mathproblem $mathproblem */
  486. foreach ($this->mathproblems as $mathproblem) {
  487. if ($mathproblem->isPublished()) {
  488. $count++;
  489. }
  490. }
  491. $this->mathproblemsCount = $count;
  492. }
  493. public function areAllProblemsPublished(): bool
  494. {
  495. foreach ($this->mathproblems as $mathproblem) {
  496. if (!$mathproblem->isPublished() && !$mathproblem->isDeleted()) {
  497. return false;
  498. }
  499. }
  500. return true;
  501. }
  502. /**
  503. * @return bool
  504. */
  505. public function getMathproblemsCount()
  506. {
  507. return $this->mathproblemsCount;
  508. }
  509. public function isAnswerPositionVertical(): bool
  510. {
  511. return $this->answerPositionVertical;
  512. }
  513. public function setAnswerPositionVertical(bool $answerPositionVertical): void
  514. {
  515. $this->answerPositionVertical = $answerPositionVertical;
  516. }
  517. public function __toString(): string
  518. {
  519. return $this->id;
  520. }
  521. public function getDndType(): ?string
  522. {
  523. return $this->dndType ?? null;
  524. }
  525. public function setDndType(?string $dndType): void
  526. {
  527. $this->dndType = $dndType;
  528. }
  529. public function getProblemTypeAsString(): string {
  530. return ProblemType::getProblemTypeFromTemplate($this)->name();
  531. }
  532. public function getProblemType(): ProblemType
  533. {
  534. return ProblemType::getProblemTypeFromTemplate($this);
  535. }
  536. public function getDeletedAt(): ?DateTime
  537. {
  538. return $this->deletedAt;
  539. }
  540. public function setDeletedAt(?DateTime $deletedAt): void
  541. {
  542. $this->deletedAt = $deletedAt;
  543. }
  544. function jsonSerialize(): mixed
  545. {
  546. return [
  547. "id" => $this->getId(),
  548. "type" => $this->getType(),
  549. "topic" => $this->getTopic(),
  550. "mpTopic" => $this->getMpTopic(),
  551. "difficulty" => $this->getDifficulty(),
  552. "helpAllowed" => $this->isHelpAllowed(),
  553. "tags" => $this->getTags()->toArray(),
  554. "duration" => $this->getDuration(),
  555. "shouldShuffle" => $this->getShouldShuffle(),
  556. "useCompactVersion" => $this->getUseCompactVersion(),
  557. "answerPositionVertical" => $this->isAnswerPositionVertical(),
  558. "templateSets" => $this->getTemplateSets()->toArray(),
  559. "mathproblemCount" => $this->getMathproblemsCount(),
  560. "creator" => $this->getCreator(),
  561. "ignoreCase" => $this->getIgnoreCase(),
  562. "target" => $this->getTarget(),
  563. ];
  564. }
  565. }