0 && $rows > $people ) ? 'scale_per_subject' : 'scale'; } if ( isset( $q['cardinality'] ) && 'many' === $q['cardinality'] ) { return 'multi'; } return 'choice'; } } if ( ! function_exists( 'aieo_qr_kind_label' ) ) { function aieo_qr_kind_label( $shape ) { $map = array( 'scale' => __( 'Score', 'ai-eshop-optimizer' ), 'scale_per_subject' => __( 'Score per product', 'ai-eshop-optimizer' ), 'number' => __( 'Number', 'ai-eshop-optimizer' ), 'choice' => __( 'One answer', 'ai-eshop-optimizer' ), 'multi' => __( 'Several answers', 'ai-eshop-optimizer' ), 'ranking' => __( 'Ranking', 'ai-eshop-optimizer' ), 'text' => __( 'Written', 'ai-eshop-optimizer' ), ); return isset( $map[ $shape ] ) ? $map[ $shape ] : $shape; } } if ( ! function_exists( 'aieo_qr_results_rows' ) ) { function aieo_qr_results_rows( $params ) { if ( ! class_exists( 'AIEO_Survey_Blueprint' ) || ! class_exists( 'AIEO_Form_Entries' ) ) { return array(); } $bp = aieo_qr_blueprint( $params ); if ( '' === $bp ) { return array(); } $surfaces = (array) AIEO_Survey_Health::surfaces( $bp ); if ( ! $surfaces ) { return array(); } $cut = aieo_qr_cut( $params ); $min_n = isset( $params['min_n'] ) ? max( 0, (int) $params['min_n'] ) : 20; $stats = AIEO_Form_Entries::question_stats( $surfaces, $cut ); $bank = AIEO_Survey_Blueprint::all()['questions']; $out = array(); $rank = 0; foreach ( AIEO_Survey_Blueprint::question_codes( $bp ) as $code ) { ++$rank; $q = isset( $bank[ $code ] ) ? $bank[ $code ] : null; if ( ! $q ) { continue; } $s = isset( $stats[ $code ] ) ? $stats[ $code ] : array(); $n_now = isset( $s['n_now'] ) ? (int) $s['n_now'] : 0; $n_before = isset( $s['n_before'] ) ? (int) $s['n_before'] : 0; $asked_now = isset( $s['asked_now'] ) ? (int) $s['asked_now'] : 0; $asked_bef = isset( $s['asked_before'] ) ? (int) $s['asked_before'] : 0; $rows_now = isset( $s['rows_now'] ) ? (int) $s['rows_now'] : 0; $mean_now = ( isset( $s['mean_now'] ) && null !== $s['mean_now'] ) ? (float) $s['mean_now'] : null; $mean_bef = ( isset( $s['mean_before'] ) && null !== $s['mean_before'] ) ? (float) $s['mean_before'] : null; $shape = aieo_qr_shape( $q, $rows_now, $n_now ); $max_now = ( isset( $s['max_now'] ) && null !== $s['max_now'] ) ? (float) $s['max_now'] : null; $max_bef = ( isset( $s['max_before'] ) && null !== $s['max_before'] ) ? (float) $s['max_before'] : null; $axis_now = ( null === $max_now ) ? '' : '1-' . ( 0 + $max_now ); $axis_bef = ( null === $max_bef ) ? '' : '1-' . ( 0 + $max_bef ); $is_scored = in_array( $shape, array( 'scale', 'scale_per_subject', 'number' ), true ); $axis_moved = ( null !== $max_now && null !== $max_bef && $max_now !== $max_bef ); $scale_label = ''; $scale_tone = ''; if ( $is_scored ) { $scale_label = $axis_moved ? sprintf( __( 'was %1$s, now %2$s', 'ai-eshop-optimizer' ), $axis_bef, $axis_now ) : ( '' !== $axis_now ? $axis_now : $axis_bef ); $scale_tone = $axis_moved ? 'danger' : ''; } if ( 'ranking' === $shape ) { $mean_now = null; $mean_bef = null; } $delta = null; if ( null !== $mean_now && null !== $mean_bef && ! $axis_moved && $n_now >= $min_n && $n_before >= $min_n ) { $lo = isset( $q['min'] ) ? (float) $q['min'] : 1.0; $span = ( null === $max_now ) ? 0.0 : ( $max_now - $lo ); if ( $span > 0 ) { $delta = ( ( $mean_now - $mean_bef ) / $span ) * 100; } } $out[] = array( 'question_key' => $code, 'question_rank' => $rank, 'question' => isset( $q['label'] ) ? (string) $q['label'] : $code, 'kind' => aieo_qr_kind_label( $shape ), 'mean_now' => $mean_now, 'mean_before' => $mean_bef, 'delta_pp' => ( null === $delta ) ? null : round( $delta, 1 ), 'scale_label' => $scale_label, 'scale_tone' => $scale_tone, 'n_now' => $n_now, 'n_before' => $n_before, 'response_rate' => $asked_now > 0 ? round( 100 * $n_now / $asked_now, 1 ) : null, 'written' => isset( $s['text_now'] ) ? (int) $s['text_now'] + (int) $s['text_before'] : 0, ); } return $out; } } if ( ! function_exists( 'aieo_qr_answers_rows' ) ) { function aieo_qr_answers_rows( $params ) { if ( ! class_exists( 'AIEO_Survey_Blueprint' ) || ! class_exists( 'AIEO_Form_Entries' ) ) { return array(); } $bp = aieo_qr_blueprint( $params ); if ( '' === $bp ) { return array(); } $surfaces = (array) AIEO_Survey_Health::surfaces( $bp ); if ( ! $surfaces ) { return array(); } $cut = aieo_qr_cut( $params ); $bank = AIEO_Survey_Blueprint::all()['questions']; $codes = AIEO_Survey_Blueprint::question_codes( $bp ); $choice_codes = array(); $value_codes = array(); foreach ( $codes as $c ) { $q = isset( $bank[ $c ] ) ? $bank[ $c ] : null; if ( ! $q ) { continue; } if ( isset( $q['scored'] ) && 'scale' === $q['scored'] && ( ! isset( $q['ordered'] ) || 'yes' !== $q['ordered'] ) ) { $value_codes[] = $c; } elseif ( 'choice' === $q['answers'] ) { $choice_codes[] = $c; } } $stats = AIEO_Form_Entries::question_stats( $surfaces, $cut ); $by_choice = AIEO_Form_Entries::answer_breakdown( $surfaces, $cut, $choice_codes, 'choice' ); $by_value = AIEO_Form_Entries::answer_breakdown( $surfaces, $cut, $value_codes, 'value' ); $out = array(); $rank = 0; foreach ( $codes as $code ) { ++$rank; $q = isset( $bank[ $code ] ) ? $bank[ $code ] : null; if ( ! $q ) { continue; } $label = isset( $q['label'] ) ? (string) $q['label'] : $code; $ranking = ( isset( $q['ordered'] ) && 'yes' === $q['ordered'] ); $rows = isset( $by_choice[ $code ] ) ? $by_choice[ $code ] : ( isset( $by_value[ $code ] ) ? $by_value[ $code ] : array() ); if ( ! $rows ) { continue; } $s = isset( $stats[ $code ] ) ? $stats[ $code ] : array(); $n_now = isset( $s['n_now'] ) ? (int) $s['n_now'] : 0; $n_before = isset( $s['n_before'] ) ? (int) $s['n_before'] : 0; $acc = array(); foreach ( $rows as $r ) { $b = (string) $r['bucket']; if ( ! isset( $acc[ $b ] ) ) { $acc[ $b ] = array( 'now' => 0, 'before' => 0, 'mean' => null, 'firsts' => 0 ); } if ( (int) $r['win'] ) { $acc[ $b ]['now'] = (int) $r['n']; } else { $acc[ $b ]['before'] = (int) $r['n']; } if ( $ranking ) { if ( null !== $r['mean_position'] ) { $acc[ $b ]['mean'] = ( null === $acc[ $b ]['mean'] ) ? (float) $r['mean_position'] : ( $acc[ $b ]['mean'] + (float) $r['mean_position'] ) / 2; } $acc[ $b ]['firsts'] += (int) $r['firsts']; } } $sub = 0; foreach ( $acc as $bucket => $a ) { ++$sub; $shown = is_numeric( $bucket ) ? (string) ( 0 + (float) $bucket ) : (string) $bucket; $share_now = $n_now > 0 ? round( 100 * $a['now'] / $n_now, 1 ) : null; $share_bef = $n_before > 0 ? round( 100 * $a['before'] / $n_before, 1 ) : null; $out[] = array( 'answer_key' => $code . ':' . $bucket, 'sort_rank' => $rank * 1000 + $sub, 'question' => $label, 'answer' => $shown, 'count_now' => (int) $a['now'], 'count_before' => (int) $a['before'], 'share_now' => $share_now, 'share_before' => $share_bef, 'share_delta_pp' => ( null !== $share_now && null !== $share_bef ) ? round( $share_now - $share_bef, 1 ) : null, 'mean_position' => $ranking ? ( null === $a['mean'] ? null : round( $a['mean'], 2 ) ) : null, 'firsts' => $ranking ? (int) $a['firsts'] : null, ); } } return $out; } } if ( ! function_exists( 'aieo_qr_responses_rows' ) ) { function aieo_qr_responses_rows( $params ) { if ( ! class_exists( 'AIEO_Survey_Blueprint' ) || ! class_exists( 'AIEO_Form_Entries' ) ) { return array(); } $bp = aieo_qr_blueprint( $params ); if ( '' === $bp ) { return array(); } $surfaces = array( 'aieo_' . AIEO_Survey_Blueprint::code( $bp ) ); $limit = isset( $params['limit'] ) ? max( 1, (int) $params['limit'] ) : 500; $wrote = ( isset( $params['written_only'] ) && 'written' === (string) $params['written_only'] ) ? 'written' : 'all'; $hpos = false; $util = 'Automattic' . chr( 92 ) . 'WooCommerce' . chr( 92 ) . 'Utilities' . chr( 92 ) . 'OrderUtil'; if ( class_exists( $util ) && method_exists( $util, 'custom_orders_table_usage_is_enabled' ) ) { $hpos = (bool) call_user_func( array( $util, 'custom_orders_table_usage_is_enabled' ) ); } $bank = AIEO_Survey_Blueprint::all()['questions']; $out = array(); $responses = AIEO_Form_Entries::surface_responses( $surfaces, $limit ); if ( ! class_exists( 'AIEO_Submission_Panel', false ) && defined( 'AIEO_DIR' ) && is_readable( AIEO_DIR . 'includes/dmm/class-aieo-submission-panel.php' ) ) { require_once AIEO_DIR . 'includes/dmm/class-aieo-submission-panel.php'; } $leads = class_exists( 'AIEO_Submission_Panel' ) ? AIEO_Submission_Panel::keys_for( 'aieo_survey', array_map( static function ( $r ) { return (int) $r['id']; }, (array) $responses ) ) : array(); foreach ( $responses as $r ) { $written = array(); foreach ( AIEO_Form_Entries::entry_answers( (int) $r['id'] ) as $a ) { if ( 'answered' !== $a['disposition'] ) { continue; } $q = isset( $bank[ $a['field_key'] ] ) ? $bank[ $a['field_key'] ] : null; $lbl = $q && isset( $q['label'] ) ? (string) $q['label'] : (string) $a['field_key']; if ( null === $a['choice_key'] && null === $a['value_num'] && '' !== (string) $a['value_text'] ) { $written[ $lbl ] = (string) $a['value_text']; } } if ( 'written' === $wrote && ! $written ) { continue; } $oid = (int) $r['order_id']; $uid = (int) $r['user_id']; $out[] = array( 'entry_id' => (int) $r['id'], 'when' => (string) $r['created_at'], 'surface' => (string) $r['surface'], 'order_id' => (int) $r['order_id'], 'customer' => ( (int) $r['eponymous_id'] > 0 ) ? (int) $r['eponymous_id'] : $uid, 'customer_kind' => ( (int) $r['eponymous_id'] > 0 ) ? __( 'customer record', 'ai-eshop-optimizer' ) : ( $uid > 0 ? __( 'account', 'ai-eshop-optimizer' ) : '' ), 'order_url' => $oid > 0 ? admin_url( $hpos ? 'admin.php?page=wc-orders&action=edit&id=' . $oid : 'post.php?action=edit&post=' . $oid ) : '', 'customer_url' => $uid > 0 ? admin_url( 'user-edit.php?user_id=' . $uid ) : '', 'lead' => isset( $leads[ (int) $r['id'] ] ) ? __( 'Campaign and journey', 'ai-eshop-optimizer' ) : '', 'lead_url' => isset( $leads[ (int) $r['id'] ] ) ? AIEO_Submission_Panel::url( $leads[ (int) $r['id'] ] ) : '', 'answered' => (int) $r['answered'], 'skipped' => (int) $r['skipped'], 'comments' => ( count( $written ) > 1 ) ? implode( ' · ', array_map( static function ( $k, $v ) { return $k . ': ' . $v; }, array_keys( $written ), $written ) ) : (string) reset( $written ), 'wrote' => $written ? 'written' : 'none', ); } return $out; } } $aieo_qr_params = array( 'blueprint' => array( 'type' => 'text', 'default' => 'post_order' ), 'days' => array( 'type' => 'int', 'default' => 30, 'options' => array( 7, 30, 90, 180, 365 ) ), 'since' => array( 'type' => 'text', 'default' => '' ), 'min_n' => array( 'type' => 'int', 'default' => 20, 'options' => array( 0, 10, 20, 50 ) ), ); return array( array( 'id' => 'questionnaire_results', 'label' => 'Every question, and whether it moved', 'description' => 'One row per question: how many people answered it, what they said on average, and the same figures for everything answered before the date you chose. Change is in percentage points of the question\'s own scale, so a 1-5 score and a 0-10 score can be read down one column.', 'footnote' => 'Nothing here is totalled: the same person answers every question, so adding the counts down the table counts them once per question. Skipped answers are excluded from the averages, and only answered-or-skipped counts as asked. A question whose scale is not the same on both sides of the date shows no change at all, because the two averages are not measured on the same ruler. A ranking never shows an average: everybody uses every position, so its mean is arithmetic rather than opinion. A change is suppressed below the minimum answers you set, rather than shown as noise. To READ the written answers rather than count them, use the third table below and filter Wrote anything to "Wrote something".', 'tier' => 'pro', 'scope' => 'local', 'source' => 'rows', 'rows' => 'aieo_qr_results_rows', 'registry_id' => 'questionnaire_results.by_question', 'version' => '1', 'per_page' => 50, 'params' => $aieo_qr_params, 'row_key' => 'question_key', 'default_sort' => array( 'question_rank', 'asc' ), 'empty_state' => 'No answers yet for this survey.', 'columns' => array( array( 'key' => 'question_key', 'label' => 'Code', 'type' => 'code', 'sortable' => true, 'hidden' => true ), array( 'key' => 'question_rank', 'label' => 'Order', 'type' => 'integer', 'sortable' => true, 'hidden' => true ), array( 'key' => 'question', 'label' => 'Question', 'type' => 'text', 'sortable' => true, 'sort_key' => 'question_rank', 'searchable' => true, 'subline' => 'question_key', 'truncate' => 70, 'tooltip' => true ), array( 'key' => 'kind', 'label' => 'Kind', 'type' => 'badge', 'sortable' => true, 'filter' => 'select' ), array( 'key' => 'mean_now', 'label' => 'Average since', 'type' => 'decimal', 'format' => array( 'precision' => 2 ), 'sortable' => true, 'empty' => '—' ), array( 'key' => 'mean_before', 'label' => 'Average before', 'type' => 'decimal', 'format' => array( 'precision' => 2 ), 'sortable' => true, 'empty' => '—' ), array( 'key' => 'delta_pp', 'label' => 'Change', 'type' => 'decimal', 'format' => array( 'precision' => 1 ), 'suffix' => ' pp', 'sortable' => true, 'empty' => '—', 'bands' => array( array( 'max' => -1, 'tone' => 'danger' ), array( 'min' => -1, 'max' => 1, 'tone' => 'muted' ), array( 'min' => 1, 'tone' => 'success' ), ), ), array( 'key' => 'scale_label', 'label' => 'Out of', 'type' => 'text', 'sortable' => false, 'empty' => '—' ), array( 'key' => 'scale_tone', 'label' => 'Scale tone', 'type' => 'text', 'sortable' => false, 'hidden' => true ), array( 'key' => 'n_now', 'label' => 'Answers since', 'type' => 'integer', 'sortable' => true ), array( 'key' => 'n_before', 'label' => 'Answers before', 'type' => 'integer', 'sortable' => true ), array( 'key' => 'response_rate', 'label' => 'Answered when asked', 'type' => 'percent', 'format' => array( 'precision' => 0 ), 'sortable' => true, 'empty' => '—' ), array( 'key' => 'written', 'label' => 'Written answers', 'type' => 'integer', 'sortable' => true ), ), ), array( 'id' => 'questionnaire_answers', 'label' => 'What people actually chose', 'description' => 'Every answer people could give and how often they gave it, since the date you chose and before it. This is the half of a survey that has no average: a yes/no, a pick-as-many-as-apply, and a ranking, where what matters is how many people put an option first.', 'footnote' => 'Shares are of the people who answered that question, not of everyone asked, so a question allowing several answers adds up to more than 100%. For a ranking, average position is out of the number of options and lower is better.', 'tier' => 'pro', 'scope' => 'local', 'source' => 'rows', 'rows' => 'aieo_qr_answers_rows', 'registry_id' => 'questionnaire_results.by_answer', 'version' => '1', 'per_page' => 100, 'params' => $aieo_qr_params, 'row_key' => 'answer_key', 'default_sort' => array( 'sort_rank', 'asc' ), 'group_by' => 'question', 'empty_state' => 'No answers with options yet. Score questions are on the question report.', 'columns' => array( array( 'key' => 'answer_key', 'label' => 'Key', 'type' => 'code', 'sortable' => true, 'hidden' => true ), array( 'key' => 'sort_rank', 'label' => 'Order', 'type' => 'integer', 'sortable' => true, 'hidden' => true ), array( 'key' => 'question', 'label' => 'Question', 'type' => 'text', 'sortable' => true, 'sort_key' => 'sort_rank', 'searchable' => true, 'filter' => 'select', 'truncate' => 52, 'tooltip' => true ), array( 'key' => 'answer', 'label' => 'Answer', 'type' => 'text', 'sortable' => true, 'searchable' => true ), array( 'key' => 'share_now', 'label' => 'Share since', 'type' => 'percent', 'format' => array( 'precision' => 0 ), 'sortable' => true, 'empty' => '—' ), array( 'key' => 'share_before', 'label' => 'Share before', 'type' => 'percent', 'format' => array( 'precision' => 0 ), 'sortable' => true, 'empty' => '—' ), array( 'key' => 'share_delta_pp', 'label' => 'Change', 'type' => 'decimal', 'format' => array( 'precision' => 1 ), 'suffix' => ' pp', 'sortable' => true, 'empty' => '—', 'bands' => array( array( 'max' => -1, 'tone' => 'danger' ), array( 'min' => -1, 'max' => 1, 'tone' => 'muted' ), array( 'min' => 1, 'tone' => 'success' ), ), ), array( 'key' => 'count_now', 'label' => 'People since', 'type' => 'integer', 'sortable' => true ), array( 'key' => 'count_before', 'label' => 'People before', 'type' => 'integer', 'sortable' => true ), array( 'key' => 'mean_position', 'label' => 'Average position', 'type' => 'decimal', 'format' => array( 'precision' => 2 ), 'sortable' => true, 'empty' => '—' ), array( 'key' => 'firsts', 'label' => 'Put first', 'type' => 'integer', 'sortable' => true, 'empty' => '—' ), ), ), array( 'id' => 'questionnaire_responses', 'label' => 'Every response, one per person', 'description' => 'One row per survey somebody filled in, newest first, with anything they wrote in their own words. This is where a single customer\'s answers can be read rather than counted.', 'footnote' => 'This opens with the Wrote anything filter set to the people who wrote something, because that is the column worth reading; clear it to see everyone who answered. The order number links to the order and the customer to their account, where they have one -- a guest checkout has no profile to link to. There is deliberately no per-person average here. One survey mixes a 1-5 satisfaction with a 0-10 recommendation, and a mean across two different rulers is a number with no meaning. A response with no customer against it is a guest, or an older one whose order could not be resolved. Written answers are shown in full on the row rather than summarised, because the point of this table is to read them.', 'tier' => 'pro', 'scope' => 'local', 'source' => 'rows', 'rows' => 'aieo_qr_responses_rows', 'registry_id' => 'questionnaire_results.by_response', 'version' => '1', 'per_page' => 50, 'params' => array( 'blueprint' => array( 'type' => 'text', 'default' => 'post_order' ), 'limit' => array( 'type' => 'int', 'default' => 2000, 'options' => array( 100, 500, 2000, 5000 ) ), 'written_only' => array( 'type' => 'text', 'default' => 'all', 'options' => array( 'written', 'all' ) ), ), 'row_key' => 'entry_id', 'default_sort' => array( 'when', 'desc' ), 'empty_state' => 'Nobody has answered this survey yet.', 'capabilities' => array( 'export' => 'manage_woocommerce' ), 'columns' => array( array( 'key' => 'entry_id', 'label' => 'Response', 'type' => 'id', 'sortable' => true ), array( 'key' => 'when', 'label' => 'When', 'type' => 'datetime', 'sortable' => true ), array( 'key' => 'order_id', 'label' => 'Order', 'type' => 'link', 'format' => array( 'href' => 'order_url' ), 'sortable' => true, 'empty' => '—' ), array( 'key' => 'order_url', 'label' => 'Order URL', 'type' => 'text', 'hidden' => true ), array( 'key' => 'customer', 'label' => 'Customer', 'type' => 'link', 'format' => array( 'href' => 'customer_url' ), 'subline' => 'customer_kind', 'sortable' => true, 'empty' => '—' ), array( 'key' => 'customer_kind', 'label' => 'Customer kind', 'type' => 'text', 'hidden' => true ), array( 'key' => 'customer_url', 'label' => 'Customer URL', 'type' => 'text', 'hidden' => true ), array( 'key' => 'lead', 'label' => 'Campaign', 'type' => 'link', 'format' => array( 'href' => 'lead_url' ), 'empty' => '—' ), array( 'key' => 'lead_url', 'label' => 'Campaign URL', 'type' => 'text', 'hidden' => true ), array( 'key' => 'answered', 'label' => 'Answered', 'type' => 'integer', 'sortable' => true ), array( 'key' => 'skipped', 'label' => 'Skipped', 'type' => 'integer', 'sortable' => true ), array( 'key' => 'wrote', 'label' => 'Wrote anything', 'type' => 'badge', 'sortable' => true, 'filter' => 'select', 'format' => array( 'map' => array( 'written' => 'info', 'none' => 'secondary' ), 'labels' => array( 'written' => 'Wrote something', 'none' => 'No comment' ), ), ), array( 'key' => 'comments', 'label' => 'In their own words', 'type' => 'text', 'sortable' => false, 'searchable' => true, 'empty' => '—' ), array( 'key' => 'surface', 'label' => 'Source', 'type' => 'code', 'sortable' => true, 'filter' => 'select', 'hidden' => true ), ), ), );