Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 493 | Rev 504 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
279 efrain 1
<?php
431 geraldo 2
 
279 efrain 3
/**
4
 *
291 geraldo 5
 * Controlador: Autoevaluación
279 efrain 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\CompanySelfEvaluationTestMapper;
20
use LeadersLinked\Mapper\NotificationMapper;
21
use LeadersLinked\Mapper\QueryMapper;
22
use LeadersLinked\Model\CompanySelfEvaluationForm;
23
use LeadersLinked\Mapper\CompanySelfEvaluationFormMapper;
24
use LeadersLinked\Model\CompanySelfEvaluationTest;
25
use Laminas\Db\Sql\Select;
26
use LeadersLinked\Mapper\CompanySelfEvaluationFormUserMapper;
27
use LeadersLinked\Form\SelfEvaluation\SelfEvaluationTestForm;
28
use LeadersLinked\Library\Functions;
29
 
431 geraldo 30
class SelfEvaluationController extends AbstractActionController {
31
 
279 efrain 32
    /**
33
     *
34
     * @var AdapterInterface
35
     */
36
    private $adapter;
431 geraldo 37
 
279 efrain 38
    /**
39
     *
40
     * @var AbstractAdapter
41
     */
42
    private $cache;
431 geraldo 43
 
279 efrain 44
    /**
45
     *
46
     * @var  LoggerInterface
47
     */
48
    private $logger;
49
 
50
    /**
51
     *
52
     * @var array
53
     */
54
    private $config;
431 geraldo 55
 
279 efrain 56
    /**
57
     *
58
     * @param AdapterInterface $adapter
59
     * @param AbstractAdapter $cache
60
     * @param LoggerInterface $logger
61
     * @param array $config
62
     */
431 geraldo 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
    }
279 efrain 69
 
70
    /**
71
     *
291 geraldo 72
     * Generación del listado de evaluaciones
279 efrain 73
     * {@inheritDoc}
74
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
75
     */
431 geraldo 76
    public function indexAction() {
279 efrain 77
        $currentUserPlugin = $this->plugin('currentUserPlugin');
78
        $currentUser = $currentUserPlugin->getUser();
431 geraldo 79
 
279 efrain 80
        $request = $this->getRequest();
431 geraldo 81
        if ($request->isGet()) {
82
 
83
 
84
            $headers = $request->getHeaders();
279 efrain 85
            $isJson = false;
431 geraldo 86
            if ($headers->has('Accept')) {
279 efrain 87
                $accept = $headers->get('Accept');
431 geraldo 88
 
279 efrain 89
                $prioritized = $accept->getPrioritized();
431 geraldo 90
 
91
                foreach ($prioritized as $key => $value) {
279 efrain 92
                    $raw = trim($value->getRaw());
431 geraldo 93
 
94
                    if (!$isJson) {
279 efrain 95
                        $isJson = strpos($raw, 'json');
96
                    }
97
                }
98
            }
431 geraldo 99
 
100
            if ($isJson) {
279 efrain 101
                $search = trim(filter_var($this->params()->fromQuery('search', ''), FILTER_SANITIZE_STRING));
281 efrain 102
 
431 geraldo 103
 
281 efrain 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
 
279 efrain 109
 
431 geraldo 110
 
279 efrain 111
                $queryMapper = QueryMapper::getInstance($this->adapter);
431 geraldo 112
 
279 efrain 113
                $select = $queryMapper->getSql()->select();
431 geraldo 114
                $select->columns(['uuid', 'name', 'description', 'text', 'language']);
492 geraldo 115
                $select->from(['f' => CompanySelfEvaluationFormMapper::_TABLE]);
294 efrain 116
                $select->join(['fu' => CompanySelfEvaluationFormUserMapper::_TABLE], 'f.id = fu.form_id', []);
493 geraldo 117
                $select->join(['t' => CompanySelfEvaluationTestMapper::_TABLE], 'fu.form_id = t.form_id AND fu.user_id = t.user_id', ['status'], Select::JOIN_LEFT_OUTER);
279 efrain 118
                $select->where->equalTo('f.status', CompanySelfEvaluationForm::STATUS_ACTIVE);
296 efrain 119
                $select->where->equalTo('fu.user_id', $currentUser->id);
431 geraldo 120
 
121
 
122
                if ($search) {
279 efrain 123
                    $select->where->NEST->like('name', '%' . $search . '%');
124
                }
125
                $select->order('name ASC, language ASC');
431 geraldo 126
 
279 efrain 127
                $records = $queryMapper->fetchAll($select);
128
                $items = [];
431 geraldo 129
                foreach ($records as $record) {
130
                    switch ($record['language']) {
279 efrain 131
                        case CompanySelfEvaluationForm::LANGUAGE_ENGLISH :
132
                            $language = 'LABEL_ENGLISH';
133
                            break;
431 geraldo 134
 
135
                        case CompanySelfEvaluationForm::LANGUAGE_SPANISH :
279 efrain 136
                            $language = 'LABEL_SPANISH';
137
                            break;
431 geraldo 138
 
139
                        default :
279 efrain 140
                            $language = '';
141
                            break;
142
                    }
431 geraldo 143
 
144
                    switch ($record['status']) {
145
 
146
                        case CompanySelfEvaluationTest::STATUS_DRAFT :
147
                            $status = 'LABEL_DRAFT';
148
                            break;
149
 
150
                        case CompanySelfEvaluationTest::STATUS_COMPLETED :
279 efrain 151
                            $status = 'LABEL_COMPLETED';
152
                            break;
431 geraldo 153
 
279 efrain 154
                        case CompanySelfEvaluationTest::STATUS_PENDING :
155
                            $status = 'LABEL_PENDING';
156
                            break;
431 geraldo 157
 
279 efrain 158
                        case CompanySelfEvaluationTest::STATUS_REVIEW :
159
                            $status = 'LABEL_REVIEW';
160
                            break;
431 geraldo 161
 
162
 
163
                        default :
281 efrain 164
                            $status = 'LABEL_AVAILABLE';
279 efrain 165
                            break;
166
                    }
431 geraldo 167
 
168
 
279 efrain 169
                    $item = [
431 geraldo 170
                        'name' => $record['name'],
279 efrain 171
                        'description' => $record['description'],
431 geraldo 172
                        'text' => $record['text'],
279 efrain 173
                        'language' => $language,
174
                        'status' => $status,
471 geraldo 175
                        'link_take_a_test' => $allowTakeATest && ( empty($record['status']) || $record['status'] == CompanySelfEvaluationTest::STATUS_DRAFT) ? $this->url()->fromRoute('profile/self-evaluation/take-a-test', ['id' => $record['uuid']]) : '',
431 geraldo 176
                        'link_report' => $allowReport && $record['status'] == CompanySelfEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('profile/self-evaluation/report', ['id' => $record['uuid']]) : '',
279 efrain 177
                    ];
178
 
431 geraldo 179
 
180
 
279 efrain 181
                    array_push($items, $item);
182
                }
183
 
431 geraldo 184
 
185
 
186
 
279 efrain 187
                $response = [
188
                    'success' => true,
493 geraldo 189
                    'data' => $items
279 efrain 190
                ];
431 geraldo 191
 
279 efrain 192
                return new JsonModel($response);
193
            } else {
431 geraldo 194
 
279 efrain 195
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
196
                $notificationMapper->markAllNotificationsAsReadByTypeAndUserId(Notification::TYPE_ACCEPT_MY_REQUEST_CONNECTION, $currentUser->id);
431 geraldo 197
 
279 efrain 198
                $this->layout()->setTemplate('layout/layout.phtml');
199
                $viewModel = new ViewModel();
200
                $viewModel->setTemplate('leaders-linked/self-evaluation/index.phtml');
431 geraldo 201
                return $viewModel;
279 efrain 202
            }
203
        } else {
204
            return new JsonModel([
205
                'success' => false,
206
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
207
            ]);
208
        }
209
    }
431 geraldo 210
 
211
    public function takeaTestAction() {
279 efrain 212
        $currentUserPlugin = $this->plugin('currentUserPlugin');
213
        $currentUser = $currentUserPlugin->getUser();
431 geraldo 214
 
279 efrain 215
        $uuid = $this->params()->fromRoute('id');
431 geraldo 216
 
279 efrain 217
        $companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
431 geraldo 218
        $companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOneByUuid($uuid);
219
 
220
        if (!$companySelfEvaluationForm) {
279 efrain 221
            return new JsonModel([
222
                'success' => false,
223
                'data' => 'ERROR_FORM_SELF_EVALUATION_NOT_FOUND'
431 geraldo 224
            ]);
279 efrain 225
        }
226
 
431 geraldo 227
        if ($companySelfEvaluationForm->status == CompanySelfEvaluationForm::STATUS_INACTIVE) {
279 efrain 228
            return new JsonModel([
229
                'success' => false,
230
                'data' => 'ERROR_FORM_SELF_EVALUATION_IS_INACTIVE'
431 geraldo 231
            ]);
279 efrain 232
        }
431 geraldo 233
 
234
 
279 efrain 235
        $companySelfEvaluationFormUserMapper = CompanySelfEvaluationFormUserMapper::getInstance($this->adapter);
236
        $companySelfEvaluationFormUser = $companySelfEvaluationFormUserMapper->fetchOneByFormIdAndUserId($companySelfEvaluationForm->id, $currentUser->id);
431 geraldo 237
        if (!$companySelfEvaluationFormUser) {
279 efrain 238
            return new JsonModel([
239
                'success' => false,
240
                'data' => 'ERROR_FORM_SELF_EVALUATION_YOU_CAN_NOT_TAKE'
431 geraldo 241
            ]);
279 efrain 242
        }
431 geraldo 243
 
244
 
279 efrain 245
        $companySelfEvaluationTestMapper = CompanySelfEvaluationTestMapper::getInstance($this->adapter);
246
        $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneBy($companySelfEvaluationForm->id, $currentUser->id);
431 geraldo 247
 
248
        if ($companySelfEvaluationTest && $companySelfEvaluationTest->status != CompanySelfEvaluationTest::STATUS_DRAFT) {
279 efrain 249
            return new JsonModel([
250
                'success' => false,
251
                'data' => 'ERROR_FORM_SELF_EVALUATION_ALREADY_YOUR_APPLICATION_IN_THIS_TEST'
431 geraldo 252
            ]);
279 efrain 253
        }
431 geraldo 254
 
255
 
279 efrain 256
        $request = $this->getRequest();
431 geraldo 257
        if ($request->isGet()) {
279 efrain 258
            return new JsonModel([
259
                'success' => false,
441 geraldo 260
                'data' => [
279 efrain 261
                    'name' => $companySelfEvaluationForm->name,
262
                    'description' => $companySelfEvaluationForm->description,
263
                    'text' => $companySelfEvaluationForm->text,
441 geraldo 264
                    'content' => $companySelfEvaluationTest && $companySelfEvaluationTest->status == CompanySelfEvaluationTest::STATUS_DRAFT ?
265
                    json_decode($companySelfEvaluationTest->content) :
266
                    json_decode($companySelfEvaluationForm->content),
279 efrain 267
                ]
431 geraldo 268
            ]);
279 efrain 269
        }
431 geraldo 270
 
271
        if ($request->isPost()) {
272
            $form = new SelfEvaluationTestForm();
279 efrain 273
            $dataPost = $request->getPost()->toArray();
431 geraldo 274
 
279 efrain 275
            $form->setData($dataPost);
431 geraldo 276
 
277
            if ($form->isValid()) {
279 efrain 278
                $dataPost = (array) $form->getData();
431 geraldo 279
 
279 efrain 280
                $selfEvaluationTest = new CompanySelfEvaluationTest();
281
                $selfEvaluationTest->company_id = $companySelfEvaluationForm->company_id;
282
                $selfEvaluationTest->form_id = $companySelfEvaluationForm->id;
283
                $selfEvaluationTest->user_id = $currentUser->id;
431 geraldo 284
                $selfEvaluationTest->status = $dataPost['status'];
279 efrain 285
                $selfEvaluationTest->content = $dataPost['content'];
431 geraldo 286
 
287
 
288
                //Check if the form is already registered
289
                $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneBy($companySelfEvaluationForm->id, $currentUser->id);
290
 
291
                $result = $companySelfEvaluationTest ?
471 geraldo 292
                        $companySelfEvaluationTestMapper->update($selfEvaluationTest, $companySelfEvaluationTest->id) :
431 geraldo 293
                        $companySelfEvaluationTestMapper->insert($selfEvaluationTest);
294
 
295
 
296
                if ($result) {
279 efrain 297
                    $this->logger->info('Se agrego un nuevo test de auto-evaluación : ' . $companySelfEvaluationForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
431 geraldo 298
 
279 efrain 299
                    $data = [
431 geraldo 300
                        'success' => true,
448 geraldo 301
                        'data' => $companySelfEvaluationTest ? 'LABEL_RECORD_UPDATED' : 'LABEL_RECORD_ADDED'
279 efrain 302
                    ];
303
                } else {
304
                    $data = [
431 geraldo 305
                        'success' => false,
306
                        'data' => $companySelfEvaluationTestMapper->getError()
279 efrain 307
                    ];
308
                }
431 geraldo 309
 
279 efrain 310
                return new JsonModel($data);
311
            } else {
312
                $messages = [];
313
                $form_messages = (array) $form->getMessages();
431 geraldo 314
                foreach ($form_messages as $fieldname => $field_messages) {
315
 
279 efrain 316
                    $messages[$fieldname] = array_values($field_messages);
317
                }
431 geraldo 318
 
279 efrain 319
                return new JsonModel([
431 geraldo 320
                    'success' => false,
321
                    'data' => $messages
279 efrain 322
                ]);
323
            }
324
        }
431 geraldo 325
 
279 efrain 326
        return new JsonModel([
327
            'success' => false,
328
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
329
        ]);
330
    }
431 geraldo 331
 
332
    public function reportAction() {
484 geraldo 333
 
487 geraldo 334
        $currentUserPlugin = $this->plugin('currentUserPlugin');
335
        $currentUser = $currentUserPlugin->getUser();
336
 
337
        $uuid = $this->params()->fromRoute('id');
338
 
493 geraldo 339
        $companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
340
        $companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOneByUuid($uuid);
341
 
342
        if (!$companySelfEvaluationForm) {
343
            return new JsonModel([
344
                'success' => false,
345
                'data' => 'ERROR_FORM_SELF_EVALUATION_NOT_FOUND'
346
            ]);
347
        }
348
 
349
        if ($companySelfEvaluationForm->status == CompanySelfEvaluationForm::STATUS_INACTIVE) {
350
            return new JsonModel([
351
                'success' => false,
352
                'data' => 'ERROR_FORM_SELF_EVALUATION_IS_INACTIVE'
353
            ]);
354
        }
355
 
356
 
357
        $companySelfEvaluationFormUserMapper = CompanySelfEvaluationFormUserMapper::getInstance($this->adapter);
358
        $companySelfEvaluationFormUser = $companySelfEvaluationFormUserMapper->fetchOneByFormIdAndUserId($companySelfEvaluationForm->id, $currentUser->id);
359
        if (!$companySelfEvaluationFormUser) {
360
            return new JsonModel([
361
                'success' => false,
362
                'data' => 'ERROR_FORM_SELF_EVALUATION_YOU_CAN_NOT_TAKE'
363
            ]);
364
        }
365
 
366
 
488 geraldo 367
        $companySelfEvaluationTestMapper = CompanySelfEvaluationTestMapper::getInstance($this->adapter);
493 geraldo 368
        $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneBy($companySelfEvaluationForm->id, $currentUser->id);
487 geraldo 369
 
488 geraldo 370
        if (!$companySelfEvaluationTest) {
487 geraldo 371
            return new JsonModel([
372
                'success' => false,
488 geraldo 373
                'data' => 'ERROR_FORM_SELF_EVALUATION_TEST_NOT_FOUND'
487 geraldo 374
            ]);
375
        }
376
 
494 geraldo 377
        if ($companySelfEvaluationTest->status != CompanySelfEvaluationTest::STATUS_COMPLETED) {
487 geraldo 378
            return new JsonModel([
379
                'success' => false,
488 geraldo 380
                'data' => 'ERROR_FORM_SELF_EVALUATION_TEST_IS_INCOPLETE'
487 geraldo 381
            ]);
382
        }
383
 
384
 
385
        $request = $this->getRequest();
386
        if ($request->isGet()) {
493 geraldo 387
            return new JsonModel([
388
                'success' => false,
389
                'data' => [
390
                    'name' => $companySelfEvaluationForm->name,
391
                    'description' => $companySelfEvaluationForm->description,
392
                    'text' => $companySelfEvaluationForm->text,
393
                    'content' => json_decode($companySelfEvaluationTest->content)
394
                ]
395
            ]);
396
        }
488 geraldo 397
 
398
 
487 geraldo 399
        return new JsonModel([
400
            'success' => false,
401
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
402
        ]);
279 efrain 403
    }
404
 
405
}