Rev 16768 | 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\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\SelfEvaluationFormMapper;use LeadersLinked\Mapper\SelfEvaluationTestMapper;use LeadersLinked\Form\SelfEvaluation\SelfEvaluationTestForm;use LeadersLinked\Model\SelfEvaluationForm;use LeadersLinked\Model\SelfEvaluationTest;use LeadersLinked\Mapper\UserMapper;use LeadersLinked\Hydrator\ObjectPropertyHydrator;use LeadersLinked\Library\SelfEvaluationPdf;use LeadersLinked\Model\Company;class SelfEvaluationReviewController extends AbstractActionController {/**** @var \Laminas\Db\Adapter\AdapterInterface*/private $adapter;/**** @var \LeadersLinked\Cache\CacheInterface*/private $cache;/**** @var \Laminas\Log\LoggerInterface*/private $logger;/**** @var array*/private $config;/**** @var \Laminas\Mvc\I18n\Translator*/private $translator;/**** @param \Laminas\Db\Adapter\AdapterInterface $adapter* @param \LeadersLinked\Cache\CacheInterface $cache* @param \Laminas\Log\LoggerInterface LoggerInterface $logger* @param array $config* @param \Laminas\Mvc\I18n\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() {$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']) ? '' : Functions::sanitizeFilterString($search['value']);$queryMapper = QueryMapper::getInstance($this->adapter);$select = $queryMapper->getSql()->select();$select->columns(['uuid', 'status', 'user_id', 'added_on']);$select->from(['test' => SelfEvaluationTestMapper::_TABLE]);$select->join(['form' => SelfEvaluationFormMapper::_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', SelfEvaluationForm::STATUS_ACTIVE);$select->where->notEqualTo('test.status', SelfEvaluationTest::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 SelfEvaluationForm::LANGUAGE_ENGLISH :$language = 'LABEL_ENGLISH';break;case SelfEvaluationForm::LANGUAGE_SPANISH :$language = 'LABEL_SPANISH';break;default :$language = '';break;}switch ($record['status']) {case SelfEvaluationTest::STATUS_COMPLETED :$status = 'LABEL_COMPLETED';break;case SelfEvaluationTest::STATUS_PENDING :$status = 'LABEL_PENDING';break;case SelfEvaluationTest::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'] != SelfEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('self-evaluation/reviews/edit', ['id' => $record['uuid']]) : '','link_report' => $record['status'] == SelfEvaluationTest::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 SelfEvaluationTestForm();$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);}$selfEvaluationTestMapper = SelfEvaluationTestMapper::getInstance($this->adapter);$selfEvaluationTest = $selfEvaluationTestMapper->fetchOneByUuid($uuid);if (!$selfEvaluationTest) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if ($request->isPost()) {$form = new SelfEvaluationTestForm();$dataPost = $request->getPost()->toArray();$form->setData($dataPost);if ($form->isValid()) {$selfEvaluationTest = new SelfEvaluationTest();$selfEvaluationTest->status = SelfEvaluationTest::STATUS_COMPLETED;$selfEvaluationTest->content = $dataPost['content'];$selfEvaluationTest->comments = $dataPost['comments'];$result = $selfEvaluationTestMapper->update($selfEvaluationTest, $selfEvaluationTest->id);if ($result) {$data = ['success' => true,'data' => 'LABEL_RECORD_UPDATED'];} else {$data = ['success' => false,'data' => $selfEvaluationTestMapper->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$selfEvaluationFormMapper = SelfEvaluationFormMapper::getInstance($this->adapter);$selfEvaluationForm = $selfEvaluationFormMapper->fetchOne($selfEvaluationTest->form_id);//get user data$CompanyUserMapper = UserMapper::getInstance($this->adapter);$userMapper = $CompanyUserMapper->fetchOne($selfEvaluationTest->user_id);if ($selfEvaluationForm && $userMapper) {$data = ['success' => true,'data' => ['id' => $selfEvaluationTest->id,'name' => $selfEvaluationForm->name,'text' => $selfEvaluationForm->text,'user' => $userMapper->first_name . ' ' . $userMapper->last_name,'status' => $selfEvaluationTest->status,'comments' => $selfEvaluationTest->comments,'content' => json_decode($selfEvaluationTest->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);}$selfEvaluationTestMapper = SelfEvaluationTestMapper::getInstance($this->adapter);$selfEvaluationTest = $selfEvaluationTestMapper->fetchOneByUuid($uuid);if (!$selfEvaluationTest) {$data = ['success' => false,'data' => 'ERROR_RECORD_NOT_FOUND'];return new JsonModel($data);}if ($request->isGet()) {$hydrator = new ObjectPropertyHydrator();//get form data$selfEvaluationFormMapper = SelfEvaluationFormMapper::getInstance($this->adapter);$selfEvaluationForm = $selfEvaluationFormMapper->fetchOne($selfEvaluationTest->form_id);//get user data$CompanyUserMapper = UserMapper::getInstance($this->adapter);$userMapper = $CompanyUserMapper->fetchOne($selfEvaluationTest->user_id);if ($selfEvaluationForm && $userMapper) {return $this->renderPDF($currentCompany, $selfEvaluationForm, $selfEvaluationTest, $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 Company $company* @param SelfEvaluationForm $selfEvaluationForm* @param SelfEvaluationTest $selfEvaluationTest* @param UserMapper $userMapper* @return mixed*/public function renderPDF($company, $selfEvaluationForm, $selfEvaluationTest, $userMapper) {$target_path = $this->config['leaderslinked.fullpath.self_evaluation'] . DIRECTORY_SEPARATOR . $selfEvaluationTest->uuid;if(file_exists($target_path)) {Functions::deleteFiles($target_path);} else {@mkdir($target_path, 0755, true);}// Set Data$headerFormName = Functions::utf8_decode($selfEvaluationForm->name);$headerUserName = Functions::utf8_decode('Informe de Auto-evaluación: ' . trim($userMapper->first_name . ' ' . $userMapper->last_name) . ' al ' . date("m-d-Y H:i:s", strtotime($selfEvaluationTest->added_on)));$sections = json_decode($selfEvaluationTest->content, true);$labels = ['Total', 'Logrado'];//Generate New PDF$pdf = new SelfEvaluationPdf();$target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $company->uuid;$header = $company->header ? $target_path . DIRECTORY_SEPARATOR . $company->header : '';if(empty($header) || !file_exists($header)) {$header = $this->config['leaderslinked.images_default.company_pdf_header'];}$footer = $company->footer ? $target_path . DIRECTORY_SEPARATOR . $company->footer : '';if(empty($footer) || !file_exists($footer)) {$footer = $this->config['leaderslinked.images_default.company_pdf_footer'];}$pdf->header = $header;$pdf->footer = $footer;$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, Functions::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, Functions::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 ($selfEvaluationTest->comments) {$pdf->SetFont('Arial', 'B', 10);$pdf->Cell(190, 10, Functions::utf8_decode('Comentarios finales'), 0, 0, 'C');$pdf->setY($pdf->getY() + 10);$pdf->SetFont('Arial', '', 8);$pdf->MultiCell(180, 8, Functions::utf8_decode($selfEvaluationTest->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;}}