Rev 16768 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpdeclare(strict_types=1);namespace LeadersLinked\Controller;use Laminas\Db\Adapter\AdapterInterface;use Laminas\Cache\Storage\Adapter\AbstractAdapter;use Laminas\Mvc\Controller\AbstractActionController;use Laminas\Mvc\I18n\Translator;use Laminas\Log\LoggerInterface;use Laminas\View\Model\ViewModel;use Laminas\View\Model\JsonModel;use LeadersLinked\Mapper\QueryMapper;use LeadersLinked\Mapper\UserMapper;use Laminas\Hydrator\ArraySerializableHydrator;use Laminas\Db\ResultSet\HydratingResultSet;use Laminas\Paginator\Adapter\DbSelect;use Laminas\Paginator\Paginator;use LeadersLinked\Mapper\PerformanceEvaluationTestMapper;use LeadersLinked\Mapper\PerformanceEvaluationFormMapper;use LeadersLinked\Model\PerformanceEvaluationTest;use LeadersLinked\Form\PerformanceEvaluation\PerformanceEvaluationTestForm;use Laminas\Hydrator\ObjectPropertyHydrator;use LeadersLinked\Library\Functions;use LeadersLinked\Library\PerformanceEvaluationInterviewPDF;class PerformanceEvaluationTestController extends AbstractActionController {/**** @var AdapterInterface*/private $adapter;/**** @var AbstractAdapter*/private $cache;/**** @var LoggerInterface*/private $logger;/**** @var array*/private $config;/**** @var Translator*/private $translator;/**** @param AdapterInterface $adapter* @param AbstractAdapter $cache* @param LoggerInterface $logger* @param array $config* @param Translator $translator*/public function __construct($adapter, $cache, $logger, $config, $translator){$this->adapter = $adapter;$this->cache = $cache;$this->logger = $logger;$this->config = $config;$this->translator = $translator;}public function indexAction() {$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$currentCompany = $currentUserPlugin->getCompany();$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) {$data = ['items' => [],'total' => 0,];$search = $this->params()->fromQuery('search');$search = empty($search) ? '' : filter_var($search, FILTER_SANITIZE_STRING);$start = intval($this->params()->fromQuery('start', 0), 10);$records_x_page = intval($this->params()->fromQuery('length', 10), 10);$page = intval($start / $records_x_page);$page++;$order = $this->params()->fromQuery('order', []);$order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);$order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));$fields = ['max_date'];$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';if (!in_array($order_direction, ['ASC', 'DESC'])) {$order_direction = 'ASC';}$acl = $this->getEvent()->getViewModel()->getVariable('acl');//$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'performance-evaluation/evaluations/delete');$queryMapper = QueryMapper::getInstance($this->adapter);$sql = $queryMapper->getSql();$select = $sql->select();$select->columns(['id','uuid','last_date','status','added_on','updated_on']);$select->from(['tb1' => PerformanceEvaluationTestMapper::_TABLE]);$select->join(['tb2' => PerformanceEvaluationFormMapper::_TABLE], 'tb1.form_id = tb2.id ', ['form' => 'name']);$select->join(['tb3' => UserMapper::_TABLE], 'tb1.supervisor_id = tb3.id ', ['supervisor_first_name' => 'first_name', 'supervisor_last_name' => 'last_name', 'supervisor_email' => 'email']);$select->join(['tb4' => UserMapper::_TABLE], 'tb1.employee_id = tb4.id ', ['employee_first_name' => 'first_name', 'employee_last_name' => 'last_name', 'employee_email' => 'email']);$select->where->equalTo('tb1.company_id', $currentCompany->id);$select->where->equalTo('tb1.type', PerformanceEvaluationTest::TYPE_BOTH);if ($search) {$select->where->nest()->like('tb1.last_date', '%' . $search . '%')->or->like('tb2.name', '%' . $search . '%')->or->like('tb3.first_name', '%' . $search . '%')->or->like('tb3.last_name', '%' . $search . '%')->or->like('tb3.email', '%' . $search . '%')->or->like('tb4.first_name', '%' . $search . '%')->or->like('tb4.last_name', '%' . $search . '%')->or->like('tb4.email', '%' . $search . '%')->unnest();}$select->order('tb1.last_date DESC, tb2.name ASC');$hydrator = new ArraySerializableHydrator();$resultset = new HydratingResultSet($hydrator);$adapter = new DbSelect($select, $sql, $resultset);$paginator = new Paginator($adapter);$paginator->setItemCountPerPage($records_x_page);$paginator->setCurrentPageNumber($page);$performanceEvaluationTestMapper = PerformanceEvaluationTestMapper::getInstance($this->adapter);$items = [];$records = $paginator->getCurrentItems();foreach ($records as $record){//$performanceEvaluationTestEmployee = $performanceEvaluationTestMapper->fetchOneByParentIdAndType($record['id'], PerformanceEvaluationTest::TYPE_EMPLOYEE);//$performanceEvaluationTestSupervisor = $performanceEvaluationTestMapper->fetchOneByParentIdAndType($record['id'], PerformanceEvaluationTest::TYPE_SUPERVISOR);$dt = \DateTime::createFromFormat('Y-m-d', $record['last_date']);$last_date = $dt->format('d/m/Y');switch($record['status']){case PerformanceEvaluationTest::STATUS_PENDING :$status = 'LABEL_PENDING';break;case PerformanceEvaluationTest::STATUS_COMPLETED :$status = 'LABEL_COMPLETED';break;default :$status = 'LABEL_UNKNOW';break;}$actions = ['link_delete' => $this->url()->fromRoute('performance-evaluation/evaluations/delete', ['id' => $record['uuid']]),];if($record['status'] == PerformanceEvaluationTest::STATUS_COMPLETED) {$actions['link_pdf_both'] = $this->url()->fromRoute('performance-evaluation/evaluations/report', ['id' => $record['uuid'] ]);} else {$actions['link_pdf_both'] = '';}$performanceEvaluationTestEmployee = $performanceEvaluationTestMapper->fetchOneByParentIdAndType( $record['id'], PerformanceEvaluationTest::TYPE_EMPLOYEE );if($performanceEvaluationTestEmployee->status == PerformanceEvaluationTest::STATUS_COMPLETED) {$actions['link_pdf_employee'] = $this->url()->fromRoute('performance-evaluation/evaluations/report', ['id' => $performanceEvaluationTestEmployee->uuid ]);} else {$actions['link_pdf_employee'] = '';}$performanceEvaluationTestSupervisor = $performanceEvaluationTestMapper->fetchOneByParentIdAndType( $record['id'], PerformanceEvaluationTest::TYPE_SUPERVISOR );if($performanceEvaluationTestSupervisor->status == PerformanceEvaluationTest::STATUS_COMPLETED) {$actions['link_pdf_supervisor'] = $this->url()->fromRoute('performance-evaluation/evaluations/report', ['id' => $performanceEvaluationTestSupervisor->uuid ]);} else {$actions['link_pdf_supervisor'] = '';}$item = ['last_date' => $last_date,'form' => $record['form'],'supervisor_first_name' => $record['supervisor_first_name'],'supervisor_last_name' => $record['supervisor_last_name'],'supervisor_email' => $record['supervisor_email'],'employee_first_name' => $record['employee_first_name'],'employee_last_name' => $record['employee_last_name'],'employee_email' => $record['employee_email'],'status' => $status,'actions' => $actions];array_push($items, $item);}$data['items'] = $items;$data['total'] = $paginator->getTotalItemCount();return new JsonModel(['success' => true,'data' => $data]);} else {$form = new PerformanceEvaluationTestForm($this->adapter, $currentCompany->id);$this->layout()->setTemplate('layout/layout-backend');$viewModel = new ViewModel();$viewModel->setTemplate('leaders-linked/performance-evaluation-tests/index.phtml');$viewModel->setVariables(['form' => $form,]);return $viewModel;}} else {return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);}}public function addAction(){$request = $this->getRequest();$currentUserPlugin = $this->plugin('currentUserPlugin');$currentCompany = $currentUserPlugin->getCompany();$currentUser = $currentUserPlugin->getUser();$request = $this->getRequest();if ($request->isPost()) {$dataPost = $request->getPost()->toArray();$dt = \DateTime::createFromFormat('d/m/Y', $dataPost['last_date']);if($dt) {$dataPost['last_date'] = $dt->format('Y-m-d');}$form = new PerformanceEvaluationTestForm($this->adapter, $currentCompany->id);$form->setData($dataPost);if ($form->isValid()) {$dataPost = (array) $form->getData();$companyPerformanceFormMapper = PerformanceEvaluationFormMapper::getInstance($this->adapter);$companyPerformanceForm = $companyPerformanceFormMapper->fetchOneByUuid($dataPost['form_id']);$userMapper = UserMapper::getInstance($this->adapter);$supervisor = $userMapper->fetchOneByUuid($dataPost['supervisor_id']);$employee = $userMapper->fetchOneByUuid($dataPost['employee_id']);$performanceEvaluationTest = new PerformanceEvaluationTest();$performanceEvaluationTest->company_id = $currentCompany->id;$performanceEvaluationTest->form_id = $companyPerformanceForm->id;$performanceEvaluationTest->supervisor_id = $supervisor->id;$performanceEvaluationTest->employee_id = $employee->id;$performanceEvaluationTest->last_date = $dataPost['last_date'];$performanceEvaluationTest->type = PerformanceEvaluationTest::TYPE_BOTH;$performanceEvaluationTest->status = PerformanceEvaluationTest::STATUS_PENDING;$performanceEvaluationTest->content = $companyPerformanceForm->content;$performanceTestMapper = PerformanceEvaluationTestMapper::getInstance($this->adapter);$result = $performanceTestMapper->insert($performanceEvaluationTest);if ($result) {$this->logger->info('Se agrego la Evaluación para el Formulario de Evaluación de Desempeño : ' . $companyPerformanceForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);$performanceEvaluationTestEmployee = new PerformanceEvaluationTest();$performanceEvaluationTestEmployee->company_id = $currentCompany->id;$performanceEvaluationTestEmployee->form_id = $companyPerformanceForm->id;$performanceEvaluationTestEmployee->employee_id = $employee->id;$performanceEvaluationTestEmployee->supervisor_id = $supervisor->id;$performanceEvaluationTestEmployee->parent_id = $performanceEvaluationTest->id;$performanceEvaluationTestEmployee->last_date = $performanceEvaluationTest->last_date;$performanceEvaluationTestEmployee->type = PerformanceEvaluationTest::TYPE_EMPLOYEE;$performanceEvaluationTestEmployee->status = PerformanceEvaluationTest::STATUS_PENDING;$performanceEvaluationTestEmployee->content = $companyPerformanceForm->content;$performanceTestMapper->insert($performanceEvaluationTestEmployee);$performanceEvaluationTestSurpevisor = new PerformanceEvaluationTest();$performanceEvaluationTestSurpevisor->company_id = $currentCompany->id;$performanceEvaluationTestSurpevisor->form_id = $companyPerformanceForm->id;$performanceEvaluationTestSurpevisor->employee_id = $employee->id;$performanceEvaluationTestSurpevisor->supervisor_id = $supervisor->id;$performanceEvaluationTestSurpevisor->parent_id = $performanceEvaluationTest->id;$performanceEvaluationTestSurpevisor->last_date = $performanceEvaluationTest->last_date;$performanceEvaluationTestSurpevisor->type = PerformanceEvaluationTest::TYPE_SUPERVISOR;$performanceEvaluationTestSurpevisor->status = PerformanceEvaluationTest::STATUS_PENDING;$performanceEvaluationTestSurpevisor->content = $companyPerformanceForm->content;$performanceTestMapper->insert($performanceEvaluationTestSurpevisor);$data = ['success' => true,'data' => 'LABEL_RECORD_ADDED'];} else {$data = ['success' => false,'data' => $performanceTestMapper->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]);}} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}return new JsonModel($data);}public function deleteAction(){$currentUserPlugin = $this->plugin('currentUserPlugin');$currentUser = $currentUserPlugin->getUser();$currentCompany = $currentUserPlugin->getCompany();$request = $this->getRequest();$uuid = $this->params()->fromRoute('id');if (!$uuid) {$data = ['success' => false,'data' => 'ERROR_INVALID_PARAMETER'];return new JsonModel($data);}$performanceTestMapper = PerformanceEvaluationTestMapper::getInstance($this->adapter);$performanceTest = $performanceTestMapper->fetchOneByUuid($uuid);if (!$performanceTest) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if ($currentCompany && $performanceTest->company_id != $currentCompany->id) {$data = ['success' => false,'data' => 'ERROR_UNAUTHORIZED'];return new JsonModel($data);}if ($request->isPost()) {$result = $performanceTestMapper->delete($performanceTest);if ($result) {$performanceFormMapper = PerformanceEvaluationFormMapper::getInstance($this->adapter);$performanceForm = $performanceFormMapper->fetchOne($performanceTest->form_id);$this->logger->info('Se borro una evaluación del formulario ' . $performanceForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);$data = ['success' => true,'data' => 'LABEL_RECORD_DELETED'];} else {$data = ['success' => false,'data' => $performanceTestMapper->getError()];return new JsonModel($data);}} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}return new JsonModel($data);}public function reportAction(){$request = $this->getRequest();$currentUserPlugin = $this->plugin('currentUserPlugin');$currentCompany = $currentUserPlugin->getCompany();$currentUser = $currentUserPlugin->getUser();$request = $this->getRequest();$uuid = $this->params()->fromRoute('id');if (!$uuid) {$data = ['success' => false,'data' => 'ERROR_INVALID_PARAMETER'];return new JsonModel($data);}$performanceEvaluationTestMapper = PerformanceEvaluationTestMapper::getInstance($this->adapter);$performanceEvaluationTest = $performanceEvaluationTestMapper->fetchOneByUuid($uuid);if (!$performanceEvaluationTest) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if ($performanceEvaluationTest->company_id != $currentCompany->id) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}if ($performanceEvaluationTest->employee_id != $currentUser->id&& $performanceEvaluationTest->supervisor_id != $currentUser->id ) {return new JsonModel(['success' => false,'data' => 'ERROR_UNAUTHORIZED']);}if($performanceEvaluationTest->status ==PerformanceEvaluationTest::STATUS_PENDING) {return new JsonModel(['success' => false,'data' => 'ERROR_RECORD_IS_PENDING']);}$request = $this->getRequest();if ($request->isGet()) {switch( $performanceEvaluationTest->type){case PerformanceEvaluationTest::TYPE_BOTH :$type = $this->translator->translate('LABEL_PERFORMANCE_EVALUATION_TYPE_BOTH');break;case PerformanceEvaluationTest::TYPE_SUPERVISOR :$type = $this->translator->translate('LABEL_PERFORMANCE_EVALUATION_TYPE_SUPERVISOR');break;case PerformanceEvaluationTest::TYPE_EMPLOYEE :$type = $this->translator->translate('LABEL_PERFORMANCE_EVALUATION_TYPE_EMPLOYEE');break;default :$type = $this->translator->translate('LABEL_UNKNOWN');break;}$performanceEvaluationFormMapper = PerformanceEvaluationFormMapper::getInstance($this->adapter);$performanceEvaluationForm = $performanceEvaluationFormMapper->fetchOne($performanceEvaluationTest->form_id);$userMapper = UserMapper::getInstance($this->adapter);$employee = $userMapper->fetchOne($performanceEvaluationTest->employee_id);$filename = $performanceEvaluationForm->name . '-' . trim($employee->first_name) . '-' . trim($employee->last_name) . '-' . $type . '-' . date('Y-m-d H:i a') . '.pdf';$filename = Functions::normalizeStringFilename( $filename );$content = base64_encode($this->renderPDF($performanceEvaluationTest));$data = ['success' => true,'data' => ['basename' => $filename,'content' => $content]];return new JsonModel($data);/*$content = $this->renderPdf($currentCompany, $jobDescription);$response = new Response();$response->setStatusCode(200);$response->setContent($content);$headers = $response->getHeaders();$headers->clearHeaders();$headers->addHeaderLine('Content-Description: File Transfer');$headers->addHeaderLine('Content-Type: application/pdf');//$headers->addHeaderLine('Content-Disposition: attachment; filename=' . $filename);$headers->addHeaderLine('Content-Transfer-Encoding: binary');$headers->addHeaderLine('Expires: 0');$headers->addHeaderLine('Cache-Control: must-revalidate');$headers->addHeaderLine('Pragma: public');return $response;*/return ;} else {return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);}}/*** Render PDF* @param PerformanceEvaluationTest $performanceEvaluationTest* @return mixed*/private function renderPDF($performanceEvaluationTest){$request = $this->getRequest();$currentUserPlugin = $this->plugin('currentUserPlugin');$currentCompany = $currentUserPlugin->getCompany();$currentUser = $currentUserPlugin->getUser();//Generate New PDF$pdf = new PerformanceEvaluationInterviewPDF();$target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $currentCompany->uuid;$header = $currentCompany->header ? $target_path . DIRECTORY_SEPARATOR . $currentCompany->header : '';if (empty($header) || !file_exists($header)) {$header = $this->config['leaderslinked.images_default.company_pdf_header'];}$footer = $currentCompany->footer ? $target_path . DIRECTORY_SEPARATOR . $currentCompany->footer : '';if (empty($footer) || !file_exists($footer)) {$footer = $this->config['leaderslinked.images_default.company_pdf_footer'];}$content = json_decode($performanceEvaluationTest->content);$pdf->header = $header;$pdf->footer = $footer;$pdf->translator = $this->translator;$pdf->SetMargins(10, 0, 10);$pdf->AliasNbPages();$pdf->AddPage();switch( $performanceEvaluationTest->type){case PerformanceEvaluationTest::TYPE_BOTH :$type = $this->translator->translate('LABEL_PERFORMANCE_EVALUATION_TYPE_BOTH');break;case PerformanceEvaluationTest::TYPE_SUPERVISOR :$type = $this->translator->translate('LABEL_PERFORMANCE_EVALUATION_TYPE_SUPERVISOR');break;case PerformanceEvaluationTest::TYPE_EMPLOYEE :$type = $this->translator->translate('LABEL_PERFORMANCE_EVALUATION_TYPE_EMPLOYEE');break;default :$type = $this->translator->translate('LABEL_UNKNOWN');break;}$dt = \DateTime::createFromFormat('Y-m-d H:i:s', $performanceEvaluationTest->updated_on);$evaluated = '';$evaluator = '';$userMapper = UserMapper::getInstance($this->adapter);if($performanceEvaluationTest->employee_id) {$user = $userMapper->fetchOne($performanceEvaluationTest->employee_id);if($user) {$evaluated = ucwords(strtolower(trim($user->first_name . ' ' . $user->last_name)));}}if($performanceEvaluationTest->supervisor_id) {$user = $userMapper->fetchOne($performanceEvaluationTest->supervisor_id);if($user) {$evaluator = ucwords(strtolower(trim($user->first_name . ' ' . $user->last_name)));}}$rows = [['title' => $this->translator->translate('LABEL_TYPE') . ' : ','content' => $type,],['title' => $this->translator->translate('LABEL_PDF_PERFORMANCE_EVALUATION_POSITION') . ' : ','content' => $content->name,],['title' => $this->translator->translate('LABEL_PDF_PERFORMANCE_EVALUATION_EVALUATED') . ' : ','content' => utf8_decode($evaluated),],['title' => $this->translator->translate('LABEL_PDF_PERFORMANCE_EVALUATION_EVALUATE_SIGNATURE') . ' : ','content' => ''],['title' => $this->translator->translate('LABEL_PDF_PERFORMANCE_EVALUATION_EVALUATOR') . ' : ','content' => utf8_decode($evaluator),],['title' => $this->translator->translate('LABEL_PDF_PERFORMANCE_EVALUATION_EVALUATOR_SIGNATURE') . ' : ','content' => ''],['title' => $this->translator->translate( 'LABEL_PDF_PERFORMANCE_EVALUATION_MANAGER_SIGNATURE') . ' : ','content' => ''],['title' => $this->translator->translate('LABEL_DATE'),'content' => $dt->format('d/m/Y H:i a')]];$pdf->borderTable($this->translator->translate('LABEL_PDF_PERFORMANCE_EVALUATION_TITLE'), $rows);$pdf->evaluationTable($content->comment, $content->points);// Competenciesif ($content->competencies_selected) {$pdf->AddPage();$i = 0;$max = count($content->competencies_selected);for($i = 0; $i < $max; $i++){$competency_selected = $content->competencies_selected[$i];$j = $i + 1;$last = $j == $max;$pdf->competencyTable($i, $competency_selected, $content->competencies, $content->competency_types, $content->behaviors, $content->evaluation, $last);}}return $pdf->Output('S');}}