Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16769 | | Comparar con el anterior | Ultima modificación | Ver Log |

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