src/Entity/Core/Gamification/GamificationProfile.php line 17

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Core\Gamification;
  4. use App\Entity\Core\KvikCustomization;
  5. use App\Entity\Core\KvikCustomizationBuy;
  6. use App\Entity\User\User;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use JsonSerializable;
  11. #[ORM\Table('gamification_profile')]
  12. #[ORM\Entity(repositoryClass: GamificationProfileRepository::class)]
  13. class GamificationProfile 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: 'student', referencedColumnName: 'id', onDelete: 'CASCADE')]
  20. #[ORM\ManyToOne(targetEntity: User::class)]
  21. private User $student;
  22. #[ORM\Column(name: 'current_points', type: 'integer', options: ['default' => 0])]
  23. private int $currentPoints = 0;
  24. #[ORM\Column(name: 'spent_points', type: 'integer', options: ['default' => 0])]
  25. private int $spentPoints = 0;
  26. #[ORM\OneToMany(targetEntity: KvikCustomizationBuy::class, mappedBy: 'gamificationProfile', cascade: ['persist'])]
  27. private Collection $kvikCustomizationBuys;
  28. #[ORM\JoinTable(name: 'kvik_customization_selection')]
  29. #[ORM\JoinColumn(name: 'gamification_profile', referencedColumnName: 'id')]
  30. #[ORM\InverseJoinColumn(name: 'kvik_customization', referencedColumnName: 'id')]
  31. #[ORM\ManyToMany(targetEntity: KvikCustomization::class)]
  32. private Collection $selectedKvikCustomizations;
  33. #[ORM\Column(type: 'boolean', options: ['default' => true])]
  34. private bool $themeEnabled = true;
  35. public function __construct()
  36. {
  37. $this->selectedKvikCustomizations = new ArrayCollection();
  38. $this->kvikCustomizationBuys = new ArrayCollection();
  39. }
  40. public function hasThemeEnabled(): bool
  41. {
  42. return $this->themeEnabled;
  43. }
  44. public function setThemeEnabled(bool $themeEnabled): void
  45. {
  46. $this->themeEnabled = $themeEnabled;
  47. }
  48. public function getId(): int
  49. {
  50. return $this->id;
  51. }
  52. public function setId(int $id): void
  53. {
  54. $this->id = $id;
  55. }
  56. public function getStudent(): User
  57. {
  58. return $this->student;
  59. }
  60. public function setStudent(User $student): void
  61. {
  62. $this->student = $student;
  63. }
  64. public function getCurrentPoints(): int
  65. {
  66. return $this->currentPoints;
  67. }
  68. public function setCurrentPoints(int $currentPoints): void
  69. {
  70. $this->currentPoints = $currentPoints;
  71. }
  72. public function getSpentPoints(): int
  73. {
  74. return $this->spentPoints;
  75. }
  76. public function setSpentPoints(int $spentPoints): void
  77. {
  78. $this->spentPoints = $spentPoints;
  79. }
  80. public function getKvikCustomizationBuys(): Collection
  81. {
  82. return $this->kvikCustomizationBuys;
  83. }
  84. public function setKvikCustomizationBuys(Collection $kvikCustomizationBuys): void
  85. {
  86. $this->kvikCustomizationBuys = $kvikCustomizationBuys;
  87. }
  88. public function getSelectedKvikCustomizations(): Collection
  89. {
  90. return $this->selectedKvikCustomizations;
  91. }
  92. public function setSelectedKvikCustomizations(Collection $selectedKvikCustomizations): void
  93. {
  94. $this->selectedKvikCustomizations = $selectedKvikCustomizations;
  95. }
  96. public function spendPoints(int $points): void
  97. {
  98. if ($points > $this->currentPoints)
  99. return;
  100. $this->currentPoints -= $points;
  101. $this->spentPoints += $points;
  102. }
  103. public function jsonSerialize(): array
  104. {
  105. return [
  106. 'id' => $this->id,
  107. 'student' => $this->student,
  108. 'currentPoints' => $this->currentPoints,
  109. 'spentPoints' => $this->spentPoints,
  110. 'selectedKvikCustomizations' => $this->selectedKvikCustomizations->toArray(),
  111. 'themeEnabled' => $this->themeEnabled
  112. ];
  113. }
  114. }