<?php
// $Id: xoopspolloption.php,v 1.2 2004/07/11 10:58:35 Farsus Exp $
// -------------------------------------------------------------------------//
//                               RUNCMS                                                   //
//                                                                          //
//         Relaible - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// 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")." VALUES (
                ".intval($option_id).",
                ".intval($poll_id).",
                '$option_text',
                ".intval($option_count).",
                '$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->insert_id();
}

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->fetch_array($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 option_id";

$result = $db->query($sql);

while ( $myrow = $db->fetch_array($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;
}
//---------------------------------------------------------------------------------------//
}
?>