/** * Create an object and load user level. * * <code> * $keys = array( * "user_id" => 1, * "group_id" => 2 * ); * $userLevel = Gamification\User\Level::getInstance($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 Level($db, $options); $item->load($keys); self::$instances[$index] = $item; } return self::$instances[$index]; }
/** * Load all user levels and set them to group index. * Every user can have only one level for a group. * * <code> * $options = array( * 'user_id' => 1, * 'group_id' => 2 * ); * * $userLevels = new Gamification\User\Levels(\JFactory::getDbo()); * $userLevels->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.level_id, a.user_id, a.group_id')->select('b.title, b.points, b.value, b.published, b.points_id, b.rank_id, b.group_id')->from($this->db->quoteName('#__gfy_userlevels', 'a'))->innerJoin($this->db->quoteName('#__gfy_levels', 'b') . ' ON a.level_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 (count($results) > 0) { $this->userId = $userId; if ($groupId > 0) { $this->groupId = $groupId; } foreach ($results as $result) { $level = new Level(\JFactory::getDbo()); $level->bind($result); $this->items[$result['group_id']][$level->getLevelId()] = $level; } } }
/** * Load all user levels and set them to group index. * Every user can have only one level for a group. * * <code> * $options = array( * "user_id" => 1, * "group_id" => 2 * ); * * $userLevels = new Gamification\User\Levels(\JFactory::getDbo()); * $userLevels->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.level_id, a.user_id, a.group_id")->select("b.title, b.points, b.value, b.published, b.points_id, b.rank_id, b.group_id")->from($this->db->quoteName("#__gfy_userlevels", "a"))->innerJoin($this->db->quoteName("#__gfy_levels", "b") . ' ON a.level_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) { $level = new Level(\JFactory::getDbo()); $level->bind($result); $this->items[$result["group_id"]][$level->getLevelId()] = $level; } } }
/** * Prepare current and next level. * * @param array $keys */ protected function prepareLevels($keys) { $this->currentUnit = Level::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.value, a.published, a.points_id, a.rank_id, a.group_id")->from($this->db->quoteName("#__gfy_levels", "a"))->where("a.points_id = " . (int) $this->points->getPointsId())->where("a.points > " . (int) $userPoints); $this->db->setQuery($query, 0, 1); $result = $this->db->loadObject(); if (!empty($result)) { $this->nextUnit = new \Gamification\Level\Level($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; } }