<?php
namespace App\Controller;
use App\Entity\Appel;
use App\Entity\Apprenant;
use App\Entity\Client;
use App\Entity\Compagne;
use App\Entity\Cours;
use App\Entity\Inscription;
use App\Entity\Priorite;
use App\Entity\Projet;
use App\Entity\Tache;
use App\Entity\Utilisateur;
use App\Form\CompagneType;
use App\Service\NewDoceboApi;
use App\Service\Notification;
use App\Service\Helper;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
/**
* Compagne controller.
*
* @Route("campagne")
*/
class CampagneController extends AbstractController
{
/**
* @Route("/liste-des-campagnes/OP", name="campagne_list")
*/
public function campagne_list()
{
$compagnes = $this->getDoctrine()->getRepository(Compagne::class)->findActivesWithClient();
return $this->render('campagne/index.html.twig', array_merge(
['compagnes' => $compagnes],
$this->countsCompagnes($compagnes)
));
}
/**
* Compteurs (tâches, appels) par campagne pour les listes
*/
private function countsCompagnes(array $compagnes)
{
$ids = array_map(function ($compagne) {
return $compagne->getId();
}, $compagnes);
$tacheRepository = $this->getDoctrine()->getRepository(Tache::class);
return [
'nbTaches' => $tacheRepository->countByCompagnes($ids),
'nbAppels' => $tacheRepository->countAppelsByCompagnes($ids),
];
}
/**
* @Route("/liste-des-campagnes-archive/OP", name="campagne_list_archive")
*/
public function campagne_list_archive()
{
set_time_limit(180);
// $user = $this->getUser();
$compagnes = $this->getDoctrine()->getRepository(Compagne::class)->findByArchiveLastYear();
// $priorites = array();
// foreach ($compagnes as $key => $compagne) {
// $priorite = $this->getDoctrine()->getRepository(Priorite::class)->findBy(['agent' => $user, 'campagne' => $compagne]);
// if ($priorite) {
// $priorites[$key] = $priorite[0]->getNiveau();
// } else {
// $priorites[$key] = 'Non défini';
// }
// }
return $this->render('campagne/listearchives.html.twig', array_merge(
['compagnes' => $compagnes],
$this->countsCompagnes($compagnes)
));
}
/**
* @Route("/new/SP", name="campagne_add")
*/
public function campagne_add(Request $request)
{
$userlogin = $this->getUser();
$compagne = new Compagne();
$form = $this->createForm(CompagneType::class, $compagne);
$form->handleRequest($request);
$em = $this->getDoctrine()->getManager();
$clients = $this->getDoctrine()->getRepository(Client::class)->findClientWithProjects();
if ($form->isSubmitted() && $form->isValid()) {
$projectId = $_POST['projet'];
$mode = $_POST['mode'];
$statut = $_POST['statut'];
if (isset($_POST['cours'])) {
$idModule = $_POST['cours'];
$module = $this->getDoctrine()->getRepository(Cours::class)->findOneById($idModule);
$compagne->setCours($module);
}
$compagne->setStatut($statut);
$projet = $this->getDoctrine()->getRepository(Projet::class)->findOneBy(["id" => $projectId]);
$compagne->setProjet($projet);
$compagne->setForceVip($_POST['force_vip']);
$compagne->setProgression(0);
$datefr = date("Y-m-d\TH:i:s", strtotime("1 hour"));
$compagne->setDateCreation($datefr);
$compagne->setCreateur($userlogin);
$inscriptions = $this->getDoctrine()->getRepository(Inscription::class)->findInscriptionNotCategorie($projet, 5);
$helper = new Helper($this->getDoctrine());
$objectif = $helper->CalcObjectif($inscriptions);
$compagne->setObjectif($objectif);
$dateRange = $_POST['dateRange'];
$dates = explode(" - ", $dateRange);
$compagne->setDateDebut(date_create($dates[0]));
$compagne->setDateFin(date_create($dates[1]));
$compagne->setState(2);
$em->persist($compagne);
$em->flush();
// $notification = new Notification($this->getDoctrine());
// $actualite = $notification->Actualite($userlogin, "a créé une nouvelle campagne " . $compagne->getNom(), $compagne->getId(), "compagne_show");
// $notification->Suivi($actualite);
if ($mode == 1) {
$helper->TacheCour($inscriptions, $compagne, $module, $statut);
} else {
$helper->Tache($inscriptions, $compagne);
}
return $this->redirectToRoute('compagne_show', ['id' => $compagne->getId()]);
}
return $this->render('campagne/add.html.twig', [
'compagne' => $compagne,
'form' => $form->createView(),
'clients' => $clients,
]);
}
/**
* @Route("/{id}/OP", name="compagne_show")
*/
public function compagne_show(Compagne $campagne)
{
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$last_appel = null;
$in_or_out = null;
$validPeople = $campagne->getValideNumbers();
$operateurs = $em->getRepository(Utilisateur::class)->findByDepartement(4);
foreach ($campagne->getTaches() as $key1 => $tach) {
$gsm = $tach->getApprenant()->getGsm();
if (strpos($gsm, ' ') !== false) {
$gsm = str_replace(' ', '', $gsm);
$tach->getApprenant()->setGsm($gsm);
$em->persist($tach);
$em->flush();
}
$in_or_out[$key1] = 0;
if (in_array($tach->getApprenant(), $validPeople)) {
$in_or_out[$key1] = 1;
}
$last_appel[$key1]['semaine'] = date('W', strtotime($tach->getApprenant()->getLastAccess()));
$appels = $em->getRepository(Tache::class)->findLastAppel($tach->getId());
$last_appel[$key1]['date'] = ($appels) ? $appels[0]->getDate() : null;
$last_appel[$key1]['statut'] = ($appels) ? $appels[0]->getStatut()->getName() : null;
}
$priorite = $this->getDoctrine()->getRepository(Priorite::class)->findOneBy(['agent' => $user, 'campagne' => $campagne]);
return $this->render('campagne/showSP.html.twig', [
'last_appels' => $last_appel,
'in_or_out' => $in_or_out,
'compagne' => $campagne,
'priorite' => $priorite,
'operateurs' => $operateurs
]);
}
/**
* @Route("/projets-by-client/SP", name="projets_by_client")
*/
public function projets_by_client(Request $request)
{
if ($request->isXmlHttpRequest()) {
if (array_key_exists('idClient', $_POST)) {
$idClient = $_POST['idClient'];
$projets = $this->getDoctrine()->getRepository(Projet::class)->findByClient($idClient);
$projetsJson = array();
$idx = 0;
foreach ($projets as $projet) {
$inscriptions = $this->getDoctrine()->getRepository(Inscription::class)->findByProjet($projet);
$temp = [
'id' => $projet->getId(),
'nom' => $projet->getNom(),
];
if (count($inscriptions) > 0 and $projet->getActif() == 1) {
$temp['actif'] = "";
} else {
$temp['actif'] = "disabled";
}
$projetsJson[$idx++] = $temp;
}
// $projetsJson[$idx]['codeClient'] = $client[0]->getCode();
return new JsonResponse($projetsJson);
} elseif (array_key_exists('idProjet', $_POST)) {
$idProjet = $_POST['idProjet'];
$projet = $this->getDoctrine()->getRepository(Projet::class)->findById($idProjet);
$jsonData1 = array(
'codeProjet' => str_replace(" ", "-", $projet[0]->getNom()),
);
return new JsonResponse($jsonData1);
}
}
}
/**
* @Route("/csv/check-apprenants/SP", name="campagne_csv_check", methods={"POST"})
*/
public function campagne_csv_check(Request $request)
{
$file = $request->files->get('csv');
if (!$file || !in_array(strtolower($file->getClientOriginalExtension()), ['csv', 'txt'])) {
return new JsonResponse(['message' => 'Veuillez importer un fichier CSV valide'], 400);
}
$handle = fopen($file->getPathname(), 'r');
$firstLine = fgets($handle);
$delimiter = (substr_count($firstLine, ';') > substr_count($firstLine, ',')) ? ';' : ',';
rewind($handle);
$usernames = [];
while (($row = fgetcsv($handle, 0, $delimiter)) !== false) {
$username = trim(str_replace("\xEF\xBB\xBF", '', (string) $row[0]));
if ($username === '' || strtolower($username) === 'username') {
continue;
}
$usernames[strtolower($username)] = $username;
}
fclose($handle);
if (!count($usernames)) {
return new JsonResponse(['message' => 'Le fichier CSV ne contient aucun username'], 400);
}
$apprenants = $this->getDoctrine()->getRepository(Apprenant::class)->findBy(['username' => array_values($usernames)]);
$found = [];
foreach ($apprenants as $apprenant) {
$key = strtolower(trim($apprenant->getUsername()));
if (!isset($found[$key])) {
$found[$key] = $apprenant;
}
}
$exist = $notExist = [];
foreach ($usernames as $key => $username) {
if (isset($found[$key])) {
$apprenant = $found[$key];
$exist[] = [
'id' => $apprenant->getId(),
'username' => $apprenant->getUsername(),
'firstname' => $apprenant->getFirstname(),
'lastname' => $apprenant->getLastname(),
'email' => $apprenant->getEmail(),
];
} else {
$notExist[] = $username;
}
}
return new JsonResponse(['exist' => $exist, 'notExist' => $notExist]);
}
/**
* @Route("/csv/create/SP", name="campagne_csv_create", methods={"POST"})
*/
public function campagne_csv_create(Request $request)
{
$em = $this->getDoctrine()->getManager();
$client = $this->getDoctrine()->getRepository(Client::class)->find($request->request->get('client'));
$ids = (array) ($request->request->all()['apprenants'] ?? []);
$apprenants = count($ids) ? $this->getDoctrine()->getRepository(Apprenant::class)->findBy(['id' => $ids]) : [];
if (!$client || !count($apprenants)) {
return new JsonResponse(['message' => 'Client ou apprenants manquants'], 400);
}
$title = trim((string) $request->request->get('title', ''));
$script = trim((string) $request->request->get('script', ''));
if ($title === '' || !filter_var($script, FILTER_VALIDATE_URL)) {
return new JsonResponse(['message' => "Titre et script d'appel (URL valide) obligatoires"], 400);
}
$nom = 'CSV-' . $client->getCode() . '-' . preg_replace('/\s+/', '-', $title) . '-' . date('d-m-Y');
$compagne = new Compagne();
$compagne->setType(4);
$compagne->setNom(mb_substr($nom, 0, 255));
$compagne->setDescription($script);
$compagne->setProjet(null);
$compagne->setStatut(null);
$compagne->setForceVip((bool) $request->request->get('force_vip'));
$compagne->setProgression(0);
$compagne->setDateCreation(date("Y-m-d\TH:i:s", strtotime("1 hour")));
$compagne->setCreateur($this->getUser());
$compagne->setDateDebut(new \DateTime('today'));
$compagne->setDateFin(new \DateTime('+1 month'));
$compagne->setState(2);
$helper = new Helper($this->getDoctrine());
$compagne->setObjectif($helper->CalcObjectifApprenants($apprenants));
$em->persist($compagne);
$em->flush();
$helper->TacheApprenants($apprenants, $compagne);
$message = 'La campagne ' . $compagne->getNom() . ' a été créée avec succès (' . count($apprenants) . ' apprenants)';
$this->addFlash('success', $message);
return new JsonResponse([
'success' => true,
'message' => $message,
'redirect' => $this->generateUrl('compagne_show', ['id' => $compagne->getId()]),
]);
}
/**
* @Route("/{id}/edit/CH", name="compagne_edit")
*/
public function compagne_edit(Request $request, Compagne $compagne)
{
$em = $this->getDoctrine()->getManager();
$editForm = $this->createForm(CompagneType::class, $compagne);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$utilisateurs = $this->getDoctrine()->getRepository(Utilisateur::class)->findAll();
foreach ($utilisateurs as $utilisateur) {
$utilisateur->removeCompagne($compagne);
$em->persist($utilisateur);
}
$em->flush();
$dateRange = $_POST['dateRange'];
$dates = explode(" - ", $dateRange);
$dateBegin = date_create($dates[0]);
$dateEnd = date_create($dates[1]);
$compagne->setDateDebut($dateBegin);
$compagne->setDateFin($dateEnd);
$compagne->setForceVip($_POST["force_vip"]);
$em->persist($compagne);
$em->flush();
$cols = $compagne->getCollaborateurs();
if (!empty($cols)) {
foreach ($cols as $col) {
$collab = $this->getDoctrine()->getRepository(Utilisateur::class)->findById($col);
$collab[0]->addCompagne($compagne);
$em->persist($collab[0]);
$priorite = new Priorite();
$priorite->setNiveau($_POST['priorite']);
$priorite->setAgent($collab[0]);
$priorite->setCampagne($compagne);
$em->persist($priorite);
$em->flush();
}
}
$em->flush();
$this->addFlash("success", "Modification a été bien faite");
return $this->redirectToRoute('compagne_show', ['id' => $compagne->getId()]);
}
return $this->render('campagne/edit.html.twig', [
'compagne' => $compagne,
'form' => $editForm->createView(),
]);
}
/**
* @Route("/{id}/majTacheCampagne/SP", name="majTacheCampagne")
*/
public function majTacheCampagne(Compagne $compagne)
{
if (!$compagne->getProjet()) {
$this->addFlash("warning", "Mise à jour impossible : campagne sans projet (créée via CSV)");
return $this->redirectToRoute("compagne_show", array('id' => $compagne->getId()));
}
$em = $this->getDoctrine()->getManager();
$tasks = $this->getDoctrine()->getRepository(Tache::class)->findByCompagne($compagne);
$doceboApi = new NewDoceboApi();
$pathApi = "/manage/v1/orgchart/" . $compagne->getProjet()->getIdProjet() . "/users";
$params = "page_size=200";
$requete = $doceboApi->useDoceboApi($pathApi, $params);
$numPages = $requete['data']['total_page_count'];
$tentative = 0;
foreach ($tasks as $task) {
$found = 0;
for ($i = 1; $i <= $numPages; $i++) {
$params = "page_size=200&page=" . $i;
$requete = $doceboApi->useDoceboApi($pathApi, $params);
$learners = $requete['data']['items'];
foreach ($learners as $learner) {
if ($task->getApprenant()->getIdUser() == $learner['user_id']) {
$found = 1;
}
}
}
if (!$found) {
$appels = $this->getDoctrine()->getRepository(Appel::class)->findByTache($task);
foreach ($appels as $appel) {
$appel->setTache(null);
$em->persist($appel);
}
$em->flush();
$em->remove($task);
}
$calls = $this->getDoctrine()->getRepository(Appel::class)->findByTache($task);
if ($calls) {
$tentative++;
}
}
$em->flush();
$compagne->setTentative($tentative);
$em->persist($compagne);
$em->flush();
for ($i = 1; $i <= $numPages; $i++) {
$params = "page_size=200&page=" . $i;
$requete = $doceboApi->useDoceboApi($pathApi, $params);
$learners = $requete['data']['items'];
foreach ($learners as $learner) {
$apprenant = $this->getDoctrine()->getRepository(Apprenant::class)->findByIdUser($learner['user_id']);
if ($apprenant) {
$tache = $this->getDoctrine()->getRepository(Tache::class)->findBy(['apprenant' => $apprenant[0], 'compagne' => $compagne]);
if (!$tache) {
$tache = new Tache();
$tache->setApprenant($apprenant[0]);
$tache->setEtat(0);
$tache->setCompagne($compagne);
$tache->setLastAccess($learner['last_access_date']);
$em->persist($tache);
$em->flush();
}
} else {
$apprenant = new Apprenant();
$apprenant->setProjet($compagne->getProjet());
$apprenant->setType(0);
$apprenant->setPermissionAppel(0);
$apprenant->setIdUser($learner['user_id']);
$pathApi = "/manage/v1/user/" . $learner['user_id'];
$requete = $doceboApi->useDoceboApi($pathApi, null);
$user_data = $requete['data']['user_data'];
$additional_fields = $requete['data']['additional_fields'];
$gsm = "";
$gsm2 = "";
foreach ($additional_fields as $additional_field) {
if (strcmp($additional_field['title'], "GSM") == 0) {
$gsm = $additional_field['value'];
}
if (strcmp($additional_field['title'], "mdp text") == 0) {
$mdp = $additional_field['value'];
}
if (strcmp($additional_field['title'], "GSM2") == 0) {
$gsm2 = $additional_field['value'];
}
}
if (strlen($gsm) == 9) {
$gsm = "0" . $gsm;
}
if (strlen($gsm2) == 9) {
$gsm2 = "0" . $gsm2;
}
$apprenant->setFirstname($user_data['first_name']);
$apprenant->setLastname($user_data['last_name']);
$apprenant->setEmail($user_data['email']);
$apprenant->setGsm($gsm);
$apprenant->setGsm2($gsm2);
$apprenant->setUsername($user_data['username']);
$apprenant->setPassword($mdp);
$apprenant->setLastAccess($learner['last_access_date']);
$em->persist($apprenant);
$em->flush();
$tache = new Tache();
$tache->setApprenant($apprenant);
$tache->setEtat(0);
$tache->setCompagne($compagne);
$tache->setLastAccess($learner['last_access_date']);
$em->persist($tache);
$em->flush();
}
}
}
$em->flush();
$this->addFlash("success", "MAJ liste des tâches a été bien faite");
return $this->redirectToRoute("compagne_show", array('id' => $compagne->getId()));
}
/**
* @Route("/downloadComms/{id}/OP", name="downloadComms")
*/
public function downloadComms(Compagne $compagne)
{
$results = array();
//boucle
foreach ($compagne->getTaches() as $key => $tach) {
$results[$key]['Nom'] = $tach->getApprenant()->getFirstname() . ' ' . $tach->getApprenant()->getLastname();
$results[$key]['Email'] = $tach->getApprenant()->getUsername();
$appels = $this->getDoctrine()->getRepository(Appel::class)->findBy(['tache' => $tach->getId(), "type" => 1]);
$arr1 = $arr2 = [];
foreach ($appels as $appel) {
$arr1[] = $appel->getDate();
$arr2[] = ($appel->getDetails()) ? $appel->getDetails() : " NONE ";
if ($appel) {
$results[$key]['Date du dernier appel'] = implode("||", $arr1);
$results[$key]['Commentaire'] = implode("||", $arr2);
}
}
}
// dd($results);
$objPHPExcel = new Spreadsheet();
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Nom');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Email');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Date du dernier appel');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'Commentaire');
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('2196f3');
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('2196f3');
$objPHPExcel->getActiveSheet()->getStyle('C1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('2196f3');
$objPHPExcel->getActiveSheet()->getStyle('D1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setRGB('2196f3');
$objPHPExcel->getActiveSheet()->getStyle("A1")->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle("B1")->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle("C1")->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle("D1")->getFont()->setBold(true);
// $objPHPExcel->getActiveSheet()->setCellValue('B1', 'Numéro');
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(60);
$objPHPExcel->getActiveSheet()->fromArray($results, null, 'A2');
// if (strlen($compagne->getNom()) >= 31) {
// $objPHPExcel->getActiveSheet()->setTitle(substr($compagne->getNom(), 31));
// } else {
// $objPHPExcel->getActiveSheet()->setTitle($compagne->getNom());
// }
$objPHPExcel->getActiveSheet()->setTitle("Commentaires");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
// if (strlen($compagne->getNom()) >= 31) {
// header('Content-Disposition: attachment;filename="' . substr($compagne->getNom(), 31) . '.xlsx"');
// } else {
// header('Content-Disposition: attachment;filename="' . $compagne->getNom() . '.xlsx"');
// }
header('Content-Disposition: attachment;filename="Commentaires.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = new Xlsx($objPHPExcel);
$objWriter->save('php://output');
exit;
}
/**
* @Route("/{id}/archiver/SP",name="archiverCampagne")
*/
public function archiverAction(Compagne $compagne)
{
// $user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$compagne->setArchive(1);
$em->persist($compagne);
$em->flush();
// $notification = new Notification($this->getDoctrine());
// $actualite = $notification->Actualite($user, "a clôturé la compagne " . $compagne->getNom(), $compagne->getId(), "compagne_show");
// $notification->Suivi($actualite);
return $this->redirectToRoute('campagne_list');
}
/**
* @Route("/{id}/desarchiver/SP",name="desarchiverCampagne")
*/
public function desarchiverAction(Compagne $compagne)
{
$em = $this->getDoctrine()->getManager();
$compagne->setArchive(0);
$em->persist($compagne);
$em->flush();
return $this->redirectToRoute('campagne_list_archive');
}
/**
* @Route("/{id}/recycler/SP",name="recyclerCampagne")
*/
public function recyclerAction(Compagne $compagne)
{
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$compagne->setState(2);
$em->persist($compagne);
$em->flush();
// $notification = new Notification($this->getDoctrine());
// $actualite = $notification->Actualite($user, "a recyclé la compagne " . $compagne->getNom(), $compagne->getId(), "compagne_show");
// $notification->Suivi($actualite);
return $this->redirectToRoute('campagne_list');
}
/**
* @Route("/{id}/superVip/CH", name="compagne_superVip")
*/
public function superVipAction(Compagne $compagne)
{
$em = $this->getDoctrine()->getManager();
$taches = $em->getRepository(Tache::class)->findByCompagne($compagne);
$operateurs = $em->getRepository(Utilisateur::class)->findCPSPOP();
if (array_key_exists('formTask', $_POST) and array_key_exists('operateurs', $_POST)) {
$tasks = explode(",", $_POST['formTask']);
if ($_POST['operateurs'] == 0) {
if (!empty($tasks)) {
foreach ($tasks as $task) {
$tache = $em->getRepository(Tache::class)->findBy(array('id' => $task));
$tache[0]->getApprenant()->setType(2);
$em->persist($tache[0]);
$em->flush();
}
}
} else {
$agent = $em->getRepository(Utilisateur::class)->findById($_POST['operateurs']);
if (!empty($tasks)) {
foreach ($tasks as $task) {
$tache = $em->getRepository(Tache::class)->findBy(array('id' => $task));
$tache[0]->getApprenant()->setType(2);
$tache[0]->setAgent($agent[0]);
$em->persist($tache[0]);
$em->flush();
}
}
}
$this->addFlash("success", "Les apprenants sélectionnés ont été définis Super VIP");
return $this->redirectToRoute('compagne_show', array('id' => $compagne->getId()));
}
return $this->render('campagne/superVip.html.twig', [
'compagne' => $compagne,
'operateurs' => $operateurs,
'taches' => $taches,
]);
}
/**
* @Route("/{id}/vip/CH", name="compagne_vip")
*/
public function vipAction(Compagne $compagne)
{
$em = $this->getDoctrine()->getManager();
$taches = $em->getRepository(Tache::class)->findByCompagne($compagne);
$operateurs = $em->getRepository(Utilisateur::class)->findCPSPOP();
if (array_key_exists('formTask', $_POST) and array_key_exists('operateur', $_POST)) {
$tasks = explode(",", $_POST['formTask']);
// dump($tasks);
// die();
if ($_POST['operateur'] == 0) {
if (!empty($tasks)) {
foreach ($tasks as $task) {
$tache = $em->getRepository(Tache::class)->findBy(array('id' => $task));
$tache[0]->getApprenant()->setType(1);
$em->persist($tache[0]);
$em->flush();
}
}
} else {
$agent = $em->getRepository(Utilisateur::class)->findById($_POST['operateur']);
if (!empty($tasks)) {
foreach ($tasks as $task) {
$tache = $em->getRepository(Tache::class)->findBy(array('id' => $task));
$tache[0]->getApprenant()->setType(1);
$tache[0]->setAgent($agent[0]);
$em->persist($tache[0]);
$em->flush();
}
}
}
$this->addFlash("success", "Les apprenants sélectionnés ont été définis VIP");
return $this->redirectToRoute('compagne_show', array('id' => $compagne->getId()));
}
return $this->render('campagne/vip.html.twig', array(
'compagne' => $compagne,
'operateurs' => $operateurs,
'taches' => $taches,
));
}
}