GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-survey.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-survey.php
4  * @package GravityView
5  * @subpackage includes\fields
6  */
7 
9 
10  var $name = 'survey';
11 
12  var $_gf_field_class_name = 'GF_Field_Survey';
13 
14  var $is_searchable = false;
15 
16  var $group = 'advanced';
17 
18  var $icon = 'dashicons-forms';
19 
20  public function __construct() {
21  $this->label = esc_html__( 'Survey', 'gk-gravityview' );
22 
23  add_action( 'gravityview/template/field/survey/rating/before', array( __CLASS__, 'output_frontend_css' ) );
24 
25  parent::__construct();
26  }
27 
28  /**
29  * Returns the score for a choice at $value
30  *
31  * A sister method to {@see RGFormsModel::get_choice_text}
32  *
33  * @since 2.11
34  *
35  * @param GF_Field_Likert $field
36  * @param string|array $value
37  * @param int|string $input_id ID of the field or input (for example, 7.3 or 7)
38  *
39  * @return mixed|string
40  */
41  public static function get_choice_score( $field, $value, $input_id = 0 ) {
42 
43  if ( ! $field->gsurveyLikertEnableScoring ) {
44  return '';
45  }
46 
47  if ( ! is_array( $field->choices ) ) {
48  return $value;
49  }
50 
51  foreach ( $field->choices as $choice ) {
52  if ( is_array( $value ) && RGFormsModel::choice_value_match( $field, $choice, $value[ $input_id ] ) ) {
53  return $choice['score'];
54  } else if ( ! is_array( $value ) && RGFormsModel::choice_value_match( $field, $choice, $value ) ) {
55  return $choice['score'];
56  }
57  }
58 
59  return is_array( $value ) ? '' : $value;
60  }
61 
62  public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
63 
64  unset( $field_options['search_filter'] );
65 
66  if ( 'edit' === $context ) {
67  return $field_options;
68  }
69 
70  $field = \GV\GF_Field::by_id( \GV\GF_Form::by_id( $form_id ), $field_id );
72  $add_options = array();
73 
74  $glue = apply_filters( 'gravityview/template/field/survey/glue', '; ' );
75  $multiple_rows_suffix = sprintf( _x( ' (separated by %s)', 'text added to a label if multiple rows are enabled for the field)', 'gk-gravityview' ), esc_html( trim( $glue ) ) );
76 
77  if ( 'likert' === $field->field->inputType ) {
78 
79  $show_suffix = $input_id || empty( $field->field->gsurveyLikertEnableMultipleRows );
80 
81  $likert_display_options = array(
82  'default' => __( 'A table (default Gravity Forms formatting)', 'gk-gravityview' ),
83  'text' => __( 'Text value of the selected choice', 'gk-gravityview' ) . ( $show_suffix ? '' : $multiple_rows_suffix ),
84  );
85 
86  if( $field->field->gsurveyLikertEnableScoring ) {
87  $likert_display_options['score'] = __( 'Score value of the selected choice', 'gk-gravityview' ) . ( $show_suffix ? '' : $multiple_rows_suffix );
88  }
89 
90  // Maintain for back-compatibility
91  $add_options['score'] = array(
92  'type' => 'hidden',
93  'value' => '',
94  'group' => 'display',
95  );
96 
97  $add_options['choice_display'] = array(
98  'type' => 'radio',
99  'label' => __( 'What should be displayed:', 'gk-gravityview' ),
100  'options' => $likert_display_options,
101  'desc' => '',
102  'group' => 'display',
103  'class' => 'block',
104  'value' => 'default',
105  'merge_tags' => false,
106  );
107  }
108 
109  if( 'checkbox' === $field->field->inputType && $input_id ) {
110  $field_options['choice_display'] = array(
111  'type' => 'radio',
112  'class' => 'vertical',
113  'label' => __( 'What should be displayed:', 'gk-gravityview' ),
114  'value' => 'tick',
115  'desc' => '',
116  'choices' => array(
117  'tick' => __( 'A check mark, if the input is checked', 'gk-gravityview' ),
118  'text' => __( 'Text value of the selected choice', 'gk-gravityview' ),
119  ),
120  'group' => 'display',
121  'priority' => 100,
122  );
123  }
124 
125  if ( 'rating' === $field->field->inputType ) {
126  $field_options['choice_display'] = array(
127  'type' => 'radio',
128  'class' => 'vertical',
129  'label' => __( 'What should be displayed:', 'gk-gravityview' ),
130  'value' => 'default',
131  'desc' => '',
132  'choices' => array(
133  'default' => __( 'Text value of the selected choice', 'gk-gravityview' ),
134  'stars' => __( 'Stars (default Gravity Forms formatting)', 'gk-gravityview' ),
135  ),
136  'group' => 'display',
137  'priority' => 100,
138  );
139  }
140 
141  return $add_options + $field_options;
142  }
143 
144 
145  /**
146  * Output CSS for star ratings.
147  *
148  * @since 2.16
149  *
150  * @return void
151  */
152  static function output_frontend_css() {
153 
154  static $did_output;
155 
156  // Only output once.
157  if ( $did_output ) {
158  return;
159  }
160 
161  $star0 = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="-1107 844.2 51.6 51.6"><style type="text/css">.st0{fill:#EEEEEE;}.st1{fill:#CCCCCC;}</style><circle class="st0" cx="-1081.2" cy="870" r="17.9"/><path class="st1" d="M-1081.2 895.8c-14.2 0-25.8-11.5-25.8-25.8s11.6-25.8 25.8-25.8c14.2 0 25.8 11.6 25.8 25.8S-1066.9 895.8-1081.2 895.8zM-1081.2 849.2c-11.5 0-20.8 9.3-20.8 20.8s9.3 20.8 20.8 20.8 20.8-9.3 20.8-20.8S-1069.7 849.2-1081.2 849.2z"/><path class="st1" d="M-1076.4 871.8l4.8-4.6 -6.6-1 -3-6 -3 6 -6.6 1 4.8 4.6 -1.1 6.6 5.9-3.1 5.9 3.1L-1076.4 871.8zM-1068.2 866.2c0 0.2-0.1 0.5-0.4 0.8l-5.7 5.5 1.3 7.8c0 0.1 0 0.2 0 0.3 0 0.5-0.2 0.8-0.6 0.8 -0.2 0-0.4-0.1-0.6-0.2l-7-3.7 -7 3.7c-0.2 0.1-0.4 0.2-0.6 0.2 -0.2 0-0.4-0.1-0.5-0.2s-0.2-0.3-0.2-0.6c0-0.1 0-0.2 0-0.3l1.3-7.8 -5.7-5.5c-0.3-0.3-0.4-0.5-0.4-0.8 0-0.4 0.3-0.6 0.9-0.7l7.8-1.1 3.5-7.1c0.2-0.4 0.5-0.6 0.8-0.6 0.3 0 0.6 0.2 0.8 0.6l3.5 7.1 7.8 1.1C-1068.5 865.6-1068.2 865.8-1068.2 866.2L-1068.2 866.2z"/></svg>';
162  $star1 = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="-1107 844.2 51.6 51.6"><style type="text/css">.st0{fill:#EEEEEE;}.st1{fill:#CCCCCC;}.st2{fill:#FF9800;}</style><circle class="st0" cx="-1081.2" cy="870" r="17.9"/><path class="st1" d="M-1081.2 895.8c-14.2 0-25.8-11.5-25.8-25.8s11.6-25.8 25.8-25.8c14.2 0 25.8 11.6 25.8 25.8S-1066.9 895.8-1081.2 895.8zM-1081.2 849.2c-11.5 0-20.8 9.3-20.8 20.8s9.3 20.8 20.8 20.8 20.8-9.3 20.8-20.8S-1069.7 849.2-1081.2 849.2z"/><path class="st2" d="M-1068.2 866.3c0 0.2-0.1 0.5-0.4 0.8l-5.7 5.5 1.3 7.8c0 0.1 0 0.2 0 0.3 0 0.2-0.1 0.4-0.2 0.6 -0.1 0.2-0.3 0.2-0.5 0.2 -0.2 0-0.4-0.1-0.6-0.2l-7-3.7 -7 3.7c-0.2 0.1-0.4 0.2-0.6 0.2 -0.2 0-0.4-0.1-0.5-0.2 -0.1-0.2-0.2-0.3-0.2-0.6 0-0.1 0-0.2 0-0.3l1.3-7.8 -5.7-5.5c-0.3-0.3-0.4-0.5-0.4-0.8 0-0.4 0.3-0.6 0.9-0.7l7.8-1.1 3.5-7.1c0.2-0.4 0.5-0.6 0.8-0.6 0.3 0 0.6 0.2 0.8 0.6l3.5 7.1 7.8 1.1C-1068.5 865.7-1068.2 865.9-1068.2 866.3L-1068.2 866.3z"/></svg>';
163 
164  ?>
165  <style>
166  .gv-field-survey-star-filled,
167  .gv-field-survey-star-empty {
168  width: 18px;
169  height: 18px;
170  display: inline-block;
171  background: transparent url( 'data:image/svg+xml;base64,<?php echo base64_encode( $star0 ); ?>') left top no-repeat;
172  background-size: contain;
173  }
174  .gv-field-survey-star-filled {
175  background-image: url( 'data:image/svg+xml;base64,<?php echo base64_encode( $star1 ); ?>');
176  }
177  .gv-field-survey-screen-reader-text {
178  border: 0;
179  clip: rect(0 0 0 0);
180  clip-path: inset(50%);
181  height: 1px;
182  margin: -1px;
183  overflow: hidden;
184  padding: 0;
185  position: absolute;
186  width: 1px;
187  white-space: nowrap;
188  }
189  .gv-field-survey-screen-reader-text.focusable {
190  .gv-field-survey-screen-reader-text:active,
191  .gv-field-survey-screen-reader-text:focus {
192  clip: auto;
193  clip-path: none;
194  height: auto;
195  margin: 0;
196  overflow: visible;
197  position: static;
198  width: auto;
199  white-space: inherit;
200  }
201  }
202  </style>
203  <?php
204 
205  $did_output = true;
206  }
207 }
208 
Modify field settings by extending this class.
gravityview_get_input_id_from_id( $field_id='')
Very commonly needed: get the # of the input based on a full field ID.
static by_id( $form, $field_id)
Get a by and Field ID.
field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id)
static get_choice_score( $field, $value, $input_id=0)
Returns the score for a choice at $value.
if(empty( $created_by)) $form_id
static output_frontend_css()
Output CSS for star ratings.
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
Definition: time.php:22