Status
Not open for further replies.

Darkwaltz4

Former Developer
I built vBCredits II Deluxe to be easy to integrate, not just for myself, but for others. Integration must be done via code, so the first thing you should do is enable vBulletin debug mode and create your own product. This will turn on the full Action and Display editors. You might also want to check out the vBookie actionset for a good example of all of these being used.

Actions

What kinds of things could you earn or spend credits on in your addon? As the label implies, these things should be distinct things that users perform. Decide on a few, and we will start by creating a new action via the Action Manager.

Go ahead and look at how the other Actions are set up to help you put together your own. Be sure to expand the advanced options. Come up with a unique identifier for your Action and remember it for later. Read each attribute carefully and decide how your action behaves. Each of these properties must be supported in your API calls, but these settings determine the corresponding features available during Events.

You can add your own category options by adding a new phrase under your product in the vBCredits Administration Phrases with a variable name like: credits_category_[something]. It will show up in that dropdown then.

When you have saved the action, look in the credits_action table. Your product will need to REPLACE INTO the credits_action table that very row upon installation. The primary key is the actionid that you came up with. After adding all the rows you want in your installer, you will need to rebuild the caches for it to show up immediately:

PHP:
vbcredits_cache();

Next, you need to find within your code or plugin where that Action actually happens, and insert this line of code:

PHP:
VBCREDITS::action($actionid, $userid, $refid = null, $negate = false, $extra = array());
$actionid - string, the actionid of the Action you are triggering.

$userid - int, the userid of the user who is performing the Action. Doesn't always have to be the currently logged in user, but still must be passed if it is.

$refid - mixed, this goes along with the reference URL of the Action, if you set one. If there isn't one and you left that property blank in the action manager, then pass null. Passing boolean true is explained later*

$negate - bool, true if the negation part of the Event should be used. If you set this in the Action properties, then you should have two API calls in the appropriate places; one for the Action regularly happening, and the other for when it is negated, and passed with true here.

$extra - array with the following keys, which should be included as they apply:

'message' - string, any text which will go into the Transaction logs, such as a high score or a note with a donation, etc.

'multiplier' - numeric, the value of the multiplier entity if you set one for your Action. For Size multipliers, pass the user-supplied text and it will automatically be counted according to user settings like ignoring bbcode. For Currency multipliers, pass the properly signed amount.

'timestamp' - unix timestamp int, useful for "catching up" Actions. If not included then TIMENOW will be used.

'currencyid' - unsinged int, generally used for user-selected Currency multiplier Actions, such as when donating or transferring a specific currency for a specific amount.

'forumid' - unsigned int, the forumid that applies to this Action, if the global property was not set.

'userinfo' - array, full user info array for the user triggering the Action, including permissions. Useful for speeding up transactions not for the current user. If not included then $vbulletin->userinfo will be used.

'ownerid' - unsigned int, userid who owns the parent entity of the current entity according to your Action settings. For example, the parent entity of a post is a thread, and a user owns that, but might not own the thread, and events can be built taking that into effect.
*Now, sometimes hooks might be in places that are not entirely ideal, such as when other validation of an Action occurs AFTER the API call, and so users might unfairly earn or spend credits for an Action that ultimately fails. Furthermore, checking if a user has enough credits to spend on an Action if an Event is so configured is an important part of verification itself. Sometimes you won't even know the refid of an Action until after it has been validated and saved to a database, but still a part of the transaction record. Datamanagers are notorious for this, but they generally have a pre and post hook which you can use the following to overcome:

In the pre hook, pass boolean true as the $refid. The API call will then return an ID which you need to hang onto. At this point the Transaction has not yet been committed, but it has been validated and will react as normal if for example a user does not have enough credits. Next in the post hooks, use this:

PHP:
VBCREDITS::apply($apiid, $refid);
So just pass the ID returned from the API call to action() and the refid which you were unable to send there before. This will commit the Transaction and again, splitting into two API calls is not always necessary if you send the refid to begin with.

Note, if you are calling this somewhere that doesn't terminate in print_output() or vBulletin's ajax handler (such as outside of a vb plugin, custom script, etc.), you need to use this afterwards to actually commit the transaction:
PHP:
VBCREDITS::shutdown();
That's it for Events, the core will take care of everything else, including stopping the Action with error messages as it applies. For recalculations, there is a credits_actions_rebuild hook for you to use. Mark your Actions that support this feature, as the ones that don't are still eligible for reprocessing automatically without your help, however they don't work for past Actions. There, you have the following to work with:

$actionid - test for the values you are handling in a switch statement or similar.

$user - the array of the current user being processed.

$vbulletin->GPC['start_date'], $vbulletin->GPC['end_date'] - range of time chosen by the user.
Of course, you have the rest of the environment available. You should run any queries for that Action and user during that range, and issue new API calls as detailed above, filling in the usual information. It is highly recommended that you include the timestamp field according to the time that you determine a recalculated action happened, so that the Transaction log looks like everything happened at the correct time in the correct order. Generally, any Actions that originally required two API calls can be merged into just the first one.

Currency Import Wizard

Want to define a currency that will be detected automatically? Just use the credits_currency_wizard and add an entry to the $currencies array, with your own array that contains the following keys that you should recognize from the credits_currency table:

'title' - string, the default of what th currency is called, usually from a setting or phrase

'productid' - string, the product that creates the currency

'minversion' - string, optional version of the product where the currency first appears

'maxversion' - string, optional version of the product where the currency is no longer available

'table' - string, the table that the currency column is found

'useprefix' - bool as int, if the table should have the vbulletin prefix included

'userid' - bool as int, if the table joins with the userid instead of the username

'usercol' - string, the column on the table which joins on the userid or username

'column' - string, the column which contains the actual currency values for each user
vBCredits II Deluxe will take care of the rest, from import to transfer!

Displays explained soon.

Let me know if you have any questions, comments, or suggestions!
 
Last edited:
Hi, thanks for a great product!
I'm looking to make certain actions from an outside script give credits to users.

I created a new event for the action Adjust. (Basicly I want to give the userid 15 credits). Then I used this code:

PHP:
    $actionid = "adjust";
    $userid = 1;
    $extra = array('message'=>'outsidescript');
    VBCREDITS::action($actionid, $userid, $refid = null, $negate = false, $extra = array());

However nothing happends :eek: Am I setting the $actionid correct? Don't I need to set the eventid somewhere?
 
Because the Adjust action uses currency directly, you need to pass the amount of credits as the multiplier as well as the currencyid of the specific currency being transferred, like this: Also, the userid will be the one receiving the credits - use accordingly.

PHP:
VBCREDITS::action('adjust', 1, null, false, array('message' => 'outsidescript', 'currencyid' => 1, 'multiplier' => 15));
 
Because the Adjust action uses currency directly, you need to pass the amount of credits as the multiplier as well as the currencyid of the specific currency being transferred, like this: Also, the userid will be the one receiving the credits - use accordingly.

PHP:
VBCREDITS::action('adjust', 1, null, false, array('message' => 'outsidescript', 'currencyid' => 1, 'multiplier' => 15));

Thanks for your reply, however still nothing happends for me :confused:
My event looks like this:
Active: Yes
Action: Adjust
Currency: Credits
Usergroups: All (none selected)
Charge for Action: No
Adjust Amount: 15
Currency Amount: 15
Affected Participant: Receiving user
Minimum Currency: (blank)
Maximum Currency: (blank)
Below Minimum Handling: Exclude Currency amounts (default)
Moderate: No

PHP:
    VBCREDITS::action('adjust', 1, null, false, array('message' => 'outsidescript', 'currencyid' => 1, 'multiplier' => 15));  
    echo "hello";
output: hello
so the function is called I guess :eek: Any ideas?
 
Within your event, get rid of the Adjust and Currency amounts - those are ADDED to the multiplier amount once and per-credit respectively (this behavior is for actions that deal directly with the movement of currency, as the Adjust action is).
Also, I was assuming your currencyid is 1 - verify just to be sure.

Is this code on a vbulletin-powered page? Does it end in either print_output() or an XML output for ajax? If not, you need to end your page with
PHP:
VBCREDITS::shutdown();
so that the queued transactions are committed to the database.

Finally, do you have immediate transactions enabled? If not, then the user that got the credits will need to load some other vbulletin-powered pages to process the pending transaction you just gave them. To reduce the number of pages needed to load, consider loading and passing the userinfo key in the VBCREDITS::action() call.
 
I'm working on another script that involves giving points to a userid and take these points from the users own points.. a donate? But not really getting it to work, what donate settings should I use and how should my action call look like? Did try sending fetched userinfo as well in the array but didn't help me but might very well be something else that messed that up.

Could I use adjust with both users being affected? How would I make that actioncall then though?
 
darkwaltz, im trying to add a similar function to what you guys used to have in vbcredits, where on a usergroup change, they would be given x$credits - is this still possible? I'm having a problem getting the api to work it out with events.
 
Darkwaltz4

where do I need to start if I want to add points for users from an external page?

What I have is:

Code:
$curdir = getcwd (); 
chdir('./community/'); 
require_once('community/global.php'); 
chdir ($curdir);

//and then my custom stuff like:

if ($vbulletin->userinfo['userid']!=0) {
   // you are logged in
}

But how would the code look like if I want to give
userid = 100
amount = 20
for action: custom_01


Best regards
Kesandal
 
Have you read through the first couple posts in this thread? Someone asked and I walked them through doing exactly that.
 
Have you read through the first couple posts in this thread? Someone asked and I walked them through doing exactly that.

Sorry buddy.. I was really blind.

Code:
VBCREDITS::action('adjust', 1, null, false, array('message' => 'outsidescript', 'currencyid' => 1, 'multiplier' => 15));
If I use this line; which php-Files i need to include aditionally?
global.php (from vbulletin) and /dbtech/credits/credits_core.php (for the class)?

Thanks
Kesandal
 
You can just include global, but after the VBCREDITS::action call, you need this line after it:

PHP:
VBCREDITS::shutdown();
which commits the changes
 
Darkwaltz4

Thanks for your reply.

Still one question left:


[index.php]
PHP:
$curdir = getcwd (); 
chdir('./community/'); 
require_once('community/global.php'); 
chdir ($curdir);

// now we have a variable called $vbulletin
require_once('core.php');  // containing submit_result

// do some stuff....

submit_result($var1,$var2,$var3); // give the user points

[core.php]
PHP:
function submit_result($var1,$var2,$var3){
        VBCREDITS::action("adjust",$user_id, $refid, $negate, array('message' => $message, 'currencyid' => 1, 'multiplier' => convert_points_to_credits($reward)));
        VBCREDITS::shutdown();
}


The problem:
If I use VBCREDITS::action and VBCREDITS::shutdown directly in my index.php (not in the function defined in core.php) everything works perfekt.

But how can I make the use of VBCREDITS in my function available?

Thanks in advance.

Kesandal
 
Now you are dealing with basic PHP fundamentals, not vBCredits specific stuff. I really can't launch into general php tutorials here.

In your example, the variables $user_id, $refi, $negate, $message, and $reward are undefined in the scope of your function. If you are getting those from the global scope, globalize those variables. I am also assuming that you are defining convert_points_to_credits somewhere. If you intend any of $var1, $var2, $var3 to be some of those, then replace them in the appropriate places.
 
Darkwaltz4
I could solve the problem...

You misunderstood me :)
I've had not a problem with $User_id and so on.

I got the following error:
PHP:
Fatal error: Class 'VBCREDITS' not found in /home/wb_1/public_html/sys/core.php on line 270

Solution:
PHP:
submit_result($var1,$var2,$var3,$vbulletin); // give the user points
and
PHP:
function submit_result($var1,$var2,$var3,$vbulletin){
        VBCREDITS::action("adjust",$user_id, $refid, $negate, array('message' => $message, 'currencyid' => 1, 'multiplier' => convert_points_to_credits($reward)));
        VBCREDITS::shutdown();
}

And now the most importand part (first the points were not added to the user):
[x] Activate the action adjust.

Greetings
Kesandal
 
If VBCREDITS was not defined, then you didnt include global.php correctly. VBCREDITS will only work on a vbulletin-powered page (that is, one that has included vbulletin's global.php)
 
I'm playing with a couple of things atm, but so far, even though I've got an entry in the database, it seems like vBC is ignoring it when I want to create a new event to utilize it. Any obvious thing I could be missing Darkwaltz4 ?
 
Status
Not open for further replies.
Back
Top