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