How to save file with the file manager in mod form ?

How to save file with the file manager in mod form ?

by Demir Agovic -
Number of replies: 10

Hi. I am added file manager with mfrom on mod_form.php page. But when I upload file and save, after refresh page file manager is still empty.

Code is here: 

$mform->addElement('filemanager', 'finalassignment_file', get_string('coursecontent', 'mod_finalassignment'), null,
array('subdirs' => 0, 'maxbytes' => 65536, 'areamaxbytes' => 10485760, 'maxfiles' => 1,
'accepted_types' => '*', 'return_types'=> FILE_INTERNAL | FILE_EXTERNAL));
How I can fix this, and after that how I can call this file on activity page ?

Average of ratings: -
In reply to Demir Agovic

Re: How to save file with the file manager in mod form ?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Have you followed all the steps in the Moodle docs: https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms ?

Particularly pay attention to the use of the functions:
  • file_prepare_standard_filemanager()
  • file_postupdate_standard_filemanager()
  • MYPLUGIN_pluginfile() in your plugin's lib.php
Average of ratings: Useful (1)
In reply to Davo Smith

Re: How to save file with the file manager in mod form ?

by Demir Agovic -
I follow instruction from API FIles moodle and I have error: Call to undefined method MoodleQuickForm::set_data()

There is my mod form:

class mod_finalassignment_mod_form extends moodleform_mod {

/**
* Defines forms elements
*/
public function definition() {
global $CFG, $USER, $COURSE;

$mform = $this->_form;
$params = $this->_customdata;
$context = \context_course::instance($COURSE->id);

// Adding the "general" fieldset, where all the common settings are shown.
$mform->addElement('header', 'general', get_string('general', 'form'));

// Adding the standard "name" field.
$mform->addElement('text', 'name', get_string('finalassignmentname', 'finalassignment'), array('size' => '64'));

if (!empty($CFG->formatstringstriptags)) {
$mform->setType('name', PARAM_TEXT);
} else {
$mform->setType('name', PARAM_CLEANHTML);
}

$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');

// Adding the standard "intro" and "introformat" fields.
if ($CFG->branch >= 29) {
$this->standard_intro_elements();
} else {
$this->add_intro_editor();
}

$mform->addElement('filemanager', 'finalassignment_file', get_string('coursecontent', 'mod_finalassignment'), null,
array('subdirs' => 0, 'maxbytes' => $COURSE->maxbytes, 'areamaxbytes' => 10485760, 'maxfiles' => 50,
'accepted_types' => array('document'), 'return_types'=> FILE_INTERNAL | FILE_EXTERNAL));

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

$draftitemid = file_get_submitted_draft_itemid('finalassignment');

file_prepare_draft_area($draftitemid, $context->id, 'mod_glossary', 'finalassignment_file', $entry->id,
array('subdirs' => 0, 'maxbytes' => $COURSE->maxbytes, 'maxfiles' => 50));

$entry->attachments = $draftitemid;

$mform->set_data($entry);

$mform->addElement('duration', 'finalassignment_timelimit', get_string('timelimit', 'mod_finalassignment'));

$options = array(
'1' => 'On hole course',
'2' => 'On section'
);

$mform->addElement('select', 'finalassignment_select', get_string('select_options', 'mod_finalassignment'), $options);

// Add standard elements.
$this->standard_coursemodule_elements();

// Add standard buttons.
$this->add_action_buttons();
}
}
In reply to Demir Agovic

Re: How to save file with the file manager in mod form ?

by Demir Agovic -
Version moodle: 3.6.4+ (Build: 20190519)'
In reply to Demir Agovic

Re: How to save file with the file manager in mod form ?

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

As I suggested previously, it is easier to use the function file_prepare_standard_filemanager(), rather than directly calling file_get_submitted_draft_itemid() and file_prepare_draft_area(), but those do still work (it is what file_prepare_standard_filemanager() does internally).

The issue you've got here is that you're calling all these functions inside the form definition - these are not related to the elements that the form has, but related to the data to initialise those elements with.

I see that you are creating a main activity instance form, so you need to use the data_preprocessing() function in the form to initialise the draft area - see mod/resource/mod_form.php for an example.

In reply to Davo Smith

Re: How to save file with the file manager in mod form ?

by Demir Agovic -
I found similar code with file_prepare_standard_filemanager() function in mod/assign/feedback/file/batchuploadfilesform.php.
I try to implement same cod in my activity but my filemanager box is still empty after upload and save changes. In database the column mdl_files, always writes the last file data after save.

$data = new stdClass();
$fileoptions = array('subdirs'=>1,
'maxbytes'=>$COURSE->maxbytes,
'accepted_types'=>'*',
'return_types'=>FILE_INTERNAL);

$data = file_prepare_standard_filemanager($data,
'files',
$fileoptions,
$params['context'],
'mod_finalassignment',
'finalassignment_files_batch', $USER->id);

$mform->addElement('filemanager', 'finalassignment_attachment', '', null, $fileoptions);

$this->set_data($data);
In reply to Demir Agovic

Re: How to save file with the file manager in mod form ?

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

What code are you using to save the filemanager data after the form has been submitted?

The "prepare" function can only copy files from the "real" file area into the "draft" file area if they were saved from the "draft" area into the "real" file area when the form was saved.

In reply to Davo Smith

Re: How to save file with the file manager in mod form ?

by Demir Agovic -
Code for form:
$data = new stdClass();
$options = array('subdirs' => 0, 'maxbytes' => $COURSE->maxbytes, 'areamaxbytes' => 10485760, 'maxfiles' => 50,
'accepted_types' => array('document'), 'return_types'=> FILE_INTERNAL | FILE_EXTERNAL);

$data = file_prepare_standard_filemanager($data,
'finalassignmentattachments',
$options,
$params['context'],
'mod_finalassignment',
'finalassignmentattachments', $USER->id);
$this->set_data($data);

$mform->addElement('filemanager', 'finalassignmentattachments', get_string('coursecontent', 'mod_finalassignment'), null,
$options);

code for save file in lib.php:

function finalassignment_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
// Check the contextlevel is as expected - if your plugin is a block, this becomes CONTEXT_BLOCK, etc.
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}

// Make sure the filearea is one of those used by the plugin.
if ($filearea !== 'finalassignmentattachments') {
return false;
}

// Make sure the user is logged in and has access to the module (plugins that are not course modules should leave out the 'cm' part).
require_login($course, true, $cm);

// Check the relevant capabilities - these may vary depending on the filearea being accessed.
if (!has_capability('mod/finalassignment:view', $context)) {
return false;
}

// Leave this line out if you set the itemid to null in make_pluginfile_url (set $itemid to 0 instead).
$itemid = array_shift($args); // The first item in the $args array.

// Use the itemid to retrieve any relevant data records and perform any security checks to see if the
// user really does have access to the file in question.

// Extract the filename / filepath from the $args array.
$relativepath = implode('/', $args);

$fullpath = "/{$context->id}/mod_finalassignment/$filearea/$itemid/$relativepath";

// Retrieve the file from the Files API.
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}

// We can now send the file back to the browser - in this case with a cache lifetime of 1 day and no filtering.
send_stored_file($file, 0, 0, $forcedownload, $options);
}

I added finalassignment_pluginfile() in lib.php, and nothing happen, also when I set debug point in this function, debuger never read this function...

Can you tell me please all steps for create input file with filemanager in mod_form.php for activity. I was read documentation but I still don't create this...
In reply to Demir Agovic

Re: How to save file with the file manager in mod form ?

by Demir Agovic -
I fix it. I forgot to add code for save files in lib (add and update instace).
In reply to Demir Agovic

Re: How to save file with the file manager in mod form ?

by Fabian Goldie -

Hi Demir, what was the code you added?

Average of ratings: Useful (1)
In reply to Demir Agovic

Re: How to save file with the file manager in mod form ?

by Fasih Zafar -
Hi Demir, can you share what you wrote for add and update instance