Moodle release 3.3rc1
[moodle.git] / tag / classes / renderer.php
blob05f3074e84615140f6e265ba4956ace9d8c79345
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 * Contains class core_tag_renderer
20 * @package core_tag
21 * @copyright 2015 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Class core_tag_renderer
30 * @package core_tag
31 * @copyright 2015 Marina Glancy
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class core_tag_renderer extends plugin_renderer_base {
36 /**
37 * Renders the tag search page
39 * @param string $query
40 * @param int $tagcollid
41 * @return string
43 public function tag_search_page($query = '', $tagcollid = 0) {
44 $rv = $this->output->heading(get_string('searchtags', 'tag'), 2);
46 $searchbox = $this->search_form($query, $tagcollid);
47 $rv .= html_writer::div($searchbox, '', array('id' => 'tag-search-box'));
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>';
59 return $rv;
62 /**
63 * Renders the tag index page
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
77 public function tag_index_page($tag, $entities, $tagareaid, $exclusivemode, $fromctx, $ctx, $rec, $page) {
78 global $CFG, $OUTPUT;
79 $this->page->requires->js_call_amd('core/tag', 'initTagindexPage');
81 $tagname = $tag->get_display_name();
82 $systemcontext = context_system::instance();
84 if ($tag->flag > 0 && has_capability('moodle/tag:manage', $systemcontext)) {
85 $tagname = '<span class="flagged-tag">' . $tagname . '</span>';
88 $rv = '';
89 $rv .= $this->output->heading($tagname, 2);
91 $rv .= $this->tag_links($tag);
93 if ($desciption = $tag->get_formatted_description()) {
94 $rv .= $this->output->box($desciption, 'generalbox tag-description');
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 .= $OUTPUT->render_from_template('core_tag/taglist', $taglist->export_for_template($OUTPUT));
103 // Display quick menu of the item types (if more than one item type found).
104 $entitylinks = array();
105 foreach ($entities as $entity) {
106 if (!empty($entity->hascontent)) {
107 $entitylinks[] = '<li><a href="#'.$entity->anchor.'">' .
108 core_tag_area::display_name($entity->component, $entity->itemtype) . '</a></li>';
112 if (count($entitylinks) > 1) {
113 $rv .= '<div class="tag-index-toc"><ul class="inline-list">' . join('', $entitylinks) . '</ul></div>';
114 } else if (!$entitylinks) {
115 $rv .= '<div class="tag-noresults">' . get_string('noresultsfor', 'tag', $tagname) . '</div>';
118 // Display entities tagged with the tag.
119 $content = '';
120 foreach ($entities as $entity) {
121 if (!empty($entity->hascontent)) {
122 $content .= $this->output->render_from_template('core_tag/index', $entity->export_for_template($this->output));
126 if ($exclusivemode) {
127 $rv .= $content;
128 } else if ($content) {
129 $rv .= html_writer::div($content, 'tag-index-items');
132 // Display back link if we are browsing one tag area.
133 if ($tagareaid) {
134 $url = $tag->get_view_url(0, $fromctx, $ctx, $rec);
135 $rv .= '<div class="tag-backtoallitems">' .
136 html_writer::link($url, get_string('backtoallitems', 'tag', $tag->get_display_name())) .
137 '</div>';
140 return $rv;
144 * Prints a box that contains the management links of a tag
146 * @param core_tag_tag $tag
147 * @return string
149 protected function tag_links($tag) {
150 if ($links = $tag->get_links()) {
151 $content = '<ul class="inline-list"><li>' . implode('</li> <li>', $links) . '</li></ul>';
152 return html_writer::div($content, 'tag-management-box');
154 return '';
158 * Prints the tag search box
160 * @param string $query last search string
161 * @param int $tagcollid last selected tag collection id
162 * @return string
164 protected function search_form($query = '', $tagcollid = 0) {
165 $searchurl = new moodle_url('/tag/search.php');
166 $output = '<form action="' . $searchurl . '">';
167 $output .= '<label class="accesshide" for="searchform_query">' . get_string('searchtags', 'tag') . '</label>';
168 $output .= '<input id="searchform_query" name="query" type="text" size="40" value="' . s($query) . '" />';
169 $tagcolls = core_tag_collection::get_collections_menu(false, true, get_string('inalltagcoll', 'tag'));
170 if (count($tagcolls) > 1) {
171 $output .= '<label class="accesshide" for="searchform_tc">' . get_string('selectcoll', 'tag') . '</label>';
172 $output .= html_writer::select($tagcolls, 'tc', $tagcollid, null, array('id' => 'searchform_tc'));
174 $output .= '<input name="go" type="submit" size="40" value="' . s(get_string('search', 'tag')) . '" />';
175 $output .= '</form>';
177 return $output;