<?php
namespace App\Entity\Core\Peer;
use App\Entity\Core\Classroom\Classroom;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
#[ORM\Table('peer_session')]
#[ORM\Entity(repositoryClass: PeerSessionRepository::class)]
class PeerSession implements JsonSerializable
{
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private int $id;
#[ORM\Column(type: 'string', nullable: true)]
protected ?string $name;
#[ORM\Column(type: 'datetime', nullable: true)]
private ?DateTime $startTime;
#[ORM\Column(type: 'integer')]
private int $durationStage1;
#[ORM\Column(type: 'integer')]
private int $durationStage2;
#[ORM\Column(type: 'integer')]
private int $durationStage3;
#[ORM\JoinColumn(name: 'classroom', referencedColumnName: 'id', onDelete: 'CASCADE')]
#[ORM\ManyToOne(targetEntity: Classroom::class, inversedBy: 'peerSessions')]
private Classroom $classroom;
#[ORM\JoinColumn(name: 'peer_theme_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: PeerTheme::class, inversedBy: 'peerSessions')]
private PeerTheme $peerTheme;
/**
* @var Collection|ArrayCollection|PeerGroup[]
*/
#[ORM\OneToMany(targetEntity: PeerGroup::class, mappedBy: 'peerSession', cascade: ['persist', 'remove'])]
private Collection $peerGroups;
#[ORM\Column(type: 'integer', nullable: false, options: ['default' => 1])]
private int $isEnabled = 1;
#[ORM\JoinColumn(name: 'peer_theme_class_type_id', referencedColumnName: 'id')]
#[ORM\ManyToOne(targetEntity: PeerThemeClassType::class)]
private PeerThemeClassType $peerThemeClassType;
public function __construct()
{
$this->peerGroups = new ArrayCollection();
}
public function getName():string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getId(): int
{
return $this->id;
}
public function getStartTime(): ?DateTime
{
return $this->startTime;
}
public function setStartTime(DateTime $startTime)
{
$this->startTime = $startTime;
}
public function getDurationStage1(): int
{
return $this->durationStage1;
}
public function setDurationStage1(int $durationStage1)
{
$this->durationStage1 = $durationStage1;
}
public function getDurationStage2(): int
{
return $this->durationStage2;
}
public function setDurationStage2(int $durationStage2)
{
$this->durationStage2 = $durationStage2;
}
public function getDurationStage3(): int
{
return $this->durationStage3;
}
public function setDurationStage3(int $durationStage3)
{
$this->durationStage3 = $durationStage3;
}
public function getClassroom(): Classroom
{
return $this->classroom;
}
public function setClassroom(Classroom $classroom)
{
$this->classroom = $classroom;
}
public function getPeerTheme(): PeerTheme
{
return $this->peerTheme;
}
public function setPeerTheme(PeerTheme $peerTheme)
{
$this->peerTheme = $peerTheme;
}
/**
* @return Collection|ArrayCollection|PeerGroup[]
*/
public function getPeerGroups(): Collection
{
return $this->peerGroups;
}
public function addPeerGroup(PeerGroup $peerGroup)
{
$peerGroup->setPeerSession($this);
$this->peerGroups->add($peerGroup);
}
public function getIsEnabled(): int
{
return $this->isEnabled;
}
public function setIsEnabled(int $isEnabled)
{
$this->isEnabled = $isEnabled;
}
public function getPeerThemeClassType(): PeerThemeClassType
{
return $this->peerThemeClassType;
}
public function setPeerThemeClassType(PeerThemeClassType $peerThemeClassType)
{
$this->peerThemeClassType = $peerThemeClassType;
}
public function jsonSerialize(): mixed
{
$deadline = null;
if ($this->getStartTime()) {
$deadline = clone $this->getStartTime();
$totalDuration = $this->getDurationStage1() + $this->getDurationStage2() + $this->getDurationStage3();
$deadline->modify(sprintf('+%d minutes', $totalDuration));
}
$topics = [];
if (!empty($this->getPeerTheme()->getName())) {
$topics[] = ['name' => $this->getPeerTheme()->getName()];
}
return [
"name" => $this->name,
"id" => $this->getId(),
"startTime" => $this->getStartTime(),
"classroom" => $this->getClassroom(),
"duration" => $this->getDurationStage1(),
"deadline" => $deadline,
"topics" => $topics,
];
}
}