HowTo backup and restore an image in a block

HowTo backup and restore an image in a block

by Urs Hunkler -
Number of replies: 7
Picture of Core developers

A block displays an image. The block image is set with the filemanager.

How do I backup and restore the image with the course backup. I have searched the Moodle documentation for block backup, for the file API, the backup API etc. but found no description how to backup and restore images/files in blocks. I didn't find Moodle core blocks that handle file/image backup and restore and I didn't find other blocks that backup and restore images.

When I backup and restore a course with this image block no image is shown after the restore. When I open the block settings the filemanager shows the image which has been chosen before the course has been backuped and restored. When I save this image again it is shown again. The »itemid« of the image is saved in the block »configdata« and restored with the course restore. Is there a way to create a new file entry for this image? There should be a possibility to write the image information to the database during the block restore or after restore. 

Please can somebody explain how to backup and restore an image in a block? 

Thank you very much for your help.

Average of ratings: -
In reply to Urs Hunkler

Re: HowTo backup and restore an image in a block

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I don't know, but I think that block_html handles this.

So, all you need to do is understand https://github.com/moodle/moodle/blob/master/blocks/html/backup/moodle2/restore_html_block_task.class.php wink

In reply to Tim Hunt

Re: HowTo backup and restore an image in a block

by Urs Hunkler -
Picture of Core developers

Thank you very much for your fast answer Tim. That would have been an easy one wink

I tried to use the backup/restore functions from the »HTML« block but without success. I changed the block name and the referenced field name in the code. The situation may be different because in the »HTML« block image references in a text are manipulated during restore. I only have the image reference number for the »files« table entry saved in the block configdata.

Maybe some post processing may be needed? Or a new image reference in the »files« table needs to be added with the changed context id?

In reply to Urs Hunkler

Re: HowTo backup and restore an image in a block

by Urs Hunkler -
Picture of Core developers

I found a way to solve the task.

In the »after_restore« function I read the block instance from the database and then call the  »file_save_draft_area_files« function with the same parameters as in the block's »instance_config_save« function where the image reference is created. The only difference is that after the restore the new context id from the restored block is used.

In reply to Urs Hunkler

Re: HowTo backup and restore an image in a block

by Urs Hunkler -
Picture of Core developers

Is this approach the right way to solve the task - or are there better ways?

In reply to Urs Hunkler

Re: HowTo backup and restore an image in a block

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I still don't really understand this, so treat the following with care.

I don't understand why calling file_save_draft_area_files would work. Restore does not put files in the draft area.

Actually, I have a guess why it is working. I think old draft areas are only garbage-collected every 5 days (or something like that). So, my guess is that your code will work if you test the backup and restore less than 5 days after you created or edited the block, but otherwise will fail.

...

No. I still don't understand. I don't see how you can "call the  »file_save_draft_area_files« function with the same parameters as in the block's »instance_config_save« function". Isn't one of those parameters draft item id? Won't that only be available at the moment when the instance config form is submitted.


Is the code of your block in github? It might be easier to help if we could see all the code.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: HowTo backup and restore an image in a block

by Urs Hunkler -
Picture of Core developers

You are completely right Tim - what I did was nonsense. I tried to get started by reading the docs and looking at some code. Both lead to not understanding the inner workings and to writing strange code that seamed to work after a lot of trail and error. Thanks for your offer to dig deeper into my issue. 

I took the time to better understand Moodle file handling and file backup/restore. The result is easy as to declare the »filearea« where the image gets stored in the »get_fileareas« function in the backup and restore task classes of the block and in the functions dealing with the image. Moodle handles the file backup/restore then automatically.

I hope I got the image handling right this time.

In reply to Urs Hunkler

Re: HowTo backup and restore an image in a block

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

That sound like the right thing. (Though I don't know why I am bothering to comment. You now seem to know more about this than me.) Anyway, thanks for sharing the solution.