Proyectos de Subversion LeadersLinked - Backend

Rev

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