diff --git a/lang/en/forum.php b/lang/en/forum.php
index e57fa4b9a91..6a67c61135c 100644
--- a/lang/en/forum.php
+++ b/lang/en/forum.php
@@ -138,7 +138,15 @@ $string['replyforum'] = 'Reply to forum';
$string['rsssubscriberssdiscussions'] = 'Display the RSS feed for \'$a\' discussions';
$string['rsssubscriberssposts'] = 'Display the RSS feed for \'$a\' posts';
$string['search'] = 'Search';
+$string['searchforumintro'] = 'Please enter search terms into one or more of the following fields:';
$string['searchforums'] = 'Search forums';
+$string['searchfullwords'] = 'These words should appear as whole words';
+$string['searchnotwords'] = 'These words should NOT be included';
+$string['searchphrase'] = 'This exact phrase must appear in the post';
+$string['searchsubject'] = 'These words should be in the subject';
+$string['searchuser'] = 'This name should match the author';
+$string['searchuserid'] = 'The Moodle ID of the author';
+$string['searchwords'] = 'These words can appear anywhere in the post';
$string['searcholderposts'] = 'Search older posts...';
$string['searchresults'] = 'Search results';
$string['sendinratings'] = 'Send in my latest ratings';
diff --git a/lib/searchlib.php b/lib/searchlib.php
index 3b55a6e0fcc..4738a09fc36 100644
--- a/lib/searchlib.php
+++ b/lib/searchlib.php
@@ -9,6 +9,7 @@ define("TOKEN_META","1");
define("TOKEN_EXACT","2");
define("TOKEN_NEGATE","3");
define("TOKEN_STRING","4");
+define("TOKEN_USERID","5");
// Class to hold token/value pairs after they're parsed.
@@ -50,6 +51,17 @@ class search_lexer extends Lexer{
//Set up the state machine and pattern matches for transitions.
+ // Patterns to handle strings of the form userid:foo
+
+ // If we see the string user: while in the base accept state, start
+ // parsing a username and go to the inuserid state.
+ $this->addEntryPattern("userid:\S+","accept","inuserid");
+
+ // Snarf everything into the username until we see whitespace, then exit
+ // back to the base accept state.
+ $this->addExitPattern("\s","inuserid");
+
+
// Patterns to handle strings of the form user:foo
// If we see the string user: while in the base accept state, start
@@ -60,6 +72,7 @@ class search_lexer extends Lexer{
// back to the base accept state.
$this->addExitPattern("\s","inusername");
+
// Patterns to handle strings of the form meta:foo
// If we see the string meta: while in the base accept state, start
@@ -138,6 +151,17 @@ class search_parser {
return true;
}
+ // State for handling userid:foo constructs. Potentially emits a token.
+ function inuserid($content){
+ if (strlen($content) < 8) { // State exit or missing parameter.
+ return true;
+ }
+ // Strip off the userid: part and add the reminder to the parsed token array
+ $param = trim(substr($content,7));
+ $this->tokens[] = new search_token(TOKEN_USERID,$param);
+ return true;
+ }
+
// State for handling user:foo constructs. Potentially emits a token.
function inusername($content){
@@ -262,6 +286,9 @@ function search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $
case TOKEN_USER:
$SQLString .= "(($mainidfield = $useridfield) AND (($userfirstnamefield $LIKE '%$value%') OR ($userlastnamefield $LIKE '%$value%')))";
break;
+ case TOKEN_USERID:
+ $SQLString .= "($useridfield = $value)";
+ break;
case TOKEN_NEGATE:
$SQLString .= "(NOT (($datafield $LIKE '%$value%') OR ($metafield $LIKE '%$value%')))";
break;
diff --git a/mod/forum/lib.php b/mod/forum/lib.php
index 1d90e8662d9..4709306e53a 100644
--- a/mod/forum/lib.php
+++ b/mod/forum/lib.php
@@ -1002,16 +1002,7 @@ function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50
$onlyvisibletable = "";
}
- switch ($CFG->dbtype) {
- case "mysql":
- $limit = "LIMIT $page,$recordsperpage";
- break;
- case "postgres7":
- $limit = "LIMIT $recordsperpage OFFSET ".($page * $recordsperpage);
- break;
- default:
- $limit = "LIMIT $recordsperpage,$page";
- }
+ $limit = sql_paging_limit($page, $recordsperpage);
/// Some differences in syntax for PostgreSQL
if ($CFG->dbtype == "postgres7") {
@@ -1041,6 +1032,7 @@ function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50
$searchstring = str_replace("\\\"","\"",$searchstring);
$parser = new search_parser();
$lexer = new search_lexer(&$parser);
+
if ($lexer->parse($searchstring)) {
$parsearray = $parser->get_parsed_array();
$messagesearch = search_generate_SQL($parsearray,"p.message","p.subject","p.userid","u.id","u.firstname","u.lastname");
@@ -3326,12 +3318,12 @@ function forum_tp_clean_read_records() {
$sql = 'SELECT fr.id, fr.userid, fr.postid '.
'FROM '.$CFG->prefix.'forum_posts fp, '.$CFG->prefix.'forum_read fr '.
'WHERE fp.modified < '.$cutoffdate.' AND fp.id = fr.postid';
-echo $sql.'
';
if (($oldreadposts = get_records_sql($sql))) {
-echo 'Deleting records: ';print_object($oldreadposts);
foreach($oldreadposts as $oldreadpost) {
delete_records('forum_read', 'userid', $oldreadpost->userid, 'postid', $oldreadpost->postid);
}
}
-}
+
+}
+
?>
diff --git a/mod/forum/search.php b/mod/forum/search.php
index e27155e1501..e2f4cd0bb79 100644
--- a/mod/forum/search.php
+++ b/mod/forum/search.php
@@ -3,21 +3,49 @@
require_once("../../config.php");
require_once("lib.php");
- 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
+ $id = required_param('id', PARAM_INT); // course id
+ $search = trim(optional_param('search', '', PARAM_NOTAGS)); // search string
+ $page = optional_param('page', 0, PARAM_INT); // which page to show
+ $perpage = optional_param('perpage', 20, PARAM_INT); // which page to show
- $search = trim(strip_tags($search));
+ $user = trim(optional_param('user', '', PARAM_NOTAGS)); // Names to search for
+ $userid = trim(optional_param('userid', 0, PARAM_INT)); // UserID to search for
+ $subject = trim(optional_param('subject', '', PARAM_NOTAGS)); // Subject
+ $phrase = trim(optional_param('phrase', '', PARAM_NOTAGS)); // Phrase
+ $words = trim(optional_param('words', '', PARAM_NOTAGS)); // Words
+ $fullwords = trim(optional_param('fullwords', '', PARAM_NOTAGS)); // Whole words
+ $notwords = trim(optional_param('notwords', '', PARAM_NOTAGS)); // Words we don't want
+
+
+ if (empty($search)) { // Check the other parameters instead
+ if (!empty($words)) {
+ $search .= ' '.$words;
+ }
+ if (!empty($userid)) {
+ $search .= ' userid:'.$userid;
+ }
+ if (!empty($user)) {
+ $search .= ' '.forum_clean_search_terms($user, 'user:');
+ }
+ if (!empty($subject)) {
+ $search .= ' '.forum_clean_search_terms($subject, 'subject:');
+ }
+ if (!empty($fullwords)) {
+ $search .= ' '.forum_clean_search_terms($fullwords, '+');
+ }
+ if (!empty($notwords)) {
+ $search .= ' '.forum_clean_search_terms($notwords, '-');
+ }
+ if (!empty($phrase)) {
+ $search .= ' "'.$phrase.'"';
+ }
+ $individualparams = true;
+ } else {
+ $individualparams = false;
+ }
if ($search) {
- $searchterms = explode(" ", $search); // Search for words independently
- foreach ($searchterms as $key => $searchterm) {
- if (strlen($searchterm) < 2) {
- unset($searchterms[$key]);
- }
- }
- $search = s(trim(implode(" ", $searchterms)));
+ $search = forum_clean_search_terms($search);
}
if (! $course = get_record("course", "id", $id)) {
@@ -32,135 +60,189 @@
$strsearch = get_string("search", "forum");
$strsearchresults = get_string("searchresults", "forum");
$strpage = get_string("page");
- $strmissingsearchterms = get_string('missingsearchterms','forum');
-
- $searchform = forum_print_search_form($course, $search, true, "navbar");
if (!$search) {
print_header_simple("$strsearch", "",
- "id\">$strforums -> $strsearch", "search.search",
+ "id\">$strforums -> $strsearch", 'search.words',
"", "", " ", navmenu($course));
- print_simple_box_start("center");
- echo "
"; - print_string("searchhelp"); - echo "
"; - echo ""; - print_string("searchhelp"); - echo "
"; - echo "'.$strmissingsearchterms.' '.$missing_terms.'
'.$message; - } - - $fulllink = "discussion#$post->id\">".get_string("postincontext", "forum").""; - //search terms already highlighted - fiedorow - 9/2/2005 - forum_print_post($post, $course->id, false, false, false, false, $fulllink); - - echo "'.$strmissingsearchterms.' '.$missing_terms.'
'.$message; + } + + $fulllink = "discussion#$post->id\">".get_string("postincontext", "forum").""; + //search terms already highlighted - fiedorow - 9/2/2005 + forum_print_post($post, $course->id, false, false, false, false, $fulllink); + + echo "