Proyectos de Subversion LeadersLinked - Backend

Rev

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