Course request and forced category selection

Course request and forced category selection

by T V -
Number of replies: 3
On 1.9.5+ course/request_form.php starting from line 57 we have the code that adds category list to the course request page. However the very first category is something that I don't want people to use, so I'd like Moodle to force people to change it. I tried using regexp on line 59 but it had no effect.

So how can I force category to be changed from the default before course request is accepted?
Average of ratings: -
In reply to T V

Re: Course request and forced category selection

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 can't see the code you refer to: http://cvs.moodle.org/moodle/course/request_form.php?view=markup&pathrev=MOODLE_19_STABLE

I suggest that instead of trying to prevent users from selecting the option you don't want, you instead just remove that option completely.

Presumably the code you are looking at gets an array of category ids and names. You just then need to do something like

unset($categoryoptions[$badcategoryid]);
In reply to Tim Hunt

Re: Course request and forced category selection

by T V -
Now I feel dumb. Of course you can't see it because it's only in my code smile We wanted our requests to specify the category also..

But your idea of dropping the option is almost what I could use. The problem is, however, that people do not make sure they have the proper category. So dropping one wont make a difference.

This is what I have:
make_categories_list($displaylist, $parentlist, 'moodle/course:request');
$mform->addElement('select', 'category', get_string('category'), $displaylist);
$mform->addRule('category', get_string('missingcategory'), 'required', null, 'client');
$mform->setType('category', PARAM_INT);

Is it possible to have regexp there to check if the category value is something else than 86?
In reply to T V

Re: Course request and forced category selection

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
OK then try

unset($displaylist[$badcategoryid]);
$displaylist = array_merge(array(0 => get_string('choose') . ' ...'),
        $displaylist);
// ...

That gets rid of the choice you don't want, and adds a "Choose ..." option at the start of the dropdown.

I don't think you can use addRule for what you want. Instead, in the form's validate method, do

if ($data->category == 0) {
$errors['category'] = get_string('Please choose a category', 'myplugin');
}

I have typed all that from memory. It may not be exactly right, but that is the general idea.

I wonder if we would like this in Moodle core. Is there a tracker issue about this? How many votes does it have? Do you know How to make a patch and attach it to a tracker issue?