Foren / Cotonti / Core Labs / Archive / Poll

oc
#1 29. August 2008, 07:57
(If I posted this in the wrong section, sorry. I didn't know where to post)

This is the only contribution I made so far for this community, sorry Trustmaster, sorry all.

This is basically an ajax poll with a core hack and some stupid effects I saw on the other sites and see no reason to not to steal the ideas. Inspired by JQuery plugins, thanks whoever did it, but code belongs to me, I found out it is bullshit to code that much,

Normal plugins would have create queries and pick up datas one by one, but I tested it, and this is faster.

--This is a core hack. I will never release another plugin about this, this is 2008 my babies :)
-I know Trustmaster stopped believing me doing something about this, but I simplified the code, I am using this also in my personal site, so I believe this is bug-free.
-Use UTF8 (without bom)
-Also you need JQuery.

Well, the code is:

plugins/recentitems/recentsitems.php -- change the whole poll function with:
/* ------------------ */
function sed_get_latestpolls($limit, $mask)
	{
	global $L, $db_polls, $db_polls_voters, $db_polls_options, $usr, $plu_empty;

	require("plugins/recentitems/lang/recentitems.en.lang.php");
	
	$sql_p = sed_sql_query("SELECT poll_id, poll_text FROM $db_polls WHERE 1 AND poll_state=0  AND poll_type=0 ORDER by poll_creationdate DESC LIMIT $limit");

	while ($row_p = sed_sql_fetcharray($sql_p))
		{
		unset($res);
		$poll_id = $row_p['poll_id'];

		if ($usr['id']>0)
	 		{ $sql2 = sed_sql_query("SELECT pv_id FROM $db_polls_voters WHERE pv_pollid='$poll_id' AND (pv_userid='".$usr['id']."' OR pv_userip='".$usr['ip']."') LIMIT 1"); }
	       else
	 		{ $sql2 = sed_sql_query("SELECT pv_id FROM $db_polls_voters WHERE pv_pollid='$poll_id' AND pv_userip='".$usr['ip']."' LIMIT 1"); }

		if (sed_sql_numrows($sql2)>0)
			{
			$alreadyvoted =1;
			$sql2 = sed_sql_query("SELECT SUM(po_count) FROM $db_polls_options WHERE po_pollid='$poll_id'");
			$totalvotes = sed_sql_result($sql2,0,"SUM(po_count)");
			}
		else
			{ $alreadyvoted =0; }


		$res .= (!$alreadyvoted) ? "<script type=\"text/javascript\">

function postp()
	{

		var id = $(\"input[@name='id']\").attr(\"value\");
		var a = $(\"input[@name='a']\").attr(\"value\");
		var vote = $(\"input[@name='vote']:checked\").attr(\"value\");


	$.ajax({
		type: 'GET',
		url: 'polls.php?',
		data: 'id='+id+'&a='+a+'&vote='+vote+'&mode=ajax',

		beforeSend: function(){
			if (!vote) {
			alert('".$L_idx['vote_opt']."');
			return false;
			}
			$('#poll-container').addClass('loading');
			},

		success: function(msg){
		$('#poll-container').removeClass('loading');
		$('#poll-container').html(msg).hide().stop().fadeIn('slow');
			},
		error: function(msg){
		$('#poll-container').removeClass('loading');
		alert('".$L_idx['vote_failed']."');
			}

		});

		return false;

	}

	</script>" : '';


		$res .= "<h5>".sed_parse(sed_cc($row_p['poll_text']), 1, 1, 1)."</h5>";
		$res .= "<div id='poll-container'>";


		$res .= ($alreadyvoted) ? '<table class="cells">' : '';

		$sql = sed_sql_query("SELECT po_id, po_text, po_count FROM $db_polls_options WHERE po_pollid='$poll_id' ORDER by po_id ASC");

		while ($row = sed_sql_fetcharray($sql))
			{
			if ($alreadyvoted)
				{
				$percentbar = floor(($row['po_count'] / $totalvotes) * 100);
				$res .= "<tr><td>".sed_parse(sed_cc($row['po_text']), 1, 1, 1)."</td><td width=\"100\"><div style=\"width:95%;\"><div class=\"bar_back\"><div class=\"bar_front\" style=\"width:".$percentbar."%;\"></div></div></div></td><td>$percentbar%</td><td>(".$row['po_count'].")</td></tr>";
				}
			else
				{
				$res .= "<input type='radio' name='vote' id='o".$row['po_id']."' value='".$row['po_id']."' /><label for='o".$row['po_id']."'> ".stripslashes($row['po_text'])."</label><br />";
				}
			}

if (!$alreadyvoted)
		{ 
        $res .= "<input type=\"hidden\" name=\"id\" value=\"".$poll_id."\" />";
        $res .= "<input type=\"hidden\" name=\"a\" value=\"send\" />";
		}

        if (!$alreadyvoted)
		{ $res .= "<p style=\"text-align: center; \"><input type=\"submit\" onclick=\"postp();\" class=\"submit\" value=\"".$L_idx['voteit']."\" /></p>"; }

	$res .= ($alreadyvoted) ? '</table>' : '';

	$res .= "</div>";


        $res .= "<p style=\"text-align: center; \"><a href=\"javascript:polls('".$poll_id."')\">".$L['polls_viewresults']."</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:polls('viewall')\">".$L['polls_viewarchives']."</a></p>";

		$res_all .= sprintf($mask, $res);
		}

//		{ $res = $plu_empty; }

	return($res_all);
	}

/* ============= */

Also, Trustmaster asked me to create an extra file, for words, language file. Please create a file and folder
plugins/recentitems/lang/recentitems.en.lang.php -- (i couldn't creating a new copyright, thanks riptide!)
<?PHP
/* ====================
Seditio - Website engine
Copyright Neocrome
http:/www.neocrome.net

[BEGIN_SED]
File=plugins/calendar/lang/calendar.en.lang.php
Version=1.1
Updated=2006-Nov-12
Type=Plugin
Author=riptide & friends (me)
Description=Plugin to display the events in a calendar
[END_SED]

==================== */

$L_idx['vote_failed'] = 'Vote failed'; //If you have problems with submitting the vote
$L_idx['vote_opt'] = 'Where is your opinion ?'; //If vote:checked false
$L_idx['voteit'] = 'Vote it!';

?>

system/core/polls/polls.inc.php --the real action

find:
$ratings = sed_import('ratings','G','BOL');
add below:
$mode = sed_import('mode','G','TXT');

(add this code right before):
$t = new XTemplate("skins/".$skin."/polls.tpl");
if ($mode=='ajax')
	{


	$result .= '
	<script type="text/javascript">

	function anim(){
	$(".bar_front").each(function(){
	var percentage = $(this).attr("id");
	$(this).css({width: "0%"}).animate({width: percentage}, "slow");
	});}

	$(document).ready(function(){  
	anim();
		});
	</script>';
	$result .= "<table class=\"cells\">";

		$sql2 = sed_sql_query("SELECT SUM(po_count) FROM $db_polls_options WHERE po_pollid='$id'");
		$totalvotes = sed_sql_result($sql2,0,"SUM(po_count)");

		$sql1 = sed_sql_query("SELECT po_id,po_text,po_count FROM $db_polls_options WHERE po_pollid='$id' ORDER by po_id ASC ");

	while ($row1 = sed_sql_fetcharray($sql1))
		{
		$po_id = $row1['po_id'];
		$po_count = $row1['po_count'];
		$percent = @round(100 * ($po_count / $totalvotes),1);
		$percentbar = floor($percent * 2.24);

		$row1['po_text'] = $row1['po_text'];

		$result .= "<tr><td>";
		$result .= sed_parse(sed_cc($row1['po_text']), 1, 1, 1);
		$result .= "</td><td><div style=\"width:100px;\"><div class=\"bar_back\"><div class=\"bar_front\" id=\"$percent%\" style=\"width:0%;\"></div></div></div></td><td>$percent%</td><td>(".$po_count.")</td></tr>";
		}

	$result .= "</table>";

	echo $result;

	exit;

	}


There are also css stuff,
add this to your css file(s):
#poll-container.loading {
  background: url('img/spinner.gif') no-repeat center center;
}

Spinner is something like this: http://www.scribd.com/spinner_bigger.gif -- (to the skins/yourskin/img/)
Trustmaster
#2 29. August 2008, 09:38
Thanks oc, I will check it, test it and include in N-0.0.1. Probably it will be a "recentpolls" plugin because we want to split recentitems into 3 plugins: recentpages, recenttopics and recentpolls.
May the Source be with you!
Trustmaster
#3 29. August 2008, 19:06
Great contribution! I eliminated corehacks and made it a new "recentpolls" plugin.
May the Source be with you!