]> git.mjollnir.org Git - moodle.git/commitdiff
Searching forums now has proper paging.
authormoodler <moodler>
Tue, 19 Aug 2003 03:35:53 +0000 (03:35 +0000)
committermoodler <moodler>
Tue, 19 Aug 2003 03:35:53 +0000 (03:35 +0000)
lang/en/moodle.php
lib/weblib.php
mod/forum/lib.php
mod/forum/search.php

index 9411e291b029aa6a5151ab1469ab31c0488afb0b..11e4c2eed45a0ce7f69d37bf981f7c5806fd2c25 100644 (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";
index 6e4ef37ddea867a52b04fc25867edc0d2b902401..e81979e565e84a3d20c33b964309da8ddea92476 100644 (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:
 ?>
index 3049a7e447e51c77a0376b9ddce4609bcef9a448..a6047abfb6cfffeb8ef0a1551a09d931fb33682f 100644 (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) {
index 1df21b32a21d37295fcae899ea98b36279f250ad..826a7466245e3b3ef339c5f5d2d902dc1b8d3f7a 100644 (file)
 
     $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");
 
         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));
             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)) {
             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);