/** * 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; }