Improved API and Features 0.9.6.1b
This concerns developers looking to integrate KarmaBank.
I've added convenience methods in order to extend KarmaBank, by adding new Meta to check (which now includes the UserMeta key/value table, as well as the User table), even new mapping operations which defines the algorithm, for checking the operation.
One for the key changes it to allow relative operations, where as before the operation would be done pure on the absolute meta value, this is not always desirable
To extend you can hook the KarmaBankMetaMap
event.
You have the AddMeta
method
You supply with
$Name
: column or key for the meta$Explanation
: which is short description.
If the default operations like Equals, Every, etc are not enough you can create your own, using AddOperation
method. Params:
$Name
: descriptive label$Explanation
: explanation$Function
: you need to supply a reference to the function, of your algorithm.
Recommended is to make a static method in you plugin and refer to it like so'YourPluginClass::YourOperation'
. If you need to reference a method of an object instance you could supplyarray($YourObject,'YourOperation')
. The function can be supplied with 4 params:$MetaValue
: this is the current meta value detected$Target
: this is the target value of the rule condition$Condition
: this is the same as the Name of the meta$User
: standard User object, with additional UserMeta table value added using the$User->Meta
property.$LastTrans
: a new object, which enables relative operations, a critical improvement. One important property is$LastTrans->TallyValue
which you can compare to the current value. This was the last value since the last transaction using that rule.
Here is an example extend code:
public static function MyAcmeOperation($MetaValue,$Target,$Condition,$User,$LastTrans){ if([some check]) return TRUE; else return FALSE; } public function KarmaBank_KarmaBankMetaMap_Handler($Sender){ $Sender->AddMeta('MyAcmeMeta','My Acme Meta Explanation'); $Sender->AddOpperation('MyAcmeOperation','My Acme Operation Explanation','YourPluginClass::MyAcmeOperation'); }
You don't aways need a new operations, the existing one might be sufficient, but you will have to add the meta you want checked
When you create your plugin that you want to extend for use with Karmabank, you need a user meta value. This either means a column in the user table, or by setting key/value pair UserMeta, which is generally recommended method as it is convenient and doesn't pollute the User Table
You can do so like this
UserMetaModel::SetUserMeta($UserID,'MyMetaPrefix.MetaName',$MetaValue);
or
$MyMeta=array('MetaName'=>$MetaValue); UserModel::SetMeta($UserID,$MyMeta,'MyMetaPrefix.');
register your meta
public function KarmaBank_KarmaBankMetaMap_Handler($Sender){ $Sender->AddMeta('MyMetaPrefix.MetaName','My Meta Explanation'); }
Note it is definitely a good idea to use a prefix to group related meta.
Now I want to go over relative operation and a new feature which is the 'factor'.
Here is a new relative style operation (that is include default).
public static function OperationDiffEquals($MetaValue,$Target,$Condition,$User,$LastTrans){ $Difference=$MetaValue-($LastTrans ? $LastTrans->LastTally : 0); if(abs($Difference)%$Target!==0) return FALSE; return abs($Difference)!= $Difference? -1:1; }
note how $LastTrans->LastTally
and the current $MetaValue
are subtracted to create a relative difference, this is just one of the way you could make use of it. It checks if the relative target has been hit. The reason for using modulus rather than just equal is the simplification of the logic, for the first first traction were there is no tally, and the meta value is absolute. But essentially you are working out if the change is equal to the target value. However this is the abs difference, but when you return you want to return plus or minus one. This is the factor you return. If you return a signed integer it will be multiplied by the set amount. This mean that not only can karma be rewarded, but it can be deducted under the same rule.
So that method gives you an idea of how develop for KarmaBank.
As always if you need a feature or facility please, sponsor it. Great thing are coming out of this plugin, due to its backers. More to come soon.
grep is your friend.
Comments
Awesome update and documentation. Can't wait to use it.
Vanilla Wiki, Tastes Great! 31,000 viewers can't be wrong. || Plugin Development Explained
I have made a correction, so you can receive $User->Meta as a key value array v0.9.6.1
grep is your friend.
found an error in public function AddMeta.
should be
Vanilla Wiki, Tastes Great! 31,000 viewers can't be wrong. || Plugin Development Explained
Explanation
There was an error rendering this rich post.
just a typo, the function AddMeta has two passed parameters $Name and $Explaination...
so $ColumnName is nothing.
Vanilla Wiki, Tastes Great! 31,000 viewers can't be wrong. || Plugin Development Explained
thank can you monkey patch for now. It will be fixed in the next release, which is being sponsored.
grep is your friend.
it's all patched up on my install... but diffequals is behaving strangely.... i need to rally a few folks to run through some tests, but it seems to misbehaving.
I have a rule:
Meta ILiked DiffEquals 1 Make transaction of 2.00 Karma
but when i like someones post, i get -2 karma... im i bit flummoxed at the moment.
If anyone has the time to test out on their forum, i'd love to know if you are getting the same results.
Vanilla Wiki, Tastes Great! 31,000 viewers can't be wrong. || Plugin Development Explained
@422
i just realized you were pointing out the spelling error in the code... yeah, i didn't fix that, since it was consistent. I thought you were looking for an explanation of the error i was posting about
Vanilla Wiki, Tastes Great! 31,000 viewers can't be wrong. || Plugin Development Explained
im also noticing some other odd behavior... at least one user does not get karma for their posts being liked... i don't understand how the triggering works well enough to debug.
Vanilla Wiki, Tastes Great! 31,000 viewers can't be wrong. || Plugin Development Explained
I think this is to do with the caching/transaction lock, I forced to expire after a transaction and it seems to be more responsive.
grep is your friend.
Fixes imminent.
grep is your friend.
@hbf
there is a new version fingers crossed. Can you try it?
first can you clear
/cache/karmacache
then try updating.grep is your friend.
new version uploaded. will be testing tonight
Vanilla Wiki, Tastes Great! 31,000 viewers can't be wrong. || Plugin Development Explained
i just did some testing and i still have at least one user who does not get karma for my liking their posts.. i verified that a different user does receive a karma balance transaction when i like one of their posts.
Vanilla Wiki, Tastes Great! 31,000 viewers can't be wrong. || Plugin Development Explained
if you clear
/cache/karmacache
and trigger a rule. any folder in there should be empty. Otherwise the permissions are wrong.grep is your friend.
i removed the folder completely.. it recreated itself when i enabled the new version of the plugin. content is being created in there. the folders have files with names that look like GUIDs (without hyphens)... should it be clearing that out? I'll check permission and give r/w/m to everybody if not already set.
Vanilla Wiki, Tastes Great! 31,000 viewers can't be wrong. || Plugin Development Explained
Inside the folders there should be no files.
grep is your friend.