DashboardCloParticipantParcoursAction.php 4.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace App\Action;

use Slim\Views\Twig;
use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

final class DashboardCloParticipantParcoursAction
{
    private $view;
    private $logger;
    private $em;
    
    public function __construct(Twig $view, LoggerInterface $logger, EntityManagerInterface $em)
    {
        $this->view = $view;
        $this->logger = $logger;
        $this->em = $em;
    }
    
    
    public function __invoke(Request $request, Response $response, $args)
    {
        $this->logger->info("dashboard page action dispatched");
        
        $params = $request->getQueryParams();
        $token = $params['token'];
        $roleSI = $request->getAttribute('roleSI');
    
        if ( ($roleSI != 'admin') && ($roleSI != 'clo') ) { 
            return $response->withStatus(401);
        }
    

        $a = [
            'token' => $token
        ];
            
        if (array_key_exists('id', $params)) {        
            $participant = $this->em->find('App\Entity\Participant', $params['id']);
            $a['participant'] = $participant;
        }
        else {
            return $response->withStatus(400);
        }
           
        $a['pa_4_am'] = $this->getParcours('2017-07-04','08:00:00','13:00:00',$participant->getId());
        $a['pa_4_pm'] = $this->getParcours('2017-07-04','12:00:00','19:00:00',$participant->getId());
        $a['pa_5_am'] = $this->getParcours('2017-07-05','08:00:00','13:00:00',$participant->getId());
        $a['pa_5_pm'] = $this->getParcours('2017-07-05','12:00:00','19:00:00',$participant->getId());
        $a['pa_6_am'] = $this->getParcours('2017-07-06','08:00:00','13:00:00',$participant->getId());
        $a['pa_6_pm'] = $this->getParcours('2017-07-06','12:00:00','19:00:00',$participant->getId());
        $a['pa_7_am'] = $this->getParcours('2017-07-07','08:00:00','13:00:00',$participant->getId());
        $a['pa_7_pm'] = $this->getParcours('2017-07-07','12:00:00','19:00:00',$participant->getId());
        

        if (array_key_exists('jour', $params)) {                
            $agendas = $this->getAgendas($params['jour'],$params['debut'],$params['fin'],$participant->getId());
            $a['agendas'] = $agendas;   
            $response = $response->write(json_encode($agendas))->withHeader('Content-type', 'application/json');
        } 
        else {
            $this->view->render($response, 'dashboard_clo_participant_parcours.twig', $a);
        }
        

        return $response;
    }
    

       //select *,
    // (select count(*) from participant_x_agenda pa where pa.id_agenda=a.id) as nb_inscrits,
    // (select count(*) from participant_x_agenda p2 where p2.id_agenda=a.id and p2.id_participant=100) as check from agenda a;
    public function getAgendas($jour,$debut,$fin,$id) {
        $date_debut=$jour." ".$debut;
        $date_fin=$jour." ".$fin;
        
        $dql  = "SELECT a as agenda, ";
        $dql .= "(select count(pa) from App\Entity\ParticipantAgenda pa where pa.agenda=a.id) as nb_inscrits, ";
        $dql .= "(select count(p2) from App\Entity\ParticipantAgenda p2 where p2.agenda=a.id and p2.participant=$id) as check, ";   
        $dql .= "(select count(p3) from App\Entity\ParticipantAgenda p3 LEFT JOIN  p3.agenda ag where p3.participant=$id and ag.dateDebut between '$date_debut' and '$date_fin' and  ag.dateFin between '$date_debut' and '$date_fin'  as inscrit ";
        $dql .= "FROM App\Entity\Agenda a ";
        $dql .= "WHERE a.dateDebut between '$date_debut' and '$date_fin' and  a.dateFin between '$date_debut' and '$date_fin'";
        $query = $this->em->createQuery($dql);
        return $query->getResult();
    }
    
    public function getParcours($jour,$debut,$fin,$id) {
        $date_debut=$jour." ".$debut;
        $date_fin=$jour." ".$fin;
        
        $dql  = "SELECT pa FROM App\Entity\ParticipantAgenda pa LEFT JOIN pa.agenda a ";
        $dql .= "WHERE pa.participant=$id ";
        $dql .= "AND a.dateDebut between '$date_debut' and '$date_fin' and  a.dateFin between '$date_debut' and '$date_fin'";
        $query = $this->em->createQuery($dql);
        return $query->getResult();
    }
 
    
}