src/CoreBundle/Entity/ClassroomPointPool.php line 13

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