I would write a stored procedure, since all the information needed resides in the database so this is the most efficient place to access that data.
A tyical rule could be implemented via a single INSERT statement along these lines:
IF eligible for THEN
INSERT INTO user_badges
SELECT
WHERE NOT EXISTS (SELECT NULL FROM user_badges
WHERE badge = );
END IF;
(I simplify somewhat!)