src/Entity/Core/Peer/PeerSolution.php line 17

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