CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Find out group membership
Pages: [1]

Author Topic: Find out group membership  (Read 692 times)

sussdorff

  • I post occasionally
  • **
  • Posts: 36
  • Karma: 4
  • CiviCRM version: 3.4
  • CMS version: Drupal 6
Find out group membership
August 17, 2011, 05:22:32 am
I thought I can get the group membership of a person in a group like this:

$results=civicrm_api("GroupContact","getsingle", array ('version' =>'3' , 'contact_id' => $contact_id , 'group_id' => '41'));

Sadly that does not work. I can supply either group_id OR contact_id.

Is this on purpose or should I file a ticket?

My solution (and sorry for different variable names :-)):

   $trainer=civicrm_api("GroupContact","get", array ('version' =>'3' , 'contact_id' => $userID ));
   $trainer_p = 0;
   foreach($trainer['values'] as $group) {
      if ($group['group_id'] == '31') {
         $trainer_p = 1;
      }
   }

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Find out group membership
August 17, 2011, 06:52:12 am
Hi,


Could you patch api/v3/GroupContact.php to add that filter if group_id exists (line 70 or so)?

btw array_key_exists is much faster than a foreach you should use it.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

sussdorff

  • I post occasionally
  • **
  • Posts: 36
  • Karma: 4
  • CiviCRM version: 3.4
  • CMS version: Drupal 6
Re: Find out group membership
August 17, 2011, 08:49:23 am
I thought about using array_key_exists but the keys in the values are actually the IDs of the relationship between contact and group. What I need though is to find out if within the array exists one array whose key group_id has a certain value. But maybe my brain isn't used to PHP arrays yet fully....

This being said, I will patch it and post it in Jira? Patch would be against 3.4.5 tough (hope that's not an issue)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Find out group membership
August 17, 2011, 09:56:24 am
My bad, I misread your code. Probably not worthwhile improving over the foreach indeed.

(the better solution would be to modify the BAO functions so they can handle the group+contact filter, but not the case, and given that unlikely you have tons of group per user, not a big deal).

Could you patch the api and add this feature?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Find out group membership

This forum was archived on 2017-11-26.