/**
  * 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;
 }
 /**
  * Create an instance of the object and load data.
  *
  * <code>
  * $rankId = 1;
  * $rank   = Gamification\Rank\Rank::getInstance(\JFactory::getDbo(), $rankId);
  * </code>
  *
  * @param \JDatabaseDriver $db
  * @param int $id
  *
  * @return null|Rank
  */
 public static function getInstance(\JDatabaseDriver $db, $id)
 {
     if (empty(self::$instances[$id])) {
         $item = new Rank($db);
         $item->load($id);
         self::$instances[$id] = $item;
     }
     return self::$instances[$id];
 }
 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();
 }
 /**
  * Get the rank where the level is positioned.
  *
  * <code>
  * $levelId = 1;
  * $level   = new Gamification\Level\Level(\JFactory::getDbo());
  *
  * $rank    = $level->getRank();
  * </code>
  *
  * @return null|Rank
  */
 public function getRank()
 {
     if (!$this->rank_id) {
         return null;
     }
     if (!$this->rank) {
         $this->rank = Rank::getInstance($this->db, $this->rank_id);
     }
     return $this->rank;
 }
 protected function prepareRankObject($rankId)
 {
     if ($rankId > 0) {
         $key = StringHelper::generateMd5Hash(BasicRank::class, $rankId);
         if ($this->container !== null) {
             if ($this->container->exists($key)) {
                 $this->rank = $this->container->get($key);
             } else {
                 $this->rank = new BasicRank($this->db);
                 $this->rank->setContainer($this->container);
                 $this->rank->load($rankId);
                 $this->container->set($key, $this->rank);
             }
         } else {
             $this->rank = new BasicRank($this->db);
             $this->rank->load($rankId);
         }
     }
 }
 /**
  * Return the ranks as array with objects.
  *
  * <code>
  * $options = array(
  *     "ids" => array(1,2,3,4,5)
  * );
  *
  * $ranks   = new Gamification\Rank\Ranks(\JFactory::getDbo());
  * $ranks->load($options);
  *
  * $ranks = $ranks->getRanks();
  * </code>
  *
  * @return array
  */
 public function getRanks()
 {
     $results = array();
     $i = 0;
     foreach ($this->items as $item) {
         $rank = new Rank($this->db);
         $rank->bind($item);
         $results[$i] = $rank;
         $i++;
     }
     return $results;
 }
 /**
  * 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;
 }
 /**
  * Get the rank where the level is positioned.
  *
  * <code>
  * $keys = array(
  *       "user_id"  => 1,
  *       "group_id" => 2
  * );
  *
  * $userLevel   = new Gamification\User\Level(JFactory::getDbo());
  * $userLevel->load($keys);
  *
  * $rank        = $userLevel->getRank();
  * </code>
  *
  * @return null|Rank
  */
 public function getRank()
 {
     if (!$this->rank_id) {
         return null;
     }
     if (!$this->rank) {
         $this->rank = Rank::getInstance(\JFactory::getDbo(), $this->rank_id);
     }
     return $this->rank;
 }