<?php
namespace App\Controller;
use App\Entity\Comment;
use App\Entity\Criterion;
use App\Entity\Jury;
use App\Entity\MalusList;
use App\Entity\Result;
use App\Entity\Team;
use App\Entity\Theme;
use App\Repository\CriterionRepository;
use App\Repository\MalusListRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class MainController extends AbstractController
{
/**
* @Route("/", name="app_home")
*/
public function index(ManagerRegistry $doctrine, Security $security): Response
{
if ($security->getUser()) {
$userRoles = $security->getUser()->getRoles();
if (in_array("ROLE_SUPER_ADMIN", $userRoles) || in_array("ROLE_ADMIN", $userRoles)) {
return $this->redirectToRoute("app_admin");
}
$allTeams = $security->getUser()->getTeam();
foreach ($allTeams as $team) {
if ($team->isEnabled()) {
return $this->redirectToRoute("app_notation_team", array(
"id" => $team->getId()
));
}
}
return $this->render('main/error.html.twig', array(
'message' => "Il n'y a aucune équipe existante.",
'concours' => $this->getParameter('app.concours')
));
}
return $this->redirectToRoute("app_login");
}
/**
* @Route("/team/{id}", name="app_notation_team")
*/
public function notationAction(ManagerRegistry $doctrine,
Security $security,
$id,
MalusListRepository $malusListRepository,
CriterionRepository $criterionRepository,
TranslatorInterface $translator,
string $projectDir): Response
{
if ($security->getUser()->getLang()) {
$translator->setLocale($security->getUser()->getLang());
}
$allThemes = $security->getUser()->getTheme();
$ts = array();
foreach ($allThemes as $alt) {
$ts[$alt->getSort()] = $alt;
}
ksort($ts);
$malusSort = array();
$themes = array();
$criteriaSort = array();
foreach ($ts as $theme) {
if ($theme->isEnabled()) {
$criteriaSortByTheme = $criterionRepository->findCriterionByThemeNotation($theme);
if (count($criteriaSortByTheme) != 0) {
$themes[] = $theme;
}
foreach ($criteriaSortByTheme as $criterion) {
if ($criterion->isBonus()) {
if ($security->getUser()->isPresident()) {
$criteriaSort[$theme->getId()][] = $criterion;
}
} else {
$criteriaSort[$theme->getId()][] = $criterion;
}
}
foreach ($criteriaSortByTheme as $criterion) {
if ($criterion->getMalus()) {
$malusSort[$criterion->getMalus()->getId()] = $malusListRepository->findBy(['malus' => $criterion->getMalus(), 'enabled' => 1], ["sort" => "ASC"]);
}
}
}
}
if (!isset($themes)) {
return $this->render('main/error.html.twig', array(
'message' => "There are no existing themes.",
'concours' => $this->getParameter('app.concours')
));
}
$allTeams = $security->getUser()->getTeam();
$teams = array();
foreach ($allTeams as $team) {
if ($team->isEnabled()) {
$teamResultsCount = $doctrine->getRepository(Result::class)->countByJuryAndTeam($security->getUser()->getId(), $team->getId());
$criteriaCount = 0;
foreach ($themes as $theme) {
/*$criteriaSortByTheme = $criterionRepository->findCriterionByThemeNotation($theme);
$criteria = array();
foreach ($criteriaSortByTheme as $criterion) {
$criteria[] = $criterion;
}*/
$criteria = $theme->getCriteriaNotation();
foreach ($criteria as $criterion) {
if (!$criterion->getMalus() and $criterion->isEnabled() and !$criterion->isBonus()) {
$criteriaCount++;
}
}
}
if ($teamResultsCount != $criteriaCount) {
$team->setCompletelyRated(false);
} else {
$team->setCompletelyRated(true);
}
$teams[$team->getName()] = $team;
}
}
ksort($teams);
if (!isset($teams)) {
return $this->render('main/error.html.twig', array(
'message' => "There is no existing team.",
'concours' => $this->getParameter('app.concours')
));
}
$currentTeam = $doctrine->getRepository(Team::class)->findOneBy(["id" => $id]);
if (!isset($currentTeam)) {
return $this->render('main/error.html.twig', array(
'message' => "The team you are trying to rate does not exist.",
'concours' => $this->getParameter('app.concours')
));
}
$results = $doctrine->getRepository(Result::class)->findAllValuesByJury($security->getUser()->getId());
$allResults = array();
foreach ($results as $result) {
if ($result->getItem()) {
$allResults[$result->getTeam()->getId()][$result->getCriterion()->getId()] = $result;
} else {
$allResults[$result->getTeam()->getId()][$result->getCriterion()->getId()] = $result->getValue();
}
}
$comments = $doctrine->getRepository(Comment::class)->findAllValuesByJury($security->getUser()->getId());
$allComments = array();
foreach ($comments as $comment) {
$allComments[$comment->getTeam()->getId()][$comment->getTheme()->getId()] = $comment->getValue();
}
// Bonus
$bonusAvailable = null;
if ($security->getUser()->isPresident()) {
$totalBonus = 25;
$usedBonusPoints = 0;
$currentTeamBonusPoints = 0;
$ratedBonuses = $doctrine->getRepository(Result::class)->findRatedBonusesByJury($security->getUser()->getId());
$currentTeamRatedBonuses = $doctrine->getRepository(Result::class)->findRatedBonusesByJuryAndTeam($security->getUser()->getId(), $id);
if (isset($ratedBonuses)) {
foreach ($ratedBonuses as $ratedBonus) {
$usedBonusPoints += $ratedBonus->getValue();
}
}
if (isset($currentTeamRatedBonuses)) {
foreach ($currentTeamRatedBonuses as $ratedBonus) {
$currentTeamBonusPoints += $ratedBonus->getValue();
}
}
$bonusAvailable = $totalBonus - $usedBonusPoints + $currentTeamBonusPoints;
}
return $this->render('main/index.html.twig', array(
'jury' => $security->getUser(),
'themes' => $themes,
'teams' => $teams,
'currentTeam' => $currentTeam,
'results' => $allResults,
'comments' => $allComments,
'malusSort' => $malusSort,
'criteriaSort' => $criteriaSort,
'bonusAvailable' => $bonusAvailable,
'concours' => $this->getParameter('app.concours'),
'locale' => $security->getUser()->getLang(),
'currentTeamId' => $id
));
}
/**
* @Route("/save/{team}/{critere}/{value}", name="app_save")
*/
public function saveAction(ManagerRegistry $doctrine, $team, $critere, $value, Security $security)
{
$result = $doctrine->getRepository(Result::class)->findOneBy(['team' => $team, 'criterion' => $critere, 'jury' => $security->getUser()->getId()]);
if (!$result) {
$result = new Result();
$team = $doctrine->getRepository(Team::class)->find($team);
$jury = $doctrine->getRepository(Jury::class)->find($security->getUser()->getId());
$critere = $doctrine->getRepository(Criterion::class)->find($critere);
if ($team && $jury && $critere) {
$result->setTeam($team);
$result->setCriterion($critere);
$result->setJury($jury);
} else {
return new JsonResponse(['error']);
}
}
$result->setValue($value);
$em = $doctrine->getManager();
$em->persist($result);
$em->flush();
return new JsonResponse(['OK']);
}
/**
* @Route("/delete/{team}/{critere}", name="app_delete")
*/
public function deleteAction(ManagerRegistry $doctrine, $team, $critere, Security $security)
{
$result = $doctrine->getRepository(Result::class)->findOneBy(['team' => $team, 'criterion' => $critere, 'jury' => $security->getUser()->getId()]);
if ($result) {
$em = $doctrine->getManager();
$em->remove($result);
$em->flush();
}
return new JsonResponse(['OK']);
}
/**
* @Route("/file/{id}", name="app_file")
*/
public function fileAction(ManagerRegistry $doctrine, $id, Security $security)
{
$team = $doctrine->getRepository(Team::class)->find($id);
if ($team) {
return $this->render('main/file.html.twig', array(
'team' => $team,
'jury' => $security->getUser(),
'concours' => $this->getParameter('app.concours'),
'locale' => $security->getUser()->getLang()
));
}else{
return new JsonResponse(['error']);
}
}
/**
* @Route("/fr/read/{id}", name="app_read")
*/
public function read(ManagerRegistry $doctrine, $id, Security $security, $projectDir){
$team = $doctrine->getRepository(Team::class)->find($id);
if ($team) {
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$team->getName());
@readfile($projectDir.'/var/uploads/'.$team->getId().'/'.$team->getDocName());
//@readfile($projectDir.'/public/reglement.pdf');
}
}
/**
* @Route("/en/read/{id}", name="app_read_en")
*/
public function readEn(ManagerRegistry $doctrine, $id, Security $security, $projectDir){
$team = $doctrine->getRepository(Team::class)->find($id);
if ($team) {
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$team->getName());
@readfile($projectDir.'/var/uploads/'.$team->getId().'/'.$team->getDocNameEn());
//@readfile($projectDir.'/public/reglement.pdf');
}
}
/**
* @Route("/saveselect/{team}/{critere}/{value}", name="app_saveselect")
*/
public function saveSelectAction(ManagerRegistry $doctrine, $team, $critere, $value, Security $security)
{
$result = $doctrine->getRepository(Result::class)->findOneBy(['team' => $team, 'criterion' => $critere, 'jury' => $security->getUser()->getId()]);
if (!$result) {
$result = new Result();
$team = $doctrine->getRepository(Team::class)->find($team);
$jury = $doctrine->getRepository(Jury::class)->find($security->getUser()->getId());
$critere = $doctrine->getRepository(Criterion::class)->find($critere);
if ($team && $jury && $critere) {
$result->setTeam($team);
$result->setCriterion($critere);
$result->setJury($jury);
} else {
return new JsonResponse(['error']);
}
}
$critere = $doctrine->getRepository(Criterion::class)->find($critere);
if ($critere->isBonus()) {
$result->setValue($value);
} elseif ($critere->isMalusBonus()) {
$malusItem = $doctrine->getRepository(MalusList::class)->find($value);
if ($malusItem) {
$malus = $critere->getNoteMax() - $malusItem->getNbLostPoint();
$result->setItem($malusItem);
$result->setValue($malus);
} else {
return new JsonResponse(['error']);
}
} else {
// Get malus item
$malusItem = $doctrine->getRepository(MalusList::class)->find($value);
if ($malusItem) {
$malus = 0 - $malusItem->getNbLostPoint();
$result->setItem($malusItem);
$result->setValue($malus);
} else {
return new JsonResponse(['error']);
}
}
$em = $doctrine->getManager();
$em->persist($result);
$em->flush();
return new JsonResponse(['OK']);
}
/**
* @Route("/comment", name="app_save_comment")
*/
public function saveCommentAction(Request $request, ManagerRegistry $doctrine, Security $security)
{
$teamId = $request->request->get("team");
$themeId = $request->request->get("theme");
$juryId = $security->getUser()->getId();
$value = $request->request->get("comment");
$comment = $doctrine->getRepository(Comment::class)->findOneBy(['team' => $teamId, 'theme' => $themeId, 'jury' => $juryId]);
if (!$comment) {
$comment = new Comment();
$team = $doctrine->getRepository(Team::class)->find($teamId);
$jury = $doctrine->getRepository(Jury::class)->find($juryId);
$theme = $doctrine->getRepository(Theme::class)->find($themeId);
if ($team && $jury && $theme) {
$comment->setTeam($team);
$comment->setTheme($theme);
$comment->setJury($jury);
} else {
return new JsonResponse(['error']);
}
}
$comment->setValue($value);
$em = $doctrine->getManager();
$em->persist($comment);
$em->flush();
return new JsonResponse(['OK']);
}
/**
* @Route("/closure", name="app_closure")
*/
public function closureAction(ManagerRegistry $doctrine, Security $security, CriterionRepository $criterionRepository)
{
$unratedCriterion = array();
$i = 0;
foreach ($security->getUser()->getTeam() as $team) {
if ($team->isEnabled()) {
$j = 0;
foreach ($security->getUser()->getTheme() as $theme) {
if ($theme->isEnabled()) {
$k = 0;
//$criteria = array();
/*$criteriaByTheme = $criterionRepository->findCriterionByThemeNotation($theme);
foreach ($criteriaByTheme as $criterion) {
$criteria[] = $criterion;
}*/
$criteria = $theme->getCriteriaNotation();
foreach ($criteria as $criterion) {
if ($criterion->isEnabled()) {
$result = $doctrine->getRepository(Result::class)->findOneBy(['team' => $team->getId(), 'criterion' => $criterion->getId(), 'jury' => $security->getUser()->getId()]);
if (!$result and !$criterion->getMalus() and !$criterion->isBonus()) {
$unratedCriterion[$i]["id"] = $team->getId();
$unratedCriterion[$i]["team"] = $team->getName();
$unratedCriterion[$i]["themes"][$j]["id"] = $theme->getId();
$unratedCriterion[$i]["themes"][$j]["theme"] = $theme->getName();
$unratedCriterion[$i]["themes"][$j]["criteria"][$k] = $criterion->getName();
$k++;
}
//}
}
}
if (isset($unratedCriterion[$i]["themes"])) {
$j++;
}
}
}
if (isset($unratedCriterion[$i])) {
$i++;
}
}
}
return new JsonResponse($unratedCriterion);
}
/**
* @Route("/results", name="app_results")
*/
public function resultsAction(ManagerRegistry $doctrine,
Security $security,
TranslatorInterface $translator): Response
{
if ($security->getUser()->getLang()) {
$translator->setLocale($security->getUser()->getLang());
}
$allThemes = $security->getUser()->getTheme();
$themes = array();
$totalMax = 0;
foreach ($allThemes as $theme) {
if ($theme->isEnabled()) {
$totalMaxTheme = $doctrine->getRepository(Criterion::class)->findTotalMaxByTheme($theme->getId());
if (isset($totalMaxTheme["val"])) {
$theme->setTotalMax($totalMaxTheme["val"]);
} else {
$theme->setTotalMax(0);
}
$themes[] = $theme;
$totalMax += $totalMaxTheme["val"];
}
}
if (!isset($themes)) {
return $this->render('main/error.html.twig', array(
'message' => "There are no existing themes.",
'concours' => $this->getParameter('app.concours')
));
}
$allTeams = $security->getUser()->getTeam();
$teams = array();
foreach ($allTeams as $team) {
if ($team->isEnabled()) {
$teams[] = $team;
}
}
$allDisqualifiedTeams = $doctrine->getRepository(Result::class)->findDisqualifiedTeamsByJury($security->getUser()->getId());
$disqualifiedTeams = array();
foreach ($allDisqualifiedTeams as $team) {
$disqualifiedTeam = $doctrine->getRepository(Team::class)->findOneBy(['id' => $team["team"]]);
if ($disqualifiedTeam->isEnabled()) {
$disqualifiedTeams[] = $disqualifiedTeam;
}
}
if (!isset($teams)) {
return $this->render('main/error.html.twig', array(
'message' => "There is no existing team.",
'concours' => $this->getParameter('app.concours')
));
}
$themeResults = array();
$total = array();
$themeComments = array();
foreach ($themes as $theme) {
$themeId = $theme->getid();
foreach ($teams as $team) {
$teamId = $team->getId();
$result = $doctrine->getRepository(Result::class)->findForResult($security->getUser()->getId(), $teamId, $themeId);
$themeResults[$security->getUser()->getId()][$teamId][$themeId] = $result['val'];
$comment = $doctrine->getRepository(Comment::class)->findOneBy(['jury' => $security->getUser()->getId(), 'team' => $teamId, 'theme' => $themeId]);
$themeComments[$security->getUser()->getId()][$teamId][$themeId] = isset($comment) ? $comment->getValue() : "";
$result = $doctrine->getRepository(Result::class)->findTotalByTeam($security->getUser()->getId(), $teamId);
$total[$security->getUser()->getId()][$teamId] = $result['val'];
}
}
return $this->render('main/results.html.twig', array(
'themes' => $themes,
'teams' => $teams,
'disqualifiedTeams' => $disqualifiedTeams,
'jury' => $security->getUser(),
'results' => $themeResults,
'comments' => $themeComments,
'total' => $total,
'totalMax' => $totalMax,
'concours' => $this->getParameter('app.concours'),
'locale' => $security->getUser()->getLang()
));
}
/**
* @Route("/adm", name="app_admin")
*/
public function adminAction(): Response
{
return $this->redirectToRoute('app_admin_theme');
}
}