Skip to content

Commit

Permalink
MDL-60880 core_search: Allow search of specific context (front-end)
Browse files Browse the repository at this point in the history
  • Loading branch information
sammarshallou committed Dec 5, 2017
1 parent cfa00fc commit 7f9fb2c
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 5 deletions.
6 changes: 6 additions & 0 deletions blocks/globalsearch/block_globalsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public function get_content() {
'type' => 'text', 'size' => '15');
$this->content->text .= html_writer::empty_tag('input', $inputoptions);

// Context id.
if ($this->page->context && $this->page->context->contextlevel !== CONTEXT_SYSTEM) {
$this->content->text .= html_writer::empty_tag('input', ['type' => 'hidden',
'name' => 'context', 'value' => $this->page->context->id]);
}

// Search button.
$this->content->text .= html_writer::tag('button', get_string('search', 'search'),
array('id' => 'searchform_button', 'type' => 'submit', 'title' => 'globalsearch', 'class' => 'btn btn-secondary'));
Expand Down
2 changes: 2 additions & 0 deletions lang/en/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
$string['enteryoursearchquery'] = 'Enter your search query';
$string['errors'] = 'Errors';
$string['errorareanotavailable'] = '{$a} search area is not available.';
$string['everywhere'] = 'Everywhere you can access';
$string['filesinindexdirectory'] = 'Files in index directory';
$string['filterheader'] = 'Filter';
$string['fromtime'] = 'Modified after';
Expand Down Expand Up @@ -89,6 +90,7 @@
$string['searching'] = 'Searching in ...';
$string['searchnotpermitted'] = 'You are not allowed to do a search';
$string['searchsetupdescription'] = 'The following steps help you to set up Moodle global search.';
$string['searchwithin'] = 'Search within';
$string['seconds'] = 'seconds';
$string['solutions'] = 'Solutions';
$string['statistics'] = 'Statistics';
Expand Down
4 changes: 4 additions & 0 deletions lib/outputrenderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3259,6 +3259,10 @@ public function search_box($id = false) {

$contents = html_writer::tag('label', get_string('enteryoursearchquery', 'search'),
array('for' => 'id_q_' . $id, 'class' => 'accesshide')) . html_writer::tag('input', '', $inputattrs);
if ($this->page->context && $this->page->context->contextlevel !== CONTEXT_SYSTEM) {
$contents .= html_writer::empty_tag('input', ['type' => 'hidden',
'name' => 'context', 'value' => $this->page->context->id]);
}
$searchinput = html_writer::tag('form', $contents, $formattrs);

return html_writer::tag('div', $searchicon . $searchinput, array('class' => 'search-input-wrapper nav-link', 'id' => $id));
Expand Down
16 changes: 16 additions & 0 deletions search/classes/output/form/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ function definition() {
$mform->setType('q', PARAM_TEXT);
$mform->addRule('q', get_string('required'), 'required', null, 'client');

// Show the 'search within' option if the user came from a particular context.
if (!empty($this->_customdata['searchwithin'])) {
$mform->addElement('select', 'searchwithin', get_string('searchwithin', 'search'),
$this->_customdata['searchwithin']);
$mform->setDefault('searchwithin', '');
}

$mform->addElement('header', 'filtersection', get_string('filterheader', 'search'));
$mform->setExpanded('filtersection', false);

Expand Down Expand Up @@ -79,12 +86,21 @@ function definition() {
$mform->addElement('course', 'courseids', get_string('courses', 'core'), $options);
$mform->setType('courseids', PARAM_INT);

// Course options should be hidden if we choose to search within a specific location.
if (!empty($this->_customdata['searchwithin'])) {
$mform->hideIf('courseids', 'searchwithin', 'ne', '');
}

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

$mform->addElement('date_time_selector', 'timeend', get_string('totime', 'search'), array('optional' => true));
$mform->setDefault('timeend', 0);

// Source context i.e. the page they came from when they clicked search.
$mform->addElement('hidden', 'context');
$mform->setType('context', PARAM_INT);

$this->add_action_buttons(false, get_string('search', 'search'));
}
}
36 changes: 34 additions & 2 deletions search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
$page = optional_param('page', 0, PARAM_INT);
$q = optional_param('q', '', PARAM_NOTAGS);
$title = optional_param('title', '', PARAM_NOTAGS);
$contextid = optional_param('context', 0, PARAM_INT);
// Moving areaids, courseids, timestart, and timeend further down as they might come as an array if they come from the form.

$context = context_system::instance();
Expand Down Expand Up @@ -55,8 +56,23 @@

$search = \core_search\manager::instance();

// We first get the submitted data as we want to set it all in the page URL.
$mform = new \core_search\output\form\search(null, array('searchengine' => $search->get_engine()->get_plugin_name()));
// Set up custom data for form.
$customdata = ['searchengine' => $search->get_engine()->get_plugin_name()];
if ($contextid) {
// When a context is supplied, check if it's within course level. If so, show dropdown.
$context = context::instance_by_id($contextid);
$coursecontext = $context->get_course_context(false);
if ($coursecontext) {
$searchwithin = [];
$searchwithin[''] = get_string('everywhere', 'search');
$searchwithin['course'] = $coursecontext->get_context_name();
if ($context->contextlevel !== CONTEXT_COURSE) {
$searchwithin['context'] = $context->get_context_name();
}
$customdata['searchwithin'] = $searchwithin;
}
}
$mform = new \core_search\output\form\search(null, $customdata);

$data = $mform->get_data();
if (!$data && $q) {
Expand All @@ -77,9 +93,25 @@
}
$data->timestart = optional_param('timestart', 0, PARAM_INT);
$data->timeend = optional_param('timeend', 0, PARAM_INT);

$data->context = $contextid;

$mform->set_data($data);
}

// Convert the 'search within' option, if used, to course or context restrictions.
if ($data && !empty($data->searchwithin)) {
switch ($data->searchwithin) {
case 'course':
$data->courseids = [$coursecontext->instanceid];
break;
case 'context':
$data->courseids = [$coursecontext->instanceid];
$data->contextids = [$context->id];
break;
}
}

// Set the page URL.
$urlparams = array('page' => $page);
if ($data) {
Expand Down
50 changes: 47 additions & 3 deletions search/tests/behat/search_query.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ Feature: Use global search interface
Background:
Given the following config values are set as admin:
| enableglobalsearch | 1 |
And the following "courses" exist:
| shortname | fullname |
| F1 | Amphibians |
And the following "activities" exist:
| activity | name | intro | course | idnumber |
| page | PageName1 | PageDesc1 | Acceptance test site | PAGE1 |
| forum | ForumName1 | ForumDesc1 | Acceptance test site | FORUM1 |
| activity | name | intro | course | idnumber |
| page | PageName1 | PageDesc1 | F1 | PAGE1 |
| forum | ForumName1 | ForumDesc1 | F1 | FORUM1 |
And I log in as "admin"

@javascript
Expand Down Expand Up @@ -47,3 +50,44 @@ Feature: Use global search interface
# Check the link works.
And I follow "ForumName1"
And I should see "ForumName1" in the ".breadcrumb" "css_element"

@javascript
Scenario: Search starting from site context (no within option)
Given global search expects the query "frogs" and will return:
| type | idnumber |
| activity | PAGE1 |
When I search for "frogs" using the header global search box
And I expand all fieldsets
Then I should not see "Search within"
And I should see "Courses"

@javascript
Scenario: Search starting from course context (within option lists course)
Given global search expects the query "frogs" and will return:
| type | idnumber |
| activity | PAGE1 |
When I am on "Amphibians" course homepage
And I search for "frogs" using the header global search box
And I expand all fieldsets
Then I should see "Search within"
And I select "Everywhere you can access" from the "Search within" singleselect
And I should see "Courses"
And I select "Course: Amphibians" from the "Search within" singleselect
And I should not see "Courses"

@javascript
Scenario: Search starting from forum context (within option lists course and forum)
Given global search expects the query "frogs" and will return:
| type | idnumber |
| activity | PAGE1 |
When I am on "Amphibians" course homepage
And I follow "ForumName1"
And I search for "frogs" using the header global search box
And I expand all fieldsets
And I should see "Search within"
And I select "Everywhere you can access" from the "Search within" singleselect
And I should see "Courses"
And I select "Course: Amphibians" from the "Search within" singleselect
And I should not see "Courses"
And I select "Forum: ForumName1" from the "Search within" singleselect
And I should not see "Courses"

0 comments on commit 7f9fb2c

Please sign in to comment.