<?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."/class/xoopsobject.php");
class XoopsPollOption extends XoopsObject{


	// constructor
	function XoopsPollOption($id=NULL){
		$this->XoopsObject();
		$this->initVar("option_id", "int", NULL, false);
		$this->initVar("poll_id", "int", NULL, false);
		$this->initVar("option_text", "textbox", NULL, true, 255, true);
		$this->initVar("option_count", "int", 0, false);
		$this->initVar("option_color", "other", NULL, false);
		if ( !empty($id) ) {
			if ( is_array($id) ) {
				$this->set($id);
			} else {
				$this->load(intval($id));
			}
		}
	}

	// public
	function store(){
		if ( !$this->isCleaned() ) {
			if ( !$this->cleanVars() ) {
				return false;
			}
		}
		foreach ( $this->cleanVars as $k=>$v ) {
			$$k = $v;
		}
		if ( empty($option_id) ) {
			$option_id = $this->db->genId($this->db->prefix("xoopspoll_option")."_option_id_seq");
			$sql = "INSERT INTO ".$this->db->prefix("xoopspoll_option")." (option_id, poll_id, option_text, option_count, option_color) VALUES ($option_id, $poll_id, '$option_text', $option_count, '$option_color')";
		} else {
			$sql = "UPDATE ".$this->db->prefix("xoopspoll_option")." SET option_text='$option_text', option_count='$option_count', option_color='$option_color'  WHERE option_id=".$option_id."";
		}
		//echo $sql;
		if ( !$result = $this->db->query($sql) ) {
			$this->setErrors("Could not store data in the database.");
			return false;
		}
		if ( empty($option_id) ) {
			return $this->db->getInsertId();
		}
		return $option_id;
	}

	// private
	function load($id){
		$sql = "SELECT * FROM ".$this->db->prefix("xoopspoll_option")." WHERE option_id=".$id."";
		$myrow = $this->db->fetchArray($this->db->query($sql));
		$this->set($myrow);
	}

	// public
	function delete(){
		$sql = "DELETE FROM ".$this->db->prefix("xoopspoll_option")." WHERE option_id=".$this->getVar("option_id")."";
        	if ( !$this->db->query($sql) ) {
			return false;
		}
		return true;
	}

	// public
	function updateCount(){
		$votes = XoopsPollLog::getTotalVotesByOptionId($this->getVar("option_id"));
		$sql ="UPDATE ".$this->db->prefix("xoopspoll_option")." SET option_count=$votes WHERE option_id=".$this->getVar("option_id")."";
		$this->db->query($sql);
	}

	// public static
	function getAllByPollId($poll_id){
		$db =& Database::getInstance();
		$ret = array();
		$sql = "SELECT * FROM ".$db->prefix("xoopspoll_option")." WHERE poll_id=".intval($poll_id)." ORDER BY option_id";
		$result = $db->query($sql);
		while ( $myrow = $db->fetchArray($result) ) {
			$ret[] = new XoopsPollOption($myrow);
		}
		//echo $sql;
		return $ret;
	}

	// public static
	function deleteByPollId($poll_id){
		$db =& Database::getInstance();
		$sql = "DELETE FROM ".$db->prefix("xoopspoll_option")." WHERE poll_id=".intval($poll_id);
        	if ( !$db->query($sql) ) {
			return false;
		}
		return true;
	}

	// public static
	function resetCountByPollId($poll_id){
		$db =& Database::getInstance();
		$sql = "UPDATE ".$db->prefix("xoopspoll_option")." SET option_count=0 WHERE poll_id=".intval($poll_id);
        	if ( !$db->query($sql) ) {
			return false;
		}
		return true;
	}
}
?>