weekly release 4.0dev
[moodle.git] / blocks / tags / block_tags.php
CommitLineData
4ca6cfbf 1<?php
7dd764b8
JG
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
f25a6839
SH
17/**
18 * Tags block.
19 *
20 * @package block_tags
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
d797e50f 24
25class block_tags extends block_base {
7dd764b8 26 public function init() {
8d4904be 27 $this->title = get_string('pluginname', 'block_tags');
d797e50f 28 }
29
7dd764b8 30 public function instance_allow_multiple() {
d797e50f 31 return true;
32 }
33
7dd764b8 34 public function applicable_formats() {
cdcaa9f2 35 return array('all' => true);
d797e50f 36 }
37
7dd764b8 38 public function instance_allow_config() {
d797e50f 39 return true;
40 }
41
7dd764b8 42 public function specialization() {
d797e50f 43
7dd764b8 44 // Load userdefined title and make sure it's never empty.
d797e50f 45 if (empty($this->config->title)) {
7dd764b8 46 $this->title = get_string('pluginname', 'block_tags');
d797e50f 47 } else {
1b374b4f 48 $this->title = format_string($this->config->title, true, ['context' => $this->context]);
d797e50f 49 }
50 }
51
7dd764b8 52 public function get_content() {
d797e50f 53
7dd764b8 54 global $CFG, $COURSE, $USER, $SCRIPT, $OUTPUT;
d797e50f 55
56 if (empty($CFG->usetags)) {
7c2cc9c8 57 $this->content = new stdClass();
d797e50f 58 $this->content->text = '';
b58961ac 59 if ($this->page->user_is_editing()) {
60 $this->content->text = get_string('disabledtags', 'block_tags');
61 }
d797e50f 62 return $this->content;
63 }
64
b85b25eb
PS
65 if (!isset($this->config)) {
66 $this->config = new stdClass();
67 }
68
d797e50f 69 if (empty($this->config->numberoftags)) {
70 $this->config->numberoftags = 80;
71 }
72
e11d7380
MG
73 if (empty($this->config->showstandard)) {
74 $this->config->showstandard = core_tag_tag::BOTH_STANDARD_AND_NOT;
b0c62e00
MG
75 }
76
77 if (empty($this->config->ctx)) {
78 $this->config->ctx = 0;
79 }
80
81 if (empty($this->config->rec)) {
82 $this->config->rec = 1;
83 }
84
85 if (empty($this->config->tagcoll)) {
86 $this->config->tagcoll = 0;
87 }
88
d797e50f 89 if ($this->content !== NULL) {
90 return $this->content;
91 }
92
93 if (empty($this->instance)) {
94 $this->content = '';
95 return $this->content;
96 }
97
98 $this->content = new stdClass;
c891a73d 99 $this->content->text = '';
d797e50f 100 $this->content->footer = '';
101
7dd764b8 102 // Get a list of tags.
d797e50f 103
b0c62e00 104 $tagcloud = core_tag_collection::get_tag_cloud($this->config->tagcoll,
e11d7380 105 $this->config->showstandard == core_tag_tag::STANDARD_ONLY,
b0c62e00
MG
106 $this->config->numberoftags,
107 'name', '', $this->page->context->id, $this->config->ctx, $this->config->rec);
108 $this->content->text = $OUTPUT->render_from_template('core_tag/tagcloud', $tagcloud->export_for_template($OUTPUT));
d797e50f 109
110 return $this->content;
111 }
bc72a54c
JL
112
113 /**
114 * Return the plugin config settings for external functions.
115 *
116 * @return stdClass the configs for both the block instance and plugin
117 * @since Moodle 3.8
118 */
119 public function get_config_for_external() {
120 // Return all settings for all users since it is safe (no private keys, etc..).
121 $configs = !empty($this->config) ? $this->config : new stdClass();
122
123 return (object) [
124 'instance' => $configs,
125 'plugin' => new stdClass(),
126 ];
127 }
d0dff189 128}