$field ) { $type = $field['type']; $answers = array(); // 각 응답에서 이 필드 값 수집. foreach ( $submissions as $sub ) { $value = isset( $sub['data'][ $index ] ) ? $sub['data'][ $index ] : ''; if ( is_array( $value ) ) { foreach ( $value as $v ) { if ( '' !== trim( (string) $v ) ) { $answers[] = $v; } } } elseif ( '' !== trim( (string) $value ) ) { $answers[] = $value; } } $entry = array( 'field' => $field, 'answered' => count( $answers ), ); if ( aj_form_is_choice_field( $type ) ) { // 옵션별 카운트 (미리 정의된 옵션 기준 + 기타 값 포함). $counts = array(); foreach ( $field['options'] as $opt ) { $counts[ $opt ] = 0; } foreach ( $answers as $ans ) { if ( ! isset( $counts[ $ans ] ) ) { $counts[ $ans ] = 0; } $counts[ $ans ]++; } // 관리자가 지정한 초기 수치를 실제 응답 위에 더한다. // 전역 설정이 꺼져 있으면 저장된 값이 있어도 무시한다 — 끄면 즉시 // 실제 응답 수만 보이게 하기 위함이다. $seeds = ( aj_form_seeds_enabled() && isset( $field['seeds'] ) && is_array( $field['seeds'] ) ) ? $field['seeds'] : array(); foreach ( $seeds as $opt => $seed ) { $seed = max( 0, (int) $seed ); if ( ! $seed ) { continue; } if ( ! isset( $counts[ $opt ] ) ) { $counts[ $opt ] = 0; } $counts[ $opt ] += $seed; } $entry['counts'] = $counts; // 비율 계산의 분모도 seed 를 포함해야 합이 100% 가 된다. $entry['answered'] = array_sum( $counts ); } elseif ( 'number' === $type ) { $nums = array_map( 'floatval', $answers ); if ( $nums ) { $entry['sum'] = array_sum( $nums ); $entry['avg'] = $entry['sum'] / count( $nums ); $entry['min'] = min( $nums ); $entry['max'] = max( $nums ); } } $stats[ $index ] = $entry; } return $stats; } /** * 화면에 표시할 전체 응답 수. * * seed 를 쓰면 실제 행 수보다 개별 질문의 응답 수가 커질 수 있다. 그대로 두면 * "6 responses" 위에 40표짜리 질문이 놓이는 모순이 생기므로, 단일 선택 질문의 * 최대값까지 끌어올린다. 체크박스는 한 사람이 여러 개를 고르므로 제외한다. * * @param int $total 실제 응답 행 수. * @param array $stats aj_form_compute_stats() 결과. * @param array $fields * @return int */ function aj_form_display_total( $total, $stats, $fields ) { foreach ( $stats as $index => $entry ) { $type = isset( $fields[ $index ]['type'] ) ? $fields[ $index ]['type'] : ''; if ( 'radio' !== $type && 'select' !== $type ) { continue; } $total = max( $total, (int) $entry['answered'] ); } return (int) $total; } /** * 응답/통계 화면 렌더링. */ function aj_form_render_entries_page() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Permission denied.', 'ajebang-form-survey-builder' ), 403 ); } // 어떤 폼의 응답을 보여줄지 읽기만 한다. 권한은 바로 위에서 검사했다. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- 위 주석 참고. $form_id = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0; $form = $form_id ? aj_form_get_form( $form_id ) : null; $back = admin_url( 'admin.php?page=aj-form-list' ); echo '
'; if ( ! $form ) { echo '

' . esc_html__( 'Form Responses', 'ajebang-form-survey-builder' ) . '

'; echo '

' . esc_html__( 'Form not found.', 'ajebang-form-survey-builder' ) . '

'; echo '← ' . esc_html__( 'Back to Forms', 'ajebang-form-survey-builder' ) . ''; echo '
'; return; } $type = aj_form_normalize_type( $form['type'] ); $submissions = aj_form_get_submissions( $form_id ); $real_total = count( $submissions ); $total = aj_form_display_total( $real_total, aj_form_compute_stats( $form['fields'], $submissions ), $form['fields'] ); if ( 'survey' === $type ) { /* translators: %s: form title */ $heading = sprintf( __( 'Statistics: %s', 'ajebang-form-survey-builder' ), $form['title'] ); } else { /* translators: %s: form title */ $heading = sprintf( __( 'Responses: %s', 'ajebang-form-survey-builder' ), $form['title'] ); } ?>

← 0 ) : ?>

' . wp_kses_post( wpautop( $description ) ) . ''; } if ( 'survey' === $type ) { aj_form_render_survey_stats( $form['fields'], $submissions, $total ); } else { aj_form_render_form_notice(); } ?>

'; esc_html_e( 'This is a basic form. Individual responses are collected but not displayed here. Change the form type to "Survey" to see aggregated statistics.', 'ajebang-form-survey-builder' ); echo '

'; } /** * 설문: 필드별 집계 통계 렌더링. * * @param array $fields * @param array $submissions * @param int $total */ function aj_form_render_survey_stats( $fields, $submissions, $total ) { if ( empty( $fields ) ) { echo '

' . esc_html__( 'This form has no fields.', 'ajebang-form-survey-builder' ) . '

'; return; } if ( 0 === $total ) { echo '

' . esc_html__( 'No submissions yet.', 'ajebang-form-survey-builder' ) . '

'; return; } $stats = aj_form_compute_stats( $fields, $submissions ); echo '
'; foreach ( $stats as $entry ) { $field = $entry['field']; $label = '' !== $field['label'] ? $field['label'] : $field['type']; /* translators: %d: number of responses to a single field */ $count_label = sprintf( _n( '%d response', '%d responses', (int) $entry['answered'], 'ajebang-form-survey-builder' ), (int) $entry['answered'] ); echo '
'; echo '

' . esc_html( $label ) . ' (' . esc_html( $count_label ) . ')

'; if ( aj_form_is_choice_field( $field['type'] ) ) { aj_form_render_choice_bars( $entry['counts'], (int) $entry['answered'] ); } elseif ( 'number' === $field['type'] && isset( $entry['avg'] ) ) { echo ''; } else { // 자유 입력형은 프라이버시상 내용을 표시하지 않고 응답 수만. /* translators: %d: number of free-text responses */ $note = sprintf( __( '%d text responses collected.', 'ajebang-form-survey-builder' ), (int) $entry['answered'] ); echo '

' . esc_html( $note ) . '

'; } echo '
'; } echo '
'; } /** * 선택형 필드의 옵션별 막대 그래프 렌더링. * * @param array $counts 옵션 => 카운트. * @param int $answered 이 필드에 응답한 총 선택 수. */ function aj_form_render_choice_bars( $counts, $answered ) { if ( empty( $counts ) ) { echo '

' . esc_html__( 'No options defined.', 'ajebang-form-survey-builder' ) . '

'; return; } $max = max( $counts ) > 0 ? max( $counts ) : 1; echo '