Page 1 of 3

Poll Plugin

Posted: Tue Nov 09, 2004 12:36 pm
by Mecca
Hey all,
Was wondering if anyone could point me in the direction of a poll plugin?

Re: Poll Plugin

Posted: Tue Nov 09, 2004 8:29 pm
by wese
Mecca wrote:Hey all,
Was wondering if anyone could point me in the direction of a poll plugin?
Currently i only know "Evan Nemerson"'s Blog (Here) he develops an PollBox Plugin, but hasnt yet been public released it.

U can workaround with an Html Block and use an IFrame, or try with the PhpBox Plugin(Here).

Posted: Tue Nov 09, 2004 8:41 pm
by Mecca
That PollBox Plugin looks to br exactly what i would like :). Your PhpBox Plugin looks pretty damn fine too, i can think of so many uses for it :) including building a poll script :D
Thanks for your help
Cam

Posted: Wed Nov 10, 2004 6:54 am
by tadpole
/me wonders why his name is surrounded by quotation marks...

Here's what I have on my blog right now. The problem is that the configuration is a bit unfriendly... and I think I wrote this before there was an event hook in the CSS file, so everything is hard-coded. If you still want the code, though...

Code: Select all

<?php # $Id$

switch ($serendipity['lang']) {
    case 'en':
    default: {
        @define('PLUGIN_POLLBOX_NAME',            'Poll Box');
        @define('PLUGIN_POLLBOX_DESC',            'Create a poll in the sidebar.');
        @define('PLUGIN_POLLBOX_NUMCHOICES',      'Number of options');
        @define('PLUGIN_POLLBOX_CHOICE',          'Option #%d');
        @define('PLUGIN_POLLBOX_QUESTION',        'Question');
        @define('PLUGIN_POLLBOX_NUMCHOICES_DESC', 'The number of options the user will have to choose from.');
        @define('PLUGIN_POLLBOX_CHOICE_DESC',     'The text for option #%d.');
        @define('PLUGIN_POLLBOX_QUESTION_DESC',   'The question you want to ask.');
        @define('PLUGIN_POLLBOX_TOTALVOTES',      'Total of %d votes.');
        @define('PLUGIN_POLLBOX_VOTE',            'Vote');
        @define('PLUGIN_POLLBOX_SHOWRESULTS',     'Show results.');
        @define('PLUGIN_POLLBOX_RESET',           'Reset');
    }
    break;
}

class serendipity_plugin_pollbox extends serendipity_plugin {
    function introspect(&$propbag)
    {
        global $serendipity;

        if ( $this->get_config('reset', 'unset') != 'unset' ) {
            /* This is what doesn't work. Can't figure out how to
               clear title, question, pollbox_number_choices, and
               reset. */
            serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}config WHERE name LIKE '{$this->instance}%';");

            $plugin_l = strlen($this->instance);
            foreach ( $serendipity as $key => $value ) {
                if ( substr($key, 0, $plugin_l) == $this->instance ) {
                    unset($serendipity[$key]);
                }
            }
        }

        $conf_items = array('title', 'question', 'pollbox_number_choices');
        $numchoices = $this->get_config('pollbox_number_choices', '');
        if ( is_numeric($numchoices) ) {
            for ( $x = 1 ; $x <= $numchoices ; $x++ ) {
                $conf_items[] = 'choice_'.$x;
            }
        }
        $conf_items[] = 'reset';

        $propbag->add('name', PLUGIN_POLLBOX_NAME);
        $propbag->add('description', PLUGIN_POLLBOX_DESC);
        $propbag->add('configuration', $conf_items);
        $propbag->add('event_hooks', array('frontend_configure' => true));
    }

    function introspect_config_item($name, &$propbag)
    {
        global $serendipity;

        if ( $name == 'pollbox_number_choices' ) {
            $propbag->add('type', 'string');
            $propbag->add('name', PLUGIN_POLLBOX_NUMCHOICES);
            $propbag->add('description', PLUGIN_POLLBOX_NUMCHOICES_DESC);
        }
        elseif ( $name == 'title' ) {
            $propbag->add('type',          'string');
            $propbag->add('name',          TITLE);
            $propbag->add('description',   TITLE);
            $propbag->add('default',       '');
        }
        elseif ( $name == 'question' ) {
            $propbag->add('type',          'string');
            $propbag->add('name',          PLUGIN_POLLBOX_QUESTION);
            $propbag->add('description',   PLUGIN_POLLBOX_QUESTION_DESC);
            $propbag->add('default',       '');
        }
        elseif ( $name == 'reset' ) {
            $propbag->add('type',          'checkbox');
            $propbag->add('name',          PLUGIN_POLLBOX_RESET);
            $propbag->add('description',   PLUGIN_POLLBOX_RESET);
        }
        elseif ( preg_match('/^choice_([0-9]*)$/', $name, $num) ) {
            $propbag->add('type', 'string');
            $propbag->add('name', sprintf(PLUGIN_POLLBOX_CHOICE, $num[1]));
            $propbag->add('description', sprintf(PLUGIN_POLLBOX_CHOICE_DESC, $num[1]));
        }

        return true;
    }

    function generate_content(&$title) {
        global $serendipity;

        $title = $this->get_config('title');
        $numchoices = $this->get_config('pollbox_number_choices');
        if ( $_REQUEST['serendipity_pollChoice'] && is_numeric( $_REQUEST['serendipity_pollChoice']) ) {
            $_SESSION['serendipity_hasVoted'] = true;

            $choice = $_REQUEST['serendipity_pollChoice'];
            $this->set_config('votes_' . $choice, $t = $this->get_config('votes_' . $choice, 0) + 1);
            $this->set_config('total_votes', $t = $this->get_config('total_votes', 0) + 1);
        }

        $showResults = (isset($_SESSION['serendipity_hasVoted']) || $_GET['pollbox_showResults']) ? true : false;

        echo '<form method="post" action="' . htmlentities($_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']) . '">';
        echo $this->get_config('question', '') . "<br/>\n";
        for ( $i = 1 ; $i <= $numchoices ; $i++ ) {
            $total_votes = $this->get_config('total_votes', 0);
            $choice = $this->get_config('choice_' . $i);

            if ( $showResults ) {
                $width = (($total_votes > 0) ? (($this->get_config('votes_' . $i, 0) / $total_votes) * 100) : 0);

                echo '<div style="padding-top: 0.5em;">' . $choice . ($showResults ? ' (' . round($width) . '%)' : '') . '</div>';

                echo '<div style="display: block; width: 100%; height: 1em; border: thin black solid;">';
                if ( $width > 0 ) {
                    echo '<div style="width: ' . $width . '%; height: 100%; background: black; border: thin white solid;"></div>';
                }
                echo "</div>";
            }

            if ( !isset($_SESSION['serendipity_hasVoted']) ) {
                echo '<div><input type="radio" name="serendipity_pollChoice" value="' . $i .'"/>' . ($showResults ? '' : $choice) . '</div>';
            }
        }

        if ( !isset($_SESSION['serendipity_hasVoted']) ) {
            echo '<div><input type="submit" name="submit" value="' . PLUGIN_POLLBOX_VOTE . '"/></div>';
        }

        echo '<div>';
        if ( $showResults ) {
            printf(PLUGIN_POLLBOX_TOTALVOTES, $total_votes);
        }
        else {
          echo '<a href="?pollbox_showResults=true">' . PLUGIN_POLLBOX_SHOWRESULTS . '</a>';
        }
        echo '</div>';
        echo '</form>';
    }
}

/* vim: set sts=4 ts=4 expandtab : */
?>
Don't get mad at me if the planet starts spinning in reverse or anything... :)

Posted: Wed Nov 10, 2004 1:56 pm
by Mecca
LOL nope the planet didnt start spinning in reverse, and thankyou very much for the code :)
Just one question where abouts do i add the the text for the options ?

-----------
Poll

Question EG: What color is the sky

Option 1 EG: Blue
Option 2 EG: Red
Option 3 EG: Green
-------------

And Thanks again for the code

Posted: Wed Nov 10, 2004 2:44 pm
by tadpole
In the configuration screen, change "Number of options" to three and press save. Then press it again (one of the reasons I haven't really published the code). Options number one, two, and three should then be blue, red, and green. This time you only have to press save once (methinks).

Posted: Wed Nov 10, 2004 3:24 pm
by Mecca
Hey Even this really is a great script works perfectly :D

Posted: Wed Nov 10, 2004 6:53 pm
by Mecca
Um ok maybe i was a little hasty lol, the script does work great i just have a small problem with the options text all the options apear about 10 characters to the right of there respective radio button putting things out of alignment. I'm noy a guru php coder, so could you point me in the right direction to fix this small problem?
again thanx for your help :)

Posted: Wed Nov 10, 2004 8:49 pm
by wese
Mecca wrote:i just have a small problem with the options text all the options apear about 10 characters to the right of there respective radio button putting things out of alignment.
I got a lil problem with my template.. The option Button is in the second line.. I will find it. ;)

But Hey works great.


Sorry about the Caps and many thanks for the script. :)

Plguin in German

Posted: Thu Dec 23, 2004 10:07 pm
by zpage
I've just translated the Plugin into german language. Here's the code:

Code: Select all

<?php # $Id$

switch ($serendipity['lang']) {
    case 'de':
    default: {
        @define('PLUGIN_POLLBOX_NAME',            'Poll Box');
        @define('PLUGIN_POLLBOX_DESC',            'Ein Poll in der Sidebar erstellen.');
        @define('PLUGIN_POLLBOX_NUMCHOICES',      'Anzahl der Antworten');
        @define('PLUGIN_POLLBOX_CHOICE',          'Antwort #%d');
        @define('PLUGIN_POLLBOX_QUESTION',        'Frage');
        @define('PLUGIN_POLLBOX_NUMCHOICES_DESC', 'Die Anzahl der Antworten die der User wählen kann.');
        @define('PLUGIN_POLLBOX_CHOICE_DESC',     'Der Text für Antwort #%d.');
        @define('PLUGIN_POLLBOX_QUESTION_DESC',   'Die Frage die gestellt werden soll.');
        @define('PLUGIN_POLLBOX_TOTALVOTES',      '%d Votes');
        @define('PLUGIN_POLLBOX_VOTE',            'Vote');
        @define('PLUGIN_POLLBOX_SHOWRESULTS',     'Ergebnisse');
        @define('PLUGIN_POLLBOX_RESET',           'Reset');
    }
    break;
}

class serendipity_plugin_pollbox extends serendipity_plugin {
    function introspect(&$propbag)
    {
        global $serendipity;

        if ( $this->get_config('reset', 'unset') != 'unset' ) {
            /* This is what doesn't work. Can't figure out how to
               clear title, question, pollbox_number_choices, and
               reset. */
            serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}config WHERE name LIKE '{$this->instance}%';");

            $plugin_l = strlen($this->instance);
            foreach ( $serendipity as $key => $value ) {
                if ( substr($key, 0, $plugin_l) == $this->instance ) {
                    unset($serendipity[$key]);
                }
            }
        }

        $conf_items = array('title', 'question', 'pollbox_number_choices');
        $numchoices = $this->get_config('pollbox_number_choices', '');
        if ( is_numeric($numchoices) ) {
            for ( $x = 1 ; $x <= $numchoices ; $x++ ) {
                $conf_items[] = 'choice_'.$x;
            }
        }
        $conf_items[] = 'reset';

        $propbag->add('name', PLUGIN_POLLBOX_NAME);
        $propbag->add('description', PLUGIN_POLLBOX_DESC);
        $propbag->add('configuration', $conf_items);
        $propbag->add('event_hooks', array('frontend_configure' => true));
    }

    function introspect_config_item($name, &$propbag)
    {
        global $serendipity;

        if ( $name == 'pollbox_number_choices' ) {
            $propbag->add('type', 'string');
            $propbag->add('name', PLUGIN_POLLBOX_NUMCHOICES);
            $propbag->add('description', PLUGIN_POLLBOX_NUMCHOICES_DESC);
        }
        elseif ( $name == 'title' ) {
            $propbag->add('type',          'string');
            $propbag->add('name',          TITLE);
            $propbag->add('description',   TITLE);
            $propbag->add('default',       '');
        }
        elseif ( $name == 'question' ) {
            $propbag->add('type',          'string');
            $propbag->add('name',          PLUGIN_POLLBOX_QUESTION);
            $propbag->add('description',   PLUGIN_POLLBOX_QUESTION_DESC);
            $propbag->add('default',       '');
        }
        elseif ( $name == 'reset' ) {
            $propbag->add('type',          'checkbox');
            $propbag->add('name',          PLUGIN_POLLBOX_RESET);
            $propbag->add('description',   PLUGIN_POLLBOX_RESET);
        }
        elseif ( preg_match('/^choice_([0-9]*)$/', $name, $num) ) {
            $propbag->add('type', 'string');
            $propbag->add('name', sprintf(PLUGIN_POLLBOX_CHOICE, $num[1]));
            $propbag->add('description', sprintf(PLUGIN_POLLBOX_CHOICE_DESC, $num[1]));
        }

        return true;
    }

    function generate_content(&$title) {
        global $serendipity;

        $title = $this->get_config('title');
        $numchoices = $this->get_config('pollbox_number_choices');
        if ( $_REQUEST['serendipity_pollChoice'] && is_numeric( $_REQUEST['serendipity_pollChoice']) ) {
            $_SESSION['serendipity_hasVoted'] = true;

            $choice = $_REQUEST['serendipity_pollChoice'];
            $this->set_config('votes_' . $choice, $t = $this->get_config('votes_' . $choice, 0) + 1);
            $this->set_config('total_votes', $t = $this->get_config('total_votes', 0) + 1);
        }

        $showResults = (isset($_SESSION['serendipity_hasVoted']) || $_GET['pollbox_showResults']) ? true : false;

        echo '<form method="post" action="' . htmlentities($_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']) . '">';
        echo $this->get_config('question', '') . "<br/>\n";
        for ( $i = 1 ; $i <= $numchoices ; $i++ ) {
            $total_votes = $this->get_config('total_votes', 0);
            $choice = $this->get_config('choice_' . $i);

            if ( $showResults ) {
                $width = (($total_votes > 0) ? (($this->get_config('votes_' . $i, 0) / $total_votes) * 100) : 0);

                echo '<div style="padding-top: 0.5em;">' . $choice . ($showResults ? ' (' . round($width) . '%)' : '') . '</div>';

                echo '<div style="display: block; width: 100%; height: 1em; border: thin black solid;">';
                if ( $width > 0 ) {
                    echo '<div style="width: ' . $width . '%; height: 100%; background: black; border: thin white solid;"></div>';
                }
                echo "</div>";
            }

            if ( !isset($_SESSION['serendipity_hasVoted']) ) {
                echo '<div><input type="radio" name="serendipity_pollChoice" value="' . $i .'"/>' . ($showResults ? '' : $choice) . '</div>';
            }
        }

        if ( !isset($_SESSION['serendipity_hasVoted']) ) {
            echo '<div><input type="submit" name="submit" value="' . PLUGIN_POLLBOX_VOTE . '"/></div>';
        }

        echo '<div>';
        if ( $showResults ) {
            printf(PLUGIN_POLLBOX_TOTALVOTES, $total_votes);
        }
        else {
          echo '<a href="?pollbox_showResults=true">' . PLUGIN_POLLBOX_SHOWRESULTS . '</a>';
        }
        echo '</div>';
        echo '</form>';
    }
}

/* vim: set sts=4 ts=4 expandtab : */
?>
To install, create a new folder called "serendipity_plugin_pollbox" in your "plugins" directory. Copy the Plugin file in there and name it to "serendipity_plugin_pollbox.php".

Zpage

polling

Posted: Tue Jan 04, 2005 12:08 am
by djmac
I Love the plugin. It setup quick and easy to use 4 stars. But, I have one question. After voting the poll doesn't go back to it's polling state. Is this normal? or can I fix this.


Thank you
Don McLaughlin

Layout problem

Posted: Thu Mar 03, 2005 5:49 pm
by pano
Hi,

the plugin is excellent... better than use the phpbox, i think.
My problem is that i get the radio button in a line over the text of its option, and i tried everything, but i dont know how to make it...

Someone can give a little help?

Thanks in advance!

Posted: Wed Apr 27, 2005 9:52 pm
by davecjr
I have the same problem as above about not going back to the polling state. After you view the results, it just always shows the results!

I hope it's not just me!

Posted: Thu Apr 28, 2005 2:06 pm
by garvinhicking
davecjr: I think this is intentional; you already have voted and cannot vote again, so it shows you the results.

Regards,
Garvin

Posted: Fri May 06, 2005 11:05 pm
by Chindogu
Mecca wrote:Um ok maybe i was a little hasty lol, the script does work great i just have a small problem with the options text all the options apear about 10 characters to the right of there respective radio button putting things out of alignment. I'm noy a guru php coder, so could you point me in the right direction to fix this small problem?
again thanx for your help :)
Hi. So does this problem have a solution? It's very annoying and just doesn't look good at all.

David