You are here

function user_badges_add_badge_action in User Badges 7.4

Same name and namespace in other branches
  1. 6.2 user_badges.module \user_badges_add_badge_action()
  2. 6 user_badges.module \user_badges_add_badge_action()
  3. 7 user_badges.actions.inc \user_badges_add_badge_action()
  4. 7.2 user_badges.actions.inc \user_badges_add_badge_action()
  5. 7.3 user_badges.actions.inc \user_badges_add_badge_action()

Implements a Drupal action.

File

./user_badges.actions.inc, line 30
Actions for User Badges module.

Code

function user_badges_add_badge_action(&$account, $context = array()) {
  if (isset($account->uid)) {
    $uid = $account->uid;
  }
  elseif (isset($context['uid'])) {
    $uid = $context['uid'];
  }
  else {
    global $user;
    $uid = $user->uid;
  }
  $success = TRUE;
  $badges = user_badges_get_badges('all');
  foreach ($context['badges'] as $bid) {
    $success = (bool) user_badges_user_add_badge($uid, $bid, 'uid');
    if ($success) {
      watchdog('action', 'Added user badge %badge to user %name.', array(
        '%name' => check_plain($user->name),
        '%badge' => check_plain($badges[$bid]->name),
      ));
    }
    else {
      watchdog('action', 'Unable to add user badge %badge to user %name.', array(
        '%name' => check_plain($user->name),
        '%badge' => check_plain($badges[$bid]->name),
      ), WATCHDOG_WARNING);
    }
  }
}