From 8b9c7aa069d2d199ef8c1eb893eb4bacea3dbb0c Mon Sep 17 00:00:00 2001
From: moodler
Date: Tue, 19 Aug 2003 03:35:53 +0000
Subject: [PATCH] Searching forums now has proper paging.
---
lang/en/moodle.php | 3 ++
lib/weblib.php | 23 ++++++++++++++
mod/forum/lib.php | 76 ++++++++++++++++++++++++--------------------
mod/forum/search.php | 44 +++++++++++++++++--------
4 files changed, 97 insertions(+), 49 deletions(-)
diff --git a/lang/en/moodle.php b/lang/en/moodle.php
index 9411e291b0..11e4c2eed4 100644
--- a/lang/en/moodle.php
+++ b/lang/en/moodle.php
@@ -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.word : find any match of this word within the text.
+word : only exact matching words will be found.
-word : don't include results containing this word.";
$string['searchresults'] = "Search results";
$string['sec'] = "sec";
$string['secs'] = "secs";
diff --git a/lib/weblib.php b/lib/weblib.php
index 6e4ef37dde..e81979e565 100644
--- a/lib/weblib.php
+++ b/lib/weblib.php
@@ -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 "
".get_string("page").":";
+ $count = 0;
+ while ($totalcount > 0) {
+ $displaypage = $count+1;
+ if ($page == $count) {
+ echo " $displaypage";
+ } else {
+ echo " $displaypage";
+ }
+ $count++;
+ $totalcount -= $perpage;
+ }
+ $pagenum = $page + 1;
+ if ($pagenum != $count) {
+ echo " (".get_string("next").")";
+ }
+ echo "
";
+ }
+}
// vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140:
?>
diff --git a/mod/forum/lib.php b/mod/forum/lib.php
index 3049a7e447..a6047abfb6 100644
--- a/mod/forum/lib.php
+++ b/mod/forum/lib.php
@@ -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 "";
+ echo "
";
popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
- echo "
\n";
+ echo "
\n";
}
function forum_print_search_form($course, $search="", $return=false, $type="") {
global $CFG;
if ($type == "plain") {
- $output = "";
- $output .= "";
- $output .= " |
";
+ $output = "";
+ $output .= "";
+ $output .= " |
";
} else {
- $output = "";
- $output .= "";
- $output .= " |
";
+ $output = "";
+ $output .= "";
+ $output .= " |
";
}
if ($return) {
diff --git a/mod/forum/search.php b/mod/forum/search.php
index 1df21b32a2..826a746624 100644
--- a/mod/forum/search.php
+++ b/mod/forum/search.php
@@ -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",
"id\">$course->shortname ->
id\">$strforums -> $strsearch", "search.search",
- "", "", $searchform);
+ "", "");
+
+
+ print_simple_box_start("center");
+ echo "";
+ echo $searchform;
+ echo "
";
+ 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 "";
+ print_paging_bar($totalcount, $page, $perpage, "search.php?search=$search&id=$course->id&perpage=$perpage&");
+ echo "";
+
foreach ($posts as $post) {
if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
@@ -80,18 +105,9 @@
echo "
";
}
- if (count($posts) == $perpage) {
- $options = array();
- $options["id"] = $course->id;
- $options["search"] = $search;
- $options["page"] = $page+1;
- $options["perpage"] = $perpage;
- echo "";
- print_single_button("search.php", $options, get_string("searcholderposts", "forum"));
- echo "";
- } else {
- print_heading(get_string("nomorepostscontaining", "forum", $search));
- }
+ echo "";
+ print_paging_bar($totalcount, $page, $perpage, "search.php?search=$search&id=$course->id&perpage=$perpage&");
+ echo "";
}
print_footer($course);
--
2.39.5