Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16766 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
133 efrain 1
<?php
2
/**
3
 *
4
 * Controlador: Mis Perfiles
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
 
18
use LeadersLinked\Library\Functions;
19
use LeadersLinked\Mapper\QueryMapper;
20
use LeadersLinked\Mapper\CompanyMapper;
21
use LeadersLinked\Mapper\CompanyMicrolearningQuizMapper;
22
use LeadersLinked\Form\QuestionCreateOrEditForm;
23
use Laminas\Hydrator\ObjectPropertyHydrator;
24
use LeadersLinked\Model\CompanyMicrolearningQuestion;
25
use LeadersLinked\Mapper\CompanyMicrolearningQuestionMapper;
26
use LeadersLinked\Model\CompanyMicroLearningQuiz;
27
 
28
class MicrolearningQuestionController extends AbstractActionController
29
{
30
    /**
31
     *
32
     * @var AdapterInterface
33
     */
34
    private $adapter;
35
 
36
 
37
    /**
38
     *
39
     * @var AbstractAdapter
40
     */
41
    private $cache;
42
 
43
    /**
44
     *
45
     * @var  LoggerInterface
46
     */
47
    private $logger;
48
 
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
    {
65
        $this->adapter      = $adapter;
66
        $this->cache        = $cache;
67
        $this->logger       = $logger;
68
        $this->config       = $config;
69
 
70
    }
71
 
72
    public function indexAction()
73
    {
74
        $request            = $this->getRequest();
75
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
76
        $currentUser        = $currentUserPlugin->getUser();
77
        $currentCompany     = $currentUserPlugin->getCompany();
78
        $quiz_id            = $this->params()->fromRoute('quiz_id');
79
 
80
        $flashMessenger     = $this->plugin('FlashMessenger');
81
 
82
 
83
        $quizMapper = CompanyMicrolearningQuizMapper::getInstance($this->adapter);
84
        $quiz = $quizMapper->fetchOneByUuid($quiz_id);
85
        if(!$quiz) {
86
            $flashMessenger->addErrorMessage('ERROR_FORM_NOT_FOUND');
87
            return $this->redirect()->toRoute('dashboard');
88
        }
89
 
90
        if($quiz->company_id != $currentCompany->id) {
91
            $flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
92
            return $this->redirect()->toRoute('dashboard');
93
        }
94
 
95
        $request = $this->getRequest();
96
        if($request->isGet()) {
97
            $headers  = $request->getHeaders();
98
 
99
            $isJson = false;
100
            if($headers->has('Accept')) {
101
                $accept = $headers->get('Accept');
102
 
103
                $prioritized = $accept->getPrioritized();
104
 
105
                foreach($prioritized as $key => $value) {
106
                    $raw = trim($value->getRaw());
107
 
108
                    if(!$isJson) {
109
                        $isJson = strpos($raw, 'json');
110
                    }
111
 
112
                }
113
            }
114
 
115
            if($isJson) {
116
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
117
                $allowAnswers = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/quizzes/questions/answers');
118
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/quizzes/questions/add');
119
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/quizzes/questions/edit');
120
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/quizzes/questions/delete');
121
 
122
 
123
 
124
                $search = $this->params()->fromQuery('search', []);
125
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
126
 
127
                $page               = intval($this->params()->fromQuery('start', 1), 10);
128
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
129
                $order =  $this->params()->fromQuery('order', []);
130
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
131
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
132
 
133
                $fields =  ['text'];
134
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'text';
135
 
136
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
137
                    $order_direction = 'ASC';
138
                }
139
 
140
 
141
 
142
                $companyMicrolearningQuestionMapper = CompanyMicrolearningQuestionMapper::getInstance($this->adapter);
143
                $paginator = $companyMicrolearningQuestionMapper->fetchAllDataTableByCompanyIdAndQuizId($quiz->company_id, $quiz->id, $search, $page, $records_x_page, $order_field, $order_direction);
144
 
145
 
146
                $items = [];
147
                foreach($paginator as $record)
148
                {
149
                    $params = [
150
                        'quiz_id' => $quiz->uuid,
151
                        'question_id' => $record->uuid,
152
                    ];
153
 
154
                    $item = [
155
                        'text' => substr(strip_tags($record->text), 0, 50),
156
                        'details' => [
157
                            'points' => $record->points,
158
                            'check' => '',
159
                            'type' => '',
160
                        ],
161
                        'actions' => [
162
                            'link_edit'   => $allowEdit ? $this->url()->fromRoute('microlearning/content/quizzes/questions/edit', $params) : '',
163
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('microlearning/content/quizzes/questions/delete', $params) : '',
164
                        ],
165
                    ];
166
 
167
                    switch($record->check)
168
                    {
169
                        case CompanyMicrolearningQuestion::CHECK_OK :
170
                            $item['details']['check'] = 'LABEL_OK';
171
                            break;
172
 
173
                        case CompanyMicrolearningQuestion::CHECK_WRONG :
174
                            $item['details']['check'] = 'LABEL_WRONG';
175
                            break;
176
 
177
                        case CompanyMicrolearningQuestion::CHECK_PENDING :
178
                            $item['details']['check'] = 'LABEL_PENDING';
179
                            break;
180
 
181
                        default :
182
                            $item['details']['check'] = '';
183
                            break;
184
                    }
185
 
186
 
187
 
188
                    $item['actions']['link_answer'] = '';
189
                    switch($record->type)
190
                    {
191
                        case CompanyMicrolearningQuestion::TYPE_MULTI_LINE :
192
                            $item['details']['type'] = 'LABEL_MULTI_LINE';
193
                            break;
194
 
195
                        case CompanyMicrolearningQuestion::TYPE_SINGLE_LINE :
196
                            $item['details']['type'] = 'LABEL_SINGLE_LINE';
197
                            break;
198
 
199
                        case CompanyMicrolearningQuestion::TYPE_SINGLE_SELECTION :
200
                            $item['details']['type'] = 'LABEL_SINGLE_SELECTION';
201
                            $item['actions']['link_answer'] = $allowAnswers ? $this->url()->fromRoute('microlearning/content/quizzes/questions/answers', $params) : '';
202
                            break;
203
 
204
                        case CompanyMicrolearningQuestion::TYPE_MULTIPLE_SELECTION;
205
                            $item['details']['type'] = 'LABEL_MULTIPLE_SELECTION';
206
                            $item['actions']['link_answer'] = $allowAnswers ? $this->url()->fromRoute('microlearning/content/quizzes/questions/answers', $params) : '';
207
                            break;
208
 
209
                        case CompanyMicrolearningQuestion::TYPE_RANGE_1_5;
210
                            $item['details']['type'] = 'LABEL_RANGE_1_5';
211
                            break;
212
 
213
                        case CompanyMicrolearningQuestion::TYPE_RANGE_1_6;
214
                            $item['details']['type'] = 'LABEL_RANGE_1_6';
215
                            break;
216
 
217
                        case CompanyMicrolearningQuestion::TYPE_RANGE_1_10;
218
                            $item['type'] = 'LABEL_RANGE_1_10';
219
                            break;
220
 
221
                        case CompanyMicrolearningQuestion::TYPE_RANGE_OPEN;
222
                            $item['details']['type'] = 'LABEL_RANGE_OPEN';
223
                            $item['actions']['link_answer'] = $allowAnswers ? $this->url()->fromRoute('microlearning/content/quizzes/questions/answer', $params) : '';
224
                            break;
225
 
226
                        default :
227
                            $item['details']['type'] = '';
228
                            break;
229
                    }
230
 
231
 
232
 
233
 
234
 
235
                    array_push($items, $item);
236
                }
237
 
238
 
239
                $data = [];
240
                $data['items'] = $items;
241
                $data['total'] = $paginator->getTotalItemCount();
242
 
243
                if($allowAdd) {
244
                    $data['actions']['link_add'] = $this->url()->fromRoute('microlearning/content/quizzes/questions/add', ['quiz_id' => $quiz->id]);
245
                }  else {
246
                    $data['actions']['link_add'] = '';
247
                }
248
 
249
                $response = [
250
                    'success' => true,
251
                    'data' => $data
252
                ];
253
 
254
                return new JsonModel($response);
255
 
256
 
257
            } else {
258
                $quizCreateOrEdit = new QuestionCreateOrEditForm();
259
 
260
                $this->layout()->setTemplate('layout/layout-backend.phtml');
261
                $viewModel = new ViewModel();
262
                $viewModel->setTemplate('leaders-linked/microlearning-questions/index.phtml');
263
                $viewModel->setVariables([
264
                    'quiz_uuid' => $quiz->uuid,
265
                    'quiz_name' => $quiz->name,
266
                    'formCreateOrEdit' => $quizCreateOrEdit,
267
                ]);
268
                return $viewModel ;
269
            }
270
 
271
        } else {
272
            return new JsonModel([
273
                'success' => false,
274
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
275
            ]);
276
        }
277
    }
278
 
279
    public function addAction()
280
    {
281
        $request            = $this->getRequest();
282
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
283
        $currentUser        = $currentUserPlugin->getUser();
284
        $currentCompany     = $currentUserPlugin->getCompany();
285
        $quiz_id            = $this->params()->fromRoute('quiz_id');
286
 
287
 
288
        $quizMapper = CompanyMicrolearningQuizMapper::getInstance($this->adapter);
289
        $quiz = $quizMapper->fetchOneByUuid($quiz_id);
290
        if(!$quiz) {
291
            return new JsonModel([
292
                'success'   => false,
293
                'data'   => 'ERROR_FORM_NOT_FOUND'
294
            ]);
295
        }
296
 
297
        if($quiz->company_id != $currentCompany->id) {
298
            return new JsonModel([
299
                'success'   => false,
300
                'data'   => 'ERROR_UNAUTHORIZED'
301
            ]);
302
        }
303
 
304
        if($request->isPost()) {
305
            $quizCreateOrEdit = new  QuestionCreateOrEditForm();
306
            $quizCreateOrEdit->setData($request->getPost()->toArray());
307
 
308
            if($quizCreateOrEdit->isValid()) {
309
                $dataPost = (array) $quizCreateOrEdit->getData();
310
 
311
                $hydrator = new ObjectPropertyHydrator();
312
                $question = new CompanyMicrolearningQuestion();
313
                $hydrator->hydrate($dataPost, $question);
314
 
315
                $question->company_id = $quiz->company_id;
316
                $question->quiz_id = $quiz->id;
317
 
318
 
319
                $questionMapper = CompanyMicrolearningQuestionMapper::getInstance($this->adapter);
320
                if($questionMapper->insert($question)) {
321
 
322
                    $quiz->check = CompanyMicroLearningQuiz::CHECK_PENDING;
323
                    $quizMapper->update($quiz);
324
 
325
                    $this->logger->info('Se agrego la pregunta ' . substr(strip_tags($question->text), 0, 50), ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
326
 
327
                    $data = [
328
                        'success'   => true,
329
                        'data'   => 'LABEL_RECORD_ADDED'
330
                    ];
331
                } else {
332
                    $data = [
333
                        'success'   => false,
334
                        'data'      => $questionMapper->getError()
335
                    ];
336
 
337
                }
338
 
339
                return new JsonModel($data);
340
 
341
            } else {
342
                $messages = [];
343
                $quiz_messages = (array) $quizCreateOrEdit->getMessages();
344
                foreach($quiz_messages  as $fieldname => $field_messages)
345
                {
346
 
347
                    $messages[$fieldname] = array_values($field_messages);
348
                }
349
 
350
                return new JsonModel([
351
                    'success'   => false,
352
                    'data'   => $messages
353
                ]);
354
            }
355
 
356
        } else {
357
            $data = [
358
                'success' => false,
359
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
360
            ];
361
 
362
            return new JsonModel($data);
363
        }
364
 
365
        return new JsonModel($data);
366
    }
367
 
368
    /**
369
     *
370
     * Borrar un perfil excepto el público
371
     * @return \Laminas\View\Model\JsonModel
372
     */
373
    public function deleteAction()
374
    {
375
        $request            = $this->getRequest();
376
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
377
        $currentUser        = $currentUserPlugin->getUser();
378
        $currentCompany     = $currentUserPlugin->getCompany();
379
        $quiz_id            = $this->params()->fromRoute('quiz_id');
380
        $question_id        = $this->params()->fromRoute('question_id');
381
 
382
 
383
        $quizMapper = CompanyMicrolearningQuizMapper::getInstance($this->adapter);
384
        $quiz = $quizMapper->fetchOneByUuid($quiz_id);
385
        if(!$quiz) {
386
            return new JsonModel([
387
                'success'   => false,
388
                'data'   => 'ERROR_FORM_NOT_FOUND'
389
            ]);
390
        }
391
 
392
        if($quiz->company_id != $currentCompany->id) {
393
            return new JsonModel([
394
                'success'   => false,
395
                'data'   => 'ERROR_UNAUTHORIZED'
396
            ]);
397
        }
398
 
399
 
400
        $questionMapper = CompanyMicrolearningQuestionMapper::getInstance($this->adapter);
401
        $question = $questionMapper->fetchOneByUuid($question_id);
402
        if(!$question) {
403
            return new JsonModel([
404
                'success'   => false,
405
                'data'   => 'ERROR_QUESTION_NOT_FOUND'
406
            ]);
407
        }
408
 
409
        if($question->quiz_id != $quiz->id) {
410
            return new JsonModel([
411
                'success'   => false,
412
                'data'   => 'ERROR_UNAUTHORIZED'
413
            ]);
414
        }
415
 
416
 
417
        if($request->isPost()) {
418
            $result = $questionMapper->delete($question);
419
            if($result) {
420
 
421
                $quiz->check = CompanyMicroLearningQuiz::CHECK_PENDING;
422
                $quizMapper->update($quiz);
423
 
424
 
425
                $this->logger->info('Se borro la pregunta : ' .  substr(strip_tags($question->text), 0, 50), ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
426
 
427
                $data = [
428
                    'success' => true,
429
                    'data' => 'LABEL_RECORD_DELETED'
430
                ];
431
            } else {
432
 
433
                $data = [
434
                    'success'   => false,
435
                    'data'      => $questionMapper->getError()
436
                ];
437
 
438
                return new JsonModel($data);
439
            }
440
 
441
        } else {
442
            $data = [
443
                'success' => false,
444
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
445
            ];
446
 
447
            return new JsonModel($data);
448
        }
449
 
450
        return new JsonModel($data);
451
    }
452
 
453
 
454
    public function editAction()
455
    {
456
        $request            = $this->getRequest();
457
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
458
        $currentUser        = $currentUserPlugin->getUser();
459
        $currentCompany     = $currentUserPlugin->getCompany();
460
        $quiz_id            = $this->params()->fromRoute('quiz_id');
461
        $question_id        = $this->params()->fromRoute('question_id');
462
 
463
 
464
        $quizMapper = CompanyMicrolearningQuizMapper::getInstance($this->adapter);
465
        $quiz = $quizMapper->fetchOneByUuid($quiz_id);
466
        if(!$quiz) {
467
            return new JsonModel([
468
                'success'   => false,
469
                'data'   => 'ERROR_FORM_NOT_FOUND'
470
            ]);
471
        }
472
 
473
        if($quiz->company_id != $currentCompany->id) {
474
            return new JsonModel([
475
                'success'   => false,
476
                'data'   => 'ERROR_UNAUTHORIZED'
477
            ]);
478
        }
479
 
480
        $questionMapper = CompanyMicrolearningQuestionMapper::getInstance($this->adapter);
481
        $question = $questionMapper->fetchOneByUuid($question_id);
482
        if(!$question) {
483
            return new JsonModel([
484
                'success'   => false,
485
                'data'   => 'ERROR_QUESTION_NOT_FOUND'
486
            ]);
487
        }
488
 
489
        if($question->quiz_id != $quiz->id) {
490
            return new JsonModel([
491
                'success'   => false,
492
                'data'   => 'ERROR_UNAUTHORIZED'
493
            ]);
494
        }
495
 
496
        if($request->isGet()) {
497
            $data = [
498
                'success' => true,
499
                'data' => [
500
                    'text' => $question->text,
501
                    'type' => $question->type,
502
                    'maxlength' => $question->maxlength,
503
                    'points' => $question->points,
504
                ]
505
            ];
506
 
507
            return new JsonModel($data);
508
        }
509
        else if($request->isPost()) {
510
            $quizCreateOrEdit = new  QuestionCreateOrEditForm();
511
            $quizCreateOrEdit->setData($request->getPost()->toArray());
512
 
513
            if($quizCreateOrEdit->isValid()) {
514
                $dataPost = (array) $quizCreateOrEdit->getData();
515
 
516
                $hydrator = new ObjectPropertyHydrator();
517
                $hydrator->hydrate($dataPost, $question);
518
 
519
                $question->check = CompanyMicrolearningQuestion::CHECK_PENDING;
520
                if($questionMapper->update($question)) {
521
                    $quiz->check = CompanyMicroLearningQuiz::CHECK_PENDING;
522
                    $quizMapper->update($quiz);
523
 
524
                    $this->logger->info('Se edito la pregunta ' .  substr(strip_tags($question->text), 0, 50), ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
525
 
526
                    $data = [
527
                        'success'   => true,
528
                        'data'   => 'LABEL_RECORD_UPDATED'
529
                    ];
530
                } else {
531
                    $data = [
532
                        'success'   => false,
533
                        'data'      => $questionMapper->getError()
534
                    ];
535
 
536
                }
537
 
538
                return new JsonModel($data);
539
 
540
            } else {
541
                $messages = [];
542
                $quiz_messages = (array) $quizCreateOrEdit->getMessages();
543
                foreach($quiz_messages  as $fieldname => $field_messages)
544
                {
545
 
546
                    $messages[$fieldname] = array_values($field_messages);
547
                }
548
 
549
                return new JsonModel([
550
                    'success'   => false,
551
                    'data'   => $messages
552
                ]);
553
            }
554
        } else {
555
            $data = [
556
                'success' => false,
557
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
558
            ];
559
 
560
            return new JsonModel($data);
561
        }
562
 
563
        return new JsonModel($data);
564
    }
565
 
566
 
567
}