<?php
namespace App\Entity\Core\ReadingTest;
use App\Entity\Core\Classroom\ClassroomType;
use App\Entity\Core\Mathproblem;
use App\Services\Core\ActivityTypeMappingService;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
#[ORM\Table('reading_test')]
#[ORM\Entity(repositoryClass: ReadingTestRepository::class)]
class ReadingTest implements JsonSerializable
{
private const int DEFAULT_DURATION = 15;
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
/**
* @var string
*/
#[ORM\Column(name: 'title', type: 'string', length: 255, nullable: true)]
private $title;
/**
* @var string
*/
#[ORM\Column(name: 'text', type: 'text', length: 65000, nullable: false)]
private $text;
/**
* @var ClassroomType
*/
#[ORM\JoinColumn(name: 'classroom_type', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: ClassroomType::class)]
private $classroomType;
#[ORM\JoinTable(name: 'reading_test_mathproblem')]
#[ORM\JoinColumn(name: 'reading_test_id')]
#[ORM\InverseJoinColumn(name: 'mathproblem_id')]
#[ORM\ManyToMany(targetEntity: Mathproblem::class)]
private Collection $mathproblems;
/**
* @var array
*/
#[ORM\Column(name: 'problem_order', type: 'simple_array', nullable: true)]
private $problemOrder;
/**
* @var array
*/
#[ORM\Column(name: 'color_wrong_count_threshold', type: 'simple_array', nullable: true)]
private $colorWrongCountThreshold;
/**
* @var array
*/
#[ORM\Column(name: 'color_wrong_word_per_minute_threshold', type: 'simple_array', nullable: true)]
private $colorWrongWordPerMinuteThreshold;
public function __construct()
{
$this->mathproblems = new ArrayCollection();
}
public function getId(): int
{
return $this->id;
}
public function setId(int $id): void
{
$this->id = $id;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): void
{
$this->title = $title;
}
public function getText(): string
{
return $this->text;
}
public function setText(string $text): void
{
$this->text = $text;
}
/**
* @return ClassroomType
*/
public function getClassroomType()
{
return $this->classroomType;
}
public function setClassroomType(ClassroomType $classroomType): void
{
$this->classroomType = $classroomType;
}
/**
* @return ArrayCollection|Mathproblem[]
*/
public function getMathproblems(): Collection
{
return $this->mathproblems;
}
/**
* @param Mathproblem[]
*/
public function setMathproblems(array $mathproblems): self
{
$this->mathproblems = new ArrayCollection($mathproblems);
return $this;
}
public function getProblemOrder(): ?array
{
return $this->problemOrder;
}
public function setProblemOrder(array $problemOrder)
{
$this->problemOrder = $problemOrder;
}
public function getColorWrongCountThreshold(): ?array
{
return $this->colorWrongCountThreshold;
}
/**
* @return $this
*/
public function setColorWrongCountThreshold(?array $colorWrongCountThreshold): self
{
$this->colorWrongCountThreshold = $colorWrongCountThreshold;
return $this;
}
public function getColorWrongWordPerMinuteThreshold(): ?array
{
return $this->colorWrongWordPerMinuteThreshold;
}
/**
* @return $this
*/
public function setColorWrongWordPerMinuteThreshold(?array $colorWrongWordPerMinuteThreshold): self
{
$this->colorWrongWordPerMinuteThreshold = $colorWrongWordPerMinuteThreshold;
return $this;
}
/**
* @return array
*/
public function getSortedMathproblems()
{
$problemOrder = $this->problemOrder;
$mathproblems = $this->mathproblems;
if ($problemOrder && count($problemOrder) == count($mathproblems)) {
$newmathproblems = [];
foreach ($mathproblems as $mp) {
$newkey = array_search($mp->getId(), $problemOrder);
$newmathproblems[$newkey] = $mp;
}
ksort($newmathproblems);
return $newmathproblems;
} else {
return $this->mathproblems->toArray();
}
}
public function toLightweightArray(ActivityTypeMappingService $activityTypeMappingService): array
{
return [
'id' => $this->getId(),
'type' => $activityTypeMappingService->getActivityTypeFromClass(ReadingTest::class),
'name' => $this->getTitle(),
'problemCount' => 1,
'classroomType' => $this->getClassroomType()?->getId(),
];
}
public function jsonSerialize(): array
{
return [
'id' => $this->getId(),
'title' => $this->getTitle(),
'classroomType' => $this->getClassroomType()->getId(),
'problemCount' => 1,
'duration' => self::DEFAULT_DURATION, // Add this to have a fallback for screening upon request from Sofie, well knowing that the activity does not come with a duration as is. Perhaps it should be added at some point
];
}
}