Poll Plugin

Creating and modifying plugins.
Mecca
Regular
Posts: 15
Joined: Tue Nov 09, 2004 1:08 pm
Location: Australia
Contact:

Poll Plugin

Post by Mecca » Tue Nov 09, 2004 1:36 pm

Hey all,
Was wondering if anyone could point me in the direction of a poll plugin?

wese
Regular
Posts: 8
Joined: Mon Nov 08, 2004 2:01 pm
Contact:

Re: Poll Plugin

Post by wese » Tue Nov 09, 2004 9:29 pm

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).
Last edited by wese on Tue Nov 09, 2004 10:12 pm, edited 1 time in total.
ImageDo your Part! Join now.

Mecca
Regular
Posts: 15
Joined: Tue Nov 09, 2004 1:08 pm
Location: Australia
Contact:

Post by Mecca » Tue Nov 09, 2004 9:41 pm

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

User avatar
tadpole
Regular
Posts: 88
Joined: Fri Oct 08, 2004 6:20 am
Location: 33°6'4.079" North, 117°3'6.563" West
Contact:

Post by tadpole » Wed Nov 10, 2004 7:54 am

/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... :)

Mecca
Regular
Posts: 15
Joined: Tue Nov 09, 2004 1:08 pm
Location: Australia
Contact:

Post by Mecca » Wed Nov 10, 2004 2:56 pm

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

User avatar
tadpole
Regular
Posts: 88
Joined: Fri Oct 08, 2004 6:20 am
Location: 33°6'4.079" North, 117°3'6.563" West
Contact:

Post by tadpole » Wed Nov 10, 2004 3:44 pm

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).

Mecca
Regular
Posts: 15
Joined: Tue Nov 09, 2004 1:08 pm
Location: Australia
Contact:

Post by Mecca » Wed Nov 10, 2004 4:24 pm

Hey Even this really is a great script works perfectly :D

Mecca
Regular
Posts: 15
Joined: Tue Nov 09, 2004 1:08 pm
Location: Australia
Contact:

Post by Mecca » Wed Nov 10, 2004 7:53 pm

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 :)

wese
Regular
Posts: 8
Joined: Mon Nov 08, 2004 2:01 pm
Contact:

Post by wese » Wed Nov 10, 2004 9:49 pm

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. :)
ImageDo your Part! Join now.

User avatar
zpage
Posts: 2
Joined: Thu Dec 23, 2004 10:57 pm

Plguin in German

Post by zpage » Thu Dec 23, 2004 11:07 pm

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

djmac
Regular
Posts: 6
Joined: Sun Jan 02, 2005 8:34 am

polling

Post by djmac » Tue Jan 04, 2005 1:08 am

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

pano

Layout problem

Post by pano » Thu Mar 03, 2005 6:49 pm

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!

davecjr
Regular
Posts: 167
Joined: Fri Oct 29, 2004 3:09 pm
Contact:

Post by davecjr » Wed Apr 27, 2005 9:52 pm

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!

User avatar
garvinhicking
Core Developer
Posts: 30020
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking » Thu Apr 28, 2005 2:06 pm

davecjr: I think this is intentional; you already have voted and cannot vote again, so it shows you the results.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

Chindogu

Post by Chindogu » Fri May 06, 2005 11:05 pm

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

Post Reply