/** * Create an instance of the object and load data. * * <code> * // create object points by ID * $pointsId = 1; * $points = Gamification\Points\Points::getInstance(\JFactory::getDbo(), $pointsId); * * // create object points by abbreviation * $keys = array( * "abbr" => "P" * ); * $points = Gamification\Points\Points::getInstance(\JFactory::getDbo(), $keys); * </code> * * @param \JDatabaseDriver $db * @param int|array $keys * * @return null|self */ public static function getInstance($db, $keys) { if (is_array($keys)) { $index = ArrayHelper::getValue($keys, "abbr"); } else { $index = (int) $keys; } $index = \JApplicationHelper::getHash($index); if (!isset(self::$instances[$index])) { $item = new Points($db); $item->load($keys); self::$instances[$index] = $item; } return self::$instances[$index]; }
protected function preparePointsObject($pointsId) { if ($pointsId > 0) { $key = StringHelper::generateMd5Hash(BasicPoints::class, $pointsId); if ($this->container !== null) { if ($this->container->exists($key)) { $this->points = $this->container->get($key); } else { $this->points = new BasicPoints($this->db); $this->points->load($pointsId); $this->container->set($key, $this->points); } } else { $this->points = new BasicPoints($this->db); $this->points->load($pointsId); } } }
/** * Create a record to the database creating first record * where the user will collect points. * * <code> * $keys = array( * 'user_id' => 1, * 'points_id' => 2 * ); * * $data = array( * 'user_id' => 1, * 'points_id' => 2, * 'points_number' => 300 * ); * * $userPoints = new Gamification\User\Points\Points(JFactory::getDbo()); * $userPoints->load($keys); * * $userPoints->startCollectingPoints($data); * </code> * * @param array $data * * @throws \InvalidArgumentException * @throws \RuntimeException * @throws \UnexpectedValueException */ public function startCollectingPoints(array $data = array()) { if (empty($data['user_id'])) { throw new \InvalidArgumentException(\JText::_('LIB_GAMIFICATION_ERROR_INVALID_PARAMETER_USER_ID')); } if (empty($data['points_id'])) { throw new \InvalidArgumentException(\JText::_('LIB_GAMIFICATION_ERROR_INVALID_PARAMETER_POINTS_ID')); } // Reset basic Points object if it does not match with points ID. if ($this->points !== null and $data['points_id'] > 0 and $this->points->getId() !== $data['points_id']) { $this->points = null; } // Create a basic points object. if ($this->points === null and $data['points_id'] > 0) { $this->preparePointsObject($data['points_id']); } $this->id = null; $this->points_id = $this->points->getId(); $this->user_id = (int) $data['user_id']; $this->points_number = array_key_exists('points_number', $data) ? $data['points_number'] : 0; $this->store(); }
/** * Set Points object. * * <code> * $pointsId = 1; * $points = new Gamification\Points\Points(\JFactory::getDbo()); * $points->load($pointsId); * * $level = new Gamification\Level\Level(\JFactory::getDbo()); * $level->setPoints($points); * </code> * * @param Points $points * @throws \UnexpectedValueException * @throws \OutOfBoundsException * * @return self */ public function setPoints(Points $points) { $this->points = $points; if ($this->points_id > 0 and $this->points_id !== $points->getId()) { throw new \UnexpectedValueException('The points ID already exists and it does not much with new Points object.'); } $this->points_id = $points->getId(); // Add the points object in the container. $key = StringHelper::generateMd5Hash(Points::class, $this->points_id); if ($this->container !== null and !$this->container->exists($key)) { $this->container->set($key, $this->points); } return $this; }