MDL-69454 core_search: consistent frontpage course search

This commit is contained in:
Bas Brands 2020-08-31 12:00:21 +02:00
parent ebfee99d90
commit 968cd48336
2 changed files with 20 additions and 51 deletions

View File

@ -399,48 +399,22 @@ class core_course_renderer extends plugin_renderer_base {
} }
/** /**
* Renders html to display a course search form. * Renders html to display a course search form
* *
* @param string $value default value to populate the search field * @param string $value default value to populate the search field
* @param string $format display format - 'plain' (default), 'short' or 'navbar'
* @return string * @return string
*/ */
public function course_search_form($value = '', $format = 'plain') { public function course_search_form($value = '') {
static $count = 0;
$formid = 'coursesearch';
if ((++$count) > 1) {
$formid .= $count;
}
switch ($format) { $data = [
case 'navbar' : 'action' => \core_search\manager::get_course_search_url(),
$formid = 'coursesearchnavbar'; 'btnclass' => 'btn-primary',
$inputid = 'navsearchbox'; 'inputname' => 'q',
$inputsize = 20; 'searchstring' => get_string('searchcourses'),
break; 'hiddenfields' => (object) ['name' => 'areaids', 'value' => 'core_course-course'],
case 'short' : 'query' => $value
$inputid = 'shortsearchbox'; ];
$inputsize = 12; return $this->render_from_template('core/search_input', $data);
break;
default :
$inputid = 'coursesearchbox';
$inputsize = 30;
}
$data = new stdClass();
$data->searchurl = \core_search\manager::get_course_search_url()->out(false);
$data->id = $formid;
$data->inputid = $inputid;
$data->inputsize = $inputsize;
$data->value = $value;
$data->areaids = 'core_course-course';
if ($format != 'navbar') {
$helpicon = new \help_icon('coursesearch', 'core');
$data->helpicon = $helpicon->export_for_template($this);
}
return $this->render_from_template('core_course/course_search_form', $data);
} }
/** /**
@ -1890,6 +1864,13 @@ class core_course_renderer extends plugin_renderer_base {
public function search_courses($searchcriteria) { public function search_courses($searchcriteria) {
global $CFG; global $CFG;
$content = ''; $content = '';
$search = '';
if (!empty($searchcriteria['search'])) {
$search = $searchcriteria['search'];
}
$content .= $this->course_search_form($search);
if (!empty($searchcriteria)) { if (!empty($searchcriteria)) {
// print search results // print search results
@ -1931,18 +1912,6 @@ class core_course_renderer extends plugin_renderer_base {
$content .= $this->heading(get_string('searchresults'). ": $totalcount"); $content .= $this->heading(get_string('searchresults'). ": $totalcount");
$content .= $courseslist; $content .= $courseslist;
} }
if (!empty($searchcriteria['search'])) {
// print search form only if there was a search by search string, otherwise it is confusing
$content .= $this->box_start('generalbox mdl-align');
$content .= $this->course_search_form($searchcriteria['search']);
$content .= $this->box_end();
}
} else {
// just print search form
$content .= $this->box_start('generalbox mdl-align');
$content .= $this->course_search_form();
$content .= $this->box_end();
} }
return $content; return $content;
} }
@ -2464,7 +2433,7 @@ class core_course_renderer extends plugin_renderer_base {
break; break;
case FRONTPAGECOURSESEARCH: case FRONTPAGECOURSESEARCH:
$output .= $this->box($this->course_search_form('', 'short'), 'mdl-align'); $output .= $this->box($this->course_search_form(''), 'd-flex justify-content-center');
break; break;
} }

View File

@ -95,7 +95,7 @@ if (empty($searchcriteria)) {
$aurl = new moodle_url('/course/management.php', $searchcriteria); $aurl = new moodle_url('/course/management.php', $searchcriteria);
$searchform = $OUTPUT->single_button($aurl, get_string('managecourses'), 'get'); $searchform = $OUTPUT->single_button($aurl, get_string('managecourses'), 'get');
} else { } else {
$searchform = $courserenderer->course_search_form($search, 'navbar'); $searchform = $courserenderer->course_search_form($search);
} }
$PAGE->set_button($searchform); $PAGE->set_button($searchform);