Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403]

   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Search renderer.
  19   *
  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
  23   */
  24  
  25  namespace core_search\output;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Search renderer.
  31   *
  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
  35   */
  36  class renderer extends \plugin_renderer_base {
  37  
  38      /**
  39       * @var int Max number chars to display of a string value
  40       */
  41      const SEARCH_RESULT_STRING_SIZE = 100;
  42  
  43      /**
  44       * @var int Max number chars to display of a text value
  45       */
  46  
  47      const SEARCH_RESULT_TEXT_SIZE = 500;
  48  
  49      /**
  50       * Renders search results.
  51       *
  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       * @param \core_search\area_category|null $cat Selected search are category or null if category functionality is disabled.
  57       * @return string HTML
  58       */
  59      public function render_results($results, $page, $totalcount, $url, $cat = null) {
  60          $content = '';
  61  
  62          if (\core_search\manager::is_search_area_categories_enabled() && !empty($cat)) {
  63              $toprow = [];
  64              foreach (\core_search\manager::get_search_area_categories() as $category) {
  65                  $taburl = clone $url;
  66                  $taburl->param('cat', $category->get_name());
  67                  $taburl->param('page', 0);
  68                  $taburl->remove_params(['page', 'areaids']);
  69                  $toprow[$category->get_name()] = new \tabobject($category->get_name(), $taburl, $category->get_visiblename());
  70              }
  71  
  72              if (\core_search\manager::should_hide_all_results_category()) {
  73                  unset($toprow[\core_search\manager::SEARCH_AREA_CATEGORY_ALL]);
  74              }
  75  
  76              $content .= $this->tabtree($toprow, $cat->get_name());
  77          }
  78  
  79          // Paging bar.
  80          $perpage = \core_search\manager::DISPLAY_RESULTS_PER_PAGE;
  81          $content .= $this->output->paging_bar($totalcount, $page, $perpage, $url);
  82  
  83          // Results.
  84          $resultshtml = array();
  85          foreach ($results as $hit) {
  86              $resultshtml[] = $this->render_result($hit);
  87          }
  88          $content .= \html_writer::tag('div', implode('<hr/>', $resultshtml), array('class' => 'search-results'));
  89  
  90          // Paging bar.
  91          $content .= $this->output->paging_bar($totalcount, $page, $perpage, $url);
  92  
  93          return $content;
  94      }
  95  
  96      /**
  97       * Top results content
  98       *
  99       * @param \core_search\document[] $results Search Results
 100       * @return string content of the top result section
 101       */
 102      public function render_top_results($results): string {
 103          $content = $this->output->box_start('topresults');
 104          $content .= $this->output->heading(get_string('topresults', 'core_search'));
 105          $content .= \html_writer::tag('hr', '');
 106          $resultshtml = array();
 107          foreach ($results as $hit) {
 108              $resultshtml[] = $this->render_result($hit);
 109          }
 110          $content .= \html_writer::tag('div', implode('<hr/>', $resultshtml), array('class' => 'search-results'));
 111          $content .= $this->output->box_end();
 112          return $content;
 113      }
 114  
 115      /**
 116       * Displaying search results.
 117       *
 118       * @param \core_search\document Containing a single search response to be displayed.a
 119       * @return string HTML
 120       */
 121      public function render_result(\core_search\document $doc) {
 122          $docdata = $doc->export_for_template($this);
 123  
 124          // Limit text fields size.
 125          $docdata['title'] = shorten_text($docdata['title'], static::SEARCH_RESULT_STRING_SIZE, true);
 126          $docdata['content'] = $docdata['content'] ? shorten_text($docdata['content'], static::SEARCH_RESULT_TEXT_SIZE, true) : '';
 127          $docdata['description1'] = $docdata['description1'] ? shorten_text($docdata['description1'], static::SEARCH_RESULT_TEXT_SIZE, true) : '';
 128          $docdata['description2'] = $docdata['description2'] ? shorten_text($docdata['description2'], static::SEARCH_RESULT_TEXT_SIZE, true) : '';
 129  
 130          return $this->output->render_from_template('core_search/result', $docdata);
 131      }
 132  
 133      /**
 134       * Returns a box with a search disabled lang string.
 135       *
 136       * @return string HTML
 137       */
 138      public function render_search_disabled() {
 139          $content = $this->output->box_start();
 140          $content .= $this->output->notification(get_string('globalsearchdisabled', 'search'), 'notifymessage');
 141          $content .= $this->output->box_end();
 142          return $content;
 143      }
 144  
 145      /**
 146       * Returns information about queued index requests.
 147       *
 148       * @param \stdClass $info Info object from get_index_requests_info
 149       * @return string HTML
 150       * @throws \moodle_exception Any error with template
 151       */
 152      public function render_index_requests_info(\stdClass $info) {
 153          return $this->output->render_from_template('core_search/index_requests', $info);
 154      }
 155  }