<?php
declare(strict_types=1);
namespace App\Entity\Core\Classroom;
use App\Entity\Core\Institution;
use App\Entity\Core\Peer\PeerSession;
use App\Entity\Core\Quiz\QuizSession;
use App\Entity\Core\Session\Session;
use App\Entity\User\User;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\PersistentCollection;
use JsonSerializable;
#[ORM\Table('classroom')]
#[ORM\Entity(repositoryClass: ClassroomRepository::class)]
class Classroom implements JsonSerializable
{
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private int $id;
#[ORM\Column(name: 'name', type: 'string', length: 255, nullable: false)]
private string $name;
#[ORM\Column(name: 'password', type: 'string', length: 255)]
private string $password;
/**
* @var User[]
*/
#[ORM\JoinTable(name: 'classroom_teacher')]
#[ORM\JoinColumn(name: 'classroom', referencedColumnName: 'id')]
#[ORM\InverseJoinColumn(name: 'teacher', referencedColumnName: 'id')]
#[ORM\ManyToMany(targetEntity: User::class)]
private array|ArrayCollection|PersistentCollection $teachers;
/**
* @var ClassroomStudent[]
*/
#[ORM\OneToMany(targetEntity: ClassroomStudent::class, mappedBy: 'classroom', cascade: ['persist', 'remove'])]
private array|ArrayCollection|PersistentCollection $members;
/**
* @var Session[]
*/
#[ORM\OneToMany(targetEntity: Session::class, mappedBy: 'classroom', cascade: ['persist'])]
private array|ArrayCollection|PersistentCollection $sessions;
#[ORM\OneToMany(targetEntity: PeerSession::class, mappedBy: 'classroom', cascade: ['persist'])]
private ArrayCollection|PersistentCollection $peerSessions;
/**
* @var QuizSession[]
*/
#[ORM\OneToMany(targetEntity: QuizSession::class, mappedBy: 'classroom', cascade: ['persist'])]
private array|ArrayCollection|PersistentCollection $quizSessions;
#[ORM\Column(name: 'is_demo', type: 'boolean', options: ['default' => false])]
private bool $isDemo;
#[ORM\Column(name: 'is_active', type: 'boolean', options: ['default' => true])]
private bool $isActive;
#[ORM\Column(type: 'boolean', nullable: true)]
private ?bool $customer;
#[ORM\JoinColumn(name: 'institution', referencedColumnName: 'id', onDelete: 'CASCADE', nullable: true)]
#[ORM\ManyToOne(targetEntity: Institution::class)]
protected ?Institution $institution = null;
#[ORM\JoinColumn(name: 'type', referencedColumnName: 'id', nullable: true)]
#[ORM\ManyToOne(targetEntity: ClassroomType::class)]
private ?ClassroomType $type = null;
#[ORM\Column(name: 'timestamp', type: 'datetime', nullable: true)]
private ?DateTime $timestamp;
public function __construct()
{
$this->members = new ArrayCollection();
$this->teachers = new ArrayCollection();
$this->sessions = new ArrayCollection();
$this->peerSessions = new ArrayCollection();
$this->quizSessions = new ArrayCollection();
$bytes = random_bytes(4);
$this->password = bin2hex($bytes);
}
public function getId(): ?int
{
return $this->id;
}
public function setName(string $name): Classroom
{
$this->name = $name;
return $this;
}
public function setNameForDemoClassroom(User $user): void
{
$this->name = "Testklasse ({$user->getUsername()})";
}
public function getName(): string
{
return $this->name;
}
public function getTeachers(): ArrayCollection|Array|PersistentCollection
{
return $this->teachers;
}
public function addTeacher(User $teacher): Classroom
{
$this->teachers->add($teacher);
return $this;
}
public function removeTeacher(User $teacher): bool
{
return $this->teachers->removeElement($teacher);
}
/**
* @return ClassroomStudent[]
*/
public function getMembers(): ArrayCollection|PersistentCollection
{
return $this->members;
}
public function getMembersIds(): array|ArrayCollection
{
$membersIds = [];
foreach ($this->members as $member) {
$membersIds[] = $member->getStudent()->getId();
}
return $membersIds;
}
public function addMember(ClassroomStudent $ClassroomStudent): Classroom
{
$ClassroomStudent->setClassroom($this);
$this->members->add($ClassroomStudent);
return $this;
}
public function removeMember(ClassroomStudent $ClassroomStudent): bool
{
$ClassroomStudent->setClassroom(null);
return $this->members->removeElement($ClassroomStudent);
}
public function getSessions(): array|ArrayCollection
{
return $this->sessions;
}
public function addSession(Session $session): Classroom
{
$session->setClassroom($this);
$this->sessions->add($session);
return $this;
}
public function removeSession(Session $session): bool
{
$session->setClassroom(null);
return $this->sessions->removeElement($session);
}
/**
* @param User[] $teachers
*/
public function setTeachers(array $teachers): Classroom
{
$this->teachers = $teachers;
return $this;
}
public function getInstitution(): ?Institution
{
return $this->institution;
}
public function setInstitution(Institution $institution): Classroom
{
$this->institution = $institution;
return $this;
}
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): Classroom
{
$this->password = $password;
return $this;
}
public function getPeerSessions(): ArrayCollection
{
return $this->peerSessions;
}
public function isDemo(): bool
{
return $this->isDemo;
}
public function setIsDemo(bool $isDemo): Classroom
{
$this->isDemo = $isDemo;
return $this;
}
public function isActive(): bool
{
return $this->isActive;
}
public function setIsActive($isActive): void
{
$this->isActive = $isActive;
}
public function getTimestamp(): ?DateTime
{
return $this->timestamp;
}
public function setTimestamp(DateTime $timestamp): void
{
$this->timestamp = $timestamp;
}
public function isCustomer(): ?bool
{
return $this->customer;
}
public function setCustomer(bool $customer): void
{
$this->customer = $customer;
}
public function getType(): ?ClassroomType
{
return $this->type;
}
public function setType(?ClassroomType $type): void
{
$this->type = $type;
}
public function hasTeacher(User $user): bool
{
foreach ($this->getTeachers() as $teacher) {
if ($teacher->getId() === $user->getId())
return true;
}
return false;
}
public function jsonSerialize(): mixed
{
return [
"id" => $this->getId(),
"name" => $this->getName(),
"type" => $this->getType(),
"timeStamp" => $this->getTimestamp(),
"institution" => $this->getInstitution(),
"members" => $this->getMembers()->toArray(),
"memberIds" => $this->getMembersIds(),
"teachers" => $this->getTeachers()->toArray()
];
}
}