Skip to content

Commit

Permalink
Merge branch 'MDL-54965-master' of git://github.com/tobiasreischmann/…
Browse files Browse the repository at this point in the history
…moodle
  • Loading branch information
stronk7 authored and danpoltawski committed Jul 11, 2017
2 parents e78d7cc + 3a133b3 commit 64ea4b5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 64 deletions.
47 changes: 12 additions & 35 deletions mod/data/field/file/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,25 @@ 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
if ($formdata) {
$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')). '<a href="'.$src.'" >'.s($file->get_filename()).'</a>';

} 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();
}
Expand Down Expand Up @@ -170,15 +154,8 @@ function update_content($recordid, $value, $name='') {
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);

Expand Down
47 changes: 18 additions & 29 deletions mod/data/field/picture/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ function display_add_field($recordid = 0, $formdata = null) {

$file = false;
$content = false;
$displayname = '';
$alttext = '';
$itemid = null;
$fs = get_file_storage();
Expand All @@ -45,29 +44,25 @@ function display_add_field($recordid = 0, $formdata = null) {
$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>';

} else {
$displayname = get_string('nofilesattached', 'repository');
}
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 ($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();
}
Expand Down Expand Up @@ -228,14 +223,8 @@ function update_field() {
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]) {
Expand Down

0 comments on commit 64ea4b5

Please sign in to comment.