How to put a pull-down menu into each row in a table?

How to put a pull-down menu into each row in a table?

by Nadyare Pérez -
Number of replies: 7

Hi! i'm doing something like a small scheduler and i have a problem. Now, i have in the table what you can see in the attached file.

The names of the pupils and then one radio for each free slot and when i select one of them, the free slot is inmediately assigned to the student.

But is a shoddy piece of work. So i want to convert these radios into a pull-down menu doing the same.

I know how to get the free slots but i don't have an idea to put them into the menu. Can anyone help me, please?

Thanks! and sorry for my horrible english.

Attachment foto.JPG
Average of ratings: -
In reply to Nadyare Pérez

Re: How to put a pull-down menu into each row in a table?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
To generate a dropdown, taking advantatge of the function from weblib:
$slots = array(
'slot1' => '10:15 - 10:30',
'slot2' => '10:30 - 10:45',
'slot3' => '10:45 - 11:00',
'slot4' => '11:00 - 11:15',
'slot5' => '11:15 - 11:30',
'slot6' => '11:30 - 11:45',
'slot7' => '11:45 - 12:00',
);
choose_from_menu($slots, 'slotforstudent123');


When the user submits the form for process, to get the value:
$user123slot = optional_param('slotforstudent123', '', PARAM_ALNUM);
if ($user123slot) {
// Somthing was selected, and $user123slot will contain 'slot1 or 'slot2' or ...
}
In reply to Tim Hunt

Re: How to put a pull-down menu into each row in a table?

by Nadyare Pérez -

Thank you very much! That's very useful, but how can i get the array of slots if i don't know the slots beforehand?

I get the free slots doing:

$slots = get_records('interview_slots', 'interviewid', $interview->id, 'id');

but how can i put them into the array?

Thanks again!

In reply to Nadyare Pérez

Re: How to put a pull-down menu into each row in a table?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
You could use a for loop to copy them into an array. Or you could use the get_records_menu function which, if you select some data with two columns, returns an array already.
In reply to Tim Hunt

Re: How to put a pull-down menu into each row in a table?

by Nadyare Pérez -

I'm doing:

 if (!record_exists('interview_slots', 'student', $student->id, 'interviewid', $interview->id)) {

   $slots = get_records('interview_slots', 'interviewid', $interview->id, 'id');
              foreach ($slots as $slot) {

       $menu = get_records_menu('interview_slots','start', $slot-                                                >start, 'end', $slot->end);
    }

     $actions = choose_from_menu($menu, 'slotforstudent'); 

}

$mtable->data[] = array($picture, $name, $email, $actions);

But i'm getting what you can see in the attached file. The actions are out of the table and they are empty while they should have all the slots of the table above

What is wrong?

Thank you very much!

Attachment Dibujo.JPG
In reply to Nadyare Pérez

Re: How to put a pull-down menu into each row in a table?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I guess my hints were too cryptic.

To start with, your code does too many separate database queries. They are expensive, so it is always best to try to get all the necessary data with one query (but not to fetch unnecessary data).

So you just need

$slots = get_records('interview_slots', 'interviewid', $interview->id, 'start', 'id,start,end');


That gets the id, start and end time of each slot for this interview, ordered by increasing 'start'.

Then do

 
if (!empty($slots)) {
$choices = array();
foreach($slots as $slot) {
$choices[$slot->id] = $slot->start . ' - ' . $slot->end;
}
$actions = choose_from_menu($choices, 'slotforstudent', '', 'choose', '', '0', true);
} else {
$actions = '';
}
$mtable->data[] = array($picture, $name, $email, $actions);


choose_from_menu normally prints out the HTML for the menu immediately. You have to pass the 7th parameter ($return) as true, if you want the HTML returned for you.
In reply to Tim Hunt

Re: How to put a pull-down menu into each row in a table?

by Nadyare Pérez -
I've got itbig grin big grin Thank you very much!!

Now the last thing!

What can I do to convert all the slots of the pull-down menu into links?
I mean that when i select a slot for a student it take me to an action in view.php named for example assing passing like parameters the id of the chosen slot and the id of the user and they get associated.

Can you tell me what i have to change in your code to convert each slot in the menu to a link that doesn't need a submit button?

Thousands of thanks!!!
In reply to Nadyare Pérez

Re: How to put a pull-down menu into each row in a table?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I think if you use the popup_form function, instead of choose_from_menu, but I am not so familiar with how that works.