src/Repository/AgendaElementRepository.php line 150

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\AgendaElement;
  4. use App\Entity\School;
  5. use App\Entity\SchoolYear;
  6. use App\Entity\User;
  7. use App\Entity\Subject;
  8. use App\Entity\TheClass;
  9. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. /**
  12.  * @extends ServiceEntityRepository<AgendaElement>
  13.  *
  14.  * @method AgendaElement|null find($id, $lockMode = null, $lockVersion = null)
  15.  * @method AgendaElement|null findOneBy(array $criteria, array $orderBy = null)
  16.  * @method AgendaElement[]    findAll()
  17.  * @method AgendaElement[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  18.  */
  19. class AgendaElementRepository extends ServiceEntityRepository
  20. {
  21.     public function __construct(ManagerRegistry $registry)
  22.     {
  23.         parent::__construct($registryAgendaElement::class);
  24.     }
  25.     public function findNearStartAtByClass(
  26.         TheClass $classe,
  27.         School $school,
  28.     ): ?AgendaElement {
  29.         // 1. On centralise la Timezone pour éviter les décalages
  30.         $tz = new \DateTimeZone('Africa/Douala');
  31.         $now = new \DateTime('now'$tz);
  32.         
  33.         // 2. Préparation des bornes (uniquement au format H:i:s pour SQL TIME)
  34.         // On cherche un cours dont la période [startAt, endAt] couvre l'heure actuelle
  35.         $currentTime $now->format('H:i:s');
  36.         
  37.         // Si tu veux vraiment garder la marge de 5min pour "anticiper" ou "prolonger" :
  38.         $startPlus5 = (clone $now)->modify('+5 minutes')->format('H:i:s');
  39.         $startMinus5 = (clone $now)->modify('-5 minutes')->format('H:i:s');
  40.         // 3. Récupération du nom du jour en anglais (Monday, Tuesday...)
  41.         $dayName $now->format('l');
  42.         return $this->createQueryBuilder('ae')
  43.             ->leftJoin('ae.agendaDay''d')
  44.             // Logique : l'heure actuelle (+/- 5min) doit être comprise entre le début et la fin du cours
  45.             ->andWhere('TIME(ae.startAt) <= :plus5')
  46.             ->andWhere('TIME(ae.endAt) >= :minus5')
  47.             ->andWhere('ae.classe = :classe')
  48.             ->andWhere('ae.school = :school')
  49.             ->andWhere('ae.active = :active')
  50.             ->andWhere('d.nameEn = :dayName')
  51.             ->setParameters([
  52.                 'plus5'   => $startPlus5,
  53.                 'minus5'  => $startMinus5,
  54.                 'classe'  => $classe,
  55.                 'school'  => $school,
  56.                 'active'  => true,
  57.                 'dayName' => $dayName
  58.             ])
  59.             ->setMaxResults(1)
  60.             ->getQuery()
  61.             ->getOneOrNullResult();
  62.     }
  63.     public function findNearStartAtByTeacherAndByClass(
  64.         User $prof,
  65.         TheClass $classe,
  66.         School $school,
  67.     ): ?AgendaElement {
  68.         // 1. On fixe la Timezone pour tout le calcul
  69.         $tz = new \DateTimeZone('Africa/Douala');
  70.         $now = new \DateTime('now'$tz);
  71.         // 2. On récupère le nom du jour selon la Timezone de Douala
  72.         $dayName $now->format('l');
  73.         // 3. On extrait uniquement l'heure pour la comparaison SQL TIME
  74.         // On utilise des chaînes de caractères "H:i:s" pour éviter les conflits de dates (1970 vs 2024)
  75.         $startPlus5 = (clone $now)->modify('+5 minutes')->format('H:i:s');
  76.         $startMinus5 = (clone $now)->modify('-5 minutes')->format('H:i:s');
  77.         return $this->createQueryBuilder('ae')
  78.             ->leftJoin('ae.subject''s')
  79.             ->leftJoin('ae.agendaDay''d')
  80.             ->andWhere('s.prof = :prof')
  81.             ->andWhere('ae.school = :school')
  82.             ->andWhere('ae.classe = :classe')
  83.             ->andWhere('d.nameEn = :dayName')
  84.             // On utilise la fonction SQL TIME() pour ignorer la partie Date des colonnes
  85.             ->andWhere('TIME(ae.startAt) <= :startPlus5')
  86.             ->andWhere('TIME(ae.endAt) >= :startMinus5')
  87.             ->andWhere('ae.active = :active')
  88.             ->setParameters([
  89.                 'prof' => $prof,
  90.                 'school' => $school,
  91.                 'classe' => $classe,
  92.                 'dayName' => $dayName,
  93.                 'startPlus5' => $startPlus5,
  94.                 'startMinus5' => $startMinus5,
  95.                 'active' => true,
  96.             ])
  97.             ->setMaxResults(1)
  98.             ->getQuery()
  99.             ->getOneOrNullResult();
  100.     }
  101.     public function findNearStartAtByTeacher(
  102.         User $prof,
  103.         School $school,
  104.     ): ?array {
  105.         // 1. On fixe la Timezone pour tout le calcul
  106.         $tz = new \DateTimeZone('Africa/Douala');
  107.         $now = new \DateTime('now'$tz);
  108.         // 2. On récupère le nom du jour selon la Timezone de Douala
  109.         $dayName $now->format('l');
  110.         // 3. On extrait uniquement l'heure pour la comparaison SQL TIME
  111.         // On utilise des chaînes de caractères "H:i:s" pour éviter les conflits de dates (1970 vs 2024)
  112.         $startPlus5 = (clone $now)->modify('+5 minutes')->format('H:i:s');
  113.         $startMinus5 = (clone $now)->modify('-5 minutes')->format('H:i:s');
  114.         return $this->createQueryBuilder('ae')
  115.             ->leftJoin('ae.subject''s')
  116.             ->leftJoin('ae.agendaDay''d')
  117.             ->andWhere('s.prof = :prof')
  118.             ->andWhere('ae.school = :school')
  119.             ->andWhere('d.nameEn = :dayName')
  120.             // On utilise la fonction SQL TIME() pour ignorer la partie Date des colonnes
  121.             ->andWhere('TIME(ae.startAt) <= :startPlus5')
  122.             ->andWhere('TIME(ae.endAt) >= :startMinus5')
  123.             ->andWhere('ae.active = :active')
  124.             ->setParameters([
  125.                 'prof' => $prof,
  126.                 'school' => $school,
  127.                 'dayName' => $dayName,
  128.                 'startPlus5' => $startPlus5,
  129.                 'startMinus5' => $startMinus5,
  130.                 'active' => true,
  131.             ])
  132.             ->getQuery()
  133.             ->getResult();
  134.     }
  135.     public function findByIdAndByDayAndBySubjectAndBySartAtAndByEndAt($elementId null$dayOfWeek$subjectId null$startAt$endAt)
  136.     {
  137.         $qb $this->createQueryBuilder('a')
  138.             ->join('a.agendaDay''d')
  139.             ->where('LOWER(d.nameEn) = :day')
  140.             ->andWhere('a.startAt = :startAt'// Suppression de l'espace après :
  141.             ->andWhere('a.endAt = :endAt')     // Utilisation de andWhere
  142.             ->setParameter('day'strtolower($dayOfWeek))
  143.             ->setParameter('startAt'$startAt instanceof \DateTimeInterface $startAt->format('H:i:s') : $startAt)
  144.             ->setParameter('endAt'$endAt instanceof \DateTimeInterface $endAt->format('H:i:s') : $endAt);
  145.         if (!empty($elementId)) {
  146.             $qb->andWhere('a.id = :id')
  147.             ->setParameter('id'$elementId);
  148.         }
  149.         if (!empty($subjectId)) {
  150.             $qb->join('a.subject''s')
  151.             ->andWhere('s.id = :subjectId')
  152.             ->setParameter('subjectId'$subjectId);
  153.         } else {
  154.             $qb->andWhere('a.subject IS NULL');
  155.         }
  156.         // IMPORTANT : On affecte le résultat à la variable
  157.         return $qb->getQuery()->getOneOrNullResult();
  158.     }
  159. //    /**
  160. //     * @return AgendaElement[] Returns an array of AgendaElement objects
  161. //     */
  162. //    public function findByExampleField($value): array
  163. //    {
  164. //        return $this->createQueryBuilder('a')
  165. //            ->andWhere('a.exampleField = :val')
  166. //            ->setParameter('val', $value)
  167. //            ->orderBy('a.id', 'ASC')
  168. //            ->setMaxResults(10)
  169. //            ->getQuery()
  170. //            ->getResult()
  171. //        ;
  172. //    }
  173. //    public function findOneBySomeField($value): ?AgendaElement
  174. //    {
  175. //        return $this->createQueryBuilder('a')
  176. //            ->andWhere('a.exampleField = :val')
  177. //            ->setParameter('val', $value)
  178. //            ->getQuery()
  179. //            ->getOneOrNullResult()
  180. //        ;
  181. //    }
  182. }