How to create a Listbox accepting multiple selections and tied to DB?

How to create a Listbox accepting multiple selections and tied to DB?

by Tan M -
Number of replies: 4

Hello there!

I am looking for some guidance today on changing the "course settings" page (course\edit.html). I would like to add a listbox that allows multiple selections. For example, something that looks like this:

http://www.fontstuff.com/images/acctut11c.gif

Now I am not a programmer, so I followed some google research to take the following step:

1. I used PHPMyAdmin to create a field in the course table called TESTFIELD.  For the field type I used SET and for the values (since you have to specify them) I put in some values (example: 'blue', 'red', 'green', 'yellow')

Now my thinking is that I need some code on the edit.html page that will pull the values from the TESTFIELD field, display them in a listbox, and then allow a user to select which values they would like.  Whether they select one or multiple, their selection should be saved in the TESTFIELD field.

This is where I am stuck.  Would someone be able to:

1) point me to code somewhere else in Moodle that does this same thing already?

2) if it's easy enough, provide me with a code snippet that will accomplish the above?

Thanks so much!  I look forward to your responses!  smile

Average of ratings: -
In reply to Tan M

Re: How to create a Listbox accepting multiple selections and tied to DB?

by Tan M -

Is it possible that not a single moodler knows how to accomplish this?!?!  surprise

I did some searching in the forums and it doesn't look like anyone has asked this question before... could it really be true that nobody has had a need for allowing multiple selections to be stored in a database field?

In reply to Tan M

Re: How to create a Listbox accepting multiple selections and tied to DB?

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers
Hi Tan -

I'm a little unclear on what you are trying to do. You may want to be more general with your question and describe the problem you are trying to solve.

It sounds like you are trying to store a number of possible values for a field per course. So, in your example, you want to specify a 'colours' field for the course which can have zero or more values.

There is a number of issues you need to resolve here. First, where will the list of possible values go? Your suggestion is to use a MySQL 'SET' so that the values are specified in the database table. I'll assume that that's what you will use. Note, that even MySQL frowns on using that data type (http://dev.mysql.com/tech-resources/articles/mysql-set-datatype.html). You may be better off just using a text field containing a CSV list, and create a separate table to store your possible values, but that's another discussion...

The next thing you will need to worry about is getting the actual data selections from the form. You are correct that you will need to modify 'edit.html' in the '/course' directory. You can add a multi-select input type like you show in your attachment. You will need to figure out how the data that comes from it looks in the php code so that you know how to handle it.

After that, you will need to process and store the data from the form. This is done in the 'edit.php' file in the '/course' directory. Again, figuring out how it comes in the form data will help. You will likely need to process it to put it into a CSV string list as required by the SET type, and load it into the course object that gets saved.

Hope this helps.

mike

In reply to Mike Churchward

Re: How to create a Listbox accepting multiple selections and tied to DB?

by Tan M -

Hi Mike,

Thanks so much for your response to my post! Per your recommendation, let me describe the actual problem I am trying to resolve:

Our classes are training courses for different products. As such, we would like a way to "tag" our courses with what products they belong to. Using categories doesn't work because a course can only belong to one category -- our courses may belong to multiple products.

As a result, I thought: Hey! It would be great if we could tag our courses with different properties, such as what product a course belongs to, whether it's a featured course or not, what role in the organization the course is meant for, etc.

I figured we just need to add fields to the course table in the DB and then modify the course settings page so that users could assign the tags when they create the course.  Doing the yes/no tags has been easy (for example: is this a featured course?) but doing the multiple selections tags has proven to be a challenge.

I have no idea what code to write that would provide me with a bunch of choices, allow me to choose one or more of those choices, and then store that info in the database so it could be pulled from moodle or other sources.

I thought maybe something like the code for choosing a country in the user profile screen, but that allows only one selection.  Then I looked at the glossary category code, which allows multiple selections, but I didn't really see it doing the same thing that I'm looking for.  All in all, I'm pretty stuck.  smile

Your post was definitely helpful in that it confirmed that I am at least thinking the right way, though I will probably need to use a different field type than SET, as you recommend. For the yes/no tags I just had to change edit.html, but from your post it seems I may need to change edit.php for the multiple selection tags as well.  Yikes!

I wonder if anyone has done something similar to this or if I'm just missing the existing code that already accomplishes this?