Uploading Files as a resource: Moodle 3.3+

Uploading Files as a resource: Moodle 3.3+

by Dave Emsley -
Number of replies: 2

I've created a Moodle module that uploads a file... or is supposed to.

I've tried to follow the documentation at https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#Load_existing_files_into_draft_area

I get the error messages:

Warning
: Invalid argument supplied for foreach() in /home/learndom/public_html/moodle/lib/pear/HTML/QuickForm.php on line 1279

Warning: Invalid argument supplied for foreach() in /home/learndom/public_html/moodle/lib/pear/HTML/QuickForm.php on line 1279


I'm assuming this error comes from my mod_form.php file but that makes no sense as I've followed the documentation that exists and it contains:

        $maxbytes = 10485760;
        $mform->addElement('filemanager',
            'userfile',
            get_string('file', 'presentationupload'),
            null,
            array('subdirs' => 0,
                'maxbytes' => $maxbytes ,
                'areamaxbytes' => 10485760,
                'maxfiles' => 1,
                'accepted_types' => array('zip'),
                'return_types'=> FILE_INTERNAL));


Second Problem:

I seem to have correctly uploaded the file to the draft area as mdl_files contains a record with the filename in it.

However I am at a loss as to how to make the section which says to save the files https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#Store_updated_set_of_files: 

Store updated set of files

if ($data = $mform->get_data()) {
    // ... store or update $entry
    file_save_draft_area_files($data->attachments, $context->id, 'mod_glossary', 'attachment',
                   $entry->id, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50));
}

If someone could provide a clue here I would be grateful.

Cheers

Dave




Average of ratings: -
In reply to Dave Emsley

Re: Uploading Files as a resource: Moodle 3.3+

by Dave Emsley -

After more fishing about, and a bit of trial and error my module's lib.php contains the following:

function presentationupload_add_instance($currentpresentation) {
...

    $id = $DB->insert_record("presentationupload", $currentpresentation);
...
...
    if (empty($entry->id)) {
        $entry = new stdClass;
        $entry->id = $id;
    }
    $maxbytes = 10485760;
    $draftitemid = file_get_submitted_draft_itemid('userfile');
    file_prepare_draft_area($draftitemid, $context->id, 'mod_presentationupload', 'presentation', $entry->id, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50));
    $entry->attachments = $draftitemid;
    $mform->set_data($entry);

$data = $mform->get_data();
file_save_draft_area_files($data->userfile, $context->id, 'mod_presentationupload', 'presentation', $entry->id, array('subdirs' => 0, 'maxbytes' => $maxbytes, 'maxfiles' => 50));

    return $id;   
}


I hope this helps others suffering the same issues.


Cheers

Dave




In reply to Dave Emsley

Re: Uploading Files as a resource: Moodle 3.3+

by Dave Emsley -

As I have now uploaded files what I think is successfully I cannot retreive them.

The document https://docs.moodle.org/dev/File_API#Serving_files_to_users says to use:

$url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
Which gives me the URL: 192.168.1.40/~learndom/moodle/pluginfile.php/33/mod_presentationupload/presentation/47/Slide3.JPG which to me looks plausible given the entry in the mdl_files table is:


350 891e79ff764313389e1a0e9b3e242a637ad81dc6 d221f4033ea671012b19b132310850a372eb3979 33 mod_presentationupload presentation 47 / Slide3.JPG 2 66383 image/jpeg

Sorry can someone shine a light on this as I'm stumped, again.

Cheers

Dave