Form element filepicker and file manager are not displaying


Form element filepicker and file manager are not displaying by $mform->addElement('filepicker' .... ) Error code: noguest

我试图在moodle v3注册表单中添加上传文件输入
$mform->addElement('filepicker' .... )moodle/login/signup_form.php中的应用

但是我得到这个错误:
Error code: noguest

Stack trace:
line 488 of /lib/setuplib.php: moodle_exception thrown
line 348 of /lib/filelib.php: call to print_error()
line 131 of /lib/form/filepicker.php: call to file_get_unused_draft_itemid()
line 189 of /lib/pear/HTML/QuickForm/Renderer/Tableless.php: call to MoodleQuickForm_filepicker->toHtml()
line 2806 of /lib/formslib.php: call to HTML_QuickForm_Renderer_Tableless->renderElement()
line 408 of /lib/pear/HTML/QuickForm/element.php: call to MoodleQuickForm_Renderer->renderElement()
line 1639 of /lib/pear/HTML/QuickForm.php: call to HTML_QuickForm_element->accept()
line 1714 of /lib/formslib.php: call to HTML_QuickForm->accept()
line 1682 of /lib/pear/HTML/QuickForm.php: call to MoodleQuickForm->accept()
line 442 of /lib/pear/HTML/Common.php: call to HTML_QuickForm->toHtml()
line 204 of /lib/pear/HTML/QuickForm/DHTMLRulesTableless.php: call to HTML_Common->display()
line 933 of /lib/formslib.php: call to HTML_QuickForm_DHTMLRulesTableless->display()
line 117 of /login/signup.php: call to moodleform->display()

所以我认为这意味着guest用户不允许使用filepicker
那么如何解决这个问题呢?

在moodle中不允许来宾和未登录的用户上传任何内容。

如果你还想在注册表单中添加文件拾取器,那么你需要修改以下代码:

1./lib/filelib.php (edit following function)
     function file_get_unused_draft_itemid() {
      if (isguestuser() or !isloggedin()) {
      //  print_error('noguest');
    }
    }

在短注释"print_error('noguest')"行

  • /lib/dml/moodle_database.php(编辑以下函数)

     public function get_record_select($table, $select, array   $params=null,$fields='*',$strictness=IGNORE_MISSING){
    if ($select) {
        $select = "WHERE $select";
    }
    try {
        return $this->get_record_sql("SELECT $fields FROM {" . $table . "} $select", $params, $strictness);
    } catch (dml_missing_record_exception $e) {
        if (!isloggedin()){}
        else{
        // create new exception which will contain correct table name
        throw new dml_missing_record_exception($table, $e->sql, $e->params);
        }
    }
    }
    
  • 将以上代码编辑到各自的文件中,您将得到解决方案。

    万一有人需要答案,我终于找到了一个解决方案。

    但是首先客户用户不能使用filepaicker/filemanager的原因是文件区的草稿文件是基于userid存储的,客户用户不应该在系统中存储数据(否则不同的人登录为'guest'可能会访问彼此的草稿文件)。

    解决方案是:使用$mform->addElement('file' .... ),并使用RegEx$mform->addRule()进行验证,如下所示:

    $mform->addRule('document_1', 'Error (allowed extensions are .jpg, .png and .pdf)', 'filename', 'myregex');