Proyectos de Subversion LeadersLinked - Services

Rev

Rev 661 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
283 www 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Mapper;
5
 
6
use LeadersLinked\Mapper\Common\MapperCommon;
7
use Laminas\Db\Adapter\AdapterInterface;
8
use LeadersLinked\Model\MicrolearningUserProgress;
9
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
10
use Laminas\Db\Sql\Expression;
590 stevensc 11
use LeadersLinked\Mapper\MicrolearningSlideMapper;
12
use LeadersLinked\Mapper\MicrolearningTopicCapsuleMapper;
283 www 13
class MicrolearningUserProgressMapper extends MapperCommon
14
{
15
    const _TABLE = 'tbl_microlearning_user_progress';
16
 
17
    /**
18
     *
19
     * @var MicrolearningUserProgressMapper
20
     */
21
    private static $_instance;
658 stevensc 22
 
23
    private $topicCapsuleMapper;
24
 
25
    private $slideMapper;
283 www 26
 
27
    /**
28
     *
29
     * @param AdapterInterface $adapter
30
     */
31
    private function __construct($adapter)
32
    {
33
        parent::__construct($adapter);
658 stevensc 34
        $this->topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($adapter);
35
        $this->slideMapper = MicrolearningSlideMapper::getInstance($adapter);
283 www 36
    }
37
 
38
    /**
39
     *
40
     * @param AdapterInterface $adapter
41
     * @return MicrolearningUserProgressMapper
42
     */
43
    public static function getInstance($adapter)
44
    {
45
        if(self::$_instance == null) {
46
            self::$_instance = new MicrolearningUserProgressMapper($adapter);
47
        }
48
        return self::$_instance;
49
    }
50
 
51
    /**
52
     *
53
     * @param int $user_id
54
     * @return MicrolearningUserProgress[]
55
     */
56
    public function fetchAllByUserId($user_id)
57
    {
58
        $prototype = new MicrolearningUserProgress();
59
 
60
        $select = $this->sql->select(self::_TABLE);
61
        $select->where->equalTo('user_id', $user_id);
62
 
63
        return $this->executeFetchAllObject($select, $prototype);
64
    }
65
 
66
    /**
67
     *
68
     * @return int[]
69
     */
70
    public function fetchAllDistinctUserIds()
71
    {
72
        $user_ids = [];
73
 
74
        $select = $this->sql->select(self::_TABLE);
75
        $select->columns(['user_id' => new Expression('DISTINCT(user_id)')]);
76
 
77
        $records = $this->executeFetchAllArray($select);
78
        foreach ($records as $record)
79
        {
80
            array_push($user_ids, $record['user_id']);
81
        }
82
 
83
        return $user_ids;
84
 
85
    }
86
 
87
    /**
88
     *
89
     * @return MicrolearningUserProgress[]
90
     */
91
    public function fetchAllTopics()
92
    {
93
        $prototype = new MicrolearningUserProgress();
94
        $select = $this->sql->select(self::_TABLE);
95
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_TOPIC);
96
 
97
        return $this->executeFetchAllObject($select, $prototype);
98
    }
99
 
100
    /**
101
     *
102
     * @return MicrolearningUserProgress[]
103
     */
104
    public function fetchAllCapsules()
105
    {
106
        $prototype = new MicrolearningUserProgress();
107
        $select = $this->sql->select(self::_TABLE);
108
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
109
 
110
        return $this->executeFetchAllObject($select, $prototype);
111
    }
112
 
113
    /**
114
     *
115
     * @param int $user_id
116
     * @param int $slide_id
117
     * @return MicrolearningUserProgress
118
     */
119
    public function fetchOneByUserIdAndSlideId($user_id, $slide_id)
120
    {
121
        $prototype = new MicrolearningUserProgress();
122
 
123
        $select = $this->sql->select(self::_TABLE);
124
        $select->where->equalTo('user_id', $user_id);
125
        $select->where->equalTo('slide_id', $slide_id);
126
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
127
        $select->limit(1);
128
 
129
        return $this->executeFetchOneObject($select, $prototype);
130
    }
590 stevensc 131
 
132
    /**
133
     * Fetch all progress data by user id and slide id
134
     * @param int $userId
135
     * @param int $slideId
136
     * @return MicrolearningUserProgress[]
137
     */
138
    public function fetchAllProgressDataByUserIdAndSlideId($userId, $slideId)
139
    {
140
        $select = $this->sql->select();
141
        $select->from(self::_TABLE);
142
        $select->where->equalTo('user_id', $userId);
143
        $select->where->equalTo('slide_id', $slideId);
144
    }
283 www 145
 
146
    /**
147
     *
148
     * @param int $user_id
149
     * @param int[] $capsule_ids
150
     * @return MicrolearningUserProgress
151
     */
152
    public function fetchOneLastCapsuleInProgressByUserIdAndCapsuleIds($user_id, $capsule_ids)
153
    {
154
        $capsule_ids = empty($capsule_ids) ? [0] : $capsule_ids;
155
 
156
        $prototype = new MicrolearningUserProgress();
157
 
158
        $select = $this->sql->select(self::_TABLE);
159
        $select->where->equalTo('user_id', $user_id);
160
        $select->where->in('capsule_id', $capsule_ids);
161
        $select->where->greaterThan('progress', 0);
162
        $select->where->lessThan('progress', 100);
163
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
164
        $select->order('updated_on');
165
        $select->limit(1);
166
 
167
 
168
        //echo $select->getSqlString($this->adapter->platform);
169
 
170
        return $this->executeFetchOneObject($select, $prototype);
171
    }
636 stevensc 172
 
173
    public function fetchOneLastCapsuleInProgressByUserIdAndTopicIds($user_id, $topic_ids)
174
    {
175
        $prototype = new MicrolearningUserProgress();
176
        $select = $this->sql->select(self::_TABLE);
177
        $select->where->equalTo('user_id', $user_id);
178
        $select->where->in('topic_id', $topic_ids);
179
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
180
        $select->where->greaterThan('progress', 0);
181
        $select->where->lessThan('progress', 100);
182
        $select->order('updated_on');
183
        $select->limit(1);
184
 
185
        return $this->executeFetchOneObject($select, $prototype);
186
    }
283 www 187
 
188
    /**
189
     *
190
     * @param int $user_id
191
     * @param int $capsule_id
192
     * @return MicrolearningUserProgress
193
     */
194
    public function fetchOneByUseridAndCapsuleId($user_id, $capsule_id)
195
    {
196
        $prototype = new MicrolearningUserProgress();
197
 
198
        $select = $this->sql->select(self::_TABLE);
199
        $select->where->equalTo('user_id', $user_id);
200
        $select->where->equalTo('capsule_id', $capsule_id);
201
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
202
        $select->limit(1);
203
 
204
 
205
        //echo $select->getSqlString($this->adapter->platform);
206
 
207
        return $this->executeFetchOneObject($select, $prototype);
208
    }
209
 
210
    /**
211
     *
212
     * @param int $user_id
213
     * @param int $topic_id
214
     * @return MicrolearningUserProgress
215
     */
216
    public function fetchOneByUserIdAndTopicId($user_id, $topic_id)
217
    {
218
        $prototype = new MicrolearningUserProgress();
219
 
220
        $select = $this->sql->select(self::_TABLE);
221
        $select->where->equalTo('user_id', $user_id);
222
        $select->where->equalTo('topic_id', $topic_id);
223
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_TOPIC);
224
        $select->limit(1);
225
 
226
        return $this->executeFetchOneObject($select, $prototype);
227
    }
228
 
229
 
230
    /**
231
     *
232
     * @param int $user_id
233
     * @param int $topic_id
234
     * @return int
235
     */
236
    public function  fetchCountCapsulesStartedByIdAndTopicId($user_id, $topic_id)
237
    {
238
        $select = $this->sql->select(self::_TABLE);
239
        $select->columns(['total' => new Expression('COUNT(*)')]);
240
        $select->where->equalTo('user_id', $user_id);
241
        $select->where->equalTo('topic_id', $topic_id);
242
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
243
        $select->where->greaterThan('progress', 0);
244
        $select->where->lessThan('progress', 100);
245
        $select->where->equalTo('completed', 0);
246
        $select->limit(1);
247
 
248
        $record = $this->executeFetchOneArray($select);
249
        return $record['total'];
250
    }
251
 
252
 
253
    /**
254
     *
255
     * @param int $user_id
256
     * @param int $topic_id
257
     * @return int
258
     */
259
    public function  fetchCountCapsulesCompletedByIdAndTopicId($user_id, $topic_id)
260
    {
261
        $select = $this->sql->select(self::_TABLE);
262
        $select->columns(['total' => new Expression('COUNT(*)')]);
263
        $select->where->equalTo('user_id', $user_id);
264
        $select->where->equalTo('topic_id', $topic_id);
265
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
266
        $select->where->equalTo('completed', 1);
267
        $select->limit(1);
268
 
269
        $record = $this->executeFetchOneArray($select);
270
        return $record['total'];
271
    }
272
 
273
 
274
 
275
 
276
    /**
277
     *
278
     * @param int $user_id
279
     * @param int $topic_id
280
     * @return int
281
     */
282
    public function fetchCountCapsulesCompletedWithReturningByIdAndTopicId($user_id, $topic_id)
283
    {
284
        $select = $this->sql->select(self::_TABLE);
285
        $select->columns(['total' => new Expression('COUNT(*)')]);
286
        $select->where->equalTo('user_id', $user_id);
287
        $select->where->equalTo('topic_id', $topic_id);
288
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
289
        $select->where->equalTo('completed', 1);
290
        $select->where->notEqualTo('returning_after_completed', 0);
291
        $select->limit(1);
292
 
293
        $record = $this->executeFetchOneArray($select);
294
        return $record['total'];
295
    }
296
 
297
    /**
298
     *
299
     * @param int $user_id
300
     * @param int $topic_id
301
     * @return int
302
     */
303
    public function fetchCountCapsulesCompletedWithoutReturningByIdAndTopicId($user_id, $topic_id)
304
    {
305
        $select = $this->sql->select(self::_TABLE);
306
        $select->columns(['total' => new Expression('COUNT(*)')]);
307
        $select->where->equalTo('user_id', $user_id);
308
        $select->where->equalTo('topic_id', $topic_id);
309
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
310
        $select->where->equalTo('completed', 1);
311
        $select->where->equalTo('returning_after_completed', 0);
312
        $select->limit(1);
313
 
314
        $record = $this->executeFetchOneArray($select);
315
        return $record['total'];
316
    }
317
 
318
 
319
    /**
320
     *
321
     * @param int $user_id
322
     * @param int $capsule_id
323
     * @return int
324
     */
325
    public function fetchCountAllSlideViewedByUserIdAndCapsuleId($user_id, $capsule_id)
326
    {
327
 
328
        $select = $this->sql->select(self::_TABLE);
329
        $select->columns(['total' => new Expression('COUNT(*)')]);
330
        $select->where->equalTo('user_id', $user_id);
331
        $select->where->equalTo('capsule_id', $capsule_id);
332
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
333
        $select->limit(1);
334
 
335
        $record = $this->executeFetchOneArray($select);
336
        return $record['total'];
337
    }
338
 
339
    /**
340
     *
341
     * @param int $user_id
342
     * @param int $topic_id
343
     * @param int $capsule_id
344
     * @return int
345
     */
346
    public function fetchCountAllSlideCompletedByUserIdAndTopicIdAndCapsuleId($user_id, $topic_id, $capsule_id)
347
    {
348
 
349
        $select = $this->sql->select(self::_TABLE);
350
        $select->columns(['total' => new Expression('COUNT(*)')]);
351
        $select->where->equalTo('user_id', $user_id);
352
        $select->where->equalTo('topic_id', $topic_id);
353
        $select->where->equalTo('capsule_id', $capsule_id);
354
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
355
        $select->where->equalTo('completed', 1);
356
        $select->limit(1);
357
 
358
        $record = $this->executeFetchOneArray($select);
359
        return $record['total'];
360
    }
361
 
362
 
363
    /**
364
     *
365
     * @param int $user_id
366
     * @param int $capsule_id
367
     * @return int
368
     */
369
    public function fetchCountAllSlideCompletedByUserIdAndCapsuleId($user_id, $capsule_id)
370
    {
371
 
372
        $select = $this->sql->select(self::_TABLE);
373
        $select->columns(['total' => new Expression('COUNT(*)')]);
374
        $select->where->equalTo('user_id', $user_id);
375
        $select->where->equalTo('capsule_id', $capsule_id);
376
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
377
        $select->where->equalTo('completed', 1);
378
        $select->limit(1);
379
 
380
        //echo $select->getSqlString($this->adapter->platform);
381
 
382
        $record = $this->executeFetchOneArray($select);
383
        return $record['total'];
384
    }
385
 
386
    /**
387
     *
388
     * @param int $user_id
389
     * @param int $capsule_id
390
     * @return int
391
     */
392
    public function fetchCountAllSlideCompletedByUserIdAndTopicId($user_id, $topic_id)
393
    {
394
 
395
        $select = $this->sql->select(self::_TABLE);
396
        $select->columns(['total' => new Expression('COUNT(*)')]);
397
        $select->where->equalTo('user_id', $user_id);
398
        $select->where->equalTo('topic_id', $topic_id);
399
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
400
        $select->where->equalTo('completed', 1);
401
        $select->limit(1);
402
 
403
       // echo $select->getSqlString($this->adapter->platform) . PHP_EOL;
404
 
405
        $record = $this->executeFetchOneArray($select);
406
        return $record['total'];
407
    }
408
 
409
    /**
410
     *
411
     * @param int $company_id
412
     * @param int $user_id
413
     * @return int
414
     */
415
    public function fetchCountAllSlideCompletedByCompanyIdAndUserId($company_id, $user_id)
416
    {
417
 
418
        $select = $this->sql->select(self::_TABLE);
419
        $select->columns(['total' => new Expression('COUNT(*)')]);
420
        $select->where->equalTo('company_id', $company_id);
421
        $select->where->equalTo('user_id', $user_id);
422
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
423
        $select->where->equalTo('completed', 1);
424
        $select->limit(1);
425
 
426
        $record = $this->executeFetchOneArray($select);
427
        return $record['total'];
428
    }
429
 
430
 
431
    /**
432
     *
433
     * @param int $user_id
434
     * @param int $topic_id
435
     * @return int
436
     */
437
    public function fetchCountAllSlideViewedByUserIdAndTopicId($user_id, $topic_id)
438
    {
439
 
440
        $select = $this->sql->select(self::_TABLE);
441
        $select->columns(['total' => new Expression('COUNT(*)')]);
442
        $select->where->equalTo('user_id', $user_id);
443
        $select->where->equalTo('topic_id', $topic_id);
444
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
445
        $select->limit(1);
446
 
447
        $record = $this->executeFetchOneArray($select);
448
        return $record['total'];
449
    }
450
 
451
    /**
452
     *
453
     * @param MicrolearningUserProgress $userProgress
454
     * return boolean
455
     */
456
    public function insert($userProgress)
457
    {
458
        $hydrator = new ObjectPropertyHydrator();
459
        $values = $hydrator->extract($userProgress);
460
        $values = $this->removeEmpty($values);
461
 
462
        $values['added_on'] = $userProgress->added_on;
463
        $values['updated_on'] = $userProgress->updated_on;
464
 
465
        $insert = $this->sql->insert(self::_TABLE);
466
        $insert->values($values);
467
 
468
        //echo $insert->getSqlString($this->adapter->platform); exit;
469
 
470
        $result = $this->executeInsert($insert);
471
        if($result) {
472
            $userProgress->id = $this->lastInsertId;
473
        }
474
        return $result;
475
    }
476
 
477
 
478
    /**
479
     *
480
     * @param MicrolearningUserProgress $userProgress
481
     * return boolean
482
     */
483
    public function update($userProgress)
484
    {
485
        $hydrator = new ObjectPropertyHydrator();
486
        $values = $hydrator->extract($userProgress);
487
        $values = $this->removeEmpty($values);
488
 
489
 
490
        $values['added_on'] = $userProgress->added_on;
491
        $values['updated_on'] = $userProgress->updated_on;
492
 
493
 
494
        $update = $this->sql->update(self::_TABLE);
495
        $update->set($values);
496
        $update->where->equalTo('id', $userProgress->id);
497
 
498
        //echo $update->getSqlString($this->adapter->platform) . PHP_EOL;
499
 
500
        return $this->executeUpdate($update);
501
    }
626 stevensc 502
 
503
    /**
504
     * Delete all user progress by topic id
505
     * @param int $topic_id
506
     * @return int
507
     */
508
    public function deleteAllTopicProgressByTopicId($topic_id)
509
    {
510
        $delete = $this->sql->delete(self::_TABLE);
511
        $delete->where->equalTo('topic_id', $topic_id);
512
        $delete->where->equalTo('type', MicrolearningUserProgress::TYPE_TOPIC);
513
        return $this->executeDelete($delete);
514
    }
632 stevensc 515
 
516
    /**
517
     * Deletes all user progress by capsule id
518
     * @param int $capsule_id
519
     * @return int
520
     */
521
    public function deleteAllByCapsuleId($capsule_id)
522
    {
523
        $delete = $this->sql->delete(self::_TABLE);
524
        $delete->where->equalTo('capsule_id', $capsule_id);
525
        $delete->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
526
        $delete->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
527
        return $this->executeDelete($delete);
528
    }
283 www 529
 
530
 
531
    /**
532
     *
533
     * @param int $userId
534
     * @return int
535
     */
536
    public function getCountCapsulesCompletedByUserId($userId)
537
    {
538
        $select = $this->sql->select(self::_TABLE);
539
        $select->columns(['total' => new Expression('COUNT(*)')] );
540
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
541
        $select->where->equalTo('user_id', $userId);
542
        $select->where->equalTo('completed', 1);
543
 
544
        $record = $this->executeFetchOneArray($select);
545
 
546
        return $record['total'];
547
 
548
    }
549
 
657 stevensc 550
    /**
658 stevensc 551
     * Update the progress of a slide
552
     * @param int $user_id
662 stevensc 553
     * @param int $company_id
658 stevensc 554
     * @param int $slide_id
555
     * @param int $capsule_id
661 stevensc 556
     * @param int $topic_id
662 stevensc 557
     * @return bool
657 stevensc 558
     */
662 stevensc 559
    public function updateSlideProgress($user_id, $company_id, $slide_id, $capsule_id, $topic_id){
658 stevensc 560
        $userProgress = $this->fetchOneByUserIdAndSlideId($user_id, $slide_id);
662 stevensc 561
        $now = date('Y-m-d H:i:s');
562
 
563
        if (!$userProgress) {
564
            // Crear nuevo progreso
658 stevensc 565
            $userProgress = new MicrolearningUserProgress();
566
            $userProgress->user_id = $user_id;
662 stevensc 567
            $userProgress->company_id = $company_id;
658 stevensc 568
            $userProgress->slide_id = $slide_id;
569
            $userProgress->capsule_id = $capsule_id;
661 stevensc 570
            $userProgress->topic_id = $topic_id;
658 stevensc 571
            $userProgress->type = MicrolearningUserProgress::TYPE_SLIDE;
657 stevensc 572
            $userProgress->progress = 100;
573
            $userProgress->completed = 1;
662 stevensc 574
            $userProgress->added_on = $now;
575
            $userProgress->updated_on = $now;
658 stevensc 576
            return $this->insert($userProgress);
577
        } else {
662 stevensc 578
            // Actualizar progreso existente
658 stevensc 579
            $userProgress->progress = 100;
580
            $userProgress->completed = 1;
662 stevensc 581
            $userProgress->updated_on = $now;
657 stevensc 582
            return $this->update($userProgress);
590 stevensc 583
        }
584
    }
585
 
657 stevensc 586
    /**
658 stevensc 587
     * Get the number of slides completed by a capsule
588
     * @param int $capsule_id
589
     * @return int
657 stevensc 590
     */
658 stevensc 591
    public function getSlidesCompletedByCapsuleId($capsule_id){
592
        // Get total slides in the capsule
593
        $totalSlides = $this->slideMapper->fetchTotalCountByCapsuleId($capsule_id);
590 stevensc 594
 
658 stevensc 595
        // Get all slides completed
596
        $select = $this->sql->select(self::_TABLE);
597
        $select->columns(['total' => new Expression('COUNT(*)')]);
598
        $select->where->equalTo('capsule_id', $capsule_id);
599
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
600
        $select->where->equalTo('completed', 1);
657 stevensc 601
 
658 stevensc 602
        $record = $this->executeFetchOneArray($select);
603
        return (int)$record['total'];
590 stevensc 604
    }
605
 
656 stevensc 606
    /**
658 stevensc 607
     * Update the progress of a capsule
608
     * @param int $user_id
662 stevensc 609
     * @param int $company_id
661 stevensc 610
     * @param int $slide_id
658 stevensc 611
     * @param int $capsule_id
612
     * @param int $topic_id
613
     * @return int
656 stevensc 614
     */
662 stevensc 615
    public function updateCapsuleProgress($user_id, $company_id, $slide_id, $capsule_id, $topic_id){
658 stevensc 616
        // Get total slides in the capsule
617
        $totalSlides = $this->slideMapper->fetchTotalCountByCapsuleId($capsule_id);
653 stevensc 618
 
658 stevensc 619
        // Get all slides completed
620
        $slidesCompleted = $this->getSlidesCompletedByCapsuleId($capsule_id);
590 stevensc 621
 
658 stevensc 622
        $progress = ($slidesCompleted * 100) / $totalSlides;
623
        $progress = min($progress, 100);
624
        $completed = $progress == 100 ? 1 : 0;
625
 
626
        $userProgress = $this->fetchOneByUserIdAndCapsuleId($user_id, $capsule_id);
627
        if(!$userProgress){
628
            $userProgress = new MicrolearningUserProgress();
629
            $userProgress->user_id = $user_id;
662 stevensc 630
            $userProgress->company_id = $company_id;
661 stevensc 631
            $userProgress->slide_id = $slide_id;
658 stevensc 632
            $userProgress->capsule_id = $capsule_id;
633
            $userProgress->topic_id = $topic_id;
634
            $userProgress->type = MicrolearningUserProgress::TYPE_CAPSULE;
635
            $userProgress->progress = $progress;
636
            $userProgress->completed = $completed;
637
            $userProgress->total_slides = $totalSlides;
638
            $userProgress->view_slides = $slidesCompleted;
639
            $userProgress->added_on = date('Y-m-d H:i:s');
640
            $userProgress->updated_on = date('Y-m-d H:i:s');
641
            return $this->insert($userProgress);
642
        } else {
643
            $userProgress->progress = $progress;
644
            $userProgress->completed = $completed;
645
            $userProgress->total_slides = $totalSlides;
646
            $userProgress->view_slides = $slidesCompleted;
647
            $userProgress->updated_on = date('Y-m-d H:i:s');
648
            return $this->update($userProgress);
649
        }
590 stevensc 650
    }
651
 
657 stevensc 652
    /**
658 stevensc 653
     * Get the number of capsules completed by a topic
654
     * @param int $topic_id
655
     * @return int
657 stevensc 656
     */
658 stevensc 657
    public function getCapsulesCompletedByTopicId($topic_id){
590 stevensc 658
        $select = $this->sql->select(self::_TABLE);
657 stevensc 659
        $select->columns(['total' => new Expression('COUNT(*)')]);
590 stevensc 660
        $select->where->equalTo('topic_id', $topic_id);
661
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
662
        $select->where->equalTo('completed', 1);
663
        $record = $this->executeFetchOneArray($select);
658 stevensc 664
        return (int)$record['total'];
590 stevensc 665
    }
658 stevensc 666
 
667
    /**
668
     * Update the progress of a topic
669
     * @param int $user_id
662 stevensc 670
     * @param int $company_id
661 stevensc 671
     * @param int $slide_id
672
     * @param int $capsule_id
658 stevensc 673
     * @param int $topic_id
674
     * @return void
675
     */
662 stevensc 676
    public function updateTopicProgress($user_id, $company_id, $slide_id, $capsule_id, $topic_id){
658 stevensc 677
        // Get all capsules in the topic
678
        $totalCapsules = $this->topicCapsuleMapper->fetchTotalCountByTopicId($topic_id);
679
 
680
        // Get all capsules completed
681
        $completedCapsules = $this->getCapsulesCompletedByTopicId($topic_id);
682
 
683
        // Calculate progress and completed
684
        $progress = ($completedCapsules * 100) / $totalCapsules;
685
        $progress = min($progress, 100);
686
        $completed = $progress == 100 ? 1 : 0;
687
 
688
        $userProgress = $this->fetchOneByUserIdAndTopicId($user_id, $topic_id);
689
        if(!$userProgress){
690
            $userProgress = new MicrolearningUserProgress();
691
            $userProgress->user_id = $user_id;
662 stevensc 692
            $userProgress->company_id = $company_id;
661 stevensc 693
            $userProgress->slide_id = $slide_id;
694
            $userProgress->capsule_id = $capsule_id;
658 stevensc 695
            $userProgress->topic_id = $topic_id;
696
            $userProgress->type = MicrolearningUserProgress::TYPE_TOPIC;
697
            $userProgress->progress = $progress;
698
            $userProgress->completed = $completed;
699
            $userProgress->added_on = date('Y-m-d H:i:s');
700
            $userProgress->updated_on = date('Y-m-d H:i:s');
701
            return $this->insert($userProgress);
702
        } else {
703
            $userProgress->progress = $progress;
704
            $userProgress->completed = $completed;
705
            $userProgress->updated_on = date('Y-m-d H:i:s');
706
            return $this->update($userProgress);
707
        }
708
    }
283 www 709
}