Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: Racketydoo - helmikuu 19, 2014, 12:39:12 IP

Otsikko: How to create member groups based on custom profile fields
Kirjoitti: Racketydoo - helmikuu 19, 2014, 12:39:12 IP
Firstly, I apologise if this is in the wrong place. Secondly, I apologise if this information is available elsewhere.  ;D I googled it to death before giving up and accepting that my total nubbiness with SMF might mean I am searching for the wrong thing entirely!

I am trying to set up a member group on my small forum (running 2.0.7) based on user-selected responses in a custom profile field that I have set up. The field is working correctly and upon registration users select which 'group' they belong to (a simple 3-option radio button selection).

I have no idea how to easily allocate people to member groups based on the group that they have selected so that they can view boards appropriate to their group. It's entirely possible that this is a simple case of my ineptitude  :-\, but if someone could point me in the right direction, I'd appreciate it.  :)

I'd be so grateful for any assistance.

Regards,

~ Racketydoo
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: kat - helmikuu 19, 2014, 04:06:43 IP


In a word, "Permissions", assuming I've got what you're after.

They can get a bit complicated. So, it's probably best if you do a bit of light reading...

http://wiki.simplemachines.org/smf/Permissions

http://wiki.simplemachines.org/smf/Board_Permissions

:)
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: margarett - helmikuu 19, 2014, 04:49:55 IP
Actually I've read it differently, chief :P
The OP needs permissions to allow each group to see/post in the adequate boards. But the main question here is (methinks), when a user registers, he chooses a group and he is automagically added to that membergroup (meaning he will get the adequate permissions right from registration)
There was a MOD to do that in 1.1.x (http://custom.simplemachines.org/mods/index.php?mod=485) :-\

This is what I understand from it:
You have a custom field, presented to the user at login, in which he chooses a membergroup. Something like this:


And you have, of course, the respective usergroups already created in your admin:


So you want that, when a user registers, he is automagically assigned to the membergroup with the same name. Is that it?
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: kat - helmikuu 19, 2014, 05:00:14 IP
You could well be right, there, young Marjorie! :)
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: Racketydoo - helmikuu 19, 2014, 06:20:42 IP
Thank you both so much for taking the trouble and time to respond, and thank you for the forum welcome.  ;D

Yes Margarett, that's exactly what I am trying to do. :) I hadn't even considered the possibility of doing it automatically (which would be fantastically fabulous!), but have even had trouble looking for a way to filter the groups selected at registration to do it manually.

I couldn't figure out how to filter by a custom field parameter so that I can display all the members of that selected group so that I could then subsequently add them to the relevant Membergroup.  :-[ At the moment I have approximately 350 members and the only way the custom field parameter seems to display is either at the post field, or in their profile. This seems prohibitive to selecting members of group A to give them access to board 1, and the members of group B to give them access to board 2.

If there's something that would help that would be fab - would I be able to install the mod you linked using the Version Emulation tutorial do you think (yes, I googled it hehe)? (I will do a gazillion backups before I try it  ??? ).

Again, thank you both for taking the time to help me - there is huge amount of info here and if I wasn't such a numpty I wouldn't have to trouble folks for their help. I genuinely appreciate it - very cool of you.  8)
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: margarett - helmikuu 19, 2014, 06:36:26 IP
There isn't a way to filter it, no...

Custom Profile Fields are meant just for display purposes, so there isn't much of way to do what you need, actually...
And don't try to install that MOD, it's really too old.

So, I can try to help you a bit. First let me try to get you a way to do the automagical membergroup assign at registration. Then we can run some queries to add your existing members to their membergroups.

Can you tell me the names of the 3 membergroups you are making available to your new members? And are you sure that the name presented in the custom field is the same name than the one the "real" membergroup has?
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: Racketydoo - helmikuu 19, 2014, 06:50:12 IP
Oh wow! Thank you so much!

Well, I guess that at least explains why I couldn't find the solution despite exhausting my google muscle.  ::) , and thank you for the warning about the old mod - I appreciate it.

Okay, the 'groups' selected on registration (these are the labels in the input field for the radio button selections):
BS
WS
F&F

The Membergroups that have been set up are identically labelled:
BS
WS
F&F

These Membergroups are currently set to Private Group Type (assigned membership) - I don't know if that makes a difference hehe.

I can't express to you how appreciative I am that you're prepared to spend your time helping me like this - I know I risk sounding like a slurpy-bum-lick, but I'm blown away.  :)

Thank you.
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: margarett - helmikuu 19, 2014, 06:52:15 IP
Another question: do you have a "do not select any group" option, or *ALL* your radio buttons do relate to a group?

And another: what's the name of the field (the name you gave when you created it)?
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: Racketydoo - helmikuu 19, 2014, 07:05:39 IP
Hi Margarett  :)

Yes, they all relate to a group - not selecting a group isn't an option for anyone. There are just the three radio buttons and those three input options.

The field name is: BS/WS/F&F (I'm not that original really am I? teehee), exactly as written, no spaces.

Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: live627 - helmikuu 19, 2014, 07:19:42 IP
try http://custom.simplemachines.org/mods/index.php?mod=2716 :)
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: margarett - helmikuu 19, 2014, 07:22:51 IP
Meh, that's an awful name :P Not the name per se, but this way I can't be sure what name did SMF give it in the tables... In my test board I got "cust_bs". We'll confirm shortly.

OK, here we go. This was actually easy.
First, you need to gather the ID of the 3 membergroups you created. To do so, go to your ACP --> Membergroups. Point your mouse to each one's names and you will see a link like this:
http://localhost/smf2/index.php?action=admin;area=membergroups;sa=members;group=9
See the "group=9" at the end? 9 is the ID. Take note of your 3 IDs (and relate them to their group, of course)

Now, we need to clarify what name did SMF gave to your custom field. The way custom fields work is a bit strange. You can find some background about that in this post (http://www.simplemachines.org/community/index.php?topic=518203.msg3666696#msg3666696) (totally unrelated subject but it serves the purpose)
So, please go to phpmyadmin, to your SMF database, table "smf_custom_fields" and find there your created field. Get the content of the column "col_name". This is the "internal name" that SMF gave to your field.

Finally, edit your file Sources/Register.php
Find:
$memberID = registerMember($regOptions, true);
Add BEFORE:
$sel_group = isset($_POST['customfield']['cust_bs']) ? (int)$_POST['customfield']['cust_bs'] : 'ABCD';
if (is_numeric($sel_group)) {
switch ($sel_group) {
case 0:
$regOptions['memberGroup'] = 9;
break;
case 1:
$regOptions['memberGroup'] = 10;
break;
case 2:
$regOptions['memberGroup'] = 11;
break;
}
}

Here's what you need to change in this last bit of code:
First line, you find twice the reference to your custom field name. Here I called it "cust_bs", you need to make it like your database has.
Then, the 3 occurrences of $regOptions['memberGroup'] = XX; Replace the number with the ID of the membergroups you created, in the order they show in the registration form.

And with this, you have your very own automagical membergroup assign on registration ;-)

edit: Ninja'd and feeling quite dumb :P I did search before I start this and couldn't find that
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: Racketydoo - helmikuu 19, 2014, 07:51:38 IP
Margarett
Thank you so much! I shall get working on this as per your instructions and will report back tomorrow! Once I've done this, is there a manageable way to go through the existing membership and retro-allocate them to their appropriate group?

Live627, thank you for linking the mod for me. I appreciate you taking the time and effort to do that. :) Having looked at it, it seems contingent on the group being free to join, and has the option to skip selection entirely. Unfortunately, the nature of the groups means that they are mandatory and aren't free to join, but are tied specifically to their input selection at registration for privacy reasons.

You guys are so cool!
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: margarett - helmikuu 19, 2014, 08:01:11 IP
It is possible, yes, but I need to scramble a MySQL query for that.

Be sure to take a copy of your Register.php before you start editing it. Just in case ;)
Also, please give me the name of the field from the table custom_fields. It will be handy ;)
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: Racketydoo - helmikuu 21, 2014, 01:57:01 IP
Hi Margrette!

Thank you, thank you!  :-* :-* The code worked like a charm - now new users are allocated into a group on registration based on their radio button selections! You're a star!

The field name you asked for is 'cust_betray'.

If there is some manageable way for me to retro-allocate people to each group that would be fantastic, because going through 350 members by hand will be laborious.

Again, thank you so much for the kind and speedy help you've all given me. You rock!  :)

Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: margarett - helmikuu 21, 2014, 04:47:11 IP
LainaaHi Margrette!
Who?! ;D

I managed to forget this, really sorry :(

I'll get to this through the weekend.
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: Racketydoo - helmikuu 21, 2014, 05:36:08 IP
Well, I would have called you Superman, but that might have been too much?  ;) The typo made my slurpy-bum-licking less nauseating? hehe.

Oh, please don't apologise - you're doing me a massive favour and I wouldn't presume to assume any sort of entitlement to your time or expertise. I can't begin to tell you how amazed I am that you're doing this to help me out as it is.  :)

Thanks Margarett aka Superman!  8)
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: margarett - helmikuu 23, 2014, 03:14:16 IP
So, I got something here. Please backup your database first because I'm really just a noob at this MySQL stuff

So, go to phpmyadmin, to your SMF database. Backup (please, backup), go to "SQL" tab and paste the following code:

UPDATE smf_members as m
JOIN smf_themes as t ON t.id_member = m.id_member
JOIN smf_membergroups as g ON g.group_name = t.value
SET m.id_group = g.id_group
WHERE m.id_group = 0

(I'm assuming your table prefix is "smf_")

It did work in my test forum and I tried several combinations.
Put your forum in maintenance mode (so that an eventual f*-up won't be noticed by your users :P ) and, after the query, check the table "smf_members" (or some known member profiles).

So, good luck ;)
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: Racketydoo - helmikuu 26, 2014, 11:11:19 AP
Margarett

*hug* *hug* *hug*

And did I mention ... **HUUUUGGGG**

Thank you so so much! You are The Awesome!  :-*

Your code was perfect - I now have my 370 members correctly retro-allocated to groups, with those group labels now nicely displayed in the memberlist.

I am blown away by your kindness, effort and trouble in helping me with this. Thank you seems so ... underwhelming ... but it's sincere and genuine. You rock!  8)
Otsikko: Re: How to create member groups based on custom profile fields
Kirjoitti: margarett - helmikuu 26, 2014, 11:17:20 AP


Glad it worked for you ;)
I'm now moving this to "Coding" boards