Skip to content

Instantly share code, notes, and snippets.

@opi
Created March 27, 2012 16:28
Show Gist options
  • Save opi/2217692 to your computer and use it in GitHub Desktop.
Save opi/2217692 to your computer and use it in GitHub Desktop.
Drupal6 - Webform - Add custom data to result table and export
<?php
// Add custom data to submission
function mymodule_webform_submission_load(&$submissions) {
foreach ($submissions as $sid => $submission) {
$submissions[$sid]->data['mydata']['value'][] = $mydata;
}
}
// Alter registry to override theme_webform_results_table
function mymodule_theme_registry_alter(&$theme_registry) {
if (!empty($theme_registry['webform_results_table'])) {
$theme_registry['webform_results_table']['function'] = 'mymodule_webform_results_table';
}
}
// Override theme_webform_results_table
function mymodule_webform_results_table($node, $components, $submissions, $total_count, $pager_count) {
drupal_add_css(drupal_get_path('module', 'webform') . '/css/webform-admin.css', 'theme', 'all', FALSE);
$header = array();
$rows = array();
$cell = array();
// This header has to be generated seperately so we can add the SQL necessary.
// to sort the results.
$header = theme('webform_results_table_header', $node);
// Generate a row for each submission.
foreach ($submissions as $sid => $submission) {
$cell[] = l($sid, 'node/' . $node->nid . '/submission/' . $sid);
$cell[] = format_date($submission->submitted, 'small');
$cell[] = theme('username', $submission);
$cell[] = $submission->remote_addr;
$component_headers = array();
// Generate a cell for each component.
foreach ($node->webform['components'] as $component) {
$data = isset($submission->data[$component['cid']]['value']) ? $submission->data[$component['cid']]['value'] : NULL;
$submission_output = webform_component_invoke($component['type'], 'table', $component, $data);
if ($submission_output !== NULL) {
$component_headers[] = check_plain($component['name']);
$cell[] = $submission_output;
}
}
// MY Module Addition
$component_headers[] = "My Data";
$cell[] = $submission->data['mydata']['value'][0];
$rows[] = $cell;
unset($cell);
}
if (!empty($component_headers)) {
$header = array_merge($header, $component_headers);
}
if (count($rows) == 0) {
$rows[] = array(array('data' => t('There are no submissions for this form. <a href="!url">View this form</a>.', array('!url' => url('node/' . $node->nid))), 'colspan' => 4));
}
$output = '';
$output .= theme('webform_results_per_page', $total_count, $pager_count);
$output .= theme('table', $header, $rows);
return $output;
}
// Add custom submit handler to download form BEFORE webform one
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'webform_results_download_form') {
array_unshift($form['#submit'], 'mymodule_webform_results_download_form_submit');
}
}
// Add our data (fake components)
function mymodule_webform_results_download_form_submit(&$form, &$form_state) {
$form_state['values']['components']['mydata'] = 'mydata';
$form_state['values']['node']->webform['components']['mydata'] = array(
'nid' => $form_state['values']['node']->nid,
'cid' => 'mydata',
'type' => 'textfield',
'name' => "My Data",
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment