src/CoreBundle/Entity/TrainingUnit.php line 14

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