src/CoreBundle/Entity/Answer.php line 16

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CoreBundle\Entity;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use JsonSerializable;
  7. use function str_replace;
  8. /**
  9. * @ORM\Table("answer")
  10. * @ORM\Entity(repositoryClass="CoreBundle\Entity\AnswerRepository")
  11. */
  12. class Answer implements JsonSerializable, HasMediaInterface
  13. {
  14. use HasMediaTraits;
  15. /**
  16. * @ORM\Column(name="id", type="integer")
  17. * @ORM\Id
  18. * @ORM\GeneratedValue(strategy="AUTO")
  19. */
  20. private $id;
  21. /**
  22. * @ORM\Column(name="answer_text", type="text", nullable=true)
  23. */
  24. private ?string $answerText;
  25. /**
  26. * TODO: delete when media migration is done
  27. * @ORM\Column(type="text", nullable=true)
  28. */
  29. private ?string $img;
  30. /**
  31. * @ORM\Column(name="is_correct", type="boolean")
  32. */
  33. private bool $isCorrect;
  34. /**
  35. * @ORM\ManyToOne(targetEntity="Mathproblem", inversedBy="answers")
  36. * @ORM\JoinColumn(name="mathproblem_id", referencedColumnName="id")
  37. */
  38. private Mathproblem $mathproblem;
  39. /**
  40. * TODO: delete when media migration is done
  41. * @ORM\Column(type="text", nullable=true)
  42. */
  43. private ?string $audio;
  44. /**
  45. * @ORM\Column(type="smallint", nullable=true)
  46. */
  47. private ?int $bin;
  48. private $transformedText;
  49. /**
  50. * @var float|null
  51. *
  52. * @ORM\Column(name="margin", type="float", nullable=true)
  53. */
  54. private ?float $margin = null;
  55. /**
  56. * @var int|null
  57. *
  58. * @ORM\Column(name="position", type="integer", nullable=true)
  59. */
  60. private ?int $position = null;
  61. public function __construct()
  62. {
  63. $this->imageReference = null;
  64. $this->audioReference = null;
  65. $this->img = null;
  66. $this->audio = null;
  67. }
  68. public function getId(): ?int
  69. {
  70. return $this->id;
  71. }
  72. /**
  73. * @return int|null
  74. */
  75. public function getPosition(): ?int
  76. {
  77. return $this->position;
  78. }
  79. /**
  80. * @param ?int $position
  81. * @return void
  82. */
  83. public function setPosition(?int $position): void
  84. {
  85. $this->position = $position;
  86. }
  87. /**
  88. * @return float|null
  89. */
  90. public function getMargin(): ?float
  91. {
  92. return $this->margin;
  93. }
  94. /**
  95. * @param ?float $margin
  96. * @return void
  97. */
  98. public function setMargin(?float $margin): void
  99. {
  100. $this->margin = $margin;
  101. }
  102. /**
  103. * @param string $answerText
  104. * @return $this
  105. */
  106. public function setAnswerText(?string $answerText): self
  107. {
  108. $this->answerText = $answerText;
  109. return $this;
  110. }
  111. /**
  112. * @return string
  113. */
  114. public function getAnswerText(): ?string
  115. {
  116. return $this->answerText;
  117. }
  118. /**
  119. * @return string
  120. */
  121. public function getCleanedAnswerText(): string
  122. {
  123. /* Get rid of Latex $'s.
  124. * Decimal numbers are written like 1{,}83 so we remove the braces. */
  125. $cleanedText = str_replace(['$', '{', '}'], '', $this->answerText);
  126. /* Commas are always replaced by periods before answers are submitted
  127. * so we do the same for answers from DB. */
  128. return str_replace(',', '.', $cleanedText);
  129. }
  130. public function getCleanedAnswerTextFromCurrency(): string
  131. {
  132. // 1.234.567,89 => 1234567.89
  133. $cleanedText = str_replace(['$', '{', '}'], '', $this->answerText);
  134. $cleanedText = str_replace('.', '', $cleanedText);
  135. return str_replace(',', '.', $cleanedText);
  136. }
  137. /**
  138. * @param bool $isCorrect
  139. * @return $this
  140. */
  141. public function setIsCorrect(bool $isCorrect): self
  142. {
  143. $this->isCorrect = $isCorrect;
  144. return $this;
  145. }
  146. /**
  147. * @return bool
  148. */
  149. public function getIsCorrect(): bool
  150. {
  151. return $this->isCorrect;
  152. }
  153. /**
  154. * @param Mathproblem $mathproblem
  155. * @return void
  156. */
  157. public function setMathproblem(Mathproblem $mathproblem)
  158. {
  159. $this->mathproblem = $mathproblem;
  160. }
  161. /**
  162. * @return Mathproblem
  163. */
  164. public function getMathproblem(): Mathproblem
  165. {
  166. return $this->mathproblem;
  167. }
  168. /**
  169. * @return int|null
  170. */
  171. public function getBin(): ?int
  172. {
  173. return $this->bin;
  174. }
  175. public function setBin(?int $bin): void
  176. {
  177. $this->bin = $bin;
  178. }
  179. public function getImg(): ?string
  180. {
  181. return $this->getImagePath();
  182. }
  183. public function getAudio(): ?string
  184. {
  185. return $this->getAudioPath();
  186. }
  187. public function setTransformedText($transformedText){
  188. $this->transformedText = $transformedText;
  189. }
  190. public function getTransformedText(){
  191. return $this->transformedText;
  192. }
  193. public function jsonSerialize(): mixed
  194. {
  195. $txt = empty(trim($this->answerText ?? '')) ? null : $this->answerText;
  196. $this->bin ??= null;
  197. $return = [
  198. "id" => $this->id,
  199. "txt" => $txt,
  200. "img" => $this->getImagePath(),
  201. "audio" => $this->getAudioPath(),
  202. "binIdx" => $this->bin,
  203. 'transformedText' => $this->transformedText
  204. ];
  205. $mathProblem = $this->getMathproblem();
  206. $mathProblemType = $mathProblem->getType();
  207. switch ($mathProblemType) {
  208. case TemplateMathproblem::TYPE_WORD_OPTIONS:
  209. case TemplateMathproblem::TYPE_MARK:
  210. case TemplateMathproblem::TYPE_PARAGRAPH:
  211. $return['correct'] = $this->isCorrect;
  212. break;
  213. default:
  214. break;
  215. }
  216. return $return;
  217. }
  218. }