Ejemplo n.º 1
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;
 }
Ejemplo n.º 2
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;
 }
Ejemplo n.º 3
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;
 }
Ejemplo n.º 4
0
 /**
  * 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;
 }