<?php
namespace App\Entity\Core\CrammingActivity;
use App\Entity\Core\Classroom\Classroom;
use App\Entity\Core\TemplateSet;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use JsonSerializable;
#[ORM\Table('cramming_activity')]
#[ORM\Entity(repositoryClass: CrammingActivityRepository::class)]
class CrammingActivity implements JsonSerializable
{
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
protected $id;
#[ORM\Column(type: 'string', nullable: true)]
protected $name;
/**
* @var Classroom
*/
#[ORM\JoinColumn(name: 'classroom', referencedColumnName: 'id', onDelete: 'CASCADE')]
#[ORM\ManyToOne(targetEntity: Classroom::class)]
private $classroom;
/**
* JSON map of template set ID -> selected classroom type ID (or null).
*/
#[ORM\Column(name: 'template_set_classroom_types', type: 'text', nullable: true)]
private ?string $templateSetClassroomTypes = null;
/**
* @var DateTime
*/
#[ORM\Column(name: 'start_time', type: 'datetime', nullable: true)]
protected $startTime;
/**
* @var DateTime
*/
#[ORM\Column(name: 'deadline', type: 'datetime', nullable: true)]
protected $deadline;
#[ORM\Column(type: 'integer', nullable: true)]
protected $duration;
#[ORM\Column(name: 'is_enabled', type: 'integer', nullable: true, options: ['default' => 1])]
private $isEnabled = 1;
#[ORM\Column(name: 'template_sets_count', type: 'integer')]
private $templateSetsCount;
/**
* @var ArrayCollection|TemplateSet[]
*/
#[ORM\JoinTable(name: 'cramming_activity_template_sets')]
#[ORM\JoinColumn(name: 'cramming_activity', referencedColumnName: 'id')]
#[ORM\InverseJoinColumn(name: 'template_set', referencedColumnName: 'id')]
#[ORM\ManyToMany(targetEntity: TemplateSet::class)]
private $templateSets;
/**
* @var CrammingActivityUnit[]
*/
#[ORM\OneToMany(targetEntity: CrammingActivityUnit::class, mappedBy: 'crammingActivity', cascade: ['persist', 'remove'])]
private $crammingActivityUnits;
#[ORM\Column(name: 'show_hints', type: 'boolean', nullable: true, options: ['default' => false])]
private $showHints = false;
#[ORM\Column(name: 'is_homework', type: 'boolean', nullable: true, options: ['default' => false])]
private $isHomework = false;
#[ORM\Column(name: 'can_pause', type: 'boolean', nullable: true, options: ['default' => false])]
private $canPause = false;
public function __construct()
{
$this->startTime = new DateTime();
$this->crammingActivityUnits = new ArrayCollection();
$this->templateSets = new ArrayCollection();
}
/**
* @throws Exception
*/
public function resetActivity(): void
{
$this->id = null;
$this->startTime = new DateTime();
$this->crammingActivityUnits = new ArrayCollection();
$this->templateSets = new ArrayCollection();
}
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(?int $id): void
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name): void
{
$this->name = $name;
}
/**
* @return Classroom
*/
public function getClassroom(): ?Classroom
{
return $this->classroom;
}
/**
* @param Classroom $classroom
*/
public function setClassroom(?Classroom $classroom): void
{
$this->classroom = $classroom;
}
public function getTemplateSetClassroomTypeIds(): array
{
if ($this->templateSetClassroomTypes === null || $this->templateSetClassroomTypes === '') {
return [];
}
$decoded = json_decode($this->templateSetClassroomTypes, true);
return is_array($decoded) ? $decoded : [];
}
public function setTemplateSetClassroomTypeIds(?array $templateSetClassroomTypeIds): void
{
if (empty($templateSetClassroomTypeIds)) {
$this->templateSetClassroomTypes = null;
return;
}
$this->templateSetClassroomTypes = json_encode($templateSetClassroomTypeIds);
}
/**
* @return DateTime
*/
public function getStartTime(): ?DateTime
{
return $this->startTime;
}
/**
* @param DateTime $startTime
*/
public function setStartTime(?DateTime $startTime): void
{
$this->startTime = $startTime;
}
/**
* @return DateTime
*/
public function getDeadline(): ?DateTime
{
return $this->deadline;
}
/**
* @param DateTime $deadline
*/
public function setDeadline(?DateTime $deadline): void
{
$this->deadline = $deadline;
}
/**
* @return mixed
*/
public function getDuration()
{
return $this->duration;
}
/**
* @param mixed $duration
*/
public function setDuration($duration): void
{
$this->duration = $duration;
}
public function getIsEnabled(): int
{
return $this->isEnabled;
}
public function setIsEnabled(int $isEnabled): void
{
$this->isEnabled = $isEnabled;
}
/**
* @return mixed
*/
public function getTemplateSetsCount()
{
return $this->templateSetsCount;
}
/**
* @param mixed $templateSetsCount
*/
public function setTemplateSetsCount($templateSetsCount): void
{
$this->templateSetsCount = $templateSetsCount;
}
/**
* @return TemplateSet[]|ArrayCollection
*/
public function getTemplateSets()
{
return $this->templateSets;
}
/**
* @param TemplateSet[]|ArrayCollection $templateSets
*/
public function setTemplateSets($templateSets): void
{
$this->templateSets = $templateSets;
}
/**
* @return $this
*/
public function addTemplateSet(TemplateSet $templateSet)
{
$this->templateSets->add($templateSet);
return $this;
}
public function removeTemplateSet(TemplateSet $templateSet)
{
$this->templateSets->removeElement($templateSet);
}
/**
* @return bool
*/
public function removeStudent(TemplateSet $templateSet)
{
return $this->templateSets->removeElement($templateSet);
}
/**
* @return CrammingActivityUnit[]|ArrayCollection
*/
public function getCrammingActivityUnits()
{
return $this->crammingActivityUnits;
}
/**
* @param CrammingActivityUnit[] $crammingActivityUnits
*/
public function setCrammingActivityUnits(?array $crammingActivityUnits): void
{
$this->crammingActivityUnits = $crammingActivityUnits;
}
public function isShowHints(): bool
{
return $this->showHints;
}
public function setShowHints(bool $showHints): void
{
$this->showHints = $showHints;
}
public function isHomework(): bool
{
return $this->isHomework;
}
public function setIsHomework(bool $isHomework): void
{
$this->isHomework = $isHomework;
}
public function isCanPause(): bool
{
return $this->canPause;
}
public function setCanPause(bool $canPause): void
{
$this->canPause = $canPause;
}
public function isSelfStudy():bool
{
return $this->classroom === null;
}
public function jsonSerialize(): array
{
$topics = [];
foreach ($this->getTemplateSets() as $templateSet) {
$topic = $templateSet->getTopic();
if ($topic) {
$topics[$topic->getId()] = $topic->jsonSerialize();
}
}
return [
'id' => $this->getId(),
'name' => $this->getName(),
'deadline' => $this->getDeadline(),
'startTime' => $this->getStartTime(),
'duration' => $this->getDuration(),
'isHomework' => $this->isHomework(),
'topics' => array_values($topics),
];
}
}