March 4, 2012

Viewing Users Who Have Not Submitted a Webform

On a recent Drupal 6 site, I received a request to build a view displaying a list of users who had not submitted a specific webform. Webform of course, has some very nice reporting as well as decent views integration for submissions. However, to view users that have yet to make a submission takes some creative views argument handling.

I began by building a basic user view. I then set the style and fields to my liking, and filtered on the specific role I was looking for. So now we have views pulling a list of users that we wish to check against. To find out who has not submitted the webform, we first have to take a look at who has.

webform_get_submissions($filters = array(), $header = NULL, $pager_count = 0);

Webform has the above function in the file that does just this. You can pass a node id to $filters and the function will return a nice array of submission objects, detailing who has submitted the form. We can then add an argument of type "User: UID" to our view, select "Provide default argument" below "Action to take if argument is not present," and select "PHP Code" under "Default argument type." Here we can provide a code snippet to return a list of User ids of users that have submitted the form.

module_load_include('inc', 'webform', 'includes/webform.submissions');
$submission_array = webform_get_submissions(/*node id of webform we are checking for*/);
$uids = array();
foreach($submission_array as $submission) {
  $uids[] = $submission->uid;
return implode(',', $uids);

This will gives us a comma separated string of user ids. The default "Validator options" are fine but be sure to check "Allow multiple terms per argument" and "Exclude the argument." After updating, your view now filters down to users that have not submitted the webform! To double check that this is happening, you can look at the query below the preview. Near the end of the query in the WHERE clause, you should see something like users.uid NOT IN (110, 13028, 210, 107, 35329, 35330, 35331). The list of numbers are the user ids that are being filtered. If no one has submitted the form, then this of course will be empty.

Now I admit, this is kinda gross. I shudder any time I include php in configuration. It is, however, a very straight forward way to get the results requested while staying in views, therefore leaving the output in the hands of a site builder/administrator. There may be some clever ways to achieve the same thing using something like Webform Report, or even a different views configuration, but nothing jumped out at me. However, if you have solved this in a different way I would love to hear about it.

Ryan Oles