src/Entity/Core/Training/TrainingUnit.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Core\Training;
  3. use App\Entity\Core\Answer;
  4. use App\Entity\Core\Session\Session;
  5. use App\Entity\Core\Topic\TopicCounterInterface;
  6. use App\Entity\Core\Topic\TopicCounterTrait;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Exception;
  11. use JsonSerializable;
  12. #[ORM\Table('training_unit')]
  13. #[ORM\Entity(repositoryClass: TrainingUnitRepository::class)]
  14. class TrainingUnit implements TopicCounterInterface, JsonSerializable
  15. {
  16. use TopicCounterTrait;
  17. /**
  18. * @var int
  19. */
  20. #[ORM\Column(name: 'id', type: 'integer')]
  21. #[ORM\Id]
  22. #[ORM\GeneratedValue(strategy: 'AUTO')]
  23. private $id;
  24. /**
  25. * @var Session
  26. */
  27. #[ORM\JoinColumn(name: 'session', referencedColumnName: 'id', onDelete: 'CASCADE')]
  28. #[ORM\ManyToOne(targetEntity: Session::class, inversedBy: 'TrainingUnits')]
  29. private $session;
  30. /**
  31. * @var Collection
  32. */
  33. #[ORM\OneToMany(targetEntity: TrainingStudent::class, mappedBy: 'trainingUnit', cascade: ['persist', 'remove'])]
  34. private $TrainingStudents;
  35. /**
  36. * @var TrainingResponse[]
  37. */
  38. #[ORM\OneToMany(targetEntity: TrainingResponse::class, mappedBy: 'trainingUnit', cascade: ['persist', 'remove'])]
  39. private $TrainingResponses;
  40. #[ORM\Column(type: 'array', nullable: true)]
  41. private $currentAnswers = [];
  42. public function __construct()
  43. {
  44. $this->TrainingStudents = new ArrayCollection();
  45. $this->TrainingResponses = new ArrayCollection();
  46. }
  47. /**
  48. * Get id.
  49. *
  50. * @return int
  51. */
  52. public function getId()
  53. {
  54. return $this->id;
  55. }
  56. /**
  57. * Set Session.
  58. *
  59. * @param Session $session
  60. *
  61. * @return TrainingUnit
  62. */
  63. public function setSession($session)
  64. {
  65. $this->session = $session;
  66. return $this;
  67. }
  68. /**
  69. * Get Session.
  70. */
  71. public function getSession(): ?Session
  72. {
  73. return $this->session;
  74. }
  75. public function getTrainingStudents(): Collection
  76. {
  77. return $this->TrainingStudents;
  78. }
  79. /**
  80. * @return TrainingUnit
  81. */
  82. public function addTrainingStudent(TrainingStudent $TrainingStudent)
  83. {
  84. $TrainingStudent->setTrainingUnit($this);
  85. $this->TrainingStudents->add($TrainingStudent);
  86. return $this;
  87. }
  88. /**
  89. * @return bool
  90. */
  91. public function removeTrainingStudent(TrainingStudent $TrainingStudent)
  92. {
  93. $TrainingStudent->setTrainingUnit(null);
  94. return $this->TrainingStudents->removeElement($TrainingStudent);
  95. }
  96. /**
  97. * @return TrainingResponse[]
  98. */
  99. public function getTrainingResponses()
  100. {
  101. return $this->TrainingResponses;
  102. }
  103. /**
  104. * @return array
  105. */
  106. public function getValidTrainingResponses()
  107. {
  108. $trainingResponses = [];
  109. foreach ($this->TrainingResponses as $response) {
  110. if ($response->isValid()) {
  111. $trainingResponses[] = $response;
  112. }
  113. }
  114. return $trainingResponses;
  115. }
  116. /**
  117. * @return TrainingUnit
  118. */
  119. public function addTrainingResponse(TrainingResponse $TrainingResponse)
  120. {
  121. $TrainingResponse->setTrainingUnit($this);
  122. $this->TrainingResponses->add($TrainingResponse);
  123. return $this;
  124. }
  125. /**
  126. * @return bool
  127. */
  128. public function removeTrainingResponse(TrainingResponse $TrainingResponse)
  129. {
  130. $TrainingResponse->setTrainingUnit(null);
  131. return $this->TrainingResponses->removeElement($TrainingResponse);
  132. }
  133. /**
  134. * @return int
  135. */
  136. public function getCorrectAnswerCount()
  137. {
  138. $correctCount = 0;
  139. foreach ($this->TrainingResponses as $answer) {
  140. if ($answer->getAnswerChosen() !== null && $answer->getAnswerChosen()->getIsCorrect()) {
  141. $correctCount++;
  142. }
  143. }
  144. return $correctCount;
  145. }
  146. /**
  147. * @return int
  148. */
  149. public function getMeanDifficultyFromCorrect()
  150. {
  151. $correctCount = 0;
  152. $totalDifficulty = 0;
  153. foreach ($this->TrainingResponses as $answer) {
  154. if ($answer->getAnswerChosen() !== null && $answer->getAnswerChosen()->getIsCorrect()) {
  155. $correctCount++;
  156. try{
  157. $totalDifficulty += $answer->getMathproblem()->getTemplate()->getDifficulty();
  158. } catch (Exception $e){
  159. // although it shouldn't fail due to non nullable constraints,
  160. // the effect of an answer not having difficulty shouldn't
  161. // impact on the result. (silently fail)
  162. }
  163. }
  164. }
  165. if($correctCount === 0){
  166. return -2;
  167. }
  168. return $totalDifficulty/$correctCount;
  169. }
  170. /**
  171. * @return int
  172. */
  173. public function getIncorrectAnswerCount()
  174. {
  175. return count($this->TrainingResponses) - $this->getCorrectAnswerCount();
  176. }
  177. /**
  178. * @return TrainingStudent
  179. */
  180. public function getTeamLeader()
  181. {
  182. foreach ($this->TrainingStudents as $student) {
  183. if ($student->getIsTeamLeader()) {
  184. return $student;
  185. }
  186. }
  187. return null;
  188. }
  189. public function getCurrentAnswers()
  190. {
  191. return $this->currentAnswers;
  192. }
  193. public function setStudentsCurrentAnswer(TrainingStudent $trainingStudent, Answer $answer)
  194. {
  195. $this->currentAnswers[$trainingStudent->getStudent()->getId()] = $answer->getId();
  196. }
  197. public function checkCurrentAnswers()
  198. {
  199. return count(array_unique(array_values($this->currentAnswers))) === 1 &&
  200. count($this->currentAnswers) == count($this->TrainingStudents);
  201. }
  202. public function jsonSerialize(): mixed
  203. {
  204. return [
  205. "id" => $this->getId(),
  206. "correctAnswerCount" => $this->getCorrectAnswerCount(),
  207. "currentAnswers" => $this->getCurrentAnswers(),
  208. "incorrectAnswerCount" => $this->getIncorrectAnswerCount(),
  209. "meanDifficultyFromCorrect" => $this->getMeanDifficultyFromCorrect(),
  210. "teamLeader" => $this->getTeamLeader(),
  211. "trainingStudents" => $this->getTrainingStudents()->toArray(),
  212. "validTrainingResponses" => $this->getValidTrainingResponses(),
  213. "currentTopicNr" => $this->getCurrentTopicNr(),
  214. ];
  215. }
  216. }