Filemanager in edit form (block)

Filemanager in edit form (block)

by alberto lara -
Number of replies: 6

I added a filemanager in edit form for block setting.

if a user uploads files, these are only visible to the user. 

I wonder if it is possible that uploaded files are visible for all the users who edit the block. (for example, to managers).

Thanks.

Average of ratings: -
In reply to alberto lara

Re: Filemanager in edit form (block)

by Susana L. -

I have exactly the same problem. I couldn't find a single example using filemanager in a block that is correct... Even found a block, submitted to Moodle plugins directory, with this same problem.

The problem is that: The files uploaded by a user are only visible to this same user.

I followed the example here: https://github.com/AndyNormore/filemanager (very helpful) and tryed to adapt it to a block with no success (on the edit by different users part). The problem seems to be loading the files to the new user draftarea.

edit_form.php
function set_data($defaults) {
  ....
  $itemid = 0;
  $draftitemid = file_get_submitted_draft_itemid('config_attachments');
  file_prepare_draft_area($draftitemid, $context->id, 'block_partnerships', 'content', $itemid, $filemanageropts);
  $defaults->config_attachments = $draftitemid;
  parent::set_data($defaults);
}
The $draftitemid gets the new generated number and the draft files (and real files) are created in the mdl_files table correctly (i think). I could not understand why files added by a user are not loaded to other users draftarea... Shouldn't this be accomplished by:
$defaults->config_attachments = $draftitemid;

What am I missing? Some help here... please smile
Thank you,
susana
In reply to Susana L.

Re: Filemanager in edit form (block)

by Susana L. -

Found another block submitted to moodle Plugins directory with this same problem!...

In reply to Susana L.

Re: Filemanager in edit form (block)

by Simon Vart -

Hi ! 

I have the same issue that i'm trying to resolve (meaning, having a block, manageable by different users, that have uploadable images in the block configuration with the help of a filemanager). 

Using the validation() function, I am able to save my images in the Moodle filesystem, and get them back on frontend with the renderer.

However, if another user try to change the block configuration, the images are missing in the form.

I'm investigating, but I would appreciate any help.


Regards!

In reply to Simon Vart

Re: Filemanager in edit form (block)

by Simon Vart -

Found it.

It misses a piece of logic. Let me explain.


In the child function, you get defaults values. Specific values (from the specific_form) are populated when calling the parent function.

The parent function overwrite any key/value from the block config to the defaults values.

So, if you want to prepare a draft area then attribute the value to the filemanager, you could do that:

for ($i = 1; $i <= $defaultvalues['numslides']; $i += 1) {
if (property_exists($this->block->config, 'slide' . $i . '_file') && $this->block->config->{'slide' . $i . '_file'}) {
$draftitemid = file_get_submitted_draft_itemid('slide_file');
file_prepare_draft_area($draftitemid, $this->block->context->id, 'block_orange_benefits', 'slide_file', $i);
$this->block->config->{'slide' . $i . '_file'} = $draftitemid;
}
}

parent::set_data($defaults);

In my case, i'm using a single filearea with several itemid. The code would be simplier for a unique filemanager with a unique itemid (0).

The trick is to rewrites the value in the block config - or set the values on $defaults but unset the key in the block config. You could have the value in any, but not in both $this-block-config and `$defaults` !

In reply to Simon Vart

Re: Filemanager in edit form (block)

by Simon Vart -

(How come that a developer oriented forum doesn't managed the pasted code properly ?)

In reply to Susana L.

Re: Filemanager in edit form (block)

by Simon Vart -
the missing part would be to add : unset($this->block->config->attachments); 


The other option is to do instead of setting default : $this->block->config->attachments = $draftitemid;

Average of ratings: Useful (2)