xoops forums

lmzan

Not too shy to talk
Posted on: 2010/9/9 19:47
lmzan
lmzan (Show more)
Not too shy to talk
Posts: 104
Since: 2005/6/21
#1

How to set the user group in User Profile module

Hi

In User Profile Module 1.58 we have the possibility to create a new field and include it in the register form (modules/profile/register.php). It's a Select form field with all the groups in the site to choose from.

The problem is that I need people to choose one group to belong to, but I don't want them to see all groups, such as administrator. I need to show only some groups.

For example, in an College website, I have specific content for each course, like Administration, Psychology, etc. When a student creates a user in the website, I'd like to give him the possibility to choose which course he is in, according to the user group he's going to choose. If it's a small group of students the admin can assign each one to a specific group, but in a big Institution, this is a lot of work.

Can I pass this information as a hidden form field?
Can I have a different register.php for each group?
Can I choose which user groups will appear in the select field in the profile registration form?

Any ideia would be welcome, so it can allow Xoops to be used in this kind of college websites.

Thanks!

ghia

Community Support Member
Posted on: 2010/9/10 1:34
ghia
ghia (Show more)
Community Support Member
Posts: 4954
Since: 2008/7/3 1
#2

Re: How to set the user group in User Profile module

I think you can add this by inserting in register.php before
if ($message) {
some repeated code for every choice like this:

if ($profile->getVar('your_field') == 1)
{
  
$member_handler->addUserToGroup(4$newuser->getVar('uid')));
}
your_field is the name of the profile field where you can do a select of some choices during registering.
In the example if choice 1 was used, the user is added to group 4 eg Psychology students.

lmzan

Not too shy to talk
Posted on: 2010/9/10 3:03
lmzan
lmzan (Show more)
Not too shy to talk
Posts: 104
Since: 2005/6/21
#3

Re: How to set the user group in User Profile module

Fantastic Ghia!

Problem is solved. I've tested in a different website and it worked just fine. I'll do another round of tests in the production site but I think it's ok.

I'll post the link and the solution I tested just to close the topic with the result.

Thank you very much.

jimmyx

Quite a regular
Posted on: 2010/9/10 5:03
jimmyx
jimmyx (Show more)
Quite a regular
Posts: 338
Since: 2007/7/18
#4

Re: How to set the user group in User Profile module

Hi lmzan can you show us how you do this ..step by step...


i think im gonna need this in a future project

thank you

lmzan

Not too shy to talk
Posted on: 2010/9/12 1:39
lmzan
lmzan (Show more)
Not too shy to talk
Posts: 104
Since: 2005/6/21
#5

Re: How to set the user group in User Profile module

First of all you have to install the User Profile module 1.58, that comes with the latest XOOPS version (2.4.5). In older XOOPS versions you can also find this module, but I didn't test it in this older versions, so I can't say it works too.

Then go to the User Profile -> Fields option in the admin area.

In this page you can see all the fields available in the registration forms. On the top left, click in "Add Field".

Then you have to create a form field to let users choose which user profile they are going to register to, for example:

- Students
- Ex-students
- Teachers

We will create a selection using radio buttons for the visitor to choose from, allowing only one choice. Depending on this selection, the user will be added to a specific user group. You have to create this groups beforehand, but that part is easy.

Fill in the form for the new field:

- Title: the name of the field in the form;

- Description: some text to explain the field;

- Category: choose "Default", to be among the first options presented;

- Weight: choose "3", to appear right after the password option in the form;

- Name: give a name to the field value, in this example lets use "user_type". We'll use it later;

- Field Type: choose "radio buttons";

- Field editable from profile: choose only "Administrator";

- Required?: choose "yes";

- Show in registration form: choose "Basic", or the first registration step;

Click on "Submit" button. You'll see a confirmation message and will come back to complete the new field's information. Now you have to add the radio button options.

You we'll see 3 fields for "key" and "value". Follow the example:

- Key:1 Value:Students
- Key:2 Value:Ex-students
- Key:3 Value:Teachers

After filling this, click "Submit". You'll see a confirmation message and will come back again to complete the new field's information. You'll be able to see the options and then you can add more or just finish the edition. Click "Submit" again, you'll see the confirmation message and will go back to the User Profile -> Fields page.

Go to the registration page in your website (http://(your_site)/modules/profile/register.php) to see if your new field is on the right place and with the right options. Correct any mistakes and then we go to the next step, it's not working yet!

In order to use Ghia's suggestion, you have to change the file http://(your_site)/modules/profile/register.php. Make a backup copy of it and open the file in your favorite editor.

Look around the line 266 for this part of code
if ($message) {


Before this line insert this code
if ($profile->getVar('user_type') == 1)//if choice is "student" 
$member_handler->addUserToGroup(4$newuser->getVar('uid'));//adduser to group 4-student 

if (
$profile->getVar('user_type') == 2)//if choice is "ex_student" 
$member_handler->addUserToGroup(5$newuser->getVar('uid'));//adduser to group 5-ex_student

if (
$profile->getVar('user_type') == 3)//if choice is "teacher"
$member_handler->addUserToGroup(6$newuser->getVar('uid'));//adduser to group 6-teacher 
}


For each condition we are checking the value that was set as the Key, in the field configuration. In the first condition, if the selected option is the first (student) the user will be added to the user group 4, in this case we created a group called "Student" and this user group id is 4.

Upload the register.php file to folder http://(your_site)/modules/profile/ , clear the cache folder and try it.

I'm far from being a PHP expert, so there must be a more elegant way to do it, but I'll let this to the ninja coders to provide it!! This is as far as I can get and it's working for me!

Hope it works for you guys.

zorro87

Just popping in
Posted on: 2011/6/7 12:53
zorro87
zorro87 (Show more)
Just popping in
Posts: 92
Since: 2006/9/29
#6

Re: How to set the user group in User Profile module

Hi!

Thank you for sharing!

But if someone wants to change group once the profile is registered how to make it?

@+
Z

lmzan

Not too shy to talk
Posted on: 2011/6/7 13:07
lmzan
lmzan (Show more)
Not too shy to talk
Posts: 104
Since: 2005/6/21
#7

Re: How to set the user group in User Profile module

Quote:

zorro87 wrote:

But if someone wants to change group once the profile is registered how to make it?


Hi there!

Well, changing groups is something very sensitive and usually a user shouldn't have the permission to do it, only the administrators.

Depending on the situation, this would require something specifically done for this purpose, because if you allow a user to manage groups, in the admin panel, you would be giving him the permission to choose from any group to be part of, and that's out of the question!

Maybe if you explain why you need this other people could help too.

Thanks for your comment!

zorro87

Just popping in
Posted on: 2011/6/8 14:17
zorro87
zorro87 (Show more)
Just popping in
Posts: 92
Since: 2006/9/29
#8

Re: How to set the user group in User Profile module

Hi lmzan!

Thank you for your answer.

In the example you give: users can choose between three groups:
- Students
- Ex-students
- Teachers
With your configuration, In profile module you can select one of this three groups.
and only one of this 3 groups.

When a student becomes ex-student: he can go change his profile but the modification will not be done: he will stay in student group.


To make it, there is probably something to add in
modules/profile/edituser.php

@+
Z