Moodle中的file_postupdate_standard_editor函数

时间:2016-10-04 11:58:45

标签: tinymce moodle

我的 moodle表单中的编辑器代码

$mform->addElement('editor', 'title_editor', 'Questions', null, array('maxfiles' => EDITOR_UNLIMITED_FILES));
$mform->addRule('title', null, 'required', null, 'client');
$mform->setType('title', PARAM_RAW);

(表格也包含其他字段)

我提交表单并在操作页面中获取数据:

$data = $questionpool_form->get_data(true);

$ data 是:

object(stdClass)[127]
  public 'title' => 
    array (size=3)
      'text' => string '<p>What is the image shown below:</p>
<p><img src="http://localhost/moodle/draftfile.php/5/user/draft/717102051/111.png" width="297" height="203" /></p>' (length=153)
      'format' => string '1' (length=1)
      'itemid' => int 717102051
  public 'answer' => 
    array (size=2)
      'text' => string '<p>Brown color</p>' (length=18)
      'format' => string '1' (length=1)
  public 'blockid' => string '' (length=0)
  public 'courseid' => string '' (length=0)
  public 'submitbutton' => string 'Proceed' (length=7)

由于图像在草稿文件中,学生无法看到图像。因此,为了保存和重新链接我使用的嵌入式图像:

$definitionoptions = array('maxfiles' => EDITOR_UNLIMITED_FILES);
$data->id = null;
$data = file_postupdate_standard_editor($data, 'title', $definitionoptions, $context, 'block_question', 'title', $data->id);

我打印(检查)新数据变量

object(stdClass)[127]
  public 'title' => 
    array (size=3)
      'text' => string '<p>What is the image shown below:</p>
<p><img src="http://localhost/moodle/draftfile.php/5/user/draft/717102051/111.png" width="297" height="203" /></p>' (length=153)
      'format' => string '1' (length=1)
      'itemid' => int 717102051
  public 'answer' => 
    array (size=2)
      'text' => string '<p>Brown color</p>' (length=18)
      'format' => string '1' (length=1)
  public 'blockid' => string '' (length=0)
  public 'courseid' => string '' (length=0)
  public 'submitbutton' => string 'Proceed' (length=7)
  public 'id' => null
  public 'editortrust' => int 0
  public 'editor' => null
  public 'editorformat' => null

但图像位置相同且学生无法看到。

我的问题是:

我的file_postupdate_standard_editor函数有什么错误?

如何将图像保存在正确的位置以成功显示图像(如果有)?

修改

之后的新$data变量
  

davosmith&#39; S

答案

object(stdClass)[117]
  public 'title_editor' => 
    array (size=3)
      'text' => string '<p>Find the color??</p>
<p><img src="http://localhost/moodle/draftfile.php/5/user/draft/239105225/flower_123.jpeg" width="200" height="200" alt="fff" /></p>' (length=157)
      'format' => string '1' (length=1)
      'itemid' => int 509796143
  public 'answer' => 
    array (size=2)
      'text' => string '<p>37</p>' (length=9)
      'format' => string '1' (length=1)
  public 'blockid' => string '' (length=0)
  public 'courseid' => string '' (length=0)
  public 'submitbutton' => string 'Proceed' (length=7)
  public 'titletrust' => int 0
  public 'title' => string '<p>Find the color??</p>
<p><img src="http://localhost/moodle/draftfile.php/5/user/draft/239105225/flower_123.jpeg" width="200" height="200" alt="fff" /></p>' (length=157)
  public 'titleformat' => string '1' (length=1)

编辑2

我的moodle表单包含 data_preprocessing

功能
function data_preprocessing(&$data) {
        if ($this->current->instance) {
            $courseid = required_param('id', PARAM_INT); // Do not use $_GET directly.
            $course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST); // Optional, but you often need the course object.
            $context = context_course::instance($courseid);
            $contextid = $context->id;
            $textfieldoptions = array('maxfiles' => EDITOR_UNLIMITED_FILES);
            $draftitemid = file_get_submitted_draft_itemid('title_editor');
            $data['title_editor']['format'] = $data['contentformat'];
           // $data = file_prepare_standard_editor($data, 'title', $textfieldoptions, $context,'block_questionpool', 'title', 0);
            $data['title_editor']['text']   = file_prepare_draft_area($draftitemid, $contextid, 'block_questionpool', 'title', 0, page_get_editor_options($contextid), $data['title']);
            $data['title_editor']['itemid'] = $draftitemid;

        }
        if (!empty($data['displayoptions'])) {
            $displayoptions = unserialize($data['displayoptions']);
            if (isset($displayoptions['printintro'])) {
                $data['printintro'] = $displayoptions['printintro'];
            }
            if (isset($displayoptions['printheading'])) {
                $data['printheading'] = $displayoptions['printheading'];
            }
            if (!empty($displayoptions['popupwidth'])) {
                $data['popupwidth'] = $displayoptions['popupwidth'];
            }
            if (!empty($displayoptions['popupheight'])) {
                $data['popupheight'] = $displayoptions['popupheight'];
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您已拨打编辑字段&#39; title&#39;,然后告诉file_postupdate_standard_editor保存名为&#39;编辑器的字段的详细信息。

如果您想调用数据库中的字段&#39; title&#39; (以及&#39; titleformat&#39;),然后表单字段应该被称为&#39; title_editor&#39;而file_postupdate_standard_editor(和file_prepare_standard_editor)的第二个参数应为&#39; title&#39;。

你需要给Moodle一个正确的上下文,否则它不会知道保存文件的位置 - 如果它是一个块,那么就给它块上下文或者可能是课程上下文。