<?php
//v1.6 www.exoops.info 2006/08 GPL 
//v1.5 www.exoops.info 2006/03 GPL
// $Id: xoopspolloption.php,v 1.4 2003/03/19 07:02:44 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Original Author: Kazumi Ono
// Author Website : http://www.mywebaddons.com/ , http://www.myweb.ne.jp
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //
include_once(XOOPS_ROOT_PATH."/class/xoopsobject.php");
class XoopsPollOption extends XoopsObject {
	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));
			}
		}
	}
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function store() {
global $db;
if ( !$this->isCleaned() ) {
	if ( !$this->cleanVars() ) {
		return false;
	}
}
foreach ( $this->cleanVars as $k=>$v ) {
	$$k = $v;
}
if ( empty($option_id) ) {
	$option_id = $db->genId($db->prefix("xoopspoll_option")."_option_id_seq");
	$sql = "
		INSERT INTO ".$db->prefix("xoopspoll_option")." SET
		option_id=$option_id,
		poll_id=$poll_id,
		option_text='$option_text',
		option_count=$option_count,
		option_color='$option_color'";
	} else {
		$sql = "UPDATE ".$db->prefix("xoopspoll_option")." SET option_text='$option_text', option_count='$option_count', option_color='$option_color'  WHERE option_id=".$option_id."";
	}
if ( !$result = $db->query($sql) ) {
	$this->setErrors(_NOTUPDATED);
	return false;
}
if ( empty($option_id) ) {
	return $db->getInsertId();
}
return $option_id;
}
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function load($id) {
global $db;
$sql   = "SELECT * FROM ".$db->prefix("xoopspoll_option")." WHERE option_id=".$id."";
$myrow = $db->fetchArray($db->query($sql));
$this->set($myrow);
}
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function delete() {
global $db;
$sql = "DELETE FROM ".$db->prefix("xoopspoll_option")." WHERE option_id=".$this->getVar("option_id")."";
if ( !$db->query($sql) ) {
	return false;
}
return true;
}
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function updateCount() {
global $db;
$votes = XoopsPollLog::getTotalVotesByOptionId($this->getVar("option_id"));
$sql   = "UPDATE ".$db->prefix("xoopspoll_option")." SET option_count=$votes WHERE option_id=".$this->getVar("option_id")."";
$db->query($sql);
}
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function &getAllByPollId($poll_id) {
global $db;
$poll_id = intval($poll_id);
$ret     = array();
$sql     = "
		SELECT
		*
		FROM
		".$db->prefix("xoopspoll_option")."
		WHERE
		poll_id=$poll_id
		ORDER BY RAND()";
$result = $db->query($sql);
while ( $myrow = $db->fetchArray($result) ) {
	$ret[] = new XoopsPollOption($myrow);
}
return $ret;
}
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function deleteByPollId($poll_id) {
global $db;
$sql = "DELETE FROM ".$db->prefix("xoopspoll_option")." WHERE poll_id=".intval($poll_id);
if ( !$db->query($sql) ) {
	return false;
}
return true;
}
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function resetCountByPollId($poll_id) {
global $db;
$sql = "UPDATE ".$db->prefix("xoopspoll_option")." SET option_count=0 WHERE poll_id=".intval($poll_id);
if ( !$db->query($sql) ) {
	return false;
}
return true;
}
//---------------------------------------------------------------------------------------//
}
?>
