Commit e86bc492 authored by Neill Magill's avatar Neill Magill
Browse files

MDL-71115 search: Only replace course search when equivalent

Not all global search configurations offer the same results as the
default course search does.

The default course search will return courses that the user can see
the details of even if they can see them via a category, even if
they are not enrolled and cannot enrol themselves on it.

The equivalent global search configuration is:

* Global search is enabled
* The course indexing is enabled
* All the name and summary of all courses are included in the results

This patch will only let global search replace the default course
search when those criteria are met.

It will also display on the global search configuration page if global
search will replace the legacy course search.
parent d135a120
......@@ -1088,6 +1088,7 @@ $string['rememberuserlicensepref'] = 'Remember user licence preference';
$string['rememberuserlicensepref_help'] = 'If enabled, the last licence selected by the user is preselected when uploading a file in the file picker. Otherwise, the default site licence is preselected.';
$string['rememberusername'] = 'Remember username';
$string['rememberusername_desc'] = 'Enable if you want to store permanent cookies with usernames during user login. Permanent cookies may be considered a privacy issue if used without consent.';
$string['replacefrontsearch'] = 'Used for front page course search';
$string['reportsmanage'] = 'Manage reports';
$string['requiredentrieschanged'] = 'Note: After upgrading, the setting \'Required entries before viewing\' is now enforced in the following database activities:<br/>{$a->text}<br/>';
$string['requiremodintro'] = 'Require activity description';
......
......@@ -11316,6 +11316,18 @@ class admin_setting_searchsetupinfo extends admin_setting {
$row[1] = $status;
$table->data[] = $row;
// Replace front page search.
$row = array();
$url = new moodle_url("/admin/search.php?query=searchincludeallcourses");
$row[0] = '6. ' . html_writer::tag('a', get_string('replacefrontsearch', 'admin'),
array('href' => $url));
$status = html_writer::tag('span', get_string('no'), array('class' => 'badge badge-danger'));
if (\core_search\manager::can_replace_course_search()) {
$status = html_writer::tag('span', get_string('yes'), array('class' => 'badge badge-success'));
}
$row[1] = $status;
$table->data[] = $row;
$return .= html_writer::table($table);
return highlight($query, $return);
......
......@@ -263,13 +263,45 @@ class manager {
return !empty($CFG->enableglobalsearch);
}
/**
* Tests if global search is configured to be equivalent to the front page course search.
*
* @return bool
*/
public static function can_replace_course_search(): bool {
global $CFG;
// Assume we can replace front page search.
$canreplace = true;
// Global search must be enabled.
if (!static::is_global_search_enabled()) {
$canreplace = false;
}
// Users must be able to search the details of all courses that they can see,
// even if they do not have access to them.
if (empty($CFG->searchincludeallcourses)) {
$canreplace = false;
}
// Course search must be enabled.
if ($canreplace) {
$areaid = static::generate_areaid('core_course', 'course');
$enabledareas = static::get_search_areas_list(true);
$canreplace = isset($enabledareas[$areaid]);
}
return $canreplace;
}
/**
* Returns the search URL for course search
*
* @return moodle_url
*/
public static function get_course_search_url() {
if (self::is_global_search_enabled()) {
if (self::can_replace_course_search()) {
$searchurl = '/search/index.php';
} else {
$searchurl = '/course/search.php';
......
......@@ -78,18 +78,102 @@ class search_manager_testcase extends advanced_testcase {
$this->assertFalse(\core_search\manager::is_global_search_enabled());
}
public function test_course_search_url() {
/**
* Tests the course search url is correct.
*
* @param bool|null $gsenabled Enable global search (null to leave as the default).
* @param bool|null $allcourses Enable searching all courses (null to leave as the default).
* @param bool|null $enablearea Enable the course search area (null to leave as the default).
* @param string $expected The expected course search url.
* @dataProvider data_course_search_url
*/
public function test_course_search_url(?bool $gsenabled, ?bool $allcourses, ?bool $enablearea, string $expected) {
$this->resetAfterTest();
if (!is_null($gsenabled)) {
set_config('enableglobalsearch', $gsenabled);
}
if (!is_null($allcourses)) {
set_config('searchincludeallcourses', $allcourses);
}
if (!is_null($enablearea)) {
// Setup the course search area.
$areaid = \core_search\manager::generate_areaid('core_course', 'course');
$area = \core_search\manager::get_search_area($areaid);
$area->set_enabled($enablearea);
}
$this->assertEquals(new moodle_url($expected), \core_search\manager::get_course_search_url());
}
/**
* Data for the test_course_search_url test.
*
* @return array[]
*/
public function data_course_search_url(): array {
return [
'defaults' => [null, null, null, '/course/search.php'],
'enabled' => [true, true, true, '/search/index.php'],
'no all courses, no search area' => [true, false, false, '/course/search.php'],
'no search area' => [true, true, false, '/course/search.php'],
'no all courses' => [true, false, true, '/course/search.php'],
'disabled' => [false, false, false, '/course/search.php'],
'no global search' => [false, true, false, '/course/search.php'],
'no global search, no all courses' => [false, false, true, '/course/search.php'],
'no global search, no search area' => [false, true, false, '/course/search.php'],
];
}
/**
* Tests that we detect that global search can replace frontpage course search.
*
* @param bool|null $gsenabled Enable global search (null to leave as the default).
* @param bool|null $allcourses Enable searching all courses (null to leave as the default).
* @param bool|null $enablearea Enable the course search area (null to leave as the default).
* @param bool $expected The expected result.
* @dataProvider data_can_replace_course_search
*/
public function test_can_replace_course_search(?bool $gsenabled, ?bool $allcourses, ?bool $enablearea, bool $expected) {
$this->resetAfterTest();
// URL is course/search.php by default.
$this->assertEquals(new moodle_url("/course/search.php"), \core_search\manager::get_course_search_url());
if (!is_null($gsenabled)) {
set_config('enableglobalsearch', $gsenabled);
}
set_config('enableglobalsearch', true);
$this->assertEquals(new moodle_url("/search/index.php"), \core_search\manager::get_course_search_url());
if (!is_null($allcourses)) {
set_config('searchincludeallcourses', $allcourses);
}
set_config('enableglobalsearch', false);
$this->assertEquals(new moodle_url("/course/search.php"), \core_search\manager::get_course_search_url());
if (!is_null($enablearea)) {
// Setup the course search area.
$areaid = \core_search\manager::generate_areaid('core_course', 'course');
$area = \core_search\manager::get_search_area($areaid);
$area->set_enabled($enablearea);
}
$this->assertEquals($expected, \core_search\manager::can_replace_course_search());
}
/**
* Data for the test_can_replace_course_search test.
*
* @return array[]
*/
public function data_can_replace_course_search(): array {
return [
'defaults' => [null, null, null, false],
'enabled' => [true, true, true, true],
'no all courses, no search area' => [true, false, false, false],
'no search area' => [true, true, false, false],
'no all courses' => [true, false, true, false],
'disabled' => [false, false, false, false],
'no global search' => [false, true, false, false],
'no global search, no all courses' => [false, false, true, false],
'no global search, no search area' => [false, true, false, false],
];
}
public function test_search_areas() {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment