src/Entity/Core/ReadingTest/ReadingTest.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Core\ReadingTest;
  3. use App\Entity\Core\Classroom\ClassroomType;
  4. use App\Entity\Core\Mathproblem;
  5. use App\Services\Core\ActivityTypeMappingService;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use JsonSerializable;
  10. #[ORM\Table('reading_test')]
  11. #[ORM\Entity(repositoryClass: ReadingTestRepository::class)]
  12. class ReadingTest implements JsonSerializable
  13. {
  14. private const int DEFAULT_DURATION = 15;
  15. /**
  16. * @var int
  17. */
  18. #[ORM\Column(name: 'id', type: 'integer')]
  19. #[ORM\Id]
  20. #[ORM\GeneratedValue(strategy: 'AUTO')]
  21. private $id;
  22. /**
  23. * @var string
  24. */
  25. #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: true)]
  26. private $title;
  27. /**
  28. * @var string
  29. */
  30. #[ORM\Column(name: 'text', type: 'text', length: 65000, nullable: false)]
  31. private $text;
  32. /**
  33. * @var ClassroomType
  34. */
  35. #[ORM\JoinColumn(name: 'classroom_type', referencedColumnName: 'id')]
  36. #[ORM\ManyToOne(targetEntity: ClassroomType::class)]
  37. private $classroomType;
  38. #[ORM\JoinTable(name: 'reading_test_mathproblem')]
  39. #[ORM\JoinColumn(name: 'reading_test_id')]
  40. #[ORM\InverseJoinColumn(name: 'mathproblem_id')]
  41. #[ORM\ManyToMany(targetEntity: Mathproblem::class)]
  42. private Collection $mathproblems;
  43. /**
  44. * @var array
  45. */
  46. #[ORM\Column(name: 'problem_order', type: 'simple_array', nullable: true)]
  47. private $problemOrder;
  48. /**
  49. * @var array
  50. */
  51. #[ORM\Column(name: 'color_wrong_count_threshold', type: 'simple_array', nullable: true)]
  52. private $colorWrongCountThreshold;
  53. /**
  54. * @var array
  55. */
  56. #[ORM\Column(name: 'color_wrong_word_per_minute_threshold', type: 'simple_array', nullable: true)]
  57. private $colorWrongWordPerMinuteThreshold;
  58. public function __construct()
  59. {
  60. $this->mathproblems = new ArrayCollection();
  61. }
  62. public function getId(): int
  63. {
  64. return $this->id;
  65. }
  66. public function setId(int $id): void
  67. {
  68. $this->id = $id;
  69. }
  70. public function getTitle(): string
  71. {
  72. return $this->title;
  73. }
  74. public function setTitle(string $title): void
  75. {
  76. $this->title = $title;
  77. }
  78. public function getText(): string
  79. {
  80. return $this->text;
  81. }
  82. public function setText(string $text): void
  83. {
  84. $this->text = $text;
  85. }
  86. /**
  87. * @return ClassroomType
  88. */
  89. public function getClassroomType()
  90. {
  91. return $this->classroomType;
  92. }
  93. public function setClassroomType(ClassroomType $classroomType): void
  94. {
  95. $this->classroomType = $classroomType;
  96. }
  97. /**
  98. * @return ArrayCollection|Mathproblem[]
  99. */
  100. public function getMathproblems(): Collection
  101. {
  102. return $this->mathproblems;
  103. }
  104. /**
  105. * @param Mathproblem[]
  106. */
  107. public function setMathproblems(array $mathproblems): self
  108. {
  109. $this->mathproblems = new ArrayCollection($mathproblems);
  110. return $this;
  111. }
  112. public function getProblemOrder(): ?array
  113. {
  114. return $this->problemOrder;
  115. }
  116. public function setProblemOrder(array $problemOrder)
  117. {
  118. $this->problemOrder = $problemOrder;
  119. }
  120. public function getColorWrongCountThreshold(): ?array
  121. {
  122. return $this->colorWrongCountThreshold;
  123. }
  124. /**
  125. * @return $this
  126. */
  127. public function setColorWrongCountThreshold(?array $colorWrongCountThreshold): self
  128. {
  129. $this->colorWrongCountThreshold = $colorWrongCountThreshold;
  130. return $this;
  131. }
  132. public function getColorWrongWordPerMinuteThreshold(): ?array
  133. {
  134. return $this->colorWrongWordPerMinuteThreshold;
  135. }
  136. /**
  137. * @return $this
  138. */
  139. public function setColorWrongWordPerMinuteThreshold(?array $colorWrongWordPerMinuteThreshold): self
  140. {
  141. $this->colorWrongWordPerMinuteThreshold = $colorWrongWordPerMinuteThreshold;
  142. return $this;
  143. }
  144. /**
  145. * @return array
  146. */
  147. public function getSortedMathproblems()
  148. {
  149. $problemOrder = $this->problemOrder;
  150. $mathproblems = $this->mathproblems;
  151. if ($problemOrder && count($problemOrder) == count($mathproblems)) {
  152. $newmathproblems = [];
  153. foreach ($mathproblems as $mp) {
  154. $newkey = array_search($mp->getId(), $problemOrder);
  155. $newmathproblems[$newkey] = $mp;
  156. }
  157. ksort($newmathproblems);
  158. return $newmathproblems;
  159. } else {
  160. return $this->mathproblems->toArray();
  161. }
  162. }
  163. public function toLightweightArray(ActivityTypeMappingService $activityTypeMappingService): array
  164. {
  165. return [
  166. 'id' => $this->getId(),
  167. 'type' => $activityTypeMappingService->getActivityTypeFromClass(ReadingTest::class),
  168. 'name' => $this->getTitle(),
  169. 'problemCount' => 1,
  170. 'classroomType' => $this->getClassroomType()?->getId(),
  171. ];
  172. }
  173. public function jsonSerialize(): array
  174. {
  175. return [
  176. 'id' => $this->getId(),
  177. 'title' => $this->getTitle(),
  178. 'classroomType' => $this->getClassroomType()->getId(),
  179. 'problemCount' => 1,
  180. 'duration' => self::DEFAULT_DURATION, // Add this to have a fallback for screening upon request from Sofie, well knowing that the activity does not come with a duration as is. Perhaps it should be added at some point
  181. ];
  182. }
  183. }