You are here

function user_badges_save_roles in User Badges 5

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

Save information about roles for user_badges (in settings)

1 call to user_badges_save_roles()
user_badges_roles_form_submit in ./user_badges.module

File

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

Code

function user_badges_save_roles($roles) {
  if (is_array($roles)) {
    $success = TRUE;
    db_query('DELETE FROM {user_badges_roles}');
    db_query("DELETE FROM {user_badges_user} WHERE type='role'");
    foreach ($roles as $rid => $bid) {
      if ($bid) {
        $success = $success && db_query('INSERT INTO {user_badges_roles} (rid, bid) VALUES (%d, %d)', $rid, $bid);

        // Blocked user (represented as `rid 0«) has no entry in the users_role table
        if ($rid == 0) {
          $success = $success && db_query("\n            INSERT INTO {user_badges_user} (uid, bid, type) \n            SELECT uid, %d, 'role' FROM {users} WHERE status = 0", $bid);
        }
        elseif ($rid == 2) {
          $success = $success && db_query("\n            INSERT INTO {user_badges_user} (uid, bid, type) \n            SELECT uid, %d, 'role' FROM {users} WHERE uid > 0", $bid);
        }
        else {
          $success = $success && db_query("\n            INSERT INTO {user_badges_user} (uid, bid, type) \n            SELECT uid, %d, 'role' FROM {users_roles} WHERE rid=%d", $bid, $rid);
        }
      }
    }
    if ($success) {
      drupal_set_message(t('Roles saved.'));
    }
    else {
      drupal_set_message(t('There was a problem saving roles to the database'));
    }
  }
}