<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
// Author of File: 							     //
// Kazumi Ono (http://www.mywebaddons.com/ , http://www.myweb.ne.jp/)        //
// ------------------------------------------------------------------------- //

include_once(XOOPS_ROOT_PATH."/modules/xoopspoll/language/".$xoopsConfig['language']."/main.php");

class XoopsPollRenderer {

	// private
	// XoopsPoll class object
	var $poll;

	// constructor
	function XoopsPollRenderer($poll){
		$this->poll =& $poll;
	}

	// public
	function renderForm(){
		$content = "<form action='".XOOPS_URL."/modules/xoopspoll/index.php' method='post'>";
		$content .= "<table border='0' cellpadding='0' cellspacing='0' valign='top' width='100%'><tr><td class='bg2'>\n";
		$content .= "<table width='100%' border='0' cellpadding='4' cellspacing='1'>\n";
		$content .= "<tr class='bg3'><td align='center' colspan='2'><input type='hidden' name='poll_id' value='".$this->poll->getVar("poll_id")."' />\n";
		$content .= "<b>".$this->poll->getVar("question")."</b></td></tr>\n";
		$options_arr = XoopsPollOption::getAllByPollId($this->poll->getVar("poll_id"));
		$option_type = "radio";
		$option_name = "option_id";
		if ( $this->poll->getVar("multiple") == 1 ) {
			$option_type = "checkbox";
			$option_name .= "[]";
		}
		foreach ( $options_arr as $option ) {
			$content .= "<tr class='bg1'><td align='center'><input type='$option_type' name='$option_name' value='".$option->getVar("option_id")."' /></td><td align='left'>".$option->getVar("option_text"). "</td></tr>\n";
		}
        	$content .= "<tr class='bg3'><td align='center' colspan='2'><input type='submit' value='"._PL_VOTE."' />&nbsp;";
        	$content .= "<input type='button' value='"._PL_RESULTS."' class='button' onclick='location=\"".XOOPS_URL."/modules/xoopspoll/pollresults.php?poll_id=".$this->poll->getVar("poll_id")."\"' />";
        	$content .= "</td></tr></table></td></tr></table></form>\n";
		return $content;
	}

	// public
	function renderResults(){
		if ( !$this->poll->hasExpired() ) {
			$end_text = sprintf(_PL_ENDSAT, formatTimestamp($this->poll->getVar("end_time"), "m"));
		} else {
			$end_text = sprintf(_PL_ENDEDAT, formatTimestamp($this->poll->getVar("end_time"), "m"));
		}
		echo "<div style='text-align:center'><table width='60%' border='0' cellpadding='4' cellspacing='0'><tr class='bg3'><td><span style='font-weight:bold;'>".$this->poll->getVar("question")."</span></td></tr><tr class='bg1'><td align='right'>$end_text</td></tr></table>";

		echo "<table width='60%' border='0' cellpadding='4' cellspacing='0'>";
		$options_arr = XoopsPollOption::getAllByPollId($this->poll->getVar("poll_id"));
		foreach ( $options_arr as $option ) {
			$total = $this->poll->getVar("votes");
			if ( $total > 0 ) {
				$percent = 100 * $option->getVar("option_count") / $total;
			} else {
				$percent = 0;
			}
			echo "<tr class='bg1'><td width='30%'>".$option->getVar("option_text")."</td><td width='70%'>";
			if ( $percent > 0 ) {
				$width = intval($percent)*2;
				echo "<img src='".XOOPS_URL."/modules/xoopspoll/images/colorbars/".$option->getVar("option_color", "E")."' height='14' width='".$width."' align='middle' alt='".intval($percent)." %' />";
			}
			printf(" %d %% (%d)", $percent, $option->getVar("option_count"));
			echo "</td></tr>";
		}
		echo "<tr class='bg1'><td colspan='2' align='center'><br /><b>".sprintf(_PL_TOTALVOTES, $this->poll->getVar("votes"))."<br />".sprintf(_PL_TOTALVOTERS, $this->poll->getVar("voters"))."</b>";
		if ( !$this->poll->hasExpired() ) {
			echo "<br />[<a href='".XOOPS_URL."/modules/xoopspoll/index.php?poll_id=".$this->poll->getVar("poll_id")."'>"._PL_VOTE."</a>]";
		}
		echo "</td></tr></table></div><br />";
	}


}
?>