Rev 283 | Rev 632 | Ir a la última revisión | 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;
/**
*
* @param AdapterInterface $adapter
*/
private function __construct($adapter)
{
parent::__construct($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);
//echo $select->getSqlString($this->adapter->platform); exit;
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);
}
/**
*
* @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);
}
/**
*
* @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'];
}
public function markSlideViewed($user_id, $topic_id, $capsule_id, $slide_id)
{
$data = [
'user_id' => $user_id,
'topic_id' => $topic_id,
'capsule_id' => $capsule_id,
'slide_id' => $slide_id,
'viewed_at' => date('Y-m-d H:i:s'),
];
// Verifica si ya existe el registro
$exists = $this->fetchOneByUserIdAndSlideId($user_id, $slide_id);
if ($exists) {
return; // ya registrado
}
return $this->insert($data);
}
public function markCapsuleCompleted($user_id, $topic_id, $capsule_id)
{
$data = [
'user_id' => $user_id,
'topic_id' => $topic_id,
'capsule_id' => $capsule_id,
'completed_at' => date('Y-m-d H:i:s'),
];
// Verifica si ya existe algún progreso de cápsula
$exists = $this->fetchOneByUserIdAndCapsuleId($user_id, $capsule_id);
if ($exists) {
return; // ya registrado
}
return $this->insert($data);
}
public function markTopicCompleted($user_id, $topic_id)
{
$data = [
'user_id' => $user_id,
'topic_id' => $topic_id,
'topic_closed' => 1,
'closed_at' => date('Y-m-d H:i:s'),
];
// Verifica si ya existe algún progreso de topic
$exists = $this->fetchOneByUserIdAndTopicId($user_id, $topic_id);
if ($exists) {
return; // ya registrado
}
return $this->insert($data);
}
public function hasViewedAllSlidesInCapsule($user_id, $capsule_id)
{
$slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
$allSlides = $slideMapper->fetchAllByCapsuleId($capsule_id);
$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_SLIDE);
$select->where->equalTo('viewed_at', 1);
$record = $this->executeFetchOneArray($select);
return count($record) >= count($allSlides);
}
public function hasCompletedAllCapsulesInTopic($user_id, $topic_id)
{
$capsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
$allCapsules = $capsuleMapper->fetchAllByTopicId($topic_id);
$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_CAPSULE);
$select->where->equalTo('completed', 1);
$record = $this->executeFetchOneArray($select);
return count($record) >= count($allCapsules);
}
}