Skip to content

Commit

Permalink
MDL-55474 block_search_forums: Convert search form to templates
Browse files Browse the repository at this point in the history
Part of MDL-55071
  • Loading branch information
Frederic Massart authored and danpoltawski committed Sep 23, 2016
1 parent 9727e14 commit 270dd87
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 15 deletions.
18 changes: 3 additions & 15 deletions blocks/search_forums/block_search_forums.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,9 @@ function get_content() {
return $this->content;
}

$advancedsearch = get_string('advancedsearch', 'block_search_forums');

$strsearch = get_string('search');
$strgo = get_string('go');

$this->content->text = '<div class="searchform">';
$this->content->text .= '<form action="'.$CFG->wwwroot.'/mod/forum/search.php" style="display:inline"><fieldset class="invisiblefieldset">';
$this->content->text .= '<legend class="accesshide">'.$strsearch.'</legend>';
$this->content->text .= '<input name="id" type="hidden" value="'.$this->page->course->id.'" />'; // course
$this->content->text .= '<label class="accesshide" for="searchform_search">'.$strsearch.'</label>'.
'<input id="searchform_search" name="search" type="text" size="16" />';
$this->content->text .= '<button id="searchform_button" type="submit" title="'.$strsearch.'">'.$strgo.'</button><br />';
$this->content->text .= '<a href="'.$CFG->wwwroot.'/mod/forum/search.php?id='.$this->page->course->id.'">'.$advancedsearch.'</a>';
$this->content->text .= $OUTPUT->help_icon('search');
$this->content->text .= '</fieldset></form></div>';
$output = $this->page->get_renderer('block_search_forums');
$searchform = new \block_search_forums\output\search_form($this->page->course->id);
$this->content->text = $output->render($searchform);

return $this->content;
}
Expand Down
50 changes: 50 additions & 0 deletions blocks/search_forums/classes/output/renderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
// 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/>.

/**
* Block search forums renderer.
*
* @package block_search_forums
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace block_search_forums\output;
defined('MOODLE_INTERNAL') || die();

use plugin_renderer_base;
use renderable;

/**
* Block search forums renderer.
*
* @package block_search_forums
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends plugin_renderer_base {

/**
* Render search form.
*
* @param renderable $searchform The search form.
* @return string
*/
public function render_search_form(renderable $searchform) {
return $this->render_from_template('block_search_forums/search_form', $searchform->export_for_template($this));
}

}
74 changes: 74 additions & 0 deletions blocks/search_forums/classes/output/search_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
// 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/>.

/**
* Search form renderable.
*
* @package block_search_forums
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace block_search_forums\output;
defined('MOODLE_INTERNAL') || die();

use help_icon;
use moodle_url;
use renderable;
use renderer_base;
use templatable;

/**
* Search form renderable class.
*
* @package block_search_forums
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class search_form implements renderable, templatable {

/** @var int The course ID. */
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;

/**
* Constructor.
*
* @param int $courseid The course ID.
*/
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->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),
'helpicon' => $this->helpicon->export_for_template($output),
];
return $data;
}

}
15 changes: 15 additions & 0 deletions blocks/search_forums/templates/search_form.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div class="searchform">
<form action="{{actionurl}}" style="display: inline;">
<fieldset class="invisiblefieldset">
<legend class="accesshide">{{#str}}search{{/str}}</legend>
<input type="hidden" name="id" value="{{courseid}}">
<label class="accesshide" for="searchform_search">{{#str}}search{{/str}}</label>
<input id="searchform_search" name="search" type="text" size="16">
<button id="searchform_button" type="submit" title={{#quote}}{{#str}}search{{/str}}{{/quote}}>{{#str}}go{{/str}}</button><br>
<a href="{{advancedsearchurl}}">{{#str}}advancedsearch, block_search_forums{{/str}}</a>
{{#helpicon}}
{{>core/help_icon}}
{{/helpicon}}
</fieldset>
</form>
</div>
5 changes: 5 additions & 0 deletions theme/noname/scss/moodle/blocks.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
position: relative;
left: initial;
}

.block_search_forums .searchform {
/* Override plugin's default. */
text-align: left;
}
17 changes: 17 additions & 0 deletions theme/noname/templates/block_search_forums/search_form.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="searchform">
<form action="{{actionurl}}" class="form-inline">
<input type="hidden" name="id" value="{{courseid}}">
<div class="form-group">
<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>
<button class="btn btn-secondary" id="searchform_button" type="submit">{{#str}}go{{/str}}</button>
</fieldset>
</form>
<div class="m-t-1">
<a href="{{advancedsearchurl}}">{{#str}}advancedsearch, block_search_forums{{/str}}</a>
{{#helpicon}}
{{>core/help_icon}}
{{/helpicon}}
</div>
</div>

0 comments on commit 270dd87

Please sign in to comment.