src/CoreBundle/Entity/Quiz.php line 17

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CoreBundle\Entity;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\HttpFoundation\Exception\BadRequestException;
  7. use UserBundle\Entity\User;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. /**
  10. * @ORM\Table("quiz")
  11. * @ORM\Entity(repositoryClass="CoreBundle\Entity\QuizRepository")
  12. */
  13. class Quiz implements HasMediaInterface
  14. {
  15. use HasMediaTraits;
  16. /**
  17. * @var int
  18. *
  19. * @ORM\Column(name="id", type="integer")
  20. * @ORM\Id
  21. * @ORM\GeneratedValue(strategy="AUTO")
  22. */
  23. private $id;
  24. /**
  25. * @var int
  26. *
  27. * @ORM\Column(type="integer", nullable=false, options={"default":0})
  28. */
  29. private $duration;
  30. /**
  31. * @var string
  32. *
  33. * @ORM\Column(name="name", type="string", length=255, nullable=true)
  34. */
  35. private $name;
  36. /**
  37. * @var string
  38. *
  39. * @ORM\Column(name="description", type="string", length=500, nullable=true)
  40. */
  41. private $description;
  42. /**
  43. * @var bool
  44. *
  45. * @ORM\Column(name="enabled", type="boolean", options={"default":1})
  46. */
  47. private $enabled;
  48. /**
  49. * @var Publication
  50. *
  51. * @ORM\ManyToOne(targetEntity="Publication", inversedBy="quizzes")
  52. * @ORM\JoinColumn(name="publication_id", referencedColumnName="id")
  53. */
  54. private $publication;
  55. /**
  56. * @ORM\OneToMany(targetEntity="RelationQuizMathproblem", mappedBy="quiz", cascade={"persist"})
  57. */
  58. private Collection $mathproblems;
  59. /**
  60. * @var array
  61. *
  62. * @ORM\Column(name="problem_order", type="simple_array", nullable=true)
  63. */
  64. private $problemOrder;
  65. /**
  66. * @var array
  67. *
  68. * @ORM\Column(name="mathproblems_for_templates", type="simple_array", nullable=true)
  69. */
  70. private $mathproblemsForTemplates;
  71. /**
  72. * @var bool
  73. *
  74. * @ORM\Column(name="reusable", type="boolean", nullable=false)
  75. */
  76. private $reusable;
  77. /**
  78. * @var bool
  79. *
  80. * @ORM\Column(name="abacus", type="boolean", nullable=true, options={"default":0})
  81. */
  82. private $abacus;
  83. /**
  84. * @var bool
  85. *
  86. * @ORM\Column(name="target_student", type="boolean", nullable=true, options={"default":0})
  87. */
  88. private $targetStudent;
  89. /**
  90. * @var Topic
  91. *
  92. * @ORM\ManyToOne(targetEntity="CoreBundle\Entity\Topic")
  93. * @ORM\JoinColumn(name="topic", referencedColumnName="id")
  94. */
  95. private $topic;
  96. /**
  97. * @var \DateTime
  98. *
  99. * @ORM\Column(name="createdAt", type="datetime", nullable=false)
  100. */
  101. private $createdAt;
  102. /**
  103. * @var User
  104. *
  105. * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User")
  106. * @ORM\JoinColumn(name="created_by", referencedColumnName="id")
  107. */
  108. private $createdBy;
  109. /**
  110. * @var User[]
  111. *
  112. * @ORM\ManyToMany(targetEntity="UserBundle\Entity\User")
  113. * @ORM\JoinTable(name="quiz_teacher",
  114. * joinColumns={@ORM\JoinColumn(name="quiz_id", referencedColumnName="id")},
  115. * inverseJoinColumns={@ORM\JoinColumn(name="teacher_id", referencedColumnName="id")})
  116. */
  117. private $teachers;
  118. /**
  119. * @var QuizSession[]
  120. *
  121. * @ORM\OneToMany(targetEntity="CoreBundle\Entity\QuizSession", mappedBy="quiz", cascade={"persist"})
  122. */
  123. private $quizSessions;
  124. /**
  125. * @var ClassroomType
  126. *
  127. * @ORM\ManyToOne(targetEntity="CoreBundle\Entity\ClassroomType")
  128. * @ORM\JoinColumn(name="level", referencedColumnName="id", nullable=true)
  129. */
  130. private $level;
  131. /**
  132. * @var QuizType
  133. *
  134. * @ORM\ManyToOne(targetEntity="CoreBundle\Entity\QuizType")
  135. * @ORM\JoinColumn(name="type", referencedColumnName="id", nullable=true)
  136. */
  137. private $type;
  138. /**
  139. * @var array
  140. *
  141. * @ORM\Column(name="color_wrong_count_threshold", type="simple_array", nullable=true)
  142. */
  143. private $colorWrongCountThreshold;
  144. /**
  145. * @var array
  146. *
  147. * @ORM\Column(name="color_percentage_threshold", type="simple_array", nullable=true)
  148. */
  149. private $colorPercentageThreshold;
  150. /**
  151. * @var string
  152. *
  153. * @ORM\Column(name="ibook_id", type="string", length=255, nullable=true)
  154. */
  155. private ?string $iBookId;
  156. /**
  157. * @ORM\OneToOne(targetEntity="VideoReference", inversedBy="quiz")
  158. * @ORM\JoinColumn(name="video_reference", referencedColumnName="id", nullable=true)
  159. */
  160. private ?VideoReference $videoReference;
  161. public function __construct()
  162. {
  163. $this->enabled = true;
  164. $this->mathproblems = new ArrayCollection();
  165. $this->teachers = new ArrayCollection();
  166. $this->quizSessions = new ArrayCollection();
  167. $this->createdAt = new \DateTime("now");
  168. $this->imageReference = null;
  169. $this->videoReference = null;
  170. $this->audioReference = null;
  171. }
  172. /**
  173. * Get id.
  174. *
  175. * @return int
  176. */
  177. public function getId()
  178. {
  179. return $this->id;
  180. }
  181. /**
  182. * @return mixed
  183. */
  184. public function getName()
  185. {
  186. return $this->name;
  187. }
  188. /**
  189. * @param string $name
  190. *
  191. * @return $this
  192. */
  193. public function setName($name)
  194. {
  195. $this->name = $name;
  196. return $this;
  197. }
  198. /**
  199. * Set duration.
  200. *
  201. * @param int $duration
  202. *
  203. * @return $this
  204. */
  205. public function setDuration($duration)
  206. {
  207. $this->duration = $duration;
  208. return $this;
  209. }
  210. /**
  211. * Get duration.
  212. *
  213. * @return int
  214. */
  215. public function getDuration()
  216. {
  217. return $this->duration;
  218. }
  219. /**
  220. * @return ArrayCollection|Mathproblem[]
  221. */
  222. public function getMathproblems(): Collection
  223. {
  224. $mathproblems = new ArrayCollection();
  225. foreach ($this->mathproblems as $mathproblem) {
  226. $mathproblems->add($mathproblem->getMathproblem());
  227. }
  228. return $mathproblems;
  229. }
  230. /**
  231. * @return ArrayCollection|Mathproblem[]
  232. */
  233. public function getEnabledMathproblems(): Collection
  234. {
  235. $mathproblems = new ArrayCollection();
  236. foreach ($this->mathproblems as $mathproblem) {
  237. if ($mathproblem->getEnabled()) {
  238. $mathproblems->add($mathproblem->getMathproblem());
  239. }
  240. }
  241. return $mathproblems;
  242. }
  243. /**
  244. * @return ArrayCollection|Mathproblem[]
  245. */
  246. public function getEnabledAndPublishedMathproblems(): Collection
  247. {
  248. $mathproblems = new ArrayCollection();
  249. foreach ($this->mathproblems as $mathproblem) {
  250. if ($mathproblem->getEnabled() && $mathproblem->getMathproblem()->isPublished()) {
  251. $mathproblems->add($mathproblem->getMathproblem());
  252. }
  253. }
  254. return $mathproblems;
  255. }
  256. /**
  257. * @return array
  258. */
  259. public function getSortedMathproblems()
  260. {
  261. $problemOrder = $this->problemOrder;
  262. $mathproblems = $this->getEnabledMathproblems();
  263. if ($problemOrder && count($problemOrder) == count($mathproblems)) {
  264. $newmathproblems = [];
  265. foreach ($mathproblems as $mp) {
  266. $newkey = array_search($mp->getID(), $problemOrder);
  267. $newmathproblems[$newkey] = $mp;
  268. }
  269. ksort($newmathproblems);
  270. return $newmathproblems;
  271. } else {
  272. return $this->getEnabledMathproblems()->toArray();
  273. }
  274. }
  275. /**
  276. * @param Mathproblem[]
  277. */
  278. public function setMathproblems(array $mathproblems)
  279. {
  280. // don't allow setting problems to a quiz that already has problems
  281. if ($this->mathproblems->count() > 0) {
  282. throw new BadRequestException('Cannot set mathproblems on a quiz that already has mathproblems');
  283. }
  284. foreach ($mathproblems as $mathproblem) {
  285. $this->addMathproblem($mathproblem);
  286. }
  287. }
  288. /**
  289. * This collection can be used to edit the relations directly,
  290. * remember to also remove the entity, when removing a relation.
  291. *
  292. * @return ArrayCollection|Collection
  293. */
  294. public function getMathproblemRelations(): ArrayCollection|Collection
  295. {
  296. return $this->mathproblems;
  297. }
  298. /**
  299. * @param Mathproblem
  300. */
  301. public function addMathproblem($mathproblem)
  302. {
  303. $relation = new RelationQuizMathproblem();
  304. $relation->setQuiz($this);
  305. $relation->setMathproblem($mathproblem);
  306. $relation->setEnabled(true);
  307. $this->mathproblems->add($relation);
  308. }
  309. /**
  310. * @return bool
  311. */
  312. public function getReusable()
  313. {
  314. return $this->reusable;
  315. }
  316. /**
  317. * @param bool $reusable
  318. *
  319. * @return $this
  320. */
  321. public function setReusable($reusable)
  322. {
  323. $this->reusable = $reusable;
  324. return $this;
  325. }
  326. /**
  327. * @return \DateTime
  328. */
  329. public function getCreatedAt()
  330. {
  331. return $this->createdAt;
  332. }
  333. /**
  334. * @return User
  335. */
  336. public function getCreatedBy()
  337. {
  338. return $this->createdBy;
  339. }
  340. /**
  341. * @param User $createdBy
  342. *
  343. * @return $this
  344. */
  345. public function setCreatedBy($createdBy)
  346. {
  347. $this->createdBy = $createdBy;
  348. return $this;
  349. }
  350. /**
  351. * @return Collection
  352. */
  353. public function getTeachers(): Collection
  354. {
  355. return $this->teachers;
  356. }
  357. /**
  358. * @param User $teacher
  359. *
  360. * @return $this
  361. */
  362. public function addTeacher(User $teacher)
  363. {
  364. $this->teachers->add($teacher);
  365. return $this;
  366. }
  367. /**
  368. * @return Collection
  369. */
  370. public function getQuizSessions(): Collection
  371. {
  372. return $this->quizSessions;
  373. }
  374. /**
  375. * @param QuizSession[] $quizSessions
  376. *
  377. * @return $this
  378. */
  379. public function setQuizSessions($quizSessions)
  380. {
  381. $this->quizSessions = $quizSessions;
  382. return $this;
  383. }
  384. /**
  385. * @param QuizSession $quizSession
  386. *
  387. * @return $this
  388. */
  389. public function addQuizSession(QuizSession $quizSession)
  390. {
  391. $this->quizSessions->add($quizSession);
  392. $quizSession->setQuiz($this);
  393. return $this;
  394. }
  395. /**
  396. * @return bool
  397. */
  398. public function isAbacus()
  399. {
  400. return $this->abacus;
  401. }
  402. /**
  403. * @param bool $abacus
  404. */
  405. public function setAbacus($abacus)
  406. {
  407. $this->abacus = $abacus;
  408. }
  409. /**
  410. * @return bool | null
  411. */
  412. public function isTargetStudent()
  413. {
  414. return $this->targetStudent;
  415. }
  416. /**
  417. * @param bool $targetStudent
  418. */
  419. public function setTargetStudent(bool $targetStudent)
  420. {
  421. $this->targetStudent = $targetStudent;
  422. }
  423. /**
  424. * @return Topic | null
  425. */
  426. public function getTopic()
  427. {
  428. return $this->topic;
  429. }
  430. /**
  431. * @param Topic $topic
  432. */
  433. public function setTopic(Topic $topic)
  434. {
  435. $this->topic = $topic;
  436. }
  437. /**
  438. * @return ClassroomType|null
  439. */
  440. public function getLevel(): ?ClassroomType
  441. {
  442. return $this->level;
  443. }
  444. /**
  445. * @param ClassroomType|null $level
  446. */
  447. public function setLevel(?ClassroomType $level): void
  448. {
  449. $this->level = $level;
  450. }
  451. /**
  452. * @return array|null
  453. */
  454. public function getProblemOrder(): ?array
  455. {
  456. return $this->problemOrder;
  457. }
  458. /**
  459. * @param array $problemOrder
  460. */
  461. public function setProblemOrder(array $problemOrder)
  462. {
  463. $this->problemOrder = $problemOrder;
  464. }
  465. /**
  466. * @return QuizType | null
  467. */
  468. public function getType(): QuizType | null
  469. {
  470. return $this->type;
  471. }
  472. /**
  473. * @param QuizType $type
  474. */
  475. public function setType(QuizType $type): void
  476. {
  477. $this->type = $type;
  478. }
  479. /**
  480. * @return array|null
  481. */
  482. public function getMathproblemsForTemplates()
  483. {
  484. return $this->mathproblemsForTemplates;
  485. }
  486. /**
  487. * @param array $mathproblemsForTemplates
  488. */
  489. public function setMathproblemsForTemplates(array $mathproblemsForTemplates): void
  490. {
  491. $this->mathproblemsForTemplates = $mathproblemsForTemplates;
  492. }
  493. /**
  494. * @return string | null
  495. */
  496. public function getDescription(): string | null
  497. {
  498. return $this->description;
  499. }
  500. /**
  501. * @param string $description
  502. */
  503. public function setDescription(string $description): void
  504. {
  505. $this->description = $description;
  506. }
  507. /**
  508. * @return array|null
  509. */
  510. public function getColorWrongCountThreshold(): ?array
  511. {
  512. return $this->colorWrongCountThreshold;
  513. }
  514. public function getColorPercentageThreshold(): ?array
  515. {
  516. return $this->colorPercentageThreshold;
  517. }
  518. public function getPublication(): ?Publication
  519. {
  520. return $this->publication;
  521. }
  522. public function setPublication(Publication $publication): void
  523. {
  524. $this->publication = $publication;
  525. }
  526. public function getEnabled(): bool
  527. {
  528. return $this->enabled;
  529. }
  530. public function setEnabled(string $enabled): void
  531. {
  532. $this->enabled = $enabled;
  533. }
  534. public function __toString()
  535. {
  536. return (string) $this->id;
  537. }
  538. public function getCmsImagePath(): string
  539. {
  540. if (!$this->imageReference) {
  541. return 'Mangler';
  542. }
  543. if (!$this->imageReference->getFilePath()) {
  544. return 'Mangler fil';
  545. }
  546. return $this->getImagePath();
  547. }
  548. public function getVideoReference(): ?VideoReference
  549. {
  550. return $this->videoReference;
  551. }
  552. public function setVideoReference(?VideoReference $videoReference): void
  553. {
  554. $this->videoReference = $videoReference;
  555. }
  556. public function getIBookId(): ?string
  557. {
  558. return $this->iBookId;
  559. }
  560. public function setIBookId(): void
  561. {
  562. $part1 = bin2hex(random_bytes(4));
  563. $part2 = $this->id;
  564. $this->iBookId = $part1 . '_' . $part2;
  565. }
  566. public function isABaCusQuiz(): bool
  567. {
  568. // abacus/kvik quizzes have abacus=1 and targetStudent=0
  569. return $this->abacus && !$this->targetStudent;
  570. }
  571. public function setIsABaCusQuiz($isABaCusQuiz): void
  572. {
  573. if ($isABaCusQuiz) {
  574. $this->abacus = true;
  575. $this->targetStudent = false;
  576. } else {
  577. $this->abacus = null;
  578. $this->targetStudent = null;
  579. }
  580. }
  581. public function getProblemCount(): int
  582. {
  583. return count($this->getEnabledMathproblems());
  584. }
  585. }