Rev 43 | 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\CompanyMicrolearningUserLog;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use Laminas\Db\Sql\Expression;
use Laminas\Db\ResultSet\HydratingResultSet;
use Laminas\Paginator\Adapter\DbSelect;
use Laminas\Paginator\Paginator;
class CompanyMicrolearningUserLogMapper extends MapperCommon
{
const _TABLE = 'tbl_company_microlearning_user_log';
/**
*
* @var CompanyMicrolearningUserLogMapper
*/
private static $_instance;
/**
*
* @param AdapterInterface $adapter
*/
private function __construct($adapter)
{
parent::__construct($adapter);
}
/**
*
* @param AdapterInterface $adapter
* @return CompanyMicrolearningUserLogMapper
*/
public static function getInstance($adapter)
{
if(self::$_instance == null) {
self::$_instance = new CompanyMicrolearningUserLogMapper($adapter);
}
return self::$_instance;
}
/**
*
* @param int $user_id
* @return CompanyMicrolearningUserLog[]
*/
public function fetchLast20ByUserId($user_id)
{
$prototype = new CompanyMicrolearningUserLog();
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('user_id', $user_id);
$select->order(['id desc']);
$select->limit(20);
return $this->executeFetchAllObject($select, $prototype);
}
/**
*
* @param int $user_id
* @return CompanyMicrolearningUserLog[]
*/
public function fetchAllByUserId($user_id)
{
$prototype = new CompanyMicrolearningUserLog();
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('user_id', $user_id);
$select->order(['id desc']);
return $this->executeFetchAllObject($select, $prototype);
}
/**
*
* @param int $user_id
* @return CompanyMicrolearningUserLog
*/
public function fetchLastBy($user_id)
{
$prototype = new CompanyMicrolearningUserLog();
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('user_id', $user_id);
$select->order(['added_on desc']);
$select->limit(1);
return $this->executeFetchOneObject($select, $prototype);
}
/**
*
* @param int $user_id
* @return CompanyMicrolearningUserLog[]
*/
public function fetchBatchLastByUserId($user_id, $max_records = 20)
{
$prototype = new CompanyMicrolearningUserLog();
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('user_id', $user_id);
$select->order(['id desc']);
$select->limit($max_records);
return $this->executeFetchAllObject($select, $prototype);
}
/**
*
* @param int $company_id
* @param int $user_id
* @return string
*/
public function fetchFirstDateByCompanyIdAndUserId($company_id, $user_id)
{
$select = $this->sql->select();
$select->columns(['date' => new Expression('MIN(added_on)') ] );
$select->from(self::_TABLE);
$select->where->equalTo('company_id', $company_id);
$select->where->equalTo('user_id', $user_id);
$select->where->in('activity', [
CompanyMicrolearningUserLog::ACTIVITY_START_TOPIC,
CompanyMicrolearningUserLog::ACTIVITY_START_CAPSULE,
CompanyMicrolearningUserLog::ACTIVITY_VIEW_SLIDE,
CompanyMicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
CompanyMicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
CompanyMicrolearningUserLog::ACTIVITY_APPROVED_TEST,
CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC,
]);
$record = $this->executeFetchOneArray($select);
return empty($record['date']) ? '' : $record['date'];
}
/**
*
* @param int $company_id
* @param int $user_id
* @return string
*/
public function fetchLastDateByCompanyIdAndUserId($company_id, $user_id)
{
$select = $this->sql->select();
$select->columns(['date' => new Expression('MAX(added_on)') ] );
$select->from(self::_TABLE);
$select->where->equalTo('company_id', $company_id);
$select->where->equalTo('user_id', $user_id);
$select->where->in('activity', [
CompanyMicrolearningUserLog::ACTIVITY_START_TOPIC,
CompanyMicrolearningUserLog::ACTIVITY_START_CAPSULE,
CompanyMicrolearningUserLog::ACTIVITY_VIEW_SLIDE,
CompanyMicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
CompanyMicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
CompanyMicrolearningUserLog::ACTIVITY_APPROVED_TEST,
CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC,
]);
$record = $this->executeFetchOneArray($select);
return empty($record['date']) ? '' : $record['date'];
}
/**
*
* @param int $company_id
* @param string $min
* @param string $max
* @return int[]
*/
public function fetchAllUserIdsLastWeekByCompanyId($company_id, $min, $max)
{
$select = $this->sql->select();
$select->columns(['user_id' => new Expression('DISTINCT(user_id)') ]);
$select->from(self::_TABLE);
$select->where->equalTo('company_id', $company_id);
$select->where->in('activity', [
CompanyMicrolearningUserLog::ACTIVITY_START_TOPIC,
CompanyMicrolearningUserLog::ACTIVITY_START_CAPSULE,
CompanyMicrolearningUserLog::ACTIVITY_VIEW_SLIDE,
CompanyMicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
CompanyMicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
CompanyMicrolearningUserLog::ACTIVITY_APPROVED_TEST,
CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC,
]);
$select->where->between(new Expression('added_on'), $min, $max);
$user_ids = [];
$records = $this->executeFetchAllArray($select);
foreach($records as $record)
{
array_push($user_ids, $record['user_id']);
}
return $user_ids;
}
/**
*
* @param CompanyMicrolearningUserLog $userLog
* return true
*/
public function insert($userLog)
{
$hydrator = new ObjectPropertyHydrator();
$values = $hydrator->extract($userLog);
$values = $this->removeEmpty($values);
$values['added_on'] = $userLog->added_on;
$insert = $this->sql->insert(self::_TABLE);
$insert->values($values);
//echo $insert->getSqlString($this->adapter->platform); exit;
$result = $this->executeInsert($insert);
if($result) {
$userLog->id = $this->lastInsertId;
}
return $result;
}
/**
*
* @param int $user_id
* @param int $topic_id
* @return int
*/
public function fetchCountTotalCountViewSlideForUserIdAndTopic($user_id, $topic_id)
{
$select = $this->sql->select(self::_TABLE);
$select->columns(['total' => new Expression('COUNT(DISTINCT(slide_id))')]);
$select->where->equalTo('user_id', $user_id);
$select->where->equalTo('topic_id', $topic_id);
$select->where->equalTo('activity', 'VIEW-SLIDE');
$record = $this->executeFetchOneArray($select);
return $record['total'];
}
/**
*
* @param int $user_id
* @param int $topic_id
* @param int $capsule_id
* @return int
*/
public function fetchCountTotalCountViewSlideForUserIdAndTopicAndCapsuleId($user_id, $topic_id, $capsule_id)
{
$select = $this->sql->select(self::_TABLE);
$select->columns(['total' => new Expression('COUNT(DISTINCT(slide_id))')]);
$select->where->equalTo('user_id', $user_id);
$select->where->equalTo('topic_id', $topic_id);
$select->where->equalTo('capsule_id', $capsule_id);
$select->where->equalTo('activity', 'VIEW-SLIDE');
$record = $this->executeFetchOneArray($select);
return $record['total'];
}
/**
*
* @param int $user_id
* @param int $page
* @param int $records_per_page
* @return Paginator
*/
public function getAllMessagesPaginatorByUserId($user_id, $page = 1, $records_per_page = 10)
{
$select = $this->sql->select(self::_TABLE);
$select->where->equalTo('user_id', $user_id);
$select->order('id DESC');
//echo $select->getSqlString($this->adapter->getPlatform()); exit;
$prototype = new CompanyMicrolearningUserLog();
$hydrator = new ObjectPropertyHydrator();
$resultset = new HydratingResultSet($hydrator, $prototype);
$adapter = new DbSelect($select, $this->sql, $resultset);
$paginator = new Paginator($adapter);
$paginator->setCurrentPageNumber($page);
$paginator->setItemCountPerPage($records_per_page);
return $paginator;
}
/**
*
* @param int $user_id
* @param int $company_id
* @param int $page
* @param int $records_per_page
* @return Paginator
*/
public function getAllMessagesPaginatorByUserIdAndCompanyId($user_id, $company_id, $page = 1, $records_per_page = 10)
{
$select = $this->sql->select(self::_TABLE);
$select->where->nest()
->equalTo('user_id', $user_id)
->in('activity', [
CompanyMicrolearningUserLog::ACTIVITY_SIGNIN,
CompanyMicrolearningUserLog::ACTIVITY_SIGNOUT,
])
->unnest()->or->nest()
->equalTo('user_id', $user_id)
->equalTo('company_id', $company_id)
->in('activity', [
CompanyMicrolearningUserLog::ACTIVITY_APPROVED_TEST,
CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC,
CompanyMicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
CompanyMicrolearningUserLog::ACTIVITY_START_CAPSULE,
CompanyMicrolearningUserLog::ACTIVITY_START_TOPIC,
CompanyMicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
CompanyMicrolearningUserLog::ACTIVITY_VIEW_SLIDE
])
->unnest();
$select->order('id DESC');
//echo $select->getSqlString($this->adapter->getPlatform()); exit;
$prototype = new CompanyMicrolearningUserLog();
$hydrator = new ObjectPropertyHydrator();
$resultset = new HydratingResultSet($hydrator, $prototype);
$adapter = new DbSelect($select, $this->sql, $resultset);
$paginator = new Paginator($adapter);
$paginator->setCurrentPageNumber($page);
$paginator->setItemCountPerPage($records_per_page);
return $paginator;
}
}