ajstimson
12/14/2016 - 12:26 AM

LikertFilter.php

<?php
/**
 * Plugin Name:         Custom Settings for Gravity Form Likert Field Type
 * Plugin URI:          https://survey.teamberkana.com
 * Description:         Add Category, ID, and Scale Type settings for GF Survey Addon Likert fields
 * Version:             1.1
 * Author:              Andrew Stimson
 * License:             GPLv2 or later
 * License URI:         http://www.gnu.org/licenses/gpl-2.0.html
 */

/**
 * Actions and Filters
 */


// creates custom settings
add_action( 'gform_field_standard_settings', 'q_custom_settings', 10, 2 );
// sets field names for custom settings
add_action( 'gform_editor_js', 'editor_script' );
// creates tooltips for custom settings
add_filter( 'gform_tooltips', 'add_q_custom_tooltips' );
// adds custom settings entry values to the entry details page
add_filter( 'gform_entry_field_value', 'add_q_custom_entry_details' );


/**
 * Functions
 */

function q_custom_settings( $position, $form_id ) {

    //create settings on position 50 (right after Admin Label)
    if ( $position == 50 ) {
        // create text input fields for Category, Question ID, and Scale Type metadata
        ?>
        <li class="category_setting field_setting">
            <label for="field_admin_label">
                <?php _e("Category", "gravityforms"); ?>
                <?php gform_tooltip("form_field_category_value") ?>
            </label>
            <input type="text" id="field_category_value" onkeyup="SetFieldProperty('categorySetting', this.value);"/>
        </li>
        <li class="id_setting field_setting">
            <label for="field_admin_label">
                <?php _e("Question ID", "gravityforms"); ?>
                <?php gform_tooltip("form_field_id_value") ?>
            </label>
            <input type="text" id="field_id_value" onkeyup="SetFieldProperty('idSetting', this.value);"/>
        </li>
        <li class="scale_type_setting field_setting">
            <label for="field_admin_label">
                <?php _e("Scale Type", "gravityforms"); ?>
                <?php gform_tooltip("form_field_scale_type_value") ?>
            </label>
            <select id="field_scale_type_value" onchange="SetFieldProperty('scaleTypeSetting', this.value);">
                <option value="importance">Importance</option>
                <option value="effectiveness">Effectiveness</option>
                <option value="truth">Truth</option>
                <option value="Generic">Generic (blank)</option>
            </select>
        </li>
        <?php
    }
}

//Action to inject supporting script to the form editor page

function editor_script(){
    ?>
    <script type='text/javascript'>
        //adding setting to fields of type "radio"
        fieldSettings["likert"] += ", .category_setting, .id_setting, .scale_type_setting";
        console.log(fieldSettings);

        //binding to the load field settings event to initialize the text inputs
        jQuery(document).bind("gform_load_field_settings", function(event, field, form){
            jQuery("#field_category_value:text").val(field["categorySetting"]);
            jQuery("#field_id_value:text").val(field["idSetting"]);
            jQuery("#field_scale_type_value").val(field["scaleTypeSetting"]);
        });
    </script>
    <?php
}

//Filter to add a new tooltip

function add_q_custom_tooltips( $tooltips ) {
   $tooltips['form_field_category_value'] = "<h6>Category</h6>Examples include Leadership, Culture, Communication";
   $tooltips['form_field_id_value'] = "<h6>Question ID</h6>Helps identify this question's pair";
   $tooltips['form_field_scale_type_value'] = "<h6>Scale Type</h6>Separates survey results by importance, effectiveness, truth, or default(blank) scales";
   return $tooltips;
}


/**
 * Add custom settings fields to the Likert Survey field entry view.
 *
 * @access public
 *
 * @param string $value The current entry value to be filtered.
 * @param array  $field The field from which the entry value was submitted.
 * @param array  $entry The current entry.
 * @param array  $form  The form from which the entry value was submitted.
 *
 * @return string
 */

function add_q_custom_entry_details( $value, $field, $entry, $form ){
    // If this is not a Likert Survey field, return the standard value.
    if ( 'likert' !== $field->get_input_type() ) {
        return $value;
    } 
    // Open the unordered list for the custom settings fields.
    $custom_fields = '<ul>';
    // Display the category field setting.
    $custom_fields .= '<li>Category: ' . rgobj( $field, 'categorySetting' )  . '</li>';
    // Display the ID field setting.
    $custom_fields .= '<li>ID: ' . rgobj( $field, 'idSetting' ) . '</li>';
    // Display the scale type field setting.
    $custom_fields .= '<li>Scale Type: ' . rgobj( $field, 'scaleTypeSetting' ) . '</li>';
    // Close the unordered list.
    $custom_fields .= '</ul>';
    // Add custom settings fields to the return value
    $value = $custom_fields . $value;
    return $value;
}