Proyectos de Subversion LeadersLinked - Services

Rev

Rev 586 | Rev 588 | 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_get'              => $this->url()->fromRoute('microlearning/get-slide', ['id' => $slide->uuid], ['force_canonical' => true]),
1464
                'link_sync'             => $this->url()->fromRoute('microlearning/sync', ['operation' => 'slide-view', 'topic_uuid' => $capsule->topic_id, 'capsule_uuid' => $capsule->uuid, 'slide_uuid' => $slide->uuid], ['force_canonical' => true]),
1465
                'link_take_a_test'      => $link_take_a_test,
1466
                'added_on'              => $slide->added_on,
1467
                'updated_on'            => $slide->updated_on,
1468
            ]);
1469
        }
1470
 
1471
        // Sort slides by order, then by added_on, then by name
1472
        usort($data, function($a, $b) {
1473
            $result = $a['order'] <=> $b['order'];
1474
            if (0 == $result) {
1475
                $result = strcasecmp($a['added_on'], $b['added_on']);
1476
                if (0 == $result) {
1477
                    $result = strcasecmp($a['name'], $b['name']);
1478
                }
1479
            }
1480
 
1481
            if ($result < 0) {
1482
                return 1;
1483
            } else if ($result > 0) {
1484
                return -1;
1485
            } else {
1486
                return 0;
1487
            }
1488
        });
1489
 
1490
        return $data;
1491
    }
1492
 
119 efrain 1493
    public function capsulesAction()
1494
    {
1495
        $request = $this->getRequest();
1496
        if($request->isGet()) {
580 stevensc 1497
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1498
            $currentUser = $currentUserPlugin->getUser();
584 stevensc 1499
 
1500
            $topic_id_param = $this->params()->fromRoute('topic_id');
283 www 1501
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
580 stevensc 1502
            $topic = $topicMapper->fetchOneByUuid($topic_id_param);
584 stevensc 1503
 
119 efrain 1504
            if(!$topic) {
1505
                return new JsonModel([
1506
                    'success' => false,
1507
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1508
                ]);
1509
            }
584 stevensc 1510
 
580 stevensc 1511
            $accessGrantedIds = $this->getAccessGranted();
584 stevensc 1512
 
119 efrain 1513
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1514
                return new JsonModel([
1515
                    'success' => false,
1516
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1517
                ]);
1518
            }
584 stevensc 1519
 
333 www 1520
            $storage = Storage::getInstance($this->config, $this->adapter);
584 stevensc 1521
            // Use the _getCapsulesByTopic method to fetch capsules
1522
            $data = $this->_getCapsulesByTopic($topic, $storage);
580 stevensc 1523
 
584 stevensc 1524
            // The _getCapsulesByTopic method already returns capsules with the required data structure.
1525
            // We just need to sort them.
1526
 
580 stevensc 1527
            usort($data, function($a, $b) {
584 stevensc 1528
 
580 stevensc 1529
                $result =  $a['order'] <=> $b['order'];
1530
                if(0 == $result) {
1531
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1532
                    if(0 == $result) {
1533
                        $result = strcasecmp($a['name'], $b['name']);
1534
                    }
1535
                }
584 stevensc 1536
 
580 stevensc 1537
                if($result < 0) {
1538
                    return 1;
1539
                } else if($result > 0) {
1540
                    return -1;
1541
                } else  {
1542
                    return  0;
1543
                }
1544
            });
584 stevensc 1545
 
1546
            return new JsonModel([
1547
                'success' => true,
1548
                'data' => $data
1549
            ]);
1550
 
119 efrain 1551
        } else {
1552
            return new JsonModel([
1553
                'success' => false,
1554
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1555
            ]);
1556
        }
1557
    }
1558
 
235 efrain 1559
    public function takeTestAction()
1560
    {
1561
        $request = $this->getRequest();
1562
        if($request->isPost()) {
1563
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1564
            $currentUser = $currentUserPlugin->getUser();
1565
 
1566
            $slide_id = $this->params()->fromRoute('slide_id');
283 www 1567
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
235 efrain 1568
            $slide = $slideMapper->fetchOneByUuid($slide_id);
1569
 
1570
            if(!$slide) {
1571
                return new JsonModel([
1572
                    'success' => false,
1573
                    'data' => 'ERROR_SLIDE_NOT_FOUND'
1574
                ]);
1575
            }
1576
 
283 www 1577
            if($slide->type != MicrolearningSlide::TYPE_QUIZ) {
235 efrain 1578
                return new JsonModel([
1579
                    'success' => false,
1580
                    'data' => 'ERROR_SLIDE_IS_NOT_QUIZ'
1581
                ]);
1582
            }
1583
 
1584
 
1585
 
283 www 1586
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
235 efrain 1587
            $capsule = $capsuleMapper->fetchOne($slide->capsule_id);
1588
 
1589
            if(!$capsule) {
1590
                return new JsonModel([
1591
                    'success' => false,
1592
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
1593
                ]);
1594
            }
1595
 
1596
            $accessGrantedIds = $this->getAccessGranted();
1597
 
1598
            if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1599
                return new JsonModel([
1600
                    'success' => false,
1601
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1602
                ]);
1603
            }
1604
 
283 www 1605
            $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
235 efrain 1606
            $userLog = $userLogMapper->fetchOneTakeATestByUserIdAndSlideId($currentUser->id, $slide->id);
1607
 
1608
            if($userLog) {
283 www 1609
                $activity = MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST;
235 efrain 1610
            } else {
283 www 1611
                $activity = MicrolearningUserLog::ACTIVITY_TAKE_A_TEST;
235 efrain 1612
            }
1613
 
1614
            $added_on = $userLogMapper->getDatebaseNow();
1615
 
283 www 1616
            $userLog = new MicrolearningUserLog();
235 efrain 1617
            $userLog->activity      = $activity;
1618
            $userLog->user_id       = $currentUser->id;
1619
            $userLog->company_id    = $slide->company_id;
1620
            $userLog->topic_id      = $slide->topic_id;
1621
            $userLog->capsule_id    = $slide->capsule_id;
1622
            $userLog->slide_id      = $slide->id;
1623
            $userLog->added_on      = $added_on;
1624
 
1625
            if($userLogMapper->insert($userLog)) {
1626
                return new JsonModel([
1627
                    'success' => true,
1628
                ]);
1629
            } else {
1630
                return new JsonModel([
1631
                    'success' => false,
1632
                    'data' => $userLogMapper->getError()
1633
                ]);
1634
            }
1635
 
1636
 
1637
 
1638
 
1639
 
1640
 
1641
 
1642
        } else {
1643
            return new JsonModel([
1644
                'success' => false,
1645
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1646
            ]);
1647
        }
1648
    }
1649
 
161 efrain 1650
    public function getCapsuleAction()
1651
    {
1652
        $request = $this->getRequest();
587 stevensc 1653
 
161 efrain 1654
        if($request->isGet()) {
1655
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1656
            $currentUser = $currentUserPlugin->getUser();
1657
 
558 stevensc 1658
            $capsule_uuid = $this->params()->fromRoute('id');
283 www 1659
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
558 stevensc 1660
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
161 efrain 1661
 
1662
            if(!$capsule) {
1663
                return new JsonModel([
1664
                    'success' => false,
1665
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
1666
                ]);
1667
            }
1668
 
1669
            $accessGrantedIds = $this->getAccessGranted();
1670
 
1671
            if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1672
                return new JsonModel([
1673
                    'success' => false,
1674
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1675
                ]);
1676
            }
1677
 
283 www 1678
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1679
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
161 efrain 1680
 
1681
            $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
587 stevensc 1682
            $progress = $userProgress->progress ?? 0;
1683
            $completed = $userProgress->completed ?? 0;
244 efrain 1684
 
586 stevensc 1685
            $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->id);
244 efrain 1686
 
587 stevensc 1687
            // Get comments data
1688
            $userMapper = UserMapper::getInstance($this->adapter);
333 www 1689
            $storage = Storage::getInstance($this->config, $this->adapter);
587 stevensc 1690
            $serviceDatetimeFormat = $this->config['leaderslinked.services.datetime'];
1691
 
1692
            $comments = [];
1693
            $records = $capsuleCommentMapper->fetchAllByCapsuleId($capsule->id);
1694
            foreach($records as $record) {
1695
                $user = $userMapper->fetchOne($record->user_id);
1696
                if(!$user) {
1697
                    continue;
1698
                }
1699
 
1700
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
1701
 
1702
                array_push($comments, [
1703
                    'date' => $dt->format($serviceDatetimeFormat),
1704
                    'image' => $storage->getUserImage($user),
1705
                    'fullname' => trim(trim($user->first_name) . ' ' . trim($user->last_name)),
1706
                    'rating' => strval($record->rating),
1707
                    'comment' => $record->comment,
1708
                    '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]) : '',
1709
                ]);
1710
            }
1711
 
1712
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 1713
            $path = $storage->getPathMicrolearningCapsule();
244 efrain 1714
 
586 stevensc 1715
            $slides = $this->_getSlidesByCapsule($capsule, $storage);
1716
 
161 efrain 1717
            $data = [
586 stevensc 1718
                'uuid'              => $capsule->uuid,
1719
                'name'              => $capsule->name ? $capsule->name : '',
1720
                'description'       => $capsule->description ? $capsule->description : '',
1721
                'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image) : '',
1722
                'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
1723
                'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
1724
                'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
587 stevensc 1725
                'comments'          => $comments,
586 stevensc 1726
                'progress'          => $progress,
1727
                'completed'         => $completed,
1728
                'total_slides'      => count($slides),
1729
                'slides'            => $slides,
1730
                'order'             => $capsule->order,
1731
                'added_on'          => $capsule->added_on,
1732
                'updated_on'        => $capsule->updated_on,
161 efrain 1733
            ];
586 stevensc 1734
 
161 efrain 1735
            return new JsonModel([
1736
                'success' => true,
1737
                'data' => $data
1738
            ]);
1739
        }
586 stevensc 1740
 
1741
        return new JsonModel([
1742
            'success' => false,
1743
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1744
        ]);
161 efrain 1745
    }
1746
 
119 efrain 1747
    public function slidesAction()
1748
    {
1749
        $request = $this->getRequest();
1750
        if($request->isGet()) {
1751
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1752
            $currentUser = $currentUserPlugin->getUser();
1753
 
1754
 
1755
 
1756
            $topic_id = $this->params()->fromRoute('topic_id');
283 www 1757
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 1758
            $topic = $topicMapper->fetchOneByUuid($topic_id);
1759
 
1760
            if(!$topic) {
1761
                return new JsonModel([
1762
                    'success' => false,
1763
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1764
                ]);
1765
            }
1766
 
1767
            $accessGrantedIds = $this->getAccessGranted();
1768
 
1769
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1770
                return new JsonModel([
1771
                    'success' => false,
1772
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1773
                ]);
1774
            }
1775
 
1776
            $capsule_id = $this->params()->fromRoute('capsule_id');
283 www 1777
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
119 efrain 1778
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
1779
 
1780
            if(!$capsule) {
1781
                return new JsonModel([
1782
                    'success' => false,
1783
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
1784
                ]);
1785
            }
1786
 
1787
 
1788
 
1789
            if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1790
                return new JsonModel([
1791
                    'success' => false,
1792
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1793
                ]);
1794
            }
1795
 
1796
 
1797
 
1798
            $data = [];
1799
 
283 www 1800
            $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
119 efrain 1801
 
283 www 1802
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
1803
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
119 efrain 1804
 
333 www 1805
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 1806
            $path = $storage->getPathMicrolearningSlide();
119 efrain 1807
 
1808
            $slides = $slideMapper->fetchAllByCompanyIdAndTopicIdAndCapsuleId($capsule->company_id, $capsule->topic_id, $capsule->id);
1809
            foreach($slides as $slide)
1810
            {
1811
 
1812
 
1813
                $userProgress = $userProgressMapper->fetchOneByUserIdAndSlideId($currentUser->id, $slide->id);
1814
                if($userProgress) {
224 efrain 1815
                    $completed =  $userProgress->completed ;
119 efrain 1816
                } else {
1817
                    $completed = 0;
1818
                }
1819
 
235 efrain 1820
 
230 efrain 1821
 
235 efrain 1822
                $link_take_a_test = '';
230 efrain 1823
                if($slide->quiz_id) {
235 efrain 1824
 
283 www 1825
                    $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
230 efrain 1826
                    $quiz = $quizMapper->fetchOne($slide->quiz_id);
1827
                    if($quiz) {
1828
                        $quiz_uuid = $quiz->uuid;
235 efrain 1829
                        $quiz_data = $this->getQuiz($slide->quiz_id);
1830
                        $link_take_a_test = $this->url()->fromRoute('microlearning/take-a-test', ['slide_id' => $slide->uuid], ['force_canonical' => true]);
230 efrain 1831
                    }
235 efrain 1832
 
1833
                }
119 efrain 1834
 
1835
 
1836
                array_push($data, [
235 efrain 1837
                    'quiz'                  => $quiz_uuid,
1838
                    'quiz_data'             => $quiz_data,
1839
                    'uuid'                  => $slide->uuid,
1840
                    'name'                  => $slide->name ? $slide->name : '',
1841
                    'description'           => $slide->description ? $slide->description : '',
1842
                    'type'                  => $slide->type,
283 www 1843
                    'background'            => $slide->background ? $storage->getGenericImage($path, $slide->uuid, $slide->background) : '',
1844
                    'file'                  => $slide->file ? $storage->getGenericFile($path, $slide->uuid, $slide->file) : '',
235 efrain 1845
                    'order'                 => $slide->order,
1846
                    'completed'             => $completed,
1847
                    'link_get'              => $this->url()->fromRoute('microlearning/get-slide', ['id' => $slide->uuid], ['force_canonical' => true]),
1848
                    '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 1849
                    'link_take_a_test'      => $link_take_a_test,
235 efrain 1850
                    'added_on'              => $slide->added_on,
1851
                    'updated_on'            => $slide->updated_on,
119 efrain 1852
                ]);
1853
 
1854
 
1855
 
1856
            }
1857
 
1858
            usort($data, function($a, $b) {
1859
 
1860
                $result =  $a['order'] <=> $b['order'];
1861
                if(0 == $result) {
1862
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1863
                    if(0 == $result) {
1864
                        $result = strcasecmp($a['name'], $b['name']);
1865
                    }
1866
                }
1867
 
1868
                if($result < 0) {
1869
                    return 1;
1870
                } else if($result > 0) {
1871
                    return -1;
1872
                } else  {
1873
                    return  0;
1874
                }
1875
            });
1876
 
1877
 
1878
 
1879
                return new JsonModel([
1880
                    'success' => true,
1881
                    'data' => $data
1882
                ]);
1883
 
1884
        } else {
1885
            return new JsonModel([
1886
                'success' => false,
1887
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1888
            ]);
1889
        }
1890
    }
1891
 
229 efrain 1892
    private function getQuiz($id)
1893
    {
1894
        $serviceDatetimeFormat = $this->config['leaderslinked.services.datetime'];
1895
 
1896
        $data = [];
1897
 
283 www 1898
        $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
229 efrain 1899
        $quiz = $quizMapper->fetchOne($id);
1900
 
1901
        if(!$quiz) {
1902
            return [];
1903
        }
1904
 
1905
        $companyMapper = CompanyMapper::getInstance($this->adapter);
1906
        $company = $companyMapper->fetchOne($quiz->company_id);
1907
 
283 www 1908
        $questionMapper = MicrolearningQuestionMapper::getInstance($this->adapter);
1909
        $answerMapper = MicrolearningAnswerMapper::getInstance($this->adapter);
229 efrain 1910
 
1911
        $record_questions = [];
1912
        $questions = $questionMapper->fetchAllByQuizId($quiz->id);
1913
        foreach($questions as $question)
1914
        {
1915
            $record_answers = [];
1916
 
1917
            $answers = $answerMapper->fetchAllByQuizIdAndQuestionId($question->quiz_id, $question->id);
1918
            foreach($answers as $answer)
1919
            {
1920
                $dtAddedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $answer->added_on);
1921
                $dtUpdatedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $answer->updated_on);
1922
 
1923
                array_push($record_answers, [
1924
                    'uuid' => $answer->uuid,
1925
                    'text' => trim($answer->text),
1926
                    'correct' => $answer->correct ? $answer->correct  : 0 ,
1927
                    'points' => strval(intval($answer->points, 10)),
1928
                    'added_on'  => $dtAddedOn->format($serviceDatetimeFormat),
1929
                    'updated_on'    => $dtUpdatedOn->format($serviceDatetimeFormat),
1930
                ]);
1931
            }
1932
 
1933
            $dtAddedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $question->added_on);
1934
            $dtUpdatedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $question->updated_on);
1935
 
1936
            array_push($record_questions, [
1937
                'uuid'          => $question->uuid,
1938
                'text'          => trim($question->text),
1939
                'type'          => $question->type,
1940
                'maxlength'     => strval($question->maxlength),
1941
                'points'        => strval($question->points),
1942
                'answers'       => $record_answers,
1943
                'added_on'      => $dtAddedOn->format($serviceDatetimeFormat),
1944
                'updated_on'    => $dtUpdatedOn->format($serviceDatetimeFormat),
1945
            ]);
1946
        }
1947
 
1948
        $dtAddedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $quiz->added_on);
1949
        $dtUpdatedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $quiz->updated_on);
1950
 
1951
        array_push($data, [
1952
            'uuid' => $quiz->uuid,
1953
            'name' => $quiz->name,
1954
            'text' => trim($quiz->text ? $quiz->text : ''),
1955
            'failed' => trim($quiz->failed ? $quiz->failed : ''),
1956
            'points' => strval($quiz->points),
1957
            'minimum_points_required' => strval($quiz->minimum_points_required),
1958
            'max_time' => $quiz->max_time ? $quiz->max_time : 5,
1959
            'company_uuid' => $company->uuid,
1960
            'company_name' => $company->name,
1961
            'company_image' => $this->url()->fromRoute('services/storage',['type' => 'company', 'code' => $company->uuid, 'filename' => $company->image], ['force_canonical' => true]),
1962
            'questions'     => $record_questions,
1963
            'added_on'      => $dtAddedOn->format($serviceDatetimeFormat),
1964
            'updated_on'    => $dtUpdatedOn->format($serviceDatetimeFormat),
1965
        ]);
1966
 
1967
        return $data;
1968
 
1969
    }
161 efrain 1970
 
229 efrain 1971
 
161 efrain 1972
    public function getSlideAction()
1973
    {
1974
        $request = $this->getRequest();
1975
        if($request->isGet()) {
1976
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1977
            $currentUser = $currentUserPlugin->getUser();
1978
 
1979
 
1980
 
1981
            $id = $this->params()->fromRoute('id');
283 www 1982
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
161 efrain 1983
            $slide = $slideMapper->fetchOneByUuid($id);
1984
 
1985
            if(!$slide) {
1986
                return new JsonModel([
1987
                    'success' => false,
1988
                    'data' => 'ERROR_SLIDE_NOT_FOUND'
1989
                ]);
1990
            }
1991
 
1992
            $accessGrantedIds = $this->getAccessGranted();
1993
 
1994
 
1995
 
1996
            if(!in_array($slide->capsule_id, $accessGrantedIds->capsules)) {
1997
                return new JsonModel([
1998
                    'success' => false,
1999
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
2000
                ]);
2001
            }
2002
 
2003
 
283 www 2004
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
161 efrain 2005
            $topic = $topicMapper->fetchOne($slide->topic_id);
2006
 
283 www 2007
            $capsuleMapper =  MicrolearningCapsuleMapper::getInstance($this->adapter);
161 efrain 2008
            $capsule = $capsuleMapper->fetchOne($slide->capsule_id);
2009
 
2010
 
2011
 
2012
 
2013
 
283 www 2014
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
2015
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
161 efrain 2016
 
2017
 
2018
            $userProgress = $userProgressMapper->fetchOneByUserIdAndSlideId($currentUser->id, $slide->id);
2019
            if($userProgress) {
2020
                $completed = $userProgress->completed;
2021
            } else {
2022
                $completed = 0;
2023
            }
235 efrain 2024
 
2025
            $quiz_uuid = '';
2026
            $quiz_data = [];
2027
            $link_take_a_test = '';
2028
            if($slide->quiz_id) {
2029
 
283 www 2030
                $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
235 efrain 2031
                $quiz = $quizMapper->fetchOne($slide->quiz_id);
2032
                if($quiz) {
2033
                    $quiz_uuid = $quiz->uuid;
2034
                    $quiz_data = $this->getQuiz($slide->quiz_id);
2035
                    $link_take_a_test = $this->url()->fromRoute('microlearning/take-a-test', ['slide_id' => $slide->uuid], ['force_canonical' => true]);
2036
                }
161 efrain 2037
 
235 efrain 2038
            }
161 efrain 2039
 
333 www 2040
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 2041
            $path = $storage->getPathMicrolearningSlide();
161 efrain 2042
 
2043
            $data =[
235 efrain 2044
                'quiz'              => $quiz_uuid,
2045
                'quiz_data'         => $quiz_data,
2046
                'uuid'              => $slide->uuid,
2047
                'name'              => $slide->name ? $slide->name : '',
2048
                'description'       => $slide->description ? $slide->description : '',
2049
                'type'              => $slide->type,
283 www 2050
                'background'        => $slide->background ? $storage->getGenericImage($path, $slide->uuid, $slide->background) : '',
2051
                'file'              => $slide->file ? $storage->getGenericFile($path, $slide->uuid, $slide->file) : '',
235 efrain 2052
                'order'             => $slide->order,
2053
                'completed'         => $completed,
2054
                '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]),
2055
                'link_take_a_test'  => $link_take_a_test,
2056
                'added_on'          => $slide->added_on,
2057
                'updated_on'        => $slide->updated_on,
161 efrain 2058
            ];
2059
 
2060
 
2061
 
2062
 
2063
            return new JsonModel([
2064
                'success' => true,
2065
                'data' => $data
2066
            ]);
2067
 
2068
        } else {
2069
            return new JsonModel([
2070
                'success' => false,
2071
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
2072
            ]);
2073
        }
2074
    }
2075
 
119 efrain 2076
    public function profileAction()
2077
    {
2078
        $request = $this->getRequest();
2079
        if($request->isGet()) {
2080
            $currentUserPlugin = $this->plugin('currentUserPlugin');
2081
            $currentUser = $currentUserPlugin->getUser();
2082
 
2083
 
2084
            $accessGrantedIds = $this->getAccessGranted();
2085
 
2086
            $companyMapper = CompanyMapper::getInstance($this->adapter);
283 www 2087
            $companyExtendUserMapper = MicrolearningExtendUserMapper::getInstance($this->adapter);
2088
            $companyExtendUserCompanyMapper = MicrolearningExtendUserCompanyMapper::getInstance($this->adapter);
2089
            $companyExtendUserFunctionMapper = MicrolearningExtendUserFunctionMapper::getInstance($this->adapter);
2090
            $companyExtendUserGroupMapper = MicrolearningExtendUserGroupMapper::getInstance($this->adapter);
2091
            $companyExtendUserInstitutionMapper = MicrolearningExtendUserInstitutionMapper::getInstance($this->adapter);
2092
            $companyExtendUserPartnerMapper = MicrolearningExtendUserPartnerMapper::getInstance($this->adapter);
2093
            $companyExtendUserProgramMapper = MicrolearningExtendUserProgramMapper::getInstance($this->adapter);
2094
            $companyExtendUserStudentTypeMapper = MicrolearningExtendUserStudentTypeMapper::getInstance($this->adapter);
2095
            $companyExtendUserSectorMapper = MicrolearningExtendUserSectorMapper::getInstance($this->adapter);
119 efrain 2096
 
333 www 2097
            $storage = Storage::getInstance($this->config, $this->adapter);
283 www 2098
 
119 efrain 2099
 
2100
            $data = [];
2101
            foreach($accessGrantedIds->companies as $company_id)
2102
            {
2103
                $company = $companyMapper->fetchOne($company_id);
2104
                if(!$company) {
2105
                    continue;
2106
                }
2107
 
2108
                $record = [
2109
                    'name' => $company->name,
283 www 2110
                    'image' => $storage->getCompanyImage($company),
119 efrain 2111
                    'details' => [],
2112
                ];
2113
 
2114
                $companyExtendUser = $companyExtendUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
2115
                if(!$companyExtendUser) {
2116
                    continue;
2117
                }
2118
 
2119
                if($companyExtendUser->extend_company_id) {
2120
 
2121
                    $extendedCompany = $companyExtendUserCompanyMapper->fetchOne($companyExtendUser->company_id);
2122
                    if($extendedCompany) {
2123
                        array_push($record['details'],[
2124
                            'uuid' => $extendedCompany->uuid,
2125
                            'label' => 'LABEL_COMPANY',
2126
                            'value' => $extendedCompany->name
2127
                        ]);
2128
                    }
2129
                }
2130
 
2131
                if($companyExtendUser->extend_function_id) {
2132
                    $extendedFunction = $companyExtendUserFunctionMapper->fetchOne($companyExtendUser->extend_function_id);
2133
                    if($extendedFunction) {
2134
                        array_push($record['details'],[
2135
                            'uuid' => $extendedFunction->uuid,
2136
                            'label' => 'LABEL_FUNCTION',
2137
                            'value' => $extendedFunction->name
2138
                        ]);
2139
                    }
2140
                }
2141
 
2142
                if($companyExtendUser->extend_group_id) {
2143
                    $extendedGroup = $companyExtendUserGroupMapper->fetchOne($companyExtendUser->extend_group_id);
2144
                    if($extendedGroup) {
2145
                        array_push($record['details'],[
2146
                            'uuid' => $extendedGroup->uuid,
2147
                            'label' => 'LABEL_GROUP',
2148
                            'value' => $extendedGroup->name
2149
                        ]);
2150
                    }
2151
                }
2152
 
2153
                if($companyExtendUser->extend_institution_id) {
2154
                    $extendedInstitution= $companyExtendUserInstitutionMapper->fetchOne($companyExtendUser->extend_institution_id);
2155
                    if($extendedInstitution) {
2156
                        array_push($record['details'],[
2157
                            'uuid' => $extendedInstitution->uuid,
2158
                            'label' => 'LABEL_INSTITUTION',
2159
                            'value' => $extendedInstitution->name
2160
                        ]);
2161
                    }
2162
                }
2163
 
2164
                if($companyExtendUser->extend_program_id) {
2165
                    $extendedProgram = $companyExtendUserProgramMapper->fetchOne($companyExtendUser->extend_program_id);
2166
                    if($extendedProgram) {
2167
                        array_push($record['details'],[
2168
                            'uuid' => $extendedProgram->uuid,
2169
                            'label' => 'LABEL_PROGRAM',
2170
                            'value' => $extendedProgram->name
2171
                        ]);
2172
 
2173
                    }
2174
                }
2175
 
2176
                if($companyExtendUser->extend_sector_id) {
2177
                    $extendedSector = $companyExtendUserSectorMapper->fetchOne($companyExtendUser->extend_sector_id);
2178
                    if($extendedSector) {
2179
                        array_push($record['details'],[
2180
                            'uuid' => $extendedSector->uuid,
2181
                            'label' => 'LABEL_SECTOR',
2182
                            'value' => $extendedSector->name
2183
                        ]);
2184
                    }
2185
                }
2186
 
2187
                if($companyExtendUser->extend_partner_id) {
2188
                    $extendedPartner = $companyExtendUserPartnerMapper->fetchOne($companyExtendUser->extend_partner_id);
2189
                    if($extendedPartner) {
2190
                        array_push($record['details'],[
2191
                            'uuid' => $extendedPartner->uuid,
2192
                            'label' => 'LABEL_PARTNER',
2193
                            'value' => $extendedPartner->name
2194
                        ]);
2195
                    }
2196
                }
2197
 
2198
                if($companyExtendUser->extend_student_type_id) {
2199
                    $extendedStudentType = $companyExtendUserStudentTypeMapper->fetchOne($companyExtendUser->extend_student_type_id);
2200
                    if($extendedStudentType) {
2201
                        array_push($record['details'],[
2202
                            'uuid' => $extendedStudentType->uuid,
2203
                            'label' => 'LABEL_TYPE',
2204
                            'value' => $extendedStudentType->name
2205
                        ]);
2206
                    }
2207
                }
2208
 
2209
                array_push($data, $record);
2210
            }
2211
 
2212
            return new JsonModel([
2213
                'success' => true,
2214
                'data' => $data
2215
            ]);
2216
 
2217
        } else {
2218
            return new JsonModel([
2219
                'success' => false,
2220
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
2221
            ]);
2222
        }
2223
    }
2224
 
167 efrain 2225
    public function syncAction()
2226
    {
2227
        $request = $this->getRequest();
168 efrain 2228
        if($request->isPost()) {
167 efrain 2229
            $currentUserPlugin = $this->plugin('currentUserPlugin');
2230
            $currentUser = $currentUserPlugin->getUser();
2231
 
2232
 
2233
            $operation = $this->params()->fromRoute('operation');
2234
            if($operation == 'slide-view' || $operation == 'capsule-close' || $operation == 'topic-close') {
2235
                $accessGrantedIds = $this->getAccessGranted();
2236
 
2237
                $topic_uuid     = $this->params()->fromRoute('topic_uuid');
2238
                $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
2239
                $slide_uuid     = $this->params()->fromRoute('slide_uuid');
2240
 
2241
                $accessGrantedIds = $this->getAccessGranted();
2242
                if($operation == 'slide-view') {
2243
 
2244
                    if(empty($slide_uuid) || empty($capsule_uuid ) || empty($topic_uuid)) {
2245
                        return new JsonModel([
2246
                            'success' => false,
2247
                            'data' => 'ERROR_INVALID_PARAMETERS'
2248
                        ]);
2249
                    }
2250
 
2251
 
283 www 2252
                    $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
167 efrain 2253
                    $topic = $topicMapper->fetchOneByUuid($topic_uuid);
2254
 
2255
                    if(!$topic) {
2256
                        return new JsonModel([
2257
                            'success' => false,
2258
                            'data' => 'ERROR_TOPIC_NOT_FOUND'
2259
                        ]);
2260
                    }
2261
 
2262
 
2263
 
2264
                    if(!in_array($topic->id, $accessGrantedIds->topics)) {
2265
                        return new JsonModel([
2266
                            'success' => false,
2267
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
2268
                        ]);
2269
                    }
2270
 
283 www 2271
                    $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
167 efrain 2272
                    $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
2273
 
2274
                    if(!$capsule) {
2275
                        return new JsonModel([
2276
                            'success' => false,
2277
                            'data' => 'ERROR_CAPSULE_NOT_FOUND'
2278
                        ]);
2279
                    }
119 efrain 2280
 
167 efrain 2281
                    if(!in_array($capsule->id, $accessGrantedIds->capsules) || $capsule->topic_id != $topic->id) {
2282
                        return new JsonModel([
2283
                            'success' => false,
2284
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
2285
                        ]);
2286
                    }
2287
 
283 www 2288
                    $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
167 efrain 2289
                    $slide = $slideMapper->fetchOneByUuid($slide_uuid);
2290
                    if(!$slide) {
2291
                        return new JsonModel([
2292
                            'success' => false,
2293
                            'data' => 'ERROR_SLIDE_NOT_FOUND'
2294
                        ]);
2295
                    }
2296
 
2297
                    if($slide->capsule_id != $capsule->id && $slide->topic_id != $topic->id) {
2298
                        return new JsonModel([
2299
                            'success' => false,
2300
                            'data' => 'ERROR_SLIDE_NOT_FOUND'
2301
                        ]);
2302
 
2303
                    }
2304
 
2305
 
283 www 2306
                    $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
167 efrain 2307
 
283 www 2308
                    $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
167 efrain 2309
                    $added_on = $userProgressMapper->getDatebaseNow();
2310
 
2311
 
2312
                    $userProgressTopic = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic->id);
2313
                    if(!$userProgressTopic) {
2314
 
283 www 2315
                        $userProgressTopic = new MicrolearningUserProgress();
558 stevensc 2316
                        $userProgressTopic->company_id                  = $topic->company_id; // Usar company_id del tópico
2317
                        $userProgressTopic->topic_id                    = $topic->id;        // Usar id del tópico
176 efrain 2318
                        $userProgressTopic->user_id                     = $currentUser->id;
167 efrain 2319
                        $userProgressTopic->progress                    = 0;
2320
                        $userProgressTopic->returning                   = 0;
2321
                        $userProgressTopic->returning_after_completed   = 0;
2322
                        $userProgressTopic->completed                   = 0;
558 stevensc 2323
                        $userProgressTopic->total_slides                = $slideMapper->fetchTotalCountByCompanyIdAndTopicId($topic->company_id, $topic->id);
167 efrain 2324
                        $userProgressTopic->view_slides                 = 0;
283 www 2325
                        $userProgressTopic->type                        = MicrolearningUserProgress::TYPE_TOPIC;
167 efrain 2326
                        $userProgressTopic->added_on                    = $added_on;
2327
                        $userProgressTopic->updated_on                  = $added_on;
2328
 
2329
                        if($userProgressMapper->insert($userProgressTopic)) {
2330
 
283 www 2331
                            $userLog = new MicrolearningUserLog();
2332
                            $userLog->activity      = MicrolearningUserLog::ACTIVITY_START_TOPIC;
167 efrain 2333
                            $userLog->user_id       = $currentUser->id;
558 stevensc 2334
                            $userLog->company_id    = $topic->company_id;
2335
                            $userLog->topic_id      = $topic->id;
167 efrain 2336
                            $userLog->added_on      = $added_on;
2337
 
2338
                            if(!$userLogMapper->insert($userLog)) {
2339
                                return new JsonModel([
2340
                                    'success' => false,
2341
                                    'data' => $userProgressMapper->getError()
2342
                                ]);
2343
                            }
2344
                        } else {
2345
                            return new JsonModel([
2346
                                'success' => false,
2347
                                'data' => $userProgressMapper->getError()
2348
                            ]);
2349
                        }
2350
                    }
2351
 
2352
 
2353
                    $userProgressCapsule = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
2354
                    if(!$userProgressCapsule) {
2355
 
283 www 2356
                        $userProgressCapsule = new MicrolearningUserProgress();
558 stevensc 2357
                        $userProgressCapsule->company_id                  = $capsule->company_id; // Usar company_id de la cápsula
2358
                        $userProgressCapsule->topic_id                    = $topic->id;         // Usar el id del tópico obtenido previamente
2359
                        $userProgressCapsule->capsule_id                  = $capsule->id;       // Usar id de la cápsula
176 efrain 2360
                        $userProgressCapsule->user_id                     = $currentUser->id;
167 efrain 2361
                        $userProgressCapsule->progress                    = 0;
2362
                        $userProgressCapsule->returning                   = 0;
2363
                        $userProgressCapsule->returning_after_completed   = 0;
2364
                        $userProgressCapsule->completed                   = 0;
558 stevensc 2365
                        $userProgressCapsule->total_slides                = $slideMapper->fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($capsule->company_id, $topic->id, $capsule->id);
167 efrain 2366
                        $userProgressCapsule->view_slides                 = 0;
2367
                        $userProgressTopic->updated_on                    = $added_on;
2368
 
2369
                        if($userProgressMapper->insert($userProgressCapsule)) {
2370
 
283 www 2371
                            $userLog = new MicrolearningUserLog();
2372
                            $userLog->activity      = MicrolearningUserLog::ACTIVITY_START_CAPSULE;
167 efrain 2373
                            $userLog->user_id       = $currentUser->id;
558 stevensc 2374
                            $userLog->company_id    = $capsule->company_id;
2375
                            $userLog->topic_id      = $topic->id;
2376
                            $userLog->capsule_id    = $capsule->id;
167 efrain 2377
                            $userLog->added_on      = $added_on;
2378
 
2379
                            if(!$userLogMapper->insert($userLog)) {
2380
                                return new JsonModel([
2381
                                    'success' => false,
2382
                                    'data' => $userLogMapper->getError()
2383
                                ]);
2384
                            }
2385
                        } else {
2386
                            return new JsonModel([
2387
                                'success' => false,
2388
                                'data' => $userProgressMapper->getError()
2389
                            ]);
2390
                        }
2391
                    }
2392
 
2393
 
2394
                    $userProgressSlide = $userProgressMapper->fetchOneByUserIdAndSlideId($currentUser->id, $slide->id);
2395
                    if(!$userProgressSlide) {
283 www 2396
                        $userProgressSlide = new MicrolearningUserProgress();
167 efrain 2397
                        $userProgressSlide->company_id                  = $slide->company_id;
2398
                        $userProgressSlide->topic_id                    = $slide->topic_id;
2399
                        $userProgressSlide->capsule_id                  = $slide->capsule_id;
2400
                        $userProgressSlide->slide_id                    = $slide->id;
176 efrain 2401
                        $userProgressSlide->user_id                     = $currentUser->id;
167 efrain 2402
                        $userProgressSlide->progress                    = 0;
2403
                        $userProgressSlide->returning                   = 0;
2404
                        $userProgressSlide->returning_after_completed   = 0;
2405
                        $userProgressSlide->completed                   = 1;
2406
                        $userProgressSlide->total_slides                = 0;
2407
                        $userProgressSlide->view_slides                 = 0;
283 www 2408
                        $userProgressSlide->type                        = MicrolearningUserProgress::TYPE_SLIDE;
167 efrain 2409
                        $userProgressSlide->added_on                    = $added_on;
2410
                        $userProgressSlide->updated_on                  = $added_on;
2411
 
2412
                        if(!$userProgressMapper->insert($userProgressSlide)) {
2413
                            return new JsonModel([
2414
                                'success' => false,
2415
                                'data' => $userProgressMapper->getError()
2416
                            ]);
2417
                        }
2418
                    }
2419
 
283 www 2420
                    $userLog = new MicrolearningUserLog();
2421
                    $userLog->activity      = MicrolearningUserLog::ACTIVITY_VIEW_SLIDE;
167 efrain 2422
                    $userLog->user_id       = $currentUser->id;
2423
                    $userLog->company_id    = $slide->company_id;
2424
                    $userLog->topic_id      = $slide->topic_id;
2425
                    $userLog->capsule_id    = $slide->capsule_id;
2426
                    $userLog->slide_id      = $slide->id;
2427
                    $userLog->added_on      = $added_on;
2428
 
2429
                    if(!$userLogMapper->insert($userLog)) {
2430
                        return new JsonModel([
2431
                            'success' => false,
2432
                            'data' => $userLogMapper->getError()
2433
                        ]);
2434
                    }
2435
 
2436
                    $closeCapsule = false;
2437
 
2438
                    if($userProgressCapsule->completed) {
2439
 
2440
                        $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $added_on);
2441
                        $returning_on = $dt->format('Y-m-d');
2442
 
558 stevensc 2443
                        // Asegurarse que returning_on existe en el objeto antes de comparar
2444
                        $current_returning_on = isset($userProgressCapsule->returning_on) ? $userProgressCapsule->returning_on : null;
2445
 
2446
                        if(!$current_returning_on || $current_returning_on != $returning_on) {
167 efrain 2447
 
2448
                            $userProgressCapsule->returning_on = $returning_on;
558 stevensc 2449
                            // Asegurarse que returning_after_completed existe y es numérico antes de incrementar
2450
                            $userProgressCapsule->returning_after_completed = (isset($userProgressCapsule->returning_after_completed) ? intval($userProgressCapsule->returning_after_completed) : 0) + 1;
167 efrain 2451
 
2452
                            if(!$userProgressMapper->update($userProgressCapsule)) {
2453
                                return new JsonModel([
2454
                                    'success' => false,
2455
                                    'data' => $userProgressMapper->getError()
2456
                                ]);
2457
                            }
2458
                        }
2459
 
2460
                    }  else {
2461
 
558 stevensc 2462
                        // Usar $topic->id para obtener el total de slides de la cápsula en el contexto de este tópico
2463
                        $userProgressCapsule->total_slides = $slideMapper->fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($capsule->company_id, $topic->id, $capsule->id);
2464
                        $userProgressCapsule->view_slides = $userProgressMapper->fetchCountAllSlideCompletedByUserIdAndCapsuleId($currentUser->id, $capsule->id);
167 efrain 2465
 
2466
                        if($userProgressCapsule->total_slides) {
2467
 
2468
                            $userProgressCapsule->progress = ($userProgressCapsule->view_slides * 100) / $userProgressCapsule->total_slides;
2469
                            $userProgressCapsule->progress = $userProgressCapsule->progress > 100 ? 100 : $userProgressCapsule->progress;
2470
                        }
2471
 
2472
                        if(!$userProgressMapper->update($userProgressCapsule)) {
2473
                            return new JsonModel([
2474
                                'success' => false,
2475
                                'data' => $userProgressMapper->getError()
2476
                            ]);
2477
                        }
2478
 
2479
                        if($userProgressCapsule->progress >= 100) {
2480
                            $closeCapsule = true;
2481
                        }
171 efrain 2482
 
283 www 2483
 
167 efrain 2484
                    }
2485
 
2486
                    $closeTopic = false;
2487
                    if(!$userProgressTopic->completed) {
2488
 
558 stevensc 2489
                        // Usar $topic->id para obtener el total de slides del tópico
2490
                        $userProgressTopic->total_slides = $slideMapper->fetchTotalCountByCompanyIdAndTopicId($topic->company_id, $topic->id);
2491
                        $userProgressTopic->view_slides = $userProgressMapper->fetchCountAllSlideCompletedByUserIdAndTopicId($currentUser->id, $topic->id);
167 efrain 2492
 
2493
                        if($userProgressTopic->total_slides) {
2494
 
2495
                            $userProgressTopic->progress = ($userProgressTopic->view_slides * 100) / $userProgressTopic->total_slides;
2496
                            $userProgressTopic->progress = $userProgressTopic->progress > 100 ? 100 : $userProgressTopic->progress;
2497
                        }
2498
                        if(!$userProgressMapper->update($userProgressTopic)) {
2499
                            return new JsonModel([
2500
                                'success' => false,
2501
                                'data' => $userProgressMapper->getError()
2502
                            ]);
2503
                        }
2504
 
2505
                        if($userProgressTopic->progress >= 100) {
2506
                            $closeTopic = true;
2507
                        }
2508
                    }
2509
 
2510
 
2511
                    $data = [
2512
                        'message' => 'LABEL_THE_USER_PROGRESS_FOR_THIS_SLIDE_HAS_BEEN_COMPLETED',
2513
 
2514
                    ];
2515
 
2516
                    if($closeCapsule) {
173 efrain 2517
                        $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 2518
                    }
2519
 
2520
 
2521
                    if($closeTopic) {
173 efrain 2522
                        $data['link_close_topic'] = $this->url()->fromRoute('microlearning/sync', ['operation' => 'topic-close', 'topic_uuid' => $topic->uuid], ['force_canonical' => true]);
167 efrain 2523
                    }
2524
 
2525
 
2526
                    return new JsonModel([
2527
                        'success' => true,
2528
                        'data' => $data
2529
                    ]);
2530
 
2531
 
2532
 
2533
 
2534
                } else if($operation == 'capsule-close') {
2535
 
2536
                    if(empty($capsule_uuid ) || empty($topic_uuid)) {
2537
                        return new JsonModel([
2538
                            'success' => false,
2539
                            'data' => 'ERROR_INVALID_PARAMETERS'
2540
                        ]);
2541
                    }
2542
 
2543
 
283 www 2544
                    $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
167 efrain 2545
                    $topic = $topicMapper->fetchOneByUuid($topic_uuid);
2546
 
2547
                    if(!$topic) {
2548
                        return new JsonModel([
2549
                            'success' => false,
2550
                            'data' => 'ERROR_TOPIC_NOT_FOUND'
2551
                        ]);
2552
                    }
2553
 
2554
 
2555
 
2556
                    if(!in_array($topic->id, $accessGrantedIds->topics)) {
2557
                        return new JsonModel([
2558
                            'success' => false,
2559
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
2560
                        ]);
2561
                    }
2562
 
283 www 2563
                    $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
167 efrain 2564
                    $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
2565
 
2566
                    if(!$capsule) {
2567
                        return new JsonModel([
2568
                            'success' => false,
2569
                            'data' => 'ERROR_CAPSULE_NOT_FOUND'
2570
                        ]);
2571
                    }
2572
 
2573
                    if(!in_array($capsule->id, $accessGrantedIds->capsules) || $capsule->topic_id != $topic->id) {
2574
                        return new JsonModel([
2575
                            'success' => false,
2576
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
2577
                        ]);
2578
                    }
2579
 
2580
 
283 www 2581
                    $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
167 efrain 2582
 
283 www 2583
                    $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
167 efrain 2584
                    $updated_on = $userProgressMapper->getDatebaseNow();
2585
 
2586
                    $userProgressCapsule = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
2587
                    if(!$userProgressCapsule) {
2588
                        return new JsonModel([
2589
                            'success' => false,
2590
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_CAPSULE_NOT_FOUND'
2591
                        ]);
2592
                    }
2593
 
2594
                    if($userProgressCapsule->completed) {
2595
                        return new JsonModel([
2596
                            'success' => false,
2597
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_CAPSULE_ALREADY_CLOSED'
2598
                        ]);
2599
                    }
2600
 
2601
 
2602
                    $userProgressCapsule->completed = 1;
2603
                    $userProgressCapsule->updated_on = $updated_on;
2604
 
2605
                    if($userProgressMapper->update($userProgressCapsule)) {
225 efrain 2606
 
2607
 
283 www 2608
                        $userLog = new MicrolearningUserLog();
2609
                        $userLog->activity      = MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE;
227 efrain 2610
                        $userLog->user_id       = $currentUser->id;
2611
                        $userLog->company_id    = $capsule->company_id;
2612
                        $userLog->topic_id      = $capsule->topic_id;
2613
                        $userLog->capsule_id    = $capsule->id;
2614
                        $userLog->added_on      = $updated_on;
225 efrain 2615
 
2616
                        if(!$userLogMapper->insert($userLog)) {
2617
                            return new JsonModel([
2618
                                'success' => false,
2619
                                'data' => $userProgressMapper->getError()
2620
                            ]);
2621
                        }
2622
 
2623
 
167 efrain 2624
                        return new JsonModel([
2625
                            'success' => true,
2626
                            'data' => 'LABEL_THE_USER_PROGRESS_FOR_THIS_CAPSULE_HAS_BEEN_COMPLETED'
2627
                        ]);
2628
                    } else {
2629
                        return new JsonModel([
2630
                            'success' => false,
2631
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_CAPSULE_COULD_NOT_BE_COMPLETED'
2632
                        ]);
2633
                    }
2634
 
2635
 
2636
 
2637
 
2638
 
2639
 
2640
 
2641
                } else if($operation == 'topic-close') {
174 efrain 2642
                    if(empty($topic_uuid)) {
167 efrain 2643
                        return new JsonModel([
2644
                            'success' => false,
2645
                            'data' => 'ERROR_INVALID_PARAMETERS'
2646
                        ]);
2647
                    }
2648
 
2649
 
283 www 2650
                    $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
167 efrain 2651
                    $topic = $topicMapper->fetchOneByUuid($topic_uuid);
2652
 
2653
                    if(!$topic) {
2654
                        return new JsonModel([
2655
                            'success' => false,
2656
                            'data' => 'ERROR_TOPIC_NOT_FOUND'
2657
                        ]);
2658
                    }
2659
 
2660
 
2661
 
2662
                    if(!in_array($topic->id, $accessGrantedIds->topics)) {
2663
                        return new JsonModel([
2664
                            'success' => false,
2665
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
2666
                        ]);
2667
                    }
2668
 
2669
 
2670
 
2671
 
283 www 2672
                    $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
167 efrain 2673
 
283 www 2674
                    $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
167 efrain 2675
                    $updated_on = $userProgressMapper->getDatebaseNow();
2676
 
2677
                    $userProgressTopic = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic->id);
2678
                    if(!$userProgressTopic) {
2679
                        return new JsonModel([
2680
                            'success' => false,
2681
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_TOPIC_NOT_FOUND'
2682
                        ]);
2683
                    }
2684
 
2685
                    if($userProgressTopic->completed) {
2686
                        return new JsonModel([
2687
                            'success' => false,
2688
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_TOPIC_ALREADY_CLOSED'
2689
                        ]);
2690
                    }
2691
 
2692
 
2693
                    $userProgressTopic->completed = 1;
2694
                    $userProgressTopic->updated_on = $updated_on;
2695
 
2696
                    if($userProgressMapper->update($userProgressTopic)) {
226 efrain 2697
 
283 www 2698
                        $userLog = new MicrolearningUserLog();
2699
                        $userLog->activity      = MicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC;
227 efrain 2700
                        $userLog->user_id       = $currentUser->id;
2701
                        $userLog->company_id    = $topic->company_id;
2702
                        $userLog->topic_id      = $topic->id;
2703
                        $userLog->added_on      = $updated_on;
226 efrain 2704
 
2705
                        if(!$userLogMapper->insert($userLog)) {
2706
                            return new JsonModel([
2707
                                'success' => false,
2708
                                'data' => $userProgressMapper->getError()
2709
                            ]);
2710
                        }
2711
 
167 efrain 2712
                        return new JsonModel([
2713
                            'success' => true,
2714
                            'data' => 'LABEL_THE_USER_PROGRESS_FOR_THIS_TOPIC_HAS_BEEN_COMPLETED'
2715
                        ]);
2716
                    } else {
2717
                        return new JsonModel([
2718
                            'success' => false,
2719
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_TOPIC_COULD_NOT_BE_COMPLETED'
2720
                        ]);
2721
                    }
2722
                }
2723
 
2724
 
2725
 
2726
 
2727
            } else {
2728
 
2729
                return new JsonModel([
2730
                    'success' => false,
2731
                    'data' => 'ERROR_OPERATION_UNKNOWN'
2732
                ]);
2733
 
2734
 
2735
            }
2736
 
2737
        } else {
2738
            return new JsonModel([
2739
                'success' => false,
2740
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
2741
            ]);
2742
        }
2743
    }
2744
 
2745
 
302 www 2746
 
2747
 
2748
 
119 efrain 2749
    /**
2750
     *
2751
     * @return \LeadersLinked\Controller\MicrolearningUserAccessGrantedIds
2752
     */
2753
    private function getAccessGranted()
2754
    {
2755
        $currentUserPlugin = $this->plugin('currentUserPlugin');
2756
        $currentUser = $currentUserPlugin->getUser();
2757
 
283 www 2758
        $capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
119 efrain 2759
        $now = $capsuleUserMapper->getDatebaseNow();
2760
 
2761
        $records = $capsuleUserMapper->fetchAllActiveByUserId($currentUser->id);
2762
 
2763
        $accessGrantedIds = new MicrolearningUserAccessGrantedIds();
2764
 
2765
 
2766
        foreach($records as $record)
2767
        {
283 www 2768
            if($record->access != MicrolearningCapsuleUser::ACCESS_UNLIMITED && $record->access != MicrolearningCapsuleUser::ACCESS_PAY_PERIOD) {
119 efrain 2769
                continue;
2770
            }
283 www 2771
            if($record->access == MicrolearningCapsuleUser::ACCESS_PAY_PERIOD) {
119 efrain 2772
                if($now < $record->paid_from || $now > $record->paid_to) {
2773
                    continue;
2774
                }
2775
            }
2776
 
2777
 
2778
            if(!in_array($record->company_id, $accessGrantedIds->companies )) {
2779
                array_push($accessGrantedIds->companies, $record->company_id);
2780
            }
2781
 
2782
            if(!in_array($record->topic_id, $accessGrantedIds->topics )) {
2783
                array_push( $accessGrantedIds->topics, $record->topic_id);
2784
            }
2785
 
2786
            if(!in_array($record->capsule_id, $accessGrantedIds->capsules)) {
2787
                array_push( $accessGrantedIds->capsules, $record->capsule_id);
2788
            }
2789
        }
2790
 
2791
        return $accessGrantedIds;
2792
    }
2793
 
2794
 
2795
}