Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 1272 | Rev 1274 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
977 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;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\CompanyPerformanceEvaluationFormMapper;
15
use LeadersLinked\Form\CompanyPerformanceEvaluationFormForm;
16
use LeadersLinked\Model\CompanyPerformanceEvaluationForm;
17
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
1052 geraldo 18
use LeadersLinked\Mapper\JobDescriptionMapper;
1270 geraldo 19
use LeadersLinked\Mapper\CompanyPerformanceEvaluationTestMapper;
1271 geraldo 20
use LeadersLinked\Mapper\JobDescriptionCompetencyMapper;
1272 geraldo 21
use LeadersLinked\Mapper\CompetencyMapper;
1273 geraldo 22
use LeadersLinked\Mapper\CompetencyTypeMapper;
977 geraldo 23
use LeadersLinked\Mapper\CompanyPerformanceEvaluationFormUserMapper;
24
 
25
class PerformanceEvaluationFormController extends AbstractActionController {
26
 
27
    /**
28
     *
29
     * @var AdapterInterface
30
     */
31
    private $adapter;
32
 
33
    /**
34
     *
35
     * @var AbstractAdapter
36
     */
37
    private $cache;
38
 
39
    /**
40
     *
41
     * @var  LoggerInterface
42
     */
43
    private $logger;
44
 
45
    /**
46
     *
47
     * @var array
48
     */
49
    private $config;
50
 
51
    /**
52
     *
53
     * @param AdapterInterface $adapter
54
     * @param AbstractAdapter $cache
55
     * @param LoggerInterface $logger
56
     * @param array $config
57
     */
58
    public function __construct($adapter, $cache, $logger, $config) {
59
        $this->adapter = $adapter;
60
        $this->cache = $cache;
61
        $this->logger = $logger;
62
        $this->config = $config;
63
    }
64
 
65
    public function indexAction() {
66
        $request = $this->getRequest();
67
        $currentUserPlugin = $this->plugin('currentUserPlugin');
68
        $currentCompany = $currentUserPlugin->getCompany();
69
        $currentUser = $currentUserPlugin->getUser();
70
 
71
 
72
        $request = $this->getRequest();
73
        if ($request->isGet()) {
74
 
75
            $headers = $request->getHeaders();
76
 
77
            $isJson = false;
78
            if ($headers->has('Accept')) {
79
                $accept = $headers->get('Accept');
80
 
81
                $prioritized = $accept->getPrioritized();
82
 
83
                foreach ($prioritized as $key => $value) {
84
                    $raw = trim($value->getRaw());
85
 
86
                    if (!$isJson) {
87
                        $isJson = strpos($raw, 'json');
88
                    }
89
                }
90
            }
91
 
92
            if ($isJson) {
93
                $search = $this->params()->fromQuery('search', []);
94
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
95
 
96
                $page = intval($this->params()->fromQuery('start', 1), 10);
97
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
98
                $order = $this->params()->fromQuery('order', []);
99
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
100
                $order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
101
 
102
                $fields = ['name'];
103
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
104
 
105
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
106
                    $order_direction = 'ASC';
107
                }
108
 
109
                $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
110
                $paginator = $companyPerformanceEvaluationMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
111
 
1078 geraldo 112
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
113
 
977 geraldo 114
                $items = [];
115
                $records = $paginator->getCurrentItems();
116
                foreach ($records as $record) {
117
 
1078 geraldo 118
 
119
 
1098 geraldo 120
                    $jobDescription = $jobDescriptionMapper->fetchOne($record->job_description_id);
121
                    if ($jobDescription) {
977 geraldo 122
 
1098 geraldo 123
                        $item = [
124
                            'id' => $record->id,
125
                            'name' => $record->name,
126
                            'job_description' => $jobDescription->name,
127
                            'status' => $record->status,
128
                            'actions' => [
1263 geraldo 129
                                'link_report' => $this->url()->fromRoute('performance-evaluation/forms/report', ['id' => $record->uuid]),
1098 geraldo 130
                                'link_edit' => $this->url()->fromRoute('performance-evaluation/forms/edit', ['id' => $record->uuid]),
131
                                'link_delete' => $this->url()->fromRoute('performance-evaluation/forms/delete', ['id' => $record->uuid])
132
                            ]
133
                        ];
134
                    }
135
 
977 geraldo 136
                    array_push($items, $item);
137
                }
138
 
139
                return new JsonModel([
140
                    'success' => true,
141
                    'data' => [
142
                        'items' => $items,
143
                        'total' => $paginator->getTotalItemCount(),
144
                    ]
145
                ]);
146
            } else {
147
 
1064 geraldo 148
                $form = new CompanyPerformanceEvaluationFormForm($this->adapter, $currentCompany->id);
977 geraldo 149
 
1051 geraldo 150
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
151
                $jobsDescription = $jobDescriptionMapper->fetchAllByCompanyId($currentCompany->id);
152
 
977 geraldo 153
                $this->layout()->setTemplate('layout/layout-backend');
154
                $viewModel = new ViewModel();
155
                $viewModel->setTemplate('leaders-linked/performance-evaluation-forms/index.phtml');
156
                $viewModel->setVariable('form', $form);
1051 geraldo 157
                $viewModel->setVariable('jobsDescription', $jobsDescription);
977 geraldo 158
                return $viewModel;
159
            }
160
        } else {
161
            return new JsonModel([
162
                'success' => false,
163
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
164
            ]);
165
            ;
166
        }
167
    }
168
 
169
    public function addAction() {
170
        $request = $this->getRequest();
171
        $currentUserPlugin = $this->plugin('currentUserPlugin');
172
        $currentCompany = $currentUserPlugin->getCompany();
173
        $currentUser = $currentUserPlugin->getUser();
174
 
175
        $request = $this->getRequest();
176
 
177
 
178
        if ($request->isPost()) {
1064 geraldo 179
            $form = new CompanyPerformanceEvaluationFormForm($this->adapter, $currentCompany->id);
977 geraldo 180
            $dataPost = $request->getPost()->toArray();
1059 geraldo 181
 
182
 
977 geraldo 183
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
184
 
185
            $form->setData($dataPost);
186
 
187
            if ($form->isValid()) {
1058 geraldo 188
 
189
 
977 geraldo 190
                $dataPost = (array) $form->getData();
191
 
192
                $hydrator = new ObjectPropertyHydrator();
193
                $companyPerformanceEvaluation = new CompanyPerformanceEvaluationForm();
194
                $hydrator->hydrate($dataPost, $companyPerformanceEvaluation);
195
 
196
                if (!$companyPerformanceEvaluation->status) {
197
                    $companyPerformanceEvaluation->status = CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
198
                }
199
                $companyPerformanceEvaluation->company_id = $currentCompany->id;
1075 geraldo 200
 
201
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
202
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
203
                $companyPerformanceEvaluation->job_description_id = $jobDescription->id;
204
 
977 geraldo 205
                $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
1076 geraldo 206
 
977 geraldo 207
                $result = $companyPerformanceEvaluationMapper->insert($companyPerformanceEvaluation);
208
 
1098 geraldo 209
 
977 geraldo 210
                if ($result) {
211
                    $this->logger->info('Se agrego el tamaño de empresa ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
212
 
213
                    // Get record by id
214
                    $record = $companyPerformanceEvaluationMapper->fetchOne($companyPerformanceEvaluation->id);
215
 
216
                    if ($record) {
1098 geraldo 217
 
977 geraldo 218
                        $data = [
219
                            'success' => true,
220
                            'id' => $record->id,
221
                            'action_edit' => $this->url()->fromRoute('performance-evaluation/forms/edit', ['id' => $record->uuid]),
222
                            'data' => 'LABEL_RECORD_ADDED'
223
                        ];
224
                    } else {
1098 geraldo 225
 
977 geraldo 226
                        $data = [
227
                            'success' => false,
228
                            'data' => 'ERROR_RECORD_NOT_FOUND'
229
                        ];
230
                    }
231
                } else {
232
                    $data = [
233
                        'success' => false,
234
                        'data' => $companyPerformanceEvaluationMapper->getError()
235
                    ];
236
                }
237
 
238
                return new JsonModel($data);
239
            } else {
240
                $messages = [];
241
                $form_messages = (array) $form->getMessages();
242
                foreach ($form_messages as $fieldname => $field_messages) {
243
 
244
                    $messages[$fieldname] = array_values($field_messages);
245
                }
246
 
247
                return new JsonModel([
248
                    'success' => false,
249
                    'data' => $messages
250
                ]);
251
            }
252
        } else {
253
            $data = [
254
                'success' => false,
255
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
256
            ];
257
 
258
            return new JsonModel($data);
259
        }
260
 
261
        return new JsonModel($data);
262
    }
263
 
264
    public function editAction() {
265
        $request = $this->getRequest();
266
        $currentUserPlugin = $this->plugin('currentUserPlugin');
267
        $currentCompany = $currentUserPlugin->getCompany();
268
        $currentUser = $currentUserPlugin->getUser();
269
 
270
        $request = $this->getRequest();
271
        $uuid = $this->params()->fromRoute('id');
272
 
273
 
274
        if (!$uuid) {
275
            $data = [
276
                'success' => false,
277
                'data' => 'ERROR_INVALID_PARAMETER'
278
            ];
279
 
280
            return new JsonModel($data);
281
        }
282
 
283
        $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
284
        $companyPerformanceEvaluation = $companyPerformanceEvaluationMapper->fetchOneByUuid($uuid);
285
        if (!$companyPerformanceEvaluation) {
286
            $data = [
287
                'success' => false,
288
                'data' => 'ERROR_RECORD_NOT_FOUND'
289
            ];
290
 
291
            return new JsonModel($data);
292
        }
293
 
294
        if ($companyPerformanceEvaluation->company_id != $currentCompany->id) {
295
            return new JsonModel([
296
                'success' => false,
297
                'data' => 'ERROR_UNAUTHORIZED'
298
            ]);
299
        }
300
 
301
 
302
        if ($request->isPost()) {
1064 geraldo 303
            $form = new CompanyPerformanceEvaluationFormForm($this->adapter, $currentCompany->id);
977 geraldo 304
            $dataPost = $request->getPost()->toArray();
305
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
306
 
307
            $form->setData($dataPost);
308
 
309
            if ($form->isValid()) {
310
                $dataPost = (array) $form->getData();
311
 
312
                $hydrator = new ObjectPropertyHydrator();
313
                $hydrator->hydrate($dataPost, $companyPerformanceEvaluation);
314
 
315
                if (!$companyPerformanceEvaluation->status) {
316
                    $companyPerformanceEvaluation->status = CompanyPerformanceEvaluationForm::STATUS_INACTIVE;
317
                }
318
 
1080 geraldo 319
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
320
                $jobDescription = $jobDescriptionMapper->fetchOneByUuid($dataPost['job_description_id']);
321
                $companyPerformanceEvaluation->job_description_id = $jobDescription->id;
322
 
977 geraldo 323
                $result = $companyPerformanceEvaluationMapper->update($companyPerformanceEvaluation);
324
 
325
                if ($result) {
326
                    $this->logger->info('Se actualizo el tamaño de empresa ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
327
                    $data = [
328
                        'success' => true,
329
                        'id' => $companyPerformanceEvaluation->id,
330
                        'action_edit' => $this->url()->fromRoute('performance-evaluation/forms/edit', ['id' => $companyPerformanceEvaluation->uuid]),
331
                        'data' => 'LABEL_RECORD_UPDATED'
332
                    ];
333
                } else {
334
                    $data = [
335
                        'success' => false,
336
                        'data' => $companyPerformanceEvaluationMapper->getError()
337
                    ];
338
                }
339
 
340
                return new JsonModel($data);
341
            } else {
342
                $messages = [];
343
                $form_messages = (array) $form->getMessages();
344
                foreach ($form_messages as $fieldname => $field_messages) {
345
                    $messages[$fieldname] = array_values($field_messages);
346
                }
347
 
348
                return new JsonModel([
349
                    'success' => false,
350
                    'data' => $messages
351
                ]);
352
            }
353
        } else if ($request->isGet()) {
354
            $hydrator = new ObjectPropertyHydrator();
355
 
1080 geraldo 356
            $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
357
            $jobDescription = $jobDescriptionMapper->fetchOne($companyPerformanceEvaluation->job_description_id);
1098 geraldo 358
            if (!$jobDescription) {
1080 geraldo 359
                $data = [
360
                    'success' => false,
361
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
362
                ];
1098 geraldo 363
 
1080 geraldo 364
                return new JsonModel($data);
365
            }
366
 
977 geraldo 367
            $data = [
368
                'success' => true,
369
                'data' => [
370
                    'id' => $companyPerformanceEvaluation->uuid,
371
                    'name' => $companyPerformanceEvaluation->name,
1080 geraldo 372
                    'job_description_id' => $jobDescription->uuid,
977 geraldo 373
                    'status' => $companyPerformanceEvaluation->status,
1089 geraldo 374
                    'description' => $companyPerformanceEvaluation->description,
375
                    'text' => $companyPerformanceEvaluation->text,
977 geraldo 376
                    'content' => $companyPerformanceEvaluation->content ? json_decode($companyPerformanceEvaluation->content) : [],
377
                ]
378
            ];
379
 
380
            return new JsonModel($data);
381
        } else {
382
            $data = [
383
                'success' => false,
384
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
385
            ];
386
 
387
            return new JsonModel($data);
388
        }
389
 
390
        return new JsonModel($data);
391
    }
392
 
393
    public function deleteAction() {
394
        $request = $this->getRequest();
395
        $currentUserPlugin = $this->plugin('currentUserPlugin');
396
        $currentCompany = $currentUserPlugin->getCompany();
397
        $currentUser = $currentUserPlugin->getUser();
398
 
399
        $request = $this->getRequest();
400
        $uuid = $this->params()->fromRoute('id');
401
 
402
        if (!$uuid) {
403
            $data = [
404
                'success' => false,
405
                'data' => 'ERROR_INVALID_PARAMETER'
406
            ];
407
 
408
            return new JsonModel($data);
409
        }
410
 
411
        $companyPerformanceEvaluationMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
412
        $companyPerformanceEvaluation = $companyPerformanceEvaluationMapper->fetchOneByUuid($uuid);
413
        if (!$companyPerformanceEvaluation) {
414
            $data = [
415
                'success' => false,
416
                'data' => 'ERROR_RECORD_NOT_FOUND'
417
            ];
418
 
419
            return new JsonModel($data);
420
        }
421
 
422
        if ($companyPerformanceEvaluation->company_id != $currentCompany->id) {
423
            return new JsonModel([
424
                'success' => false,
425
                'data' => 'ERROR_UNAUTHORIZED'
426
            ]);
427
        }
428
 
429
        if ($request->isPost()) {
430
 
431
            //Falta borrar los test  primeramente
432
            $companyPerformanceEvaluationFormUserMapper = CompanyPerformanceEvaluationFormUserMapper::getInstance($this->adapter);
433
            $companyPerformanceEvaluationFormUserMapper->deleteAllByFormId($companyPerformanceEvaluation->id);
434
 
435
 
436
            $result = $companyPerformanceEvaluationMapper->delete($companyPerformanceEvaluation->id);
437
            if ($result) {
438
                $this->logger->info('Se borro el formulario de evaluación de desempeño ' . $companyPerformanceEvaluation->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
439
 
440
                $data = [
441
                    'success' => true,
442
                    'data' => 'LABEL_RECORD_DELETED'
443
                ];
444
            } else {
445
 
446
                $data = [
447
                    'success' => false,
448
                    'data' => $companyPerformanceEvaluationMapper->getError()
449
                ];
450
 
451
                return new JsonModel($data);
452
            }
453
        } else {
454
            $data = [
455
                'success' => false,
456
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
457
            ];
458
 
459
            return new JsonModel($data);
460
        }
461
 
462
        return new JsonModel($data);
463
    }
464
 
1263 geraldo 465
 
1269 geraldo 466
    public function reportAction() {
1263 geraldo 467
        $currentUserPlugin = $this->plugin('currentUserPlugin');
468
        $currentUser = $currentUserPlugin->getUser();
469
 
470
        $uuid = $this->params()->fromRoute('id');
471
 
472
        $companyPerformanceEvaluationFormMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
473
        $companyPerformanceEvaluationForm = $companyPerformanceEvaluationFormMapper->fetchOneByUuid($uuid);
474
 
475
        if (!$companyPerformanceEvaluationForm) {
476
            return new JsonModel([
477
                'success' => false,
478
                'data' => 'ERROR_FORM_EVALUATION_NOT_FOUND'
479
            ]);
480
        }
481
 
482
        if ($companyPerformanceEvaluationForm->status == CompanyPerformanceEvaluationForm::STATUS_INACTIVE) {
483
            return new JsonModel([
484
                'success' => false,
485
                'data' => 'ERROR_FORM_EVALUATION_IS_INACTIVE'
486
            ]);
487
        }
488
 
489
 
490
        $companyPerformanceEvaluationFormUserMapper = CompanyPerformanceEvaluationFormUserMapper::getInstance($this->adapter);
491
        $companyPerformanceEvaluationFormUser = $companyPerformanceEvaluationFormUserMapper->fetchOneByFormIdAndUserId($companyPerformanceEvaluationForm->id, $currentUser->id);
492
        if (!$companyPerformanceEvaluationFormUser) {
493
            return new JsonModel([
494
                'success' => false,
495
                'data' => 'ERROR_FORM_EVALUATION_YOU_CAN_NOT_TAKE'
496
            ]);
497
        }
498
 
499
 
500
        $companyPerformanceEvaluationTestMapper = CompanyPerformanceEvaluationTestMapper::getInstance($this->adapter);
501
        $companyPerformanceEvaluationTest = $companyPerformanceEvaluationTestMapper->fetchOneBy($companyPerformanceEvaluationForm->id, $currentUser->id);
502
 
503
        if ($companyPerformanceEvaluationTest && $companyPerformanceEvaluationTest->status != CompanyPerformanceEvaluationTest::STATUS_DRAFT) {
504
            return new JsonModel([
505
                'success' => false,
506
                'data' => 'ERROR_FORM_EVALUATION_ALREADY_YOUR_APPLICATION_IN_THIS_TEST'
507
            ]);
508
        }
509
 
510
        $request = $this->getRequest();
511
        if ($request->isGet()) {
512
 
513
 
514
 
515
            // set content
516
 
517
            $content = $companyPerformanceEvaluationTest && $companyPerformanceEvaluationTest->status == CompanyPerformanceEvaluationTest::STATUS_DRAFT ?
518
                    json_decode($companyPerformanceEvaluationTest->content, true) :
519
                    json_decode($companyPerformanceEvaluationForm->content, true);
520
 
521
 
522
            //Competencies
523
 
524
            $jobDescriptionCompetencyMapper = JobDescriptionCompetencyMapper::getInstance($this->adapter);
525
            $competencyMapper = CompetencyMapper::getInstance($this->adapter);
526
            $competenceTypeMapper = CompetencyTypeMapper::getInstance($this->adapter);
527
            $behaviorCompetencyMapper = BehaviorCompetencyMapper::getInstance($this->adapter);
528
            $jobDescriptionBehaviorCompetencyMapper = JobDescriptionBehaviorCompetencyMapper::getInstance($this->adapter);
529
            $behaviorMapper = BehaviorsMapper::getInstance($this->adapter);
530
 
531
            $competencies = [];
532
 
533
            $jobDescriptionCompetency = $jobDescriptionCompetencyMapper->fetchByJobDescriptionId($companyPerformanceEvaluationForm->job_description_id);
534
 
535
            foreach ($jobDescriptionCompetency as $record) {
536
 
537
 
538
                $competency = $competencyMapper->fetchOne($record->competency_id);
539
                $competenceType = $competenceTypeMapper->fetchOne($competency->competency_type_id);
540
 
541
                if($competency && $competenceType){
542
 
543
                    $behaviorCompetencies = $behaviorCompetencyMapper->fetchByCompetencyId($competency->id);
544
                    $behaviors = [];
545
 
546
                    foreach ($behaviorCompetencies as $rows) {
547
 
548
                        $behavior = $behaviorMapper->fetchOne($rows->behavior_id);
549
                        $jobDescriptionBehaviorCompetency = $jobDescriptionBehaviorCompetencyMapper->fetchOneByBehavior($companyPerformanceEvaluationForm->job_description_id, $record->competency_id, $rows->behavior_id);
550
 
551
                        if ($behavior && $jobDescriptionBehaviorCompetency) {
552
 
553
                            array_push($behaviors, [
554
                                'id_section' => $competency->uuid,
555
                                'id_option' => $behavior->uuid,
556
                                'title' => $behavior->description,
557
                                'level' => $jobDescriptionBehaviorCompetency->level,
558
                                'answer' => ''
559
                            ]);
560
                        }
561
                    }
562
 
563
                    array_push($competencies, [
564
                        'id_section' => $competency->uuid,
565
                        'title' => $competenceType->name. '  '.$competency->name,
566
                        'text' => $competency->description,
567
                        'type'=>'competency',
568
                        'options' => $behaviors
569
                    ]);
570
 
571
                }
572
 
573
            }
574
 
575
            return new JsonModel([
576
                'success' => true,
577
                'data' => [
578
                    'name' => $companyPerformanceEvaluationForm->name,
579
                    'description' => $companyPerformanceEvaluationForm->description,
580
                    'text' => $companyPerformanceEvaluationForm->text,
581
                    'competencies'=>$competencies,
582
                    'content' => $content,
583
                ]
584
            ]);
585
        }
586
 
587
        if ($request->isPost()) {
588
            $form = new PerformanceEvaluationTestForm();
589
            $dataPost = $request->getPost()->toArray();
590
 
591
            $form->setData($dataPost);
592
 
593
            if ($form->isValid()) {
594
 
595
 
596
                $dataPost = (array) $form->getData();
597
 
598
                $performanceEvaluationTest = new CompanyPerformanceEvaluationTest();
599
                $performanceEvaluationTest->company_id = $companyPerformanceEvaluationForm->company_id;
600
                $performanceEvaluationTest->form_id = $companyPerformanceEvaluationForm->id;
601
                $performanceEvaluationTest->user_id = $currentUser->id;
602
                $performanceEvaluationTest->status = $dataPost['status'];
603
                $performanceEvaluationTest->content = $dataPost['content'];
604
 
605
 
606
                //Check if the form is already registered
607
                $companyPerformanceEvaluationTest = $companyPerformanceEvaluationTestMapper->fetchOneBy($companyPerformanceEvaluationForm->id, $currentUser->id);
608
 
609
 
610
                $result = $companyPerformanceEvaluationTest ?
611
                        $companyPerformanceEvaluationTestMapper->update($performanceEvaluationTest, $companyPerformanceEvaluationTest->id) :
612
                        $companyPerformanceEvaluationTestMapper->insert($performanceEvaluationTest);
613
 
614
 
615
                if ($result) {
616
                    $this->logger->info('Se agrego un nuevo test de auto-evaluación : ' . $companyPerformanceEvaluationForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
617
 
618
                    $data = [
619
                        'success' => true,
620
                        'data' => $companyPerformanceEvaluationTest ? 'LABEL_RECORD_UPDATED' : 'LABEL_RECORD_ADDED'
621
                    ];
622
                } else {
623
                    $data = [
624
                        'success' => false,
625
                        'data' => $companyPerformanceEvaluationTestMapper->getError()
626
                    ];
627
                }
628
 
629
                return new JsonModel($data);
630
            } else {
631
                $messages = [];
632
                $form_messages = (array) $form->getMessages();
633
                foreach ($form_messages as $fieldname => $field_messages) {
634
 
635
                    $messages[$fieldname] = array_values($field_messages);
636
                }
637
 
638
                return new JsonModel([
639
                    'success' => false,
640
                    'data' => $messages
641
                ]);
642
            }
643
        }
644
 
645
        return new JsonModel([
646
            'success' => false,
647
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
648
        ]);
649
    }
650
 
977 geraldo 651
}