PDA

View Full Version : Rebuild User Reputation


Captain Tycoon
10-15-2012, 03:02 PM
I have a query that adds to users reputation automatically via mysql in the 'reputation' table (it does not modify the users table). The problem I'm having is the value on the forums doesn't update unless I manually run the "Rebuild User Reputation" in General Update Tools (admincp).

I had this same problem with user ranks not updating if I make changes directly to the sql records, what fixed it was the following:
$userdm =& datamanager_init('User', $vbulletin, ERRTYPE_SILENT);
$userdm->set_existing($user);
$userdm->set('posts', $user['posts']); // This will activate the rank update
$userdm->save();

My question now is, how can I have the 'Rebuild User Reputation' run automatically via php so I can add it to my script that I have (prefer to have it only run just for a specified userid).

Any help would be appreciated.

kh99
10-16-2012, 01:32 PM
I'm doing this from the code in the admincp that rebuilds user reputations and I haven't tested it, but:

$userid = X; // set to userid you want to recalculate
$user = $db->query_read_first("
SELECT SUM(reputation.reputation) AS totalrep
FROM " . TABLE_PREFIX . "reputation AS reputation
WHERE reputation.userid = $userid
GROUP BY reputation.userid
");

$reputation = $vbulletin->options['reputationdefault'];
if (!empty($user))
{
$reputation += $user['totalrep'];
}

$db->query_write("
UPDATE " . TABLE_PREFIX . "user
SET reputation = $reputation
");


require_once(DIR . '/includes/adminfunctions_reputation.php');
$count = 1;
$reputations = $db->query_read("
SELECT reputationlevelid, minimumreputation
FROM " . TABLE_PREFIX . "reputationlevel
ORDER BY minimumreputation
");
while ($reputation = $db->fetch_array($reputations))
{
$ourreputation[$count]['value'] = $reputation['minimumreputation'];
$ourreputation[$count]['index'] = $reputation['reputationlevelid'];
$count++;
}
if ($count > 1)
{
$sql = fetch_event_recurrence_sql($ourreputation);
$vbulletin->db->query_write("
UPDATE " . TABLE_PREFIX . "user
SET reputationlevelid = $sql
WHERE userid = $userid
");

}
else
{
// it seems we have deleted all of our reputation levels??
$vbulletin->db->query_write("
UPDATE " . TABLE_PREFIX . "user
SET reputationlevelid = 0
WHERE userid = $userid
");
}

Captain Tycoon
10-17-2012, 01:32 PM
Thank you so much!!!

I changed $user = $db->query_read_first to $user = $db->query_first since that function doesn't exist (i think you meant for it to be that)

Although is query_first or query_read recommended in this case where the reputation table will have multiple records per user?

kh99
10-17-2012, 02:07 PM
You're right - I can never remember exactly what the function names are. I might have been thinking of query_first_slave(), but if you don't have a master/slave db setup it won't matter.

Anyway, I think query_first will work because we added "WHERE reputation.userid = $userid", so along with the SUM and GROUP BY, you should only get one row back, the total reputation for that userid. You could check by executing the query directly (I would have checked but I don't have any test data for reputations).