<?php

	lt_include( PLOG_CLASS_PATH."class/action/blogaction.class.php" );
	
	/**
     * Adds new vote action
     */
    class AddVoteAction extends BlogAction 
	{
	    
        function AddVoteAction( $actionInfo, $request )
        {
        	$this->BlogAction( $actionInfo, $request );
        }

        function perform()
        {
            // Get poll Id and the selected choice
            $pollId = $this->_request->getValue( 'pollId' );
            $choice = $this->_request->getValue( 'choice' );
            
            // Check if selected choice and poll id passed successfully
            // If choice key = 0 (that mean the first choice was selected) or choice key grater than 0
            if( ($choice > 0 || $choice === '0') && $pollId ) {
                lt_include( PLOG_CLASS_PATH."plugins/crystalpoll/class/dao/polls.class.php" );
                lt_include( PLOG_CLASS_PATH."class/net/client.class.php" );

                // Get client host Ip address
                $clientIp = Client::getIp();

                $polls = new Polls();
                // Check if there is any vote came from this IP on this poll
                if( !$polls->validateVoteByIp( $clientIp, $pollId) ) {
                    $poll = $polls->getPoll( $pollId );
                    
                    if( $poll ) {
                        $polls->addPollVote( $pollId, $choice, $clientIp, date( 'Y-m-d' ) );
			echo 1;
			exit;
                    }
                }
            }
            echo 0;
	    exit;
        }
    }
?>
