importing MoodleXML questions with images as answers

importing MoodleXML questions with images as answers

Massimiliano Del Gatto tomonidan -
Number of replies: 2

Hi, everyone.

recently I migrated from Moolde 1.9.6 to Moodle 3.0 (I probably should say it was a jump). 

With this lastest version I am no longer able to create Moodle XML comliant questions with images as answers and audiofiles as question text because I can no loger use an absolute URL to point to the location where images and audiofiles are stored. In Moodle 3.0 file management is database maintained, almost all image files are converted into a base64 format and their storage is unintelligible for the human mind.

There is no option for me to create manually the question inside moodle, because I already have a database of questions amounting to 4 thousand questions and counting, besides all questions without images were imported into moodle using my Moodle XML creator engine with only minor or no change at all. The only problem I bumped into is when importing questions with images and audiofiles.

My question database is setup as follows:

question database snapshot

Each record (each question) has 4 related files: one audiofile and 3 imagefiles whose folder-names and file-names are created on-the-fly on the basis of the information pertaining to the question and match and file with the audio for the question (Qtext) and those with the graphics for the answer (Option1, Option2 and Option3) and the physical folder/s where the files are stored.

Since in Moodle 1.9.7 all course files where stored in a folder with the name of the course-ID inside the moodledata directory, It was easy to upload the course files and manage them. Therefore after uploading the necessary files for the questions, in order to make sure I would feed Moodle an XML that it would like, I would set up a question inside moodle (i.e.manually), export it and then reverse-engineer it to fit my needs, upload the necessary files into the proper folders and then importing the modified XML back into Moodle with almost no sweat at all. I could successfully set up these kind of questions because I could point to the audio and image files using relative/absolute URLs.

With the change in the file system management of Moodle 3.0 whenever I select a resource to be used anywhere in a course, Moodle creates a database record to manage the selected resource,rather than storing the relative/absolute URL to the resource.

Is there a workaround to this file system management of Moodle 3.0 so that I could use relative/absolute URLs pointing to to the audio and image files instead of the database managed media and the base64 encoding?

I've tried several different on-line search in the last couple of days, but, unfortunately, with no luck!

I really hope that someone could help, because it's about time I started testing my students...

Thank you very much for your kind support.

Happy new year,

Massimiliano

O'rtacha reytinglar: -
In reply to Massimiliano Del Gatto

Re: importing MoodleXML questions with images as answers

Tim Hunt tomonidan -
Core developers rasmi Documentation writers rasmi Particularly helpful Moodlers rasmi Peer reviewers rasmi Plugin developers rasmi

Moodle's file management systems is not new in Moodle 3.0. It has been like that since Moodle 2.0 (5 years ago) and it is not going to change now. So, you will just have to deal with it.

However, if you got things working generating Moodle XML files from you question database with Moodle 1.9, then I am sure you will be able to do the same with Moodle 3.0.

Although you would not want to write base-64 encoded data by hand, it is quite easy with any scripting language. Here is the code where Moodle exports files:

$string .= '<file name="' . $file->get_filename() . '" path="' . $file->get_filepath() . '" encoding="base64">';
$string .= base64_encode($file->get_content());
$string .= "</file>\n";

https://github.com/moodle/moodle/blob/master/question/format/xml/format.php#L1120

So, create one question with images in Moodle 3.0 using the Moodle UI, then export it as Moodle XML, then you will see what you have to generate to make the import work.

In reply to Massimiliano Del Gatto

Re: importing MoodleXML questions with images as answers

Eoin Campbell tomonidan -
Core developers rasmi Particularly helpful Moodlers rasmi Plugin developers rasmi

You can convert your images to base64 quite easily, and then write a script to replace the image references with the Base64 versions directly in your Moodle XML file. For example, if your answer is of the form:

<answer fraction="100">
<text><![CDATA[<p><img src="PET.LS.P01.E01.Q02_a.jpg"/></p>]]></text>
</answer>

then you could replace it with the Base64 data, as follows:

<answer fraction="100">
<text><![CDATA[<p><img src="data:image/jpg;base64,iVBORw0KGgoAAAANSUh..."/></p>]]></text>
</answer>

There is a YouTube tutorial that might help you do this here: