src/Entity/Core/Classroom/ClassroomPointPool.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Core\Classroom;
  3. use DateTime;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use JsonSerializable;
  6. #[ORM\Table('classroom_point_pool')]
  7. #[ORM\Entity(repositoryClass: ClassroomPointPoolRepository::class)]
  8. class ClassroomPointPool implements JsonSerializable
  9. {
  10. private const int DEFAULT_POINT_GOAL_PER_STUDENT = 200;
  11. private const array MULTIPLIERS = [
  12. 'easy' => 0.75,
  13. 'medium' => 1.00,
  14. 'hard' => 1.25
  15. ];
  16. #[ORM\Column(name: 'id', type: 'integer')]
  17. #[ORM\Id]
  18. #[ORM\GeneratedValue(strategy: 'AUTO')]
  19. private int $id;
  20. #[ORM\JoinColumn(name: 'classroom_id', referencedColumnName: 'id', onDelete: 'CASCADE', nullable: false)]
  21. #[ORM\ManyToOne(targetEntity: Classroom::class)]
  22. private Classroom $classroom;
  23. #[ORM\Column(name: 'start')]
  24. private DateTime $start;
  25. #[ORM\Column(name: 'complete', nullable: true)]
  26. private ?DateTime $complete;
  27. #[ORM\Column(name: 'point_goal')]
  28. private int $pointGoal;
  29. #[ORM\Column(name: 'current_points')]
  30. private int $currentPoints;
  31. #[ORM\Column(name: 'difficulty', type: 'string', length: 16, options: ['default' => 'medium'])]
  32. private string $difficulty = 'medium';
  33. #[ORM\Column(name: 'goal_description', nullable: true)]
  34. private ?string $goalDescription;
  35. public function getId(): int
  36. {
  37. return $this->id;
  38. }
  39. public function setId(int $id): void
  40. {
  41. $this->id = $id;
  42. }
  43. public function getClassroom(): Classroom
  44. {
  45. return $this->classroom;
  46. }
  47. public function setClassroom(Classroom $classroom): void
  48. {
  49. $this->classroom = $classroom;
  50. }
  51. public function getStart(): DateTime
  52. {
  53. return $this->start;
  54. }
  55. public function setStart(DateTime $start): void
  56. {
  57. $this->start = $start;
  58. }
  59. public function getComplete(): ?DateTime
  60. {
  61. return $this->complete;
  62. }
  63. public function setComplete(?DateTime $complete): void
  64. {
  65. $this->complete = $complete;
  66. }
  67. public function getPointGoal(): int
  68. {
  69. return $this->pointGoal;
  70. }
  71. public function setPointGoal(int $pointGoal): void
  72. {
  73. $this->pointGoal = $pointGoal;
  74. }
  75. public function getCurrentPoints(): int
  76. {
  77. return $this->currentPoints;
  78. }
  79. public function setCurrentPoints(int $currentPoints): void
  80. {
  81. $this->currentPoints = $currentPoints;
  82. }
  83. public function getDifficulty(): string
  84. {
  85. return $this->difficulty;
  86. }
  87. public function setDifficulty(string $difficulty): void
  88. {
  89. $this->difficulty = $difficulty;
  90. }
  91. public function getGoalDescription(): ?string
  92. {
  93. return $this->goalDescription;
  94. }
  95. public function setGoalDescription(?string $goalDescription): void
  96. {
  97. $this->goalDescription = $goalDescription;
  98. }
  99. public function updatePointGoal(): bool
  100. {
  101. $numStudents = count($this->getClassroom()->getMembers());
  102. $multiplier = self::MULTIPLIERS[$this->difficulty] ?? self::MULTIPLIERS['medium'];
  103. $newGoal = max($numStudents, 1) * self::DEFAULT_POINT_GOAL_PER_STUDENT * $multiplier;
  104. if ($newGoal !== $this->pointGoal) {
  105. $this->pointGoal = $newGoal;
  106. return true;
  107. }
  108. return false;
  109. }
  110. public function getProgressPercentage(): float
  111. {
  112. if ($this->pointGoal === 0)
  113. return 0.0;
  114. $progress = $this->currentPoints / $this->pointGoal * 100;
  115. return min(round($progress, 1), 100.0);
  116. }
  117. public function jsonSerialize(): array
  118. {
  119. return [
  120. 'id' => $this->id,
  121. 'classroom' => [
  122. 'id' => $this->classroom->getId(),
  123. 'name' => $this->classroom->getName()
  124. ],
  125. 'start' => $this->start,
  126. 'complete' => $this->complete,
  127. 'pointGoal' => $this->pointGoal,
  128. 'currentPoints' => $this->currentPoints,
  129. 'progressPercentage' => $this->getProgressPercentage(),
  130. 'difficulty' => $this->difficulty,
  131. 'goalDescription' => $this->goalDescription
  132. ];
  133. }
  134. }