'',
'form_key' => NULL,
'mandatory' => 0,
'pid' => 0,
'weight' => 0,
'extra' => array(
'options' => '',
'questions' => '',
'optrand' => 0,
'qrand' => 0,
'title_display' => 0,
'custom_option_keys' => 0,
'custom_question_keys' => 0,
'description' => '',
),
);
}
/**
* Implementation of _webform_theme_component().
*/
function _webform_theme_grid() {
return array(
'webform_grid' => array(
'render element' => 'element',
),
'webform_display_grid' => array(
'render element' => 'element',
),
);
}
/**
* Implementation of _webform_edit_component().
*/
function _webform_edit_grid($component) {
$form = array();
if (module_exists('options_element')) {
$form['options'] = array(
'#type' => 'fieldset',
'#title' => t('Options'),
'#collapsible' => TRUE,
'#description' => t('Options to select across the top. Usually these are ratings such as "poor" through "excellent" or "strongly disagree" through "strongly agree".'),
'#attributes' => array('class' => array('webform-options-element')),
'#element_validate' => array('_webform_edit_validate_options'),
);
$form['options']['options'] = array(
'#type' => 'options',
'#options' => _webform_select_options_from_text($component['extra']['options'], TRUE),
'#optgroups' => FALSE,
'#default_value' => FALSE,
'#optgroups' => FALSE,
'#key_type' => 'mixed',
'#key_type_toggle' => t('Customize option keys (Advanced)'),
'#key_type_toggled' => $component['extra']['custom_option_keys'],
);
$form['questions'] = array(
'#type' => 'fieldset',
'#title' => t('Questions'),
'#collapsible' => TRUE,
'#description' => t('Questions list down the side of the grid.'),
'#attributes' => array('class' => array('webform-options-element')),
'#element_validate' => array('_webform_edit_validate_options'),
);
$form['questions']['options'] = array(
'#type' => 'options',
'#options' => _webform_select_options_from_text($component['extra']['questions'], TRUE),
'#optgroups' => FALSE,
'#default_value' => FALSE,
'#optgroups' => FALSE,
'#key_type' => 'mixed',
'#key_type_toggle' => t('Customize question keys (Advanced)'),
'#key_type_toggled' => $component['extra']['custom_question_keys'],
);
}
else {
$form['extra']['options'] = array(
'#type' => 'textarea',
'#title' => t('Options'),
'#default_value' => $component['extra']['options'],
'#description' => t('Options to select across the top. One option per line. Key-value pairs MUST be specified as "safe_key|Some readable option". Use of only alphanumeric characters and underscores is recommended in keys.') . theme('webform_token_help'),
'#cols' => 60,
'#rows' => 5,
'#weight' => -3,
'#required' => TRUE,
'#wysiwyg' => FALSE,
'#element_validate' => array('_webform_edit_validate_select'),
);
$form['extra']['questions'] = array(
'#type' => 'textarea',
'#title' => t('Questions'),
'#default_value' => $component['extra']['questions'],
'#description' => t('Questions list down the side of the grid. One question per line. Key-value pairs MUST be specified as "safe_key|Some readable option". Use of only alphanumeric characters and underscores is recommended in keys.') . theme('webform_token_help'),
'#cols' => 60,
'#rows' => 5,
'#weight' => -2,
'#required' => TRUE,
'#wysiwyg' => FALSE,
'#element_validate' => array('_webform_edit_validate_select'),
);
}
$form['display']['optrand'] = array(
'#type' => 'checkbox',
'#title' => t('Randomize Options'),
'#default_value' => $component['extra']['optrand'],
'#description' => t('Randomizes the order of options on the top when they are displayed in the form.'),
'#parents' => array('extra', 'optrand')
);
$form['display']['qrand'] = array(
'#type' => 'checkbox',
'#title' => t('Randomize Questions'),
'#default_value' => $component['extra']['qrand'],
'#description' => t('Randomize the order of the questions on the side when they are displayed in the form.'),
'#parents' => array('extra', 'qrand')
);
return $form;
}
/**
* Implementation of _webform_render_component().
*/
function _webform_render_grid($component, $value = NULL, $filter = TRUE) {
$element = array(
'#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
'#required' => $component['mandatory'],
'#weight' => $component['weight'],
'#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
'#theme' => 'webform_grid',
'#theme_wrappers' => array('webform_element'),
'#webform_component' => $component,
);
$questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
$options = _webform_select_options_from_text($component['extra']['options'], TRUE);
if ($component['extra']['optrand']) {
_webform_shuffle_options($options);
}
if ($component['extra']['qrand']) {
_webform_shuffle_options($questions);
}
foreach ($questions as $key => $question) {
if ($question != '') {
$element[$key] = array(
'#title' => $question,
'#required' => $component['mandatory'],
'#options' => $options,
'#type' => 'radios',
'#process' => array('form_process_radios', 'webform_expand_select_ids'),
// Webform handles validation manually.
'#validated' => TRUE,
'#webform_validated' => FALSE,
);
}
}
if (isset($value)) {
foreach (element_children($element) as $key) {
$element[$key]['#default_value'] = isset($value[$key]) ? $value[$key] : NULL;
}
}
return $element;
}
/**
* Implementation of _webform_display_component().
*/
function _webform_display_grid($component, $value, $format = 'html') {
$questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
$options = _webform_select_options_from_text($component['extra']['options'], TRUE);
$element = array(
'#title' => $component['name'],
'#weight' => $component['weight'],
'#format' => $format,
'#questions' => $questions,
'#options' => $options,
'#theme' => 'webform_display_grid',
'#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
'#sorted' => TRUE,
'#webform_component' => $component,
);
foreach ($questions as $key => $question) {
if ($question !== '') {
$element[$question] = array(
'#title' => $question,
'#value' => isset($value[$key]) ? $value[$key] : NULL,
);
}
}
return $element;
}
/**
* Format the text output for this component.
*/
function theme_webform_display_grid($variables) {
$element = $variables['element'];
$component = $element['#webform_component'];
$format = $element['#format'];
if ($format == 'html') {
$rows = array();
$header = array(array('data' => '', 'class' => array('webform-grid-question')));
foreach ($element['#options'] as $option) {
$header[] = array('data' => _webform_filter_xss($option), 'class' => array('checkbox', 'webform-grid-option'));
}
foreach (element_children($element) as $key) {
$row = array();
$row[] = array('data' => _webform_filter_xss($element[$key]['#title']), 'class' => array('webform-grid-question'));
foreach ($element['#options'] as $option_value => $option_label) {
if (strcmp($option_value, $element[$key]['#value']) == 0) {
$row[] = array('data' => 'X', 'class' => array('checkbox', 'webform-grid-option'));
}
else {
$row[] = array('data' => ' ', 'class' => array('checkbox', 'webform-grid-option'));
}
}
$rows[] = $row;
}
$option_count = count($header) - 1;
$output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('webform-grid', 'webform-grid-' . $option_count))));
}
else {
$items = array();
foreach (element_children($element) as $key) {
$items[] = ' - ' . $element[$key]['#title'] . ': ' . $element[$key]['#value'];
}
$output = implode("\n", $items);
}
return $output;
}
/**
* Implementation of _webform_analysis_component().
*/
function _webform_analysis_grid($component, $sids = array()) {
// Generate the list of options and questions.
$options = _webform_select_options_from_text($component['extra']['options'], TRUE);
$questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
// Generate a lookup table of results.
$query = db_select('webform_submitted_data', 'wsd')
->fields('wsd', array('no', 'data'))
->condition('nid', $component['nid'])
->condition('cid', $component['cid'])
->condition('data', '', '<>')
->groupBy('no')
->groupBy('data');
$query->addExpression('COUNT(sid)', 'datacount');
if (count($sids)) {
$query->condition('sid', $sids, 'IN');
}
$result = $query->execute();
$counts = array();
foreach ($result as $data) {
$counts[$data->no][$data->data] = $data->datacount;
}
// Create an entire table to be put into the returned row.
$rows = array();
$header = array('');
// Add options as a header row.
foreach ($options as $option) {
$header[] = _webform_filter_xss($option);
}
// Add questions as each row.
foreach ($questions as $qkey => $question) {
$row = array(_webform_filter_xss($question));
foreach ($options as $okey => $option) {
$row[] = !empty($counts[$qkey][$okey]) ? $counts[$qkey][$okey] : 0;
}
$rows[] = $row;
}
$output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('webform-grid'))));
return array(array(array('data' => $output, 'colspan' => 2)));
}
/**
* Implementation of _webform_table_component().
*/
function _webform_table_grid($component, $value) {
$questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
$output = '';
// Set the value as a single string.
if (is_array($value)) {
foreach ($value as $item => $value) {
if ($value !== '') {
$output .= _webform_filter_xss($questions[$item]) . ': ' . _webform_filter_xss($value) . '
';
}
}
}
else {
$output = !isset($value[0]) ? '' : _webform_filter_xss($value[0]);
}
return $output;
}
/**
* Implementation of _webform_csv_headers_component().
*/
function _webform_csv_headers_grid($component, $export_options) {
$header = array();
$header[0] = array('');
$header[1] = array($component['name']);
$items = _webform_select_options_from_text($component['extra']['questions'], TRUE);
$count = 0;
foreach ($items as $key => $item) {
// Empty column per sub-field in main header.
if ($count != 0) {
$header[0][] = '';
$header[1][] = '';
}
// The value for this option.
$header[2][] = $item;
$count++;
}
return $header;
}
/**
* Implementation of _webform_csv_data_component().
*/
function _webform_csv_data_grid($component, $export_options, $value) {
$questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
$options = _webform_select_options_from_text($component['extra']['options'], TRUE);
$return = array();
foreach ($questions as $key => $question) {
if (isset($value[$key]) && isset($options[$value[$key]])) {
$return[] = $export_options['select_keys'] ? $value[$key] : $options[$value[$key]];
}
else {
$return[] = '';
}
}
return $return;
}
function theme_webform_grid($variables) {
$element = $variables['element'];
$rows = array();
$header = array(array('data' => '', 'class' => array('webform-grid-question')));
$first = TRUE;
foreach (element_children($element) as $key) {
$question_element = $element[$key];
// Set the header for the table.
if ($first) {
foreach ($question_element['#options'] as $option) {
$header[] = array('data' => _webform_filter_xss($option), 'class' => array('checkbox', 'webform-grid-option'));
}
$first = FALSE;
}
// Create a row with the question title.
$row = array(array('data' => _webform_filter_xss($question_element['#title']), 'class' => array('webform-grid-question')));
// Render each radio button in the row.
$radios = form_process_radios($question_element);
foreach (element_children($radios) as $key) {
unset($radios[$key]['#title']);
$row[] = array('data' => drupal_render($radios[$key]), 'class' => array('checkbox', 'webform-grid-option'));
}
$rows[] = $row;
}
$option_count = count($header) - 1;
return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('webform-grid', 'webform-grid-' . $option_count))));
}