<?php
namespace App\Controller;
use App\Entity\Challenge;
use App\Entity\Team;
use App\Entity\TeamChallenge;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
class RetrieveStatsAction extends AbstractController
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager) {
$this->entityManager = $entityManager;
}
public function __invoke()
{
$teams = $this->entityManager
->getRepository(Team::class)
->createQueryBuilder('t')
->leftJoin('t.teamChallenges', 'tc')
->select('SUM(tc.points) as teamPoints, t.name as teamName, t.id, t.icon')
->groupBy('tc.team')
->addOrderBy('teamPoints', 'ASC')
->where('tc.points is not NULL')
->getQuery()
->getResult();
foreach ($teams as $key => $team) {
if ($team['id'] != null) {
$results = $this->entityManager
->getRepository(TeamChallenge::class)
->createQueryBuilder('tc')
->leftJoin('tc.team', 't')
->leftJoin('tc.challenge', 'c')
->select('c.name as challengeName, tc.points')
->where('t.id = ' . $team["id"])
->getQuery()
->getResult();
// dd(str_replace("\t", '', $teams[$key]['icon']));
// $teams[$key]['icon'] = str_replace("\t", '', str_replace("\n", '', str_replace("\r", '', $teams[$key]['icon'])));
// dd($teams[$key]['icon']);
// dd($results);
foreach ($results as $result) {
$teams[(int)$key][$result['challengeName']] = $result['points'];
$teams[(int)$key][$result['challengeName'] . 'Color'] = sprintf('#%06X', mt_rand(0, 0xFFFFFF));
}
} else {
// unset($teams[$key]);
}
}
// dd($teams);
return new JsonResponse($teams);
}
}