Rev 6169 | Rev 15444 | 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\Log\LoggerInterface;use Laminas\View\Model\ViewModel;use Laminas\View\Model\JsonModel;use LeadersLinked\Mapper\QueryMapper;use Laminas\Db\Sql\Select;use LeadersLinked\Library\Functions;use LeadersLinked\Mapper\CompanySelfEvaluationFormMapper;use LeadersLinked\Mapper\CompanySelfEvaluationTestMapper;use LeadersLinked\Form\CompanySelfEvaluationTestForm;use LeadersLinked\Model\CompanySelfEvaluationForm;use LeadersLinked\Model\CompanySelfEvaluationTest;use LeadersLinked\Mapper\UserMapper;use LeadersLinked\Hydrator\ObjectPropertyHydrator;use LeadersLinked\Library\SelfEvaluationPdf;use LeadersLinked\Mapper\CompanySelfEvaluationFormUserMapper;class SelfEvaluationReviewController 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;}public function indexAction() {$request = $this->getRequest();$currentUserPlugin = $this->plugin('currentUserPlugin');$currentCompany = $currentUserPlugin->getCompany();$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 = $this->params()->fromQuery('search', []);$search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);$queryMapper = QueryMapper::getInstance($this->adapter);$select = $queryMapper->getSql()->select();$select->columns(['uuid', 'status', 'user_id', 'added_on']);$select->from(['test' => CompanySelfEvaluationTestMapper::_TABLE]);$select->join(['form' => CompanySelfEvaluationFormMapper::_TABLE], 'test.form_id = form.id', ['name', 'language']);$select->join(['user' => UserMapper::_TABLE], 'test.user_id = user.id', ['first_name', 'last_name'], Select::JOIN_LEFT_OUTER);$select->where->equalTo('form.status', CompanySelfEvaluationForm::STATUS_ACTIVE);$select->where->notEqualTo('test.status', CompanySelfEvaluationTest::STATUS_DRAFT);//Search itemsif ($search) {$select->where->NEST->like('name', '%' . $search . '%')->OR->like('first_name', '%' . $search . '%')->OR->like('last_name', '%' . $search . '%')->UNNEST;}$select->order('added_on DESC');$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'],'user' => $record['first_name'] . ' ' . $record['last_name'],'language' => $language,'status' => $status,'added_on' => $record['added_on'],'actions' => ['link_edit' => $record['status'] != CompanySelfEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('self-evaluation/reviews/edit', ['id' => $record['uuid']]) : '','link_report' => $record['status'] == CompanySelfEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('self-evaluation/reviews/report', ['id' => $record['uuid']]) : '',]];array_push($items, $item);}return new JsonModel(['success' => true,'data' => ['items' => $items,'total' => count($items),]]);} else {$form = new CompanySelfEvaluationTestForm();$this->layout()->setTemplate('layout/layout-backend');$viewModel = new ViewModel();$viewModel->setTemplate('leaders-linked/self-evaluation-reviews/index.phtml');$viewModel->setVariable('form', $form);return $viewModel;}} else {return new JsonModel(['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED']);;}}public function editAction() {$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);}$companySelfEvaluationTestMapper = CompanySelfEvaluationTestMapper::getInstance($this->adapter);$companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneByUuid($uuid);if (!$companySelfEvaluationTest) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if ($request->isPost()) {$form = new CompanySelfEvaluationTestForm();$dataPost = $request->getPost()->toArray();$form->setData($dataPost);if ($form->isValid()) {$selfEvaluationTest = new CompanySelfEvaluationTest();$selfEvaluationTest->status = CompanySelfEvaluationTest::STATUS_COMPLETED;$selfEvaluationTest->content = $dataPost['content'];$selfEvaluationTest->comments = $dataPost['comments'];$result = $companySelfEvaluationTestMapper->update($selfEvaluationTest, $companySelfEvaluationTest->id);if ($result) {$data = ['success' => true,'data' => 'LABEL_RECORD_UPDATED'];} 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]);}} else if ($request->isGet()) {$hydrator = new ObjectPropertyHydrator();//get form data$companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);$companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOne($companySelfEvaluationTest->form_id);//get user data$CompanyUserMapper = UserMapper::getInstance($this->adapter);$userMapper = $CompanyUserMapper->fetchOne($companySelfEvaluationTest->user_id);if ($companySelfEvaluationForm && $userMapper) {$data = ['success' => true,'data' => ['id' => $companySelfEvaluationTest->id,'name' => $companySelfEvaluationForm->name,'text' => $companySelfEvaluationForm->text,'user' => $userMapper->first_name . ' ' . $userMapper->last_name,'status' => $companySelfEvaluationTest->status,'comments' => $companySelfEvaluationTest->comments,'content' => json_decode($companySelfEvaluationTest->content),]];return new JsonModel($data);} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];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);}$companySelfEvaluationTestMapper = CompanySelfEvaluationTestMapper::getInstance($this->adapter);$companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneByUuid($uuid);if (!$companySelfEvaluationTest) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if ($request->isGet()) {$hydrator = new ObjectPropertyHydrator();//get form data$companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);$companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOne($companySelfEvaluationTest->form_id);//get user data$CompanyUserMapper = UserMapper::getInstance($this->adapter);$userMapper = $CompanyUserMapper->fetchOne($companySelfEvaluationTest->user_id);if ($companySelfEvaluationForm && $userMapper) {return $this->renderPDF($companySelfEvaluationForm, $companySelfEvaluationTest, $userMapper);} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}} else {$data = ['success' => false,'data' => 'ERROR_METHOD_NOT_ALLOWED'];return new JsonModel($data);}return new JsonModel($data);}/*** Render Pdf document* @param CompanySelfEvaluationForm $companySelfEvaluationForm* @param CompanySelfEvaluationTest $companySelfEvaluationTest* @param UserMapper $userMapper* @return mixed*/public function renderPDF($companySelfEvaluationForm, $companySelfEvaluationTest, $userMapper) {$target_path = $this->config['leaderslinked.fullpath.self_evaluation'] . DIRECTORY_SEPARATOR . $companySelfEvaluationTest->uuid;if(file_exists($target_path)) {Functions::deleteFiles($target_path);} else {@mkdir($target_path, 0755, true);}// Set Data$headerFormName = utf8_decode($companySelfEvaluationForm->name);$headerUserName = utf8_decode('Informe de Auto-evaluación: ' . trim($userMapper->first_name . ' ' . $userMapper->last_name) . ' al ' . date("m-d-Y H:i:s", strtotime($companySelfEvaluationTest->added_on)));$sections = json_decode($companySelfEvaluationTest->content, true);$labels = ['Total', 'Logrado'];//Generate New PDF$pdf = new SelfEvaluationPdf();$pdf->AliasNbPages();$pdf->AddPage();// Set header secundary$pdf->customHeader($headerFormName, $headerUserName);$valueFormSections = 0;$valueFormQuestions = 0;$countSection = 0;for ($i = 0; $i < count($sections); $i++) {if ($countSection > 1) {$countSection = 0;$pdf->AddPage();$pdf->customHeader($headerFormName, $headerUserName);}$pdf->SetY(70 + (92 * $countSection));$totalQuestion = 0;$valueSection = floatval($sections[$i]['value']);$valueFormSections = $valueFormSections + $valueSection;for ($j = 0; $j < count($sections[$i]['questions']); $j++) {$totalQuestion = $totalQuestion + $this->getPtosAnswer($sections[$i]['questions'][$j]);}$values = [$valueSection, $totalQuestion];$valueFormQuestions = $valueFormQuestions + $totalQuestion;$filename = $target_path . DIRECTORY_SEPARATOR . $sections[$i]['slug_section'] . '.png';$pdf->pieChart($labels, $values, '', $filename);$pdf->SetFont('Arial', '', 8);$pdf->Cell(190, 10, utf8_decode($sections[$i]['name']), 0, 0, 'C');$pdf->setY($pdf->getY() + 10);// Salto de línea$pdf->Image($filename, 60, $pdf->getY(), 90);$pdf->setY($pdf->getY() + 60);$countSection++;}$pdf->AddPage();$pdf->customHeader($headerFormName, $headerUserName);$pdf->SetFont('Arial', 'B', 10);$pdf->Cell(190, 10, utf8_decode('Desempeño General'), 0, 0, 'C');$pdf->setY($pdf->getY() + 10);$values = [$valueFormSections, $valueFormQuestions];$filenameGeneral = $target_path . DIRECTORY_SEPARATOR . 'general.png';$pdf->pieChart($labels, $values, '', $filenameGeneral);$pdf->Image($filenameGeneral, 60, $pdf->getY(), 90);$pdf->setY($pdf->getY() + 60);if ($companySelfEvaluationTest->comments) {$pdf->SetFont('Arial', 'B', 10);$pdf->Cell(190, 10, utf8_decode('Comentarios finales'), 0, 0, 'C');$pdf->setY($pdf->getY() + 10);$pdf->SetFont('Arial', '', 8);$pdf->MultiCell(180, 8, utf8_decode($companySelfEvaluationTest->comments));$pdf->setY(60);}return $pdf->Output();}/*** get total ptos Answer* @param array $question* @return int*/public function getPtosAnswer($question) {$ptos = 0;if ($question['type'] == "open" || $question['type'] == "rating-open" || $question['type'] == "rating-range") {$ptos = isset($question['question_value']) ? $question['question_value'] : 0;} else {if ($question['type'] == 'multiple') {for ($o = 0; $o < count($question['options']); $o++) {if (in_array($question['options'][$o]['slug_option'], $question['answer'])) {$ptos = $ptos + $question['options'][$o]['value'];}}} else {for ($o = 0; $o < count($question['options']); $o++) {if ($question['options'][$o]['slug_option'] == $question['answer']) {$ptos = $question['options'][$o]['value'];}}}}return $ptos;}}