When attempting to select and save role badges (admin/config/people/user_badges/roles) a series of errors are generated:

Warning: Missing argument 1 for SelectQuery::condition(), called in ...\user_badges\user_badges.module on line 1166 and defined in SelectQuery->condition() (line 1006 of ...\htdocs\includes\database\select.inc).

Notice: Undefined variable: field in SelectQuery->condition() (line 1007 of ...\htdocs\includes\database\select.inc).

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IS NULL )' at line 2: SELECT u.uid AS uid FROM {users} u LEFT OUTER JOIN {user_badges_user} ubu ON ubu.uid=u.uid AND ubu.type='role' WHERE (u.status = :db_condition_placeholder_0) AND (ubu.bid = :db_condition_placeholder_1) AND (ubu.uid IS NULL ) AND ( IS NULL ) ; Array ( [:db_condition_placeholder_0] => 0 [:db_condition_placeholder_1] => 2 ) in user_badges_save_roles() (line 1169 of ...\user_badges\user_badges.module).

If you remove all badges from the roles and save, all goes well.

If you have no badges selected, you can select one badge and it will save, but will give the simple error, "There was a problem saving roles to the database." After that you get the above errors.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Killpill’s picture

Priority: Normal » Minor

Wow... I was debugging this thing and writing something as I went then totally Google searched with this tab and lost my text.

I have the same problem.

The "correct" code, from looking at the 6.x module, should be:

if ($rid > 2) {
          $query = db_select('users_roles', 'u')
          ->fields('u', array('uid'))
	  ->condition('u.rid', $rid)
          ->isNull('ubu.uid')
          ;
        }

I think, at least.

After that I get this error:

PDOException: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1: INSERT INTO {user_badges_user} (uid, bid, type) VALUES (:db_insert_placeholder_0_0, :db_insert_placeholder_0_1, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_1] => 1 [:db_insert_placeholder_2] => role [:db_insert_placeholder_0_0] => 1 [:db_insert_placeholder_0_1] => 2 ) in user_badges_save_roles() (line 1214 of .../user_badges/user_badges.module).

No idea on that one but now I'm almost late for something. Hopefully this will help someone... but I am new to all this.

Edit:

Just got back. I'm going to start from the old module function and rewrite the queries to work with D7. I hope that will do the trick. Just trying to understand this, as I need it to work.

Edit2:

Seems to work for me! I'll submit a patch after I figure out how to.

Killpill’s picture

Priority: Minor » Normal
Status: Active » Needs review
Issue tags: +D7 port patch
FileSize
46.85 KB

I don't really know how the patch file system works.

This is what I changed:

/**
 * Save information about roles for user_badges (in settings)
 *
 * @param $roles
 *   An array in the format rid => bid for each role/badge relationship.
 */
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("
            INSERT INTO {user_badges_user} (uid, bid, type)
            SELECT u.uid, :bid, 'role'
            FROM {users} u
            LEFT JOIN {user_badges_user} ubu
            ON ubu.uid=u.uid AND ubu.bid=:bid AND ubu.type='role'
            WHERE status = 0 AND ubu.uid IS NULL
          ", array(':bid' => $bid));
        }
        // The authenticated user role (represented as rid 2) has no entry in the users_role table, so it needs its own special query
        elseif ($rid == 2) {
          $success = $success && db_query("
            INSERT INTO {user_badges_user} (uid, bid, type)
            SELECT u.uid, :bid, 'role'
            FROM {users} u
            LEFT JOIN {user_badges_user} ubu
            ON ubu.uid=u.uid AND ubu.bid=:bid AND ubu.type='role'
            WHERE u.uid > 0 AND ubu.uid IS NULL
          ", array(':bid' => $bid));
        }
        //For all the normal roles, we want to run this query
        else {
          $success = $success && db_query("
            INSERT INTO {user_badges_user} (uid, bid, type)
            SELECT ur.uid, :bid, 'role'
            FROM {users_roles} ur
            LEFT JOIN {user_badges_user} ubu
            ON ubu.uid=ur.uid AND ubu.bid=:bid AND ubu.type='role'
            WHERE ur.rid=:rid AND ubu.uid IS NULL
          ", 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'));
    }
  }
}

I just took the function from 6.x, replaced the 7.x function with it, then updated all the queries to 7.x syntax. It seems to work for me but I must stress that I am new to this.

Delty’s picture

Thanks for doing that Killpill. I'm going to test it here in a minute.

WRT creating patch files, you need to use the diff utility. It's very simple and is built into most Linux/UNIX operating systems. On Windows, you can install Cygwin to get a unix shell that has the patch and diff utilities. Once you have access to it, the diff command is:

diff -u original.c new.c > original.patch

That's all there is to it. I've attached the patch.

UPDATE: My initial testing looks good - I was able to save and remove badges and the role badges were assigned properly when roles were added or removed. Thanks!

Delty’s picture

I'm getting a couple of badge related errors in the log now if you want to take a look. It might not have anything to do with your changes though, because now I have a badge set for blocked (new) users where before I was unable to set that badge successfuly for testing.

Notice: Undefined property: stdClass::$badges_all in user_badges_user_presave() (line 343 of /var/www/clients/client3/web2/web/sites/default/modules/user_badges/user_badges.module).

and

Warning: key_exists() expects parameter 2 to be array, null given in user_badges_user_presave() (line 343 of /var/www/clients/client3/web2/web/sites/default/modules/user_badges/user_badges.module).

Killpill’s picture

My badge image file is also being deleted from the folder but the badge remains. Other folders, such as user title images and user pictures, are not cleared so this is unique to badges.

I'm checking in on user_badges_user_presave now.

Edit:

I don't get that notice message when editing a user profile and blocking someone, but the function is called and I had it print the $account variable - which had badges_all in it. Where do these notices occur for you, page wise?

Edit2:

Found the warning and notice in my log from when a user registered.

Delty’s picture

As you noticed, it happens when a new user registers, so it might be trying to set the badge before the account is available (i.e. on the Anonymous account) so my guess is a simple check to see if it's a new user on the anonymous account would fix it.

Thanks!

Killpill’s picture

I created a separate issue for this here. I don't have any intention of fixing it at the moment unless it is causing problems.

imoreno’s picture

Hi,
unable to apply this patch, is it still in use?

imoreno’s picture

patch from #1 worked for me, i can now save the role based tags with no errors.

SebCorbin’s picture

Patch committed (I've rewritten comments according to Drupal Coding standards)

SebCorbin’s picture

Status: Needs review » Fixed

Automatically closed -- issue fixed for 2 weeks with no activity.