3

How do I schedule the execution of a Rules Action based on a webform submission date field?

I have installed Webform Rules so I am able to use the Rules Event Webform submitted to trigger the execution of a Rules Action. In my case, I want to schedule the execution of the Rules Action based on the date input of a webform field.

I have no problem scheduling the execution using node:creation time, but I need to use the date the user inputs.

I have not found any data-selector that let's me do this.

Any hint's will be much appreciated.

EDIT: I have found a link on StackOverflow that seems related to what I want to achieve.

https://stackoverflow.com/questions/5192108/how-do-i-schedule-a-rule-using-fields

That would reduce my question to : how do I use the Entity has field on a webform in order to make the field available ?

  • I haven't tried this module myself nor do i know if you have already; but, this maybe what you need: drupal.org/project/rules_forms – chrisjlee Apr 20 '12 at 12:58
  • I have installed this module but no new Data selectors appear under the Scheduled evaluation date field. – drcelus Apr 20 '12 at 13:15
  • 1
    rules_forms does not pertain to webforms – Johnathan Elmore Apr 24 '12 at 18:26
  • drcelus: check that link, it is referencing a work-around for a Rules bug that was actually fixed in March 2011 – Johnathan Elmore Apr 24 '12 at 20:41
  • Merci (thank you) for the accept (and upvote) of my (late) answer ... Too bad I seemed to have missed some bounty when you first posted this question ... – Pierre.Vriens Dec 27 '16 at 14:33
1

The "clue" to what you're trying to get to work, is that you have access to all labels and all values of any field of a webform, ie:

  • [data:MachineFieldName-title] contains the Label of a field with machine name MachineFieldName.
  • [data:MachineFieldName-value] contains the Value of a field with machine name MachineFieldName

All that's left to fully answer your question is to have your rule use similar tokens.

For way more details on this, and a prototype rule about this, refer to the answer to "How to create a node from a Webform using rules?":

  • the 1st attempt relates to using "Entity has field" (which doesn't work for Webform)
  • the 2nd attempt explains what to do in case of Webform.
1

I'm not really familiar with Rules but this might be a step in the right direction.

If node:created worked you could give http://drupal.org/project/webform_tokens a try, it should define tokens for your form components, however I'm not sure if the date will be in the correct format.

If it's not in the correct format you could alter it for the compontent in a presave hook.

function hook_webform_submission_presave($node, $submission) {
  // Use dpm() to find out how the date is formatted, and make it into a timestamp.
  $date = strtotime($submission->data['COMPONENT_ID']['value'][0]);
  $submision->data['COMPONENT_ID']['value'][0] = $date;
}

Now you have altered the data for the submisison, converting a date into a timestamp which should then be used through webform_tokens just like node:created.

  • I tried to use webform_tokens on my site but it has integration issues when I tried to use it with the Rules module. I can't wait for the module to become more stable. – Johnathan Elmore Apr 25 '12 at 19:12
1

Edit: Okay, re-read the question and I totally missed the point. Removed all the stuff about e-mail processing.

Step #1: Create a hidden field "processed" which can be used to make sure it only gets processed once

Step #2: create a condition, Execute PHP code:

<?php

  // Load the webform by its Node ID
  $webform = node_load(123);

  // Load the webform's submissions by the Node ID
  module_load_include('inc', 'webform', 'includes/webform.submissions');
  $submissions = webform_get_submissions(123);

  // The submission array uses component ids,
  //   so search through the webform and reference component ids by their field name
  foreach($webform->webform['components'] as $component_id => $component)
  {
    if ($component['form_key'] == 'processed')
    {
      $processed_comp_id = $component_id;
    }
    elseif ($component['form_key'] == 'date')
    {
      $date_comp_id = $component_id;
    }
    elseif ($component['form_key'] == 'time')
    {
      $time_comp_id = $component_id;
    }
  }

  // Check all submissions and see if any need to be processed
  foreach($submissions as $submission)
  {
    // First check if the submission has already been processed
    if (isset($submission->data[$processed_comp_id]) &&
        empty($submission->data[$processed_comp_id]['value'][0]))
    {
      // Check the Scheduled Release time
      if (isset($submission->data[$date_comp_id]) && isset($submission->data[$time_comp_id]))
      {
        if (!empty($submission->data[$date_comp_id]['value'][0]) ||
            !empty($submission->data[$time_comp_id]['value'][0]))
        {
          $now = date("U"); // Current UNIX timestamp
          // Check if the current time is still prior to the scheduled time
          if($now < strtotime($submission->data[$date_comp_id]['value'][0]." ".
                                   $submission->data[$time_comp_id]['value'][0]))
          {
            // If it is, continue on to the next submission
            continue;
          }
        }
      }

      // Set the submission to successfully sent, and then update it
      $submission->data[$processed_comp_id]['value'][0] = 1;

      // If it couldn't be updated, change e-mail settings to notify of the error
      if (!webform_submission_update($webform, $submission))
      {
        $email_to = "your@email.com";
        $email_subject = "Error processing scheduled webform rule";
        $email_message = print_r($submission,true);
      }

      // Return True so that it passes the condition
      return true;
    }
  }

?>
  • Totally missed your question and had all this fancy stuff for e-mailing. This should accomplish what you need. – Johnathan Elmore Apr 24 '12 at 20:42
  • I found this workaround a bit of an overkill. I would like to solve this without having to hardcode so much. Thanks anyway. – drcelus Apr 26 '12 at 9:45
  • Meh. If there was an easier way I'd like to use it on our system as well. – Johnathan Elmore Apr 26 '12 at 14:33
1

Turns out the webform submission data is wrapped with it's own type and thus the date fields from webform submissions are not recognized as Dates by the Rules module.

The only workaround I found was to turn my webform into a different node type and use Views to emulate the webform results. The date fields in my custom node types are recognized by the rules module and I can schedule the mails to send.

There is an open issue on d.o. to change the way webform stores it's data.

0

I believe what you are trying to do is not possible. The reason being that webform submission data are not stored in the database. Therefore you can access the webform data only on a event like "When a form is submitted."

But maybe you could give a little more information on the field you are trying to create a rule frοm. Is it a field that you could possible write to a user's profile so you can have access to it without the need to know the sid of the submission? Maybe after saving it to the profile you could find another way to create a rule around that specific field now that is linked exclusively to the user that submitted the form.

  • 2
    Every form submission data is stored on the data base. The user field is a hack I may consider, thx. – drcelus Apr 23 '12 at 9:27
  • Yes the submission data is stored to the database but with no easy way of retrieving them through drupal. If you are going to code something like this yourself then you could add a cron job to run and parse through all you submission data and retrieve the specified field. If you are using drupal 6 do check Webform MySQL Views as you might find it useful later on dev.nodeone.se/node/666 – Perisdr Apr 23 '12 at 9:49
0

Shooting in the dark, but perhaps the Entity API module's "Entity Token" sub-module would help. Here is a quote about that sub-module (from the module's project page):

... makes sure there are tokens for most entity properties and fields, i.e. it provides token replacements for all entity properties (or fields) that have no tokens and are known to the entity module.

  • I already have this module activated. The problem is I need access to a webform date field to schedule an action. – drcelus Apr 23 '12 at 9:25

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.