help with moodle file API

help with moodle file API

by Patrick Juhl -
Number of replies: 5

Hello moodlers,

In my attempt to integrate PaintWeb (and here) in moodle I have stumbled upon a little obstacle when trying to handle files.

I use the following code:

$file_record = new object();
$file_record->contextid = $context->id; //5
$file_record->component = $component; //user
$file_record->filearea  = $filearea; //user_draft
$file_record->itemid    = $draftitemid; //randomly generated number
$file_record->filepath  = $filepath; // /paintweb/
$file_record->filename  = $filename; //radomlygenerated name.ext
$file_record->userid    = $USER->id; //2

try {
    $file = $fs->create_file_from_string($file_record, $imgdata);
} catch (Exception $err) {
    paintweb_send_result($imgurl, $imgurlnew, false, $err->getMessage());
}

$binfo = $fbrowser->get_file_info($context, $component, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
if (empty($binfo)) {
    paintweb_send_result($imgurl, $imgurlnew, false,get_string('moodleServer:saveFailed', 'paintweb'));
}

$imgurlnew = $binfo->get_url();

if (empty($imgurlnew)) {
    paintweb_send_result($imgurl, $imgurlnew, false,
        get_string('moodleServer:saveFailed', 'paintweb'));
}

Apparently, the file is saved on the first try but for some reason, the fileinfo cannot be fetched and the binfo variable remains empty. If I try to save again, an exception is caught which I suspect is caused by an attempt to overide the existing file. I imagine I should detect that before hand and treat it accordingly. For the first case though, when the file is saved, I'd like to understand why the file info cannot be retrieved...

Any idea ?

Thanks in advance!

 

Patrick Juhl

Average of ratings: -
In reply to Patrick Juhl

Re: help with moodle file API

by Dan Poltawski -

Hi Patrick,

As mentioned via email, apologies as I have been meaning to reply to this for some time. I am not sure of the problem without checking (right now).

However, just glancing at this, I wonder:

1) if the component is user, then the context needs to be user context - is that what you are setting it to?

2) The random itemid - is that consistently random?

In reply to Dan Poltawski

Re: help with moodle file API

by Patrick Juhl -

Hi Dan,

Thanks for the answer. Here's the code I use for the context:

// Files saved by PaintWeb are stored in the draft area.
$filearea = 'user_draft';
$component = 'user';
$filepath = '/';
$context = get_context_instance(CONTEXT_USER, $USER->id);
$contextid = optional_param('contextid', SITEID, PARAM_INT);

Now maybe I should use a specific component for this application. For instance: 'paintweb_mod'

In that case, can I use the user context ?

Thanks again for the help!

 

Patrick Juhl

In reply to Patrick Juhl

Re: help with moodle file API

by Patrick Juhl -

Well actually, I found part of my mistake. The file area should have been 'draft' and not 'user_draft'. Because in the get_file_info process, a method is called based on the component and the filearea.

In file_info_context_user.php:


$methodname = "get_area_{$component}_{$filearea}";

if (method_exists($this, $methodname)) {

And the method to be called is get_area_user_draft.

This gets the code a little further. I actually have a successful save message! Now it still fails on something else afterwards so I'll continue my investigations.

Cheers,

 

Patrick

 

 

In reply to Patrick Juhl

Re: help with moodle file API

by Patrick Juhl -

Ok, this works pretty well so far. To the next problem now:

when I use this code:

   $binfo = $fbrowser->get_file_info($context, $component, $filearea,$draftitemid, $filepath, $filename);

$imgurlnew = $binfo->get_url();

I get in return an URL of the kind:

http://localhost/moodle/pluginfile.php/5/user/draft/539215088/file.png

But if I use the file picker to select this file, which conveniently appear in the recent files tab, it has this URL:

http://localhost/moodle/draftfile.php/5/user/draft/539215088/file.png

why is it that the URL retrieved by the get_file_info is wrong ?

Thanks in advance for any help out of this...

 

Patrick