Webform conditional answer options

For a Webform based survey site I needed to create questions offering a choice between some *conditionally shown* answers. Because I had very little time, I decided to make some quick & easy changes to the Webform template, instead of creating a custom module.

My solution was to check for previous entries within the webform-form.tpl.php file & remove unneeded answers using some jQuery magic. Since it is no problem if the user unexpectedly might still see the hidden answers (for example, when JS is turned off), this suffices for now.

The resulting quick-n-easy adapted webform-form.tpl.php is attached below.

<?php
/**
 * @file
 * Customize the display of a complete webform.
 *
 * This file may be renamed "webform-form-[nid].tpl.php" to target a specific
 * webform on your site. Or you can leave it "webform-form.tpl.php" to affect
 * all webforms on your site.
 *
 * Available variables:
 * - $form: The complete form array.
 * - $nid: The node ID of the Webform.
 *
 * The $form array contains two main pieces:
 * - $form['submitted']: The main content of the user-created form.
 * - $form['details']: Internal information stored by Webform.
 */

 // Retrieve total pages and current page.
 
$current_page = $form['details']['page_num']['#value'];
 
$total_pages = $form['details']['page_count']['#value'];
?>


<div class="webformed-<?php echo $current_page; ?>">

<?php
 
echo '<div id="page-count">' . $current_page . " / " . $total_pages . '</div>';

 
// If editing or viewing submissions, display the navigation at the top.
 
if (isset($form['submission_info']) || isset($form['navigation'])) {
    print
drupal_render($form['navigation']);
    print
drupal_render($form['submission_info']);
  }

 
// Print out the main part of the form.
  // Feel free to break this up and move the pieces within the array.
 
print drupal_render($form['submitted']);

 
// Always print out the entire $form. This renders the remaining pieces of the
  // form that haven't yet been rendered above.
 
print drupal_render($form);

 
// Print out the navigation again at the bottom.
 
if (isset($form['submission_info']) || isset($form['navigation'])) {
    unset(
$form['navigation']['#printed']);
    print
drupal_render($form['navigation']);
  }


?>

</div>

<script type='text/javascript'>
<?php

// Check if current page is relevant one.
if ($current_page==15) {

 
// Retrieve current submission id.
 
$sid = $form['#submission']->sid;
 
// Include webform.submissions.inc.
 
include_once(drupal_get_path('module', 'webform') .'/includes/webform.submissions.inc');
 
// Retrieve results
 
$subm = webform_get_submission($nid, $sid);

 
// Remove relevant answers using jQuery - no case statement, loop or function, just some quick if/thens 
 
if ($subm->data[22]['value'][0]!=4) echo "$('#edit-submitted-question77-1-wrapper').remove();"
  if (
$subm->data[33]['value'][0]!=7) echo "$('#edit-submitted-question77-2-wrapper').remove();";
  if (
$subm->data[64]['value'][0]!=3) echo "$('#edit-submitted-question77-3-wrapper').remove();"
  if (
$subm->data[15]['value'][0]==1) echo "$('#edit-submitted-question77-4-wrapper').remove();"
  if (
$subm->data[12]['value'][0]!=7) echo "$('#edit-submitted-question77-5-wrapper').remove();"
  if (
$subm->data[13]['value'][0]!=3) echo "$('#edit-submitted-question77-6-wrapper').remove();";

}
?>

</script>

Comments