How to save filemanager input and display it to student and teacher/manager

Working example of filemanager usage

by Anna Heynkes -
Number of replies: 0

Here's a commented excerpt of my working code. I hope you'll find it useful.

(If you're writing your own plugin, you need to write a function called myplugin_pluginfile() for serving files to users. See:

https://docs.moodle.org/dev/File_API#Serving_files_to_users

)

// Form settings
$context = $this->page->context;
$contextid = $context->id;
$component= 'mod_scheduler';
$filearea = 'attachment';
$itemid = 0;
$options = array('subdirs' => 0, 'maxbytes' => 0, 'areamaxbytes' => 10485760, 'maxfiles' => 50,
                              'accepted_types' => array('image', 'document', 'application/pdf', 'application/zip', 'presentation',
                              'application/vnd.openxmlformats-officedocument.presentationml.template'), 'return_types'=> FILE_INTERNAL |
                              FILE_EXTERNAL);       
// Form data
$data = new stdClass();
$schedulerid = $booker->actionurl->get_param('id');
$data->id = $schedulerid;
       
// Load any preexisting files into the draftarea
file_prepare_standard_filemanager($data, 'attachment', $options, $context, $component, $filearea, $itemid);

// Initialise mform, pass on $data and $contextid
$mform_studentcomment = new scheduler_studentcomment_form($booker->actionurl, '');
$mform_studentcomment->set_data($data);
$mform_studentcomment->set_data($contextid);
     
// Check wether the mform has just been cancelled or submitted or is to be displayed
      
//If cancelled: reload page
if ($mform_studentcomment->is_cancelled()) {
    redirect($booker->actionurl);
}
       
// If submitted: get files from filemanager
if ($data = $mform_studentcomment->get_data()) {
           
    // Get the draft itemid (Files in the drag-and-drop area are automatically saved as drafts in mdl_files even before the form is submitted.)
    $itemid = file_get_submitted_draft_itemid('attachment');
           
    if ($itemid != null) {       
        $data->attachment_filemanager = $itemid;
       
        // Save draftarea content permanently
        $data = file_postupdate_standard_filemanager($data, 'attachment', $options, $context, $component, 'studentcomment',
                                                                                            $itemid);     
        // Access the content
        $fs = get_file_storage();
        $files = $fs->get_area_files($contextid, 'mod_scheduler', 'studentcomment', $itemid, $sort = "itemid, filepath, filename",
                                                        $includedirs = true, $updatedsince = 0);  
        // Create an access url for each file and save all of them in an array
        if ($files != null) {
            $url = array();
            foreach($files as $file){
                $tmp = moodle_url::make_pluginfile_url(
                        $file->get_contextid(),
                        $file->get_component(),
                        $file->get_filearea(),  // studentcomment
                        $file->get_itemid(),  
                        $file->get_filepath(),
                        $file->get_filename(),
                        false // $forcedownload
                        );
                $url[]=$tmp;
            }
            // Save the array here ...
        }  
        // reload page
        redirect($booker->actionurl);
    }
}
// If the form was neither cancelled nor submitted: Display it       
$mform_studentcomment->display();