<?php
declare(strict_types=1);
namespace App\Entity\Core\ReadingTest;
use App\Entity\Core\Classroom\Classroom;
use App\Entity\Core\ActivitySessionInterface;
use App\Entity\Core\FolderAwareInterface;
use App\Entity\Core\FolderAwareTrait;
use App\Entity\User\User;
use DateTime;
use DateTimeZone;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use JsonSerializable;
#[ORM\Table('reading_test_session')]
#[ORM\Entity(repositoryClass: ReadingTestSessionRepository::class)]
class ReadingTestSession implements JsonSerializable, FolderAwareInterface, ActivitySessionInterface
{
use FolderAwareTrait;
private const FOLDER_ACTIVITY_TYPE = 'reading_test';
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
/**
* @var ReadingTest
*/
#[ORM\JoinColumn(name: 'readingTest', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: ReadingTest::class)]
private $readingTest;
/**
* @var User
*/
#[ORM\JoinColumn(name: 'created_by', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: User::class)]
private $createdBy;
/**
* @var Classroom
*/
#[ORM\ManyToOne(targetEntity: Classroom::class)]
private $classroom;
/**
* @var string
*/
#[ORM\Column(name: 'name', type: 'string', length: 255, nullable: true)]
private $name;
/**
* @var DateTime
*/
#[ORM\Column(name: 'startTime', type: 'datetime', nullable: true)]
private $startTime;
/**
* @var DateTime
*/
#[ORM\Column(name: 'deadline', type: 'datetime', nullable: true)]
private $deadline;
/**
* @var DateTime
*/
#[ORM\Column(name: 'createdAt', type: 'datetime', nullable: false)]
private $createdAt;
/**
* @var ReadingTestUnit[]
*/
#[ORM\OneToMany(targetEntity: ReadingTestUnit::class, mappedBy: 'readingTestSession', cascade: ['persist'])]
private $readingTestUnits;
#[ORM\Column(type: 'boolean', nullable: true)]
private ?bool $deleted;
/**
* @var int
*/
#[ORM\Column(type: 'smallint', nullable: true)]
private $duration;
/**
* ReadingTestSession constructor.
* @throws Exception
*/
public function __construct()
{
$this->createdAt = new DateTime('now', new DateTimeZone('Europe/Copenhagen'));
$this->readingTestUnits = new ArrayCollection();
$this->startTime = new DateTime("now");
$this->deadline = new DateTime("+1DAY");
}
public function getId(): int
{
return $this->id;
}
public function setId(int $id): void
{
$this->id = $id;
}
/**
* @return ReadingTest
*/
public function getReadingTest()
{
return $this->readingTest;
}
public function setReadingTest(ReadingTest $readingTest): void
{
$this->readingTest = $readingTest;
}
/**
* @return User
*/
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(User $createdBy): void
{
$this->createdBy = $createdBy;
}
/**
* @return Classroom
*/
public function getClassroom(): ?Classroom
{
return $this->classroom;
}
public function setClassroom(Classroom $classroom): void
{
$this->classroom = $classroom;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
public function setName(?string $name): void
{
$this->name = $name;
}
/**
* @return DateTime
*/
public function getStartTime()
{
return $this->startTime;
}
public function setStartTime(DateTime $startTime): void
{
$this->startTime = $startTime;
}
/**
* @return DateTime
*/
public function getDeadline()
{
return $this->deadline;
}
public function setDeadline(\DateTime $deadline): void
{
$this->deadline = $deadline;
}
/**
* @return DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
public function setCreatedAt(\DateTime $createdAt): void
{
$this->createdAt = $createdAt;
}
/**
* @return ReadingTestUnit[]
*/
public function getReadingTestUnits()
{
return $this->readingTestUnits;
}
/**
* @param ReadingTestUnit[] $readingTestUnits
*/
public function setReadingTestUnits(array $readingTestUnits): void
{
$this->readingTestUnits = $readingTestUnits;
}
public function isDeleted(): ?bool
{
return $this->deleted;
}
public function setDeleted(bool $deleted): void
{
$this->deleted = $deleted;
}
public function getDuration(): ?int
{
return $this->duration;
}
public function setDuration(?int $duration)
{
$this->duration = $duration;
}
public function jsonSerialize(): mixed
{
$topics = [];
$readingTest = $this->getReadingTest();
if ($readingTest) {
foreach ($readingTest->getSortedMathproblems() as $mathproblem) {
$template = $mathproblem->getTemplate();
if (!$template) {
continue;
}
if($topic = $template->getTopic()){
$topics[$topic->getId()] = $topic->jsonSerialize();
}
}
$topics = array_values($topics);
}
return [
'duration' => $this->getDuration(),
'id' => $this->getId(),
'name' => $this->prefixNameWithFolderSortOrder($this->getName(), self::FOLDER_ACTIVITY_TYPE, $this->getReadingTest()?->getId()),
'startTime' => $this->getStartTime(),
'deadline' => $this->getDeadline(),
'topics' => $topics,
'session_type' => basename(str_replace('\\', '/', __CLASS__)),
'activity_type' => 'reading_test',
'start' => $this->getStartTime()->getTimestamp()
];
}
public function getSortOrderInFolder(): ?int
{
return $this->getFolderSortOrder(self::FOLDER_ACTIVITY_TYPE, $this->getReadingTest()?->getId());
}
}