Commit e6a707d4 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Petites améliorations autour des notifs concernant les changements détectés...

Petites améliorations autour des notifs concernant les changements détectés lors de l'import (admission, corrections attendues).
parent e51c5226
Loading
Loading
Loading
Loading
+45 −57
Changes for module/Import/src/Import/Model/Service/ImportObservResultService.php: 45 added lines, 57 removed lines.
Original line number Diff line number Diff line
@@ -3,24 +3,24 @@
namespace Import\Model\Service;

use Application\Constants;
use Application\Service\Variable\VariableServiceAwareTrait;
use Depot\Rule\NotificationDepotVersionCorrigeeAttenduRule;
use Doctrine\ORM\Exception\ORMException;
use Import\Model\ImportObserv;
use Import\Model\ImportObservResult;
use Notification\NotificationResult;
use These\Entity\Db\These;
use Depot\Rule\NotificationDepotVersionCorrigeeAttenduRule;
use Import\Model\Repository\ImportObservResultRepository;
use Laminas\Log\LoggerAwareTrait;
use Notification\Notification;
use Notification\Service\NotifierServiceAwareTrait;
use These\Entity\Db\These;
use These\Service\Notification\TheseNotificationFactoryAwareTrait;
use These\Service\These\TheseServiceAwareTrait;
use Application\Service\Variable\VariableServiceAwareTrait;
use Doctrine\ORM\OptimisticLockException;
use Notification\Notification;
use UnicaenApp\Exception\RuntimeException;
use Laminas\Log\LoggerAwareTrait;

/**
 * @author Unicaen
 * @method ImportObservResultRepository getRepository()
 */
class ImportObservResultService extends \UnicaenDbImport\Entity\Db\Service\ImportObservResult\ImportObservResultService /** enc. @see \Application\Service\BaseService */
class ImportObservResultService extends \UnicaenDbImport\Entity\Db\Service\ImportObservResult\ImportObservResultService
{
    use TheseServiceAwareTrait;
    use NotifierServiceAwareTrait;
@@ -28,24 +28,14 @@ class ImportObservResultService extends \UnicaenDbImport\Entity\Db\Service\Impor
    use VariableServiceAwareTrait;
    use LoggerAwareTrait;

    /**
     * @var NotificationDepotVersionCorrigeeAttenduRule
     */
    protected $notificationDepotVersionCorrigeeAttenduRule;
    protected NotificationDepotVersionCorrigeeAttenduRule $notificationDepotVersionCorrigeeAttenduRule;

    /**
     * ImportObservResultService constructor.
     */
    public function __construct()
    {
        $this->setNotificationDepotVersionCorrigeeAttenduRule(new NotificationDepotVersionCorrigeeAttenduRule());
    }

    /**
     * @param NotificationDepotVersionCorrigeeAttenduRule $notificationDepotVersionCorrigeeAttenduRule
     * @return static
     */
    public function setNotificationDepotVersionCorrigeeAttenduRule($notificationDepotVersionCorrigeeAttenduRule)
    public function setNotificationDepotVersionCorrigeeAttenduRule(NotificationDepotVersionCorrigeeAttenduRule $notificationDepotVersionCorrigeeAttenduRule): static
    {
        $this->notificationDepotVersionCorrigeeAttenduRule = $notificationDepotVersionCorrigeeAttenduRule;

@@ -54,12 +44,8 @@ class ImportObservResultService extends \UnicaenDbImport\Entity\Db\Service\Impor

    /**
     * Traitement des résultats d'observation des changements lors de la synchro.
     *
     * @param \Import\Model\ImportObserv $importObserv Observation concernée
     * @param These|null $these
     * @return void
     */
    public function processImportObservForThese(ImportObserv $importObserv, These $these = null)
    public function processImportObservForThese(ImportObserv $importObserv, These $these = null): void
    {
        switch ($importObserv->getCode()) {
            case ImportObserv::CODE_RESULTAT_PASSE_A_ADMIS:
@@ -73,24 +59,19 @@ class ImportObservResultService extends \UnicaenDbImport\Entity\Db\Service\Impor
                break;
            default:
                throw new RuntimeException("Cas non prévu!");
                break;
        }
    }

    /**
     * Traitement des résultats d'observation des changements lors de la synchro :
     * notifications au sujet des thèses dont le résultat est passé à "admis".
     *
     * @param ImportObserv $importObserv
     * @param These|null $these
     * @throws \Doctrine\ORM\ORMException
     */
    private function processImportObservResultsForResultatAdmis(ImportObserv $importObserv, These $these = null)
    private function processImportObservResultsForResultatAdmis(ImportObserv $importObserv, These $these = null): void
    {
        $this->logger->info(sprintf(
        $this->logger->info(
            "# Traitement des résultats d'import : " .
            "notifications au sujet des thèses dont le résultat est passé à \"admis\""
        ));
        );

        $records = $this->getRepository()->fetchImportObservResults($importObserv, $these);

@@ -100,6 +81,10 @@ class ImportObservResultService extends \UnicaenDbImport\Entity\Db\Service\Impor
            return;
        }

        foreach ($records as $ior) {
            $this->logger->info(sprintf("- %s", $ior));
        }

        // Mise en forme des données pour le template du mail
        $data = $this->prepareDataForResultatAdmis($records);

@@ -110,20 +95,27 @@ class ImportObservResultService extends \UnicaenDbImport\Entity\Db\Service\Impor
            return;
        }

        // Notification des doctorants dont le résultat de la thèse est passé à Admis.
        //
        // Notification des doctorants dont le résultat de la thèse est passé à 'Admis'.
        //
        try {
            $notifs = $this->theseNotificationFactory->createNotificationsChangementResultatThesesAdmis($data);
            $notifs = $this->theseNotificationFactory->createNotificationsChangementResultatThesesAdmisDoctorant($data);
            foreach ($notifs as $notif) {
                $this->notifierService->trigger($notif);
            }
            $this->logAboutNotifications($notifs);
        } catch (\Notification\Exception\RuntimeException $e) {
            // aucun destinataire, todo : cas à gérer !
            // pb de destinataire, todo : cas à gérer !
            error_log("Impossible d'envoyer la notification aux doctorants dont le résultat de la thèse est passé à Admis !");
            error_log($e->getMessage());
            error_log($e->getTraceAsString());
        }

        // Notification concernant l'évolution des résultats de thèses.
        //
        // Notification des gestionnaires concernant l'évolution des résultats de thèses.
        //
        try {
            $notif = $this->theseNotificationFactory->createNotificationChangementResultatTheses($data);
            $notif = $this->theseNotificationFactory->createNotificationChangementResultatThesesGestionnaires($data);
            $result = $this->notifierService->trigger($notif);
            $this->logAboutNotifications([$notif]);

@@ -133,17 +125,19 @@ class ImportObservResultService extends \UnicaenDbImport\Entity\Db\Service\Impor
            }
            try {
                $this->getEntityManager()->flush($records);
            } catch (OptimisticLockException $e) {
            } catch (ORMException $e) {
                throw new RuntimeException("Enregistrement des ImportObservResultEtab impossible", null, $e);
            }
        } catch (\Notification\Exception\RuntimeException $e) {
            // aucun destinataire, todo : cas à gérer !
            // pb de destinataire, todo : cas à gérer !
            error_log("Impossible d'envoyer les notifications aux gestionnaires concernant l'évolution des résultats de thèses !");
            error_log($e->getMessage());
            error_log($e->getTraceAsString());
        }
    }

    /**
     * @param \Import\Model\ImportObservResult[] $importObservResults
     * @return array
     */
    private function prepareDataForResultatAdmis(array $importObservResults): array
    {
@@ -194,16 +188,13 @@ class ImportObservResultService extends \UnicaenDbImport\Entity\Db\Service\Impor
    /**
     * Traitement des résultats d'observation des changements lors de la synchro :
     * notifications au sujet des thèses pour lesquelles le témoin "correction autorisée" est passé à "facultative".
     *
     * @param \Import\Model\ImportObserv $importObserv
     * @param These|null $these
     */
    private function processImportObservResultsForCorrectionFacultative(ImportObserv $importObserv, These $these = null)
    private function processImportObservResultsForCorrectionFacultative(ImportObserv $importObserv, These $these = null): void
    {
        $this->logger->info(sprintf(
        $this->logger->info(
            "# Traitement des résultats d'import : " .
            "notifications au sujet des thèses pour lesquelles le témoin \"correction autorisée\" est passé à \"facultative\""
        ));
        );

        $records = $this->getRepository()->fetchImportObservResults($importObserv, $these);

@@ -213,16 +204,13 @@ class ImportObservResultService extends \UnicaenDbImport\Entity\Db\Service\Impor
    /**
     * Traitement des résultats d'observation des changements lors de la synchro :
     * notifications au sujet des thèses pour lesquelles le témoin "correction autorisée" est passé à "obligatoire".
     *
     * @param ImportObserv $importObserv
     * @param These|null $these
     */
    private function processImportObservResultsForCorrectionObligatoire(ImportObserv $importObserv, These $these = null)
    private function processImportObservResultsForCorrectionObligatoire(ImportObserv $importObserv, These $these = null): void
    {
        $this->logger->info(sprintf(
        $this->logger->info(
            "# Traitement des résultats d'import : " .
            "notifications au sujet des thèses pour lesquelles le témoin \"correction autorisée\" est passé à \"obligatoire\""
        ));
        );

        $records = $this->getRepository()->fetchImportObservResults($importObserv, $these);

@@ -235,7 +223,7 @@ class ImportObservResultService extends \UnicaenDbImport\Entity\Db\Service\Impor
     *
     * @param ImportObservResult[] $importObservResults
     */
    private function _processImportObservResultsForCorrection(array $importObservResults)
    private function _processImportObservResultsForCorrection(array $importObservResults): void
    {
        if (!count($importObservResults)) {
            $this->logger->info("Aucun résultat d'import à traiter.");
@@ -287,7 +275,7 @@ class ImportObservResultService extends \UnicaenDbImport\Entity\Db\Service\Impor

        try {
            $this->getEntityManager()->flush($recordsToFlush);
        } catch (OptimisticLockException $e) {
        } catch (ORMException $e) {
            throw new RuntimeException("Enregistrement des ImportObservResult impossible", null, $e);
        }
    }
@@ -295,7 +283,7 @@ class ImportObservResultService extends \UnicaenDbImport\Entity\Db\Service\Impor
    /**
     * @param Notification[] $notifs
     */
    private function logAboutNotifications(array $notifs)
    private function logAboutNotifications(array $notifs): void
    {
        foreach ($notifs as $notif) {
            $this->logger->info("Notification envoyée : " . $notif);
+2 −5
Changes for module/These/src/These/Entity/Db/TheseCorrectionAwareTrait.php: 2 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ use BadMethodCallException;
use DateInterval;
use DateTime;
use UnicaenApp\Exception\RuntimeException;
use Webmozart\Assert\Assert;

trait TheseCorrectionAwareTrait
{
@@ -123,14 +124,10 @@ trait TheseCorrectionAwareTrait
    /**
     * Change la valeur du témoin de corrections autorisées importé.
     * NB: cette méthode ne devrait pas être utilisée, sauf pour les tests unitaires.
     *
     * @param string|null $correctionAutorisee
     * @return self
     * @throws \Assert\AssertionFailedException
     */
    public function setCorrectionAutorisee(string $correctionAutorisee = null): self
    {
        Assertion::inArray($correctionAutorisee, [
        Assert::inArray($correctionAutorisee, [
            null,
            self::$CORRECTION_AUTORISEE_FACULTATIVE,
            self::$CORRECTION_AUTORISEE_OBLIGATOIRE,
+4 −13
Changes for module/These/src/These/Notification/ResultatTheseAdmisDoctorantNotification.php: 4 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -5,21 +5,15 @@ namespace These\Notification;
use Notification\Notification;
use These\Entity\Db\Interfaces\TheseAwareTrait;

class ResultatTheseAdmisNotification extends Notification
class ResultatTheseAdmisDoctorantNotification extends Notification
{
    use TheseAwareTrait;

    protected ?string $templatePath = 'these/these/mail/notif-resultat-admis-doctorant';

    /**
     * @var bool
     */
    protected $emailDoctorantAbsent;
    protected bool $emailDoctorantAbsent;

    /**
     * @return string
     */
    public function __toString()
    public function __toString(): string
    {
        $toString = parent::__toString();

@@ -30,10 +24,7 @@ class ResultatTheseAdmisNotification extends Notification
        return $toString;
    }

    /**
     * @return static
     */
    public function prepare()
    public function prepare(): static
    {
        $individu = $this->these->getDoctorant()->getIndividu();
        $to = $individu->getEmailContact() ?: $individu->getEmailPro() ?: $individu->getEmailUtilisateur();
+4 −6
Changes for module/These/src/These/Service/Notification/TheseNotificationFactory.php: 4 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -10,11 +10,10 @@ use Import\Model\ImportObservResult;
use Notification\Exception\RuntimeException;
use Notification\Factory\NotificationFactory;
use Notification\Notification;
use Notification\NotificationResult;
use These\Entity\Db\Acteur;
use These\Entity\Db\These;
use These\Notification\ChangementsResultatsThesesNotification;
use These\Notification\ResultatTheseAdmisNotification;
use These\Notification\ResultatTheseAdmisDoctorantNotification;

/**
 * Classe de construction de notifications par mail.
@@ -30,7 +29,7 @@ class TheseNotificationFactory extends NotificationFactory
     *
     * @param array $data Données concernant les thèses dont le résultat a changé
     */
    public function createNotificationChangementResultatTheses(array $data): Notification
    public function createNotificationChangementResultatThesesGestionnaires(array $data): Notification
    {
        $these = current($data)['these'];

@@ -54,10 +53,9 @@ class TheseNotificationFactory extends NotificationFactory
    /**
     * Crée les notifications à propos de résultats de thèses passés à 'Admis'.
     *
     * @param array $data
     * @return \Notification\Notification[]
     */
    public function createNotificationsChangementResultatThesesAdmis(array $data): array
    public function createNotificationsChangementResultatThesesAdmisDoctorant(array $data): array
    {
        $notifs = [];

@@ -70,7 +68,7 @@ class TheseNotificationFactory extends NotificationFactory
                throw new RuntimeException("Aucune adresse mail trouvée pour la Maison du doctorat (thèse {$these->getId()})");
            }

            $notif = new ResultatTheseAdmisNotification();
            $notif = new ResultatTheseAdmisDoctorantNotification();
            $notif->setThese($these);
            $notif->setEmailsBdd($emailsBdd);