/** * 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; }
/** * 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; }