Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes reversing issue #2157

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions includes/fields/class-gravityview-field-survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,36 @@ public static function get_choice_score( $field, $value, $input_id = 0 ) {
return is_array( $value ) ? '' : $value;
}

/**
* Get field choices directly from the database (To avoid issues where choices are reversed by the survey plugin).
omarkasem marked this conversation as resolved.
Show resolved Hide resolved
*
* @since TBD
*
* @param int $form_id The ID of the form.
* @param int $field_id The ID of the field.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@omarkasem, please align param descriptions:

* @param int $form_id  The ID of the form.
* @param int $field_id The ID of the field.

*
* @return array The choices for the field.
*/
public static function get_field_choices($form_id, $field_id) {
global $wpdb;
$table_name = GFFormsModel::get_meta_table_name();
$form_row = $wpdb->get_row( $wpdb->prepare( "SELECT display_meta FROM {$table_name} WHERE form_id=%d", $form_id ), ARRAY_A );
$form_meta = GFFormsModel::unserialize( rgar( $form_row, 'display_meta' ) );
omarkasem marked this conversation as resolved.
Show resolved Hide resolved

$field_choices = [];
if ( $form_meta ) {
omarkasem marked this conversation as resolved.
Show resolved Hide resolved
foreach ( $form_meta['fields'] as $form_field ) {
if ( $form_field['id'] == $field_id ) {
$field_choices = $form_field['choices'];
break;
}
}
}

return $field_choices;
}


public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {

unset( $field_options['search_filter'] );
Expand Down
8 changes: 2 additions & 6 deletions templates/fields/field-survey-html.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
$field = $gravityview->field;
$display_value = $gravityview->display_value;
$input_id = gravityview_get_input_id_from_id( $field->ID );
$form_id = $gravityview->view->form->ID;

// Used in filters below.
$return_true = function () {
Expand Down Expand Up @@ -142,12 +143,7 @@
return;
}

$choices = $field->field->choices;

// If the choices are reversed, reverse them back.
if ( ! empty( $choices ) && $choices[0]['text'] === 'Excellent' ) {
$choices = array_reverse( $choices );
}
$choices = GravityView_Field_Survey::get_field_choices( $form_id, $field->ID );

$choice_values = wp_list_pluck( $choices, 'value', $gravityview->value );
$starred_index = array_search( $gravityview->value, $choice_values );
Expand Down