MDL-54965 mod_data: Init draft area for empty file/pic fields
When a file or picture field was added with existing records, these records could not be saved, since the draft area was not created properly. Co-authored-by: Jérôme Mouneyrac <mouneyrac>
This commit is contained in:
co-authored by
Jérôme Mouneyrac <mouneyrac>
parent
62c58d22be
commit
016c8adf69
@@ -40,24 +40,29 @@ 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))) {
|
||||
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);
|
||||
|
||||
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')). '<a href="'.$src.'" >'.s($file->get_filename()).'</a>';
|
||||
|
||||
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')). '<a href="'.$src.'" >'.s($file->get_filename()).'</a>';
|
||||
|
||||
} else {
|
||||
$displayname = 'no file added';
|
||||
}
|
||||
} else {
|
||||
$displayname = 'no file added';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -167,15 +172,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);
|
||||
|
||||
|
||||
@@ -45,29 +45,35 @@ 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')). '<a href="'.$src.'" >'.s($file->get_filename()).'</a>';
|
||||
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);
|
||||
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')). '<a href="'.$src.'" >'.s($file->get_filename()).'</a>';
|
||||
|
||||
} else {
|
||||
$displayname = get_string('nofilesattached', 'repository');
|
||||
}
|
||||
} else {
|
||||
$displayname = get_string('nofilesattached', 'repository');
|
||||
}
|
||||
}
|
||||
$alttext = $content->content1;
|
||||
}
|
||||
$alttext = $content->content1;
|
||||
} else {
|
||||
$itemid = file_get_unused_draft_itemid();
|
||||
}
|
||||
@@ -225,14 +231,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]) {
|
||||
|
||||
Reference in New Issue
Block a user