]> git.mjollnir.org Git - moodle.git/commitdiff
Much improved forum searching, with independent words and paging
authormoodler <moodler>
Sat, 9 Aug 2003 05:35:02 +0000 (05:35 +0000)
committermoodler <moodler>
Sat, 9 Aug 2003 05:35:02 +0000 (05:35 +0000)
lang/en/forum.php
mod/forum/lib.php
mod/forum/search.php

index 6e06a449c6f21b74c5f1b22af27d960908d80716..88539e1dabefb67ac0f9c1b8e296f8449292c91e 100644 (file)
@@ -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";
index c2a2984054339c8111d16d3a70f2bf1a0e9d0ffb..be1a35da9f8ceb8ad153cb39455f167ece9b773b 100644 (file)
@@ -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' 
index 3218d5805beff96c2813ca8e8041fe01306f6161..96777157b5d2b0b418ed356cdad6b3709ff47228 100644 (file)
@@ -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.");
     }
 
     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 = "<a href=\"view.php?f=$forum->id\">$forum->name</a>";
                 if ($forum->type != "single") {
                 $fulllink = "<p align=\"right\"><a href=\"discuss.php?d=$post->discussion&parent=$post->id\">".get_string("postincontext", "forum")."</a></p>";
                 forum_print_post($post, $course->id, false, false, false, false, $fulllink, $search);
 
-                echo "<BR>";
+                echo "<br />";
             }
         }
+
+        if (count($posts) == $perpage) {
+            $options = array();
+            $options["id"] = $course->id;
+            $options["search"] = urlencode($search);
+            $options["page"] = $page+1;
+            $options["perpage"] = $perpage;
+            echo "<center>";
+            print_single_button("search.php", $options, get_string("searcholderposts", "forum"));
+            echo "</center>";
+        }
     }
 
     print_footer($course);