Пример #1
0
 public function getKarmaLevel()
 {
     $c = new Criteria();
     $c->add('user_id', $this->getUserId());
     $karma = DB_UserKarmaPeer::instance()->selectOne($c);
     if ($karma) {
         return $karma->getLevel();
     } else {
         return 0;
     }
 }
Пример #2
0
 public function updateLevels()
 {
     /* How many points you need to have to get to a level. */
     $minPointsLevel1 = 30;
     $minPointsLevel2 = 100;
     $minPointsLevel3 = 200;
     $minPointsLevel4 = 300;
     $minPointsLevel5 = 500;
     /* Once you pass this limit, we will not take your level5 limit back. */
     $keepLevel5Limit = 1000;
     /* Calculate the distribution. */
     $db = Database::$connection;
     $totalUsers = DB_UserKarmaPeer::instance()->selectCount();
     /* Make karma=none for non-active users. */
     $q = "UPDATE user_karma SET level=0 WHERE points < {$minPointsLevel1}";
     $db->query($q);
     /* Calculate total users but excluding these with less that $minPointsLevel1 points. */
     $c = new Criteria();
     $c->add('points', $minPointsLevel1, '>=');
     $totalUsers = DB_UserKarmaPeer::instance()->selectCount($c);
     /* Number of users to fall into a given level. */
     $limits = array();
     $limitLevel5 = ceil($totalUsers * 0.05);
     $limitLevel4 = ceil($totalUsers * 0.1);
     $limitLevel3 = ceil($totalUsers * 0.2);
     $limitLevel2 = ceil($totalUsers * 0.3);
     //$c = new Criteria();
     //$c->add('points', $minPointsLevel5, '.=');
     //$c->setLimit()
     /* Set level one by default. */
     $q = array();
     $q[] = "UPDATE user_karma SET level=1 WHERE points >= {$minPointsLevel1} AND (level < 5 OR points < {$keepLevel5Limit})";
     $q[] = "UPDATE user_karma SET level=5 WHERE user_id IN (SELECT user_id FROM user_karma WHERE points >= {$minPointsLevel5} ORDER BY points DESC LIMIT {$limitLevel5})";
     $q[] = "UPDATE user_karma SET level=4 WHERE user_id IN (SELECT user_id FROM user_karma WHERE points >= {$minPointsLevel4} AND level < 5 ORDER BY points DESC LIMIT {$limitLevel4})";
     $q[] = "UPDATE user_karma SET level=3 WHERE user_id IN (SELECT user_id FROM user_karma WHERE points >= {$minPointsLevel3} AND level < 4 ORDER BY points DESC LIMIT {$limitLevel3})";
     $q[] = "UPDATE user_karma SET level=2 WHERE user_id IN (SELECT user_id FROM user_karma WHERE points >= {$minPointsLevel2} AND level < 3 ORDER BY points DESC LIMIT {$limitLevel2})";
     $db->query($q);
 }