Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 294 | Rev 431 | 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
2
/**
3
 *
291 geraldo 4
 * Controlador: Autoevaluación
279 efrain 5
 *
6
 */
7
declare(strict_types=1);
8
 
9
namespace LeadersLinked\Controller;
10
 
11
use Laminas\Db\Adapter\AdapterInterface;
12
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
13
use Laminas\Mvc\Controller\AbstractActionController;
14
use Laminas\Log\LoggerInterface;
15
use Laminas\View\Model\ViewModel;
16
use Laminas\View\Model\JsonModel;
17
use LeadersLinked\Model\Notification;
18
use LeadersLinked\Mapper\CompanySelfEvaluationTestMapper;
19
use LeadersLinked\Mapper\NotificationMapper;
20
use LeadersLinked\Mapper\QueryMapper;
21
use LeadersLinked\Model\CompanySelfEvaluationForm;
22
use LeadersLinked\Mapper\CompanySelfEvaluationFormMapper;
23
use LeadersLinked\Model\CompanySelfEvaluationTest;
24
use Laminas\Db\Sql\Select;
25
use LeadersLinked\Mapper\CompanySelfEvaluationFormUserMapper;
26
use LeadersLinked\Form\SelfEvaluation\SelfEvaluationTestForm;
27
use LeadersLinked\Library\Functions;
28
 
29
class SelfEvaluationController extends AbstractActionController
30
{
31
    /**
32
     *
33
     * @var AdapterInterface
34
     */
35
    private $adapter;
36
 
37
 
38
    /**
39
     *
40
     * @var AbstractAdapter
41
     */
42
    private $cache;
43
 
44
    /**
45
     *
46
     * @var  LoggerInterface
47
     */
48
    private $logger;
49
 
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
    {
66
        $this->adapter      = $adapter;
67
        $this->cache        = $cache;
68
        $this->logger       = $logger;
69
        $this->config       = $config;
70
 
71
    }
72
 
73
    /**
74
     *
291 geraldo 75
     * Generación del listado de evaluaciones
279 efrain 76
     * {@inheritDoc}
77
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
78
     */
79
    public function indexAction()
80
    {
81
        $currentUserPlugin = $this->plugin('currentUserPlugin');
82
        $currentUser = $currentUserPlugin->getUser();
83
 
84
        $request = $this->getRequest();
85
        if($request->isGet()) {
86
 
87
 
88
            $headers  = $request->getHeaders();
89
            $isJson = false;
90
            if($headers->has('Accept')) {
91
                $accept = $headers->get('Accept');
92
 
93
                $prioritized = $accept->getPrioritized();
94
 
95
                foreach($prioritized as $key => $value) {
96
                    $raw = trim($value->getRaw());
97
 
98
                    if(!$isJson) {
99
                        $isJson = strpos($raw, 'json');
100
                    }
101
 
102
                }
103
            }
104
 
105
            if($isJson) {
106
                $search = trim(filter_var($this->params()->fromQuery('search', ''), FILTER_SANITIZE_STRING));
107
 
281 efrain 108
 
109
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
110
                $allowTakeATest = $acl->isAllowed($currentUser->usertype_id, 'profile/self-evaluation/take-a-test');
111
                $allowReport = $acl->isAllowed($currentUser->usertype_id, 'profile/self-evaluation/report');
112
 
113
 
279 efrain 114
 
115
 
116
                $queryMapper = QueryMapper::getInstance($this->adapter);
117
 
118
                $select = $queryMapper->getSql()->select();
119
                $select->columns(['uuid','name', 'description', 'text', 'language']);
120
                $select->from(['f' => CompanySelfEvaluationFormMapper::_TABLE]);
294 efrain 121
                $select->join(['fu' => CompanySelfEvaluationFormUserMapper::_TABLE], 'f.id = fu.form_id', []);
279 efrain 122
                $select->join(['t' => CompanySelfEvaluationTestMapper::_TABLE], 'fu.form_id = t.form_id AND fu.user_id = t.user_id', ['status'], Select::JOIN_LEFT_OUTER );
123
                $select->where->equalTo('f.status', CompanySelfEvaluationForm::STATUS_ACTIVE);
296 efrain 124
                $select->where->equalTo('fu.user_id', $currentUser->id);
279 efrain 125
 
296 efrain 126
 
279 efrain 127
                if($search) {
128
                    $select->where->NEST->like('name', '%' . $search . '%');
129
                }
130
                $select->order('name ASC, language ASC');
131
 
132
                $records = $queryMapper->fetchAll($select);
133
                $items = [];
134
                foreach($records as $record)
135
                {
136
                    switch($record['language'])
137
                    {
138
                        case CompanySelfEvaluationForm::LANGUAGE_ENGLISH :
139
                            $language = 'LABEL_ENGLISH';
140
                            break;
141
 
142
                        case CompanySelfEvaluationForm::LANGUAGE_SPANISH :
143
                            $language = 'LABEL_SPANISH';
144
                            break;
145
 
146
                        default :
147
                            $language = '';
148
                            break;
149
                    }
150
 
151
                    switch ($record['status'])
152
                    {
153
                        case CompanySelfEvaluationTest::STATUS_COMPLETED :
154
                            $status = 'LABEL_COMPLETED';
155
                            break;
156
 
157
                        case CompanySelfEvaluationTest::STATUS_PENDING :
158
                            $status = 'LABEL_PENDING';
159
                            break;
160
 
161
                        case CompanySelfEvaluationTest::STATUS_REVIEW :
162
                            $status = 'LABEL_REVIEW';
163
                            break;
164
 
165
 
166
                        default :
281 efrain 167
                            $status = 'LABEL_AVAILABLE';
279 efrain 168
                            break;
169
 
170
                    }
171
 
172
 
173
                    $item = [
174
                        'name' => $record['name'],
175
                        'description' => $record['description'],
176
                        'text' => $record['text'],
177
                        'language' => $language,
178
                        'status' => $status,
281 efrain 179
                        'link_take_a_test' => $allowTakeATest && empty($record['status']) ? $this->url()->fromRoute('profile/self-evaluation/take-a-test', ['id' => $record['uuid']  ]) : '',
180
                        'link_report'   => $allowReport && $record['status'] == CompanySelfEvaluationTest::STATUS_COMPLETED ?  $this->url()->fromRoute('profile/self-evaluation/report', ['id' => $record['uuid']  ]) : '',
279 efrain 181
 
182
                    ];
183
 
184
 
185
 
186
                    array_push($items, $item);
187
                }
188
 
189
 
190
 
191
 
192
                $response = [
193
                    'success' => true,
194
                    'data' => $items
195
                ];
196
 
197
                return new JsonModel($response);
198
 
199
 
200
            } else {
201
 
202
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
203
                $notificationMapper->markAllNotificationsAsReadByTypeAndUserId(Notification::TYPE_ACCEPT_MY_REQUEST_CONNECTION, $currentUser->id);
204
 
205
                $this->layout()->setTemplate('layout/layout.phtml');
206
                $viewModel = new ViewModel();
207
                $viewModel->setTemplate('leaders-linked/self-evaluation/index.phtml');
208
                return $viewModel ;
209
            }
210
 
211
        } else {
212
            return new JsonModel([
213
                'success' => false,
214
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
215
            ]);
216
        }
217
    }
218
 
219
    public function takeaTestAction()
220
    {
221
        $currentUserPlugin = $this->plugin('currentUserPlugin');
222
        $currentUser = $currentUserPlugin->getUser();
223
 
224
        $uuid = $this->params()->fromRoute('id');
225
 
226
        $companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
227
        $companySelfEvaluationForm =  $companySelfEvaluationFormMapper->fetchOneByUuid($uuid);
228
 
229
        if(!$companySelfEvaluationForm) {
230
            return new JsonModel([
231
                'success' => false,
232
                'data' => 'ERROR_FORM_SELF_EVALUATION_NOT_FOUND'
233
            ]) ;
234
        }
235
 
236
        if($companySelfEvaluationForm->status == CompanySelfEvaluationForm::STATUS_INACTIVE) {
237
            return new JsonModel([
238
                'success' => false,
239
                'data' => 'ERROR_FORM_SELF_EVALUATION_IS_INACTIVE'
240
            ]) ;
241
        }
242
 
243
 
244
        $companySelfEvaluationFormUserMapper = CompanySelfEvaluationFormUserMapper::getInstance($this->adapter);
245
        $companySelfEvaluationFormUser = $companySelfEvaluationFormUserMapper->fetchOneByFormIdAndUserId($companySelfEvaluationForm->id, $currentUser->id);
246
        if(!$companySelfEvaluationFormUser) {
247
            return new JsonModel([
248
                'success' => false,
249
                'data' => 'ERROR_FORM_SELF_EVALUATION_YOU_CAN_NOT_TAKE'
250
            ]) ;
251
        }
252
 
253
 
254
        $companySelfEvaluationTestMapper = CompanySelfEvaluationTestMapper::getInstance($this->adapter);
255
        $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneBy($companySelfEvaluationForm->id, $currentUser->id);
256
 
257
        if($companySelfEvaluationTest) {
258
            return new JsonModel([
259
                'success' => false,
260
                'data' => 'ERROR_FORM_SELF_EVALUATION_ALREADY_YOUR_APPLICATION_IN_THIS_TEST'
261
            ]) ;
262
        }
263
 
264
 
265
        $request = $this->getRequest();
266
        if($request->isGet()) {
267
            return new JsonModel([
268
                'success' => false,
269
                'data' => [
270
                    'name' => $companySelfEvaluationForm->name,
271
                    'description' => $companySelfEvaluationForm->description,
272
                    'text' => $companySelfEvaluationForm->text,
273
                    'content' => json_decode($companySelfEvaluationForm->content),
274
                ]
275
            ]) ;
276
        }
277
 
278
        if($request->isPost()) {
279
            $form = new  SelfEvaluationTestForm();
280
            $dataPost = $request->getPost()->toArray();
281
 
282
            $form->setData($dataPost);
283
 
284
            if($form->isValid()) {
285
                $dataPost = (array) $form->getData();
286
 
287
                $selfEvaluationTest = new CompanySelfEvaluationTest();
288
                $selfEvaluationTest->company_id = $companySelfEvaluationForm->company_id;
289
                $selfEvaluationTest->form_id = $companySelfEvaluationForm->id;
290
                $selfEvaluationTest->user_id = $currentUser->id;
291
                $selfEvaluationTest->status = CompanySelfEvaluationTest::STATUS_PENDING;
292
                $selfEvaluationTest->content = $dataPost['content'];
293
 
294
 
295
 
296
 
297
                $result = $companySelfEvaluationTestMapper->insert($selfEvaluationTest);
298
 
299
                if($result) {
300
                    $this->logger->info('Se agrego un nuevo test de auto-evaluación : ' . $companySelfEvaluationForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
301
 
302
                    $data = [
303
                        'success'   => true,
304
                        'data'   => 'LABEL_RECORD_ADDED'
305
                    ];
306
                } else {
307
                    $data = [
308
                        'success'   => false,
309
                        'data'      => $companySelfEvaluationTestMapper->getError()
310
                    ];
311
 
312
                }
313
 
314
                return new JsonModel($data);
315
 
316
            } else {
317
                $messages = [];
318
                $form_messages = (array) $form->getMessages();
319
                foreach($form_messages  as $fieldname => $field_messages)
320
                {
321
 
322
                    $messages[$fieldname] = array_values($field_messages);
323
                }
324
 
325
                return new JsonModel([
326
                    'success'   => false,
327
                    'data'   => $messages
328
                ]);
329
            }
330
        }
331
 
332
        return new JsonModel([
333
            'success' => false,
334
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
335
        ]);
336
    }
337
 
338
    public function reportAction()
339
    {
340
        /*
341
         *     'ERROR_FORM_SELF_EVALUATION_NOT_FOUND' => 'El Test no existe',
342
    'ERROR_FORM_SELF_EVALUATION_IS_INACTIVE' => 'El Test esta inactivo',
343
    'ERROR_FORM_SELF_EVALUATION_YOU_CAN_NOT_TAKE' => 'No puede tomar este Test',
344
    'ERROR_FORM_SELF_EVALUATION_ALREADY_YOUR_APPLICATION_IN_THIS_TEST' => 'Ya realizo este Test previamente',
345
    'ERROR_FORM_SELF_EVALUATION_YOUR_APPLICATION_IN_THIS_TEST_NO_FOUND' => 'No ha realizado este Test previamente',
346
    'ERROR_FORM_SELF_EVALUATION_YOUR_APPLICATION_IN_THIS_TEST_IS_NOT_COMPLETED' => 'Su Test no se encuentra disponible para consultar',
347
         */
348
    }
349
 
350
}
291 geraldo 351