How to get images of question and options . I am creating custom web-services.

How to get images of question and options . I am creating custom web-services.

by Baiju Sharma -
Number of replies: 6

I am able to get all the required data, but facing issue getting images path.I need to also convert the images in base64_encode.  

Path is stored as <img src="@@pluginfile@@/koala.jpg">. I know there is a function file_rewrite_pluginfile_urls to do this, but I am unable to understand these required  parameter. What will be my these parameter $contextid, $component, $filearea, $itemid in this case.


Average of ratings: -
In reply to Baiju Sharma

Re: How to get images of question and options . I am creating custom web-services.

by Baiju Sharma -

            $text = "<p><img src='https://moodle.org/pluginfile.php/114/mod_forum/post/1432254/baijuabcd.jpg' alt=''/></p>";

            $file = "pluginfile.php";

            $component = "local_quiz";  

            $dataid = 41406;  

            $filearea = "draft";

            $itemid = 44750; 

            $contextid = 598;

            $str = file_rewrite_pluginfile_urls($text, $file, $contextid, $component, $filearea, $itemid);

            echo $str;

I am getting below string.

pluginfile.php/598/local_quiz/draft/44750/baijuabcd.jpg

In reply to Baiju Sharma

Re: How to get images of question and options . I am creating custom web-services.

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

All of those items $contextid, $component, $filearea, $itemid, depend on the exact place you are using file_rewrite_pluginfile_urls (otherwise, you wouldn't need to provide them each time).

Ultimately, you'll need to trace through the code that outputs the questions in core code in order to understand what values you need to use here (questions are one of the more complex use-cases for this, so this may take a while to get your head around it).


In reply to Davo Smith

Re: How to get images of question and options . I am creating custom web-services.

by Baiju Sharma -

$text = "<p><img src='https://moodle.org/pluginfile.php/114/mod_forum/post/1432482/baijuabcd.jpg' alt=''/></p>";  

            $file = "draftfile.php"; // Is this correct or I have to use pluginfile.php

            $component = "user";   //if activity: database

            $filearea = "draft";

            $itemid = 824469588; /

            $contextid = 598; 


Can I directly specify the path 

Below is the Url which I am getting.

$str= http://onlinedrillingcourse.com/draftfile.php/598/user/draft/388210897/baijuabcd.jpg....when I run this url it allows me to download images, but when I call it 

$im=file_get_contents("$str");

      $base64 = base64_encode($im);

       echo $base64;

It encode and give me home page.Please help

In reply to Baiju Sharma

Re: How to get images of question and options . I am creating custom web-services.

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

user/draft is only relevant to when a user is editing something in a form. Files are temporarily copied to a user draft area while things are being edited. Then, if the user cancels the form, the draft area is descarded, and if the user saves, the files are copied back to the real file area.

None of htat is relevant to displyaing questions to users.

I suggest you look at how the Moodel mobile app handles this. They have solved all these problems. https://github.com/moodlehq/moodlemobile2

In reply to Tim Hunt

Re: How to get images of question and options . I am creating custom web-services.

by Baiju Sharma -

Thank you for a quick reply.

For I am able to get all question and option text, My requirement is whenever there is an image in question and images in options. It needs to be displayed with it.

I will check the link which you have suggested.


In reply to Tim Hunt

Re: How to get images of question and options . I am creating custom web-services.

by Baiju Sharma -

Hi, I am using this File API for reading images stored path inside OUTPUT:"/home/onlin200/moodledata32/filedir/ce/2b/ce2b454fcdf9601f75065cd794df46d20006b932"

Requirement: Read data from the above file path covert this data into base_encode and send data to web services.

Just want to know, is it possible to read all data from "ce2b454fcdf9601f75065cd794df46d20006b932" This is an image file.

Below is my code:

 $filename  = "baijuabcd.jpg";

        $component = "user"; //if activity: database

        $filearea  = "draft";

        $itemid    = 824469588;// ID from table mdl_question (for activity: database) - row in the table where the above $text is stored


        if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, '/', $filename)) {

            $level  = $fileinfo->get_parent();

            $params = $fileinfo->get_params();

            $fs     = get_file_storage();            

            $file = $fs->get_file($params['contextid'], $params['component'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename']);    

            if ($file) {

                $contenthash = $file->get_contenthash();

                $l1          = $contenthash[0] . $contenthash[1];

                $l2          = $contenthash[2] . $contenthash[3];

                $location    = $CFG->dataroot . '/filedir' . '/' . $l1 . '/' . $l2 . '/' . $contenthash;

                echo $location;            

            }

OUTPUT:"/home/onlin200/moodledata32/filedir/ce/2b/ce2b454fcdf9601f75065cd794df46d20006b932"

Kindly, help.