<?
include 'config.php';
include 'func.php';
head();
aut();
ban();
####################Запись онлайна##################
mysql_query("UPDATE `profile` SET `online` = 'votes' WHERE `id` = '".$id_session."'");
####################################################

echo head . "<b>Голосования</b>" . div; 

switch($_GET['act']) {
    default:
    $all = mysql_result(mysql_query("SELECT count(*) FROM `votes`"), 0);
    if($all==0) {
        echo link1 . 'Голосования не созданы' . div;
    } else {
        //echo link2;
        $result = mysql_query("SELECT * FROM votes order by id desc");
        while($vote = mysql_fetch_assoc($result)){
            $all_votes = mysql_result(mysql_query("SELECT count(*) FROM `votes_list` WHERE `vote` = '".$vote['id']."'"), 0);
        echo link2 . " <a href=\"votes.php?act=view&vot=".$vote['id']."\">".$vote['nazv']."</a> [".$all_votes." / ".date2($vote['date'])." / ".user($vote['avtor'])."]" . div;
}
//echo div;
    }
    
    echo link1 . "<b><a href=\"votes.php?act=add\">Добавить опрос</a></b>" . div;
    break;
    
    case 'view':
    echo katalog;
    echo "[<a href=\"votes.php?act=vote&vot=".num($_GET['vot'])."\">Голосовать</a>]<br/ ><br/ >";
    
    echo '<b>Описание:</b><br/ >';
    $vote_info = mysql_fetch_assoc(mysql_query("SELECT * FROM `votes` WHERE `id` = '".num($_GET['vot'])."'"));
    echo "Название: <b>".$vote_info['nazv']."</b><br/ >";
    echo "Автор: <b>".user($vote_info['avtor'])."</b><br/ ><br/ >";
    
    echo "Вопрос: <b>".$vote_info['vopros']."</b><br/ ><br/ >";
    
   
   $vote = mysql_fetch_assoc(mysql_query("SELECT * FROM `votes` WHERE `id` = '".num($_GET['vot'])."'"));
   for($i=1; $i<6; $i++)
   {
    if($vote['v_'.$i.'']){
    $all_votes = mysql_result(mysql_query("SELECT count(*) FROM `votes_list` WHERE `vote` = '".num($_GET['vot'])."' and `variant` = '".$i."'"), 0);
    echo "".$i.". " . $vote['v_'.$i.''] . " [".$all_votes."]<br/>";
    }
   }
   
   echo div;
    break;
    
    case 'users':
    if (isset($_GET['user'])) $id = num($_GET['user']); else $id = $id_session;
    
        $all = mysql_result(mysql_query("SELECT count(*) FROM `votes` WHERE `avtor` = '".$id."'"), 0);
    if($all==0) {
        echo katalog . 'Голосования не созданы' . div;
    } else {
        echo katalog;
        $result = mysql_query("SELECT * FROM votes WHERE `avtor` = '".$id."'");
        while($vote = mysql_fetch_assoc($result)){
            $all_votes = mysql_result(mysql_query("SELECT count(*) FROM `votes_list` WHERE `vote` = '".$vote['id']."'"), 0);
        echo "<img src=\"style/".$icon."/dot.gif\" alt=\"\" /> <a href=\"votes.php?act=view&vot=".$vote['id']."\">".$vote['nazv']."</a> [".$all_votes." / ".date2($vote['date'])." / ".user($vote['avtor'])."]";
}
echo div;
    }
    
    echo link1 . "<b><a href=\"votes.php?act=add\">Добавить опрос</a></b>" . div;
    break;
    case 'add':
    //echo katalog;
    if(empty($_POST['nazv'])){
    echo link1 . '
      <form method="POST" action="votes.php?act=add" name="auth">
      <b>Создание голосований:</b></div><div class="link2">
      Тема:<br/>
      <input name="nazv" maxlength="50" value="" title="infa" emptyok="false"/><br/>
      Вопрос:<br/>
      <input name="vopros" maxlength="100" value="" title="infa" emptyok="false"/><br/>
      Вариант 1:<br/>
      <input name="v1" maxlength="50" value="" title="infa" emptyok="false"/><br/>
      Вариант 2:<br/>
      <input name="v2" maxlength="50" value="" title="infa" emptyok="false"/><br/>
      Вариант 3:<br/>
      <input name="v3" maxlength="50" value="" title="infa" emptyok="false"/><br/>
      Вариант 4:<br/>
      <input name="v4" maxlength="50" value="" title="infa" emptyok="false"/><br/>
      Вариант 5:<br/>
      <input name="v5" maxlength="50" value="" title="infa" emptyok="false"/><br/><input type="submit" class="button" value="Добавить" name="enter"></div>
    
    ';
    } else {
        $nazv = html($_POST['nazv']);
        $vopros = html($_POST['vopros']);
        $v1 = html($_POST['v1']);
        $v2 = html($_POST['v2']);
        $v3 = html($_POST['v3']);
        $v4 = html($_POST['v4']);
        $v5 = html($_POST['v5']);
        
        if (!$vopros or !$v1 or !$v2) {
        echo link1 . 'Не введён вопрос, или опрос содержит менее двух вариантов ответов' . div;
        } else {
        mysql_query("Insert into votes set nazv ='".$nazv."', avtor ='".$id_session."', date ='".time()."', vopros = '".$vopros."', v_1 = '".$v1."', v_2 = '".$v2."', v_3 = '".$v3."', v_4 = '".$v4."', v_5 = '".$v5."'");
        echo link1 . 'Опрос успешно добавлен' . div;
    }
    }
    
    echo div;
    break;
    
    case 'vote':
    $vote_info = mysql_fetch_assoc(mysql_query("SELECT * FROM `votes` WHERE `id` = '".num($_GET['vot'])."'"));
    //echo katalog;
    if(isset($vote_info['id'])) {
    echo '<form method="POST" action="votes.php?act=golos&vot='.num($_GET['vot']).'" name="n">';
    
    echo link2 . "Вопрос: <b>".$vote_info['vopros']."</b><br/>";
    if($vote_info['v_1'])
    echo '<b>1.</b> <input type="radio" name="v" value="1"/>'.$vote_info['v_1'].'<br/>';
    if($vote_info['v_2'])
    echo '<b>2.</b> <input type="radio" name="v" value="2"/>'.$vote_info['v_2'].'<br/>';
    if($vote_info['v_3'])
    echo '<b>3.</b> <input type="radio" name="v" value="3"/>'.$vote_info['v_3'].'<br/>';
    if($vote_info['v_4'])
    echo '<b>4.</b> <input type="radio" name="v" value="4"/>'.$vote_info['v_4'].'<br/>';
    if($vote_info['v_5'])
    echo '<b>5.</b> <input type="radio" name="v" value="5"/>'.$vote_info['v_5'].'<br/>';
    echo '<input type="submit" class="button" value="Голосовать"/>' . div;
    }else{
        echo link1 . 'Данного голосования не существует.' .div;
    }
    echo div;
    
    
    break;
    
    case 'golos':
    $golos = num($_POST['v']);
    $voting = num($_GET['vot']);
    
    if (empty($golos)) $error = 'Вы не выбрали вариант.';
    mysql_query ("Select * from votes WHERE id = '".$voting."'");
    if(mysql_affected_rows()==0) $error = 'Данного голосования не существует.';
    
        
    mysql_query ("Select * from votes_list WHERE `vote` = '".$voting."' and `user` = '".$id_session."'");
    if(mysql_affected_rows()!=0) $error = 'Вы уже голосовали.';
    
    if($error) {
        echo link1 . $error . div;
    } else {
        mysql_query("Insert into votes_list set vote ='".$voting."', user ='".$id_session."', date ='".time()."', variant = '".$golos."', coment = '".$coment."'");
        echo link1 . 'Вы успешно проголосовали' . div;
    }
    break;

}

navig();
foot();
?>