-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclass-gf-survey.php
1158 lines (982 loc) · 38.6 KB
/
class-gf-survey.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
//------------------------------------------
// don't load directly
if ( ! defined( 'ABSPATH' ) ) {
die();
}
GFForms::include_addon_framework();
class GFSurvey extends GFAddOn {
protected $_version = GF_SURVEY_VERSION;
protected $_min_gravityforms_version = '2.0';
protected $_slug = 'gravityformssurvey';
protected $_path = 'gravityformssurvey/survey.php';
protected $_full_path = __FILE__;
protected $_url = 'http://www.gravityforms.com';
protected $_title = 'Gravity Forms Survey Add-On';
protected $_short_title = 'Survey';
protected $_enable_rg_autoupgrade = true;
/**
* Whether this add-on has access to the Gravity Forms settings renderer.
*
* @since 3.5
*
* @var bool
*/
protected $_has_settings_renderer;
/**
* Members plugin integration
*/
protected $_capabilities = array( 'gravityforms_survey', 'gravityforms_survey_uninstall', 'gravityforms_survey_results' );
/**
* Permissions
*/
protected $_capabilities_settings_page = 'gravityforms_survey';
protected $_capabilities_form_settings = 'gravityforms_survey';
protected $_capabilities_uninstall = 'gravityforms_survey_uninstall';
private static $_instance = null;
/**
* Get an instance of this class.
*
* @return GFSurvey
*/
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new GFSurvey();
}
return self::$_instance;
}
private function __clone() {
} /* do nothing */
/**
* Handles anything which requires early initialization.
*/
public function pre_init() {
parent::pre_init();
if ( $this->is_gravityforms_supported() && class_exists( 'GF_Field' ) ) {
require_once( 'includes/class-gf-field-survey.php' );
require_once( 'includes/class-gf-field-likert.php' );
require_once( 'includes/class-gf-field-rank.php' );
require_once( 'includes/class-gf-field-rating.php' );
add_filter( 'gform_export_field_value', array( $this, 'export_field_value' ), 10, 4 );
}
}
/**
* Handles hooks and loading of language files.
*/
public function init() {
$this->_has_settings_renderer = $this->is_gravityforms_supported( '2.5-beta' );
// Integration with the feed add-ons as of GF 1.9.15.12; for add-ons which don't override get_field_value().
add_filter( 'gform_addon_field_value', array( $this, 'addon_field_value' ), 10, 5 );
// AWeber 2.3 and newer use the gform_addon_field_value hook, only use the gform_aweber_field_value hook with older versions.
if ( defined( 'GF_AWEBER_VERSION' ) && version_compare( 'GF_AWEBER_VERSION', '2.3', '<' ) ) {
add_filter( 'gform_aweber_field_value', array( $this, 'legacy_addon_field_value' ), 10, 4 );
}
// Campaign Monitor Add-On integration
add_filter( 'gform_campaignmonitor_field_value', array( $this, 'legacy_addon_field_value' ), 10, 4 );
// Mailchimp Add-On integration
add_filter( 'gform_mailchimp_field_value', array( $this, 'legacy_addon_field_value' ), 10, 4 );
// Zapier Add-On integration
add_filter( 'gform_zapier_field_value', array( $this, 'zapier_field_value' ), 10, 4 );
// merge tags
add_filter( 'gform_replace_merge_tags', array( $this, 'replace_merge_tags' ), 10, 7 );
// add a special class to likert fields so we can identify them later
add_action( 'gform_field_css_class', array( $this, 'add_custom_class' ), 10, 3 );
// display survey results on entry detail
add_filter( 'gform_entry_field_value', array( $this, 'entry_field_value' ), 10, 4 );
// conditional logic filters
add_filter( 'gform_entry_meta_conditional_logic_confirmations', array( $this, 'conditional_logic_filters' ), 10, 3 );
add_filter( 'gform_entry_meta_conditional_logic_notifications', array( $this, 'conditional_logic_filters' ), 10, 3 );
parent::init();
}
/**
* Initialize the admin specific hooks.
*/
public function init_admin() {
// form editor
add_action( 'gform_field_standard_settings', array( $this, 'survey_field_settings' ), 10, 2 );
add_filter( 'gform_tooltips', array( $this, 'add_survey_tooltips' ) );
// merge tags
add_filter( 'gform_admin_pre_render', array( $this, 'add_merge_tags' ) );
// display results on entry list
add_filter( 'gform_entries_field_value', array( $this, 'export_field_value' ), 10, 4 );
// declare arrays on form import
add_filter( 'gform_import_form_xml_options', array( $this, 'import_file_options' ) );
// contacts
add_filter( 'gform_contacts_tabs_contact_detail', array( $this, 'add_tab_to_contact_detail' ), 10, 2 );
add_action( 'gform_contacts_tab_survey', array( $this, 'contacts_tab' ) );
parent::init_admin();
}
/**
* The Survey add-on does not support logging.
*
* @param array $plugins The plugins which support logging.
*
* @return array
*/
public function set_logging_supported( $plugins ) {
return $plugins;
}
// # SCRIPTS & STYLES -----------------------------------------------------------------------------------------------
/**
* Return the scripts which should be enqueued.
*
* @return array
*/
public function scripts() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$gsurvey_js_deps = array( 'jquery', 'jquery-ui-sortable' );
if ( wp_is_mobile() ) {
$gsurvey_js_deps[] = 'jquery-touch-punch';
}
$scripts = array(
array(
'handle' => 'gsurvey_form_editor_js',
'src' => $this->get_base_url() . "/js/gsurvey_form_editor{$min}.js",
'version' => $this->_version,
'deps' => array( 'jquery' ),
'callback' => array( $this, 'localize_scripts' ),
'enqueue' => array(
array( 'admin_page' => array( 'form_editor' ) ),
),
),
array(
'handle' => 'gsurvey_js',
'src' => $this->get_base_url() . "/js/gsurvey{$min}.js",
'version' => $this->_version,
'deps' => $gsurvey_js_deps,
'enqueue' => array(
array( 'admin_page' => array( 'form_editor', 'results', 'entry_view', 'entry_detail', 'entry_edit' ) ),
array( 'field_types' => array( 'survey' ) ),
),
),
);
return array_merge( parent::scripts(), $scripts );
}
/**
* Return the stylesheets which should be enqueued.
*
* @return array
*/
public function styles() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$styles = array(
array(
'handle' => 'gsurvey_form_editor_css',
'src' => $this->get_base_url() . "/css/gsurvey_form_editor{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array( 'admin_page' => array( 'form_editor' ) ),
),
),
array(
'handle' => 'gsurvey_css',
'src' => $this->get_base_url() . "/css/gsurvey{$min}.css",
'version' => $this->_version,
'media' => 'screen',
'enqueue' => array(
array( 'admin_page' => array( 'form_editor', 'results', 'entry_view', 'entry_detail', 'entry_edit' ) ),
array(
'field_types' => array( 'survey' )
),
),
),
);
return array_merge( parent::styles(), $styles );
}
/**
* Localize the strings used by the scripts.
*/
public function localize_scripts() {
// Get current page protocol
$protocol = isset( $_SERVER['HTTPS'] ) ? 'https://' : 'http://';
// Output admin-ajax.php URL with same protocol as current page
$params = array(
'ajaxurl' => admin_url( 'admin-ajax.php', $protocol ),
'imagesUrl' => $this->get_base_url() . '/images',
'strings' => array(
'untitledSurveyField' => wp_strip_all_tags( __( 'Untitled Survey Field', 'gravityformssurvey' ) ),
),
);
wp_localize_script( 'gsurvey_form_editor_js', 'gsurveyVars', $params );
//localize strings for the js file
$strings = array(
'isLegacy' => ! $this->_has_settings_renderer ? 'true' : 'false',
'firstChoice' => wp_strip_all_tags( __( 'First row', 'gravityformssurvey' ) ),
'secondChoice' => wp_strip_all_tags( __( 'Second row', 'gravityformssurvey' ) ),
'thirdChoice' => wp_strip_all_tags( __( 'Third row', 'gravityformssurvey' ) ),
'fourthChoice' => wp_strip_all_tags( __( 'Fourth row', 'gravityformssurvey' ) ),
'fifthChoice' => wp_strip_all_tags( __( 'Fifth row', 'gravityformssurvey' ) ),
'dragToReOrder' => wp_strip_all_tags( __( 'Drag to re-order', 'gravityformssurvey' ) ),
'addAnotherRow' => wp_strip_all_tags( __( 'Add another row', 'gravityformssurvey' ) ),
'removeThisRow' => wp_strip_all_tags( __( 'Remove this row', 'gravityformssurvey' ) ),
'addAnotherColumn' => wp_strip_all_tags( __( 'Add another column', 'gravityformssurvey' ) ),
'removeThisColumn' => wp_strip_all_tags( __( 'Remove this column', 'gravityformssurvey' ) ),
'columnLabel1' => wp_strip_all_tags( __( 'Strongly disagree', 'gravityformssurvey' ) ),
'columnLabel2' => wp_strip_all_tags( __( 'Disagree', 'gravityformssurvey' ) ),
'columnLabel3' => wp_strip_all_tags( __( 'Neutral', 'gravityformssurvey' ) ),
'columnLabel4' => wp_strip_all_tags( __( 'Agree', 'gravityformssurvey' ) ),
'columnLabel5' => wp_strip_all_tags( __( 'Strongly agree', 'gravityformssurvey' ) ),
);
wp_localize_script( 'gsurvey_form_editor_js', 'gsurveyLikertStrings', $strings );
//localize strings for the rank field
$rank_strings = array(
'firstChoice' => wp_strip_all_tags( __( 'First Choice', 'gravityformssurvey' ) ),
'secondChoice' => wp_strip_all_tags( __( 'Second Choice', 'gravityformssurvey' ) ),
'thirdChoice' => wp_strip_all_tags( __( 'Third Choice', 'gravityformssurvey' ) ),
'fourthChoice' => wp_strip_all_tags( __( 'Fourth Choice', 'gravityformssurvey' ) ),
'fifthChoice' => wp_strip_all_tags( __( 'Fifth Choice', 'gravityformssurvey' ) ),
);
wp_localize_script( 'gsurvey_form_editor_js', 'gsurveyRankStrings', $rank_strings );
//localize strings for the ratings field
$rating_strings = array(
'firstChoice' => wp_strip_all_tags( __( 'Terrible', 'gravityformssurvey' ) ),
'secondChoice' => wp_strip_all_tags( __( 'Not so great', 'gravityformssurvey' ) ),
'thirdChoice' => wp_strip_all_tags( __( 'Neutral', 'gravityformssurvey' ) ),
'fourthChoice' => wp_strip_all_tags( __( 'Pretty good', 'gravityformssurvey' ) ),
'fifthChoice' => wp_strip_all_tags( __( 'Excellent', 'gravityformssurvey' ) ),
);
wp_localize_script( 'gsurvey_form_editor_js', 'gsurveyRatingStrings', $rating_strings );
}
public function localize_results_scripts() {
$filter_fields = rgget( 'f' );
$filter_types = rgget( 't' );
$filter_operators = rgget( 'o' );
$filter_values = rgget( 'v' );
// Get current page protocol
$protocol = isset( $_SERVER['HTTPS'] ) ? 'https://' : 'http://';
// Output admin-ajax.php URL with same protocol as current page
$vars = array(
'ajaxurl' => admin_url( 'admin-ajax.php', $protocol ),
'imagesUrl' => $this->get_base_url() . '/images',
'filterFields' => $filter_fields,
'filterTypes' => $filter_types,
'filterOperators' => $filter_operators,
'filterValues' => $filter_values,
);
wp_localize_script( 'gsurvey_results_js', 'gresultsVars', $vars );
$strings = array(
'noFilters' => wp_strip_all_tags( __( 'No filters', 'gravityformspolls' ) ),
'addFieldFilter' => wp_strip_all_tags( __( 'Add a field filter', 'gravityformspolls' ) ),
'removeFieldFilter' => wp_strip_all_tags( __( 'Remove a field filter', 'gravityformspolls' ) ),
'ajaxError' => wp_strip_all_tags( __( 'Error retrieving results. Please contact support.', 'gravityformspolls' ) ),
);
wp_localize_script( 'gsurvey_results_js', 'gresultsStrings', $strings );
}
// # RESULTS & SCORING ----------------------------------------------------------------------------------------------
/**
* Configure the survey results page.
*
* @return array
*/
public function get_results_page_config() {
return array(
'title' => esc_html__( 'Survey Results', 'gravityformssurvey' ),
'capabilities' => array( 'gravityforms_survey_results' ),
'callbacks' => array(
'fields' => array( $this, 'results_fields' ),
'filters' => array( $this, 'results_filters' ),
),
);
}
/**
* Remove the score from the results page filters if scoring is not enabled on a Likert field.
*
* @param array $filters
* @param array $form
*
* @return array
*/
public function results_filters( $filters, $form ) {
$fields = $form['fields'];
foreach ( $fields as $field ) {
if ( $field->get_input_type() == 'likert' && $field->gsurveyLikertEnableScoring ) {
return $filters;
}
}
foreach ( $filters as $key => $filter ) {
if ( $filter['key'] == 'gsurvey_score' ) {
unset( $filters[ $key ] );
}
}
return $filters;
}
/**
* Get all the survey fields for the current form.
*
* @param array $form The current form object.
*
* @return GF_Field[]
*/
public function results_fields( $form ) {
return GFAPI::get_fields_by_type( $form, array( 'survey' ) );
}
/**
* Helper to check if scoring is enabled on at least one of the forms Likert fields.
*
* @param array $form The current form object.
*
* @return bool
*/
private function scoring_enabled( $form ) {
$survey_fields = $this->results_fields( $form );
foreach ( $survey_fields as $field ) {
if ( $field->get_input_type() == 'likert' && $field->gsurveyLikertEnableScoring ) {
return true;
}
}
return false;
}
/**
* Returns the total score of all likert fields with scoring enabled.
*
* @param array $form The current form object.
* @param array $entry The current entry object.
*
* @return float|int
*/
public function get_survey_score( $form, $entry ) {
$survey_fields = $this->results_fields( $form );
$score = 0;
foreach ( $survey_fields as $field ) {
if ( $field->get_input_type() == 'likert' && $field->gsurveyLikertEnableScoring ) {
$score += $this->get_field_score( $field, $entry );
}
}
return $score;
}
/**
* Returns the total score of all likert fields with scoring enabled.
*
* @param array $form The current form object.
* @param array $entry The current entry object.
*
* @return float|int
*/
public function get_total_score( $form, $entry ) {
return $this->get_survey_score( $form, $entry );
}
/**
* Returns the score for the specified field.
*
* Called statically by GFResults.
*
* @param GF_Field_Likert $field The current field.
* @param array $entry The current entry object.
*
* @return float|int
*/
public static function get_field_score( $field, $entry ) {
$score = 0;
if ( $field->gsurveyLikertEnableMultipleRows ) {
// cycle through the entry values in case the the number of choices has changed since the entry was submitted
foreach ( $entry as $key => $value ) {
if ( intval( $key ) != $field->id ) {
continue;
}
if ( false === strpos( $value, ':' ) ) {
continue;
}
list( $row_val, $col_val ) = explode( ':', $value, 2 );
foreach ( $field->gsurveyLikertRows as $row ) {
if ( $row['value'] == $row_val ) {
foreach ( $field->choices as $choice ) {
if ( $choice['value'] == $col_val ) {
$score += floatval( rgar( $choice, 'score' ) );
}
}
}
}
}
} else {
$value = rgar( $entry, $field->id );
if ( ! empty( $value ) ) {
foreach ( $field->choices as $choice ) {
if ( $choice['value'] == $value ) {
$score += floatval( rgar( $choice, 'score' ) );
}
}
}
}
return $score;
}
/**
* Returns the score for the specified row.
*
* @param string $target_row_val The unique id (value) for the likert row.
* @param GF_Field_Likert $field The current field.
* @param array $entry The current entry.
*
* @return float|int
*/
public static function get_likert_row_score( $target_row_val, $field, $entry ) {
$score = 0;
if ( $field->gsurveyLikertEnableMultipleRows ) {
foreach ( $entry as $key => $value ) {
if ( intval( $key ) != $field->id ) {
continue;
}
if ( false === strpos( $value, ':' ) ) {
continue;
}
list( $row_val, $col_val ) = explode( ':', $value, 2 );
foreach ( $field->gsurveyLikertRows as $row ) {
if ( $row['value'] == $row_val && $target_row_val == $row_val ) {
foreach ( $field->choices as $choice ) {
if ( $choice['value'] == $col_val ) {
$score = floatval( rgar( $choice, 'score' ) );
return $score;
}
}
}
}
}
} else {
$score = self::get_field_score( $field, $entry );
}
return $score;
}
// # MERGE TAGS -----------------------------------------------------------------------------------------------------
/**
* Add the score merge tags to the merge tag drop downs in the admin.
*
* @param array $form The current form.
*
* @return array
*/
public function add_merge_tags( $form ) {
if ( ! $this->is_form_settings() ) {
return $form;
}
$survey_fields = GFAPI::get_fields_by_type( $form, array( 'survey' ) );
if ( empty( $survey_fields ) ) {
return $form;
}
$scoring_enabled = false;
$merge_tags = array();
foreach ( $form['fields'] as $field ) {
if ( $field->get_input_type() == 'likert' && $field->gsurveyLikertEnableScoring ) {
$scoring_enabled = true;
$field_id = $field->id;
$field_label = $field->label;
$group = $field->isRequired ? 'required' : 'optional';
$merge_tags[] = array( 'group' => $group, 'label' => esc_html__( 'Survey Field Score: ', 'gravityformssurvey' ) . $field_label, 'tag' => "{score:id={$field_id}}" );
}
}
if ( $scoring_enabled ) {
$merge_tags[] = array( 'group' => 'other', 'label' => esc_html__( 'Survey Total Score', 'gravityformssurvey' ), 'tag' => '{survey_total_score}' );
}
?>
<script type="text/javascript">
if (window.gform)
gform.addFilter("gform_merge_tags", "gsurvey_add_merge_tags");
function gsurvey_add_merge_tags(mergeTags, elementId, hideAllFields, excludeFieldTypes, isPrepop, option) {
if (isPrepop)
return mergeTags;
var customMergeTags = <?php echo json_encode( $merge_tags ); ?>;
jQuery.each(customMergeTags, function (i, customMergeTag) {
mergeTags[customMergeTag.group].tags.push({ tag: customMergeTag.tag, label: customMergeTag.label });
});
return mergeTags;
}
</script>
<?php
//return the form object from the php hook
return $form;
}
/**
* Replace the score merge tags.
*
* @param string $text The current text in which merge tags are being replaced.
* @param array $form The current form object.
* @param array $entry The current entry object.
* @param bool $url_encode Whether or not to encode any URLs found in the replaced value.
* @param bool $esc_html Whether or not to encode HTML found in the replaced value.
* @param bool $nl2br Whether or not to convert newlines to break tags.
* @param string $format The format requested for the location the merge is being used. Possible values: html, text or url.
*
* @return string
*/
public function replace_merge_tags( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
if ( empty( $entry ) || empty( $form ) ) {
return $text;
}
$survey_fields = GFAPI::get_fields_by_type( $form, array( 'survey' ) );
if ( empty( $survey_fields ) ) {
return $text;
}
$total_merge_tag = '{survey_total_score}';
if ( false !== strpos( $text, $total_merge_tag ) ) {
$score_total = $this->get_total_score( $form, $entry );
$text = str_replace( $total_merge_tag, $score_total, $text );
}
preg_match_all( "/\{score:(.*?)\}/", $text, $matches, PREG_SET_ORDER );
foreach ( $matches as $match ) {
$full_tag = $match[0];
$options_string = isset( $match[1] ) ? $match[1] : '';
$options = shortcode_parse_atts( $options_string );
extract(
shortcode_atts(
array(
'id' => 0,
), $options
)
);
if ( 0 == $id ) {
continue;
}
$field = GFFormsModel::get_field( $form, $id );
if ( ! is_object( $field ) || $field->get_input_type() != 'likert' ) {
continue;
}
if ( $id == intval( $id ) ) {
$score = $this->get_field_score( $field, $entry );
} else {
$score = $this->get_likert_row_score( $field->get_row_id( $id ), $field, $entry );
}
$text = str_replace( $full_tag, $url_encode ? urlencode( $score ) : $score, $text );
}
return $text;
}
// # ENTRY RELATED --------------------------------------------------------------------------------------------------
/**
* Add the Survey Total Score entry meta property.
*
* @param array $entry_meta An array of entry meta already registered with the gform_entry_meta filter.
* @param int $form_id The form id
*
* @return array The filtered entry meta array.
*/
public function get_entry_meta( $entry_meta, $form_id ) {
if ( empty( $form_id ) ) {
return $entry_meta;
}
$form = RGFormsModel::get_form_meta( $form_id );
$survey_fields = GFAPI::get_fields_by_type( $form, array( 'survey' ) );
if ( false === empty( $survey_fields ) && $this->scoring_enabled( $form ) ) {
$entry_meta['gsurvey_score'] = array(
'label' => esc_html__( 'Survey Total Score', 'gravityformssurvey' ),
'is_numeric' => true,
'is_default_column' => false,
'update_entry_meta_callback' => array( $this, 'update_entry_meta' ),
'filter' => array(
'operators' => array( 'is', 'isnot', '>', '<' ),
),
);
}
return $entry_meta;
}
/**
* Used to update the Survey Total Score entry meta property
*
* @param string $key The key of the property to be updated.
* @param array $entry The current entry object.
* @param array $form The current form object.
*
* @return mixed
*/
public function update_entry_meta( $key, $entry, $form ) {
$value = '';
if ( $key == 'gsurvey_score' ) {
$value = $this->get_survey_score( $form, $entry );
}
return $value;
}
/**
* Remove the survey score from the entry meta conditional logic filters on the notifications/confirmations pages.
*
* @param array $filters The array of filters.
* @param array $form The current form object.
* @param string $id The ID of the notification/confirmation being edited.
*
* @return mixed
*/
public function conditional_logic_filters( $filters, $form, $id ) {
$survey_fields = GFAPI::get_fields_by_type( $form, array( 'survey' ) );
if ( empty( $survey_fields ) ) {
return $filters;
}
if ( false === $this->scoring_enabled( $form ) ) {
unset( $filters['gsurvey_score'] );
}
return $filters;
}
/**
* Format the Survey field values for entry exports and the entry list page so they use the choice text instead of values.
*
* @param string|array $value The field value.
* @param int $form_id The ID of the form currently being processed.
* @param string $field_id The ID of the field currently being processed.
* @param array $entry The entry object currently being processed.
*
* @return string|array
*/
public function export_field_value( $value, $form_id, $field_id, $entry ) {
if ( ! rgblank( $value ) ) {
$form_meta = RGFormsModel::get_form_meta( $form_id );
$field = RGFormsModel::get_field( $form_meta, $field_id );
return $this->maybe_format_field_values( $value, $field );
}
return $value;
}
/**
* Format the Survey field values on the entry detail page so they use the choice text instead of values.
*
* @param string|array $value The field value.
* @param GF_Field $field The field currently being processed.
* @param array $entry The entry object currently being processed.
* @param array $form The form object currently being processed.
*
* @return string|array
*/
public function entry_field_value( $value, $field, $entry, $form ) {
return ! rgblank( $value ) ? $this->maybe_format_field_values( $value, $field ) : $value;
}
/**
* Format the Survey field values so they use the choice text instead of values before being passed to the third-party.
*
* @param string $value The field value.
* @param array $form The form currently being processed.
* @param array $entry The entry object currently being processed.
* @param string $field_id The ID of the field currently being processed.
*
* @return string
*/
public function addon_field_value( $value, $form, $entry, $field_id, $slug ) {
if ( ! rgblank( $value ) ) {
$field = RGFormsModel::get_field( $form, $field_id );
return $this->maybe_format_field_values( $value, $field );
}
return $value;
}
/**
* If the field is a Survey type radio, select or checkbox then replace the choice value with the choice text.
*
* @param string $value The field value.
* @param GF_Field|null $field The field object being processed or null.
*
* @return string
*/
public function maybe_format_field_values( $value, $field ) {
if ( is_object( $field ) && $field->type == 'survey' ) {
switch ( $field->inputType ) {
case 'radio' :
case 'select' :
return RGFormsModel::get_choice_text( $field, $value );
case 'checkbox' :
if ( is_array( $value ) ) {
foreach ( $value as &$choice ) {
if ( ! empty( $choice ) ) {
$choice = RGFormsModel::get_choice_text( $field, $choice );
}
}
} else {
foreach ( $field->choices as $choice ) {
$val = rgar( $choice, 'value' );
$text = rgar( $choice, 'text' );
$value = str_replace( $val, $text, $value );
}
}
}
}
return $value;
}
/**
* Format the Survey field values so they use the choice text instead of values before being passed to the third-party.
*
* @param string $value The field value.
* @param int $form_id The ID of the form currently being processed.
* @param string $field_id The ID of the field currently being processed.
* @param array $entry The entry object currently being processed.
*
* @return string
*/
public function legacy_addon_field_value( $value, $form_id, $field_id, $entry ) {
if ( ! rgblank( $value ) ) {
$form_meta = RGFormsModel::get_form_meta( $form_id );
$field = RGFormsModel::get_field( $form_meta, $field_id );
if ( is_object( $field ) && $field->type == 'survey' ) {
return $field->get_value_export( $entry, $field_id, true );
}
}
return $value;
}
/**
* Format the Survey field values so they use the choice text instead of values before they are sent to Zapier.
*
* @param string|array $value The field value.
* @param int $form_id The ID of the form currently being processed.
* @param string $field_id The ID of the field currently being processed.
* @param array $entry The entry object currently being processed.
*
* @return string|array
*/
public function zapier_field_value( $value, $form_id, $field_id, $entry ) {
if ( ! rgblank( $value ) ) {
$form_meta = RGFormsModel::get_form_meta( $form_id );
$field = RGFormsModel::get_field( $form_meta, $field_id );
if ( is_object( $field ) && $field->type == 'survey' ) {
switch ( $field->inputType ) {
case 'likert' :
if ( is_array( $value ) ) {
foreach ( $value as $key => &$row ) {
if ( ! empty( $row ) ) {
$row = $field->get_column_text( $value, false, $key );
}
}
} else {
return $field->get_column_text( $value );
}
break;
case 'rank' :
case 'rating' :
case 'radio' :
case 'select' :
return $field->get_value_export( $entry, $field_id, true );
case 'checkbox' :
foreach ( $value as &$choice ) {
if ( ! empty( $choice ) ) {
$choice = RGFormsModel::get_choice_text( $field, $choice );
}
}
}
}
}
return $value;
}
// # FIELD SETTINGS -------------------------------------------------------------------------------------------------
/**
* Add the gsurvey-field class to the Survey field.
*
* @param string $classes The CSS classes to be filtered, separated by empty spaces.
* @param GF_Field $field The field currently being processed.
* @param array $form The form currently being processed.
*
* @return string
*/
public function add_custom_class( $classes, $field, $form ) {
if ( $field->type == 'survey' ) {
$classes .= ' gsurvey-survey-field ';
}
return $classes;
}
/**
* Add the tooltips for the Survey field.
*
* @param array $tooltips An associative array of tooltips where the key is the tooltip name and the value is the tooltip.
*
* @return array
*/
public function add_survey_tooltips( $tooltips ) {
$tooltips['gsurvey_question'] = '<h6>' . esc_html__( 'Survey Question', 'gravityformssurvey' ) . '</h6>' . esc_html__( 'Enter the question you would like to ask the user.', 'gravityformssurvey' );
$tooltips['gsurvey_field_type'] = '<h6>' . esc_html__( 'Survey Field Type', 'gravityformssurvey' ) . '</h6>' . esc_html__( 'Select the type of field that will be used for the survey.', 'gravityformssurvey' );
$tooltips['gsurvey_likert_columns'] = '<h6>' . esc_html__( 'Likert Columns', 'gravityformssurvey' ) . '</h6>' . esc_html__( 'Edit the choices for this likert field.', 'gravityformssurvey' );
$tooltips['gsurvey_likert_enable_multiple_rows'] = '<h6>' . esc_html__( 'Enable Multiple Rows', 'gravityformssurvey' ) . '</h6>' . esc_html__( 'Select to add multiple rows to the likert field.', 'gravityformssurvey' );
$tooltips['gsurvey_likert_rows'] = '<h6>' . esc_html__( 'Likert Rows', 'gravityformssurvey' ) . '</h6>' . esc_html__( 'Edit the texts that will appear to the left of each row of choices.', 'gravityformssurvey' );
$tooltips['gsurvey_likert_enable_scoring'] = '<h6>' . esc_html__( 'Enable Scoring', 'gravityformssurvey' ) . '</h6>' . esc_html__( 'Scoring allows different scores for each column. Aggregate scores are displayed in the results page and can be used in merge tags.', 'gravityformssurvey' );
return $tooltips;
}
/**
* Add the custom settings for the Survey fields to the fields general tab.
*
* @param int $position The position the settings should be located at.
* @param int $form_id The ID of the form currently being edited.
*/
public function survey_field_settings( $position, $form_id ) {
if ( $position == 25 ) {
?>
<li class="gsurvey-setting-question field_setting">
<label for="gsurvey-question" class="section_label">
<?php esc_html_e( 'Survey Question', 'gravityformssurvey' ); ?>
<?php gform_tooltip( 'gsurvey_question' ); ?>
</label>
<textarea id="gsurvey-question" class="fieldwidth-3 fieldheight-2"
onkeyup="SetFieldLabel(this.value)"
size="35"></textarea>
</li>
<li class="gsurvey-setting-field-type field_setting">
<label for="gsurvey-field-type" class="section_label">
<?php esc_html_e( 'Survey Field Type', 'gravityformssurvey' ); ?>
<?php gform_tooltip( 'gsurvey_field_type' ); ?>
</label>
<select id="gsurvey-field-type"
onchange="if(jQuery(this).val() == '') return; jQuery('#field_settings').slideUp(function(){StartChangeSurveyType(jQuery('#gsurvey-field-type').val());});">
<option value="likert"><?php esc_html_e( 'Likert', 'gravityformssurvey' ); ?></option>
<option value="rank"><?php esc_html_e( 'Rank', 'gravityformssurvey' ); ?></option>
<option value="rating"><?php esc_html_e( 'Rating', 'gravityformssurvey' ); ?></option>
<option value="radio"><?php esc_html_e( 'Radio Buttons', 'gravityformssurvey' ); ?></option>
<option value="checkbox"><?php esc_html_e( 'Checkboxes', 'gravityformssurvey' ); ?></option>
<option value="text"><?php esc_html_e( 'Single Line Text', 'gravityformssurvey' ); ?></option>
<option value="textarea"><?php esc_html_e( 'Paragraph Text', 'gravityformssurvey' ); ?></option>
<option value="select"><?php esc_html_e( 'Drop Down', 'gravityformssurvey' ); ?></option>
</select>
</li>
<?php
} elseif ( $position == 1362 ) {
?>
<li class="gsurvey-likert-setting-columns field_setting">
<div style="float:right;">
<input id="gsurvey-likert-enable-scoring" type="checkbox"
onclick="SetFieldProperty('gsurveyLikertEnableScoring', this.checked); jQuery('#gsurvey-likert-columns-container').toggleClass('gsurvey-likert-scoring-enabled');">
<label class="inline gfield_value_label" for="gsurvey-likert-enable-scoring"><?php esc_html_e( 'Enable Scoring', 'gravityformssurvey' ); ?></label> <?php gform_tooltip( 'gsurvey_likert_enable_scoring' ) ?>
</div>
<label for="gsurvey-likert-columns" class="section_label">
<?php esc_html_e( 'Columns', 'gravityformssurvey' ); ?>
<?php gform_tooltip( 'gsurvey_likert_columns' ); ?>
</label>
<div id="gsurvey-likert-columns-container">
<ul id="gsurvey-likert-columns">
</ul>
</div>
</li>
<li class="gsurvey-likert-setting-enable-multiple-rows field_setting">
<input type="checkbox" id="gsurvey-likert-enable-multiple-rows"
onclick="field = GetSelectedField(); var value = jQuery(this).is(':checked'); SetFieldProperty('gsurveyLikertEnableMultipleRows', value); gsurveyLikertUpdateInputs(field); gsurveyLikertUpdatePreview(); jQuery('.gsurvey-likert-setting-rows').toggle();" />
<label for="gsurvey-likert-enable-multiple-rows" class="inline">
<?php esc_html_e( 'Enable Multiple Rows', 'gravityformssurvey' ); ?>
<?php gform_tooltip( 'gsurvey_likert_enable_multiple_rows' ) ?>
</label>
</li>
<li class="gsurvey-likert-setting-rows field_setting">
<label for="gsurvey-likert-rows" class="section_label">
<?php esc_html_e( 'Rows', 'gravityformssurvey' ); ?>
<?php gform_tooltip( 'gsurvey_likert_rows' ) ?>
</label>
<div id="gsurvey-likert-rows-container">
<ul id="gsurvey-likert-rows"></ul>
</div>
</li>
<?php
}