src/Entity/Training/Planning.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Training;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Repository\Training\PlanningRepository;
  5. use App\Traits\Actions;
  6. use App\Entity\Training\Groupe;
  7. use App\Entity\Training\Planline;
  8. use App\Entity\Training\Trainee;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. #[ORM\Entity(repositoryClassPlanningRepository::class)]
  13. #[ORM\Table(name'hs_tc_training_planning')]
  14. #[ApiResource]
  15. class Planning implements \Stringable
  16. {
  17.     use Actions;
  18.     #[ORM\ManyToOne(targetEntity"Groupe"inversedBy"plannings")]
  19.     #[ORM\JoinColumn(nullabletrue)]
  20.     private Groupe $groupe;
  21.     #[ORM\Column(name"event"type"string"length255)]
  22.     private string $event;
  23.     #[ORM\Column(name"start_At"type"datetime"nullabletrue)]
  24.     private \DateTime $startAt;
  25.     #[ORM\Column(name"year"type"string"length255nullabletrue)]
  26.     private string $year;
  27.     #[ORM\Column(name"start_number"type"string"length255nullabletrue)]
  28.     private string $startNumber;
  29.     #[ORM\Column(name"order_number"type"string"length255nullabletrue)]
  30.     private string $orderNumber;
  31.     #[ORM\OneToMany(mappedBy"planning"targetEntity"Planline"cascade: ["persist"], orphanRemovaltrue)]
  32.     #[ORM\OrderBy(["takeAt" => "ASC"])]
  33.     private ?Collection $planlines null;
  34.     public function __construct()
  35.     {
  36.         // date_default_timezone_set('Africa/Casablanca');
  37.         $this->createAt=new \DateTime('now');
  38.         $this->published=true;
  39.     }
  40.     /**
  41.      * @return Groupe
  42.      */
  43.     public function getGroupe(): ?Groupe
  44.     {
  45.         return $this->groupe;
  46.     }
  47.     /**
  48.      * @param Groupe $groupe
  49.      */
  50.     public function setGroupe(?Groupe $groupe): Planning
  51.     {
  52.         $this->groupe $groupe;
  53.         return $this;
  54.     }
  55.     /**
  56.      * @return string
  57.      */
  58.     public function getEvent(): ?string
  59.     {
  60.         return $this->event;
  61.     }
  62.     /**
  63.      * @param string $event
  64.      */
  65.     public function setEvent(?string $event): Planning
  66.     {
  67.         $this->event $event;
  68.         return $this;
  69.     }
  70.     /**
  71.      * @return \DateTime
  72.      */
  73.     public function getStartAt(): ?\DateTime
  74.     {
  75.         return $this->startAt;
  76.     }
  77.     /**
  78.      * @param \DateTime $startAt
  79.      */
  80.     public function setStartAt(?\DateTime $startAt): Planning
  81.     {
  82.         $this->startAt $startAt;
  83.         return $this;
  84.     }
  85.     /**
  86.      * @return string
  87.      */
  88.     public function getYear(): ?string
  89.     {
  90.         return $this->year;
  91.     }
  92.     /**
  93.      * @param string $year
  94.      */
  95.     public function setYear(?string $year): Planning
  96.     {
  97.         $this->year $year;
  98.         return $this;
  99.     }
  100.     public function getYearFr()
  101.     {
  102.         return match ($this->year) {
  103.             'first_year' => '1ère année',
  104.             'second_year' => '2ème année',
  105.             'third_year' => '3ème année',
  106.             default => '',
  107.         };
  108.     }
  109.     /**
  110.      * @return string
  111.      */
  112.     public function getStartNumber(): ?string
  113.     {
  114.         return $this->startNumber;
  115.     }
  116.     /**
  117.      * @param string $startNumber
  118.      */
  119.     public function setStartNumber(?string $startNumber): Planning
  120.     {
  121.         $this->startNumber $startNumber;
  122.         return $this;
  123.     }
  124.     /**
  125.      * @return string
  126.      */
  127.     public function getOrderNumber(): ?string
  128.     {
  129.         return $this->orderNumber;
  130.     }
  131.     /**
  132.      * @param string $orderNumber
  133.      */
  134.     public function setOrderNumber(?string $orderNumber): Planning
  135.     {
  136.         $this->orderNumber $orderNumber;
  137.         return $this;
  138.     }
  139.     /**
  140.      * Add planline
  141.      *
  142.      *
  143.      * @return Planning
  144.      */
  145.     public function addPlanline(Planline $planline)
  146.     {
  147.         $planline->setPlanning($this);
  148.         $this->planlines->add($planline);
  149.         return $this;
  150.     }
  151.     /**
  152.      * Remove planline
  153.      */
  154.     public function removePlanline(Planline $planline)
  155.     {
  156.         $this->planlines->removeElement($planline);
  157.     }
  158.     /**
  159.      * Get planlines
  160.      *
  161.      * @return Collection
  162.      */
  163.     public function getPlanlines()
  164.     {
  165.         return $this->planlines;
  166.     }
  167.     public function getLinesSorted(){
  168.         /** @var Planline $item */
  169.         return $this->planlines->groupBy(fn($item) => $item->getStartAt()->format('Y-m-d'));
  170.     }
  171.     public function generateOrder(){
  172. /*        $numbers=[];
  173.         while (count($numbers)<$this->groupe->getTrainees()->count()){
  174.             $index=array_rand($this->groupe->getTrainees()->toArray());
  175.             if(!in_array($this->groupe->getTrainees()[$index]->getId(), $numbers)){
  176.                 $numbers[]=$this->groupe->getTrainees()[$index]->getId();
  177.             }
  178.         }
  179.         $this->setOrderNumber(implode(',', $numbers));*/
  180.         $activeTrainees array_filter(
  181.             $this->groupe->getTrainees()->toArray(),
  182.             fn($trainee) => !$trainee->isAbandoned() // Méthode à adapter selon votre entité
  183.         );
  184.         // Mélanger la liste
  185.         shuffle($activeTrainees);
  186.         // Récupérer les IDs
  187.         $numbers array_map(fn($trainee) => $trainee->getId(), $activeTrainees);
  188.         $this->setOrderNumber(implode(','$numbers));
  189.     }
  190.     public function getTraineeNumber(Trainee $trainee){
  191.         $numbers=explode(',',$this->getOrderNumber());
  192.         return intval($this->startNumber)+array_search($trainee->getId(), $numbers);
  193.     }
  194.     public function __toString(): string
  195.     {
  196.         return $this->event.' '.$this->groupe;
  197.     }
  198. }