Documentation is available at tgcSimplePoll_common.php
1 <?php
2 /**
3 * This class contains common methods used by tgcSimplePoll and tgcSimplePollAdmin
4 *
5 * $Id: tgcSimplePoll_common.php,v 1.1 2004/05/30 17:19:54 luckec Exp $
6 *
7 * @package tgcSimplePoll
8 * @version 1.1.0
9 * @author Carsten Lucke <carsten@tool-garage.de>
10 * @copyright Carsten Lucke <carsten@tool-garage.de>
11 * @link http://www.tool-garage.de
12 */
13
14 /**
15 * This class contains common methods used by tgcSimplePoll and tgcSimplePollAdmin
16 *
17 * @package tgcSimplePoll
18 * @access public
19 * @version 1.1.0
20 * @author Carsten Lucke <carsten@tool-garage.de>
21 * @link http://www.tool-garage.de
22 */
23 class tgcSimplePoll_common
24 {
25 /**
26 * active skin
27 *
28 * @access private
29 * @var string
30 */
31 var $_skin = 'tg/default';
32
33 /**
34 * language
35 *
36 * @access private
37 * @var string
38 */
39 var $_lang = 'en';
40
41 /**
42 * use sessions for user authentication | flooding-protection
43 *
44 * @access private
45 * @var boolean
46 */
47 var $_useSessions = true;
48
49 /**
50 * DB-Object (PEAR::DB)
51 *
52 * @access private
53 * @var object
54 */
55 var $_dbc;
56
57 /**
58 * dispatcher
59 *
60 * @access private
61 * @var string
62 */
63 var $_dispatcher = '';
64
65 /**
66 * patTemplate object
67 *
68 * @access private
69 * @var object
70 * @link http://www.php-tools.de <www.php-tools.de>
71 */
72 var $_tmpl = null;
73
74 /**
75 * current site
76 *
77 * @access private
78 * @var string
79 */
80 var $_site = null;
81
82
83 /**
84 * Returns the question for a given question-id
85 *
86 * @access private
87 * @param int $qId question-id
88 * @return string question on success, otherwise a PEAR_Error object
89 * @throws object PEAR_Error
90 */
91 function _getQuestionFromDB($qId)
92 {
93 return $this->_dbc->getOne('SELECT question FROM ' . SP_DB_TABLEPREFIX . 'questions WHERE qId = ' . $qId);
94 }
95
96 /**
97 * Returns the options for a given question-id
98 *
99 * @access private
100 * @param int $qId question-id
101 * @return array options on success, otherwise a PEAR_Error object
102 * @throws object PEAR_Error
103 */
104 function _getOptionsFromDB($qId)
105 {
106 $result = $this->_dbc->getAll('SELECT oId, answer FROM ' . SP_DB_TABLEPREFIX . 'options WHERE qId = ' . $qId . ' ORDER BY oId');
107 if (PEAR::isError($result)) {
108 return $result;
109 }
110 $options = array();
111 foreach ($result as $row) {
112 $options[$row['oId']] = $row['answer'];
113 }
114 return $options;
115 }
116
117 /**
118 * Returns the table-prefix
119 *
120 * @access private
121 * @param string $table tablename (will be appended to prefix)
122 * @return string prefix
123 */
124 function _prefix($table = '')
125 {
126 return SP_DB_TABLEPREFIX . $table;
127 }
128
129 /**
130 * Get total sum of votes for a question
131 *
132 * @access private
133 * @param int $qId question-id
134 * @return int total number of votes for this question on success, otherwise a PEAR_Error object
135 * @throws object PEAR_Error
136 */
137 function _getOverallVotes($qId)
138 {
139 return $this->_dbc->getOne('SELECT COUNT(*) FROM ' . $this->_prefix('votes') . ' WHERE qId=' . $qId);
140 }
141
142 /**
143 * Returns the vote-results of a question
144 *
145 * @access private
146 * @param int $qId question-id
147 * @return array vote-results on success, otherwise a PEAR_Error object
148 * @throws object PEAR_Error
149 */
150 function _getVoteResults($qId)
151 {
152 $query = sprintf('SELECT q.question, o.answer AS \'option\', COUNT(v.vId) AS votes ' .
153 'FROM %s v RIGHT OUTER JOIN %s q, %s o ON q.qId = v.qId AND o.oId = v.oId WHERE q.qId = o.qId AND q.qId = %d ' .
154 'GROUP BY o.oId ORDER BY o.oId',
155 $this->_prefix('votes'), $this->_prefix('questions'), $this->_prefix('options'), $qId);
156 $result = $this->_dbc->getAll($query);
157 if (PEAR::isError($result)) {
158 $result = $this->_getVoteResultsFailSafe($qId);
159 }
160 return $result;
161 }
162
163 /**
164 * Returns the vote-results of a question
165 *
166 * Comes in play, when _getVoteResults() fails because of older MySQL, or sth. else
167 *
168 * @access private
169 * @param int $qId question-id
170 * @return array vote-results on success, otherwise a PEAR_Error object
171 * @throws object PEAR_Error
172 */
173 function _getVoteResultsFailSafe($qId)
174 {
175 $voteResults = array();
176
177 $question = $this->_getQuestionFromDB($qId);
178 if (PEAR::isError($question)) {
179 return $question;
180 }
181
182 $options = $this->_getOptionsFromDB($qId);
183 if (PEAR::isError($options)) {
184 return $options;
185 }
186 foreach ($options as $oId => $option) {
187 $oData = array();
188 $votes = $this->_dbc->getOne('SELECT COUNT(*) FROM ' . $this->_prefix('votes') . ' WHERE qId = ' . $qId . ' AND oId = ' . $oId);
189 if (PEAR::isError($votes)) {
190 return $votes;
191 }
192 $oData['question'] = $question;
193 $oData['option'] = $option;
194 $oData['votes'] = $votes;
195 array_push($voteResults, $oData);
196 }
197 return $voteResults;
198 }
199 }
200 ?>
Documentation generated on Fri, 19 Nov 2004 23:51:51 +0100 by phpDocumentor 1.2.3