Commit f90b734d authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Nettoyage de redondance du service AgentService + poursuite refactoring

parent 86447ea2
Loading
Loading
Loading
Loading
+1 −1
Changes for documentation/002-BD_EMC2.sql: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -495,7 +495,7 @@ create table formation_instance_presence
    histo_destruction timestamp,
    histo_destructeur_id integer constraint formation_instance_presence_user_id_fk_3 references unicaen_utilisateur_user
);
create unique index formation_instance_presence_id_uindex on formation_instance_presence (id);
create unique index formation_instance_presence_id_uindex on formation_presence (id);



+34 −46
Changes for module/Application/src/Application/Service/Agent/AgentService.php: 34 added lines, 46 removed lines.
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@ use Structure\Entity\Db\Structure;
use Structure\Entity\Db\StructureGestionnaire;
use Structure\Entity\Db\StructureResponsable;
use Structure\Service\Structure\StructureServiceAwareTrait;
use UnicaenApp\Exception\LogicException;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\EntityManagerAwareTrait;
use UnicaenUtilisateur\Entity\Db\User;
@@ -183,7 +182,7 @@ class AgentService {
        //en utilisant l'username si echec
        $qb = $this->getEntityManager()->getRepository(Agent::class)->createQueryBuilder('agent')
            ->andWhere('agent.login = :username')
            ->setParameter('username', $user->getUsername());
            ->setParameter('username', $user->getUsername())
        ;
        try {
            $result = $qb->getQuery()->getOneOrNullResult();
@@ -237,7 +236,7 @@ class AgentService {
            ->orderBy('agent.nomUsuel, agent.prenom', 'ASC')
        ;

        if ($structures !== null) {
        if (!empty($structures)) {
            $qb = $qb->andWhere('affectation.structure IN (:structures)')
                ->setParameter('structures', $structures);
        }
@@ -257,7 +256,7 @@ class AgentService {
            ->addSelect('forcage')->join('agent.structuresForcees', 'forcage')
            ->andWhere('forcage.histoDestruction IS NULL');

        if ($structures !== null) {
        if (!empty(null)) {
            $qb = $qb->andWhere('forcage.structure IN (:structures)')
                ->setParameter('structures', $structures);
        }
@@ -304,15 +303,9 @@ class AgentService {

    /** Recuperation des supérieures et autorités  *********************************************************************/

    /**
     *
     * @var Agent $agent
     * @return array
     */
    public function computeSuperieures(Agent $agent) : array
    public function computeComplements(Agent $agent, string $type) : array
    {
       //checking complement
        $complements = $this->getComplementService()->getCompelementsByAttachement(Agent::class, $agent->getId(), Complement::COMPLEMENT_TYPE_RESPONSABLE);
        $complements = $this->getComplementService()->getCompelementsByAttachement(Agent::class, $agent->getId(), $type);
        $liste = [];
        if (!empty($complements)) {
            foreach ($complements as $complement) {
@@ -320,6 +313,16 @@ class AgentService {
                if ($superieure !== null) $liste["complement_" . $complement->getId()] = $superieure;
            }
        }
        return $liste;
    }

    /**
     * @param Agent $agent
     * @return array
     */
    public function computeSuperieures(Agent $agent) : array
    {
        $liste = $this->computeComplements($agent, Complement::COMPLEMENT_TYPE_RESPONSABLE);
        if (!empty($liste)) return $liste;

       //checking structure
@@ -352,15 +355,7 @@ class AgentService {
     */
    public function computeAutorites(Agent $agent, ?array $superieurs = null) : array
    {
        //checking complement
        $complements = $this->getComplementService()->getCompelementsByAttachement(Agent::class, $agent->getId(), Complement::COMPLEMENT_TYPE_AUTORITE);
        $liste = [];
        if (!empty($complements)) {
            foreach ($complements as $complement) {
                $superieure = $this->getAgent($complement->getComplementId());
                if ($superieure !== null) $liste["complement_" . $complement->getId()] = $superieure;
            }
        }
        $liste = $this->computeComplements($agent, Complement::COMPLEMENT_TYPE_AUTORITE);
        if (!empty($liste)) return $liste;

        // fetching superieurs if not given
@@ -491,31 +486,36 @@ class AgentService {
    /** FONCTION POUR LES RÔLES AUTOMATIQUES **************************************************************************/

    /**
     * @param Complement[] $complements
     * @return User[]
     */
    public function getUsersInSuperieurs() : array
    public function getUsersInComplements(array $complements) : array
    {
        $qb = $this->getEntityManager()->getRepository(Complement::class)->createQueryBuilder("complement")
            ->andWhere('complement.histoDestruction IS NULL')
            ->andWhere('complement.type = :SUPERIEUR')
            ->setParameter('SUPERIEUR', Complement::COMPLEMENT_TYPE_RESPONSABLE)
        ;
        $result = $qb->getQuery()->getResult();

        /** @var Complement[] $result */

        $ids = [];
        foreach ($result as $item) $ids[$item->getComplementId()] = $item->getComplementId();
        foreach ($complements as $item) $ids[$item->getComplementId()] = $item->getComplementId();

        $users = [];
        foreach ($ids as $id) {
            if ($this->getAgent($id) !== null) { $users[] = $this->getAgent($id)->getUtilisateur(); }
            //else {var_dump("No agent with id = ".$id);}
        }

        return $users;
    }

    /**
     * @return User[]
     */
    public function getUsersInSuperieurs() : array
    {
        $qb = $this->getEntityManager()->getRepository(Complement::class)->createQueryBuilder("complement")
            ->andWhere('complement.histoDestruction IS NULL')
            ->andWhere('complement.type = :SUPERIEUR')
            ->setParameter('SUPERIEUR', Complement::COMPLEMENT_TYPE_RESPONSABLE)
        ;
        $result = $qb->getQuery()->getResult();
        return $this->getUsersInComplements($result);
    }

    /**
     * @param User|null $user
     * @return Complement[]|null
@@ -549,19 +549,7 @@ class AgentService {
            ->setParameter('AUTORITE', Complement::COMPLEMENT_TYPE_AUTORITE)
        ;
        $result = $qb->getQuery()->getResult();

        /** @var Complement[] $result */

        $ids = [];
        foreach ($result as $item) $ids[$item->getComplementId()] = $item->getComplementId();

        $users = [];
        foreach ($ids as $id) {
            if ($this->getAgent($id) !== null) { $users[] = $this->getAgent($id)->getUtilisateur(); }
            //else {var_dump("No agent with id = ".$id);}
        }

        return $users;
        return $this->getUsersInComplements($result);
    }

    /**
+1 −1
Changes for module/Formation/config/merged/formateur.config.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ use Formation\Form\Formateur\FormateurHydrator;
use Formation\Form\Formateur\FormateurHydratorFactory;
use Formation\Provider\Privilege\FormationinstancePrivileges;
use Formation\Service\Formateur\FormateurService;
use Formation\Service\FormationInstanceFormateur\FormateurServiceFactory;
use Formation\Service\Formateur\FormateurServiceFactory;
use Laminas\Router\Http\Segment;
use UnicaenPrivilege\Guard\PrivilegeController;

+12 −12
Changes for module/Formation/config/merged/presence.config.php: 12 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -2,20 +2,20 @@

namespace Formation;

use Formation\Controller\FormationInstancePresenceController;
use Formation\Controller\FormationInstancePresenceControllerFactory;
use Formation\Controller\PresenceController;
use Formation\Controller\PresenceControllerFactory;
use Formation\Provider\Privilege\FormationinstancepresencePrivileges;
use Formation\Service\FormationInstancePresence\FormationInstancePresenceService;
use Formation\Service\FormationInstancePresence\FormationInstancePresenceServiceFactory;
use UnicaenPrivilege\Guard\PrivilegeController;
use Formation\Service\Presence\PresenceService;
use Formation\Service\Presence\PresenceServiceFactory;
use Laminas\Router\Http\Segment;
use UnicaenPrivilege\Guard\PrivilegeController;

return [
    'bjyauthorize' => [
        'guards' => [
            PrivilegeController::class => [
                [
                    'controller' => FormationInstancePresenceController::class,
                    'controller' => PresenceController::class,
                    'action' => [
                        'renseigner-presences',
                    ],
@@ -24,7 +24,7 @@ return [
                    ],
                ],
                [
                    'controller' => FormationInstancePresenceController::class,
                    'controller' => PresenceController::class,
                    'action' => [
                        'toggle-presence',
                        'toggle-presences',
@@ -46,7 +46,7 @@ return [
                        'options' => [
                            'route'    => '/renseigner-presences/:formation-instance',
                            'defaults' => [
                                'controller' => FormationInstancePresenceController::class,
                                'controller' => PresenceController::class,
                                'action'     => 'renseigner-presences',
                            ],
                        ],
@@ -56,7 +56,7 @@ return [
                        'options' => [
                            'route'    => '/toggle-presence/:journee/:inscrit',
                            'defaults' => [
                                'controller' => FormationInstancePresenceController::class,
                                'controller' => PresenceController::class,
                                'action'     => 'toggle-presence',
                            ],
                        ],
@@ -66,7 +66,7 @@ return [
                        'options' => [
                            'route'    => '/toggle-presences/:mode/:inscrit',
                            'defaults' => [
                                'controller' => FormationInstancePresenceController::class,
                                'controller' => PresenceController::class,
                                'action'     => 'toggle-presences',
                            ],
                        ],
@@ -78,12 +78,12 @@ return [

    'service_manager' => [
        'factories' => [
            FormationInstancePresenceService::class => FormationInstancePresenceServiceFactory::class,
            PresenceService::class => PresenceServiceFactory::class,
        ],
    ],
    'controllers'     => [
        'factories' => [
            FormationInstancePresenceController::class => FormationInstancePresenceControllerFactory::class,
            PresenceController::class => PresenceControllerFactory::class,
        ],
    ],
    'form_elements' => [
+7 −7
Changes for module/Formation/src/Formation/Controller/ImportationLagafController.php: 7 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -11,20 +11,20 @@ use Formation\Entity\Db\FormationInstance;
use Formation\Entity\Db\FormationInstanceFrais;
use Formation\Entity\Db\FormationInstanceInscrit;
use Formation\Entity\Db\FormationInstanceJournee;
use Formation\Entity\Db\FormationInstancePresence;
use Formation\Entity\Db\LAGAFStagiaire;
use Formation\Entity\Db\Presence;
use Formation\Service\Formation\FormationServiceAwareTrait;
use Formation\Service\FormationGroupe\FormationGroupeServiceAwareTrait;
use Formation\Service\FormationInstance\FormationInstanceServiceAwareTrait;
use Formation\Service\FormationInstanceFrais\FormationInstanceFraisServiceAwareTrait;
use Formation\Service\FormationInstanceInscrit\FormationInstanceInscritServiceAwareTrait;
use Formation\Service\FormationInstanceJournee\FormationInstanceJourneeServiceAwareTrait;
use Formation\Service\FormationInstancePresence\FormationInstancePresenceAwareTrait;
use Formation\Service\HasFormationCollection\HasFormationCollectionServiceAwareTrait;
use Formation\Service\Presence\PresenceAwareTrait;
use Formation\Service\Stagiaire\StagiaireServiceAwareTrait;
use UnicaenEtat\Service\Etat\EtatServiceAwareTrait;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use UnicaenEtat\Service\Etat\EtatServiceAwareTrait;

class ImportationLagafController extends AbstractActionController {
    use EtatServiceAwareTrait;
@@ -34,7 +34,7 @@ class ImportationLagafController extends AbstractActionController {
    use FormationInstanceJourneeServiceAwareTrait;
    use FormationInstanceInscritServiceAwareTrait;
    use FormationInstanceFraisServiceAwareTrait;
    use FormationInstancePresenceAwareTrait;
    use PresenceAwareTrait;
    use StagiaireServiceAwareTrait;
    use HasFormationCollectionServiceAwareTrait;

@@ -495,7 +495,7 @@ class ImportationLagafController extends AbstractActionController {
            }
        }

        $presences_tmp = $this->getFormationInstancePresenceService()->getFormationsInstancesPresences();
        $presences_tmp = $this->getPresenceService()->getPresences();
        $olds = [];
        foreach ($presences_tmp as $presence) {
            if ($presence->getSource() === 'LAGAF') {
@@ -518,13 +518,13 @@ class ImportationLagafController extends AbstractActionController {

            if ($journee !== null AND $inscrit !== null) {
                if (!isset($olds[$st_journee . "-" . $st_inscrit])) {
                    $presence = new FormationInstancePresence();
                    $presence = new Presence();
                    $presence->setJournee($journee);
                    $presence->setInscrit($inscrit);
                    $presence->setPresent($data[$position_presence] === "1");
                    $presence->setSource('LAGAF');
                    $presence->setIdSource($st_journee . "-" . $st_inscrit);
                    $this->getFormationInstancePresenceService()->create($presence);
                    $this->getPresenceService()->create($presence);
                    $presences[] = $presence;
                }
            } else {
Loading