Skip to content

Commit 8980ca2

Browse files
committed
Rename check point function to check_max_min
Solve bug with max and min points
1 parent 11bb281 commit 8980ca2

1 file changed

Lines changed: 75 additions & 43 deletions

File tree

root/includes/functions_reputation.php

Lines changed: 75 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ function give_point($to, $post_id = 0, $comment, $notify = false, $point, $mode
291291
$user_data = $db->sql_fetchrow($result);
292292
$db->sql_freeresult($result);
293293

294-
//Nofity user about the point
294+
//Notify user about the point
295295
$new_points = '';
296296
if ($config['rs_notification'])
297297
{
@@ -312,7 +312,10 @@ function give_point($to, $post_id = 0, $comment, $notify = false, $point, $mode
312312
$db->sql_query($sql);
313313

314314
///Check max/min points
315-
$this->check_point($to);
315+
if ($config['rs_max_point'] || $config['rs_min_point'])
316+
{
317+
$this->check_max_min($to);
318+
}
316319

317320
//If config allows and we are told so, we should send a private message to a user, who received the vote
318321
if ($notify && $config['rs_pm_notify'])
@@ -374,44 +377,6 @@ function give_point($to, $post_id = 0, $comment, $notify = false, $point, $mode
374377
return true;
375378
}
376379

377-
/**
378-
* @param int $user_id user ID
379-
*/
380-
private function check_point($user_id)
381-
{
382-
global $config, $db;
383-
384-
if (!$config['rs_max_point'] || !$config['rs_min_point'])
385-
{
386-
return;
387-
}
388-
389-
$sql = 'SELECT SUM(point) AS points
390-
FROM ' . REPUTATIONS_TABLE . "
391-
WHERE action != 5
392-
AND rep_to = $user_id";
393-
$result = $db->sql_query($sql);
394-
$points = $db->sql_fetchrow($result);
395-
$db->sql_freeresult($result);
396-
397-
//Max user reputation
398-
if ($config['rs_max_point'] && ($config['rs_max_point'] < $points['points']))
399-
{
400-
$sql = 'UPDATE ' . USERS_TABLE . "
401-
SET user_reputation = {$config['rs_max_point']}
402-
WHERE user_id = $user_id";
403-
$db->sql_query($sql);
404-
}
405-
//Min user reputation
406-
if ($config['rs_min_point'] && ($config['rs_min_point'] > $points['points']))
407-
{
408-
$sql = 'UPDATE ' . USERS_TABLE . "
409-
SET user_reputation = {$config['rs_min_point']}
410-
WHERE user_id = $user_id";
411-
$db->sql_query($sql);
412-
}
413-
}
414-
415380
/** Function responsible for deleting reputation
416381
* @param int $id reputation ID
417382
* @return bool
@@ -460,8 +425,13 @@ function delete($id)
460425
WHERE user_id = {$row['rep_to']}";
461426
$db->sql_query($sql);
462427

428+
global $config;
429+
463430
//Check max/min points
464-
$this->check_point($row['rep_to']);
431+
if ($config['rs_max_point'] || $config['rs_min_point'])
432+
{
433+
$this->check_max_min($row['rep_to']);
434+
}
465435
}
466436

467437
//Update new status field
@@ -526,8 +496,13 @@ function clear_reputation($mode, $id, $post_ids = array())
526496
WHERE user_id = {$point['rep_to']}";
527497
$db->sql_query($sql);
528498

499+
global $config;
500+
529501
//Check max/min points
530-
$this->check_point($point['rep_to']);
502+
if ($config['rs_max_point'] || $config['rs_min_point'])
503+
{
504+
$this->check_max_min($point['rep_to']);
505+
}
531506
}
532507

533508
$sql = 'SELECT topic_id, forum_id, post_subject
@@ -578,6 +553,63 @@ function clear_reputation($mode, $id, $post_ids = array())
578553
add_log('mod', $log_forum, $log_topic, $log_clear_action, $log_clear_data);
579554
}
580555

556+
/**
557+
* @param int $user_id user ID
558+
*/
559+
private function check_max_min($user_id)
560+
{
561+
global $config, $db;
562+
563+
$sql = 'SELECT SUM(point) AS points
564+
FROM ' . REPUTATIONS_TABLE . "
565+
WHERE action != 5
566+
AND rep_to = $user_id";
567+
$result = $db->sql_query($sql);
568+
$points = $db->sql_fetchfield('points');
569+
$db->sql_freeresult($result);
570+
571+
//Choose mode
572+
$mode = ($points > 0) ? 'max' : 'min';
573+
574+
//Max user reputation
575+
if ($mode == 'max' && $config['rs_max_point'])
576+
{
577+
if ($points > $config['rs_max_point'])
578+
{
579+
$sql = 'UPDATE ' . USERS_TABLE . "
580+
SET user_reputation = {$config['rs_max_point']}
581+
WHERE user_id = $user_id";
582+
$db->sql_query($sql);
583+
}
584+
else
585+
{
586+
$sql = 'UPDATE ' . USERS_TABLE . "
587+
SET user_reputation = $points
588+
WHERE user_id = $user_id";
589+
$db->sql_query($sql);
590+
}
591+
}
592+
593+
//Min user reputation
594+
if ($mode == 'min' && $config['rs_min_point'])
595+
{
596+
if ($points < $config['rs_min_point'])
597+
{
598+
$sql = 'UPDATE ' . USERS_TABLE . "
599+
SET user_reputation = {$config['rs_min_point']}
600+
WHERE user_id = $user_id";
601+
$db->sql_query($sql);
602+
}
603+
else
604+
{
605+
$sql = 'UPDATE ' . USERS_TABLE . "
606+
SET user_reputation = $points
607+
WHERE user_id = $user_id";
608+
$db->sql_query($sql);
609+
}
610+
}
611+
}
612+
581613
/** Obtain reputation ranks
582614
*/
583615
function obtain_rs_ranks()
@@ -740,7 +772,7 @@ function get_rs_new_rank($points, $title = false, $color = false)
740772
return $return_rank;
741773
}
742774

743-
/** Prevent overrating a same user by another user
775+
/** Prevent overrating one user by another user
744776
* @param $user_id user ID
745777
*/
746778
function prevent_rating($user_id)

0 commit comments

Comments
 (0)