MDL-51283 core_tag: Allow each tag area to set 'showstandard'
[moodle.git] / lib / form / tags.php
CommitLineData
9f1c9dfc 1<?php
6c1fd304
RT
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
9f1c9dfc 17
18/**
bb6ca6f3 19 * Tag autocomplete field.
6c1fd304 20 *
4be9c7ad 21 * Contains HTML class for editing tags, both standard and not.
9f1c9dfc 22 *
6c1fd304
RT
23 * @package core_form
24 * @copyright 2009 Tim Hunt
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
9f1c9dfc 28global $CFG;
bb6ca6f3 29require_once($CFG->libdir . '/form/autocomplete.php');
9f1c9dfc 30
31/**
6c1fd304
RT
32 * Form field type for editing tags.
33 *
4be9c7ad 34 * HTML class for editing tags, both standard and not.
6c1fd304
RT
35 *
36 * @package core_form
6c1fd304
RT
37 * @copyright 2009 Tim Hunt
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9f1c9dfc 39 */
bb6ca6f3 40class MoodleQuickForm_tags extends MoodleQuickForm_autocomplete {
6c1fd304
RT
41 /**
42 * Inidcates that the user should be the usual interface, with the official
9f1c9dfc 43 * tags listed seprately, and a text box where they can type anything.
4be9c7ad 44 * @deprecated since 3.1
6c1fd304
RT
45 * @var int
46 */
f48dccd4 47 const DEFAULTUI = 'defaultui';
6c1fd304
RT
48
49 /**
50 * Indicates that the user should only be allowed to select official tags.
4be9c7ad 51 * @deprecated since 3.1
6c1fd304
RT
52 * @var int
53 */
f48dccd4 54 const ONLYOFFICIAL = 'onlyofficial';
6c1fd304
RT
55
56 /**
57 * Indicates that the user should just be given a text box to type in (they
9f1c9dfc 58 * can still type official tags though.
4be9c7ad 59 * @deprecated since 3.1
6c1fd304
RT
60 * @var int
61 */
f48dccd4 62 const NOOFFICIAL = 'noofficial';
9f1c9dfc 63
64 /**
4be9c7ad 65 * @var boolean $showstandard Standard tags suggested? (if not, then don't show link to manage standard tags).
9f1c9dfc 66 */
4be9c7ad 67 protected $showstandard = false;
9f1c9dfc 68
c026a28d
MG
69 /**
70 * Options passed when creating an element.
71 * @var array
72 */
73 protected $tagsoptions = array();
74
9f1c9dfc 75 /**
f48dccd4 76 * Constructor
77 *
78 * @param string $elementName Element name
79 * @param mixed $elementLabel Label(s) for an element
80 * @param array $options Options to control the element's display
81 * @param mixed $attributes Either a typical HTML attribute string or an associative array.
82 */
1a0df553 83 public function __construct($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
c026a28d
MG
84 $validoptions = array();
85
86 if (!empty($options)) {
87 // Only execute it when the element was created and $options has values set by user.
88 // In onQuickFormEvent() we make sure that $options is not empty even if developer left it empty.
4be9c7ad
MG
89 $showstandard = core_tag_tag::BOTH_STANDARD_AND_NOT;
90 if (isset($options['showstandard'])) {
91 $showstandard = $options['showstandard'];
92 } else if (isset($options['display'])) {
93 debugging('Option "display" is deprecated, each tag area can be configured to show standard tags or not ' .
94 'by admin or manager. If it is necessary for the developer to override it, please use "showstandard" option',
95 DEBUG_DEVELOPER);
96 if ($options['display'] === self::NOOFFICIAL) {
97 $showstandard = core_tag_tag::HIDE_STANDARD;
98 } else if ($options['display'] === self::ONLYOFFICIAL) {
99 $showstandard = core_tag_tag::STANDARD_ONLY;
100 }
101 } else if (!empty($options['component']) && !empty($options['itemtype'])) {
102 $showstandard = core_tag_area::get_showstandard($options['component'], $options['itemtype']);
c026a28d 103 }
c026a28d 104
4be9c7ad 105 $this->tagsoptions = $options;
c026a28d 106
4be9c7ad
MG
107 $this->showstandard = ($showstandard != core_tag_tag::HIDE_STANDARD);
108 if ($this->showstandard) {
109 $validoptions = $this->load_standard_tags();
c026a28d
MG
110 }
111 // Option 'tags' allows us to type new tags.
4be9c7ad 112 $attributes['tags'] = ($showstandard != core_tag_tag::STANDARD_ONLY);
c026a28d
MG
113 $attributes['multiple'] = 'multiple';
114 $attributes['placeholder'] = get_string('entertags', 'tag');
4be9c7ad 115 $attributes['showsuggestions'] = $this->showstandard;
9f1c9dfc 116 }
9f1c9dfc 117
c026a28d
MG
118 parent::__construct($elementName, $elementLabel, $validoptions, $attributes);
119 }
9f1c9dfc 120
c026a28d
MG
121 /**
122 * Called by HTML_QuickForm whenever form event is made on this element
123 *
124 * @param string $event Name of event
125 * @param mixed $arg event arguments
126 * @param object $caller calling object
127 * @return bool
128 */
129 public function onQuickFormEvent($event, $arg, &$caller) {
130 if ($event === 'createElement') {
131 $arg[2] += array('itemtype' => '', 'component' => '');
9f1c9dfc 132 }
c026a28d
MG
133 return parent::onQuickFormEvent($event, $arg, $caller);
134 }
9f1c9dfc 135
c026a28d
MG
136 /**
137 * Checks if tagging is enabled for this itemtype
138 *
139 * @return boolean
140 */
141 protected function is_tagging_enabled() {
142 if (!empty($this->tagsoptions['itemtype']) && !empty($this->tagsoptions['component'])) {
143 $enabled = core_tag_tag::is_enabled($this->tagsoptions['component'], $this->tagsoptions['itemtype']);
144 if ($enabled === false) {
145 return false;
146 }
147 }
148 // Backward compatibility with code developed before Moodle 3.0 where itemtype/component were not specified.
149 return true;
1a0df553
MG
150 }
151
152 /**
153 * Old syntax of class constructor. Deprecated in PHP7.
154 *
155 * @deprecated since Moodle 3.1
156 */
157 public function MoodleQuickForm_tags($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
158 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
159 self::__construct($elementName, $elementLabel, $options, $attributes);
9f1c9dfc 160 }
161
c026a28d 162 /**
4be9c7ad 163 * Finds the tag collection to use for standard tag selector
c026a28d
MG
164 *
165 * @return int
166 */
167 protected function get_tag_collection() {
168 if (empty($this->tagsoptions['tagcollid']) && (empty($this->tagsoptions['itemtype']) ||
169 empty($this->tagsoptions['component']))) {
170 debugging('You need to specify \'itemtype\' and \'component\' of the tagged '
171 . 'area in the tags form element options',
172 DEBUG_DEVELOPER);
173 }
174 if (!empty($this->tagsoptions['tagcollid'])) {
175 return $this->tagsoptions['tagcollid'];
176 }
177 if ($this->tagsoptions['itemtype']) {
178 $this->tagsoptions['tagcollid'] = core_tag_area::get_collection($this->tagsoptions['component'],
179 $this->tagsoptions['itemtype']);
180 } else {
181 $this->tagsoptions['tagcollid'] = core_tag_collection::get_default();
182 }
183 return $this->tagsoptions['tagcollid'];
184 }
185
6c1fd304 186 /**
bb6ca6f3 187 * Returns HTML for select form element.
6c1fd304 188 *
bb6ca6f3 189 * @return string
6c1fd304 190 */
bb6ca6f3 191 function toHtml(){
c026a28d 192 global $OUTPUT;
f48dccd4 193
bb6ca6f3 194 $managelink = '';
4be9c7ad 195 if (has_capability('moodle/tag:manage', context_system::instance()) && $this->showstandard) {
c026a28d 196 $url = new moodle_url('/tag/manage.php', array('tc' => $this->get_tag_collection()));
e11d7380 197 $managelink = ' ' . $OUTPUT->action_link($url, get_string('managestandardtags', 'tag'));
bb6ca6f3 198 }
f48dccd4 199
bb6ca6f3 200 return parent::toHTML() . $managelink;
9f1c9dfc 201 }
202
6c1fd304 203 /**
c026a28d 204 * Accepts a renderer
6c1fd304 205 *
c026a28d
MG
206 * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object
207 * @param bool $required Whether a group is required
208 * @param string $error An error message associated with a group
209 */
210 public function accept(&$renderer, $required = false, $error = null) {
211 if ($this->is_tagging_enabled()) {
212 $renderer->renderElement($this, $required, $error);
213 } else {
214 $renderer->renderHidden($this);
215 }
216 }
217
218 /**
4be9c7ad 219 * Internal function to load standard tags
6c1fd304 220 */
4be9c7ad 221 protected function load_standard_tags() {
bb6ca6f3 222 global $CFG, $DB;
c026a28d
MG
223 if (!$this->is_tagging_enabled()) {
224 return array();
225 }
bb6ca6f3 226 $namefield = empty($CFG->keeptagnamecase) ? 'name' : 'rawname';
c026a28d 227 $tags = $DB->get_records_menu('tag',
4be9c7ad 228 array('isstandard' => 1, 'tagcollid' => $this->get_tag_collection()),
c026a28d
MG
229 $namefield, 'id,' . $namefield);
230 return array_combine($tags, $tags);
231 }
9f1c9dfc 232
c026a28d
MG
233 /**
234 * Returns a 'safe' element's value
235 *
236 * @param array $submitValues array of submitted values to search
237 * @param bool $assoc whether to return the value as associative array
238 * @return mixed
239 */
240 public function exportValue(&$submitValues, $assoc = false) {
241 if (!$this->is_tagging_enabled()) {
242 return $assoc ? array($this->getName() => array()) : array();
9f1c9dfc 243 }
bb6ca6f3 244
c026a28d
MG
245 return parent::exportValue($submitValues, $assoc);
246 }
9f1c9dfc 247}