Home / Attachments / Una plant Quiz Score Calculator
Duplicate Snippet

Embed Snippet on Your Site

Una plant Quiz Score Calculator

<10
Code Preview
php
<?php
/**
 * Author: Sumaiya, Clickup Doc: https://app.clickup.com/36636088/v/dc/12y1dr-22535/12y1dr-25375
 * Function to handle quiz submission, calculate results based on user answers, and return the response for FlowMattic.
 *
 * @param array $answers The array containing quiz answers from the form
 * @return array The results including counts and the winning type
 */
function handle_quiz_submission($answers) {
    // Initialize counts for each answer type (A, B, C, D)
    $counts = [
        'A' => 0,
        'B' => 0,
        'C' => 0,
        'D' => 0,
    ];
    // Iterate over the given answers
    foreach ($answers as $question => $answer) {
        if (isset($counts[$answer])) {
            $counts[$answer]++;
        }
    }
    // Determine the highest count to find the winner
    $max_count = max($counts);
    $winners = []; // Array to hold types with the maximum count
    // Collect all types that have the maximum count
    foreach ($counts as $type => $count) {
        if ($count === $max_count) {
            $winners[] = $type; // Add to winners if tied
        }
    }
    // Logic for handling ties
    if (count($winners) > 1) {
        // If there is a tie, randomly select one of the tied types as the winner
        $winner = $winners[array_rand($winners)];
    } else {
        // Only one winner
        $winner = $winners[0];
    }
    // Prepare the response with counts and winner information, formatted for FlowMattic
    return [
        'success' => true,
        'data_counts_count_type_a' => $counts['A'],
        'data_counts_count_type_b' => $counts['B'],
        'data_counts_count_type_c' => $counts['C'],
        'data_counts_count_type_d' => $counts['D'],
        'data_quiz_winner_type' => $winner, // Provide the winning type directly as A, B, C, or D
    ];
}

Comments

Add a Comment