src/CoreBundle/Entity/HomeworkUnit.php line 16

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