How do I retrieve values for custom form fields when editing a course?

How do I retrieve values for custom form fields when editing a course?

by v k -
Number of replies: 2

Hello all,

For my work, I must slightly edit Moodle from its standard...

I added my own new several custom form fields for Course Setting. So, when I press Create Course and "/course/edit.php" is opened, my custom form fields are also shown in addition to standard "Course Fullname, Shortname, Category, Visible" and so on.

When I fill in all fields, and press "Save", all the data is stored succesfully to DB tables: all standard fields' values are saved to standard table like 'course', and my own custom fields' values are saved to my custom DB table successfully.


Now the topic question:
When a course is created, and I want to edit its settings, the 'course/edit.php&id=..' is loaded again. And all standard fields are already filled with values from DB, and my custom fields are empty. 
How do I fill my own custom fields when editing the existing course, taking into account that all needed values data is saved in my custom table? Which function is used to fill standard field's values?

I am looking through 'course/edit.php' code, and some 'course_form', 'get_data' etc, but can't find the code of retrieving data itself...

Average of ratings: -
In reply to v k

Re: How do I retrieve values for custom form fields when editing a course?

by Sam Chaffee -
Picture of Core developers

I'm looking at Moodle 3.3.2 so this may be a little different as far as line numbers go, but take a look at course/edit.php starting at line 145. Looks like this:

// First create the form.
$args = array(
    'course' => $course,
    'category' => $category,
    'editoroptions' => $editoroptions,
    'returnto' => $returnto,
    'returnurl' => $returnurl
);
$editform = new course_edit_form(null, $args);

Those args are being passed into the form. If you look at how $course gets built up earlier in the script you can see that for an update it is loaded from the database. Then in the form definition (in course/edit_form.php) it is available in the _customdata property of the form. Look at line 26 in course/edit_form.php for how it is used.

Hope this gets you on the right track.

Average of ratings: Useful (1)
In reply to Sam Chaffee

Re: How do I retrieve values for custom form fields when editing a course?

by v k -

Thanks a lot!

I am going to try this method