| 1 | <?php |
|---|
| 2 | // $Id: xoopspolllog.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 XoopsPollLog extends XoopsObject |
|---|
| 34 | { |
|---|
| 35 | var $db; |
|---|
| 36 | |
|---|
| 37 | // constructor |
|---|
| 38 | function XoopsPollLog($id=null) |
|---|
| 39 | { |
|---|
| 40 | $this->db =& Database::getInstance(); |
|---|
| 41 | $this->initVar("log_id", XOBJ_DTYPE_INT, 0); |
|---|
| 42 | $this->initVar("poll_id", XOBJ_DTYPE_INT, null, true); |
|---|
| 43 | $this->initVar("option_id", XOBJ_DTYPE_INT, null, true); |
|---|
| 44 | $this->initVar("ip", XOBJ_DTYPE_OTHER, null); |
|---|
| 45 | $this->initVar("user_id", XOBJ_DTYPE_INT, 0); |
|---|
| 46 | $this->initVar("time", XOBJ_DTYPE_INT, null); |
|---|
| 47 | $this->initVar("rating_id", XOBJ_DTYPE_INT, null); |
|---|
| 48 | $this->initVar("comment", XOBJ_DTYPE_TXTAREA, null); |
|---|
| 49 | if ( !empty($id) ) { |
|---|
| 50 | if ( is_array($id) ) { |
|---|
| 51 | $this->assignVars($id); |
|---|
| 52 | } else { |
|---|
| 53 | $this->load(intval($id)); |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | // public |
|---|
| 59 | function store() |
|---|
| 60 | { |
|---|
| 61 | if ( !$this->cleanVars() ) { |
|---|
| 62 | return false; |
|---|
| 63 | } |
|---|
| 64 | foreach ( $this->cleanVars as $k=>$v ) { |
|---|
| 65 | $$k = $v; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | $log_id = $this->db->genId($this->db->prefix("xoopspoll_log")."_log_id_seq"); |
|---|
| 69 | $sql = "INSERT INTO ".$this->db->prefix("xoopspoll_log")." (log_id, poll_id, option_id, ip, user_id, time, rating_id, comment) VALUES ($log_id, $poll_id, $option_id, ".$this->db->quoteString($ip).", $user_id, ".time().", $rating_id, ".$this->db->quoteString($comment).")"; |
|---|
| 70 | $result = $this->db->query($sql); |
|---|
| 71 | if (!$result) { |
|---|
| 72 | $this->setErrors("Could not store log data in the database."); |
|---|
| 73 | return false; |
|---|
| 74 | } |
|---|
| 75 | return $option_id; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | // private |
|---|
| 79 | function load($id) |
|---|
| 80 | { |
|---|
| 81 | $sql = "SELECT * FROM ".$this->db->prefix("xoopspoll_log")." WHERE log_id=".$id.""; |
|---|
| 82 | $myrow = $this->db->fetchArray($this->db->query($sql)); |
|---|
| 83 | $this->assignVars($myrow); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | // public |
|---|
| 87 | function delete() |
|---|
| 88 | { |
|---|
| 89 | $sql = sprintf("DELETE FROM %s WHERE log_id = %u", $this->db->prefix("xoopspoll_log"), $this->getVar("log_id")); |
|---|
| 90 | if ( !$this->db->query($sql) ) { |
|---|
| 91 | return false; |
|---|
| 92 | } |
|---|
| 93 | return true; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | // public static |
|---|
| 97 | function &getAllByPollId($poll_id, $orderby="time ASC") |
|---|
| 98 | { |
|---|
| 99 | $db =& Database::getInstance(); |
|---|
| 100 | $ret = array(); |
|---|
| 101 | $sql = "SELECT * FROM ".$db->prefix("xoopspoll_log")." WHERE poll_id=".intval($poll_id)." ORDER BY $orderby"; |
|---|
| 102 | $result = $db->query($sql); |
|---|
| 103 | while ( $myrow = $db->fetchArray($result) ) { |
|---|
| 104 | $ret[] = new XoopsPollLog($myrow); |
|---|
| 105 | } |
|---|
| 106 | //echo $sql; |
|---|
| 107 | return $ret; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | // public static |
|---|
| 111 | function hasVoted($poll_id, $ip, $user_id=null) |
|---|
| 112 | { |
|---|
| 113 | $db =& Database::getInstance(); |
|---|
| 114 | $sql = "SELECT COUNT(*) FROM ".$db->prefix("xoopspoll_log")." WHERE poll_id=".intval($poll_id)." AND"; |
|---|
| 115 | if ( !empty($user_id) ) { |
|---|
| 116 | $sql .= " user_id=".intval($user_id); |
|---|
| 117 | } else { |
|---|
| 118 | $sql .= " ip='".$ip."'"; |
|---|
| 119 | } |
|---|
| 120 | list($count) = $db->fetchRow($db->query($sql)); |
|---|
| 121 | if ( $count > 0 ) { |
|---|
| 122 | return true; |
|---|
| 123 | } |
|---|
| 124 | return false; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | // public static |
|---|
| 128 | function deleteByPollId($poll_id) |
|---|
| 129 | { |
|---|
| 130 | $db =& Database::getInstance(); |
|---|
| 131 | $sql = sprintf("DELETE FROM %s WHERE poll_id = %u", $db->prefix("xoopspoll_log"), intval($poll_id)); |
|---|
| 132 | if ( !$db->query($sql) ) { |
|---|
| 133 | return false; |
|---|
| 134 | } |
|---|
| 135 | return true; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | // public static |
|---|
| 139 | function deleteByOptionId($option_id) |
|---|
| 140 | { |
|---|
| 141 | $db =& Database::getInstance(); |
|---|
| 142 | $sql = sprintf("DELETE FROM %s WHERE option_id = %u", $db->prefix("xoopspoll_log"), intval($option_id)); |
|---|
| 143 | if ( !$db->query($sql) ) { |
|---|
| 144 | return false; |
|---|
| 145 | } |
|---|
| 146 | return true; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | // public static |
|---|
| 150 | function getTotalVotersByPollId($poll_id) |
|---|
| 151 | { |
|---|
| 152 | $db =& Database::getInstance(); |
|---|
| 153 | $sql = "SELECT DISTINCT user_id FROM ".$db->prefix("xoopspoll_log")." WHERE poll_id=".intval($poll_id)." AND user_id > 0 AND rating_id = 1"; |
|---|
| 154 | $users = $db->getRowsNum($db->query($sql)); |
|---|
| 155 | $sql = "SELECT DISTINCT ip FROM ".$db->prefix("xoopspoll_log")." WHERE poll_id=".intval($poll_id)." AND user_id=0 AND rating_id = 1"; |
|---|
| 156 | $anons = $db->getRowsNum($db->query($sql)); |
|---|
| 157 | return $users+$anons; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | // public static |
|---|
| 161 | function getTotalVotesByPollId($poll_id) |
|---|
| 162 | { |
|---|
| 163 | $db =& Database::getInstance(); |
|---|
| 164 | $sql = "SELECT COUNT(*) FROM ".$db->prefix("xoopspoll_log")." WHERE poll_id = ".intval($poll_id)." AND rating_id = 1"; |
|---|
| 165 | list($votes) = $db->fetchRow($db->query($sql)); |
|---|
| 166 | return $votes; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | // public static |
|---|
| 170 | function getTotalVotesByOptionId($option_id, $rating_id) |
|---|
| 171 | { |
|---|
| 172 | $db =& Database::getInstance(); |
|---|
| 173 | $sql = "SELECT COUNT(*) FROM ".$db->prefix("xoopspoll_log")." WHERE option_id = ".intval($option_id)." AND rating_id = $rating_id"; |
|---|
| 174 | list($votes) = $db->fetchRow($db->query($sql)); |
|---|
| 175 | return $votes; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | ?> |
|---|