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) »
  • Support »
  • Using CiviCRM »
  • Using CiviEvent (Moderator: Yashodha Chaku) »
  • [interim solution] CiviCRM API for query of events not working properly
Pages: [1]

Author Topic: [interim solution] CiviCRM API for query of events not working properly  (Read 1108 times)

damaxl

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 0
  • CiviCRM version: 4.1.2 DE
  • CMS version: Drupal 7.12 / 7.14
  • MySQL version: 14.12
  • PHP version: 5.2.6
[interim solution] CiviCRM API for query of events not working properly
May 09, 2012, 05:28:33 am
Hello,

unfortunately and after many hours experimenting I have to say, that CiviCRM API doesn't what it is expected to in case of querying events.

The starting point was, that I wrote a drupal module to display upcoming events. The following example was a basis for the code
http://wiki.civicrm.org/confluence/display/CRMDOC40/List+of+Upcoming+CiviEvents+or+Feed+of+Events

Finally I had to realize, that some of the events were not displayed and so I started to find out why. I activated the build in CiviCRM block for upcoming events on a test node and to my suprise it showed all events correctly.
So I went back to experiment with my module, stripped of all unnecessary things like filtering, sorting, limiting and so on.

But whatever I do, the missing events are not shown. So I dare to say, that there's an issue with the API.

Any idea what could be the reason?
Did anybody write a module on basis of CRM/Event/BAO/Event.php, which is the basis for the built in upcoming events block?

The left over test function inside the module:
Quote

function civicrm_test_getevents2() {

  if(module_exists('civicrm')) {

    civicrm_initialize(TRUE);
    require_once 'api/v2/Event.php';
    // This example limits listing to public and active events
    $params = array ('is_public'     => 1, 'is_active'     => 1);
    $myEvents = civicrm_event_search( $params );
    $display = '';
    if($myEvents) {
      $count = 0;
      $last = '';
      usort($myEvents,'cmp_date');
      foreach ($myEvents as $event) {
        $display .=  "<li>".sprintf("%2d", (int)$event['id'])." - ".$event['event_title']."</li>";
        $count++;
      }
      $display = "<pre><ul>".$display."</ul></pre>";
      if ($count <= 0) {
        $display = 'No events found.';
      }
    } else {
      $display = 'No events found.';
    }
  }
  else {
    $display = 'CiviCRM not installed.';
  }
  return($display);
}



« Last Edit: August 02, 2012, 07:50:00 am by damaxl »

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: CiviCRM API for query of events not working properly
May 09, 2012, 08:51:03 am
That wiki page is outdated and uses v2 of the API. You will find the v3 is much better: http://wiki.civicrm.org/confluence/display/CRMDOC41/CiviCRM+Public+APIs
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

damaxl

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 0
  • CiviCRM version: 4.1.2 DE
  • CMS version: Drupal 7.12 / 7.14
  • MySQL version: 14.12
  • PHP version: 5.2.6
Re: CiviCRM API for query of events not working properly
May 09, 2012, 03:56:16 pm
Hello Hershel,

thank you for your response.

I forgot to say, that in may original module I used API V3.

To be sure I even changed my test modul. Unfortunately the same behaviour as I descriped in my first post. Some events are not listet, but they are listet with the built in upcoming events block.

Any other idea?

Changed source code to this
Quote

function civicrm_test_getevents2() {

  if(module_exists('civicrm')) {

    civicrm_initialize(TRUE);
    require_once 'api/api.php';
    $params = array (
      'is_public' => 1,
      'is_active' => 1,
      'version' => 3
    );
    $myEvents = civicrm_api('event', 'get', $params);

    $display = '';
    if($myEvents) {
      $count = 0;
      $last = '';
      usort($myEvents['values'],'cmp_date');
      foreach ($myEvents['values'] as $event) {
        $display .=  "<li>".sprintf("%2d", (int)$event['id'])." - ".$event['event_title']."</li>";
        $count++;
      }
      $display = "<pre><ul>".$display."</ul></pre>";
      if ($count <= 0) {
        $display = 'No events found.';
      }
    } else {
      $display = 'No events found.';
    }
  }
  else {
    $display = 'CiviCRM not installed.';
  }
  return($display);
}




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: CiviCRM API for query of events not working properly
May 09, 2012, 07:43:36 pm

The event API is just starting to get more usage, so it would be great if you can jump into the API code and see what needs to be fixed. You could illustrate whats broken via a test and then help figure out a patch to ensure the test passes in future releases

Ping us on IRC if u need help getting started. The API v3 team is super helpful and would love to have some extra set of hands helping make the api all inclusive, all powerful etc

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

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: CiviCRM API for query of events not working properly
May 09, 2012, 10:26:44 pm
Hi,

Spent some time yesterday on it with IRC. It seems that the get ignores the param and always return all the events (at least was the issue when trying to filter by title).

To verify it's this (and that what you do fixes it), add tests (that should fail) tests/phpunit/api/v3/EventTest.php

Code: [Select]
  function testGetEventByWrongTitle() {
    $params = array('title' => 'No event with that title',
      'version' => $this->_apiversion,
    );

    $result = civicrm_api('event', 'get', $params);
    $this->assertEquals($result['count'], 0); // this should fail and return one event, even if the title doesn't match
  }


The get event is probably better replaced by a generic get

Code: [Select]
function civicrm_api3_event_get($params) {
  return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
}


But I haven't verified if it breaks something (run all the tests for Events should confirm)

You'd need to install from svn & set up a test environement. Despite all my moaning, isn't that difficult.

Would be super helpful if you could do the above.

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

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: CiviCRM API for query of events not working properly
May 09, 2012, 11:17:18 pm
Add my 2p....would be great if you could spend some time on fixing the Event API!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

damaxl

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 0
  • CiviCRM version: 4.1.2 DE
  • CMS version: Drupal 7.12 / 7.14
  • MySQL version: 14.12
  • PHP version: 5.2.6
Re: CiviCRM API for query of events not working properly
August 02, 2012, 07:40:59 am
Sorry but I didn't and don't have the time to debug this, but here is a solution that works for us, in hopes this will help someone.

The function returns the values as the API V3 function does, so one doesn't have to change the rest of the code. This piece of code can then be changed in the future, when API V3 works as expected.

Code: [Select]
function civicrm_eventlist_sortdate($a,$b) {
  if ($a['start_date'] > $b['start_date']) return 1;
  if ($a['start_date'] < $b['start_date']) return -1;
  return 0;
}

function civicrm_eventlist_eventlist($eventtype=0) {

  $result = array();

  if (!civicrm_initialize()) {
    $result['is_error'] = 1;
    return($result);
  }

  $result['is_error'] = 0;
  $result['count'] = 0;
  $result['values'] = array();
  $file1 = './'. drupal_get_path('module', 'civicrm') ."/../CRM/Event/BAO/Event.php";
  $file2 = './'. drupal_get_path('module', 'civicrm') ."/../CRM/Core/Permission.php";
  if( is_file($file1) AND is_file($file2) ) {
    require_once($file1);
    require_once($file2);
    $cv = new CRM_Event_BAO_Event();
    $start = null;
    $type = ($eventtype == 0) ? null : $eventtype;
    $eventId = null;
    $end = null;
    $result['values'] = $cv->getCompleteInfo($start, $type, $eventId, $end);
    $result['count'] = sizeof($result['values']);
  }

  usort($result['values'], 'civicrm_eventlist_sortdate');
  return($result);
}

I don't mark this thread as solved, because this is only a partial or interim solution.

Maybe I'll find the time to examine this in one or two month.

« Last Edit: August 02, 2012, 07:50:41 am by damaxl »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviEvent (Moderator: Yashodha Chaku) »
  • [interim solution] CiviCRM API for query of events not working properly

This forum was archived on 2017-11-26.