Import question item with image in Moodle XML format

Import question item with image in Moodle XML format

by Komal Barde -
Number of replies: 6

Hi All,

I have to import question items  to question bank, which contains image in question text.

I am trying to use <img> tag but it is not adding image to question text.

Any help will be much appreciated.

Thanks.


Average of ratings: -
In reply to Komal Barde

Re: Import question item with image in Moodle XML format

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

  1. Create a question containing an image in Moodle.
  2. Export that question as Moodle XML.
  3. Look at the contents of that file.
Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Import question item with image in Moodle XML format

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

To add to Tims answer, images are stored as base64, so you will need a way of saving your image to that format.

In reply to Marcus Green

Re: Import question item with image in Moodle XML format

by Komal Barde -

Hi,

Tim and Marcus Thanks for your reply.

Export of  question with image in Moodle XML format  takes following form:

<img src="filename.jpg" alt="" width="1024" height="768">

<file name="Penguins.jpg" path="/" encoding="base64">

---encoding of image file

</file>

I am trying to add image from my local machine with base64 encoding

<text><![CDATA[Write as essay on given image.<p><img src="komal.jpg"><br></p>]]></text>
<file name="komal.jpg" path="/" encoding="base64">

---encoding of image file

</file>

But it is displaying image as undefined image(See attachment).

Could you please tell me where I am going wrong?


Thanks.


Attachment error.png
In reply to Komal Barde

Re: Import question item with image in Moodle XML format

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Which version of Moodle are you using? If you paste images into the question fields and then view the HTML you should see something like <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAAC/CAIAAAD2LUA0AAAgAElEQVR4nHS995Mj15XnWz bla bla bla


Which is how base64 appears.

I had a quick look around for tools that would export as Base64 and didn't find any.

In reply to Marcus Green

Re: Import question item with image in Moodle XML format

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

This has nothing to do with pasting.

In reply to Komal Barde

Re: Import question item with image in Moodle XML format

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

The most likely explanation is that the base64 enocding of the image was wrong. Look at the code in question/format/xml to see how Moodle generates it and decodes it.

Anyway, you are going to have to do some debugging to work out why your file is not importing correctly. At what point along the process of parsing the file, then saving the data in the database, is the image getting lost?