Advertisement
vtxyzzy

Shortcode for Visual Form Builder Survey

Mar 15th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.14 KB | None | 0 0
  1. add_shortcode('survey_results','vfb_survey_results');
  2. function vfb_survey_results($atts) {
  3.    /*
  4.    * This shortcode will tally the results of a Visual Form Builder form.
  5.    *
  6.     * Usage: [mam_survey_results form_id=n]
  7.     *     where n is the id number of the form to tally.
  8.     *
  9.     * Only fields of type 'checkbox', 'radio', or 'select' are totalled.
  10.     *
  11.     * It is a good idea to have an option like '-- Please Select --' as
  12.     * the default item in a 'select' field.  Otherwise, the first item
  13.     * is shown as selected if the user does not choose another one.
  14.     *
  15.    */
  16.    global $wpdb;
  17.    extract( shortcode_atts( array(
  18.          'form_id' => '0',
  19.          ),
  20.       $atts
  21.       )
  22.    );
  23.    if (!$form_id) {
  24.       return "<p>Missing form_id:  [survey_results form_id='form id']</p>";
  25.    }
  26.  
  27.    // Get the fields in the form in order of appearance
  28.    $sql = "
  29.    SELECT * FROM {$wpdb->prefix}vfb_pro_fields
  30.    WHERE form_id = $form_id
  31.    AND field_type IN ('checkbox', 'radio', 'select')
  32.    ORDER BY field_sequence
  33.    ";
  34.    $field_defs = $wpdb->get_results($sql);
  35.  
  36.    // Add (Not Selected) for radio and checkboxes because they will show
  37.    // an empty value if none are selected.
  38.    $not_selected = '(Not Selected)';
  39.    foreach ($field_defs as $field_def) {
  40.       $option_names = unserialize($field_def->field_options);
  41.       if ($field_def->field_type == 'checkbox' || $field_def->field_type == 'radio') {
  42.          $option_names[] = $not_selected;
  43.       }
  44.       $field_def->option_names = $option_names;
  45.    }
  46.  
  47.    // Get the responses
  48.    $sql = "
  49.    SELECT * FROM {$wpdb->prefix}vfb_pro_entries
  50.    WHERE form_id = $form_id
  51.    AND entry_approved = 1
  52.    ";
  53.    $survey_rows = $wpdb->get_results($sql);
  54.  
  55.    // Tabulate the results
  56.    $total_responses = 0;
  57.    foreach ($survey_rows as $survey_row) {
  58.       ++$total_responses;
  59.       $options = unserialize($survey_row->data);
  60.       foreach ($options as $option) {
  61.          $type = $option['type'];
  62.          if ($type == 'select' || $type == 'radio' || $type == 'checkbox') {
  63.             $id = $option['id'];
  64.             $name = $option['name'];
  65.             $value = $option['value'];
  66.             if ($type == 'checkbox' || $type == 'radio') {
  67.                if (empty($value)) {
  68.                   $value = $not_selected;
  69.                }
  70.             }
  71.             if ($type == 'checkbox') {
  72.                $values = explode(',', $value);
  73.             } else {
  74.                $values = array( $value );
  75.             }
  76.             $totals[$id]['name'] = $name;
  77.             foreach ($values as $onevalue) {
  78.                ++$totals[$id]['counts'][trim($onevalue)];
  79.             }
  80.          }
  81.       }
  82.    }
  83.  
  84.    // Format the results
  85.    ob_start(); ?>
  86.    <div class='vfb-survey-results vfbsr-form-<?php echo $form_id; ?>'>
  87.       <div class='vfbsr-total-responses-line'>
  88.          <span class='vfbsf-total-responses-label'>Total Responses:</span>
  89.          <span class='vfbsr-total-responses'><?php echo $total_responses; ?></span>
  90.       </div>
  91.  
  92.       <?php foreach ($field_defs as $field_def) {
  93.          $total = $totals[$field_def->field_id];
  94.          $counts = $total['counts'];
  95.          $name = $total['name'];
  96.       ?>
  97.       <div class='vfbsr-field'>
  98.          <span class='vfbsr-field-name'><?php echo $name; ?></span>
  99.          <ul class='vfbsr-option-counts'>
  100.          <?php
  101.          // Use field def option names so that we show only current options.
  102.          // Some may have been added, deleted, or rearranged after the survey
  103.          // was first deployed.
  104.          $option_names = $field_def->option_names;
  105.          foreach ($option_names as $option_name) {
  106.             $name = trim($option_name);
  107.             $count = $counts[$name];
  108.             if ($count) { ?>
  109.                <li>
  110.                   <span class='vfbsr-count'><?php echo $count; ?></span>
  111.                   &nbsp;<span class='vfbsr-option-name'><?php echo $option_name; ?></span>
  112.                </li>
  113.             <?php }
  114.          } ?>
  115.          </ul>
  116.       </div><!-- vfbsr-field -->
  117.       <?php } ?>
  118.    </div><!-- vfbsr-survey-results -->
  119.  
  120.    <?php $results = ob_get_clean();
  121.    return $results;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement