__( 'You do not have permission to read individual answers.', 'ai-eshop-optimizer' ) ), 403 ); } if ( ! class_exists( 'AIEO_Form_Entries' ) || ! class_exists( 'AIEO_Survey_Blueprint' ) ) { wp_send_json_error( array( 'message' => __( 'The survey store is not loaded.', 'ai-eshop-optimizer' ) ) ); } $entry_id = isset( $_POST['entry_id'] ) ? (int) $_POST['entry_id'] : 0; $bp = isset( $_POST['blueprint'] ) ? sanitize_key( wp_unslash( $_POST['blueprint'] ) ) : ''; if ( $entry_id <= 0 ) { wp_send_json_error( array( 'message' => __( 'No response was named.', 'ai-eshop-optimizer' ) ) ); } $answers = (array) AIEO_Form_Entries::entry_answers( $entry_id ); if ( ! $answers ) { wp_send_json_error( array( 'message' => __( 'That response has no stored answers.', 'ai-eshop-optimizer' ) ) ); } wp_send_json_success( array( 'entry_id' => $entry_id, 'rows' => $this->answer_rows( $answers, $bp ), ) ); } private function answer_rows( array $answers, $bp ) { $bank = AIEO_Survey_Blueprint::all()['questions']; $by_code = array(); foreach ( $answers as $a ) { $code = (string) $a['field_key']; if ( ! isset( $by_code[ $code ] ) ) { $by_code[ $code ] = array( 'parts' => array(), 'disposition' => (string) $a['disposition'] ); } if ( 'answered' === (string) $a['disposition'] ) { $by_code[ $code ]['disposition'] = 'answered'; } $q = isset( $bank[ $code ] ) ? $bank[ $code ] : null; $text = $this->answer_text( $q, $a ); if ( '' !== $text ) { $by_code[ $code ]['parts'][] = $text; } } $order = ( '' !== $bp ) ? (array) AIEO_Survey_Blueprint::question_codes( $bp ) : array(); $codes = array(); foreach ( $order as $c ) { if ( isset( $by_code[ $c ] ) ) { $codes[] = $c; } } foreach ( array_keys( $by_code ) as $c ) { if ( ! in_array( $c, $codes, true ) ) { $codes[] = $c; } } $out = array(); foreach ( $codes as $code ) { $q = isset( $bank[ $code ] ) ? $bank[ $code ] : null; $out[] = array( 'label' => ( $q && isset( $q['label'] ) && '' !== (string) $q['label'] ) ? (string) $q['label'] : $code, 'answer' => implode( ', ', $by_code[ $code ]['parts'] ), 'disposition' => $by_code[ $code ]['disposition'], ); } return $out; } private function answer_text( $q, $a ) { $choice = ( null === $a['choice_key'] ) ? '' : (string) $a['choice_key']; if ( '' !== $choice ) { if ( $q && ! empty( $q['choices'] ) && is_array( $q['choices'] ) && isset( $q['choices'][ $choice ] ) ) { return (string) $q['choices'][ $choice ]; } return $choice; } if ( null !== $a['value_num'] ) { $n = (float) $a['value_num']; return ( 0.0 === fmod( $n, 1.0 ) ) ? (string) (int) $n : (string) $n; } return (string) $a['value_text']; } public function enqueue_admin_assets() { $css = 'assets/css/aieo-questionnaire-results-admin.css'; wp_enqueue_style( 'aieo-questionnaire-results-admin', plugins_url( $css, AIEO_FILE ), array(), file_exists( AIEO_DIR . $css ) ? (string) filemtime( AIEO_DIR . $css ) : '1' ); } public function render_subtab() { $blueprints = class_exists( 'AIEO_Survey_Blueprint' ) ? (array) AIEO_Survey_Blueprint::options() : array(); $bp = isset( $_GET['qr_bp'] ) ? sanitize_key( wp_unslash( $_GET['qr_bp'] ) ) : ''; $days = isset( $_GET['qr_days'] ) ? (int) $_GET['qr_days'] : 30; $since = isset( $_GET['qr_since'] ) ? sanitize_text_field( wp_unslash( $_GET['qr_since'] ) ) : ''; $min_n = isset( $_GET['qr_min'] ) ? (int) $_GET['qr_min'] : 20; $wrote = isset( $_GET['qr_wrote'] ) ? sanitize_key( wp_unslash( $_GET['qr_wrote'] ) ) : 'all'; if ( ! isset( $blueprints[ $bp ] ) ) { $keys = array_keys( $blueprints ); $bp = $keys ? (string) $keys[0] : ''; } if ( ! in_array( $days, array( 7, 30, 90, 180, 365 ), true ) ) { $days = 30; } if ( ! in_array( $wrote, array( 'written', 'all' ), true ) ) { $wrote = 'all'; } if ( ! in_array( $min_n, array( 0, 10, 20, 50 ), true ) ) { $min_n = 20; } if ( '' !== $since && ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $since ) ) { $since = ''; } $this->render_template( 'questionnaire-results.php', array( 'blueprints' => $blueprints, 'bp' => $bp, 'days' => $days, 'since' => $since, 'min_n' => $min_n, 'wrote' => $wrote, ) ); } }