<?php
declare(strict_types=1);
namespace CoreBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
use function str_replace;
/**
* @ORM\Table("answer")
* @ORM\Entity(repositoryClass="CoreBundle\Entity\AnswerRepository")
*/
class Answer implements JsonSerializable, HasMediaInterface
{
use HasMediaTraits;
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="answer_text", type="text", nullable=true)
*/
private ?string $answerText;
/**
* TODO: delete when media migration is done
* @ORM\Column(type="text", nullable=true)
*/
private ?string $img;
/**
* @ORM\Column(name="is_correct", type="boolean")
*/
private bool $isCorrect;
/**
* @ORM\ManyToOne(targetEntity="Mathproblem", inversedBy="answers")
* @ORM\JoinColumn(name="mathproblem_id", referencedColumnName="id")
*/
private Mathproblem $mathproblem;
/**
* TODO: delete when media migration is done
* @ORM\Column(type="text", nullable=true)
*/
private ?string $audio;
/**
* @ORM\Column(type="smallint", nullable=true)
*/
private ?int $bin;
private $transformedText;
/**
* @var float|null
*
* @ORM\Column(name="margin", type="float", nullable=true)
*/
private ?float $margin = null;
/**
* @var int|null
*
* @ORM\Column(name="position", type="integer", nullable=true)
*/
private ?int $position = null;
public function __construct()
{
$this->imageReference = null;
$this->audioReference = null;
$this->img = null;
$this->audio = null;
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return int|null
*/
public function getPosition(): ?int
{
return $this->position;
}
/**
* @param ?int $position
* @return void
*/
public function setPosition(?int $position): void
{
$this->position = $position;
}
/**
* @return float|null
*/
public function getMargin(): ?float
{
return $this->margin;
}
/**
* @param ?float $margin
* @return void
*/
public function setMargin(?float $margin): void
{
$this->margin = $margin;
}
/**
* @param string $answerText
* @return $this
*/
public function setAnswerText(?string $answerText): self
{
$this->answerText = $answerText;
return $this;
}
/**
* @return string
*/
public function getAnswerText(): ?string
{
return $this->answerText;
}
/**
* @return string
*/
public function getCleanedAnswerText(): string
{
/* Get rid of Latex $'s.
* Decimal numbers are written like 1{,}83 so we remove the braces. */
$cleanedText = str_replace(['$', '{', '}'], '', $this->answerText);
/* Commas are always replaced by periods before answers are submitted
* so we do the same for answers from DB. */
return str_replace(',', '.', $cleanedText);
}
public function getCleanedAnswerTextFromCurrency(): string
{
// 1.234.567,89 => 1234567.89
$cleanedText = str_replace(['$', '{', '}'], '', $this->answerText);
$cleanedText = str_replace('.', '', $cleanedText);
return str_replace(',', '.', $cleanedText);
}
/**
* @param bool $isCorrect
* @return $this
*/
public function setIsCorrect(bool $isCorrect): self
{
$this->isCorrect = $isCorrect;
return $this;
}
/**
* @return bool
*/
public function getIsCorrect(): bool
{
return $this->isCorrect;
}
/**
* @param Mathproblem $mathproblem
* @return void
*/
public function setMathproblem(Mathproblem $mathproblem)
{
$this->mathproblem = $mathproblem;
}
/**
* @return Mathproblem
*/
public function getMathproblem(): Mathproblem
{
return $this->mathproblem;
}
/**
* @return int|null
*/
public function getBin(): ?int
{
return $this->bin;
}
public function setBin(?int $bin): void
{
$this->bin = $bin;
}
public function getImg(): ?string
{
return $this->getImagePath();
}
public function getAudio(): ?string
{
return $this->getAudioPath();
}
public function setTransformedText($transformedText){
$this->transformedText = $transformedText;
}
public function getTransformedText(){
return $this->transformedText;
}
public function jsonSerialize(): mixed
{
$txt = empty(trim($this->answerText ?? '')) ? null : $this->answerText;
$this->bin ??= null;
$return = [
"id" => $this->id,
"txt" => $txt,
"img" => $this->getImagePath(),
"audio" => $this->getAudioPath(),
"binIdx" => $this->bin,
'transformedText' => $this->transformedText
];
$mathProblem = $this->getMathproblem();
$mathProblemType = $mathProblem->getType();
switch ($mathProblemType) {
case TemplateMathproblem::TYPE_WORD_OPTIONS:
case TemplateMathproblem::TYPE_MARK:
case TemplateMathproblem::TYPE_PARAGRAPH:
$return['correct'] = $this->isCorrect;
break;
default:
break;
}
return $return;
}
}