Proyectos de Subversion LeadersLinked - Backend

Rev

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