src/Entity/Core/Answer.php line 13

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