<?php
declare(strict_types=1);
namespace App\Entity\Core\SelfStudy;
use App\Entity\Core\Topic\Topic;
use App\Entity\User\User;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
#[ORM\Table('self_study_point_pool')]
#[ORM\Entity(repositoryClass: SelfStudyPointPoolRepository::class)]
class SelfStudyPointPool implements JsonSerializable
{
private const int POINT_GOAL = 450;
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private int $id;
#[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', onDelete: 'CASCADE', nullable: false)]
#[ORM\ManyToOne(targetEntity: User::class)]
private User $student;
#[ORM\JoinColumn(name: 'topic_id', referencedColumnName: 'id', onDelete: 'CASCADE', nullable: false)]
#[ORM\ManyToOne(targetEntity: Topic::class)]
private Topic $topic;
#[ORM\Column(name: 'current_points')]
private int $currentPoints;
public function getId(): int
{
return $this->id;
}
public function setId(int $id): void
{
$this->id = $id;
}
public function getStudent(): User
{
return $this->student;
}
public function setStudent(User $student): void
{
$this->student = $student;
}
public function getTopic(): Topic
{
return $this->topic;
}
public function setTopic(Topic $topic): void
{
$this->topic = $topic;
}
public function getCurrentPoints(): int
{
return $this->currentPoints;
}
public function setCurrentPoints(int $currentPoints): void
{
$this->currentPoints = $currentPoints;
}
public function getProgressPercentage(): float
{
if (self::POINT_GOAL === 0)
return 0.0;
$progress = $this->currentPoints / self::POINT_GOAL * 100;
return round($progress, 1);
}
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'student' => $this->student,
'topic' => $this->topic,
'currentPoints' => $this->currentPoints,
'progressPercentage' => $this->getProgressPercentage()
];
}
}