Poll Plugin
Posted: Tue Nov 09, 2004 12:36 pm
Hey all,
Was wondering if anyone could point me in the direction of a poll plugin?
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.Mecca wrote:Hey all,
Was wondering if anyone could point me in the direction of a poll plugin?
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 : */
?>
I got a lil problem with my template.. The option Button is in the second line.. I will find it.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.
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 : */
?>
Hi. So does this problem have a solution? It's very annoying and just doesn't look good at all.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