src/Controller/ParticipationsController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\Routing\Annotation\Route;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use App\Helper\Query\FeeQueries;
  9. use App\Helper\Query\SeasonQueries;
  10. use App\Helper\Query\SessionQueries;
  11. use App\Helper\Query\TrainerQueries;
  12. use App\Helper\Query\ParticipationQueries;
  13. use App\Helper\Query\HouseholdQueries;
  14. use App\Helper\Query\GroupQueries;
  15. use App\Settings\Settings;
  16. use App\Helper\FormHelper;
  17. use App\Entity\Participation;
  18. use Doctrine\Persistence\ManagerRegistry;
  19. //use App\Entity\Season;
  20. class ParticipationsController extends AbstractController
  21. {
  22.     /**
  23.      * @Route("/participations", name="participations")
  24.      */
  25.     public function participationsAction()
  26.     {        
  27.         return $this->render('screens/manageParticipationsView.html.twig', [
  28.             'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
  29.             'commonData' => Settings::getCommonData()
  30.         ]);
  31.     }
  32.     
  33.     /**
  34.      * @Route("/participations/getFees", name="getFees")
  35.      */
  36.     public function getActiveFeesAction(ManagerRegistry $doctrine) {
  37.        
  38.       $em $doctrine->getManager();
  39.         
  40.        $fq = new FeeQueries($emSettings::RETURN_ARRAY);
  41.        
  42.        $fees $fq->getActiveFees();
  43.        
  44.        return new JsonResponse($fees);
  45.        
  46.     }
  47.     
  48.     /**
  49.      * @Route("/participations/getSessionDatesForSeason/{seasonId}", name="getSessionDatesForSeason")
  50.      */
  51.     function getSessionDatesForSeasonAction($seasonIdManagerRegistry $doctrine) {        
  52.         $em $doctrine->getManager();
  53.         
  54.         $sq = new SeasonQueries($emSettings::RETURN_ARRAY);
  55.         
  56.         $result $sq->getSessionDatesForSeason($seasonId);
  57.         foreach($result as $key => $thisResult) {
  58.          date_default_timezone_set ('Europe/Luxembourg');
  59.          setlocale(LC_ALL'french');
  60.          $d $thisResult['date'];
  61.          $result[$key]['dateForDisplay'] = $d->format('d/m/Y - l');         
  62.         }
  63.         return new JsonResponse($result);
  64.     }
  65.     
  66.     /**
  67.      * @Route("/participations/getGroupsForSeasonAndClubAndSessionDate", name="getGroupsForSeasonAndClubAndSessionDate")
  68.      */
  69.     function getGroupsForSeasonAndClubAndSessionDateAction(Request $requestManagerRegistry $doctrine) {
  70.       $seasonId $request->get('seasonId');
  71.       $clubId $request->get('clubId');
  72.       $sessionDate $request->get('sessionDate');
  73.       
  74.       $em $doctrine->getManager();
  75.         
  76.       $gq = new GroupQueries($emSettings::RETURN_ARRAY);
  77.             
  78.       $result $gq->getGroupsForSeasonAndClubAndSessionDate($seasonId$clubId, new \DateTime($sessionDate));  
  79.       return new JsonResponse($result);
  80.     }
  81.     
  82.     /**
  83.      * @Route("/participations/getPotentialParticipantsForGroup", name="getPotentialParticipantsForGroup")
  84.      */
  85.     function getPotentialParticipantsForGroupAction(Request $requestManagerRegistry $doctrine) {
  86.       //TODO : take into account closed participations included in a statement
  87.      
  88.       $groupId $request->get('groupId');
  89.       $sessionDateParameter $request->get('sessionDate');
  90.       
  91.       $em $doctrine->getManager();
  92.         
  93.       $sq = new SessionQueries($emSettings::RETURN_OBJECTS);
  94.       
  95.       $session $sq->getSessionForGroupAndDate($groupId, new \DateTime($sessionDateParameter));      
  96.             
  97.       //var_dump($session->toArray());      
  98.       assert(is_object($session)); 
  99.       //$sessionDate = FormHelper::mySqlDateToDateObject($session->date);
  100.       $sessionDate $session->getDate();
  101.       //var_dump($session->id);
  102.       
  103.        $gq = new GroupQueries($emSettings::RETURN_ARRAY);
  104.        
  105.       //var_dump("Session ID: ".$session->getId());
  106.       
  107.       $potentialParticipants $gq->getPotentialParticipantsForGroup($groupId,$session->getId());      
  108.       //var_dump($potentialParticipants[0]);
  109.       /*      
  110.       $pq = new ParticipationQueries($em, Settings::RETURN_ARRAY);
  111.       
  112.       $participations = $pq->getParticipationsForSession($session->getId());
  113.      
  114.       var_dump($participations);             
  115.       */
  116.       //var_dump($fee_ids);
  117.       
  118.       //die();
  119.       $result = array();
  120.       foreach($potentialParticipants as $key => $tp) {
  121.         //var_dump($key);
  122.         $tpOutput $tp[Settings::PARTICIPANT];
  123.         if ($tp[1]!==null) {          
  124.           //There is a participation
  125.           
  126.           $tpOutput['present'] = 1;
  127.           $participationData explode("_",$tp[1]);
  128.           if ($participationData[Settings::CLOSED] != /*|| $tpOutput['closed'] == 1*/) {
  129.             $tpOutput['closed'] = 1;
  130.           } else {
  131.             $tpOutput['closed'] = 0;
  132.           }
  133.           
  134.           //$potentialParticipants[$key]['closed'] = $participations[$key]['closedCount'] || $participations[$key]['active'];
  135.           
  136.           if($participationData[Settings::FEE]==0) {
  137.             $tpOutput['free'] = 1;
  138.           } else {
  139.             $tpOutput['free'] = 0;
  140.           }
  141.           
  142.           $result[] = $tpOutput;
  143.           
  144.         } else {
  145.           //No participation yet          
  146.           $regBeginDate $tpOutput['begindate'];
  147.           $regEndDate $tpOutput['enddate'];
  148.           //if(!is_null($regBeginDate)) $regBeginDate = formHelper::mySqlDateToDateObject($regBeginDate);
  149.           //if(!is_null($regEndDate)) $regEndDate = formHelper::mySqlDateToDateObject($regEndDate);
  150.           //var_dump($regBeginDate);
  151.           //var_dump($regEndDate);
  152.           if ((!is_null($regEndDate)) && ($sessionDate $regBeginDate || $sessionDate $regEndDate)) {
  153.             //Don't display inactive registrations
  154.             //unset($potentialParticipants[$key]);
  155.           } else {
  156.             //Registration was active at that time, show it
  157.             $tpOutput['present'] = 0;
  158.             $tpOutput['free'] = 0;
  159.             $tpOutput['closed'] = 0;
  160.             $result[] = $tpOutput;
  161.           }
  162.         }   
  163.       }
  164.       
  165.       //var_dump($potentialParticipants);
  166.       //var_dump($participations);
  167.             
  168.       //var_dump($potentialParticipants); 
  169.       
  170.       //var_dump(count($result));
  171.       
  172.       return new JsonResponse($result);
  173.     }
  174.     
  175.     /**
  176.      * @Route("/participations/getTrainersOfSession", name="getTrainersOfSession")
  177.      */
  178.     function getTrainersOfSessionAction(Request $requestManagerRegistry $doctrine) {
  179.       $groupId $request->get('groupId');
  180.       $sessionDate $request->get('sessionDate');
  181.       
  182.       $em $doctrine->getManager();
  183.       
  184.       $sq = new SessionQueries($em,Settings::RETURN_OBJECTS);
  185.       
  186.       $session $sq->getSessionForGroupAndDate($groupId, new \DateTime($sessionDate));      
  187.       //var_dump($session->toArray());      
  188.       assert(is_object($session));
  189.       //var_dump($session);
  190.       
  191.       $tq = new TrainerQueries($emSettings::RETURN_ARRAY);
  192.       
  193.       //var_dump($session->getId());
  194.       
  195.       $trainers $tq->getTrainersOfSession($session->getId());
  196.       return new JsonResponse($trainers);
  197.     }
  198.     
  199.     /**
  200.      * @Route("/participations/getActiveTrainersByName", name="getActiveTrainersByName")
  201.      */
  202.     function getActiveTrainersByNameAction(ManagerRegistry $doctrine) {
  203.     $em $doctrine->getManager();
  204.     $tq = new TrainerQueries($emSettings::RETURN_ARRAY);
  205.     
  206.     $trainers $tq->getActiveTrainersByName();   
  207.     return new JsonResponse($trainers);
  208.   }
  209.   
  210.   /**
  211.      * @Route("/participations/saveParticipations", name="saveParticipations")
  212.      */
  213.   function saveParticipationsAction(Request $requestManagerRegistry $doctrine) {
  214.       define('REGULAR_FEE_ID',1);
  215.       define('REDUCED_FEE_ID',2);
  216.       define('FREE_FEE_ID',3);
  217.       
  218.       $em $doctrine->getManager();
  219.       
  220.       $regularFee $em->find('App:Fee'REGULAR_FEE_ID);
  221.       $reducedFee $em->find('App:Fee'REDUCED_FEE_ID);
  222.       $freeFee $em->find('App:Fee'FREE_FEE_ID);
  223.       
  224.       $groupId $request->get('groupId');
  225.       $sessionDate $request->get('sessionDate');
  226.       $pda $request->get('pda');
  227.       $trainers $request->get('trainers');
  228.       
  229.       $sq = new SessionQueries($emSettings::RETURN_OBJECTS);
  230.                   
  231.       $session $sq->getSessionForGroupAndDate($groupId$sessionDate);
  232.       assert(!is_null($session));
  233.       if(is_null($trainers)) {
  234.         $trainers=array();
  235.       }
  236.       
  237.       //var_dump($parameters['pda']);
  238.       
  239.       foreach($pda as $index => $participationData) {
  240.         $thisPersonId $participationData[0];
  241.         $presentFlag $participationData[1];
  242.         $freeFlag $participationData[2];
  243.                 
  244.         if(is_numeric($thisPersonId) && is_numeric($presentFlag) && is_numeric($freeFlag)) {
  245.           //var_dump($thisPersonId);
  246.           //var_dump($session->id);
  247.           //var_dump(Person::hasHouseholdOfPersonReducedFee($thisPersonId));
  248.           
  249.           $pq = new ParticipationQueries($emSettings::RETURN_OBJECTS);
  250.           $hq = new HouseholdQueries($emSettings::RETURN_OBJECTS);
  251.           
  252.           $thisParticipation $pq->getParticipationForSessionAndPerson($session->getId(), $thisPersonId);
  253.           if(is_array($thisParticipation)) {
  254.             //there is a participation
  255.             //var_dump($thisParticipation->toArray());            
  256.               if ($thisParticipation['closedCount'] == 0) {
  257.                 if ($presentFlag==0) {
  258.                   //the participation needs to be deleted
  259.                   $em->remove($thisParticipation[0]);
  260.                 } else {
  261.                   //update the feeID
  262.                   if ($freeFlag==1) {                    
  263.                     $thisParticipation[0]->setFee($freeFee);                
  264.                   } else {
  265.                   if ($hq->hasHouseholdOfPersonReducedFee($thisPersonId)) {                   
  266.                     $thisParticipation[0]->setFee($reducedFee); 
  267.                   }
  268.                   else {                   
  269.                     $thisParticipation[0]->setFee($regularFee); 
  270.                   }
  271.                   }
  272.                   //var_dump($thisParticipation->toArray());
  273.                   $em->persist($thisParticipation[0]);
  274.                   $em->flush();
  275.                 }
  276.               }
  277.           } else {
  278.             //there is no participation yet
  279.             if ($presentFlag==1) {
  280.               $thisParticipation = new Participation();
  281.               $thisParticipation->setSession($session);
  282.               $thisPerson $em->find('App:Person'$thisPersonId);
  283.               $thisParticipation->setPerson($thisPerson);
  284.               if ($freeFlag==1) {
  285.                 $thisParticipation->setFee($freeFee);               
  286.               } else {
  287.                 
  288.                 if ($hq->hasHouseholdOfPersonReducedFee($thisPersonId))
  289.                 $thisParticipation->setFee($reducedFee);  
  290.               else
  291.                 $thisParticipation->setFee($regularFee);  
  292.               }
  293.               $em->persist($thisParticipation);
  294.               $em->flush();
  295.             }
  296.           }
  297.         }        
  298.       }
  299.       $this->saveTrainers($session$trainers$doctrine);
  300.       return new JsonResponse('Finished');    
  301.       
  302.     }
  303.     
  304.     function saveTrainers($session$trainersListManagerRegistry $doctrine) {
  305.       $em $doctrine->getManager();
  306.       //$session = $em->find('App:Session', $sessionId);
  307.       assert(is_object($session));
  308.       
  309.       foreach($session->getTrainer() as $thisTrainer) {
  310.         $thisTrainer->getSession()->removeElement($session);
  311.         //foreach($thisTrainer->getSession() as $thisSession ) {
  312.         //  var_dump($thisSession->getId());
  313.        // }
  314.       }
  315.       $session->getTrainer()->clear();
  316.       $em->flush();
  317.       foreach($trainersList as $thisTrainerId) {
  318.         $trainer $em->find('App:Trainer'$thisTrainerId);
  319.         $session->getTrainer()->add($trainer);
  320.         $trainer->getSession()->add($session);
  321.            
  322.       }
  323.          $em->flush();
  324.     }
  325. }