Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   * Contains class core_tag_renderer
  19   *
  20   * @package   core_tag
  21   * @copyright 2015 Marina Glancy
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Class core_tag_renderer
  29   *
  30   * @package   core_tag
  31   * @copyright 2015 Marina Glancy
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class core_tag_renderer extends plugin_renderer_base {
  35  
  36      /**
  37       * Renders the tag search page
  38       *
  39       * @param string $query
  40       * @param int $tagcollid
  41       * @return string
  42       */
  43      public function tag_search_page($query = '', $tagcollid = 0) {
  44          $rv = $this->output->heading(get_string('searchtags', 'tag'), 2);
  45  
  46          $searchbox = $this->search_form($query, $tagcollid);
  47          $rv .= html_writer::div($searchbox, '', array('id' => 'tag-search-box'));
  48  
  49          $tagcloud = core_tag_collection::get_tag_cloud($tagcollid, false, 150, 'name', $query);
  50          $searchresults = '';
  51          if ($tagcloud->get_count()) {
  52              $searchresults = $this->output->render_from_template('core_tag/tagcloud',
  53                      $tagcloud->export_for_template($this->output));
  54              $rv .= html_writer::div($searchresults, '', array('id' => 'tag-search-results'));
  55          } else if (strval($query) !== '') {
  56              $rv .= '<div class="tag-search-empty">' . get_string('notagsfound', 'tag', s($query)) . '</div>';
  57          }
  58  
  59          return $rv;
  60      }
  61  
  62      /**
  63       * Renders the tag index page
  64       *
  65       * @param core_tag_tag $tag
  66       * @param \core_tag\output\tagindex[] $entities
  67       * @param int $tagareaid
  68       * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
  69       *             are displayed on the page and the per-page limit may be bigger
  70       * @param int $fromctx context id where the link was displayed, may be used by callbacks
  71       *            to display items in the same context first
  72       * @param int $ctx context id where to search for records
  73       * @param bool $rec search in subcontexts as well
  74       * @param int $page 0-based number of page being displayed
  75       * @return string
  76       */
  77      public function tag_index_page($tag, $entities, $tagareaid, $exclusivemode, $fromctx, $ctx, $rec, $page) {
  78          global $CFG;
  79          $this->page->requires->js_call_amd('core/tag', 'initTagindexPage');
  80  
  81          $tagname = $tag->get_display_name();
  82          $systemcontext = context_system::instance();
  83  
  84          if ($tag->flag > 0 && has_capability('moodle/tag:manage', $systemcontext)) {
  85              $tagname = '<span class="flagged-tag">' . $tagname . '</span>';
  86          }
  87  
  88          $rv = '';
  89          $rv .= $this->output->heading($tagname, 2);
  90  
  91          $rv .= $this->tag_links($tag);
  92  
  93          if ($desciption = $tag->get_formatted_description()) {
  94              $rv .= $this->output->box($desciption, 'generalbox tag-description');
  95          }
  96  
  97          $relatedtagslimit = 10;
  98          $relatedtags = $tag->get_related_tags();
  99          $taglist = new \core_tag\output\taglist($relatedtags, get_string('relatedtags', 'tag'),
 100                  'tag-relatedtags', $relatedtagslimit);
 101          $rv .= $this->output->render_from_template('core_tag/taglist',
 102                  $taglist->export_for_template($this->output));
 103  
 104          // Display quick menu of the item types (if more than one item type found).
 105          $entitylinks = array();
 106          foreach ($entities as $entity) {
 107              if (!empty($entity->hascontent)) {
 108                  $entitylinks[] = '<li><a href="#'.$entity->anchor.'">' .
 109                          core_tag_area::display_name($entity->component, $entity->itemtype) . '</a></li>';
 110              }
 111          }
 112  
 113          if (count($entitylinks) > 1) {
 114              $rv .= '<div class="tag-index-toc"><ul class="inline-list">' . join('', $entitylinks) . '</ul></div>';
 115          } else if (!$entitylinks) {
 116              $rv .= '<div class="tag-noresults">' . get_string('noresultsfor', 'tag', $tagname) . '</div>';
 117          }
 118  
 119          // Display entities tagged with the tag.
 120          $content = '';
 121          foreach ($entities as $entity) {
 122              if (!empty($entity->hascontent)) {
 123                  $content .= $this->output->render_from_template('core_tag/index', $entity->export_for_template($this->output));
 124              }
 125          }
 126  
 127          if ($exclusivemode) {
 128              $rv .= $content;
 129          } else if ($content) {
 130              $rv .= html_writer::div($content, 'tag-index-items');
 131          }
 132  
 133          // Display back link if we are browsing one tag area.
 134          if ($tagareaid) {
 135              $url = $tag->get_view_url(0, $fromctx, $ctx, $rec);
 136              $rv .= '<div class="tag-backtoallitems">' .
 137                      html_writer::link($url, get_string('backtoallitems', 'tag', $tag->get_display_name())) .
 138                      '</div>';
 139          }
 140  
 141          return $rv;
 142      }
 143  
 144      /**
 145       * Prints a box that contains the management links of a tag
 146       *
 147       * @param core_tag_tag $tag
 148       * @return string
 149       */
 150      protected function tag_links($tag) {
 151          if ($links = $tag->get_links()) {
 152              $content = '<ul class="inline-list"><li>' . implode('</li> <li>', $links) . '</li></ul>';
 153              return html_writer::div($content, 'tag-management-box');
 154          }
 155          return '';
 156      }
 157  
 158      /**
 159       * Prints the tag search box
 160       *
 161       * @param string $query last search string
 162       * @param int $tagcollid last selected tag collection id
 163       * @return string
 164       */
 165      protected function search_form($query = '', $tagcollid = 0) {
 166          $searchurl = new moodle_url('/tag/search.php');
 167          $output = '<form action="' . $searchurl . '">';
 168          $output .= '<label class="accesshide" for="searchform_query">' . get_string('searchtags', 'tag') . '</label>';
 169          $output .= '<input id="searchform_query" name="query" type="text" size="40" value="' . s($query) . '" />';
 170          $tagcolls = core_tag_collection::get_collections_menu(false, true, get_string('inalltagcoll', 'tag'));
 171          if (count($tagcolls) > 1) {
 172              $output .= '<label class="accesshide" for="searchform_tc">' . get_string('selectcoll', 'tag') . '</label>';
 173              $output .= html_writer::select($tagcolls, 'tc', $tagcollid, null, array('id' => 'searchform_tc'));
 174          }
 175          $output .= '<input name="go" type="submit" size="40" value="' . s(get_string('search', 'tag')) . '" />';
 176          $output .= '</form>';
 177  
 178          return $output;
 179      }
 180  
 181  }