Image not display in newly created plug-in against new file area.

Image not display in newly created plug-in against new file area.

by Pradnya Nale -
Number of replies: 5

Hi,

We have created new plug-in in block called odevmail and did the following steps to insert data in DB

1. Add Form element

 $mform->addElement('editor','mail_message_editor',get_string('message','block_odevmail'),null,$editoroptions);
 $mform->setType('mail_message_editor', PARAM_RAW);  

2. Saved data using following code

$newcategory = new stdClass();
$newcategory->textfield = '';
$data->mail_message_format = 1;             
$newcategory = file_postupdate_standard_editor($data, 'mail_message', $editoroptions, $context, 'course', 'mail_message',$newinsert_main);
$DB->set_field('block_odevmail_main','message',$newcategory ->mail_message,array('id'=>$newinsert_main));

Inserted values in all table like block table and mdl_files table against the block

But if we want to display image using following code image is not display

$options     = array('noclean' => true, 'overflowdiv' => true);
$context     = context_course::instance(1);
$text         = file_rewrite_pluginfile_urls($receive_main->message, 'pluginfile.php',$context->id, 'course', 'mail_message', $receive_main->id);
$course_image = format_text($text,1, $options);


Any one having idea,where did go wrong?

Average of ratings: -
In reply to Pradnya Nale

Re: Image not display in newly created plug-in against new file area.

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You need to implement a method like odevmail_pluginfile in mod/odevmail/lib.php (look at the equvialent for mod_forum to see an example). That needs to do the necessary permissions checks to verify that the current user is allowed to see this image now.

(Can I just check, do you have Debugging set to DEVELOPER level while you are doing development? I would hope that if that necessary method was missing, Moodle would output a debugging message to tell you what the problem is, rather than just silenetly failing.)

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Image not display in newly created plug-in against new file area.

by Pradnya Nale -

Hi,

But i have created block then how should i create mod folder?

I have write this method in block/odevmail/lib.php

But still getting error.

The method is as follows

function block_odevmail_pluginfile($course, $birecord, $context, $filearea, $args, $forcedownload){
        global $CFG, $DB;
       
        $fileareas = array('attachment', 'mail_message');
        if (!in_array($filearea, $fileareas)) {
            return false;
        }
        $messageid = (int)array_shift($args);       
        $fs = get_file_storage();
        $relativepath = implode('/', $args);
        $fullpath = "/$context->id/block_odevmail/$filearea/$messageid/$relativepath";
        if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
            return false;
        }
       
        // finally send the file
        send_stored_file($file, 0, 0, true); // download MUST be forced - security!
    }

In reply to Pradnya Nale

Re: Image not display in newly created plug-in against new file area.

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You don't specify what $context you were using when saving the file data - but I'd guess it was a block context.

When you call file_rewrite_pluginfile_urls you use the course context for course 1 (really, you should use $SITE->id, if you want the id of the front page course, but 1 will probably work in most cases).

You also set the file area to be one called 'course'.

In your ..._pluginfile function you check to see if the filearea is either 'attachment' or 'mail_message' - which it will never be, as you set the file area to 'course'.

The rest of the function will probably work (but with some slightly strange variable names, that don't seem to match the meaning you assigned to them in the other functions).

If this doesn't help, I'd strongly recommend you install xdebug + use an IDE to insert breakpoints and step through the code. If you can't do that, then insert some 'echo' or 'var_dump' lines into your ..._pluginfile function to see where it is stopping.

In reply to Davo Smith

Re: Image not display in newly created plug-in against new file area.

by Pradnya Nale -

Hello Davo,

I am agree with you, but i am able to insert value in DB in mdl_files  and when trying to display that it looks like

and after copy and paste link it shows the following error.



In reply to Pradnya Nale

Re: Image not display in newly created plug-in against new file area.

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

OK, so you agree with me - but have you addressed any of the issues I suggested in my post?

Have you checked the inconsistent use of $context?

Have you tried using xdebug or inserting extra echo/var_dump lines into your function to see where it is going wrong?

Actually, taking another look at your code - you have set the 'component' field to 'course' in your code - that means that Moodle will be checking in the course code to see if the file is valid, so it will never go anywhere near your plugin.

You need to set the component to 'block_odevmail' if you want to serve the file back from your plugin.