How to save and display uploaded image via tinymce editor in MOODLE 2.9

女生的网名这么多〃 提交于 2019-12-12 03:56:19

问题


Save and display tinymce content in Moodle.

I have a block that save question and answer in db.

I use tinymce editor for this, so that user can enter text and image.

My editor form is:

.....
$editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$context);
        $mform->addElement('editor', 'title_editor', 'Questions', null, $editoroptions);
        $mform->addRule('title_editor', null, 'required', null, 'client');
        $mform->setType('title_editor', PARAM_RAW);
.....

I submit the form and save the data(text+image) from tinymce in db

......

if($data = $sample_form->get_data()) {
if ($draftitemid = file_get_submitted_draft_itemid('title_editor')) {
    $data->title_editor['text'] = file_save_draft_area_files($draftitemid, $contextid, 'block_sample', questiontext, array('subdirs' => true, 'maxfiles' => 5),$data->title_editor['text']);
}

//insert to database
$inserRecord = new stdClass();
$inserRecord->suggestion     = $sgid;
$inserRecord->questiontext     = $data->title_editor['text'];
$inserRecord->answertext     = $data->answer['text'];
$insertRes = add_question_desc($inserRecord);
......

In db the data(here question and answer) saved. The question data is looks like:

<p>What color is this?</p>
<p><img src="@@PLUGINFILE@@/sample_image.png" width="309" height="212" alt="green" /></p>

Is this complete to save the data? Where did the uploaded file saved. How I retreive/display the uploaded file.

I use:

$qn = file_rewrite_pluginfile_urls($qnDetails[$qnid]->questiontext, "pluginfile.php", $context->id, "block_sample", 'questiontext', $qnid);
echo $qn;

The above code only display the text and image is not displaying.

I inspect the broken image field and it is:

<img src="http://localhost/moodle/pluginfile.php/24/block_sample/questiontext/12/mc4.png" width="309" height="212" alt="mc4.png">

回答1:


To manipulate files in an editor you must use the following methods:

  • Prior to displaying the form: file_prepare_standard_editor()
  • When saving the form: file_postupdate_standard_editor()
  • When displaying the content: file_rewrite_pluginfile_urls() followed by format_text()

You can find an example of this in cohort/edit.php and cohort/index.php.

Once that is done, you need to implement the function _pluginfile which Moodle core will call to get the file. The _pluginfile functions are required so that your plugin can check whether or not the user can access the file. You can find default implementations in filelib.php file_pluginfile() and in various lib.php <component_name>_pluginfile().



来源:https://stackoverflow.com/questions/39999051/how-to-save-and-display-uploaded-image-via-tinymce-editor-in-moodle-2-9

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!