Action: Poll

Current version: 0.3
Compatible with: R6.0
Credits: Evaggelos Balaskas

{{poll q="Do you like WackoWiki?" a="Yes:No"}}


/action/poll.php

<?php

if (!defined('IN_WACKO'))
{
    exit;
}

// WackoWiki Poll, Delimeter is ":"
// eg. {{poll q="Do you like WackoWiki?" a="Yes:No"}}
?>
<!-- Action Poll -->

<!--Load the AJAX API-->
<?php
$this->add_html('header', '<script src="https://www.gstatic.com/charts/loader.js"></script>');

$answers    = explode(':', $a);
$filename    = UPLOAD_GLOBAL_DIR . '/polls_' . preg_replace('/\//', '_', $this->tag) . '_' . hash('sha1', $q);

if (!file_exists($filename))
{
    $fp = fopen($filename, 'w');

    foreach ($answers as $ans)
    {
        $data .= $ans . '=0:';
    }

    fwrite($fp, substr($data, 0, -1));
    fclose($fp);
}

if (!empty($_POST['answer']))
{
    $data        = '';
    $i            = 0;

    $poll        = file($filename);
    $results    = explode(':', $poll[0]);

    foreach ($results as $vote)
    {
        $res = explode('=', $vote);

        if ( $res[0] == $_POST['answer'] )
        {
            $res[1]++;
        }

        $data        .= $res[0] . '=' . $res[1] . ':';
        $sum[$i][0]     = $res[0];
        $sum[$i][1]     = $res[1];

        $i++;
    }

    # Ut::debug_print_r($sum);
?>

<p><strong><?php echo $q; ?></strong></p>
<p>Poll Results:</p>
<script type="text/javascript">

    // Load the Visualization API and the corechart package.
    google.charts.load('current', {'packages':['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.charts.setOnLoadCallback(drawChart);

    // Callback that creates and populates a data table,
    // instantiates the pie chart, passes in the data and
    // draws it.
    function drawChart() {

        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Topping');
        data.addColumn('number', 'Slices');
        data.addRows([
            <?php foreach ($sum as $result)
            {
                echo '["' .$result[0] . '", ' . $result[1] .']';

                if (end($sum) !== $result) echo ',' . "\n";
            }
            ?>
        ]);

        // Set chart options
        var options = {'title':'<?php echo $q; ?>',
                        'width':800,
                        'height':600};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    }
</script>
<!-- Div that will hold the pie chart-->
<div id="chart_div"></div>
<p> </p>
<?php
    $fp = fopen($filename, 'w');
    fwrite($fp, substr($data, 0, -1));
    fclose($fp);

    $url = $this->config['base_url'] . $this->tag;
    echo '<a href="' . $url . '">Return</a>';
}
else
{
    echo $this->form_open('poll');
    ?>
    <table border="0" cellpadding="3" cellspacing="0">
        <tr>
            <td colspan="2"><strong><?php echo $q; ?></strong></td>
        </tr>
    <?php
    foreach ($answers as $answer)
    {
    ?>
        <tr>
            <td><?php echo $answer; ?></td>
            <td>
                <input name="answer" value="<?php echo $answer; ?>" type="radio">
            </td>
        </tr>
        <?php
    }
    ?>
        <tr>
            <td colspan="2"><input type="submit" value="Count me In"></td>
        </tr>
    </table>
    </form>
    <?php
}

1. Documentation

The code isn't perfect - i am still working on this.
I've just add https://developers.google.com/chart/


ATTENTION You must not change the answers!
I am working to find a good solution on this.


the result is something like this:

2. How to


for users available for copy and paste

2.1. Localization (optional)

put this at the end of your language file


lang/custom.<lang>.php

//hacks
'YourPhrase' => 'your translation here',	

[xyz] proposed translations
[de] ...
[fr]
[ru]

3. To Do

  • fix php code
  • add template
  • translate (if necessary)

Referring pages:

  1. Dev/PatchesHacks

Files

google_charts_example.png 8.3 KiB  13.12.2020 11:18