Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17322 | | Comparar con el anterior | 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;
16768 efrain 12
 
133 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;
17002 efrain 20
use LeadersLinked\Model\MicrolearningQuiz;
21
use LeadersLinked\Mapper\MicrolearningQuizMapper;
17320 stevensc 22
use LeadersLinked\Form\Microlearning\QuizCreateOrEditForm;
133 efrain 23
use Laminas\Hydrator\ObjectPropertyHydrator;
17002 efrain 24
use LeadersLinked\Mapper\MicrolearningQuestionMapper;
25
use LeadersLinked\Mapper\MicrolearningAnswerMapper;
26
use LeadersLinked\Model\MicrolearningQuestion;
27
use LeadersLinked\Model\MicrolearningAnswer;
28
use LeadersLinked\Mapper\MicrolearningSlideMapper;
133 efrain 29
 
30
class MicrolearningQuizController extends AbstractActionController
31
{
16768 efrain 32
    /**
133 efrain 33
     *
16769 efrain 34
     * @var \Laminas\Db\Adapter\AdapterInterface
133 efrain 35
     */
36
    private $adapter;
37
 
38
    /**
39
     *
16769 efrain 40
     * @var \LeadersLinked\Cache\CacheInterface
133 efrain 41
     */
16769 efrain 42
    private $cache;
43
 
44
 
45
    /**
46
     *
47
     * @var \Laminas\Log\LoggerInterface
48
     */
133 efrain 49
    private $logger;
50
 
51
    /**
52
     *
53
     * @var array
54
     */
55
    private $config;
56
 
16769 efrain 57
 
133 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
133 efrain 70
     * @param array $config
16769 efrain 71
     * @param \Laminas\Mvc\I18n\Translator $translator
133 efrain 72
     */
16769 efrain 73
    public function __construct($adapter, $cache, $logger, $config, $translator)
133 efrain 74
    {
16769 efrain 75
        $this->adapter      = $adapter;
76
        $this->cache        = $cache;
77
        $this->logger       = $logger;
78
        $this->config       = $config;
79
        $this->translator   = $translator;
133 efrain 80
    }
81
 
82
    public function indexAction()
83
    {
17321 stevensc 84
        try {
85
            $request            = $this->getRequest();
86
            $currentUserPlugin  = $this->plugin('currentUserPlugin');
87
            $currentUser        = $currentUserPlugin->getUser();
88
            $currentCompany     = $currentUserPlugin->getCompany();
89
 
90
            $request = $this->getRequest();
91
            if($request->isGet()) {
92
                $headers  = $request->getHeaders();
133 efrain 93
 
17321 stevensc 94
                $isJson = false;
95
                if($headers->has('Accept')) {
96
                    $accept = $headers->get('Accept');
133 efrain 97
 
17321 stevensc 98
                    $prioritized = $accept->getPrioritized();
99
 
100
                    foreach($prioritized as $key => $value) {
101
                        $raw = trim($value->getRaw());
102
 
103
                        if(!$isJson) {
104
                            $isJson = strpos($raw, 'json');
105
                        }
106
 
133 efrain 107
                    }
108
                }
109
 
17321 stevensc 110
                if($isJson) {
111
                    $acl = $this->getEvent()->getViewModel()->getVariable('acl');
112
                    $allowQuestion = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/quizzes/questions');
113
                    $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/quizzes/add');
114
                    $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/quizzes/edit');
115
                    $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/quizzes/delete');
116
                    $allowCopy = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/quizzes/copy');
117
                    $allowCheck = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/quizzes/check');
133 efrain 118
 
17321 stevensc 119
                    $search = $this->params()->fromQuery('search', []);
120
                    $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
133 efrain 121
 
17321 stevensc 122
                    $page               = intval($this->params()->fromQuery('start', 1), 10);
123
                    $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
124
                    $order =  $this->params()->fromQuery('order', []);
125
                    $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
126
                    $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
127
 
128
                    $fields =  ['name'];
129
                    $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
130
 
131
                    if(!in_array($order_direction, ['ASC', 'DESC'])) {
132
                        $order_direction = 'ASC';
133 efrain 133
                    }
134
 
17321 stevensc 135
 
136
 
137
                    $microlearningSlideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
138
                    $microlearningQuizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
139
                    $paginator = $microlearningQuizMapper->fetchAllDataTableByCompanyId($currentCompany->id,  $search, $page, $records_x_page, $order_field, $order_direction);
140
                    $items = [];
141
                    foreach($paginator as $record)
133 efrain 142
                    {
17321 stevensc 143
 
144
 
17322 stevensc 145
                        $slides = $microlearningSlideMapper->fetchTotalCountByCompanyIdAndQuizId($currentCompany->id, $record->id);
17321 stevensc 146
 
147
                        $status_description = '';
148
                        switch ($record->status)
149
                        {
150
                            case MicrolearningQuiz::STATUS_ACTIVE :
151
                                $status_description = 'LABEL_ACTIVE';
152
                                break;
153
 
154
                            case MicrolearningQuiz::STATUS_INACTIVE :
155
                                $status_description = 'LABEL_INACTIVE';
156
                                break;
157
                        }
158
 
159
                        $check_description = '';
160
                        switch($record->check)
161
                        {
162
                            case MicrolearningQuiz::CHECK_OK :
163
                                $check_description = 'LABEL_WITHOUT_ERRORS';
164
                                break;
165
 
166
                            case MicrolearningQuiz::CHECK_WRONG :
167
                                $check_description = 'LABEL_WITH_ERRORS';
168
                                break;
169
 
170
                            case MicrolearningQuiz::CHECK_PENDING :
171
                                $check_description = 'LABEL_PENDING';
172
                                break;
173
                        }
174
 
175
                        $params = [
176
                            'quiz_id' => $record->uuid
177
                        ];
178
 
179
                        $item = [
180
                            'name' => $record->name,
181
                            'details' => [
182
                                'status' => $status_description,
183
                                'check' => $check_description,
184
                                'points' => $record->points,
185
                                'max_time' => $record->max_time,
186
                                'slides' => $slides,
187
                            ],
188
                            'actions' => [
189
                                'link_question' => $allowQuestion ? $this->url()->fromRoute('microlearning/content/quizzes/questions', $params) : '',
190
                                'link_edit'     => $allowEdit ? $this->url()->fromRoute('microlearning/content/quizzes/edit', $params) : '',
191
                                'link_delete'   => $allowDelete ? $this->url()->fromRoute('microlearning/content/quizzes/delete', $params) : '',
192
                                'link_copy'     => $allowCopy ? $this->url()->fromRoute('microlearning/content/quizzes/copy', $params) : '',
193
                                'link_check'    => $allowCheck ? $this->url()->fromRoute('microlearning/content/quizzes/check', $params) : '',
194
                            ],
195
                        ];
196
 
197
 
198
                        array_push($items, $item);
133 efrain 199
                    }
200
 
17321 stevensc 201
                    $data = [];
202
                    $data['items'] = $items;
203
                    $data['total'] = $paginator->getTotalItemCount();
133 efrain 204
 
17321 stevensc 205
                    if($allowAdd) {
206
                        $data['link_add'] = $this->url()->fromRoute('microlearning/content/quizzes/add');
207
                    }  else {
208
                        $data['link_add'] = '';
209
                    }
210
 
211
                    $response = [
212
                        'success' => true,
213
                        'data' => $data
133 efrain 214
                    ];
215
 
17321 stevensc 216
                    return new JsonModel($response);
217
 
218
 
219
                } else {
220
                    $form = new QuizCreateOrEditForm();
221
 
222
                    $this->layout()->setTemplate('layout/layout-backend.phtml');
223
                    $viewModel = new ViewModel();
224
                    $viewModel->setTemplate('leaders-linked/microlearning-quizzes/index.phtml');
225
                    $viewModel->setVariables([
226
                        'form' => $form,
227
                    ]);
228
                    return $viewModel ;
133 efrain 229
                }
230
 
231
            } else {
17321 stevensc 232
                return new JsonModel([
233
                    'success' => false,
234
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
133 efrain 235
                ]);
17321 stevensc 236
        }
237
        } catch (\Throwable $th) {
238
            $this->logger->err($th->getMessage());
133 efrain 239
            return new JsonModel([
240
                'success' => false,
17323 stevensc 241
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
133 efrain 242
            ]);
243
        }
244
    }
245
 
246
    public function addAction()
247
    {
248
        $request            = $this->getRequest();
249
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
250
        $currentUser        = $currentUserPlugin->getUser();
251
        $currentCompany     = $currentUserPlugin->getCompany();
252
 
253
        if($request->isPost()) {
254
            $formCreateOrEdit = new  QuizCreateOrEditForm();
255
            $formCreateOrEdit->setData($request->getPost()->toArray());
256
 
257
            if($formCreateOrEdit->isValid()) {
258
                $dataPost = (array) $formCreateOrEdit->getData();
259
 
260
                $hydrator = new ObjectPropertyHydrator();
17002 efrain 261
                $quiz = new MicrolearningQuiz();
133 efrain 262
                $hydrator->hydrate($dataPost, $quiz);
263
 
264
                $quiz->company_id = $currentCompany->id;
265
 
17002 efrain 266
                $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
133 efrain 267
                if($quizMapper->insert($quiz)) {
268
 
269
 
270
                    $this->logger->info('Se agrego el quiz ' . $quiz->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
271
 
272
                    $data = [
273
                        'success'   => true,
274
                        'data'   => 'LABEL_RECORD_ADDED'
275
                    ];
276
                } else {
277
                    $data = [
278
                        'success'   => false,
279
                        'data'      => $quizMapper->getError()
280
                    ];
281
 
282
                }
283
 
284
                return new JsonModel($data);
285
 
286
            } else {
287
                $messages = [];
288
                $form_messages = (array) $formCreateOrEdit->getMessages();
289
                foreach($form_messages  as $fieldname => $field_messages)
290
                {
291
 
292
                    $messages[$fieldname] = array_values($field_messages);
293
                }
294
 
295
                return new JsonModel([
296
                    'success'   => false,
297
                    'data'   => $messages
298
                ]);
299
            }
300
 
301
        } else {
302
            $data = [
303
                'success' => false,
304
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
305
            ];
306
 
307
            return new JsonModel($data);
308
        }
309
 
310
        return new JsonModel($data);
311
    }
312
 
313
    /**
314
     *
315
     * Borrar un perfil excepto el público
316
     * @return \Laminas\View\Model\JsonModel
317
     */
318
    public function deleteAction()
319
    {
320
        $request            = $this->getRequest();
321
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
322
        $currentUser        = $currentUserPlugin->getUser();
323
        $currentCompany     = $currentUserPlugin->getCompany();
324
        $quiz_id            = $this->params()->fromRoute('quiz_id');
325
 
17002 efrain 326
        $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
133 efrain 327
        $quiz = $quizMapper->fetchOneByUuid($quiz_id);
328
        if(!$quiz) {
329
            return new JsonModel([
330
                'success'   => false,
331
                'data'   => 'ERROR_FORM_NOT_FOUND'
332
            ]);
333
        }
334
 
335
        if($quiz->company_id != $currentCompany->id) {
336
            return new JsonModel([
337
                'success'   => false,
338
                'data'   => 'ERROR_UNAUTHORIZED'
339
            ]);
340
        }
341
 
342
        if($request->isPost()) {
343
            $result = $quizMapper->delete($quiz);
344
            if($result) {
345
                $this->logger->info('Se borro el cuestionario : ' .  $quiz->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
346
 
347
                $data = [
348
                    'success' => true,
349
                    'data' => 'LABEL_RECORD_DELETED'
350
                ];
351
            } else {
352
 
353
                $data = [
354
                    'success'   => false,
355
                    'data'      => $quizMapper->getError()
356
                ];
357
 
358
                return new JsonModel($data);
359
            }
360
 
361
        } else {
362
            $data = [
363
                'success' => false,
364
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
365
            ];
366
 
367
            return new JsonModel($data);
368
        }
369
 
370
        return new JsonModel($data);
371
    }
372
 
373
 
374
    public function editAction()
375
    {
376
        $request            = $this->getRequest();
377
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
378
        $currentUser        = $currentUserPlugin->getUser();
379
        $currentCompany     = $currentUserPlugin->getCompany();
380
        $quiz_id            = $this->params()->fromRoute('quiz_id');
381
 
17002 efrain 382
        $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
133 efrain 383
        $quiz = $quizMapper->fetchOneByUuid($quiz_id);
384
        if(!$quiz) {
385
            return new JsonModel([
386
                'success'   => false,
387
                'data'   => 'ERROR_FORM_NOT_FOUND'
388
            ]);
389
        }
390
 
391
        if($quiz->company_id != $currentCompany->id) {
392
            return new JsonModel([
393
                'success'   => false,
394
                'data'   => 'ERROR_UNAUTHORIZED'
395
            ]);
396
        }
397
 
398
 
399
        if($request->isGet()) {
400
            $data = [
401
                'success' => true,
402
                'data' => [
403
                    'name' => $quiz->name,
404
                    'text' => $quiz->text,
405
                    'status' => $quiz->status,
406
                    'points' => $quiz->points,
407
                    'minimum_points_required' => $quiz->minimum_points_required,
408
                    'max_time' => $quiz->max_time,
409
                    'failed' => $quiz->failed,
410
 
411
                ]
412
            ];
413
 
414
            return new JsonModel($data);
415
        }
416
        else if($request->isPost()) {
417
            $formCreateOrEdit = new  QuizCreateOrEditForm();
418
            $formCreateOrEdit->setData($request->getPost()->toArray());
419
 
420
            if($formCreateOrEdit->isValid()) {
421
                $dataPost = (array) $formCreateOrEdit->getData();
422
 
423
                $hydrator = new ObjectPropertyHydrator();
424
                $hydrator->hydrate($dataPost, $quiz);
425
 
17002 efrain 426
                $quiz->check = MicrolearningQuiz::CHECK_PENDING;
133 efrain 427
                if($quizMapper->update($quiz)) {
428
 
429
                    $this->logger->info('Se edito el cuestionario ' . $quiz->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
430
 
431
                    $data = [
432
                        'success'   => true,
433
                        'data'   => 'LABEL_RECORD_UPDATED'
434
                    ];
435
                } else {
436
                    $data = [
437
                        'success'   => false,
438
                        'data'      => $quizMapper->getError()
439
                    ];
440
 
441
                }
442
 
443
                return new JsonModel($data);
444
 
445
            } else {
446
                $messages = [];
447
                $form_messages = (array) $formCreateOrEdit->getMessages();
448
                foreach($form_messages  as $fieldname => $field_messages)
449
                {
450
 
451
                    $messages[$fieldname] = array_values($field_messages);
452
                }
453
 
454
                return new JsonModel([
455
                    'success'   => false,
456
                    'data'   => $messages
457
                ]);
458
            }
459
        } else {
460
            $data = [
461
                'success' => false,
462
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
463
            ];
464
 
465
            return new JsonModel($data);
466
        }
467
 
468
        return new JsonModel($data);
469
    }
470
 
471
 
472
    public function checkAction()
473
    {
474
        $request            = $this->getRequest();
475
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
476
        $currentUser        = $currentUserPlugin->getUser();
477
        $currentCompany     = $currentUserPlugin->getCompany();
478
        $quiz_id            = $this->params()->fromRoute('quiz_id');
479
 
17002 efrain 480
        $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
133 efrain 481
        $quiz = $quizMapper->fetchOneByUuid($quiz_id);
482
        if(!$quiz) {
483
            return new JsonModel([
484
                'success'   => false,
485
                'data'   => 'ERROR_QUIZ_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
 
496
         if($request->isPost()) {
17002 efrain 497
            $questionMapper = MicrolearningQuestionMapper::getInstance($this->adapter);
498
            $answerMapper = MicrolearningAnswerMapper::getInstance($this->adapter);
133 efrain 499
 
500
            $total_wrong_questions = 0;
501
            $total_point_questions = 0;
502
 
503
 
504
            $questions = $questionMapper->fetchAllByQuizId($quiz->id);
505
 
506
            foreach($questions as $question)
507
            {
508
                $total_point_questions += $question->points;
509
                $answers = $answerMapper->fetchAllByQuizIdAndQuestionId($question->quiz_id, $question->id);
510
 
17002 efrain 511
                if($question->type == MicrolearningQuestion::TYPE_SINGLE_SELECTION ) {
133 efrain 512
                    $number_correct     = 0;
513
                    $number_incorrect   = 0;
514
                    foreach($answers as $answer)
515
                    {
17002 efrain 516
                        if($answer->correct == MicrolearningAnswer::CORRECT_YES) {
133 efrain 517
                            $number_correct++;
518
                        } else {
519
                            $number_incorrect++;
520
                        }
521
                    }
522
 
523
                    if($number_correct == 1 && $number_incorrect >= 1 ) {
17002 efrain 524
                        $check_question = MicrolearningQuestion::CHECK_OK;
133 efrain 525
                    } else {
17002 efrain 526
                        $check_question = MicrolearningQuestion::CHECK_WRONG;
133 efrain 527
                    }
17002 efrain 528
                } else if($question->type == MicrolearningQuestion::TYPE_MULTIPLE_SELECTION ) {
133 efrain 529
                    $number_correct     = 0;
530
                    $number_incorrect   = 0;
531
                    $points = 0;
532
 
533
                    foreach($answers as $answer)
534
                    {
535
                        $points += $answer->points;
17002 efrain 536
                        if($answer->correct == MicrolearningAnswer::CORRECT_YES) {
133 efrain 537
                            $number_correct++;
538
                        } else {
539
                            $number_incorrect++;
540
                        }
541
                    }
542
 
543
                    if($number_correct < 2 || $number_incorrect < 1) {
17002 efrain 544
                        $check_question = MicrolearningQuestion::CHECK_WRONG;
133 efrain 545
                    } else {
546
                        if($points == $question->points) {
17002 efrain 547
                            $check_question = MicrolearningQuestion::CHECK_OK;
133 efrain 548
                        } else {
17002 efrain 549
                            $check_question = MicrolearningQuestion::CHECK_WRONG;
133 efrain 550
                        }
551
 
552
                    }
553
                } else {
17002 efrain 554
                    $check_question = MicrolearningQuestion::CHECK_OK;
133 efrain 555
                }
556
 
557
 
17002 efrain 558
                if($check_question == MicrolearningQuestion::CHECK_WRONG) {
559
                    $quiz->check = MicrolearningQuiz::CHECK_WRONG;
133 efrain 560
                }
561
 
562
                $question->check = $check_question;
563
                $questionMapper->update($question);
564
            }
565
 
17002 efrain 566
            if($quiz->check == MicrolearningQuiz::CHECK_PENDING) {
567
                $quiz->check = $quiz->points == $total_point_questions ? MicrolearningQuiz::CHECK_OK : MicrolearningQuiz::CHECK_WRONG;
133 efrain 568
            }
569
 
570
 
571
            $quizMapper->update($quiz);
572
 
17002 efrain 573
            if($quiz->check ==  MicrolearningQuiz::CHECK_OK) {
133 efrain 574
                $this->logger->info('Se comprobo el cuestionario ' . $quiz->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
575
 
576
                $data = [
577
                    'success'   => true,
578
                    'data'   => 'LABEL_QUIZ_CHECK_OK'
579
                ];
580
            } else {
581
                $messages = [];
582
                if($total_wrong_questions == 1) {
583
                    array_push($messages, '1 LABEL_QUESTION_CHECK_WRONG ');
584
                } else if($total_wrong_questions > 1) {
585
                    array_push($messages,  $total_wrong_questions . 'LABEL_QUESTIONS_CHECK_WRONG ');
586
                }
587
 
588
                if(!$messages) {
589
                    $messages = 'LABEL_QUIZ_CHECK_WRONG';
590
                } else {
591
                    $messages = implode(', ', $messages);
592
                }
593
 
594
                $data = [
595
                    'success'   => false,
596
                    'data'   => $messages
597
                ];
598
            }
599
 
600
 
601
        } else {
602
            $data = [
603
                'success' => false,
604
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
605
            ];
606
 
607
            return new JsonModel($data);
608
        }
609
 
610
        return new JsonModel($data);
611
    }
612
 
613
 
614
 
615
    public function copyAction()
616
    {
617
        $request            = $this->getRequest();
618
        $currentUserPlugin  = $this->plugin('currentUserPlugin');
619
        $currentUser        = $currentUserPlugin->getUser();
620
        $currentCompany     = $currentUserPlugin->getCompany();
621
        $quiz_id            = $this->params()->fromRoute('quiz_id');
622
 
17002 efrain 623
        $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
133 efrain 624
        $quiz = $quizMapper->fetchOneByUuid($quiz_id);
625
        if(!$quiz) {
626
            return new JsonModel([
627
                'success'   => false,
628
                'data'   => 'ERROR_QUIZ_NOT_FOUND'
629
            ]);
630
        }
631
 
632
        if($quiz->company_id != $currentCompany->id) {
633
            return new JsonModel([
634
                'success'   => false,
635
                'data'   => 'ERROR_UNAUTHORIZED'
636
            ]);
637
        }
638
 
639
        if($request->isPost()) {
640
 
17002 efrain 641
            $quizClone = new MicrolearningQuiz();
133 efrain 642
 
17002 efrain 643
            $quizClone->check = MicrolearningQuiz::CHECK_PENDING;
644
            $quizClone->status = MicrolearningQuiz::STATUS_INACTIVE;
133 efrain 645
            $quizClone->company_id = $quiz->company_id;
646
            $quizClone->failed = $quiz->failed;
647
            $quizClone->name = 'COPIA - ' . $quiz->name;
648
            $quizClone->max_time = $quiz->max_time;
649
            $quizClone->minimum_points_required = $quiz->minimum_points_required;
650
            $quizClone->points = $quiz->points;
651
            $quizClone->text = $quiz->text;
652
 
653
            if($quizMapper->insert($quizClone)) {
654
                $this->logger->info('Se copio el quiz ' . $quizClone->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
655
 
17002 efrain 656
                $questionMapper = MicrolearningQuestionMapper::getInstance($this->adapter);
657
                $answerMapper = MicrolearningAnswerMapper::getInstance($this->adapter);
133 efrain 658
 
659
                $questions = $questionMapper->fetchAllByQuizId($quiz->id);
660
                foreach($questions as $question)
661
                {
17002 efrain 662
                    $questionClone = new MicrolearningQuestion();
663
                    $questionClone->check = MicrolearningQuestion::CHECK_PENDING;
133 efrain 664
                    $questionClone->company_id = $question->company_id;
665
                    $questionClone->maxlength = $question->maxlength;
666
                    $questionClone->points = $question->points;
667
                    $questionClone->quiz_id = $quizClone->id;
668
                    $questionClone->text = $question->text;
669
                    $questionClone->type = $question->type;
670
 
671
                    if($questionMapper->insert($questionClone)) {
672
                        $answers = $answerMapper->fetchAllByQuizIdAndQuestionId($question->quiz_id, $question->id);
673
                        foreach($answers as $answer)
674
                        {
17002 efrain 675
                            $answerClone = new MicrolearningAnswer();
133 efrain 676
                            $answerClone->company_id = $answer->company_id;
677
                            $answerClone->correct = $answer->correct;
678
                            $answerClone->points = $answer->points;
679
                            $answerClone->question_id = $questionClone->id;
680
                            $answerClone->quiz_id = $quizClone->id;
681
                            $answerClone->text = $answer->text;
682
 
683
                            if(!$answerMapper->insert($answerClone)) {
684
                                $response = [
685
                                    'success'   => false,
686
                                    'data'   => $answerMapper->getError()
687
                                ];
688
                            }
689
                        }
690
                    } else {
691
                        $response = [
692
                            'success'   => false,
693
                            'data'   => $questionMapper->getError()
694
                        ];
695
                    }
696
                }
697
 
698
                $response = [
699
                    'success'   => true,
700
                    'data'   => 'LABEL_RECORD_ADDED'
701
                ];
702
            } else {
703
                $response = [
704
                    'success'   => false,
705
                    'data'   => $quizMapper->getError()
706
                ];
707
 
708
            }
709
 
710
        } else {
711
            $response = [
712
                'success' => false,
713
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
714
            ];
715
        }
716
 
717
        return new JsonModel($response);
718
    }
719
}