1

I have added custom column to store company logo. I have used file api of moodle like :

$mform->addElement('filepicker', 'certificatelogo', 'Company Logo', null,
               array('maxbytes' => $maxbytes, 'accepted_types' => '*'));
$mform->setDefault('certificatelogo', '0');
$mform->addHelpButton('certificatelogo', 'certificatelogo', 'certificate');

Once the form is submitted itemid will be stored in custom column. Say "648557354"

Now I need to get image to print logo on certificate. How can I get image path from itemid? Do I need to store any other information to retrieve image?

stefun
  • 1,261
  • 3
  • 24
  • 54

1 Answers1

3

The itemid returned is the temporary id of the draft area where the file is stored whilst the form is being displayed. You need to copy the file into its 'real' location, when the form is submitted, otherwise the file will be automatically deleted after a few days (and it will only be accessible to the user who originally uploaded it).

I'd always recommend using the filemanager element, if you are planning on keeping the file around (filepicker elements are for files you want to process and discard, such as when uploading a CSV file data to parse and add to the database).

Details of how to use it are here: https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#filemanager

But the basic steps are:

  1. Copy any existing files from the 'real' area to the draft area (file_prepare_standard_filemanager).
  2. Display the form.
  3. On submission, copy files from the draft area to the 'real' area (file_postupdate_standard_filemanager).
  4. When you want to display the file to the user, get a list of files stored in the file area (defined by the component, filearea, context and, optionally, itemid, you used in file_prepare_standard_filemanager and file_postupdate_standard_filemanager). You can do this with: $fs = get_file_storage(); $fs->get_area_files().
  5. For those files (maybe only 1 file, in your case), generate the URL with moodle_url::make_pluginfile_url.
  6. Make sure your plugin has a PLUGINNAME_pluginfile() function in lib.php, to examine incoming file requests, do security checks on them, then serve the file.

There is a reasonable example of all of this at: https://github.com/AndyNormore/filemanager

davosmith
  • 6,037
  • 2
  • 14
  • 23