1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of pollsFactory, a plugin for Dotclear 2. |
---|
4 | # |
---|
5 | # Copyright (c) 2009-2010 JC Denis and contributors |
---|
6 | # jcdenis@gdwd.com |
---|
7 | # |
---|
8 | # Licensed under the GPL version 2.0 license. |
---|
9 | # A copy of this license is available in LICENSE file or at |
---|
10 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | |
---|
13 | if (!defined('DC_CONTEXT_ADMIN')){return;} |
---|
14 | |
---|
15 | # Polls voters lists |
---|
16 | class adminPeopleList extends adminGenericList |
---|
17 | { |
---|
18 | public function peopleDisplay($page,$nb_per_page,$enclose_block='') |
---|
19 | { |
---|
20 | $echo = ''; |
---|
21 | if ($this->rs->isEmpty()) |
---|
22 | { |
---|
23 | $echo .= '<p><strong>'.__('No user').'</strong></p>'; |
---|
24 | } |
---|
25 | else |
---|
26 | { |
---|
27 | $pager = new pager($page,$this->rs_count,$nb_per_page,10); |
---|
28 | $pager->html_prev = $this->html_prev; |
---|
29 | $pager->html_next = $this->html_next; |
---|
30 | $pager->var_page = 'u_page'; |
---|
31 | |
---|
32 | $html_block = |
---|
33 | '<table class="clear">'. |
---|
34 | '<tr>'. |
---|
35 | '<th colspan="2">'.__('Ip').'</th>'. |
---|
36 | '<tr>'. |
---|
37 | '</tr>%s</table>'; |
---|
38 | |
---|
39 | if ($enclose_block) { |
---|
40 | $html_block = sprintf($enclose_block,$html_block); |
---|
41 | } |
---|
42 | |
---|
43 | $echo .= '<p>'.__('Page(s)').' : '.$pager->getLinks().'</p>'; |
---|
44 | |
---|
45 | $blocks = explode('%s',$html_block); |
---|
46 | |
---|
47 | $echo .= $blocks[0]; |
---|
48 | |
---|
49 | while ($this->rs->fetch()) |
---|
50 | { |
---|
51 | $echo .= $this->peopleLine(); |
---|
52 | } |
---|
53 | |
---|
54 | $echo .= $blocks[1]; |
---|
55 | |
---|
56 | $echo .= '<p>'.__('Page(s)').' : '.$pager->getLinks().'</p>'; |
---|
57 | } |
---|
58 | return $echo; |
---|
59 | } |
---|
60 | |
---|
61 | private function peopleLine() |
---|
62 | { |
---|
63 | $u_page = isset($_REQUEST['u_page']) ? abs((integer) $_REQUEST['u_page']) : 1; |
---|
64 | $u_limit = isset($_REQUEST['u_limit']) ? abs((integer) $_REQUEST['u_limit']) : 20; |
---|
65 | |
---|
66 | $res = |
---|
67 | '<tr class="line">'. |
---|
68 | '<td class="nowrap">'.form::checkbox(array('entries[]'),$this->rs->option_title).' </td>'. |
---|
69 | '<td class="maximal"><a href="plugin.php?p=pollsFactory&tab=result&sub=user&poll_id='.$this->rs->post_id.'&people_id='.$this->rs->option_title.'&section=responses&u_page='.$u_page.'&u_limit='.$u_limit.'" title="'.__('Show user').'">'. |
---|
70 | html::escapeHTML($this->rs->option_title).'</a></td>'. |
---|
71 | '</tr>'; |
---|
72 | |
---|
73 | return $res; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | $section = isset($_REQUEST['section']) ? $_REQUEST['section'] : ''; |
---|
78 | $sub = isset($_REQUEST['sub']) && in_array($_REQUEST['sub'],array('query','user')) ? $_REQUEST['sub'] : 'query'; |
---|
79 | |
---|
80 | $poll_id = isset($_REQUEST['poll_id']) ? $_REQUEST['poll_id'] : -1; |
---|
81 | $poll_id = (integer) $poll_id; |
---|
82 | |
---|
83 | $poll_params['post_type'] = 'pollsfactory'; |
---|
84 | $poll_params['post_id'] = $poll_id; |
---|
85 | $poll_params['no_content'] = true; |
---|
86 | $poll_params['limit'] = 1; |
---|
87 | $poll = $core->blog->getPosts($poll_params); |
---|
88 | |
---|
89 | # No poll |
---|
90 | if ($poll->isEmpty()) { |
---|
91 | $core->error->add(__('no such poll')); |
---|
92 | |
---|
93 | echo |
---|
94 | '<html>'. |
---|
95 | '<head><title>'.__('Polls manager').'</title>'.$header.'</head>'. |
---|
96 | '<body>'. |
---|
97 | '<h2>'.html::escapeHTML($core->blog->name). |
---|
98 | ' › <a href="'.$p_url.'&tab=polls">'.__('Polls').'</a>'. |
---|
99 | ' › '.__('Poll result'). |
---|
100 | ' - <a class="button" href="'.$p_url.'&tab=poll">'.__('New poll').'</a>'. |
---|
101 | '</h2>'. |
---|
102 | '<p><a href="'.$p_url.'&tab=polls" title="'.__('select poll').'">'. |
---|
103 | __('select poll to view from polls list').'</a></p>'; |
---|
104 | } |
---|
105 | # Results by user |
---|
106 | elseif ($sub == 'user') |
---|
107 | { |
---|
108 | $people_id = isset($_REQUEST['people_id']) ? $_REQUEST['people_id'] : null; |
---|
109 | $u_page = isset($_REQUEST['u_page']) ? abs((integer) $_REQUEST['u_page']) : 1; |
---|
110 | $u_limit = isset($_REQUEST['u_limit']) ? abs((integer) $_REQUEST['u_limit']) : 20; |
---|
111 | |
---|
112 | if (!empty($_POST['response_id']) && in_array($action,array('selectresponse','unselectresponse'))) |
---|
113 | { |
---|
114 | try { |
---|
115 | $id = (integer) $_POST['response_id']; |
---|
116 | |
---|
117 | $factory->updOptionSelected($id,($action == 'selectresponse')); |
---|
118 | |
---|
119 | http::redirect($p_url.'&tab=result&poll_id='.$poll_id.'&people_id='.$people_id.'&u_page='.$u_page.'&u_limit='.$u_limit.'&sub='.$sub.'§ion='.$section.'&msg='.$action); |
---|
120 | } |
---|
121 | catch (Exception $e) { |
---|
122 | $core->error->add($e->getMessage()); |
---|
123 | } |
---|
124 | } |
---|
125 | if (!empty($_POST['entries']) && $action == 'deletepeoples') |
---|
126 | { |
---|
127 | try { |
---|
128 | foreach($_POST['entries'] as $k => $id) |
---|
129 | { |
---|
130 | $del_peoples_params['option_type'] = 'pollsresponse'; |
---|
131 | $del_peoples_params['post_id'] = $poll_id; |
---|
132 | $del_peoples_params['option_title'] = $id; |
---|
133 | $del_peoples = $factory->getOptions($del_peoples_params); |
---|
134 | |
---|
135 | while($del_peoples->fetch()) |
---|
136 | { |
---|
137 | $factory->delOption($del_peoples->option_id); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | http::redirect($p_url.'&tab=result&poll_id='.$poll_id.'&people_id=&u_page='.$u_page.'&u_limit='.$u_limit.'&sub='.$sub.'§ion='.$section.'&msg='.$action); |
---|
142 | } |
---|
143 | catch (Exception $e) { |
---|
144 | $core->error->add($e->getMessage()); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | # Get peoples |
---|
149 | try { |
---|
150 | $peoples = $factory->getVotes($poll_id,(($u_page-1)*$u_limit),$u_limit); |
---|
151 | $counter = $factory->getVotes($poll_id); |
---|
152 | $peoples_list = new adminPeopleList($core,$peoples,$counter->count()); |
---|
153 | } catch (Exception $e) {echo $e->getMessage();exit(1); |
---|
154 | $core->error->add($e->getMessage()); |
---|
155 | } |
---|
156 | |
---|
157 | echo ' |
---|
158 | <html> |
---|
159 | <head><title>'.__('Polls manager').'</title>'.$header.'</head> |
---|
160 | <body>'.$msg.' |
---|
161 | <h2>'.html::escapeHTML($core->blog->name). |
---|
162 | ' › <a href="'.$p_url.'&tab=polls">'.__('Polls').'</a>'. |
---|
163 | ' › '.__('Results by user'). |
---|
164 | ' - <a class="button" href="'.$p_url.'&tab=poll">'.__('New poll').'</a>'. |
---|
165 | '</h2>'. |
---|
166 | '<p><a href="'.$core->getPostAdminURL($poll->post_type,$poll->post_id).'">« '. |
---|
167 | sprintf(__('Back to "%s"'),html::escapeHTML($poll->post_title)).'</a>'. |
---|
168 | ' - <a href="'.$p_url.'&tab=result&poll_id='.$poll_id.'&sub=query">'.__('Results by query').'</a></p>'; |
---|
169 | |
---|
170 | if ($people_id != '') { |
---|
171 | echo '<div class="two-cols"><div class="col">'; |
---|
172 | } |
---|
173 | echo ' |
---|
174 | <fieldset id="users"><legend>'.__('Users').'</legend>'. |
---|
175 | $peoples_list->peopleDisplay($u_page,$u_limit, |
---|
176 | '<form action="plugin.php" method="post" id="form-entries">'. |
---|
177 | '%s'. |
---|
178 | '<div class="two-cols">'. |
---|
179 | '<p class="col checkboxes-helpers"></p>'. |
---|
180 | '<p class="col right">'. |
---|
181 | '<input type="submit" value="'.__('delete selected users') .'" /></p>'. |
---|
182 | form::hidden(array('action'),'deletepeoples'). |
---|
183 | form::hidden(array('poll_id'),$poll_id). |
---|
184 | form::hidden(array('u_page'),$u_page). |
---|
185 | form::hidden(array('u_limit'),$u_limit). |
---|
186 | form::hidden(array('section'),'users'). |
---|
187 | form::hidden(array('sub'),'user'). |
---|
188 | form::hidden(array('tab'),'result'). |
---|
189 | form::hidden(array('p'),'pollsFactory'). |
---|
190 | $core->formNonce(). |
---|
191 | '</div>'. |
---|
192 | '</form>' |
---|
193 | ). |
---|
194 | '</fieldset>'; |
---|
195 | |
---|
196 | if ($people_id != '') { |
---|
197 | echo '</div><div class="col"><fieldset id="responses"><legend>'.__('Responses').'</legend>'; |
---|
198 | |
---|
199 | $queries_params['option_type'] = 'pollsquery'; |
---|
200 | $queries_params['post_id'] = $poll_id; |
---|
201 | $queries = $factory->getOptions($queries_params); |
---|
202 | if (!$queries->isEmpty()) { |
---|
203 | while ($queries->fetch()) { |
---|
204 | |
---|
205 | echo '<h3>'.html::escapeHTML($queries->option_title).'</h3><p><em>('.array_search($queries->option_meta,$factory->getQueryTypes()).')</em></p>'; |
---|
206 | |
---|
207 | $rsp_params['option_type'] = 'pollsresponse'; |
---|
208 | $rsp_params['post_id'] = $poll_id; |
---|
209 | $rsp_params['option_meta'] = $queries->option_id; |
---|
210 | $rsp_params['option_title'] = $people_id; |
---|
211 | $rsp = $factory->getOptions($rsp_params); |
---|
212 | |
---|
213 | if ($rsp->isEmpty() || $rsp->option_content == '') { |
---|
214 | echo '<p>'.__('User has not answered this query').'</p>'; |
---|
215 | } |
---|
216 | else { |
---|
217 | if (in_array($queries->option_meta,array('field','textarea'))) { |
---|
218 | echo |
---|
219 | '<form action="plugin.php" method="post"><p>'. |
---|
220 | html::escapeHTML($rsp->option_content).'</p><p>'; |
---|
221 | if ($rsp->option_selected) { |
---|
222 | echo |
---|
223 | '<input type="submit" value="'.__('Remove this response from public side').'" />'. |
---|
224 | form::hidden(array('action'),'unselectresponse'); |
---|
225 | } |
---|
226 | else { |
---|
227 | echo |
---|
228 | '<input type="submit" value="'.__('Show this response on public side').'" />'. |
---|
229 | form::hidden(array('action'),'selectresponse'); |
---|
230 | } |
---|
231 | echo |
---|
232 | form::hidden(array('response_id'),$rsp->option_id). |
---|
233 | form::hidden(array('poll_id'),$poll_id). |
---|
234 | form::hidden(array('people_id'),$people_id). |
---|
235 | form::hidden(array('u_page'),$u_page). |
---|
236 | form::hidden(array('u_limit'),$u_limit). |
---|
237 | form::hidden(array('section'),'responses'). |
---|
238 | form::hidden(array('sub'),'user'). |
---|
239 | form::hidden(array('tab'),'result'). |
---|
240 | form::hidden(array('p'),'pollsFactory'). |
---|
241 | $core->formNonce(). |
---|
242 | '</p></form>'; |
---|
243 | } |
---|
244 | else { |
---|
245 | while($rsp->fetch()) { |
---|
246 | echo '<ul>'; |
---|
247 | $selection_params['option_type'] = 'pollsselection'; |
---|
248 | $selection_params['post_id'] = $poll_id; |
---|
249 | $selection_params['option_id'] = $rsp->option_content; |
---|
250 | $selection_params['limit'] = 1; |
---|
251 | $selection = $factory->getOptions($selection_params); |
---|
252 | if ($selection->isEmpty()) { |
---|
253 | echo '<li>'.__('Failed to retrieve option title').'</li>'; |
---|
254 | } |
---|
255 | else { |
---|
256 | echo '<li>'.html::escapeHTML($selection->option_title).'</li>'; |
---|
257 | } |
---|
258 | echo '</ul>'; |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | echo '</p>'; |
---|
264 | } |
---|
265 | } |
---|
266 | echo '</fieldset></div></div>'; |
---|
267 | } |
---|
268 | } |
---|
269 | # Results by query |
---|
270 | else |
---|
271 | { |
---|
272 | $query_id = isset($_REQUEST['query_id']) ? $_REQUEST['query_id'] : -1; |
---|
273 | $r_page = isset($_REQUEST['r_page']) ? abs((integer) $_REQUEST['r_page']) : 1; |
---|
274 | $r_limit = isset($_REQUEST['r_limit']) ? abs((integer) $_REQUEST['r_limit']) : 20; |
---|
275 | $count = (integer) $factory->countVotes($poll->post_id); |
---|
276 | |
---|
277 | if ($action == 'selectresponses') |
---|
278 | { |
---|
279 | try { |
---|
280 | foreach($_POST['responses_list'] as $k => $id) |
---|
281 | { |
---|
282 | $factory->updOptionSelected($id,$_POST['responses_sel'][$id]); |
---|
283 | } |
---|
284 | http::redirect($p_url.'&tab=result&poll_id='.$poll_id.'&query_id='.$query_id.'&r_page='.$r_page.'&r_limit='.$r_limit.'&sub='.$sub.'&msg='.$action); |
---|
285 | } |
---|
286 | catch (Exception $e) { |
---|
287 | $core->error->add($e->getMessage()); |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | echo ' |
---|
292 | |
---|
293 | <html> |
---|
294 | <head><title>'.__('Polls manager').'</title>'.$header.'</head> |
---|
295 | <body>'.$msg.' |
---|
296 | <h2>'.html::escapeHTML($core->blog->name). |
---|
297 | ' › <a href="'.$p_url.'&tab=polls">'.__('Polls').'</a>'. |
---|
298 | ' › '.__('Results by query'). |
---|
299 | ' - <a class="button" href="'.$p_url.'&tab=poll">'.__('New poll').'</a>'. |
---|
300 | '</h2>'. |
---|
301 | '<p><a href="'.$core->getPostAdminURL($poll->post_type,$poll->post_id).'">« '. |
---|
302 | sprintf(__('Back to "%s"'),html::escapeHTML($poll->post_title)).'</a>'. |
---|
303 | ' - <a href="'.$p_url.'&tab=result&poll_id='.$poll_id.'&sub=user">'.__('Results by user').'</a></p>'; |
---|
304 | |
---|
305 | |
---|
306 | # Queries |
---|
307 | $queries_params['option_type'] = 'pollsquery'; |
---|
308 | $queries_params['post_id'] = $poll_id; |
---|
309 | $queries = $factory->getOptions($queries_params); |
---|
310 | |
---|
311 | # Query |
---|
312 | $query_params['option_type'] = 'pollsquery'; |
---|
313 | $query_params['post_id'] = $poll_id; |
---|
314 | $query_params['option_id'] = $query_id; |
---|
315 | $query = $factory->getOptions($query_params); |
---|
316 | |
---|
317 | |
---|
318 | if (!$query->isEmpty()) { |
---|
319 | echo '<div class="two-cols"><div class="col">'; |
---|
320 | } |
---|
321 | |
---|
322 | echo '<fieldset id="queries"><legend>'.__('Queries').'</legend><ul>'; |
---|
323 | |
---|
324 | while ($queries->fetch()) |
---|
325 | { |
---|
326 | if ($query_id == $queries->option_id) { |
---|
327 | echo '<li>'.html::escapeHTML($queries->option_title).'<br /><em>('.array_search($queries->option_meta,$factory->getQueryTypes()).')</em></li>'; |
---|
328 | } |
---|
329 | else { |
---|
330 | echo '<li><a href="'.$p_url.'&tab=result&section=responses&poll_id='.$poll_id.'&query_id='.$queries->option_id.'">'.html::escapeHTML($queries->option_title).'</a><br /><em>('.array_search($queries->option_meta,$factory->getQueryTypes()).')</em></li>'; |
---|
331 | } |
---|
332 | } |
---|
333 | echo '</ul></fieldset>'; |
---|
334 | |
---|
335 | if (!$query->isEmpty()) { |
---|
336 | |
---|
337 | echo '</div><div class="col"><fieldset id="responses"><legend>'.__('Responses').'</legend>'; |
---|
338 | |
---|
339 | $selections_params['option_type'] = 'pollsselection'; |
---|
340 | $selections_params['post_id'] = $poll_id; |
---|
341 | $selections_params['option_meta'] = $query_id; |
---|
342 | |
---|
343 | $selections = $factory->getOptions($selections_params); |
---|
344 | if ($query->isEmpty() || $selections->isEmpty()) { |
---|
345 | echo '<p>'.__('There is no response for this query.').'</p>'; |
---|
346 | } |
---|
347 | else { |
---|
348 | $rs = array(); |
---|
349 | $responses_params = array('option_meta' => $query->option_id); |
---|
350 | $responses_params['option_type'] = 'pollsresponse'; |
---|
351 | $responses_params['post_id'] = $poll_id; |
---|
352 | |
---|
353 | # limit results for numeric (id) responses |
---|
354 | if (in_array($query->option_type,array('checkbox','radio','combo'))) { |
---|
355 | $responses = $factory->getResponses($responses_params); |
---|
356 | } |
---|
357 | # limit results for text responses |
---|
358 | else { |
---|
359 | $r_count = $factory->getOptions($responses_params,true)->f(0); |
---|
360 | $responses_params['limit'] = array(($r_page * $r_limit - $r_limit),$r_limit); |
---|
361 | |
---|
362 | $responses = $factory->getOptions($responses_params); |
---|
363 | |
---|
364 | if ($r_count > $responses->count()) { |
---|
365 | $pager = new pager($r_page,$r_count,$r_limit); |
---|
366 | $pager->var_page = 'r_page'; |
---|
367 | $pager->base_url = $p_url.'&tab=result&section=responses&poll_id='.$poll_id.'&query_id='.$queries->option_id.'&r_limit='.$r_limit.'&r_page=%s'; |
---|
368 | echo $pager->getLinks(); |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | # Loop through responses for this query |
---|
373 | while($responses->fetch()) |
---|
374 | { |
---|
375 | switch($query->option_meta) { |
---|
376 | |
---|
377 | case 'checkbox': |
---|
378 | case 'radio': |
---|
379 | case 'combo': |
---|
380 | if (!isset($rs[$responses->option_content])) { |
---|
381 | $rs[$responses->option_content] = 0; |
---|
382 | } |
---|
383 | $rs[$responses->option_content] += 1; |
---|
384 | break; |
---|
385 | |
---|
386 | case 'field': |
---|
387 | case 'textarea': |
---|
388 | if ('' != $responses->option_content) { |
---|
389 | $rs[] = |
---|
390 | '<tr class="line">'. |
---|
391 | '<td class="minimal nowrap">'. |
---|
392 | form::hidden(array('responses_list[]'),$responses->option_id). |
---|
393 | form::checkbox(array('responses_sel['.$responses->option_id.']'),1,$responses->option_selected). |
---|
394 | '</td>'. |
---|
395 | '<td class="maximal">'.html::escapeHTML(substr($responses->option_content,0,350)).'</td>'. |
---|
396 | '</tr>'; |
---|
397 | } |
---|
398 | default: |
---|
399 | break; |
---|
400 | } |
---|
401 | } |
---|
402 | # There's something to show |
---|
403 | if (!empty($rs)) |
---|
404 | { |
---|
405 | # for numeric (id) responses |
---|
406 | if (in_array($query->option_meta,array('checkbox','radio','combo'))) { |
---|
407 | # Sort responses by number of votes |
---|
408 | $rs_sort = array(); |
---|
409 | while($selections->fetch()) |
---|
410 | { |
---|
411 | $nb = isset($rs[$selections->option_id]) ? $rs[$selections->option_id] : 0; |
---|
412 | $percent = $count ? ceil($nb / $count * 100).'%' : ''; |
---|
413 | |
---|
414 | $rs_sort[] = array( |
---|
415 | 'nb'=>$nb, |
---|
416 | 'text'=>'<tr class="line"><td class="maximal">'.html::escapeHTML($selections->option_title).'</td><td class="nowrap"> '.$percent.'</td><td class="nowrap">'.sprintf(__('%s votes'),$nb).'</td></tr>' |
---|
417 | ); |
---|
418 | } |
---|
419 | $sorted = staticRecord::newFromArray($rs_sort); |
---|
420 | $sorted->sort('nb','desc'); |
---|
421 | |
---|
422 | # Parse responses |
---|
423 | echo '<table>'; |
---|
424 | while($sorted->fetch()) |
---|
425 | { |
---|
426 | echo $sorted->text; |
---|
427 | } |
---|
428 | echo '</table>'; |
---|
429 | } |
---|
430 | # for text responses |
---|
431 | else { |
---|
432 | echo |
---|
433 | '<table>'. |
---|
434 | '<form method="post" action="plugin.php">'. |
---|
435 | implode('',$rs). |
---|
436 | '</table>'. |
---|
437 | '<p>'. |
---|
438 | '<input type="submit" name="save" value="'.__('Show selected responses on public side').'" />'. |
---|
439 | $GLOBALS['core']->formNonce(). |
---|
440 | form::hidden(array('action'),'selectresponses'). |
---|
441 | form::hidden(array('poll_id'),$poll_id). |
---|
442 | form::hidden(array('query_id'),$query_id). |
---|
443 | form::hidden(array('r_limit'),$r_limit). |
---|
444 | form::hidden(array('r_page'),$r_page). |
---|
445 | form::hidden(array('p'),'pollsFactory'). |
---|
446 | form::hidden(array('tab'),'result'). |
---|
447 | form::hidden(array('section'),'queries'). |
---|
448 | '</form>'; |
---|
449 | } |
---|
450 | } |
---|
451 | else { |
---|
452 | echo '<p>'.__('There is no response for this query.').'</p>'; |
---|
453 | } |
---|
454 | } |
---|
455 | echo '</fieldset></div></div>'; |
---|
456 | } |
---|
457 | } |
---|
458 | dcPage::helpBlock('pollsFactory'); |
---|
459 | echo $footer.'</body></html>'; |
---|
460 | ?> |
---|