Automatic Linking of Profile Form and Groups

This is rather complicated, and only partly affects the Flexprofile and Flexreg plugins!

My site links up people who have an interest in areas of the country. The user profile has a field for members to select the area they live in, and another field to select all the areas they are interested in. For example, some of the options are "Wales - West", "England - South West".

Each area has a group with the same area name as used in the profile, but with the prefix "Area: ". For example "Area: Wales - West", "Area: England - South West".

Members need to belong to the groups for the areas chosen in their profile, and by default have e-mail notification set. I'm using the AU Group Notifications plugin to set e-mail notifications when people join a group.

So ideally what I need is that when a member adds or removes an area in their profile, they immediately join or leave the matching group. I think this would be difficult, so I would be happy to run a cron job that synchronises all members at regular intervals.

I'm familiar enough with PHP to have made a lot of minor changes to the appearance of my Elgg system, but I'm struggling to make sense of getting any deeper and dealing with entities etc. I usually take bits of existing code and adapt them, but I'm still confused by it!

I appreciate that the correct way to do this would be to write a plugin. Is it going to be as difficult as I think to write one to do this task? Can anyone make some suggestions to get me started? I've asked in this forum because I think dealing with the Forms plugins might be the most difficult bit.

The quicker alternative, that also seems easier, could be to do it using SQL statements. Would I actually do any harm with this if I tested it carefully, or would I come up with unforseen problems?

I've tried adding the option to allow users to select groups to join on the registration form, but this only covers registration, and didn't seem to work for groups with logged in user access.

 

  • I've got a partial solution to this. I've written a plugin that allows Admin to add and remove users from groups from their profile page. I'm reading the contents of their profile, and selecting groups to add or remove manually. I'm working with the live data at the moment, and can see that this isn't going to work very well. I need to be able to access the contents of two fields in the user's profile. The fields are:

    A Pulldown (Select) box called location_presentarea
    A Checkbox group called location_preferred that can have multiple selections

    I'm still trying to make sense of accessing Elgg data. Would someone help me make sense of doing this please?

  • If you have a $user entity and a field with the internal name of internal_name, then you can access the field value with $user->internal_name. If a checkbox field has multiple values, then $user->internal_name will be an array.

    You can also automatically save a value by simply setting it.

    Eg.

    $field->favourite_colours = 'red';

    or even

    $field_favourite_numbers = array(3,9,17);

    Elgg automatically loads and saves the database values for you.

  • Sorry, that should have been:

    $user->favourite_colour = 'red';

    or even

    $user->favourite_numbers = array(3,9,17);

  • Thanks Kevin. Just what I needed. I've got it to do exactly what I want for now, and think I can do the automated version when I have time.

  • So when will we all see your nifty code / plugin published here GPL`ed on the community for the rest of the Elggsters to enjoy and comment on ?

  • Not for a while I'm afraid. A lot of it is hard coded to deal with specific data in my profile form, and my groups. I'd like to turn it into something useful to other people when I can.

  • I've just discoverd that a multiple selection with only one item selected returns a variable and not an array! I'm using in_array() to check if the option I'm looking for has been selected, but it doesn't work with only one option selected.

    It seems to work if I do this:

                        if (in_array("lid_wales", $user->housing_sitetype)){
                            .........
                        }
                        if ($user->housing_sitetype == "lid_wales"){
                            .........
                        }

    but is this the correct way to do it?

  • That feature is actually part of the Elgg API - single values are returned as scalars. This is for convenience as most fields store single values. Typical code uses is_array() to see if the value is an array or a scalar.

  • I tried is_array() but it didn't work. I'll have to try again!

Form and related plugins

Form and related plugins

User-generated content, flexible user and group profiles, registration forms, custom file forms