I have a plugin that has two file areas (conceptually): video and audio. How can I save the upload the chosen file from the filepicker to the video file area of my plugin, for example?
Here's the documentation you're looking for: https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#filemanager
When a file is uploaded with the filemanager, it's stored in the user's draft filearea, and the itemid is passed as the value of the field when the form is submitted. You then use file_save_draft_area_files() to move the files to your plugin's file area.
In the section 'Load existing files into draft area', where do I call that code?
In the section 'Store updated set of files', where do I call that code?
file_get_submitted_draft_itemId I think I need to call that in lib.php?.
In file_prepare_draft_area, where do I get the 'context'? Nowhere does it explain, what a context is in Moodle!
Context information:
https://docs.moodle.org/dev/Access_API#Context_fetching
https://docs.moodle.org/dev/Roles#Context
Plus search the core code for the same functions and see how they are used in other solutions to learn from what has been done before.
The "load existing files into draft area" code is called when you're creating an instance of your form, so between when you do
$mform = new my_form_class();
and
$mform->display();
The "Store updated set of files" code is called wherever you're handling the data submitted by the form, after you've done $mform->get_data();
Broadly speaking, you can think of a context as "an area of the system". For example, each user has a context, each course has a context, each activity module within a course has a context. For the draftarea, you'll use the user's context (context_user::instance($USER->id)). When you save the file, the context you use depends on the type of plugin you're making. If you're making an activity module, then use the module instance's context (context_module::instance($cmid)).