src/Controller/FrontDashboardController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Course;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use App\Entity\Subscription;
  8. use App\Entity\User;
  9. use App\Entity\Question;
  10. use App\Entity\CodeStep;
  11. /**
  12.  * @Route("/dashboard")
  13.  */
  14. class FrontDashboardController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/", name="dashboard_front")
  18.      */
  19.     public function index(): Response
  20.     {
  21.         if(in_array('ROLE_ADMIN'$this->getUser()->getRoles())){
  22.             return $this->redirectToRoute('admin');
  23.         }
  24.         $lastSubscription null;
  25.         $md5UserID null;
  26.         $isMinor false;
  27.         $lastSubscriptions $this->getDoctrine()->getRepository(Subscription::class)->findBy(array('user'=>$this->getUser()), array('id'=>'DESC'), 10);
  28.         if($lastSubscriptions && $lastSubscriptions[0]->getPack()->getCategory() != 'code'){
  29.             if($lastSubscriptions[0]->getExamDate()) {
  30.                 $examDate $lastSubscriptions[0]->getExamDate();
  31.             } else {
  32.                 $examDate $lastSubscriptions[0]->getExam()->getDate();
  33.             }
  34.             if(strtotime($examDate->format('Y-m-d')) > strtotime(date('Y-m-d'))){
  35.                 $lastSubscription $lastSubscriptions[0];
  36.                 if($this->getUser()->getDatebirth()->getTimestamp() > strtotime("-18 years")) $isMinor true;
  37.                 $md5UserID md5($this->getUser()->getId());
  38.             }
  39.         }
  40.         return $this->render('front/dashboard/index.html.twig', [
  41.             'controller_name' => 'FrontDashboardController',
  42.             'lastSubscription' => $lastSubscription,
  43.             'isMinor' => $isMinor,
  44.             'md5UserID' => $md5UserID
  45.         ]);
  46.     }
  47.     /**
  48.      * @Route("/profile", name="dashboard_profile")
  49.      */
  50.     public function profile(): Response
  51.     {
  52.         $isMinor false;
  53.         if($this->getUser()->getDatebirth() && $this->getUser()->getDatebirth()->getTimestamp() > strtotime("-18 years")) $isMinor true;
  54.         $userData = array();
  55.         if($this->getUser()->getData()){
  56.             $userData $this->getUser()->getData();
  57.         }
  58.         return $this->render('front/dashboard/profile.html.twig', [
  59.             'controller_name' => 'FrontDashboardController',
  60.             'isMinor' => $isMinor,
  61.             'userData' => $userData
  62.         ]);
  63.     }
  64.     /**
  65.      * @Route("/profile-edit", name="dashboard_edit_profile")
  66.      */
  67.     public function edit_profile(): Response
  68.     {
  69.         $isMinor false;
  70.         if($this->getUser()->getDatebirth() && $this->getUser()->getDatebirth()->getTimestamp() > strtotime("-18 years")) $isMinor true;
  71.         $userData = array();
  72.         if($this->getUser()->getData()){
  73.             $userData $this->getUser()->getData();
  74.         }
  75.         return $this->render('front/dashboard/profile-edit.html.twig', [
  76.             'controller_name' => 'FrontDashboardController',
  77.             'isMinor' => $isMinor,
  78.             'userData' => $userData
  79.         ]);
  80.     }
  81.     /**
  82.      * @Route("/subscriptions", name="dashboard_subscriptions")
  83.      */
  84.     public function subscriptions(): Response
  85.     {
  86.         if(in_array('ROLE_PARTNER'$this->getUser()->getRoles())){
  87.             return $this->redirectToRoute('dashboard_parrainage');
  88.         }
  89.         $showSubscription = isset($_GET['sID']) ? (int)$_GET['sID'] : null;
  90.         $subscriptions $this->getDoctrine()->getRepository(Subscription::class)->findBy(array('user'=>$this->getUser()));
  91.         return $this->render('front/dashboard/subscriptions.html.twig', [
  92.             'controller_name' => 'FrontDashboardController',
  93.             'subscriptions' => $subscriptions,
  94.             'showSubscription' => $showSubscription
  95.         ]);
  96.     }
  97.     /**
  98.      * @Route("/parrainage", name="dashboard_parrainage")
  99.      */
  100.     public function parrainage(): Response
  101.     {
  102.         if(!in_array('ROLE_PARTNER'$this->getUser()->getRoles())){
  103.             return $this->redirectToRoute('dashboard_subscriptions');
  104.         }
  105.         $users $this->getDoctrine()->getRepository(User::class)->findBy(array('partner'=>$this->getUser()));
  106.         return $this->render('front/dashboard/parrainage.html.twig', [
  107.             'controller_name' => 'FrontDashboardController',
  108.             'users' => $users
  109.         ]);
  110.     }
  111.     /**
  112.      * @Route("/code", name="dashboard_code")
  113.      */
  114.     public function code(): Response
  115.     {
  116.         // if(!$this->getUser()->canCode()) throw $this->createNotFoundException('', new NoConfigurationException());
  117.         if(!$this->getUser()->canCode()) return $this->redirectToRoute('dashboard_front');
  118.      
  119.         $canCode $this->getUser()->canCode();
  120.         $courseID 9;
  121.         $codeStep $this->getDoctrine()->getRepository(CodeStep::class)->findOneBy(array('subscription'=>$canCode->getId()));
  122.         $getCodeStatsExam $this->getCodeResults($codeStepfalse);
  123.         $getCodeStatsTraining $this->getCodeResults($codeSteptrue);
  124.         // var_dump($getCodeStatsExam['codeResults']);
  125.         // die;
  126.         return $this->render('front/dashboard/code.html.twig', [
  127.             'controller_name' => 'FrontDashboardController',
  128.             'subbscription' => $canCode,
  129.             'getCodeStatsExam' => $getCodeStatsExam,
  130.             'getCodeStatsTraining' => $getCodeStatsTraining
  131.         ]);
  132.     }
  133.     private function getCodeResults($codeStep$isTraining false){
  134.         if($codeStep){
  135.             $codeStepAnswers = !$isTraining $codeStep->getAnswers() : $codeStep->getTrainingAnswers();
  136.             $codeStepAnswers $codeStepAnswers json_decode($codeStepAnswers) : null;
  137.         } else {
  138.             $codeStepAnswers null;
  139.         }
  140.         $lastQuestion null;
  141.         $totalAnswered 0;
  142.         $activeCourses $this->getDoctrine()->getRepository(Course::class)->findBy(array('active'=>1));
  143.         $questions $this->getDoctrine()->getRepository(Question::class)->findBy(array('course'=>$activeCourses'active'=>1));
  144.         $userGoodAnswers = array();
  145.         foreach($questions as $question){
  146.             $goodQuestionAnswers $question->getAnswers();
  147.             $goodQuestionArrayAnswers = array();
  148.             if($goodQuestionAnswers){
  149.                 foreach($goodQuestionAnswers as $goodQuestionAnswerPos => $goodQuestionAnswer){
  150.                     if($goodQuestionAnswer->getCorrect() == 1){
  151.                         $goodQuestionArrayAnswers[] = $goodQuestionAnswerPos;
  152.                     }
  153.                 }
  154.                 if(isset($codeStepAnswers->{$question->getId()})) {
  155.                     if($goodQuestionArrayAnswers == $codeStepAnswers->{$question->getId()}) $userGoodAnswers[] = $question->getId();
  156.                     $lastQuestion $question;
  157.                     $totalAnswered++;
  158.                 }
  159.             }
  160.         }
  161.         return array(
  162.             'userGoodAnswers' => $userGoodAnswers,
  163.             'total' => sizeof($questions),
  164.             'totalAnswered' => $totalAnswered,
  165.             'lastQuestion' => $lastQuestion
  166.         );
  167.     }
  168.     
  169.     /**
  170.      * @Route("/code/entrainement", name="dashboard_code_training")
  171.      */
  172.     public function code_training(): Response
  173.     {
  174.         if(!$this->getUser()->canCode()) return $this->redirectToRoute('dashboard_front');
  175.         $courses $this->getDoctrine()->getRepository(Course::class)->findBy(array('active'=>true));
  176.         return $this->render('front/dashboard/code-training.html.twig', [
  177.             'controller_name' => 'FrontDashboardController',
  178.             'courses' => $courses
  179.         ]);
  180.     }
  181.     /**
  182.      * @Route("/code/entrainement/{courseID}", name="code_training_course")
  183.      */
  184.     public function code_training_course($courseID): Response
  185.     {
  186.         if(!$this->getUser()->canCode()) return $this->redirectToRoute('dashboard_front');
  187.         $course $this->getDoctrine()->getRepository(Course::class)->findOneBy(array('id'=>(int)$courseID));
  188.         return $this->render('front/dashboard/code-training-course.html.twig', [
  189.             'controller_name' => 'FrontDashboardController',
  190.             'course' => $course
  191.         ]);
  192.     }
  193.     /**
  194.      * @Route("/code/examen", name="dashboard_code_examen")
  195.      */
  196.     public function code_examen(): Response
  197.     {
  198.         if(!$this->getUser()->canCode()) return $this->redirectToRoute('dashboard_front');
  199.         return $this->render('front/dashboard/code-examen.html.twig', [
  200.             'controller_name' => 'FrontDashboardController'
  201.         ]);
  202.     }
  203.     /**
  204.      * @Route("/code/galerie", name="dashboard_code_gallery")
  205.      */
  206.     public function code_gallery(): Response
  207.     {
  208.         if(!$this->getUser()->canCode()) return $this->redirectToRoute('dashboard_front');
  209.         $em $this->getDoctrine()->getManager();
  210.         $query $em->getRepository('App:Question')
  211.         ->createQueryBuilder('q')
  212.         ->leftJoin("App\Entity\Course""c""WITH""c.id = q.course and c.active = 1 and q.active = 1")
  213.         ->where("c.active = 1 and q.active = 1")
  214.         ->addOrderBy('c.position''ASC')
  215.         ->addOrderBy('q.position''ASC');
  216.         $questions $query->getQuery()->getResult();
  217.         return $this->render('front/dashboard/code-gallery.html.twig', [
  218.             'controller_name' => 'FrontDashboardController',
  219.             'questions' => $questions
  220.         ]);
  221.     }
  222.     /**
  223.      * @Route("/autoeval", name="auto_eval")
  224.      */
  225.     public function auto_eval(): Response
  226.     {
  227.         $lastSubscription null;
  228.         $lastSubscriptions $this->getDoctrine()->getRepository(Subscription::class)->findBy(array('user'=>$this->getUser()), array('id'=>'DESC'), 10);
  229.         if($lastSubscriptions && $lastSubscriptions[0]->getPack()->getCategory() != 'code'){
  230.             if(strtotime($lastSubscriptions[0]->getExam()->getDate()->format('Y-m-d')) > strtotime(date('Y-m-d'))){
  231.                 $lastSubscription $lastSubscriptions[0];
  232.             }
  233.         }
  234.         return $this->render('front/dashboard/auto-eval.html.twig', [
  235.             'controller_name' => 'FrontDashboardController',
  236.             'lastSubscription' => $lastSubscription
  237.         ]);
  238.     }
  239.     /**
  240.      * @Route("/signcontract", name="sign_contract")
  241.      */
  242.     public function sign_contract(): Response
  243.     {
  244.         $userData $this->getUser()->getData();
  245.         if(isset($userData)) {
  246.             if(isset($userData['signedContract'])) {
  247.                 return $this->redirectToRoute('dashboard_front');
  248.             }
  249.         }
  250.         $lastSubscription null;
  251.         $lastSubscriptions $this->getDoctrine()->getRepository(Subscription::class)->findBy(array('user'=>$this->getUser(), 'status'=>1), array('id'=>'DESC'), 10);
  252.         if($lastSubscriptions && $lastSubscriptions[0]->getPack()->getCategory() != 'code'){
  253.             if(strtotime($lastSubscriptions[0]->getExam()->getDate()->format('Y-m-d')) > strtotime(date('Y-m-d'))){
  254.                 $lastSubscription $lastSubscriptions[0];
  255.                 require __DIR__ '/../../vendor/autoload.php';
  256.                 $pdf = new \TCPDF();
  257.                 $pdf->setPrintHeader(false);
  258.                 $pdf->setPrintFooter(false);
  259.                 $pdf->setListIndentWidth(6);
  260.                 $pdf->setImageScale(1.53);
  261.                 $pdf->SetFont('dejavusans'''10''false);
  262.                 // $pdf->SetMargins(10, 5, 10, true);
  263.                 // $pdf->SetAutoPageBreak(true, 0);
  264.                 $pdf->AddPage();
  265.                 $html $this->renderView('front/dashboard/pdf-contract.html.twig', [
  266.                     'lastSubscription' => $lastSubscription
  267.                 ]);
  268.                 if(isset($_GET['echo'])){
  269.                     var_dump($html);
  270.                     die;
  271.                 }
  272.                 $pdf->writeHTML($htmltruefalsetruefalse'');
  273.                 $pdf->AddPage();
  274.                 $html $this->renderView('front/dashboard/annexe_1.html.twig', [
  275.                     'lastSubscription' => $lastSubscription
  276.                 ]);
  277.                 $pdf->writeHTML($htmltruefalsetruefalse'');
  278.                 if($this->getParameter('kernel.environment') == 'prod'){
  279.                     $uploadDir 'www';
  280.                 } else {
  281.                     $uploadDir 'public_html';
  282.                 }
  283.                 $pdfUrl '/assets/contracts/contrat_'.md5($this->getUser()->getId()).'.pdf';
  284.                 $pdfFile __DIR__ .'/../../'$uploadDir .$pdfUrl;
  285.                 $pdf->Output($pdfFile'F');
  286.             }
  287.         }
  288.         return $this->render('front/dashboard/sign-contract.html.twig', [
  289.             'controller_name' => 'FrontDashboardController',
  290.             'lastSubscription' => $lastSubscription,
  291.             'pdfUrl' => $pdfUrl
  292.         ]);
  293.     }
  294. }