MDL-39888 mnet: Removal of an old sql statement invloving the mdl_log table.
[moodle.git] / lib / form / tags.php
blob2c60979bfc8ddc26886aad1cb67040b6ce8898e3
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/>.
18 /**
19 * Drop down for question categories.
21 * Contains HTML class for editing tags, both official and peronal.
23 * @package core_form
24 * @copyright 2009 Tim Hunt
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 global $CFG;
29 require_once($CFG->libdir . '/form/group.php');
31 /**
32 * Form field type for editing tags.
34 * HTML class for editing tags, both official and peronal.
36 * @package core_form
37 * @category form
38 * @copyright 2009 Tim Hunt
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class MoodleQuickForm_tags extends MoodleQuickForm_group {
42 /**
43 * Inidcates that the user should be the usual interface, with the official
44 * tags listed seprately, and a text box where they can type anything.
45 * @var int
47 const DEFAULTUI = 'defaultui';
49 /**
50 * Indicates that the user should only be allowed to select official tags.
51 * @var int
53 const ONLYOFFICIAL = 'onlyofficial';
55 /**
56 * Indicates that the user should just be given a text box to type in (they
57 * can still type official tags though.
58 * @var int
60 const NOOFFICIAL = 'noofficial';
62 /**
63 * Control the fieldnames for form elements display => int, one of the constants above.
64 * @var array
66 protected $_options = array('display' => MoodleQuickForm_tags::DEFAULTUI);
68 /**
69 * Caches the list of official tags, to save repeat DB queries.
70 * @var array
72 protected $_officialtags = null;
74 /**
75 * Constructor
77 * @param string $elementName Element name
78 * @param mixed $elementLabel Label(s) for an element
79 * @param array $options Options to control the element's display
80 * @param mixed $attributes Either a typical HTML attribute string or an associative array.
82 function MoodleQuickForm_tags($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
83 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
84 $this->_persistantFreeze = true;
85 $this->_appendName = true;
86 $this->_type = 'tags';
87 // set the options, do not bother setting bogus ones
88 if (is_array($options)) {
89 foreach ($options as $name => $value) {
90 if (isset($this->_options[$name])) {
91 if (is_array($value) && is_array($this->_options[$name])) {
92 $this->_options[$name] = array_merge($this->_options[$name], $value);
93 } else {
94 $this->_options[$name] = $value;
99 global $CFG;
100 if (empty($CFG->usetags)) {
101 debugging('A tags formslib field has been created even thought $CFG->usetags is false.', DEBUG_DEVELOPER);
106 * Internal function to load official tags
108 * @access protected
110 protected function _load_official_tags() {
111 global $CFG, $DB;
112 if (!is_null($this->_officialtags)) {
113 return;
115 $namefield = empty($CFG->keeptagnamecase) ? 'name' : 'rawname';
116 $this->_officialtags = $DB->get_records_menu('tag', array('tagtype' => 'official'), $namefield, 'id,' . $namefield);
120 * Creates the group's elements.
122 function _createElements() {
123 global $CFG, $OUTPUT;
124 $this->_elements = array();
126 // Official tags.
127 $showingofficial = $this->_options['display'] != MoodleQuickForm_tags::NOOFFICIAL;
128 if ($showingofficial) {
129 $this->_load_official_tags();
131 // If the user can manage official tags, give them a link to manage them.
132 $label = get_string('otags', 'tag');
133 if (has_capability('moodle/tag:manage', context_system::instance())) {
134 $url = $CFG->wwwroot .'/tag/manage.php';
135 $label .= ' (' . $OUTPUT->action_link(
136 $url,
137 get_string('manageofficialtags', 'tag'),
138 new popup_action('click', $url, 'managetags'),
139 array('title'=>get_string('newwindow'))) . ')';
142 // Get the list of official tags.
143 $noofficial = false;
144 if (empty($this->_officialtags)) {
145 $officialtags = array('' => get_string('none'));
146 $noofficial = true;
147 } else {
148 $officialtags = array_combine($this->_officialtags, $this->_officialtags);
151 // Create the element.
152 $size = min(5, count($officialtags));
153 // E_STRICT creating elements without forms is nasty because it internally uses $this
154 $officialtagsselect = @MoodleQuickForm::createElement('select', 'officialtags', $label, $officialtags, array('size' => $size));
155 $officialtagsselect->setMultiple(true);
156 if ($noofficial) {
157 $officialtagsselect->updateAttributes(array('disabled' => 'disabled'));
159 $this->_elements[] = $officialtagsselect;
162 // Other tags.
163 if ($this->_options['display'] != MoodleQuickForm_tags::ONLYOFFICIAL) {
164 if ($showingofficial) {
165 $label = get_string('othertags', 'tag');
166 } else {
167 $label = get_string('entertags', 'tag');
169 // E_STRICT creating elements without forms is nasty because it internally uses $this
170 $othertags = @MoodleQuickForm::createElement('textarea', 'othertags', $label, array('cols'=>'40', 'rows'=>'5'));
171 $this->_elements[] = $othertags;
174 // Paradoxically, the only way to get labels output is to ask for 'hidden'
175 // labels, and then override the .accesshide class in the CSS!
176 foreach ($this->_elements as $element){
177 if (method_exists($element, 'setHiddenLabel')){
178 $element->setHiddenLabel(true);
184 * Called by HTML_QuickForm whenever form event is made on this element
186 * @param string $event Name of event
187 * @param mixed $arg event arguments
188 * @param object $caller calling object
190 function onQuickFormEvent($event, $arg, &$caller) {
191 switch ($event) {
192 case 'updateValue':
193 // Get the value we should be setting.
194 $value = $this->_findValue($caller->_constantValues);
195 if (null === $value) {
196 // if no boxes were checked, then there is no value in the array
197 // yet we don't want to display default value in this case
198 if ($caller->isSubmitted()) {
199 $value = $this->_findValue($caller->_submitValues);
200 } else {
201 $value = $this->_findValue($caller->_defaultValues);
205 if (!empty($value) && !(isset($value['officialtags']) || isset($value['othertags']))) {
206 // Separate the official and unoffical tags, if necessary.
207 $official = array();
208 $other = array();
209 if ($this->_options['display'] != MoodleQuickForm_tags::NOOFFICIAL) {
210 $this->_load_official_tags();
211 if (!empty($this->_officialtags)) {
212 $officaltags = array_combine($this->_officialtags, $this->_officialtags);
213 } else {
214 $officaltags = array();
216 foreach ($value as $tag) {
217 if (isset($officaltags[$tag])) {
218 $official[] = $tag;
219 } else {
220 $other[] = $tag;
223 } else {
224 $other = $value;
226 $value = array('officialtags' => $official, 'othertags' => implode(', ', $other));
228 if (!empty($value)) {
229 $this->setValue($value);
232 break;
233 default:
234 return parent::onQuickFormEvent($event, $arg, $caller);
239 * Returns HTML for submitlink form element.
241 * @return string
243 function toHtml() {
244 require_once('HTML/QuickForm/Renderer/Default.php');
245 $renderer = new HTML_QuickForm_Renderer_Default();
246 $renderer->setElementTemplate('{element}');
247 parent::accept($renderer);
248 return $renderer->toHtml();
252 * Accepts a renderer
254 * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object
255 * @param bool $required Whether a group is required
256 * @param string $error An error message associated with a group
258 function accept(&$renderer, $required = false, $error = null)
260 $renderer->renderElement($this, $required, $error);
264 * Output both official and peronal.
266 * @param array $submitValues values submitted.
267 * @param bool $assoc specifies if returned array is associative
268 * @return array
270 function exportValue(&$submitValues, $assoc = false) {
271 $valuearray = array();
273 // Get the data out of our child elements.
274 foreach ($this->_elements as $element){
275 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
276 if ($thisexport != null){
277 $valuearray += $thisexport;
281 // Get any manually typed tags.
282 $tags = array();
283 if ($this->_options['display'] != MoodleQuickForm_tags::ONLYOFFICIAL &&
284 !empty($valuearray['othertags'])) {
285 $rawtags = explode(',', clean_param($valuearray['othertags'], PARAM_NOTAGS));
286 foreach ($rawtags as $tag) {
287 $tags[] = trim($tag);
291 // Add any official tags that were selected.
292 if ($this->_options['display'] != MoodleQuickForm_tags::NOOFFICIAL &&
293 !empty($valuearray['officialtags'])) {
294 $tags = array_unique(array_merge($tags, $valuearray['officialtags']));
297 return array($this->getName() => $tags);