You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
304 lines
12 KiB
304 lines
12 KiB
<?php
|
|
|
|
|
|
namespace App\Controller\Admin;
|
|
use App\Controller\ChatController;
|
|
use App\Entity\Comentarios;
|
|
use App\Entity\ComentariosHistorico;
|
|
use App\RuleEngine\Service\AcdrContext;
|
|
use Zitec\RuleEngineBundle\Service\RuleEvaluator;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
use Sonata\AdminBundle\Controller\CRUDController;
|
|
use App\Entity\TareaGreenEnterpreneur;
|
|
use App\Entity\Respuestas;
|
|
use App\Entity\RespuestasHistoricas;
|
|
use Zitec\RuleEngineBundle\Traits\ActionsTrait;
|
|
use App\Traits\SaveTasks;
|
|
use Symfony\Component\HttpKernel\KernelInterface;
|
|
use \TCPDF;
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
class RespuestasTareaAdminController extends CRUDController
|
|
{
|
|
use ActionsTrait;
|
|
use SaveTasks;
|
|
protected $requestStack;
|
|
/**
|
|
* @var RuleEvaluator
|
|
*/
|
|
protected $evaluator;
|
|
|
|
/**
|
|
* @var AcdrContext
|
|
*/
|
|
protected $context;
|
|
/**
|
|
* @var KernelInterface
|
|
*/
|
|
private $kernel;
|
|
protected $translator;
|
|
private $chatController;
|
|
|
|
public function setRuleEvaluator(RuleEvaluator $evaluator){
|
|
$this->evaluator = $evaluator;
|
|
}
|
|
public function setContext(AcdrContext $context){
|
|
$this->context = $context;
|
|
}
|
|
public function setKernel(KernelInterface $kernel){
|
|
$this->kernel = $kernel;
|
|
}
|
|
public function __construct(
|
|
RequestStack $requestStack,
|
|
RuleEvaluator $evaluator,
|
|
AcdrContext $context,
|
|
KernelInterface $kernel,
|
|
ChatController $chatController,
|
|
TranslatorInterface $translator
|
|
) {
|
|
$this->requestStack = $requestStack;
|
|
$this->evaluator = $evaluator;
|
|
$this->context = $context;
|
|
$this->kernel = $kernel;
|
|
$this->chatController = $chatController;
|
|
$this->translator = $translator;
|
|
}
|
|
|
|
/**
|
|
* @param $tarea
|
|
* @param $preguntas
|
|
* @param $request
|
|
* @param $em
|
|
* @return mixed
|
|
*/
|
|
private function trainerSave($tarea, $preguntas, $request,$em)
|
|
{
|
|
//Check if load on innerframe
|
|
$modal=0;
|
|
if (!empty($request->request->get('modal'))){
|
|
$modal=1;
|
|
}
|
|
if (empty($request->request->get('ge'))) {
|
|
$res['error'] = 'Unidentified Green Entrepreneur';
|
|
return $this->renderWithExtraParams('admin/errorprecondition_view.html.twig', ['tarea' => $tarea, 'preguntas' => $preguntas, 'error' => $res['error']]);
|
|
}
|
|
//die('Llega hasta desvio'.print_r($request->request->get('ge')));
|
|
|
|
$ge = $em->getRepository('App:Licitador')->findOneById([$request->request->get('ge')]);
|
|
$tareasRealizadas = $em->getRepository('App:TareaGreenEnterpreneur')
|
|
->findBy(['greenEntrepreneur'=> $ge]);
|
|
$sendMessage = $this->trainerSaveOnly($preguntas, $request, $ge, $em);
|
|
if($sendMessage){
|
|
$trainer = $this->getUser();
|
|
$this->createChatAlert($trainer, $ge, $tarea->getId());
|
|
}
|
|
$tareasRealizadasGe = $em->getRepository('App:TareaGreenEnterpreneur')
|
|
->findBy(['greenEntrepreneur'=> $ge]);
|
|
$tareasRealizadas=[];
|
|
if ($tarea[0]->getId() ==$tarea[0]->getProcesosId()->getTareafinal()->getId()){
|
|
return $this->renderWithExtraParams('admin/tarea_completada_view.html.twig', ['tarea' => $tarea, 'preguntas' => $preguntas]);
|
|
}
|
|
foreach($tareasRealizadasGe as $t){
|
|
if($tarea[0]->getProcesosId()->getId()!==$t->getTarea()->getProcesosId()->getId())
|
|
continue;
|
|
$tareasRealizadas[]=$t->getTarea();
|
|
}
|
|
usort($tareasRealizadas, [$this,'comparator']);
|
|
foreach($tarea[0]->getPostCondition() as $postcondition)
|
|
{
|
|
$acciones=json_decode($postcondition->getAction(), true);
|
|
foreach($acciones as $accion){
|
|
if($accion['tipo']==='redireccion'){
|
|
return $this->redirectToRoute('realizar_tarea_comentar', ['id' => $accion['valor'], 'userge'=>$ge->getId(), 'modal'=>$modal], 307);
|
|
}
|
|
}
|
|
}
|
|
// foreach ($tareasRealizadas as $tareaRea) {
|
|
// if($tarea[0]->getPosition()<$tareaRea->getPosition()){
|
|
//// echo $tarea[0]->getPosition().'position=>'.$tareaRea->getPosition();
|
|
//// die($this->generateUrl('realizar_tarea_comentar', ['id' => $tareaRea->getId(), 'userge'=>$ge->getId()]));
|
|
// return $this->redirectToRoute('realizar_tarea_comentar', ['id' => $tareaRea->getId(), 'userge'=>$ge->getId()], 307);
|
|
// }
|
|
// }
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @param $tareaRealizada2
|
|
* @param $preguntas
|
|
* @param $em
|
|
*/
|
|
private function fillRulesContext($tareaRealizada2,$preguntas,$em){
|
|
if(!empty($tareaRealizada2)){
|
|
$this->context->setMyDataSource($tareaRealizada2);
|
|
}else{
|
|
$this->context->setMyDataSource(null);
|
|
}
|
|
if(!empty($this->getUser())){
|
|
$this->context->setMyUserContext($this->getUser());
|
|
}else{
|
|
$this->context->setMyUserContext(null);
|
|
}
|
|
$preguntasfiltered=null;
|
|
foreach($preguntas as $pregunta){
|
|
if($pregunta->getIsBifurcation()){
|
|
$preguntasfiltered=$pregunta;
|
|
}
|
|
}
|
|
|
|
$respuestasContext = $em->getRepository('App:Respuestas')
|
|
->findOneBy(['preguntas'=>$preguntasfiltered,'greenEntrepreneur'=> $this->getUser() ]);
|
|
if(!empty($respuestasContext)){
|
|
$this->context->setRespuestasContext($respuestasContext);
|
|
}else{
|
|
$this->context->setRespuestasContext(null);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $em
|
|
* @param $tarea
|
|
* @param $user
|
|
* @param $preguntas
|
|
* @param $request
|
|
* @return mixed
|
|
*/
|
|
private function geSave($em, $tarea, $user, $preguntas, $request){
|
|
$tareaRealizada = $em->getRepository('App:TareaGreenEnterpreneur')
|
|
->findBy(['tarea'=>$tarea[0], 'greenEntrepreneur'=> $user]);
|
|
$tareaRealizada2 = $em->getRepository('App:TareaGreenEnterpreneur')
|
|
->findBy(['greenEntrepreneur'=> $user]);
|
|
$currentTaskId=$tarea[0]->getId();
|
|
$currentTask = $em->getRepository('App:Tarea')->findOneById($currentTaskId);
|
|
|
|
$respuestas=$this->geSaveOnly($preguntas, $request,$user, $em, false);
|
|
$currentTask = $em->getRepository('App:Tarea')->findOneById($currentTaskId);
|
|
|
|
if(empty($tareaRealizada[0])){
|
|
$tareaRealizada=new TareaGreenEnterpreneur();
|
|
$tareaRealizada->setCompletada(1);
|
|
$tareaRealizada->setGreenEntrepreneur($user);
|
|
$tareaRealizada->setTarea($currentTask);
|
|
}else{
|
|
$tareaRealizada[0]->setCompletada(1);
|
|
$tareaRealizada=$tareaRealizada[0];
|
|
}
|
|
|
|
$em->persist($tareaRealizada);
|
|
$em->flush();
|
|
$this->fillRulesContext($tareaRealizada2,$preguntas,$em);
|
|
|
|
$resultados=[];
|
|
$resultado='';
|
|
if ($currentTask->getId() == $currentTask->getProcesosId()->getTareafinal()->getId()){
|
|
return $this->renderWithExtraParams('admin/tarea_completada_view.html.twig', ['tarea' => $tarea, 'preguntas' => $preguntas, 'respuestas' => $respuestas]);
|
|
}
|
|
$respos=[];
|
|
if($currentTask->getPostcondition()) {
|
|
foreach ($currentTask->getPostcondition() as $postcondicion) {
|
|
if (!$postcondicion->getActive()) continue;
|
|
if ($this->evaluator->evaluate($postcondicion, $this->context)) {
|
|
|
|
$resultado = $this->executeConditionActions($user->getId(), $postcondicion->getId(), 'true');
|
|
if (!empty($resultado)) {
|
|
$respos[]='Si'.$postcondicion->getId().' '.$resultado;
|
|
$resultados[] = $resultado;
|
|
$res = json_decode($resultado, true);
|
|
if (isset($res['error'])) {
|
|
return $this->renderWithExtraParams('admin/errorprecondition_view.html.twig', ['tarea' => $tarea, 'preguntas' => $preguntas, 'respuestas' => $respuestas, 'error' => $res['error']]);
|
|
}
|
|
}
|
|
} else {
|
|
$resultado = $this->executeConditionActions($user->getId(), $postcondicion->getId(), 'false');
|
|
if (!empty($resultado)) {
|
|
$respos[]='No'.$postcondicion->getId().' '.$resultado;
|
|
|
|
$resultados[] = $resultado;
|
|
$res = json_decode($resultado, true);
|
|
if (isset($res['error'])) {
|
|
return $this->renderWithExtraParams('admin/errorprecondition_view.html.twig', ['tarea' => $tarea, 'preguntas' => $preguntas, 'respuestas' => $respuestas, 'error' => $res['error']]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
foreach ($resultados as $resultado) {
|
|
$res = json_decode($resultado, true);
|
|
if (isset($res['route'])) {
|
|
// die('llega aqui'.$user->getId().'-'.$postcondicion->getId().'-'.$res['route'].$res['id']);
|
|
$this->executePrePostCondicions($currentTask, $user, $em);
|
|
return $this->redirectToRoute($res['route'], ['id' => $res['id']], 307);
|
|
}
|
|
}
|
|
}
|
|
|
|
if($currentTask->getRepetible()){
|
|
return $this->renderWithExtraParams($tarea[0]->getTemplateId()->getFilename(), ['tarea' =>$tarea, 'preguntas'=>$preguntas, 'respuestas'=>$respuestas, /*'respuestasarray'=>$respuestasArray, 'casoestudio'=>$respuestasCasoEstudio*/]);
|
|
}
|
|
return $this->renderWithExtraParams($tarea[0]->getTemplateId()->getFilename(), ['tarea' => $tarea, 'preguntas' => $preguntas, 'respuestas' => $respuestas]);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @param $object1
|
|
* @param $object2
|
|
* @return bool
|
|
*/
|
|
private function comparator($object1, $object2) {
|
|
return $object1->getPosition() > $object2->getPosition();
|
|
}
|
|
|
|
/**
|
|
* @param null $id
|
|
*/
|
|
public function editAction($id=null)
|
|
{
|
|
$request = $this->requestStack->getCurrentRequest();
|
|
$em = $this->getDoctrine()->getManager();
|
|
$tarea = $em->getRepository('App:Tarea')
|
|
->findById($id);
|
|
$preguntas = $tarea[0]->getPreguntasId();
|
|
/** @var \App\Entity\Licitador $user */
|
|
$user = $this->getUser();
|
|
switch ($user->getType()) {
|
|
case 'trainer':
|
|
return $this->trainerSave($tarea,$preguntas, $request,$em);
|
|
break;
|
|
case 'green_entrepreneur':
|
|
return $this->geSave($em, $tarea, $user, $preguntas, $request);
|
|
break;
|
|
default:
|
|
$res['error']='User Type Not Allowed';
|
|
return $this->renderWithExtraParams('admin/errorusuario_view.html.twig', ['tarea' => $tarea, 'preguntas' => $preguntas, 'error' => $res['error']]);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private function createChatAlert($trainer, $receptor, $tareaId){
|
|
|
|
$a1=$trainer->getSalas()->toArray();
|
|
$a2=$receptor->getSalas()->toArray();
|
|
$sala = array_uintersect($a1, $a2, function ($a,$b)
|
|
{
|
|
if ($a->getId() === $b->getId() && strtolower($b->getDenominacion()) === 'my trainer')
|
|
{
|
|
return 0;
|
|
}
|
|
return ($a>$b)?1:-1;
|
|
});// end array_uintersect
|
|
if(empty($sala)){
|
|
return;
|
|
}// end if
|
|
if(is_array($sala)){
|
|
$sala = array_shift($sala);
|
|
}// end if
|
|
$route = 'realizar_tarea_edit';
|
|
$url = $this->generateUrl($route, ['id' => $tareaId]);
|
|
$mensaje = $this->translator->trans("Tu trainer a realizado comentarios en la siguiente tarea:");
|
|
$mensaje .= '<br><a href="'.$url.'">';
|
|
$mensaje .=$this->translator->trans("Ver tarea").'</a>';
|
|
|
|
// $mensaje = "Tu trainer a realizado comentarios en la siguiente tarea:" . '<br><a href="'.$url.'">Ver tarea</a>';
|
|
$this->chatController->publishMessage($mensaje, $trainer, $sala);
|
|
}
|
|
}
|