Index: trunk/extensions/ArticleFeedbackv5/api/ApiFlagFeedbackArticleFeedbackv5.php |
— | — | @@ -41,6 +41,7 @@ |
42 | 42 | $feedbackId = $params['feedbackid']; |
43 | 43 | $flag = $params['flagtype']; |
44 | 44 | $notes = $params['note']; |
| 45 | + $toggle = $params['toggle']; |
45 | 46 | $direction = isset( $params['direction'] ) ? $params['direction'] : 'increase'; |
46 | 47 | $where = array( 'af_id' => $feedbackId ); |
47 | 48 | |
— | — | @@ -208,7 +209,7 @@ |
209 | 210 | elseif($direction == 'decrease' && $record->af_abuse_count > 0 ) { |
210 | 211 | $activity = 'unrequest'; |
211 | 212 | $filters['needsoversight'] = -1; |
212 | | - $update[] = "af_oversight_count = GREATEST(af_oversight_count - 1, 0)"; |
| 213 | + $update[] = "af_oversight_count = GREATEST(CONVERT(af_oversight_count, SIGNED) - 1, 0)"; |
213 | 214 | |
214 | 215 | // Un-hide if we don't have oversight flags anymore |
215 | 216 | if( $record->af_oversight_count == 1 && true == $record->af_is_hidden ) { |
— | — | @@ -227,14 +228,50 @@ |
228 | 229 | // helpful and unhelpful flagging |
229 | 230 | } elseif( 'unhelpful' === $flag || 'helpful' === $flag) { |
230 | 231 | |
231 | | - if ( 'unhelpful' === $flag ) { |
232 | | - $update[] = "af_unhelpful_count = af_unhelpful_count + 1"; |
233 | | - } elseif ( 'helpful' === $flag ) { |
234 | | - $update[] = "af_helpful_count = af_helpful_count + 1"; |
| 232 | + $results['toggle'] = $toggle; |
| 233 | + $helpful = $record->af_helpful_count; |
| 234 | + $unhelpful = $record->af_unhelpful_count; |
| 235 | + |
| 236 | + // if toggle is on, we are decreasing one and increasing the other atomically |
| 237 | + // means one less http request and the counts don't mess up |
| 238 | + if (true == $toggle) { |
| 239 | + |
| 240 | + if( ( ($flag == 'helpful' && $direction == 'increase' ) |
| 241 | + || ($flag == 'unhelpful' && $direction == 'decrease' ) ) |
| 242 | + ) { |
| 243 | + $update[] = "af_helpful_count = af_helpful_count + 1"; |
| 244 | + $update[] = "af_unhelpful_count = GREATEST(0, CONVERT(af_unhelpful_count, SIGNED) - 1)"; |
| 245 | + $helpful++; |
| 246 | + $unhelpful--; |
| 247 | + |
| 248 | + } elseif ( ( ($flag == 'unhelpful' && $direction == 'increase' ) |
| 249 | + || ($flag == 'helpful' && $direction == 'decrease' ) ) |
| 250 | + ) { |
| 251 | + $update[] = "af_unhelpful_count = af_unhelpful_count + 1"; |
| 252 | + $update[] = "af_helpful_count = GREATEST(0, CONVERT(af_helpful_count, SIGNED) - 1)"; |
| 253 | + $helpful--; |
| 254 | + $unhelpful++; |
| 255 | + } |
| 256 | + |
| 257 | + } else { |
| 258 | + |
| 259 | + if ( 'unhelpful' === $flag && $direction == 'increase') { |
| 260 | + $update[] = "af_unhelpful_count = af_unhelpful_count + 1"; |
| 261 | + $unhelpful++; |
| 262 | + } elseif ( 'unhelpful' === $flag && $direction == 'decrease') { |
| 263 | + $update[] = "af_unhelpful_count = GREATEST(0, CONVERT(af_unhelpful_count, SIGNED) - 1)"; |
| 264 | + $unhelpful--; |
| 265 | + } elseif ( $flag == 'helpful' && $direction == 'increase' ) { |
| 266 | + $update[] = "af_helpful_count = af_helpful_count + 1"; |
| 267 | + $helpful++; |
| 268 | + } elseif ( $flag == 'helpful' && $direction == 'decrease' ) { |
| 269 | + $update[] = "af_helpful_count = GREATEST(0, CONVERT(af_helpful_count, SIGNED) - 1)"; |
| 270 | + $helpful--; |
| 271 | + } |
| 272 | + |
235 | 273 | } |
236 | 274 | |
237 | | - // note that a net helpfulness of 0 is neither helpful nor unhelpful |
238 | | - $netHelpfulness = $record->af_net_helpfulness; |
| 275 | + $netHelpfulness = $helpful - $unhelpful; |
239 | 276 | |
240 | 277 | // increase helpful OR decrease unhelpful |
241 | 278 | if( ( ($flag == 'helpful' && $direction == 'increase' ) |
— | — | @@ -294,18 +331,6 @@ |
295 | 332 | |
296 | 333 | // Update helpful/unhelpful display count after submission. |
297 | 334 | if ( $flag == 'helpful' || $flag == 'unhelpful' ) { |
298 | | - $helpful = $record->af_helpful_count; |
299 | | - $unhelpful = $record->af_unhelpful_count; |
300 | | - |
301 | | - if( $flag == 'helpful' && $direction == 'increase' ) { |
302 | | - $helpful++; |
303 | | - } elseif ( $flag == 'helpful' && $direction == 'decrease' ) { |
304 | | - $helpful--; |
305 | | - } elseif ( $flag == 'unhelpful' && $direction == 'increase' ) { |
306 | | - $unhelpful++; |
307 | | - } elseif ( $flag == 'unhelpful' && $direction == 'decrease' ) { |
308 | | - $unhelpful--; |
309 | | - } |
310 | 335 | |
311 | 336 | // no negative numbers please |
312 | 337 | $helpful = max(0, $helpful); |
— | — | @@ -460,6 +485,11 @@ |
461 | 486 | ApiBase::PARAM_REQUIRED => false, |
462 | 487 | ApiBase::PARAM_ISMULTI => false, |
463 | 488 | ApiBase::PARAM_TYPE => 'string' |
| 489 | + ), |
| 490 | + 'toggle' => array( |
| 491 | + ApiBase::PARAM_REQUIRED => false, |
| 492 | + ApiBase::PARAM_ISMULTI => false, |
| 493 | + ApiBase::PARAM_TYPE => 'boolean' |
464 | 494 | ) |
465 | 495 | ); |
466 | 496 | } |
— | — | @@ -473,7 +503,8 @@ |
474 | 504 | return array( |
475 | 505 | 'feedbackid' => 'FeedbackID to flag', |
476 | 506 | 'type' => 'Type of flag to apply - hide or abuse', |
477 | | - 'note' => 'Information on why the feedback activity occurred' |
| 507 | + 'note' => 'Information on why the feedback activity occurred', |
| 508 | + 'toggle' => 'The flag is being toggled atomically, only useful for (un)helpful' |
478 | 509 | ); |
479 | 510 | } |
480 | 511 | |