forum files MDL-25167 files in the post file area were not moved when the post was moved or deleted.

Also, I refactord some code out of forum_move_post and into a new method file_storage::move_area_files_to_new_context. I hope that is OK.
This commit is contained in:
Tim Hunt
2010-11-11 18:58:06 +00:00
parent d8c4a74e29
commit d2af10140a
2 changed files with 43 additions and 18 deletions
+29
View File
@@ -441,6 +441,35 @@ class file_storage {
return true; // BC only
}
/**
* Move all the files in a file area from one context to another.
* @param integer $oldcontextid the context the files are being moved from.
* @param integer $newcontextid the context the files are being moved to.
* @param string $component the plugin that these files belong to.
* @param string $filearea the name of the file area.
* @return integer the number of files moved, for information.
*/
public function move_area_files_to_new_context($oldcontextid, $newcontextid, $component, $filearea, $itemid = false) {
// Note, this code is based on some code that Petr wrote in
// forum_move_attachments in mod/forum/lib.php. I moved it here because
// I needed it in the question code too.
$count = 0;
$oldfiles = $this->get_area_files($oldcontextid, $component, $filearea, $itemid, 'id', false);
foreach ($oldfiles as $oldfile) {
$filerecord = new stdClass();
$filerecord->contextid = $newcontextid;
$this->create_file_from_storedfile($filerecord, $oldfile);
$count += 1;
}
if ($count) {
$this->delete_area_files($oldcontextid, $component, $filearea, $itemid);
}
return $count;
}
/**
* Recursively creates directory.
*
+14 -18
View File
@@ -3759,24 +3759,18 @@ function forum_move_attachments($discussion, $forumfrom, $forumto) {
// loop through all posts, better not use attachment flag ;-)
if ($posts = $DB->get_records('forum_posts', array('discussion'=>$discussion->id), '', 'id, attachment')) {
foreach ($posts as $post) {
if ($oldfiles = $fs->get_area_files($oldcontext->id, 'mod_forum', 'attachment', $post->id, "id", false)) {
foreach ($oldfiles as $oldfile) {
$file_record = new stdClass();
$file_record->contextid = $newcontext->id;
$fs->create_file_from_storedfile($file_record, $oldfile);
}
$fs->delete_area_files($oldcontext->id, 'mod_forum', 'attachment', $post->id);
if ($post->attachment != '1') {
//weird - let's fix it
$post->attachment = '1';
$DB->update_record('forum_posts', $post);
}
} else {
if ($post->attachment != '') {
//weird - let's fix it
$post->attachment = '';
$DB->update_record('forum_posts', $post);
}
$fs->move_area_files_to_new_context($oldcontext->id,
$newcontext->id, 'mod_forum', 'post', $post->id);
$attachmentsmoved = $fs->move_area_files_to_new_context($oldcontext->id,
$newcontext->id, 'mod_forum', 'attachment', $post->id);
if ($attachmentsmoved > 0 && $post->attachment != '1') {
// Weird - let's fix it
$post->attachment = '1';
$DB->update_record('forum_posts', $post);
} else if ($attachmentsmoved == 0 && $post->attachment != '') {
// Weird - let's fix it
$post->attachment = '';
$DB->update_record('forum_posts', $post);
}
}
}
@@ -4235,6 +4229,7 @@ function forum_delete_post($post, $children, $course, $cm, $forum, $skipcompleti
//delete attachments
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_forum', 'attachment', $post->id);
$fs->delete_area_files($context->id, 'mod_forum', 'post', $post->id);
if ($DB->delete_records("forum_posts", array("id" => $post->id))) {
@@ -7006,6 +7001,7 @@ function forum_reset_userdata($data) {
}
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$fs->delete_area_files($context->id, 'mod_forum', 'attachment');
$fs->delete_area_files($context->id, 'mod_forum', 'post');
//remove ratings
$ratingdeloptions->contextid = $context->id;