/** * Change user achievement to higher one. * * <code> * $context = "com_user.registration"; * * $keys = array( * "id" => 1, * "group_id" => 2 * ); * * // Create user achievement object based. * $achievement = new Gamification\Achievement\Achievement(\JFactory::getDbo()); * $achievement->load($keys); * * $achievementManager = new Gamification\User\Achievement\AchievementManager(\JFactory::getDbo()); * $achievementManager->setAchievement($achievement); * * if ($achievementManager->accomplish($context, $userId, $options)) { * // ... * } * </code> * * @param string $context * @param int $userId * @param array $options * * @throws \UnexpectedValueException * @throws \RuntimeException * @throws \InvalidArgumentException * * @return null|Achievement */ public function accomplish($context, $userId, array $options = array()) { if (!$this->achievement instanceof BasicAchievement) { throw new \UnexpectedValueException('It is missing user achievement object.'); } // Check if this achievement already exists in user profile. $userProfile = new Profile($this->db); $achievementAccomplished = $userProfile->isAchievementAccomplished($this->achievement, $userId); if (!$achievementAccomplished) { $date = new \JDate(); $data = array('user_id' => $userId, 'achievement_id' => $this->achievement->getId(), 'accomplished' => 1, 'accomplished_at' => $date->toSql()); // Create user achievement. $userAchievement = new Achievement(\JFactory::getDbo()); $userAchievement->bind($data); // Implement JObservableInterface: Pre-processing by observers $this->observers->update('onBeforeAccomplishAchievement', array($context, &$userAchievement, &$options)); $userAchievement->store(); // Implement JObservableInterface: Post-processing by observers $this->observers->update('onAfterAccomplishAchievement', array($context, &$userAchievement, &$options)); return $userAchievement; } return null; }
protected function prepareAchievementObject($achievementId) { if ($achievementId > 0) { $key = StringHelper::generateMd5Hash(Achievement::class, $achievementId); if ($this->container !== null) { if ($this->container->exists($key)) { $this->achievement = $this->container->get($key); } else { $this->achievement = new BasicAchievement($this->db); $this->achievement->setContainer($this->container); $this->achievement->load($achievementId); $this->container->set($key, $this->achievement); } } else { $this->achievement = new BasicAchievement($this->db); $this->achievement->load($achievementId); } } }