[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/webform/components/ -> grid.inc (source)

   1  <?php
   2  // $Id: grid.inc,v 1.14.2.11 2010/09/29 18:38:18 quicksketch Exp $
   3  
   4  /**
   5   * @file
   6   * Webform module grid component.
   7   */
   8  
   9  // Grid depends on functions provided by select.
  10  webform_component_include('select');
  11  
  12  /**
  13   * Implementation of _webform_defaults_component().
  14   */
  15  function _webform_defaults_grid() {
  16    return array(
  17      'name' => '',
  18      'form_key' => NULL,
  19      'email' => 1,
  20      'mandatory' => 0,
  21      'pid' => 0,
  22      'weight' => 0,
  23      'extra' => array(
  24        'options' => '',
  25        'questions' => '',
  26        'optrand' => 0,
  27        'qrand' => 0,
  28        'title_display' => 0,
  29        'custom_option_keys' => 0,
  30        'custom_question_keys' => 0,
  31        'description' => '',
  32      ),
  33    );
  34  }
  35  
  36  
  37  /**
  38   * Implementation of _webform_theme_component().
  39   */
  40  function _webform_theme_grid() {
  41    return array(
  42      'webform_grid' => array(
  43        'arguments' => array('grid_element' => NULL),
  44      ),
  45      'webform_display_grid' => array(
  46        'arguments' => array('element' => NULL),
  47      ),
  48    );
  49  }
  50  
  51  /**
  52   * Implementation of _webform_edit_component().
  53   */
  54  function _webform_edit_grid($component) {
  55    $form = array();
  56  
  57    if (module_exists('options_element')) {
  58      $form['options'] = array(
  59        '#type' => 'fieldset',
  60        '#title' => t('Options'),
  61        '#collapsible' => TRUE,
  62        '#description' => t('Options to select across the top. Usually these are ratings such as "poor" through "excellent" or "strongly disagree" through "strongly agree".'),
  63        '#attributes' => array('class' => 'webform-options-element'),
  64        '#element_validate' => array('_webform_edit_validate_options'),
  65      );
  66      $form['options']['options'] = array(
  67        '#type' => 'options',
  68        '#options' => _webform_select_options_from_text($component['extra']['options'], TRUE),
  69        '#optgroups' => FALSE,
  70        '#default_value' => FALSE,
  71        '#optgroups' => FALSE,
  72        '#key_type' => 'mixed',
  73        '#key_type_toggle' => t('Customize option keys (Advanced)'),
  74        '#key_type_toggled' => $component['extra']['custom_option_keys'],
  75      );
  76  
  77      $form['questions'] = array(
  78        '#type' => 'fieldset',
  79        '#title' => t('Questions'),
  80        '#collapsible' => TRUE,
  81        '#description' => t('Questions list down the side of the grid.'),
  82        '#attributes' => array('class' => 'webform-options-element'),
  83        '#element_validate' => array('_webform_edit_validate_options'),
  84      );
  85      $form['questions']['options'] = array(
  86        '#type' => 'options',
  87        '#options' => _webform_select_options_from_text($component['extra']['questions'], TRUE),
  88        '#optgroups' => FALSE,
  89        '#default_value' => FALSE,
  90        '#optgroups' => FALSE,
  91        '#key_type' => 'mixed',
  92        '#key_type_toggle' => t('Customize question keys (Advanced)'),
  93        '#key_type_toggled' => $component['extra']['custom_question_keys'],
  94      );
  95    }
  96    else {
  97      $form['extra']['options'] = array(
  98        '#type' => 'textarea',
  99        '#title' => t('Options'),
 100        '#default_value' => $component['extra']['options'],
 101        '#description' => t('Options to select across the top. One option per line. Key-value pairs must be entered separated by pipes. i.e. safe_key|Some readable option') . theme('webform_token_help'),
 102        '#cols' => 60,
 103        '#rows' => 5,
 104        '#weight' => -3,
 105        '#required' => TRUE,
 106        '#wysiwyg' => FALSE,
 107        '#element_validate' => array('_webform_edit_validate_select'),
 108      );
 109      $form['extra']['questions'] = array(
 110        '#type' => 'textarea',
 111        '#title' => t('Questions'),
 112        '#default_value' => $component['extra']['questions'],
 113        '#description' => t('Questions list down the side of the grid. One question per line. Key-value pairs must be entered separated by pipes. i.e safe_key|Some readable option') . theme('webform_token_help'),
 114        '#cols' => 60,
 115        '#rows' => 5,
 116        '#weight' => -2,
 117        '#required' => TRUE,
 118        '#wysiwyg' => FALSE,
 119        '#element_validate' => array('_webform_edit_validate_select'),
 120      );
 121    }
 122  
 123    $form['display']['optrand'] = array(
 124      '#type' => 'checkbox',
 125      '#title' => t('Randomize Options'),
 126      '#default_value' => $component['extra']['optrand'],
 127      '#description' => t('Randomizes the order of options on the top when they are displayed in the form.'),
 128      '#parents' => array('extra', 'optrand')
 129    );
 130    $form['display']['qrand'] = array(
 131      '#type' => 'checkbox',
 132      '#title' => t('Randomize Questions'),
 133      '#default_value' => $component['extra']['qrand'],
 134      '#description' => t('Randomize the order of the questions on the side when they are displayed in the form.'),
 135      '#parents' => array('extra', 'qrand')
 136    );
 137    return $form;
 138  }
 139  
 140  /**
 141   * Implementation of _webform_render_component().
 142   */
 143  function _webform_render_grid($component, $value = NULL, $filter = TRUE) {
 144    $element = array(
 145      '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
 146      '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : NULL,
 147      '#required' => $component['mandatory'],
 148      '#weight' => $component['weight'],
 149      '#theme' => 'webform_grid',
 150      '#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
 151      '#prefix' => '<div class="webform-component webform-component-' . $component['type'] . '" id="webform-component-' . $component['form_key'] . '">',
 152      '#suffix' => '</div>',
 153      '#pre_render' => array('webform_element_title_display'),
 154      '#webform_component' => $component,
 155    );
 156  
 157    $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
 158    $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
 159  
 160    if ($component['extra']['optrand']) {
 161      _webform_shuffle_options($options);
 162    }
 163  
 164    if ($component['extra']['qrand']) {
 165      _webform_shuffle_options($questions);
 166    }
 167  
 168    foreach ($questions as $key => $question) {
 169      if ($question != '') {
 170        $element[$key] = array(
 171          '#title' => $question,
 172          '#required' => $component['mandatory'],
 173          '#options' => $options,
 174          '#type' => 'radios',
 175          '#process' => array('expand_radios', 'webform_expand_select_ids'),
 176  
 177          // Webform handles validation manually.
 178          '#validated' => TRUE,
 179          '#webform_validated' => FALSE,
 180        );
 181      }
 182    }
 183  
 184    if (isset($value)) {
 185      foreach (element_children($element) as $key) {
 186        $element[$key]['#default_value'] = isset($value[$key]) ? $value[$key] : NULL;
 187      }
 188    }
 189  
 190    return $element;
 191  }
 192  
 193  /**
 194   * Implementation of _webform_display_component().
 195   */
 196  function _webform_display_grid($component, $value, $format = 'html') {
 197    $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
 198    $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
 199  
 200    $element = array(
 201      '#title' => $component['name'],
 202      '#weight' => $component['weight'],
 203      '#format' => $format,
 204      '#questions' => $questions,
 205      '#options' => $options,
 206      '#theme' => 'webform_display_grid',
 207      '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
 208      '#post_render' => array('webform_element_wrapper'),
 209      '#sorted' => TRUE,
 210      '#webform_component' => $component,
 211    );
 212  
 213    foreach ($questions as $key => $question) {
 214      if ($question != '') {
 215        $element[$question] = array(
 216          '#title' => $question,
 217          '#value' => isset($value[$key]) ? $value[$key] : NULL,
 218        );
 219      }
 220    }
 221  
 222    return $element;
 223  }
 224  
 225  /**
 226   * Format the text output for this component.
 227   */
 228  function theme_webform_display_grid($element) {
 229    $component = $element['#webform_component'];
 230    $format = $element['#format'];
 231  
 232    if ($format == 'html') {
 233      $rows = array();
 234      $header = array(array('data' => '', 'class' => 'webform-grid-question'));
 235      foreach ($element['#options'] as $option) {
 236        $header[] = array('data' => _webform_filter_xss($option), 'class' => 'checkbox webform-grid-option');
 237      }
 238      foreach (element_children($element) as $key) {
 239        $row = array();
 240        $row[] = array('data' => _webform_filter_xss($element[$key]['#title']), 'class' => 'webform-grid-question');
 241        foreach ($element['#options'] as $option_value => $option_label) {
 242          if ($option_value == $element[$key]['#value']) {
 243            $row[] = array('data' => '<strong>X</strong>', 'class' => 'checkbox webform-grid-option');
 244          }
 245          else {
 246            $row[] = array('data' => '&nbsp', 'class' => 'checkbox webform-grid-option');
 247          }
 248        }
 249        $rows[] = $row;
 250      }
 251  
 252      $option_count = count($header) - 1;
 253      $output = theme('table', $header, $rows, array('class' => 'webform-grid webform-grid-' . $option_count));
 254    }
 255    else {
 256      $items = array();
 257      foreach (element_children($element) as $key) {
 258        $items[] = ' - ' . $element[$key]['#title'] . ': ' . (isset($element['#options'][$element[$key]['#value']]) ? $element['#options'][$element[$key]['#value']] : '');
 259      }
 260      $output = implode("\n", $items);
 261    }
 262  
 263    return $output;
 264  }
 265  
 266  /**
 267   * Implementation of _webform_analysis_component().
 268   */
 269  function _webform_analysis_grid($component, $sids = array()) {
 270    // Generate the list of options and questions.
 271    $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
 272    $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
 273  
 274    // Generate a lookup table of results.
 275    $placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array();
 276    $sidfilter = count($sids) ? " AND sid in (" . implode(",", $placeholders) . ")" : "";
 277    $query = 'SELECT no, data, count(data) as datacount ' .
 278      ' FROM {webform_submitted_data} ' .
 279      ' WHERE nid = %d ' .
 280      ' AND cid = %d ' .
 281      " AND data != '' " . $sidfilter .
 282      ' GROUP BY no, data';
 283    $result = db_query($query, array_merge(array($component['nid'], $component['cid']), $sids));
 284    $counts = array();
 285    while ($data = db_fetch_object($result)) {
 286      $counts[$data->no][$data->data] = $data->datacount;
 287    }
 288  
 289    // Create an entire table to be put into the returned row.
 290    $rows = array();
 291    $header = array('');
 292  
 293    // Add options as a header row.
 294    foreach ($options as $option) {
 295      $header[] = _webform_filter_xss($option);
 296    }
 297  
 298    // Add questions as each row.
 299    foreach ($questions as $qkey => $question) {
 300      $row = array(_webform_filter_xss($question));
 301      foreach ($options as $okey => $option) {
 302        $row[] = !empty($counts[$qkey][$okey]) ? $counts[$qkey][$okey] : 0;
 303      }
 304      $rows[] = $row;
 305    }
 306    $output = theme('table', $header, $rows, array('class' => 'webform-grid'));
 307  
 308  
 309    return array(array(array('data' => $output, 'colspan' => 2)));
 310  }
 311  
 312  /**
 313   * Implementation of _webform_table_component().
 314   */
 315  function _webform_table_grid($component, $value) {
 316    $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
 317    $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
 318  
 319    $output = '';
 320    // Set the value as a single string.
 321    foreach ($questions as $key => $label) {
 322      if (isset($value[$key]) && isset($options[$value[$key]])) {
 323        $output .= _webform_filter_xss($label) . ': ' . _webform_filter_xss($options[$value[$key]]) . '<br />';
 324      }
 325    }
 326  
 327    return $output;
 328  }
 329  
 330  /**
 331   * Implementation of _webform_csv_headers_component().
 332   */
 333  function _webform_csv_headers_grid($component, $export_options) {
 334    $header = array();
 335    $header[0] = array('');
 336    $header[1] = array($component['name']);
 337    $items = _webform_select_options_from_text($component['extra']['questions'], TRUE);
 338    $count = 0;
 339    foreach ($items as $key => $item) {
 340      // Empty column per sub-field in main header.
 341      if ($count != 0) {
 342        $header[0][] = '';
 343        $header[1][] = '';
 344      }
 345      // The value for this option.
 346      $header[2][] = $item;
 347      $count++;
 348    }
 349  
 350    return $header;
 351  }
 352  
 353  /**
 354   * Implementation of _webform_csv_data_component().
 355   */
 356  function _webform_csv_data_grid($component, $export_options, $value) {
 357    $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
 358    $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
 359    $return = array();
 360    foreach ($questions as $key => $question) {
 361      if (isset($value[$key]) && isset($options[$value[$key]])) {
 362        $return[] = $export_options['select_keys'] ? $value[$key] : $options[$value[$key]];
 363      }
 364    }
 365    return $return;
 366  }
 367  
 368  function theme_webform_grid($element) {
 369    $rows = array();
 370    $header = array(array('data' => '', 'class' => 'webform-grid-question'));
 371    $first = TRUE;
 372    foreach (element_children($element) as $key) {
 373      $question_element = $element[$key];
 374  
 375      // Set the header for the table.
 376      if ($first) {
 377        foreach ($question_element['#options'] as $option) {
 378          $header[] = array('data' => _webform_filter_xss($option), 'class' => 'checkbox webform-grid-option');
 379        }
 380        $first = FALSE;
 381      }
 382  
 383      // Create a row with the question title.
 384      $row = array(array('data' => _webform_filter_xss($question_element['#title']), 'class' => 'webform-grid-question'));
 385  
 386      // Render each radio button in the row.
 387      $radios = expand_radios($question_element);
 388      foreach (element_children($radios) as $key) {
 389        unset($radios[$key]['#title']);
 390        $row[] = array('data' => drupal_render($radios[$key]), 'class' => 'checkbox webform-grid-option');
 391      }
 392      $rows[] = $row;
 393    }
 394  
 395    $option_count = count($header) - 1;
 396    return theme('form_element', $element, theme('table', $header, $rows, array('class' => 'webform-grid webform-grid-' . $option_count)));
 397  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7