function user_badges_user in User Badges 6.2
Same name and namespace in other branches
- 5 user_badges.module \user_badges_user()
- 6 user_badges.module \user_badges_user()
Implements hook_user().
This handles assignment of badges based on role. When a role is assigned or removed, appropriate badges are added or removed.
4 string references to 'user_badges_user'
- user_badges_update_6001 in ./
user_badges.install - Implements hook_update_N() new DB structure
- user_badges_update_6101 in ./
user_badges.install - user_badges_update_6102 in ./
user_badges.install - user_badges_views_data in ./
user_badges.views.inc - Implements hook_views_data().
File
- ./
user_badges.module, line 189 - @brief User Badges module file
Code
function user_badges_user($op, &$edit, &$account, $category = 'account') {
global $user;
static $badges = array();
switch ($op) {
case 'load':
// Have we loaded this user before?
if (isset($badges[$account->uid])) {
$account->badges = $badges[$account->uid];
break;
}
$account->badges = array();
if ($account->uid > 0) {
// Get all user badges for this user, regardless of whether we filter the ones we show.
$account->badges_all = user_badges_get_badges($account->uid, array(
'nolimit' => TRUE,
));
// Now make the array of badges we will show.
$account->badges = $account->badges_all;
if ($limit = variable_get('user_badges_showone', 0)) {
foreach ($account->badges_all as $bid => $badge) {
$badge->class = 'badge ' . _user_badges_class($badge);
// Display the badge if there's no limit or if the badge is unhideable or if we are within our limit.
if ($limit > 0 || $badge->unhideable == 1) {
$account->badges[$bid] = $badge;
// Count down our limit, unless the badge doesn't count towards it.
if (!$badge->doesnotcounttolimit) {
$limit--;
}
}
}
}
}
$badges[$account->uid] = $account->badges;
break;
case 'insert':
if (is_array($account->roles)) {
// Get the list of role badges.
$roles = user_badges_get_roles();
$badges = user_badges_get_badges('select');
$message = user_access('manage badges');
$rids = array_keys($account->roles);
foreach ($rids as $rid) {
// If this role has a badge...
if (array_key_exists($rid, $roles)) {
// and user doesn't already have this badge.
if (!array_key_exists($roles[$rid], $account->badges)) {
$success = user_badges_user_add_badge($account->uid, $roles[$rid], 'role');
if ($success && $message) {
drupal_set_message(t('User assigned %name badge.', array(
'%name' => $badges[$roles[$rid]],
)));
}
}
}
}
}
break;
case 'update':
if (is_array($edit['roles'])) {
// Badges only get assigned or removed when a user's role assignments are changed.
// Add authenticated users (code below only cares about array keys) to prevent badge deletion
$new_roles = $edit['roles'];
// Get the list of role badges.
$roles = user_badges_get_roles();
$badges = user_badges_get_badges('select');
$message = user_access('manage badges');
// What are the added roles?
$added = array_diff(array_keys($new_roles), array_keys((array) $account->roles));
foreach ($added as $rid) {
// if this role has a badge
if (array_key_exists($rid, $roles) && !array_key_exists($roles[$rid], $account->badges_all)) {
$success = user_badges_user_add_badge($account->uid, $roles[$rid], 'role');
if ($success && $message) {
drupal_set_message(t('User assigned %name badge.', array(
'%name' => $badges[$roles[$rid]],
)));
}
}
}
// What are the removed roles?
$removed = array_diff(array_keys((array) $account->roles), array_keys($new_roles));
foreach ($removed as $rid) {
// If this role has a badge and user has this badge..
if (array_key_exists($rid, $roles) && array_key_exists($roles[$rid], $account->badges_all)) {
$success = user_badges_user_remove_badge($account->uid, $roles[$rid], 'role');
drupal_set_message(t('%name badge removed from user.', array(
'%name' => $badges[$roles[$rid]],
)));
}
}
//As we may have altered the badges, we need to refresh them in the $account object
$account->badges = user_badges_get_badges($account->uid);
$account->badges_all = user_badges_get_badges($account->uid, array(
'nolimit' => TRUE,
));
}
break;
case 'delete':
db_query('DELETE FROM {user_badges_user} WHERE uid = %d', $account->uid);
break;
case 'view':
if (is_array($account->badges) && count($account->badges)) {
$badgeimgs = array();
foreach ($account->badges as $badge) {
$badgeimgs[] = theme('user_badge', $badge, $account);
}
$account->content['user_badges'] = array(
'#type' => 'user_profile_category',
'#title' => t('Badges'),
'#weight' => 10,
'#attributes' => array(
'class' => 'user-badges',
),
);
$account->content['user_badges']['badges'] = array(
'#type' => 'user_profile_item',
'#value' => theme('user_badge_group', $badgeimgs),
'#attributes' => array(
'class' => 'badges',
),
);
}
}
}