Filemanager in block - files dissapear from form when moodle delete old draft files

Filemanager in block - files dissapear from form when moodle delete old draft files

by Kamil Łuczak -
Number of replies: 1
Picture of Plugin developers

When editing block setting after deleting draft files by cron, added images to block dissapear from filemanager fields.

Files are displaying correctly in block and in edit form until cron delete old draft files.


Code in edit_form.php:

class block_slider_edit_form extends block_edit_form {

protected function specific_definition($mform) {

for($i = 1; $i <= 10; $i++) {

$mform->addElement('filemanager', 'config_slide'.$i, get_string('images', 'block_slider', $i), null, array('subdirs' => 0, 'maxbytes' => 5000000, 'maxfiles' => 1, 'accepted_types' => array('.png', '.jpg', '.gif') ));

}
}

function set_data($defaults) {

if (empty($entry->id)) {
$entry = new stdClass;
$entry->id = null;
}

//loop for 10 filemanagers
for($i = 1; $i <= 10; $i++) {
$slide = 'slide'.$i;
$config_slide = 'config_slide'.$i;
$draftitemid = file_get_submitted_draft_itemid('config_slide'.$i);

file_prepare_draft_area($draftitemid, $this->block->context->id, 'local_filemanager', $config_slide, 0, array());

$entry->{$slide} = $draftitemid; //this dont work?

//file_prepare_standard_filemanager($entry, $config_slide, array(), $this->block->context->id, 'block_slider', $config_slide, 0);

parent::set_data($defaults);

if ($data = parent::get_data()) {
file_save_draft_area_files($data->{$config_slide}, $this->block->context->id, 'block_slider', $config_slide, 0, array('subdirs' => false));
}
}
}
}
Running cron:
Deleting old draft files... ... started 14:48:58. Current memory use 2.7MB. done. Deleting orphaned preview files... ... started 14:48:58. Current memory use 3MB. done. 
Form is empty:
Empty form after deleting draft files
But file is stored and can be displayed with:
for($i = 1; $i <= 10; $i++) {
$slide = 'config_slide'.$i;
$config_slide = 'config_slide'.$i;
$files = $fs->get_area_files($this->context->id, 'block_slider', $config_slide, 0);

 (...)

Cant find more information how its done in documentation  ( https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms )
Average of ratings: -
In reply to Kamil Łuczak

Odp: Filemanager in block - files dissapear from form when moodle delete old draft files

by Kamil Łuczak -
Picture of Plugin developers

I have rewritten block edit_form  set_data()  code:

/* data prepare and store */
	function set_data($defaults) {

		if (empty($defaults->id)) {
	    	$defaults = new stdClass;
	    	$defaults->id = null;
		}

		for($i = 1; $i <= 10; $i++) {
			$attachments = 'config_file'.$i;
						
			$draftitemid = file_get_submitted_draft_itemid($attachments);
			
			file_prepare_draft_area($draftitemid, $this->block->context->id, 'block_slider', $attachments, $defaults->id, array('subdirs' => false, 'maxbytes' => 50000000, 'maxfiles' => 1, 'accepted_types' => array('.png', '.jpg', '.gif')));
			
			$defaults->{$attachments} = $draftitemid; //I think thats the problem
		}

		parent::set_data($defaults);
		
		if ($data = parent::get_data()) {
    		// ... store or update $entry
			for($i = 1; $i <= 10; $i++) {
				$attachments = 'config_file'.$i;
				$draftitemid = file_get_submitted_draft_itemid($attachments);
				file_save_draft_area_files($data->{$attachments}, $this->block->context->id, 'block_slider', $attachments, $defaults->id, array('subdirs' => false, 'maxbytes' => 5000000, 'maxfiles' => 1, 'accepted_types' => array('.png', '.jpg', '.gif')));
			}
		} 
	}

But it still dont display files in filemanager on edit form (if it was deleted after 4 days). 

Files draft entries are created from stored files in database everytime I refresh block edit form.



to reproduce problem I've commented part od line 2168 in /lib/filestorage/file_storage.php:

$old = time() ;//- 60*60*24*4;

and wrote a script that runs filestorage cron (attached draftdel.php)

Version of Moodle im working on - 2.8.6+ (Build: 20150528)