A little adjustment about polls and cookies

Hi All.

I have a code about poll system for my webite. That system sends a cookie for all users, when they vote for any poll. Everypoll have their own uniqe id’s like 1,2,3,4 etc. If a user votes for poll 1, and if multivote option is closed, so that user cannot vote for that poll1 again, but he/she can vote for other polls ofcourse…

What I want is, that user to not poll again for any other polls. For example,

if a user votes for poll 2, then that user couldn’t vote for other polls too. It should give same error like, you alredy voted for today… (there is also a time limit function)

But I couldn’t understand which informations that cookie saves, and which function handles poll id matches.

There is 2 files which are related about this functions.

One is;

[php]

/------------------------------------------------------------------------------
Check if user already voted for that poll
------------------------------------------------------------------------------
/

function checkVote($poll, $email = null) {

$database = JFactory::getDBO();
$my = JFactory::getUser();

$conf = new pollxtConfig();
$conf->pollid = $poll->id;
$conf->load();
$result = “”;
//check date and time
$now = date(“Y-m-d”);
$nowtime = date(“H:i:s”);

if (
($poll->datefrom > $now && $poll->datefrom != “0000-00-00”)
OR ($poll->datefrom == $now AND $poll->timefrom < $nowtime)
OR ($poll->dateto < $now && $poll->dateto != “0000-00-00”)
OR ($poll->dateto == $now AND $poll->timeto < $nowtime)
)
return true;

$cookiename = “voted$poll->id”;
$vcookie = JRequest::getVar($cookiename, ‘0’, ‘COOKIE’);

$scookie = false;

$now = strtotime(date(“Y-m-d G:i:s”));

//cookie check
if ($conf->seccookie==1 && $vcookie != 0) {
if (!$poll->multivote)
$scookie = true;
elseif ((strtotime($vcookie)+ $poll->lag*60) > $now)
$scookie = true;
}

$ip = $conf->ip;

if (!$my->id && $poll->logon) return true;

if (!$my->id)
$logonQuery = “AND p.logon=0”;
else
$logonQuery = “”;

$where = "";
$id = intval ($poll->id);

// 
if ($my->id) $uname = $my->username;
elseif ($email) $uname = $email;
else $uname = null;

// if logged on check username else check IP
if (($uname) and $conf->secuname and $conf->secip) {
$where = " AND (d.user = “”.$uname."" OR d.ip = “”.$ip."" )";
}
elseif ($conf->secip) $where = " AND d.ip = “”.$ip.""";
elseif (($uname) and $conf->secuname) $where = " AND d.user = “”.$uname.""";

if (!$where=="") {
$query = “SELECT d.*
FROM #__pollsxt_questions AS q
LEFT JOIN #__pollsxt_options AS o ON o.quid = q.id
LEFT JOIN #__pollxt_data AS d ON d.optid = o.id
WHERE q.pollid =”".$id.""".$where.“ORDER BY d.datu DESC
LIMIT 1”;

  $database->setQuery($query);


  $result = $database->loadObject();
  echo $database->getErrorMsg();

}
$sip = false;
if (isset($result->datu)) {
if (!$poll->multivote )
$sip = true;
elseif (strtotime($result->datu)+ $poll->lag*60 > $now)
$sip = true;
}

return ($scookie or $sip);
}[/php]

Second is;
[php]
// already voted?
/* $cookiename = “voted$poll->id”;
$vcookie = JRequest::getVar($cookiename, ‘0’, ‘COOKIE’);
$now = strtotime(date(“Y-m-d G:i:s”));

  $voted = checkVote($poll, $email);
  if (!$poll->multivote == 1) {
     if ($voted  || ((strtotime($vcookie)+ $poll->lag*60) > $now)) {
          $frontend->error = true;
          $frontend->message = JText::_('POLLXT_ALREADY_VOTE');
          return;
     }
  }

*/

  // already voted?
  $voted = checkVote($poll, $email);
  
  if ($voted)  {
         $frontend->error = true;
       $frontend->message = JText::_('POLLXT_ALREADY_VOTE');
       return;
  }

[/php]

There is a checkvote function, for ip or mail or cookie checks. I’m sure that It’s damn too simple. I worked with visual basic and c++ codes for long times. But still I couldn’t see that simple thing.

If that poll is already voted, it’s checked by $voted , But what is $voted. Is it if pollid = cookie id then its voted ? or what ?

I tried something like if $voted >0 code but it didn’t changed anything.

So everything should start with $voted = checkVote($poll, $email); .

But its too complicated for me.

I appreciate all your helps. Thank you.

Sponsor our Newsletter | Privacy Policy | Terms of Service