Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

<?php
/**
 * 
 * Controlador: Microlearning
 * 
 */
declare(strict_types=1);

namespace LeadersLinked\Controller;

use Laminas\Db\Adapter\AdapterInterface;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Log\LoggerInterface;
use Laminas\View\Model\ViewModel;
use Laminas\View\Model\JsonModel;
use LeadersLinked\Mapper\CompanyMicrolearningUserLogMapper;
use LeadersLinked\Model\CompanyMicrolearningUserLog;
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleUserMapper;
use LeadersLinked\Mapper\CompanyMicrolearningUserProgressMapper;
use LeadersLinked\Mapper\CompanyMicrolearningSlideMapper;

class ProfileMicrolearningController extends AbstractActionController
{
    /**
     *
     * @var \Laminas\Db\Adapter\AdapterInterface
     */
    private $adapter;
    
    /**
     *
     * @var \LeadersLinked\Cache\CacheInterface
     */
    private $cache;
    
    
    /**
     *
     * @var \Laminas\Log\LoggerInterface
     */
    private $logger;
    
    /**
     *
     * @var array
     */
    private $config;
    
    
    /**
     *
     * @var \Laminas\Mvc\I18n\Translator
     */
    private $translator;
    
    
    /**
     *
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
     * @param \LeadersLinked\Cache\CacheInterface $cache
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
     * @param array $config
     * @param \Laminas\Mvc\I18n\Translator $translator
     */
    public function __construct($adapter, $cache, $logger, $config, $translator)
    {
        $this->adapter      = $adapter;
        $this->cache        = $cache;
        $this->logger       = $logger;
        $this->config       = $config;
        $this->translator   = $translator;
    }
    
    /**
     * 
     * Generación del listado de perfiles
     * {@inheritDoc}
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
     */
    public function indexAction()
    {
        //$currentUserPlugin = $this->plugin('currentUserPlugin');
        //$currentUser = $currentUserPlugin->getUser();
        
        $request = $this->getRequest();
        if($request->isGet()) {
            

           
            $this->layout()->setTemplate('layout/layout.phtml');
            $viewModel = new ViewModel();
            $viewModel->setTemplate('leaders-linked/profile/microlearning.phtml');
            $viewModel->setVariables([
                'activities' => [
                    'ACTIVITY_SIGNIN' =>  CompanyMicrolearningUserLog::ACTIVITY_SIGNIN,
                    'ACTIVITY_SIGNOUT' => CompanyMicrolearningUserLog::ACTIVITY_SIGNOUT,
                    'ACTIVITY_START_TOPIC' => CompanyMicrolearningUserLog::ACTIVITY_START_TOPIC,
                    'ACTIVITY_START_CAPSULE' => CompanyMicrolearningUserLog::ACTIVITY_START_CAPSULE,
                    'ACTIVITY_VIEW_SLIDE' => CompanyMicrolearningUserLog::ACTIVITY_VIEW_SLIDE,
                    'ACTIVITY_TAKE_A_TEST' => CompanyMicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
                    'ACTIVITY_RETAKE_A_TEST' => CompanyMicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
                    'ACTIVITY_APPROVED_TEST' => CompanyMicrolearningUserLog::ACTIVITY_APPROVED_TEST,
                    'ACTIVITY_COMPLETED_CAPSULE' => CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
                    'ACTIVITY_COMPLETED_TOPIC' => CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC
                ]
            ]);
            return $viewModel ;
            
            
        } else {
            return new JsonModel([
                'success' => false,
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
            ]);
        }
    }
    
    
    /**
     *
     * Generación del timeline de microaprendizaje para el usuario actual
     * solo con agregar ?page=XX
     * 
     * Para las repuesta afirmativa
     * [ 
     *    'success' => true,
     *    'data' => [
     *      'total' => [
     *          'count' => cantidad de registros totales,
     *          'pages' => cantidad de páginas totales,
     *      ],
     *      'current' => [
     *          'items'    => [
     *              [
     *                  'activity' => actividad realizada,
     *                  'added_on' => fecha en la que fué realizada
     *              ]
     *           ] ,
     *          'page'     => página actual,
     *          'count'    => cantidad de registros de la página actual,
     *       ]
     *    ]
     *    
     *  En caso contrario
     *  [
     *      'success' => false,
     *      'data' => mensaje de error
     *  ]  
     *
     *
     * @return \Laminas\View\Model\JsonModel
     */
    public function timelineAction()
    {
        
        $request = $this->getRequest();
        if($request->isGet()) {
            $currentUserPlugin = $this->plugin('currentUserPlugin');
            $currentUser = $currentUserPlugin->getUser();
        
            
            $page = intval($this->params()->fromQuery('page'), 10); 
            
            $companyMicrolearningUserLogMapper = CompanyMicrolearningUserLogMapper::getInstance($this->adapter);
            $paginator = $companyMicrolearningUserLogMapper->getAllMessagesPaginatorByUserId($currentUser->id, $page);
            
            $items = [];
            foreach($paginator as $record)
            {
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
                
                array_push($items, [
                    'activity' => $record->activity,
                    'added_on' => $dt->format('d/m/Y H:i a')
                ]); 
            }
            
            
            return new JsonModel([
                    'success' => true,
                    'data' => [
                        'total' => [
                            'count' => $paginator->getTotalItemCount(),
                            'pages' => $paginator->getPages()->pageCount,
                        ],
                        'current' => [
                            'items'    => $items,
                            'page'     => $paginator->getCurrentPageNumber(),
                            'count'    => $paginator->getCurrentItemCount(),
                        ]
                    ]
            ]);
            
            
        } else {
            return new JsonModel([
                'success' => false,
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
            ]);
        }
    }
    
    /**
     * Valores para la generación de los gráficos de progreso
     * Para las repuesta afirmativa
     * [
     *  'success' => true,
     *      'data' => [
     *          'topicTotal' => cantidad total de tópicos,
     *          'topicStarted' => cantidad de tópicos iniciados,
     *          'topicIncompleted' => cantidad de tópicos incompletos,
     *          'topicCompleted' => cantidad de tópicos completos,
     *          'percentCompleted' => % de diapositivas completados ,
     *          'percentIncompleted' => % de diapositivas incompletos ,
     *          'percentWithoutReturning' => % de cápsulas sin retorno después de completada,
     *          'percentWithReturning' => % de cápsulas con retorno después de completada,  
     *       ],
     * ]
     *
     *    
     *  En caso contrario
     *  [
     *      'success' => false,
     *      'data' => mensaje de error
     *  ]  
     *
     *
     * @return \Laminas\View\Model\JsonModel
     */
    public function progressAction()
    {
        
        $request = $this->getRequest();
        if($request->isGet()) {
            $currentUserPlugin = $this->plugin('currentUserPlugin');
            $currentUser = $currentUserPlugin->getUser();
            
            $topicIdsUsers = [];
            $capsuleIdsUser = [];
            
            $topicTotal = 0;
            $topicStarted = 0;
            $topicIncompleted = 0;
            $topicCompleted =  0;
            $totalSlides = 0;
            $totalSlidesCompleted = 0;
            //$totalSlideWithReturning = 0;
            
            $totalCapsules = 0;
            $totalCapsulesCompleted = 0;
            $totalCapsulesCompletedWithReturn = 0;
            $totalCapsulesCompletedWithoutReturn = 0;
            
            //$companyMicrolearningTopicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
            $companyMicrolearningSlideMapper = CompanyMicrolearningSlideMapper::getInstance($this->adapter);
            
            $companyMicrolearningUserProgressMapper = CompanyMicrolearningUserProgressMapper::getInstance($this->adapter);
            
            
            $companyMicrolearningCapsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
            $capsulesUser = $companyMicrolearningCapsuleUserMapper->fetchAllActiveByUserId($currentUser->id);
            
            foreach($capsulesUser as $capsuleUser)
            {
                $company_id = $capsuleUser->company_id;
                $topic_id   = $capsuleUser->topic_id;
                $capsule_id = $capsuleUser->capsule_id;
                
                if(!in_array($topic_id, $topicIdsUsers)) {
                    $topicTotal++;
                    array_push($topicIdsUsers, $topic_id);
                    
                    $progress = $companyMicrolearningUserProgressMapper->fetchOneByUserIdAndTopicId($currentUser->id, $topic_id);
                    if($progress) {
                        $topicStarted++;
                        if(100 > $progress->progress) {
                            $topicIncompleted++;
                        } else {
                            $topicCompleted++;
                        }
                        
                    }
                }
                
                if(!in_array($capsule_id, $capsuleIdsUser)) {
                    
                    $totalSlides += $companyMicrolearningSlideMapper->fetchTotalCountByCompanyIdAndTopicIdAndCapsuleId($company_id, $topic_id, $capsule_id);
                    $totalSlidesCompleted += $companyMicrolearningUserProgressMapper->fetchCountAllSlideCompletedByUserIdAndCapsuleId($currentUser->id, $capsule_id);
                    
                    array_push($capsuleIdsUser, $capsule_id);
                    
                    $totalCapsules++;
                    $progress = $companyMicrolearningUserProgressMapper->fetchOneByUseridAndCapsuleId($currentUser->id, $capsule_id);
                    if($progress && $progress->completed) {
                        
                        $totalCapsulesCompleted++;
                        if($progress->returning_after_completed) {
                            $totalCapsulesCompletedWithReturn++;
                        } else {
                            $totalCapsulesCompletedWithoutReturn++;
                        }
                        
                    }
                    
                    
                }
                
                
            }
            
            $percentCompleted = 0;
            $percentIncompleted = 100;
            
            if( $totalSlides > 0) {
                $percentCompleted = ($totalSlidesCompleted  * 100) /  $totalSlides;
                $percentIncompleted = 100 - $percentCompleted;
            }
            
            
            $percentWithoutReturning  = 0;
            $percentWithReturning = 0;
            
            if($totalCapsulesCompleted > 0) {
                $percentWithReturning = ($totalCapsulesCompletedWithReturn  * 100) / $totalCapsulesCompleted;
                $percentWithoutReturning = ($totalCapsulesCompletedWithoutReturn  * 100) / $totalCapsulesCompleted;
            }
 
            
            
            return new JsonModel([
                'success' => true,
                'data' => [
                    'topicTotal' => $topicTotal,
                    'topicStarted' => $topicStarted,
                    'topicIncompleted' => $topicIncompleted,
                    'topicCompleted' => $topicCompleted,
                    'percentCompleted' => number_format($percentCompleted, 2, '.', ','),
                    'percentIncompleted' => number_format($percentIncompleted, 2, '.', ','),
                    'percentWithoutReturning' => number_format($percentWithoutReturning, 2, '.', ','),
                    'percentWithReturning' => number_format($percentWithReturning, 2, '.', ','),
                ],
            ]);
            
            
        } else {
            return new JsonModel([
                'success' => false,
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
            ]);
        }
    }
    
    
    
}