Proyectos de Subversion LeadersLinked - Backend

Rev

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