Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16768 | Rev 16943 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
16768 efrain 7
 
1 www 8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
 
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
15
use LeadersLinked\Library\Image;
16
 
17
 
18
use LeadersLinked\Mapper\CompanyMapper;
19
use LeadersLinked\Mapper\CompanyMicrolearningTopicMapper;
20
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleMapper;
21
use LeadersLinked\Mapper\CompanyMicrolearningSlideMapper;
22
use LeadersLinked\Model\CompanyMicrolearningSlide;
23
use LeadersLinked\Model\VideoConvert;
24
use LeadersLinked\Mapper\VideoConvertMapper;
25
use LeadersLinked\Form\SlideForm;
26
use LeadersLinked\Form\SlideTextAddForm;
27
use LeadersLinked\Form\SlideTextEditForm;
28
use LeadersLinked\Form\SlideImageAddForm;
29
use LeadersLinked\Form\SlideImageEditForm;
30
use LeadersLinked\Form\SlideVideoAddForm;
31
use LeadersLinked\Form\SlideDocumentAddForm;
32
use LeadersLinked\Form\SlideDocumentEditForm;
33
use LeadersLinked\Form\SlideAudioAddForm;
34
use LeadersLinked\Form\SlideAudioEditForm;
35
use LeadersLinked\Form\SlideQuizzAddForm;
36
use LeadersLinked\Form\SlideQuizzEditForm;
37
use LeadersLinked\Form\SlideVideoEditForm;
38
 
39
 
40
class MicrolearningSlideController extends AbstractActionController
41
{
42
    /**
43
     *
16769 efrain 44
     * @var \Laminas\Db\Adapter\AdapterInterface
1 www 45
     */
46
    private $adapter;
47
 
48
    /**
49
     *
16769 efrain 50
     * @var \LeadersLinked\Cache\CacheInterface
1 www 51
     */
16769 efrain 52
    private $cache;
53
 
54
 
55
    /**
56
     *
57
     * @var \Laminas\Log\LoggerInterface
58
     */
1 www 59
    private $logger;
60
 
61
    /**
62
     *
63
     * @var array
64
     */
65
    private $config;
66
 
16769 efrain 67
 
1 www 68
    /**
69
     *
16769 efrain 70
     * @var \Laminas\Mvc\I18n\Translator
71
     */
72
    private $translator;
73
 
74
 
75
    /**
76
     *
77
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
78
     * @param \LeadersLinked\Cache\CacheInterface $cache
79
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
1 www 80
     * @param array $config
16769 efrain 81
     * @param \Laminas\Mvc\I18n\Translator $translator
1 www 82
     */
16769 efrain 83
    public function __construct($adapter, $cache, $logger, $config, $translator)
1 www 84
    {
16769 efrain 85
        $this->adapter      = $adapter;
86
        $this->cache        = $cache;
87
        $this->logger       = $logger;
88
        $this->config       = $config;
89
        $this->translator   = $translator;
1 www 90
    }
91
 
92
    /**
93
     *
94
     * Generación del listado de perfiles
95
     * {@inheritDoc}
96
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
97
     */
98
    public function indexAction()
99
    {
100
        $currentUserPlugin = $this->plugin('currentUserPlugin');
101
        $currentUser = $currentUserPlugin->getUser();
102
        $currentCompany = $currentUserPlugin->getCompany();
103
 
104
        $request = $this->getRequest();
105
        if($request->isGet()) {
106
 
107
 
108
            $headers  = $request->getHeaders();
109
 
110
            $isJson = false;
111
            if($headers->has('Accept')) {
112
                $accept = $headers->get('Accept');
113
 
114
                $prioritized = $accept->getPrioritized();
115
 
116
                foreach($prioritized as $key => $value) {
117
                    $raw = trim($value->getRaw());
118
 
119
                    if(!$isJson) {
120
                        $isJson = strpos($raw, 'json');
121
                    }
122
 
123
                }
124
            }
125
 
126
            if($isJson) {
127
 
128
 
16766 efrain 129
                $topic_uuid     = Functions::sanitizeFilterString($this->params()->fromQuery('topic_uuid'));
130
                $capsule_uuid   = Functions::sanitizeFilterString($this->params()->fromQuery('capsule_uuid'));
1 www 131
 
132
                $data = [
133
                    'link_add' => '',
134
                    'items' => [] ,
135
                    'total' => 0,
136
 
137
                ];
138
 
139
 
140
                if(!$topic_uuid) {
141
                    return new JsonModel([
142
                        'success' => true,
143
                        'data' => $data
144
                    ]);
145
 
146
                }
147
 
148
 
149
                $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
150
                $topic = $topicMapper->fetchOneByUuid($topic_uuid);
151
                if(!$topic) {
152
                    return new JsonModel([
153
                        'success' => true,
154
                        'data' => 'ERROR_TOPIC_NOT_FOUND'
155
                    ]);
156
                }
157
 
158
                if($topic->company_id != $currentCompany->id) {
159
                    return new JsonModel([
160
                        'success' => true,
161
                        'data' => 'ERROR_UNAUTHORIZED'
162
                    ]);
163
                }
164
 
165
 
166
 
167
                if(!$capsule_uuid) {
168
                    $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
169
                    $records = $capsuleMapper->fetchAllByCompanyIdAndTopicId($topic->company_id, $topic->id);
170
 
171
                    $capsules = [];
172
                    foreach($records as $record)
173
                    {
174
                        if(!$capsule_uuid) {
175
                            $capsule_uuid = $record->uuid;
176
                        }
177
 
178
                        $capsules[ $record->uuid ] = $record->name;
179
                    }
180
 
181
                    $data['capsules']  = $capsules;
182
                }
183
 
184
                if(!$capsule_uuid) {
185
                    return new JsonModel([
186
                        'success' => true,
187
                        'data' => $data
188
                    ]);
189
 
190
                }
191
 
192
                $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
193
                $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
194
 
195
 
196
 
197
                if(!$capsule) {
198
                    return new JsonModel([
199
                        'success' => true,
200
                        'data' => 'ERROR_CAPSULE_NOT_FOUND'
201
                    ]);
202
                }
203
 
204
                if($capsule->topic_id != $topic->id) {
205
                    return new JsonModel([
206
                        'success' => true,
207
                        'data' => 'ERROR_UNAUTHORIZED'
208
                    ]);
209
                }
210
 
211
                $search = $this->params()->fromQuery('search', []);
16766 efrain 212
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
1 www 213
 
214
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
7065 nelberth 215
                $page               = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
1 www 216
                $order =  $this->params()->fromQuery('order', []);
217
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 218
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
1 www 219
 
220
                $fields =  ['name'];
221
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
222
 
223
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
224
                    $order_direction = 'ASC';
225
                }
226
 
227
 
228
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
229
                $allowAdd = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/slides/add');
230
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/slides/edit');
231
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'microlearning/content/slides/delete');
232
 
233
                $slideMapper = CompanyMicrolearningSlideMapper::getInstance($this->adapter);
234
                $paginator = $slideMapper->fetchAllDataTableByCompanyIdAndTopicIdAndCapsuleId($currentCompany->id, $topic->id, $capsule->id,  $search, $page, $records_x_page, $order_field, $order_direction);
235
 
236
                $records = $paginator->getCurrentItems();
237
 
238
 
239
                $items = [];
240
                foreach($records as $record)
241
                {
242
 
243
                    $params = [
244
                        'topic_uuid'    => $topic->uuid,
245
                        'capsule_uuid'  => $capsule->uuid,
246
                        'slide_uuid'    => $record->uuid,
247
 
248
                    ];
249
 
250
 
251
                    $item = [
252
 
253
                        'name' => $record->name,
254
                        'details' => [
255
                            'type' => '',
256
                         ],
257
                         'media' => [
258
                            'image' => '',
259
                            'audio' => '',
260
                            'video' => '',
261
                            'document' => '',
262
                            'text'
263
                        ],
264
 
265
                        'actions' => [
266
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('microlearning/content/slides/edit', $params)  : '',
267
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('microlearning/content/slides/delete', $params )  : '',
268
                         ],
269
 
270
                    ];
271
 
272
 
273
                    switch($record->type)
274
                    {
275
                        case CompanyMicrolearningSlide::TYPE_AUDIO :
276
 
277
                            $item['media']['audio'] =  $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->file ]);
278
                            $item['media']['image'] = $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->background ]);
279
                            $item['details']['type'] = 'LABEL_AUDIO';
280
                            break;
281
 
282
                        case CompanyMicrolearningSlide::TYPE_VIDEO :
283
                            $item['media']['video'] =  $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->file ]);
284
                            $item['media']['image'] = $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->background ]);
285
                            $item['details']['type'] = 'LABEL_VIDEO';
286
                            break;
287
 
288
                        case CompanyMicrolearningSlide::TYPE_DOCUMENT :
289
                            $item['media']['document'] =  $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->file ]);
290
                            $item['media']['image'] = $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->background ]);
291
                            $item['details']['type'] = 'LABEL_DOCUMENT';
292
                            break;
293
 
294
                        case CompanyMicrolearningSlide::TYPE_TEXT :
295
                            $item['media']['image'] = $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->background ]);
296
                            $item['media']['text'] = $this->url()->fromRoute('microlearning/content/slides/text', $params);
297
                            $item['details']['type'] = 'LABEL_TEXT';
298
                            break;
299
 
300
                        case CompanyMicrolearningSlide::TYPE_QUIZ :
301
                            $item['media']['image'] = $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->background ]);
302
                            $item['details']['type'] = 'LABEL_QUIZ';
303
                            break;
304
 
305
                        case CompanyMicrolearningSlide::TYPE_IMAGE :
306
                            $item['media']['image'] = $this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $record->uuid, 'filename' =>  $record->file ]);
307
                            $item['details']['type'] = 'LABEL_IMAGE';
308
                            break;
309
 
310
                    }
311
 
312
 
313
                    array_push($items, $item);
314
 
315
                }
316
 
317
                if($allowAdd && $topic && $capsule) {
318
                    $data['link_add'] = $this->url()->fromRoute('microlearning/content/slides/add', ['topic_uuid' => $topic->uuid, 'capsule_uuid' => $capsule->uuid ]);
319
                }
320
                $data['items'] = $items;
321
                $data['total'] = $paginator->getTotalItemCount();
322
 
323
                return new JsonModel([
324
                    'success' => true,
325
                    'data' => $data
326
                ]);
327
 
328
            } else {
329
                $image_size = $this->config['leaderslinked.image_sizes.microlearning_image_upload'];
330
 
331
                $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
332
                $topics = $topicMapper->fetchAllByCompanyId($currentCompany->id);
333
 
334
                if($topics) {
335
                    $topic_id = $topics[0]->id;
336
                }  else {
337
                    $topic_id = 0;
338
                }
339
 
340
 
341
                $form = new SlideForm($this->adapter,  $currentCompany->id, $topic_id);
342
                $formTextAdd = new SlideTextAddForm();
343
                $formTextEdit = new SlideTextEditForm();
344
                $formImageAdd = new SlideImageAddForm();
345
                $formImageEdit = new SlideImageEditForm();
346
                $formVideoAdd = new SlideVideoAddForm();
347
                $formVideoEdit = new SlideVideoEditForm();
348
                $formDocumentAdd = new SlideDocumentAddForm();
349
                $formDocumentEdit = new SlideDocumentEditForm();
350
                $formAudioAdd = new SlideAudioAddForm();
351
                $formAudioEdit = new SlideAudioEditForm();
352
                $formQuizzAdd = new SlideQuizzAddForm($this->adapter, $currentCompany->id);
353
                $formQuizzEdit = new SlideQuizzEditForm($this->adapter, $currentCompany->id);
354
 
355
                $this->layout()->setTemplate('layout/layout-backend.phtml');
356
                $viewModel = new ViewModel();
357
                $viewModel->setTemplate('leaders-linked/microlearning-slides/index.phtml');
358
                $viewModel->setVariables([
359
                    'form' => $form,
360
                    'formTextAdd' => $formTextAdd,
361
                    'formTextEdit' => $formTextEdit,
362
                    'formImageAdd' => $formImageAdd,
363
                    'formImageEdit' => $formImageEdit,
364
                    'formVideoAdd' => $formVideoAdd,
365
                    'formVideoEdit' => $formVideoEdit,
366
                    'formDocumentAdd' => $formDocumentAdd,
367
                    'formDocumentEdit' => $formDocumentEdit,
368
                    'formAudioAdd' => $formAudioAdd,
369
                    'formAudioEdit' => $formAudioEdit,
370
                    'formQuizzAdd' => $formQuizzAdd,
371
                    'formQuizzEdit' => $formQuizzEdit,
372
                    'image_size' => $image_size,
373
 
374
                ]);
375
                return $viewModel ;
376
            }
377
        }
378
 
379
        return new JsonModel([
380
            'success' => false,
381
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
382
        ]);
383
 
384
 
385
 
386
 
387
 
388
 
389
 
390
    }
391
 
392
    public function addAction()
393
    {
394
 
395
        $currentUserPlugin = $this->plugin('currentUserPlugin');
396
        $currentUser = $currentUserPlugin->getUser();
397
        $currentCompany = $currentUserPlugin->getCompany();
398
 
399
        $request    = $this->getRequest();
400
        $topic_uuid   = $this->params()->fromRoute('topic_uuid');
401
        $capsule_uuid = $this->params()->fromRoute('capsule_uuid');
402
 
403
 
404
 
405
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
406
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
407
        if(!$topic) {
408
            return new JsonModel([
409
                'success'   => false,
410
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
411
            ]);
412
        }
413
 
414
        if($topic->company_id != $currentCompany->id) {
415
            return new JsonModel([
416
                'success'   => false,
417
                'data'   => 'ERROR_UNAUTHORIZED'
418
            ]);
419
        }
420
 
421
 
422
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
423
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
424
        if(!$capsule) {
425
            return new JsonModel([
426
                'success'   => false,
427
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
428
            ]);
429
        }
430
 
431
        if($capsule->topic_id != $topic->id) {
432
            return new JsonModel([
433
                'success'   => false,
434
                'data'   => 'ERROR_UNAUTHORIZED'
435
            ]);
436
        }
437
 
438
 
439
        if($request->isPost()) {
440
 
441
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
442
 
443
            switch($dataPost['type'])
444
            {
445
                case CompanyMicrolearningSlide::TYPE_AUDIO :
446
                    $form = new SlideAudioAddForm();
447
                    break;
448
 
449
                case CompanyMicrolearningSlide::TYPE_VIDEO :
450
                    $form = new SlideVideoAddForm();
451
                    break;
452
 
453
                case CompanyMicrolearningSlide::TYPE_DOCUMENT:
454
                    $form = new SlideDocumentAddForm();
455
                    break;
456
 
457
                case CompanyMicrolearningSlide::TYPE_IMAGE :
458
                    $form = new SlideImageAddForm();
459
                    break;
460
 
461
 
462
                case CompanyMicrolearningSlide::TYPE_QUIZ:
463
                    $form = new SlideQuizzAddForm($this->adapter, $currentCompany->id);
464
                    break;
465
 
466
                default :
467
                    $form = new SlideTextAddForm();
468
                    break;
469
            }
470
 
471
            $form->setData($dataPost);
472
 
473
            if($form->isValid()) {
474
                $dataPost = (array) $form->getData();
475
 
476
                $hydrator = new ObjectPropertyHydrator();
477
                $slide = new CompanyMicrolearningSlide();
478
                $hydrator->hydrate($dataPost, $slide);
479
 
480
                $slide->company_id = $topic->company_id;
481
                $slide->topic_id = $topic->id;
482
                $slide->capsule_id = $capsule->id;
483
                $slide->file = null;
484
                $slide->background = null;
6194 nelberth 485
 
1 www 486
                $slideMapper = CompanyMicrolearningSlideMapper::getInstance($this->adapter);
487
                if($slideMapper->insert($slide)) {
488
                    $slide = $slideMapper->fetchOne($slide->id);
489
 
490
                    $files = $this->getRequest()->getFiles()->toArray();
6196 nelberth 491
 
6498 nelberth 492
 
6196 nelberth 493
 
494
                    if($slide->type == CompanyMicrolearningSlide::TYPE_IMAGE) {
6498 nelberth 495
                        $fileBase64Content = $this->params()->fromPost('file');
6197 nelberth 496
 
497
                        try {
498
 
499
                            $fileBase64Content = base64_decode($fileBase64Content);
7115 nelberth 500
                            $filename      = 'slide-' .uniqid() . '.jpg';
6204 nelberth 501
                            $tmp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp';
502
                            $target_path = $this->config['leaderslinked.fullpath.microlearning_slide'] .  $slide->uuid;
503
                            if(!file_exists($target_path)) {
504
                                mkdir($target_path, 0755, true);
6197 nelberth 505
                            }
6204 nelberth 506
                            $tmp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $filename;
507
                                file_put_contents($tmp_filename, $fileBase64Content);
6197 nelberth 508
 
509
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
510
 
6204 nelberth 511
                            $crop_to_dimensions = true;
512
                            if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions )) {
513
                                $slide->file = basename($filename);
514
                                $slideMapper->update($slide);
6197 nelberth 515
                            }
516
                        } catch(\Throwable $e) {
517
                            error_log($e->getTraceAsString());
6499 nelberth 518
                        }
6196 nelberth 519
 
520
                    }
521
 
522
 
523
 
524
 
1 www 525
                    $target_path = $this->config['leaderslinked.fullpath.microlearning_slide'] .  $slide->uuid;
526
                    if(!file_exists($target_path)) {
527
                        mkdir($target_path, 0755, true);
528
                    }
529
 
530
 
531
                    if(isset($files['file']) && empty($files['file']['error'])) {
532
                        $tmp_filename       = $files['file']['tmp_name'];
533
                        $original_filename  = trim(strtolower($files['file']['name']));
534
 
535
 
536
 
537
 
538
 
539
                        if($slide->file) {
540
                            if(!image ::delete($target_path, $slide->file)) {
541
                                return new JsonModel([
542
                                    'success'   => false,
543
                                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
544
                                ]);
545
                            }
546
                        }
547
 
548
                        if($slide->type == CompanyMicrolearningSlide::TYPE_DOCUMENT) {
549
                            try {
550
 
551
                                $parts = explode('.', $original_filename);
552
                                $filename = 'document-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
553
 
554
                                $full_filename = $target_path  . DIRECTORY_SEPARATOR .$filename;
555
                                if(move_uploaded_file($tmp_filename, $full_filename)) {
556
                                    $slide->file = basename($full_filename);
557
                                    $slideMapper->update($slide);
558
                                }
559
                            } catch(\Throwable $e) {
560
                                error_log($e->getTraceAsString());
561
                            }
562
                        }
563
 
564
                        if($slide->type == CompanyMicrolearningSlide::TYPE_AUDIO) {
565
                            try {
566
 
567
                                $parts = explode('.', $original_filename);
568
                                $filename = 'audio-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
569
 
570
                                $full_filename      = $target_path  . DIRECTORY_SEPARATOR .$filename;
571
                                if(move_uploaded_file($tmp_filename , $full_filename)) {
572
 
573
 
574
                                    $generateFileName   = substr($filename, 0, strrpos($filename, '.'));
575
                                    $generateFile       =  $target_path  . DIRECTORY_SEPARATOR . $generateFileName .  '.mp3';
576
                                    $cmd                = "/usr/bin/ffmpeg -i $full_filename -b:a 320000 $generateFile";
577
                                    exec($cmd);
578
 
579
                                    $slide->file = basename($full_filename);
580
                                    $slideMapper->update($slide);
581
                                }
582
 
583
                            } catch(\Throwable $e) {
584
                                error_log($e->getTraceAsString());
585
                            }
586
                        }
587
 
588
                        if($slide->type == CompanyMicrolearningSlide::TYPE_VIDEO) {
589
                            try {
590
                                $parts = explode('.', $original_filename);
591
                                $filename = 'video-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
592
 
593
                                $full_filename      = $target_path  . DIRECTORY_SEPARATOR .$filename;
594
 
595
                                if(move_uploaded_file($tmp_filename , $full_filename)) {
596
 
597
                                    $videoConvert = new VideoConvert();
598
                                    $videoConvert->filename = $full_filename;
599
                                    $videoConvert->type = VideoConvert::TYPE_MICRO_LEARNING;
600
 
601
                                    $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
602
                                    $videoConvertMapper->insert($videoConvert);
603
 
604
                                    $slide->file = basename($full_filename);
605
                                    $slideMapper->update($slide);
606
                                }
607
 
608
                            } catch(\Throwable $e) {
609
                                echo $e->getMessage();
610
 
611
                                error_log($e->getTraceAsString());
612
                            }
613
                        }
614
 
6187 nelberth 615
 
1 www 616
                    }
6594 nelberth 617
 
618
                        $fileBase64Content = $this->params()->fromPost('background');
619
                        $target_path = $this->config['leaderslinked.fullpath.microlearning_slide'] .  $slide->uuid;
620
                        if($slide->background) {
621
                            if(!image ::delete($target_path, $slide->background)) {
622
                                return new JsonModel([
623
                                    'success'   => false,
624
                                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
625
                                ]);
626
                            }
627
                        }
628
 
629
                        try {
630
 
631
                            $fileBase64Content = base64_decode($fileBase64Content);
7115 nelberth 632
                            $filename      = 'background-' .uniqid() . '.jpg';
6594 nelberth 633
                            $tmp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp';
634
                            if(!file_exists($target_path)) {
635
                                mkdir($target_path, 0755, true);
636
                            }
637
                            $tmp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $filename;
638
                                file_put_contents($tmp_filename, $fileBase64Content);
639
 
640
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
641
 
642
                            $crop_to_dimensions = true;
643
                            if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions )) {
644
                                $slide->background = basename($filename);
645
                                $slideMapper->update($slide);
646
                            }
647
                        } catch(\Throwable $e) {
648
                            error_log($e->getTraceAsString());
649
                        }
650
 
1 www 651
 
6594 nelberth 652
                    /*if(isset($files['background']) && empty($files['background']['error'])) {
1 www 653
                        if($slide->background) {
654
                            if(!image ::delete($target_path, $slide->background)) {
655
                                return new JsonModel([
656
                                    'success'   => false,
657
                                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
658
                                ]);
659
                            }
660
                        }
661
 
662
                        $tmp_filename  = $files['background']['tmp_name'];
663
 
664
                        try {
665
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
666
 
667
                            $filename = 'background-' .uniqid() . '.png';
668
                            $crop_to_dimensions = true;
669
                            if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
670
                                $slide->background = $filename;
671
                                $slideMapper->update($slide);
672
                            }
673
                        } catch(\Throwable $e) {
674
                            error_log($e->getTraceAsString());
675
                        }
6594 nelberth 676
                    }*/
1 www 677
 
678
                    $this->logger->info('Se agrego la diapositiva ' . $slide->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
679
 
680
                    $data = [
681
                        'success'   => true,
682
                        'data'   => 'LABEL_RECORD_ADDED'
683
                    ];
684
                } else {
685
                    $data = [
686
                        'success'   => false,
687
                        'data'      => $topicMapper->getError()
688
                    ];
689
 
690
                }
691
 
692
                return new JsonModel($data);
693
 
694
            } else {
695
                $messages = [];
696
                $form_messages = (array) $form->getMessages();
697
                foreach($form_messages  as $fieldname => $field_messages)
698
                {
699
 
700
                    $messages[$fieldname] = array_values($field_messages);
701
                }
702
 
703
                return new JsonModel([
704
                    'success'   => false,
705
                    'data'   => $messages
706
                ]);
707
            }
708
 
709
        } else {
710
            $data = [
711
                'success' => false,
712
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
713
            ];
714
 
715
            return new JsonModel($data);
716
        }
717
 
718
        return new JsonModel($data);
719
    }
720
 
721
    /**
722
     *
723
     * Borrar un perfil excepto el público
724
     * @return \Laminas\View\Model\JsonModel
725
     */
726
    public function deleteAction()
727
    {
728
        $currentUserPlugin = $this->plugin('currentUserPlugin');
729
        $currentUser = $currentUserPlugin->getUser();
730
        $currentCompany = $currentUserPlugin->getCompany();
731
 
732
        $request    = $this->getRequest();
733
        $topic_uuid   = $this->params()->fromRoute('topic_uuid');
734
        $capsule_uuid = $this->params()->fromRoute('capsule_uuid');
735
        $slide_uuid = $this->params()->fromRoute('slide_uuid');
736
 
737
 
738
 
739
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
740
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
741
        if(!$topic) {
742
            return new JsonModel([
743
                'success'   => false,
744
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
745
            ]);
746
        }
747
 
748
        if($topic->company_id != $currentCompany->id) {
749
            return new JsonModel([
750
                'success'   => false,
751
                'data'   => 'ERROR_UNAUTHORIZED'
752
            ]);
753
        }
754
 
755
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
756
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
757
        if(!$capsule) {
758
            return new JsonModel([
759
                'success'   => false,
760
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
761
            ]);
762
        }
763
 
764
        if($capsule->topic_id != $topic->id) {
765
            return new JsonModel([
766
                'success'   => false,
767
                'data'   => 'ERROR_UNAUTHORIZED'
768
            ]);
769
        }
770
 
771
        $slideMapper = CompanyMicrolearningSlideMapper::getInstance($this->adapter);
772
        $slide = $slideMapper->fetchOneByUuid($slide_uuid);
773
        if(!$slide) {
774
            return new JsonModel([
775
                'success'   => false,
776
                'data'   => 'ERROR_SLIDE_NOT_FOUND'
777
            ]);
778
        }
779
 
780
        if($slide->capsule_id != $capsule->id) {
781
            return new JsonModel([
782
                'success'   => false,
783
                'data'   => 'ERROR_UNAUTHORIZED'
784
            ]);
785
        }
786
 
787
        if($request->isPost()) {
788
 
789
            $result =  $slideMapper->delete($slide);
790
            if($result) {
791
                $this->logger->info('Se borro la diapositiva : ' .  $capsule->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
792
                try {
793
                    $target_path = $this->config['leaderslinked.fullpath.microlearning_slide'] . $slide->uuid;
794
                    if(file_exists($target_path)) {
795
                        Functions::rmDirRecursive($target_path);
796
                    }
797
                } catch(\Throwable $e) {
798
                    error_log($e->getTraceAsString());
799
                }
800
 
801
 
802
                $data = [
803
                    'success' => true,
804
                    'data' => 'LABEL_RECORD_DELETED'
805
                ];
806
            } else {
807
 
808
                $data = [
809
                    'success'   => false,
810
                    'data'      => $capsuleMapper->getError()
811
                ];
812
 
813
                return new JsonModel($data);
814
            }
815
 
816
        } else {
817
            $data = [
818
                'success' => false,
819
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
820
            ];
821
 
822
            return new JsonModel($data);
823
        }
824
 
825
        return new JsonModel($data);
826
    }
827
 
828
 
829
    public function editAction()
830
    {
831
        $currentUserPlugin = $this->plugin('currentUserPlugin');
832
        $currentUser = $currentUserPlugin->getUser();
833
        $currentCompany = $currentUserPlugin->getCompany();
834
 
835
        $request    = $this->getRequest();
836
        $topic_uuid   = $this->params()->fromRoute('topic_uuid');
837
        $capsule_uuid = $this->params()->fromRoute('capsule_uuid');
838
        $slide_uuid   = $this->params()->fromRoute('slide_uuid');
839
 
840
 
841
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
842
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
843
        if(!$topic) {
844
            return new JsonModel([
845
                'success'   => false,
846
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
847
            ]);
848
        }
849
 
850
        if($topic->company_id != $currentCompany->id) {
851
            return new JsonModel([
852
                'success'   => false,
853
                'data'   => 'ERROR_UNAUTHORIZED'
854
            ]);
855
        }
856
 
857
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
858
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
859
        if(!$capsule) {
860
            return new JsonModel([
861
                'success'   => false,
862
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
863
            ]);
864
        }
865
 
866
        if($capsule->topic_id != $topic->id) {
867
            return new JsonModel([
868
                'success'   => false,
869
                'data'   => 'ERROR_UNAUTHORIZED'
870
            ]);
871
        }
872
 
873
 
874
        $slideMapper = CompanyMicrolearningSlideMapper::getInstance($this->adapter);
875
        $slide = $slideMapper->fetchOneByUuid($slide_uuid);
876
        if(!$slide) {
877
            return new JsonModel([
878
                'success'   => false,
879
                'data'   => 'ERROR_SLIDE_NOT_FOUND'
880
            ]);
881
        }
882
 
883
        if($slide->capsule_id != $capsule->id) {
884
            return new JsonModel([
885
                'success'   => false,
886
                'data'   => 'ERROR_UNAUTHORIZED'
887
            ]);
888
        }
889
 
890
        if($request->isGet()) {
6647 nelberth 891
            $sliderFile='';
6648 nelberth 892
            if($slide->type=='image'){
6647 nelberth 893
                $sliderFile=$this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $slide->uuid, 'filename' => $slide->file]);
894
            }
1 www 895
            $data = [
896
                'success' => true,
897
                'data' => [
898
                    'name' => $slide->name,
899
                    'type' => $slide->type,
900
                    'description' => $slide->description,
901
                    'order' => $slide->order,
6509 nelberth 902
                    'quiz_id' => $slide->quiz_id,
6647 nelberth 903
                    'file'=> $sliderFile,
6595 nelberth 904
                    'background' =>$this->url()->fromRoute('storage', ['type' => 'microlearning-slide', 'code' => $slide->uuid, 'filename' => $slide->background]),
1 www 905
                ]
906
            ];
907
 
908
            return new JsonModel($data);
909
        }
910
        else if($request->isPost()) {
911
            $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
912
 
913
            switch($dataPost['type'])
914
            {
915
                case CompanyMicrolearningSlide::TYPE_AUDIO :
916
                    $form = new SlideAudioEditForm();
917
                    break;
918
 
919
                case CompanyMicrolearningSlide::TYPE_VIDEO :
920
                    $form = new SlideVideoEditForm();
921
                    break;
922
 
923
                case CompanyMicrolearningSlide::TYPE_DOCUMENT:
924
                    $form = new SlideDocumentEditForm();
925
                    break;
926
 
927
                case CompanyMicrolearningSlide::TYPE_IMAGE :
928
                    $form = new SlideImageEditForm();
929
                    break;
930
 
931
                case CompanyMicrolearningSlide::TYPE_QUIZ:
932
                    $form = new SlideQuizzEditForm($this->adapter, $currentCompany->id);
933
                    break;
934
 
935
                default :
936
                    $form = new SlideTextEditForm();
937
                    break;
938
 
939
            }
940
            $form->setData($dataPost);
941
 
942
            if($form->isValid()) {
943
                $dataPost = (array) $form->getData();
944
                $hydrator = new ObjectPropertyHydrator();
945
                $hydrator->hydrate($dataPost, $slide);
946
 
947
                $slide->file = null;
948
                $slide->background = null;
949
 
950
                if($slideMapper->update($slide)) {
951
                    $slide = $slideMapper->fetchOne($slide->id);
952
 
953
 
954
                    $target_path = $this->config['leaderslinked.fullpath.microlearning_slide'] .  $slide->uuid;
955
                    if(!file_exists($target_path)) {
956
                        mkdir($target_path, 0755, true);
957
                    }
958
 
959
                    $files = $this->getRequest()->getFiles()->toArray();
6520 nelberth 960
 
961
                    if($slide->type == CompanyMicrolearningSlide::TYPE_IMAGE) {
962
                            $fileBase64Content = $this->params()->fromPost('file');
963
 
964
                        try {
7238 nelberth 965
 
966
                            $target_path = $this->config['leaderslinked.fullpath.microlearning_slide'] .  $slide->uuid;
7236 nelberth 967
                            if($slide->file) {
968
                                if(!image ::delete($target_path, $slide->file)) {
969
                                    return new JsonModel([
970
                                        'success'   => false,
971
                                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
972
                                    ]);
973
                                }
974
                            }
6520 nelberth 975
                            $fileBase64Content = base64_decode($fileBase64Content);
976
                            $filename      = 'slide-' .uniqid() . '.png';
977
                            $tmp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp';
978
                            if(!file_exists($target_path)) {
979
                                mkdir($target_path, 0755, true);
980
                            }
981
                            $tmp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $filename;
982
                                file_put_contents($tmp_filename, $fileBase64Content);
983
 
984
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
985
 
986
                            $crop_to_dimensions = true;
987
                            if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions )) {
988
                                $slide->file = basename($filename);
989
                                $slideMapper->update($slide);
990
                            }
991
                        } catch(\Throwable $e) {
992
                            error_log($e->getTraceAsString());
993
                        }
994
                        /*
995
                        try {
996
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
997
 
998
                            $filename = 'slide-' .uniqid() . '.png';
999
                            $crop_to_dimensions = true;
1000
                            if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
1001
                                $slide->file = $filename;
1002
                                $slideMapper->update($slide);
1003
                            }
1004
                        } catch(\Throwable $e) {
1005
                            error_log($e->getTraceAsString());
1006
                        }*/
1007
                    }
1008
 
1 www 1009
                    if(isset($files['file']) && empty($files['file']['error'])) {
1010
                        $tmp_filename       = $files['file']['tmp_name'];
1011
                        $original_filename  = trim(strtolower($files['file']['name']));
1012
 
1013
 
1014
                        if($slide->file) {
1015
                            if(!image ::delete($target_path, $slide->file)) {
1016
                                return new JsonModel([
1017
                                    'success'   => false,
1018
                                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1019
                                ]);
1020
                            }
1021
                        }
1022
 
1023
                        if($slide->type == CompanyMicrolearningSlide::TYPE_DOCUMENT) {
1024
                            try {
1025
 
1026
                                $parts = explode('.', $original_filename);
1027
                                $filename = 'document-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
1028
 
1029
                                $full_filename = $target_path  . DIRECTORY_SEPARATOR .$filename;
1030
                                if(move_uploaded_file($tmp_filename, $full_filename)) {
1031
                                    $slide->file = basename($full_filename);
1032
                                    $slideMapper->update($slide);
1033
                                }
1034
                            } catch(\Throwable $e) {
1035
                                error_log($e->getTraceAsString());
1036
                            }
1037
                        }
1038
 
1039
                        if($slide->type == CompanyMicrolearningSlide::TYPE_AUDIO) {
1040
                            try {
1041
 
1042
                                $parts = explode('.', $original_filename);
1043
                                $filename = 'audio-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
1044
 
1045
                                $full_filename      = $target_path  . DIRECTORY_SEPARATOR .$filename;
1046
                                if(move_uploaded_file($tmp_filename , $full_filename)) {
1047
 
1048
 
1049
                                    $generateFileName   = substr($filename, 0, strrpos($filename, '.'));
1050
                                    $generateFile       =  $target_path  . DIRECTORY_SEPARATOR . $generateFileName .  '.mp3';
1051
                                    $cmd                = "/usr/bin/ffmpeg -i $full_filename -b:a 320000 $generateFile";
1052
                                    exec($cmd);
1053
 
1054
                                    $slide->file = basename($full_filename);
1055
                                    $slideMapper->update($slide);
1056
                                }
1057
 
1058
                            } catch(\Throwable $e) {
1059
                                error_log($e->getTraceAsString());
1060
                            }
1061
                        }
1062
 
1063
                        if($slide->type == CompanyMicrolearningSlide::TYPE_VIDEO) {
1064
                            try {
1065
                                $parts = explode('.', $original_filename);
1066
                                $filename = 'video-' . uniqid() . '.' . $parts[ count($parts) - 1 ];
1067
 
1068
                                $full_filename      = $target_path  . DIRECTORY_SEPARATOR .$filename;
1069
 
1070
                                if(move_uploaded_file($tmp_filename , $full_filename)) {
1071
 
1072
                                    $videoConvert = new VideoConvert();
1073
                                    $videoConvert->filename = $full_filename;
1074
                                    $videoConvert->type = VideoConvert::TYPE_MICRO_LEARNING;
1075
 
1076
                                    $videoConvertMapper = VideoConvertMapper::getInstance($this->adapter);
1077
                                    $videoConvertMapper->insert($videoConvert);
1078
 
1079
                                    $slide->file = basename($full_filename);
1080
                                    $slideMapper->update($slide);
1081
                                }
1082
 
1083
                            } catch(\Throwable $e) {
1084
                                echo $e->getMessage();
1085
 
1086
                                error_log($e->getTraceAsString());
1087
                            }
1088
                        }
1089
 
6520 nelberth 1090
 
1 www 1091
                    }
6595 nelberth 1092
                    $fileBase64Content = $this->params()->fromPost('background');
1093
                        $target_path = $this->config['leaderslinked.fullpath.microlearning_slide'] .  $slide->uuid;
1094
                        if($slide->background) {
1095
                            if(!image ::delete($target_path, $slide->background)) {
1096
                                return new JsonModel([
1097
                                    'success'   => false,
1098
                                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1099
                                ]);
1100
                            }
1101
                        }
6594 nelberth 1102
 
6595 nelberth 1103
                        try {
1104
 
1105
                            $fileBase64Content = base64_decode($fileBase64Content);
1106
                            $filename      = 'background-' .uniqid() . '.png';
1107
                            $tmp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp';
1108
                            if(!file_exists($target_path)) {
1109
                                mkdir($target_path, 0755, true);
1110
                            }
1111
                            $tmp_filename = 'data' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $filename;
1112
                                file_put_contents($tmp_filename, $fileBase64Content);
1113
 
1114
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
1115
 
1116
                            $crop_to_dimensions = true;
1117
                            if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions )) {
1118
                                $slide->background = basename($filename);
1119
                                $slideMapper->update($slide);
1120
                            }
1121
                        } catch(\Throwable $e) {
1122
                            error_log($e->getTraceAsString());
1123
                        }
1 www 1124
 
6594 nelberth 1125
                    /*
1 www 1126
                    if(isset($files['background']) && empty($files['background']['error'])) {
1127
                        if($slide->background) {
1128
                            if(!image ::delete($target_path, $slide->background)) {
1129
                                return new JsonModel([
1130
                                    'success'   => false,
1131
                                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1132
                                ]);
1133
                            }
1134
                        }
1135
 
1136
                        $tmp_filename  = $files['background']['tmp_name'];
1137
 
1138
                        try {
1139
                            list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.microlearning_image_size']);
1140
 
1141
                            $filename = 'background-' .uniqid() . '.png';
1142
                            $crop_to_dimensions = true;
1143
                            if(Image::uploadImage($tmp_filename, $target_path, $filename, $target_width, $target_height, $crop_to_dimensions)) {
1144
                                $slide->background = $filename;
1145
                                $slideMapper->update($slide);
1146
                            }
1147
                        } catch(\Throwable $e) {
1148
                            error_log($e->getTraceAsString());
1149
                        }
6594 nelberth 1150
                    }*/
1 www 1151
 
1152
                    $this->logger->info('Se edito la diapositiva ' . $slide->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
1153
 
1154
                    $data = [
1155
                        'success'   => true,
1156
                        'data'   => 'LABEL_RECORD_UPDATED'
1157
                    ];
1158
                } else {
1159
                    $data = [
1160
                        'success'   => false,
1161
                        'data'      => $capsuleMapper->getError()
1162
                    ];
1163
 
1164
                }
1165
 
1166
                return new JsonModel($data);
1167
 
1168
            } else {
1169
                $messages = [];
1170
                $form_messages = (array) $form->getMessages();
1171
                foreach($form_messages  as $fieldname => $field_messages)
1172
                {
1173
 
1174
                    $messages[$fieldname] = array_values($field_messages);
1175
                }
1176
 
1177
                return new JsonModel([
1178
                    'success'   => false,
1179
                    'data'   => $messages
1180
                ]);
1181
            }
1182
        } else {
1183
            $data = [
1184
                'success' => false,
1185
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1186
            ];
1187
 
1188
            return new JsonModel($data);
1189
        }
1190
 
1191
        return new JsonModel($data);
1192
    }
1193
 
1194
 
1195
    public function textAction()
1196
    {
1197
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1198
        $currentUser = $currentUserPlugin->getUser();
1199
        $currentCompany = $currentUserPlugin->getCompany();
1200
 
1201
        $request    = $this->getRequest();
1202
        $topic_uuid   = $this->params()->fromRoute('topic_uuid');
1203
        $capsule_uuid = $this->params()->fromRoute('capsule_uuid');
1204
        $slide_uuid   = $this->params()->fromRoute('slide_uuid');
1205
 
1206
 
1207
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
1208
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
1209
        if(!$topic) {
1210
            return new JsonModel([
1211
                'success'   => false,
1212
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
1213
            ]);
1214
        }
1215
 
1216
        if($topic->company_id != $currentCompany->id) {
1217
            return new JsonModel([
1218
                'success'   => false,
1219
                'data'   => 'ERROR_UNAUTHORIZED'
1220
            ]);
1221
        }
1222
 
1223
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
1224
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
1225
        if(!$capsule) {
1226
            return new JsonModel([
1227
                'success'   => false,
1228
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
1229
            ]);
1230
        }
1231
 
1232
        if($capsule->topic_id != $topic->id) {
1233
            return new JsonModel([
1234
                'success'   => false,
1235
                'data'   => 'ERROR_UNAUTHORIZED'
1236
            ]);
1237
        }
1238
 
1239
 
1240
        $slideMapper = CompanyMicrolearningSlideMapper::getInstance($this->adapter);
1241
        $slide = $slideMapper->fetchOneByUuid($slide_uuid);
1242
        if(!$slide) {
1243
            return new JsonModel([
1244
                'success'   => false,
1245
                'data'   => 'ERROR_SLIDE_NOT_FOUND'
1246
            ]);
1247
        }
1248
 
1249
        if($slide->capsule_id != $capsule->id) {
1250
            return new JsonModel([
1251
                'success'   => false,
1252
                'data'   => 'ERROR_UNAUTHORIZED'
1253
            ]);
1254
        }
1255
 
1256
        if($request->isGet()) {
1257
            $data = [
1258
                'success' => true,
1259
                'data' => $slide->description,
1260
            ];
1261
 
1262
            return new JsonModel($data);
1263
 
1264
        } else {
1265
            $data = [
1266
                'success' => false,
1267
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1268
            ];
1269
 
1270
            return new JsonModel($data);
1271
        }
1272
 
1273
        return new JsonModel($data);
1274
    }
1275
 
1276
 
1277
 
1278
 
1279
 
1280
}