Better behaviour for "-" course searches under MSSQL and Oracle. MDL-7312 ; merged...
authorstronk7 <stronk7>
Tue, 1 Jan 2008 12:03:00 +0000 (12:03 +0000)
committerstronk7 <stronk7>
Tue, 1 Jan 2008 12:03:00 +0000 (12:03 +0000)
lib/datalib.php

index 06450d9a9e4764de21027978291e458e6a4a2ad7..f9c22dac140cc9d8a8f97eac0aa231d4486e0893 100644 (file)
@@ -1058,9 +1058,15 @@ function get_courses_search($searchterms, $sort='fullname ASC', $page=0, $record
 
     foreach ($searchterms as $searchterm) {
 
+        $NOT = ''; /// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle
+                   /// will use it to simulate the "-" operator with LIKE clause
+
     /// Under Oracle and MSSQL, trim the + and - operators and perform
-    /// simpler LIKE search
+    /// simpler LIKE (or NOT LIKE) queries
         if ($CFG->dbfamily == 'oracle' || $CFG->dbfamily == 'mssql') {
+            if (substr($searchterm, 0, 1) == '-') {
+                $NOT = ' NOT ';
+            }
             $searchterm = trim($searchterm, '+-');
         }
 
@@ -1080,8 +1086,8 @@ function get_courses_search($searchterms, $sort='fullname ASC', $page=0, $record
             $summarysearch  .= " c.summary $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
             $fullnamesearch .= " c.fullname $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
         } else {
-            $summarysearch  .= ' c.summary '. $LIKE .' \'%'. $searchterm .'%\' ';
-            $fullnamesearch .= ' c.fullname '. $LIKE .' \'%'. $searchterm .'%\' ';
+            $summarysearch .= ' summary '. $NOT . $LIKE .' \'%'. $searchterm .'%\' ';
+            $fullnamesearch .= ' fullname '. $NOT . $LIKE .' \'%'. $searchterm .'%\' ';
         }
 
     }
@@ -1092,7 +1098,7 @@ function get_courses_search($searchterms, $sort='fullname ASC', $page=0, $record
             FROM {$CFG->prefix}course c
             JOIN {$CFG->prefix}context ctx
              ON (c.id = ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
-            WHERE ( $fullnamesearch OR  $summarysearch )
+            WHERE (( $fullnamesearch ) OR ( $summarysearch ))
                   AND category > 0
             ORDER BY " . $sort;