0

I am trying to learn Rules and adding a custom condition. I need help to figure out what I am doing wrong.

My Condiiton set up:

function rules_example_capacity_condition_info() {
   $conditions = array(
     'rules_example_action_count_capacity' => array(
       'label' => t('Print capacity count on page'),
       'group' => t('Rules examples'),
       'parameters' => array(
         'count' => array('type' => 'value', 'label' => t('registration event count')),
         'capacity' => array('type' => 'value', 'label' => t('registration event capacity')),         
         ),
       ),
     );
     return $conditions; 
 }

and the function that calls it...

function rules_example_action_count_capacity($count, $capacity) {
    $reg_count = reg_count_load($count);
    return($reg_count->capacity == $capacity) ? true : false;
}  

I am trying to determine the count (how many people have registered so far) and the capacity. When the count and capacity are the same send an email.

I am not a developer so this is a little bit of a struggle. I found this series (http://nodeone.se/node/28) which is helpful, however there are so many smaller things I might be missing. Please help if you can. I am hoping that this will help more then just myself.

Thanks Tim

1

Looking at your function, I believe you need to rename it to something like mymodule_rules_condition_info. The hook you are trying to implement is hook_rules_condition_info(). Did you find that your custom condition was showing up when you were editing a rule?

function MYMODULE_rules_condition_info() {
   $conditions = array(
     'mymodule_event_is_at_capacity' => array(
       'label' => t('Event has reached maximum registrations'),
       'group' => t('MY MODULE'),
       'parameters' => array(
         'registration' => array(
            'type' => 'registration',
            'label' => t('The registration event'),
           ),         
         ),
       ),
     );
     return $conditions; 
 }

Semantically you may want to call your condition 'mymodule_event_is_at_capacity' and it should only need one parameter which is the capacity that you will provide in the Rules UI.

The function you will be writing mymodule_event_is_at_capacity($capacity) will query for the number of (active) users in Drupal, and if it is greater than or equal to $capacity, it will return TRUE.

All of this can be kicked off by add a new Rule that reacts on the "After saving a new user account" event.

  • Thanks for the information! However, I am still a little confused. While the tweaked you shared does help a little, I still need to set up a function for the action. Right now the above triggers whenever you go to a page with Registration on it, not when capacity is hit. My guess is the function to call the action would control that. I think that I have to compare the registration count with the capacity to trigger the email. I do have a rule set up 'After saving a new registration'. I was hoping that this new condition could help count the registrations and and compare to the capacity. – timwhelan Jun 16 '14 at 12:37
  • Yes. You should look at the code for the registration modules. It has two functions, registration_has_room() and registration_event_count() which should help you. Your callback for your condition should only need to take in one parameter, which is the $registration that was just saved and it should be able to return TRUE/FALSE by passing that $registration to the registration_has_room() function. If it returns TRUE, your action for your Rule will send the email. – Al Gorgeous Jun 22 '14 at 2:44
  • Okay, that all makes sense, however I do not know how to write the function that passes $registration to registration_has_room() and return true or false. I have search but cannot find anything that helps me. I have tried multiple ideas. Hoping that there is a an example somewhere...If you know or can share please let me know. Thanks! – timwhelan Jul 18 '14 at 19:12
  • Sorry to take so long. This did answer my question. I tend to have questions on my questions. Thank you! – timwhelan Jul 22 '14 at 20:27
0

Al Gorgeous Thank you for you help. Below is the final working example.

So, I had a lot to learn here. I have some help figuring this out.

I had to create a custom condition for Rules and then figure out the true/false statement to trigger the email. The hard part was having to figure out how to bring in the entity_id. I found this hacked way and it worked.

Hacked way to to grab the entity_id - How do I get entity_type and entity_id from any entity page?

function ecg_registration_rules_condition_info() {
  $conditions = array(
  'ecg_registration_event_is_at_capacity' => array(
       'label' => t('Event has reached maximum registrations'),
       'group' => t('MY MODULE'),
       'parameters' => array(
         'registration' => array(
            'type' => 'registration',
            'label' => t('The registration event capacity'),
           ),   
         ),
       ),
}

The function that tracks and triggers the capacity count.

function ecg_registration_event_is_at_capacity($registration) {
  $backtrace = debug_backtrace();
  foreach ($backtrace as $caller) {
    if ($caller['function'] == 'field_attach_view') {
      $entity_type = $caller['args'][0];
      $entity = $caller['args'][1];
      // do stuff with entity
      break;
    }  
  } 
  $entity_id = $entity->{'nid'};  
  $count =  registration_event_count($entity_type, $entity_id);
  $settings = registration_entity_settings($entity_type, $entity_id);
  if ($settings['capacity'] == 0) {
    return FALSE;
    break;
  } 
  if ($count >= $settings['capacity']) {   
      return TRUE;
  } 
  return FALSE;
}

Hope this helps someone else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.