Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_shortcode('survey_results','vfb_survey_results');
- function vfb_survey_results($atts) {
- /*
- * This shortcode will tally the results of a Visual Form Builder form.
- *
- * Usage: [mam_survey_results form_id=n]
- * where n is the id number of the form to tally.
- *
- * Only fields of type 'checkbox', 'radio', or 'select' are totalled.
- *
- * It is a good idea to have an option like '-- Please Select --' as
- * the default item in a 'select' field. Otherwise, the first item
- * is shown as selected if the user does not choose another one.
- *
- */
- global $wpdb;
- extract( shortcode_atts( array(
- 'form_id' => '0',
- ),
- $atts
- )
- );
- if (!$form_id) {
- return "<p>Missing form_id: [survey_results form_id='form id']</p>";
- }
- // Get the fields in the form in order of appearance
- $sql = "
- SELECT * FROM {$wpdb->prefix}vfb_pro_fields
- WHERE form_id = $form_id
- AND field_type IN ('checkbox', 'radio', 'select')
- ORDER BY field_sequence
- ";
- $field_defs = $wpdb->get_results($sql);
- // Add (Not Selected) for radio and checkboxes because they will show
- // an empty value if none are selected.
- $not_selected = '(Not Selected)';
- foreach ($field_defs as $field_def) {
- $option_names = unserialize($field_def->field_options);
- if ($field_def->field_type == 'checkbox' || $field_def->field_type == 'radio') {
- $option_names[] = $not_selected;
- }
- $field_def->option_names = $option_names;
- }
- // Get the responses
- $sql = "
- SELECT * FROM {$wpdb->prefix}vfb_pro_entries
- WHERE form_id = $form_id
- AND entry_approved = 1
- ";
- $survey_rows = $wpdb->get_results($sql);
- // Tabulate the results
- $total_responses = 0;
- foreach ($survey_rows as $survey_row) {
- ++$total_responses;
- $options = unserialize($survey_row->data);
- foreach ($options as $option) {
- $type = $option['type'];
- if ($type == 'select' || $type == 'radio' || $type == 'checkbox') {
- $id = $option['id'];
- $name = $option['name'];
- $value = $option['value'];
- if ($type == 'checkbox' || $type == 'radio') {
- if (empty($value)) {
- $value = $not_selected;
- }
- }
- if ($type == 'checkbox') {
- $values = explode(',', $value);
- } else {
- $values = array( $value );
- }
- $totals[$id]['name'] = $name;
- foreach ($values as $onevalue) {
- ++$totals[$id]['counts'][trim($onevalue)];
- }
- }
- }
- }
- // Format the results
- ob_start(); ?>
- <div class='vfb-survey-results vfbsr-form-<?php echo $form_id; ?>'>
- <div class='vfbsr-total-responses-line'>
- <span class='vfbsf-total-responses-label'>Total Responses:</span>
- <span class='vfbsr-total-responses'><?php echo $total_responses; ?></span>
- </div>
- <?php foreach ($field_defs as $field_def) {
- $total = $totals[$field_def->field_id];
- $counts = $total['counts'];
- $name = $total['name'];
- ?>
- <div class='vfbsr-field'>
- <span class='vfbsr-field-name'><?php echo $name; ?></span>
- <ul class='vfbsr-option-counts'>
- <?php
- // Use field def option names so that we show only current options.
- // Some may have been added, deleted, or rearranged after the survey
- // was first deployed.
- $option_names = $field_def->option_names;
- foreach ($option_names as $option_name) {
- $name = trim($option_name);
- $count = $counts[$name];
- if ($count) { ?>
- <li>
- <span class='vfbsr-count'><?php echo $count; ?></span>
- <span class='vfbsr-option-name'><?php echo $option_name; ?></span>
- </li>
- <?php }
- } ?>
- </ul>
- </div><!-- vfbsr-field -->
- <?php } ?>
- </div><!-- vfbsr-survey-results -->
- <?php $results = ob_get_clean();
- return $results;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement