| 5866 |
eleazar |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace LeadersLinked\Controller;
|
|
|
6 |
|
|
|
7 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
8 |
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
|
|
|
9 |
use Laminas\Mvc\Controller\AbstractActionController;
|
|
|
10 |
use Laminas\Log\LoggerInterface;
|
|
|
11 |
use Laminas\View\Model\ViewModel;
|
|
|
12 |
use Laminas\View\Model\JsonModel;
|
|
|
13 |
use LeadersLinked\Mapper\QueryMapper;
|
|
|
14 |
use Laminas\Db\Sql\Select;
|
|
|
15 |
use LeadersLinked\Library\Functions;
|
|
|
16 |
use LeadersLinked\Mapper\SurveyTestMapper;
|
|
|
17 |
use LeadersLinked\Mapper\SurveyMapper;
|
|
|
18 |
use LeadersLinked\Mapper\SurveyFormMapper;
|
|
|
19 |
use LeadersLinked\Form\SurveyTestForm;
|
|
|
20 |
use LeadersLinked\Model\SurveyTest;
|
|
|
21 |
use LeadersLinked\Mapper\UserMapper;
|
|
|
22 |
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
|
|
|
23 |
use LeadersLinked\Library\SurveyReport;
|
|
|
24 |
|
|
|
25 |
class SurveyReportController extends AbstractActionController {
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
*
|
|
|
29 |
* @var AdapterInterface
|
|
|
30 |
*/
|
|
|
31 |
private $adapter;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
*
|
|
|
35 |
* @var AbstractAdapter
|
|
|
36 |
*/
|
|
|
37 |
private $cache;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
*
|
|
|
41 |
* @var LoggerInterface
|
|
|
42 |
*/
|
|
|
43 |
private $logger;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
*
|
|
|
47 |
* @var array
|
|
|
48 |
*/
|
|
|
49 |
private $config;
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
*
|
|
|
53 |
* @param AdapterInterface $adapter
|
|
|
54 |
* @param AbstractAdapter $cache
|
|
|
55 |
* @param LoggerInterface $logger
|
|
|
56 |
* @param array $config
|
|
|
57 |
*/
|
|
|
58 |
public function __construct($adapter, $cache, $logger, $config) {
|
|
|
59 |
$this->adapter = $adapter;
|
|
|
60 |
$this->cache = $cache;
|
|
|
61 |
$this->logger = $logger;
|
|
|
62 |
$this->config = $config;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
public function indexAction() {
|
| 5911 |
eleazar |
66 |
|
| 5866 |
eleazar |
67 |
$request = $this->getRequest();
|
|
|
68 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
69 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
70 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
71 |
|
| 5911 |
eleazar |
72 |
try{
|
| 5866 |
eleazar |
73 |
$request = $this->getRequest();
|
|
|
74 |
if ($request->isGet()) {
|
|
|
75 |
|
|
|
76 |
$headers = $request->getHeaders();
|
|
|
77 |
|
|
|
78 |
$isJson = false;
|
|
|
79 |
if ($headers->has('Accept')) {
|
|
|
80 |
$accept = $headers->get('Accept');
|
|
|
81 |
|
|
|
82 |
$prioritized = $accept->getPrioritized();
|
|
|
83 |
|
|
|
84 |
foreach ($prioritized as $key => $value) {
|
|
|
85 |
$raw = trim($value->getRaw());
|
|
|
86 |
|
|
|
87 |
if (!$isJson) {
|
|
|
88 |
$isJson = strpos($raw, 'json');
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
if ($isJson) {
|
| 5911 |
eleazar |
94 |
$survey_uuid = $this->params()->fromRoute('survey_id');
|
|
|
95 |
|
|
|
96 |
$surveyMapper = SurveyMapper::getInstance($this->adapter);
|
|
|
97 |
$survey = $surveyMapper->fetchOneByUuid($survey_uuid);
|
|
|
98 |
|
| 5866 |
eleazar |
99 |
$search = $this->params()->fromQuery('search', []);
|
|
|
100 |
$search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
|
|
|
101 |
|
|
|
102 |
$page = intval($this->params()->fromQuery('start', 1), 10);
|
|
|
103 |
$records_x_page = intval($this->params()->fromQuery('length', 10), 10);
|
|
|
104 |
$order = $this->params()->fromQuery('order', []);
|
|
|
105 |
$order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
|
|
|
106 |
$order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
|
|
|
107 |
|
|
|
108 |
$fields = ['first_name'];
|
|
|
109 |
$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
|
|
|
110 |
|
|
|
111 |
if (!in_array($order_direction, ['ASC', 'DESC'])) {
|
|
|
112 |
$order_direction = 'ASC';
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
$surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
|
|
|
116 |
$paginator = $surveyTestMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
|
|
|
117 |
|
|
|
118 |
$items = [];
|
|
|
119 |
$records = $paginator->getCurrentItems();
|
|
|
120 |
|
|
|
121 |
foreach ($records as $record) {
|
| 5911 |
eleazar |
122 |
$params = [
|
| 5936 |
eleazar |
123 |
'survey_id' => $survey_uuid,
|
| 5911 |
eleazar |
124 |
'id' => $record->uuid,
|
|
|
125 |
];
|
| 5866 |
eleazar |
126 |
|
|
|
127 |
$item = [
|
|
|
128 |
'id' => $record->id,
|
|
|
129 |
'first_name' => $record->first_name,
|
| 5911 |
eleazar |
130 |
|
| 5866 |
eleazar |
131 |
'actions' => [
|
| 5952 |
eleazar |
132 |
'link_report_one' => $this->url()->fromRoute('survey/report/one', ['id' => $record->uuid, 'survey_id' => $survey_uuid])
|
| 5866 |
eleazar |
133 |
]
|
|
|
134 |
];
|
|
|
135 |
|
|
|
136 |
array_push($items, $item);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
return new JsonModel([
|
|
|
140 |
'success' => true,
|
|
|
141 |
'data' => [
|
|
|
142 |
'items' => $items,
|
|
|
143 |
'total' => $paginator->getTotalItemCount(),
|
|
|
144 |
]
|
|
|
145 |
]);
|
| 5911 |
eleazar |
146 |
} else {
|
|
|
147 |
$surveyMapper = SurveyMapper::getInstance($this->adapter);
|
|
|
148 |
$survies = $surveyMapper->fetchAllByCompanyId($currentCompany->id);
|
|
|
149 |
|
| 5866 |
eleazar |
150 |
$form = new SurveyTestForm($this->adapter, $currentCompany->id);
|
|
|
151 |
|
|
|
152 |
$this->layout()->setTemplate('layout/layout-backend');
|
|
|
153 |
$viewModel = new ViewModel();
|
| 5933 |
eleazar |
154 |
$viewModel->setTemplate('leaders-linked/survey-report/index.phtml');
|
| 5866 |
eleazar |
155 |
$viewModel->setVariables([
|
| 5911 |
eleazar |
156 |
'form' => $form,
|
|
|
157 |
'survies' => $survies
|
| 5866 |
eleazar |
158 |
]);
|
|
|
159 |
return $viewModel;
|
|
|
160 |
}
|
|
|
161 |
} else {
|
|
|
162 |
return new JsonModel([
|
|
|
163 |
'success' => false,
|
|
|
164 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
165 |
]);
|
|
|
166 |
|
|
|
167 |
}
|
| 5911 |
eleazar |
168 |
} catch (\Throwable $e) {
|
|
|
169 |
$e->getMessage();
|
|
|
170 |
return new JsonModel([
|
|
|
171 |
'success' => false,
|
|
|
172 |
'data' => $e
|
|
|
173 |
]);
|
|
|
174 |
}
|
| 5866 |
eleazar |
175 |
}
|
| 5911 |
eleazar |
176 |
|
| 5930 |
eleazar |
177 |
public function oneAction() {
|
|
|
178 |
$request = $this->getRequest();
|
|
|
179 |
$currentUserPlugin = $this->plugin('currentUserPlugin');
|
|
|
180 |
$currentCompany = $currentUserPlugin->getCompany();
|
|
|
181 |
$currentUser = $currentUserPlugin->getUser();
|
|
|
182 |
|
|
|
183 |
$request = $this->getRequest();
|
| 5961 |
eleazar |
184 |
$uuid = $this->params()->fromRoute('id');
|
|
|
185 |
|
| 5930 |
eleazar |
186 |
|
|
|
187 |
|
|
|
188 |
if (!$uuid) {
|
|
|
189 |
$data = [
|
|
|
190 |
'success' => false,
|
|
|
191 |
'data' => 'ERROR_INVALID_PARAMETER'
|
|
|
192 |
];
|
|
|
193 |
|
|
|
194 |
return new JsonModel($data);
|
|
|
195 |
}
|
|
|
196 |
|
| 5965 |
eleazar |
197 |
$surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
|
| 5930 |
eleazar |
198 |
$surveyTest = $surveyTestMapper->fetchOneByUuid($uuid);
|
| 5966 |
eleazar |
199 |
|
| 5930 |
eleazar |
200 |
if (!$surveyTest) {
|
|
|
201 |
$data = [
|
|
|
202 |
'success' => false,
|
| 5961 |
eleazar |
203 |
'data' => 'ERROR_RECORD_NOT_FOUND'
|
| 5930 |
eleazar |
204 |
];
|
|
|
205 |
|
|
|
206 |
return new JsonModel($data);
|
|
|
207 |
}
|
|
|
208 |
|
| 5965 |
eleazar |
209 |
$surveyMapper = SurveyMapper::getInstance($this->adapter);
|
|
|
210 |
$survey = $surveyMapper->fetchOne($surveyTest->survey_id);
|
|
|
211 |
|
| 5930 |
eleazar |
212 |
if ($request->isGet()) {
|
|
|
213 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
214 |
|
|
|
215 |
//get form data
|
|
|
216 |
$surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
|
| 5965 |
eleazar |
217 |
$surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
|
| 5930 |
eleazar |
218 |
|
|
|
219 |
if ($surveyForm) {
|
|
|
220 |
|
| 5969 |
eleazar |
221 |
return $this->renderPDF($surveyForm, $surveyTest, $survey);
|
| 5930 |
eleazar |
222 |
} else {
|
|
|
223 |
|
|
|
224 |
$data = [
|
|
|
225 |
'success' => false,
|
|
|
226 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
227 |
];
|
|
|
228 |
|
|
|
229 |
return new JsonModel($data);
|
|
|
230 |
}
|
|
|
231 |
} else {
|
|
|
232 |
$data = [
|
|
|
233 |
'success' => false,
|
|
|
234 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
235 |
];
|
|
|
236 |
|
|
|
237 |
return new JsonModel($data);
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
return new JsonModel($data);
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
|
| 5969 |
eleazar |
244 |
public function renderPDF($surveyForm, $surveyTest, $survey) {
|
| 5930 |
eleazar |
245 |
|
|
|
246 |
$target_path = $this->config['leaderslinked.fullpath.self_evaluation'] . DIRECTORY_SEPARATOR . $surveyTest->uuid;
|
|
|
247 |
|
|
|
248 |
|
|
|
249 |
if(file_exists($target_path)) {
|
|
|
250 |
Functions::deleteFiles($target_path);
|
|
|
251 |
} else {
|
|
|
252 |
@mkdir($target_path, 0755, true);
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
// Set Data
|
|
|
256 |
$headerFormName = utf8_decode($surveyForm->name);
|
| 5967 |
eleazar |
257 |
$headerUserName = utf8_decode('Informe de Auto-evaluación: ' . trim($surveyTest->first_name . ' ' . $surveyTest->last_name) . ' al ' . date("m-d-Y H:i:s", strtotime($survey->added_on)));
|
| 5930 |
eleazar |
258 |
$sections = json_decode($surveyTest->content, true);
|
|
|
259 |
$labels = ['Total', 'Logrado'];
|
|
|
260 |
|
|
|
261 |
|
|
|
262 |
//Generate New PDF
|
| 5971 |
eleazar |
263 |
$pdf = new SurveyReport();
|
| 5930 |
eleazar |
264 |
|
|
|
265 |
$pdf->AliasNbPages();
|
|
|
266 |
$pdf->AddPage();
|
|
|
267 |
|
|
|
268 |
// Set header secundary
|
|
|
269 |
$pdf->customHeader($headerFormName, $headerUserName);
|
|
|
270 |
|
|
|
271 |
$countSection = 0;
|
|
|
272 |
|
|
|
273 |
for ($i = 0; $i < count($sections); $i++) {
|
|
|
274 |
if ($countSection > 1) {
|
|
|
275 |
$countSection = 0;
|
|
|
276 |
$pdf->AddPage();
|
|
|
277 |
$pdf->customHeader($headerFormName, $headerUserName);
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
$pdf->SetY(70 + (92 * $countSection));
|
|
|
281 |
$totalQuestion = 0;
|
|
|
282 |
|
|
|
283 |
|
|
|
284 |
$filename = $target_path . DIRECTORY_SEPARATOR . $sections[$i]['slug_section'] . '.png';
|
| 5975 |
eleazar |
285 |
$pdf->pieChart($labels, '', $filename);
|
| 5930 |
eleazar |
286 |
$pdf->SetFont('Arial', '', 8);
|
|
|
287 |
$pdf->Cell(190, 10, utf8_decode($sections[$i]['name']), 0, 0, 'C');
|
|
|
288 |
$pdf->setY($pdf->getY() + 10);
|
|
|
289 |
// Salto de línea
|
|
|
290 |
$pdf->Image($filename, 60, $pdf->getY(), 90);
|
|
|
291 |
$pdf->setY($pdf->getY() + 60);
|
|
|
292 |
|
|
|
293 |
$countSection++;
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
$pdf->AddPage();
|
|
|
297 |
$pdf->customHeader($headerFormName, $headerUserName);
|
|
|
298 |
|
|
|
299 |
$pdf->SetFont('Arial', 'B', 10);
|
|
|
300 |
$pdf->Cell(190, 10, utf8_decode('Desempeño General'), 0, 0, 'C');
|
|
|
301 |
$pdf->setY($pdf->getY() + 10);
|
|
|
302 |
|
|
|
303 |
|
|
|
304 |
$filenameGeneral = $target_path . DIRECTORY_SEPARATOR . 'general.png';
|
| 5975 |
eleazar |
305 |
$pdf->pieChart($labels, '', $filenameGeneral);
|
| 5930 |
eleazar |
306 |
$pdf->Image($filenameGeneral, 60, $pdf->getY(), 90);
|
|
|
307 |
$pdf->setY($pdf->getY() + 60);
|
|
|
308 |
|
|
|
309 |
return $pdf->Output();
|
|
|
310 |
}
|
|
|
311 |
|
| 5866 |
eleazar |
312 |
}
|