/** * Create an instance of the object and load data. * * <code> * $badgeId = 1; * $badge = Gamification\Badge\Badge::getInstance(\JFactory::getDbo(), $badgeId); * </code> * * @param \JDatabaseDriver $db * @param int $id * * @return null|self */ public static function getInstance($db, $id) { if (!isset(self::$instances[$id])) { $item = new Badge($db); $item->load($id); self::$instances[$id] = $item; } return self::$instances[$id]; }
public function hasBadge(Badge $badge, $userId = 0) { if (!$userId and $this->id > 0) { $userId = $this->id; } $query = $this->db->getQuery(true); $query->select('COUNT(*)')->from($this->db->quoteName('#__gfy_userbadges', 'a'))->where('a.badge_id = ' . (int) $badge->getId())->where('a.user_id = ' . (int) $userId)->where('a.group_id = ' . (int) $badge->getGroupId()); $this->db->setQuery($query, 0, 1); return (bool) $this->db->loadResult(); }
/** * Find a badge that has to be given to the user. * * <code> * $keys = array( * 'user_id' => 1, * 'points_id' => 2 * ); * $points = new Gamification\User\Points\Points(\JFactory::getDbo()); * $points->load($keys); * * $badgeSeeker = new Gamification\User\Badge\BadgePointsSeeker(\JFactory::getDbo()); * $badgeSeeker->setUserPoints($points); * * $newBadge = $badgeSeeker->find(); * </code> * * @throws \RuntimeException * * @return null|BasicBadge */ public function find() { // Get basic badges based on specific points. $options = array('points_id' => (int) $this->userPoints->getPointsId(), 'state' => (int) Constants::PUBLISHED); $badges = new BasicBadges($this->db); $badges->load($options); $results = $badges->toArray(); /** @var array $results */ $badgeData = 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'] === $this->userPoints->getPointsNumber()) { $badgeData = $next; break; } // Check for coincidence with current item. if ((int) $current['points_number'] <= $this->userPoints->getPointsNumber() and (int) $next['points_number'] > $this->userPoints->getPointsNumber()) { $badgeData = $current; break; } } else { // If there is not next item, we compare it with last (current one). if ((int) $current['points_number'] <= $this->userPoints->getPointsNumber()) { $badgeData = $current; break; } } } // Create a badge object. $badge = null; if (count($badgeData) > 0) { $badge = new BasicBadge($this->db); $badge->bind($badgeData); } return $badge; }
/** * Return the badges as array with objects. * * <code> * $options = array( * "ids" => array(1,2,3,4,5) * ); * * $badges = new Gamification\Badge\Badges(\JFactory::getDbo()); * $badges->load($options); * * $badges = $badges->getBadges(); * </code> * * @return array */ public function getBadges() { $results = array(); $i = 0; foreach ($this->items as $item) { $badge = new Badge($this->db); $badge->bind($item); $results[$i] = $badge; $i++; } return $results; }
/** * Set Badge object. * This method will reset badge ID, user ID the ID of the record. * * <code> * $userBadge = new Gamification\User\Badge\Badge(JFactory::getDbo()); * * $basicBadge = new Gamification\Points\Points(JFactory::getDbo()); * $userBadge->setBadge($basicBadge); * </code> * * @param BasicBadge $badge * * @throws \OutOfBoundsException * * @return self */ public function setBadge(BasicBadge $badge) { $this->badge = $badge; $this->id = null; $this->user_id = null; $this->badge_id = $badge->getId(); // Add the badge object in the container. $key = StringHelper::generateMd5Hash(BasicBadge::class, $this->badge_id); if ($this->container !== null and !$this->container->exists($key)) { $this->container->set($key, $this->badge); } return $this; }