Proyectos de Subversion LeadersLinked - Backend

Rev

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