General developer forum

Best way to debug pluginfile errors in editor field of form

 
Richard Jones
Best way to debug pluginfile errors in editor field of form
Particularly helpful MoodlersPlugin developersTesters

Hi

I'm struggling mightily with a file not found error for an image in my editor.  On the instance settings I have an editor field which saves images correctly.  When the view.php file is displayed I see:

<img src="http://192.168.1.120/moodle/pluginfile.php/53/mod_simplelesson/firstpage/1/faq.jpg" alt="" width="200" height="200" role="presentation" class="img-responsive atto_image_button_text-bottom">

Wonderful.

However, when I use a different page (edit_page.php) and file area (declared in get_file_areas), the image is in the editor but not when I move to show the page (showpage.php).  It looks like this:

<img src="http://192.168.1.120/moodle/pluginfile.php/53/mod_simplelesson/pages/1/moodle-partner-CSP-horizontal-small-trans-gray%20copy.png" alt="" width="462" height="83" role="presentation" class="img-responsive atto_image_button_text-bottom">

But there is a 404 not found error and no image is displayed.  I'm using pretty much the same process in both cases (as from https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#Simple_use).  Incidentally I don't see mod/glossary/comment.php (real world example given).

What is the most likely error here and/or what is the best way to track it down?

Is there perhaps a difference in processing or callbacks for a form that extends moodleform_mod and for one that just extends moodleform?

Thanks if you can advise.


 
Average of ratings: -
Picture of Darko Miletić
Re: Best way to debug pluginfile errors in editor field of form
Core developersParticularly helpful Moodlers

We already had similar discussion(s). As a result of one of those I created a minimal example of  a plugin that does what you are trying to do.

https://github.com/kiklop74/moodle-local_fooplug


 
Average of ratings: Useful (1)
Richard Jones
Re: Best way to debug pluginfile errors in editor field of form
Particularly helpful MoodlersPlugin developersTesters

Thank you for your quick response Darko, sorry if I'm missing the point - is the code you referenced a diagnostic tool for examining file areas?  It just seems to enable loading and displaying of an image.

I'm not generally having trouble getting pluginfiles to work in editor areas, the instance settings form has an additional editor area, similar to mod_page and that's fine.

It's just this one case where I'm using a Moodle form, which is not part of the instance settings, and the same techniques are working to the extent that they create the @@PLUGINFILE@@ code in the editor field in the database and the re-write plugin method produces what seems to be a valid URL, but the URL results in a 404 not found.  The file is also present in the user draftfile area as it reappears when the edit form is shown again.


Thanks again.
Richard 
 
Average of ratings: -
Davo
Re: Best way to debug pluginfile errors in editor field of form
Core developersParticularly helpful MoodlersPlugin developers

My first port of call whenever I have a problem serving saved files is to stick a breakpoint in pluginfile.php, then step through to see what is happening.

Of course, before you can do that, you need to set up xdebug on your server (usually just install the library + add a couple of lines to php.ini), configure your IDE to use xdebug (PHPStorm pretty much does this out of the box, other IDEs may need more or less configuration) then turn on debugging (xdebug helper for Chrome helps here). Time spent setting up a debugger will be paid back within a day or two.

 
Average of ratings: Useful (1)
Richard Jones
Re: Best way to debug pluginfile errors in editor field of form
Particularly helpful MoodlersPlugin developersTesters

Thanks Davo

I have Eclipse (but usually just use it for browsing code).  I tried to set up xdebug, was a bit of a mission, but I suppose I will have to at some point.


 
Average of ratings: -
Picture of Darko Miletić
Re: Best way to debug pluginfile errors in editor field of form
Core developersParticularly helpful Moodlers

I misunderstood your initial post thinking that you might need an example. Will take a look at the code in question and get back to you.

 
Average of ratings: -
Richard Jones
Re: Best way to debug pluginfile errors in editor field of form
Particularly helpful MoodlersPlugin developersTesters

Much appreciated.

 
Average of ratings: -
Picture of Darko Miletić
Re: Best way to debug pluginfile errors in editor field of form
Core developersParticularly helpful Moodlers

Since you have two file areas could it be that in one case you are storing file in the wrong file area? I suggest to check the actual file table and see what you have there. 


There is nothing obviously wrong in your code so the error is something small smile. I would recommend you to pass the code through style checker since there are quite a few stylistic errors.

Given that you already use github writing a travis script could be the easiest way for this.

 
Average of ratings: Useful (1)
Richard Jones
Re: Best way to debug pluginfile errors in editor field of form
Particularly helpful MoodlersPlugin developersTesters

Hi Darko

Thank you for the advice.  I will try your suggestions.

Richard

 
Average of ratings: -
Picture of Marcus Green
Re: Best way to debug pluginfile errors in editor field of form
Core developersParticularly helpful MoodlersPlugin developersTesters

Excuse me if I am stating the obvious, but when writing code that stores files it is a very good idea to keep some kind of database query available, e.g. select * from mdl_files order by id desc.  I like to use something like mysql workbench of phpmyadmin to do that. It has helped massively when writing file writing code.

 
Average of ratings: Useful (2)
Richard Jones
Re: Best way to debug pluginfile errors in editor field of form
Particularly helpful MoodlersPlugin developersTesters

Thanks for the suggestion Marcus.

 
Average of ratings: -
Richard Jones
Re: Best way to debug pluginfile errors in editor field of form
Particularly helpful MoodlersPlugin developersTesters

Just reporting back (maybe others have the same issue):

The problem (tracked down by a combination of the helpful advice above) was the image itemid included in the path.  This needed to be the id of the page that had the image (when re-writing the pluginfile) and not the module id that I was previously using.

big grin


 
Average of ratings: -