Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
553 geraldo 1
<?php
2
 
3
/**
4
 *
5
 * Controlador: Autoevaluación
6
 *
7
 */
8
declare(strict_types=1);
9
 
10
namespace LeadersLinked\Controller;
11
 
12
use Laminas\Db\Adapter\AdapterInterface;
13
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
14
use Laminas\Mvc\Controller\AbstractActionController;
15
use Laminas\Log\LoggerInterface;
16
use Laminas\View\Model\ViewModel;
17
use Laminas\View\Model\JsonModel;
18
use LeadersLinked\Model\Notification;
19
use LeadersLinked\Mapper\CompanyPerformanceEvaluationTestMapper;
20
use LeadersLinked\Mapper\NotificationMapper;
21
use LeadersLinked\Mapper\QueryMapper;
22
use LeadersLinked\Model\CompanyPerformanceEvaluationForm;
23
use LeadersLinked\Mapper\CompanyPerformanceEvaluationFormMapper;
24
use LeadersLinked\Model\CompanyPerformanceEvaluationTest;
25
use Laminas\Db\Sql\Select;
26
use LeadersLinked\Form\PerformanceEvaluation\PerformanceEvaluationTestForm;
617 geraldo 27
use LeadersLinked\Mapper\CompetencyMapper;
561 geraldo 28
use LeadersLinked\Mapper\JobDescriptionMapper;
617 geraldo 29
use LeadersLinked\Mapper\JobDescriptionCompetencyMapper;
621 geraldo 30
use LeadersLinked\Mapper\CompetencyTypeMapper;
617 geraldo 31
use LeadersLinked\Mapper\JobDescriptionSubordinateMapper;
32
use LeadersLinked\Model\JobDescriptionCompetency;
33
use LeadersLinked\Model\JobDescriptionBehaviorCompetency;
34
use LeadersLinked\Mapper\JobDescriptionBehaviorCompetencyMapper;
35
use LeadersLinked\Model\JobDescriptionSubordinate;
36
use LeadersLinked\Mapper\BehaviorCompetencyMapper;
37
use LeadersLinked\Model\BehaviorCompetency;
630 efrain 38
use LeadersLinked\Mapper\BehaviorMapper;
553 geraldo 39
use LeadersLinked\Library\Functions;
3265 efrain 40
use LeadersLinked\Model\CompanyPerformanceEvaluationTestUser;
41
use LeadersLinked\Mapper\CompetencyBehaviorMapper;
553 geraldo 42
 
43
class PerformanceEvaluationController extends AbstractActionController {
44
 
45
    /**
46
     *
47
     * @var AdapterInterface
48
     */
49
    private $adapter;
50
 
51
    /**
52
     *
53
     * @var AbstractAdapter
54
     */
55
    private $cache;
56
 
57
    /**
58
     *
59
     * @var  LoggerInterface
60
     */
61
    private $logger;
62
 
63
    /**
64
     *
65
     * @var array
66
     */
67
    private $config;
68
 
69
    /**
70
     *
71
     * @param AdapterInterface $adapter
72
     * @param AbstractAdapter $cache
73
     * @param LoggerInterface $logger
74
     * @param array $config
75
     */
76
    public function __construct($adapter, $cache, $logger, $config) {
77
        $this->adapter = $adapter;
78
        $this->cache = $cache;
79
        $this->logger = $logger;
80
        $this->config = $config;
81
    }
82
 
83
    /**
84
     *
85
     * Generación del listado de evaluaciones
86
     * {@inheritDoc}
87
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
88
     */
89
    public function indexAction() {
90
        $currentUserPlugin = $this->plugin('currentUserPlugin');
91
        $currentUser = $currentUserPlugin->getUser();
92
 
93
        $request = $this->getRequest();
561 geraldo 94
 
553 geraldo 95
        if ($request->isGet()) {
561 geraldo 96
 
553 geraldo 97
            $headers = $request->getHeaders();
98
            $isJson = false;
99
            if ($headers->has('Accept')) {
100
                $accept = $headers->get('Accept');
101
 
102
                $prioritized = $accept->getPrioritized();
103
 
104
                foreach ($prioritized as $key => $value) {
105
                    $raw = trim($value->getRaw());
106
 
107
                    if (!$isJson) {
108
                        $isJson = strpos($raw, 'json');
109
                    }
110
                }
111
            }
112
 
113
            if ($isJson) {
114
                $search = trim(filter_var($this->params()->fromQuery('search', ''), FILTER_SANITIZE_STRING));
115
 
116
 
117
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
556 geraldo 118
                $allowTakeATest = $acl->isAllowed($currentUser->usertype_id, 'profile/performance-evaluation/take-a-test');
119
                $allowReport = $acl->isAllowed($currentUser->usertype_id, 'profile/performance-evaluation/report');
553 geraldo 120
 
121
                $queryMapper = QueryMapper::getInstance($this->adapter);
122
 
123
                $select = $queryMapper->getSql()->select();
556 geraldo 124
                $select->columns(['uuid', 'name', 'description', 'text', 'job_description_id']);
553 geraldo 125
                $select->from(['f' => CompanyPerformanceEvaluationFormMapper::_TABLE]);
3265 efrain 126
                $select->join(['fu' => CompanyPerformanceEvaluationTestUser::_TABLE], 'f.id = fu.form_id', []);
553 geraldo 127
                $select->join(['t' => CompanyPerformanceEvaluationTestMapper::_TABLE], 'fu.form_id = t.form_id AND fu.user_id = t.user_id', ['status'], Select::JOIN_LEFT_OUTER);
128
                $select->where->equalTo('f.status', CompanyPerformanceEvaluationForm::STATUS_ACTIVE);
129
                $select->where->equalTo('fu.user_id', $currentUser->id);
130
 
131
 
132
                if ($search) {
133
                    $select->where->NEST->like('name', '%' . $search . '%');
134
                }
556 geraldo 135
                $select->order('name ASC, job_description_id ASC');
553 geraldo 136
 
137
                $records = $queryMapper->fetchAll($select);
138
                $items = [];
561 geraldo 139
 
140
                $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
141
 
553 geraldo 142
                foreach ($records as $record) {
143
 
561 geraldo 144
 
553 geraldo 145
                    switch ($record['status']) {
146
 
147
                        case CompanyPerformanceEvaluationTest::STATUS_DRAFT :
148
                            $status = 'LABEL_DRAFT';
149
                            break;
150
 
151
                        case CompanyPerformanceEvaluationTest::STATUS_COMPLETED :
152
                            $status = 'LABEL_COMPLETED';
153
                            break;
154
 
155
                        case CompanyPerformanceEvaluationTest::STATUS_PENDING :
156
                            $status = 'LABEL_PENDING';
157
                            break;
158
 
159
                        case CompanyPerformanceEvaluationTest::STATUS_REVIEW :
160
                            $status = 'LABEL_REVIEW';
161
                            break;
162
 
163
 
164
                        default :
165
                            $status = 'LABEL_AVAILABLE';
166
                            break;
167
                    }
168
 
563 geraldo 169
                    $jobDescription = $jobDescriptionMapper->fetchOne($record['job_description_id']);
561 geraldo 170
                    if ($jobDescription) {
171
                        $item = [
172
                            'name' => $record['name'],
173
                            'description' => $record['description'],
174
                            'text' => $record['text'],
175
                            'job_description' => $jobDescription->name,
176
                            'status' => $status,
582 geraldo 177
                            'link_take_a_test' => $allowTakeATest && ( empty($record['status']) || $record['status'] == CompanyPerformanceEvaluationTest::STATUS_DRAFT) ? $this->url()->fromRoute('profile/performance-evaluation/take-a-test', ['id' => $record['uuid']]) : '',
178
                            'link_report' => $allowReport && $record['status'] == CompanyPerformanceEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('profile/performance-evaluation/report', ['id' => $record['uuid']]) : '',
561 geraldo 179
                        ];
553 geraldo 180
 
561 geraldo 181
                        array_push($items, $item);
182
                    }
553 geraldo 183
                }
184
 
185
                $response = [
186
                    'success' => true,
187
                    'data' => $items
188
                ];
189
 
190
                return new JsonModel($response);
191
            } else {
192
 
193
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
194
                $notificationMapper->markAllNotificationsAsReadByTypeAndUserId(Notification::TYPE_ACCEPT_MY_REQUEST_CONNECTION, $currentUser->id);
195
 
196
                $this->layout()->setTemplate('layout/layout.phtml');
197
                $viewModel = new ViewModel();
556 geraldo 198
                $viewModel->setTemplate('leaders-linked/performance-evaluation/index.phtml');
553 geraldo 199
                return $viewModel;
200
            }
201
        } else {
202
            return new JsonModel([
203
                'success' => false,
204
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
205
            ]);
206
        }
207
    }
208
 
209
    public function takeaTestAction() {
571 geraldo 210
        $currentUserPlugin = $this->plugin('currentUserPlugin');
211
        $currentUser = $currentUserPlugin->getUser();
212
 
213
        $uuid = $this->params()->fromRoute('id');
214
 
215
        $companyPerformanceEvaluationFormMapper = CompanyPerformanceEvaluationFormMapper::getInstance($this->adapter);
216
        $companyPerformanceEvaluationForm = $companyPerformanceEvaluationFormMapper->fetchOneByUuid($uuid);
217
 
218
        if (!$companyPerformanceEvaluationForm) {
219
            return new JsonModel([
220
                'success' => false,
221
                'data' => 'ERROR_FORM_EVALUATION_NOT_FOUND'
222
            ]);
223
        }
224
 
225
        if ($companyPerformanceEvaluationForm->status == CompanyPerformanceEvaluationForm::STATUS_INACTIVE) {
226
            return new JsonModel([
227
                'success' => false,
228
                'data' => 'ERROR_FORM_EVALUATION_IS_INACTIVE'
229
            ]);
230
        }
231
 
232
 
233
        $companyPerformanceEvaluationFormUserMapper = CompanyPerformanceEvaluationFormUserMapper::getInstance($this->adapter);
234
        $companyPerformanceEvaluationFormUser = $companyPerformanceEvaluationFormUserMapper->fetchOneByFormIdAndUserId($companyPerformanceEvaluationForm->id, $currentUser->id);
235
        if (!$companyPerformanceEvaluationFormUser) {
236
            return new JsonModel([
237
                'success' => false,
238
                'data' => 'ERROR_FORM_EVALUATION_YOU_CAN_NOT_TAKE'
239
            ]);
240
        }
241
 
242
 
243
        $companyPerformanceEvaluationTestMapper = CompanyPerformanceEvaluationTestMapper::getInstance($this->adapter);
244
        $companyPerformanceEvaluationTest = $companyPerformanceEvaluationTestMapper->fetchOneBy($companyPerformanceEvaluationForm->id, $currentUser->id);
245
 
246
        if ($companyPerformanceEvaluationTest && $companyPerformanceEvaluationTest->status != CompanyPerformanceEvaluationTest::STATUS_DRAFT) {
247
            return new JsonModel([
248
                'success' => false,
249
                'data' => 'ERROR_FORM_EVALUATION_ALREADY_YOUR_APPLICATION_IN_THIS_TEST'
250
            ]);
251
        }
252
 
253
        $request = $this->getRequest();
254
        if ($request->isGet()) {
617 geraldo 255
 
628 geraldo 256
 
257
 
258
            // set content
259
 
260
            $content = $companyPerformanceEvaluationTest && $companyPerformanceEvaluationTest->status == CompanyPerformanceEvaluationTest::STATUS_DRAFT ?
629 geraldo 261
                    json_decode($companyPerformanceEvaluationTest->content, true) :
262
                    json_decode($companyPerformanceEvaluationForm->content, true);
628 geraldo 263
 
625 geraldo 264
 
617 geraldo 265
            //Competencies
266
 
267
            $jobDescriptionCompetencyMapper = JobDescriptionCompetencyMapper::getInstance($this->adapter);
268
            $competencyMapper = CompetencyMapper::getInstance($this->adapter);
269
            $competenceTypeMapper = CompetencyTypeMapper::getInstance($this->adapter);
3265 efrain 270
            $competencyBehaviorMapper = CompetencyBehaviorMapper::getInstance($this->adapter);
617 geraldo 271
            $jobDescriptionBehaviorCompetencyMapper = JobDescriptionBehaviorCompetencyMapper::getInstance($this->adapter);
630 efrain 272
            $behaviorMapper = BehaviorMapper::getInstance($this->adapter);
617 geraldo 273
 
274
            $competencies = [];
275
 
622 geraldo 276
            $jobDescriptionCompetency = $jobDescriptionCompetencyMapper->fetchByJobDescriptionId($companyPerformanceEvaluationForm->job_description_id);
617 geraldo 277
 
278
            foreach ($jobDescriptionCompetency as $record) {
279
 
625 geraldo 280
 
619 geraldo 281
                $competency = $competencyMapper->fetchOne($record->competency_id);
282
                $competenceType = $competenceTypeMapper->fetchOne($competency->competency_type_id);
617 geraldo 283
 
619 geraldo 284
                if($competency && $competenceType){
285
 
3265 efrain 286
                    $behaviorCompetencies = $competencyBehaviorMapper->fetchAllByCompetencyId($competency->id);
624 geraldo 287
                    $behaviors = [];
288
 
289
                    foreach ($behaviorCompetencies as $rows) {
290
 
291
                        $behavior = $behaviorMapper->fetchOne($rows->behavior_id);
292
                        $jobDescriptionBehaviorCompetency = $jobDescriptionBehaviorCompetencyMapper->fetchOneByBehavior($companyPerformanceEvaluationForm->job_description_id, $record->competency_id, $rows->behavior_id);
293
 
294
                        if ($behavior && $jobDescriptionBehaviorCompetency) {
295
 
296
                            array_push($behaviors, [
628 geraldo 297
                                'id_section' => $competency->uuid,
298
                                'id_option' => $behavior->uuid,
299
                                'title' => $behavior->description,
625 geraldo 300
                                'level' => $jobDescriptionBehaviorCompetency->level,
301
                                'answer' => ''
624 geraldo 302
                            ]);
303
                        }
623 geraldo 304
                    }
624 geraldo 305
 
619 geraldo 306
                    array_push($competencies, [
628 geraldo 307
                        'id_section' => $competency->uuid,
308
                        'title' => $competenceType->name. '  '.$competency->name,
309
                        'text' => $competency->description,
310
                        'type'=>'competency',
311
                        'options' => $behaviors
619 geraldo 312
                    ]);
313
 
314
                }
315
 
617 geraldo 316
            }
317
 
571 geraldo 318
            return new JsonModel([
585 geraldo 319
                'success' => true,
571 geraldo 320
                'data' => [
321
                    'name' => $companyPerformanceEvaluationForm->name,
322
                    'description' => $companyPerformanceEvaluationForm->description,
323
                    'text' => $companyPerformanceEvaluationForm->text,
618 geraldo 324
                    'competencies'=>$competencies,
629 geraldo 325
                    'content' => array_push($content, $competencies),
571 geraldo 326
                ]
327
            ]);
328
        }
329
 
330
        if ($request->isPost()) {
331
            $form = new PerformanceEvaluationTestForm();
332
            $dataPost = $request->getPost()->toArray();
333
 
334
            $form->setData($dataPost);
335
 
336
            if ($form->isValid()) {
594 geraldo 337
 
338
 
571 geraldo 339
                $dataPost = (array) $form->getData();
340
 
594 geraldo 341
                $performanceEvaluationTest = new CompanyPerformanceEvaluationTest();
342
                $performanceEvaluationTest->company_id = $companyPerformanceEvaluationForm->company_id;
343
                $performanceEvaluationTest->form_id = $companyPerformanceEvaluationForm->id;
344
                $performanceEvaluationTest->user_id = $currentUser->id;
345
                $performanceEvaluationTest->status = $dataPost['status'];
346
                $performanceEvaluationTest->content = $dataPost['content'];
571 geraldo 347
 
348
 
349
                //Check if the form is already registered
350
                $companyPerformanceEvaluationTest = $companyPerformanceEvaluationTestMapper->fetchOneBy($companyPerformanceEvaluationForm->id, $currentUser->id);
351
 
596 geraldo 352
 
571 geraldo 353
                $result = $companyPerformanceEvaluationTest ?
594 geraldo 354
                        $companyPerformanceEvaluationTestMapper->update($performanceEvaluationTest, $companyPerformanceEvaluationTest->id) :
355
                        $companyPerformanceEvaluationTestMapper->insert($performanceEvaluationTest);
571 geraldo 356
 
357
 
358
                if ($result) {
359
                    $this->logger->info('Se agrego un nuevo test de auto-evaluación : ' . $companyPerformanceEvaluationForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
360
 
361
                    $data = [
362
                        'success' => true,
363
                        'data' => $companyPerformanceEvaluationTest ? 'LABEL_RECORD_UPDATED' : 'LABEL_RECORD_ADDED'
364
                    ];
365
                } else {
366
                    $data = [
367
                        'success' => false,
368
                        'data' => $companyPerformanceEvaluationTestMapper->getError()
369
                    ];
370
                }
371
 
372
                return new JsonModel($data);
373
            } else {
374
                $messages = [];
375
                $form_messages = (array) $form->getMessages();
376
                foreach ($form_messages as $fieldname => $field_messages) {
377
 
378
                    $messages[$fieldname] = array_values($field_messages);
379
                }
380
 
381
                return new JsonModel([
382
                    'success' => false,
383
                    'data' => $messages
384
                ]);
385
            }
386
        }
387
 
553 geraldo 388
        return new JsonModel([
389
            'success' => false,
390
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
391
        ]);
571 geraldo 392
    }
570 geraldo 393
 
553 geraldo 394
}