AutorÃa | Ultima modificación | Ver Log |
<?phpdeclare(strict_types=1);namespace LeadersLinked\Mapper;use LeadersLinked\Mapper\Common\MapperCommon;use Laminas\Db\Adapter\AdapterInterface;use LeadersLinked\Model\MicrolearningUserLog;use LeadersLinked\Hydrator\ObjectPropertyHydrator;use Laminas\Db\Sql\Expression;use Laminas\Db\ResultSet\HydratingResultSet;use Laminas\Paginator\Adapter\DbSelect;use Laminas\Paginator\Paginator;class MicrolearningUserLogMapper extends MapperCommon{const _TABLE = 'tbl_microlearning_user_log';/**** @var MicrolearningUserLogMapper*/private static $_instance;/**** @param AdapterInterface $adapter*/private function __construct($adapter){parent::__construct($adapter);}/**** @param AdapterInterface $adapter* @return MicrolearningUserLogMapper*/public static function getInstance($adapter){if(self::$_instance == null) {self::$_instance = new MicrolearningUserLogMapper($adapter);}return self::$_instance;}/**** @param int $user_id* @return MicrolearningUserLog[]*/public function fetchLast20ByUserId($user_id){$prototype = new MicrolearningUserLog();$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 MicrolearningUserLog[]*/public function fetchAllByUserId($user_id){$prototype = new MicrolearningUserLog();$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 MicrolearningUserLog*/public function fetchLastBy($user_id){$prototype = new MicrolearningUserLog();$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 MicrolearningUserLog[]*/public function fetchBatchLastByUserId($user_id, $max_records = 20){$prototype = new MicrolearningUserLog();$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', [MicrolearningUserLog::ACTIVITY_START_TOPIC,MicrolearningUserLog::ACTIVITY_START_CAPSULE,MicrolearningUserLog::ACTIVITY_VIEW_SLIDE,MicrolearningUserLog::ACTIVITY_TAKE_A_TEST,MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,MicrolearningUserLog::ACTIVITY_APPROVED_TEST,MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,MicrolearningUserLog::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', [MicrolearningUserLog::ACTIVITY_START_TOPIC,MicrolearningUserLog::ACTIVITY_START_CAPSULE,MicrolearningUserLog::ACTIVITY_VIEW_SLIDE,MicrolearningUserLog::ACTIVITY_TAKE_A_TEST,MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,MicrolearningUserLog::ACTIVITY_APPROVED_TEST,MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,MicrolearningUserLog::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', [MicrolearningUserLog::ACTIVITY_START_TOPIC,MicrolearningUserLog::ACTIVITY_START_CAPSULE,MicrolearningUserLog::ACTIVITY_VIEW_SLIDE,MicrolearningUserLog::ACTIVITY_TAKE_A_TEST,MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,MicrolearningUserLog::ACTIVITY_APPROVED_TEST,MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,MicrolearningUserLog::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 MicrolearningUserLog $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 MicrolearningUserLog();$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', [MicrolearningUserLog::ACTIVITY_SIGNIN,MicrolearningUserLog::ACTIVITY_SIGNOUT,])->unnest()->or->nest()->equalTo('user_id', $user_id)->equalTo('company_id', $company_id)->in('activity', [MicrolearningUserLog::ACTIVITY_APPROVED_TEST,MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,MicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC,MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,MicrolearningUserLog::ACTIVITY_START_CAPSULE,MicrolearningUserLog::ACTIVITY_START_TOPIC,MicrolearningUserLog::ACTIVITY_TAKE_A_TEST,MicrolearningUserLog::ACTIVITY_VIEW_SLIDE])->unnest();$select->order('id DESC');//echo $select->getSqlString($this->adapter->getPlatform()); exit;$prototype = new MicrolearningUserLog();$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 $company_id* @return string*/public function fetchOneMaxDateActivityFromCompanyId($company_id){$select = $this->sql->select();$select->columns(['date' => new Expression('DATE(MAX(added_on))') ] );$select->from(self::_TABLE);$select->where->equalTo('company_id', $company_id);$record = $this->executeFetchOneArray($select);return empty($record['date']) ? '' : $record['date'];}/**** @param int $user_id* @param int $slide_id;* @return string*/public function fetchOneTakeATestByUserIdAndSlideId($user_id, $slide_id,){$prototype = new MicrolearningUserLog();$select = $this->sql->select();$select->from(self::_TABLE);$select->where->equalTo('user_id', $user_id);$select->where->equalTo('slide_id', $slide_id);$select->where->equalTo('activity', MicrolearningUserLog::ACTIVITY_TAKE_A_TEST);return $this->executeFetchOneObject($select, $prototype);}/**** @param int $company_id* @param string $start_date* @param string $end_date* @return array*/public function fetchAllCountClosedCapsulesDailyByCompanyIdAndStartDateAndEndDate($company_id, $start_date, $end_date){$select = $this->sql->select();$select->columns(['total' => new Expression('COUNT(*)'), 'date' => new Expression('DATE(added_on)') ] );$select->from(self::_TABLE);$select->where->equalTo('company_id', $company_id);$select->where->between(new Expression('DATE(added_on)'), $start_date, $end_date);$select->group('date');$select->order('date desc');// echo $select->getSqlString($this->adapter->platform); exit;return $this->executeFetchAllArray($select);}/**** @param int $company_id* @param string $start_date* @param string $end_date* @return array*/public function fetchAllCountUsersWithClosedCapsulesDailyByCompanyIdAndStartDateAndEndDate($company_id, $start_date, $end_date){$select = $this->sql->select();$select->columns(['total' => new Expression('COUNT(DISTINCT(user_id))'), 'date' => new Expression('DATE(added_on)') ] );$select->from(self::_TABLE);$select->where->equalTo('company_id', $company_id);$select->where->between(new Expression('DATE(added_on)'), $start_date, $end_date);$select->group('date');$select->order('date desc');return $this->executeFetchAllArray($select);}/*select count(distinct(user_id)) as c, date(added_on) as dfrom tbl_microlearning_user_logwhere company_id = 1 and activity = 'completed-capsule'and date(added_on) BETWEEN '2023-06-09' and '2023-07-09'group by dorder by d desc;select max(date(added_on)) fromtbl_microlearning_user_logwhere company_id = 1;select count(*) as c, date(added_on) as dfrom tbl_microlearning_user_logwhere company_id = 1 and activity = 'completed-capsule'and date(added_on) BETWEEN '2023-06-09' and '2023-07-09'group by dorder by d descselect user_id, count(*) as cfrom tbl_microlearning_user_logwhere company_id = 1 and activity = 'completed-capsule'and date(added_on) BETWEEN '2023-06-09' and '2023-07-09'group by user_idorder by c desc;*/}