<?PHP
include('checkcookie.php');
echo "<h3>Жалобы на тесты</h3>";

if(isset($_POST["id_abuse"])){
$id_abuse = intval($_POST["id_abuse"]);
$id_link = isset($_POST["id_link"]) ? intval($_POST["id_link"]) : '0';
if($id_link > 0){
mysql_query("UPDATE `b_tests` SET `modered`='2', `paused`='1' WHERE `id`='$id_link'") or die(mysql_error());
echo "<b style='color:#6EAD2B;'>Отправлено на модерацию</b><br>";
}else{
mysql_query("DELETE FROM `b_complaints` WHERE `id`='$id_abuse' AND `link_id`='$id_link' AND `type`='2'") or die(mysql_error());
mysql_query("UPDATE `b_tests` SET `all_zh`=`all_zh`-'1' WHERE `id`='$id_link'") or die(mysql_error());
echo "<b style='color:#6EAD2B;'>Удалено</b><br>";
}
}




function us_name($id){
$sql = mysql_query("SELECT `username` FROM `b_users` WHERE `id`='$id' LIMIT 1");
if(mysql_num_rows($sql) > 0){
return "<span style='color:#387021;'>".mysql_result($sql,0)."</span>";
}else{
return "<span style='color:#FF0000;'>Удалён</span>";
}
}

$sql = mysql_query("SELECT `id` FROM `b_complaints` WHERE `type`='2'");
$allnews = mysql_num_rows($sql);
if($allnews > 0){
$kolvo = 1; //кол-во выводимых на странице
$get_pg = (isset($_GET["pg"])) ? intval($_GET["pg"]) : 0;
 //общее кол-во
$vsego = intval(($allnews - 1) / $kolvo) + 1;  
$nowpage = intval($get_pg);
if(empty($nowpage) or $nowpage < 0) $nowpage = 1; 
if($nowpage > $vsego) $nowpage = $vsego; 
$gg = $nowpage * $kolvo - $kolvo; 
$urls = "adminmain.php?p=abuse_test&pg";
// Находим две ближайшие станицы с обоих краев, если они есть 
if($nowpage - 3 > 0){ $pagefirst = '<a href= "'.$urls.'=1&">1<</a>';}else{$pagefirst = NULL;}
if($nowpage - 2 > 0) {$page2left = '<a href= "'.$urls.'='. ($nowpage - 2) .'">'. ($nowpage - 2) .'</a> ';  }else{$page2left = NULL;}
if($nowpage - 1 > 0) {$page1left = '<a href= "'.$urls.'='. ($nowpage - 1) .'">'. ($nowpage - 1) .'</a> ';  }else{$page1left = NULL;}
if($nowpage + 2 <= $vsego) {$page2right = '<a href= "'.$urls.'='. ($nowpage + 2) .'">'. ($nowpage + 2) .'</a>';  }else{$page2right = NULL;}
if($nowpage + 1 <= $vsego) {$page1right = '<a href= "'.$urls.'='. ($nowpage + 1) .'">'. ($nowpage + 1) .'</a>'; }else{$page1right = NULL;}
if($nowpage + 2 < $vsego){ $pagelast = '<a href= "'.$urls.'='.$vsego.'">>'.$vsego.'</a>';}else{$pagelast = NULL;}
// Вывод меню 
echo "<table width='100%' style='margin-bottom: 5px;'><tr><td nowrap='nowrap'>";
$nextp = $nowpage + 1;
$lastp = $nowpage - 1;
if($nowpage != 1) { echo "<a href='".$urls."=".$lastp."'><span class='text14'>"; }else{ echo "<span class='textgray'>"; } echo "&larr;&nbsp;Предыдущие</span>";if($nowpage==1) { echo "</a>"; } 
echo "</td><td width='90%' align='center'>$pagefirst $page2left $page1left <b> $nowpage </b> $page1right $page2right $pagelast</td><td nowrap='nowrap'>";
if($nowpage!=$vsego){ echo "<a href='".$urls."=".$nextp."'><span class='text14'>"; }else{ echo "<span class='textgray'>"; } echo "Следующие&nbsp;&rarr;</span>"; if($nowpage!=$vsego) { echo "</a>"; } 
echo "</td></tr></table>";
$limit = "LIMIT $gg,$kolvo";
}else{ $limit = "";}

echo <<<html
<b>На модерацию</b> - отправить тест на модерацию<br><br>
<table class='adn'>
<head>
<tr class='lineb'>
<td>ID</td>
<td>ID ссылки</td>
<td>От кого</td>
<td>Текс</td>
<td>Дата</td>
<td>Действие</td>
</tr>
</head>
<tbody>
html;

$sql = mysql_query("SELECT * FROM `b_complaints` WHERE `type`='2' ORDER BY `date` DESC $limit") or die(mysql_error());
while($row = mysql_fetch_assoc($sql)){
if(time() - $row["date"] < 86400){
$date = "<span style='color:#387021;'>".date("d-m-y H:i",$row["date"])."</span>";
}else{
$date = date("d-m-y H:i",$row["date"]);
}

$us_from = us_name($row["user"]);
echo <<<html
<tr class="liney ell">
<td align="center">{$row["id"]}</td>
<td width="100px" align="center">{$row["link_id"]}</td>
<td align="center">ID: {$row["user"]} | {$us_from}</td>
<td>{$row["text"]}</td>
<td>{$date}</td>
<td width="146px">
<form action="adminmain.php?p=abuse_test" method="POST"><input type="hidden" value="{$row["id"]}" name="id_abuse"><input type="hidden" value="{$row["link_id"]}" name="id_link"><input type="submit" value="На модерацию"></form>
<form action="adminmain.php?p=abuse_test" method="POST"><input type="hidden" value="{$row["id"]}" name="id_abuse"><input type="hidden" value="0" name="id_link"><input type="submit" value="Удалить жалобу"></form>
</td>
</tr>
html;
}
echo "</tbody></table></form>";
?>