Proyectos de Subversion LeadersLinked - Services

Rev

Rev 283 | Rev 302 | 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;
119 efrain 40
 
41
 
42
class MicrolearningUserAccessGrantedIds
43
{
44
    public $companies;
45
    public $topics;
46
    public $capsules;
47
 
48
 
49
    public function __construct()
50
    {
51
        $this->companies    = [];
52
        $this->topics       = [];
53
        $this->capsules     = [];
54
    }
55
}
56
 
57
class MicrolearningController extends AbstractActionController
58
{
59
 
60
    /**
61
     *
62
     * @var \Laminas\Db\Adapter\AdapterInterface
63
     */
64
    private $adapter;
65
 
66
    /**
67
     *
68
     * @var \LeadersLinked\Cache\CacheInterface
69
     */
70
    private $cache;
71
 
72
 
73
    /**
74
     *
75
     * @var \Laminas\Log\LoggerInterface
76
     */
77
    private $logger;
78
 
79
    /**
80
     *
81
     * @var array
82
     */
83
    private $config;
84
 
85
 
86
    /**
87
     *
88
     * @var \Laminas\Mvc\I18n\Translator
89
     */
90
    private $translator;
91
 
92
 
93
    /**
94
     *
95
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
96
     * @param \LeadersLinked\Cache\CacheInterface $cache
97
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
98
     * @param array $config
99
     * @param \Laminas\Mvc\I18n\Translator $translator
100
     */
101
    public function __construct($adapter, $cache, $logger, $config, $translator)
102
    {
103
        $this->adapter      = $adapter;
104
        $this->cache        = $cache;
105
        $this->logger       = $logger;
106
        $this->config       = $config;
107
        $this->translator   = $translator;
108
    }
109
 
110
    public function indexAction()
111
    {
112
        $request = $this->getRequest();
113
 
114
        if($request->isGet()) {
115
            return new JsonModel([
116
                'success' => true,
117
                'data' =>  [
118
                    'link_companies' => $this->url()->fromRoute('microlearning/companies',[], ['force_canonical' => true]),
119
                    'link_timeline' => $this->url()->fromRoute('microlearning/timeline',[], ['force_canonical' => true]),
120
                    'link_last_capsule_in_progress' => $this->url()->fromRoute('microlearning/last-capsule-in-progress',[], ['force_canonical' => true]),
121
                    'link_profile' => $this->url()->fromRoute('microlearning/profile',[], ['force_canonical' => true]),
122
                    'link_topics' => $this->url()->fromRoute('microlearning/topics',[], ['force_canonical' => true]),
123
                    'link_capsules_pending' => $this->url()->fromRoute('microlearning/capsules-pending',[], ['force_canonical' => true]),
124
                    'link_capsules_completed' => $this->url()->fromRoute('microlearning/capsules-completed',[], ['force_canonical' => true]),
125
                    'link_capsules_in_progress' => $this->url()->fromRoute('microlearning/capsules-in-progress',[], ['force_canonical' => true]),
126
 
127
 
128
                ]
129
            ]);
130
        }
131
 
132
        return new JsonModel([
133
            'success' => false,
134
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
135
        ]);
136
    }
137
 
138
    public function companiesAction()
139
    {
140
 
141
        $request = $this->getRequest();
142
 
143
        if($request->isGet()) {
144
            $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
145
            $currentNetwork = $currentNetworkPlugin->getNetwork();
146
 
147
 
148
            $accessGrantedIds = $this->getAccessGranted();
149
            $companyMapper = CompanyMapper::getInstance($this->adapter);
150
            $records = $companyMapper->fetchAllByIdsAndNetworkId($accessGrantedIds->companies, $currentNetwork->id);
151
 
283 www 152
            $storage = Storage::getInstance($this->config);
119 efrain 153
 
154
            $companies = [];
155
            foreach($records as $record)
156
            {
157
                array_push($companies, [
164 efrain 158
                    'uuid' => $record->uuid,
119 efrain 159
                    'name' => $record->name,
283 www 160
                    'image' => $storage->getCompanyImage($record),
119 efrain 161
                    'link_progress' => $this->url()->fromRoute('microlearning/progress',['id' => $record->uuid], ['force_canonical' => true]),
162
                ]);
163
            }
164
 
165
            return new JsonModel([
166
                'success' => true,
167
                'data' =>  $companies
168
            ]);
169
 
170
 
171
        }
172
        return new JsonModel([
173
            'success' => false,
174
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
175
        ]);
176
    }
177
 
178
 
179
 
180
    public function capsuleCommentsAction()
181
    {
182
 
183
        $request = $this->getRequest();
184
 
185
        if($request->isGet()) {
186
            $serviceDatetimeFormat = $this->config['leaderslinked.services.datetime'];
187
 
188
            $currentUserPlugin = $this->plugin('currentUserPlugin');
189
            $currentUser = $currentUserPlugin->getUser();
190
 
191
            $capsule_id = $this->params()->fromRoute('capsule_id');
192
 
283 www 193
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
119 efrain 194
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
195
 
196
            if(!$capsule) {
197
                return new JsonModel([
198
                    'success' => false,
199
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
200
 
201
                ]);
202
 
203
            }
204
 
283 www 205
            $capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
119 efrain 206
            $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $capsule->id);
207
            if(! $capsuleUser) {
208
                return new JsonModel([
209
                    'success' => false,
210
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE',
211
                ]);
212
            }
213
 
214
            $userMapper = UserMapper::getInstance($this->adapter);
215
            $users = [];
216
 
283 www 217
 
218
            $storage = Storage::getInstance($this->config);
219
 
220
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
119 efrain 221
            $records = $capsuleCommentMapper->fetchAllByCapsuleId($capsule->id);
222
 
223
            $comments = [];
224
            foreach($records as $record)
225
            {
226
 
227
                if(isset($users[$record->user_id])) {
228
 
229
                    $user = $users[$record->user_id];
230
 
231
                } else {
232
 
233
                    $user = $userMapper->fetchOne($record->user_id);
234
                    if(!$user) {
235
                        continue;
236
                    }
237
 
238
                    $users[$record->user_id] = $user;
239
 
240
 
241
                }
242
 
243
 
244
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
245
 
246
                array_push($comments, [
247
                    'date' => $dt->format($serviceDatetimeFormat),
283 www 248
                    'image' => $storage->getUserImage($user),
119 efrain 249
                    'fullname' => trim(trim($user->first_name) . ' ' . trim($user->last_name)),
250
                    'rating' => strval($record->rating),
251
                    'comment' => $record->comment,
252
                    '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]) : '',
253
                ]);
254
            }
255
 
256
            $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->topic_id, $capsule->id);
257
 
258
            return new JsonModel([
259
                'success' => true,
260
                'data' => [
261
                    'comments' => $comments,
262
                    'capsule' => [
263
                        'total_comments' => strval($dataCountAndRatingAverage['total_comments']),
264
                        'total_rating' => strval($dataCountAndRatingAverage['total_rating'])
265
                    ]
266
                ]
267
 
268
            ]);
269
 
270
        }
271
 
272
        return new JsonModel([
273
            'success' => false,
274
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
275
        ]);
276
    }
277
 
278
 
279
 
280
    public function capsuleDeleteMyCommentAction()
281
    {
282
 
283
        $request = $this->getRequest();
284
 
285
        if($request->isPost()) {
286
 
287
            $currentUserPlugin = $this->plugin('currentUserPlugin');
288
            $currentUser = $currentUserPlugin->getUser();
289
 
290
            $capsule_id = $this->params()->fromRoute('capsule_id');
291
            $comment_id = $this->params()->fromRoute('comment_id');
292
 
283 www 293
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
119 efrain 294
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
295
 
296
            if(!$capsule) {
297
                return new JsonModel([
298
                    'success' => false,
299
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
300
 
301
                ]);
302
 
303
            }
304
 
283 www 305
            $capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
119 efrain 306
            $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $capsule->id);
307
            if(! $capsuleUser) {
308
                return new JsonModel([
309
                    'success' => false,
310
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE',
311
                ]);
312
            }
313
 
283 www 314
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
119 efrain 315
            $capsuleComment = $capsuleCommentMapper->fetchOneByUuid($comment_id);
316
 
317
            if(!$capsuleComment) {
318
                return new JsonModel([
319
                    'success' => false,
320
                    'data' => 'ERROR_CAPSULE_COMMENT_NOT_FOUND',
321
                ]);
322
            }
323
 
324
            if($capsuleComment->capsule_id != $capsule->id || $capsuleComment->user_id != $currentUser->id) {
325
                return new JsonModel([
326
                    'success' => false,
327
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE_COMMENT',
328
                ]);
329
            }
330
 
331
 
332
            $result = $capsuleCommentMapper->delete($capsuleComment->id);
333
            if($result) {
334
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->topic_id, $capsule->id);
335
 
336
 
337
 
338
 
339
                return new JsonModel([
340
                    'success' => true,
341
                    'data' => [
342
                        'message' => 'LABEL_CAPSULE_COMMENT_HAVE_BEEN_SUCCESSFULLY_DELETE',
343
                        'capsule' => [
344
                            'total_comments' => strval($dataCountAndRatingAverage['total_comments']),
345
                            'total_rating' => strval($dataCountAndRatingAverage['total_rating'])
346
                        ]
347
                    ],
348
 
349
                ]);
350
            } else {
351
                return new JsonModel([
352
                    'success' => false,
353
                    'data' => $capsuleCommentMapper->getError()
354
 
355
                ]);
356
            }
357
 
358
 
359
 
360
        }
361
 
362
        return new JsonModel([
363
            'success' => false,
364
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
365
        ]);
366
    }
367
 
368
    public function capsuleAddMyCommentAction()
369
    {
370
 
371
        $request = $this->getRequest();
372
 
373
        if($request->isPost()) {
374
 
375
            $currentUserPlugin = $this->plugin('currentUserPlugin');
376
            $currentUser = $currentUserPlugin->getUser();
377
 
378
            $capsule_id = $this->params()->fromRoute('capsule_id');
379
 
380
 
283 www 381
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
119 efrain 382
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
383
 
384
            if(!$capsule) {
385
                return new JsonModel([
386
                    'success' => false,
387
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
388
 
389
                ]);
390
 
391
            }
392
 
283 www 393
            $capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
119 efrain 394
            $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $capsule->id);
395
            if(! $capsuleUser) {
396
                return new JsonModel([
397
                    'success' => false,
398
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE',
399
                ]);
400
            }
401
 
402
            //$rawdata = file_get_contents("php://input");
403
            //  error_log('$rawdata = ' . $rawdata );
404
 
405
 
406
            $form = new  CapsuleCommentForm();
407
            $dataPost = $request->getPost()->toArray();
408
            $dataPost['added_on'] = $capsuleMapper->getDatebaseNow();
409
 
410
 
411
            $form->setData($dataPost);
412
 
413
            if($form->isValid()) {
414
                $dataPost = (array) $form->getData();
415
 
416
 
283 www 417
                $capsuleComment = new MicrolearningCapsuleComment();
119 efrain 418
                $capsuleComment->company_id = $capsule->company_id;
419
                $capsuleComment->topic_id = $capsule->topic_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
 
433
                    $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->topic_id, $capsule->id);
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
 
301 www 517
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $topic->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
 
613
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->topic_id, $capsule->id);
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
 
757
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->topic_id, $capsule->id);
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
 
916
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->topic_id, $capsule->id);
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
 
283 www 1163
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1164
            $progressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
119 efrain 1165
 
1166
 
1167
            foreach($topics as $topic)
1168
            {
1169
                $resultCount = $capsuleMapper->fetchCountByCompanyIdAndTopicId($company->id, $topic->id);
1170
                $capsuleTotal =  $capsuleTotal + $resultCount;
1171
 
1172
                $resultCount = $progressMapper->fetchCountCapsulesCompletedByIdAndTopicId($currentUser->id, $topic->id);
1173
                $capsuleCompleted = $capsuleCompleted + $resultCount;
1174
 
1175
                $resultCount = $progressMapper->fetchCountCapsulesCompletedWithReturningByIdAndTopicId($currentUser->id, $topic->id);
1176
                $capsuleWithReturning = $capsuleWithReturning + $resultCount;
1177
 
1178
                $resultCount = $progressMapper->fetchCountCapsulesCompletedWithoutReturningByIdAndTopicId($currentUser->id, $topic->id);
1179
                $capsuleWithoutReturning = $capsuleWithoutReturning + $resultCount;
1180
 
1181
                $resultCount = $progressMapper->fetchCountCapsulesCompletedByIdAndTopicId($currentUser->id, $topic->id);
1182
                $capsuleStarted = $capsuleStarted + $resultCount;
1183
            }
1184
 
1185
            $capsuleToStart = $capsuleTotal -  $capsuleStarted;
1186
 
1187
 
1188
            if($capsuleTotal > 0) {
1189
                $percentCompleted = ($capsuleCompleted * 100) /  $capsuleTotal;
1190
                $percentIncompleted = 100 - $percentCompleted;
1191
            }
1192
 
1193
 
1194
 
1195
            return new JsonModel([
1196
                'success' => true,
1197
                'data' => [
1198
                    'capsuleTotal' => $capsuleTotal,
1199
                    'capsuleCompleted' => $capsuleCompleted,
1200
                    'capsuleStarted' => $capsuleStarted,
1201
                    'capsuleToStart' => $capsuleToStart,
1202
                    'percentCompleted' => number_format($percentCompleted, 2, '.', ','),
1203
                    'percentIncompleted' => number_format($percentIncompleted, 2, '.', ','),
1204
                    'capsuleWithReturning' => $capsuleWithReturning,
1205
                    'capsuleWithoutReturning' => $capsuleWithoutReturning,
1206
                ],
1207
            ]);
1208
 
1209
 
1210
        } else {
1211
            return new JsonModel([
1212
                'success' => false,
1213
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1214
            ]);
1215
        }
1216
    }
1217
 
1218
 
1219
    public function topicsAction()
1220
    {
1221
        $request = $this->getRequest();
1222
        if($request->isGet()) {
1223
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1224
            $currentUser = $currentUserPlugin->getUser();
1225
 
1226
 
1227
            $data = [];
1228
            $accessGrantedIds = $this->getAccessGranted();
1229
 
283 www 1230
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1231
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 1232
 
283 www 1233
            $storage = Storage::getInstance($this->config);
1234
            $path = $storage->getPathMicrolearningTopic();
119 efrain 1235
 
1236
            foreach($accessGrantedIds->topics as $id)
1237
            {
1238
                $topic = $topicMapper->fetchOne($id);
1239
                if(!$topic) {
1240
                    continue;
1241
                }
1242
 
1243
                $userProgress = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $id);
1244
                if($userProgress) {
1245
                    $progress = $userProgress->progress;
1246
                    $completed = $userProgress->completed;
1247
                } else {
1248
                    $progress = 0;
1249
                    $completed = 0;
1250
                }
1251
 
1252
 
1253
 
1254
                array_push($data, [
164 efrain 1255
                    'uuid'          => $topic->uuid,
119 efrain 1256
                    'name'          => $topic->name ? $topic->name : '',
1257
                    'description'   => $topic->description ? $topic->description : '',
283 www 1258
                    'image'         => $topic->image ? $storage->getGenericImage($path, $topic->uuid, $topic->image) : '',
119 efrain 1259
                    'progress'      => $progress,
1260
                    'completed'     => $completed,
1261
                    'order'         => $topic->order,
1262
                    'added_on'      => $topic->added_on,
1263
                    'updated_on'    => $topic->updated_on,
1264
                    'link_capsules' => $this->url()->fromRoute('microlearning/capsules', ['topic_id' => $topic->uuid], ['force_canonical' => true]),
161 efrain 1265
                    'link_get'      => $this->url()->fromRoute('microlearning/get-topic', ['id' => $topic->uuid], ['force_canonical' => true]),
119 efrain 1266
 
1267
                ]);
1268
            }
1269
 
1270
            usort($data, function($a, $b) {
1271
 
1272
                $result =  $a['order'] <=> $b['order'];
1273
                if(0 == $result) {
1274
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1275
                    if(0 == $result) {
1276
                        $result = strcasecmp($a['name'], $b['name']);
1277
                    }
1278
                }
1279
 
1280
                if($result < 0) {
1281
                    return 1;
1282
                } else if($result > 0) {
1283
                    return -1;
1284
                } else  {
1285
                    return  0;
1286
                }
1287
            });
1288
 
1289
 
1290
 
1291
                return new JsonModel([
1292
                    'success' => true,
1293
                    'data' => $data
1294
                ]);
1295
 
1296
        } else {
1297
            return new JsonModel([
1298
                'success' => false,
1299
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1300
            ]);
1301
        }
1302
    }
1303
 
161 efrain 1304
    public function getTopicAction()
1305
    {
1306
        $request = $this->getRequest();
1307
        if($request->isGet()) {
1308
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1309
            $currentUser = $currentUserPlugin->getUser();
1310
 
1311
 
1312
            $id = $this->params()->fromRoute('id');
283 www 1313
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
161 efrain 1314
            $topic = $topicMapper->fetchOneByUuid($id);
1315
 
1316
            if(!$topic) {
1317
                return new JsonModel([
1318
                    'success' => false,
1319
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1320
                ]);
1321
            }
1322
 
1323
            $accessGrantedIds = $this->getAccessGranted();
1324
 
1325
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1326
                return new JsonModel([
1327
                    'success' => false,
1328
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1329
                ]);
1330
            }
1331
 
1332
 
1333
            $data = [];
1334
            $accessGrantedIds = $this->getAccessGranted();
1335
 
283 www 1336
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
161 efrain 1337
 
1338
            $userProgress = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $id);
1339
            if($userProgress) {
1340
                $progress = $userProgress->progress;
1341
                $completed = $userProgress->completed;
1342
            } else {
1343
                $progress = 0;
1344
                $completed = 0;
1345
            }
1346
 
283 www 1347
            $storage = Storage::getInstance($this->config);
1348
            $path = $storage->getPathMicrolearningTopic();
161 efrain 1349
 
1350
            $data = [
165 efrain 1351
                'uuid'          => $topic->uuid,
161 efrain 1352
                'name'          => $topic->name ? $topic->name : '',
1353
                'description'   => $topic->description ? $topic->description : '',
283 www 1354
                'image'         => $topic->image ? $storage->getGenericImage($path, $topic->uuid, $topic->image) : '',
161 efrain 1355
                'progress'      => $progress,
1356
                'completed'     => $completed,
1357
                'order'         => $topic->order,
1358
                'added_on'      => $topic->added_on,
1359
                'updated_on'    => $topic->updated_on,
1360
                'link_capsules' => $this->url()->fromRoute('microlearning/capsules', ['topic_id' => $topic->uuid], ['force_canonical' => true]),
1361
 
1362
            ];
1363
 
1364
 
1365
 
1366
            return new JsonModel([
1367
                'success' => true,
1368
                'data' => $data
1369
            ]);
1370
 
1371
        } else {
1372
            return new JsonModel([
1373
                'success' => false,
1374
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1375
            ]);
1376
        }
1377
    }
119 efrain 1378
 
161 efrain 1379
 
119 efrain 1380
    public function capsulesAction()
1381
    {
1382
        $request = $this->getRequest();
1383
        if($request->isGet()) {
1384
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1385
            $currentUser = $currentUserPlugin->getUser();
1386
 
1387
 
1388
 
1389
            $topic_id = $this->params()->fromRoute('topic_id');
283 www 1390
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 1391
            $topic = $topicMapper->fetchOneByUuid($topic_id);
1392
 
1393
            if(!$topic) {
1394
                return new JsonModel([
1395
                    'success' => false,
1396
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1397
                ]);
1398
            }
1399
 
1400
            $accessGrantedIds = $this->getAccessGranted();
1401
 
1402
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1403
                return new JsonModel([
1404
                    'success' => false,
1405
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1406
                ]);
1407
            }
1408
 
1409
 
1410
 
1411
            $data = [];
1412
 
1413
 
283 www 1414
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1415
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1416
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
119 efrain 1417
 
283 www 1418
            $storage = Storage::getInstance($this->config);
1419
            $path = $storage->getPathMicrolearningCapsule();
119 efrain 1420
 
1421
            $capsules = $capsuleMapper->fetchAllActiveByCompanyIdAndTopicId($topic->company_id, $topic->id);
1422
            foreach($capsules as $capsule)
1423
            {
1424
 
1425
                if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1426
                    continue;
1427
                }
1428
 
1429
                $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
1430
                if($userProgress) {
1431
                    $progress = $userProgress->progress;
1432
                    $completed = $userProgress->completed;
1433
                } else {
1434
                    $progress = 0;
1435
                    $completed = 0;
1436
                }
1437
 
1438
                $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->topic_id, $capsule->id);
1439
 
1440
 
1441
 
1442
                array_push($data, [
166 efrain 1443
                    'uuid'              => $capsule->uuid,
119 efrain 1444
                    'name'              => $capsule->name ? $capsule->name : '',
1445
                    'description'       => $capsule->description ? $capsule->description : '',
283 www 1446
                    'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image)  : '',
119 efrain 1447
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
1448
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
1449
                    'link_slides'       => $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic->uuid,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]),
161 efrain 1450
                    'link_get'          => $this->url()->fromRoute('microlearning/get-capsule', ['id' => $capsule->uuid], ['force_canonical' => true]),
119 efrain 1451
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
1452
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
1453
                    'progress'          => $progress,
1454
                    'completed'         => $completed,
1455
                    'order'             => $capsule->order,
1456
                    'added_on'          => $capsule->added_on,
1457
                    'updated_on'        => $capsule->updated_on,
1458
                ]);
1459
 
1460
 
1461
 
1462
            }
1463
 
1464
            usort($data, function($a, $b) {
1465
 
1466
                $result =  $a['order'] <=> $b['order'];
1467
                if(0 == $result) {
1468
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1469
                    if(0 == $result) {
1470
                        $result = strcasecmp($a['name'], $b['name']);
1471
                    }
1472
                }
1473
 
1474
                if($result < 0) {
1475
                    return 1;
1476
                } else if($result > 0) {
1477
                    return -1;
1478
                } else  {
1479
                    return  0;
1480
                }
1481
            });
1482
 
1483
 
1484
 
1485
                return new JsonModel([
1486
                    'success' => true,
1487
                    'data' => $data
1488
                ]);
1489
 
1490
        } else {
1491
            return new JsonModel([
1492
                'success' => false,
1493
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1494
            ]);
1495
        }
1496
    }
1497
 
235 efrain 1498
    public function takeTestAction()
1499
    {
1500
        $request = $this->getRequest();
1501
        if($request->isPost()) {
1502
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1503
            $currentUser = $currentUserPlugin->getUser();
1504
 
1505
            $slide_id = $this->params()->fromRoute('slide_id');
283 www 1506
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
235 efrain 1507
            $slide = $slideMapper->fetchOneByUuid($slide_id);
1508
 
1509
            if(!$slide) {
1510
                return new JsonModel([
1511
                    'success' => false,
1512
                    'data' => 'ERROR_SLIDE_NOT_FOUND'
1513
                ]);
1514
            }
1515
 
283 www 1516
            if($slide->type != MicrolearningSlide::TYPE_QUIZ) {
235 efrain 1517
                return new JsonModel([
1518
                    'success' => false,
1519
                    'data' => 'ERROR_SLIDE_IS_NOT_QUIZ'
1520
                ]);
1521
            }
1522
 
1523
 
1524
 
283 www 1525
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
235 efrain 1526
            $capsule = $capsuleMapper->fetchOne($slide->capsule_id);
1527
 
1528
            if(!$capsule) {
1529
                return new JsonModel([
1530
                    'success' => false,
1531
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
1532
                ]);
1533
            }
1534
 
1535
            $accessGrantedIds = $this->getAccessGranted();
1536
 
1537
            if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1538
                return new JsonModel([
1539
                    'success' => false,
1540
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1541
                ]);
1542
            }
1543
 
283 www 1544
            $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
235 efrain 1545
            $userLog = $userLogMapper->fetchOneTakeATestByUserIdAndSlideId($currentUser->id, $slide->id);
1546
 
1547
            if($userLog) {
283 www 1548
                $activity = MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST;
235 efrain 1549
            } else {
283 www 1550
                $activity = MicrolearningUserLog::ACTIVITY_TAKE_A_TEST;
235 efrain 1551
            }
1552
 
1553
            $added_on = $userLogMapper->getDatebaseNow();
1554
 
283 www 1555
            $userLog = new MicrolearningUserLog();
235 efrain 1556
            $userLog->activity      = $activity;
1557
            $userLog->user_id       = $currentUser->id;
1558
            $userLog->company_id    = $slide->company_id;
1559
            $userLog->topic_id      = $slide->topic_id;
1560
            $userLog->capsule_id    = $slide->capsule_id;
1561
            $userLog->slide_id      = $slide->id;
1562
            $userLog->added_on      = $added_on;
1563
 
1564
            if($userLogMapper->insert($userLog)) {
1565
                return new JsonModel([
1566
                    'success' => true,
1567
                ]);
1568
            } else {
1569
                return new JsonModel([
1570
                    'success' => false,
1571
                    'data' => $userLogMapper->getError()
1572
                ]);
1573
            }
1574
 
1575
 
1576
 
1577
 
1578
 
1579
 
1580
 
1581
        } else {
1582
            return new JsonModel([
1583
                'success' => false,
1584
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1585
            ]);
1586
        }
1587
    }
1588
 
161 efrain 1589
    public function getCapsuleAction()
1590
    {
1591
        $request = $this->getRequest();
1592
        if($request->isGet()) {
1593
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1594
            $currentUser = $currentUserPlugin->getUser();
1595
 
1596
 
1597
 
1598
            $capsule_id = $this->params()->fromRoute('id');
283 www 1599
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
161 efrain 1600
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
1601
 
1602
            if(!$capsule) {
1603
                return new JsonModel([
1604
                    'success' => false,
1605
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
1606
                ]);
1607
            }
1608
 
1609
            $accessGrantedIds = $this->getAccessGranted();
1610
 
1611
            if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1612
                return new JsonModel([
1613
                    'success' => false,
1614
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1615
                ]);
1616
            }
1617
 
1618
 
1619
 
1620
 
283 www 1621
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
161 efrain 1622
            $topic = $topicMapper->fetchOne($capsule->topic_id);
1623
 
283 www 1624
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1625
            $capsuleCommentMapper = MicrolearningCapsuleCommentMapper::getInstance($this->adapter);
161 efrain 1626
 
1627
 
1628
 
1629
            $userProgress = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
1630
            if($userProgress) {
1631
                $progress = $userProgress->progress;
1632
                $completed = $userProgress->completed;
1633
            } else {
1634
                $progress = 0;
1635
                $completed = 0;
1636
            }
1637
 
1638
            $dataCountAndRatingAverage = $capsuleCommentMapper->fetchCountAndRatingAverage($capsule->company_id, $capsule->topic_id, $capsule->id);
1639
 
244 efrain 1640
 
283 www 1641
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
244 efrain 1642
            $totalSlides = $slideMapper->fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($capsule->company_id, $capsule->topic_id, $capsule->id);
1643
 
1644
 
1645
            $slide = $slideMapper->fetchFirstByCompanyIdAndTopicIdAndCapsuleId($capsule->company_id, $capsule->topic_id, $capsule->id);
1646
            if($slide) {
1647
 
1648
                $link_first_slide = $this->url()->fromRoute('microlearning/get-slide', ['id' => $slide->uuid], ['force_canonical' => true]);
1649
                $type_first_slide = $slide->type;
161 efrain 1650
 
1651
 
244 efrain 1652
            } else {
1653
                $link_first_slide = '';
1654
                $type_first_slide = '';
1655
            }
1656
 
283 www 1657
            $storage = Storage::getInstance($this->config);
1658
            $path = $storage->getPathMicrolearningCapsule();
244 efrain 1659
 
1660
 
1661
 
161 efrain 1662
            $data = [
164 efrain 1663
                    'uuid'              => $capsule->uuid,
161 efrain 1664
                    'name'              => $capsule->name ? $capsule->name : '',
1665
                    'description'       => $capsule->description ? $capsule->description : '',
283 www 1666
                    'image'             => $capsule->image ? $storage->getGenericImage($path, $capsule->uuid, $capsule->image)  : '',
161 efrain 1667
                    'link_comments'     => $this->url()->fromRoute('microlearning/capsules-comments', ['capsule_id' => $capsule->uuid], ['force_canonical' => true]),
1668
                    'link_comment_add'  => $this->url()->fromRoute('microlearning/capsules-comments/add', ['capsule_id' => $capsule->uuid],['force_canonical' => true]),
1669
                    'link_slides'       => $this->url()->fromRoute('microlearning/slides', ['topic_id' => $topic->uuid,  'capsule_id' => $capsule->uuid], ['force_canonical' => true]),
1670
                    'total_comments'    => strval($dataCountAndRatingAverage['total_comments']),
1671
                    'total_rating'      => strval($dataCountAndRatingAverage['total_rating']),
1672
                    'progress'          => $progress,
1673
                    'completed'         => $completed,
245 efrain 1674
                    'total_slides'      => $totalSlides,
244 efrain 1675
                    'link_first_slide'  => $link_first_slide,
1676
                    'type_first_slide'  => $type_first_slide,
161 efrain 1677
                    'order'             => $capsule->order,
1678
                    'added_on'          => $capsule->added_on,
1679
                    'updated_on'        => $capsule->updated_on,
1680
            ];
1681
 
1682
 
1683
 
1684
            return new JsonModel([
1685
                'success' => true,
1686
                'data' => $data
1687
            ]);
1688
 
1689
        } else {
1690
            return new JsonModel([
1691
                'success' => false,
1692
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1693
            ]);
1694
        }
1695
    }
1696
 
119 efrain 1697
    public function slidesAction()
1698
    {
1699
        $request = $this->getRequest();
1700
        if($request->isGet()) {
1701
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1702
            $currentUser = $currentUserPlugin->getUser();
1703
 
1704
 
1705
 
1706
            $topic_id = $this->params()->fromRoute('topic_id');
283 www 1707
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
119 efrain 1708
            $topic = $topicMapper->fetchOneByUuid($topic_id);
1709
 
1710
            if(!$topic) {
1711
                return new JsonModel([
1712
                    'success' => false,
1713
                    'data' => 'ERROR_TOPIC_NOT_FOUND'
1714
                ]);
1715
            }
1716
 
1717
            $accessGrantedIds = $this->getAccessGranted();
1718
 
1719
            if(!in_array($topic->id, $accessGrantedIds->topics)) {
1720
                return new JsonModel([
1721
                    'success' => false,
1722
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
1723
                ]);
1724
            }
1725
 
1726
            $capsule_id = $this->params()->fromRoute('capsule_id');
283 www 1727
            $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
119 efrain 1728
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
1729
 
1730
            if(!$capsule) {
1731
                return new JsonModel([
1732
                    'success' => false,
1733
                    'data' => 'ERROR_CAPSULE_NOT_FOUND'
1734
                ]);
1735
            }
1736
 
1737
 
1738
 
1739
            if(!in_array($capsule->id, $accessGrantedIds->capsules)) {
1740
                return new JsonModel([
1741
                    'success' => false,
1742
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1743
                ]);
1744
            }
1745
 
1746
 
1747
 
1748
            $data = [];
1749
 
283 www 1750
            $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
119 efrain 1751
 
283 www 1752
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
1753
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
119 efrain 1754
 
283 www 1755
            $storage = Storage::getInstance($this->config);
1756
            $path = $storage->getPathMicrolearningSlide();
119 efrain 1757
 
1758
            $slides = $slideMapper->fetchAllByCompanyIdAndTopicIdAndCapsuleId($capsule->company_id, $capsule->topic_id, $capsule->id);
1759
            foreach($slides as $slide)
1760
            {
1761
 
1762
 
1763
                $userProgress = $userProgressMapper->fetchOneByUserIdAndSlideId($currentUser->id, $slide->id);
1764
                if($userProgress) {
224 efrain 1765
                    $completed =  $userProgress->completed ;
119 efrain 1766
                } else {
1767
                    $completed = 0;
1768
                }
1769
 
235 efrain 1770
 
230 efrain 1771
 
235 efrain 1772
                $link_take_a_test = '';
230 efrain 1773
                if($slide->quiz_id) {
235 efrain 1774
 
283 www 1775
                    $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
230 efrain 1776
                    $quiz = $quizMapper->fetchOne($slide->quiz_id);
1777
                    if($quiz) {
1778
                        $quiz_uuid = $quiz->uuid;
235 efrain 1779
                        $quiz_data = $this->getQuiz($slide->quiz_id);
1780
                        $link_take_a_test = $this->url()->fromRoute('microlearning/take-a-test', ['slide_id' => $slide->uuid], ['force_canonical' => true]);
230 efrain 1781
                    }
235 efrain 1782
 
1783
                }
119 efrain 1784
 
1785
 
1786
                array_push($data, [
235 efrain 1787
                    'quiz'                  => $quiz_uuid,
1788
                    'quiz_data'             => $quiz_data,
1789
                    'uuid'                  => $slide->uuid,
1790
                    'name'                  => $slide->name ? $slide->name : '',
1791
                    'description'           => $slide->description ? $slide->description : '',
1792
                    'type'                  => $slide->type,
283 www 1793
                    'background'            => $slide->background ? $storage->getGenericImage($path, $slide->uuid, $slide->background) : '',
1794
                    'file'                  => $slide->file ? $storage->getGenericFile($path, $slide->uuid, $slide->file) : '',
235 efrain 1795
                    'order'                 => $slide->order,
1796
                    'completed'             => $completed,
1797
                    'link_get'              => $this->url()->fromRoute('microlearning/get-slide', ['id' => $slide->uuid], ['force_canonical' => true]),
1798
                    '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 1799
                    'link_take_a_test'      => $link_take_a_test,
235 efrain 1800
                    'added_on'              => $slide->added_on,
1801
                    'updated_on'            => $slide->updated_on,
119 efrain 1802
                ]);
1803
 
1804
 
1805
 
1806
            }
1807
 
1808
            usort($data, function($a, $b) {
1809
 
1810
                $result =  $a['order'] <=> $b['order'];
1811
                if(0 == $result) {
1812
                    $result = strcasecmp($a['added_on'], $b['added_on']);
1813
                    if(0 == $result) {
1814
                        $result = strcasecmp($a['name'], $b['name']);
1815
                    }
1816
                }
1817
 
1818
                if($result < 0) {
1819
                    return 1;
1820
                } else if($result > 0) {
1821
                    return -1;
1822
                } else  {
1823
                    return  0;
1824
                }
1825
            });
1826
 
1827
 
1828
 
1829
                return new JsonModel([
1830
                    'success' => true,
1831
                    'data' => $data
1832
                ]);
1833
 
1834
        } else {
1835
            return new JsonModel([
1836
                'success' => false,
1837
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1838
            ]);
1839
        }
1840
    }
1841
 
229 efrain 1842
    private function getQuiz($id)
1843
    {
1844
        $serviceDatetimeFormat = $this->config['leaderslinked.services.datetime'];
1845
 
1846
        $data = [];
1847
 
283 www 1848
        $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
229 efrain 1849
        $quiz = $quizMapper->fetchOne($id);
1850
 
1851
        if(!$quiz) {
1852
            return [];
1853
        }
1854
 
1855
        $companyMapper = CompanyMapper::getInstance($this->adapter);
1856
        $company = $companyMapper->fetchOne($quiz->company_id);
1857
 
283 www 1858
        $questionMapper = MicrolearningQuestionMapper::getInstance($this->adapter);
1859
        $answerMapper = MicrolearningAnswerMapper::getInstance($this->adapter);
229 efrain 1860
 
1861
        $record_questions = [];
1862
        $questions = $questionMapper->fetchAllByQuizId($quiz->id);
1863
        foreach($questions as $question)
1864
        {
1865
            $record_answers = [];
1866
 
1867
            $answers = $answerMapper->fetchAllByQuizIdAndQuestionId($question->quiz_id, $question->id);
1868
            foreach($answers as $answer)
1869
            {
1870
                $dtAddedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $answer->added_on);
1871
                $dtUpdatedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $answer->updated_on);
1872
 
1873
                array_push($record_answers, [
1874
                    'uuid' => $answer->uuid,
1875
                    'text' => trim($answer->text),
1876
                    'correct' => $answer->correct ? $answer->correct  : 0 ,
1877
                    'points' => strval(intval($answer->points, 10)),
1878
                    'added_on'  => $dtAddedOn->format($serviceDatetimeFormat),
1879
                    'updated_on'    => $dtUpdatedOn->format($serviceDatetimeFormat),
1880
                ]);
1881
            }
1882
 
1883
            $dtAddedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $question->added_on);
1884
            $dtUpdatedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $question->updated_on);
1885
 
1886
            array_push($record_questions, [
1887
                'uuid'          => $question->uuid,
1888
                'text'          => trim($question->text),
1889
                'type'          => $question->type,
1890
                'maxlength'     => strval($question->maxlength),
1891
                'points'        => strval($question->points),
1892
                'answers'       => $record_answers,
1893
                'added_on'      => $dtAddedOn->format($serviceDatetimeFormat),
1894
                'updated_on'    => $dtUpdatedOn->format($serviceDatetimeFormat),
1895
            ]);
1896
        }
1897
 
1898
        $dtAddedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $quiz->added_on);
1899
        $dtUpdatedOn = \DateTime::createFromFormat('Y-m-d H:i:s', $quiz->updated_on);
1900
 
1901
        array_push($data, [
1902
            'uuid' => $quiz->uuid,
1903
            'name' => $quiz->name,
1904
            'text' => trim($quiz->text ? $quiz->text : ''),
1905
            'failed' => trim($quiz->failed ? $quiz->failed : ''),
1906
            'points' => strval($quiz->points),
1907
            'minimum_points_required' => strval($quiz->minimum_points_required),
1908
            'max_time' => $quiz->max_time ? $quiz->max_time : 5,
1909
            'company_uuid' => $company->uuid,
1910
            'company_name' => $company->name,
1911
            'company_image' => $this->url()->fromRoute('services/storage',['type' => 'company', 'code' => $company->uuid, 'filename' => $company->image], ['force_canonical' => true]),
1912
            'questions'     => $record_questions,
1913
            'added_on'      => $dtAddedOn->format($serviceDatetimeFormat),
1914
            'updated_on'    => $dtUpdatedOn->format($serviceDatetimeFormat),
1915
        ]);
1916
 
1917
        return $data;
1918
 
1919
    }
161 efrain 1920
 
229 efrain 1921
 
161 efrain 1922
    public function getSlideAction()
1923
    {
1924
        $request = $this->getRequest();
1925
        if($request->isGet()) {
1926
            $currentUserPlugin = $this->plugin('currentUserPlugin');
1927
            $currentUser = $currentUserPlugin->getUser();
1928
 
1929
 
1930
 
1931
            $id = $this->params()->fromRoute('id');
283 www 1932
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
161 efrain 1933
            $slide = $slideMapper->fetchOneByUuid($id);
1934
 
1935
            if(!$slide) {
1936
                return new JsonModel([
1937
                    'success' => false,
1938
                    'data' => 'ERROR_SLIDE_NOT_FOUND'
1939
                ]);
1940
            }
1941
 
1942
            $accessGrantedIds = $this->getAccessGranted();
1943
 
1944
 
1945
 
1946
            if(!in_array($slide->capsule_id, $accessGrantedIds->capsules)) {
1947
                return new JsonModel([
1948
                    'success' => false,
1949
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
1950
                ]);
1951
            }
1952
 
1953
 
283 www 1954
            $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
161 efrain 1955
            $topic = $topicMapper->fetchOne($slide->topic_id);
1956
 
283 www 1957
            $capsuleMapper =  MicrolearningCapsuleMapper::getInstance($this->adapter);
161 efrain 1958
            $capsule = $capsuleMapper->fetchOne($slide->capsule_id);
1959
 
1960
 
1961
 
1962
 
1963
 
283 www 1964
            $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
1965
            $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
161 efrain 1966
 
1967
 
1968
            $userProgress = $userProgressMapper->fetchOneByUserIdAndSlideId($currentUser->id, $slide->id);
1969
            if($userProgress) {
1970
                $completed = $userProgress->completed;
1971
            } else {
1972
                $completed = 0;
1973
            }
235 efrain 1974
 
1975
            $quiz_uuid = '';
1976
            $quiz_data = [];
1977
            $link_take_a_test = '';
1978
            if($slide->quiz_id) {
1979
 
283 www 1980
                $quizMapper = MicrolearningQuizMapper::getInstance($this->adapter);
235 efrain 1981
                $quiz = $quizMapper->fetchOne($slide->quiz_id);
1982
                if($quiz) {
1983
                    $quiz_uuid = $quiz->uuid;
1984
                    $quiz_data = $this->getQuiz($slide->quiz_id);
1985
                    $link_take_a_test = $this->url()->fromRoute('microlearning/take-a-test', ['slide_id' => $slide->uuid], ['force_canonical' => true]);
1986
                }
161 efrain 1987
 
235 efrain 1988
            }
161 efrain 1989
 
283 www 1990
            $storage = Storage::getInstance($this->config);
1991
            $path = $storage->getPathMicrolearningSlide();
161 efrain 1992
 
1993
            $data =[
235 efrain 1994
                'quiz'              => $quiz_uuid,
1995
                'quiz_data'         => $quiz_data,
1996
                'uuid'              => $slide->uuid,
1997
                'name'              => $slide->name ? $slide->name : '',
1998
                'description'       => $slide->description ? $slide->description : '',
1999
                'type'              => $slide->type,
283 www 2000
                'background'        => $slide->background ? $storage->getGenericImage($path, $slide->uuid, $slide->background) : '',
2001
                'file'              => $slide->file ? $storage->getGenericFile($path, $slide->uuid, $slide->file) : '',
235 efrain 2002
                'order'             => $slide->order,
2003
                'completed'         => $completed,
2004
                '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]),
2005
                'link_take_a_test'  => $link_take_a_test,
2006
                'added_on'          => $slide->added_on,
2007
                'updated_on'        => $slide->updated_on,
161 efrain 2008
            ];
2009
 
2010
 
2011
 
2012
 
2013
            return new JsonModel([
2014
                'success' => true,
2015
                'data' => $data
2016
            ]);
2017
 
2018
        } else {
2019
            return new JsonModel([
2020
                'success' => false,
2021
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
2022
            ]);
2023
        }
2024
    }
2025
 
119 efrain 2026
    public function profileAction()
2027
    {
2028
        $request = $this->getRequest();
2029
        if($request->isGet()) {
2030
            $currentUserPlugin = $this->plugin('currentUserPlugin');
2031
            $currentUser = $currentUserPlugin->getUser();
2032
 
2033
 
2034
            $accessGrantedIds = $this->getAccessGranted();
2035
 
2036
            $companyMapper = CompanyMapper::getInstance($this->adapter);
283 www 2037
            $companyExtendUserMapper = MicrolearningExtendUserMapper::getInstance($this->adapter);
2038
            $companyExtendUserCompanyMapper = MicrolearningExtendUserCompanyMapper::getInstance($this->adapter);
2039
            $companyExtendUserFunctionMapper = MicrolearningExtendUserFunctionMapper::getInstance($this->adapter);
2040
            $companyExtendUserGroupMapper = MicrolearningExtendUserGroupMapper::getInstance($this->adapter);
2041
            $companyExtendUserInstitutionMapper = MicrolearningExtendUserInstitutionMapper::getInstance($this->adapter);
2042
            $companyExtendUserPartnerMapper = MicrolearningExtendUserPartnerMapper::getInstance($this->adapter);
2043
            $companyExtendUserProgramMapper = MicrolearningExtendUserProgramMapper::getInstance($this->adapter);
2044
            $companyExtendUserStudentTypeMapper = MicrolearningExtendUserStudentTypeMapper::getInstance($this->adapter);
2045
            $companyExtendUserSectorMapper = MicrolearningExtendUserSectorMapper::getInstance($this->adapter);
119 efrain 2046
 
283 www 2047
            $storage = Storage::getInstance($this->config);
2048
 
119 efrain 2049
 
2050
            $data = [];
2051
            foreach($accessGrantedIds->companies as $company_id)
2052
            {
2053
                $company = $companyMapper->fetchOne($company_id);
2054
                if(!$company) {
2055
                    continue;
2056
                }
2057
 
2058
                $record = [
2059
                    'name' => $company->name,
283 www 2060
                    'image' => $storage->getCompanyImage($company),
119 efrain 2061
                    'details' => [],
2062
                ];
2063
 
2064
                $companyExtendUser = $companyExtendUserMapper->fetchOneByCompanyIdAndUserId($company->id, $currentUser->id);
2065
                if(!$companyExtendUser) {
2066
                    continue;
2067
                }
2068
 
2069
                if($companyExtendUser->extend_company_id) {
2070
 
2071
                    $extendedCompany = $companyExtendUserCompanyMapper->fetchOne($companyExtendUser->company_id);
2072
                    if($extendedCompany) {
2073
                        array_push($record['details'],[
2074
                            'uuid' => $extendedCompany->uuid,
2075
                            'label' => 'LABEL_COMPANY',
2076
                            'value' => $extendedCompany->name
2077
                        ]);
2078
                    }
2079
                }
2080
 
2081
                if($companyExtendUser->extend_function_id) {
2082
                    $extendedFunction = $companyExtendUserFunctionMapper->fetchOne($companyExtendUser->extend_function_id);
2083
                    if($extendedFunction) {
2084
                        array_push($record['details'],[
2085
                            'uuid' => $extendedFunction->uuid,
2086
                            'label' => 'LABEL_FUNCTION',
2087
                            'value' => $extendedFunction->name
2088
                        ]);
2089
                    }
2090
                }
2091
 
2092
                if($companyExtendUser->extend_group_id) {
2093
                    $extendedGroup = $companyExtendUserGroupMapper->fetchOne($companyExtendUser->extend_group_id);
2094
                    if($extendedGroup) {
2095
                        array_push($record['details'],[
2096
                            'uuid' => $extendedGroup->uuid,
2097
                            'label' => 'LABEL_GROUP',
2098
                            'value' => $extendedGroup->name
2099
                        ]);
2100
                    }
2101
                }
2102
 
2103
                if($companyExtendUser->extend_institution_id) {
2104
                    $extendedInstitution= $companyExtendUserInstitutionMapper->fetchOne($companyExtendUser->extend_institution_id);
2105
                    if($extendedInstitution) {
2106
                        array_push($record['details'],[
2107
                            'uuid' => $extendedInstitution->uuid,
2108
                            'label' => 'LABEL_INSTITUTION',
2109
                            'value' => $extendedInstitution->name
2110
                        ]);
2111
                    }
2112
                }
2113
 
2114
                if($companyExtendUser->extend_program_id) {
2115
                    $extendedProgram = $companyExtendUserProgramMapper->fetchOne($companyExtendUser->extend_program_id);
2116
                    if($extendedProgram) {
2117
                        array_push($record['details'],[
2118
                            'uuid' => $extendedProgram->uuid,
2119
                            'label' => 'LABEL_PROGRAM',
2120
                            'value' => $extendedProgram->name
2121
                        ]);
2122
 
2123
                    }
2124
                }
2125
 
2126
                if($companyExtendUser->extend_sector_id) {
2127
                    $extendedSector = $companyExtendUserSectorMapper->fetchOne($companyExtendUser->extend_sector_id);
2128
                    if($extendedSector) {
2129
                        array_push($record['details'],[
2130
                            'uuid' => $extendedSector->uuid,
2131
                            'label' => 'LABEL_SECTOR',
2132
                            'value' => $extendedSector->name
2133
                        ]);
2134
                    }
2135
                }
2136
 
2137
                if($companyExtendUser->extend_partner_id) {
2138
                    $extendedPartner = $companyExtendUserPartnerMapper->fetchOne($companyExtendUser->extend_partner_id);
2139
                    if($extendedPartner) {
2140
                        array_push($record['details'],[
2141
                            'uuid' => $extendedPartner->uuid,
2142
                            'label' => 'LABEL_PARTNER',
2143
                            'value' => $extendedPartner->name
2144
                        ]);
2145
                    }
2146
                }
2147
 
2148
                if($companyExtendUser->extend_student_type_id) {
2149
                    $extendedStudentType = $companyExtendUserStudentTypeMapper->fetchOne($companyExtendUser->extend_student_type_id);
2150
                    if($extendedStudentType) {
2151
                        array_push($record['details'],[
2152
                            'uuid' => $extendedStudentType->uuid,
2153
                            'label' => 'LABEL_TYPE',
2154
                            'value' => $extendedStudentType->name
2155
                        ]);
2156
                    }
2157
                }
2158
 
2159
                array_push($data, $record);
2160
            }
2161
 
2162
            return new JsonModel([
2163
                'success' => true,
2164
                'data' => $data
2165
            ]);
2166
 
2167
        } else {
2168
            return new JsonModel([
2169
                'success' => false,
2170
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
2171
            ]);
2172
        }
2173
    }
2174
 
167 efrain 2175
    public function syncAction()
2176
    {
2177
        $request = $this->getRequest();
168 efrain 2178
        if($request->isPost()) {
167 efrain 2179
            $currentUserPlugin = $this->plugin('currentUserPlugin');
2180
            $currentUser = $currentUserPlugin->getUser();
2181
 
2182
 
2183
            $operation = $this->params()->fromRoute('operation');
2184
            if($operation == 'slide-view' || $operation == 'capsule-close' || $operation == 'topic-close') {
2185
                $accessGrantedIds = $this->getAccessGranted();
2186
 
2187
                $topic_uuid     = $this->params()->fromRoute('topic_uuid');
2188
                $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
2189
                $slide_uuid     = $this->params()->fromRoute('slide_uuid');
2190
 
2191
                $accessGrantedIds = $this->getAccessGranted();
2192
                if($operation == 'slide-view') {
2193
 
2194
                    if(empty($slide_uuid) || empty($capsule_uuid ) || empty($topic_uuid)) {
2195
                        return new JsonModel([
2196
                            'success' => false,
2197
                            'data' => 'ERROR_INVALID_PARAMETERS'
2198
                        ]);
2199
                    }
2200
 
2201
 
283 www 2202
                    $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
167 efrain 2203
                    $topic = $topicMapper->fetchOneByUuid($topic_uuid);
2204
 
2205
                    if(!$topic) {
2206
                        return new JsonModel([
2207
                            'success' => false,
2208
                            'data' => 'ERROR_TOPIC_NOT_FOUND'
2209
                        ]);
2210
                    }
2211
 
2212
 
2213
 
2214
                    if(!in_array($topic->id, $accessGrantedIds->topics)) {
2215
                        return new JsonModel([
2216
                            'success' => false,
2217
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
2218
                        ]);
2219
                    }
2220
 
283 www 2221
                    $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
167 efrain 2222
                    $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
2223
 
2224
                    if(!$capsule) {
2225
                        return new JsonModel([
2226
                            'success' => false,
2227
                            'data' => 'ERROR_CAPSULE_NOT_FOUND'
2228
                        ]);
2229
                    }
119 efrain 2230
 
167 efrain 2231
                    if(!in_array($capsule->id, $accessGrantedIds->capsules) || $capsule->topic_id != $topic->id) {
2232
                        return new JsonModel([
2233
                            'success' => false,
2234
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
2235
                        ]);
2236
                    }
2237
 
283 www 2238
                    $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
167 efrain 2239
                    $slide = $slideMapper->fetchOneByUuid($slide_uuid);
2240
                    if(!$slide) {
2241
                        return new JsonModel([
2242
                            'success' => false,
2243
                            'data' => 'ERROR_SLIDE_NOT_FOUND'
2244
                        ]);
2245
                    }
2246
 
2247
                    if($slide->capsule_id != $capsule->id && $slide->topic_id != $topic->id) {
2248
                        return new JsonModel([
2249
                            'success' => false,
2250
                            'data' => 'ERROR_SLIDE_NOT_FOUND'
2251
                        ]);
2252
 
2253
                    }
2254
 
2255
 
283 www 2256
                    $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
167 efrain 2257
 
283 www 2258
                    $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
167 efrain 2259
                    $added_on = $userProgressMapper->getDatebaseNow();
2260
 
2261
 
2262
                    $userProgressTopic = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic->id);
2263
                    if(!$userProgressTopic) {
2264
 
283 www 2265
                        $userProgressTopic = new MicrolearningUserProgress();
167 efrain 2266
                        $userProgressTopic->company_id                  = $slide->company_id;
2267
                        $userProgressTopic->topic_id                    = $slide->topic_id;
176 efrain 2268
                        $userProgressTopic->user_id                     = $currentUser->id;
167 efrain 2269
                        $userProgressTopic->progress                    = 0;
2270
                        $userProgressTopic->returning                   = 0;
2271
                        $userProgressTopic->returning_after_completed   = 0;
2272
                        $userProgressTopic->completed                   = 0;
2273
                        $userProgressTopic->total_slides                = $slideMapper->fetchTotalCountByCompanyIdAndTopicId($slide->company_id, $slide->topic_id);
2274
                        $userProgressTopic->view_slides                 = 0;
283 www 2275
                        $userProgressTopic->type                        = MicrolearningUserProgress::TYPE_TOPIC;
167 efrain 2276
                        $userProgressTopic->added_on                    = $added_on;
2277
                        $userProgressTopic->updated_on                  = $added_on;
2278
 
2279
                        if($userProgressMapper->insert($userProgressTopic)) {
2280
 
283 www 2281
                            $userLog = new MicrolearningUserLog();
2282
                            $userLog->activity      = MicrolearningUserLog::ACTIVITY_START_TOPIC;
167 efrain 2283
                            $userLog->user_id       = $currentUser->id;
2284
                            $userLog->company_id    = $slide->company_id;
2285
                            $userLog->topic_id      = $slide->topic_id;
2286
                            $userLog->added_on      = $added_on;
2287
 
2288
                            if(!$userLogMapper->insert($userLog)) {
2289
                                return new JsonModel([
2290
                                    'success' => false,
2291
                                    'data' => $userProgressMapper->getError()
2292
                                ]);
2293
                            }
2294
                        } else {
2295
                            return new JsonModel([
2296
                                'success' => false,
2297
                                'data' => $userProgressMapper->getError()
2298
                            ]);
2299
                        }
2300
                    }
2301
 
2302
 
2303
                    $userProgressCapsule = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
2304
                    if(!$userProgressCapsule) {
2305
 
283 www 2306
                        $userProgressCapsule = new MicrolearningUserProgress();
167 efrain 2307
                        $userProgressCapsule->company_id                  = $slide->company_id;
2308
                        $userProgressCapsule->topic_id                    = $slide->topic_id;
2309
                        $userProgressCapsule->capsule_id                  = $slide->capsule_id;
176 efrain 2310
                        $userProgressCapsule->user_id                     = $currentUser->id;
167 efrain 2311
                        $userProgressCapsule->progress                    = 0;
2312
                        $userProgressCapsule->returning                   = 0;
2313
                        $userProgressCapsule->returning_after_completed   = 0;
2314
                        $userProgressCapsule->completed                   = 0;
2315
                        $userProgressCapsule->total_slides                = $slideMapper->fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($slide->company_id, $slide->topic_id, $slide->capsule_id);
2316
                        $userProgressCapsule->view_slides                 = 0;
283 www 2317
                        $userProgressCapsule->type                        = MicrolearningUserProgress::TYPE_CAPSULE;
167 efrain 2318
                        $userProgressCapsule->added_on                    = $added_on;
2319
                        $userProgressTopic->updated_on                    = $added_on;
2320
 
2321
                        if($userProgressMapper->insert($userProgressCapsule)) {
2322
 
283 www 2323
                            $userLog = new MicrolearningUserLog();
2324
                            $userLog->activity      = MicrolearningUserLog::ACTIVITY_START_CAPSULE;
167 efrain 2325
                            $userLog->user_id       = $currentUser->id;
2326
                            $userLog->company_id    = $slide->company_id;
2327
                            $userLog->topic_id      = $slide->topic_id;
2328
                            $userLog->capsule_id    = $slide->capsule_id;
2329
                            $userLog->added_on      = $added_on;
2330
 
2331
                            if(!$userLogMapper->insert($userLog)) {
2332
                                return new JsonModel([
2333
                                    'success' => false,
2334
                                    'data' => $userLogMapper->getError()
2335
                                ]);
2336
                            }
2337
                        } else {
2338
                            return new JsonModel([
2339
                                'success' => false,
2340
                                'data' => $userProgressMapper->getError()
2341
                            ]);
2342
                        }
2343
                    }
2344
 
2345
 
2346
                    $userProgressSlide = $userProgressMapper->fetchOneByUserIdAndSlideId($currentUser->id, $slide->id);
2347
                    if(!$userProgressSlide) {
283 www 2348
                        $userProgressSlide = new MicrolearningUserProgress();
167 efrain 2349
                        $userProgressSlide->company_id                  = $slide->company_id;
2350
                        $userProgressSlide->topic_id                    = $slide->topic_id;
2351
                        $userProgressSlide->capsule_id                  = $slide->capsule_id;
2352
                        $userProgressSlide->slide_id                    = $slide->id;
176 efrain 2353
                        $userProgressSlide->user_id                     = $currentUser->id;
167 efrain 2354
                        $userProgressSlide->progress                    = 0;
2355
                        $userProgressSlide->returning                   = 0;
2356
                        $userProgressSlide->returning_after_completed   = 0;
2357
                        $userProgressSlide->completed                   = 1;
2358
                        $userProgressSlide->total_slides                = 0;
2359
                        $userProgressSlide->view_slides                 = 0;
283 www 2360
                        $userProgressSlide->type                        = MicrolearningUserProgress::TYPE_SLIDE;
167 efrain 2361
                        $userProgressSlide->added_on                    = $added_on;
2362
                        $userProgressSlide->updated_on                  = $added_on;
2363
 
2364
                        if(!$userProgressMapper->insert($userProgressSlide)) {
2365
                            return new JsonModel([
2366
                                'success' => false,
2367
                                'data' => $userProgressMapper->getError()
2368
                            ]);
2369
                        }
2370
                    }
2371
 
283 www 2372
                    $userLog = new MicrolearningUserLog();
2373
                    $userLog->activity      = MicrolearningUserLog::ACTIVITY_VIEW_SLIDE;
167 efrain 2374
                    $userLog->user_id       = $currentUser->id;
2375
                    $userLog->company_id    = $slide->company_id;
2376
                    $userLog->topic_id      = $slide->topic_id;
2377
                    $userLog->capsule_id    = $slide->capsule_id;
2378
                    $userLog->slide_id      = $slide->id;
2379
                    $userLog->added_on      = $added_on;
2380
 
2381
                    if(!$userLogMapper->insert($userLog)) {
2382
                        return new JsonModel([
2383
                            'success' => false,
2384
                            'data' => $userLogMapper->getError()
2385
                        ]);
2386
                    }
2387
 
2388
                    $closeCapsule = false;
2389
 
2390
                    if($userProgressCapsule->completed) {
2391
 
2392
                        $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $added_on);
2393
                        $returning_on = $dt->format('Y-m-d');
2394
 
2395
                        if(!$userProgressCapsule->returning_on || $userProgressCapsule->returning_on != $returning_on) {
2396
 
2397
                            $userProgressCapsule->returning_on = $returning_on;
2398
                            $userProgressCapsule->returning_after_completed = $userProgressCapsule->returning_after_completed + 1;
2399
 
2400
                            if(!$userProgressMapper->update($userProgressCapsule)) {
2401
                                return new JsonModel([
2402
                                    'success' => false,
2403
                                    'data' => $userProgressMapper->getError()
2404
                                ]);
2405
                            }
2406
                        }
2407
 
2408
                    }  else {
2409
 
2410
                        $userProgressCapsule->total_slides = $slideMapper->fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($slide->company_id, $slide->topic_id, $slide->capsule_id);
2411
                        $userProgressCapsule->view_slides = $userProgressMapper->fetchCountAllSlideCompletedByUserIdAndCapsuleId($currentUser->id, $slide->capsule_id);
2412
 
2413
                        if($userProgressCapsule->total_slides) {
2414
 
2415
                            $userProgressCapsule->progress = ($userProgressCapsule->view_slides * 100) / $userProgressCapsule->total_slides;
2416
                            $userProgressCapsule->progress = $userProgressCapsule->progress > 100 ? 100 : $userProgressCapsule->progress;
2417
                        }
2418
 
2419
                        if(!$userProgressMapper->update($userProgressCapsule)) {
2420
                            return new JsonModel([
2421
                                'success' => false,
2422
                                'data' => $userProgressMapper->getError()
2423
                            ]);
2424
                        }
2425
 
2426
                        if($userProgressCapsule->progress >= 100) {
2427
                            $closeCapsule = true;
2428
                        }
171 efrain 2429
 
283 www 2430
 
167 efrain 2431
                    }
2432
 
2433
                    $closeTopic = false;
2434
                    if(!$userProgressTopic->completed) {
2435
 
2436
 
2437
                        $userProgressTopic->total_slides = $slideMapper->fetchTotalCountByCompanyIdAndTopicId($slide->company_id, $slide->topic_id);
2438
                        $userProgressTopic->view_slides = $userProgressMapper->fetchCountAllSlideCompletedByUserIdAndTopicId($currentUser->id, $slide->topic_id);
2439
 
2440
                        if($userProgressTopic->total_slides) {
2441
 
2442
                            $userProgressTopic->progress = ($userProgressTopic->view_slides * 100) / $userProgressTopic->total_slides;
2443
                            $userProgressTopic->progress = $userProgressTopic->progress > 100 ? 100 : $userProgressTopic->progress;
2444
                        }
2445
                        if(!$userProgressMapper->update($userProgressTopic)) {
2446
                            return new JsonModel([
2447
                                'success' => false,
2448
                                'data' => $userProgressMapper->getError()
2449
                            ]);
2450
                        }
2451
 
2452
                        if($userProgressTopic->progress >= 100) {
2453
                            $closeTopic = true;
2454
                        }
2455
                    }
2456
 
2457
 
2458
                    $data = [
2459
                        'message' => 'LABEL_THE_USER_PROGRESS_FOR_THIS_SLIDE_HAS_BEEN_COMPLETED',
2460
 
2461
                    ];
2462
 
2463
                    if($closeCapsule) {
173 efrain 2464
                        $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 2465
                    }
2466
 
2467
 
2468
                    if($closeTopic) {
173 efrain 2469
                        $data['link_close_topic'] = $this->url()->fromRoute('microlearning/sync', ['operation' => 'topic-close', 'topic_uuid' => $topic->uuid], ['force_canonical' => true]);
167 efrain 2470
                    }
2471
 
2472
 
2473
                    return new JsonModel([
2474
                        'success' => true,
2475
                        'data' => $data
2476
                    ]);
2477
 
2478
 
2479
 
2480
 
2481
                } else if($operation == 'capsule-close') {
2482
 
2483
                    if(empty($capsule_uuid ) || empty($topic_uuid)) {
2484
                        return new JsonModel([
2485
                            'success' => false,
2486
                            'data' => 'ERROR_INVALID_PARAMETERS'
2487
                        ]);
2488
                    }
2489
 
2490
 
283 www 2491
                    $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
167 efrain 2492
                    $topic = $topicMapper->fetchOneByUuid($topic_uuid);
2493
 
2494
                    if(!$topic) {
2495
                        return new JsonModel([
2496
                            'success' => false,
2497
                            'data' => 'ERROR_TOPIC_NOT_FOUND'
2498
                        ]);
2499
                    }
2500
 
2501
 
2502
 
2503
                    if(!in_array($topic->id, $accessGrantedIds->topics)) {
2504
                        return new JsonModel([
2505
                            'success' => false,
2506
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
2507
                        ]);
2508
                    }
2509
 
283 www 2510
                    $capsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
167 efrain 2511
                    $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
2512
 
2513
                    if(!$capsule) {
2514
                        return new JsonModel([
2515
                            'success' => false,
2516
                            'data' => 'ERROR_CAPSULE_NOT_FOUND'
2517
                        ]);
2518
                    }
2519
 
2520
                    if(!in_array($capsule->id, $accessGrantedIds->capsules) || $capsule->topic_id != $topic->id) {
2521
                        return new JsonModel([
2522
                            'success' => false,
2523
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_CAPSULE'
2524
                        ]);
2525
                    }
2526
 
2527
 
283 www 2528
                    $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
167 efrain 2529
 
283 www 2530
                    $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
167 efrain 2531
                    $updated_on = $userProgressMapper->getDatebaseNow();
2532
 
2533
                    $userProgressCapsule = $userProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule->id);
2534
                    if(!$userProgressCapsule) {
2535
                        return new JsonModel([
2536
                            'success' => false,
2537
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_CAPSULE_NOT_FOUND'
2538
                        ]);
2539
                    }
2540
 
2541
                    if($userProgressCapsule->completed) {
2542
                        return new JsonModel([
2543
                            'success' => false,
2544
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_CAPSULE_ALREADY_CLOSED'
2545
                        ]);
2546
                    }
2547
 
2548
 
2549
                    $userProgressCapsule->completed = 1;
2550
                    $userProgressCapsule->updated_on = $updated_on;
2551
 
2552
                    if($userProgressMapper->update($userProgressCapsule)) {
225 efrain 2553
 
2554
 
283 www 2555
                        $userLog = new MicrolearningUserLog();
2556
                        $userLog->activity      = MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE;
227 efrain 2557
                        $userLog->user_id       = $currentUser->id;
2558
                        $userLog->company_id    = $capsule->company_id;
2559
                        $userLog->topic_id      = $capsule->topic_id;
2560
                        $userLog->capsule_id    = $capsule->id;
2561
                        $userLog->added_on      = $updated_on;
225 efrain 2562
 
2563
                        if(!$userLogMapper->insert($userLog)) {
2564
                            return new JsonModel([
2565
                                'success' => false,
2566
                                'data' => $userProgressMapper->getError()
2567
                            ]);
2568
                        }
2569
 
2570
 
167 efrain 2571
                        return new JsonModel([
2572
                            'success' => true,
2573
                            'data' => 'LABEL_THE_USER_PROGRESS_FOR_THIS_CAPSULE_HAS_BEEN_COMPLETED'
2574
                        ]);
2575
                    } else {
2576
                        return new JsonModel([
2577
                            'success' => false,
2578
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_CAPSULE_COULD_NOT_BE_COMPLETED'
2579
                        ]);
2580
                    }
2581
 
2582
 
2583
 
2584
 
2585
 
2586
 
2587
 
2588
                } else if($operation == 'topic-close') {
174 efrain 2589
                    if(empty($topic_uuid)) {
167 efrain 2590
                        return new JsonModel([
2591
                            'success' => false,
2592
                            'data' => 'ERROR_INVALID_PARAMETERS'
2593
                        ]);
2594
                    }
2595
 
2596
 
283 www 2597
                    $topicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
167 efrain 2598
                    $topic = $topicMapper->fetchOneByUuid($topic_uuid);
2599
 
2600
                    if(!$topic) {
2601
                        return new JsonModel([
2602
                            'success' => false,
2603
                            'data' => 'ERROR_TOPIC_NOT_FOUND'
2604
                        ]);
2605
                    }
2606
 
2607
 
2608
 
2609
                    if(!in_array($topic->id, $accessGrantedIds->topics)) {
2610
                        return new JsonModel([
2611
                            'success' => false,
2612
                            'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS_TO_THIS_TOPIC'
2613
                        ]);
2614
                    }
2615
 
2616
 
2617
 
2618
 
283 www 2619
                    $userLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
167 efrain 2620
 
283 www 2621
                    $userProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
167 efrain 2622
                    $updated_on = $userProgressMapper->getDatebaseNow();
2623
 
2624
                    $userProgressTopic = $userProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic->id);
2625
                    if(!$userProgressTopic) {
2626
                        return new JsonModel([
2627
                            'success' => false,
2628
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_TOPIC_NOT_FOUND'
2629
                        ]);
2630
                    }
2631
 
2632
                    if($userProgressTopic->completed) {
2633
                        return new JsonModel([
2634
                            'success' => false,
2635
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_TOPIC_ALREADY_CLOSED'
2636
                        ]);
2637
                    }
2638
 
2639
 
2640
                    $userProgressTopic->completed = 1;
2641
                    $userProgressTopic->updated_on = $updated_on;
2642
 
2643
                    if($userProgressMapper->update($userProgressTopic)) {
226 efrain 2644
 
283 www 2645
                        $userLog = new MicrolearningUserLog();
2646
                        $userLog->activity      = MicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC;
227 efrain 2647
                        $userLog->user_id       = $currentUser->id;
2648
                        $userLog->company_id    = $topic->company_id;
2649
                        $userLog->topic_id      = $topic->id;
2650
                        $userLog->added_on      = $updated_on;
226 efrain 2651
 
2652
                        if(!$userLogMapper->insert($userLog)) {
2653
                            return new JsonModel([
2654
                                'success' => false,
2655
                                'data' => $userProgressMapper->getError()
2656
                            ]);
2657
                        }
2658
 
167 efrain 2659
                        return new JsonModel([
2660
                            'success' => true,
2661
                            'data' => 'LABEL_THE_USER_PROGRESS_FOR_THIS_TOPIC_HAS_BEEN_COMPLETED'
2662
                        ]);
2663
                    } else {
2664
                        return new JsonModel([
2665
                            'success' => false,
2666
                            'data' => 'ERROR_THE_USER_PROGRESS_FOR_THIS_TOPIC_COULD_NOT_BE_COMPLETED'
2667
                        ]);
2668
                    }
2669
                }
2670
 
2671
 
2672
 
2673
 
2674
            } else {
2675
 
2676
                return new JsonModel([
2677
                    'success' => false,
2678
                    'data' => 'ERROR_OPERATION_UNKNOWN'
2679
                ]);
2680
 
2681
 
2682
            }
2683
 
2684
        } else {
2685
            return new JsonModel([
2686
                'success' => false,
2687
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
2688
            ]);
2689
        }
2690
    }
2691
 
2692
 
119 efrain 2693
    /**
2694
     *
2695
     * @return \LeadersLinked\Controller\MicrolearningUserAccessGrantedIds
2696
     */
2697
    private function getAccessGranted()
2698
    {
2699
        $currentUserPlugin = $this->plugin('currentUserPlugin');
2700
        $currentUser = $currentUserPlugin->getUser();
2701
 
283 www 2702
        $capsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
119 efrain 2703
        $now = $capsuleUserMapper->getDatebaseNow();
2704
 
2705
        $records = $capsuleUserMapper->fetchAllActiveByUserId($currentUser->id);
2706
 
2707
        $accessGrantedIds = new MicrolearningUserAccessGrantedIds();
2708
 
2709
 
2710
        foreach($records as $record)
2711
        {
283 www 2712
            if($record->access != MicrolearningCapsuleUser::ACCESS_UNLIMITED && $record->access != MicrolearningCapsuleUser::ACCESS_PAY_PERIOD) {
119 efrain 2713
                continue;
2714
            }
283 www 2715
            if($record->access == MicrolearningCapsuleUser::ACCESS_PAY_PERIOD) {
119 efrain 2716
                if($now < $record->paid_from || $now > $record->paid_to) {
2717
                    continue;
2718
                }
2719
            }
2720
 
2721
 
2722
            if(!in_array($record->company_id, $accessGrantedIds->companies )) {
2723
                array_push($accessGrantedIds->companies, $record->company_id);
2724
            }
2725
 
2726
            if(!in_array($record->topic_id, $accessGrantedIds->topics )) {
2727
                array_push( $accessGrantedIds->topics, $record->topic_id);
2728
            }
2729
 
2730
            if(!in_array($record->capsule_id, $accessGrantedIds->capsules)) {
2731
                array_push( $accessGrantedIds->capsules, $record->capsule_id);
2732
            }
2733
        }
2734
 
2735
        return $accessGrantedIds;
2736
    }
2737
 
2738
 
2739
}