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) »
  • Activity API: get activities assigned to a given contact
Pages: [1]

Author Topic: Activity API: get activities assigned to a given contact  (Read 1732 times)

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Activity API: get activities assigned to a given contact
February 20, 2014, 08:30:40 am
Hi All,

I'm looking at the API code examples here: https://github.com/civicrm/civicrm-core/blob/master/api/v3/examples/Activity/GetTargetandAssignee.php , and it seems like I should be able to get all activities assigned to a given contact. However, using the API Explorer, I'm not getting the results I expect.

URL: /civicrm/ajax/rest?entity=Activity&action=get&debug=1&sequential=1&json=1&assignee_contact_id=231574
Code: [Select]
CRM.api('Activity', 'get', {'sequential': 1, 'assignee_contact_id': 231574},
  {success: function(data) {
      cj.each(data, function(key, value) {// do something });
    }
  }
);

The above parameters will simply return the first 25 activities, whereas I expected it to return only activities where contact 231574 is an assignee.

So I'm wondering:
1. Am I doing it wrong?
2. Should I be looking at other documentation?

Thanks for any tips you can offer!

- Allen
« Last Edit: February 20, 2014, 08:39:33 am by TwoMice »
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Activity API: get activities assigned to a given contact
February 20, 2014, 10:45:07 am
We recently had a similar issue in CiviHR's hrabsence extension -- except that we needed to find activities by target_contact_id. A little reading in Activity.get turned up this interesting conditional:

https://github.com/civicrm/civicrm-core/blob/4.4/api/v3/Activity.php#L225

So if you need to search by contact (regardless of their role in the activity), then "contact_id" might work. (However, the behavior of *other* fields may change when you using "contact_id" -- because it runs through a different search process.)

For hrabsence (and it sounds like your use-case, too), that's not good enough. So we added this to support "target_contact_id":

https://github.com/civicrm/civicrm-core/blob/4.4/CRM/Activity/BAO/Activity.php#L2605
https://github.com/civicrm/civicrm-core/blob/4.4/tests/phpunit/api/v3/ActivityTest.php#L552

So that's in 4.4 now. You might be able to adapt it to also support assignee_contact_id.

Epilogue: Eventually, we found that wasn't good powerful enough because our search required some additional joins/lookups/groupings based on HR-specific data. So we wrote a sibling API:

https://github.com/civicrm/civihr/blob/master/hrabsence/api/v3/Activity/Getabsences.php

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Activity API: get activities assigned to a given contact
February 20, 2014, 02:01:56 pm
Not sure if it's more than a matter of taste, but I tend to promote my specialised actions as their own custom entities. eg instead of api.activity.getabsences I would have gone for api.absence.get

The other benefit is that it can allow to rename fields to something more meaninful to the context (eg instead of "custom_42" have "galaxy")

eg (candidate being a contact type)

https://github.com/tttp/ep2014/blob/master/api/v3/Candidate.php
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Activity API: get activities assigned to a given contact
February 20, 2014, 02:48:53 pm
At the time, I thought "Activity.getAbsence" was better because I only wanted to customize the "get" action -- I wanted to keep the enitity-name and keep the implementations of "create" and "delete". (When using APIv3's Backbone bindings -- http://wiki.civicrm.org/confluence/display/CRMDOC/Backbone+Reference -- it's really handy if all the APIs have the same entity-name.)

But now that you mention it... and now that I look at how the requirements evolved... it would have been better to make a new entity-name. (It would require making dummy implementations of "create" and "delete" which delegate to the Activity API, but that's not a lot of code.) The big advantage of this would be access-control -- with a new entity, you get more freedom to customize the authorization logic. (So you grant someone permission to call "Absence.create" without necessarily granting access to the broader "Activity.create".)

An aside: If anyone else needs to go down the route of writing a "get" API that includes interesting joins/groupings, let's talk about https://github.com/civicrm/civihr/blob/master/hrabsence/CRM/HRAbsence/DGWDIHTWT.php . It's safer/cleaner than straight SQL string-manipulation, more flexible than DB_DataObject/DAO, and more re-usable than the BAO_Query or Reports. It doesn't really hold a candle to Doctrine, but it's easy to backport and run on top of other SQL APIs.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Activity API: get activities assigned to a given contact
February 20, 2014, 03:34:27 pm
Love the name, but...

+1 for rename and put in the core.
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

capo

  • I post occasionally
  • **
  • Posts: 108
  • Karma: 5
Re: Activity API: get activities assigned to a given contact
February 21, 2014, 05:46:44 am
Wouldn't it be better to implement a "ActivityContact" entity? It sounds like it would be more flexible. Having such an entity, we would be able to list the participants (no matter its roles) of an activity. I'm not sure we can do that now. Isn't it?

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Activity API: get activities assigned to a given contact
February 21, 2014, 11:34:02 am
capo:
Quote
Wouldn't it be better to implement a "ActivityContact" entity?

Yes, that's what I was thinking, too. totten, xavier, is this practical?
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Activity API: get activities assigned to a given contact
February 21, 2014, 02:13:19 pm
@capo, @TwoMice - Good point. Don't think it would be any harder to write than the other APIs.

@xavier - PR is https://github.com/civicrm/civicrm-core/pull/2544

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Activity API: get activities assigned to a given contact
February 21, 2014, 11:17:38 pm
@totten cool, thx
and now the magic question: how to promote it so everyone is aware of it.

@TwoMice

Certainly very useful to have it, and not complicated to write (check out other simple entities like email or phone, it's a dozen lines of code)

However, you might have to chain with the activitiy entity, because this api will just return the activity id, and you might want more info about it.

Depending of your use case, it might be too slow, if it's the case, you can smarten up the activity.get action to join fetch the activity and activitycontact and filter directly.

-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) »
  • Activity API: get activities assigned to a given contact

This forum was archived on 2017-11-26.