MDL-69454 core_search: consistent block searches

This commit is contained in:
Bas Brands 2020-08-31 13:39:20 +02:00
parent d5593b18c2
commit 411cf64738
10 changed files with 50 additions and 110 deletions

View File

@ -50,7 +50,7 @@ class block_blog_menu extends block_base {
}
function get_content() {
global $CFG;
global $CFG, $OUTPUT;
// detect if blog enabled
if ($this->content !== NULL) {
@ -98,15 +98,14 @@ class block_blog_menu extends block_base {
// Prepare the footer for this block
if (has_capability('moodle/blog:search', context_system::instance())) {
// Full-text search field
$form = html_writer::tag('label', get_string('search', 'admin'), array('for' => 'blogsearchquery',
'class' => 'accesshide'));
$form .= html_writer::empty_tag('input', array('id' => 'blogsearchquery', 'class' => 'form-control mr-1',
'type' => 'text', 'name' => 'search'));
$form .= html_writer::empty_tag('input', array('type' => 'submit', 'class' => 'btn btn-secondary',
'value' => get_string('search')));
$this->content->footer = html_writer::tag('form', html_writer::tag('div', $form), array(
'class' => 'blogsearchform form-inline', 'method' => 'get', 'action' => new moodle_url('/blog/index.php')));
$data = [
'action' => new moodle_url('/blog/index.php'),
'inputname' => 'search',
'searchstring' => get_string('search', 'admin'),
'extraclasses' => 'mt-3'
];
$this->content->footer = $OUTPUT->render_from_template('core/search_input', $data);
} else {
// No footer to display
$this->content->footer = '';

View File

@ -206,7 +206,7 @@ Feature: Enable Block blog menu in an activity
When I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "Test assignment 1"
And I set the field "blogsearchquery" to "First"
And I set the field "Search" to "First"
And I press "Search"
Then I should see "S1 First Blog"
And I should see "S2 First Blog"

View File

@ -183,7 +183,7 @@ Feature: Students can use block blog menu in a course
And I log out
When I log in as "teacher1"
And I am on "Course 1" course homepage
And I set the field "blogsearchquery" to "First"
And I set the field "Search" to "First"
And I press "Search"
Then I should see "S1 First Blog"
And I should see "S2 First Blog"

View File

@ -64,32 +64,17 @@ class block_globalsearch extends block_base {
return $this->content;
}
$url = new moodle_url('/search/index.php');
$this->content->footer .= html_writer::link($url, get_string('advancedsearch', 'search'));
$data = [
'action' => new moodle_url('/search/index.php'),
'inputname' => 'q',
'searchstring' => get_string('search'),
];
$this->content->text = html_writer::start_tag('div', array('class' => 'searchform'));
$this->content->text .= html_writer::start_tag('form', array('action' => $url->out()));
$this->content->text .= html_writer::start_tag('fieldset', array('action' => 'invisiblefieldset'));
// Input.
$this->content->text .= html_writer::tag('label', get_string('search', 'search'),
array('for' => 'searchform_search', 'class' => 'accesshide'));
$inputoptions = array('id' => 'searchform_search', 'name' => 'q', 'class' => 'form-control',
'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]);
$data['hiddenfields'] = (object) ['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'));
$this->content->text .= html_writer::end_tag('fieldset');
$this->content->text .= html_writer::end_tag('form');
$this->content->text .= html_writer::end_tag('div');
$this->content->text = $OUTPUT->render_from_template('core/search_input', $data);
return $this->content;
}

View File

@ -44,8 +44,6 @@ class search_form implements renderable, templatable {
protected $courseid;
/** @var moodle_url The form action URL. */
protected $actionurl;
/** @var moodle_url The advanced search URL. */
protected $advancedsearchurl;
/** @var help_icon The help icon. */
protected $helpicon;
@ -56,17 +54,17 @@ class search_form implements renderable, templatable {
*/
public function __construct($courseid) {
$this->courseid = $courseid;
$this->actionurl = new moodle_url('/mod/forum/search.php');
$this->advancedsearchurl = new moodle_url('/mod/forum/search.php', ['id' => $this->courseid]);
$this->actionurl = new moodle_url('/mod/forum/search.php', ['id' => $courseid]);
$this->helpicon = new help_icon('search', 'core');
}
public function export_for_template(renderer_base $output) {
$data = [
'actionurl' => $this->actionurl->out(false),
'courseid' => $this->courseid,
'advancedsearchurl' => $this->advancedsearchurl->out(false),
'action' => $this->actionurl,
'helpicon' => $this->helpicon->export_for_template($output),
'hiddenfields' => (object) ['name' => 'id', 'value' => $this->courseid],
'inputname' => 'search',
'searchstring' => get_string('search')
];
return $data;
}

View File

@ -21,25 +21,23 @@
Example context (json):
{
"actionurl": "https://domain.example/mod/forum/search.php",
"courseid": "2",
"advancedsearchurl": "https://domain.example/mod/forum/search.php?id=2",
"action": "https://moodle.local/admin/search.php",
"inputname": "search",
"searchstring": "Search settings",
"value": "policy",
"hiddenfields": [
{
"name": "course",
"value": "11"
}
],
"helpicon": "<a class='btn'><i class='icon fa fa-question-circle'></i></a>"
}
}}
<div class="searchform">
<form action="{{actionurl}}" class="form-inline">
<input type="hidden" name="id" value="{{courseid}}">
<div class="input-group w-100">
<label class="sr-only" for="searchform_search">{{#str}}search{{/str}}</label>
<input id="searchform_search" name="search" type="text" class="form-control" size="10">
<div class="input-group-append">
<button class="btn btn-secondary" id="searchform_button" type="submit">{{#str}}go{{/str}}</button>
</div>
</div>
</form>
{{>core/search_input}}
<div class="mt-3">
<a href="{{advancedsearchurl}}">{{#str}}advancedsearch, block_search_forums{{/str}}</a>
<a href="{{action}}">{{#str}}advancedsearch, block_search_forums{{/str}}</a>
{{#helpicon}}
{{>core/help_icon}}
{{/helpicon}}

View File

@ -29,9 +29,8 @@ Feature: The search forums block allows users to search for forum posts on cours
Scenario: Use the search forum block in a course without any forum posts
Given I log in as "student1"
And I am on "Course 1" course homepage
When I set the following fields to these values:
| searchform_search | Moodle |
And I press "Go"
When I set the field "Search" to "Moodle"
And I press "Search"
Then I should see "No posts"
Scenario: Use the search forum block in a course with a hidden forum and search for posts
@ -50,9 +49,8 @@ Feature: The search forums block allows users to search for forum posts on cours
When I log in as "student1"
And I am on "Course 1" course homepage
And "Search forums" "block" should exist
And I set the following fields to these values:
| searchform_search | message |
And I press "Go"
When I set the field "Search" to "message"
And I press "Search"
Then I should see "No posts"
Scenario: Use the search forum block in a course and search for posts
@ -65,7 +63,6 @@ Feature: The search forums block allows users to search for forum posts on cours
When I log in as "student1"
And I am on "Course 1" course homepage
And "Search forums" "block" should exist
And I set the following fields to these values:
| searchform_search | message |
And I press "Go"
When I set the field "Search" to "message"
And I press "Search"
Then I should see "My subject"

View File

@ -17,15 +17,13 @@ Feature: The search forums block allows users to search for forum posts on front
Scenario: Use the search forum block on the frontpage and search for posts as a user
Given I log in as "student1"
And I am on site homepage
When I set the following fields to these values:
| searchform_search | Moodle |
And I press "Go"
When I set the field "Search" to "Moodle"
And I press "Search"
Then I should see "No posts"
Scenario: Use the search forum block on the frontpage and search for posts as a guest
Given I log in as "guest"
And I am on site homepage
When I set the following fields to these values:
| searchform_search | Moodle |
And I press "Go"
When I set the field "Search" to "Moodle"
And I press "Search"
Then I should see "No posts"

View File

@ -144,11 +144,12 @@ class block_settings_renderer extends plugin_renderer_base {
public function search_form(moodle_url $formtarget, $searchvalue) {
$data = [
'action' => $formtarget->out(false),
'label' => get_string('searchinsettings', 'admin'),
'searchvalue' => $searchvalue
'action' => $formtarget,
'inputname' => 'query',
'searchstring' => get_string('searchinsettings', 'admin'),
'query' => $searchvalue
];
return $this->render_from_template('block_settings/search_form', $data);
return $this->render_from_template('core/search_input', $data);
}
}

View File

@ -1,36 +0,0 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template block_settings/search_form
This template renders the search form.
Example context (json):
{
"action": "https://domain.example/admin/search.php",
"label": "Search in settings",
"searchvalue": "Find this setting",
"quote": "Search"
}
}}
<form method="get" action="{{action}}" class="adminsearchform form-inline" role="search">
<div class="form-group">
<label class="sr-only" for="adminsearchquery">{{label}}</label>
<input class="form-control" type="text" name="query" value="{{searchvalue}}" id="adminsearchquery" size="10">
</div>
<input type="submit" class="btn btn-secondary" value={{#quote}}{{#str}}search{{/str}}{{/quote}}>
</form>