Dynamic Dependent Dropdown LIsts

Dynamic Dependent Dropdown LIsts

by W F -
Number of replies: 15

Hi All;

Please forgive me if this is covered somewhere that I have not found.  If so, a link or a search term to find it would be truly appreciated.

I need to customize the moodle registration page (moodle v2.3.1+ with skillsoft mod), such that my "Other" fields that I have added to the page, using the moodle admin capabilities, can use dynamic dependendent dropdown lists.  So for example, I want to be able to have a dropdown with choices of "Employee" or "Contractor".  If Employee, one dd list appears, if Contractor, another dd list appears or is populated with the correct choices to match the selected choice (employee or contractor).  I have 3 pairs that I need to apply this to.

I found on the forums a piece about the enhancement for dynamic dependendant dropdowns on the "Add community course" page.  The problem I have here is it seems these dependencies are built upon fields that are always on the page, rather than the dynamically added "other" fields that I am trying to make this work on.  The other problem I have here is this is based on building tiered lists rather than showing next selection only if correct choice is made in first selection.

I see a few ways this can be done, but am hoping someone can point me to the best solution, or a solution/enhancement that has already been built.  The scenarios I see are:

1.  Write code similar to what is in the enhancement for the  "Add community course" page, but put it on the new registration page and have it go in the setion where the "other" fields are being generated, using an if/then/else statement to determine which fields it will apply to.  I would also need to modify it to make the fields appear/populate based on the correct choices, but could live with the "tiered" style as well.

2.  Get a js into the header of that page to provide me with the dynamic dependendant dropdowns, using scripts that are already available (I remember seeing somewhere in the moodle admin, a place to insert calls to js, but don't know if this is appropriate nor do I remember where that is).

3.  Make the "other" fields I am using, permanenet fields on the registration page, and then apply the "enhancement" mentioned above.

Any thoughts, tips, pointers, links, help, etc. very much appreciated.  This is my first project in moodle, but I have a lot of experience in php as well as several other languages.

Thanks

Average of ratings: Useful (1)
In reply to W F

Re: Dynamic Dependent Dropdown LIsts

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

I cannot offer a solution but I am very interesting if someone can provide one.

Joseph

In reply to W F

Re: Dynamic Dependent Dropdown LIsts

by Hubert Chathi -

Are you referring to the custom profile fields?  You can create a custom field type (see /moodle/user/profile/field/*).  You can have the custom field type insert JavaScript to hide/show the field.

In reply to W F

Re: Dynamic Dependent Dropdown LIsts

by Nicola Bennett -

Hi,

I am also very interested in a solution for this, so if anyone can help at all that would be great.

Many thanks
Nicola.

In reply to Nicola Bennett

Re: Dynamic Dependent Dropdown LIsts

by Patrick Pollet -

The library quickforms has an addon named advmultiselect that does it http://pear.php.net/package/HTML_QuickForm_advmultiselect

thus it should be possible to copy it to yourmoodle/lib/pear/HTML/QuickForm directory, and create the needed wrapper in lib/form/advmultiselect.php, similar to other wrappers  and finally to include it in your Moodle forms by adding a line such as

MoodleQuickForm::registerElementType('advmultiselect', "$CFG->libdir/form/advmultiselect.php", 'MoodleQuickForm_advmultiselect');

at the end of lib/formlib.php 

Cheers

 

Average of ratings: Useful (1)
In reply to Patrick Pollet

Re: Dynamic Dependent Dropdown LIsts

by Patrick Pollet -

Oups my fault. I picked up the wrong component.

It should be HTML::QuickForm hierselect (for hierarchical select) that IS INCLUDED in lib/pear/HTML/QuickForm/ but does not have a Moodle wrapper in lib/form.

The only things missing to be able to use it is to add the wrapper and register it by adding 

MoodleQuickForm::registerElementType('hierselect', "$CFG->libdir/form/hierselect.php", 'MoodleQuickForm_hierselect');

near the end of lib/formslib.php

See http://tracker.moodle.org/browse/MDL-20589 when this was signaled back in 2009 and is still not resolved ; it has the wrapper the activation code and some sample code 

Looks like it was not included since its behavior when javascript is disabled does not seems to be proven.

Cheers.

 

Edit : also discussed here (in french) https://moodle.org/mod/forum/discuss.php?d=140392

Average of ratings: Useful (1)
In reply to Patrick Pollet

Re: Dynamic Dependent Dropdown LIsts

by george alex -
Patrick, I've done all the steps you mentioned in order to add a hierselect field in my form (the existing resource form with extra fields), but it doesn't work. When i'm trying to add a file, the page is frozen, nothing is displayed. My moodle version is 2.2.6. Is it possibly a compatibility problem?? Any other idea for dependent dropdown lists?
In reply to W F

Re: Dynamic Dependent Dropdown LIsts

by Andrew Normore -

I've spent a GREAT deal of time on creating this, unfortuantley I cannot share the code. 

I started by replicated the extra profile fields functionality, and converting it to work by attaching to courses. It'll take you a solid week, but is a GREAT experience if you're new to Moodle. Expect to spend 15-20 hours on it, but yes, it can totally be done.

In reply to Andrew Normore

Re: Dynamic Dependent Dropdown LIsts

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Andrew "I've spent a GREAT deal of time on creating this, unfortuantley I cannot share the code. "

sad

In reply to Joseph Rézeau

Re: Dynamic Dependent Dropdown LIsts

by Patrick Pollet -

Please find attached a sample code using hierarchical select with Moodle 2.3.2

  1. do the modifications as in tracker http://tracker.moodle.org/browse/MDL-20589 (add the file lib/form/hierselect.php AND add a line to lib/formslib.php
  2. copy the attached file into Moodle's local directory
  3. test by calling http://yourmoodle/local/test_hierselect.php see attached screenshot

Enjoy

Average of ratings: Useful (1)
In reply to Patrick Pollet

Re: Dynamic Dependent Dropdown LIsts

by Céline Perves -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello,

I worked on a new html_quickform_element that dynamicaly load a sytem of chain of multiple select objects.

It works with ajax ( with jquery), each time you choose an option in a select it refresh all the sub select with ajax


after selecting

It allow to select threw an infinite number of level

I'm currently finishing passing this code to moodle 2.x, integrating this component to a custom edition of course form

 Are you interested by that code? It allows to take in charge a large amount odf course categories for example.

Average of ratings: Useful (4)
In reply to Céline Perves

Re: Dynamic Dependent Dropdown LIsts

by sakai user -

Yes! Very interested in a way to integrate this into a given moodle plugin.

In reply to Céline Perves

Re: Dynamic Dependent Dropdown LIsts

by Naveen eAbyas -

Very Very interested with this..please help me.....

In reply to Patrick Pollet

Re: Dynamic Dependent Dropdown LIsts

by Colin Gray -

Hi Patrick, thanks for posting this - it's been really helpful.

I've completed all of the above, all is working fine. I'm a little stuck on how to implement it on my user profile page though I'm afraid.

I don't mind editing the core moodle code - could you give me some pointers on which sections from the test php file that I need to integrate into the user/editlib.php file to make this function as a new user profile field. I'd like to add it in right at the bottom of the main "general" section.

I'd really appreciate the help, thanks very much!

Colin 

In reply to Patrick Pollet

Re: Dynamic Dependent Dropdown LIsts

by Luis de Vasconcelos -

Thanks for this demo code Patrick...

Your test_hierselect.php page has the values of the arrays for the two lists ($letters and $words) hardcoded in the page. How can you build those arrays using values from the Moodle database?

Example: if I want to build the first list using mdl_grade_grades.userid and the second list using mdl_grade_grades.finalgrade. Once you select a userid on the first list it shows you all the corresponding finalgrades for that userid on the second list.

Thanks.