/** * Create an object and load user badge. * * <code> * $keys = array( * "user_id" => 1, * "group_id" => 2 * ); * $userBadge = Gamification\User\Badge::getInstance(\JFactory::getDbo(), $keys); * </code> * * @param \JDatabaseDriver $db * @param array $keys * @param array $options * * @return null|self */ public static function getInstance(\JDatabaseDriver $db, array $keys, array $options = array()) { $userId = ArrayHelper::getValue($keys, "user_id"); $groupId = ArrayHelper::getValue($keys, "group_id"); $index = md5($userId . ":" . $groupId); if (!isset(self::$instances[$index])) { $item = new Badge($db); $item->load($keys, $options); self::$instances[$index] = $item; } return self::$instances[$index]; }
/** * Prepare current and next badges. * * @param array $keys */ protected function prepareBadges($keys) { $this->currentUnit = Badge::getInstance($this->db, $keys); $userPoints = $this->points->getPoints(); // Get all units $query = $this->db->getQuery(true); $query->select("a.id, a.title, a.points, a.image, a.note, a.published, a.points_id, a.group_id")->from($this->db->quoteName("#__gfy_badges", "a"))->where("a.points_id = " . (int) $this->points->getPointsId())->where("a.published = 1")->where("a.points > " . (int) $userPoints); $this->db->setQuery($query, 0, 1); $result = $this->db->loadObject(); if (!empty($result)) { $this->nextUnit = new \Gamification\Badge\Badge($this->db); $this->nextUnit->bind($result); $this->percentage = $this->calculatePercentage($userPoints, $this->nextUnit->getPoints()); $this->percentNext = 100 - $this->percentage; } else { $this->percentage = 100; $this->percentNext = 100; } }
/** * Load all user badges and set them to group index. * Every user can have only one badge for a group. * * <code> * $options = array( * "user_id" => 1, * "group_id" => 2 * ); * * $userBadges = new Gamification\User\Badges(\JFactory::getDbo()) * $userBadges->load($options); * </code> * * @param array $options */ public function load($options = array()) { $userId = ArrayHelper::getValue($options, "user_id"); $groupId = ArrayHelper::getValue($options, "group_id"); // Create a new query object. $query = $this->db->getQuery(true); $query->select("a.id, a.badge_id, a.user_id, a.group_id, " . "b.title, b.description, b.points, b.image, b.published, b.points_id, b.group_id")->from($this->db->quoteName("#__gfy_userbadges", "a"))->innerJoin($this->db->quoteName("#__gfy_badges", "b") . ' ON a.badge_id = b.id')->where("a.user_id = " . (int) $userId); if (!empty($groupId)) { $query->where("a.group_id = " . (int) $groupId); } $this->db->setQuery($query); $results = (array) $this->db->loadAssocList(); if (!empty($results)) { $this->userId = $userId; if (!empty($groupId)) { $this->groupId = $groupId; } foreach ($results as $result) { $badge = new Badge(\JFactory::getDbo()); $badge->bind($result); $this->items[$result["group_id"]][$result["badge_id"]] = $badge; } } }