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) »
  • [tutorial] :: Display an event's max number of participants in Drupal views
Pages: [1]

Author Topic: [tutorial] :: Display an event's max number of participants in Drupal views  (Read 2182 times)

BusinessMediaCenter

  • I post occasionally
  • **
  • Posts: 82
  • Karma: 2
  • CiviCRM version: 4.1.x
  • CMS version: Drupal 6 & 7
  • MySQL version: 5
  • PHP version: 5
[tutorial] :: Display an event's max number of participants in Drupal views
August 08, 2012, 11:08:03 am
By default, max number of participants is not available as a views field, to get around this, we can use the Views PHP module and the civicrm api to get the max participants value.  Thanks to drastik-jw on the civicrm irc for help constructing this.  Works with Civi 4.1.5 / Drupal 7 / Views 3 at the time of writing.

1) Install views php module http://drupal.org/project/views_php
2) Create a view showing CiviCRM Events
3) Add a views global php field to the view and follow code below.

Setup code (you will need to tick the 'Use setup code' checkbox first and do not use php enclosures)
Code: [Select]
if (module_exists('civicrm')){
  civicrm_initialize(TRUE);
  require_once 'sites/all/modules/civicrm/api/api.php';
  require_once 'CRM/Event/BAO/Participant.php';
}

Value code (do not use php enclosures)
Code: [Select]
if (module_exists('civicrm')){
  $params['id']=$row->id;
  $params = array( 'id' => $params['id'], 'version' => 3);
  $getEvent= civicrm_api('Event','Get',$params);
  $max=$getEvent;
  return $max; 
  //return a variable that contains all available data 
  //use print_r($data) in Output code to determine structure of data
}

Output code (you will need to use php enclosures here)
Code: [Select]
<?php
$event_id
=$row->id;  
//gets event id for use in the data field value
 
$max = $data->views_php_24['values'][$event_id]['max_participants'];
//gets max participants value (remember to replace views_php_24 with your appropriate field_name)
//field name can be found using print_r($data) below
 
print $max." Maximum Participants";  
//print max number of participants and add (concatenate) some descriptive text                

//uncomment code below to view all available data
//print "<pre>";
//print_r($data);
//print "</pre>";
?>

« Last Edit: August 14, 2012, 11:30:48 pm by everything »

Michael

  • I post occasionally
  • **
  • Posts: 69
  • Karma: 2
  • Isn't this just terrific?
  • CiviCRM version: 4.4.0
  • CMS version: Drupal 7.23
  • MySQL version: 5.3.19
  • PHP version: 5.5.30
Re: [tutorial] :: Display an event's max number of participants in Drupal views
March 31, 2013, 10:12:06 am
I am trying to create a view that will show a list of events that are not "full" and followed the directions in the post above from BusinessMediaCenter. However, I am testing this using version 4.2.8 on Drupal 7.21 and it doesn't work, I can't seem to get the field for max_participants to display. I checked the paths in the first block of code and they are still correct. I've tried every string I can think of to replace the field as per the instructions :

$max = $data->views_php_24['values'][$event_id]['max_participants'];
//gets max participants value (remember to replace views_php_24 with your appropriate field_name)
//field name can be found using print_r($data) below

... but that doesn't appear to work, I get an ajax error. I can't seem to get the max_participants to show up as either a field in the view, that max_participants field data isn't available as a variable.

Our organization has over a hundred volunteer events per month and needs a way to show which events are full and which have vacancies, or even just a list that displays only events with vacancies remaining.

Any advice on how to get the max_participants field to be available to a view would be greatly appreciated.

A comment was made that this might be a useful function for other organizations. If so, then maybe a better approach would be using a civicrm hook since it could be then deployed by all civicrm users, not only Drupalers. Any advice or examples on how to go about that specific task would be very much appreciated. Thanks in advance.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviEvent (Moderator: Yashodha Chaku) »
  • [tutorial] :: Display an event's max number of participants in Drupal views

This forum was archived on 2017-11-26.