]> git.mjollnir.org Git - moodle.git/commitdiff
Added (default) option to search for whole words with forums.
authormoodler <moodler>
Thu, 14 Aug 2003 06:34:56 +0000 (06:34 +0000)
committermoodler <moodler>
Thu, 14 Aug 2003 06:34:56 +0000 (06:34 +0000)
(SQL regular expressions from David Delgado - thanks!)

mod/forum/lib.php

index 4528ba341f7b2f3abdc800a2720bc93aafa0af7f..9a866850baaca1fdc7cfdbf490b3be5da3fc3e54 100644 (file)
@@ -474,7 +474,7 @@ function forum_get_child_posts($parent) {
 }
 
 
-function forum_search_posts($search, $courseid, $page=0, $recordsperpage=50) {
+function forum_search_posts($search, $courseid, $page=0, $recordsperpage=50, $wholewords=true) {
 /// Returns a list of posts that were found
     global $CFG;
 
@@ -495,11 +495,13 @@ function forum_search_posts($search, $courseid, $page=0, $recordsperpage=50) {
              $limit = "LIMIT $recordsperpage,$page";
     }
 
-    //to allow caseinsensitive search for postgesql
-    if($CFG->dbtype == "postgres7") {
-       $LIKE = "ILIKE";
+    /// Some differences in syntax for PostgreSQL
+    if ($CFG->dbtype == "postgres7") {
+       $LIKE = "ILIKE";   // case-insensitive
+       $REGEXP = "~";
     } else {
        $LIKE = "LIKE";
+       $REGEXP = "REGEXP";
     }
 
     $messagesearch = "";
@@ -511,12 +513,17 @@ function forum_search_posts($search, $courseid, $page=0, $recordsperpage=50) {
         if ($messagesearch) {
             $messagesearch .= " AND ";
         }
-        $messagesearch .= " p.message $LIKE '%$searchterm%' ";
-
         if ($subjectsearch) {
             $subjectsearch .= " AND ";
         }
-        $subjectsearch .= " p.subject $LIKE '%$searchterm%' ";
+
+        if ($wholewords) {
+            $messagesearch .= " p.message $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
+            $subjectsearch .= " p.subject $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
+        } else {
+            $messagesearch .= " p.message $LIKE '%$searchterm%' ";
+            $subjectsearch .= " p.subject $LIKE '%$searchterm%' ";
+        }
     }
 
 
@@ -534,6 +541,7 @@ function forum_search_posts($search, $courseid, $page=0, $recordsperpage=50) {
                          ORDER BY p.modified DESC $limit");
 }
 
+
 function forum_get_ratings($postid, $sort="u.firstname ASC") {
 /// Returns a list of ratings for a particular post - sorted.
     global $CFG;