Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 554 | Rev 557 | 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();
516 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,
516 geraldo 163
                        'added_on' => $record['added_on'],
164
                        'actions' => [
469 geraldo 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
                        ]
286 geraldo 168
                    ];
169
 
170
                    array_push($items, $item);
171
                }
172
 
244 geraldo 173
                return new JsonModel([
174
                    'success' => true,
175
                    'data' => [
286 geraldo 176
                        'items' => $items,
291 geraldo 177
                        'total' => count($items),
244 geraldo 178
                    ]
179
                ]);
180
            } else {
181
 
349 geraldo 182
                $form = new CompanySelfEvaluationTestForm();
244 geraldo 183
 
184
                $this->layout()->setTemplate('layout/layout-backend');
185
                $viewModel = new ViewModel();
250 geraldo 186
                $viewModel->setTemplate('leaders-linked/self-evaluation-reviews/index.phtml');
244 geraldo 187
                $viewModel->setVariable('form', $form);
188
                return $viewModel;
189
            }
190
        } else {
191
            return new JsonModel([
192
                'success' => false,
193
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
194
            ]);
195
            ;
196
        }
197
    }
302 geraldo 198
 
244 geraldo 199
    public function editAction() {
200
        $request = $this->getRequest();
201
        $currentUserPlugin = $this->plugin('currentUserPlugin');
202
        $currentCompany = $currentUserPlugin->getCompany();
203
        $currentUser = $currentUserPlugin->getUser();
204
 
205
        $request = $this->getRequest();
206
        $uuid = $this->params()->fromRoute('id');
207
 
208
 
209
        if (!$uuid) {
210
            $data = [
211
                'success' => false,
212
                'data' => 'ERROR_INVALID_PARAMETER'
213
            ];
214
 
215
            return new JsonModel($data);
216
        }
217
 
302 geraldo 218
        $companySelfEvaluationTestMapper = CompanySelfEvaluationTestMapper::getInstance($this->adapter);
446 geraldo 219
        $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneByUuid($uuid);
220
        if (!$companySelfEvaluationTest) {
244 geraldo 221
            $data = [
222
                'success' => false,
223
                'data' => 'ERROR_RECORD_NOT_FOUND'
224
            ];
225
 
226
            return new JsonModel($data);
227
        }
228
 
229
        if ($request->isPost()) {
349 geraldo 230
            $form = new CompanySelfEvaluationTestForm();
363 geraldo 231
            $dataPost = $request->getPost()->toArray();
232
            $form->setData($dataPost);
364 geraldo 233
 
244 geraldo 234
            if ($form->isValid()) {
366 geraldo 235
 
365 geraldo 236
                $selfEvaluationTest = new CompanySelfEvaluationTest();
466 geraldo 237
                $selfEvaluationTest->status = CompanySelfEvaluationTest::STATUS_COMPLETED;
365 geraldo 238
                $selfEvaluationTest->content = $dataPost['content'];
353 geraldo 239
 
446 geraldo 240
                $result = $companySelfEvaluationTestMapper->update($selfEvaluationTest, $companySelfEvaluationTest->id);
364 geraldo 241
 
242
                if ($result) {
243
                    $data = [
244
                        'success' => true,
245
                        'data' => 'LABEL_RECORD_UPDATED'
246
                    ];
247
                } else {
248
                    $data = [
249
                        'success' => false,
250
                        'data' => $companySelfEvaluationTestMapper->getError()
251
                    ];
252
                }
253
 
362 geraldo 254
                return new JsonModel($data);
244 geraldo 255
            } else {
256
                $messages = [];
257
                $form_messages = (array) $form->getMessages();
258
                foreach ($form_messages as $fieldname => $field_messages) {
259
                    $messages[$fieldname] = array_values($field_messages);
260
                }
261
 
262
                return new JsonModel([
263
                    'success' => false,
264
                    'data' => $messages
265
                ]);
266
            }
267
        } else if ($request->isGet()) {
268
            $hydrator = new ObjectPropertyHydrator();
269
 
315 geraldo 270
            //get form data
271
            $companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
446 geraldo 272
            $companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOne($companySelfEvaluationTest->form_id);
244 geraldo 273
 
315 geraldo 274
            //get user data
275
            $CompanyUserMapper = UserMapper::getInstance($this->adapter);
446 geraldo 276
            $userMapper = $CompanyUserMapper->fetchOne($companySelfEvaluationTest->user_id);
315 geraldo 277
 
446 geraldo 278
            if ($companySelfEvaluationForm && $userMapper) {
315 geraldo 279
 
280
                $data = [
281
                    'success' => true,
282
                    'data' => [
446 geraldo 283
                        'id' => $companySelfEvaluationTest->id,
284
                        'name' => $companySelfEvaluationForm->name,
285
                        'text' => $companySelfEvaluationForm->text,
315 geraldo 286
                        'user' => $userMapper->first_name . ' ' . $userMapper->last_name,
446 geraldo 287
                        'status' => $companySelfEvaluationTest->status,
288
                        'content' => json_decode($companySelfEvaluationTest->content),
315 geraldo 289
                    ]
290
                ];
291
 
292
                return new JsonModel($data);
293
            } else {
294
 
295
                $data = [
296
                    'success' => false,
297
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
298
                ];
299
 
300
                return new JsonModel($data);
301
            }
244 geraldo 302
        } else {
303
            $data = [
304
                'success' => false,
305
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
306
            ];
307
 
308
            return new JsonModel($data);
309
        }
310
 
311
        return new JsonModel($data);
312
    }
313
 
487 geraldo 314
    public function reportAction() {
315
        $request = $this->getRequest();
316
        $currentUserPlugin = $this->plugin('currentUserPlugin');
317
        $currentCompany = $currentUserPlugin->getCompany();
318
        $currentUser = $currentUserPlugin->getUser();
484 geraldo 319
 
487 geraldo 320
        $request = $this->getRequest();
321
        $uuid = $this->params()->fromRoute('id');
322
 
323
 
324
        if (!$uuid) {
325
            $data = [
326
                'success' => false,
327
                'data' => 'ERROR_INVALID_PARAMETER'
328
            ];
329
 
330
            return new JsonModel($data);
331
        }
332
 
333
        $companySelfEvaluationTestMapper = CompanySelfEvaluationTestMapper::getInstance($this->adapter);
334
        $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneByUuid($uuid);
335
        if (!$companySelfEvaluationTest) {
336
            $data = [
337
                'success' => false,
338
                'data' => 'ERROR_RECORD_NOT_FOUND'
339
            ];
340
 
341
            return new JsonModel($data);
342
        }
343
 
344
        if ($request->isGet()) {
345
            $hydrator = new ObjectPropertyHydrator();
346
 
347
            //get form data
348
            $companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
349
            $companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOne($companySelfEvaluationTest->form_id);
350
 
351
            //get user data
352
            $CompanyUserMapper = UserMapper::getInstance($this->adapter);
353
            $userMapper = $CompanyUserMapper->fetchOne($companySelfEvaluationTest->user_id);
354
 
355
            if ($companySelfEvaluationForm && $userMapper) {
356
 
519 geraldo 357
                return $this->renderPDF($companySelfEvaluationForm, $companySelfEvaluationTest, $userMapper);
523 geraldo 358
 
519 geraldo 359
            } else {
516 geraldo 360
 
519 geraldo 361
                $data = [
362
                    'success' => false,
363
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
364
                ];
516 geraldo 365
 
519 geraldo 366
                return new JsonModel($data);
367
            }
524 geraldo 368
 
519 geraldo 369
        } else {
370
            $data = [
371
                'success' => false,
372
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
373
            ];
516 geraldo 374
 
519 geraldo 375
            return new JsonModel($data);
376
        }
516 geraldo 377
 
519 geraldo 378
        return new JsonModel($data);
379
    }
516 geraldo 380
 
519 geraldo 381
    /**
382
     * Render Pdf document
383
     * @param type $companySelfEvaluationForm
384
     * @param type $companySelfEvaluationTest
385
     * @param type $userMapper
386
     * @return type
387
     */
388
    public function renderPDF($companySelfEvaluationForm, $companySelfEvaluationTest, $userMapper) {
516 geraldo 389
 
521 geraldo 390
        // Set Data
519 geraldo 391
        $headerFormName = utf8_decode($companySelfEvaluationForm->name);
555 geraldo 392
        $headerUserName = utf8_decode('LABEL_SELF_EVALUATION_INFORM' . trim($userMapper->first_name . ' ' . $userMapper->last_name) . ' al ' . date("m-d-Y H:i:s", strtotime($companySelfEvaluationTest->added_on)));
519 geraldo 393
        $sections = json_decode($companySelfEvaluationTest->content, true);
521 geraldo 394
        $comments = '';
555 geraldo 395
        $labels = ['LABEL_TOTAL', 'LABEL_SUCCES'];
516 geraldo 396
 
397
 
521 geraldo 398
        //Generate New PDF
519 geraldo 399
        $pdf = new Pdf();
516 geraldo 400
 
519 geraldo 401
        $pdf->AliasNbPages();
402
        $pdf->AddPage();
521 geraldo 403
 
404
        // Set header secundary
519 geraldo 405
        $pdf->pageHeader($headerFormName, $headerUserName);
516 geraldo 406
 
519 geraldo 407
        $valueFormSections = 0;
408
        $valueFormQuestions = 0;
516 geraldo 409
 
519 geraldo 410
        $countSection = 0;
516 geraldo 411
 
525 geraldo 412
        for ($i = 0; $i < count($sections); $i++) {
519 geraldo 413
            if ($countSection > 1) {
414
                $countSection = 0;
516 geraldo 415
                $pdf->AddPage();
416
                $pdf->pageHeader($headerFormName, $headerUserName);
519 geraldo 417
            }
516 geraldo 418
 
519 geraldo 419
            $pdf->SetY(70 + (92 * $countSection));
420
            $totalQuestion = 0;
516 geraldo 421
 
519 geraldo 422
            $valueSection = floatval($sections[$i]['value']);
423
            $valueFormSections = $valueFormSections + $valueSection;
516 geraldo 424
 
519 geraldo 425
            for ($j = 0; $j < count($sections[$i]['questions']); $j++) {
426
                $totalQuestion = $totalQuestion + $this->getPtosAnswer($sections[$i]['questions'][$j]);
521 geraldo 427
                if(isset($sections[$i]['questions'][$j]['comment'])){
554 geraldo 428
                    $comments = $comments.' '.$sections[$i]['questions'][$j]['comment'];
521 geraldo 429
                }
519 geraldo 430
            }
516 geraldo 431
 
519 geraldo 432
            $values = [$valueSection, $totalQuestion];
433
            $valueFormQuestions = $valueFormQuestions + $totalQuestion;
516 geraldo 434
 
435
 
550 geraldo 436
            $filename = 'data' . DIRECTORY_SEPARATOR . 'self-evaluation'. $sections[$i]['slug_section'] . '.png';
532 geraldo 437
            $pdf->barChart($labels, $values,'',$filename);
519 geraldo 438
            $pdf->SetFont('Arial', '', 8);
439
            $pdf->Cell(190, 10, utf8_decode($sections[$i]['name']), 0, 0, 'C');
440
            $pdf->setY($pdf->getY() + 10);
441
            // Salto de línea
538 geraldo 442
            $pdf->Image($filename, 60, $pdf->getY(), 90);
519 geraldo 443
            $pdf->setY($pdf->getY() + 60);
516 geraldo 444
 
519 geraldo 445
            $countSection++;
446
        }
487 geraldo 447
 
519 geraldo 448
        $pdf->AddPage();
449
        $pdf->pageHeader($headerFormName, $headerUserName);
487 geraldo 450
 
519 geraldo 451
        $pdf->SetFont('Arial', 'B', 10);
555 geraldo 452
        $pdf->Cell(190, 10, utf8_decode('LABEL_OVERAL_PERFORMANCE'), 0, 0, 'C');
519 geraldo 453
        $pdf->setY($pdf->getY() + 10);
487 geraldo 454
 
455
 
519 geraldo 456
        $values = [$valueFormSections, $valueFormQuestions];
457
 
550 geraldo 458
        $filenameGeneral = 'data' . DIRECTORY_SEPARATOR . 'self-evaluation'. uniqid() . '.png';
538 geraldo 459
        $pdf->barChart($labels, $values, '', $filenameGeneral);
460
        $pdf->Image($filenameGeneral, 60, $pdf->getY(), 90);
519 geraldo 461
        $pdf->setY($pdf->getY() + 60);
462
 
521 geraldo 463
        $pdf->SetFont('Arial','B', 10);
555 geraldo 464
        $pdf->Cell(190,10, utf8_decode('LABEL_FINAL_COMMENT'),0,0,'C');
521 geraldo 465
        $pdf->setY($pdf->getY() + 10);
466
 
467
        $pdf->SetFont('Arial','', 8);
468
        $pdf->MultiCell(180,  8, $comments);
519 geraldo 469
        $pdf->setY(60);
470
 
471
        return $pdf->Output();
487 geraldo 472
    }
473
 
516 geraldo 474
    /**
475
     * get total ptos Answer
476
     * @param type $question
477
     * @return type
478
     */
479
    public function getPtosAnswer($question) {
513 geraldo 480
 
516 geraldo 481
        $ptos = 0;
482
 
483
        if ($question['type'] == "open" || $question['type'] == "rating-open" || $question['type'] == "rating-range") {
484
 
485
            $ptos = isset($question['question_value']) ? $question['question_value'] : 0;
486
        } else {
487
            if ($question['type'] == 'multiple') {
488
 
489
                for ($o = 0; $o < count($question['options']); $o++) {
490
                    if (in_array($question['options'][$o]['slug_option'], $question['answer'])) {
491
                        $ptos = $ptos + $question['options'][$o]['value'];
492
                    }
493
                }
494
            } else {
495
 
496
                for ($o = 0; $o < count($question['options']); $o++) {
497
                    if ($question['options'][$o]['slug_option'] == $question['answer']) {
498
                        $ptos = $question['options'][$o]['value'];
499
                    }
500
                }
501
            }
502
        }
503
        return $ptos;
513 geraldo 504
    }
505
 
244 geraldo 506
}