<?php
// $Id: ratelink.php,v 1.4 2005/10/28 11:56:02 ohwada Exp $

// 2005-09-04 K.OHWADA
// BUG 2932: dont work correctly when register_long_arrays = off

// 2005-01-20 K.OHWADA
// add $weblinksConfig

// 2004-12-05 K.OHWADA
// check_access()

// 2004-10-20 K.OHWADA
// ratelink_use

// 2004/08/10 K.OHWADA
// enable to install this module two or more. 

//================================================================
// rate link
// use class weblinksCategory, weblinksLink
// 2004/01/23 K.OHWADA
//================================================================

include "header.php";

$tbl_votedata =& table_votedata::getInstance();

check_access();

// BUG 2932: dont work correctly when register_long_arrays = off
$submit = get_post('submit');
$lid    = get_post_get_int('lid');
$cid    = get_post_get_int('cid');
$rating = get_post_int('rating');

if ( !empty($submit) ) 
{
	$eh = new ErrorHandler; //ErrorHandler object

//Make sure only 1 anonymous from an IP in a single day.
	$anonwaitdays = 1;

// Check if Rating is Null
   	if ($rating=="--") 
   	{
		redirect_header("ratelink.php?lid=$lid", 4, _WLS_NORATING);
		exit();
   	}

// Check if Link POSTER is voting (UNLESS Anonymous users allowed to post)
   	if ($xoops_user_uid != 0) 
   	{
		$record = $tbl_link->getRowByLid($lid);
		$uid    = $record['uid'];

		if ($uid == $xoops_user_uid) 
		{
			redirect_header("index.php", 4, _WLS_CANTVOTEOWN);
			exit();
		}

// Check if REG user is trying to vote twice.
		$records = $tbl_votedata->getRowsByLid($lid);
		foreach ($records as $record)
		{
			$ratinguser = $record['ratinguser'];

       		if ($ratinguser == $xoops_user_uid) 
       		{
				redirect_header("index.php", 4, _WLS_VOTEONCE2);
				exit();
           	}
      	}
   	}
   	else 
   	{
// Check if ANONYMOUS user is trying to vote more than once per day.
		$yesterday = (time()-(86400 * $anonwaitdays));
		$anonvotecount = $tbl_votedata->getNumByLidIpTime($lid, $server_remote_addr, $yesterday);

   		if ($anonvotecount > 0) 
   		{
			redirect_header("index.php", 4, _WLS_VOTEONCE2);
			exit();
       	}
   	}

	if($rating > 10) {	$rating = 10;	}

// Add to Line Item Rate to DB.
	$tbl_votedata->insert($lid, $xoops_user_uid, $rating, $server_remote_addr) or $eh->show("0013");

// Calculate Score & Add to Summary (for quick retrieval & sorting) to DB.
	list($finalrating, $votesDB) = $tbl_votedata->getRating($lid);
	$tbl_link->updateRating($lid, $finalrating, $votesDB);

	$ratemessage = _WLS_VOTEAPPRE."<br />".sprintf(_WLS_THANKURATE,$xoopsConfig['sitename']);

	redirect_header("index.php", 2, $ratemessage);
	exit();

}
else 
{
// enable to install this module two or more. 
	$xoopsOption['template_main'] = $MODULE_DIRNAME."_ratelink.html";

	include XOOPS_ROOT_PATH."/header.php";

	$record = $tbl_link->getRowByLid($lid);
	$title  = $myts->htmlSpecialChars( $record['title'] );

	assignIndex();
	$xoopsTpl->assign('lang_ratethissite', _WLS_RATETHISSITE);
	$xoopsTpl->assign('lang_voteonce',    _WLS_VOTEONCE);
	$xoopsTpl->assign('lang_ratingscale', _WLS_RATINGSCALE);
	$xoopsTpl->assign('lang_beobjective', _WLS_BEOBJECTIVE);
	$xoopsTpl->assign('lang_donotvote',   _WLS_DONOTVOTE);
	$xoopsTpl->assign('lang_rateit',      _WLS_RATEIT);
	$xoopsTpl->assign('lang_cancel',      _CANCEL);

	$xoopsTpl->assign('link_id',    $lid);
	$xoopsTpl->assign('link_cid',   $cid);
	$xoopsTpl->assign('link_title', $title);

	include XOOPS_ROOT_PATH.'/footer.php';
}

exit();
// --- end of main ---


//---------------------------------------------------------
// function
//---------------------------------------------------------
function check_access()
{
	global $is_xoops_user_admin;
	global $xoops_user_uid, $xoops_user_uname, $xoops_user_email;
	global $xoops_user_groups;

	global $weblinksConfig;
	$use_ratelink = $weblinksConfig['use_ratelink'];
	$is_permit    = $weblinksConfig['is_auth_ratelink'];

// not use
	if ( !$use_ratelink ) 
	{
		redirect_header("index.php", 1, _NOPERM);
		exit();
	}

// not permit
	if ( !$is_permit ) 
	{
		if ($xoops_user_uid) 
		{
			redirect_header("index.php", 1, _NOPERM);
			exit();
		}
// anonymous
		else
		{
			redirect_header(XOOPS_URL."/user.php", 2, _WLS_MUSTREGFIRST);
			exit();
		}
	}

}

?>
