Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 12-15-2015, 08:36 PM
Chris.08i Chris.08i is offline
 
Join Date: Oct 2008
Posts: 30
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Infraction Hook

Hello,

It's me again - I was wondering if anyone could tell me which hook to latch my plugin onto if I wanted to modify a column within the users table.

I have tried the hooks 'infractiondata_postsave, infractiondata_presave, and memberinfraction_complete".

Either I have the wrong hook, or something in my code is wrong (which is probably the case)

PHP Code:
<?php
global $vbulletin$prepared;
$usersid $prepared['userid'];

$ipoints $vbulletin->db->query_first("
    SELECT COUNT(*)
    FROM " 
TABLE_PREFIX "user.ipoints
    WHERE userid = " 
$usersid 
"
);

$imodpp 150;
    
    if (
$ipoints >= 1) {
        
$imodpp $imodpp * ($ipoints 2.5);
    }
    
    if (
$ipoints 0) {
            
$vbulletin->db->query_write("
            UPDATE " 
TABLE_PREFIX "user
            SET reputation = reputation - " 
$imodpp "
            WHERE userid = " 
$usersid)
        );    
    }



?>
Reply With Quote
Благодарность от:
MarkFL
  #2  
Old 12-15-2015, 10:26 PM
MarkFL's Avatar
MarkFL MarkFL is offline
 
Join Date: Feb 2014
Location: St. Augustine, FL
Posts: 3,853
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Okay, the first thing is, in a plugin you don't want to include the PHP tags. If your code is in an external file called by a plugin, then you do need the tags.

The hook location "infractiondata_postsave" is a good one (assuming you want this code to execute after the infraction is given), and you'll need to make the $userinfo array global to access it. This array will contain information regarding the user receiving the infraction.

You first query looks like it needs some tweaking, but please let me know exactly what you want to accomplish with this code, and I will try to point you in the right direction.
Reply With Quote
Благодарность от:
Chris.08i
  #3  
Old 12-15-2015, 10:58 PM
Chris.08i Chris.08i is offline
 
Join Date: Oct 2008
Posts: 30
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by MarkFL View Post
Okay, the first thing is, in a plugin you don't want to include the PHP tags. If your code is in an external file called by a plugin, then you do need the tags.

The hook location "infractiondata_postsave" is a good one (assuming you want this code to execute after the infraction is given), and you'll need to make the $userinfo array global to access it. This array will contain information regarding the user receiving the infraction.

You first query looks like it needs some tweaking, but please let me know exactly what you want to accomplish with this code, and I will try to point you in the right direction.
I want to deduct reputation points from the user that is receiving the infraction.

The base amount is 150 points, but each active infraction point (ipoints in users table as far as I know) will apply a 2.5x multiplier. For example, someone with 4 infraction points will receive 1500 points deduction from their reputation column.

I hope that makes sense.
Reply With Quote
  #4  
Old 12-16-2015, 01:41 AM
MarkFL's Avatar
MarkFL MarkFL is offline
 
Join Date: Feb 2014
Location: St. Augustine, FL
Posts: 3,853
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Chris.08i View Post
I want to deduct reputation points from the user that is receiving the infraction.

The base amount is 150 points, but each active infraction point (ipoints in users table as far as I know) will apply a 2.5x multiplier. For example, someone with 4 infraction points will receive 1500 points deduction from their reputation column.

I hope that makes sense.
Yes, makes perfect sense. What we can do is get this done with just one query. While this code (hopefully) won't be called often, it's a good idea to get in the practice of minimizing the number of queries for any operation.

PHP Code:
global $userinfo;

$vbulletin->db->query_write("
UPDATE " 
TABLE_PREFIX "user
SET reputation = (reputation - (375*ipoints))
WHERE userid = " 
$userinfo['userid']
); 
Reply With Quote
  #5  
Old 12-16-2015, 05:13 AM
Chris.08i Chris.08i is offline
 
Join Date: Oct 2008
Posts: 30
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by MarkFL View Post
Yes, makes perfect sense. What we can do is get this done with just one query. While this code (hopefully) won't be called often, it's a good idea to get in the practice of minimizing the number of queries for any operation.

PHP Code:
global $userinfo;

$vbulletin->db->query_write("
UPDATE " 
TABLE_PREFIX "user
SET reputation = (reputation - (375*ipoints))
WHERE userid = " 
$userinfo['userid']
); 

What if ipoints = 0?

I still want to deduct 150 from their reputation.
Reply With Quote
  #6  
Old 12-16-2015, 05:49 AM
MarkFL's Avatar
MarkFL MarkFL is offline
 
Join Date: Feb 2014
Location: St. Augustine, FL
Posts: 3,853
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Chris.08i View Post
What if ipoints = 0?

I still want to deduct 150 from their reputation.
Your original code indicated that a deduction only occurs when ipoints > 0. But, if you want 150 rep deducted when ipoints = 0, and otherwise 375 * ipoints rep deducted then our code would be (since we don't have a nice linear relationship):

PHP Code:
global $userinfo;

$vbulletin->db->query_write("
    UPDATE " 
TABLE_PREFIX "user
    SET reputation = CASE ipoints
        WHEN 0 THEN (reputation - 150)
        ELSE (reputation - (375*ipoints))
    WHERE userid = " 
$userinfo['userid']
); 
Reply With Quote
  #7  
Old 12-16-2015, 08:38 AM
Chris.08i Chris.08i is offline
 
Join Date: Oct 2008
Posts: 30
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by MarkFL View Post
Your original code indicated that a deduction only occurs when ipoints > 0. But, if you want 150 rep deducted when ipoints = 0, and otherwise 375 * ipoints rep deducted then our code would be (since we don't have a nice linear relationship):

PHP Code:
global $userinfo;

$vbulletin->db->query_write("
    UPDATE " 
TABLE_PREFIX "user
    SET reputation = CASE ipoints
        WHEN 0 THEN (reputation - 150)
        ELSE (reputation - (375*ipoints))
    WHERE userid = " 
$userinfo['userid']
); 
Well that sums it up nicely!

I was going to use if statements to create the effect I needed, turns out I don't need to!

Thanks so much for your help Mark, definitely learned a lot these couple days.

Edit: I just tried it - I'm getting a Server 500 error when I try infract someone. Any ideas?

Edit #2: So I globalised $vbulletin as well and it started running the code properly. However it isn't properly deducting from the table.

Edit #3: I got it to deduct!

It turns out we were missing an 'END' to the case which cause an MySQL syntax error.

However it now seems like it's going through both cases and deducting a total of 525 every time, no matter what! Not sure why the case rules aren't working!

Edit #4:
Okay - I got fed up so I ended up going with this. However, this code for some reason deducts double what I calculate it should do. For example, those with 0 ipoints, gets deducted 300 instead of 150, and those with 1 ipoint get deducted 750 instead of 375.

PHP Code:
global $userinfo$vbulletin;

if (
$userinfo['ipoints'] == 0) {
$base 150;
$vbulletin->db->query_write("
    UPDATE " 
TABLE_PREFIX "user
    SET reputation = reputation - " 
$base "
    WHERE userid = " 
$userinfo['userid']
);
}

if (
$userinfo['ipoints'] > 0) {
$imodpp 150 * ($userinfo['ipoints'] * 2.5);
$vbulletin->db->query_write("
    UPDATE " 
TABLE_PREFIX "user
    SET reputation = reputation - " 
$imodpp "
    WHERE userid = " 
$userinfo['userid']
);

I know this isn't the most efficient way - but it's the most working I could get it to so far!
Reply With Quote
  #8  
Old 12-17-2015, 03:05 PM
MarkFL's Avatar
MarkFL MarkFL is offline
 
Join Date: Feb 2014
Location: St. Augustine, FL
Posts: 3,853
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

This plugin works correctly on my local dev site:

Product: vBulletin

Hook Location: infractiondata_postsave

Title: Deduct Rep When Infracting

Execution Order: 5

Plugin PHP Code:

PHP Code:
global $vbulletin$db$userinfo;

$points $userinfo['ipoints'];
$rep $userinfo['reputation'];

if (
$points 0)
{
    
$newrep = ($rep $points*375);
}
else
{
    
$newrep = ($rep 150);
}

$vbulletin->db->query_write("
    UPDATE " 
TABLE_PREFIX "user
    SET reputation = " 
$newrep "
    WHERE userid = " 
$userinfo['userid']
); 
Plugin is Active: Yes
Reply With Quote
  #9  
Old 12-19-2015, 10:33 AM
Chris.08i Chris.08i is offline
 
Join Date: Oct 2008
Posts: 30
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I had an adaptation of mine that I have been using for the past couple days that have been working for me.

I have changed over to your code as it's much neater! Thank you again Mark for your time and help!

I was wondering if it was possible to only trigger the above deduction when an actual point is issued, and no deduction for warnings?
Reply With Quote
  #10  
Old 12-19-2015, 10:54 AM
MarkFL's Avatar
MarkFL MarkFL is offline
 
Join Date: Feb 2014
Location: St. Augustine, FL
Posts: 3,853
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Chris.08i View Post
I had an adaptation of mine that I have been using for the past couple days that have been working for me.

I have changed over to your code as it's much neater! Thank you again Mark for your time and help!

I was wondering if it was possible to only trigger the above deduction when an actual point is issued, and no deduction for warnings?
Do you mean then a deduction based only on the points being issued at the time, and not on the cumulative total of active points? If so, I would have to investigate to see how to retrieve that value.

The code I posted does exactly the opposite of that...it uses $userinfo['ipoints'] which does not include the points currently being given, but rather the number of points accumulated by the user before the current infraction.

edit: Try using the hook location "infraction_update_complete", and at that hook, the points for the current infraction is in $vbulletin->GPC['points']. Then you can base the rep deduction on that and update the user table.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:55 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.09763 seconds
  • Memory Usage 2,311KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (7)bbcode_php
  • (6)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (10)post_thanks_box
  • (2)post_thanks_box_bit
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (2)post_thanks_postbit
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete