From beecf8da4ba4453bf0d8b922bcbde4a5f8d43f6b Mon Sep 17 00:00:00 2001
From: Jean-Philippe Metivier <jean-philippe.metivier@unicaen.fr>
Date: Mon, 2 Dec 2024 13:30:09 +0100
Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20de=20correction=20correction=20de?=
 =?UTF-8?q?=20mail=20mal=20form=C3=A9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../Controller/SessionController.php          | 25 ++++++++++++++++---
 .../Notification/NotificationService.php      | 15 ++++++-----
 .../Service/Session/SessionService.php        |  9 ++++---
 .../view/formation/session/afficher.phtml     |  2 +-
 4 files changed, 38 insertions(+), 13 deletions(-)

diff --git a/module/Formation/src/Formation/Controller/SessionController.php b/module/Formation/src/Formation/Controller/SessionController.php
index 9e77bef1..9738a3c2 100644
--- a/module/Formation/src/Formation/Controller/SessionController.php
+++ b/module/Formation/src/Formation/Controller/SessionController.php
@@ -325,11 +325,30 @@ class SessionController extends AbstractActionController
         return $this->redirect()->toRoute('session/afficher', ['session' => $session->getId()], [], true);
     }
 
-    public function demanderRetourAction(): Response
+    public function demanderRetourAction(): ViewModel
     {
         $session = $this->getSessionService()->getRequestedSession($this);
-        $this->getSessionService()->demanderRetour($session);
-        return $this->redirect()->toRoute('session/afficher', ['session' => $session->getId()], [], true);
+        $mails = $this->getSessionService()->demanderRetour($session);
+
+        $probleme = [];
+        foreach ($mails as $individu => $mail) {
+            if ($mail === null) {
+                $probleme[] = $individu;
+            }
+        }
+        if (!empty($probleme)) {
+            $problemeTexte = "Les stagiaire·s suivant·es n'ont pas pu être prévenu·es : <ul>";
+            foreach ($probleme as $individu) $problemeTexte .= "<li>" . $individu . "</li>";
+            $problemeTexte .= "</ul>";
+        }
+
+        $vm = new ViewModel([
+            'title' => "Demande de retour",
+            'reponse' => "La demande a bien été effectuée",
+            'error' => $problemeTexte??null,
+        ]);
+        $vm->setTemplate('default/reponse');
+        return $vm;
     }
 
     public function cloturerAction(): Response
diff --git a/module/Formation/src/Formation/Service/Notification/NotificationService.php b/module/Formation/src/Formation/Service/Notification/NotificationService.php
index eb7a08dd..f894c2f4 100644
--- a/module/Formation/src/Formation/Service/Notification/NotificationService.php
+++ b/module/Formation/src/Formation/Service/Notification/NotificationService.php
@@ -294,12 +294,15 @@ class NotificationService
 
         $vars = $this->generateVariableArray($inscrit);
         $rendu = $this->getRenduService()->generateRenduByTemplateCode(MailTemplates::SESSION_DEMANDE_RETOUR, $vars);
-        $mail = $this->getMailService()->sendMail($inscrit->getIndividu()->getEmail(), $rendu->getSujet(), $rendu->getCorps(), 'Formation');
-        $mail->setMotsClefs([$session->generateTag(), $inscrit->generateTag(), $rendu->getTemplate()->generateTag()]);
-        $this->getMailService()->update($mail);
-        $session->addMail($mail);
-        $this->getObjectManager()->flush($session);
-        return $mail;
+        if ($inscrit->getIndividu() !== null) {
+            $mail = $this->getMailService()->sendMail($inscrit->getIndividu()->getEmail(), $rendu->getSujet(), $rendu->getCorps(), 'Formation');
+            $mail->setMotsClefs([$session->generateTag(), $inscrit->generateTag(), $rendu->getTemplate()->generateTag()]);
+            $this->getMailService()->update($mail);
+            $session->addMail($mail);
+            $this->getObjectManager()->flush($session);
+            return $mail;
+        }
+        return null;
     }
 
     /** Notification vers les inscrit·e ayant eu une absence */
diff --git a/module/Formation/src/Formation/Service/Session/SessionService.php b/module/Formation/src/Formation/Service/Session/SessionService.php
index 4d107be1..c95fd475 100644
--- a/module/Formation/src/Formation/Service/Session/SessionService.php
+++ b/module/Formation/src/Formation/Service/Session/SessionService.php
@@ -24,6 +24,7 @@ use Laminas\Mvc\Controller\AbstractActionController;
 use UnicaenApp\Exception\RuntimeException;
 use UnicaenEtat\Service\EtatInstance\EtatInstanceServiceAwareTrait;
 use UnicaenEtat\Service\EtatType\EtatTypeServiceAwareTrait;
+use UnicaenMail\Entity\Db\Mail;
 use UnicaenParametre\Service\Parametre\ParametreServiceAwareTrait;
 use UnicaenUtilisateur\Entity\Db\UserInterface;
 
@@ -419,18 +420,20 @@ class SessionService
         return $session;
     }
 
-    public function demanderRetour(Session $instance): Session
+    /** @return Mail[] $mails */
+    public function demanderRetour(Session $instance): array
     {
+        $mails = [];
         $this->getEtatInstanceService()->setEtatActif($instance, SessionEtats::ETAT_ATTENTE_RETOURS);
         $this->update($instance);
 
         $configuration = $this->getNotificationConfigurationService()->getNotificationConfigurationWithParams("SESSION", MailTemplates::SESSION_DEMANDE_RETOUR, "Demande de retour");
         if ($configuration === null OR $configuration->isActive()) {
             foreach ($instance->getListePrincipale() as $inscrit) {
-                $this->getNotificationService()->triggerDemandeRetour($inscrit);
+                $mails[$inscrit->getIndividu()->getDenomination()] = $this->getNotificationService()->triggerDemandeRetour($inscrit);
             }
         }
-        return $instance;
+        return $mails;
     }
 
     public function envoyerAbsence(Session $session, ?Inscription $inscription = null): Session
diff --git a/module/Formation/view/formation/session/afficher.phtml b/module/Formation/view/formation/session/afficher.phtml
index 3bb6045f..bdeb3186 100644
--- a/module/Formation/view/formation/session/afficher.phtml
+++ b/module/Formation/view/formation/session/afficher.phtml
@@ -153,7 +153,7 @@ $canMesSessions = $this->isAllowed(FormateurPrivileges::getResourceId(FormateurP
                     <br/>
                     <a <?php /** @see SessionController::demanderRetourAction() */ ?>
                             href="<?php echo $this->url('session/demander-retour', ['session' => $session->getId()], [], true); ?>"
-                            class="btn btn-primary">
+                            class="btn btn-primary ajax-modal" data-event="modification">
                         Demander les retours
                     </a>
                 <?php endif; ?>
-- 
GitLab