Преглед изворни кода

automatique update des education notation + recalcul des notes en post transform

Vincent GUFFON пре 3 година
родитељ
комит
55ddf5c757

+ 5 - 0
.env

@@ -80,3 +80,8 @@ LOCK_DSN=semaphore
 ###> symfony/mailer ###
 MAILER_DSN=smtp://localhost
 ###< symfony/mailer ###
+
+###> elasticsearch ###
+ELASTICSEARCH_HOST=localhost
+ELASTICSEARCH_PORT=9200
+###< elasticsearch ###

+ 7 - 1
config/services.yaml

@@ -99,4 +99,10 @@ services:
 
     App\DataPersister\Organization\OrganizationDataPersister:
         bind:
-            $decorated: '@api_platform.doctrine.orm.data_persister'
+            $decorated: '@api_platform.doctrine.orm.data_persister'
+
+    #########################################
+    ##  ELASTIC SERVICE ##
+    App\Service\Elasticsearch\EducationNotationUpdate:
+        arguments:
+            - '@fos_elastica.object_persister.search.educationNotation'

+ 68 - 0
src/EventSubscriber/ElasticaPostTransformSubscriber.php

@@ -0,0 +1,68 @@
+<?php
+declare(strict_types=1);
+
+namespace App\EventSubscriber;
+
+use App\Entity\Education\EducationNotation;
+use App\Service\Education\EducationNotationUtils;
+use FOS\ElasticaBundle\Event\PostTransformEvent;
+use FOS\ElasticaBundle\Event\PreTransformEvent;
+use FOS\ElasticaBundle\Event\TransformEvent;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+/**
+ * Class ElasticaPostTransformSubscriber : Permet de réaliser des traitements avant et après un update d'un objet dans ES
+ * @package App\EventSubscriber
+ */
+final class ElasticaPostTransformSubscriber implements EventSubscriberInterface
+{
+    public function __construct(
+        private EducationNotationUtils $educationNotationUtils
+    )
+    {
+    }
+
+    /**
+     * ne se déclenche qu'après le post validate d'api platform
+     */
+    public static function getSubscribedEvents()
+    {
+        return [
+            PreTransformEvent::class => 'doPostTransform',
+            PostTransformEvent::class => 'doPreTransform'
+        ];
+    }
+
+    /**
+     * Evénement se passant avant l'update d'un index ES
+     * @param TransformEvent $event
+     */
+    public function doPreTransform(TransformEvent $event)
+    {
+
+    }
+
+    /**
+     * Evénement se passant après l'update d'un index ES
+     * @param TransformEvent $event
+     */
+    public function doPostTransform(TransformEvent $event)
+    {
+        $document = $event->getDocument();
+        $object = $event->getObject();
+
+        if ($object instanceof EducationNotation) {
+            $this->educationNotationTransform($document, $object);
+        }
+    }
+
+    /**
+     * Recalcul et met à jour les note d'une évaluation suivant la note maximal configurée par la structure
+     * @param $document
+     * @param $object
+     */
+    private function educationNotationTransform($document, EducationNotation $educationNotation){
+        $document->set('note_origine', $this->educationNotationUtils->getNotationOriginal($educationNotation));
+        $document->set('note_recalcul', $this->educationNotationUtils->getNotationTransformed($educationNotation));
+    }
+}

+ 18 - 3
src/Message/Handler/Parameters/AverageChangeHandler.php

@@ -4,17 +4,32 @@ declare(strict_types=1);
 namespace App\Message\Handler\Parameters;
 
 use App\Message\Command\Parameters\AverageChange;
+use App\Repository\Education\EducationNotationRepository;
+use App\Repository\Organization\ParametersRepository;
+use App\Service\Elasticsearch\EducationNotationUpdate;
 use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
 
+/**
+ * Classe AverageChangeHandler qui traite les messages AverageChange
+ */
 class AverageChangeHandler implements MessageHandlerInterface
 {
     public function __construct(
+        private EducationNotationUpdate $educationNotationUpdate,
+        private EducationNotationRepository $educationNotationRepository,
+        private ParametersRepository $parametersRepository
     ) {}
 
+    /**
+     * Fonction permettant de remettre à jour les notes dans elasticsearch par rapport à la note maximale changée
+     * @param AverageChange $averageChange
+     */
     public function __invoke(AverageChange $averageChange)
     {
-        /**
-         * @todo Si on utilise toujours ES, il faut ici repopuler les educationNotations qui ont changées afin de mettre les bonnes notes
-         */
+        $parameter = $this->parametersRepository->find($averageChange->getParametersId());
+        if($parameter){
+            $educationNotations = $this->educationNotationRepository->findAllEducationNotation($parameter->getOrganization());
+            $this->educationNotationUpdate->update($educationNotations);
+        }
     }
 }

+ 42 - 0
src/Repository/Education/EducationNotationRepository.php

@@ -0,0 +1,42 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Repository\Education;
+
+use App\Entity\Education\EducationNotation;
+use App\Entity\Organization\Organization;
+use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
+use Doctrine\Persistence\ManagerRegistry;
+
+class EducationNotationRepository extends ServiceEntityRepository
+{
+    public function __construct(ManagerRegistry $registry)
+    {
+        parent::__construct($registry, EducationNotation::class);
+    }
+
+    /**
+     * Permet de récupérer toutes les évaluations d'une structure sans tenir compte de l'année des education student
+     * @param Organization $organization
+     * @return array|int|mixed|string
+     */
+    public function findAllEducationNotation(Organization $organization){
+        $this->_em->getFilters()->disable('activity_year_filter');
+
+        $queryBuilder = $this->createQueryBuilder('education_notation');
+        $queryBuilder
+            ->select('education_notation')
+            ->innerJoin('education_notation.educationStudent','educationStudent')
+            ->innerJoin('educationStudent.access','access')
+            ->innerJoin('access.organization','organization')
+            ->andWhere('organization.id=:organization_id')
+            ->setParameter('organization_id', $organization->getId())
+        ;
+
+        $query = $queryBuilder->getQuery();
+        $results = $query->getResult();
+
+        $this->_em->getFilters()->enable('activity_year_filter');
+        return $results;
+    }
+}

+ 52 - 0
src/Service/Education/EducationNotationUtils.php

@@ -0,0 +1,52 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Service\Education;
+
+use App\Entity\Education\EducationNotation;
+use App\Enum\Education\TypeCriteriaEnum;
+
+/**
+ * Classe EducationNotationUtils qui contiens les fonctions relatives aux évaluations d'un enseignement
+ */
+class EducationNotationUtils
+{
+    /**
+     * Fonction permettant de retrouver la note calculé par rapport à la note maximale possible définie par la structure
+     * @param EducationNotation $educationNotation
+     * @return float|null
+     */
+    public function getNotationTransformed(EducationNotation $educationNotation)
+    {
+        $noteMax = $educationNotation->getEducationStudent()->getAccess()->getOrganization()->getParameters()->getAverage();
+        return $this->calculNotationByAMaxNote($educationNotation, $noteMax);
+    }
+
+    /**
+     * Fonction permettant de retrouver la note calculé par rapport à la note maximale du critère de notation possible définie par la structure
+     * @param EducationNotation $educationNotation
+     * @return float|null
+     */
+    public function getNotationOriginal(EducationNotation $educationNotation)
+    {
+        if(!$educationNotation->getCriteriaNotation()) return null;
+        $noteMax = $educationNotation->getCriteriaNotation()->getNoteMax();
+        return $this->calculNotationByAMaxNote($educationNotation, $noteMax);
+    }
+
+    /**
+     * Effectue un ratio de la note sur 100 à la base pour la cacluler par rapport à la note maximale passée en paramètre.
+     * @param $educationNotation
+     * @param $noteMax
+     * @return float|null
+     */
+    private function calculNotationByAMaxNote($educationNotation, $noteMax){
+        if(is_null($educationNotation->getNote())) return null;
+        if(!$educationNotation->getCriteriaNotation()) return null;
+        if($educationNotation->getCriteriaNotation()->getType() !== TypeCriteriaEnum::WITH_NOTATION()->getValue()) return null;
+
+        $note = (float)(($educationNotation->getNote() * $noteMax) / 100);
+
+        return round ( $note , 2 );
+    }
+}

+ 24 - 0
src/Service/Elasticsearch/EducationNotationUpdate.php

@@ -0,0 +1,24 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Service\Elasticsearch;
+
+use FOS\ElasticaBundle\Persister\ObjectPersister;
+
+/**
+ * Classe EducationNotationUpdate qui s'occupe de la mise à jour ES pour EducationNotation
+ */
+class EducationNotationUpdate
+{
+    public function __construct(private ObjectPersister $objectPersisterOrganization)
+    {
+    }
+
+    /**
+     * Effectue le replace many des educations notations
+     * @param $educationNotations
+     */
+    public function update(array $educationNotations){
+        $this->objectPersisterOrganization->replaceMany($educationNotations);
+    }
+}