Proyectos de Subversion LeadersLinked - Services

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
/**
4
 *
5
 * Controlador: Autoevaluación
6
 *
7
 */
8
declare(strict_types=1);
9
 
10
namespace LeadersLinked\Controller;
11
 
12
use Laminas\Db\Adapter\AdapterInterface;
13
 
14
use Laminas\Mvc\Controller\AbstractActionController;
15
use Laminas\Log\LoggerInterface;
16
use Laminas\View\Model\ViewModel;
17
use Laminas\View\Model\JsonModel;
18
use LeadersLinked\Model\Notification;
19
use LeadersLinked\Mapper\SelfEvaluationTestMapper;
20
use LeadersLinked\Mapper\NotificationMapper;
21
use LeadersLinked\Mapper\QueryMapper;
22
use LeadersLinked\Model\SelfEvaluationForm;
23
use LeadersLinked\Mapper\SelfEvaluationFormMapper;
24
use LeadersLinked\Model\SelfEvaluationTest;
25
use Laminas\Db\Sql\Select;
26
use LeadersLinked\Library\SelfEvaluationPDF;
27
use LeadersLinked\Mapper\SelfEvaluationFormUserMapper;
28
use LeadersLinked\Form\SelfEvaluation\SelfEvaluationTestForm;
29
use LeadersLinked\Library\Functions;
30
use LeadersLinked\Mapper\UserMapper;
31
 
32
class SelfEvaluationController extends AbstractActionController {
33
 
34
    /**
35
     *
36
     * @var \Laminas\Db\Adapter\AdapterInterface
37
     */
38
    private $adapter;
39
 
40
    /**
41
     *
42
     * @var \LeadersLinked\Cache\CacheInterface
43
     */
44
    private $cache;
45
 
46
 
47
    /**
48
     *
49
     * @var \Laminas\Log\LoggerInterface
50
     */
51
    private $logger;
52
 
53
    /**
54
     *
55
     * @var array
56
     */
57
    private $config;
58
 
59
 
60
    /**
61
     *
62
     * @var \Laminas\Mvc\I18n\Translator
63
     */
64
    private $translator;
65
 
66
 
67
    /**
68
     *
69
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
70
     * @param \LeadersLinked\Cache\CacheInterface $cache
71
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
72
     * @param array $config
73
     * @param \Laminas\Mvc\I18n\Translator $translator
74
     */
75
    public function __construct($adapter, $cache, $logger, $config, $translator)
76
    {
77
        $this->adapter      = $adapter;
78
        $this->cache        = $cache;
79
        $this->logger       = $logger;
80
        $this->config       = $config;
81
        $this->translator   = $translator;
82
    }
83
 
84
    /**
85
     *
86
     * Generación del listado de evaluaciones
87
     * {@inheritDoc}
88
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
89
     */
90
    public function indexAction() {
91
        $currentUserPlugin = $this->plugin('currentUserPlugin');
92
        $currentUser = $currentUserPlugin->getUser();
93
 
94
        $request = $this->getRequest();
95
        if ($request->isGet()) {
96
 
97
 
98
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));
99
 
100
 
101
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
102
                $allowTakeATest = $acl->isAllowed($currentUser->usertype_id, 'profile/self-evaluation/take-a-test');
103
                $allowReport = $acl->isAllowed($currentUser->usertype_id, 'profile/self-evaluation/report');
104
 
105
 
106
 
107
 
108
                $queryMapper = QueryMapper::getInstance($this->adapter);
109
 
110
                $select = $queryMapper->getSql()->select();
111
                $select->columns(['uuid', 'name', 'description', 'text', 'language']);
112
                $select->from(['f' => SelfEvaluationFormMapper::_TABLE]);
113
                $select->join(['fu' => SelfEvaluationFormUserMapper::_TABLE], 'f.id = fu.form_id', []);
114
                $select->join(['t' => SelfEvaluationTestMapper::_TABLE], 'fu.form_id = t.form_id AND fu.user_id = t.user_id', ['status'], Select::JOIN_LEFT_OUTER);
115
                $select->where->equalTo('f.status', SelfEvaluationForm::STATUS_ACTIVE);
116
                $select->where->equalTo('fu.user_id', $currentUser->id);
117
 
118
 
119
                if ($search) {
120
                    $select->where->NEST->like('name', '%' . $search . '%');
121
                }
122
                $select->order('name ASC, language ASC');
123
 
124
                $records = $queryMapper->fetchAll($select);
125
                $items = [];
126
                foreach ($records as $record) {
127
                    switch ($record['language']) {
128
                        case SelfEvaluationForm::LANGUAGE_ENGLISH :
129
                            $language = 'LABEL_ENGLISH';
130
                            break;
131
 
132
                        case SelfEvaluationForm::LANGUAGE_SPANISH :
133
                            $language = 'LABEL_SPANISH';
134
                            break;
135
 
136
                        default :
137
                            $language = '';
138
                            break;
139
                    }
140
 
141
                    switch ($record['status']) {
142
 
143
                        case SelfEvaluationTest::STATUS_DRAFT :
144
                            $status = 'LABEL_DRAFT';
145
                            break;
146
 
147
                        case SelfEvaluationTest::STATUS_COMPLETED :
148
                            $status = 'LABEL_COMPLETED';
149
                            break;
150
 
151
                        case SelfEvaluationTest::STATUS_PENDING :
152
                            $status = 'LABEL_PENDING';
153
                            break;
154
 
155
                        case SelfEvaluationTest::STATUS_REVIEW :
156
                            $status = 'LABEL_REVIEW';
157
                            break;
158
 
159
 
160
                        default :
161
                            $status = 'LABEL_AVAILABLE';
162
                            break;
163
                    }
164
 
165
 
166
                    $item = [
167
                        'name' => $record['name'],
168
                        'description' => $record['description'],
169
                        'text' => $record['text'],
170
                        'language' => $language,
171
                        'status' => $status,
172
                        'link_take_a_test' => $allowTakeATest && ( empty($record['status']) || $record['status'] == SelfEvaluationTest::STATUS_DRAFT) ? $this->url()->fromRoute('profile/self-evaluation/take-a-test', ['id' => $record['uuid']]) : '',
173
                        'link_report' => $allowReport && $record['status'] == SelfEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('profile/self-evaluation/report', ['id' => $record['uuid']]) : '',
174
                    ];
175
 
176
 
177
 
178
                    array_push($items, $item);
179
                }
180
 
181
 
182
 
183
 
184
                $response = [
185
                    'success' => true,
186
                    'data' => $items
187
                ];
188
 
189
                return new JsonModel($response);
190
 
191
        } else {
192
            return new JsonModel([
193
                'success' => false,
194
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
195
            ]);
196
        }
197
    }
198
 
199
    public function takeaTestAction() {
200
        $currentUserPlugin = $this->plugin('currentUserPlugin');
201
        $currentUser = $currentUserPlugin->getUser();
202
 
203
        $uuid = $this->params()->fromRoute('id');
204
 
205
        $companySelfEvaluationFormMapper = SelfEvaluationFormMapper::getInstance($this->adapter);
206
        $companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOneByUuid($uuid);
207
 
208
        if (!$companySelfEvaluationForm) {
209
            return new JsonModel([
210
                'success' => false,
211
                'data' => 'ERROR_FORM_EVALUATION_NOT_FOUND'
212
            ]);
213
        }
214
 
215
        if ($companySelfEvaluationForm->status == SelfEvaluationForm::STATUS_INACTIVE) {
216
            return new JsonModel([
217
                'success' => false,
218
                'data' => 'ERROR_FORM_EVALUATION_IS_INACTIVE'
219
            ]);
220
        }
221
 
222
 
223
        $companySelfEvaluationFormUserMapper = SelfEvaluationFormUserMapper::getInstance($this->adapter);
224
        $companySelfEvaluationFormUser = $companySelfEvaluationFormUserMapper->fetchOneByFormIdAndUserId($companySelfEvaluationForm->id, $currentUser->id);
225
        if (!$companySelfEvaluationFormUser) {
226
            return new JsonModel([
227
                'success' => false,
228
                'data' => 'ERROR_FORM_EVALUATION_YOU_CAN_NOT_TAKE'
229
            ]);
230
        }
231
 
232
 
233
        $companySelfEvaluationTestMapper = SelfEvaluationTestMapper::getInstance($this->adapter);
234
        $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneBy($companySelfEvaluationForm->id, $currentUser->id);
235
 
236
        if ($companySelfEvaluationTest && $companySelfEvaluationTest->status != SelfEvaluationTest::STATUS_DRAFT) {
237
            return new JsonModel([
238
                'success' => false,
239
                'data' => 'ERROR_FORM_EVALUATION_ALREADY_YOUR_APPLICATION_IN_THIS_TEST'
240
            ]);
241
        }
242
 
243
 
244
        $request = $this->getRequest();
245
        if ($request->isGet()) {
246
            return new JsonModel([
247
                'success' => true,
248
                'data' => [
249
                    'name' => $companySelfEvaluationForm->name,
250
                    'description' => $companySelfEvaluationForm->description,
251
                    'text' => $companySelfEvaluationForm->text,
252
                    'content' => $companySelfEvaluationTest && $companySelfEvaluationTest->status == SelfEvaluationTest::STATUS_DRAFT ?
253
                    json_decode($companySelfEvaluationTest->content) :
254
                    json_decode($companySelfEvaluationForm->content),
255
                ]
256
            ]);
257
        }
258
 
259
        if ($request->isPost()) {
260
            $form = new SelfEvaluationTestForm();
261
            $dataPost = $request->getPost()->toArray();
262
 
263
            $form->setData($dataPost);
264
 
265
            if ($form->isValid()) {
266
                $dataPost = (array) $form->getData();
267
 
268
                $selfEvaluationTest = new SelfEvaluationTest();
269
                $selfEvaluationTest->company_id = $companySelfEvaluationForm->company_id;
270
                $selfEvaluationTest->form_id = $companySelfEvaluationForm->id;
271
                $selfEvaluationTest->user_id = $currentUser->id;
272
                $selfEvaluationTest->status = $dataPost['status'];
273
                $selfEvaluationTest->content = $dataPost['content'];
274
 
275
 
276
                //Check if the form is already registered
277
                $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneBy($companySelfEvaluationForm->id, $currentUser->id);
278
 
279
                $result = $companySelfEvaluationTest ?
280
                        $companySelfEvaluationTestMapper->update($selfEvaluationTest, $companySelfEvaluationTest->id) :
281
                        $companySelfEvaluationTestMapper->insert($selfEvaluationTest);
282
 
283
 
284
                if ($result) {
285
                    $this->logger->info('Se agrego un nuevo test de auto-evaluación : ' . $companySelfEvaluationForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
286
 
287
                    $data = [
288
                        'success' => true,
289
                        'data' => $companySelfEvaluationTest ? 'LABEL_RECORD_UPDATED' : 'LABEL_RECORD_ADDED'
290
                    ];
291
                } else {
292
                    $data = [
293
                        'success' => false,
294
                        'data' => $companySelfEvaluationTestMapper->getError()
295
                    ];
296
                }
297
 
298
                return new JsonModel($data);
299
            } else {
300
                $messages = [];
301
                $form_messages = (array) $form->getMessages();
302
                foreach ($form_messages as $fieldname => $field_messages) {
303
 
304
                    $messages[$fieldname] = array_values($field_messages);
305
                }
306
 
307
                return new JsonModel([
308
                    'success' => false,
309
                    'data' => $messages
310
                ]);
311
            }
312
        }
313
 
314
        return new JsonModel([
315
            'success' => false,
316
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
317
        ]);
318
    }
319
 
320
    public function reportAction() {
321
 
322
        $currentUserPlugin = $this->plugin('currentUserPlugin');
323
        $currentUser = $currentUserPlugin->getUser();
324
 
325
        $uuid = $this->params()->fromRoute('id');
326
 
327
        $companySelfEvaluationFormMapper = SelfEvaluationFormMapper::getInstance($this->adapter);
328
        $companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOneByUuid($uuid);
329
 
330
        if (!$companySelfEvaluationForm) {
331
            return new JsonModel([
332
                'success' => false,
333
                'data' => 'ERROR_FORM_EVALUATION_NOT_FOUND'
334
            ]);
335
        }
336
 
337
        if ($companySelfEvaluationForm->status == SelfEvaluationForm::STATUS_INACTIVE) {
338
            return new JsonModel([
339
                'success' => false,
340
                'data' => 'ERROR_FORM_EVALUATION_IS_INACTIVE'
341
            ]);
342
        }
343
 
344
 
345
        $companySelfEvaluationFormUserMapper = SelfEvaluationFormUserMapper::getInstance($this->adapter);
346
        $companySelfEvaluationFormUser = $companySelfEvaluationFormUserMapper->fetchOneByFormIdAndUserId($companySelfEvaluationForm->id, $currentUser->id);
347
        if (!$companySelfEvaluationFormUser) {
348
            return new JsonModel([
349
                'success' => false,
350
                'data' => 'ERROR_FORM_EVALUATION_YOU_CAN_NOT_TAKE'
351
            ]);
352
        }
353
 
354
 
355
        $companySelfEvaluationTestMapper = SelfEvaluationTestMapper::getInstance($this->adapter);
356
        $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneBy($companySelfEvaluationForm->id, $currentUser->id);
357
 
358
        if (!$companySelfEvaluationTest) {
359
            return new JsonModel([
360
                'success' => false,
361
                'data' => 'ERROR_FORM_EVALUATION_TEST_NOT_FOUND'
362
            ]);
363
        }
364
 
365
        if ($companySelfEvaluationTest->status != SelfEvaluationTest::STATUS_COMPLETED) {
366
            return new JsonModel([
367
                'success' => false,
368
                'data' => 'ERROR_FORM_EVALUATION_TEST_IS_INCOPLETE'
369
            ]);
370
        }
371
 
372
 
373
        $request = $this->getRequest();
374
 
375
        if ($request->isGet()) {
376
 
377
            return $this->renderPDF($companySelfEvaluationForm, $companySelfEvaluationTest, $currentUser);
378
        }
379
 
380
        return new JsonModel([
381
            'success' => false,
382
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
383
        ]);
384
    }
385
 
386
    /**
387
     * Render Pdf document
388
     * @param SelfEvaluationForm $companySelfEvaluationForm
389
     * @param SelfEvaluationTest $companySelfEvaluationTest
390
     * @param UserMapper $userMapper
391
     * @return mixed
392
     */
393
    public function renderPDF($companySelfEvaluationForm, $companySelfEvaluationTest, $userMapper) {
394
 
395
        // Set Data
396
        $headerFormName = Functions::utf8_decode($companySelfEvaluationForm->name);
397
        $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($companySelfEvaluationTest->added_on)));
398
        $sections = json_decode($companySelfEvaluationTest->content, true);
399
        $labels = ['Total', 'Logrado'];
400
 
401
 
402
        //Generate New PDF
403
        $pdf = new SelfEvaluationPDF();
404
 
405
        $pdf->AliasNbPages();
406
        $pdf->AddPage();
407
 
408
        // Set header secundary
409
        $pdf->customHeader($headerFormName, $headerUserName);
410
 
411
        $valueFormSections = 0;
412
        $valueFormQuestions = 0;
413
 
414
        $countSection = 0;
415
 
416
        for ($i = 0; $i < count($sections); $i++) {
417
            if ($countSection > 1) {
418
                $countSection = 0;
419
                $pdf->AddPage();
420
                $pdf->customHeader($headerFormName, $headerUserName);
421
            }
422
 
423
            $pdf->SetY(70 + (92 * $countSection));
424
            $totalQuestion = 0;
425
 
426
            $valueSection = floatval($sections[$i]['value']);
427
            $valueFormSections = $valueFormSections + $valueSection;
428
 
429
            for ($j = 0; $j < count($sections[$i]['questions']); $j++) {
430
                $totalQuestion = $totalQuestion + $this->getPtosAnswer($sections[$i]['questions'][$j]);
431
            }
432
 
433
            $values = [$valueSection, $totalQuestion];
434
            $valueFormQuestions = $valueFormQuestions + $totalQuestion;
435
 
436
 
437
            $filename = 'data' . DIRECTORY_SEPARATOR . 'self-evaluation' . $sections[$i]['slug_section'] . '.png';
438
            $pdf->PieChart($labels, $values, '', $filename);
439
            $pdf->SetFont('Arial', '', 8);
440
            $pdf->Cell(190, 10, Functions::utf8_decode($sections[$i]['name']), 0, 0, 'C');
441
            $pdf->setY($pdf->getY() + 10);
442
            // Salto de línea
443
            $pdf->Image($filename, 60, $pdf->getY(), 90);
444
            $pdf->setY($pdf->getY() + 60);
445
 
446
            $countSection++;
447
        }
448
 
449
        $pdf->AddPage();
450
        $pdf->customHeader($headerFormName, $headerUserName);
451
 
452
        $pdf->SetFont('Arial', 'B', 10);
453
        $pdf->Cell(190, 10, Functions::utf8_decode('Desempeño General'), 0, 0, 'C');
454
        $pdf->setY($pdf->getY() + 10);
455
 
456
 
457
        $values = [$valueFormSections, $valueFormQuestions];
458
 
459
        $filenameGeneral = 'data' . DIRECTORY_SEPARATOR . 'self-evaluation' . uniqid() . '.png';
460
        $pdf->PieChart($labels, $values, '', $filenameGeneral);
461
        $pdf->Image($filenameGeneral, 60, $pdf->getY(), 90);
462
        $pdf->setY($pdf->getY() + 60);
463
 
464
 
465
        if ($companySelfEvaluationTest->comments) {
466
 
467
            $pdf->SetFont('Arial', 'B', 10);
468
            $pdf->Cell(190, 10, Functions::utf8_decode('Comentarios finales'), 0, 0, 'C');
469
            $pdf->setY($pdf->getY() + 10);
470
 
471
            $pdf->SetFont('Arial', '', 8);
472
            $pdf->MultiCell(180, 8, Functions::utf8_decode($companySelfEvaluationTest->comments));
473
            $pdf->setY(60);
474
        }
475
 
476
        return $pdf->Output();
477
    }
478
 
479
    /**
480
     * get total ptos Answer
481
     * @param array $question
482
     * @return int
483
     */
484
    public function getPtosAnswer($question) {
485
 
486
        $ptos = 0;
487
 
488
        if ($question['type'] == "open" || $question['type'] == "rating-open" || $question['type'] == "rating-range") {
489
 
490
            $ptos = isset($question['question_value']) ? $question['question_value'] : 0;
491
        } else {
492
            if ($question['type'] == 'multiple') {
493
 
494
                for ($o = 0; $o < count($question['options']); $o++) {
495
                    if (in_array($question['options'][$o]['slug_option'], $question['answer'])) {
496
                        $ptos = $ptos + $question['options'][$o]['value'];
497
                    }
498
                }
499
            } else {
500
 
501
                for ($o = 0; $o < count($question['options']); $o++) {
502
                    if ($question['options'][$o]['slug_option'] == $question['answer']) {
503
                        $ptos = $question['options'][$o]['value'];
504
                    }
505
                }
506
            }
507
        }
508
        return $ptos;
509
    }
510
 
511
}