src/Entity/Core/Homework/HomeworkUnit.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Core\Homework;
  3. use App\Entity\Core\Topic\TopicCounterInterface;
  4. use App\Entity\Core\Topic\TopicCounterTrait;
  5. use App\Entity\User\User;
  6. use App\Session\Core\SessionUnitInterface;
  7. use DateTime;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use JsonSerializable;
  11. #[ORM\Table('homework_unit')]
  12. #[ORM\Entity(repositoryClass: HomeworkUnitRepository::class)]
  13. class HomeworkUnit implements TopicCounterInterface, JsonSerializable, SessionUnitInterface
  14. {
  15. use TopicCounterTrait;
  16. public function getParent(): Homework
  17. {
  18. return $this->getHomework();
  19. }
  20. /**
  21. * @var int
  22. */
  23. #[ORM\Column(name: 'id', type: 'integer')]
  24. #[ORM\Id]
  25. #[ORM\GeneratedValue(strategy: 'AUTO')]
  26. private $id;
  27. /**
  28. * @var int
  29. */
  30. #[ORM\Column(name: 'current_difficulty', type: 'integer', options: ['default' => 3])]
  31. private $currentDifficulty;
  32. /**
  33. * @var int
  34. */
  35. #[ORM\Column(name: 'prev_answer_correct', nullable: true, type: 'boolean', options: ['default' => false])]
  36. private $prevAnswerCorrect;
  37. /**
  38. * @var int
  39. */
  40. #[ORM\Column(name: 'prev_difficulty', type: 'integer', nullable: true, options: ['default' => 1])]
  41. private $prevDifficulty;
  42. /**
  43. * @var Homework
  44. */
  45. #[ORM\JoinColumn(name: 'homework', referencedColumnName: 'id', onDelete: 'CASCADE')]
  46. #[ORM\ManyToOne(targetEntity: Homework::class, inversedBy: 'homeworkUnits')]
  47. private $homework;
  48. /**
  49. * @var User
  50. */
  51. #[ORM\ManyToOne(targetEntity: User::class, cascade: ['persist', 'remove'])]
  52. private $student;
  53. /**
  54. * @var HomeworkResponse[]
  55. */
  56. #[ORM\OneToMany(targetEntity: HomeworkResponse::class, mappedBy: 'homeworkUnit', cascade: ['persist', 'remove'])]
  57. private $homeworkResponses;
  58. /**
  59. * @var DateTime
  60. */
  61. #[ORM\Column(name: 'startTime', type: 'datetime', nullable: true)]
  62. private $startTime;
  63. /**
  64. * @var DateTime
  65. */
  66. #[ORM\Column(name: 'deadline', type: 'datetime', nullable: true)]
  67. private $deadline;
  68. /**
  69. * @var int
  70. */
  71. #[ORM\Column(name: 'seconds_left', type: 'integer', nullable: true, options: ['default' => 0])]
  72. private $secondsLeft;
  73. /**
  74. * @var int
  75. */
  76. #[ORM\Column(name: 'seconds_paused', type: 'integer', nullable: false, options: ['default' => 0])]
  77. private $secondsPaused;
  78. /**
  79. * @var DateTime
  80. */
  81. #[ORM\Column(name: 'time_paused', type: 'datetime', nullable: true)]
  82. private $timePaused;
  83. /**
  84. * @var DateTime
  85. */
  86. #[ORM\Column(name: 'time_resumed', type: 'datetime', nullable: true)]
  87. private $timeResumed;
  88. /**
  89. * @var DateTime
  90. */
  91. #[ORM\Column(name: 'extended_deadline', type: 'datetime', nullable: true)]
  92. private $extendedDeadline;
  93. public function __construct()
  94. {
  95. $this->homeworkResponses = new ArrayCollection();
  96. }
  97. /**
  98. * @return int
  99. */
  100. public function getId()
  101. {
  102. return $this->id;
  103. }
  104. /**
  105. * @param int $id
  106. */
  107. public function setId($id)
  108. {
  109. $this->id = $id;
  110. }
  111. /**
  112. * @return int
  113. */
  114. public function getCurrentDifficulty()
  115. {
  116. return $this->currentDifficulty;
  117. }
  118. /**
  119. * @param int $currentDifficulty
  120. */
  121. public function setCurrentDifficulty($currentDifficulty)
  122. {
  123. $this->currentDifficulty = $currentDifficulty;
  124. }
  125. /**
  126. * @return int
  127. */
  128. public function getPrevAnswerCorrect()
  129. {
  130. return $this->prevAnswerCorrect;
  131. }
  132. /**
  133. * @param int $prevAnswerCorrect
  134. */
  135. public function setPrevAnswerCorrect($prevAnswerCorrect)
  136. {
  137. $this->prevAnswerCorrect = $prevAnswerCorrect;
  138. }
  139. /**
  140. * @return int
  141. */
  142. public function getPrevDifficulty()
  143. {
  144. return $this->prevDifficulty;
  145. }
  146. /**
  147. * @param int $prevDifficulty
  148. */
  149. public function setPrevDifficulty($prevDifficulty)
  150. {
  151. $this->prevDifficulty = $prevDifficulty;
  152. }
  153. /**
  154. * @return Homework
  155. */
  156. public function getHomework()
  157. {
  158. return $this->homework;
  159. }
  160. /**
  161. * @param Homework $homework
  162. */
  163. public function setHomework($homework)
  164. {
  165. $this->homework = $homework;
  166. }
  167. public function getSession(): ?Homework
  168. {
  169. return $this->homework;
  170. }
  171. /**
  172. * @return User
  173. */
  174. public function getStudent()
  175. {
  176. return $this->student;
  177. }
  178. /**
  179. * @param User $student
  180. */
  181. public function setStudent($student)
  182. {
  183. $this->student = $student;
  184. }
  185. public function getStartTime(): ?DateTime
  186. {
  187. return $this->startTime;
  188. }
  189. /**
  190. * @param DateTime $startTime
  191. */
  192. public function setStartTime(?DateTime $startTime)
  193. {
  194. $this->startTime = $startTime;
  195. }
  196. /**
  197. * @return HomeworkResponse[]
  198. */
  199. public function getHomeworkResponses()
  200. {
  201. return $this->homeworkResponses;
  202. }
  203. /**
  204. * @return array
  205. */
  206. public function getValidHomeworkResponses()
  207. {
  208. $homeworkResponses = [];
  209. foreach ($this->homeworkResponses as $response) {
  210. if ($response->isValid()) {
  211. $homeworkResponses[] = $response;
  212. }
  213. }
  214. return $homeworkResponses;
  215. }
  216. /**
  217. * @param HomeworkResponse[] $homeworkResponses
  218. */
  219. public function setHomeworkResponses($homeworkResponses)
  220. {
  221. $this->homeworkResponses = $homeworkResponses;
  222. }
  223. public function getDeadline(): DateTime
  224. {
  225. return $this->deadline;
  226. }
  227. /**
  228. * @param DateTime $deadline
  229. * @return $this
  230. */
  231. public function setDeadline($deadline)
  232. {
  233. $this->deadline = $deadline;
  234. return $this;
  235. }
  236. public function isPaused(): bool
  237. {
  238. return $this->timePaused > $this->timeResumed;
  239. }
  240. public function getSecondsLeft(): int
  241. {
  242. return $this->secondsLeft;
  243. }
  244. public function setSecondsLeft(?int $secondsLeft)
  245. {
  246. $this->secondsLeft = $secondsLeft;
  247. }
  248. public function getSecondsPaused(): int
  249. {
  250. return $this->secondsPaused;
  251. }
  252. public function setSecondsPaused(int $secondsPaused)
  253. {
  254. $this->secondsPaused = $secondsPaused;
  255. }
  256. public function addSecondsPaused(int $secondsPaused)
  257. {
  258. $this->secondsPaused += $secondsPaused;
  259. }
  260. public function getTimePaused(): ?DateTime
  261. {
  262. return $this->timePaused;
  263. }
  264. public function setTimePaused(?DateTime $timePaused)
  265. {
  266. $this->timePaused = $timePaused;
  267. }
  268. public function getTimeResumed(): ?DateTime
  269. {
  270. return $this->timeResumed;
  271. }
  272. public function setTimeResumed(?DateTime $timeResumed)
  273. {
  274. $this->timeResumed = $timeResumed;
  275. }
  276. public function getExtendedDeadline(): ?\DateTime
  277. {
  278. return $this->extendedDeadline;
  279. }
  280. public function setExtendedDeadline(DateTime $extendedDeadline): void
  281. {
  282. $this->extendedDeadline = $extendedDeadline;
  283. }
  284. public function jsonSerialize(): mixed
  285. {
  286. return [
  287. 'id' => $this->id,
  288. 'startTime' => $this->startTime,
  289. 'deadline' => $this->deadline,
  290. 'student' => $this->student,
  291. 'currentDifficulty' => $this->currentDifficulty,
  292. 'extendedDeadline' => $this->extendedDeadline,
  293. 'prevAnswerCorrect' => $this->prevAnswerCorrect,
  294. 'prevDifficulty' => $this->prevDifficulty,
  295. 'secondsLeft' => $this->secondsLeft,
  296. 'secondsPaused' => $this->secondsPaused,
  297. 'timePaused' => $this->timePaused,
  298. 'timeResumed' => $this->timeResumed,
  299. 'currentTopicNr' => $this->currentTopicNr
  300. ];
  301. }
  302. }