Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wpmudev-sls/c6048a3058d5666c95a36e2d9a51ff13 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/c6048a3058d5666c95a36e2d9a51ff13 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Number of responses for each question in personality quiz
<?php
/**
* Plugin Name: [Forminator Pro] Number of responses for each question in personality quiz
* Description: Show number of responses for each question in personality quiz.
* Author: Prashant @ WPMUDEV
* Task: SLS-6162
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
add_action( 'forminator_after_form_render', 'wpmudev_quiz_answer_count', 10, 5 );
function wpmudev_quiz_answer_count( $id, $form_type, $post_id, $form_fields, $form_settings ) {
if ( ! is_user_logged_in() ) {
return;
}
if ( 2129 === intval( $id ) && 1 === get_current_user_id() && isset( $_GET['show_count'] ) ) { // Please change the quiz ID.
global $wpdb;
$meta_answers = array();
$table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY_META );
$entry_table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY );
$sql = "SELECT m.meta_value FROM {$table_name} m
LEFT JOIN {$entry_table_name} e ON(e.entry_id = m.entry_id)
WHERE e.form_id = %d
AND m.meta_key = '%s'
AND ( e.draft_id IS NULL OR e.draft_id = '' )
AND e.is_spam = 0";
$quiz_entry = $wpdb->get_results( $wpdb->prepare( $sql, $id, 'entry' ) );
foreach ( $quiz_entry as $kent => $ent ) {
if ( ! empty( $ent->meta_value ) ) {
$meta_data = unserialize( $ent->meta_value );
foreach ( $meta_data as $meta => $data ) {
$meta_answers[] = $data['answer'];
}
}
}
if ( ! empty( $meta_answers ) ) {
$count_answers = array_count_values( $meta_answers );
echo '<table>';
foreach ( $count_answers as $ckey => $cvalue ) {
echo '<tr><td>' . $ckey . '</td><td>' . $cvalue . '</td></tr>';
}
echo '</table>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment