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).
This commit is contained in:
moodler 2005-04-19 11:25:59 +00:00
parent 5d154b0d91
commit b82faacd03
3 changed files with 61 additions and 13 deletions

View File

@ -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';

View File

@ -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);
}

View File

@ -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 "<center><hr />";
forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
echo "</center>";
}
print_footer($course);
die;