Simple survey customization for Drupal

Ever wanted to force users to submit a quick (or not so quick) survey before giving them access to content on your Drupal site? With the webform module and a couple of quick customizations, it’s a breeze. This solution uses the webform module, so the first step is to download and install this module.Ever wanted to force users to submit a quick (or not so quick) survey before giving them access to content on your Drupal site?

Steven Merrill, Director of Devops
#Drupal | Posted

Ever wanted to force users to submit a quick (or not so quick) survey before giving them access to content on your Drupal site? With the webform module and a couple of quick customizations, it’s a breeze. This solution uses the webform module, so the first step is to download and install this module.Ever wanted to force users to submit a quick (or not so quick) survey before giving them access to content on your Drupal site? With the webform module and a couple of quick customizations, it’s a breeze.

This solution uses the webform module, so the first step is to download and install this module. Next create a custom module that contains the following code:

  1. function module_name_init() {
  2. global $user;
  3. $webform_nid = 999;
  4. $submission_count = db_result(db_query("<span class="caps">SELECT</span> count(*) <span class="caps">FROM</span> {webform_submissions} <span class="caps">WHERE</span> nid = %d <span class="caps">AND</span> uid = %d", $webform_nid, $user->uid));
  5. if ($submission_count > 0)
  6. return;

$_SESSION['return_url'] = 'node/' . $node->nid;
header('Location: ' . url('node/' . $webform_nid));
}

function module_name_nodeapi(&$node, $op)
{
if ($op  'load' && $node->type  'webform') {
$node->webform['confirmation'] = 'internal:' . $_SESSION['return_url'];
}
}

The hook_init() function determines if the user has previously submitted the corresponding webform. A couple of assumptions are made here. First, you’ll want to set the $webform_nid variable to something more reasonable then 999. For me, this value was derived from linking a webform via a CCK node reference field. Secondly, you probably want to limit the code to only running for a subset of the content on your site by putting some kind of if statement around the example code I’ve provided.

The hook_nodeapi() function simply sets a value on the webform object to dynamically control the redirect location based on a value found in the session. You might find it weird that I’m looking for the “load” operation here, but this is because the webform module does not actually create any nodes when you make a submission. Instead, the form submission handling code loads the desired webform object in order to complete the internal processing required for the module to function as desired.

The next step for me is to play around with trying to get the survey to display in a dynamic div or lightbox instead of using all this old-school HTTP redirect magic!

Steven Merrill

Director of Devops