/** * Change user rank to higher one. * * <code> * $context = "com_user.registration"; * * $keys = array( * "id" => 1, * "group_id" => 2 * ); * * // Create user badge object based. * $rank = new Gamification\Rank\Rank(\JFactory::getDbo()); * $rank->load($keys); * * $rankManager = new Gamification\User\Rank\RankManager(\JFactory::getDbo()); * $rankManager->setRank($rank); * * if ($rankManager->give($context, $userId, $options)) { * // ... * } * </code> * * @param string $context * @param int $userId * @param array $options * * @throws \UnexpectedValueException * @throws \RuntimeException * @throws \InvalidArgumentException * * @return null|Rank */ public function give($context, $userId, array $options = array()) { if (!$this->rank instanceof BasicRank) { throw new \UnexpectedValueException('It is missing user rank object.'); } $keys = array('user_id' => $userId, 'group_id' => $this->rank->getGroupId()); $userRank = new Rank(\JFactory::getDbo()); $userRank->load($keys); // Implement JObservableInterface: Pre-processing by observers $this->observers->update('onBeforeGiveRank', array($context, &$userRank, &$options)); if (!$userRank->getId()) { // Start ranking. $keys['rank_id'] = $this->rank->getId(); $userRank->startRanking($keys); } else { if ((int) $this->rank->getId() === (int) $userRank->getRankId()) { return null; } // Change the current rank ID with another one. $userRank->setRankId($this->rank->getId()); $userRank->store(); } // Implement JObservableInterface: Post-processing by observers $this->observers->update('onAfterGiveRank', array($context, &$userRank, &$options)); return $userRank; }
/** * Set Rank object. * * <code> * $rankId = 1; * $rank = new Gamification\Rank\Rank(\JFactory::getDbo()); * $rank->load($rankId); * * $level = new Gamification\Level\Level(\JFactory::getDbo()); * $level->setRank($rank); * </code> * * @param Rank $rank * @throws \UnexpectedValueException * @throws \OutOfBoundsException * * @return self */ public function setRank(Rank $rank) { $this->rank = $rank; if ($this->rank_id > 0 and $this->rank_id !== $rank->getId()) { throw new \UnexpectedValueException('The points ID already exists and it does not much with new Rank object.'); } $this->rank_id = $rank->getId(); // Add the points object in the container. $key = StringHelper::generateMd5Hash(Points::class, $this->rank_id); if ($this->container !== null and !$this->container->exists($key)) { $this->container->set($key, $this->rank); } return $this; }
public function isRankAchieved(Rank $rank, $userId = 0) { if (!$userId and $this->id > 0) { $userId = $this->id; } $query = $this->db->getQuery(true); $query->select('COUNT(*)')->from($this->db->quoteName('#__gfy_userranks', 'a'))->where('a.rank_id = ' . (int) $rank->getId())->where('a.user_id = ' . (int) $userId)->where('a.group_id = ' . (int) $rank->getGroupId()); $this->db->setQuery($query, 0, 1); return (bool) $this->db->loadResult(); }