View Full Version : Creating Custom Promotions & validating profile fields
vibethemes
11-30-2012, 02:55 AM
I new new to VB and have been trying to setup a support form.
My requirement:
For each purchase I give user a purchase code which she uses to gain access for the support forum for that respoective product only.
What I have dont so far?
->I have created usergroups for each product, where each product forum is accessible to that user group.
-> Added profile fields for users to enter purchase code against each product.
What I need to do and How can I do this?
-> Validate purchase code using my http API on Profile field saving.
-> Add additional usergroup to the user's groups if validation is successful.
The second one I believe could be done by creating some custom promotion to change the usergroup.
Please suggest if you've a better idea to how can I achieve this...
vibethemes
12-06-2012, 08:16 AM
Well, since more people could be visiting this thread. The solution is to create a plugin which changes the usergroup based on the profile field. Since CURL is allowed in the plugin one can validate it via and API call. I have partially succeeded in achieving this so will post the whole code once its complete.
cheers,
VT
vibethemes
12-07-2012, 12:52 PM
have to Admit, very poor documentation to start coding on VB.
To VB guys here's a suggestion: Learn from WordPress on how CODEX should be built to help developers.
I agree, it's difficult to get started. I haven't been around that long compared to some here, but I think the reason is that vbulletin wasn't originally designed to support code modifications. The hook system was added later, but it was just a matter of choosing some locations in the code where plugin code can be executed, and there really is no API so you have to read the code to figure out what to do.
Having said that - if you have specific questions you can ask and someone will probably be able to help.
vibethemes
12-07-2012, 01:16 PM
Thanks KH99, was hoping someone to reply.
To begin with:
I want to know the hook which gets triggered when a user saves her custom profile field.
Lets say I created a profile field from adminCp as "A"
Now I want to run my code when "A" is saved/updated by the user.
OK, well, there are two places it can be changed (the "settings" page and the profile), as well as registration, if you've set it to show up there. But I think you could handle any case by using hook userdata_presave. At that point you can check the value and set an error if it's invalid (or do anything else you want, of course). So basically something like this:
if ($this->setfields["fieldX"]) // field X has been set, new value in $this->userfield["fieldX"]
{
if ($this->userfield["fieldX"] is bad)
{
$this->error('errorphrase');
}
}
Of course you'd replace X with the id of your field, and errorphrase with the varname of an actual phrase (that you'd need to create). Also note that that plugin is executed inside a function, so if you need global variables you have to use "global". If you want to see what's going on, look at includes/class_dm_user.php, around line 1865, and maybe ajax.php, the 'saveuserfield' section that starts around line 814 to see how the datamanager is used to update profile fields.
vibethemes
12-07-2012, 05:10 PM
Thanks a lot,
So I get most of my answers with this hook "userdata_presave" to validate my field via API & "userdata_postsave" to change the usergroup....
--------------- Added 1354952836 at 1354952836 ---------------
I more query, How do I set a secondary user group.
I can fetch all data of a user using
global $vbulletin;
get the existing membergroup ids as $vbulletin->userinfo['membergroupids']; now I want update the exisiting member ids.
I found this line in forums:
$userdata->set('membergroupids',$membergroupids);
is $userdata also a global var like $vbulletin? when I try updating the update field is stuck ... is there something Im doing wrong here..
--------------- Added 1354955672 at 1354955672 ---------------
That worked....
But now I am stuck at another issue, I used my code for the administrator and it worked, ,
$userdata->set('membergroupids',$membergroupids);
So, now the admin is has addition usergroup, but this did not work for the other sample user I tried...
even a simple error message on profile field update is not appearing for the user...
any clues?
vibethemes
12-12-2012, 11:50 AM
All Right,
Everything is complete now and I've tested that if the profile field is set then add additional member group to user and change its primary group.
But now,
when I make an api call to validate the data entered in the profile field the user get stuck on loading saving the profile field.....
So, I guessed that it might be because of the api call (which is pretty fast)...
But what I could not understand is even if I set other field the loading state of the profile field never ends even if it is not field "X" ....
need help..please..almost done..
mokujin
12-12-2012, 12:25 PM
PS: can you give me a demo (PM me)? I dont understand all what you needed, I think I can help you.
I think you can run a query for the user if that user has set userfieldX
vibethemes
12-12-2012, 02:20 PM
great,
Below is my plugin code, what it does is as soon as user fills the profile field "X" , it validates it via an API call, if correct then changes the primary usergroup of the user to registered members from novice and adds a membergroup which gives user privelages to access the special forum:
global $vbulletin,$userdata;
if ($this->setfields["field7"])
{
$json_results = file_get_contents('some API URL'.$this->userfield["field7"].'.json');
$results=json_decode($json_results, true);
foreach($result as $res){
$itemid=$res->item_id;
$purchaser=$res->buyer;break;
}
if($purchaser == $this->userfield["field5"] && $item_id == 'CUSTOMID')
if(!in_array($vbulletin->userinfo['usergroupid'], array(2,6,7))){
$userdata->set('usergroupid',2);
$membergroupids = $userdata->fetch_field('membergroupids');
if ($membergroupids)
{
if(strpos($membergroupids, ', 9,') == false)
$membergroupids = $membergroupids . ", 9";
}
else{ $membergroupids = 9;
}
$userdata->set('membergroupids', $membergroupids);
}
}else{
$this->error('Invalid Purchase Code !');
}
}
--------------- Added 1355325963 at 1355325963 ---------------
Now the issue is:
Whether or not field7 is edited or not. User can not save profile changes as it gets stuck at loading state.
My inferences form this:
-> So it looks like the code is being executed for all the fields whereas it should execute only when field 7 is saved.
-> The wait for save never ends, I dont see an error or save.
but when I refersh the page the new value is saved and shown for field7.
mokujin
12-12-2012, 02:53 PM
The user will have to validate (or fill the code you gave) via Edit Profile - Additional Information (https://vborg.vbsupport.ru/profile.php?do=editprofile) ?
vibethemes
12-12-2012, 03:58 PM
Yes, user will fill the additional fields in "About me" tab and click save (ajax save).....my plugin gets triggered at user_presavedata hook. which means (correct me if I am wrong) that when user enters the value and clicks save, my plugin executes first before saving the value in the database.
My plugin then validates that the entered information is consistent (i.e the API call) and then I change user's usergroup and the value is saved in the profile field.....
mokujin
12-12-2012, 04:23 PM
I would rather create a custom page where your users can validate, add a new database table with codes inserted than using the method which you are trying to do.
vibethemes
12-12-2012, 05:55 PM
I would not be going for new database as the code is unique for every user and I validate it via a 3rd party call, so I am not aware of the codes the user is having...
Anyways, I believe that I am not doing this right:
file_get_contents('some API URL'.$this->userfield["field7"].'.json');
as soon as I pass the contents of json everything works fine...
mokujin
12-12-2012, 06:06 PM
So tell me, what your 'some API URL' is?
vibethemes
12-12-2012, 06:10 PM
~~ url removed ~~
just a sample...
--------------- Added 1355339588 at 1355339588 ---------------
I found a js error which appears when someone tries to change the profile field.
http://www.imagetoo.com/images/screentxt.png
--------------- Added 1355340181 at 1355340181 ---------------
I tried with Curl also but no luck....any clues how/why am I getting this js error on profile field save?
Here's my forum link (in case you want to try):
http://forums.vibethemes.com
--------------- Added 1355341379 at 1355341379 ---------------
Update: API call works, validate working, some issue with json handling....working on it..
--------------- Added 1355370916 at 1355370916 ---------------
Finally got it working!!
Cheers!
( v )
http://forums.vibethemes.com
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.