Workflow-ng is already able to send email to a user when certain actions and conditions are met when browsing Drupal. Sending mail to all users of a specific role may be useful to notify all website administrators about updates to their site. It likely has unknown numbers of useful applications when used alongside custom content created with CCK.

The objective is to create a new action listed in the System section named "Send a mail to a role". The action should perform in relatively equivalent manner to the "Send a mail to a user" action with the exception that it will send the mail to all users in a selected role from a select box.

1. Read more about workflow-ng: http://drupal.org/node/156282
2. Familiarize yourself with writing actions and conditions: http://drupal.org/node/156754
3. Use existing Drupal API functions where appropriate. Examples:
user_roles(): http://api.drupal.org/api/function/user_roles/5
drupal_mail(): http://api.drupal.org/api/function/drupal_mail/5

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

deekayen’s picture

Title: GHOP: send a mail to a role » GHOP #103: send a mail to a role
cwgordon7’s picture

Status: Active » Needs review
FileSize
4.43 KB

Patch attached. Needs review.

fago’s picture

Status: Needs review » Needs work

* please make use of the latest workflow-ng token API. Read the docs about it (http://drupal.org/node/156754) and make sure to work with the latest workflow-ng code (branch DRUPAL-5)

* use the user_roles() parameter to get members only, instead of manually removing anonymous users.

* use array_filter() to filter the results of the checkbox. Best do this before return the settings of the action.

* text: "Select which roles should receive this email." Roles can't receive mails, but the users do.

* Sending such masses of mails might be to resource intensive for a lot of sites with many users. For this case we would need a mail queuing system, but this isn't the scope of this task. However please add a note, so that people can decide on their on and use this action or not. E.g. add it to the description of your action. Or elsewhere, but people should not overlook this!

fago’s picture

Component: Miscellaneous » Code
Category: feature » task
deekayen’s picture

* watchdog() has other status constants than notice. You should use WATCHDOG_ERROR when you have an error status (setting NULL isn't necessary either since it's the default).

* be sure to keep your spacing consistent on loops. foreach( should have a space like foreach (

cwgordon7’s picture

FileSize
4.55 KB

Patch re-rolled.

cwgordon7’s picture

Status: Needs work » Needs review
cwgordon7’s picture

Assigned: Unassigned » cwgordon7
fago’s picture

Status: Needs review » Needs work

Thanks, we are already closer.. :)

* Don't use workflow_ng_token_get_used_arguments(), use workflow_ng_token_get_settings() instead (new API) - this will make your code simpler.

* The warning in the checkboxes description can be overlooked too easily. I suggest, we add a paragraph on top of the checkboxes showing the warning - I think this would be recognized by much more users.

* Another thing I overlooked before: Use http://api.drupal.org/api/constant/DRUPAL_AUTHENTICATED_RID/5 instead of 2.

cwgordon7’s picture

Status: Needs work » Needs review
FileSize
5.47 KB

Patch re-rolled again. Hopefully this is it :) .

fago’s picture

Status: Needs review » Needs work

again, we are closer. Though there are some points left that need to be addressed:

* minor, but usually first comes the action implementation function workflow_ng_action_mail_to_users_of_role() and then the form configuration - please also adapt the order like this. Also add a short comment at least to first function like it's done for all other actions.

* Don't use the token integration on the recipients array. Token replacements can be only done with string values!

* Then let's improve your code a bit. You could eliminate the last foreach - just build $result in the above if/else, then use one while loop, in which you send out the mails.

* You have added now array_filter, so you can remove the check " if ($chose_role) {" when iterating over $recipients. So now you build up $recipient_roles from $recipients, which isn't necessary any more. Use in_array() to check for authenticated users.

* Your second query uses a %s placeholder, which doesn't fit for this case. Just add the imploded role ids directly to the query - they need not be checked as it are only ids, that stem from the database - it's no user input.

cwgordon7’s picture

Status: Needs work » Needs review
FileSize
5.72 KB

Ok, I got up early with the hope that I could get feedback on this without waiting 24 hours. Patch re-rolled.

fago’s picture

Status: Needs review » Needs work

hm, or work more carefully then it needs less reviews..

* you have got some debug statements in your code

* coding style: while( -> while (

* move the while() out of the if/else statement. The while is identical for both cases, so don't duplicate it!

cwgordon7’s picture

FileSize
5.28 KB

Ok, patch rerolled. So sorry about the debug code left in there :(. I guess this is what I get for trying to work on drupal early in the morning... Thank you for being so patient with me through this process :).

-cwgordon7

fago’s picture

Status: Needs work » Fixed

thanks for your work! good job, committed :)

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.