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