Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 441 | Rev 459 | 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']);
279 efrain 115
                $select->from(['f' => CompanySelfEvaluationFormMapper::_TABLE]);
294 efrain 116
                $select->join(['fu' => CompanySelfEvaluationFormUserMapper::_TABLE], 'f.id = fu.form_id', []);
431 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,
439 geraldo 175
                        'link_take_a_test' => $allowTakeATest && ( empty($record['status'])  || $record['status'] == CompanySelfEvaluationTest::STATUS_DRAFT)
436 geraldo 176
                        ? $this->url()->fromRoute('profile/self-evaluation/take-a-test', ['id' => $record['uuid']]) : '',
431 geraldo 177
                        'link_report' => $allowReport && $record['status'] == CompanySelfEvaluationTest::STATUS_COMPLETED ? $this->url()->fromRoute('profile/self-evaluation/report', ['id' => $record['uuid']]) : '',
279 efrain 178
                    ];
179
 
431 geraldo 180
 
181
 
279 efrain 182
                    array_push($items, $item);
183
                }
184
 
431 geraldo 185
 
186
 
187
 
279 efrain 188
                $response = [
189
                    'success' => true,
190
                    'data' => $items
191
                ];
431 geraldo 192
 
279 efrain 193
                return new JsonModel($response);
194
            } else {
431 geraldo 195
 
279 efrain 196
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
197
                $notificationMapper->markAllNotificationsAsReadByTypeAndUserId(Notification::TYPE_ACCEPT_MY_REQUEST_CONNECTION, $currentUser->id);
431 geraldo 198
 
279 efrain 199
                $this->layout()->setTemplate('layout/layout.phtml');
200
                $viewModel = new ViewModel();
201
                $viewModel->setTemplate('leaders-linked/self-evaluation/index.phtml');
431 geraldo 202
                return $viewModel;
279 efrain 203
            }
204
        } else {
205
            return new JsonModel([
206
                'success' => false,
207
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
208
            ]);
209
        }
210
    }
431 geraldo 211
 
212
    public function takeaTestAction() {
279 efrain 213
        $currentUserPlugin = $this->plugin('currentUserPlugin');
214
        $currentUser = $currentUserPlugin->getUser();
431 geraldo 215
 
279 efrain 216
        $uuid = $this->params()->fromRoute('id');
431 geraldo 217
 
279 efrain 218
        $companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
431 geraldo 219
        $companySelfEvaluationForm = $companySelfEvaluationFormMapper->fetchOneByUuid($uuid);
220
 
221
        if (!$companySelfEvaluationForm) {
279 efrain 222
            return new JsonModel([
223
                'success' => false,
224
                'data' => 'ERROR_FORM_SELF_EVALUATION_NOT_FOUND'
431 geraldo 225
            ]);
279 efrain 226
        }
227
 
431 geraldo 228
        if ($companySelfEvaluationForm->status == CompanySelfEvaluationForm::STATUS_INACTIVE) {
279 efrain 229
            return new JsonModel([
230
                'success' => false,
231
                'data' => 'ERROR_FORM_SELF_EVALUATION_IS_INACTIVE'
431 geraldo 232
            ]);
279 efrain 233
        }
431 geraldo 234
 
235
 
279 efrain 236
        $companySelfEvaluationFormUserMapper = CompanySelfEvaluationFormUserMapper::getInstance($this->adapter);
237
        $companySelfEvaluationFormUser = $companySelfEvaluationFormUserMapper->fetchOneByFormIdAndUserId($companySelfEvaluationForm->id, $currentUser->id);
431 geraldo 238
        if (!$companySelfEvaluationFormUser) {
279 efrain 239
            return new JsonModel([
240
                'success' => false,
241
                'data' => 'ERROR_FORM_SELF_EVALUATION_YOU_CAN_NOT_TAKE'
431 geraldo 242
            ]);
279 efrain 243
        }
431 geraldo 244
 
245
 
279 efrain 246
        $companySelfEvaluationTestMapper = CompanySelfEvaluationTestMapper::getInstance($this->adapter);
247
        $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneBy($companySelfEvaluationForm->id, $currentUser->id);
431 geraldo 248
 
249
        if ($companySelfEvaluationTest && $companySelfEvaluationTest->status != CompanySelfEvaluationTest::STATUS_DRAFT) {
279 efrain 250
            return new JsonModel([
251
                'success' => false,
252
                'data' => 'ERROR_FORM_SELF_EVALUATION_ALREADY_YOUR_APPLICATION_IN_THIS_TEST'
431 geraldo 253
            ]);
279 efrain 254
        }
431 geraldo 255
 
256
 
279 efrain 257
        $request = $this->getRequest();
431 geraldo 258
        if ($request->isGet()) {
279 efrain 259
            return new JsonModel([
260
                'success' => false,
441 geraldo 261
                'data' => [
279 efrain 262
                    'name' => $companySelfEvaluationForm->name,
263
                    'description' => $companySelfEvaluationForm->description,
264
                    'text' => $companySelfEvaluationForm->text,
441 geraldo 265
                    'content' => $companySelfEvaluationTest && $companySelfEvaluationTest->status == CompanySelfEvaluationTest::STATUS_DRAFT ?
266
                    json_decode($companySelfEvaluationTest->content) :
267
                    json_decode($companySelfEvaluationForm->content),
279 efrain 268
                ]
431 geraldo 269
            ]);
279 efrain 270
        }
431 geraldo 271
 
272
        if ($request->isPost()) {
273
            $form = new SelfEvaluationTestForm();
279 efrain 274
            $dataPost = $request->getPost()->toArray();
431 geraldo 275
 
279 efrain 276
            $form->setData($dataPost);
431 geraldo 277
 
278
            if ($form->isValid()) {
279 efrain 279
                $dataPost = (array) $form->getData();
431 geraldo 280
 
279 efrain 281
                $selfEvaluationTest = new CompanySelfEvaluationTest();
282
                $selfEvaluationTest->company_id = $companySelfEvaluationForm->company_id;
283
                $selfEvaluationTest->form_id = $companySelfEvaluationForm->id;
284
                $selfEvaluationTest->user_id = $currentUser->id;
431 geraldo 285
                $selfEvaluationTest->status = $dataPost['status'];
279 efrain 286
                $selfEvaluationTest->content = $dataPost['content'];
431 geraldo 287
 
288
 
289
                //Check if the form is already registered
290
                $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneBy($companySelfEvaluationForm->id, $currentUser->id);
291
 
292
                $result = $companySelfEvaluationTest ?
293
                        $companySelfEvaluationTestMapper->update($selfEvaluationTest):
294
                        $companySelfEvaluationTestMapper->insert($selfEvaluationTest);
295
 
296
 
297
                if ($result) {
279 efrain 298
                    $this->logger->info('Se agrego un nuevo test de auto-evaluación : ' . $companySelfEvaluationForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
431 geraldo 299
 
279 efrain 300
                    $data = [
431 geraldo 301
                        'success' => true,
448 geraldo 302
                        'data' => $companySelfEvaluationTest ? 'LABEL_RECORD_UPDATED' : 'LABEL_RECORD_ADDED'
279 efrain 303
                    ];
304
                } else {
305
                    $data = [
431 geraldo 306
                        'success' => false,
307
                        'data' => $companySelfEvaluationTestMapper->getError()
279 efrain 308
                    ];
309
                }
431 geraldo 310
 
279 efrain 311
                return new JsonModel($data);
312
            } else {
313
                $messages = [];
314
                $form_messages = (array) $form->getMessages();
431 geraldo 315
                foreach ($form_messages as $fieldname => $field_messages) {
316
 
279 efrain 317
                    $messages[$fieldname] = array_values($field_messages);
318
                }
431 geraldo 319
 
279 efrain 320
                return new JsonModel([
431 geraldo 321
                    'success' => false,
322
                    'data' => $messages
279 efrain 323
                ]);
324
            }
325
        }
431 geraldo 326
 
279 efrain 327
        return new JsonModel([
328
            'success' => false,
329
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
330
        ]);
331
    }
431 geraldo 332
 
333
    public function reportAction() {
279 efrain 334
        /*
335
         *     'ERROR_FORM_SELF_EVALUATION_NOT_FOUND' => 'El Test no existe',
431 geraldo 336
          'ERROR_FORM_SELF_EVALUATION_IS_INACTIVE' => 'El Test esta inactivo',
337
          'ERROR_FORM_SELF_EVALUATION_YOU_CAN_NOT_TAKE' => 'No puede tomar este Test',
338
          'ERROR_FORM_SELF_EVALUATION_ALREADY_YOUR_APPLICATION_IN_THIS_TEST' => 'Ya realizo este Test previamente',
339
          'ERROR_FORM_SELF_EVALUATION_YOUR_APPLICATION_IN_THIS_TEST_NO_FOUND' => 'No ha realizado este Test previamente',
340
          'ERROR_FORM_SELF_EVALUATION_YOUR_APPLICATION_IN_THIS_TEST_IS_NOT_COMPLETED' => 'Su Test no se encuentra disponible para consultar',
279 efrain 341
         */
342
    }
343
 
344
}