Send An Email To All Group Managers Via A Reactive Rule In Drupal 7

Updated about 2 yrs, 2 mths ago (September 30, 2015). Know a better answer? Let me know!

Send an email to all group managers via a reactive rule in Drupal 7

The idea was that when someone signed up and requested to join a group, the administrators (not just the author) of that group would get an email letting them know someone had requested access.

I recently had to send an email to all the administrators of the current user’s Drupal organic group, via a Rules reaction rule. Because this was triggered by a pending account activation using a one-time login link, I wasn’t able to easily get the administrators of the current user’s group.

The idea was that when someone signed up and requested to join a group, the administrators (not just the author) of that group would get an email letting them know someone had requested access.

Anyway, in case it’s of use, here’s how I got all the administrators of an organic group, as a Rules list variable, and then used that to send an email to all the group administrators, once someone activated their account.

Rules configuration can be found at URL:

/admin/config/workflow/rules

Here we have a rule which reacts to the event “When the user account is activated”:

Active Rule

Inside that rule, we check a few things such as the user who has just activated their account actually has an existing group (in this case, they pick one on signup), then we get a list of the group managers, loop through it, and send them each an email:

Editing the rule

 

To create the action Get group managers for current user, you need to create a module (or tack this onto an existing one if you have something suitable you’ve already written), as below:

To create a new module:

 

<?php
/**
 * Implement hook_rules_action_info().
 */
function my_module_name_rules_action_info() {
  return array(
      'my_module_name_get_managers' => array(
          'label' => t('Get group managers for current user'),
          'group' => t('Custom'),
          'provides' => array(
              'group_managers' => array('type' => 'list', 'label' => t('List of group managers')),
          ),
          'base' => '_my_module_name_get_group_managers'
          // 'access callback' => 'access_callback_function',
      ),
  );
}

function _my_module_name_get_group_managers()
{
  global $user;

  $uid = $user->uid;
  if($uid === 0) {
    // not logged in
    watchdog('rules', 'No logged in user found', array(), WATCHDOG_WARNING);
    return;
  }
  $account = user_load($user->uid);

  if(!isset($account->og_service[LANGUAGE_NONE][0]['target_id'])) {
    // no service found
    watchdog('rules', 'No service found', array(), WATCHDOG_WARNING);
    return;
  }
  // get group id
  // this is specific to my implementation - the user has a field
  // called "og_group" which stores their group
  // your implementation would probably vary
  $gid = $account->og_group[LANGUAGE_NONE][0]['target_id'];

  $members = array();
  $current_members = db_select('og_users_roles', 'ogur');
  $current_members->fields('ogur', array('uid'));
  $current_members->condition('ogur.rid', 3, '>=');
  $current_members->condition('ogur.gid', $gid);

  $result = $current_members->execute();
  while ($res = $result->fetchAssoc()) {
    $members[] = $res['uid'];
  }

  // Remove duplicate items.
  $members = array_keys(array_flip($members));

  // get user objects
  $return = array();
  foreach($members as $key => $val)
  {
    $return[] = user_load($val);
  }
  return array('group_managers' => $return);
}

More Information

Information above gathered from the following sources:

 

 

Updated about 2 yrs, 2 mths ago (September 30, 2015). Know a better answer? Let me know!

Related categories [coloured].

Comment on this article (no HTML, max 1200 characters):