hi,

the problem:
if the comment are showed on multiple pages then after the comment post the user will be redirected to the first page of the comments and not the last! also if the user edit one comment, after the post the redirect will be the same.

i wrote a "15min" module, it works fine (after new comment or edited comment post the user will be redirected to the correct place) but just fast tested... please, check it and if you like it implement it to advanced forum!

regards,
Andrew

the code is:

<?php

//Redirect Comment Form 5.x-1-x-dev

// hook_form_alter
function rcf_form_alter($form_id, &$form) {
  //is it a comment form?
  if ($form_id == 'comment_form') {
    //is the comment posted to forum node?
    if (rcf_is_forum($form['nid']['#value'])) {
      //is the comment an editet comment or is it new post?
      if ($form['cid']['#value']) {
        //it is an edited comment
        $redirect_url = array('node/'.$form['nid']['#value'], rcf_get_current_page_query($form['nid']['#value'], $form['cid']['#value']), 'comment-'.$form['cid']['#value']);
      } else {
        //it is a new comment
        $redirect_url = array('node/'.$form['nid']['#value'], rcf_get_last_page_query($form['nid']['#value']), 'new');
      }
      //set up the form redirect destination
      $form['#redirect'] = $redirect_url;
    }
  }
}

// check is the node forum or not?
function rcf_is_forum($nid) {
  $result = db_result(db_query('SELECT type FROM {node} WHERE nid = %d', $nid));
  if ($result == 'forum') {
    return true;
  } else {
    return false;
  }
}

//check total number of comments
function rcf_comment_count($nid) {
//the query
  $result = db_result(db_query('SELECT COUNT(c.cid) FROM {comments} c WHERE c.nid = %d AND c.status = %d', $nid, COMMENT_PUBLISHED));
  return $result;
}

//check number of previous comments
function rcf_previous_comment_count($nid, $cid) {
  $result = db_result(db_query('SELECT COUNT(c.cid) FROM {comments} c WHERE c.nid = %d AND c.cid < %d AND c.status = %d', $nid, $cid, COMMENT_PUBLISHED));
  return $result;
}

//bulid page query to the last comment page
function rcf_get_last_page_query($nid) {
  $page_num = floor((rcf_comment_count($nid) + 1) / variable_get('comment_default_per_page', 50));
  if ($page_num) {
    return 'page='.$page_num;
  } else {
    return null;
  }
}

//bulid page query to that page where the edited comment is
function rcf_get_current_page_query($nid, $cid) {
  $page_num = floor(rcf_previous_comment_count($nid, $cid) / variable_get('comment_default_per_page', 50));
  if ($page_num) {
    return 'page='.$page_num;
  } else {
    return null;
  }
}
?>

Comments

michelle’s picture

Status: Active » Needs review

I'll test it out but I've got some other things I need to take care of first in the module.

Thanks,

Michelle

naheemsays’s picture

Status: Needs review » Needs work

Not tested, but two comments -

1. this module may be useful in its own right for all nodes - not just those in a forum. Maybe it should be its own module? in this case you should remove the rcf_is_forum check and function.

2. If it is just for forums and you want this as part of advanced_forum module, the rcf_is_forum will not work correctly in Drupal 6 - all node types are allowed in the forums. You need to check by vocabulary.

Advanced forum already has an _is_forum function which you may want to use instead of coding another one. That way this will only need to be fixed in one place.

There is already an issue to fix the _is_forum function for Drupal 6: #232513: theme all forum nodes, not just those with nodetype of forum

EDIT - just a word of warning, but I am planning to borrow and adapt your function "rcf_previous_comment_count" for phpbb_redirect. (a module bundled with phpbb2drupal)

andrew_kabai’s picture

"1. this module may be useful in its own right for all nodes - not just those in a forum. Maybe it should be its own module? in this case you should remove the rcf_is_forum check and function."

yes, it could be, but i just code it fast and currently it's working well if the comment order is "oldest first" and the display is "flat", that is why i just post it to here (and because i love advanced forum:).

of course with some other lines the code can handle the other cases too, maybe soon as a new module...

"2. If it is just for forums and you want this as part of advanced_forum module, the rcf_is_forum will not work correctly in Drupal 6 - all node types are allowed in the forums. You need to check by vocabulary."

okay, currently i'm just developing under 5.x and don't know a lot of abou 6.x, but Michelle could change the code as she want!

"Advanced forum already has an _is_forum function which you may want to use instead of coding another one. That way this will only need to be fixed in one place."

i know, it is because of the code is a standalone module, but your view is right, if the code will adapt to advanced forum then my rcf_is_forum() is needless!

"EDIT - just a word of warning, but I am planning to borrow and adapt your function "rcf_previous_comment_count" for phpbb_redirect. (a module bundled with phpbb2drupal)"

i'd love to! so just do it! :)

naheemsays’s picture

Here's your shout out:

http://drupal.org/cvs?commit=105715

(sorry to Michelle for polluting her issue queue. I'll try to be good in the future.)

michelle’s picture

No problem. I appreciate you guys helping. This is on my to do list.

Michelle

maulwuff’s picture

I did something similar for my version. But I hacked the comment module for this. (it's ok for me, because there are no comments on other nodes on my site)
some parts are from advanced forum.

function comment_form_submit($form_id, $form_values) {
  $form_values = _comment_form_submit($form_values);
  if ($cid = comment_save($form_values)) {
#mod start
    #return array('node/'. $form_values['nid'], NULL, "comment-$cid");
    $comments_per_page = _comment_get_display_setting('comments_per_page');
    $count = db_result(db_query('SELECT COUNT(c.cid) FROM {comments} c WHERE c.nid=%d', $form_values['nid']));
    // Find the ending point. The pager URL is always 1 less than
    // the number being displayed because the first page is 0.
        $last_display_page = ceil($count / $comments_per_page);
        $last_pager_page = $last_display_page - 1;

    return array('node/'. $form_values['nid'], 'page='.$last_pager_page, "comment-$cid");
#mod end
  }
}


It only interferes the flow if a user has "newest posts first". My users are used to have "oldest posts first" for years, so I don't bother. If someone has something generic which looks at the sort order, I'd be happy, too. :) (Have no clue on that, though)

maulwuff’s picture

I had a short look at the code from andrew_kabai (starting this issue)
If a user is allowed to alter the number of posts per page, the code will fail.
The code uses
variable_get('comment_default_per_page', 50)

Maybe it would be more generic by using 'comments_per_page'.
But that does not solve the overall "go to right place after responding"-problem, neither. There has to be a check for the sorting order, too. :/

catch’s picture

This was fixed in Drupal 6, so ideally the same logic could be used for 5: http://drupal.org/node/187391 - I also think it's worth a module in it's own right since it's a problem not just for forums.

rdilauro’s picture

I have a basic question. I've been running several PunBB forum sites and am comfortable with updating or writing some basic PHP code. I was used to the Install.txt file that came with any of the PunBB Packages.
So, my question is is there any set of instructions on how to install this new code AND where does it get installed? In an existing .php file?
Once, I get started and know where to put things, will be fine

Thanks

naheemsays’s picture

The above code should work as a stand alone module.

Create a folder called rcf, a file called rcf.module inside it and paste the above code. create a second file called rcf.info and put in it what info files generally contain (have a look at another) and put that in there. Move the folder to sites/all/modules, and it should show up on your modules page ready for you to enable.

michelle’s picture

Status: Needs work » Closed (won't fix)

This has been hanging out here for a while and I wasn't sure what to do with it. I've finally decided that it's not something that should be part of AF because it's not just a forum issue but rather affects the comment system as a whole. Plus, it's fixed in D6 and I'm winding down the amount of time I'm spending on D5 functionality that isn't needed in D6.

Michelle

maulwuff’s picture

sorry for posting this in here, but i just finished this.

for those of you (or maybe michelle ;) ) looking for a SIMPLE "D5-patch", finally here is it. (note to self: never ever set eaccelerator.check_mtime=0 as it makes you go crazy for sure and steals tons of time to find this)

It works with alpha12

just add this function to the advanced_forum.module-file at the end:

function advanced_forum_form_alter($form_id, &$form)
{
  if($form_id != 'comment_form')
  {
    return;
  }

  $type = db_result(db_query('SELECT type FROM {node} n WHERE n.nid=%d', $form['nid']['#value']));
  if($type == 'forum')
  {
    $comments_per_page = _comment_get_display_setting('comments_per_page');
    $count = db_result(db_query('SELECT COUNT(nid) FROM {comments} WHERE nid=%d', $form['nid']['#value']));

    $last_display_page = ceil($count / $comments_per_page);
    $last_pager_page = $last_display_page - 1;

    if($form['cid']['#value'] == '')
    { //new comment
      $redirect_url = array('node/'.$form['nid']['#value'], 'page='.$last_pager_page, 'new');
    }
    else
    { //edit comment
      $redirect_url = array('node/'.$form['nid']['#value'], 'page='.$last_pager_page, 'comment-'.$form['cid']['#value']);
    }

     $form['#redirect'] = $redirect_url;
  }
}

advantages:
- one simple function
- no extra module
- no core-hack
- it fits advanced forum, because advanced forum advices users to set commentlist to flat / use flatcomments plus to set oldes comment first. (so the newest comment is on the last page)

michelle’s picture

Version: 5.x-1.0-alpha4 » 5.x-1.x-dev
Status: Closed (won't fix) » Active

That seems pretty simple. I'll take a look at it. Thanks.

Michelle

michelle’s picture

Status: Active » Closed (won't fix)

Sorry to be so wishy washy on this one. It's tempting to add because there's not much to it. But my gut keeps telling me that this doesn't belong in AF. This is a core wide problem that affects all node types. To hack in a solution just for forums, especially when AF doesn't deal with the editing end in any other way, just feels wrong. The code is there if people want to use it but I'm not going to add it. This is fixed in D6 so will become a non issue after a while anyway.

Michelle

aharown07’s picture

Oops. Wrong issue. Deleted my post.