You are here

function user_badges_save_roles in User Badges 7

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. 6 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.

2 calls to user_badges_save_roles()
user_badges_cron in ./user_badges.autoassignrole.inc
Implements hook_cron(). Enables User Badges to work along with Auto Assign Role. Notes: - this is a quite fast solution in order to solve Auto Assign Role issue.
user_badges_roles_form_submit in ./user_badges.admin.inc
Submission function for user_badges_roles_form.

File

./user_badges.module, line 1061
@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 (:rid, :bid)", array(
          ':rid' => $rid,
          ':bid' => $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, :bid, 'role'\n            FROM {users} u\n            LEFT JOIN {user_badges_user} ubu\n            ON ubu.uid=u.uid AND ubu.bid=:bid AND ubu.type='role'\n            WHERE status = 0 AND ubu.uid IS NULL\n          ", array(
            ':bid' => $bid,
          ));
        }
        elseif ($rid == 2) {
          $success = $success && db_query("\n            INSERT INTO {user_badges_user} (uid, bid, type)\n            SELECT u.uid, :bid, 'role'\n            FROM {users} u\n            LEFT JOIN {user_badges_user} ubu\n            ON ubu.uid=u.uid AND ubu.bid=:bid AND ubu.type='role'\n            WHERE u.uid > 0 AND ubu.uid IS NULL\n          ", array(
            ':bid' => $bid,
          ));
        }
        else {
          $success = $success && db_query("\n            INSERT INTO {user_badges_user} (uid, bid, type)\n            SELECT ur.uid, :bid, 'role'\n            FROM {users_roles} ur\n            LEFT JOIN {user_badges_user} ubu\n            ON ubu.uid=ur.uid AND ubu.bid=:bid AND ubu.type='role'\n            WHERE ur.rid=:rid AND ubu.uid IS NULL\n          ", array(
            ':bid' => $bid,
            ':rid' => $rid,
          ));
        }
      }
    }
    if ($success) {
      drupal_set_message(t('Roles saved.'));
    }
    else {
      drupal_set_message(t('There was a problem saving roles to the database'));
    }
  }
}