General developer forum

pluginfile not using my library function: Moodle 3.3+

 
Picture of Dave Emsley
pluginfile not using my library function: Moodle 3.3+
Particularly helpful Moodlers
Hi there,

I've created my own module called - presentationupload and followed, as far as possible, the moodledocs documentation.  The files are now uploading to moodledata and all seems fine there. 

However is there any reason my view.php might not be using my own module's lib.php file.
The code:

        $url = moodle_url::make_pluginfile_url(
        $result->contextid,    //$file->get_contextid(),
        $result->component,    //$file->get_component(),
        $result->filearea,    // $file->get_filearea(),
        $result->itemid,    //$file->get_itemid(),
        $result->filepath,    //$file->get_filepath(),
        $result->filename
        );
        echo "<br />URL is ".$url;
        $out = html_writer::empty_tag('img', array('src'=> $url));
echo $out;

writes the URL on the screen but doesn't show the graphic file that is stored - the broken image file appears. 

I've stuck some echo statements into the library function which don't come up.

Attached them both.

Any help gratefully received.

Cheers

Dave


 
Average of ratings: -
Davo
Re: pluginfile not using my library function: Moodle 3.3+
Core developersParticularly helpful MoodlersPlugin developers

The first thing I'd mention is that the moment you are doing direct DB queries against the 'files' table, alarm bells should be ringing because that is not something you should ever need to do.

If you are wanting to access a file, you should be using the files API:

$context = context_module::instance($cm->id);
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'mod_presentationupload', 'presentationupload');
foreach ($files as $file) {
...


Next, you've got an 'echo' at the top of your presentatioupload_pluginfile function - that is a very bad idea, as that function has the job of outputting the content of the image. Outputting some text before your output the image content will cause the image to always be broken.

If you're trying to debug the file serving, then make sure xdebug + your IDE are properly configured and stick a breakpoint at the top of pluginfile.php - you should only need to trace down a couple of layers before you find out what is going wrong.


 
Average of ratings: Useful (1)
Picture of Dave Emsley
Re: pluginfile not using my library function: Moodle 3.3+
Particularly helpful Moodlers

Thanks for that Davo.
Much of the additional code was out of pure frustration and me not really getting what was/should be going on.

I've changed it all back to the standard code from the API.  Still not working though.  I usually use DreamWeaver to write the code and upload but I'm going to have to get a better IDE.

Cheers for your help, (again)

Dave



 
Average of ratings: -
Davo
Re: pluginfile not using my library function: Moodle 3.3+
Core developersParticularly helpful MoodlersPlugin developers

Just to emphasise again - the time it takes to configure xdebug and an IDE that supports it will be recouped within a day. I can only speak for debugging with PHPStorm, but once xdebug was installed on my local server and 'xdebug helper' was added to Chrome, I just had to click 'start listening for debug connections' in the IDE and I was able to set breakpoints and step through my code.


 
Average of ratings: Useful (1)
Picture of Dave Emsley
Re: pluginfile not using my library function: Moodle 3.3+
Particularly helpful Moodlers

Thanks for that Davo, 

Lots of playing installing XDebug and PHPStorm (and other IDEs) has left me little nearer a solution and still a lot confused.  All Xdebug seems to be reporting is errors in jquery

XDebug is reporting:


( ! ) Warning: Invalid argument supplied for foreach() in /home/learndom/public_html/moodle/lib/pear/HTML/QuickForm.php on line 1279
Call Stack
#TimeMemoryFunctionLocation
10.0001378352{main}egg.../modedit.php:0
20.18135923008add_moduleinfoegg.../modedit.php:154
30.19435966408presentationupload_add_instanceegg.../modlib.php:124
40.19515966472moodleform->get_file_contentegg.../lib.php:22
50.19515966472MoodleQuickForm->exportValuesegg.../formslib.php:919
60.19525966984HTML_QuickForm::arrayMergeegg.../formslib.php:2024

   As far as I can see from my code this is the line:

$content = $mform->get_file_content('userfile');
which is taken directly from the documentation so should be right?


Cheers
Dave


 
Average of ratings: -
Picture of Dave Emsley
Re: pluginfile not using my library function: Moodle 3.3+
Particularly helpful Moodlers

Hi Davo,

I can definitely see how the time I've spent on installing and getting my head round the debugger instead of manual breakpoints and dumping variables I have been using.

However in this case it was me not getting my context correct.   I was using context_course instead of context_module.  No wonder the debugger wasn't actually showing me a lot.

Anyway many thanks again for your kind support.


Best Regards

Dave

 
Average of ratings: -