From 6a750d283d1dcb5d94f08fd73bf6a42bea43511a Mon Sep 17 00:00:00 2001 From: Bas Brands Date: Mon, 31 Aug 2020 13:39:20 +0200 Subject: [PATCH] MDL-69454 core_search: consistent block searches --- blocks/blog_menu/block_blog_menu.php | 19 +++++----- .../behat/block_blog_menu_activity.feature | 2 +- .../behat/block_blog_menu_course.feature | 2 +- blocks/globalsearch/block_globalsearch.php | 29 ++++----------- .../classes/output/search_form.php | 12 +++---- .../templates/search_form.mustache | 26 +++++++------- .../behat/block_search_forums_course.feature | 15 ++++---- .../block_search_forums_frontpage.feature | 10 +++--- blocks/settings/renderer.php | 9 ++--- .../settings/templates/search_form.mustache | 36 ------------------- 10 files changed, 50 insertions(+), 110 deletions(-) delete mode 100644 blocks/settings/templates/search_form.mustache diff --git a/blocks/blog_menu/block_blog_menu.php b/blocks/blog_menu/block_blog_menu.php index 7bc53b130c9..3c4d9c9166c 100644 --- a/blocks/blog_menu/block_blog_menu.php +++ b/blocks/blog_menu/block_blog_menu.php @@ -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 = ''; diff --git a/blocks/blog_menu/tests/behat/block_blog_menu_activity.feature b/blocks/blog_menu/tests/behat/block_blog_menu_activity.feature index 9a342d231c4..586e00c51ec 100644 --- a/blocks/blog_menu/tests/behat/block_blog_menu_activity.feature +++ b/blocks/blog_menu/tests/behat/block_blog_menu_activity.feature @@ -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" diff --git a/blocks/blog_menu/tests/behat/block_blog_menu_course.feature b/blocks/blog_menu/tests/behat/block_blog_menu_course.feature index 39821d29276..11d9625b02e 100644 --- a/blocks/blog_menu/tests/behat/block_blog_menu_course.feature +++ b/blocks/blog_menu/tests/behat/block_blog_menu_course.feature @@ -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" diff --git a/blocks/globalsearch/block_globalsearch.php b/blocks/globalsearch/block_globalsearch.php index d3d96b20662..91892ae26eb 100644 --- a/blocks/globalsearch/block_globalsearch.php +++ b/blocks/globalsearch/block_globalsearch.php @@ -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; } diff --git a/blocks/search_forums/classes/output/search_form.php b/blocks/search_forums/classes/output/search_form.php index 8213abe987f..76b41a025be 100644 --- a/blocks/search_forums/classes/output/search_form.php +++ b/blocks/search_forums/classes/output/search_form.php @@ -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; } diff --git a/blocks/search_forums/templates/search_form.mustache b/blocks/search_forums/templates/search_form.mustache index b5f813325b4..320a5463365 100644 --- a/blocks/search_forums/templates/search_form.mustache +++ b/blocks/search_forums/templates/search_form.mustache @@ -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": "" } }}
-
- -
- - -
- -
-
-
+ {{>core/search_input}}
- {{#str}}advancedsearch, block_search_forums{{/str}} + {{#str}}advancedsearch, block_search_forums{{/str}} {{#helpicon}} {{>core/help_icon}} {{/helpicon}} diff --git a/blocks/search_forums/tests/behat/block_search_forums_course.feature b/blocks/search_forums/tests/behat/block_search_forums_course.feature index dfdeb6f53ed..9444846f6fa 100644 --- a/blocks/search_forums/tests/behat/block_search_forums_course.feature +++ b/blocks/search_forums/tests/behat/block_search_forums_course.feature @@ -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" diff --git a/blocks/search_forums/tests/behat/block_search_forums_frontpage.feature b/blocks/search_forums/tests/behat/block_search_forums_frontpage.feature index 53f3ba95767..31d868376cd 100644 --- a/blocks/search_forums/tests/behat/block_search_forums_frontpage.feature +++ b/blocks/search_forums/tests/behat/block_search_forums_frontpage.feature @@ -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" diff --git a/blocks/settings/renderer.php b/blocks/settings/renderer.php index bceefd8a5d8..3e928483a8d 100644 --- a/blocks/settings/renderer.php +++ b/blocks/settings/renderer.php @@ -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); } } diff --git a/blocks/settings/templates/search_form.mustache b/blocks/settings/templates/search_form.mustache deleted file mode 100644 index 2ae6011112e..00000000000 --- a/blocks/settings/templates/search_form.mustache +++ /dev/null @@ -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 . -}} -{{! - @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" - } -}} -