Skip to content
Browse files

New function to update the last post in a given discussion.

Called whenever deleting posts or splitting discussions - bug 3050 (SE).
  • Loading branch information...
1 parent dba1f6b commit 1da8c5685a5e7706df3c41ffacd412ca673b621e moodler committed Apr 28, 2005
Showing with 45 additions and 12 deletions.
  1. +42 −2 mod/forum/lib.php
  2. +3 −10 mod/forum/post.php
View
44 mod/forum/lib.php
@@ -2383,11 +2383,13 @@ function forum_delete_discussion($discussion) {
function forum_delete_post($post, $children=false) {
- if ($children) {
- if ($childposts = get_records('forum_posts', 'parent', $post->id)) {
+ if ($childposts = get_records('forum_posts', 'parent', $post->id)) {
+ if ($children) {
foreach ($childposts as $childpost) {
forum_delete_post($childpost, true);
}
+ } else {
+ return false;
}
}
if (delete_records("forum_posts", "id", $post->id)) {
@@ -2401,6 +2403,10 @@ function forum_delete_post($post, $children=false) {
$post->forum = $discussion->forum;
forum_delete_old_attachments($post);
}
+
+ /// Just in case we are deleting the last post
+ forum_discussion_update_last_post($post->discussion);
+
return true;
}
return false;
@@ -3502,5 +3508,39 @@ function forum_tp_clean_read_records() {
}
}
+
+
+/**
+ * Sets the last post for a given discussion
+ **/
+function forum_discussion_update_last_post($discussionid) {
+ global $CFG, $db;
+
+/// Check the given discussion exists
+ if (!record_exists('forum_discussions', 'id', $discussionid)) {
+ return false;
+ }
+
+/// Use SQL to find the last post for this discussion
+ $sql = 'SELECT `id`, `userid`, `modified` '.
+ 'FROM '.$CFG->prefix.'forum_posts '.
+ 'WHERE `discussion`='.$discussionid.' '.
+ 'ORDER BY `modified` DESC ';
+
+/// Lets go find the last post
+ if (($lastpost = get_record_sql($sql, true))) {
+ $discussionobject = new Object;
+ $discussionobject->id = $discussionid;
+ $discussionobject->usermodified = $lastpost->userid;
+ $discussionobject->timemodified = $lastpost->modified;
+ if (update_record('forum_discussions', $discussionobject)) {
+ return $lastpost->id;
+ }
+ }
+
+/// To get here either we couldn't find a post for the discussion (weird)
+/// or we couldn't update the discussion record (weird x2)
+ return false;
+}
?>
View
13 mod/forum/post.php
@@ -465,16 +465,9 @@
forum_change_discussionid($post->id, $newid);
- // set timemodified to time of last post in each discussion
- $lastpost = get_record_sql("SELECT MAX(modified) AS time
- FROM {$CFG->prefix}forum_posts
- WHERE discussion = '$discussion->id'");
- set_field('forum_discussions', 'timemodified', $lastpost->time, 'id', $discussion->id);
- $lastpost = get_record_sql("SELECT MAX(modified) AS time
- FROM {$CFG->prefix}forum_posts
- WHERE discussion = '$newid'");
- set_field('forum_discussions', 'timemodified', $lastpost->time, 'id', $newid);
-
+ // update last post in each discussion
+ forum_discussion_update_last_post($discussion->id);
+ forum_discussion_update_last_post($newid);
add_to_log($discussion->course, "forum", "prune post",
"discuss.php?d=$newid", "$post->id", $cm->id);

0 comments on commit 1da8c56

Please sign in to comment.
Something went wrong with that request. Please try again.