Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 292 | Rev 294 | 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]);
121
                $select->join(['fu' => CompanySelfEvaluationFormUserMapper::_TABLE, 'f.id = fu.form_id']);
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);
124
 
125
 
126
                if($search) {
127
                    $select->where->NEST->like('name', '%' . $search . '%');
128
                }
129
                $select->order('name ASC, language ASC');
130
 
131
                $records = $queryMapper->fetchAll($select);
132
                $items = [];
133
                foreach($records as $record)
134
                {
135
                    switch($record['language'])
136
                    {
137
                        case CompanySelfEvaluationForm::LANGUAGE_ENGLISH :
138
                            $language = 'LABEL_ENGLISH';
139
                            break;
140
 
141
                        case CompanySelfEvaluationForm::LANGUAGE_SPANISH :
142
                            $language = 'LABEL_SPANISH';
143
                            break;
144
 
145
                        default :
146
                            $language = '';
147
                            break;
148
                    }
149
 
150
                    switch ($record['status'])
151
                    {
152
                        case CompanySelfEvaluationTest::STATUS_COMPLETED :
153
                            $status = 'LABEL_COMPLETED';
154
                            break;
155
 
156
                        case CompanySelfEvaluationTest::STATUS_PENDING :
157
                            $status = 'LABEL_PENDING';
158
                            break;
159
 
160
                        case CompanySelfEvaluationTest::STATUS_REVIEW :
161
                            $status = 'LABEL_REVIEW';
162
                            break;
163
 
164
 
165
                        default :
281 efrain 166
                            $status = 'LABEL_AVAILABLE';
279 efrain 167
                            break;
168
 
169
                    }
170
 
171
 
172
                    $item = [
173
                        'name' => $record['name'],
174
                        'description' => $record['description'],
175
                        'text' => $record['text'],
176
                        'language' => $language,
177
                        'status' => $status,
281 efrain 178
                        'link_take_a_test' => $allowTakeATest && empty($record['status']) ? $this->url()->fromRoute('profile/self-evaluation/take-a-test', ['id' => $record['uuid']  ]) : '',
179
                        'link_report'   => $allowReport && $record['status'] == CompanySelfEvaluationTest::STATUS_COMPLETED ?  $this->url()->fromRoute('profile/self-evaluation/report', ['id' => $record['uuid']  ]) : '',
279 efrain 180
 
181
                    ];
182
 
183
 
184
 
185
                    array_push($items, $item);
186
                }
187
 
188
 
189
 
190
 
191
                $response = [
192
                    'success' => true,
193
                    'data' => $items
194
                ];
195
 
196
                return new JsonModel($response);
197
 
198
 
199
            } else {
200
 
201
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
202
                $notificationMapper->markAllNotificationsAsReadByTypeAndUserId(Notification::TYPE_ACCEPT_MY_REQUEST_CONNECTION, $currentUser->id);
203
 
204
                $this->layout()->setTemplate('layout/layout.phtml');
205
                $viewModel = new ViewModel();
206
                $viewModel->setTemplate('leaders-linked/self-evaluation/index.phtml');
207
                return $viewModel ;
208
            }
209
 
210
        } else {
211
            return new JsonModel([
212
                'success' => false,
213
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
214
            ]);
215
        }
216
    }
217
 
218
    public function takeaTestAction()
219
    {
220
        $currentUserPlugin = $this->plugin('currentUserPlugin');
221
        $currentUser = $currentUserPlugin->getUser();
222
 
223
        $uuid = $this->params()->fromRoute('id');
224
 
225
        $companySelfEvaluationFormMapper = CompanySelfEvaluationFormMapper::getInstance($this->adapter);
226
        $companySelfEvaluationForm =  $companySelfEvaluationFormMapper->fetchOneByUuid($uuid);
227
 
228
        if(!$companySelfEvaluationForm) {
229
            return new JsonModel([
230
                'success' => false,
231
                'data' => 'ERROR_FORM_SELF_EVALUATION_NOT_FOUND'
232
            ]) ;
233
        }
234
 
235
        if($companySelfEvaluationForm->status == CompanySelfEvaluationForm::STATUS_INACTIVE) {
236
            return new JsonModel([
237
                'success' => false,
238
                'data' => 'ERROR_FORM_SELF_EVALUATION_IS_INACTIVE'
239
            ]) ;
240
        }
241
 
242
 
243
        $companySelfEvaluationFormUserMapper = CompanySelfEvaluationFormUserMapper::getInstance($this->adapter);
244
        $companySelfEvaluationFormUser = $companySelfEvaluationFormUserMapper->fetchOneByFormIdAndUserId($companySelfEvaluationForm->id, $currentUser->id);
245
        if(!$companySelfEvaluationFormUser) {
246
            return new JsonModel([
247
                'success' => false,
248
                'data' => 'ERROR_FORM_SELF_EVALUATION_YOU_CAN_NOT_TAKE'
249
            ]) ;
250
        }
251
 
252
 
253
        $companySelfEvaluationTestMapper = CompanySelfEvaluationTestMapper::getInstance($this->adapter);
254
        $companySelfEvaluationTest = $companySelfEvaluationTestMapper->fetchOneBy($companySelfEvaluationForm->id, $currentUser->id);
255
 
256
        if($companySelfEvaluationTest) {
257
            return new JsonModel([
258
                'success' => false,
259
                'data' => 'ERROR_FORM_SELF_EVALUATION_ALREADY_YOUR_APPLICATION_IN_THIS_TEST'
260
            ]) ;
261
        }
262
 
263
 
264
        $request = $this->getRequest();
265
        if($request->isGet()) {
266
            return new JsonModel([
267
                'success' => false,
268
                'data' => [
269
                    'name' => $companySelfEvaluationForm->name,
270
                    'description' => $companySelfEvaluationForm->description,
271
                    'text' => $companySelfEvaluationForm->text,
272
                    'content' => json_decode($companySelfEvaluationForm->content),
273
                ]
274
            ]) ;
275
        }
276
 
277
        if($request->isPost()) {
278
            $form = new  SelfEvaluationTestForm();
279
            $dataPost = $request->getPost()->toArray();
280
 
281
            $form->setData($dataPost);
282
 
283
            if($form->isValid()) {
284
                $dataPost = (array) $form->getData();
285
 
286
                $selfEvaluationTest = new CompanySelfEvaluationTest();
287
                $selfEvaluationTest->company_id = $companySelfEvaluationForm->company_id;
288
                $selfEvaluationTest->form_id = $companySelfEvaluationForm->id;
289
                $selfEvaluationTest->user_id = $currentUser->id;
290
                $selfEvaluationTest->status = CompanySelfEvaluationTest::STATUS_PENDING;
291
                $selfEvaluationTest->content = $dataPost['content'];
292
 
293
 
294
 
295
 
296
                $result = $companySelfEvaluationTestMapper->insert($selfEvaluationTest);
297
 
298
                if($result) {
299
                    $this->logger->info('Se agrego un nuevo test de auto-evaluación : ' . $companySelfEvaluationForm->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
300
 
301
                    $data = [
302
                        'success'   => true,
303
                        'data'   => 'LABEL_RECORD_ADDED'
304
                    ];
305
                } else {
306
                    $data = [
307
                        'success'   => false,
308
                        'data'      => $companySelfEvaluationTestMapper->getError()
309
                    ];
310
 
311
                }
312
 
313
                return new JsonModel($data);
314
 
315
            } else {
316
                $messages = [];
317
                $form_messages = (array) $form->getMessages();
318
                foreach($form_messages  as $fieldname => $field_messages)
319
                {
320
 
321
                    $messages[$fieldname] = array_values($field_messages);
322
                }
323
 
324
                return new JsonModel([
325
                    'success'   => false,
326
                    'data'   => $messages
327
                ]);
328
            }
329
        }
330
 
331
        return new JsonModel([
332
            'success' => false,
333
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
334
        ]);
335
    }
336
 
337
    public function reportAction()
338
    {
339
        /*
340
         *     'ERROR_FORM_SELF_EVALUATION_NOT_FOUND' => 'El Test no existe',
341
    'ERROR_FORM_SELF_EVALUATION_IS_INACTIVE' => 'El Test esta inactivo',
342
    'ERROR_FORM_SELF_EVALUATION_YOU_CAN_NOT_TAKE' => 'No puede tomar este Test',
343
    'ERROR_FORM_SELF_EVALUATION_ALREADY_YOUR_APPLICATION_IN_THIS_TEST' => 'Ya realizo este Test previamente',
344
    'ERROR_FORM_SELF_EVALUATION_YOUR_APPLICATION_IN_THIS_TEST_NO_FOUND' => 'No ha realizado este Test previamente',
345
    'ERROR_FORM_SELF_EVALUATION_YOUR_APPLICATION_IN_THIS_TEST_IS_NOT_COMPLETED' => 'Su Test no se encuentra disponible para consultar',
346
         */
347
    }
348
 
349
}
291 geraldo 350