Skip to content

Commit

Permalink
MDL-59913 Global search: Allow search of non-enrolled courses
Browse files Browse the repository at this point in the history
  • Loading branch information
sammarshallou committed Sep 29, 2017
1 parent 23ab0d7 commit 2d94d4e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
10 changes: 9 additions & 1 deletion admin/settings/plugins.php
Expand Up @@ -557,13 +557,21 @@
$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'),
Expand Down
4 changes: 4 additions & 0 deletions lang/en/admin.php
Expand Up @@ -981,6 +981,10 @@
$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';
Expand Down
3 changes: 2 additions & 1 deletion search/classes/manager.php
Expand Up @@ -379,7 +379,8 @@ protected function get_areas_user_accesses($limitcourseids = false) {
}

// 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);
Expand Down
83 changes: 83 additions & 0 deletions search/tests/manager_test.php
Expand Up @@ -463,6 +463,89 @@ public function test_search_user_accesses_blocks() {
$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
*
Expand Down

0 comments on commit 2d94d4e

Please sign in to comment.