/** * Create and initialize the object. * * <code> * $keys = array( * "user_id" => 1, * "group_id" => 2 * ); * $userRank = Gamification\User\Rank::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 Rank($db, $options); $item->load($keys); self::$instances[$index] = $item; } return self::$instances[$index]; }
/** * Load all user ranks and set them to group index. * Every user can have only one rank for a group. * * <code> * $options = array( * 'user_id' => 1, * 'group_id' => 2 * ); * * $userRanks = new Gamification\User\Ranks(JFactory::getDbo()); * $userRanks->load($options); * </code> * * @param array $options */ public function load(array $options = array()) { $userId = $this->getOptionId($options, 'user_id'); $groupId = $this->getOptionId($options, 'group_id'); // Create a new query object. $query = $this->db->getQuery(true); $query->select('a.rank_id, a.user_id, a.group_id')->select('b.title, b.points, b.image, b.published, b.points_id, b.group_id')->from($this->db->quoteName('#__gfy_userranks', 'a'))->innerJoin($this->db->quoteName('#__gfy_ranks', 'b') . ' ON a.rank_id = b.id')->where('a.user_id = ' . (int) $userId); if ($groupId > 0) { $query->where('a.group_id = ' . (int) $groupId); } $this->db->setQuery($query); $results = (array) $this->db->loadAssocList(); if (count($results) > 0) { $this->userId = $userId; foreach ($results as $result) { $rank = new Rank(\JFactory::getDbo()); $rank->bind($result); $this->items[$result['group_id']][$rank->getRankId()] = $rank; } } }
/** * Load all user ranks and set them to group index. * Every user can have only one rank for a group. * * <code> * $options = array( * "user_id" => 1, * "group_id" => 2 * ); * * $userRanks = new Gamification\User\Ranks(JFactory::getDbo()); * $userRanks->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.rank_id, a.user_id, a.group_id")->select("b.title, b.points, b.image, b.published, b.points_id, b.group_id")->from($this->db->quoteName("#__gfy_userranks", "a"))->innerJoin($this->db->quoteName("#__gfy_ranks", "b") . ' ON a.rank_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; foreach ($results as $result) { $rank = new Rank(\JFactory::getDbo()); $rank->bind($result); $this->items[$result["group_id"]][$rank->getRankId()] = $rank; } } }
/** * Prepare current and next ranks. * * @param array $keys */ protected function prepareRanks($keys) { $this->currentUnit = Rank::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.published, a.points_id, a.group_id")->from($this->db->quoteName("#__gfy_ranks", "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\Rank\Rank($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; } }