| 1 | <?php |
|---|
| 2 | // $Id: xoopspolloption.php,v 1.2 2005/03/18 12:52:49 onokazu Exp $ |
|---|
| 3 | // ------------------------------------------------------------------------ // |
|---|
| 4 | // XOOPS - PHP Content Management System // |
|---|
| 5 | // Copyright (c) 2000 XOOPS.org // |
|---|
| 6 | // <http://www.xoops.org/> // |
|---|
| 7 | // ------------------------------------------------------------------------ // |
|---|
| 8 | // This program is free software; you can redistribute it and/or modify // |
|---|
| 9 | // it under the terms of the GNU General Public License as published by // |
|---|
| 10 | // the Free Software Foundation; either version 2 of the License, or // |
|---|
| 11 | // (at your option) any later version. // |
|---|
| 12 | // // |
|---|
| 13 | // You may not change or alter any portion of this comment or credits // |
|---|
| 14 | // of supporting developers from this source code or any supporting // |
|---|
| 15 | // source code which is considered copyrighted (c) material of the // |
|---|
| 16 | // original comment or credit authors. // |
|---|
| 17 | // // |
|---|
| 18 | // This program is distributed in the hope that it will be useful, // |
|---|
| 19 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|---|
| 20 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
|---|
| 21 | // GNU General Public License for more details. // |
|---|
| 22 | // // |
|---|
| 23 | // You should have received a copy of the GNU General Public License // |
|---|
| 24 | // along with this program; if not, write to the Free Software // |
|---|
| 25 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // |
|---|
| 26 | // ------------------------------------------------------------------------ // |
|---|
| 27 | // Author: Kazumi Ono (AKA onokazu) // |
|---|
| 28 | // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ // |
|---|
| 29 | // Project: The XOOPS Project // |
|---|
| 30 | // ------------------------------------------------------------------------- // |
|---|
| 31 | include_once XOOPS_ROOT_PATH."/class/xoopsobject.php"; |
|---|
| 32 | |
|---|
| 33 | class XoopsPollOption extends XoopsObject |
|---|
| 34 | { |
|---|
| 35 | var $db; |
|---|
| 36 | |
|---|
| 37 | // constructor |
|---|
| 38 | function XoopsPollOption($id=null) |
|---|
| 39 | { |
|---|
| 40 | $this->db =& Database::getInstance(); |
|---|
| 41 | $this->initVar("option_id", XOBJ_DTYPE_INT, null, false); |
|---|
| 42 | $this->initVar("poll_id", XOBJ_DTYPE_INT, null, false); |
|---|
| 43 | $this->initVar("option_text", XOBJ_DTYPE_TXTBOX, null, true, 255); |
|---|
| 44 | $this->initVar("option_count", XOBJ_DTYPE_INT, 0, false); |
|---|
| 45 | $this->initVar("option_color", XOBJ_DTYPE_OTHER, null, false); |
|---|
| 46 | if ( !empty($id) ) { |
|---|
| 47 | if ( is_array($id) ) { |
|---|
| 48 | $this->assignVars($id); |
|---|
| 49 | } else { |
|---|
| 50 | $this->load(intval($id)); |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | // public |
|---|
| 56 | function store() |
|---|
| 57 | { |
|---|
| 58 | if ( !$this->cleanVars() ) { |
|---|
| 59 | return false; |
|---|
| 60 | } |
|---|
| 61 | foreach ( $this->cleanVars as $k=>$v ) { |
|---|
| 62 | $$k = $v; |
|---|
| 63 | } |
|---|
| 64 | if ( empty($option_id) ) { |
|---|
| 65 | $option_id = $this->db->genId($this->db->prefix("xoopspoll_option")."_option_id_seq"); |
|---|
| 66 | $sql = "INSERT INTO ".$this->db->prefix("xoopspoll_option")." (option_id, poll_id, option_text, option_count, option_color) VALUES ($option_id, $poll_id, ".$this->db->quoteString($option_text).", $option_count, ".$this->db->quoteString($option_color).")"; |
|---|
| 67 | } else { |
|---|
| 68 | $sql = "UPDATE ".$this->db->prefix("xoopspoll_option")." SET option_text=".$this->db->quoteString($option_text).", option_count=$option_count, option_color=".$this->db->quoteString($option_color)." WHERE option_id=".$option_id.""; |
|---|
| 69 | } |
|---|
| 70 | //echo $sql; |
|---|
| 71 | if ( !$result = $this->db->query($sql) ) { |
|---|
| 72 | $this->setErrors("Could not store data in the database."); |
|---|
| 73 | return false; |
|---|
| 74 | } |
|---|
| 75 | if ( empty($option_id) ) { |
|---|
| 76 | return $this->db->getInsertId(); |
|---|
| 77 | } |
|---|
| 78 | return $option_id; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | // private |
|---|
| 82 | function load($id) |
|---|
| 83 | { |
|---|
| 84 | $sql = "SELECT * FROM ".$this->db->prefix("xoopspoll_option")." WHERE option_id=".$id.""; |
|---|
| 85 | $myrow = $this->db->fetchArray($this->db->query($sql)); |
|---|
| 86 | $this->assignVars($myrow); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | // public |
|---|
| 90 | function delete() |
|---|
| 91 | { |
|---|
| 92 | $sql = sprintf("DELETE FROM %s WHERE option_id = %u", $this->db->prefix("xoopspoll_option"), $this->getVar("option_id")); |
|---|
| 93 | if ( !$this->db->query($sql) ) { |
|---|
| 94 | return false; |
|---|
| 95 | } |
|---|
| 96 | return true; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | // public |
|---|
| 100 | function updateCount() |
|---|
| 101 | { |
|---|
| 102 | $votes = XoopsPollLog::getTotalVotesByOptionId($this->getVar("option_id")); |
|---|
| 103 | $sql ="UPDATE ".$this->db->prefix("xoopspoll_option")." SET option_count=$votes WHERE option_id=".$this->getVar("option_id").""; |
|---|
| 104 | $this->db->query($sql); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | // public static |
|---|
| 108 | function &getAllByPollId($poll_id, $limit = 10) |
|---|
| 109 | { |
|---|
| 110 | $db =& Database::getInstance(); |
|---|
| 111 | $ret = array(); |
|---|
| 112 | $sql = "SELECT * FROM ".$db->prefix("xoopspoll_option")." WHERE poll_id=".intval($poll_id)." ORDER BY option_count DESC LIMIT 0, $limit"; |
|---|
| 113 | $result = $db->query($sql); |
|---|
| 114 | while ( $myrow = $db->fetchArray($result) ) { |
|---|
| 115 | $ret[] = new XoopsPollOption($myrow); |
|---|
| 116 | } |
|---|
| 117 | //echo $sql; |
|---|
| 118 | return $ret; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | // public static |
|---|
| 122 | function &getOptionByOptionId($option_id) |
|---|
| 123 | { |
|---|
| 124 | $db =& Database::getInstance(); |
|---|
| 125 | $ret = array(); |
|---|
| 126 | $sql = "SELECT * FROM ".$db->prefix("xoopspoll_option")." WHERE option_id=".intval($option_id).""; |
|---|
| 127 | $result = $db->query($sql); |
|---|
| 128 | while ( $myrow = $db->fetchArray($result) ) { |
|---|
| 129 | $ret[] = new XoopsPollOption($myrow); |
|---|
| 130 | } |
|---|
| 131 | //echo $sql; |
|---|
| 132 | return $ret; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | // public static |
|---|
| 136 | function &getAllOption() |
|---|
| 137 | { |
|---|
| 138 | $db =& Database::getInstance(); |
|---|
| 139 | $ret = array(); |
|---|
| 140 | $sql = "SELECT * FROM ".$db->prefix("xoopspoll_option") . " ORDER BY update_date DESC"; |
|---|
| 141 | $result = $db->query($sql); |
|---|
| 142 | while ( $myrow = $db->fetchArray($result) ) { |
|---|
| 143 | $ret[] = $myrow; |
|---|
| 144 | } |
|---|
| 145 | //echo $sql; |
|---|
| 146 | return $ret; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | // public static |
|---|
| 151 | function deleteByPollId($poll_id) |
|---|
| 152 | { |
|---|
| 153 | $db =& Database::getInstance(); |
|---|
| 154 | $sql = sprintf("DELETE FROM %s WHERE poll_id = %u", $db->prefix("xoopspoll_option"), intval($poll_id)); |
|---|
| 155 | if ( !$db->query($sql) ) { |
|---|
| 156 | return false; |
|---|
| 157 | } |
|---|
| 158 | return true; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | // public static |
|---|
| 162 | function resetCountByPollId($poll_id) |
|---|
| 163 | { |
|---|
| 164 | $db =& Database::getInstance(); |
|---|
| 165 | $sql = "UPDATE ".$db->prefix("xoopspoll_option")." SET option_count=0 WHERE poll_id=".intval($poll_id); |
|---|
| 166 | if ( !$db->query($sql) ) { |
|---|
| 167 | return false; |
|---|
| 168 | } |
|---|
| 169 | return true; |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | ?> |
|---|