Rev 279 | Rev 292 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
/**
*
* Controlador: Mis Perfiles
*
*/
declare(strict_types=1);
namespace LeadersLinked\Controller;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Log\LoggerInterface;
use Laminas\View\Model\ViewModel;
use Laminas\View\Model\JsonModel;
use LeadersLinked\Model\Notification;
use LeadersLinked\Mapper\CompanySelfEvaluationTestMapper;
use LeadersLinked\Mapper\NotificationMapper;
use LeadersLinked\Mapper\QueryMapper;
use LeadersLinked\Model\CompanySelfEvaluationForm;
use LeadersLinked\Mapper\CompanySelfEvaluationFormMapper;
use LeadersLinked\Model\CompanySelfEvaluationTest;
use Laminas\Db\Sql\Select;
use LeadersLinked\Mapper\CompanySelfEvaluationFormUserMapper;
use LeadersLinked\Form\SelfEvaluation\SelfEvaluationTestForm;
use LeadersLinked\Library\Functions;
class SelfEvaluationController extends AbstractActionController
{
/**
*
* @var AdapterInterface
*/
private $adapter;
/**
*
* @var AbstractAdapter
*/
private $cache;
/**
*
* @var LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @param AdapterInterface $adapter
* @param AbstractAdapter $cache
* @param LoggerInterface $logger
* @param array $config
*/
public function __construct($adapter, $cache , $logger, $config)
{
$this->adapter = $adapter;
$this->cache = $cache;
$this->logger = $logger;
$this->config = $config;
}
/**
*
* Generación del listado de perfiles
* {@inheritDoc}
* @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
*/
public function indexAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
if($request->isGet()) {
$headers = $request->getHeaders();
$isJson = false;
if($headers->has('Accept')) {
$accept = $headers->get('Accept');
$prioritized = $accept->getPrioritized();
foreach($prioritized as $key => $value) {
$raw = trim($value->getRaw());
if(!$isJson) {
$isJson = strpos($raw, 'json');
}
}
}
if($isJson) {
$search = trim(filter_var($this->params()->fromQuery('search', ''), FILTER_SANITIZE_STRING));
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
$allowTakeATest = $acl->isAllowed($currentUser->usertype_id, 'profile/self-evaluation/take-a-test');
$allowReport = $acl->isAllowed($currentUser->usertype_id, 'profile/self-evaluation/report');
$queryMapper = QueryMapper::getInstance($this->adapter);
$select = $queryMapper->getSql()->select();
$select->columns(['uuid','name', 'description', 'text', 'language']);
$select->from(['f' => CompanySelfEvaluationFormMapper::_TABLE]);
$select->join(['fu' => CompanySelfEvaluationFormUserMapper::_TABLE, 'f.id = fu.form_id']);
$select->join(['t' => CompanySelfEvaluationTestMapper::_TABLE], 'fu.form_id = t.form_id AND fu.user_id = t.user_id', ['status'], Select::JOIN_LEFT_OUTER );
$select->where->equalTo('f.status', CompanySelfEvaluationForm::STATUS_ACTIVE);
if($search) {
$select->where->NEST->like('name', '%' . $search . '%');
}
$select->order('name ASC, language ASC');
$records = $queryMapper->fetchAll($select);
$items = [];
foreach($records as $record)
{
switch($record['language'])
{
case CompanySelfEvaluationForm::LANGUAGE_ENGLISH :
$language = 'LABEL_ENGLISH';
break;
case CompanySelfEvaluationForm::LANGUAGE_SPANISH :
$language = 'LABEL_SPANISH';
break;
default :
$language = '';
break;
}
switch ($record['status'])
{
case CompanySelfEvaluationTest::STATUS_COMPLETED :
$status = 'LABEL_COMPLETED';
break;
case CompanySelfEvaluationTest::STATUS_PENDING :
$status = 'LABEL_PENDING';
break;
case CompanySelfEvaluationTest::STATUS_REVIEW :
$status = 'LABEL_REVIEW';
break;
default :
$status = 'LABEL_AVAILABLE';
break;
}
$item = [
'name' => $record['name'],
'description' => $record['description'],
'text' => $record['text'],
'language' => $language,
'status' => $status,
'link_take_a_test' => $allowTakeATest && empty($record['status']) ? $this->url()->fromRoute('profile/self-evaluation/take-a-test', ['id' => $record['uuid'] ]) : '',
'link_report' => $allowReport && $record['status'] == CompanySelfEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('profile/self-evaluation/report', ['id' => $record['uuid'] ]) : '',
];
array_push($items, $item);
}
$response = [
'success' => true,
'data' => $items
];
return new JsonModel($response);
} else {
$notificationMapper = NotificationMapper::getInstance($this->adapter);
$notificationMapper->markAllNotificationsAsReadByTypeAndUserId(Notification::TYPE_ACCEPT_MY_REQUEST_CONNECTION, $currentUser->id);
$this->layout()->setTemplate('layout/layout.phtml');
$viewModel = new ViewModel();
$viewModel->setTemplate('leaders-linked/self-evaluation/index.phtml');
return $viewModel ;
}
} else {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
}
}
public function takeaTestAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$uuid = $this->params()->fromRoute('id');
$companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
$companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOneByUuid($uuid);
if(!$companySelfEvaluationForm) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_FORM_SELF_EVALUATION_NOT_FOUND'
]) ;
}
if($companySelfEvaluationForm->status == CompanySelfEvaluationForm::STATUS_INACTIVE) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_FORM_SELF_EVALUATION_IS_INACTIVE'
]) ;
}
$companySelfEvaluationFormUserMapper = CompanySelfEvaluationFormUserMapper::getInstance($this->adapter);
$companySelfEvaluationFormUser = $companySelfEvaluationFormUserMapper->fetchOneByFormIdAndUserId($companySelfEvaluationForm->id, $currentUser->id);
if(!$companySelfEvaluationFormUser) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_FORM_SELF_EVALUATION_YOU_CAN_NOT_TAKE'
]) ;
}
$companySelfEvaluationTestMapper = CompanySelfEvaluationTestMapper::getInstance($this->adapter);
$companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneBy($companySelfEvaluationForm->id, $currentUser->id);
if($companySelfEvaluationTest) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_FORM_SELF_EVALUATION_ALREADY_YOUR_APPLICATION_IN_THIS_TEST'
]) ;
}
$request = $this->getRequest();
if($request->isGet()) {
return new JsonModel([
'success' => false,
'data' => [
'name' => $companySelfEvaluationForm->name,
'description' => $companySelfEvaluationForm->description,
'text' => $companySelfEvaluationForm->text,
'content' => json_decode($companySelfEvaluationForm->content),
]
]) ;
}
if($request->isPost()) {
$form = new SelfEvaluationTestForm();
$dataPost = $request->getPost()->toArray();
$form->setData($dataPost);
if($form->isValid()) {
$dataPost = (array) $form->getData();
$selfEvaluationTest = new CompanySelfEvaluationTest();
$selfEvaluationTest->company_id = $companySelfEvaluationForm->company_id;
$selfEvaluationTest->form_id = $companySelfEvaluationForm->id;
$selfEvaluationTest->user_id = $currentUser->id;
$selfEvaluationTest->status = CompanySelfEvaluationTest::STATUS_PENDING;
$selfEvaluationTest->content = $dataPost['content'];
$result = $companySelfEvaluationTestMapper->insert($selfEvaluationTest);
if($result) {
$this->logger->info('Se agrego un nuevo test de auto-evaluación : ' . $companySelfEvaluationForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_ADDED'
];
} else {
$data = [
'success' => false,
'data' => $companySelfEvaluationTestMapper->getError()
];
}
return new JsonModel($data);
} else {
$messages = [];
$form_messages = (array) $form->getMessages();
foreach($form_messages as $fieldname => $field_messages)
{
$messages[$fieldname] = array_values($field_messages);
}
return new JsonModel([
'success' => false,
'data' => $messages
]);
}
}
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
}
public function reportAction()
{
/*
* 'ERROR_FORM_SELF_EVALUATION_NOT_FOUND' => 'El Test no existe',
'ERROR_FORM_SELF_EVALUATION_IS_INACTIVE' => 'El Test esta inactivo',
'ERROR_FORM_SELF_EVALUATION_YOU_CAN_NOT_TAKE' => 'No puede tomar este Test',
'ERROR_FORM_SELF_EVALUATION_ALREADY_YOUR_APPLICATION_IN_THIS_TEST' => 'Ya realizo este Test previamente',
'ERROR_FORM_SELF_EVALUATION_YOUR_APPLICATION_IN_THIS_TEST_NO_FOUND' => 'No ha realizado este Test previamente',
'ERROR_FORM_SELF_EVALUATION_YOUR_APPLICATION_IN_THIS_TEST_IS_NOT_COMPLETED' => 'Su Test no se encuentra disponible para consultar',
*/
}
}