下面是我用于表单的代码示例,然后处理文件保存。我做错了什么,但我不确定它是什么。请协助。我附上了一个屏幕截图,显示了我在提交表单后收到的错误。
error on form submission in moodle inside activity module
class mod_audio_mod_form extends moodleform_mod {
/**
* Defines forms elements
*/
public function definition() {
global $CFG;
$mform = $this->_form;
// Adding the "general" fieldset, where all the common settings are showed.
$mform->addElement('header', 'general', get_string('general', 'form'));
// Adding the standard "name" field.
$mform->addElement('text', 'name', get_string('audioname', 'audio'), array('size' => '64'));
if (!empty($CFG->formatstringstriptags)) {
$mform->setType('name', PARAM_TEXT);
} else {
$mform->setType('name', PARAM_CLEANHTML);
}
$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addHelpButton('name', 'audioname', 'audio');
// Adding the standard "intro" and "introformat" fields.
if ($CFG->branch >= 29) {
$this->standard_intro_elements();
} else {
$this->add_intro_editor();
}
// Adding the rest of audio settings, spreading all them into this fieldset
// ... or adding more fieldsets ('header' elements) if needed for better logic.
$mform->addElement('header', 'audiofieldset', get_string('audiofieldset', 'audio'));
$filemanager_options = array();
$filemanager_options['accepted_types'] = '*';
$filemanager_options['maxbytes'] = 0;
$filemanager_options['maxfiles'] = -1;
$filemanager_options['mainfile'] = true;
$mform->addElement('filemanager', 'files', get_string('selectfiles'), null, $filemanager_options);
$mform->addRule('files', null, 'required', null, 'client');
// Add standard grading elements.
$this->standard_grading_coursemodule_elements();
// Add standard elements, common to all modules.
$this->standard_coursemodule_elements();
// Add standard buttons, common to all modules.
$this->add_action_buttons();
}
function data_preprocessing(&$default_values) {
if ($this->current->instance) {
// editing existing instance - copy existing files into draft area
$draftitemid = file_get_submitted_draft_itemid('files');
file_prepare_draft_area($draftitemid, $this->context->id, 'mod_audio', 'content', 0, array('subdirs' => true));
$default_values['files'] = $draftitemid;
}
}
function definition_after_data() {
if ($this->current->instance and $this->current->tobemigrated) {
// resource not migrated yet
return;
}
parent::definition_after_data();
}
function validation($data, $files) {
global $USER;
$errors = parent::validation($data, $files);
$usercontext = context_user::instance($USER->id);
$fs = get_file_storage();
if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['files'], 'sortorder, id', false)) {
$errors['files'] = get_string('required');
return $errors;
}
if (count($files) == 1) {
// no need to select main file if only one picked
return $errors;
} else if (count($files) > 1) {
$mainfile = false;
foreach ($files as $file) {
if ($file->get_sortorder() == 1) {
$mainfile = true;
break;
}
}
// set a default main file
if (!$mainfile) {
$file = reset($files);
file_set_sortorder($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename(), 1);
}
}
return $errors;
}
}
// LOCALLIB.PHP中的课程
class audio_content_file_info extends file_info_stored {
public function get_parent() {
if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
return $this->browser->get_file_info($this->context);
}
return parent::get_parent();
}
public function get_visible_name() {
if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
return $this->topvisiblename;
}
return parent::get_visible_name();
}
function save_files($data) {
global $DB;
// Storage of files from the filemanager (videos).
$draftitemid = $data->files;
if ($draftitemid) {
file_save_draft_area_files(
$draftitemid, $this->context->id, 'mod_audio', 'files', 0
);
}
}
}
//在LIB.PHP中添加实例的功能
function audio_add_instance(stdClass $audio, mod_audio_mod_form $mform = null) {
global $CFG, $DB;
require_once("$CFG->libdir/resourcelib.php");
require_once("$CFG->dirroot/mod/audio/locallib.php");
$cmid = $audio->coursemodule;
$audio->timecreated = time();
$audio->timemodified = time();
resource_set_display_options($audio);
$audio->id = $DB->insert_record('audio', $audio);
$context = context_module::instance($audio->coursemodule);
$audiofile = new audio_content_file_info($context, null, null);
$audiofile->save_files($audio);
// we need to use context now, so we need to make sure all needed info is already in db
$DB->set_field('course_modules', 'instance', $audio->id, array('id' => $cmid));
resource_set_mainfile($audio);
return $audio->id;
}
请告诉我这里可能出错的地方。