Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16817 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
15444 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
16768 efrain 7
 
15444 efrain 8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\JsonModel;
11
use Laminas\View\Model\ViewModel;
12
use LeadersLinked\Library\Functions;
13
use LeadersLinked\Mapper\PerformanceEvaluationTestMapper;
14
use LeadersLinked\Mapper\PerformanceEvaluationFormMapper;
15
use LeadersLinked\Mapper\QueryMapper;
16
use LeadersLinked\Mapper\UserMapper;
17
use LeadersLinked\Model\PerformanceEvaluationTest;
18
use Laminas\Hydrator\ArraySerializableHydrator;
19
use Laminas\Db\ResultSet\HydratingResultSet;
20
use Laminas\Paginator\Adapter\DbSelect;
21
use Laminas\Paginator\Paginator;
22
use LeadersLinked\Form\PerformanceEvaluation\PerformanceEvaluationTakeAnTestForm;
23
use Laminas\Mvc\I18n\Translator;
24
use LeadersLinked\Library\PerformanceEvaluationInterviewPDF;
16768 efrain 25
use LeadersLinked\Cache\CacheInterface;
26
use LeadersLinked\Cache\CacheImpl;
15444 efrain 27
 
28
class ActivityCenterPerformanceEvaluationController extends AbstractActionController
29
{
30
    /**
31
     *
16769 efrain 32
     * @var \Laminas\Db\Adapter\AdapterInterface
15444 efrain 33
     */
34
    private $adapter;
35
 
36
    /**
37
     *
16769 efrain 38
     * @var \LeadersLinked\Cache\CacheInterface
15444 efrain 39
     */
16769 efrain 40
    private $cache;
41
 
42
 
43
    /**
44
     *
45
     * @var \Laminas\Log\LoggerInterface
46
     */
15444 efrain 47
    private $logger;
48
 
49
    /**
50
     *
51
     * @var array
52
     */
53
    private $config;
54
 
55
 
56
    /**
16769 efrain 57
     *
58
     * @var \Laminas\Mvc\I18n\Translator
15444 efrain 59
     */
60
    private $translator;
61
 
62
 
16768 efrain 63
    /**
15444 efrain 64
     *
16769 efrain 65
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
66
     * @param \LeadersLinked\Cache\CacheInterface $cache
67
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
15444 efrain 68
     * @param array $config
16769 efrain 69
     * @param \Laminas\Mvc\I18n\Translator $translator
15444 efrain 70
     */
16769 efrain 71
    public function __construct($adapter, $cache, $logger, $config, $translator)
15444 efrain 72
    {
73
        $this->adapter      = $adapter;
16769 efrain 74
        $this->cache        = $cache;
15444 efrain 75
        $this->logger       = $logger;
76
        $this->config       = $config;
77
        $this->translator   = $translator;
78
    }
79
 
80
    public function indexAction()
81
    {
82
        $currentUserPlugin = $this->plugin('currentUserPlugin');
83
        $currentUser = $currentUserPlugin->getUser();
84
        $currentCompany = $currentUserPlugin->getCompany();
85
 
86
 
87
 
88
 
89
 
90
        $request = $this->getRequest();
91
 
92
        if ($request->isGet()) {
93
 
94
            $headers = $request->getHeaders();
95
 
96
            $isJson = false;
97
            if ($headers->has('Accept')) {
98
                $accept = $headers->get('Accept');
99
 
100
                $prioritized = $accept->getPrioritized();
101
 
102
                foreach ($prioritized as $key => $value) {
103
                    $raw = trim($value->getRaw());
104
 
105
                    if (!$isJson) {
106
                        $isJson = strpos($raw, 'json');
107
                    }
108
                }
109
            }
110
 
111
 
112
            if ($isJson) {
113
 
114
 
115
                $data = [
116
                    'items' => [],
117
                    'total' => 0,
118
                ];
119
 
120
 
121
                $search = $this->params()->fromQuery('search');
16766 efrain 122
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
15444 efrain 123
 
124
                $start = intval($this->params()->fromQuery('start', 0), 10);
125
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
126
                $page =  intval($start / $records_x_page);
127
                $page++;
128
 
129
                $order = $this->params()->fromQuery('order', []);
130
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
16766 efrain 131
                $order_direction = empty($order[0]['dir']) ? 'ASC' : Functions::sanitizeFilterString(filter_var($order[0]['dir']));
15444 efrain 132
 
133
                $fields = ['max_date'];
134
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
135
 
136
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
137
                    $order_direction = 'ASC';
138
                }
139
 
140
 
141
 
142
 
143
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
144
                //$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'performance-evaluation/evaluations/delete');
145
 
146
 
147
 
148
                $queryMapper = QueryMapper::getInstance($this->adapter);
149
                $sql = $queryMapper->getSql();
150
                $select = $sql->select();
151
                $select->columns([
152
                    'id',
153
                    'uuid',
154
                    'last_date',
155
                    'status',
156
                    'type',
157
                    'added_on',
158
                    'updated_on'
159
 
160
                ]);
161
 
162
 
163
 
164
                $select->from(['tb1' => PerformanceEvaluationTestMapper::_TABLE]);
165
                $select->join(['tb2' => PerformanceEvaluationFormMapper::_TABLE], 'tb1.form_id = tb2.id ', ['form' => 'name']);
166
                $select->join(['tb3' => UserMapper::_TABLE], 'tb1.supervisor_id = tb3.id ', ['supervisor_first_name' => 'first_name', 'supervisor_last_name' => 'last_name', 'supervisor_email' => 'email']);
167
                $select->join(['tb4' => UserMapper::_TABLE], 'tb1.employee_id = tb4.id ', ['employee_first_name' => 'first_name', 'employee_last_name' => 'last_name', 'employee_email' => 'email']);
168
                $select->where->equalTo('tb1.company_id', $currentCompany->id);
169
                $select->where->nest()
170
                    ->nest()
171
                        ->equalTo('tb1.supervisor_id', $currentUser->id)
172
                        ->and->equalTo('tb1.type', PerformanceEvaluationTest::TYPE_SUPERVISOR)
173
                    ->unnest()
174
                    ->or->nest()
175
                        ->equalTo('tb1.supervisor_id', $currentUser->id)
176
                        ->and->equalTo('tb1.type', PerformanceEvaluationTest::TYPE_BOTH)
177
                    ->unnest()
178
                    ->or->nest()
179
                    ->equalTo('tb1.employee_id', $currentUser->id)
180
                    ->and->equalTo('tb1.type', PerformanceEvaluationTest::TYPE_EMPLOYEE)
181
                    ->unnest()
182
                ->unnest();
183
 
184
                if ($search) {
185
                    $select->where->nest()
186
                        ->like('tb2.name', '%' . $search . '%')
187
                        ->or->like('tb1.uuid', '%' . $search . '%')
188
                        ->unnest();
189
                }
190
 
191
 
192
                $select->order('tb1.last_date DESC, tb2.name ASC');
193
 
194
 
195
 
196
                $hydrator = new ArraySerializableHydrator();
197
                $resultset = new HydratingResultSet($hydrator);
198
 
199
                $adapter = new DbSelect($select, $sql, $resultset);
200
                $paginator = new Paginator($adapter);
201
                $paginator->setItemCountPerPage($records_x_page);
202
                $paginator->setCurrentPageNumber($page);
203
 
204
 
205
 
206
                $items = [];
207
                $records = $paginator->getCurrentItems();
208
                foreach ($records as $record)
209
                {
210
 
211
                    $dt = \DateTime::createFromFormat('Y-m-d', $record['last_date']);
212
                    $last_date = $dt->format('d/m/Y');
213
 
214
                    switch($record['status'])
215
                    {
216
                        case PerformanceEvaluationTest::STATUS_PENDING :
217
                            $status = 'LABEL_PENDING';
218
                            break;
219
 
220
                        case PerformanceEvaluationTest::STATUS_COMPLETED :
221
                            $status = 'LABEL_COMPLETED';
222
                            break;
223
 
224
                        default :
225
                            $status = 'LABEL_UNKNOW';
226
                            break;
227
                    }
228
 
229
                    switch($record['type'])
230
                    {
231
                        case PerformanceEvaluationTest::TYPE_BOTH :
232
                            $type = 'LABEL_PERFORMANCE_EVALUATION_TYPE_BOTH';
233
                            break;
234
 
235
                        case PerformanceEvaluationTest::TYPE_SUPERVISOR :
236
                            $type = 'LABEL_PERFORMANCE_EVALUATION_TYPE_SUPERVISOR';
237
                            break;
238
 
239
                        case PerformanceEvaluationTest::TYPE_EMPLOYEE :
240
                            $type = 'LABEL_PERFORMANCE_EVALUATION_TYPE_EMPLOYEE';
241
                            break;
242
 
243
                        default :
244
                            $type = 'LABEL_UNKNOWN';
245
                            break;
246
                    }
247
 
248
 
249
 
250
                    if($record['status'] == PerformanceEvaluationTest::STATUS_COMPLETED) {
251
                        $link_pdf = $this->url()->fromRoute('activities-center/performance-evaluation/report', ['id' => $record['uuid']]);
252
                        $link_take_a_test = '';
253
 
254
 
255
                    } else {
256
                        $link_pdf = '';
257
                        $link_take_a_test = $this->url()->fromRoute('activities-center/performance-evaluation/take-a-test', ['id' => $record['uuid']]);
258
                    }
259
 
260
 
261
                    $item = [
262
                        'last_date' => $last_date,
263
                        'form' => $record['form'],
264
                        'type' => $type,
265
                        'supervisor_first_name' => $record['supervisor_first_name'],
266
                        'supervisor_last_name' => $record['supervisor_last_name'],
267
                        'supervisor_email' => $record['supervisor_email'],
268
                        'employee_first_name' => $record['employee_first_name'],
269
                        'employee_last_name' => $record['employee_last_name'],
270
                        'employee_email' => $record['employee_email'],
271
                        'status' => $status,
272
                        'actions' => [
273
                            'link_pdf' => $link_pdf,
274
                            'link_take_a_test' => $link_take_a_test,
275
                        ]
276
                    ];
277
                    /*
278
                     if($testSelf) {
279
                     $item['actions']['link_report_self'] = $this->url()->fromRoute('performance-evaluation/evaluations/report-self', ['id' => $record['uuid'] ]);
280
                     } else{
281
                     $item['actions']['link_self'] = $this->url()->fromRoute('performance-evaluation/evaluations/self', ['id' => $record['uuid'] ]);
282
                     }
283
                     if($testBoth) {
284
                     $item['actions']['link_report_both'] = $this->url()->fromRoute('performance-evaluation/evaluations/report-both', ['id' => $record['uuid'] ]);
285
                     } else{
286
                     $item['actions']['link_both'] = $this->url()->fromRoute('performance-evaluation/evaluations/both', ['id' => $record['uuid'] ]);
287
                     }
288
                     if($testSupervisor) {
289
                     $item['actions']['link_report_superviser'] = $this->url()->fromRoute('performance-evaluation/evaluations/report-superviser', ['id' => $record['uuid'] ]);
290
                     } else{
291
                     $item['actions']['link_superviser'] = $this->url()->fromRoute('performance-evaluation/evaluations/superviser', ['id' => $record['uuid'] ]);
292
                     }*/
293
 
294
                    array_push($items, $item);
295
                }
296
 
297
                $data['items'] = $items;
298
                $data['total'] = $paginator->getTotalItemCount();
299
 
300
 
301
                return new JsonModel([
302
                    'success' => true,
303
                    'data' => $data
304
                ]);
305
            } else {
16768 efrain 306
 
15444 efrain 307
                if($this->cache->hasItem('ACTIVITY_CENTER_RELATIONAL')) {
308
                    $search =  $this->cache->getItem('ACTIVITY_CENTER_RELATIONAL');
309
                    $this->cache->removeItem('ACTIVITY_CENTER_RELATIONAL');
310
                } else {
311
                    $search = '';
312
                }
313
 
314
 
315
 
316
 
317
                $form = new PerformanceEvaluationTakeAnTestForm();
318
 
319
                $this->layout()->setTemplate('layout/layout-backend');
320
                $viewModel = new ViewModel();
321
                $viewModel->setTemplate('leaders-linked/activity-center-performance-evaluation/index.phtml');
322
                $viewModel->setVariables([
323
                    'search' => $search,
324
                    'form' => $form
325
                ]);
326
                return $viewModel;
327
            }
328
        } else {
329
            return new JsonModel([
330
                'success' => false,
331
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
332
            ]);
333
 
334
        }
335
    }
336
 
337
 
338
    public function takeaTestAction()
339
    {
340
        $request = $this->getRequest();
341
        $currentUserPlugin = $this->plugin('currentUserPlugin');
342
        $currentCompany = $currentUserPlugin->getCompany();
343
        $currentUser = $currentUserPlugin->getUser();
344
 
345
        $request = $this->getRequest();
346
        $uuid = $this->params()->fromRoute('id');
347
 
348
 
349
        if (!$uuid) {
350
            $data = [
351
                'success' => false,
352
                'data' => 'ERROR_INVALID_PARAMETER'
353
            ];
354
 
355
            return new JsonModel($data);
356
        }
357
 
358
        $performanceEvaluationTestMapper = PerformanceEvaluationTestMapper::getInstance($this->adapter);
359
        $performanceEvaluationTest = $performanceEvaluationTestMapper->fetchOneByUuid($uuid);
360
 
361
 
362
 
363
 
364
 
365
        if (!$performanceEvaluationTest) {
366
            $data = [
367
                'success' => false,
368
                'data' => 'ERROR_RECORD_NOT_FOUND'
369
            ];
370
 
371
            return new JsonModel($data);
372
        }
373
 
374
        if ($performanceEvaluationTest->company_id == $currentCompany->id) {
375
            switch ($performanceEvaluationTest->type)
376
            {
377
                case PerformanceEvaluationTest::TYPE_BOTH :
378
                    $ok = $performanceEvaluationTest->supervisor_id == $currentUser->id
379
                    || $performanceEvaluationTest->employee_id == $currentUser->id;
380
                    break;
381
 
382
                case PerformanceEvaluationTest::TYPE_SUPERVISOR :
383
                    $ok = $performanceEvaluationTest->supervisor_id == $currentUser->id;
384
                    break;
385
 
386
                case PerformanceEvaluationTest::TYPE_EMPLOYEE :
387
                    $ok =  $performanceEvaluationTest->employee_id == $currentUser->id;
388
                    break;
389
 
390
                default :
391
                    $ok = false;
392
                    break;
393
 
394
            }
395
        } else {
396
            $ok = false;
397
        }
398
 
399
        if(!$ok) {
400
            return new JsonModel([
401
                'success' => false,
402
                'data' => 'ERROR_UNAUTHORIZED'
403
            ]);
404
        }
405
 
406
 
407
 
408
        if ($request->isPost()) {
409
 
16930 efrain 410
 
15444 efrain 411
 
412
            $dataPost = $request->getPost()->toArray();
413
 
414
            $form = new PerformanceEvaluationTakeAnTestForm($this->adapter, $currentCompany->id);
415
            $form->setData($dataPost);
416
 
417
            if ($form->isValid()) {
16930 efrain 418
 
15444 efrain 419
                switch( $performanceEvaluationTest->type)
420
                {
421
                    case PerformanceEvaluationTest::TYPE_BOTH :
422
                        $type = 'LABEL_PERFORMANCE_EVALUATION_TYPE_BOTH';
423
                        break;
424
 
425
                    case PerformanceEvaluationTest::TYPE_SUPERVISOR :
426
                        $type = 'LABEL_PERFORMANCE_EVALUATION_TYPE_SUPERVISOR';
427
                        break;
428
 
429
                    case PerformanceEvaluationTest::TYPE_EMPLOYEE :
430
                        $type = 'LABEL_PERFORMANCE_EVALUATION_TYPE_EMPLOYEE';
431
                        break;
432
 
433
                    default :
434
                        $type = 'LABEL_UNKNOWN';
435
                        break;
436
                }
437
 
438
 
439
                $type = $this->translator->translate($type);
16930 efrain 440
 
15444 efrain 441
 
442
                $performanceEvaluationFormMapper = PerformanceEvaluationFormMapper::getInstance($this->adapter);
443
                $performanceEvaluationForm = $performanceEvaluationFormMapper->fetchOne($performanceEvaluationTest->form_id);
444
 
445
                $dataPost = (array) $form->getData();
446
 
447
                $content = json_decode($performanceEvaluationTest->content);
16930 efrain 448
 
449
 
450
 
15444 efrain 451
                $content->points = $dataPost['points'];
452
                $content->comment = $dataPost['comment'];
453
                $content->evaluation = [];
454
 
455
                foreach($content->competencies_selected as $competency)
456
                {
16930 efrain 457
 
458
 
459
 
15444 efrain 460
                    foreach($competency->behaviors as $behavior)
461
                    {
16930 efrain 462
 
15444 efrain 463
                        $key_comment = $competency->uuid . '-' . $behavior->uuid . '-comment';
464
                        $key_points = $competency->uuid . '-' . $behavior->uuid . '-points';
16930 efrain 465
 
466
                        $value_comment = $this->params()->fromPost($key_comment);
467
                        $value_comment = Functions::sanitizeFilterString($value_comment);
468
                        $value_comment = empty($value_comment) ? '' : $value_comment;
15444 efrain 469
 
470
 
16930 efrain 471
 
16817 efrain 472
                        $value_points = intval($this->params()->fromPost($key_points), 10);
15444 efrain 473
 
474
                        array_push($content->evaluation, [
475
                            'competency' => $competency->uuid,
476
                            'behavior' => $behavior->uuid,
477
                            'comment' => $value_comment,
478
                            'points' => $value_points
479
                        ]);
480
 
481
 
482
 
483
 
484
                    }
485
                }
486
 
16930 efrain 487
 
15444 efrain 488
                $performanceEvaluationTest->status = PerformanceEvaluationTest::STATUS_COMPLETED;
489
                $performanceEvaluationTest->content = json_encode($content);
490
 
491
                $result = $performanceEvaluationTestMapper->update($performanceEvaluationTest);
492
 
493
                if ($result) {
494
                    $this->logger->info('Se agrego la prueba de tipo '  . $type . ' de la Evaluación de  Desempeño : ' . $performanceEvaluationTest->uuid, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
495
                    $data = [
496
                        'success' => true,
497
                        'data' => 'LABEL_RECORD_UPDATED'
498
                    ];
499
                } else {
500
                    $data = [
501
                        'success' => false,
502
                        'data' => $performanceEvaluationTestMapper->getError()
503
                    ];
504
                }
505
 
506
                return new JsonModel($data);
507
            } else {
508
                $messages = [];
509
                $form_messages = (array) $form->getMessages();
510
                foreach ($form_messages as $fieldname => $field_messages) {
511
                    $messages[$fieldname] = array_values($field_messages);
512
                }
513
 
514
                return new JsonModel([
515
                    'success' => false,
516
                    'data' => $messages
517
                ]);
518
            }
519
        } else if ($request->isGet()) {
520
            switch( $performanceEvaluationTest->type)
521
            {
522
                case PerformanceEvaluationTest::TYPE_BOTH :
523
                    $type = 'LABEL_PERFORMANCE_EVALUATION_TYPE_BOTH';
524
                    break;
525
 
526
                case PerformanceEvaluationTest::TYPE_SUPERVISOR :
527
                    $type = 'LABEL_PERFORMANCE_EVALUATION_TYPE_SUPERVISOR';
528
                    break;
529
 
530
                case PerformanceEvaluationTest::TYPE_EMPLOYEE :
531
                    $type = 'LABEL_PERFORMANCE_EVALUATION_TYPE_EMPLOYEE';
532
                    break;
533
 
534
                default :
535
                    $type = 'LABEL_UNKNOWN';
536
                    break;
537
            }
538
 
539
 
540
            $performanceEvaluationFormMapper = PerformanceEvaluationFormMapper::getInstance($this->adapter);
541
            $performanceEvaluationForm = $performanceEvaluationFormMapper->fetchOne($performanceEvaluationTest->form_id);
542
 
543
 
544
            $content = json_decode($performanceEvaluationTest->content);
545
            foreach($content->competencies_selected as &$competency)
546
            {
547
                foreach($competency->behaviors as &$behavior)
548
                {
549
                    $behavior->competency_uuid = $competency->uuid;
550
                }
551
            }
552
 
553
            $userMapper = UserMapper::getInstance($this->adapter);
554
 
555
            if($performanceEvaluationTest->supervisor_id) {
556
                $user = $userMapper->fetchOne($performanceEvaluationTest->supervisor_id);
557
                $supervisor = $user->first_name . ' ' . $user->last_name;
558
            } else {
559
                $supervisor = '';
560
            }
561
 
562
            if($performanceEvaluationTest->employee_id) {
563
                $user = $userMapper->fetchOne($performanceEvaluationTest->employee_id);
564
                $employee = $user->first_name . ' ' . $user->last_name;
565
            } else {
566
                $employee = '';
567
            }
568
 
569
            $dt = \DateTime::createFromFormat('Y-m-d', $performanceEvaluationTest->last_date);
570
            if($dt) {
571
                $last_date = $dt->format('d/m/Y');
572
            } else {
573
                $last_date = '';
574
            }
575
 
576
 
577
            $data = [
578
               // 'uuid' =>  $content->uuid,
579
                'name' =>  $content->name,
580
                'functions' =>  $content->functions,
581
                'objectives' =>  $content->objectives,
582
                'form' => $performanceEvaluationForm->name,
583
                'type' => $type,
584
                'supervisor' => $supervisor,
585
                'employee' => $employee,
586
                'last_date' => $last_date,
587
                //'job_description_id_boss' =>  $content->job_description_id_boss,
588
                'competency_types' => [],
589
                'competencies' => [],
590
                'behaviors' => [],
591
                'competencies_selected' => [],
592
                'subordinates_selected' => [],
593
            ];
594
 
595
            foreach($content->competency_types as $record)
596
            {
597
               array_push($data['competency_types'], $record);
598
            }
599
 
600
            foreach($content->competencies as $record)
601
            {
602
                array_push($data['competencies'], $record);
603
            }
604
 
605
            foreach($content->behaviors as $record)
606
            {
607
                array_push($data['behaviors'], $record);
608
            }
609
 
610
            foreach($content->competencies_selected as $record)
611
            {
612
                array_push($data['competencies_selected'], $record);
613
            }
614
 
615
            foreach($content->subordinates_selected as $record)
616
            {
617
                array_push($data['subordinates_selected'], $record);
618
            }
619
 
620
 
621
 
622
            $data = [
623
                'success' => true,
624
                'data' => $data
625
            ];
626
 
627
 
628
            return new JsonModel($data);
629
        } else {
630
            $data = [
631
                'success' => false,
632
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
633
            ];
634
 
635
            return new JsonModel($data);
636
        }
637
 
638
        return new JsonModel($data);
639
    }
640
 
641
    public function reportAction()
642
    {
643
        $request = $this->getRequest();
644
        $currentUserPlugin = $this->plugin('currentUserPlugin');
645
        $currentCompany = $currentUserPlugin->getCompany();
646
        $currentUser = $currentUserPlugin->getUser();
647
 
648
        $request = $this->getRequest();
649
        $uuid = $this->params()->fromRoute('id');
650
 
651
        if (!$uuid) {
652
            $data = [
653
                'success' => false,
654
                'data' => 'ERROR_INVALID_PARAMETER'
655
            ];
656
 
657
            return new JsonModel($data);
658
        }
659
 
660
        $performanceEvaluationTestMapper = PerformanceEvaluationTestMapper::getInstance($this->adapter);
661
        $performanceEvaluationTest = $performanceEvaluationTestMapper->fetchOneByUuid($uuid);
662
        if (!$performanceEvaluationTest) {
663
            $data = [
664
                'success' => false,
665
                'data' => 'ERROR_RECORD_NOT_FOUND'
666
            ];
667
 
668
            return new JsonModel($data);
669
        }
670
 
671
 
672
        if ($performanceEvaluationTest->company_id != $currentCompany->id) {
673
            return new JsonModel([
674
                'success' => false,
675
                'data' => 'ERROR_UNAUTHORIZED'
676
            ]);
677
        }
678
 
679
        if ($performanceEvaluationTest->employee_id != $currentUser->id
680
            && $performanceEvaluationTest->supervisor_id != $currentUser->id ) {
681
            return new JsonModel([
682
                'success' => false,
683
                'data' => 'ERROR_UNAUTHORIZED'
684
            ]);
685
        }
686
 
687
        if($performanceEvaluationTest->status ==PerformanceEvaluationTest::STATUS_PENDING) {
688
            return new JsonModel([
689
                'success' => false,
690
                'data' =>   'ERROR_RECORD_IS_PENDING'
691
            ]);
692
 
693
        }
694
 
695
 
696
 
697
        $request = $this->getRequest();
698
        if ($request->isGet()) {
699
 
700
            switch( $performanceEvaluationTest->type)
701
            {
702
                case PerformanceEvaluationTest::TYPE_BOTH :
703
                    $type = $this->translator->translate('LABEL_PERFORMANCE_EVALUATION_TYPE_BOTH');
704
                    break;
705
 
706
                case PerformanceEvaluationTest::TYPE_SUPERVISOR :
707
                    $type = $this->translator->translate('LABEL_PERFORMANCE_EVALUATION_TYPE_SUPERVISOR');
708
                    break;
709
 
710
                case PerformanceEvaluationTest::TYPE_EMPLOYEE :
711
                    $type = $this->translator->translate('LABEL_PERFORMANCE_EVALUATION_TYPE_EMPLOYEE');
712
                    break;
713
 
714
                default :
715
                    $type = $this->translator->translate('LABEL_UNKNOWN');
716
                    break;
717
            }
718
 
719
 
720
            $performanceEvaluationFormMapper = PerformanceEvaluationFormMapper::getInstance($this->adapter);
721
            $performanceEvaluationForm = $performanceEvaluationFormMapper->fetchOne($performanceEvaluationTest->form_id);
722
 
723
            $userMapper = UserMapper::getInstance($this->adapter);
724
            $employee = $userMapper->fetchOne($performanceEvaluationTest->employee_id);
725
 
726
            $filename = $performanceEvaluationForm->name . '-' . trim($employee->first_name) . '-' . trim($employee->last_name)  . '-' . $type . '-' . date('Y-m-d H:i a') . '.pdf';
727
 
728
            $filename = Functions::normalizeStringFilename( $filename );
729
 
730
 
731
 
732
 
733
            $content = base64_encode($this->renderPDF($performanceEvaluationTest));
734
            $data = [
735
                'success' => true,
736
                'data' => [
737
                    'basename' => $filename,
738
                    'content' => $content
739
                ]
740
            ];
741
 
742
            return new JsonModel($data);
743
 
744
            /*
745
 
746
            $content = $this->renderPdf($currentCompany, $jobDescription);
747
            $response = new Response();
748
            $response->setStatusCode(200);
749
            $response->setContent($content);
750
 
751
 
752
 
753
            $headers = $response->getHeaders();
754
            $headers->clearHeaders();
755
 
756
            $headers->addHeaderLine('Content-Description: File Transfer');
757
            $headers->addHeaderLine('Content-Type: application/pdf');
758
            //$headers->addHeaderLine('Content-Disposition: attachment; filename=' . $filename);
759
            $headers->addHeaderLine('Content-Transfer-Encoding: binary');
760
            $headers->addHeaderLine('Expires: 0');
761
            $headers->addHeaderLine('Cache-Control: must-revalidate');
762
            $headers->addHeaderLine('Pragma: public');
763
            return $response;
764
            */
765
 
766
 
767
 
768
 
769
            return ;
770
        } else {
771
 
772
 
773
            return new JsonModel([
774
                'success' => false,
775
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
776
            ]);
777
        }
778
    }
779
 
780
    /**
781
     * Render PDF
782
     * @param PerformanceEvaluationTest $performanceEvaluationTest
783
 
784
     * @return mixed
785
     */
786
    private function renderPDF($performanceEvaluationTest)
787
    {
788
        $request = $this->getRequest();
789
        $currentUserPlugin = $this->plugin('currentUserPlugin');
790
        $currentCompany = $currentUserPlugin->getCompany();
791
        $currentUser = $currentUserPlugin->getUser();
792
 
793
 
794
        //Generate New PDF
795
        $pdf = new PerformanceEvaluationInterviewPDF();
796
 
797
        $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $currentCompany->uuid;
798
        $header = $currentCompany->header ? $target_path . DIRECTORY_SEPARATOR . $currentCompany->header : '';
799
        if (empty($header) || !file_exists($header)) {
800
            $header = $this->config['leaderslinked.images_default.company_pdf_header'];
801
        }
802
 
803
        $footer = $currentCompany->footer ? $target_path . DIRECTORY_SEPARATOR . $currentCompany->footer : '';
804
        if (empty($footer) || !file_exists($footer)) {
805
            $footer = $this->config['leaderslinked.images_default.company_pdf_footer'];
806
        }
807
 
808
        $content = json_decode($performanceEvaluationTest->content);
809
 
810
        $pdf->header = $header;
811
        $pdf->footer = $footer;
812
        $pdf->translator = $this->translator;
813
 
814
        $pdf->SetMargins(10, 0, 10);
815
 
816
        $pdf->AliasNbPages();
817
        $pdf->AddPage();
818
 
819
        switch( $performanceEvaluationTest->type)
820
        {
821
            case PerformanceEvaluationTest::TYPE_BOTH :
822
                $type = $this->translator->translate('LABEL_PERFORMANCE_EVALUATION_TYPE_BOTH');
823
                break;
824
 
825
            case PerformanceEvaluationTest::TYPE_SUPERVISOR :
826
                $type = $this->translator->translate('LABEL_PERFORMANCE_EVALUATION_TYPE_SUPERVISOR');
827
                break;
828
 
829
            case PerformanceEvaluationTest::TYPE_EMPLOYEE :
830
                $type = $this->translator->translate('LABEL_PERFORMANCE_EVALUATION_TYPE_EMPLOYEE');
831
                break;
832
 
833
            default :
834
                $type = $this->translator->translate('LABEL_UNKNOWN');
835
                break;
836
        }
837
 
838
        $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $performanceEvaluationTest->updated_on);
839
 
840
        $evaluated = '';
841
        $evaluator = '';
842
 
843
        $userMapper = UserMapper::getInstance($this->adapter);
844
        if($performanceEvaluationTest->employee_id) {
845
            $user = $userMapper->fetchOne($performanceEvaluationTest->employee_id);
846
            if($user) {
847
                $evaluated = ucwords(strtolower(trim($user->first_name . ' ' . $user->last_name)));
848
            }
849
        }
850
        if($performanceEvaluationTest->supervisor_id) {
851
            $user = $userMapper->fetchOne($performanceEvaluationTest->supervisor_id);
852
            if($user) {
853
                $evaluator = ucwords(strtolower(trim($user->first_name . ' ' . $user->last_name)));
854
            }
855
        }
856
 
857
        $rows = [
858
            [
859
                'title' => $this->translator->translate('LABEL_TYPE') . ' : ',
860
                'content' => $type,
861
            ],
862
            [
863
                'title' => $this->translator->translate('LABEL_PDF_PERFORMANCE_EVALUATION_POSITION') . ' : ',
864
                'content' => $content->name,
865
            ],
866
            [
867
                'title' => $this->translator->translate('LABEL_PDF_PERFORMANCE_EVALUATION_EVALUATED')  . ' : ',
16768 efrain 868
                'content' => Functions::utf8_decode($evaluated),
15444 efrain 869
            ],
870
 
871
            [
872
                'title' => $this->translator->translate('LABEL_PDF_PERFORMANCE_EVALUATION_EVALUATE_SIGNATURE') . ' : ',
873
                'content' => ''
874
            ],
875
            [
876
                'title' => $this->translator->translate('LABEL_PDF_PERFORMANCE_EVALUATION_EVALUATOR') . ' : ',
16768 efrain 877
                'content' => Functions::utf8_decode($evaluator),
15444 efrain 878
            ],
879
            [
880
                'title' => $this->translator->translate('LABEL_PDF_PERFORMANCE_EVALUATION_EVALUATOR_SIGNATURE') . ' : ',
881
                'content' => ''
882
            ],
883
            [
884
                'title' => $this->translator->translate( 'LABEL_PDF_PERFORMANCE_EVALUATION_MANAGER_SIGNATURE') . ' : ',
885
                'content' => ''
886
            ],
887
            [
888
                'title' => $this->translator->translate('LABEL_DATE'),
889
                'content' => $dt->format('d/m/Y H:i a')
890
            ]
891
        ];
892
 
893
 
894
 
895
        $pdf->borderTable($this->translator->translate('LABEL_PDF_PERFORMANCE_EVALUATION_TITLE'), $rows);
896
 
897
        $pdf->evaluationTable($content->comment, $content->points);
898
 
899
 
900
 
901
        // Competencies
902
        if ($content->competencies_selected) {
903
            $pdf->AddPage();
904
 
905
            $i = 0;
906
 
907
            $max = count($content->competencies_selected);
908
            for($i = 0; $i < $max; $i++)
909
            {
910
                $competency_selected = $content->competencies_selected[$i];
911
 
912
                $j = $i + 1;
913
                $last = $j == $max;
914
                $pdf->competencyTable($i, $competency_selected, $content->competencies, $content->competency_types, $content->behaviors, $content->evaluation, $last);
915
            }
916
 
917
        }
918
 
919
        return $pdf->Output('S');
920
    }
921
}