Searching forums now has proper paging.

This commit is contained in:
moodler
2003-08-19 03:35:53 +00:00
parent 7f67a6ed40
commit 8b9c7aa069
4 changed files with 97 additions and 49 deletions
+3
View File
@@ -533,6 +533,7 @@ $string['newsitems'] = "news items";
$string['newsitemsnumber'] = "News items to show";
$string['never'] = "Never";
$string['neverdeletelogs'] = "Never delete logs";
$string['next'] = "Next";
$string['no'] = "No";
$string['nocoursesyet'] = "No courses in this category";
$string['noexistingadmins'] = "No existing admins, this is a serious error and you should never have seen this message.";
@@ -576,6 +577,7 @@ $string['optional'] = "optional";
$string['order'] = "Order";
$string['other'] = "Other";
$string['outline'] = "Outline";
$string['page'] = "Page";
$string['parentlanguage'] = "";
$string['participants'] = "Participants";
$string['password'] = "Password";
@@ -649,6 +651,7 @@ $string['scalesstandard'] = "Standard scales";
$string['search'] = "Search";
$string['searchagain'] = "Search again";
$string['searchcourses'] = "Search courses";
$string['searchhelp'] = "You can search for multiple words at once.<p>word : find any match of this word within the text.<br>+word : only exact matching words will be found.<br>-word : don't include results containing this word.";
$string['searchresults'] = "Search results";
$string['sec'] = "sec";
$string['secs'] = "secs";
+23
View File
@@ -1615,6 +1615,29 @@ function obfuscate_mailto($email, $label="") {
obfuscate_text($label));
}
function print_paging_bar($totalcount, $page, $perpage, $baseurl) {
/// Prints a single paging bar to provide access to other pages (usually in a search)
if ($totalcount > $perpage) {
echo "<p>".get_string("page").":";
$count = 0;
while ($totalcount > 0) {
$displaypage = $count+1;
if ($page == $count) {
echo "&nbsp;&nbsp;$displaypage";
} else {
echo "&nbsp;&nbsp;<a href=\"{$baseurl}page=$count\">$displaypage</a>";
}
$count++;
$totalcount -= $perpage;
}
$pagenum = $page + 1;
if ($pagenum != $count) {
echo "&nbsp;&nbsp;(<a href=\"{$baseurl}page=$pagenum\">".get_string("next")."</a>)";
}
echo "</p>";
}
}
// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
?>
+41 -35
View File
@@ -490,8 +490,11 @@ function forum_get_child_posts($parent) {
}
function forum_search_posts($search, $courseid, $page=0, $recordsperpage=50) {
/// Returns a list of posts that were found
function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50, &$totalcount) {
/// Returns a list of posts found using an array of search terms
/// eg word +word -word
///
global $CFG;
if (!isteacher($courseid)) {
@@ -527,9 +530,11 @@ function forum_search_posts($search, $courseid, $page=0, $recordsperpage=50) {
$messagesearch = "";
$subjectsearch = "";
$searchterms = explode(" ", $search); // Search for words independently
foreach ($searchterms as $searchterm) {
if (strlen($searchterm) < 2) {
continue;
}
if ($messagesearch) {
$messagesearch .= " AND ";
}
@@ -551,19 +556,20 @@ function forum_search_posts($search, $courseid, $page=0, $recordsperpage=50) {
}
}
$selectsql = "{$CFG->prefix}forum_posts p,
{$CFG->prefix}forum_discussions d,
{$CFG->prefix}user u,
{$CFG->prefix}forum f
WHERE ($messagesearch OR $subjectsearch)
AND p.userid = u.id
AND p.discussion = d.id
AND d.course = '$courseid'
AND d.forum = f.id $notteacherforum";
$totalcount = count_records_sql("SELECT COUNT(*) FROM $selectsql");
return get_records_sql("SELECT p.*,u.firstname,u.lastname,u.email,u.picture
FROM {$CFG->prefix}forum_posts p,
{$CFG->prefix}forum_discussions d,
{$CFG->prefix}user u,
{$CFG->prefix}forum f
WHERE ($messagesearch OR $subjectsearch)
AND p.userid = u.id
AND p.discussion = d.id
AND d.course = '$courseid'
AND d.forum = f.id
$notteacherforum
ORDER BY p.modified DESC $limit");
return get_records_sql("SELECT p.*,u.firstname,u.lastname,u.email,u.picture FROM
$selectsql ORDER BY p.modified DESC $limit");
}
@@ -1266,34 +1272,34 @@ function forum_print_rating_menu($postid, $userid, $scale) {
function forum_print_mode_form($discussion, $mode) {
GLOBAL $FORUM_LAYOUT_MODES;
echo "<CENTER><P>";
echo "<center><p>";
popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
echo "</P></CENTER>\n";
echo "</p></center>\n";
}
function forum_print_search_form($course, $search="", $return=false, $type="") {
global $CFG;
if ($type == "plain") {
$output = "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0><TR><TD NOWRAP>";
$output .= "<FORM NAME=search ACTION=\"$CFG->wwwroot/mod/forum/search.php\">";
$output .= "<FONT SIZE=\"-1\">";
$output .= "<INPUT NAME=search TYPE=text SIZE=15 VALUE=\"$search\">";
$output .= "<INPUT VALUE=\"".get_string("searchforums", "forum")."\" TYPE=submit>";
$output .= "</FONT>";
$output .= "<INPUT NAME=id TYPE=hidden VALUE=\"$course->id\">";
$output .= "</FORM>";
$output .= "</TD></TR></TABLE>";
$output = "<table border=0 cellpadding=0 cellspacing=0><tr><td nowrap>";
$output .= "<form name=search action=\"$CFG->wwwroot/mod/forum/search.php\">";
$output .= "<font size=\"-1\">";
$output .= "<input name=search type=text size=20 value=\"$search\">";
$output .= "<input value=\"".get_string("searchforums", "forum")."\" type=submit>";
$output .= "</font>";
$output .= "<input name=id type=hidden value=\"$course->id\">";
$output .= "</form>";
$output .= "</td></tr></table>";
} else {
$output = "<TABLE BORDER=0 CELLPADDING=10 CELLSPACING=0><TR><TD ALIGN=CENTER>";
$output .= "<FORM NAME=search ACTION=\"$CFG->wwwroot/mod/forum/search.php\">";
$output .= "<FONT SIZE=\"-1\">";
$output .= "<INPUT NAME=search TYPE=text SIZE=15 VALUE=\"$search\"><BR>";
$output .= "<INPUT VALUE=\"".get_string("searchforums", "forum")."\" TYPE=submit>";
$output .= "</FONT>";
$output .= "<INPUT NAME=id TYPE=hidden VALUE=\"$course->id\">";
$output .= "</FORM>";
$output .= "</TD></TR></TABLE>";
$output = "<table border=0 cellpadding=10 cellspacing=0><tr><td align=center>";
$output .= "<form name=search action=\"$CFG->wwwroot/mod/forum/search.php\">";
$output .= "<font size=\"-1\">";
$output .= "<input name=search type=text size=20 value=\"$search\"><br>";
$output .= "<input value=\"".get_string("searchforums", "forum")."\" type=submit>";
$output .= "</font>";
$output .= "<input name=id type=hidden value=\"$course->id\">";
$output .= "</form>";
$output .= "</td></tr></table>";
}
if ($return) {
+30 -14
View File
@@ -10,6 +10,16 @@
$search = trim(strip_tags($search));
if ($search) {
$searchterms = explode(" ", $search); // Search for words independently
foreach ($searchterms as $key => $searchterm) {
if (strlen($searchterm) < 2) {
unset($searchterms[$key]);
}
}
$search = trim(implode(" ", $searchterms));
}
if (! $course = get_record("course", "id", $id)) {
error("Course id is incorrect.");
}
@@ -23,6 +33,7 @@
$strforums = get_string("modulenameplural", "forum");
$strsearch = get_string("search", "forum");
$strsearchresults = get_string("searchresults", "forum");
$strpage = get_string("page");
$searchform = forum_print_search_form($course, $search, true, "plain");
@@ -36,12 +47,20 @@
print_header("$course->shortname: $strsearch", "$course->fullname",
"<A HREF=\"../../course/view.php?id=$course->id\">$course->shortname</A> ->
<A HREF=\"index.php?id=$course->id\">$strforums</A> -> $strsearch", "search.search",
"", "", $searchform);
"", "");
print_simple_box_start("center");
echo "<center>";
echo $searchform;
echo "</center><br />";
print_string("searchhelp");
print_simple_box_end();
}
if ($search) {
if (!$posts = forum_search_posts($search, $course->id, $page*$perpage, $perpage)) {
if (!$posts = forum_search_posts($searchterms, $course->id, $page*$perpage, $perpage, $totalcount)) {
if ($page) {
print_heading(get_string("nomorepostscontaining", "forum", $search));
print_continue("search.php?id=$course->id&search=".urlencode($search));
@@ -52,6 +71,12 @@
exit;
}
print_heading("$strsearchresults: $totalcount");
echo "<center>";
print_paging_bar($totalcount, $page, $perpage, "search.php?search=$search&id=$course->id&perpage=$perpage&");
echo "</center>";
foreach ($posts as $post) {
if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
@@ -80,18 +105,9 @@
echo "<br />";
}
if (count($posts) == $perpage) {
$options = array();
$options["id"] = $course->id;
$options["search"] = $search;
$options["page"] = $page+1;
$options["perpage"] = $perpage;
echo "<center>";
print_single_button("search.php", $options, get_string("searcholderposts", "forum"));
echo "</center>";
} else {
print_heading(get_string("nomorepostscontaining", "forum", $search));
}
echo "<center>";
print_paging_bar($totalcount, $page, $perpage, "search.php?search=$search&id=$course->id&perpage=$perpage&");
echo "</center>";
}
print_footer($course);