Proyectos de Subversion LeadersLinked - Services

Rev

Rev 657 | Rev 661 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 657 Rev 658
Línea 8... Línea 8...
8
use LeadersLinked\Model\MicrolearningUserProgress;
8
use LeadersLinked\Model\MicrolearningUserProgress;
9
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
9
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
10
use Laminas\Db\Sql\Expression;
10
use Laminas\Db\Sql\Expression;
11
use LeadersLinked\Mapper\MicrolearningSlideMapper;
11
use LeadersLinked\Mapper\MicrolearningSlideMapper;
12
use LeadersLinked\Mapper\MicrolearningTopicCapsuleMapper;
12
use LeadersLinked\Mapper\MicrolearningTopicCapsuleMapper;
13
 
-
 
14
class MicrolearningUserProgressMapper extends MapperCommon
13
class MicrolearningUserProgressMapper extends MapperCommon
15
{
14
{
16
    const _TABLE = 'tbl_microlearning_user_progress';
15
    const _TABLE = 'tbl_microlearning_user_progress';
Línea 17... Línea 16...
17
    
16
    
18
    /**
17
    /**
19
     *
18
     *
20
     * @var MicrolearningUserProgressMapper
19
     * @var MicrolearningUserProgressMapper
21
     */
20
     */
-
 
21
    private static $_instance;
-
 
22
 
-
 
23
    private $topicCapsuleMapper;
-
 
24
 
Línea 22... Línea 25...
22
    private static $_instance;
25
    private $slideMapper;
23
    
26
    
24
    /**
27
    /**
25
     *
28
     *
26
     * @param AdapterInterface $adapter
29
     * @param AdapterInterface $adapter
27
     */
30
     */
28
    private function __construct($adapter)
31
    private function __construct($adapter)
-
 
32
    {
-
 
33
        parent::__construct($adapter);
29
    {
34
        $this->topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($adapter);
Línea 30... Línea 35...
30
        parent::__construct($adapter);
35
        $this->slideMapper = MicrolearningSlideMapper::getInstance($adapter);
31
    }
36
    }
32
    
37
    
Línea 77... Línea 82...
77
        
82
        
Línea 78... Línea 83...
78
        return $user_ids;
83
        return $user_ids;
Línea 79... Línea -...
79
 
-
 
80
    }
-
 
81
    
-
 
82
 
-
 
83
    
84
 
84
    
85
    }
85
    
86
    
86
    /**
87
    /**
87
     *
88
     *
88
     * @return MicrolearningUserProgress[]
89
     * @return MicrolearningUserProgress[]
89
     */
90
     */
90
    public function fetchAllTopics()
-
 
91
    {
91
    public function fetchAllTopics()
92
        $prototype = new MicrolearningUserProgress();
92
    {
Línea 93... Línea -...
93
        
-
 
94
        $select = $this->sql->select(self::_TABLE);
-
 
95
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_TOPIC);
93
        $prototype = new MicrolearningUserProgress();
96
        
94
        $select = $this->sql->select(self::_TABLE);
Línea 97... Línea -...
97
        //echo $select->getSqlString($this->adapter->platform); exit;
-
 
98
        
95
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_TOPIC);
99
        return $this->executeFetchAllObject($select, $prototype);
96
        
100
    }
97
        return $this->executeFetchAllObject($select, $prototype);
101
    
98
    }
102
    
99
    
103
    /**
100
    /**
104
     *
101
     *
105
     * @return MicrolearningUserProgress[]
-
 
106
     */
102
     * @return MicrolearningUserProgress[]
107
    public function fetchAllCapsules()
103
     */
Línea 108... Línea 104...
108
    {
104
    public function fetchAllCapsules()
109
        $prototype = new MicrolearningUserProgress();
105
    {
Línea 110... Línea -...
110
        
-
 
111
        $select = $this->sql->select(self::_TABLE);
106
        $prototype = new MicrolearningUserProgress();
112
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
107
        $select = $this->sql->select(self::_TABLE);
113
        
108
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
114
        return $this->executeFetchAllObject($select, $prototype);
109
        
115
    }
110
        return $this->executeFetchAllObject($select, $prototype);
Línea 127... Línea 122...
127
        
122
        
128
        $select = $this->sql->select(self::_TABLE);
123
        $select = $this->sql->select(self::_TABLE);
129
        $select->where->equalTo('user_id', $user_id);
124
        $select->where->equalTo('user_id', $user_id);
130
        $select->where->equalTo('slide_id', $slide_id);
125
        $select->where->equalTo('slide_id', $slide_id);
131
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
-
 
132
        
126
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
Línea 133... Línea 127...
133
        $select->limit(1);
127
        $select->limit(1);
134
        
128
        
Línea 550... Línea 544...
550
        $record = $this->executeFetchOneArray($select);
544
        $record = $this->executeFetchOneArray($select);
Línea 551... Línea 545...
551
        
545
        
Línea 552... Línea 546...
552
        return $record['total'];
546
        return $record['total'];
553
        
-
 
554
    }
-
 
555
    
-
 
556
    /**
-
 
557
     * Marca una diapositiva como vista por un usuario.
-
 
558
     *
-
 
559
     * @param int $user_id ID del usuario
-
 
560
     * @param int $topic_id ID del tema
-
 
561
     * @param int $capsule_id ID de la cápsula
-
 
562
     * @param int $slide_id ID de la diapositiva
-
 
563
     * @return bool true si se marcó como vista correctamente o ya estaba vista, false en caso de error
-
 
564
     * @throws \InvalidArgumentException Si alguno de los IDs es inválido
-
 
565
     * @throws \RuntimeException Si hay un error en la base de datos
-
 
566
     */
-
 
567
    public function markSlideViewed($user_id, $topic_id, $capsule_id, $slide_id)
-
 
568
    {
-
 
569
        // Verifica si ya existe el registro
-
 
570
        $exists = $this->fetchOneByUserIdAndSlideId($user_id, $slide_id);
-
 
571
        if ($exists) {
-
 
572
            return true; // ya está registrado, consideramos esto un éxito
-
 
573
        }
-
 
574
 
-
 
575
        $now = date('Y-m-d H:i:s');
-
 
576
        
-
 
577
        // Crear nuevo progreso
-
 
578
        $userProgress = new MicrolearningUserProgress();
-
 
579
        $userProgress->user_id = $user_id;
-
 
580
        $userProgress->topic_id = $topic_id;
-
 
581
        $userProgress->capsule_id = $capsule_id;
-
 
582
        $userProgress->slide_id = $slide_id;
-
 
583
        $userProgress->type = MicrolearningUserProgress::TYPE_SLIDE;
-
 
584
        $userProgress->progress = 100;
-
 
585
        $userProgress->completed = 1;
-
 
586
        $userProgress->added_on = $now;
-
 
587
        $userProgress->updated_on = $now;
-
 
588
 
-
 
Línea 589... Línea 547...
589
        return $this->insert($userProgress);
547
        
590
    }
548
    }
591
 
-
 
592
    /**
549
 
593
     * Marca una cápsula como completada por un usuario.
550
    /**
594
     *
551
     * Update the progress of a slide
595
     * @param int $user_id ID del usuario
552
     * @param int $user_id
596
     * @param int $topic_id ID del tema
553
     * @param int $slide_id
597
     * @param int $capsule_id ID de la cápsula
554
     * @param int $capsule_id
598
     * @return bool true si se marcó como completada correctamente, false en caso de error
-
 
-
 
555
     * @return int
599
     */
556
     */
600
    public function markCapsuleCompleted($user_id, $topic_id, $capsule_id)
-
 
601
    {
557
    public function updateSlideProgress($user_id, $slide_id, $capsule_id){
602
        $now = date('Y-m-d H:i:s');
558
        $userProgress = $this->fetchOneByUserIdAndSlideId($user_id, $slide_id);
603
        
-
 
604
        // Verifica si ya existe algún progreso de cápsula
559
        if(!$userProgress){
605
        $userProgress = $this->fetchOneByUserIdAndCapsuleId($user_id, $capsule_id);
560
            $userProgress = new MicrolearningUserProgress();
-
 
561
            $userProgress->user_id = $user_id;
606
        
562
            $userProgress->slide_id = $slide_id;
607
        if ($userProgress) {
563
            $userProgress->capsule_id = $capsule_id;
608
            // Si ya existe, actualizar el progreso
564
            $userProgress->type = MicrolearningUserProgress::TYPE_SLIDE;
609
            $userProgress->progress = 100;
-
 
610
            $userProgress->completed = 1;
-
 
611
            $userProgress->updated_on = $now;
-
 
612
            return $this->update($userProgress);
-
 
613
        }
-
 
614
 
-
 
615
        // Si no existe, crear nuevo progreso
-
 
616
        $userProgress = new MicrolearningUserProgress();
-
 
617
        $userProgress->user_id = $user_id;
-
 
618
        $userProgress->topic_id = $topic_id;
-
 
619
        $userProgress->capsule_id = $capsule_id;
-
 
620
        $userProgress->type = MicrolearningUserProgress::TYPE_CAPSULE;
-
 
621
        $userProgress->progress = 100;
565
            $userProgress->progress = 100;
622
        $userProgress->completed = 1;
-
 
623
        $userProgress->added_on = $now;
566
            $userProgress->completed = 1;
624
        $userProgress->updated_on = $now;
-
 
625
 
-
 
626
        return $this->insert($userProgress);
-
 
627
    }
-
 
628
 
-
 
629
    /**
-
 
630
     * Marca un tema/tópico como completado por un usuario.
-
 
631
     *
-
 
632
     * @param int $user_id ID del usuario
-
 
633
     * @param int $topic_id ID del tema
-
 
634
     * @return bool true si se marcó como completado correctamente, false en caso de error
-
 
635
     */
-
 
636
    public function markTopicCompleted($user_id, $topic_id)
-
 
637
    {
-
 
638
        $now = date('Y-m-d H:i:s');
-
 
639
        
-
 
640
        // Verifica si ya existe algún progreso del tema
567
            $userProgress->added_on = date('Y-m-d H:i:s');
641
        $userProgress = $this->fetchOneByUserIdAndTopicId($user_id, $topic_id);
-
 
642
        
568
            $userProgress->updated_on = date('Y-m-d H:i:s');
643
        if ($userProgress) {
569
            return $this->insert($userProgress);
644
            // Si ya existe, actualizar el progreso
570
        } else {
645
            $userProgress->progress = 100;
571
            $userProgress->progress = 100;
646
            $userProgress->completed = 1;
572
            $userProgress->completed = 1;
647
            $userProgress->updated_on = $now;
-
 
648
            return $this->update($userProgress);
-
 
649
        }
-
 
650
 
-
 
651
        // Si no existe, crear nuevo progreso
-
 
652
        $userProgress = new MicrolearningUserProgress();
-
 
653
        $userProgress->user_id = $user_id;
-
 
654
        $userProgress->topic_id = $topic_id;
-
 
655
        $userProgress->type = MicrolearningUserProgress::TYPE_TOPIC;
-
 
656
        $userProgress->progress = 100;
-
 
657
        $userProgress->completed = 1;
-
 
658
        $userProgress->added_on = $now;
-
 
659
        $userProgress->updated_on = $now;
573
            $userProgress->updated_on = date('Y-m-d H:i:s');
Línea 660... Línea 574...
660
 
574
            return $this->update($userProgress);
661
        return $this->insert($userProgress);
575
        }
662
    }
-
 
663
 
576
    }
664
    /**
577
 
665
     * Verifica si un usuario ha visto todas las diapositivas de una cápsula específica.
-
 
666
     *
-
 
667
     * @param int $user_id ID del usuario
578
    /**
668
     * @param int $capsule_id ID de la cápsula
579
     * Get the number of slides completed by a capsule
669
     * @return bool true si ha visto todas las diapositivas, false en caso contrario
-
 
670
     * @throws \InvalidArgumentException Si alguno de los IDs es inválido
580
     * @param int $capsule_id
671
     */
581
     * @return int
Línea -... Línea 582...
-
 
582
     */
-
 
583
    public function getSlidesCompletedByCapsuleId($capsule_id){
672
    public function hasViewedAllSlidesInCapsule($user_id, $capsule_id)
584
        // Get total slides in the capsule
673
    {
585
        $totalSlides = $this->slideMapper->fetchTotalCountByCapsuleId($capsule_id);
-
 
586
 
674
        $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
587
        // Get all slides completed
Línea 675... Línea 588...
675
        $allSlides = $slideMapper->fetchAllByCapsuleId($capsule_id);
588
        $select = $this->sql->select(self::_TABLE);
676
 
589
        $select->columns(['total' => new Expression('COUNT(*)')]);
677
        // Obtener el total de diapositivas vistas por el usuario en esta cápsula
590
        $select->where->equalTo('capsule_id', $capsule_id);
Línea 678... Línea 591...
678
        $viewedSlides = $this->fetchCountAllSlideCompletedByUserIdAndCapsuleId($user_id, $capsule_id);
591
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
679
        $totalSlides = count($allSlides);
592
        $select->where->equalTo('completed', 1);
680
 
-
 
681
        // Comparar si el número de diapositivas vistas es igual al total
593
 
682
        return $viewedSlides >= $totalSlides;
594
        $record = $this->executeFetchOneArray($select);
683
    }
-
 
684
 
595
        return (int)$record['total'];
685
    /**
596
    }
686
     * Verifica si un usuario ha completado todas las cápsulas disponibles en un tema específico.
597
 
687
     *
598
    /**
-
 
599
     * Update the progress of a capsule
-
 
600
     * @param int $user_id
688
     * @param int $user_id ID del usuario
601
     * @param int $capsule_id
-
 
602
     * @param int $topic_id
689
     * @param int $topic_id ID del tema
603
     * @return int
-
 
604
     */
690
     * @return bool true si el usuario ha completado todas las cápsulas del tema, false en caso contrario
605
    public function updateCapsuleProgress($user_id, $capsule_id, $topic_id){
-
 
606
        // Get total slides in the capsule
-
 
607
        $totalSlides = $this->slideMapper->fetchTotalCountByCapsuleId($capsule_id);
Línea -... Línea 608...
-
 
608
 
691
     * @throws \InvalidArgumentException Si alguno de los IDs es inválido
609
        // Get all slides completed
-
 
610
        $slidesCompleted = $this->getSlidesCompletedByCapsuleId($capsule_id);
-
 
611
 
-
 
612
        $progress = ($slidesCompleted * 100) / $totalSlides;
-
 
613
        $progress = min($progress, 100);
-
 
614
        $completed = $progress == 100 ? 1 : 0;
-
 
615
 
-
 
616
        $userProgress = $this->fetchOneByUserIdAndCapsuleId($user_id, $capsule_id);
-
 
617
        if(!$userProgress){
-
 
618
            $userProgress = new MicrolearningUserProgress();
-
 
619
            $userProgress->user_id = $user_id;
-
 
620
            $userProgress->capsule_id = $capsule_id;
-
 
621
            $userProgress->topic_id = $topic_id;
692
     * @throws \RuntimeException Si hay un error en la base de datos
622
            $userProgress->type = MicrolearningUserProgress::TYPE_CAPSULE;
-
 
623
            $userProgress->progress = $progress;
-
 
624
            $userProgress->completed = $completed;
-
 
625
            $userProgress->total_slides = $totalSlides;
-
 
626
            $userProgress->view_slides = $slidesCompleted;
-
 
627
            $userProgress->added_on = date('Y-m-d H:i:s');
-
 
628
            $userProgress->updated_on = date('Y-m-d H:i:s');
693
     */
629
            return $this->insert($userProgress);
-
 
630
        } else {
Línea -... Línea 631...
-
 
631
            $userProgress->progress = $progress;
-
 
632
            $userProgress->completed = $completed;
-
 
633
            $userProgress->total_slides = $totalSlides;
-
 
634
            $userProgress->view_slides = $slidesCompleted;
-
 
635
            $userProgress->updated_on = date('Y-m-d H:i:s');
-
 
636
            return $this->update($userProgress);
694
    public function hasCompletedAllCapsulesInTopic($user_id, $topic_id)
637
        }
695
    {
638
    }
696
        $capsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
-
 
697
        $allCapsules = $capsuleMapper->fetchAllByTopicId($topic_id);
639
 
698
 
640
    /**
699
        if (empty($allCapsules)) {
641
     * Get the number of capsules completed by a topic
700
            return false;
-
 
701
        }
642
     * @param int $topic_id
702
 
643
     * @return int
-
 
644
     */
-
 
645
    public function getCapsulesCompletedByTopicId($topic_id){
-
 
646
        $select = $this->sql->select(self::_TABLE);
-
 
647
        $select->columns(['total' => new Expression('COUNT(*)')]);
-
 
648
        $select->where->equalTo('topic_id', $topic_id);
-
 
649
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
-
 
650
        $select->where->equalTo('completed', 1);
-
 
651
        $record = $this->executeFetchOneArray($select);
-
 
652
        return (int)$record['total'];
-
 
653
    }
-
 
654
 
-
 
655
    /**
-
 
656
     * Update the progress of a topic
-
 
657
     * @param int $user_id
-
 
658
     * @param int $topic_id
-
 
659
     * @return void
-
 
660
     */
-
 
661
    public function updateTopicProgress($user_id, $topic_id){
-
 
662
        // Get all capsules in the topic
-
 
663
        $totalCapsules = $this->topicCapsuleMapper->fetchTotalCountByTopicId($topic_id);
-
 
664
 
-
 
665
        // Get all capsules completed
-
 
666
        $completedCapsules = $this->getCapsulesCompletedByTopicId($topic_id);
-
 
667
 
-
 
668
        // Calculate progress and completed
-
 
669
        $progress = ($completedCapsules * 100) / $totalCapsules;
-
 
670
        $progress = min($progress, 100);
-
 
671
        $completed = $progress == 100 ? 1 : 0;
-
 
672
 
-
 
673
        $userProgress = $this->fetchOneByUserIdAndTopicId($user_id, $topic_id);
-
 
674
        if(!$userProgress){
-
 
675
            $userProgress = new MicrolearningUserProgress();
-
 
676
            $userProgress->user_id = $user_id;
-
 
677
            $userProgress->topic_id = $topic_id;
-
 
678
            $userProgress->type = MicrolearningUserProgress::TYPE_TOPIC;
-
 
679
            $userProgress->progress = $progress;
-
 
680
            $userProgress->completed = $completed;
703
        $select = $this->sql->select(self::_TABLE);
681
            $userProgress->added_on = date('Y-m-d H:i:s');
704
        $select->columns(['total' => new Expression('COUNT(*)')]);
682
            $userProgress->updated_on = date('Y-m-d H:i:s');
705
        $select->where->equalTo('user_id', $user_id);
683
            return $this->insert($userProgress);