From 2d94d4ea7c465960fbaa9a426fb91cd536c68b6d Mon Sep 17 00:00:00 2001 From: sam marshall Date: Thu, 24 Aug 2017 11:28:18 +0100 Subject: [PATCH] MDL-59913 Global search: Allow search of non-enrolled courses --- admin/settings/plugins.php | 10 ++++- lang/en/admin.php | 4 ++ search/classes/manager.php | 3 +- search/tests/manager_test.php | 83 +++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) diff --git a/admin/settings/plugins.php b/admin/settings/plugins.php index 3bb3c4a3988..4444ff06444 100644 --- a/admin/settings/plugins.php +++ b/admin/settings/plugins.php @@ -557,13 +557,21 @@ if ($hassiteconfig) { $temp->add(new admin_setting_heading('searchengineheading', new lang_string('searchengine', 'admin'), '')); $temp->add(new admin_setting_configselect('searchengine', new lang_string('selectsearchengine', 'admin'), '', 'solr', $engines)); - $temp->add(new admin_setting_heading('searchindexingheading', new lang_string('searchoptions', 'admin'), '')); + $temp->add(new admin_setting_heading('searchoptionsheading', new lang_string('searchoptions', 'admin'), '')); $temp->add(new admin_setting_configcheckbox('searchindexwhendisabled', new lang_string('searchindexwhendisabled', 'admin'), new lang_string('searchindexwhendisabled_desc', 'admin'), 0)); $temp->add(new admin_setting_configduration('searchindextime', new lang_string('searchindextime', 'admin'), new lang_string('searchindextime_desc', 'admin'), 600)); + $options = [ + 0 => new lang_string('searchallavailablecourses_off', 'admin'), + 1 => new lang_string('searchallavailablecourses_on', 'admin') + ]; + $temp->add(new admin_setting_configselect('searchallavailablecourses', + new lang_string('searchallavailablecourses', 'admin'), + new lang_string('searchallavailablecourses_desc', 'admin'), + 0, $options)); $ADMIN->add('searchplugins', $temp); $ADMIN->add('searchplugins', new admin_externalpage('searchareas', new lang_string('searchareas', 'admin'), diff --git a/lang/en/admin.php b/lang/en/admin.php index c342d025fe3..80258ba7179 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -981,6 +981,10 @@ $string['save'] = 'Save'; $string['savechanges'] = 'Save changes'; $string['scssinvalid'] = 'SCSS code is not valid, fails with: {$a}'; $string['search'] = 'Search'; +$string['searchallavailablecourses'] = 'Searchable courses'; +$string['searchallavailablecourses_off'] = 'Search within enrolled courses only'; +$string['searchallavailablecourses_on'] = 'Search within all courses the user can access'; +$string['searchallavailablecourses_desc'] = 'In some situations the search engine may not work when searching across a large number of courses. Set to search only enrolled courses if you need to restrict the number of courses searched.'; $string['searchalldeleted'] = 'All indexed contents have been deleted'; $string['searchareaenabled'] = 'Search area enabled'; $string['searchareadisabled'] = 'Search area disabled'; diff --git a/search/classes/manager.php b/search/classes/manager.php index f89b3a11858..3e8daac9d8e 100644 --- a/search/classes/manager.php +++ b/search/classes/manager.php @@ -379,7 +379,8 @@ class manager { } // Get the courses where the current user has access. - $courses = enrol_get_my_courses(array('id', 'cacherev')); + $courses = enrol_get_my_courses(array('id', 'cacherev'), 'id', 0, [], + (bool)get_config('core', 'searchallavailablecourses')); if (empty($limitcourseids) || in_array(SITEID, $limitcourseids)) { $courses[SITEID] = get_course(SITEID); diff --git a/search/tests/manager_test.php b/search/tests/manager_test.php index a44b25086e0..9ae2a4dab37 100644 --- a/search/tests/manager_test.php +++ b/search/tests/manager_test.php @@ -463,6 +463,89 @@ class search_manager_testcase extends advanced_testcase { $this->assertEquals($contexts['block_html-content'], $limitedcontexts['block_html-content']); } + /** + * Test get_areas_user_accesses with regard to the 'all available courses' config option. + * + * @return void + */ + public function test_search_user_accesses_allavailable() { + global $DB, $CFG; + + $this->resetAfterTest(); + + // Front page, including a forum. + $frontpage = $DB->get_record('course', array('id' => SITEID)); + $forumfront = $this->getDataGenerator()->create_module('forum', array('course' => $frontpage->id)); + $forumfrontctx = context_module::instance($forumfront->cmid); + + // Course 1 does not allow guest access. + $course1 = $this->getDataGenerator()->create_course((object)array( + 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED, + 'enrol_guest_password_0' => '')); + $forum1 = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id)); + $forum1ctx = context_module::instance($forum1->cmid); + + // Course 2 does not allow guest but is accessible by all users. + $course2 = $this->getDataGenerator()->create_course((object)array( + 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED, + 'enrol_guest_password_0' => '')); + $course2ctx = context_course::instance($course2->id); + $forum2 = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id)); + $forum2ctx = context_module::instance($forum2->cmid); + assign_capability('moodle/course:view', CAP_ALLOW, $CFG->defaultuserroleid, $course2ctx->id); + + // Course 3 allows guest access without password. + $course3 = $this->getDataGenerator()->create_course((object)array( + 'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED, + 'enrol_guest_password_0' => '')); + $forum3 = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id)); + $forum3ctx = context_module::instance($forum3->cmid); + + // Student user is enrolled in course 1. + $student = $this->getDataGenerator()->create_user(); + $this->getDataGenerator()->enrol_user($student->id, $course1->id, 'student'); + + // No access user is just a user with no permissions. + $noaccess = $this->getDataGenerator()->create_user(); + + // First test without the all available option. + $search = testable_core_search::instance(); + + // Admin user can access everything. + $this->setAdminUser(); + $this->assertTrue($search->get_areas_user_accesses()); + + // No-access user can access only the front page forum. + $this->setUser($noaccess); + $contexts = $search->get_areas_user_accesses(); + $this->assertEquals([$forumfrontctx->id], array_keys($contexts[$this->forumpostareaid])); + + // Student can access the front page forum plus the enrolled one. + $this->setUser($student); + $contexts = $search->get_areas_user_accesses(); + $this->assertEquals([$forum1ctx->id, $forumfrontctx->id], + array_keys($contexts[$this->forumpostareaid])); + + // Now turn on the all available option. + set_config('searchallavailablecourses', 1); + + // Admin user can access everything. + $this->setAdminUser(); + $this->assertTrue($search->get_areas_user_accesses()); + + // No-access user can access the front page forum and course 2, 3. + $this->setUser($noaccess); + $contexts = $search->get_areas_user_accesses(); + $this->assertEquals([$forum2ctx->id, $forum3ctx->id, $forumfrontctx->id], + array_keys($contexts[$this->forumpostareaid])); + + // Student can access the front page forum plus the enrolled one plus courses 2, 3. + $this->setUser($student); + $contexts = $search->get_areas_user_accesses(); + $this->assertEquals([$forum1ctx->id, $forum2ctx->id, $forum3ctx->id, $forumfrontctx->id], + array_keys($contexts[$this->forumpostareaid])); + } + /** * test_is_search_area * -- 2.43.0