Esempio n. 1
0
 /**
  * Pre-processor for $table->store($data)
  *
  * @param   Points $userPoints
  * @param   array $options
  *
  * @return  void
  */
 public function onAfterPointsIncrease($userPoints, $options = array())
 {
     // Get the context.
     $alias = isset($options["context"]) ? $options["context"] : "";
     // Check for allowed context.
     if (!in_array($alias, $this->allowedContext)) {
         return;
     }
     // Load all badges.
     $keys = array("user_id" => $userPoints->getUserId(), "group_id" => $userPoints->getGroupId());
     $badge = new Points\Badge(\JFactory::getDbo());
     $badge->setUserPoints($userPoints);
     if ($badge->giveBadge($options) and ($this->storeActivity or $this->sendNotification)) {
         // Give a badge.
         $params = \JComponentHelper::getParams("com_gamification");
         $user = \JFactory::getUser($userPoints->getUserId());
         $optionsActivitiesNotifications = array("social_platform" => "", "user_id" => $user->get("id"), "context_id" => $user->get("id"), "app" => "gamification.badge");
         $activityService = $params->get("integration_activities");
         if ($this->storeActivity and $activityService) {
             $optionsActivitiesNotifications["social_platform"] = $activityService;
             $message = \JText::sprintf("LIB_GAMIFICATION_BADGING_RECEIVE_NEW_BADGE", $user->get("name"), $badge->getTitle());
             Helper::storeActivity($message, $optionsActivitiesNotifications);
         }
         $notificationService = $params->get("integration_notifications");
         if ($this->sendNotification and $notificationService) {
             $optionsActivitiesNotifications["social_platform"] = $notificationService;
             $message = \JText::sprintf("LIB_GAMIFICATION_BADGING_NOTIFICATION", $badge->getTitle());
             Helper::sendNotification($message, $optionsActivitiesNotifications);
         }
     }
 }
Esempio n. 2
0
 /**
  * Create an object and load user level.
  *
  * <code>
  * // Create and initialize the object using points ID.
  * $keys = array(
  *       "user_id"   => 1,
  *       "points_id" => 2
  * );
  * $userPoints    = Gamification\User\Points::getInstance(\JFactory::getDbo(), $keys);
  *
  * // Create and initialize the object using abbreviation.
  * $keys = array(
  *       "user_id"  => 1,
  *       "abbr"     => "P"
  * );
  * $userPoints    = Gamification\User\Points::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");
     $pointsId = ArrayHelper::getValue($keys, "points_id");
     $abbr = ArrayHelper::getValue($keys, "abbr");
     $index = null;
     if (!empty($pointsId)) {
         $index = md5($userId . ":" . $pointsId);
     } elseif (!empty($abbr)) {
         $index = md5($userId . ":" . $abbr);
     }
     if (!isset(self::$instances[$index])) {
         $item = new Points($db);
         $item->load($keys, $options);
         self::$instances[$index] = $item;
     }
     return self::$instances[$index];
 }
Esempio n. 3
0
 /**
  * Pre-processor for $table->store($data)
  *
  * @param   Points $userPoints
  * @param   array $options
  *
  * @return  void
  */
 public function onAfterPointsIncrease($userPoints, $options = array())
 {
     // Get the context.
     $alias = isset($options["context"]) ? $options["context"] : "";
     // Check for allowed context.
     if (!in_array($alias, $this->allowedContext)) {
         return;
     }
     $keys = array("user_id" => $userPoints->getUserId(), "group_id" => $userPoints->getGroupId());
     // Get user level
     $level = new Points\Level(\JFactory::getDbo());
     $level->load($keys);
     $level->setUserPoints($userPoints);
     if (!$level->getId()) {
         // Create a level record
         $level->startLeveling($keys);
     } else {
         // Level UP
         if ($level->levelUp($options) and ($this->storeActivity or $this->sendNotification)) {
             $params = \JComponentHelper::getParams("com_gamification");
             $user = \JFactory::getUser($userPoints->getUserId());
             $optionsActivitiesNotifications = array("social_platform" => "", "user_id" => $user->get("id"), "context_id" => $user->get("id"), "app" => "gamification.level");
             $activityService = $params->get("integration_activities");
             if ($this->storeActivity and $activityService) {
                 $optionsActivitiesNotifications["social_platform"] = $activityService;
                 $message = \JText::sprintf("LIB_GAMIFICATION_LEVELING_LEVEL_UP", $user->get("name"), $level->getLevel());
                 Helper::storeActivity($message, $optionsActivitiesNotifications);
             }
             $notificationService = $params->get("integration_notifications");
             if ($this->sendNotification and $notificationService) {
                 $optionsActivitiesNotifications["social_platform"] = $notificationService;
                 $message = \JText::sprintf("LIB_GAMIFICATION_LEVELING_NOTIFICATION", $level->getLevel());
                 Helper::sendNotification($message, $optionsActivitiesNotifications);
             }
         }
     }
 }
Esempio n. 4
0
 /**
  * Pre-processor for $table->store($data)
  *
  * @param   Points $userPoints
  * @param   array $options
  *
  * @return  void
  */
 public function onAfterPointsIncrease($userPoints, $options = array())
 {
     // Get the context.
     $alias = isset($options["context"]) ? $options["context"] : "";
     // Check for allowed context.
     if (!in_array($alias, $this->allowedContext)) {
         return;
     }
     $keys = array("user_id" => $userPoints->getUserId(), "group_id" => $userPoints->getGroupId());
     // Get user rank.
     $rank = new Points\Rank(\JFactory::getDbo());
     $rank->load($keys);
     $rank->setUserPoints($userPoints);
     if (!$rank->getId()) {
         // Create a rank record
         $rank->startRanking($keys);
     } else {
         // Give new rank.
         if ($rank->giveRank($options) and ($this->storeActivity or $this->sendNotification)) {
             $params = \JComponentHelper::getParams("com_gamification");
             $user = \JFactory::getUser($userPoints->getUserId());
             $optionsActivitiesNotifications = array("social_platform" => "", "user_id" => $user->get("id"), "context_id" => $user->get("id"), "app" => "gamification.rank");
             $activityService = $params->get("integration_activities");
             if ($this->storeActivity and $activityService) {
                 $optionsActivitiesNotifications["social_platform"] = $activityService;
                 $message = \JText::sprintf("LIB_GAMIFICATION_RANKING_REACH_NEW_RANK", $user->get("name"), $rank->getTitle());
                 Helper::storeActivity($message, $optionsActivitiesNotifications);
             }
             $notificationService = $params->get("integration_notifications");
             if ($this->sendNotification and $notificationService) {
                 $optionsActivitiesNotifications["social_platform"] = $notificationService;
                 $message = \JText::sprintf("LIB_GAMIFICATION_RANKING_NOTIFICATION", $rank->getTitle());
                 Helper::sendNotification($message, $optionsActivitiesNotifications);
             }
         }
     }
 }
Esempio n. 5
0
 /**
  * Find a rank that actual have to be.
  *
  * @return null|int Rank ID
  */
 protected function findActualRankId()
 {
     // Get all ranks
     $query = $this->db->getQuery(true);
     $query->select("a.id, a.points")->from($this->db->quoteName("#__gfy_ranks", "a"))->where("a.points_id = " . (int) $this->userPoints->getPointsId());
     $this->db->setQuery($query);
     $results = $this->db->loadAssocList();
     $rankId = null;
     for ($i = 0, $max = count($results); $i < $max; $i++) {
         // Get current item
         $current = isset($results[$i]) ? $results[$i] : null;
         /** @var $current object */
         // Get next item
         $n = abs($i + 1);
         $next = isset($results[$n]) ? $results[$n] : null;
         /** @var $next object */
         if (!empty($next)) {
             // Check for coincidence with next item
             if ($next["points"] == $this->userPoints->getPoints()) {
                 $rankId = $next["id"];
                 break;
             }
             // Check for coincidence with current item
             if ($current["points"] <= $this->userPoints->getPoints() and $next["points"] > $this->userPoints->getPoints()) {
                 $rankId = $current["id"];
                 break;
             }
         } else {
             // If there is not next item, we compare it with last (current one).
             if ($current["points"] <= $this->userPoints->getPoints()) {
                 $rankId = $current["id"];
                 break;
             }
         }
     }
     return $rankId;
 }
Esempio n. 6
0
 /**
  * Find a badge that has to be given to the user.
  *
  * @return null|array
  */
 protected function findActualBadge()
 {
     // Get all badges for given points.
     $query = $this->db->getQuery(true);
     $query->select("a.id AS badge_id, a.title, a.points")->from($this->db->quoteName("#__gfy_badges", "a"))->where("a.points_id = " . (int) $this->userPoints->getPointsId())->where("a.published = " . (int) Constants::PUBLISHED);
     $this->db->setQuery($query);
     $results = $this->db->loadAssocList();
     $badge = null;
     for ($i = 0, $max = count($results); $i < $max; $i++) {
         // Get current item
         $current = isset($results[$i]) ? $results[$i] : null;
         /** @var $current object */
         // Get next item
         $n = abs($i + 1);
         $next = isset($results[$n]) ? $results[$n] : null;
         /** @var $next object */
         if (!empty($next)) {
             // Check for coincidence with next item.
             if ($next["points"] == $this->userPoints->getPoints()) {
                 $badge = $next;
                 break;
             }
             // Check for coincidence with current item.
             if ($current["points"] <= $this->userPoints->getPoints() and $next["points"] > $this->userPoints->getPoints()) {
                 $badge = $current;
                 break;
             }
         } else {
             // If there is not next item, we compare it with last (current one).
             if ($current["points"] <= $this->userPoints->getPoints()) {
                 $badge = $current;
                 break;
             }
         }
     }
     return $badge;
 }
Esempio n. 7
0
 /**
  * Find the level that has to be reached by the user.
  *
  * @return null|integer
  */
 protected function findActualLevelId()
 {
     // Get all levels
     $query = $this->db->getQuery(true);
     $query->select("a.id, a.points")->from($this->db->quoteName("#__gfy_levels", "a"))->where("a.points_id = " . (int) $this->userPoints->getPointsId());
     $this->db->setQuery($query);
     $results = $this->db->loadObjectList();
     $levelId = null;
     for ($i = 0, $max = count($results); $i < $max; $i++) {
         // Get current item
         $current = isset($results[$i]) ? $results[$i] : null;
         /** @var $current object */
         // Get next item
         $n = abs($i + 1);
         $next = isset($results[$n]) ? $results[$n] : null;
         /** @var $next object */
         if (!empty($next)) {
             // Check for coincidence with next item
             if ($this->userPoints->getPoints() == $next->points) {
                 $levelId = $next->id;
                 break;
             }
             // Check for coincidence with current item
             if ($this->userPoints->getPoints() >= $current->points and $this->userPoints->getPoints() < $next->points) {
                 $levelId = $current->id;
                 break;
             }
         } else {
             // If there is not next item, we compare with last (current).
             if ($this->userPoints->getPoints() >= $current->points) {
                 $levelId = $current->id;
                 break;
             }
         }
     }
     return $levelId;
 }
 /**
  * Return the number of points, which the user has.
  *
  * <code>
  * // Get user points
  * $keys = array(
  *       "user_id"   => 1,
  *       "points_id" => 2
  * );
  * $userPoints    = Gamification\User\Points::getInstance(\JFactory::getDbo, $keys);
  *
  * // A game mechanic - levels, ranks, badges,...
  * $gameMechanic  = "levels";
  *
  * $progressBar   = new Gamification\User\ProgressBar(\JFactory::getDbo, $userPoints);
  * $progressBar->build($gameMechanic);
  *
  * $points        = $progressBar->getPoints();
  * </code>
  *
  * @return integer
  */
 public function getPoints()
 {
     return !empty($this->points) ? $this->points->getPoints() : 0;
 }