Rev 656 | Rev 658 | Ir a la última revisión | Autoría | Comparar con el anterior | 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\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);}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'];}/*** Marca una diapositiva como vista por un usuario.** @param int $user_id ID del usuario* @param int $topic_id ID del tema* @param int $capsule_id ID de la cápsula* @param int $slide_id ID de la diapositiva* @return bool true si se marcó como vista correctamente o ya estaba vista, false en caso de error* @throws \InvalidArgumentException Si alguno de los IDs es inválido* @throws \RuntimeException Si hay un error en la base de datos*/public function markSlideViewed($user_id, $topic_id, $capsule_id, $slide_id){// Verifica si ya existe el registro$exists = $this->fetchOneByUserIdAndSlideId($user_id, $slide_id);if ($exists) {return true; // ya está registrado, consideramos esto un éxito}$now = date('Y-m-d H:i:s');// Crear nuevo progreso$userProgress = new MicrolearningUserProgress();$userProgress->user_id = $user_id;$userProgress->topic_id = $topic_id;$userProgress->capsule_id = $capsule_id;$userProgress->slide_id = $slide_id;$userProgress->type = MicrolearningUserProgress::TYPE_SLIDE;$userProgress->progress = 100;$userProgress->completed = 1;$userProgress->added_on = $now;$userProgress->updated_on = $now;return $this->insert($userProgress);}/*** Marca una cápsula como completada por un usuario.** @param int $user_id ID del usuario* @param int $topic_id ID del tema* @param int $capsule_id ID de la cápsula* @return bool true si se marcó como completada correctamente, false en caso de error*/public function markCapsuleCompleted($user_id, $topic_id, $capsule_id){$now = date('Y-m-d H:i:s');// Verifica si ya existe algún progreso de cápsula$userProgress = $this->fetchOneByUserIdAndCapsuleId($user_id, $capsule_id);if ($userProgress) {// Si ya existe, actualizar el progreso$userProgress->progress = 100;$userProgress->completed = 1;$userProgress->updated_on = $now;return $this->update($userProgress);}// Si no existe, crear nuevo progreso$userProgress = new MicrolearningUserProgress();$userProgress->user_id = $user_id;$userProgress->topic_id = $topic_id;$userProgress->capsule_id = $capsule_id;$userProgress->type = MicrolearningUserProgress::TYPE_CAPSULE;$userProgress->progress = 100;$userProgress->completed = 1;$userProgress->added_on = $now;$userProgress->updated_on = $now;return $this->insert($userProgress);}/*** Marca un tema/tópico como completado por un usuario.** @param int $user_id ID del usuario* @param int $topic_id ID del tema* @return bool true si se marcó como completado correctamente, false en caso de error*/public function markTopicCompleted($user_id, $topic_id){$now = date('Y-m-d H:i:s');// Verifica si ya existe algún progreso del tema$userProgress = $this->fetchOneByUserIdAndTopicId($user_id, $topic_id);if ($userProgress) {// Si ya existe, actualizar el progreso$userProgress->progress = 100;$userProgress->completed = 1;$userProgress->updated_on = $now;return $this->update($userProgress);}// Si no existe, crear nuevo progreso$userProgress = new MicrolearningUserProgress();$userProgress->user_id = $user_id;$userProgress->topic_id = $topic_id;$userProgress->type = MicrolearningUserProgress::TYPE_TOPIC;$userProgress->progress = 100;$userProgress->completed = 1;$userProgress->added_on = $now;$userProgress->updated_on = $now;return $this->insert($userProgress);}/*** Verifica si un usuario ha visto todas las diapositivas de una cápsula específica.** @param int $user_id ID del usuario* @param int $capsule_id ID de la cápsula* @return bool true si ha visto todas las diapositivas, false en caso contrario* @throws \InvalidArgumentException Si alguno de los IDs es inválido*/public function hasViewedAllSlidesInCapsule($user_id, $capsule_id){$slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);$allSlides = $slideMapper->fetchAllByCapsuleId($capsule_id);// Obtener el total de diapositivas vistas por el usuario en esta cápsula$viewedSlides = $this->fetchCountAllSlideCompletedByUserIdAndCapsuleId($user_id, $capsule_id);$totalSlides = count($allSlides);// Comparar si el número de diapositivas vistas es igual al totalreturn $viewedSlides >= $totalSlides;}/*** Verifica si un usuario ha completado todas las cápsulas disponibles en un tema específico.** @param int $user_id ID del usuario* @param int $topic_id ID del tema* @return bool true si el usuario ha completado todas las cápsulas del tema, false en caso contrario* @throws \InvalidArgumentException Si alguno de los IDs es inválido* @throws \RuntimeException Si hay un error en la base de datos*/public function hasCompletedAllCapsulesInTopic($user_id, $topic_id){$capsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);$allCapsules = $capsuleMapper->fetchAllByTopicId($topic_id);if (empty($allCapsules)) {return false;}$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);$record = $this->executeFetchOneArray($select);return intval($record['total']) >= count($allCapsules);}}