<?php
declare(strict_types=1);
namespace App\Entity\Core;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
use function str_replace;
#[ORM\Table('answer')]
#[ORM\Entity(repositoryClass: AnswerRepository::class)]
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;
#[ORM\Column(name: 'is_correct', type: 'boolean')]
private bool $isCorrect;
#[ORM\JoinColumn(name: 'mathproblem_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: Mathproblem::class, inversedBy: 'answers')]
private Mathproblem $mathproblem;
#[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;
}
public function getId(): ?int
{
return $this->id;
}
public function getPosition(): ?int
{
return $this->position;
}
public function setPosition(?int $position): void
{
$this->position = $position;
}
public function getMargin(): ?float
{
return $this->margin;
}
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;
}
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);
}
/**
* @return $this
*/
public function setIsCorrect(bool $isCorrect): self
{
$this->isCorrect = $isCorrect;
return $this;
}
public function getIsCorrect(): bool
{
return $this->isCorrect;
}
/**
* @return void
*/
public function setMathproblem(Mathproblem $mathproblem)
{
$this->mathproblem = $mathproblem;
}
public function getMathproblem(): Mathproblem
{
return $this->mathproblem;
}
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;
}
}