/**
  * Change user level to higher one.
  *
  * <code>
  * $context = "com_user.registration";
  *
  * $keys = array(
  *     "id" => 1,
  *     "group_id" => 2
  * );
  *
  * // Create user badge object based.
  * $level  = new Gamification\Level\Level(\JFactory::getDbo());
  * $level->load($keys);
  *
  * $levelManager = new Gamification\User\Level\LevelManager(\JFactory::getDbo());
  * $levelManager->setLevel($level);
  *
  * if ($levelManager->give($context, $userId, $options)) {
  * // ...
  * }
  * </code>
  *
  * @param string $context
  * @param int $userId
  * @param array $options
  *
  * @throws \UnexpectedValueException
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  *
  * @return null|Level
  */
 public function levelUp($context, $userId, array $options = array())
 {
     if (!$this->level instanceof BasicLevel) {
         throw new \UnexpectedValueException('It is missing user level object.');
     }
     $keys = array('user_id' => $userId, 'group_id' => $this->level->getGroupId());
     $userLevel = new Level(\JFactory::getDbo());
     $userLevel->load($keys);
     // Implement JObservableInterface: Pre-processing by observers
     $this->observers->update('onBeforeLevelUp', array($context, &$userLevel, &$options));
     if (!$userLevel->getId()) {
         $keys['level_id'] = $this->level->getId();
         $userLevel->startLeveling($keys);
     } else {
         if ((int) $this->level->getId() === (int) $userLevel->getLevelId()) {
             return null;
         }
         // Change the current rank ID with another one.
         $userLevel->setLevelId($this->level->getId());
         $userLevel->store();
     }
     // Implement JObservableInterface: Post-processing by observers
     $this->observers->update('onAfterLevelUp', array($context, &$userLevel, &$options));
     return $userLevel;
 }
 /**
  * Prepare current and next levels.
  *
  * @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());
     $userLevel = new UserLevel($this->db);
     $userLevel->load($keys);
     $this->currentUnit = $userLevel->getLevel();
     // Get incoming level.
     $query = $this->db->getQuery(true);
     $query->select('a.id, a.title, a.value, a.published, a.points_id, a.points_number, a.rank_id, a.group_id')->from($this->db->quoteName('#__gfy_levels', '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 BasicLevel($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;
     }
 }
Ejemplo n.º 3
0
 protected function prepareLevelObject($levelId)
 {
     if ($levelId > 0) {
         $key = StringHelper::generateMd5Hash(BasicLevel::class, $levelId);
         if ($this->container !== null) {
             if ($this->container->exists($key)) {
                 $this->level = $this->container->get($key);
             } else {
                 $this->level = new BasicLevel($this->db);
                 $this->level->setContainer($this->container);
                 $this->level->load($levelId);
                 $this->container->set($key, $this->level);
             }
         } else {
             $this->level = new BasicLevel($this->db);
             $this->level->load($levelId);
         }
     }
 }