From b82faacd036e4da13294f5697fb59c8e472c79f0 Mon Sep 17 00:00:00 2001 From: moodler Date: Tue, 19 Apr 2005 11:25:59 +0000 Subject: [PATCH] Fix for bug 1938. Now a teacher can delete any post, and all the children are deleted as well (after a warning and a preview of the affected posts). --- lang/en/forum.php | 2 ++ mod/forum/lib.php | 29 +++++++++++++++++++++++++++-- mod/forum/post.php | 43 ++++++++++++++++++++++++++++++++----------- 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/lang/en/forum.php b/lang/en/forum.php index 4ba4f2740f5..52faa510a92 100644 --- a/lang/en/forum.php +++ b/lang/en/forum.php @@ -30,7 +30,9 @@ $string['couldnotupdate'] = 'Could not update your post due to an unknown error' $string['delete'] = 'Delete'; $string['deleteddiscussion'] = 'The discussion topic has been deleted'; $string['deletedpost'] = 'The post has been deleted'; +$string['deletedposts'] = 'Those posts have been deleted'; $string['deletesure'] = 'Are you sure you want to delete this post?'; +$string['deletesureplural'] = 'Are you sure you want to delete this post and all replies? ($a posts)'; $string['digestmailheader'] = 'This is your daily digest of new posts from the $a->sitename forums. To change your forum email preferences, go to $a->userprefs.'; $string['digestmailprefs'] = 'your user profile'; $string['digestmailsubject'] = '$a: forum digest'; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 6761b6dae32..608f39f0b92 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -2326,11 +2326,18 @@ function forum_delete_discussion($discussion) { } -function forum_delete_post($post) { +function forum_delete_post($post, $children=false) { + if ($children) { + if ($childposts = get_records('forum_posts', 'parent', $post->id)) { + foreach ($childposts as $childpost) { + forum_delete_post($childpost, true); + } + } + } if (delete_records("forum_posts", "id", $post->id)) { delete_records("forum_ratings", "post", $post->id); // Just in case - forum_tp_delete_read_records(-1, $post->id); + forum_tp_delete_read_records(-1, $post->id); if ($post->attachment) { $discussion = get_record("forum_discussions", "id", $post->discussion); @@ -2343,6 +2350,24 @@ function forum_delete_post($post) { return false; } +function forum_count_replies($post, $children=true) { + $count = 0; + + if ($children) { + if ($childposts = get_records('forum_posts', 'parent', $post->id)) { + foreach ($childposts as $childpost) { + $count ++; // For this child + $count += forum_count_replies($childpost, true); + } + } + } else { + $count += count_records('forum_posts', 'parent', $post->id); + } + + return $count; +} + + function forum_forcesubscribe($forumid, $value=1) { return set_field("forum", "forcesubscribe", $value, "id", $forumid); } diff --git a/mod/forum/post.php b/mod/forum/post.php index fc0f0bdf13e..fe59a0959ad 100644 --- a/mod/forum/post.php +++ b/mod/forum/post.php @@ -343,13 +343,15 @@ } } + $replycount = forum_count_replies($post); + if (isset($confirm)) { // User has confirmed the delete if ($post->totalscore) { notice(get_string("couldnotdeleteratings", "forum"), forum_go_back_to("discuss.php?d=$post->discussion")); - } else if (record_exists("forum_posts", "parent", $delete)) { + } else if ($replycount && !isteacher($course->id)) { error(get_string("couldnotdeletereplies", "forum"), forum_go_back_to("discuss.php?d=$post->discussion")); @@ -370,13 +372,13 @@ redirect("view.php?f=$discussion->forum", get_string("deleteddiscussion", "forum"), 1); - } else if (forum_delete_post($post)) { + } else if (forum_delete_post($post, isteacher($course->id))) { add_to_log($discussion->course, "forum", "delete post", "discuss.php?d=$post->discussion", "$post->id", $cm->id); - redirect(forum_go_back_to("discuss.php?d=$post->discussion"), - get_string("deletedpost", "forum"), 1); + $feedback = $replycount ? get_string('deletedposts', 'forum') : get_string('deletedpost', 'forum'); + redirect(forum_go_back_to("discuss.php?d=$post->discussion"), $feedback, 1); } else { error("An error occurred while deleting record $post->id"); } @@ -387,14 +389,33 @@ forum_set_return(); - print_header(); - notice_yesno(get_string("deletesure", "forum"), - "post.php?delete=$delete&confirm=$delete", - $_SERVER["HTTP_REFERER"]); + if ($replycount) { + if (!isteacher($course->id)) { + error(get_string("couldnotdeletereplies", "forum"), + forum_go_back_to("discuss.php?d=$post->discussion")); + } + print_header(); + notice_yesno(get_string("deletesureplural", "forum", $replycount+1), + "post.php?delete=$delete&confirm=$delete", + $_SERVER["HTTP_REFERER"]); + + forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false); + if (empty($post->edit)) { + if ($CFG->forum_trackreadposts) { + $user_read_array = forum_tp_get_discussion_read_records($USER->id, $discussion->id); + } else { + $user_read_array = array(); + } + forum_print_posts_nested($post->id, $course->id, false, false, $user_read_array, $forum->id); + } + } else { + print_header(); + notice_yesno(get_string("deletesure", "forum", $replycount), + "post.php?delete=$delete&confirm=$delete", + $_SERVER["HTTP_REFERER"]); + forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false); + } - echo "

"; - forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false); - echo "
"; } print_footer($course); die;