Index: discuss.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/forum/discuss.php,v retrieving revision 1.123 diff -u -r1.123 discuss.php --- discuss.php 5 Jun 2008 20:16:10 -0000 1.123 +++ discuss.php 3 Jul 2008 23:21:56 -0000 @@ -6,12 +6,18 @@ require_once('../../config.php'); require_once('lib.php'); - $d = required_param('d', PARAM_INT); // Discussion ID - $parent = optional_param('parent', 0, PARAM_INT); // If set, then display this post and all children. - $mode = optional_param('mode', 0, PARAM_INT); // If set, changes the layout of the thread - $move = optional_param('move', 0, PARAM_INT); // If set, moves this discussion to another forum - $mark = optional_param('mark', '', PARAM_ALPHA); // Used for tracking read posts if user initiated. - $postid = optional_param('postid', 0, PARAM_INT); // Used for tracking read posts if user initiated. + $d = required_param('d', PARAM_INT); // Discussion ID + $parent = optional_param('parent', 0, PARAM_INT); // If set, then display this post and all children. + $mode = optional_param('mode', 0, PARAM_INT); // If set, changes the layout of the thread + $move = optional_param('move', 0, PARAM_INT); // If set, moves this discussion to another forum + $mark = optional_param('mark', '', PARAM_ALPHA); // Used for tracking read posts if user initiated. + $postid = optional_param('postid', 0, PARAM_INT); // Used for tracking read posts if user initiated. + $perpage = optional_param('perpage', 30, PARAM_INT); // Maximum number of posts per page. + $page = optional_param('page', 0, PARAM_INT); // Page to display. + + if ($perpage < 1) { + $perpage = 1; + } if (!$discussion = $DB->get_record('forum_discussions', array('id' => $d))) { error("Discussion ID was incorrect or no longer exists"); @@ -219,7 +225,7 @@ } $canrate = has_capability('mod/forum:rate', $modcontext); - forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate); + forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate, $page, $perpage); print_footer($course); Index: lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/forum/lib.php,v retrieving revision 1.680 diff -u -r1.680 lib.php --- lib.php 15 Jun 2008 10:12:27 -0000 1.680 +++ lib.php 3 Jul 2008 23:21:57 -0000 @@ -4825,9 +4825,9 @@ /** - * + * $post is the first post in the discussion */ -function forum_print_discussion($course, $cm, $forum, $discussion, $post, $mode, $canreply=NULL, $canrate=false) { +function forum_print_discussion($course, $cm, $forum, $discussion, $post, $mode, $canreply=NULL, $canrate=false, $page=0, $perpage=100) { global $USER, $CFG, $DB; @@ -4857,7 +4857,7 @@ $sort = "p.created ASC"; } - $forumtracked = forum_tp_is_tracked($forum); + $forumtracked = forum_tp_is_tracked($forum); $posts = forum_get_all_discussion_posts($discussion->id, $sort, $forumtracked); $post = $posts[$post->id]; @@ -4913,33 +4913,23 @@ $post->forum = $forum->id; // Add the forum id to the post object, later used by forum_print_post $post->forumtype = $forum->type; - - $post->subject = format_string($post->subject); - - $postread = !empty($post->postread); - - if (forum_print_post($post, $discussion, $forum, $cm, $course, $ownpost, $reply, false, $ratings, - '', '', $postread, true, $forumtracked)) { - $ratingsmenuused = true; - } - switch ($mode) { case FORUM_MODE_FLATOLDEST : case FORUM_MODE_FLATNEWEST : default: - if (forum_print_posts_flat($course, $cm, $forum, $discussion, $post, $mode, $ratings, $reply, $forumtracked, $posts)) { + if (forum_print_posts_flat($course, $cm, $forum, $discussion, $post, $mode, $ratings, $reply, $forumtracked, $posts, $page, $perpage)) { $ratingsmenuused = true; } break; case FORUM_MODE_THREADED : - if (forum_print_posts_threaded($course, $cm, $forum, $discussion, $post, 0, $ratings, $reply, $forumtracked, $posts)) { + if (forum_print_posts_threaded($course, $cm, $forum, $discussion, $post, $mode, $ratings, $reply, $forumtracked, $posts)) { $ratingsmenuused = true; } break; case FORUM_MODE_NESTED : - if (forum_print_posts_nested($course, $cm, $forum, $discussion, $post, $ratings, $reply, $forumtracked, $posts)) { + if (forum_print_posts_nested($course, $cm, $forum, $discussion, $post, $mode, $ratings, $reply, $forumtracked, $posts, $page, $perpage)) { $ratingsmenuused = true; } break; @@ -4966,7 +4956,7 @@ /** * */ -function forum_print_posts_flat($course, &$cm, $forum, $discussion, $post, $mode, $ratings, $reply, $forumtracked, $posts) { +function forum_print_posts_flat($course, &$cm, $forum, $discussion, $post, $mode, $ratings, $reply, $forumtracked, $posts, $page, $perpage) { global $USER, $CFG; $link = false; @@ -4978,10 +4968,10 @@ $sort = "ORDER BY created ASC"; } - foreach ($posts as $post) { - if (!$post->parent) { - continue; - } + $totalposts = count($posts); + $posts = array_slice($posts, $perpage * $page, $perpage); + + foreach ($posts as $post) { $post->subject = format_string($post->subject); $ownpost = ($USER->id == $post->userid); @@ -4993,6 +4983,7 @@ } } + print_paging_bar($totalposts, $page, $perpage, "discuss.php?d=$discussion->id&mode=$mode&perpage=$perpage&"); return $ratingsmenuused; } @@ -5000,7 +4991,7 @@ /** * TODO document */ -function forum_print_posts_threaded($course, &$cm, $forum, $discussion, $parent, $depth, $ratings, $reply, $forumtracked, $posts) { +function forum_print_posts_threaded_aux($course, &$cm, $forum, $discussion, $parent, $depth, $ratings, $reply, $forumtracked, $posts) { global $USER, $CFG; $link = false; @@ -5016,6 +5007,7 @@ echo '
'; if ($depth > 0) { + // TODO What is this supposed to do? depth is always negative $ownpost = ($USER->id == $post->userid); $post->subject = format_string($post->subject); @@ -5048,7 +5040,7 @@ echo ""; } - if (forum_print_posts_threaded($course, $cm, $forum, $discussion, $post, $depth-1, $ratings, $reply, $forumtracked, $posts)) { + if (forum_print_posts_threaded_aux($course, $cm, $forum, $discussion, $post, $depth-1, $ratings, $reply, $forumtracked, $posts)) { $ratingsmenuused = true; } echo "
\n"; @@ -5057,35 +5049,55 @@ return $ratingsmenuused; } +function forum_print_posts_threaded($course, &$cm, $forum, $discussion, $parent, $mode, $ratings, $reply, $forumtracked, $posts) { + $parent->subject = format_string($post->subject); + $postread = !empty($post->postread); + if (forum_print_post($parent, $discussion, $forum, $cm, $course, $ownpost, $reply, false, $ratings, + '', '', $postread, true, $forumtracked)) { + $ratingsmenuused = true; + } + forum_print_posts_threaded_aux($course, &$cm, $forum, $discussion, $parent, 0, $ratings, $reply, $forumtracked, $posts); +} + + /** - * + * TODO document + * @param int $postindex index of the post in an pre-order traversal + * @param int $firstinpage index of the first post to display in this page + * @param int $lastinpage index following the last post to display in this page */ -function forum_print_posts_nested($course, &$cm, $forum, $discussion, $parent, $ratings, $reply, $forumtracked, $posts) { +function forum_print_posts_nested_aux($course, &$cm, $forum, $discussion, $parent, $ratings, $reply, $forumtracked, $posts, &$postindex, + $firstinpage, $lastinpage) { global $USER, $CFG; $link = false; $ratingsmenuused = false; + if ($postindex >= $firstinpage && $postindex < $lastinpage) { + // Display parent post + if (empty($USER->id)) { + $ownpost = false; + } else { + $ownpost = ($USER->id == $parent->userid); + } + $parent->subject = format_string($parent->subject); + $postread = !empty($parent->postread); + if (forum_print_post($parent, $discussion, $forum, $cm, $course, $ownpost, $reply, false, $ratings, + '', '', $postread, true, $forumtracked)) { + $ratingsmenuused = true; + } + } + ++$postindex; + + // Display all the children if (!empty($posts[$parent->id]->children)) { $posts = $posts[$parent->id]->children; + $numposts += count($posts); foreach ($posts as $post) { - echo '
'; - if (empty($USER->id)) { - $ownpost = false; - } else { - $ownpost = ($USER->id == $post->userid); - } - - $post->subject = format_string($post->subject); - $postread = !empty($post->postread); - - if (forum_print_post($post, $discussion, $forum, $cm, $course, $ownpost, $reply, $link, $ratings, - '', '', $postread, true, $forumtracked)) { - $ratingsmenuused = true; - } - if (forum_print_posts_nested($course, $cm, $forum, $discussion, $post, $ratings, $reply, $forumtracked, $posts)) { + if (forum_print_posts_nested_aux($course, $cm, $forum, $discussion, $post, $ratings, $reply, $forumtracked, $posts, $postindex, + $firstinpage, $lastinpage)) { $ratingsmenuused = true; } echo "
\n"; @@ -5094,6 +5106,13 @@ return $ratingsmenuused; } +function forum_print_posts_nested($course, &$cm, $forum, $discussion, $parent, $mode, $ratings, $reply, $forumtracked, $posts, $page, $perpage) { + $postindex = 0; + forum_print_posts_nested_aux($course, $cm, $forum, $discussion, $parent, $ratings, $reply, $forumtracked, $posts, $postindex, + $page * $perpage, ($page + 1) * $perpage); + print_paging_bar($postindex, $page, $perpage, "discuss.php?d=$discussion->id&mode=$mode&perpage=$perpage&"); +} + /** * Returns all forum posts since a given time in specified forum. */