src/CoreBundle/Entity/PeerSolution.php line 16

Open in your IDE?
  1. <?php
  2. namespace CoreBundle\Entity;
  3. use DateTimeInterface;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use JsonSerializable;
  8. /**
  9. * @ORM\Table("peer_solution")
  10. * @ORM\Entity(repositoryClass="CoreBundle\Entity\PeerSolutionRepository")
  11. */
  12. class PeerSolution implements JsonSerializable
  13. {
  14. /**
  15. * @ORM\Column(name="id", type="integer")
  16. * @ORM\Id
  17. * @ORM\GeneratedValue(strategy="AUTO")
  18. */
  19. private int $id;
  20. /**
  21. * @ORM\OneToOne(targetEntity="CoreBundle\Entity\PeerUnit", inversedBy="peerSolution")
  22. * @ORM\JoinColumn(name="peer_unit", referencedColumnName="id", onDelete="CASCADE")
  23. */
  24. private PeerUnit $peerUnit;
  25. /**
  26. * @ORM\OneToMany(targetEntity="PeerFeedback", mappedBy="peerSolution", cascade={"persist","remove"})
  27. */
  28. private Collection $peerFeedbacks;
  29. /**
  30. * @ORM\Column(name="solution_text", type="text", nullable=true)
  31. */
  32. private ?string $solutionText;
  33. /**
  34. * @ORM\OneToMany(targetEntity="PeerSolutionFile", mappedBy="peerSolution", cascade={"persist"})
  35. */
  36. private Collection $peerSolutionFiles;
  37. /**
  38. * @ORM\Column(type="datetime", nullable=true)
  39. */
  40. private ?DateTimeInterface $updatedAt;
  41. /**
  42. * @ORM\Column(name="solution_result", type="text", nullable=true)
  43. */
  44. private ?string $solutionResult;
  45. public function __construct()
  46. {
  47. $this->peerFeedbacks = new ArrayCollection();
  48. $this->peerSolutionFiles = new ArrayCollection();
  49. }
  50. public function getId(): int
  51. {
  52. return $this->id;
  53. }
  54. public function getPeerUnit(): PeerUnit
  55. {
  56. return $this->peerUnit;
  57. }
  58. public function setPeerUnit(PeerUnit $peerUnit)
  59. {
  60. $this->peerUnit = $peerUnit;
  61. }
  62. public function getSolutionText(): ?string
  63. {
  64. return $this->solutionText;
  65. }
  66. /**
  67. * @param string $solutionText
  68. * @throws \Exception
  69. */
  70. public function setSolutionText(string $solutionText)
  71. {
  72. $this->solutionText = $solutionText;
  73. $this->updatedAt = new \DateTimeImmutable('now', new \DateTimeZone('Europe/Copenhagen'));
  74. }
  75. /**
  76. * @return ArrayCollection | PeerFeedback[]
  77. */
  78. public function getPeerFeedbacks(): Collection
  79. {
  80. return $this->peerFeedbacks;
  81. }
  82. public function hasAnswer(): bool
  83. {
  84. return (bool)strlen($this->solutionText) || !$this->peerSolutionFiles->isEmpty();
  85. }
  86. public function hasRatingByPeerUnit(PeerUnit $peerUnit): bool
  87. {
  88. foreach ($this->peerFeedbacks as $feedback) {
  89. /** @var $feedback PeerFeedback */
  90. if ($feedback->getPeerUnit() === $peerUnit && (bool)$feedback->getRating()) {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. public function getUpdatedAt(): ?DateTimeInterface
  97. {
  98. return $this->updatedAt;
  99. }
  100. public function addPeerSolutionFile(PeerSolutionFile $peerSolutionFile)
  101. {
  102. if (null !== $peerSolutionFile->getFile()) {
  103. $peerSolutionFile->setPeerSolution($this);
  104. $this->peerSolutionFiles->add($peerSolutionFile);
  105. }
  106. }
  107. public function removePeerSolutionFile(PeerSolutionFile $peerSolutionFile)
  108. {
  109. $this->peerSolutionFiles->removeElement($peerSolutionFile);
  110. }
  111. public function getPeerSolutionFiles(): Collection
  112. {
  113. return $this->peerSolutionFiles;
  114. }
  115. public function getSolutionResult(): ?string
  116. {
  117. return $this->solutionResult;
  118. }
  119. public function setSolutionResult(?string $solutionResult): void
  120. {
  121. $this->solutionResult = $solutionResult;
  122. }
  123. public function jsonSerialize(): mixed
  124. {
  125. $feedback = $this->peerFeedbacks->map(fn(PeerFeedback $fb) => [
  126. "id" => $fb->getId(),
  127. "txt" => $fb->getFeedbackText(),
  128. "studentNames" => $fb->getPeerUnit()->getPeerStudents()
  129. ->map(fn(PeerStudent $ps) => $ps->getStudent()->getFullName())->toArray(),
  130. "stars" => $fb->getRating(),
  131. "rating" => !empty($fb->getPeerFeedback2nd()) ? $fb->getPeerFeedback2nd()->getRating() : null,
  132. ]);
  133. return [
  134. "id" => $this->id,
  135. "txt" => $this->solutionText,
  136. "files" => $this->peerSolutionFiles->toArray(),
  137. "result" => $this->solutionResult,
  138. "updatedAt" => $this->updatedAt,
  139. "feedback" => $feedback->toArray(),
  140. ];
  141. }
  142. }