Rev 629 | Autoría | Ultima modificación | Ver Log |
<?php
/**
*
* Controlador: Autoevaluación
*
*/
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\CompanyPerformanceEvaluationTestMapper;
use LeadersLinked\Mapper\NotificationMapper;
use LeadersLinked\Mapper\QueryMapper;
use LeadersLinked\Model\CompanyPerformanceEvaluationForm;
use LeadersLinked\Mapper\CompanyPerformanceEvaluationFormMapper;
use LeadersLinked\Model\CompanyPerformanceEvaluationTest;
use Laminas\Db\Sql\Select;
use LeadersLinked\Mapper\CompanyPerformanceEvaluationFormUserMapper;
use LeadersLinked\Form\PerformanceEvaluation\PerformanceEvaluationTestForm;
use LeadersLinked\Mapper\CompetencyMapper;
use LeadersLinked\Mapper\JobDescriptionMapper;
use LeadersLinked\Mapper\JobDescriptionCompetencyMapper;
use LeadersLinked\Mapper\CompetencyTypeMapper;
use LeadersLinked\Mapper\JobDescriptionSubordinateMapper;
use LeadersLinked\Model\JobDescriptionCompetency;
use LeadersLinked\Model\JobDescriptionBehaviorCompetency;
use LeadersLinked\Mapper\JobDescriptionBehaviorCompetencyMapper;
use LeadersLinked\Model\JobDescriptionSubordinate;
use LeadersLinked\Mapper\BehaviorCompetencyMapper;
use LeadersLinked\Model\BehaviorCompetency;
use LeadersLinked\Mapper\BehaviorMapper;
use LeadersLinked\Library\Functions;
class PerformanceEvaluationController 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 evaluaciones
* {@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/performance-evaluation/take-a-test');
$allowReport = $acl->isAllowed($currentUser->usertype_id, 'profile/performance-evaluation/report');
$queryMapper = QueryMapper::getInstance($this->adapter);
$select = $queryMapper->getSql()->select();
$select->columns(['uuid', 'name', 'description', 'text', 'job_description_id']);
$select->from(['f' => CompanyPerformanceEvaluationFormMapper::_TABLE]);
$select->join(['fu' => CompanyPerformanceEvaluationFormUserMapper::_TABLE], 'f.id = fu.form_id', []);
$select->join(['t' => CompanyPerformanceEvaluationTestMapper::_TABLE], 'fu.form_id = t.form_id AND fu.user_id = t.user_id', ['status'], Select::JOIN_LEFT_OUTER);
$select->where->equalTo('f.status', CompanyPerformanceEvaluationForm::STATUS_ACTIVE);
$select->where->equalTo('fu.user_id', $currentUser->id);
if ($search) {
$select->where->NEST->like('name', '%' . $search . '%');
}
$select->order('name ASC, job_description_id ASC');
$records = $queryMapper->fetchAll($select);
$items = [];
$jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
foreach ($records as $record) {
switch ($record['status']) {
case CompanyPerformanceEvaluationTest::STATUS_DRAFT :
$status = 'LABEL_DRAFT';
break;
case CompanyPerformanceEvaluationTest::STATUS_COMPLETED :
$status = 'LABEL_COMPLETED';
break;
case CompanyPerformanceEvaluationTest::STATUS_PENDING :
$status = 'LABEL_PENDING';
break;
case CompanyPerformanceEvaluationTest::STATUS_REVIEW :
$status = 'LABEL_REVIEW';
break;
default :
$status = 'LABEL_AVAILABLE';
break;
}
$jobDescription = $jobDescriptionMapper->fetchOne($record['job_description_id']);
if ($jobDescription) {
$item = [
'name' => $record['name'],
'description' => $record['description'],
'text' => $record['text'],
'job_description' => $jobDescription->name,
'status' => $status,
'link_take_a_test' => $allowTakeATest && ( empty($record['status']) || $record['status'] == CompanyPerformanceEvaluationTest::STATUS_DRAFT) ? $this->url()->fromRoute('profile/performance-evaluation/take-a-test', ['id' => $record['uuid']]) : '',
'link_report' => $allowReport && $record['status'] == CompanyPerformanceEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('profile/performance-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/performance-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');
$companyPerformanceEvaluationFormMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
$companyPerformanceEvaluationForm = $companyPerformanceEvaluationFormMapper->fetchOneByUuid($uuid);
if (!$companyPerformanceEvaluationForm) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_FORM_EVALUATION_NOT_FOUND'
]);
}
if ($companyPerformanceEvaluationForm->status == CompanyPerformanceEvaluationForm::STATUS_INACTIVE) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_FORM_EVALUATION_IS_INACTIVE'
]);
}
$companyPerformanceEvaluationFormUserMapper = CompanyPerformanceEvaluationFormUserMapper::getInstance($this->adapter);
$companyPerformanceEvaluationFormUser = $companyPerformanceEvaluationFormUserMapper->fetchOneByFormIdAndUserId($companyPerformanceEvaluationForm->id, $currentUser->id);
if (!$companyPerformanceEvaluationFormUser) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_FORM_EVALUATION_YOU_CAN_NOT_TAKE'
]);
}
$companyPerformanceEvaluationTestMapper = CompanyPerformanceEvaluationTestMapper::getInstance($this->adapter);
$companyPerformanceEvaluationTest = $companyPerformanceEvaluationTestMapper->fetchOneBy($companyPerformanceEvaluationForm->id, $currentUser->id);
if ($companyPerformanceEvaluationTest && $companyPerformanceEvaluationTest->status != CompanyPerformanceEvaluationTest::STATUS_DRAFT) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_FORM_EVALUATION_ALREADY_YOUR_APPLICATION_IN_THIS_TEST'
]);
}
$request = $this->getRequest();
if ($request->isGet()) {
// set content
$content = $companyPerformanceEvaluationTest && $companyPerformanceEvaluationTest->status == CompanyPerformanceEvaluationTest::STATUS_DRAFT ?
json_decode($companyPerformanceEvaluationTest->content, true) :
json_decode($companyPerformanceEvaluationForm->content, true);
//Competencies
$jobDescriptionCompetencyMapper = JobDescriptionCompetencyMapper::getInstance($this->adapter);
$competencyMapper = CompetencyMapper::getInstance($this->adapter);
$competenceTypeMapper = CompetencyTypeMapper::getInstance($this->adapter);
$behaviorCompetencyMapper = BehaviorCompetencyMapper::getInstance($this->adapter);
$jobDescriptionBehaviorCompetencyMapper = JobDescriptionBehaviorCompetencyMapper::getInstance($this->adapter);
$behaviorMapper = BehaviorMapper::getInstance($this->adapter);
$competencies = [];
$jobDescriptionCompetency = $jobDescriptionCompetencyMapper->fetchByJobDescriptionId($companyPerformanceEvaluationForm->job_description_id);
foreach ($jobDescriptionCompetency as $record) {
$competency = $competencyMapper->fetchOne($record->competency_id);
$competenceType = $competenceTypeMapper->fetchOne($competency->competency_type_id);
if($competency && $competenceType){
$behaviorCompetencies = $behaviorCompetencyMapper->fetchByCompetencyId($competency->id);
$behaviors = [];
foreach ($behaviorCompetencies as $rows) {
$behavior = $behaviorMapper->fetchOne($rows->behavior_id);
$jobDescriptionBehaviorCompetency = $jobDescriptionBehaviorCompetencyMapper->fetchOneByBehavior($companyPerformanceEvaluationForm->job_description_id, $record->competency_id, $rows->behavior_id);
if ($behavior && $jobDescriptionBehaviorCompetency) {
array_push($behaviors, [
'id_section' => $competency->uuid,
'id_option' => $behavior->uuid,
'title' => $behavior->description,
'level' => $jobDescriptionBehaviorCompetency->level,
'answer' => ''
]);
}
}
array_push($competencies, [
'id_section' => $competency->uuid,
'title' => $competenceType->name. ' '.$competency->name,
'text' => $competency->description,
'type'=>'competency',
'options' => $behaviors
]);
}
}
return new JsonModel([
'success' => true,
'data' => [
'name' => $companyPerformanceEvaluationForm->name,
'description' => $companyPerformanceEvaluationForm->description,
'text' => $companyPerformanceEvaluationForm->text,
'competencies'=>$competencies,
'content' => array_push($content, $competencies),
]
]);
}
if ($request->isPost()) {
$form = new PerformanceEvaluationTestForm();
$dataPost = $request->getPost()->toArray();
$form->setData($dataPost);
if ($form->isValid()) {
$dataPost = (array) $form->getData();
$performanceEvaluationTest = new CompanyPerformanceEvaluationTest();
$performanceEvaluationTest->company_id = $companyPerformanceEvaluationForm->company_id;
$performanceEvaluationTest->form_id = $companyPerformanceEvaluationForm->id;
$performanceEvaluationTest->user_id = $currentUser->id;
$performanceEvaluationTest->status = $dataPost['status'];
$performanceEvaluationTest->content = $dataPost['content'];
//Check if the form is already registered
$companyPerformanceEvaluationTest = $companyPerformanceEvaluationTestMapper->fetchOneBy($companyPerformanceEvaluationForm->id, $currentUser->id);
$result = $companyPerformanceEvaluationTest ?
$companyPerformanceEvaluationTestMapper->update($performanceEvaluationTest, $companyPerformanceEvaluationTest->id) :
$companyPerformanceEvaluationTestMapper->insert($performanceEvaluationTest);
if ($result) {
$this->logger->info('Se agrego un nuevo test de auto-evaluación : ' . $companyPerformanceEvaluationForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => $companyPerformanceEvaluationTest ? 'LABEL_RECORD_UPDATED' : 'LABEL_RECORD_ADDED'
];
} else {
$data = [
'success' => false,
'data' => $companyPerformanceEvaluationTestMapper->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'
]);
}
}