<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.2 www.exoops.info 2006/03 
// $Id: ratelink.php,v 1.1 2003/08/31 12:16:29 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Original Author: James Knickelbein
// Author Website : http://www.journeymilwaukee.com
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //
include_once("header.php");
include_once(XOOPS_ROOT_PATH."/class/module.errorhandler.php");
if ($_POST['submit']) {
$eh = new ErrorHandler();
if (!$xoopsUser) {
	$ratinguser = 0;
	} else {
		$ratinguser = $xoopsUser->uid();
	}
//Make sure only 1 anonymous from an IP in a single day.
$anonwaitdays = 1;
$ip     = _REMOTE_ADDR;
$lid    = intval($_POST['lid']);
$rating = intval($_POST['rating']);
// Check if Rating is Null
if ($rating=="--") {
	redirect_header("ratelink.php?lid=".$lid."", 4, _NORATINGSELECTED);
	exit();
}
// Check if Link POSTER is voting (UNLESS Anonymous users allowed to post)
if ($ratinguser != 0) {
	$result = $db->query("SELECT submitter FROM ".$db->prefix("mylinks_links")." WHERE lid=$lid");
	while (list($ratinguserDB) = $db->fetchRow($result)) {
		if ($ratinguserDB == $ratinguser) {
			redirect_header("index.php", 4, _CANTVOTEYOURSELF);
			exit();
		}
	}
	// Check if REG user is trying to vote twice.
	$result = $db->query("SELECT ratinguser FROM ".$db->prefix("mylinks_votedata")." WHERE lid=$lid");
	while (list($ratinguserDB) = $db->fetchRow($result)) {
		if ($ratinguserDB == $ratinguser) {
			redirect_header("index.php", 4, _MD_VOTEONCE2);
			exit();
		}
	}
}
// Check if ANONYMOUS user is trying to vote more than once per day.
if ($ratinguser == 0) {
	$yesterday = (time()-(86400 * $anonwaitdays));
	$result = $db->query("SELECT COUNT(*) FROM ".$db->prefix("mylinks_votedata")." WHERE lid=$lid AND ratinguser=0 AND ratinghostname='$ip' AND ratingtimestamp>$yesterday");
	list($anonvotecount) = $db->fetchRow($result);
	if ($anonvotecount > 0) {
			redirect_header("index.php", 4, _MD_VOTEONCE2);
			exit();
	}
}
if ($rating > 10) {
	$rating = 10;
}
//All is well.  Add to Line Item Rate to DB.
$newid    = $db->genId($db->prefix("mylinks_votedata")."_ratingid_seq");
$datetime = time();
$db->query("INSERT INTO ".$db->prefix("mylinks_votedata")." SET ratingid=$newid, lid=$lid, ratinguser=$ratinguser, rating=$rating, ratinghostname='$ip', ratingtimestamp=$datetime") or $eh->show("0013");
//All is well.  Calculate Score & Add to Summary (for quick retrieval & sorting) to DB.
updaterating($lid);
$ratemessage = _VOTEAPPRECTIATED."<br />".sprintf(_MD_THANKURATE, $bcoosConfig['title']);
redirect_header("index.php", 2, $ratemessage);
exit();
} else {
include_once(XOOPS_ROOT_PATH."/header.php");
OpenTable();
mainheader();
$result = $db->query("SELECT title FROM ".$db->prefix("mylinks_links")." WHERE lid=$lid");
list($title) = $db->fetchRow($result);
$title = $myts->makeTboxData4Show($title);
?>
<hr size="1" noshade>
<table border="0" cellpadding="1" cellspacing="0" width="80%"><tr><td>
<h4><center><?php echo $title;?></center></h4>
<ul>
<li><?php echo _VOTEONCE;?>
<li><?php echo _RATINGSCALE;?>
<li><?php echo _BEOBJECTIVE;?>
<li><?php echo _CANTVOTEYOURSELF;?>
</ul>
</td></tr>
<tr><td align="center">
<form method="post" action="ratelink.php">
<input type="hidden" name="lid" value="<?php echo $lid;?>">
<select class="select" name="rating">
<option>--</option>
<?php
for ($i=10; $i>0; $i--) {
	echo "<option value='$i'>$i</option>";
}
echo "
</select>
<input type='submit' class='button' name='submit' value='"._RATEIT."'>
<input type='button' class='button' value='"._CANCEL."' onclick='javascript:history.go(-1)'>
</form></td></tr></table>";
CloseTable();
}
include_once("footer.php");
?>
