Skip to content

Commit 9dbb24f

Browse files
author
David Monllao
committed
Merge branch 'MDL-60953-master' of https://github.com/timhunt/moodle
2 parents d2c8008 + 3c49b0e commit 9dbb24f

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

search/classes/output/form/search.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class search extends \moodleform {
3636
* @return void
3737
*/
3838
function definition() {
39-
global $CFG;
39+
global $USER;
4040

4141
$mform =& $this->_form;
4242
$mform->disable_form_change_checker();
@@ -95,9 +95,42 @@ function definition() {
9595
$mform->addElement('course', 'courseids', get_string('courses', 'core'), $options);
9696
$mform->setType('courseids', PARAM_INT);
9797

98-
// Course options should be hidden if we choose to search within a specific location.
9998
if (!empty($this->_customdata['searchwithin'])) {
99+
// Course options should be hidden if we choose to search within a specific location.
100100
$mform->hideIf('courseids', 'searchwithin', 'ne', '');
101+
102+
// Get groups on course (we don't show group selector if there aren't any).
103+
$courseid = $this->_customdata['withincourseid'];
104+
$allgroups = groups_get_all_groups($courseid);
105+
if ($allgroups && $search->get_engine()->supports_groups()) {
106+
$groupnames = [];
107+
foreach ($allgroups as $group) {
108+
$groupnames[$group->id] = $group->name;
109+
}
110+
111+
// Create group autocomplete option.
112+
$options = array(
113+
'multiple' => true,
114+
'noselectionstring' => get_string('allgroups'),
115+
);
116+
$mform->addElement('autocomplete', 'groupids', get_string('groups'), $groupnames, $options);
117+
118+
// Is the second 'search within' option a cm?
119+
if (!empty($this->_customdata['withincmid'])) {
120+
// Find out if the cm supports groups.
121+
$modinfo = get_fast_modinfo($courseid);
122+
$cm = $modinfo->get_cm($this->_customdata['withincmid']);
123+
if ($cm->effectivegroupmode != NOGROUPS) {
124+
// If it does, group ids are available when you have course or module selected.
125+
$mform->hideIf('groupids', 'searchwithin', 'eq', '');
126+
} else {
127+
// Group ids are only available if you have course selected.
128+
$mform->hideIf('groupids', 'searchwithin', 'ne', 'course');
129+
}
130+
} else {
131+
$mform->hideIf('groupids', 'searchwithin', 'eq', '');
132+
}
133+
}
101134
}
102135

103136
$mform->addElement('date_time_selector', 'timestart', get_string('fromtime', 'search'), array('optional' => true));

search/index.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@
6666
$searchwithin = [];
6767
$searchwithin[''] = get_string('everywhere', 'search');
6868
$searchwithin['course'] = $coursecontext->get_context_name();
69-
if ($context->contextlevel !== CONTEXT_COURSE) {
69+
if ($context->contextlevel != CONTEXT_COURSE) {
7070
$searchwithin['context'] = $context->get_context_name();
71+
if ($context->contextlevel == CONTEXT_MODULE) {
72+
$customdata['withincmid'] = $context->instanceid;
73+
}
7174
}
7275
$customdata['searchwithin'] = $searchwithin;
76+
$customdata['withincourseid'] = $coursecontext->instanceid;
7377
}
7478

7579
// Get available ordering options from search engine.

search/tests/behat/search_query.feature

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,38 @@ Feature: Use global search interface
9191
And I should not see "Courses" in the "region-main" "region"
9292
And I select "Forum: ForumName1" from the "Search within" singleselect
9393
And I should not see "Courses" in the "region-main" "region"
94+
95+
@javascript
96+
Scenario: Check that groups option in search form appears when intended
97+
Given the following "groups" exist:
98+
| name | course | idnumber |
99+
| A Group | F1 | G1 |
100+
| B Group | F1 | G2 |
101+
And the following "activities" exist:
102+
| activity | name | intro | course | idnumber | groupmode |
103+
| forum | ForumSG | ForumDesc1 | F1 | FORUM2 | 1 |
104+
When I am on "Amphibians" course homepage
105+
And I follow "ForumSG"
106+
And global search expects the query "frogs" and will return:
107+
| type | idnumber |
108+
| activity | PAGE1 |
109+
And I search for "frogs" using the header global search box
110+
And I expand all fieldsets
111+
Then I should not see "Groups" in the "region-main" "region"
112+
And I select "Course: Amphibians" from the "Search within" singleselect
113+
And I should see "Groups" in the "region-main" "region"
114+
And I set the field "Groups" to "A Group"
115+
And I select "Forum: ForumSG" from the "Search within" singleselect
116+
And I should see "Groups" in the "region-main" "region"
117+
And I am on "Amphibians" course homepage
118+
And I follow "ForumName1"
119+
And global search expects the query "frogs" and will return:
120+
| type | idnumber |
121+
| activity | PAGE1 |
122+
And I search for "frogs" using the header global search box
123+
And I expand all fieldsets
124+
Then I should not see "Groups" in the "region-main" "region"
125+
And I select "Course: Amphibians" from the "Search within" singleselect
126+
And I should see "Groups" in the "region-main" "region"
127+
And I select "Forum: ForumName1" from the "Search within" singleselect
128+
And I should not see "Groups" in the "region-main" "region"

0 commit comments

Comments
 (0)