vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Rebuild User Reputation (https://vborg.vbsupport.ru/showthread.php?t=289094)

Captain Tycoon 10-15-2012 03:02 PM

Rebuild User Reputation
 
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:
Code:

                                $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:

Code:

        $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).


All times are GMT. The time now is 07:06 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01254 seconds
  • Memory Usage 1,724KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (4)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete