< ?
// User settings
$rater_ip_voting_restriction = true; // restrict ip address voting (true or false)
$rater_ip_vote_qty=1; // how many times an ip address can vote
$rater_already_rated_msg="You have already rated this item. You were allowed ".$rater_ip_vote_qty." vote(s).";
$rater_not_selected_msg="You have not selected a rating value.";
$rater_thankyou_msg="Thankyou for voting.";
$rater_generic_text="this item"; // generic item text
$rater_end_of_line_char="\n"; // may want to change for different operating systems
if(!isset($rater_id)) $rater_id=1;
if(!isset($rater_item_name)) $rater_item_name=$rater_generic_text;
// DO NOT MODIFY BELOW THIS LINE
$rater_filename='item_'.$rater_id.".rating";
$rater_rating=0;
$rater_stars="";
$rater_stars_txt="";
$rater_rating=0;
$rater_votes=0;
$rater_msg="";
// Rating action==结果是将投票的结果写入文件中,如果没有submit,则不执行,如果没有评分就submit了,则显示告警信息
if(isset($_REQUEST["rate".$rater_id])){ //判断submit按钮有没有被按下
if(isset($_REQUEST["rating_".$rater_id])){//在此基础上判断用户按下submit按钮时是否没有评分,没有的话则给出一个告警消息
while(list($key,$val)=each($_REQUEST["rating_".$rater_id])){//将用户的评分值存为中间变量rater_rating值,其实觉得没有必要用while的表达式
$rater_rating=$val;
}
$rater_ip = getenv("REMOTE_ADDR");//得到用户的ip
$rater_file=fopen($rater_filename,"a+");//打开存放投票值的文件
$rater_str="";
$rater_str = rtrim(fread($rater_file, 1024*8),$rater_end_of_line_char);//读取最大1024*768个字节,并将每行末尾的空格去掉,
if($rater_str!=""){//如果文件非空,也即在此之前已经有人投过票了
if($rater_ip_voting_restriction){//而且系统管理员设置了每个ip能够投票的限制次数
$rater_data=explode($rater_end_of_line_char,$rater_str);//将文件进行切割,rater_data数组中每一个元素存放着文件中的一行数据
$rater_ip_vote_count=0;//初始化
foreach($rater_data as $d){//遍历文件,也即查看每一行,查看跟当前用户ip投票的次数有没有超过限制
$rater_tmp=explode("|",$d);//此处需要查看文件的格式,格式为"投票值|投票ip",将投票的值和投票的ip分别作为rater_tmp数组的两个元素
$rater_oldip=str_replace($rater_end_of_line_char,"",$rater_tmp[1]);//去掉空格
if($rater_ip==$rater_oldip){//如果文件中的ip跟当前用户的ip相同的话,则执行下面的++操作,也即判断有没有超过限制值
$rater_ip_vote_count++;
}
}
if($rater_ip_vote_count > ($rater_ip_vote_qty - 1)){//同一个ip的投票次数超过限制
$rater_msg=$rater_already_rated_msg;//显示告警信息
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);//将此次投票结果写入文件中
$rater_msg=$rater_thankyou_msg;//显示感谢信息
}
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;
}
}else{//如果文件是一个空文件,也即之前没有人投过票,直接将投票结果写入文件中
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;//显示感谢信息
}
fclose($rater_file);
}else{//如果没有投票就按了submit按钮的话,显示告警信息
$rater_msg=$rater_not_selected_msg;
}
}//如果submit按钮没有被按下,则上面的过程都不进行
// Get current rating==结果是从文件中读取所有的评价值,算出平均评价得分值,并设置对应的图像
if(is_file($rater_filename)){//文件存在的话,打开文件,统计评价的平均值
$rater_file=fopen($rater_filename,"r");
$rater_str="";
$rater_str = fread($rater_file, 1024*8);//读取最大1024*8个字节,赋值给rater_str
if($rater_str!=""){//如果文件不为空的话,则取出每一行,放入数组rater_data中
$rater_data=explode($rater_end_of_line_char,$rater_str);
$rater_votes=count($rater_data)-1;//统计行数,也即评价的总数count
$rater_sum=0;
foreach($rater_data as $d){//将每一行的评价值相加,得到总值
$d=explode("|",$d);//取出以"|"隔开的行的前一部分
$rater_sum+=$d[0];//相加得到总值
}
$rater_rating=number_format(($rater_sum/$rater_votes), 2, '.', '');//得到评价均值,并格式化输出
}
fclose($rater_file);
}else{//如果文件不存在,则什么都不做
$rater_file=fopen($rater_filename,"w");
fclose($rater_file);
}
// Assign star image==根据平均评分值来设置对应的图像
if ($rater_rating < = 0 ){$rater_stars = "./img/00star.gif";$rater_stars_txt="Not Rated";}
if ($rater_rating >= 0.5){$rater_stars = "./img/05star.gif";$rater_stars_txt="0.5";}
if ($rater_rating >= 1 ){$rater_stars = "./img/1star.gif";$rater_stars_txt="1";}
if ($rater_rating >= 1.5){$rater_stars = "./img/15star.gif";$rater_stars_txt="1.5";}
if ($rater_rating >= 2 ){$rater_stars = "./img/2star.gif";$rater_stars_txt="2";}
if ($rater_rating >= 2.5){$rater_stars = "./img/25star.gif";$rater_stars_txt="2.5";}
if ($rater_rating >= 3 ){$rater_stars = "./img/3star.gif";$rater_stars_txt="3";}
if ($rater_rating >= 3.5){$rater_stars = "./img/35star.gif";$rater_stars_txt="3.5";}
if ($rater_rating >= 4 ){$rater_stars = "./img/4star.gif";$rater_stars_txt="4";}
if ($rater_rating >= 4.5){$rater_stars = "./img/45star.gif";$rater_stars_txt="4.5";}
if ($rater_rating >= 5 ){$rater_stars = "./img/5star.gif";$rater_stars_txt="5";}
// Output
echo '<div class="hreview">';
echo '<form method="post" action="'.$_SERVER["PHP_SELF"].'">';
echo '<h3 class="item">Rate <span class="fn">'.$rater_item_name.'</span></h3>';
echo '<div>';//下面的一行的?x=是隐藏参数传递,根据作者的意思,这样做的意思是传递一个x参数进来,使得每次的link都不一样,不会导致系统使用cache的link
echo '<span class="rating"><img src="'.$rater_stars.'?x='.uniqid((double)microtime()*1000000,1).'" alt="'.$rater_stars_txt.' stars" /> Ave. rating: '.$rater_stars_txt.'</span> from <span class="reviewcount"> '.$rater_votes.' votes</span>.';
echo '</div>';
echo '<div>';
echo '<label for="rate5_'.$rater_id.'"><input type="radio" value="5" name="rating_'.$rater_id.'[]" id="rate5_'.$rater_id.'" />Excellent</label>';
echo '<label for="rate4_'.$rater_id.'"><input type="radio" value="4" name="rating_'.$rater_id.'[]" id="rate4_'.$rater_id.'" />Very Good</label>';
echo '<label for="rate3_'.$rater_id.'"><input type="radio" value="3" name="rating_'.$rater_id.'[]" id="rate3_'.$rater_id.'" />Good</label>';
echo '<label for="rate2_'.$rater_id.'"><input type="radio" value="2" name="rating_'.$rater_id.'[]" id="rate2_'.$rater_id.'" />Fair</label>';
echo '<label for="rate1_'.$rater_id.'"><input type="radio" value="1" name="rating_'.$rater_id.'[]" id="rate1_'.$rater_id.'" />Poor</label>';
echo '<input type="hidden" name="rs_id" value="'.$rater_id.'" />';
echo '<input type="submit" name="rate'.$rater_id.'" value="Rate" />';
echo '</div>';
if($rater_msg!="") echo "<div>".$rater_msg."</div>";
echo '</form>';
echo '</div>';
?>
|