User Registration: Conditional User Profile Fields

User Registration: Conditional User Profile Fields

by Andrew Shaw -
Number of replies: 7

Hi smile


I'm hoping someone can point me in the right direction as I have searched for a couple of days now and not really found an answer.

I have set up some additional user profile questions for the new user registration process but not all of these will need to show, as they only need to be dependent on the selection from a previous question. Is there a way to show/hide these dependent on the answer of another profile question?


I have been attempting this by editing the signup_form.php and although I can add these fields in manually, I am struggling to find a way to show/hide them based on another question selection.


Many thanks,


Andy

Average of ratings: -
In reply to Andrew Shaw

Re: User Registration: Conditional User Profile Fields

by Jamie Kramer -

If anyone knows a non-development solution to this I would be interested to hear about that as well.

In the past we have customized the signup page for our clients to build some logic in like what you are suggesting. We used javascript and jquery to add conditional fields with some logic, and auto-complete dropdown selectors. We also added server side validation in PHP. This did require some changes to Moodle's code.

To your question, there is not a way to do what you want, built into Moodle.

In reply to Jamie Kramer

Re: User Registration: Conditional User Profile Fields

by Andrew Shaw -

Thanks for your reply Jamie, is there any current documentation/guides where I could take a look at how to do this with javascript/jquery, or could you provide any pointers for me?

I have been editing the php manually to try and achieve this but I'm not great with jquery and php I'm afraid, if it was .net/asp I'd probably be ok. I did find the moodle api info so I've been able to add and validate custom field entries by adding those into the singup php page but I haven't had much luck past that.

Any help would be greatly appreciated.

Thanks,

Andy 

In reply to Andrew Shaw

Re: User Registration: Conditional User Profile Fields

by Andrew Shaw -

Ok, so just to update this thread as I have now solved my issue. I searched so many threads at the beginning and nobody posted if or how they managed to find a way of doing it. Hopefully this will help someone out there.


I guess this technically is a 'development' solution to the problem but the way I did it didn't require me to edit the php script itself, that remained the default signup php page. I did have to spend a bit of time learning a bit of jquery though but it wasn't too hard once I got to grips with it.


Set up all the additional fields you need via site administration>users>accounts>user profile fields. Enter ALL the possible entries in the options for each added profile field, that way when you build the dropdown menu options in jquery later, they will submit to the database and will also validate correctly.


The jquery itself goes in the 'Additional html' section of moodle, via the administration options. It's FAR easier to be editing your code in something like notepad++ and pasting it in the 'Additional html' section. Don't bother trying to edit your code in the little window within moodle itself.


A good starting point, if you didn't know jquery like me, is to look at https://css-tricks.com/dynamic-dropdowns/. I used the json method (method 2) as it fit the bill quite nicely. If you didn't know this already, you can find the html element IDs/names via your browser in development tools.


Once you can get this far you can start looking at using conditional statements to show hide the next dropdown profile field, then the next, using the case statement function to clear and repopulate the dropdown options based on the previous selection. 


It took me a lot of trial and error because I was learning as I went along (Google is your friend) but if you have a little experience in programming then you can get there. I'm no genius and even I managed it ;)


 

Average of ratings: Useful (1)
In reply to Andrew Shaw

Re: User Registration: Conditional User Profile Fields

by Alex Cabezuelo -

Hi Andrew


I have the same problem and was going to try to develop it by scratch, I saw your solution and I would like to know if you can send me an example of this jquery code that you add in the 'Additional html' section.


Thank you very much!


Àlex

In reply to Alex Cabezuelo

Re: User Registration: Conditional User Profile Fields

by Andrew Shaw -

Hi Alex,

Sorry for the delayed reply, I have been moving house!

I have  attached my code and my json file so you can see what I've done. I've commented quite a bit as I went along so hopefully that will help.

I work in health care and we want the user to select their role (the 1st custom profile dropdown), so if are a Doctor/Nurse/Healthcare Assistant etc.

The 2nd dropdown (this one asks about the Specialty that the healthcare provider works in) will then show if necessary and only give the options related to their 1st dropdown selection.

There is a possibility, depending on the first 2 dropdowns, that there is a 3rd dropdown relating to the location of the healthcare provider and that will show if needed.

You'll notice that I have also re-labelled the profile fields and also added a function to find the existing selections of the dropdowns if they are not blank... this was because if the user goes in and edits their profile after initial setup it will show what they originally chose instead of resetting itself. These were re-labelled because for some reason they didn't show as 'required' with the asterisk when the text changed. 

I hope it helps anyway,

Andy smile

Average of ratings: Useful (2)
In reply to Andrew Shaw

Re: User Registration: Conditional User Profile Fields

by Albert Leatherman -

Hi Andrew, any thoughts on how to autofill a custom user profile field based on performance on a task? For instance, the field could be called Level and the possible values could be A, B, or C, depending on how one performed on a certain diagnostic test. It would be really handy to have such a field automatically populated based on performance on an activity in order to be able to enable or restrict access to other activities site-wide. Thx