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