src/Controller/RetrieveStatsAction.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Challenge;
  4. use App\Entity\Team;
  5. use App\Entity\TeamChallenge;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. class RetrieveStatsAction extends AbstractController
  10. {
  11.     private $entityManager;
  12.     public function __construct(EntityManagerInterface $entityManager) {
  13.         $this->entityManager $entityManager;
  14.     }
  15.     public function __invoke()
  16.     {
  17.         $teams $this->entityManager
  18.             ->getRepository(Team::class)
  19.             ->createQueryBuilder('t')
  20.             ->leftJoin('t.teamChallenges''tc')
  21.             ->select('SUM(tc.points) as teamPoints, t.name as teamName, t.id, t.icon')
  22.             ->groupBy('tc.team')
  23.             ->addOrderBy('teamPoints''ASC')
  24.             ->where('tc.points is not NULL')
  25.             ->getQuery()
  26.             ->getResult();
  27.         foreach ($teams as $key => $team) {
  28.             if ($team['id'] != null) {
  29.                 $results $this->entityManager
  30.                     ->getRepository(TeamChallenge::class)
  31.                     ->createQueryBuilder('tc')
  32.                     ->leftJoin('tc.team''t')
  33.                     ->leftJoin('tc.challenge''c')
  34.                     ->select('c.name as challengeName, tc.points')
  35.                     ->where('t.id = ' $team["id"])
  36.                     ->getQuery()
  37.                     ->getResult();
  38. //            dd(str_replace("\t", '', $teams[$key]['icon']));
  39. //            $teams[$key]['icon'] = str_replace("\t", '', str_replace("\n", '', str_replace("\r", '', $teams[$key]['icon'])));
  40. //            dd($teams[$key]['icon']);
  41. //                dd($results);
  42.                 foreach ($results as $result) {
  43.                     $teams[(int)$key][$result['challengeName']] = $result['points'];
  44.                     $teams[(int)$key][$result['challengeName'] . 'Color'] = sprintf('#%06X'mt_rand(00xFFFFFF));
  45.                 }
  46.             } else {
  47. //                unset($teams[$key]);
  48.             }
  49.         }
  50. //        dd($teams);
  51.         return new JsonResponse($teams);
  52.     }
  53. }