Proyectos de Subversion LeadersLinked - Services

Rev

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

<?php
declare(strict_types=1);

namespace LeadersLinked\Mapper;

use LeadersLinked\Mapper\Common\MapperCommon;
use Laminas\Db\Adapter\AdapterInterface;
use LeadersLinked\Model\MicrolearningUserProgress;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use Laminas\Db\Sql\Expression;
use LeadersLinked\Mapper\MicrolearningSlideMapper;
use LeadersLinked\Mapper\MicrolearningTopicCapsuleMapper;
class MicrolearningUserProgressMapper extends MapperCommon
{
    const _TABLE = 'tbl_microlearning_user_progress';
    
    /**
     *
     * @var MicrolearningUserProgressMapper
     */
    private static $_instance;

    private $topicCapsuleMapper;

    private $slideMapper;
    
    /**
     *
     * @param AdapterInterface $adapter
     */
    private function __construct($adapter)
    {
        parent::__construct($adapter);
        $this->topicCapsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($adapter);
        $this->slideMapper = MicrolearningSlideMapper::getInstance($adapter);
    }
    
    /**
     *
     * @param AdapterInterface $adapter
     * @return MicrolearningUserProgressMapper
     */
    public static function getInstance($adapter)
    {
        if(self::$_instance == null) {
            self::$_instance = new MicrolearningUserProgressMapper($adapter);
        }
        return self::$_instance;
    }
    
    /**
     *
     * @param int $user_id
     * @return MicrolearningUserProgress[]
     */
    public function fetchAllByUserId($user_id)
    {
        $prototype = new MicrolearningUserProgress();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('user_id', $user_id);
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     *
     * @return int[]
     */
    public function fetchAllDistinctUserIds()
    {
        $user_ids = [];
        
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['user_id' => new Expression('DISTINCT(user_id)')]);
        
        $records = $this->executeFetchAllArray($select);
        foreach ($records as $record)
        {
            array_push($user_ids, $record['user_id']);            
        }
        
        return $user_ids;

    }
    
    /**
     *
     * @return MicrolearningUserProgress[]
     */
    public function fetchAllTopics()
    {
        $prototype = new MicrolearningUserProgress();
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_TOPIC);
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     *
     * @return MicrolearningUserProgress[]
     */
    public function fetchAllCapsules()
    {
        $prototype = new MicrolearningUserProgress();
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
        
        return $this->executeFetchAllObject($select, $prototype);
    }
    
    /**
     * 
     * @param int $user_id
     * @param int $slide_id
     * @return MicrolearningUserProgress
     */
    public function fetchOneByUserIdAndSlideId($user_id, $slide_id)
    {
        $prototype = new MicrolearningUserProgress();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('user_id', $user_id);
        $select->where->equalTo('slide_id', $slide_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
        $select->limit(1);
        
        return $this->executeFetchOneObject($select, $prototype);
    }

    /**
     * Fetch all progress data by user id and slide id
     * @param int $userId
     * @param int $slideId
     * @return MicrolearningUserProgress[]
     */
    public function fetchAllProgressDataByUserIdAndSlideId($userId, $slideId)
    {
        $select = $this->sql->select();
        $select->from(self::_TABLE);
        $select->where->equalTo('user_id', $userId);
        $select->where->equalTo('slide_id', $slideId);
    }
    
    /**
     *
     * @param int $user_id
     * @param int[] $capsule_ids
     * @return MicrolearningUserProgress
     */
    public function fetchOneLastCapsuleInProgressByUserIdAndCapsuleIds($user_id, $capsule_ids)
    {
        $capsule_ids = empty($capsule_ids) ? [0] : $capsule_ids;
        
        $prototype = new MicrolearningUserProgress();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('user_id', $user_id);
        $select->where->in('capsule_id', $capsule_ids);
        $select->where->greaterThan('progress', 0);
        $select->where->lessThan('progress', 100);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
        $select->order('updated_on');
        $select->limit(1);
        
        
        //echo $select->getSqlString($this->adapter->platform);
        
        return $this->executeFetchOneObject($select, $prototype);
    }

    public function fetchOneLastCapsuleInProgressByUserIdAndTopicIds($user_id, $topic_ids)
    {
        $prototype = new MicrolearningUserProgress();
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('user_id', $user_id);
        $select->where->in('topic_id', $topic_ids);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
        $select->where->greaterThan('progress', 0);
        $select->where->lessThan('progress', 100);
        $select->order('updated_on');
        $select->limit(1);

        return $this->executeFetchOneObject($select, $prototype);
    }
    
    /**
     *
     * @param int $user_id
     * @param int $capsule_id
     * @return MicrolearningUserProgress
     */
    public function fetchOneByUseridAndCapsuleId($user_id, $capsule_id)
    {
        $prototype = new MicrolearningUserProgress();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('user_id', $user_id);
        $select->where->equalTo('capsule_id', $capsule_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
        $select->limit(1);
        
        
        //echo $select->getSqlString($this->adapter->platform);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    /**
     *
     * @param int $user_id
     * @param int $topic_id
     * @return MicrolearningUserProgress
     */
    public function fetchOneByUserIdAndTopicId($user_id, $topic_id)
    {
        $prototype = new MicrolearningUserProgress();
        
        $select = $this->sql->select(self::_TABLE);
        $select->where->equalTo('user_id', $user_id);
        $select->where->equalTo('topic_id', $topic_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_TOPIC);
        $select->limit(1);
        
        return $this->executeFetchOneObject($select, $prototype);
    }
    
    
    /**
     *
     * @param int $user_id
     * @param int $topic_id
     * @return int
     */
    public function  fetchCountCapsulesStartedByIdAndTopicId($user_id, $topic_id)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('user_id', $user_id);
        $select->where->equalTo('topic_id', $topic_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
        $select->where->greaterThan('progress', 0);
        $select->where->lessThan('progress', 100);
        $select->where->equalTo('completed', 0);
        $select->limit(1);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    
    /**
     *
     * @param int $user_id
     * @param int $topic_id
     * @return int
     */
    public function  fetchCountCapsulesCompletedByIdAndTopicId($user_id, $topic_id)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('user_id', $user_id);
        $select->where->equalTo('topic_id', $topic_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
        $select->where->equalTo('completed', 1);
        $select->limit(1);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    
    
    
    /**
     *
     * @param int $user_id
     * @param int $topic_id
     * @return int
     */
    public function fetchCountCapsulesCompletedWithReturningByIdAndTopicId($user_id, $topic_id)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('user_id', $user_id);
        $select->where->equalTo('topic_id', $topic_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
        $select->where->equalTo('completed', 1);
        $select->where->notEqualTo('returning_after_completed', 0);
        $select->limit(1);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    /**
     *
     * @param int $user_id
     * @param int $topic_id
     * @return int
     */
    public function fetchCountCapsulesCompletedWithoutReturningByIdAndTopicId($user_id, $topic_id)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('user_id', $user_id);
        $select->where->equalTo('topic_id', $topic_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
        $select->where->equalTo('completed', 1);
        $select->where->equalTo('returning_after_completed', 0);
        $select->limit(1);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    
    /**
     *
     * @param int $user_id
     * @param int $capsule_id
     * @return int
     */
    public function fetchCountAllSlideViewedByUserIdAndCapsuleId($user_id, $capsule_id)
    {
 
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('user_id', $user_id);
        $select->where->equalTo('capsule_id', $capsule_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
        $select->limit(1);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    /**
     *
     * @param int $user_id
     * @param int $topic_id
     * @param int $capsule_id
     * @return int
     */
    public function fetchCountAllSlideCompletedByUserIdAndTopicIdAndCapsuleId($user_id, $topic_id, $capsule_id)
    {
        
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('user_id', $user_id);
        $select->where->equalTo('topic_id', $topic_id);
        $select->where->equalTo('capsule_id', $capsule_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
        $select->where->equalTo('completed', 1);
        $select->limit(1);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    
    /**
     *
     * @param int $user_id
     * @param int $capsule_id
     * @return int
     */
    public function fetchCountAllSlideCompletedByUserIdAndCapsuleId($user_id, $capsule_id)
    {
        
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('user_id', $user_id);
        $select->where->equalTo('capsule_id', $capsule_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
        $select->where->equalTo('completed', 1);
        $select->limit(1);
        
        //echo $select->getSqlString($this->adapter->platform);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    /**
     *
     * @param int $user_id
     * @param int $capsule_id
     * @return int
     */
    public function fetchCountAllSlideCompletedByUserIdAndTopicId($user_id, $topic_id)
    {
        
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('user_id', $user_id);
        $select->where->equalTo('topic_id', $topic_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
        $select->where->equalTo('completed', 1);
        $select->limit(1);
        
       // echo $select->getSqlString($this->adapter->platform) . PHP_EOL;
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    /**
     *
     * @param int $company_id
     * @param int $user_id
     * @return int
     */
    public function fetchCountAllSlideCompletedByCompanyIdAndUserId($company_id, $user_id)
    {
        
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('company_id', $company_id);
        $select->where->equalTo('user_id', $user_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
        $select->where->equalTo('completed', 1);
        $select->limit(1);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    
    /**
     *
     * @param int $user_id
     * @param int $topic_id
     * @return int
     */
    public function fetchCountAllSlideViewedByUserIdAndTopicId($user_id, $topic_id)
    {
        
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('user_id', $user_id);
        $select->where->equalTo('topic_id', $topic_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
        $select->limit(1);
        
        $record = $this->executeFetchOneArray($select);
        return $record['total'];
    }
    
    /**
     * 
     * @param MicrolearningUserProgress $userProgress
     * return boolean
     */
    public function insert($userProgress)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($userProgress);
        $values = $this->removeEmpty($values);
        
        $values['added_on'] = $userProgress->added_on;
        $values['updated_on'] = $userProgress->updated_on;
        
        $insert = $this->sql->insert(self::_TABLE);
        $insert->values($values);
        
        //echo $insert->getSqlString($this->adapter->platform); exit;
        
        $result = $this->executeInsert($insert);
        if($result) {
            $userProgress->id = $this->lastInsertId;
        }
        return $result;
    }
    
    
    /**
     *
     * @param MicrolearningUserProgress $userProgress
     * return boolean
     */
    public function update($userProgress)
    {
        $hydrator = new ObjectPropertyHydrator();
        $values = $hydrator->extract($userProgress);
        $values = $this->removeEmpty($values);
        
        
        $values['added_on'] = $userProgress->added_on;
        $values['updated_on'] = $userProgress->updated_on;
        
        
        $update = $this->sql->update(self::_TABLE);
        $update->set($values);
        $update->where->equalTo('id', $userProgress->id);
        
        //echo $update->getSqlString($this->adapter->platform) . PHP_EOL;
        
        return $this->executeUpdate($update);
    }

    /**
     * Delete all user progress by topic id
     * @param int $topic_id
     * @return int
     */
    public function deleteAllTopicProgressByTopicId($topic_id)
    {
        $delete = $this->sql->delete(self::_TABLE);
        $delete->where->equalTo('topic_id', $topic_id);
        $delete->where->equalTo('type', MicrolearningUserProgress::TYPE_TOPIC);
        return $this->executeDelete($delete);
    }

    /**
     * Deletes all user progress by capsule id
     * @param int $capsule_id
     * @return int
     */
    public function deleteAllByCapsuleId($capsule_id)
    {
        $delete = $this->sql->delete(self::_TABLE);
        $delete->where->equalTo('capsule_id', $capsule_id);
        $delete->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
        $delete->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
        return $this->executeDelete($delete);
    }
    
    
    /**
     *
     * @param int $userId
     * @return int
     */
    public function getCountCapsulesCompletedByUserId($userId)
    {
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')] );
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
        $select->where->equalTo('user_id', $userId);
        $select->where->equalTo('completed', 1);
        
        $record = $this->executeFetchOneArray($select);
        
        return $record['total'];
        
    }

    /**
     * Update the progress of a slide
     * @param int $user_id
     * @param int $company_id
     * @param int $slide_id
     * @param int $capsule_id
     * @param int $topic_id
     * @return bool
     */
    public function updateSlideProgress($user_id, $company_id, $slide_id, $capsule_id, $topic_id){
        $userProgress = $this->fetchOneByUserIdAndSlideId($user_id, $slide_id);
        $now = date('Y-m-d H:i:s');

        if (!$userProgress) {
            // Crear nuevo progreso
            $userProgress = new MicrolearningUserProgress();
            $userProgress->user_id = $user_id;
            $userProgress->company_id = $company_id;
            $userProgress->slide_id = $slide_id;
            $userProgress->capsule_id = $capsule_id;
            $userProgress->topic_id = $topic_id;
            $userProgress->type = MicrolearningUserProgress::TYPE_SLIDE;
            $userProgress->progress = 100;
            $userProgress->completed = 1;
            $userProgress->added_on = $now;
            $userProgress->updated_on = $now;
            return $this->insert($userProgress);
        } else {
            // Actualizar progreso existente
            $userProgress->progress = 100;
            $userProgress->completed = 1;
            $userProgress->updated_on = $now;
            return $this->update($userProgress);
        }
    }

    /**
     * Get the number of slides completed by a capsule
     * @param int $capsule_id
     * @return int
     */
    public function getSlidesCompletedByCapsuleId($capsule_id){
        // Get total slides in the capsule
        $totalSlides = $this->slideMapper->fetchTotalCountByCapsuleId($capsule_id);

        // Get all slides completed
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('capsule_id', $capsule_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
        $select->where->equalTo('completed', 1);

        $record = $this->executeFetchOneArray($select);
        return (int)$record['total'];
    }

    /**
     * Update the progress of a capsule
     * @param int $user_id
     * @param int $company_id
     * @param int $slide_id
     * @param int $capsule_id
     * @param int $topic_id
     * @return int
     */
    public function updateCapsuleProgress($user_id, $company_id, $slide_id, $capsule_id, $topic_id){
        // Get total slides in the capsule
        $totalSlides = $this->slideMapper->fetchTotalCountByCapsuleId($capsule_id);

        // Get all slides completed
        $slidesCompleted = $this->getSlidesCompletedByCapsuleId($capsule_id);

        $progress = ($slidesCompleted * 100) / $totalSlides;
        $progress = min($progress, 100);
        $completed = $progress == 100 ? 1 : 0;

        $userProgress = $this->fetchOneByUserIdAndCapsuleId($user_id, $capsule_id);
        if(!$userProgress){
            $userProgress = new MicrolearningUserProgress();
            $userProgress->user_id = $user_id;
            $userProgress->company_id = $company_id;
            $userProgress->slide_id = $slide_id;
            $userProgress->capsule_id = $capsule_id;
            $userProgress->topic_id = $topic_id;
            $userProgress->type = MicrolearningUserProgress::TYPE_CAPSULE;
            $userProgress->progress = $progress;
            $userProgress->completed = $completed;
            $userProgress->total_slides = $totalSlides;
            $userProgress->view_slides = $slidesCompleted;
            $userProgress->added_on = date('Y-m-d H:i:s');
            $userProgress->updated_on = date('Y-m-d H:i:s');
            return $this->insert($userProgress);
        } else {
            $userProgress->progress = $progress;
            $userProgress->completed = $completed;
            $userProgress->total_slides = $totalSlides;
            $userProgress->view_slides = $slidesCompleted;
            $userProgress->updated_on = date('Y-m-d H:i:s');
            return $this->update($userProgress);
        }
    }

    /**
     * Get the number of capsules completed by a topic
     * @param int $topic_id
     * @return int
     */
    public function getCapsulesCompletedByTopicId($topic_id){
        $select = $this->sql->select(self::_TABLE);
        $select->columns(['total' => new Expression('COUNT(*)')]);
        $select->where->equalTo('topic_id', $topic_id);
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
        $select->where->equalTo('completed', 1);
        $record = $this->executeFetchOneArray($select);
        return (int)$record['total'];
    }

    /**
     * Update the progress of a topic
     * @param int $user_id
     * @param int $company_id
     * @param int $slide_id
     * @param int $capsule_id
     * @param int $topic_id
     * @return void
     */
    public function updateTopicProgress($user_id, $company_id, $slide_id, $capsule_id, $topic_id){
        // Get all capsules in the topic
        $totalCapsules = $this->topicCapsuleMapper->fetchTotalCountByTopicId($topic_id);

        // Get all capsules completed
        $completedCapsules = $this->getCapsulesCompletedByTopicId($topic_id);

        // Calculate progress and completed
        $progress = ($completedCapsules * 100) / $totalCapsules;
        $progress = min($progress, 100);
        $completed = $progress == 100 ? 1 : 0;

        $userProgress = $this->fetchOneByUserIdAndTopicId($user_id, $topic_id);
        if(!$userProgress){
            $userProgress = new MicrolearningUserProgress();
            $userProgress->user_id = $user_id;
            $userProgress->company_id = $company_id;
            $userProgress->slide_id = $slide_id;
            $userProgress->capsule_id = $capsule_id;
            $userProgress->topic_id = $topic_id;
            $userProgress->type = MicrolearningUserProgress::TYPE_TOPIC;
            $userProgress->progress = $progress;
            $userProgress->completed = $completed;
            $userProgress->added_on = date('Y-m-d H:i:s');
            $userProgress->updated_on = date('Y-m-d H:i:s');
            return $this->insert($userProgress);
        } else {
            $userProgress->progress = $progress;
            $userProgress->completed = $completed;
            $userProgress->updated_on = date('Y-m-d H:i:s');
            return $this->update($userProgress);
        }
    }
}