Proyectos de Subversion LeadersLinked - Services

Rev

Rev 301 | Rev 333 | 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
 
283 www 153
            $storage = Storage::getInstance($this->config);
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
 
219
            $storage = Storage::getInstance($this->config);
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) {
283 www 506
                $storage = Storage::getInstance($this->config);
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);
119 efrain 515
 
516
 
302 www 517
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id,  $capsule->id);
119 efrain 518
 
301 www 519
 
119 efrain 520
                $response = [
521
                    'success' => true,
522
                    'data' => [
164 efrain 523
                        'uuid'              => $capsule->uuid,
119 efrain 524
                        'name'              => $capsule->name ? $capsule->name : '',
525
                        'description'       => $capsule->description ? $capsule->description : '',
283 www 526
                        'image'             => $capsule->image ? $storage->getGenericImage($path,  $capsule->uuid, $capsule->image )  : '',
119 efrain 527
                        'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
528
                        'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
529
                        'link_slides'       => $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic->uuid,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]),
530
                        'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
531
                        'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
532
                        'completed'         => $userProgress->completed,
145 efrain 533
                        'progress'          => $userProgress->progress,
162 efrain 534
                        'link_get'          => $this->url()->fromRoute('microlearning/get-capsule', ['id' => $capsule->uuid], ['force_canonical' => true]),
119 efrain 535
                        'added_on'          => $userProgress->added_on,
536
                        'updated_on'        => $userProgress->updated_on
537
                    ]
538
                ];
539
 
540
 
541
            } else {
542
                $response = [
543
                    'success' => true,
544
                    'data' => [
545
 
546
                    ]
547
                ];
548
            }
549
 
550
 
551
        } else {
552
 
553
            $response = [
554
                'success' => false,
555
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
556
            ];
557
 
558
 
559
        }
560
 
561
        return new JsonModel($response);
562
    }
563
 
564
    public function capsulesPendingAction()
565
    {
566
        $request = $this->getRequest();
567
        if($request->isGet()) {
568
            $currentUserPlugin = $this->plugin('currentUserPlugin');
569
            $currentUser = $currentUserPlugin->getUser();
570
 
571
            $name = Functions::sanitizeFilterString($this->params()->fromQuery('name'));
245 efrain 572
            $order_field = Functions::sanitizeFilterString($this->params()->fromQuery('order_field'));
573
            $order_direction = Functions::sanitizeFilterString($this->params()->fromQuery('order_direction'));
119 efrain 574
 
245 efrain 575
            if(!in_array($order_field,['name', 'added_on'] )) {
576
                $order_field = 'name';
577
            }
119 efrain 578
 
245 efrain 579
            if(!in_array( $order_direction,['asc', 'desc'])) {
580
                $order_field = 'asc';
581
            }
582
 
583
 
283 www 584
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
585
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
586
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
587
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 588
 
589
            $accessGranted = $this->getAccessGranted();
590
 
591
 
592
            $topics = [];
593
            $capsules = [];
594
 
283 www 595
            $storage = Storage::getInstance($this->config);
596
            $path = $storage->getPathMicrolearningCapsule();
597
 
119 efrain 598
            foreach($accessGranted->capsules as $capsule_id)
599
            {
600
                $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule_id);
601
                if($userProgress) {
602
                    continue;
603
                }
604
 
605
                $capsule = $capsuleMapper->fetchOne($capsule_id);
606
 
607
                if($name) {
608
                    if(empty($name) || stripos($capsule->name, $name) === false) {
609
                        continue;
610
                    }
611
                }
612
 
302 www 613
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id,  $capsule->id);
119 efrain 614
 
615
                if(isset($topics[$capsule->topic_id])) {
616
                    $topic = $topics[$capsule->topic_id];
617
                } else {
618
                    $topic = $topicMapper->fetchOne($capsule->topic_id);
619
                    $topics[ $capsule->topic_id ] = $topic;
620
                }
621
 
622
 
623
                array_push($capsules, [
624
                    'uuid'              => $capsule->uuid,
625
                    'name'              => $capsule->name ? $capsule->name : '',
626
                    'description'       => $capsule->description ? $capsule->description : '',
283 www 627
                    'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image)  : '',
119 efrain 628
                    'position'          => $capsule->order,
629
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
630
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
631
                    'link_slides'       => $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic->uuid,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]),
632
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
633
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
191 efrain 634
                    'progress'          => 0,
162 efrain 635
                    'link_get'          => $this->url()->fromRoute('microlearning/get-capsule', ['id' => $capsule->uuid], ['force_canonical' => true]),
119 efrain 636
                    'added_on'          => $capsule->added_on,
637
                    'updated_on'        => $capsule->updated_on,
638
                ]);
639
            }
640
 
245 efrain 641
 
642
            if($order_field == 'name') {
643
                if($order_direction == 'asc') {
644
                    usort($capsules, function($a, $b) {
645
                        return strcasecmp($a['name'], $b['name']);
646
                    });
647
                } else {
648
                    usort($capsules, function($a, $b) {
649
                        $result = strcasecmp($a['name'], $b['name']);
650
                        if($result < 0) {
651
                            return 1;
652
                        } else if($result > 0) {
653
                            return -1;
654
                        } else  {
655
                            return  0;
656
                        }
657
                    });
119 efrain 658
                }
659
 
245 efrain 660
            }
661
 
662
            if($order_field == 'added_on') {
663
                if($order_direction == 'asc') {
664
                    usort($capsules, function($a, $b) {
665
                        return strcasecmp($a['added_on'], $b['added_on']);
666
                    });
667
                } else {
668
                    usort($capsules, function($a, $b) {
669
                        $result = strcasecmp($a['added_on'], $b['added_on']);
670
                        if($result < 0) {
671
                            return 1;
672
                        } else if($result > 0) {
673
                            return -1;
674
                        } else  {
675
                            return  0;
676
                        }
677
                    });
678
                }
119 efrain 679
 
245 efrain 680
            }
681
 
682
 
683
 
684
 
685
 
119 efrain 686
 
245 efrain 687
 
119 efrain 688
            return new JsonModel([
689
                'success' => true,
690
                'data' => $capsules
691
            ]);
692
 
693
 
694
 
695
        }
696
 
697
        return new JsonModel([
698
            'success' => false,
699
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
700
        ]);
701
 
702
    }
703
 
704
    public function capsulesCompletedAction()
705
    {
706
        $request = $this->getRequest();
707
        if($request->isGet()) {
708
            $currentUserPlugin = $this->plugin('currentUserPlugin');
709
            $currentUser = $currentUserPlugin->getUser();
710
 
711
            $name = Functions::sanitizeFilterString($this->params()->fromQuery('name'));
245 efrain 712
            $order_field = Functions::sanitizeFilterString($this->params()->fromQuery('order_field'));
713
            $order_direction = Functions::sanitizeFilterString($this->params()->fromQuery('order_direction'));
119 efrain 714
 
246 efrain 715
            if(!in_array($order_field,['name', 'added_on','last_access_on'] )) {
245 efrain 716
                $order_field = 'name';
717
            }
119 efrain 718
 
245 efrain 719
            if(!in_array( $order_direction,['asc', 'desc'])) {
720
                $order_field = 'asc';
721
            }
722
 
283 www 723
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
724
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
725
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
726
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 727
 
728
            $accessGranted = $this->getAccessGranted();
729
 
730
            $topics = [];
731
            $capsules = [];
732
 
283 www 733
            $storage = Storage::getInstance($this->config);
734
            $path = $storage->getPathMicrolearningCapsule();
735
 
119 efrain 736
            foreach($accessGranted->capsules as $capsule_id)
737
            {
738
                $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule_id);
739
                if(!$userProgress) {
740
                    continue;
741
                }
742
 
743
                if(!$userProgress->completed) {
744
                    continue;
745
                }
746
 
747
 
748
 
749
                $capsule = $capsuleMapper->fetchOne($capsule_id);
750
 
751
                if($name) {
752
                    if(empty($name) || stripos($capsule->name, $name) === false) {
753
                        continue;
754
                    }
755
                }
756
 
302 www 757
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->id);
119 efrain 758
 
759
                if(isset($topics[$capsule->topic_id])) {
760
                    $topic = $topics[$capsule->topic_id];
761
                } else {
762
                    $topic = $topicMapper->fetchOne($capsule->topic_id);
763
                    $topics[ $capsule->topic_id ] = $topic;
764
                }
765
 
766
 
767
 
768
                array_push($capsules, [
769
                    'uuid'              => $capsule->uuid,
770
                    'name'              => $capsule->name ? $capsule->name : '',
771
                    'description'       => $capsule->description ? $capsule->description : '',
283 www 772
                    'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image)  : '',
119 efrain 773
                    'position'          => $capsule->order,
774
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
775
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
776
                    'link_slides'       => $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic->uuid,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]),
777
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
778
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
145 efrain 779
                    'progress'          => $userProgress->progress,
162 efrain 780
                    'link_get'          => $this->url()->fromRoute('microlearning/get-capsule', ['id' => $capsule->uuid], ['force_canonical' => true]),
119 efrain 781
                    'added_on'          => $capsule->added_on,
246 efrain 782
                    'last_access_on'    => $userProgress->updated_on,
119 efrain 783
                    'updated_on'        => $capsule->updated_on,
784
                ]);
785
            }
786
 
245 efrain 787
            if($order_field == 'name') {
788
                if($order_direction == 'asc') {
789
                    usort($capsules, function($a, $b) {
790
                        return strcasecmp($a['name'], $b['name']);
791
                    });
792
                } else {
793
                    usort($capsules, function($a, $b) {
794
                        $result = strcasecmp($a['name'], $b['name']);
795
                        if($result < 0) {
796
                            return 1;
797
                        } else if($result > 0) {
798
                            return -1;
799
                        } else  {
800
                            return  0;
801
                        }
802
                    });
119 efrain 803
                }
804
 
245 efrain 805
            }
806
 
807
            if($order_field == 'added_on') {
808
                if($order_direction == 'asc') {
809
                    usort($capsules, function($a, $b) {
810
                        return strcasecmp($a['added_on'], $b['added_on']);
811
                    });
812
                } else {
813
                    usort($capsules, function($a, $b) {
814
                        $result = strcasecmp($a['added_on'], $b['added_on']);
815
                        if($result < 0) {
816
                            return 1;
817
                        } else if($result > 0) {
818
                            return -1;
819
                        } else  {
820
                            return  0;
821
                        }
822
                    });
823
                }
119 efrain 824
 
245 efrain 825
            }
246 efrain 826
 
827
            if($order_field == 'last_access_on') {
828
                if($order_direction == 'asc') {
829
                    usort($capsules, function($a, $b) {
830
                        return strcasecmp($a['last_access_on'], $b['last_access_on']);
831
                    });
832
                } else {
833
                    usort($capsules, function($a, $b) {
834
                        $result = strcasecmp($a['last_access_on'], $b['last_access_on']);
835
                        if($result < 0) {
836
                            return 1;
837
                        } else if($result > 0) {
838
                            return -1;
839
                        } else  {
840
                            return  0;
841
                        }
842
                    });
843
                }
245 efrain 844
 
246 efrain 845
            }
846
 
847
 
848
 
849
 
119 efrain 850
 
851
            return new JsonModel([
852
                'success' => true,
853
                'data' => $capsules
854
            ]);
855
 
856
        }
857
 
858
        return new JsonModel([
859
            'success' => false,
860
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
861
        ]);
862
    }
863
 
864
    public function capsulesInProgressAction()
865
    {
866
        $request = $this->getRequest();
867
        if($request->isGet()) {
868
            $currentUserPlugin = $this->plugin('currentUserPlugin');
869
            $currentUser = $currentUserPlugin->getUser();
870
 
871
            $name = Functions::sanitizeFilterString($this->params()->fromQuery('name'));
245 efrain 872
            $order_field = Functions::sanitizeFilterString($this->params()->fromQuery('order_field'));
873
            $order_direction = Functions::sanitizeFilterString($this->params()->fromQuery('order_direction'));
119 efrain 874
 
246 efrain 875
            if(!in_array($order_field,['name', 'added_on', 'last_access_on'] )) {
245 efrain 876
                $order_field = 'name';
877
            }
119 efrain 878
 
245 efrain 879
            if(!in_array( $order_direction,['asc', 'desc'])) {
880
                $order_field = 'asc';
881
            }
882
 
283 www 883
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
884
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
885
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
886
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 887
 
888
            $accessGranted = $this->getAccessGranted();
889
 
890
            $topics = [];
891
            $capsules = [];
892
 
283 www 893
            $storage = Storage::getInstance($this->config);
894
            $path = $storage->getPathMicrolearningCapsule();
895
 
119 efrain 896
            foreach($accessGranted->capsules as $capsule_id)
897
            {
898
                $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule_id);
899
                if(!$userProgress) {
900
                    continue;
901
                }
902
 
903
                if($userProgress->completed) {
904
                    continue;
905
                }
246 efrain 906
 
119 efrain 907
                $capsule = $capsuleMapper->fetchOne($capsule_id);
908
 
909
                if($name) {
910
                    if(empty($name) || stripos($capsule->name, $name) === false) {
911
                        continue;
912
                    }
913
                }
914
 
915
 
302 www 916
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id,  $capsule->id);
119 efrain 917
 
918
                if(isset($topics[$capsule->topic_id])) {
919
                    $topic = $topics[$capsule->topic_id];
920
                } else {
921
                    $topic = $topicMapper->fetchOne($capsule->topic_id);
922
                    $topics[ $capsule->topic_id ] = $topic;
923
                }
924
 
925
 
926
 
927
 
928
                array_push($capsules, [
929
                    'uuid'              => $capsule->uuid,
930
                    'name'              => $capsule->name ? $capsule->name : '',
931
                    'description'       => $capsule->description ? $capsule->description : '',
283 www 932
                    'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image)  : '',
119 efrain 933
                    'position'          => $capsule->order,
934
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
935
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
936
                    'link_slides'       => $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic->uuid,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]),
937
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
938
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
145 efrain 939
                    'progress'          => $userProgress->progress,
162 efrain 940
                    'link_get'          => $this->url()->fromRoute('microlearning/get-capsule', ['id' => $capsule->uuid], ['force_canonical' => true]),
246 efrain 941
                    'last_access_on'    => $userProgress->updated_on,
119 efrain 942
                    'added_on'          => $capsule->added_on,
943
                    'updated_on'        => $capsule->updated_on,
944
                ]);
945
            }
946
 
245 efrain 947
            if($order_field == 'name') {
948
                if($order_direction == 'asc') {
949
                    usort($capsules, function($a, $b) {
950
                        return strcasecmp($a['name'], $b['name']);
951
                    });
952
                } else {
953
                    usort($capsules, function($a, $b) {
954
                        $result = strcasecmp($a['name'], $b['name']);
955
                        if($result < 0) {
956
                            return 1;
957
                        } else if($result > 0) {
958
                            return -1;
959
                        } else  {
960
                            return  0;
961
                        }
962
                    });
119 efrain 963
                }
964
 
245 efrain 965
            }
966
 
967
            if($order_field == 'added_on') {
968
                if($order_direction == 'asc') {
969
                    usort($capsules, function($a, $b) {
970
                        return strcasecmp($a['added_on'], $b['added_on']);
971
                    });
972
                } else {
973
                    usort($capsules, function($a, $b) {
974
                        $result = strcasecmp($a['added_on'], $b['added_on']);
975
                        if($result < 0) {
976
                            return 1;
977
                        } else if($result > 0) {
978
                            return -1;
979
                        } else  {
980
                            return  0;
981
                        }
982
                    });
983
                }
119 efrain 984
 
245 efrain 985
            }
246 efrain 986
 
987
            if($order_field == 'last_access_on') {
988
                if($order_direction == 'asc') {
989
                    usort($capsules, function($a, $b) {
990
                        return strcasecmp($a['last_access_on'], $b['last_access_on']);
991
                    });
992
                } else {
993
                    usort($capsules, function($a, $b) {
994
                        $result = strcasecmp($a['last_access_on'], $b['last_access_on']);
995
                        if($result < 0) {
996
                            return 1;
997
                        } else if($result > 0) {
998
                            return -1;
999
                        } else  {
1000
                            return  0;
1001
                        }
1002
                    });
1003
                }
119 efrain 1004
 
246 efrain 1005
            }
245 efrain 1006
 
246 efrain 1007
 
119 efrain 1008
            return new JsonModel([
1009
                'success' => true,
1010
                'data' => $capsules
1011
            ]);
1012
 
1013
 
1014
 
1015
        }
1016
 
1017
        return new JsonModel([
1018
            'success' => false,
1019
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1020
        ]);
1021
    }
1022
 
1023
 
1024
    public function timelineAction()
1025
    {
1026
 
1027
        $request = $this->getRequest();
1028
        if($request->isGet()) {
1029
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1030
            $currentUser = $currentUserPlugin->getUser();
1031
 
1032
 
1033
            $page = intval($this->params()->fromQuery('page'), 10);
1034
 
1035
            $activities = [
283 www 1036
                MicrolearningUserLog::ACTIVITY_SIGNIN            => 'LABEL_MICROLEARNING_ACTIVITY_SIGNIN',
1037
                MicrolearningUserLog::ACTIVITY_SIGNOUT           => 'LABEL_MICROLEARNING_ACTIVITY_SIGNOUT',
1038
                MicrolearningUserLog::ACTIVITY_START_TOPIC       => 'LABEL_MICROLEARNING_ACTIVITY_START_TOPIC',
1039
                MicrolearningUserLog::ACTIVITY_START_CAPSULE     => 'LABEL_MICROLEARNING_ACTIVITY_START_CAPSULE',
1040
                MicrolearningUserLog::ACTIVITY_VIEW_SLIDE        => 'LABEL_MICROLEARNING_ACTIVITY_VIEW_SLIDE',
1041
                MicrolearningUserLog::ACTIVITY_TAKE_A_TEST       => 'LABEL_MICROLEARNING_ACTIVITY_TAKE_A_TEST',
1042
                MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST     => 'LABEL_MICROLEARNING_ACTIVITY_RETAKE_A_TEST',
1043
                MicrolearningUserLog::ACTIVITY_APPROVED_TEST     => 'LABEL_MICROLEARNING_ACTIVITY_APPROVED_TEST',
1044
                MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE => 'LABEL_MICROLEARNING_ACTIVITY_COMPLETED_CAPSULE',
1045
                MicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC   => 'LABEL_MICROLEARNING_ACTIVITY_COMPLETED_TOPIC',
119 efrain 1046
            ];
1047
 
1048
 
1049
 
283 www 1050
            $microlearningUserLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
1051
            $paginator = $microlearningUserLogMapper->getAllMessagesPaginatorByUserId($currentUser->id, $page);
119 efrain 1052
 
1053
            $items = [];
1054
            foreach($paginator as $record)
1055
            {
1056
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
1057
 
1058
                array_push($items, [
1059
                    'activity' => $activities[$record->activity],
1060
                    'added_on' => $dt->format('d/m/Y H:i a')
1061
                ]);
1062
            }
1063
 
1064
 
1065
            return new JsonModel([
1066
                'success' => true,
1067
                'data' => [
1068
                    'total' => [
1069
                        'count' => $paginator->getTotalItemCount(),
1070
                        'pages' => $paginator->getPages()->pageCount,
1071
                    ],
1072
                    'current' => [
1073
                        'items'    => $items,
1074
                        'page'     => $paginator->getCurrentPageNumber(),
1075
                        'count'    => $paginator->getCurrentItemCount(),
1076
                    ]
1077
                ]
1078
            ]);
1079
 
1080
 
1081
        } else {
1082
            return new JsonModel([
1083
                'success' => false,
1084
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1085
            ]);
1086
        }
1087
    }
1088
 
1089
    /**
1090
     * Valores para la generación de los gráficos de progreso
1091
     * Para las repuesta afirmativa
1092
     * [
1093
     *  'success' => true,
1094
     *      'data' => [
1095
     *          'topicTotal' => cantidad total de tópicos,
1096
     *          'topicStarted' => cantidad de tópicos iniciados,
1097
     *          'topicIncompleted' => cantidad de tópicos incompletos,
1098
     *          'topicCompleted' => cantidad de tópicos completos,
1099
     *          'percentCompleted' => % de diapositivas completados ,
1100
     *          'percentIncompleted' => % de diapositivas incompletos ,
1101
     *          'percentWithoutReturning' => % de cápsulas sin retorno después de completada,
1102
     *          'percentWithReturning' => % de cápsulas con retorno después de completada,
1103
     *       ],
1104
     * ]
1105
     *
1106
     *
1107
     *  En caso contrario
1108
     *  [
1109
     *      'success' => false,
1110
     *      'data' => mensaje de error
1111
     *  ]
1112
     *
1113
     *
1114
     * @return \Laminas\View\Model\JsonModel
1115
     */
1116
    public function progressAction()
1117
    {
1118
 
1119
        $request = $this->getRequest();
1120
        if($request->isGet()) {
1121
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1122
            $currentUser = $currentUserPlugin->getUser();
1123
 
1124
 
1125
            $accessGrantedIds = $this->getAccessGranted();
1126
            $id = $this->params()->fromRoute('id');
1127
 
1128
            $companyMapper = CompanyMapper::getInstance($this->adapter);
1129
            $company = $companyMapper->fetchOneByUuid($id);
1130
 
1131
            if(!$company) {
1132
                $response = [
1133
                    'success' => false,
1134
                    'data' => 'ERROR_COMPANY_NOT_FOUND',
1135
                ];
1136
 
1137
 
1138
                return new JsonModel($response);
1139
            }
1140
 
1141
            if(!in_array($company->id, $accessGrantedIds->companies)) {
1142
                $response = [
1143
                    'success' => false,
1144
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_COMPANY',
1145
                ];
1146
 
1147
 
1148
                return new JsonModel($response);
1149
            }
1150
 
1151
            $capsuleTotal              = 0;
1152
            $capsuleCompleted          = 0;
1153
            $capsuleWithReturning      = 0;
1154
            $capsuleWithoutReturning   = 0;
1155
            $capsuleStarted            = 0;
1156
            $capsuleToStart            = 0;
1157
            $percentCompleted          = 0;
1158
            $percentIncompleted        = 100;
1159
 
283 www 1160
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 1161
            $topics = $topicMapper->fetchAllActiveByCompanyIdAndIds($company->id, $accessGrantedIds->topics);
1162
 
302 www 1163
            //$capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
283 www 1164
            $progressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
119 efrain 1165
 
302 www 1166
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
119 efrain 1167
 
302 www 1168
            //$capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
1169
 
1170
 
119 efrain 1171
            foreach($topics as $topic)
1172
            {
302 www 1173
                $resultCount = $topicCapsuleMapper->fetchCountByCompanyIdAndTopicId($company->id, $topic->id);
119 efrain 1174
                $capsuleTotal =  $capsuleTotal + $resultCount;
1175
 
1176
                $resultCount = $progressMapper->fetchCountCapsulesCompletedByIdAndTopicId($currentUser->id, $topic->id);
1177
                $capsuleCompleted = $capsuleCompleted + $resultCount;
1178
 
1179
                $resultCount = $progressMapper->fetchCountCapsulesCompletedWithReturningByIdAndTopicId($currentUser->id, $topic->id);
1180
                $capsuleWithReturning = $capsuleWithReturning + $resultCount;
1181
 
1182
                $resultCount = $progressMapper->fetchCountCapsulesCompletedWithoutReturningByIdAndTopicId($currentUser->id, $topic->id);
1183
                $capsuleWithoutReturning = $capsuleWithoutReturning + $resultCount;
1184
 
1185
                $resultCount = $progressMapper->fetchCountCapsulesCompletedByIdAndTopicId($currentUser->id, $topic->id);
1186
                $capsuleStarted = $capsuleStarted + $resultCount;
1187
            }
1188
 
1189
            $capsuleToStart = $capsuleTotal -  $capsuleStarted;
1190
 
1191
 
1192
            if($capsuleTotal > 0) {
1193
                $percentCompleted = ($capsuleCompleted * 100) /  $capsuleTotal;
1194
                $percentIncompleted = 100 - $percentCompleted;
1195
            }
1196
 
1197
 
1198
 
1199
            return new JsonModel([
1200
                'success' => true,
1201
                'data' => [
1202
                    'capsuleTotal' => $capsuleTotal,
1203
                    'capsuleCompleted' => $capsuleCompleted,
1204
                    'capsuleStarted' => $capsuleStarted,
1205
                    'capsuleToStart' => $capsuleToStart,
1206
                    'percentCompleted' => number_format($percentCompleted, 2, '.', ','),
1207
                    'percentIncompleted' => number_format($percentIncompleted, 2, '.', ','),
1208
                    'capsuleWithReturning' => $capsuleWithReturning,
1209
                    'capsuleWithoutReturning' => $capsuleWithoutReturning,
1210
                ],
1211
            ]);
1212
 
1213
 
1214
        } else {
1215
            return new JsonModel([
1216
                'success' => false,
1217
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1218
            ]);
1219
        }
1220
    }
1221
 
1222
 
1223
    public function topicsAction()
1224
    {
1225
        $request = $this->getRequest();
1226
        if($request->isGet()) {
1227
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1228
            $currentUser = $currentUserPlugin->getUser();
1229
 
1230
 
1231
            $data = [];
1232
            $accessGrantedIds = $this->getAccessGranted();
1233
 
283 www 1234
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1235
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 1236
 
283 www 1237
            $storage = Storage::getInstance($this->config);
1238
            $path = $storage->getPathMicrolearningTopic();
119 efrain 1239
 
1240
            foreach($accessGrantedIds->topics as $id)
1241
            {
1242
                $topic = $topicMapper->fetchOne($id);
1243
                if(!$topic) {
1244
                    continue;
1245
                }
1246
 
1247
                $userProgress = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $id);
1248
                if($userProgress) {
1249
                    $progress = $userProgress->progress;
1250
                    $completed = $userProgress->completed;
1251
                } else {
1252
                    $progress = 0;
1253
                    $completed = 0;
1254
                }
1255
 
1256
 
1257
 
1258
                array_push($data, [
164 efrain 1259
                    'uuid'          => $topic->uuid,
119 efrain 1260
                    'name'          => $topic->name ? $topic->name : '',
1261
                    'description'   => $topic->description ? $topic->description : '',
283 www 1262
                    'image'         => $topic->image ? $storage->getGenericImage($path, $topic->uuid, $topic->image) : '',
119 efrain 1263
                    'progress'      => $progress,
1264
                    'completed'     => $completed,
1265
                    'order'         => $topic->order,
1266
                    'added_on'      => $topic->added_on,
1267
                    'updated_on'    => $topic->updated_on,
1268
                    'link_capsules' => $this->url()->fromRoute('microlearning/capsules', ['topic_id' => $topic->uuid], ['force_canonical' => true]),
161 efrain 1269
                    'link_get'      => $this->url()->fromRoute('microlearning/get-topic', ['id' => $topic->uuid], ['force_canonical' => true]),
119 efrain 1270
 
1271
                ]);
1272
            }
1273
 
1274
            usort($data, function($a, $b) {
1275
 
1276
                $result =  $a['order'] <=> $b['order'];
1277
                if(0 == $result) {
1278
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1279
                    if(0 == $result) {
1280
                        $result = strcasecmp($a['name'], $b['name']);
1281
                    }
1282
                }
1283
 
1284
                if($result < 0) {
1285
                    return 1;
1286
                } else if($result > 0) {
1287
                    return -1;
1288
                } else  {
1289
                    return  0;
1290
                }
1291
            });
1292
 
1293
 
1294
 
1295
                return new JsonModel([
1296
                    'success' => true,
1297
                    'data' => $data
1298
                ]);
1299
 
1300
        } else {
1301
            return new JsonModel([
1302
                'success' => false,
1303
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1304
            ]);
1305
        }
1306
    }
1307
 
161 efrain 1308
    public function getTopicAction()
1309
    {
1310
        $request = $this->getRequest();
1311
        if($request->isGet()) {
1312
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1313
            $currentUser = $currentUserPlugin->getUser();
1314
 
1315
 
1316
            $id = $this->params()->fromRoute('id');
283 www 1317
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
161 efrain 1318
            $topic = $topicMapper->fetchOneByUuid($id);
1319
 
1320
            if(!$topic) {
1321
                return new JsonModel([
1322
                    'success' => false,
1323
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1324
                ]);
1325
            }
1326
 
1327
            $accessGrantedIds = $this->getAccessGranted();
1328
 
1329
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1330
                return new JsonModel([
1331
                    'success' => false,
1332
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1333
                ]);
1334
            }
1335
 
1336
 
1337
            $data = [];
1338
            $accessGrantedIds = $this->getAccessGranted();
1339
 
283 www 1340
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
161 efrain 1341
 
1342
            $userProgress = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $id);
1343
            if($userProgress) {
1344
                $progress = $userProgress->progress;
1345
                $completed = $userProgress->completed;
1346
            } else {
1347
                $progress = 0;
1348
                $completed = 0;
1349
            }
1350
 
283 www 1351
            $storage = Storage::getInstance($this->config);
1352
            $path = $storage->getPathMicrolearningTopic();
161 efrain 1353
 
1354
            $data = [
165 efrain 1355
                'uuid'          => $topic->uuid,
161 efrain 1356
                'name'          => $topic->name ? $topic->name : '',
1357
                'description'   => $topic->description ? $topic->description : '',
283 www 1358
                'image'         => $topic->image ? $storage->getGenericImage($path, $topic->uuid, $topic->image) : '',
161 efrain 1359
                'progress'      => $progress,
1360
                'completed'     => $completed,
1361
                'order'         => $topic->order,
1362
                'added_on'      => $topic->added_on,
1363
                'updated_on'    => $topic->updated_on,
1364
                'link_capsules' => $this->url()->fromRoute('microlearning/capsules', ['topic_id' => $topic->uuid], ['force_canonical' => true]),
1365
 
1366
            ];
1367
 
1368
 
1369
 
1370
            return new JsonModel([
1371
                'success' => true,
1372
                'data' => $data
1373
            ]);
1374
 
1375
        } else {
1376
            return new JsonModel([
1377
                'success' => false,
1378
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1379
            ]);
1380
        }
1381
    }
119 efrain 1382
 
161 efrain 1383
 
119 efrain 1384
    public function capsulesAction()
1385
    {
1386
        $request = $this->getRequest();
1387
        if($request->isGet()) {
1388
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1389
            $currentUser = $currentUserPlugin->getUser();
1390
 
1391
 
1392
 
1393
            $topic_id = $this->params()->fromRoute('topic_id');
283 www 1394
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 1395
            $topic = $topicMapper->fetchOneByUuid($topic_id);
1396
 
1397
            if(!$topic) {
1398
                return new JsonModel([
1399
                    'success' => false,
1400
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1401
                ]);
1402
            }
1403
 
1404
            $accessGrantedIds = $this->getAccessGranted();
1405
 
1406
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1407
                return new JsonModel([
1408
                    'success' => false,
1409
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1410
                ]);
1411
            }
1412
 
1413
 
1414
 
1415
            $data = [];
1416
 
1417
 
283 www 1418
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
302 www 1419
            $topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
283 www 1420
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1421
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
119 efrain 1422
 
283 www 1423
            $storage = Storage::getInstance($this->config);
1424
            $path = $storage->getPathMicrolearningCapsule();
119 efrain 1425
 
302 www 1426
            $capsules = $topicCapsuleMapper->fetchAllActiveByTopicId($topic->id);
119 efrain 1427
            foreach($capsules as $capsule)
1428
            {
1429
 
1430
                if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1431
                    continue;
1432
                }
1433
 
1434
                $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
1435
                if($userProgress) {
1436
                    $progress = $userProgress->progress;
1437
                    $completed = $userProgress->completed;
1438
                } else {
1439
                    $progress = 0;
1440
                    $completed = 0;
1441
                }
1442
 
302 www 1443
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id,  $capsule->id);
119 efrain 1444
 
1445
 
1446
 
1447
                array_push($data, [
166 efrain 1448
                    'uuid'              => $capsule->uuid,
119 efrain 1449
                    'name'              => $capsule->name ? $capsule->name : '',
1450
                    'description'       => $capsule->description ? $capsule->description : '',
283 www 1451
                    'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image)  : '',
119 efrain 1452
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
1453
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
1454
                    'link_slides'       => $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic->uuid,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]),
161 efrain 1455
                    'link_get'          => $this->url()->fromRoute('microlearning/get-capsule', ['id' => $capsule->uuid], ['force_canonical' => true]),
119 efrain 1456
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
1457
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
1458
                    'progress'          => $progress,
1459
                    'completed'         => $completed,
1460
                    'order'             => $capsule->order,
1461
                    'added_on'          => $capsule->added_on,
1462
                    'updated_on'        => $capsule->updated_on,
1463
                ]);
1464
 
1465
 
1466
 
1467
            }
1468
 
1469
            usort($data, function($a, $b) {
1470
 
1471
                $result =  $a['order'] <=> $b['order'];
1472
                if(0 == $result) {
1473
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1474
                    if(0 == $result) {
1475
                        $result = strcasecmp($a['name'], $b['name']);
1476
                    }
1477
                }
1478
 
1479
                if($result < 0) {
1480
                    return 1;
1481
                } else if($result > 0) {
1482
                    return -1;
1483
                } else  {
1484
                    return  0;
1485
                }
1486
            });
1487
 
1488
 
1489
 
1490
                return new JsonModel([
1491
                    'success' => true,
1492
                    'data' => $data
1493
                ]);
1494
 
1495
        } else {
1496
            return new JsonModel([
1497
                'success' => false,
1498
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1499
            ]);
1500
        }
1501
    }
1502
 
235 efrain 1503
    public function takeTestAction()
1504
    {
1505
        $request = $this->getRequest();
1506
        if($request->isPost()) {
1507
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1508
            $currentUser = $currentUserPlugin->getUser();
1509
 
1510
            $slide_id = $this->params()->fromRoute('slide_id');
283 www 1511
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
235 efrain 1512
            $slide = $slideMapper->fetchOneByUuid($slide_id);
1513
 
1514
            if(!$slide) {
1515
                return new JsonModel([
1516
                    'success' => false,
1517
                    'data' => 'ERROR_SLIDE_NOT_FOUND'
1518
                ]);
1519
            }
1520
 
283 www 1521
            if($slide->type != MicrolearningSlide::TYPE_QUIZ) {
235 efrain 1522
                return new JsonModel([
1523
                    'success' => false,
1524
                    'data' => 'ERROR_SLIDE_IS_NOT_QUIZ'
1525
                ]);
1526
            }
1527
 
1528
 
1529
 
283 www 1530
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
235 efrain 1531
            $capsule = $capsuleMapper->fetchOne($slide->capsule_id);
1532
 
1533
            if(!$capsule) {
1534
                return new JsonModel([
1535
                    'success' => false,
1536
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
1537
                ]);
1538
            }
1539
 
1540
            $accessGrantedIds = $this->getAccessGranted();
1541
 
1542
            if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1543
                return new JsonModel([
1544
                    'success' => false,
1545
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1546
                ]);
1547
            }
1548
 
283 www 1549
            $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
235 efrain 1550
            $userLog = $userLogMapper->fetchOneTakeATestByUserIdAndSlideId($currentUser->id, $slide->id);
1551
 
1552
            if($userLog) {
283 www 1553
                $activity = MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST;
235 efrain 1554
            } else {
283 www 1555
                $activity = MicrolearningUserLog::ACTIVITY_TAKE_A_TEST;
235 efrain 1556
            }
1557
 
1558
            $added_on = $userLogMapper->getDatebaseNow();
1559
 
283 www 1560
            $userLog = new MicrolearningUserLog();
235 efrain 1561
            $userLog->activity      = $activity;
1562
            $userLog->user_id       = $currentUser->id;
1563
            $userLog->company_id    = $slide->company_id;
1564
            $userLog->topic_id      = $slide->topic_id;
1565
            $userLog->capsule_id    = $slide->capsule_id;
1566
            $userLog->slide_id      = $slide->id;
1567
            $userLog->added_on      = $added_on;
1568
 
1569
            if($userLogMapper->insert($userLog)) {
1570
                return new JsonModel([
1571
                    'success' => true,
1572
                ]);
1573
            } else {
1574
                return new JsonModel([
1575
                    'success' => false,
1576
                    'data' => $userLogMapper->getError()
1577
                ]);
1578
            }
1579
 
1580
 
1581
 
1582
 
1583
 
1584
 
1585
 
1586
        } else {
1587
            return new JsonModel([
1588
                'success' => false,
1589
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1590
            ]);
1591
        }
1592
    }
1593
 
161 efrain 1594
    public function getCapsuleAction()
1595
    {
1596
        $request = $this->getRequest();
1597
        if($request->isGet()) {
1598
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1599
            $currentUser = $currentUserPlugin->getUser();
1600
 
1601
 
1602
 
1603
            $capsule_id = $this->params()->fromRoute('id');
283 www 1604
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
161 efrain 1605
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
1606
 
1607
            if(!$capsule) {
1608
                return new JsonModel([
1609
                    'success' => false,
1610
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
1611
                ]);
1612
            }
1613
 
1614
            $accessGrantedIds = $this->getAccessGranted();
1615
 
1616
            if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1617
                return new JsonModel([
1618
                    'success' => false,
1619
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1620
                ]);
1621
            }
1622
 
1623
 
1624
 
1625
 
283 www 1626
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
161 efrain 1627
            $topic = $topicMapper->fetchOne($capsule->topic_id);
1628
 
283 www 1629
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1630
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
161 efrain 1631
 
1632
 
1633
 
1634
            $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
1635
            if($userProgress) {
1636
                $progress = $userProgress->progress;
1637
                $completed = $userProgress->completed;
1638
            } else {
1639
                $progress = 0;
1640
                $completed = 0;
1641
            }
1642
 
302 www 1643
            $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id,  $capsule->id);
161 efrain 1644
 
244 efrain 1645
 
283 www 1646
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
244 efrain 1647
            $totalSlides = $slideMapper->fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($capsule->company_id, $capsule->topic_id, $capsule->id);
1648
 
1649
 
1650
            $slide = $slideMapper->fetchFirstByCompanyIdAndTopicIdAndCapsuleId($capsule->company_id, $capsule->topic_id, $capsule->id);
1651
            if($slide) {
1652
 
1653
                $link_first_slide = $this->url()->fromRoute('microlearning/get-slide', ['id' => $slide->uuid], ['force_canonical' => true]);
1654
                $type_first_slide = $slide->type;
161 efrain 1655
 
1656
 
244 efrain 1657
            } else {
1658
                $link_first_slide = '';
1659
                $type_first_slide = '';
1660
            }
1661
 
283 www 1662
            $storage = Storage::getInstance($this->config);
1663
            $path = $storage->getPathMicrolearningCapsule();
244 efrain 1664
 
1665
 
1666
 
161 efrain 1667
            $data = [
164 efrain 1668
                    'uuid'              => $capsule->uuid,
161 efrain 1669
                    'name'              => $capsule->name ? $capsule->name : '',
1670
                    'description'       => $capsule->description ? $capsule->description : '',
283 www 1671
                    'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image)  : '',
161 efrain 1672
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
1673
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
1674
                    'link_slides'       => $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic->uuid,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]),
1675
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
1676
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
1677
                    'progress'          => $progress,
1678
                    'completed'         => $completed,
245 efrain 1679
                    'total_slides'      => $totalSlides,
244 efrain 1680
                    'link_first_slide'  => $link_first_slide,
1681
                    'type_first_slide'  => $type_first_slide,
161 efrain 1682
                    'order'             => $capsule->order,
1683
                    'added_on'          => $capsule->added_on,
1684
                    'updated_on'        => $capsule->updated_on,
1685
            ];
1686
 
1687
 
1688
 
1689
            return new JsonModel([
1690
                'success' => true,
1691
                'data' => $data
1692
            ]);
1693
 
1694
        } else {
1695
            return new JsonModel([
1696
                'success' => false,
1697
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1698
            ]);
1699
        }
1700
    }
1701
 
119 efrain 1702
    public function slidesAction()
1703
    {
1704
        $request = $this->getRequest();
1705
        if($request->isGet()) {
1706
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1707
            $currentUser = $currentUserPlugin->getUser();
1708
 
1709
 
1710
 
1711
            $topic_id = $this->params()->fromRoute('topic_id');
283 www 1712
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 1713
            $topic = $topicMapper->fetchOneByUuid($topic_id);
1714
 
1715
            if(!$topic) {
1716
                return new JsonModel([
1717
                    'success' => false,
1718
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1719
                ]);
1720
            }
1721
 
1722
            $accessGrantedIds = $this->getAccessGranted();
1723
 
1724
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1725
                return new JsonModel([
1726
                    'success' => false,
1727
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1728
                ]);
1729
            }
1730
 
1731
            $capsule_id = $this->params()->fromRoute('capsule_id');
283 www 1732
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
119 efrain 1733
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
1734
 
1735
            if(!$capsule) {
1736
                return new JsonModel([
1737
                    'success' => false,
1738
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
1739
                ]);
1740
            }
1741
 
1742
 
1743
 
1744
            if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1745
                return new JsonModel([
1746
                    'success' => false,
1747
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1748
                ]);
1749
            }
1750
 
1751
 
1752
 
1753
            $data = [];
1754
 
283 www 1755
            $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
119 efrain 1756
 
283 www 1757
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
1758
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
119 efrain 1759
 
283 www 1760
            $storage = Storage::getInstance($this->config);
1761
            $path = $storage->getPathMicrolearningSlide();
119 efrain 1762
 
1763
            $slides = $slideMapper->fetchAllByCompanyIdAndTopicIdAndCapsuleId($capsule->company_id, $capsule->topic_id, $capsule->id);
1764
            foreach($slides as $slide)
1765
            {
1766
 
1767
 
1768
                $userProgress = $userProgressMapper->fetchOneByUserIdAndSlideId($currentUser->id, $slide->id);
1769
                if($userProgress) {
224 efrain 1770
                    $completed =  $userProgress->completed ;
119 efrain 1771
                } else {
1772
                    $completed = 0;
1773
                }
1774
 
235 efrain 1775
 
230 efrain 1776
 
235 efrain 1777
                $link_take_a_test = '';
230 efrain 1778
                if($slide->quiz_id) {
235 efrain 1779
 
283 www 1780
                    $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
230 efrain 1781
                    $quiz = $quizMapper->fetchOne($slide->quiz_id);
1782
                    if($quiz) {
1783
                        $quiz_uuid = $quiz->uuid;
235 efrain 1784
                        $quiz_data = $this->getQuiz($slide->quiz_id);
1785
                        $link_take_a_test = $this->url()->fromRoute('microlearning/take-a-test', ['slide_id' => $slide->uuid], ['force_canonical' => true]);
230 efrain 1786
                    }
235 efrain 1787
 
1788
                }
119 efrain 1789
 
1790
 
1791
                array_push($data, [
235 efrain 1792
                    'quiz'                  => $quiz_uuid,
1793
                    'quiz_data'             => $quiz_data,
1794
                    'uuid'                  => $slide->uuid,
1795
                    'name'                  => $slide->name ? $slide->name : '',
1796
                    'description'           => $slide->description ? $slide->description : '',
1797
                    'type'                  => $slide->type,
283 www 1798
                    'background'            => $slide->background ? $storage->getGenericImage($path, $slide->uuid, $slide->background) : '',
1799
                    'file'                  => $slide->file ? $storage->getGenericFile($path, $slide->uuid, $slide->file) : '',
235 efrain 1800
                    'order'                 => $slide->order,
1801
                    'completed'             => $completed,
1802
                    'link_get'              => $this->url()->fromRoute('microlearning/get-slide', ['id' => $slide->uuid], ['force_canonical' => true]),
1803
                    '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 1804
                    'link_take_a_test'      => $link_take_a_test,
235 efrain 1805
                    'added_on'              => $slide->added_on,
1806
                    'updated_on'            => $slide->updated_on,
119 efrain 1807
                ]);
1808
 
1809
 
1810
 
1811
            }
1812
 
1813
            usort($data, function($a, $b) {
1814
 
1815
                $result =  $a['order'] <=> $b['order'];
1816
                if(0 == $result) {
1817
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1818
                    if(0 == $result) {
1819
                        $result = strcasecmp($a['name'], $b['name']);
1820
                    }
1821
                }
1822
 
1823
                if($result < 0) {
1824
                    return 1;
1825
                } else if($result > 0) {
1826
                    return -1;
1827
                } else  {
1828
                    return  0;
1829
                }
1830
            });
1831
 
1832
 
1833
 
1834
                return new JsonModel([
1835
                    'success' => true,
1836
                    'data' => $data
1837
                ]);
1838
 
1839
        } else {
1840
            return new JsonModel([
1841
                'success' => false,
1842
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1843
            ]);
1844
        }
1845
    }
1846
 
229 efrain 1847
    private function getQuiz($id)
1848
    {
1849
        $serviceDatetimeFormat = $this->config['leaderslinked.services.datetime'];
1850
 
1851
        $data = [];
1852
 
283 www 1853
        $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
229 efrain 1854
        $quiz = $quizMapper->fetchOne($id);
1855
 
1856
        if(!$quiz) {
1857
            return [];
1858
        }
1859
 
1860
        $companyMapper = CompanyMapper::getInstance($this->adapter);
1861
        $company = $companyMapper->fetchOne($quiz->company_id);
1862
 
283 www 1863
        $questionMapper = MicrolearningQuestionMapper::getInstance($this->adapter);
1864
        $answerMapper = MicrolearningAnswerMapper::getInstance($this->adapter);
229 efrain 1865
 
1866
        $record_questions = [];
1867
        $questions = $questionMapper->fetchAllByQuizId($quiz->id);
1868
        foreach($questions as $question)
1869
        {
1870
            $record_answers = [];
1871
 
1872
            $answers = $answerMapper->fetchAllByQuizIdAndQuestionId($question->quiz_id, $question->id);
1873
            foreach($answers as $answer)
1874
            {
1875
                $dtAddedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $answer->added_on);
1876
                $dtUpdatedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $answer->updated_on);
1877
 
1878
                array_push($record_answers, [
1879
                    'uuid' => $answer->uuid,
1880
                    'text' => trim($answer->text),
1881
                    'correct' => $answer->correct ? $answer->correct  : 0 ,
1882
                    'points' => strval(intval($answer->points, 10)),
1883
                    'added_on'  => $dtAddedOn->format($serviceDatetimeFormat),
1884
                    'updated_on'    => $dtUpdatedOn->format($serviceDatetimeFormat),
1885
                ]);
1886
            }
1887
 
1888
            $dtAddedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $question->added_on);
1889
            $dtUpdatedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $question->updated_on);
1890
 
1891
            array_push($record_questions, [
1892
                'uuid'          => $question->uuid,
1893
                'text'          => trim($question->text),
1894
                'type'          => $question->type,
1895
                'maxlength'     => strval($question->maxlength),
1896
                'points'        => strval($question->points),
1897
                'answers'       => $record_answers,
1898
                'added_on'      => $dtAddedOn->format($serviceDatetimeFormat),
1899
                'updated_on'    => $dtUpdatedOn->format($serviceDatetimeFormat),
1900
            ]);
1901
        }
1902
 
1903
        $dtAddedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $quiz->added_on);
1904
        $dtUpdatedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $quiz->updated_on);
1905
 
1906
        array_push($data, [
1907
            'uuid' => $quiz->uuid,
1908
            'name' => $quiz->name,
1909
            'text' => trim($quiz->text ? $quiz->text : ''),
1910
            'failed' => trim($quiz->failed ? $quiz->failed : ''),
1911
            'points' => strval($quiz->points),
1912
            'minimum_points_required' => strval($quiz->minimum_points_required),
1913
            'max_time' => $quiz->max_time ? $quiz->max_time : 5,
1914
            'company_uuid' => $company->uuid,
1915
            'company_name' => $company->name,
1916
            'company_image' => $this->url()->fromRoute('services/storage',['type' => 'company', 'code' => $company->uuid, 'filename' => $company->image], ['force_canonical' => true]),
1917
            'questions'     => $record_questions,
1918
            'added_on'      => $dtAddedOn->format($serviceDatetimeFormat),
1919
            'updated_on'    => $dtUpdatedOn->format($serviceDatetimeFormat),
1920
        ]);
1921
 
1922
        return $data;
1923
 
1924
    }
161 efrain 1925
 
229 efrain 1926
 
161 efrain 1927
    public function getSlideAction()
1928
    {
1929
        $request = $this->getRequest();
1930
        if($request->isGet()) {
1931
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1932
            $currentUser = $currentUserPlugin->getUser();
1933
 
1934
 
1935
 
1936
            $id = $this->params()->fromRoute('id');
283 www 1937
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
161 efrain 1938
            $slide = $slideMapper->fetchOneByUuid($id);
1939
 
1940
            if(!$slide) {
1941
                return new JsonModel([
1942
                    'success' => false,
1943
                    'data' => 'ERROR_SLIDE_NOT_FOUND'
1944
                ]);
1945
            }
1946
 
1947
            $accessGrantedIds = $this->getAccessGranted();
1948
 
1949
 
1950
 
1951
            if(!in_array($slide->capsule_id, $accessGrantedIds->capsules)) {
1952
                return new JsonModel([
1953
                    'success' => false,
1954
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1955
                ]);
1956
            }
1957
 
1958
 
283 www 1959
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
161 efrain 1960
            $topic = $topicMapper->fetchOne($slide->topic_id);
1961
 
283 www 1962
            $capsuleMapper =  MicrolearningCapsuleMapper::getInstance($this->adapter);
161 efrain 1963
            $capsule = $capsuleMapper->fetchOne($slide->capsule_id);
1964
 
1965
 
1966
 
1967
 
1968
 
283 www 1969
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
1970
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
161 efrain 1971
 
1972
 
1973
            $userProgress = $userProgressMapper->fetchOneByUserIdAndSlideId($currentUser->id, $slide->id);
1974
            if($userProgress) {
1975
                $completed = $userProgress->completed;
1976
            } else {
1977
                $completed = 0;
1978
            }
235 efrain 1979
 
1980
            $quiz_uuid = '';
1981
            $quiz_data = [];
1982
            $link_take_a_test = '';
1983
            if($slide->quiz_id) {
1984
 
283 www 1985
                $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
235 efrain 1986
                $quiz = $quizMapper->fetchOne($slide->quiz_id);
1987
                if($quiz) {
1988
                    $quiz_uuid = $quiz->uuid;
1989
                    $quiz_data = $this->getQuiz($slide->quiz_id);
1990
                    $link_take_a_test = $this->url()->fromRoute('microlearning/take-a-test', ['slide_id' => $slide->uuid], ['force_canonical' => true]);
1991
                }
161 efrain 1992
 
235 efrain 1993
            }
161 efrain 1994
 
283 www 1995
            $storage = Storage::getInstance($this->config);
1996
            $path = $storage->getPathMicrolearningSlide();
161 efrain 1997
 
1998
            $data =[
235 efrain 1999
                'quiz'              => $quiz_uuid,
2000
                'quiz_data'         => $quiz_data,
2001
                'uuid'              => $slide->uuid,
2002
                'name'              => $slide->name ? $slide->name : '',
2003
                'description'       => $slide->description ? $slide->description : '',
2004
                'type'              => $slide->type,
283 www 2005
                'background'        => $slide->background ? $storage->getGenericImage($path, $slide->uuid, $slide->background) : '',
2006
                'file'              => $slide->file ? $storage->getGenericFile($path, $slide->uuid, $slide->file) : '',
235 efrain 2007
                'order'             => $slide->order,
2008
                'completed'         => $completed,
2009
                '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]),
2010
                'link_take_a_test'  => $link_take_a_test,
2011
                'added_on'          => $slide->added_on,
2012
                'updated_on'        => $slide->updated_on,
161 efrain 2013
            ];
2014
 
2015
 
2016
 
2017
 
2018
            return new JsonModel([
2019
                'success' => true,
2020
                'data' => $data
2021
            ]);
2022
 
2023
        } else {
2024
            return new JsonModel([
2025
                'success' => false,
2026
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
2027
            ]);
2028
        }
2029
    }
2030
 
119 efrain 2031
    public function profileAction()
2032
    {
2033
        $request = $this->getRequest();
2034
        if($request->isGet()) {
2035
            $currentUserPlugin = $this->plugin('currentUserPlugin');
2036
            $currentUser = $currentUserPlugin->getUser();
2037
 
2038
 
2039
            $accessGrantedIds = $this->getAccessGranted();
2040
 
2041
            $companyMapper = CompanyMapper::getInstance($this->adapter);
283 www 2042
            $companyExtendUserMapper = MicrolearningExtendUserMapper::getInstance($this->adapter);
2043
            $companyExtendUserCompanyMapper = MicrolearningExtendUserCompanyMapper::getInstance($this->adapter);
2044
            $companyExtendUserFunctionMapper = MicrolearningExtendUserFunctionMapper::getInstance($this->adapter);
2045
            $companyExtendUserGroupMapper = MicrolearningExtendUserGroupMapper::getInstance($this->adapter);
2046
            $companyExtendUserInstitutionMapper = MicrolearningExtendUserInstitutionMapper::getInstance($this->adapter);
2047
            $companyExtendUserPartnerMapper = MicrolearningExtendUserPartnerMapper::getInstance($this->adapter);
2048
            $companyExtendUserProgramMapper = MicrolearningExtendUserProgramMapper::getInstance($this->adapter);
2049
            $companyExtendUserStudentTypeMapper = MicrolearningExtendUserStudentTypeMapper::getInstance($this->adapter);
2050
            $companyExtendUserSectorMapper = MicrolearningExtendUserSectorMapper::getInstance($this->adapter);
119 efrain 2051
 
283 www 2052
            $storage = Storage::getInstance($this->config);
2053
 
119 efrain 2054
 
2055
            $data = [];
2056
            foreach($accessGrantedIds->companies as $company_id)
2057
            {
2058
                $company = $companyMapper->fetchOne($company_id);
2059
                if(!$company) {
2060
                    continue;
2061
                }
2062
 
2063
                $record = [
2064
                    'name' => $company->name,
283 www 2065
                    'image' => $storage->getCompanyImage($company),
119 efrain 2066
                    'details' => [],
2067
                ];
2068
 
2069
                $companyExtendUser = $companyExtendUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
2070
                if(!$companyExtendUser) {
2071
                    continue;
2072
                }
2073
 
2074
                if($companyExtendUser->extend_company_id) {
2075
 
2076
                    $extendedCompany = $companyExtendUserCompanyMapper->fetchOne($companyExtendUser->company_id);
2077
                    if($extendedCompany) {
2078
                        array_push($record['details'],[
2079
                            'uuid' => $extendedCompany->uuid,
2080
                            'label' => 'LABEL_COMPANY',
2081
                            'value' => $extendedCompany->name
2082
                        ]);
2083
                    }
2084
                }
2085
 
2086
                if($companyExtendUser->extend_function_id) {
2087
                    $extendedFunction = $companyExtendUserFunctionMapper->fetchOne($companyExtendUser->extend_function_id);
2088
                    if($extendedFunction) {
2089
                        array_push($record['details'],[
2090
                            'uuid' => $extendedFunction->uuid,
2091
                            'label' => 'LABEL_FUNCTION',
2092
                            'value' => $extendedFunction->name
2093
                        ]);
2094
                    }
2095
                }
2096
 
2097
                if($companyExtendUser->extend_group_id) {
2098
                    $extendedGroup = $companyExtendUserGroupMapper->fetchOne($companyExtendUser->extend_group_id);
2099
                    if($extendedGroup) {
2100
                        array_push($record['details'],[
2101
                            'uuid' => $extendedGroup->uuid,
2102
                            'label' => 'LABEL_GROUP',
2103
                            'value' => $extendedGroup->name
2104
                        ]);
2105
                    }
2106
                }
2107
 
2108
                if($companyExtendUser->extend_institution_id) {
2109
                    $extendedInstitution= $companyExtendUserInstitutionMapper->fetchOne($companyExtendUser->extend_institution_id);
2110
                    if($extendedInstitution) {
2111
                        array_push($record['details'],[
2112
                            'uuid' => $extendedInstitution->uuid,
2113
                            'label' => 'LABEL_INSTITUTION',
2114
                            'value' => $extendedInstitution->name
2115
                        ]);
2116
                    }
2117
                }
2118
 
2119
                if($companyExtendUser->extend_program_id) {
2120
                    $extendedProgram = $companyExtendUserProgramMapper->fetchOne($companyExtendUser->extend_program_id);
2121
                    if($extendedProgram) {
2122
                        array_push($record['details'],[
2123
                            'uuid' => $extendedProgram->uuid,
2124
                            'label' => 'LABEL_PROGRAM',
2125
                            'value' => $extendedProgram->name
2126
                        ]);
2127
 
2128
                    }
2129
                }
2130
 
2131
                if($companyExtendUser->extend_sector_id) {
2132
                    $extendedSector = $companyExtendUserSectorMapper->fetchOne($companyExtendUser->extend_sector_id);
2133
                    if($extendedSector) {
2134
                        array_push($record['details'],[
2135
                            'uuid' => $extendedSector->uuid,
2136
                            'label' => 'LABEL_SECTOR',
2137
                            'value' => $extendedSector->name
2138
                        ]);
2139
                    }
2140
                }
2141
 
2142
                if($companyExtendUser->extend_partner_id) {
2143
                    $extendedPartner = $companyExtendUserPartnerMapper->fetchOne($companyExtendUser->extend_partner_id);
2144
                    if($extendedPartner) {
2145
                        array_push($record['details'],[
2146
                            'uuid' => $extendedPartner->uuid,
2147
                            'label' => 'LABEL_PARTNER',
2148
                            'value' => $extendedPartner->name
2149
                        ]);
2150
                    }
2151
                }
2152
 
2153
                if($companyExtendUser->extend_student_type_id) {
2154
                    $extendedStudentType = $companyExtendUserStudentTypeMapper->fetchOne($companyExtendUser->extend_student_type_id);
2155
                    if($extendedStudentType) {
2156
                        array_push($record['details'],[
2157
                            'uuid' => $extendedStudentType->uuid,
2158
                            'label' => 'LABEL_TYPE',
2159
                            'value' => $extendedStudentType->name
2160
                        ]);
2161
                    }
2162
                }
2163
 
2164
                array_push($data, $record);
2165
            }
2166
 
2167
            return new JsonModel([
2168
                'success' => true,
2169
                'data' => $data
2170
            ]);
2171
 
2172
        } else {
2173
            return new JsonModel([
2174
                'success' => false,
2175
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
2176
            ]);
2177
        }
2178
    }
2179
 
167 efrain 2180
    public function syncAction()
2181
    {
2182
        $request = $this->getRequest();
168 efrain 2183
        if($request->isPost()) {
167 efrain 2184
            $currentUserPlugin = $this->plugin('currentUserPlugin');
2185
            $currentUser = $currentUserPlugin->getUser();
2186
 
2187
 
2188
            $operation = $this->params()->fromRoute('operation');
2189
            if($operation == 'slide-view' || $operation == 'capsule-close' || $operation == 'topic-close') {
2190
                $accessGrantedIds = $this->getAccessGranted();
2191
 
2192
                $topic_uuid     = $this->params()->fromRoute('topic_uuid');
2193
                $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
2194
                $slide_uuid     = $this->params()->fromRoute('slide_uuid');
2195
 
2196
                $accessGrantedIds = $this->getAccessGranted();
2197
                if($operation == 'slide-view') {
2198
 
2199
                    if(empty($slide_uuid) || empty($capsule_uuid ) || empty($topic_uuid)) {
2200
                        return new JsonModel([
2201
                            'success' => false,
2202
                            'data' => 'ERROR_INVALID_PARAMETERS'
2203
                        ]);
2204
                    }
2205
 
2206
 
283 www 2207
                    $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
167 efrain 2208
                    $topic = $topicMapper->fetchOneByUuid($topic_uuid);
2209
 
2210
                    if(!$topic) {
2211
                        return new JsonModel([
2212
                            'success' => false,
2213
                            'data' => 'ERROR_TOPIC_NOT_FOUND'
2214
                        ]);
2215
                    }
2216
 
2217
 
2218
 
2219
                    if(!in_array($topic->id, $accessGrantedIds->topics)) {
2220
                        return new JsonModel([
2221
                            'success' => false,
2222
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
2223
                        ]);
2224
                    }
2225
 
283 www 2226
                    $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
167 efrain 2227
                    $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
2228
 
2229
                    if(!$capsule) {
2230
                        return new JsonModel([
2231
                            'success' => false,
2232
                            'data' => 'ERROR_CAPSULE_NOT_FOUND'
2233
                        ]);
2234
                    }
119 efrain 2235
 
167 efrain 2236
                    if(!in_array($capsule->id, $accessGrantedIds->capsules) || $capsule->topic_id != $topic->id) {
2237
                        return new JsonModel([
2238
                            'success' => false,
2239
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
2240
                        ]);
2241
                    }
2242
 
283 www 2243
                    $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
167 efrain 2244
                    $slide = $slideMapper->fetchOneByUuid($slide_uuid);
2245
                    if(!$slide) {
2246
                        return new JsonModel([
2247
                            'success' => false,
2248
                            'data' => 'ERROR_SLIDE_NOT_FOUND'
2249
                        ]);
2250
                    }
2251
 
2252
                    if($slide->capsule_id != $capsule->id && $slide->topic_id != $topic->id) {
2253
                        return new JsonModel([
2254
                            'success' => false,
2255
                            'data' => 'ERROR_SLIDE_NOT_FOUND'
2256
                        ]);
2257
 
2258
                    }
2259
 
2260
 
283 www 2261
                    $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
167 efrain 2262
 
283 www 2263
                    $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
167 efrain 2264
                    $added_on = $userProgressMapper->getDatebaseNow();
2265
 
2266
 
2267
                    $userProgressTopic = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic->id);
2268
                    if(!$userProgressTopic) {
2269
 
283 www 2270
                        $userProgressTopic = new MicrolearningUserProgress();
167 efrain 2271
                        $userProgressTopic->company_id                  = $slide->company_id;
2272
                        $userProgressTopic->topic_id                    = $slide->topic_id;
176 efrain 2273
                        $userProgressTopic->user_id                     = $currentUser->id;
167 efrain 2274
                        $userProgressTopic->progress                    = 0;
2275
                        $userProgressTopic->returning                   = 0;
2276
                        $userProgressTopic->returning_after_completed   = 0;
2277
                        $userProgressTopic->completed                   = 0;
2278
                        $userProgressTopic->total_slides                = $slideMapper->fetchTotalCountByCompanyIdAndTopicId($slide->company_id, $slide->topic_id);
2279
                        $userProgressTopic->view_slides                 = 0;
283 www 2280
                        $userProgressTopic->type                        = MicrolearningUserProgress::TYPE_TOPIC;
167 efrain 2281
                        $userProgressTopic->added_on                    = $added_on;
2282
                        $userProgressTopic->updated_on                  = $added_on;
2283
 
2284
                        if($userProgressMapper->insert($userProgressTopic)) {
2285
 
283 www 2286
                            $userLog = new MicrolearningUserLog();
2287
                            $userLog->activity      = MicrolearningUserLog::ACTIVITY_START_TOPIC;
167 efrain 2288
                            $userLog->user_id       = $currentUser->id;
2289
                            $userLog->company_id    = $slide->company_id;
2290
                            $userLog->topic_id      = $slide->topic_id;
2291
                            $userLog->added_on      = $added_on;
2292
 
2293
                            if(!$userLogMapper->insert($userLog)) {
2294
                                return new JsonModel([
2295
                                    'success' => false,
2296
                                    'data' => $userProgressMapper->getError()
2297
                                ]);
2298
                            }
2299
                        } else {
2300
                            return new JsonModel([
2301
                                'success' => false,
2302
                                'data' => $userProgressMapper->getError()
2303
                            ]);
2304
                        }
2305
                    }
2306
 
2307
 
2308
                    $userProgressCapsule = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
2309
                    if(!$userProgressCapsule) {
2310
 
283 www 2311
                        $userProgressCapsule = new MicrolearningUserProgress();
167 efrain 2312
                        $userProgressCapsule->company_id                  = $slide->company_id;
2313
                        $userProgressCapsule->topic_id                    = $slide->topic_id;
2314
                        $userProgressCapsule->capsule_id                  = $slide->capsule_id;
176 efrain 2315
                        $userProgressCapsule->user_id                     = $currentUser->id;
167 efrain 2316
                        $userProgressCapsule->progress                    = 0;
2317
                        $userProgressCapsule->returning                   = 0;
2318
                        $userProgressCapsule->returning_after_completed   = 0;
2319
                        $userProgressCapsule->completed                   = 0;
2320
                        $userProgressCapsule->total_slides                = $slideMapper->fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($slide->company_id, $slide->topic_id, $slide->capsule_id);
2321
                        $userProgressCapsule->view_slides                 = 0;
283 www 2322
                        $userProgressCapsule->type                        = MicrolearningUserProgress::TYPE_CAPSULE;
167 efrain 2323
                        $userProgressCapsule->added_on                    = $added_on;
2324
                        $userProgressTopic->updated_on                    = $added_on;
2325
 
2326
                        if($userProgressMapper->insert($userProgressCapsule)) {
2327
 
283 www 2328
                            $userLog = new MicrolearningUserLog();
2329
                            $userLog->activity      = MicrolearningUserLog::ACTIVITY_START_CAPSULE;
167 efrain 2330
                            $userLog->user_id       = $currentUser->id;
2331
                            $userLog->company_id    = $slide->company_id;
2332
                            $userLog->topic_id      = $slide->topic_id;
2333
                            $userLog->capsule_id    = $slide->capsule_id;
2334
                            $userLog->added_on      = $added_on;
2335
 
2336
                            if(!$userLogMapper->insert($userLog)) {
2337
                                return new JsonModel([
2338
                                    'success' => false,
2339
                                    'data' => $userLogMapper->getError()
2340
                                ]);
2341
                            }
2342
                        } else {
2343
                            return new JsonModel([
2344
                                'success' => false,
2345
                                'data' => $userProgressMapper->getError()
2346
                            ]);
2347
                        }
2348
                    }
2349
 
2350
 
2351
                    $userProgressSlide = $userProgressMapper->fetchOneByUserIdAndSlideId($currentUser->id, $slide->id);
2352
                    if(!$userProgressSlide) {
283 www 2353
                        $userProgressSlide = new MicrolearningUserProgress();
167 efrain 2354
                        $userProgressSlide->company_id                  = $slide->company_id;
2355
                        $userProgressSlide->topic_id                    = $slide->topic_id;
2356
                        $userProgressSlide->capsule_id                  = $slide->capsule_id;
2357
                        $userProgressSlide->slide_id                    = $slide->id;
176 efrain 2358
                        $userProgressSlide->user_id                     = $currentUser->id;
167 efrain 2359
                        $userProgressSlide->progress                    = 0;
2360
                        $userProgressSlide->returning                   = 0;
2361
                        $userProgressSlide->returning_after_completed   = 0;
2362
                        $userProgressSlide->completed                   = 1;
2363
                        $userProgressSlide->total_slides                = 0;
2364
                        $userProgressSlide->view_slides                 = 0;
283 www 2365
                        $userProgressSlide->type                        = MicrolearningUserProgress::TYPE_SLIDE;
167 efrain 2366
                        $userProgressSlide->added_on                    = $added_on;
2367
                        $userProgressSlide->updated_on                  = $added_on;
2368
 
2369
                        if(!$userProgressMapper->insert($userProgressSlide)) {
2370
                            return new JsonModel([
2371
                                'success' => false,
2372
                                'data' => $userProgressMapper->getError()
2373
                            ]);
2374
                        }
2375
                    }
2376
 
283 www 2377
                    $userLog = new MicrolearningUserLog();
2378
                    $userLog->activity      = MicrolearningUserLog::ACTIVITY_VIEW_SLIDE;
167 efrain 2379
                    $userLog->user_id       = $currentUser->id;
2380
                    $userLog->company_id    = $slide->company_id;
2381
                    $userLog->topic_id      = $slide->topic_id;
2382
                    $userLog->capsule_id    = $slide->capsule_id;
2383
                    $userLog->slide_id      = $slide->id;
2384
                    $userLog->added_on      = $added_on;
2385
 
2386
                    if(!$userLogMapper->insert($userLog)) {
2387
                        return new JsonModel([
2388
                            'success' => false,
2389
                            'data' => $userLogMapper->getError()
2390
                        ]);
2391
                    }
2392
 
2393
                    $closeCapsule = false;
2394
 
2395
                    if($userProgressCapsule->completed) {
2396
 
2397
                        $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $added_on);
2398
                        $returning_on = $dt->format('Y-m-d');
2399
 
2400
                        if(!$userProgressCapsule->returning_on || $userProgressCapsule->returning_on != $returning_on) {
2401
 
2402
                            $userProgressCapsule->returning_on = $returning_on;
2403
                            $userProgressCapsule->returning_after_completed = $userProgressCapsule->returning_after_completed + 1;
2404
 
2405
                            if(!$userProgressMapper->update($userProgressCapsule)) {
2406
                                return new JsonModel([
2407
                                    'success' => false,
2408
                                    'data' => $userProgressMapper->getError()
2409
                                ]);
2410
                            }
2411
                        }
2412
 
2413
                    }  else {
2414
 
2415
                        $userProgressCapsule->total_slides = $slideMapper->fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($slide->company_id, $slide->topic_id, $slide->capsule_id);
2416
                        $userProgressCapsule->view_slides = $userProgressMapper->fetchCountAllSlideCompletedByUserIdAndCapsuleId($currentUser->id, $slide->capsule_id);
2417
 
2418
                        if($userProgressCapsule->total_slides) {
2419
 
2420
                            $userProgressCapsule->progress = ($userProgressCapsule->view_slides * 100) / $userProgressCapsule->total_slides;
2421
                            $userProgressCapsule->progress = $userProgressCapsule->progress > 100 ? 100 : $userProgressCapsule->progress;
2422
                        }
2423
 
2424
                        if(!$userProgressMapper->update($userProgressCapsule)) {
2425
                            return new JsonModel([
2426
                                'success' => false,
2427
                                'data' => $userProgressMapper->getError()
2428
                            ]);
2429
                        }
2430
 
2431
                        if($userProgressCapsule->progress >= 100) {
2432
                            $closeCapsule = true;
2433
                        }
171 efrain 2434
 
283 www 2435
 
167 efrain 2436
                    }
2437
 
2438
                    $closeTopic = false;
2439
                    if(!$userProgressTopic->completed) {
2440
 
2441
 
2442
                        $userProgressTopic->total_slides = $slideMapper->fetchTotalCountByCompanyIdAndTopicId($slide->company_id, $slide->topic_id);
2443
                        $userProgressTopic->view_slides = $userProgressMapper->fetchCountAllSlideCompletedByUserIdAndTopicId($currentUser->id, $slide->topic_id);
2444
 
2445
                        if($userProgressTopic->total_slides) {
2446
 
2447
                            $userProgressTopic->progress = ($userProgressTopic->view_slides * 100) / $userProgressTopic->total_slides;
2448
                            $userProgressTopic->progress = $userProgressTopic->progress > 100 ? 100 : $userProgressTopic->progress;
2449
                        }
2450
                        if(!$userProgressMapper->update($userProgressTopic)) {
2451
                            return new JsonModel([
2452
                                'success' => false,
2453
                                'data' => $userProgressMapper->getError()
2454
                            ]);
2455
                        }
2456
 
2457
                        if($userProgressTopic->progress >= 100) {
2458
                            $closeTopic = true;
2459
                        }
2460
                    }
2461
 
2462
 
2463
                    $data = [
2464
                        'message' => 'LABEL_THE_USER_PROGRESS_FOR_THIS_SLIDE_HAS_BEEN_COMPLETED',
2465
 
2466
                    ];
2467
 
2468
                    if($closeCapsule) {
173 efrain 2469
                        $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 2470
                    }
2471
 
2472
 
2473
                    if($closeTopic) {
173 efrain 2474
                        $data['link_close_topic'] = $this->url()->fromRoute('microlearning/sync', ['operation' => 'topic-close', 'topic_uuid' => $topic->uuid], ['force_canonical' => true]);
167 efrain 2475
                    }
2476
 
2477
 
2478
                    return new JsonModel([
2479
                        'success' => true,
2480
                        'data' => $data
2481
                    ]);
2482
 
2483
 
2484
 
2485
 
2486
                } else if($operation == 'capsule-close') {
2487
 
2488
                    if(empty($capsule_uuid ) || empty($topic_uuid)) {
2489
                        return new JsonModel([
2490
                            'success' => false,
2491
                            'data' => 'ERROR_INVALID_PARAMETERS'
2492
                        ]);
2493
                    }
2494
 
2495
 
283 www 2496
                    $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
167 efrain 2497
                    $topic = $topicMapper->fetchOneByUuid($topic_uuid);
2498
 
2499
                    if(!$topic) {
2500
                        return new JsonModel([
2501
                            'success' => false,
2502
                            'data' => 'ERROR_TOPIC_NOT_FOUND'
2503
                        ]);
2504
                    }
2505
 
2506
 
2507
 
2508
                    if(!in_array($topic->id, $accessGrantedIds->topics)) {
2509
                        return new JsonModel([
2510
                            'success' => false,
2511
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
2512
                        ]);
2513
                    }
2514
 
283 www 2515
                    $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
167 efrain 2516
                    $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
2517
 
2518
                    if(!$capsule) {
2519
                        return new JsonModel([
2520
                            'success' => false,
2521
                            'data' => 'ERROR_CAPSULE_NOT_FOUND'
2522
                        ]);
2523
                    }
2524
 
2525
                    if(!in_array($capsule->id, $accessGrantedIds->capsules) || $capsule->topic_id != $topic->id) {
2526
                        return new JsonModel([
2527
                            'success' => false,
2528
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
2529
                        ]);
2530
                    }
2531
 
2532
 
283 www 2533
                    $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
167 efrain 2534
 
283 www 2535
                    $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
167 efrain 2536
                    $updated_on = $userProgressMapper->getDatebaseNow();
2537
 
2538
                    $userProgressCapsule = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
2539
                    if(!$userProgressCapsule) {
2540
                        return new JsonModel([
2541
                            'success' => false,
2542
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_CAPSULE_NOT_FOUND'
2543
                        ]);
2544
                    }
2545
 
2546
                    if($userProgressCapsule->completed) {
2547
                        return new JsonModel([
2548
                            'success' => false,
2549
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_CAPSULE_ALREADY_CLOSED'
2550
                        ]);
2551
                    }
2552
 
2553
 
2554
                    $userProgressCapsule->completed = 1;
2555
                    $userProgressCapsule->updated_on = $updated_on;
2556
 
2557
                    if($userProgressMapper->update($userProgressCapsule)) {
225 efrain 2558
 
2559
 
283 www 2560
                        $userLog = new MicrolearningUserLog();
2561
                        $userLog->activity      = MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE;
227 efrain 2562
                        $userLog->user_id       = $currentUser->id;
2563
                        $userLog->company_id    = $capsule->company_id;
2564
                        $userLog->topic_id      = $capsule->topic_id;
2565
                        $userLog->capsule_id    = $capsule->id;
2566
                        $userLog->added_on      = $updated_on;
225 efrain 2567
 
2568
                        if(!$userLogMapper->insert($userLog)) {
2569
                            return new JsonModel([
2570
                                'success' => false,
2571
                                'data' => $userProgressMapper->getError()
2572
                            ]);
2573
                        }
2574
 
2575
 
167 efrain 2576
                        return new JsonModel([
2577
                            'success' => true,
2578
                            'data' => 'LABEL_THE_USER_PROGRESS_FOR_THIS_CAPSULE_HAS_BEEN_COMPLETED'
2579
                        ]);
2580
                    } else {
2581
                        return new JsonModel([
2582
                            'success' => false,
2583
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_CAPSULE_COULD_NOT_BE_COMPLETED'
2584
                        ]);
2585
                    }
2586
 
2587
 
2588
 
2589
 
2590
 
2591
 
2592
 
2593
                } else if($operation == 'topic-close') {
174 efrain 2594
                    if(empty($topic_uuid)) {
167 efrain 2595
                        return new JsonModel([
2596
                            'success' => false,
2597
                            'data' => 'ERROR_INVALID_PARAMETERS'
2598
                        ]);
2599
                    }
2600
 
2601
 
283 www 2602
                    $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
167 efrain 2603
                    $topic = $topicMapper->fetchOneByUuid($topic_uuid);
2604
 
2605
                    if(!$topic) {
2606
                        return new JsonModel([
2607
                            'success' => false,
2608
                            'data' => 'ERROR_TOPIC_NOT_FOUND'
2609
                        ]);
2610
                    }
2611
 
2612
 
2613
 
2614
                    if(!in_array($topic->id, $accessGrantedIds->topics)) {
2615
                        return new JsonModel([
2616
                            'success' => false,
2617
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
2618
                        ]);
2619
                    }
2620
 
2621
 
2622
 
2623
 
283 www 2624
                    $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
167 efrain 2625
 
283 www 2626
                    $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
167 efrain 2627
                    $updated_on = $userProgressMapper->getDatebaseNow();
2628
 
2629
                    $userProgressTopic = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic->id);
2630
                    if(!$userProgressTopic) {
2631
                        return new JsonModel([
2632
                            'success' => false,
2633
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_TOPIC_NOT_FOUND'
2634
                        ]);
2635
                    }
2636
 
2637
                    if($userProgressTopic->completed) {
2638
                        return new JsonModel([
2639
                            'success' => false,
2640
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_TOPIC_ALREADY_CLOSED'
2641
                        ]);
2642
                    }
2643
 
2644
 
2645
                    $userProgressTopic->completed = 1;
2646
                    $userProgressTopic->updated_on = $updated_on;
2647
 
2648
                    if($userProgressMapper->update($userProgressTopic)) {
226 efrain 2649
 
283 www 2650
                        $userLog = new MicrolearningUserLog();
2651
                        $userLog->activity      = MicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC;
227 efrain 2652
                        $userLog->user_id       = $currentUser->id;
2653
                        $userLog->company_id    = $topic->company_id;
2654
                        $userLog->topic_id      = $topic->id;
2655
                        $userLog->added_on      = $updated_on;
226 efrain 2656
 
2657
                        if(!$userLogMapper->insert($userLog)) {
2658
                            return new JsonModel([
2659
                                'success' => false,
2660
                                'data' => $userProgressMapper->getError()
2661
                            ]);
2662
                        }
2663
 
167 efrain 2664
                        return new JsonModel([
2665
                            'success' => true,
2666
                            'data' => 'LABEL_THE_USER_PROGRESS_FOR_THIS_TOPIC_HAS_BEEN_COMPLETED'
2667
                        ]);
2668
                    } else {
2669
                        return new JsonModel([
2670
                            'success' => false,
2671
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_TOPIC_COULD_NOT_BE_COMPLETED'
2672
                        ]);
2673
                    }
2674
                }
2675
 
2676
 
2677
 
2678
 
2679
            } else {
2680
 
2681
                return new JsonModel([
2682
                    'success' => false,
2683
                    'data' => 'ERROR_OPERATION_UNKNOWN'
2684
                ]);
2685
 
2686
 
2687
            }
2688
 
2689
        } else {
2690
            return new JsonModel([
2691
                'success' => false,
2692
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
2693
            ]);
2694
        }
2695
    }
2696
 
2697
 
302 www 2698
 
2699
 
2700
 
119 efrain 2701
    /**
2702
     *
2703
     * @return \LeadersLinked\Controller\MicrolearningUserAccessGrantedIds
2704
     */
2705
    private function getAccessGranted()
2706
    {
2707
        $currentUserPlugin = $this->plugin('currentUserPlugin');
2708
        $currentUser = $currentUserPlugin->getUser();
2709
 
283 www 2710
        $capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
119 efrain 2711
        $now = $capsuleUserMapper->getDatebaseNow();
2712
 
2713
        $records = $capsuleUserMapper->fetchAllActiveByUserId($currentUser->id);
2714
 
2715
        $accessGrantedIds = new MicrolearningUserAccessGrantedIds();
2716
 
2717
 
2718
        foreach($records as $record)
2719
        {
283 www 2720
            if($record->access != MicrolearningCapsuleUser::ACCESS_UNLIMITED && $record->access != MicrolearningCapsuleUser::ACCESS_PAY_PERIOD) {
119 efrain 2721
                continue;
2722
            }
283 www 2723
            if($record->access == MicrolearningCapsuleUser::ACCESS_PAY_PERIOD) {
119 efrain 2724
                if($now < $record->paid_from || $now > $record->paid_to) {
2725
                    continue;
2726
                }
2727
            }
2728
 
2729
 
2730
            if(!in_array($record->company_id, $accessGrantedIds->companies )) {
2731
                array_push($accessGrantedIds->companies, $record->company_id);
2732
            }
2733
 
2734
            if(!in_array($record->topic_id, $accessGrantedIds->topics )) {
2735
                array_push( $accessGrantedIds->topics, $record->topic_id);
2736
            }
2737
 
2738
            if(!in_array($record->capsule_id, $accessGrantedIds->capsules)) {
2739
                array_push( $accessGrantedIds->capsules, $record->capsule_id);
2740
            }
2741
        }
2742
 
2743
        return $accessGrantedIds;
2744
    }
2745
 
2746
 
2747
}