Index: trunk/extensions/AJAXPoll/AJAXPoll.php |
— | — | @@ -19,7 +19,7 @@ |
20 | 20 | * @author Jack Phoenix <jack@countervandalism.net> |
21 | 21 | * @author Thomas Gries |
22 | 22 | * @maintainer Thomas Gries |
23 | | - * @version 1.600 |
| 23 | + * @version 1.603 |
24 | 24 | * @link http://www.mediawiki.org/wiki/Extension:AJAX_Poll Documentation |
25 | 25 | */ |
26 | 26 | |
— | — | @@ -31,7 +31,7 @@ |
32 | 32 | $wgExtensionCredits['parserhook'][] = array( |
33 | 33 | 'path' => __FILE__, |
34 | 34 | 'name' => 'AJAX Poll', |
35 | | - 'version' => '1.602 20120219', |
| 35 | + 'version' => '1.603 20120219', |
36 | 36 | 'author' => array( 'Dariusz Siedlecki', 'Jack Phoenix', 'Thomas Gries' ), |
37 | 37 | 'descriptionmsg' => 'ajaxpoll-desc', |
38 | 38 | 'url' => 'https://www.mediawiki.org/wiki/Extension:AJAX_Poll', |
— | — | @@ -40,340 +40,6 @@ |
41 | 41 | // Internationalization + AJAX function |
42 | 42 | $dir = dirname( __FILE__ ) . '/'; |
43 | 43 | $wgExtensionMessagesFiles['AJAXPoll'] = $dir . 'AJAXPoll.i18n.php'; |
| 44 | +$wgAutoloadClasses['AJAXPoll'] = $dir . 'AJAXPoll_body.php'; |
44 | 45 | $wgAjaxExportList[] = 'AJAXPoll::submitVote'; |
45 | 46 | $wgHooks['ParserFirstCallInit'][] = 'AJAXPoll::AJAXPollParserInit'; |
46 | | - |
47 | | -class AJAXPoll { |
48 | | - |
49 | | - /** |
50 | | - * Register <poll> tag with the parser |
51 | | - * |
52 | | - * @param $parser Object: instance of Parser (not necessarily $wgParser) |
53 | | - * @return Boolean: true |
54 | | - */ |
55 | | - static function AJAXPollParserInit( $parser ) { |
56 | | - $parser->setHook( 'poll', array( __CLASS__, 'AJAXPollRender' ) ); |
57 | | - return true; |
58 | | - } |
59 | | - |
60 | | - # The callback function for converting the input text to HTML output |
61 | | - static function AJAXPollRender( $input ) { |
62 | | - global $wgParser, $wgUser, $wgOut, $wgTitle, $wgScriptPath; |
63 | | - |
64 | | - $wgParser->disableCache(); |
65 | | - |
66 | | - if ( $wgUser->getName() == '' ) { |
67 | | - $user = wfGetIP(); |
68 | | - } else { |
69 | | - $user = $wgUser->getName(); |
70 | | - } |
71 | | - |
72 | | - // ID of the poll |
73 | | - $ID = strtoupper( md5( $input ) ); |
74 | | - |
75 | | - $par = new Parser(); |
76 | | - $input = $par->parse( $input, $wgTitle, $wgOut->parserOptions() ); |
77 | | - $input = trim( strip_tags( $input->getText() ) ); |
78 | | - $lines = explode( "\n", trim( $input ) ); |
79 | | - |
80 | | - // Deprecating AJAX |
81 | | - /*if ( isset( $_POST['ajaxpoll-post-id'] ) && isset( $_POST['ajaxpoll-post-answer'] ) && $_POST['ajaxpoll-post-id'] == $ID ) { |
82 | | - AJAXPoll::submitVote( $_POST['ajaxpoll-post-id'], intval( $_POST['ajaxpoll-post-answer'] ) ); |
83 | | - }*/ |
84 | | - |
85 | | - $dbw = wfGetDB( DB_MASTER ); |
86 | | - $dbw->begin(); |
87 | | - /** |
88 | | - * Register poll in the database |
89 | | - */ |
90 | | - $row = $dbw->selectRow( |
91 | | - array( 'poll_info' ), |
92 | | - array( 'COUNT(poll_id) AS count' ), |
93 | | - array( 'poll_id' => $ID ), |
94 | | - __METHOD__ |
95 | | - ); |
96 | | - |
97 | | - if( empty( $row->count ) ) { |
98 | | - $dbw->insert( |
99 | | - 'poll_info', |
100 | | - array( |
101 | | - 'poll_id' => $ID, |
102 | | - 'poll_txt' => $input, |
103 | | - 'poll_date' => wfTimestampNow(), |
104 | | - 'poll_title' => $wgParser->mTitle->getText() |
105 | | - ), |
106 | | - __METHOD__ |
107 | | - ); |
108 | | - } |
109 | | - $dbw->commit(); |
110 | | - |
111 | | - // Add CSS |
112 | | - $wgOut->addExtensionStyle( $wgScriptPath . '/extensions/AJAXPoll/AJAXPoll.css' ); |
113 | | - switch( $lines[0] ) { |
114 | | - case 'STATS': |
115 | | - $retVal = AJAXPoll::buildStats( $ID, $user ); |
116 | | - break; |
117 | | - default: |
118 | | - $retVal = ' |
119 | | -<div id="ajaxpoll-container-' . $ID . '">' . AJAXPoll::buildHTML( $ID, $user, $lines ) . '</div>'; |
120 | | - break; |
121 | | - } |
122 | | - return $retVal; |
123 | | - } |
124 | | - |
125 | | - private static function buildStats( $ID, $user ) { |
126 | | - |
127 | | - $dbw = wfGetDB( DB_MASTER ); |
128 | | - |
129 | | - $res = $dbw->select( |
130 | | - 'poll_vote', |
131 | | - array( |
132 | | - 'COUNT(*)', |
133 | | - 'COUNT(DISTINCT poll_id)', |
134 | | - 'COUNT(DISTINCT poll_user)', |
135 | | - 'TIMEDIFF(NOW(), MAX(poll_date))' |
136 | | - ), |
137 | | - array(), |
138 | | - __METHOD__ |
139 | | - ); |
140 | | - $tab = $dbw->fetchRow( $res ); |
141 | | - |
142 | | - $clock = explode( ':', $tab[3] ); |
143 | | - |
144 | | - if ( $clock[0] == '00' && $clock[1] == '00' ) { |
145 | | - $x = $clock[2]; |
146 | | - $y = 'second'; |
147 | | - } elseif( $clock[0] == '00' ) { |
148 | | - $x = $clock[1]; |
149 | | - $y = 'minute'; |
150 | | - } else { |
151 | | - if ( $clock[0] < 24 ) { |
152 | | - $x = $clock[0]; |
153 | | - $y = 'hour'; |
154 | | - } else { |
155 | | - $x = floor( $hr / 24 ); |
156 | | - $y = 'day'; |
157 | | - } |
158 | | - } |
159 | | - |
160 | | - $clockago = $x . ' ' . $y . ( $x > 1 ? 's' : '' ); |
161 | | - |
162 | | - $res = $dbw->select( |
163 | | - 'poll_vote', |
164 | | - 'COUNT(*)', |
165 | | - array( 'DATE_SUB(CURDATE(), INTERVAL 2 DAY) <= poll_date' ), |
166 | | - __METHOD__ |
167 | | - ); |
168 | | - $tab2 = $dbw->fetchRow( $res ); |
169 | | - |
170 | | - return "There are $tab[1] polls and $tab[0] votes given by $tab[2] different people.<br /> |
171 | | - The last vote has been given $clockago ago.<br/> |
172 | | - During the last 48 hours, $tab2[0] votes have been given."; |
173 | | - } |
174 | | - |
175 | | - public static function submitVote( $ID, $answer ) { |
176 | | - global $wgUser,$wgOut; |
177 | | - |
178 | | - $dbw = wfGetDB( DB_MASTER ); |
179 | | - |
180 | | - if ( $wgUser->getName() == '' ) { |
181 | | - $user = wfGetIP(); |
182 | | - } else { |
183 | | - $user = $wgUser->getName(); |
184 | | - } |
185 | | - |
186 | | - if ( $wgUser->isAllowed( 'bot' ) ) { |
187 | | - return AJAXPoll::buildHTML( $ID, $user ); |
188 | | - } |
189 | | - |
190 | | - $answer = ++$answer; |
191 | | - |
192 | | - $q = $dbw->select( |
193 | | - 'poll_vote', |
194 | | - 'COUNT(*) AS c', |
195 | | - array( |
196 | | - 'poll_id' => $ID, |
197 | | - 'poll_user' => $user |
198 | | - ), |
199 | | - __METHOD__ |
200 | | - ); |
201 | | - $row = $dbw->fetchRow( $q ); |
202 | | - |
203 | | - if ( $row['c'] > 0 ) { |
204 | | - |
205 | | - $updateQuery = $dbw->update( |
206 | | - 'poll_vote', |
207 | | - array( |
208 | | - 'poll_answer' => $answer, |
209 | | - 'poll_date' => wfTimestampNow() |
210 | | - ), |
211 | | - array( |
212 | | - 'poll_id' => $ID, |
213 | | - 'poll_user' => $user |
214 | | - ), |
215 | | - __METHOD__ |
216 | | - ); |
217 | | - $dbw->commit(); |
218 | | - $pollContainerText = ( $updateQuery ) ? 'ajaxpoll-vote-update' : 'ajaxpoll-vote-error'; |
219 | | - |
220 | | - } else { |
221 | | - |
222 | | - $insertQuery = $dbw->insert( |
223 | | - 'poll_vote', |
224 | | - array( |
225 | | - 'poll_id' => $ID, |
226 | | - 'poll_user' => $user, |
227 | | - 'poll_ip' => wfGetIP(), |
228 | | - 'poll_answer' => $answer, |
229 | | - 'poll_date' => wfTimestampNow() |
230 | | - ), |
231 | | - __METHOD__ |
232 | | - ); |
233 | | - $dbw->commit(); |
234 | | - $pollContainerText = ( $insertQuery ) ? 'ajaxpoll-vote-add' : 'ajaxpoll-vote-error'; |
235 | | - |
236 | | - } |
237 | | - |
238 | | - return AJAXPoll::buildHTML( $ID, $user, '', $pollContainerText ); |
239 | | - |
240 | | - } |
241 | | - |
242 | | - private static function buildHTML( $ID, $user, $lines = '', $extra_from_ajax = '' ) { |
243 | | - global $wgTitle, $wgLang, $wgUseAjax; |
244 | | - |
245 | | - $dbw = wfGetDB( DB_SLAVE ); |
246 | | - |
247 | | - $q = $dbw->select( |
248 | | - 'poll_info', |
249 | | - array( 'poll_txt', 'poll_date' ), |
250 | | - array( 'poll_id' => $ID ), |
251 | | - __METHOD__ |
252 | | - ); |
253 | | - $row = $dbw->fetchRow( $q ); |
254 | | - |
255 | | - if ( empty( $lines ) ) { |
256 | | - $lines = explode( "\n", trim( $row['poll_txt'] ) ); |
257 | | - } |
258 | | - |
259 | | - $start_date = $row['poll_date']; |
260 | | - |
261 | | - $q = $dbw->select( |
262 | | - 'poll_vote', |
263 | | - array( 'poll_answer', 'COUNT(*)' ), |
264 | | - array( 'poll_id' => $ID ), |
265 | | - __METHOD__, |
266 | | - array( 'GROUP BY' => 'poll_answer' ) |
267 | | - ); |
268 | | - |
269 | | - $poll_result = array(); |
270 | | - |
271 | | - while ( $row = $q->fetchRow() ) { |
272 | | - $poll_result[$row[0]] = $row[1]; |
273 | | - } |
274 | | - |
275 | | - $amountOfVotes = array_sum( $poll_result ); |
276 | | - |
277 | | - // Did we vote? |
278 | | - $q = $dbw->select( |
279 | | - 'poll_vote', |
280 | | - array( 'poll_answer', 'poll_date' ), |
281 | | - array( |
282 | | - 'poll_id' => $ID, |
283 | | - 'poll_user' => $user |
284 | | - ), |
285 | | - __METHOD__ |
286 | | - ); |
287 | | - |
288 | | - if ( $row = $dbw->fetchRow( $q ) ) { |
289 | | - $ourLastVoteDate = wfMsg( |
290 | | - 'ajaxpoll-your-vote', |
291 | | - $lines[$row[0] - 1], |
292 | | - $wgLang->timeanddate( wfTimestamp( TS_MW, $row[1] ), true /* adjust? */ ) |
293 | | - ); |
294 | | - } |
295 | | - |
296 | | - if ( is_object( $wgTitle ) ) { |
297 | | - if( !empty( $extra_from_ajax ) ) { |
298 | | - $attributes = ' style="display: block;"'; |
299 | | - $ajaxMessage = wfMsg( $extra_from_ajax ); |
300 | | - } else { |
301 | | - $attributes = ''; |
302 | | - $ajaxMessage = ''; |
303 | | - } |
304 | | - // HTML output has to be on one line thanks to a MediaWiki bug |
305 | | - // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=1319 |
306 | | - $ret = '<div id="ajaxpoll-id-' . $ID . '" class="ajaxpoll"> |
307 | | -<div id="ajaxpoll-ajax-' . $ID . '" class="ajaxpoll-ajax"' . $attributes . '>' . $ajaxMessage . '</div> |
308 | | -<script>var tmp; |
309 | | -function mover(x){ |
310 | | - var sp=$(x).find("span"); |
311 | | - tmp=sp.html(); |
312 | | - sp.text(sp.attr("title")); |
313 | | - sp.attr("title",""); |
314 | | -} |
315 | | -function mout(x){ |
316 | | - var sp=$(x).find("span"); |
317 | | - sp.attr("title",sp.text()); |
318 | | - sp.text(tmp); |
319 | | -} |
320 | | -</script> |
321 | | -<div class="ajaxpoll-question">' . strip_tags( $lines[0] ) . '</div>'; |
322 | | - |
323 | | - // Different message depending on if the user has already voted or not. |
324 | | - $message = ( isset( $row[0] ) ) ? $ourLastVoteDate : wfMsg( 'ajaxpoll-no-vote' ); |
325 | | - $ret .= '<div class="ajaxpoll-misc">' . $message . ' |
326 | | -</div>'; |
327 | | - |
328 | | - $ret .= '<form method="post" action="' . $wgTitle->getLocalURL() . |
329 | | - '" id="ajaxpoll-answer-id-' . $ID . '"><input type="hidden" name="ajaxpoll-post-id" value="' . $ID . '" />'; |
330 | | - |
331 | | - for ( $i = 1; $i < count( $lines ); $i++ ) { |
332 | | - $ans_no = $i - 1; |
333 | | - |
334 | | - if ( $amountOfVotes == 0 ) { |
335 | | - $percent = 0; |
336 | | - } else { |
337 | | - $percent = $wgLang->formatNum( round( ( isset( $poll_result[$i + 1] ) ? $poll_result[$i + 1] : 0 ) * 100 / $amountOfVotes, 2 ) ); |
338 | | - } |
339 | | - |
340 | | - $our = ( isset( $row[0] ) && ( $row[0] - 1 == $i ) ); |
341 | | - |
342 | | - // If AJAX is enabled, as it is by default in modern MWs, we can |
343 | | - // just use sajax library function here for that AJAX-y feel. |
344 | | - // If not, we'll have to submit the form old-school way... |
345 | | - if ( $wgUseAjax ) { |
346 | | - $submitJS = "sajax_do_call(\"AJAXPoll::submitVote\",[\"" . $ID . "\",\"" . $i . "\"], $(\"#ajaxpoll-container-" . $ID . "\")[0]);"; |
347 | | - } else { |
348 | | - $submitJS = "$(\"#ajaxpoll-answer-id-" . $ID . "\").submit();"; |
349 | | - } |
350 | | - |
351 | | - // HTML output has to be on one line thanks to a MediaWiki bug |
352 | | - // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=1319 |
353 | | - $ret .= " |
354 | | -<div id='ajaxpoll-answer-" . $ans_no . "' class='ajaxpoll-answer'><div class='ajaxpoll-answer-name'><label for='ajaxpoll-answer-radio-" . $ans_no . "' onclick='$(\"#ajaxpoll-ajax-" . $ID . "\").html(\"" . wfMsg( 'ajaxpoll-submitting' ) . "\");$(\"#ajaxpoll-ajax-" . $ID . "\").css(\"display\",\"block\");$(this).addClass(\"ajaxpoll-checkevent\").prop(\"checked\",true); " . $submitJS . "'><input type='radio' id='ajaxpoll-post-answer-" . $ans_no . "' name='ajaxpoll-post-answer' value='" . $i . "'" . ( $our ? 'checked=true ' : '' ) . "/>" . strip_tags( $lines[$i] ) . |
355 | | -"</label></div><div class='ajaxpoll-answer-vote" . ( $our ? ' ajaxpoll-our-vote' : '' ) ."' onmouseover='mover(this)' onmouseout='mout(this);'><span title='" . wfMsg( 'ajaxpoll-percent-votes', sprintf( $percent ) ) . "'>" . ( ( isset( $poll_result ) && !empty( $poll_result[$i + 1] ) ) ? $poll_result[$i + 1] : 0 ) . "</span><div style='width: " . $percent . "%;" . ( $percent == 0 ? ' border:0;' : '' ) . "'></div></div> |
356 | | -</div> |
357 | | -"; |
358 | | - } |
359 | | - |
360 | | - $ret .= '</form>'; |
361 | | - |
362 | | - // Display information about the poll (creation date, amount of votes) |
363 | | - $pollSummary = wfMsgExt( |
364 | | - 'ajaxpoll-info', |
365 | | - 'parsemag', // parse PLURAL |
366 | | - $amountOfVotes, // amount of votes |
367 | | - $wgLang->timeanddate( wfTimestamp( TS_MW, $start_date ), true /* adjust? */ ) |
368 | | - ); |
369 | | - |
370 | | - $ret .= '<div id="ajaxpoll-info-' . $ID . '" class="ajaxpoll-info">' . $pollSummary . '</div>'; |
371 | | - |
372 | | - $ret .= '</div>'; |
373 | | - } else { |
374 | | - $ret = ''; |
375 | | - } |
376 | | - |
377 | | - return $ret; |
378 | | - } |
379 | | - |
380 | | -} /* class AJAXPoll */ |
Index: trunk/extensions/AJAXPoll/AJAXPoll_body.php |
— | — | @@ -0,0 +1,350 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * AJAX Poll class |
| 5 | + * Created by Dariusz Siedlecki, based on the work by Eric David. |
| 6 | + * Licensed under the GFDL. |
| 7 | + * |
| 8 | + * @file |
| 9 | + * @ingroup Extensions |
| 10 | + * @author Dariusz Siedlecki <datrio@gmail.com> |
| 11 | + * @author Jack Phoenix <jack@countervandalism.net> |
| 12 | + * @author Thomas Gries |
| 13 | + * @maintainer Thomas Gries |
| 14 | + * @version 1.603 |
| 15 | + * @link http://www.mediawiki.org/wiki/Extension:AJAX_Poll Documentation |
| 16 | + */ |
| 17 | + |
| 18 | +class AJAXPoll { |
| 19 | + |
| 20 | + /** |
| 21 | + * Register <poll> tag with the parser |
| 22 | + * |
| 23 | + * @param $parser Object: instance of Parser (not necessarily $wgParser) |
| 24 | + * @return Boolean: true |
| 25 | + */ |
| 26 | + static function AJAXPollParserInit( $parser ) { |
| 27 | + $parser->setHook( 'poll', array( __CLASS__, 'AJAXPollRender' ) ); |
| 28 | + return true; |
| 29 | + } |
| 30 | + |
| 31 | + # The callback function for converting the input text to HTML output |
| 32 | + static function AJAXPollRender( $input ) { |
| 33 | + global $wgParser, $wgUser, $wgOut, $wgTitle, $wgScriptPath; |
| 34 | + |
| 35 | + $wgParser->disableCache(); |
| 36 | + |
| 37 | + if ( $wgUser->getName() == '' ) { |
| 38 | + $user = wfGetIP(); |
| 39 | + } else { |
| 40 | + $user = $wgUser->getName(); |
| 41 | + } |
| 42 | + |
| 43 | + // ID of the poll |
| 44 | + $ID = strtoupper( md5( $input ) ); |
| 45 | + |
| 46 | + $par = new Parser(); |
| 47 | + $input = $par->parse( $input, $wgTitle, $wgOut->parserOptions() ); |
| 48 | + $input = trim( strip_tags( $input->getText() ) ); |
| 49 | + $lines = explode( "\n", trim( $input ) ); |
| 50 | + |
| 51 | + // Deprecating AJAX |
| 52 | + /*if ( isset( $_POST['ajaxpoll-post-id'] ) && isset( $_POST['ajaxpoll-post-answer'] ) && $_POST['ajaxpoll-post-id'] == $ID ) { |
| 53 | + AJAXPoll::submitVote( $_POST['ajaxpoll-post-id'], intval( $_POST['ajaxpoll-post-answer'] ) ); |
| 54 | + }*/ |
| 55 | + |
| 56 | + $dbw = wfGetDB( DB_MASTER ); |
| 57 | + $dbw->begin(); |
| 58 | + /** |
| 59 | + * Register poll in the database |
| 60 | + */ |
| 61 | + $row = $dbw->selectRow( |
| 62 | + array( 'poll_info' ), |
| 63 | + array( 'COUNT(poll_id) AS count' ), |
| 64 | + array( 'poll_id' => $ID ), |
| 65 | + __METHOD__ |
| 66 | + ); |
| 67 | + |
| 68 | + if( empty( $row->count ) ) { |
| 69 | + $dbw->insert( |
| 70 | + 'poll_info', |
| 71 | + array( |
| 72 | + 'poll_id' => $ID, |
| 73 | + 'poll_txt' => $input, |
| 74 | + 'poll_date' => wfTimestampNow(), |
| 75 | + 'poll_title' => $wgParser->mTitle->getText() |
| 76 | + ), |
| 77 | + __METHOD__ |
| 78 | + ); |
| 79 | + } |
| 80 | + $dbw->commit(); |
| 81 | + |
| 82 | + // Add CSS |
| 83 | + $wgOut->addExtensionStyle( $wgScriptPath . '/extensions/AJAXPoll/AJAXPoll.css' ); |
| 84 | + switch( $lines[0] ) { |
| 85 | + case 'STATS': |
| 86 | + $retVal = AJAXPoll::buildStats( $ID, $user ); |
| 87 | + break; |
| 88 | + default: |
| 89 | + $retVal = ' |
| 90 | +<div id="ajaxpoll-container-' . $ID . '">' . AJAXPoll::buildHTML( $ID, $user, $lines ) . '</div>'; |
| 91 | + break; |
| 92 | + } |
| 93 | + return $retVal; |
| 94 | + } |
| 95 | + |
| 96 | + private static function buildStats( $ID, $user ) { |
| 97 | + |
| 98 | + $dbw = wfGetDB( DB_MASTER ); |
| 99 | + |
| 100 | + $res = $dbw->select( |
| 101 | + 'poll_vote', |
| 102 | + array( |
| 103 | + 'COUNT(*)', |
| 104 | + 'COUNT(DISTINCT poll_id)', |
| 105 | + 'COUNT(DISTINCT poll_user)', |
| 106 | + 'TIMEDIFF(NOW(), MAX(poll_date))' |
| 107 | + ), |
| 108 | + array(), |
| 109 | + __METHOD__ |
| 110 | + ); |
| 111 | + $tab = $dbw->fetchRow( $res ); |
| 112 | + |
| 113 | + $clock = explode( ':', $tab[3] ); |
| 114 | + |
| 115 | + if ( $clock[0] == '00' && $clock[1] == '00' ) { |
| 116 | + $x = $clock[2]; |
| 117 | + $y = 'second'; |
| 118 | + } elseif( $clock[0] == '00' ) { |
| 119 | + $x = $clock[1]; |
| 120 | + $y = 'minute'; |
| 121 | + } else { |
| 122 | + if ( $clock[0] < 24 ) { |
| 123 | + $x = $clock[0]; |
| 124 | + $y = 'hour'; |
| 125 | + } else { |
| 126 | + $x = floor( $hr / 24 ); |
| 127 | + $y = 'day'; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + $clockago = $x . ' ' . $y . ( $x > 1 ? 's' : '' ); |
| 132 | + |
| 133 | + $res = $dbw->select( |
| 134 | + 'poll_vote', |
| 135 | + 'COUNT(*)', |
| 136 | + array( 'DATE_SUB(CURDATE(), INTERVAL 2 DAY) <= poll_date' ), |
| 137 | + __METHOD__ |
| 138 | + ); |
| 139 | + $tab2 = $dbw->fetchRow( $res ); |
| 140 | + |
| 141 | + return "There are $tab[1] polls and $tab[0] votes given by $tab[2] different people.<br /> |
| 142 | + The last vote has been given $clockago ago.<br/> |
| 143 | + During the last 48 hours, $tab2[0] votes have been given."; |
| 144 | + } |
| 145 | + |
| 146 | + public static function submitVote( $ID, $answer ) { |
| 147 | + global $wgUser,$wgOut; |
| 148 | + |
| 149 | + $dbw = wfGetDB( DB_MASTER ); |
| 150 | + |
| 151 | + if ( $wgUser->getName() == '' ) { |
| 152 | + $user = wfGetIP(); |
| 153 | + } else { |
| 154 | + $user = $wgUser->getName(); |
| 155 | + } |
| 156 | + |
| 157 | + if ( $wgUser->isAllowed( 'bot' ) ) { |
| 158 | + return AJAXPoll::buildHTML( $ID, $user ); |
| 159 | + } |
| 160 | + |
| 161 | + $answer = ++$answer; |
| 162 | + |
| 163 | + $q = $dbw->select( |
| 164 | + 'poll_vote', |
| 165 | + 'COUNT(*) AS c', |
| 166 | + array( |
| 167 | + 'poll_id' => $ID, |
| 168 | + 'poll_user' => $user |
| 169 | + ), |
| 170 | + __METHOD__ |
| 171 | + ); |
| 172 | + $row = $dbw->fetchRow( $q ); |
| 173 | + |
| 174 | + if ( $row['c'] > 0 ) { |
| 175 | + |
| 176 | + $updateQuery = $dbw->update( |
| 177 | + 'poll_vote', |
| 178 | + array( |
| 179 | + 'poll_answer' => $answer, |
| 180 | + 'poll_date' => wfTimestampNow() |
| 181 | + ), |
| 182 | + array( |
| 183 | + 'poll_id' => $ID, |
| 184 | + 'poll_user' => $user |
| 185 | + ), |
| 186 | + __METHOD__ |
| 187 | + ); |
| 188 | + $dbw->commit(); |
| 189 | + $pollContainerText = ( $updateQuery ) ? 'ajaxpoll-vote-update' : 'ajaxpoll-vote-error'; |
| 190 | + |
| 191 | + } else { |
| 192 | + |
| 193 | + $insertQuery = $dbw->insert( |
| 194 | + 'poll_vote', |
| 195 | + array( |
| 196 | + 'poll_id' => $ID, |
| 197 | + 'poll_user' => $user, |
| 198 | + 'poll_ip' => wfGetIP(), |
| 199 | + 'poll_answer' => $answer, |
| 200 | + 'poll_date' => wfTimestampNow() |
| 201 | + ), |
| 202 | + __METHOD__ |
| 203 | + ); |
| 204 | + $dbw->commit(); |
| 205 | + $pollContainerText = ( $insertQuery ) ? 'ajaxpoll-vote-add' : 'ajaxpoll-vote-error'; |
| 206 | + |
| 207 | + } |
| 208 | + |
| 209 | + return AJAXPoll::buildHTML( $ID, $user, '', $pollContainerText ); |
| 210 | + |
| 211 | + } |
| 212 | + |
| 213 | + private static function buildHTML( $ID, $user, $lines = '', $extra_from_ajax = '' ) { |
| 214 | + global $wgTitle, $wgLang, $wgUseAjax; |
| 215 | + |
| 216 | + $dbw = wfGetDB( DB_SLAVE ); |
| 217 | + |
| 218 | + $q = $dbw->select( |
| 219 | + 'poll_info', |
| 220 | + array( 'poll_txt', 'poll_date' ), |
| 221 | + array( 'poll_id' => $ID ), |
| 222 | + __METHOD__ |
| 223 | + ); |
| 224 | + $row = $dbw->fetchRow( $q ); |
| 225 | + |
| 226 | + if ( empty( $lines ) ) { |
| 227 | + $lines = explode( "\n", trim( $row['poll_txt'] ) ); |
| 228 | + } |
| 229 | + |
| 230 | + $start_date = $row['poll_date']; |
| 231 | + |
| 232 | + $q = $dbw->select( |
| 233 | + 'poll_vote', |
| 234 | + array( 'poll_answer', 'COUNT(*)' ), |
| 235 | + array( 'poll_id' => $ID ), |
| 236 | + __METHOD__, |
| 237 | + array( 'GROUP BY' => 'poll_answer' ) |
| 238 | + ); |
| 239 | + |
| 240 | + $poll_result = array(); |
| 241 | + |
| 242 | + while ( $row = $q->fetchRow() ) { |
| 243 | + $poll_result[$row[0]] = $row[1]; |
| 244 | + } |
| 245 | + |
| 246 | + $amountOfVotes = array_sum( $poll_result ); |
| 247 | + |
| 248 | + // Did we vote? |
| 249 | + $q = $dbw->select( |
| 250 | + 'poll_vote', |
| 251 | + array( 'poll_answer', 'poll_date' ), |
| 252 | + array( |
| 253 | + 'poll_id' => $ID, |
| 254 | + 'poll_user' => $user |
| 255 | + ), |
| 256 | + __METHOD__ |
| 257 | + ); |
| 258 | + |
| 259 | + if ( $row = $dbw->fetchRow( $q ) ) { |
| 260 | + $ourLastVoteDate = wfMsg( |
| 261 | + 'ajaxpoll-your-vote', |
| 262 | + $lines[$row[0] - 1], |
| 263 | + $wgLang->timeanddate( wfTimestamp( TS_MW, $row[1] ), true /* adjust? */ ) |
| 264 | + ); |
| 265 | + } |
| 266 | + |
| 267 | + if ( is_object( $wgTitle ) ) { |
| 268 | + if( !empty( $extra_from_ajax ) ) { |
| 269 | + $attributes = ' style="display: block;"'; |
| 270 | + $ajaxMessage = wfMsg( $extra_from_ajax ); |
| 271 | + } else { |
| 272 | + $attributes = ''; |
| 273 | + $ajaxMessage = ''; |
| 274 | + } |
| 275 | + // HTML output has to be on one line thanks to a MediaWiki bug |
| 276 | + // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=1319 |
| 277 | + $ret = '<div id="ajaxpoll-id-' . $ID . '" class="ajaxpoll"> |
| 278 | +<div id="ajaxpoll-ajax-' . $ID . '" class="ajaxpoll-ajax"' . $attributes . '>' . $ajaxMessage . '</div> |
| 279 | +<script>var tmp; |
| 280 | +function mover(x){ |
| 281 | + var sp=$(x).find("span"); |
| 282 | + tmp=sp.html(); |
| 283 | + sp.text(sp.attr("title")); |
| 284 | + sp.attr("title",""); |
| 285 | +} |
| 286 | +function mout(x){ |
| 287 | + var sp=$(x).find("span"); |
| 288 | + sp.attr("title",sp.text()); |
| 289 | + sp.text(tmp); |
| 290 | +} |
| 291 | +</script> |
| 292 | +<div class="ajaxpoll-question">' . strip_tags( $lines[0] ) . '</div>'; |
| 293 | + |
| 294 | + // Different message depending on if the user has already voted or not. |
| 295 | + $message = ( isset( $row[0] ) ) ? $ourLastVoteDate : wfMsg( 'ajaxpoll-no-vote' ); |
| 296 | + $ret .= '<div class="ajaxpoll-misc">' . $message . ' |
| 297 | +</div>'; |
| 298 | + |
| 299 | + $ret .= '<form method="post" action="' . $wgTitle->getLocalURL() . |
| 300 | + '" id="ajaxpoll-answer-id-' . $ID . '"><input type="hidden" name="ajaxpoll-post-id" value="' . $ID . '" />'; |
| 301 | + |
| 302 | + for ( $i = 1; $i < count( $lines ); $i++ ) { |
| 303 | + $ans_no = $i - 1; |
| 304 | + |
| 305 | + if ( $amountOfVotes == 0 ) { |
| 306 | + $percent = 0; |
| 307 | + } else { |
| 308 | + $percent = $wgLang->formatNum( round( ( isset( $poll_result[$i + 1] ) ? $poll_result[$i + 1] : 0 ) * 100 / $amountOfVotes, 2 ) ); |
| 309 | + } |
| 310 | + |
| 311 | + $our = ( isset( $row[0] ) && ( $row[0] - 1 == $i ) ); |
| 312 | + |
| 313 | + // If AJAX is enabled, as it is by default in modern MWs, we can |
| 314 | + // just use sajax library function here for that AJAX-y feel. |
| 315 | + // If not, we'll have to submit the form old-school way... |
| 316 | + if ( $wgUseAjax ) { |
| 317 | + $submitJS = "sajax_do_call(\"AJAXPoll::submitVote\",[\"" . $ID . "\",\"" . $i . "\"], $(\"#ajaxpoll-container-" . $ID . "\")[0]);"; |
| 318 | + } else { |
| 319 | + $submitJS = "$(\"#ajaxpoll-answer-id-" . $ID . "\").submit();"; |
| 320 | + } |
| 321 | + |
| 322 | + // HTML output has to be on one line thanks to a MediaWiki bug |
| 323 | + // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=1319 |
| 324 | + $ret .= " |
| 325 | +<div id='ajaxpoll-answer-" . $ans_no . "' class='ajaxpoll-answer'><div class='ajaxpoll-answer-name'><label for='ajaxpoll-answer-radio-" . $ans_no . "' onclick='$(\"#ajaxpoll-ajax-" . $ID . "\").html(\"" . wfMsg( 'ajaxpoll-submitting' ) . "\");$(\"#ajaxpoll-ajax-" . $ID . "\").css(\"display\",\"block\");$(this).addClass(\"ajaxpoll-checkevent\").prop(\"checked\",true); " . $submitJS . "'><input type='radio' id='ajaxpoll-post-answer-" . $ans_no . "' name='ajaxpoll-post-answer' value='" . $i . "'" . ( $our ? 'checked=true ' : '' ) . "/>" . strip_tags( $lines[$i] ) . |
| 326 | +"</label></div><div class='ajaxpoll-answer-vote" . ( $our ? ' ajaxpoll-our-vote' : '' ) ."' onmouseover='mover(this)' onmouseout='mout(this);'><span title='" . wfMsg( 'ajaxpoll-percent-votes', sprintf( $percent ) ) . "'>" . ( ( isset( $poll_result ) && !empty( $poll_result[$i + 1] ) ) ? $poll_result[$i + 1] : 0 ) . "</span><div style='width: " . $percent . "%;" . ( $percent == 0 ? ' border:0;' : '' ) . "'></div></div> |
| 327 | +</div> |
| 328 | +"; |
| 329 | + } |
| 330 | + |
| 331 | + $ret .= '</form>'; |
| 332 | + |
| 333 | + // Display information about the poll (creation date, amount of votes) |
| 334 | + $pollSummary = wfMsgExt( |
| 335 | + 'ajaxpoll-info', |
| 336 | + 'parsemag', // parse PLURAL |
| 337 | + $amountOfVotes, // amount of votes |
| 338 | + $wgLang->timeanddate( wfTimestamp( TS_MW, $start_date ), true /* adjust? */ ) |
| 339 | + ); |
| 340 | + |
| 341 | + $ret .= '<div id="ajaxpoll-info-' . $ID . '" class="ajaxpoll-info">' . $pollSummary . '</div>'; |
| 342 | + |
| 343 | + $ret .= '</div>'; |
| 344 | + } else { |
| 345 | + $ret = ''; |
| 346 | + } |
| 347 | + |
| 348 | + return $ret; |
| 349 | + } |
| 350 | + |
| 351 | +} /* class AJAXPoll */ |
Property changes on: trunk/extensions/AJAXPoll/AJAXPoll_body.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 352 | + native |