/**
  * 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;
 }
 /**
  * Prepare current and next ranks.
  *
  * @throws \RuntimeException
  */
 public function prepareData()
 {
     $userPoints = (int) $this->points->getPointsNumber();
     // Get current level.
     $keys = array('user_id' => $this->points->getUserId(), 'group_id' => $this->points->getPoints()->getGroupId());
     $userRank = new UserRank($this->db);
     $userRank->load($keys);
     $this->currentUnit = $userRank->getRank();
     // Get incoming level.
     $query = $this->db->getQuery(true);
     $query->select('a.id, a.title, a.description, a.image, a.activity_text, a.published, a.points_id, a.points_number, a.group_id')->from($this->db->quoteName('#__gfy_ranks', 'a'))->where('a.points_id = ' . (int) $this->points->getPointsId())->where('a.published = ' . (int) Constants::PUBLISHED)->where('a.points_number > ' . $userPoints)->order('a.points_number ASC');
     $this->db->setQuery($query, 0, 1);
     $result = (array) $this->db->loadAssoc();
     if (count($result) > 0) {
         $this->nextUnit = new BasicRank($this->db);
         $this->nextUnit->bind($result);
         $this->percentageCurrent = (int) MathHelper::calculatePercentage($userPoints, $this->nextUnit->getPointsNumber());
         $this->percentageNext = 100 - $this->percentageCurrent;
     } else {
         $this->percentageCurrent = 100;
         $this->percentageNext = 100;
     }
 }
 public static function rank(UserRank $rank, $mediaPath, $tip = false, $placeholders = array())
 {
     $title = '';
     $class = '';
     $classes = array();
     if ($tip and $rank->getRank()->getActivityText()) {
         JHtml::_('bootstrap.tooltip');
         $classes[] = 'hasTooltip';
         $description = strip_tags(trim($rank->getRank()->getActivityText($placeholders)));
         $title = ' title="' . htmlspecialchars($description, ENT_QUOTES, 'UTF-8') . '"';
     }
     // Prepare class property
     if (count($classes) > 0) {
         $class = ' class="' . implode(' ', $classes) . '"';
     }
     // Prepare alt property
     $alt = strip_tags(trim($rank->getRank()->getTitle()));
     if ($alt !== null and $alt !== '') {
         $alt = ' alt="' . htmlspecialchars($alt, ENT_QUOTES, 'UTF-8') . '"';
     }
     $html = '<img src="' . $mediaPath . '/' . $rank->getRank()->getImage() . '"' . $class . $alt . $title . ' />';
     return $html;
 }
Exemplo n.º 4
0
 protected function prepareRankObject($rankId)
 {
     if ($rankId > 0) {
         $key = StringHelper::generateMd5Hash(Rank::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);
         }
     }
 }