Skip to content

Commit

Permalink
Merge branch 'master-MDL-49750' of git://github.com/jojoob/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao authored and danpoltawski committed Oct 6, 2015
2 parents c6ce30b + 8c3b27d commit 86c48d1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 53 deletions.
34 changes: 10 additions & 24 deletions mod/data/field/file/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,36 +176,22 @@ function update_content($recordid, $value, $name='') {
$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;
} 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;
}
$content->content = array_values($files)[0]->get_filename();
if (count($files) > 1) {
// This should not happen with a consistent database. Inform admins/developers about the inconsistency.
debugging('more then one file found in mod_data instance {$this->data->id} file field (field id: {$this->field->id}) area during update data record {$recordid} (content id: {$content->id})', DEBUG_NORMAL);
}
}
$DB->update_record('data_content', $content);
}

function text_export_supported() {
Expand Down
53 changes: 24 additions & 29 deletions mod/data/field/picture/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,39 +235,34 @@ function update_content($recordid, $value, $name='') {
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
$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;
} 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++;
}
}
$file = array_values($files)[0];

if (count($files) > 1) {
// This should not happen with a consistent database. Inform admins/developers about the inconsistency.
debugging('more then one file found in mod_data instance {$this->data->id} picture field (field id: {$this->field->id}) area during update data record {$recordid} (content id: {$content->id})', DEBUG_NORMAL);
}

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);
}
$DB->update_record('data_content', $content);

break;

Expand Down

0 comments on commit 86c48d1

Please sign in to comment.