You are here

function user_badges_save_roles in User Badges 6

Same name and namespace in other branches
  1. 5 user_badges.module \user_badges_save_roles()
  2. 6.2 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)

Parameters

$roles: An array in the format rid => bid for each role/badge relationship.

1 call to user_badges_save_roles()
user_badges_roles_form_submit in ./user_badges.admin.inc
submission function for user_badges_roles_form

File

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

Code

function user_badges_save_roles($roles) {
  if (is_array($roles)) {

    // We have to clear out all badges first.
    $success = TRUE;
    db_query('DELETE FROM {user_badges_roles}');
    db_query("DELETE FROM {user_badges_user} WHERE type='role'");

    // Now we loop through the roles and their badges, and assign them to
    // each user accordingly.
    foreach ($roles as $rid => $bid) {
      if ($bid) {

        // First of all, insert all the role and badge relationship
        // into user_badges_roles.
        $success = $success && db_query('INSERT INTO {user_badges_roles} (rid, bid) VALUES (%d, %d)', $rid, $bid);

        // For all of these queries, we LEFT JOIN user_badges_user to check
        // whether there are existing entries for that badge for that user
        // of the "role" type. Otherwise, we get database errors when
        // multiple roles assign the same badge
        // The blocked user "role" (represented as rid 0) has no entry in
        // the users_role table, so it needs its own special query.
        if ($rid == 0) {
          $success = $success && db_query("\n            INSERT INTO {user_badges_user} (uid, bid, type)\n            SELECT u.uid, %d, 'role'\n            FROM {users} u\n            LEFT JOIN {user_badges_user} ubu\n            ON ubu.uid=u.uid AND ubu.bid=%d AND ubu.type='role'\n            WHERE status = 0 AND ubu.uid IS NULL\n          ", $bid, $bid);
        }
        elseif ($rid == 2) {
          $success = $success && db_query("\n            INSERT INTO {user_badges_user} (uid, bid, type)\n            SELECT u.uid, %d, 'role'\n            FROM {users} u\n            LEFT JOIN {user_badges_user} ubu\n            ON ubu.uid=u.uid AND ubu.bid=%d AND ubu.type='role'\n            WHERE u.uid > 0 AND ubu.uid IS NULL\n          ", $bid, $bid);
        }
        else {
          $success = $success && db_query("\n            INSERT INTO {user_badges_user} (uid, bid, type)\n            SELECT ur.uid, %d, 'role'\n            FROM {users_roles} ur\n            LEFT JOIN {user_badges_user} ubu\n            ON ubu.uid=ur.uid AND ubu.bid=%d AND ubu.type='role'\n            WHERE ur.rid=%d AND ubu.uid IS NULL\n          ", $bid, $bid, $rid);
        }
      }
    }
    if ($success) {
      drupal_set_message(t('Roles saved.'));
    }
    else {
      drupal_set_message(t('There was a problem saving roles to the database'));
    }
  }
}