Merge branch 'MDL-63765-master' of git://github.com/damyon/moodle
[moodle.git] / search / classes / output / renderer.php
blob97cbe29e1109bdc40d9ad611d86ac0c17fbc79ee
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Search renderer.
20 * @package core_search
21 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_search\output;
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Search renderer.
32 * @package core_search
33 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class renderer extends \plugin_renderer_base {
38 /**
39 * @var int Max number chars to display of a string value
41 const SEARCH_RESULT_STRING_SIZE = 100;
43 /**
44 * @var int Max number chars to display of a text value
47 const SEARCH_RESULT_TEXT_SIZE = 500;
49 /**
50 * Renders search results.
52 * @param \core_search\document[] $results
53 * @param int $page Zero based page number.
54 * @param int $totalcount Total number of results available.
55 * @param \moodle_url $url
56 * @return string HTML
58 public function render_results($results, $page, $totalcount, $url) {
60 // Paging bar.
61 $perpage = \core_search\manager::DISPLAY_RESULTS_PER_PAGE;
62 $content = $this->output->paging_bar($totalcount, $page, $perpage, $url);
64 // Results.
65 $resultshtml = array();
66 foreach ($results as $hit) {
67 $resultshtml[] = $this->render_result($hit);
69 $content .= \html_writer::tag('div', implode('<hr/>', $resultshtml), array('class' => 'search-results'));
71 // Paging bar.
72 $content .= $this->output->paging_bar($totalcount, $page, $perpage, $url);
74 return $content;
77 /**
78 * Displaying search results.
80 * @param \core_search\document Containing a single search response to be displayed.a
81 * @return string HTML
83 public function render_result(\core_search\document $doc) {
84 $docdata = $doc->export_for_template($this);
86 // Limit text fields size.
87 $docdata['title'] = shorten_text($docdata['title'], static::SEARCH_RESULT_STRING_SIZE, true);
88 $docdata['content'] = $docdata['content'] ? shorten_text($docdata['content'], static::SEARCH_RESULT_TEXT_SIZE, true) : '';
89 $docdata['description1'] = $docdata['description1'] ? shorten_text($docdata['description1'], static::SEARCH_RESULT_TEXT_SIZE, true) : '';
90 $docdata['description2'] = $docdata['description2'] ? shorten_text($docdata['description2'], static::SEARCH_RESULT_TEXT_SIZE, true) : '';
92 return $this->output->render_from_template('core_search/result', $docdata);
95 /**
96 * Returns a box with a search disabled lang string.
98 * @return string HTML
100 public function render_search_disabled() {
101 $content = $this->output->box_start();
102 $content .= $this->output->notification(get_string('globalsearchdisabled', 'search'), 'notifymessage');
103 $content .= $this->output->box_end();
104 return $content;
108 * Returns information about queued index requests.
110 * @param \stdClass $info Info object from get_index_requests_info
111 * @return string HTML
112 * @throws \moodle_exception Any error with template
114 public function render_index_requests_info(\stdClass $info) {
115 return $this->output->render_from_template('core_search/index_requests', $info);