src/Controller/StatisticsController.php line 196

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  5. use App\Service\SchoolYearService;
  6. use App\Repository\ClassRoomRepository;
  7. use App\Repository\StudentRepository;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Knp\Component\Pager\PaginatorInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use App\Entity\ClassRoom;
  16. use App\Entity\SchoolYear;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Knp\Snappy\Pdf;
  19. /**
  20.  * Sequence controller.
  21.  *
  22.  * @Route("/admin/stats")
  23.  */
  24. class StatisticsController extends AbstractController
  25. {
  26.     private SchoolYearService $schoolYearService;
  27.     private ClassRoomRepository $repo;
  28.     private StudentRepository $stdRepo;
  29.     private $em;
  30.     public function __construct(EntityManagerInterface $em,StudentRepository $stdRepo,ClassRoomRepository $repo,SchoolYearService $schoolYearService)
  31.     {
  32.         $this->em $em;
  33.         $this->repo $repo;
  34.         $this->schoolYearService $schoolYearService;
  35.         $this->stdRepo $stdRepo;
  36.     }
  37.        /**
  38.      * Displays a graph updated.
  39.      *
  40.      * @Route("/update", name="admin_graph_update",  options = { "expose" = true })
  41.      * @Method("GET")
  42.      * @Template()
  43.      */
  44.     public function updateGraphs(Request $request): JsonResponse
  45.     {
  46.           // URL de redirection et paramètres
  47.         $url $this->generateUrl('admin_statistics', ['id' => intval($request->query->get('id'))]);
  48.         return new JsonResponse(['url' => $url]);
  49.     }
  50.       /**
  51.      * Displays a pdf of students grouping by gender.
  52.      *
  53.      * @Route("printgr/{id}", name="admin_stat_print_gender_room", defaults={"id"=0}  )
  54.      * @Method("GET")
  55.      * @Template()
  56.      */
  57.     public function genderRoomPdfPdf $pdfint $id=0): Response
  58.     {
  59.         $year $this->schoolYearService->sessionYearById();
  60.         $rooms $this->repo->findAll();
  61.         if($id 0){
  62.             $rooms $this->repo->findBy(array("id" => $id));
  63.             $this->viewGender($id);
  64.         } else {
  65.             $this->viewGender();
  66.         }
  67.         $connection $this->em->getConnection();
  68.         $gender_datas $connection->executeQuery("SELECT *  FROM V_GENDER_ROOM ")->fetchAll();
  69.         $html $this->render('statistics/pdf/gender_room.html.twig', [
  70.             "rooms"=>$rooms
  71.             'year' => $year,
  72.             "gender_datas"=>$gender_datas
  73.         ]);
  74.         return new Response(
  75.             $pdf->getOutputFromHtml($html),
  76.             200,
  77.             array(
  78.                 'Content-Type'          => 'application/pdf',
  79.                 'Content-Disposition'   => 'inline; filename="stat_gender_room' .( count($rooms)==?  $rooms[0]->getName():"") . '.pdf"'
  80.             )
  81.         );
  82.        
  83.     }
  84.     /**
  85.      * Displays a pdf of students grouping by gender.
  86.      *
  87.      * @Route("printagr/{id}", name="admin_stat_print_age_room", defaults={"id"=0}  )
  88.      * @Method("GET")
  89.      * @Template()
  90.      */
  91.     public function ageGroupRoomPdfPdf $pdfint $id=0): Response
  92.     {
  93.         $year $this->schoolYearService->sessionYearById();
  94.         $rooms $this->repo->findAll();
  95.         if($id 0){
  96.             $rooms $this->repo->findBy(array("id" => $id));
  97.             $this->viewAgeGroup($id);
  98.         } else {
  99.             $this->viewAgeGroup();
  100.         }
  101.         $connection $this->em->getConnection();
  102.         $age_group_datas $connection->executeQuery("SELECT *  FROM V_AGE_GROUP_ROOM ")->fetchAll();
  103.       
  104.         $html $this->render('statistics/pdf/age_group_room.html.twig', [
  105.             "rooms"=>$rooms
  106.             'year' => $year,
  107.             "age_group_datas"=>$age_group_datas
  108.         ]);
  109.         return new Response(
  110.             $pdf->getOutputFromHtml($html),
  111.             200,
  112.             array(
  113.                 'Content-Type'          => 'application/pdf',
  114.                 'Content-Disposition'   => 'inline; filename="stat_gender_room' .( count($rooms)==?  $rooms[0]->getName():"") . '.pdf"'
  115.             )
  116.         );
  117.     }
  118.     /**
  119.      * Displays a pdf of students grouping by gender.
  120.      *
  121.      * @Route("printagrgen/{id}", name="admin_stat_print_age_room_gender", defaults={"id"=0}  )
  122.      * @Method("GET")
  123.      * @Template()
  124.      */
  125.     public function ageGroupGenderRoomPdfPdf $pdfint $id=0): Response
  126.     {
  127.         $year $this->schoolYearService->sessionYearById();
  128.         $rooms $this->repo->findAll();
  129.         $connection $this->em->getConnection();
  130.         if($id 0){
  131.             $rooms $this->repo->findBy(array("id" => $id));
  132.             $this->viewGenderAgeGroup($id);
  133.             $age_group_gender_datas $connection->executeQuery("SELECT *  FROM V_AGE_GROUP_GENDER_ROOM ")->fetchAll();
  134.         
  135.         } else {
  136.             $rooms $this->repo->findAll();
  137.             foreach($rooms as $key=>$room){
  138.                 $this->viewGenderAgeGroup($room->getId());
  139.                 $age_group_gender_datas[$room->getId()] = $connection->executeQuery("SELECT *  FROM V_AGE_GROUP_GENDER_ROOM ")->fetchAll();
  140.             }
  141.             foreach ($rooms as $room) {
  142.                 $roomNames[$room->getId()] = $room->getName();
  143.             }
  144.            
  145.            // dd($age_group_gender_datas);
  146.         }
  147.         
  148.       
  149.         $html $this->render($id 'statistics/pdf/age_group_gender_room.html.twig' :  'statistics/pdf/age_group_gender_room_wide.html.twig'  , [
  150.             "rooms"=>$rooms
  151.             "roomNames"=>$roomNames,
  152.             'year' => $year,
  153.             "minAge" => $this->findMinMaxAge()[0]["minAge"],
  154.             "maxAge" => $this->findMinMaxAge()[0]["maxAge"],
  155.             "age_group_gender_datas"=>$age_group_gender_datas
  156.         ]);
  157.         $options = [
  158.             'orientation' => 'landscape',
  159.             'margin-top' => 0,
  160.             'margin-right' => 0,
  161.             'margin-bottom' => 0,
  162.             'margin-left' => 0,
  163.         ];
  164.         return new Response(
  165.             $pdf->getOutputFromHtml($html$options),
  166.             200,
  167.             array(
  168.                 'Content-Type'          => 'application/pdf',
  169.                 'Content-Disposition'   => 'inline; filename="stat_gender_agegrp_room' .( count($rooms)==?  $rooms[0]->getName():"") . '.pdf"'
  170.             )
  171.         );
  172.        
  173.     }
  174.     /**
  175.      * Lists all Sequenceme entities.
  176.      *
  177.      * @Route("/{id}", name="admin_statistics", defaults={"id"=0})
  178.      * @Method("GET")
  179.      * @Template()
  180.      */
  181.     public function indexAction(Request $request,int $id=)
  182.     {
  183.         $rooms $this->repo->findAll();
  184.         $connection $this->em->getConnection();
  185.         // Extration des donnees de la BD
  186.         if($id == 0){
  187.             $this->viewGender();
  188.             $this->viewAgeGroup();
  189.             $this->viewGenderAgeGroup();
  190.         } else {
  191.             $this->viewGender($id);
  192.             $this->viewAgeGroup($id);
  193.             $this->viewGenderAgeGroup($id);
  194.         }  
  195.         $gender_datas $connection->executeQuery("SELECT *  FROM V_GENDER_ROOM ")->fetchAll();
  196.         $age_group_datas $connection->executeQuery("SELECT *  FROM V_AGE_GROUP_ROOM ")->fetchAll();
  197.         $age_group_gender_datas $connection->executeQuery("SELECT *  FROM V_AGE_GROUP_GENDER_ROOM ")->fetchAll();
  198.          // Traitements de donnees pour les graphes de repartition de sexe par classe
  199.         foreach ($rooms as $room) {
  200.             $roomNames[] = $room->getName();
  201.         }
  202.         $masculin = [];
  203.         $feminin = [];
  204.        
  205.         foreach ($roomNames as $name) {
  206.             foreach($gender_datas as $data){
  207.                 if(strcmp($data["room"], $name)==0  && strcmp($data["gender"], "0")==0){
  208.                     array_push($masculin $data["workforce"]);
  209.                 }
  210.                 if(strcmp($data["room"], $name)==0  && strcmp($data["gender"], "1")==0){
  211.                     array_push($feminin $data["workforce"]);
  212.                 }
  213.                 continue;
  214.             }
  215.         }
  216.         // Traitement des donnees du graphes des groupes d'ages
  217.         $age_groups_weight= [];
  218.         $age_groups_label= [];
  219.         $previousKey null;
  220.         foreach ($age_group_datas as $key=>$group) {
  221.             array_push($age_groups_weight $group["effectif"]);
  222.             if ($previousKey == null) {
  223.                 array_push($age_groups_label "0_".$group["tranche_age"]);
  224.             } else {
  225.                 array_push($age_groups_label $age_group_datas[$previousKey]["tranche_age"]."_".$group["tranche_age"]);
  226.             }
  227.             $previousKey $key;
  228.         }
  229.         // Encodage Json
  230.         $roomNames json_encode($roomNames);
  231.         if($id 0){
  232.             $roomNames json_encode($this->repo->findOneById($id)->getName());
  233.         }
  234.         $ageGroupsWeight json_encode($age_groups_weight);
  235.         $ageGroupsLabel json_encode($age_groups_label);
  236.        
  237.         $scatterData $age_group_gender_datas;
  238.        
  239.         
  240.         return $this->render('statistics/dashboard.html.twig', [
  241.             "rooms"=>$rooms
  242.             "feminin"=>json_encode($feminin),
  243.             "masculin"=> json_encode($masculin), 
  244.             "roomNames"=>$roomNames,
  245.             "ageGroupsLabel"=>$ageGroupsLabel,
  246.             "ageGroupsWeight"=>$ageGroupsWeight,
  247.             'scatterData' => json_encode($scatterData), 
  248.         ]);
  249.     }
  250.   // Cette fonction genere les vue d'effectif par tranche d'age par classe
  251.         public function viewAgeGroup(int $room=0){
  252.             $year $this->schoolYearService->sessionYearById();
  253.             $connection $this->em->getConnection();
  254.             if($room>0){
  255.                 $statement $connection->prepare(
  256.                     " CREATE OR REPLACE VIEW V_AGE_GROUP_ROOM  AS
  257.                         SELECT
  258.                             FLOOR(DATEDIFF(NOW(), birthday) / 365)  AS tranche_age,
  259.                             COUNT(*) AS effectif
  260.                         FROM  student    std  
  261.                         JOIN  subscription sub    ON  sub.student_id      =   std.id     
  262.                         JOIN  class_room room    ON  sub.class_room_id     =   room.id
  263.                         WHERE sub.school_year_id =? AND  room.id = ?
  264.                         GROUP BY
  265.                              tranche_age
  266.                         ORDER BY
  267.                             tranche_age;
  268.                     "
  269.                 );
  270.                 $statement->bindValue(2$room);
  271.             } else {
  272.                 $statement $connection->prepare(
  273.                     " CREATE OR REPLACE VIEW V_AGE_GROUP_ROOM  AS
  274.                     SELECT
  275.                         FLOOR(DATEDIFF(NOW(), birthday) / 365 )  AS tranche_age,
  276.                         COUNT(*) AS effectif
  277.                     FROM  student    std  
  278.                     JOIN  subscription sub    ON  sub.student_id      =   std.id     
  279.                     JOIN  class_room room    ON  sub.class_room_id     =   room.id
  280.                     WHERE sub.school_year_id =? 
  281.                     GROUP BY
  282.                          tranche_age
  283.                     ORDER BY
  284.                         tranche_age;
  285.                     "
  286.                 );
  287.             }
  288.             $statement->bindValue(1$year->getId());
  289.             $statement->execute();
  290.         }
  291. // Cette fonction genere les vues d'effectif par sexe par classe
  292.         public function viewGender(int $room=0){
  293.             $year $this->schoolYearService->sessionYearById();
  294.             $connection $this->em->getConnection();
  295.             if($room>0){
  296.                 $statement $connection->prepare(
  297.                     " CREATE OR REPLACE VIEW V_GENDER_ROOM  AS
  298.                     SELECT   room.name as room ,  COUNT(std.id) as workforce,  std.gender as gender
  299.                     FROM  student    std  
  300.                     JOIN  subscription sub    ON  sub.student_id      =   std.id     
  301.                     JOIN  class_room room    ON  sub.class_room_id     =   room.id
  302.                     WHERE sub.school_year_id =? AND  room.id = ? 
  303.                     GROUP BY   gender;    "
  304.                 );
  305.                 $statement->bindValue(2$room);
  306.             } else {
  307.                 $statement $connection->prepare(
  308.                     " CREATE OR REPLACE VIEW V_GENDER_ROOM  AS
  309.                     SELECT   room.name as room , COUNT(std.id) as workforce,  std.gender as gender
  310.                     FROM  student    std  
  311.                     JOIN  subscription sub    ON  sub.student_id      =   std.id   
  312.                     JOIN  class_room room    ON  sub.class_room_id     =   room.id  
  313.                     WHERE sub.school_year_id =? 
  314.                     GROUP BY  room, gender;    "
  315.                 );
  316.             }
  317.             $statement->bindValue(1$year->getId());
  318.             $statement->execute();
  319.         }
  320.         // Cette fonction genere les vues d'effectif par sexe par classe
  321.         public function viewGenderAgeGroup(int $room=0){
  322.             $year $this->schoolYearService->sessionYearById();
  323.             $connection $this->em->getConnection();
  324.             if($room>0){
  325.                 $statement $connection->prepare(
  326.                     " CREATE OR REPLACE VIEW V_AGE_GROUP_GENDER_ROOM  AS
  327.                         SELECT
  328.                             FLOOR(DATEDIFF(NOW(), birthday) / 365 )  AS age, std.gender as sexe,
  329.                             COUNT(std.id) AS poids
  330.                         FROM  student    std  
  331.                         JOIN  subscription sub    ON  sub.student_id      =   std.id     
  332.                         JOIN  class_room room    ON  sub.class_room_id     =   room.id
  333.                         WHERE sub.school_year_id =? AND  room.id = ?
  334.                         GROUP BY
  335.                                 std.gender, age
  336.                         ORDER BY
  337.                             age;
  338.                     "
  339.                 );
  340.                 $statement->bindValue(2$room);
  341.             } else {
  342.                 $statement $connection->prepare(
  343.                     " CREATE OR REPLACE VIEW V_AGE_GROUP_GENDER_ROOM  AS
  344.                     SELECT
  345.                         FLOOR(DATEDIFF(NOW(), birthday) / 365 )  AS age, std.gender as sexe,
  346.                         COUNT(std.id) AS poids
  347.                     FROM  student    std  
  348.                     JOIN  subscription sub    ON  sub.student_id      =   std.id     
  349.                     JOIN  class_room room    ON  sub.class_room_id     =   room.id
  350.                     WHERE sub.school_year_id =? 
  351.                     GROUP BY
  352.                          std.gender, age
  353.                     ORDER BY
  354.                         age;
  355.                     "
  356.                 );
  357.             }
  358.             $statement->bindValue(1$year->getId());
  359.             $statement->execute();
  360.         }
  361.         public function findMinMaxAge()
  362.         {
  363.             $connection $this->em->getConnection();
  364.             $year $this->schoolYearService->sessionYearById();
  365.             $query " SELECT MIN(YEAR(NOW()) - YEAR(std.birthday)-1) AS minAge, 
  366.                         MAX(YEAR(NOW()) - YEAR(std.birthday)) AS maxAge 
  367.                   FROM student std
  368.                   JOIN  subscription sub    ON  sub.student_id      =   std.id  
  369.                   WHERE sub.school_year_id = :year   
  370.                 ";
  371.            $parameters = ['year' => $year->getId()];
  372.            $statement $connection->prepare($query);
  373.            $result =  $statement->executeQuery($parameters)->fetchAll();
  374.            return $result;
  375.         }
  376.         public function numberPerClass(){
  377.             $connection $this->em->getConnection();
  378.             $year $this->schoolYearService->sessionYearById();
  379.             $query " SELECT COUNT(std.id) AS poids , sub.class_room_id AS room
  380.                 FROM student std
  381.                 JOIN  subscription sub    ON  sub.student_id      =   std.id  
  382.                 WHERE sub.school_year_id = :year
  383.                 GROUP BY ( sub.class_room_id)  ";
  384.            $parameters = ['year' => $year->getId()];
  385.            $statement $connection->prepare($query);
  386.            $result =  $statement->executeQuery($parameters)->fetchAll();
  387.            return $result;
  388.         }
  389. }