コード例 #1
0
 /**
  * Find the reward that has to be reached by the user.
  *
  * <code>
  * $keys   = array(
  *    'user_id' => 1,
  *    'points_id' => 2
  * );
  * $points = new Gamification\User\Points\Points(\JFactory::getDbo());
  * $points->load($keys);
  *
  * $rewardSeeker = new Gamification\User\Reward\RewardPointsSeeker(\JFactory::getDbo());
  * $rewardSeeker->setUserPoints($points);
  *
  * $newReward = $rewardSeeker->find();
  * </code>
  *
  * @throws \RuntimeException
  *
  * @return null|BasicReward
  */
 public function find()
 {
     // Get basic rewards based on specific points.
     $options = array('points_id' => (int) $this->userPoints->getPointsId(), 'state' => (int) Constants::PUBLISHED);
     $rewards = new BasicRewards($this->db);
     $rewards->load($options);
     $results = $rewards->toArray();
     /** @var array $results */
     $rewardData = array();
     for ($i = 0, $max = count($results); $i < $max; $i++) {
         // Get current item
         $current = array_key_exists($i, $results) ? $results[$i] : array();
         /** @var $current array */
         // Get next item
         $n = $i + 1;
         $next = array_key_exists($n, $results) ? $results[$n] : array();
         /** @var $next array */
         if (count($next) > 0) {
             // Check for coincidence with next item
             if ((int) $next['points_number'] === (int) $this->userPoints->getPointsNumber()) {
                 $rewardData = $next;
                 break;
             }
             // Check for coincidence with current item
             if ((int) $current['points_number'] <= (int) $this->userPoints->getPointsNumber() and (int) $next['points_number'] > (int) $this->userPoints->getPointsNumber()) {
                 $rewardData = $current;
                 break;
             }
         } else {
             // If there is not next item, we compare with last (current).
             if ((int) $current['points_number'] <= (int) $this->userPoints->getPointsNumber()) {
                 $rewardData = $current;
                 break;
             }
         }
     }
     // Create a reward object.
     $reward = null;
     if (count($rewardData) > 0) {
         $reward = new BasicReward($this->db);
         $reward->bind($rewardData);
     }
     return $reward;
 }
コード例 #2
0
 /**
  * Change user reward to higher one.
  *
  * <code>
  * $context = "com_user.registration";
  *
  * $keys = array(
  *     "id" => 1,
  *     "group_id" => 2
  * );
  *
  * // Create user reward object based.
  * $reward  = new Gamification\Reward\Reward(\JFactory::getDbo());
  * $reward->load($keys);
  *
  * $rewardManager = new Gamification\User\Reward\RewardManager(\JFactory::getDbo());
  * $rewardManager->setReward($reward);
  *
  * if ($rewardManager->give($context, $userId, $options)) {
  * // ...
  * }
  * </code>
  *
  * @param string $context
  * @param int $userId
  * @param array $options
  *
  * @throws \UnexpectedValueException
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  *
  * @return null|Reward
  */
 public function give($context, $userId, array $options = array())
 {
     if (!$this->reward instanceof BasicReward) {
         throw new \UnexpectedValueException('It is missing user reward object.');
     }
     // Check if this reward already exists in user profile.
     $userProfile = new Profile($this->db);
     $rewardReceived = $userProfile->isRewardReceived($this->reward, $userId);
     if (!$rewardReceived) {
         $data = array('user_id' => $userId, 'group_id' => $this->reward->getGroupId(), 'reward_id' => $this->reward->getId());
         // Create user reward.
         $userReward = new Reward(\JFactory::getDbo());
         $userReward->bind($data);
         // Implement JObservableInterface: Pre-processing by observers
         $this->observers->update('onBeforeGiveReward', array($context, &$userReward, &$options));
         $userReward->store();
         // Implement JObservableInterface: Post-processing by observers
         $this->observers->update('onAfterGiveReward', array($context, &$userReward, &$options));
         return $userReward;
     }
     return null;
 }
コード例 #3
0
 public function isRewardReceived(Reward $reward, $userId = 0)
 {
     if (!$userId and $this->id > 0) {
         $userId = $this->id;
     }
     $query = $this->db->getQuery(true);
     $query->select('COUNT(*)')->from($this->db->quoteName('#__gfy_userrewards', 'a'))->where('a.reward_id  = ' . (int) $reward->getId())->where('a.user_id  = ' . (int) $userId);
     $this->db->setQuery($query, 0, 1);
     return (bool) $this->db->loadResult();
 }
コード例 #4
0
 /**
  * Return the rewards as array with objects.
  *
  * <code>
  * $options = array(
  *     "ids" => array(1,2,3,4,5)
  * );
  *
  * $rewards   = new Gamification\Reward\Rewards(\JFactory::getDbo());
  * $rewards->load($options);
  *
  * $rewards = $rewards->getRewards();
  * </code>
  *
  * @return array
  */
 public function getRewards()
 {
     $results = array();
     $i = 0;
     foreach ($this->items as $item) {
         $reward = new Reward($this->db);
         $reward->bind($item);
         $results[$i] = $reward;
         $i++;
     }
     return $results;
 }