Action for uc_roles

Contrib type: 
Code/CSS Snippet
Status: 
Bug testing
Moderation: 
Awaiting moderation

Downloads

Latest version: 
RC1
Compatibility: 
Ubercart 2.x

This file creates a configurable Views Bulk Operations action to apply role expirations to users within the uc_roles system.

Supported operations:

  • Emulates the "Add role" part of the "Ubercart roles" fieldset on the user edit form, allowing you to choose one role from a predefined set of roles and add that role with expiration period to the selected users.

Requirements:

  • Ubercart / UC Roles
  • Views Bulk Operations

Usage:

  1. Install and configure uc_roles, creating the role feature as described in the ubercart documentation.
  2. Install and enable views_bulk_operations (http://drupal.org/project/views_bulk_operations).
  3. If there is no "User" type view for Views Bulk Operations, create one. Currently, Views Bulk Operations does not include a "User" type view by default.
  4. Download / unzip this file into the views_bulk_operations directory.
  5. Edit the view to enable "Set role expirations" as an allowed action.
  6. Visit the view and perform this action on the selected users, setting options on the subsequent pages.

PreviewAttachmentSize
uc_roles.action.inc_.tar.gz1.39 KB
Splicer's picture
Offline
Joined: 09/22/2009
Juice: 26
No joy here

On my sandbox installation (d16, uc_roles 6.x-2.2, Views 6.x-2.8, VBO 6.x-1.9) nothing happens when I attempt to change role expiration. I get the message:

Performed Set role expirations on user username

but the role is not added, nor is an expiration set if the role was already there.

I've tried it in bulk and one at a time. I've looked through the code and I'm afraid I don't see what might be wrong.

Let me know what I might do to help troubleshoot this.

Splicer's picture
Offline
Joined: 09/22/2009
Juice: 26
Re: Action for uc_roles

The only thing I see so far is the line (146):

  uc_roles_user('update', $context, $object, 'account');

but looking at http://api.ubercart.org/api/function/uc_roles_user/2 I see that the first argument to uc_roles_user() doesn't have any action associated with "update".

I'm thinking that perhaps it should be

  uc_roles_user('submit', $context, $object, 'account');

instead?

Splicer's picture
Offline
Joined: 09/22/2009
Juice: 26
That seems to have almost done it

The change from 'update' to 'submit' successfully forced the role/expiration addition! So that's progress. The downside is that for every user I've applied updated, every other role they had were removed entirely.

That's good enough for my purposes right now, but anyone looking for this functionality should be warned that simply changing 'update' to 'submit' on line 146 may have serious consequences.

kentr's picture
Offline
Joined: 07/18/2009
Juice: 27
Re: That seems to have almost done it

Without looking at the code, I'd hazard a guess that the parameters need to include the current status for any existing roles. 'Submit' sounds like it's recording everything "anew", and if the incoming parameter is missing the other roles, they're not being saved.

Sorry it didn't work out of the box. I'm confused as to why...

kentr's picture
Offline
Joined: 07/18/2009
Juice: 27
Re: That seems to have almost done it

Hmm, looking at uc_roles_user_submit(), this may be where the existing roles are being deleted:

<?php
 
// If a user's role is removed using Drupal, then so is any expiration data.
 
if (is_array($edit['roles'])) {
    foreach (
$account->roles as $rid => $role) {
      if (!
in_array($rid, array_keys($edit['roles'])) && $rid != DRUPAL_AUTHENTICATED_RID) {
       
uc_roles_delete($account, $rid);
      }
    }
  }
?>

The next user might try adding the existing roles to $edit['roles']

joachim@drupal.org's picture
Offline
Joined: 10/16/2008
Juice: 31
Re: Action for uc_roles

The original call to uc_roles_user_submit() with 'update' won't work simply because uc_roles_user_submit() won't act on that op (maybe that was removed in newer version of UC core?). So nothing happens.

Changing that to 'submit' runs the submit operation, which is really not a good idea as it's actually hook_user's submit handler for the user form. So it's doing all sorts of other stuff, such as checking whether you've made any roles permanent and so on. As kentr points out, to make this work you need to fake up what it's expecting, as if you were coming in from the user form. This is not a good approach.

Instead, use the API -- that is what it's there for. There is a function uc_roles_grant() which even does the user saving for you!

Working code on the way Smiling

kentr's picture
Offline
Joined: 07/18/2009
Juice: 27
Re: Re: Action for uc_roles

It worked at one point on my local box...

Thanks for pointing out uc_roles_grant(). Didn't know that function existed.

joachim@drupal.org's picture
Offline
Joined: 10/16/2008
Juice: 31
Re: Action for uc_roles

Fixed version of the code.

This really belongs as a patch to the UC roles module IMO.

I've also:

- removed $polarity_widget form element as it's not used
- removed the 'Add role' -- as far as I can see this is just a kludge to get uc_roles_user_submit() to act.
- changed the form to allow changing / assigning more than one role at a time
- added help text and an explanatory note in the event that there are no available roles.

AttachmentSize
uc_roles.action.inc_.zip 2 KB