Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev Autor Línea Nro. Línea
119 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Mvc\Controller\AbstractActionController;
7
use Laminas\View\Model\JsonModel;
8
use LeadersLinked\Library\Functions;
283 www 9
use LeadersLinked\Mapper\MicrolearningCapsuleMapper;
119 efrain 10
use LeadersLinked\Mapper\QueryMapper;
11
use LeadersLinked\Mapper\UserMapper;
12
use Laminas\Db\Sql\Select;
283 www 13
use LeadersLinked\Mapper\MicrolearningCapsuleUserMapper;
14
use LeadersLinked\Mapper\MicrolearningCapsuleCommentMapper;
119 efrain 15
use LeadersLinked\Form\Service\CapsuleCommentForm;
283 www 16
use LeadersLinked\Model\MicrolearningCapsuleComment;
17
use LeadersLinked\Model\MicrolearningCapsuleUser;
18
use LeadersLinked\Model\MicrolearningAnswer;
19
use LeadersLinked\Mapper\MicrolearningUserProgressMapper;
20
use LeadersLinked\Mapper\MicrolearningSlideMapper;
21
use LeadersLinked\Mapper\MicrolearningUserLogMapper;
22
use LeadersLinked\Model\MicrolearningUserLog;
23
use LeadersLinked\Mapper\MicrolearningTopicMapper;
119 efrain 24
use LeadersLinked\Mapper\CompanyMapper;
283 www 25
use LeadersLinked\Model\MicrolearningUserProgress;
26
use LeadersLinked\Mapper\MicrolearningExtendUserMapper;
27
use LeadersLinked\Mapper\MicrolearningExtendUserCompanyMapper;
28
use LeadersLinked\Mapper\MicrolearningExtendUserFunctionMapper;
29
use LeadersLinked\Mapper\MicrolearningExtendUserGroupMapper;
30
use LeadersLinked\Mapper\MicrolearningExtendUserInstitutionMapper;
31
use LeadersLinked\Mapper\MicrolearningExtendUserPartnerMapper;
32
use LeadersLinked\Mapper\MicrolearningExtendUserProgramMapper;
33
use LeadersLinked\Mapper\MicrolearningExtendUserStudentTypeMapper;
34
use LeadersLinked\Mapper\MicrolearningExtendUserSectorMapper;
35
use LeadersLinked\Mapper\MicrolearningQuizMapper;
36
use LeadersLinked\Mapper\MicrolearningQuestionMapper;
37
use LeadersLinked\Mapper\MicrolearningAnswerMapper;
38
use LeadersLinked\Model\MicrolearningSlide;
39
use LeadersLinked\Library\Storage;
302 www 40
use LeadersLinked\Mapper\MicrolearningTopicCapsuleMapper;
119 efrain 41
 
42
 
43
class MicrolearningUserAccessGrantedIds
44
{
45
    public $companies;
46
    public $topics;
47
    public $capsules;
48
 
49
 
50
    public function __construct()
51
    {
52
        $this->companies    = [];
53
        $this->topics       = [];
54
        $this->capsules     = [];
55
    }
56
}
57
 
58
class MicrolearningController extends AbstractActionController
59
{
60
 
61
    /**
62
     *
63
     * @var \Laminas\Db\Adapter\AdapterInterface
64
     */
65
    private $adapter;
66
 
67
    /**
68
     *
69
     * @var \LeadersLinked\Cache\CacheInterface
70
     */
71
    private $cache;
72
 
73
 
74
    /**
75
     *
76
     * @var \Laminas\Log\LoggerInterface
77
     */
78
    private $logger;
79
 
80
    /**
81
     *
82
     * @var array
83
     */
84
    private $config;
85
 
86
 
87
    /**
88
     *
89
     * @var \Laminas\Mvc\I18n\Translator
90
     */
91
    private $translator;
92
 
93
 
94
    /**
95
     *
96
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
97
     * @param \LeadersLinked\Cache\CacheInterface $cache
98
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
99
     * @param array $config
100
     * @param \Laminas\Mvc\I18n\Translator $translator
101
     */
102
    public function __construct($adapter, $cache, $logger, $config, $translator)
103
    {
104
        $this->adapter      = $adapter;
105
        $this->cache        = $cache;
106
        $this->logger       = $logger;
107
        $this->config       = $config;
108
        $this->translator   = $translator;
109
    }
110
 
111
    public function indexAction()
112
    {
113
        $request = $this->getRequest();
114
 
115
        if($request->isGet()) {
116
            return new JsonModel([
117
                'success' => true,
118
                'data' =>  [
119
                    'link_companies' => $this->url()->fromRoute('microlearning/companies',[], ['force_canonical' => true]),
120
                    'link_timeline' => $this->url()->fromRoute('microlearning/timeline',[], ['force_canonical' => true]),
121
                    'link_last_capsule_in_progress' => $this->url()->fromRoute('microlearning/last-capsule-in-progress',[], ['force_canonical' => true]),
122
                    'link_profile' => $this->url()->fromRoute('microlearning/profile',[], ['force_canonical' => true]),
123
                    'link_topics' => $this->url()->fromRoute('microlearning/topics',[], ['force_canonical' => true]),
124
                    'link_capsules_pending' => $this->url()->fromRoute('microlearning/capsules-pending',[], ['force_canonical' => true]),
125
                    'link_capsules_completed' => $this->url()->fromRoute('microlearning/capsules-completed',[], ['force_canonical' => true]),
126
                    'link_capsules_in_progress' => $this->url()->fromRoute('microlearning/capsules-in-progress',[], ['force_canonical' => true]),
127
 
128
 
129
                ]
130
            ]);
131
        }
132
 
133
        return new JsonModel([
134
            'success' => false,
135
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
136
        ]);
137
    }
138
 
139
    public function companiesAction()
140
    {
141
 
142
        $request = $this->getRequest();
143
 
144
        if($request->isGet()) {
145
            $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
146
            $currentNetwork = $currentNetworkPlugin->getNetwork();
147
 
148
 
149
            $accessGrantedIds = $this->getAccessGranted();
150
            $companyMapper = CompanyMapper::getInstance($this->adapter);
151
            $records = $companyMapper->fetchAllByIdsAndNetworkId($accessGrantedIds->companies, $currentNetwork->id);
152
 
333 www 153
            $storage = Storage::getInstance($this->config, $this->adapter);
119 efrain 154
 
155
            $companies = [];
156
            foreach($records as $record)
157
            {
158
                array_push($companies, [
164 efrain 159
                    'uuid' => $record->uuid,
119 efrain 160
                    'name' => $record->name,
283 www 161
                    'image' => $storage->getCompanyImage($record),
119 efrain 162
                    'link_progress' => $this->url()->fromRoute('microlearning/progress',['id' => $record->uuid], ['force_canonical' => true]),
163
                ]);
164
            }
165
 
166
            return new JsonModel([
167
                'success' => true,
168
                'data' =>  $companies
169
            ]);
170
 
171
 
172
        }
173
        return new JsonModel([
174
            'success' => false,
175
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
176
        ]);
177
    }
178
 
179
 
180
 
181
    public function capsuleCommentsAction()
182
    {
183
 
184
        $request = $this->getRequest();
185
 
186
        if($request->isGet()) {
187
            $serviceDatetimeFormat = $this->config['leaderslinked.services.datetime'];
188
 
189
            $currentUserPlugin = $this->plugin('currentUserPlugin');
190
            $currentUser = $currentUserPlugin->getUser();
191
 
192
            $capsule_id = $this->params()->fromRoute('capsule_id');
193
 
283 www 194
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
119 efrain 195
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
196
 
197
            if(!$capsule) {
198
                return new JsonModel([
199
                    'success' => false,
200
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
201
 
202
                ]);
203
 
204
            }
205
 
283 www 206
            $capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
119 efrain 207
            $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $capsule->id);
208
            if(! $capsuleUser) {
209
                return new JsonModel([
210
                    'success' => false,
211
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE',
212
                ]);
213
            }
214
 
215
            $userMapper = UserMapper::getInstance($this->adapter);
216
            $users = [];
217
 
283 www 218
 
333 www 219
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 220
 
221
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
119 efrain 222
            $records = $capsuleCommentMapper->fetchAllByCapsuleId($capsule->id);
223
 
224
            $comments = [];
225
            foreach($records as $record)
226
            {
227
 
228
                if(isset($users[$record->user_id])) {
229
 
230
                    $user = $users[$record->user_id];
231
 
232
                } else {
233
 
234
                    $user = $userMapper->fetchOne($record->user_id);
235
                    if(!$user) {
236
                        continue;
237
                    }
238
 
239
                    $users[$record->user_id] = $user;
240
 
241
 
242
                }
243
 
244
 
245
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
246
 
247
                array_push($comments, [
248
                    'date' => $dt->format($serviceDatetimeFormat),
283 www 249
                    'image' => $storage->getUserImage($user),
119 efrain 250
                    'fullname' => trim(trim($user->first_name) . ' ' . trim($user->last_name)),
251
                    'rating' => strval($record->rating),
252
                    'comment' => $record->comment,
253
                    'link_delete' => $record->user_id == $currentUser->id ? $this->url()->fromRoute('microlearning/capsules-comments/delete', ['capsule_id' => $capsule->uuid, 'comment_id' => $record->uuid], ['force_canonical' => true]) : '',
254
                ]);
255
            }
256
 
302 www 257
            $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->id);
119 efrain 258
 
259
            return new JsonModel([
260
                'success' => true,
261
                'data' => [
262
                    'comments' => $comments,
263
                    'capsule' => [
264
                        'total_comments' => strval($dataCountAndRatingAverage['total_comments']),
265
                        'total_rating' => strval($dataCountAndRatingAverage['total_rating'])
266
                    ]
267
                ]
268
 
269
            ]);
270
 
271
        }
272
 
273
        return new JsonModel([
274
            'success' => false,
275
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
276
        ]);
277
    }
278
 
279
 
280
 
281
    public function capsuleDeleteMyCommentAction()
282
    {
283
 
284
        $request = $this->getRequest();
285
 
286
        if($request->isPost()) {
287
 
288
            $currentUserPlugin = $this->plugin('currentUserPlugin');
289
            $currentUser = $currentUserPlugin->getUser();
290
 
291
            $capsule_id = $this->params()->fromRoute('capsule_id');
292
            $comment_id = $this->params()->fromRoute('comment_id');
293
 
283 www 294
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
119 efrain 295
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
296
 
297
            if(!$capsule) {
298
                return new JsonModel([
299
                    'success' => false,
300
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
301
 
302
                ]);
303
 
304
            }
305
 
283 www 306
            $capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
119 efrain 307
            $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $capsule->id);
308
            if(! $capsuleUser) {
309
                return new JsonModel([
310
                    'success' => false,
311
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE',
312
                ]);
313
            }
314
 
283 www 315
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
119 efrain 316
            $capsuleComment = $capsuleCommentMapper->fetchOneByUuid($comment_id);
317
 
318
            if(!$capsuleComment) {
319
                return new JsonModel([
320
                    'success' => false,
321
                    'data' => 'ERROR_CAPSULE_COMMENT_NOT_FOUND',
322
                ]);
323
            }
324
 
325
            if($capsuleComment->capsule_id != $capsule->id || $capsuleComment->user_id != $currentUser->id) {
326
                return new JsonModel([
327
                    'success' => false,
328
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE_COMMENT',
329
                ]);
330
            }
331
 
332
 
333
            $result = $capsuleCommentMapper->delete($capsuleComment->id);
334
            if($result) {
302 www 335
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->id);
119 efrain 336
 
337
 
338
 
339
 
340
                return new JsonModel([
341
                    'success' => true,
342
                    'data' => [
343
                        'message' => 'LABEL_CAPSULE_COMMENT_HAVE_BEEN_SUCCESSFULLY_DELETE',
344
                        'capsule' => [
345
                            'total_comments' => strval($dataCountAndRatingAverage['total_comments']),
346
                            'total_rating' => strval($dataCountAndRatingAverage['total_rating'])
347
                        ]
348
                    ],
349
 
350
                ]);
351
            } else {
352
                return new JsonModel([
353
                    'success' => false,
354
                    'data' => $capsuleCommentMapper->getError()
355
 
356
                ]);
357
            }
358
 
359
 
360
 
361
        }
362
 
363
        return new JsonModel([
364
            'success' => false,
365
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
366
        ]);
367
    }
368
 
369
    public function capsuleAddMyCommentAction()
370
    {
371
 
372
        $request = $this->getRequest();
373
 
374
        if($request->isPost()) {
375
 
376
            $currentUserPlugin = $this->plugin('currentUserPlugin');
377
            $currentUser = $currentUserPlugin->getUser();
378
 
379
            $capsule_id = $this->params()->fromRoute('capsule_id');
380
 
381
 
283 www 382
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
119 efrain 383
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
384
 
385
            if(!$capsule) {
386
                return new JsonModel([
387
                    'success' => false,
388
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
389
 
390
                ]);
391
 
392
            }
393
 
283 www 394
            $capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
119 efrain 395
            $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $capsule->id);
396
            if(! $capsuleUser) {
397
                return new JsonModel([
398
                    'success' => false,
399
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE',
400
                ]);
401
            }
402
 
403
            //$rawdata = file_get_contents("php://input");
404
            //  error_log('$rawdata = ' . $rawdata );
405
 
406
 
407
            $form = new  CapsuleCommentForm();
408
            $dataPost = $request->getPost()->toArray();
409
            $dataPost['added_on'] = $capsuleMapper->getDatebaseNow();
410
 
411
 
412
            $form->setData($dataPost);
413
 
414
            if($form->isValid()) {
415
                $dataPost = (array) $form->getData();
416
 
417
 
283 www 418
                $capsuleComment = new MicrolearningCapsuleComment();
119 efrain 419
                $capsuleComment->company_id = $capsule->company_id;
420
                $capsuleComment->capsule_id = $capsule->id;
421
                $capsuleComment->user_id = $currentUser->id;
422
                $capsuleComment->comment = $dataPost['comment'];
423
                $capsuleComment->rating = strval($dataPost['rating']);
424
                $capsuleComment->added_on =  $dataPost['added_on'];
425
 
426
 
283 www 427
                $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
119 efrain 428
                $result = $capsuleCommentMapper->insert($capsuleComment);
429
                if($result) {
430
 
431
                    $capsuleComment = $capsuleCommentMapper->fetchOne($capsuleComment->id);
432
 
302 www 433
                    $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id,  $capsule->id);
119 efrain 434
 
435
 
436
 
437
                    return new JsonModel([
438
                        'success' => true,
439
                        'data' => [
440
                            'message' =>'LABEL_CAPSULE_COMMENT_HAVE_BEEN_SUCCESSFULLY_ADDED',
441
 
442
                            'comment' => [
443
                                'comment' => $capsuleComment->comment,
444
                                'rating' => $capsuleComment->rating,
445
                                'link_delete' => $this->url()->fromRoute('microlearning/capsules-comments/delete', ['capsule_id' => $capsule->uuid, 'comment_id' => $capsuleComment->uuid], ['force_canonical' => true])
446
                            ],
447
                            'capsule' => [
448
                                'total_comments' => strval($dataCountAndRatingAverage['total_comments']),
449
                                'total_rating' => strval($dataCountAndRatingAverage['total_rating'])
450
                            ]
451
                        ]
452
 
453
                    ]);
454
                } else {
455
                    return new JsonModel([
456
                        'success' => false,
457
                        'data' => $capsuleCommentMapper->getError()
458
 
459
                    ]);
460
                }
461
 
462
 
463
            } else {
464
                $messages = [];
465
                $form_messages = (array) $form->getMessages();
466
                foreach($form_messages  as $fieldname => $field_messages)
467
                {
468
 
469
                    $messages[$fieldname] = array_values($field_messages);
470
                }
471
 
472
                return new JsonModel([
473
                    'success'   => false,
474
                    'data'   => $messages
475
                ]);
476
            }
477
 
478
 
479
 
480
 
481
        }
482
 
483
        return new JsonModel([
484
            'success' => false,
485
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
486
        ]);
487
    }
488
 
489
    public function lastCapsuleInProgressAction()
490
    {
491
        $request = $this->getRequest();
492
 
493
        if($request->isGet())
494
        {
495
            $currentUserPlugin = $this->plugin('currentUserPlugin');
496
            $currentUser = $currentUserPlugin->getUser();
497
 
498
            $accessGrantedIds = $this->getAccessGranted();
499
 
500
 
283 www 501
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
119 efrain 502
            $userProgress = $userProgressMapper->fetchOneLastCapsuleInProgressByUserIdAndCapsuleIds($currentUser->id, $accessGrantedIds->capsules);
283 www 503
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 504
 
505
            if($userProgress) {
333 www 506
                $storage = Storage::getInstance($this->config, $this->adapter);
283 www 507
                $path = $storage->getPathMicrolearningCapsule();
508
 
509
                $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
119 efrain 510
                $capsule = $capsuleMapper->fetchOne($userProgress->capsule_id);
511
 
301 www 512
                $topic = $topicMapper->fetchOne($userProgress->topic_id);
513
 
283 www 514
                $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
302 www 515
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id,  $capsule->id);
576 stevensc 516
                $image = $storage->getGenericImage($path, $topic->uuid, $capsule->image);
517
 
119 efrain 518
                $response = [
519
                    'success' => true,
520
                    'data' => [
164 efrain 521
                        'uuid'              => $capsule->uuid,
119 efrain 522
                        'name'              => $capsule->name ? $capsule->name : '',
523
                        'description'       => $capsule->description ? $capsule->description : '',
575 stevensc 524
                        'image'             => ($capsule->image && $topic) ? $storage->getGenericImage($path,  $topic->uuid, $capsule->image )  : '',
119 efrain 525
                        'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
526
                        'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
527
                        'completed'         => $userProgress->completed,
145 efrain 528
                        'progress'          => $userProgress->progress,
119 efrain 529
                        'added_on'          => $userProgress->added_on,
530
                        'updated_on'        => $userProgress->updated_on
531
                    ]
532
                ];
533
 
534
 
535
            } else {
536
                $response = [
537
                    'success' => true,
575 stevensc 538
                    'data' => []
119 efrain 539
                ];
540
            }
541
 
542
 
543
        } else {
544
 
545
            $response = [
546
                'success' => false,
547
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
548
            ];
549
 
550
 
551
        }
552
 
553
        return new JsonModel($response);
554
    }
555
 
556
    public function capsulesPendingAction()
557
    {
558
        $request = $this->getRequest();
559
        if($request->isGet()) {
560
            $currentUserPlugin = $this->plugin('currentUserPlugin');
561
            $currentUser = $currentUserPlugin->getUser();
562
 
563
            $name = Functions::sanitizeFilterString($this->params()->fromQuery('name'));
245 efrain 564
            $order_field = Functions::sanitizeFilterString($this->params()->fromQuery('order_field'));
565
            $order_direction = Functions::sanitizeFilterString($this->params()->fromQuery('order_direction'));
119 efrain 566
 
245 efrain 567
            if(!in_array($order_field,['name', 'added_on'] )) {
568
                $order_field = 'name';
569
            }
119 efrain 570
 
245 efrain 571
            if(!in_array( $order_direction,['asc', 'desc'])) {
558 stevensc 572
                $order_direction = 'asc'; // Corregido: asignar a $order_direction
245 efrain 573
            }
574
 
283 www 575
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
576
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
558 stevensc 577
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter); // Añadido
283 www 578
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
558 stevensc 579
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter); // Reordenado
119 efrain 580
 
581
            $accessGranted = $this->getAccessGranted();
558 stevensc 582
 
583
            // $topics = []; // Ya no es necesario cachear tópicos aquí
584
            $capsulesData = []; // Renombrado
119 efrain 585
 
333 www 586
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 587
            $path = $storage->getPathMicrolearningCapsule();
588
 
119 efrain 589
            foreach($accessGranted->capsules as $capsule_id)
590
            {
558 stevensc 591
                // 1. Verificar progreso (debe ser nulo para pendiente)
119 efrain 592
                $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule_id);
593
                if($userProgress) {
594
                    continue;
595
                }
558 stevensc 596
 
597
                // 2. Obtener detalles de la cápsula
119 efrain 598
                $capsule = $capsuleMapper->fetchOne($capsule_id);
558 stevensc 599
                if(!$capsule) { // Añadir verificación por si la cápsula no existe
600
                    continue;
601
                }
119 efrain 602
 
558 stevensc 603
                // 3. Filtrar por nombre
119 efrain 604
                if($name) {
605
                    if(empty($name) || stripos($capsule->name, $name) === false) {
606
                        continue;
607
                    }
608
                }
609
 
558 stevensc 610
                // 4. Obtener Tópico asociado válido
611
                $topic = null;
612
                $topic_uuid_for_links = null;
613
                $relation = $topicCapsuleMapper->fetchOneByCapsuleId($capsule->id);
614
                if ($relation && in_array($relation->topic_id, $accessGranted->topics)) {
615
                    $topic = $topicMapper->fetchOne($relation->topic_id);
616
                    if ($topic) {
617
                        $topic_uuid_for_links = $topic->uuid;
618
                    }
619
                }
620
 
621
                // 5. Obtener datos de comentarios
302 www 622
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id,  $capsule->id);
119 efrain 623
 
558 stevensc 624
                // 6. Construir enlace slides
625
                $link_slides = $topic_uuid_for_links ? $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic_uuid_for_links,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]) : '';
119 efrain 626
 
558 stevensc 627
                // 7. Añadir al array de resultados
628
                array_push($capsulesData, [
119 efrain 629
                    'uuid'              => $capsule->uuid,
630
                    'name'              => $capsule->name ? $capsule->name : '',
631
                    'description'       => $capsule->description ? $capsule->description : '',
283 www 632
                    'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image)  : '',
119 efrain 633
                    'position'          => $capsule->order,
634
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
635
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
558 stevensc 636
                    'link_slides'       => $link_slides, // Usar enlace construido
119 efrain 637
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
638
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
191 efrain 639
                    'progress'          => 0,
119 efrain 640
                    'added_on'          => $capsule->added_on,
641
                    'updated_on'        => $capsule->updated_on,
642
                ]);
643
            }
644
 
558 stevensc 645
            // 8. Ordenar
245 efrain 646
 
647
            if($order_field == 'name') {
648
                if($order_direction == 'asc') {
558 stevensc 649
                    usort($capsulesData, function($a, $b) {
245 efrain 650
                        return strcasecmp($a['name'], $b['name']);
651
                    });
652
                } else {
558 stevensc 653
                    usort($capsulesData, function($a, $b) {
245 efrain 654
                        $result = strcasecmp($a['name'], $b['name']);
558 stevensc 655
                        if($result < 0) { return 1; } else if($result > 0) { return -1; } else { return 0; } // Simplificado
245 efrain 656
                    });
119 efrain 657
                }
658
 
245 efrain 659
            }
660
 
661
            if($order_field == 'added_on') {
662
                if($order_direction == 'asc') {
558 stevensc 663
                    usort($capsulesData, function($a, $b) {
245 efrain 664
                        return strcasecmp($a['added_on'], $b['added_on']);
665
                    });
666
                } else {
558 stevensc 667
                    usort($capsulesData, function($a, $b) {
245 efrain 668
                        $result = strcasecmp($a['added_on'], $b['added_on']);
558 stevensc 669
                        if($result < 0) { return 1; } else if($result > 0) { return -1; } else { return 0; } // Simplificado
245 efrain 670
                    });
671
                }
119 efrain 672
 
558 stevensc 673
            }
119 efrain 674
 
558 stevensc 675
            // 9. Retornar
119 efrain 676
            return new JsonModel([
677
                'success' => true,
558 stevensc 678
                'data' => $capsulesData
119 efrain 679
            ]);
680
 
681
 
682
 
683
        }
684
 
685
        return new JsonModel([
686
            'success' => false,
687
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
688
        ]);
689
 
690
    }
691
 
692
    public function capsulesCompletedAction()
693
    {
694
        $request = $this->getRequest();
695
        if($request->isGet()) {
696
            $currentUserPlugin = $this->plugin('currentUserPlugin');
697
            $currentUser = $currentUserPlugin->getUser();
698
 
699
            $name = Functions::sanitizeFilterString($this->params()->fromQuery('name'));
245 efrain 700
            $order_field = Functions::sanitizeFilterString($this->params()->fromQuery('order_field'));
701
            $order_direction = Functions::sanitizeFilterString($this->params()->fromQuery('order_direction'));
119 efrain 702
 
246 efrain 703
            if(!in_array($order_field,['name', 'added_on','last_access_on'] )) {
245 efrain 704
                $order_field = 'name';
705
            }
119 efrain 706
 
245 efrain 707
            if(!in_array( $order_direction,['asc', 'desc'])) {
708
                $order_field = 'asc';
709
            }
710
 
283 www 711
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
712
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
558 stevensc 713
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
714
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
283 www 715
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
119 efrain 716
 
717
            $accessGranted = $this->getAccessGranted();
718
 
558 stevensc 719
            $capsulesData = [];
119 efrain 720
 
333 www 721
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 722
            $path = $storage->getPathMicrolearningCapsule();
723
 
119 efrain 724
            foreach($accessGranted->capsules as $capsule_id)
725
            {
558 stevensc 726
                // 1. Verificar progreso
119 efrain 727
                $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule_id);
728
                if(!$userProgress) {
729
                    continue;
730
                }
731
 
732
                if(!$userProgress->completed) {
733
                    continue;
734
                }
735
 
558 stevensc 736
                // 2. Obtener detalles de la cápsula
119 efrain 737
                $capsule = $capsuleMapper->fetchOne($capsule_id);
558 stevensc 738
                if(!$capsule) {
739
                    continue;
740
                }
119 efrain 741
 
558 stevensc 742
                // 3. Filtrar por nombre
119 efrain 743
                if($name) {
744
                    if(empty($name) || stripos($capsule->name, $name) === false) {
745
                        continue;
746
                    }
747
                }
748
 
558 stevensc 749
                // 4. Obtener Tópico asociado válido
750
                $topic = null;
751
                $topic_uuid_for_links = null;
752
                $relation = $topicCapsuleMapper->fetchOneByCapsuleId($capsule->id);
753
                if ($relation && in_array($relation->topic_id, $accessGranted->topics)) {
754
                    $topic = $topicMapper->fetchOne($relation->topic_id);
755
                    if ($topic) {
756
                        $topic_uuid_for_links = $topic->uuid;
757
                    }
758
                }
759
 
760
                // 5. Obtener datos de comentarios
302 www 761
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->id);
119 efrain 762
 
558 stevensc 763
                // 6. Construir enlace slides
764
                $link_slides = $topic_uuid_for_links ? $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic_uuid_for_links,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]) : '';
119 efrain 765
 
558 stevensc 766
                // 7. Añadir al array de resultados
767
                array_push($capsulesData, [
119 efrain 768
                    'uuid'              => $capsule->uuid,
769
                    'name'              => $capsule->name ? $capsule->name : '',
770
                    'description'       => $capsule->description ? $capsule->description : '',
283 www 771
                    'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image)  : '',
119 efrain 772
                    'position'          => $capsule->order,
773
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
774
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
558 stevensc 775
                    'link_slides'       => $link_slides,
119 efrain 776
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
777
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
145 efrain 778
                    'progress'          => $userProgress->progress,
119 efrain 779
                    'added_on'          => $capsule->added_on,
246 efrain 780
                    'last_access_on'    => $userProgress->updated_on,
119 efrain 781
                    'updated_on'        => $capsule->updated_on,
782
                ]);
783
            }
784
 
558 stevensc 785
            // 8. Ordenar (lógica de ordenación permanece igual, usando $capsulesData)
245 efrain 786
            if($order_field == 'name') {
787
                if($order_direction == 'asc') {
558 stevensc 788
                    usort($capsulesData, function($a, $b) {
245 efrain 789
                        return strcasecmp($a['name'], $b['name']);
790
                    });
791
                } else {
558 stevensc 792
                    usort($capsulesData, function($a, $b) {
245 efrain 793
                        $result = strcasecmp($a['name'], $b['name']);
558 stevensc 794
                        if($result < 0) { return 1; } else if($result > 0) { return -1; } else { return 0; }
245 efrain 795
                    });
119 efrain 796
                }
245 efrain 797
            }
798
 
799
            if($order_field == 'added_on') {
800
                if($order_direction == 'asc') {
558 stevensc 801
                    usort($capsulesData, function($a, $b) {
245 efrain 802
                        return strcasecmp($a['added_on'], $b['added_on']);
803
                    });
804
                } else {
558 stevensc 805
                    usort($capsulesData, function($a, $b) {
245 efrain 806
                        $result = strcasecmp($a['added_on'], $b['added_on']);
558 stevensc 807
                        if($result < 0) { return 1; } else if($result > 0) { return -1; } else { return 0; }
245 efrain 808
                    });
809
                }
810
            }
246 efrain 811
 
812
            if($order_field == 'last_access_on') {
813
                if($order_direction == 'asc') {
558 stevensc 814
                    usort($capsulesData, function($a, $b) {
246 efrain 815
                        return strcasecmp($a['last_access_on'], $b['last_access_on']);
816
                    });
817
                } else {
558 stevensc 818
                    usort($capsulesData, function($a, $b) {
246 efrain 819
                        $result = strcasecmp($a['last_access_on'], $b['last_access_on']);
558 stevensc 820
                        if($result < 0) { return 1; } else if($result > 0) { return -1; } else { return 0; }
246 efrain 821
                    });
822
                }
823
            }
824
 
825
 
826
 
827
 
119 efrain 828
 
829
            return new JsonModel([
830
                'success' => true,
558 stevensc 831
                'data' => $capsulesData
119 efrain 832
            ]);
833
 
834
        }
835
 
836
        return new JsonModel([
837
            'success' => false,
838
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
839
        ]);
840
    }
841
 
842
    public function capsulesInProgressAction()
843
    {
844
        $request = $this->getRequest();
845
        if($request->isGet()) {
846
            $currentUserPlugin = $this->plugin('currentUserPlugin');
847
            $currentUser = $currentUserPlugin->getUser();
848
 
849
            $name = Functions::sanitizeFilterString($this->params()->fromQuery('name'));
245 efrain 850
            $order_field = Functions::sanitizeFilterString($this->params()->fromQuery('order_field'));
851
            $order_direction = Functions::sanitizeFilterString($this->params()->fromQuery('order_direction'));
119 efrain 852
 
246 efrain 853
            if(!in_array($order_field,['name', 'added_on', 'last_access_on'] )) {
245 efrain 854
                $order_field = 'name';
855
            }
119 efrain 856
 
245 efrain 857
            if(!in_array( $order_direction,['asc', 'desc'])) {
558 stevensc 858
                $order_direction = 'asc';
245 efrain 859
            }
860
 
283 www 861
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
862
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
558 stevensc 863
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
864
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
283 www 865
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
119 efrain 866
 
867
            $accessGranted = $this->getAccessGranted();
868
 
558 stevensc 869
            $capsulesData = [];
119 efrain 870
 
333 www 871
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 872
            $path = $storage->getPathMicrolearningCapsule();
873
 
119 efrain 874
            foreach($accessGranted->capsules as $capsule_id)
875
            {
558 stevensc 876
                // 1. Verificar progreso
119 efrain 877
                $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule_id);
558 stevensc 878
                if(!$userProgress || $userProgress->completed) { // Si no hay progreso o ya está completada
119 efrain 879
                    continue;
880
                }
558 stevensc 881
 
882
                // 2. Obtener detalles de la cápsula
883
                $capsule = $capsuleMapper->fetchOne($capsule_id);
884
                if(!$capsule) {
119 efrain 885
                    continue;
886
                }
887
 
558 stevensc 888
                // 3. Filtrar por nombre
119 efrain 889
                if($name) {
890
                    if(empty($name) || stripos($capsule->name, $name) === false) {
891
                        continue;
892
                    }
893
                }
894
 
558 stevensc 895
                // 4. Obtener Tópico asociado válido
896
                $topic = null;
897
                $topic_uuid_for_links = null;
898
                $relation = $topicCapsuleMapper->fetchOneByCapsuleId($capsule->id);
899
                if ($relation && in_array($relation->topic_id, $accessGranted->topics)) {
900
                    $topic = $topicMapper->fetchOne($relation->topic_id);
901
                    if ($topic) {
902
                        $topic_uuid_for_links = $topic->uuid;
903
                    }
904
                }
119 efrain 905
 
558 stevensc 906
                // 5. Obtener datos de comentarios
302 www 907
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id,  $capsule->id);
119 efrain 908
 
558 stevensc 909
                // 6. Construir enlace slides
910
                $link_slides = $topic_uuid_for_links ? $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic_uuid_for_links,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]) : '';
119 efrain 911
 
558 stevensc 912
                // 7. Añadir al array de resultados
913
                array_push($capsulesData, [
119 efrain 914
                    'uuid'              => $capsule->uuid,
915
                    'name'              => $capsule->name ? $capsule->name : '',
916
                    'description'       => $capsule->description ? $capsule->description : '',
283 www 917
                    'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image)  : '',
119 efrain 918
                    'position'          => $capsule->order,
919
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
920
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
558 stevensc 921
                    'link_slides'       => $link_slides,
119 efrain 922
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
923
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
585 stevensc 924
                    'progress'          => $userProgress->progress,
925
                    'last_access_on'    => $userProgress->updated_on,
926
                    'added_on'          => $capsule->added_on,
927
                    'updated_on'        => $capsule->updated_on,
119 efrain 928
                ]);
929
            }
930
 
558 stevensc 931
            // 8. Ordenar (lógica de ordenación permanece igual, usando $capsulesData)
245 efrain 932
            if($order_field == 'name') {
933
                if($order_direction == 'asc') {
558 stevensc 934
                    usort($capsulesData, function($a, $b) {
245 efrain 935
                        return strcasecmp($a['name'], $b['name']);
936
                    });
937
                } else {
558 stevensc 938
                    usort($capsulesData, function($a, $b) {
245 efrain 939
                        $result = strcasecmp($a['name'], $b['name']);
558 stevensc 940
                        if($result < 0) { return 1; } else if($result > 0) { return -1; } else { return 0; }
245 efrain 941
                    });
119 efrain 942
                }
245 efrain 943
            }
944
 
945
            if($order_field == 'added_on') {
946
                if($order_direction == 'asc') {
558 stevensc 947
                    usort($capsulesData, function($a, $b) {
245 efrain 948
                        return strcasecmp($a['added_on'], $b['added_on']);
949
                    });
950
                } else {
558 stevensc 951
                    usort($capsulesData, function($a, $b) {
245 efrain 952
                        $result = strcasecmp($a['added_on'], $b['added_on']);
558 stevensc 953
                        if($result < 0) { return 1; } else if($result > 0) { return -1; } else { return 0; }
245 efrain 954
                    });
955
                }
956
            }
246 efrain 957
 
958
            if($order_field == 'last_access_on') {
959
                if($order_direction == 'asc') {
558 stevensc 960
                    usort($capsulesData, function($a, $b) {
246 efrain 961
                        return strcasecmp($a['last_access_on'], $b['last_access_on']);
962
                    });
963
                } else {
558 stevensc 964
                    usort($capsulesData, function($a, $b) {
246 efrain 965
                        $result = strcasecmp($a['last_access_on'], $b['last_access_on']);
558 stevensc 966
                        if($result < 0) { return 1; } else if($result > 0) { return -1; } else { return 0; }
246 efrain 967
                    });
968
                }
969
            }
245 efrain 970
 
246 efrain 971
 
119 efrain 972
            return new JsonModel([
973
                'success' => true,
558 stevensc 974
                'data' => $capsulesData
119 efrain 975
            ]);
976
        }
977
 
978
        return new JsonModel([
979
            'success' => false,
980
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
981
        ]);
982
    }
983
 
984
 
985
    public function timelineAction()
986
    {
987
 
988
        $request = $this->getRequest();
989
        if($request->isGet()) {
990
            $currentUserPlugin = $this->plugin('currentUserPlugin');
991
            $currentUser = $currentUserPlugin->getUser();
992
 
993
 
994
            $page = intval($this->params()->fromQuery('page'), 10);
995
 
996
            $activities = [
283 www 997
                MicrolearningUserLog::ACTIVITY_SIGNIN            => 'LABEL_MICROLEARNING_ACTIVITY_SIGNIN',
998
                MicrolearningUserLog::ACTIVITY_SIGNOUT           => 'LABEL_MICROLEARNING_ACTIVITY_SIGNOUT',
999
                MicrolearningUserLog::ACTIVITY_START_TOPIC       => 'LABEL_MICROLEARNING_ACTIVITY_START_TOPIC',
1000
                MicrolearningUserLog::ACTIVITY_START_CAPSULE     => 'LABEL_MICROLEARNING_ACTIVITY_START_CAPSULE',
1001
                MicrolearningUserLog::ACTIVITY_VIEW_SLIDE        => 'LABEL_MICROLEARNING_ACTIVITY_VIEW_SLIDE',
1002
                MicrolearningUserLog::ACTIVITY_TAKE_A_TEST       => 'LABEL_MICROLEARNING_ACTIVITY_TAKE_A_TEST',
1003
                MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST     => 'LABEL_MICROLEARNING_ACTIVITY_RETAKE_A_TEST',
1004
                MicrolearningUserLog::ACTIVITY_APPROVED_TEST     => 'LABEL_MICROLEARNING_ACTIVITY_APPROVED_TEST',
1005
                MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE => 'LABEL_MICROLEARNING_ACTIVITY_COMPLETED_CAPSULE',
1006
                MicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC   => 'LABEL_MICROLEARNING_ACTIVITY_COMPLETED_TOPIC',
119 efrain 1007
            ];
1008
 
1009
 
1010
 
283 www 1011
            $microlearningUserLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
1012
            $paginator = $microlearningUserLogMapper->getAllMessagesPaginatorByUserId($currentUser->id, $page);
119 efrain 1013
 
1014
            $items = [];
1015
            foreach($paginator as $record)
1016
            {
1017
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
1018
 
1019
                array_push($items, [
1020
                    'activity' => $activities[$record->activity],
1021
                    'added_on' => $dt->format('d/m/Y H:i a')
1022
                ]);
1023
            }
1024
 
1025
 
1026
            return new JsonModel([
1027
                'success' => true,
1028
                'data' => [
1029
                    'total' => [
1030
                        'count' => $paginator->getTotalItemCount(),
1031
                        'pages' => $paginator->getPages()->pageCount,
1032
                    ],
1033
                    'current' => [
1034
                        'items'    => $items,
1035
                        'page'     => $paginator->getCurrentPageNumber(),
1036
                        'count'    => $paginator->getCurrentItemCount(),
1037
                    ]
1038
                ]
1039
            ]);
1040
 
1041
 
1042
        } else {
1043
            return new JsonModel([
1044
                'success' => false,
1045
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1046
            ]);
1047
        }
1048
    }
1049
 
1050
    /**
1051
     * Valores para la generación de los gráficos de progreso
1052
     * Para las repuesta afirmativa
1053
     * [
1054
     *  'success' => true,
1055
     *      'data' => [
1056
     *          'topicTotal' => cantidad total de tópicos,
1057
     *          'topicStarted' => cantidad de tópicos iniciados,
1058
     *          'topicIncompleted' => cantidad de tópicos incompletos,
1059
     *          'topicCompleted' => cantidad de tópicos completos,
1060
     *          'percentCompleted' => % de diapositivas completados ,
1061
     *          'percentIncompleted' => % de diapositivas incompletos ,
1062
     *          'percentWithoutReturning' => % de cápsulas sin retorno después de completada,
1063
     *          'percentWithReturning' => % de cápsulas con retorno después de completada,
558 stevensc 1064
     *      ],
119 efrain 1065
     * ]
1066
     *
1067
     *
1068
     *  En caso contrario
1069
     *  [
1070
     *      'success' => false,
1071
     *      'data' => mensaje de error
1072
     *  ]
1073
     *
1074
     *
1075
     * @return \Laminas\View\Model\JsonModel
1076
     */
1077
    public function progressAction()
1078
    {
1079
 
1080
        $request = $this->getRequest();
1081
        if($request->isGet()) {
1082
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1083
            $currentUser = $currentUserPlugin->getUser();
1084
 
1085
 
1086
            $accessGrantedIds = $this->getAccessGranted();
1087
            $id = $this->params()->fromRoute('id');
1088
 
1089
            $companyMapper = CompanyMapper::getInstance($this->adapter);
1090
            $company = $companyMapper->fetchOneByUuid($id);
1091
 
1092
            if(!$company) {
1093
                $response = [
1094
                    'success' => false,
1095
                    'data' => 'ERROR_COMPANY_NOT_FOUND',
1096
                ];
1097
 
1098
 
1099
                return new JsonModel($response);
1100
            }
1101
 
1102
            if(!in_array($company->id, $accessGrantedIds->companies)) {
1103
                $response = [
1104
                    'success' => false,
1105
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_COMPANY',
1106
                ];
1107
 
1108
 
1109
                return new JsonModel($response);
1110
            }
1111
 
1112
            $capsuleTotal              = 0;
1113
            $capsuleCompleted          = 0;
1114
            $capsuleWithReturning      = 0;
1115
            $capsuleWithoutReturning   = 0;
1116
            $capsuleStarted            = 0;
1117
            $capsuleToStart            = 0;
1118
            $percentCompleted          = 0;
1119
            $percentIncompleted        = 100;
1120
 
283 www 1121
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 1122
            $topics = $topicMapper->fetchAllActiveByCompanyIdAndIds($company->id, $accessGrantedIds->topics);
1123
 
302 www 1124
            //$capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
283 www 1125
            $progressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
119 efrain 1126
 
302 www 1127
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
119 efrain 1128
 
302 www 1129
            //$capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
1130
 
1131
 
119 efrain 1132
            foreach($topics as $topic)
1133
            {
302 www 1134
                $resultCount = $topicCapsuleMapper->fetchCountByCompanyIdAndTopicId($company->id, $topic->id);
119 efrain 1135
                $capsuleTotal =  $capsuleTotal + $resultCount;
1136
 
1137
                $resultCount = $progressMapper->fetchCountCapsulesCompletedByIdAndTopicId($currentUser->id, $topic->id);
1138
                $capsuleCompleted = $capsuleCompleted + $resultCount;
1139
 
1140
                $resultCount = $progressMapper->fetchCountCapsulesCompletedWithReturningByIdAndTopicId($currentUser->id, $topic->id);
1141
                $capsuleWithReturning = $capsuleWithReturning + $resultCount;
1142
 
1143
                $resultCount = $progressMapper->fetchCountCapsulesCompletedWithoutReturningByIdAndTopicId($currentUser->id, $topic->id);
1144
                $capsuleWithoutReturning = $capsuleWithoutReturning + $resultCount;
1145
 
1146
                $resultCount = $progressMapper->fetchCountCapsulesCompletedByIdAndTopicId($currentUser->id, $topic->id);
1147
                $capsuleStarted = $capsuleStarted + $resultCount;
1148
            }
1149
 
1150
            $capsuleToStart = $capsuleTotal -  $capsuleStarted;
1151
 
1152
 
1153
            if($capsuleTotal > 0) {
1154
                $percentCompleted = ($capsuleCompleted * 100) /  $capsuleTotal;
1155
                $percentIncompleted = 100 - $percentCompleted;
1156
            }
1157
 
1158
 
1159
 
1160
            return new JsonModel([
1161
                'success' => true,
1162
                'data' => [
1163
                    'capsuleTotal' => $capsuleTotal,
1164
                    'capsuleCompleted' => $capsuleCompleted,
1165
                    'capsuleStarted' => $capsuleStarted,
1166
                    'capsuleToStart' => $capsuleToStart,
1167
                    'percentCompleted' => number_format($percentCompleted, 2, '.', ','),
1168
                    'percentIncompleted' => number_format($percentIncompleted, 2, '.', ','),
1169
                    'capsuleWithReturning' => $capsuleWithReturning,
1170
                    'capsuleWithoutReturning' => $capsuleWithoutReturning,
1171
                ],
1172
            ]);
1173
 
1174
 
1175
        } else {
1176
            return new JsonModel([
1177
                'success' => false,
1178
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1179
            ]);
1180
        }
1181
    }
1182
 
1183
 
1184
    public function topicsAction()
1185
    {
1186
        $request = $this->getRequest();
1187
        if($request->isGet()) {
1188
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1189
            $currentUser = $currentUserPlugin->getUser();
1190
 
1191
 
1192
            $data = [];
1193
            $accessGrantedIds = $this->getAccessGranted();
1194
 
283 www 1195
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1196
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 1197
 
333 www 1198
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 1199
            $path = $storage->getPathMicrolearningTopic();
119 efrain 1200
 
1201
            foreach($accessGrantedIds->topics as $id)
1202
            {
1203
                $topic = $topicMapper->fetchOne($id);
1204
                if(!$topic) {
1205
                    continue;
1206
                }
1207
 
1208
                $userProgress = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $id);
1209
                if($userProgress) {
1210
                    $progress = $userProgress->progress;
1211
                    $completed = $userProgress->completed;
1212
                } else {
1213
                    $progress = 0;
1214
                    $completed = 0;
1215
                }
1216
 
1217
 
1218
 
1219
                array_push($data, [
164 efrain 1220
                    'uuid'          => $topic->uuid,
119 efrain 1221
                    'name'          => $topic->name ? $topic->name : '',
1222
                    'description'   => $topic->description ? $topic->description : '',
283 www 1223
                    'image'         => $topic->image ? $storage->getGenericImage($path, $topic->uuid, $topic->image) : '',
119 efrain 1224
                    'progress'      => $progress,
1225
                    'completed'     => $completed,
1226
                    'order'         => $topic->order,
1227
                    'added_on'      => $topic->added_on,
564 stevensc 1228
                    'updated_on'    => $topic->updated_on
119 efrain 1229
                ]);
1230
            }
1231
 
1232
            usort($data, function($a, $b) {
1233
 
1234
                $result =  $a['order'] <=> $b['order'];
1235
                if(0 == $result) {
1236
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1237
                    if(0 == $result) {
1238
                        $result = strcasecmp($a['name'], $b['name']);
1239
                    }
1240
                }
1241
 
1242
                if($result < 0) {
1243
                    return 1;
1244
                } else if($result > 0) {
1245
                    return -1;
1246
                } else  {
1247
                    return  0;
1248
                }
1249
            });
1250
 
1251
 
1252
 
1253
                return new JsonModel([
1254
                    'success' => true,
1255
                    'data' => $data
1256
                ]);
1257
 
1258
        } else {
1259
            return new JsonModel([
1260
                'success' => false,
1261
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1262
            ]);
1263
        }
1264
    }
1265
 
161 efrain 1266
    public function getTopicAction()
1267
    {
563 stevensc 1268
        // Handle GET request
161 efrain 1269
        $request = $this->getRequest();
1270
        if($request->isGet()) {
563 stevensc 1271
            // Get current user
161 efrain 1272
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1273
            $currentUser = $currentUserPlugin->getUser();
563 stevensc 1274
 
1275
            // Get topic ID from route
161 efrain 1276
            $id = $this->params()->fromRoute('id');
283 www 1277
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
161 efrain 1278
            $topic = $topicMapper->fetchOneByUuid($id);
563 stevensc 1279
 
1280
            // Return error if topic not found
161 efrain 1281
            if(!$topic) {
1282
                return new JsonModel([
1283
                    'success' => false,
1284
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1285
                ]);
1286
            }
563 stevensc 1287
 
1288
            // Check if current user has access to the topic
161 efrain 1289
            $accessGrantedIds = $this->getAccessGranted();
1290
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1291
                return new JsonModel([
1292
                    'success' => false,
1293
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1294
                ]);
1295
            }
563 stevensc 1296
 
1297
            // Get user progress for this topic
283 www 1298
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
563 stevensc 1299
            $userProgress = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic->id);
161 efrain 1300
 
580 stevensc 1301
            $progress = $userProgress->progress ?? 0;
1302
            $completed = $userProgress->completed ?? 0;
563 stevensc 1303
 
1304
            // Initialize storage service to get image paths
333 www 1305
            $storage = Storage::getInstance($this->config, $this->adapter);
563 stevensc 1306
            $pathTopic = $storage->getPathMicrolearningTopic();
1307
 
1308
            // Fetch associated capsules using the private helper method
1309
            $capsulesData = $this->_getCapsulesByTopic($topic, $storage);
1310
 
1311
            // Prepare data for JSON response
161 efrain 1312
            $data = [
165 efrain 1313
                'uuid'          => $topic->uuid,
161 efrain 1314
                'name'          => $topic->name ? $topic->name : '',
1315
                'description'   => $topic->description ? $topic->description : '',
563 stevensc 1316
                'image'         => $topic->image ? $storage->getGenericImage($pathTopic, $topic->uuid, $topic->image) : '',
161 efrain 1317
                'progress'      => $progress,
1318
                'completed'     => $completed,
1319
                'order'         => $topic->order,
1320
                'added_on'      => $topic->added_on,
1321
                'updated_on'    => $topic->updated_on,
563 stevensc 1322
                'total_capsules' => count($capsulesData),
1323
                'capsules'      => $capsulesData,
161 efrain 1324
            ];
563 stevensc 1325
 
161 efrain 1326
            return new JsonModel([
1327
                'success' => true,
1328
                'data' => $data
1329
            ]);
563 stevensc 1330
 
161 efrain 1331
        } else {
563 stevensc 1332
            // Return error if not a GET request
161 efrain 1333
            return new JsonModel([
1334
                'success' => false,
1335
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1336
            ]);
1337
        }
1338
    }
563 stevensc 1339
 
1340
    /**
580 stevensc 1341
     * @param MicrolearningTopic $topic The topic object.
1342
     * @param Storage $storage The storage object.
1343
     * @return array The capsules data.
563 stevensc 1344
     */
1345
    private function _getCapsulesByTopic($topic, $storage)
1346
    {
578 stevensc 1347
        $data = [];
584 stevensc 1348
        $accessGrantedIds = $this->getAccessGranted();
563 stevensc 1349
 
578 stevensc 1350
        if (!$topic) {
1351
            return $data;
1352
        }
1353
 
584 stevensc 1354
        if (!in_array($topic->id, $accessGrantedIds->topics)) {
578 stevensc 1355
            return $data;
584 stevensc 1356
        }
578 stevensc 1357
 
1358
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1359
        $currentUser = $currentUserPlugin->getUser();
1360
 
1361
        $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
574 stevensc 1362
        $topicCapsuleRelations = $topicCapsuleMapper->fetchAllActiveByTopicId($topic->id);
1363
 
581 stevensc 1364
        $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1365
        $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
578 stevensc 1366
 
581 stevensc 1367
        $path = $storage->getPathMicrolearningCapsule();
1368
 
578 stevensc 1369
        foreach ($topicCapsuleRelations as $relation) {
584 stevensc 1370
            if(!in_array($relation->capsule_id, $accessGrantedIds->capsules)) {
1371
                continue;
1372
            }
578 stevensc 1373
 
581 stevensc 1374
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1375
            $capsule = $capsuleMapper->fetchOne($relation->capsule_id);
563 stevensc 1376
 
581 stevensc 1377
            if(!$capsule) {
1378
                continue;
1379
            }
1380
 
578 stevensc 1381
            $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
1382
            $progress = $userProgress->progress ?? 0;
1383
            $completed = $userProgress->completed ?? 0;
1384
            $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->id);
1385
            $image = $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image) : '';
1386
            $total_comments = strval($dataCountAndRatingAverage['total_comments']);
1387
            $total_rating = strval($dataCountAndRatingAverage['total_rating']);
1388
 
581 stevensc 1389
            array_push($data, [
578 stevensc 1390
                'uuid'              => $capsule->uuid,
1391
                'name'              => $capsule->name ? $capsule->name : '',
1392
                'description'       => $capsule->description ? $capsule->description : '',
1393
                'image'             => $image,
1394
                'total_comments'    => $total_comments,
1395
                'total_rating'      => $total_rating,
1396
                'progress'          => $progress,
1397
                'completed'         => $completed,
1398
                'order'             => $capsule->order,
1399
                'added_on'          => $capsule->added_on,
1400
                'updated_on'        => $capsule->updated_on,
581 stevensc 1401
            ]);
563 stevensc 1402
        }
578 stevensc 1403
 
581 stevensc 1404
        return $data;
563 stevensc 1405
    }
586 stevensc 1406
 
1407
    /**
1408
     * @param MicrolearningCapsule $capsule The capsule object.
1409
     * @param Storage $storage The storage object.
1410
     * @return array The slides data.
1411
     */
1412
    private function _getSlidesByCapsule($capsule, $storage)
1413
    {
1414
        $data = [];
1415
        $accessGrantedIds = $this->getAccessGranted();
1416
 
1417
        if (!$capsule) {
1418
            return $data;
1419
        }
1420
 
1421
        if (!in_array($capsule->id, $accessGrantedIds->capsules)) {
1422
            return $data;
1423
        }
1424
 
1425
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1426
        $currentUser = $currentUserPlugin->getUser();
1427
 
1428
        $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
1429
        $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1430
        $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
1431
 
1432
        $path = $storage->getPathMicrolearningSlide();
587 stevensc 1433
        $slides = $slideMapper->fetchAllByCompanyIdAndCapsuleId($capsule->company_id, $capsule->id);
586 stevensc 1434
 
1435
        foreach ($slides as $slide) {
1436
            $userProgress = $userProgressMapper->fetchOneByUserIdAndSlideId($currentUser->id, $slide->id);
1437
            $completed = $userProgress ? $userProgress->completed : 0;
1438
 
1439
            $quiz_uuid = '';
1440
            $quiz_data = [];
1441
            $link_take_a_test = '';
1442
 
1443
            if ($slide->quiz_id) {
1444
                $quiz = $quizMapper->fetchOne($slide->quiz_id);
1445
                if ($quiz) {
1446
                    $quiz_uuid = $quiz->uuid;
1447
                    $quiz_data = $this->getQuiz($slide->quiz_id);
1448
                    $link_take_a_test = $this->url()->fromRoute('microlearning/take-a-test', ['slide_id' => $slide->uuid], ['force_canonical' => true]);
1449
                }
1450
            }
1451
 
1452
            array_push($data, [
1453
                'quiz'                  => $quiz_uuid,
1454
                'quiz_data'             => $quiz_data,
1455
                'uuid'                  => $slide->uuid,
1456
                'name'                  => $slide->name ? $slide->name : '',
1457
                'description'           => $slide->description ? $slide->description : '',
1458
                'type'                  => $slide->type,
1459
                'background'            => $slide->background ? $storage->getGenericImage($path, $slide->uuid, $slide->background) : '',
1460
                'file'                  => $slide->file ? $storage->getGenericFile($path, $slide->uuid, $slide->file) : '',
1461
                'order'                 => $slide->order,
1462
                'completed'             => $completed,
1463
                'link_take_a_test'      => $link_take_a_test,
1464
                'added_on'              => $slide->added_on,
1465
                'updated_on'            => $slide->updated_on,
1466
            ]);
1467
        }
1468
 
1469
        // Sort slides by order, then by added_on, then by name
1470
        usort($data, function($a, $b) {
1471
            $result = $a['order'] <=> $b['order'];
1472
            if (0 == $result) {
1473
                $result = strcasecmp($a['added_on'], $b['added_on']);
1474
                if (0 == $result) {
1475
                    $result = strcasecmp($a['name'], $b['name']);
1476
                }
1477
            }
1478
 
1479
            if ($result < 0) {
1480
                return 1;
1481
            } else if ($result > 0) {
1482
                return -1;
1483
            } else {
1484
                return 0;
1485
            }
1486
        });
1487
 
1488
        return $data;
1489
    }
1490
 
119 efrain 1491
    public function capsulesAction()
1492
    {
1493
        $request = $this->getRequest();
1494
        if($request->isGet()) {
580 stevensc 1495
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1496
            $currentUser = $currentUserPlugin->getUser();
584 stevensc 1497
 
1498
            $topic_id_param = $this->params()->fromRoute('topic_id');
283 www 1499
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
580 stevensc 1500
            $topic = $topicMapper->fetchOneByUuid($topic_id_param);
584 stevensc 1501
 
119 efrain 1502
            if(!$topic) {
1503
                return new JsonModel([
1504
                    'success' => false,
1505
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1506
                ]);
1507
            }
584 stevensc 1508
 
580 stevensc 1509
            $accessGrantedIds = $this->getAccessGranted();
584 stevensc 1510
 
119 efrain 1511
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1512
                return new JsonModel([
1513
                    'success' => false,
1514
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1515
                ]);
1516
            }
584 stevensc 1517
 
333 www 1518
            $storage = Storage::getInstance($this->config, $this->adapter);
584 stevensc 1519
            // Use the _getCapsulesByTopic method to fetch capsules
1520
            $data = $this->_getCapsulesByTopic($topic, $storage);
580 stevensc 1521
 
584 stevensc 1522
            // The _getCapsulesByTopic method already returns capsules with the required data structure.
1523
            // We just need to sort them.
1524
 
580 stevensc 1525
            usort($data, function($a, $b) {
584 stevensc 1526
 
580 stevensc 1527
                $result =  $a['order'] <=> $b['order'];
1528
                if(0 == $result) {
1529
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1530
                    if(0 == $result) {
1531
                        $result = strcasecmp($a['name'], $b['name']);
1532
                    }
1533
                }
584 stevensc 1534
 
580 stevensc 1535
                if($result < 0) {
1536
                    return 1;
1537
                } else if($result > 0) {
1538
                    return -1;
1539
                } else  {
1540
                    return  0;
1541
                }
1542
            });
584 stevensc 1543
 
1544
            return new JsonModel([
1545
                'success' => true,
1546
                'data' => $data
1547
            ]);
1548
 
119 efrain 1549
        } else {
1550
            return new JsonModel([
1551
                'success' => false,
1552
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1553
            ]);
1554
        }
1555
    }
1556
 
235 efrain 1557
    public function takeTestAction()
1558
    {
1559
        $request = $this->getRequest();
1560
        if($request->isPost()) {
1561
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1562
            $currentUser = $currentUserPlugin->getUser();
1563
 
1564
            $slide_id = $this->params()->fromRoute('slide_id');
283 www 1565
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
235 efrain 1566
            $slide = $slideMapper->fetchOneByUuid($slide_id);
1567
 
1568
            if(!$slide) {
1569
                return new JsonModel([
1570
                    'success' => false,
1571
                    'data' => 'ERROR_SLIDE_NOT_FOUND'
1572
                ]);
1573
            }
1574
 
283 www 1575
            if($slide->type != MicrolearningSlide::TYPE_QUIZ) {
235 efrain 1576
                return new JsonModel([
1577
                    'success' => false,
1578
                    'data' => 'ERROR_SLIDE_IS_NOT_QUIZ'
1579
                ]);
1580
            }
1581
 
1582
 
1583
 
283 www 1584
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
235 efrain 1585
            $capsule = $capsuleMapper->fetchOne($slide->capsule_id);
1586
 
1587
            if(!$capsule) {
1588
                return new JsonModel([
1589
                    'success' => false,
1590
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
1591
                ]);
1592
            }
1593
 
1594
            $accessGrantedIds = $this->getAccessGranted();
1595
 
1596
            if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1597
                return new JsonModel([
1598
                    'success' => false,
1599
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1600
                ]);
1601
            }
1602
 
283 www 1603
            $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
235 efrain 1604
            $userLog = $userLogMapper->fetchOneTakeATestByUserIdAndSlideId($currentUser->id, $slide->id);
1605
 
1606
            if($userLog) {
283 www 1607
                $activity = MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST;
235 efrain 1608
            } else {
283 www 1609
                $activity = MicrolearningUserLog::ACTIVITY_TAKE_A_TEST;
235 efrain 1610
            }
1611
 
1612
            $added_on = $userLogMapper->getDatebaseNow();
1613
 
283 www 1614
            $userLog = new MicrolearningUserLog();
235 efrain 1615
            $userLog->activity      = $activity;
1616
            $userLog->user_id       = $currentUser->id;
1617
            $userLog->company_id    = $slide->company_id;
1618
            $userLog->topic_id      = $slide->topic_id;
1619
            $userLog->capsule_id    = $slide->capsule_id;
1620
            $userLog->slide_id      = $slide->id;
1621
            $userLog->added_on      = $added_on;
1622
 
1623
            if($userLogMapper->insert($userLog)) {
1624
                return new JsonModel([
1625
                    'success' => true,
1626
                ]);
1627
            } else {
1628
                return new JsonModel([
1629
                    'success' => false,
1630
                    'data' => $userLogMapper->getError()
1631
                ]);
1632
            }
1633
 
1634
 
1635
 
1636
 
1637
 
1638
 
1639
 
1640
        } else {
1641
            return new JsonModel([
1642
                'success' => false,
1643
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1644
            ]);
1645
        }
1646
    }
1647
 
161 efrain 1648
    public function getCapsuleAction()
1649
    {
1650
        $request = $this->getRequest();
587 stevensc 1651
 
161 efrain 1652
        if($request->isGet()) {
1653
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1654
            $currentUser = $currentUserPlugin->getUser();
1655
 
558 stevensc 1656
            $capsule_uuid = $this->params()->fromRoute('id');
283 www 1657
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
558 stevensc 1658
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
161 efrain 1659
 
1660
            if(!$capsule) {
1661
                return new JsonModel([
1662
                    'success' => false,
1663
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
1664
                ]);
1665
            }
1666
 
1667
            $accessGrantedIds = $this->getAccessGranted();
1668
 
1669
            if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1670
                return new JsonModel([
1671
                    'success' => false,
1672
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1673
                ]);
1674
            }
1675
 
283 www 1676
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1677
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
161 efrain 1678
 
1679
            $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
587 stevensc 1680
            $progress = $userProgress->progress ?? 0;
1681
            $completed = $userProgress->completed ?? 0;
244 efrain 1682
 
586 stevensc 1683
            $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->id);
244 efrain 1684
 
587 stevensc 1685
            // Get comments data
1686
            $userMapper = UserMapper::getInstance($this->adapter);
333 www 1687
            $storage = Storage::getInstance($this->config, $this->adapter);
587 stevensc 1688
            $serviceDatetimeFormat = $this->config['leaderslinked.services.datetime'];
1689
 
1690
            $comments = [];
1691
            $records = $capsuleCommentMapper->fetchAllByCapsuleId($capsule->id);
1692
            foreach($records as $record) {
1693
                $user = $userMapper->fetchOne($record->user_id);
1694
                if(!$user) {
1695
                    continue;
1696
                }
1697
 
1698
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
1699
 
1700
                array_push($comments, [
1701
                    'date' => $dt->format($serviceDatetimeFormat),
1702
                    'image' => $storage->getUserImage($user),
1703
                    'fullname' => trim(trim($user->first_name) . ' ' . trim($user->last_name)),
1704
                    'rating' => strval($record->rating),
1705
                    'comment' => $record->comment,
1706
                    'link_delete' => $record->user_id == $currentUser->id ? $this->url()->fromRoute('microlearning/capsules-comments/delete', ['capsule_id' => $capsule->uuid, 'comment_id' => $record->uuid], ['force_canonical' => true]) : '',
1707
                ]);
1708
            }
1709
 
1710
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 1711
            $path = $storage->getPathMicrolearningCapsule();
244 efrain 1712
 
586 stevensc 1713
            $slides = $this->_getSlidesByCapsule($capsule, $storage);
1714
 
161 efrain 1715
            $data = [
586 stevensc 1716
                'uuid'              => $capsule->uuid,
1717
                'name'              => $capsule->name ? $capsule->name : '',
1718
                'description'       => $capsule->description ? $capsule->description : '',
1719
                'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image) : '',
1720
                'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
1721
                'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
1722
                'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
587 stevensc 1723
                'comments'          => $comments,
586 stevensc 1724
                'progress'          => $progress,
1725
                'completed'         => $completed,
1726
                'total_slides'      => count($slides),
1727
                'slides'            => $slides,
1728
                'order'             => $capsule->order,
1729
                'added_on'          => $capsule->added_on,
1730
                'updated_on'        => $capsule->updated_on,
161 efrain 1731
            ];
586 stevensc 1732
 
161 efrain 1733
            return new JsonModel([
1734
                'success' => true,
1735
                'data' => $data
1736
            ]);
1737
        }
586 stevensc 1738
 
1739
        return new JsonModel([
1740
            'success' => false,
1741
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1742
        ]);
161 efrain 1743
    }
1744
 
119 efrain 1745
    public function slidesAction()
1746
    {
1747
        $request = $this->getRequest();
1748
        if($request->isGet()) {
1749
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1750
            $currentUser = $currentUserPlugin->getUser();
1751
 
1752
 
1753
 
1754
            $topic_id = $this->params()->fromRoute('topic_id');
283 www 1755
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 1756
            $topic = $topicMapper->fetchOneByUuid($topic_id);
1757
 
1758
            if(!$topic) {
1759
                return new JsonModel([
1760
                    'success' => false,
1761
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1762
                ]);
1763
            }
1764
 
1765
            $accessGrantedIds = $this->getAccessGranted();
1766
 
1767
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1768
                return new JsonModel([
1769
                    'success' => false,
1770
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1771
                ]);
1772
            }
1773
 
1774
            $capsule_id = $this->params()->fromRoute('capsule_id');
283 www 1775
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
119 efrain 1776
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
1777
 
1778
            if(!$capsule) {
1779
                return new JsonModel([
1780
                    'success' => false,
1781
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
1782
                ]);
1783
            }
1784
 
1785
 
1786
 
1787
            if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1788
                return new JsonModel([
1789
                    'success' => false,
1790
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1791
                ]);
1792
            }
1793
 
1794
 
1795
 
1796
            $data = [];
1797
 
283 www 1798
            $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
119 efrain 1799
 
283 www 1800
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
1801
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
119 efrain 1802
 
333 www 1803
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 1804
            $path = $storage->getPathMicrolearningSlide();
119 efrain 1805
 
1806
            $slides = $slideMapper->fetchAllByCompanyIdAndTopicIdAndCapsuleId($capsule->company_id, $capsule->topic_id, $capsule->id);
1807
            foreach($slides as $slide)
1808
            {
1809
 
1810
 
1811
                $userProgress = $userProgressMapper->fetchOneByUserIdAndSlideId($currentUser->id, $slide->id);
1812
                if($userProgress) {
224 efrain 1813
                    $completed =  $userProgress->completed ;
119 efrain 1814
                } else {
1815
                    $completed = 0;
1816
                }
1817
 
235 efrain 1818
 
230 efrain 1819
 
235 efrain 1820
                $link_take_a_test = '';
230 efrain 1821
                if($slide->quiz_id) {
235 efrain 1822
 
283 www 1823
                    $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
230 efrain 1824
                    $quiz = $quizMapper->fetchOne($slide->quiz_id);
1825
                    if($quiz) {
1826
                        $quiz_uuid = $quiz->uuid;
235 efrain 1827
                        $quiz_data = $this->getQuiz($slide->quiz_id);
1828
                        $link_take_a_test = $this->url()->fromRoute('microlearning/take-a-test', ['slide_id' => $slide->uuid], ['force_canonical' => true]);
230 efrain 1829
                    }
235 efrain 1830
 
1831
                }
119 efrain 1832
 
1833
 
1834
                array_push($data, [
235 efrain 1835
                    'quiz'                  => $quiz_uuid,
1836
                    'quiz_data'             => $quiz_data,
1837
                    'uuid'                  => $slide->uuid,
1838
                    'name'                  => $slide->name ? $slide->name : '',
1839
                    'description'           => $slide->description ? $slide->description : '',
1840
                    'type'                  => $slide->type,
283 www 1841
                    'background'            => $slide->background ? $storage->getGenericImage($path, $slide->uuid, $slide->background) : '',
1842
                    'file'                  => $slide->file ? $storage->getGenericFile($path, $slide->uuid, $slide->file) : '',
235 efrain 1843
                    'order'                 => $slide->order,
1844
                    'completed'             => $completed,
1845
                    'link_get'              => $this->url()->fromRoute('microlearning/get-slide', ['id' => $slide->uuid], ['force_canonical' => true]),
1846
                    'link_sync'             => $this->url()->fromRoute('microlearning/sync', ['operation' => 'slide-view', 'topic_uuid' => $topic->uuid, 'capsule_uuid' => $capsule->uuid, 'slide_uuid' => $slide->uuid], ['force_canonical' => true]),
236 efrain 1847
                    'link_take_a_test'      => $link_take_a_test,
235 efrain 1848
                    'added_on'              => $slide->added_on,
1849
                    'updated_on'            => $slide->updated_on,
119 efrain 1850
                ]);
1851
 
1852
 
1853
 
1854
            }
1855
 
1856
            usort($data, function($a, $b) {
1857
 
1858
                $result =  $a['order'] <=> $b['order'];
1859
                if(0 == $result) {
1860
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1861
                    if(0 == $result) {
1862
                        $result = strcasecmp($a['name'], $b['name']);
1863
                    }
1864
                }
1865
 
1866
                if($result < 0) {
1867
                    return 1;
1868
                } else if($result > 0) {
1869
                    return -1;
1870
                } else  {
1871
                    return  0;
1872
                }
1873
            });
1874
 
1875
 
1876
 
1877
                return new JsonModel([
1878
                    'success' => true,
1879
                    'data' => $data
1880
                ]);
1881
 
1882
        } else {
1883
            return new JsonModel([
1884
                'success' => false,
1885
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1886
            ]);
1887
        }
1888
    }
1889
 
229 efrain 1890
    private function getQuiz($id)
1891
    {
1892
        $serviceDatetimeFormat = $this->config['leaderslinked.services.datetime'];
1893
 
1894
        $data = [];
1895
 
283 www 1896
        $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
229 efrain 1897
        $quiz = $quizMapper->fetchOne($id);
1898
 
1899
        if(!$quiz) {
1900
            return [];
1901
        }
1902
 
1903
        $companyMapper = CompanyMapper::getInstance($this->adapter);
1904
        $company = $companyMapper->fetchOne($quiz->company_id);
1905
 
283 www 1906
        $questionMapper = MicrolearningQuestionMapper::getInstance($this->adapter);
1907
        $answerMapper = MicrolearningAnswerMapper::getInstance($this->adapter);
229 efrain 1908
 
1909
        $record_questions = [];
1910
        $questions = $questionMapper->fetchAllByQuizId($quiz->id);
1911
        foreach($questions as $question)
1912
        {
1913
            $record_answers = [];
1914
 
1915
            $answers = $answerMapper->fetchAllByQuizIdAndQuestionId($question->quiz_id, $question->id);
1916
            foreach($answers as $answer)
1917
            {
1918
                $dtAddedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $answer->added_on);
1919
                $dtUpdatedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $answer->updated_on);
1920
 
1921
                array_push($record_answers, [
1922
                    'uuid' => $answer->uuid,
1923
                    'text' => trim($answer->text),
1924
                    'correct' => $answer->correct ? $answer->correct  : 0 ,
1925
                    'points' => strval(intval($answer->points, 10)),
1926
                    'added_on'  => $dtAddedOn->format($serviceDatetimeFormat),
1927
                    'updated_on'    => $dtUpdatedOn->format($serviceDatetimeFormat),
1928
                ]);
1929
            }
1930
 
1931
            $dtAddedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $question->added_on);
1932
            $dtUpdatedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $question->updated_on);
1933
 
1934
            array_push($record_questions, [
1935
                'uuid'          => $question->uuid,
1936
                'text'          => trim($question->text),
1937
                'type'          => $question->type,
1938
                'maxlength'     => strval($question->maxlength),
1939
                'points'        => strval($question->points),
1940
                'answers'       => $record_answers,
1941
                'added_on'      => $dtAddedOn->format($serviceDatetimeFormat),
1942
                'updated_on'    => $dtUpdatedOn->format($serviceDatetimeFormat),
1943
            ]);
1944
        }
1945
 
1946
        $dtAddedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $quiz->added_on);
1947
        $dtUpdatedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $quiz->updated_on);
1948
 
1949
        array_push($data, [
1950
            'uuid' => $quiz->uuid,
1951
            'name' => $quiz->name,
1952
            'text' => trim($quiz->text ? $quiz->text : ''),
1953
            'failed' => trim($quiz->failed ? $quiz->failed : ''),
1954
            'points' => strval($quiz->points),
1955
            'minimum_points_required' => strval($quiz->minimum_points_required),
1956
            'max_time' => $quiz->max_time ? $quiz->max_time : 5,
1957
            'company_uuid' => $company->uuid,
1958
            'company_name' => $company->name,
1959
            'company_image' => $this->url()->fromRoute('services/storage',['type' => 'company', 'code' => $company->uuid, 'filename' => $company->image], ['force_canonical' => true]),
1960
            'questions'     => $record_questions,
1961
            'added_on'      => $dtAddedOn->format($serviceDatetimeFormat),
1962
            'updated_on'    => $dtUpdatedOn->format($serviceDatetimeFormat),
1963
        ]);
1964
 
1965
        return $data;
1966
 
1967
    }
161 efrain 1968
 
229 efrain 1969
 
161 efrain 1970
    public function getSlideAction()
1971
    {
1972
        $request = $this->getRequest();
1973
        if($request->isGet()) {
1974
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1975
            $currentUser = $currentUserPlugin->getUser();
1976
 
1977
 
1978
 
1979
            $id = $this->params()->fromRoute('id');
283 www 1980
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
161 efrain 1981
            $slide = $slideMapper->fetchOneByUuid($id);
1982
 
1983
            if(!$slide) {
1984
                return new JsonModel([
1985
                    'success' => false,
1986
                    'data' => 'ERROR_SLIDE_NOT_FOUND'
1987
                ]);
1988
            }
1989
 
1990
            $accessGrantedIds = $this->getAccessGranted();
1991
 
1992
 
1993
 
1994
            if(!in_array($slide->capsule_id, $accessGrantedIds->capsules)) {
1995
                return new JsonModel([
1996
                    'success' => false,
1997
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1998
                ]);
1999
            }
2000
 
2001
 
283 www 2002
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
161 efrain 2003
            $topic = $topicMapper->fetchOne($slide->topic_id);
2004
 
283 www 2005
            $capsuleMapper =  MicrolearningCapsuleMapper::getInstance($this->adapter);
161 efrain 2006
            $capsule = $capsuleMapper->fetchOne($slide->capsule_id);
2007
 
2008
 
2009
 
2010
 
2011
 
283 www 2012
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
2013
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
161 efrain 2014
 
2015
 
2016
            $userProgress = $userProgressMapper->fetchOneByUserIdAndSlideId($currentUser->id, $slide->id);
2017
            if($userProgress) {
2018
                $completed = $userProgress->completed;
2019
            } else {
2020
                $completed = 0;
2021
            }
235 efrain 2022
 
2023
            $quiz_uuid = '';
2024
            $quiz_data = [];
2025
            $link_take_a_test = '';
2026
            if($slide->quiz_id) {
2027
 
283 www 2028
                $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
235 efrain 2029
                $quiz = $quizMapper->fetchOne($slide->quiz_id);
2030
                if($quiz) {
2031
                    $quiz_uuid = $quiz->uuid;
2032
                    $quiz_data = $this->getQuiz($slide->quiz_id);
2033
                    $link_take_a_test = $this->url()->fromRoute('microlearning/take-a-test', ['slide_id' => $slide->uuid], ['force_canonical' => true]);
2034
                }
161 efrain 2035
 
235 efrain 2036
            }
161 efrain 2037
 
333 www 2038
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 2039
            $path = $storage->getPathMicrolearningSlide();
161 efrain 2040
 
2041
            $data =[
235 efrain 2042
                'quiz'              => $quiz_uuid,
2043
                'quiz_data'         => $quiz_data,
2044
                'uuid'              => $slide->uuid,
2045
                'name'              => $slide->name ? $slide->name : '',
2046
                'description'       => $slide->description ? $slide->description : '',
2047
                'type'              => $slide->type,
283 www 2048
                'background'        => $slide->background ? $storage->getGenericImage($path, $slide->uuid, $slide->background) : '',
2049
                'file'              => $slide->file ? $storage->getGenericFile($path, $slide->uuid, $slide->file) : '',
235 efrain 2050
                'order'             => $slide->order,
2051
                'completed'         => $completed,
2052
                'link_sync'         => $this->url()->fromRoute('microlearning/sync', ['operation' => 'slide-view', 'topic_uuid' => $topic->uuid, 'capsule_uuid' => $capsule->uuid, 'slide_uuid' => $slide->uuid], ['force_canonical' => true]),
2053
                'link_take_a_test'  => $link_take_a_test,
2054
                'added_on'          => $slide->added_on,
2055
                'updated_on'        => $slide->updated_on,
161 efrain 2056
            ];
2057
 
2058
 
2059
 
2060
 
2061
            return new JsonModel([
2062
                'success' => true,
2063
                'data' => $data
2064
            ]);
2065
 
2066
        } else {
2067
            return new JsonModel([
2068
                'success' => false,
2069
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
2070
            ]);
2071
        }
2072
    }
2073
 
119 efrain 2074
    public function profileAction()
2075
    {
2076
        $request = $this->getRequest();
2077
        if($request->isGet()) {
2078
            $currentUserPlugin = $this->plugin('currentUserPlugin');
2079
            $currentUser = $currentUserPlugin->getUser();
2080
 
2081
 
2082
            $accessGrantedIds = $this->getAccessGranted();
2083
 
2084
            $companyMapper = CompanyMapper::getInstance($this->adapter);
283 www 2085
            $companyExtendUserMapper = MicrolearningExtendUserMapper::getInstance($this->adapter);
2086
            $companyExtendUserCompanyMapper = MicrolearningExtendUserCompanyMapper::getInstance($this->adapter);
2087
            $companyExtendUserFunctionMapper = MicrolearningExtendUserFunctionMapper::getInstance($this->adapter);
2088
            $companyExtendUserGroupMapper = MicrolearningExtendUserGroupMapper::getInstance($this->adapter);
2089
            $companyExtendUserInstitutionMapper = MicrolearningExtendUserInstitutionMapper::getInstance($this->adapter);
2090
            $companyExtendUserPartnerMapper = MicrolearningExtendUserPartnerMapper::getInstance($this->adapter);
2091
            $companyExtendUserProgramMapper = MicrolearningExtendUserProgramMapper::getInstance($this->adapter);
2092
            $companyExtendUserStudentTypeMapper = MicrolearningExtendUserStudentTypeMapper::getInstance($this->adapter);
2093
            $companyExtendUserSectorMapper = MicrolearningExtendUserSectorMapper::getInstance($this->adapter);
119 efrain 2094
 
333 www 2095
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 2096
 
119 efrain 2097
 
2098
            $data = [];
2099
            foreach($accessGrantedIds->companies as $company_id)
2100
            {
2101
                $company = $companyMapper->fetchOne($company_id);
2102
                if(!$company) {
2103
                    continue;
2104
                }
2105
 
2106
                $record = [
2107
                    'name' => $company->name,
283 www 2108
                    'image' => $storage->getCompanyImage($company),
119 efrain 2109
                    'details' => [],
2110
                ];
2111
 
2112
                $companyExtendUser = $companyExtendUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
2113
                if(!$companyExtendUser) {
2114
                    continue;
2115
                }
2116
 
2117
                if($companyExtendUser->extend_company_id) {
2118
 
2119
                    $extendedCompany = $companyExtendUserCompanyMapper->fetchOne($companyExtendUser->company_id);
2120
                    if($extendedCompany) {
2121
                        array_push($record['details'],[
2122
                            'uuid' => $extendedCompany->uuid,
2123
                            'label' => 'LABEL_COMPANY',
2124
                            'value' => $extendedCompany->name
2125
                        ]);
2126
                    }
2127
                }
2128
 
2129
                if($companyExtendUser->extend_function_id) {
2130
                    $extendedFunction = $companyExtendUserFunctionMapper->fetchOne($companyExtendUser->extend_function_id);
2131
                    if($extendedFunction) {
2132
                        array_push($record['details'],[
2133
                            'uuid' => $extendedFunction->uuid,
2134
                            'label' => 'LABEL_FUNCTION',
2135
                            'value' => $extendedFunction->name
2136
                        ]);
2137
                    }
2138
                }
2139
 
2140
                if($companyExtendUser->extend_group_id) {
2141
                    $extendedGroup = $companyExtendUserGroupMapper->fetchOne($companyExtendUser->extend_group_id);
2142
                    if($extendedGroup) {
2143
                        array_push($record['details'],[
2144
                            'uuid' => $extendedGroup->uuid,
2145
                            'label' => 'LABEL_GROUP',
2146
                            'value' => $extendedGroup->name
2147
                        ]);
2148
                    }
2149
                }
2150
 
2151
                if($companyExtendUser->extend_institution_id) {
2152
                    $extendedInstitution= $companyExtendUserInstitutionMapper->fetchOne($companyExtendUser->extend_institution_id);
2153
                    if($extendedInstitution) {
2154
                        array_push($record['details'],[
2155
                            'uuid' => $extendedInstitution->uuid,
2156
                            'label' => 'LABEL_INSTITUTION',
2157
                            'value' => $extendedInstitution->name
2158
                        ]);
2159
                    }
2160
                }
2161
 
2162
                if($companyExtendUser->extend_program_id) {
2163
                    $extendedProgram = $companyExtendUserProgramMapper->fetchOne($companyExtendUser->extend_program_id);
2164
                    if($extendedProgram) {
2165
                        array_push($record['details'],[
2166
                            'uuid' => $extendedProgram->uuid,
2167
                            'label' => 'LABEL_PROGRAM',
2168
                            'value' => $extendedProgram->name
2169
                        ]);
2170
 
2171
                    }
2172
                }
2173
 
2174
                if($companyExtendUser->extend_sector_id) {
2175
                    $extendedSector = $companyExtendUserSectorMapper->fetchOne($companyExtendUser->extend_sector_id);
2176
                    if($extendedSector) {
2177
                        array_push($record['details'],[
2178
                            'uuid' => $extendedSector->uuid,
2179
                            'label' => 'LABEL_SECTOR',
2180
                            'value' => $extendedSector->name
2181
                        ]);
2182
                    }
2183
                }
2184
 
2185
                if($companyExtendUser->extend_partner_id) {
2186
                    $extendedPartner = $companyExtendUserPartnerMapper->fetchOne($companyExtendUser->extend_partner_id);
2187
                    if($extendedPartner) {
2188
                        array_push($record['details'],[
2189
                            'uuid' => $extendedPartner->uuid,
2190
                            'label' => 'LABEL_PARTNER',
2191
                            'value' => $extendedPartner->name
2192
                        ]);
2193
                    }
2194
                }
2195
 
2196
                if($companyExtendUser->extend_student_type_id) {
2197
                    $extendedStudentType = $companyExtendUserStudentTypeMapper->fetchOne($companyExtendUser->extend_student_type_id);
2198
                    if($extendedStudentType) {
2199
                        array_push($record['details'],[
2200
                            'uuid' => $extendedStudentType->uuid,
2201
                            'label' => 'LABEL_TYPE',
2202
                            'value' => $extendedStudentType->name
2203
                        ]);
2204
                    }
2205
                }
2206
 
2207
                array_push($data, $record);
2208
            }
2209
 
2210
            return new JsonModel([
2211
                'success' => true,
2212
                'data' => $data
2213
            ]);
2214
 
2215
        } else {
2216
            return new JsonModel([
2217
                'success' => false,
2218
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
2219
            ]);
2220
        }
2221
    }
2222
 
167 efrain 2223
    public function syncAction()
2224
    {
2225
        $request = $this->getRequest();
168 efrain 2226
        if($request->isPost()) {
167 efrain 2227
            $currentUserPlugin = $this->plugin('currentUserPlugin');
2228
            $currentUser = $currentUserPlugin->getUser();
2229
 
2230
 
2231
            $operation = $this->params()->fromRoute('operation');
2232
            if($operation == 'slide-view' || $operation == 'capsule-close' || $operation == 'topic-close') {
2233
                $accessGrantedIds = $this->getAccessGranted();
2234
 
2235
                $topic_uuid     = $this->params()->fromRoute('topic_uuid');
2236
                $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
2237
                $slide_uuid     = $this->params()->fromRoute('slide_uuid');
2238
 
2239
                $accessGrantedIds = $this->getAccessGranted();
2240
                if($operation == 'slide-view') {
2241
 
2242
                    if(empty($slide_uuid) || empty($capsule_uuid ) || empty($topic_uuid)) {
2243
                        return new JsonModel([
2244
                            'success' => false,
2245
                            'data' => 'ERROR_INVALID_PARAMETERS'
2246
                        ]);
2247
                    }
2248
 
2249
 
283 www 2250
                    $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
167 efrain 2251
                    $topic = $topicMapper->fetchOneByUuid($topic_uuid);
2252
 
2253
                    if(!$topic) {
2254
                        return new JsonModel([
2255
                            'success' => false,
2256
                            'data' => 'ERROR_TOPIC_NOT_FOUND'
2257
                        ]);
2258
                    }
2259
 
2260
 
2261
 
2262
                    if(!in_array($topic->id, $accessGrantedIds->topics)) {
2263
                        return new JsonModel([
2264
                            'success' => false,
2265
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
2266
                        ]);
2267
                    }
2268
 
283 www 2269
                    $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
167 efrain 2270
                    $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
2271
 
2272
                    if(!$capsule) {
2273
                        return new JsonModel([
2274
                            'success' => false,
2275
                            'data' => 'ERROR_CAPSULE_NOT_FOUND'
2276
                        ]);
2277
                    }
119 efrain 2278
 
167 efrain 2279
                    if(!in_array($capsule->id, $accessGrantedIds->capsules) || $capsule->topic_id != $topic->id) {
2280
                        return new JsonModel([
2281
                            'success' => false,
2282
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
2283
                        ]);
2284
                    }
2285
 
283 www 2286
                    $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
167 efrain 2287
                    $slide = $slideMapper->fetchOneByUuid($slide_uuid);
2288
                    if(!$slide) {
2289
                        return new JsonModel([
2290
                            'success' => false,
2291
                            'data' => 'ERROR_SLIDE_NOT_FOUND'
2292
                        ]);
2293
                    }
2294
 
2295
                    if($slide->capsule_id != $capsule->id && $slide->topic_id != $topic->id) {
2296
                        return new JsonModel([
2297
                            'success' => false,
2298
                            'data' => 'ERROR_SLIDE_NOT_FOUND'
2299
                        ]);
2300
 
2301
                    }
2302
 
2303
 
283 www 2304
                    $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
167 efrain 2305
 
283 www 2306
                    $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
167 efrain 2307
                    $added_on = $userProgressMapper->getDatebaseNow();
2308
 
2309
 
2310
                    $userProgressTopic = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic->id);
2311
                    if(!$userProgressTopic) {
2312
 
283 www 2313
                        $userProgressTopic = new MicrolearningUserProgress();
558 stevensc 2314
                        $userProgressTopic->company_id                  = $topic->company_id; // Usar company_id del tópico
2315
                        $userProgressTopic->topic_id                    = $topic->id;        // Usar id del tópico
176 efrain 2316
                        $userProgressTopic->user_id                     = $currentUser->id;
167 efrain 2317
                        $userProgressTopic->progress                    = 0;
2318
                        $userProgressTopic->returning                   = 0;
2319
                        $userProgressTopic->returning_after_completed   = 0;
2320
                        $userProgressTopic->completed                   = 0;
558 stevensc 2321
                        $userProgressTopic->total_slides                = $slideMapper->fetchTotalCountByCompanyIdAndTopicId($topic->company_id, $topic->id);
167 efrain 2322
                        $userProgressTopic->view_slides                 = 0;
283 www 2323
                        $userProgressTopic->type                        = MicrolearningUserProgress::TYPE_TOPIC;
167 efrain 2324
                        $userProgressTopic->added_on                    = $added_on;
2325
                        $userProgressTopic->updated_on                  = $added_on;
2326
 
2327
                        if($userProgressMapper->insert($userProgressTopic)) {
2328
 
283 www 2329
                            $userLog = new MicrolearningUserLog();
2330
                            $userLog->activity      = MicrolearningUserLog::ACTIVITY_START_TOPIC;
167 efrain 2331
                            $userLog->user_id       = $currentUser->id;
558 stevensc 2332
                            $userLog->company_id    = $topic->company_id;
2333
                            $userLog->topic_id      = $topic->id;
167 efrain 2334
                            $userLog->added_on      = $added_on;
2335
 
2336
                            if(!$userLogMapper->insert($userLog)) {
2337
                                return new JsonModel([
2338
                                    'success' => false,
2339
                                    'data' => $userProgressMapper->getError()
2340
                                ]);
2341
                            }
2342
                        } else {
2343
                            return new JsonModel([
2344
                                'success' => false,
2345
                                'data' => $userProgressMapper->getError()
2346
                            ]);
2347
                        }
2348
                    }
2349
 
2350
 
2351
                    $userProgressCapsule = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
2352
                    if(!$userProgressCapsule) {
2353
 
283 www 2354
                        $userProgressCapsule = new MicrolearningUserProgress();
558 stevensc 2355
                        $userProgressCapsule->company_id                  = $capsule->company_id; // Usar company_id de la cápsula
2356
                        $userProgressCapsule->topic_id                    = $topic->id;         // Usar el id del tópico obtenido previamente
2357
                        $userProgressCapsule->capsule_id                  = $capsule->id;       // Usar id de la cápsula
176 efrain 2358
                        $userProgressCapsule->user_id                     = $currentUser->id;
167 efrain 2359
                        $userProgressCapsule->progress                    = 0;
2360
                        $userProgressCapsule->returning                   = 0;
2361
                        $userProgressCapsule->returning_after_completed   = 0;
2362
                        $userProgressCapsule->completed                   = 0;
558 stevensc 2363
                        $userProgressCapsule->total_slides                = $slideMapper->fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($capsule->company_id, $topic->id, $capsule->id);
167 efrain 2364
                        $userProgressCapsule->view_slides                 = 0;
2365
                        $userProgressTopic->updated_on                    = $added_on;
2366
 
2367
                        if($userProgressMapper->insert($userProgressCapsule)) {
2368
 
283 www 2369
                            $userLog = new MicrolearningUserLog();
2370
                            $userLog->activity      = MicrolearningUserLog::ACTIVITY_START_CAPSULE;
167 efrain 2371
                            $userLog->user_id       = $currentUser->id;
558 stevensc 2372
                            $userLog->company_id    = $capsule->company_id;
2373
                            $userLog->topic_id      = $topic->id;
2374
                            $userLog->capsule_id    = $capsule->id;
167 efrain 2375
                            $userLog->added_on      = $added_on;
2376
 
2377
                            if(!$userLogMapper->insert($userLog)) {
2378
                                return new JsonModel([
2379
                                    'success' => false,
2380
                                    'data' => $userLogMapper->getError()
2381
                                ]);
2382
                            }
2383
                        } else {
2384
                            return new JsonModel([
2385
                                'success' => false,
2386
                                'data' => $userProgressMapper->getError()
2387
                            ]);
2388
                        }
2389
                    }
2390
 
2391
 
2392
                    $userProgressSlide = $userProgressMapper->fetchOneByUserIdAndSlideId($currentUser->id, $slide->id);
2393
                    if(!$userProgressSlide) {
283 www 2394
                        $userProgressSlide = new MicrolearningUserProgress();
167 efrain 2395
                        $userProgressSlide->company_id                  = $slide->company_id;
2396
                        $userProgressSlide->topic_id                    = $slide->topic_id;
2397
                        $userProgressSlide->capsule_id                  = $slide->capsule_id;
2398
                        $userProgressSlide->slide_id                    = $slide->id;
176 efrain 2399
                        $userProgressSlide->user_id                     = $currentUser->id;
167 efrain 2400
                        $userProgressSlide->progress                    = 0;
2401
                        $userProgressSlide->returning                   = 0;
2402
                        $userProgressSlide->returning_after_completed   = 0;
2403
                        $userProgressSlide->completed                   = 1;
2404
                        $userProgressSlide->total_slides                = 0;
2405
                        $userProgressSlide->view_slides                 = 0;
283 www 2406
                        $userProgressSlide->type                        = MicrolearningUserProgress::TYPE_SLIDE;
167 efrain 2407
                        $userProgressSlide->added_on                    = $added_on;
2408
                        $userProgressSlide->updated_on                  = $added_on;
2409
 
2410
                        if(!$userProgressMapper->insert($userProgressSlide)) {
2411
                            return new JsonModel([
2412
                                'success' => false,
2413
                                'data' => $userProgressMapper->getError()
2414
                            ]);
2415
                        }
2416
                    }
2417
 
283 www 2418
                    $userLog = new MicrolearningUserLog();
2419
                    $userLog->activity      = MicrolearningUserLog::ACTIVITY_VIEW_SLIDE;
167 efrain 2420
                    $userLog->user_id       = $currentUser->id;
2421
                    $userLog->company_id    = $slide->company_id;
2422
                    $userLog->topic_id      = $slide->topic_id;
2423
                    $userLog->capsule_id    = $slide->capsule_id;
2424
                    $userLog->slide_id      = $slide->id;
2425
                    $userLog->added_on      = $added_on;
2426
 
2427
                    if(!$userLogMapper->insert($userLog)) {
2428
                        return new JsonModel([
2429
                            'success' => false,
2430
                            'data' => $userLogMapper->getError()
2431
                        ]);
2432
                    }
2433
 
2434
                    $closeCapsule = false;
2435
 
2436
                    if($userProgressCapsule->completed) {
2437
 
2438
                        $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $added_on);
2439
                        $returning_on = $dt->format('Y-m-d');
2440
 
558 stevensc 2441
                        // Asegurarse que returning_on existe en el objeto antes de comparar
2442
                        $current_returning_on = isset($userProgressCapsule->returning_on) ? $userProgressCapsule->returning_on : null;
2443
 
2444
                        if(!$current_returning_on || $current_returning_on != $returning_on) {
167 efrain 2445
 
2446
                            $userProgressCapsule->returning_on = $returning_on;
558 stevensc 2447
                            // Asegurarse que returning_after_completed existe y es numérico antes de incrementar
2448
                            $userProgressCapsule->returning_after_completed = (isset($userProgressCapsule->returning_after_completed) ? intval($userProgressCapsule->returning_after_completed) : 0) + 1;
167 efrain 2449
 
2450
                            if(!$userProgressMapper->update($userProgressCapsule)) {
2451
                                return new JsonModel([
2452
                                    'success' => false,
2453
                                    'data' => $userProgressMapper->getError()
2454
                                ]);
2455
                            }
2456
                        }
2457
 
2458
                    }  else {
2459
 
558 stevensc 2460
                        // Usar $topic->id para obtener el total de slides de la cápsula en el contexto de este tópico
2461
                        $userProgressCapsule->total_slides = $slideMapper->fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($capsule->company_id, $topic->id, $capsule->id);
2462
                        $userProgressCapsule->view_slides = $userProgressMapper->fetchCountAllSlideCompletedByUserIdAndCapsuleId($currentUser->id, $capsule->id);
167 efrain 2463
 
2464
                        if($userProgressCapsule->total_slides) {
2465
 
2466
                            $userProgressCapsule->progress = ($userProgressCapsule->view_slides * 100) / $userProgressCapsule->total_slides;
2467
                            $userProgressCapsule->progress = $userProgressCapsule->progress > 100 ? 100 : $userProgressCapsule->progress;
2468
                        }
2469
 
2470
                        if(!$userProgressMapper->update($userProgressCapsule)) {
2471
                            return new JsonModel([
2472
                                'success' => false,
2473
                                'data' => $userProgressMapper->getError()
2474
                            ]);
2475
                        }
2476
 
2477
                        if($userProgressCapsule->progress >= 100) {
2478
                            $closeCapsule = true;
2479
                        }
171 efrain 2480
 
283 www 2481
 
167 efrain 2482
                    }
2483
 
2484
                    $closeTopic = false;
2485
                    if(!$userProgressTopic->completed) {
2486
 
558 stevensc 2487
                        // Usar $topic->id para obtener el total de slides del tópico
2488
                        $userProgressTopic->total_slides = $slideMapper->fetchTotalCountByCompanyIdAndTopicId($topic->company_id, $topic->id);
2489
                        $userProgressTopic->view_slides = $userProgressMapper->fetchCountAllSlideCompletedByUserIdAndTopicId($currentUser->id, $topic->id);
167 efrain 2490
 
2491
                        if($userProgressTopic->total_slides) {
2492
 
2493
                            $userProgressTopic->progress = ($userProgressTopic->view_slides * 100) / $userProgressTopic->total_slides;
2494
                            $userProgressTopic->progress = $userProgressTopic->progress > 100 ? 100 : $userProgressTopic->progress;
2495
                        }
2496
                        if(!$userProgressMapper->update($userProgressTopic)) {
2497
                            return new JsonModel([
2498
                                'success' => false,
2499
                                'data' => $userProgressMapper->getError()
2500
                            ]);
2501
                        }
2502
 
2503
                        if($userProgressTopic->progress >= 100) {
2504
                            $closeTopic = true;
2505
                        }
2506
                    }
2507
 
2508
 
2509
                    $data = [
2510
                        'message' => 'LABEL_THE_USER_PROGRESS_FOR_THIS_SLIDE_HAS_BEEN_COMPLETED',
2511
 
2512
                    ];
2513
 
2514
                    if($closeCapsule) {
173 efrain 2515
                        $data['link_close_capsule'] = $this->url()->fromRoute('microlearning/sync', ['operation' => 'capsule-close', 'topic_uuid' => $topic->uuid, 'capsule_uuid' => $capsule->uuid], ['force_canonical' => true]);
167 efrain 2516
                    }
2517
 
2518
 
2519
                    if($closeTopic) {
173 efrain 2520
                        $data['link_close_topic'] = $this->url()->fromRoute('microlearning/sync', ['operation' => 'topic-close', 'topic_uuid' => $topic->uuid], ['force_canonical' => true]);
167 efrain 2521
                    }
2522
 
2523
 
2524
                    return new JsonModel([
2525
                        'success' => true,
2526
                        'data' => $data
2527
                    ]);
2528
 
2529
 
2530
 
2531
 
2532
                } else if($operation == 'capsule-close') {
2533
 
2534
                    if(empty($capsule_uuid ) || empty($topic_uuid)) {
2535
                        return new JsonModel([
2536
                            'success' => false,
2537
                            'data' => 'ERROR_INVALID_PARAMETERS'
2538
                        ]);
2539
                    }
2540
 
2541
 
283 www 2542
                    $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
167 efrain 2543
                    $topic = $topicMapper->fetchOneByUuid($topic_uuid);
2544
 
2545
                    if(!$topic) {
2546
                        return new JsonModel([
2547
                            'success' => false,
2548
                            'data' => 'ERROR_TOPIC_NOT_FOUND'
2549
                        ]);
2550
                    }
2551
 
2552
 
2553
 
2554
                    if(!in_array($topic->id, $accessGrantedIds->topics)) {
2555
                        return new JsonModel([
2556
                            'success' => false,
2557
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
2558
                        ]);
2559
                    }
2560
 
283 www 2561
                    $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
167 efrain 2562
                    $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
2563
 
2564
                    if(!$capsule) {
2565
                        return new JsonModel([
2566
                            'success' => false,
2567
                            'data' => 'ERROR_CAPSULE_NOT_FOUND'
2568
                        ]);
2569
                    }
2570
 
2571
                    if(!in_array($capsule->id, $accessGrantedIds->capsules) || $capsule->topic_id != $topic->id) {
2572
                        return new JsonModel([
2573
                            'success' => false,
2574
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
2575
                        ]);
2576
                    }
2577
 
2578
 
283 www 2579
                    $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
167 efrain 2580
 
283 www 2581
                    $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
167 efrain 2582
                    $updated_on = $userProgressMapper->getDatebaseNow();
2583
 
2584
                    $userProgressCapsule = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
2585
                    if(!$userProgressCapsule) {
2586
                        return new JsonModel([
2587
                            'success' => false,
2588
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_CAPSULE_NOT_FOUND'
2589
                        ]);
2590
                    }
2591
 
2592
                    if($userProgressCapsule->completed) {
2593
                        return new JsonModel([
2594
                            'success' => false,
2595
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_CAPSULE_ALREADY_CLOSED'
2596
                        ]);
2597
                    }
2598
 
2599
 
2600
                    $userProgressCapsule->completed = 1;
2601
                    $userProgressCapsule->updated_on = $updated_on;
2602
 
2603
                    if($userProgressMapper->update($userProgressCapsule)) {
225 efrain 2604
 
2605
 
283 www 2606
                        $userLog = new MicrolearningUserLog();
2607
                        $userLog->activity      = MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE;
227 efrain 2608
                        $userLog->user_id       = $currentUser->id;
2609
                        $userLog->company_id    = $capsule->company_id;
2610
                        $userLog->topic_id      = $capsule->topic_id;
2611
                        $userLog->capsule_id    = $capsule->id;
2612
                        $userLog->added_on      = $updated_on;
225 efrain 2613
 
2614
                        if(!$userLogMapper->insert($userLog)) {
2615
                            return new JsonModel([
2616
                                'success' => false,
2617
                                'data' => $userProgressMapper->getError()
2618
                            ]);
2619
                        }
2620
 
2621
 
167 efrain 2622
                        return new JsonModel([
2623
                            'success' => true,
2624
                            'data' => 'LABEL_THE_USER_PROGRESS_FOR_THIS_CAPSULE_HAS_BEEN_COMPLETED'
2625
                        ]);
2626
                    } else {
2627
                        return new JsonModel([
2628
                            'success' => false,
2629
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_CAPSULE_COULD_NOT_BE_COMPLETED'
2630
                        ]);
2631
                    }
2632
 
2633
 
2634
 
2635
 
2636
 
2637
 
2638
 
2639
                } else if($operation == 'topic-close') {
174 efrain 2640
                    if(empty($topic_uuid)) {
167 efrain 2641
                        return new JsonModel([
2642
                            'success' => false,
2643
                            'data' => 'ERROR_INVALID_PARAMETERS'
2644
                        ]);
2645
                    }
2646
 
2647
 
283 www 2648
                    $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
167 efrain 2649
                    $topic = $topicMapper->fetchOneByUuid($topic_uuid);
2650
 
2651
                    if(!$topic) {
2652
                        return new JsonModel([
2653
                            'success' => false,
2654
                            'data' => 'ERROR_TOPIC_NOT_FOUND'
2655
                        ]);
2656
                    }
2657
 
2658
 
2659
 
2660
                    if(!in_array($topic->id, $accessGrantedIds->topics)) {
2661
                        return new JsonModel([
2662
                            'success' => false,
2663
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
2664
                        ]);
2665
                    }
2666
 
2667
 
2668
 
2669
 
283 www 2670
                    $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
167 efrain 2671
 
283 www 2672
                    $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
167 efrain 2673
                    $updated_on = $userProgressMapper->getDatebaseNow();
2674
 
2675
                    $userProgressTopic = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic->id);
2676
                    if(!$userProgressTopic) {
2677
                        return new JsonModel([
2678
                            'success' => false,
2679
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_TOPIC_NOT_FOUND'
2680
                        ]);
2681
                    }
2682
 
2683
                    if($userProgressTopic->completed) {
2684
                        return new JsonModel([
2685
                            'success' => false,
2686
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_TOPIC_ALREADY_CLOSED'
2687
                        ]);
2688
                    }
2689
 
2690
 
2691
                    $userProgressTopic->completed = 1;
2692
                    $userProgressTopic->updated_on = $updated_on;
2693
 
2694
                    if($userProgressMapper->update($userProgressTopic)) {
226 efrain 2695
 
283 www 2696
                        $userLog = new MicrolearningUserLog();
2697
                        $userLog->activity      = MicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC;
227 efrain 2698
                        $userLog->user_id       = $currentUser->id;
2699
                        $userLog->company_id    = $topic->company_id;
2700
                        $userLog->topic_id      = $topic->id;
2701
                        $userLog->added_on      = $updated_on;
226 efrain 2702
 
2703
                        if(!$userLogMapper->insert($userLog)) {
2704
                            return new JsonModel([
2705
                                'success' => false,
2706
                                'data' => $userProgressMapper->getError()
2707
                            ]);
2708
                        }
2709
 
167 efrain 2710
                        return new JsonModel([
2711
                            'success' => true,
2712
                            'data' => 'LABEL_THE_USER_PROGRESS_FOR_THIS_TOPIC_HAS_BEEN_COMPLETED'
2713
                        ]);
2714
                    } else {
2715
                        return new JsonModel([
2716
                            'success' => false,
2717
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_TOPIC_COULD_NOT_BE_COMPLETED'
2718
                        ]);
2719
                    }
2720
                }
2721
 
2722
 
2723
 
2724
 
2725
            } else {
2726
 
2727
                return new JsonModel([
2728
                    'success' => false,
2729
                    'data' => 'ERROR_OPERATION_UNKNOWN'
2730
                ]);
2731
 
2732
 
2733
            }
2734
 
2735
        } else {
2736
            return new JsonModel([
2737
                'success' => false,
2738
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
2739
            ]);
2740
        }
2741
    }
2742
 
2743
 
302 www 2744
 
2745
 
2746
 
119 efrain 2747
    /**
2748
     *
2749
     * @return \LeadersLinked\Controller\MicrolearningUserAccessGrantedIds
2750
     */
2751
    private function getAccessGranted()
2752
    {
2753
        $currentUserPlugin = $this->plugin('currentUserPlugin');
2754
        $currentUser = $currentUserPlugin->getUser();
2755
 
283 www 2756
        $capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
119 efrain 2757
        $now = $capsuleUserMapper->getDatebaseNow();
2758
 
2759
        $records = $capsuleUserMapper->fetchAllActiveByUserId($currentUser->id);
2760
 
2761
        $accessGrantedIds = new MicrolearningUserAccessGrantedIds();
2762
 
2763
 
2764
        foreach($records as $record)
2765
        {
283 www 2766
            if($record->access != MicrolearningCapsuleUser::ACCESS_UNLIMITED && $record->access != MicrolearningCapsuleUser::ACCESS_PAY_PERIOD) {
119 efrain 2767
                continue;
2768
            }
283 www 2769
            if($record->access == MicrolearningCapsuleUser::ACCESS_PAY_PERIOD) {
119 efrain 2770
                if($now < $record->paid_from || $now > $record->paid_to) {
2771
                    continue;
2772
                }
2773
            }
2774
 
2775
 
2776
            if(!in_array($record->company_id, $accessGrantedIds->companies )) {
2777
                array_push($accessGrantedIds->companies, $record->company_id);
2778
            }
2779
 
2780
            if(!in_array($record->topic_id, $accessGrantedIds->topics )) {
2781
                array_push( $accessGrantedIds->topics, $record->topic_id);
2782
            }
2783
 
2784
            if(!in_array($record->capsule_id, $accessGrantedIds->capsules)) {
2785
                array_push( $accessGrantedIds->capsules, $record->capsule_id);
2786
            }
2787
        }
2788
 
2789
        return $accessGrantedIds;
2790
    }
2791
 
2792
 
2793
}