Help on how to use filemanager

Help on how to use filemanager

by Andreas Ragen Ayal -
Number of replies: 5

I'm completely stumped on how to use the filemanager form element to have a user upload some files and save them.

I'm working on making a course resource where the instructor must upload one or more files. Then when a student opens the resource they will see some specially rendered version of what the instructor uploaded.

I managed to add the filemanager element to the form (that was the easy part).
But I cannot, for the ..... figure out how I'm supposed to use the 'file_save_draft_area_files' function to save the actual stuff.

The relevant pieces of my source code are attached to this post.
I used the 'mod_assign' activity as some sort of example, so I understood that the 'file_save_draft_area_files' function should be in the 'add_instance' function in the 'lib.php' file. But I can't get it to actually work. I currently get the error message in the image attachment. The stacktrace leads to the 'file_save_draft_area_files' in my 'lib.php' file.

I'm almost pulling my hair out, I don't understand. I'm thankful for all help on this.

Attachment Screenshot from 2020-10-28 22-43-32.png
Average of ratings: -
In reply to Andreas Ragen Ayal

Re: Help on how to use filemanager

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

file_save_draft_area_files() takes a context id as the second param, not a context object.

So, to fix the error you need to write:

file_save_draft_area_files($data->attachments, $context->id, 'mod_doc', 'docs', 0, array('subdirs' => true));

I've not checked the rest of you code, so there may be other problems I've not spotted, but that should get you past the error you have reported.

Average of ratings: Useful (1)
In reply to Davo Smith

Re: Help on how to use filemanager

by Andreas Ragen Ayal -
Thank you very very much!
Now I can move on the the next inevitable error. Hopefully I can make better sense of it.
In reply to Andreas Ragen Ayal

Re: Help on how to use filemanager

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

This is a reasonable example that demonstrates how to use filemanager:

https://github.com/AndyNormore/filemanager

(I'd probably use file_prepare_standard_filemanager() and file_postupdate_standard_filemananger(), as that does some of the work for you, but that is just a wrapper for the file_prepare_draft_area(), etc. functions, so does the same thing in the end)

In reply to Davo Smith

Re: Help on how to use filemanager

by Andreas Ragen Ayal -

Is this code still relevant?
It seems to be 3-8 years old.

In reply to Andreas Ragen Ayal

Re: Help on how to use filemanager

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I'm not aware of any relevant changes to the Moodle files API in that time - is there something you think is out of date?