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!