GFPDFHelperFieldsField_Quiz

Controls the display and output of a Gravity Form field.

Defined (1)

The class is defined in the following location(s).

/src/helper/fields/Field_Quiz.php  
  1. class Field_Quiz extends Helper_Abstract_Fields { 
  2.  
  3. /** 
  4. * Return the HTML form data 
  5. * @return array 
  6. * @since 4.0 
  7. */ 
  8. public function form_data() { 
  9.  
  10. $value = $this->value(); 
  11. $label = GFFormsModel::get_label( $this->field ); 
  12. $data = []; 
  13.  
  14. $data['field'][ $this->field->id . '.' . $label ] = $value; 
  15. $data['field'][ $this->field->id ] = $value; 
  16. $data['field'][ $label ] = $value; 
  17.  
  18. /** Backwards compatible */ 
  19. $data['field'][ $this->field->id . '.' . $label . '_name' ] = $value; 
  20. $data['field'][ $this->field->id . '_name' ] = $value; 
  21. $data['field'][ $label . '_name' ] = $value; 
  22.  
  23. return $data; 
  24.  
  25. /** 
  26. * Display the HTML version of this field 
  27. * @param string $value 
  28. * @param bool $label 
  29. * @return string 
  30. * @since 4.0 
  31. */ 
  32. public function html( $value = '', $label = true ) { 
  33. $value = apply_filters( 'gform_entry_field_value', $this->get_value(), $this->field, $this->entry, $this->form ); 
  34.  
  35. /** Return early to prevent any problems with when field is empty or the quiz plugin isn't enabled */ 
  36. if ( ! class_exists( 'GFQuiz' ) || ! is_string( $value ) || trim( $value ) == false ) { 
  37. return parent::html( '' ); 
  38.  
  39. /** 
  40. * Add class to the quiz images so mPDF can style them (limited cascade support) 
  41. * We'll try use our DOM reader to correctly process the HTML, otherwise use string replace 
  42. */ 
  43. try { 
  44. $qp = new Helper_QueryPath(); 
  45. $value = $qp->html5( $value, 'img' )->addClass( 'gf-quiz-img' )->top( 'html' )->innerHTML5(); 
  46. } catch ( Exception $e ) { 
  47. $value = str_replace( '<img ', '<img class="gf-quiz-img" ', $value ); 
  48.  
  49. return parent::html( $value ); 
  50.  
  51. /** 
  52. * Get the standard GF value of this field 
  53. * @return string|array 
  54. * @since 4.0 
  55. */ 
  56. public function value() { 
  57.  
  58. /** Get the field value */ 
  59. $value = $this->get_value(); 
  60. $value = ( ! is_array( $value ) ) ? [ $value ] : $value; 
  61.  
  62. $formatted = []; 
  63.  
  64. /** Loop through our results */ 
  65. foreach ( $value as $item ) { 
  66. foreach ( $this->field->choices as $choice ) { 
  67. if ( $choice['value'] == $item ) { 
  68. $formatted[] = [ 
  69. 'text' => esc_html( $choice['text'] ),  
  70. 'isCorrect' => $choice['gquizIsCorrect'],  
  71. 'weight' => ( isset( $choice['gquizWeight'] ) ) ? $choice['gquizWeight'] : '',  
  72. ]; 
  73.  
  74. /** Ensure results are formatted to v3 expectations */ 
  75. if ( 1 === sizeof( $formatted ) ) { 
  76. return $formatted[0]; 
  77.  
  78. /** Return our results, if we have any */ 
  79. if ( 0 < sizeof( $formatted ) ) { 
  80. return $formatted; 
  81.  
  82. /** Return the default expected structure */ 
  83.  
  84. return [];