function user_badges_ensure_old_roles_badges_removed in User Badges 7.4
A function to ensure that we remove automatically assigned badges.
Parameters
array $roles An array of the roles and their bid (if any).:
int $blocked The bid of the blocked user badge (if one).:
Return value
null
2 calls to user_badges_ensure_old_roles_badges_removed()
- user_badges_cron in ./
user_badges.module - Implements hook_cron().
- user_badges_roles_form_add_badges_submit in includes/
user_badges.admin.inc - Submit for add badges to corresponding users.
File
- ./
user_badges.module, line 1152 - Hooks and other stuff related to user badge.
Code
function user_badges_ensure_old_roles_badges_removed($roles, $blocked = FALSE) {
// First we grab all user badges that are assigned automatically.
$query = db_select('user_badges_assignment', 'uba');
$badges = $query
->fields('uba', array(
'bid',
))
->condition('uba.type', 2, '=')
->groupBy('uba.bid')
->execute()
->fetchAll();
$remove_badges = array();
// Go through each badge, each that are not assigned to a role, we remove.
foreach ($badges as $badge) {
if ($badge->bid != $blocked && !in_array($badge->bid, $roles)) {
$remove_badges[] = $badge->bid;
}
}
// TODO: Automatic removal of badges from users whose role was taken away.
// This can either be done here, or in a hook_user_update (?) function.
// If we have some badges, then we will remove them. We don't want to do this
// straight away, because it might go through MANY users who have badges
// assigned.
if (!empty($remove_badges)) {
$query = db_select('user_badges_assignment', 'uba');
$badges = $query
->fields('uba', array(
'bid',
'uid',
))
->condition('uba.type', 2, '=')
->condition('uba.bid', $remove_badges, 'IN')
->execute()
->fetchAll();
foreach ($badges as $badge) {
user_badges_user_remove_badge($badge->uid, $badge->bid, NULL, 0);
}
}
}