MDL-55474 block_search_forums: Convert search form to templates
authorFrederic Massart <fred@moodle.com>
Fri, 5 Aug 2016 04:35:15 +0000 (12:35 +0800)
committerDan Poltawski <dan@moodle.com>
Fri, 23 Sep 2016 09:53:29 +0000 (10:53 +0100)
Part of MDL-55071

blocks/search_forums/block_search_forums.php
blocks/search_forums/classes/output/renderer.php [new file with mode: 0644]
blocks/search_forums/classes/output/search_form.php [new file with mode: 0644]
blocks/search_forums/templates/search_form.mustache [new file with mode: 0644]
theme/noname/scss/moodle/blocks.scss
theme/noname/templates/block_search_forums/search_form.mustache [new file with mode: 0644]

index 2bf7d6c..669d5e8 100644 (file)
@@ -42,21 +42,9 @@ class block_search_forums extends block_base {
             return $this->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;
     }
 
         return $this->content;
     }
diff --git a/blocks/search_forums/classes/output/renderer.php b/blocks/search_forums/classes/output/renderer.php
new file mode 100644 (file)
index 0000000..292ce34
--- /dev/null
@@ -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));
+    }
+
+}
diff --git a/blocks/search_forums/classes/output/search_form.php b/blocks/search_forums/classes/output/search_form.php
new file mode 100644 (file)
index 0000000..8213abe
--- /dev/null
@@ -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;
+    }
+
+}
diff --git a/blocks/search_forums/templates/search_form.mustache b/blocks/search_forums/templates/search_form.mustache
new file mode 100644 (file)
index 0000000..9f0411f
--- /dev/null
@@ -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>
index 0a2e0b7..81a3ca5 100644 (file)
@@ -2,3 +2,8 @@
     position: relative;
     left: initial;
 }
     position: relative;
     left: initial;
 }
+
+.block_search_forums .searchform {
+    /* Override plugin's default. */
+    text-align: left;
+}
diff --git a/theme/noname/templates/block_search_forums/search_form.mustache b/theme/noname/templates/block_search_forums/search_form.mustache
new file mode 100644 (file)
index 0000000..f34e895
--- /dev/null
@@ -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>