From b800ac5a1097bf4f55744546631f42365865f7cc Mon Sep 17 00:00:00 2001 From: moodler Date: Sat, 9 Aug 2003 05:35:02 +0000 Subject: [PATCH] Much improved forum searching, with independent words and paging --- lang/en/forum.php | 2 ++ mod/forum/lib.php | 22 ++++++++++++++++++++-- mod/forum/search.php | 34 +++++++++++++++++++++++++++------- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/lang/en/forum.php b/lang/en/forum.php index 6e06a449c6..88539e1dab 100644 --- a/lang/en/forum.php +++ b/lang/en/forum.php @@ -65,6 +65,7 @@ $string['newforumposts'] = "New forum posts"; $string['nodiscussions'] = "There are no discussion topics yet in this forum"; $string['noguestpost'] = "Sorry, guests are not allowed to post"; $string['nonews'] = "No news has been posted yet"; +$string['nomorepostscontaining'] = "No more posts containing '\$a' were found"; $string['noposts'] = "No posts"; $string['nopostscontaining'] = "No posts containing '\$a' were found"; $string['nosubscribers'] = "There are no subscribers yet for this forum"; @@ -96,6 +97,7 @@ $string['repliesmany'] = "\$a replies so far"; $string['repliesone'] = "\$a reply so far"; $string['reply'] = "Reply"; $string['search'] = "Search"; +$string['searcholderposts'] = "Search older posts..."; $string['searchresults'] = "Search results"; $string['searchforums'] = "Search forums"; $string['sendinratings'] = "Send in my latest ratings"; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index c2a2984054..be1a35da9f 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -498,16 +498,34 @@ function forum_search_posts($search, $courseid, $page=0, $recordsperpage=50) { //to allow caseinsensitive search for postgesql if($CFG->dbtype == "postgres7") { $LIKE = "ILIKE"; - }else { + } else { $LIKE = "LIKE"; } + $messagesearch = ""; + $subjectsearch = ""; + + $searchterms = explode(" ", $search); // Search for words independently + + foreach ($searchterms as $searchterm) { + if ($messagesearch) { + $messagesearch .= " AND "; + } + $messagesearch .= " p.message $LIKE '%$searchterm%' "; + + if ($subjectsearch) { + $subjectsearch .= " AND "; + } + $subjectsearch .= " p.subject $LIKE '%$searchterm%' "; + } + + return get_records_sql("SELECT p.*,u.firstname,u.lastname,u.email,u.picture FROM {$CFG->prefix}forum_posts p, {$CFG->prefix}forum_discussions d, {$CFG->prefix}user u, {$CFG->prefix}forum f - WHERE (p.message $LIKE '%$search%' OR p.subject $LIKE '%$search%') + WHERE ($messagesearch OR $subjectsearch) AND p.userid = u.id AND p.discussion = d.id AND d.course = '$courseid' diff --git a/mod/forum/search.php b/mod/forum/search.php index 3218d5805b..96777157b5 100644 --- a/mod/forum/search.php +++ b/mod/forum/search.php @@ -5,8 +5,10 @@ require_variable($id); // course id optional_variable($search, ""); // search string + optional_variable($page, "0"); // which page to show + optional_variable($perpage, "20"); // which page to show - $search = strip_tags($search); + $search = trim(strip_tags($search)); if (! $course = get_record("course", "id", $id)) { error("Course id is incorrect."); @@ -38,12 +40,19 @@ } if ($search) { - - if (!$posts = forum_search_posts($search, $course->id)) { - print_heading(get_string("nopostscontaining", "forum", $search)); + + if (!$posts = forum_search_posts($search, $course->id, $page*$perpage, $perpage)) { + if ($page) { + print_heading(get_string("nomorepostscontaining", "forum", $search)); + print_continue("search.php?id=$course->id&search=".urlencode($search)); + } else { + print_heading(get_string("nopostscontaining", "forum", $search)); + } } else { + foreach ($posts as $post) { + if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) { error("Discussion ID was incorrect"); } @@ -51,8 +60,8 @@ error("Could not find forum $discussion->forum"); } - $post->subject = highlightfast("$search", $post->subject); - $discussion->name = highlightfast("$search", $discussion->name); + $post->subject = highlight("$search", $post->subject); + $discussion->name = highlight("$search", $discussion->name); $fullsubject = "id\">$forum->name"; if ($forum->type != "single") { @@ -67,9 +76,20 @@ $fulllink = "

discussion&parent=$post->id\">".get_string("postincontext", "forum")."

"; forum_print_post($post, $course->id, false, false, false, false, $fulllink, $search); - echo "
"; + echo "
"; } } + + if (count($posts) == $perpage) { + $options = array(); + $options["id"] = $course->id; + $options["search"] = urlencode($search); + $options["page"] = $page+1; + $options["perpage"] = $perpage; + echo "
"; + print_single_button("search.php", $options, get_string("searcholderposts", "forum")); + echo "
"; + } } print_footer($course); -- 2.39.5