Moodle 2: image in question answer problem, pls help ASAP

Moodle 2: image in question answer problem, pls help ASAP

by Alaa Elkholy -
Number of replies: 36

Hi all,

I am using Moodle 2. When I create an MCQ with image in the answer, only me can see the image in the answer in the question preview. Other users see the answer blank.

This problem does not happen when inserting image in the question text.

 

I really appreciate your help

Al

Average of ratings: -
In reply to Alaa Elkholy

Re: Moodle 2: image in question answer problem, pls help ASAP

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 sounds like a bug. Are you using the very latest version if Moodle 2.0. If you are please file this bug at http://tracker.moodle.org/ and I will take a look.

In reply to Tim Hunt

Re: Moodle 2: image in question answer problem, pls help ASAP

by Alaa Elkholy -

Thanks Tim,

I was desperate to get an answer, thanks for your rely.

I am using Moodle 2.0 RC1. I got this build more than week ago. Is there a difference if I get Moodle 2.0 RC1 the latest build?

If not then I will report this as a bug.

Please let me know.

In reply to Alaa Elkholy

Re: Moodle 2: image in question answer problem, pls help ASAP

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Al,

Could you please describe the exact steps you take to insert an image in your MCQ question answer. Most important, where do you upload and retrieve the image in your moodle files?

Joseph

In reply to Joseph Rézeau

Re: Moodle 2: image in question answer problem, pls help ASAP

by Alaa Elkholy -

Tim,

here are the steps I follow:

1- insert the required image into the answer feedback section.

2- display the HTML code for the inserted image

3- copy the code for the image starting from <img src= ............. >

4- paste this cosde into the answer text box

5-  delete the image from the feedback section

I tried to upload the image from "upload file" repository and from "file system" repository. Both ways resulted in same problem

I really appreciate your help.

Al

 

In reply to Alaa Elkholy

Re: Moodle 2: image in question answer problem, pls help ASAP

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

Ah, that is the old Moodle 1.x way of doing things, and copying and pasting image links like that will not work in Moodle 2.0.

I thought that in the process of converting the question types to the new file API, the 'answer' boxes had been replaced by HTML editor elements so you could upload files there. If that has not been done, it is a bug, and needs to be reported in the bug tracker, so I remember to fix it.

(And yes, I know, that will make an already very long form even more ridiculously long. There is not a good answer to that right now.)

In reply to Tim Hunt

Re: Moodle 2: image in question answer problem, pls help ASAP

by Alaa Elkholy -

Tim,

Please correct me if I am wrong.

You mean that in Moodle 2.0 answer boxes should have HTML editor?

Moodle 2.0 does not have this option now?

How long you think it will take to be implemented?

What is the alternative for the time being?

 

Regards

Al

In reply to Alaa Elkholy

Re: Moodle 2: image in question answer problem, pls help ASAP

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

Yes, I mean that.

I don't know exactly what the state of play is in Moodle 2.0. I have been bee busy with OU-specific stuff for the last few weeks. I will catch up with Moodle 2.0 stuff soon.

If this is not already done, it should be really easy to implement - but I am liable to forget if no one reports it in the bug tracker.

There is no alternative until it is done.

In reply to Tim Hunt

Re: Moodle 2: image in question answer problem, pls help ASAP

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
In reply to Joseph Rézeau

Re: Moodle 2: image in question answer problem, pls help ASAP

by Alaa Elkholy -

Thanks Tim and Joseph.

When you expect this feature to be implemented?
I will plan my work accordingly.

 

Regards

Al

In reply to Joseph Rézeau

Re: Moodle 2: image in question answer problem, pls help ASAP

by Alaa Elkholy -

Joseph and Tim,

How long you think it will take to have this feature implemented.

Also when you guys expect to have Moodle 2.0 final relaese?

 

regards

Al

In reply to Alaa Elkholy

Re: Moodle 2: image in question answer problem, pls help ASAP

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

Two terribly good questions. I'm afraid that there is not a better answer than 'as soon as possible' right now.

In reply to Tim Hunt

Re: Moodle 2: image in question answer problem, pls help ASAP

by Alaa Elkholy -

Hi,

I have an urgent project that needs this feature. So I need to know how soon this feature will be available. Or, I may roll back to Moodle 1.9 to use this feature instead? Regards and appreciate your quick reply

Al

In reply to Alaa Elkholy

Re: Moodle 2: image in question answer problem, pls help ASAP

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

Sorry, I can't make any promises about this. If you really need this feature, you will have to go back to 1.9.

(That is a really sucky answer isn't it. I really wish I could give you a better one.)

In reply to Tim Hunt

Re: Moodle 2: image in question answer problem, pls help ASAP

by Peter Friesen -

I've been working on rewriting a 1.9 backup file to 2.0, and this part of the shift has proven to be pretty flummoxing.

Is the mapping of the files settled? Would you be able to anticipate, even before the incorporation of the editor,  what the contextid, component, and filearea values should map to?

That is to say, if the answertext were HTML, presumably, we'd end up with the answertextformat element set to 1 and the answertext itself looking something like this:

<answertext>&lt;img width="100" height="107" alt="" src="https://moodle.org/pluginfile.php/134/mod_forum/post/726945/nose.jpg" /&gt;Nose. &lt;em&gt;.</answertext>

But where would the pictures be mapped in the files.xml? I've been looking into the options but none seems to work. I expect I'd need something like

<contextid>68</contextid>
<component>qtype_multichoice [or questiontext or subquestion]</component>
<filearea>answertext</filearea>

But, not having an example to work with, I've no idea what the component or filearea values should be, much less whether these are already being parsed for the questions. And I'm a bit baffled too by whether the contextid would in this circumstance reach to the course, the category, or answer for its value.

If you've got suggestions, I'd be happy to hear them: if these details are all still in flux, I'd be happy to hear that too...

Peter

In reply to Tim Hunt

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

imgeHi Tim

I have modifed the edit_multichoice_form and the questiontype functions so that I can put an image in the multichoice answers.

However when previewing the question, the image is only visible after a submit but not in the initial state (even if the element is not chosen).

It seems to be controlled by some style settings ( probably related to feedback) so as you have redone  all the CSS, where should I look to obtain that the images can be seen in the initial state.

Pierre

In reply to Tim Hunt

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

Tim,

I just find the quiz_rewrite_question_urls() function but I don't know how to set it so that the image is always visible.

Pierre

In reply to Tim Hunt

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

More readings in question_check_file_access() processes mean that your help is more necessary ...wink

Pierre

In reply to Pierre Pichet

Re: Moodle 2: image in question answer problem, pls help ASAP

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

Is your changed code visible anywhere? It would be easier to comment if I could see the code.

In reply to Tim Hunt

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

I did not changed any setting in css or in multichoice/display.html.

The &&plugin&& is correctly converted in all situations (the image appears then disappears in the initial display (preview or quiz) .

After a submit then everything is dispalyed correctly.

A similar image put in the question text is displayed correctly in all cases.

So there seems to be something (could be related to the option->feedback set after a submit) that either control the display part of the answer (so css related) or the control of the loading of images.

the url conversion for questiontext is done in type/questiontype.php/function print_question()

$question->questiontext = quiz_rewrite_question_urls($question->questiontext, 'pluginfile.php', $context->id, 'question', 'questiontext', array($state->attempt, $state->question), $question->id);

the url conversion for answer is done in multichoice/questiontype.php/function print_question_formulation_and_controls()

$a->text = quiz_rewrite_question_urls($answer->answer, 'pluginfile.php', $context->id, 'question', 'answer',array( $state->attempt ,$state->question ), $answer->id);

 

I will set a tracker issue on adding image to multichoice answers to continue this quite technical  discussion.

Pierre

In reply to Pierre Pichet

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

A further test shows that this is a very tricky working of the access control to files.

Using the http://demo.moodle.net/ site, I create a multichoice question and I put in the answer field two image code obtained using  the feedback editor or the source option from firefox.

This give

<img src="http://demo.moodle.net/pluginfile.php/2/question/answerfeedback/0/955/1929/ivee" alt="ivv" width="200" height="150" /> <img src="http://demo.moodle.net/draftfile.php/89/user/draft/25098959/ivee" alt="ivv" width="200" height="150" />


When I preview the question the first image did not display but the second one display correctly.

However after a submit the 2 images were displayed as if the file display control was off.

initial

init2

after submit

submit2

 

Is this a normal feature for questions ?

Pierre

In reply to Pierre Pichet

Re: Moodle 2: image in question answer problem, pls help ASAP

by Adriane Boyd -

I'm not an expert, but I just dealt a lot with files to update my question types.  I'm pretty sure all the image URLs in a question preview or quiz should be of the pluginfile.php variety.  I think draftfile.php is only used on the editing pages to display the files within the editors and should never show up in a question preview.

Because of how the new files stuff works, you also can't copy the URLs that include "draftfile.php" between HTML editors because that URL is a temporary URL only intended to work in the particular area where you added it using the HTML editor image dialog.  You can see the "question/answerfeedback" part of the first URL, which indicates that it's only happy as part of the answer feedback and won't have the right permissions to be displayed as part of an answer.

I'm not totally sure about exactly what's going wrong here in terms of before/after submit (though I suspect you're just seeing the image as part of the feedback) or why you can still see the draftfiles.php image in the question preview at all, but I'm sure that this is not the right approach.  You need to end up with pluginfile.php/XX/question/answer/XX/XX/XX/image.gif URLs that have been generated by a new answer HTML editor that has been added to the edit page.

I am also sure that the way some of the new file stuff has been implemented makes it very hard to debug and I do not envy you in your exploration of this part of the code!

In reply to Adriane Boyd

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

Hi Adriane,

I thank you for your concerns that I could want to hack the multiplechoice/answer as we could do in 1.9, but this is not the case.wink

I am upgrading multiple choice so that they can include imaqe in the answers using new file API.

At the current stage of the project, I can edit answers using HTML editor and save the image as they are saved for answer->feedback (with @@plug-in@@ convention).

As in the answer-> feedback containing image, the answer images are already converted using the complete address i.e

"http://132.208.141.198/moodle_head_new/pluginfile.php/239/question/answer/0/955/1929/iver"

before sending as HTML output.

Everything work fine with the exception of initial display either in preview or quiz.

Apart from the question text, the images in answer are the first image elements to be shown in initial question display.

This is why I have done hacking on http://demo.moodle.net/ to understand what is happening if I use different image files and more specifically those already stored in answer->feedback to exclude possible bug from my code.

So far the problem is not on my side smile.

 

Pierre

In reply to Pierre Pichet

Re: Moodle 2: image in question answer problem, pls help ASAP

by Adriane Boyd -

Just checking, but what did you add to check_file_access in multichoice/questiontype.php?

I have this and it seems to work:

} else if ($component == 'question' && $filearea == 'answer') {
return (array_key_exists($itemid, $question->options->answers));

I just spent a little while playing with adding images to answers and I managed to get images in the answers, but the editing page isn't working yet if you edit an existing question.

If your URL conversion is right, I also have no idea what would be causing the images to only be displayed part of the time.  If you're always in the preview context, the image URL is always the same, right?

On the editing page, the validation and other issues are causing me problems.  I've ended up with multiple choice questions with tons of blank answers and the editing page is also now slower than molasses...

In reply to Adriane Boyd

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

Thanks,

I have use  exactly the same solutionwink

Pierre

In reply to Adriane Boyd

Re: Moodle 2: image in question answer problem, pls help ASAP

by Mike Holzer -

I think this is related to if not the same problem I just reported: http://tracker.moodle.org/browse/MDL-25932

 

Glad I'm not the only person seeing this issue.

In reply to Mike Holzer

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

Petr Skoda pinpoint another problem in file saving so that the image file can be retrieved as part of the lesson in your case.

This is not the case here.

Pierre

In reply to Adriane Boyd

Re: Moodle 2: image in question answer problem, pls help ASAP

by Jiri Princ -

Hi,

please, have you found any solution for this problem? I have very same problem in 2.0.1 and I am not sure what I can do from the posts here and ore other forums.

Even any workaround would definitely help me.

Thank you for the answer.

Regards

Jiri

In reply to Pierre Pichet

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

This could perhaps be related to

if ($attemptid === 0) {
// preview
require_once($CFG->dirroot . '/question/previewlib.php');
return question_preview_question_pluginfile($course, $context,
$component, $filearea, $attemptid, $questionid, $args, $forcedownload);

} else {
$module = $DB->get_field('question_attempts', 'modulename',
array('id' => $attemptid));

$dir = get_component_directory($module);
if (!file_exists("$dir/lib.php")) {
send_file_not_found();
}
include_once("$dir/lib.php");

$filefunction = $module . '_question_pluginfile';
if (!function_exists($filefunction)) {
send_file_not_found();
}

in questionlib/function question_pluginfile($course, $context, $component, $filearea, $args, $forcedownload) {

because when I test a similar feedback image put in an answer field, the file was not displayed in the initial preview  but was displayed in the initial view when used in a quiz where the attempt field was not 0 even if the file in the answer field as the following definition

http://demo.moodle.net/pluginfile.php/239/question/answerfeedback/0/955/1929/iver

 

Pierre

In reply to Tim Hunt

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

I have done a further test.

I output the answers also just after the questiontext (i.e in the same div) and I got the same results i.e. when in the initial condition the answer images do not display either just after the question text or at their regular place.

On a submit however the answer images display correctly at the 2 places after a submit. In all cases the conversion is done correctly.

This excludes css effect but there could be some changes in the url conversion options between the type/questiontype.php/function print_question() and the multichoice/questiontype.php/function print_question_formulation_and_controls().

I will explore the options settings and come back.

Pierre

here the initial display

initialand submitsubmit

In reply to Tim Hunt

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

Searching, searching and I finally pinpoint the function check_file_access($question, $state, $options, $contextid, $component,)

HOURRAH

big grin

big grin

big grin

hourrah

 

Learning is always a difficult process...

Pierre

In reply to Pierre Pichet

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

OK, The answer images were not displayed in the initial preview because they where not allowed to do so because of the function check_file_access().

However as they were displayed after a submit, must we conclude that there is no test to answer images using  function check_file_access() after a submit ?

If so, this means that there are no check_file_access() to any files after a submit including all the feedbacks that are not accessed in the initial display.

Houston,  have we a problem ?

Pierre

In reply to Pierre Pichet

Re: Moodle 2: image in question answer problem, pls help ASAP

by Adriane Boyd -

Take a closer look at check_file_access() for multichoice.

The "question/answerfeedback" images have a check that means that they only have permission to be viewed if feedback is going be displayed, which is only after a submit.  This explains the weird behavior you were seeing before/after submit when you pasted a URL into to the answer box that was meant for answerfeedback.  check_file_access() is called (indirectly by pluginfile.php) every time the image is displayed, both before and after submit.

You can add a check for "question/answer" that doesn't involve feedback status.  (See my last post above.)

I've attached a diff that includes the changes to questiontype.php to enable answers to be saved in the database and to be displayed, assuming you've gotten everything working in edit_multichoice_form.php already so that you can create questions with images in answers.

In reply to Adriane Boyd

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

Thanks for the code .

As I wrote on one of the posts, I have already done the necessary modifications to edit_multichoice_form.php and questiontype.php.

Some trimmings remained to the validation process and I need to test import-export so that everything is fine.

However my last question remains as the tests where done with image in the answers and stored as is from the modified edit_multichoice_form.php and not by hacking.

So why they do not displayed in the initial preview ( I understand now that this was because of checkfileaccess()) although they were displayed after a submit?

My actual understanding is that there is no file access test after a submit.

I did not have the time to check all the code flow on a submit.

Pierre

In reply to Pierre Pichet

Re: Moodle 2: image in question answer problem, pls help ASAP

by Adriane Boyd -

check_file_access() is called every time there is a pluginfile.php image URL, which is every time the question is displayed, regardless of submits.  Are you sure you have the same check in check_file_access() that I included above?  If you copied the "question/answerfeedback" check exactly, this is the behavior that would result.  (I know, because I had the same error at one point.)

Would it be possible for you to post your code?  It's hard to figure out what's going on without seeing the whole picture.  Could you post a diff of the whole question/ directory to the current HEAD version?

In reply to Adriane Boyd

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

Hi Adriane,

I think I (or we wink)  need to read Tim docs on question engine and more specifically

http://docs.moodle.org/en/Development:Question_engine#Serving_files_and_access_checks

Come back tonight...

Pierre

In reply to Pierre Pichet

Re: Moodle 2: image in question answer problem, pls help ASAP

by Pierre Pichet -

To conclude:

If I set correctly the check_file_access in multichoice/questiontype.php

} else if ($component == 'question' && $filearea == 'answer') {
return array_key_exists($itemid, $question->options->answers);

everything works correctly.

Tim and others have done a great job. wink

 

Why things seems to work almost correctly if the check_file_access() is not modified , is a question to which I do not have the complete response

although I  suspect that it is related to the saving of attempts results in another specific context .

 

If somebody needs an urgent solution to add image to multiple choice answers, I can put in the tracker the actual not completely tested code diffs.

 

I need time to prepare my winter semester courses and reinstall my moodle working set-up to new GIT interface.

 

Pierre

P.S. thanks Adriane for your collaborationwink