<?php
namespace App\Entity\Core\Homework;
use App\Entity\Core\Topic\TopicCounterInterface;
use App\Entity\Core\Topic\TopicCounterTrait;
use App\Entity\User\User;
use App\Session\Core\SessionUnitInterface;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
#[ORM\Table('homework_unit')]
#[ORM\Entity(repositoryClass: HomeworkUnitRepository::class)]
class HomeworkUnit implements TopicCounterInterface, JsonSerializable, SessionUnitInterface
{
use TopicCounterTrait;
public function getParent(): Homework
{
return $this->getHomework();
}
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
/**
* @var int
*/
#[ORM\Column(name: 'current_difficulty', type: 'integer', options: ['default' => 3])]
private $currentDifficulty;
/**
* @var int
*/
#[ORM\Column(name: 'prev_answer_correct', nullable: true, type: 'boolean', options: ['default' => false])]
private $prevAnswerCorrect;
/**
* @var int
*/
#[ORM\Column(name: 'prev_difficulty', type: 'integer', nullable: true, options: ['default' => 1])]
private $prevDifficulty;
/**
* @var Homework
*/
#[ORM\JoinColumn(name: 'homework', referencedColumnName: 'id', onDelete: 'CASCADE')]
#[ORM\ManyToOne(targetEntity: Homework::class, inversedBy: 'homeworkUnits')]
private $homework;
/**
* @var User
*/
#[ORM\ManyToOne(targetEntity: User::class, cascade: ['persist', 'remove'])]
private $student;
/**
* @var HomeworkResponse[]
*/
#[ORM\OneToMany(targetEntity: HomeworkResponse::class, mappedBy: 'homeworkUnit', cascade: ['persist', 'remove'])]
private $homeworkResponses;
/**
* @var DateTime
*/
#[ORM\Column(name: 'startTime', type: 'datetime', nullable: true)]
private $startTime;
/**
* @var DateTime
*/
#[ORM\Column(name: 'deadline', type: 'datetime', nullable: true)]
private $deadline;
/**
* @var int
*/
#[ORM\Column(name: 'seconds_left', type: 'integer', nullable: true, options: ['default' => 0])]
private $secondsLeft;
/**
* @var int
*/
#[ORM\Column(name: 'seconds_paused', type: 'integer', nullable: false, options: ['default' => 0])]
private $secondsPaused;
/**
* @var DateTime
*/
#[ORM\Column(name: 'time_paused', type: 'datetime', nullable: true)]
private $timePaused;
/**
* @var DateTime
*/
#[ORM\Column(name: 'time_resumed', type: 'datetime', nullable: true)]
private $timeResumed;
/**
* @var DateTime
*/
#[ORM\Column(name: 'extended_deadline', type: 'datetime', nullable: true)]
private $extendedDeadline;
public function __construct()
{
$this->homeworkResponses = new ArrayCollection();
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return int
*/
public function getCurrentDifficulty()
{
return $this->currentDifficulty;
}
/**
* @param int $currentDifficulty
*/
public function setCurrentDifficulty($currentDifficulty)
{
$this->currentDifficulty = $currentDifficulty;
}
/**
* @return int
*/
public function getPrevAnswerCorrect()
{
return $this->prevAnswerCorrect;
}
/**
* @param int $prevAnswerCorrect
*/
public function setPrevAnswerCorrect($prevAnswerCorrect)
{
$this->prevAnswerCorrect = $prevAnswerCorrect;
}
/**
* @return int
*/
public function getPrevDifficulty()
{
return $this->prevDifficulty;
}
/**
* @param int $prevDifficulty
*/
public function setPrevDifficulty($prevDifficulty)
{
$this->prevDifficulty = $prevDifficulty;
}
/**
* @return Homework
*/
public function getHomework()
{
return $this->homework;
}
/**
* @param Homework $homework
*/
public function setHomework($homework)
{
$this->homework = $homework;
}
public function getSession(): ?Homework
{
return $this->homework;
}
/**
* @return User
*/
public function getStudent()
{
return $this->student;
}
/**
* @param User $student
*/
public function setStudent($student)
{
$this->student = $student;
}
public function getStartTime(): ?DateTime
{
return $this->startTime;
}
/**
* @param DateTime $startTime
*/
public function setStartTime(?DateTime $startTime)
{
$this->startTime = $startTime;
}
/**
* @return HomeworkResponse[]
*/
public function getHomeworkResponses()
{
return $this->homeworkResponses;
}
/**
* @return array
*/
public function getValidHomeworkResponses()
{
$homeworkResponses = [];
foreach ($this->homeworkResponses as $response) {
if ($response->isValid()) {
$homeworkResponses[] = $response;
}
}
return $homeworkResponses;
}
/**
* @param HomeworkResponse[] $homeworkResponses
*/
public function setHomeworkResponses($homeworkResponses)
{
$this->homeworkResponses = $homeworkResponses;
}
public function getDeadline(): DateTime
{
return $this->deadline;
}
/**
* @param DateTime $deadline
* @return $this
*/
public function setDeadline($deadline)
{
$this->deadline = $deadline;
return $this;
}
public function isPaused(): bool
{
return $this->timePaused > $this->timeResumed;
}
public function getSecondsLeft(): int
{
return $this->secondsLeft;
}
public function setSecondsLeft(?int $secondsLeft)
{
$this->secondsLeft = $secondsLeft;
}
public function getSecondsPaused(): int
{
return $this->secondsPaused;
}
public function setSecondsPaused(int $secondsPaused)
{
$this->secondsPaused = $secondsPaused;
}
public function addSecondsPaused(int $secondsPaused)
{
$this->secondsPaused += $secondsPaused;
}
public function getTimePaused(): ?DateTime
{
return $this->timePaused;
}
public function setTimePaused(?DateTime $timePaused)
{
$this->timePaused = $timePaused;
}
public function getTimeResumed(): ?DateTime
{
return $this->timeResumed;
}
public function setTimeResumed(?DateTime $timeResumed)
{
$this->timeResumed = $timeResumed;
}
public function getExtendedDeadline(): ?\DateTime
{
return $this->extendedDeadline;
}
public function setExtendedDeadline(DateTime $extendedDeadline): void
{
$this->extendedDeadline = $extendedDeadline;
}
public function jsonSerialize(): mixed
{
return [
'id' => $this->id,
'startTime' => $this->startTime,
'deadline' => $this->deadline,
'student' => $this->student,
'currentDifficulty' => $this->currentDifficulty,
'extendedDeadline' => $this->extendedDeadline,
'prevAnswerCorrect' => $this->prevAnswerCorrect,
'prevDifficulty' => $this->prevDifficulty,
'secondsLeft' => $this->secondsLeft,
'secondsPaused' => $this->secondsPaused,
'timePaused' => $this->timePaused,
'timeResumed' => $this->timeResumed,
'currentTopicNr' => $this->currentTopicNr
];
}
}