Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16768 | | 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;
16768 efrain 8
 
244 geraldo 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;
15444 efrain 16
use LeadersLinked\Mapper\SelfEvaluationFormMapper;
17
use LeadersLinked\Mapper\SelfEvaluationTestMapper;
18
use LeadersLinked\Form\SelfEvaluation\SelfEvaluationTestForm;
19
use LeadersLinked\Model\SelfEvaluationForm;
20
use LeadersLinked\Model\SelfEvaluationTest;
284 geraldo 21
use LeadersLinked\Mapper\UserMapper;
244 geraldo 22
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
1336 efrain 23
use LeadersLinked\Library\SelfEvaluationPdf;
15079 efrain 24
use LeadersLinked\Model\Company;
244 geraldo 25
 
26
class SelfEvaluationReviewController extends AbstractActionController {
27
 
28
    /**
29
     *
16769 efrain 30
     * @var \Laminas\Db\Adapter\AdapterInterface
244 geraldo 31
     */
32
    private $adapter;
16768 efrain 33
 
244 geraldo 34
    /**
35
     *
16769 efrain 36
     * @var \LeadersLinked\Cache\CacheInterface
244 geraldo 37
     */
16769 efrain 38
    private $cache;
39
 
40
 
41
    /**
42
     *
43
     * @var \Laminas\Log\LoggerInterface
44
     */
244 geraldo 45
    private $logger;
16768 efrain 46
 
244 geraldo 47
    /**
48
     *
49
     * @var array
50
     */
51
    private $config;
16768 efrain 52
 
16769 efrain 53
 
244 geraldo 54
    /**
55
     *
16769 efrain 56
     * @var \Laminas\Mvc\I18n\Translator
57
     */
58
    private $translator;
59
 
60
 
61
    /**
62
     *
63
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
64
     * @param \LeadersLinked\Cache\CacheInterface $cache
65
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
244 geraldo 66
     * @param array $config
16769 efrain 67
     * @param \Laminas\Mvc\I18n\Translator $translator
244 geraldo 68
     */
16769 efrain 69
    public function __construct($adapter, $cache, $logger, $config, $translator)
16768 efrain 70
    {
16769 efrain 71
        $this->adapter      = $adapter;
72
        $this->cache        = $cache;
73
        $this->logger       = $logger;
74
        $this->config       = $config;
75
        $this->translator   = $translator;
244 geraldo 76
    }
77
 
78
    public function indexAction() {
79
        $request = $this->getRequest();
80
        $currentUserPlugin = $this->plugin('currentUserPlugin');
81
        $currentCompany = $currentUserPlugin->getCompany();
82
        $currentUser = $currentUserPlugin->getUser();
83
 
84
 
85
        $request = $this->getRequest();
86
        if ($request->isGet()) {
87
 
88
            $headers = $request->getHeaders();
89
 
90
            $isJson = false;
91
            if ($headers->has('Accept')) {
92
                $accept = $headers->get('Accept');
93
 
94
                $prioritized = $accept->getPrioritized();
95
 
96
                foreach ($prioritized as $key => $value) {
97
                    $raw = trim($value->getRaw());
98
 
99
                    if (!$isJson) {
100
                        $isJson = strpos($raw, 'json');
101
                    }
102
                }
103
            }
104
 
105
            if ($isJson) {
106
                $search = $this->params()->fromQuery('search', []);
16766 efrain 107
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
244 geraldo 108
 
268 geraldo 109
                $queryMapper = QueryMapper::getInstance($this->adapter);
244 geraldo 110
 
268 geraldo 111
                $select = $queryMapper->getSql()->select();
516 geraldo 112
                $select->columns(['uuid', 'status', 'user_id', 'added_on']);
15444 efrain 113
                $select->from(['test' => SelfEvaluationTestMapper::_TABLE]);
114
                $select->join(['form' => SelfEvaluationFormMapper::_TABLE], 'test.form_id = form.id', ['name', 'language']);
302 geraldo 115
                $select->join(['user' => UserMapper::_TABLE], 'test.user_id = user.id', ['first_name', 'last_name'], Select::JOIN_LEFT_OUTER);
277 geraldo 116
 
15444 efrain 117
                $select->where->equalTo('form.status', SelfEvaluationForm::STATUS_ACTIVE);
118
                $select->where->notEqualTo('test.status', SelfEvaluationTest::STATUS_DRAFT);
244 geraldo 119
 
302 geraldo 120
 
121
                //Search items
268 geraldo 122
                if ($search) {
297 geraldo 123
                    $select->where->NEST->like('name', '%' . $search . '%')
302 geraldo 124
                                    ->OR
125
                                    ->like('first_name', '%' . $search . '%')
126
                                    ->OR
127
                                    ->like('last_name', '%' . $search . '%')
128
                            ->UNNEST;
268 geraldo 129
                }
453 geraldo 130
                $select->order('added_on DESC');
244 geraldo 131
 
268 geraldo 132
                $records = $queryMapper->fetchAll($select);
133
                $items = [];
276 geraldo 134
 
286 geraldo 135
                foreach ($records as $record) {
136
                    switch ($record['language']) {
15444 efrain 137
                        case SelfEvaluationForm::LANGUAGE_ENGLISH :
286 geraldo 138
                            $language = 'LABEL_ENGLISH';
139
                            break;
244 geraldo 140
 
15444 efrain 141
                        case SelfEvaluationForm::LANGUAGE_SPANISH :
286 geraldo 142
                            $language = 'LABEL_SPANISH';
143
                            break;
144
 
145
                        default :
146
                            $language = '';
147
                            break;
148
                    }
149
 
150
                    switch ($record['status']) {
151
 
15444 efrain 152
                        case SelfEvaluationTest::STATUS_COMPLETED :
286 geraldo 153
                            $status = 'LABEL_COMPLETED';
154
                            break;
155
 
15444 efrain 156
                        case SelfEvaluationTest::STATUS_PENDING :
286 geraldo 157
                            $status = 'LABEL_PENDING';
158
                            break;
159
 
15444 efrain 160
                        case SelfEvaluationTest::STATUS_REVIEW :
286 geraldo 161
                            $status = 'LABEL_REVIEW';
162
                            break;
163
 
164
 
165
                        default :
166
                            $status = 'LABEL_AVAILABLE';
167
                            break;
168
                    }
169
 
170
                    $item = [
171
                        'name' => $record['name'],
302 geraldo 172
                        'user' => $record['first_name'] . ' ' . $record['last_name'],
286 geraldo 173
                        'language' => $language,
289 geraldo 174
                        'status' => $status,
516 geraldo 175
                        'added_on' => $record['added_on'],
176
                        'actions' => [
15444 efrain 177
                            'link_edit' => $record['status'] != SelfEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('self-evaluation/reviews/edit', ['id' => $record['uuid']]) : '',
178
                            'link_report' => $record['status'] == SelfEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('self-evaluation/reviews/report', ['id' => $record['uuid']]) : '',
469 geraldo 179
                        ]
286 geraldo 180
                    ];
181
 
182
                    array_push($items, $item);
183
                }
184
 
244 geraldo 185
                return new JsonModel([
186
                    'success' => true,
187
                    'data' => [
286 geraldo 188
                        'items' => $items,
291 geraldo 189
                        'total' => count($items),
244 geraldo 190
                    ]
191
                ]);
192
            } else {
193
 
15444 efrain 194
                $form = new SelfEvaluationTestForm();
244 geraldo 195
 
196
                $this->layout()->setTemplate('layout/layout-backend');
197
                $viewModel = new ViewModel();
250 geraldo 198
                $viewModel->setTemplate('leaders-linked/self-evaluation-reviews/index.phtml');
244 geraldo 199
                $viewModel->setVariable('form', $form);
200
                return $viewModel;
201
            }
202
        } else {
203
            return new JsonModel([
204
                'success' => false,
205
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
206
            ]);
207
            ;
208
        }
209
    }
302 geraldo 210
 
244 geraldo 211
    public function editAction() {
212
        $request = $this->getRequest();
213
        $currentUserPlugin = $this->plugin('currentUserPlugin');
214
        $currentCompany = $currentUserPlugin->getCompany();
215
        $currentUser = $currentUserPlugin->getUser();
216
 
217
        $request = $this->getRequest();
218
        $uuid = $this->params()->fromRoute('id');
219
 
220
 
221
        if (!$uuid) {
222
            $data = [
223
                'success' => false,
224
                'data' => 'ERROR_INVALID_PARAMETER'
225
            ];
226
 
227
            return new JsonModel($data);
228
        }
229
 
15444 efrain 230
        $selfEvaluationTestMapper = SelfEvaluationTestMapper::getInstance($this->adapter);
231
        $selfEvaluationTest = $selfEvaluationTestMapper->fetchOneByUuid($uuid);
232
        if (!$selfEvaluationTest) {
244 geraldo 233
            $data = [
234
                'success' => false,
235
                'data' => 'ERROR_RECORD_NOT_FOUND'
236
            ];
237
 
238
            return new JsonModel($data);
239
        }
240
 
241
        if ($request->isPost()) {
15444 efrain 242
            $form = new SelfEvaluationTestForm();
363 geraldo 243
            $dataPost = $request->getPost()->toArray();
244
            $form->setData($dataPost);
364 geraldo 245
 
244 geraldo 246
            if ($form->isValid()) {
366 geraldo 247
 
15444 efrain 248
                $selfEvaluationTest = new SelfEvaluationTest();
249
                $selfEvaluationTest->status = SelfEvaluationTest::STATUS_COMPLETED;
365 geraldo 250
                $selfEvaluationTest->content = $dataPost['content'];
855 geraldo 251
                $selfEvaluationTest->comments = $dataPost['comments'];
353 geraldo 252
 
15444 efrain 253
                $result = $selfEvaluationTestMapper->update($selfEvaluationTest, $selfEvaluationTest->id);
364 geraldo 254
 
255
                if ($result) {
256
                    $data = [
257
                        'success' => true,
258
                        'data' => 'LABEL_RECORD_UPDATED'
259
                    ];
260
                } else {
261
                    $data = [
262
                        'success' => false,
15444 efrain 263
                        'data' => $selfEvaluationTestMapper->getError()
364 geraldo 264
                    ];
265
                }
266
 
362 geraldo 267
                return new JsonModel($data);
244 geraldo 268
            } else {
269
                $messages = [];
270
                $form_messages = (array) $form->getMessages();
271
                foreach ($form_messages as $fieldname => $field_messages) {
272
                    $messages[$fieldname] = array_values($field_messages);
273
                }
274
 
275
                return new JsonModel([
276
                    'success' => false,
277
                    'data' => $messages
278
                ]);
279
            }
280
        } else if ($request->isGet()) {
281
            $hydrator = new ObjectPropertyHydrator();
282
 
315 geraldo 283
            //get form data
15444 efrain 284
            $selfEvaluationFormMapper = SelfEvaluationFormMapper::getInstance($this->adapter);
285
            $selfEvaluationForm = $selfEvaluationFormMapper->fetchOne($selfEvaluationTest->form_id);
244 geraldo 286
 
315 geraldo 287
            //get user data
288
            $CompanyUserMapper = UserMapper::getInstance($this->adapter);
15444 efrain 289
            $userMapper = $CompanyUserMapper->fetchOne($selfEvaluationTest->user_id);
315 geraldo 290
 
15444 efrain 291
            if ($selfEvaluationForm && $userMapper) {
315 geraldo 292
 
293
                $data = [
294
                    'success' => true,
295
                    'data' => [
15444 efrain 296
                        'id' => $selfEvaluationTest->id,
297
                        'name' => $selfEvaluationForm->name,
298
                        'text' => $selfEvaluationForm->text,
315 geraldo 299
                        'user' => $userMapper->first_name . ' ' . $userMapper->last_name,
15444 efrain 300
                        'status' => $selfEvaluationTest->status,
301
                        'comments' => $selfEvaluationTest->comments,
302
                        'content' => json_decode($selfEvaluationTest->content),
315 geraldo 303
                    ]
304
                ];
305
 
306
                return new JsonModel($data);
307
            } else {
308
 
309
                $data = [
310
                    'success' => false,
311
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
312
                ];
313
 
314
                return new JsonModel($data);
315
            }
244 geraldo 316
        } else {
317
            $data = [
318
                'success' => false,
319
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
320
            ];
321
 
322
            return new JsonModel($data);
323
        }
324
 
325
        return new JsonModel($data);
326
    }
327
 
487 geraldo 328
    public function reportAction() {
329
        $request = $this->getRequest();
330
        $currentUserPlugin = $this->plugin('currentUserPlugin');
331
        $currentCompany = $currentUserPlugin->getCompany();
332
        $currentUser = $currentUserPlugin->getUser();
484 geraldo 333
 
487 geraldo 334
        $request = $this->getRequest();
335
        $uuid = $this->params()->fromRoute('id');
336
 
337
 
338
        if (!$uuid) {
339
            $data = [
340
                'success' => false,
341
                'data' => 'ERROR_INVALID_PARAMETER'
342
            ];
343
 
344
            return new JsonModel($data);
345
        }
346
 
15444 efrain 347
        $selfEvaluationTestMapper = SelfEvaluationTestMapper::getInstance($this->adapter);
348
        $selfEvaluationTest = $selfEvaluationTestMapper->fetchOneByUuid($uuid);
349
        if (!$selfEvaluationTest) {
487 geraldo 350
            $data = [
351
                'success' => false,
352
                'data' => 'ERROR_RECORD_NOT_FOUND'
353
            ];
354
 
355
            return new JsonModel($data);
356
        }
357
 
358
        if ($request->isGet()) {
359
            $hydrator = new ObjectPropertyHydrator();
360
 
361
            //get form data
15444 efrain 362
            $selfEvaluationFormMapper = SelfEvaluationFormMapper::getInstance($this->adapter);
363
            $selfEvaluationForm = $selfEvaluationFormMapper->fetchOne($selfEvaluationTest->form_id);
487 geraldo 364
 
365
            //get user data
366
            $CompanyUserMapper = UserMapper::getInstance($this->adapter);
15444 efrain 367
            $userMapper = $CompanyUserMapper->fetchOne($selfEvaluationTest->user_id);
487 geraldo 368
 
15444 efrain 369
            if ($selfEvaluationForm && $userMapper) {
487 geraldo 370
 
15444 efrain 371
                return $this->renderPDF($currentCompany, $selfEvaluationForm, $selfEvaluationTest, $userMapper);
519 geraldo 372
            } else {
516 geraldo 373
 
519 geraldo 374
                $data = [
375
                    'success' => false,
376
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
377
                ];
516 geraldo 378
 
519 geraldo 379
                return new JsonModel($data);
380
            }
381
        } else {
382
            $data = [
383
                'success' => false,
384
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
385
            ];
516 geraldo 386
 
519 geraldo 387
            return new JsonModel($data);
388
        }
516 geraldo 389
 
519 geraldo 390
        return new JsonModel($data);
391
    }
516 geraldo 392
 
519 geraldo 393
    /**
394
     * Render Pdf document
15079 efrain 395
     * @param Company $company
15444 efrain 396
     * @param SelfEvaluationForm $selfEvaluationForm
397
     * @param SelfEvaluationTest $selfEvaluationTest
1336 efrain 398
     * @param UserMapper $userMapper
399
     * @return mixed
519 geraldo 400
     */
15444 efrain 401
    public function renderPDF($company, $selfEvaluationForm, $selfEvaluationTest, $userMapper) {
516 geraldo 402
 
15079 efrain 403
 
404
 
15444 efrain 405
        $target_path = $this->config['leaderslinked.fullpath.self_evaluation'] . DIRECTORY_SEPARATOR . $selfEvaluationTest->uuid;
1336 efrain 406
 
407
 
408
        if(file_exists($target_path)) {
409
            Functions::deleteFiles($target_path);
410
        } else {
411
            @mkdir($target_path, 0755, true);
412
        }
413
 
414
 
415
 
416
 
417
 
418
 
419
 
521 geraldo 420
        // Set Data
16768 efrain 421
        $headerFormName = Functions::utf8_decode($selfEvaluationForm->name);
422
        $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)));
15444 efrain 423
        $sections = json_decode($selfEvaluationTest->content, true);
557 geraldo 424
        $labels = ['Total', 'Logrado'];
516 geraldo 425
 
426
 
521 geraldo 427
        //Generate New PDF
1336 efrain 428
        $pdf = new SelfEvaluationPdf();
15079 efrain 429
 
430
 
431
        $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $company->uuid;
432
 
433
        $header = $company->header ? $target_path . DIRECTORY_SEPARATOR . $company->header : '';
434
        if(empty($header) || !file_exists($header)) {
435
            $header = $this->config['leaderslinked.images_default.company_pdf_header'];
436
        }
437
 
438
        $footer = $company->footer ? $target_path . DIRECTORY_SEPARATOR . $company->footer : '';
439
        if(empty($footer) || !file_exists($footer)) {
440
            $footer = $this->config['leaderslinked.images_default.company_pdf_footer'];
441
        }
442
 
443
 
444
        $pdf->header = $header;
445
        $pdf->footer = $footer;
446
 
516 geraldo 447
 
519 geraldo 448
        $pdf->AliasNbPages();
449
        $pdf->AddPage();
939 geraldo 450
 
521 geraldo 451
        // Set header secundary
567 geraldo 452
        $pdf->customHeader($headerFormName, $headerUserName);
516 geraldo 453
 
519 geraldo 454
        $valueFormSections = 0;
455
        $valueFormQuestions = 0;
516 geraldo 456
 
519 geraldo 457
        $countSection = 0;
516 geraldo 458
 
525 geraldo 459
        for ($i = 0; $i < count($sections); $i++) {
519 geraldo 460
            if ($countSection > 1) {
461
                $countSection = 0;
516 geraldo 462
                $pdf->AddPage();
567 geraldo 463
                $pdf->customHeader($headerFormName, $headerUserName);
519 geraldo 464
            }
516 geraldo 465
 
519 geraldo 466
            $pdf->SetY(70 + (92 * $countSection));
467
            $totalQuestion = 0;
516 geraldo 468
 
519 geraldo 469
            $valueSection = floatval($sections[$i]['value']);
470
            $valueFormSections = $valueFormSections + $valueSection;
516 geraldo 471
 
519 geraldo 472
            for ($j = 0; $j < count($sections[$i]['questions']); $j++) {
473
                $totalQuestion = $totalQuestion + $this->getPtosAnswer($sections[$i]['questions'][$j]);
474
            }
516 geraldo 475
 
519 geraldo 476
            $values = [$valueSection, $totalQuestion];
477
            $valueFormQuestions = $valueFormQuestions + $totalQuestion;
516 geraldo 478
 
1336 efrain 479
 
516 geraldo 480
 
1336 efrain 481
            $filename = $target_path . DIRECTORY_SEPARATOR .  $sections[$i]['slug_section'] . '.png';
939 geraldo 482
            $pdf->pieChart($labels, $values, '', $filename);
519 geraldo 483
            $pdf->SetFont('Arial', '', 8);
16768 efrain 484
            $pdf->Cell(190, 10, Functions::utf8_decode($sections[$i]['name']), 0, 0, 'C');
519 geraldo 485
            $pdf->setY($pdf->getY() + 10);
486
            // Salto de línea
538 geraldo 487
            $pdf->Image($filename, 60, $pdf->getY(), 90);
6170 eleazar 488
            $pdf->setY($pdf->getY() + 60);
516 geraldo 489
 
519 geraldo 490
            $countSection++;
491
        }
487 geraldo 492
 
519 geraldo 493
        $pdf->AddPage();
567 geraldo 494
        $pdf->customHeader($headerFormName, $headerUserName);
487 geraldo 495
 
519 geraldo 496
        $pdf->SetFont('Arial', 'B', 10);
16768 efrain 497
        $pdf->Cell(190, 10, Functions::utf8_decode('Desempeño General'), 0, 0, 'C');
519 geraldo 498
        $pdf->setY($pdf->getY() + 10);
487 geraldo 499
 
519 geraldo 500
        $values = [$valueFormSections, $valueFormQuestions];
501
 
1336 efrain 502
        $filenameGeneral = $target_path . DIRECTORY_SEPARATOR . 'general.png';
567 geraldo 503
        $pdf->pieChart($labels, $values, '', $filenameGeneral);
538 geraldo 504
        $pdf->Image($filenameGeneral, 60, $pdf->getY(), 90);
519 geraldo 505
        $pdf->setY($pdf->getY() + 60);
506
 
15444 efrain 507
        if ($selfEvaluationTest->comments) {
519 geraldo 508
 
939 geraldo 509
            $pdf->SetFont('Arial', 'B', 10);
16768 efrain 510
            $pdf->Cell(190, 10, Functions::utf8_decode('Comentarios finales'), 0, 0, 'C');
855 geraldo 511
            $pdf->setY($pdf->getY() + 10);
939 geraldo 512
 
513
            $pdf->SetFont('Arial', '', 8);
16768 efrain 514
            $pdf->MultiCell(180, 8, Functions::utf8_decode($selfEvaluationTest->comments));
855 geraldo 515
            $pdf->setY(60);
516
        }
517
 
519 geraldo 518
        return $pdf->Output();
487 geraldo 519
    }
520
 
516 geraldo 521
    /**
522
     * get total ptos Answer
1336 efrain 523
     * @param array $question
524
     * @return int
516 geraldo 525
     */
526
    public function getPtosAnswer($question) {
513 geraldo 527
 
516 geraldo 528
        $ptos = 0;
529
 
530
        if ($question['type'] == "open" || $question['type'] == "rating-open" || $question['type'] == "rating-range") {
531
 
532
            $ptos = isset($question['question_value']) ? $question['question_value'] : 0;
533
        } else {
534
            if ($question['type'] == 'multiple') {
535
 
536
                for ($o = 0; $o < count($question['options']); $o++) {
537
                    if (in_array($question['options'][$o]['slug_option'], $question['answer'])) {
538
                        $ptos = $ptos + $question['options'][$o]['value'];
539
                    }
540
                }
541
            } else {
542
 
543
                for ($o = 0; $o < count($question['options']); $o++) {
544
                    if ($question['options'][$o]['slug_option'] == $question['answer']) {
545
                        $ptos = $question['options'][$o]['value'];
546
                    }
547
                }
548
            }
549
        }
550
        return $ptos;
513 geraldo 551
    }
552
 
244 geraldo 553
}