src/Entity/Core/TemplateMathproblem.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Core;
  4. use App\Controller\Admin\EasyAdmin\ProblemType;
  5. use App\Entity\Core\Classroom\ClassroomType;
  6. use App\Entity\Core\Topic\Topic;
  7. use App\Entity\User\User;
  8. use DateTime;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Doctrine\ORM\PersistentCollection;
  13. use JsonSerializable;
  14. #[ORM\Table('template_mathproblem')]
  15. #[ORM\Index(name: 'difficulty_idx', columns: ['difficulty'])]
  16. #[ORM\Index(name: 'type_idx', columns: ['type'])]
  17. #[ORM\Entity(repositoryClass: TemplateMathproblemRepository::class)]
  18. class TemplateMathproblem implements JsonSerializable
  19. {
  20. // types used in database
  21. public const int TYPE_RADIO_BUTTON = 0;
  22. public const int TYPE_NUMBER = 1;
  23. public const int TYPE_DRAG_AND_DROP = 4;
  24. public const int TYPE_FORMULA = 5;
  25. public const int TYPE_DROPDOWN = 6;
  26. public const int TYPE_WORD_OPTIONS = 7;
  27. public const int TYPE_TEXT = 8;
  28. public const int TYPE_PARAGRAPH = 9;
  29. public const int TYPE_MARK = 10;
  30. public const int TEMPLATE_MATH_PROBLEM_TABLE = 11;
  31. public const int TEMPLATE_MATH_PROBLEM_GRID = 12;
  32. public const array TEMPLATE_MATH_PROBLEM_TYPES = [
  33. self::TYPE_RADIO_BUTTON,
  34. self::TYPE_NUMBER,
  35. self::TYPE_DRAG_AND_DROP,
  36. self::TYPE_FORMULA,
  37. self::TYPE_DROPDOWN,
  38. self::TYPE_WORD_OPTIONS,
  39. self::TYPE_TEXT,
  40. self::TYPE_PARAGRAPH,
  41. self::TYPE_MARK
  42. ];
  43. #[ORM\Column(name: 'id', type: 'integer')]
  44. #[ORM\Id]
  45. #[ORM\GeneratedValue(strategy: 'AUTO')]
  46. private ?int $id = null; // RSM: This seems to work for both Doctrine and EasyAdmin, might have to remove the typehint if more issues pop up though.
  47. #[ORM\ManyToOne(targetEntity: User::class)]
  48. private ?User $creator = null;
  49. #[ORM\Column(name: 'is_help_allowed', type: 'boolean')]
  50. private bool $helpAllowed;
  51. #[ORM\Column(name: 'difficulty', type: 'smallint')]
  52. private int $difficulty;
  53. /**
  54. * Specifies the type of problem:<br>
  55. * 0 = single choice, multiple available answers<br>
  56. * 1 = single choice, text answer<br>
  57. * 4 = Drag and Drop
  58. */
  59. #[ORM\Column(name: 'type', type: 'smallint')]
  60. private int $type;
  61. #[ORM\Column(name: 'is_unique', type: 'boolean', unique: false)]
  62. private bool $unique;
  63. #[ORM\Column(name: 'has_priority', type: 'boolean', unique: false)]
  64. private bool $priority;
  65. #[ORM\Column(name: 'is_valid', type: 'boolean', unique: false, options: ['default' => true])]
  66. private bool $valid;
  67. /**
  68. * The mathproblems that are created using this template
  69. *
  70. * @var Collection|ArrayCollection|Mathproblem[]
  71. */
  72. #[ORM\OneToMany(targetEntity: Mathproblem::class, mappedBy: 'template', cascade: ['persist', 'remove'])]
  73. private Collection|array $mathproblems;
  74. #[ORM\JoinColumn(name: 'topic_id', referencedColumnName: 'id')]
  75. #[ORM\ManyToOne(targetEntity: Topic::class, inversedBy: 'templates')]
  76. private ?Topic $topic = null;
  77. /**
  78. * Target can be all - 0, session - 1, homework - 2, single session - 3, screening - 4
  79. */
  80. #[ORM\Column(name: 'target', type: 'smallint', nullable: false)]
  81. private int $target;
  82. /**
  83. * @var ArrayCollection|TagMathproblem[]
  84. */
  85. #[ORM\JoinTable(name: 'relation_template_tag')]
  86. #[ORM\JoinColumn(name: 'mpt_tag_id', referencedColumnName: 'id')]
  87. #[ORM\ManyToMany(targetEntity: TagMathproblem::class, inversedBy: 'templates')]
  88. private array|Collection $tags;
  89. #[ORM\Column(name: 'mptopic', type: 'string', nullable: true)]
  90. private ?string $mptopic = null;
  91. /**
  92. * Mathproblem solving time in minutes
  93. */
  94. #[ORM\Column(name: 'duration', type: 'smallint')]
  95. private int $duration;
  96. /**
  97. * @var ArrayCollection|TemplateSet[]
  98. *
  99. */
  100. #[ORM\ManyToMany(targetEntity: TemplateSet::class, mappedBy: 'templates')]
  101. private array|Collection|PersistentCollection $templateSets;
  102. /**
  103. * @var Collection|ArrayCollection|ClassroomType[]
  104. */
  105. #[ORM\ManyToMany(targetEntity: ClassroomType::class)]
  106. #[ORM\JoinTable(name: 'template_excluded_classroomtypes')]
  107. #[ORM\JoinColumn(name: 'template_id', referencedColumnName: 'id')]
  108. #[ORM\InverseJoinColumn(name: 'classroomtype_id', referencedColumnName: 'id')]
  109. private Collection $excludedClassroomTypes;
  110. #[ORM\Column(name: 'use_for_worksheet', type: 'boolean', options: ['default' => true])]
  111. private bool $useForWorksheet = true;
  112. #[ORM\Column(name: 'use_compact_version', type: 'boolean', options: ['default' => false])]
  113. private bool $useCompactVersion = false;
  114. #[ORM\Column(name: 'should_shuffle', type: 'boolean', options: ['default' => true])]
  115. private bool $shouldShuffle = true;
  116. #[ORM\Column(name: 'ignore_case', type: 'boolean', options: ['default' => false])]
  117. private bool $ignoreCase = false;
  118. /**
  119. * Flag used to count number of valid template mathproblems
  120. */
  121. private int $mathproblemsCount = 0;
  122. #[ORM\Column(name: 'answer_position_vertical', type: 'boolean', options: ['default' => false])]
  123. private bool $answerPositionVertical;
  124. /**
  125. * For the moment type is 4 for both match, block and sort problems. This field is used to differentiate.
  126. */
  127. #[ORM\Column(name: 'dnd_type', type: 'string', nullable: true)]
  128. private ?string $dndType;
  129. #[ORM\Column(name: 'deleted_at', type: 'datetime', nullable: true)]
  130. private ?DateTime $deletedAt;
  131. /**
  132. * TemplateMathproblem constructor.
  133. */
  134. public function __construct()
  135. {
  136. $this->mathproblems = new ArrayCollection();
  137. $this->tags = new ArrayCollection();
  138. $this->templateSets = new ArrayCollection();
  139. $this->excludedClassroomTypes = new ArrayCollection();
  140. }
  141. public function getId(): ?int
  142. {
  143. return $this->id;
  144. }
  145. /**
  146. * @return Collection|ArrayCollection|Mathproblem[]
  147. */
  148. public function getMathproblems(): Collection
  149. {
  150. return $this->mathproblems;
  151. }
  152. public function addMathproblem(Mathproblem $mathproblem): TemplateMathproblem
  153. {
  154. $this->mathproblems->add($mathproblem);
  155. return $this;
  156. }
  157. public function removeMathproblem(Mathproblem $mathproblem): bool
  158. {
  159. return $this->mathproblems->removeElement($mathproblem);
  160. }
  161. public function getTopic(): ?Topic
  162. {
  163. return $this->topic;
  164. }
  165. public function setTopic(?Topic $topic): void
  166. {
  167. $this->topic = $topic;
  168. }
  169. public function getDifficulty(): int
  170. {
  171. return $this->difficulty;
  172. }
  173. public function setDifficulty(int $difficulty) : void
  174. {
  175. $this->difficulty = $difficulty;
  176. }
  177. public function getType(): int
  178. {
  179. return $this->type;
  180. }
  181. public function setType(int $type): void
  182. {
  183. $this->type = $type;
  184. }
  185. public function getTags(): Collection
  186. {
  187. return $this->tags;
  188. }
  189. public function addTag(TagMathproblem $tag): void
  190. {
  191. $this->tags->add($tag);
  192. }
  193. /**
  194. * @return Collection|ArrayCollection|ClassroomType[]
  195. */
  196. public function getExcludedClassroomTypes(): Collection
  197. {
  198. return $this->excludedClassroomTypes;
  199. }
  200. public function addExcludedClassroomType(ClassroomType $classroomType): self
  201. {
  202. if (!$this->excludedClassroomTypes->contains($classroomType)) {
  203. $this->excludedClassroomTypes->add($classroomType);
  204. }
  205. return $this;
  206. }
  207. public function removeExcludedClassroomType(ClassroomType $classroomType): bool
  208. {
  209. return $this->excludedClassroomTypes->removeElement($classroomType);
  210. }
  211. public function getValidTagArray(): array {
  212. $tagList = [];
  213. foreach ($this->tags as $tag) {
  214. if ($tag->isEnabled()) {
  215. $tagList[] = $tag->getName();
  216. }
  217. }
  218. return $tagList;
  219. }
  220. public function getTagList(): string {
  221. $tagArray = $this->getValidTagArray();
  222. if (empty($tagArray)) {
  223. return '-';
  224. }
  225. return implode(', ', $tagArray);
  226. }
  227. public function getMpTopic(): ?string
  228. {
  229. return $this->mptopic;
  230. }
  231. public function setMpTopic(?string $mpTopic): void
  232. {
  233. $this->mptopic = $mpTopic;
  234. }
  235. public function isHelpAllowed(): bool
  236. {
  237. return $this->helpAllowed;
  238. }
  239. public function setHelpAllowed(bool $isAllowed): void
  240. {
  241. $this->helpAllowed = $isAllowed;
  242. }
  243. public function getTarget(): int
  244. {
  245. return $this->target;
  246. }
  247. public function setTarget(int $target): void
  248. {
  249. $this->target = $target;
  250. }
  251. public function isUnique(): bool
  252. {
  253. return $this->unique;
  254. }
  255. public function setUnique(bool $isUnique): void
  256. {
  257. $this->unique = $isUnique;
  258. }
  259. public function isValid(): bool
  260. {
  261. return $this->valid;
  262. }
  263. public function setValid(bool $valid): void
  264. {
  265. $this->valid = $valid;
  266. }
  267. public function getDuration(): int
  268. {
  269. return $this->duration;
  270. }
  271. public function setDuration(int $duration): TemplateMathproblem
  272. {
  273. $this->duration = $duration;
  274. return $this;
  275. }
  276. public function getCreator(): ?User
  277. {
  278. return $this->creator;
  279. }
  280. public function setCreator(?User $creator): void
  281. {
  282. $this->creator = $creator;
  283. }
  284. public function isPriority(): bool
  285. {
  286. return $this->priority;
  287. }
  288. public function setPriority(bool $priority): void
  289. {
  290. $this->priority = $priority;
  291. }
  292. public function getTemplateSets(): array|ArrayCollection|PersistentCollection
  293. {
  294. return $this->templateSets;
  295. }
  296. public function addTemplateSet(TemplateSet $templateSet): void
  297. {
  298. if (!$this->templateSets->contains($templateSet)) {
  299. $this->templateSets->add($templateSet);
  300. $templateSet->addTemplate($this);
  301. }
  302. }
  303. /**
  304. * @param TemplateSet[]|ArrayCollection $templateSets
  305. */
  306. public function setTemplateSets(array $templateSets): void
  307. {
  308. $this->templateSets = $templateSets;
  309. }
  310. public function isUseForWorksheet(): bool
  311. {
  312. return $this->useForWorksheet;
  313. }
  314. public function setUseForWorksheet(bool $useForWorksheet): void
  315. {
  316. $this->useForWorksheet = $useForWorksheet;
  317. }
  318. public function getUseCompactVersion(): bool
  319. {
  320. return $this->useCompactVersion;
  321. }
  322. public function setUseCompactVersion(bool $useCompactVersion): void
  323. {
  324. $this->useCompactVersion = $useCompactVersion;
  325. }
  326. public function getShouldShuffle(): bool
  327. {
  328. return $this->shouldShuffle;
  329. }
  330. public function setShouldShuffle(bool $shouldShuffle): void
  331. {
  332. $this->shouldShuffle = $shouldShuffle;
  333. }
  334. public function getIgnoreCase(): bool
  335. {
  336. return $this->ignoreCase;
  337. }
  338. public function setIgnoreCase(bool $ignoreCase): void
  339. {
  340. $this->ignoreCase = $ignoreCase;
  341. }
  342. /**
  343. *
  344. */
  345. public function setMathproblemsCount()
  346. {
  347. $count = 0;
  348. /** @var Mathproblem $mathproblem */
  349. foreach ($this->mathproblems as $mathproblem) {
  350. if ($mathproblem->isPublished()) {
  351. $count++;
  352. }
  353. }
  354. $this->mathproblemsCount = $count;
  355. }
  356. public function areAllProblemsPublished(): bool
  357. {
  358. foreach ($this->mathproblems as $mathproblem) {
  359. if (!$mathproblem->isPublished() && !$mathproblem->isDeleted()) {
  360. return false;
  361. }
  362. }
  363. return true;
  364. }
  365. public function getMathproblemsCount(): int
  366. {
  367. return $this->mathproblemsCount;
  368. }
  369. public function isAnswerPositionVertical(): bool
  370. {
  371. return $this->answerPositionVertical;
  372. }
  373. public function setAnswerPositionVertical(bool $answerPositionVertical): void
  374. {
  375. $this->answerPositionVertical = $answerPositionVertical;
  376. }
  377. public function __toString(): string
  378. {
  379. return (string)$this->id;
  380. }
  381. public function getDndType(): ?string
  382. {
  383. return $this->dndType ?? null;
  384. }
  385. public function setDndType(?string $dndType): void
  386. {
  387. $this->dndType = $dndType;
  388. }
  389. public function getProblemTypeAsString(): string {
  390. return ProblemType::getProblemTypeFromTemplate($this)->name();
  391. }
  392. public function getProblemType(): ProblemType
  393. {
  394. return ProblemType::getProblemTypeFromTemplate($this);
  395. }
  396. public function getDeletedAt(): ?DateTime
  397. {
  398. return $this->deletedAt;
  399. }
  400. public function setDeletedAt(?DateTime $deletedAt): void
  401. {
  402. $this->deletedAt = $deletedAt;
  403. }
  404. function jsonSerialize(): mixed
  405. {
  406. return [
  407. "id" => $this->getId(),
  408. "type" => $this->getType(),
  409. "topic" => $this->getTopic(),
  410. "mpTopic" => $this->getMpTopic(),
  411. "difficulty" => $this->getDifficulty(),
  412. "helpAllowed" => $this->isHelpAllowed(),
  413. "tags" => $this->getTags()->toArray(),
  414. "duration" => $this->getDuration(),
  415. "shouldShuffle" => $this->getShouldShuffle(),
  416. "useCompactVersion" => $this->getUseCompactVersion(),
  417. "answerPositionVertical" => $this->isAnswerPositionVertical(),
  418. "templateSets" => $this->getTemplateSets()->toArray(),
  419. "mathproblemCount" => $this->getMathproblemsCount(),
  420. "creator" => $this->getCreator(),
  421. "ignoreCase" => $this->getIgnoreCase(),
  422. "target" => $this->getTarget(),
  423. ];
  424. }
  425. }