diff --git a/mod/data/field/file/field.class.php b/mod/data/field/file/field.class.php
index 251fa7c93dc..0e2c464abbf 100644
--- a/mod/data/field/file/field.class.php
+++ b/mod/data/field/file/field.class.php
@@ -26,13 +26,8 @@ class data_field_file extends data_field_base {
var $type = 'file';
function display_add_field($recordid = 0, $formdata = null) {
- global $CFG, $DB, $OUTPUT, $PAGE, $USER;
+ global $DB, $OUTPUT, $PAGE;
- $file = false;
- $content = false;
- $displayname = '';
- $fs = get_file_storage();
- $context = $PAGE->context;
$itemid = null;
// editing an existing database entry
@@ -40,27 +35,16 @@ class data_field_file extends data_field_base {
$fieldname = 'field_' . $this->field->id . '_file';
$itemid = clean_param($formdata->$fieldname, PARAM_INT);
} else if ($recordid) {
- if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
-
- file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id);
-
- if (!empty($content->content)) {
- if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
- $usercontext = context_user::instance($USER->id);
- if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false)) {
- return false;
- }
- if (empty($content->content1)) {
- // Print icon if file already exists
- $src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename());
- $displayname = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')). ''.s($file->get_filename()).'';
-
- } else {
- $displayname = 'no file added';
- }
- }
- }
+ if (!$content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
+ // Quickly make one now!
+ $content = new stdClass();
+ $content->fieldid = $this->field->id;
+ $content->recordid = $recordid;
+ $id = $DB->insert_record('data_content', $content);
+ $content = $DB->get_record('data_content', array('id' => $id));
}
+ file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id);
+
} else {
$itemid = file_get_unused_draft_itemid();
}
@@ -170,15 +154,8 @@ class data_field_file extends data_field_base {
global $CFG, $DB, $USER;
$fs = get_file_storage();
- if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
-
- // Quickly make one now!
- $content = new stdClass();
- $content->fieldid = $this->field->id;
- $content->recordid = $recordid;
- $id = $DB->insert_record('data_content', $content);
- $content = $DB->get_record('data_content', array('id'=>$id));
- }
+ // Should always be available since it is set by display_add_field before initializing the draft area.
+ $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid));
file_save_draft_area_files($value, $this->context->id, 'mod_data', 'content', $content->id);
diff --git a/mod/data/field/picture/field.class.php b/mod/data/field/picture/field.class.php
index 53e8662c092..7354415e598 100644
--- a/mod/data/field/picture/field.class.php
+++ b/mod/data/field/picture/field.class.php
@@ -32,7 +32,6 @@ class data_field_picture extends data_field_base {
$file = false;
$content = false;
- $displayname = '';
$alttext = '';
$itemid = null;
$fs = get_file_storage();
@@ -45,29 +44,25 @@ class data_field_picture extends data_field_base {
$alttext = $formdata->$fieldname;
}
} else if ($recordid) {
- if ($content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
- file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id);
- if (!empty($content->content)) {
- if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
- $usercontext = context_user::instance($USER->id);
- if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $itemid, 'id DESC', false)) {
- return false;
- }
- if ($thumbfile = $fs->get_file($usercontext->id, 'user', 'draft', $itemid, '/', 'thumb_'.$content->content)) {
- $thumbfile->delete();
- }
- if (empty($content->content1)) {
- // Print icon if file already exists
- $src = moodle_url::make_draftfile_url($itemid, '/', $file->get_filename());
- $displayname = $OUTPUT->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')). ''.s($file->get_filename()).'';
+ if (!$content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
+ // Quickly make one now!
+ $content = new stdClass();
+ $content->fieldid = $this->field->id;
+ $content->recordid = $recordid;
+ $id = $DB->insert_record('data_content', $content);
+ $content = $DB->get_record('data_content', array('id' => $id));
+ }
+ file_prepare_draft_area($itemid, $this->context->id, 'mod_data', 'content', $content->id);
+ if (!empty($content->content)) {
+ if ($file = $fs->get_file($this->context->id, 'mod_data', 'content', $content->id, '/', $content->content)) {
+ $usercontext = context_user::instance($USER->id);
- } else {
- $displayname = get_string('nofilesattached', 'repository');
- }
+ if ($thumbfile = $fs->get_file($usercontext->id, 'user', 'draft', $itemid, '/', 'thumb_'.$content->content)) {
+ $thumbfile->delete();
}
}
- $alttext = $content->content1;
}
+ $alttext = $content->content1;
} else {
$itemid = file_get_unused_draft_itemid();
}
@@ -228,14 +223,8 @@ class data_field_picture extends data_field_base {
function update_content($recordid, $value, $name='') {
global $CFG, $DB, $USER;
- if (!$content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
- // Quickly make one now!
- $content = new stdClass();
- $content->fieldid = $this->field->id;
- $content->recordid = $recordid;
- $id = $DB->insert_record('data_content', $content);
- $content = $DB->get_record('data_content', array('id'=>$id));
- }
+ // Should always be available since it is set by display_add_field before initializing the draft area.
+ $content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid));
$names = explode('_', $name);
switch ($names[2]) {