There is a petition module available for drupal, but it requires installation of CiviCRM - a complicated affair.

Webform seems poised to accomplish this task too. I'd like to use Webform to generate a petition that allows the user to see all those who have previously signed. It seems like it would be easy enough to do if I only knew PHP better.

I envision calling the _webform_results_table function (sans the #, time, and IP address) within the confirmation message. Anybody able to assist? Thanks!

Comments

scottrussell-1’s picture

Okay, after a little work, I've found a working solution. In the confirmation message section, insert this text/code...


echo "Thank you for signing the petition.  We hope to use this as a tool to support our efforts.";

$nid=arg(1);

function _webform_results_table($nid) {
  include_once(drupal_get_path('module', 'webform')."/webform.inc");
  // Load Components
  _webform_load_components();

  $header = array();
  $rows = array();
  $cell = array();

  
  // Get all the component cid and names for the node
  $query = 'SELECT cid, name, type, extra FROM {webform_component} WHERE nid = %d ORDER BY weight, name';
  $res = db_query($query, $nid);
  while($component = db_fetch_array($res)) {
    $components[] = $component;
  }

  // Get all the submissions for the node
  $submissions = _webform_fetch_submissions($nid);
  // Generate a row for each submission
  foreach ($submissions as $sid => $submission) {
    $componentHeaders = array();

    // Generate a cell for each component
    foreach($components as $component) {
      $component['extra'] = unserialize($component['extra']);
      $tableFunction = "_webform_table_data_".$component['type'];
      if (function_exists($tableFunction)) {
        $submissionOutput = $tableFunction($submission['data'][$component['cid']],$component);
        if ($submissionOutput !== NULL) {
          $componentHeaders[] = $component['name'];
          $cell[]   = $submissionOutput;
        }
      }
    }
    
    $rows[] = $cell;
    unset($cell);
  }
  if (!empty($componentHeaders)) {
    $header = array_merge($header,$componentHeaders);
  }

  return theme('table', $header, $rows);
} // end function _webform_results_table($nid)

$content= _webform_results_table($nid);

return $content;

Make sure you select "PHP Format" under the "Input Format" dropdown. For me, it worked exactly as I'd hoped!

scottrussell-1’s picture

I've run into one stumbling block with this approach. The only way I can allow the website visitor to see the petition result is if I provide the anonymous user privileges to "access webform results" within the 'Administer --> Access Control' settings.

Unfortunately, I think there's a bug here regarding the "access webform results" privilege. I've set it up so that the administrator has full webform privileges (all are checked) but the authenticated and anonymous user have ONLY the 'access webform results' privilege.

For these two account types, "webform" is displayed at the root level of the navigation menu (and this links to http://examplesite.com/admin/webform). This option does not appear to an administrator account, except where it should (under Admin --> webform). When this root navigation choice is clicked, the authenticated & anonymous user have full access to administering the webform -- and this should NOT be happening.

Anyone else seeing this anomaly? Know how to remedy it? Thanks!

evanssd’s picture

The php solution works well for me. What would I change to exclude one component, for example email address? Thanks in advance.

naught101’s picture

Title: webform to see petition results? » webform to see customisable petition results?
Version: 4.7.x-1.x-dev » 5.x-1.x-dev

this is the same for version 5.x-1 so I've changed it.

thanks scott, for the phpcode. note you can also replace "arg(1)" with the node ID number and submit the code as a new node, and it also wrks fine.

I second evanssd's comment. it would be VERY useful to know how to exclude certain fields. email is a good example, but if it's for a petition, you might also have some fields like a "stay in touch" tick-box, which you wouldn't neccesarily want to display on a page of signed submissions.

with big petition pages, it might be a good idea to have the option to view these 50 entries at a time or something.

it would be really good to be able to do this be default with webform! it's a great module, and with this ability, it would be useful for SOOO many more things.

cheers
ned

naught101’s picture

Component: Miscellaneous » Code

so... this works for me. it gets rid of the email column in the table. infact, even if you have multiple email components in your form, it'll get rid of all of them.

this is a patch on scott's code.

  // Get all the component cid and names for the node
  $query = 'SELECT cid, name, type, extra FROM {webform_component} WHERE nid = %d ORDER BY weight, name';
  $res = db_query($query, $nid);
  while($component = db_fetch_array($res)) {
    $components[] = $component;
  }

//   -------   ADD FROM HERE   -------   
 //remove all any component arrays including the term "email"
for ($i = 0; $i < count($components); $i++) {   
if ($components[$i]['type'] == "email"){
	unset($components[$i]);
	}
}
//   -------   TO HERE   -------   

  // Get all the submissions for the node
  $submissions = _webform_fetch_submissions($nid);

you can remove any column for any type in this way. you can also remove columns by name if you change 'type' for 'name'. make sure you spell the name correctly though!

I don't seem to have the problme that scott's having to do with anon users requiring editing permission. maybe because I'm using drupal 5.

quicksketch’s picture

Status: Active » Closed (fixed)

I'm closing this issue because it encourages the general practice of hacking the module directly, a bad Drupal practice. The same thing can now be accomplished using the "Additional Submission Code" option under the Advanced webform options.

Leech’s picture

Thank you, works great for me!