From bdb38dd301e0aa41f59fbe7f1f6af0f5e13c96c8 Mon Sep 17 00:00:00 2001 From: afornerot Date: Mon, 28 Oct 2019 16:55:36 +0100 Subject: [PATCH] notification du nombre de message non lu (fixes #6) --- .../CronBundle/Command/InitDataCommand.php | 24 ++++ .../Command/CountMessageCommand.php | 107 ++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 src/ninegate-1.0/src/Cadoles/WebsocketBundle/Command/CountMessageCommand.php diff --git a/src/ninegate-1.0/src/Cadoles/CronBundle/Command/InitDataCommand.php b/src/ninegate-1.0/src/Cadoles/CronBundle/Command/InitDataCommand.php index 92bf6baf..5c4600d2 100644 --- a/src/ninegate-1.0/src/Cadoles/CronBundle/Command/InitDataCommand.php +++ b/src/ninegate-1.0/src/Cadoles/CronBundle/Command/InitDataCommand.php @@ -178,6 +178,30 @@ class InitDataCommand extends ContainerAwareCommand elseif($entity&&!$activate_widmoodle) { $this->entityManager->remove($entity); } + + // Job notification message non lus + // Toute les 24h à 5h00 + $websocket_activate = $this->getContainer()->getParameter('websocket_activate'); + $entity = $this->entityManager->getRepository('CadolesCronBundle:Cron')->find(2000); + if(!$entity&&($portal_activate||$calendar_activate)) { + $entity = new Cron; + $nextdate=$entity->getSubmitdate(); + $nextdate->setTime(5,0); + $entity->setCommand("Websocket:CountMessage"); + $entity->setDescription("Notifier les utilisateurs des messages non lus"); + $entity->setId(2000); + $entity->setStatut(2); + $entity->setRepeatcall(0); + $entity->setRepeatexec(0); + $entity->setRepeatinterval(86400); + $entity->setNextexecdate($nextdate); + $this->entityManager->persist($entity); + } + elseif($entity&&!($websocket_activate)) { + $this->entityManager->remove($entity); + } + + // On flush $this->entityManager->flush(); } } diff --git a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Command/CountMessageCommand.php b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Command/CountMessageCommand.php new file mode 100644 index 00000000..891125af --- /dev/null +++ b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Command/CountMessageCommand.php @@ -0,0 +1,107 @@ +setName('Websocket:CountMessage') + ->setDescription('Count number of messages not read and send mail notification') + ->setHelp('Count number of message') + ->addArgument('cronid', InputArgument::OPTIONAL, 'ID Cron Job') + ->addArgument('lastchance', InputArgument::OPTIONAL, 'Lastchance to run the cron') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->container = $this->getApplication()->getKernel()->getContainer(); + $this->em = $this->container->get('doctrine')->getEntityManager(); + $this->output = $output; + $this->filesystem = new Filesystem(); + $this->rootlog = $this->container->get('kernel')->getRootDir()."/../var/logs/"; + $this->noreply = $this->container->getParameter('noreply'); + + + $this->writelnred(''); + $this->writelnred('== Websocket:Countmessage'); + $this->writelnred('=========================================================================================================='); + + $now=new \DateTime('now'); + $fgdebug = false; + + // Config + $this->appname = $this->em->getRepository("CadolesCoreBundle:Config")->findOneBy(["id"=>"appname"])->getValue(); + $this->url= "https://".$this->container->getParameter('weburl')."/".$this->container->getParameter('alias'); + + // Pour chaque utilisateur + $users=$this->em->getRepository("CadolesCoreBundle:User")->findAll(); + foreach($users as $user) { + $cptnotread=0; + // Pour chaque group de l'utilisateur + foreach($user->getGroups() as $usergroup) { + // On calcule le nombre de message non lu pour l'utilisateur' + $group=$usergroup->getGroup(); + $qb = $this->em->createQueryBuilder(); + $tm = $qb ->select($qb->expr()->count('m.id')) + ->from('CadolesWebsocketBundle:Message', 'm') + ->where('m.group = :group') + ->andWhere('m.user != :user') + ->setParameter('group', $group) + ->setParameter('user', $user) + ->getQuery()->getSingleScalarResult(); + + $qb = $this->em->createQueryBuilder(); + $tr = $qb ->select($qb->expr()->count('m.id')) + ->from('CadolesWebsocketBundle:Message', 'm') + ->where('m.group = :group') + ->andWhere('m.user != :user') + ->andWhere(':user MEMBER OF m.readers') + ->setParameter('group', $group) + ->setParameter('user', $user) + ->getQuery()->getSingleScalarResult(); + + if($tm-$tr>0) $cptnotread+=($tm-$tr); + } + if($cptnotread>0) { + $this->writeln($user->getUsername()." notifié de ".$cptnotread." messages non lus"); + + $template="template"; + + $mail_params=array( + "subject" => $this->appname." : Messages non lus", + "body_html"=>"

Vous avez ".$cptnotread." messages non lus sur ".$this->appname."

Vous pouvez les consulter sur ".$this->url."

", + "body_text"=>"Vous avez ".$cptnotread." messages non lus sur ".$this->appname."\nVous pouvez les consulter sur ".$this->url, + ); + + $message = $this->container->get('cadoles.core.service.mail'); + $message->sendEmail($template, $mail_params, $user->getEmail(), $this->noreply, $this->appname); + } + + } + $this->writeln(''); + return 1; + } + + + private function writelnred($string) { + $this->output->writeln(''.$string.''); + $this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n"); + } + private function writeln($string) { + $this->output->writeln($string); + $this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n"); + } +}