Proyectos de Subversion LeadersLinked - Backend

Rev

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