<?php
namespace App\Entity\Core\Worksheet;
use App\Entity\Core\Classroom\Classroom;
use App\Entity\User\User;
use DateTime;
use DateTimeZone;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
#[ORM\Table('worksheet_session')]
#[ORM\Entity(repositoryClass: WorksheetSessionRepository::class)]
class WorksheetSession implements JsonSerializable
{
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
/**
* @var Worksheet
*/
#[ORM\JoinColumn(name: 'worksheet', referencedColumnName: 'id', onDelete: 'CASCADE', nullable: true)]
#[ORM\ManyToOne(targetEntity: Worksheet::class, inversedBy: 'worksheetSessions')]
private $worksheet;
/**
* @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 bool
*/
#[ORM\Column(name: 'dormant', type: 'boolean', nullable: true)]
private $dormant;
/**
* @var int
*/
#[ORM\Column(type: 'smallint', nullable: true)]
private $duration;
/**
* @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;
#[ORM\Column(name: 'deadline', type: 'datetime', nullable: true)]
private ?DateTime $deadline;
/**
* @var DateTime
*/
#[ORM\Column(name: 'createdAt', type: 'datetime', nullable: false)]
private $createdAt;
/**
* @var integer
* [0 - created, in phase of editing; 1 - ready to start, if it's dormant ; 2 - startTime set]
*/
#[ORM\Column(type: 'integer', nullable: false, options: ['default' => 0])]
private $status = 0;
/**
* @var integer
*/
#[ORM\Column(type: 'integer', nullable: false, options: ['default' => 1])]
private $isEnabled = 1;
/**
* @var WorksheetUnit[]
*/
#[ORM\OneToMany(targetEntity: WorksheetUnit::class, mappedBy: 'worksheetSession', cascade: ['persist'])]
private $worksheetUnits;
/**
* @var bool
*/
#[ORM\Column(type: 'boolean', nullable: true)]
private $durationIgnored;
/**
* @var bool
*/
#[ORM\Column(name: 'solutions_available', type: 'boolean', nullable: true)]
private $solutionsAvailable;
public function __construct(?int $duration)
{
$this->createdAt = new DateTime('now', new DateTimeZone('Europe/Copenhagen'));
$this->worksheetUnits = new ArrayCollection();
$this->duration = $duration;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return Worksheet
*/
public function getWorksheet()
{
return $this->worksheet;
}
/**
* @param Worksheet $worksheet
* @return $this
*/
public function setWorksheet($worksheet)
{
$this->worksheet = $worksheet;
return $this;
}
/**
* @return User
*/
public function getCreatedBy()
{
return $this->createdBy;
}
/**
* @param User $createdBy
* @return $this
*/
public function setCreatedBy($createdBy)
{
$this->createdBy = $createdBy;
return $this;
}
/**
* @return Classroom
*/
public function getClassroom()
{
return $this->classroom;
}
/**
* @param Classroom $classroom
* @return $this
*/
public function setClassroom($classroom)
{
$this->classroom = $classroom;
return $this;
}
/**
* @return boolean
*/
public function isDormant()
{
return $this->dormant;
}
/**
* @param boolean $dormant
* @return $this
*/
public function setDormant($dormant)
{
$this->dormant = $dormant;
return $this;
}
/**
* @return DateTime
*/
public function getStartTime()
{
return $this->startTime;
}
/**
* @param DateTime $startTime
* @return $this
*/
public function setStartTime($startTime)
{
$this->startTime = $startTime;
return $this;
}
/**
* @return DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* @return mixed
*/
public function getStatus()
{
return $this->status;
}
/**
* @param mixed $status
* @return $this
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* @return WorksheetUnit[]
*/
public function getWorksheetUnits()
{
return $this->worksheetUnits;
}
/**
* @param WorksheetUnit[] $worksheetUnits
* @return $this
*/
public function setWorksheetUnits($worksheetUnits)
{
$this->worksheetUnits = $worksheetUnits;
return $this;
}
/**
* @return $this
*/
public function addWorksheetUnit(WorksheetUnit $worksheetUnit)
{
$worksheetUnit->setWorksheetSession($this);
$this->worksheetUnits->add($worksheetUnit);
return $this;
}
public function getEndTime()
{
if ($this->getDeadline() !== null) {
return $this->getDeadline();
}
$endTime = clone $this->startTime;
return $endTime->modify(sprintf('+%d minutes', $this->getDuration()));
}
/**
* @param int $isEnabled
*/
public function setIsEnabled($isEnabled)
{
$this->isEnabled = $isEnabled;
}
/**
* @return int
*/
public function getIsEnabled()
{
return $this->isEnabled;
}
public function getDeadline(): ?DateTime
{
return $this->deadline;
}
public function setDeadline(?DateTime $deadline = null)
{
$this->deadline = $deadline;
}
public function isDurationIgnored(): ?bool
{
return $this->durationIgnored;
}
public function setDurationIgnored(bool $durationIgnored)
{
$this->durationIgnored = $durationIgnored;
}
public function getDuration(): ?int
{
return $this->duration;
}
public function setDuration(?int $duration)
{
$this->duration = $duration;
}
public function isSolutionsAvailable(): ?bool
{
return $this->solutionsAvailable;
}
public function setSolutionsAvailable(?bool $solutionsAvailable)
{
$this->solutionsAvailable = $solutionsAvailable;
}
private function cmpWorksheetUnits(WorksheetUnit $a, WorksheetUnit $b): bool
{
return strcmp($a->getStudent()->getNickname(), $b->getStudent()->getNickname());
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name)
{
$this->name = $name;
}
public function isHomework(): bool
{
return boolval($this->deadline);
}
public function jsonSerialize(): mixed
{
return [
"id" => $this->getId(),
"name" => $this->getName(),
"duration" => $this->getDuration(),
"startTime" => $this->getStartTime(),
];
}
}