Hi all,

proceeding in my development as described here http://drupal.org/node/1818378, I'm trying now to develop the following feature:

If a user already belongs to a group, he cannot join to other groups.

So, to check the first part I can reuse a piece of code from http://drupal.org/node/1818378:

// Check if og_membership table already contains the user id:
$query = db_query('SELECT id FROM {og_membership} WHERE etid = :uid AND  entity_type = :entity_type LIMIT 1', array(':uid' => _helper_get_current_user_id(), ':entity_type' => 'user'));
if($query->rowCount() > 0) {
   // Yes, the user already belongs to a group, so deny group subscription:
   ***WHAT TO DO NOW?***
}

The problem is that I don't know where to put the code above (some OG hook ? a system hook? where?).

The final result should be an "access denied" when the user clicks on "Request membership" to join to a group.

Any suggestion?

Please help me!

Thank you very much

Files: 
CommentFileSizeAuthor
#15 If a user cannot anymore create groups - node-add page should not be accessible.jpg147.62 KBMXT
#14 subscribe.png29.01 KBAmitaibu
#14 1818390-og-subscribe-14.patch1.62 KBAmitaibu
PASSED: [[SimpleTest]]: [MySQL] 745 pass(es).
[ View ]
#11 1818390-og-ui-subscribe-11.patch2.13 KBAmitaibu
PASSED: [[SimpleTest]]: [MySQL] 745 pass(es).
[ View ]

Comments

I don't know: is there a way to hook the function og_ui_subscribe ?

In this way I could add my validation... and write a feedback message to the user similar to "You are already subscribed to a group, so you actually can't join this group, sorry"...

Do you think I have to use hook_og_membership_presave ? Please tell me if it's the right way!

I wrote the following code:

function helper_og_membership_presave(OgMembership $og_membership) {
  $og_array = og_get_entity_groups();
  if(!empty($og_array)) {
    // Yes, the user already belongs to a group, so deny group subscription:
    drupal_set_message('You already belong to a group');
    drupal_access_denied();
    drupal_exit();
  }
}

The code above seems to work, but please, tell me if this is a good stuff!

Did you get this to work? If so how? I am interested in doing this also.

The above code worked for me. How would we get the function to only act on specific roles though? If user has "role" then run the helper function?

Status:Active» Fixed

I don't think any special code is needed. If the og-audience field attached to the user is set to cardinality == 1; then OG won't let that user to subscribe to other groups. Remember that in 2.x you can have multiple OG-audience fields.

I'm still running 7.x-1.5 ... should I make the upgrade now? Should I fear it too? haha

Status:Fixed» Active

If the og-audience field attached to the user is set to cardinality == 1; then OG won't let that user to subscribe to other groups.

This is not true:
In /admin/config/people/accounts/fields I've tried to set cardinality == 1 to "Group membership " (og_user_group_ref) field with the following results:

  1. When a user not belonging to any group try to subscribe to an existing group (e.g. /group/node/78/subscribe/og_user_group_ref ) a "page not found error" is shown (this not happens when cardinality is setted to unlimited: a subscription input request is shown in this case)
  2. When a user try to create a group (e.g. /node/add/pizzagroup) the message "Can't save entity as group, because user XXX can't be subscribed to group and become a manager." (this not happens when cardinality is setted to unlimited: the user can create the group without any problems)

Any suggestion?

Thank you very much for any help

@MXT, please provide the exact link I should check based on the DB you provided.

For point 1:

  1. Login as administrator
  2. Go to home page
  3. Switch to user "Test1" using the provided "Switch user" block
  4. Try to subscribe to the existing group: "/group/node/1/subscribe/og_user_node"
  5. A "page not found error" appears and user can't subscribe to group (this not happens when cardinality /admin/config/people/accounts/fields/og_user_node is setted to unlimited: a subscription input request is shown in this case)

For point 2:

  1. Login as administrator
  2. Go to home page
  3. Switch to user "Test2" using the provided "Switch user" block
  4. Try to create a group: /node/add/sport-club
  5. Write something for "Title" and "Body" input fields
  6. Click on "save" to save the node group
  7. Result: the error "Can't save entity as group, because user Test2 can't be subscribed to group and become a manager." appears and node group is not saved (this not happens when cardinality /admin/config/people/accounts/fields/og_user_node is setted to unlimited: the user can create the group without any problems)

The expected result:

  • If cardinality for field "group membership" on the users general settings (/admin/config/people/accounts/fields/og_user_node) is setted to unlimited then the user can subscribe to infinite groups
  • If cardinality is setted to a defined X value then the user can subscribe to max X groups (e.g. cardinality = 1 then user can subscribe only to 1 group)

Thank you very much for working on this

> For point 2:

Point 2 works as expected. If the user can be subscribed only to 1 group, they are not allowed to create another one, as they will not be able to be subscribed to it.

I'm checking point 1.

Status:Active» Needs review
StatusFileSize
new2.13 KB
PASSED: [[SimpleTest]]: [MySQL] 745 pass(es).
[ View ]

> Point 1

User should now get "You cannot register to this group, as you have reached your maximum allowed subscriptions." message.

Ok:

POINT 1:
I can confirm that the provided patch resolves the issue. Thank you!

POINT 2:
you said:

If the user can be subscribed only to 1 group, they are not allowed to create another one, as they will not be able to be subscribed to it.

I completely agree with you, but user TEST2 was no subscribed to any group. So, it should be able to create at least ONE group.
I get a doubt now: how does the subscription mechanism work? I mean: if a user is a manager is ALSO a member so the cardinality is already reached? (1 subscription as manager + 1 subscription as member = 2 subscription?)

POINT 3NEW
I've noticed a minor issue: no "subscribe to this group" link is provided in any group page.

I've committed #11

StatusFileSize
new1.62 KB
PASSED: [[SimpleTest]]: [MySQL] 745 pass(es).
[ View ]
new29.01 KB

Patch to address point 2, thanks for the explanation.

point 3 -- seems to work for me.

subscribe.png

GREAT WORK Amitaibu, all works fine now!!! GREAT!!! ;)

I have a suggestion to improve usability (if this doesn't request so much work for you obviously):

If a user has reached his creation group limit, group creation page (node/add) and related creation link in menu shouldn't appear at all (see screenshot)

Thank you!!!

Status:Needs review» Fixed

Committed #14

> If a user has reached his creation group limit, group creation page (node/add) and related creation link in menu shouldn't appear at all (see screenshot)

Please open a separate issue for this.

Status:Fixed» Closed (fixed)

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