General developer forum

Storing a file in moodle

 
Picture of Iraklis D
Storing a file in moodle
 

Hi everybody,

I am currently working on an activity module, where the creator should be able to upload a pdf (or select an already existing pdf, which is already stored in moodle). There are also some other kind of information submitted within the same form as the file. I already figured out how to store the information except the file.

in the mod_form.php I already added the filepicker element, but somehow i am unable to save and store the the pdf  in my add_instance function (inside lib.php). As far as i understood, there are two ways to store a file in moodle: 1. Store it by yourself within the activity module, or 2. let the file api of moodle manage and save the submitted file. I would like to do the second thing, since I want also offer the opportunity to select an already existing file within this lecture/module and would like to keep all the pdfs reusable within other plugins.

So i already read the files api documentation and checked some other activity modules, but somehow I am unable to figure out how to properly add this file. I also had a briefly look over the resource mod files, but again without success.

Could someone share an concrete example of how to store a pdf with moodles file api (or anything else that would fit my requirements)?  

I am fairly new to moodle plugin development and might be on the wrong track here, so any help is appreciated.

thanks in advance! 


 
Average of ratings: -
Picture of Robert Payne
Re: Storing a file in moodle
 

Hi Iraklis,

I just came here to ask almost the same thing! Currently I have code that catches the file picker's response and I think it stores it but I'm not sure because I can't get the file again when I go looking for it.

Here's my code so far:

$data = $add_file->get_data();
$contextmodule = context_module::instance($cm->id);
if ($draftitemid = file_get_submitted_draft_itemid('attachmentedfile')) {
      file_save_draft_area_files($draftitemid, $contextmodule->id, 'mod_mediaassignment', 'attachmentedfile', 0, array('subdirs' => false, 'maxfiles' => 1));
}
$content = $add_file->get_file_content('attachmentedfile'); $name = $add_file->get_new_filename('attachmentedfile');
print_object($add_file->get_file_content('attachmentedfile'));
$success = $add_file->save_file('attachmentedfile', 'submission', false);
$url = moodle_url::make_pluginfile_url($contextmodule->id, 'mod_mediaassignment', 'submission', $data->attachmentedfile, null, $name);

The save_file() function doesn't seem to return anything and the make_plugin_url() produces a url that isn't giving me a file so I'm clearly doing things wrong. 

$content is returning the data from inside the file and $name is showing the file name so I've know I've got it good up until then.

 
Average of ratings: -