From a8b56716973f370cccb1d63e8491b3e08e43e373 Mon Sep 17 00:00:00 2001 From: moodler Date: Tue, 19 Aug 2003 05:32:20 +0000 Subject: [PATCH] Improvements to course searching - can search for full words or non words with +word -word - no longer groups by category, sorts by full name - prints paging as well as total number - search help --- course/lib.php | 27 ++++++++++++++++----- course/search.php | 62 +++++++++++++++++++++++++++++------------------ lib/datalib.php | 40 +++++++++++++++++++++--------- 3 files changed, 88 insertions(+), 41 deletions(-) diff --git a/course/lib.php b/course/lib.php index bd5fdb947b..d20343e07d 100644 --- a/course/lib.php +++ b/course/lib.php @@ -1122,17 +1122,32 @@ function print_my_moodle() { } -function print_course_search($value="") { +function print_course_search($value="", $return=false, $format="plain") { global $CFG; $strsearchcourses= get_string("searchcourses"); - echo "

"; - echo "

wwwroot/course/search.php\" method=\"get\">"; - echo ""; - echo ""; - echo "

"; + if ($format == "plain") { + $output = "

"; + $output .= "

wwwroot/course/search.php\" method=\"get\">"; + $output .= ""; + $output .= ""; + $output .= "

"; + } else if ($format == "navbar") { + $output = "
"; + $output .= "
wwwroot/course/search.php\" method=\"get\">"; + $output .= ""; + $output .= ""; + $output .= "id\">"; + $output .= "
"; + $output .= "
"; + } + + if ($return) { + return $output; + } + echo $output; } /// MODULE FUNCTIONS ///////////////////////////////////////////////////////////////// diff --git a/course/search.php b/course/search.php index 2ceba51239..b60cd5f6ac 100644 --- a/course/search.php +++ b/course/search.php @@ -11,6 +11,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)); + } + $site = get_site(); if (empty($THEME->custompix)) { @@ -31,44 +41,50 @@ if (!$search) { print_header("$site->fullname : $strsearch", $site->fullname, "$strcourses -> $strsearch", "", ""); - print_course_search(); + print_simple_box_start("center"); + echo "
"; + echo "
"; + print_course_search("", false, "plain"); + echo "

"; + print_string("searchhelp"); + echo "

"; + echo "
"; + print_simple_box_end(); print_footer(); exit; } + $searchform = print_course_search($search, true, "navbar"); + print_header("$site->fullname : $strsearchresults", $site->fullname, - "$strcourses -> $strsearchresults -> '$search'", "", ""); + "$strcourses -> $strsearch -> '$search'", "", "", "", $searchform); - print_heading("$strsearchresults"); $lastcategory = -1; - if ($courses = get_courses_search($search, "category ASC, sortorder DESC", $page*$perpage, $perpage)) { + if ($courses = get_courses_search($searchterms, "fullname ASC", + $page*$perpage, $perpage, $totalcount)) { + + print_heading("$strsearchresults: $totalcount"); + + echo "
"; + print_paging_bar($totalcount, $page, $perpage, "search.php?search=$search&perpage=$perpage&"); + echo "
"; + foreach ($courses as $course) { - if ($course->category != $lastcategory) { - $lastcategory = $course->category; - echo "

"; - echo "category\">"; - echo $displaylist[$course->category]; - echo "

"; - } $course->fullname = highlight("$search", $course->fullname); $course->summary = highlight("$search", $course->summary); + $course->summary .= "

"; + $course->summary .= "$strcategory: category\">"; + $course->summary .= $displaylist[$course->category]; + $course->summary .= "

"; print_course($course); print_spacer(5,5); } - if (count($courses) == $perpage) { - $options = array(); - $options["search"] = $search; - $options["page"] = $page+1; - $options["perpage"] = $perpage; - echo "
"; - echo "
"; - print_single_button("search.php", $options, get_string("findmorecourses")); - echo "
"; - } else { - print_heading(get_string("nomorecourses", "", $search)); - } + echo "
"; + print_paging_bar($totalcount, $page, $perpage, "search.php?search=$search&perpage=$perpage&"); + echo "
"; + } else { print_heading(get_string("nocoursesfound", "", $search)); } diff --git a/lib/datalib.php b/lib/datalib.php index 324e775066..d0fb17d173 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -1144,7 +1144,7 @@ function get_my_courses($userid, $sort="c.fullname ASC") { } -function get_courses_search($search, $sort="fullname ASC", $page=0, $recordsperpage=50) { +function get_courses_search($searchterms, $sort="fullname ASC", $page=0, $recordsperpage=50, &$totalcount) { /// Returns a list of courses that match a search global $CFG; @@ -1162,39 +1162,55 @@ function get_courses_search($search, $sort="fullname ASC", $page=0, $recordsperp //to allow caseinsensitive search for postgesql if ($CFG->dbtype == "postgres7") { - $LIKE = "ILIKE"; + $LIKE = "ILIKE"; + $NOTLIKE = "NOT ILIKE"; // case-insensitive + $REGEXP = "~*"; + $NOTREGEXP = "!~*"; } else { - $LIKE = "LIKE"; + $LIKE = "LIKE"; + $NOTLIKE = "NOT LIKE"; + $REGEXP = "REGEXP"; + $NOTREGEXP = "NOT REGEXP"; } $fullnamesearch = ""; $summarysearch = ""; - $searchterms = explode(" ", $search); // Search for words independently - foreach ($searchterms as $searchterm) { if ($fullnamesearch) { $fullnamesearch .= " AND "; } - $fullnamesearch .= " fullname $LIKE '%$searchterm%' "; - if ($summarysearch) { $summarysearch .= " AND "; } - $summarysearch .= " summary $LIKE '%$searchterm%' "; + + if (substr($searchterm,0,1) == "+") { + $searchterm = substr($searchterm,1); + $summarysearch .= " summary $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; + $fullnamesearch .= " fullname $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; + } else if (substr($searchterm,0,1) == "-") { + $searchterm = substr($searchterm,1); + $summarysearch .= " summary $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; + $fullnamesearch .= " fullname $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; + } else { + $summarysearch .= " summary $LIKE '%$searchterm%' "; + $fullnamesearch .= " fullname $LIKE '%$searchterm%' "; + } + } + $selectsql = "{$CFG->prefix}course WHERE ($fullnamesearch OR $summarysearch)"; + + $totalcount = count_records_sql("SELECT COUNT(*) FROM $selectsql"); - $courses = get_records_sql("SELECT * - FROM {$CFG->prefix}course - WHERE ($fullnamesearch OR $summarysearch) - ORDER BY $sort $limit"); + $courses = get_records_sql("SELECT * FROM $selectsql ORDER BY $sort $limit"); if ($courses) { /// Remove unavailable courses from the list foreach ($courses as $key => $course) { if (!$course->visible) { if (!isteacher($course->id)) { unset($courses[$key]); + $totalcount--; } } } -- 2.39.5