<?php
namespace App\Entity\Training;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\Training\PlanningRepository;
use App\Traits\Actions;
use App\Entity\Training\Groupe;
use App\Entity\Training\Planline;
use App\Entity\Training\Trainee;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: PlanningRepository::class)]
#[ORM\Table(name: 'hs_tc_training_planning')]
#[ApiResource]
class Planning implements \Stringable
{
use Actions;
#[ORM\ManyToOne(targetEntity: "Groupe", inversedBy: "plannings")]
#[ORM\JoinColumn(nullable: true)]
private Groupe $groupe;
#[ORM\Column(name: "event", type: "string", length: 255)]
private string $event;
#[ORM\Column(name: "start_At", type: "datetime", nullable: true)]
private \DateTime $startAt;
#[ORM\Column(name: "year", type: "string", length: 255, nullable: true)]
private string $year;
#[ORM\Column(name: "start_number", type: "string", length: 255, nullable: true)]
private string $startNumber;
#[ORM\Column(name: "order_number", type: "string", length: 255, nullable: true)]
private string $orderNumber;
#[ORM\OneToMany(mappedBy: "planning", targetEntity: "Planline", cascade: ["persist"], orphanRemoval: true)]
#[ORM\OrderBy(["takeAt" => "ASC"])]
private ?Collection $planlines = null;
public function __construct()
{
// date_default_timezone_set('Africa/Casablanca');
$this->createAt=new \DateTime('now');
$this->published=true;
}
/**
* @return Groupe
*/
public function getGroupe(): ?Groupe
{
return $this->groupe;
}
/**
* @param Groupe $groupe
*/
public function setGroupe(?Groupe $groupe): Planning
{
$this->groupe = $groupe;
return $this;
}
/**
* @return string
*/
public function getEvent(): ?string
{
return $this->event;
}
/**
* @param string $event
*/
public function setEvent(?string $event): Planning
{
$this->event = $event;
return $this;
}
/**
* @return \DateTime
*/
public function getStartAt(): ?\DateTime
{
return $this->startAt;
}
/**
* @param \DateTime $startAt
*/
public function setStartAt(?\DateTime $startAt): Planning
{
$this->startAt = $startAt;
return $this;
}
/**
* @return string
*/
public function getYear(): ?string
{
return $this->year;
}
/**
* @param string $year
*/
public function setYear(?string $year): Planning
{
$this->year = $year;
return $this;
}
public function getYearFr()
{
return match ($this->year) {
'first_year' => '1ère année',
'second_year' => '2ème année',
'third_year' => '3ème année',
default => '',
};
}
/**
* @return string
*/
public function getStartNumber(): ?string
{
return $this->startNumber;
}
/**
* @param string $startNumber
*/
public function setStartNumber(?string $startNumber): Planning
{
$this->startNumber = $startNumber;
return $this;
}
/**
* @return string
*/
public function getOrderNumber(): ?string
{
return $this->orderNumber;
}
/**
* @param string $orderNumber
*/
public function setOrderNumber(?string $orderNumber): Planning
{
$this->orderNumber = $orderNumber;
return $this;
}
/**
* Add planline
*
*
* @return Planning
*/
public function addPlanline(Planline $planline)
{
$planline->setPlanning($this);
$this->planlines->add($planline);
return $this;
}
/**
* Remove planline
*/
public function removePlanline(Planline $planline)
{
$this->planlines->removeElement($planline);
}
/**
* Get planlines
*
* @return Collection
*/
public function getPlanlines()
{
return $this->planlines;
}
public function getLinesSorted(){
/** @var Planline $item */
return $this->planlines->groupBy(fn($item) => $item->getStartAt()->format('Y-m-d'));
}
public function generateOrder(){
/* $numbers=[];
while (count($numbers)<$this->groupe->getTrainees()->count()){
$index=array_rand($this->groupe->getTrainees()->toArray());
if(!in_array($this->groupe->getTrainees()[$index]->getId(), $numbers)){
$numbers[]=$this->groupe->getTrainees()[$index]->getId();
}
}
$this->setOrderNumber(implode(',', $numbers));*/
$activeTrainees = array_filter(
$this->groupe->getTrainees()->toArray(),
fn($trainee) => !$trainee->isAbandoned() // Méthode à adapter selon votre entité
);
// Mélanger la liste
shuffle($activeTrainees);
// Récupérer les IDs
$numbers = array_map(fn($trainee) => $trainee->getId(), $activeTrainees);
$this->setOrderNumber(implode(',', $numbers));
}
public function getTraineeNumber(Trainee $trainee){
$numbers=explode(',',$this->getOrderNumber());
return intval($this->startNumber)+array_search($trainee->getId(), $numbers);
}
public function __toString(): string
{
return $this->event.' '.$this->groupe;
}
}