Finishing off search capability with the ability to search by instance,
and in this case, by forum.
This commit is contained in:
@@ -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';
|
||||
|
||||
+28
-1
@@ -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;
|
||||
|
||||
+1
-1
@@ -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,
|
||||
|
||||
+60
-1
@@ -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 '</td>';
|
||||
echo '</tr>';
|
||||
|
||||
echo '<tr>';
|
||||
echo '<td class="c0">'.get_string('searchwhichforums', 'forum').':</td>';
|
||||
echo '<td class="c1">';
|
||||
choose_from_menu(forum_menu_list($course), 'forumid', '', get_string('allforums', 'forum'), '');
|
||||
echo '</td>';
|
||||
echo '</tr>';
|
||||
|
||||
echo '<tr>';
|
||||
echo '<td class="c0">'.get_string('searchsubject', 'forum').':</td>';
|
||||
echo '<td class="c1"><input type="text" size="35" name="subject" value="'.s($subject).'" alt=""></td>';
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user