source: temp/test-xoops.ec-cube.net/html/modules/xoopspoll/class/xoopspoll.php @ 795

Revision 795, 8.1 KB checked in by uehara, 19 years ago (diff)
Line 
1<?php
2// $Id: xoopspoll.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// ------------------------------------------------------------------------- //
31include_once XOOPS_ROOT_PATH."/class/xoopsobject.php";
32
33class XoopsPoll extends XoopsObject
34{
35    var $db;
36
37    //constructor
38    function XoopsPoll($id=null)
39    {
40        $this->db =& Database::getInstance();
41        $this->initVar("poll_id", XOBJ_DTYPE_INT, null, false);
42        $this->initVar("question", XOBJ_DTYPE_TXTBOX, null, true, 255);
43        $this->initVar("description", XOBJ_DTYPE_TXTBOX, null, false, 255);
44        $this->initVar("user_id", XOBJ_DTYPE_INT, null, false);
45        $this->initVar("start_time", XOBJ_DTYPE_INT, null, false);
46        $this->initVar("end_time", XOBJ_DTYPE_INT, null, true);
47        $this->initVar("votes", XOBJ_DTYPE_INT, 0, false);
48        $this->initVar("voters", XOBJ_DTYPE_INT, 0, false);
49        $this->initVar("display", XOBJ_DTYPE_INT, 1, false);
50        $this->initVar("weight", XOBJ_DTYPE_INT, 0, false);
51        $this->initVar("multiple", XOBJ_DTYPE_INT, 0, false);
52        $this->initVar("mail_status", XOBJ_DTYPE_INT, 1, false);
53        if ( !empty($id) ) {
54            if ( is_array($id) ) {
55                $this->assignVars($id);
56            } else {
57                $this->load(intval($id));
58            }
59        }
60    }
61
62    // public
63    function store()
64    {
65        if ( !$this->cleanVars() ) {
66            return false;
67        }
68        foreach ( $this->cleanVars as $k=>$v ) {
69            $$k = $v;
70        }
71        $start_time = empty($start_time) ? time() : $start_time;
72        if ( $end_time <= $start_time ) {
73            $this->setErrors("End time must be set to future");
74            return false;
75        }
76        if ( empty($poll_id) ) {
77            $poll_id = $this->db->genId($this->db->prefix("xoopspoll_desc")."_poll_id_seq");
78            $sql = "INSERT INTO ".$this->db->prefix("xoopspoll_desc")." (poll_id, question, description, user_id, start_time, end_time, votes, voters, display, weight, multiple, mail_status) VALUES ($poll_id, ".$this->db->quoteString($question).", ".$this->db->quoteString($description).", $user_id, $start_time, $end_time, 0, 0, $display, $weight, $multiple, $mail_status)";
79        } else {
80            $sql ="UPDATE ".$this->db->prefix("xoopspoll_desc")." SET question=".$this->db->quoteString($question).", description=".$this->db->quoteString($description).", start_time=$start_time, end_time=$end_time, display=$display, weight=$weight, multiple=$multiple, mail_status=$mail_status WHERE poll_id=$poll_id";
81        }
82        //echo $sql;
83        if ( !$result = $this->db->query($sql) ) {
84            $this->setErrors("Could not store data in the database.");
85            return false;
86        }
87        if ( empty($poll_id) ) {
88            return $this->db->getInsertId();
89        }
90        return $poll_id;
91    }
92
93    // private
94    function load($id)
95    {
96        $sql = "SELECT * FROM ".$this->db->prefix("xoopspoll_desc")." WHERE poll_id=".$id."";
97        $myrow = $this->db->fetchArray($this->db->query($sql));
98        $this->assignVars($myrow);
99    }
100
101    // public
102    function hasExpired()
103    {
104        if ( $this->getVar("end_time") > time() ) {
105            return false;
106        }
107        return true;
108    }
109
110    // public
111    function delete()
112    {
113        $sql = sprintf("DELETE FROM %s WHERE poll_id = %u", $this->db->prefix("xoopspoll_desc"), $this->getVar("poll_id"));
114            if ( !$this->db->query($sql) ) {
115            return false;
116        }
117        return true;
118    }
119
120    // private, static
121    function &getAll($criteria=array(), $asobject=true, $orderby="end_time DESC", $limit=0, $start=0)
122    {
123        $db =& Database::getInstance();
124        $ret = array();
125        $where_query = "";
126        if ( is_array($criteria) && count($criteria) > 0 ) {
127            $where_query = " WHERE";
128            foreach ( $criteria as $c ) {
129                $where_query .= " $c AND";
130            }
131            $where_query = substr($where_query, 0, -4);
132        }
133        if ( !$asobject ) {
134            $sql = "SELECT poll_id FROM ".$db->prefix("xoopspoll_desc")."$where_query ORDER BY $orderby";
135            $result = $db->query($sql,intval($limit),intval($start));
136            while ( $myrow = $db->fetchArray($result) ) {
137                $ret[] = $myrow['poll_id'];
138            }
139        } else {
140            $sql = "SELECT * FROM ".$db->prefix("xoopspoll_desc")."".$where_query." ORDER BY $orderby";
141            $result = $db->query($sql,$limit,$start);
142            while ( $myrow = $db->fetchArray($result) ) {
143                $ret[] = new XoopsPoll($myrow);
144            }
145        }
146        //echo $sql;
147        return $ret;
148    }
149
150    // public
151    function vote($option_id, $ip, $user_id=null, $rating_id = 1, $comment = "")
152    {
153        if (!empty($option_id)) {
154            if (is_array($option_id)) {
155                foreach ($option_id as $vote) {
156                    $option = new XoopsPollOption($vote);
157                    if ( $this->getVar("poll_id") == $option->getVar("poll_id") ) {
158                        $log = new XoopsPollLog();
159                        $log->setVar("poll_id", $this->getVar("poll_id"));
160                        $log->setVar("option_id", $vote);
161                        $log->setVar("ip", $ip);
162                        $log->setVar("rating_id", $rating_id);
163                        $log->setVar("comment", $comment);
164                        if ( isset($user_id) ) {
165                            $log->setVar("user_id", $user_id);
166                        }
167                        if(!$log->store()) {
168                        } else {
169                            $option->updateCount($rating_id);
170                        }
171                    }
172                }
173            } else {
174                $option = new XoopsPollOption($option_id);
175                if ( $this->getVar("poll_id") == $option->getVar("poll_id") ) {
176                    $log = new XoopsPollLog();
177                    $log->setVar("poll_id", $this->getVar("poll_id"));
178                    $log->setVar("option_id", $option_id);
179                    $log->setVar("ip", $ip);
180                    $log->setVar("rating_id", $rating_id);
181                    $log->setVar("comment", $comment);
182                    if ( isset($user_id) ) {
183                        $log->setVar("user_id", $user_id);
184                    }
185                    $log->store();
186                    $option->updateCount($rating_id);
187                }
188            }
189            return true;
190        }
191        return false;
192    }
193
194    // public
195    function updateCount()
196    {
197        $votes = XoopsPollLog::getTotalVotesByPollId($this->getVar("poll_id"));
198        $voters = XoopsPollLog::getTotalVotersByPollId($this->getVar("poll_id"));
199        $sql ="UPDATE ".$this->db->prefix("xoopspoll_desc")." SET votes=$votes, voters=$voters WHERE poll_id=".$this->getVar("poll_id")."";
200        $this->db->query($sql);
201    }
202
203    // public
204    function insertOption($arrData)
205    {
206        $option_text = str_replace("'", "''", $arrData['title']);
207        $body = str_replace("'", "''", $arrData['body']);
208        $sql ="INSERT INTO ".$this->db->prefix("xoopspoll_option")." (poll_id, option_text, body, status, category_id, update_date) ".
209                "VALUES (".$arrData['poll_id'].", ".$option_text.", ".$body.", 1, ".$arrData['category_id'].", now())";
210       
211        $this->db->query($sql);
212echo $sql;
213exit;
214    }   
215}
216?>
Note: See TracBrowser for help on using the repository browser.