notification du nombre de message non lu (fixes #6)

This commit is contained in:
afornerot 2019-10-28 16:55:36 +01:00
parent 04c061b7f6
commit bdb38dd301
2 changed files with 131 additions and 0 deletions

View File

@ -178,6 +178,30 @@ class InitDataCommand extends ContainerAwareCommand
elseif($entity&&!$activate_widmoodle) { elseif($entity&&!$activate_widmoodle) {
$this->entityManager->remove($entity); $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(); $this->entityManager->flush();
} }
} }

View File

@ -0,0 +1,107 @@
<?php
namespace Cadoles\WebsocketBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpKernel\KernelInterface;
use Doctrine\DBAL\Connection as DBALConnection;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Validator\Constraints\DateTime;
class CountMessageCommand extends Command
{ protected function configure()
{
$this
->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"=>"<p>Vous avez ".$cptnotread." messages non lus sur ".$this->appname."</p><p>Vous pouvez les consulter sur <a href='".$this->url."'>".$this->url."</a></p>",
"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('<fg=red>'.$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");
}
}