You are here

function user_badges_user in User Badges 5

Same name and namespace in other branches
  1. 6.2 user_badges.module \user_badges_user()
  2. 6 user_badges.module \user_badges_user()

Implementation of hook_user() This handles assignment of badges based on role. When role is assigned or removed, appropriate badges are added or removed.

File

./user_badges.module, line 139
@brief User Badges module file

Code

function user_badges_user($op, &$edit, &$user, $category = 'account') {
  switch ($op) {
    case 'load':
      if ($user->uid > 0) {
        $user->badges = user_badges_get_badges($user->uid);
      }
      break;
    case 'insert':
      if (is_array($user->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($user->roles);
        foreach ($rids as $rid) {

          // if this role has a badge
          if (key_exists($rid, $roles)) {

            // and user doesn't already have this badge
            if (!key_exists($roles[$rid], $user->badges)) {
              $success = user_badges_user_add_badge($user->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'];
        $new_roles[2] = 2;

        // 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) $user->roles));
        foreach ($added as $rid) {

          // if this role has a badge
          if (key_exists($rid, $roles) && !key_exists($roles[$rid], $user->badges)) {
            $success = user_badges_user_add_badge($user->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) $user->roles), array_keys($new_roles));
        foreach ($removed as $rid) {

          // If this role has a badge and user has this badge..
          if (key_exists($rid, $roles) && key_exists($roles[$rid], $user->badges)) {
            $success = user_badges_user_remove_badge($user->uid, $roles[$rid], 'role');
            drupal_set_message(t('%name badge removed from user.', array(
              '%name' => $badges[$roles[$rid]],
            )));
          }
        }
      }
      break;
    case 'delete':
      db_query('DELETE FROM {user_badges_user} WHERE uid = %d', $user->uid);
      break;
    case 'view':
      foreach ($user->badges as $badge) {
        $badgeimgs[] = theme('user_badge', $badge);
      }
      if ($badgeimgs) {
        $badge_group[] = array(
          'title' => '',
          'value' => theme('user_badge_group', $badgeimgs),
          'class' => '',
        );
        return array(
          t('Badges') => $badge_group,
        );
      }
  }
}