|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace XoopsModules\Xoopspoll; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
XOOPS - PHP Content Management System |
|
7
|
|
|
Copyright (c) 2000-2020 XOOPS.org |
|
8
|
|
|
<https://xoops.org> |
|
9
|
|
|
This program is free software; you can redistribute it and/or modify |
|
10
|
|
|
it under the terms of the GNU General Public License as published by |
|
11
|
|
|
the Free Software Foundation; either version 2 of the License, or |
|
12
|
|
|
(at your option) any later version. |
|
13
|
|
|
|
|
14
|
|
|
You may not change or alter any portion of this comment or credits |
|
15
|
|
|
of supporting developers from this source code or any supporting |
|
16
|
|
|
source code which is considered copyrighted (c) material of the |
|
17
|
|
|
original comment or credit authors. |
|
18
|
|
|
|
|
19
|
|
|
This program is distributed in the hope that it will be useful, |
|
20
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
GNU General Public License for more details. |
|
23
|
|
|
|
|
24
|
|
|
You should have received a copy of the GNU General Public License |
|
25
|
|
|
along with this program; if not, write to the Free Software |
|
26
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Poll Renderer class for the XoopsPoll Module |
|
31
|
|
|
* |
|
32
|
|
|
* @copyright :: {@link https://xoops.org/ XOOPS Project} |
|
33
|
|
|
* @license :: {@link http://www.fsf.org/copyleft/gpl.html GNU public license} |
|
34
|
|
|
* @package :: xoopspoll |
|
35
|
|
|
* @subpackage:: admin |
|
36
|
|
|
* @since :: 1.0 |
|
37
|
|
|
* @author :: {@link http://www.myweb.ne.jp/ Kazumi Ono (AKA onokazu)} |
|
38
|
|
|
*/ |
|
39
|
|
|
|
|
40
|
|
|
use Xmf\Request; |
|
41
|
|
|
|
|
42
|
|
|
Helper::getInstance()->loadLanguage('main'); |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Class Renderer |
|
47
|
|
|
*/ |
|
48
|
|
|
class Renderer |
|
49
|
|
|
{ |
|
50
|
|
|
// XoopsPoll class object |
|
51
|
|
|
protected $pollObj; |
|
52
|
|
|
protected $pollHandler; |
|
53
|
|
|
protected $optionHandler; |
|
54
|
|
|
protected $logHandler; |
|
55
|
|
|
protected $helper; |
|
56
|
|
|
|
|
57
|
|
|
// constructor(s) |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param null $poll |
|
|
|
|
|
|
61
|
|
|
* @param null $helper |
|
|
|
|
|
|
62
|
|
|
*/ |
|
63
|
|
|
public function __construct($poll = null, $helper = null) |
|
64
|
|
|
{ |
|
65
|
|
|
if (null === $helper) { |
|
|
|
|
|
|
66
|
|
|
$this->helper = Helper::getInstance(); |
|
67
|
|
|
} else { |
|
68
|
|
|
$this->helper = $helper; |
|
69
|
|
|
} |
|
70
|
|
|
// setup handlers |
|
71
|
|
|
$this->pollHandler = $this->helper->getHandler('Poll'); |
|
72
|
|
|
$this->optionHandler = $this->helper->getHandler('Option'); |
|
73
|
|
|
$this->logHandler = $this->helper->getHandler('Log'); |
|
74
|
|
|
|
|
75
|
|
|
if ($poll instanceof Poll) { |
|
76
|
|
|
$this->pollObj = $poll; |
|
77
|
|
|
} elseif (!empty($poll) && ((int)$poll > 0)) { |
|
78
|
|
|
$this->pollObj = $this->pollHandler->get((int)$poll); |
|
79
|
|
|
} else { |
|
80
|
|
|
$this->pollObj = $this->pollHandler->create(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* create html form to display poll |
|
86
|
|
|
* @access public |
|
87
|
|
|
* @return string html form for display |
|
88
|
|
|
*/ |
|
89
|
|
|
public function renderForm() |
|
90
|
|
|
{ |
|
91
|
|
|
$myTpl = new \XoopsTpl(); |
|
92
|
|
|
$this->assignForm($myTpl); // get the poll information |
|
93
|
|
|
|
|
94
|
|
|
return $myTpl->fetch($GLOBALS['xoops']->path('modules/xoopspoll/templates/xoopspoll_view.tpl')); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* assigns form values to template for display |
|
99
|
|
|
* @access public |
|
100
|
|
|
* @var \XoopsTpl $tpl |
|
101
|
|
|
*/ |
|
102
|
|
|
public function assignForm(\XoopsTpl $tpl) |
|
103
|
|
|
{ |
|
104
|
|
|
$myts = \MyTextSanitizer::getInstance(); |
|
105
|
|
|
$optionObjs = $this->optionHandler->getAllByPollId($this->pollObj->getVar('poll_id')); |
|
106
|
|
|
|
|
107
|
|
|
if (empty($optionObjs)) { |
|
108
|
|
|
/* there was a problem with missing Options */ |
|
109
|
|
|
// redirect_header(Request::getString('HTTP_REFERER', '', 'SERVER'), Constants::REDIRECT_DELAY_MEDIUM, _MD_XOOPSPOLL_ERROR_OPTIONS_MISSING); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (Constants::MULTIPLE_SELECT_POLL === $this->pollObj->getVar('multiple')) { |
|
113
|
|
|
$optionType = 'checkbox'; |
|
114
|
|
|
$optionName = 'option_id[]'; |
|
115
|
|
|
} else { |
|
116
|
|
|
$optionType = 'radio'; |
|
117
|
|
|
$optionName = 'option_id'; |
|
118
|
|
|
} |
|
119
|
|
|
foreach ($optionObjs as $optionObj) { |
|
120
|
|
|
$options[] = [ |
|
121
|
|
|
'input' => "<input type='{$optionType}' " . "name='{$optionName}' " . "value='" . $optionObj->getVar('option_id') . "'>", |
|
122
|
|
|
'text' => $optionObj->getVar('option_text'), |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
$uid = (isset($GLOBALS['xoopsUser']) |
|
126
|
|
|
&& \is_object($GLOBALS['xoopsUser'])) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
|
127
|
|
|
$can_vote = false; |
|
128
|
|
|
if ($this->pollObj->isAllowedToVote() |
|
|
|
|
|
|
129
|
|
|
&& (!$this->logHandler->hasVoted($this->pollObj->getVar('poll_id'), \xoops_getenv('REMOTE_ADDR'), $uid))) { |
|
130
|
|
|
$can_vote = true; |
|
131
|
|
|
} |
|
132
|
|
|
/* |
|
133
|
|
|
$tpl->assign('poll', array( |
|
134
|
|
|
'question' => $myts->htmlSpecialChars($this->pollObj->getVar('question')), |
|
135
|
|
|
'pollId' => $this->pollObj->getVar('poll_id'), |
|
136
|
|
|
'viewresults' => $GLOBALS['xoops']->url("modules/xoopspoll/pollresults.php") . "?poll_id=" . $this->pollObj->getVar('poll_id'), |
|
137
|
|
|
'options' => $options, |
|
138
|
|
|
'description' => $myts->displayTarea($myts->undoHtmlSpecialChars($this->pollObj->getVar('description')), 1)) |
|
139
|
|
|
); |
|
140
|
|
|
*/ |
|
141
|
|
|
$tpl->assign( |
|
142
|
|
|
[ |
|
143
|
|
|
'poll' => [ |
|
144
|
|
|
'question' => $myts->htmlSpecialChars($this->pollObj->getVar('question')), |
|
145
|
|
|
'pollId' => $this->pollObj->getVar('poll_id'), |
|
146
|
|
|
'viewresults' => $GLOBALS['xoops']->url('modules/xoopspoll/pollresults.php') . '?poll_id=' . $this->pollObj->getVar('poll_id'), |
|
147
|
|
|
'options' => $options ?? '', |
|
148
|
|
|
'description' => $myts->displayTarea($myts->undoHtmlSpecialChars($this->pollObj->getVar('description')), 1), |
|
149
|
|
|
], |
|
150
|
|
|
'can_vote' => $can_vote, |
|
151
|
|
|
'action' => $GLOBALS['xoops']->url('modules/xoopspoll/index.php'), |
|
152
|
|
|
'lang_vote' => \_MD_XOOPSPOLL_VOTE, |
|
153
|
|
|
'lang_results' => \_MD_XOOPSPOLL_RESULTS, |
|
154
|
|
|
] |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* display html results to screen (echo) |
|
160
|
|
|
* @access public |
|
161
|
|
|
*/ |
|
162
|
|
|
public function renderResults() |
|
163
|
|
|
{ |
|
164
|
|
|
$myTpl = new \XoopsTpl(); |
|
165
|
|
|
$this->assignResults($myTpl); // get the poll information |
|
166
|
|
|
|
|
167
|
|
|
return $myTpl->fetch($GLOBALS['xoops']->path('modules/xoopspoll/templates/xoopspoll_results_renderer.tpl')); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* assigns form results to template |
|
172
|
|
|
* @access public |
|
173
|
|
|
* @var \XoopsTpl tpl |
|
174
|
|
|
*/ |
|
175
|
|
|
public function assignResults(\XoopsTpl $tpl) |
|
176
|
|
|
{ |
|
177
|
|
|
$myts = \MyTextSanitizer::getInstance(); |
|
178
|
|
|
$xuEndTimestamp = \xoops_getUserTimestamp($this->pollObj->getVar('end_time')); |
|
179
|
|
|
$xuEndFormatted = \ucfirst(\date(_MEDIUMDATESTRING, $xuEndTimestamp)); |
|
180
|
|
|
$xuStartTimestamp = \xoops_getUserTimestamp($this->pollObj->getVar('start_time')); |
|
181
|
|
|
$xuStartFormatted = \ucfirst(\date(_MEDIUMDATESTRING, $xuStartTimestamp)); |
|
182
|
|
|
|
|
183
|
|
|
// $logHandler = $this->helper->getHandler('Log'); |
|
184
|
|
|
$criteria = new \CriteriaCompo(); |
|
185
|
|
|
$criteria->add(new \Criteria('poll_id', $this->pollObj->getVar('poll_id'), '=')); |
|
|
|
|
|
|
186
|
|
|
$criteria->setSort('option_id'); |
|
187
|
|
|
$optObjsArray = $this->optionHandler->getAll($criteria); |
|
188
|
|
|
$total = $this->pollObj->getVar('votes'); |
|
189
|
|
|
$i = 0; |
|
190
|
|
|
foreach ($optObjsArray as $optObj) { |
|
191
|
|
|
$optionVars = $optObj->getValues(); |
|
192
|
|
|
$percent = ($total > 0) ? (100 * $optionVars['option_count'] / $total) : 0; |
|
193
|
|
|
if ($percent > 0) { |
|
194
|
|
|
$width = (int)($percent * 2); |
|
195
|
|
|
$options[$i]['image'] = "<img src='" . $GLOBALS['xoops']->url("modules/xoopspoll/assets/images/colorbars/{$optionVars['option_color']}'") . " style='height: 14px; width: {$width}px; vertical-align: middle;' alt='" . (int)$percent . "%'>"; |
|
196
|
|
|
} else { |
|
197
|
|
|
$options[$i]['image'] = ''; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/* setup module config handler - required since this is called by newbb too */ |
|
201
|
|
|
/** @var \XoopsModuleHandler $moduleHandler */ |
|
202
|
|
|
$moduleHandler = \xoops_getHandler('module'); |
|
203
|
|
|
$configHandler = \xoops_getHandler('config'); |
|
204
|
|
|
$xp_module = $moduleHandler->getByDirname('xoopspoll'); |
|
205
|
|
|
$module_id = $xp_module->getVar('mid'); |
|
206
|
|
|
$xp_config = $configHandler->getConfigsByCat(0, $module_id); |
|
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
if ($xp_config['disp_vote_nums']) { |
|
209
|
|
|
$options[$i]['percent'] = \sprintf(' %01.1f%% (%d)', $percent, $optionVars['option_count']); |
|
210
|
|
|
} else { |
|
211
|
|
|
$options[$i]['percent'] = \sprintf(' %01.1f%%', $percent); |
|
212
|
|
|
} |
|
213
|
|
|
$options[$i]['text'] = $optionVars['option_text']; |
|
214
|
|
|
$options[$i]['total'] = $optionVars['option_count']; |
|
215
|
|
|
$options[$i]['value'] = (int) $percent; |
|
216
|
|
|
++$i; |
|
217
|
|
|
unset($optionVars); |
|
218
|
|
|
} |
|
219
|
|
|
$uid = (isset($GLOBALS['xoopsUser']) |
|
220
|
|
|
&& \is_object($GLOBALS['xoopsUser'])) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
|
221
|
|
|
$vote = null; |
|
222
|
|
|
if (!$this->pollObj->hasExpired() && $this->pollObj->isAllowedToVote() |
|
|
|
|
|
|
223
|
|
|
&& !$this->logHandler->hasVoted($this->pollObj->getVar('poll_id'), \xoops_getenv('REMOTE_ADDR'), $uid)) { |
|
224
|
|
|
$vote = "<a href='" . $GLOBALS['xoops']->url('modules/xoopspoll/index.php') . '?poll_id=' . $this->pollObj->getVar('poll_id') . "'>" . \_MD_XOOPSPOLL_VOTE_NOW . '</a>'; |
|
225
|
|
|
} |
|
226
|
|
|
if ($xp_config['disp_vote_nums']) { |
|
|
|
|
|
|
227
|
|
|
$totalVotes = \sprintf(\_MD_XOOPSPOLL_TOTALVOTES, $total); |
|
|
|
|
|
|
228
|
|
|
$totalVoters = \sprintf(\_MD_XOOPSPOLL_TOTALVOTERS, $this->pollObj->getVar('voters')); |
|
229
|
|
|
} else { |
|
230
|
|
|
$totalVotes = $totalVoters = ''; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
$tpl->assign( |
|
234
|
|
|
'poll', |
|
235
|
|
|
[ |
|
236
|
|
|
'question' => $myts->htmlSpecialChars($this->pollObj->getVar('question')), |
|
237
|
|
|
'end_text' => $xuEndFormatted, |
|
238
|
|
|
'start_text' => $xuStartFormatted, |
|
239
|
|
|
'totalVotes' => $totalVotes, |
|
240
|
|
|
'totalVoters' => $totalVoters, |
|
241
|
|
|
'vote' => $vote, |
|
242
|
|
|
'options' => $options, |
|
|
|
|
|
|
243
|
|
|
'description' => $this->pollObj->getVar('description'), //allow html |
|
244
|
|
|
] |
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|