<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Formation\Classe;
use App\Entity\Training\Jury;
use App\Entity\Tutoring\Course;
use App\Traits\Actions;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use App\Entity\Month;
#[ApiResource]
#[ORM\Entity]
#[ORM\Table(name: 'hs_tc_schoolyear')]
class Schoolyear implements \Stringable
{
use Actions;
#[ORM\Column(type: 'string', length: 255)]
private string $title;
#[ORM\Column(type: 'datetime')]
private \DateTime $startAt;
#[ORM\Column(type: 'datetime')]
private \DateTime $endAt;
#[ORM\Column(type: 'boolean')]
private bool $current = false;
#[ORM\Column(type: 'boolean')]
private bool $opened = false;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $msgRegistration = null;
#[ORM\OneToOne(inversedBy: 'next', targetEntity: Schoolyear::class, cascade: ['persist'])]
#[ORM\JoinColumn(nullable: true)]
private ?Schoolyear $previous = null;
#[ORM\OneToOne(mappedBy: 'previous', targetEntity: Schoolyear::class)]
private ?Schoolyear $next = null;
#[ORM\OneToMany(mappedBy: 'schoolyear', targetEntity: Jury::class, cascade: ['all'])]
private Collection $jurys;
public function __construct(\DateTime $date=null)
{
// date_default_timezone_set('Africa/Casablanca');
$this->createAt=new \DateTime('now');
if($date===null){
$date=new \DateTime('now');
}
$y1=$date->format('Y');
$date->modify('+1 year');
$y2=$date->format('Y');
$this->title=$y1.'/'.$y2;
$this->startAt= new \DateTime($y1.'-10-01');
$this->endAt= new \DateTime($y2.'-07-31');
$this->published=false;
$this->opened=false;
$this->current=false;
$this->jurys=new ArrayCollection();
$this->id = 0;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): Schoolyear
{
$this->title = $title;
return $this;
}
public function getStartAt(): \DateTime
{
return $this->startAt;
}
public function setStartAt(\DateTime $startAt): Schoolyear
{
$this->startAt = $startAt;
return $this;
}
public function getEndAt(): \DateTime
{
return $this->endAt;
}
public function setEndAt(\DateTime $endAt): Schoolyear
{
$this->endAt = $endAt;
return $this;
}
public function isCurrent(): bool
{
return $this->current;
}
public function setCurrent(bool $current): Schoolyear
{
$this->current = $current;
return $this;
}
public function isOpened(): bool
{
return $this->opened;
}
public function setOpened(bool $opened): Schoolyear
{
$this->opened = $opened;
return $this;
}
public function getMsgRegistration(): ?string
{
return $this->msgRegistration;
}
public function setMsgRegistration(?string $msgRegistration): Schoolyear
{
$this->msgRegistration = $msgRegistration;
return $this;
}
public function getPrevious(): ?Schoolyear
{
return $this->previous;
}
public function setPrevious(?Schoolyear $previous): Schoolyear
{
$this->previous = $previous;
return $this;
}
public function getNext(): ?Schoolyear
{
return $this->next;
}
public function setNext(?Schoolyear $next): Schoolyear
{
$this->next = $next;
return $this;
}
// public function getMonths(): Collection
// {
// return $this->months;
// }
//
// public function setMonths(Collection $months): Schoolyear
// {
// $this->months = $months;
// return $this;
// }
function __toString(): string
{
return $this->title;
}
/**
* @return mixed
*/
public function getCourses()
{
return $this->courses;
}
/**
* @return Schoolyear
*/
public function setCourses(mixed $courses)
{
$this->courses = $courses;
return $this;
}
public function getJurys(): Collection
{
return $this->jurys;
}
public function setJurys(Collection $jurys): Schoolyear
{
$this->jurys = $jurys;
return $this;
}
public function addCourse(Course $course): Schoolyear {
$course->setSchoolyear($this);
$this->courses[]=$course;
return $this;
}
public function removeCourse(Course $course) {
$this->courses->removeElement($course);
}
public function addJury(Jury $jury): Schoolyear {
$jury->setSchoolyear($this);
$this->courses[]=$jury;
return $this;
}
public function removeJury(Jury $jury): void
{
$this->courses->removeElement($jury);
}
public function getButtonCurrent()
{
$confirm="data-toggle='confirmation'
data-btn-ok-label='Oui'
data-btn-ok-icon='glyphicon glyphicon-ok'
data-btn-ok-class='btn-success'
data-btn-cancel-label='Non'
data-btn-cancel-icon='glyphicon glyphicon-remove'
data-btn-cancel-class='btn-danger'
data-title='Changer statut ?'
data-popout='true'
data-content=\"Voulez-vous vraiment changer l'année scolaire actuelle ? \"";
if($this->current)
$btn="<i class=\"text-green fa fa-toggle-on\"> Oui</i>";
else
$btn="<a ".$confirm." href='javascript:void(0)' class='boledit-redirect' data-id='".$this->id."' data-route='".$this->getRoute('current')."'><i class=\"text-red fa fa-toggle-off\"> Non</i></a>";
return $btn;
}
public function getButtonOpened()
{
$confirm="data-toggle='confirmation'
data-btn-ok-label='Oui'
data-btn-ok-icon='glyphicon glyphicon-ok'
data-btn-ok-class='btn-success'
data-btn-cancel-label='Non'
data-btn-cancel-icon='glyphicon glyphicon-remove'
data-btn-cancel-class='btn-danger'
data-title=\"Ouvrir l'inscription ?\"
data-popout='true'
data-content=\"Voulez-vous vraiment ouvrir l'inscription pour cet année scolaire actuelle ? \"";
if($this->opened)
$btn="<a ".$confirm." href='javascript:void(0)' class='boledit-redirect' data-id='".$this->id."' data-route='".$this->getRoute('opened')."'><i class=\"text-green fa fa-toggle-on\"> Oui</i></a>";
else
$btn="<a ".$confirm." href='javascript:void(0)' class='boledit-redirect' data-id='".$this->id."' data-route='".$this->getRoute('opened')."'><i class=\"text-red fa fa-toggle-off\"> Non</i></a>";
return $btn;
}
public function getMonths($format='m-Y'){
// date_default_timezone_set('Africa/Casablanca');
$items=[];
$date= clone $this->startAt;
$date->modify('first day of this month');
while ($date<=$this->endAt){
$month=$date->format($format);
$items[]=$month;
$date->modify('+1 month');
}
return $items;
}
public function getIndexMonthCurrent(){
// date_default_timezone_set('Africa/Casablanca');
$new=new \DateTime('now');
$month=$new->format('m-Y');
if (array_search($month, $this->getMonths()))
return array_search($month, $this->getMonths());
else
if ($new<$this->startAt) return -1;
else return -2;
}
public function getMonthsCurrent($monthsPassed, $monthsComing, $format='m-Y')
{
$months = [];
$now=new \DateTimeImmutable('now');
// obtenir la date du premier jour du mois
$firstDayOfMonth = $now->modify('first day of this month');
for($i=-1*$monthsPassed; $i<=$monthsComing; $i++){
$m=($firstDayOfMonth->modify($i.' month'))->format($format);
if(in_array($m,$this->getMonths($format))){
$months[] = $m;
}
}
return $months;
}
public function getMonthsPassed(){
$ind=$this->getIndexMonthCurrent();
if($ind==-2) return $this->getMonths();
elseif ($ind==-1) return [];
else{
return array_slice($this->getMonths(),0,$ind+1);
}
}
public function getFirstYear(){
return intval($this->getStartAt()->format('Y'));
}
public function getSecondYear(){
return intval($this->getEndAt()->format('Y'));
}
public function getJury($pole){
if(!empty($this->jurys)) {
foreach ($this->jurys as $item) {
if ($item->getPole() == $pole) {
return $item;
}
}
}
return null;
}
public function isPassed()
{
return $this->endAt < new \DateTime('now');
}
public function isIn(\DateTime $date): bool
{
return $this->startAt <= $date && $this->endAt >= $date;
}
}