src/Entity/Schoolyear.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Entity\Formation\Classe;
  5. use App\Entity\Training\Jury;
  6. use App\Entity\Tutoring\Course;
  7. use App\Traits\Actions;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Doctrine\Common\Collections\Collection;
  11. use App\Entity\Month;
  12. #[ApiResource]
  13. #[ORM\Entity]
  14. #[ORM\Table(name'hs_tc_schoolyear')]
  15. class Schoolyear implements \Stringable
  16. {
  17.     use Actions;
  18.     #[ORM\Column(type'string'length255)]
  19.     private string $title;
  20.     #[ORM\Column(type'datetime')]
  21.     private \DateTime $startAt;
  22.     #[ORM\Column(type'datetime')]
  23.     private \DateTime $endAt;
  24.     #[ORM\Column(type'boolean')]
  25.     private bool $current false;
  26.     #[ORM\Column(type'boolean')]
  27.     private bool $opened false;
  28.     #[ORM\Column(type'string'length255nullabletrue)]
  29.     private ?string $msgRegistration null;
  30.     #[ORM\OneToOne(inversedBy'next'targetEntitySchoolyear::class, cascade: ['persist'])]
  31.     #[ORM\JoinColumn(nullabletrue)]
  32.     private ?Schoolyear $previous null;
  33.     #[ORM\OneToOne(mappedBy'previous'targetEntitySchoolyear::class)]
  34.     private ?Schoolyear $next null;
  35.     #[ORM\OneToMany(mappedBy'schoolyear'targetEntityJury::class, cascade: ['all'])]
  36.     private Collection $jurys;
  37.     public function __construct(\DateTime $date=null)
  38.     {
  39.         // date_default_timezone_set('Africa/Casablanca');
  40.         $this->createAt=new \DateTime('now');
  41.         if($date===null){
  42.             $date=new \DateTime('now');
  43.         }
  44.         $y1=$date->format('Y');
  45.         $date->modify('+1 year');
  46.         $y2=$date->format('Y');
  47.         $this->title=$y1.'/'.$y2;
  48.         $this->startAt= new \DateTime($y1.'-10-01');
  49.         $this->endAt= new \DateTime($y2.'-07-31');
  50.         $this->published=false;
  51.         $this->opened=false;
  52.         $this->current=false;
  53.         $this->jurys=new ArrayCollection();
  54.         $this->id 0;
  55.     }
  56.     public function getTitle(): string
  57.     {
  58.         return $this->title;
  59.     }
  60.     public function setTitle(string $title): Schoolyear
  61.     {
  62.         $this->title $title;
  63.         return $this;
  64.     }
  65.     public function getStartAt(): \DateTime
  66.     {
  67.         return $this->startAt;
  68.     }
  69.     public function setStartAt(\DateTime $startAt): Schoolyear
  70.     {
  71.         $this->startAt $startAt;
  72.         return $this;
  73.     }
  74.     public function getEndAt(): \DateTime
  75.     {
  76.         return $this->endAt;
  77.     }
  78.     public function setEndAt(\DateTime $endAt): Schoolyear
  79.     {
  80.         $this->endAt $endAt;
  81.         return $this;
  82.     }
  83.     public function isCurrent(): bool
  84.     {
  85.         return $this->current;
  86.     }
  87.     public function setCurrent(bool $current): Schoolyear
  88.     {
  89.         $this->current $current;
  90.         return $this;
  91.     }
  92.     public function isOpened(): bool
  93.     {
  94.         return $this->opened;
  95.     }
  96.     public function setOpened(bool $opened): Schoolyear
  97.     {
  98.         $this->opened $opened;
  99.         return $this;
  100.     }
  101.     public function getMsgRegistration(): ?string
  102.     {
  103.         return $this->msgRegistration;
  104.     }
  105.     public function setMsgRegistration(?string $msgRegistration): Schoolyear
  106.     {
  107.         $this->msgRegistration $msgRegistration;
  108.         return $this;
  109.     }
  110.     public function getPrevious(): ?Schoolyear
  111.     {
  112.         return $this->previous;
  113.     }
  114.     public function setPrevious(?Schoolyear $previous): Schoolyear
  115.     {
  116.         $this->previous $previous;
  117.         return $this;
  118.     }
  119.     public function getNext(): ?Schoolyear
  120.     {
  121.         return $this->next;
  122.     }
  123.     public function setNext(?Schoolyear $next): Schoolyear
  124.     {
  125.         $this->next $next;
  126.         return $this;
  127.     }
  128. //    public function getMonths(): Collection
  129. //    {
  130. //        return $this->months;
  131. //    }
  132. //
  133. //    public function setMonths(Collection $months): Schoolyear
  134. //    {
  135. //        $this->months = $months;
  136. //        return $this;
  137. //    }
  138.     function __toString(): string
  139.     {
  140.         return $this->title;
  141.     }
  142.     /**
  143.      * @return mixed
  144.      */
  145.     public function getCourses()
  146.     {
  147.         return $this->courses;
  148.     }
  149.     /**
  150.      * @return Schoolyear
  151.      */
  152.     public function setCourses(mixed $courses)
  153.     {
  154.         $this->courses $courses;
  155.         return $this;
  156.     }
  157.     public function getJurys(): Collection
  158.     {
  159.         return $this->jurys;
  160.     }
  161.     public function setJurys(Collection $jurys): Schoolyear
  162.     {
  163.         $this->jurys $jurys;
  164.         return $this;
  165.     }
  166.     public function addCourse(Course $course): Schoolyear {
  167.         $course->setSchoolyear($this);
  168.         $this->courses[]=$course;
  169.         return $this;
  170.     }
  171.     public function removeCourse(Course $course) {
  172.         $this->courses->removeElement($course);
  173.     }
  174.     public function addJury(Jury $jury): Schoolyear {
  175.         $jury->setSchoolyear($this);
  176.         $this->courses[]=$jury;
  177.         return $this;
  178.     }
  179.     public function removeJury(Jury $jury): void
  180.     {
  181.         $this->courses->removeElement($jury);
  182.     }
  183.     public function getButtonCurrent()
  184.     {
  185.         $confirm="data-toggle='confirmation'
  186.                 data-btn-ok-label='Oui' 
  187.                 data-btn-ok-icon='glyphicon glyphicon-ok'
  188.                 data-btn-ok-class='btn-success'
  189.                 data-btn-cancel-label='Non' 
  190.                 data-btn-cancel-icon='glyphicon glyphicon-remove'
  191.                 data-btn-cancel-class='btn-danger'
  192.                 data-title='Changer statut ?'
  193.                 data-popout='true'
  194.                 data-content=\"Voulez-vous vraiment changer l'année scolaire actuelle ? \"";
  195.         if($this->current)
  196.             $btn="<i class=\"text-green fa fa-toggle-on\"> Oui</i>";
  197.         else
  198.             $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>";
  199.         return $btn;
  200.     }
  201.     public function getButtonOpened()
  202.     {
  203.         $confirm="data-toggle='confirmation'
  204.                 data-btn-ok-label='Oui' 
  205.                 data-btn-ok-icon='glyphicon glyphicon-ok'
  206.                 data-btn-ok-class='btn-success'
  207.                 data-btn-cancel-label='Non' 
  208.                 data-btn-cancel-icon='glyphicon glyphicon-remove'
  209.                 data-btn-cancel-class='btn-danger'
  210.                 data-title=\"Ouvrir l'inscription ?\"
  211.                 data-popout='true'
  212.                 data-content=\"Voulez-vous vraiment ouvrir l'inscription pour cet année scolaire actuelle ? \"";
  213.         if($this->opened)
  214.             $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>";
  215.         else
  216.             $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>";
  217.         return $btn;
  218.     }
  219.     public function getMonths($format='m-Y'){
  220.         // date_default_timezone_set('Africa/Casablanca');
  221.         $items=[];
  222.         $date= clone $this->startAt;
  223.         $date->modify('first day of this month');
  224.         while ($date<=$this->endAt){
  225.             $month=$date->format($format);
  226.             $items[]=$month;
  227.             $date->modify('+1 month');
  228.         }
  229.         return $items;
  230.     }
  231.     public function getIndexMonthCurrent(){
  232.         // date_default_timezone_set('Africa/Casablanca');
  233.         $new=new \DateTime('now');
  234.         $month=$new->format('m-Y');
  235.         if (array_search($month$this->getMonths()))
  236.             return array_search($month$this->getMonths());
  237.         else
  238.             if ($new<$this->startAt) return -1;
  239.             else return -2;
  240.     }
  241.     public function getMonthsCurrent($monthsPassed$monthsComing$format='m-Y')
  242.     {
  243.         $months = [];
  244.         $now=new \DateTimeImmutable('now');
  245.         // obtenir la date du premier jour du mois
  246.         $firstDayOfMonth $now->modify('first day of this month');
  247.         for($i=-1*$monthsPassed$i<=$monthsComing$i++){
  248.             $m=($firstDayOfMonth->modify($i.' month'))->format($format);
  249.             if(in_array($m,$this->getMonths($format))){
  250.                 $months[] = $m;
  251.             }
  252.         }
  253.         return $months;
  254.     }
  255.     public function getMonthsPassed(){
  256.         $ind=$this->getIndexMonthCurrent();
  257.         if($ind==-2) return $this->getMonths();
  258.         elseif ($ind==-1) return [];
  259.         else{
  260.             return array_slice($this->getMonths(),0,$ind+1);
  261.         }
  262.     }
  263.     public function getFirstYear(){
  264.         return intval($this->getStartAt()->format('Y'));
  265.     }
  266.     public function getSecondYear(){
  267.         return intval($this->getEndAt()->format('Y'));
  268.     }
  269.     public function getJury($pole){
  270.         if(!empty($this->jurys)) {
  271.             foreach ($this->jurys as $item) {
  272.                 if ($item->getPole() == $pole) {
  273.                     return $item;
  274.                 }
  275.             }
  276.         }
  277.         return null;
  278.     }
  279.     public function isPassed()
  280.     {
  281.         return $this->endAt < new \DateTime('now');
  282.     }
  283.     public function isIn(\DateTime $date): bool
  284.     {
  285.         return $this->startAt <= $date && $this->endAt >= $date;
  286.     }
  287. }