Images stored in moodledata

Images stored in moodledata

by Sir Leo -
Number of replies: 5

I'd like to know what means this image path:

http://localhost/moodle/pluginfile.php/14/question/questiontext/0/1120/1120/Logo%20downtuga%20bmp.bmp

 

I created a new question and upload the image "Logo downtuga bmp.bmp".

Moodle store the image in $CGF->moodledata/filedir\79\73\797341a26754add65bdeae16aec5541ac43b00f2

In my case is C:\moodledata\filedir\79\73\797341a26754add65bdeae16aec5541ac43b00f2

 

So what is the relation between "Logo downtuga bmp.bmp" and the location stored?

I create un plugin that import my own question, but there are associated images and i'd like using the same structure of moodle to keep portability.

When i import my own question, I send un ZIP file with associated images and questions, what i can know what function i need to call and what is the returned image?

I've tried in:

C:\xampp\htdocs\moodle\draftfile.php

C:\xampp\htdocs\moodle\pluginfile.php

But i can't understand the relation between the image and the location stored.

 

Would appreciate any help,

Thanks

Average of ratings: -
In reply to Sir Leo

Re: Images stored in moodledata

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 should only access moodledata/filedir using the File API. You should not try to touch the files directly.

For question import/export it is quite easy, becaues all the hard work has already been done for the Moodle XML import (in particular, look at the unit tests for the import). Have a look at the code there.

Basically, in Moodle 1.9 the import had to generate an object with fields like $qo->questiontext = "<p>Here is some question text.</p>".

In Moodle 2.0 you need to do something similar, but a bit more complex:

$qo->questiontext = array(
'text' => "<p>Here is some question text.</p>",
'format' => FORMAT_HTML,
'files' => array(/* ... you need to put the right structure here ... */),
);

If you really want to understand the link between the files on disc, and your image URL, look in the files table in the database, and read http://docs.moodle.org/dev/File_API.

In reply to Tim Hunt

Re: Images stored in moodledata

by Sir Leo -

Yes, i have this structure, i can import the content of question but  without the images.

My doubt is, i have my question like

" This is my question and i have this image <image> C:\Windows\Temp\example.gif </image>"

 

I know the image path, what is necessary to pass to FILE API? and what is the function?

In Moodle XML import, there is (line 175)

I need something like this code to replace my path by correct image src which is generated by FILE API.

When you said "'files' => array(/* ... you need to put the right structure here ... */)," where i can say these structure?

 

Would appreciate any help,

Thanks

 

 

In reply to Sir Leo

Re: Images stored in moodledata

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 bit of example code you have chosen to look as is not the clearest. Try this bit instead:

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

and

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

The  array structure is taken apart and processed here:

https://github.com/moodle/moodle/blob/master/question/type/questiontypebase.php#L1131

So, to explain the

'files' => array(/* ... you need to put the right structure here ... */),

bit. The files array is an array of objects. Each object represents one file. One of these objects has fields:

->name - the filename. If the name is 'path/name.png' then the following HTML fragment will link to it: <img src="@PLUGINFILE@/path/name.png" />

->encoding - says how the ->content field should be interpreted. Currently, the only supported value is 'base64'. (We should probably add 'raw' to that list.)

->content - the contents of the file. In your case, where the file comes from a zip file, you will have to do something like $file->contents = base64_encode(file_get_contents($wherethefileisnow)).

In reply to Tim Hunt

Re: Images stored in moodledata

by Sir Leo -

I will show un example, it is easier to explain..

where i had

<image> C:\Windows\Temp\function2.gif </image>

i have

i can't inserted this code manually because the first line change to <img src=  alt=""/>

<file name='function2.gif' encoding='base64'>'.$this->encode_img("C:/Windows/Temp/function2.gif").'</file>

 

this is only replaced inside of string..

and before i have this part to store the image in moodledata using FILE API

 

 

(i tested this function and it is all correct)

i followed your indications?

 

When i import the image appear this:

 

 

when i get the "images" url it is:

http://localhost/moodle/pluginfile.php/14/question/questiontext/0/1305/1305/function2.gif

http://localhost/moodle/question/

 

But there is no images stored in moodledata.. why?

 

I only use the import files functions in answers!

$question->correctfeedback = array('text' => '', 'format' => FORMAT_MOODLE,

'files' => array('text' => $q->answer_correct, 'format' => FORMAT_MOODLE, 'files' => $this->import_files($q->body)),);;

when i try use in question i have this error

Fatal error: Out of memory (allocated 1904214016) (tried to allocate 1239 bytes) in C:\xampp\htdocs\moodle\question\format\dmat\format.php on line 189

 


I increase the memory_limit  in php.ini but the error continues..

 

 

Would appreciate any help,

Thanks

In reply to Sir Leo

Re: Images stored in moodledata

by Sir Leo -

Someone have any idea?

I really need to know this..

 

Thanks