Proyectos de Subversion LeadersLinked - Backend

Rev

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