Question

I'm trying to write a piece of code that checks a users stats, be that their points, rank or num_friends and in turn awards them a badge similar to that used in SO. The system is set so when a task (vote up, comment, etc.) is completed by a user, their user_id is recorded and queued to be evaluated at 5 min intervals by a cron job. Is this the most efficient way of doing this? I saw the other post about badges but am not sure if this is the method that was selected as the preferred way.

Another question I have is about actually checking these variables (points, rank, num_friends) against the user. I have a number of badges and want to allow admins to add their own where they select a variable, the operator (==, >=, etc.) and the value it's set to. How do I evaluate that in an IF conditional statement? I tried using SWITCH for the operators but can't get the variables to be evaluated correctly, like:

function checkbadges($userid,$points,$rank,$friends){
    global $database;

    $q = $database->db_query("SELECT * FROM badges");
    while($bq = $database->db_fetch_assoc($q)) {
        switch($bq[badge_sign]) {
          case "1":
              if($bq[badge_var] == $bq[badge_value])
                  givebadge($userid, $bq[badge_id]);
            break;
          case "2":
              if($bq[badge_var] >= $bq[badge_value]))
                  givebadge($userid, $bq[badge_id]);
            break;
          case "3":
              if($bq[badge_var] <= $bq[badge_value]))
                  givebadge($userid, $bq[badge_id]);
            break;
          case "4":
              if($bq[badge_var] != $bq[badge_value]))
                  givebadge($userid, $bq[badge_id]);
            break;
        }
    }
}

I think I have to use eval() but I'm not sure how, any help would be appreciated!

Was it helpful?

Solution

If $bq['badge_var'] is a string which is a valid variable name in the current scope, you can just do this:

$value = ${ $bq['badge_var'] };

It might be safer and easier to extend if you pass an array to checkbadges() instead:

function checkbadges($userid,$userdata){
    global $database;
    $q = $database->db_query("SELECT * FROM badges");
    while($bq = $database->db_fetch_assoc($q)) {
        $currentValue = $userdata[ $db['badge_var'] ];
        $requiredValue = $db['badge_value'];
        $issueBadge = false;
        switch($bq['badge_sign']) {
          case "1":
              $issueBadge = ( $currentValue == $requiredValue );  
          break;
          case "2":
              $issueBadge = ( $currentValue >= $requiredValue );  
            break;
          case "3":
              $issueBadge = ( $currentValue <= $requiredValue );  
            break;
          case "4":
              $issueBadge = ( $currentValue != $requiredValue );  
          break;
        }

        if ($issueBadge) {
            givebadge($userid, $bq['badge_id']);
        }
    }
}

where $userdata is something like this:

array( 'points' => 1 , 'friends' => 10, 'rank' => 10 );

You can then extend the available variables without modifying the function

OTHER TIPS

Which is the expected value and the observed value of the variable you are not getting the right value for?

Additionally, its better practice to use:

$arr['key']

instead of

$arr[key]

as PHP will evaluate it first as a constant which is bad practice, see PHP Reference: Arrays

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top