如何在 MOODLE 2.9 中通过 tinymce 编辑器保存和显示上传的图像

问题描述:

Moodle 中保存和显示 tinymce 内容.

Save and display tinymce content in Moodle.

我有一个在数据库中保存问题和答案的块.

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

为此我使用 tinymce editor,以便用户可以输入文本和图像.

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

我的编辑器表单是:

.....
$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);
.....

我提交表单并将tinymce中的数据(文本+图像)保存在db中

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.

我使用:

$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">

要在编辑器中操作文件,您必须使用以下方法:

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

  • 在显示表单之前:file_prepare_standard_editor()
  • 保存表单时:file_postupdate_standard_editor()
  • 显示内容时:file_rewrite_pluginfile_urls() 后跟format_text()

您可以在 cohort/edit.phpcohort/index.php 中找到此类示例.

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

完成后,您需要实现函数_pluginfile,Moodle 核心将调用该函数来获取文件._pluginfile 函数是必需的,以便您的插件可以检查用户是否可以访问该文件.您可以在 filelib.php file_pluginfile() 和各种 lib.php _pluginfile() 中找到默认实现.

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().