From 77ffdf4b4c8fdc3f5317333b471c7abdbaaad66e Mon Sep 17 00:00:00 2001 From: moodler Date: Thu, 3 Mar 2005 12:22:13 +0000 Subject: [PATCH] Finishing off search capability with the ability to search by instance, and in this case, by forum. --- lang/en/forum.php | 2 ++ lib/searchlib.php | 29 ++++++++++++++++++++- mod/forum/lib.php | 2 +- mod/forum/search.php | 61 +++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 91 insertions(+), 3 deletions(-) diff --git a/lang/en/forum.php b/lang/en/forum.php index 63405afb029..5b1b5b08311 100644 --- a/lang/en/forum.php +++ b/lang/en/forum.php @@ -4,6 +4,7 @@ $string['addanewdiscussion'] = 'Add a new discussion topic'; $string['addanewtopic'] = 'Add a new topic'; +$string['allforums'] = 'All forums'; $string['allowchoice'] = 'Allow everyone to choose'; $string['allowdiscussions'] = 'Can a $a post to this forum?'; $string['allowratings'] = 'Allow posts to be rated?'; @@ -151,6 +152,7 @@ $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['searchwhichforums'] = 'Choose which forums to search'; $string['seeallposts'] = 'See all posts made by this user'; $string['sendinratings'] = 'Send in my latest ratings'; $string['showsubscribers'] = 'Show/edit current subscribers'; diff --git a/lib/searchlib.php b/lib/searchlib.php index 0209951a317..d836acbb0b4 100644 --- a/lib/searchlib.php +++ b/lib/searchlib.php @@ -12,6 +12,7 @@ define("TOKEN_STRING","4"); define("TOKEN_USERID","5"); define("TOKEN_DATEFROM","6"); define("TOKEN_DATETO","7"); +define("TOKEN_INSTANCE","8"); // Class to hold token/value pairs after they're parsed. @@ -75,6 +76,17 @@ class search_lexer extends Lexer{ $this->addExitPattern("\s","indateto"); + // Patterns to handle strings of the form instance:foo + + // If we see the string instance: while in the base accept state, start + // parsing for instance number and go to the ininstance state. + $this->addEntryPattern("instance:\S+","accept","ininstance"); + + // Snarf everything into the username until we see whitespace, then exit + // back to the base accept state. + $this->addExitPattern("\s","ininstance"); + + // Patterns to handle strings of the form userid:foo // If we see the string userid: while in the base accept state, start @@ -197,6 +209,18 @@ class search_parser { return true; } + // State for handling instance:foo constructs. Potentially emits a token. + function ininstance($content){ + if (strlen($content) < 10) { // State exit or missing parameter. + return true; + } + // Strip off the instance: part and add the reminder to the parsed token array + $param = trim(substr($content,9)); + $this->tokens[] = new search_token(TOKEN_INSTANCE,$param); + return true; + } + + // State for handling userid:foo constructs. Potentially emits a token. function inuserid($content){ if (strlen($content) < 8) { // State exit or missing parameter. @@ -287,7 +311,7 @@ class search_parser { // Other fields are database table names to search. function search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $useridfield, - $userfirstnamefield, $userlastnamefield, $timefield) { + $userfirstnamefield, $userlastnamefield, $timefield, $instancefield) { global $CFG; if ($CFG->dbtype == "postgres7") { @@ -335,6 +359,9 @@ function search_generate_SQL($parsetree, $datafield, $metafield, $mainidfield, $ case TOKEN_USERID: $SQLString .= "($useridfield = $value)"; break; + case TOKEN_INSTANCE: + $SQLString .= "($instancefield = $value)"; + break; case TOKEN_DATETO: $SQLString .= "($timefield <= $value)"; break; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 5187325442a..45e1f6cca08 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -1035,7 +1035,7 @@ function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50 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','p.modified'); + $messagesearch = search_generate_SQL($parsearray,'p.message','p.subject','p.userid','u.id','u.firstname','u.lastname','p.modified', 'd.forum'); } $selectsql = "{$CFG->prefix}forum_posts p, diff --git a/mod/forum/search.php b/mod/forum/search.php index bfcc8df4b7b..1b0471c1bbf 100644 --- a/mod/forum/search.php +++ b/mod/forum/search.php @@ -10,6 +10,7 @@ $user = trim(optional_param('user', '', PARAM_NOTAGS)); // Names to search for $userid = trim(optional_param('userid', 0, PARAM_INT)); // UserID to search for + $forumid = trim(optional_param('forumid', 0, PARAM_INT)); // ForumID 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 @@ -49,6 +50,9 @@ if (!empty($userid)) { $search .= ' userid:'.$userid; } + if (!empty($forumid)) { + $search .= ' forumid:'.$forumid; + } if (!empty($user)) { $search .= ' '.forum_clean_search_terms($user, 'user:'); } @@ -104,7 +108,8 @@ /// We need to do a search now and print results - $searchterms = explode(' ', $search); + $searchterms = str_replace('forumid:', 'instance:', $search); + $searchterms = explode(' ', $searchterms); $searchform = forum_print_search_form($course, "", true, "navbar"); @@ -127,6 +132,8 @@ } forum_print_big_search_form($course); + + print_footer($course); exit; } @@ -288,6 +295,13 @@ function forum_print_big_search_form($course) { echo ''; echo ''; + echo ''; + echo ''.get_string('searchwhichforums', 'forum').':'; + echo ''; + choose_from_menu(forum_menu_list($course), 'forumid', '', get_string('allforums', 'forum'), ''); + echo ''; + echo ''; + echo ''; echo ''.get_string('searchsubject', 'forum').':'; echo ''; @@ -327,5 +341,50 @@ function forum_clean_search_terms($words, $prefix='') { return trim(implode(' ', $searchterms)); } +function forum_menu_list($course) { + + $menu = array(); + + $currentgroup = get_current_group($course->id); + $isteacher = isteacher($course->id); + + if ($isteacher) { // Add teacher forum + if ($forum = forum_get_course_forum($course->id, 'teacher')) { + $menu[$forum->id] = $forum->name; + } + } + + if ($forums = get_all_instances_in_course("forum", $course)) { + if ($course->format == 'weeks') { + $strsection = get_string('week'); + } else { + $strsection = get_string('topic'); + } + + foreach ($forums as $forum) { + if (!$isteacher) { // Non-teachers + if ($forum->type == "teacher") { + continue; + } + if (!isset($forum->visible)) { + if (! instance_is_visible("forum", $forum)) { + continue; + } + } + if ($cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { + $groupmode = groupmode($course, $cm); // Groups are being used + if (($groupmode == SEPARATEGROUPS) and ($currentgroup === false)) { + continue; + } + } + } + + $menu[$forum->id] = $forum->name; + } + } + + return $menu; +} + ?>