Commit de167cbe authored by Thomas Hamel's avatar Thomas Hamel
Browse files

modif logique envoi de mail aux doctorants pour formation spécifique ouverte

parent a301b9f5
Loading
Loading
Loading
Loading
+0 −17
Changes for module/Formation/src/Formation/Controller/FormationController.php: 0 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -56,23 +56,6 @@ class FormationController extends AbstractController
            $form->setData($data);
            if ($form->isValid()) {
                $this->getFormationService()->create($formation);
                //Envoi d'un mail lors de la création d'une formation spécifique à la liste de diffusion du site déclaré
                if($formation->getType() === Formation::TYPE_CODE_SPECIFIQUE && $formation->getSite() && $formation->getTypeStructure()){
                    try {
                        $notif = $this->formationNotificationFactory->createNotificationFormationSpecifiqueAjoutee($formation);
                        $this->notifierService->trigger($notif);
                        $destinataires = implode(', ', array_reduce(array_keys($notif->getTo()), function(array $accu, string $key) use ($notif) {
                            $accu[] = sprintf('%s (%s)', $notif->getTo()[$key], $key);
                            return $accu;
                        }, []));
                        $this->flashMessenger()->addInfoMessage("Une notification par mail a été envoyée aux doctorants appartenant à la liste de diffusion suivante : ".$destinataires);
                    } catch (RuntimeException $e) {
                        $ed = $formation->getTypeStructure()->getEcoleDoctorale();
                        $this->flashMessenger()->addErrorMessage("La session a bien été créee, cependant aucune liste de diffusion trouvée pour l'ED {$ed} (afin de notifier les doctorants).");
                    }
                }else if($formation->getType() === Formation::TYPE_CODE_SPECIFIQUE && !($formation->getSite() && $formation->getTypeStructure())){
                    $this->flashMessenger()->addWarningMessage("La session a bien été créee, cependant si vous voulez la notification des doctorants de l'ouverture de cette formation spécifique ({$formation->getLibelle()}), il faut renseigner le site organisateur et la structure associée.");
                }
            }
        }

+58 −1
Changes for module/Formation/src/Formation/Controller/SessionController.php: 58 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -3,7 +3,10 @@
namespace Formation\Controller;

use Application\Controller\AbstractController;
use Doctrine\ORM\PersistentCollection;
use Formation\Service\Notification\FormationNotificationFactoryAwareTrait;
use Notification\Exception\ExceptionInterface;
use Notification\Exception\RuntimeException;
use These\Entity\Db\These;
use Fichier\Service\Fichier\FichierStorageServiceAwareTrait;
use Fichier\Service\Storage\Adapter\Exception\StorageAdapterException;
@@ -217,7 +220,7 @@ class SessionController extends AbstractController
            if ($etat !== null) {
                $session->setEtat($etat);
                $this->sessionService->addHeurodatage($session, $etat);
                $this->getSessionService()->update($session);
//                $this->getSessionService()->update($session);

                switch ($session->getEtat()->getCode()) {
                    case Etat::CODE_FERME :
@@ -235,6 +238,21 @@ class SessionController extends AbstractController
                    case Etat::CODE_ANNULEE :
                        $this->triggerNotificationSessionAnnulee($session);
                        break;
                    case Etat::CODE_OUVERTE :
                        //Envoi d'un mail lors de la création d'une formation spécifique à la liste de diffusion de l'ED déclarée
                        $formation = $session->getFormation();
                        $formationSpecifique = $formation->getType() === Formation::TYPE_CODE_SPECIFIQUE;
                        $structuresValides = $session->getStructuresValides();

                        if ($formationSpecifique) {
                            if ($structuresValides instanceof PersistentCollection && !$structuresValides->isEmpty()) {
                                $this->triggerNotificationFormationSpecifiqueAjoutee($session);
                                break;
                            } else {
                                $this->flashMessenger()->addWarningMessage("Si vous voulez la notification des doctorants de l'ouverture des inscriptions de cette session (<b>{$formation->getLibelle()}</b>), il faut renseigner une ou des structures valides.");
                            }
                        }

                }
            }
        }
@@ -272,6 +290,45 @@ class SessionController extends AbstractController
        }
    }

    private function triggerNotificationFormationSpecifiqueAjoutee(Session $session)
    {
        $destinataires = [];
        /** @var SessionStructureValide $structureValide */
        foreach ($session->getStructuresValides() as $structureValide) {
            //on notifie les doctorants des structures non historisées
            if(!$structureValide->estHistorise()){
                try {
                    $notif = $this->formationNotificationFactory->createNotificationFormationSpecifiqueAjoutee($session->getFormation(), $structureValide);
                    $result = $this->notifierService->trigger($notif);
                    if(!empty($result->getErrorMessages())){
                        $this->flashMessenger()->addErrorMessage($result->getErrorMessages()[0]. " (".$structureValide->getStructure()->getEcoleDoctorale().").");
                    } else{
                        //Si il n'y a pas d'erreurs, on informe l'utilisateur des destinataires notifiés
                        $destinataires[] = $notif->getTo();
                    }
                } catch (RuntimeException $e) {
                    $errorMessages[] = $e->getMessage();
                }
            }
        }

        if (!empty($errorMessages)) {
            $errorMessage = implode(", ", $errorMessages);
            $this->flashMessenger()->addErrorMessage("Les doctorants de(s) ED <b>".$errorMessage."</b> n'ont pas reçu de mail, puisqu'aucune liste de diffusion n'a été trouvée");
        }

        //Si il y a au moins un destinataire qui a pu recevoir un mail
        if (!empty($destinataires)) {
            $destinataires = array_reduce($destinataires, function ($carry, $item) {
                $carry[] = implode(', ', $item);
                return $carry;
            }, []);
            $destinataires = implode(', ', $destinataires);

            $this->flashMessenger()->addInfoMessage("Une notification par mail (de l'ouverture des inscriptions) a été envoyée aux doctorants appartenant à la liste de diffusion suivante : " . $destinataires);
        }
    }

    private function triggerNotificationInscriptionClose(Session $session)
    {
        $nonClasses = $session->getNonClasses();
+7 −6
Changes for module/Formation/src/Formation/Service/Notification/FormationNotificationFactory.php: 7 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -9,10 +9,12 @@ use Formation\Entity\Db\Formateur;
use Formation\Entity\Db\Formation;
use Formation\Entity\Db\Inscription;
use Formation\Entity\Db\Session;
use Formation\Entity\Db\SessionStructureValide;
use Formation\Provider\Template\MailTemplates;
use Notification\Exception\RuntimeException;
use Notification\Factory\NotificationFactory;
use Notification\Notification;
use Structure\Entity\Db\Structure;
use UnicaenRenderer\Service\Rendu\RenduServiceAwareTrait;

/**
@@ -254,12 +256,11 @@ class FormationNotificationFactory extends NotificationFactory
    }

    /** FORMATIONS ******************************************************************************************************/
    public function createNotificationFormationSpecifiqueAjoutee(Formation $formation): Notification
    public function createNotificationFormationSpecifiqueAjoutee(Formation $formation, SessionStructureValide $structureValide): Notification
    {
        $ed = $formation->getTypeStructure()->getEcoleDoctorale();
        $site = $formation->getSite();
        $role = $this->roleService->getRepository()->findOneByCodeAndStructure(Role::CODE_DOCTORANT, $site->getStructure());
        $ng = $this->listeDiffusionService->createNameGenerator($ed, $role, $role->getStructure());
        $ed = $structureValide->getStructure()->getEcoleDoctorale();
        $role = $this->roleService->getRepository()->findOneBy(["code" => Role::CODE_DOCTORANT]);
        $ng = $this->listeDiffusionService->createNameGenerator($ed, $role, null);
        $domain = $this->listeDiffusionService->getEmailDomain();
        $ng->setDomain($domain);

@@ -277,7 +278,7 @@ class FormationNotificationFactory extends NotificationFactory

        //si une liste a été récupérée et qu'elle n'est pas active
        if(!$mail || !in_array($mail, $adressesListesActives)){
            throw new RuntimeException("Aucune liste de diffusion trouvée pour l'ED {$ed}.");
            throw new RuntimeException($ed);
        }

        $notif = new Notification();