blueprint_id = AIEO_Survey_Blueprint::code( $blueprint_id ); } public function surface() { return '' === $this->blueprint_id ? '' : 'aieo_' . $this->blueprint_id; } public function label() { $bp = AIEO_Survey_Blueprint::get( $this->blueprint_id ); return $bp ? $bp['label'] : $this->blueprint_id; } public function available() { return (bool) AIEO_Survey_Blueprint::get( $this->blueprint_id ); } public function blueprint_id() { return $this->blueprint_id; } public function field_catalog() { $out = array(); foreach ( AIEO_Survey_Blueprint::question_codes( $this->blueprint_id ) as $code ) { $q = AIEO_Survey_Blueprint::question( $code ); if ( ! $q ) { continue; } $out[ $code ] = array( 'key' => $code, 'label' => $q['label'], 'admin_label' => $code, 'shape' => self::shape_of( $q ), 'choices' => $q['choices'], ); } return $out; } public static function shape_of( array $q ) { if ( 'text' === $q['answers'] ) { return 'text'; } if ( 'yes' === $q['ordered'] ) { return 'ranking'; } if ( 'scale' === $q['scored'] ) { return 'scale'; } if ( 'number' === $q['answers'] ) { return 'number'; } return 'choice'; } public function observed_values( $field_key ) { global $wpdb; $entries = AIEO_Form_CPT::entries_table(); $values = AIEO_Form_CPT::entry_values_table(); $rows = (array) $wpdb->get_results( $wpdb->prepare( "SELECT COALESCE(v.choice_key, v.value_text) AS val, COUNT(DISTINCT v.entry_id) AS hits FROM {$values} v JOIN {$entries} e ON e.id = v.entry_id WHERE e.surface = %s AND v.field_key = %s AND v.disposition = 'answered' GROUP BY val ORDER BY hits DESC LIMIT 200", $this->surface(), (string) $field_key ), ARRAY_A ); $out = array(); foreach ( $rows as $r ) { if ( null !== $r['val'] && '' !== (string) $r['val'] ) { $out[ (string) $r['val'] ] = (int) $r['hits']; } } return $out; } public function entry_ids( array $args = array() ) { global $wpdb; $entries = AIEO_Form_CPT::entries_table(); $after = isset( $args['after_id'] ) ? (int) $args['after_id'] : 0; $limit = isset( $args['limit'] ) ? max( 1, (int) $args['limit'] ) : 500; $since = isset( $args['since'] ) ? (string) $args['since'] : ''; $sql = "SELECT id FROM {$entries} WHERE surface = %s AND id > %d"; $vals = array( $this->surface(), $after ); if ( '' !== $since ) { $sql .= ' AND created_at >= %s'; $vals[] = $since; } $sql .= ' ORDER BY id ASC LIMIT %d'; $vals[] = $limit; return array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( $sql, $vals ) ) ); } public function entry( $entry_id ) { global $wpdb; $entries = AIEO_Form_CPT::entries_table(); $values = AIEO_Form_CPT::entry_values_table(); $entry_id = (int) $entry_id; $head = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$entries} WHERE id = %d AND surface = %s", $entry_id, $this->surface() ), ARRAY_A ); if ( ! $head ) { return null; } $rows = (array) $wpdb->get_results( $wpdb->prepare( "SELECT field_key, disposition, choice_key, value_num, value_text FROM {$values} WHERE entry_id = %d", $entry_id ), ARRAY_A ); return array( 'rows' => $rows, 'context' => array( 'user_id' => (int) $head['user_id'], 'order_id' => (int) $head['order_id'], 'eponymous_id' => (int) $head['eponymous_id'], 'run_id' => (int) $head['run_id'], 'source' => (string) $head['source'], 'created_at' => (string) $head['created_at'], ), ); } public function record( array $answers, array $context = array() ) { $bp = AIEO_Survey_Blueprint::get( $this->blueprint_id ); if ( ! $bp ) { return new WP_Error( 'aieo_survey_blueprint', 'Unknown blueprint.' ); } if ( method_exists( 'AIEO_Form_CPT', 'maybe_install_tables' ) ) { AIEO_Form_CPT::maybe_install_tables(); } $not_shown = array(); foreach ( (array) ( isset( $context['not_shown'] ) ? $context['not_shown'] : array() ) as $c ) { $not_shown[ AIEO_Survey_Blueprint::code( $c ) ] = true; } foreach ( AIEO_Survey_Blueprint::question_codes( $this->blueprint_id ) as $code ) { $q = AIEO_Survey_Blueprint::question( $code ); if ( $q && ! AIEO_Survey_Blueprint::depends_met( $q, $answers ) ) { $not_shown[ $code ] = true; } } $rows = array(); foreach ( AIEO_Survey_Blueprint::question_codes( $this->blueprint_id ) as $code ) { $q = AIEO_Survey_Blueprint::question( $code ); if ( ! $q ) { continue; } if ( isset( $not_shown[ $code ] ) ) { $rows[] = array( 'field_key' => $code, 'disposition' => 'not_shown', 'choice_key' => null, 'value_num' => null, 'value_text' => null ); continue; } $given = array_key_exists( $code, $answers ) ? $answers[ $code ] : null; $made = $this->rows_for( $q, $given ); if ( $made ) { $rows = array_merge( $rows, $made ); } else { $rows[] = array( 'field_key' => $code, 'disposition' => 'skipped', 'choice_key' => null, 'value_num' => null, 'value_text' => null ); } } return AIEO_Form_Entries::record_external( $this->surface(), isset( $context['external_id'] ) ? (int) $context['external_id'] : self::mint_external_id(), $rows, array_merge( array( 'source' => 'aieo' ), $context ) ); } private function mint_external_id() { global $wpdb; $entries = AIEO_Form_CPT::entries_table(); $max = (int) $wpdb->get_var( $wpdb->prepare( "SELECT MAX(external_id) FROM {$entries} WHERE surface = %s", $this->surface() ) ); return $max + 1; } private function rows_for( array $q, $given ) { $code = $q['code']; if ( 'text' === $q['answers'] ) { $txt = trim( (string) $given ); if ( '' === $txt ) { return array(); } return array( array( 'field_key' => $code, 'disposition' => 'answered', 'choice_key' => null, 'value_num' => null, 'value_text' => mb_substr( wp_check_invalid_utf8( $txt ), 0, 5000 ), ) ); } if ( 'number' === $q['answers'] ) { if ( ! is_numeric( $given ) ) { return array(); } return array( array( 'field_key' => $code, 'disposition' => 'answered', 'choice_key' => null, 'value_num' => (float) $given, 'value_text' => null, ) ); } if ( 'yes' === $q['ordered'] ) { $order = is_array( $given ) ? $given : array_filter( array_map( 'trim', explode( ',', (string) $given ) ) ); $rows = array(); $pos = 1; foreach ( $order as $c ) { $c = AIEO_Survey_Blueprint::code( $c ); if ( '' === $c || ! isset( $q['choices'][ $c ] ) ) { continue; } $rows[] = array( 'field_key' => $code, 'disposition' => 'answered', 'choice_key' => $c, 'value_num' => $pos, 'value_text' => null, ); $pos++; } return $rows; } if ( 'scale' === $q['scored'] ) { if ( is_array( $given ) ) { $rows = array(); foreach ( $given as $subject => $n ) { if ( ! is_numeric( $n ) ) { continue; } $rows[] = array( 'field_key' => $code, 'disposition' => 'answered', 'choice_key' => mb_substr( (string) $subject, 0, 64 ), 'value_num' => (float) $n, 'value_text' => null, ); } return $rows; } if ( ! is_numeric( $given ) ) { return array(); } $n = (float) $given; if ( $n < $q['min'] || $n > $q['max'] ) { return array(); } return array( array( 'field_key' => $code, 'disposition' => 'answered', 'choice_key' => null, 'value_num' => $n, 'value_text' => null, ) ); } $chosen = is_array( $given ) ? $given : array( $given ); $rows = array(); foreach ( $chosen as $c ) { $c = AIEO_Survey_Blueprint::code( $c ); if ( '' === $c || ! isset( $q['choices'][ $c ] ) ) { continue; } $rows[] = array( 'field_key' => $code, 'disposition' => 'answered', 'choice_key' => $c, 'value_num' => ( 'correct' === $q['scored'] ) ? ( $c === $q['answer_key'] ? 1 : 0 ) : null, 'value_text' => null, ); if ( 'one' === $q['cardinality'] ) { break; } } return $rows; } }