Problem/Motivation

Currently redhen_engagement includes three module-specific rules actions enabling scoring engagements for comments, registrations, and webform submissions. These rules use either a user ID or an email address to match contact records.

However, it would be good to have more flexible actions that are not specific to given modules. More flexible actionis would make it possible to configure rules for any entity that provides an email address or user ID.

Also, engagement scores in rules are stored, and therefore exported, by ID. It would be better to use the machine name (the 'name' field) so that exported rules will import consistently.

Proposed resolution

  • Convert existing rules to use the machine name field for engagement scores rather than the ID field. For consistency, also convert the filtering on engagements, which uses the same helper function as the rules to generate a list of options for engagement scores.
  • Introduce two new rules actions, score_engagement_by_mail and score_engagement_by_uid. Both take four arguments: score, entity_type, entity_id, and either mail or uid.
  • Rework the three existing redhen_engagement rules actions to use these new generic actions.
  • Add a new API function, redhen_contact_load_by_mail() and, in parallel, reworks the existing redhen_contact_load_by_user() to avoid using the deprecated $conditions argument to entity_load().

Remaining tasks

Not tested yet. Needs review.

Consider dropping some or all of the existing comment, registration, and webform rules actions. For example, if this patch is successful, the comment action should be superfluous as it will be possible to construct a rules scoring comment engagements using the newly introduced generic actions--though two rules might be required, one for anonymous (using email address) and another for authenticated (using UID).

User interface changes

Users building rules will see two new rules actions.

API changes

Adds a new API function, redhen_contact_load_by_mail().

Comments

nedjo’s picture

Status: Active » Needs work
StatusFileSize
new8.89 KB

I've only conceptually sketched this in and done no testing, so marking needs work.

I'm working on this partly as an enabler for #1772710: Enhance default rules integration for RedHen contacts.

nedjo’s picture

Status: Needs work » Needs review
StatusFileSize
new9.16 KB

Fixed up some problems with the patch. Haven't fully tested. Here's a sample rule to test with, to score an engagement when a comment is posted. Requires an engagement type with the ID of 1.

{ "rules_redhen_score_comment" : {
    "LABEL" : "RedHen score comment",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "redhen_engagement", "comment" ],
    "ON" : [ "comment_insert" ],
    "IF" : [
      { "data_is_empty" : { "data" : [ "comment:author:uid" ] } },
      { "NOT data_is_empty" : { "data" : [ "comment:mail" ] } }
    ],
    "DO" : [
      { "score_engagement_by_mail" : {
          "score" : { "value" : { "1" : "1" } },
          "type" : "comment",
          "id" : [ "comment:cid" ],
          "mail" : [ "comment:mail" ]
        }
      }
    ]
  }
}
nedjo’s picture

StatusFileSize
new11.34 KB

We should really be using the machine name of engagement scores rather than IDs in rules so that they will export and import dependably.

Revised patch attached makes this change. Note: this will break existing rules that use the score IDs.

This also changes the existing engagement type filtering to use the 'name' field rather than the 'engagement_score_id' field.

Needs more testing.

nedjo’s picture

Issue summary: View changes

Consider dropping some existing rules actions.

nedjo’s picture

Here's an updated sample rule to try with the patch in #3. Assumes an engagement score with the machine name "standard".

{ "rules_redhen_score_comment" : {
    "LABEL" : "RedHen score comment",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "redhen_engagement", "comment" ],
    "ON" : [ "comment_insert" ],
    "IF" : [
      { "data_is_empty" : { "data" : [ "comment:author:uid" ] } },
      { "NOT data_is_empty" : { "data" : [ "comment:mail" ] } }
    ],
    "DO" : [
      { "score_engagement_by_mail" : {
          "score" : { "value" : { "standard" : "standard" } },
          "type" : "comment",
          "id" : [ "comment:cid" ],
          "mail" : [ "comment:mail" ]
        }
      }
    ]
  }
}
levelos’s picture

Status: Needs review » Needs work

Great concept @nedjo, I whole heartedly support the approach. I'm unable to get your patch working, though. In particular,

+++ b/modules/redhen_engagement/redhen_engagement.rules.incundefined
   $defaults = array(
     'parameter' => array(
       'score' => array(
-        'type' => 'list<integer>',
+        'type' => 'list<string>',
         'label' => t('Engagement Score'),
         'options list' => 'redhen_engagement_score_options',
       ),

Type list does not work on my end. Setting it back to integer works, but the list is allowing multiple options.

+++ b/modules/redhen_engagement/redhen_engagement.rules.incundefined
@@ -106,43 +172,17 @@ function redhen_engagement_rules_action_score_webform($score, $webform) {
+      redhen_engagement_rules_action_score_by_uid($scores, 'node', $record->nid, $record->uid);

Should be $score.

levelos’s picture

Assigned: Unassigned » tauno
tauno’s picture

Working on this. Committed changes to 1.x-dev that changes engagements to use the machine names of engagement scores and adds some additional metadata handling. The rules actions still need to be updates. I like the generic approach in the patch, so we'll probably work off of that.

tauno’s picture

Status: Needs work » Fixed

Re-worked the patch and committed it. Thanks for getting it started nedjo!

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

Use machine name rather than IDs for storing engagement scores in rules.

  • Commit a090701 on 7.x-1.x, tests, redhen-donation by tauno:
    Issue #1796944 by tauno, nedjo: Add generic rules actions for RedHen...

  • Commit a090701 on 7.x-1.x, tests, redhen-donation, relation-roles by tauno:
    Issue #1796944 by tauno, nedjo: Add generic rules actions for RedHen...