How do I use the Cohort form in my plugin

90 Views Asked by At

How do I use the Form for creating a Cohort in Moodle in my own plugin?

I want to use the form, create the Cohort and return to a URL specified by with the Cohort id as a GET parameter ie http://myip/moodle/myplugin/myscrip.php?cohort_id=0

I've taken a look at the moodle files for the form but it's all chinese for me as i'm a complete novice as it comes to moodle development. What is the 'nice' way of using it?

1

There are 1 best solutions below

0
On

The forms take a bit of getting used to, they are based on quickforms.

https://docs.moodle.org/dev/Form_API

https://docs.moodle.org/dev/lib/formslib.php_Usage

https://docs.moodle.org/dev/lib/formslib.php_Form_Definition

Usually there is a file named edit.php which uses the class in edit_form.php - but the file names can be anything.

You could take a copy of /cohort/edit.php and put it into your local plugin. If you are only creating cohorts and not updating them, then remove the update and delete code and keep the add cohort code:

    $cohortid = cohort_add_cohort($data);
    if ($usetags) {
        if (isset($data->otags)) {
            tag_set('cohort', $cohortid, tag_get_name($data->otags));
        } else {
            tag_set('cohort', $cohortid, array());
        }
    }
    //update textarea
    $data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context, 'cohort', 'cohort', $cohortid);
    $DB->set_field('cohort', 'description', $data->description, array('id' => $cohortid));

Then redirect to your link

    $url = new moodle_url('/myplugin/myscrip.php', array('cohort_id' => $cohortid));
    redirect($url);