src/Controller/MainController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Comment;
  4. use App\Entity\Criterion;
  5. use App\Entity\Jury;
  6. use App\Entity\MalusList;
  7. use App\Entity\Result;
  8. use App\Entity\Team;
  9. use App\Entity\Theme;
  10. use App\Repository\CriterionRepository;
  11. use App\Repository\MalusListRepository;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\Security\Core\Security;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. class MainController extends AbstractController
  21. {
  22.     /**
  23.      * @Route("/", name="app_home")
  24.      */
  25.     public function index(ManagerRegistry $doctrineSecurity $security): Response
  26.     {
  27.         if ($security->getUser()) {
  28.             $userRoles $security->getUser()->getRoles();
  29.             if (in_array("ROLE_SUPER_ADMIN"$userRoles) || in_array("ROLE_ADMIN"$userRoles)) {
  30.                 return $this->redirectToRoute("app_admin");
  31.             }
  32.             $allTeams $security->getUser()->getTeam();
  33.             foreach ($allTeams as $team) {
  34.                 if ($team->isEnabled()) {
  35.                     return $this->redirectToRoute("app_notation_team", array(
  36.                         "id" => $team->getId()
  37.                     ));
  38.                 }
  39.             }
  40.             return $this->render('main/error.html.twig', array(
  41.                 'message' => "Il n'y a aucune équipe existante.",
  42.                 'concours' => $this->getParameter('app.concours')
  43.             ));
  44.         }
  45.         return $this->redirectToRoute("app_login");
  46.     }
  47.     /**
  48.      * @Route("/team/{id}", name="app_notation_team")
  49.      */
  50.     public function notationAction(ManagerRegistry     $doctrine,
  51.                                    Security            $security,
  52.                                                        $id,
  53.                                    MalusListRepository $malusListRepository,
  54.                                    CriterionRepository $criterionRepository,
  55.                                    TranslatorInterface    $translator,
  56.                                    string $projectDir): Response
  57.     {
  58.         if ($security->getUser()->getLang()) {
  59.             $translator->setLocale($security->getUser()->getLang());
  60.         }
  61.         $allThemes $security->getUser()->getTheme();
  62.         $ts = array();
  63.         foreach ($allThemes as $alt) {
  64.             $ts[$alt->getSort()] = $alt;
  65.         }
  66.         ksort($ts);
  67.         $malusSort = array();
  68.         $themes = array();
  69.         $criteriaSort = array();
  70.         foreach ($ts as $theme) {
  71.             if ($theme->isEnabled()) {
  72.                 $criteriaSortByTheme $criterionRepository->findCriterionByThemeNotation($theme);
  73.                 if (count($criteriaSortByTheme) != 0) {
  74.                     $themes[] = $theme;
  75.                 }
  76.                 foreach ($criteriaSortByTheme as $criterion) {
  77.                     if ($criterion->isBonus()) {
  78.                         if ($security->getUser()->isPresident()) {
  79.                             $criteriaSort[$theme->getId()][] = $criterion;
  80.                         }
  81.                     } else {
  82.                         $criteriaSort[$theme->getId()][] = $criterion;
  83.                     }
  84.                 }
  85.                 foreach ($criteriaSortByTheme as $criterion) {
  86.                     if ($criterion->getMalus()) {
  87.                         $malusSort[$criterion->getMalus()->getId()] = $malusListRepository->findBy(['malus' => $criterion->getMalus(), 'enabled' => 1], ["sort" => "ASC"]);
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.         if (!isset($themes)) {
  93.             return $this->render('main/error.html.twig', array(
  94.                 'message' => "There are no existing themes.",
  95.                 'concours' => $this->getParameter('app.concours')
  96.             ));
  97.         }
  98.         $allTeams $security->getUser()->getTeam();
  99.         $teams = array();
  100.         foreach ($allTeams as $team) {
  101.             if ($team->isEnabled()) {
  102.                 $teamResultsCount $doctrine->getRepository(Result::class)->countByJuryAndTeam($security->getUser()->getId(), $team->getId());
  103.                 $criteriaCount 0;
  104.                 foreach ($themes as $theme) {
  105.                     /*$criteriaSortByTheme = $criterionRepository->findCriterionByThemeNotation($theme);
  106.                     $criteria = array();
  107.                     foreach ($criteriaSortByTheme as $criterion) {
  108.                          $criteria[] = $criterion;
  109.                     }*/
  110.                     $criteria $theme->getCriteriaNotation();
  111.                     foreach ($criteria as $criterion) {
  112.                         if (!$criterion->getMalus() and $criterion->isEnabled() and !$criterion->isBonus()) {
  113.                             $criteriaCount++;
  114.                         }
  115.                     }
  116.                 }
  117.                 if ($teamResultsCount != $criteriaCount) {
  118.                     $team->setCompletelyRated(false);
  119.                 } else {
  120.                     $team->setCompletelyRated(true);
  121.                 }
  122.                 $teams[$team->getName()] = $team;
  123.             }
  124.         }
  125.         ksort($teams);
  126.         if (!isset($teams)) {
  127.             return $this->render('main/error.html.twig', array(
  128.                 'message' => "There is no existing team.",
  129.                 'concours' => $this->getParameter('app.concours')
  130.             ));
  131.         }
  132.         $currentTeam $doctrine->getRepository(Team::class)->findOneBy(["id" => $id]);
  133.         if (!isset($currentTeam)) {
  134.             return $this->render('main/error.html.twig', array(
  135.                 'message' => "The team you are trying to rate does not exist.",
  136.                 'concours' => $this->getParameter('app.concours')
  137.             ));
  138.         }
  139.         $results $doctrine->getRepository(Result::class)->findAllValuesByJury($security->getUser()->getId());
  140.         $allResults = array();
  141.         foreach ($results as $result) {
  142.             if ($result->getItem()) {
  143.                 $allResults[$result->getTeam()->getId()][$result->getCriterion()->getId()] = $result;
  144.             } else {
  145.                 $allResults[$result->getTeam()->getId()][$result->getCriterion()->getId()] = $result->getValue();
  146.             }
  147.         }
  148.         $comments $doctrine->getRepository(Comment::class)->findAllValuesByJury($security->getUser()->getId());
  149.         $allComments = array();
  150.         foreach ($comments as $comment) {
  151.             $allComments[$comment->getTeam()->getId()][$comment->getTheme()->getId()] = $comment->getValue();
  152.         }
  153.         // Bonus
  154.         $bonusAvailable null;
  155.         if ($security->getUser()->isPresident()) {
  156.             $totalBonus 25;
  157.             $usedBonusPoints 0;
  158.             $currentTeamBonusPoints 0;
  159.             $ratedBonuses $doctrine->getRepository(Result::class)->findRatedBonusesByJury($security->getUser()->getId());
  160.             $currentTeamRatedBonuses $doctrine->getRepository(Result::class)->findRatedBonusesByJuryAndTeam($security->getUser()->getId(), $id);
  161.             if (isset($ratedBonuses)) {
  162.                 foreach ($ratedBonuses as $ratedBonus) {
  163.                     $usedBonusPoints += $ratedBonus->getValue();
  164.                 }
  165.             }
  166.             if (isset($currentTeamRatedBonuses)) {
  167.                 foreach ($currentTeamRatedBonuses as $ratedBonus) {
  168.                     $currentTeamBonusPoints += $ratedBonus->getValue();
  169.                 }
  170.             }
  171.             $bonusAvailable $totalBonus $usedBonusPoints $currentTeamBonusPoints;
  172.         }
  173.         return $this->render('main/index.html.twig', array(
  174.             'jury' => $security->getUser(),
  175.             'themes' => $themes,
  176.             'teams' => $teams,
  177.             'currentTeam' => $currentTeam,
  178.             'results' => $allResults,
  179.             'comments' => $allComments,
  180.             'malusSort' => $malusSort,
  181.             'criteriaSort' => $criteriaSort,
  182.             'bonusAvailable' => $bonusAvailable,
  183.             'concours' => $this->getParameter('app.concours'),
  184.             'locale' => $security->getUser()->getLang(),
  185.             'currentTeamId' => $id
  186.         ));
  187.     }
  188.     /**
  189.      * @Route("/save/{team}/{critere}/{value}", name="app_save")
  190.      */
  191.     public function saveAction(ManagerRegistry $doctrine$team$critere$valueSecurity $security)
  192.     {
  193.         $result $doctrine->getRepository(Result::class)->findOneBy(['team' => $team'criterion' => $critere'jury' => $security->getUser()->getId()]);
  194.         if (!$result) {
  195.             $result = new Result();
  196.             $team $doctrine->getRepository(Team::class)->find($team);
  197.             $jury $doctrine->getRepository(Jury::class)->find($security->getUser()->getId());
  198.             $critere $doctrine->getRepository(Criterion::class)->find($critere);
  199.             if ($team && $jury && $critere) {
  200.                 $result->setTeam($team);
  201.                 $result->setCriterion($critere);
  202.                 $result->setJury($jury);
  203.             } else {
  204.                 return new JsonResponse(['error']);
  205.             }
  206.         }
  207.         $result->setValue($value);
  208.         $em $doctrine->getManager();
  209.         $em->persist($result);
  210.         $em->flush();
  211.         return new JsonResponse(['OK']);
  212.     }
  213.     /**
  214.      * @Route("/delete/{team}/{critere}", name="app_delete")
  215.      */
  216.     public function deleteAction(ManagerRegistry $doctrine$team$critereSecurity $security)
  217.     {
  218.         $result $doctrine->getRepository(Result::class)->findOneBy(['team' => $team'criterion' => $critere'jury' => $security->getUser()->getId()]);
  219.         if ($result) {
  220.             $em $doctrine->getManager();
  221.             $em->remove($result);
  222.             $em->flush();
  223.         }
  224.         return new JsonResponse(['OK']);
  225.     }
  226.     /**
  227.      * @Route("/file/{id}", name="app_file")
  228.      */
  229.     public function fileAction(ManagerRegistry $doctrine$idSecurity        $security)
  230.     {
  231.         $team $doctrine->getRepository(Team::class)->find($id);
  232.         if ($team) {
  233.             return $this->render('main/file.html.twig', array(
  234.                 'team' => $team,
  235.                 'jury' => $security->getUser(),
  236.                 'concours' => $this->getParameter('app.concours'),
  237.                 'locale' => $security->getUser()->getLang()
  238.             ));
  239.         }else{
  240.            return new JsonResponse(['error']);
  241.         }
  242.     }
  243.     /**
  244.      * @Route("/fr/read/{id}", name="app_read")
  245.      */
  246.     public function read(ManagerRegistry $doctrine$idSecurity        $security$projectDir){
  247.         $team $doctrine->getRepository(Team::class)->find($id);
  248.         if ($team) {
  249.             header("Content-type: application/pdf");
  250.             header("Content-Disposition: inline; filename=".$team->getName());
  251.             @readfile($projectDir.'/var/uploads/'.$team->getId().'/'.$team->getDocName());
  252.             //@readfile($projectDir.'/public/reglement.pdf');
  253.         }
  254.     }
  255.     /**
  256.      * @Route("/en/read/{id}", name="app_read_en")
  257.      */
  258.     public function readEn(ManagerRegistry $doctrine$idSecurity        $security$projectDir){
  259.         $team $doctrine->getRepository(Team::class)->find($id);
  260.         if ($team) {
  261.             header("Content-type: application/pdf");
  262.             header("Content-Disposition: inline; filename=".$team->getName());
  263.             @readfile($projectDir.'/var/uploads/'.$team->getId().'/'.$team->getDocNameEn());
  264.             //@readfile($projectDir.'/public/reglement.pdf');
  265.         }
  266.     }
  267.     /**
  268.      * @Route("/saveselect/{team}/{critere}/{value}", name="app_saveselect")
  269.      */
  270.     public function saveSelectAction(ManagerRegistry $doctrine$team$critere$valueSecurity $security)
  271.     {
  272.         $result $doctrine->getRepository(Result::class)->findOneBy(['team' => $team'criterion' => $critere'jury' => $security->getUser()->getId()]);
  273.         if (!$result) {
  274.             $result = new Result();
  275.             $team $doctrine->getRepository(Team::class)->find($team);
  276.             $jury $doctrine->getRepository(Jury::class)->find($security->getUser()->getId());
  277.             $critere $doctrine->getRepository(Criterion::class)->find($critere);
  278.             if ($team && $jury && $critere) {
  279.                 $result->setTeam($team);
  280.                 $result->setCriterion($critere);
  281.                 $result->setJury($jury);
  282.             } else {
  283.                 return new JsonResponse(['error']);
  284.             }
  285.         }
  286.         $critere $doctrine->getRepository(Criterion::class)->find($critere);
  287.         if ($critere->isBonus()) {
  288.             $result->setValue($value);
  289.         } elseif ($critere->isMalusBonus()) {
  290.             $malusItem $doctrine->getRepository(MalusList::class)->find($value);
  291.             if ($malusItem) {
  292.                 $malus $critere->getNoteMax() - $malusItem->getNbLostPoint();
  293.                 $result->setItem($malusItem);
  294.                 $result->setValue($malus);
  295.             } else {
  296.                 return new JsonResponse(['error']);
  297.             }
  298.         } else {
  299.             // Get malus item
  300.             $malusItem $doctrine->getRepository(MalusList::class)->find($value);
  301.             if ($malusItem) {
  302.                 $malus $malusItem->getNbLostPoint();
  303.                 $result->setItem($malusItem);
  304.                 $result->setValue($malus);
  305.             } else {
  306.                 return new JsonResponse(['error']);
  307.             }
  308.         }
  309.         $em $doctrine->getManager();
  310.         $em->persist($result);
  311.         $em->flush();
  312.         return new JsonResponse(['OK']);
  313.     }
  314.     /**
  315.      * @Route("/comment", name="app_save_comment")
  316.      */
  317.     public function saveCommentAction(Request $requestManagerRegistry $doctrineSecurity $security)
  318.     {
  319.         $teamId $request->request->get("team");
  320.         $themeId $request->request->get("theme");
  321.         $juryId $security->getUser()->getId();
  322.         $value $request->request->get("comment");
  323.         $comment $doctrine->getRepository(Comment::class)->findOneBy(['team' => $teamId'theme' => $themeId'jury' => $juryId]);
  324.         if (!$comment) {
  325.             $comment = new Comment();
  326.             $team $doctrine->getRepository(Team::class)->find($teamId);
  327.             $jury $doctrine->getRepository(Jury::class)->find($juryId);
  328.             $theme $doctrine->getRepository(Theme::class)->find($themeId);
  329.             if ($team && $jury && $theme) {
  330.                 $comment->setTeam($team);
  331.                 $comment->setTheme($theme);
  332.                 $comment->setJury($jury);
  333.             } else {
  334.                 return new JsonResponse(['error']);
  335.             }
  336.         }
  337.         $comment->setValue($value);
  338.         $em $doctrine->getManager();
  339.         $em->persist($comment);
  340.         $em->flush();
  341.         return new JsonResponse(['OK']);
  342.     }
  343.     /**
  344.      * @Route("/closure", name="app_closure")
  345.      */
  346.     public function closureAction(ManagerRegistry $doctrineSecurity $securityCriterionRepository $criterionRepository)
  347.     {
  348.         $unratedCriterion = array();
  349.         $i 0;
  350.         foreach ($security->getUser()->getTeam() as $team) {
  351.             if ($team->isEnabled()) {
  352.                 $j 0;
  353.                 foreach ($security->getUser()->getTheme() as $theme) {
  354.                     if ($theme->isEnabled()) {
  355.                         $k 0;
  356.                         //$criteria = array();
  357.                         /*$criteriaByTheme = $criterionRepository->findCriterionByThemeNotation($theme);
  358.                         foreach ($criteriaByTheme as $criterion) {
  359.                              $criteria[] = $criterion;
  360.                         }*/
  361.                         $criteria $theme->getCriteriaNotation();
  362.                         foreach ($criteria as $criterion) {
  363.                             if ($criterion->isEnabled()) {
  364.                                 $result $doctrine->getRepository(Result::class)->findOneBy(['team' => $team->getId(), 'criterion' => $criterion->getId(), 'jury' => $security->getUser()->getId()]);
  365.                                 if (!$result and !$criterion->getMalus() and !$criterion->isBonus()) {
  366.                                     $unratedCriterion[$i]["id"] = $team->getId();
  367.                                     $unratedCriterion[$i]["team"] = $team->getName();
  368.                                     $unratedCriterion[$i]["themes"][$j]["id"] = $theme->getId();
  369.                                     $unratedCriterion[$i]["themes"][$j]["theme"] = $theme->getName();
  370.                                     $unratedCriterion[$i]["themes"][$j]["criteria"][$k] = $criterion->getName();
  371.                                     $k++;
  372.                                 }
  373.                                 //}
  374.                             }
  375.                         }
  376.                         if (isset($unratedCriterion[$i]["themes"])) {
  377.                             $j++;
  378.                         }
  379.                     }
  380.                 }
  381.                 if (isset($unratedCriterion[$i])) {
  382.                     $i++;
  383.                 }
  384.             }
  385.         }
  386.         return new JsonResponse($unratedCriterion);
  387.     }
  388.     /**
  389.      * @Route("/results", name="app_results")
  390.      */
  391.     public function resultsAction(ManagerRegistry $doctrine,
  392.                                   Security        $security,
  393.                                   TranslatorInterface    $translator): Response
  394.     {
  395.         if ($security->getUser()->getLang()) {
  396.             $translator->setLocale($security->getUser()->getLang());
  397.         }
  398.         $allThemes $security->getUser()->getTheme();
  399.         $themes = array();
  400.         $totalMax 0;
  401.         foreach ($allThemes as $theme) {
  402.             if ($theme->isEnabled()) {
  403.                 $totalMaxTheme $doctrine->getRepository(Criterion::class)->findTotalMaxByTheme($theme->getId());
  404.                 if (isset($totalMaxTheme["val"])) {
  405.                     $theme->setTotalMax($totalMaxTheme["val"]);
  406.                 } else {
  407.                     $theme->setTotalMax(0);
  408.                 }
  409.                 $themes[] = $theme;
  410.                 $totalMax += $totalMaxTheme["val"];
  411.             }
  412.         }
  413.         if (!isset($themes)) {
  414.             return $this->render('main/error.html.twig', array(
  415.                 'message' => "There are no existing themes.",
  416.                 'concours' => $this->getParameter('app.concours')
  417.             ));
  418.         }
  419.         $allTeams $security->getUser()->getTeam();
  420.         $teams = array();
  421.         foreach ($allTeams as $team) {
  422.             if ($team->isEnabled()) {
  423.                 $teams[] = $team;
  424.             }
  425.         }
  426.         $allDisqualifiedTeams $doctrine->getRepository(Result::class)->findDisqualifiedTeamsByJury($security->getUser()->getId());
  427.         $disqualifiedTeams = array();
  428.         foreach ($allDisqualifiedTeams as $team) {
  429.             $disqualifiedTeam $doctrine->getRepository(Team::class)->findOneBy(['id' => $team["team"]]);
  430.             if ($disqualifiedTeam->isEnabled()) {
  431.                 $disqualifiedTeams[] = $disqualifiedTeam;
  432.             }
  433.         }
  434.         if (!isset($teams)) {
  435.             return $this->render('main/error.html.twig', array(
  436.                 'message' => "There is no existing team.",
  437.                 'concours' => $this->getParameter('app.concours')
  438.             ));
  439.         }
  440.         $themeResults = array();
  441.         $total = array();
  442.         $themeComments = array();
  443.         foreach ($themes as $theme) {
  444.             $themeId $theme->getid();
  445.             foreach ($teams as $team) {
  446.                 $teamId $team->getId();
  447.                 $result $doctrine->getRepository(Result::class)->findForResult($security->getUser()->getId(), $teamId$themeId);
  448.                 $themeResults[$security->getUser()->getId()][$teamId][$themeId] = $result['val'];
  449.                 $comment $doctrine->getRepository(Comment::class)->findOneBy(['jury' => $security->getUser()->getId(), 'team' => $teamId'theme' => $themeId]);
  450.                 $themeComments[$security->getUser()->getId()][$teamId][$themeId] = isset($comment) ? $comment->getValue() : "";
  451.                 $result $doctrine->getRepository(Result::class)->findTotalByTeam($security->getUser()->getId(), $teamId);
  452.                 $total[$security->getUser()->getId()][$teamId] = $result['val'];
  453.             }
  454.         }
  455.         return $this->render('main/results.html.twig', array(
  456.             'themes' => $themes,
  457.             'teams' => $teams,
  458.             'disqualifiedTeams' => $disqualifiedTeams,
  459.             'jury' => $security->getUser(),
  460.             'results' => $themeResults,
  461.             'comments' => $themeComments,
  462.             'total' => $total,
  463.             'totalMax' => $totalMax,
  464.             'concours' => $this->getParameter('app.concours'),
  465.             'locale' => $security->getUser()->getLang()
  466.         ));
  467.     }
  468.     /**
  469.      * @Route("/adm", name="app_admin")
  470.      */
  471.     public function adminAction(): Response
  472.     {
  473.         return $this->redirectToRoute('app_admin_theme');
  474.     }
  475. }