diff --git a/mod/data/field/file/field.class.php b/mod/data/field/file/field.class.php index b1cee24..db96a4d 100644 --- a/mod/data/field/file/field.class.php +++ b/mod/data/field/file/field.class.php @@ -174,36 +174,21 @@ class data_field_file extends data_field_base { $content = $DB->get_record('data_content', array('id'=>$id)); } - // delete existing files - $fs->delete_area_files($this->context->id, 'mod_data', 'content', $content->id); + file_save_draft_area_files($value, $this->context->id, 'mod_data', 'content', $content->id); $usercontext = context_user::instance($USER->id); - $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value, 'timecreated DESC'); + $files = $fs->get_area_files($this->context->id, 'mod_data', 'content', $content->id, 'itemid, filepath, filename', false); - if (count($files)<2) { - // no file + // we expect no or just one file (maxfiles = 1 option is set for the form_filemanager) + if (count($files) == 0) { + $content->content = null; + } elseif (count($files) == 1) { + $content->content = array_values($files)[0]->get_filename(); } else { - foreach ($files as $draftfile) { - if (!$draftfile->is_directory()) { - $file_record = array( - 'contextid' => $this->context->id, - 'component' => 'mod_data', - 'filearea' => 'content', - 'itemid' => $content->id, - 'filepath' => '/', - 'filename' => $draftfile->get_filename(), - ); - - $content->content = $file_record['filename']; - - $fs->create_file_from_storedfile($file_record, $draftfile); - $DB->update_record('data_content', $content); - - // Break from the loop now to avoid overwriting the uploaded file record - break; - } - } + // this should not happen + throw new invalid_state_exception('more then one file found in area during update data record'); } + $DB->update_record('data_content', $content); } function text_export_supported() { diff --git a/mod/data/field/picture/field.class.php b/mod/data/field/picture/field.class.php index d4ade19..9695639 100644 --- a/mod/data/field/picture/field.class.php +++ b/mod/data/field/picture/field.class.php @@ -233,39 +233,26 @@ class data_field_picture extends data_field_base { switch ($names[2]) { case 'file': $fs = get_file_storage(); - $fs->delete_area_files($this->context->id, 'mod_data', 'content', $content->id); + file_save_draft_area_files($value, $this->context->id, 'mod_data', 'content', $content->id); $usercontext = context_user::instance($USER->id); - $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $value); - if (count($files)<2) { - // no file - } else { - $count = 0; - foreach ($files as $draftfile) { - $file_record = array('contextid'=>$this->context->id, 'component'=>'mod_data', 'filearea'=>'content', 'itemid'=>$content->id, 'filepath'=>'/'); - if (!$draftfile->is_directory()) { - $file_record['filename'] = $draftfile->get_filename(); - - $content->content = $draftfile->get_filename(); - - $file = $fs->create_file_from_storedfile($file_record, $draftfile); - - // If the file is not a valid image, redirect back to the upload form. - if ($file->get_imageinfo() === false) { - $url = new moodle_url('/mod/data/edit.php', array('d' => $this->field->dataid)); - redirect($url, get_string('invalidfiletype', 'error', $file->get_filename())); - } - - $DB->update_record('data_content', $content); - $this->update_thumbnail($content, $file); - - if ($count > 0) { - break; - } else { - $count++; - } - } + $files = $fs->get_area_files($this->context->id, 'mod_data', 'content', $content->id, 'itemid, filepath, filename', false); + + // we expect no or just one file (maxfiles = 1 option is set for the form_filemanager) + if (count($files) == 0) { + $content->content = null; + } elseif (count($files) == 1) { + $file = array_values($files)[0]; + if ($file->get_imageinfo() === false) { + $url = new moodle_url('/mod/data/edit.php', array('d' => $this->field->dataid)); + redirect($url, get_string('invalidfiletype', 'error', $file->get_filename())); } + $content->content = $file->get_filename(); + $this->update_thumbnail($content, $file); + } else { + // this should not happen + throw new invalid_state_exception('more then one file found in area during update data record'); } + $DB->update_record('data_content', $content); break;