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 »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Getting the Group-Contact status of a contact efficiently
Pages: [1]

Author Topic: Getting the Group-Contact status of a contact efficiently  (Read 1046 times)

torenware

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 4
Getting the Group-Contact status of a contact efficiently
July 07, 2010, 06:48:08 pm
I was looking thought the v2 API, and can't find a simple way to get the following information without loading a lot of stuff I don't need.

The code here shows you what I want to do.  This works fine, but is so low level that it will likely break in a future release:

Code: [Select]
<?php
function da_core_get_group_status($contact_id, $group_id) {
  
//AFAICT, there isn't a way to do this efficiently in either the
  //high level or low level API.  So we do the following hack:
  
civicrm_initialize();
  
$sql = "
  select gc.contact_id, gc.group_id, gc.status, sh.date as last_changed
  from civicrm_group_contact gc
  left join civicrm_subscription_history sh
    on gc.group_id = sh.group_id and
       gc.contact_id = sh.contact_id and
       gc.status = sh.status
  where gc.contact_id = %1 and
        gc.group_id = %2
  order by sh.date desc limit 1;
  "
;

  
$args =
    array(
      
1 => array($contact_id, 'Integer'),
      
2 => array($group_id, 'Integer')
    );

  
$dao =& CRM_Core_DAO::executeQuery( $sql, $args );
  
$rslt = array();
  if (
$dao->fetch()) {
    
$rslt['contact_id'] = $dao->contact_id;
    
$rslt['group_id'] = $dao->group_id;
    
$rslt['status'] = $dao->status;
    
$rslt['last_changed'] = $dao->last_changed;
  }
  
$dao->free();
  return 
$rslt;

}
?>


You can get all the groups someone is added to, and you can get all the groups for a contact of a particular status, or you can get all of the groups with no status info.  But I couldn't find a high level way to do exactly what I'm doing here: get the status and date info for a single contact_id/group_id pair.

Is there a high level way to do this, or did I really need to roll my own here?

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Getting the Group-Contact status of a contact efficiently
July 07, 2010, 09:53:41 pm

the set of functions we have for this is at:

CRM/Contact/BAO/GroupContact.php

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

torenware

  • I post frequently
  • ***
  • Posts: 153
  • Karma: 4
Re: Getting the Group-Contact status of a contact efficiently
July 09, 2010, 02:43:27 pm
Yep.  And it turns out, it's real hard to do what I'm trying to do with any of them.  Hence the post.  In more detail:
Quote
You can get all the groups someone is added to,
civicrm_group_contact_get()  will retrieve a list of all the groups the contact is Added on.  This is hard coded as late as 3.1 (around line 64:
Code: [Select]
<?php
    $status 
= CRM_Utils_Array::value( 'status', $params, 'Added' );
    require_once 
'CRM/Contact/BAO/GroupContact.php';
    
$values =& CRM_Contact_BAO_GroupContact::getContactGroup( $params['contact_id'], $status, null, false, true );
?>

Here, it would be better if $params could include a 'status' argument.
Quote
and you can get all the groups for a contact of a particular status,
using CRM_Contact_BAO_GroupContact::getContactGroup().  Which will give you all contacts for a single status, or
Quote
or you can get all of the groups with no status info.
if you specify status is NULL, you will get the full list of group-contact records, but the results do not return the status field at all.  So you get a list of groups to which a contact is Added, Pending, or Removed.  And you can't tell which is which   :'(
Quote
  But I couldn't find a high level way to do exactly what I'm doing here: get the status and date info for a single contact_id/group_id pair.

Using the HL API, you can't do this at all.  Using the OO API, you have to call CRM_Contact_BAO_GroupContact::getContactGroup() twice, and merge the results.

This is pretty heavy weight.  IMNSHO, we should change the behavior of one or more of these higher level APIs to fix this.

I'd be willing to make the HL API actually use the 'status' argument, which I suspect is the best way to solve this problem in the HL framework.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Getting the Group-Contact status of a contact efficiently
July 10, 2010, 06:49:06 am
Code: [Select]
[quote author=torenware link=topic=14524.msg62075#msg62075 date=1278711807]
    $status = CRM_Utils_Array::value( 'status', $params, 'Added' );
?>
Here, it would be better if $params could include a 'status' argument.
[/quote]

Isn't it exactly what this line does ?

This being said, it would make sense indeed to add the status as a param if it isn't the case. Could you:

1) document it in the wiki http://wiki.civicrm.org/confluence/display/CRMUPCOMING/Group+APIs#GroupAPIs-GroupContactAPIs
2) add a test case tests/phpunit/api/v2/GroupContactTest.php

?

-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 »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Getting the Group-Contact status of a contact efficiently

This forum was archived on 2017-11-26.