Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 508 | Rev 510 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
244 geraldo 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;
270 geraldo 13
use LeadersLinked\Mapper\QueryMapper;
280 geraldo 14
use Laminas\Db\Sql\Select;
244 geraldo 15
use LeadersLinked\Library\Functions;
16
use LeadersLinked\Mapper\CompanySelfEvaluationFormMapper;
260 geraldo 17
use LeadersLinked\Mapper\CompanySelfEvaluationTestMapper;
349 geraldo 18
use LeadersLinked\Form\CompanySelfEvaluationTestForm;
244 geraldo 19
use LeadersLinked\Model\CompanySelfEvaluationForm;
287 geraldo 20
use LeadersLinked\Model\CompanySelfEvaluationTest;
284 geraldo 21
use LeadersLinked\Mapper\UserMapper;
244 geraldo 22
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
490 geraldo 23
use LeadersLinked\Library\Pdf;
244 geraldo 24
use LeadersLinked\Mapper\CompanySelfEvaluationFormUserMapper;
25
 
26
class SelfEvaluationReviewController extends AbstractActionController {
27
 
28
    /**
29
     *
30
     * @var AdapterInterface
31
     */
32
    private $adapter;
33
 
34
    /**
35
     *
36
     * @var AbstractAdapter
37
     */
38
    private $cache;
39
 
40
    /**
41
     *
42
     * @var  LoggerInterface
43
     */
44
    private $logger;
45
 
46
    /**
47
     *
48
     * @var array
49
     */
50
    private $config;
51
 
52
    /**
53
     *
54
     * @param AdapterInterface $adapter
55
     * @param AbstractAdapter $cache
56
     * @param LoggerInterface $logger
57
     * @param array $config
58
     */
59
    public function __construct($adapter, $cache, $logger, $config) {
60
        $this->adapter = $adapter;
61
        $this->cache = $cache;
62
        $this->logger = $logger;
63
        $this->config = $config;
64
    }
65
 
66
    public function indexAction() {
67
        $request = $this->getRequest();
68
        $currentUserPlugin = $this->plugin('currentUserPlugin');
69
        $currentCompany = $currentUserPlugin->getCompany();
70
        $currentUser = $currentUserPlugin->getUser();
71
 
72
 
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) {
94
                $search = $this->params()->fromQuery('search', []);
95
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
96
 
268 geraldo 97
                $queryMapper = QueryMapper::getInstance($this->adapter);
244 geraldo 98
 
268 geraldo 99
                $select = $queryMapper->getSql()->select();
447 geraldo 100
                $select->columns(['uuid', 'status', 'user_id','added_on']);
268 geraldo 101
                $select->from(['test' => CompanySelfEvaluationTestMapper::_TABLE]);
302 geraldo 102
                $select->join(['form' => CompanySelfEvaluationFormMapper::_TABLE], 'test.form_id = form.id', ['name', 'language']);
103
                $select->join(['user' => UserMapper::_TABLE], 'test.user_id = user.id', ['first_name', 'last_name'], Select::JOIN_LEFT_OUTER);
277 geraldo 104
 
273 geraldo 105
                $select->where->equalTo('form.status', CompanySelfEvaluationForm::STATUS_ACTIVE);
301 geraldo 106
                $select->where->notEqualTo('test.status', CompanySelfEvaluationTest::STATUS_DRAFT);
244 geraldo 107
 
302 geraldo 108
 
109
                //Search items
268 geraldo 110
                if ($search) {
297 geraldo 111
                    $select->where->NEST->like('name', '%' . $search . '%')
302 geraldo 112
                                    ->OR
113
                                    ->like('first_name', '%' . $search . '%')
114
                                    ->OR
115
                                    ->like('last_name', '%' . $search . '%')
116
                            ->UNNEST;
268 geraldo 117
                }
453 geraldo 118
                $select->order('added_on DESC');
244 geraldo 119
 
268 geraldo 120
                $records = $queryMapper->fetchAll($select);
121
                $items = [];
276 geraldo 122
 
286 geraldo 123
                foreach ($records as $record) {
124
                    switch ($record['language']) {
125
                        case CompanySelfEvaluationForm::LANGUAGE_ENGLISH :
126
                            $language = 'LABEL_ENGLISH';
127
                            break;
244 geraldo 128
 
286 geraldo 129
                        case CompanySelfEvaluationForm::LANGUAGE_SPANISH :
130
                            $language = 'LABEL_SPANISH';
131
                            break;
132
 
133
                        default :
134
                            $language = '';
135
                            break;
136
                    }
137
 
138
                    switch ($record['status']) {
139
 
140
                        case CompanySelfEvaluationTest::STATUS_COMPLETED :
141
                            $status = 'LABEL_COMPLETED';
142
                            break;
143
 
144
                        case CompanySelfEvaluationTest::STATUS_PENDING :
145
                            $status = 'LABEL_PENDING';
146
                            break;
147
 
148
                        case CompanySelfEvaluationTest::STATUS_REVIEW :
149
                            $status = 'LABEL_REVIEW';
150
                            break;
151
 
152
 
153
                        default :
154
                            $status = 'LABEL_AVAILABLE';
155
                            break;
156
                    }
157
 
158
                    $item = [
159
                        'name' => $record['name'],
302 geraldo 160
                        'user' => $record['first_name'] . ' ' . $record['last_name'],
286 geraldo 161
                        'language' => $language,
289 geraldo 162
                        'status' => $status,
447 geraldo 163
                        'added_on'=>$record['added_on'],
469 geraldo 164
                        'actions'=>[
165
                            'link_edit' => $record['status'] != CompanySelfEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('self-evaluation/reviews/edit', ['id' => $record['uuid']]) : '',
166
                            'link_report' => $record['status'] == CompanySelfEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('self-evaluation/reviews/report', ['id' => $record['uuid']]) : '',
167
                        ]
168
 
286 geraldo 169
                    ];
170
 
171
                    array_push($items, $item);
172
                }
173
 
244 geraldo 174
                return new JsonModel([
175
                    'success' => true,
176
                    'data' => [
286 geraldo 177
                        'items' => $items,
291 geraldo 178
                        'total' => count($items),
244 geraldo 179
                    ]
180
                ]);
181
            } else {
182
 
349 geraldo 183
                $form = new CompanySelfEvaluationTestForm();
244 geraldo 184
 
185
                $this->layout()->setTemplate('layout/layout-backend');
186
                $viewModel = new ViewModel();
250 geraldo 187
                $viewModel->setTemplate('leaders-linked/self-evaluation-reviews/index.phtml');
244 geraldo 188
                $viewModel->setVariable('form', $form);
189
                return $viewModel;
190
            }
191
        } else {
192
            return new JsonModel([
193
                'success' => false,
194
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
195
            ]);
196
            ;
197
        }
198
    }
302 geraldo 199
 
244 geraldo 200
    public function editAction() {
201
        $request = $this->getRequest();
202
        $currentUserPlugin = $this->plugin('currentUserPlugin');
203
        $currentCompany = $currentUserPlugin->getCompany();
204
        $currentUser = $currentUserPlugin->getUser();
205
 
206
        $request = $this->getRequest();
207
        $uuid = $this->params()->fromRoute('id');
208
 
209
 
210
        if (!$uuid) {
211
            $data = [
212
                'success' => false,
213
                'data' => 'ERROR_INVALID_PARAMETER'
214
            ];
215
 
216
            return new JsonModel($data);
217
        }
218
 
302 geraldo 219
        $companySelfEvaluationTestMapper = CompanySelfEvaluationTestMapper::getInstance($this->adapter);
446 geraldo 220
        $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneByUuid($uuid);
221
        if (!$companySelfEvaluationTest) {
244 geraldo 222
            $data = [
223
                'success' => false,
224
                'data' => 'ERROR_RECORD_NOT_FOUND'
225
            ];
226
 
227
            return new JsonModel($data);
228
        }
229
 
230
        if ($request->isPost()) {
349 geraldo 231
            $form = new CompanySelfEvaluationTestForm();
363 geraldo 232
            $dataPost = $request->getPost()->toArray();
233
            $form->setData($dataPost);
364 geraldo 234
 
244 geraldo 235
            if ($form->isValid()) {
366 geraldo 236
 
365 geraldo 237
                $selfEvaluationTest = new CompanySelfEvaluationTest();
466 geraldo 238
                $selfEvaluationTest->status = CompanySelfEvaluationTest::STATUS_COMPLETED;
365 geraldo 239
                $selfEvaluationTest->content = $dataPost['content'];
353 geraldo 240
 
446 geraldo 241
                $result = $companySelfEvaluationTestMapper->update($selfEvaluationTest, $companySelfEvaluationTest->id);
364 geraldo 242
 
243
                if ($result) {
244
                    $data = [
245
                        'success' => true,
246
                        'data' => 'LABEL_RECORD_UPDATED'
247
                    ];
248
                } else {
249
                    $data = [
250
                        'success' => false,
251
                        'data' => $companySelfEvaluationTestMapper->getError()
252
                    ];
253
                }
254
 
362 geraldo 255
                return new JsonModel($data);
244 geraldo 256
            } else {
257
                $messages = [];
258
                $form_messages = (array) $form->getMessages();
259
                foreach ($form_messages as $fieldname => $field_messages) {
260
                    $messages[$fieldname] = array_values($field_messages);
261
                }
262
 
263
                return new JsonModel([
264
                    'success' => false,
265
                    'data' => $messages
266
                ]);
267
            }
268
        } else if ($request->isGet()) {
269
            $hydrator = new ObjectPropertyHydrator();
270
 
315 geraldo 271
            //get form data
272
            $companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
446 geraldo 273
            $companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOne($companySelfEvaluationTest->form_id);
244 geraldo 274
 
315 geraldo 275
            //get user data
276
            $CompanyUserMapper = UserMapper::getInstance($this->adapter);
446 geraldo 277
            $userMapper = $CompanyUserMapper->fetchOne($companySelfEvaluationTest->user_id);
315 geraldo 278
 
446 geraldo 279
            if ($companySelfEvaluationForm && $userMapper) {
315 geraldo 280
 
281
                $data = [
282
                    'success' => true,
283
                    'data' => [
446 geraldo 284
                        'id' => $companySelfEvaluationTest->id,
285
                        'name' => $companySelfEvaluationForm->name,
286
                        'text' => $companySelfEvaluationForm->text,
315 geraldo 287
                        'user' => $userMapper->first_name . ' ' . $userMapper->last_name,
446 geraldo 288
                        'status' => $companySelfEvaluationTest->status,
289
                        'content' => json_decode($companySelfEvaluationTest->content),
315 geraldo 290
                    ]
291
                ];
292
 
293
                return new JsonModel($data);
294
            } else {
295
 
296
                $data = [
297
                    'success' => false,
298
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
299
                ];
300
 
301
                return new JsonModel($data);
302
            }
244 geraldo 303
        } else {
304
            $data = [
305
                'success' => false,
306
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
307
            ];
308
 
309
            return new JsonModel($data);
310
        }
311
 
312
        return new JsonModel($data);
313
    }
314
 
484 geraldo 315
 
487 geraldo 316
    public function reportAction() {
317
        $request = $this->getRequest();
318
        $currentUserPlugin = $this->plugin('currentUserPlugin');
319
        $currentCompany = $currentUserPlugin->getCompany();
320
        $currentUser = $currentUserPlugin->getUser();
484 geraldo 321
 
487 geraldo 322
        $request = $this->getRequest();
323
        $uuid = $this->params()->fromRoute('id');
324
 
325
 
326
        if (!$uuid) {
327
            $data = [
328
                'success' => false,
329
                'data' => 'ERROR_INVALID_PARAMETER'
330
            ];
331
 
332
            return new JsonModel($data);
333
        }
334
 
335
        $companySelfEvaluationTestMapper = CompanySelfEvaluationTestMapper::getInstance($this->adapter);
336
        $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneByUuid($uuid);
337
        if (!$companySelfEvaluationTest) {
338
            $data = [
339
                'success' => false,
340
                'data' => 'ERROR_RECORD_NOT_FOUND'
341
            ];
342
 
343
            return new JsonModel($data);
344
        }
345
 
346
        if ($request->isGet()) {
347
            $hydrator = new ObjectPropertyHydrator();
348
 
349
            //get form data
350
            $companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
351
            $companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOne($companySelfEvaluationTest->form_id);
352
 
353
            //get user data
354
            $CompanyUserMapper = UserMapper::getInstance($this->adapter);
355
            $userMapper = $CompanyUserMapper->fetchOne($companySelfEvaluationTest->user_id);
356
 
357
            if ($companySelfEvaluationForm && $userMapper) {
358
 
502 geraldo 359
 
490 geraldo 360
                $pdf = new Pdf();
502 geraldo 361
 
492 geraldo 362
 
363
                $headerFormName = utf8_decode($companySelfEvaluationForm->name);
494 geraldo 364
                $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)) );
492 geraldo 365
 
502 geraldo 366
                $pdf->AliasNbPages();
367
                $pdf->AddPage();
492 geraldo 368
                $pdf->pageHeader($headerFormName, $headerUserName);
502 geraldo 369
 
370
 
504 geraldo 371
                $sections = json_decode($companySelfEvaluationTest->content, true);
506 geraldo 372
 
502 geraldo 373
                $valueFormSections = 0;
374
                $valueFormQuestions = 0;
375
 
376
                $countSection = 0;
377
                for($i = 0, $maxSection = count($sections); $i < $maxSection; $i++)
378
                {
379
                    if( $countSection > 1)
380
                    {
381
                        $countSection = 0;
382
                        $pdf->AddPage();
383
                        $pdf->pageHeader($headerFormName, $headerUserName);
384
                    }
385
 
386
                    $pdf->SetY(70 + (92 * $countSection));
387
                    $totalQuestion = 0;
388
 
389
                    $valueSection       =  floatval($sections[$i]['value']);
390
                    $valueFormSections  = $valueFormSections  + $valueSection;
391
 
392
 
393
                    for($j = 0, $maxQuestion = count($sections[$i]['questions']); $j < $maxQuestion; $j++)
394
                    {
395
                        //$valueQuestion = floatval($sections[$i]['questions'][$j]['user_evaluation']);
396
                        $totalQuestion = $totalQuestion + 1;
397
                    }
398
 
399
                    $labels = [
400
                        'Total',
401
                        'Logrado'
402
                    ];
403
 
404
                    $values = [
405
                        $valueSection,
406
                        $totalQuestion
407
                    ];
408
 
409
                    $valueFormQuestions = $valueFormQuestions + $totalQuestion;
410
 
411
 
412
                    $title = '';
509 geraldo 413
                    $filename = $_SERVER['DOCUMENT_ROOT']. '/../public/pdf/tmp/' . $sections[$i]['slug'] . '.png';
414
                    $pdf->barChart($labels, $values, $title, $filename);
502 geraldo 415
                    $pdf->SetFont('Arial','',8);
416
                    $pdf->Cell(190,10, utf8_decode($sections[$i]['name']),0,0,'C');
417
                    $pdf->setY($pdf->getY() + 10);
418
                    // Salto de línea
419
 
509 geraldo 420
                    $pdf->Image($filename,60,$pdf->getY(),90);
502 geraldo 421
                    $pdf->setY($pdf->getY() + 60);
506 geraldo 422
 
502 geraldo 423
                    $countSection++;
424
 
425
                }
426
 
427
                $pdf->AddPage();
428
                $pdf->pageHeader($headerFormName, $headerUserName);
429
 
430
                $pdf->SetFont('Arial','B', 10);
431
                $pdf->Cell(190,10, utf8_decode('Desempeño General'),0,0,'C');
432
                $pdf->setY($pdf->getY() + 10);
433
 
434
 
435
                $labels = [
436
                    'Total',
437
                    'Logrado'
438
                ];
439
 
440
                $values = [
441
                    $valueFormSections,
442
                    $valueFormQuestions
443
                ];
444
 
509 geraldo 445
                $filenameGeneral = $_SERVER['DOCUMENT_ROOT']. '/../public/pdf/tmp/'  . uniqid() . '.png';
446
                $pdf->barChart($labels, $values, $title, $filenameGeneral);
447
                $pdf->Image($filenameGeneral,60,$pdf->getY(),90);
502 geraldo 448
                $pdf->setY($pdf->getY() + 60);
449
 
450
 
451
                $pdf->SetFont('Arial','B', 10);
452
                $pdf->Cell(190,10, utf8_decode('Comentarios finales'),0,0,'C');
453
                $pdf->setY($pdf->getY() + 10);
454
 
455
                $pdf->SetFont('Arial','', 8);
456
                $pdf->setY(60);
492 geraldo 457
 
489 geraldo 458
                return $pdf->Output();
488 geraldo 459
 
489 geraldo 460
 
487 geraldo 461
            } else {
462
 
463
                $data = [
464
                    'success' => false,
465
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
466
                ];
467
 
468
                return new JsonModel($data);
469
            }
470
        } else {
471
            $data = [
472
                'success' => false,
473
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
474
            ];
475
 
476
            return new JsonModel($data);
477
        }
478
 
479
        return new JsonModel($data);
480
    }
481
 
244 geraldo 482
}