Filemanager - Override a file

Filemanager - Override a file

by Helson C -
Number of replies: 10

Hello everyone,

When I submit a file in filemanager and generate link, works well

But when access the filemanager again, passing the same context, component, itemid

I can't see the file to override it.


$draftitemid = file_get_submitted_draft_itemid('attachments');

file_prepare_draft_area($draftitemid, $context->id, 'block_myblock, 'attachment', $itemid, $filemanageropts);

 To save I use:

file_save_draft_area_files($draftitemid, $context->id, 'block_myblock, 'attachment', $itemid, $filemanageropts);


I need only open the filemanager and see the uploaded before and override it.


What is wrong?


Average of ratings: -
In reply to Helson C

Re: Filemanager - Override a file

by Helson C -

Anyone, please?

In reply to Helson C

Re: Filemanager - Override a file

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

The code looks right, at a brief glance.

At this point, I would usually recommend putting a breakpoint on file_prepare_draft_area and then stepping through the code to figure out what is going wrong (you need xdebug + a suitable IDE to do this - but any time spent setting that up will be paid back many times over in terms of easier fixing of issues in your code).


In reply to Davo Smith

Re: Filemanager - Override a file

by Helson C -

Davo,

$draftitemid = file_get_submitted_draft_itemid('attachments'); 

ever return 0;


In reply to Helson C

Re: Filemanager - Override a file

by Helson C -

I think that is the problem, but i dont know how solve....

In reply to Helson C

Re: Filemanager - Override a file

by Helson C -

Davo, thanks for your help!

I found the issue!

file_prepare_draft_area(...,...);

$default_values->attachments = $draftitemid;

In reply to Helson C

Re: Filemanager - Override a file

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

Yes - that would be the problem.

You may want to consider using file_prepare_standard_filemanager and file_postupdate_standard_filemanager in future - https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms

(If you look inside the code for file_prepare_standard_filemanger, you will see it wraps the exact steps you were doing).

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

Re: Filemanager - Override a file

by Helson C -

Ok in future i'll try;


Thanks for your help.

In reply to Davo Smith

Re: Filemanager - Override a file

by Helson C -

Davo, when this method is used to retrieve file from draft:

file_prepare_draft_area($draftitemid, $context->id, 'block_myblock, 'attachment', $itemid, $filemanageropts);


After few days the draft will cleared from db when con running and the files can not be recovered to filemanager?

Or I don't need be afraid ?

In reply to Helson C

Re: Filemanager - Override a file

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

That function does not retrieve a file from draft.

That function copies any existing files from the 'real' file area into the draft file area.

Once the form is submitted, file_save_draft_area_files() will copy the files out of the draft area and back into the 'real' file area (ok, it is slightly more complex than that - files that have been deleted by the user in the form, will be deleted, files that have been updated will be updated and files that are unchanged are left unchanged; and, when you dig right down into the code, the 'file' itself on the server is not affected, it is the file record that points to it that is updated).

So, it is fine for the draft area to be cleared - any files you care about will have been saved into the 'real' area, long before the draft area is wiped.


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

Re: Filemanager - Override a file

by Helson C -

Good answer!

Then, when the form is openned, the real file is aways copied to draf and the user aways will see the file to edit!


Thanks for you great explanation.