$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 '
' . esc_html__( 'Form not found.', 'ajebang-form-survey-builder' ) . '
'; echo '← ' . esc_html__( 'Back to Forms', 'ajebang-form-survey-builder' ) . ''; echo '
'; 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 '' . esc_html( $note ) . '
'; } echo '' . esc_html__( 'No options defined.', 'ajebang-form-survey-builder' ) . '
'; return; } $max = max( $counts ) > 0 ? max( $counts ) : 1; echo '