<?php
/* +--------------------------------------------
* |
* | Product: PHPEchoCMS
* | Author: Anas Husseini
* | License: GPL
* | Last updated: 05.03.07
* | For version: 2.0
* | Desc: Voting module
* |
* +--------------------------------------------
*/
if( !@$BASE_LOAD )
{
die('Application error.');
}
function show_voting()
{
global $root_path, $smarty, $settings, $core;
// -------------------
// Get lang for module
// -------------------
$mlang = $core->getlang("voting");
$content = "";
if (@$_GET['id'] && $_GET['act']=='1')
{
// Display a specific voting with user selection
$voting = query ("select * from `".PREFIX."voting` where `id`=".addslashes($_GET['id']), 4);
if ($voting['id']==addslashes($_GET['id']))
{
$content .= "<table width='100%'>";
$content .= "<tr><td class='title'><h3>".$voting['title'];
$content .= "</h3></td></tr>";
$content .= "</table>";
$elements = query ("select * from `".PREFIX."voting_elements` where `parent`=".addslashes($_GET['id']), 1);
$content .= "<form action=index.php?module=voting&id=".$voting['id']."&vote=1 method='post'>";
while ($element = mysql_fetch_array($elements))
{
$smarty->assign("child_id", $element['id']);
$smarty->assign("title", $element['title']);
$content .= $smarty->fetch('modules/voting_act1.tpl');
}
$content .= "<br><input type=submit value=".$mlang['003'].">";
$content .= "</form>";
$content .= " <a href=index.php?module=voting>".$mlang['008']."</a>";
}
else
$content .= $mlang['002'];
}
elseif (@$_GET['id'] && $_GET['act']=='2')
{
// Display a selected voting with selections statistics
$voting = query ("select * from `".PREFIX."voting` where `id`=".addslashes($_GET['id']), 4);
if ($voting['id']==addslashes($_GET['id']))
{
$content .= "<table width='100%'>";
$content .= "<tr><td class='title'><h3>".$voting['title'];
$content .= "</h3></td></tr>";
$content .= "</table>";
$elements = query ("select * from `".PREFIX."voting_elements` where `parent`=".addslashes($_GET['id']), 1);
$total_freq = query ("select sum(`frequency`) from `".PREFIX."voting_elements` where `parent`=".addslashes($_GET['id']), 2);
while ($element = mysql_fetch_array($elements))
{
$smarty->assign("title", $element['title']);
$smarty->assign("freq", $element['frequency']);
$smarty->assign("percent", 1+(($total_freq>0)?($element['frequency']*50/$total_freq):0));
$content .= $smarty->fetch('modules/voting_act2.tpl');
}
$content .= "<br> <a href=index.php?module=voting>".$mlang['008']."</a>";
}
else
$content .= $mlang['002'];
}
elseif (@$_GET['vote']=='1' && $_GET['id'] && $_POST['child_id'])
{
// Increasing Voting frequency for an element
$q = "update `".PREFIX."voting_elements` set `frequency`=`frequency`+1 where `id`=".$_POST['child_id'];
query ($q, 1);
echo $mlang['004'];
redirect("index.php?module=voting&id=".addslashes($_GET['id'])."&act=2");
}
else
{
// Display all the votings
$content .= "<table width='100%'><tr><td width='80%' class = 'title'> <b>".$mlang['001']."</b>";
$content .= "</td><td width='20%' class='title'><center><b>".$mlang['007']."</b></center></td></tr></table>";
if (@$_GET['page'])
$votings = query ("select * from `".PREFIX."voting` order by `id` desc limit ".((addslashes($_GET['id'])-1)*15).",15", 1);
else
$votings = query ("select * from `".PREFIX."voting` order by `id` desc limit 15", 1);
$nb = query ("select * from `".PREFIX."voting`", 3);
if ($nb > 0)
{
while ($voting = mysql_fetch_array($votings))
{
$smarty->assign("id", $voting['id']);
$smarty->assign("title", $voting['title']);
$smarty->assign("date", $voting['date']);
$content .= $smarty->fetch('modules/voting.tpl');
}
$content .= "<br>".$mlang['006'];
$nmax = ($nb % 15 == 0) ? ($nb / 15) : ($nb / 15 + 1);
for ($j=1; $j<=$nmax; $j++)
if (($j == $_GET['page']) || ($j==1 && !$_GET['page']))
$content .= $j." ";
else
$content .= "<a href=index.php?module=voting&page=".$j.">".$j."</a> ";
}
else
$content .= $mlang['005'];
}
return $content;
}
echo show_voting();
?>