[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7