Save to file area

Save to file area

by Pedro Remedios -
Number of replies: 6
I've been looking for this in the docs but I can't find it or didn't see it.

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?


Average of ratings: -
In reply to Pedro Remedios

Re: Save to file area

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

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.

Average of ratings: Useful (2)
In reply to Mark Johnson

Re: Save to file area

by Pedro Remedios -

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!

In reply to Pedro Remedios

Re: Save to file area

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

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.

Average of ratings: Useful (2)
In reply to Gareth J Barnard

Re: Save to file area

by Pedro Remedios -
Thank you very much for clearing that up! I wouldn't have known that the Access API handles contexts!
In reply to Pedro Remedios

Re: Save to file area

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

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)).

Average of ratings: Useful (2)