Proyectos de Subversion LeadersLinked - Services

Rev

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