In Moodle block
(example block_sample
) the images uploaded via tinymce
editor is not displaying.
I save the text (with image) in db as:
$instance = $DB->get_record('block_instances', array('id' => 57));
$blockname = 'sample';
$block = block_instance($blockname, $instance);
if($data = $question_form->get_data()) {
if ($draftitemid = file_get_submitted_draft_itemid('title')) {
$data->title['text'] = file_save_draft_area_files($draftitemid, $block->context->id 'block_sample', 'questiontext', 0, array('subdirs' => true, 'maxfiles' => 5),$data->title['text']);
$inserRecord = new stdClass();
$inserRecord->blockid = $cid;
$inserRecord->questiontext = $data->title['text'];
$insertRes = $DB->insert_record("sample_data", $inserRecord);
}
}
Here questiontext
is the db field name where the question is saved.
And the data is saved in db as
id blockid questiontext
1 2 <p>Test</p><p><img src="https://moodle.org/pluginfile.php/148/mod_forum/post/1376587/flower_123.jpeg" width="200" height="200" /></p>
Now I need to display the data from db(for example first record):
$descDetails = $DB->get_records('sample_data', array('id'=>1));
$qn = file_rewrite_pluginfile_urls($descDetails[1]->questiontext, "pluginfile.php", $block->context->id, "block_sample", 'questiontext', $descDetails[1]->id);
$format = FORMAT_HTML;
$filteropt = new stdClass;
$filteropt->overflowdiv = true;
$qn = format_text($qn, $format, $filteropt);
echo $qn;
But the image is not displaying here. How can I fix this? I inspect the element and I got:
<img src="http://localhost/moodle/pluginfile.php/122/block_sample/questiontext/1/flower_123.jpeg" width="297" height="203" alt="d" id="yui_3_17_2_2_1476699462273_239">
Please help me..Is there any issue while taking the context id
of block or there is another issue.
For moodle module(activity)
I am successfully displayed the image for the same case. This is not working in the case of moodle block
.
The only difference here, is taking the context id
for activity module
and block
.
For activity module
I take the context id
as:
$context = context_module::instance($cm->id);
Conclusion: For activity module the images (uploaded via tinymce editor) is displaying but for moodle block images is not displaying.
How can I resolve this?