For a site I'm working on, I'd like to be able to automatically create a set of registration codes when a certain Rules Event triggers. Providing a Rules Action that creates a Registration Code would be great.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

smokris’s picture

Status: Active » Needs review
FileSize
2.98 KB

A patch is attached, which adds a new "Create registration codes" Rules Action.

smokris’s picture

Alternate patch, which accepts a single taxonomy_term instead of a list<taxonomy_term> (so, in a single rule, you can create a term and then use it to create a regcode).

sumit@skytesters.com’s picture

Issue summary: View changes

You missed to increment

function regcode_rules_action_create($quantity, $size, $prefix, $format, $case, $maxuses, $begins, $expires, $term, $settings) {
  $code = new stdClass;
  $code->is_active = 1;
  $code->maxuses = $maxuses;
  $code->begins = $begins;
  $code->expires = $expires;

  for ($i = 0; $i < $quantity; $i++) {
    $code->code = $prefix . regcode_generate($size, $format, $case);
    regcode_save($code, array($term->tid => TRUE), REGCODE_MODE_REPLACE);
  }
}

value of i which results infinite loop of registration code being create.moreover you are not taking in consideration total length which results in increased length if we add prefix to it.

and as module is designed if no. of registration code is 1 only registration code produced should be the code with prefix character.....

sorry ,if something in wrong in English .hope things i wanted to convey are conveyed.

for which i made following modification...a newbie so code may not be optimised

function regcode_rules_action_create($quantity, $size, $prefix, $format, $case, $maxuses, $begins, $expires, $term, $settings) {
  $code = new stdClass;
  $code->is_active = 1;
  $code->maxuses = $maxuses;
  $code->begins = $begins;
  $code->expires = $expires;

  if ($quantity == "1")
  {$code->code = $prefix;
    regcode_save($code, array($term->tid => TRUE), REGCODE_MODE_REPLACE);
	}
	else{
  for ($i = 0; $i < $quantity; $i++) {
    $code->code = $prefix . regcode_generate($size, $format, $case);
    regcode_save($code, array($term->tid => TRUE), REGCODE_MODE_REPLACE);
  }}
}
bobfries’s picture

Has anyone had a deadlock issue running this rule action?

Using the following rule which attempts to create a new reg code using a hidden field when a group is created. It works most the time, typically the error comes when the client tests it!

{ "rules_create_registration_codes_for_group_invitees" : {
    "LABEL" : "Create Registration Codes For Group Invitees",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "regcode", "group" ],
    "ON" : { "group_insert" : [] },
    "IF" : [
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "4" : "4", "5" : "5" } },
          "operation" : "OR"
        }
      }
    ],
    "DO" : [
      { "regcode_rules_action_create" : {
          "quantity" : "1",
          "size" : "0",
          "prefix" : "[group:field-reg-code]",
          "format" : "alphanum",
          "case" : "1",
          "maxuses" : "100",
          "begins" : "now",
          "expires" : "+ 10 days",
          "term" : "5"
        }
      }
    ]
  }
}
TR’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev
Component: Code » Rules integration
Status: Needs review » Needs work

New features need to be put into the current branch first, then backported to previous versions if there is community interest.