简体   繁体   中英

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

I am trying to add upload file input in signup form in moodle v3
By using $mform->addElement('filepicker' .... ) in moodle/login/signup_form.php

But I am getting this error:
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()

So I think this mean that guest user does't allowed to use filepicker
So how can fix this ?

In case of someone need the answer, I finally find a solution.

But first the reason why guest user can't use filepaicker/filemanager is that the draft files for a filearea are stored based on the userid and guest users shouldn't be storing data in the system (otherwise different people logged in as 'guest' could potentially access each other's draft files).

The solution is: using $mform->addElement('file' .... ) and use RegEx with $mform->addRule() for validation as the following:

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

In moodle guests and not-logged-in users can not be allowed to upload anything.

if you want to still add filepicker in signup form then you need to modify following code .

1./lib/filelib.php (edit following function)

     function file_get_unused_draft_itemid() {
      if (isguestuser() or !isloggedin()) {
      //  print_error('noguest');
    }
    }

in short comment "print_error('noguest')" line .

  1. /lib/dml/moodle_database.php (edit following function)

      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); } } } 

edit above codes to respective files you will get solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM