<?php
declare(strict_types=1);
namespace App\Entity\Core\Dictate;
use App\Entity\Core\Classroom\Classroom;
use App\Entity\User\User;
use App\Entity\Core\ActivitySessionInterface;
use App\Entity\Core\FolderAwareInterface;
use App\Entity\Core\FolderAwareTrait;
use DateTime;
use DateTimeZone;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use JsonSerializable;
#[ORM\Table('dictate_session')]
#[ORM\Entity(repositoryClass: DictateSessionRepository::class)]
class DictateSession implements JsonSerializable, FolderAwareInterface, ActivitySessionInterface
{
use FolderAwareTrait;
private const FOLDER_ACTIVITY_TYPE = 'dictate';
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private int $id;
/**
* @var Dictate
*/
#[ORM\JoinColumn(name: 'dictate', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: Dictate::class)]
private Dictate $dictate;
/**
* @var User
*/
#[ORM\JoinColumn(name: 'created_by', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: User::class)]
private User $createdBy;
/**
* @var
*/
#[ORM\ManyToOne(targetEntity: Classroom::class)]
private Classroom $classroom;
/**
* @var
*/
#[ORM\Column(name: 'name', type: 'string', length: 255, nullable: true)]
private ?string $name;
#[ORM\Column(name: 'startTime', type: 'datetime', nullable: true)]
private ?DateTime $startTime;
#[ORM\Column(name: 'deadline', type: 'datetime', nullable: true)]
private ?DateTime $deadline;
#[ORM\Column(name: 'createdAt', type: 'datetime', nullable: false)]
private DateTime $createdAt;
/**
* @var DictateUnit[]
*/
#[ORM\OneToMany(targetEntity: DictateUnit::class, mappedBy: 'dictateSession', cascade: ['persist'])]
private Collection $dictateUnits;
#[ORM\Column(type: 'boolean', nullable: true)]
private ?bool $deleted;
#[ORM\Column(type: 'smallint', nullable: true)]
private ?int $duration;
/**
* DictateSession constructor.
* @throws Exception
*/
public function __construct()
{
$this->createdAt = new DateTime('now', new DateTimeZone('Europe/Copenhagen'));
$this->dictateUnits = 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;
}
public function getDictate(): Dictate
{
return $this->dictate;
}
public function setDictate(Dictate $dictate): void
{
$this->dictate = $dictate;
}
public function getCreatedBy(): User
{
return $this->createdBy;
}
public function setCreatedBy(User $createdBy): void
{
$this->createdBy = $createdBy;
}
public function getClassroom(): Classroom
{
return $this->classroom;
}
public function setClassroom(Classroom $classroom): void
{
$this->classroom = $classroom;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): void
{
$this->name = $name;
}
public function getStartTime(): ?DateTime
{
return $this->startTime;
}
public function setStartTime(DateTime $startTime): void
{
$this->startTime = $startTime;
}
public function getDeadline(): ?DateTime
{
return $this->deadline;
}
public function setDeadline(?DateTime $deadline): void
{
$this->deadline = $deadline;
}
public function getCreatedAt(): DateTime
{
return $this->createdAt;
}
public function setCreatedAt(DateTime $createdAt): void
{
$this->createdAt = $createdAt;
}
public function getDictateUnits(): Collection
{
return $this->dictateUnits;
}
/**
* @param Collection|DictateUnit[] $dictateUnits
*/
public function setDictateUnits(Collection $dictateUnits): void
{
$this->dictateUnits = $dictateUnits;
}
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): void
{
$this->duration = $duration;
}
/**
* Specify data which should be serialized to JSON
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4
*/
public function jsonSerialize(): array
{
$topics = [];
$dictate = $this->getDictate();
if ($dictate && !empty($dictate->getIntroduction())) {
$topics[] = ['name' => $dictate->getIntroduction()];
}
return [
'id' => $this->getId(),
'name' => $this->prefixNameWithFolderSortOrder($this->getName(), self::FOLDER_ACTIVITY_TYPE, $this->getDictate()?->getId()),
'deadline' => $this->getDeadline(),
'startTime' => $this->getStartTime(),
'duration' => $this->getDuration(),
'topics' => $topics,
'session_type' => basename(str_replace('\\', '/', __CLASS__)),
'activity_type' => 'dictate',
'start' => $this->getStartTime()->getTimestamp()
];
}
public function getSortOrderInFolder(): ?int
{
return $this->getFolderSortOrder(self::FOLDER_ACTIVITY_TYPE, $this->getDictate()?->getId());
}
}