Skip to content

Instantly share code, notes, and snippets.

@richardW8k
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardW8k/638ccb9b5dbf6ae36b1e to your computer and use it in GitHub Desktop.
Save richardW8k/638ccb9b5dbf6ae36b1e to your computer and use it in GitHub Desktop.
add_action( 'gform_pre_submission_217', 'count_likert_columns' );
function count_likert_columns( $form ) {
$survey_fields = GFCommon::get_fields_by_type( $form, array( 'survey' ) );
if ( ! empty( $survey_fields ) ) {
$count = array_fill( 1, 5, 0 );
foreach ( $survey_fields as $field ) {
$type = GFFormsModel::get_input_type( $field );
if ( 'likert' == $type ) {
if ( rgar( $field, 'gsurveyLikertEnableMultipleRows' ) && isset( $field['inputs'] ) && is_array( $field['inputs'] ) ) {
foreach ( $field['inputs'] as $input ) {
$input_name = 'input_' . str_replace( '.', '_', $input['id'] );
$value = rgpost( $input_name );
if ( ! empty( $value ) ) {
list( , $col_val ) = explode( ':', $value, 2 );
foreach ( $field['choices'] as $choice ) {
if ( $choice['value'] == $col_val ) {
++ $count[ $choice['score'] ];
}
}
}
}
} else {
$value = rgpost( 'input_' . $field['id'] );
if ( ! empty( $value ) ) {
foreach ( $field['choices'] as $choice ) {
if ( $choice['value'] == $value ) {
++ $count[ $choice['score'] ];
}
}
}
}
}
}
$_POST['input_3'] = $count[1];
$_POST['input_4'] = $count[2];
$_POST['input_5'] = $count[3];
$_POST['input_6'] = $count[4];
$_POST['input_7'] = $count[5];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment