src/Controller/AdminBundle/CRONController.php line 88

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: grego
  5.  * Date: 07/11/2022
  6.  * Time: 12:25
  7.  */
  8. namespace App\Controller\AdminBundle;
  9. use App\Entity\BatchEmail;
  10. use App\Entity\Campaign;
  11. use App\Entity\MailTemplate;
  12. use App\Entity\Participation;
  13. use App\Entity\User;
  14. use App\Manager\CossManager;
  15. use App\Manager\MailerManager;
  16. use App\Manager\MistralManager;
  17. use App\Manager\ReportManager;
  18. use App\Manager\UserManager;
  19. use App\Repository\BatchEmailRepository;
  20. use App\Repository\CampaignRepository;
  21. use App\Repository\MailTemplateRepository;
  22. use App\Repository\ParticipationRepository;
  23. use App\Repository\RecipientRepository;
  24. use DateTime;
  25. use Doctrine\ORM\EntityManagerInterface;
  26. use Exception;
  27. use PhpOffice\PhpSpreadsheet\Shared\Date;
  28. use Sonata\MediaBundle\Provider\Pool;
  29. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  30. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\HttpFoundation\Response;
  33. use Symfony\Component\HttpFoundation\StreamedResponse;
  34. use Symfony\Component\Routing\Annotation\Route;
  35. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  36. use Symfony\Contracts\Translation\TranslatorInterface;
  37. class CRONController extends AbstractController
  38. {
  39.     private $CRON_TOKEN "ffd6600b2594eafae3b2f0528a4cb51a";
  40.     /**
  41.      * @Route("/cron/debug/quality/{token}")
  42.      */
  43.     public function debugQuality($tokenEntityManagerInterface $entityManagerMailerManager $mailerManagerUserManager $userManagerCossManager $cossManagerMistralManager $mistralManagerTranslatorInterface $translatorParameterBagInterface $parameterBag){
  44.         set_time_limit(0);
  45.         ini_set('max_execution_time'3600); //3600 seconds = 1 heure
  46.         ini_set('memory_limit''-1');
  47.         $user $entityManager->getRepository(User::class)->find(145);
  48.         return New Response("OK"200);
  49.     }
  50.     /**
  51.      * @Route("/cron/send/batch/email/{token}")
  52.      */
  53.     public function sendBatchEmail($tokenRequest $requestEntityManagerInterface $entityManagerMailerManager $mailerManager)
  54.     {
  55.         if ($token == $this->CRON_TOKEN) {
  56.             $batchEmails $entityManager->getRepository(BatchEmail::class)->getPendingBatchEmailsPastFiveMinuts();
  57.             foreach ($batchEmails as $batchEmail) {
  58.                 $recipient $batchEmail->getRecipient();
  59.                 // Générer l'URL du répondant
  60.                 $recipientLink $this->generateUrl(
  61.                     'recipient_quiz', ['token' => $recipient->getToken()],
  62.                     UrlGeneratorInterface::ABSOLUTE_URL
  63.                 );
  64.                 $mailTemplate $entityManager->getRepository(MailTemplate::class)->findOneBy(array('type' => $batchEmail->getType(), 'program' => $recipient->getParticipation()->getCampaign()->getProgram()));
  65.                 $mailerManager->sendBatchEmail($recipient$mailTemplate$recipientLink$recipient->getParticipation()->getParticipant()->getLocale());
  66.                 $batchEmail->setStatus(BatchEmailRepository::STATUS_TREATED);
  67.                 $entityManager->persist($batchEmail);
  68.             }
  69.             $entityManager->flush();
  70.             return New Response(""200);
  71.         } else {
  72.             return New Response("access denied"401);
  73.         }
  74.     }
  75.     /**
  76.      * @Route("/cron/send/recall/email/{type}/{token}")
  77.      */
  78.     public function sendRecallEmail($type$tokenRequest $requestEntityManagerInterface $entityManagerMailerManager $mailerManagerTranslatorInterface $translator)
  79.     {
  80.         if ($token == $this->CRON_TOKEN) {
  81.             switch($type){
  82.                 case "1_week_start":
  83.                     // Recall 1 week after participation started only if no active recipients
  84.                     $participations $entityManager->getRepository(Participation::class)->getParticipationsWithNoActiveRecipientsAfter1Week();
  85.                     foreach($participations as $participation){
  86.                         $template $entityManager->getRepository(MailTemplate::class)->findOneBy(array('type' => MailTemplateRepository::PARTICIPANT_RECALL'program' => $participation->getCampaign()->getProgram()));
  87.                         // Générer l'URL du participant
  88.                         $participationLink $this->generateUrl('participation_recipients', ['campaignToken' => $participation->getCampaign()->getToken()],
  89.                             UrlGeneratorInterface::ABSOLUTE_URL
  90.                         );
  91.                         $mailObject $template->getObject();
  92.                         $mailObject str_replace('_FIRSTNAME_'$participation->getParticipant()->getFirstname(), $mailObject);
  93.                         $mailObject str_replace('_FULLNAME_'$participation->getParticipant()->getFirstname()." ".$participation->getParticipant()->getLastname(), $mailObject);
  94.                         $mailContent $template->getContent();
  95.                         $mailContent str_replace('_LINK_'$participationLink$mailContent);
  96.                         $mailContent str_replace('_FIRSTNAME_'$participation->getParticipant()->getFirstname(), $mailContent);
  97.                         $mailContent str_replace('_FULLNAME_'$participation->getParticipant()->getFirstname()." ".$participation->getParticipant()->getLastname(), $mailContent);
  98.                         $mailerManager->sendParticipantRecall($participation$mailObject$mailContent);
  99.                     }
  100.                     break;
  101.                 case "10_days_end":
  102.                     // Recall 10 days before participation ends for recipients who havn't answered
  103.                     $participations $entityManager->getRepository(Participation::class)->getParticipationsBefore10Days();
  104.                     foreach($participations as $participation){
  105.                         $template $entityManager->getRepository(MailTemplate::class)->findOneBy(array('type' => MailTemplateRepository::SECOND_RECALL'program' => $participation->getCampaign()->getProgram()));
  106.                         foreach ($participation->getRecipients() as $recipient){
  107.                             if($recipient->getStatus() == RecipientRepository::STATUS_ACTIVE && count($recipient->getQuizAnswers()) == 0){
  108.                                 // Générer l'URL du répondant
  109.                                 $recipientLink $this->generateUrl('quiz_answer', ['participationId' => $participation->getId() ,'token' => $recipient->getToken()],
  110.                                     UrlGeneratorInterface::ABSOLUTE_URL
  111.                                 );
  112.                                 $mailObject $template->getObject();
  113.                                 $mailObject str_replace('_FIRSTNAME_'$recipient->getFirstname(), $mailObject);
  114.                                 $mailObject str_replace('_FULLNAME_'$recipient->getFirstname()." ".$recipient->getLastname(), $mailObject);
  115.                                 $mailContent $template->getContent();
  116.                                 $mailContent str_replace('_LINK_'$recipientLink$mailContent);
  117.                                 $mailContent str_replace('_FIRSTNAME_'$recipient->getFirstname(), $mailContent);
  118.                                 $mailContent str_replace('_FULLNAME_'$recipient->getFirstname()." ".$recipient->getLastname(), $mailContent);
  119.                                 $mailerManager->sendRecipientRecall($recipient$mailObject$mailContent);
  120.                             }
  121.                         }
  122.                     }
  123.                 break;
  124.                 case "3_days_end":
  125.                     // Recall 3 days before participation ends for recipients who havn't answered
  126.                     $participations $entityManager->getRepository(Participation::class)->getParticipationsBefore3Days();
  127.                     foreach($participations as $participation){
  128.                         $template $entityManager->getRepository(MailTemplate::class)->findOneBy(array('type' => MailTemplateRepository::THIRD_RECALL'program' => $participation->getCampaign()->getProgram()));
  129.                         foreach ($participation->getRecipients() as $recipient){
  130.                             if($recipient->getStatus() == RecipientRepository::STATUS_ACTIVE && count($recipient->getQuizAnswers()) == 0){
  131.                                 // Générer l'URL du répondant
  132.                                 $recipientLink $this->generateUrl('quiz_answer', ['participationId' => $participation->getId() ,'token' => $recipient->getToken()],
  133.                                     UrlGeneratorInterface::ABSOLUTE_URL
  134.                                 );
  135.                                 $mailObject $template->getObject();
  136.                                 $mailObject str_replace('_FIRSTNAME_'$recipient->getFirstname(), $mailObject);
  137.                                 $mailObject str_replace('_FULLNAME_'$recipient->getFirstname()." ".$recipient->getLastname(), $mailObject);
  138.                                 $mailContent $template->getContent();
  139.                                 $mailContent str_replace('_LINK_'$recipientLink$mailContent);
  140.                                 $mailContent str_replace('_FIRSTNAME_'$recipient->getFirstname(), $mailContent);
  141.                                 $mailContent str_replace('_FULLNAME_'$recipient->getFirstname()." ".$recipient->getLastname(), $mailContent);
  142.                                 $mailerManager->sendRecipientRecall($recipient$mailObject$mailContent);
  143.                             }
  144.                         }
  145.                     }
  146.                 break;
  147.             }
  148.             return New Response(""200);
  149.         } else {
  150.             return New Response("access denied"401);
  151.         }
  152.     }
  153.     /**
  154.      * @Route("/cron/prepare/reports/{token}")
  155.      */
  156.     public function prepareReports($tokenRequest $requestEntityManagerInterface $entityManagerReportManager $reportManager)
  157.     {
  158.         if ($token == $this->CRON_TOKEN) {
  159.             $participationsWithReportsToGenerate $entityManager->getRepository(Participation::class)->getParticipationsWithReportsToGenerate();
  160.             foreach ($participationsWithReportsToGenerate as $participation){
  161.                 $reportManager->prepareParticipationReport($participation);
  162.                 // Check if campaign is over
  163.                 $campaign $participation->getCampaign();
  164.                 $otherParticipations $campaign->getParticipations();
  165.                 $endCampaign true;
  166.                 foreach ($otherParticipations as $otherParticipation) {
  167.                     if($otherParticipation->getReportData() == null){
  168.                         $endCampaign false;
  169.                     }
  170.                 }
  171.                 if($endCampaign){
  172.                     $campaign->setStatus(CampaignRepository::STATUS_FINISHED);
  173.                     $entityManager->persist($campaign);
  174.                     $entityManager->flush();
  175.                 }
  176.             }
  177.             return New Response(""200);
  178.         } else {
  179.             return New Response("access denied"401);
  180.         }
  181.     }
  182.     /**
  183.      * @Route("/cron/archive/campaigns/{token}")
  184.      */
  185.     public function archiveCampaigns($tokenRequest $requestEntityManagerInterface $entityManager)
  186.     {
  187.         if ($token == $this->CRON_TOKEN) {
  188.             $finishedCampaigns $entityManager->getRepository(Campaign::class)->findBy(array('status' => CampaignRepository::STATUS_FINISHED));
  189.             foreach ($finishedCampaigns as $campaign){
  190.                 $today = new DateTime();
  191.                 $end $campaign->getEndDate();
  192.                 $end $end->modify('+30 days');
  193.                 if($today $end){
  194.                     foreach ($campaign->getParticipations() as $participation) {
  195.                         $uid uniqid();
  196.                         $user $participation->getParticipant();
  197.                         $user->setFirstname("xxxxxxxx");
  198.                         $user->setLastname("xxxxxxxx");
  199.                         $user->setEmail("xxxxxxxx".$uid);
  200.                         $user->setUsername("xxxxxxxx".$uid);
  201.                         $user->setEnabled(false);
  202.                         $user->setRoles(array());
  203.                         $user->setDeletedAt(new \Datetime());
  204.                         $entityManager->persist($user);
  205.                         $entityManager->flush();
  206.                     }
  207.                     $campaign->setStatus(CampaignRepository::STATUS_ARCHIVED);
  208.                     $entityManager->persist($campaign);
  209.                     $entityManager->flush();
  210.                 }
  211.             }
  212.             return New Response(""200);
  213.         } else {
  214.             return New Response("access denied"401);
  215.         }
  216.     }
  217.     /**
  218.      * @Route("/cron/debug/mistral")
  219.      */
  220.     public function debugMistral(Request $requestEntityManagerInterface $entityManagerMistralManager $mistralManager)
  221.     {
  222.         $participation $entityManager->getRepository(Participation::class)->find(1);
  223.         $strengths = array();
  224.         $weaknesses = array();
  225.         foreach($participation->getReportData()['questions'] as $i => $question){
  226.             if($i == 0){
  227.                 $strengths $mistralManager->generateWordCloud($participation->getParticipant()->getLocale(), $question['answers']);
  228.             }
  229.             if($i == 1){
  230.                 $weaknesses $mistralManager->generateWordCloud($participation->getParticipant()->getLocale(), $question['answers']);
  231.             }
  232.         }
  233.         $reportData $participation->getReportData();
  234.         $reportData['strengths'] = $strengths['data']['words'];
  235.         $reportData['weaknesses'] = $weaknesses['data']['words'];
  236.         $participation->setReportData($reportData);
  237.         $entityManager->persist($participation);
  238.         $entityManager->flush();
  239.         die();
  240.     }
  241. }