Element_Responses::show_results( array $results, awsmug\Torro_Forms\DB_Objects\Forms\Form $form, array $args = array() )

Renders evaluation results for a specific form.

Description

Parameters

$results

(array) (Required) Results to show.

$form

(awsmug\Torro_Forms\DB_Objects\Forms\Form) (Required) Form the results belong to.

$args

(array) (Optional) Additional arguments for displaying the results.

  • 'element_id'
    (int|string|array) One or more element IDs to only display results for those elements. Otherwise results are displayed for all elements. Default none.

Default value: array()

Source

File: src/modules/evaluators/element-responses.php

	public function show_results( $results, $form, $args = array() ) {
		$tabs = array();

		$elements = $this->module->manager()->forms()->get_child_manager( 'containers' )->get_child_manager( 'elements' );

		if ( ! empty( $args['element_id'] ) ) {
			if ( is_int( $args['element_id'] ) ) {
				$element_ids = array( $args['element_id'] );
			} else {
				$element_ids = wp_parse_id_list( $args['element_id'] );
			}
		} else {
			$element_ids = $form->get_elements( array(
				'fields' => 'ids',
			) );
		}

		foreach ( $element_ids as $element_id ) {
			if ( ! $this->is_element_evaluatable( $element_id ) ) {
				continue;
			}

			$element = $elements->get( $element_id );
			if ( ! $element ) {
				continue;
			}

			// TODO: Multi-field element support.

			$tabs[ $element_id . '__main' ] = array(
				'label'    => $element->label,
				'callback' => function() use ( $results, $element, $form ) {
					$responses = array();
					$response_values = array();

					foreach ( $element->get_element_choices() as $element_choice ) {
						if ( ! empty( $element_choice->field ) && '_main' !== $element_choice->field ) {
							continue;
						}

						$responses[] = $element_choice->value;
						if ( isset( $results[ $element->id ]['_main'][ $element_choice->value ] ) ) {
							$response_values[] = (int) $results[ $element->id ]['_main'][ $element_choice->value ];
						} else {
							$response_values[] = 0;
						}
					}

					?>
					<div id="<?php echo esc_attr( $this->slug . '-chart-' . $element->id . '__main' ); ?>"></div>
					<script type="application/json" class="c3-chart-data">
						<?php echo json_encode( $this->get_chart_json( $form, esc_attr( $this->slug . '-chart-' . $element->id . '__main' ), $responses, $response_values ) ); ?>
					</script>
					<?php
				},
			);
		}

		if ( ! empty( $tabs ) ) {
			$this->display_tabs( $tabs );
		} else {
			echo '<p>' . __( 'This form does not contain any evaluatable elements.', 'torro-forms' ) . '</p>';
		}
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.