src/Application/Internit/ContentBundle/Controller/CoreController.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Application\Internit\ContentBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Sonata\AdminBundle\Controller\CoreController as BaseCoreContBroller;
  7. class CoreController extends BaseCoreContBroller
  8. {
  9.     public function dashboardAction()
  10.     {
  11.         $user $this->get('security.token_storage')->getToken()->getUser();
  12.         // construtora pode ver todos os imóveis e espelhos de vendas cadastrados no sistema,
  13.         // independente da imobiliária a qual o imóvel percente.
  14.         // userId para construtora será null. Assim ela poderá ver tudo
  15.         $userId in_array('ROLE_CONSTRUTORA'$user->getRoles()) ? null $user->getId();
  16.         $realtys $this->getDoctrine()->getRepository("ApplicationInternitRealEstateBundle:Realty")->dashBoardRealty($userId);
  17.         $mirrors $this->getDoctrine()->getRepository("ApplicationInternitRealEstateBundle:Realty")->dashBoardMirror($userId);
  18.         $where = array();
  19.         $proposalOrder = array('createdAt' => 'DESC');
  20.         
  21.         if (in_array('ROLE_CORRETOR'$this->get('security.token_storage')->getToken()->getUser()->getRoles()))
  22.             $where = array('user'=>$this->get('security.token_storage')->getToken()->getUser()->getid());      
  23.         // usuário com perfil imobiliária vê todas as propostas abertas para empreendimentos
  24.         // da imobiliária
  25.         if (in_array('ROLE_IMOBILIARIA'$user->getRoles())) {
  26.             $proposals $this->getDoctrine()
  27.                 ->getRepository('ApplicationInternitProposalBundle:Proposal')
  28.                 ->findByUserRealEstates($user->getId());
  29.         }
  30.         else {
  31.             // usuário com perfil corretor e perfil construtora
  32.             $proposals $this->getDoctrine()
  33.                 ->getRepository('ApplicationInternitProposalBundle:Proposal')
  34.                 ->findBy($where$proposalOrder);
  35.         }
  36.         
  37.         // mostrar opções de imóveis das imobiliárias que o corretor faz parte
  38.         if (in_array('ROLE_CONSTRUTORA'$user->getRoles())) {
  39.             // construtora: vê todos os rankings do sistema
  40.             $rankings $this->getDoctrine()
  41.                 ->getRepository("ApplicationInternitRealEstateBundle:Realty")
  42.                 ->dashBoardRanking();
  43.         }
  44.         elseif (in_array('ROLE_IMOBILIARIA'$user->getRoles())) {
  45.             // imobiliária: vê os rankings dos imóveis da imobiliária
  46.             $rankings $this->getDoctrine()
  47.                 ->getRepository("ApplicationInternitRealEstateBundle:Realty")
  48.                 ->dashBoardRankingRealEstate($userId);
  49.         }
  50.         else {
  51.             // corretor: vê os rankings dos quais participa
  52.             $rankings $this->getDoctrine()
  53.                 ->getRepository("ApplicationInternitRealEstateBundle:Realty")
  54.                 ->dashBoardRankingUser($userId);
  55.         }
  56.         // Adicionando uma opção vazia ao array de opções
  57.         $rankings array_merge(
  58.             [ [ 'id' => '''realty' => 'Selecione um Empreendimento' ] ], 
  59.             $rankings
  60.         );
  61.         
  62.         //SALESGOAL
  63.         if (in_array('ROLE_CONSTRUTORA'$user->getRoles())) {
  64.             $data $this->getDoctrine()->getRepository('ApplicationInternitSalesGoalBundle:SalesGoal')->findAll();
  65.         }
  66.         else {
  67.             $data $this->getDoctrine()
  68.                 ->getRepository('ApplicationInternitSalesGoalBundle:SalesGoal')
  69.                 ->findByUserRealEstate($userId);
  70.         }
  71.         $infoSaleGoal $this->getDoctrine()->getRepository('ApplicationInternitSalesGoalBundle:SalesGoal')->showSalesGoal();
  72.         
  73.         $goals = array();
  74.         $value 0;
  75.         
  76.         $salesGoal = array();
  77.         foreach($data as $d)
  78.         {            
  79.             foreach($infoSaleGoal as $info)
  80.             {                   
  81.                 if($info['id'] == $d->getId())
  82.                 {   
  83.                     if($info['type']=='valor')       
  84.                     {         
  85.                         $value floatval($info['total_proposal']) + $value;
  86.                         $goals[$d->getId()]['total'] = $value;                                           
  87.                         $goals[$d->getId()]['points'][$info['month_year']] = floatval($info['total_proposal']);
  88.                     }
  89.                     elseif($info['type']=='unidades')
  90.                     {                        
  91.                         $value floatval($info['amount_proposal']) + $value;
  92.                         $goals[$d->getId()]['total'] = $value;                                           
  93.                         $goals[$d->getId()]['points'][$info['month_year']] = floatval($info['amount_proposal']);
  94.                     }
  95.                 }
  96.             }
  97.             $value 0;
  98.         }
  99.         $salesGoal['goals'] = $goals;
  100.         $salesGoal['datas'] = $data;
  101.         
  102.         //SALESGOAL        
  103.         /********************  QUIZ **********************/
  104.         $userId $this->getUser()->getId();
  105.         // obtém as respostas que o usuário fez aos quizes
  106.         $userAnswers $this->getDoctrine()->getRepository('ApplicationInternitQuizBundle:Quiz')
  107.             ->viewUserAnswers($userId);
  108.         // quizes que o usuário ainda não respondeu
  109.         if (in_array('ROLE_CONSTRUTORA'$user->getRoles())) {
  110.             $quizesToAnswer $this->getDoctrine()
  111.                 ->getRepository('ApplicationInternitQuizBundle:Quiz')
  112.                 ->viewUnansweredQuiz($userId);
  113.         }
  114.         else {
  115.             $quizesToAnswer $this->getDoctrine()
  116.                 ->getRepository('ApplicationInternitQuizBundle:Quiz')
  117.                 ->viewRealtorUnansweredQuiz($userId);
  118.         }
  119.         $numQuizesToAnswer count($quizesToAnswer);
  120.         $spentPoints $this->getDoctrine()
  121.             ->getRepository('ApplicationInternitQuizBundle:Quiz')->fetchSpentPoints($userId);
  122.         // para a última resposta do usuário
  123.         $lastUserAnswer = empty($userAnswers) ? null $userAnswers[0];
  124.         $lastUserAnswerHitsPercentage 0;
  125.         // para estatísticas de pontos e porcentagem de acertos totais
  126.         $totalScore 0;
  127.         $totalHits 0;
  128.         $totalHitsPercentage 0;
  129.         $totalNumAnswers 0;
  130.         $lastHits 0;
  131.         $lastNumAnswers 0;
  132.         foreach($userAnswers as $ua) {
  133.             $totalScore += $ua->getScore();
  134.             $totalHits += $ua->getNumHits();
  135.             $totalNumAnswers += count($ua->getAnswers());
  136.             if ($ua->getCreatedAt() > $lastUserAnswer->getCreatedAt()) {
  137.                 $lastUserAnswer $ua;
  138.             }
  139.         }
  140.         $availablePoints $totalScore $spentPoints;
  141.         if ($totalNumAnswers 0) {
  142.             $totalHitsPercentage = ($totalHits $totalNumAnswers) * 100;
  143.         }
  144.         if ($lastUserAnswer) {
  145.             $lastUserAnswerHitsPercentage $lastUserAnswer->getHitsPercentage();
  146.             $lastHits $lastUserAnswer->getNumHits();
  147.             $lastNumAnswers count($lastUserAnswer->getAnswers());
  148.         }
  149.         $randomRewards $this->getDoctrine()
  150.             ->getRepository('ApplicationInternitQuizBundle:Quiz')->viewRandomReward($userId2);
  151.         
  152.         /********************* FIM QUIZ **********************/
  153.         return $this->render(
  154.             '@ApplicationInternit/ContentBundle/Resources/views/home.html.twig',
  155.             [
  156.                 'realtys' => $realtys,
  157.                 'mirrors' => $mirrors,
  158.                 'proposals' => $proposals,
  159.                 'rankings' => $rankings,
  160.                 'salesGoal' => $salesGoal,
  161.                 'availablePoints' => $availablePoints,
  162.                 'totalHitsPercentage' => $totalHitsPercentage,
  163.                 'numQuizesToAnswer' => $numQuizesToAnswer,
  164.                 'lastAnswerHits' => $lastUserAnswerHitsPercentage,
  165.                 'randomRewards' => $randomRewards,
  166.                 'totalHits' => $totalHits,
  167.                 'totalNumAnswers' => $totalNumAnswers,
  168.                 'totalHitsPercentage' => $totalHitsPercentage,
  169.                 'lastHits' => $lastHits,
  170.                 'lastNumAnswers' => $lastNumAnswers,
  171.                 'lastUserAnswerHitsPercentage' => $lastUserAnswerHitsPercentage
  172.             ]
  173.         );
  174.     }
  175. }